From bddc170cf23e41f24e640ba2eb3d559ec2bfb0b3 Mon Sep 17 00:00:00 2001 From: Auke Kok Date: Tue, 15 Jul 2025 14:55:04 -0700 Subject: [PATCH] Run kmemleak during tests. Enable kmemleak possible leak collection during each test. Suspected or real leaks *fail* the test. Only a clean scan is passing. This requires that the kernel is compiled with kmemleak enabled in the config (`CONFIG_DEBUG_KMEMLEAK`) and that kmemleak isn't disabled by default (`CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF`) at boot time, which is the case for the default distro kernels. In that case, the easiest is to add `kmemleak=on` to the kernel boot cmdline. During each test, the initial kmemleak results are wiped and the auto stack and scan workers are disabled. After each test is finished the `scan` command is given to kmemleak and the results are collected. If nothing is found, the kmemleak output is empty. If there is any output from kmemleak, it will throw a dmesg error that leaks were found, and the (suspected) leaks are dumped with stack traces of each allocation, size, and the first 32b are dumped. If kmemleak is present in the kernel, but (irreversably) disabled, the test will fail to run. Same if it is entirely missing from the kernel. Signed-off-by: Auke Kok --- tests/run-tests.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/run-tests.sh b/tests/run-tests.sh index b9917d4f..a1707b19 100755 --- a/tests/run-tests.sh +++ b/tests/run-tests.sh @@ -60,6 +60,8 @@ $(basename $0) options: | the file system to be tested. Will be clobbered by -m mkfs. -m | Run mkfs on the device before mounting and running | tests. Implies unmounting existing mounts first. + -l | Enable kmemleak scan during each test. Requires "kmemleak=on" in + | kernel cmdline boot args. -n | The number of devices and mounts to test. -o | Add option string to all mounts during all tests. -P | Enable trace_printk. @@ -129,6 +131,12 @@ while true; do -i) T_INSMOD="1" ;; + -l) + echo "stack=off" > /sys/kernel/debug/kmemleak && + echo "scan=off" > /sys/kernel/debug/kmemleak || + die "kmemleak disabled or missing" + T_KMEMLEAK="1" + ;; -M) test -n "$2" || die "-z must have meta device file argument" T_META_DEVICE="$2" @@ -569,6 +577,11 @@ for t in $tests; do # mark in dmesg as to what test we are running echo "run scoutfs test $test_name" > /dev/kmsg + # clean kmemleak scan + if [ -n "$T_KMEMLEAK" ]; then + echo "clear" > /sys/kernel/debug/kmemleak + fi + # record dmesg before dmesg | t_filter_dmesg > "$T_TMPDIR/dmesg.before" @@ -616,6 +629,17 @@ for t in $tests; do fi fi + # record kmemleak scan + if [ -n "$T_KMEMLEAK" ]; then + echo scan > /sys/kernel/debug/kmemleak + cp /sys/kernel/debug/kmemleak "$T_TMPDIR/kmemleak.scan" + if [ -s "$T_TMPDIR/kmemleak.scan" ]; then + message="kmemleak detected memory leak" + sts=$T_FAIL_STATUS + cat "$T_TMPDIR/kmemleak.scan" >> "$T_RESULTS/fail.log" + fi + fi + # record unknown exit status if [ "$sts" -lt "$T_FIRST_STATUS" -o "$sts" -gt "$T_LAST_STATUS" ]; then message="unknown status: $sts"