Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1611603

[PATCH v2] module: check if memory leak by module.

From Maninder Singh <maninder1.s@samsung.com>
Newsgroups linux.kernel
Subject [PATCH v2] module: check if memory leak by module.
Date 2017-03-29 08:10 +0200
Message-ID <tqeXT-2Ou-1@gated-at.bofh.it> (permalink)
References <tqeXT-2Ou-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


This patch checks if any module which is going to be unloaded
is doing vmalloc memory leak or not.

Logs:-
[  129.336368] Module [test_module] is getting unloaded before doing vfree
[  129.336371] Memory still allocated: addr:0xffffc90001461000 - 0xffffc900014c7000, pages 101
[  129.336376] Allocating function kernel_init+0x1c/0x20 [test_module]

Signed-off-by: Vaneet Narang <v.narang@samsung.com>
Signed-off-by: Maninder Singh <maninder1.s@samsung.com>
---
v1->v2: made code generic rather than dependent on config.
	changed pr_alert to pr_err.

 include/linux/vmalloc.h |  2 ++
 kernel/module.c         | 22 ++++++++++++++++++++++
 mm/vmalloc.c            |  2 --
 3 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index 46991ad..5531af3 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -29,6 +29,8 @@
 #define IOREMAP_MAX_ORDER	(7 + PAGE_SHIFT)	/* 128 pages */
 #endif
 
+#define VM_VM_AREA  0x04
+
 struct vm_struct {
 	struct vm_struct	*next;
 	void			*addr;
diff --git a/kernel/module.c b/kernel/module.c
index f953df9..98a8018 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2117,9 +2117,31 @@ void __weak module_arch_freeing_init(struct module *mod)
 {
 }
 
+static void check_memory_leak(struct module *mod)
+{
+	struct vmap_area *va;
+
+	rcu_read_lock();
+	list_for_each_entry_rcu(va, &vmap_area_list, list) {
+		if (!(va->flags & VM_VM_AREA))
+			continue;
+		if ((mod->core_layout.base < va->vm->caller) &&
+			(mod->core_layout.base + mod->core_layout.size) > va->vm->caller) {
+			pr_err("Module [%s] is getting unloaded before doing vfree\n", mod->name);
+			pr_err("Memory still allocated: addr:0x%lx - 0x%lx, pages %u\n",
+				va->va_start, va->va_end, va->vm->nr_pages);
+			pr_err("Allocating function %pS\n", va->vm->caller);
+		}
+
+	}
+	rcu_read_unlock();
+}
+
 /* Free a module, remove from lists, etc. */
 static void free_module(struct module *mod)
 {
+	check_memory_leak(mod);
+
 	trace_module_free(mod);
 
 	mod_sysfs_teardown(mod);
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 68eb002..0166a0a 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -314,8 +314,6 @@ unsigned long vmalloc_to_pfn(const void *vmalloc_addr)
 
 /*** Global kva allocator ***/
 
-#define VM_VM_AREA	0x04
-
 static DEFINE_SPINLOCK(vmap_area_lock);
 /* Export for kexec only */
 LIST_HEAD(vmap_area_list);
-- 
1.9.1

Back to linux.kernel | Previous | NextNext in thread | Find similar | Unroll thread


Thread

[PATCH v2] module: check if memory leak by module. Maninder Singh <maninder1.s@samsung.com> - 2017-03-29 08:10 +0200
  Re: [PATCH v2] module: check if memory leak by module. Michal Hocko <mhocko@kernel.org> - 2017-03-29 09:50 +0200
    Re: [PATCH v2] module: check if memory leak by module. Miroslav Benes <mbenes@suse.cz> - 2017-03-29 10:10 +0200
      Re: [PATCH v2] module: check if memory leak by module. Vaneet Narang <v.narang@samsung.com> - 2017-03-29 12:40 +0200
        Re: [PATCH v2] module: check if memory leak by module. Michal Hocko <mhocko@kernel.org> - 2017-03-29 13:40 +0200
          Re: [PATCH v2] module: check if memory leak by module. Joel Fernandes <joelaf@google.com> - 2017-03-31 08:50 +0200
            Re: [PATCH v2] module: check if memory leak by module. Michal Hocko <mhocko@kernel.org> - 2017-03-31 10:10 +0200
              Re: [PATCH v2] module: check if memory leak by module. Joel Fernandes <joelaf@google.com> - 2017-03-31 19:10 +0200
                Re: [PATCH v2] module: check if memory leak by module. Michal Hocko <mhocko@kernel.org> - 2017-04-03 09:30 +0200
        Re: [PATCH v2] module: check if memory leak by module. Jessica Yu <jeyu@redhat.com> - 2017-04-01 00:20 +0200
  Re: [PATCH v2] module: check if memory leak by module. Pavel Machek <pavel@ucw.cz> - 2017-03-30 15:40 +0200

csiph-web