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

From: Matteo Croce

Date: Fri Nov 20 2026 - 15:54:40 EST


Il giorno ven 20 nov 2026 alle ore 21:03 Paul Eggert
<eggert@xxxxxxxxxxx> ha scritto:
>
> On 2026-11-20 10:42, Matteo Croce wrote:
> > + if (file->f_op->fop_flags & FOP_UNSIGNED_OFFSET)
> > + return count;
>
> This means there's still a problem when reading past the maximum file offset of /dev/mem and other files with FOP_UNSIGNED_OFFSET, right? If FOP_UNSIGNED_OFFSET is set, are the file's own read/write methods expected to shorten I/O requests in this situation?
>
> Thanks for looking into this, by the way.

Right. For files with FOP_UNSIGNED_OFFSET the limit is the unsigned
one, 2^64 - 1, and crossing LLONG_MAX is already allowed for them.
At that limit rw_verify_area() still returns -EOVERFLOW instead of
shortening the request, and this patch deliberately leaves them
untouched.

As things stand the VFS only guarantees that pos + count doesn't wrap
past 2^64, and everything else is up to the file's own methods. They
don't behave consistently: /dev/mem returns -EFAULT for a range that
goes past the end of RAM, while /proc/pid/mem returns a short count
at the first page it can't access.

I can extend rw_clamp_count() to shorten the count so the range ends
at 2^64 - 1 for these files too, so that the behaviour is the same at
both limits. In practice none of these files has data near 2^64, so it
would only be for consistency. I'll do it in v2 if you think it's
worth it.

Thanks for the review.

One correction to the commit message. The second reproducer is wrong
as written: with "dd ... | cat" it is dd itself that does the read at
LLONG_MAX and fails, not cat. The intended example has dd only move
the shared file offset of stdin, so that cat then reads from
LLONG_MAX:

$ (dd bs=1 skip=$((2**63 - 1)) count=0 status=none; cat) < hugefile
cat: -: Invalid argument

The code is not affected. If you apply this version, could you please
replace that example while applying? Otherwise I'll fix it in v2.

Regards,
--
per aspera ad upstream