From: Yury Norov <yury.norov@gmail.com>
To: linux-kernel@vger.kernel.org,
"Alexander Lobakin" <alexandr.lobakin@intel.com>,
"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"Arnd Bergmann" <arnd@arndb.de>,
"David Gow" <davidgow@google.com>,
"Eric Dumazet" <edumazet@google.com>,
"Isabella Basso" <isabbasso@riseup.net>,
"Kees Cook" <keescook@chromium.org>,
"Keith Busch" <kbusch@kernel.org>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Marco Elver" <elver@google.com>,
"Mark Rutland" <mark.rutland@arm.com>,
"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
"Steven Rostedt" <rostedt@goodmis.org>,
"Toke Høiland-Jørgensen" <toke@redhat.com>,
"Valentin Schneider" <vschneid@redhat.com>
Cc: Yury Norov <yury.norov@gmail.com>
Subject: [PATCH v3 6/6] cpumask: add cpumask_nth_{,and,andnot}
Date: Sat, 17 Sep 2022 20:07:16 -0700 [thread overview]
Message-ID: <20220918030716.1252285-7-yury.norov@gmail.com> (raw)
In-Reply-To: <20220918030716.1252285-1-yury.norov@gmail.com>
Add cpumask_nth_{,and,andnot} as wrappers around corresponding
find functions, and use it in cpumask_local_spread().
Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
include/linux/cpumask.h | 44 +++++++++++++++++++++++++++++++++++++++++
lib/cpumask.c | 28 ++++++++++++--------------
2 files changed, 57 insertions(+), 15 deletions(-)
diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 31bf08c786e3..621b59820561 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -337,6 +337,50 @@ unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
return i;
}
+/**
+ * cpumask_nth - get the first cpu in a cpumask
+ * @srcp: the cpumask pointer
+ * @cpu: the N'th cpu to find, starting from 0
+ *
+ * Returns >= nr_cpu_ids if such cpu doesn't exist.
+ */
+static inline unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp)
+{
+ return find_nth_bit(cpumask_bits(srcp), nr_cpumask_bits, cpumask_check(cpu));
+}
+
+/**
+ * cpumask_nth_and - get the first cpu in 2 cpumasks
+ * @srcp1: the cpumask pointer
+ * @srcp2: the cpumask pointer
+ * @cpu: the N'th cpu to find, starting from 0
+ *
+ * Returns >= nr_cpu_ids if such cpu doesn't exist.
+ */
+static inline
+unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1,
+ const struct cpumask *srcp2)
+{
+ return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
+ nr_cpumask_bits, cpumask_check(cpu));
+}
+
+/**
+ * cpumask_nth_andnot - get the first cpu set in 1st cpumask, and clear in 2nd.
+ * @srcp1: the cpumask pointer
+ * @srcp2: the cpumask pointer
+ * @cpu: the N'th cpu to find, starting from 0
+ *
+ * Returns >= nr_cpu_ids if such cpu doesn't exist.
+ */
+static inline
+unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1,
+ const struct cpumask *srcp2)
+{
+ return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
+ nr_cpumask_bits, cpumask_check(cpu));
+}
+
#define CPU_BITS_NONE \
{ \
[0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
diff --git a/lib/cpumask.c b/lib/cpumask.c
index f0ae119be8c4..2c4a63b6f03f 100644
--- a/lib/cpumask.c
+++ b/lib/cpumask.c
@@ -128,23 +128,21 @@ unsigned int cpumask_local_spread(unsigned int i, int node)
i %= num_online_cpus();
if (node == NUMA_NO_NODE) {
- for_each_cpu(cpu, cpu_online_mask)
- if (i-- == 0)
- return cpu;
+ cpu = cpumask_nth(i, cpu_online_mask);
+ if (cpu < nr_cpu_ids)
+ return cpu;
} else {
/* NUMA first. */
- for_each_cpu_and(cpu, cpumask_of_node(node), cpu_online_mask)
- if (i-- == 0)
- return cpu;
-
- for_each_cpu(cpu, cpu_online_mask) {
- /* Skip NUMA nodes, done above. */
- if (cpumask_test_cpu(cpu, cpumask_of_node(node)))
- continue;
-
- if (i-- == 0)
- return cpu;
- }
+ cpu = cpumask_nth_and(i, cpu_online_mask, cpumask_of_node(node));
+ if (cpu < nr_cpu_ids)
+ return cpu;
+
+ i -= cpumask_weight_and(cpu_online_mask, cpumask_of_node(node));
+
+ /* Skip NUMA nodes, done above. */
+ cpu = cpumask_nth_andnot(i, cpu_online_mask, cpumask_of_node(node));
+ if (cpu < nr_cpu_ids)
+ return cpu;
}
BUG();
}
--
2.34.1
next prev parent reply other threads:[~2022-09-18 3:08 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-18 3:07 [PATCH v3 0/5] lib/find: add find_nth_bit() Yury Norov
2022-09-18 3:07 ` [PATCH v3 1/6] lib/bitmap: don't call __bitmap_weight() in kernel code Yury Norov
2022-09-18 3:07 ` [PATCH v3 2/6] lib/bitmap: add bitmap_weight_and() Yury Norov
2022-09-18 3:07 ` [PATCH v3 3/6] lib: add find_nth{,_and,_andnot}_bit() Yury Norov
2022-09-18 3:07 ` [PATCH v3 4/6] lib/bitmap: add tests for find_nth_bit() Yury Norov
2022-09-18 3:07 ` [PATCH v3 5/6] lib/bitmap: remove bitmap_ord_to_pos Yury Norov
2022-09-18 3:07 ` Yury Norov [this message]
2022-09-23 16:18 ` [PATCH v3 0/5] lib/find: add find_nth_bit() Yury Norov
2022-09-26 19:17 ` 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=20220918030716.1252285-7-yury.norov@gmail.com \
--to=yury.norov@gmail.com \
--cc=alexandr.lobakin@intel.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=arnd@arndb.de \
--cc=davidgow@google.com \
--cc=edumazet@google.com \
--cc=elver@google.com \
--cc=isabbasso@riseup.net \
--cc=kbusch@kernel.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mark.rutland@arm.com \
--cc=memxor@gmail.com \
--cc=rostedt@goodmis.org \
--cc=toke@redhat.com \
--cc=vschneid@redhat.com \
/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®