* [PATCH v4 0/1] Fix /proc/cpuinfo cpumask warning
@ 2022-11-03 14:25 Andrew Jones
2022-11-03 14:25 ` [PATCH v4 1/1] x86: cpuinfo: Ensure inputs to cpumask_next are valid Andrew Jones
0 siblings, 1 reply; 3+ messages in thread
From: Andrew Jones @ 2022-11-03 14:25 UTC (permalink / raw)
To: x86, linux-riscv, linux-kernel
Cc: Jonas Bonn, linux-s390, Alexander Gordeev, Albert Ou,
Vasily Gorbik, Yury Norov, Heiko Carstens, Dave Hansen,
Stefan Kristiansson, Ingo Molnar, Borislav Petkov, Paul Walmsley,
Palmer Dabbelt, Thomas Gleixner, linuxppc-dev, Stafford Horne,
openrisc
Commit 78e5a3399421 ("cpumask: fix checking valid cpu range") started
issuing warnings[1] when cpu indices equal to nr_cpu_ids - 1 were passed
to cpumask_next* functions. The commit has since been reverted with
commit 80493877d7d0 ("Revert "cpumask: fix checking valid cpu range"."),
which raises the question as to how much this proposed patch is needed.
Additionally, there's some discussion as to whether or not cpumask_next()
should even be validating its inputs[2]. So, with that in mind, I'm fine
with the patch being dropped. However, it may still be reasonable to add
the checking to /proc/cpuinfo until cpumask_next has made changes and
better documented its API.
[1] Warnings will only appear with DEBUG_PER_CPU_MAPS enabled.
[2] https://lore.kernel.org/all/CAHk-=wihz-GXx66MmEyaADgS1fQE_LDcB9wrHAmkvXkd8nx9tA@mail.gmail.com/
This series addresses the issue for x86. riscv has already merged an
equivalent patch (v3 of this series). Also, from a quick grep of cpuinfo
seq operations, I think at least openrisc, powerpc, and s390 could get an
equivalent patch. While the test is simple (see next paragraph) I'm not
equipped to test on each architecture.
To test, just build a kernel with DEBUG_PER_CPU_MAPS enabled, boot to
a shell, do 'cat /proc/cpuinfo', and look for a kernel warning.
v4:
- The riscv patch has already been merged.
- Mostly rewrote the cover letter as the situation has changed since
78e5a3399421 was reverted.
- Rewrote the commit message in order to try an better clarify things
and also to add the reference to the revert commit, which results in
the commit no longer claiming its a 'fix' in its summary. [Boris]
v3:
- Change condition from >= to == in order to still get a warning
for > as that's unexpected. [Yury]
- Picked up tags on the riscv patch
v2:
- Added all the information I should have in the first place
to the commit message [Boris]
- Changed style of fix [Boris]
Andrew Jones (1):
x86: cpuinfo: Ensure inputs to cpumask_next are valid
arch/x86/kernel/cpu/proc.c | 3 +++
1 file changed, 3 insertions(+)
--
2.37.3
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v4 1/1] x86: cpuinfo: Ensure inputs to cpumask_next are valid
2022-11-03 14:25 [PATCH v4 0/1] Fix /proc/cpuinfo cpumask warning Andrew Jones
@ 2022-11-03 14:25 ` Andrew Jones
2022-11-11 4:14 ` Yury Norov
0 siblings, 1 reply; 3+ messages in thread
From: Andrew Jones @ 2022-11-03 14:25 UTC (permalink / raw)
To: x86, linux-riscv, linux-kernel
Cc: Jonas Bonn, linux-s390, Alexander Gordeev, Albert Ou,
Vasily Gorbik, Yury Norov, Heiko Carstens, Dave Hansen,
Stefan Kristiansson, Ingo Molnar, Borislav Petkov, Paul Walmsley,
Palmer Dabbelt, Thomas Gleixner, linuxppc-dev, Stafford Horne,
openrisc
The valid cpumask range is [0, nr_cpu_ids) and cpumask_next()
currently calls find_next_bit() with its input CPU ID number plus one
for the bit number, giving cpumask_next() the range [-1, nr_cpu_ids - 1).
seq_read_iter() and cpuinfo's start and next seq operations implement a
pattern like
n = cpumask_next(n - 1, mask);
show(n);
while (1) {
++n;
n = cpumask_next(n - 1, mask);
if (n >= nr_cpu_ids)
break;
show(n);
}
which will eventually result in cpumask_next() being called with
nr_cpu_ids - 1. A kernel compiled with commit 78e5a3399421 ("cpumask:
fix checking valid cpu range"), but not its revert, commit
80493877d7d0 ("Revert "cpumask: fix checking valid cpu range"."),
will generate a warning when DEBUG_PER_CPU_MAPS is enabled each time
/proc/cpuinfo is read. Future-proof cpuinfo by checking its input to
cpumask_next() is valid.
Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Cc: Yury Norov <yury.norov@gmail.com>
---
arch/x86/kernel/cpu/proc.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/arch/x86/kernel/cpu/proc.c b/arch/x86/kernel/cpu/proc.c
index 099b6f0d96bd..de3f93ac6e49 100644
--- a/arch/x86/kernel/cpu/proc.c
+++ b/arch/x86/kernel/cpu/proc.c
@@ -153,6 +153,9 @@ static int show_cpuinfo(struct seq_file *m, void *v)
static void *c_start(struct seq_file *m, loff_t *pos)
{
+ if (*pos == nr_cpu_ids)
+ return NULL;
+
*pos = cpumask_next(*pos - 1, cpu_online_mask);
if ((*pos) < nr_cpu_ids)
return &cpu_data(*pos);
--
2.37.3
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v4 1/1] x86: cpuinfo: Ensure inputs to cpumask_next are valid
2022-11-03 14:25 ` [PATCH v4 1/1] x86: cpuinfo: Ensure inputs to cpumask_next are valid Andrew Jones
@ 2022-11-11 4:14 ` Yury Norov
0 siblings, 0 replies; 3+ messages in thread
From: Yury Norov @ 2022-11-11 4:14 UTC (permalink / raw)
To: Andrew Jones
Cc: Jonas Bonn, linux-s390, Alexander Gordeev, Dave Hansen,
Vasily Gorbik, Heiko Carstens, x86, linux-kernel,
Stefan Kristiansson, openrisc, Ingo Molnar, Borislav Petkov,
Paul Walmsley, Stafford Horne, Palmer Dabbelt, linux-riscv,
linuxppc-dev, Thomas Gleixner, Albert Ou
On Thu, Nov 03, 2022 at 03:25:04PM +0100, Andrew Jones wrote:
> The valid cpumask range is [0, nr_cpu_ids) and cpumask_next()
> currently calls find_next_bit() with its input CPU ID number plus one
> for the bit number, giving cpumask_next() the range [-1, nr_cpu_ids - 1).
> seq_read_iter() and cpuinfo's start and next seq operations implement a
> pattern like
>
> n = cpumask_next(n - 1, mask);
> show(n);
> while (1) {
> ++n;
> n = cpumask_next(n - 1, mask);
> if (n >= nr_cpu_ids)
> break;
> show(n);
> }
>
> which will eventually result in cpumask_next() being called with
> nr_cpu_ids - 1. A kernel compiled with commit 78e5a3399421 ("cpumask:
> fix checking valid cpu range"), but not its revert, commit
> 80493877d7d0 ("Revert "cpumask: fix checking valid cpu range"."),
> will generate a warning when DEBUG_PER_CPU_MAPS is enabled each time
> /proc/cpuinfo is read. Future-proof cpuinfo by checking its input to
> cpumask_next() is valid.
>
> Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
> Cc: Yury Norov <yury.norov@gmail.com>
Reviewed-by: Yury Norov <yury.norov@gmail.com>
> ---
> arch/x86/kernel/cpu/proc.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/arch/x86/kernel/cpu/proc.c b/arch/x86/kernel/cpu/proc.c
> index 099b6f0d96bd..de3f93ac6e49 100644
> --- a/arch/x86/kernel/cpu/proc.c
> +++ b/arch/x86/kernel/cpu/proc.c
> @@ -153,6 +153,9 @@ static int show_cpuinfo(struct seq_file *m, void *v)
>
> static void *c_start(struct seq_file *m, loff_t *pos)
> {
> + if (*pos == nr_cpu_ids)
> + return NULL;
> +
> *pos = cpumask_next(*pos - 1, cpu_online_mask);
> if ((*pos) < nr_cpu_ids)
> return &cpu_data(*pos);
> --
> 2.37.3
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-11-11 4:15 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-03 14:25 [PATCH v4 0/1] Fix /proc/cpuinfo cpumask warning Andrew Jones
2022-11-03 14:25 ` [PATCH v4 1/1] x86: cpuinfo: Ensure inputs to cpumask_next are valid Andrew Jones
2022-11-11 4:14 ` Yury Norov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).