Re: [RFC PATCH v6 15/26] exit: Support P_USERLAND in waitid()
From: bot+bpf-ci
Date: Sat Nov 21 2026 - 21:13:31 EST
> exit: Support P_USERLAND in waitid()
TL;DR: A blocking waitid(P_USERLAND) may return an unexpected -EAGAIN if a
failed clone3() bootstrap is retried while it sleeps; it should probably
return -ECHILD or keep waiting.
> diff --git a/kernel/userland.c b/kernel/userland.c
> --- a/kernel/userland.c
> +++ b/kernel/userland.c
[ ... ]
> +/**
> + * DOC: P_USERLAND
> + *
> + * ``waitid(P_USERLAND, userland_fd, &infop, options, NULL)`` waits freezably until
> + * ``userland_fd`` reaches ``USERLAND_DEAD`` (unless ``WNOHANG`` or ``O_NONBLOCK`` is
> + * set) and writes init's exit information into ``infop`` without consuming it.
[ ... ]
> + if (!nonblock) {
> + ret = wait_event_freezable(userland->waitq,
> + READ_ONCE(userland->state) ==
> + USERLAND_DEAD ||
> + READ_ONCE(userland->state) ==
> + USERLAND_INIT);
> + if (ret)
> + return ret;
> + }
> +
> + scoped_guard(spinlock, &userland->lock) {
> + if (userland->state == USERLAND_INIT)
> + return -ECHILD;
> + if (userland->state != USERLAND_DEAD)
> + return (options & WNOHANG) ? 0 : -EAGAIN;
Can a blocking waitid(P_USERLAND) return -EAGAIN here, even though the DOC
comment says it waits until USERLAND_DEAD unless WNOHANG or O_NONBLOCK is
set?
The wait above ends on DEAD or INIT, but userland->lock is only taken after
the wait returns, so the state can change in between. INIT is not terminal
once 498ccb2ffbc4 ("fork: Support CLONE_INTO_USERLAND in clone3()") is
applied: userland_finish_fork() sets FORKING back to INIT under
userland->lock when copy_process() fails, and then calls
wake_up_all(&userland->waitq).
Sequence:
Task A: waitid(P_USERLAND, fd, &si, WEXITED, NULL), blocking fd
passes the early INIT check and sleeps in wait_event_freezable()
Task B: clone3(CLONE_INTO_USERLAND), state is FORKING
copy_process() fails (e.g. -EAGAIN from RLIMIT_NPROC)
userland_finish_fork() sets INIT and wakes A
Task B or C: retries clone3(CLONE_INTO_USERLAND)
userland_prepare_fork() sees INIT and sets FORKING again
Task A: takes userland->lock, state is FORKING or RUNNING,
neither INIT nor DEAD, WNOHANG is clear, returns -EAGAIN
With neither WNOHANG nor O_NONBLOCK set, waitid() does not return -EAGAIN
for P_PID or P_PIDFD, so a manager doing a blocking wait gets an error it has
no reason to expect, possibly while the new init is running.
In the !nonblock case a non-DEAD state under the lock means a bootstrap
failed, so should this return -ECHILD like the early INIT check, or go back
to waiting? -EAGAIN and 0 would then only apply to the nonblock case.
userland_waitid() is unchanged through the end of the series, so no later
commit fixes this. The path is only reachable once 498ccb2ffbc4 adds the
FORKING state; in this commit the state never leaves INIT.
---
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/44972669664