From: Will Deacon <will@kernel.org>
To: "Paul E. McKenney" <paulmck@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>, Daniel Xu <dxu@dxuuu.xyz>,
mingo@redhat.com, longman@redhat.com, boqun.feng@gmail.com,
linux-kernel@vger.kernel.org, linux-toolchains@vger.kernel.org
Subject: Re: [PATCH] seqlock: Use WRITE_ONCE() when updating sequence
Date: Thu, 19 Dec 2024 18:31:15 +0000 [thread overview]
Message-ID: <20241219183115.GA25652@willie-the-truck> (raw)
In-Reply-To: <3c748145-5184-4e98-a1f2-079f85cf12b8@paulmck-laptop>
On Thu, Dec 19, 2024 at 10:06:23AM -0800, Paul E. McKenney wrote:
> On Thu, Dec 19, 2024 at 05:58:03PM +0000, Will Deacon wrote:
> > On Thu, Dec 19, 2024 at 09:53:48AM -0800, Paul E. McKenney wrote:
> > > On Thu, Dec 19, 2024 at 04:34:55PM +0000, Will Deacon wrote:
> > > > On Wed, Dec 18, 2024 at 06:12:41PM +0100, Peter Zijlstra wrote:
> > > > > diff --git a/arch/s390/kernel/idle.c b/arch/s390/kernel/idle.c
> > > > > index 39cb8d0ae348..8fb7cd75fe62 100644
> > > > > --- a/arch/s390/kernel/idle.c
> > > > > +++ b/arch/s390/kernel/idle.c
> > > > > @@ -45,7 +45,7 @@ void account_idle_time_irq(void)
> > > > >
> > > > > /* Account time spent with enabled wait psw loaded as idle time. */
> > > > > WRITE_ONCE(idle->idle_time, READ_ONCE(idle->idle_time) + idle_time);
> > > > > - WRITE_ONCE(idle->idle_count, READ_ONCE(idle->idle_count) + 1);
> > > > > + INC_ONC(idle->idle_count);
> > > >
> > > > (nit: drive-by typo)
> > >
> > > Heh! I was clearly building with GCC. ;-)
> >
> > And not for S390 :p
>
> True enough! ;-)
>
> > > > > diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
> > > > > index c9b58188ec61..77c253e29758 100644
> > > > > --- a/include/linux/compiler-gcc.h
> > > > > +++ b/include/linux/compiler-gcc.h
> > > > > @@ -137,3 +137,8 @@
> > > > > #if GCC_VERSION < 90100
> > > > > #undef __alloc_size__
> > > > > #endif
> > > > > +
> > > > > +/*
> > > > > + * GCC can't properly optimize the real one with volatile on.
> > > > > + */
> > > > > +#define INC_ONCE(v) (v)++
> > > >
> > > > I think I'm missing what we're trying to do here, but how is this a
> > > > safe fallback? I'd have thought we'd need the whole READ_ONCE() +
> > > > WRITE_ONCE() sequence if the compiler doesn't give us what we need...
> > > >
> > > > > diff --git a/include/linux/compiler.h b/include/linux/compiler.h
> > > > > index efd43df3a99a..b1b13dac1b9e 100644
> > > > > --- a/include/linux/compiler.h
> > > > > +++ b/include/linux/compiler.h
> > > > > @@ -8,6 +8,10 @@
> > > > >
> > > > > #ifdef __KERNEL__
> > > > >
> > > > > +#ifndef INC_ONCE
> > > > > +#define INC_ONCE(v) (*(volatile typeof(v) *)&(v))++
> > > > > +#endif
> > > >
> > > > ... but I'm honestly not sure what this is aiming to elide. Is this about
> > > > having the compiler emit an increment instruction with memory operands
> > > > on architectures that support it? If so, what about architectures that
> > > > _don't_ support that? We still need to guarantee that the load/store
> > > > instructions are single-copy atomic for the type in that case.
> > >
> > > Architectures whose increment instructions lack memory operands can still
> > > load, increment, then store. So yes, if two of these run concurrently,
> > > you can lose counts when incrementing normal memory. (Of course, with
> > > device memory, you get what you get.)
> >
> > ... but can we guarantee that those load and store instructions won't
> > now be torn?
>
> The volatile type qualifier must take care of that, or device drivers
> no longer work.
>
> Or are you worried about the C-language ++ fallback? In that case,
> as I understand it, this fallback is only there until GCC learns to
> generate a increment-to-memory instruction for x86.
As I understand it, both the clang and the GCC implementations in the
proposed diff are written using the C-language ++ operator, no?
The GCC one doesn't have 'volatile' at all:
#define INC_ONCE(v) (v)++
so I don't understand why it's a correct replacement for e.g.:
WRITE_ONCE(idle->idle_time, READ_ONCE(idle->idle_time) + idle_time);
If it was, then we wouldn't need any of these macros in the first place!
The clang version does have 'volatile':
#define INC_ONCE(v) (*(volatile typeof(v) *)&(v))++
but then I don't understand why this is enough for the compiler to
generate an 'inc' with memory operands on x86 if it cannot do that for
the READ_ONCE + WRITE_ONCE implementation. If somehow the above is more
"optimisable" then I worry about the atomicity being preserved as well.
So, basically, I don't understand what's going on here :)
I think I was expecting to see arch-specific implementations of
INC_ONCE() written in assembly where they have the relevant instructions
but that doesn't seem to be the case.
Will
next prev parent reply other threads:[~2024-12-19 18:31 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-17 23:17 Daniel Xu
2024-12-18 3:30 ` Waiman Long
2024-12-18 15:45 ` Paul E. McKenney
2024-12-18 16:10 ` Waiman Long
2024-12-18 16:57 ` Paul E. McKenney
2024-12-18 18:38 ` Waiman Long
2024-12-18 18:52 ` Paul E. McKenney
2024-12-18 20:09 ` Waiman Long
2024-12-18 10:30 ` Peter Zijlstra
2024-12-18 15:43 ` Paul E. McKenney
2024-12-18 16:23 ` Peter Zijlstra
2024-12-18 16:29 ` Peter Zijlstra
2024-12-18 16:59 ` Paul E. McKenney
2024-12-18 17:12 ` Peter Zijlstra
2024-12-18 19:15 ` Paul E. McKenney
2024-12-18 19:56 ` Florian Weimer
2024-12-19 16:10 ` Paul E. McKenney
2024-12-19 16:45 ` Florian Weimer
2024-12-19 17:48 ` Paul E. McKenney
2024-12-19 17:53 ` Peter Zijlstra
2024-12-19 16:34 ` Will Deacon
2024-12-19 17:53 ` Paul E. McKenney
2024-12-19 17:58 ` Will Deacon
2024-12-19 18:06 ` Paul E. McKenney
2024-12-19 18:31 ` Will Deacon [this message]
2024-12-20 17:40 ` Paul E. McKenney
2025-01-25 0:31 ` Daniel Xu
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=20241219183115.GA25652@willie-the-truck \
--to=will@kernel.org \
--cc=boqun.feng@gmail.com \
--cc=dxu@dxuuu.xyz \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-toolchains@vger.kernel.org \
--cc=longman@redhat.com \
--cc=mingo@redhat.com \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.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®