Message ID | 20230602150752.1306532-6-dhowells@redhat.com (mailing list archive) |
---|---|
State | Handled Elsewhere |
Headers | show |
Series | None | expand |
On Fri, Jun 2, 2023 at 11:08 AM David Howells <dhowells@redhat.com> wrote: > > Fix this by making splice_direct_to_actor() always signal SPLICE_F_MORE if > we haven't yet hit the requested operation size. Well, I certainly like this patch better than the previous versions, just because it doesn't add random fd-specific code. That said, I think it might be worth really documenting the behavior, particularly for files where the kernel *could* know "the file is at EOF, no more data". I hope that if user space wants to splice() a file to a socket, said user space would have done an 'fstat()' and actually pass in the file size as the length to splice(). Because if they do, I think this simplified patch does the right thing automatically. But if user space instead passes in a "maximally big len", and just depends on the kernel then doing tha ret = do_splice_to(in, &pos, pipe, len, flags); if (unlikely(ret <= 0)) goto out_release; to stop splicing at EOF, then the last splice_write() will have had SPLICE_F_MORE set, even though no more data is coming from the file, of course. And I think that's fine. But wasn't that effectively what the old code was already doing because 'read_len' was smaller than 'len'? I thought that was what you wanted to fix? IOW, I thought you wanted to clear SPLICE_F_MORE when we hit EOF. This still doesn't do that. So now I'm confused about what your "fix" is. Your patch doesn't actually seem to change existing behavior in splice_direct_to_actor(). I was expecting you to actually pass the 'sd' down to do_splice_to() and then to ->splice_read(), so that the splice_read() function could say "I have no more", and clear it. But you didn't do that. Am I misreading something, or did I miss another patch? Linus
diff --git a/fs/splice.c b/fs/splice.c index 9b1d43c0c562..c71bd8e03469 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -1052,13 +1052,17 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, */ bytes = 0; len = sd->total_len; + + /* Don't block on output, we have to drain the direct pipe. */ flags = sd->flags; + sd->flags &= ~SPLICE_F_NONBLOCK; /* - * Don't block on output, we have to drain the direct pipe. + * We signal MORE until we've read sufficient data to fulfill the + * request and we keep signalling it if the caller set it. */ - sd->flags &= ~SPLICE_F_NONBLOCK; more = sd->flags & SPLICE_F_MORE; + sd->flags |= SPLICE_F_MORE; WARN_ON_ONCE(!pipe_empty(pipe->head, pipe->tail)); @@ -1074,14 +1078,12 @@ ssize_t splice_direct_to_actor(struct file *in, struct splice_desc *sd, sd->total_len = read_len; /* - * If more data is pending, set SPLICE_F_MORE - * If this is the last data and SPLICE_F_MORE was not set - * initially, clears it. + * If we now have sufficient data to fulfill the request then + * we clear SPLICE_F_MORE if it was not set initially. */ - if (read_len < len) - sd->flags |= SPLICE_F_MORE; - else if (!more) + if (read_len >= len && !more) sd->flags &= ~SPLICE_F_MORE; + /* * NOTE: nonblocking mode only applies to the input. We * must not do the output in nonblocking mode as then we
splice_direct_to_actor() doesn't manage SPLICE_F_MORE correctly[1] - and, as a result, it incorrectly signals/fails to signal MSG_MORE when splicing to a socket. The problem I'm seeing happens when a short splice occurs because we got a short read due to hitting the EOF on a file: as the length read (read_len) is less than the remaining size to be spliced (len), SPLICE_F_MORE (and thus MSG_MORE) is set. The issue is that, for the moment, we have no way to know *why* the short read occurred and so can't make a good decision on whether we *should* keep MSG_MORE set. Further, the argument can be made that it should be left to userspace to decide how to handle it - userspace could perform some sort of cancellation for example. MSG_SENDPAGE_NOTLAST was added to work around this, but that is also set incorrectly under some circumstances - for example if a short read fills a single pipe_buffer, but the next read would return more (seqfile can do this). This was observed with the multi_chunk_sendfile tests in the tls kselftest program. Some of those tests would hang and time out when the last chunk of file was less than the sendfile request size: build/kselftest/net/tls -r tls.12_aes_gcm.multi_chunk_sendfile This has been observed before[2] and worked around in AF_TLS[3]. Fix this by making splice_direct_to_actor() always signal SPLICE_F_MORE if we haven't yet hit the requested operation size. SPLICE_F_MORE remains signalled if the user passed it in to splice() but otherwise gets cleared when we've read sufficient data to fulfill the request. The cleanup of a short splice to userspace is left to userspace. [!] Note that this changes user-visible behaviour. It will cause the multi_chunk_sendfile tests in the TLS kselftest to fail. This failure in the testsuite will be addressed in a subsequent patch by making userspace do a zero-length send(). It appears that SPLICE_F_MORE is only used by splice-to-socket. Signed-off-by: David Howells <dhowells@redhat.com> cc: Linus Torvalds <torvalds@linux-foundation.org> cc: Jakub Kicinski <kuba@kernel.org> cc: Jens Axboe <axboe@kernel.dk> cc: Christoph Hellwig <hch@lst.de> cc: Al Viro <viro@zeniv.linux.org.uk> cc: Matthew Wilcox <willy@infradead.org> cc: Jan Kara <jack@suse.cz> cc: Jeff Layton <jlayton@kernel.org> cc: David Hildenbrand <david@redhat.com> cc: Christian Brauner <brauner@kernel.org> cc: Chuck Lever <chuck.lever@oracle.com> cc: Boris Pismenny <borisp@nvidia.com> cc: John Fastabend <john.fastabend@gmail.com> cc: Eric Dumazet <edumazet@google.com> cc: "David S. Miller" <davem@davemloft.net> cc: Paolo Abeni <pabeni@redhat.com> cc: linux-fsdevel@vger.kernel.org cc: linux-block@vger.kernel.org cc: linux-mm@kvack.org cc: netdev@vger.kernel.org Link: https://lore.kernel.org/r/499791.1685485603@warthog.procyon.org.uk/ [1] Link: https://lore.kernel.org/r/1591392508-14592-1-git-send-email-pooja.trivedi@stackpath.com/ [2] Link: https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit/?id=d452d48b9f8b1a7f8152d33ef52cfd7fe1735b0a [3] --- fs/splice.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)