mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
Subsystems (2)
PATCH v3 1/1 mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
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.
---
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(-)
mm/khugepaged.c +15 -16
@@ -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" ); + +if (IS_ERR (new_thread)) { +pr_err ("khugepaged: kthread_run(khugepaged) failed\n" ); +return PTR_ERR (new_thread); + } + + khugepaged_thread = new_thread; }if (!list_empty (&khugepaged_scan.mm_head))
@@ -3132,17 +3134,14 @@ int start_stop_khugepaged(void) khugepaged_thread =NULL ; }set_recommended_min_free_kbytes (); -fail: -mutex_unlock (&khugepaged_mutex); -return err; +return 0 ; }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 )
Signature · 1 line
2.55.0
Quoted 49 lines · Jakov Novak
Currently, khugepaged locks the
khugepaged_mutexin two functions:start_stop_khugepagedandkhugepaged_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 instart_stop_khugepaged
- Made default case return 0 instead of err instart_stop_khugepaged
v3:
- Moved new thread creation logic inside if (!khugepaged_thread) block instart_stop_khugepaged
- Removed suboptimal err variablemm/khugepaged.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)mm/khugepaged.c +2
-5@@ -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,
mm/khugepaged.c void set_recommended_min_free_kbytes(void) · line 3120
Quoted 10 lines
mm/khugepaged.c +4
-6- "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.
Signature · 3 lines
Cheers, David
Quoted 11 lines · Jakov Novak
Currently, khugepaged locks the
khugepaged_mutexin two functions:start_stop_khugepagedandkhugepaged_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]>
Hmm, did Andrew actually give this R-b tag out? I can't find it on-list.
Signature · 1 line
Pedro
Quoted 8 lines · On Fri, 31 Jul 2026 11:33:47 +0100 Pedro Falcato <[email protected]> wrote:
Quoted 7 lines · Jakov Novak
Currently, khugepaged locks the
khugepaged_mutexin two functions:start_stop_khugepagedandkhugepaged_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.
Quoted 6 lines
Quoted 4 lines
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]>Hmm, did Andrew actually give this R-b tag out? I can't find it on-list.
I don't think so. Hallucination ;)
I do sometimes sneak in an offlist R-b when I'm looking at things but I'm pretty inconsistent and lazy about it.
<looks at it again>
Quoted 11 lines
Quoted 6 lines
Quoted 4 lines
Reviewed-by Andrew Morton <[email protected]>Hmm, did Andrew actually give this R-b tag out? I can't find it on-list.
I don't think so. Hallucination ;)
I do sometimes sneak in an offlist R-b when I'm looking at things but I'm pretty inconsistent and lazy about it.
I saw the tag on the mm-new git [1] and I wasn't completely sure if it should be included in the next patch. Sorry about that.
As a general rule, offlist tags should never be copied?