Skip to contentSkip to conversation

selftests: cachestat: fix build and run on older kernels

PATCH Superseded Andre Przywara 3 participants 13 messages Aug 15, 2023 – Aug 17, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover selftests: cachestat: fix build and run on older kernels

I ran all kernel selftests on some test machine, and stumbled upon cachestat failing (among others).
Those patches fix the cachestat test compilation and run on older kernels.

Also I found that the but-last test (on a normal file) fails when run on a tmpfs mounted directory, as it happens on an initramfs-only system, or when the current directory happens to be /dev/shm or /tmp:
# Create/open tmpfilecachestat
# Cachestat call returned 0
# Using cachestat: Cached: 4, Dirty: 4, Writeback: 0, Evicted: 0, Recently Evicted: 0 # Cachestat call (after fsync) returned 0
# Using cachestat: Cached: 4, Dirty: 4, Writeback: 0, Evicted: 0, Recently Evicted: 0 # Number of dirty should be zero after fsync.
not ok 6 cachestat fails with normal file

That same test binary succeeds on the same machine right afterwards if the current directory is changed to an ext4 filesystem.

I don't really know if this is expected, and whether we should try to figure out if the test file lives on a tmpfs filesystem, or whether the test itself is not strict enough, and requires more "flushing" (drop_caches?) to cover tmpfs directories as well.

Any ideas how to fix this would be appreciated.

Cheers,
Andre

Andre Przywara (3):
selftests: cachestat: properly link in librt
selftests: cachestat: use proper syscall number macro
selftests: cachestat: test for cachestat availability

 tools/testing/selftests/cachestat/Makefile    |  2 +-
 .../selftests/cachestat/test_cachestat.c      | 29 +++++++++++++++----
 2 files changed, 25 insertions(+), 6 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/3 selftests: cachestat: properly link in librt

Libraries should be listed last on the compiler's command line, so that
the linker can look for and find still unresolved symbols. The librt
library, required for the shm_* functions, was announced using CFLAGS,
which puts the library *before* the source files, and fails compilation
on my system:
======================
gcc -isystem /src/linux-selftests/usr/include -Wall -lrt test_cachestat.c
-o /src/linux-selftests/kselftest/cachestat/test_cachestat
/usr/bin/ld: /tmp/cceQWO3u.o: in function `test_cachestat_shmem':
test_cachestat.c:(.text+0x890): undefined reference to `shm_open'
/usr/bin/ld: test_cachestat.c:(.text+0x99c): undefined reference to `shm_unlink'
collect2: error: ld returned 1 exit status
make[4]: *** [../lib.mk:181: /src/linux-selftests/kselftest/cachestat/test_cachestat] Error 1
======================

Announce the library using the LDLIBS variable, which ensures the proper ordering on the command line.

---
 tools/testing/selftests/cachestat/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/cachestat/Makefile +1 -1
index fca73aaa7d141..778b54ebb0364 100644
@@ -3,6 +3,6 @@ TEST_GEN_PROGS := test_cachestat
 
 CFLAGS += $(KHDR_INCLUDES)
 CFLAGS += -Wall
-CFLAGS += -lrt
+LDLIBS += -lrt
 
 include ../lib.mk
Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH 1/3] selftests: cachestat: properly link in librt

Quoted 28 lines · Andre Przywara

Libraries should be listed last on the compiler's command line, so that
the linker can look for and find still unresolved symbols. The librt
library, required for the shm_* functions, was announced using CFLAGS,
which puts the library *before* the source files, and fails compilation
on my system:
======================
gcc -isystem /src/linux-selftests/usr/include -Wall -lrt test_cachestat.c
-o /src/linux-selftests/kselftest/cachestat/test_cachestat
/usr/bin/ld: /tmp/cceQWO3u.o: in function `test_cachestat_shmem':
test_cachestat.c:(.text+0x890): undefined reference to `shm_open'
/usr/bin/ld: test_cachestat.c:(.text+0x99c): undefined reference to `shm_unlink'
collect2: error: ld returned 1 exit status
make[4]: *** [../lib.mk:181: /src/linux-selftests/kselftest/cachestat/test_cachestat] Error 1
======================

Announce the library using the LDLIBS variable, which ensures the proper ordering on the command line.

