linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] percpu: Fix self-assignment of __old in raw_cpu_generic_try_cmpxchg()
@ 2023-06-07 21:20 Nathan Chancellor
  2023-06-08  8:29 ` Peter Zijlstra
  0 siblings, 1 reply; 3+ messages in thread
From: Nathan Chancellor @ 2023-06-07 21:20 UTC (permalink / raw)
  To: peterz
  Cc: mark.rutland, arnd, ndesaulniers, trix, linux-arch, linux-kernel,
	llvm, patches, Nathan Chancellor

After commit c5c0ba953b8c ("percpu: Add {raw,this}_cpu_try_cmpxchg()"),
clang built ARCH=arm and ARCH=arm64 kernels with CONFIG_INIT_STACK_NONE
started panicking on boot in alloc_vmap_area():

  [    0.000000] kernel BUG at mm/vmalloc.c:1638!
  [    0.000000] Internal error: Oops - BUG: 00000000f2000800 [#1] PREEMPT SMP
  [    0.000000] Modules linked in:
  [    0.000000] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.4.0-rc2-ARCH+ #1
  [    0.000000] Hardware name: linux,dummy-virt (DT)
  [    0.000000] pstate: 200000c9 (nzCv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
  [    0.000000] pc : alloc_vmap_area+0x7ec/0x7f8
  [    0.000000] lr : alloc_vmap_area+0x7e8/0x7f8

Compiling mm/vmalloc.c with W=2 reveals an instance of -Wshadow, which
helps uncover that through macro expansion, '__old = *(ovalp)' in
raw_cpu_generic_try_cmpxchg() can become '__old = *(&__old)' through
raw_cpu_generic_cmpxchg(), which results in garbage being assigned to
the inner __old and the cmpxchg not working properly.

Add an extra underscore to __old in raw_cpu_generic_try_cmpxchg() so
that there is no more self-assignment, which resolves the panics.

Closes: https://github.com/ClangBuiltLinux/linux/issues/1868
Debugged-by: Nick Desaulniers <ndesaulniers@google.com>
Fixes: c5c0ba953b8c ("percpu: Add {raw,this}_cpu_try_cmpxchg()")
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
 include/asm-generic/percpu.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/asm-generic/percpu.h b/include/asm-generic/percpu.h
index 68c410e85cd7..94cbd50cc870 100644
--- a/include/asm-generic/percpu.h
+++ b/include/asm-generic/percpu.h
@@ -101,9 +101,9 @@ do {									\
 #define raw_cpu_generic_try_cmpxchg(pcp, ovalp, nval)			\
 ({									\
 	typeof(pcp) *__p = raw_cpu_ptr(&(pcp));				\
-	typeof(pcp) __val = *__p, __old = *(ovalp);			\
+	typeof(pcp) __val = *__p, ___old = *(ovalp);			\
 	bool __ret;							\
-	if (__val == __old) {						\
+	if (__val == ___old) {						\
 		*__p = nval;						\
 		__ret = true;						\
 	} else {							\

---
base-commit: ef558b4b7bbbf7e115c87e4da21ce86444d6ec3b
change-id: 20230607-fix-shadowing-in-raw_cpu_generic_try_cmpxchg-579b101f3039

Best regards,
-- 
Nathan Chancellor <nathan@kernel.org>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] percpu: Fix self-assignment of __old in raw_cpu_generic_try_cmpxchg()
  2023-06-07 21:20 [PATCH] percpu: Fix self-assignment of __old in raw_cpu_generic_try_cmpxchg() Nathan Chancellor
@ 2023-06-08  8:29 ` Peter Zijlstra
  2023-06-08 15:54   ` Nathan Chancellor
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Zijlstra @ 2023-06-08  8:29 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: mark.rutland, arnd, ndesaulniers, trix, linux-arch, linux-kernel,
	llvm, patches

On Wed, Jun 07, 2023 at 02:20:59PM -0700, Nathan Chancellor wrote:
> After commit c5c0ba953b8c ("percpu: Add {raw,this}_cpu_try_cmpxchg()"),
> clang built ARCH=arm and ARCH=arm64 kernels with CONFIG_INIT_STACK_NONE
> started panicking on boot in alloc_vmap_area():
> 
>   [    0.000000] kernel BUG at mm/vmalloc.c:1638!
>   [    0.000000] Internal error: Oops - BUG: 00000000f2000800 [#1] PREEMPT SMP
>   [    0.000000] Modules linked in:
>   [    0.000000] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.4.0-rc2-ARCH+ #1
>   [    0.000000] Hardware name: linux,dummy-virt (DT)
>   [    0.000000] pstate: 200000c9 (nzCv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
>   [    0.000000] pc : alloc_vmap_area+0x7ec/0x7f8
>   [    0.000000] lr : alloc_vmap_area+0x7e8/0x7f8
> 
> Compiling mm/vmalloc.c with W=2 reveals an instance of -Wshadow, which
> helps uncover that through macro expansion, '__old = *(ovalp)' in
> raw_cpu_generic_try_cmpxchg() can become '__old = *(&__old)' through
> raw_cpu_generic_cmpxchg(), which results in garbage being assigned to
> the inner __old and the cmpxchg not working properly.
> 
> Add an extra underscore to __old in raw_cpu_generic_try_cmpxchg() so
> that there is no more self-assignment, which resolves the panics.
> 
> Closes: https://github.com/ClangBuiltLinux/linux/issues/1868

First time I see Closes; is this an 'official' tag?

> Debugged-by: Nick Desaulniers <ndesaulniers@google.com>
> Fixes: c5c0ba953b8c ("percpu: Add {raw,this}_cpu_try_cmpxchg()")
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>

Durr, C is such a lovely language :-)

I'll go stick it in locking/core to go with the bunch that wrecked it.
Thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] percpu: Fix self-assignment of __old in raw_cpu_generic_try_cmpxchg()
  2023-06-08  8:29 ` Peter Zijlstra
