mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Neel Natu <neelnatu@google.com>
To: Yury Norov <yury.norov@gmail.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Peter Zijlstra <peterz@infradead.org>,
	linux-kernel@vger.kernel.org
Cc: Neel Natu <neelnatu@google.com>
Subject: [PATCH] sched, cpumask: don't leak impossible cpus via for_each_cpu_wrap().
Date: Tue,  2 Aug 2022 14:40:41 -0700	[thread overview]
Message-ID: <20220802214041.2656586-1-neelnatu@google.com> (raw)

The value of 'nr_cpumask_bits' is dependent on CONFIG_CPUMASK_OFFSTACK.
This in turn can change the set of cpus visited by for_each_cpu_wrap()
with a mask that has bits set in the range [nr_cpu_ids, NR_CPUS).

Specifically on !CONFIG_CPUMASK_OFFSTACK kernels the API can iterate
over cpus outside the 'cpu_possible_mask'.

Fix this to make its behavior match for_each_cpu() which always limits
the iteration to the range [0, nr_cpu_ids).

Signed-off-by: Neel Natu <neelnatu@google.com>
---
 include/linux/cpumask.h | 2 +-
 lib/cpumask.c           | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index fe29ac7cc469..2a308cfc43da 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -303,7 +303,7 @@ extern int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool
  */
 #define for_each_cpu_wrap(cpu, mask, start)					\
 	for ((cpu) = cpumask_next_wrap((start)-1, (mask), (start), false);	\
-	     (cpu) < nr_cpumask_bits;						\
+	     (cpu) < nr_cpu_ids;						\
 	     (cpu) = cpumask_next_wrap((cpu), (mask), (start), true))
 
 /**
diff --git a/lib/cpumask.c b/lib/cpumask.c
index a971a82d2f43..d47937fb49eb 100644
--- a/lib/cpumask.c
+++ b/lib/cpumask.c
@@ -82,9 +82,9 @@ int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap)
 	next = cpumask_next(n, mask);
 
 	if (wrap && n < start && next >= start) {
-		return nr_cpumask_bits;
+		return nr_cpu_ids;
 
-	} else if (next >= nr_cpumask_bits) {
+	} else if (next >= nr_cpu_ids) {
 		wrap = true;
 		n = -1;
 		goto again;
-- 
2.37.1.455.g008518b4e5-goog


             reply	other threads:[~2022-08-02 21:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-02 21:40 Neel Natu [this message]
2022-08-03  1:22 ` Yury Norov
2022-08-03 17:49   ` Neel Natu
2022-08-03 18:52     ` Yury Norov
2022-08-03 21:21       ` Neel Natu
2022-08-03 23:51         ` 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=20220802214041.2656586-1-neelnatu@google.com \
    --to=neelnatu@google.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=peterz@infradead.org \
    --cc=yury.norov@gmail.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®