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

"David Hildenbrand (Arm)" <[email protected]> Fri, 31 Jul 2026 11:20:38 +0200
Newsgroups dev.linux.lists.linux-kernel-mentees,org.kernel.vger.linux-kernel,org.kvack.linux-mm
Message-ID <[email protected]>
On 7/30/26 22:47, 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]>
> Reviewed-by: Dev Jain <[email protected]>
> Reviewed-by: Lorenzo Stoakes (ARM) <[email protected]>
> Reviewed-by: Andrew Morton <[email protected]>
> Reviewed-by: Zi Yan <[email protected]>
> ---
> v2:
>   - Added #include <linux/cleanup.h> to the includes at the top of the file
>   - Moved err declaration to the scope where it's used in
>     start_stop_khugepaged
>   - Made default case return 0 instead of err in start_stop_khugepaged
> v3:
>   - Moved new thread creation logic inside if (!khugepaged_thread) block
>     in start_stop_khugepaged
>   - Removed suboptimal err variable
> 
>  mm/khugepaged.c | 31 +++++++++++++++----------------
>  1 file changed, 15 insertions(+), 16 deletions(-)
> 
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 617bca76db49..97f61a050f7c 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -23,6 +23,7 @@
>  #include <linux/ksm.h>
>  #include <linux/pgalloc.h>
>  #include <linux/backing-dev.h>
> +#include <linux/cleanup.h>
>  
>  #include <asm/tlb.h>
>  #include "internal.h"
> @@ -3111,18 +3112,19 @@ void set_recommended_min_free_kbytes(void)
>  
>  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,
> -							"khugepaged");
> -		if (IS_ERR(khugepaged_thread)) {
> -			pr_err("khugepaged: kthread_run(khugepaged) failed\n");
> -			err = PTR_ERR(khugepaged_thread);
> -			khugepaged_thread = NULL;
> -			goto fail;
> +		if (!khugepaged_thread) {
> +			struct task_struct *new_thread = kthread_run(khugepaged,
> +								     NULL,
> +								     "khugepaged");


Nit: we can exceed 80c to increase readability.

Acked-by: David Hildenbrand (Arm) <[email protected]>

-- 
Cheers,

David