From patchwork Fri Mar 31 16:08:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13196198 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA228C761A6 for ; Fri, 31 Mar 2023 16:11:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C3446B00AA; Fri, 31 Mar 2023 12:11:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 773556B00AB; Fri, 31 Mar 2023 12:11:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 577E36B00AC; Fri, 31 Mar 2023 12:11:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 42ADE6B00AA for ; Fri, 31 Mar 2023 12:11:10 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1D18CAC901 for ; Fri, 31 Mar 2023 16:11:10 +0000 (UTC) X-FDA: 80629682700.19.6640B3F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 26EB420019 for ; Fri, 31 Mar 2023 16:11:07 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ihF2u1cs; spf=pass (imf03.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680279068; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ecYVxyEqGMFmF1eT8L3egIdesMHxN/xKxl+aET3XOq4=; b=HfkSCRrHqW/lnMS0TpoasrLMT3omddBoh0+f7hMVshKKFzRaKAjaQfILW8T3DRzUvVJfq2 F3o9ZwxEP6uDO3IU8aa7MgVvPl3IfxXw/B5RUDgcc3XalqgzhqDlUETLKQyoRBa/nO+ls1 ixPhZYdXw1WmgQ7lCSl2mbf3aazWdpw= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ihF2u1cs; spf=pass (imf03.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680279068; a=rsa-sha256; cv=none; b=HYauAuG6ioarji3chLJll3FulVoE2IV50zY7/ZBHof5gBjAdem+XFbveCKZ06veGGZuVXL YN7rLKfNI0592RiTzr6qD9PrOutkicqfiJQSagDDNWudlB34EuiIfCJzBBm69qrg1j5SRE NUefqko1xrTSTyrZf0d6NxXu430VA7E= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680279067; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ecYVxyEqGMFmF1eT8L3egIdesMHxN/xKxl+aET3XOq4=; b=ihF2u1csuu+8JBU6QTuT9vy3+j2D3RuPeXa6oYQ24WwTYJyALwZ+gWWXBT0M7cjhP96ePe +HJ2IF5SUEZ+QvQkvmcu9h+XcQWNdUjNzUdTnVyQ5ZiL/+1ujoKsyCI1zj3geZMzoBKKhg QsjIm0QmfM/UwUEmi2vuGDVpRp6ZmW4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-364-3bh1vnySPnOS90RyB8ZPRw-1; Fri, 31 Mar 2023 12:11:04 -0400 X-MC-Unique: 3bh1vnySPnOS90RyB8ZPRw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 61AEC88606A; Fri, 31 Mar 2023 16:11:00 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7FD721121314; Fri, 31 Mar 2023 16:10:58 +0000 (UTC) From: David Howells To: Matthew Wilcox , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: David Howells , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , netdev@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v3 36/55] splice, net: Reimplement splice_to_socket() to pass multiple bufs to sendmsg() Date: Fri, 31 Mar 2023 17:08:55 +0100 Message-Id: <20230331160914.1608208-37-dhowells@redhat.com> In-Reply-To: <20230331160914.1608208-1-dhowells@redhat.com> References: <20230331160914.1608208-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Rspamd-Queue-Id: 26EB420019 X-Stat-Signature: 89gdqop7c8ym3qykursbg3tjot55xtn8 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1680279067-463571 X-HE-Meta: U2Fs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3I9LgelqO8gdNqho8GEu8yavXbO9FMNHyDBEGzuMm0W1yqRXk0yot6qX5HVWcJEY3dLY8mXseB17Ra4qn8ycUFDCelQutCXKo1Zc4oRppkcAv8agcoLVMYlQ3No6hFXwrdmkRgR4jkfOUu392aHLTO8/XZ4zYY6k2CVLBpfl/LEETeza6Vd/48KqXTHUwnM1U9VkyuyGgcZpQFw6mMM3TASgwvY1Mc3VCr8Qis4iyZDR1XVS0mOlJGWUir8qU4FXY1xoza87Tu+LpUDd8HPZ7xmIl0rSUlSHlLUb1VoigH7S2SshnsRAPoPxizy2aevZq5przr4MJ1YC1t1uQZVxOnEkSuLONHuuhq X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Reimplement splice_to_socket() so that it can pass multiple pipe buffer pages to sendmsg() in a single go. Signed-off-by: David Howells cc: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- fs/splice.c | 148 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 120 insertions(+), 28 deletions(-) diff --git a/fs/splice.c b/fs/splice.c index 23ead122d631..d5bc28b59720 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -411,33 +411,6 @@ const struct pipe_buf_operations nosteal_pipe_buf_ops = { }; EXPORT_SYMBOL(nosteal_pipe_buf_ops); -#ifdef CONFIG_NET -/* - * Send 'sd->len' bytes to socket from 'sd->file' at position 'sd->pos' - * using sendpage(). Return the number of bytes sent. - */ -static int pipe_to_sendmsg(struct pipe_inode_info *pipe, - struct pipe_buffer *buf, struct splice_desc *sd) -{ - struct socket *sock = sock_from_file(sd->u.file); - struct bio_vec bvec; - struct msghdr msg = { - .msg_flags = MSG_SPLICE_PAGES, - }; - - if (sd->flags & SPLICE_F_MORE) - msg.msg_flags |= MSG_MORE; - - if (sd->len < sd->total_len && - pipe_occupancy(pipe->head, pipe->tail) > 1) - msg.msg_flags |= MSG_MORE; - - bvec_set_page(&bvec, buf->page, sd->len, buf->offset); - iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, sd->len); - return sock_sendmsg(sock, &msg); -} -#endif - static void wakeup_pipe_writers(struct pipe_inode_info *pipe) { smp_mb(); @@ -816,7 +789,126 @@ EXPORT_SYMBOL(iter_file_splice_write); ssize_t splice_to_socket(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags) { - return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_sendmsg); + struct socket *sock = sock_from_file(out); + struct bio_vec bvec[16]; + struct msghdr msg = {}; + ssize_t ret; + size_t spliced = 0; + bool need_wakeup = false; + + pipe_lock(pipe); + + while (len > 0) { + unsigned int head, tail, mask, bc = 0; + size_t remain = len; + + /* + * Check for signal early to make process killable when there + * are always buffers available + */ + ret = -ERESTARTSYS; + if (signal_pending(current)) + break; + + while (pipe_empty(pipe->head, pipe->tail)) { + ret = 0; + if (!pipe->writers) + goto out; + + if (spliced) + goto out; + + ret = -EAGAIN; + if (flags & SPLICE_F_NONBLOCK) + goto out; + + ret = -ERESTARTSYS; + if (signal_pending(current)) + goto out; + + if (need_wakeup) { + wakeup_pipe_writers(pipe); + need_wakeup = false; + } + + pipe_wait_readable(pipe); + } + + head = pipe->head; + tail = pipe->tail; + mask = pipe->ring_size - 1; + + while (!pipe_empty(head, tail)) { + struct pipe_buffer *buf = &pipe->bufs[tail & mask]; + size_t seg; + + if (!buf->len) { + tail++; + continue; + } + + seg = min_t(size_t, remain, buf->len); + seg = min_t(size_t, seg, PAGE_SIZE); + + ret = pipe_buf_confirm(pipe, buf); + if (unlikely(ret)) { + if (ret == -ENODATA) + ret = 0; + break; + } + + bvec_set_page(&bvec[bc++], buf->page, seg, buf->offset); + remain -= seg; + if (seg >= buf->len) + tail++; + if (bc >= ARRAY_SIZE(bvec)) + break; + } + + if (!bc) + break; + + msg.msg_flags = 0; + if (flags & SPLICE_F_MORE) + msg.msg_flags = MSG_MORE; + if (remain && pipe_occupancy(pipe->head, tail) > 0) + msg.msg_flags = MSG_MORE; + msg.msg_flags |= MSG_SPLICE_PAGES; + + iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, bvec, bc, len - remain); + ret = sock_sendmsg(sock, &msg); + if (ret <= 0) + break; + + spliced += ret; + len -= ret; + tail = pipe->tail; + while (ret > 0) { + struct pipe_buffer *buf = &pipe->bufs[tail & mask]; + size_t seg = min_t(size_t, ret, buf->len); + + buf->offset += seg; + buf->len -= seg; + ret -= seg; + + if (!buf->len) { + pipe_buf_release(pipe, buf); + tail++; + } + } + + if (tail != pipe->tail) { + pipe->tail = tail; + if (pipe->files) + need_wakeup = true; + } + } + +out: + pipe_unlock(pipe); + if (need_wakeup) + wakeup_pipe_writers(pipe); + return spliced ?: ret; } #endif