Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 002720e7 authored by Vinayak Menon's avatar Vinayak Menon Committed by Gerrit - the friendly Code Review server
Browse files

mm/kmemleak.c: wait for scan completion before disabling free

A crash is observed when kmemleak_scan accesses the object->pointer,
likely due to the following race.

TASK A             TASK B                     TASK C
kmemleak_write
 (with "scan" and
 NOT "scan=on")
kmemleak_scan()
                   create_object
                   kmem_cache_alloc fails
                   kmemleak_disable
                   kmemleak_do_cleanup
                   kmemleak_free_enabled = 0
                                              kfree
                                              kmemleak_free bails out
                                               (kmemleak_free_enabled is 0)
                                              slub frees object->pointer
update_checksum
crash - object->pointer
 freed (DEBUG_PAGEALLOC)

kmemleak_do_cleanup waits for the scan thread to complete, but not for
direct call to kmemleak_scan via kmemleak_write.  So add a wait for
kmemleak_scan completion before disabling kmemleak_free, and while at it
fix the comment on stop_scan_thread.

[vinmenon@codeaurora.org: fix stop_scan_thread comment]
  Link: http://lkml.kernel.org/r/1522219972-22809-1-git-send-email-vinmenon@codeaurora.org
Link: http://lkml.kernel.org/r/1522063429-18992-1-git-send-email-vinmenon@codeaurora.org


Reviewed-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarStephen Rothwell <sfr@canb.auug.org.au>
Git-Commit: 5b909d2e92fc6b741b283cb6f34fa6ecd01fba4c
Git-Repo: git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git


Change-Id: Id5ae7f105c70739cc83dd3e4cb3a676410f93632
Signed-off-by: default avatarVinayak Menon <vinmenon@codeaurora.org>
parent f0a4ec7d
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -1670,8 +1670,7 @@ static void start_scan_thread(void)
}

/*
 * Stop the automatic memory scanning thread. This function must be called
 * with the scan_mutex held.
 * Stop the automatic memory scanning thread.
 */
static void stop_scan_thread(void)
{
@@ -1934,12 +1933,15 @@ static void kmemleak_do_cleanup(struct work_struct *work)
{
	stop_scan_thread();

	mutex_lock(&scan_mutex);
	/*
	 * 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.
	 * Once it is made sure that kmemleak_scan 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;
	mutex_unlock(&scan_mutex);

	if (!kmemleak_found_leaks)
		__kmemleak_do_cleanup();