Skip to contentSkip to conversation

selftests/bpf: Fix d_path test

PATCH In review Jiri Olsa 4 participants 5 messages Aug 31, 2023 – Sep 5, 2023

Subsystems (6)
Kselftest BPF Tracing BPF selftests +2 more subsystems
lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH v2 1/1 selftests/bpf: Fix d_path test

Recent commit [1] broken d_path test, because now filp_close is not called directly from sys_close, but eventually later when the file is finally released.

As suggested by Hou Tao we don't need to re-hook the bpf program, but just instead we can use sys_close_range to trigger filp_close synchronously.

[1] 021a160abf62 ("fs: use __fput_sync in close(2)")

---
 .../testing/selftests/bpf/prog_tests/d_path.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

v2: call close_range through syscall call [Hou Tao]

tools/testing/selftests/bpf/prog_tests/d_path.c +18 -1
index 911345c526e6..ccc768592e66 100644
@@ -12,6 +12,17 @@
 #include "test_d_path_check_rdonly_mem.skel.h"
 #include "test_d_path_check_types.skel.h"
 
+/* sys_close_range is not around for long time, so let's
+ * make sure we can call it on systems with older glibc
+ */
+#ifndef __NR_close_range
+#ifdef __alpha__
+#define __NR_close_range 546
+#else
+#define __NR_close_range 436
+#endif
+#endif
+
 static int duration;
 
 static struct {
@@ -90,7 +101,11 @@ static int trigger_fstat_events(pid_t pid)
 	fstat(indicatorfd, &fileStat);
 
 out_close:
-	/* triggers filp_close */
+	/* sys_close no longer triggers filp_close, but we can
+	 * call sys_close_range instead which still does
+	 */
+#define close(fd) syscall(__NR_close_range, fd, fd, 0)
+
 	close(pipefd[0]);
 	close(pipefd[1]);
 	close(sockfd);
@@ -98,6 +113,8 @@ static int trigger_fstat_events(pid_t pid)
 	close(devfd);
 	close(localfd);
 	close(indicatorfd);
+
+#undef close
 	return ret;
 }
 
Signature · 1 line
2.41.0
maintainer
#1 lore raw
Quoted 10 lines · Jiri Olsa

Recent commit [1] broken d_path test, because now filp_close is not called directly from sys_close, but eventually later when the file is finally released.

As suggested by Hou Tao we don't need to re-hook the bpf program, but just instead we can use sys_close_range to trigger filp_close synchronously.

[1] 021a160abf62 ("fs: use __fput_sync in close(2)")

That did the trick, thanks everyone, applied!

maintainer
#2 lore raw
Quoted 6 lines · Daniel Borkmann
Quoted 4 lines · Jiri Olsa

Recent commit [1] broken d_path test, because now filp_close is not called directly from sys_close, but eventually later when the file is finally released.

Quoted 8 lines
Quoted 6 lines

As suggested by Hou Tao we don't need to re-hook the bpf program, but just instead we can use sys_close_range to trigger filp_close synchronously.

[1] 021a160abf62 ("fs: use __fput_sync in close(2)")

That did the trick, thanks everyone, applied!

I guess I am a bit late. But how about we use something like the following? I like this one better because it tests bpf_d_path() from retval at fexit.

Thanks,
Song

kernel/trace/bpf_trace.c +1 -0
index a7264b2c17ad..fe91836cedcd 100644
@@ -941,6 +941,7 @@ BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
 BTF_ID(func, filp_close)
+BTF_ID(func, close_fd_get_file)
 BTF_SET_END(btf_allowlist_d_path)
 
 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
(quoted diff) +0 -0

w/tools/testing/selftests/bpf/progs/test_d_path.c
index 84e1f883f97b..c880cfc95737 100644

tools/testing/selftests/bpf/progs/test_d_path.c +2 -2
@@ -40,8 +40,8 @@ int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
        return 0;
 }
 
