Skip to contentSkip to conversation

selftests/bpf: Fix d_path test

PATCH Superseded Jiri Olsa 4 participants 5 messages Aug 31, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

PATCH 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)")

---
 tools/testing/selftests/bpf/prog_tests/d_path.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/d_path.c +7 -1
index 911345c526e6..81e34a4a05d1 100644
@@ -90,7 +90,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) close_range(fd, fd, 0)
+
 	close(pipefd[0]);
 	close(pipefd[1]);
 	close(sockfd);
@@ -98,6 +102,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 19 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)")

---
  tools/testing/selftests/bpf/prog_tests/d_path.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
index 911345c526e6..81e34a4a05d1 100644

tools/testing/selftests/bpf/prog_tests/d_path.c static int trigger_fstat_events(pid_t pid) · line 97

Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +5 -1
@@ -90,7 +90,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) close_range(fd, fd, 0)
+

The BPF CI selftest build says:

[...]
TEST-OBJ [test_progs] lookup_key.test.o
TEST-OBJ [test_progs] migrate_reuseport.test.o
TEST-OBJ [test_progs] user_ringbuf.test.o
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c: In function ‘trigger_fstat_events’:
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c:96:19: error: implicit declaration of function ‘close_range’ [-Werror=implicit-function-declaration]

     96 | #define close(fd) close_range(fd, fd, 0)
        |                   ^~~~~~~~~~~
  /tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c:98:2: note: in expansion of macro ‘close’
     98 |  close(pipefd[0]);
        |  ^~~~~

TEST-OBJ [test_progs] task_pt_regs.test.o
[...]

Perhaps #include <linux/close_range.h> missing ?

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +2 -0
  	close(pipefd[0]);
  	close(pipefd[1]);
  	close(sockfd);
@@ -98,6 +102,8 @@ static int trigger_fstat_events(pid_t pid)
  	close(devfd);
  	close(localfd);
  	close(indicatorfd);
+
+#undef close
  	return ret;
  }
 
#2 lore raw
Quoted 42 lines · Daniel Borkmann
Quoted 19 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)")

---
  tools/testing/selftests/bpf/prog_tests/d_path.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
index 911345c526e6..81e34a4a05d1 100644

tools/testing/selftests/bpf/prog_tests/d_path.c static int trigger_fstat_events(pid_t pid) · line 96

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +5 -1
@@ -90,7 +90,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) close_range(fd, fd, 0)
+

The BPF CI selftest build says:

[...]
TEST-OBJ [test_progs] lookup_key.test.o
TEST-OBJ [test_progs] migrate_reuseport.test.o
TEST-OBJ [test_progs] user_ringbuf.test.o
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c: In function ‘trigger_fstat_events’:
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c:96:19: error: implicit declaration of function ‘close_range’ [-Werror=implicit-function-declaration]

     96 | #define close(fd) close_range(fd, fd, 0)
        |                   ^~~~~~~~~~~
  /tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c:98:2: note: in expansion of macro ‘close’
     98 |  close(pipefd[0]);
        |  ^~~~~
Quoted 4 lines

TEST-OBJ [test_progs] task_pt_regs.test.o
[...]

Perhaps #include <linux/close_range.h> missing ?

yes, will send v2

thanks,
jirka

Quoted 14 lines
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +2 -0
  	close(pipefd[0]);
  	close(pipefd[1]);
  	close(sockfd);
@@ -98,6 +102,8 @@ static int trigger_fstat_events(pid_t pid)
  	close(devfd);
  	close(localfd);
  	close(indicatorfd);
+
+#undef close
  	return ret;
  }
 
#3 lore raw
In reply to Daniel Borkmann

Hi,

Quoted 54 lines · Daniel Borkmann
Quoted 28 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)")

---
  tools/testing/selftests/bpf/prog_tests/d_path.c | 8 +++++++-

  1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
index 911345c526e6..81e34a4a05d1 100644
@@ -90,7 +90,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 +     */

tools/testing/selftests/bpf/prog_tests/d_path.c

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +2 -0
+#define close(fd) close_range(fd, fd, 0)
+

The BPF CI selftest build says:

    [...]
    TEST-OBJ [test_progs] lookup_key.test.o
    TEST-OBJ [test_progs] migrate_reuseport.test.o
    TEST-OBJ [test_progs] user_ringbuf.test.o /tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c:
In function ‘trigger_fstat_events’:
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c:96:19:
error: implicit declaration of function ‘close_range’ [-Werror=implicit-function-declaration]
     96 | #define close(fd) close_range(fd, fd, 0)
        |                   ^~~~~~~~~~~
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c:98:2:
note: in expansion of macro ‘close’
     98 |  close(pipefd[0]);
        |  ^~~~~
    TEST-OBJ [test_progs] task_pt_regs.test.o
    [...]

Quoted 1 line

Perhaps #include <linux/close_range.h> missing ?

