From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Guenter Roeck <linux@roeck-us.net>,
Geert Uytterhoeven <geert+renesas@glider.be>,
Vernon Yang <vernon2gm@gmail.com>,
Yury Norov <yury.norov@gmail.com>,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
Sasha Levin <sashal@kernel.org>,
mpe@ellerman.id.au, tytso@mit.edu, davem@davemloft.net,
edumazet@google.com, kuba@kernel.org, pabeni@redhat.com,
james.smart@broadcom.com, dick.kennedy@broadcom.com,
jejb@linux.ibm.com, martin.petersen@oracle.com,
christophe.leroy@csgroup.eu, npiggin@gmail.com,
dmitry.osipenko@collabora.com, joel@jms.id.au,
nathanl@linux.ibm.com, gustavoars@kernel.org,
naveen.n.rao@linux.vnet.ibm.com, linuxppc-dev@lists.ozlabs.org,
wireguard@lists.zx2c4.com, netdev@vger.kernel.org,
linux-scsi@vger.kernel.org
Subject: [PATCH AUTOSEL 6.2 09/30] cpumask: fix incorrect cpumask scanning result checks
Date: Sun, 19 Mar 2023 20:52:34 -0400 [thread overview]
Message-ID: <20230320005258.1428043-9-sashal@kernel.org> (raw)
In-Reply-To: <20230320005258.1428043-1-sashal@kernel.org>
From: Linus Torvalds <torvalds@linux-foundation.org>
[ Upstream commit 8ca09d5fa3549d142c2080a72a4c70ce389163cd ]
It turns out that commit 596ff4a09b89 ("cpumask: re-introduce
constant-sized cpumask optimizations") exposed a number of cases of
drivers not checking the result of "cpumask_next()" and friends
correctly.
The documented correct check for "no more cpus in the cpumask" is to
check for the result being equal or larger than the number of possible
CPU ids, exactly _because_ we've always done those constant-sized
cpumask scans using a widened type before. So the return value of a
cpumask scan should be checked with
if (cpu >= nr_cpu_ids)
...
because the cpumask scan did not necessarily stop exactly *at* that
maximum CPU id.
But a few cases ended up instead using checks like
if (cpu == nr_cpumask_bits)
...
which used that internal "widened" number of bits. And that used to
work pretty much by accident (ok, in this case "by accident" is simply
because it matched the historical internal implementation of the cpumask
scanning, so it was more of a "intentionally using implementation
details rather than an accident").
But the extended constant-sized optimizations then did that internal
implementation differently, and now that code that did things wrong but
matched the old implementation no longer worked at all.
Which then causes subsequent odd problems due to using what ends up
being an invalid CPU ID.
Most of these cases require either unusual hardware or special uses to
hit, but the random.c one triggers quite easily.
All you really need is to have a sufficiently small CONFIG_NR_CPUS value
for the bit scanning optimization to be triggered, but not enough CPUs
to then actually fill that widened cpumask. At that point, the cpumask
scanning will return the NR_CPUS constant, which is _not_ the same as
nr_cpumask_bits.
This just does the mindless fix with
sed -i 's/== nr_cpumask_bits/>= nr_cpu_ids/'
to fix the incorrect uses.
The ones in the SCSI lpfc driver in particular could probably be fixed
more cleanly by just removing that repeated pattern entirely, but I am
not emptionally invested enough in that driver to care.
Reported-and-tested-by: Guenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/lkml/481b19b5-83a0-4793-b4fd-194ad7b978c3@roeck-us.net/
Reported-and-tested-by: Geert Uytterhoeven <geert+renesas@glider.be>
Link: https://lore.kernel.org/lkml/CAMuHMdUKo_Sf7TjKzcNDa8Ve+6QrK+P8nSQrSQ=6LTRmcBKNww@mail.gmail.com/
Reported-by: Vernon Yang <vernon2gm@gmail.com>
Link: https://lore.kernel.org/lkml/20230306160651.2016767-1-vernon2gm@gmail.com/
Cc: Yury Norov <yury.norov@gmail.com>
Cc: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
arch/powerpc/xmon/xmon.c | 2 +-
drivers/char/random.c | 2 +-
drivers/net/wireguard/queueing.h | 2 +-
drivers/scsi/lpfc/lpfc_init.c | 14 +++++++-------
4 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/arch/powerpc/xmon/xmon.c b/arch/powerpc/xmon/xmon.c
index 0da66bc4823d4..3b4e2475fc4ef 100644
--- a/arch/powerpc/xmon/xmon.c
+++ b/arch/powerpc/xmon/xmon.c
@@ -1277,7 +1277,7 @@ static int xmon_batch_next_cpu(void)
while (!cpumask_empty(&xmon_batch_cpus)) {
cpu = cpumask_next_wrap(smp_processor_id(), &xmon_batch_cpus,
xmon_batch_start_cpu, true);
- if (cpu == nr_cpumask_bits)
+ if (cpu >= nr_cpu_ids)
break;
if (xmon_batch_start_cpu == -1)
xmon_batch_start_cpu = cpu;
diff --git a/drivers/char/random.c b/drivers/char/random.c
index ce3ccd172cc86..253f2ddb89130 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1311,7 +1311,7 @@ static void __cold try_to_generate_entropy(void)
/* Basic CPU round-robin, which avoids the current CPU. */
do {
cpu = cpumask_next(cpu, &timer_cpus);
- if (cpu == nr_cpumask_bits)
+ if (cpu >= nr_cpu_ids)
cpu = cpumask_first(&timer_cpus);
} while (cpu == smp_processor_id() && num_cpus > 1);
diff --git a/drivers/net/wireguard/queueing.h b/drivers/net/wireguard/queueing.h
index 583adb37ee1e3..125284b346a77 100644
--- a/drivers/net/wireguard/queueing.h
+++ b/drivers/net/wireguard/queueing.h
@@ -106,7 +106,7 @@ static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
{
unsigned int cpu = *stored_cpu, cpu_index, i;
- if (unlikely(cpu == nr_cpumask_bits ||
+ if (unlikely(cpu >= nr_cpu_ids ||
!cpumask_test_cpu(cpu, cpu_online_mask))) {
cpu_index = id % cpumask_weight(cpu_online_mask);
cpu = cpumask_first(cpu_online_mask);
diff --git a/drivers/scsi/lpfc/lpfc_init.c b/drivers/scsi/lpfc/lpfc_init.c
index 25ba20e428255..3fbd3bec26fc1 100644
--- a/drivers/scsi/lpfc/lpfc_init.c
+++ b/drivers/scsi/lpfc/lpfc_init.c
@@ -12507,7 +12507,7 @@ lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
goto found_same;
new_cpu = cpumask_next(
new_cpu, cpu_present_mask);
- if (new_cpu == nr_cpumask_bits)
+ if (new_cpu >= nr_cpu_ids)
new_cpu = first_cpu;
}
/* At this point, we leave the CPU as unassigned */
@@ -12521,7 +12521,7 @@ lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
* selecting the same IRQ.
*/
start_cpu = cpumask_next(new_cpu, cpu_present_mask);
- if (start_cpu == nr_cpumask_bits)
+ if (start_cpu >= nr_cpu_ids)
start_cpu = first_cpu;
lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
@@ -12557,7 +12557,7 @@ lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
goto found_any;
new_cpu = cpumask_next(
new_cpu, cpu_present_mask);
- if (new_cpu == nr_cpumask_bits)
+ if (new_cpu >= nr_cpu_ids)
new_cpu = first_cpu;
}
/* We should never leave an entry unassigned */
@@ -12575,7 +12575,7 @@ lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
* selecting the same IRQ.
*/
start_cpu = cpumask_next(new_cpu, cpu_present_mask);
- if (start_cpu == nr_cpumask_bits)
+ if (start_cpu >= nr_cpu_ids)
start_cpu = first_cpu;
lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
@@ -12648,7 +12648,7 @@ lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
goto found_hdwq;
}
new_cpu = cpumask_next(new_cpu, cpu_present_mask);
- if (new_cpu == nr_cpumask_bits)
+ if (new_cpu >= nr_cpu_ids)
new_cpu = first_cpu;
}
@@ -12663,7 +12663,7 @@ lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
goto found_hdwq;
new_cpu = cpumask_next(new_cpu, cpu_present_mask);
- if (new_cpu == nr_cpumask_bits)
+ if (new_cpu >= nr_cpu_ids)
new_cpu = first_cpu;
}
@@ -12674,7 +12674,7 @@ lpfc_cpu_affinity_check(struct lpfc_hba *phba, int vectors)
found_hdwq:
/* We found an available entry, copy the IRQ info */
start_cpu = cpumask_next(new_cpu, cpu_present_mask);
- if (start_cpu == nr_cpumask_bits)
+ if (start_cpu >= nr_cpu_ids)
start_cpu = first_cpu;
cpup->hdwq = new_cpup->hdwq;
logit:
--
2.39.2
next prev parent reply other threads:[~2023-03-20 0:54 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-20 0:52 [PATCH AUTOSEL 6.2 01/30] drm/cirrus: NULL-check pipe->plane.state->fb in cirrus_pipe_update() Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 02/30] HID: cp2112: Fix driver not registering GPIO IRQ chip as threaded Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 03/30] ca8210: fix mac_len negative array access Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 04/30] HID: logitech-hidpp: Add support for Logitech MX Master 3S mouse Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 05/30] HID: intel-ish-hid: ipc: Fix potential use-after-free in work function Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 06/30] m68k: mm: Fix systems with memory at end of 32-bit address space Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 07/30] m68k: Only force 030 bus error if PC not in exception table Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 08/30] selftests/bpf: check that modifier resolves after pointer Sasha Levin
2023-03-20 15:31 ` Lorenz Bauer
2023-03-20 15:48 ` Greg KH
2023-03-28 10:18 ` Lorenz Bauer
2023-04-11 15:08 ` Lorenz Bauer
2023-04-11 15:14 ` Greg KH
2023-05-24 11:03 ` Lorenz Bauer
2023-05-24 16:03 ` Greg KH
2023-05-24 17:04 ` Lorenz Bauer
2023-05-26 16:43 ` Greg KH
2023-05-30 11:24 ` Lorenz Bauer
2023-03-20 0:52 ` Sasha Levin [this message]
2023-03-20 1:59 ` [PATCH AUTOSEL 6.2 09/30] cpumask: fix incorrect cpumask scanning result checks Linus Torvalds
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 10/30] scsi: target: iscsi: Fix an error message in iscsi_check_key() Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 11/30] scsi: qla2xxx: Add option to disable FC2 Target support Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 12/30] scsi: hisi_sas: Check devm_add_action() return value Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 13/30] scsi: ufs: core: Add soft dependency on governor_simpleondemand Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 14/30] scsi: lpfc: Check kzalloc() in lpfc_sli4_cgn_params_read() Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 15/30] scsi: lpfc: Avoid usage of list iterator variable after loop Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 16/30] scsi: mpi3mr: ioctl timeout when disabling/enabling interrupt Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 17/30] scsi: mpi3mr: Driver unload crashes host when enhanced logging is enabled Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 18/30] scsi: mpi3mr: Wait for diagnostic save during controller init Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 19/30] scsi: mpi3mr: Return proper values for failures in firmware init path Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 20/30] scsi: mpi3mr: NVMe command size greater than 8K fails Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 21/30] scsi: mpi3mr: Bad drive in topology results kernel crash Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 22/30] scsi: storvsc: Handle BlockSize change in Hyper-V VHD/VHDX file Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 23/30] platform/x86: ISST: Increase range of valid mail box commands Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 24/30] platform/x86: int3472: Add GPIOs to Surface Go 3 Board data Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 25/30] net: usb: cdc_mbim: avoid altsetting toggling for Telit FE990 Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 26/30] net: usb: qmi_wwan: add Telit 0x1080 composition Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 27/30] drm/amd/display: Update clock table to include highest clock setting Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 28/30] sh: sanitize the flags on sigreturn Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 29/30] drm/amdgpu: Fix call trace warning and hang when removing amdgpu device Sasha Levin
2023-03-20 0:52 ` [PATCH AUTOSEL 6.2 30/30] drm/amd: Fix initialization mistake for NBIO 7.3.0 Sasha Levin
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=20230320005258.1428043-9-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=Jason@zx2c4.com \
--cc=christophe.leroy@csgroup.eu \
--cc=davem@davemloft.net \
--cc=dick.kennedy@broadcom.com \
--cc=dmitry.osipenko@collabora.com \
--cc=edumazet@google.com \
--cc=geert+renesas@glider.be \
--cc=gustavoars@kernel.org \
--cc=james.smart@broadcom.com \
--cc=jejb@linux.ibm.com \
--cc=joel@jms.id.au \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=martin.petersen@oracle.com \
--cc=mpe@ellerman.id.au \
--cc=nathanl@linux.ibm.com \
--cc=naveen.n.rao@linux.vnet.ibm.com \
--cc=netdev@vger.kernel.org \
--cc=npiggin@gmail.com \
--cc=pabeni@redhat.com \
--cc=stable@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=tytso@mit.edu \
--cc=vernon2gm@gmail.com \
--cc=wireguard@lists.zx2c4.com \
--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