From: Preetam Sundar Das <daspreetam4@gmail.com>
To: nphamcs@gmail.com
Cc: hannes@cmpxchg.org, shuah@kernel.org, linux-mm@kvack.org,
linux-kselftest@vger.kernel.org,
Preetam Sundar Das <daspreetam4@gmail.com>
Subject: [PATCH] selftests/cachestat: test unaligned and zero-length ranges
Date: Sat, 5 Dec 2026 03:56:23 +0530 [thread overview]
Message-ID: <20261204222623.50735-1-daspreetam4@gmail.com> (raw)
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
next reply other threads:[~2026-12-04 22:26 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 22:26 Preetam Sundar Das [this message]
2026-12-04 22:39 ` [PATCH] selftests/cachestat: test unaligned and zero-length ranges Nhat Pham
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204222623.50735-1-daspreetam4@gmail.com \
--to=daspreetam4@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox