Skip to contentSkip to conversation

taskstats: Avoid counting an exiting thread twice in TGID queries

PATCH In review Baek Kyu, Kim 3 participants 5 messages Oct 31 – Nov 7

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 taskstats: Avoid counting an exiting thread twice in TGID queries

fill_tgid_exit() adds a thread's contribution to signal->stats before exit_notify() sets exit_state. A TGID query in that interval copies the cached contribution, then scans the same thread because exit_state is still clear. The thread's contribution is counted twice.

Mark a task when its contribution reaches the cache, under the same siglock used by the TGID query. Exclude marked tasks from the live scan. Leave a task eligible for the scan if no cache exists, and clear the marker when a new task is copied during fork.

Fixes ad4ecbcba728 ("[PATCH] delay accounting taskstats interface send tgid once")

---
In an arm64 QEMU guest, holding the interval after cache accounting but before exit_state changed made a 100 ms worker appear twice in a TGID CPU query. The baseline reported 208000 us for 8000 us of the querying thread plus 100000 us of the worker; the change reported 112000 us for 12000 us plus 100000 us. Natural race frequency and other architectures were not measured; KASAN and KCSAN were not enabled.

 include/linux/sched.h | 5 +++++
 kernel/fork.c         | 3 +++
 kernel/taskstats.c    | 7 ++++---
 3 files changed, 12 insertions(+), 3 deletions(-)
