lib: xarray: free unused spare node in xas_create_range()
Subsystems (5)
PATCH v4 1/1 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().
---
v4:
- Drop redundant if (xa_alloc) around xas_destroy(), as xas_destroy() already checks xa_alloc internally.
v3:
- Move fix from collapse_file() to xas_create_range() as suggested by Matthew Wilcox
- Fix in library function makes API safer by default, preventing callers from needing to remember cleanup
- Use shared cleanup label that both restore: and success: paths jump to
- Clean up unused spare node on both success and error exit paths
v2:
- Call xas_destroy() on both success and failure
- Explained retry semantics and xa_alloc / concurrency risk
- Dropped cleanup_empty_nodes from previous proposal
lib/xarray.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
lib/xarray.c +6 -1
@@ -744,11 +744,16 @@ void xas_create_range(struct xa_state *xas) 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);
Signature · 1 line
2.34.1
Quoted 56 lines · Shardul Bankar
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().Fixes cae106dd67b9 ("mm/khugepaged: refactor collapse_file control flow")Signed-off-by Shardul Bankar <[email protected]>---
v4:
- Drop redundantif (xa_alloc)aroundxas_destroy(), asxas_destroy()already checksxa_allocinternally.
v3:
- Move fix fromcollapse_file()toxas_create_range()as suggested by Matthew Wilcox
- Fix in library function makes API safer by default, preventing callers from needing to remember cleanup
- Use shared cleanup label that both restore: and success: paths jump to
- Clean up unused spare node on both success and error exit paths
v2:
- Callxas_destroy()on both success and failure
- Explained retry semantics andxa_alloc/ concurrency risk
- Droppedcleanup_empty_nodesfrom previous proposallib/xarray.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)lib/xarray.c +2
-1@@ -744,11 +744,16 @@ void xas_create_range(struct xa_state *xas) xas->xa_shift = shift; xas->xa_sibs = sibs; xas->xa_index = index; -return ; + goto cleanup; + success:
lib/xarray.c void xas_create_range(struct xa_state *xas) · line 759
Quoted 10 lines
lib/xarray.c +4
-0xas->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);
Nothing jumped at me, except that the label situation is a bit suboptimal.
Hoping Willy will take another look as well.
BTW, do we have a way to test this in a test case?
A follow-up cleanup that avoids labels could be something like (untested):
lib/xarray.c +17 -12
@@ -714,6 +714,7 @@ void xas_create_range(struct xa_state *xas)unsigned long index = xas->xa_index;unsigned char shift = xas->xa_shift;unsigned char sibs = xas->xa_sibs; +bool success = false; xas->xa_index |= ((sibs +1UL ) << shift) -1 ;if (xas_is_node (xas) && xas->xa_node->shift == xas->xa_shift)
@@ -724,9 +725,11 @@ void xas_create_range(struct xa_state *xas)for (;;) {xas_create (xas, true);if (xas_error (xas)) - goto restore; -if (xas->xa_index <= (index |XA_CHUNK_MASK )) - goto success; + break +if (xas->xa_index <= (index |XA_CHUNK_MASK )) { + succeess = true; +break ; + } xas->xa_index -=XA_CHUNK_SIZE ;for (;;) {
@@ -740,15 +743,17 @@ void xas_create_range(struct xa_state *xas) } } -restore: - xas->xa_shift = shift; - xas->xa_sibs = sibs; - xas->xa_index = index; -return ; -success: - xas->xa_index = index; -if (xas->xa_node) -xas_set_offset (xas); +if (success) { + xas->xa_index = index; +if (xas->xa_node) +xas_set_offset (xas); + }else { + xas->xa_shift = shift; + xas->xa_sibs = sibs; + xas->xa_index = index; + } +/* Free any unused spare node from xas_nomem() */ +xas_destroy (xas); }EXPORT_SYMBOL_GPL (xas_create_range);
Signature · 3 lines
Cheers David
Quoted 7 lines · David Hildenbrand (Red Hat)
...
Reviewed-by David Hildenbrand (Red Hat) <[email protected]>BTW, do we have a way to test this in a test case?
Hi David,
Thanks for the review and the Reviewed-by.
Regarding a test case: I don’t have a focused selftest or fault- injection setup yet that reliably hits this xas_nomem() + xas_create_range() corner case.
I noticed this spare-node leak while analyzing the Syzbot report I referenced in the Link: tag, but the reproducer I see there doesn’t isolate this path and reports other kmemleaks.
For now I’d prefer to treat this as a small correctness fix in xarray itself. If I manage to come up with a robust way to exercise this path in a selftest (e.g. via targeted fault injection in lib/test_xarray.c), I can follow up with a separate patch, but I don’t have anything solid to propose today.
Quoted 58 lines
A follow-up cleanup that avoids labels could be something like (untested):
lib/xarray.c +1
-0@@ -714,6 +714,7 @@ void xas_create_range(struct xa_state *xas) unsigned long index = xas->xa_index; unsigned char shift = xas->xa_shift; unsigned char sibs = xas->xa_sibs; + bool success = false;
xas->xa_index|= ((sibs + 1UL) << shift) - 1; if (xas_is_node(xas)&&xas->xa_node->shift== xas-Quoted 1 line
xa_shift)lib/xarray.c +5
-3@@ -724,9 +725,11 @@ void xas_create_range(struct xa_state *xas) for (;;) { xas_create(xas, true); if (xas_error(xas)) - goto restore; - if (xas->xa_index <= (index | XA_CHUNK_MASK)) - goto success; + break + if (xas->xa_index <= (index | XA_CHUNK_MASK)) { + succeess = true; + break; + } xas->xa_index -= XA_CHUNK_SIZE;
for (;;) {lib/xarray.c +0
-0@@ -740,15 +743,17 @@ void xas_create_range(struct xa_state *xas) } }lib/xarray.c +11
-9-restore: - xas->xa_shift = shift; - xas->xa_sibs = sibs; - xas->xa_index = index; - return; -success: - xas->xa_index = index; - if (xas->xa_node) - xas_set_offset(xas); + if (success) { + xas->xa_index = index; + if (xas->xa_node) + xas_set_offset(xas); + } else { + xas->xa_shift = shift; + xas->xa_sibs = sibs; + xas->xa_index = index; + } + /* Free any unused spare node from xas_nomem() */ + xas_destroy(xas);
Quoted 4 lines
}
EXPORT_SYMBOL_GPL(xas_create_range);
Your bool-based version reads nicer; I’m happy to follow up with a small cleanup patch on top that switches xas_create_range() over to that style (with a Suggested-by tag).
Thanks and Regards,
Shardul
David Hildenbrand (Red Hat)
maintainerQuoted 19 lines
Quoted 19 lines
lib/xarray.c +10
-9-restore: - xas->xa_shift = shift; - xas->xa_sibs = sibs; - xas->xa_index = index; - return; -success: - xas->xa_index = index; - if (xas->xa_node) - xas_set_offset(xas); + if (success) { + xas->xa_index = index; + if (xas->xa_node) + xas_set_offset(xas); + } else { + xas->xa_shift = shift; + xas->xa_sibs = sibs; + xas->xa_index = index; + } + /* Free any unused spare node from xas_nomem() */
Quoted 8 lines
Quoted 5 lines
+
xas_destroy(xas);
}
EXPORT_SYMBOL_GPL(xas_create_range);Your bool-based version reads nicer; I’m happy to follow up with a small cleanup patch on top that switches
xas_create_range()over to that style (with a Suggested-by tag).
Yeah, feel free to send a cleanup out that removes some of these labels (doesn't necessarily have to be what I proposed).
Signature · 3 lines
Cheers David
Dev Jain
reviewerQuoted 5 lines · Shardul Bankar
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
Nit: "When the lock is dropped after xas_create_range()".
Quoted 50 lines
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().Fixes cae106dd67b9 ("mm/khugepaged: refactor collapse_file control flow")Signed-off-by Shardul Bankar <[email protected]>---
v4:
- Drop redundantif (xa_alloc)aroundxas_destroy(), asxas_destroy()already checksxa_allocinternally.
v3:
- Move fix fromcollapse_file()toxas_create_range()as suggested by Matthew Wilcox
- Fix in library function makes API safer by default, preventing callers from needing to remember cleanup
- Use shared cleanup label that both restore: and success: paths jump to
- Clean up unused spare node on both success and error exit paths
v2:
- Callxas_destroy()on both success and failure
- Explained retry semantics andxa_alloc/ concurrency risk
- Droppedcleanup_empty_nodesfrom previous proposallib/xarray.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)lib/xarray.c +2
-1@@ -744,11 +744,16 @@ void xas_create_range(struct xa_state *xas) xas->xa_shift = shift; xas->xa_sibs = sibs; xas->xa_index = index; -return ; + goto cleanup; +
lib/xarray.c void xas_create_range(struct xa_state *xas) · line 758
Quoted 10 lines
lib/xarray.c +4
-0success: 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);
Since there are other code paths doing this fashion of xas_create/xas_store followed by calling xas_nomem till the former succeeds, I believe we should handle this in the caller - for example, I believe we need to fix xa_store_range() as well? It does xas_create, then drops the lock in the same fashion, and I believe we have no choice here but to handle it in the caller - we should not put xas_destroy inside xas_create.
Although for this particular case, we have only one caller of xas_create_range, so this is fine really.
Quoted 1 line
Quoted 15 lines · Shardul Bankar
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.
Quoted 6 lines
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().
I encountered another memory leak issue in xas_create_range().
collapse_file() calls xas_create_range() to pre-create all slots needed.
If collapse_file() finally fails, these pre-created slots are empty nodes. When the file is deleted, shmem_evict_inode()->shmem_truncate_range()->shmem_undo_range() calls xas_store(&xas, NULL) for each entries to delete nodes, but leaving those pre-created empty nodes leaked.
I can reproduce it with following steps.
1) create file /tmp/test_madvise_collapse and ftruncate to 4MB size, and then mmap the file
2) memset for the first 2MB
3) madvise(MADV_COLLAPSE) for the second 2MB.
4) unlink the file
in 3), collapse_file() calls xas_create_range() to expand xarray depth, and fails to collapse due to the whole 2M region is empty, leading to the new created empty nodes leaked.
To fix it, maybe we should add a new function xas_delete_range() to revert what xas_create_range() does when xas_create_range() runs into rollback path?
Quoted 45 lines
Fixes cae106dd67b9 ("mm/khugepaged: refactor collapse_file control flow")Signed-off-by Shardul Bankar<[email protected]>---
v4:
- Drop redundantif (xa_alloc)aroundxas_destroy(), asxas_destroy()already checksxa_allocinternally.
v3:
- Move fix fromcollapse_file()toxas_create_range()as suggested by Matthew Wilcox
- Fix in library function makes API safer by default, preventing callers from needing to remember cleanup
- Use shared cleanup label that both restore: and success: paths jump to
- Clean up unused spare node on both success and error exit paths
v2:
- Callxas_destroy()on both success and failure
- Explained retry semantics andxa_alloc/ concurrency risk
- Droppedcleanup_empty_nodesfrom previous proposallib/xarray.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)lib/xarray.c +6
-1@@ -744,11 +744,16 @@ void xas_create_range(struct xa_state *xas) 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);
Quoted 38 lines · Jinjiang Tu
Quoted 15 lines · Shardul Bankar
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.Quoted 6 lines
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().I encountered another memory leak issue in
xas_create_range().
collapse_file()callsxas_create_range()to pre-create all slots needed.
Ifcollapse_file()finally fails, these pre-created slots are empty nodes. When the file is deleted, shmem_evict_inode()->shmem_truncate_range()->shmem_undo_range() callsxas_store(&xas, NULL)for each entries to delete nodes, but leaving those pre-created empty nodes leaked.I can reproduce it with following steps.
1) create file/tmp/test_madvise_collapseand ftruncate to 4MB size, and then mmap the file
2) memset for the first 2MB
3)madvise(MADV_COLLAPSE)for the second 2MB.
4) unlink the file
Quoted 5 lines
in 3),
collapse_file()callsxas_create_range()to expand xarray depth, and fails to collapse due to the whole 2M region is empty, leading to the new created empty nodes leaked.To fix it, maybe we should add a new function
xas_delete_range()to revert whatxas_create_range()does whenxas_create_range()runs into rollback path?
How about the following diff? I tried it, and the memory leak disappears. I'm new in xarray, so I don't if this fix works properly.
include/linux/xarray.h +0 -0
@@ -1555,6 +1555,7 @@ void xas_destroy(struct xa_state *);
void xas_pause(struct xa_state *);
void xas_create_range(struct xa_state *);
+void xas_destroy_range(struct xa_state *xas, unsigned long start, unsigned long end);
#ifdef CONFIG_XARRAY_MULTI
int xa_get_order(struct xarray *, unsigned long index);
lib/xarray.c +0 -0
@@ -752,6 +752,21 @@ void xas_create_range(struct xa_state *xas)
}
EXPORT_SYMBOL_GPL(xas_create_range);
+void xas_destroy_range(struct xa_state *xas, unsigned long start, unsigned long end) +{
+ unsigned long index;
+ void *entry;
+
+ for (index = start; index < end; ++index) {
+ xas_set(xas, index);
+ entry = xas_load(xas);
+ if (entry)
+ continue;
+ else if (xas->xa_node && !xas->xa_node->count)
+ xas_delete_node(xas);
+ }
+}
+
static void update_node(struct xa_state *xas, struct xa_node *node, int count, int values)
{
mm/khugepaged.c +0 -0
@@ -2247,7 +2247,10 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
goto out;
rollback:
- /* Something went wrong: roll back page cache changes */ + /* Something went wrong: roll back empty xa_node created by + * xas_create_range() and page cache changes
+ */
+ xas_destroy_range(&xas, start, end);
+
if (nr_none) {
xas_lock_irq(&xas);
mapping->nrpages -= nr_none;
Quoted 44 lines
Quoted 44 lines
Fixes cae106dd67b9 ("mm/khugepaged: refactor collapse_file control flow")Signed-off-by Shardul Bankar<[email protected]>---
v4:
- Drop redundantif (xa_alloc)aroundxas_destroy(), asxas_destroy()already checksxa_allocinternally.
v3:
- Move fix fromcollapse_file()toxas_create_range()as suggested by Matthew Wilcox
- Fix in library function makes API safer by default, preventing callers from needing to remember cleanup
- Use shared cleanup label that both restore: and success: paths jump to
- Clean up unused spare node on both success and error exit paths
v2:
- Callxas_destroy()on both success and failure
- Explained retry semantics andxa_alloc/ concurrency risk
- Droppedcleanup_empty_nodesfrom previous proposallib/xarray.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)lib/xarray.c +6
-1@@ -744,11 +744,16 @@ void xas_create_range(struct xa_state *xas) 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);
Hi Matthew, Andrew,
Just a gentle ping on this one.
v4 has a Reviewed-by from David, and Dev and Jinjiang both followed up with additional observations and ideas for related cleanups. As far as I
can see, there are no outstanding objections to the current xas_nomem() /xas_create_range() spare-node fix.
If this looks good to you, could it be queued for inclusion via whichever
tree you think is appropriate?
The separate question that Jinjiang raised about empty xa_nodes installed
by xas_create_range() but never populated is being discussed in its own bug-report thread here:
https://lore.kernel.org/all/[email protected]/
Once this patch is accepted/taken, I plan to follow up with a small cleanup
patch that simplifies the label usage in xas_create_range() along the lines
David suggested.
Thanks,
Shardul
Hi Matthew, Andrew,
Gently pinging this patch. I’ve checked the latest linux-next (next-20260123) and the akpm-unstable branches, and it doesn't appear to have been picked up yet.
The patch has a Reviewed-by from David Hildenbrand and fixes a memory leak in the XArray library identified by syzbot.
If there are no further concerns, could you please let me know if this is queued for the next cycle?
Thanks, Shardul
Andrew Morton
maintainerQuoted 10 lines · On Mon, 26 Jan 2026 11:16:08 +0530 Shardul B <[email protected]>…
Hi Matthew, Andrew,
Gently pinging this patch. I’ve checked the latest linux-next (next-20260123) and the akpm-unstable branches, and it doesn't appear to have been picked up yet.
The patch has a Reviewed-by from David Hildenbrand and fixes a memory leak in the XArray library identified by syzbot.
If there are no further concerns, could you please let me know if this is queued for the next cycle?
There are comments from Jinjiang Tu and Dev Jain which remain unaddressed, please.
The leak is a rare and this is a minor problem, I believe?
I'll queue the patch for some exposure and testing but it appears that some additional consideration is needed before it should be progressed further.
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Mon, 26 Jan 2026 11:16:08 +0530 Shardul B <[email protected]>…
Hi Matthew, Andrew,
Gently pinging this patch. I’ve checked the latest linux-next (next-20260123) and the akpm-unstable branches, and it doesn't appear to have been picked up yet.
Quoted 6 lines
Quoted 4 lines
The patch has a Reviewed-by from David Hildenbrand and fixes a memory leak in the XArray library identified by syzbot.
If there are no further concerns, could you please let me know if this is queued for the next cycle?
There are comments from Jinjiang Tu and Dev Jain which remain unaddressed, please.
Hi, Andrew
The issue is another problem, independent ofShardul's patch.
I have posted a patch to solve it.
https://lore.kernel.org/linux-mm/[email protected]
Quoted 6 lines
The leak is a rare and this is a minor problem, I believe?
I'll queue the patch for some exposure and testing but it appears that some additional consideration is needed before it should be progressed further.
Andrew Morton
maintainerQuoted 10 lines · On Mon, 26 Jan 2026 11:16:08 +0530 Shardul B <[email protected]>…
Hi Matthew, Andrew,
Gently pinging this patch. I’ve checked the latest linux-next (next-20260123) and the akpm-unstable branches, and it doesn't appear to have been picked up yet.
The patch has a Reviewed-by from David Hildenbrand and fixes a memory leak in the XArray library identified by syzbot.
If there are no further concerns, could you please let me know if this is queued for the next cycle?
I finally got onto this. I prepared a test_array case to demonstrate the bug and confirmed that your patch made the test happy.
I'll send the 2-patch series separately.
Thanks.
Matthew Wilcox
maintainerQuoted 1 line · Shardul Bankar
Hi Matthew, Andrew,
pinging on december 31st is an entirely ineffective thing to do. for one, oracle is in shutdown.