linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] mm: task_stack: Stack handling cleanups
@ 2025-08-29 11:44 Linus Walleij
  2025-08-29 11:44 ` [PATCH 1/2] fork: check charging success before zeroing stack Linus Walleij
  2025-08-29 11:44 ` [PATCH 2/2] task_stack.h: Clean-up stack_not_used() implementation Linus Walleij
  0 siblings, 2 replies; 7+ messages in thread
From: Linus Walleij @ 2025-08-29 11:44 UTC (permalink / raw)
  To: Andrew Morton, Ingo Molnar, Peter Zijlstra, Juri Lelli,
	Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Mel Gorman, Valentin Schneider, Kees Cook, David Hildenbrand,
	Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Pasha Tatashin
  Cc: linux-kernel, linux-mm, Linus Walleij

These are some small cleanups for the fork code that was
split off from Pasha:s dynamic stack patch series, they
are generally nice on their own so let's propose them for
merging.

This will go through Andrews tree I suppose.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
Pasha Tatashin (2):
      fork: check charging success before zeroing stack
      task_stack.h: Clean-up stack_not_used() implementation

 kernel/exit.c | 23 ++++++++++++++---------
 kernel/fork.c | 10 +++++-----
 2 files changed, 19 insertions(+), 14 deletions(-)
---
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
change-id: 20250829-fork-cleanups-for-dynstack-2051bdf2a2ea

Best regards,
-- 
Linus Walleij <linus.walleij@linaro.org>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] fork: check charging success before zeroing stack
  2025-08-29 11:44 [PATCH 0/2] mm: task_stack: Stack handling cleanups Linus Walleij
@ 2025-08-29 11:44 ` Linus Walleij
  2025-08-29 15:39   ` Liam R. Howlett
  2025-09-01 11:58   ` Lorenzo Stoakes
  2025-08-29 11:44 ` [PATCH 2/2] task_stack.h: Clean-up stack_not_used() implementation Linus Walleij
  1 sibling, 2 replies; 7+ messages in thread
From: Linus Walleij @ 2025-08-29 11:44 UTC (permalink / raw)
  To: Andrew Morton, Ingo Molnar, Peter Zijlstra, Juri Lelli,
	Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Mel Gorman, Valentin Schneider, Kees Cook, David Hildenbrand,
	Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Pasha Tatashin
  Cc: linux-kernel, linux-mm, Linus Walleij

From: Pasha Tatashin <pasha.tatashin@soleen.com>

No need to do zero cached stack if memcg charge fails, so move the
charging attempt before the memset operation.

Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Link: https://lore.kernel.org/20240311164638.2015063-6-pasha.tatashin@soleen.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 kernel/fork.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/kernel/fork.c b/kernel/fork.c
index af673856499dcaa35e135a9e8042ef28d5c5370d..2a5b7a5fa09b1f3a42473cf44a1316ec8b3b31d0 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -290,6 +290,11 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)
 		if (!vm_area)
 			continue;
 
+		if (memcg_charge_kernel_stack(vm_area)) {
+			vfree(vm_area->addr);
+			return -ENOMEM;
+		}
+
 		/* Reset stack metadata. */
 		kasan_unpoison_range(vm_area->addr, THREAD_SIZE);
 
@@ -298,11 +303,6 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)
 		/* Clear stale pointers from reused stack. */
 		memset(stack, 0, THREAD_SIZE);
 
-		if (memcg_charge_kernel_stack(vm_area)) {
-			vfree(vm_area->addr);
-			return -ENOMEM;
-		}
-
 		tsk->stack_vm_area = vm_area;
 		tsk->stack = stack;
 		return 0;

-- 
2.50.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] task_stack.h: Clean-up stack_not_used() implementation
  2025-08-29 11:44 [PATCH 0/2] mm: task_stack: Stack handling cleanups Linus Walleij
  2025-08-29 11:44 ` [PATCH 1/2] fork: check charging success before zeroing stack Linus Walleij
@ 2025-08-29 11:44 ` Linus Walleij
  2025-08-29 15:41   ` Liam R. Howlett
  2025-09-01 11:58   ` Lorenzo Stoakes
  1 sibling, 2 replies; 7+ messages in thread
From: Linus Walleij @ 2025-08-29 11:44 UTC (permalink / raw)
  To: Andrew Morton, Ingo Molnar, Peter Zijlstra, Juri Lelli,
	Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Mel Gorman, Valentin Schneider, Kees Cook, David Hildenbrand,
	Lorenzo Stoakes, Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Pasha Tatashin
  Cc: linux-kernel, linux-mm, Linus Walleij

From: Pasha Tatashin <pasha.tatashin@soleen.com>

Inside the small stack_not_used() function there are several ifdefs for
stack growing-up vs. regular versions. Instead just implement this
function two times, one for growing-up and another regular.

