From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BCDCE606A322 for ; Thu, 3 Dec 2026 01:26:14 +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 kanga.kvack.org (Postfix) id 2CE1E6B05C8; Wed, 2 Dec 2026 20:26:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 27F286B05C9; Wed, 2 Dec 2026 20:26:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1BB166B05CA; Wed, 2 Dec 2026 20:26:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id AB2CF6B05C8 for ; Wed, 2 Dec 2026 20:26:08 -0500 (EST) Received: from smtpin18.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id F0D6EA0572 for ; Thu, 3 Dec 2026 01:26:07 +0000 (UTC) X-FDA: 85500930774.18.1E27EA1 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf03.hostedemail.com (Postfix) with ESMTP id 6F0F42000C for ; Thu, 3 Dec 2026 01:26:06 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=P2oGkuBC; dmarc=none; spf=pass (imf03.hostedemail.com: domain of akpm@linux-foundation.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796261166; b=yT6ep5pHPstmrR6OceXqSE012Izt1lIIYii8i19iW+GKJgab1DPtg2IEAJ0Tz/xtvhBBmF LqMO/dNpTaC3K69T0nr+z4SUx8MUUAM0b4rpoAjyqKtbOErqdXRWq0l3of8DcBDUy33N7C Hwj9svGMQtCM64i6+igAUI+w1caZBbU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796261166; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=mDPsemAMSsQh8LRvyqMeOyNCnxiSczSJ+VOwtbYQlvs=; b=hr1gW1mBukPHorgJ7z3G7ojs3Vl7WiyzBvei2ytBCaxaH66I+PDQDkE8W3Wtdd9SBlQHgy A8/ohsgwAsySkbpaLcSRLNDy/rG9A/nSoVYNgNKOiGiNOu+UctVZxK5tK6/qIf3n8fczr5 IQMOQjNv4UMqDqZ9AHWl+1IacyO8aZ4= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=P2oGkuBC; dmarc=none; spf=pass (imf03.hostedemail.com: domain of akpm@linux-foundation.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 009EC60D4D; Thu, 3 Dec 2026 01:26:06 +0000 (UTC) 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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6F0F42000C X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Stat-Signature: tx1gfu89b7xwmt8ohmkzz5qiigtmibri X-Rspam-User: X-HE-Tag: 1796261166-732743 X-HE-Meta: U2FsdGVkX18Ls1Sfh5cPQ8/SE6YuisODAI1Nql30aRdQEA4NgM1UjkPOGstP61Jn1HV21njAEaFoFsT/JoQtmc9OHN7ExRpsT4jjtLaGZYzV9JbMs9xSah8Z8wvvjfAB11jYwxNRZNMM8xgDiNjL9JIXCFrOFMhuwmEf94GcJcEmE5kYZo6NflXuZA2BSMVgrb71GoXYMVBvcdi4uv/ONrMQA7r1MSdZh2/L/CdnZHRifkseThmmxILG0WKveelfHVf9eDex09+QfUo5RNwoIOJHpwVhW1tN19UXl81RJ1s3tKsjokWHnqbXoUBOoIuwEJDwIs5wwslkgu79cmiSxJg3eWPjOJvnb0zYNW+33X4RwcyY49EbzHSjFAMKHQSu86ru9NBd9cQHIQU8gm5bhiu4yfAZBOhHDl0l9fTBqNKHoH6ewTThQ8euKPROaT24Job7BQRg9ap+mT0W9cgV2tqWLc1lDq8pxUX1MNOLytlIwLwCRegZ96KVR7Z21J4iBku/+4aeg2cJuyoSNyx0iWe46tF0jY7jcYlRCKRH7pvhaQMLUBPdSBe9t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FtCAaA20L1TSpcC5L5HpCDC12Ye6dwbLO7wWVlCf6nCnGjisq3KAE6J8qoRVzqdgREEA== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: 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(); _