From: Peter Zijlstra <peterz@infradead.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Boqun Feng <boqun.feng@gmail.com>,
David Howells <dhowells@redhat.com>,
Ingo Molnar <mingo@redhat.com>,
Li RongQing <lirongqing@baidu.com>,
Waiman Long <longman@redhat.com>, Will Deacon <will@kernel.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/4] seqlock: introduce scoped_seqlock_read() and scoped_seqlock_read_irqsave()
Date: Fri, 10 Oct 2025 10:03:27 +0200 [thread overview]
Message-ID: <20251010080327.GF4067720@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <CAHk-=whmjm0BbirO8HhT_TZQ2JJMs_FpTcT9SXXaA3NifW2a4w@mail.gmail.com>
On Thu, Oct 09, 2025 at 03:55:15PM -0700, Linus Torvalds wrote:
> On Thu, 9 Oct 2025 at 15:12, Peter Zijlstra <peterz@infradead.org> wrote:
> >
> > Sure; otoh compiler should be able to tell the same using liveness
> > analysis I suppose, but perhaps they're not *that* clever.
>
> They are that clever, but only if they end up unrolling the loop
> statically. If the loop remains a loop, the two variables end up live
> in the same code.
>
> And while a compiler _could_ in theory still see that they aren't
> actually live in the same _iteration_, I don't think any compiler
> actually ends up being that clever in practice.
>
> So making it a union then hopefully gets the compiler to basically use
> that explicit information.
Right, so I had to use -Os to not make it unroll the thing, but then
indeed, sharing the variable helps it.
> > So I thought they were fine; we handle all the enum cases with 'return'
> > so its impossible to not exit the switch() but the silly compiler was
> > complaining about possible fall-through, so clearly it was getting
> > confused.
>
> Yeah, I found the same thing with the 0/1/2 approach - the compiler
> wouldn't realize that the range was limited until I added a very
> explicit limit check that "shouldn't matter", but did.
>
> This might obviously end up depending on compiler version and other
> random things, but in general the whole value range analysis tends to
> be a pretty fragile thing.
>
> In practice, compilers tend to be good at doing value range analysis
> if they see particular patterns (like initializing it to some value,
> always incrementing it by one, and comparing against another value).
>
> But when it's written more like a state machine like this, it's
> clearly very hit and miss.
I reordered the code, it is happier now.
Anyway, the below seems to generate decent code for
{-O2,-Os}x{gcc-14,clang-22}. Yay for optimizing compilers I suppose :-)
---
diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
index 5ce48eab7a2a..45fab026f7d6 100644
--- a/include/linux/seqlock.h
+++ b/include/linux/seqlock.h
@@ -1209,4 +1209,83 @@ done_seqretry_irqrestore(seqlock_t *lock, int seq, unsigned long flags)
if (seq & 1)
read_sequnlock_excl_irqrestore(lock, flags);
}
+
+enum ss_state {
+ ss_done = 0,
+ ss_lock,
+ ss_lock_irqsave,
+ ss_lockless,
+};
+
+struct ss_tmp {
+ enum ss_state state;
+ unsigned long data;
+ spinlock_t *lock;
+ spinlock_t *lock_irqsave;
+};
+
+static inline void __scoped_seqlock_cleanup(struct ss_tmp *sst)
+{
+ if (sst->lock)
+ spin_unlock(sst->lock);
+ if (sst->lock_irqsave)
+ spin_unlock_irqrestore(sst->lock, sst->data);
+}
+
+extern void __scoped_seqlock_invalid_target(void);
+extern void __scoped_seqlock_bug(void);
+
+static inline void
+__scoped_seqlock_next(struct ss_tmp *sst, seqlock_t *lock, enum ss_state target)
+{
+ switch (sst->state) {
+ case ss_done:
+ __scoped_seqlock_bug();
+ return;
+
+ case ss_lock:
+ case ss_lock_irqsave:
+ sst->state = ss_done;
+ return;
+
+ case ss_lockless:
+ if (!read_seqretry(lock, sst->data)) {
+ sst->state = ss_done;
+ return;
+ }
+ break;
+ }
+
+ switch (target) {
+ case ss_done:
+ __scoped_seqlock_invalid_target();
+ return;
+
+ case ss_lock:
+ sst->lock = &lock->lock;
+ spin_lock(sst->lock);
+ sst->state = ss_lock;
+ return;
+
+ case ss_lock_irqsave:
+ sst->lock_irqsave = &lock->lock;
+ spin_lock_irqsave(sst->lock, sst->data);
+ sst->state = ss_lock_irqsave;
+ return;
+
+ case ss_lockless:
+ sst->data = read_seqbegin(lock);
+ return;
+ }
+}
+
+#define __scoped_seqlock_read(_seqlock, _target, _s) \
+ for (struct ss_tmp _s __cleanup(__scoped_seqlock_cleanup) = \
+ { .state = ss_lockless, .data = read_seqbegin(_seqlock) }; \
+ _s.state != ss_done; \
+ __scoped_seqlock_next(&_s, _seqlock, _target))
+
+#define scoped_seqlock_read(_seqlock, _target) \
+ __scoped_seqlock_read(_seqlock, _target, __UNIQUE_ID(seqlock))
+
#endif /* __LINUX_SEQLOCK_H */
diff --git a/kernel/sched/cputime.c b/kernel/sched/cputime.c
index 7097de2c8cda..d2b3f987c888 100644
--- a/kernel/sched/cputime.c
+++ b/kernel/sched/cputime.c
@@ -313,10 +313,8 @@ static u64 read_sum_exec_runtime(struct task_struct *t)
void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
{
struct signal_struct *sig = tsk->signal;
- u64 utime, stime;
struct task_struct *t;
- unsigned int seq, nextseq;
- unsigned long flags;
+ u64 utime, stime;
/*
* Update current task runtime to account pending time since last
@@ -329,27 +327,19 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
if (same_thread_group(current, tsk))
(void) task_sched_runtime(current);
- rcu_read_lock();
- /* Attempt a lockless read on the first round. */
- nextseq = 0;
- do {
- seq = nextseq;
- flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
+ guard(rcu)();
+ scoped_seqlock_read(&sig->stats_lock, ss_lock_irqsave) {
times->utime = sig->utime;
times->stime = sig->stime;
times->sum_exec_runtime = sig->sum_sched_runtime;
- for_each_thread(tsk, t) {
+ __for_each_thread(sig, t) {
task_cputime(t, &utime, &stime);
times->utime += utime;
times->stime += stime;
times->sum_exec_runtime += read_sum_exec_runtime(t);
}
- /* If lockless access failed, take the lock. */
- nextseq = 1;
- } while (need_seqretry(&sig->stats_lock, seq));
- done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
- rcu_read_unlock();
+ }
}
#ifdef CONFIG_IRQ_TIME_ACCOUNTING
next prev parent reply other threads:[~2025-10-10 8:03 UTC|newest]
Thread overview: 74+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-28 16:19 [PATCH 0/1] documentation: seqlock: fix the wrong documentation of read_seqbegin_or_lock/need_seqretry Oleg Nesterov
2025-09-28 16:20 ` [PATCH 1/1] " Oleg Nesterov
2025-10-01 18:21 ` Waiman Long
2025-10-01 19:06 ` Oleg Nesterov
2025-10-01 19:24 ` Waiman Long
2025-10-01 19:34 ` Waiman Long
2025-10-02 11:01 ` Oleg Nesterov
2025-10-21 10:35 ` [tip: locking/core] " tip-bot2 for Oleg Nesterov
2025-09-28 16:20 ` [RFC 2/1] seqlock: make the read_seqbegin_or_lock() API more simple and less error-prone ? Oleg Nesterov
2025-09-29 0:41 ` [????] " Li,Rongqing
2025-09-29 6:47 ` Oleg Nesterov
2025-10-01 13:02 ` Peter Zijlstra
2025-10-01 13:13 ` Oleg Nesterov
2025-10-01 13:46 ` Oleg Nesterov
2025-10-02 12:58 ` Oleg Nesterov
2025-09-30 22:09 ` David Howells
2025-10-01 11:51 ` Oleg Nesterov
2025-10-05 14:47 ` [PATCH 0/5] seqlock: introduce SEQLOCK_READ_SECTION() Oleg Nesterov
2025-10-05 14:49 ` Oleg Nesterov
2025-10-05 14:50 ` [PATCH 1/5] " Oleg Nesterov
2025-10-05 15:34 ` Linus Torvalds
2025-10-05 16:07 ` Oleg Nesterov
2025-10-05 16:35 ` Linus Torvalds
2025-10-05 14:50 ` [PATCH 2/5] seqlock: change thread_group_cputime() to use SEQLOCK_READ_SECTION() Oleg Nesterov
2025-10-05 14:50 ` [PATCH 3/5] seqlock: change do_task_stat() " Oleg Nesterov
2025-10-05 14:50 ` [PATCH 4/5] seqlock: change do_io_accounting() " Oleg Nesterov
2025-10-05 14:50 ` [PATCH 5/5] seqlock: change __dentry_path() to use __SEQLOCK_READ_SECTION() Oleg Nesterov
2025-10-05 15:48 ` Linus Torvalds
2025-10-05 15:30 ` [PATCH 0/5] seqlock: introduce SEQLOCK_READ_SECTION() Al Viro
2025-10-05 17:40 ` Oleg Nesterov
2025-10-07 14:20 ` [PATCH 0/4] seqlock: introduce scoped_seqlock_read() and scoped_seqlock_read_irqsave() Oleg Nesterov
2025-10-07 14:21 ` [PATCH 1/4] " Oleg Nesterov
2025-10-07 16:35 ` Waiman Long
2025-10-07 17:18 ` Oleg Nesterov
2025-10-07 17:21 ` Waiman Long
2025-10-07 14:21 ` [PATCH 2/4] seqlock: change thread_group_cputime() to use scoped_seqlock_read_irqsave() Oleg Nesterov
2025-10-07 14:21 ` [PATCH 3/4] seqlock: change do_task_stat() " Oleg Nesterov
2025-10-07 14:21 ` [PATCH 4/4] seqlock: change do_io_accounting() " Oleg Nesterov
2025-10-07 15:38 ` [PATCH 0/4] seqlock: introduce scoped_seqlock_read() and scoped_seqlock_read_irqsave() Linus Torvalds
2025-10-07 16:34 ` Oleg Nesterov
2025-10-08 12:30 ` [PATCH v2 " Oleg Nesterov
2025-10-08 12:30 ` [PATCH v2 1/4] " Oleg Nesterov
2025-10-08 12:55 ` Peter Zijlstra
2025-10-08 12:59 ` Oleg Nesterov
2025-10-08 13:54 ` Peter Zijlstra
2025-10-08 16:05 ` Linus Torvalds
2025-10-08 16:55 ` Oleg Nesterov
2025-10-09 5:31 ` Linus Torvalds
2025-10-09 7:04 ` Linus Torvalds
2025-10-09 14:37 ` Oleg Nesterov
2025-10-09 16:18 ` Linus Torvalds
2025-10-09 19:50 ` Peter Zijlstra
2025-10-09 20:11 ` Peter Zijlstra
2025-10-09 20:24 ` Linus Torvalds
2025-10-09 22:12 ` Peter Zijlstra
2025-10-09 22:55 ` Linus Torvalds
2025-10-10 8:03 ` Peter Zijlstra [this message]
2025-10-10 12:32 ` Oleg Nesterov
2025-10-10 13:14 ` Oleg Nesterov
2025-10-13 9:03 ` Peter Zijlstra
2025-10-13 11:50 ` Oleg Nesterov
2025-10-10 15:30 ` Linus Torvalds
2025-10-09 23:20 ` Peter Zijlstra
2025-10-09 23:26 ` Linus Torvalds
2025-10-21 10:35 ` [tip: locking/core] seqlock: Introduce scoped_seqlock_read() tip-bot2 for Peter Zijlstra
2025-10-08 12:30 ` [PATCH v2 2/4] seqlock: change thread_group_cputime() to use scoped_seqlock_read_irqsave() Oleg Nesterov
2025-10-21 10:35 ` [tip: locking/core] seqlock: Change thread_group_cputime() to use scoped_seqlock_read() tip-bot2 for Oleg Nesterov
2025-10-08 12:30 ` [PATCH v2 3/4] seqlock: change do_task_stat() to use scoped_seqlock_read_irqsave() Oleg Nesterov
2025-10-21 10:35 ` [tip: locking/core] seqlock: Change do_task_stat() to use scoped_seqlock_read() tip-bot2 for Oleg Nesterov
2025-10-08 12:31 ` [PATCH v2 4/4] seqlock: change do_io_accounting() to use scoped_seqlock_read_irqsave() Oleg Nesterov
2025-10-21 10:35 ` [tip: locking/core] seqlock: Change do_io_accounting() to use scoped_seqlock_read() tip-bot2 for Oleg Nesterov
2025-10-08 12:56 ` [PATCH v2 0/4] seqlock: introduce scoped_seqlock_read() and scoped_seqlock_read_irqsave() Peter Zijlstra
2025-10-08 13:13 ` Oleg Nesterov
2025-10-08 13:55 ` Peter Zijlstra
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=20251010080327.GF4067720@noisy.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=boqun.feng@gmail.com \
--cc=dhowells@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lirongqing@baidu.com \
--cc=longman@redhat.com \
--cc=mingo@redhat.com \
--cc=oleg@redhat.com \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
--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®