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=1796369946; b=u5ody6UPM50RIbqQuFkEo9ZrEU+4yfi8Z/VNouXTCbSi6Dzr+e6iKHkiMZzHPIvh+Fwy v9lOpHolVWy+EAcEdoTlgbg/EPoFjpy5LiUvTkvD2m3EtIsXuy1iq84+c9nDvwtrel06k 5EMXfjcRnMGupl87rds+stnTELrEiVcZ213ApbCXdyJP2qoRwBHKifN4QLTbp2grPz/C8 MDg6UlY8QcH/Y4oT+0J4jcWCJV29Th3TSHtqYovKRUuaPaNoSED5e2qTpgtiD8uPAlqSz z2PIHalxKALUU4jxUBkcmtFX0pteGN9tRP2KBIKWhgC25eh54nvdbXErnol4I4gHh5A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796369946; h=Received:DKIM-Signature:From:To:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=WzNgkiC1rHu2FB/dP0pW1iZ+e5LUgHWtt13Bxpa1LTA=; b=iqcRfOtmhVHu4ROjmrvSGU+PqmPFyjPmkBGK+DRc60/9OnjFURcQ8yKSv4xqSAS5FHsF Y4lP9QxpnkpAPaTNssfztIaaQi9/s712wKOaAZ0FiW3hMI856hqn4q7lBPVn+GPX0yRRt tThrx9aUtbI0AMB5VSkuOKpTKLJV6AaTC2SasDmE23MUJmrRKQMJF2+TnV5W1vuSRS66D DZUdYwwiS4cpkYYTpVR2lMU/GURz+rKPs2D8ZRsDSMlNJL50cflcCBaQ9l5VGkeDW8rBa 6Hq1ErlGO8Nz6z1pbg84OMQp0EVPlG+x1v4WnwzNDDeerNJ7hBqnPlyUAMGjE49NVpA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=okG+FUxg; 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 13400606A33A for ; Fri, 4 Dec 2026 07:39:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=okG+FUxg Received: by smtp.kernel.org (Postfix) with ESMTPSA id A28801F00937; Fri, 4 Dec 2026 07:39:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796369945; bh=WzNgkiC1rHu2FB/dP0pW1iZ+e5LUgHWtt13Bxpa1LTA=; h=From:To:Subject:Date:In-Reply-To:References; b=okG+FUxgCaSrh5ugAG3XBuV3iYKcSbV6KjUYMrBuTiWhn1SKKI453abbc/DwtzcI7 UsVyfjvSVoYwPuHrIhK1BS0PkgrNx50UM5YazEdHmD1Lls3m30hQTl5G263FMEhx/G oxMqNOjSdmvd3jC4Fg5r3XihgvXOLEdpWjI6OiRL5Rxx1E2LfSNnpsnTtzscxlpFnE tUbmCKZik7x35Cz5TXq5DvqZs8KHl1UZuI4BRLkCUyYO1W9BTL5kwRe7mgfL0d2axD uJK42bwl7Be7n22d+2XqPRMLObcKWU0xd10VHG9nTmU4jIbjlcpMXUv9Nr9lBxrnV9 TD0pTSD54zWew== From: Mike Snitzer To: Matthew Wilcox , Jan Kara , linux-mm@kvack.org, Christian Brauner , linux-fsdevel@vger.kernel.org Subject: [PATCH v13 4/7] mm: add folio_mark_dropbehind() to mark a folio dropbehind and account it Date: Fri, 4 Dec 2026 02:38:53 -0500 Message-ID: X-Mailer: git-send-email 2.44.0 In-Reply-To: References: X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit WB_DONTCACHE_DIRTY counts the dirty folios that are marked dropbehind. When a dropbehind folio is dirtied, folio_account_dirtied() adds it to the counter. When the folio is cleaned, folio_clear_dirty_for_io() or folio_account_cleaned() subtracts it again. This balances only if the folio is marked before it is dirtied, as it is for the folios that an RWF_DONTCACHE write creates. An RWF_DONTCACHE write that covers a folio only partially shares the folio with the other writes that cover the rest. With records smaller than the folio there can be several. For example, four or five 4000-byte records that append to a file share a 16 KiB folio. The next commit keeps such a folio until a write completes it: the write that finds the rest of the folio written. That write marks the folio dropbehind. It does this in iomap, so XFS and every other file system that uses iomap for buffered writes get it. The write marks the folio after it dirties it, so the folio is already dirty when the write marks it. folio_set_dropbehind() only sets the flag. With it, the clean would subtract the folio from WB_DONTCACHE_DIRTY although it was never added, and the counter would go negative. Add folio_mark_dropbehind(), the inverse of filemap_clear_dropbehind(). With the folio locked, it sets PG_dropbehind. Under the i_pages lock, it adds the folio to WB_DONTCACHE_DIRTY if both PG_dirty and the dirty tag are set. A dirtier that has set PG_dirty but not yet counted the folio finds the flag set and counts the folio itself. Leave a mapped folio unmarked. Through its page table, a mapped folio can be dirtied without the folio lock, and could then be counted twice. The folio lock keeps an unmapped folio unmapped. A folio that is mapped when a write completes it stays cached until reclaim. The name follows folio_set_dirty() and folio_mark_dirty(): the set variant only sets the flag, the mark variant also accounts for it. Signed-off-by: Mike Snitzer --- include/linux/pagemap.h | 1 + mm/filemap.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 73af18a373670..3f5295d6fd73b 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -1306,6 +1306,7 @@ void end_page_writeback(struct page *page); void folio_end_writeback(struct folio *folio); void folio_end_writeback_no_dropbehind(struct folio *folio); void folio_end_dropbehind(struct folio *folio); +void folio_mark_dropbehind(struct folio *folio); void folio_wait_stable(struct folio *folio); void __folio_mark_dirty(struct folio *folio, struct address_space *, int warn); void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb); diff --git a/mm/filemap.c b/mm/filemap.c index 9ecd5060ea4c9..dc270b668258b 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -1654,6 +1654,41 @@ void folio_end_dropbehind(struct folio *folio) } EXPORT_SYMBOL_GPL(folio_end_dropbehind); +/** + * folio_mark_dropbehind - mark an existing folio dropbehind + * @folio: the folio, locked by the caller + * + * Mark @folio so that the writeback that cleans it drops it. If the folio + * is already counted as dirty, which with the i_pages lock held means both + * PG_dirty and the dirty tag are set, add it to WB_DONTCACHE_DIRTY so that + * the clean that subtracts it leaves the counter balanced. A mapped folio + * is left unmarked: a page table can dirty it without the folio lock. + */ +void folio_mark_dropbehind(struct folio *folio) +{ + struct address_space *mapping = folio->mapping; + unsigned long flags; + + VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); + + if (folio_test_dropbehind(folio)) + return; + if (folio_mapped(folio)) + return; + if (!mapping || !mapping_can_writeback(mapping)) { + folio_set_dropbehind(folio); + return; + } + xa_lock_irqsave(&mapping->i_pages, flags); + folio_set_dropbehind(folio); + if (folio_test_dirty(folio) && + xa_get_mark(&mapping->i_pages, folio->index, PAGECACHE_TAG_DIRTY)) + wb_stat_mod(inode_to_wb(mapping->host), WB_DONTCACHE_DIRTY, + folio_nr_pages(folio)); + xa_unlock_irqrestore(&mapping->i_pages, flags); +} +EXPORT_SYMBOL_GPL(folio_mark_dropbehind); + /** * folio_end_writeback_no_dropbehind - End writeback against a folio. * @folio: The folio. -- 2.52.0