mm: kmemleak: wait for scan completion before disabling free
Subsystems (2)
PATCH 1/1 mm: kmemleak: 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 Ckmemleak_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->pointerupdate_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.
--- mm/kmemleak.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
mm/kmemleak.c +6 -3
@@ -1931,12 +1931,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 ();
Signature · 2 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, hosted by The Linux Foundation
Quoted 21 lines · Vinayak Menon
A crash is observed when
kmemleak_scanaccesses theobject->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_allocfails
kmemleak_disable
kmemleak_do_cleanup
kmemleak_free_enabled= 0
kfreekmemleak_freebails out
(kmemleak_free_enabledis 0)
slub freesobject->pointerupdate_checksum
crash -object->pointer
freed (DEBUG_PAGEALLOC)
Quoted 5 lines
kmemleak_do_cleanupwaits for the scan thread to complete, but not for direct call tokmemleak_scanviakmemleak_write. So add a wait forkmemleak_scancompletion before disablingkmemleak_free.Signed-off-by Vinayak Menon <[email protected]>
It looks fine to me. Maybe Andrew can pick it up.
Thanks.
Andrew Morton
maintainerQuoted 26 lines · On Mon, 26 Mar 2018 16:44:21 +0100 Catalin Marinas <[email protected]>…
Quoted 25 lines · Vinayak Menon
A crash is observed when
kmemleak_scanaccesses theobject->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_allocfails
kmemleak_disable
kmemleak_do_cleanup
kmemleak_free_enabled= 0
kfreekmemleak_freebails out
(kmemleak_free_enabledis 0)
slub freesobject->pointerupdate_checksum
crash -object->pointer
freed (DEBUG_PAGEALLOC)
kmemleak_do_cleanupwaits for the scan thread to complete, but not for direct call tokmemleak_scanviakmemleak_write. So add a wait forkmemleak_scancompletion before disablingkmemleak_free.
Quoted 5 lines
Quoted 1 line
Signed-off-by Vinayak Menon <[email protected]>It looks fine to me. Maybe Andrew can pick it up.
Reviewed-by Catalin Marinas <[email protected]>
Well, the comment says:
/*
* Stop the automatic memory scanning thread. This function must be called
* with the scan_mutex held.
*/
static void stop_scan_thread(void)
So shouldn't we do it this way?
mm/kmemleak.c renamed from mm/kmemleak.c~mm-kmemleak-wait-for-scan-completion-before-disabling-free-fix +1 -1
@@ -1919,9 +1919,9 @@ static void __kmemleak_do_cleanup(void)*/ static void kmemleak_do_cleanup (struct work_struct *work) { +mutex_lock (&scan_mutex);stop_scan_thread (); -mutex_lock (&scan_mutex);/* * 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
_
Andrew Morton
maintainerQuoted 7 lines · On Mon, 26 Mar 2018 12:26:11 -0700 Andrew Morton <[email protected]>…
Quoted 4 lines
It looks fine to me. Maybe Andrew can pick it up.
Reviewed-by Catalin Marinas <[email protected]>Well, the comment says:
Quoted 8 lines
/*
* Stop the automatic memory scanning thread. This function must be called
* with thescan_mutexheld.
*/
static voidstop_scan_thread(void)So shouldn't we do it this way?
If "yes" then could someone please runtime test this?
Quoted 15 lines
mm/kmemleak.c renamed from mm/kmemleak.c~mm-kmemleak-wait-for-scan-completion-before-disabling-free-fix +1
-1@@ -1919,9 +1919,9 @@ static void __kmemleak_do_cleanup(void)*/ static void kmemleak_do_cleanup (struct work_struct *work) { +mutex_lock (&scan_mutex);stop_scan_thread (); -mutex_lock (&scan_mutex);/* * 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 _
Quoted 34 lines · Andrew Morton
Quoted 22 lines · On Mon, 26 Mar 2018 16:44:21 +0100 Catalin Marinas <[email protected]>…
Quoted 21 lines · Vinayak Menon
A crash is observed when
kmemleak_scanaccesses theobject->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_allocfails
kmemleak_disable
kmemleak_do_cleanup
kmemleak_free_enabled= 0
kfreekmemleak_freebails out
(kmemleak_free_enabledis 0)
slub freesobject->pointerupdate_checksum
crash -object->pointer
freed (DEBUG_PAGEALLOC)Quoted 8 lines
Quoted 5 lines
kmemleak_do_cleanupwaits for the scan thread to complete, but not for direct call tokmemleak_scanviakmemleak_write. So add a wait forkmemleak_scancompletion before disablingkmemleak_free.Signed-off-by Vinayak Menon <[email protected]>It looks fine to me. Maybe Andrew can pick it up.
Reviewed-by Catalin Marinas <[email protected]>Well, the comment says:
Quoted 8 lines
/*
* Stop the automatic memory scanning thread. This function must be called
* with thescan_mutexheld.
*/
static voidstop_scan_thread(void)So shouldn't we do it this way?
Earlier it was done the way you mentioned. But that was changed to fix a deadlock by
commit 5f369f374ba4889fe3c17883402db5ee8d254216
Author: Catalin Marinas <[email protected]>
Date:A A Wed Jun 24 16:58:31 2015 -0700
A A A mm: kmemleak: do not acquire scan_mutex in kmemleak_do_cleanup()
Not able to see a reason why stop_scan_thread must be called with scan_mutex held. The comment needs a fix ?
Quoted 16 lines
mm/kmemleak.c renamed from mm/kmemleak.c~mm-kmemleak-wait-for-scan-completion-before-disabling-free-fix +1
-1@@ -1919,9 +1919,9 @@ static void __kmemleak_do_cleanup(void)*/ static void kmemleak_do_cleanup (struct work_struct *work) { +mutex_lock (&scan_mutex);stop_scan_thread (); -mutex_lock (&scan_mutex);/* * 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 _
Catalin Marinas
maintainerQuoted 46 lines · Vinayak Menon
Quoted 33 lines · Andrew Morton
Quoted 22 lines · On Mon, 26 Mar 2018 16:44:21 +0100 Catalin Marinas <[email protected]>…
Quoted 21 lines · Vinayak Menon
A crash is observed when
kmemleak_scanaccesses theobject->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_allocfails
kmemleak_disable
kmemleak_do_cleanup
kmemleak_free_enabled= 0
kfreekmemleak_freebails out
(kmemleak_free_enabledis 0)
slub freesobject->pointerupdate_checksum
crash -object->pointer
freed (DEBUG_PAGEALLOC)Quoted 8 lines
Quoted 5 lines
kmemleak_do_cleanupwaits for the scan thread to complete, but not for direct call tokmemleak_scanviakmemleak_write. So add a wait forkmemleak_scancompletion before disablingkmemleak_free.Signed-off-by Vinayak Menon <[email protected]>It looks fine to me. Maybe Andrew can pick it up.
Reviewed-by Catalin Marinas <[email protected]>Well, the comment says:
Quoted 8 lines
/*
* Stop the automatic memory scanning thread. This function must be called
* with thescan_mutexheld.
*/
static voidstop_scan_thread(void)So shouldn't we do it this way?
Earlier it was done the way you mentioned. But that was changed to fix a deadlock by
Quoted 8 lines
commit
5f369f374ba4889fe3c17883402db5ee8d254216Author: Catalin Marinas <[email protected]> Date: Wed Jun 24 16:58:31 2015 -0700mm: kmemleak: do not acquire
scan_mutexinkmemleak_do_cleanup()Not able to see a reason why
stop_scan_threadmust be called withscan_mutexheld. The comment needs a fix ?
Indeed, the comment needs fixing as waiting on the mutex here may lead deadlock. Would you mind sending an updated patch? Feel free to keep my reviewed-by tag.
Thanks.
Signature · 1 line
Catalin
Quoted 50 lines · Catalin Marinas
Quoted 49 lines · Vinayak Menon
Quoted 33 lines · Andrew Morton
Quoted 22 lines · On Mon, 26 Mar 2018 16:44:21 +0100 Catalin Marinas <[email protected]>…
Quoted 21 lines · Vinayak Menon
A crash is observed when
kmemleak_scanaccesses theobject->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_allocfails
kmemleak_disable
kmemleak_do_cleanup
kmemleak_free_enabled= 0
kfreekmemleak_freebails out
(kmemleak_free_enabledis 0)
slub freesobject->pointerupdate_checksum
crash -object->pointer
freed (DEBUG_PAGEALLOC)Quoted 8 lines
Quoted 5 lines
kmemleak_do_cleanupwaits for the scan thread to complete, but not for direct call tokmemleak_scanviakmemleak_write. So add a wait forkmemleak_scancompletion before disablingkmemleak_free.Signed-off-by Vinayak Menon <[email protected]>It looks fine to me. Maybe Andrew can pick it up.
Reviewed-by Catalin Marinas <[email protected]>Well, the comment says:
Quoted 8 lines
/*
* Stop the automatic memory scanning thread. This function must be called
* with thescan_mutexheld.
*/
static voidstop_scan_thread(void)So shouldn't we do it this way?
Earlier it was done the way you mentioned. But that was changed to fix a deadlock by
commit
5f369f374ba4889fe3c17883402db5ee8d254216
Author: Catalin Marinas <[email protected]>
Date:A A Wed Jun 24 16:58:31 2015 -0700
Quoted 7 lines
Quoted 4 lines
A A A mm: kmemleak: do not acquire
scan_mutexinkmemleak_do_cleanup()Not able to see a reason why
stop_scan_threadmust be called withscan_mutexheld. The comment needs a fix ?Indeed, the comment needs fixing as waiting on the mutex here may lead deadlock. Would you mind sending an updated patch? Feel free to keep my reviewed-by tag.
Sure. done.
Quoted 3 lines
Thanks.