Root Cause Analysis
1. Functions, Structs, and Fields Involved
- Source Files:
kernel/taskstats.c, kernel/exit.c, include/linux/sched/signal.h, include/linux/list.h
- Data Structures and Fields:
struct task_struct::thread_node: The node linking each thread in the thread group's circular doubly-linked list (thread_head).
struct signal_struct::thread_head: The head of the list containing all active threads of the thread group.
struct signal_struct::stats: Pointer to dynamically allocated struct taskstats for thread-group accounting.
struct sighand_struct::siglock / tasklist_lock: The spinlocks that protect modifications to thread_head.
- Functions:
2. Detailed Technical Mechanism and Invariant Violation
In the Linux kernel, a process's thread group list (signal->thread_head) is populated when new threads are spawned via copy_process() (kernel/fork.c:2575) using list_add_tail_rcu(&p->thread_node, &p->signal->thread_head) under tasklist_lock and sighand->siglock. When a sub-thread is unhashed during task release (__exit_signal() -> __unhash_process()), it is removed via list_del_rcu(&p->thread_node) while holding both write_lock_irq(&tasklist_lock) and spin_lock(&sighand->siglock).
list_del_rcu() invokes __list_del(entry->prev, entry->next), which writes to prev->next using WRITE_ONCE(prev->next, next).
Concurrently, during process exit:
-
Reader Task (Group Leader, Task 9915 on CPU 0):
- Task 9915 is the thread group leader executing
do_exit() -> taskstats_exit().
taskstats_exit() calls taskstats_tgid_alloc(tsk):
static struct taskstats *taskstats_tgid_alloc(struct task_struct *tsk)
{
struct signal_struct *sig = tsk->signal;
struct taskstats *stats_new, *stats;
stats = smp_load_acquire(&sig->stats);
if (stats || thread_group_empty(tsk))
return stats;
...
- If
sig->stats has not yet been allocated (e.g., if a previous exiting sub-thread's kmem_cache_zalloc() failed, as permitted by line 538, or if family_registered was not set when earlier threads exited), taskstats_tgid_alloc() executes thread_group_empty(tsk):
static inline int thread_group_empty(struct task_struct *p)
{
return thread_group_leader(p) &&
list_is_last(&p->thread_node, &p->signal->thread_head);
}
list_is_last(&p->thread_node, &p->signal->thread_head) tests:
return list->next == head;
- This performs a plain, unannotated load of
tsk->thread_node.next. Crucially, this read occurs without holding sighand->siglock or tasklist_lock, and without using marked accessors (READ_ONCE() or RCU accessors).
-
Writer Task (Untraced Sub-Thread, Task 9916 on CPU 1):
- Task 9916 is an untraced sub-thread in the same thread group exiting concurrently via
do_exit().
- Because it is an untraced sub-thread,
exit_notify() marks it for automatic reaping (autoreap = true) and immediately invokes release_task(p).
- In
release_task(), under tasklist_lock and sighand->siglock, __exit_signal() -> __unhash_process() calls list_del_rcu(&p->thread_node).
- Because Task 9916 immediately followed leader Task 9915 in
thread_head, p->thread_node.prev is &task_9915->thread_node.
__list_del() writes to prev->next (address 0xffff888101dfdce8) with WRITE_ONCE(), updating it from Task 9916's thread_node (0xffff888104633a68) to the next entry (0xffff8881025186a8).
-
Data Race Trigger:
- Task 9916 performs a concurrent marked 8-byte write (
WRITE_ONCE) to tsk->thread_node.next.
- Task 9915 performs a concurrent plain 8-byte read (
list->next) from tsk->thread_node.next.
- KCSAN flags this data race on
0xffff888101dfdce8.
3. Summary of Flaw
taskstats_tgid_alloc() attempts an optimistic lockless fast-path check via thread_group_empty(tsk) to avoid allocating memory if the process was single-threaded. However, thread_group_empty() relies on list_is_last(), which assumes serialized list access and performs an unannotated plain read of thread_node.next. Because concurrent sub-threads can be unhashed and deleted from thread_head concurrently, calling thread_group_empty(tsk) without holding siglock (or without atomic/RCU access annotations) violates the kernel concurrency model.