On Thu, Feb 09, 2023 at 08:41:02AM -0800, Linus Torvalds wrote: > Adding Jens, because he's one of the main splice people. You do seem > to be stepping on his work ;) > > Jens, see > > https://lore.kernel.org/lkml/0cfd9f02-dea7-90e2-e932-c8129b6013c7@xxxxxxxxx > > On Thu, Feb 9, 2023 at 5:56 AM Stefan Metzmacher <metze@xxxxxxxxx> wrote: > > > > So we have two cases: > > > > 1. network -> socket -> splice -> pipe -> splice -> file -> storage > > > > 2. storage -> file -> splice -> pipe -> splice -> socket -> network > > > > With 1. I guess everything can work reliable [..] > > > > But with 2. there's a problem, as the pages from the file, > > which are spliced into the pipe are still shared without > > copy on write with the file(system). > > Well, honestly, that's really the whole point of splice. It was > designed to be a way to share the storage data without having to go > through a copy. > > > I'm wondering if there's a possible way out of this, maybe triggered by a new > > flag passed to splice. > > Not really. > > So basically, you cannot do "copy on write" on a page cache page, > because that breaks sharing. > > You *want* the sharing to break, but that's because you're violating > what splice() was for, but think about all the cases where somebody is > just using mmap() and expects to see the file changes. > > You also aren't thinking of the case where the page is already mapped > writably, and user processes may be changing the data at any time. > > > I looked through the code and noticed the existence of IOMAP_F_SHARED. > > Yeah, no. That's a hacky filesystem thing. It's not even a flag in > anything core like 'struct page', it's just entirely internal to the > filesystem itself. It's the mechanism that the filesystem uses to tell the generic write IO path that the filesystem needs to allocate a new COW extent in the backing store because it can't write to the original extent. i.e. it's not allowed to overwrite in place. It's no different to the VM_SHARED flag in the vma so the generic page fault path knows if it has to allocate a new COW page to take place on a write fault because it can't write to the original page. i.e. it's not allowed to overwrite in place. So by the same measure, VM_SHARED is a "hacky mm thing". It's not even a flag in anything core like 'struct page', it's just entirely internal to the mm subsystem itself. COW is COW is COW no matter which layer implements. :/ > > Is there any other way we could archive something like this? > > I suspect you simply want to copy it at splice time, rather than push > the page itself into the pipe as we do in copy_page_to_iter_pipe(). > > Because the whole point of zero-copy really is that zero copy. And the > whole point of splice() was to *not* complicate the rest of the system > over-much, while allowing special cases. > > Linux is not the heap of bad ideas that is Hurd that does various > versioning etc, and that made copy-on-write a first-class citizen > because it uses the concept of "immutable mapped data" for reads and > writes. > > Now, I do see a couple of possible alternatives to "just create a stable copy". > > For example, we very much have the notion of "confirm buffer data > before copying". It's used for things like "I started the IO on the > page, but the IO failed with an error, so even though I gave you a > splice buffer, it turns out you can't use it". > > And I do wonder if we could introduce a notion of "optimistic splice", > where the splice works exactly the way it does now (you get a page > reference), but the "confirm" phase could check whether something has > changed in that mapping (using the file versioning or whatever - I'm > hand-waving) and simply fail the confirm. > > That would mean that the "splice to socket" part would fail in your > chain, and you'd have to re-try it. But then the onus would be on > *you* as a splicer, not on the rest of the system to fix up your > special case. > > That idea sounds fairly far out there, and complicated and maybe not > usable. So I'm just throwing it out as a "let's try to think of > alternative solutions". Oh, that's sounds like an exact analogy to the new IOMAP_F_STALE flag and the validity cookie we have in the iomap write path code. The iomap contains cached, unserialised information, and the filesystem side mapping it is derived from can change asynchronously (e.g. by IO completion doing unwritten extent conversion). Hence the cached iomap can become stale, and that's a data corruption vector. The validity cookie is created when the iomap is built, and it is passed to a filesystem callback when a folio is locked for copy-in. This allows the IO path to detect that the filesystem side extent map has changed during the write() operations before we modify the contents of the folio. It is done under the locked folio so that the validation is atomic w.r.t. the modification to the folio contents we are about to perform. On detection of a cookie mismatch, the write operation then sets the IOMAP_F_STALE flag, backs out of the write to that page and ends the write to the iomap. The iomap infrastructure then remaps the file range from the offset of the folio at which the iomap change was detected. The write the proceeds with the new, up to date iomap.... We have had a similar "is the cached iomap still valid?" mechanism on the writeback side of the page cache for years. The details are slightly different, though I plan to move that code to use the same IOMAP_F_STALE infrastructure in the near future because it simplifies the writeback context wrapper shenanigans an awful lot. And it helps make it explicit that iomaps are cached/shadowed state, not the canonical source of reality. Applying the same principle it to multiply referenced cached page contents will be more complex. I suspect we might be able to leverage inode->i_version or ctime as the "data changed" cookie as they are both supposed to change on every explicit user data modification made to an inode. However, I think most of the complexity would be in requiring spliced pages to travel in some kind of container that holds the necessary verification information.... Cheers, Dave. -- Dave Chinner david@xxxxxxxxxxxxx