Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selftests/cachestat: test unaligned and zero-length ranges
@ 2026-12-04 22:26 Preetam Sundar Das
  2026-12-04 22:39 ` Nhat Pham
  0 siblings, 1 reply; 2+ messages in thread
From: Preetam Sundar Das @ 2026-12-04 22:26 UTC (permalink / raw)
  To: nphamcs; +Cc: hannes, shuah, linux-mm, linux-kselftest, Preetam Sundar Das

Signed-off-by: Preetam Sundar Das <daspreetam4@gmail.com>
---
 .../selftests/cachestat/test_cachestat.c      | 101 +++++++++++++++---
 1 file changed, 89 insertions(+), 12 deletions(-)

diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/testing/selftests/cachestat/test_cachestat.c
index 542cd09cb443..c7aa58bdcf7a 100644
--- a/tools/testing/selftests/cachestat/test_cachestat.c
+++ b/tools/testing/selftests/cachestat/test_cachestat.c
@@ -18,7 +18,7 @@
 
 #include "kselftest.h"
 
-#define NR_TESTS	9
+#define NR_TESTS	12
 
 static const char * const dev_files[] = {
 	"/dev/zero", "/dev/null", "/dev/urandom",
@@ -121,8 +121,8 @@ static bool is_on_tmpfs(int fd)
  * pages.
  */
 static int test_cachestat(const char *filename, bool write_random, bool create,
-			  bool test_fsync, unsigned long num_pages,
-			  int open_flags, mode_t open_mode)
+			  bool test_fsync, bool allow_eperm,
+			  unsigned long num_pages, int open_flags, mode_t open_mode)
 {
 	size_t PS = sysconf(_SC_PAGESIZE);
 	int filesize = num_pages * PS;
@@ -154,10 +154,14 @@ static int test_cachestat(const char *filename, bool write_random, bool create,
 	ksft_print_msg("Cachestat call returned %ld\n", syscall_ret);
 
 	if (syscall_ret) {
-		ksft_print_msg("Cachestat returned non-zero.\n");
-		ret = KSFT_FAIL;
-		goto out1;
-
+		if (allow_eperm && errno == EPERM) {
+			ksft_print_msg("Cachestat denied by permission check.\n");
+		} else {
+			ksft_print_msg("Cachestat returned non-zero: %s.\n",
+				       strerror(errno));
+			ret = KSFT_FAIL;
+			goto out1;
+		}
 	} else {
 		print_cachestat(&cs);
 
@@ -206,6 +210,60 @@ static int test_cachestat(const char *filename, bool write_random, bool create,
 out:
 	return ret;
 }
+
+/*
+ * Check that cachestat() accounts for the pages intersecting a byte range.
+ */
+static bool check_cachestat_range(int fd, size_t off, size_t len,
+				  unsigned long expected_pages)
+{
+	struct cachestat_range range = { off, len };
+	struct cachestat cs;
+
+	if (syscall(__NR_cachestat, fd, &range, &cs, 0)) {
+		ksft_print_msg("Cachestat range query failed: %s.\n",
+			       strerror(errno));
+		return false;
+	}
+
+	if (cs.nr_cache + cs.nr_evicted != expected_pages) {
+		ksft_print_msg("Expected %lu pages, got %llu cached and %llu evicted.\n",
+			       expected_pages, cs.nr_cache, cs.nr_evicted);
+		return false;
+	}
+
+	return true;
+}
+
+static int test_cachestat_ranges(void)
+{
+	size_t ps = sysconf(_SC_PAGESIZE);
+	int fd, ret = 0;
+
+	fd = open("tmpfilecachestat-range",
+		  O_CREAT | O_RDWR | O_TRUNC, 0600);
+	if (fd < 0) {
+		ksft_print_msg("Unable to create range test file.\n");
+		return 0;
+	}
+
+	if (!write_exactly(fd, 4 * ps)) {
+		ksft_print_msg("Unable to populate range test file.\n");
+		goto out;
+	}
+
+	if (check_cachestat_range(fd, ps - 1, 2, 2))
+		ret |= 1;
+
+	if (check_cachestat_range(fd, ps, 0, 3))
+		ret |= 2;
+
+out:
+	close(fd);
+	unlink("tmpfilecachestat-range");
+	return ret;
+}
+
 const char *file_type_str(enum file_type type)
 {
 	switch (type) {
@@ -317,9 +375,9 @@ int main(void)
 	for (int i = 0; i < 5; i++) {
 		const char *dev_filename = dev_files[i];
 
-		if (test_cachestat(dev_filename, false, false, false,
-			4, O_RDONLY, 0400) == KSFT_PASS)
-			ksft_test_result_pass("cachestat works with %s\n", dev_filename);
+		if (test_cachestat(dev_filename, false, false, false, i >= 3,
+				   4, O_RDONLY, 0400) == KSFT_PASS)
+			ksft_test_result_pass("cachestat handles %s\n", dev_filename);
 		else {
 			ksft_test_result_fail("cachestat fails with %s\n", dev_filename);
 			ret = 1;
@@ -327,7 +385,7 @@ int main(void)
 	}
 
 	if (test_cachestat("tmpfilecachestat", true, true,
-		false, 4, O_CREAT | O_RDWR, 0600) == KSFT_PASS)
+		false, false, 4, O_CREAT | O_RDWR, 0600) == KSFT_PASS)
 		ksft_test_result_pass("cachestat works with a normal file\n");
 	else {
 		ksft_test_result_fail("cachestat fails with normal file\n");
@@ -335,7 +393,7 @@ int main(void)
 	}
 
 	switch (test_cachestat("tmpfilecachestat", true, true,
-		true, 4, O_CREAT | O_RDWR, 0600)) {
+		true, false, 4, O_CREAT | O_RDWR, 0600)) {
 	case KSFT_FAIL:
 		ksft_test_result_fail("cachestat fsync fails with normal file\n");
 		ret = KSFT_FAIL;
@@ -361,5 +419,24 @@ int main(void)
 		ksft_test_result_fail("cachestat fails with a mmap file\n");
 		ret = 1;
 	}
+
+	{
+		int range_results = test_cachestat_ranges();
+
+		if (range_results & 1) {
+			ksft_test_result_pass("cachestat handles an unaligned range\n");
+		} else {
+			ksft_test_result_fail("cachestat handles an unaligned range\n");
+			ret = 1;
+		}
+
+		if (range_results & 2) {
+			ksft_test_result_pass("cachestat handles a zero-length range\n");
+		} else {
+			ksft_test_result_fail("cachestat handles a zero-length range\n");
+			ret = 1;
+		}
+	}
+
 	return ret;
 }
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] selftests/cachestat: test unaligned and zero-length ranges
  2026-12-04 22:26 [PATCH] selftests/cachestat: test unaligned and zero-length ranges Preetam Sundar Das
@ 2026-12-04 22:39 ` Nhat Pham
  0 siblings, 0 replies; 2+ messages in thread
From: Nhat Pham @ 2026-12-04 22:39 UTC (permalink / raw)
  To: Preetam Sundar Das; +Cc: hannes, shuah, linux-mm, linux-kselftest

On Fri, Dec 4, 2026 at 3:26 PM Preetam Sundar Das <daspreetam4@gmail.com> wrote:
>

Seems a bit terse? Why is this test case interesting? What's the
intended effect/output?


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04 22:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 22:26 [PATCH] selftests/cachestat: test unaligned and zero-length ranges Preetam Sundar Das
2026-12-04 22:39 ` Nhat Pham

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox