Re: [PATCH 2/3] exit: don't leave stale parent and leader pointers
From: bot+bpf-ci
Date: Thu Dec 03 2026 - 15:55:28 EST
> exit: don't leave stale parent and leader pointers
TL;DR: Released threads now pin their group leader, which may create a
reference cycle with BPF task-storage kptrs, leaking both task_structs until
the map is destroyed. The commit message omits this lifetime change.
> diff --git a/kernel/exit.c b/kernel/exit.c
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
[ ... ]
> @@ -262,6 +263,17 @@ void release_task(struct task_struct *p)
> */
> thread_pid = get_pid(task_pid(p));
>
> + /*
> + * A released thread pins its leader, which lockless next_thread()
> + * needs. Until @p is released its leader can neither change nor go
> + * away. notify_count < 0 means mt-exec: @p is the old leader, and
> + * must not pin the new one, or repeated execs could build a chain.
> + */
> + leader = p->group_leader;
> + pin_leader = leader != p && READ_ONCE(p->signal->notify_count) >= 0;
> + if (pin_leader)
> + get_task_struct(leader);
> +
Can this pin create a reference cycle between a thread and its leader?
Previously the reference to the leader L held on behalf of a released
thread T was not counted, so L's task_struct was freed once L itself was
reaped. Now T holds a usage reference on L until T's own
__put_task_struct():
kernel/fork.c:__put_task_struct()
/* release_task() has pinned the leader of a thread */
if (tsk->group_leader != tsk)
put_task_struct(tsk->group_leader);
Anything whose release of T is tied to L's final free now forms a cycle.
BPF task-local storage looks like one such case, since
bpf_task_storage_free() is only called from free_task(), and
map_check_btf() allows BPF_KPTR_REF fields in BPF_MAP_TYPE_TASK_STORAGE
values. A program that stores a referenced kptr to thread T in L's task
storage:
v = bpf_task_storage_get(&map, L, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE);
old = bpf_kptr_xchg(&v->t, bpf_task_from_pid(tid_of_T));
used to be fine, because L's free released the storage and dropped T. With
this patch:
T exits -> exit_notify() -> release_task(T) -> get_task_struct(L)
L exits and is reaped -> put_task_struct_rcu_user(L) -> delayed_put_task_struct(L)
-> put_task_struct(L): usage stays >= 1 (T's pin), L is not freed
free_task(L) never runs, so L's storage and its kptr to T are never released
T's usage stays >= 1, so __put_task_struct(T) never drops the pin on L
Both task_structs then stay allocated until the map itself is destroyed,
along with what __put_task_struct() would have released: creds,
signal_struct, css_set and LSM blobs. A cleanup hook on lsm/task_free for L
also never runs. A long-running BPF LSM or sched_ext program could leak
this way once per process.
The commit message also does not mention this new lifetime dependency:
holding any thread's task_struct now keeps its leader's task_struct alive.
Is that intended?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47195022171