From nobody Tue Feb 10 02:59:17 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 634C9C761A6 for ; Fri, 31 Mar 2023 16:13:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233309AbjCaQNP (ORCPT ); Fri, 31 Mar 2023 12:13:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232611AbjCaQMD (ORCPT ); Fri, 31 Mar 2023 12:12:03 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B25522930 for ; Fri, 31 Mar 2023 09:10:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680279023; 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=4OGPYhZN0j7iZw/KVrz34Q1/SXztHasPFDnntHCFNPo=; b=AOzUNC31Ku18ZgDpcN+6TtMgNDflZ2Y+MfH4wjlk9JR5uqnsvApPVJIqUmDoYD1pFTUB4A C96joJPs7IqyfeIqPyEFp+A75sLlRSUoitxPBMzQW6mF7d/d1c8ZQR4X9UmJSWGt5U8302 nhce1flDRkxfMqT03cbcJk8BArHFZJY= 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-79-meTwAxIdNDKMLFAudYCPiQ-1; Fri, 31 Mar 2023 12:10:18 -0400 X-MC-Unique: meTwAxIdNDKMLFAudYCPiQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 85825886067; Fri, 31 Mar 2023 16:10:14 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id A0AA7202701F; Fri, 31 Mar 2023 16:10:12 +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 19/55] af_unix: Support MSG_SPLICE_PAGES Date: Fri, 31 Mar 2023 17:08:38 +0100 Message-Id: <20230331160914.1608208-20-dhowells@redhat.com> In-Reply-To: <20230331160914.1608208-1-dhowells@redhat.com> References: <20230331160914.1608208-1-dhowells@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Make AF_UNIX sendmsg() support MSG_SPLICE_PAGES, splicing in pages from the source iterator if possible and copying the data in 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: "David S. Miller" cc: Eric Dumazet cc: Jakub Kicinski cc: Paolo Abeni cc: Jens Axboe cc: Matthew Wilcox cc: netdev@vger.kernel.org --- net/unix/af_unix.c | 93 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 16 deletions(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 347122c3575e..a9ad97f3c57f 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2151,6 +2151,53 @@ static int queue_oob(struct socket *sock, struct msg= hdr *msg, struct sock *other } #endif =20 +/* + * Extract pages from an iterator and add them to the socket buffer. + */ +static ssize_t unix_extract_bvec_to_skb(struct sk_buff *skb, + struct iov_iter *iter, ssize_t maxsize) +{ + struct page *pages[8], **ppages =3D pages; + unsigned int i, nr; + ssize_t ret =3D 0; + + while (iter->count > 0) { + size_t off, len; + + nr =3D min_t(size_t, MAX_SKB_FRAGS - skb_shinfo(skb)->nr_frags, + ARRAY_SIZE(pages)); + if (nr =3D=3D 0) + break; + + len =3D iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off); + if (len <=3D 0) { + if (!ret) + ret =3D len ?: -EIO; + break; + } + + i =3D 0; + do { + size_t part =3D min_t(size_t, PAGE_SIZE - off, len); + + if (skb_append_pagefrags(skb, pages[i++], off, part) < 0) { + if (!ret) + ret =3D -EMSGSIZE; + goto out; + } + off =3D 0; + ret +=3D part; + maxsize -=3D part; + len -=3D part; + } while (len > 0); + if (maxsize <=3D 0) + break; + } + +out: + return ret; +} + static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) { @@ -2194,19 +2241,25 @@ static int unix_stream_sendmsg(struct socket *sock,= struct msghdr *msg, while (sent < len) { size =3D len - sent; =20 - /* Keep two messages in the pipe so it schedules better */ - size =3D min_t(int, size, (sk->sk_sndbuf >> 1) - 64); + if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) { + skb =3D sock_alloc_send_pskb(sk, 0, 0, + msg->msg_flags & MSG_DONTWAIT, + &err, 0); + } else { + /* Keep two messages in the pipe so it schedules better */ + size =3D min_t(int, size, (sk->sk_sndbuf >> 1) - 64); =20 - /* allow fallback to order-0 allocations */ - size =3D min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ); + /* allow fallback to order-0 allocations */ + size =3D min_t(int, size, SKB_MAX_HEAD(0) + UNIX_SKB_FRAGS_SZ); =20 - data_len =3D max_t(int, 0, size - SKB_MAX_HEAD(0)); + data_len =3D max_t(int, 0, size - SKB_MAX_HEAD(0)); =20 - data_len =3D min_t(size_t, size, PAGE_ALIGN(data_len)); + data_len =3D min_t(size_t, size, PAGE_ALIGN(data_len)); =20 - skb =3D sock_alloc_send_pskb(sk, size - data_len, data_len, - msg->msg_flags & MSG_DONTWAIT, &err, - get_order(UNIX_SKB_FRAGS_SZ)); + skb =3D sock_alloc_send_pskb(sk, size - data_len, data_len, + msg->msg_flags & MSG_DONTWAIT, &err, + get_order(UNIX_SKB_FRAGS_SZ)); + } if (!skb) goto out_err; =20 @@ -2218,13 +2271,21 @@ static int unix_stream_sendmsg(struct socket *sock,= struct msghdr *msg, } fds_sent =3D true; =20 - skb_put(skb, size - data_len); - skb->data_len =3D data_len; - skb->len =3D size; - err =3D skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size); - if (err) { - kfree_skb(skb); - goto out_err; + if (unlikely(msg->msg_flags & MSG_SPLICE_PAGES)) { + size =3D unix_extract_bvec_to_skb(skb, &msg->msg_iter, size); + skb->data_len +=3D size; + skb->len +=3D size; + skb->truesize +=3D size; + refcount_add(size, &sk->sk_wmem_alloc); + } else { + skb_put(skb, size - data_len); + skb->data_len =3D data_len; + skb->len =3D size; + err =3D skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size); + if (err) { + kfree_skb(skb); + goto out_err; + } } =20 unix_state_lock(other);