Skip to contentSkip to conversation

fanotify: lift pidfd reporting restrictions

PATCH In review AnonymeMeow 6 participants 24 messages Jun 7 – Jun 16

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v5 cover fanotify: lift pidfd reporting restrictions

The pidfd API now supports pidfds for tasks that are not thread group leaders, but fanotify has not caught up yet. This patch set lifts that restriction and allows fanotify to report pidfds referring to the event-generating thread.

Additionally, this patch set allows fanotify to hand out pidfds for reaped tasks by registering the event pid with pidfs when pidfd reporting is requested and dropping the pid_has_task() check before the pidfd_prepare() call, as suggested by Christian.

Changes since v4:
- Add a pidfs_register_pid_gfp() helper function to pass in custom flags.

v4: https://lore.kernel.org/lkml/[email protected]/

AnonymeMeow (2):
fanotify: report thread pidfds for FAN_REPORT_TID
fanotify: allow reporting pidfds for reaped tasks

 fs/notify/fanotify/fanotify.c      | 17 +++++++++------
 fs/notify/fanotify/fanotify_user.c | 33 +++++++-----------------------
 fs/pidfs.c                         | 10 +++++----
 include/linux/pidfs.h              | 18 +++++++++++++++-
 4 files changed, 41 insertions(+), 37 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v5 1/2 fanotify: report thread pidfds for FAN_REPORT_TID

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.

---
 fs/notify/fanotify/fanotify_user.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)
fs/notify/fanotify/fanotify_user.c +8 -19
index ae904451dfc0..ebdd48942029 100644
@@ -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)
Signature · 1 line
2.54.0
#2 lore raw

PATCH v5 2/2 fanotify: allow reporting pidfds for reaped tasks

In reply to AnonymeMeow

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.

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.

---
 fs/notify/fanotify/fanotify.c      | 17 +++++++++++------
 fs/notify/fanotify/fanotify_user.c | 18 +++++-------------
 fs/pidfs.c                         | 10 ++++++----
 include/linux/pidfs.h              | 18 +++++++++++++++++-
 4 files changed, 39 insertions(+), 24 deletions(-)
fs/notify/fanotify/fanotify.c +11 -6
index 38290b9c07f7..ece9523e775b 100644
@@ -14,6 +14,7 @@
 #include <linux/sched/mm.h>
 #include <linux/statfs.h>
 #include <linux/stringhash.h>
+#include <linux/pidfs.h>
 
 #include "fanotify.h"
 
