From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Linus Torvalds <torvalds@linux-foundation.org>,
bpf <bpf@vger.kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <martin.lau@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Peter Zijlstra <peterz@infradead.org>,
Steven Rostedt <rostedt@goodmis.org>,
Shakeel Butt <shakeel.butt@linux.dev>,
Michal Hocko <mhocko@suse.com>, linux-mm <linux-mm@kvack.org>,
LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] locking/local_lock, mm: Replace localtry_ helpers with local_trylock_t type
Date: Thu, 3 Apr 2025 07:44:07 -0700 [thread overview]
Message-ID: <CAADnVQJjy6fFGpPCm_bo-MmpgP136jxtn6De-AiemL28de91MA@mail.gmail.com> (raw)
In-Reply-To: <5feaf4c7-4970-4d9b-84a2-fcba2cbe0bc4@suse.cz>
On Thu, Apr 3, 2025 at 2:09 AM Vlastimil Babka <vbabka@suse.cz> wrote:
>
> On 4/2/25 23:35, Alexei Starovoitov wrote:
> > On Wed, Apr 2, 2025 at 2:02 AM Vlastimil Babka <vbabka@suse.cz> wrote:
> >
> > This is because the macro specifies the type:
> > DEFINE_GUARD(local_lock, local_lock_t __percpu*,
> >
> > and that type is used to define two static inline functions
> > with that type,
> > so by the time our __local_lock_acquire() macro is used
> > it sees 'local_lock_t *' and not the actual type of memcg.stock_lock.
>
> Hm but I didn't even try to instantiate any guard. In fact the compilation
> didn't even error on compiling my slub.o but earlier in compiling
> arch/x86/kernel/asm-offsets.c
The compiler will error compiling a random first file in your build process
that happens to include local_lock.h.
In your case it was asm-offsets.c.
Try make mm/ and it will be another file.
It doesn't matter that nothing is using guard(local_lock).
The static inline functions are there in that compilation unit and
they have to go through the compiler front-end to be discarded
as unused by the middle end later.
> I think the problem is rather that the guard creates static inline functions
> and _Generic() only works via macros as you pointed out in the reply to Andrew?
>
> I guess it's solvable if we care in the future, but it means more code
> duplication - move the _Generic() dispatch outside the whole implementation
> to choose between two variants, have guards use use the specific variant
> directly without _Generic()?
Unlikely. _Generic() works only when the original expression
is preserved all the way to _Generic() statement.
> Or maybe there's a simpler way I'm just not familiar with both the guards
> and _Generic() enough.
There are options.
Here is one:
diff --git a/include/linux/local_lock.h b/include/linux/local_lock.h
index 1a0bc35839e3..e053e187d99d 100644
--- a/include/linux/local_lock.h
+++ b/include/linux/local_lock.h
@@ -124,6 +124,9 @@
DEFINE_GUARD(local_lock, local_lock_t __percpu*,
local_lock(_T),
local_unlock(_T))
+DEFINE_GUARD(local_lock_for_trylock, local_trylock_t __percpu*,
+ local_lock(_T),
+ local_unlock(_T))
and it can be used as:
guard(local_lock_for_trylock)(&lock)
Naming is hard, of course.
tbh I'm not a fan of guard()-s because I see it's being used
in cases when open coded lock/unlock would be more readable.
They're useful when there are plenty of cleanup code and goto-s,
but not universally.
In case of local_trylock() the guard is likely unusable.
The pattern, so far, is something like:
if (condition)
local_trylock_...(&lock);
else
local_lock_...(&lock);
so guard()-s don't fit.
prev parent reply other threads:[~2025-04-03 14:44 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-01 0:51 Alexei Starovoitov
2025-04-01 14:18 ` Vlastimil Babka
2025-04-01 20:55 ` Alexei Starovoitov
2025-04-02 7:30 ` Sebastian Andrzej Siewior
2025-04-02 9:02 ` Vlastimil Babka
2025-04-02 21:35 ` Alexei Starovoitov
2025-04-03 0:22 ` Alexei Starovoitov
2025-04-03 9:09 ` Vlastimil Babka
2025-04-03 14:44 ` Alexei Starovoitov [this message]
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=CAADnVQJjy6fFGpPCm_bo-MmpgP136jxtn6De-AiemL28de91MA@mail.gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=bigeasy@linutronix.de \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=martin.lau@kernel.org \
--cc=mhocko@suse.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=shakeel.butt@linux.dev \
--cc=torvalds@linux-foundation.org \
--cc=vbabka@suse.cz \
/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