Skip to contentSkip to conversation

udp msg_zerocopy

PATCH Superseded Willem de Bruijn 2 participants 6 messages Nov 25, 2018

Subsystems (5)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover udp msg_zerocopy

From: Willem de Bruijn <[email protected]>

Enable MSG_ZEROCOPY for udp sockets

Patch 1/2 is the main patch, a rebase from RFC patch
http://patchwork.ozlabs.org/patch/899630/
more details in the patch commit message

Patch 2/2 runs the already existing udp zerocopy tests
as part of kselftest

See also recent Linux Plumbers presentation
https://linuxplumbersconf.org/event/2/contributions/106/attachments/104/128/willemdebruijn-lpc2018-udpgso-presentation-20181113.pdf

Willem de Bruijn (2):
udp: msg_zerocopy
tools/testing: extend zerocopy tests to udp

 include/linux/skbuff.h                      |  1 +
 net/core/skbuff.c                           | 15 +++++++++++++-
 net/core/sock.c                             |  5 ++++-
 net/ipv4/ip_output.c                        | 22 ++++++++++++++++++++-
 net/ipv6/ip6_output.c                       | 22 ++++++++++++++++++++-
 tools/testing/selftests/net/msg_zerocopy.c  |  3 ++-
 tools/testing/selftests/net/msg_zerocopy.sh |  2 ++
 tools/testing/selftests/net/udpgso_bench.sh |  3 +++
 8 files changed, 68 insertions(+), 5 deletions(-)
Signature · 1 line
2.20.0.rc0.387.gc7a69e6b6c-goog
#1 lore raw

PATCH 1/2 udp: msg_zerocopy

From: Willem de Bruijn <[email protected]>

Extend zerocopy to udp sockets. Allow setting sockopt SO_ZEROCOPY and interpret flag MSG_ZEROCOPY.

This patch was previously part of the zerocopy RFC patchsets. Zerocopy is not effective at small MTU. With segmentation offload building larger datagrams, the benefit of page flipping outweights the cost of generating a completion notification.

The datagram implementation has initial refcnt of 0, instead of 1 for TCP. The tcp_sendmsg_locked function has to hold a reference on uarg independent those held by the skbs it generates, because the skbs can be sent and freed in the function main loop. This is not needed for other sockets.

tools/testing/selftests/net/msg_zerocopy.sh after applying follow-on test patch and making skb_orphan_frags_rx same as skb_orphan_frags:

    ipv4 udp -t 1
    tx=191312 (11938 MB) txc=0 zc=n
    rx=191312 (11938 MB)
    ipv4 udp -z -t 1
    tx=304507 (19002 MB) txc=304507 zc=y
    rx=304507 (19002 MB)
    ok
    ipv6 udp -t 1
    tx=174485 (10888 MB) txc=0 zc=n
    rx=174485 (10888 MB)
    ipv6 udp -z -t 1
    tx=294801 (18396 MB) txc=294801 zc=y
    rx=294801 (18396 MB)
    ok
---
 include/linux/skbuff.h |  1 +
 net/core/skbuff.c      | 15 ++++++++++++++-
 net/core/sock.c        |  5 ++++-
 net/ipv4/ip_output.c   | 22 +++++++++++++++++++++-
 net/ipv6/ip6_output.c  | 22 +++++++++++++++++++++-
 5 files changed, 61 insertions(+), 4 deletions(-)
include/linux/skbuff.h +1 -0
index a2e8297a5b00..7c1a649d229b 100644
@@ -485,6 +485,7 @@ void sock_zerocopy_put_abort(struct ubuf_info *uarg);
 
 void sock_zerocopy_callback(struct ubuf_info *uarg, bool success);
 
+int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len);
 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
 			     struct msghdr *msg, int len,
 			     struct ubuf_info *uarg);
net/core/skbuff.c +14 -1
index 9a8a72cefe9b..b7f8894fbcb4 100644
@@ -957,7 +957,7 @@ struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
 	uarg->len = 1;
 	uarg->bytelen = size;
 	uarg->zerocopy = 1;
