From patchwork Wed May 24 14:49:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13254101 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 A6C08C77B73 for ; Wed, 24 May 2023 14:49:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 141DF280003; Wed, 24 May 2023 10:49:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 07BC4280001; Wed, 24 May 2023 10:49:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD6A5280003; Wed, 24 May 2023 10:49:45 -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 BD0C5280001 for ; Wed, 24 May 2023 10:49:45 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 9E3671409B9 for ; Wed, 24 May 2023 14:49:45 +0000 (UTC) X-FDA: 80825432730.26.DB5DA1B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf25.hostedemail.com (Postfix) with ESMTP id D3408A000B for ; Wed, 24 May 2023 14:49:43 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FbusvB9N; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf25.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=1684939783; 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=3J8PHmDPb0Wk4jmNU2VIBExWXmpJgDgYePfc2qOgcg8=; b=fSLY1TiPQiTFYUGPE+XaxoIaUVQwOalcqsMsaWEEVJvAamNER5TQFWn91G/I36Hb3WE5+W iGrjRLdVPZI1JOAdzurMzrs0Ie82MeX12l/9MvjuRSV0v/Lkzi89gDRkNtmk3PjcrPANXz sdzZ7B3ForXSnTUxCbDyzJMrkhJXYEo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FbusvB9N; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf25.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=1684939783; a=rsa-sha256; cv=none; b=TSrwBRDJNZTPFoaqO2cp85CTy0n2F/xxsmyxU2Pahls9o/QUkO/HbKeeydhH0OFHpP5o/d 4S2Vp84FI5CNhNfmQcHIFPCA5QhqERI0elmt39DfEEboIVin23SpPp8ljo1LLGVgG/rC5t RynwlczoGNEBOodQzhEnHzF8VMs0nqE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684939783; 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=3J8PHmDPb0Wk4jmNU2VIBExWXmpJgDgYePfc2qOgcg8=; b=FbusvB9NnRXGqXiBpoPmcDFIE9Ii/wrKBNFTJhKDom9Tr3lnbtKpXyXbqWOPSMcn4Vaddb arsjDapeeK1xy9Cpp5Y2U5/Ho9Kxqcu0vIYfr8CBWI0vNogm1riA3h86YOhk7K7itXkoRm ShTA9yAKIa8dFjIQmCbJG7Kv3N2PiIU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-576-zslUpdpYNp6S1clsL_W_IQ-1; Wed, 24 May 2023 10:49:39 -0400 X-MC-Unique: zslUpdpYNp6S1clsL_W_IQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C44043800E80; Wed, 24 May 2023 14:49:38 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.39.192.68]) by smtp.corp.redhat.com (Postfix) with ESMTP id A8A32C1ED99; Wed, 24 May 2023 14:49:36 +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 , Jens Axboe , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Tom Herbert , Tom Herbert Subject: [PATCH net-next 4/4] kcm: Convert kcm_sendpage() to use MSG_SPLICE_PAGES Date: Wed, 24 May 2023 15:49:23 +0100 Message-Id: <20230524144923.3623536-5-dhowells@redhat.com> In-Reply-To: <20230524144923.3623536-1-dhowells@redhat.com> References: <20230524144923.3623536-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Rspamd-Queue-Id: D3408A000B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 3audsu6z8ptrhqamdwcyqhwczbyagtts X-HE-Tag: 1684939783-861043 X-HE-Meta: U2FsdGVkX1/BCI4M8X/rq5Aw+RF9RCxL4Nj3BnevbsiXxJ3dcOoIOOkKxHexSbe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6K6k8Rxb2NdlYeqzRuO/t9x4N3gSSPjQk1OO5F6wyDsShHsSwMXE4Axt3z3vPNW5Zk1d2API8jXp0B55q92LXSFlv7QK8+YBA65g4ljNxV8D9dnfNhhsNJ/DLEzPfwNdoop5d42ssHKqekTvJ+kUaEFTsCKs7jVoCHmyy2i9ujt9LHC7sfPAqUDneTAolkiooGV7/g0zIjo0nFWL+fXc1wER3Wyv7xyCOng+dCYzRfxITfzrdTAVhnEvX4McWQHeBaKYvxwPCzJR42skIwMkm2qbBh/PTj3DEtt1BCRQq3SeQxJaqEKSixuha4Ah2vwI/JlS3yEosoFX9hXBbuahdHMPl3uDdaJkSQfzxepsT0GQaPGMiLEtdWx3YyY2eKOEW8CjeZU73Q7QefImQdB89t4SG06jhpGgaHcZwZjqereVhl+ILF3ZUh8YbbbKI3u3h8XRnPKQ5HW5cvtLuT+pJ+A8= 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: Convert kcm_sendpage() to use sendmsg() with MSG_SPLICE_PAGES rather than directly splicing in the pages itself. This allows ->sendpage() to be replaced by something that can handle multiple multipage folios in a single transaction. Signed-off-by: David Howells cc: Tom Herbert cc: Tom Herbert cc: Jakub Kicinski cc: Eric Dumazet cc: "David S. Miller" cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/kcm/kcmsock.c | 161 ++++++---------------------------------------- 1 file changed, 18 insertions(+), 143 deletions(-) diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c index 411726d830c0..f6e0e017e3cc 100644 --- a/net/kcm/kcmsock.c +++ b/net/kcm/kcmsock.c @@ -761,149 +761,6 @@ static void kcm_push(struct kcm_sock *kcm) kcm_write_msgs(kcm); } -static ssize_t kcm_sendpage(struct socket *sock, struct page *page, - int offset, size_t size, int flags) - -{ - struct sock *sk = sock->sk; - struct kcm_sock *kcm = kcm_sk(sk); - struct sk_buff *skb = NULL, *head = NULL; - long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); - bool eor; - int err = 0; - int i; - - if (flags & MSG_SENDPAGE_NOTLAST) - flags |= MSG_MORE; - - /* No MSG_EOR from splice, only look at MSG_MORE */ - eor = !(flags & MSG_MORE); - - lock_sock(sk); - - sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); - - err = -EPIPE; - if (sk->sk_err) - goto out_error; - - if (kcm->seq_skb) { - /* Previously opened message */ - head = kcm->seq_skb; - skb = kcm_tx_msg(head)->last_skb; - i = skb_shinfo(skb)->nr_frags; - - if (skb_can_coalesce(skb, i, page, offset)) { - skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); - skb_shinfo(skb)->flags |= SKBFL_SHARED_FRAG; - goto coalesced; - } - - if (i >= MAX_SKB_FRAGS) { - struct sk_buff *tskb; - - tskb = alloc_skb(0, sk->sk_allocation); - while (!tskb) { - kcm_push(kcm); - err = sk_stream_wait_memory(sk, &timeo); - if (err) - goto out_error; - } - - if (head == skb) - skb_shinfo(head)->frag_list = tskb; - else - skb->next = tskb; - - skb = tskb; - skb->ip_summed = CHECKSUM_UNNECESSARY; - i = 0; - } - } else { - /* Call the sk_stream functions to manage the sndbuf mem. */ - if (!sk_stream_memory_free(sk)) { - kcm_push(kcm); - set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); - err = sk_stream_wait_memory(sk, &timeo); - if (err) - goto out_error; - } - - head = alloc_skb(0, sk->sk_allocation); - while (!head) { - kcm_push(kcm); - err = sk_stream_wait_memory(sk, &timeo); - if (err) - goto out_error; - } - - skb = head; - i = 0; - } - - get_page(page); - skb_fill_page_desc_noacc(skb, i, page, offset, size); - skb_shinfo(skb)->flags |= SKBFL_SHARED_FRAG; - -coalesced: - skb->len += size; - skb->data_len += size; - skb->truesize += size; - sk->sk_wmem_queued += size; - sk_mem_charge(sk, size); - - if (head != skb) { - head->len += size; - head->data_len += size; - head->truesize += size; - } - - if (eor) { - bool not_busy = skb_queue_empty(&sk->sk_write_queue); - - /* Message complete, queue it on send buffer */ - __skb_queue_tail(&sk->sk_write_queue, head); - kcm->seq_skb = NULL; - KCM_STATS_INCR(kcm->stats.tx_msgs); - - if (flags & MSG_BATCH) { - kcm->tx_wait_more = true; - } else if (kcm->tx_wait_more || not_busy) { - err = kcm_write_msgs(kcm); - if (err < 0) { - /* We got a hard error in write_msgs but have - * already queued this message. Report an error - * in the socket, but don't affect return value - * from sendmsg - */ - pr_warn("KCM: Hard failure on kcm_write_msgs\n"); - report_csk_error(&kcm->sk, -err); - } - } - } else { - /* Message not complete, save state */ - kcm->seq_skb = head; - kcm_tx_msg(head)->last_skb = skb; - } - - KCM_STATS_ADD(kcm->stats.tx_bytes, size); - - release_sock(sk); - return size; - -out_error: - kcm_push(kcm); - - err = sk_stream_error(sk, flags, err); - - /* make sure we wake any epoll edge trigger waiter */ - if (unlikely(skb_queue_len(&sk->sk_write_queue) == 0 && err == -EAGAIN)) - sk->sk_write_space(sk); - - release_sock(sk); - return err; -} - static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) { struct sock *sk = sock->sk; @@ -1109,6 +966,24 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) return err; } +static ssize_t kcm_sendpage(struct socket *sock, struct page *page, + int offset, size_t size, int flags) + +{ + struct bio_vec bvec; + struct msghdr msg = { .msg_flags = flags | MSG_SPLICE_PAGES, }; + + if (flags & MSG_SENDPAGE_NOTLAST) + msg.msg_flags |= MSG_MORE; + + if (flags & MSG_OOB) + return -EOPNOTSUPP; + + bvec_set_page(&bvec, page, size, offset); + iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size); + return kcm_sendmsg(sock, &msg, size); +} + static int kcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, int flags) {