-SEC("fentry/filp_close")
-int BPF_PROG(prog_close, struct file *file, void *id)
+SEC("fexit/close_fd_get_file")
+int BPF_PROG(close_fd_get_file, int fd, struct file *file /* retval */)
 {
        pid_t pid = bpf_get_current_pid_tgid() >> 32;
        __u32 cnt = cnt_close;
#3 lore raw
Quoted 10 lines · Song Liu
Quoted 9 lines · Daniel Borkmann
Quoted 7 lines · Jiri Olsa

Recent commit [1] broken d_path test, because now filp_close is not called directly from sys_close, but eventually later when the file is finally released.

As suggested by Hou Tao we don't need to re-hook the bpf program, but just instead we can use sys_close_range to trigger filp_close synchronously.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

[1] 021a160abf62 ("fs: use __fput_sync in close(2)")

That did the trick, thanks everyone, applied!

I guess I am a bit late. But how about we use something like the following? I like this one better because it tests bpf_d_path() from retval at fexit.

right, that would have been an option as well

Quoted 12 lines

Thanks,
Song

kernel/trace/bpf_trace.c +0 -0
index a7264b2c17ad..fe91836cedcd 100644

kernel/trace/bpf_trace.c BTF_ID(func, vfs_fallocate) · line 944

Quoted 4 lines
kernel/trace/bpf_trace.c +1 -0
@@ -941,6 +941,7 @@ BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
 BTF_ID(func, filp_close)
+BTF_ID(func, close_fd_get_file)

I liked using the close_range syscall because we did not need to add new allowed function.. however close_fd_get_file looks safe enough so I wouldn't mind changing that if you insist ;-)

jirka

Quoted 19 lines
kernel/trace/bpf_trace.c +0 -0
 BTF_SET_END(btf_allowlist_d_path)
 
 static bool bpf_d_path_allowed(const struct bpf_prog *prog)
(quoted diff) +0 -0

w/tools/testing/selftests/bpf/progs/test_d_path.c
index 84e1f883f97b..c880cfc95737 100644

tools/testing/selftests/bpf/progs/test_d_path.c +2 -2
@@ -40,8 +40,8 @@ int BPF_PROG(prog_stat, struct path *path, struct kstat *stat,
        return 0;
 }
 
-SEC("fentry/filp_close")
-int BPF_PROG(prog_close, struct file *file, void *id)
+SEC("fexit/close_fd_get_file")
+int BPF_PROG(close_fd_get_file, int fd, struct file *file /* retval */)
 {
        pid_t pid = bpf_get_current_pid_tgid() >> 32;
        __u32 cnt = cnt_close;
maintainer
#4 lore raw
Quoted 30 lines · Jiri Olsa
Quoted 10 lines · Song Liu
Quoted 9 lines · Daniel Borkmann
Quoted 7 lines · Jiri Olsa

Recent commit [1] broken d_path test, because now filp_close is not called directly from sys_close, but eventually later when the file is finally released.

As suggested by Hou Tao we don't need to re-hook the bpf program, but just instead we can use sys_close_range to trigger filp_close synchronously.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

[1] 021a160abf62 ("fs: use __fput_sync in close(2)")

That did the trick, thanks everyone, applied!

I guess I am a bit late. But how about we use something like the following? I like this one better because it tests bpf_d_path() from retval at fexit.

right, that would have been an option as well

Quoted 7 lines

Thanks,
Song

Quoted 13 lines

kernel/trace/bpf_trace.c BTF_ID(func, vfs_fallocate) · line 944

Quoted 9 lines
kernel/trace/bpf_trace.c +1 -0
index a7264b2c17ad..fe91836cedcd 100644
@@ -941,6 +941,7 @@ BTF_ID(func, vfs_fallocate)
 BTF_ID(func, dentry_open)
 BTF_ID(func, vfs_getattr)
 BTF_ID(func, filp_close)
+BTF_ID(func, close_fd_get_file)

I liked using the close_range syscall because we did not need to add new allowed function.. however close_fd_get_file looks safe enough so I wouldn't mind changing that if you insist ;-)

Let's use close_range() then. I will send another patch to test something with "struct file *" as retval on fexit.

Thanks,
Song