From: Qian Xiao <heyheyco@gmail.com>
To: Matthew Wilcox <willy@infradead.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org,
Krystian Kaniewski <krystianmkaniewski@gmail.com>,
Qian Xiao <heyheyco@gmail.com>
Subject: [PATCH] xarray: fix index jumping backwards in xas_find_marked()
Date: Tue, 24 Nov 2026 13:34:24 -0800 [thread overview]
Message-ID: <20261124213424.2224554-1-heyheyco@gmail.com> (raw)
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
next reply other threads:[~2026-11-24 21:34 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 21:34 Qian Xiao [this message]
2026-11-25 21:34 ` [PATCH] xarray: fix index jumping backwards in xas_find_marked() 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
-- strict thread matches above, loose matches on Subject: below --
2026-11-24 21:34 [PATCH] " Qian Xiao
2026-11-25 21:34 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124213424.2224554-1-heyheyco@gmail.com \
--to=heyheyco@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=krystianmkaniewski@gmail.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.