* [PATCH 2/3] xarray: fix index jumping backwards in xas_find_marked()
2026-12-03 1:25 [PATCH 0/3] xarray: fix marked iterator index after multi-index entries Andrew Morton
@ 2026-12-03 1:26 ` Andrew Morton
0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2026-12-03 1:26 UTC (permalink / raw)
To: Matthew Wilcox; +Cc: linux-mm, Andrew Morton, Qian Xiao, stable
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();
_
^ permalink raw reply [flat|nested] 2+ messages in thread