From nobody Sun May 24 18:41:30 2026 Received: from mail-dl1-f65.google.com (mail-dl1-f65.google.com [74.125.82.65]) (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 AD17229D267 for ; Sun, 24 May 2026 10:25:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.65 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779618345; cv=none; b=Wqp9CAinV9V3joZ2dpo5DZyjul0vUOCwYzqi7UEvD76M4hBP0rcMRapx2YBIwytcMx2A9Z+vpMHBY0qLnhoJRjc4Mx20D67R+ASkf6eUfbNXyOltpIhmTCdSkJDSQTkzWgPKM2Nw6/RaFBCH32eHhgsEVNzbwrM04Lvukzq/o7I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779618345; c=relaxed/simple; bh=Zkwe+FHjTnwCvOOjOCEzpofz4/XYgwXRVgWyjO2nKVk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=IRtYn0NxRG3hgvElHMHIk1iI9LzxGeYJm9SHu5W8pg2uTjXFFlWJOx3FTh6nJU6ntdE5zb9jw19RFeBegVuRJtaULAMBtH9PpzNjcgXpyxvn9OqWv91RFgwWA/MfHGtFsfFWdcpfS1osYxQHaAMW2LWh/S+DzBUimTwJ8SFveP8= 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=DFDzvWhY; arc=none smtp.client-ip=74.125.82.65 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="DFDzvWhY" Received: by mail-dl1-f65.google.com with SMTP id a92af1059eb24-1329fc4bf77so4806419c88.1 for ; Sun, 24 May 2026 03:25:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779618343; x=1780223143; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Yt4h7VRnlzFq8oh7lDYohLF9u5YnnKlteE8zE4IEGK4=; b=DFDzvWhYpsETc1F/mjRXzw2yPBo5ZSdkqimCKnMJOkveMvNnMAk2bVonsxANO7MSac 3loo223Q1gHZgDDu9AzesTCx9FgakTaMdd4oOtXsYsbbENBfqbPvcHq5Zl0Jm5POlzY8 kekoUIDk+3GR1/QCVvKPSE65gb3wv356SsjX9h/wK1vEn8PiveK7/6lJ6zZTuaR7nO5B PVEBhJKlWoWh9N7712l6a+CSyK5sOgfCzH3Nq1JPyebX39ZIK9XgA3sx4iQ9GWMpUumi +OLZwCHuajfLTyjQ8eHTz1mSkHr5r6xk+pc7YfjIzsZdTUlvBIHdeymIYY+RBVeooapY BdlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779618343; x=1780223143; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Yt4h7VRnlzFq8oh7lDYohLF9u5YnnKlteE8zE4IEGK4=; b=mSLx8FylMpx63vj8Fep2ZxGAI825Zs4AAXTQonpRHbC2f6Keqx0rvklAjYIKvT4UKD LcX02D3cz33DyipQ8fN2JVyFSZ3umR5AzhMPgOWlEKJGoqGtAk0f+X1TWsbYtW70+OLn pZ7Cq1xXietfItttk0iN0g89QLU8ykeVH8vM9Q2uZWOFtkYGRWj1nHx/K2pIGuzo32rD ZYejfNjjZMIjvHwi/Mfh6SaZh00a2LJKvTKwX1qG06W0TGXuHe7yxrLULZ/srgnQuKzq tSk45Xy/+6uZjqtxSFj7ayRb+8m8zEOxWciW1IS+TJQj9qBpItoJvocf7M7hd9bPrctV ucyw== X-Forwarded-Encrypted: i=1; AFNElJ82zE67uuX/vmpK/s7rZxdgDtYJ6l8JOJ0/bIkBI1p9mi0fLLBpvsyDkjv+4tpOZ2UrgMQjNvqlFj8sQNU=@vger.kernel.org X-Gm-Message-State: AOJu0YxhTv4jSN0aX6asS7ao+x0Jn74UgEBIHtm0UQi2vUwjataYLNv1 4GhEREbPHvKc/6aFpBioomb2gN0VwTFPsY8Ao9CrGZzrt1jnq/aCvxnFArICy4c9 X-Gm-Gg: Acq92OGaTtOzVRWJZ4LS0pYH+Xl581pA4GttTm0s+WhrM93bdhegBRUpGd5hM+3TWCS 6i83IImIlmtxc7PbH2nRhmRp3xo48+RwXhETQ6Q7lfSh5juD1u0qqppy/g+sWT9gj+UhnPfE8Bj Q0zULtKbZcMvPOOe5oKHGVjvlMTrxXvVig5teKxeNvDsWFAcpceQxqkYRIt1V5QZTxuE72B1T4g vnMTxHRDt+6RAUca1hUkmw/ndKBDWASdjVd0LLo0A65cD3h3SfAcy4bEk5P/XwWtidLqkXDbsHf s9lVvEXfhkhSbbhc+n1zmvhYZHns84wUnLDuNYQ+2R1XcikdvuBVJeFJZpW20zs99LH1wDD4kUF iTyWvFf4wlSnqpLwl6wCOk0zQDLZCTuFC12wIhuwXf8dNFVCAn84xwtWrtCFusb2ms/eiHT4+9Z gyylyZ0pKj5rMNdSsgqWg= X-Received: by 2002:a05:7022:6726:b0:133:1ba6:f42d with SMTP id a92af1059eb24-1365f812ee4mr4507328c88.3.1779618342713; Sun, 24 May 2026 03:25:42 -0700 (PDT) Received: from anonyme ([2605:52c0:2:2f27:be24:11ff:fe89:6f0f]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1366aa88cffsm3847216c88.11.2026.05.24.03.25.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 May 2026 03:25:42 -0700 (PDT) From: AnonymeMeow To: jack@suse.cz Cc: amir73il@gmail.com, repnop@google.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, AnonymeMeow Subject: [PATCH] fanotify: report thread pidfds for FAN_REPORT_TID Date: Sun, 24 May 2026 18:24:39 +0800 Message-ID: <20260524102439.44642-1-anonymemeow@gmail.com> X-Mailer: git-send-email 2.54.0 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. This patch allows listeners using FAN_REPORT_PIDFD | FAN_REPORT_TID to receive the pidfd referring to the thread that triggered the event. Signed-off-by: AnonymeMeow --- 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