Including <linux/close_range.h> doesn't help because it only defines two macros.

I got the same error when testing locally. It seems close_range() was introduced by glibc 2.34 [1] and it was defined in <unistd.h>, but the version of glibc in my local VM is 2.29. I modify d_path locally to call close_range through syscall():

(quoted diff) +0 -0

b/tools/testing/selftests/bpf/prog_tests/d_path.c
index 81e34a4a05d1..c5811843ce7e 100644

tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
@@ -12,6 +12,14 @@

 #include "test_d_path_check_rdonly_mem.skel.h"
 #include "test_d_path_check_types.skel.h"

+#ifndef __NR_close_range
+#ifdef __alpha__
+#define __NR_close_range 546
+#else
+#define __NR_close_range 436
+#endif
+#endif
+
 static int duration;

 static struct {
@@ -93,7 +101,7 @@ static int trigger_fstat_events(pid_t pid) /* sys_close no longer triggers filp_close, but we can
         * call sys_close_range instead which still does */
-#define close(fd) close_range(fd, fd, 0)
+#define close(fd) syscall(__NR_close_range, fd, fd, 0)

[1]: 9b4feb630e8e arch: wire-up close_range()

Quoted 13 lines
Quoted 13 lines

      close(pipefd[0]);
      close(pipefd[1]);
      close(sockfd);

tools/testing/selftests/bpf/prog_tests/d_path.c +2 -0
@@ -98,6 +102,8 @@ static int trigger_fstat_events(pid_t pid)
       close(devfd);
       close(localfd);
       close(indicatorfd);
+
+#undef close
       return ret;
   }

#4 lore raw
Quoted 57 lines · Hou Tao

Hi,

Quoted 54 lines · Daniel Borkmann
Quoted 28 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)")

---
  tools/testing/selftests/bpf/prog_tests/d_path.c | 8 +++++++-

  1 file changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/d_path.c +0 -0
index 911345c526e6..81e34a4a05d1 100644
@@ -90,7 +90,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 +     */

tools/testing/selftests/bpf/prog_tests/d_path.c

Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +2 -0
+#define close(fd) close_range(fd, fd, 0)
+

The BPF CI selftest build says:

    [...]
    TEST-OBJ [test_progs] lookup_key.test.o
    TEST-OBJ [test_progs] migrate_reuseport.test.o
    TEST-OBJ [test_progs] user_ringbuf.test.o /tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c:
In function ‘trigger_fstat_events’:
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c:96:19:
error: implicit declaration of function ‘close_range’ [-Werror=implicit-function-declaration]
     96 | #define close(fd) close_range(fd, fd, 0)
        |                   ^~~~~~~~~~~
/tmp/work/bpf/bpf/tools/testing/selftests/bpf/prog_tests/d_path.c:98:2:
note: in expansion of macro ‘close’
     98 |  close(pipefd[0]);
        |  ^~~~~
    TEST-OBJ [test_progs] task_pt_regs.test.o
    [...]

Quoted 4 lines
Quoted 1 line

Perhaps #include <linux/close_range.h> missing ?

Including <linux/close_range.h> doesn't help because it only defines two macros.

right, just saw that ;-)

Quoted 27 lines

I got the same error when testing locally. It seems close_range() was introduced by glibc 2.34 [1] and it was defined in <unistd.h>, but the version of glibc in my local VM is 2.29. I modify d_path locally to call close_range through syscall():

tools/testing/selftests/bpf/prog_tests/d_path.c +8 -0
index 81e34a4a05d1..c5811843ce7e 100644
@@ -12,6 +12,14 @@
  #include "test_d_path_check_rdonly_mem.skel.h"
  #include "test_d_path_check_types.skel.h"
 
+#ifndef __NR_close_range
+#ifdef __alpha__
+#define __NR_close_range 546
+#else
+#define __NR_close_range 436
+#endif
+#endif
+
  static int duration;
 
  static struct {

tools/testing/selftests/bpf/prog_tests/d_path.c static int trigger_fstat_events(pid_t pid) · line 106

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/d_path.c +1 -1
@@ -93,7 +101,7 @@ static int trigger_fstat_events(pid_t pid)
         /* sys_close no longer triggers filp_close, but we can
          * call sys_close_range instead which still does
          */
-#define close(fd) close_range(fd, fd, 0)
+#define close(fd) syscall(__NR_close_range, fd, fd, 0)
 
 [1]: 9b4feb630e8e arch: wire-up close_range()

nice, thanks for all the info

jirka

Quoted 16 lines
Quoted 15 lines
Quoted 15 lines

      close(pipefd[0]);
      close(pipefd[1]);
      close(sockfd);

tools/testing/selftests/bpf/prog_tests/d_path.c +2 -0
@@ -98,6 +102,8 @@ static int trigger_fstat_events(pid_t pid)
       close(devfd);
       close(localfd);
       close(indicatorfd);
+
+#undef close
       return ret;
   }