Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Matthew Wilcox <willy@infradead.org>
Cc: linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
	Qian Xiao <heyheyco@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH 2/3] xarray: fix index jumping backwards in xas_find_marked()
Date: Wed, 02 Dec 2026 17:26:05 -0800	[thread overview]
Message-ID: <b0bf98a720f546f69ae25ce7a9ebf702.2@linux-foundation.org> (raw)
In-Reply-To: <b0bf98a720f546f69ae25ce7a9ebf702.0@linux-foundation.org>

From: Qian Xiao <heyheyco@gmail.com>

A page-cache scan starting inside a marked large folio can move its
index backwards. If that folio is split during the scan,
filemap_get_folios_tag() can return newly split folios before the
requested range. Background writeback can spend its finite page
budget on those folios and leave dirty folios in the requested range
for a later pass. This can increase dirty-page throttling for writers.
Folios under writeback cannot be split, and this review has not found
a case where a data-integrity sync reports success without writing
the requested dirty data.

xas_find_marked() can move xa_index backwards to the canonical slot
of a multi-index entry. After the cursor is walked, xa_offset can
also disagree with xa_index. If that entry is split, the next search
can return a marked entry below the index already reached.

Keep xa_index while resolving a sibling. Align xa_offset before
advancing or pausing, and make xas_next_marked() use the slower path
when the two disagree. Return XA_RETRY_ENTRY if a changed tree would
move the index backwards or descend into the wrong node. Have
xa_find_after() handle the retry before its sibling check.

Link: https://patch.msgid.link/20261127222110.3755894-1-heyheyco@gmail.com
Fixes: b803b42823d0 ("xarray: Add XArray iterators")
Signed-off-by: Qian Xiao <heyheyco@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: <stable@vger.kernel.org>

 include/linux/xarray.h |    3 ++-
 lib/xarray.c           |   24 +++++++++++++++++++-----
 2 files changed, 21 insertions(+), 6 deletions(-)

--- a/include/linux/xarray.h~xarray-fix-index-jumping-backwards-in-xas_find_marked
+++ a/include/linux/xarray.h
@@ -1778,7 +1778,8 @@ static inline void *xas_next_marked(stru
 	void *entry;
 	unsigned int offset;
 
-	if (unlikely(xas_not_node(node) || node->shift))
+	if (unlikely(xas_not_node(node) || node->shift ||
+			xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
 		return xas_find_marked(xas, max, mark);
 	offset = xas_find_chunk(xas, true, mark);
 	xas->xa_offset = offset;
--- a/lib/xarray.c~xarray-fix-index-jumping-backwards-in-xas_find_marked
+++ a/lib/xarray.c
@@ -1288,6 +1288,9 @@ void xas_pause(struct xa_state *xas)
 	if (xas_invalid(xas))
 		return;
 
+	if (node && xas->xa_offset != get_offset(xas->xa_index, node))
+		xas->xa_offset = get_offset(xas->xa_index, node);
+
 	xas->xa_node = XAS_RESTART;
 	if (node) {
 		unsigned long offset = xas->xa_offset;
@@ -1500,6 +1503,14 @@ void *xas_find_marked(struct xa_state *x
 		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;
@@ -1512,14 +1523,14 @@ void *xas_find_marked(struct xa_state *x
 
 		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 *x
 			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);
 	}
@@ -2263,12 +2276,13 @@ void *xa_find_after(struct xarray *xa, u
 		else
 			entry = xas_find(&xas, max);
 
+		if (xas_retry(&xas, entry))
+			continue;
 		if (xas_invalid(&xas))
 			break;
 		if (xas_sibling(&xas))
 			continue;
-		if (!xas_retry(&xas, entry))
-			break;
+		break;
 	}
 	rcu_read_unlock();
 
_


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

Thread overview: 4+ 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 ` Andrew Morton [this message]
2026-12-03  1:26 ` [PATCH 3/3] xarray: keep advancing after a concurrent sibling store Andrew Morton

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=b0bf98a720f546f69ae25ce7a9ebf702.2@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=heyheyco@gmail.com \
    --cc=linux-mm@kvack.org \
    --cc=stable@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox