Add and use get_cpu_cacheinfo_level()
Subsystems (3)
Tony Luck
maintainerv4 cover Add and use get_cpu_cacheinfo_level()
This helper function came up in discussion of the resctrl patches for Sub-NUMA Cluster (SNC) support. Reinette pointed out[1] that there are already two places where it would clean things up by avoiding open coding. The SNC patches will add two additional call sites.
So rather than have this jammed up as part of the SNC series, I'm posting it as a simple standalone cleanup.
[1] https://lore.kernel.org/all/[email protected]/
---
Changes since v3:
Addition of lockdep_assert_cpus_held() in v3 led to #include hell because <linux/cacheinfo.h> needed <linux/cpu.h>
Patch 1 resolves this hell by cut-and-pasting all the CPU hotplug defintions out of cpu.h and into a new file cpuhplock.h.
Patch 2 brings the pasted code up to current coding standards
Patch 3 is the previous patch 1, but adds <linux/cpuhplock.h> instead of <linux/cpu.h> to <linux/cacheinfo.h>
Patch 4 is the previous patch 2, unchanged.
Tony Luck (4):
cpu: Move CPU hotplug function declarations into their own header
cpu: Drop "extern" from function declarations in cpuhplock.h
cacheinfo: Add function to get cacheinfo for a given (cpu, cachelevel) x86/resctrl: Replace open code cacheinfo searches
include/linux/cacheinfo.h | 25 +++++++++--- include/linux/cpu.h | 33 +--------------- include/linux/cpuhplock.h | 47 +++++++++++++++++++++++ arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 17 +++----- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 14 +++---- 5 files changed, 79 insertions(+), 57 deletions(-) create mode 100644 include/linux/cpuhplock.h
base-commit: 83a7eefedc9b56fe7bfeff13b6c7356688ffa670
Signature · 1 line
2.45.0
Tony Luck
maintainerPATCH v4 1/4 cpu: Move CPU hotplug function declarations into their own header
Avoid upcoming #include hell when <linux/cachinfo.h> wants to use lockdep_assert_cpus_held() and creates a #include loop that would break the build for arch/riscv.
--- include/linux/cpu.h | 33 +-------------------------- include/linux/cpuhplock.h | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 include/linux/cpuhplock.h
include/linux/cpu.h +1 -32
@@ -18,6 +18,7 @@#include <linux/compiler.h> #include <linux/cpumask.h> #include <linux/cpuhotplug.h> +#include <linux/cpuhplock.h> #include <linux/cpu_smt.h> struct device ;
@@ -132,38 +133,6 @@ static inline int add_cpu(unsigned int cpu) { return 0;}#endif /* CONFIG_SMP */ extern const struct bus_type cpu_subsys; -extern int lockdep_is_cpus_held (void ); - -#ifdef CONFIG_HOTPLUG_CPU -extern void cpus_write_lock (void ); -extern void cpus_write_unlock (void ); -extern void cpus_read_lock (void ); -extern void cpus_read_unlock (void ); -extern int cpus_read_trylock (void ); -extern void lockdep_assert_cpus_held (void ); -extern void cpu_hotplug_disable (void ); -extern void cpu_hotplug_enable (void ); -void clear_tasks_mm_cpumask (int cpu); -int remove_cpu (unsigned int cpu); -int cpu_device_down (struct device *dev); -extern void smp_shutdown_nonboot_cpus (unsigned int primary_cpu); - -#else /* CONFIG_HOTPLUG_CPU */ - -static inline void cpus_write_lock (void ) { } -static inline void cpus_write_unlock (void ) { } -static inline void cpus_read_lock (void ) { } -static inline void cpus_read_unlock (void ) { } -static inline int cpus_read_trylock (void ) {return true; } -static inline void lockdep_assert_cpus_held (void ) { } -static inline void cpu_hotplug_disable (void ) { } -static inline void cpu_hotplug_enable (void ) { } -static inline int remove_cpu (unsigned int cpu) {return -EPERM ; } -static inline void smp_shutdown_nonboot_cpus (unsigned int primary_cpu) { } -#endif /* !CONFIG_HOTPLUG_CPU */ - -DEFINE_LOCK_GUARD_0(cpus_read_lock, cpus_read_lock(), cpus_read_unlock()) -#ifdef CONFIG_PM_SLEEP_SMP extern int freeze_secondary_cpus (int primary);extern void thaw_secondary_cpus (void );
include/linux/cpuhplock.h new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.45.0
[tip: x86/cache] cpu: Move CPU hotplug function declarations into their own header
The following commit has been merged into the x86/cache branch of tip:
The following commit has been merged into the x86/cache branch of tip:
Commit-ID: 195fb517ee25bfefde9c74ecd86348eccbd6d2e4 Gitweb: https://git.kernel.org/tip/195fb517ee25bfefde9c74ecd86348eccbd6d2e4 Author: Tony Luck <[email protected]> AuthorDate: Sun, 09 Jun 2024 17:39:24 -07:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 10 Jun 2024 08:50:01 +02:00
cpu: Move CPU hotplug function declarations into their own header
Avoid upcoming #include hell when <linux/cachinfo.h> wants to use lockdep_assert_cpus_held() and creates a #include loop that would break the build for arch/riscv.
[ bp: s/cpu/CPU/g ]
--- include/linux/cpu.h | 33 +--------------------------- include/linux/cpuhplock.h | 47 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 include/linux/cpuhplock.h
include/linux/cpu.h +1 -32
@@ -18,6 +18,7 @@#include <linux/compiler.h> #include <linux/cpumask.h> #include <linux/cpuhotplug.h> +#include <linux/cpuhplock.h> #include <linux/cpu_smt.h> struct device ;
@@ -132,38 +133,6 @@ static inline int add_cpu(unsigned int cpu) { return 0;}#endif /* CONFIG_SMP */ extern const struct bus_type cpu_subsys; -extern int lockdep_is_cpus_held (void ); - -#ifdef CONFIG_HOTPLUG_CPU -extern void cpus_write_lock (void ); -extern void cpus_write_unlock (void ); -extern void cpus_read_lock (void ); -extern void cpus_read_unlock (void ); -extern int cpus_read_trylock (void ); -extern void lockdep_assert_cpus_held (void ); -extern void cpu_hotplug_disable (void ); -extern void cpu_hotplug_enable (void ); -void clear_tasks_mm_cpumask (int cpu); -int remove_cpu (unsigned int cpu); -int cpu_device_down (struct device *dev); -extern void smp_shutdown_nonboot_cpus (unsigned int primary_cpu); - -#else /* CONFIG_HOTPLUG_CPU */ - -static inline void cpus_write_lock (void ) { } -static inline void cpus_write_unlock (void ) { } -static inline void cpus_read_lock (void ) { } -static inline void cpus_read_unlock (void ) { } -static inline int cpus_read_trylock (void ) {return true; } -static inline void lockdep_assert_cpus_held (void ) { } -static inline void cpu_hotplug_disable (void ) { } -static inline void cpu_hotplug_enable (void ) { } -static inline int remove_cpu (unsigned int cpu) {return -EPERM ; } -static inline void smp_shutdown_nonboot_cpus (unsigned int primary_cpu) { } -#endif /* !CONFIG_HOTPLUG_CPU */ - -DEFINE_LOCK_GUARD_0(cpus_read_lock, cpus_read_lock(), cpus_read_unlock()) -#ifdef CONFIG_PM_SLEEP_SMP extern int freeze_secondary_cpus (int primary);extern void thaw_secondary_cpus (void );
include/linux/cpuhplock.h new file +47 -0
47 lines not on this page · raw message
Tony Luck
maintainerPATCH v4 2/4 cpu: Drop "extern" from function declarations in cpuhplock.h
In reply to Tony LuckThis file was created with a direct cut and paste from cpu.h so kept the legacy declaration style.
But the Linux coding standard for function declarations in header files is to avoid use of "extern".
Drop "extern" from all function declarations.
--- include/linux/cpuhplock.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/cpuhplock.h +9 -9
@@ -15,18 +15,18 @@ struct device;extern int lockdep_is_cpus_held (void );#ifdef CONFIG_HOTPLUG_CPU -extern void cpus_write_lock (void ); -extern void cpus_write_unlock (void ); -extern void cpus_read_lock (void ); -extern void cpus_read_unlock (void ); -extern int cpus_read_trylock (void ); -extern void lockdep_assert_cpus_held (void ); -extern void cpu_hotplug_disable (void ); -extern void cpu_hotplug_enable (void ); +void cpus_write_lock (void ); +void cpus_write_unlock (void ); +void cpus_read_lock (void ); +void cpus_read_unlock (void ); +int cpus_read_trylock (void ); +void lockdep_assert_cpus_held (void ); +void cpu_hotplug_disable (void ); +void cpu_hotplug_enable (void );void clear_tasks_mm_cpumask (int cpu);int remove_cpu (unsigned int cpu);int cpu_device_down (struct device *dev); -extern void smp_shutdown_nonboot_cpus (unsigned int primary_cpu); +void smp_shutdown_nonboot_cpus (unsigned int primary_cpu);#else /* CONFIG_HOTPLUG_CPU */
Signature · 1 line
2.45.0
[tip: x86/cache] cpu: Drop "extern" from function declarations in cpuhplock.h
The following commit has been merged into the x86/cache branch of tip:
The following commit has been merged into the x86/cache branch of tip:
Commit-ID: ddefcfdeb5a2238cbcb07b80dda9ac3136735b1e Gitweb: https://git.kernel.org/tip/ddefcfdeb5a2238cbcb07b80dda9ac3136735b1e Author: Tony Luck <[email protected]> AuthorDate: Sun, 09 Jun 2024 17:39:25 -07:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 10 Jun 2024 08:50:05 +02:00
cpu: Drop "extern" from function declarations in cpuhplock.h
This file was created with a direct cut and paste from cpu.h so kept the legacy declaration style.
But the Linux coding standard for function declarations in header files is to avoid use of "extern".
Drop "extern" from all function declarations.
--- include/linux/cpuhplock.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/cpuhplock.h +9 -9
@@ -15,18 +15,18 @@ struct device;extern int lockdep_is_cpus_held (void );#ifdef CONFIG_HOTPLUG_CPU -extern void cpus_write_lock (void ); -extern void cpus_write_unlock (void ); -extern void cpus_read_lock (void ); -extern void cpus_read_unlock (void ); -extern int cpus_read_trylock (void ); -extern void lockdep_assert_cpus_held (void ); -extern void cpu_hotplug_disable (void ); -extern void cpu_hotplug_enable (void ); +void cpus_write_lock (void ); +void cpus_write_unlock (void ); +void cpus_read_lock (void ); +void cpus_read_unlock (void ); +int cpus_read_trylock (void ); +void lockdep_assert_cpus_held (void ); +void cpu_hotplug_disable (void ); +void cpu_hotplug_enable (void );void clear_tasks_mm_cpumask (int cpu);int remove_cpu (unsigned int cpu);int cpu_device_down (struct device *dev); -extern void smp_shutdown_nonboot_cpus (unsigned int primary_cpu); +void smp_shutdown_nonboot_cpus (unsigned int primary_cpu);#else /* CONFIG_HOTPLUG_CPU */
Tony Luck
maintainerPATCH v4 3/4 cacheinfo: Add function to get cacheinfo for a given (cpu, cachelevel)
In reply to Tony LuckResctrl code open codes a search for information about a given cache level in a couple of places (and more are on the way).
Provide a new inline function get_cpu_cacheinfo_level() in <linux/cacheinfo.h> to do the search and return a pointer to the cacheinfo structure.
Add lockdep_assert_cpus_held() to enforce the comment that cpuhp lock must be held.
Simplify the existing get_cpu_cacheinfo_id() by using this new function to do the search.
--- include/linux/cacheinfo.h | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
include/linux/cacheinfo.h +20 -5
@@ -3,6 +3,7 @@#define _LINUX_CACHEINFO_H#include <linux/bitops.h> +#include <linux/cpuhplock.h> #include <linux/cpumask.h> #include <linux/smp.h>
@@ -113,23 +114,37 @@ int acpi_get_cache_info(unsigned int cpu,const struct attribute_group *cache_get_priv_group (struct cacheinfo *this_leaf);/* -* Get the id of the cache associated with @cpu at level @level. +* Get the cacheinfo structure for the cache associated with @cpu at +* level @level. * cpuhp lock must be held. */ -static inline int get_cpu_cacheinfo_id (int cpu,int level) +static inline struct cacheinfo *get_cpu_cacheinfo_level (int cpu,int level) {struct cpu_cacheinfo *ci =get_cpu_cacheinfo (cpu);int i; +lockdep_assert_cpus_held (); +for (i =0 ; i < ci->num_leaves; i++) {if (ci->info_list[i].level == level) {if (ci->info_list[i].attributes &CACHE_ID ) -return ci->info_list[i].id; -return -1 ; +return &ci->info_list[i]; +return NULL ; } } -return -1 ; +return NULL ; +} + +/* +* Get the id of the cache associated with @cpu at level @level. +* cpuhp lock must be held. +*/ +static inline int get_cpu_cacheinfo_id (int cpu,int level) +{ +struct cacheinfo *ci =get_cpu_cacheinfo_level (cpu, level); + +return ci ? ci->id :-1 ; }#ifdef CONFIG_ARM64
Signature · 1 line
2.45.0
[tip: x86/cache] cacheinfo: Add function to get cacheinfo for a given CPU and cache level
The following commit has been merged into the x86/cache branch of tip:
The following commit has been merged into the x86/cache branch of tip:
Commit-ID: 685cb1674060c2cb1b9da051a12933c082b8e874 Gitweb: https://git.kernel.org/tip/685cb1674060c2cb1b9da051a12933c082b8e874 Author: Tony Luck <[email protected]> AuthorDate: Sun, 09 Jun 2024 17:39:26 -07:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 10 Jun 2024 08:50:09 +02:00
cacheinfo: Add function to get cacheinfo for a given CPU and cache level
Resctrl open codes a search for information about a given cache level in a couple of places (and more are on the way).
Provide a new inline function get_cpu_cacheinfo_level() in <linux/cacheinfo.h> to do the search and return a pointer to the cacheinfo structure.
Add lockdep_assert_cpus_held() to enforce the comment that cpuhp lock must be held.
Simplify the existing get_cpu_cacheinfo_id() by using this new function to do the search.
--- include/linux/cacheinfo.h | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
include/linux/cacheinfo.h +20 -5
@@ -3,6 +3,7 @@#define _LINUX_CACHEINFO_H#include <linux/bitops.h> +#include <linux/cpuhplock.h> #include <linux/cpumask.h> #include <linux/smp.h>
@@ -113,23 +114,37 @@ int acpi_get_cache_info(unsigned int cpu,const struct attribute_group *cache_get_priv_group (struct cacheinfo *this_leaf);/* -* Get the id of the cache associated with @cpu at level @level. +* Get the cacheinfo structure for the cache associated with @cpu at +* level @level. * cpuhp lock must be held. */ -static inline int get_cpu_cacheinfo_id (int cpu,int level) +static inline struct cacheinfo *get_cpu_cacheinfo_level (int cpu,int level) {struct cpu_cacheinfo *ci =get_cpu_cacheinfo (cpu);int i; +lockdep_assert_cpus_held (); +for (i =0 ; i < ci->num_leaves; i++) {if (ci->info_list[i].level == level) {if (ci->info_list[i].attributes &CACHE_ID ) -return ci->info_list[i].id; -return -1 ; +return &ci->info_list[i]; +return NULL ; } } -return -1 ; +return NULL ; +} + +/* +* Get the id of the cache associated with @cpu at level @level. +* cpuhp lock must be held. +*/ +static inline int get_cpu_cacheinfo_id (int cpu,int level) +{ +struct cacheinfo *ci =get_cpu_cacheinfo_level (cpu, level); + +return ci ? ci->id :-1 ; }#ifdef CONFIG_ARM64
Tony Luck
maintainerPATCH v4 4/4 x86/resctrl: Replace open code cacheinfo searches
In reply to Tony Luckpseudo_lock_region_init() and rdtgroup_cbm_to_size() open code a search for details of a particular cache level.
Replace with get_cpu_cacheinfo_level()
--- arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 17 ++++++----------- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 14 +++++--------- 2 files changed, 11 insertions(+), 20 deletions(-)
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +6 -11
@@ -292,9 +292,8 @@ static void pseudo_lock_region_clear(struct pseudo_lock_region *plr)*/ static int pseudo_lock_region_init (struct pseudo_lock_region *plr) { -struct cpu_cacheinfo *ci; +struct cacheinfo *ci;int ret; -int i;/* Pick the first cpu we find that is associated with the cache. */ plr->cpu =cpumask_first (&plr->d->cpu_mask);
@@ -306,15 +305,11 @@ static int pseudo_lock_region_init(struct pseudo_lock_region *plr) goto out_region; } - ci =get_cpu_cacheinfo (plr->cpu); - - plr->size =rdtgroup_cbm_to_size (plr->s->res, plr->d, plr->cbm); - -for (i =0 ; i < ci->num_leaves; i++) { -if (ci->info_list[i].level == plr->s->res->cache_level) { - plr->line_size = ci->info_list[i].coherency_line_size; -return 0 ; - } + ci =get_cpu_cacheinfo_level (plr->cpu, plr->s->res->cache_level); +if (ci) { + plr->line_size = ci->coherency_line_size; + plr->size =rdtgroup_cbm_to_size (plr->s->res, plr->d, plr->cbm); +return 0 ; } ret =-1 ;
arch/x86/kernel/cpu/resctrl/rdtgroup.c +5 -9
@@ -1450,18 +1450,14 @@ static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,unsigned int rdtgroup_cbm_to_size (struct rdt_resource *r,struct rdt_domain *d,unsigned long cbm) { -struct cpu_cacheinfo *ci;unsigned int size =0 ; -int num_b, i; +struct cacheinfo *ci; +int num_b; num_b =bitmap_weight (&cbm, r->cache.cbm_len); - ci =get_cpu_cacheinfo (cpumask_any (&d->cpu_mask)); -for (i =0 ; i < ci->num_leaves; i++) { -if (ci->info_list[i].level == r->cache_level) { - size = ci->info_list[i].size / r->cache.cbm_len * num_b; -break ; - } - } + ci =get_cpu_cacheinfo_level (cpumask_any (&d->cpu_mask), r->cache_level); +if (ci) + size = ci->size / r->cache.cbm_len * num_b;return size; }
Signature · 1 line
2.45.0
[tip: x86/cache] x86/resctrl: Replace open coded cacheinfo searches
The following commit has been merged into the x86/cache branch of tip:
The following commit has been merged into the x86/cache branch of tip:
Commit-ID: f385f024639431bec3e70c33cdbc9563894b3ee5 Gitweb: https://git.kernel.org/tip/f385f024639431bec3e70c33cdbc9563894b3ee5 Author: Tony Luck <[email protected]> AuthorDate: Sun, 09 Jun 2024 17:39:27 -07:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 10 Jun 2024 08:50:12 +02:00
x86/resctrl: Replace open coded cacheinfo searches
pseudo_lock_region_init() and rdtgroup_cbm_to_size() open code a search for details of a particular cache level.
Replace with get_cpu_cacheinfo_level().
--- arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 17 ++++++----------- arch/x86/kernel/cpu/resctrl/rdtgroup.c | 14 +++++--------- 2 files changed, 11 insertions(+), 20 deletions(-)
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +6 -11
@@ -292,9 +292,8 @@ static void pseudo_lock_region_clear(struct pseudo_lock_region *plr)*/ static int pseudo_lock_region_init (struct pseudo_lock_region *plr) { -struct cpu_cacheinfo *ci; +struct cacheinfo *ci;int ret; -int i;/* Pick the first cpu we find that is associated with the cache. */ plr->cpu =cpumask_first (&plr->d->cpu_mask);
@@ -306,15 +305,11 @@ static int pseudo_lock_region_init(struct pseudo_lock_region *plr) goto out_region; } - ci =get_cpu_cacheinfo (plr->cpu); - - plr->size =rdtgroup_cbm_to_size (plr->s->res, plr->d, plr->cbm); - -for (i =0 ; i < ci->num_leaves; i++) { -if (ci->info_list[i].level == plr->s->res->cache_level) { - plr->line_size = ci->info_list[i].coherency_line_size; -return 0 ; - } + ci =get_cpu_cacheinfo_level (plr->cpu, plr->s->res->cache_level); +if (ci) { + plr->line_size = ci->coherency_line_size; + plr->size =rdtgroup_cbm_to_size (plr->s->res, plr->d, plr->cbm); +return 0 ; } ret =-1 ;
arch/x86/kernel/cpu/resctrl/rdtgroup.c +5 -9
@@ -1450,18 +1450,14 @@ out:unsigned int rdtgroup_cbm_to_size (struct rdt_resource *r,struct rdt_domain *d,unsigned long cbm) { -struct cpu_cacheinfo *ci;unsigned int size =0 ; -int num_b, i; +struct cacheinfo *ci; +int num_b; num_b =bitmap_weight (&cbm, r->cache.cbm_len); - ci =get_cpu_cacheinfo (cpumask_any (&d->cpu_mask)); -for (i =0 ; i < ci->num_leaves; i++) { -if (ci->info_list[i].level == r->cache_level) { - size = ci->info_list[i].size / r->cache.cbm_len * num_b; -break ; - } - } + ci =get_cpu_cacheinfo_level (cpumask_any (&d->cpu_mask), r->cache_level); +if (ci) + size = ci->size / r->cache.cbm_len * num_b;return size; }