From nobody Fri Apr 3 04:39:51 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 778A93115B0; Tue, 17 Feb 2026 22:36:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771367794; cv=none; b=dtft8O2zRae/FXOMhqXIskkvD6BgTKKweQ6kRkP2znJ8/waNjVKjjvIcH/E5fqiIPf+vcSJ7BE6YKpeRgqVV/rXJTSZ1WKg4pdVShaFJog99Gjzk5OggzbksCl0kOgnFEHPKzOUgWAikOqxumky4AS0FTbsTMfwjdO7MsMODpGw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771367794; c=relaxed/simple; bh=E0lhMzmnJrixfNinuGEW4FL+ZM370Fzk/3eehHyVNkY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=meVlqUl4mDDRnvs4aUSxmAwQIs1XSQLL5w65w1xslr0EGQP/IlvIhZjEwJRx84iTcLSCVFQs9DqZTSvIN1ak08EwFIA7Xt/4kIsWnUpqen/AwFUEbwmfg7VUuWwjMh6tizCnF7m5SefYYAlFdVZiZE1tw4rWf2L3c8/Jl3OjMZc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pmXkeQx/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pmXkeQx/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A12D7C19421; Tue, 17 Feb 2026 22:36:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771367794; bh=E0lhMzmnJrixfNinuGEW4FL+ZM370Fzk/3eehHyVNkY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pmXkeQx/y2nbZr20gyNU1AghbzhIXSM9eoi6P39eeBbgMGVXsl22SjXzDY2fG3gX4 AMTd38drLgsP4wXBxMVNwxml8gsjH2cYgPPHNubgsc77+fe0FCmMeD74stNCZu92oG h7fjp4MXbIzW5tVbnp0uFVcjfDQFRM37dVGA2GcLOlozVv2DQg/RAySU8WWAMEPRfG UOZIiH52DQOtuWV7jzPmWW6/zRevURTXyNGM2rFjMpLo260QRYqctRjRStB1VjYbB/ /bGiaZo0ZeFbJqjx/Sy7R+fKc1bidy0/LpT1LUfOLFPLezeXU4FXZPDXqVIfgnIMer MyMeAL5SmAChQ== From: Christian Brauner Date: Tue, 17 Feb 2026 23:35:50 +0100 Subject: [PATCH RFC v3 1/4] clone: add CLONE_AUTOREAP Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260217-work-pidfs-autoreap-v3-1-33a403c20111@kernel.org> References: <20260217-work-pidfs-autoreap-v3-0-33a403c20111@kernel.org> In-Reply-To: <20260217-work-pidfs-autoreap-v3-0-33a403c20111@kernel.org> To: Oleg Nesterov , Jann Horn Cc: Linus Torvalds , Ingo Molnar , Peter Zijlstra , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Christian Brauner X-Mailer: b4 0.15-dev-47773 X-Developer-Signature: v=1; a=openpgp-sha256; l=5637; i=brauner@kernel.org; h=from:subject:message-id; bh=E0lhMzmnJrixfNinuGEW4FL+ZM370Fzk/3eehHyVNkY=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWROeZv7/Tv/G5+jza/6Pm9k/3OhZSOP0okjq1yuThUSX 8K3gn2+R0cpC4MYF4OsmCKLQ7tJuNxynorNRpkaMHNYmUCGMHBxCsBEMioY/udIlUn9mnL6+FFV m5M/ba6+f908eUbNiT9l1z58jQzd3rmf4a/AG6VpV9brObB/U221WO2z8NaXD9ynDj4XNy5bmCB 1aykLAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 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 returns autoreap=3Dtrue causing 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. 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 Signed-off-by: Christian Brauner --- include/linux/sched/signal.h | 1 + include/uapi/linux/sched.h | 1 + kernel/fork.c | 14 +++++++++++++- kernel/ptrace.c | 3 ++- kernel/signal.c | 4 ++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index a22248aebcf9..f842c86b806f 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -132,6 +132,7 @@ struct signal_struct { */ unsigned int is_child_subreaper:1; unsigned int has_child_subreaper:1; + unsigned int autoreap:1; =20 #ifdef CONFIG_POSIX_TIMERS =20 diff --git a/include/uapi/linux/sched.h b/include/uapi/linux/sched.h index 359a14cc76a4..8a22ea640817 100644 --- a/include/uapi/linux/sched.h +++ b/include/uapi/linux/sched.h @@ -36,6 +36,7 @@ /* Flags for the clone3() syscall. */ #define CLONE_CLEAR_SIGHAND 0x100000000ULL /* Clear any signal handler and= reset to SIG_DFL. */ #define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup g= iven the right permissions. */ +#define CLONE_AUTOREAP 0x400000000ULL /* Auto-reap child on exit. */ =20 /* * cloning flags intersect with CSIGNAL so can be used with unshare and cl= one3 diff --git a/kernel/fork.c b/kernel/fork.c index e832da9d15a4..bc27dc10c309 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2028,6 +2028,13 @@ __latent_entropy struct task_struct *copy_process( return ERR_PTR(-EINVAL); } =20 + if (clone_flags & CLONE_AUTOREAP) { + if (clone_flags & CLONE_THREAD) + return ERR_PTR(-EINVAL); + if (args->exit_signal) + return ERR_PTR(-EINVAL); + } + /* * Force any signals received before this point to be delivered * before the fork happens. Collect up signals sent to multiple @@ -2374,6 +2381,8 @@ __latent_entropy struct task_struct *copy_process( p->parent_exec_id =3D current->parent_exec_id; if (clone_flags & CLONE_THREAD) p->exit_signal =3D -1; + else if (clone_flags & CLONE_AUTOREAP) + p->exit_signal =3D 0; else p->exit_signal =3D current->group_leader->exit_signal; } else { @@ -2435,6 +2444,8 @@ __latent_entropy struct task_struct *copy_process( */ p->signal->has_child_subreaper =3D p->real_parent->signal->has_child_su= breaper || p->real_parent->signal->is_child_subreaper; + if (clone_flags & CLONE_AUTOREAP) + p->signal->autoreap =3D 1; list_add_tail(&p->sibling, &p->real_parent->children); list_add_tail_rcu(&p->tasks, &init_task.tasks); attach_pid(p, PIDTYPE_TGID); @@ -2897,7 +2908,8 @@ static bool clone3_args_valid(struct kernel_clone_arg= s *kargs) { /* Verify that no unknown flags are passed along. */ if (kargs->flags & - ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP)) + ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP | + CLONE_AUTOREAP)) return false; =20 /* diff --git a/kernel/ptrace.c b/kernel/ptrace.c index 392ec2f75f01..68c17daef8d4 100644 --- a/kernel/ptrace.c +++ b/kernel/ptrace.c @@ -549,7 +549,8 @@ static bool __ptrace_detach(struct task_struct *tracer,= struct task_struct *p) if (!dead && thread_group_empty(p)) { if (!same_thread_group(p->real_parent, tracer)) dead =3D do_notify_parent(p, p->exit_signal); - else if (ignoring_children(tracer->sighand)) { + else if (ignoring_children(tracer->sighand) || + p->signal->autoreap) { __wake_up_parent(p, tracer); dead =3D true; } diff --git a/kernel/signal.c b/kernel/signal.c index d65d0fe24bfb..e61f39fa8c8a 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -2251,6 +2251,10 @@ bool do_notify_parent(struct task_struct *tsk, int s= ig) if (psig->action[SIGCHLD-1].sa.sa_handler =3D=3D SIG_IGN) sig =3D 0; } + if (!tsk->ptrace && tsk->signal->autoreap) { + autoreap =3D true; + sig =3D 0; + } /* * Send with __send_signal as si_pid and si_uid are in the * parent's namespaces. --=20 2.47.3 From nobody Fri Apr 3 04:39:51 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6BF72D5C6C; Tue, 17 Feb 2026 22:36:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771367796; cv=none; b=RwbgsW95lIJRageYtBlMsVb5clbw72rH+/CIYnwiaEwcb8+F7bD1jDftlE3GK1k9rFSuuPve2IiJR5rAg/nm57CpiL1OEQQ+uSSjl5pqlLLMmJQJsZw0v9JARzZ4N/G5TOC55JmLhT23pF9aZWbj6gVvh1Joi7y2QdED9wyjsb8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771367796; c=relaxed/simple; bh=GGzmN2/fE67M1JU7gWLPFT4Zm6OWe251kFDBMS3MTEo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TEoeiELT8uD3TWRQ3/uVKcth/g1bXXNL2R+9bEUN78u6KER5UAeXyoAgHxhDPVren/+ZODuTgJNQiFgiTf9R4l4CnFC+yEKHRLhoGNIOZjm3haWW3VnvXlKA95MQM2sC0Qaz6+7YTP14R+t2x/7wiGHxSRdKvNWH7bFoVdVpkQA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OQbptT86; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OQbptT86" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C295AC2BC87; Tue, 17 Feb 2026 22:36:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771367796; bh=GGzmN2/fE67M1JU7gWLPFT4Zm6OWe251kFDBMS3MTEo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=OQbptT86YN5+nOMS/mN8pxSEaY3GoQmPY8M0zSx33QT65Tc2GDRUENEI3JL8bind3 /DvXAbAAYceVRFE2i7J3+hfU4el86WxI5a6MfqomCoZvW5gVpw+uye0xyvr6FRh2J+ s8O/mQWvRVeMhgLQ1pt3Cfwmas3/61gGYJ2GNwm/UKgGAVjsECzar/PbxGQ90mpDdB nTKNj5p9mb2/6JU+vGFx4Wzx9v+Vx7b39QWOuJOwTDINI6lbtL2EtuXoDvNKa2ewIs wuvOFWUKcrO3zGDZoSsZ97Zta7gpHYdhrbzMixMda5Jx0iaP11G77iuLnQsVMzOODP pZIeHybCGBBFA== From: Christian Brauner Date: Tue, 17 Feb 2026 23:35:51 +0100 Subject: [PATCH RFC v3 2/4] pidfd: add CLONE_PIDFD_AUTOKILL Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260217-work-pidfs-autoreap-v3-2-33a403c20111@kernel.org> References: <20260217-work-pidfs-autoreap-v3-0-33a403c20111@kernel.org> In-Reply-To: <20260217-work-pidfs-autoreap-v3-0-33a403c20111@kernel.org> To: Oleg Nesterov , Jann Horn Cc: Linus Torvalds , Ingo Molnar , Peter Zijlstra , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Christian Brauner X-Mailer: b4 0.15-dev-47773 X-Developer-Signature: v=1; a=openpgp-sha256; l=5319; i=brauner@kernel.org; h=from:subject:message-id; bh=GGzmN2/fE67M1JU7gWLPFT4Zm6OWe251kFDBMS3MTEo=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWROeZsn8qHb8NkMAXFx5zXGhb90mHLNl+f+lwxpULi0M mI3r5FGRykLgxgXg6yYIotDu0m43HKeis1GmRowc1iZQIYwcHEKwEQOBjAy9Nlee1Pdwu+UZjF5 zSPlg1PrbeKENppdzhG/Li2+ZV7IJIa/Mkm/ZbUrZtxknOd3MKRrpuLRGyYFVy7M69BmzT/W3qH PAgA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add a new clone3() flag CLONE_PIDFD_AUTOKILL that ties a child's lifetime to the pidfd returned from clone3(). When the last reference to the struct file created by clone3() is closed the kernel sends SIGKILL to the child. A pidfd obtained via pidfd_open() for the same process does not keep the child alive and does not trigger autokill - only the specific struct file from clone3() has this property. This is useful for container runtimes, service managers, and sandboxed subprocess execution - any scenario where the child must die if the parent crashes or abandons the pidfd. CLONE_PIDFD_AUTOKILL requires both CLONE_PIDFD (the whole point is tying lifetime to the pidfd file) and CLONE_AUTOREAP (a killed child with no one to reap it would become a zombie). CLONE_THREAD is rejected because autokill targets a process not a thread. The clone3 pidfd is identified by storing a pointer to the struct file in signal_struct.autokill_pidfd. The pidfs .release handler compares the file being closed against this pointer and sends SIGKILL via group_send_sig_info(SIGKILL, SEND_SIG_PRIV, ...) only on match. Files from pidfd_open() or open_by_handle_at() are distinct struct files and will never match. dup()/fork() share the same struct file so they extend the child's lifetime until the last reference drops. Unlike pdeath_signal autokill isn't disarmed on exec and on credential changes that cross privilege boundaries. It would defeat the purpose of this whole endeavour. Signed-off-by: Christian Brauner --- fs/pidfs.c | 16 ++++++++++++++++ include/linux/sched/signal.h | 3 +++ include/uapi/linux/sched.h | 1 + kernel/fork.c | 16 ++++++++++++++-- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/fs/pidfs.c b/fs/pidfs.c index 318253344b5c..b3891b2097eb 100644 --- a/fs/pidfs.c +++ b/fs/pidfs.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include #include @@ -637,7 +639,21 @@ static long pidfd_ioctl(struct file *file, unsigned in= t cmd, unsigned long arg) return open_namespace(ns_common); } =20 +static int pidfs_file_release(struct inode *inode, struct file *file) +{ + struct pid *pid =3D inode->i_private; + struct task_struct *task; + + guard(rcu)(); + task =3D pid_task(pid, PIDTYPE_TGID); + if (task && READ_ONCE(task->signal->autokill_pidfd) =3D=3D file) + do_send_sig_info(SIGKILL, SEND_SIG_PRIV, task, PIDTYPE_TGID); + + return 0; +} + static const struct file_operations pidfs_file_operations =3D { + .release =3D pidfs_file_release, .poll =3D pidfd_poll, #ifdef CONFIG_PROC_FS .show_fdinfo =3D pidfd_show_fdinfo, diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index f842c86b806f..85a3de5c4030 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -134,6 +134,9 @@ struct signal_struct { unsigned int has_child_subreaper:1; unsigned int autoreap:1; =20 + /* pidfd that triggers SIGKILL on close, or NULL */ + const struct file *autokill_pidfd; + #ifdef CONFIG_POSIX_TIMERS =20 /* POSIX.1b Interval Timers */ diff --git a/include/uapi/linux/sched.h b/include/uapi/linux/sched.h index 8a22ea640817..b1aea8a86e2f 100644 --- a/include/uapi/linux/sched.h +++ b/include/uapi/linux/sched.h @@ -37,6 +37,7 @@ #define CLONE_CLEAR_SIGHAND 0x100000000ULL /* Clear any signal handler and= reset to SIG_DFL. */ #define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup g= iven the right permissions. */ #define CLONE_AUTOREAP 0x400000000ULL /* Auto-reap child on exit. */ +#define CLONE_PIDFD_AUTOKILL 0x800000000ULL /* Kill child when clone pidfd= closes. */ =20 /* * cloning flags intersect with CSIGNAL so can be used with unshare and cl= one3 diff --git a/kernel/fork.c b/kernel/fork.c index bc27dc10c309..7bcdba54c9a0 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2035,6 +2035,15 @@ __latent_entropy struct task_struct *copy_process( return ERR_PTR(-EINVAL); } =20 + if (clone_flags & CLONE_PIDFD_AUTOKILL) { + if (!(clone_flags & CLONE_PIDFD)) + return ERR_PTR(-EINVAL); + if (!(clone_flags & CLONE_AUTOREAP)) + return ERR_PTR(-EINVAL); + if (clone_flags & CLONE_THREAD) + return ERR_PTR(-EINVAL); + } + /* * Force any signals received before this point to be delivered * before the fork happens. Collect up signals sent to multiple @@ -2470,8 +2479,11 @@ __latent_entropy struct task_struct *copy_process( syscall_tracepoint_update(p); write_unlock_irq(&tasklist_lock); =20 - if (pidfile) + if (pidfile) { + if (clone_flags & CLONE_PIDFD_AUTOKILL) + p->signal->autokill_pidfd =3D pidfile; fd_install(pidfd, pidfile); + } =20 proc_fork_connector(p); sched_post_fork(p); @@ -2909,7 +2921,7 @@ static bool clone3_args_valid(struct kernel_clone_arg= s *kargs) /* Verify that no unknown flags are passed along. */ if (kargs->flags & ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP | - CLONE_AUTOREAP)) + CLONE_AUTOREAP | CLONE_PIDFD_AUTOKILL)) return false; =20 /* --=20 2.47.3 From nobody Fri Apr 3 04:39:51 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41BAD31619A; Tue, 17 Feb 2026 22:36:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771367799; cv=none; b=aXtME1us7/5ezr81jVl72XimD/fmW0WKWuTGp+9jAPajPIF8VqQOgbIgUphmElO4d/kypFwEiUTGd8LnHsInvUa7ujCqQZz94lTNhRRbs7ZHPhJ44bUuZsnlHNaaK9FqY9fsJonMQ1McElVN5PvsKkkbbI5L1CztVduc2X1S1KM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771367799; c=relaxed/simple; bh=tAfgRWlfKy1rptEL1tx4djYiqEm5BYBS5cQoE3FtW/o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=T8ZhH6sBbiuvERZldKlW4c+Z8uvOcBffUHMU41xtHtWKnw/bFynYJppJ/P02SQ/Tb+5ib1O3tX13W3NBA3J6SnZAJSJw4aHNmMtha/NMhJKlyTAHgImrH26d2hp1ZGuF5bS/Rvvj2RPhYtLC+b2i7vewjgdE0lQb38JV4QMkP90= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OEyzhOkE; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OEyzhOkE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E6355C19423; Tue, 17 Feb 2026 22:36:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771367798; bh=tAfgRWlfKy1rptEL1tx4djYiqEm5BYBS5cQoE3FtW/o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=OEyzhOkENZeYsOGTimBWk0yP5MaAQxEF4jq8Lym0aRFSuU3aOzAiAphYCbetgizkR 5z5ZkabMwQKvbmTjWoic0Wb5k6GTEUfMVTGOuOG57gU5SVQfwncLrAZ1PNeVdeSRxY q4Wj/vps+DC3qrZv2VylvYrxZZmwJ/S5mNGGPL7nmoaaSjpxUsV9UvVD6TUQe5yG2r iqwTmqTRMNfNTy4C5bVOPP2U5joE2pBCpxBhWw5r9aBU2jskbTAGbRay9cj91fibBD SNb8aONqt9ZrzNtbth2ZdzDs6dW69StGuUjKVhpghF8Ao2xIiG4TZT8ywvFlE4m7s7 xQ7TadLlj5OBg== From: Christian Brauner Date: Tue, 17 Feb 2026 23:35:52 +0100 Subject: [PATCH RFC v3 3/4] selftests/pidfd: add CLONE_AUTOREAP tests Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260217-work-pidfs-autoreap-v3-3-33a403c20111@kernel.org> References: <20260217-work-pidfs-autoreap-v3-0-33a403c20111@kernel.org> In-Reply-To: <20260217-work-pidfs-autoreap-v3-0-33a403c20111@kernel.org> To: Oleg Nesterov , Jann Horn Cc: Linus Torvalds , Ingo Molnar , Peter Zijlstra , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Christian Brauner X-Mailer: b4 0.15-dev-47773 X-Developer-Signature: v=1; a=openpgp-sha256; l=14185; i=brauner@kernel.org; h=from:subject:message-id; bh=tAfgRWlfKy1rptEL1tx4djYiqEm5BYBS5cQoE3FtW/o=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWROeZtXteAJa/5vIVNxvzSV4kUXN7TusgrSurPBfnP0y fJ9XFI7O0pZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACaiKcrIcDLNr21jvuB0XlkV yYV9DHIb1Yz0987Ima7687C95h+NLob/FTNMuw3UFs+3OTd/jeIDrwT7mT8jrt1QzuNrKFxyctM GDgA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add tests for the new CLONE_AUTOREAP clone3() flag: - autoreap_requires_pidfd: CLONE_AUTOREAP without CLONE_PIDFD fails - autoreap_rejects_exit_signal: CLONE_AUTOREAP with non-zero exit_signal fails - autoreap_rejects_thread: CLONE_AUTOREAP with CLONE_THREAD fails - autoreap_basic: child exits, pidfd poll works, PIDFD_GET_INFO returns correct exit code, waitpid() returns -ECHILD - autoreap_signaled: child killed by signal, exit info correct via pidfd - autoreap_reparent: autoreap grandchild reparented to subreaper still auto-reaps - autoreap_multithreaded: autoreap process with sub-threads auto-reaps after last thread exits - autoreap_no_inherit: grandchild forked without CLONE_AUTOREAP becomes a regular zombie Signed-off-by: Christian Brauner --- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 2 +- .../testing/selftests/pidfd/pidfd_autoreap_test.c | 489 +++++++++++++++++= ++++ 3 files changed, 491 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/pidfd/.gitignore b/tools/testing/selft= ests/pidfd/.gitignore index 144e7ff65d6a..4cd8ec7fd349 100644 --- a/tools/testing/selftests/pidfd/.gitignore +++ b/tools/testing/selftests/pidfd/.gitignore @@ -12,3 +12,4 @@ pidfd_info_test pidfd_exec_helper pidfd_xattr_test pidfd_setattr_test +pidfd_autoreap_test diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftes= ts/pidfd/Makefile index 764a8f9ecefa..4211f91e9af8 100644 --- a/tools/testing/selftests/pidfd/Makefile +++ b/tools/testing/selftests/pidfd/Makefile @@ -4,7 +4,7 @@ CFLAGS +=3D -g $(KHDR_INCLUDES) $(TOOLS_INCLUDES) -pthread = -Wall TEST_GEN_PROGS :=3D pidfd_test pidfd_fdinfo_test pidfd_open_test \ pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \ pidfd_file_handle_test pidfd_bind_mount pidfd_info_test \ - pidfd_xattr_test pidfd_setattr_test + pidfd_xattr_test pidfd_setattr_test pidfd_autoreap_test =20 TEST_GEN_PROGS_EXTENDED :=3D pidfd_exec_helper =20 diff --git a/tools/testing/selftests/pidfd/pidfd_autoreap_test.c b/tools/te= sting/selftests/pidfd/pidfd_autoreap_test.c new file mode 100644 index 000000000000..3c0c45359473 --- /dev/null +++ b/tools/testing/selftests/pidfd/pidfd_autoreap_test.c @@ -0,0 +1,489 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2026 Christian Brauner + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pidfd.h" +#include "kselftest_harness.h" + +#ifndef CLONE_AUTOREAP +#define CLONE_AUTOREAP 0x400000000ULL +#endif + +static pid_t create_autoreap_child(int *pidfd) +{ + struct __clone_args args =3D { + .flags =3D CLONE_PIDFD | CLONE_AUTOREAP, + .exit_signal =3D 0, + .pidfd =3D ptr_to_u64(pidfd), + }; + + return sys_clone3(&args, sizeof(args)); +} + +/* + * Test that CLONE_AUTOREAP works without CLONE_PIDFD (fire-and-forget). + */ +TEST(autoreap_without_pidfd) +{ + struct __clone_args args =3D { + .flags =3D CLONE_AUTOREAP, + .exit_signal =3D 0, + }; + pid_t pid; + int ret; + + pid =3D sys_clone3(&args, sizeof(args)); + if (pid < 0 && errno =3D=3D EINVAL) + SKIP(return, "CLONE_AUTOREAP not supported"); + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) + _exit(0); + + /* + * Give the child a moment to exit and be autoreaped. + * Then verify no zombie remains. + */ + usleep(200000); + ret =3D waitpid(pid, NULL, WNOHANG); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, ECHILD); +} + +/* + * Test that CLONE_AUTOREAP with a non-zero exit_signal fails. + */ +TEST(autoreap_rejects_exit_signal) +{ + struct __clone_args args =3D { + .flags =3D CLONE_PIDFD | CLONE_AUTOREAP, + .exit_signal =3D SIGCHLD, + }; + int pidfd =3D -1; + pid_t pid; + + args.pidfd =3D ptr_to_u64(&pidfd); + + pid =3D sys_clone3(&args, sizeof(args)); + ASSERT_EQ(pid, -1); + ASSERT_EQ(errno, EINVAL); +} + +/* + * Test that CLONE_AUTOREAP with CLONE_THREAD fails. + */ +TEST(autoreap_rejects_thread) +{ + struct __clone_args args =3D { + .flags =3D CLONE_PIDFD | CLONE_AUTOREAP | + CLONE_THREAD | CLONE_SIGHAND | + CLONE_VM, + .exit_signal =3D 0, + }; + int pidfd =3D -1; + pid_t pid; + + args.pidfd =3D ptr_to_u64(&pidfd); + + pid =3D sys_clone3(&args, sizeof(args)); + ASSERT_EQ(pid, -1); + ASSERT_EQ(errno, EINVAL); +} + +/* + * Basic test: create an autoreap child, let it exit, verify: + * - pidfd becomes readable (poll returns POLLIN) + * - PIDFD_GET_INFO returns the correct exit code + * - waitpid() returns -1/ECHILD (no zombie) + */ +TEST(autoreap_basic) +{ + struct pidfd_info info =3D { .mask =3D PIDFD_INFO_EXIT }; + int pidfd =3D -1, ret; + struct pollfd pfd; + pid_t pid; + + pid =3D create_autoreap_child(&pidfd); + if (pid < 0 && errno =3D=3D EINVAL) + SKIP(return, "CLONE_AUTOREAP not supported"); + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) + _exit(42); + + ASSERT_GE(pidfd, 0); + + /* Wait for the child to exit via pidfd poll. */ + pfd.fd =3D pidfd; + pfd.events =3D POLLIN; + ret =3D poll(&pfd, 1, 5000); + ASSERT_EQ(ret, 1); + ASSERT_TRUE(pfd.revents & POLLIN); + + /* Verify exit info via PIDFD_GET_INFO. */ + ret =3D ioctl(pidfd, PIDFD_GET_INFO, &info); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(info.mask & PIDFD_INFO_EXIT); + /* + * exit_code is in waitpid format: for _exit(42), + * WIFEXITED is true and WEXITSTATUS is 42. + */ + ASSERT_TRUE(WIFEXITED(info.exit_code)); + ASSERT_EQ(WEXITSTATUS(info.exit_code), 42); + + /* Verify no zombie: waitpid should fail with ECHILD. */ + ret =3D waitpid(pid, NULL, WNOHANG); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, ECHILD); + + close(pidfd); +} + +/* + * Test that an autoreap child killed by a signal reports + * the correct exit info. + */ +TEST(autoreap_signaled) +{ + struct pidfd_info info =3D { .mask =3D PIDFD_INFO_EXIT }; + int pidfd =3D -1, ret; + struct pollfd pfd; + pid_t pid; + + pid =3D create_autoreap_child(&pidfd); + if (pid < 0 && errno =3D=3D EINVAL) + SKIP(return, "CLONE_AUTOREAP not supported"); + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) { + pause(); + _exit(1); + } + + ASSERT_GE(pidfd, 0); + + /* Kill the child. */ + ret =3D sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0); + ASSERT_EQ(ret, 0); + + /* Wait for exit via pidfd. */ + pfd.fd =3D pidfd; + pfd.events =3D POLLIN; + ret =3D poll(&pfd, 1, 5000); + ASSERT_EQ(ret, 1); + ASSERT_TRUE(pfd.revents & POLLIN); + + /* Verify signal info. */ + ret =3D ioctl(pidfd, PIDFD_GET_INFO, &info); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(info.mask & PIDFD_INFO_EXIT); + ASSERT_TRUE(WIFSIGNALED(info.exit_code)); + ASSERT_EQ(WTERMSIG(info.exit_code), SIGKILL); + + /* No zombie. */ + ret =3D waitpid(pid, NULL, WNOHANG); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, ECHILD); + + close(pidfd); +} + +/* + * Test autoreap survives reparenting: middle process creates an + * autoreap grandchild, then exits. The grandchild gets reparented + * to us (the grandparent, which is a subreaper). When the grandchild + * exits, it should still be autoreaped - no zombie under us. + */ +TEST(autoreap_reparent) +{ + int ipc_sockets[2], ret; + int pidfd =3D -1; + struct pollfd pfd; + pid_t mid_pid, grandchild_pid; + char buf[32] =3D {}; + + /* Make ourselves a subreaper so reparented children come to us. */ + ret =3D prctl(PR_SET_CHILD_SUBREAPER, 1); + ASSERT_EQ(ret, 0); + + ret =3D socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets); + ASSERT_EQ(ret, 0); + + mid_pid =3D fork(); + ASSERT_GE(mid_pid, 0); + + if (mid_pid =3D=3D 0) { + /* Middle child: create an autoreap grandchild. */ + int gc_pidfd =3D -1; + + close(ipc_sockets[0]); + + grandchild_pid =3D create_autoreap_child(&gc_pidfd); + if (grandchild_pid < 0) { + write_nointr(ipc_sockets[1], "E", 1); + close(ipc_sockets[1]); + _exit(1); + } + + if (grandchild_pid =3D=3D 0) { + /* Grandchild: wait for signal to exit. */ + close(ipc_sockets[1]); + if (gc_pidfd >=3D 0) + close(gc_pidfd); + pause(); + _exit(0); + } + + /* Send grandchild PID to grandparent. */ + snprintf(buf, sizeof(buf), "%d", grandchild_pid); + write_nointr(ipc_sockets[1], buf, strlen(buf)); + close(ipc_sockets[1]); + if (gc_pidfd >=3D 0) + close(gc_pidfd); + + /* Middle child exits, grandchild gets reparented. */ + _exit(0); + } + + close(ipc_sockets[1]); + + /* Read grandchild's PID. */ + ret =3D read_nointr(ipc_sockets[0], buf, sizeof(buf) - 1); + close(ipc_sockets[0]); + ASSERT_GT(ret, 0); + + if (buf[0] =3D=3D 'E') { + waitpid(mid_pid, NULL, 0); + prctl(PR_SET_CHILD_SUBREAPER, 0); + SKIP(return, "CLONE_AUTOREAP not supported"); + } + + grandchild_pid =3D atoi(buf); + ASSERT_GT(grandchild_pid, 0); + + /* Wait for the middle child to exit. */ + ret =3D waitpid(mid_pid, NULL, 0); + ASSERT_EQ(ret, mid_pid); + + /* + * Now the grandchild is reparented to us (subreaper). + * Open a pidfd for the grandchild and kill it. + */ + pidfd =3D sys_pidfd_open(grandchild_pid, 0); + ASSERT_GE(pidfd, 0); + + ret =3D sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0); + ASSERT_EQ(ret, 0); + + /* Wait for it to exit via pidfd poll. */ + pfd.fd =3D pidfd; + pfd.events =3D POLLIN; + ret =3D poll(&pfd, 1, 5000); + ASSERT_EQ(ret, 1); + ASSERT_TRUE(pfd.revents & POLLIN); + + /* + * The grandchild should have been autoreaped even though + * we (the new parent) haven't set SA_NOCLDWAIT. + * waitpid should return -1/ECHILD. + */ + ret =3D waitpid(grandchild_pid, NULL, WNOHANG); + EXPECT_EQ(ret, -1); + EXPECT_EQ(errno, ECHILD); + + close(pidfd); + + /* Clean up subreaper status. */ + prctl(PR_SET_CHILD_SUBREAPER, 0); +} + +static int thread_sock_fd; + +static void *thread_func(void *arg) +{ + /* Signal parent we're running. */ + write_nointr(thread_sock_fd, "1", 1); + + /* Give main thread time to call _exit() first. */ + usleep(200000); + + return NULL; +} + +/* + * Test that an autoreap child with multiple threads is properly + * autoreaped only after all threads have exited. + */ +TEST(autoreap_multithreaded) +{ + struct pidfd_info info =3D { .mask =3D PIDFD_INFO_EXIT }; + int ipc_sockets[2], ret; + int pidfd =3D -1; + struct pollfd pfd; + pid_t pid; + char c; + + ret =3D socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets); + ASSERT_EQ(ret, 0); + + pid =3D create_autoreap_child(&pidfd); + if (pid < 0 && errno =3D=3D EINVAL) { + close(ipc_sockets[0]); + close(ipc_sockets[1]); + SKIP(return, "CLONE_AUTOREAP not supported"); + } + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) { + pthread_t thread; + + close(ipc_sockets[0]); + + /* + * Create a sub-thread that outlives the main thread. + * The thread signals readiness, then sleeps. + * The main thread waits briefly, then calls _exit(). + */ + thread_sock_fd =3D ipc_sockets[1]; + pthread_create(&thread, NULL, thread_func, NULL); + pthread_detach(thread); + + /* Wait for thread to be running. */ + usleep(100000); + + /* Main thread exits; sub-thread is still alive. */ + _exit(99); + } + + close(ipc_sockets[1]); + + /* Wait for the sub-thread to signal readiness. */ + ret =3D read_nointr(ipc_sockets[0], &c, 1); + close(ipc_sockets[0]); + ASSERT_EQ(ret, 1); + + /* Wait for the process to fully exit via pidfd poll. */ + pfd.fd =3D pidfd; + pfd.events =3D POLLIN; + ret =3D poll(&pfd, 1, 5000); + ASSERT_EQ(ret, 1); + ASSERT_TRUE(pfd.revents & POLLIN); + + /* Verify exit info. */ + ret =3D ioctl(pidfd, PIDFD_GET_INFO, &info); + ASSERT_EQ(ret, 0); + ASSERT_TRUE(info.mask & PIDFD_INFO_EXIT); + ASSERT_TRUE(WIFEXITED(info.exit_code)); + ASSERT_EQ(WEXITSTATUS(info.exit_code), 99); + + /* No zombie. */ + ret =3D waitpid(pid, NULL, WNOHANG); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, ECHILD); + + close(pidfd); +} + +/* + * Test that autoreap is NOT inherited by grandchildren. + */ +TEST(autoreap_no_inherit) +{ + int ipc_sockets[2], ret; + int pidfd =3D -1; + pid_t pid; + char buf[2] =3D {}; + struct pollfd pfd; + + ret =3D socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets); + ASSERT_EQ(ret, 0); + + pid =3D create_autoreap_child(&pidfd); + if (pid < 0 && errno =3D=3D EINVAL) { + close(ipc_sockets[0]); + close(ipc_sockets[1]); + SKIP(return, "CLONE_AUTOREAP not supported"); + } + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) { + pid_t gc; + int status; + + close(ipc_sockets[0]); + + /* Autoreap child forks a grandchild (without autoreap). */ + gc =3D fork(); + if (gc < 0) { + write_nointr(ipc_sockets[1], "E", 1); + _exit(1); + } + if (gc =3D=3D 0) { + /* Grandchild: exit immediately. */ + close(ipc_sockets[1]); + _exit(77); + } + + /* + * The grandchild should become a regular zombie + * since it was NOT created with CLONE_AUTOREAP. + * Wait for it to verify. + */ + ret =3D waitpid(gc, &status, 0); + if (ret =3D=3D gc && WIFEXITED(status) && + WEXITSTATUS(status) =3D=3D 77) { + write_nointr(ipc_sockets[1], "P", 1); + } else { + write_nointr(ipc_sockets[1], "F", 1); + } + close(ipc_sockets[1]); + _exit(0); + } + + close(ipc_sockets[1]); + + ret =3D read_nointr(ipc_sockets[0], buf, 1); + close(ipc_sockets[0]); + ASSERT_EQ(ret, 1); + + /* + * 'P' means the autoreap child was able to waitpid() its + * grandchild (correct - grandchild should be a normal zombie, + * not autoreaped). + */ + ASSERT_EQ(buf[0], 'P'); + + /* Wait for the autoreap child to exit. */ + pfd.fd =3D pidfd; + pfd.events =3D POLLIN; + ret =3D poll(&pfd, 1, 5000); + ASSERT_EQ(ret, 1); + + /* Autoreap child itself should be autoreaped. */ + ret =3D waitpid(pid, NULL, WNOHANG); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, ECHILD); + + close(pidfd); +} + +TEST_HARNESS_MAIN --=20 2.47.3 From nobody Fri Apr 3 04:39:51 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 506973115B0; Tue, 17 Feb 2026 22:36:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771367801; cv=none; b=Nz7OPFfgrrO6x8D2SDpumH1Qs/4Ga2mQQerDJihbDHrmPIVhc0gB7JniAQP8HXbC2qq4KZYY4dD8/DHzRos8DJHWq/D081oMEe2J/DIi7+Zqzo204Cm/pHfLcy5Bh70+UR6c/qbjNF6d+WyfPbVL7nlX4bf3UhL25u8tBmDLQbk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771367801; c=relaxed/simple; bh=s0blIPC3flognjkRr3c4Cqmf4RCIecIC8Ux7vpdHkxc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HrEW0Ksi5/13vZsqfPfoTs+Ox7fDqRwSJUsOEUH6Jg92rv5LbQlOTH7GMv8UFbkQvVtg7udKY/cgOPK8Xhv50/rGDdcXYu+gKEfHvp5EMOgCwCt0nXHi8uOANEIT6l9RIqkjWU88MUpz2gbzl6o//WYC1ZoXHQ8UbioNDlut++M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qx8V/DRn; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qx8V/DRn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4265FC4CEF7; Tue, 17 Feb 2026 22:36:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771367800; bh=s0blIPC3flognjkRr3c4Cqmf4RCIecIC8Ux7vpdHkxc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qx8V/DRn1WkYWv1ZbRCoNdfrqTSJ7i5D6POKmNbY3gRKLhAEFJ/Q2ZxiQS7+bR+3X 1F/B19NI2TQQhayklCohBfra8rPbgKh8OPRFYPCdKlAQ27cmJKhluOXFxiy/L67Zps j36cMY4R0gAOkLTNEE0c9lN4eo/Go1gtPnGTlY0DRZ1K49J46dsOQARGI9Gr09TsX9 A6kjzn+zTjGSvi9ioXOn3Y7hWiuwtwoMcrqpOBVFbTrB3zwnT1aaIgheSh3yGttgdT OzPyr3QhQ3SDyMbKmV71cRWcSVH7tmFG6BCx0yQ8Rr+9k09fAfU1BE+l/Dde/r+2nO +5I1G/40y6Jdg== From: Christian Brauner Date: Tue, 17 Feb 2026 23:35:53 +0100 Subject: [PATCH RFC v3 4/4] selftests/pidfd: add CLONE_PIDFD_AUTOKILL tests Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260217-work-pidfs-autoreap-v3-4-33a403c20111@kernel.org> References: <20260217-work-pidfs-autoreap-v3-0-33a403c20111@kernel.org> In-Reply-To: <20260217-work-pidfs-autoreap-v3-0-33a403c20111@kernel.org> To: Oleg Nesterov , Jann Horn Cc: Linus Torvalds , Ingo Molnar , Peter Zijlstra , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Christian Brauner X-Mailer: b4 0.15-dev-47773 X-Developer-Signature: v=1; a=openpgp-sha256; l=5781; i=brauner@kernel.org; h=from:subject:message-id; bh=s0blIPC3flognjkRr3c4Cqmf4RCIecIC8Ux7vpdHkxc=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWROeZt3bXL23/QFqq+E93jM+io51+zS4i037tYd3tpjO jPUwLoisaOUhUGMi0FWTJHFod0kXG45T8Vmo0wNmDmsTCBDGLg4BWAih20Z/tfqf7eNmXtXIWTy gm8c3X8+c/3Tval+YPKvbcciTmRU2Csw/OEz2cx2w//QoVd7N77rf71HRsiIt6BA78qj6mn3U34 6s3EDAA== X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Add tests for the new CLONE_PIDFD_AUTOKILL clone3() flag: - autokill_basic: child blocks in pause(), parent closes clone3 pidfd, child is killed and autoreaped - autokill_requires_pidfd: CLONE_PIDFD_AUTOKILL without CLONE_PIDFD fails with EINVAL - autokill_requires_autoreap: CLONE_PIDFD_AUTOKILL without CLONE_AUTOREAP fails with EINVAL - autokill_rejects_thread: CLONE_PIDFD_AUTOKILL with CLONE_THREAD fails with EINVAL - autokill_pidfd_open_no_effect: closing a pidfd_open() fd does not kill the child, closing the clone3 pidfd does Signed-off-by: Christian Brauner --- .../testing/selftests/pidfd/pidfd_autoreap_test.c | 187 +++++++++++++++++= ++++ 1 file changed, 187 insertions(+) diff --git a/tools/testing/selftests/pidfd/pidfd_autoreap_test.c b/tools/te= sting/selftests/pidfd/pidfd_autoreap_test.c index 3c0c45359473..a1dc4f075fc3 100644 --- a/tools/testing/selftests/pidfd/pidfd_autoreap_test.c +++ b/tools/testing/selftests/pidfd/pidfd_autoreap_test.c @@ -28,6 +28,10 @@ #define CLONE_AUTOREAP 0x400000000ULL #endif =20 +#ifndef CLONE_PIDFD_AUTOKILL +#define CLONE_PIDFD_AUTOKILL 0x800000000ULL +#endif + static pid_t create_autoreap_child(int *pidfd) { struct __clone_args args =3D { @@ -486,4 +490,187 @@ TEST(autoreap_no_inherit) close(pidfd); } =20 +/* + * Helper: create a child with CLONE_PIDFD | CLONE_PIDFD_AUTOKILL | CLONE_= AUTOREAP. + */ +static pid_t create_autokill_child(int *pidfd) +{ + struct __clone_args args =3D { + .flags =3D CLONE_PIDFD | CLONE_PIDFD_AUTOKILL | + CLONE_AUTOREAP, + .exit_signal =3D 0, + .pidfd =3D ptr_to_u64(pidfd), + }; + + return sys_clone3(&args, sizeof(args)); +} + +/* + * Basic autokill test: child blocks in pause(), parent closes the + * clone3 pidfd, child should be killed and autoreaped. + */ +TEST(autokill_basic) +{ + int pidfd =3D -1, pollfd_fd =3D -1, ret; + struct pollfd pfd; + pid_t pid; + + pid =3D create_autokill_child(&pidfd); + if (pid < 0 && errno =3D=3D EINVAL) + SKIP(return, "CLONE_PIDFD_AUTOKILL not supported"); + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) { + pause(); + _exit(1); + } + + ASSERT_GE(pidfd, 0); + + /* + * Open a second pidfd via pidfd_open() so we can observe the + * child's death after closing the clone3 pidfd. + */ + pollfd_fd =3D sys_pidfd_open(pid, 0); + ASSERT_GE(pollfd_fd, 0); + + /* Close the clone3 pidfd =E2=80=94 this should trigger autokill. */ + close(pidfd); + + /* Wait for the child to die via the pidfd_open'd fd. */ + pfd.fd =3D pollfd_fd; + pfd.events =3D POLLIN; + ret =3D poll(&pfd, 1, 5000); + ASSERT_EQ(ret, 1); + ASSERT_TRUE(pfd.revents & POLLIN); + + /* Child should be autoreaped =E2=80=94 no zombie. */ + usleep(100000); + ret =3D waitpid(pid, NULL, WNOHANG); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, ECHILD); + + close(pollfd_fd); +} + +/* + * CLONE_PIDFD_AUTOKILL without CLONE_PIDFD must fail with EINVAL. + */ +TEST(autokill_requires_pidfd) +{ + struct __clone_args args =3D { + .flags =3D CLONE_PIDFD_AUTOKILL | CLONE_AUTOREAP, + .exit_signal =3D 0, + }; + pid_t pid; + + pid =3D sys_clone3(&args, sizeof(args)); + ASSERT_EQ(pid, -1); + ASSERT_EQ(errno, EINVAL); +} + +/* + * CLONE_PIDFD_AUTOKILL without CLONE_AUTOREAP must fail with EINVAL. + */ +TEST(autokill_requires_autoreap) +{ + struct __clone_args args =3D { + .flags =3D CLONE_PIDFD | CLONE_PIDFD_AUTOKILL, + .exit_signal =3D SIGCHLD, + }; + int pidfd =3D -1; + pid_t pid; + + args.pidfd =3D ptr_to_u64(&pidfd); + + pid =3D sys_clone3(&args, sizeof(args)); + ASSERT_EQ(pid, -1); + ASSERT_EQ(errno, EINVAL); +} + +/* + * CLONE_PIDFD_AUTOKILL with CLONE_THREAD must fail with EINVAL. + */ +TEST(autokill_rejects_thread) +{ + struct __clone_args args =3D { + .flags =3D CLONE_PIDFD | CLONE_PIDFD_AUTOKILL | + CLONE_AUTOREAP | CLONE_THREAD | + CLONE_SIGHAND | CLONE_VM, + .exit_signal =3D 0, + }; + int pidfd =3D -1; + pid_t pid; + + args.pidfd =3D ptr_to_u64(&pidfd); + + pid =3D sys_clone3(&args, sizeof(args)); + ASSERT_EQ(pid, -1); + ASSERT_EQ(errno, EINVAL); +} + +/* + * Test that only the clone3 pidfd triggers autokill, not pidfd_open(). + * Close the pidfd_open'd fd first =E2=80=94 child should survive. + * Then close the clone3 pidfd =E2=80=94 child should be killed and autore= aped. + */ +TEST(autokill_pidfd_open_no_effect) +{ + int pidfd =3D -1, open_fd =3D -1, ret; + struct pollfd pfd; + pid_t pid; + + pid =3D create_autokill_child(&pidfd); + if (pid < 0 && errno =3D=3D EINVAL) + SKIP(return, "CLONE_PIDFD_AUTOKILL not supported"); + ASSERT_GE(pid, 0); + + if (pid =3D=3D 0) { + pause(); + _exit(1); + } + + ASSERT_GE(pidfd, 0); + + /* Open a second pidfd via pidfd_open(). */ + open_fd =3D sys_pidfd_open(pid, 0); + ASSERT_GE(open_fd, 0); + + /* + * Close the pidfd_open'd fd =E2=80=94 child should survive because + * only the clone3 pidfd has autokill. + */ + close(open_fd); + usleep(200000); + + /* Verify child is still alive by polling the clone3 pidfd. */ + pfd.fd =3D pidfd; + pfd.events =3D POLLIN; + ret =3D poll(&pfd, 1, 0); + ASSERT_EQ(ret, 0) { + TH_LOG("Child died after closing pidfd_open fd =E2=80=94 should still be= alive"); + } + + /* Open another observation fd before triggering autokill. */ + open_fd =3D sys_pidfd_open(pid, 0); + ASSERT_GE(open_fd, 0); + + /* Now close the clone3 pidfd =E2=80=94 this triggers autokill. */ + close(pidfd); + + pfd.fd =3D open_fd; + pfd.events =3D POLLIN; + ret =3D poll(&pfd, 1, 5000); + ASSERT_EQ(ret, 1); + ASSERT_TRUE(pfd.revents & POLLIN); + + /* Child should be autoreaped =E2=80=94 no zombie. */ + usleep(100000); + ret =3D waitpid(pid, NULL, WNOHANG); + ASSERT_EQ(ret, -1); + ASSERT_EQ(errno, ECHILD); + + close(open_fd); +} + TEST_HARNESS_MAIN --=20 2.47.3