Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
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:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-234187@kernel.org> (raw)
In-Reply-To: <20261124213424.2224554-1-heyheyco@gmail.com>

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


  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-234187@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox