From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69ECD3F411A for ; Thu, 14 May 2026 01:12:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778721143; cv=none; b=MgED3FzpFLKHoIw1INa/AgDDkOVu2FrEOgAbttXHoWWMSLvfPdBZ2DLPbOd22F38W3kSFaO4/OCPYzfJpwHeR+3pRF81C0/WtV0OW1TMEllQqkqEHeQLAyxuHeP7+hALWLIl6LU8dLewF8EbIg9e45Mecn/DGDwGMjB/XUjFouc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778721143; c=relaxed/simple; bh=I5BsO9LjXXTko10w91/UPAFRg7TLDcOecDFgCr7EIFg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=grHteW827/r1vt8nJ3pEZIKSWtjKbpTK+lo7WuPqPYrdynfp2Vst7iUBrxgHFqunJisVkq/ceCPoAeXEIDW4DJeyhkJsw7mYJhe8wetXqS5OLdRRSAVFgZTrsYtwsrNQkwNRQbimfCObGsw1UX4+NSGzMs5M5/Rsul6z8qIitZs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=a2tSNKal; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="a2tSNKal" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E5A84C19425; Thu, 14 May 2026 01:12:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778721143; bh=I5BsO9LjXXTko10w91/UPAFRg7TLDcOecDFgCr7EIFg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a2tSNKalpud4RGoSM7eHWLelKomAhV7IwcV/EId7G3IgEwbaYa4ApHQjZSAN/ZLFX lz8524xu0VUwEhWJbHi16N8VXZ16llXXz3EQQozpLHeKkvR3kwdmUvAtlQI7ZZBdDF xu7V4pRClzKIOBYBq8hCl/EVKdyGsZe5whnDEziV5YJv5mPJwBQBISev9kGrYLGMoB ZtQeUF8YJn0JiB0mm0ZwuTi5eOiL8QPcW3qtVk/H+M0h95lfI19+Hecvi+lQxwjaCz gWzEJKS9JmF/XzDAQT1g+LeFgHGHHgzFCtz6SwhtYlLPvFSa5QXZdAjnbqc5ju3AgG 7jm/E5YclT/EA== From: SeongJae Park To: wangxuewen <18810879172@163.com> Cc: SeongJae Park , 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 Subject: Re: [PATCH v2] mm/shrinker: simplify shrinker_memcg_alloc() using guard() Date: Wed, 13 May 2026 18:12:06 -0700 Message-ID: <20260514011207.148756-1-sj@kernel.org> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260513075214.2655710-1-18810879172@163.com> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 > --- > 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 [...]