@ 2023-06-08 15:54   ` Nathan Chancellor
  0 siblings, 0 replies; 3+ messages in thread
From: Nathan Chancellor @ 2023-06-08 15:54 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: mark.rutland, arnd, ndesaulniers, trix, linux-arch, linux-kernel,
	llvm, patches

On Thu, Jun 08, 2023 at 10:29:40AM +0200, Peter Zijlstra wrote:
> On Wed, Jun 07, 2023 at 02:20:59PM -0700, Nathan Chancellor wrote:
> > After commit c5c0ba953b8c ("percpu: Add {raw,this}_cpu_try_cmpxchg()"),
> > clang built ARCH=arm and ARCH=arm64 kernels with CONFIG_INIT_STACK_NONE
> > started panicking on boot in alloc_vmap_area():
> > 
> >   [    0.000000] kernel BUG at mm/vmalloc.c:1638!
> >   [    0.000000] Internal error: Oops - BUG: 00000000f2000800 [#1] PREEMPT SMP
> >   [    0.000000] Modules linked in:
> >   [    0.000000] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.4.0-rc2-ARCH+ #1
> >   [    0.000000] Hardware name: linux,dummy-virt (DT)
> >   [    0.000000] pstate: 200000c9 (nzCv daIF -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
> >   [    0.000000] pc : alloc_vmap_area+0x7ec/0x7f8
> >   [    0.000000] lr : alloc_vmap_area+0x7e8/0x7f8
> > 
> > Compiling mm/vmalloc.c with W=2 reveals an instance of -Wshadow, which
> > helps uncover that through macro expansion, '__old = *(ovalp)' in
> > raw_cpu_generic_try_cmpxchg() can become '__old = *(&__old)' through
> > raw_cpu_generic_cmpxchg(), which results in garbage being assigned to
> > the inner __old and the cmpxchg not working properly.
> > 
> > Add an extra underscore to __old in raw_cpu_generic_try_cmpxchg() so
> > that there is no more self-assignment, which resolves the panics.
> > 
> > Closes: https://github.com/ClangBuiltLinux/linux/issues/1868
> 
> First time I see Closes; is this an 'official' tag?

It is, but only recently:

https://git.kernel.org/linus/0d828200ad56505a827610af876ca0b138b943a6

checkpatch.pl wants Closes: after Reported-by: so I have just started
getting into the habit of using it even when I am the reporter :)

> > Debugged-by: Nick Desaulniers <ndesaulniers@google.com>
> > Fixes: c5c0ba953b8c ("percpu: Add {raw,this}_cpu_try_cmpxchg()")
> > Signed-off-by: Nathan Chancellor <nathan@kernel.org>
> 
> Durr, C is such a lovely language :-)

:)

> I'll go stick it in locking/core to go with the bunch that wrecked it.
> Thanks!

Thanks for the quick response!

Cheers,
Nathan

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-08 15:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-07 21:20 [PATCH] percpu: Fix self-assignment of __old in raw_cpu_generic_try_cmpxchg() Nathan Chancellor
2023-06-08  8:29 ` Peter Zijlstra
2023-06-08 15:54   ` Nathan Chancellor

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).