@@ -842,6 +843,15 @@ static struct fanotify_event *fanotify_alloc_event(
 	/* Whoever is interested in the event, pays for the allocation. */
 	old_memcg = set_active_memcg(group->memcg);
 
+	if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
+		pid = task_pid(current);
+	else
+		pid = task_tgid(current);
+
+	if (FAN_GROUP_FLAG(group, FAN_REPORT_PIDFD) &&
+	    pidfs_register_pid_gfp(pid, gfp))
+		goto out;
+
 	if (fanotify_is_perm_event(mask)) {
 		event = fanotify_alloc_perm_event(data, data_type, gfp);
 	} else if (fanotify_is_error_event(mask)) {
@@ -863,15 +873,10 @@ static struct fanotify_event *fanotify_alloc_event(
 	if (!event)
 		goto out;
 
-	if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
-		pid = get_pid(task_pid(current));
-	else
-		pid = get_pid(task_tgid(current));
-
 	/* Mix event info, FAN_ONDIR flag and pid into event merge key */
 	hash ^= hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS);
 	fanotify_init_event(event, hash, mask);
-	event->pid = pid;
+	event->pid = get_pid(pid);
 
 out:
 	set_active_memcg(old_memcg);
fs/notify/fanotify/fanotify_user.c +5 -13
index ebdd48942029..b604e3da58ad 100644
@@ -904,20 +904,12 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
 		metadata.fd = fd >= 0 ? fd : FAN_NOFD;
 
 	if (pidfd_mode) {
-		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;
+		unsigned int pidfd_flags = PIDFD_STALE;
 
-		/*
-		 * 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 |= PIDFD_THREAD;
+
+		if (metadata.pid)
 			pidfd = pidfd_prepare(event->pid, pidfd_flags, &pidfd_file);
 
 		if (!FAN_GROUP_FLAG(group, FAN_REPORT_FD_ERROR) && pidfd < 0)
fs/pidfs.c +6 -4
index 1cce4f34a051..15efecf5cb07 100644
@@ -991,14 +991,16 @@ static void pidfs_put_data(void *data)
 }
 
 /**
- * pidfs_register_pid - register a struct pid in pidfs
+ * pidfs_register_pid_gfp - register a struct pid in pidfs with custom GFP
+ * flags
  * @pid: pid to pin
+ * @gfp: GFP flags for memory allocation
  *
- * Register a struct pid in pidfs.
+ * Register a struct pid in pidfs with custom GFP flags.
  *
  * Return: On success zero, on error a negative error code is returned.
  */
-int pidfs_register_pid(struct pid *pid)
+int pidfs_register_pid_gfp(struct pid *pid, gfp_t gfp)
 {
 	struct pidfs_attr *new_attr __free(kfree) = NULL;
 	struct pidfs_attr *attr;
@@ -1014,7 +1016,7 @@ int pidfs_register_pid(struct pid *pid)
 	if (attr)
 		return 0;
 
-	new_attr = kmem_cache_zalloc(pidfs_attr_cachep, GFP_KERNEL);
+	new_attr = kmem_cache_zalloc(pidfs_attr_cachep, gfp);
 	if (!new_attr)
 		return -ENOMEM;
 
include/linux/pidfs.h +17 -1
index 416bdff4d6ce..0abf7da9ab23 100644
@@ -2,6 +2,8 @@
 #ifndef _LINUX_PID_FS_H
 #define _LINUX_PID_FS_H
 
+#include <linux/gfp_types.h>
+
 struct coredump_params;
 
 struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags);
@@ -14,7 +16,21 @@ void pidfs_exit(struct task_struct *tsk);
 void pidfs_coredump(const struct coredump_params *cprm);
 #endif
 extern const struct dentry_operations pidfs_dentry_operations;
-int pidfs_register_pid(struct pid *pid);
+int pidfs_register_pid_gfp(struct pid *pid, gfp_t gfp);
+
+/**
+ * pidfs_register_pid - register a struct pid in pidfs
+ * @pid: pid to pin
+ *
+ * Register a struct pid in pidfs.
+ *
+ * Return: On success zero, on error a negative error code is returned.
+ */
+static inline int pidfs_register_pid(struct pid *pid)
+{
+	return pidfs_register_pid_gfp(pid, GFP_KERNEL);
+}
+
 void pidfs_free_pid(struct pid *pid);
 
 #endif /* _LINUX_PID_FS_H */
Signature · 1 line
2.54.0
maintainer
#3 lore raw
In reply to AnonymeMeow
Quoted 9 lines · AnonymeMeow

The pidfd API now supports pidfds for tasks that are not thread group leaders, but fanotify has not caught up yet. This patch set lifts that restriction and allows fanotify to report pidfds referring to the event-generating thread.

Additionally, this patch set allows fanotify to hand out pidfds for reaped tasks by registering the event pid with pidfs when pidfd reporting is requested and dropping the pid_has_task() check before the pidfd_prepare() call, as suggested by Christian.

Quoted 7 lines

Changes since v4:
- Add a pidfs_register_pid_gfp() helper function to pass in custom flags.

v4: https://lore.kernel.org/lkml/[email protected]/

The patches look good to me. Thanks! I'd prefer the change adding gfp argument to pidfs_register_pid() was a separate commit but unless Christian objects I can live with that. I'll wait for Christian's ack for pidfs changes before merging this through my tree.
Honza

Quoted 13 lines

AnonymeMeow (2):
fanotify: report thread pidfds for FAN_REPORT_TID
fanotify: allow reporting pidfds for reaped tasks

 fs/notify/fanotify/fanotify.c      | 17 +++++++++------
 fs/notify/fanotify/fanotify_user.c | 33 +++++++-----------------------
 fs/pidfs.c                         | 10 +++++----
 include/linux/pidfs.h              | 18 +++++++++++++++-
 4 files changed, 41 insertions(+), 37 deletions(-)
Signature · 1 line
2.54.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw
Quoted 13 lines · Jan Kara
Quoted 12 lines · AnonymeMeow

The pidfd API now supports pidfds for tasks that are not thread group leaders, but fanotify has not caught up yet. This patch set lifts that restriction and allows fanotify to report pidfds referring to the event-generating thread.

Additionally, this patch set allows fanotify to hand out pidfds for reaped tasks by registering the event pid with pidfs when pidfd reporting is requested and dropping the pid_has_task() check before the pidfd_prepare() call, as suggested by Christian.

Quoted 8 lines
Quoted 4 lines

Changes since v4:
- Add a pidfs_register_pid_gfp() helper function to pass in custom flags.

v4: https://lore.kernel.org/lkml/[email protected]/

The patches look good to me. Thanks! I'd prefer the change adding gfp argument to pidfs_register_pid() was a separate commit but unless Christian objects I can live with that. I'll wait for Christian's ack for pidfs

I would also prefer it to be a separate commit but same as you I'm not gonna insist. :)

Quoted 1 line

changes before merging this through my tree.

maintainer
#5 lore raw
Quoted 23 lines · Christian Brauner
Quoted 13 lines · Jan Kara
Quoted 12 lines · AnonymeMeow

The pidfd API now supports pidfds for tasks that are not thread group leaders, but fanotify has not caught up yet. This patch set lifts that restriction and allows fanotify to report pidfds referring to the event-generating thread.

Additionally, this patch set allows fanotify to hand out pidfds for reaped tasks by registering the event pid with pidfs when pidfd reporting is requested and dropping the pid_has_task() check before the pidfd_prepare() call, as suggested by Christian.

Quoted 8 lines
Quoted 4 lines

Changes since v4:
- Add a pidfs_register_pid_gfp() helper function to pass in custom flags.

v4: https://lore.kernel.org/lkml/[email protected]/

The patches look good to me. Thanks! I'd prefer the change adding gfp argument to pidfs_register_pid() was a separate commit but unless Christian objects I can live with that. I'll wait for Christian's ack for pidfs

Quoted 6 lines

I would also prefer it to be a separate commit but same as you I'm not gonna insist. :)

Quoted 1 line

changes before merging this through my tree.

Thanks! Merged and pushed out. AnonymeMeow, what's the situation with fixing up fanotify20 & fanotify21 LTP tests? Are you working on it please? They'll now start failing with linux-next so it would be good to fix that up ASAP... Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#6 lore raw

cover fanotify: update pidfd tests for FAN_REPORT_TID

This patch set updates fanotify pidfd tests for the kernel change that adds FAN_REPORT_PIDFD support together with FAN_REPORT_TID.

---

Sorry for the late reply, I accidentally broke my system last week, so I was busy repairing it. And I may not be able to respond in time due to final exams this week, sorry.

---

AnonymeMeow (5):
fanotify20: Allow FAN_REPORT_PIDFD with FAN_REPORT_TID on v7.2+
fanotify21: Stop relying on exited child for pidfd error
fanotify21: Simplify read_pidfd_fdinfo()
fanotify21: Add test variants for FAN_REPORT_TID
fanotify21: Add FAN_REPORT_TID pidfd coverage

 include/lapi/pidfd.h                          |   4 +
 .../kernel/syscalls/fanotify/fanotify20.c     |  19 +-
 .../kernel/syscalls/fanotify/fanotify21.c     | 191 +++++++++++-------
 3 files changed, 136 insertions(+), 78 deletions(-)
Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

PATCH 1/5 fanotify20: Allow FAN_REPORT_PIDFD with FAN_REPORT_TID on v7.2+

fanotify_init() used to reject FAN_REPORT_PIDFD combined with FAN_REPORT_TID with EINVAL. Since Linux v7.2, fanotify supports reporting pidfds for thread IDs, so this combination is expected to succeed.

Keep expecting EINVAL on older kernels and adjust the test expectation based on the running kernel version.

---
 .../kernel/syscalls/fanotify/fanotify20.c     | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/fanotify/fanotify20.c +13 -6
index b32ecf6aa..9a78f6ff0 100644
@@ -8,13 +8,14 @@
 
 /*\
  * This source file contains a test case which ensures that the
- * :manpage:`fanotify(7)` API returns an expected error code when provided an
- * invalid initialization flag alongside FAN_REPORT_PIDFD. Additionally, it
- * checks that the operability with existing FAN_REPORT_* flags is maintained
- * and functioning as intended.
+ * :manpage:`fanotify(7)` API returns an expected error code when provided
+ * unsupported initialization flags, e.g. FAN_REPORT_PIDFD combined with
+ * FAN_REPORT_TID. Additionally, it checks that the operability with
+ * supported FAN_REPORT_* flags is maintained and functioning as intended.
  *
  * NOTE: FAN_REPORT_PIDFD support was added in v5.15-rc1 in
  * af579beb666a ("fanotify: add pidfd support to the fanotify API").
+ * FAN_REPORT_PIDFD combined with FAN_REPORT_TID is supported since v7.2-rc1.
  */
 
 #define _GNU_SOURCE
@@ -26,8 +27,10 @@
 
 #define MOUNT_PATH	"fs_mnt"
 #define FLAGS_DESC(x) .flags = x, .desc = #x
+#define PIDFD_TID_FLAGS (FAN_REPORT_PIDFD | FAN_REPORT_TID)
 
 static int fd;
+static int thread_pidfd_supported;
 
 static struct test_case_t {
 	unsigned int flags;
@@ -51,17 +54,21 @@ static void do_setup(void)
 	 */
 	REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_PIDFD,
 						    MOUNT_PATH);
+
+	thread_pidfd_supported = tst_kvercmp(7, 2, 0) >= 0;
 }
 
 static void do_test(unsigned int i)
 {
 	struct test_case_t *tc = &test_cases[i];
+	int exp_errno = (tc->flags & PIDFD_TID_FLAGS) == PIDFD_TID_FLAGS &&
+			thread_pidfd_supported ? 0 : tc->exp_errno;
 
-	tst_res(TINFO, "Test %s on %s", tc->exp_errno ? "fail" : "pass",
+	tst_res(TINFO, "Test %s on %s", exp_errno ? "fail" : "pass",
 		tc->desc);
 
 	TST_EXP_FD_OR_FAIL(fd = fanotify_init(tc->flags, O_RDONLY),
-			   tc->exp_errno);
+			   exp_errno);
 
 	if (fd > 0)
 		SAFE_CLOSE(fd);
Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

Re: [LTP] fanotify20: Allow FAN_REPORT_PIDFD with FAN_REPORT_TID on v7.2+

Hi AnonymeMeow,

Quoted 1 line · AnonymeMeow

fanotify20: Allow FAN_REPORT_PIDFD with FAN_REPORT_TID on v7.2+

--- [PATCH 1/5] ---

Quoted 3 lines

fanotify20: Allow FAN_REPORT_PIDFD with FAN_REPORT_TID on v7.2+

+ * FAN_REPORT_PIDFD combined with FAN_REPORT_TID is supported since v7.2-rc1.

The latest stable kernel release is v7.1, so v7.2 has not been released yet. Per LTP convention, tests for features not yet in a mainline kernel release need the [STAGING] subject prefix.

Since the patch adjusts test expectations for the unreleased v7.2 feature, the subject should be prefixed with [STAGING].

--- [PATCH 4/5] ---

Quoted 1 line

fanotify21: Add test variants for FAN_REPORT_TID

Same staging concern here. The new test variants exercise FAN_REPORT_PIDFD combined with FAN_REPORT_TID, which is a v7.2-rc1 feature. This commit should also carry the [STAGING] prefix until v7.2 is released.

--- [PATCH 5/5] ---

Quoted 1 line

fanotify21: Add FAN_REPORT_TID pidfd coverage

Same as above: the thread-based pidfd verification is for the unreleased v7.2 FAN_REPORT_TID support and needs [STAGING].

Verdict - Needs revision

Pre-existing issues (not introduced by this series):

- fanotify20.c: static int fd; is not initialized to -1, and the validity checks use if (fd > 0) instead of if (fd != -1).

- fanotify21.c: static int fanotify_fd; is not initialized to -1, and the cleanup check uses if (fanotify_fd >= 0) instead of if (fanotify_fd != -1).

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
maintainer
#9 lore raw

Re: [LTP] [PATCH 1/5] fanotify20: Allow FAN_REPORT_PIDFD with FAN_REPORT_TID on v7.2+

In reply to AnonymeMeow
Quoted 9 lines · AnonymeMeow

fanotify_init() used to reject FAN_REPORT_PIDFD combined with FAN_REPORT_TID with EINVAL. Since Linux v7.2, fanotify supports reporting pidfds for thread IDs, so this combination is expected to succeed.

Keep expecting EINVAL on older kernels and adjust the test expectation based on the running kernel version.

Thanks for the fixes! Some comment below. And take your time with final exams, that's more important than some failing LTP tests :)

Quoted 45 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +9 -4
index b32ecf6aa..9a78f6ff0 100644
@@ -8,13 +8,14 @@
 
 /*\
  * This source file contains a test case which ensures that the
- * :manpage:`fanotify(7)` API returns an expected error code when provided an
- * invalid initialization flag alongside FAN_REPORT_PIDFD. Additionally, it
- * checks that the operability with existing FAN_REPORT_* flags is maintained
- * and functioning as intended.
+ * :manpage:`fanotify(7)` API returns an expected error code when provided
+ * unsupported initialization flags, e.g. FAN_REPORT_PIDFD combined with
+ * FAN_REPORT_TID. Additionally, it checks that the operability with
+ * supported FAN_REPORT_* flags is maintained and functioning as intended.
  *
  * NOTE: FAN_REPORT_PIDFD support was added in v5.15-rc1 in
  * af579beb666a ("fanotify: add pidfd support to the fanotify API").
+ * FAN_REPORT_PIDFD combined with FAN_REPORT_TID is supported since v7.2-rc1.
  */
 
 #define _GNU_SOURCE
@@ -26,8 +27,10 @@
 
 #define MOUNT_PATH	"fs_mnt"
 #define FLAGS_DESC(x) .flags = x, .desc = #x
+#define PIDFD_TID_FLAGS (FAN_REPORT_PIDFD | FAN_REPORT_TID)
 
 static int fd;
+static int thread_pidfd_supported;
 
 static struct test_case_t {
 	unsigned int flags;
@@ -51,17 +54,21 @@ static void do_setup(void)
 	 */
 	REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_PIDFD,
 						    MOUNT_PATH);
+
+	thread_pidfd_supported = tst_kvercmp(7, 2, 0) >= 0;
 }
 
 static void do_test(unsigned int i)
 {
 	struct test_case_t *tc = &test_cases[i];

testcases/kernel/syscalls/fanotify/fanotify20.c static void do_setup(void) · line 71

Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +4 -2
+	int exp_errno = (tc->flags & PIDFD_TID_FLAGS) == PIDFD_TID_FLAGS &&
+			thread_pidfd_supported ? 0 : tc->exp_errno;
 
-	tst_res(TINFO, "Test %s on %s", tc->exp_errno ? "fail" : "pass",
+	tst_res(TINFO, "Test %s on %s", exp_errno ? "fail" : "pass",
 		tc->desc);
 
 	TST_EXP_FD_OR_FAIL(fd = fanotify_init(tc->flags, O_RDONLY),
-			   tc->exp_errno);
+			   exp_errno);

Hum, this looks somewhat ugly. Ideally we'd initialize test case .exp_errno accordingly to the expected return of the syscall (but that's not really possible because the initializer has to be constant). Checking how other tests handle it, I think we can clone the tests to two. One that expects failure and is skipped for kernels >= 7.2 and one that expects success and is skipped for kernels < 7.2. And we can have in the test description .min_kver and .max_kver fields that would store for which kernel versions the test should be performed.

Honza

Signature · 5 lines
Jan Kara <[email protected]>
SUSE Labs, CR

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

Re: [LTP] [PATCH 1/5] fanotify20: Allow FAN_REPORT_PIDFD with FAN_REPORT_TID on v7.2+

Quoted 21 lines · Jan Kara
Quoted 9 lines · AnonymeMeow

fanotify_init() used to reject FAN_REPORT_PIDFD combined with FAN_REPORT_TID with EINVAL. Since Linux v7.2, fanotify supports reporting pidfds for thread IDs, so this combination is expected to succeed.

Keep expecting EINVAL on older kernels and adjust the test expectation based on the running kernel version.

Thanks for the fixes! Some comment below. And take your time with final exams, that's more important than some failing LTP tests :)

Quoted 6 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +0 -0
index b32ecf6aa..9a78f6ff0 100644
@@ -8,13 +8,14 @@
 
Quoted 14 lines
Quoted 4 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +0 -2
 /*\
  * This source file contains a test case which ensures that the
- * :manpage:`fanotify(7)` API returns an expected error code when provided an
- * invalid initialization flag alongside FAN_REPORT_PIDFD. Additionally, it

testcases/kernel/syscalls/fanotify/fanotify20.c line 18

Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +5 -2
- * checks that the operability with existing FAN_REPORT_* flags is maintained
- * and functioning as intended.
+ * :manpage:`fanotify(7)` API returns an expected error code when provided
+ * unsupported initialization flags, e.g. FAN_REPORT_PIDFD combined with
+ * FAN_REPORT_TID. Additionally, it checks that the operability with
+ * supported FAN_REPORT_* flags is maintained and functioning as intended.
  *
  * NOTE: FAN_REPORT_PIDFD support was added in v5.15-rc1 in
  * af579beb666a ("fanotify: add pidfd support to the fanotify API").
+ * FAN_REPORT_PIDFD combined with FAN_REPORT_TID is supported since v7.2-rc1.

Can add the upstream commit here.

Quoted 32 lines
Quoted 32 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +7 -1
  */
 
 #define _GNU_SOURCE
@@ -26,8 +27,10 @@
 
 #define MOUNT_PATH   "fs_mnt"
 #define FLAGS_DESC(x) .flags = x, .desc = #x
+#define PIDFD_TID_FLAGS (FAN_REPORT_PIDFD | FAN_REPORT_TID)
 
 static int fd;
+static int thread_pidfd_supported;
 
 static struct test_case_t {
      unsigned int flags;
@@ -51,17 +54,21 @@ static void do_setup(void)
       */
      REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_PIDFD,
                                                  MOUNT_PATH);
+
+     thread_pidfd_supported = tst_kvercmp(7, 2, 0) >= 0;
 }
 
 static void do_test(unsigned int i)
 {
      struct test_case_t *tc = &test_cases[i];
+     int exp_errno = (tc->flags & PIDFD_TID_FLAGS) == PIDFD_TID_FLAGS &&
+                     thread_pidfd_supported ? 0 : tc->exp_errno;
 
-     tst_res(TINFO, "Test %s on %s", tc->exp_errno ? "fail" : "pass",
+     tst_res(TINFO, "Test %s on %s", exp_errno ? "fail" : "pass",
              tc->desc);
 
Quoted 12 lines

testcases/kernel/syscalls/fanotify/fanotify20.c static void do_setup(void) · line 71

Quoted 3 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +1 -1
      TST_EXP_FD_OR_FAIL(fd = fanotify_init(tc->flags, O_RDONLY),
-                        tc->exp_errno);
+                        exp_errno);

Hum, this looks somewhat ugly. Ideally we'd initialize test case .exp_errno accordingly to the expected return of the syscall (but that's not really possible because the initializer has to be constant). Checking how other tests handle it, I think we can clone the tests to two. One that expects failure and is skipped for kernels >= 7.2 and one that expects success and is skipped for kernels < 7.2. And we can have in the test description .min_kver and .max_kver fields that would store for which kernel versions the test should be performed.

LOL there is nothing to test in the >= 7.2 test.

The test that these flag combination are allowed:
FLAGS_DESC(FAN_REPORT_PIDFD | FAN_REPORT_TID), FLAGS_DESC(FAN_REPORT_PIDFD | FAN_REPORT_FID |
FAN_REPORT_DFID_NAME),

Is already "tested" by the tests that use them. and we don't have any tests that >= 5.10 has support for FAN_MARK_FILESYSTEM etc.

Easiest is to just skip fanotify20 for version >= 7.2

Thanks,
Amir.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
maintainer
#11 lore raw

Re: [LTP] [PATCH 1/5] fanotify20: Allow FAN_REPORT_PIDFD with FAN_REPORT_TID on v7.2+

Quoted 91 lines · Amir Goldstein
Quoted 21 lines · Jan Kara
Quoted 9 lines · AnonymeMeow

fanotify_init() used to reject FAN_REPORT_PIDFD combined with FAN_REPORT_TID with EINVAL. Since Linux v7.2, fanotify supports reporting pidfds for thread IDs, so this combination is expected to succeed.

Keep expecting EINVAL on older kernels and adjust the test expectation based on the running kernel version.

Thanks for the fixes! Some comment below. And take your time with final exams, that's more important than some failing LTP tests :)

Quoted 6 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +0 -0
index b32ecf6aa..9a78f6ff0 100644
@@ -8,13 +8,14 @@
 
Quoted 14 lines
Quoted 4 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +0 -2
 /*\
  * This source file contains a test case which ensures that the
- * :manpage:`fanotify(7)` API returns an expected error code when provided an
- * invalid initialization flag alongside FAN_REPORT_PIDFD. Additionally, it

testcases/kernel/syscalls/fanotify/fanotify20.c line 18

Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +5 -2
- * checks that the operability with existing FAN_REPORT_* flags is maintained
- * and functioning as intended.
+ * :manpage:`fanotify(7)` API returns an expected error code when provided
+ * unsupported initialization flags, e.g. FAN_REPORT_PIDFD combined with
+ * FAN_REPORT_TID. Additionally, it checks that the operability with
+ * supported FAN_REPORT_* flags is maintained and functioning as intended.
  *
  * NOTE: FAN_REPORT_PIDFD support was added in v5.15-rc1 in
  * af579beb666a ("fanotify: add pidfd support to the fanotify API").
+ * FAN_REPORT_PIDFD combined with FAN_REPORT_TID is supported since v7.2-rc1.

Can add the upstream commit here.

Quoted 32 lines
Quoted 32 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +7 -1
  */
 
 #define _GNU_SOURCE
@@ -26,8 +27,10 @@
 
 #define MOUNT_PATH   "fs_mnt"
 #define FLAGS_DESC(x) .flags = x, .desc = #x
+#define PIDFD_TID_FLAGS (FAN_REPORT_PIDFD | FAN_REPORT_TID)
 
 static int fd;
+static int thread_pidfd_supported;
 
 static struct test_case_t {
      unsigned int flags;
@@ -51,17 +54,21 @@ static void do_setup(void)
       */
      REQUIRE_FANOTIFY_INIT_FLAGS_SUPPORTED_ON_FS(FAN_REPORT_PIDFD,
                                                  MOUNT_PATH);
+
+     thread_pidfd_supported = tst_kvercmp(7, 2, 0) >= 0;
 }
 
 static void do_test(unsigned int i)
 {
      struct test_case_t *tc = &test_cases[i];
+     int exp_errno = (tc->flags & PIDFD_TID_FLAGS) == PIDFD_TID_FLAGS &&
+                     thread_pidfd_supported ? 0 : tc->exp_errno;
 
-     tst_res(TINFO, "Test %s on %s", tc->exp_errno ? "fail" : "pass",
+     tst_res(TINFO, "Test %s on %s", exp_errno ? "fail" : "pass",
              tc->desc);
 
Quoted 12 lines

testcases/kernel/syscalls/fanotify/fanotify20.c static void do_setup(void) · line 71

Quoted 3 lines
testcases/kernel/syscalls/fanotify/fanotify20.c +1 -1
      TST_EXP_FD_OR_FAIL(fd = fanotify_init(tc->flags, O_RDONLY),
-                        tc->exp_errno);
+                        exp_errno);

Hum, this looks somewhat ugly. Ideally we'd initialize test case .exp_errno accordingly to the expected return of the syscall (but that's not really possible because the initializer has to be constant). Checking how other tests handle it, I think we can clone the tests to two. One that expects failure and is skipped for kernels >= 7.2 and one that expects success and is skipped for kernels < 7.2. And we can have in the test description .min_kver and .max_kver fields that would store for which kernel versions the test should be performed.

LOL there is nothing to test in the >= 7.2 test.

The test that these flag combination are allowed:
FLAGS_DESC(FAN_REPORT_PIDFD | FAN_REPORT_TID), FLAGS_DESC(FAN_REPORT_PIDFD | FAN_REPORT_FID |
FAN_REPORT_DFID_NAME),

Quoted 5 lines

Is already "tested" by the tests that use them. and we don't have any tests that >= 5.10 has support for FAN_MARK_FILESYSTEM etc.

Easiest is to just skip fanotify20 for version >= 7.2

Right, I probably overengineered this :) Just skip the test for newer kernel versions.