---
  tools/testing/selftests/cachestat/Makefile | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/cachestat/Makefile +0 -0
index fca73aaa7d141..778b54ebb0364 100644

tools/testing/selftests/cachestat/Makefile TEST_GEN_PROGS := test_cachestat · line 8

Quoted 7 lines
tools/testing/selftests/cachestat/Makefile +1 -1
@@ -3,6 +3,6 @@ TEST_GEN_PROGS := test_cachestat
 
  CFLAGS += $(KHDR_INCLUDES)
  CFLAGS += -Wall
-CFLAGS += -lrt
+LDLIBS += -lrt
 
  include ../lib.mk

Thank you. Applied to linux-kselftest next for Linux 6.6-rc1

thanks,
-- Shuah

#3 lore raw

PATCH 2/3 selftests: cachestat: use proper syscall number macro

In reply to Andre Przywara

At the moment the cachestat syscall number is hard coded into the test source code.
Remove that and replace it with the proper __NR_cachestat macro. That ensures compatibility should other architectures pick a different number.

---
 tools/testing/selftests/cachestat/test_cachestat.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
tools/testing/selftests/cachestat/test_cachestat.c +3 -4
index 54d09b820ed4b..a5a4ac8dcb76c 100644
@@ -19,7 +19,6 @@ static const char * const dev_files[] = {
 	"/dev/zero", "/dev/null", "/dev/urandom",
 	"/proc/version", "/proc"
 };
-static const int cachestat_nr = 451;
 
 void print_cachestat(struct cachestat *cs)
 {
@@ -126,7 +125,7 @@ bool test_cachestat(const char *filename, bool write_random, bool create,
 		}
 	}
 
-	syscall_ret = syscall(cachestat_nr, fd, &cs_range, &cs, 0);
+	syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
 	ksft_print_msg("Cachestat call returned %ld\n", syscall_ret);
 
@@ -152,7 +151,7 @@ bool test_cachestat(const char *filename, bool write_random, bool create,
 			ksft_print_msg("fsync fails.\n");
 			ret = false;
 		} else {
-			syscall_ret = syscall(cachestat_nr, fd, &cs_range, &cs, 0);
+			syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
 			ksft_print_msg("Cachestat call (after fsync) returned %ld\n",
 				syscall_ret);
@@ -213,7 +212,7 @@ bool test_cachestat_shmem(void)
 		goto close_fd;
 	}
 
-	syscall_ret = syscall(cachestat_nr, fd, &cs_range, &cs, 0);
+	syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
 	if (syscall_ret) {
 		ksft_print_msg("Cachestat returned non-zero.\n");
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] selftests: cachestat: use proper syscall number macro

Quoted 52 lines · Andre Przywara

At the moment the cachestat syscall number is hard coded into the test source code.
Remove that and replace it with the proper __NR_cachestat macro. That ensures compatibility should other architectures pick a different number.

---
 tools/testing/selftests/cachestat/test_cachestat.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
tools/testing/selftests/cachestat/test_cachestat.c +3 -5
index 54d09b820ed4b..a5a4ac8dcb76c 100644
@@ -19,7 +19,6 @@ static const char * const dev_files[] = {
        "/dev/zero", "/dev/null", "/dev/urandom",
        "/proc/version", "/proc"
 };
-static const int cachestat_nr = 451;
 
 void print_cachestat(struct cachestat *cs)
 {
@@ -126,7 +125,7 @@ bool test_cachestat(const char *filename, bool write_random, bool create,
                }
        }
 
-       syscall_ret = syscall(cachestat_nr, fd, &cs_range, &cs, 0);
+       syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
        ksft_print_msg("Cachestat call returned %ld\n", syscall_ret);
 
@@ -152,7 +151,7 @@ bool test_cachestat(const char *filename, bool write_random, bool create,
                        ksft_print_msg("fsync fails.\n");
                        ret = false;
                } else {
-                       syscall_ret = syscall(cachestat_nr, fd, &cs_range, &cs, 0);
+                       syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
                        ksft_print_msg("Cachestat call (after fsync) returned %ld\n",
                                syscall_ret);
@@ -213,7 +212,7 @@ bool test_cachestat_shmem(void)
                goto close_fd;
        }
 
