Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: SeongJae Park <sj@kernel.org>
To: wangxuewen <18810879172@163.com>
Cc: SeongJae Park <sj@kernel.org>,
	akpm@linux-foundation.org, david@fromorbit.com,
	qi.zheng@linux.dev, roman.gushchin@linux.dev,
	muchun.song@linux.dev, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, wangxuewen <wangxuewen@kylinos.cn>
Subject: Re: [PATCH v2] mm/shrinker: simplify shrinker_memcg_alloc() using guard()
Date: Wed, 13 May 2026 18:12:06 -0700	[thread overview]
Message-ID: <20260514011207.148756-1-sj@kernel.org> (raw)
In-Reply-To: <20260513075214.2655710-1-18810879172@163.com>

On Wed, 13 May 2026 15:52:14 +0800 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.
> 
> v2:
> - Drop unused 'ret' variable to avoid compiler warning (Muchun Song).

Patch changelog should go to commentary area [1].  Also adding links to
previous versions would be helpful.

> 
> Signed-off-by: wangxuewen <wangxuewen@kylinos.cn>
> ---
>  mm/shrinker.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/mm/shrinker.c b/mm/shrinker.c
> index 76b3f750cf65..cb03fbecc75d 100644
> --- a/mm/shrinker.c
> +++ b/mm/shrinker.c
> @@ -215,29 +215,26 @@ static DEFINE_IDR(shrinker_idr);
>  
>  static int shrinker_memcg_alloc(struct shrinker *shrinker)
>  {
> -	int id, ret = -ENOMEM;
> +	int id;
>  
>  	if (mem_cgroup_disabled())
>  		return -ENOSYS;
>  	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;

Could this change the return value?  This code was always returning -ENOMEM
before, but seems it can now return -ENOSPC?  Seems that doesn't matter,
though.

>  
>  	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;
> +	return 0;
>  }

[1] https://docs.kernel.org/process/submitting-patches.html#commentary


Thanks,
SJ

[...]


      parent reply	other threads:[~2026-05-14  1:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-13  7:52 [PATCH v2] mm/shrinker: simplify shrinker_memcg_alloc() using guard() wangxuewen
2026-05-13  7:55 ` Muchun Song
2026-05-14  1:12 ` SeongJae Park [this message]

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=20260514011207.148756-1-sj@kernel.org \
    --to=sj@kernel.org \
    --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=muchun.song@linux.dev \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox