Skip to contentSkip to conversation

Add test for fanotify monitoring of tracefs

PATCH Applied Martin Doucha 6 participants 13 messages Feb 20 – Feb 26

lore
Share & tools
lore
#0 lore raw

cover Add test for fanotify monitoring of tracefs

Add test for reading basic fanotify events from a tracefs mount. The test will activate monitoring of FAN_MODIFY events from the whole mountpoint, create a new kprobe, activate it, deactivate it and remove it again. This should generate exactly 4 FAN_MODIFY events.

On some kernels with badly backported tracefs fixes, this may trigger NULL pointer dereference. The testcase is intentionally simple but sufficient to trigger this kind of bug. More complex tracing subtests can be added later.

Martin Doucha (2):
tst_fs.h: Add TST_TRACEFS_MAGIC constant
Add test for fanotify monitoring of tracing filesystem

 include/tst_fs.h                              |   1 +
 runtest/syscalls                              |   1 +
 runtest/tracing                               |   1 +
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 .../kernel/syscalls/fanotify/fanotify25.c     | 126 ++++++++++++++++++
 5 files changed, 130 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify25.c
Signature · 5 lines
2.52.0


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

PATCH 1/2 tst_fs.h: Add TST_TRACEFS_MAGIC constant

---
 include/tst_fs.h | 1 +
 1 file changed, 1 insertion(+)
include/tst_fs.h +1 -0
index ceae78e7e..6dfa157a5 100644
@@ -11,6 +11,7 @@
 #define TST_NFS_MAGIC      0x6969
 #define TST_RAMFS_MAGIC    0x858458f6
 #define TST_TMPFS_MAGIC    0x01021994
+#define TST_TRACEFS_MAGIC  0x74726163
 #define TST_V9FS_MAGIC     0x01021997
 #define TST_XFS_MAGIC      0x58465342
 #define TST_EXT2_OLD_MAGIC 0xEF51
Signature · 5 lines
2.52.0


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

Re: [LTP] [PATCH 1/2] tst_fs.h: Add TST_TRACEFS_MAGIC constant

Hi Martin,

Obviously correct.

Kind regards,
Petr

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

PATCH 2/2 Add test for fanotify monitoring of tracing filesystem

In reply to Martin Doucha

Add a test that will start monitoring the kernel tracing filesystem, write new configuration into tracing sysfiles and then verify that the writes triggered fanotify events.

---
 runtest/syscalls                              |   1 +
 runtest/tracing                               |   1 +
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 .../kernel/syscalls/fanotify/fanotify25.c     | 126 ++++++++++++++++++
 4 files changed, 129 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify25.c
runtest/syscalls +1 -0
index 2f629e4e4..30224f5b4 100644
@@ -671,6 +671,7 @@ fanotify21 fanotify21
 fanotify22 fanotify22
 fanotify23 fanotify23
 fanotify24 fanotify24
+fanotify25 fanotify25
 
 ioperm01 ioperm01
 ioperm02 ioperm02
runtest/tracing +1 -0
index d2700ca57..674e2ad97 100644
@@ -3,6 +3,7 @@ ftrace_regression01	ftrace_regression01.sh
 ftrace_regression02	ftrace_regression02.sh
 ftrace-stress-test	ftrace_stress_test.sh 90
 dynamic_debug01		dynamic_debug01.sh
+fanotify25 fanotify25
 pt_full_trace_basic pt_test
 pt_snapshot_trace_basic pt_test -m
 pt_ex_user pt_test -e user
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index 16af3db85..d6d0599f1 100644
@@ -22,4 +22,5 @@
 /fanotify22
 /fanotify23
 /fanotify24
+/fanotify25
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify25.c new file +126 -0

126 lines not on this page · raw message

Signature · 5 lines
2.52.0


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

Re: [LTP] [PATCH 2/2] Add test for fanotify monitoring of tracing filesystem

Hi!
CCing Jan and Amir.

Quoted 185 lines

Add a test that will start monitoring the kernel tracing filesystem, write new configuration into tracing sysfiles and then verify that the writes triggered fanotify events.

