mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Thomas Gleixner <tglx@linutronix.de>
Cc: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 3/3] group_cpus: simplify inner loop in grp_spread_init_one()
Date: Tue, 18 Nov 2025 22:13:05 -0500	[thread overview]
Message-ID: <20251119031306.644129-4-yury.norov@gmail.com> (raw)
In-Reply-To: <20251119031306.644129-1-yury.norov@gmail.com>

Three optimizations for grp_spread_init_one().

1. Drop most of housekeeping code in grp_spread_init_one() with
   for_each_cpu_and_andnot_from().

2. Fix Shlemiel the Painter's algorithm by adding 'sibl = cpu' line. This
   improves the grp_spread_init_one() complexity from quadratic to linear.

3. Don't clear the nmsk because it's rewritten in the caller code anyways,
   and switch to non-atomic bit setter for irqmsk as the mask is local
   and implies no concurrency.

Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
 lib/group_cpus.c | 25 ++++++-------------------
 1 file changed, 6 insertions(+), 19 deletions(-)

diff --git a/lib/group_cpus.c b/lib/group_cpus.c
index 6aae1560b796..35aba99d8cd0 100644
--- a/lib/group_cpus.c
+++ b/lib/group_cpus.c
@@ -17,27 +17,14 @@ static void grp_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
 	const struct cpumask *siblmsk;
 	int cpu, sibl;
 
-	for ( ; cpus_per_grp > 0; ) {
-		cpu = cpumask_first(nmsk);
-
-		/* Should not happen, but I'm too lazy to think about it */
-		if (cpu >= nr_cpu_ids)
-			return;
-
-		cpumask_clear_cpu(cpu, nmsk);
-		cpumask_set_cpu(cpu, irqmsk);
-		cpus_per_grp--;
-
+	for_each_cpu(cpu, nmsk) {
 		/* 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)
-				break;
-			if (!cpumask_test_and_clear_cpu(sibl, nmsk))
-				continue;
-			cpumask_set_cpu(sibl, irqmsk);
-			cpus_per_grp--;
+		sibl = cpu;
+		for_each_cpu_and_andnot_from(sibl, nmsk, siblmsk, irqmsk) {
+			__cpumask_set_cpu(sibl, irqmsk);
+			if (--cpus_per_grp)
+				return;
 		}
 	}
 }
-- 
2.43.0


  parent reply	other threads:[~2025-11-19  3:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-19  3:13 [PATCH 0/3] improve group_cpus initialization routines Yury Norov (NVIDIA)
2025-11-19  3:13 ` [PATCH 1/3] bitmap: cpumask: introduce and_andnot search helper and iterator Yury Norov (NVIDIA)
2025-11-19  3:13 ` [PATCH 2/3] group_cpus: don't call cpumask_weight() prematurely Yury Norov (NVIDIA)
2025-11-19  3:13 ` Yury Norov (NVIDIA) [this message]
2025-11-28  2:25 ` [PATCH 0/3] improve group_cpus initialization routines Yury Norov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251119031306.644129-4-yury.norov@gmail.com \
    --to=yury.norov@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®