Commit 019db118 authored by Catalin Marinas's avatar Catalin Marinas Committed by Zefan Li

mm: kmemleak: allow safe memory scanning during kmemleak disabling

commit c5f3b1a5 upstream.

The kmemleak scanning thread can run for minutes.  Callbacks like
kmemleak_free() are allowed during this time, the race being taken care
of by the object->lock spinlock.  Such lock also prevents a memory block
from being freed or unmapped while it is being scanned by blocking the
kmemleak_free() -> ...  -> __delete_object() function until the lock is
released in scan_object().

When a kmemleak error occurs (e.g.  it fails to allocate its metadata),
kmemleak_enabled is set and __delete_object() is no longer called on
freed objects.  If kmemleak_scan is running at the same time,
kmemleak_free() no longer waits for the object scanning to complete,
allowing the corresponding memory block to be freed or unmapped (in the
case of vfree()).  This leads to kmemleak_scan potentially triggering a
page fault.

This patch separates the kmemleak_free() enabling/disabling from the
overall kmemleak_enabled nob so that we can defer the disabling of the
object freeing tracking until the scanning thread completed.  The
kmemleak_free_part() is deliberately ignored by this patch since this is
only called during boot before the scanning thread started.
Signed-off-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
Reported-by: default avatarVignesh Radhakrishnan <vigneshr@codeaurora.org>
Tested-by: default avatarVignesh Radhakrishnan <vigneshr@codeaurora.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
[lizf: Backported to 3.4: adjust context]
Signed-off-by: default avatarZefan Li <lizefan@huawei.com>
parent 176a2eac
...@@ -193,6 +193,8 @@ static struct kmem_cache *scan_area_cache; ...@@ -193,6 +193,8 @@ static struct kmem_cache *scan_area_cache;
/* set if tracing memory operations is enabled */ /* set if tracing memory operations is enabled */
static atomic_t kmemleak_enabled = ATOMIC_INIT(0); static atomic_t kmemleak_enabled = ATOMIC_INIT(0);
/* same as above but only for the kmemleak_free() callback */
static int kmemleak_free_enabled;
/* set in the late_initcall if there were no errors */ /* set in the late_initcall if there were no errors */
static atomic_t kmemleak_initialized = ATOMIC_INIT(0); static atomic_t kmemleak_initialized = ATOMIC_INIT(0);
/* enables or disables early logging of the memory operations */ /* enables or disables early logging of the memory operations */
...@@ -936,7 +938,7 @@ void __ref kmemleak_free(const void *ptr) ...@@ -936,7 +938,7 @@ void __ref kmemleak_free(const void *ptr)
{ {
pr_debug("%s(0x%p)\n", __func__, ptr); pr_debug("%s(0x%p)\n", __func__, ptr);
if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
delete_object_full((unsigned long)ptr); delete_object_full((unsigned long)ptr);
else if (atomic_read(&kmemleak_early_log)) else if (atomic_read(&kmemleak_early_log))
log_early(KMEMLEAK_FREE, ptr, 0, 0); log_early(KMEMLEAK_FREE, ptr, 0, 0);
...@@ -976,7 +978,7 @@ void __ref kmemleak_free_percpu(const void __percpu *ptr) ...@@ -976,7 +978,7 @@ void __ref kmemleak_free_percpu(const void __percpu *ptr)
pr_debug("%s(0x%p)\n", __func__, ptr); pr_debug("%s(0x%p)\n", __func__, ptr);
if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr)) if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
for_each_possible_cpu(cpu) for_each_possible_cpu(cpu)
delete_object_full((unsigned long)per_cpu_ptr(ptr, delete_object_full((unsigned long)per_cpu_ptr(ptr,
cpu)); cpu));
...@@ -1690,6 +1692,13 @@ static void kmemleak_do_cleanup(struct work_struct *work) ...@@ -1690,6 +1692,13 @@ static void kmemleak_do_cleanup(struct work_struct *work)
mutex_lock(&scan_mutex); mutex_lock(&scan_mutex);
stop_scan_thread(); stop_scan_thread();
/*
* Once the scan thread has stopped, it is safe to no longer track
* object freeing. Ordering of the scan thread stopping and the memory
* accesses below is guaranteed by the kthread_stop() function.
*/
kmemleak_free_enabled = 0;
if (cleanup) { if (cleanup) {
rcu_read_lock(); rcu_read_lock();
list_for_each_entry_rcu(object, &object_list, object_list) list_for_each_entry_rcu(object, &object_list, object_list)
...@@ -1717,6 +1726,8 @@ static void kmemleak_disable(void) ...@@ -1717,6 +1726,8 @@ static void kmemleak_disable(void)
/* check whether it is too early for a kernel thread */ /* check whether it is too early for a kernel thread */
if (atomic_read(&kmemleak_initialized)) if (atomic_read(&kmemleak_initialized))
schedule_work(&cleanup_work); schedule_work(&cleanup_work);
else
kmemleak_free_enabled = 0;
pr_info("Kernel memory leak detector disabled\n"); pr_info("Kernel memory leak detector disabled\n");
} }
...@@ -1782,8 +1793,10 @@ void __init kmemleak_init(void) ...@@ -1782,8 +1793,10 @@ void __init kmemleak_init(void)
if (atomic_read(&kmemleak_error)) { if (atomic_read(&kmemleak_error)) {
local_irq_restore(flags); local_irq_restore(flags);
return; return;
} else } else {
atomic_set(&kmemleak_enabled, 1); atomic_set(&kmemleak_enabled, 1);
kmemleak_free_enabled = 1;
}
local_irq_restore(flags); local_irq_restore(flags);
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment