Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753342AbdC2GDX (ORCPT ); Wed, 29 Mar 2017 02:03:23 -0400 Received: from mailout2.samsung.com ([203.254.224.25]:12215 "EHLO epoutp02.samsung.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753254AbdC2GDT (ORCPT ); Wed, 29 Mar 2017 02:03:19 -0400 X-AuditID: b6c32a2d-f792a6d0000055a1-6b-58db4e235d64 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 v2] module: check if memory leak by module. Date: Wed, 29 Mar 2017 11:32:02 +0530 Message-Id: <1490767322-9914-1-git-send-email-maninder1.s@samsung.com> X-Mailer: git-send-email 1.9.1 X-Brightmail-Tracker: H4sIAAAAAAAAA0VTbUxTVxjO6b29t7h1XotjJ83UehfDIGuhTUuPQ+YXczdhWTD8IS6E3cEN baQt620N3bJYxyAMmPixKKuAIMFBZeFDXBAQlzrQOsjA0a0EW9kkQxntjPCnVt3aXrb9e57n fd7nvO+bHAkmO03KJUazjbOa2XKa2IB/dyMtXfnae/OFmddr09DMCIceLJ/AUXNvD4EmfHcI tHYqTKK536bE6KEnDdWEIgSau9tOopU/r4hRZJZG19pFaLLehOaWmnHU2JGFfh5uJlCw528x unG+BkdPPGp07/gedOn5bYCCK1dEKHB7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qJzeLLpHfsFjcfnmW1eHlI06Lt7U82i5t3FrJbvHm9ldXi5xUli70LmSzOdOda 3Hw+h8Wif7GZxeVdc9gs7q35z2pxeH4bi8WvQ4YW9/scLFb/O8Voce/NViaLu6eOslk8XvEW aO60CywWPb8vMlscOjmX0aLxyQVWBxmPhZeOs3ms/KDlMbvhIovHzll32T0WbCr1eHegh91j 8Z6XTB6bVnWyeWz6NInd48SM3ywe804GerTu+Mvu8X7fVTaPFRtOMHv0bVnF6LF+y1WWAOEo LpuU1JzMstQifbsEroz5i1vZCvYLV9y4OJuxgfEffxcjB4eEgInEomdRXYycQKaYxIV769lA bCGB7YwSq14IQ8TVJZbMvs8KYQtLrPz3nL2LkQuo5hOjxMXbT1hAEmwCehKrdu1hAUmICHxg lni6/SZYFbPAC0aJ9/u2gI0VFrCWWPXnJ5jNIqAqcX3fIzCbV8BNYlpPNyPECjmJk8cms05g 5F3AyLCKUTS1oDg3Pbe4wEivODG3uDQvXS85P3cTIzgGtZJ2MG6aEX6IUYCDUYmHd8XqWxFC rIllxZW5hxglOJiVRHj1FW9HCPGmJFZWpRblxxeV5qQWH2KU5mBREufdXr0hQkggPbEkNTs1 tSC1CCbLxMEp1cAYuUJbP/fizuWGHTMWMQmKyH1sZ2SavSloldzRN/9bL34zW1B++X/T2nt7 7uRcna0Rve3BhhJdqS9ZMy7tS6phOWH3trPNeOb0xh+7FXj8Uy5y8M7vEqj2P+1lpS668mty 5qagRAP7hqA9fdXvZP3vX/y9e4/6snvXJu5ub7E5bfv5X/DkAIENSizFGYmGWsxFxYkA+P2W lr0CAAA= X-CMS-MailID: 20170329060315epcas5p1c6f7ce3aca1b2770c5e1d9aaeb1a27e1 X-Msg-Generator: CA X-Sender-IP: 182.195.40.14 X-Local-Sender: =?UTF-8?B?TWFuaW5kZXIgU2luZ2gbU1JJLURlbGhpLVBsYXRmb3JtIFMv?= =?UTF-8?B?VyAxIFRlYW0b7IK87ISx7KCE7J6QG0xlYWQgRW5naW5lZXI=?= 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: 20170329060315epcas5p1c6f7ce3aca1b2770c5e1d9aaeb1a27e1 X-EPHeader: CA CMS-TYPE: 105P DLP-Filter: Pass X-CFilter-Loop: Reflected X-CMS-RootMailID: 20170329060315epcas5p1c6f7ce3aca1b2770c5e1d9aaeb1a27e1 X-RootMTR: 20170329060315epcas5p1c6f7ce3aca1b2770c5e1d9aaeb1a27e1 References: Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2410 Lines: 83 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 Signed-off-by: Maninder Singh --- 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