Skip to contentSkip to conversation

lib/group_cpus.c: simplify grp_spread_init_one()

PATCH New Ming Lei 1 participant 1 message Jan 20, 2024

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 lib/group_cpus.c: simplify grp_spread_init_one()

What the inner loop needs to do is to assign each cpu in siblmsk & nmsk.

Clean it by using cpumask_next_and(), meantime add helper of grp_assign_cpu().

So grp_spread_init_one() becomes more readable now.

Cc Yury Norov <[email protected]>
Cc Thomas Gleixner <[email protected]>

---

Hello Andrew,

Please consider to use this one to replace the 1st, 2nd & 4th patches in Yury's V5:

- avoid to add new kernel API
- avoid to update iterator variable inside loop, which is tricky
- fix bug in the 4th patch
- add grp_assign_cpu() to make code more readable & clean

 lib/group_cpus.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
lib/group_cpus.c +19 -15
index ee272c4cefcc..6cbc7379d954 100644
@@ -11,32 +11,36 @@
 
 #ifdef CONFIG_SMP
 
+static bool grp_assign_cpu(int cpu, struct cpumask *irqmsk,
+		struct cpumask *nmsk)
+{
+	if (cpu >= nr_cpu_ids)
+		return false;
+
+	cpumask_clear_cpu(cpu, nmsk);
+	cpumask_set_cpu(cpu, irqmsk);
+	return true;
+}
+
 static void grp_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
 				unsigned int cpus_per_grp)
 {
 	const struct cpumask *siblmsk;
-	int cpu, sibl;
-
-	for ( ; cpus_per_grp > 0; ) {
-		cpu = cpumask_first(nmsk);
+	int cpu = -1, sibl;
 
-		/* Should not happen, but I'm too lazy to think about it */
-		if (cpu >= nr_cpu_ids)
+	while (cpus_per_grp > 0) {
+		cpu = cpumask_next(cpu, nmsk);
+		if (!grp_assign_cpu(cpu, irqmsk, nmsk))
 			return;
-
-		cpumask_clear_cpu(cpu, nmsk);
-		cpumask_set_cpu(cpu, irqmsk);
 		cpus_per_grp--;
 
 		/* If the cpu has siblings, use them first */
 		siblmsk = topology_sibling_cpumask(cpu);
-		for (sibl = -1; cpus_per_grp > 0; ) {
-			sibl = cpumask_next(sibl, siblmsk);
-			if (sibl >= nr_cpu_ids)
+		sibl = cpu;
+		while (cpus_per_grp > 0) {
+			sibl = cpumask_next_and(sibl, siblmsk, nmsk);
+			if (!grp_assign_cpu(sibl, irqmsk, nmsk))
 				break;
-			if (!cpumask_test_and_clear_cpu(sibl, nmsk))
-				continue;
-			cpumask_set_cpu(sibl, irqmsk);
 			cpus_per_grp--;
 		}
 	}
Signature · 1 line
2.41.0