[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20231118104040.386381-6-zhangxiaoxu@huaweicloud.com>
Date: Sat, 18 Nov 2023 18:40:31 +0800
From: Zhang Xiaoxu <zhangxiaoxu@...weicloud.com>
To: zhangxiaoxu5@...wei.com, weiyongjun1@...wei.com,
linux-kernel@...r.kernel.org, broonie@...nel.org,
rostedt@...dmis.org, mingo@...hat.com, frowand.list@...il.com,
linux-spi@...r.kernel.org
Subject: [PATCH -next 05/14] kddv/core: Support kernel memory leak detector
From: Zhang Xiaoxu <zhangxiaoxu5@...wei.com>
Check the kmemleak before return from test case,
fail the test case if a kmemleak is detected.
Signed-off-by: Wei Yongjun <weiyongjun1@...wei.com>
Signed-off-by: Zhang Xiaoxu <zhangxiaoxu5@...wei.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 --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@...wei.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()
--
2.34.1
Powered by blists - more mailing lists