From patchwork Thu May 18 11:34:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13246565 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 52736C77B7A for ; Thu, 18 May 2023 11:35:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E34B2280001; Thu, 18 May 2023 07:35:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DE4B4900003; Thu, 18 May 2023 07:35:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD3B0280001; Thu, 18 May 2023 07:35:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id BDEFB900003 for ; Thu, 18 May 2023 07:35:30 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 91ABA1C75FE for ; Thu, 18 May 2023 11:35:30 +0000 (UTC) X-FDA: 80803170420.10.1A34B75 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf24.hostedemail.com (Postfix) with ESMTP id C9AD1180015 for ; Thu, 18 May 2023 11:35:28 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ghqrAqnD; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf24.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684409728; 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=DZXfiar5CKohUwsPymjzwnCAT+wFqXG5Kam/6y6M8pg=; b=NcNymczFHzV76SlMoIy2fPCG0LlMzlq3LlFweESijpmQUw03xOyQqURK2ShufI7nYpaKoW 9zE6HrKM1q33mG/15m0TG0aI2+yUT5bvmpucnYErHqQyTDx2Fcu/vM5cOPMIVvB8wDpmiE HBNQ6O0GA4nvlqJTZxnXpjaHv8UCdRg= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ghqrAqnD; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf24.hostedemail.com: domain of dhowells@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684409728; a=rsa-sha256; cv=none; b=PmbJly1TVCuaV0z4NtyHKQry8tV1xsOUYKKGIyfY3zqBfUPdUnamRvMDZ5/WL+tixvfzgo 9NZIwayx4O6wBOu8wv+N5YxZSjAqdRprmzes/lXX1rsj50gKk/ETHWozmPDvJ8nmUJIGnP VppDEFoE71zGCeeNZgGEhkHnwgxlgJA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684409728; 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=DZXfiar5CKohUwsPymjzwnCAT+wFqXG5Kam/6y6M8pg=; b=ghqrAqnDoODeY4g0YN49NXkq5gcbMXsRe3pSmd2HqixBOfzKK8Y1SXIDC6O1ZukimiUKlB 97/A4XkbXQMb2U15wv66KOjLhFUTiOxJZ0u+R0e+4pmZLLnGRm4GaPd6ZGiH3BObZ86UPz FdUOsDtRQAp8ie1Hjd2Mo/aDNUKdhWk= 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-206-lw-qVOlBOruNJ3l5NNmWVQ-1; Thu, 18 May 2023 07:35:21 -0400 X-MC-Unique: lw-qVOlBOruNJ3l5NNmWVQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D7B0C10146FE; Thu, 18 May 2023 11:35:20 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.42.28.221]) by smtp.corp.redhat.com (Postfix) with ESMTP id 78A2B492B01; Thu, 18 May 2023 11:35:16 +0000 (UTC) From: David Howells To: netdev@vger.kernel.org Cc: David Howells , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , David Ahern , Matthew Wilcox , Al Viro , Christoph Hellwig , Jens Axboe , Jeff Layton , Christian Brauner , Chuck Lever III , Linus Torvalds , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH net-next v8 04/16] tcp: Support MSG_SPLICE_PAGES Date: Thu, 18 May 2023 12:34:41 +0100 Message-Id: <20230518113453.1350757-5-dhowells@redhat.com> In-Reply-To: <20230518113453.1350757-1-dhowells@redhat.com> References: <20230518113453.1350757-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Stat-Signature: b7nkz485mifw78zfxze8iatuuuzx31kc X-Rspam-User: X-Rspamd-Queue-Id: C9AD1180015 X-Rspamd-Server: rspam07 X-HE-Tag: 1684409728-290804 X-HE-Meta: U2FsdGVkX19jXBD1YULw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X/xVXiBS/4XPmYeiqxXbbHxFkleVqkXoj06jiedbex4I9fB6MnwLLDb9wkVWAOF6Qrc0HmjyF1YmO7pqwky+nUi9WpGCS296G70KoC/PjdN9awfUPu2Nsio3xRqo2QcjuNxR/nlrtLUzxLv2GvbvkVcnMUzx4rDrXKet/JqhVJaEmsWAuo3w07VRw3PMfhVD1Cmm/SYMDngBt89w4RZJ+0dheYUotsBXRQGrbCX1oo6CQPIi+Si90F9b5TO9knkT1ypqYfLmo3OLFyckLGcnUdm18KZZphXXQ93E3S6xb232HsvWG5e68YMs1EANdvFmd/v+hRs19Wl2DzerUSEf2p1sraAri0LLrdZZEyQENBTfYxUs1LPsYOMb0eiBUtgsIped6sXQZw+zl3JnfYkw== 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: Make TCP's sendmsg() support MSG_SPLICE_PAGES. This causes pages to be spliced or copied (if it cannot be spliced) from the source iterator. This allows ->sendpage() to be replaced by something that can handle multiple multipage folios in a single transaction. Signed-off-by: David Howells cc: Eric Dumazet cc: "David S. Miller" cc: David Ahern cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- Notes: ver #7) - Missed a "zc = 1" in tcp_sendmsg_locked(). ver #6) - Set zc to 0/MSG_ZEROCOPY/MSG_SPLICE_PAGES rather than 0/1/2. - Use common helper. net/ipv4/tcp.c | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 4d6392c16b7a..026f483f42e3 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -1223,7 +1223,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) int flags, err, copied = 0; int mss_now = 0, size_goal, copied_syn = 0; int process_backlog = 0; - bool zc = false; + int zc = 0; long timeo; flags = msg->msg_flags; @@ -1234,17 +1234,22 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) if (msg->msg_ubuf) { uarg = msg->msg_ubuf; net_zcopy_get(uarg); - zc = sk->sk_route_caps & NETIF_F_SG; + if (sk->sk_route_caps & NETIF_F_SG) + zc = MSG_ZEROCOPY; } else if (sock_flag(sk, SOCK_ZEROCOPY)) { uarg = msg_zerocopy_realloc(sk, size, skb_zcopy(skb)); if (!uarg) { err = -ENOBUFS; goto out_err; } - zc = sk->sk_route_caps & NETIF_F_SG; - if (!zc) + if (sk->sk_route_caps & NETIF_F_SG) + zc = MSG_ZEROCOPY; + else uarg_to_msgzc(uarg)->zerocopy = 0; } + } else if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES) && size) { + if (sk->sk_route_caps & NETIF_F_SG) + zc = MSG_SPLICE_PAGES; } if (unlikely(flags & MSG_FASTOPEN || inet_sk(sk)->defer_connect) && @@ -1307,7 +1312,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) goto do_error; while (msg_data_left(msg)) { - int copy = 0; + ssize_t copy = 0; skb = tcp_write_queue_tail(sk); if (skb) @@ -1348,7 +1353,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) if (copy > msg_data_left(msg)) copy = msg_data_left(msg); - if (!zc) { + if (zc == 0) { bool merge = true; int i = skb_shinfo(skb)->nr_frags; struct page_frag *pfrag = sk_page_frag(sk); @@ -1393,7 +1398,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) page_ref_inc(pfrag->page); } pfrag->offset += copy; - } else { + } else if (zc == MSG_ZEROCOPY) { /* First append to a fragless skb builds initial * pure zerocopy skb */ @@ -1414,6 +1419,30 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) if (err < 0) goto do_error; copy = err; + } else if (zc == MSG_SPLICE_PAGES) { + /* Splice in data if we can; copy if we can't. */ + if (tcp_downgrade_zcopy_pure(sk, skb)) + goto wait_for_space; + copy = tcp_wmem_schedule(sk, copy); + if (!copy) + goto wait_for_space; + + err = skb_splice_from_iter(skb, &msg->msg_iter, copy, + sk->sk_allocation); + if (err < 0) { + if (err == -EMSGSIZE) { + tcp_mark_push(tp, skb); + goto new_segment; + } + goto do_error; + } + copy = err; + + if (!(flags & MSG_NO_SHARED_FRAGS)) + skb_shinfo(skb)->flags |= SKBFL_SHARED_FRAG; + + sk_wmem_queued_add(sk, copy); + sk_mem_charge(sk, copy); } if (!copied)