Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Andrew Morton" <akpm@linux-foundation.org>,
	"Matthew Wilcox" <willy@infradead.org>
Cc: linux-mm@kvack.org, "Qian Xiao" <heyheyco@gmail.com>
Subject: Re: [PATCH 2/3] xarray: fix index jumping backwards in xas_find_marked()
Date: Fri, 04 Dec 2026 01:26:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-248890@kernel.org> (raw)
In-Reply-To: <b0bf98a720f546f69ae25ce7a9ebf702.2@linux-foundation.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] xarray: missing retry check for in-place split in
  xas_find_marked()
--

commit 0b16d0644fcf9d5b7e4eae2d0ccfab3c8c7a45b3
Author: Qian Xiao <heyheyco@gmail.com>

xarray: fix index jumping backwards in xas_find_marked()

This commit keeps xa_index unchanged when resolving a sibling slot in
xas_find_marked(), realigns xa_offset with xa_index before advancing or
pausing, and returns XA_RETRY_ENTRY if a concurrent tree change would move
the search backwards or descend into the wrong node.

Link: https://patch.msgid.link/20261127222110.3755894-1-heyheyco@gmail.com

> diff --git a/lib/xarray.c b/lib/xarray.c
> index 8c09f6c6cd8f7..5b68f41480610 100644
> --- a/lib/xarray.c
> +++ b/lib/xarray.c

[ ... ]

> @@ -1512,14 +1523,14 @@ 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) {
> +			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 *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;
>  		xas->xa_node = xa_to_node(entry);
>  		xas_set_offset(xas);
>  	}

[Severity: Medium]
Can xas_find_marked() still return a split entry from a lower slot or skip
the entry at xas->xa_index when a marked multi-index entry is split in
place?

Suppose a marked multi-index entry covers slots 0..3 of a node and a search
starts at index 3 with advance set to false:

CPU0                                  CPU1
xas_find_marked()
  entry = xa_entry(..., 3) (sibling 0)
  xas->xa_offset = 0
                                      xas_split() / xas_try_split()
                                        splits slots 0..3 in place;
                                        slot 0 keeps mark and holds E0
  offset = xas_find_chunk(..., false, mark)
    returns 0 (not > xas->xa_offset)
  entry = xa_entry(..., 0) (E0)
  if (!xa_is_node(entry))
    return E0;  <-- returns slot 0 with xa_index == 3, xa_offset == 0

Because offset equals xas->xa_offset, the first XA_RETRY_ENTRY check inside
if (offset > xas->xa_offset) is bypassed, and because E0 is not a node,
xas_find_marked() returns E0 before reaching the second XA_RETRY_ENTRY
check.

In xas_for_each_marked(), the caller receives E0 from slot 0 (below the
starting index 3), and the next iteration realigns xas->xa_offset to
xas->xa_index (3) with advance set to true, advancing to slot 4 and
skipping the marked entry at slot 3.  In xa_find_after(), xas_sibling()
sees xas->xa_offset < get_offset(xas->xa_index, xas->xa_node), treats E0 as
a sibling covering index 3, and loops again, also skipping slot 3.

Similarly, if slot 0 is concurrently erased after xas_find_chunk() sees the
mark, xa_entry() reads NULL and hits continue with advance still false and
xas->xa_offset at 0.  Follow-up commit "xarray: keep advancing after a
concurrent sibling store" sets advance = true on the xa_is_sibling(entry)
branch, but does not cover the non-node return or the NULL continue branch
when xas->xa_offset != get_offset(xas->xa_index, xas->xa_node).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/b0bf98a720f546f69ae25ce7a9ebf702.0@linux-foundation.org?part=2


  reply	other threads:[~2026-12-04  1:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-04  1:26   ` sashiko-bot [this message]
2026-12-03  1:26 ` [PATCH 3/3] xarray: keep advancing after a concurrent sibling store Andrew Morton
2026-12-04  1:26   ` 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-248890@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=heyheyco@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