Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

clone: add CLONE_AUTOREAP

Add a new clone3() flag CLONE_AUTOREAP that makes a child process
auto-reap on exit without ever becoming a zombie. This is a per-process
property in contrast to the existing auto-reap mechanism via
SA_NOCLDWAIT or SIG_IGN for SIGCHLD which applies to all children of a
given parent.

Currently the only way to automatically reap children is to set
SA_NOCLDWAIT or SIG_IGN on SIGCHLD. This is a parent-scoped property
affecting all children which makes it unsuitable for libraries or
applications that need selective auto-reaping of specific children while
still being able to wait() on others.

CLONE_AUTOREAP stores an autoreap flag in the child's signal_struct.
When the child exits do_notify_parent() checks this flag and causes
exit_notify() to transition the task directly to EXIT_DEAD. Since the
flag lives on the child it survives reparenting: if the original parent
exits and the child is reparented to a subreaper or init the child still
auto-reaps when it eventually exits.

CLONE_AUTOREAP can be combined with CLONE_PIDFD to allow the parent to
monitor the child's exit via poll() and retrieve exit status via
PIDFD_GET_INFO. Without CLONE_PIDFD it provides a fire-and-forget
pattern where the parent simply doesn't care about the child's exit
status. No exit signal is delivered so exit_signal must be zero.

CLONE_AUTOREAP is rejected in combination with CLONE_PARENT. If a
CLONE_AUTOREAP child were to clone(CLONE_PARENT) the new grandchild
would inherit exit_signal == 0 from the autoreap parent's group leader
but without signal->autoreap. This grandchild would become a zombie that
never sends a signal and is never autoreaped - confusing and arguably
broken behavior.

The flag is not inherited by the autoreap process's own children. Each
child that should be autoreaped must be explicitly created with
CLONE_AUTOREAP.

Link: https://github.com/uapi-group/kernel-features/issues/45
Link: https://patch.msgid.link/20260226-work-pidfs-autoreap-v5-1-d148b984a989@kernel.org
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>

+26 -4
+1
include/linux/sched/signal.h
··· 132 132 */ 133 133 unsigned int is_child_subreaper:1; 134 134 unsigned int has_child_subreaper:1; 135 + unsigned int autoreap:1; 135 136 136 137 #ifdef CONFIG_POSIX_TIMERS 137 138
+3 -2
include/uapi/linux/sched.h
··· 34 34 #define CLONE_IO 0x80000000 /* Clone io context */ 35 35 36 36 /* Flags for the clone3() syscall. */ 37 - #define CLONE_CLEAR_SIGHAND 0x100000000ULL /* Clear any signal handler and reset to SIG_DFL. */ 38 - #define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */ 37 + #define CLONE_CLEAR_SIGHAND (1ULL << 32) /* Clear any signal handler and reset to SIG_DFL. */ 38 + #define CLONE_INTO_CGROUP (1ULL << 33) /* Clone into a specific cgroup given the right permissions. */ 39 + #define CLONE_AUTOREAP (1ULL << 34) /* Auto-reap child on exit. */ 39 40 40 41 /* 41 42 * cloning flags intersect with CSIGNAL so can be used with unshare and clone3
+16 -1
kernel/fork.c
··· 2028 2028 return ERR_PTR(-EINVAL); 2029 2029 } 2030 2030 2031 + if (clone_flags & CLONE_AUTOREAP) { 2032 + if (clone_flags & CLONE_THREAD) 2033 + return ERR_PTR(-EINVAL); 2034 + if (clone_flags & CLONE_PARENT) 2035 + return ERR_PTR(-EINVAL); 2036 + if (args->exit_signal) 2037 + return ERR_PTR(-EINVAL); 2038 + } 2039 + 2040 + if ((clone_flags & CLONE_PARENT) && current->signal->autoreap) 2041 + return ERR_PTR(-EINVAL); 2042 + 2031 2043 /* 2032 2044 * Force any signals received before this point to be delivered 2033 2045 * before the fork happens. Collect up signals sent to multiple ··· 2447 2435 */ 2448 2436 p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || 2449 2437 p->real_parent->signal->is_child_subreaper; 2438 + if (clone_flags & CLONE_AUTOREAP) 2439 + p->signal->autoreap = 1; 2450 2440 list_add_tail(&p->sibling, &p->real_parent->children); 2451 2441 list_add_tail_rcu(&p->tasks, &init_task.tasks); 2452 2442 attach_pid(p, PIDTYPE_TGID); ··· 2911 2897 { 2912 2898 /* Verify that no unknown flags are passed along. */ 2913 2899 if (kargs->flags & 2914 - ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP)) 2900 + ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP | 2901 + CLONE_AUTOREAP)) 2915 2902 return false; 2916 2903 2917 2904 /*
+2 -1
kernel/ptrace.c
··· 549 549 if (!dead && thread_group_empty(p)) { 550 550 if (!same_thread_group(p->real_parent, tracer)) 551 551 dead = do_notify_parent(p, p->exit_signal); 552 - else if (ignoring_children(tracer->sighand)) { 552 + else if (ignoring_children(tracer->sighand) || 553 + p->signal->autoreap) { 553 554 __wake_up_parent(p, tracer); 554 555 dead = true; 555 556 }
+4
kernel/signal.c
··· 2251 2251 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) 2252 2252 sig = 0; 2253 2253 } 2254 + if (!tsk->ptrace && tsk->signal->autoreap) { 2255 + autoreap = true; 2256 + sig = 0; 2257 + } 2254 2258 /* 2255 2259 * Send with __send_signal as si_pid and si_uid are in the 2256 2260 * parent's namespaces.