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 E5B963FB7D5 for ; Fri, 10 Jul 2026 10:02:15 +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=1783677737; cv=none; b=G9M+zPlZyb9HQ6fDx6INa9GP9FJ39bIx7XhVS8hudI/222h7socTjcBO4jzOwoSdJ/BZZ6DASK55OtCXNxTejWxZtK5h6EDmE+r461SjpxRGN3cooZ0paBQ0PqGE7Yc2bKnQm5+MTeyXBQmCvLFhCPe9VgafVNA5ACtR4MSziSc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783677737; c=relaxed/simple; bh=PtRnoz/mJCDZVjxB9h4OO9RjLcGJW9idzsTGqQlpMq8=; h=Message-ID:Date:MIME-Version:From:Subject:To:Cc:References: In-Reply-To:Content-Type; b=PIZWXJyjhiLtHp4/gWNL0Fy6XjKyKVaQ6Xse/HY19byr1JHhiGFtFtM6lC0nPSuZCAIfVEjvnLGBXyDK0xpFveuqdBsB9O44GRKrIKkFvnk0vlR5A7iC9Ztz8MGYMXDXRQuguokF08DX0gSjpPu9WuE7hq7cSLDmk+BfHHmG16c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NStmKb/t; 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="NStmKb/t" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BB50D1F000E9; Fri, 10 Jul 2026 10:02:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1783677735; bh=Wd3z5gi9ZC47QyTFTl0PrNt/+R38+SG2Pwn5cKGSZoA=; h=Date:From:Subject:To:Cc:References:In-Reply-To; b=NStmKb/tZbEEPMPVygxPhonVPyi4RDn5Kix9Z4adjj21L9u7s9esvhbDWTiluRbL5 34bskCAwb7NWKzagyv3PRLwpCTl9Th8JsRM5b27bJkJYZAGXF3cmPeTr0L/V2DkO+A 1lS7CBGBF2XwBxyrU97dcS5JRonyJb1trEuhQ0dw6bulrZiFqXkmOAsY+QKW3jLbjq MzQP+gGhvDaVAehKoZWp6nGf7KSwWBvD4S41VwKcBvoeF7QFc+6nXqbSWLqNKD4S2C Cn3kF44klvJCdWtW6SVHjjrXh+VT+PJjUxBUCGHHlMCY0tkXGCpo+0DSiP3guF9PNk XuF4RvjDnkr2w== Message-ID: <5d1bb55e-7dad-4013-9c72-5a43051057d6@kernel.org> Date: Fri, 10 Jul 2026 19:02:09 +0900 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Harry Yoo Subject: Re: [PATCH] mm/slab: extract __free_to_pcs_batch() from free_to_pcs_bulk() To: "Vlastimil Babka (SUSE)" , Hao Li Cc: Andrew Morton , Shengming Hu , Christoph Lameter , David Rientjes , Roman Gushchin , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20260707-slab-simplify-bulk-pcs-v1-1-4850dbe0d904@kernel.org> Content-Language: en-US In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="------------0iZRncWzsyBwJEE9LOW9wmSR" This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --------------0iZRncWzsyBwJEE9LOW9wmSR Content-Type: multipart/mixed; boundary="------------32Spjo3a2GL1OUJCFYd7q0sU"; protected-headers="v1" From: Harry Yoo To: "Vlastimil Babka (SUSE)" , Hao Li Cc: Andrew Morton , Shengming Hu , Christoph Lameter , David Rientjes , Roman Gushchin , linux-mm@kvack.org, linux-kernel@vger.kernel.org Message-ID: <5d1bb55e-7dad-4013-9c72-5a43051057d6@kernel.org> Subject: Re: [PATCH] mm/slab: extract __free_to_pcs_batch() from free_to_pcs_bulk() References: <20260707-slab-simplify-bulk-pcs-v1-1-4850dbe0d904@kernel.org> In-Reply-To: --------------32Spjo3a2GL1OUJCFYd7q0sU Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 7/10/26 6:54 PM, Vlastimil Babka (SUSE) wrote: > On 7/10/26 07:21, Hao Li wrote: >> On Fri, Jul 10, 2026 at 01:18:25PM +0800, Hao Li wrote: >>> On Tue, Jul 07, 2026 at 02:16:00PM +0200, Vlastimil Babka (SUSE) wrot= e: >>>> It has been noted that free_to_pcs_bulk() is difficult to follow, wi= th a >>>> number of goto labels, and this has contributed to two memory leak b= ugs >>>> in there. >>>> >>>> Extract part of the code to __free_to_pcs_batch(), which focuses onl= y on >>>> freeing free-hook-processed local objects to a percpu sheaf, and >>>> returning how many were freed. Zero means a trylock failure or no em= pty >>>> sheaf available, and thus the caller should fallback to >>>> __kmem_cache_free_bulk(). >>>> >>>> Make free_to_pcs_bulk() call this in a while loop, removing all goto= >>>> labels from the function. __free_to_pcs_batch() retains two rather >>>> straightforward ones. >>>> >>>> Signed-off-by: Vlastimil Babka (SUSE) >>> >>> Nice simplification! >>> >>> [...] >>>> +/* >>>> + * Bulk free objects to the percpu sheaves. >>>> + * Unlike free_to_pcs() this includes the calls to all necessary ho= oks >>>> + * and the fallback to freeing to slab pages. >>>> + */ >>>> +static void free_to_pcs_bulk(struct kmem_cache *s, size_t size, voi= d **p) >>>> +{ >>>> + bool init =3D slab_want_init_on_free(s); >>>> + void *remote_objects[PCS_BATCH_MAX]; >>>> + unsigned int remote_nr =3D 0; >>>> + >>>> + for (unsigned int i =3D 0; i < size;) { >>>> + struct slab *slab =3D virt_to_slab(p[i]); >>>> + >>>> + memcg_slab_free_hook(s, slab, p + i, 1); >>>> + alloc_tagging_slab_free_hook(s, slab, p + i, 1); >>>> + >>>> + if (unlikely(!slab_free_hook(s, p[i], init, false))) { >>>> + p[i] =3D p[--size]; >>>> + continue; >>>> + } >>>> + >>>> + if (unlikely(!can_free_to_pcs(slab))) { >>>> + remote_objects[remote_nr] =3D p[i]; >>>> + p[i] =3D p[--size]; >>>> + if (++remote_nr >=3D PCS_BATCH_MAX) { >>>> + __kmem_cache_free_bulk(s, remote_nr, &remote_objects[0]); >>>> + stat_add(s, FREE_SLOWPATH, remote_nr); >>>> + remote_nr =3D 0; >>>> + } >>>> + continue; >>>> + } >>>> + >>>> + i++; >>>> } >>>> =20 >>>> - if (remote_nr) >>>> - goto flush_remote; >>>> + while (size) { >>>> + unsigned int batch_freed =3D __free_to_pcs_batch(s, size, p); >>>> =20 >>>> - return; >>>> + if (!batch_freed) >>>> + break; >>>> =20 >>>> -no_empty: >>>> - local_unlock(&s->cpu_sheaves->lock); >>>> + p +=3D batch_freed; >>>> + size -=3D batch_freed; >>>> + } >>>> =20 >>>> - /* >>>> - * if we depleted all empty sheaves in the barn or there are too >>>> - * many full sheaves, free the rest to slab pages >>>> - */ >>>> -fallback: >>>> - __kmem_cache_free_bulk(s, size, p); >>>> - stat_add(s, FREE_SLOWPATH, size); >>>> + if (size) { >>>> + __kmem_cache_free_bulk(s, size, p); >>>> + stat_add(s, FREE_SLOWPATH, size); >>>> + } >>> >>> By the way, could we move this directly into the loop inside the >>> `if (!batch_freed)` block to make it a bit more concise? but it's tot= ally fine >>> to leave it as is! :) >=20 > Ah right, great suggestion! will do. Including the suggested change: Reviewed-by: Harry Yoo (Oracle) --=20 Cheers, Harry / Hyeonggon --------------32Spjo3a2GL1OUJCFYd7q0sU-- --------------0iZRncWzsyBwJEE9LOW9wmSR Content-Type: application/pgp-signature; name="OpenPGP_signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="OpenPGP_signature.asc" -----BEGIN PGP SIGNATURE----- iHUEARYKAB0WIQQQ1ub6gR5ogjaKRmOGXBN6rc5S1gUCalDDIQAKCRCGXBN6rc5S 1ppCAP0dTWmRJEYiuWr7o3WH3S+iVpYgFhgA5Z0t5uQu3BB2LwEAnH7y7Js/OXM5 lP/HFvFp2ebYjuBUAC9VWpgBdpOWGAw= =XzdP -----END PGP SIGNATURE----- --------------0iZRncWzsyBwJEE9LOW9wmSR--