Skip to contentSkip to conversation

seqlock, procfs: Fix scoped_seqlock_read() critical section in do_task_stat()

PATCH In review Ingo Molnar 3 participants 6 messages Dec 2, 2025 Proc filesystem

lore
Share & tools
lore
#0 lore raw

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.

Fixes b76f72bea2c6 ("seqlock: Change do_task_stat() to use scoped_seqlock_read()")
Fixes 7601df8031fd ("fs/proc: do_task_stat: use sig->stats_lock to gather the threads/children stats")
Cc Peter Zijlstra <[email protected]>
---
 fs/proc/array.c | 41 +++++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 20 deletions(-)
fs/proc/array.c +21 -20
index cbd4bc4a58e4..6a35825c45a6 100644
@@ -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
#1 lore raw
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.

#2 lore raw

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 versus
do_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
index cbd4bc4a58e4..42932f88141a 100644
@@ -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();
 		}
 	}
 
Acked-by on v1 1/1
#3 lore raw

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 versus
do_io_accounting().

Yes, yes, agreed

Quoted 2 lines

How about the -v2 phrasing below? I also removed the Fixes tags.

#4 lore raw

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 versus
do_io_accounting().

Quoted 6 lines

Yes, yes, agreed

Quoted 2 lines

How about the -v2 phrasing below? I also removed the Fixes tags.

Thanks Oleg!

Ingo

merge bot Applied
#5 lore raw

[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 Molnar
The 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
index cbd4bc4..42932f8 100644
@@ -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();
 		}
 	}