From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796261166; b=KRXm3cDxB4i+/ZWNB/I2Rh6sOfItLSHGn+pnuOTzVS1KPOtChT5n07PL5egjXMwgOz1N 0O9VHkUS7FOyVuMnL0SbpeZ+olAf6yEszPwfjcsNBJBWmXQdcAmCYIwavq8euR3X7x5rT FU58GFyvdL01dquQa9uFz/nMMcJWtqYPabxX8C6LX/QdyX/UgMtXsewbKMMvCNuWwJ0qP ikbyyThiW7c7ARrieODa/rcC2SPFoxjPpwmkG7/N97j2NJt9FjVH4E+yY1rJX5ghXSIqE GCaFkoGgfHHZT3PH+VAHat+MpIdAcjRw9WEsDHZa5jbF/UwI2SqDf+gB1gvAZZ/KOtg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796261166; h=Received:DKIM-Signature:Date:To:Cc:From:Message-Id:In-Reply-To: References:Subject; bh=mDPsemAMSsQh8LRvyqMeOyNCnxiSczSJ+VOwtbYQlvs=; b=3reMTSLHLrslQJlTarFOVsHfXKSPM5vbxCXyzh6eeNWOk6Uq7wM4LKSUz6A8FM0y+k4A mlpnqf5Eqdl/PVETPgMALDzAn3fa6APoedO/bwD3XM+ZRmhVo46xUBy/KgpRtswsQa9R9 Qo3MTBfqMy18VS+bG08gqha3EDHjx0ZUSy4LO3Za9ybDp1T8oAZrXy130v42Ggo2W9GBM OGEK8bRheLzkpfQqxzwHVbWOx2eEv6CC7GudAug6RX5dyNAFhQgsV5bTNz/7sTjct7qSQ WvKhBaF9MaBPMeO+VmkLUYoYhbTp/0Dou2OfaLmxSowpUGavAimAfiIq1ffptYLCsDw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=P2oGkuBC; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C25A606A322 for ; Thu, 3 Dec 2026 01:26:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=P2oGkuBC Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A4281F000C1; Thu, 3 Dec 2026 01:26:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796261165; bh=mDPsemAMSsQh8LRvyqMeOyNCnxiSczSJ+VOwtbYQlvs=; h=Date:To:Cc:From:In-Reply-To:References:Subject; b=P2oGkuBCMQMkxWnFXTbSKGzDnUi1ijCjGh7jgoIb4uWi9Oro3YNRnyBt98TyD6100 NuzjGuiAay+0FFCLx6bNrOCajnTYXNV7MecJASUj9pJyKHZZZZ0/E3McMXCU2NyIFx XvOKfNJtNAyocyddn4N2Znn2Ji8EysvzTKf9He+o= Date: Wed, 02 Dec 2026 17:26:05 -0800 To: Matthew Wilcox Cc: linux-mm@kvack.org, Andrew Morton , Qian Xiao , stable@vger.kernel.org From: Andrew Morton Message-Id: In-Reply-To: References: Subject: [PATCH 2/3] xarray: fix index jumping backwards in xas_find_marked() X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list From: Qian Xiao 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 Signed-off-by: Andrew Morton Cc: Matthew Wilcox Cc: 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(); _