All of lore.kernel.org
 help / color / mirror / Atom feed
From: AnonymeMeow <anonymemeow@gmail.com>
To: jack@suse.cz
Cc: amir73il@gmail.com, brauner@kernel.org,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	AnonymeMeow <anonymemeow@gmail.com>
Subject: [PATCH v5 1/2] fanotify: report thread pidfds for FAN_REPORT_TID
Date: Sun,  7 Jun 2026 08:33:42 +0800	[thread overview]
Message-ID: <20260607003343.425939-2-anonymemeow@gmail.com> (raw)
In-Reply-To: <20260607003343.425939-1-anonymemeow@gmail.com>

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.

Reviewed-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: AnonymeMeow <anonymemeow@gmail.com>
---
 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/fanotify_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 <linux/memcontrol.h>
 #include <linux/statfs.h>
 #include <linux/exportfs.h>
+#include <linux/pidfd.h>
 
 #include <asm/ioctls.h>
 
@@ -903,25 +904,21 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
 		metadata.fd = fd >= 0 ? fd : FAN_NOFD;
 
 	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 = FAN_GROUP_FLAG(group, FAN_REPORT_TID);
+		enum pid_type pidtype = tid_mode ? PIDTYPE_PID : PIDTYPE_TGID;
+		unsigned int pidfd_flags = tid_mode ? PIDFD_THREAD : 0;
 
 		/*
-		 * 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 = pidfd_prepare(event->pid, 0, &pidfd_file);
+		if (metadata.pid && pid_has_task(event->pid, pidtype))
+			pidfd = pidfd_prepare(event->pid, pidfd_flags, &pidfd_file);
 
 		if (!FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR) && pidfd < 0)
 			pidfd = pidfd == -ESRCH ? FAN_NOPIDFD : FAN_EPIDFD;
@@ -1628,14 +1625,6 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
 #endif
 		return -EINVAL;
 
-	/*
-	 * 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 != FAN_CLASS_NOTIF)
-- 
2.54.0


  reply	other threads:[~2026-06-07  0:34 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-07  0:33 [PATCH v5 0/2] fanotify: lift pidfd reporting restrictions AnonymeMeow
2026-06-07  0:33 ` AnonymeMeow [this message]
2026-06-07  0:33 ` [PATCH v5 2/2] fanotify: allow reporting pidfds for reaped tasks AnonymeMeow
2026-06-09 10:29 ` [PATCH v5 0/2] fanotify: lift pidfd reporting restrictions Jan Kara
2026-06-10  7:31   ` Christian Brauner
2026-06-10  9:14     ` Jan Kara
2026-06-15 18:06       ` [LTP] [PATCH 0/5] fanotify: update pidfd tests for FAN_REPORT_TID AnonymeMeow
2026-06-15 18:06         ` [LTP] [PATCH 1/5] fanotify20: Allow FAN_REPORT_PIDFD with FAN_REPORT_TID on v7.2+ AnonymeMeow
2026-06-15 18:31           ` [LTP] " linuxtestproject.agent
2026-06-16  7:40           ` [LTP] [PATCH 1/5] " Jan Kara
2026-06-16 11:27             ` Amir Goldstein
2026-06-16 11:48               ` Jan Kara
2026-06-15 18:06         ` [LTP] [PATCH 2/5] fanotify21: Stop relying on exited child for pidfd error AnonymeMeow
2026-06-16  7:48           ` Jan Kara
2026-06-15 18:06         ` [LTP] [PATCH 3/5] fanotify21: Simplify read_pidfd_fdinfo() AnonymeMeow
2026-06-16  7:46           ` Jan Kara
2026-06-15 18:06         ` [LTP] [PATCH 4/5] fanotify21: Add test variants for FAN_REPORT_TID AnonymeMeow
2026-06-16  7:55           ` Jan Kara
2026-06-16  8:24           ` Andrea Cervesato via ltp
2026-06-16  9:12             ` Jan Kara
2026-06-16  9:37               ` Andrea Cervesato via ltp
2026-06-15 18:06         ` [LTP] [PATCH 5/5] fanotify21: Add FAN_REPORT_TID pidfd coverage AnonymeMeow
2026-06-16  7:57           ` Jan Kara
2026-06-16 11:39         ` [LTP] [PATCH 0/5] fanotify: update pidfd tests for FAN_REPORT_TID Amir Goldstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260607003343.425939-2-anonymemeow@gmail.com \
    --to=anonymemeow@gmail.com \
    --cc=amir73il@gmail.com \
    --cc=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.