Skip to contentSkip to conversation

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

PATCH Superseded Jakov Novak 3 participants 5 messages Jul 28 – Jul 30

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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

 mm/khugepaged.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
mm/khugepaged.c +6 -9
index 617bca76db49..7890bd3e2a9d 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,17 @@ 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)) {
+			int err;
 			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;
+	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
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 36 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

 mm/khugepaged.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
mm/khugepaged.c +1 -0
index 617bca76db49..7890bd3e2a9d 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,17 @@ void set_recommended_min_free_kbytes(void)
 
 int start_stop_khugepaged(void)
 {

mm/khugepaged.c void set_recommended_min_free_kbytes(void) · line 3121

Quoted 10 lines
mm/khugepaged.c +2 -3
-	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)) {
+			int err;

A new line here?

mm/khugepaged.c void set_recommended_min_free_kbytes(void) · line 3128

Quoted 8 lines
mm/khugepaged.c +1 -1
 			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))

Otherwise, LGTM.

Signature · 2 lines
Best Regards,
Yan, Zi
#2 lore raw
In reply to Jakov Novak
Quoted 42 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

 mm/khugepaged.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
mm/khugepaged.c +2 -3
index 617bca76db49..7890bd3e2a9d 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,17 @@ 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)

mm/khugepaged.c void set_recommended_min_free_kbytes(void) · line 3126

Quoted 10 lines
mm/khugepaged.c +2 -1
 			khugepaged_thread = kthread_run(khugepaged, NULL,
 							"khugepaged");
 		if (IS_ERR(khugepaged_thread)) {
+			int err;
 			pr_err("khugepaged: kthread_run(khugepaged) failed\n");
 			err = PTR_ERR(khugepaged_thread);
 			khugepaged_thread = NULL;
-			goto fail;
+			return err;
 		}

While at it, can we just do

struct task_struct *new_thread = kthread_run...

if (IS_ERR(new_thread)) {
	pr_err("khugepaged: kthread_run(khugepaged) failed\n");
	return PTR_ERR(new_thread);
}

khugepaged_thread = new_thread;

?

Avoid the suboptimal err variable and temporarily assigning error values to the global variable.

Signature · 3 lines
Cheers,

David
#3 lore raw

I guess the error checking could be moved inside the if (!khugepaged_thread) block like:

if (!khugepaged_thread) {
struct task_struct *new_thread = kthread_run...

  if (IS_ERR(new_thread)) {
    pr_err...
    return PTR_ERR(new_thread);
  }

khugepaged_thread = new_thread;
}

I can send a v3 with these changes if this looks good to everybody.

Best,
Jakov

Quoted 74 lines · David Hildenbrand (Arm)
Quoted 42 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

 mm/khugepaged.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
mm/khugepaged.c +2 -3
index 617bca76db49..7890bd3e2a9d 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,17 @@ 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)

mm/khugepaged.c void set_recommended_min_free_kbytes(void) · line 3126

Quoted 10 lines
mm/khugepaged.c +2 -1
                      khugepaged_thread = kthread_run(khugepaged, NULL,
                                                      "khugepaged");
              if (IS_ERR(khugepaged_thread)) {
+                     int err;
                      pr_err("khugepaged: kthread_run(khugepaged) failed\n");
                      err = PTR_ERR(khugepaged_thread);
                      khugepaged_thread = NULL;
-                     goto fail;
+                     return err;
              }

While at it, can we just do

struct task_struct *new_thread = kthread_run...

if (IS_ERR(new_thread)) {
        pr_err("khugepaged: kthread_run(khugepaged) failed\n");
        return PTR_ERR(new_thread);
}

khugepaged_thread = new_thread;

?

Avoid the suboptimal err variable and temporarily assigning error values to the global variable.

--
Cheers,

David

#4 lore raw
Quoted 11 lines · jakov novak

I guess the error checking could be moved inside the if (!khugepaged_thread) block like:

if (!khugepaged_thread) {
struct task_struct *new_thread = kthread_run...

  if (IS_ERR(new_thread)) {
    pr_err...
    return PTR_ERR(new_thread);
  }
Quoted 5 lines

khugepaged_thread = new_thread;
}

I can send a v3 with these changes if this looks good to everybody.

Yes, please so so.

Signature · 3 lines
Cheers,

David