mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ard Biesheuvel" <ardb@kernel.org>
To: "Nick Desaulniers" <ndesaulniers@google.com>,
	"Sebastian Andrzej Siewior" <bigeasy@linutronix.de>,
	"Bill Wendling" <morbo@google.com>
Cc: "Ard Biesheuvel" <ardb+git@google.com>,
	linux-kernel@vger.kernel.org,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>, "Will Deacon" <will@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Waiman Long" <longman@redhat.com>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Justin Stitt" <justinstitt@google.com>,
	llvm@lists.linux.dev
Subject: Re: [PATCH] locking/local_lock: Don't evaluate the per-CPU lock pointer if unused
Date: Tue, 24 Nov 2026 23:24:57 +0100	[thread overview]
Message-ID: <1d4d7a84-d0b8-4b81-adb5-4fcecd1e19a1@app.fastmail.com> (raw)
In-Reply-To: <CAKwvOd=KbZgizb1VhD-zq4S1jRGCxUaLM+VHeRNhTFQf+KMrRQ@mail.gmail.com>



On Tue, 24 Nov 2026, at 20:32, Nick Desaulniers wrote:
> On Tue, Nov 24, 2026 at 8:39 AM Sebastian Andrzej Siewior
> <bigeasy@linutronix.de> wrote:
>>
>> On 2026-11-23 13:52:09 [-0800], Nick Desaulniers wrote:
>> > On Mon, Nov 23, 2026 at 1:23 PM Ard Biesheuvel <ardb@kernel.org> wrote:
>> > >
>> > >
>> > > On Mon, 23 Nov 2026, at 20:12, Nick Desaulniers wrote:
>> > > > Very detailed commit message, but you kind of bury the lede here on
>> > > > what the actual problem this patch is fixing *is*.
>> > > >
>> > >
>> > > Apologies. I'll trim it down,
>> > >
>> > > The TL;DR is that non-volatile asm() statements with memory input
>> > > operands are not elided by Clang if their output is never used.
>> >
>> > That's not a bug; both Clang and GCC behave that way.
>> > https://godbolt.org/z/sEhec4oae
>> >
>> > (perhaps there is a clang bug somewhere, just trying to suss out if
>> > there's something we need to fix here; forgive me for being pedantic
>> > here)
>>
>> I see a
>>         movq %gs:this_cpu_off(%rip), %rXX
>>
>> on x86-64 with clang-21, not with gcc-16 for local_lock() and
>> local_lock_nested_bh(). Are we hunting a clang bug or considering this
>> patch?
>
> Both; I'm just trying to better understand:
> 1. what is the actual point of the patch (see my "burying the lede"
> comment upthread)

The point of the patch is to avoid evaluating the macro argument unless
it is used, to work around the Clang issue  where it doesn't elide asm()
statements whose outputs are never used.

> 2. what issues on the clang side are outstanding and conspiring here.
> I think a v2 with a revised commit message is good to go, though I
> don't know if Ard saw (or ignored) my questions about the temporary or
> casts?

Apologies, I did see them when I read the email, but had forgotten about
them when I got around to answering it.

>> +/*
>> + * Only evaluate @lock if it is actually used, i.e., for local_trylock_t, or
>> + * when lock debugging is enabled (see local_lock_acquire() above).
>> + */
>>  #define __local_lock_acquire(lock)                                     \
>>         do {                                                            \
>> -               local_trylock_t *__tl;                                  \
>> -               local_lock_t *__l;                                      \
>> -                                                                       \
>> -               __l = (local_lock_t *)(lock);                           \
>> -               __tl = (local_trylock_t *)__l;                          \
>>                 _Generic((lock),                                        \
>>                         local_trylock_t *: ({                           \
>> +                               local_trylock_t *__tl = (local_trylock_t *)(lock); \
>> +                                                                       \
>
> Why do we need the temporary?
>

Because 'lock' is a macro argument that we should evaluate only once.

>>                                 lockdep_assert(__tl->acquired == 0);    \
>>                                 WRITE_ONCE(__tl->acquired, 1);          \
>> +                               local_lock_acquire((local_lock_t *)__tl); \
>>                         }),                                             \
>> -                       local_lock_t *: (void)0);                       \
>> -               local_lock_acquire(__l);                                \
>> +                       local_lock_t *: ({                              \
>> +                               local_lock_acquire((local_lock_t *)(lock)); \
>
> Is this cast necessary?
>

Surprisingly, yes.

https://godbolt.org/z/E8rGchfev

> Ideally v2 adds git trailers for:
>
> Link: https://github.com/llvm/llvm-project/issues/93449
> Link: https://github.com/llvm/llvm-project/issues/245738
>

Ok

> (Since if both of the above were fixed, this patch would be moot)
> (I've assigned the second one to you, Bill, since you're "in the
> neighborhood" (touching lots of related code in LLVM recently))
> -- 
> Thanks,
> ~Nick Desaulniers

  reply	other threads:[~2026-11-24 22:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 10:56 Ard Biesheuvel
2026-11-23 19:12 ` Nick Desaulniers
2026-11-23 19:19   ` Nick Desaulniers
2026-11-23 21:23   ` Ard Biesheuvel
2026-11-23 21:52     ` Nick Desaulniers
2026-11-23 22:15       ` Ard Biesheuvel
2026-11-24 16:39       ` Sebastian Andrzej Siewior
2026-11-24 19:32         ` Nick Desaulniers
2026-11-24 22:24           ` Ard Biesheuvel [this message]
2026-11-24 22:44             ` Nick Desaulniers

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=1d4d7a84-d0b8-4b81-adb5-4fcecd1e19a1@app.fastmail.com \
    --to=ardb@kernel.org \
    --cc=ardb+git@google.com \
    --cc=bigeasy@linutronix.de \
    --cc=boqun@kernel.org \
    --cc=justinstitt@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=will@kernel.org \
    /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

all inboxes | Powered by JetHome®