-	refcount_set(&uarg->refcnt, 1);
+	refcount_set(&uarg->refcnt, sk->sk_type == SOCK_STREAM ? 1 : 0);
 	sock_hold(sk);
 
 	return uarg;
@@ -1097,6 +1097,13 @@ void sock_zerocopy_put_abort(struct ubuf_info *uarg)
 		atomic_dec(&sk->sk_zckey);
 		uarg->len--;
 
+		/* Stream socks hold a ref for the syscall, as skbs can be sent
+		 * and freed inside the loop, dropping refcnt to 0 inbetween.
+		 * Datagrams do not need this, but sock_zerocopy_put expects it.
+		 */
+		if (sk->sk_type != SOCK_STREAM && !refcount_read(&uarg->refcnt))
+			refcount_set(&uarg->refcnt, 1);
+
 		sock_zerocopy_put(uarg);
 	}
 }
@@ -1105,6 +1112,12 @@ EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
 extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb,
 				   struct iov_iter *from, size_t length);
 
+int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len)
+{
+	return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len);
+}
+EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram);
+
 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
 			     struct msghdr *msg, int len,
 			     struct ubuf_info *uarg)
net/core/sock.c +4 -1
index 6d7e189e3cd9..f5bb89785e47 100644
@@ -1018,7 +1018,10 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
 
 	case SO_ZEROCOPY:
 		if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6) {
-			if (sk->sk_protocol != IPPROTO_TCP)
+			if (!((sk->sk_type == SOCK_STREAM &&
+			       sk->sk_protocol == IPPROTO_TCP) ||
+			      (sk->sk_type == SOCK_DGRAM &&
+			       sk->sk_protocol == IPPROTO_UDP)))
 				ret = -ENOTSUPP;
 		} else if (sk->sk_family != PF_RDS) {
 			ret = -ENOTSUPP;
net/ipv4/ip_output.c +21 -1
index c09219e7f230..2d1e4b67a1e8 100644
@@ -868,6 +868,7 @@ static int __ip_append_data(struct sock *sk,
 {
 	struct inet_sock *inet = inet_sk(sk);
 	struct sk_buff *skb;
+	struct ubuf_info *uarg = NULL;
 
 	struct ip_options *opt = cork->opt;
 	int hh_len;
@@ -916,6 +917,19 @@ static int __ip_append_data(struct sock *sk,
 	    (!exthdrlen || (rt->dst.dev->features & NETIF_F_HW_ESP_TX_CSUM)))
 		csummode = CHECKSUM_PARTIAL;
 
+	if (flags & MSG_ZEROCOPY && length) {
+		uarg = sock_zerocopy_realloc(sk, length, skb_zcopy(skb));
+		if (!uarg)
+			return -ENOBUFS;
+		if (rt->dst.dev->features & NETIF_F_SG &&
+		    csummode == CHECKSUM_PARTIAL) {
+			paged = true;
+		} else {
+			uarg->zerocopy = 0;
+			skb_zcopy_set(skb, uarg);
+		}
+	}
+
 	cork->length += length;
 
 	/* So, what's going on in the loop below?
@@ -1005,6 +1019,7 @@ static int __ip_append_data(struct sock *sk,
 			cork->tx_flags = 0;
 			skb_shinfo(skb)->tskey = tskey;
 			tskey = 0;
+			skb_zcopy_set(skb, uarg);
 
 			/*
 			 *	Find where to start putting bytes.
@@ -1067,7 +1082,7 @@ static int __ip_append_data(struct sock *sk,
 				err = -EFAULT;
 				goto error;
 			}
-		} else {
+		} else if (!uarg || !uarg->zerocopy) {
 			int i = skb_shinfo(skb)->nr_frags;
 
 			err = -ENOMEM;
@@ -1097,6 +1112,10 @@ static int __ip_append_data(struct sock *sk,
 			skb->data_len += copy;
 			skb->truesize += copy;
 			wmem_alloc_delta += copy;
+		} else {
+			err = skb_zerocopy_iter_dgram(skb, from, copy);
+			if (err < 0)
+				goto error;
 		}
 		offset += copy;
 		length -= copy;
@@ -1109,6 +1128,7 @@ static int __ip_append_data(struct sock *sk,
 error_efault:
 	err = -EFAULT;
 error:
+	sock_zerocopy_put_abort(uarg);
 	cork->length -= length;
 	IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
 	refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
net/ipv6/ip6_output.c +21 -1
index 89e0d5118afe..8a49ff985ed8 100644
@@ -1245,6 +1245,7 @@ static int __ip6_append_data(struct sock *sk,
 {
 	struct sk_buff *skb, *skb_prev = NULL;
 	unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu, pmtu;
+	struct ubuf_info *uarg = NULL;
 	int exthdrlen = 0;
 	int dst_exthdrlen = 0;
 	int hh_len;
@@ -1322,6 +1323,19 @@ static int __ip6_append_data(struct sock *sk,
 	    rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
 		csummode = CHECKSUM_PARTIAL;
 
+	if (flags & MSG_ZEROCOPY && length) {
+		uarg = sock_zerocopy_realloc(sk, length, skb_zcopy(skb));
+		if (!uarg)
+			return -ENOBUFS;
+		if (rt->dst.dev->features & NETIF_F_SG &&
+		    csummode == CHECKSUM_PARTIAL) {
+			paged = true;
+		} else {
+			uarg->zerocopy = 0;
+			skb_zcopy_set(skb, uarg);
+		}
+	}
+
 	/*
 	 * Let's try using as much space as possible.
 	 * Use MTU if total length of the message fits into the MTU.
@@ -1443,6 +1457,7 @@ static int __ip6_append_data(struct sock *sk,
 			skb_shinfo(skb)->tx_flags = cork->tx_flags;
 			cork->tx_flags = 0;
 			skb_shinfo(skb)->tskey = tskey;
+			skb_zcopy_set(skb, uarg);
 			tskey = 0;
 
 			/*
@@ -1505,7 +1520,7 @@ static int __ip6_append_data(struct sock *sk,
 				err = -EFAULT;
 				goto error;
 			}
-		} else {
+		} else if (!uarg || !uarg->zerocopy) {
 			int i = skb_shinfo(skb)->nr_frags;
 
 			err = -ENOMEM;
@@ -1535,6 +1550,10 @@ static int __ip6_append_data(struct sock *sk,
 			skb->data_len += copy;
 			skb->truesize += copy;
 			wmem_alloc_delta += copy;
+		} else {
+			err = skb_zerocopy_iter_dgram(skb, from, copy);
+			if (err < 0)
+				goto error;
 		}
 		offset += copy;
 		length -= copy;
@@ -1547,6 +1566,7 @@ static int __ip6_append_data(struct sock *sk,
 error_efault:
 	err = -EFAULT;
 error:
+	sock_zerocopy_put_abort(uarg);
 	cork->length -= length;
 	IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
 	refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
Signature · 1 line
2.20.0.rc0.387.gc7a69e6b6c-goog
#4 lore raw

PATCH 2/2 tools/testing: extend zerocopy tests to udp

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Both msg_zerocopy and udpgso_bench have udp zerocopy variants. Exercise these as part of the standard kselftest run.

With udp, msg_zerocopy has no control channel. Ensure that the receiver exits after the sender by accounting for the initial delay in starting them (in msg_zerocopy.sh).

---
 tools/testing/selftests/net/msg_zerocopy.c  | 3 ++-
 tools/testing/selftests/net/msg_zerocopy.sh | 2 ++
 tools/testing/selftests/net/udpgso_bench.sh | 3 +++
 3 files changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/net/msg_zerocopy.c +2 -1
index 406cc70c571d..4b02933cab8a 100644
@@ -651,12 +651,13 @@ static void do_flush_datagram(int fd, int type)
 
 static void do_rx(int domain, int type, int protocol)
 {
+	const int cfg_receiver_wait_ms = 400;
 	uint64_t tstop;
 	int fd;
 
 	fd = do_setup_rx(domain, type, protocol);
 
-	tstop = gettimeofday_ms() + cfg_runtime_ms;
+	tstop = gettimeofday_ms() + cfg_runtime_ms + cfg_receiver_wait_ms;
 	do {
 		if (type == SOCK_STREAM)
 			do_flush_tcp(fd);
tools/testing/selftests/net/msg_zerocopy.sh +2 -0
index c43c6debda06..825ffec85cea 100755
@@ -25,6 +25,8 @@ readonly path_sysctl_mem="net.core.optmem_max"
 if [[ "$#" -eq "0" ]]; then
 	$0 4 tcp -t 1
 	$0 6 tcp -t 1
+	$0 4 udp -t 1
+	$0 6 udp -t 1
 	echo "OK. All tests passed"
 	exit 0
 fi
tools/testing/selftests/net/udpgso_bench.sh +3 -0
index 0f0628613f81..5670a9ffd8eb 100755
@@ -35,6 +35,9 @@ run_udp() {
 
 	echo "udp gso"
 	run_in_netns ${args} -S 0
+
+	echo "udp gso zerocopy"
+	run_in_netns ${args} -S 0 -z
 }
 
 run_tcp() {
Signature · 1 line
2.20.0.rc0.387.gc7a69e6b6c-goog
maintainer
#5 lore raw
In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>
Date: Sat, 24 Nov 2018 14:24:29 -0500

Quoted 8 lines

From: Willem de Bruijn <[email protected]>

Enable MSG_ZEROCOPY for udp sockets

Patch 1/2 is the main patch, a rebase from RFC patch
http://patchwork.ozlabs.org/patch/899630/
more details in the patch commit message

Quoted 5 lines

Patch 2/2 runs the already existing udp zerocopy tests
as part of kselftest

See also recent Linux Plumbers presentation
https://linuxplumbersconf.org/event/2/contributions/106/attachments/104/128/willemdebruijn-lpc2018-udpgso-presentation-20181113.pdf

Series looks good, just one coding style to fix up, I'll reply to patch #1.

maintainer
#2 lore raw

Re: [PATCH net-next 1/2] udp: msg_zerocopy

In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>
Date: Sat, 24 Nov 2018 14:24:30 -0500

net/ipv4/ip_output.c static int __ip_append_data(struct sock *sk, · line 871

Quoted 9 lines
net/ipv4/ip_output.c +1 -0
index c09219e7f230..2d1e4b67a1e8 100644
@@ -868,6 +868,7 @@ static int __ip_append_data(struct sock *sk,
 {
 	struct inet_sock *inet = inet_sk(sk);
 	struct sk_buff *skb;
+	struct ubuf_info *uarg = NULL;

Reverse christmas tree, please.

Thanks.

#3 lore raw

Re: [PATCH net-next 1/2] udp: msg_zerocopy

Quoted 4 lines · David Miller

From: Willem de Bruijn <[email protected]>
Date: Sat, 24 Nov 2018 14:24:30 -0500

Quoted 11 lines

net/ipv4/ip_output.c static int __ip_append_data(struct sock *sk, · line 871

Quoted 9 lines
net/ipv4/ip_output.c +1 -0
index c09219e7f230..2d1e4b67a1e8 100644
@@ -868,6 +868,7 @@ static int __ip_append_data(struct sock *sk,
 {
      struct inet_sock *inet = inet_sk(sk);
      struct sk_buff *skb;
+     struct ubuf_info *uarg = NULL;

Reverse christmas tree, please.

Oh right, sorry.

Will wait with v2 for a day or two, in case anyone else has comments. Thanks for the quick review!