Skip to contentSkip to conversation

mm: kmemleak: Ensure that the task stack is not freed during scanning

PATCH Applied Catalin Marinas 5 participants 8 messages Oct 12, 2016 – Oct 20, 2016

Subsystems (2)
lore
Share & tools
lore

Catalin Marinas

author maintainer
#0 lore raw

PATCH 1/1 mm: kmemleak: Ensure that the task stack is not freed during scanning

Commit 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK") may cause the task->stack to be freed during kmemleak_scan() execution, leading to either a NULL pointer fault (if task->stack is NULL) or kmemleak accessing already freed memory. This patch uses the new try_get_task_stack() API to ensure that the task stack is not freed during kmemleak stack scanning.

Fixes 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK")
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc CAI Qian <[email protected]>

---

This was reported in a subsequent comment here:

https://bugzilla.kernel.org/show_bug.cgi?id=173901

However, the original bugzilla entry doesn't look related to task stack freeing as it was first reported on 4.8-rc8. Andy, sorry for cc'ing you to bugzilla, please feel free to remove your email from the bug above (I can't seem to be able to do it).

 mm/kmemleak.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/kmemleak.c +5 -2
index a5e453cf05c4..e5355a5b423f 100644
@@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
 
 		read_lock(&tasklist_lock);
 		do_each_thread(g, p) {
-			scan_block(task_stack_page(p), task_stack_page(p) +
-				   THREAD_SIZE, NULL);
+			void *stack = try_get_task_stack(p);
+			if (stack) {
+				scan_block(stack, stack + THREAD_SIZE, NULL);
+				put_task_stack(p);
+			}
 		} while_each_thread(g, p);
 		read_unlock(&tasklist_lock);
 	}

--
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>

#1 lore raw

mm/kmemleak.c static void kmemleak_scan(void) · line 1455

Quoted 4 lines
mm/kmemleak.c +0 -0
@@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
 
 		read_lock(&tasklist_lock);
 		do_each_thread(g, p) {

Take a look at this commit please.
1da4db0cd5 ("oom_kill: change oom_kill.c to use for_each_thread()")

mm/kmemleak.c static void kmemleak_scan(void) · line 1463

Quoted 11 lines
mm/kmemleak.c +5 -2
-			scan_block(task_stack_page(p), task_stack_page(p) +
-				   THREAD_SIZE, NULL);
+			void *stack = try_get_task_stack(p);
+			if (stack) {
+				scan_block(stack, stack + THREAD_SIZE, NULL);
+				put_task_stack(p);
+			}
 		} while_each_thread(g, p);
 		read_unlock(&tasklist_lock);
 	}

--
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>

author maintainer
#2 lore raw
Quoted 7 lines · Hillf Danton

mm/kmemleak.c static void kmemleak_scan(void) · line 1455

Quoted 4 lines
mm/kmemleak.c +0 -0
@@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
 
 		read_lock(&tasklist_lock);
 		do_each_thread(g, p) {

Take a look at this commit please.
1da4db0cd5 ("oom_kill: change oom_kill.c to use for_each_thread()")

Thanks. Isn't holding tasklist_lock here enough to avoid such races?

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>
Acked-by on v1 1/1
#3 lore raw
In reply to Catalin Marinas
Quoted 6 lines · Catalin Marinas

Commit 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK") may cause the task->stack to be freed during kmemleak_scan() execution, leading to either a NULL pointer fault (if task->stack is NULL) or kmemleak accessing already freed memory. This patch uses the new try_get_task_stack() API to ensure that the task stack is not freed during kmemleak stack scanning.

Looks good to me

Quoted 6 lines
Fixes 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK")
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc CAI Qian <[email protected]>
Quoted 38 lines

---

This was reported in a subsequent comment here:

https://bugzilla.kernel.org/show_bug.cgi?id=173901

However, the original bugzilla entry doesn't look related to task stack freeing as it was first reported on 4.8-rc8. Andy, sorry for cc'ing you to bugzilla, please feel free to remove your email from the bug above (I can't seem to be able to do it).

 mm/kmemleak.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/kmemleak.c +5 -3
index a5e453cf05c4..e5355a5b423f 100644
@@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
 
 		read_lock(&tasklist_lock);
 		do_each_thread(g, p) {
-			scan_block(task_stack_page(p), task_stack_page(p) +
-				   THREAD_SIZE, NULL);
+			void *stack = try_get_task_stack(p);
+			if (stack) {
+				scan_block(stack, stack + THREAD_SIZE, NULL);
+				put_task_stack(p);
+			}
 		} while_each_thread(g, p);
 		read_unlock(&tasklist_lock);
 	}
 
--

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>

Signature · 8 lines
Michal Hocko
SUSE Labs

--
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>
Tested-by on v1 1/1
#4 lore raw
In reply to Catalin Marinas

----- Original Message -----

Quoted 14 lines

From: "Catalin Marinas" <[email protected]>
To: [email protected]
Cc: [email protected], "Andrew Morton" <[email protected]>, "Andy Lutomirski" <[email protected]>, "CAI Qian" <[email protected]>
Sent: Wednesday, October 12, 2016 5:57:03 AM
Subject: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

Commit 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK") may cause the task->stack to be freed during kmemleak_scan() execution, leading to either a NULL pointer fault (if task->stack is NULL) or kmemleak accessing already freed memory. This patch uses the new try_get_task_stack() API to ensure that the task stack is not freed during kmemleak stack scanning.

Quoted 7 lines
Fixes 68f24b08ee89 ("sched/core: Free the stack early if

CONFIG_THREAD_INFO_IN_TASK")

Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc CAI Qian <[email protected]>
Quoted 34 lines

---

This was reported in a subsequent comment here:

https://bugzilla.kernel.org/show_bug.cgi?id=173901

However, the original bugzilla entry doesn't look related to task stack freeing as it was first reported on 4.8-rc8. Andy, sorry for cc'ing you to bugzilla, please feel free to remove your email from the bug above (I can't seem to be able to do it).

 mm/kmemleak.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/kmemleak.c +5 -3
index a5e453cf05c4..e5355a5b423f 100644
@@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
 
 		read_lock(&tasklist_lock);
 		do_each_thread(g, p) {
-			scan_block(task_stack_page(p), task_stack_page(p) +
-				   THREAD_SIZE, NULL);
+			void *stack = try_get_task_stack(p);
+			if (stack) {
+				scan_block(stack, stack + THREAD_SIZE, NULL);
+				put_task_stack(p);
+			}
 		} while_each_thread(g, p);
 		read_unlock(&tasklist_lock);
 	}
 
--
Quoted 7 lines

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>

--
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>

author maintainer
#5 lore raw
Quoted 23 lines · CAI Qian

----- Original Message -----

Quoted 14 lines

From: "Catalin Marinas" <[email protected]>
To: [email protected]
Cc: [email protected], "Andrew Morton" <[email protected]>, "Andy Lutomirski" <[email protected]>, "CAI Qian" <[email protected]>
Sent: Wednesday, October 12, 2016 5:57:03 AM
Subject: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

Commit 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK") may cause the task->stack to be freed during kmemleak_scan() execution, leading to either a NULL pointer fault (if task->stack is NULL) or kmemleak accessing already freed memory. This patch uses the new try_get_task_stack() API to ensure that the task stack is not freed during kmemleak stack scanning.

Quoted 7 lines
Fixes 68f24b08ee89 ("sched/core: Free the stack early if

CONFIG_THREAD_INFO_IN_TASK")

Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc CAI Qian <[email protected]>
Quoted 1 line

Thanks.

BTW, I noticed a few false positives reported by kmemleak with the CONFIG_VMAP_STACK enabled caused by the fact that kmemleak requires two references (instead of one) to a vmalloc'ed object because of the vm_struct already containing the address. The cached_stack[] array only stores the vm_struct rather than the stack address, hence the kmemleak report. I'll work on a fix/annotation.

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>
maintainer
#6 lore raw
In reply to Catalin Marinas
Quoted 7 lines · On Wed, 12 Oct 2016 10:57:03 +0100 Catalin Marinas <[email protected]>…

Commit 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK") may cause the task->stack to be freed during kmemleak_scan() execution, leading to either a NULL pointer fault (if task->stack is NULL) or kmemleak accessing already freed memory. This patch uses the new try_get_task_stack() API to ensure that the task stack is not freed during kmemleak stack scanning.

Quoted 6 lines
Fixes 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK")
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc CAI Qian <[email protected]>

I'll add

Cc <[email protected]> [4.8.x]

--
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>

author maintainer
#7 lore raw

Hi Andrew,

Quoted 15 lines · Andrew Morton
Quoted 7 lines · On Wed, 12 Oct 2016 10:57:03 +0100 Catalin Marinas <[email protected]>…

Commit 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK") may cause the task->stack to be freed during kmemleak_scan() execution, leading to either a NULL pointer fault (if task->stack is NULL) or kmemleak accessing already freed memory. This patch uses the new try_get_task_stack() API to ensure that the task stack is not freed during kmemleak stack scanning.

Quoted 6 lines
Fixes 68f24b08ee89 ("sched/core: Free the stack early if CONFIG_THREAD_INFO_IN_TASK")
Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc CAI Qian <[email protected]>
Quoted 3 lines

I'll add

Cc <[email protected]> [4.8.x]

This should be 4.9.x. The commit that introduces try_get_task_stack() was merged in 4.9-rc1: c6c314a613cd ("sched/core: Add try_get_task_stack() and put_task_stack()").

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>