On my test vm a full scan takes almost 5s. As this would slowdown the test runs too much, only run them every couple of tests. This allows to detect when there is a leak reported at the end of the script, and it allows to narrow down the test case/group that triggers the issue. Add new -K flag to force kmemleak runs after each test if its available, this can then be used to find the exact test case. Signed-off-by: Florian Westphal <fw@xxxxxxxxx> --- tests/shell/run-tests.sh | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/shell/run-tests.sh b/tests/shell/run-tests.sh index 1a6998759831..980aea0fc510 100755 --- a/tests/shell/run-tests.sh +++ b/tests/shell/run-tests.sh @@ -74,6 +74,11 @@ if [ "$1" == "-V" ] ; then shift fi +if [ "$1" == "-K" ]; then + KMEMLEAK=y + shift +fi + for arg in "$@"; do SINGLE+=" $arg" VERBOSE=y @@ -167,6 +172,50 @@ check_taint() fi } +kmem_runs=0 +kmemleak_found=0 + +# kmemleak may report suspected leaks +# that get free'd after all, so +# do not increment failed counter +# except for the last run. +check_kmemleak_force() +{ + test -f /sys/kernel/debug/kmemleak || return 0 + + echo scan > /sys/kernel/debug/kmemleak + + lines=$(grep "unreferenced object" /sys/kernel/debug/kmemleak | wc -l) + if [ $lines -ne $kmemleak_found ];then + msg_warn "[FAILED] kmemleak detected $lines memory leaks" + kmemleak_found=$lines + fi + + if [ $lines -ne 0 ];then + return 1 + fi + + return 0 +} + +check_kmemleak() +{ + test -f /sys/kernel/debug/kmemleak || return + + if [ "$KMEMLEAK" == "y" ] ; then + check_kmemleak_force + return + fi + + kmem_runs=$((kmem_runs + 1)) + if [ $((kmem_runs % 30)) -eq 0 ]; then + # scan slows tests down quite a bit, hence + # do this only for every 30th test file by + # default. + check_kmemleak_force + fi +} + check_taint for testfile in $(find_tests) @@ -218,9 +267,15 @@ do fi check_taint + check_kmemleak done echo "" +check_kmemleak_force +if [ "$?" -ne 0 ];then + ((failed++)) +fi + msg_info "results: [OK] $ok [FAILED] $failed [TOTAL] $((ok+failed))" kernel_cleanup -- 2.41.0