Add comments like /* !CONFIG_DEBUG_STACK_USAGE */ to clarify what the
ifdefs are doing.

[linus.walleij@linaro.org: Rebased, function moved elsewhere in the kernel]
Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Link: https://lore.kernel.org/20240311164638.2015063-13-pasha.tatashin@soleen.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 kernel/exit.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/kernel/exit.c b/kernel/exit.c
index 343eb97543d568baeb23142edcc9050a8b8be8bf..9f74e8f1c431b6aa6e391ff71aadf9895a3857ae 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -780,24 +780,29 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
 }
 
 #ifdef CONFIG_DEBUG_STACK_USAGE
+#ifdef CONFIG_STACK_GROWSUP
 unsigned long stack_not_used(struct task_struct *p)
 {
 	unsigned long *n = end_of_stack(p);
 
 	do {	/* Skip over canary */
-# ifdef CONFIG_STACK_GROWSUP
 		n--;
-# else
-		n++;
-# endif
 	} while (!*n);
 
-# ifdef CONFIG_STACK_GROWSUP
 	return (unsigned long)end_of_stack(p) - (unsigned long)n;
-# else
+}
+#else /* !CONFIG_STACK_GROWSUP */
+unsigned long stack_not_used(struct task_struct *p)
+{
+	unsigned long *n = end_of_stack(p);
+
+	do {	/* Skip over canary */
+		n++;
+	} while (!*n);
+
 	return (unsigned long)n - (unsigned long)end_of_stack(p);
-# endif
 }
+#endif /* CONFIG_STACK_GROWSUP */
 
 /* Count the maximum pages reached in kernel stacks */
 static inline void kstack_histogram(unsigned long used_stack)
@@ -856,9 +861,9 @@ static void check_stack_usage(void)
 	}
 	spin_unlock(&low_water_lock);
 }
-#else
+#else /* !CONFIG_DEBUG_STACK_USAGE */
 static inline void check_stack_usage(void) {}
-#endif
+#endif /* CONFIG_DEBUG_STACK_USAGE */
 
 static void synchronize_group_exit(struct task_struct *tsk, long code)
 {

-- 
2.50.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] fork: check charging success before zeroing stack
  2025-08-29 11:44 ` [PATCH 1/2] fork: check charging success before zeroing stack Linus Walleij
@ 2025-08-29 15:39   ` Liam R. Howlett
  2025-09-01 11:58   ` Lorenzo Stoakes
  1 sibling, 0 replies; 7+ messages in thread
From: Liam R. Howlett @ 2025-08-29 15:39 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Andrew Morton, Ingo Molnar, Peter Zijlstra, Juri Lelli,
	Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Mel Gorman, Valentin Schneider, Kees Cook, David Hildenbrand,
	Lorenzo Stoakes, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Pasha Tatashin, linux-kernel,
	linux-mm

* Linus Walleij <linus.walleij@linaro.org> [250829 07:44]:
> From: Pasha Tatashin <pasha.tatashin@soleen.com>
> 
> No need to do zero cached stack if memcg charge fails, so move the
> charging attempt before the memset operation.
> 
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> Link: https://lore.kernel.org/20240311164638.2015063-6-pasha.tatashin@soleen.com
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>


Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>

> ---
>  kernel/fork.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
> 
> diff --git a/kernel/fork.c b/kernel/fork.c
> index af673856499dcaa35e135a9e8042ef28d5c5370d..2a5b7a5fa09b1f3a42473cf44a1316ec8b3b31d0 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -290,6 +290,11 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)
>  		if (!vm_area)
>  			continue;
>  
> +		if (memcg_charge_kernel_stack(vm_area)) {
> +			vfree(vm_area->addr);
> +			return -ENOMEM;
> +		}
> +
>  		/* Reset stack metadata. */
>  		kasan_unpoison_range(vm_area->addr, THREAD_SIZE);
>  
> @@ -298,11 +303,6 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)
>  		/* Clear stale pointers from reused stack. */
>  		memset(stack, 0, THREAD_SIZE);
>  
> -		if (memcg_charge_kernel_stack(vm_area)) {
> -			vfree(vm_area->addr);
> -			return -ENOMEM;
> -		}
> -
>  		tsk->stack_vm_area = vm_area;
>  		tsk->stack = stack;
>  		return 0;
> 
> -- 
> 2.50.1
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] task_stack.h: Clean-up stack_not_used() implementation
  2025-08-29 11:44 ` [PATCH 2/2] task_stack.h: Clean-up stack_not_used() implementation Linus Walleij
@ 2025-08-29 15:41   ` Liam R. Howlett
  2025-09-01 11:58   ` Lorenzo Stoakes
  1 sibling, 0 replies; 7+ messages in thread
