mm/slub: fix memory leak in free_to_pcs_bulk()

The commit 989b09b73978 ("slab: skip percpu sheaves for remote object
freeing") introduced the remote_objects array in free_to_pcs_bulk() to
skip sheaves when objects from a remote node are freed.

However, the array is flushed only when:
1) the array becomes full (++remote_nr >= PCS_BATCH_MAX), or
2) slab_free_hook() returns false and size becomes zero.

When neither of the conditions is met, objects in the array are leaked.
This resulted in a memory leak [1], where 82 GiB of memory was allocated
for the maple_node cache.

Flush the array after successfully freeing objects to sheaves
in the do_free: path.

In the meantime, move the snippet if (!size) goto flush_remote; outside
the while loop for readability. Let's say all objects in the array are
from a remote node: then we acquire s->cpu_sheaves->lock and try to free
an object even when size is zero. This doesn't appear to be harmful,
but isn't really readable.

Reported-by: Tytus Rogalewski <admin@simplepod.ai>
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=220765 [1]
Closes: https://lore.kernel.org/linux-mm/20251107094809.12e9d705b7bf4815783eb184@linux-foundation.org
Closes: https://lore.kernel.org/all/aRGDTwbt2EIz2CYn@hyeyoo
Fixes: 989b09b73978 ("slab: skip percpu sheaves for remote object freeing")
Signed-off-by: Harry Yoo <harry.yoo@oracle.com>
Link: https://patch.msgid.link/20251111125331.12246-1-harry.yoo@oracle.com
Acked-by: Liam R. Howlett <Liam.Howlett@oracle.com>
Tested-by: Darrick J. Wong <djwong@kernel.org>
Tested-by: Tytus Rogalewski <admin@simplepod.ai>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>

authored by Harry Yoo and committed by Vlastimil Babka cbcff934 c379b745

+6 -2
+6 -2
mm/slub.c
··· 6332 6333 if (unlikely(!slab_free_hook(s, p[i], init, false))) { 6334 p[i] = p[--size]; 6335 - if (!size) 6336 - goto flush_remote; 6337 continue; 6338 } 6339 ··· 6345 6346 i++; 6347 } 6348 6349 next_batch: 6350 if (!local_trylock(&s->cpu_sheaves->lock)) ··· 6402 size -= batch; 6403 goto next_batch; 6404 } 6405 6406 return; 6407
··· 6332 6333 if (unlikely(!slab_free_hook(s, p[i], init, false))) { 6334 p[i] = p[--size]; 6335 continue; 6336 } 6337 ··· 6347 6348 i++; 6349 } 6350 + 6351 + if (!size) 6352 + goto flush_remote; 6353 6354 next_batch: 6355 if (!local_trylock(&s->cpu_sheaves->lock)) ··· 6401 size -= batch; 6402 goto next_batch; 6403 } 6404 + 6405 + if (remote_nr) 6406 + goto flush_remote; 6407 6408 return; 6409