From: sashiko-bot@kernel.org
To: "Qian Xiao" <heyheyco@gmail.com>,
"Matthew Wilcox" <willy@infradead.org>,
"Andrew Morton" <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org, "Krystian Kaniewski" <krystianmkaniewski@gmail.com>
Subject: Re: [PATCH] xarray: fix index jumping backwards in xas_find_marked()
Date: Wed, 25 Nov 2026 21:34:25 +0000 [thread overview]
Message-ID: <sashiko-outbox-234186@kernel.org> (raw)
In-Reply-To: <20261124213409.2224472-1-heyheyco@gmail.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] xarray: race with split after sibling resolution in
xas_find_marked()
- [High] xarray: missing xa_offset alignment check in xas_next_marked()
- [High] xarray: xas_pause() overshoots xa_index after xas_find_marked()
--
commit 4eb10c5fe97f140df6410177fd9785f2f1edc4d2
Author: Qian Xiao <heyheyco@gmail.com>
xarray: fix index jumping backwards in xas_find_marked()
This commit keeps xas->xa_index unchanged when xas_find_marked() resolves a
sibling slot to its canonical slot, and realigns xas->xa_offset with
xas->xa_index before advancing on subsequent calls.
> 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 realignment when
iterating with xas_for_each_marked()?
When xas_find_marked() starts at a sibling index inside a leaf node
(!node->shift), it now returns with xas->xa_offset on the canonical slot
while xas->xa_index remains at the sibling index. Unlike xas_next_entry(),
which checks xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK) before using
its leaf fast path, xas_next_marked() only checks node->shift:
include/linux/xarray.h:xas_next_marked() {
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;
...
}
If a leaf-level multi-index entry is split after the initial lookup, the
next step in xas_for_each_marked() searches from the canonical
xas->xa_offset + 1 and rewinds xas->xa_index backwards. Adding
xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK) to the fallback condition
in xas_next_marked() would match xas_next_entry().
>
> 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 rewinding xas->xa_offset to the canonical slot without updating
xas->xa_index corrupt the cursor if a concurrent split occurs before the
slot is reloaded?
CPU0 CPU1
xas_find_marked() [xa_index = 127]
/* shift-6 node,xa_offset = 1 */
entry = xa_entry(..., 1)
if (xa_is_sibling(entry))
xas->xa_offset = 0
xas_split()
/* replaces slots 0 and 1 with
* marked child nodes child0
* (0..63) and child1 (64..127)
*/
offset = xas_find_chunk(..., false, ...)
/* returns 0; offset > xa_offset
* is false, xa_index stays 127
*/
entry = xa_entry(..., 0)
/* loads child0 */
xas->xa_node = xa_to_node(entry)
xas_set_offset(xas)
/* descends into child0[63] with
* xa_index == 127
*/ <-- returns entry for index 63
with xa_index == 127
Because xas_reload() only checks the offset within xas->xa_node, callers
such as find_get_entry() reload child0->slots[63] and do not detect the
mismatched subtree. On the next call, climbing out of child0 via
child0->offset + 1 moves xas->xa_index backwards to 64. Similarly, if slot
0 is no longer marked after the split while slot 1 is marked,
xas_find_chunk() returns offset 1, which satisfies offset > xas->xa_offset
and calls xas_move_index(xas, 1), zeroing the low bits of xas->xa_index and
rewinding it from 127 to 64.
[Severity: High]
Does leaving xas->xa_offset on the canonical slot while xas->xa_index holds
a sibling index also cause xas_pause() to overshoot the next entry?
lib/xarray.c:xas_pause() {
...
if (node) {
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;
...
}
xas_pause() masks off only the bits below node->shift and adds
(offset - xas->xa_offset) << node->shift, assuming get_offset(xas->xa_index,
node) equals xas->xa_offset. When xas_find_marked() matches an entry covering
slots 0..1 at start index 127 (slot 1 in a shift-6 node), it returns with
xas->xa_offset == 0 and xas->xa_index == 127. A subsequent xas_pause()
computes (127 & ~63UL) + ((2 - 0) << 6) = 192 instead of 128, causing callers
such as tag_pages_for_writeback() to skip marked entries in indices 128..191.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124213409.2224472-1-heyheyco@gmail.com?part=1
next prev parent reply other threads:[~2026-11-25 21:34 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 21:34 [PATCH] xarray: fix index jumping backwards in xas_find_marked() Qian Xiao
2026-11-25 21:34 ` sashiko-bot [this message]
-- strict thread matches above, loose matches on Subject: below --
2026-11-24 21:34 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=sashiko-outbox-234186@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=heyheyco@gmail.com \
--cc=krystianmkaniewski@gmail.com \
--cc=linux-mm@kvack.org \
--cc=sashiko-reviews@lists.linux.dev \
--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.