Kernel memory leak detector
Subsystems (12)
Catalin Marinas
maintainercover Kernel memory leak detector
Hi,
That's the latest version of kmemleak. It is also available from:
git://linux-arm.org/linux-2.6.git kmemleak
For the past couple of months, kmemleak lived in the linux-next without major incidents. I consider that the implementation is stable for mainline inclusion.
Is there a consensus on whether kmemleak should be merged into the 2.6.31-rc1 kernel?
Are there any other comments on the code?
Thanks.
Catalin Marinas (14):
kmemleak: Add the corresponding MAINTAINERS entry
kmemleak: Simple testing module for kmemleak
kmemleak: Enable the building of the memory leak detector
kmemleak: Remove some of the kmemleak false positives
arm: Provide _sdata and __bss_stop in the vmlinux.lds.S file
x86: Provide _sdata in the vmlinux_*.lds.S files
kmemleak: Add modules support
kmemleak: Add kmemleak_alloc callback from alloc_large_system_hash
kmemleak: Add the vmalloc memory allocation/freeing hooks
kmemleak: Add the slub memory allocation/freeing hooks
kmemleak: Add the slob memory allocation/freeing hooks
kmemleak: Add the slab memory allocation/freeing hooks
kmemleak: Add documentation on the memory leak detector
kmemleak: Add the base support
Documentation/kernel-parameters.txt | 4 Documentation/kmemleak.txt | 142 +++ MAINTAINERS | 6 arch/arm/kernel/vmlinux.lds.S | 2 arch/x86/kernel/vmlinux_32.lds.S | 1 arch/x86/kernel/vmlinux_64.lds.S | 1 drivers/char/vt.c | 7 fs/block_dev.c | 6 include/linux/kmemleak.h | 96 ++ include/linux/percpu.h | 5 include/linux/slab.h | 2 init/main.c | 4 kernel/module.c | 56 + lib/Kconfig.debug | 33 + mm/Makefile | 2 mm/kmemleak-test.c | 111 +++ mm/kmemleak.c | 1498 +++++++++++++++++++++++++++++++++++ mm/page_alloc.c | 11 mm/slab.c | 32 + mm/slob.c | 7 mm/slub.c | 5 mm/vmalloc.c | 30 + 22 files changed, 2054 insertions(+), 7 deletions(-) create mode 100644 Documentation/kmemleak.txt create mode 100644 include/linux/kmemleak.h create mode 100644 mm/kmemleak-test.c create mode 100644 mm/kmemleak.c
Signature · 1 line
Catalin
Catalin Marinas
maintainerPATCH 1/14 kmemleak: Add the base support
This patch adds the base support for the kernel memory leak detector. It traces the memory allocation/freeing in a way similar to the Boehm's conservative garbage collector, the difference being that the unreferenced objects are not freed but only shown in /sys/kernel/debug/kmemleak. Enabling this feature introduces an overhead to memory allocations.
--- include/linux/kmemleak.h | 96 +++ init/main.c | 4 mm/kmemleak.c | 1498 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1597 insertions(+), 1 deletions(-) create mode 100644 include/linux/kmemleak.h create mode 100644 mm/kmemleak.c
include/linux/kmemleak.h new file +96 -0
@@ -0,0 +1,96 @@ +/* +* include/linux/kmemleak.h +* +* Copyright (C) 2008 ARM Limited +* Written by Catalin Marinas <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef __KMEMLEAK_H +#define __KMEMLEAK_H + +#ifdef CONFIG_DEBUG_KMEMLEAK + +extern void kmemleak_init (void ); +extern void kmemleak_alloc (const void *ptr,size_t size,int min_count, +gfp_t gfp); +extern void kmemleak_free (const void *ptr); +extern void kmemleak_padding (const void *ptr,unsigned long offset, +size_t size); +extern void kmemleak_not_leak (const void *ptr); +extern void kmemleak_ignore (const void *ptr); +extern void kmemleak_scan_area (const void *ptr,unsigned long offset, +size_t length,gfp_t gfp); +extern void kmemleak_no_scan (const void *ptr); + +static inline void kmemleak_alloc_recursive (const void *ptr,size_t size, +int min_count,unsigned long flags, +gfp_t gfp) +{ +if (!(flags &SLAB_NOLEAKTRACE )) +kmemleak_alloc (ptr, size, min_count, gfp); +} + +static inline void kmemleak_free_recursive (const void *ptr,unsigned long flags) +{ +if (!(flags &SLAB_NOLEAKTRACE )) +kmemleak_free (ptr); +} + +static inline void kmemleak_erase (void **ptr) +{ + *ptr =NULL ; +} + +#else + +static inline void kmemleak_init (void ) +{ +} +static inline void kmemleak_alloc (const void *ptr,size_t size,int min_count, +gfp_t gfp) +{ +} +static inline void kmemleak_alloc_recursive (const void *ptr,size_t size, +int min_count,unsigned long flags, +gfp_t gfp) +{ +} +static inline void kmemleak_free (const void *ptr) +{ +} +static inline void kmemleak_free_recursive (const void *ptr,unsigned long flags) +{ +} +static inline void kmemleak_not_leak (const void *ptr) +{ +} +static inline void kmemleak_ignore (const void *ptr) +{ +} +static inline void kmemleak_scan_area (const void *ptr,unsigned long offset, +size_t length,gfp_t gfp) +{ +} +static inline void kmemleak_erase (void **ptr) +{ +} +static inline void kmemleak_no_scan (const void *ptr) +{ +} + +#endif /* CONFIG_DEBUG_KMEMLEAK */ + +#endif /* __KMEMLEAK_H */
init/main.c +3 -1
@@ -56,6 +56,7 @@#include <linux/debug_locks.h> #include <linux/debugobjects.h> #include <linux/lockdep.h> +#include <linux/kmemleak.h> #include <linux/pid_namespace.h> #include <linux/device.h> #include <linux/kthread.h>
@@ -604,6 +605,7 @@ asmlinkage void __init start_kernel(void)/* init some links before init_ISA_irqs() */ early_irq_init ();init_IRQ (); +prio_tree_init ();pidhash_init ();init_timers ();hrtimers_init ();
@@ -655,6 +657,7 @@ asmlinkage void __init start_kernel(void)cpu_hotplug_init ();kmem_cache_init ();kmemtrace_init (); +kmemleak_init ();debug_objects_mem_init ();idr_init_cache ();setup_per_cpu_pageset ();
@@ -664,7 +667,6 @@ asmlinkage void __init start_kernel(void)calibrate_delay ();pidmap_init ();pgtable_cache_init (); -prio_tree_init ();anon_vma_init ();#ifdef CONFIG_X86 if (efi_enabled)
mm/kmemleak.c new file +1498 -0
1,498 lines not on this page · raw message
Catalin Marinas
maintainerPATCH 2/14 kmemleak: Add documentation on the memory leak detector
In reply to Catalin MarinasThis patch adds the Documentation/kmemleak.txt file with some information about how kmemleak works.
--- Documentation/kernel-parameters.txt | 4 + Documentation/kmemleak.txt | 142 +++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 0 deletions(-) create mode 100644 Documentation/kmemleak.txt
Documentation/kernel-parameters.txt +4 -0
@@ -1066,6 +1066,10 @@ and is between 256 and 4096 characters. It is defined in the file Configure the RouterBoard 532 series on-chip Ethernet adapter MAC address. + kmemleak= [KNL] Boot-time kmemleak enable/disable + Valid arguments: on, off + Default: on + kstack=N [X86] Print N words from the kernel stack in oops dumps.
Documentation/kmemleak.txt new file +142 -0
142 lines not on this page · raw message
Catalin Marinas
maintainerPATCH 3/14 kmemleak: Add the slab memory allocation/freeing hooks
In reply to Catalin MarinasThis patch adds the callbacks to kmemleak_(alloc|free) functions from the slab allocator. The patch also adds the SLAB_NOLEAKTRACE flag to avoid recursive calls to kmemleak when it allocates its own data structures.
--- include/linux/slab.h | 2 ++ mm/slab.c | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-)
include/linux/slab.h +2 -0
@@ -62,6 +62,8 @@# define SLAB_DEBUG_OBJECTS 0x00000000UL#endif +#define SLAB_NOLEAKTRACE 0x00800000UL/* Avoid kmemleak tracing */ +/* The following flags affect the page allocator grouping pages by mobility */ #define SLAB_RECLAIM_ACCOUNT 0x00020000UL/* Objects are reclaimable */ #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT/* Objects are short-lived */
mm/slab.c +30 -2
@@ -107,6 +107,7 @@#include <linux/string.h> #include <linux/uaccess.h> #include <linux/nodemask.h> +#include <linux/kmemleak.h> #include <linux/mempolicy.h> #include <linux/mutex.h> #include <linux/fault-inject.h>
@@ -178,13 +179,13 @@ SLAB_STORE_USER | \ SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \ SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \ - SLAB_DEBUG_OBJECTS) + SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE)#else # define CREATE_MASK (SLAB_HWCACHE_ALIGN | \ SLAB_CACHE_DMA | \ SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \ SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \ - SLAB_DEBUG_OBJECTS) + SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE)#endif /*
@@ -964,6 +965,14 @@ static struct array_cache *alloc_arraycache(int node, int entries,struct array_cache *nc =NULL ; nc =kmalloc_node (memsize,GFP_KERNEL , node); +/* +* The array_cache structures contain pointers to free object. +* However, when such objects are allocated or transfered to another +* cache the pointers are not cleared and they could be counted as +* valid references during a kmemleak scan. Therefore, kmemleak must +* not scan such objects. +*/ +kmemleak_no_scan (nc);if (nc) { nc->avail =0 ; nc->limit = entries;
@@ -2621,6 +2630,14 @@ static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,/* Slab management obj is off-slab. */ slabp =kmem_cache_alloc_node (cachep->slabp_cache, local_flags, nodeid); +/* +* If the first object in the slab is leaked (it's allocated +* but no one has a reference to it), we want to make sure +* kmemleak does not treat the ->s_mem pointer as a reference +* to the object. Otherwise we will not report the leak. +*/ +kmemleak_scan_area (slabp, offsetof(struct slab , list), +sizeof (struct list_head ), local_flags);if (!slabp)return NULL ; }else {
@@ -3141,6 +3158,12 @@ static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)STATS_INC_ALLOCMISS (cachep); objp =cache_alloc_refill (cachep, flags); } +/* +* To avoid a false negative, if an object that is in one of the +* per-CPU caches is leaked, we need to make sure kmemleak doesn't +* treat the array pointers as a reference to the object. +*/ +kmemleak_erase (&ac->entry[ac->avail]);return objp; }
@@ -3360,6 +3383,8 @@ __cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid, out:local_irq_restore (save_flags); ptr =cache_alloc_debugcheck_after (cachep, flags, ptr, caller); +kmemleak_alloc_recursive (ptr,obj_size (cachep),1 , cachep->flags, + flags);if (unlikely ((flags & __GFP_ZERO) && ptr))memset (ptr,0 ,obj_size (cachep));
@@ -3415,6 +3440,8 @@ __cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller) objp =__do_cache_alloc (cachep, flags);local_irq_restore (save_flags); objp =cache_alloc_debugcheck_after (cachep, flags, objp, caller); +kmemleak_alloc_recursive (objp,obj_size (cachep),1 , cachep->flags, + flags);prefetchw (objp);if (unlikely ((flags & __GFP_ZERO) && objp))
@@ -3530,6 +3557,7 @@ static inline void __cache_free(struct kmem_cache *cachep, void *objp)struct array_cache *ac =cpu_cache_get (cachep);check_irq_off (); +kmemleak_free_recursive (objp, cachep->flags); objp =cache_free_debugcheck (cachep, objp,__builtin_return_address (0 ));/*
Catalin Marinas
maintainerPATCH 4/14 kmemleak: Add the slob memory allocation/freeing hooks
In reply to Catalin MarinasThis patch adds the callbacks to kmemleak_(alloc|free) functions from the slob allocator.
--- mm/slob.c | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)
mm/slob.c +7 -0
@@ -60,6 +60,7 @@#include <linux/kernel.h> #include <linux/slab.h> #include <linux/mm.h> +#include <linux/kmemleak.h> #include <linux/cache.h> #include <linux/init.h> #include <linux/module.h>
@@ -506,6 +507,7 @@ void *__kmalloc_node(size_t size, gfp_t gfp, int node) size, PAGE_SIZE << order, gfp, node); } +kmemleak_alloc (ret, size,1 , gfp);return ret; }EXPORT_SYMBOL (__kmalloc_node);
@@ -518,6 +520,7 @@ void kfree(const void *block)if (unlikely (ZERO_OR_NULL_PTR (block)))return ; +kmemleak_free (block); sp =slob_page (block);if (is_slob_page (sp)) {
@@ -581,12 +584,14 @@ struct kmem_cache *kmem_cache_create(const char *name, size_t size, }else if (flags &SLAB_PANIC )panic ("Cannot create slab cache %s\n" , name); +kmemleak_alloc (c,sizeof (struct kmem_cache ),1 ,GFP_KERNEL );return c; }EXPORT_SYMBOL (kmem_cache_create);void kmem_cache_destroy (struct kmem_cache *c) { +kmemleak_free (c);slob_free (c,sizeof (struct kmem_cache )); }EXPORT_SYMBOL (kmem_cache_destroy);
@@ -610,6 +615,7 @@ void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node)if (c->ctor) c->ctor (b); +kmemleak_alloc_recursive (b, c->size,1 , c->flags, flags);return b; }EXPORT_SYMBOL (kmem_cache_alloc_node);
@@ -632,6 +638,7 @@ static void kmem_rcu_free(struct rcu_head *head)void kmem_cache_free (struct kmem_cache *c,void *b) { +kmemleak_free_recursive (b, c->flags);if (unlikely (c->flags &SLAB_DESTROY_BY_RCU )) {struct slob_rcu *slob_rcu; slob_rcu = b + (c->size -sizeof (struct slob_rcu ));
Catalin Marinas
maintainerPATCH 5/14 kmemleak: Add the slub memory allocation/freeing hooks
In reply to Catalin MarinasThis patch adds the callbacks to kmemleak_(alloc|free) functions from the slub allocator.
--- mm/slub.c | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-)
mm/slub.c +4 -1
@@ -19,6 +19,7 @@#include <trace/kmemtrace.h> #include <linux/cpu.h> #include <linux/cpuset.h> +#include <linux/kmemleak.h> #include <linux/mempolicy.h> #include <linux/ctype.h> #include <linux/debugobjects.h>
@@ -142,7 +143,7 @@* Set of flags that will prevent slab merging */ #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ - SLAB_TRACE | SLAB_DESTROY_BY_RCU) + SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE)#define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \ SLAB_CACHE_DMA)
@@ -1614,6 +1615,7 @@ static __always_inline void *slab_alloc(struct kmem_cache *s,if (unlikely ((gfpflags & __GFP_ZERO) && object))memset (object,0 , objsize); +kmemleak_alloc_recursive (object, objsize,1 , s->flags, gfpflags);return object; }
@@ -1743,6 +1745,7 @@ static __always_inline void slab_free(struct kmem_cache *s,struct kmem_cache_cpu *c;unsigned long flags; +kmemleak_free_recursive (x, s->flags);local_irq_save (flags); c =get_cpu_slab (s,smp_processor_id ());debug_check_no_locks_freed (object, c->objsize);
Catalin Marinas
maintainerPATCH 6/14 kmemleak: Add the vmalloc memory allocation/freeing hooks
In reply to Catalin MarinasThis patch adds the callbacks to kmemleak_(alloc|free) functions from vmalloc/vfree.
--- mm/vmalloc.c | 30 +++++++++++++++++++++++++++--- 1 files changed, 27 insertions(+), 3 deletions(-)
mm/vmalloc.c +27 -3
@@ -25,6 +25,7 @@#include <linux/rcupdate.h> #include <linux/bootmem.h> #include <linux/pfn.h> +#include <linux/kmemleak.h> #include <asm/atomic.h> #include <asm/uaccess.h>
@@ -1326,6 +1327,9 @@ static void __vunmap(const void *addr, int deallocate_pages)void vfree (const void *addr) {BUG_ON (in_interrupt ()); + +kmemleak_free (addr); +__vunmap (addr,1 ); }EXPORT_SYMBOL (vfree);
@@ -1438,8 +1442,17 @@ fail:void *__vmalloc_area (struct vm_struct *area,gfp_t gfp_mask,pgprot_t prot) { -return __vmalloc_area_node (area, gfp_mask, prot,-1 , -__builtin_return_address (0 )); +void *addr =__vmalloc_area_node (area, gfp_mask, prot,-1 , +__builtin_return_address (0 )); + +/* +* A ref_count = 3 is needed because the vm_struct and vmap_area +* structures allocated in the __get_vm_area_node() function contain +* references to the virtual address of the vmalloc'ed block. +*/ +kmemleak_alloc (addr, area->size -PAGE_SIZE ,3 , gfp_mask); + +return addr; }/**
@@ -1458,6 +1471,8 @@ static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot, int node, void *caller) { struct vm_struct *area; +void *addr; +unsigned long real_size = size; size =PAGE_ALIGN (size);if (!size || (size >>PAGE_SHIFT ) > num_physpages)
@@ -1469,7 +1484,16 @@ static void *__vmalloc_node(unsigned long size, gfp_t gfp_mask, pgprot_t prot,if (!area)return NULL ; -return __vmalloc_area_node (area, gfp_mask, prot, node, caller); + addr =__vmalloc_area_node (area, gfp_mask, prot, node, caller); + +/* +* A ref_count = 3 is needed because the vm_struct and vmap_area +* structures allocated in the __get_vm_area_node() function contain +* references to the virtual address of the vmalloc'ed block. +*/ +kmemleak_alloc (addr, real_size,3 , gfp_mask); + +return addr; }void *__vmalloc (unsigned long size,gfp_t gfp_mask,pgprot_t prot)
Catalin Marinas
maintainerPATCH 7/14 kmemleak: Add kmemleak_alloc callback from alloc_large_system_hash
In reply to Catalin MarinasThe alloc_large_system_hash function is called from various places in the kernel and it contains pointers to other allocated structures. It therefore needs to be traced by kmemleak.
--- mm/page_alloc.c | 11 +++++++++++ 1 files changed, 11 insertions(+), 0 deletions(-)
mm/page_alloc.c +11 -0
@@ -46,6 +46,7 @@#include <linux/page-isolation.h> #include <linux/page_cgroup.h> #include <linux/debugobjects.h> +#include <linux/kmemleak.h> #include <asm/tlbflush.h> #include <asm/div64.h>
@@ -4597,6 +4598,16 @@ void *__init alloc_large_system_hash(const char *tablename,if (_hash_mask) *_hash_mask = (1 << log2qty) -1 ; +/* +* If hashdist is set, the table allocation is done with __vmalloc() +* which invokes the kmemleak_alloc() callback. This function may also +* be called before the slab and kmemleak are initialised when +* kmemleak simply buffers the request to be executed later +* (GFP_ATOMIC flag ignored in this case). +*/ +if (!hashdist) +kmemleak_alloc (table, size,1 ,GFP_ATOMIC ); +return table; }
Catalin Marinas
maintainerPATCH 8/14 kmemleak: Add modules support
In reply to Catalin MarinasThis patch handles the kmemleak operations needed for modules loading so that memory allocations from inside a module are properly tracked.
--- kernel/module.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 56 insertions(+), 0 deletions(-)
kernel/module.c +56 -0
@@ -52,6 +52,7 @@#include <linux/ftrace.h> #include <linux/async.h> #include <linux/percpu.h> +#include <linux/kmemleak.h> #if 0 #define DEBUGP printk
@@ -429,6 +430,7 @@ static void *percpu_modalloc(unsigned long size, unsigned long align,unsigned long extra;unsigned int i;void *ptr; +int cpu;if (align >PAGE_SIZE ) { printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
@@ -458,6 +460,11 @@ static void *percpu_modalloc(unsigned long size, unsigned long align,if (!split_block (i, size))return NULL ; +/* add the per-cpu scanning areas */ +for_each_possible_cpu (cpu) +kmemleak_alloc (ptr +per_cpu_offset (cpu), size,0 , +GFP_KERNEL ); +/* Mark allocated */ pcpu_size[i] = -pcpu_size[i];return ptr;
@@ -472,6 +479,7 @@ static void percpu_modfree(void *freeme) {unsigned int i;void *ptr = __per_cpu_start +block_size (pcpu_size[0 ]); +int cpu;/* First entry is core kernel percpu data. */ for (i =1 ; i < pcpu_num_used; ptr +=block_size (pcpu_size[i]), i++) {
@@ -483,6 +491,10 @@ static void percpu_modfree(void *freeme)BUG (); free: +/* remove the per-cpu scanning areas */ +for_each_possible_cpu (cpu) +kmemleak_free (freeme +per_cpu_offset (cpu)); +/* Merge with previous? */ if (pcpu_size[i-1 ] >=0 ) { pcpu_size[i-1 ] += pcpu_size[i];
@@ -1878,6 +1890,36 @@ static void *module_alloc_update_bounds(unsigned long size)return ret; } +#ifdef CONFIG_DEBUG_KMEMLEAK +static void kmemleak_load_module (struct module *mod,Elf_Ehdr *hdr, +Elf_Shdr *sechdrs,char *secstrings) +{ +unsigned int i; + +/* only scan the sections containing data */ +kmemleak_scan_area (mod->module_core, (unsigned long )mod - + (unsigned long )mod->module_core, +sizeof (struct module ),GFP_KERNEL ); + +for (i =1 ; i < hdr->e_shnum; i++) { +if (!(sechdrs[i].sh_flags &SHF_ALLOC )) +continue ; +if (strncmp (secstrings + sechdrs[i].sh_name,".data" ,5 ) !=0 + &&strncmp (secstrings + sechdrs[i].sh_name,".bss" ,4 ) !=0 ) +continue ; + +kmemleak_scan_area (mod->module_core, sechdrs[i].sh_addr - + (unsigned long )mod->module_core, + sechdrs[i].sh_size,GFP_KERNEL ); + } +} +#else +static inline void kmemleak_load_module (struct module *mod,Elf_Ehdr *hdr, +Elf_Shdr *sechdrs,char *secstrings) +{ +} +#endif +/* Allocate and load the module: note that size of section 0 is always zero, and we rely on this for optional sections. */ static noinline struct module *load_module(void __user *umod,
@@ -2050,6 +2092,12 @@ static noinline struct module *load_module(void __user *umod,/* Do the allocs. */ ptr =module_alloc_update_bounds (mod->core_size); +/* +* The pointer to this block is stored in the module structure +* which is inside the block. Just mark it as not being a +* leak. +*/ +kmemleak_not_leak (ptr);if (!ptr) { err = -ENOMEM ; goto free_percpu;
@@ -2058,6 +2106,13 @@ static noinline struct module *load_module(void __user *umod, mod->module_core = ptr; ptr =module_alloc_update_bounds (mod->init_size); +/* +* The pointer to this block is stored in the module structure +* which is inside the block. This block doesn't need to be +* scanned as it contains data and code that will be freed +* after the module is initialized. +*/ +kmemleak_ignore (ptr);if (!ptr && mod->init_size) { err = -ENOMEM ; goto free_core;
@@ -2088,6 +2143,7 @@ static noinline struct module *load_module(void __user *umod, }/* Module has been moved. */ mod = (void *)sechdrs[modindex].sh_addr; +kmemleak_load_module (mod, hdr, sechdrs, secstrings);#if defined(CONFIG_MODULE_UNLOAD ) && defined(CONFIG_SMP ) mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
Catalin Marinas
maintainerPATCH 9/14 x86: Provide _sdata in the vmlinux_*.lds.S files
In reply to Catalin Marinas_sdata is a common symbol defined by many architectures and made available to the kernel via asm-generic/sections.h. Kmemleak uses this symbol when scanning the data sections.
--- arch/x86/kernel/vmlinux_32.lds.S | 1 + arch/x86/kernel/vmlinux_64.lds.S | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/kernel/vmlinux_32.lds.S +1 -0
@@ -63,6 +63,7 @@ SECTIONS /* writeable */ . = ALIGN(PAGE_SIZE); + _sdata = .; /* Start of data section */ .data : AT(ADDR(.data) - LOAD_OFFSET) { /* Data */ DATA_DATA CONSTRUCTORS
arch/x86/kernel/vmlinux_64.lds.S +1 -0
@@ -57,6 +57,7 @@ SECTIONS RODATA . = ALIGN(PAGE_SIZE); /* Align data segment to page size boundary */ + _sdata = .; /* Start of data section */ /* Data */ .data : AT(ADDR(.data) - LOAD_OFFSET) { DATA_DATA
Re: [PATCH 09/14] x86: Provide _sdata in the vmlinux_*.lds.S files
Quoted 10 lines · Catalin Marinas
_sdatais a common symbol defined by many architectures and made available to the kernel viaasm-generic/sections.h. Kmemleak uses this symbol when scanning the data sections.Signed-off-by Catalin Marinas <[email protected]>Cc Ingo Molnar <[email protected]>--- arch/x86/kernel/vmlinux_32.lds.S | 1 + arch/x86/kernel/vmlinux_64.lds.S | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)
i tought i acked this before but apparently not, here it is:
Ingo
Catalin Marinas
maintainerPATCH 10/14 arm: Provide _sdata and __bss_stop in the vmlinux.lds.S file
In reply to Catalin Marinas_sdata and __bss_stop are common symbols defined by many architectures and made available to the kernel via asm-generic/sections.h. Kmemleak uses these symbols when scanning the data sections.
--- arch/arm/kernel/vmlinux.lds.S | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
arch/arm/kernel/vmlinux.lds.S +2 -0
@@ -141,6 +141,7 @@ SECTIONS .data : AT(__data_loc) { _data = .; /* address in memory */ + _sdata = .; /* * first, the init task union, aligned
@@ -192,6 +193,7 @@ SECTIONS __bss_start = .; /* BSS */ *(.bss) *(COMMON) + __bss_stop = .; _end = .; } /* Stabs debugging sections. */
Catalin Marinas
maintainerPATCH 11/14 kmemleak: Remove some of the kmemleak false positives
In reply to Catalin MarinasThere are allocations for which the main pointer cannot be found but they are not memory leaks. This patch fixes some of them. For more information on false positives, see Documentation/kmemleak.txt.
--- drivers/char/vt.c | 7 +++++++ fs/block_dev.c | 6 ++++++ include/linux/percpu.h | 5 +++++ 3 files changed, 18 insertions(+), 0 deletions(-)
drivers/char/vt.c +7 -0
@@ -104,6 +104,7 @@#include <linux/io.h> #include <asm/system.h> #include <linux/uaccess.h> +#include <linux/kmemleak.h> #define MAX_NR_CON_DRIVER 16
@@ -2880,6 +2881,12 @@ static int __init con_init(void)*/ for (currcons =0 ; currcons <MIN_NR_CONSOLES ; currcons++) { vc_cons[currcons].d = vc =alloc_bootmem (sizeof (struct vc_data )); +/* +* Kmemleak does not track the memory allocated via +* alloc_bootmem() but this block contains pointers to +* other blocks allocated via kmalloc. +*/ +kmemleak_alloc (vc,sizeof (struct vc_data ),1 ,GFP_ATOMIC );INIT_WORK (&vc_cons[currcons].SAK_work, vc_SAK);visual_init (vc, currcons,1 ); vc->vc_screenbuf = (unsigned short *)alloc_bootmem (vc->vc_screenbuf_size);
fs/block_dev.c +6 -0
@@ -25,6 +25,7 @@#include <linux/uio.h> #include <linux/namei.h> #include <linux/log2.h> +#include <linux/kmemleak.h> #include <asm/uaccess.h> #include "internal.h"
@@ -492,6 +493,11 @@ void __init bdev_cache_init(void) bd_mnt =kern_mount (&bd_type);if (IS_ERR (bd_mnt))panic ("Cannot create bdev pseudo-fs" ); +/* +* This vfsmount structure is only used to obtain the +* blockdev_superblock, so tell kmemleak not to report it. +*/ +kmemleak_not_leak (bd_mnt); blockdev_superblock = bd_mnt->mnt_sb;/* For writeback */ }
include/linux/percpu.h +5 -0
@@ -130,7 +130,12 @@ struct percpu_data {void *ptrs[1 ]; }; +/* pointer disguising messes up the kmemleak objects tracking */ +#ifndef CONFIG_DEBUG_KMEMLEAK #define __percpu_disguise (pdata) (struct percpu_data *)~(unsigned long)(pdata) +#else +#define __percpu_disguise (pdata) (struct percpu_data *)(pdata) +#endif #define per_cpu_ptr (ptr, cpu) \ ({ \
Catalin Marinas
maintainerPATCH 12/14 kmemleak: Enable the building of the memory leak detector
In reply to Catalin MarinasThis patch adds the Kconfig.debug and Makefile entries needed for building kmemleak into the kernel.
--- lib/Kconfig.debug | 22 ++++++++++++++++++++++ mm/Makefile | 1 + 2 files changed, 23 insertions(+), 0 deletions(-)
lib/Kconfig.debug +22 -0
@@ -336,6 +336,28 @@ config SLUB_STATS out which slabs are relevant to a particular load. Try running: slabinfo -DA +config DEBUG_KMEMLEAK + bool "Kernel memory leak detector" + default n + depends on EXPERIMENTAL && (X86 || ARM) && !MEMORY_HOTPLUG + select DEBUG_SLAB if SLAB + select SLUB_DEBUG if SLUB + select DEBUG_FS if SYSFS + select STACKTRACE if STACKTRACE_SUPPORT + select KALLSYMS + help + Say Y here if you want to enable the memory leak + detector. The memory allocation/freeing is traced in a way + similar to the Boehm's conservative garbage collector, the + difference being that the orphan objects are not freed but + only shown in /sys/kernel/debug/kmemleak. Enabling this + feature will introduce an overhead to memory + allocations. See Documentation/kmemleak.txt for more + details. + + In order to access the kmemleak file, debugfs needs to be + mounted (usually at /sys/kernel/debug). + config DEBUG_PREEMPT bool "Debug preemptible kernel" depends on DEBUG_KERNEL && PREEMPT && (TRACE_IRQFLAGS_SUPPORT || PPC64)
mm/Makefile +1 -0
@@ -38,3 +38,4 @@ obj-$(CONFIG_SMP) += allocpercpu.o endif obj-$(CONFIG_QUICKLIST) += quicklist.o obj-$(CONFIG_CGROUP_MEM_RES_CTLR) += memcontrol.o page_cgroup.o +obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o
David Rientjes
reviewerRe: [PATCH 12/14] kmemleak: Enable the building of the memory leak detector
Quoted 14 lines · Catalin Marinas
This patch adds the
Kconfig.debugandMakefileentries needed for building kmemleak into the kernel.Signed-off-by Catalin Marinas <[email protected]>--- lib/Kconfig.debug | 22 ++++++++++++++++++++++ mm/Makefile | 1 + 2 files changed, 23 insertions(+), 0 deletions(-)lib/Kconfig.debug +0
-0
lib/Kconfig.debug config SLUB_STATS · line 342
Quoted 7 lines
lib/Kconfig.debug +4
-0@@ -336,6 +336,28 @@ config SLUB_STATS out which slabs are relevant to a particular load. Try running: slabinfo -DA +config DEBUG_KMEMLEAK + bool "Kernel memory leak detector" + default n + depends on EXPERIMENTAL && (X86 || ARM) && !MEMORY_HOTPLUG
This should include DEBUG_KERNEL.
Catalin Marinas
maintainerRe: [PATCH 12/14] kmemleak: Enable the building of the memory leak detector
Quoted 19 lines · David Rientjes
Quoted 17 lines · Catalin Marinas
This patch adds the
Kconfig.debugandMakefileentries needed for building kmemleak into the kernel.Signed-off-by Catalin Marinas <[email protected]>--- lib/Kconfig.debug | 22 ++++++++++++++++++++++ mm/Makefile | 1 + 2 files changed, 23 insertions(+), 0 deletions(-)lib/Kconfig.debug +0
-0@@ -336,6 +336,28 @@ config SLUB_STATS out which slabs are relevant to a particular load. Try running: slabinfo -DA
Quoted 6 lines
lib/Kconfig.debug config SLUB_STATS · line 342
Quoted 4 lines
lib/Kconfig.debug +4
-0+config DEBUG_KMEMLEAK + bool "Kernel memory leak detector" + default n + depends on EXPERIMENTAL && (X86 || ARM) && !MEMORY_HOTPLUGThis should include
DEBUG_KERNEL.
Yes, it makes sense. I'll update the patch.
Thanks.
Signature · 1 line
Catalin
Catalin Marinas
maintainerPATCH 13/14 kmemleak: Simple testing module for kmemleak
In reply to Catalin MarinasThis patch adds a loadable module that deliberately leaks memory. It is used for testing various memory leaking scenarios.
--- lib/Kconfig.debug | 11 +++++ mm/Makefile | 1 mm/kmemleak-test.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 0 deletions(-) create mode 100644 mm/kmemleak-test.c
lib/Kconfig.debug +11 -0
@@ -358,6 +358,17 @@ config DEBUG_KMEMLEAK In order to access the kmemleak file, debugfs needs to be mounted (usually at /sys/kernel/debug). +config DEBUG_KMEMLEAK_TEST + tristate "Simple test for the kernel memory leak detector" + default n + depends on DEBUG_KMEMLEAK + help + Say Y or M here to build a test for the kernel memory leak + detector. This option enables a module that explicitly leaks + memory. + + If unsure, say N. + config DEBUG_PREEMPT bool "Debug preemptible kernel" depends on DEBUG_KERNEL && PREEMPT && (TRACE_IRQFLAGS_SUPPORT || PPC64)
mm/Makefile +1 -0
@@ -39,3 +39,4 @@ endif obj-$(CONFIG_QUICKLIST) += quicklist.o obj-$(CONFIG_CGROUP_MEM_RES_CTLR) += memcontrol.o page_cgroup.o obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o +obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak-test.o
mm/kmemleak-test.c new file +111 -0
111 lines not on this page · raw message
Catalin Marinas
maintainerPATCH 14/14 kmemleak: Add the corresponding MAINTAINERS entry
In reply to Catalin Marinas--- MAINTAINERS | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
MAINTAINERS +6 -0
@@ -3344,6 +3344,12 @@ F: Documentation/vm/kmemtrace.txt F: include/trace/kmemtrace.h F: kernel/trace/kmemtrace.c +KMEMLEAK +P: Catalin Marinas +M: [email protected] +L: [email protected] +S: Maintained + KPROBES P: Ananth N Mavinakayanahalli M: [email protected]