diff options
| author | Bradley Morgan <brads@mainlining.org> | 2026-08-04 09:34:00 +0000 |
|---|---|---|
| committer | Andrew Morton <akpm@linux-foundation.org> | 2026-08-27 22:50:03 -0700 |
| commit | d1a0b8c9f068d5e9b5cd65993dd2bec0ef0101cc (patch) | |
| tree | 602cae6f315aac3a96389b45420ee825c2083a0e /scripts | |
| parent | 64b4b3bd37ab539bc32620a9554889745dd80d44 (diff) | |
| download | linux-next-d1a0b8c9f068d5e9b5cd65993dd2bec0ef0101cc.tar.gz linux-next-d1a0b8c9f068d5e9b5cd65993dd2bec0ef0101cc.zip | |
taskstats: copy signal->stats under siglock in taskstats_exit
taskstats_exit() copies tsk->signal->stats into the exit reply without
taking any lock. Every other writer of this struct holds sighand->siglock
before touching it, and this copy does not.
The copy happens on the last thread of a thread group that exits.
group_dead being 1 only says that every thread has dropped signal->live,
it does not say how far the other threads got in do_exit(). One of them
can still be inside fill_tgid_exit() adding its counters to the struct
while the last thread copies it out, so the copy can read the struct in
the middle of an update.
The commit that added the copy assumed no locking was needed because the
group was dead:
/* No locking needed for tsk->signal->stats since
group is dead */
but at that point the other threads have not necessarily finished
their exit path.
cpu0 (thread A, not last) cpu1 (thread B, last)
=========================== ==============================
atomic_dec(&signal->live) atomic_dec(&signal->live) -> 0
group_dead = 0 group_dead = 1
... taskstats_exit(tsk, 1)
taskstats_exit(tsk, 0) fill_tgid_exit(tsk) [siglock]
fill_tgid_exit(tsk) memcpy(stats, signal->stats)
spin_lock(siglock) reads ac_utime (new)
stats->ac_utime += x reads ac_stime (old)
stats->ac_stime += y torn snapshot -> netlink
spin_unlock(siglock)
The listeners receive a partially updated tgid snapshot, with some fields
from before the concurrent update and some from after. There is no crash
or splat, which is likely why this went unnoticed since 2006. A userspace
model of the same shape, writer under a lock and a lockless memcpy reader,
produces millions of torn reads in a few seconds.
Take siglock around the copy like every other access does. sighand is
still alive here because taskstats_exit() runs before exit_notify(), and
fill_tgid_exit() already takes this same lock earlier in this function.
Link: https://lore.kernel.org/20260804093400.3922-1-include@grrlz.net
Fixes: ad4ecbcba728 ("[PATCH] delay accounting taskstats interface send tgid once")
Signed-off-by: Bradley Morgan <brads@mainlining.org>
Acked-by: Oleg Nesterov <oleg@redhat.com>
Cc: Balbir Singh <bsingharora@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'scripts')
0 files changed, 0 insertions, 0 deletions
