Skip to contentSkip to conversation

percpu: reduce the time complexity of mapping units and cpus

PATCH In review Hyunmin Lee 2 participants 2 messages Jun 13, 2025 – Jun 19, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 percpu: reduce the time complexity of mapping units and cpus

For mapping units and CPUs belonging to groups, it can be inefficient to iterate through all CPUs by each group to find what CPUs belong to that group.

Since group_map already has the information on which CPUs belong to which group, CPUs can be directly mapped to a unit in the group to which the CPU belongs.

---
 mm/percpu.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
mm/percpu.c +7 -3
index b35494c8ede2..968aa0ace482 100644
@@ -2906,6 +2906,13 @@ static struct pcpu_alloc_info * __init __flatten pcpu_build_alloc_info(
 	ai->atom_size = atom_size;
 	ai->alloc_size = alloc_size;
 
+	for_each_possible_cpu(cpu) {
+		group = group_map[cpu];
+		struct pcpu_group_info *gi = &ai->groups[group];
+
+		gi->cpu_map[gi->nr_units++] = cpu;
+	}
+
 	for (group = 0, unit = 0; group < nr_groups; group++) {
 		struct pcpu_group_info *gi = &ai->groups[group];
 
@@ -2916,9 +2923,6 @@ static struct pcpu_alloc_info * __init __flatten pcpu_build_alloc_info(
 		 */
 		gi->base_offset = unit * ai->unit_size;
 
-		for_each_possible_cpu(cpu)
-			if (group_map[cpu] == group)
-				gi->cpu_map[gi->nr_units++] = cpu;
 		gi->nr_units = roundup(gi->nr_units, upa);
 		unit += gi->nr_units;
 	}
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hello,

Quoted 8 lines · Hyunmin Lee

For mapping units and CPUs belonging to groups, it can be inefficient to iterate through all CPUs by each group to find what CPUs belong to that group.

Since group_map already has the information on which CPUs belong to which group, CPUs can be directly mapped to a unit in the group to which the CPU belongs.

Idk. For any single socket machine, it's even money. For large machines with say 4+ numa nodes, you're not really going to notice the microseconds the extra for loop is going to take.

I'm neutral but inclined to prefer what's already there.

Quoted 10 lines
---
 mm/percpu.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
mm/percpu.c +0 -0
index b35494c8ede2..968aa0ace482 100644

mm/percpu.c static struct pcpu_alloc_info * __init __flatten pcpu_build_alloc_info( · line 2915

Quoted 10 lines
mm/percpu.c +7 -0
@@ -2906,6 +2906,13 @@ static struct pcpu_alloc_info * __init __flatten pcpu_build_alloc_info(
 	ai->atom_size = atom_size;
 	ai->alloc_size = alloc_size;
 
+	for_each_possible_cpu(cpu) {
+		group = group_map[cpu];
+		struct pcpu_group_info *gi = &ai->groups[group];
+
+		gi->cpu_map[gi->nr_units++] = cpu;
+	}
+

Nit: the struct declaration goes at the top in the block.

for_each_possible_cpu(cpu) {
    struct pcpu_group_info *gi;

    group = group_map[cpu];
    gi = &ai->groups[group];

    gi->cpu_map[gi->nr_units++] = cpu;
}

Or condense it into a single line:

for_each_possible_cpu(cpu) {
    struct pcpu_group_info *gi = &ai->groups[group_map[cpu]];

    gi->cpu_map[gi->nr_units++] = cpu;
}
Quoted 16 lines
mm/percpu.c +0 -3
 	for (group = 0, unit = 0; group < nr_groups; group++) {
 		struct pcpu_group_info *gi = &ai->groups[group];
 
@@ -2916,9 +2923,6 @@ static struct pcpu_alloc_info * __init __flatten pcpu_build_alloc_info(
 		 */
 		gi->base_offset = unit * ai->unit_size;
 
-		for_each_possible_cpu(cpu)
-			if (group_map[cpu] == group)
-				gi->cpu_map[gi->nr_units++] = cpu;
 		gi->nr_units = roundup(gi->nr_units, upa);
 		unit += gi->nr_units;
 	}
Signature · 1 line
2.43.0

Thanks,
Dennis