Re: [RFC] Bypass filesystems for reading cached pages

From: Matthew Wilcox
Date: Sat Jun 20 2020 - 15:40:30 EST


On Sat, Jun 20, 2020 at 09:19:37AM +0300, Amir Goldstein wrote:
> On Fri, Jun 19, 2020 at 6:52 PM Matthew Wilcox <willy@xxxxxxxxxxxxx> wrote:
> > This patch lifts the IOCB_CACHED idea expressed by Andreas to the VFS.
> > The advantage of this patch is that we can avoid taking any filesystem
> > lock, as long as the pages being accessed are in the cache (and we don't
> > need to readahead any pages into the cache). We also avoid an indirect
> > function call in these cases.
>
> XFS is taking i_rwsem lock in read_iter() for a surprising reason:
> https://lore.kernel.org/linux-xfs/CAOQ4uxjpqDQP2AKA8Hrt4jDC65cTo4QdYDOKFE-C3cLxBBa6pQ@xxxxxxxxxxxxxx/
> In that post I claim that ocfs2 and cifs also do some work in read_iter().
> I didn't go back to check what, but it sounds like cache coherence among
> nodes.

That's out of date. Here's POSIX-2017:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html

"I/O is intended to be atomic to ordinary files and pipes and
FIFOs. Atomic means that all the bytes from a single operation that
started out together end up together, without interleaving from other
I/O operations. It is a known attribute of terminals that this is not
honored, and terminals are explicitly (and implicitly permanently)
excepted, making the behavior unspecified. The behavior for other
device types is also left unspecified, but the wording is intended to
imply that future standards might choose to specify atomicity (or not)."

That _doesn't_ say "a read cannot observe a write in progress". It says
"Two writes cannot interleave". Indeed, further down in that section, it says:

"Earlier versions of this standard allowed two very different behaviors
with regard to the handling of interrupts. In order to minimize the
resulting confusion, it was decided that POSIX.1-2017 should support
only one of these behaviors. Historical practice on AT&T-derived systems
was to have read() and write() return -1 and set errno to [EINTR] when
interrupted after some, but not all, of the data requested had been
transferred. However, the US Department of Commerce FIPS 151-1 and FIPS
151-2 require the historical BSD behavior, in which read() and write()
return the number of bytes actually transferred before the interrupt. If
-1 is returned when any data is transferred, it is difficult to recover
from the error on a seekable device and impossible on a non-seekable
device. Most new implementations support this behavior. The behavior
required by POSIX.1-2017 is to return the number of bytes transferred."

That explicitly allows for a write to be interrupted by a signal and
later resumed, allowing a read to observe a half-complete write.

> Because if I am not mistaken, even though this change has a potential
> to improve many workloads, it may also degrade some workloads in cases
> where case readahead is not properly tuned. Imagine reading a large file
> and getting only a few pages worth of data read on every syscall.
> Or did I misunderstand your patch's behavior in that case?

I think you did. If the IOCB_CACHED read hits a readahead page,
it returns early. Then call_read_iter() notices the read is not yet
complete, and calls ->read_iter() to finish the read. So it's two
calls to generic_file_buffered_read() rather than one, but it's still
one syscall.