@@ -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()
new file mode 100755
@@ -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()