Honza

Signature · 5 lines
Jan Kara <[email protected]>
SUSE Labs, CR

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw

PATCH 2/5 fanotify21: Stop relying on exited child for pidfd error

In reply to AnonymeMeow

fanotify21 used to verify the pidfd error case by generating an event from a child process and reading the event after the child had exited. This is no longer a stable way to trigger a pidfd error, because newer kernels can report pidfds for exited event processes.

Read the event from a child process in a descendant PID namespace instead. The reader cannot get the pidfd in the parent PID namespace from fanotify, so pidfd creation will fail and the test can verify FAN_NOPIDFD, or -ESRCH with FAN_REPORT_FD_ERROR.

---
 .../kernel/syscalls/fanotify/fanotify21.c     | 55 ++++++++-----------
 1 file changed, 24 insertions(+), 31 deletions(-)
testcases/kernel/syscalls/fanotify/fanotify21.c +24 -31
index 4d2115469..bd1a43513 100644
@@ -42,7 +42,6 @@ struct pidfd_fdinfo_t {
 
 static struct test_case_t {
 	char *name;
-	int fork;
 	int want_pidfd_err;
 	int remount_ro;
 } test_cases[] = {
@@ -50,18 +49,15 @@ static struct test_case_t {
 		"return a valid pidfd for event created by self",
 		0,
 		0,
-		0,
 	},
 	{
-		"return invalid pidfd for event created by terminated child",
-		1,
+		"return invalid pidfd for reader in descendant PID namespace",
 		1,
 		0,
 	},
 	{
 		"fail to open rw fd for event created on read-only mount",
 		0,
-		0,
 		1,
 	},
 };
@@ -72,6 +68,11 @@ static struct pidfd_fdinfo_t *self_pidfd_fdinfo;
 
 static int fd_error_unsupported;
 
+static struct tst_clone_args clone_args = {
+	.flags = CLONE_NEWPID,
+	.exit_signal = SIGCHLD,
+};
+
 static struct pidfd_fdinfo_t *read_pidfd_fdinfo(int pidfd)
 {
 	char *fdinfo_path;
@@ -100,24 +101,6 @@ static void generate_event(void)
 	SAFE_CLOSE(fd);
 }
 
-static void do_fork(void)
-{
-	int status;
-	pid_t child;
-
-	child = SAFE_FORK();
-	if (child == 0) {
-		SAFE_CLOSE(fanotify_fd);
-		generate_event();
-		exit(EXIT_SUCCESS);
-	}
-
-	SAFE_WAITPID(child, &status, 0);
-	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
-		tst_brk(TBROK,
-			"child process terminated incorrectly");
-}
-
 static void do_setup(void)
 {
 	int pidfd;
@@ -165,6 +148,8 @@ static void do_test(unsigned int num)
 	int nopidfd_err = tc->want_pidfd_err ?
 			  (tst_variant ? -ESRCH : FAN_NOPIDFD) : 0;
 	int fd_err = (tc->remount_ro && tst_variant) ? -EROFS : 0;
+	pid_t reader_pid;
+	int reader_exit_status;
 
 	tst_res(TINFO, "Test #%d.%d: %s %s", num, tst_variant, tc->name,
 			tst_variant ? "(FAN_REPORT_FD_ERROR)" : "");
@@ -178,15 +163,20 @@ static void do_test(unsigned int num)
 	SAFE_MOUNT("none", MOUNT_PATH, "none", MS_BIND | MS_REMOUNT |
 		   (tc->remount_ro ? MS_RDONLY : 0), NULL);
 
+	generate_event();
+
 	/*
-	 * Generate the event in either self or a child process. Event
-	 * generation in a child process is done so that the FAN_NOPIDFD case
-	 * can be verified.
+	 * Read the event in either self or a child process in a descendant
+	 * PID namespace. A reader in a descendant PID namespace cannot obtain
+	 * the pidfd of the event process in the parent PID namespace so that
+	 * the FAN_NOPIDFD case can be verified.
 	 */
-	if (tc->fork)
-		do_fork();
-	else
-		generate_event();
+	if (tc->want_pidfd_err && (reader_pid = SAFE_CLONE(&clone_args))) {
+		SAFE_WAITPID(reader_pid, &reader_exit_status, 0);
+		if (!WIFEXITED(reader_exit_status) || WEXITSTATUS(reader_exit_status))
+			tst_brk(TBROK, "reader process exited incorrectly");
+		return;
+	}
 
 	/*
 	 * Read all of the queued events into the provided event
@@ -274,7 +264,7 @@ static void do_test(unsigned int num)
 			goto next_event;
 		} else if (tc->want_pidfd_err && info->pidfd == nopidfd_err) {
 			tst_res(TPASS,
-				"pid: %u terminated before pidfd was created, "
+				"pid: %u is invisible in reader PID namespace, "
 				"pidfd set to the value of: %d, as expected",
 				(unsigned int)event->pid,
 				nopidfd_err);
@@ -343,6 +333,9 @@ next_event:
 		if (event_pidfd_fdinfo)
 			free(event_pidfd_fdinfo);
 	}
+
+	if (tc->want_pidfd_err)
+		exit(0);
 }
 
 static void do_cleanup(void)
Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
maintainer Reviewed-by on v1 2/5
#13 lore raw

Re: [LTP] [PATCH 2/5] fanotify21: Stop relying on exited child for pidfd error

Quoted 11 lines · AnonymeMeow

fanotify21 used to verify the pidfd error case by generating an event from a child process and reading the event after the child had exited. This is no longer a stable way to trigger a pidfd error, because newer kernels can report pidfds for exited event processes.

Read the event from a child process in a descendant PID namespace instead. The reader cannot get the pidfd in the parent PID namespace from fanotify, so pidfd creation will fail and the test can verify FAN_NOPIDFD, or -ESRCH with FAN_REPORT_FD_ERROR.

Looks good. Feel free to add:

Honza

Quoted 132 lines
---
 .../kernel/syscalls/fanotify/fanotify21.c     | 55 ++++++++-----------
 1 file changed, 24 insertions(+), 31 deletions(-)
testcases/kernel/syscalls/fanotify/fanotify21.c +24 -31
index 4d2115469..bd1a43513 100644
@@ -42,7 +42,6 @@ struct pidfd_fdinfo_t {
 
 static struct test_case_t {
 	char *name;
-	int fork;
 	int want_pidfd_err;
 	int remount_ro;
 } test_cases[] = {
@@ -50,18 +49,15 @@ static struct test_case_t {
 		"return a valid pidfd for event created by self",
 		0,
 		0,
-		0,
 	},
 	{
-		"return invalid pidfd for event created by terminated child",
-		1,
+		"return invalid pidfd for reader in descendant PID namespace",
 		1,
 		0,
 	},
 	{
 		"fail to open rw fd for event created on read-only mount",
 		0,
-		0,
 		1,
 	},
 };
@@ -72,6 +68,11 @@ static struct pidfd_fdinfo_t *self_pidfd_fdinfo;
 
 static int fd_error_unsupported;
 
+static struct tst_clone_args clone_args = {
+	.flags = CLONE_NEWPID,
+	.exit_signal = SIGCHLD,
+};
+
 static struct pidfd_fdinfo_t *read_pidfd_fdinfo(int pidfd)
 {
 	char *fdinfo_path;
@@ -100,24 +101,6 @@ static void generate_event(void)
 	SAFE_CLOSE(fd);
 }
 
-static void do_fork(void)
-{
-	int status;
-	pid_t child;
-
-	child = SAFE_FORK();
-	if (child == 0) {
-		SAFE_CLOSE(fanotify_fd);
-		generate_event();
-		exit(EXIT_SUCCESS);
-	}
-
-	SAFE_WAITPID(child, &status, 0);
-	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
-		tst_brk(TBROK,
-			"child process terminated incorrectly");
-}
-
 static void do_setup(void)
 {
 	int pidfd;
@@ -165,6 +148,8 @@ static void do_test(unsigned int num)
 	int nopidfd_err = tc->want_pidfd_err ?
 			  (tst_variant ? -ESRCH : FAN_NOPIDFD) : 0;
 	int fd_err = (tc->remount_ro && tst_variant) ? -EROFS : 0;
+	pid_t reader_pid;
+	int reader_exit_status;
 
 	tst_res(TINFO, "Test #%d.%d: %s %s", num, tst_variant, tc->name,
 			tst_variant ? "(FAN_REPORT_FD_ERROR)" : "");
@@ -178,15 +163,20 @@ static void do_test(unsigned int num)
 	SAFE_MOUNT("none", MOUNT_PATH, "none", MS_BIND | MS_REMOUNT |
 		   (tc->remount_ro ? MS_RDONLY : 0), NULL);
 
+	generate_event();
+
 	/*
-	 * Generate the event in either self or a child process. Event
-	 * generation in a child process is done so that the FAN_NOPIDFD case
-	 * can be verified.
+	 * Read the event in either self or a child process in a descendant
+	 * PID namespace. A reader in a descendant PID namespace cannot obtain
+	 * the pidfd of the event process in the parent PID namespace so that
+	 * the FAN_NOPIDFD case can be verified.
 	 */
-	if (tc->fork)
-		do_fork();
-	else
-		generate_event();
+	if (tc->want_pidfd_err && (reader_pid = SAFE_CLONE(&clone_args))) {
+		SAFE_WAITPID(reader_pid, &reader_exit_status, 0);
+		if (!WIFEXITED(reader_exit_status) || WEXITSTATUS(reader_exit_status))
+			tst_brk(TBROK, "reader process exited incorrectly");
+		return;
+	}
 
 	/*
 	 * Read all of the queued events into the provided event
@@ -274,7 +264,7 @@ static void do_test(unsigned int num)
 			goto next_event;
 		} else if (tc->want_pidfd_err && info->pidfd == nopidfd_err) {
 			tst_res(TPASS,
-				"pid: %u terminated before pidfd was created, "
+				"pid: %u is invisible in reader PID namespace, "
 				"pidfd set to the value of: %d, as expected",
 				(unsigned int)event->pid,
 				nopidfd_err);
@@ -343,6 +333,9 @@ next_event:
 		if (event_pidfd_fdinfo)
 			free(event_pidfd_fdinfo);
 	}
+
+	if (tc->want_pidfd_err)
+		exit(0);
 }
 
 static void do_cleanup(void)
Signature · 1 line
2.54.0
Signature · 5 lines
Jan Kara <[email protected]>
SUSE Labs, CR

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#14 lore raw

PATCH 3/5 fanotify21: Simplify read_pidfd_fdinfo()

In reply to AnonymeMeow

Let callers provide storage for struct pidfd_fdinfo_t instead of allocating it in read_pidfd_fdinfo() every time. This removes unnecessary allocation/free paths and the NULL checks after read_pidfd_fdinfo() and simplifies the code.

---
 .../kernel/syscalls/fanotify/fanotify21.c     | 61 ++++++-------------
 1 file changed, 20 insertions(+), 41 deletions(-)
testcases/kernel/syscalls/fanotify/fanotify21.c +20 -41
index bd1a43513..22f9767db 100644
@@ -64,7 +64,7 @@ static struct test_case_t {
 
 static int fanotify_fd;
 static char event_buf[BUF_SZ];
-static struct pidfd_fdinfo_t *self_pidfd_fdinfo;
+static struct pidfd_fdinfo_t exp_pidfd_fdinfo;
 
 static int fd_error_unsupported;
 
@@ -73,12 +73,9 @@ static struct tst_clone_args clone_args = {
 	.exit_signal = SIGCHLD,
 };
 
-static struct pidfd_fdinfo_t *read_pidfd_fdinfo(int pidfd)
+static void read_pidfd_fdinfo(int pidfd, struct pidfd_fdinfo_t *pidfd_fdinfo)
 {
 	char *fdinfo_path;
-	struct pidfd_fdinfo_t *pidfd_fdinfo;
-
-	pidfd_fdinfo = SAFE_MALLOC(sizeof(struct pidfd_fdinfo_t));
 
 	SAFE_ASPRINTF(&fdinfo_path, "/proc/self/fdinfo/%d", pidfd);
 	SAFE_FILE_LINES_SCANF(fdinfo_path, "pos: %d", &pidfd_fdinfo->pos);
@@ -88,8 +85,6 @@ static struct pidfd_fdinfo_t *read_pidfd_fdinfo(int pidfd)
 	SAFE_FILE_LINES_SCANF(fdinfo_path, "NSpid: %d", &pidfd_fdinfo->ns_pid);
 
 	free(fdinfo_path);
-
-	return pidfd_fdinfo;
 }
 
 static void generate_event(void)
@@ -133,12 +128,9 @@ static void do_setup(void)
 
 	pidfd = SAFE_PIDFD_OPEN(getpid(), 0);
 
-	self_pidfd_fdinfo = read_pidfd_fdinfo(pidfd);
-	if (self_pidfd_fdinfo == NULL) {
-		tst_brk(TBROK,
-			"pidfd=%d, failed to read pidfd fdinfo",
-			pidfd);
-	}
+	read_pidfd_fdinfo(pidfd, &exp_pidfd_fdinfo);
+
+	SAFE_CLOSE(pidfd);
 }
 
 static void do_test(unsigned int num)
@@ -195,7 +187,7 @@ static void do_test(unsigned int num)
 	while (i < len) {
 		struct fanotify_event_metadata *event;
 		struct fanotify_event_info_pidfd *info;
-		struct pidfd_fdinfo_t *event_pidfd_fdinfo = NULL;
+		struct pidfd_fdinfo_t event_pidfd_fdinfo;
 
 		event = (struct fanotify_event_metadata *)&event_buf[i];
 		info = (struct fanotify_event_info_pidfd *)(event + 1);
@@ -275,39 +267,32 @@ static void do_test(unsigned int num)
 		 * No pidfd errors occurred, continue with verifying pidfd
 		 * fdinfo validity.
 		 */
-		event_pidfd_fdinfo = read_pidfd_fdinfo(info->pidfd);
-		if (event_pidfd_fdinfo == NULL) {
-			tst_brk(TBROK,
-				"reading fdinfo for pidfd: %d "
-				"describing pid: %u failed",
-				info->pidfd,
-				(unsigned int)event->pid);
-			goto next_event;
-		} else if (event_pidfd_fdinfo->pid != event->pid) {
+		read_pidfd_fdinfo(info->pidfd, &event_pidfd_fdinfo);
+		if (event_pidfd_fdinfo.pid != event->pid) {
 			tst_res(TFAIL,
 				"pidfd provided for incorrect pid "
 				"(expected pidfd for pid: %u, got pidfd for "
 				"pid: %u)",
 				(unsigned int)event->pid,
-				(unsigned int)event_pidfd_fdinfo->pid);
+				(unsigned int)event_pidfd_fdinfo.pid);
 			goto next_event;
-		} else if (memcmp(event_pidfd_fdinfo, self_pidfd_fdinfo,
+		} else if (memcmp(&event_pidfd_fdinfo, &exp_pidfd_fdinfo,
 				  sizeof(struct pidfd_fdinfo_t))) {
 			tst_res(TFAIL,
 				"pidfd fdinfo values for self and event differ "
 				"(expected pos: %d, flags: %x, mnt_id: %d, "
 				"pid: %d, ns_pid: %d, got pos: %d, "
 				"flags: %x, mnt_id: %d, pid: %d, ns_pid: %d",
-				self_pidfd_fdinfo->pos,
-				self_pidfd_fdinfo->flags,
-				self_pidfd_fdinfo->mnt_id,
-				self_pidfd_fdinfo->pid,
-				self_pidfd_fdinfo->ns_pid,
-				event_pidfd_fdinfo->pos,
-				event_pidfd_fdinfo->flags,
-				event_pidfd_fdinfo->mnt_id,
-				event_pidfd_fdinfo->pid,
-				event_pidfd_fdinfo->ns_pid);
+				exp_pidfd_fdinfo.pos,
+				exp_pidfd_fdinfo.flags,
+				exp_pidfd_fdinfo.mnt_id,
+				exp_pidfd_fdinfo.pid,
+				exp_pidfd_fdinfo.ns_pid,
+				event_pidfd_fdinfo.pos,
+				event_pidfd_fdinfo.flags,
+				event_pidfd_fdinfo.mnt_id,
+				event_pidfd_fdinfo.pid,
+				event_pidfd_fdinfo.ns_pid);
 			goto next_event;
 		} else {
 			tst_res(TPASS,
@@ -329,9 +314,6 @@ next_event:
 
 		if (info && info->pidfd >= 0)
 			SAFE_CLOSE(info->pidfd);
-
-		if (event_pidfd_fdinfo)
-			free(event_pidfd_fdinfo);
 	}
 
 	if (tc->want_pidfd_err)
@@ -343,9 +325,6 @@ static void do_cleanup(void)
 	if (fanotify_fd >= 0)
 		SAFE_CLOSE(fanotify_fd);
 
-	if (self_pidfd_fdinfo)
-		free(self_pidfd_fdinfo);
-
 	/* Unmount the bind mount */
 	SAFE_UMOUNT(MOUNT_PATH);
 }
Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
maintainer Reviewed-by on v1 3/5
#15 lore raw

Re: [LTP] [PATCH 3/5] fanotify21: Simplify read_pidfd_fdinfo()

Quoted 6 lines · AnonymeMeow

Let callers provide storage for struct pidfd_fdinfo_t instead of allocating it in read_pidfd_fdinfo() every time. This removes unnecessary allocation/free paths and the NULL checks after read_pidfd_fdinfo() and simplifies the code.

Looks good. Feel free to add:

Honza

Quoted 142 lines
---
 .../kernel/syscalls/fanotify/fanotify21.c     | 61 ++++++-------------
 1 file changed, 20 insertions(+), 41 deletions(-)
testcases/kernel/syscalls/fanotify/fanotify21.c +20 -41
index bd1a43513..22f9767db 100644
@@ -64,7 +64,7 @@ static struct test_case_t {
 
 static int fanotify_fd;
 static char event_buf[BUF_SZ];
-static struct pidfd_fdinfo_t *self_pidfd_fdinfo;
+static struct pidfd_fdinfo_t exp_pidfd_fdinfo;
 
 static int fd_error_unsupported;
 
@@ -73,12 +73,9 @@ static struct tst_clone_args clone_args = {
 	.exit_signal = SIGCHLD,
 };
 
-static struct pidfd_fdinfo_t *read_pidfd_fdinfo(int pidfd)
+static void read_pidfd_fdinfo(int pidfd, struct pidfd_fdinfo_t *pidfd_fdinfo)
 {
 	char *fdinfo_path;
-	struct pidfd_fdinfo_t *pidfd_fdinfo;
-
-	pidfd_fdinfo = SAFE_MALLOC(sizeof(struct pidfd_fdinfo_t));
 
 	SAFE_ASPRINTF(&fdinfo_path, "/proc/self/fdinfo/%d", pidfd);
 	SAFE_FILE_LINES_SCANF(fdinfo_path, "pos: %d", &pidfd_fdinfo->pos);
@@ -88,8 +85,6 @@ static struct pidfd_fdinfo_t *read_pidfd_fdinfo(int pidfd)
 	SAFE_FILE_LINES_SCANF(fdinfo_path, "NSpid: %d", &pidfd_fdinfo->ns_pid);
 
 	free(fdinfo_path);
-
-	return pidfd_fdinfo;
 }
 
 static void generate_event(void)
@@ -133,12 +128,9 @@ static void do_setup(void)
 
 	pidfd = SAFE_PIDFD_OPEN(getpid(), 0);
 
-	self_pidfd_fdinfo = read_pidfd_fdinfo(pidfd);
-	if (self_pidfd_fdinfo == NULL) {
-		tst_brk(TBROK,
-			"pidfd=%d, failed to read pidfd fdinfo",
-			pidfd);
-	}
+	read_pidfd_fdinfo(pidfd, &exp_pidfd_fdinfo);
+
+	SAFE_CLOSE(pidfd);
 }
 
 static void do_test(unsigned int num)
@@ -195,7 +187,7 @@ static void do_test(unsigned int num)
 	while (i < len) {
 		struct fanotify_event_metadata *event;
 		struct fanotify_event_info_pidfd *info;
-		struct pidfd_fdinfo_t *event_pidfd_fdinfo = NULL;
+		struct pidfd_fdinfo_t event_pidfd_fdinfo;
 
 		event = (struct fanotify_event_metadata *)&event_buf[i];
 		info = (struct fanotify_event_info_pidfd *)(event + 1);
@@ -275,39 +267,32 @@ static void do_test(unsigned int num)
 		 * No pidfd errors occurred, continue with verifying pidfd
 		 * fdinfo validity.
 		 */
-		event_pidfd_fdinfo = read_pidfd_fdinfo(info->pidfd);
-		if (event_pidfd_fdinfo == NULL) {
-			tst_brk(TBROK,
-				"reading fdinfo for pidfd: %d "
-				"describing pid: %u failed",
-				info->pidfd,
-				(unsigned int)event->pid);
-			goto next_event;
-		} else if (event_pidfd_fdinfo->pid != event->pid) {
+		read_pidfd_fdinfo(info->pidfd, &event_pidfd_fdinfo);
+		if (event_pidfd_fdinfo.pid != event->pid) {
 			tst_res(TFAIL,
 				"pidfd provided for incorrect pid "
 				"(expected pidfd for pid: %u, got pidfd for "
 				"pid: %u)",
 				(unsigned int)event->pid,
-				(unsigned int)event_pidfd_fdinfo->pid);
+				(unsigned int)event_pidfd_fdinfo.pid);
 			goto next_event;
-		} else if (memcmp(event_pidfd_fdinfo, self_pidfd_fdinfo,
+		} else if (memcmp(&event_pidfd_fdinfo, &exp_pidfd_fdinfo,
 				  sizeof(struct pidfd_fdinfo_t))) {
 			tst_res(TFAIL,
 				"pidfd fdinfo values for self and event differ "
 				"(expected pos: %d, flags: %x, mnt_id: %d, "
 				"pid: %d, ns_pid: %d, got pos: %d, "
 				"flags: %x, mnt_id: %d, pid: %d, ns_pid: %d",
-				self_pidfd_fdinfo->pos,
-				self_pidfd_fdinfo->flags,
-				self_pidfd_fdinfo->mnt_id,
-				self_pidfd_fdinfo->pid,
-				self_pidfd_fdinfo->ns_pid,
-				event_pidfd_fdinfo->pos,
-				event_pidfd_fdinfo->flags,
-				event_pidfd_fdinfo->mnt_id,
-				event_pidfd_fdinfo->pid,
-				event_pidfd_fdinfo->ns_pid);
+				exp_pidfd_fdinfo.pos,
+				exp_pidfd_fdinfo.flags,
+				exp_pidfd_fdinfo.mnt_id,
+				exp_pidfd_fdinfo.pid,
+				exp_pidfd_fdinfo.ns_pid,
+				event_pidfd_fdinfo.pos,
+				event_pidfd_fdinfo.flags,
+				event_pidfd_fdinfo.mnt_id,
+				event_pidfd_fdinfo.pid,
+				event_pidfd_fdinfo.ns_pid);
 			goto next_event;
 		} else {
 			tst_res(TPASS,
@@ -329,9 +314,6 @@ next_event:
 
 		if (info && info->pidfd >= 0)
 			SAFE_CLOSE(info->pidfd);
-
-		if (event_pidfd_fdinfo)
-			free(event_pidfd_fdinfo);
 	}
 
 	if (tc->want_pidfd_err)
@@ -343,9 +325,6 @@ static void do_cleanup(void)
 	if (fanotify_fd >= 0)
 		SAFE_CLOSE(fanotify_fd);
 
-	if (self_pidfd_fdinfo)
-		free(self_pidfd_fdinfo);
-
 	/* Unmount the bind mount */
 	SAFE_UMOUNT(MOUNT_PATH);
 }
Signature · 1 line
2.54.0
Signature · 5 lines
Jan Kara <[email protected]>
SUSE Labs, CR

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#16 lore raw

PATCH 4/5 fanotify21: Add test variants for FAN_REPORT_TID

In reply to AnonymeMeow

Add test variants for FAN_REPORT_PIDFD combined with FAN_REPORT_TID, both with and without FAN_REPORT_FD_ERROR. Add PIDFD_THREAD fallback definition for old headers.

---
 include/lapi/pidfd.h                          |  4 ++
 .../kernel/syscalls/fanotify/fanotify21.c     | 56 ++++++++++++++-----
 2 files changed, 47 insertions(+), 13 deletions(-)
include/lapi/pidfd.h +4 -0
index a3205032c..eb4e3466d 100644
@@ -43,6 +43,10 @@ struct pidfd_info {
 # define PIDFD_NONBLOCK O_NONBLOCK
 #endif
 
+#ifndef PIDFD_THREAD
+# define PIDFD_THREAD O_EXCL
+#endif
+
 #ifndef PIDFS_IOCTL_MAGIC
 # define PIDFS_IOCTL_MAGIC	0xFF
 #endif
testcases/kernel/syscalls/fanotify/fanotify21.c +43 -13

126 lines not on this page · raw message

Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
maintainer Reviewed-by on v1 4/5
#17 lore raw

Re: [LTP] [PATCH 4/5] fanotify21: Add test variants for FAN_REPORT_TID

Quoted 5 lines · AnonymeMeow

Add test variants for FAN_REPORT_PIDFD combined with FAN_REPORT_TID, both with and without FAN_REPORT_FD_ERROR. Add PIDFD_THREAD fallback definition for old headers.

Looks good. Feel free to add:

Honza

Quoted 161 lines
---
 include/lapi/pidfd.h                          |  4 ++
 .../kernel/syscalls/fanotify/fanotify21.c     | 56 ++++++++++++++-----
 2 files changed, 47 insertions(+), 13 deletions(-)
include/lapi/pidfd.h +4 -0
index a3205032c..eb4e3466d 100644
@@ -43,6 +43,10 @@ struct pidfd_info {
 # define PIDFD_NONBLOCK O_NONBLOCK
 #endif
 
+#ifndef PIDFD_THREAD
+# define PIDFD_THREAD O_EXCL
+#endif
+
 #ifndef PIDFS_IOCTL_MAGIC
 # define PIDFS_IOCTL_MAGIC	0xFF
 #endif
testcases/kernel/syscalls/fanotify/fanotify21.c +43 -13
index 22f9767db..fd43c8c1c 100644
@@ -12,6 +12,7 @@
  *
  * NOTE: FAN_REPORT_PIDFD support was added in v5.15-rc1 in
  * af579beb666a ("fanotify: add pidfd support to the fanotify API").
+ * FAN_REPORT_PIDFD combined with FAN_REPORT_TID is supported since v7.2-rc1.
  */
 
 #define _GNU_SOURCE
@@ -32,6 +33,9 @@
 #define MOUNT_PATH	"fs_mnt"
 #define TEST_FILE	MOUNT_PATH "/testfile"
 
+#define TST_VARIANT_FD_ERROR (tst_variant & 1)
+#define TST_VARIANT_PIDFD_THREAD (tst_variant & 2)
+
 struct pidfd_fdinfo_t {
 	int pos;
 	int flags;
@@ -67,6 +71,7 @@ static char event_buf[BUF_SZ];
 static struct pidfd_fdinfo_t exp_pidfd_fdinfo;
 
 static int fd_error_unsupported;
+static int thread_pidfd_unsupported;
 
 static struct tst_clone_args clone_args = {
 	.flags = CLONE_NEWPID,
@@ -90,21 +95,37 @@ static void read_pidfd_fdinfo(int pidfd, struct pidfd_fdinfo_t *pidfd_fdinfo)
 static void generate_event(void)
 {
 	int fd;
+	int pidfd;
+
+	pidfd = TST_VARIANT_PIDFD_THREAD ? SAFE_PIDFD_OPEN(tst_gettid(), PIDFD_THREAD)
+					 : SAFE_PIDFD_OPEN(tst_getpid(), 0);
+	read_pidfd_fdinfo(pidfd, &exp_pidfd_fdinfo);
+	SAFE_CLOSE(pidfd);
 
 	/* Generate a single FAN_OPEN event on the watched object. */
 	fd = SAFE_OPEN(TEST_FILE, O_RDONLY);
 	SAFE_CLOSE(fd);
 }
 
+static const char *test_variant_name(void)
+{
+	switch (tst_variant) {
+	case 0: return "";
+	case 1: return "(FAN_REPORT_FD_ERROR)";
+	case 2: return "(FAN_REPORT_TID)";
+	case 3: return "(FAN_REPORT_FD_ERROR | FAN_REPORT_TID)";
+	}
+	return NULL;
+}
+
 static void do_setup(void)
 {
-	int pidfd;
 	int init_flags = FAN_REPORT_PIDFD;
 
 	/* Bind mount so remount ro/rw always work */
 	SAFE_MOUNT(MOUNT_PATH, MOUNT_PATH, "none", MS_BIND, NULL);
 
-	if (tst_variant) {
+	if (TST_VARIANT_FD_ERROR) {
 		fanotify_fd = -1;
 		fd_error_unsupported = fanotify_init_flags_supported_on_fs(FAN_REPORT_FD_ERROR, ".");
 		if (fd_error_unsupported)
@@ -112,6 +133,15 @@ static void do_setup(void)
 		init_flags |= FAN_REPORT_FD_ERROR;
 	}
 
+	if (TST_VARIANT_PIDFD_THREAD) {
+		fanotify_fd = -1;
+		thread_pidfd_unsupported = fanotify_init_flags_supported_on_fs(
+			FAN_REPORT_PIDFD | FAN_REPORT_TID, ".");
+		if (thread_pidfd_unsupported)
+			return;
+		init_flags |= FAN_REPORT_TID;
+	}
+
 	SAFE_TOUCH(TEST_FILE, 0666, NULL);
 
 	/*
@@ -125,12 +155,6 @@ static void do_setup(void)
 	fanotify_fd = SAFE_FANOTIFY_INIT(init_flags, O_RDWR);
 	SAFE_FANOTIFY_MARK(fanotify_fd, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD,
 			   TEST_FILE);
-
-	pidfd = SAFE_PIDFD_OPEN(getpid(), 0);
-
-	read_pidfd_fdinfo(pidfd, &exp_pidfd_fdinfo);
-
-	SAFE_CLOSE(pidfd);
 }
 
 static void do_test(unsigned int num)
@@ -138,19 +162,25 @@ static void do_test(unsigned int num)
 	int i = 0, len;
 	struct test_case_t *tc = &test_cases[num];
 	int nopidfd_err = tc->want_pidfd_err ?
-			  (tst_variant ? -ESRCH : FAN_NOPIDFD) : 0;
-	int fd_err = (tc->remount_ro && tst_variant) ? -EROFS : 0;
+			  (TST_VARIANT_FD_ERROR ? -ESRCH : FAN_NOPIDFD) : 0;
+	int fd_err = (tc->remount_ro && TST_VARIANT_FD_ERROR) ? -EROFS : 0;
 	pid_t reader_pid;
 	int reader_exit_status;
 
 	tst_res(TINFO, "Test #%d.%d: %s %s", num, tst_variant, tc->name,
-			tst_variant ? "(FAN_REPORT_FD_ERROR)" : "");
+		       test_variant_name());
 
-	if (fd_error_unsupported && tst_variant) {
+	if (fd_error_unsupported && TST_VARIANT_FD_ERROR) {
 		FANOTIFY_INIT_FLAGS_ERR_MSG(FAN_REPORT_FD_ERROR, fd_error_unsupported);
 		return;
 	}
 
+	if (thread_pidfd_unsupported && TST_VARIANT_PIDFD_THREAD) {
+		FANOTIFY_INIT_FLAGS_ERR_MSG(
+			FAN_REPORT_PIDFD | FAN_REPORT_TID, thread_pidfd_unsupported);
+		return;
+	}
+
 	/* remount ro/rw the bind mount */
 	SAFE_MOUNT("none", MOUNT_PATH, "none", MS_BIND | MS_REMOUNT |
 		   (tc->remount_ro ? MS_RDONLY : 0), NULL);
@@ -333,7 +363,7 @@ static struct tst_test test = {
 	.setup = do_setup,
 	.test = do_test,
 	.tcnt = ARRAY_SIZE(test_cases),
-	.test_variants = 2,
+	.test_variants = 4,
 	.cleanup = do_cleanup,
 	.all_filesystems = 1,
 	.needs_root = 1,
Signature · 1 line
2.54.0
Signature · 5 lines
Jan Kara <[email protected]>
SUSE Labs, CR

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
maintainer
#19 lore raw

Re: [LTP] [PATCH 4/5] fanotify21: Add test variants for FAN_REPORT_TID

Hello!

Quoted 4 lines · Andrea Cervesato

the patch-set is failing to compile on our CI. Please send a new version fixing it:

https://github.com/linux-test-project/ltp/actions/runs/27572447779/job/81512172691

Hum, I had a look and the failure actually is:

parse_opts.c: In function 'usc_test_looping':
parse_opts.c:521:3: warning: call to function 'usc_recressive_func' without a real prototype [-Wunprototyped-calls]
usc_recressive_func(0, STD_LP_recfun, *STD_bigstack);
^
parse_opts.c:107:13: note: 'usc_recressive_func' was declared here

 static void usc_recressive_func();
             ^

CC libs/numa/tse_numa.o
AR libltpnuma.a
RANLIB libltpnuma.a
CC lib/random_range.o
CC lib/parse_opts.o
CC lib/safe_file_ops.o
In file included from ../../include/tse_sigwait.h:11:0,
from tse_sigwait.c:8:
../../include/tst_timer.h: In function 'tst_ts_from_timespec':
../../include/tst_timer.h:626:3: warning: missing initializer for field 'tv_nsec' of 'struct timespec' [-Wmissing-field-initializers]
.ts.libc_ts.tv_nsec = ts.tv_nsec,
^
In file included from /usr/include/sys/select.h:43:0,

                 from /usr/include/sys/types.h:219,
                 from /usr/include/stdlib.h:314,
                 from tse_sigwait.c:6:

/usr/include/time.h:123:23: note: 'tv_nsec' declared here
__syscall_slong_t tv_nsec; /* Nanoseconds. */
^
CC lib/safe_pthread.o

So I'm a bit at loss what change in fanotify tests could have caused a failure in compilation of the generic LTP library code? Any idea?

Honza

Signature · 5 lines
Jan Kara <[email protected]>
SUSE Labs, CR

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#20 lore raw

Re: [LTP] [PATCH 4/5] fanotify21: Add test variants for FAN_REPORT_TID

Hi Jan,

Quoted 38 lines

Hello!

Quoted 4 lines · Andrea Cervesato

the patch-set is failing to compile on our CI. Please send a new version fixing it:

https://github.com/linux-test-project/ltp/actions/runs/27572447779/job/81512172691

Hum, I had a look and the failure actually is:

parse_opts.c: In function 'usc_test_looping':
parse_opts.c:521:3: warning: call to function 'usc_recressive_func' without a real prototype [-Wunprototyped-calls]

   usc_recressive_func(0, STD_LP_recfun, *STD_bigstack);
   ^
parse_opts.c:107:13: note: 'usc_recressive_func' was declared here
 static void usc_recressive_func();
             ^

CC libs/numa/tse_numa.o
AR libltpnuma.a
RANLIB libltpnuma.a
CC lib/random_range.o
CC lib/parse_opts.o
CC lib/safe_file_ops.o
In file included from ../../include/tse_sigwait.h:11:0,
from tse_sigwait.c:8:
../../include/tst_timer.h: In function 'tst_ts_from_timespec':
../../include/tst_timer.h:626:3: warning: missing initializer for field 'tv_nsec' of 'struct timespec' [-Wmissing-field-initializers]
.ts.libc_ts.tv_nsec = ts.tv_nsec,
^
In file included from /usr/include/sys/select.h:43:0,

                 from /usr/include/sys/types.h:219,
                 from /usr/include/stdlib.h:314,
                 from tse_sigwait.c:6:

/usr/include/time.h:123:23: note: 'tv_nsec' declared here
__syscall_slong_t tv_nsec; /* Nanoseconds. */
^
CC lib/safe_pthread.o

Quoted 7 lines

So I'm a bit at loss what change in fanotify tests could have caused a failure in compilation of the generic LTP library code? Any idea?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR

Those are warnings caused by old GCC. It's possible to fix them by changing the explicit struct initialization with a 2 step init such as:

struct mystruct s;
s.item0 = i1;
s.item1 = i2;

The real issue is that fanotify21 needs to link pthread:

-fanotify11: CFLAGS+=-pthread
+fanotify11 fanotify21: CFLAGS+=-pthread

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#21 lore raw

PATCH 5/5 fanotify21: Add FAN_REPORT_TID pidfd coverage

In reply to AnonymeMeow

Add a test case that generates an event from a thread. Without FAN_REPORT_TID, the reported pidfd is expected to refer to the thread group leader. With FAN_REPORT_TID, it is expected to refer to the triggering thread. Keep the event thread alive until the reported pidfd is verified, because pidfd fdinfo reports Pid: -1 after the thread exits.

---
 .../kernel/syscalls/fanotify/fanotify21.c     | 47 ++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/fanotify/fanotify21.c +46 -1
index fd43c8c1c..8a5b35334 100644
@@ -24,6 +24,7 @@
 #include "tst_test.h"
 #include "tst_safe_stdio.h"
 #include "tst_safe_macros.h"
+#include "tst_safe_pthread.h"
 #include "lapi/pidfd.h"
 
 #ifdef HAVE_SYS_FANOTIFY_H
@@ -46,6 +47,7 @@ struct pidfd_fdinfo_t {
 
 static struct test_case_t {
 	char *name;
+	int event_by_thread;
 	int want_pidfd_err;
 	int remount_ro;
 } test_cases[] = {
@@ -53,15 +55,24 @@ static struct test_case_t {
 		"return a valid pidfd for event created by self",
 		0,
 		0,
+		0,
+	},
+	{
+		"return a valid pidfd for event created by thread",
+		1,
+		0,
+		0,
 	},
 	{
 		"return invalid pidfd for reader in descendant PID namespace",
+		0,
 		1,
 		0,
 	},
 	{
 		"fail to open rw fd for event created on read-only mount",
 		0,
+		0,
 		1,
 	},
 };
@@ -107,6 +118,26 @@ static void generate_event(void)
 	SAFE_CLOSE(fd);
 }
 
+static void *generate_event_pthread(void *args LTP_ATTRIBUTE_UNUSED)
+{
+	generate_event();
+	TST_CHECKPOINT_WAIT(0);
+	return NULL;
+}
+
+static pthread_t event_thread_create(void)
+{
+	pthread_t pthread_id;
+	SAFE_PTHREAD_CREATE(&pthread_id, NULL, generate_event_pthread, NULL);
+	return pthread_id;
+}
+
+static void event_thread_join(pthread_t pthread_id)
+{
+	TST_CHECKPOINT_WAKE(0);
+	SAFE_PTHREAD_JOIN(pthread_id, NULL);
+}
+
 static const char *test_variant_name(void)
 {
 	switch (tst_variant) {
@@ -164,6 +195,7 @@ static void do_test(unsigned int num)
 	int nopidfd_err = tc->want_pidfd_err ?
 			  (TST_VARIANT_FD_ERROR ? -ESRCH : FAN_NOPIDFD) : 0;
 	int fd_err = (tc->remount_ro && TST_VARIANT_FD_ERROR) ? -EROFS : 0;
+	pthread_t event_thread;
 	pid_t reader_pid;
 	int reader_exit_status;
 
@@ -185,7 +217,16 @@ static void do_test(unsigned int num)
 	SAFE_MOUNT("none", MOUNT_PATH, "none", MS_BIND | MS_REMOUNT |
 		   (tc->remount_ro ? MS_RDONLY : 0), NULL);
 
-	generate_event();
+	/*
+	 * Generate the event by either self or a thread. Event generation in
+	 * a thread is done so that in FAN_REPORT_TID mode the pidfd can be
+	 * verified to refer to the tid, not the tgid. The thread waits until
+	 * the pidfd is verified before exiting.
+	 */
+	if (tc->event_by_thread)
+		event_thread = event_thread_create();
+	else
+		generate_event();
 
 	/*
 	 * Read the event in either self or a child process in a descendant
@@ -348,6 +389,9 @@ next_event:
 
 	if (tc->want_pidfd_err)
 		exit(0);
+
+	if (tc->event_by_thread)
+		event_thread_join(event_thread);
 }
 
 static void do_cleanup(void)
@@ -370,6 +414,7 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.mntpoint = MOUNT_PATH,
 	.forks_child = 1,
+	.needs_checkpoints = 1,
 };
 
 #else
Signature · 5 lines
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
maintainer Reviewed-by on v1 5/5
#22 lore raw

Re: [LTP] [PATCH 5/5] fanotify21: Add FAN_REPORT_TID pidfd coverage

Quoted 8 lines · AnonymeMeow

Add a test case that generates an event from a thread. Without FAN_REPORT_TID, the reported pidfd is expected to refer to the thread group leader. With FAN_REPORT_TID, it is expected to refer to the triggering thread. Keep the event thread alive until the reported pidfd is verified, because pidfd fdinfo reports Pid: -1 after the thread exits.

Nice! Feel free to add:

Honza

Quoted 123 lines
---
 .../kernel/syscalls/fanotify/fanotify21.c     | 47 ++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/fanotify/fanotify21.c +46 -1
index fd43c8c1c..8a5b35334 100644
@@ -24,6 +24,7 @@
 #include "tst_test.h"
 #include "tst_safe_stdio.h"
 #include "tst_safe_macros.h"
+#include "tst_safe_pthread.h"
 #include "lapi/pidfd.h"
 
 #ifdef HAVE_SYS_FANOTIFY_H
@@ -46,6 +47,7 @@ struct pidfd_fdinfo_t {
 
 static struct test_case_t {
 	char *name;
+	int event_by_thread;
 	int want_pidfd_err;
 	int remount_ro;
 } test_cases[] = {
@@ -53,15 +55,24 @@ static struct test_case_t {
 		"return a valid pidfd for event created by self",
 		0,
 		0,
+		0,
+	},
+	{
+		"return a valid pidfd for event created by thread",
+		1,
+		0,
+		0,
 	},
 	{
 		"return invalid pidfd for reader in descendant PID namespace",
+		0,
 		1,
 		0,
 	},
 	{
 		"fail to open rw fd for event created on read-only mount",
 		0,
+		0,
 		1,
 	},
 };
@@ -107,6 +118,26 @@ static void generate_event(void)
 	SAFE_CLOSE(fd);
 }
 
+static void *generate_event_pthread(void *args LTP_ATTRIBUTE_UNUSED)
+{
+	generate_event();
+	TST_CHECKPOINT_WAIT(0);
+	return NULL;
+}
+
+static pthread_t event_thread_create(void)
+{
+	pthread_t pthread_id;
+	SAFE_PTHREAD_CREATE(&pthread_id, NULL, generate_event_pthread, NULL);
+	return pthread_id;
+}
+
+static void event_thread_join(pthread_t pthread_id)
+{
+	TST_CHECKPOINT_WAKE(0);
+	SAFE_PTHREAD_JOIN(pthread_id, NULL);
+}
+
 static const char *test_variant_name(void)
 {
 	switch (tst_variant) {
@@ -164,6 +195,7 @@ static void do_test(unsigned int num)
 	int nopidfd_err = tc->want_pidfd_err ?
 			  (TST_VARIANT_FD_ERROR ? -ESRCH : FAN_NOPIDFD) : 0;
 	int fd_err = (tc->remount_ro && TST_VARIANT_FD_ERROR) ? -EROFS : 0;
+	pthread_t event_thread;
 	pid_t reader_pid;
 	int reader_exit_status;
 
@@ -185,7 +217,16 @@ static void do_test(unsigned int num)
 	SAFE_MOUNT("none", MOUNT_PATH, "none", MS_BIND | MS_REMOUNT |
 		   (tc->remount_ro ? MS_RDONLY : 0), NULL);
 
-	generate_event();
+	/*
+	 * Generate the event by either self or a thread. Event generation in
+	 * a thread is done so that in FAN_REPORT_TID mode the pidfd can be
+	 * verified to refer to the tid, not the tgid. The thread waits until
+	 * the pidfd is verified before exiting.
+	 */
+	if (tc->event_by_thread)
+		event_thread = event_thread_create();
+	else
+		generate_event();
 
 	/*
 	 * Read the event in either self or a child process in a descendant
@@ -348,6 +389,9 @@ next_event:
 
 	if (tc->want_pidfd_err)
 		exit(0);
+
+	if (tc->event_by_thread)
+		event_thread_join(event_thread);
 }
 
 static void do_cleanup(void)
@@ -370,6 +414,7 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.mntpoint = MOUNT_PATH,
 	.forks_child = 1,
+	.needs_checkpoints = 1,
 };
 
 #else
Signature · 1 line
2.54.0
Signature · 5 lines
Jan Kara <[email protected]>
SUSE Labs, CR

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
reviewer Reviewed-by on the v1 series
#23 lore raw

Re: [LTP] [PATCH 0/5] fanotify: update pidfd tests for FAN_REPORT_TID

In reply to AnonymeMeow
Quoted 4 lines · AnonymeMeow

This patch set updates fanotify pidfd tests for the kernel change that adds FAN_REPORT_PIDFD support together with FAN_REPORT_TID.

Quoted 8 lines

---

Sorry for the late reply, I accidentally broke my system last week, so I was busy repairing it. And I may not be able to respond in time due to final exams this week, sorry.

Great series. Nice job!

I am glad that you managed to salvage the variants idea and made the test better on the way.

Feel free to add to the series:

(no need to repost for adding RVB only if reposting for another reason)

Thanks!
Amir.

Quoted 17 lines

---

AnonymeMeow (5):
fanotify20: Allow FAN_REPORT_PIDFD with FAN_REPORT_TID on v7.2+
fanotify21: Stop relying on exited child for pidfd error
fanotify21: Simplify read_pidfd_fdinfo()
fanotify21: Add test variants for FAN_REPORT_TID
fanotify21: Add FAN_REPORT_TID pidfd coverage

 include/lapi/pidfd.h                          |   4 +
 .../kernel/syscalls/fanotify/fanotify20.c     |  19 +-
 .../kernel/syscalls/fanotify/fanotify21.c     | 191 +++++++++++-------
 3 files changed, 136 insertions(+), 78 deletions(-)

--
2.54.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp