From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 0AED4420874 for ; Tue, 7 Jul 2026 09:27:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783416436; cv=none; b=lVzdDkQtichvBvw6Wrg7RE2SbbzaDYAG2eInUzvW8ql/DtBFWQYxT02xawFAuswLKB99EDbDIpVWUQ8eLXTkWXZ6fPyTReVPJWDzC0Jxy7vGWSfc1Tp8nYpR6tiUt87D0PlIDea1+5kQS3H4waG/eyBLM9LLmV218qsyEXoAlkw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783416436; c=relaxed/simple; bh=gOQ2HA9UUrMWU6nczbPc/DzS6ViRak4ZB77YpwsFEWg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=i7Eb7i11gBE/gboM2NLFXFFTf9tLI6/6Sl3IXMxUz65oY5+aLz0go/wZxWhhSMlKJs1mebK8k2VM3XsgvUheI1JB/c/rVB90CzhVtXKAlTqJ2vZQUFH1wxQCFnbJrAdEpB95226HDWBPrtJlWl8RsVTpXtWD2X1hT+l8ZIJ+H0Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gWOo6k3R; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gWOo6k3R" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 996591F00A3A; Tue, 7 Jul 2026 09:27:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1783416434; bh=Xyxzpiw3OM9UElk+MAeq0fkOAFAIG6DAhu3VfVnuM6I=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=gWOo6k3R34w9fw1RtN344zs/rlFr56g9/JuFLeuvFXmkLRS3YZFcB1AToHyj0dTTW PZffp/rgtQJPiJkwIVTtEjKQ1XNw9ZG9LHGTTG7MQZ5MmlS9dpCMuNiIrjVPPL5NV2 xN1vz7p5mcxYGbncfuT3GfQv0lJoitPFvCy45WuGJ4tTZn1jpypDERmwq+NaCc/xEx XfZSlvdLziTtvDRtR5awfg+iBfirteTy6H4/0fT3qJ4dTGsVfQp032G5SX0nKNkHdQ wlIOyCusSe7BGlaMFd+yTmXEtLgOuw78+k9dsyuvS/AA499nIDc9JtbRaYm9a8zO4G iXzmSftTKyAZw== Message-ID: <84c4c2bf-397e-461c-b898-482d1a49f40b@kernel.org> Date: Tue, 7 Jul 2026 11:27:10 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] mm/slub: fix lost local objects when bulk remote free batch fills To: hu.shengming@zte.com.cn, harry@kernel.org, akpm@linux-foundation.org Cc: hao.li@linux.dev, cl@gentwo.org, rientjes@google.com, roman.gushchin@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhang.run@zte.com.cn, cai.qu@zte.com.cn References: <202607062139095043SOsLi6TIf403tcjPf8fm@zte.com.cn> From: "Vlastimil Babka (SUSE)" Content-Language: en-US Autocrypt: addr=vbabka@kernel.org; keydata= xsFNBFZdmxYBEADsw/SiUSjB0dM+vSh95UkgcHjzEVBlby/Fg+g42O7LAEkCYXi/vvq31JTB KxRWDHX0R2tgpFDXHnzZcQywawu8eSq0LxzxFNYMvtB7sV1pxYwej2qx9B75qW2plBs+7+YB 87tMFA+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vqYapVbZ+9KqT74FsuB0Ya9uXwgBF2Q6cRuETZk5vqaqKxzqoQZCO8AOz/58j6O 2RHNy/mZEN+7tJ5Tsq42zVJ4jxsT8b9YplavCMsnBgDeRWhcbYhCyttoL7nYISyWg4kQYZ/P wIV3OuNv2f8iKYsxNsRuClOAF82+gvqOy1/1pprFjy8uo2pkoOrb63aOP3vO5VHnRKgra6dq NcaZ+c6J4H+nEJGi2SkHAUJz5oBzuThvPudLvPA/SK8sKoM01IRxSihev/S/5WLazXB1PGem OCbvzC1IjWJJraxiDJ5IygokapUa2RP7+WBR22skQ3SSl6G107QgWKSyTOGWEaRmV53vxQLV jXuCmzSSasTL60zq5yGrT4/DYQVSNEUiUbG4pYekxJujNeEDkUlky0Y= In-Reply-To: <202607062139095043SOsLi6TIf403tcjPf8fm@zte.com.cn> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 7/6/26 15:39, hu.shengming@zte.com.cn wrote: > From: Shengming Hu > > In free_to_pcs_bulk(), when remote_objects[] fills to PCS_BATCH_MAX, > the code jumps to flush_remote to free the batch. If all remote entries > have already been compacted out of p[] via tail swaps while local objects > remain, the flush_remote path returns early since `i < size` no longer > holds. The leftover local objects are then neither cached in the sheaf > nor returned to the slab freelist, causing a memory leak. > > For illustration: > size = 64, local objects at p[0..31], remote objects at p[32..63] > After scanning all remotes: i = 32, size = 32 > p[0..31] local objects are dropped. > > Harry pointed out that, although the logic contains a real leak, it does > not appear to be triggerable with the current in-tree users. To hit this > path, at least PCS_BATCH_MAX objects, currently hardcoded to 32, need to > be collected in remote_objects[]. Looking at current kmem_cache_free_bulk() > users: > > * maple_node has sheaf_capacity = 32 > * skbuff_head_cache has sheaf_capacity = 28 > * panthor and msm drivers have sheaf_capacity = 4 > > The sheaf capacity is, at least for now, derived purely from the object > size, with the user-requested capacity used as a minimum. Therefore, among > the current users, only maple_node has a sheaf_capacity large enough to > reach PCS_BATCH_MAX. > > However, for the bug to trigger in maple_node, all objects in the sheaf > would have to be from remote nodes. In that case, there would be no local > objects left to leak. So this issue was found by code review rather than > from a runtime report, and it does not seem to be triggerable by current > users. > > Still, the bug could become reachable with future users, a different sheaf > capacity, or a change to PCS_BATCH_MAX. Fix the logic by freeing a full > remote batch in place during the scan and then continuing to process the > compacted array. This keeps all local objects on the normal fast path, > while the tail path only handles any leftover partial remote batch. The > redundant next_remote_batch jump label is removed as well. > > Fixes: <989b09b73978>("slab: skip percpu sheaves for remote object freeing") Removed the < > > Signed-off-by: Shengming Hu Thanks! I added cc: stable anyway so we avoid unexpected surprises in case something else is backported there that exposes the bug. And the fix is small enough. Merged to slab/for-next-fixes > --- > Changes in v2: > - Incorporate Harry's detailed trigger condition analysis (thanks Harry) > - Link to v1: https://lore.kernel.org/linux-mm/20260704160450302fnznThT3hBeBiwlhE-oGv@zte.com.cn/ > > --- > mm/slub.c | 12 +++++------- > 1 file changed, 5 insertions(+), 7 deletions(-) > > diff --git a/mm/slub.c b/mm/slub.c > index 9f754cf1c187..4d7e3067a5d6 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -6193,7 +6193,6 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p) > void *remote_objects[PCS_BATCH_MAX]; > unsigned int remote_nr = 0; > > -next_remote_batch: > while (i < size) { > struct slab *slab = virt_to_slab(p[i]); > > @@ -6208,8 +6207,11 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p) > if (unlikely(!can_free_to_pcs(slab))) { > remote_objects[remote_nr] = p[i]; > p[i] = p[--size]; > - if (++remote_nr >= PCS_BATCH_MAX) > - goto flush_remote; > + if (++remote_nr >= PCS_BATCH_MAX) { > + __kmem_cache_free_bulk(s, remote_nr, &remote_objects[0]); > + stat_add(s, FREE_SLOWPATH, remote_nr); > + remote_nr = 0; > + } > continue; > } > > @@ -6293,10 +6295,6 @@ static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, void **p) > if (remote_nr) { > __kmem_cache_free_bulk(s, remote_nr, &remote_objects[0]); > stat_add(s, FREE_SLOWPATH, remote_nr); > - if (i < size) { > - remote_nr = 0; > - goto next_remote_batch; > - } > } > } >