Re: [PATCH] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro

"Lorenzo Stoakes (ARM)" <[email protected]> Tue, 28 Jul 2026 15:57:41 +0100
Newsgroups dev.linux.lists.linux-kernel-mentees,org.kernel.vger.linux-kernel,org.kvack.linux-mm
Message-ID <amjCQJUDNYseR6y-@lucifer>
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 <[email protected]>

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

Reviewed-by: Lorenzo Stoakes (ARM) <[email protected]>

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