* [PATCH] xarray: fix index jumping backwards in xas_find_marked()
@ 2026-11-24 21:34 Qian Xiao
2026-11-25 21:34 ` sashiko-bot
2026-11-26 19:08 ` [PATCH v2] " Qian Xiao
0 siblings, 2 replies; 12+ messages in thread
From: Qian Xiao @ 2026-11-24 21:34 UTC (permalink / raw)
To: Matthew Wilcox, Andrew Morton
Cc: linux-fsdevel, linux-mm, linux-kernel, Krystian Kaniewski,
Qian Xiao
xas_find() keeps xa_index when a lookup starts inside a multi-index
entry stored in a non-leaf node, and aligns xa_offset with that index
before advancing. xas_find_marked() still does neither.
Walking from a sibling slot to the canonical slot calls
xas_move_index(), so a start index such as 127 becomes 0. After the
cursor has been walked, xa_offset can also stay on the canonical slot
while xa_index is inside a later slot of the same entry. The next
xas_find_marked() then recomputes xa_index from the stale offset. If
the entry is split, the call returns a marked entry below the index
already reached. xas_find() returns the following entry in the same
situation.
Resolve the sibling without moving xa_index. Before advancing a cursor
that has already been walked, set xa_offset from xa_index the same way
xas_find() does. Keep skipping sibling entries at the return site.
The userspace XArray test suite passed. The kernel was not built.
Fixes: b803b42823d0 ("xarray: Add XArray iterators")
Cc: <stable@vger.kernel.org>
Signed-off-by: Qian Xiao <heyheyco@gmail.com>
---
lib/test_xarray.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++
lib/xarray.c | 12 ++++--
2 files changed, 113 insertions(+), 3 deletions(-)
diff --git a/lib/test_xarray.c b/lib/test_xarray.c
index 615f7730a5bd..ddc392d42d29 100644
--- a/lib/test_xarray.c
+++ b/lib/test_xarray.c
@@ -1316,6 +1316,109 @@ static noinline void check_multi_find_4(struct xarray *xa)
#endif
}
+static noinline void check_multi_find_marked_nonleaf(struct xarray *xa)
+{
+#ifdef CONFIG_XARRAY_MULTI
+ unsigned int order = XA_CHUNK_SHIFT + 1;
+ unsigned long next = 1UL << order;
+ unsigned long start = next - 1;
+ XA_STATE(xas, xa, start);
+ XA_STATE_ORDER(split, xa, 0, 0);
+ void *entry;
+ unsigned long i;
+
+ /* Multi-index entry in two slots of a non-leaf node, both marked. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+ xa_set_mark(xa, next, XA_MARK_0);
+
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(next));
+ XA_BUG_ON(xa, xas.xa_index != next);
+
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != NULL);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+ XA_BUG_ON(xa, !xa_empty(xa));
+
+ /* Split the multi-index entry after a lookup begins inside it. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+ xa_set_mark(xa, next, XA_MARK_0);
+
+ xas_set(&xas, start);
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+ rcu_read_unlock();
+
+ xas_split_alloc(&split, xa_mk_index(0), order, GFP_KERNEL);
+ if (xas_error(&split)) {
+ XA_BUG_ON(xa, true);
+ goto out;
+ }
+ xas_lock(&split);
+ xas_split(&split, xa_mk_index(0), order);
+ for (i = 0; i < next; i++)
+ __xa_store(xa, i, xa_mk_index(i), 0);
+ xas_unlock(&split);
+
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(next));
+ XA_BUG_ON(xa, xas.xa_index != next);
+
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != NULL);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+
+ /* Nothing marked past the index already walked. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+
+ xas_set(&xas, start);
+ rcu_read_lock();
+ entry = xas_load(&xas);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+ rcu_read_unlock();
+
+ xas_set(&split, 0);
+ xas_split_alloc(&split, xa_mk_index(0), order, GFP_KERNEL);
+ if (xas_error(&split)) {
+ XA_BUG_ON(xa, true);
+ goto out;
+ }
+ xas_lock(&split);
+ xas_split(&split, xa_mk_index(0), order);
+ for (i = 0; i < next; i++)
+ __xa_store(xa, i, xa_mk_index(i), 0);
+ xas_unlock(&split);
+
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != NULL);
+ rcu_read_unlock();
+
+out:
+ xa_destroy(xa);
+ XA_BUG_ON(xa, !xa_empty(xa));
+#endif
+}
+
static noinline void check_find_1(struct xarray *xa)
{
unsigned long i, j, k;
@@ -1440,6 +1543,7 @@ static noinline void check_find(struct xarray *xa)
check_multi_find_2(xa);
check_multi_find_3(xa);
check_multi_find_4(xa);
+ check_multi_find_marked_nonleaf(xa);
}
/* See find_swap_entry() in mm/shmem.c */
diff --git a/lib/xarray.c b/lib/xarray.c
index 48f440d114e1..4d943791a7b4 100644
--- a/lib/xarray.c
+++ b/lib/xarray.c
@@ -1495,6 +1495,14 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
}
+ if (advance &&
+ xas->xa_offset != get_offset(xas->xa_index, xas->xa_node)) {
+ if (!xas->xa_node->shift)
+ xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
+ else
+ xas->xa_offset = get_offset(xas->xa_index, xas->xa_node);
+ }
+
while (xas->xa_index <= max) {
if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
xas->xa_offset = xas->xa_node->offset + 1;
@@ -1507,10 +1515,8 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
if (!advance) {
entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
- if (xa_is_sibling(entry)) {
+ if (xa_is_sibling(entry))
xas->xa_offset = xa_to_sibling(entry);
- xas_move_index(xas, xas->xa_offset);
- }
}
offset = xas_find_chunk(xas, advance, mark);
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] xarray: fix index jumping backwards in xas_find_marked()
2026-11-24 21:34 [PATCH] xarray: fix index jumping backwards in xas_find_marked() Qian Xiao
@ 2026-11-25 21:34 ` sashiko-bot
2026-11-26 19:08 ` [PATCH v2] " Qian Xiao
1 sibling, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-25 21:34 UTC (permalink / raw)
To: Qian Xiao, Matthew Wilcox, Andrew Morton; +Cc: linux-mm, Krystian Kaniewski
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] xarray: concurrent split race in xas_find_marked() on sibling
resolution
- [High] xarray: xas_next_marked() misses leaf offset check and jumps
xa_index back
- [High] xarray: xas_pause() overshoots xa_index and skips entries after
sibling find
--
commit 9233cae6490ceca4faaeb7112a2155eec3568913
Author: Qian Xiao <heyheyco@gmail.com>
xarray: fix index jumping backwards in xas_find_marked()
This commit preserves xas->xa_index when xas_find_marked() resolves a
sibling entry and aligns xas->xa_offset from xas->xa_index before
advancing an already walked cursor.
> diff --git a/lib/xarray.c b/lib/xarray.c
> index 8c09f6c6cd8f7..a433f77f61a72 100644
> --- a/lib/xarray.c
> +++ b/lib/xarray.c
> @@ -1500,6 +1500,14 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
> xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
> }
>
> + if (advance &&
> + xas->xa_offset != get_offset(xas->xa_index, xas->xa_node)) {
> + if (!xas->xa_node->shift)
> + xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
> + else
> + xas->xa_offset = get_offset(xas->xa_index, xas->xa_node);
> + }
[Severity: High]
Does xas_next_marked() bypass this leaf-node offset alignment when
advancing via xas_for_each_marked()?
Unlike xas_next_entry(), xas_next_marked() does not fall back to
xas_find_marked() when xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK):
include/linux/xarray.h:xas_next_marked() {
struct xa_node *node = xas->xa_node;
void *entry;
unsigned int offset;
if (unlikely(xas_not_node(node) || node->shift))
return xas_find_marked(xas, max, mark);
offset = xas_find_chunk(xas, true, mark);
xas->xa_offset = offset;
xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
When a lookup starts at a sibling index inside a marked multi-index entry
in a leaf node (node->shift == 0), xas_find_marked() now returns with
xas->xa_offset at the canonical slot and xas->xa_index at the sibling
index. If that entry is split before the next step, xas_next_marked()
searches from the stale canonical xas->xa_offset + 1 and recomputes
xas->xa_index backwards.
> while (xas->xa_index <= max) {
> if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
> xas->xa_offset = xas->xa_node->offset + 1;
> @@ -1512,10 +1520,8 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
>
> if (!advance) {
> entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
> - if (xa_is_sibling(entry)) {
> + if (xa_is_sibling(entry))
> xas->xa_offset = xa_to_sibling(entry);
> - xas_move_index(xas, xas->xa_offset);
> - }
> }
>
> offset = xas_find_chunk(xas, advance, mark);
[Severity: High]
Can a concurrent xas_split() on a non-leaf multi-index entry cause
xas_find_marked() to descend into the canonical slot's child node while
xas->xa_index still points into the sibling slot's range?
CPU0 CPU1
xas_find_marked(&xas, ...)
/* shift-6 node, xas_index = 100 */
/* slot 1 is sibling of slot 0 */
xas->xa_offset = 0;
xas_split()
/* replaces slots 0 and 1 with */
/* child_0 (0..63) and child_1 */
offset = xas_find_chunk(...) /* 0 */
entry = xa_entry(..., 0) /* child_0 */
xas->xa_node = xa_to_node(entry);
xas_set_offset(xas); /* offset = 36 */
return child_0->slots[36]; <-- entry for index 36 at xa_index 100
Because xas->xa_index is left at 100 while xas->xa_node is child_0,
xas_find_marked() returns the entry for index 36 with xas.xa_index == 100.
xas_reload() in find_get_entry() also indexes child_0 by
get_offset(100, child_0) == 36, so the race passes reload validation and
triggers VM_BUG_ON_FOLIO(!folio_contains(folio, xas.xa_index)) in
find_lock_entries().
In addition, if the mark on canonical slot 0 is cleared after the split
while a slot at canon_offset < offset <= get_offset(xas->xa_index, node)
remains marked, xas_find_chunk() returns that offset and
xas_move_index(xas, offset) moves xas->xa_index backwards (from 100 to 64).
[Severity: High]
Does leaving xas->xa_offset on the canonical slot while keeping
xas->xa_index at the sibling index also cause xas_pause() to overshoot
xas->xa_index and skip entries?
lib/xarray.c:xas_pause() {
...
unsigned long offset = xas->xa_offset;
while (++offset < XA_CHUNK_SIZE) {
if (!xa_is_sibling(xa_entry(xas->xa, node, offset)))
break;
}
xas->xa_index &= ~0UL << node->shift;
xas->xa_index += (offset - xas->xa_offset) << node->shift;
if (xas->xa_index == 0)
xas->xa_node = XAS_BOUNDS;
...
}
xas_pause() masks out only the bits below node->shift and adds the full
entry span (offset - xas->xa_offset) << node->shift onto xas->xa_index,
assuming get_offset(xas->xa_index, node) == xas->xa_offset. When a walk
starts at index 3 inside an order-2 marked entry at 0..3 (xas->xa_offset == 0,
xas->xa_index == 3) and calls xas_pause(), xas->xa_index becomes 3 + 4 = 7
instead of 4, skipping indices 4..6 in callers such as
tag_pages_for_writeback().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124213424.2224554-1-heyheyco@gmail.com?part=1
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v2] xarray: fix index jumping backwards in xas_find_marked()
2026-11-24 21:34 [PATCH] xarray: fix index jumping backwards in xas_find_marked() Qian Xiao
2026-11-25 21:34 ` sashiko-bot
@ 2026-11-26 19:08 ` Qian Xiao
2026-11-27 19:09 ` sashiko-bot
` (2 more replies)
1 sibling, 3 replies; 12+ messages in thread
From: Qian Xiao @ 2026-11-26 19:08 UTC (permalink / raw)
To: Matthew Wilcox, Andrew Morton
Cc: linux-fsdevel, linux-mm, linux-kernel, Krystian Kaniewski,
Qian Xiao
xas_find() keeps xa_index when a lookup starts inside a multi-index
entry stored in a non-leaf node, and aligns xa_offset with that index
before advancing. xas_find_marked() still does neither.
Walking from a sibling slot to the canonical slot calls
xas_move_index(), so a start index such as 127 becomes 0. After the
cursor has been walked, xa_offset can also stay on the canonical slot
while xa_index is inside a later slot of the same entry. The next
xas_find_marked() then recomputes xa_index from the stale offset. If
the entry is split, the call returns a marked entry below the index
already reached. xas_find() returns the following entry in the same
situation.
Resolve the sibling without moving xa_index. Before advancing a cursor
that has already been walked, set xa_offset from xa_index the same way
xas_find() does. xas_next_marked() uses that path when the offset and
index disagree, as xas_next_entry() already does. xas_pause() aligns
xa_offset before its existing calculation, so the resume index stays at
the end of the entry.
If the tree changes while a sibling is being resolved, do not descend
into a node that does not contain xa_index, and do not let
xas_move_index() decrease xa_index. Return XA_RETRY_ENTRY instead.
Callers that use xas_retry() restart from the same index.
The userspace XArray test suite passed. The kernel was not built.
Fixes: b803b42823d0 ("xarray: Add XArray iterators")
Cc: <stable@vger.kernel.org>
Signed-off-by: Qian Xiao <heyheyco@gmail.com>
---
v2:
- Use the slow path in xas_next_marked() when the offset and index disagree.
- Align xa_offset in xas_pause() so the resume index stays at the end of
the entry.
- Return XA_RETRY_ENTRY when a concurrent change would descend into the
wrong node or decrease xa_index.
include/linux/xarray.h | 3 +-
lib/test_xarray.c | 165 +++++++++++++++++++++++++++++++++++++++++
lib/xarray.c | 23 +++++-
3 files changed, 187 insertions(+), 4 deletions(-)
diff --git a/include/linux/xarray.h b/include/linux/xarray.h
index be850174e802..8c42d899d94c 100644
--- a/include/linux/xarray.h
+++ b/include/linux/xarray.h
@@ -1778,7 +1778,8 @@ static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
void *entry;
unsigned int offset;
- if (unlikely(xas_not_node(node) || node->shift))
+ if (unlikely(xas_not_node(node) || node->shift ||
+ xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
return xas_find_marked(xas, max, mark);
offset = xas_find_chunk(xas, true, mark);
xas->xa_offset = offset;
diff --git a/lib/test_xarray.c b/lib/test_xarray.c
index 615f7730a5bd..02e6843d0227 100644
--- a/lib/test_xarray.c
+++ b/lib/test_xarray.c
@@ -1316,6 +1316,170 @@ static noinline void check_multi_find_4(struct xarray *xa)
#endif
}
+static noinline void check_multi_find_marked_nonleaf(struct xarray *xa)
+{
+#ifdef CONFIG_XARRAY_MULTI
+ unsigned int order = XA_CHUNK_SHIFT + 1;
+ unsigned long next = 1UL << order;
+ unsigned long start = next - 1;
+ XA_STATE(xas, xa, start);
+ XA_STATE_ORDER(split, xa, 0, 0);
+ void *entry;
+ unsigned long i;
+
+ /* Multi-index entry in two slots of a non-leaf node, both marked. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+ xa_set_mark(xa, next, XA_MARK_0);
+
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(next));
+ XA_BUG_ON(xa, xas.xa_index != next);
+
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != NULL);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+ XA_BUG_ON(xa, !xa_empty(xa));
+
+ /* Split the multi-index entry after a lookup begins inside it. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+ xa_set_mark(xa, next, XA_MARK_0);
+
+ xas_set(&xas, start);
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+ rcu_read_unlock();
+
+ xas_split_alloc(&split, xa_mk_index(0), order, GFP_KERNEL);
+ if (xas_error(&split)) {
+ XA_BUG_ON(xa, true);
+ goto out;
+ }
+ xas_lock(&split);
+ xas_split(&split, xa_mk_index(0), order);
+ for (i = 0; i < next; i++)
+ __xa_store(xa, i, xa_mk_index(i), 0);
+ xas_unlock(&split);
+
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(next));
+ XA_BUG_ON(xa, xas.xa_index != next);
+
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != NULL);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+
+ /* Nothing marked past the index already walked. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+
+ xas_set(&xas, start);
+ rcu_read_lock();
+ entry = xas_load(&xas);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+ rcu_read_unlock();
+
+ xas_set(&split, 0);
+ xas_split_alloc(&split, xa_mk_index(0), order, GFP_KERNEL);
+ if (xas_error(&split)) {
+ XA_BUG_ON(xa, true);
+ goto out;
+ }
+ xas_lock(&split);
+ xas_split(&split, xa_mk_index(0), order);
+ for (i = 0; i < next; i++)
+ __xa_store(xa, i, xa_mk_index(i), 0);
+ xas_unlock(&split);
+
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != NULL);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+
+ /* Pausing resumes at the next marked entry, without repeating. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+ xa_set_mark(xa, next, XA_MARK_0);
+ xas_set(&xas, start);
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+ xas_pause(&xas);
+ XA_BUG_ON(xa, xas.xa_index != next);
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(next));
+ XA_BUG_ON(xa, xas.xa_index != next);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+
+ /*
+ * A leaf multi-index entry. The next step must not use a canonical
+ * slot left behind by the first lookup.
+ */
+ if (XA_CHUNK_SHIFT > 2) {
+ unsigned int leaf_order = 2;
+ unsigned long leaf_next = 1UL << leaf_order;
+ unsigned long leaf_start = leaf_next - 1;
+
+ xa_store_order(xa, 0, leaf_order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, leaf_next, GFP_KERNEL) != NULL);
+ xa_set_mark(xa, leaf_next, XA_MARK_0);
+
+ xas_set(&xas, leaf_start);
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != leaf_start);
+ rcu_read_unlock();
+
+ xas_set(&split, 0);
+ xas_split_alloc(&split, xa_mk_index(0), leaf_order, GFP_KERNEL);
+ if (xas_error(&split)) {
+ XA_BUG_ON(xa, true);
+ goto out;
+ }
+ xas_lock(&split);
+ xas_split(&split, xa_mk_index(0), leaf_order);
+ for (i = 0; i < leaf_next; i++)
+ __xa_store(xa, i, xa_mk_index(i), 0);
+ xas_unlock(&split);
+
+ rcu_read_lock();
+ entry = xas_next_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(leaf_next));
+ XA_BUG_ON(xa, xas.xa_index != leaf_next);
+ rcu_read_unlock();
+ }
+
+out:
+ xa_destroy(xa);
+ XA_BUG_ON(xa, !xa_empty(xa));
+#endif
+}
+
static noinline void check_find_1(struct xarray *xa)
{
unsigned long i, j, k;
@@ -1440,6 +1604,7 @@ static noinline void check_find(struct xarray *xa)
check_multi_find_2(xa);
check_multi_find_3(xa);
check_multi_find_4(xa);
+ check_multi_find_marked_nonleaf(xa);
}
/* See find_swap_entry() in mm/shmem.c */
diff --git a/lib/xarray.c b/lib/xarray.c
index 48f440d114e1..eadb39894913 100644
--- a/lib/xarray.c
+++ b/lib/xarray.c
@@ -1283,6 +1283,9 @@ void xas_pause(struct xa_state *xas)
if (xas_invalid(xas))
return;
+ if (node && xas->xa_offset != get_offset(xas->xa_index, node))
+ xas->xa_offset = get_offset(xas->xa_index, node);
+
xas->xa_node = XAS_RESTART;
if (node) {
unsigned long offset = xas->xa_offset;
@@ -1495,6 +1498,14 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
}
+ if (advance &&
+ xas->xa_offset != get_offset(xas->xa_index, xas->xa_node)) {
+ if (!xas->xa_node->shift)
+ xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
+ else
+ xas->xa_offset = get_offset(xas->xa_index, xas->xa_node);
+ }
+
while (xas->xa_index <= max) {
if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
xas->xa_offset = xas->xa_node->offset + 1;
@@ -1507,14 +1518,18 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
if (!advance) {
entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
- if (xa_is_sibling(entry)) {
+ if (xa_is_sibling(entry))
xas->xa_offset = xa_to_sibling(entry);
- xas_move_index(xas, xas->xa_offset);
- }
}
offset = xas_find_chunk(xas, advance, mark);
if (offset > xas->xa_offset) {
+ unsigned long index = xas->xa_index;
+
+ index &= ~XA_CHUNK_MASK << xas->xa_node->shift;
+ index += offset << xas->xa_node->shift;
+ if (index < xas->xa_index)
+ return XA_RETRY_ENTRY;
advance = false;
xas_move_index(xas, offset);
/* Mind the wrap */
@@ -1532,6 +1547,8 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
continue;
if (!xa_is_node(entry))
return entry;
+ if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
+ return XA_RETRY_ENTRY;
xas->xa_node = xa_to_node(entry);
xas_set_offset(xas);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v2] xarray: fix index jumping backwards in xas_find_marked()
2026-11-26 19:08 ` [PATCH v2] " Qian Xiao
@ 2026-11-27 19:09 ` sashiko-bot
2026-11-27 22:21 ` [PATCH v3] " Qian Xiao
2026-12-03 7:00 ` [syzbot ci] " syzbot ci
2 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-27 19:09 UTC (permalink / raw)
To: Qian Xiao, Matthew Wilcox, Andrew Morton; +Cc: linux-mm, Krystian Kaniewski
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Critical] xarray: 32-bit shift overflow and wraparound in
xas_find_marked()
- [Medium] xarray: missed retry in xa_find_after() after sibling rewind
- [Medium] xarray: cursor desync in xas_find_marked() on concurrent
sibling store
--
commit 7642e490f837a825b02324be66c62d579219e1aa
Author: Qian Xiao <heyheyco@gmail.com>
xarray: fix index jumping backwards in xas_find_marked()
This commit keeps xa_index unchanged when xas_find_marked() resolves a
sibling entry, aligns xa_offset before advancing or pausing, and returns
XA_RETRY_ENTRY if a concurrent tree change would move xa_index backwards
or descend into the wrong node.
> diff --git a/lib/xarray.c b/lib/xarray.c
> index 8c09f6c6cd8f7..6b2e838117132 100644
> --- a/lib/xarray.c
> +++ b/lib/xarray.c
[ ... ]
> @@ -1512,14 +1523,18 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
>
> if (!advance) {
> entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
> - if (xa_is_sibling(entry)) {
> + if (xa_is_sibling(entry))
> xas->xa_offset = xa_to_sibling(entry);
> - xas_move_index(xas, xas->xa_offset);
> - }
> }
>
> offset = xas_find_chunk(xas, advance, mark);
> if (offset > xas->xa_offset) {
> + unsigned long index = xas->xa_index;
> +
> + index &= ~XA_CHUNK_MASK << xas->xa_node->shift;
> + index += offset << xas->xa_node->shift;
> + if (index < xas->xa_index)
> + return XA_RETRY_ENTRY;
[Severity: Critical]
Can "offset << xas->xa_node->shift" overflow 32 bits, or wrap index around
ULONG_MAX, and make xas_find_marked() return XA_RETRY_ENTRY when no race
occurred?
offset is an unsigned int, so on 64-bit systems shifting it by
xas->xa_node->shift >= 30 overflows or hits undefined behavior in 32-bit
arithmetic before widening to unsigned long (for example, 64U << 30 is 0).
Even at smaller shifts, when xas_find_chunk() returns XA_CHUNK_SIZE in a
node at the top of the ULONG_MAX range, adding offset << shift wraps index
to 0 before the wrap check below can terminate the search.
In either case index < xas->xa_index is true on an unmodified tree: RCU
callers using xas_retry() such as find_get_entry() spin forever at the
same xa_index, while locked xas_for_each_marked() callers that do not
check xas_retry() dereference XA_RETRY_ENTRY. Checking
"offset <= get_offset(xas->xa_index, xas->xa_node)" instead would avoid
both the 32-bit shift and the ULONG_MAX wrap.
> advance = false;
> xas_move_index(xas, offset);
> /* Mind the wrap */
> @@ -1537,6 +1552,8 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
> continue;
> if (!xa_is_node(entry))
> return entry;
> + if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
> + return XA_RETRY_ENTRY;
[Severity: Medium]
What happens in xa_find_after() when xas_find_marked() returns
XA_RETRY_ENTRY after rewinding xas->xa_offset from a sibling slot?
When xas->xa_offset is rewound to xa_to_sibling(entry) without updating
xas->xa_index, xas->xa_offset is less than get_offset(xas->xa_index,
xas->xa_node). If a concurrent split is then caught by either of the new
XA_RETRY_ENTRY returns, xas->xa_node is still a valid node pointer:
lib/xarray.c:xa_find_after() {
...
if (xas_invalid(&xas))
break;
if (xas_sibling(&xas))
continue;
if (!xas_retry(&xas, entry))
break;
...
}
Because xas_sibling() checks whether (xas->xa_index & mask) exceeds
xas->xa_offset << node->shift, it returns true for XA_RETRY_ENTRY and
xa_find_after() continues without calling xas_retry() to reset the cursor.
The next iteration enters xas_find_marked() with advance == true, aligns
xa_offset to get_offset(xas->xa_index, xas->xa_node), and searches from
xa_offset + 1, skipping any marked entries at or after *indexp + 1 in the
newly split child node.
[Severity: Medium]
Can the "if (xa_is_sibling(entry)) continue;" branch just above this
desynchronize xas->xa_offset and xas->xa_index when a concurrent store
replaces the target slot with a sibling entry?
CPU0 CPU1
xas_find_marked()
offset = xas_find_chunk(...)
if (offset > xas->xa_offset)
advance = false
xas_move_index(xas, offset)
xas->xa_offset = offset
/* multi-index store at canon < offset */
/* slot[offset] becomes sibling(canon) */
entry = xa_entry(..., offset)
if (xa_is_sibling(entry))
continue
/* next iteration: !advance is true */
if (xa_is_sibling(entry))
xas->xa_offset = canon <-- xa_offset < get_offset(xa_index)
Because xas_move_index() already moved xas->xa_index forward to
offset << shift, rewinding xas->xa_offset to canon on the next iteration
leaves xas->xa_index at the higher slot while xas_find_chunk(xas, false,
mark) searches from canon. If canon is marked, xas_find_marked() can
re-return an entry at or below an index already visited, or return the
entry at canon with xas->xa_offset == canon and xas->xa_index ==
offset << shift, causing xa_find_after() to treat it as a sibling via
xas_sibling() and skip it.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126190847.3064508-1-heyheyco@gmail.com?part=1
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3] xarray: fix index jumping backwards in xas_find_marked()
2026-11-26 19:08 ` [PATCH v2] " Qian Xiao
2026-11-27 19:09 ` sashiko-bot
@ 2026-11-27 22:21 ` Qian Xiao
2026-11-28 22:21 ` sashiko-bot
2026-11-29 3:12 ` Andrew Morton
2026-12-03 7:00 ` [syzbot ci] " syzbot ci
2 siblings, 2 replies; 12+ messages in thread
From: Qian Xiao @ 2026-11-27 22:21 UTC (permalink / raw)
To: Matthew Wilcox, Andrew Morton
Cc: linux-fsdevel, linux-mm, linux-kernel, Krystian Kaniewski,
Qian Xiao
xas_find() keeps xa_index when a lookup starts inside a multi-index
entry stored in a non-leaf node, and aligns xa_offset with that index
before advancing. xas_find_marked() still does neither.
Walking from a sibling slot to the canonical slot calls
xas_move_index(), so a start index such as 127 becomes 0. After the
cursor has been walked, xa_offset can also stay on the canonical slot
while xa_index is inside a later slot of the same entry. The next
xas_find_marked() then recomputes xa_index from the stale offset. If
the entry is split, the call returns a marked entry below the index
already reached. xas_find() returns the following entry in the same
situation.
Resolve the sibling without moving xa_index. Before advancing a cursor
that has already been walked, set xa_offset from xa_index the same way
xas_find() does. xas_next_marked() uses that path when the offset and
index disagree, as xas_next_entry() already does. xas_pause() aligns
xa_offset before its existing calculation, so the resume index stays at
the end of the entry.
If the tree changes while a sibling is being resolved, do not descend
into a node that does not contain xa_index, and do not move xa_index
backwards. Compare the next marked slot with get_offset() instead of
recomputing xa_index with a 32-bit shift. Return XA_RETRY_ENTRY.
xa_find_after() handles that retry before xas_sibling(), so a rewind
that then retries restarts the walk. Callers that use xas_retry()
restart from the same index.
The userspace XArray test suite passed. The kernel was not built.
Fixes: b803b42823d0 ("xarray: Add XArray iterators")
Cc: <stable@vger.kernel.org>
Signed-off-by: Qian Xiao <heyheyco@gmail.com>
---
v3:
- Compare the next marked slot with get_offset() instead of shifting
in 32-bit arithmetic.
- Handle XA_RETRY_ENTRY in xa_find_after() before xas_sibling().
v2:
- Use the slow path in xas_next_marked() when the offset and index disagree.
- Align xa_offset in xas_pause() so the resume index stays at the end of
the entry.
- Return XA_RETRY_ENTRY when a concurrent change would descend into the
wrong node or decrease xa_index.
include/linux/xarray.h | 3 +-
lib/test_xarray.c | 165 +++++++++++++++++++++++++++++++++++++++++
lib/xarray.c | 24 ++++--
3 files changed, 186 insertions(+), 6 deletions(-)
diff --git a/include/linux/xarray.h b/include/linux/xarray.h
index be850174e802..8c42d899d94c 100644
--- a/include/linux/xarray.h
+++ b/include/linux/xarray.h
@@ -1778,7 +1778,8 @@ static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
void *entry;
unsigned int offset;
- if (unlikely(xas_not_node(node) || node->shift))
+ if (unlikely(xas_not_node(node) || node->shift ||
+ xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
return xas_find_marked(xas, max, mark);
offset = xas_find_chunk(xas, true, mark);
xas->xa_offset = offset;
diff --git a/lib/test_xarray.c b/lib/test_xarray.c
index 615f7730a5bd..02e6843d0227 100644
--- a/lib/test_xarray.c
+++ b/lib/test_xarray.c
@@ -1316,6 +1316,170 @@ static noinline void check_multi_find_4(struct xarray *xa)
#endif
}
+static noinline void check_multi_find_marked_nonleaf(struct xarray *xa)
+{
+#ifdef CONFIG_XARRAY_MULTI
+ unsigned int order = XA_CHUNK_SHIFT + 1;
+ unsigned long next = 1UL << order;
+ unsigned long start = next - 1;
+ XA_STATE(xas, xa, start);
+ XA_STATE_ORDER(split, xa, 0, 0);
+ void *entry;
+ unsigned long i;
+
+ /* Multi-index entry in two slots of a non-leaf node, both marked. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+ xa_set_mark(xa, next, XA_MARK_0);
+
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(next));
+ XA_BUG_ON(xa, xas.xa_index != next);
+
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != NULL);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+ XA_BUG_ON(xa, !xa_empty(xa));
+
+ /* Split the multi-index entry after a lookup begins inside it. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+ xa_set_mark(xa, next, XA_MARK_0);
+
+ xas_set(&xas, start);
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+ rcu_read_unlock();
+
+ xas_split_alloc(&split, xa_mk_index(0), order, GFP_KERNEL);
+ if (xas_error(&split)) {
+ XA_BUG_ON(xa, true);
+ goto out;
+ }
+ xas_lock(&split);
+ xas_split(&split, xa_mk_index(0), order);
+ for (i = 0; i < next; i++)
+ __xa_store(xa, i, xa_mk_index(i), 0);
+ xas_unlock(&split);
+
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(next));
+ XA_BUG_ON(xa, xas.xa_index != next);
+
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != NULL);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+
+ /* Nothing marked past the index already walked. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+
+ xas_set(&xas, start);
+ rcu_read_lock();
+ entry = xas_load(&xas);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+ rcu_read_unlock();
+
+ xas_set(&split, 0);
+ xas_split_alloc(&split, xa_mk_index(0), order, GFP_KERNEL);
+ if (xas_error(&split)) {
+ XA_BUG_ON(xa, true);
+ goto out;
+ }
+ xas_lock(&split);
+ xas_split(&split, xa_mk_index(0), order);
+ for (i = 0; i < next; i++)
+ __xa_store(xa, i, xa_mk_index(i), 0);
+ xas_unlock(&split);
+
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != NULL);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+
+ /* Pausing resumes at the next marked entry, without repeating. */
+ xa_store_order(xa, 0, order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL) != NULL);
+ xa_set_mark(xa, next, XA_MARK_0);
+ xas_set(&xas, start);
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != start);
+ xas_pause(&xas);
+ XA_BUG_ON(xa, xas.xa_index != next);
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(next));
+ XA_BUG_ON(xa, xas.xa_index != next);
+ rcu_read_unlock();
+
+ xa_destroy(xa);
+
+ /*
+ * A leaf multi-index entry. The next step must not use a canonical
+ * slot left behind by the first lookup.
+ */
+ if (XA_CHUNK_SHIFT > 2) {
+ unsigned int leaf_order = 2;
+ unsigned long leaf_next = 1UL << leaf_order;
+ unsigned long leaf_start = leaf_next - 1;
+
+ xa_store_order(xa, 0, leaf_order, xa_mk_index(0), GFP_KERNEL);
+ xa_set_mark(xa, 0, XA_MARK_0);
+ XA_BUG_ON(xa, xa_store_index(xa, leaf_next, GFP_KERNEL) != NULL);
+ xa_set_mark(xa, leaf_next, XA_MARK_0);
+
+ xas_set(&xas, leaf_start);
+ rcu_read_lock();
+ entry = xas_find_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(0));
+ XA_BUG_ON(xa, xas.xa_index != leaf_start);
+ rcu_read_unlock();
+
+ xas_set(&split, 0);
+ xas_split_alloc(&split, xa_mk_index(0), leaf_order, GFP_KERNEL);
+ if (xas_error(&split)) {
+ XA_BUG_ON(xa, true);
+ goto out;
+ }
+ xas_lock(&split);
+ xas_split(&split, xa_mk_index(0), leaf_order);
+ for (i = 0; i < leaf_next; i++)
+ __xa_store(xa, i, xa_mk_index(i), 0);
+ xas_unlock(&split);
+
+ rcu_read_lock();
+ entry = xas_next_marked(&xas, ULONG_MAX, XA_MARK_0);
+ XA_BUG_ON(xa, entry != xa_mk_index(leaf_next));
+ XA_BUG_ON(xa, xas.xa_index != leaf_next);
+ rcu_read_unlock();
+ }
+
+out:
+ xa_destroy(xa);
+ XA_BUG_ON(xa, !xa_empty(xa));
+#endif
+}
+
static noinline void check_find_1(struct xarray *xa)
{
unsigned long i, j, k;
@@ -1440,6 +1604,7 @@ static noinline void check_find(struct xarray *xa)
check_multi_find_2(xa);
check_multi_find_3(xa);
check_multi_find_4(xa);
+ check_multi_find_marked_nonleaf(xa);
}
/* See find_swap_entry() in mm/shmem.c */
diff --git a/lib/xarray.c b/lib/xarray.c
index 48f440d114e1..9f29a064849f 100644
--- a/lib/xarray.c
+++ b/lib/xarray.c
@@ -1283,6 +1283,9 @@ void xas_pause(struct xa_state *xas)
if (xas_invalid(xas))
return;
+ if (node && xas->xa_offset != get_offset(xas->xa_index, node))
+ xas->xa_offset = get_offset(xas->xa_index, node);
+
xas->xa_node = XAS_RESTART;
if (node) {
unsigned long offset = xas->xa_offset;
@@ -1495,6 +1498,14 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
}
+ if (advance &&
+ xas->xa_offset != get_offset(xas->xa_index, xas->xa_node)) {
+ if (!xas->xa_node->shift)
+ xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
+ else
+ xas->xa_offset = get_offset(xas->xa_index, xas->xa_node);
+ }
+
while (xas->xa_index <= max) {
if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
xas->xa_offset = xas->xa_node->offset + 1;
@@ -1507,14 +1518,14 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
if (!advance) {
entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
- if (xa_is_sibling(entry)) {
+ if (xa_is_sibling(entry))
xas->xa_offset = xa_to_sibling(entry);
- xas_move_index(xas, xas->xa_offset);
- }
}
offset = xas_find_chunk(xas, advance, mark);
if (offset > xas->xa_offset) {
+ if (offset <= get_offset(xas->xa_index, xas->xa_node))
+ return XA_RETRY_ENTRY;
advance = false;
xas_move_index(xas, offset);
/* Mind the wrap */
@@ -1532,6 +1543,8 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
continue;
if (!xa_is_node(entry))
return entry;
+ if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
+ return XA_RETRY_ENTRY;
xas->xa_node = xa_to_node(entry);
xas_set_offset(xas);
}
@@ -2258,12 +2271,13 @@ void *xa_find_after(struct xarray *xa, unsigned long *indexp,
else
entry = xas_find(&xas, max);
+ if (xas_retry(&xas, entry))
+ continue;
if (xas_invalid(&xas))
break;
if (xas_sibling(&xas))
continue;
- if (!xas_retry(&xas, entry))
- break;
+ break;
}
rcu_read_unlock();
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v3] xarray: fix index jumping backwards in xas_find_marked()
2026-11-27 22:21 ` [PATCH v3] " Qian Xiao
@ 2026-11-28 22:21 ` sashiko-bot
2026-11-29 3:12 ` Andrew Morton
1 sibling, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-11-28 22:21 UTC (permalink / raw)
To: Qian Xiao, Matthew Wilcox, Andrew Morton; +Cc: linux-mm, Krystian Kaniewski
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] xarray: cursor index desync in xas_find_marked() on
concurrent sibling store
--
commit 57aa975de70fddb8babaa9a1b191b30f80289953
Author: Qian Xiao <heyheyco@gmail.com>
xarray: fix index jumping backwards in xas_find_marked()
This commit preserves xas->xa_index when resolving sibling slots in
xas_find_marked(), aligns xas->xa_offset before advancing or pausing, and
returns XA_RETRY_ENTRY when a concurrent tree modification would move the
cursor backwards or descend into the wrong node.
> diff --git a/lib/xarray.c b/lib/xarray.c
> index 8c09f6c6cd8f7..5b68f41480610 100644
> --- a/lib/xarray.c
> +++ b/lib/xarray.c
[ ... ]
> @@ -1512,14 +1523,14 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
>
> if (!advance) {
> entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
> - if (xa_is_sibling(entry)) {
> + if (xa_is_sibling(entry))
> xas->xa_offset = xa_to_sibling(entry);
> - xas_move_index(xas, xas->xa_offset);
> - }
> }
>
> offset = xas_find_chunk(xas, advance, mark);
> if (offset > xas->xa_offset) {
> + if (offset <= get_offset(xas->xa_index, xas->xa_node))
> + return XA_RETRY_ENTRY;
> advance = false;
> xas_move_index(xas, offset);
> /* Mind the wrap */
> @@ -1537,6 +1548,8 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
> continue;
> if (!xa_is_node(entry))
> return entry;
> + if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
> + return XA_RETRY_ENTRY;
> xas->xa_node = xa_to_node(entry);
> xas_set_offset(xas);
> }
[Severity: Medium]
Can a concurrent multi-index store after xas_move_index() desynchronize
xas->xa_offset and xas->xa_index?
CPU0 CPU1
xas_find_marked()
offset = xas_find_chunk(...)
/* offset > xas->xa_offset */
advance = false
xas_move_index(xas, offset)
xas->xa_offset = offset
xas_store()
/* multi-index store [canon..end]
* with canon < offset <= end;
* squashes mark to canon
*/
entry = xa_entry(..., offset)
/* xa_is_sibling(entry) */
continue
/* next iteration: !advance */
xas->xa_offset = canon
offset = xas_find_chunk(..., false, mark)
/* offset == canon == xas->xa_offset */
return entry <-- xa_offset = canon,
xa_index = offset << shift
Because offset == xas->xa_offset on the second iteration, the
if (offset > xas->xa_offset) block is skipped and xas_find_marked()
returns the entry at canon with xas->xa_offset == canon while
xas->xa_index remains at offset << shift.
When the search started at or below canon << shift, xa_find_after() sees
xas_sibling(&xas) as true and skips an unvisited entry after *indexp,
while xa_find() reports offset << shift instead of canon << shift. If
the cursor had already visited an entry at or above canon and entered
with advance == true, rewinding xas->xa_offset to canon also lets
xas_find_marked() return an already-visited entry again, or if a retry is
triggered after xas_move_index() has mutated xas->xa_index, xas_retry()
restarts from offset << shift and skips marked entries below it.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127222110.3755894-1-heyheyco@gmail.com?part=1
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v3] xarray: fix index jumping backwards in xas_find_marked()
2026-11-27 22:21 ` [PATCH v3] " Qian Xiao
2026-11-28 22:21 ` sashiko-bot
@ 2026-11-29 3:12 ` Andrew Morton
2026-11-29 23:52 ` Qian Xiao
1 sibling, 1 reply; 12+ messages in thread
From: Andrew Morton @ 2026-11-29 3:12 UTC (permalink / raw)
To: Qian Xiao
Cc: Matthew Wilcox, linux-fsdevel, linux-mm, linux-kernel,
Krystian Kaniewski
On Fri, 27 Nov 2026 14:21:09 -0800 Qian Xiao <heyheyco@gmail.com> wrote:
> xas_find() keeps xa_index when a lookup starts inside a multi-index
> entry stored in a non-leaf node, and aligns xa_offset with that index
> before advancing. xas_find_marked() still does neither.
>
> Walking from a sibling slot to the canonical slot calls
> xas_move_index(), so a start index such as 127 becomes 0. After the
> cursor has been walked, xa_offset can also stay on the canonical slot
> while xa_index is inside a later slot of the same entry. The next
> xas_find_marked() then recomputes xa_index from the stale offset. If
> the entry is split, the call returns a marked entry below the index
> already reached. xas_find() returns the following entry in the same
> situation.
>
> Resolve the sibling without moving xa_index. Before advancing a cursor
> that has already been walked, set xa_offset from xa_index the same way
> xas_find() does. xas_next_marked() uses that path when the offset and
> index disagree, as xas_next_entry() already does. xas_pause() aligns
> xa_offset before its existing calculation, so the resume index stays at
> the end of the entry.
>
> If the tree changes while a sibling is being resolved, do not descend
> into a node that does not contain xa_index, and do not move xa_index
> backwards. Compare the next marked slot with get_offset() instead of
> recomputing xa_index with a 32-bit shift. Return XA_RETRY_ENTRY.
> xa_find_after() handles that retry before xas_sibling(), so a rewind
> that then retries restarts the walk. Callers that use xas_retry()
> restart from the same index.
What is the relationship between this and Krystian's identically-named
patch?
https://lore.kernel.org/20260904121301.200049-1-krystianmkaniewski@gmail.com
> The userspace XArray test suite passed.
> The kernel was not built.
Well that isn't encouraging :(
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v3] xarray: fix index jumping backwards in xas_find_marked()
2026-11-29 3:12 ` Andrew Morton
@ 2026-11-29 23:52 ` Qian Xiao
2026-12-03 1:06 ` Andrew Morton
0 siblings, 1 reply; 12+ messages in thread
From: Qian Xiao @ 2026-11-29 23:52 UTC (permalink / raw)
To: Andrew Morton
Cc: Matthew Wilcox, linux-fsdevel, linux-mm, linux-kernel,
Krystian Kaniewski
On Sat, 28 Nov 2026 19:12:03 -0800 Andrew Morton wrote:
> What is the relationship between this and Krystian's identically-named
> patch?
Thanks for taking a look. Krystian's patch is a4e667346a8d. It only
changes xas_find(). This is the same fix for xas_find_marked(), and it
doesn't touch xas_find().
> The kernel was not built.
>
> Well that isn't encouraging :(
Fair point. I've since built an x86_64 defconfig vmlinux
successfully, without errors.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v3] xarray: fix index jumping backwards in xas_find_marked()
2026-11-29 23:52 ` Qian Xiao
@ 2026-12-03 1:06 ` Andrew Morton
2026-12-03 18:31 ` Qian Xiao
0 siblings, 1 reply; 12+ messages in thread
From: Andrew Morton @ 2026-12-03 1:06 UTC (permalink / raw)
To: Qian Xiao
Cc: Matthew Wilcox, linux-fsdevel, linux-mm, linux-kernel,
Krystian Kaniewski
On Sun, 29 Nov 2026 15:52:55 -0800 Qian Xiao <heyheyco@gmail.com> wrote:
> On Sat, 28 Nov 2026 19:12:03 -0800 Andrew Morton wrote:
> > What is the relationship between this and Krystian's identically-named
> > patch?
>
> Thanks for taking a look. Krystian's patch is a4e667346a8d. It only
> changes xas_find(). This is the same fix for xas_find_marked(), and it
> doesn't touch xas_find().
>
> > The kernel was not built.
> >
> > Well that isn't encouraging :(
>
> Fair point. I've since built an x86_64 defconfig vmlinux
> successfully, without errors.
The bug is legit and your patch (after fixing the Sashiko thing) fixes
it.
I'm struggling to figure out the userspace impact of the bug. It seems
to be minor. afaict it's
__folio_mark_dirty() -> set PAGECACHE_TAG_DIRTY
WB_SYNC_ALL or tagged writeback -> transform PAGECACHE_TAG_DIRTY into
PAGECACHE_TAG_TOWRITE
writeback_iter() -> start IO on PAGECACHE_TAG_TOWRITE folios
concurrent split -> uh oh, this makes the marked scan of
mapping->i_pages move its xas.xa_index backwards. This can consume
background writeback's page budget before it reaches later dirty
folios.
I could reproduce the backward jumping in test_xarray and in a kernel
module scanning a real tmpfs page-cache XArray. The module set
PAGECACHE_TAG_DIRTY itself - I couldn't reproduce the
writeback-didn't-write-enough-stuff thing in actual testing.
It turns out that there's more to the world than mm/, but Token Taxing
Terrorist couldn't convincingly find any other xarray users which are
impacted by this. DAX and memfd are "maybe, possibly".
I've temporarily turned your patch into a series of three:
1: add the test
2: add your fix
3: add a (needed) -fix for the Sashiko thing
I like to stage the test before the fix for simplifying the
test-without/test-with testing. Shall scrunch everything
together later.
Sashiko -fix:
--- a/lib/xarray.c~xarray-fix-index-jumping-backwards-in-xas_find_marked-fix
+++ a/lib/xarray.c
@@ -1544,8 +1544,10 @@ void *xas_find_marked(struct xa_state *x
entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
if (!entry && !(xa_track_free(xas->xa) && mark == XA_FREE_MARK))
continue;
- if (xa_is_sibling(entry))
+ if (xa_is_sibling(entry)) {
+ advance = true;
continue;
+ }
if (!xa_is_node(entry))
return entry;
if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
_
^ permalink raw reply [flat|nested] 12+ messages in thread
* [syzbot ci] Re: xarray: fix index jumping backwards in xas_find_marked()
2026-11-26 19:08 ` [PATCH v2] " Qian Xiao
2026-11-27 19:09 ` sashiko-bot
2026-11-27 22:21 ` [PATCH v3] " Qian Xiao
@ 2026-12-03 7:00 ` syzbot ci
2026-12-03 7:13 ` Andrew Morton
2 siblings, 1 reply; 12+ messages in thread
From: syzbot ci @ 2026-12-03 7:00 UTC (permalink / raw)
To: akpm, heyheyco, krystianmkaniewski, linux-fsdevel, linux-kernel,
linux-mm, willy
Cc: syzbot, syzkaller-bugs
syzbot ci has tested the following series
[v2] xarray: fix index jumping backwards in xas_find_marked()
https://lore.kernel.org/all/20261126190847.3064508-1-heyheyco@gmail.com
* [PATCH v2] xarray: fix index jumping backwards in xas_find_marked()
and found the following issue:
UBSAN: shift-out-of-bounds in xa_find
Full report is available here:
https://ci.syzbot.org/series/c1effee3-f992-4c4c-a1f3-63e64e1fa823
***
UBSAN: shift-out-of-bounds in xa_find
tree: mm-new
URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/akpm/mm.git
base: 7903e5d4dcaa83ff649ac1573619e7dc35c84116
arch: amd64
compiler: Debian clang version 22.1.8 (1~deb13u5), Debian LLD 22.1.8
config: https://ci.syzbot.org/builds/5a93f427-8e6a-4190-863a-de5526fa8d7c/config
------------[ cut here ]------------
UBSAN: shift-out-of-bounds in lib/xarray.c:1530:20
shift exponent 60 is too large for 32-bit type 'unsigned int'
CPU: 0 UID: 0 PID: 35 Comm: kworker/0:2 Not tainted syzkaller #0 PREEMPT(full)
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
Workqueue: events debugfs_reap_work
Call Trace:
<TASK>
dump_stack_lvl+0xe8/0x150
ubsan_epilogue+0xa/0x30
__ubsan_handle_shift_out_of_bounds+0x36d/0x400
xas_find_marked+0x106f/0x15b0
xa_find+0x17a/0x2b0
debugfs_reap_work+0x97/0x210
process_scheduled_works+0xc5d/0x1650
worker_thread+0xa47/0xfb0
kthread+0x38b/0x480
ret_from_fork+0x514/0xb70
ret_from_fork_asm+0x1a/0x30
</TASK>
---[ end trace ]---
Kernel panic - not syncing: UBSAN: panic_on_warn set ...
CPU: 0 UID: 0 PID: 35 Comm: kworker/0:2 Not tainted syzkaller #0 PREEMPT(full)
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
Workqueue: events debugfs_reap_work
Call Trace:
<TASK>
vpanic+0x26f/0x710
panic+0xc5/0xd0
check_panic_on_warn+0x89/0xb0
__ubsan_handle_shift_out_of_bounds+0x36d/0x400
xas_find_marked+0x106f/0x15b0
xa_find+0x17a/0x2b0
debugfs_reap_work+0x97/0x210
process_scheduled_works+0xc5d/0x1650
worker_thread+0xa47/0xfb0
kthread+0x38b/0x480
ret_from_fork+0x514/0xb70
ret_from_fork_asm+0x1a/0x30
</TASK>
Kernel Offset: disabled
Rebooting in 86400 seconds..
***
If these findings have caused you to resend the series or submit a
separate fix, please add the following tag to your commit message:
Tested-by: syzbot@syzkaller.appspotmail.com
---
This report is generated by a bot. It may contain errors.
syzbot ci engineers can be reached at syzkaller@googlegroups.com.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [syzbot ci] Re: xarray: fix index jumping backwards in xas_find_marked()
2026-12-03 7:00 ` [syzbot ci] " syzbot ci
@ 2026-12-03 7:13 ` Andrew Morton
0 siblings, 0 replies; 12+ messages in thread
From: Andrew Morton @ 2026-12-03 7:13 UTC (permalink / raw)
To: syzbot ci
Cc: heyheyco, krystianmkaniewski, linux-fsdevel, linux-kernel,
linux-mm, willy, syzbot, syzkaller-bugs
On Wed, 02 Dec 2026 23:00:03 -0800 syzbot ci <syzbot+cif1b1747c51179b79@syzkaller.appspotmail.com> wrote:
> syzbot ci has tested the following series
>
> [v2] xarray: fix index jumping backwards in xas_find_marked()
That's the v2 patch. mm-new has v3.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v3] xarray: fix index jumping backwards in xas_find_marked()
2026-12-03 1:06 ` Andrew Morton
@ 2026-12-03 18:31 ` Qian Xiao
0 siblings, 0 replies; 12+ messages in thread
From: Qian Xiao @ 2026-12-03 18:31 UTC (permalink / raw)
To: Andrew Morton
Cc: Matthew Wilcox, linux-fsdevel, linux-mm, linux-kernel,
Krystian Kaniewski
On Wed, 2 Dec 2026 17:06:44 -0800 Andrew Morton wrote:
> The bug is legit and your patch (after fixing the Sashiko thing) fixes
> it.
Thanks for reproducing this, including with a tmpfs page-cache XArray.
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-12-03 18:32 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 21:34 [PATCH] xarray: fix index jumping backwards in xas_find_marked() Qian Xiao
2026-11-25 21:34 ` sashiko-bot
2026-11-26 19:08 ` [PATCH v2] " Qian Xiao
2026-11-27 19:09 ` sashiko-bot
2026-11-27 22:21 ` [PATCH v3] " Qian Xiao
2026-11-28 22:21 ` sashiko-bot
2026-11-29 3:12 ` Andrew Morton
2026-11-29 23:52 ` Qian Xiao
2026-12-03 1:06 ` Andrew Morton
2026-12-03 18:31 ` Qian Xiao
2026-12-03 7:00 ` [syzbot ci] " syzbot ci
2026-12-03 7:13 ` Andrew Morton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox