* [PATCH v2] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
@ 2026-07-28 20:46 Jakov Novak
2026-07-29 1:35 ` Zi Yan
2026-07-29 7:56 ` David Hildenbrand (Arm)
0 siblings, 2 replies; 5+ messages in thread
From: Jakov Novak @ 2026-07-28 20:46 UTC (permalink / raw)
To: linux-mm, linux-kernel, linux-kernel-mentees, Andrew Morton,
David Hildenbrand, Lorenzo Stoakes
Cc: Zi Yan, Baolin Wang, \ Liam R . Howlett \ , Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Shuah Khan, 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.
Signed-off-by: Jakov Novak <jakovnovak30@gmail.com>
Reviewed-by: Dev Jain <dev.jain@arm.com>
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
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(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 617bca76db49..7890bd3e2a9d 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,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)
--
2.55.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v2] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
2026-07-28 20:46 [PATCH v2] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro Jakov Novak
@ 2026-07-29 1:35 ` Zi Yan
2026-07-29 7:56 ` David Hildenbrand (Arm)
1 sibling, 0 replies; 5+ messages in thread
From: Zi Yan @ 2026-07-29 1:35 UTC (permalink / raw)
To: Jakov Novak, linux-mm, linux-kernel, linux-kernel-mentees,
Andrew Morton, David Hildenbrand, Lorenzo Stoakes
Cc: Baolin Wang, Liam R . Howlett , Nico Pache, Ryan Roberts,
Dev Jain, Barry Song, Lance Yang, Usama Arif, Shuah Khan
On Tue Jul 28, 2026 at 4:46 PM EDT, 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@gmail.com>
> Reviewed-by: Dev Jain <dev.jain@arm.com>
> Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> 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(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 617bca76db49..7890bd3e2a9d 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,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;
A new line here?
> 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.
Reviewed-by: Zi Yan <ziy@nvidia.com>
--
Best Regards,
Yan, Zi
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
2026-07-28 20:46 [PATCH v2] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro Jakov Novak
2026-07-29 1:35 ` Zi Yan
@ 2026-07-29 7:56 ` David Hildenbrand (Arm)
2026-07-29 11:56 ` jakov novak
1 sibling, 1 reply; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-07-29 7:56 UTC (permalink / raw)
To: Jakov Novak, linux-mm, linux-kernel, linux-kernel-mentees,
Andrew Morton, Lorenzo Stoakes
Cc: Zi Yan, Baolin Wang, \ Liam R . Howlett \, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Usama Arif,
Shuah Khan
On 7/28/26 22:46, 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@gmail.com>
> Reviewed-by: Dev Jain <dev.jain@arm.com>
> Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> 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(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 617bca76db49..7890bd3e2a9d 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,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;
> }
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
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
2026-07-29 7:56 ` David Hildenbrand (Arm)
@ 2026-07-29 11:56 ` jakov novak
2026-07-30 14:01 ` David Hildenbrand (Arm)
0 siblings, 1 reply; 5+ messages in thread
From: jakov novak @ 2026-07-29 11:56 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: linux-mm, linux-kernel, linux-kernel-mentees, Andrew Morton,
Lorenzo Stoakes, Zi Yan, Baolin Wang, \ Liam R . Howlett \,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Shuah Khan
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
On Wed, Jul 29, 2026 at 9:56 AM David Hildenbrand (Arm)
<david@kernel.org> wrote:
>
> On 7/28/26 22:46, 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@gmail.com>
> > Reviewed-by: Dev Jain <dev.jain@arm.com>
> > Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> > ---
> > 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(-)
> >
> > diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> > index 617bca76db49..7890bd3e2a9d 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,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;
> > }
>
> 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
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v2] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro
2026-07-29 11:56 ` jakov novak
@ 2026-07-30 14:01 ` David Hildenbrand (Arm)
0 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-07-30 14:01 UTC (permalink / raw)
To: jakov novak
Cc: linux-mm, linux-kernel, linux-kernel-mentees, Andrew Morton,
Lorenzo Stoakes, Zi Yan, Baolin Wang, \ Liam R . Howlett \,
Nico Pache, Ryan Roberts, Dev Jain, Barry Song, Lance Yang,
Usama Arif, Shuah Khan
On 7/29/26 13:56, jakov novak wrote:
> 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.
>
Yes, please so so.
--
Cheers,
David
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-07-30 14:01 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-28 20:46 [PATCH v2] mm/khugepaged: replace mutex_lock/mutex_unlock usage with guard macro Jakov Novak
2026-07-29 1:35 ` Zi Yan
2026-07-29 7:56 ` David Hildenbrand (Arm)
2026-07-29 11:56 ` jakov novak
2026-07-30 14:01 ` David Hildenbrand (Arm)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox