Skip to contentSkip to conversation

fs/proc: do_task_stat: use sig->stats_lock to gather the threads/children stats

PATCH In review Bin Lan 2 participants 2 messages Nov 25, 2024 Stable branch

lore
Share & tools
lore

Bin Lan

author
#0 lore raw

PATCH 1/1 fs/proc: do_task_stat: use sig->stats_lock to gather the threads/children stats

From: Oleg Nesterov <[email protected]>

[ Upstream commit 7601df8031fd67310af891897ef6cc0df4209305 ]

lock_task_sighand() can trigger a hard lockup. If NR_CPUS threads call do_task_stat() at the same time and the process has NR_THREADS, it will spin with irqs disabled O(NR_CPUS * NR_THREADS) time.

Change do_task_stat() to use sig->stats_lock to gather the statistics outside of ->siglock protected section, in the likely case this code will run lockless.

---
 fs/proc/array.c | 57 +++++++++++++++++++++++++++----------------------
 1 file changed, 32 insertions(+), 25 deletions(-)
fs/proc/array.c +32 -25
index 37b8061d84bb..34a47fb0c57f 100644
@@ -477,13 +477,13 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 	int permitted;
 	struct mm_struct *mm;
 	unsigned long long start_time;
-	unsigned long cmin_flt = 0, cmaj_flt = 0;
-	unsigned long  min_flt = 0,  maj_flt = 0;
-	u64 cutime, cstime, utime, stime;
-	u64 cgtime, gtime;
+	unsigned long cmin_flt, cmaj_flt, min_flt, maj_flt;
+	u64 cutime, cstime, cgtime, utime, stime, gtime;
 	unsigned long rsslim = 0;
 	unsigned long flags;
 	int exit_code = task->exit_code;
+	struct signal_struct *sig = task->signal;
+	unsigned int seq = 1;
 
 	state = *get_task_state(task);
 	vsize = eip = esp = 0;
@@ -511,12 +511,8 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 
 	sigemptyset(&sigign);
 	sigemptyset(&sigcatch);
-	cutime = cstime = 0;
-	cgtime = gtime = 0;
 
 	if (lock_task_sighand(task, &flags)) {
-		struct signal_struct *sig = task->signal;
-
 		if (sig->tty) {
 			struct pid *pgrp = tty_get_pgrp(sig->tty);
 			tty_pgrp = pid_nr_ns(pgrp, ns);
@@ -527,26 +523,9 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 		num_threads = get_nr_threads(task);
 		collect_sigign_sigcatch(task, &sigign, &sigcatch);
 
-		cmin_flt = sig->cmin_flt;
-		cmaj_flt = sig->cmaj_flt;
-		cutime = sig->cutime;
-		cstime = sig->cstime;
-		cgtime = sig->cgtime;
 		rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
 
-		/* add up live thread stats at the group level */
 		if (whole) {
-			struct task_struct *t = task;
-			do {
-				min_flt += t->min_flt;
-				maj_flt += t->maj_flt;
-				gtime += task_gtime(t);
-			} while_each_thread(task, t);
-
-			min_flt += sig->min_flt;
-			maj_flt += sig->maj_flt;
-			gtime += sig->gtime;
-
 			if (sig->flags & (SIGNAL_GROUP_EXIT | SIGNAL_STOP_STOPPED))
 				exit_code = sig->group_exit_code;
 		}
@@ -561,6 +540,34 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
 	if (permitted && (!whole || num_threads < 2))
 		wchan = !task_is_running(task);
 
+	do {
+		seq++; /* 2 on the 1st/lockless path, otherwise odd */
+		flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
+
+		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);
+			}
+			rcu_read_unlock();
+		}
+	} while (need_seqretry(&sig->stats_lock, seq));
+	done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
+
 	if (whole) {
 		thread_group_cputime_adjusted(task, &utime, &stime);
 	} else {
Signature · 1 line
2.43.0
maintainer
#1 lore raw

[ Sasha's backport helper bot ]

Hi,

The upstream commit SHA1 provided is correct: 7601df8031fd67310af891897ef6cc0df4209305

WARNING: Author mismatch between patch and upstream commit:
Backport author: Bin Lan <[email protected]> Commit author: Oleg Nesterov <[email protected]>

Status in newer kernel trees:
6.12.y | Present (exact SHA1)
6.11.y | Present (exact SHA1)
6.6.y | Not found

Note: The patch differs from the upstream commit:
---

tmp/tmp.DBppGejMK4 renamed from - +15 -9
@@ -1,3 +1,5 @@
+[ Upstream commit 7601df8031fd67310af891897ef6cc0df4209305 ]
+
 lock_task_sighand() can trigger a hard lockup.  If NR_CPUS threads call
 do_task_stat() at the same time and the process has NR_THREADS, it will
 spin with irqs disabled O(NR_CPUS * NR_THREADS) time.
@@ -12,12 +14,14 @@
 Cc: Eric W. Biederman <[email protected]>
 Cc: <[email protected]>
 Signed-off-by: Andrew Morton <[email protected]>
+[ Resolve minor conflicts ]
+Signed-off-by: Bin Lan <[email protected]>
 ---
- fs/proc/array.c | 58 +++++++++++++++++++++++++++----------------------
- 1 file changed, 32 insertions(+), 26 deletions(-)
+ fs/proc/array.c | 57 +++++++++++++++++++++++++++----------------------
+ 1 file changed, 32 insertions(+), 25 deletions(-)
 
 diff --git a/fs/proc/array.c b/fs/proc/array.c
-index 45ba918638084..34a47fb0c57f2 100644
+index 37b8061d84bb..34a47fb0c57f 100644
 --- a/fs/proc/array.c
 +++ b/fs/proc/array.c
 @@ -477,13 +477,13 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
@@ -51,7 +55,7 @@
  		if (sig->tty) {
  			struct pid *pgrp = tty_get_pgrp(sig->tty);
  			tty_pgrp = pid_nr_ns(pgrp, ns);
-@@ -527,27 +523,9 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
+@@ -527,26 +523,9 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
  		num_threads = get_nr_threads(task);
  		collect_sigign_sigcatch(task, &sigign, &sigcatch);
  
@@ -64,13 +68,12 @@
  
 -		/* add up live thread stats at the group level */
  		if (whole) {
--			struct task_struct *t;
--
--			__for_each_thread(sig, t) {
+-			struct task_struct *t = task;
+-			do {
 -				min_flt += t->min_flt;
 -				maj_flt += t->maj_flt;
 -				gtime += task_gtime(t);
--			}
+-			} while_each_thread(task, t);
 -
 -			min_flt += sig->min_flt;
 -			maj_flt += sig->maj_flt;
@@ -79,7 +82,7 @@
  			if (sig->flags & (SIGNAL_GROUP_EXIT | SIGNAL_STOP_STOPPED))
  				exit_code = sig->group_exit_code;
  		}
-@@ -562,6 +540,34 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
+@@ -561,6 +540,34 @@ static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
  	if (permitted && (!whole || num_threads < 2))
  		wchan = !task_is_running(task);
  
@@ -114,3 +117,6 @@
  	if (whole) {
  		thread_group_cputime_adjusted(task, &utime, &stime);
  	} else {
+-- 
+2.43.0
+

---

Results of testing on various branches:

| Branch                    | Patch Apply | Build Test |
|---------------------------|-------------|------------|
| stable/linux-6.6.y        |  Success    |  Success   |