mm: kmemleak: Improve vmalloc() false positives for thread stack allocation
Subsystems (5)
Catalin Marinas
maintainerv2 cover mm: kmemleak: Improve vmalloc() false positives for thread stack allocation
Hi,
This is a follow up from [1] (mm: kmemleak: Treat vm_struct as alternative reference to vmalloc'ed objects).
The first two patches are just clean-up and refactoring. The third introduces the kmemleak_vmalloc() API which allows a vmalloc() caller to keep either the returned pointer or a pointer to vm_struct as a reference (see the patch description for the implementation details). The false positives were noticed with alloc_thread_stack_node(), free_thread_stack() and CONFIG_VMAP_STACK where a per-CPU array is used to cache the freed thread stacks as vm_struct pointers.
Changes since v1:
- Split the patch into three for easier review
- Only call update_refs() if !color_gray() on the found object, it avoids an unnecessary function call
[1] http://lkml.kernel.org/r/[email protected]
Catalin Marinas (3):
mm: kmemleak: Slightly reduce the size of some structures on 64-bit
architectures
mm: kmemleak: Factor object reference updating out of scan_block()
mm: kmemleak: Treat vm_struct as alternative reference to vmalloc'ed
objects
Documentation/dev-tools/kmemleak.rst | 1 + include/linux/kmemleak.h | 7 ++ mm/kmemleak.c | 136 +++++++++++++++++++++++++++++------ mm/vmalloc.c | 7 +- 4 files changed, 123 insertions(+), 28 deletions(-)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Catalin Marinas
maintainerPATCH v2 1/3 mm: kmemleak: Slightly reduce the size of some structures on 64-bit architectures
This patch changes the kmemleak_object.flags type to unsigned int and moves the early_log.min_count (int) near early_log.op_type (int) to slightly reduce the size of these structures on 64-bit architectures.
--- mm/kmemleak.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/kmemleak.c +3 -3
@@ -150,7 +150,7 @@ struct kmemleak_scan_area {*/ struct kmemleak_object {spinlock_t lock; -unsigned long flags;/* object status flags */ +unsigned int flags;/* object status flags */ struct list_head object_list;struct list_head gray_list;struct rb_node rb_node;
@@ -262,9 +262,9 @@ enum {*/ struct early_log {int op_type;/* kmemleak operation type */ +int min_count;/* minimum reference count */ const void *ptr;/* allocated/freed memory block */ size_t size;/* memory block size */ -int min_count;/* minimum reference count */ unsigned long trace[MAX_TRACE ];/* stack trace */ unsigned int trace_len;/* stack trace length */ };
@@ -393,7 +393,7 @@ static void dump_object_info(struct kmemleak_object *object) object->comm, object->pid, object->jiffies);pr_notice (" min_count = %d\n" , object->min_count);pr_notice (" count = %d\n" , object->count); -pr_notice (" flags = 0x%lx\n" , object->flags); +pr_notice (" flags = 0x%x\n" , object->flags);pr_notice (" checksum = %u\n" , object->checksum);pr_notice (" backtrace:\n" );print_stack_trace (&trace,4 );
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Catalin Marinas
maintainerPATCH v2 2/3 mm: kmemleak: Factor object reference updating out of scan_block()
In reply to Catalin MarinasThe scan_block() function updates the number of references (pointers) to objects, adding them to the gray_list when object->min_count is reached. The patch factors out this functionality into a separate update_refs() function.
--- mm/kmemleak.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-)
mm/kmemleak.c +25 -18
@@ -1188,6 +1188,30 @@ static bool update_checksum(struct kmemleak_object *object) }/* +* Update an object's references. object->lock must be held by the caller. +*/ +static void update_refs (struct kmemleak_object *object) +{ +if (!color_white (object)) { +/* non-orphan, ignored or new */ +return ; + } + +/* +* Increase the object's reference count (number of pointers to the +* memory block). If this count reaches the required minimum, the +* object's color will become gray and it will be added to the +* gray_list. +*/ + object->count++; +if (color_gray (object)) { +/* put_object() called when removing from gray_list */ +WARN_ON (!get_object (object)); +list_add_tail (&object->gray_list, &gray_list); + } +} + +/* * Memory scanning is a long process and it needs to be interruptable. This * function checks whether such interrupt condition occurred. */
@@ -1259,24 +1283,7 @@ static void scan_block(void *_start, void *_end,* enclosed by scan_mutex. */ spin_lock_nested (&object->lock,SINGLE_DEPTH_NESTING ); -if (!color_white (object)) { -/* non-orphan, ignored or new */ -spin_unlock (&object->lock); -continue ; - } - -/* -* Increase the object's reference count (number of pointers -* to the memory block). If this count reaches the required -* minimum, the object's color will become gray and it will be -* added to the gray_list. -*/ - object->count++; -if (color_gray (object)) { -/* put_object() called when removing from gray_list */ -WARN_ON (!get_object (object)); -list_add_tail (&object->gray_list, &gray_list); - } +update_refs (object);spin_unlock (&object->lock); }read_unlock_irqrestore (&kmemleak_lock, flags);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2 2/3] mm: kmemleak: Factor object reference updating out of scan_block()
Quoted 62 lines · Catalin Marinas
The
scan_block()function updates the number of references (pointers) to objects, adding them to thegray_listwhenobject->min_countis reached. The patch factors out this functionality into a separateupdate_refs()function.Cc Michal Hocko <[email protected]>Cc Andy Lutomirski <[email protected]>Cc "Luis R. Rodriguez" <[email protected]>Signed-off-by Catalin Marinas <[email protected]>--- mm/kmemleak.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-)mm/kmemleak.c +24
-10@@ -1188,6 +1188,30 @@ static bool update_checksum(struct kmemleak_object *object) }/* +* Update an object's references. object->lock must be held by the caller. +*/ +static void update_refs (struct kmemleak_object *object) +{ +if (!color_white (object)) { +/* non-orphan, ignored or new */ +return ; + } + +/* +* Increase the object's reference count (number of pointers to the +* memory block). If this count reaches the required minimum, the +* object's color will become gray and it will be added to the +* gray_list. +*/ + object->count++; +if (color_gray (object)) { +/* put_object() called when removing from gray_list */ +WARN_ON (!get_object (object)); +list_add_tail (&object->gray_list, &gray_list); + } +} + +/* * Memory scanning is a long process and it needs to be interruptable. This * function checks whether such interrupt condition occurred. */ @@ -1259,24 +1283,7 @@ static void scan_block(void *_start, void *_end,* enclosed by scan_mutex. */ spin_lock_nested (&object->lock,SINGLE_DEPTH_NESTING ); -if (!color_white (object)) { -/* non-orphan, ignored or new */ -spin_unlock (&object->lock); -continue ; - } - -/* -* Increase the object's reference count (number of pointers -* to the memory block). If this count reaches the required -* minimum, the object's color will become gray and it will be
mm/kmemleak.c static void scan_block(void *_start, void *_end, · line 1287
Quoted 10 lines
mm/kmemleak.c +1
-8-* added to the gray_list. -*/ - object->count++; -if (color_gray (object)) { -/* put_object() called when removing from gray_list */ -WARN_ON (!get_object (object)); -list_add_tail (&object->gray_list, &gray_list); - } +update_refs (object);spin_unlock (&object->lock);
FWIW, I've tested this patchset and I don't see kmemleak triggering the false positives anymore.
I've also done a quick review and couldn't find anything obviously incorrect, just a question: why didn't you moved the spin_lock/unlock into update_refs() too? It would save you 2 lines in the next patch :)
Cheers,
--
Luis
mm/kmemleak.c static void scan_block(void *_start, void *_end, · line 1289
Quoted 3 lines
mm/kmemleak.c +0
-0}read_unlock_irqrestore (&kmemleak_lock, flags);
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Catalin Marinas
maintainerRe: [PATCH v2 2/3] mm: kmemleak: Factor object reference updating out of scan_block()
Quoted 68 lines · Luis Henriques
Quoted 67 lines · Catalin Marinas
The
scan_block()function updates the number of references (pointers) to objects, adding them to thegray_listwhenobject->min_countis reached. The patch factors out this functionality into a separateupdate_refs()function.Cc Michal Hocko <[email protected]>Cc Andy Lutomirski <[email protected]>Cc "Luis R. Rodriguez" <[email protected]>Signed-off-by Catalin Marinas <[email protected]>--- mm/kmemleak.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-)mm/kmemleak.c +24
-15@@ -1188,6 +1188,30 @@ static bool update_checksum(struct kmemleak_object *object) }/* +* Update an object's references. object->lock must be held by the caller. +*/ +static void update_refs (struct kmemleak_object *object) +{ +if (!color_white (object)) { +/* non-orphan, ignored or new */ +return ; + } + +/* +* Increase the object's reference count (number of pointers to the +* memory block). If this count reaches the required minimum, the +* object's color will become gray and it will be added to the +* gray_list. +*/ + object->count++; +if (color_gray (object)) { +/* put_object() called when removing from gray_list */ +WARN_ON (!get_object (object)); +list_add_tail (&object->gray_list, &gray_list); + } +} + +/* * Memory scanning is a long process and it needs to be interruptable. This * function checks whether such interrupt condition occurred. */ @@ -1259,24 +1283,7 @@ static void scan_block(void *_start, void *_end,* enclosed by scan_mutex. */ spin_lock_nested (&object->lock,SINGLE_DEPTH_NESTING ); -if (!color_white (object)) { -/* non-orphan, ignored or new */ -spin_unlock (&object->lock); -continue ; - } - -/* -* Increase the object's reference count (number of pointers -* to the memory block). If this count reaches the required -* minimum, the object's color will become gray and it will be -* added to the gray_list. -*/ - object->count++; -if (color_gray (object)) { -/* put_object() called when removing from gray_list */
Quoted 8 lines
mm/kmemleak.c static void scan_block(void *_start, void *_end, · line 1287
Quoted 5 lines
mm/kmemleak.c +1
-3-WARN_ON (!get_object (object)); -list_add_tail (&object->gray_list, &gray_list); - } +update_refs (object);spin_unlock (&object->lock);FWIW, I've tested this patchset and I don't see kmemleak triggering the false positives anymore.
Thanks for re-testing (I dropped your tested-by from the initial patch since I made a small modification).
Quoted 3 lines
I've also done a quick review and couldn't find anything obviously incorrect, just a question: why didn't you moved the spin_lock/unlock into
update_refs()too? It would save you 2 lines in the next patch :)
There is a small difference: for the first object it needs to check color_gray() and access object->excess_ref while the lock is held. It doesn't need this in the second case. I could've written it in different ways but probably with a similar number of lines; I just found this clearer.
Signature · 1 line
Catalin
Catalin Marinas
maintainerRe: [PATCH v2 2/3] mm: kmemleak: Factor object reference updating out of scan_block()
Quoted 11 lines · Catalin Marinas
Quoted 10 lines · Luis Henriques
Quoted 9 lines · Catalin Marinas
The
scan_block()function updates the number of references (pointers) to objects, adding them to thegray_listwhenobject->min_countis reached. The patch factors out this functionality into a separateupdate_refs()function.Cc Michal Hocko <[email protected]>Cc Andy Lutomirski <[email protected]>Cc "Luis R. Rodriguez" <[email protected]>Signed-off-by Catalin Marinas <[email protected]>
[...]
Quoted 5 lines
Quoted 2 lines
FWIW, I've tested this patchset and I don't see kmemleak triggering the false positives anymore.
Thanks for re-testing (I dropped your tested-by from the initial patch since I made a small modification).
Sorry, the "re-testing" comment was meant at the other Luis on cc ;) (Luis R. Rodriguez). It's been a long day...
Signature · 7 lines
Catalin -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v2 2/3] mm: kmemleak: Factor object reference updating out of scan_block()
Quoted 13 lines · Catalin Marinas
Quoted 11 lines · Catalin Marinas
Quoted 10 lines · Luis Henriques
Quoted 9 lines · Catalin Marinas
The
scan_block()function updates the number of references (pointers) to objects, adding them to thegray_listwhenobject->min_countis reached. The patch factors out this functionality into a separateupdate_refs()function.Cc Michal Hocko <[email protected]>Cc Andy Lutomirski <[email protected]>Cc "Luis R. Rodriguez" <[email protected]>Signed-off-by Catalin Marinas <[email protected]>[...]
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
FWIW, I've tested this patchset and I don't see kmemleak triggering the false positives anymore.
Thanks for re-testing (I dropped your tested-by from the initial patch since I made a small modification).
Sorry, the "re-testing" comment was meant at the other Luis on cc ;) (Luis R. Rodriguez). It's been a long day...
Heh, no worries! What are the odds of having 2 different guys named Luis testing the same patch? :-)
Cheers,
--
Luis
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Catalin Marinas
maintainerPATCH v2 3/3 mm: kmemleak: Treat vm_struct as alternative reference to vmalloc'ed objects
In reply to Catalin MarinasKmemleak requires that vmalloc'ed objects have a minimum reference count of 2: one in the corresponding vm_struct object and the other owned by the vmalloc() caller. There are cases, however, where the original vmalloc() returned pointer is lost and, instead, a pointer to vm_struct is stored (see free_thread_stack()). Kmemleak currently reports such objects as leaks.
This patch adds support for treating any surplus references to an object as additional references to a specified object. It introduces the kmemleak_vmalloc() API function which takes a vm_struct pointer and sets its surplus reference passing to the actual vmalloc() returned pointer. The __vmalloc_node_range() calling site has been modified accordingly.
--- Documentation/dev-tools/kmemleak.rst | 1 + include/linux/kmemleak.h | 7 +++ mm/kmemleak.c | 93 ++++++++++++++++++++++++++++++++++-- mm/vmalloc.c | 7 +-- 4 files changed, 98 insertions(+), 10 deletions(-)
Documentation/dev-tools/kmemleak.rst +1 -0
@@ -150,6 +150,7 @@ See the include/linux/kmemleak.h header for the functions prototype. - ``kmemleak_init`` - initialize kmemleak - ``kmemleak_alloc`` - notify of a memory block allocation - ``kmemleak_alloc_percpu`` - notify of a percpu memory block allocation +- ``kmemleak_vmalloc`` - notify of a vmalloc() memory allocation - ``kmemleak_free`` - notify of a memory block freeing - ``kmemleak_free_part`` - notify of a partial memory block freeing - ``kmemleak_free_percpu`` - notify of a percpu memory block freeing
include/linux/kmemleak.h +7 -0
@@ -22,6 +22,7 @@#define __KMEMLEAK_H#include <linux/slab.h> +#include <linux/vmalloc.h> #ifdef CONFIG_DEBUG_KMEMLEAK
@@ -30,6 +31,8 @@ extern void kmemleak_alloc(const void *ptr, size_t size, int min_count, gfp_t gfp) __ref;extern void kmemleak_alloc_percpu (const void __percpu *ptr,size_t size,gfp_t gfp)__ref ; +extern void kmemleak_vmalloc (const struct vm_struct *area,size_t size, +gfp_t gfp)__ref ;extern void kmemleak_free (const void *ptr)__ref ;extern void kmemleak_free_part (const void *ptr,size_t size)__ref ;extern void kmemleak_free_percpu (const void __percpu *ptr)__ref ;
@@ -81,6 +84,10 @@ static inline void kmemleak_alloc_percpu(const void __percpu *ptr, size_t size, gfp_t gfp) { } +static inline void kmemleak_vmalloc (const struct vm_struct *area,size_t size, +gfp_t gfp) +{ +}static inline void kmemleak_free (const void *ptr) { }
mm/kmemleak.c +89 -4
154 lines not on this page · raw message
mm/vmalloc.c +1 -6
@@ -1759,12 +1759,7 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,*/ clear_vm_uninitialized_flag (area); -/* -* A ref_count = 2 is needed because vm_struct allocated in -* __get_vm_area_node() contains a reference to the virtual address of -* the vmalloc'ed block. -*/ -kmemleak_alloc (addr, real_size,2 , gfp_mask); +kmemleak_vmalloc (area, size, gfp_mask);return addr;
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>