All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
@ 2026-07-27 18:11 Jakov Novak
  2026-07-28  6:12 ` Dev Jain
  2026-07-28 14:57 ` Lorenzo Stoakes (ARM)
  0 siblings, 2 replies; 3+ messages in thread
From: Jakov Novak @ 2026-07-27 18:11 UTC (permalink / raw)
  To: linux-mm, linux-kernel, Andrew Morton, David Hildenbrand,
	Lorenzo Stoakes
  Cc: Zi Yan, Baolin Wang, \  Liam R . Howlett \ , Nico Pache,
	Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
	linux-kernel-mentees, Shuah Khan, Jakov Novak

Currently, khugepaged locks the khugepaged_mutex in two functions:
start_stop_khugepaged and khugepaged_min_free_kbytes_update. Remove
mutex_lock/mutex_unlock usage in these functions and replace it with the
guard macro. This makes the code more readable (removing a goto statement)
and makes it harder to introduce bugs in the future.
No functional changes introduced.

Signed-off-by: Jakov Novak <jakovnovak30@gmail.com>
---
 mm/khugepaged.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 617bca76db49..c583867f2e7a 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -3113,7 +3113,7 @@ int start_stop_khugepaged(void)
 {
 	int err = 0;
 
-	mutex_lock(&khugepaged_mutex);
+	guard(mutex)(&khugepaged_mutex);
 	if (hugepage_enabled()) {
 		if (!khugepaged_thread)
 			khugepaged_thread = kthread_run(khugepaged, NULL,
@@ -3122,7 +3122,7 @@ int start_stop_khugepaged(void)
 			pr_err("khugepaged: kthread_run(khugepaged) failed\n");
 			err = PTR_ERR(khugepaged_thread);
 			khugepaged_thread = NULL;
-			goto fail;
+			return err;
 		}
 
 		if (!list_empty(&khugepaged_scan.mm_head))
@@ -3132,17 +3132,14 @@ int start_stop_khugepaged(void)
 		khugepaged_thread = NULL;
 	}
 	set_recommended_min_free_kbytes();
-fail:
-	mutex_unlock(&khugepaged_mutex);
 	return err;
 }
 
 void khugepaged_min_free_kbytes_update(void)
 {
-	mutex_lock(&khugepaged_mutex);
+	guard(mutex)(&khugepaged_mutex);
 	if (hugepage_enabled() && khugepaged_thread)
 		set_recommended_min_free_kbytes();
-	mutex_unlock(&khugepaged_mutex);
 }
 
 bool current_is_khugepaged(void)
-- 
2.55.0


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

* Re: [PATCH] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
  2026-07-27 18:11 [PATCH] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro Jakov Novak
@ 2026-07-28  6:12 ` Dev Jain
  2026-07-28 14:57 ` Lorenzo Stoakes (ARM)
  1 sibling, 0 replies; 3+ messages in thread
From: Dev Jain @ 2026-07-28  6:12 UTC (permalink / raw)
  To: Jakov Novak, linux-mm, linux-kernel, Andrew Morton,
	David Hildenbrand, Lorenzo Stoakes
  Cc: Zi Yan, Baolin Wang, \ Liam R . Howlett \, Nico Pache,
	Ryan Roberts, Barry Song, Lance Yang, Usama Arif,
	linux-kernel-mentees, Shuah Khan



On 27/07/26 11:41 pm, Jakov Novak wrote:
> Currently, khugepaged locks the khugepaged_mutex in two functions:
> start_stop_khugepaged and khugepaged_min_free_kbytes_update. Remove
> mutex_lock/mutex_unlock usage in these functions and replace it with the
> guard macro. This makes the code more readable (removing a goto statement)
> and makes it harder to introduce bugs in the future.
> No functional changes introduced.
> 
> Signed-off-by: Jakov Novak <jakovnovak30@gmail.com>
> ---

Thanks.

Reviewed-by: Dev Jain <dev.jain@arm.com>

