All of lore.kernel.org
 help / color / mirror / Atom feed
From: Muchun Song <muchun.song@linux.dev>
To: wangxuewen <18810879172@163.com>
Cc: akpm@linux-foundation.org, david@fromorbit.com,
	qi.zheng@linux.dev, roman.gushchin@linux.dev, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, wangxuewen <wangxuewen@kylinos.cn>
Subject: Re: [PATCH v1] mm/shrinker: simplify shrinker_memcg_alloc() using guard()
Date: Tue, 12 May 2026 17:10:03 +0800	[thread overview]
Message-ID: <616E5E26-BB36-43CD-B086-0F238903B1BF@linux.dev> (raw)
In-Reply-To: <20260512085546.368911-1-18810879172@163.com>



> On May 12, 2026, at 16:55, wangxuewen <18810879172@163.com> wrote:
> 
> Use guard(mutex) to automatically handle shrinker_mutex locking and
> unlocking in shrinker_memcg_alloc(). This removes the explicit
> mutex_unlock() call, the goto-based error path, and the redundant
> ret variable, resulting in cleaner and more concise code.
> 
> Signed-off-by: wangxuewen <wangxuewen@kylinos.cn>
> ---
> mm/shrinker.c | 11 ++++-------
> 1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/mm/shrinker.c b/mm/shrinker.c
> index 76b3f750cf65..1274130323bf 100644
> --- a/mm/shrinker.c
> +++ b/mm/shrinker.c
> @@ -222,22 +222,19 @@ static int shrinker_memcg_alloc(struct shrinker *shrinker)
> if (mem_cgroup_kmem_disabled() && !(shrinker->flags & SHRINKER_NONSLAB))
> return -ENOSYS;
> 
> - 	mutex_lock(&shrinker_mutex);
> + 	guard(mutex)(&shrinker_mutex);
> 	id = idr_alloc(&shrinker_idr, shrinker, 0, 0, GFP_KERNEL);
> 	if (id < 0)
> - 		goto unlock;
> + 	return id;
> 
> 	if (id >= shrinker_nr_max) {
> 		if (expand_shrinker_info(id)) {
> 			idr_remove(&shrinker_idr, id);
> - 			goto unlock;
> + 			return -ENOMEM;
> 		}
> 	}
> 	shrinker->id = id;
> - 	ret = 0;
> -unlock:
> - 	mutex_unlock(&shrinker_mutex);
> - 	return ret;

One small thing: since ret is no longer used after this change,
it should be dropped from the declaration to avoid an unused-variable
warning:

    -       int id, ret = -ENOMEM;
    +       int id;

Otherwise looks good to me.

Thanks,
Muchun

> + 	return 0;
> }
> 
> static void shrinker_memcg_remove(struct shrinker *shrinker)
> -- 
> 2.25.1
> 



  reply	other threads:[~2026-05-12  9:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-12  8:55 [PATCH v1] mm/shrinker: simplify shrinker_memcg_alloc() using guard() wangxuewen
2026-05-12  9:10 ` Muchun Song [this message]
2026-05-13  7:55   ` wangxuewen
2026-05-12  9:12 ` Qi Zheng
2026-05-15  3:15 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=616E5E26-BB36-43CD-B086-0F238903B1BF@linux.dev \
    --to=muchun.song@linux.dev \
    --cc=18810879172@163.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@fromorbit.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=qi.zheng@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=wangxuewen@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.