Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-20 Thread Catalin Marinas
Hi Andrew,

On Wed, Oct 19, 2016 at 11:33:27AM -0700, Andrew Morton wrote:
> On Wed, 12 Oct 2016 10:57:03 +0100 Catalin Marinas  
> wrote:
> > 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 
> > Cc: Andy Lutomirski 
> > Cc: CAI Qian 
> > Reported-by: CAI Qian 
> > Signed-off-by: Catalin Marinas 
> 
> I'll add
> 
> Cc: [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()").

-- 
Catalin


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-20 Thread Catalin Marinas
Hi Andrew,

On Wed, Oct 19, 2016 at 11:33:27AM -0700, Andrew Morton wrote:
> On Wed, 12 Oct 2016 10:57:03 +0100 Catalin Marinas  
> wrote:
> > 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 
> > Cc: Andy Lutomirski 
> > Cc: CAI Qian 
> > Reported-by: CAI Qian 
> > Signed-off-by: Catalin Marinas 
> 
> I'll add
> 
> Cc: [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()").

-- 
Catalin


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-19 Thread Andrew Morton
On Wed, 12 Oct 2016 10:57:03 +0100 Catalin Marinas  
wrote:

> 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 
> Cc: Andy Lutomirski 
> Cc: CAI Qian 
> Reported-by: CAI Qian 
> Signed-off-by: Catalin Marinas 

I'll add

Cc: [4.8.x]


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-19 Thread Andrew Morton
On Wed, 12 Oct 2016 10:57:03 +0100 Catalin Marinas  
wrote:

> 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 
> Cc: Andy Lutomirski 
> Cc: CAI Qian 
> Reported-by: CAI Qian 
> Signed-off-by: Catalin Marinas 

I'll add

Cc: [4.8.x]


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-12 Thread CAI Qian


- Original Message -
> From: "Catalin Marinas" 
> To: linux...@kvack.org
> Cc: linux-kernel@vger.kernel.org, "Andrew Morton" 
> , "Andy Lutomirski" ,
> "CAI Qian" 
> 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.
> 
> Fixes: 68f24b08ee89 ("sched/core: Free the stack early if
> CONFIG_THREAD_INFO_IN_TASK")
> Cc: Andrew Morton 
> Cc: Andy Lutomirski 
> Cc: CAI Qian 
> Reported-by: CAI Qian 
> Signed-off-by: Catalin Marinas 

Tested-by: CAI Qian 

> ---
> 
> 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(-)
> 
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index a5e453cf05c4..e5355a5b423f 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
>  
>   read_lock(_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(_lock);
>   }
> 
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majord...@kvack.org.  For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: mailto:"d...@kvack.org;> em...@kvack.org 
> 
> 
> 


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-12 Thread CAI Qian


- Original Message -
> From: "Catalin Marinas" 
> To: linux...@kvack.org
> Cc: linux-kernel@vger.kernel.org, "Andrew Morton" 
> , "Andy Lutomirski" ,
> "CAI Qian" 
> 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.
> 
> Fixes: 68f24b08ee89 ("sched/core: Free the stack early if
> CONFIG_THREAD_INFO_IN_TASK")
> Cc: Andrew Morton 
> Cc: Andy Lutomirski 
> Cc: CAI Qian 
> Reported-by: CAI Qian 
> Signed-off-by: Catalin Marinas 

Tested-by: CAI Qian 

> ---
> 
> 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(-)
> 
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index a5e453cf05c4..e5355a5b423f 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
>  
>   read_lock(_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(_lock);
>   }
> 
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majord...@kvack.org.  For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: mailto:"d...@kvack.org;> em...@kvack.org 
> 
> 
> 


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-12 Thread Catalin Marinas
On Wed, Oct 12, 2016 at 11:54:17AM -0400, CAI Qian wrote:
> - Original Message -
> > From: "Catalin Marinas" 
> > To: linux...@kvack.org
> > Cc: linux-kernel@vger.kernel.org, "Andrew Morton" 
> > , "Andy Lutomirski" ,
> > "CAI Qian" 
> > 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.
> > 
> > Fixes: 68f24b08ee89 ("sched/core: Free the stack early if
> > CONFIG_THREAD_INFO_IN_TASK")
> > Cc: Andrew Morton 
> > Cc: Andy Lutomirski 
> > Cc: CAI Qian 
> > Reported-by: CAI Qian 
> > Signed-off-by: Catalin Marinas 
> 
> Tested-by: CAI Qian 

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.

-- 
Catalin


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-12 Thread Catalin Marinas
On Wed, Oct 12, 2016 at 11:54:17AM -0400, CAI Qian wrote:
> - Original Message -
> > From: "Catalin Marinas" 
> > To: linux...@kvack.org
> > Cc: linux-kernel@vger.kernel.org, "Andrew Morton" 
> > , "Andy Lutomirski" ,
> > "CAI Qian" 
> > 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.
> > 
> > Fixes: 68f24b08ee89 ("sched/core: Free the stack early if
> > CONFIG_THREAD_INFO_IN_TASK")
> > Cc: Andrew Morton 
> > Cc: Andy Lutomirski 
> > Cc: CAI Qian 
> > Reported-by: CAI Qian 
> > Signed-off-by: Catalin Marinas 
> 
> Tested-by: CAI Qian 

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.

-- 
Catalin


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-12 Thread Catalin Marinas
On Wed, Oct 12, 2016 at 06:16:46PM +0800, Hillf Danton wrote:
> > @@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
> > 
> > read_lock(_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?

-- 
Catalin


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-12 Thread Catalin Marinas
On Wed, Oct 12, 2016 at 06:16:46PM +0800, Hillf Danton wrote:
> > @@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
> > 
> > read_lock(_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?

-- 
Catalin


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-12 Thread Michal Hocko
On Wed 12-10-16 10:57:03, Catalin Marinas wrote:
> 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
 
> Fixes: 68f24b08ee89 ("sched/core: Free the stack early if 
> CONFIG_THREAD_INFO_IN_TASK")
> Cc: Andrew Morton 
> Cc: Andy Lutomirski 
> Cc: CAI Qian 
> Reported-by: CAI Qian 
> Signed-off-by: Catalin Marinas 

Acked-by: Michal Hocko 

> ---
> 
> 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(-)
> 
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index a5e453cf05c4..e5355a5b423f 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
>  
>   read_lock(_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(_lock);
>   }
> 
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majord...@kvack.org.  For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: mailto:"d...@kvack.org;> em...@kvack.org 

-- 
Michal Hocko
SUSE Labs


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-12 Thread Michal Hocko
On Wed 12-10-16 10:57:03, Catalin Marinas wrote:
> 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
 
> Fixes: 68f24b08ee89 ("sched/core: Free the stack early if 
> CONFIG_THREAD_INFO_IN_TASK")
> Cc: Andrew Morton 
> Cc: Andy Lutomirski 
> Cc: CAI Qian 
> Reported-by: CAI Qian 
> Signed-off-by: Catalin Marinas 

Acked-by: Michal Hocko 

> ---
> 
> 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(-)
> 
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index a5e453cf05c4..e5355a5b423f 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
>  
>   read_lock(_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(_lock);
>   }
> 
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majord...@kvack.org.  For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: mailto:"d...@kvack.org;> em...@kvack.org 

-- 
Michal Hocko
SUSE Labs


Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-12 Thread Hillf Danton
> @@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
> 
>   read_lock(_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()")

> - 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(_lock);
>   }
> 



Re: [PATCH] mm: kmemleak: Ensure that the task stack is not freed during scanning

2016-10-12 Thread Hillf Danton
> @@ -1453,8 +1453,11 @@ static void kmemleak_scan(void)
> 
>   read_lock(_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()")

> - 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(_lock);
>   }
>