include/linux/sched.h +5 -0
index d35ae49a991f..359ee982323c 100644
@@ -1457,6 +1457,11 @@ struct task_struct {
 	struct task_delay_info		*delays;
 #endif
 
+#ifdef CONFIG_TASKSTATS
+	/* Protected by sighand->siglock. */
+	bool				taskstats_tgid_accounted;
+#endif
+
 #ifdef CONFIG_FAULT_INJECTION
 	int				make_it_fail;
 	unsigned int			fail_nth;
kernel/fork.c +3 -0
index 33760ece3bb3..76d9abcc5ca8 100644
@@ -2202,6 +2202,9 @@ __latent_entropy struct task_struct *copy_process(
 		goto bad_fork_cleanup_count;
 
 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
+#ifdef CONFIG_TASKSTATS
+	p->taskstats_tgid_accounted = false;
+#endif
 	p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE | PF_NO_SETAFFINITY);
 	p->flags |= PF_FORKNOEXEC;
 	INIT_LIST_HEAD(&p->children);
kernel/taskstats.c +4 -3
index f31df72f0e9d..dd9054de761f 100644
@@ -245,8 +245,8 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
 	u64 now_ns;
 
 	/*
-	 * Add additional stats from live tasks except zombie thread group
-	 * leaders who are already counted with the dead tasks
+	 * Add stats from tasks not yet included in the dead-task cache.
+	 * Accounting can finish before exit_state is set.
 	 */
 	rcu_read_lock();
 	first = find_task_by_vpid(tgid);
@@ -261,7 +261,7 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
 
 	now_ns = ktime_get_ns();
 	for_each_thread(first, tsk) {
-		if (tsk->exit_state)
+		if (tsk->exit_state || tsk->taskstats_tgid_accounted)
 			continue;
 
 		tgid_stats_add_task(stats, tsk, now_ns);
@@ -291,6 +291,7 @@ static void fill_tgid_exit(struct task_struct *tsk)
 
 	now_ns = ktime_get_ns();
 	tgid_stats_add_task(tsk->signal->stats, tsk, now_ns);
+	tsk->taskstats_tgid_accounted = true;
 ret:
 	spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
 	return;
Signature · 1 line
2.55.0
#1 lore raw
Quoted 9 lines · Kim

fill_tgid_exit() adds a thread's contribution to signal->stats before exit_notify() sets exit_state. A TGID query in that interval copies the cached contribution, then scans the same thread because exit_state is still clear. The thread's contribution is counted twice.

Mark a task when its contribution reaches the cache, under the same siglock used by the TGID query. Exclude marked tasks from the live scan. Leave a task eligible for the scan if no cache exists, and clear the marker when a new task is copied during fork.

This part of the changelog would need to go, see below.

Quoted 2 lines
Fixes ad4ecbcba728 ("[PATCH] delay accounting taskstats interface send tgid once")

Thx, Oleg already suggested this kinda thing

Quoted 35 lines

---
In an arm64 QEMU guest, holding the interval after cache accounting but before exit_state changed made a 100 ms worker appear twice in a TGID CPU query. The baseline reported 208000 us for 8000 us of the querying thread plus 100000 us of the worker; the change reported 112000 us for 12000 us plus 100000 us. Natural race frequency and other architectures were not measured; KASAN and KCSAN were not enabled.

include/linux/sched.h | 5 +++++
kernel/fork.c         | 3 +++
kernel/taskstats.c    | 7 ++++---
3 files changed, 12 insertions(+), 3 deletions(-)
include/linux/sched.h +5 -0
index d35ae49a991f..359ee982323c 100644
@@ -1457,6 +1457,11 @@ struct task_struct {
 	struct task_delay_info		*delays;
 #endif
 
+#ifdef CONFIG_TASKSTATS
+	/* Protected by sighand->siglock. */
+	bool				taskstats_tgid_accounted;
+#endif
+
 #ifdef CONFIG_FAULT_INJECTION
 	int				make_it_fail;
 	unsigned int			fail_nth;
kernel/fork.c +0 -0
index 33760ece3bb3..76d9abcc5ca8 100644

kernel/fork.c __latent_entropy struct task_struct *copy_process( · line 2207

Quoted 6 lines
kernel/fork.c +3 -0
@@ -2202,6 +2202,9 @@ __latent_entropy struct task_struct *copy_process(
 		goto bad_fork_cleanup_count;
 
 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
+#ifdef CONFIG_TASKSTATS
+	p->taskstats_tgid_accounted = false;
+#endif

Id like to pray that this is dead, and I'm not just being a idiot.

The flag is set in fill_tgid_exit() and the task that already is past taskstats_exit() in do_exit, it can't call fork() again, dup_task_struct() copies from current, so I'd reckon that this is useless.

Quoted 33 lines
kernel/fork.c +0 -0
 	p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE | PF_NO_SETAFFINITY);
 	p->flags |= PF_FORKNOEXEC;
 	INIT_LIST_HEAD(&p->children);
kernel/taskstats.c +4 -3
index f31df72f0e9d..dd9054de761f 100644
@@ -245,8 +245,8 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
 	u64 now_ns;
 
 	/*
-	 * Add additional stats from live tasks except zombie thread group
-	 * leaders who are already counted with the dead tasks
+	 * Add stats from tasks not yet included in the dead-task cache.
+	 * Accounting can finish before exit_state is set.
 	 */
 	rcu_read_lock();
 	first = find_task_by_vpid(tgid);
@@ -261,7 +261,7 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
 
 	now_ns = ktime_get_ns();
 	for_each_thread(first, tsk) {
-		if (tsk->exit_state)
+		if (tsk->exit_state || tsk->taskstats_tgid_accounted)
 			continue;
 
 		tgid_stats_add_task(stats, tsk, now_ns);
@@ -291,6 +291,7 @@ static void fill_tgid_exit(struct task_struct *tsk)
 
 	now_ns = ktime_get_ns();
 	tgid_stats_add_task(tsk->signal->stats, tsk, now_ns);
+	tsk->taskstats_tgid_accounted = true;

ret:

Quoted 3 lines
	spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
	return;

Btw: you've sent 8 patches today, please stop spamming please.

--- Thanks!

#2 lore raw
In reply to Baek Kyu, Kim
Quoted 5 lines · Kim

fill_tgid_exit() adds a thread's contribution to signal->stats before exit_notify() sets exit_state. A TGID query in that interval copies the cached contribution, then scans the same thread because exit_state is still clear. The thread's contribution is counted twice.

I forgot everything about this code, quite possibly I am wrong but let me add some comments anyway...

Yes, known problem. Plus afaics fill_stats_for_tgid() can also miss an exiting task, see below.

Another problem is that taskstats_exit(group_exit => true) can miss other threads which have not called taskstats_exit() yet and report TGID/AGROUP. Afaics this patch doesn't try to fix this.

kernel/fork.c __latent_entropy struct task_struct *copy_process( · line 2207

Quoted 9 lines
kernel/fork.c +3 -0
@@ -2202,6 +2202,9 @@ __latent_entropy struct task_struct *copy_process(
 		goto bad_fork_cleanup_count;
 
 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
+#ifdef CONFIG_TASKSTATS
+	p->taskstats_tgid_accounted = false;
+#endif

I agree with Bradley, this looks unnecessary.

kernel/fork.c static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats) · line 265

Quoted 7 lines
kernel/fork.c +1 -1
@@ -261,7 +261,7 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)
 
 	now_ns = ktime_get_ns();
 	for_each_thread(first, tsk) {
-		if (tsk->exit_state)
+		if (tsk->exit_state || tsk->taskstats_tgid_accounted)
 			continue;

If we add taskstats_tgid_accounted, why do we need to check exit_state?

In the multithreaded case, if taskstats_tgid_accounted is true, then exit_state should be true too (ignoring the kmem_cache_zalloc() failures).

In the single threaded case this looks simply wrong. The exiting single threaded task will not call fill_tgid_exit(). If cmd_attr_tgid() is called after exit_notify(), it will miss that zombie.

And... Perhaps it would be better to use ->atomic_flags rather than add yet another member to task_struct?

Oleg.

#3 lore raw
Quoted 22 lines · Oleg Nesterov
Quoted 5 lines · Kim

fill_tgid_exit() adds a thread's contribution to signal->stats before exit_notify() sets exit_state. A TGID query in that interval copies the cached contribution, then scans the same thread because exit_state is still clear. The thread's contribution is counted twice.

I forgot everything about this code, quite possibly I am wrong but let me add some comments anyway...

Yes, known problem. Plus afaics fill_stats_for_tgid() can also miss an exiting task, see below.

Another problem is that taskstats_exit(group_exit => true) can miss other threads which have not called taskstats_exit() yet and report TGID/AGROUP. Afaics this patch doesn't try to fix this.

Quoted 5 lines
kernel/fork.c +0 -0
@@ -2202,6 +2202,9 @@ __latent_entropy struct task_struct *copy_process(
 		goto bad_fork_cleanup_count;
 
Quoted 7 lines

kernel/fork.c __latent_entropy struct task_struct *copy_process( · line 2207

Quoted 4 lines
kernel/fork.c +3 -0
 	delayacct_tsk_init(p);	/* Must remain after dup_task_struct() */
+#ifdef CONFIG_TASKSTATS
+	p->taskstats_tgid_accounted = false;
+#endif

I agree with Bradley, this looks unnecessary.

I checked, it is dead code, it IS unnecessary (impossible to reach)

Quoted 19 lines

kernel/fork.c static int fill_stats_for_tgid(pid_t tgid, struct

Quoted 1 line
kernel/fork.c +0 -0
@@ -261,7 +261,7 @@ static int fill_stats_for_tgid(pid_t tgid, struct

taskstats *stats)

kernel/fork.c static int fill_stats_for_tgid(pid_t tgid, struct · line 264

Quoted 6 lines
kernel/fork.c +1 -1
 	now_ns = ktime_get_ns();
 	for_each_thread(first, tsk) {
-		if (tsk->exit_state)
+		if (tsk->exit_state || tsk->taskstats_tgid_accounted)
 			continue;

If we add taskstats_tgid_accounted, why do we need to check exit_state?

In the multithreaded case, if taskstats_tgid_accounted is true, then exit_state should be true too (ignoring the kmem_cache_zalloc() failures).

In the single threaded case this looks simply wrong. The exiting single threaded task will not call fill_tgid_exit(). If cmd_attr_tgid() is called after exit_notify(), it will miss that zombie.

Quoted 5 lines

And... Perhaps it would be better to use ->atomic_flags rather than add yet another member to task_struct?

Oleg.

--- Thanks!

#4 lore raw

PATCH v2 1/1 taskstats: account exiting threads once in TGID reports

In reply to Baek Kyu, Kim

A TGID query can count an exiting thread twice after its contribution reaches the cache, or miss a zombie thread whose contribution has not been cached. The final TGID exit notification can also be sent before other exiting threads finish cache accounting.

Track whether each exiting thread has contributed to the cache, and use that state when collecting TGID statistics. Defer the final exit notification until all exiting threads finish cache accounting. This keeps TGID queries and the final notification consistent across thread exit races.

In an arm64 QEMU/KVM guest, forced exit races confirmed that TGID queries count a thread once and the final notification includes both exiting threads. The existing taskstats selftest passed.

Fixes ad4ecbcba728 ("[PATCH] delay accounting taskstats interface send tgid once")

---
Changes in v2:
- Remove the fork-time reset: a task cannot fork after taskstats_exit().
- Drop the exit_state check: an unaccounted zombie still belongs in the TGID query, including for a single-threaded process.
- Defer the final PID/AGROUP and TGID notification until every exiting thread has finished cache accounting; keep both records in one message.
- Use task_struct::atomic_flags rather than adding a new task_struct member.
- Mark failed cache allocation as finished so a deferred notification is not left pending.

v1: https://lists.openwall.net/linux-kernel/2026/10/31/267

 include/linux/sched.h        |   9 ++++
 include/linux/sched/signal.h |   5 +++
 kernel/taskstats.c           | 104 +++++++++++++++++++++++++++++++++----------
 3 files changed, 95 insertions(+), 23 deletions(-)
include/linux/sched.h +9 -0
index d35ae49a991f7..257e6028c88c1 100644
@@ -1875,6 +1875,8 @@ static __always_inline bool is_user_task(struct task_struct *task)
 #define PFA_SPEC_IB_DISABLE		5	/* Indirect branch speculation restricted */
 #define PFA_SPEC_IB_FORCE_DISABLE	6	/* Indirect branch speculation permanently restricted */
 #define PFA_SPEC_SSB_NOEXEC		7	/* Speculative Store Bypass clear on execve() */
+#define PFA_TASKSTATS_TGID_ACCOUNTED	8	/* Included in signal->stats */
+#define PFA_TASKSTATS_TGID_SKIPPED	9	/* No TGID cache at taskstats_exit */
 
 #define TASK_PFA_TEST(name, func)					\
 	static inline bool task_##func(struct task_struct *p)		\
@@ -1913,6 +1915,13 @@ TASK_PFA_CLEAR(SPEC_IB_DISABLE, spec_ib_disable)
 TASK_PFA_TEST(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable)
 TASK_PFA_SET(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable)
 
+#ifdef CONFIG_TASKSTATS
+TASK_PFA_TEST(TASKSTATS_TGID_ACCOUNTED, tgid_stats_accounted)
+TASK_PFA_SET(TASKSTATS_TGID_ACCOUNTED, tgid_stats_accounted)
+TASK_PFA_TEST(TASKSTATS_TGID_SKIPPED, tgid_stats_skipped)
+TASK_PFA_SET(TASKSTATS_TGID_SKIPPED, tgid_stats_skipped)
+#endif
+
 static inline void
 current_restore_flags(unsigned long orig_flags, unsigned long flags)
 {
include/linux/sched/signal.h +5 -0
index efa1e2d6c7a6b..b6f4e24e1552e 100644
@@ -14,6 +14,9 @@
 #include <linux/mm_types.h>
 #include <asm/ptrace.h>
 
+struct listener_list;
+struct sk_buff;
+
 /*
  * Types defining task->signal and task->sighand and APIs using them:
  */
@@ -221,6 +224,8 @@ struct signal_struct {
 #endif
 #ifdef CONFIG_TASKSTATS
 	struct taskstats *stats;
+	struct sk_buff *taskstats_pending_skb;
+	struct listener_list *taskstats_pending_listeners;
 #endif
 #ifdef CONFIG_AUDIT
 	unsigned audit_tty;
kernel/taskstats.c +81 -23

165 lines not on this page · raw message

Signature · 1 line
2.55.0