---
 runtest/syscalls                              |   1 +
 runtest/tracing                               |   1 +
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 .../kernel/syscalls/fanotify/fanotify25.c     | 126 ++++++++++++++++++
 4 files changed, 129 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify25.c
runtest/syscalls +1 -0
index 2f629e4e4..30224f5b4 100644
@@ -671,6 +671,7 @@ fanotify21 fanotify21
 fanotify22 fanotify22
 fanotify23 fanotify23
 fanotify24 fanotify24
+fanotify25 fanotify25
 
 ioperm01 ioperm01
 ioperm02 ioperm02
runtest/tracing +1 -0
index d2700ca57..674e2ad97 100644
@@ -3,6 +3,7 @@ ftrace_regression01	ftrace_regression01.sh
 ftrace_regression02	ftrace_regression02.sh
 ftrace-stress-test	ftrace_stress_test.sh 90
 dynamic_debug01		dynamic_debug01.sh
+fanotify25 fanotify25
 pt_full_trace_basic pt_test
 pt_snapshot_trace_basic pt_test -m
 pt_ex_user pt_test -e user
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index 16af3db85..d6d0599f1 100644
@@ -22,4 +22,5 @@
 /fanotify22
 /fanotify23
 /fanotify24
+/fanotify25
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify25.c new file +126 -0
new file mode 100644
index 000000000..c982f3225
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 SUSE LLC
+ * Author: Petr Pavlu <[email protected]>
+ * LTP port: Martin Doucha <[email protected]>
+ */
+
+/*\
+ * Verify that fanotify monitoring can be applied to the tracing filesystem
+ * and write events will be correctly delivered.
+ */
+
+#define _GNU_SOURCE
+#include "tst_test.h"
+#include "lapi/mount.h"
+
+#ifdef HAVE_SYS_FANOTIFY_H
+#include "fanotify.h"
+
+#define MNTPOINT "/sys/kernel/tracing"
+#define EVENTS_SYSFILE MNTPOINT "/kprobe_events"
+
+static const struct traceconfig {
+	const char *filename;
+	const char *wdata;
+} trace_cmds[] = {
+	{EVENTS_SYSFILE, "p:ltp_load_module_0 load_module"},
+	{MNTPOINT "/events/kprobes/ltp_load_module_0/enable", "1"},
+	{MNTPOINT "/events/kprobes/ltp_load_module_0/enable", "0"},
+	{EVENTS_SYSFILE, "-:ltp_load_module_0"},
+	{}
+};
+
+static int fan_fd = -1;
+static int unmount_needed;
+
+static void setup(void)
+{
+	if (tst_fs_type(MNTPOINT) != TST_TRACEFS_MAGIC) {
+		SAFE_MOUNT("tracefs", MNTPOINT, "tracefs",
+			MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);
+		unmount_needed = 1;
+	}
+
+	if (access(EVENTS_SYSFILE, F_OK))
+		tst_brk(TCONF, "Kprobe events not supported by kernel");
+
+	fan_fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK, O_RDONLY);
+	SAFE_FANOTIFY_MARK(fan_fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_MODIFY,
+		-1, MNTPOINT);
+}
+
+static void do_child(void)
+{
+	int i, fd, events, ret;
+	pid_t pid = getpid();
+	struct fanotify_event_metadata buf;
+
+	for (i = 0, events = 0; trace_cmds[i].filename; i++) {
+		fd = SAFE_OPEN(trace_cmds[i].filename, O_WRONLY, 0644);
+		SAFE_WRITE(1, fd, trace_cmds[i].wdata,
+			strlen(trace_cmds[i].wdata));
+		SAFE_CLOSE(fd);
+
+		while ((ret = read(fan_fd, &buf, sizeof(buf))) > 0) {
+			if (buf.pid != pid)
+				continue;
+
+			if (!(buf.mask & FAN_MODIFY)) {
+				tst_res(TFAIL, "Unexpected event %llx",
+					buf.mask);
+				continue;
+			}
+
+			events++;
+		}
+
+		if (ret < 0 && errno != EAGAIN)
+			tst_res(TFAIL | TERRNO, "fanotify read() failed");
+	}
+
+	if (events == i)
+		tst_res(TPASS, "Received %d events", events);
+	else
+		tst_res(TFAIL, "Received %d events, expected %d", events, i);
+}
+
+static void run(void)
+{
+	/*
+	 * Fork a child to do the actual trace writes, otherwise tracefs
+	 * would be busy until the current process exits and it would become
+	 * impossible to unmount in cleanup().
+	 */
+	if (!SAFE_FORK()) {
+		do_child();
+		SAFE_CLOSE(fan_fd);
+		exit(0);
+	}
+}
+
+static void cleanup(void)
+{
+	if (fan_fd >= 0)
+		SAFE_CLOSE(fan_fd);
+
+	if (unmount_needed)
+		SAFE_UMOUNT(MNTPOINT);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_root = 1,
+	.forks_child = 1,
+	.taint_check = TST_TAINT_W | TST_TAINT_D,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_TRACING",
+		NULL
+	}
+};
+
+#else
+	TST_TEST_TCONF("system doesn't have required fanotify support");
+#endif
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH 2/2] Add test for fanotify monitoring of tracing filesystem

