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 46862606A32C for ; Fri, 4 Dec 2026 07:39:34 +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=OwTTiATI Received: by kanga.kvack.org (Postfix) id D1CAB6B05BA; Fri, 4 Dec 2026 02:39:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CCE176B05BC; Fri, 4 Dec 2026 02:39:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C0A196B05BA; Fri, 4 Dec 2026 02:39:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 90E296B05BA for ; Fri, 4 Dec 2026 02:39:14 -0500 (EST) Received: from smtpin25.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AB59DC02A3 for ; Fri, 4 Dec 2026 07:39:09 +0000 (UTC) X-FDA: 85505499618.25.B4EF116 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf12.hostedemail.com (Postfix) with ESMTP id 0B2174000B for ; Fri, 4 Dec 2026 07:39:07 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=OwTTiATI; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of snitzer@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=snitzer@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796369948; b=GZ4wDRUhUITNd51tBjpTYJwdOm8UFI8yapVXfHyKbMRUtPeyFICODcRcjdLvFLCxL0vslK yHhvl9wYXQ2d7T1yX8AA+ib85X2vBQ6owtZiNArxkwCF3JqlruOTCYzyTTDbS4TGx83Xwo rKc6X7vpJ6zhPP7SdVy+FXlnDdoyXWI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796369948; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=tEfoqtQsDaV5+QJ7iaFocltCaR7Jb0pgULB6DTDXW1w=; b=J8YtPZJKWHgY25mWKrG5LlPP7pk72g/wVRnrfDKLb+hRdYHHi4vg/Rz4HWR1SFi4/aFZYN jP3V/mUiOsXEO5S+zqojefPCvfejeaRAxAwnKNbN6OEsokWMnqAK3BthumPmg11OH/HULL M9L18EpteGKWtUk5IC7z9KTocDgoRd0= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=OwTTiATI; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf12.hostedemail.com: domain of snitzer@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=snitzer@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 5015C45819; Fri, 4 Dec 2026 07:39:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2B7B1F00A0A; Fri, 4 Dec 2026 07:39:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796369947; bh=tEfoqtQsDaV5+QJ7iaFocltCaR7Jb0pgULB6DTDXW1w=; h=From:To:Subject:Date:In-Reply-To:References; b=OwTTiATIQBT9KwAD9bcojm/zCZlmvHHDjwEgLWiBNupDUWWgeRm0CgPh9NT7xSUjz 9EydcbSGV9gD8R3CWzW1XZi9SfGwPKP3KDOysnN91bPLndtnmvhDFy0XIzb5DJvEPR LAKoAl8GZl3s5PlYZCrDahYH8jyzwoowGIJbI8BBYq9wjgpJ7IHZdugqagtdcDmWYz sPEWHhwlvO9WPq+ZfRL7bKIyqTFAfUrxIOgQ92LOonIECX7S9ws5dJTp/rPqYrEAsk BJSIVWv6nsqA8vXr+C7wlWCC3BZFRMp4qUDJrye5oufQvsulZQ2jdhLgUH87n1Bd29 K0uxVjHpRD4+g== From: Mike Snitzer To: Matthew Wilcox , Jan Kara , linux-mm@kvack.org, Christian Brauner , linux-fsdevel@vger.kernel.org Subject: [PATCH v13 5/7] iomap: keep a DONTCACHE edge folio until a write completes it Date: Fri, 4 Dec 2026 02:38:54 -0500 Message-ID: <077b73a253ca413d615b8d53be147efee84f78c8.1796367526.git.snitzer@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam11 X-Rspam-User: X-Stat-Signature: 6u6yrraxnqed5ywzxh3fuf3mpfyfcede X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Queue-Id: 0B2174000B X-HE-Tag: 1796369947-79621 X-HE-Meta: U2FsdGVkX18yxRYpYT1z6C9woNMLp3Gx7rzDNZqsrlO4VN/nUH2NjUR5/fahElRrMlRVsk7efnwahA1ghd2LmBbr9FBwlzQ2nifaOI8DVuEnN7HndWXPrmcAkc0lUo60xT81dzQYnhiVIu8d/NIupkCK1HvPVf599Y+IGrIMlykJvxpLr+r52tNkyKGekYxfTiHImlV0L8rcbK7pA0g0z3314ZhCJZHT/DK9Wm6eC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EXsbXvvUm11CWIuo5Vh5Z04g2ExOHozchmpgfWIv21t79cm0YPMybUQrWJEYOA3hrShMIFmnQ2HsfFvUKMZKnDb2FDVuXG8hN9TDYfQIjp2VdIQPXCT1cI+gc8w5hYx906bDU9ss6Ok68= 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 An RWF_DONTCACHE buffered write goes through the page cache like any buffered write. It marks every folio it creates dropbehind, so writeback drops the folio as soon as the data of the write is on disk. A write that starts or ends inside a folio covers that folio only partially. Usually other writes cover the rest of the folio. An example is the next record of a file written in records that are not a multiple of the page size. Writeback can drop the folio before the other writes write their parts. Then the next of them must first read the folio back from disk: one read per misaligned write. When a DONTCACHE write starts or ends inside the smallest folio the mapping allows, iomap looks that folio up without FGP_DONTCACHE. A new folio is then not marked dropbehind. Then iomap checks whether the other writes have written their parts. If so, this write completes the folio. iomap marks it with folio_mark_dropbehind() once the write has copied its part into the folio, before it unlocks the folio. The writeback that cleans the data of all these writes drops it. iomap can give a folio up without writing its part: the mapping can go stale, or a copy can come up short. Such a folio stays unmarked. A larger folio, such as one from readahead, is left alone. A found folio that is already marked loses its mark in the lookup, and gets it back if this write completes the folio. The other parts are written if the folio is uptodate. With blocks smaller than the folio, the edge is not uptodate yet, so it is enough that the blocks outside this write are uptodate. But an uptodate folio is not always a written one. A write that extends the file fills the part of the folio past EOF with zeroes, and the folio becomes uptodate. The next append then finds the folio uptodate before it is complete, and the folio is dropped. The append after it must read the folio back. So the part of the folio outside this write must also lie below EOF. The kernel cannot know if another write will come, and this commit does not assume it. It reads only the uptodate state of the folio and the file size. An edge that no other write completes, such as the last page of a file, stays cached until reclaim. Writeback drops all other folios of the write, as before. With this commit, 32 threads write interleaved 47008-byte records to one XFS file with RWF_DONTCACHE. Device reads per write fell from 0.97 to 0.0005, and throughput rose from 831 to 2281 MiB/s. 47008 bytes is the transfer size of IO500's ior-hard, in which each rank writes interleaved records to one shared file. Any record size that is not a multiple of the page size has the same problem. With 6000-byte records, device reads per write fell from 0.97 to 0.0001, and throughput rose from 121 to 488 MiB/s. Signed-off-by: Mike Snitzer --- fs/iomap/buffered-io.c | 103 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 7 deletions(-) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index 25915c0f5e1cc..65e48e99f234c 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -787,6 +787,63 @@ bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count) } EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate); +/* + * Have the other writes written the parts of this edge folio that this write + * does not cover? The unit the writes share is the smallest folio the mapping + * allows. Call before the write, while i_size does not include it yet. + */ +static bool iomap_dontcache_edge_written(struct folio *folio, loff_t pos, + size_t len) +{ + struct inode *inode = folio->mapping->host; + size_t size = folio_size(folio); + loff_t fpos = folio_pos(folio); + loff_t isize = i_size_read(inode); + size_t from, to, bs; + + if (folio_nr_pages(folio) != mapping_min_folio_nrpages(folio->mapping)) + return false; + /* + * Past EOF the folio holds zeroes, not the data of the other writes. An + * appending write makes it uptodate with those zeroes, so the next + * append would find it uptodate. + */ + if ((pos > fpos && pos > isize) || + (pos + len < fpos + size && fpos + size > isize)) + return false; + if (folio_test_uptodate(folio)) + return true; + + /* + * With blocks smaller than the folio, the edge is not uptodate yet. + * The blocks this write covers in full are [from, to). + */ + bs = i_blocksize(inode); + from = offset_in_folio(folio, pos); + to = min(from + len, size); + from = round_up(from, bs); + to = round_down(to, bs); + if (from && !iomap_is_partially_uptodate(folio, 0, from)) + return false; + if (to < size && !iomap_is_partially_uptodate(folio, to, size - to)) + return false; + return true; +} + +/* + * For a DONTCACHE write: the bytes it covers of the unit at @pos if it + * covers that unit only partially, else 0. + */ +static size_t iomap_dontcache_edge(const struct iomap_iter *iter, loff_t pos) +{ + size_t unit = mapping_min_folio_nrbytes(iter->inode->i_mapping); + size_t offset = pos & (unit - 1); + + if (!offset && iter->len >= unit) + return 0; + return min_t(u64, iter->len, unit - offset); +} + /** * iomap_get_folio - get a folio reference for writing * @iter: iteration structure @@ -802,8 +859,20 @@ struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len) if (iter->flags & IOMAP_NOWAIT) fgp |= FGP_NOWAIT; - if (iter->flags & IOMAP_DONTCACHE) - fgp |= FGP_DONTCACHE; + if (iter->flags & IOMAP_DONTCACHE) { + size_t edge = iomap_dontcache_edge(iter, pos); + + /* + * Other writes may cover the rest of this unit. Look it up + * without FGP_DONTCACHE, as one unit, so that a new folio is + * not marked. If this write completes it, iomap_write_iter() + * marks it once the write has written its part. + */ + if (edge) + len = min(len, edge); + else + fgp |= FGP_DONTCACHE; + } fgp |= fgf_set_order(len); return __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT, @@ -1029,11 +1098,13 @@ static int iomap_write_begin_inline(const struct iomap_iter *iter, /* * Grab and prepare a folio for write based on iter state. Returns the folio, * offset, and length. Callers can optionally pass a max length *plen, - * otherwise init to zero. + * otherwise init to zero. If @pdontcache_edge is not NULL, it is set to + * whether this DONTCACHE write completes a folio it covers only partially, + * which the caller then marks dropbehind once it has written its part. */ static int iomap_write_begin(struct iomap_iter *iter, const struct iomap_write_ops *write_ops, struct folio **foliop, - size_t *poffset, u64 *plen) + size_t *poffset, u64 *plen, bool *pdontcache_edge) { const struct iomap *srcmap = iomap_iter_srcmap(iter); loff_t pos; @@ -1099,6 +1170,15 @@ static int iomap_write_begin(struct iomap_iter *iter, pos = iomap_trim_folio_range(iter, folio, poffset, &len); + /* + * Decide now, before __iomap_write_begin() reads or zeroes the rest of + * the folio, whether this write completes a folio it shares with other + * writes. + */ + if (pdontcache_edge) + *pdontcache_edge = iomap_dontcache_edge(iter, pos) && + iomap_dontcache_edge_written(folio, pos, len); + if (srcmap->type == IOMAP_INLINE) status = iomap_write_begin_inline(iter, folio); else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) @@ -1202,6 +1282,7 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i, size_t copied; /* Bytes copied from user */ u64 written; /* Bytes have been written */ loff_t pos; + bool dontcache_edge = false; /* completes a shared folio? */ bytes = iov_iter_count(i); retry: @@ -1231,7 +1312,8 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i, } status = iomap_write_begin(iter, write_ops, &folio, &offset, - &bytes); + &bytes, (iter->flags & IOMAP_DONTCACHE) ? + &dontcache_edge : NULL); if (unlikely(status)) { iomap_write_failed(iter->inode, iter->pos, bytes); break; @@ -1247,6 +1329,13 @@ static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i, copied = copy_folio_from_iter_atomic(folio, offset, bytes, i); written = iomap_write_end(iter, bytes, copied, folio) ? copied : 0; + /* + * Mark a folio this write completes only now, with the folio + * still locked: a stale mapping or a short copy can make the + * write give the folio up without writing its part. + */ + if (dontcache_edge && written == bytes) + folio_mark_dropbehind(folio); /* * Update the in-memory inode size after copying the data into @@ -1616,7 +1705,7 @@ static int iomap_unshare_iter(struct iomap_iter *iter, bytes = min_t(u64, SIZE_MAX, bytes); status = iomap_write_begin(iter, write_ops, &folio, &offset, - &bytes); + &bytes, NULL); if (unlikely(status)) return status; if (iomap->flags & IOMAP_F_STALE) @@ -1692,7 +1781,7 @@ static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero, bytes = min_t(u64, SIZE_MAX, bytes); status = iomap_write_begin(iter, write_ops, &folio, &offset, - &bytes); + &bytes, NULL); if (status) return status; if (iter->iomap.flags & IOMAP_F_STALE) -- 2.52.0