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: Wed, 2 Apr 2025 14:35:26 -0700 [thread overview]
Message-ID: <CAADnVQLce4pH4DJW2WW6W2-ct-17OnQE7D8q7KiwdNougis2BQ@mail.gmail.com> (raw)
In-Reply-To: <62dd026d-1290-49cb-a411-897f4d5f6ca7@suse.cz>
On Wed, Apr 2, 2025 at 2:02 AM Vlastimil Babka <vbabka@suse.cz> wrote:
>
> On 4/2/25 09:30, Sebastian Andrzej Siewior wrote:
> > On 2025-03-31 17:51:34 [-0700], Alexei Starovoitov wrote:
> >> From: Alexei Starovoitov <ast@kernel.org>
> >>
> >> Partially revert commit 0aaddfb06882 ("locking/local_lock: Introduce localtry_lock_t").
> >> Remove localtry_*() helpers, since localtry_lock() name might
> >> be misinterpreted as "try lock".
> >
> > So we back to what you suggested initially. I was more a fan of
> > explicitly naming things but if this is misleading so be it. So
> >
> > Acked-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> >
> > While at it, could you look at the hunk below and check if it worth it?
> > The struct duplication and hoping that the first part remains the same,
> > is hoping. This still relies that the first part remains the same but…
>
> I've updated your fixups to v2
> https://lore.kernel.org/all/20250401205245.70838-1-alexei.starovoitov@gmail.com/
Sebastian, Vlastimil,
Thanks for the fixups. Folded.
> and to support runtime local_trylock_init(), and it's at the end of my e-mail
>
> But I also thought we could go all the way with removing casting in
> that way and stop relying on the same layout implicitly.
>
> So I rewrote this:
>
> #define __local_lock_acquire(lock) \
> do { \
> local_trylock_t *tl; \
> local_lock_t *l; \
> \
> _Generic((lock), \
> local_lock_t *: ({ \
> l = this_cpu_ptr(lock); \
> }), \
> local_trylock_t *: ({ \
> tl = this_cpu_ptr(lock); \
> l = &tl->llock; \
> lockdep_assert(tl->acquired == 0); \
> WRITE_ONCE(tl->acquired, 1); \
> }), \
> default:(void)0); \
> local_lock_acquire(l); \
> } while (0)
>
> But I'm getting weird errors:
>
> ./include/linux/local_lock_internal.h:107:36: error: assignment to ‘local_trylock_t *’ from incompatible pointer type ‘local_lock_t *’ [-Wincompatible-pointer-types]
> 107 | tl = this_cpu_ptr(lock); \
>
> coming from the guard expansions. I don't understand why it goes to the
> _Generic() "branch" of local_trylock_t * with a local_lock_t *.
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.
Your macro can be hacked with addition of:
local_lock_t *l = NULL;
...
l = (void *)this_cpu_ptr(lock);
...
tl = (void *)this_cpu_ptr(lock);
...
DEFINE_GUARD(local_lock, void __percpu*,
then
guard(local_lock)(&memcg_stock.stock_lock);
will compile without warnings with both
typeof(stock_lock) = local_lock_t and local_trylock_t,
but the generated code will take default:(void)0) path
and will pass NULL into local_lock_acquire(NULL);
In other words guard(local_lock) can only support one
specific type. It cannot be made polymorphic with _Generic() trick.
This is an unfortunate tradeoff with this approach.
Thankfully there are no users of it in the tree:
git grep 'guard(local'|wc -l
0
so I think it's ok that guard(local_lock) can only be used with local_lock_t.
next prev parent reply other threads:[~2025-04-02 21:35 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 [this message]
2025-04-03 0:22 ` Alexei Starovoitov
2025-04-03 9:09 ` Vlastimil Babka
2025-04-03 14:44 ` Alexei Starovoitov
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=CAADnVQLce4pH4DJW2WW6W2-ct-17OnQE7D8q7KiwdNougis2BQ@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