Quoted 9 lines · Cyril Hrubis

Hi!
CCing Jan and Amir.

Quoted 5 lines

Add a test that will start monitoring the kernel tracing filesystem, write new configuration into tracing sysfiles and then verify that the writes triggered fanotify events.

LGTM

Feel free to add

Quoted 184 lines
Quoted 178 lines
---
 runtest/syscalls                              |   1 +
 runtest/tracing                               |   1 +
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 .../kernel/syscalls/fanotify/fanotify25.c     | 126 ++++++++++++++++++
 4 files changed, 129 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify25.c
runtest/syscalls +1 -0
index 2f629e4e4..30224f5b4 100644
@@ -671,6 +671,7 @@ fanotify21 fanotify21
 fanotify22 fanotify22
 fanotify23 fanotify23
 fanotify24 fanotify24
+fanotify25 fanotify25
 
 ioperm01 ioperm01
 ioperm02 ioperm02
runtest/tracing +1 -0
index d2700ca57..674e2ad97 100644
@@ -3,6 +3,7 @@ ftrace_regression01   ftrace_regression01.sh
 ftrace_regression02  ftrace_regression02.sh
 ftrace-stress-test   ftrace_stress_test.sh 90
 dynamic_debug01              dynamic_debug01.sh
+fanotify25 fanotify25
 pt_full_trace_basic pt_test
 pt_snapshot_trace_basic pt_test -m
 pt_ex_user pt_test -e user
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index 16af3db85..d6d0599f1 100644
@@ -22,4 +22,5 @@
 /fanotify22
 /fanotify23
 /fanotify24
+/fanotify25
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify25.c new file +126 -1
new file mode 100644
index 000000000..c982f3225
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 SUSE LLC
+ * Author: Petr Pavlu <[email protected]>
+ * LTP port: Martin Doucha <[email protected]>
+ */
+
+/*\
+ * Verify that fanotify monitoring can be applied to the tracing filesystem
+ * and write events will be correctly delivered.
+ */
+
+#define _GNU_SOURCE
+#include "tst_test.h"
+#include "lapi/mount.h"
+
+#ifdef HAVE_SYS_FANOTIFY_H
+#include "fanotify.h"
+
+#define MNTPOINT "/sys/kernel/tracing"
+#define EVENTS_SYSFILE MNTPOINT "/kprobe_events"
+
+static const struct traceconfig {
+     const char *filename;
+     const char *wdata;
+} trace_cmds[] = {
+     {EVENTS_SYSFILE, "p:ltp_load_module_0 load_module"},
+     {MNTPOINT "/events/kprobes/ltp_load_module_0/enable", "1"},
+     {MNTPOINT "/events/kprobes/ltp_load_module_0/enable", "0"},
+     {EVENTS_SYSFILE, "-:ltp_load_module_0"},
+     {}
+};
+
+static int fan_fd = -1;
+static int unmount_needed;
+
+static void setup(void)
+{
+     if (tst_fs_type(MNTPOINT) != TST_TRACEFS_MAGIC) {
+             SAFE_MOUNT("tracefs", MNTPOINT, "tracefs",
+                     MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);
+             unmount_needed = 1;
+     }
+
+     if (access(EVENTS_SYSFILE, F_OK))
+             tst_brk(TCONF, "Kprobe events not supported by kernel");
+
+     fan_fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK, O_RDONLY);
+     SAFE_FANOTIFY_MARK(fan_fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_MODIFY,
+             -1, MNTPOINT);
+}
+
+static void do_child(void)
+{
+     int i, fd, events, ret;
+     pid_t pid = getpid();
+     struct fanotify_event_metadata buf;
+
+     for (i = 0, events = 0; trace_cmds[i].filename; i++) {
+             fd = SAFE_OPEN(trace_cmds[i].filename, O_WRONLY, 0644);
+             SAFE_WRITE(1, fd, trace_cmds[i].wdata,
+                     strlen(trace_cmds[i].wdata));
+             SAFE_CLOSE(fd);
+
+             while ((ret = read(fan_fd, &buf, sizeof(buf))) > 0) {
+                     if (buf.pid != pid)
+                             continue;
+
+                     if (!(buf.mask & FAN_MODIFY)) {
+                             tst_res(TFAIL, "Unexpected event %llx",
+                                     buf.mask);
+                             continue;
+                     }
+
+                     events++;
+             }
+
+             if (ret < 0 && errno != EAGAIN)
+                     tst_res(TFAIL | TERRNO, "fanotify read() failed");
+     }
+
+     if (events == i)
+             tst_res(TPASS, "Received %d events", events);
+     else
+             tst_res(TFAIL, "Received %d events, expected %d", events, i);
+}
+
+static void run(void)
+{
+     /*
+      * Fork a child to do the actual trace writes, otherwise tracefs
+      * would be busy until the current process exits and it would become
+      * impossible to unmount in cleanup().
+      */
+     if (!SAFE_FORK()) {
+             do_child();
+             SAFE_CLOSE(fan_fd);
+             exit(0);
+     }
+}
+
+static void cleanup(void)
+{
+     if (fan_fd >= 0)
+             SAFE_CLOSE(fan_fd);
+
+     if (unmount_needed)
+             SAFE_UMOUNT(MNTPOINT);
+}
+
+static struct tst_test test = {
+     .test_all = run,
+     .setup = setup,
+     .cleanup = cleanup,
+     .needs_root = 1,
+     .forks_child = 1,
+     .taint_check = TST_TAINT_W | TST_TAINT_D,
+     .needs_kconfigs = (const char *[]) {
+             "CONFIG_TRACING",
+             NULL
+     }
+};
+
+#else
+     TST_TEST_TCONF("system doesn't have required fanotify support");
+#endif
--

2.52.0

Quoted 2 lines

--
Mailing list info: https://lists.linux.it/listinfo/ltp

--
Cyril Hrubis
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by
#7 lore raw

Re: [LTP] [PATCH 2/2] Add test for fanotify monitoring of tracing filesystem

In reply to Cyril Hrubis
Quoted 8 lines · Cyril Hrubis

Hi!
CCing Jan and Amir.

Quoted 5 lines

Add a test that will start monitoring the kernel tracing filesystem, write new configuration into tracing sysfiles and then verify that the writes triggered fanotify events.

Looks good. Feel free to add:

Honza

Quoted 184 lines
Quoted 180 lines
---
 runtest/syscalls                              |   1 +
 runtest/tracing                               |   1 +
 testcases/kernel/syscalls/fanotify/.gitignore |   1 +
 .../kernel/syscalls/fanotify/fanotify25.c     | 126 ++++++++++++++++++
 4 files changed, 129 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify25.c
runtest/syscalls +1 -0
index 2f629e4e4..30224f5b4 100644
@@ -671,6 +671,7 @@ fanotify21 fanotify21
 fanotify22 fanotify22
 fanotify23 fanotify23
 fanotify24 fanotify24
+fanotify25 fanotify25
 
 ioperm01 ioperm01
 ioperm02 ioperm02
runtest/tracing +1 -0
index d2700ca57..674e2ad97 100644
@@ -3,6 +3,7 @@ ftrace_regression01	ftrace_regression01.sh
 ftrace_regression02	ftrace_regression02.sh
 ftrace-stress-test	ftrace_stress_test.sh 90
 dynamic_debug01		dynamic_debug01.sh