-       syscall_ret = syscall(cachestat_nr, fd, &cs_range, &cs, 0);
+       syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
        if (syscall_ret) {
                ksft_print_msg("Cachestat returned non-zero.\n");
--
Quoted 2 lines

2.25.1

Oops something I forgot to fix. Thanks, Andre!

maintainer
#5 lore raw

Re: [PATCH 2/3] selftests: cachestat: use proper syscall number macro

In reply to Andre Przywara
Quoted 42 lines · Andre Przywara

At the moment the cachestat syscall number is hard coded into the test source code.
Remove that and replace it with the proper __NR_cachestat macro. That ensures compatibility should other architectures pick a different number.

---
  tools/testing/selftests/cachestat/test_cachestat.c | 7 +++----
  1 file changed, 3 insertions(+), 4 deletions(-)
tools/testing/selftests/cachestat/test_cachestat.c +2 -3
index 54d09b820ed4b..a5a4ac8dcb76c 100644
@@ -19,7 +19,6 @@ static const char * const dev_files[] = {
  	"/dev/zero", "/dev/null", "/dev/urandom",
  	"/proc/version", "/proc"
  };
-static const int cachestat_nr = 451;
 
  void print_cachestat(struct cachestat *cs)
  {
@@ -126,7 +125,7 @@ bool test_cachestat(const char *filename, bool write_random, bool create,
  		}
  	}
 
-	syscall_ret = syscall(cachestat_nr, fd, &cs_range, &cs, 0);
+	syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
  	ksft_print_msg("Cachestat call returned %ld\n", syscall_ret);
 
@@ -152,7 +151,7 @@ bool test_cachestat(const char *filename, bool write_random, bool create,
  			ksft_print_msg("fsync fails.\n");
  			ret = false;
  		} else {
-			syscall_ret = syscall(cachestat_nr, fd, &cs_range, &cs, 0);
+			syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
  			ksft_print_msg("Cachestat call (after fsync) returned %ld\n",
  				syscall_ret);

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 218

Quoted 8 lines
tools/testing/selftests/cachestat/test_cachestat.c +1 -1
@@ -213,7 +212,7 @@ bool test_cachestat_shmem(void)
  		goto close_fd;
  	}
 
-	syscall_ret = syscall(cachestat_nr, fd, &cs_range, &cs, 0);
+	syscall_ret = syscall(__NR_cachestat, fd, &cs_range, &cs, 0);
 
  	if (syscall_ret) {
  		ksft_print_msg("Cachestat returned non-zero.\n");

Thank you. Applied to linux-kselftest next for Linux 6.6-rc1

thanks,
-- Shuah

#6 lore raw

PATCH 3/3 selftests: cachestat: test for cachestat availability

In reply to Andre Przywara

As cachestat is a new syscall, it won't be available on older kernels, for instance those running on a build machine. In this case, a run reports all tests as "not ok" at the moment.

Test for the cachestat syscall availability first, before doing further tests, and bail out early with a TAP SKIP comment.

This also uses the opportunity to add the proper TAP headers, and add one check for the syscall error handling (illegal file descriptor).

---
 .../selftests/cachestat/test_cachestat.c      | 22 ++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
tools/testing/selftests/cachestat/test_cachestat.c +21 -1
index a5a4ac8dcb76c..77620e7ecf562 100644
@@ -15,6 +15,8 @@
 
 #include "../kselftest.h"
 
+#define NR_TESTS	8
+
 static const char * const dev_files[] = {
 	"/dev/zero", "/dev/null", "/dev/urandom",
 	"/proc/version", "/proc"
@@ -235,7 +237,25 @@ bool test_cachestat_shmem(void)
 
 int main(void)
 {
-	int ret = 0;
+	int ret;
+
+	ksft_print_header();
+
+	ret = syscall(__NR_cachestat, -1, NULL, NULL, 0);
+	if (ret == -1 && errno == ENOSYS) {
+		printf("1..0 # Skipped: cachestat syscall not available\n");
+		return KSFT_SKIP;
+	}
+
+	ksft_set_plan(NR_TESTS);
+
+	if (ret == -1 && errno == EBADF) {
+		ksft_test_result_pass("bad file descriptor recognized\n");
+		ret = 0;
+	} else {
+		ksft_test_result_fail("bad file descriptor ignored\n");
+		ret = 1;
+	}
 
 	for (int i = 0; i < 5; i++) {
 		const char *dev_filename = dev_files[i];
Signature · 1 line
2.25.1
maintainer Acked-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] selftests: cachestat: test for cachestat availability

Quoted 4 lines · Andre Przywara

As cachestat is a new syscall, it won't be available on older kernels, for instance those running on a build machine. In this case, a run reports all tests as "not ok" at the moment.

Interesting - I was under the assumption that if you backported the selftests for cachestat, you would also backport the syscall's implementation and wiring.

But yeah, I guess if you build with !CONFIG_CACHESTAT_SYSCALL, these tests would fail.

Quoted 6 lines

Test for the cachestat syscall availability first, before doing further tests, and bail out early with a TAP SKIP comment.

This also uses the opportunity to add the proper TAP headers, and add one check for the syscall error handling (illegal file descriptor).

Thanks for the addition!

Quoted 20 lines
---
 .../selftests/cachestat/test_cachestat.c      | 22 ++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
tools/testing/selftests/cachestat/test_cachestat.c +2 -0
index a5a4ac8dcb76c..77620e7ecf562 100644
@@ -15,6 +15,8 @@
 
 #include "../kselftest.h"
 
+#define NR_TESTS       8
+
 static const char * const dev_files[] = {
        "/dev/zero", "/dev/null", "/dev/urandom",
        "/proc/version", "/proc"

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 245

Quoted 10 lines
tools/testing/selftests/cachestat/test_cachestat.c +6 -1
@@ -235,7 +237,25 @@ bool test_cachestat_shmem(void)
 
 int main(void)
 {
-       int ret = 0;
+       int ret;
+
+       ksft_print_header();
+
+       ret = syscall(__NR_cachestat, -1, NULL, NULL, 0);
+       if (ret == -1 && errno == ENOSYS) {

nit: if (ret && errno == ENOSYS) sounds cleaner, but up to you.

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 246

Quoted 1 line
tools/testing/selftests/cachestat/test_cachestat.c +1 -0
+               printf("1..0 # Skipped: cachestat syscall not available\n");

nit: perhaps ksft_print_msg()?

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 258

Quoted 12 lines
tools/testing/selftests/cachestat/test_cachestat.c +12 -0
+               return KSFT_SKIP;
+       }
+
+       ksft_set_plan(NR_TESTS);
+
+       if (ret == -1 && errno == EBADF) {
+               ksft_test_result_pass("bad file descriptor recognized\n");
+               ret = 0;
+       } else {
+               ksft_test_result_fail("bad file descriptor ignored\n");
+               ret = 1;
+       }

Nice!

Quoted 6 lines
tools/testing/selftests/cachestat/test_cachestat.c +0 -1
        for (int i = 0; i < 5; i++) {
                const char *dev_filename = dev_files[i];
--

2.25.1

Nitpicking aside:

#8 lore raw

Re: [PATCH 3/3] selftests: cachestat: test for cachestat availability

On Tue, 15 Aug 2023 16:25:54 -0700
Nhat Pham <[email protected]> wrote:

Hi Nhat,

many thanks for having a look!

Quoted 8 lines · Andre Przywara
Quoted 4 lines

As cachestat is a new syscall, it won't be available on older kernels, for instance those running on a build machine. In this case, a run reports all tests as "not ok" at the moment.

Interesting - I was under the assumption that if you backported the selftests for cachestat, you would also backport the syscall's implementation and wiring.

Well, just running the tests on the kernel you just built is only one use case. I build the tests from latest git HEAD, then copy them to a target system with some kernel running. Or I just build the tests and run them for regression testing on my build system with a distro kernel, which is Ubuntu's 5.15 flavour, in my case.
The documentation explicitly mentions that selftests should work on older kernels (copying the normal userland compatibility requirements), check the second paragraph of Documentation/dev-tools/kselftest.rst.

Quoted 30 lines

But yeah, I guess if you build with !CONFIG_CACHESTAT_SYSCALL, these tests would fail.

Quoted 6 lines

Test for the cachestat syscall availability first, before doing further tests, and bail out early with a TAP SKIP comment.

This also uses the opportunity to add the proper TAP headers, and add one check for the syscall error handling (illegal file descriptor).

Thanks for the addition!

Quoted 21 lines
---
 .../selftests/cachestat/test_cachestat.c      | 22 ++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
tools/testing/selftests/cachestat/test_cachestat.c +2 -0
index a5a4ac8dcb76c..77620e7ecf562 100644
@@ -15,6 +15,8 @@
 
 #include "../kselftest.h"
 
+#define NR_TESTS       8
+
 static const char * const dev_files[] = {
        "/dev/zero", "/dev/null", "/dev/urandom",
        "/proc/version", "/proc"
@@ -235,7 +237,25 @@ bool test_cachestat_shmem(void)
 
Quoted 10 lines

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 245

Quoted 9 lines
tools/testing/selftests/cachestat/test_cachestat.c +6 -1
 int main(void)
 {
-       int ret = 0;
+       int ret;
+
+       ksft_print_header();
+
+       ret = syscall(__NR_cachestat, -1, NULL, NULL, 0);
+       if (ret == -1 && errno == ENOSYS) {  

nit: if (ret && errno == ENOSYS) sounds cleaner, but up to you.

Do you ever return a positive value other than 0? I think technically errno is only valid when the return value is -1, so in the error case, which I wanted to test here explicitly.
Some syscall selftests (I checked landlock the other day) do very elaborate testing of the error path, trying to carefully cover all corner cases:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/landlock/base_test.c#n24

So this test was inspired by that, but I didn't want to go that far here ;-)

Quoted 2 lines

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 246

Quoted 1 line
tools/testing/selftests/cachestat/test_cachestat.c +1 -0
+               printf("1..0 # Skipped: cachestat syscall not available\n");  

nit: perhaps ksft_print_msg()?

Ah, yes, of course!

Quoted 14 lines

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 258

Quoted 12 lines
tools/testing/selftests/cachestat/test_cachestat.c +12 -0
+               return KSFT_SKIP;
+       }
+
+       ksft_set_plan(NR_TESTS);
+
+       if (ret == -1 && errno == EBADF) {
+               ksft_test_result_pass("bad file descriptor recognized\n");
+               ret = 0;
+       } else {
+               ksft_test_result_fail("bad file descriptor ignored\n");
+               ret = 1;
+       }  

Nice!

Quoted 1 line
Quoted 7 lines
Quoted 5 lines
        for (int i = 0; i < 5; i++) {
                const char *dev_filename = dev_files[i];

--
2.25.1

Nitpicking aside:

Thanks, I will send a v2 later, using ksft_print_msg(). But first I will try if I can detect a tmpfs instance without boiling the ocean.

Cheers,
Andre

maintainer
#9 lore raw

Re: [PATCH 3/3] selftests: cachestat: test for cachestat availability

In reply to Andre Przywara
Quoted 31 lines · Andre Przywara

As cachestat is a new syscall, it won't be available on older kernels, for instance those running on a build machine. In this case, a run reports all tests as "not ok" at the moment.

Test for the cachestat syscall availability first, before doing further tests, and bail out early with a TAP SKIP comment.

This also uses the opportunity to add the proper TAP headers, and add one check for the syscall error handling (illegal file descriptor).

---
  .../selftests/cachestat/test_cachestat.c      | 22 ++++++++++++++++++-
  1 file changed, 21 insertions(+), 1 deletion(-)
tools/testing/selftests/cachestat/test_cachestat.c +2 -0
index a5a4ac8dcb76c..77620e7ecf562 100644
@@ -15,6 +15,8 @@
 
  #include "../kselftest.h"
 
+#define NR_TESTS	8
+
  static const char * const dev_files[] = {
  	"/dev/zero", "/dev/null", "/dev/urandom",
  	"/proc/version", "/proc"
@@ -235,7 +237,25 @@ bool test_cachestat_shmem(void)
 
  int main(void)

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 247

Quoted 10 lines
tools/testing/selftests/cachestat/test_cachestat.c +8 -1
  {
-	int ret = 0;
+	int ret;
+
+	ksft_print_header();
+
+	ret = syscall(__NR_cachestat, -1, NULL, NULL, 0);
+	if (ret == -1 && errno == ENOSYS) {
+		printf("1..0 # Skipped: cachestat syscall not available\n");
+		return KSFT_SKIP;

What happens when other errors besides ENOSYS? The test shouldn't continue.

Quoted 4 lines
tools/testing/selftests/cachestat/test_cachestat.c +4 -0
+	}
+
+	ksft_set_plan(NR_TESTS);
+

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 261

Quoted 10 lines
tools/testing/selftests/cachestat/test_cachestat.c +7 -0
+	if (ret == -1 && errno == EBADF) {
+		ksft_test_result_pass("bad file descriptor recognized\n");
+		ret = 0;
+	} else {
+		ksft_test_result_fail("bad file descriptor ignored\n");
+		ret = 1;
+	}
 
  	for (int i = 0; i < 5; i++) {
  		const char *dev_filename = dev_files[i];

thanks,
-- Shuah

#10 lore raw

Re: [PATCH 3/3] selftests: cachestat: test for cachestat availability

On Wed, 16 Aug 2023 11:11:49 -0600
Shuah Khan <[email protected]> wrote:

Hi,

Quoted 31 lines · Andre Przywara
Quoted 30 lines

As cachestat is a new syscall, it won't be available on older kernels, for instance those running on a build machine. In this case, a run reports all tests as "not ok" at the moment.

Test for the cachestat syscall availability first, before doing further tests, and bail out early with a TAP SKIP comment.

This also uses the opportunity to add the proper TAP headers, and add one check for the syscall error handling (illegal file descriptor).

---
  .../selftests/cachestat/test_cachestat.c      | 22 ++++++++++++++++++-
  1 file changed, 21 insertions(+), 1 deletion(-)
tools/testing/selftests/cachestat/test_cachestat.c +2 -0
index a5a4ac8dcb76c..77620e7ecf562 100644
@@ -15,6 +15,8 @@
 
  #include "../kselftest.h"
 
+#define NR_TESTS	8
+
  static const char * const dev_files[] = {
  	"/dev/zero", "/dev/null", "/dev/urandom",
  	"/proc/version", "/proc"
@@ -235,7 +237,25 @@ bool test_cachestat_shmem(void)
 
Quoted 13 lines

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 247

Quoted 11 lines
tools/testing/selftests/cachestat/test_cachestat.c +8 -1
  int main(void)
  {
-	int ret = 0;
+	int ret;
+
+	ksft_print_header();
+
+	ret = syscall(__NR_cachestat, -1, NULL, NULL, 0);
+	if (ret == -1 && errno == ENOSYS) {
+		printf("1..0 # Skipped: cachestat syscall not available\n");
+		return KSFT_SKIP;  

What happens when other errors besides ENOSYS? The test shouldn't continue.

-1 is an illegal file descriptor, and this is checked below (still using the same ret and errno), but reported using the normal framework. This check above is done early, before we even announce the plan, so that we can skip *all* of the tests, since they don't make any sense when the syscall is not available at all.

Does that make sense?

Cheers,
Andre

Quoted 18 lines
Quoted 5 lines
tools/testing/selftests/cachestat/test_cachestat.c +4 -0
+	}
+
+	ksft_set_plan(NR_TESTS);
+

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 261

Quoted 10 lines
tools/testing/selftests/cachestat/test_cachestat.c +7 -0
+	if (ret == -1 && errno == EBADF) {
+		ksft_test_result_pass("bad file descriptor recognized\n");
+		ret = 0;
+	} else {
+		ksft_test_result_fail("bad file descriptor ignored\n");
+		ret = 1;
+	}
 
  	for (int i = 0; i < 5; i++) {
  		const char *dev_filename = dev_files[i];  

thanks,
-- Shuah

maintainer
#11 lore raw

Re: [PATCH 3/3] selftests: cachestat: test for cachestat availability

Quoted 50 lines · Andre Przywara

On Wed, 16 Aug 2023 11:11:49 -0600
Shuah Khan <[email protected]> wrote:

Hi,

Quoted 31 lines · Andre Przywara
Quoted 30 lines

As cachestat is a new syscall, it won't be available on older kernels, for instance those running on a build machine. In this case, a run reports all tests as "not ok" at the moment.

Test for the cachestat syscall availability first, before doing further tests, and bail out early with a TAP SKIP comment.

This also uses the opportunity to add the proper TAP headers, and add one check for the syscall error handling (illegal file descriptor).

---
   .../selftests/cachestat/test_cachestat.c      | 22 ++++++++++++++++++-
   1 file changed, 21 insertions(+), 1 deletion(-)
tools/testing/selftests/cachestat/test_cachestat.c +2 -0
index a5a4ac8dcb76c..77620e7ecf562 100644
@@ -15,6 +15,8 @@
 
   #include "../kselftest.h"
 
+#define NR_TESTS	8
+
   static const char * const dev_files[] = {
   	"/dev/zero", "/dev/null", "/dev/urandom",
   	"/proc/version", "/proc"
@@ -235,7 +237,25 @@ bool test_cachestat_shmem(void)
 
Quoted 13 lines

tools/testing/selftests/cachestat/test_cachestat.c bool test_cachestat_shmem(void) · line 247

Quoted 11 lines
tools/testing/selftests/cachestat/test_cachestat.c +8 -1
   int main(void)
   {
-	int ret = 0;
+	int ret;
+
+	ksft_print_header();
+
+	ret = syscall(__NR_cachestat, -1, NULL, NULL, 0);
+	if (ret == -1 && errno == ENOSYS) {
+		printf("1..0 # Skipped: cachestat syscall not available\n");
+		return KSFT_SKIP;

What happens when other errors besides ENOSYS? The test shouldn't continue.

Quoted 8 lines

-1 is an illegal file descriptor, and this is checked below (still using the same ret and errno), but reported using the normal framework. This check above is done early, before we even announce the plan, so that we can skip *all* of the tests, since they don't make any sense when the syscall is not available at all.

Does that make sense?

Yup. I will apply this for Linux 6.6-rc1. You will get patchbot notification shortly.

thanks,
-- Shuah

maintainer
#12 lore raw
In reply to Andre Przywara
Quoted 17 lines · Andre Przywara

I ran all kernel selftests on some test machine, and stumbled upon cachestat failing (among others).
Those patches fix the cachestat test compilation and run on older kernels.

Also I found that the but-last test (on a normal file) fails when run on a tmpfs mounted directory, as it happens on an initramfs-only system, or when the current directory happens to be /dev/shm or /tmp:
# Create/open tmpfilecachestat
# Cachestat call returned 0
# Using cachestat: Cached: 4, Dirty: 4, Writeback: 0, Evicted: 0, Recently Evicted: 0 # Cachestat call (after fsync) returned 0
# Using cachestat: Cached: 4, Dirty: 4, Writeback: 0, Evicted: 0, Recently Evicted: 0 # Number of dirty should be zero after fsync.
not ok 6 cachestat fails with normal file

Quoted 2 lines

That same test binary succeeds on the same machine right afterwards if the current directory is changed to an ext4 filesystem.

Ah, if I recall correctly, these kinds of fs have no-op fsync, correct? Something along the line of:
https://github.com/torvalds/linux/blob/91aa6c4/mm/shmem.c#L4111
The fsync logic would fail indeed. Thanks for pointing that out!

Quoted 3 lines

I don't really know if this is expected, and whether we should try to figure out if the test file lives on a tmpfs filesystem, or whether the

This would be nice. I think there's a userspace method to check this, right? There's a TMPFS_MAGIC here - not sure if relevant:

https://man7.org/linux/man-pages/man2/statfs.2.html

Quoted 20 lines

test itself is not strict enough, and requires more "flushing" (drop_caches?) to cover tmpfs directories as well.

Any ideas how to fix this would be appreciated.

Cheers,
Andre

Andre Przywara (3):
selftests: cachestat: properly link in librt
selftests: cachestat: use proper syscall number macro
selftests: cachestat: test for cachestat availability

 tools/testing/selftests/cachestat/Makefile    |  2 +-
 .../selftests/cachestat/test_cachestat.c      | 29 +++++++++++++++----
 2 files changed, 25 insertions(+), 6 deletions(-)

--
2.25.1