[PATCH v2 1/2] lib/test_xarray: check the offset of new root nodes

From: Andrew Morton

Date: Sat Nov 21 2026 - 17:18:34 EST


From: Nickolai Zeldovich <nickolai@xxxxxxxxxxxxx>

Free XArray nodes whose offsets are nonzero, then check that roots
created by xas_create() and xas_expand() have offset zero. An old
root with a stale offset can make a concurrent iterator resume in
the wrong slot after expansion.

Link: https://lore.kernel.org/r/20261024014344.830116-1-nickolai@xxxxxxxxxxxxx
Signed-off-by: Nickolai Zeldovich <nickolai@xxxxxxxxxxxxx>
Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx>
Cc: Baoli Zhang <baoli.zhang@xxxxxxxxx>
Cc: <syzbot+73641b60a3bc11dfca8c@xxxxxxxxxxxxxxxxxxxxxxxxx>

lib/test_xarray.c | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)

--- a/lib/test_xarray.c~lib-test_xarray-check-the-offset-of-new-root-nodes
+++ a/lib/test_xarray.c
@@ -370,6 +370,38 @@ static noinline void check_xa_shrink(str
}
}

+static noinline void check_xa_root_offset(struct xarray *xa)
+{
+ DEFINE_XARRAY(tmp);
+ struct xa_node *node;
+
+ /* Free two nodes with non-zero offsets so the slab can reuse them */
+ xa_store_index(&tmp, 0, GFP_KERNEL);
+ xa_store_index(&tmp, 5UL << XA_CHUNK_SHIFT, GFP_KERNEL);
+ xa_store_index(&tmp, 6UL << XA_CHUNK_SHIFT, GFP_KERNEL);
+ xa_store_index(&tmp, 7UL << XA_CHUNK_SHIFT, GFP_KERNEL);
+ xa_erase_index(&tmp, 5UL << XA_CHUNK_SHIFT);
+ xa_erase_index(&tmp, 6UL << XA_CHUNK_SHIFT);
+ rcu_barrier();
+
+ /* A root created by xas_create() must have offset 0 */
+ XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL));
+ rcu_read_lock();
+ node = xa_to_node(xa_head(xa));
+ XA_BUG_ON(xa, node->offset != 0);
+ rcu_read_unlock();
+
+ /* And so must a root created by xas_expand() */
+ XA_BUG_ON(xa, xa_store_index(xa, XA_CHUNK_SIZE, GFP_KERNEL));
+ rcu_read_lock();
+ node = xa_to_node(xa_head(xa));
+ XA_BUG_ON(xa, node->offset != 0);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+ xa_destroy(&tmp);
+}
+
static noinline void check_insert(struct xarray *xa)
{
unsigned long i;
@@ -2336,6 +2368,7 @@ static int xarray_checks(void)
check_xa_load(&array);
check_xa_mark(&array);
check_xa_shrink(&array);
+ check_xa_root_offset(&array);
check_xas_erase(&array);
check_insert(&array);
check_cmpxchg(&array);
_