From ec6034080b69d04ec20a8f4d960de005b301add7 Mon Sep 17 00:00:00 2001 From: Li Zefan Date: Thu, 3 Apr 2014 14:46:26 -0700 Subject: [PATCH] kmemleak: free internal objects only if there're no leaks to be reported Currently if you stop kmemleak thread before disabling kmemleak, kmemleak objects will be freed and so you won't be able to check previously reported leaks. With this patch, kmemleak objects won't be freed if there're leaks that can be reported. Change-Id: I31c837fa63f99f65a553471de46729d8d8e08ed5 Signed-off-by: Li Zefan Acked-by: Catalin Marinas Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Git-commit: dc9b3f424903f7d6992778b69b1e35d864914ae5 Git-repo: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/ Signed-off-by: Vignesh Radhakrishnan --- mm/kmemleak.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 7bca7d81344f..f985965a3c4a 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -228,7 +228,8 @@ static int kmemleak_stack_scan = 1; static DEFINE_MUTEX(scan_mutex); /* setting kmemleak=on, will set this var, skipping the disable */ static int kmemleak_skip_disable; - +/* If there are leaks that can be reported */ +static bool kmemleak_found_leaks; /* * Early object allocation/freeing logging. Kmemleak is initialized after the @@ -1392,9 +1393,12 @@ static void kmemleak_scan(void) } rcu_read_unlock(); - if (new_leaks) + if (new_leaks) { + kmemleak_found_leaks = true; + pr_info("%d new suspected memory leaks (see " "/sys/kernel/debug/kmemleak)\n", new_leaks); + } } @@ -1602,6 +1606,8 @@ static void kmemleak_clear(void) spin_unlock_irqrestore(&object->lock, flags); } rcu_read_unlock(); + + kmemleak_found_leaks = false; } /* @@ -1695,12 +1701,11 @@ static const struct file_operations kmemleak_fops = { static void kmemleak_do_cleanup(struct work_struct *work) { struct kmemleak_object *object; - bool cleanup = scan_thread == NULL; mutex_lock(&scan_mutex); stop_scan_thread(); - if (cleanup) { + if (!kmemleak_found_leaks) { rcu_read_lock(); list_for_each_entry_rcu(object, &object_list, object_list) delete_object_full(object->pointer);