From nobody Mon Jun 8 09:48:23 2026 Received: from mail-dy1-f195.google.com (mail-dy1-f195.google.com [74.125.82.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B22762F90E0 for ; Sat, 30 May 2026 01:37:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780105071; cv=none; b=YyhI78OkFlDO9uXiWZixc9OZQ4dgiyb0FZh96k1KLobV22IsT0f2xaivmo6mZCcMSRb8ZWZ+Kcyt+Y6HTFShyAGNpM6K+w+csn7UuI28AusfunVM6HFWXrBsXzzWOW1nsxlODCDIHyto4n05ZORi+cK8rGP66TXhsZ647CxAxWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780105071; c=relaxed/simple; bh=OzxvXiS7gZrNxYNXj9poA0um2pTBU84WZQnPOJzbptA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TytdBtfvk4R9B7mRI0zng1zhH7gJC4HkjVwiHN+HNA+05mPcPYDVHZdifCI8gW5yjV4AuHG+wUT7GzAM1K8q8R75xix8v5qWUxJVT409A3eMws5Cr4IdJjmL3cXksdqQuifGLB0tmvtMCo4Z9RteXjZitXV3lkkPlc3VAQ6FAQU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cmeSLqgY; arc=none smtp.client-ip=74.125.82.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cmeSLqgY" Received: by mail-dy1-f195.google.com with SMTP id 5a478bee46e88-304d8e3bb72so1893640eec.1 for ; Fri, 29 May 2026 18:37:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780105069; x=1780709869; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=30Zmw8a16oaljA7WTVadlHqTZXIGr1FxDCtkAmoDkcE=; b=cmeSLqgYEz9uG//mSUedBUh3pW8TkWgMqn/K8YfwWtuebB3vrXSgMuCAoZGXyKvPIF y5UqMUOmqwuiDo4EZnPN+0GXCR4zQ4MDf62duMNleuDDcPp3gD+Z/7Rser+YiPuvAI2J nZiu/VsE5zjek0djlMrQhxgEIgbfMYyTMMWu2oXK+yg1M84Uy6DaP0XvurHDxy2PZExl vRZKDf3wajPxOc0EG1lLCa9AFwc6h7LXby9+DfilmkffnX01U1TJsSIWgmKUF1ee6kU2 1nPLXAiqWA1mLi1grxwHHvak6nw/JNRHa+kKehTU6BhZqxmcXX3EPxvbaZUdvOk36lDg 40lQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780105069; x=1780709869; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=30Zmw8a16oaljA7WTVadlHqTZXIGr1FxDCtkAmoDkcE=; b=tMYzn0oQoPZtcn2EmjpkJgLop/vXUNDhwFMNMBXgi84y8fNFnbh5NcwEryJWpYVfj8 PNluYrVyY4QMsw90+AWdeit2w5wo15tCHFt6MEm+iaxXk1yS5gPOl5UrknBCXuZJTjQM ghLK/FFGIuPjbx3r6bW7oh0TMpYLJBMtRcxQISL8Jrit8Su6KiRI6MIxL3VEaxv++B6U gztxkyXHmSo33f7o0XDIS7QLkBTtwomrweZBbyggLiBozbn0TGKrsZuRS6uSTjmTdeva i33FZDA23FvzP1ftF/9rvZEGpt9WyivTbAdcz9sT4UeAAFuPO8GLc0Vpel6ElfN8R8iY UBGg== X-Forwarded-Encrypted: i=1; AFNElJ8dLuTx/bT6ZdOjfw2cEDKUkE+y1uplH5XZqAG5h5qBJwTVEp4Mwczgelo1YXgsF5XGIfVqxIbqmcVqcn0=@vger.kernel.org X-Gm-Message-State: AOJu0YwXmtKB7WiyYipy9f2BhS27fCGIMmSH2BXwgeNFx4EuFiy5uRVr gXcUan6kAaMKlIwb/I5lyQa3UosJx4Llo8HRKgzQDcL0yv/3hSZiqR+a X-Gm-Gg: Acq92OFkzRsknDWHgqXS4N9qZmf3SLcy+nCz4IqR8lDWDaCsg8mJhv+5fO+wdJoZtGv jVwYq3QdRAntuu/LaM1E2E1+AnRg4ttMTmzmxr9pTFEmNfSqTWa3F+C4JT3kAd3yYd5IcEXeE8j 0Me3WQsNVqfjuC0KpxddfgVeTXcIo/r3B79+y/0MLWsCVjGS59xpv+L4FIxOkitjmOLvxkeRbhY U5Pr8sj6FDM+bUEumtXuklVFSRqB72sH36mnKAKS1RsfIlMFM6OhOXiCvtYH1+JfpGGk17klvTK toi4myHUIZEsVBFJEdDmCbiZcvIJNoHXwujTYslmEpLxHwXWLbi2l0ey0HVGwwcwKYAN1mt2ld7 Z2q9rOSsIPZzP2LkCGu/QoGP8j79rF4fBQWTyJgv151zTuYrIo3+IyfUh0H35sEAlQQdT9OWEwa y3uM8E2mVc0T0Ssek5hkHn6mWy7wyACw== X-Received: by 2002:a05:7300:6427:b0:304:d75b:f5f4 with SMTP id 5a478bee46e88-304fa67e9c3mr1186278eec.16.1780105068778; Fri, 29 May 2026 18:37:48 -0700 (PDT) Received: from anonyme ([2605:52c0:2:2f27:be24:11ff:fe89:6f0f]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-304ed5a114dsm2881199eec.24.2026.05.29.18.37.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 18:37:48 -0700 (PDT) From: AnonymeMeow To: amir73il@gmail.com Cc: brauner@kernel.org, jack@suse.cz, repnop@google.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, AnonymeMeow Subject: [PATCH v3 1/2] fanotify: report thread pidfds for FAN_REPORT_TID Date: Sat, 30 May 2026 09:37:31 +0800 Message-ID: <20260530013732.50811-2-anonymemeow@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260530013732.50811-1-anonymemeow@gmail.com> References: <20260530013732.50811-1-anonymemeow@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The FAN_REPORT_PIDFD and FAN_REPORT_TID flags used to be mutually exclusive because by the time the pidfd support was introduced to fanotify, pidfds could only be created for thread group leaders. Now that the pidfd API supports thread-specific pidfds via PIDFD_THREAD, this restriction can be lifted. Signed-off-by: AnonymeMeow Reviewed-by: Amir Goldstein --- fs/notify/fanotify/fanotify_user.c | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanoti= fy_user.c index ae904451dfc0..ebdd48942029 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -19,6 +19,7 @@ #include #include #include +#include =20 #include =20 @@ -903,25 +904,21 @@ static ssize_t copy_event_to_user(struct fsnotify_gro= up *group, metadata.fd =3D fd >=3D 0 ? fd : FAN_NOFD; =20 if (pidfd_mode) { - /* - * Complain if the FAN_REPORT_PIDFD and FAN_REPORT_TID mutual - * exclusion is ever lifted. At the time of incoporating pidfd - * support within fanotify, the pidfd API only supported the - * creation of pidfds for thread-group leaders. - */ - WARN_ON_ONCE(FAN_GROUP_FLAG(group, FAN_REPORT_TID)); + unsigned int tid_mode =3D FAN_GROUP_FLAG(group, FAN_REPORT_TID); + enum pid_type pidtype =3D tid_mode ? PIDTYPE_PID : PIDTYPE_TGID; + unsigned int pidfd_flags =3D tid_mode ? PIDFD_THREAD : 0; =20 /* - * The PIDTYPE_TGID check for an event->pid is performed + * The pid_has_task() check for an event->pid is performed * preemptively in an attempt to catch out cases where the event - * listener reads events after the event generating process has + * listener reads events after the event generating task has * already terminated. Depending on flag FAN_REPORT_FD_ERROR, * report either -ESRCH or FAN_NOPIDFD to the event listener in * those cases with all other pidfd creation errors reported as * the error code itself or as FAN_EPIDFD. */ - if (metadata.pid && pid_has_task(event->pid, PIDTYPE_TGID)) - pidfd =3D pidfd_prepare(event->pid, 0, &pidfd_file); + if (metadata.pid && pid_has_task(event->pid, pidtype)) + pidfd =3D pidfd_prepare(event->pid, pidfd_flags, &pidfd_file); =20 if (!FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR) && pidfd < 0) pidfd =3D pidfd =3D=3D -ESRCH ? FAN_NOPIDFD : FAN_EPIDFD; @@ -1628,14 +1625,6 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, = unsigned int, event_f_flags) #endif return -EINVAL; =20 - /* - * A pidfd can only be returned for a thread-group leader; thus - * FAN_REPORT_PIDFD and FAN_REPORT_TID need to remain mutually - * exclusive. - */ - if ((flags & FAN_REPORT_PIDFD) && (flags & FAN_REPORT_TID)) - return -EINVAL; - /* Don't allow mixing mnt events with inode events for now */ if (flags & FAN_REPORT_MNT) { if (class !=3D FAN_CLASS_NOTIF) --=20 2.54.0 From nobody Mon Jun 8 09:48:23 2026 Received: from mail-dy1-f193.google.com (mail-dy1-f193.google.com [74.125.82.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 476001F5437 for ; Sat, 30 May 2026 01:37:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.193 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780105073; cv=none; b=hjCDKJquLSm2dDxlMTvpgGnLcIYoDhcB6voAQ1iRGNCgyM/87NankcCXTgB1nCS2HHHHakus3ot/iC7YznJ3saFZIFYJOzgkT93AvvM7gQnb7iHJAgIcVj02HCYZTFkzHwGkZ8Cant8I9k5SaIfl2Lm9D8ABksP/9KOLuKlnuiI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780105073; c=relaxed/simple; bh=zp/O9C3NrfWRcoAYcVmLQslmY4MXAMaLJUWh8lNXlPs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PMHZCRNZBiQHuBK9zLjhTtzSZH/xZlfwEjBqEmyFtRBFT0EKwG6PZUd443vB/RimHF/wUDV3rdI5TaOTP/NFMWLlZe14MgBcC/TN4w6Rwzle3js7SLRwTBZDhil414M1rN1b0SoUTPaTdbu4K0e1DMQfJyzeoDkURs6P5/YzOck= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=oq0QC1Qm; arc=none smtp.client-ip=74.125.82.193 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oq0QC1Qm" Received: by mail-dy1-f193.google.com with SMTP id 5a478bee46e88-304f0039c02so2205143eec.1 for ; Fri, 29 May 2026 18:37:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780105071; x=1780709871; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pIx0nlS5Y68ho2QMjRaFjrw3/zH1pK3m15usRWzKhCo=; b=oq0QC1QmsvIc8asmV5pZtz+E1dO2sUGu/ovy0SOa1ETN/Ci3JO9vxs+84L6QAdeb8m 0hpitpEKv0v1qd69MAWWIfy6wIY4fY66d5Ntiy0AttlQILmkS1xL9AuuJwHD3pfc3P96 4t2g1zCAal8gg6q7FoeaD5J+7WN0+Sb44+3o0KC0AXgLvYwdKIKRBjwzVxYBlSniJqX+ zD/aIEyuRLgOCty/2QVFELlKHROeZ67YnHWU/JgE3+KGn7cHZMhnZbb1eafDtk2pYk2O j5KJxmSBmQqxVdD1jakHeSprSdjKoYwlzZ76+oxRj/8KMtXygPST4bpL2baR46tO7D8U xJfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780105071; x=1780709871; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=pIx0nlS5Y68ho2QMjRaFjrw3/zH1pK3m15usRWzKhCo=; b=hLT4ZA7Wz+QRdKBcUmTJ+4yrijABLArs7u0+6kVCWZ/nvtOYfR4RAOEQFlRbZY3W+U g11wk3B/dltr8m5SQYZ6nVUfWM4kpNhcrry18bRPHPlKlfFX/Wnz9waIqYVS1wJ8rW1+ HyWBbRELoPg1m+FvzpNYtbXwVWSrYYWus9IZ99q4maua6HCkbZAGLIMU+nLUymoPt6lb ZCo/rZkU6k3XFdy4isWT9cSJb8Xrl1CtUd30ahI2OJFrjnimZgBYxBwQKL1aCiN/rBlx SbKvOiAaeVdMc2/ZvaoRsUNtoh37NCPuv8f9Am3cVt/6b6mNmQmzxUJ6zUL7mFBTsNtP 3w/w== X-Forwarded-Encrypted: i=1; AFNElJ8mNtBLp5R6ORucmLdpYNPfiEHHUEp71sFAwLXBLMHjRLPHgdeee4evgLy8sDSt1+l517GKmhqvohGIhYU=@vger.kernel.org X-Gm-Message-State: AOJu0YyBr8KSBOP5dphZC6b+w8457hickP2SOKV9lTBk4dWyeD8QoMFs c1MjgAj+wn/l2qVeyXg2htR3FzQV3X4luwHpmvxKXf6BAoGkBLoohvU6 X-Gm-Gg: Acq92OHZIcc4fw+dsnbJRqSX957JLj0i7QJF2AcKQbZLmzfK1UmmNthpArDH+aL39vQ l+yZ/CH37Yf7nEKqjwo+ybIkYmcbUfmFlCPkJgAKb01Ta93niRemSY29h95txk7Eee61gKM8Ybd Zx4QOojmUb4ky3cIyImpoL/qwWuziUvRJbl2tm9OfR5UsVOw4lsrhCWPdk1LF/ly/NAVxuVAgkO uKu8SKf6hTWOrAV6+ULjNRhIX+lx60tpI5WwaZGEbIIZLUC4Uj6Je+q4yL1gxpLaIvUMDYJrZbT 0YEDFIOUlF3y4CCz3hsvA8Ti9RJVMf46fgswhb7ihF/HemNZn+udINldktwfCSLkWZS7A/exC19 UI7jNIwJ6bpVgum30aocBdTaAWcyST+zO4vWDy1+CDqbpNlPsFs8o3g5ifG8Q7JcBVQctJb9MDy jKduVvBotezZ6a5kGTrffbkwmIunkmDw== X-Received: by 2002:a05:7300:3722:b0:304:d4c0:82ea with SMTP id 5a478bee46e88-304fa5e85b7mr1116536eec.21.1780105071364; Fri, 29 May 2026 18:37:51 -0700 (PDT) Received: from anonyme ([2605:52c0:2:2f27:be24:11ff:fe89:6f0f]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-304ed5a114dsm2881199eec.24.2026.05.29.18.37.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 May 2026 18:37:51 -0700 (PDT) From: AnonymeMeow To: amir73il@gmail.com Cc: brauner@kernel.org, jack@suse.cz, repnop@google.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, AnonymeMeow Subject: [PATCH v3 2/2] fanotify: allow reporting pidfds for reaped tasks Date: Sat, 30 May 2026 09:37:32 +0800 Message-ID: <20260530013732.50811-3-anonymemeow@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260530013732.50811-1-anonymemeow@gmail.com> References: <20260530013732.50811-1-anonymemeow@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Fanotify used to refuse to report pidfds for reaped tasks by applying a pid_has_task() check before calling pidfd_prepare(). This prevented userspace from obtaining information about the task. Fanotify events already hold a reference to the task's pid struct, so the check can be safely dropped and pidfds can be reported for reaped tasks as well. Signed-off-by: AnonymeMeow Reviewed-by: Amir Goldstein Suggested-by: Christian Brauner --- fs/notify/fanotify/fanotify_user.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanoti= fy_user.c index ebdd48942029..b604e3da58ad 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -904,20 +904,12 @@ static ssize_t copy_event_to_user(struct fsnotify_gro= up *group, metadata.fd =3D fd >=3D 0 ? fd : FAN_NOFD; =20 if (pidfd_mode) { - unsigned int tid_mode =3D FAN_GROUP_FLAG(group, FAN_REPORT_TID); - enum pid_type pidtype =3D tid_mode ? PIDTYPE_PID : PIDTYPE_TGID; - unsigned int pidfd_flags =3D tid_mode ? PIDFD_THREAD : 0; + unsigned int pidfd_flags =3D PIDFD_STALE; =20 - /* - * The pid_has_task() check for an event->pid is performed - * preemptively in an attempt to catch out cases where the event - * listener reads events after the event generating task has - * already terminated. Depending on flag FAN_REPORT_FD_ERROR, - * report either -ESRCH or FAN_NOPIDFD to the event listener in - * those cases with all other pidfd creation errors reported as - * the error code itself or as FAN_EPIDFD. - */ - if (metadata.pid && pid_has_task(event->pid, pidtype)) + if (FAN_GROUP_FLAG(group, FAN_REPORT_TID)) + pidfd_flags |=3D PIDFD_THREAD; + + if (metadata.pid) pidfd =3D pidfd_prepare(event->pid, pidfd_flags, &pidfd_file); =20 if (!FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR) && pidfd < 0) --=20 2.54.0 From nobody Mon Jun 8 09:48:23 2026 Received: from mail-dy1-f195.google.com (mail-dy1-f195.google.com [74.125.82.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C9E737AA83 for ; Sat, 30 May 2026 10:51:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780138284; cv=none; b=P/SQYwmTLBJSxTHoh+4CI+dBRWByVRED32JXpJtsoUZ8GD/WN39Sg0M42vazsWm66YTnMQeHoFtbV8i8aw0bv4I5zqGkRrPShrp5VlkFrOazKj0QIHECRLQ9BdjBHYpptzzfDisEqqymM+zq7VkQZkqE+pfpTITkCSCPQbG/OrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780138284; c=relaxed/simple; bh=/+1+lZ9YS5HjmLAOWU1Gb2rtgqEauNFXeqweFhA2+C8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZqrP5byqAryzAvgESpWdyfKsrEwM/DH8w10AVO+xyL7/0F22dZ4AwGmElK8lrAUZdXg17AEckFGSHC1k1BLMfa6dCMJpEWWJMVDNzaOr/H7MpDaHLsBAiA/2UN+Wjkq09vzZf43aYMMazDxsVadJzTUm+p5AgjOrTgac5+Rv9BI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SJbV301A; arc=none smtp.client-ip=74.125.82.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SJbV301A" Received: by mail-dy1-f195.google.com with SMTP id 5a478bee46e88-304ffa40c5dso1531909eec.1 for ; Sat, 30 May 2026 03:51:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780138282; x=1780743082; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=iheNwFPT6VXXyqKSS6/OjeYfCgrEDkZZRTM2pKQumvc=; b=SJbV301Azp0wPIoJV+96jA0I4ylpoiYi6WdSA2DuXVV0ib5Nv/Tbog7SuLd4+Pn3S8 UdBZNzEfNSzRGkFD2cinIPzYJScZcgYWCaJ99AbsLXWBlXHim7N92M2I4bkDoczFyrPy Z3FqxKl8ZpdfDYr0TKzZXNqW/ezaAr2zuX0GXhu9TvhsQ+O4a93FNtnZ3dyzoZfcMnhq 73wYgvjuu0WPjxlYj2TRtMpPABhIdTwcfE0uylyn1O2ipp4UEokz6vtpDISkDBozPV/U e6VYrbHt7Hue0pKZlJL4UR5zseXlhXj2UjHnZ3RPj6J6aUbAAVEJoNGto8N8654R4Clw 7yoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780138282; x=1780743082; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=iheNwFPT6VXXyqKSS6/OjeYfCgrEDkZZRTM2pKQumvc=; b=SHxkaUz1JoloJAHXenCu7SzHA88N3EpSTAoXqxR/qNhKrBG6dHMrTqnz76oJp+l/fq Ozr0p8yVo9DaTrOgAm4FrkZVfhQwwta06+xlv6WkQlPS1a4w4T/WfVGaShVQmpNKEzpZ DmSjRBKH601Bzmkc3oTCzxK54ntwVq5i9S5gJWi44k6Ger8INIkomN52KZ1Y/UuroOlO U+IkEd8p8/GNfw0Cys0nvMLjBXTs8RjGYEEp94+p7nL8oQK9O2zhGgwuaH06hTyj9BMk ERF6+TNoMbWLuX7ZK5gnloJbaUXnCVcpWQxGbJVSS17Ocs+wd5zHNH4hCT8+DdGANxxq ZDYQ== X-Forwarded-Encrypted: i=1; AFNElJ8553bQB/BAKeSGCxR758/pq+YVHgKrMnF88mhFVMf2giUyiu7aVZwYwy1x4zae37h5ZelwxVcdlUJB+XE=@vger.kernel.org X-Gm-Message-State: AOJu0YyuZpqpaKmMqjSewEI9HDrxk8VcXsJIWHXUrwcwLrLPD8W7F0wh TElwel01lEklZpgZpkPwo303umHWy/Dgtq9k+mTfPoNmYXIXZ+sE7bKN X-Gm-Gg: Acq92OGUUOJg/a91YkyS+BJK1Cw/qCHMT4EvVRBhjPQ5Lr60iyUTXTk+MNXg9DfETXF kKT72qFbFqj/lQwc/JpoSeW1QTe5iwhQ3oBGaDaNvYrHVXz+d/Qc5KnOxIpvjKuD5BGNS6uXUXb zMUhBxl6tgEEihzLTedCIS2mtunJ59RbZo1qyqg718qvBF8byLmewzQxHKLN2IeX4yTjZgEPeJJ jGEotlCxCu9xwffqRxI61IOpvRIOtDuNcFWUYzJaTKRY8lanvTxTJHmK2ZVtNFd93C9Prt1mihP 8uV1dzQaZakiVbUs9konvVZ9fDA8CuzsGGq2SXdkRbizTp6RAhhrdk0HiL+5Ez1KRF0Bcm/Tieg 2uvYt/2MALl3KZG9KaHM7p/Kdb2GBz97gApwsvJgg8Cn7TrfkDH5EsvcqQMQi5ylzFFV53LhRpr +bj8Cztx+OIrvfPi857Jefh235Nh+uow== X-Received: by 2002:a05:7301:7213:b0:2f1:496c:94bf with SMTP id 5a478bee46e88-304fa5ab148mr1741836eec.16.1780138282318; Sat, 30 May 2026 03:51:22 -0700 (PDT) Received: from anonyme ([2605:52c0:2:2f27:be24:11ff:fe89:6f0f]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-304ed5a114dsm3814007eec.24.2026.05.30.03.51.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 May 2026 03:51:22 -0700 (PDT) From: AnonymeMeow To: brauner@kernel.org Cc: amir73il@gmail.com, jack@suse.cz, repnop@google.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, AnonymeMeow Subject: [RFC][PATCH] fanotify: register event pid for pidfd reporting Date: Sat, 30 May 2026 18:51:17 +0800 Message-ID: <20260530105117.45792-1-anonymemeow@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260530013732.50811-1-anonymemeow@gmail.com> References: <20260530013732.50811-1-anonymemeow@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" pidfd_prepare() may still fail if userspace reads the event after the task has been reaped and the pid has not been registered with pidfs. Register the event pid with pidfs when creating the fanotify event if pidfd reporting was requested, so pidfd_prepare() can later create a pidfd for the reaped task. Signed-off-by: AnonymeMeow --- Hi, Christian, I tested this patch set and found that fanotify still usually can not report pidfds for reaped tasks, because the event pid may not have been registered before it exits, and registering it after the task has been reaped is not allowed, so fanotify still fails to report a pidfd for the event. This patch registers the event pid when the fanotify event is created. With this change, the current LTP fanotify21 test fails as expected. But I do not know whether it is worth doing so to improve fanotify's ability to report pidfds for reaped tasks. And, what should fanotify do if pidfs_register_pid() fails? Should the error be reported to userspace through the pidfd info record, using the raw error when FAN_REPORT_FD_ERROR is set and FAN_EPIDFD otherwise? But the event struct in fanotify does not have spare space to store this error code. Or should it just ignore the failure here and let pidfd_prepare() fail later in copy_event_to_user()? I would appreciate your feedback. With Best Regards, AnonymeMeow Link: https://lore.kernel.org/lkml/20260528-schmuckvoll-heilen-garen-be77b4= 208671@brauner/ --- fs/notify/fanotify/fanotify.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 38290b9c07f7..24b2af397126 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -14,6 +14,7 @@ #include #include #include +#include =20 #include "fanotify.h" =20 @@ -868,6 +869,15 @@ static struct fanotify_event *fanotify_alloc_event( else pid =3D get_pid(task_tgid(current)); =20 + if (FAN_GROUP_FLAG(group, FAN_REPORT_PIDFD)) { + int err =3D pidfs_register_pid(pid); + if (err) { + /* + * What to do here? Pass this err to userspace via pidfd? + */ + } + } + /* Mix event info, FAN_ONDIR flag and pid into event merge key */ hash ^=3D hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS); fanotify_init_event(event, hash, mask); --=20 2.54.0