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 <auke.kok@versity.com>
This commit is contained in:
Auke Kok
2025-07-15 14:55:04 -07:00
parent aeb24433d0
commit bddc170cf2

View File

@@ -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 <nr> | The number of devices and mounts to test.
-o <opts> | 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"