[PATCH] selftests/cachestat: clean up the mmap test scratch file

From: tongzhiwei

Date: Wed Nov 25 2026 - 20:19:10 EST


NOTE: This fix is found and coded by llm, but it has been
reviewed by human.

run_cachestat_test() creates its FILE_MMAP scratch file with open() but
cleans it up with shm_unlink(), which only removes a POSIX shared-memory
object under /dev/shm. The regular file "tmpshmcstat" is therefore left
behind in the working directory after every run.

The leftover file was previously worked around by adding its name to
.gitignore in commit b90cafb43887 ("selftests/cachestat: add
tmpshmcstat file to .gitignore"); the entry remains as a safety net
for runs interrupted mid-test, but the test itself should not leave
the file behind in the first place.

Use remove() for the mmap case and keep shm_unlink() for the FILE_SHMEM
case only, matching the cleanup the sibling test_cachestat() helper
already does for its own temp file.

Remove the file unconditionally, by design: the name "tmpshmcstat" is
test-specific so a real collision is implausible, and gating removal on
whether this run created the file would preserve stale files left by a
crashed previous run instead of cleaning them up.

Assisted-by: LLM
Signed-off-by: tongzhiwei <tongzhiwei@xxxxxxxxxxxxxxxxxxxx>
---
For context: the same leftover file was previously discussed in a
related but different proposal from Madhur Kumar, which took the
EXTRA_CLEAN approach (removing the file in "make kselftest-clean")
and has not been applied so far:
https://lore.kernel.org/r/20250927161141.778631-1-madhurkumar004@xxxxxxxxx/
This patch instead removes the leftover inside the test itself, so
the file is cleaned up whether or not the test builds/runs via make.

tools/testing/selftests/cachestat/test_cachestat.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/testing/selftests/cachestat/test_cachestat.c
index 542cd09..5e62c49 100644
--- a/tools/testing/selftests/cachestat/test_cachestat.c
+++ b/tools/testing/selftests/cachestat/test_cachestat.c
@@ -289,7 +289,10 @@ bool run_cachestat_test(enum file_type type)
}

close_fd:
- shm_unlink(filename);
+ if (type == FILE_SHMEM)
+ shm_unlink(filename);
+ else
+ remove(filename);
out:
return ret;
}
--
2.33.0