Re: [PATCH v2] fs: shorten I/O that would extend past the maximum file offset

From: Matteo Croce

Date: Fri Nov 20 2026 - 19:50:39 EST


Il giorno sab 21 nov 2026 alle ore 01:34 Matteo Croce
<technoboy85@xxxxxxxxx> ha scritto:
>
> rw_verify_area() fails with -EINVAL any read or write where
> pos + count would exceed LLONG_MAX, even when pos itself is valid.
> Such a request should not fail: a read should return the bytes that
> are there up to the maximum offset, or 0 at end of file, and a write
> should write what fits and return a short count.
>
> This breaks ordinary programs that read a file up to an end at or near
> LLONG_MAX, even with ordinary buffer sizes. For example, dd fails on
> the last block, where it should get a short read, and cat fails where
> it should get end of file:
>
> $ truncate -s $((2**63 - 1)) hugefile
> $ dd if=hugefile of=/dev/null bs=1M skip=$((2**43 - 2))
> dd: error reading 'hugefile': Invalid argument
> 1+0 records in
> 1+0 records out
> 1048576 bytes (1,0 MB, 1,0 MiB) copied, 0,103536 s, 10,1 MB/s
>
> $ (dd bs=1 skip=$((2**63 - 1)) count=0 status=none; cat) < hugefile
> cat: -: Invalid argument
>
> The same happens on a 100-byte file:
>
> preadv2(fd, 200 bytes, pos = LLONG_MAX - 50) = -1 EINVAL
> preadv2(fd, 50 bytes, pos = LLONG_MAX - 50) = 0
>
> With a large count, the range crosses LLONG_MAX even far from it:
> splice() of a 20 MB file with a count of 0x7FFFFFFFFF000000 fails once
> the position reaches 16 MB. sendfile(), aio and io_uring fail in the
> same way as read() and splice().
>
> Files with FOP_UNSIGNED_OFFSET, such as /dev/mem and /proc/pid/mem,
> have the same problem at U64_MAX, their maximum offset: a read or
> write that starts below U64_MAX but would end past it fails with
> -EOVERFLOW.
>
> Add rw_clamp_count(), which shortens the count so that the range ends
> at the maximum offset, LLONG_MAX or U64_MAX for files with
> FOP_UNSIGNED_OFFSET, and call it before rw_verify_area() in the read
> and write paths of the VFS, splice, aio and io_uring. Where the I/O is
> described by an iov_iter, truncate the iterator. This is what
> filemap_read() already does for s_maxbytes. In io_uring, also save the
> iterator state again after truncating it, so that the short read retry
> loop does not restore the full length.
>
> A read that starts at the maximum offset returns 0. A write that starts
> there cannot transfer anything and still fails in rw_verify_area(), as
> do negative positions on files without FOP_UNSIGNED_OFFSET and counts
> above SSIZE_MAX. For files with FOP_UNSIGNED_OFFSET, the file's own
> read and write methods still decide how much of the shortened range
> they can actually transfer.
>
> rw_verify_area() keeps its check, so callers that are not converted
> behave as before. copy_file_range() already clamps the range in
> generic_copy_file_checks() and is not changed.
>
> Reported-by: Ding Feng <dingf3ng@xxxxxxxxx>
> Closes: https://lore.kernel.org/linux-fsdevel/20261120051457.47753-1-dingf3ng@xxxxxxxxx/
> Suggested-by: Paul Eggert <eggert@xxxxxxxxxxx>
> Link: https://lists.gnu.org/archive/html/coreutils/2025-12/msg00097.html
> Link: https://lore.kernel.org/linux-fsdevel/20251219125250.65245-1-teknoraver@xxxxxxxx/
> Signed-off-by: Matteo Croce <technoboy85@xxxxxxxxx>

Forgot to re-run get_maintainer.pl after the io_uring change

Regards,
--
per aspera ad upstream