[bootlin/training-materials updates] master: debugging: labs: set leaks in default worker instead of module init function (9142645b)

Alexis Lothoré alexis.lothore at bootlin.com
Fri Jul 7 16:10:44 CEST 2023


Repository : https://github.com/bootlin/training-materials
On branch  : master
Link       : https://github.com/bootlin/training-materials/commit/9142645beb7205df04cf4f57e310071d26c5f48c

>---------------------------------------------------------------

commit 9142645beb7205df04cf4f57e310071d26c5f48c
Author: Alexis Lothoré <alexis.lothore at bootlin.com>
Date:   Fri Jul 7 16:10:44 2023 +0200

    debugging: labs: set leaks in default worker instead of module init
    function
    
    Current kmemleak diagnostic can be quite hard because of the way the leak
    is implemented:
    - it is in an external module, so module loading address must be
      substracted from addresses returned by kmemleak
    - the leak is implemented in leaky_module_init, which is flagged as __init,
      so this function address has a special offset
    
    Example of current implementation: kmemleak points to address 0xbf005090
    - substract module loading address (0xbf000000, as seen in /proc/modules):
      relevant address is now 0x5090
    - substract __init__ function offset (0x5000, discoverable when enabling
      logs in module.c): relevant address is now 0x90
    
    Remove the second constraint by moving the leak from the init function to a
    function pushed in default worker, so trainees only have to care about
    modules loading offset
    
    Fixes #198
    
    Signed-off-by: Alexis Lothoré <alexis.lothore at bootlin.com>


>---------------------------------------------------------------

9142645beb7205df04cf4f57e310071d26c5f48c
 lab-data/debugging/nfsroot/root/kmemleak/kmemleak_test.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/lab-data/debugging/nfsroot/root/kmemleak/kmemleak_test.c b/lab-data/debugging/nfsroot/root/kmemleak/kmemleak_test.c
index 55e34fca..045487d4 100644
--- a/lab-data/debugging/nfsroot/root/kmemleak/kmemleak_test.c
+++ b/lab-data/debugging/nfsroot/root/kmemleak/kmemleak_test.c
@@ -16,7 +16,7 @@
 #include <linux/list.h>
 #include <linux/percpu.h>
 #include <linux/fdtable.h>
-
+#include <linux/workqueue.h>
 #include <linux/kmemleak.h>
 
 struct test_node {
@@ -28,11 +28,11 @@ struct test_node {
 static LIST_HEAD(test_list);
 void *module_data;
 
-static int __init leaky_module_init(void)
+static void my_work_func(struct work_struct *unused)
 {
 	struct test_node *elem;
-	void *local_data;
 	int i;
+	void *local_data;
 
 	module_data = kmalloc(1024, GFP_KERNEL);
 	local_data = kmalloc(1024, GFP_KERNEL);
@@ -41,10 +41,16 @@ static int __init leaky_module_init(void)
 		elem = kzalloc(sizeof(*elem), GFP_KERNEL);
 		pr_info("kzalloc(sizeof(*elem)) = %p\n", elem);
 		if (!elem)
-			return -ENOMEM;
+			return;
 		INIT_LIST_HEAD(&elem->list);
 		list_add_tail(&elem->list, &test_list);
 	}
+}
+static DECLARE_WORK(my_work, my_work_func);
+
+static int __init leaky_module_init(void)
+{
+	schedule_work(&my_work);
 
 	return 0;
 }




More information about the training-materials-updates mailing list