>  mm/khugepaged.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
> 
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 617bca76db49..c583867f2e7a 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -3113,7 +3113,7 @@ int start_stop_khugepaged(void)
>  {
>  	int err = 0;
>  
> -	mutex_lock(&khugepaged_mutex);
> +	guard(mutex)(&khugepaged_mutex);
>  	if (hugepage_enabled()) {
>  		if (!khugepaged_thread)
>  			khugepaged_thread = kthread_run(khugepaged, NULL,
> @@ -3122,7 +3122,7 @@ int start_stop_khugepaged(void)
>  			pr_err("khugepaged: kthread_run(khugepaged) failed\n");
>  			err = PTR_ERR(khugepaged_thread);
>  			khugepaged_thread = NULL;
> -			goto fail;
> +			return err;
>  		}
>  
>  		if (!list_empty(&khugepaged_scan.mm_head))
> @@ -3132,17 +3132,14 @@ int start_stop_khugepaged(void)
>  		khugepaged_thread = NULL;
>  	}
>  	set_recommended_min_free_kbytes();
> -fail:
> -	mutex_unlock(&khugepaged_mutex);
>  	return err;
>  }
>  
>  void khugepaged_min_free_kbytes_update(void)
>  {
> -	mutex_lock(&khugepaged_mutex);
> +	guard(mutex)(&khugepaged_mutex);
>  	if (hugepage_enabled() && khugepaged_thread)
>  		set_recommended_min_free_kbytes();
> -	mutex_unlock(&khugepaged_mutex);
>  }
>  
>  bool current_is_khugepaged(void)



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

* Re: [PATCH] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
  2026-07-27 18:11 [PATCH] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro Jakov Novak
  2026-07-28  6:12 ` Dev Jain
@ 2026-07-28 14:57 ` Lorenzo Stoakes (ARM)
  1 sibling, 0 replies; 3+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-07-28 14:57 UTC (permalink / raw)
  To: Jakov Novak
  Cc: linux-mm, linux-kernel, Andrew Morton, David Hildenbrand, Zi Yan,
	Baolin Wang, \  Liam R . Howlett \ , Nico Pache, Ryan Roberts,
	Dev Jain, Barry Song, Lance Yang, Usama Arif,
	linux-kernel-mentees, Shuah Khan

On Mon, Jul 27, 2026 at 08:11:36PM +0200, Jakov Novak wrote:
> Currently, khugepaged locks the khugepaged_mutex in two functions:
> start_stop_khugepaged and khugepaged_min_free_kbytes_update. Remove
> mutex_lock/mutex_unlock usage in these functions and replace it with the
> guard macro. This makes the code more readable (removing a goto statement)
> and makes it harder to introduce bugs in the future.
> No functional changes introduced.
>
> Signed-off-by: Jakov Novak <jakovnovak30@gmail.com>

Various nits below, with them addressed feel free to add:

Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

To the respin.

> ---
>  mm/khugepaged.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 617bca76db49..c583867f2e7a 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -3113,7 +3113,7 @@ int start_stop_khugepaged(void)
>  {

Please add

#include <linux/cleanup.h>

To the #include's up top.

You might get away with not doing it locally because of recursive header
includes but be better to be explicit.

>  	int err = 0;
>
> -	mutex_lock(&khugepaged_mutex);
> +	guard(mutex)(&khugepaged_mutex);
>  	if (hugepage_enabled()) {
>  		if (!khugepaged_thread)
>  			khugepaged_thread = kthread_run(khugepaged, NULL,
> @@ -3122,7 +3122,7 @@ int start_stop_khugepaged(void)

		if (IS_ERR(khugepaged_thread)) {

Can we move the 'int err' declaration to here then? And not initialise it as it
gets assigned below.

>  			pr_err("khugepaged: kthread_run(khugepaged) failed\n");
>  			err = PTR_ERR(khugepaged_thread);
>  			khugepaged_thread = NULL;
> -			goto fail;
> +			return err;
>  		}
>
>  		if (!list_empty(&khugepaged_scan.mm_head))
> @@ -3132,17 +3132,14 @@ int start_stop_khugepaged(void)
>  		khugepaged_thread = NULL;
>  	}
>  	set_recommended_min_free_kbytes();
> -fail:
> -	mutex_unlock(&khugepaged_mutex);
>  	return err;

Let's make this return 0 now.

>  }
>
>  void khugepaged_min_free_kbytes_update(void)
>  {
> -	mutex_lock(&khugepaged_mutex);
> +	guard(mutex)(&khugepaged_mutex);
>  	if (hugepage_enabled() && khugepaged_thread)
>  		set_recommended_min_free_kbytes();
> -	mutex_unlock(&khugepaged_mutex);

Nice and simple!

>  }
>
>  bool current_is_khugepaged(void)
> --
> 2.55.0
>

Cheers, Lorenzo


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

end of thread, other threads:[~2026-07-28 14:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-27 18:11 [PATCH] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro Jakov Novak
2026-07-28  6:12 ` Dev Jain
2026-07-28 14:57 ` Lorenzo Stoakes (ARM)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.