]> git.apps.os.sepia.ceph.com Git - xfstests-dev.git/commitdiff
common: fix kmemleak to work with sections
authorDarrick J. Wong <darrick.wong@oracle.com>
Tue, 12 Feb 2019 02:17:26 +0000 (18:17 -0800)
committerEryu Guan <guaneryu@gmail.com>
Sat, 16 Feb 2019 11:10:38 +0000 (19:10 +0800)
Refactor the kmemleak code to work correctly with sections.  This
requires changing the location of the "is kmemleak enabled?" flag to
use /tmp instead of RESULT_BASE, scanning for leaks after every
test, and clarifying which functions get used when.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
README
check
common/rc

diff --git a/README b/README
index 73f057f3d403fd9a78fd7d08bfb82973eb95e2a4..6e9aa34d9cfb0a0ab0406d2ddaec9482521986e1 100644 (file)
--- a/README
+++ b/README
@@ -99,6 +99,8 @@ Preparing system for tests:
                that relevant results are compared.  For example 'spinningrust'
                for configurations that use spinning disks and 'nvme' for tests
                using nvme drives.
+             - set USE_KMEMLEAK=yes to scan for memory leaks in the kernel
+               after every test, if the kernel supports kmemleak.
 
         - or add a case to the switch in common/config assigning
           these variables based on the hostname of your test
diff --git a/check b/check
index 77a06b00dbba677f9c04ca6d9a5e266ec67579eb..20e95302932cbba6aa81de8850bc28c0d9db88c0 100755 (executable)
--- a/check
+++ b/check
@@ -496,7 +496,7 @@ _expunge_test()
        return 0
 }
 
-_init_kmemleak
+_detect_kmemleak
 _prepare_test_list
 
 if $OPTIONS_HAVE_SECTIONS; then
@@ -771,9 +771,12 @@ for section in $HOST_OPTIONS_SECTIONS; do
                        # and log messages that shouldn't be there.
                        _check_filesystems
                        _check_dmesg || err=true
-                       _check_kmemleak || err=true
                fi
 
+               # Scan for memory leaks after every test so that associating
+               # a leak to a particular test will be as accurate as possible.
+               _check_kmemleak || err=true
+
                # test ends after all checks are done.
                $timestamp && _timestamp
                stop=`_wallclock`
index b8ed177618a790b8cec91297151a66f53ab3b83c..ab468adf19e25cadbc052e5a977d2bd74440c03f 100644 (file)
--- a/common/rc
+++ b/common/rc
@@ -3500,7 +3500,7 @@ _check_dmesg()
 # capture the kmemleak report
 _capture_kmemleak()
 {
-       local kern_knob="${DEBUGFS_MNT}/kmemleak"
+       local kern_knob="$DEBUGFS_MNT/kmemleak"
        local leak_file="$1"
 
        # Tell the kernel to scan for memory leaks.  Apparently the write
@@ -3521,17 +3521,20 @@ ENDL
        echo "clear" > "$kern_knob"
 }
 
-# set up kmemleak
-_init_kmemleak()
+# Figure out if the running kernel supports kmemleak; if it does, clear out
+# anything that leaked before we even started testing.  The leak checker only
+# needs to be primed like this once per ./check invocation.
+_detect_kmemleak()
 {
-       local kern_knob="${DEBUGFS_MNT}/kmemleak"
+       local kern_knob="$DEBUGFS_MNT/kmemleak"
+       KMEMLEAK_CHECK_FILE="/tmp/check_kmemleak"
 
        # Since kernel v4.19-rc3, the kmemleak knob exists even if kmemleak is
        # disabled, but returns EBUSY on write. So instead of relying on
        # existance of writable knob file, we use a test file to indicate that
        # _check_kmemleak() is enabled only if we actually managed to write to
        # the knob file.
-       rm -f ${RESULT_BASE}/check_kmemleak
+       rm -f "$KMEMLEAK_CHECK_FILE"
 
        if [ ! -w "$kern_knob" ]; then
                return 0
@@ -3541,17 +3544,26 @@ _init_kmemleak()
        # then dump all the leaks recorded so far.
        if echo "scan=off" > "$kern_knob" 2>/dev/null; then
                _capture_kmemleak /dev/null
-               touch ${RESULT_BASE}/check_kmemleak
+               touch "$KMEMLEAK_CHECK_FILE"
        fi
 }
 
-# check kmemleak log
+# Kick the kmemleak checker to scan for leaks.  Background leak scan mode is
+# not enabled, so we must call the kernel to ask for a scan and deal with the
+# results appropriately.  This we do after every test completes, whether or not
+# it was successful.
 _check_kmemleak()
 {
-       local kern_knob="${DEBUGFS_MNT}/kmemleak"
-       local leak_file="${seqres}.kmemleak"
+       local kern_knob="$DEBUGFS_MNT/kmemleak"
+       local leak_file="$seqres.kmemleak"
 
-       if [ ! -f ${RESULT_BASE}/check_kmemleak ]; then
+       if [ ! -f "$KMEMLEAK_CHECK_FILE" ]; then
+               return 0
+       fi
+
+       # Not enabled, so discard any report of leaks found.
+       if [ "$USE_KMEMLEAK" != "yes" ]; then
+               _capture_kmemleak /dev/null
                return 0
        fi