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 v2] xarray: fix index jumping backwards in xas_find_marked()
Date: Fri, 27 Nov 2026 19:09:11 +0000 [thread overview]
Message-ID: <sashiko-outbox-238143@kernel.org> (raw)
In-Reply-To: <20261126190847.3064508-1-heyheyco@gmail.com>
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
next prev parent reply other threads:[~2026-11-27 19:09 UTC|newest]
Thread overview: 8+ 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
2026-11-26 19:08 ` [PATCH v2] " Qian Xiao
2026-11-27 19:09 ` sashiko-bot [this message]
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
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-238143@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.