xarray: free unused spare node after range creation
Subsystems (3)
Andrew Morton
maintainercover xarray: free unused spare node after range creation
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(-)
Andrew Morton
maintainerPATCH 1/2 lib: xarray: free unused spare node in xas_create_range()
From: Shardul Bankar <[email protected]>
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().
lib/xarray.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
lib/xarray.c renamed from lib/xarray.c~lib-xarray-free-unused-spare-node-in-xas_create_range +6 -1
@@ -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);
_
[email protected]
AI reviewRe: [PATCH 1/2] lib: xarray: free unused spare node in xas_create_range()
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 --
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 <[email protected]>
Quoted 24 lines
lib: xarray: free unused spare node in
xas_create_range()
xas_create_range()is typically called in a retry loop that usesxas_nomem()to handle-ENOMEMerrors.xas_nomem()may allocate a sparexa_nodeand store it inxas->xa_allocfor 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 inxas->xa_alloc. If the function returns without freeing this spare node, it leaks.
xas_create_range()callsxas_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 eachxas_create()call. We can only safely free it afterxas_create_range()completes.Fix this by calling
xas_destroy()at the end ofxas_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.cand potentially other callers that usexas_nomem()withxas_create_range().
Quoted 12 lines
Fixes cae106dd67b9 ("mm/khugepaged: refactor collapse_file control flow")Signed-off-by Shardul Bankar <[email protected]>Signed-off-by Andrew Morton <[email protected]>Reviewed-by David Hildenbrand (ARM) <[email protected]>Tested-by Andrew Morton <[email protected]>Cc Matthew Wilcox (Oracle) <[email protected]>Cc Dev Jain <[email protected]>Cc <[email protected]>Cc Jinjiang Tu <[email protected]>
[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").
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Andrew Morton
maintainerRe: [PATCH 1/2] lib: xarray: free unused spare node in xas_create_range()
Quoted 1 line · On Mon, 16 Nov 2026 03:17:13 +0000 [email protected] wrote:
[ nag nag nag ]
bah.
Quoted 2 lines
[Severity: Low]
Is the Fixes tag pointing to the right commit?
OK, let's make it
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
unless people disagree?
Andrew Morton
maintainerPATCH 2/2 lib/test_xarray: check unused spare node after range creation
In reply to Andrew MortonShardul 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.
lib/test_xarray.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
lib/test_xarray.c renamed from lib/test_xarray.c~lib-test_xarray-check-unused-spare-node-after-range-creation +31 -0
@@ -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,
_