Re: [PATCH v3] xarray: fix index jumping backwards in xas_find_marked()

From: Andrew Morton

Date: Wed Dec 02 2026 - 20:06:48 EST


On Sun, 29 Nov 2026 15:52:55 -0800 Qian Xiao <heyheyco@xxxxxxxxx> wrote:

> On Sat, 28 Nov 2026 19:12:03 -0800 Andrew Morton wrote:
> > What is the relationship between this and Krystian's identically-named
> > patch?
>
> Thanks for taking a look. Krystian's patch is a4e667346a8d. It only
> changes xas_find(). This is the same fix for xas_find_marked(), and it
> doesn't touch xas_find().
>
> > The kernel was not built.
> >
> > Well that isn't encouraging :(
>
> Fair point. I've since built an x86_64 defconfig vmlinux
> successfully, without errors.

The bug is legit and your patch (after fixing the Sashiko thing) fixes
it.

I'm struggling to figure out the userspace impact of the bug. It seems
to be minor. afaict it's

__folio_mark_dirty() -> set PAGECACHE_TAG_DIRTY

WB_SYNC_ALL or tagged writeback -> transform PAGECACHE_TAG_DIRTY into
PAGECACHE_TAG_TOWRITE

writeback_iter() -> start IO on PAGECACHE_TAG_TOWRITE folios

concurrent split -> uh oh, this makes the marked scan of
mapping->i_pages move its xas.xa_index backwards. This can consume
background writeback's page budget before it reaches later dirty
folios.

I could reproduce the backward jumping in test_xarray and in a kernel
module scanning a real tmpfs page-cache XArray. The module set
PAGECACHE_TAG_DIRTY itself - I couldn't reproduce the
writeback-didn't-write-enough-stuff thing in actual testing.


It turns out that there's more to the world than mm/, but Token Taxing
Terrorist couldn't convincingly find any other xarray users which are
impacted by this. DAX and memfd are "maybe, possibly".


I've temporarily turned your patch into a series of three:

1: add the test
2: add your fix
3: add a (needed) -fix for the Sashiko thing

I like to stage the test before the fix for simplifying the
test-without/test-with testing. Shall scrunch everything
together later.


Sashiko -fix:

--- a/lib/xarray.c~xarray-fix-index-jumping-backwards-in-xas_find_marked-fix
+++ a/lib/xarray.c
@@ -1544,8 +1544,10 @@ void *xas_find_marked(struct xa_state *x
entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
if (!entry && !(xa_track_free(xas->xa) && mark == XA_FREE_MARK))
continue;
- if (xa_is_sibling(entry))
+ if (xa_is_sibling(entry)) {
+ advance = true;
continue;
+ }
if (!xa_is_node(entry))
return entry;
if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
_