Re: [PATCH] mm/filemap: bring a folio uptodate if all blocks below EOF are uptodate
From: Carol Soto
Date: Tue Dec 01 2026 - 11:08:53 EST
On 11/30/26 5:24 PM, Andrew Morton wrote:
On Mon, 30 Nov 2026 10:17:03 -0800 Carol L Soto <csoto@xxxxxxxxxx> wrote:
When a filesystem block is smaller than a folio, a write that endsSo the write which makes all blocks which are inside EOF uptodate
before the last block of a new folio leaves the folio partially
uptodate. The write marks its own blocks uptodate and leaves PG_uptodate
clear. With 64 KiB pages and 4 KiB blocks, a 1280-byte write to a new
file is such a write.
doesn't bring the whole folio uptodate because the parts which are
outside EOF haven't been zeroed?
A read of that folio takes the folio lock and checks the blocks in theBut does that read zero out the outside-EOF page contents? Seems not.
range that the caller asked for. If one of those blocks is not uptodate,
the read calls ->read_folio(), which brings the whole folio uptodate.
If, on the other hand, all of those blocks are uptodate, then the read
copies its data and leaves PG_uptodate clear.
For the 1280-byte file, anah, there it is.
8 KiB request is the first case and a 1280-byte request is the second.
A reader whose requests are all in the second case never brings the
folio uptodate, so every one of its reads takes the folio lock.
Concurrent readers of a small, newly written file therefore end up being
serialized on the folio lock.
Fix this by making a blocking read bring the folio uptodate in the
second case as well. If every block of the folio below EOF is uptodate,
the read calls ->read_folio(), as in the first case. The call does not
issue any I/O for the blocks below EOF, because they are already
uptodate. The filesystem zeroes each block beyond EOF,
or reads a blockOK. Seems a rare case - is any real workload known to be affected?
that holds fsverity metadata, and sets PG_uptodate. Every later read
then finds PG_uptodate set, so it does not take the folio lock.
The read path calls ->read_folio() and does not zero the folio itself,
because only the filesystem knows whether a block beyond EOF is zero.
The filesystem also keeps an uptodate state for each block, and iomap
requires that state to agree with PG_uptodate.
A read with IOCB_NOWAIT, IOCB_NOIO or IOCB_WAITQ set is not allowed to
start I/O and wait for it to complete. A ->read_folio() call may do
both, so the read path never makes that call for such a read. The fix
keeps that rule, so such a read still takes the folio lock, as before.
The fix provides a 4.32x speed-up for the test case, which runs 48 nginx
workers on one 1280-byte file. The test ran on v7.4-rc1, on arm64 with
64 KiB pages and ext4 with 4 KiB blocks: three nginx instances of 16
workers each served the file over HTTPS to 16 wrk threads per instance.
Each figure is the median of three 90-second runs, in million requests
per second:
How the file was created Before After
1280-byte write 1.255 5.417
1280-byte write, fsync, FADV_DONTNEED 5.041 5.169
64 KiB write, truncate to 1280 bytes 5.369 4.949
In the last two rows, the reads never find a partially uptodate folio,
so the fix does not apply.
So far, we have observed this in the nginx benchmark described in the commit message; the reporter has not identified a separate production workload.
The reporter found it while profiling folio-access costs and investigating discrepancies between machines with different configurations. The affected pattern is a short buffered write that leaves a partially uptodate folio cached, followed by concurrent reads of valid ranges. Those reads repeatedly acquire the folio lock.
Other applications using that access pattern could encounter the same contention, but we do not currently have measurements beyond the benchmark. Evicting the file after fsync, or writing a full folio before truncating, avoids the slowdown in our tests.
I’ll also incorporate your suggested comment and declaration changes in v2.
--- a/mm/filemap.c"a caller that can read the folio" is a head-scratcher. How about
+++ b/mm/filemap.c
@@ -2535,8 +2535,10 @@ static int filemap_read_folio(struct file *file, filler_t filler,
static bool filemap_range_uptodate(struct address_space *mapping,
loff_t pos, size_t count, struct folio *folio,
- bool need_uptodate)
+ bool need_uptodate, bool can_read)
{
+ loff_t isize;
+
if (folio_test_uptodate(folio))
return true;
/* pipes can't handle partially uptodate pages */
@@ -2557,6 +2559,18 @@ static bool filemap_range_uptodate(struct address_space *mapping,
if (pos == 0 && count >= folio_size(folio))
return false;
+ /*
+ * If only blocks beyond i_size are not uptodate, have a caller that can
+ * read the folio bring it uptodate, so that later reads do not have to
+ * take the lock.
+ */
/*
* If all blocks below i_size are uptodate, complete the folio now
* so later reads need not take the folio lock.
*/
+ isize = i_size_read(mapping->host);nit:
+ if (can_read && isize > folio_pos(folio) &&
+ isize < folio_next_pos(folio) &&
+ mapping->a_ops->is_partially_uptodate(folio, 0,
+ isize - folio_pos(folio)))
+ return false;
+
return mapping->a_ops->is_partially_uptodate(folio, pos, count);
}
@@ -2564,6 +2578,8 @@ static int filemap_update_page(struct kiocb *iocb,
struct address_space *mapping, size_t count,
struct folio *folio, bool need_uptodate)
{
+ bool can_read = !(iocb->ki_flags &
+ (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ));
int error;
bool can_read;
int error;
can_read = !(iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ));
avoids the 80-col party tricks and creates a convenient place for adding
an explanatory comment (not that I think one is needed in this case).
if (iocb->ki_flags & IOCB_NOWAIT) {
@@ -2597,11 +2613,11 @@ static int filemap_update_page(struct kiocb *iocb,
error = 0;
if (filemap_range_uptodate(mapping, iocb->ki_pos, count, folio,
- need_uptodate))
+ need_uptodate, can_read))
goto unlock;
error = -EAGAIN;
- if (iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ))
+ if (!can_read)
goto unlock;
error = filemap_read_folio(iocb->ki_filp, mapping->a_ops->read_folio,
--
2.56.1