+fanotify25 fanotify25
 pt_full_trace_basic pt_test
 pt_snapshot_trace_basic pt_test -m
 pt_ex_user pt_test -e user
testcases/kernel/syscalls/fanotify/.gitignore +1 -0
index 16af3db85..d6d0599f1 100644
@@ -22,4 +22,5 @@
 /fanotify22
 /fanotify23
 /fanotify24
+/fanotify25
 /fanotify_child
testcases/kernel/syscalls/fanotify/fanotify25.c new file +126 -0
new file mode 100644
index 000000000..c982f3225
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 SUSE LLC
+ * Author: Petr Pavlu <[email protected]>
+ * LTP port: Martin Doucha <[email protected]>
+ */
+
+/*\
+ * Verify that fanotify monitoring can be applied to the tracing filesystem
+ * and write events will be correctly delivered.
+ */
+
+#define _GNU_SOURCE
+#include "tst_test.h"
+#include "lapi/mount.h"
+
+#ifdef HAVE_SYS_FANOTIFY_H
+#include "fanotify.h"
+
+#define MNTPOINT "/sys/kernel/tracing"
+#define EVENTS_SYSFILE MNTPOINT "/kprobe_events"
+
+static const struct traceconfig {
+	const char *filename;
+	const char *wdata;
+} trace_cmds[] = {
+	{EVENTS_SYSFILE, "p:ltp_load_module_0 load_module"},
+	{MNTPOINT "/events/kprobes/ltp_load_module_0/enable", "1"},
+	{MNTPOINT "/events/kprobes/ltp_load_module_0/enable", "0"},
+	{EVENTS_SYSFILE, "-:ltp_load_module_0"},
+	{}
+};
+
+static int fan_fd = -1;
+static int unmount_needed;
+
+static void setup(void)
+{
+	if (tst_fs_type(MNTPOINT) != TST_TRACEFS_MAGIC) {
+		SAFE_MOUNT("tracefs", MNTPOINT, "tracefs",
+			MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);
+		unmount_needed = 1;
+	}
+
+	if (access(EVENTS_SYSFILE, F_OK))
+		tst_brk(TCONF, "Kprobe events not supported by kernel");
+
+	fan_fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK, O_RDONLY);
+	SAFE_FANOTIFY_MARK(fan_fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_MODIFY,
+		-1, MNTPOINT);
+}
+
+static void do_child(void)
+{
+	int i, fd, events, ret;
+	pid_t pid = getpid();
+	struct fanotify_event_metadata buf;
+
+	for (i = 0, events = 0; trace_cmds[i].filename; i++) {
+		fd = SAFE_OPEN(trace_cmds[i].filename, O_WRONLY, 0644);
+		SAFE_WRITE(1, fd, trace_cmds[i].wdata,
+			strlen(trace_cmds[i].wdata));
+		SAFE_CLOSE(fd);
+
+		while ((ret = read(fan_fd, &buf, sizeof(buf))) > 0) {
+			if (buf.pid != pid)
+				continue;
+
+			if (!(buf.mask & FAN_MODIFY)) {
+				tst_res(TFAIL, "Unexpected event %llx",
+					buf.mask);
+				continue;
+			}
+
+			events++;
+		}
+
+		if (ret < 0 && errno != EAGAIN)
+			tst_res(TFAIL | TERRNO, "fanotify read() failed");
+	}
+
+	if (events == i)
+		tst_res(TPASS, "Received %d events", events);
+	else
+		tst_res(TFAIL, "Received %d events, expected %d", events, i);
+}
+
+static void run(void)
+{
+	/*
+	 * Fork a child to do the actual trace writes, otherwise tracefs
+	 * would be busy until the current process exits and it would become
+	 * impossible to unmount in cleanup().
+	 */
+	if (!SAFE_FORK()) {
+		do_child();
+		SAFE_CLOSE(fan_fd);
+		exit(0);
+	}
+}
+
+static void cleanup(void)
+{
+	if (fan_fd >= 0)
+		SAFE_CLOSE(fan_fd);
+
+	if (unmount_needed)
+		SAFE_UMOUNT(MNTPOINT);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_root = 1,
+	.forks_child = 1,
+	.taint_check = TST_TAINT_W | TST_TAINT_D,
+	.needs_kconfigs = (const char *[]) {
+		"CONFIG_TRACING",
+		NULL
+	}
+};
+
+#else
+	TST_TEST_TCONF("system doesn't have required fanotify support");
+#endif
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
Signature · 5 lines
Jan Kara <[email protected]>
SUSE Labs, CR

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

