* locking/local_lock, mm: sparse warnings about shadowed variable
@ 2025-06-11 17:37 Charlemagne Lasse
2025-06-11 17:57 ` Vlastimil Babka
0 siblings, 1 reply; 5+ messages in thread
From: Charlemagne Lasse @ 2025-06-11 17:37 UTC (permalink / raw)
To: Johannes Weiner, Michal Hocko, Roman Gushchin, Shakeel Butt,
Muchun Song, Andrew Morton, Sebastian Andrzej Siewior,
Clark Williams, Steven Rostedt, Alexei Starovoitov,
Thomas Gleixner, Vlastimil Babka, Jakub Kicinski,
Peter Zijlstra (Intel), Davidlohr Bueso, LKML, cgroups, linux-mm,
linux-rt-devel
HI,
when I run `make C=2 mm/mlock.o CHECK="sparse -Wshadow"`, I get a lot of
./include/linux/local_lock.h:88:1: warning: symbol 'l' shadows an earlier one
./include/linux/local_lock.h:88:1: originally declared here
after commit
51339d99c0131bc0d16d378e9b05bc498d2967e2 is the first bad commit
commit 51339d99c0131bc0d16d378e9b05bc498d2967e2
Author: Alexei Starovoitov <ast@kernel.org>
Date: 2025-04-02 19:55:14 -0700
locking/local_lock, mm: replace localtry_ helpers with local_trylock_t type
Partially revert commit 0aaddfb06882 ("locking/local_lock: Introduce
localtry_lock_t"). Remove localtry_*() helpers, since localtry_lock()
name might be misinterpreted as "try lock".
Introduce local_trylock[_irqsave]() helpers that only work with newly
introduced local_trylock_t type. Note that attempt to use
local_trylock[_irqsave]() with local_lock_t will cause compilation
failure.
Usage and behavior in !PREEMPT_RT:
local_lock_t lock; // sizeof(lock) == 0
local_lock(&lock); // preempt disable
local_lock_irqsave(&lock, ...); // irq save
if (local_trylock_irqsave(&lock, ...)) // compilation error
local_trylock_t lock; // sizeof(lock) == 4
local_lock(&lock); // preempt disable, acquired = 1
local_lock_irqsave(&lock, ...); // irq save, acquired = 1
if (local_trylock(&lock)) // if (!acquired) preempt
disable, acquired = 1
if (local_trylock_irqsave(&lock, ...)) // if (!acquired) irq save,
acquired = 1
The existing local_lock_*() macros can be used either with local_lock_t or
local_trylock_t. With local_trylock_t they set acquired = 1 while
local_unlock_*() clears it.
In !PREEMPT_RT local_lock_irqsave(local_lock_t *) disables interrupts to
protect critical section, but it doesn't prevent NMI, so the fully
reentrant code cannot use local_lock_irqsave(local_lock_t *) for exclusive
access.
The local_lock_irqsave(local_trylock_t *) helper disables interrupts and
sets acquired=1, so local_trylock_irqsave(local_trylock_t *) from NMI
attempting to acquire the same lock will return false.
In PREEMPT_RT local_lock_irqsave() maps to preemptible spin_lock(). Map
local_trylock_irqsave() to preemptible spin_trylock(). When in hard IRQ
or NMI return false right away, since spin_trylock() is not safe due to
explicit locking in the underneath rt_spin_trylock() implementation.
Removing this explicit locking and attempting only "trylock" is undesired
due to PI implications.
The local_trylock() without _irqsave can be used to avoid the cost of
disabling/enabling interrupts by only disabling preemption, so
local_trylock() in an interrupt attempting to acquire the same lock will
return false.
Note there is no need to use local_inc for acquired variable, since it's a
percpu variable with strict nesting scopes.
Note that guard(local_lock)(&lock) works only for "local_lock_t lock".
The patch also makes sure that local_lock_release(l) is called before
WRITE_ONCE(l->acquired, 0). Though IRQs are disabled at this point the
local_trylock() from NMI will succeed and local_lock_acquire(l) will warn.
Link: https://lkml.kernel.org/r/20250403025514.41186-1-alexei.starovoitov@gmail.com
Fixes: 0aaddfb06882 ("locking/local_lock: Introduce localtry_lock_t")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Daniel Borkman <daniel@iogearbox.net>
Cc: Linus Torvalds <torvalds@linuxfoundation.org>
Cc: Martin KaFai Lau <martin.lau@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/linux/local_lock.h | 58 ++--------
include/linux/local_lock_internal.h | 211 +++++++++++++++---------------------
mm/memcontrol.c | 39 ++++---
3 files changed, 116 insertions(+), 192 deletions(-)
bisect found first bad commit
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: locking/local_lock, mm: sparse warnings about shadowed variable
2025-06-11 17:37 locking/local_lock, mm: sparse warnings about shadowed variable Charlemagne Lasse
@ 2025-06-11 17:57 ` Vlastimil Babka
2025-06-11 18:20 ` Alexei Starovoitov
0 siblings, 1 reply; 5+ messages in thread
From: Vlastimil Babka @ 2025-06-11 17:57 UTC (permalink / raw)
To: Charlemagne Lasse, Johannes Weiner, Michal Hocko, Roman Gushchin,
Shakeel Butt, Muchun Song, Andrew Morton,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
Alexei Starovoitov, Thomas Gleixner, Jakub Kicinski,
Peter Zijlstra (Intel), Davidlohr Bueso, LKML, cgroups, linux-mm,
linux-rt-devel
On 6/11/25 19:37, Charlemagne Lasse wrote:
> HI,
>
> when I run `make C=2 mm/mlock.o CHECK="sparse -Wshadow"`, I get a lot of
>
> ./include/linux/local_lock.h:88:1: warning: symbol 'l' shadows an earlier one
> ./include/linux/local_lock.h:88:1: originally declared here
>
> after commit
>
> 51339d99c0131bc0d16d378e9b05bc498d2967e2 is the first bad commit
> commit 51339d99c0131bc0d16d378e9b05bc498d2967e2
> Author: Alexei Starovoitov <ast@kernel.org>
> Date: 2025-04-02 19:55:14 -0700
>
> locking/local_lock, mm: replace localtry_ helpers with local_trylock_t type
Looks like __DEFINE_LOCK_GUARD_1() has "_type *l" and __local_lock_acquire()
has "local_lock_t *l;". It can be fixed e.g. like this, although it's
harmless? The _release() part is not necessary, just for symmetry.
diff --git a/include/linux/local_lock_internal.h b/include/linux/local_lock_internal.h
index 8d5ac16a9b17..075338f270d0 100644
--- a/include/linux/local_lock_internal.h
+++ b/include/linux/local_lock_internal.h
@@ -97,17 +97,17 @@ do { \
#define __local_lock_acquire(lock) \
do { \
local_trylock_t *tl; \
- local_lock_t *l; \
+ local_lock_t *ll; \
\
- l = (local_lock_t *)this_cpu_ptr(lock); \
- tl = (local_trylock_t *)l; \
+ ll = (local_lock_t *)this_cpu_ptr(lock); \
+ tl = (local_trylock_t *)ll; \
_Generic((lock), \
__percpu local_trylock_t *: ({ \
lockdep_assert(tl->acquired == 0); \
WRITE_ONCE(tl->acquired, 1); \
}), \
__percpu local_lock_t *: (void)0); \
- local_lock_acquire(l); \
+ local_lock_acquire(ll); \
} while (0)
#define __local_lock(lock) \
@@ -165,11 +165,11 @@ do { \
#define __local_lock_release(lock) \
do { \
local_trylock_t *tl; \
- local_lock_t *l; \
+ local_lock_t *ll; \
\
- l = (local_lock_t *)this_cpu_ptr(lock); \
- tl = (local_trylock_t *)l; \
- local_lock_release(l); \
+ ll = (local_lock_t *)this_cpu_ptr(lock); \
+ tl = (local_trylock_t *)ll; \
+ local_lock_release(ll); \
_Generic((lock), \
__percpu local_trylock_t *: ({ \
lockdep_assert(tl->acquired == 1); \
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: locking/local_lock, mm: sparse warnings about shadowed variable
2025-06-11 17:57 ` Vlastimil Babka
@ 2025-06-11 18:20 ` Alexei Starovoitov
2025-06-11 22:33 ` Andrew Morton
2025-06-12 7:03 ` Charlemagne Lasse
0 siblings, 2 replies; 5+ messages in thread
From: Alexei Starovoitov @ 2025-06-11 18:20 UTC (permalink / raw)
To: Vlastimil Babka
Cc: Charlemagne Lasse, Johannes Weiner, Michal Hocko, Roman Gushchin,
Shakeel Butt, Muchun Song, Andrew Morton,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
Alexei Starovoitov, Thomas Gleixner, Jakub Kicinski,
Peter Zijlstra (Intel), Davidlohr Bueso, LKML,
open list:CONTROL GROUP (CGROUP), linux-mm, linux-rt-devel
On Wed, Jun 11, 2025 at 10:57 AM Vlastimil Babka <vbabka@suse.cz> wrote:
>
> On 6/11/25 19:37, Charlemagne Lasse wrote:
> > HI,
> >
> > when I run `make C=2 mm/mlock.o CHECK="sparse -Wshadow"`, I get a lot of
> >
> > ./include/linux/local_lock.h:88:1: warning: symbol 'l' shadows an earlier one
> > ./include/linux/local_lock.h:88:1: originally declared here
> >
> > after commit
> >
> > 51339d99c0131bc0d16d378e9b05bc498d2967e2 is the first bad commit
> > commit 51339d99c0131bc0d16d378e9b05bc498d2967e2
> > Author: Alexei Starovoitov <ast@kernel.org>
> > Date: 2025-04-02 19:55:14 -0700
> >
> > locking/local_lock, mm: replace localtry_ helpers with local_trylock_t type
>
> Looks like __DEFINE_LOCK_GUARD_1() has "_type *l" and __local_lock_acquire()
> has "local_lock_t *l;". It can be fixed e.g. like this, although it's
> harmless? The _release() part is not necessary, just for symmetry.
>
> diff --git a/include/linux/local_lock_internal.h b/include/linux/local_lock_internal.h
> index 8d5ac16a9b17..075338f270d0 100644
> --- a/include/linux/local_lock_internal.h
> +++ b/include/linux/local_lock_internal.h
> @@ -97,17 +97,17 @@ do { \
> #define __local_lock_acquire(lock) \
> do { \
> local_trylock_t *tl; \
> - local_lock_t *l; \
> + local_lock_t *ll; \
I wouldn't bother messing with the code because of sparse.
Compilers don't warn here.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: locking/local_lock, mm: sparse warnings about shadowed variable
2025-06-11 18:20 ` Alexei Starovoitov
@ 2025-06-11 22:33 ` Andrew Morton
2025-06-12 7:03 ` Charlemagne Lasse
1 sibling, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2025-06-11 22:33 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Vlastimil Babka, Charlemagne Lasse, Johannes Weiner, Michal Hocko,
Roman Gushchin, Shakeel Butt, Muchun Song,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
Alexei Starovoitov, Thomas Gleixner, Jakub Kicinski,
Peter Zijlstra (Intel), Davidlohr Bueso, LKML,
open list:CONTROL GROUP (CGROUP), linux-mm, linux-rt-devel
On Wed, 11 Jun 2025 11:20:16 -0700 Alexei Starovoitov <alexei.starovoitov@gmail.com> wrote:
> > diff --git a/include/linux/local_lock_internal.h b/include/linux/local_lock_internal.h
> > index 8d5ac16a9b17..075338f270d0 100644
> > --- a/include/linux/local_lock_internal.h
> > +++ b/include/linux/local_lock_internal.h
> > @@ -97,17 +97,17 @@ do { \
> > #define __local_lock_acquire(lock) \
> > do { \
> > local_trylock_t *tl; \
> > - local_lock_t *l; \
> > + local_lock_t *ll; \
>
> I wouldn't bother messing with the code because of sparse.
> Compilers don't warn here.
There's some value in not cluttering up the sparse output in this
fashion. sparse does often find things which we choose to fix.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: locking/local_lock, mm: sparse warnings about shadowed variable
2025-06-11 18:20 ` Alexei Starovoitov
2025-06-11 22:33 ` Andrew Morton
@ 2025-06-12 7:03 ` Charlemagne Lasse
1 sibling, 0 replies; 5+ messages in thread
From: Charlemagne Lasse @ 2025-06-12 7:03 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Vlastimil Babka, Johannes Weiner, Michal Hocko, Roman Gushchin,
Shakeel Butt, Muchun Song, Andrew Morton,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
Alexei Starovoitov, Thomas Gleixner, Jakub Kicinski,
Peter Zijlstra (Intel), Davidlohr Bueso, LKML,
open list:CONTROL GROUP (CGROUP), linux-mm, linux-rt-devel
Am Mi., 11. Juni 2025 um 20:20 Uhr schrieb Alexei Starovoitov
<alexei.starovoitov@gmail.com>:
> I wouldn't bother messing with the code because of sparse.
> Compilers don't warn here.
Actually, they do:
$ make W=2 mm/mlock.o
[...]
In file included from ./include/linux/preempt.h:11,
from ./include/linux/spinlock.h:56,
from ./include/linux/wait.h:9,
from ./include/linux/wait_bit.h:8,
from ./include/linux/fs.h:7,
from ./include/linux/mman.h:5,
from mm/mlock.c:10:
./include/linux/local_lock.h: In function
‘class_local_lock_irqsave_constructor’:
./include/linux/local_lock_internal.h:100:31: warning: declaration of
‘l’ shadows a parameter [-Wshadow]
100 | local_lock_t *l; \
| ^
./include/linux/cleanup.h:394:9: note: in definition of macro
‘__DEFINE_LOCK_GUARD_1’
394 | _lock; \
| ^~~~~
./include/linux/local_lock.h:88:1: note: in expansion of macro
‘DEFINE_LOCK_GUARD_1’
88 | DEFINE_LOCK_GUARD_1(local_lock_irqsave, local_lock_t __percpu,
| ^~~~~~~~~~~~~~~~~~~
./include/linux/local_lock_internal.h:128:17: note: in expansion of
macro ‘__local_lock_acquire’
128 | __local_lock_acquire(lock); \
| ^~~~~~~~~~~~~~~~~~~~
./include/linux/local_lock.h:31:9: note: in expansion of macro
‘__local_lock_irqsave’
31 | __local_lock_irqsave(lock, flags)
| ^~~~~~~~~~~~~~~~~~~~
./include/linux/local_lock.h:89:21: note: in expansion of macro
‘local_lock_irqsave’
89 | local_lock_irqsave(_T->lock, _T->flags),
| ^~~~~~~~~~~~~~~~~~
./include/linux/cleanup.h:391:68: note: shadowed declaration is here
391 | static inline class_##_name##_t class_##_name##_constructor(_type *l) \
./include/linux/cleanup.h:410:1: note: in expansion of macro
‘__DEFINE_LOCK_GUARD_1’
410 | __DEFINE_LOCK_GUARD_1(_name, _type, _lock)
| ^~~~~~~~~~~~~~~~~~~~~
./include/linux/local_lock.h:88:1: note: in expansion of macro
‘DEFINE_LOCK_GUARD_1’
88 | DEFINE_LOCK_GUARD_1
$ gcc --version
gcc (Debian 14.2.0-19) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-06-12 7:03 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-11 17:37 locking/local_lock, mm: sparse warnings about shadowed variable Charlemagne Lasse
2025-06-11 17:57 ` Vlastimil Babka
2025-06-11 18:20 ` Alexei Starovoitov
2025-06-11 22:33 ` Andrew Morton
2025-06-12 7:03 ` Charlemagne Lasse
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).