Re: [PATCH 1/3] fork: make the creator the parent of an unlinked task
From: Chris Mason
Date: Fri Dec 04 2026 - 11:28:21 EST
On Fri, 4 Dec 2026 16:27:58 +0100, Oleg Nesterov wrote:
> I know nothing about bpf, could you add more details please ?
>
> OK, B, child of A, does copy_process().
>
> A exits right after dup_task_struct(). p->real_parent points to nowhere.
>
> Who exactly can dererence p->real_parent at this point?
BPF programs...security_task_alloc() and sched_cgroup_fork() call them with
p before it's linked, and they're allowed to read p->real_parent->pid, for
example. The scx_lavd scheduler does this now.
> With the change in dup_task_struct(), why is it correct to pretend that
> p->real_parent == B ?
It's only correct for a plain fork, where B will be the parent anyway? For a
new thread, the parent will be A, and copy_process() sets that at the end.
CLONE_PARENT is the same.
Until then, bpf programs need p->real_parent pointing to a task that can't
be freed. A can exit at any time, but B can't, because it's busy with the fork.
The downside is a window where a bpf program sees B as the parent of a
new thread.
(I don't love any of these fixes, just explaining the crash itself)
[ ... ]
> OK, this change suggests that we have another problem: copy_process() with
> CLONE_PARENT or CLONE_THREAD can fail and drop tasklist_lock after it sets
> p->real_parent = A. A can exit.
>
> Who can derefence ->real_parent of the soon-to-be-freed and not-yet-linked
> task p?
sched_cancel_fork() calls sched_ext's ops.exit_task() after tasklist_lock is
dropped. This patch missed that one, thanks for flagging.
> I am just trying to understand the problem. Perhaps we should change
> dup_task_struct() to nullify p->thread_pid to make pid_alive(p) false?
It looks like that isn't enough on its own. BPF programs follow ->real_parent
without checking pid_alive().
-chris