* [PATCH] selftests/cachestat: test unaligned and zero-length ranges
@ 2026-12-04 22:26 Preetam Sundar Das
2026-12-04 22:39 ` Nhat Pham
2026-12-05 22:26 ` sashiko-bot
0 siblings, 2 replies; 6+ 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] 6+ 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
2026-12-05 9:20 ` Preetam Sundar Das
` (2 more replies)
2026-12-05 22:26 ` sashiko-bot
1 sibling, 3 replies; 6+ 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] 6+ messages in thread
* Re: [PATCH] selftests/cachestat: test unaligned and zero-length ranges
2026-12-04 22:39 ` Nhat Pham
@ 2026-12-05 9:20 ` Preetam Sundar Das
2026-12-05 12:53 ` Preetam Sundar Das
2026-12-05 13:04 ` Preetam Sundar Das
2 siblings, 0 replies; 6+ messages in thread
From: Preetam Sundar Das @ 2026-12-05 9:20 UTC (permalink / raw)
To: Nhat Pham; +Cc: hannes, shuah, linux-mm, linux-kselftest
[-- Attachment #1: Type: text/plain, Size: 880 bytes --]
Thanks for the reply. Let me elaborate.
The patch adds coverage for the following boundary cases:
*First test: Unaligned range* – Queries a two-byte range spanning a page
boundary and verifies that both pages are accounted for.
*Second test: Zero-length range* – The cachestat API explicitly defines len
== 0 as querying from off to the end of the file. Here, we verify that
querying from the beginning of page 1 to EOF in a four-page file accounts
for the remaining three pages.
*Permission handling* – The existing device-file tests treated all
cachestat() errors as failures, including EPERM caused by permission checks
on certain devices. This change allows expected EPERM responses for
selected device cases (the last two entries) while continuing to fail on
other unexpected syscall errors. The regular-file tests still treat EPERM
as a failure.
[-- Attachment #2: Type: text/html, Size: 1298 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] selftests/cachestat: test unaligned and zero-length ranges
2026-12-04 22:39 ` Nhat Pham
2026-12-05 9:20 ` Preetam Sundar Das
@ 2026-12-05 12:53 ` Preetam Sundar Das
2026-12-05 13:04 ` Preetam Sundar Das
2 siblings, 0 replies; 6+ messages in thread
From: Preetam Sundar Das @ 2026-12-05 12:53 UTC (permalink / raw)
To: Nhat Pham; +Cc: hannes, shuah, linux-mm, linux-kselftest
[-- Attachment #1: Type: text/plain, Size: 1144 bytes --]
Thanks for the reply. Let me elaborate.
The patch adds coverage for the following boundary cases:
First test: Unaligned range – Queries a two-byte range spanning a page
boundary and verifies that both pages are accounted for.
Second test: Zero-length range – The cachestat API explicitly defines len
== 0 as querying from off to the end of the file. Here, we verify that
querying from the beginning of page 1 to EOF in a four-page file accounts
for the remaining three pages.
Permission handling – The existing device-file tests treated all
cachestat() errors as failures, including EPERM caused by permission checks
on certain devices. This change allows expected EPERM responses for
selected device cases (the last two entries) while continuing to fail on
other unexpected syscall errors. The regular-file tests still treat EPERM
as a failure.
On Sat, Dec 5, 2026 at 4:09 AM Nhat Pham <nphamcs@gmail.com> wrote:
> 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?
>
[-- Attachment #2: Type: text/html, Size: 1544 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] selftests/cachestat: test unaligned and zero-length ranges
2026-12-04 22:39 ` Nhat Pham
2026-12-05 9:20 ` Preetam Sundar Das
2026-12-05 12:53 ` Preetam Sundar Das
@ 2026-12-05 13:04 ` Preetam Sundar Das
2 siblings, 0 replies; 6+ messages in thread
From: Preetam Sundar Das @ 2026-12-05 13:04 UTC (permalink / raw)
To: Nhat Pham; +Cc: hannes, shuah, linux-mm, linux-kselftest
Thanks for the reply. Let me elaborate.
The patch adds coverage for the following boundary cases:
First test: Unaligned range – Queries a two-byte range spanning a page
boundary and verifies that both pages are accounted for.
Second test: Zero-length range – The cachestat API explicitly defines
len == 0 as querying from off to the end of the file. Here, we verify
that querying from the beginning of page 1 to EOF in a four-page file
accounts for the remaining three pages.
Permission handling – The existing device-file tests treated all
cachestat() errors as failures, including EPERM caused by permission
checks on certain devices. This change allows expected EPERM responses
for selected device cases (the last two entries) while continuing to
fail on other unexpected syscall errors. The regular-file tests still
treat EPERM as a failure.
On Sat, Dec 5, 2026 at 4:09 AM Nhat Pham <nphamcs@gmail.com> wrote:
>
> 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] 6+ 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
@ 2026-12-05 22:26 ` sashiko-bot
1 sibling, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-05 22:26 UTC (permalink / raw)
To: Preetam Sundar Das, nphamcs; +Cc: hannes, shuah, linux-mm
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] selftests/cachestat: allow_eperm masks regressions and
hardcodes index
- [Low] selftests/cachestat: read of clobbered errno after
ksft_print_msg()
- [Low] selftests/cachestat: setup errors reported as test failures in
ranges
- [Low] selftests/cachestat: empty commit message bundles unrelated
fixes
--
commit 53d9d32356be8f076423856bd41d85c09de99078
Author: Preetam Sundar Das <daspreetam4@gmail.com>
> selftests/cachestat: test unaligned and zero-length ranges
>
> Signed-off-by: Preetam Sundar Das <daspreetam4@gmail.com>
[Severity: Low]
This commit message has no body explaining the new range tests, and the
patch bundles two unrelated changes not mentioned in the subject:
- adding allow_eperm for /proc/version and /proc after commit
5f537664e705 ("cachestat: fix page cache statistics permission
checking"), and
- bumping NR_TESTS by 3 instead of 2 to fix the count mismatch from
commit d6a511dea45c ("selftests: cachestat: add tests for mmap,
refactor and enhance mmap test for cachestat validation").
Should those two fixes be split into separate patches, with a brief
changelog added here?
> diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/testing/selftests/cachestat/test_cachestat.c
> index 542cd09cb4434..c7aa58bdcf7af 100644
> --- a/tools/testing/selftests/cachestat/test_cachestat.c
> +++ b/tools/testing/selftests/cachestat/test_cachestat.c
[ ... ]
> @@ -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 {
[Severity: Low]
Can ksft_print_msg() clobber errno before it is checked here?
ksft_print_msg() in tools/testing/selftests/kselftest.h restores errno
before calling vprintf(), but not after vprintf() returns. If vprintf()
modifies errno, test_cachestat() can misclassify a valid EPERM as a
failure or print the wrong strerror() message.
Saving errno immediately after syscall(__NR_cachestat, ...) avoids this.
[ ... ]
> +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;
> + }
[Severity: Low]
Should setup failures in open() or write_exactly() be reported with
ksft_test_result_error() instead of ksft_test_result_fail()?
Returning 0 on those paths produces the same bitmask as both
check_cachestat_range() calls failing, so main() reports both range
tests as kernel failures even though cachestat() was never called.
Reporting results directly in test_cachestat_ranges() or returning a
distinct error status would keep setup errors separate.
[ ... ]
> @@ -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;
[Severity: Medium]
Does accepting both 0 and EPERM as KSFT_PASS mask permission regressions
in can_do_cachestat()?
With allow_eperm set unconditionally for /proc/version and /proc, a
privileged run that wrongly returns EPERM passes silently, and an
unprivileged run that wrongly succeeds on those root-owned read-only
files also passes.
Validating the expected result based on caller permissions, or reporting
KSFT_SKIP with a reason when unprivileged, would preserve coverage.
In addition, passing i >= 3 with a hardcoded loop bound of 5 couples
allow_eperm to dev_files[] ordering; pairing each path with its expected
behavior in a table using ARRAY_SIZE() avoids silent breakage on edits.
Also, the allow_eperm branch in test_cachestat() does not jump to out1,
so it would fall through into the test_fsync block if those flags were
ever combined.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204222623.50735-1-daspreetam4@gmail.com?part=1
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-05 22:27 UTC | newest]
Thread overview: 6+ 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
2026-12-05 9:20 ` Preetam Sundar Das
2026-12-05 12:53 ` Preetam Sundar Das
2026-12-05 13:04 ` Preetam Sundar Das
2026-12-05 22:26 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox