selftests/cachestat: test unaligned and zero-length ranges
Subsystems (3)
PATCH 1/1 selftests/cachestat: test unaligned and zero-length ranges
--- .../selftests/cachestat/test_cachestat.c | 101 +++++++++++++++--- 1 file changed, 89 insertions(+), 12 deletions(-)
tools/testing/selftests/cachestat/test_cachestat.c +89 -12
@@ -18,7 +18,7 @@#include "kselftest.h" -#define NR_TESTS 9 +#define NR_TESTS 12static 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; }
Signature · 1 line
2.53.0
Nhat Pham
maintainerQuoted 1 line · Preetam Sundar Das
Seems a bit terse? Why is this test case interesting? What's the intended effect/output?
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.
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.
Quoted 7 lines · Nhat Pham
Quoted 1 line · Preetam Sundar Das
Seems a bit terse? Why is this test case interesting? What's the intended effect/output?
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.
Quoted 6 lines · Nhat Pham
Quoted 1 line · Preetam Sundar Das
Seems a bit terse? Why is this test case interesting? What's the intended effect/output?
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] selftests/cachestat: allow_eperm masks regressions and hardcodes index - [Low]…
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 <[email protected]>
Quoted 3 lines
selftests/cachestat: test unaligned and zero-length ranges
Signed-off-by Preetam Sundar Das <[email protected]>
[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?
Quoted 4 lines
tools/testing/selftests/cachestat/test_cachestat.c +0
-0
[ ... ]
Quoted 7 lines
tools/testing/selftests/cachestat/test_cachestat.c +0
-3@@ -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;
tools/testing/selftests/cachestat/test_cachestat.c static int test_cachestat(const char *filename, bool write_random, bool create, · line 165
Quoted 10 lines
tools/testing/selftests/cachestat/test_cachestat.c +8
-1- +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.
[ ... ]
Quoted 6 lines
tools/testing/selftests/cachestat/test_cachestat.c +6
-0+static int test_cachestat_ranges (void ) +{ +size_t ps =sysconf (_SC_PAGESIZE); +int fd, ret =0 ; + + fd = open("tmpfilecachestat-range",
tools/testing/selftests/cachestat/test_cachestat.c static int test_cachestat(const char *filename, bool write_random, bool create, · line 253
Quoted 10 lines
tools/testing/selftests/cachestat/test_cachestat.c +10
-0+ 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.
[ ... ]
tools/testing/selftests/cachestat/test_cachestat.c int main(void) · line 383
Quoted 13 lines
tools/testing/selftests/cachestat/test_cachestat.c +3
-3@@ -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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1