[PATCH 2/2] mm/kmemleak: Use PF_KTHREAD flag to detect kernel threads

Zhongqiu Han posted 2 patches 1 week, 3 days ago
[PATCH 2/2] mm/kmemleak: Use PF_KTHREAD flag to detect kernel threads
Posted by Zhongqiu Han 1 week, 3 days ago
Replace the current->mm check with PF_KTHREAD flag for more reliable
kernel thread detection in scan_should_stop(). The PF_KTHREAD flag is the
standard way to identify kernel threads and is not affected by temporary
mm borrowing via use_mm() (although kmemleak does not currently encounter
such cases, this makes the code more robust).

No functional change.

Signed-off-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
---
 mm/kmemleak.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index fb0022f34393..eb2ffbaf2f7e 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -1507,10 +1507,10 @@ static int scan_should_stop(void)
 	 * This function may be called from either process or kthread context,
 	 * hence the need to check for both stop conditions.
 	 */
-	if (current->mm)
-		return signal_pending(current);
+	if (current->flags & PF_KTHREAD)
+		return kthread_should_stop();
 
-	return kthread_should_stop();
+	return signal_pending(current);
 }
 
 /*
-- 
2.43.0
Re: [PATCH 2/2] mm/kmemleak: Use PF_KTHREAD flag to detect kernel threads
Posted by Catalin Marinas 5 days, 21 hours ago
On Fri, Jan 30, 2026 at 05:37:29PM +0800, Zhongqiu Han wrote:
> Replace the current->mm check with PF_KTHREAD flag for more reliable
> kernel thread detection in scan_should_stop(). The PF_KTHREAD flag is the
> standard way to identify kernel threads and is not affected by temporary
> mm borrowing via use_mm() (although kmemleak does not currently encounter
> such cases, this makes the code more robust).

Nitpick: it's called kthread_use_mm() now.

But you are right, this function is either called in a user thread
context (via a sysfs write) or from the kthread that kmemleak created
which would not use kthread_use_mm().

> No functional change.
> 
> Signed-off-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
> ---
>  mm/kmemleak.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index fb0022f34393..eb2ffbaf2f7e 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -1507,10 +1507,10 @@ static int scan_should_stop(void)
>  	 * This function may be called from either process or kthread context,
>  	 * hence the need to check for both stop conditions.
>  	 */
> -	if (current->mm)
> -		return signal_pending(current);
> +	if (current->flags & PF_KTHREAD)
> +		return kthread_should_stop();
>  
> -	return kthread_should_stop();
> +	return signal_pending(current);

Acked-by: Catalin Marinas <catalin.marinas@arm.com>