diff mbox series

[-next,05/14] kddv/core: Support kernel memory leak detector

Message ID 20231118104040.386381-6-zhangxiaoxu@huaweicloud.com
State New
Headers show
Series Implement a ligth weight device driver test framework | expand

Commit Message

Zhang Xiaoxu Nov. 18, 2023, 10:40 a.m. UTC
From: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>

Check the kmemleak before return from test case,
fail the test case if a kmemleak is detected.

Signed-off-by: Wei Yongjun <weiyongjun1@huawei.com>
Signed-off-by: Zhang Xiaoxu <zhangxiaoxu5@huawei.com>
---
 tools/testing/kddv/kddv/core/environ.py | 13 ++++++++-
 tools/testing/kddv/kddv/core/memleak.py | 39 +++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/kddv/kddv/core/memleak.py
diff mbox series

Patch

diff --git a/tools/testing/kddv/kddv/core/environ.py b/tools/testing/kddv/kddv/core/environ.py
index d57e33a6ac6e..68c98e8c44da 100755
--- a/tools/testing/kddv/kddv/core/environ.py
+++ b/tools/testing/kddv/kddv/core/environ.py
@@ -9,20 +9,31 @@ 
 import logging
 
 from .dmesg import KernelMessage
+from .memleak import Kmemleak
 
 logger = logging.getLogger(__name__)
 
 class Environ(object):
     def __init__(self):
         self.kmsg = KernelMessage()
+        self.leak = Kmemleak()
 
     def setup(self):
         self.kmsg.setup()
+        self.leak.setup()
 
     def teardown(self):
+        self.leak.teardown()
         self.kmsg.teardown()
 
+    def enable_kmemleak(self):
+        """Enable Kernel memory leak detector"""
+        self.leak.enabled = True
+
     def check_failure(self):
-        return self.kmsg.check_failure()
+        msg = self.kmsg.check_failure()
+        if msg:
+            return msg
+        return self.leak.check_failure()
 
 environ = Environ()
diff --git a/tools/testing/kddv/kddv/core/memleak.py b/tools/testing/kddv/kddv/core/memleak.py
new file mode 100755
index 000000000000..7ff67997560f
--- /dev/null
+++ b/tools/testing/kddv/kddv/core/memleak.py
@@ -0,0 +1,39 @@ 
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+#
+# Kernel device driver verification
+#
+# Copyright (C) 2022-2023 Huawei Technologies Co., Ltd
+# Author: Wei Yongjun <weiyongjun1@huawei.com>
+
+import os
+import logging
+import pathlib
+
+KMEMLEAK = '/sys/kernel/debug/kmemleak'
+
+logger = logging.getLogger(__name__)
+
+class Kmemleak(object):
+    def __init__(self):
+        self.has_feature = os.path.exists(KMEMLEAK)
+        self.ctrl = pathlib.Path(KMEMLEAK)
+        self.enabled = False
+
+    def setup(self):
+        if not self.has_feature or not self.enabled:
+            return
+        self.ctrl.write_text('clear')
+
+    def teardown(self):
+        if not self.has_feature or not self.enabled:
+            return
+        self.ctrl.write_text('clear')
+
+    def check_failure(self):
+        if not self.has_feature or not self.enabled:
+            return None
+        logger.debug('check kernel memleak')
+        self.ctrl.write_text('scan')
+        self.ctrl.write_text('scan')
+        return self.ctrl.read_text().rstrip()