From: Jens Axboe <axboe@kernel.dk>
To: Matteo Croce <technoboy85@gmail.com>,
linux-fsdevel@vger.kernel.org,
Christian Brauner <brauner@kernel.org>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Ding Feng <dingf3ng@gmail.com>
Cc: Paul Eggert <eggert@cs.ucla.edu>,
LKML <linux-kernel@vger.kernel.org>,
io-uring@vger.kernel.org
Subject: Re: [PATCH v2] fs: shorten I/O that would extend past the maximum file offset
Date: Sat, 21 Nov 2026 14:59:19 -0700 [thread overview]
Message-ID: <d1a1bfcd-234d-41a5-a0c4-16065e5f4857@kernel.dk> (raw)
In-Reply-To: <CAFnufp2uT0D8-5jw3DLhQzdfpB=eSuzhmGdmDqoB7arExaw9jw@mail.gmail.com>
On 11/20/26 5:49 PM, Matteo Croce wrote:
> Il giorno sab 21 nov 2026 alle ore 01:34 Matteo Croce
> <technoboy85@gmail.com> 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@gmail.com>
>> Closes: https://lore.kernel.org/linux-fsdevel/20261120051457.47753-1-dingf3ng@gmail.com/
>> Suggested-by: Paul Eggert <eggert@cs.ucla.edu>
>> Link: https://lists.gnu.org/archive/html/coreutils/2025-12/msg00097.html
>> Link: https://lore.kernel.org/linux-fsdevel/20251219125250.65245-1-teknoraver@meta.com/
>> Signed-off-by: Matteo Croce <technoboy85@gmail.com>
>
> Forgot to re-run get_maintainer.pl after the io_uring change
Maybe send it again then if it's something we shoudl see. You cut the
patch out of this reply...
--
Jens Axboe
prev parent reply other threads:[~2026-11-21 21:59 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-21 0:33 Matteo Croce
2026-11-21 0:49 ` Matteo Croce
2026-11-21 21:59 ` Jens Axboe [this message]
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=d1a1bfcd-234d-41a5-a0c4-16065e5f4857@kernel.dk \
--to=axboe@kernel.dk \
--cc=brauner@kernel.org \
--cc=dingf3ng@gmail.com \
--cc=eggert@cs.ucla.edu \
--cc=io-uring@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=technoboy85@gmail.com \
--cc=viro@zeniv.linux.org.uk \
/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®