taskstats: fix TGID dead-thread stat retention
Subsystems (3)
v3 cover taskstats: fix TGID dead-thread stat retention
This series fixes a taskstats TGID aggregation bug where fields added in the TGID query path were not preserved after thread exit, and adds a kselftest covering the regression.
The first patch keeps the cached TGID aggregate used for dead threads in step with the fields already accumulated for live threads, and also fixes the final TGID exit notification emitted when group_dead is true.
The second patch adds a kselftest that verifies TGID CPU stats do not regress after a worker thread exits and has been reaped.
---
Changes in v3:
- fix the helper comment in patch 1 to use per-task-foo(stats, tsk)
- add Acked-by tags to both patches
- Link to v2: <https://lore.kernel.org/all/[email protected]/>
Changes in v2:
- add Fixes tags for the two commits that introduced the regression
- clarify that the regression affects both TGID queries and the final
TGID exit notification
- add a kselftest that checks TGID CPU stats do not regress after
thread exit
- Link to v1: <https://lore.kernel.org/all/6f4ed79d96c389a9a1d67d5ced96c6326eda82ae.1774552296.git.cyyzero16@gmail.com/>
Signature · 1 line
2.43.0
PATCH v3 1/2 taskstats: retain dead thread stats in TGID queries
fill_stats_for_tgid() builds TGID stats from two sources: the cached aggregate in signal->stats and a scan of the live threads in the group.
However, fill_tgid_exit() only accumulates delay accounting into signal->stats. This means that once a thread exits, TGID queries lose the fields that fill_stats_for_tgid() adds for live threads.
This gap was introduced incrementally by two earlier changes that extended fill_stats_for_tgid() but did not make the corresponding update to fill_tgid_exit():
- commit 8c733420bdd5 ("taskstats: add e/u/stime for TGID command") added ac_etime, ac_utime, and ac_stime to the TGID query path.
- commit b663a79c1915 ("taskstats: add context-switch counters") added nvcsw and nivcsw to the TGID query path.
As a result, those fields were accounted for live threads in TGID queries, but were dropped from the cached TGID aggregate after thread exit. The final TGID exit notification emitted when group_dead is true also copies that cached aggregate, so it loses the same fields.
Factor the per-task TGID accumulation into tgid_stats_add_task() and use it in both fill_stats_for_tgid() and fill_tgid_exit(). This keeps the cached aggregate used for dead threads aligned with the live-thread accumulation used by TGID queries.
kernel/taskstats.c +33 -29
@@ -210,13 +210,39 @@ static int fill_stats_for_pid(pid_t pid, struct taskstats *stats)return 0 ; } +static void tgid_stats_add_task (struct taskstats *stats, +struct task_struct *tsk,u64 now_ns) +{ +u64 delta, utime, stime; + +/* +* Each accounting subsystem calls its functions here to +* accumulate its per-task stats for tsk, into the per-tgid structure +* +* per-task-foo(stats, tsk); +*/ +delayacct_add_tsk (stats, tsk); + +/* calculate task elapsed time in nsec */ + delta = now_ns - tsk->start_time; +/* Convert to micro seconds */ +do_div (delta,NSEC_PER_USEC ); + stats->ac_etime += delta; + +task_cputime (tsk, &utime, &stime); + stats->ac_utime +=div_u64 (utime,NSEC_PER_USEC ); + stats->ac_stime +=div_u64 (stime,NSEC_PER_USEC ); + + stats->nvcsw += tsk->nvcsw; + stats->nivcsw += tsk->nivcsw; +} +static int fill_stats_for_tgid (pid_t tgid,struct taskstats *stats) {struct task_struct *tsk, *first;unsigned long flags;int rc = -ESRCH ; -u64 delta, utime, stime; -u64 start_time; +u64 now_ns;/* * Add additional stats from live tasks except zombie thread group
@@ -233,30 +259,12 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)else memset (stats,0 ,sizeof (*stats)); - start_time =ktime_get_ns (); + now_ns =ktime_get_ns ();for_each_thread (first, tsk) {if (tsk->exit_state)continue ; -/* -* Accounting subsystem can call its functions here to -* fill in relevant parts of struct taskstsats as follows -* -* per-task-foo(stats, tsk); -*/ -delayacct_add_tsk (stats, tsk); - -/* calculate task elapsed time in nsec */ - delta = start_time - tsk->start_time; -/* Convert to micro seconds */ -do_div (delta,NSEC_PER_USEC ); - stats->ac_etime += delta; -task_cputime (tsk, &utime, &stime); - stats->ac_utime +=div_u64 (utime,NSEC_PER_USEC ); - stats->ac_stime +=div_u64 (stime,NSEC_PER_USEC ); - - stats->nvcsw += tsk->nvcsw; - stats->nivcsw += tsk->nivcsw; +tgid_stats_add_task (stats, tsk, now_ns); }unlock_task_sighand (first, &flags);
@@ -275,18 +283,14 @@ static int fill_stats_for_tgid(pid_t tgid, struct taskstats *stats)static void fill_tgid_exit (struct task_struct *tsk) {unsigned long flags; +u64 now_ns;spin_lock_irqsave (&tsk->sighand->siglock, flags);if (!tsk->signal->stats) goto ret; -/* -* Each accounting subsystem calls its functions here to -* accumalate its per-task stats for tsk, into the per-tgid structure -* -* per-task-foo(tsk->signal->stats, tsk); -*/ -delayacct_add_tsk (tsk->signal->stats, tsk); + now_ns =ktime_get_ns (); +tgid_stats_add_task (tsk->signal->stats, tsk, now_ns); ret:spin_unlock_irqrestore (&tsk->sighand->siglock, flags);return ;
Signature · 1 line
2.43.0
PATCH v3 2/2 selftests/acct: add taskstats TGID retention test
In reply to Yiyang ChenAdd a kselftest for the taskstats TGID aggregation fix.
The test creates a worker thread, snapshots TGID taskstats while the worker is still alive, lets the worker exit, and then verifies that the TGID CPU total does not regress after the thread has been reaped.
The pass/fail check intentionally keys off ac_utime + ac_stime only, which is the primary user-visible regression fixed by the taskstats change and is less sensitive to scheduling noise than context-switch counters.
tools/testing/selftests/acct/.gitignore +2 -1
@@ -1,3 +1,4 @@ acct_syscall +taskstats_fill_stats_tgid config -process_log +process_log
tools/testing/selftests/acct/Makefile +4 -1
@@ -1,5 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 TEST_GEN_PROGS := acct_syscall +TEST_GEN_PROGS += taskstats_fill_stats_tgid + CFLAGS += -Wall +LDLIBS += -lpthread -include ../lib.mk +include ../lib.mk
tools/testing/selftests/acct/taskstats_fill_stats_tgid.c new file +375 -0
375 lines not on this page · raw message
Signature · 1 line
2.43.0
Hi Andrew,
Just a gentle ping on this taskstats fix series [1], which fixes a TGID aggregation bug and adds a kselftest.
The series already has an Ack from Balbir [2], so I wanted to check if there’s anything else needed from my side, or if it looks suitable for you to pick up into nonmm-unstable?
Thanks,
Yiyang
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/