Re: [LTP] [PATCH 2/2] Add test for fanotify monitoring of tracing filesystem

In reply to Martin Doucha

Hi Martin,

LGTM

...

Quoted 2 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +0 -0

...

Quoted 12 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +12 -0
+#define MNTPOINT "/sys/kernel/tracing"
+#define EVENTS_SYSFILE MNTPOINT "/kprobe_events"
+
+static const struct traceconfig {
+	const char *filename;
+	const char *wdata;
+} trace_cmds[] = {
+	{EVENTS_SYSFILE, "p:ltp_load_module_0 load_module"},
+	{MNTPOINT "/events/kprobes/ltp_load_module_0/enable", "1"},
+	{MNTPOINT "/events/kprobes/ltp_load_module_0/enable", "0"},
+	{EVENTS_SYSFILE, "-:ltp_load_module_0"},
+	{}

testcases/kernel/syscalls/fanotify/fanotify25.c line 41

Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +10 -0
+};
+
+static int fan_fd = -1;
+static int unmount_needed;
+
+static void setup(void)
+{
+	if (tst_fs_type(MNTPOINT) != TST_TRACEFS_MAGIC) {
+		SAFE_MOUNT("tracefs", MNTPOINT, "tracefs",
+			MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);

I see possible mounting is the reason...

testcases/kernel/syscalls/fanotify/fanotify25.c line 46

Quoted 5 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +5 -0
+		unmount_needed = 1;
+	}
+
+	if (access(EVENTS_SYSFILE, F_OK))
+		tst_brk(TCONF, "Kprobe events not supported by kernel");

... why we cannot check /sys/kernel/tracing/kprobe_events presence via .save_restore.

Quoted 30 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +30 -0
+
+	fan_fd = SAFE_FANOTIFY_INIT(FAN_CLASS_NOTIF | FAN_NONBLOCK, O_RDONLY);
+	SAFE_FANOTIFY_MARK(fan_fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_MODIFY,
+		-1, MNTPOINT);
+}
+
+static void do_child(void)
+{
+	int i, fd, events, ret;
+	pid_t pid = getpid();
+	struct fanotify_event_metadata buf;
+
+	for (i = 0, events = 0; trace_cmds[i].filename; i++) {
+		fd = SAFE_OPEN(trace_cmds[i].filename, O_WRONLY, 0644);
+		SAFE_WRITE(1, fd, trace_cmds[i].wdata,
+			strlen(trace_cmds[i].wdata));
+		SAFE_CLOSE(fd);
+
+		while ((ret = read(fan_fd, &buf, sizeof(buf))) > 0) {
+			if (buf.pid != pid)
+				continue;
+
+			if (!(buf.mask & FAN_MODIFY)) {
+				tst_res(TFAIL, "Unexpected event %llx",
+					buf.mask);
+				continue;
+			}
+
+			events++;
+		}

testcases/kernel/syscalls/fanotify/fanotify25.c line 86

Quoted 10 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +10 -0
+
+		if (ret < 0 && errno != EAGAIN)
+			tst_res(TFAIL | TERRNO, "fanotify read() failed");
+	}
+
+	if (events == i)
+		tst_res(TPASS, "Received %d events", events);
+	else
+		tst_res(TFAIL, "Received %d events, expected %d", events, i);
+}

OT: I guess we miss optional string arg for TST_EXP_EQ_*() macros to be usable.

testcases/kernel/syscalls/fanotify/fanotify25.c line 94

