From: David Ahern <dsahern@kernel.org>
To: Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
David Howells <dhowells@redhat.com>
Cc: netdev@vger.kernel.org, "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Matthew Wilcox <willy@infradead.org>,
Al Viro <viro@zeniv.linux.org.uk>,
Christoph Hellwig <hch@infradead.org>,
Jens Axboe <axboe@kernel.dk>, Jeff Layton <jlayton@kernel.org>,
Christian Brauner <brauner@kernel.org>,
Chuck Lever III <chuck.lever@oracle.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org
Subject: Re: [PATCH net-next v5 05/19] tcp: Support MSG_SPLICE_PAGES
Date: Thu, 6 Apr 2023 20:13:48 -0600 [thread overview]
Message-ID: <3a109738-f666-20e5-a135-b466c5546c29@kernel.org> (raw)
In-Reply-To: <CAF=yD-+QCYsjuRvzTOjhn=sKCWwOd5ZWxG6VS-xkYEoxzGkUkA@mail.gmail.com>
On 4/6/23 7:56 PM, Willem de Bruijn wrote:
> On Thu, Apr 6, 2023 at 5:43 AM David Howells <dhowells@redhat.com> wrote:
>>
>> Make TCP's sendmsg() support MSG_SPLICE_PAGES. This causes pages to 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 <dhowells@redhat.com>
>> cc: Eric Dumazet <edumazet@google.com>
>> cc: "David S. Miller" <davem@davemloft.net>
>> cc: David Ahern <dsahern@kernel.org>
>> cc: Jakub Kicinski <kuba@kernel.org>
>> cc: Paolo Abeni <pabeni@redhat.com>
>> cc: Jens Axboe <axboe@kernel.dk>
>> cc: Matthew Wilcox <willy@infradead.org>
>> cc: netdev@vger.kernel.org
>> ---
>> net/ipv4/tcp.c | 67 ++++++++++++++++++++++++++++++++++++++++++++------
>> 1 file changed, 60 insertions(+), 7 deletions(-)
>>
>> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
>> index fd68d49490f2..510bacc7ce7b 100644
>> --- a/net/ipv4/tcp.c
>> +++ b/net/ipv4/tcp.c
>> @@ -1221,7 +1221,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;
>> @@ -1232,17 +1232,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 = 1;
>> } 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 = 1;
>> + 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 = 2;
>> }
>>
>> if (unlikely(flags & MSG_FASTOPEN || inet_sk(sk)->defer_connect) &&
>> @@ -1305,7 +1310,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)
>> @@ -1346,7 +1351,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);
>> @@ -1391,7 +1396,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 == 1) {
>
> Instead of 1 and 2, MSG_ZEROCOPY and MSG_SPLICE_PAGES make the code
> more self-documenting.
>
>> /* First append to a fragless skb builds initial
>> * pure zerocopy skb
>> */
>> @@ -1412,6 +1417,54 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
>> if (err < 0)
>> goto do_error;
>> copy = err;
>> + } else if (zc == 2) {
>> + /* Splice in data. */
>> + struct page *page = NULL, **pages = &page;
>> + size_t off = 0, part;
>> + bool can_coalesce;
>> + int i = skb_shinfo(skb)->nr_frags;
>> +
>> + copy = iov_iter_extract_pages(&msg->msg_iter, &pages,
>> + copy, 1, 0, &off);
>> + if (copy <= 0) {
>> + err = copy ?: -EIO;
>> + goto do_error;
>> + }
>> +
>> + can_coalesce = skb_can_coalesce(skb, i, page, off);
>> + if (!can_coalesce && i >= READ_ONCE(sysctl_max_skb_frags)) {
>> + tcp_mark_push(tp, skb);
>> + iov_iter_revert(&msg->msg_iter, copy);
>> + goto new_segment;
>> + }
>> + if (tcp_downgrade_zcopy_pure(sk, skb)) {
>> + iov_iter_revert(&msg->msg_iter, copy);
>> + goto wait_for_space;
>> + }
>> +
>> + part = tcp_wmem_schedule(sk, copy);
>> + iov_iter_revert(&msg->msg_iter, copy - part);
>> + if (!part)
>> + goto wait_for_space;
>> + copy = part;
>> +
>> + if (can_coalesce) {
>> + skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
>> + } else {
>> + get_page(page);
>> + skb_fill_page_desc_noacc(skb, i, page, off, copy);
>> + }
>> + page = NULL;
>> +
>> + if (!(flags & MSG_NO_SHARED_FRAGS))
>> + skb_shinfo(skb)->flags |= SKBFL_SHARED_FRAG;
>> +
>> + skb->len += copy;
>> + skb->data_len += copy;
>> + skb->truesize += copy;
>> + sk_wmem_queued_add(sk, copy);
>> + sk_mem_charge(sk, copy);
>> +
>
> Similar to udp, perhaps in a helper?
tcp_sendmsg_locked is already more than 250 lines long and this 47 lines
is compounding it. I was staring at this code 2 weeks ago wondering if
it can be split or refactored to reduce the complexity.
next prev parent reply other threads:[~2023-04-07 2:13 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-06 9:42 [PATCH net-next v5 00/19] splice, net: Replace sendpage with sendmsg(MSG_SPLICE_PAGES), part 1 David Howells
2023-04-06 9:42 ` [PATCH net-next v5 01/19] net: Declare MSG_SPLICE_PAGES internal sendmsg() flag David Howells
2023-04-06 9:42 ` [PATCH net-next v5 02/19] mm: Move the page fragment allocator from page_alloc.c into its own file David Howells
2023-04-06 9:42 ` [PATCH net-next v5 03/19] mm: Make the page_frag_cache allocator use multipage folios David Howells
2023-04-07 0:54 ` Jakub Kicinski
2023-04-06 9:42 ` [PATCH net-next v5 04/19] mm: Make the page_frag_cache allocator use per-cpu David Howells
2023-04-06 9:42 ` [PATCH net-next v5 05/19] tcp: Support MSG_SPLICE_PAGES David Howells
2023-04-07 1:56 ` Willem de Bruijn
2023-04-07 2:13 ` David Ahern [this message]
2023-04-06 9:42 ` [PATCH net-next v5 06/19] tcp: Make sendmsg(MSG_SPLICE_PAGES) copy unspliceable data David Howells
2023-04-07 2:01 ` Willem de Bruijn
2023-04-06 9:42 ` [PATCH net-next v5 07/19] tcp: Convert do_tcp_sendpages() to use MSG_SPLICE_PAGES David Howells
2023-04-06 9:42 ` [PATCH net-next v5 08/19] tcp_bpf: Inline do_tcp_sendpages as it's now a wrapper around tcp_sendmsg David Howells
2023-04-06 9:42 ` [PATCH net-next v5 09/19] espintcp: Inline do_tcp_sendpages() David Howells
2023-04-06 9:42 ` [PATCH net-next v5 10/19] tls: " David Howells
2023-04-06 9:42 ` [PATCH net-next v5 11/19] siw: " David Howells
2023-04-06 15:36 ` Bernard Metzler
2023-04-06 9:42 ` [PATCH net-next v5 12/19] tcp: Fold do_tcp_sendpages() into tcp_sendpage_locked() David Howells
2023-04-06 9:42 ` [PATCH net-next v5 13/19] ip, udp: Support MSG_SPLICE_PAGES David Howells
2023-04-06 9:42 ` [PATCH net-next v5 14/19] ip, udp: Make sendmsg(MSG_SPLICE_PAGES) copy unspliceable data David Howells
2023-04-06 9:42 ` [PATCH net-next v5 15/19] ip6, udp6: Support MSG_SPLICE_PAGES David Howells
2023-04-06 9:42 ` [PATCH net-next v5 16/19] udp: Convert udp_sendpage() to use MSG_SPLICE_PAGES David Howells
2023-04-06 9:42 ` [PATCH net-next v5 17/19] ip: Remove ip_append_page() David Howells
2023-04-06 9:42 ` [PATCH net-next v5 18/19] af_unix: Support MSG_SPLICE_PAGES David Howells
2023-04-06 9:42 ` [PATCH net-next v5 19/19] af_unix: Make sendmsg(MSG_SPLICE_PAGES) copy unspliceable data David Howells
2023-04-06 9:46 ` [PATCH net-next v5 00/19] splice, net: Replace sendpage with sendmsg(MSG_SPLICE_PAGES), part 1 Eric Dumazet
2023-04-06 10:56 ` David Howells
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3a109738-f666-20e5-a135-b466c5546c29@kernel.org \
--to=dsahern@kernel.org \
--cc=axboe@kernel.dk \
--cc=brauner@kernel.org \
--cc=chuck.lever@oracle.com \
--cc=davem@davemloft.net \
--cc=dhowells@redhat.com \
--cc=edumazet@google.com \
--cc=hch@infradead.org \
--cc=jlayton@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
--cc=willemdebruijn.kernel@gmail.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).