All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] module: check if memory leak by module.
       [not found] <CGME20170324113058epcas5p48d9b7cf45d62d2cf7c2146ebc8719542@epcas5p4.samsung.com>
@ 2017-03-24 11:30   ` Maninder Singh
  0 siblings, 0 replies; 3+ messages in thread
From: Maninder Singh @ 2017-03-24 11:30 UTC (permalink / raw)
  To: jeyu, rusty, akpm, chris, aryabinin, joonas.lahtinen, mhocko,
	keescook, pavel, jinb.park7, anisse, rafael.j.wysocki, zijun_hu,
	mingo, mawilcox, thgarnie, joelaf, kirill.shutemov, linux-mm,
	linux-kernel
  Cc: pankaj.m, ajeet.y, hakbong5.lee, a.sahrawat, lalit.mohan, cpgs,
	Maninder Singh, Vaneet Narang

This patch adds new config VMALLOC_MEMORY_LEAK to check if any
module which is going to be unloaded is doing vmalloc memory leak.

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

Signed-off-by: Maninder Singh <maninder1.s@samsung.com>
Signed-off-by: Vaneet Narang <v.narang@samsung.com>
---
 include/linux/vmalloc.h |  2 ++
 kernel/module.c         | 28 ++++++++++++++++++++++++++++
 mm/Kconfig.debug        |  7 +++++++
 mm/vmalloc.c            |  2 --
 4 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index 3d9d786..abfc03c 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -29,6 +29,8 @@ struct notifier_block;		/* in notifier.h */
 #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 529efae..b492f34 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2082,9 +2082,37 @@ void __weak module_arch_freeing_init(struct module *mod)
 {
 }
 
+#ifdef CONFIG_VMALLOC_MEMORY_LEAK
+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_alert("Module %s is getting unloaded before doing vfree\n", mod->name);
+			pr_alert("Memory still allocated: addr:0x%lx - 0x%lx, pages %u\n",
+				va->va_start, va->va_end, va->vm->nr_pages);
+			pr_alert("Allocating function %pS\n", va->vm->caller);
+		}
+
+	}
+	rcu_read_unlock();
+}
+#else
+static inline void check_memory_leak(struct module *mod)
+{
+}
+#endif
+
 /* 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/Kconfig.debug b/mm/Kconfig.debug
index 22f4cd9..0bead5d 100644
--- a/mm/Kconfig.debug
+++ b/mm/Kconfig.debug
@@ -92,3 +92,10 @@ config DEBUG_PAGE_REF
 	  careful when enabling this feature because it adds about 30 KB to the
 	  kernel code.  However the runtime performance overhead is virtually
 	  nil until the tracepoints are actually enabled.
+
+config VMALLOC_MEMORY_LEAK
+	bool "Enable to check memory leaks by modules"
+	default n
+	---help---
+	  This is a feature to check if any module has allocated memory using vmalloc
+	  but getting unloaded without doing vfree.
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 91f44e7..7fba87a 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -275,8 +275,6 @@ EXPORT_SYMBOL(vmalloc_to_pfn);
 
 /*** 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

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 1/1] module: check if memory leak by module.
@ 2017-03-24 11:30   ` Maninder Singh
  0 siblings, 0 replies; 3+ messages in thread
From: Maninder Singh @ 2017-03-24 11:30 UTC (permalink / raw)
  To: jeyu, rusty, akpm, chris, aryabinin, joonas.lahtinen, mhocko,
	keescook, pavel, jinb.park7, anisse, rafael.j.wysocki, zijun_hu,
	mingo, mawilcox, thgarnie, joelaf, kirill.shutemov, linux-mm,
	linux-kernel
  Cc: pankaj.m, ajeet.y, hakbong5.lee, a.sahrawat, lalit.mohan, cpgs,
	Maninder Singh, Vaneet Narang

This patch adds new config VMALLOC_MEMORY_LEAK to check if any
module which is going to be unloaded is doing vmalloc memory leak.

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

Signed-off-by: Maninder Singh <maninder1.s@samsung.com>
Signed-off-by: Vaneet Narang <v.narang@samsung.com>
---
 include/linux/vmalloc.h |  2 ++
 kernel/module.c         | 28 ++++++++++++++++++++++++++++
 mm/Kconfig.debug        |  7 +++++++
 mm/vmalloc.c            |  2 --
 4 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h
index 3d9d786..abfc03c 100644
--- a/include/linux/vmalloc.h
+++ b/include/linux/vmalloc.h
@@ -29,6 +29,8 @@ struct notifier_block;		/* in notifier.h */
 #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 529efae..b492f34 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2082,9 +2082,37 @@ void __weak module_arch_freeing_init(struct module *mod)
 {
 }
 
+#ifdef CONFIG_VMALLOC_MEMORY_LEAK
+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_alert("Module %s is getting unloaded before doing vfree\n", mod->name);
+			pr_alert("Memory still allocated: addr:0x%lx - 0x%lx, pages %u\n",
+				va->va_start, va->va_end, va->vm->nr_pages);
+			pr_alert("Allocating function %pS\n", va->vm->caller);
+		}
+
+	}
+	rcu_read_unlock();
+}
+#else
+static inline void check_memory_leak(struct module *mod)
+{
+}
+#endif
+
 /* 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/Kconfig.debug b/mm/Kconfig.debug
index 22f4cd9..0bead5d 100644
--- a/mm/Kconfig.debug
+++ b/mm/Kconfig.debug
@@ -92,3 +92,10 @@ config DEBUG_PAGE_REF
 	  careful when enabling this feature because it adds about 30 KB to the
 	  kernel code.  However the runtime performance overhead is virtually
 	  nil until the tracepoints are actually enabled.
+
+config VMALLOC_MEMORY_LEAK
+	bool "Enable to check memory leaks by modules"
+	default n
+	---help---
+	  This is a feature to check if any module has allocated memory using vmalloc
+	  but getting unloaded without doing vfree.
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 91f44e7..7fba87a 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -275,8 +275,6 @@ EXPORT_SYMBOL(vmalloc_to_pfn);
 
 /*** 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

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/1] module: check if memory leak by module.
  2017-03-24 11:30   ` Maninder Singh
  (?)
@ 2017-03-27  6:43   ` Pavel Machek
  -1 siblings, 0 replies; 3+ messages in thread
From: Pavel Machek @ 2017-03-27  6:43 UTC (permalink / raw)
  To: Maninder Singh
  Cc: jeyu, rusty, akpm, chris, aryabinin, joonas.lahtinen, mhocko,
	keescook, jinb.park7, anisse, rafael.j.wysocki, zijun_hu, mingo,
	mawilcox, thgarnie, joelaf, kirill.shutemov, linux-mm,
	linux-kernel, pankaj.m, ajeet.y, hakbong5.lee, a.sahrawat,
	lalit.mohan, cpgs, Vaneet Narang

[-- Attachment #1: Type: text/plain, Size: 1803 bytes --]

Hi!

> This patch adds new config VMALLOC_MEMORY_LEAK to check if any
> module which is going to be unloaded is doing vmalloc memory leak.
> 
> Logs:-
> [  129.336368] Module vmalloc is getting unloaded before doing vfree
> [  129.336371] Memory still allocated: addr:0xffffc90001461000 - 0xffffc900014c7000, pages 101
> [  129.336376] Allocating function kernel_init+0x1c/0x20 [vmalloc]
> 
> Signed-off-by: Maninder Singh <maninder1.s@samsung.com>
> Signed-off-by: Vaneet Narang <v.narang@samsung.com>

Let me see...

> diff --git a/kernel/module.c b/kernel/module.c
> index 529efae..b492f34 100644
> --- a/kernel/module.c
> +++ b/kernel/module.c
> @@ -2082,9 +2082,37 @@ void __weak module_arch_freeing_init(struct module *mod)
>  {
>  }
>  
> +#ifdef CONFIG_VMALLOC_MEMORY_LEAK

I'd not make this optional -- the performance cost is not all that
big, right?

> +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) {

Two spaces after "+".

> +			pr_alert("Module %s is getting unloaded before doing vfree\n", mod->name);
> +			pr_alert("Memory still allocated: addr:0x%lx - 0x%lx, pages %u\n",
> +				va->va_start, va->va_end, va->vm->nr_pages);
> +			pr_alert("Allocating function %pS\n", va->vm->caller);
> +		}

Plain pr_err() would be preffered. Its just a memory leak.

Otherwise looks good to me..
									Pavel

-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-03-27  6:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20170324113058epcas5p48d9b7cf45d62d2cf7c2146ebc8719542@epcas5p4.samsung.com>
2017-03-24 11:30 ` [PATCH 1/1] module: check if memory leak by module Maninder Singh
2017-03-24 11:30   ` Maninder Singh
2017-03-27  6:43   ` Pavel Machek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.