From: Liam R. Howlett @ 2025-08-29 15:41 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Andrew Morton, Ingo Molnar, Peter Zijlstra, Juri Lelli,
	Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Mel Gorman, Valentin Schneider, Kees Cook, David Hildenbrand,
	Lorenzo Stoakes, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Pasha Tatashin, linux-kernel,
	linux-mm

* Linus Walleij <linus.walleij@linaro.org> [250829 07:44]:
> From: Pasha Tatashin <pasha.tatashin@soleen.com>
> 
> Inside the small stack_not_used() function there are several ifdefs for
> stack growing-up vs. regular versions. Instead just implement this
> function two times, one for growing-up and another regular.
> 
> Add comments like /* !CONFIG_DEBUG_STACK_USAGE */ to clarify what the
> ifdefs are doing.
> 
> [linus.walleij@linaro.org: Rebased, function moved elsewhere in the kernel]
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> Link: https://lore.kernel.org/20240311164638.2015063-13-pasha.tatashin@soleen.com
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

Thanks for this.

Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>

> ---
>  kernel/exit.c | 23 ++++++++++++++---------
>  1 file changed, 14 insertions(+), 9 deletions(-)
> 
> diff --git a/kernel/exit.c b/kernel/exit.c
> index 343eb97543d568baeb23142edcc9050a8b8be8bf..9f74e8f1c431b6aa6e391ff71aadf9895a3857ae 100644
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
> @@ -780,24 +780,29 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
>  }
>  
>  #ifdef CONFIG_DEBUG_STACK_USAGE
> +#ifdef CONFIG_STACK_GROWSUP
>  unsigned long stack_not_used(struct task_struct *p)
>  {
>  	unsigned long *n = end_of_stack(p);
>  
>  	do {	/* Skip over canary */
> -# ifdef CONFIG_STACK_GROWSUP
>  		n--;
> -# else
> -		n++;
> -# endif
>  	} while (!*n);
>  
> -# ifdef CONFIG_STACK_GROWSUP
>  	return (unsigned long)end_of_stack(p) - (unsigned long)n;
> -# else
> +}
> +#else /* !CONFIG_STACK_GROWSUP */
> +unsigned long stack_not_used(struct task_struct *p)
> +{
> +	unsigned long *n = end_of_stack(p);
> +
> +	do {	/* Skip over canary */
> +		n++;
> +	} while (!*n);
> +
>  	return (unsigned long)n - (unsigned long)end_of_stack(p);
> -# endif
>  }
> +#endif /* CONFIG_STACK_GROWSUP */
>  
>  /* Count the maximum pages reached in kernel stacks */
>  static inline void kstack_histogram(unsigned long used_stack)
> @@ -856,9 +861,9 @@ static void check_stack_usage(void)
>  	}
>  	spin_unlock(&low_water_lock);
>  }
> -#else
> +#else /* !CONFIG_DEBUG_STACK_USAGE */
>  static inline void check_stack_usage(void) {}
> -#endif
> +#endif /* CONFIG_DEBUG_STACK_USAGE */
>  
>  static void synchronize_group_exit(struct task_struct *tsk, long code)
>  {
> 
> -- 
> 2.50.1
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/2] fork: check charging success before zeroing stack
  2025-08-29 11:44 ` [PATCH 1/2] fork: check charging success before zeroing stack Linus Walleij
  2025-08-29 15:39   ` Liam R. Howlett
@ 2025-09-01 11:58   ` Lorenzo Stoakes
  1 sibling, 0 replies; 7+ messages in thread
From: Lorenzo Stoakes @ 2025-09-01 11:58 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Andrew Morton, Ingo Molnar, Peter Zijlstra, Juri Lelli,
	Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Mel Gorman, Valentin Schneider, Kees Cook, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Pasha Tatashin, linux-kernel,
	linux-mm

On Fri, Aug 29, 2025 at 01:44:40PM +0200, Linus Walleij wrote:
> From: Pasha Tatashin <pasha.tatashin@soleen.com>
>
> No need to do zero cached stack if memcg charge fails, so move the
> charging attempt before the memset operation.
>
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> Link: https://lore.kernel.org/20240311164638.2015063-6-pasha.tatashin@soleen.com
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

LGTM, so:

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

> ---
>  kernel/fork.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/kernel/fork.c b/kernel/fork.c
> index af673856499dcaa35e135a9e8042ef28d5c5370d..2a5b7a5fa09b1f3a42473cf44a1316ec8b3b31d0 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -290,6 +290,11 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)
>  		if (!vm_area)
>  			continue;
>
> +		if (memcg_charge_kernel_stack(vm_area)) {
> +			vfree(vm_area->addr);
> +			return -ENOMEM;
> +		}
> +
>  		/* Reset stack metadata. */
>  		kasan_unpoison_range(vm_area->addr, THREAD_SIZE);
>
> @@ -298,11 +303,6 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)
>  		/* Clear stale pointers from reused stack. */
>  		memset(stack, 0, THREAD_SIZE);
>
> -		if (memcg_charge_kernel_stack(vm_area)) {
> -			vfree(vm_area->addr);
> -			return -ENOMEM;
> -		}
> -
>  		tsk->stack_vm_area = vm_area;
>  		tsk->stack = stack;
>  		return 0;
>
> --
> 2.50.1
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] task_stack.h: Clean-up stack_not_used() implementation
  2025-08-29 11:44 ` [PATCH 2/2] task_stack.h: Clean-up stack_not_used() implementation Linus Walleij
  2025-08-29 15:41   ` Liam R. Howlett
@ 2025-09-01 11:58   ` Lorenzo Stoakes
  1 sibling, 0 replies; 7+ messages in thread
