Skip to contentSkip to conversation

mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro

PATCH Applied Jakov Novak 4 participants 5 messages Jul 30 – Jul 31

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index 617bca76db49..97f61a050f7c 100644
@@ -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
maintainer Acked-by on v3 1/1
#1 lore raw
Quoted 49 lines · 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.

---
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 +2 -5
index 617bca76db49..97f61a050f7c 100644
@@ -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
#2 lore raw
In reply to Jakov Novak
Quoted 11 lines · 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.

Hmm, did Andrew actually give this R-b tag out? I can't find it on-list.

Signature · 1 line
Pedro
maintainer Reviewed-by
#3 lore raw
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_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.

Quoted 6 lines
Quoted 4 lines

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>

#4 lore raw
Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

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?

[1] https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-khugepaged-replace-mutex_lock-mutex_unlock-usage-with-guard-macro.patch