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 2/3] group_cpus: don't call cpumask_weight() prematurely
Date: Tue, 18 Nov 2025 22:13:04 -0500 [thread overview]
Message-ID: <20251119031306.644129-3-yury.norov@gmail.com> (raw)
In-Reply-To: <20251119031306.644129-1-yury.norov@gmail.com>
alloc_nodes_groups() and __group_cpus_evenly() call cpumask_weight()
unconditionally in the for_each() loops. cpumask_weight() is O(N), so
the complexity of the functions become O(MAX_NUMNODES * nr_cpu_ids).
This call may be avoided if the nmsk is empty.
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
lib/group_cpus.c | 17 ++++++-----------
1 file changed, 6 insertions(+), 11 deletions(-)
diff --git a/lib/group_cpus.c b/lib/group_cpus.c
index 6d08ac05f371..6aae1560b796 100644
--- a/lib/group_cpus.c
+++ b/lib/group_cpus.c
@@ -142,15 +142,11 @@ static void alloc_nodes_groups(unsigned int numgrps,
}
for_each_node_mask(n, nodemsk) {
- unsigned ncpus;
-
- cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
- ncpus = cpumask_weight(nmsk);
-
- if (!ncpus)
+ if (!cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]))
continue;
- remaining_ncpus += ncpus;
- node_groups[n].ncpus = ncpus;
+
+ node_groups[n].ncpus = cpumask_weight(nmsk);
+ remaining_ncpus += node_groups[n].ncpus;
}
numgrps = min_t(unsigned, remaining_ncpus, numgrps);
@@ -294,11 +290,10 @@ static int __group_cpus_evenly(unsigned int startgrp, unsigned int numgrps,
continue;
/* Get the cpus on this node which are in the mask */
- cpumask_and(nmsk, cpu_mask, node_to_cpumask[nv->id]);
- ncpus = cpumask_weight(nmsk);
- if (!ncpus)
+ if (!cpumask_and(nmsk, cpu_mask, node_to_cpumask[nv->id]))
continue;
+ ncpus = cpumask_weight(nmsk);
WARN_ON_ONCE(nv->ngroups > ncpus);
/* Account for rounding errors */
--
2.43.0
next prev parent reply other threads:[~2025-11-19 3:13 UTC|newest]
Thread overview: 6+ 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 ` Yury Norov (NVIDIA) [this message]
2025-11-19 3:13 ` [PATCH 3/3] group_cpus: simplify inner loop in grp_spread_init_one() Yury Norov (NVIDIA)
2025-11-28 2:25 ` [PATCH 0/3] improve group_cpus initialization routines Yury Norov
-- strict thread matches above, loose matches on Subject: below --
2025-09-10 21:08 Yury Norov
2025-09-10 21:08 ` [PATCH 2/3] group_cpus: don't call cpumask_weight() prematurely 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-3-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®