mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Carol Soto <csoto@nvidia.com>
To: Jan Kara <jack@suse.cz>
Cc: willy@infradead.org, akpm@linux-foundation.org,
	linux-fsdevel@vger.kernel.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, John Hubbard <jhubbard@nvidia.com>
Subject: Re: [PATCH v2] mm/filemap: bring a folio uptodate if all blocks below EOF are uptodate
Date: Thu, 3 Dec 2026 13:46:31 -0600	[thread overview]
Message-ID: <ef655ef7-0923-4c29-841e-4ddef25e36ad@nvidia.com> (raw)
In-Reply-To: <mw5nwobi2nqdpmescw4tt77pysmiypbsefft2uxzorqgm7kaqz@lr4spdzfpu2h>


On 12/2/26 6:09 AM, Jan Kara wrote:
> On Tue 01-12-26 13:21:40, Carol L Soto wrote:
>> When a filesystem block is smaller than a folio, a write that ends
>> 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.
>>
>> A read of that folio takes the folio lock and checks the blocks in the
>> 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, an
>> 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 block
>> 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.
>>
>> Co-developed-by: John Hubbard <jhubbard@nvidia.com>
>> Signed-off-by: John Hubbard <jhubbard@nvidia.com>
>> Signed-off-by: Carol L Soto <csoto@nvidia.com>
>> ---
>> Changes since v1:
>> - Simplify the EOF-folio completion comment as suggested in review.
>> - Separate the can_read declaration and assignment as suggested in review.
>> - No functional changes; retain the v7.4-rc1 measurements.
>>
>> v1: https://lore.kernel.org/all/20261130181703.836051-1-csoto@nvidia.com/
>>
>>   mm/filemap.c | 22 +++++++++++++++++++---
>>   1 file changed, 19 insertions(+), 3 deletions(-)
>>
>> diff --git a/mm/filemap.c b/mm/filemap.c
>> index b74bc1e..b0d93a6 100644
>> --- a/mm/filemap.c
>> +++ b/mm/filemap.c
>> @@ -2553,8 +2553,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 */
>> @@ -2575,6 +2577,17 @@ static bool filemap_range_uptodate(struct address_space *mapping,
>>   	if (pos == 0 && count >= folio_size(folio))
>>   		return false;
>>   
>> +	/*
>> +	 * 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);
>> +	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;
> Hrm, this is the classical dilema. Do we pay higher cost upfront to reduce
> future costs (folio locking) or do we go as fast as we can for this request
> (current behavior). So I can forsee some heuristics back-and-forth here :)
>
> I can imagine that with e.g. 2M folios there is non-negligible chance some
> of the blocks actually is not uptodate and then this would just add
> pointless scanning of bitmaps / buffer heads on each read.
>
> A more conservative way to fix your particular case would be to just do:
>
> 	uptodate = mapping->a_ops->is_partially_uptodate(folio, pos, count);
> 	if (can_read && uptodate && pos == 0 && isize <= pos + count)
> 		return false;
> 	return uptodate;
>
> This way we don't introduce extra uptodate state scanning but OTOH you
> won't make the folio uptodate unless you read the whole tail part of it in
> one go (which again is less probable with larger folios, but your
> particular benchmark would benefit the same way).
>
> So overall I'm not strongly against this but I'm kind of reluctant with
> this change without a practical case we are optimizing the heuristics for.
> Because it isn't an obvious win in all the cases...
>
> 								Honza
>
Thanks for the suggestion. I implemented and tested the conservative approach, accounting for folio-relative offsets when checking EOF. It retains the nginx benefit: 2.34x higher shared-file throughput on rc5, with controls essentially unchanged. The targeted runtime tests also passed.
I haven’t posted v3 yet, as I’m investigating the write-side alternative suggested from Matthew's review.

Thanks,
Carol

>> @@ -2582,8 +2595,11 @@ static int filemap_update_page(struct kiocb *iocb,
>>   		struct address_space *mapping, size_t count,
>>   		struct folio *folio, bool need_uptodate)
>>   {
>> +	bool can_read;
>>   	int error;
>>   
>> +	can_read = !(iocb->ki_flags & (IOCB_NOIO | IOCB_NOWAIT | IOCB_WAITQ));
>> +
>>   	if (iocb->ki_flags & IOCB_NOWAIT) {
>>   		if (!filemap_invalidate_trylock_shared(mapping))
>>   			return -EAGAIN;
>> @@ -2615,11 +2631,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.25.1
>>

  reply	other threads:[~2026-12-03 19:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 21:21 Carol L Soto
2026-12-02 12:09 ` Jan Kara
2026-12-03 19:46   ` Carol Soto [this message]
2026-12-03 16:59 ` Matthew Wilcox
  -- strict thread matches above, loose matches on Subject: below --
2026-11-30 18:17 [PATCH] " Carol L Soto
2026-12-01 16:43 ` [PATCH v2] " Carol L Soto
2026-12-01 20:41   ` John Hubbard
2026-12-01 21:21     ` Carol Soto

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ef655ef7-0923-4c29-841e-4ddef25e36ad@nvidia.com \
    --to=csoto@nvidia.com \
    --cc=akpm@linux-foundation.org \
    --cc=jack@suse.cz \
    --cc=jhubbard@nvidia.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®