lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAADnVQLce4pH4DJW2WW6W2-ct-17OnQE7D8q7KiwdNougis2BQ@mail.gmail.com>
Date: Wed, 2 Apr 2025 14:35:26 -0700
From: Alexei Starovoitov <alexei.starovoitov@...il.com>
To: Vlastimil Babka <vbabka@...e.cz>
Cc: Sebastian Andrzej Siewior <bigeasy@...utronix.de>, Linus Torvalds <torvalds@...ux-foundation.org>, 
	bpf <bpf@...r.kernel.org>, Daniel Borkmann <daniel@...earbox.net>, 
	Andrii Nakryiko <andrii@...nel.org>, Martin KaFai Lau <martin.lau@...nel.org>, 
	Andrew Morton <akpm@...ux-foundation.org>, Peter Zijlstra <peterz@...radead.org>, 
	Steven Rostedt <rostedt@...dmis.org>, Shakeel Butt <shakeel.butt@...ux.dev>, 
	Michal Hocko <mhocko@...e.com>, linux-mm <linux-mm@...ck.org>, 
	LKML <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH] locking/local_lock, mm: Replace localtry_ helpers with
 local_trylock_t type

On Wed, Apr 2, 2025 at 2:02 AM Vlastimil Babka <vbabka@...e.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@...nel.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@...utronix.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.

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