From: Lorenzo Stoakes @ 2025-09-01 11:58 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Andrew Morton, Ingo Molnar, Peter Zijlstra, Juri Lelli,
	Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
	Mel Gorman, Valentin Schneider, Kees Cook, David Hildenbrand,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Pasha Tatashin, linux-kernel,
	linux-mm

On Fri, Aug 29, 2025 at 01:44:41PM +0200, Linus Walleij wrote:
> From: Pasha Tatashin <pasha.tatashin@soleen.com>
>
> Inside the small stack_not_used() function there are several ifdefs for
> stack growing-up vs. regular versions. Instead just implement this
> function two times, one for growing-up and another regular.
>
> Add comments like /* !CONFIG_DEBUG_STACK_USAGE */ to clarify what the
> ifdefs are doing.
>
> [linus.walleij@linaro.org: Rebased, function moved elsewhere in the kernel]
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> Link: https://lore.kernel.org/20240311164638.2015063-13-pasha.tatashin@soleen.com
> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

LGTM, lovely change! :)

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

> ---
>  kernel/exit.c | 23 ++++++++++++++---------
>  1 file changed, 14 insertions(+), 9 deletions(-)
>
> diff --git a/kernel/exit.c b/kernel/exit.c
> index 343eb97543d568baeb23142edcc9050a8b8be8bf..9f74e8f1c431b6aa6e391ff71aadf9895a3857ae 100644
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
> @@ -780,24 +780,29 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
>  }
>
>  #ifdef CONFIG_DEBUG_STACK_USAGE
> +#ifdef CONFIG_STACK_GROWSUP
>  unsigned long stack_not_used(struct task_struct *p)
>  {
>  	unsigned long *n = end_of_stack(p);
>
>  	do {	/* Skip over canary */
> -# ifdef CONFIG_STACK_GROWSUP
>  		n--;
> -# else
> -		n++;
> -# endif
>  	} while (!*n);
>
> -# ifdef CONFIG_STACK_GROWSUP
>  	return (unsigned long)end_of_stack(p) - (unsigned long)n;
> -# else
> +}
> +#else /* !CONFIG_STACK_GROWSUP */
> +unsigned long stack_not_used(struct task_struct *p)
> +{
> +	unsigned long *n = end_of_stack(p);
> +
> +	do {	/* Skip over canary */
> +		n++;
> +	} while (!*n);
> +
>  	return (unsigned long)n - (unsigned long)end_of_stack(p);
> -# endif
>  }
> +#endif /* CONFIG_STACK_GROWSUP */
>
>  /* Count the maximum pages reached in kernel stacks */
>  static inline void kstack_histogram(unsigned long used_stack)
> @@ -856,9 +861,9 @@ static void check_stack_usage(void)
>  	}
>  	spin_unlock(&low_water_lock);
>  }
> -#else
> +#else /* !CONFIG_DEBUG_STACK_USAGE */
>  static inline void check_stack_usage(void) {}
> -#endif
> +#endif /* CONFIG_DEBUG_STACK_USAGE */
>
>  static void synchronize_group_exit(struct task_struct *tsk, long code)
>  {
>
> --
> 2.50.1
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-09-01 11:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-29 11:44 [PATCH 0/2] mm: task_stack: Stack handling cleanups Linus Walleij
2025-08-29 11:44 ` [PATCH 1/2] fork: check charging success before zeroing stack Linus Walleij
2025-08-29 15:39   ` Liam R. Howlett
2025-09-01 11:58   ` Lorenzo Stoakes
2025-08-29 11:44 ` [PATCH 2/2] task_stack.h: Clean-up stack_not_used() implementation Linus Walleij
2025-08-29 15:41   ` Liam R. Howlett
2025-09-01 11:58   ` Lorenzo Stoakes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).