Quoted 8 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +8 -0
+
+static void run(void)
+{
+	/*
+	 * Fork a child to do the actual trace writes, otherwise tracefs
+	 * would be busy until the current process exits and it would become
+	 * impossible to unmount in cleanup().
+	 */

+1

Kind regards,
Petr

Quoted 6 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +6 -0
+	if (!SAFE_FORK()) {
+		do_child();
+		SAFE_CLOSE(fan_fd);
+		exit(0);
+	}
+}

...

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

Re: [LTP] [PATCH 2/2] Add test for fanotify monitoring of tracing filesystem

Hi!

Quoted 8 lines · Petr Vorel

testcases/kernel/syscalls/fanotify/fanotify25.c line 41

Quoted 5 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +5 -0
+static void setup(void)
+{
+	if (tst_fs_type(MNTPOINT) != TST_TRACEFS_MAGIC) {
+		SAFE_MOUNT("tracefs", MNTPOINT, "tracefs",
+			MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);

I see possible mounting is the reason...

Quoted 8 lines

testcases/kernel/syscalls/fanotify/fanotify25.c line 46

Quoted 5 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +5 -0
+		unmount_needed = 1;
+	}
+
+	if (access(EVENTS_SYSFILE, F_OK))
+		tst_brk(TCONF, "Kprobe events not supported by kernel");

... why we cannot check /sys/kernel/tracing/kprobe_events presence via .save_restore.

Well, yes, tracefs may need to be mounted during test setup. But .save_restore also assumes that sysfiles contain only one line while kprobe_events may hold arbitrarily long kprobe list. Also note that the restore function opens the sysfile with O_TRUNC, which might wipe all existing kprobes before creating them again. Therefore I've decided to check the file availability in setup() and send a targeted kprobe delete command instead of saving and restoring the whole sysfile.

Signature · 10 lines
Martin Doucha   [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

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

Re: [LTP] [PATCH 2/2] Add test for fanotify monitoring of tracing filesystem

Quoted 20 lines

Hi!

Quoted 8 lines · Petr Vorel

testcases/kernel/syscalls/fanotify/fanotify25.c line 41

Quoted 5 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +5 -0
+static void setup(void)
+{
+	if (tst_fs_type(MNTPOINT) != TST_TRACEFS_MAGIC) {
+		SAFE_MOUNT("tracefs", MNTPOINT, "tracefs",
+			MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);

I see possible mounting is the reason...

Quoted 8 lines

testcases/kernel/syscalls/fanotify/fanotify25.c line 46

Quoted 5 lines
testcases/kernel/syscalls/fanotify/fanotify25.c new file +5 -0
+		unmount_needed = 1;
+	}
+
+	if (access(EVENTS_SYSFILE, F_OK))
+		tst_brk(TCONF, "Kprobe events not supported by kernel");

... why we cannot check /sys/kernel/tracing/kprobe_events presence via .save_restore.

Quoted 7 lines

Well, yes, tracefs may need to be mounted during test setup. But .save_restore also assumes that sysfiles contain only one line while kprobe_events may hold arbitrarily long kprobe list. Also note that the restore function opens the sysfile with O_TRUNC, which might wipe all existing kprobes before creating them again. Therefore I've decided to check the file availability in setup() and send a targeted kprobe delete command instead of saving and restoring the whole sysfile.

Makes sense. Thanks for info.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw
In reply to Martin Doucha
Quoted 11 lines · Martin Doucha

Add test for reading basic fanotify events from a tracefs mount. The test will activate monitoring of FAN_MODIFY events from the whole mountpoint, create a new kprobe, activate it, deactivate it and remove it again. This should generate exactly 4 FAN_MODIFY events.

On some kernels with badly backported tracefs fixes, this may trigger NULL pointer dereference. The testcase is intentionally simple but sufficient to trigger this kind of bug. More complex tracing subtests can be added later.

[...]

Applied, thanks!

[1/2] tst_fs.h: Add TST_TRACEFS_MAGIC constant

      commit: a510fbdd2e85eb0d736a1fe498e03340d4af21fe
[2/2] Add test for fanotify monitoring of tracing filesystem
      commit: 6d941650ab4b629b3c27423edbda08597106db5f

Best regards,

Signature · 5 lines
Andrea Cervesato <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp