* [PATCH 0/3] xarray: fix marked iterator index after multi-index entries
@ 2026-12-03 1:25 Andrew Morton
2026-12-03 1:26 ` [PATCH 1/3] lib/test_xarray: check marked iteration across " Andrew Morton
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Andrew Morton @ 2026-12-03 1:25 UTC (permalink / raw)
To: Matthew Wilcox; +Cc: linux-mm, Andrew Morton, Qian Xiao, stable
A marked XArray lookup can move its index backwards when the search starts
in a multi-index entry. A later search can use an offset that no longer
agrees with the index and return an entry already passed.
Add tests for lookup, splitting, pausing and advancing across marked
multi-index entries. Then apply Qian Xiao's iterator fix and prevent a
concurrent sibling store from sending an advancing search backwards.
Qian Xiao (2):
lib/test_xarray: check marked iteration across multi-index entries
xarray: fix index jumping backwards in xas_find_marked()
Andrew Morton (1):
xarray: keep advancing after a concurrent sibling store
include/linux/xarray.h | 3 +-
lib/test_xarray.c | 165 ++++++++++++++++++++++++++++++++++++++++++++
lib/xarray.c | 28 ++++++--
3 files changed, 189 insertions(+), 7 deletions(-)
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/3] lib/test_xarray: check marked iteration across multi-index entries
2026-12-03 1:25 [PATCH 0/3] xarray: fix marked iterator index after multi-index entries Andrew Morton
@ 2026-12-03 1:26 ` Andrew Morton
2026-12-03 1:26 ` [PATCH 2/3] xarray: fix index jumping backwards in xas_find_marked() Andrew Morton
2026-12-03 1:26 ` [PATCH 3/3] xarray: keep advancing after a concurrent sibling store Andrew Morton
2 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-12-03 1:26 UTC (permalink / raw)
To: Matthew Wilcox; +Cc: linux-mm, Andrew Morton, Qian Xiao
From: Qian Xiao <heyheyco@gmail.com>
Add Qian's test cases first. They check the index and returned entry
when a lookup starts inside a marked multi-index entry, after that
entry is split, and when the walk is paused or continued. They expose
the existing index and offset mismatch before the iterator changes.
Link: https://patch.msgid.link/20261127222110.3755894-1-heyheyco@gmail.com
Signed-off-by: Qian Xiao <heyheyco@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
lib/test_xarray.c | 165 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 165 insertions(+)
--- a/lib/test_xarray.c~lib-test_xarray-check-marked-iteration-across-multi-index-entries
+++ a/lib/test_xarray.c
@@ -1348,6 +1348,170 @@ out:
#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;
@@ -1472,6 +1636,7 @@ static noinline void check_find(struct x
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 */
_
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/3] xarray: fix index jumping backwards in xas_find_marked()
2026-12-03 1:25 [PATCH 0/3] xarray: fix marked iterator index after multi-index entries Andrew Morton
2026-12-03 1:26 ` [PATCH 1/3] lib/test_xarray: check marked iteration across " Andrew Morton
@ 2026-12-03 1:26 ` Andrew Morton
2026-12-03 1:26 ` [PATCH 3/3] xarray: keep advancing after a concurrent sibling store Andrew Morton
2 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-12-03 1:26 UTC (permalink / raw)
To: Matthew Wilcox; +Cc: linux-mm, Andrew Morton, Qian Xiao, stable
From: Qian Xiao <heyheyco@gmail.com>
A page-cache scan starting inside a marked large folio can move its
index backwards. If that folio is split during the scan,
filemap_get_folios_tag() can return newly split folios before the
requested range. Background writeback can spend its finite page
budget on those folios and leave dirty folios in the requested range
for a later pass. This can increase dirty-page throttling for writers.
Folios under writeback cannot be split, and this review has not found
a case where a data-integrity sync reports success without writing
the requested dirty data.
xas_find_marked() can move xa_index backwards to the canonical slot
of a multi-index entry. After the cursor is walked, xa_offset can
also disagree with xa_index. If that entry is split, the next search
can return a marked entry below the index already reached.
Keep xa_index while resolving a sibling. Align xa_offset before
advancing or pausing, and make xas_next_marked() use the slower path
when the two disagree. Return XA_RETRY_ENTRY if a changed tree would
move the index backwards or descend into the wrong node. Have
xa_find_after() handle the retry before its sibling check.
Link: https://patch.msgid.link/20261127222110.3755894-1-heyheyco@gmail.com
Fixes: b803b42823d0 ("xarray: Add XArray iterators")
Signed-off-by: Qian Xiao <heyheyco@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: <stable@vger.kernel.org>
include/linux/xarray.h | 3 ++-
lib/xarray.c | 24 +++++++++++++++++++-----
2 files changed, 21 insertions(+), 6 deletions(-)
--- a/include/linux/xarray.h~xarray-fix-index-jumping-backwards-in-xas_find_marked
+++ a/include/linux/xarray.h
@@ -1778,7 +1778,8 @@ static inline void *xas_next_marked(stru
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;
--- a/lib/xarray.c~xarray-fix-index-jumping-backwards-in-xas_find_marked
+++ a/lib/xarray.c
@@ -1288,6 +1288,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;
@@ -1500,6 +1503,14 @@ void *xas_find_marked(struct xa_state *x
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;
@@ -1512,14 +1523,14 @@ void *xas_find_marked(struct xa_state *x
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 *x
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);
}
@@ -2263,12 +2276,13 @@ void *xa_find_after(struct xarray *xa, u
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();
_
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 3/3] xarray: keep advancing after a concurrent sibling store
2026-12-03 1:25 [PATCH 0/3] xarray: fix marked iterator index after multi-index entries Andrew Morton
2026-12-03 1:26 ` [PATCH 1/3] lib/test_xarray: check marked iteration across " Andrew Morton
2026-12-03 1:26 ` [PATCH 2/3] xarray: fix index jumping backwards in xas_find_marked() Andrew Morton
@ 2026-12-03 1:26 ` Andrew Morton
2 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-12-03 1:26 UTC (permalink / raw)
To: Matthew Wilcox; +Cc: linux-mm, Andrew Morton, Qian Xiao
An RCU reader in xas_find_marked() can select a marked slot and move
xa_index to it. A concurrent multi-index store can then replace that slot
with a sibling. The reader sees the sibling, loops, and resolves it to
its canonical slot. That leaves xa_offset behind xa_index and can return
an entry already passed. xa_find_after() can mistake the result for a
sibling of its starting entry and skip it.
Once the search has selected a later marked slot, keep advancing when that
slot has become a sibling. The next search starts after the slot already
reached instead of resolving it to a lower canonical slot. RCU iteration
may miss an entry changed concurrently, but must not move its cursor
backwards or repeatedly return an old entry.
Link: https://lore.kernel.org/r/20261127222110.3755894-1-heyheyco@gmail.com
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: <sashiko-bot@kernel.org>
Closes: <https://lore.kernel.org/sashiko-outbox-239089@kernel.org>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Qian Xiao <heyheyco@gmail.com>
lib/xarray.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
--- 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] 4+ messages in thread
end of thread, other threads:[~2026-12-03 1:26 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 1:25 [PATCH 0/3] xarray: fix marked iterator index after multi-index entries Andrew Morton
2026-12-03 1:26 ` [PATCH 1/3] lib/test_xarray: check marked iteration across " Andrew Morton
2026-12-03 1:26 ` [PATCH 2/3] xarray: fix index jumping backwards in xas_find_marked() Andrew Morton
2026-12-03 1:26 ` [PATCH 3/3] xarray: keep advancing after a concurrent sibling store 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.