From patchwork Fri Mar 31 16:08:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13196194 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 8B585C761A6 for ; Fri, 31 Mar 2023 16:11:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 293376B008C; Fri, 31 Mar 2023 12:11:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 243746B00A3; Fri, 31 Mar 2023 12:11:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10BEA6B00A5; Fri, 31 Mar 2023 12:11:05 -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 01BA86B008C for ; Fri, 31 Mar 2023 12:11:04 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C9BFC1A0D6F for ; Fri, 31 Mar 2023 16:11:04 +0000 (UTC) X-FDA: 80629682448.27.B8C01AF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf01.hostedemail.com (Postfix) with ESMTP id 162D540017 for ; Fri, 31 Mar 2023 16:11:01 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aZBFHWHc; spf=pass (imf01.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.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=1680279062; 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=KbTdePCQuY00Eh43/mGhCoyrcL0EDX5k9L0XaPYIsA0=; b=ozMvYYkEdAFonj/TkptQPERQ1okmdFVsiNjKLKNT0Bq7aIWUfXAm3fheZwpYydzVit5Y8W exzgFR5XtrgxvXsDDGb9WbcTpIHfOKG+abA7LreSm0BJB4cZYAttvXrvhgF+HGhrUD8IbL zIhm49GDBe8ilB/EXpblSvYsYTqOXuQ= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aZBFHWHc; spf=pass (imf01.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.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=1680279062; a=rsa-sha256; cv=none; b=YMlnvmmsAdoxyTMQ2+ij235trE9KnJhby5fwyuFEkxdS1uLmV4u9y/yDpdsXjHl0J/9d86 IhIEe3LZ5jr8CKsuZD90u4x5uGvejv6TG1LHz6j0cTr2s0woOKXr+IDArfG6LZu77SNINe 16eINPqqEA81HIXK5floNPSG9AF/Rxs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680279061; 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=KbTdePCQuY00Eh43/mGhCoyrcL0EDX5k9L0XaPYIsA0=; b=aZBFHWHcgm3f9EzBuWjVyEjEzYlLXSkJmSWI2kq0KOHcHlqeXRVnjHSPBrQmFiuB7Wo6Dk qM1kfV0tder+9HZvf7KHfDoE4qp2q5ufNpaKgmEXoK3bXF/zBpCCQK+yukp/Um8xF3Md3x 8RBXHePRmPjf3LalwHZlZMUIk+XNcBM= 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-615--qgn__EoP-eE5uusdvkQ2A-1; Fri, 31 Mar 2023 12:10:54 -0400 X-MC-Unique: -qgn__EoP-eE5uusdvkQ2A-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7FD543802AC1; Fri, 31 Mar 2023 16:10:52 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 63D4851FF; Fri, 31 Mar 2023 16:10:50 +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, Tom Herbert , Tom Herbert Subject: [PATCH v3 33/55] kcm: Support MSG_SPLICE_PAGES Date: Fri, 31 Mar 2023 17:08:52 +0100 Message-Id: <20230331160914.1608208-34-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.5 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 8pjycai81g4jyqd8juhcxj6sdgs5ksq3 X-Rspamd-Queue-Id: 162D540017 X-HE-Tag: 1680279061-406551 X-HE-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n9+eMxDfw7BugGuzcSj2spixFzTQb1QcGhI6dJSVET3I0lQZesZsyFRKfj6kbx4MpjZkrUgOWFgolGxrX/3S0dqTxbw5Y32S3rJkY6CmFLxwtDcMaBYqD/xlBT/k5PhY953pZ82suClZJmDJycoq3vy00mtM2aH8ZtI591gL1mVyvXDKq3kKfoj0TbGHr/rwUSrA67fOrN4bV7YzacijX4+r4sHMTCnllGlmMVEKZrhDxVBeXUiRTEWgw+0Wg/qZKO+2tQb4MXKFgxuQzlPKUhJ2YKmQfx+b6tgpSUFqW3lRFjF3NR47jlA0j6E0Tpa0XrqOGHpZjPAPTVdueo/dFWc2MjnfenarVK08oic575HN8PoHaaSkYrtZSvNMH74xDA3/rG5d6ShqRTkB9dZVWFG0ozb6qiZwtHcIOD6VAGYB5bm3L/eY0X/GpdiCDFihl/fRDLdRHOca0nUWfeAVQPiOsgNVzVbLUQ== 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 AF_KCM sendmsg() support MSG_SPLICE_PAGES. This causes pages to be spliced from the source iterator if possible and copied otherwise. 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 | 89 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 17 deletions(-) diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c index cfe828bd7fc6..0a3f79d81595 100644 --- a/net/kcm/kcmsock.c +++ b/net/kcm/kcmsock.c @@ -989,29 +989,84 @@ static int kcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) merge = false; } - copy = min_t(int, msg_data_left(msg), - pfrag->size - pfrag->offset); + if (msg->msg_flags & MSG_SPLICE_PAGES) { + struct page *page = NULL, **pages = &page; + size_t off; + bool put = false; + + err = iov_iter_extract_pages(&msg->msg_iter, &pages, + INT_MAX, 1, 0, &off); + if (err <= 0) { + err = err ?: -EIO; + goto out_error; + } + copy = err; - if (!sk_wmem_schedule(sk, copy)) - goto wait_for_memory; + if (skb_can_coalesce(skb, i, page, off)) { + skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); + goto coalesced; + } - err = skb_copy_to_page_nocache(sk, &msg->msg_iter, skb, - pfrag->page, - pfrag->offset, - copy); - if (err) - goto out_error; + if (!sk_wmem_schedule(sk, copy)) { + iov_iter_revert(&msg->msg_iter, copy); + goto wait_for_memory; + } + + if (!sendpage_ok(page)) { + const void *p = kmap_local_page(page); + void *q; - /* Update the skb. */ - if (merge) { - skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); + q = page_frag_memdup(NULL, p + off, copy, + sk->sk_allocation, ULONG_MAX); + kunmap_local(p); + if (!q) { + iov_iter_revert(&msg->msg_iter, copy); + err = -ENOMEM; + goto out_error; + } + page = virt_to_page(q); + off = offset_in_page(q); + put = true; + } + + skb_fill_page_desc_noacc(skb, i, page, off, copy); + if (put) + put_page(page); +coalesced: + skb_shinfo(skb)->flags |= SKBFL_SHARED_FRAG; + skb->len += copy; + skb->data_len += copy; + skb->truesize += copy; + sk->sk_wmem_queued += copy; + sk_mem_charge(sk, copy); + + if (head != skb) + head->truesize += copy; } else { - skb_fill_page_desc(skb, i, pfrag->page, - pfrag->offset, copy); - get_page(pfrag->page); + copy = min_t(int, msg_data_left(msg), + pfrag->size - pfrag->offset); + if (!sk_wmem_schedule(sk, copy)) + goto wait_for_memory; + + err = skb_copy_to_page_nocache(sk, &msg->msg_iter, skb, + pfrag->page, + pfrag->offset, + copy); + if (err) + goto out_error; + + /* Update the skb. */ + if (merge) { + skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy); + } else { + skb_fill_page_desc(skb, i, pfrag->page, + pfrag->offset, copy); + get_page(pfrag->page); + } + + pfrag->offset += copy; } - pfrag->offset += copy; copied += copy; if (head != skb) { head->len += copy;