Re: [PATCH] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
From: Lorenzo Stoakes (ARM)
Date: Tue Jul 28 2026 - 11:19:54 EST
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@xxxxxxxxx>
Various nits below, with them addressed feel free to add:
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
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