Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756915AbdCXLbl (ORCPT ); Fri, 24 Mar 2017 07:31:41 -0400 Received: from mailout1.samsung.com ([203.254.224.24]:51886 "EHLO mailout1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751421AbdCXLbT (ORCPT ); Fri, 24 Mar 2017 07:31:19 -0400 X-AuditID: b6c32a2d-f793d6d0000012b6-a6-58d50372b25b From: Maninder Singh To: jeyu@redhat.com, rusty@rustcorp.com.au, akpm@linux-foundation.org, chris@chris-wilson.co.uk, aryabinin@virtuozzo.com, joonas.lahtinen@linux.intel.com, mhocko@suse.com, keescook@chromium.org, pavel@ucw.cz, jinb.park7@gmail.com, anisse@astier.eu, rafael.j.wysocki@intel.com, zijun_hu@htc.com, mingo@kernel.org, mawilcox@microsoft.com, thgarnie@google.com, joelaf@google.com, kirill.shutemov@linux.intel.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: pankaj.m@samsung.com, ajeet.y@samsung.com, hakbong5.lee@samsung.com, a.sahrawat@samsung.com, lalit.mohan@samsung.com, cpgs@samsung.com, Maninder Singh , Vaneet Narang Subject: [PATCH 1/1] module: check if memory leak by module. Date: Fri, 24 Mar 2017 17:00:28 +0530 Message-id: <1490355028-13292-1-git-send-email-maninder1.s@samsung.com> X-Mailer: git-send-email 1.9.1 X-Brightmail-Tracker: H4sIAAAAAAAAA0VTb0wbZRzO27velbkuZ8HxitPUc3wApaO1hXcG3AioF2cWDH7AkYVd4Cw4 +ie9ljijsSRS+TPZAIUKbEAmE5CsW0Mn1DFJGxjdWKSMKtWtTCVxdICKKGHNpr0dxm/P8/ye 5/29eZKfDFO0kCmySqOVsxjZKprYhl/0p6VnWLBQcabnGx0Kfs2hO9GTOOpyDRFoMjRLoPWW VRKFf7ouRUu+NORY2SRQ+GYviZbveqRoc45GY70SNN1oQOFfu3B04kwWuuHtIlBk6B8p8nc7 cHTPp0YLTfvRlw+uAhRZ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naW5lZXI=?= X-Global-Sender: =?UTF-8?B?TWFuaW5kZXIgU2luZ2gbU1JJLURlbGhpLVBsYXRmb3JtIFMv?= =?UTF-8?B?VyAxIFRlYW0bU2Ftc3VuZ8KgRWxlY3Ryb25pY3PCoBtMZWFkIEVuZ2luZWVy?= X-Sender-Code: =?UTF-8?B?QzEwG1NXQUhRG0MxMElEMDJJRDAyODExNQ==?= Content-type: text/plain; charset=utf-8 X-CPGSPASS: Y X-MTR: 20170324113058epcas5p48d9b7cf45d62d2cf7c2146ebc8719542 X-EPHeader: CA CMS-TYPE: 105P DLP-Filter: Pass X-CFilter-Loop: Reflected X-Auth-Email: maninder1.s@samsung.com X-HopCount: 7 X-CMS-RootMailID: 20170324113058epcas5p48d9b7cf45d62d2cf7c2146ebc8719542 X-RootMTR: 20170324113058epcas5p48d9b7cf45d62d2cf7c2146ebc8719542 References: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3119 Lines: 102 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 Signed-off-by: Vaneet Narang --- 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