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

jakov novak <[email protected]> Wed, 29 Jul 2026 13:56:49 +0200
Newsgroups dev.linux.lists.linux-kernel-mentees,org.kernel.vger.linux-kernel,org.kvack.linux-mm
Message-ID <CAPUiDP1POYKJQ=Gfm-irzUmGaSNfZm+qO0xGP7Z=FO8a=R-6Kw@mail.gmail.com>
I guess the error checking could be moved inside the if
(!khugepaged_thread) block like:

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

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

  khugepaged_thread =3D 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=E2=80=AFAM David Hildenbrand (Arm)
<[email protected]> 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 th=
e
> > guard macro. This makes the code more readable (removing a goto stateme=
nt)
> > 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]>
> > ---
> > 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 =3D 0;
> > -
> > -     mutex_lock(&khugepaged_mutex);
> > +     guard(mutex)(&khugepaged_mutex);
> >       if (hugepage_enabled()) {
> >               if (!khugepaged_thread)
> >                       khugepaged_thread =3D kthread_run(khugepaged, NUL=
L,
> >                                                       "khugepaged");
> >               if (IS_ERR(khugepaged_thread)) {
> > +                     int err;
> >                       pr_err("khugepaged: kthread_run(khugepaged) faile=
d\n");
> >                       err =3D PTR_ERR(khugepaged_thread);
> >                       khugepaged_thread =3D NULL;
> > -                     goto fail;
> > +                     return err;
> >               }
>
> While at it, can we just do
>
> struct task_struct *new_thread =3D kthread_run...
>
> if (IS_ERR(new_thread)) {
>         pr_err("khugepaged: kthread_run(khugepaged) failed\n");
>         return PTR_ERR(new_thread);
> }
> khugepaged_thread =3D new_thread;
>
> ?
>
> Avoid the suboptimal err variable and temporarily assigning error values =
to the
> global variable.
>
> --
> Cheers,
>
> David