seqlock, procfs: Fix scoped_seqlock_read() critical section in do_task_stat()
PATCH 1/1 seqlock, procfs: Fix scoped_seqlock_read() critical section in do_task_stat()
RCU read-lock should not nest inside a read-seqlock irqsave ->stats_lock IRQs-off critical section, but should go on the outside, like it's done in fs/proc/base.c:do_io_accounting().
Looks like this was a pre-existing bug dating back to:
7601df8031fd ("fs/proc: do_task_stat: use sig->stats_lock to gather the threads/children stats")
... but this recent commit made it more apparent:
b76f72bea2c6 ("seqlock: Change do_task_stat() to use scoped_seqlock_read()")
To fix it, move the rcu_read_lock() on the outside, and convert it to a guard(rcu)() construct. Add an extra depth to the local scope to make sure the RCU critical section is kept strictly to the intended area.
--- fs/proc/array.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-)
fs/proc/array.c +21 -20
@@ -537,27 +537,28 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,if (permitted && (!whole || num_threads <2 )) wchan = !task_is_running (task); - scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) { - cmin_flt = sig->cmin_flt; - cmaj_flt = sig->cmaj_flt; - cutime = sig->cutime; - cstime = sig->cstime; - cgtime = sig->cgtime; - -if (whole) { -struct task_struct *t; - - min_flt = sig->min_flt; - maj_flt = sig->maj_flt; - gtime = sig->gtime; - -rcu_read_lock (); -__for_each_thread (sig, t) { - min_flt += t->min_flt; - maj_flt += t->maj_flt; - gtime +=task_gtime (t); + { +guard (rcu)(); + scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) { + cmin_flt = sig->cmin_flt; + cmaj_flt = sig->cmaj_flt; + cutime = sig->cutime; + cstime = sig->cstime; + cgtime = sig->cgtime; + +if (whole) { +struct task_struct *t; + + min_flt = sig->min_flt; + maj_flt = sig->maj_flt; + gtime = sig->gtime; + +__for_each_thread (sig, t) { + min_flt += t->min_flt; + maj_flt += t->maj_flt; + gtime +=task_gtime (t); + } } -rcu_read_unlock (); } }
Signature · 1 line
2.51.0
Quoted 3 lines · Ingo Molnar
RCU read-lock should not nest inside a read-seqlock irqsave ->stats_lock IRQs-off critical section,
Hmm... I agree with this patch, but is it actually wrong?
I thought that rcu_read_lock/unlock is safe under spin_lock_irq...
fs/proc/array.c static int do_task_stat(struct seq_file *m, struct pid_namespace *ns, · line 542
Quoted 3 lines
fs/proc/array.c +3
-0+ { +guard (rcu)(); + scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) {
how about
scoped_guard(rcu) {
scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) {
...
}
}?
Oleg.
PATCH 1/1 seqlock, procfs: Match scoped_seqlock_read() critical section vs. RCU ordering in do_task_stat() to do_io_accounting()
Quoted 8 lines · Oleg Nesterov
Quoted 3 lines · Ingo Molnar
RCU read-lock should not nest inside a read-seqlock irqsave ->stats_lock IRQs-off critical section,
Hmm... I agree with this patch, but is it actually wrong?
I thought that rcu_read_lock/unlock is safe under
spin_lock_irq...
Yeah, true - it's allowed and not a bug, merely
discouraged inside irqs-off sections if it can be
avoided, and it's an inconsistency versusdo_io_accounting().
How about the -v2 phrasing below? I also removed the Fixes tags.
Thanks,
Ingo
===================================>
From: Ingo Molnar <[email protected]>
Date: Tue, 2 Dec 2025 05:09:28 +0100
Subject: [PATCH] seqlock, procfs: Match scoped_seqlock_read() critical section vs. RCU ordering in do_task_stat() to do_io_accounting()
There's two patterns of taking the RCU read-lock and the sig->stats_lock read-seqlock in do_task_stat() and do_io_accounting(), with a different ordering:
# do_io_accounting():
guard(rcu)();
scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) {
# do_task_stat():
scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) {
...
rcu_read_lock();The ordering is RCU-read+seqlock_read in the first case, seqlock_read+RCU-read in the second case.
While technically these read locks can be taken in any order, nevertheless it's good practice to use the more intrusive lock on the inside (which is the IRQs-off section in this case), and reduces head-scratching during review when done consistently, so let's use the do_io_accounting() pattern in do_task_stat().
This will also reduce irqs-off latencies in do_task_stat() a tiny bit.
--- fs/proc/array.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
fs/proc/array.c +20 -20
@@ -537,27 +537,27 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,if (permitted && (!whole || num_threads <2 )) wchan = !task_is_running (task); - scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) { - cmin_flt = sig->cmin_flt; - cmaj_flt = sig->cmaj_flt; - cutime = sig->cutime; - cstime = sig->cstime; - cgtime = sig->cgtime; - -if (whole) { -struct task_struct *t; - - min_flt = sig->min_flt; - maj_flt = sig->maj_flt; - gtime = sig->gtime; - -rcu_read_lock (); -__for_each_thread (sig, t) { - min_flt += t->min_flt; - maj_flt += t->maj_flt; - gtime +=task_gtime (t); +scoped_guard (rcu) { + scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) { + cmin_flt = sig->cmin_flt; + cmaj_flt = sig->cmaj_flt; + cutime = sig->cutime; + cstime = sig->cstime; + cgtime = sig->cgtime; + +if (whole) { +struct task_struct *t; + + min_flt = sig->min_flt; + maj_flt = sig->maj_flt; + gtime = sig->gtime; + +__for_each_thread (sig, t) { + min_flt += t->min_flt; + maj_flt += t->maj_flt; + gtime +=task_gtime (t); + } } -rcu_read_unlock (); } }
Re: [PATCH -v2] seqlock, procfs: Match scoped_seqlock_read() critical section vs. RCU ordering in do_task_stat() to do_io_accounting()
Quoted 5 lines · Ingo Molnar
Quoted 2 lines · Oleg Nesterov
Quoted 1 line · Ingo Molnar
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
RCU read-lock should not nest inside a read-seqlock irqsave ->stats_lock IRQs-off critical section,
Hmm... I agree with this patch, but is it actually wrong?
I thought that rcu_read_lock/unlock is safe under
spin_lock_irq...Yeah, true - it's allowed and not a bug,
OK, thanks ;)
Quoted 4 lines
merely
discouraged inside irqs-off sections if it can be
avoided, and it's an inconsistency versusdo_io_accounting().
Yes, yes, agreed
Quoted 2 lines
How about the -v2 phrasing below? I also removed the Fixes tags.
Re: [PATCH -v2] seqlock, procfs: Match scoped_seqlock_read() critical section vs. RCU ordering in do_task_stat() to do_io_accounting()
Quoted 22 lines · Oleg Nesterov
Quoted 5 lines · Ingo Molnar
Quoted 2 lines · Oleg Nesterov
Quoted 1 line · Ingo Molnar
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
RCU read-lock should not nest inside a read-seqlock irqsave ->stats_lock IRQs-off critical section,
Hmm... I agree with this patch, but is it actually wrong?
I thought that rcu_read_lock/unlock is safe under
spin_lock_irq...Yeah, true - it's allowed and not a bug,
OK, thanks ;)
Quoted 4 lines
merely
discouraged inside irqs-off sections if it can be
avoided, and it's an inconsistency versusdo_io_accounting().
Quoted 6 lines
Yes, yes, agreed
Quoted 2 lines
How about the -v2 phrasing below? I also removed the Fixes tags.
Acked-by Oleg Nesterov <[email protected]>
Thanks Oleg!
Ingo
[tip: locking/urgent] seqlock, procfs: Match scoped_seqlock_read() critical section vs. RCU ordering in do_task_stat() to do_io_accounting()
In reply to Ingo MolnarThe following commit has been merged into the locking/urgent branch of tip:
The following commit has been merged into the locking/urgent branch of tip:
Commit-ID: 24bc5ea5c01a7695a1308ac24435810855ec71c9 Gitweb: https://git.kernel.org/tip/24bc5ea5c01a7695a1308ac24435810855ec71c9 Author: Ingo Molnar <[email protected]> AuthorDate: Tue, 02 Dec 2025 10:05:10 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Tue, 02 Dec 2025 11:21:07 +01:00
seqlock, procfs: Match scoped_seqlock_read() critical section vs. RCU ordering in do_task_stat() to do_io_accounting()
There's two patterns of taking the RCU read-lock and the sig->stats_lock read-seqlock in do_task_stat() and do_io_accounting(), with a different ordering:
# do_io_accounting():
guard(rcu)();
scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) {
# do_task_stat():
scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) {
...
rcu_read_lock();The ordering is RCU-read+seqlock_read in the first case, seqlock_read+RCU-read in the second case.
While technically these read locks can be taken in any order, nevertheless it's good practice to use the more intrusive lock on the inside (which is the IRQs-off section in this case), and reduces head-scratching during review when done consistently, so let's use the do_io_accounting() pattern in do_task_stat().
This will also reduce irqs-off latencies in do_task_stat() a tiny bit.
--- fs/proc/array.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
fs/proc/array.c +20 -20
@@ -537,27 +537,27 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,if (permitted && (!whole || num_threads <2 )) wchan = !task_is_running (task); - scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) { - cmin_flt = sig->cmin_flt; - cmaj_flt = sig->cmaj_flt; - cutime = sig->cutime; - cstime = sig->cstime; - cgtime = sig->cgtime; - -if (whole) { -struct task_struct *t; - - min_flt = sig->min_flt; - maj_flt = sig->maj_flt; - gtime = sig->gtime; - -rcu_read_lock (); -__for_each_thread (sig, t) { - min_flt += t->min_flt; - maj_flt += t->maj_flt; - gtime +=task_gtime (t); +scoped_guard (rcu) { + scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) { + cmin_flt = sig->cmin_flt; + cmaj_flt = sig->cmaj_flt; + cutime = sig->cutime; + cstime = sig->cstime; + cgtime = sig->cgtime; + +if (whole) { +struct task_struct *t; + + min_flt = sig->min_flt; + maj_flt = sig->maj_flt; + gtime = sig->gtime; + +__for_each_thread (sig, t) { + min_flt += t->min_flt; + maj_flt += t->maj_flt; + gtime +=task_gtime (t); + } } -rcu_read_unlock (); } }