* [PATCH v1] mm/shrinker: simplify shrinker_memcg_alloc() using guard()
@ 2026-05-12 8:55 wangxuewen
2026-05-12 9:10 ` Muchun Song
2026-05-12 9:12 ` Qi Zheng
0 siblings, 2 replies; 4+ messages in thread
From: wangxuewen @ 2026-05-12 8:55 UTC (permalink / raw)
To: akpm, david, qi.zheng, roman.gushchin, muchun.song
Cc: linux-mm, linux-kernel, wangxuewen, wangxuewen
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;
+ return 0;
}
static void shrinker_memcg_remove(struct shrinker *shrinker)
--
2.25.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v1] mm/shrinker: simplify shrinker_memcg_alloc() using guard()
2026-05-12 8:55 [PATCH v1] mm/shrinker: simplify shrinker_memcg_alloc() using guard() wangxuewen
@ 2026-05-12 9:10 ` Muchun Song
2026-05-13 7:55 ` wangxuewen
2026-05-12 9:12 ` Qi Zheng
1 sibling, 1 reply; 4+ messages in thread
From: Muchun Song @ 2026-05-12 9:10 UTC (permalink / raw)
To: wangxuewen
Cc: akpm, david, qi.zheng, roman.gushchin, linux-mm, linux-kernel,
wangxuewen
> 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
>
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH v1] mm/shrinker: simplify shrinker_memcg_alloc() using guard()
2026-05-12 9:10 ` Muchun Song
@ 2026-05-13 7:55 ` wangxuewen
0 siblings, 0 replies; 4+ messages in thread
From: wangxuewen @ 2026-05-13 7:55 UTC (permalink / raw)
To: Muchun Song
Cc: akpm, david, qi.zheng, roman.gushchin, linux-mm, linux-kernel,
wangxuewen
Hi,Muchun Song,
Thanks for your review.
I've updated the patch to v2 following your suggestions.
This change is only a local cleanup for this specific function, not part
of any large-scale conversion to use the guard() mechanism.
Please review v2, thank you.
在 2026/5/12 17:10, Muchun Song 写道:
>
>
>> 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
>>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v1] mm/shrinker: simplify shrinker_memcg_alloc() using guard()
2026-05-12 8:55 [PATCH v1] mm/shrinker: simplify shrinker_memcg_alloc() using guard() wangxuewen
2026-05-12 9:10 ` Muchun Song
@ 2026-05-12 9:12 ` Qi Zheng
1 sibling, 0 replies; 4+ messages in thread
From: Qi Zheng @ 2026-05-12 9:12 UTC (permalink / raw)
To: wangxuewen, akpm, david, roman.gushchin, muchun.song
Cc: linux-mm, linux-kernel, wangxuewen
On 5/12/26 4:55 PM, wangxuewen 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.
I'm inclined to use guard() only for newly added code. If we were to
convert existing code, there would be countless places across the
kernel that need updating.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-05-13 7:55 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-12 8:55 [PATCH v1] mm/shrinker: simplify shrinker_memcg_alloc() using guard() wangxuewen
2026-05-12 9:10 ` Muchun Song
2026-05-13 7:55 ` wangxuewen
2026-05-12 9:12 ` Qi Zheng
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox