From: "Darrick J. Wong" <djwong@kernel.org>
To: Joanne Koong <joannelkoong@gmail.com>
Cc: brauner@kernel.org, hch@infradead.org, bfoster@redhat.com,
linux-fsdevel@vger.kernel.org, kernel-team@meta.com,
Christoph Hellwig <hch@lst.de>
Subject: Re: [PATCH v3 1/8] iomap: account for unaligned end offsets when truncating read range
Date: Tue, 4 Nov 2025 17:27:21 -0800 [thread overview]
Message-ID: <20251105012721.GD196362@frogsfrogsfrogs> (raw)
In-Reply-To: <20251104205119.1600045-2-joannelkoong@gmail.com>
On Tue, Nov 04, 2025 at 12:51:12PM -0800, Joanne Koong wrote:
> The end position to start truncating from may be at an offset into a
> block, which under the current logic would result in overtruncation.
>
> Adjust the calculation to account for unaligned end offsets.
When would the read offset be in the middle of a block? My guess is
that happens when fuse reads some unaligned number of bytes into a folio
and then stops? Can this also happen with inlinedata mappings? I think
those are the only two conditions where iomap isn't dealing with
fsblock-aligned mappings and/or IOs.
(Obviously the pagecache copies themselves aren't required to be
aligned)
--D
> Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> ---
> fs/iomap/buffered-io.c | 22 ++++++++++++++++++++--
> 1 file changed, 20 insertions(+), 2 deletions(-)
>
> diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> index f099c086cbe8..c79b59e52a49 100644
> --- a/fs/iomap/buffered-io.c
> +++ b/fs/iomap/buffered-io.c
> @@ -218,6 +218,22 @@ static void ifs_free(struct folio *folio)
> kfree(ifs);
> }
>
> +/*
> + * Calculate how many bytes to truncate based off the number of blocks to
> + * truncate and the end position to start truncating from.
> + */
> +static size_t iomap_bytes_to_truncate(loff_t end_pos, unsigned block_bits,
> + unsigned blocks_truncated)
> +{
> + unsigned block_size = 1 << block_bits;
> + unsigned block_offset = end_pos & (block_size - 1);
> +
> + if (!block_offset)
> + return blocks_truncated << block_bits;
> +
> + return ((blocks_truncated - 1) << block_bits) + block_offset;
> +}
> +
> /*
> * Calculate the range inside the folio that we actually need to read.
> */
> @@ -263,7 +279,8 @@ static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
> /* truncate len if we find any trailing uptodate block(s) */
> while (++i <= last) {
> if (ifs_block_is_uptodate(ifs, i)) {
> - plen -= (last - i + 1) * block_size;
> + plen -= iomap_bytes_to_truncate(*pos + plen,
> + block_bits, last - i + 1);
> last = i - 1;
> break;
> }
> @@ -279,7 +296,8 @@ static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
> unsigned end = offset_in_folio(folio, isize - 1) >> block_bits;
>
> if (first <= end && last > end)
> - plen -= (last - end) * block_size;
> + plen -= iomap_bytes_to_truncate(*pos + plen, block_bits,
> + last - end);
> }
>
> *offp = poff;
> --
> 2.47.3
>
>
next prev parent reply other threads:[~2025-11-05 1:27 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-04 20:51 [PATCH v3 0/8] iomap: buffered io changes Joanne Koong
2025-11-04 20:51 ` [PATCH v3 1/8] iomap: account for unaligned end offsets when truncating read range Joanne Koong
2025-11-05 1:27 ` Darrick J. Wong [this message]
2025-11-06 17:08 ` Joanne Koong
2025-11-06 23:08 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 2/8] docs: document iomap writeback's iomap_finish_folio_write() requirement Joanne Koong
2025-11-05 1:22 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 3/8] iomap: optimize pending async writeback accounting Joanne Koong
2025-11-05 1:28 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 4/8] iomap: simplify ->read_folio_range() error handling for reads Joanne Koong
2025-11-05 1:50 ` Darrick J. Wong
2025-11-06 17:17 ` Joanne Koong
2025-11-06 23:09 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 5/8] iomap: simplify when reads can be skipped for writes Joanne Koong
2025-11-05 1:40 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 6/8] iomap: use loff_t for file positions and offsets in writeback code Joanne Koong
2025-11-05 1:41 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 7/8] iomap: use find_next_bit() for dirty bitmap scanning Joanne Koong
2025-11-05 1:42 ` Darrick J. Wong
2025-11-04 20:51 ` [PATCH v3 8/8] iomap: use find_next_bit() for uptodate " Joanne Koong
2025-11-05 1:42 ` Darrick J. Wong
2025-11-11 12:09 ` [PATCH v3 0/8] iomap: buffered io changes Christian Brauner
2025-11-11 18:05 ` Joanne Koong
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=20251105012721.GD196362@frogsfrogsfrogs \
--to=djwong@kernel.org \
--cc=bfoster@redhat.com \
--cc=brauner@kernel.org \
--cc=hch@infradead.org \
--cc=hch@lst.de \
--cc=joannelkoong@gmail.com \
--cc=kernel-team@meta.com \
--cc=linux-fsdevel@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).