Kmemleak patches for 2.6.33
Subsystems (2)
Catalin Marinas
maintainercover Kmemleak patches for 2.6.33
This is a series of kmemleak patches for the upcoming merging window. The final patch in the series changes the leak reporting logic a bit to reduce the transient false positives.
If there are no objections, I'll push them to the -next tree. Thanks.
Catalin Marinas (4):
kmemleak: Simplify the kmemleak_scan_area() function prototype
kmemleak: Scan the _ftrace_events section in modules
kmemleak: Store object reverse references for debugging purposes
kmemleak: Allow two scanning passes before reported an object as leak
include/linux/kmemleak.h | 8 +-- kernel/module.c | 13 +++- mm/kmemleak.c | 141 ++++++++++++++++++---------------------------- mm/slab.c | 4 + 4 files changed, 68 insertions(+), 98 deletions(-)
Signature · 1 line
Catalin
Catalin Marinas
maintainerPATCH 1/4 kmemleak: Simplify the kmemleak_scan_area() function prototype
This function was taking non-necessary arguments which can be determined by kmemleak. The patch also modifies the calling sites.
--- include/linux/kmemleak.h | 8 ++++---- kernel/module.c | 7 ++----- mm/kmemleak.c | 49 ++++++++++++++++++++-------------------------- mm/slab.c | 4 ++-- 4 files changed, 29 insertions(+), 39 deletions(-)
include/linux/kmemleak.h +4 -4
@@ -32,8 +32,8 @@ extern void kmemleak_padding(const void *ptr, unsigned long offset, size_t size) __ref;extern void kmemleak_not_leak (const void *ptr)__ref ;extern void kmemleak_ignore (const void *ptr)__ref ; -extern void kmemleak_scan_area (const void *ptr,unsigned long offset, -size_t length,gfp_t gfp)__ref ; +extern void kmemleak_scan_area (const void *ptr,size_t length, +gfp_t gfp)__ref ;extern void kmemleak_no_scan (const void *ptr)__ref ; static inline void kmemleak_alloc_recursive(const void *ptr, size_t size,
@@ -84,8 +84,8 @@ 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_scan_area (const void *ptr,size_t length, +gfp_t gfp) { }static inline void kmemleak_erase (void **ptr)
kernel/module.c +2 -5
@@ -2043,9 +2043,7 @@ static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,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 ); +kmemleak_scan_area (mod,sizeof (struct module ),GFP_KERNEL );for (i =1 ; i < hdr->e_shnum; i++) {if (!(sechdrs[i].sh_flags &SHF_ALLOC ))
@@ -2054,8 +2052,7 @@ static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr, && 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, +kmemleak_scan_area ((void *)sechdrs[i].sh_addr, sechdrs[i].sh_size,GFP_KERNEL ); } }
mm/kmemleak.c +21 -28
155 lines not on this page · raw message
mm/slab.c +2 -2
@@ -2584,8 +2584,8 @@ static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,* 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); +kmemleak_scan_area (&slabp->list,sizeof (struct list_head ), + local_flags);if (!slabp)return NULL ; }else {
Catalin Marinas
maintainerPATCH 2/4 kmemleak: Scan the _ftrace_events section in modules
In reply to Catalin MarinasThis section contains pointers to allocated objects and not scanning it leads to false positives.
--- kernel/module.c | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
kernel/module.c +6 -0
@@ -2380,6 +2380,12 @@ static noinline struct module *load_module(void __user *umod,"_ftrace_events" ,sizeof (*mod->trace_events), &mod->num_trace_events); +/* +* This section contains pointers to allocated objects in the trace +* code and not scanning it leads to false positives. +*/ +kmemleak_scan_area (mod->trace_events,sizeof (*mod->trace_events) * + mod->num_trace_events,GFP_KERNEL );#endif #ifdef CONFIG_FTRACE_MCOUNT_RECORD /* sechdrs[0].sh_size is always zero */
Re: [PATCH 2/4] kmemleak: Scan the _ftrace_events section in modules
Quoted 2 lines · Catalin Marinas
This section contains pointers to allocated objects and not scanning it leads to false positives.
Thanks, applied. Want me to push this for 2.6.32?
Rusty.
Catalin Marinas
maintainerRe: [PATCH 2/4] kmemleak: Scan the _ftrace_events section in modules
Quoted 5 lines · Rusty Russell
Quoted 2 lines · Catalin Marinas
This section contains pointers to allocated objects and not scanning it leads to false positives.
Thanks, applied. Want me to push this for 2.6.32?
This patch requires 1/4 to be applied as it changes the kmemleak API a bit, so it won't even compile on its own. I can push them both together with your ack for the second.
I'm not sure whether API changes make sense during -rc releases, I was more thinking of pushing the patches for 2.6.33.
For the current kernel, I could send you patch with the current API, though calling kmemleak_scan_area() for _ftrace_events is a bit ugly.
Thanks.
Signature · 1 line
Catalin
Rusty Russell
Acked-byRe: [PATCH 2/4] kmemleak: Scan the _ftrace_events section in modules
Quoted 5 lines · Catalin Marinas
Quoted 4 lines · Rusty Russell
Quoted 2 lines · Catalin Marinas
This section contains pointers to allocated objects and not scanning it leads to false positives.
Quoted 8 lines
Quoted 1 line
Thanks, applied. Want me to push this for 2.6.32?
This patch requires 1/4 to be applied as it changes the kmemleak API a bit, so it won't even compile on its own. I can push them both together with your ack for the second.
I'm not sure whether API changes make sense during -rc releases, I was more thinking of pushing the patches for 2.6.33.
Definitely 2.6.33 material. And if it's tied to your other patches, best for you to hold them all.
Thanks,
Rusty.
Catalin Marinas
maintainerPATCH 3/4 kmemleak: Store object reverse references for debugging purposes
In reply to Catalin MarinasThere are some memory leak reports which come and go but are hard to identify whether they are false positives or not. This patch stores up to 4 reverse references in an object so that once a leak disappeared, using "echo dump=<ptr> > debug/kmemleak" would list where an object is referenced from.
--- mm/kmemleak.c | 15 ++++++++++++++- 1 files changed, 14 insertions(+), 1 deletions(-)
mm/kmemleak.c +14 -1
@@ -110,6 +110,7 @@#define SECS_SCAN_WAIT 600/* subsequent auto scanning delay */ #define GRAY_LIST_PASSES 25/* maximum number of gray list scans */ #define MAX_SCAN_SIZE 4096/* maximum size of a scanned block */ +#define MAX_REV_REF 4/* number of reverse references */ #define BYTES_PER_POINTER sizeof(void *)
@@ -156,6 +157,7 @@ struct kmemleak_object {unsigned long jiffies;/* creation timestamp */ pid_t pid;/* pid of the current task */ char comm[TASK_COMM_LEN ];/* executable name */ +void *rev_ref[MAX_REV_REF ];/* reverse references */ };/* flag representing the memory block allocation status */
@@ -379,6 +381,13 @@ static void dump_object_info(struct kmemleak_object *object)pr_notice (" min_count = %d\n" , object->min_count);pr_notice (" count = %d\n" , object->count);pr_notice (" flags = 0x%lx\n" , object->flags); +if (object->count) { +int i; +pr_notice (" referred from:" ); +for (i =0 ; i < object->count; i++) +printk (" 0x%p" , object->rev_ref[i]); +printk ("\n" ); + }pr_notice (" backtrace:\n" );print_stack_trace (&trace,4 ); }
@@ -1011,8 +1020,12 @@ static void scan_block(void *_start, void *_end,*/ spin_lock_irqsave_nested (&object->lock, flags,SINGLE_DEPTH_NESTING ); +if (object->count <MAX_REV_REF ) + object->rev_ref[object->count] = ptr; +if (!color_white (object)) {/* non-orphan, ignored or new */ + object->count++;spin_unlock_irqrestore (&object->lock, flags);put_object (object);continue ;
@@ -1416,7 +1429,7 @@ static int dump_str_object_info(const char *str)unsigned long addr; addr=simple_strtoul (str,NULL ,0 ); - object =find_and_get_object (addr,0 ); + object =find_and_get_object (addr,1 );if (!object) {pr_info ("Unknown object at 0x%08lx\n" , addr);return -EINVAL ;
Catalin Marinas
maintainerPATCH 4/4 kmemleak: Allow two scanning passes before reported an object as leak
In reply to Catalin MarinasThe majority of the transient false positives in kmemleak are caused by pointers being moved to a data structure after kmemleak scanned it during a scanning episode. This may cause a false positive report on the object pointed to by such pointer. This patch simplifies the scanning algorithm for objects allocated during a scanning episode and instead requires an object to be found as a leak twice consecutively before being reported.
--- mm/kmemleak.c | 77 ++++++++++++++------------------------------------------- 1 files changed, 19 insertions(+), 58 deletions(-)
mm/kmemleak.c +19 -58
@@ -108,7 +108,6 @@#define MSECS_MIN_AGE 5000/* minimum object age for reporting */ #define SECS_FIRST_SCAN 60/* delay before the first scan */ #define SECS_SCAN_WAIT 600/* subsequent auto scanning delay */ -#define GRAY_LIST_PASSES 25/* maximum number of gray list scans */ #define MAX_SCAN_SIZE 4096/* maximum size of a scanned block */ #define MAX_REV_REF 4/* number of reverse references */
@@ -166,8 +165,8 @@ struct kmemleak_object {#define OBJECT_REPORTED (1 << 1)/* flag set to not scan the object */ #define OBJECT_NO_SCAN (1 << 2) -/* flag set on newly allocated objects */ -#define OBJECT_NEW (1 << 3) +/* flag set on unreferenced objects during first a scanning pass */ +#define OBJECT_FIRST_PASS (1 << 3)/* number of bytes to print per line; must be 16 or 32 */ #define HEX_ROW_SIZE 16
@@ -205,9 +204,6 @@ static unsigned long min_addr = ULONG_MAX;static unsigned long max_addr;static struct task_struct *scan_thread; -/* used to avoid reporting of recently allocated objects */ -static unsigned long jiffies_min_age; -static unsigned long jiffies_last_scan;/* delay between automatic memory scannings */ static signed long jiffies_scan_wait;/* enables or disables the task stacks scanning */
@@ -323,21 +319,13 @@ static bool color_gray(const struct kmemleak_object *object) object->count >= object->min_count; } -static bool color_black (const struct kmemleak_object *object) -{ -return object->min_count ==KMEMLEAK_BLACK ; -} -/* -* Objects are considered unreferenced only if their color is white, they have -* not be deleted and have a minimum age to avoid false positives caused by -* pointers temporarily stored in CPU registers. +* Objects are considered unreferenced only if their color is white and they +* have not been deleted. */ static bool unreferenced_object (struct kmemleak_object *object) { -return (object->flags &OBJECT_ALLOCATED ) &&color_white (object) && -time_before_eq (object->jiffies + jiffies_min_age, - jiffies_last_scan); +return (object->flags &OBJECT_ALLOCATED ) &&color_white (object); }/*
@@ -529,11 +517,11 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,INIT_HLIST_HEAD (&object->area_list);spin_lock_init (&object->lock);atomic_set (&object->use_count,1 ); - object->flags =OBJECT_ALLOCATED |OBJECT_NEW ; + object->flags =OBJECT_ALLOCATED ; object->pointer = ptr; object->size = size; object->min_count = min_count; - object->count =-1 ;/* no color initially */ + object->count =KMEMLEAK_BLACK ;/* no white color initially */ object->jiffies = jiffies;/* task information */
@@ -1100,9 +1088,6 @@ static void kmemleak_scan(void)struct kmemleak_object *object, *tmp;int i;int new_leaks =0 ; -int gray_list_pass =0 ; - - jiffies_last_scan = jiffies;/* prepare the kmemleak_object's */ rcu_read_lock ();
@@ -1121,7 +1106,6 @@ static void kmemleak_scan(void)#endif /* reset the reference count (whiten the object) */ object->count =0 ; - object->flags &= ~OBJECT_NEW ;if (color_gray (object) &&get_object (object))list_add_tail (&object->gray_list, &gray_list);
@@ -1185,7 +1169,6 @@ static void kmemleak_scan(void)* kmemleak objects cannot be freed from outside the loop because their * use_count was increased. */ -repeat: object =list_entry (gray_list.next,typeof (*object), gray_list);while (&object->gray_list != &gray_list) {cond_resched ();
@@ -1203,38 +1186,12 @@ repeat: object = tmp; } - -if (scan_should_stop () || ++gray_list_pass >=GRAY_LIST_PASSES ) - goto scan_end; - -/* -* Check for new objects allocated during this scanning and add them -* to the gray list. -*/ -rcu_read_lock (); -list_for_each_entry_rcu (object, &object_list, object_list) { -spin_lock_irqsave (&object->lock, flags); -if ((object->flags &OBJECT_NEW ) && !color_black (object) && -get_object (object)) { - object->flags &= ~OBJECT_NEW ; -list_add_tail (&object->gray_list, &gray_list); - } -spin_unlock_irqrestore (&object->lock, flags); - } -rcu_read_unlock (); - -if (!list_empty (&gray_list)) - goto repeat; - -scan_end:WARN_ON (!list_empty (&gray_list));/* -* If scanning was stopped or new objects were being allocated at a -* higher rate than gray list scanning, do not report any new -* unreferenced objects. +* If scanning was stopped do not report any new unreferenced objects. */ -if (scan_should_stop () || gray_list_pass >=GRAY_LIST_PASSES ) +if (scan_should_stop ())return ;/*
@@ -1243,11 +1200,16 @@ scan_end:rcu_read_lock ();list_for_each_entry_rcu (object, &object_list, object_list) {spin_lock_irqsave (&object->lock, flags); -if (unreferenced_object (object) && - !(object->flags &OBJECT_REPORTED )) { - object->flags |=OBJECT_REPORTED ; - new_leaks++; - } +if (unreferenced_object (object)) { +if (!(object->flags &OBJECT_FIRST_PASS )) + object->flags |=OBJECT_FIRST_PASS ; +else if (!(object->flags &OBJECT_REPORTED )) { +/* previously found as leak, report it */ + new_leaks++; + object->flags |=OBJECT_REPORTED ; + } + }else + object->flags &= ~OBJECT_FIRST_PASS ;spin_unlock_irqrestore (&object->lock, flags); }rcu_read_unlock ();
@@ -1609,7 +1571,6 @@ void __init kmemleak_init(void)int i;unsigned long flags; - jiffies_min_age =msecs_to_jiffies (MSECS_MIN_AGE ); jiffies_scan_wait =msecs_to_jiffies (SECS_SCAN_WAIT *1000 ); object_cache =KMEM_CACHE (kmemleak_object,SLAB_NOLEAKTRACE );