* [PATCH 0/2] xarray: free unused spare node after range creation @ 2026-11-15 3:16 Andrew Morton 2026-11-15 3:16 ` [PATCH 1/2] lib: xarray: free unused spare node in xas_create_range() Andrew Morton 2026-11-15 3:16 ` [PATCH 2/2] lib/test_xarray: check unused spare node after range creation Andrew Morton 0 siblings, 2 replies; 5+ messages in thread From: Andrew Morton @ 2026-11-15 3:16 UTC (permalink / raw) To: Matthew Wilcox (Oracle) Cc: linux-mm, Andrew Morton, Shardul Bankar, David Hildenbrand (ARM), Dev Jain, janak, Jinjiang Tu xas_nomem() can leave a spare xa_node in an XA state when another user populates the tree before xas_create_range() retries. Free an unused spare at the end of range creation, and add a deterministic lib/test_xarray check for that path. The fix is first and the test is second. The test patch was held on both sides of a kernel A/B run while only the fix was removed and restored. Without the fix, its spare-node assertion failed (158825705 of 158825706 checks passed). With the fix, all 158825706 checks passed. Both runs used the same verified guest test script. Shardul Bankar (1): lib: xarray: free unused spare node in xas_create_range() Andrew Morton (1): lib/test_xarray: check unused spare node after range creation lib/test_xarray.c | 31 +++++++++++++++++++++++++++++++ lib/xarray.c | 7 ++++++- 2 files changed, 37 insertions(+), 1 deletion(-) ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2] lib: xarray: free unused spare node in xas_create_range() 2026-11-15 3:16 [PATCH 0/2] xarray: free unused spare node after range creation Andrew Morton @ 2026-11-15 3:16 ` Andrew Morton 2026-11-16 3:17 ` sashiko-bot 2026-11-15 3:16 ` [PATCH 2/2] lib/test_xarray: check unused spare node after range creation Andrew Morton 1 sibling, 1 reply; 5+ messages in thread From: Andrew Morton @ 2026-11-15 3:16 UTC (permalink / raw) To: Matthew Wilcox (Oracle) Cc: linux-mm, Andrew Morton, Shardul Bankar, David Hildenbrand (ARM), Dev Jain, janak, Jinjiang Tu From: Shardul Bankar <shardul.b@mpiricsoftware.com> xas_create_range() is typically called in a retry loop that uses xas_nomem() to handle -ENOMEM errors. xas_nomem() may allocate a spare xa_node and store it in xas->xa_alloc for use in the retry. If the lock is dropped after xas_nomem(), another thread can expand the xarray tree in the meantime. On the next retry, xas_create_range() can then succeed without consuming the spare node stored in xas->xa_alloc. If the function returns without freeing this spare node, it leaks. xas_create_range() calls xas_create() multiple times in a loop for different index ranges. A spare node that isn't needed for one range iteration might be needed for the next, so we cannot free it after each xas_create() call. We can only safely free it after xas_create_range() completes. Fix this by calling xas_destroy() at the end of xas_create_range() to free any unused spare node. This makes the API safer by default and prevents callers from needing to remember cleanup. This fixes a memory leak in mm/khugepaged.c and potentially other callers that use xas_nomem() with xas_create_range(). Link: https://lore.kernel.org/20251204142625.1763372-1-shardul.b@mpiricsoftware.com Link: https://syzkaller.appspot.com/bug?id=a274d65fc733448ed518ad15481ed575669dd98c Link: https://lore.kernel.org/all/20251201074540.3576327-1-shardul.b@mpiricsoftware.com/ ("v3") Fixes: cae106dd67b9 ("mm/khugepaged: refactor collapse_file control flow") Signed-off-by: Shardul Bankar <shardul.b@mpiricsoftware.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: David Hildenbrand (ARM) <david@kernel.org> Tested-by: Andrew Morton <akpm@linux-foundation.org> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Dev Jain <dev.jain@arm.com> Cc: <janak@mpiricsoftware.com> Cc: Jinjiang Tu <tujinjiang@huawei.com> lib/xarray.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) --- a/lib/xarray.c~lib-xarray-free-unused-spare-node-in-xas_create_range +++ a/lib/xarray.c @@ -745,11 +745,16 @@ restore: xas->xa_shift = shift; xas->xa_sibs = sibs; xas->xa_index = index; - return; + goto cleanup; + success: xas->xa_index = index; if (xas->xa_node) xas_set_offset(xas); + +cleanup: + /* Free any unused spare node from xas_nomem() */ + xas_destroy(xas); } EXPORT_SYMBOL_GPL(xas_create_range); _ ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] lib: xarray: free unused spare node in xas_create_range() 2026-11-15 3:16 ` [PATCH 1/2] lib: xarray: free unused spare node in xas_create_range() Andrew Morton @ 2026-11-16 3:17 ` sashiko-bot 2026-11-16 3:59 ` Andrew Morton 0 siblings, 1 reply; 5+ messages in thread From: sashiko-bot @ 2026-11-16 3:17 UTC (permalink / raw) To: Andrew Morton, Matthew Wilcox (Oracle) Cc: linux-mm, Shardul Bankar, David Hildenbrand (ARM), Dev Jain, janak, Jinjiang Tu Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] lib: xarray: inaccurate Fixes trailer in xas_create_range() commit -- commit 844089b2ad8c28a781a9ee223d1c1eac3cd107f2 Author: Shardul Bankar <shardul.b@mpiricsoftware.com> > lib: xarray: free unused spare node in xas_create_range() > > xas_create_range() is typically called in a retry loop that uses > xas_nomem() to handle -ENOMEM errors. xas_nomem() may allocate a spare > xa_node and store it in xas->xa_alloc for use in the retry. > > If the lock is dropped after xas_nomem(), another thread can expand the > xarray tree in the meantime. On the next retry, xas_create_range() can > then succeed without consuming the spare node stored in xas->xa_alloc. If > the function returns without freeing this spare node, it leaks. > > xas_create_range() calls xas_create() multiple times in a loop for > different index ranges. A spare node that isn't needed for one range > iteration might be needed for the next, so we cannot free it after each > xas_create() call. We can only safely free it after xas_create_range() > completes. > > Fix this by calling xas_destroy() at the end of xas_create_range() to free > any unused spare node. This makes the API safer by default and prevents > callers from needing to remember cleanup. > > This fixes a memory leak in mm/khugepaged.c and potentially other callers > that use xas_nomem() with xas_create_range(). > > Link: https://lore.kernel.org/20251204142625.1763372-1-shardul.b@mpiricsoftware.com > Link: https://syzkaller.appspot.com/bug?id=a274d65fc733448ed518ad15481ed575669dd98c > Link: https://lore.kernel.org/all/20251201074540.3576327-1-shardul.b@mpiricsoftware.com/ ("v3") > Fixes: cae106dd67b9 ("mm/khugepaged: refactor collapse_file control flow") > Signed-off-by: Shardul Bankar <shardul.b@mpiricsoftware.com> > Signed-off-by: Andrew Morton <akpm@linux-foundation.org> > Reviewed-by: David Hildenbrand (ARM) <david@kernel.org> > Tested-by: Andrew Morton <akpm@linux-foundation.org> > Cc: Matthew Wilcox (Oracle) <willy@infradead.org> > Cc: Dev Jain <dev.jain@arm.com> > Cc: <janak@mpiricsoftware.com> > Cc: Jinjiang Tu <tujinjiang@huawei.com> [Severity: Low] Is the Fixes tag pointing to the right commit? cae106dd67b9 ("mm/khugepaged: refactor collapse_file control flow") only moved __SetPageLocked(hpage) before the xas_create_range() loop and changed the !xas_nomem() failure target from out to rollback, where xas.xa_alloc is already NULL. xas_create_range() was introduced without xas_destroy() in 2264f5132fe4 ("xarray: Add xas_create_range"), and the retry loop in collapse_file() that breaks out on !xas_error(&xas) without freeing xas.xa_alloc dates back to 77da9389b9d5 ("mm: Convert collapse_shmem to XArray"). -- Sashiko AI review · https://sashiko.dev/#/patchset/2dfabfd9476542e39c7d5473a6597208.0@linux-foundation.org?part=1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] lib: xarray: free unused spare node in xas_create_range() 2026-11-16 3:17 ` sashiko-bot @ 2026-11-16 3:59 ` Andrew Morton 0 siblings, 0 replies; 5+ messages in thread From: Andrew Morton @ 2026-11-16 3:59 UTC (permalink / raw) To: sashiko-reviews Cc: sashiko-bot, Matthew Wilcox, linux-mm, Shardul Bankar, David Hildenbrand, Dev Jain, janak, Jinjiang Tu On Mon, 16 Nov 2026 03:17:13 +0000 sashiko-bot@kernel.org wrote: > [ nag nag nag ] bah. > [Severity: Low] > Is the Fixes tag pointing to the right commit? OK, let's make it Fixes: 77da9389b9d5 ("mm: Convert collapse_shmem to XArray") it doesn't have cc:stable anyway, but afaik lib/ is subject to -stable's blanket "backport everything which has Fixes:" rule. So I'll also add Cc: <stable+noautosel@kernel.org> # not harmful enough to bother unless people disagree? ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] lib/test_xarray: check unused spare node after range creation 2026-11-15 3:16 [PATCH 0/2] xarray: free unused spare node after range creation Andrew Morton 2026-11-15 3:16 ` [PATCH 1/2] lib: xarray: free unused spare node in xas_create_range() Andrew Morton @ 2026-11-15 3:16 ` Andrew Morton 1 sibling, 0 replies; 5+ messages in thread From: Andrew Morton @ 2026-11-15 3:16 UTC (permalink / raw) To: Matthew Wilcox (Oracle) Cc: linux-mm, Andrew Morton, Shardul Bankar, David Hildenbrand (ARM), Dev Jain, janak, Jinjiang Tu Shardul Bankar's "[PATCH v4] lib: xarray: free unused spare node in xas_create_range()" [1] releases a spare node left over after range creation. Exercise the case without depending on allocator failures or timing: let xas_nomem() reserve a node, then populate the relevant XArray node through a separate operation before xas_create_range() retries. Check that the retry succeeds and releases the unused spare node. Link: https://lore.kernel.org/r/20251204142625.1763372-1-shardul.b@mpiricsoftware.com [1] Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Cc: Shardul Bankar <shardul.b@mpiricsoftware.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: David Hildenbrand (ARM) <david@kernel.org> Cc: Dev Jain <dev.jain@arm.com> Cc: <janak@mpiricsoftware.com> Cc: Jinjiang Tu <tujinjiang@huawei.com> lib/test_xarray.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) --- a/lib/test_xarray.c~lib-test_xarray-check-unused-spare-node-after-range-creation +++ a/lib/test_xarray.c @@ -1834,6 +1834,34 @@ static noinline void check_create_range_ xa_destroy(xa); } +#ifdef CONFIG_XARRAY_MULTI +static noinline void check_create_range_spare(struct xarray *xa) +{ + XA_STATE_ORDER(xas, xa, 0, XA_CHUNK_SHIFT); + bool spare_left; + + XA_BUG_ON(xa, !xa_empty(xa)); + + /* Model a failed allocation followed by a successful xas_nomem(). */ + xas_set_err(&xas, -ENOMEM); + XA_BUG_ON(xa, !xas_nomem(&xas, GFP_KERNEL)); + + /* Model another user populating the tree before the retry. */ + xa_insert_index(xa, 0); + xa_insert_index(xa, 1); + + xas_lock(&xas); + xas_create_range(&xas); + xas_unlock(&xas); + + XA_BUG_ON(xa, xas_error(&xas)); + spare_left = xas.xa_alloc != NULL; + xas_destroy(&xas); + XA_BUG_ON(xa, spare_left); + xa_destroy(xa); +} +#endif + static noinline void check_create_range(struct xarray *xa) { unsigned int order; @@ -1867,6 +1895,9 @@ static noinline void check_create_range( } check_create_range_3(); +#ifdef CONFIG_XARRAY_MULTI + check_create_range_spare(xa); +#endif } static noinline void __check_store_range(struct xarray *xa, unsigned long first, _ ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-16 3:59 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-15 3:16 [PATCH 0/2] xarray: free unused spare node after range creation Andrew Morton 2026-11-15 3:16 ` [PATCH 1/2] lib: xarray: free unused spare node in xas_create_range() Andrew Morton 2026-11-16 3:17 ` sashiko-bot 2026-11-16 3:59 ` Andrew Morton 2026-11-15 3:16 ` [PATCH 2/2] lib/test_xarray: check unused spare node after range creation Andrew Morton
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.