Re: [RFC PATCH v2 8/8] signal: move group_leader into signal_struct
From: Oleg Nesterov
Date: Sat Dec 05 2026 - 10:25:23 EST
Oh, I'll _try_ to read this series tomorrow... Just one question
for now.
On 12/05, Chris Mason wrote:
>
> release_task() makes a thread look like its own group leader to BPF
> programs.
>
> All threads of a process have the same leader, but each task_struct has its
> own pointer to it. The leader can be freed while somebody still holds the
> thread, so release_task() has to reset the thread's pointer.
>
> The fix used here is to share one pointer in signal_struct, as Oleg
> Nesterov suggested, and to point it at init_task when the leader's last
> reference is dropped. A task now keeps its signal_struct until it's freed.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This looks confusing, a task alredy keeps its signal_struct until it's freed?
> +void __put_task_struct(struct task_struct *tsk)
> {
> - struct task_struct *task = container_of(rhp, struct task_struct, rcu);
> + struct signal_struct *sig = tsk->signal;
> +
> + /*
> + * Somebody can still hold another thread of the group, and follow the
> + * pointer under RCU. So reset it a grace period before the leader is
> + * freed. It can't be NULL, as BPF programs don't check for that.
> + */
> + if (READ_ONCE(sig->group_leader) == tsk)
> + WRITE_ONCE(sig->group_leader, &init_task);
Yes, but perhaps we can avoid this? I am not sure, but I was thinking
about _something_ like below.
If this can work then bacct_add_tsk() can just do
scoped_guard (rcu)
delta = now_ns - task_group_leader(tsk)->start_time;
without pid_alive() check.
Oleg.
--- x/kernel/fork.c
+++ x/kernel/fork.c
@@ -773,10 +773,15 @@ static inline void free_signal_struct(st
kmem_cache_free(signal_cachep, sig);
}
-static inline void put_signal_struct(struct signal_struct *sig)
+static struct task_struct *put_signal_struct(struct signal_struct *sig)
{
- if (refcount_dec_and_test(&sig->sigcnt))
+ struct task_struct *leader = NULL;
+ if (refcount_dec_and_test(&sig->sigcnt)) {
+ leader = sig->group_leader;
free_signal_struct(sig);
+ free_task(leader);
+ }
+ return leader;
}
void __put_task_struct(struct task_struct *tsk)
@@ -792,9 +797,10 @@ void __put_task_struct(struct task_struc
security_task_free(tsk);
exit_creds(tsk);
delayacct_tsk_free(tsk);
- put_signal_struct(tsk->signal);
sched_core_free(tsk);
- free_task(tsk);
+ leader = put_signal_struct(tsk->signal);
+ if (tsk != leader);
+ free_task(tsk);
}
EXPORT_SYMBOL_GPL(__put_task_struct);