Skip to contentSkip to conversation

Add and use get_cpu_cacheinfo_level()

PATCH Superseded Tony Luck 2 participants 6 messages Jun 5, 2024 – Jun 6, 2024

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tony Luck

author maintainer
#0 lore raw

v2 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 v1:

1: s/for cache/for the cache/ in comment for get_cpu_cacheinfo_level() 2: No change
3: Fixed broken firtree declaration order in rdtgroup_cbm_to_size()

Added Reinette's "Reviewed-by:" tag to all three.

Tony Luck (3):
cacheinfo: Add function to get cacheinfo for a given (cpu, cachelevel) x86/resctrl: Replace open code cacheinfo search in
pseudo_lock_region_init()
x86/resctrl: Replace open code cacheinfo search in
rdtgroup_cbm_to_size()

 include/linux/cacheinfo.h                 | 22 +++++++++++++++++-----
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 17 ++++++-----------
 arch/x86/kernel/cpu/resctrl/rdtgroup.c    | 14 +++++---------
 3 files changed, 28 insertions(+), 25 deletions(-)

base-commit: c3f38fa61af77b49866b006939479069cd451173

Signature · 1 line
2.45.0
author maintainer
#1 lore raw

PATCH v2 1/3 cacheinfo: Add function to get cacheinfo for a given (cpu, cachelevel)

Resctrl 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.

Simplify the existing get_cpu_cacheinfo_id() by using this new function to do the search.

---
 include/linux/cacheinfo.h | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
include/linux/cacheinfo.h +17 -5
index 2cb15fe4fe12..b4d99052d186 100644
@@ -113,10 +113,11 @@ 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;
@@ -124,12 +125,23 @@ static inline int get_cpu_cacheinfo_id(int cpu, int level)
 	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
maintainer
#2 lore raw

Re: [PATCH v2 1/3] cacheinfo: Add function to get cacheinfo for a given (cpu, cachelevel)

include/linux/cacheinfo.h static inline int get_cpu_cacheinfo_id(int cpu, int level)

Quoted 12 lines · Tony Luck

/*
- * 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.
*/

include/linux/cacheinfo.h +1 -1
-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;
@@ -124,12 +125,23 @@ static inline int get_cpu_cacheinfo_id(int cpu, int level)

So:

lockdep_assert_cpus_held()

?

Quoted 13 lines
include/linux/cacheinfo.h +4 -3
 	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;
+}
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author maintainer
#3 lore raw

PATCH v2 2/3 x86/resctrl: Replace open code cacheinfo search in pseudo_lock_region_init()

In reply to Tony Luck

Use get_cpu_cacheinfo_level() instead of open coded search.

---
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +6 -11
index aacf236dfe3b..1bbfd3c1e300 100644
@@ -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;
Signature · 1 line
2.45.0
author maintainer
#4 lore raw

PATCH v2 3/3 x86/resctrl: Replace open code cacheinfo search in rdtgroup_cbm_to_size()

In reply to Tony Luck

Use get_cpu_cacheinfo_level() instead of open coded search.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +5 -9
index 02f213f1c51c..cb68a121dabb 100644
@@ -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
maintainer
#5 lore raw

Re: [PATCH v2 3/3] x86/resctrl: Replace open code cacheinfo search in rdtgroup_cbm_to_size()

Quoted 27 lines · Tony Luck

Use get_cpu_cacheinfo_level() instead of open coded search.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -5
index 02f213f1c51c..cb68a121dabb 100644
@@ -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) {

arch/x86/kernel/cpu/resctrl/rdtgroup.c static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of, · line 1463

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -4
-			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;
 }

Those last two patches can be a single one which replaces open-coded cacheinfo search in resctrl.

Or is there any particular reason for them to be separate?

Because it is a single logical change...

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette