Skip to contentSkip to conversation

Perf. optimizations for TCP Recv. Zerocopy

PATCH Superseded Arjun Roy 5 participants 18 messages Dec 2, 2020 – Dec 4, 2020

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Arjun Roy

author
#0 lore raw

v2 cover Perf. optimizations for TCP Recv. Zerocopy

From: Arjun Roy <[email protected]>

This patchset contains several optimizations for TCP Recv. Zerocopy.

Note this is v2 of the patchset, fixing two 32-bit compilation errors and a stylistic error.

Summarized:
1. It is possible that a read payload is not exactly page aligned - that there may exist "straggler" bytes that we cannot map into the caller's address space cleanly. For this, we allow the caller to provide as argument a "hybrid copy buffer", turning getsockopt(TCP_ZEROCOPY_RECEIVE) into a "hybrid" operation that allows the caller to avoid a subsequent recvmsg() call to read the stragglers.

2. Similarly, for "small" read payloads that are either below the size of a page, or small enough that remapping pages is not a performance win - we allow the user to short-circuit the remapping operations entirely and simply copy into the buffer provided.

Some of the patches in the middle of this set are refactors to support this "short-circuiting" optimization.

3. We allow the user to provide a hint that performing a page zap operation (and the accompanying TLB shootdown) may not be necessary, for the provided region that the kernel will attempt to map pages into. This allows us to avoid this expensive operation while holding the socket lock, which provides a significant performance advantage.

With all of these changes combined, "medium" sized receive traffic (multiple tens to few hundreds of KB) see significant efficiency gains when using TCP receive zerocopy instead of regular recvmsg(). For example, with RPC-style traffic with 32KB messages, there is a roughly 15% efficiency improvement when using zerocopy. Without these changes, there is a roughly 60-70% efficiency reduction with such messages when employing zerocopy.

Arjun Roy (8):
net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.
net-tcp: Introduce tcp_recvmsg_locked().
net-zerocopy: Refactor skb frag fast-forward op.
net-zerocopy: Refactor frag-is-remappable test.
net-zerocopy: Fast return if inq < PAGE_SIZE
net-zerocopy: Introduce short-circuit small reads.
net-zerocopy: Set zerocopy hint when data is copied
net-zerocopy: Defer vm zap unless actually needed.

 include/uapi/linux/tcp.h |   4 +
 net/ipv4/tcp.c           | 446 +++++++++++++++++++++++++++++----------
 2 files changed, 343 insertions(+), 107 deletions(-)
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
author
#1 lore raw

PATCH v2 1/8 net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.

From: Arjun Roy <[email protected]>

When TCP receive zerocopy does not successfully map the entire requested space, it outputs a 'hint' that the caller should recvmsg().

Augment zerocopy to accept a user buffer that it tries to copy this hint into - if it is possible to copy the entire hint, it will do so. This elides a recvmsg() call for received traffic that isn't exactly page-aligned in size.

This was tested with RPC-style traffic of arbitrary sizes. Normally, each received message required at least one getsockopt() call, and one recvmsg() call for the remaining unaligned data.

With this change, almost all of the recvmsg() calls are eliminated, leading to a savings of about 25%-50% in number of system calls for RPC-style workloads.

---
 include/uapi/linux/tcp.h |  2 +
 net/ipv4/tcp.c           | 84 ++++++++++++++++++++++++++++++++--------
 2 files changed, 70 insertions(+), 16 deletions(-)
include/uapi/linux/tcp.h +2 -0
index cfcb10b75483..62db78b9c1a0 100644
@@ -349,5 +349,7 @@ struct tcp_zerocopy_receive {
 	__u32 recv_skip_hint;	/* out: amount of bytes to skip */
 	__u32 inq; /* out: amount of bytes in read queue */
 	__s32 err; /* out: socket error */
+	__u64 copybuf_address;	/* in: copybuf address (small reads) */
+	__s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */
 };
 #endif /* _UAPI_LINUX_TCP_H */
net/ipv4/tcp.c +68 -16

130 lines not on this page · raw message

Signature · 1 line
2.29.2.576.ga3fc446d84-goog
author
#11 lore raw

PATCH v2 2/8 net-tcp: Introduce tcp_recvmsg_locked().

In reply to Arjun Roy

From: Arjun Roy <[email protected]>

Refactor tcp_recvmsg() by splitting it into locked and unlocked portions. Callers already holding the socket lock and not using ERRQUEUE/cmsg/busy polling can simply call tcp_recvmsg_locked(). This is in preparation for a short-circuit copy performed by TCP receive zerocopy for small (< PAGE_SIZE, or otherwise requested by the user) reads.

---
 net/ipv4/tcp.c | 69 ++++++++++++++++++++++++++++----------------------
 1 file changed, 39 insertions(+), 30 deletions(-)
net/ipv4/tcp.c +39 -30
index 887c6e986927..232cb478bacd 100644
@@ -2065,36 +2065,28 @@ static int tcp_inq_hint(struct sock *sk)
  *	Probably, code can be easily improved even more.
  */
 
-int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
-		int flags, int *addr_len)
+static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
+			      int nonblock, int flags,
+			      struct scm_timestamping_internal *tss,
+			      int *cmsg_flags)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
 	int copied = 0;
 	u32 peek_seq;
 	u32 *seq;
 	unsigned long used;
-	int err, inq;
+	int err;
 	int target;		/* Read at least this many bytes */
 	long timeo;
 	struct sk_buff *skb, *last;
 	u32 urg_hole = 0;
-	struct scm_timestamping_internal tss;
-	int cmsg_flags;
-
-	if (unlikely(flags & MSG_ERRQUEUE))
-		return inet_recv_error(sk, msg, len, addr_len);
-
-	if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue) &&
-	    (sk->sk_state == TCP_ESTABLISHED))
-		sk_busy_loop(sk, nonblock);
-
-	lock_sock(sk);
 
 	err = -ENOTCONN;
 	if (sk->sk_state == TCP_LISTEN)
 		goto out;
 
-	cmsg_flags = tp->recvmsg_inq ? 1 : 0;
+	if (tp->recvmsg_inq)
+		*cmsg_flags = 1;
 	timeo = sock_rcvtimeo(sk, nonblock);
 
 	/* Urgent data needs to be handled specially. */
@@ -2274,8 +2266,8 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
 		}
 
 		if (TCP_SKB_CB(skb)->has_rxtstamp) {
-			tcp_update_recv_tstamps(skb, &tss);
-			cmsg_flags |= 2;
+			tcp_update_recv_tstamps(skb, tss);
+			*cmsg_flags |= 2;
 		}
 
 		if (used + offset < skb->len)
@@ -2301,22 +2293,9 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
 
 	/* Clean up data we have read: This will do ACK frames. */
 	tcp_cleanup_rbuf(sk, copied);
-
-	release_sock(sk);
-
-	if (cmsg_flags) {
-		if (cmsg_flags & 2)
-			tcp_recv_timestamp(msg, sk, &tss);
-		if (cmsg_flags & 1) {
-			inq = tcp_inq_hint(sk);
-			put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
-		}
-	}
-
 	return copied;
 
 out:
-	release_sock(sk);
 	return err;
 
 recv_urg:
@@ -2327,6 +2306,36 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
 	err = tcp_peek_sndq(sk, msg, len);
 	goto out;
 }
+
+int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
+		int flags, int *addr_len)
+{
+	int cmsg_flags = 0, ret, inq;
+	struct scm_timestamping_internal tss;
+
+	if (unlikely(flags & MSG_ERRQUEUE))
+		return inet_recv_error(sk, msg, len, addr_len);
+
+	if (sk_can_busy_loop(sk) &&
+	    skb_queue_empty_lockless(&sk->sk_receive_queue) &&
+	    sk->sk_state == TCP_ESTABLISHED)
+		sk_busy_loop(sk, nonblock);
+
+	lock_sock(sk);
+	ret = tcp_recvmsg_locked(sk, msg, len, nonblock, flags, &tss,
+				 &cmsg_flags);
+	release_sock(sk);
+
+	if (cmsg_flags && ret >= 0) {
+		if (cmsg_flags & 2)
+			tcp_recv_timestamp(msg, sk, &tss);
+		if (cmsg_flags & 1) {
+			inq = tcp_inq_hint(sk);
+			put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
+		}
+	}
+	return ret;
+}
 EXPORT_SYMBOL(tcp_recvmsg);
 
 void tcp_set_state(struct sock *sk, int state)
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
author
#12 lore raw

PATCH v2 3/8 net-zerocopy: Refactor skb frag fast-forward op.

In reply to Arjun Roy

From: Arjun Roy <[email protected]>

Refactor skb frag fast-forwarding for tcp receive zerocopy. This is part of a patch set that introduces short-circuited hybrid copies for small receive operations, which results in roughly 33% fewer syscalls for small RPC scenarios.

skb_advance_to_frag(), given a skb and an offset into the skb, iterates from the first frag for the skb until we're at the frag specified by the offset. Assuming the offset provided refers to how many bytes in the skb are already read, the returned frag points to the next frag we may read from, while offset_frag is set to the number of bytes from this frag that we have already read.

If frag is not null and offset_frag is equal to 0, then we may be able to map this frag's page into the process address space with vm_insert_page(). However, if offset_frag is not equal to 0, then we cannot do so.

---
 net/ipv4/tcp.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
net/ipv4/tcp.c +26 -9
index 232cb478bacd..0f17b46c4c0c 100644
@@ -1743,6 +1743,28 @@ int tcp_mmap(struct file *file, struct socket *sock,
 }
 EXPORT_SYMBOL(tcp_mmap);
 
+static skb_frag_t *skb_advance_to_frag(struct sk_buff *skb, u32 offset_skb,
+				       u32 *offset_frag)
+{
+	skb_frag_t *frag;
+
+	offset_skb -= skb_headlen(skb);
+	if ((int)offset_skb < 0 || skb_has_frag_list(skb))
+		return NULL;
+
+	frag = skb_shinfo(skb)->frags;
+	while (offset_skb) {
+		if (skb_frag_size(frag) > offset_skb) {
+			*offset_frag = offset_skb;
+			return frag;
+		}
+		offset_skb -= skb_frag_size(frag);
+		++frag;
+	}
+	*offset_frag = 0;
+	return frag;
+}
+
 static int tcp_copy_straggler_data(struct tcp_zerocopy_receive *zc,
 				   struct sk_buff *skb, u32 copylen,
 				   u32 *offset, u32 *seq)
@@ -1869,6 +1891,8 @@ static int tcp_zerocopy_receive(struct sock *sk,
 	curr_addr = address;
 	while (length + PAGE_SIZE <= zc->length) {
 		if (zc->recv_skip_hint < PAGE_SIZE) {
+			u32 offset_frag;
+
 			/* If we're here, finish the current batch. */
 			if (pg_idx) {
 				ret = tcp_zerocopy_vm_insert_batch(vma, pages,
@@ -1889,16 +1913,9 @@ static int tcp_zerocopy_receive(struct sock *sk,
 				skb = tcp_recv_skb(sk, seq, &offset);
 			}
 			zc->recv_skip_hint = skb->len - offset;
-			offset -= skb_headlen(skb);
-			if ((int)offset < 0 || skb_has_frag_list(skb))
+			frags = skb_advance_to_frag(skb, offset, &offset_frag);
+			if (!frags || offset_frag)
 				break;
-			frags = skb_shinfo(skb)->frags;
-			while (offset) {
-				if (skb_frag_size(frags) > offset)
-					goto out;
-				offset -= skb_frag_size(frags);
-				frags++;
-			}
 		}
 		if (skb_frag_size(frags) != PAGE_SIZE || skb_frag_off(frags)) {
 			int remaining = zc->recv_skip_hint;
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
author
#13 lore raw

PATCH v2 4/8 net-zerocopy: Refactor frag-is-remappable test.

In reply to Arjun Roy

From: Arjun Roy <[email protected]>

Refactor frag-is-remappable test for tcp receive zerocopy. This is part of a patch set that introduces short-circuited hybrid copies for small receive operations, which results in roughly 33% fewer syscalls for small RPC scenarios.

---
 net/ipv4/tcp.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)
net/ipv4/tcp.c +26 -8
index 0f17b46c4c0c..4bdd4a358588 100644
@@ -1765,6 +1765,26 @@ static skb_frag_t *skb_advance_to_frag(struct sk_buff *skb, u32 offset_skb,
 	return frag;
 }
 
+static bool can_map_frag(const skb_frag_t *frag)
+{
+	return skb_frag_size(frag) == PAGE_SIZE && !skb_frag_off(frag);
+}
+
+static int find_next_mappable_frag(const skb_frag_t *frag,
+				   int remaining_in_skb)
+{
+	int offset = 0;
+
+	if (likely(can_map_frag(frag)))
+		return 0;
+
+	while (offset < remaining_in_skb && !can_map_frag(frag)) {
+		offset += skb_frag_size(frag);
+		++frag;
+	}
+	return offset;
+}
+
 static int tcp_copy_straggler_data(struct tcp_zerocopy_receive *zc,
 				   struct sk_buff *skb, u32 copylen,
 				   u32 *offset, u32 *seq)
@@ -1890,6 +1910,8 @@ static int tcp_zerocopy_receive(struct sock *sk,
 	ret = 0;
 	curr_addr = address;
 	while (length + PAGE_SIZE <= zc->length) {
+		int mappable_offset;
+
 		if (zc->recv_skip_hint < PAGE_SIZE) {
 			u32 offset_frag;
 
@@ -1917,15 +1939,11 @@ static int tcp_zerocopy_receive(struct sock *sk,
 			if (!frags || offset_frag)
 				break;
 		}
-		if (skb_frag_size(frags) != PAGE_SIZE || skb_frag_off(frags)) {
-			int remaining = zc->recv_skip_hint;
 
-			while (remaining && (skb_frag_size(frags) != PAGE_SIZE ||
-					     skb_frag_off(frags))) {
-				remaining -= skb_frag_size(frags);
-				frags++;
-			}
-			zc->recv_skip_hint -= remaining;
+		mappable_offset = find_next_mappable_frag(frags,
+							  zc->recv_skip_hint);
+		if (mappable_offset) {
+			zc->recv_skip_hint = mappable_offset;
 			break;
 		}
 		pages[pg_idx] = skb_frag_page(frags);
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
author
#14 lore raw

PATCH v2 5/8 net-zerocopy: Fast return if inq < PAGE_SIZE

In reply to Arjun Roy

From: Arjun Roy <[email protected]>

Sometimes, we may call tcp receive zerocopy when inq is 0, or inq < PAGE_SIZE, in which case we cannot remap pages. In this case, simply return the appropriate hint for regular copying without taking mmap_sem.

---
 net/ipv4/tcp.c | 8 ++++++++
 1 file changed, 8 insertions(+)
net/ipv4/tcp.c +8 -0
index 4bdd4a358588..b2f24a5ec230 100644
@@ -1889,6 +1889,14 @@ static int tcp_zerocopy_receive(struct sock *sk,
 
 	sock_rps_record_flow(sk);
 
+	if (inq < PAGE_SIZE) {
+		zc->length = 0;
+		zc->recv_skip_hint = inq;
+		if (!inq && sock_flag(sk, SOCK_DONE))
+			return -EIO;
+		return 0;
+	}
+
 	mmap_read_lock(current->mm);
 
 	vma = find_vma(current->mm, address);
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
author
#16 lore raw

PATCH v2 7/8 net-zerocopy: Set zerocopy hint when data is copied

In reply to Arjun Roy

From: Arjun Roy <[email protected]>

Set zerocopy hint, event when falling back to copy, so that the pending data can be efficiently received using zerocopy when possible.

---
 net/ipv4/tcp.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
net/ipv4/tcp.c +45 -0
index f67dd732a47b..49480ce162db 100644
@@ -1785,6 +1785,43 @@ static int find_next_mappable_frag(const skb_frag_t *frag,
 	return offset;
 }
 
+static void tcp_zerocopy_set_hint_for_skb(struct sock *sk,
+					  struct tcp_zerocopy_receive *zc,
+					  struct sk_buff *skb, u32 offset)
+{
+	u32 frag_offset, partial_frag_remainder = 0;
+	int mappable_offset;
+	skb_frag_t *frag;
+
+	/* worst case: skip to next skb. try to improve on this case below */
+	zc->recv_skip_hint = skb->len - offset;
+
+	/* Find the frag containing this offset (and how far into that frag) */
+	frag = skb_advance_to_frag(skb, offset, &frag_offset);
+	if (!frag)
+		return;
+
+	if (frag_offset) {
+		struct skb_shared_info *info = skb_shinfo(skb);
+
+		/* We read part of the last frag, must recvmsg() rest of skb. */
+		if (frag == &info->frags[info->nr_frags - 1])
+			return;
+
+		/* Else, we must at least read the remainder in this frag. */
+		partial_frag_remainder = skb_frag_size(frag) - frag_offset;
+		zc->recv_skip_hint -= partial_frag_remainder;
+		++frag;
+	}
+
+	/* partial_frag_remainder: If part way through a frag, must read rest.
+	 * mappable_offset: Bytes till next mappable frag, *not* counting bytes
+	 * in partial_frag_remainder.
+	 */
+	mappable_offset = find_next_mappable_frag(frag, zc->recv_skip_hint);
+	zc->recv_skip_hint = mappable_offset + partial_frag_remainder;
+}
+
 static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 			      int nonblock, int flags,
 			      struct scm_timestamping_internal *tss,
@@ -1815,6 +1852,14 @@ static int receive_fallback_to_copy(struct sock *sk,
 		return err;
 
 	zc->copybuf_len = err;
+	if (likely(zc->copybuf_len)) {
+		struct sk_buff *skb;
+		u32 offset;
+
+		skb = tcp_recv_skb(sk, tcp_sk(sk)->copied_seq, &offset);
+		if (skb)
+			tcp_zerocopy_set_hint_for_skb(sk, zc, skb, offset);
+	}
 	return 0;
 }
 
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
author
#17 lore raw

PATCH v2 8/8 net-zerocopy: Defer vm zap unless actually needed.

In reply to Arjun Roy

From: Arjun Roy <[email protected]>

Zapping pages is required only if we are calling vm_insert_page into a region where pages had previously been mapped. Receive zerocopy allows reusing such regions, and hitherto called zap_page_range() before calling vm_insert_page() in that range.

zap_page_range() can also be triggered from userspace with madvise(MADV_DONTNEED). If userspace is configured to call this before reusing a segment, or if there was nothing mapped at this virtual address to begin with, we can avoid calling zap_page_range() under the socket lock. That said, if userspace does not do that, then we are still responsible for calling zap_page_range().

This patch adds a flag that the user can use to hint to the kernel that a zap is not required. If the flag is not set, or if an older user application does not have a flags field at all, then the kernel calls zap_page_range as before. Also, if the flag is set but a zap is still required, the kernel performs that zap as necessary. Thus incorrectly indicating that a zap can be avoided does not change the correctness of operation. It also increases the batchsize for vm_insert_pages and prefetches the page struct for the batch since we're about to bump the refcount.

An alternative mechanism could be to not have a flag, assume by default a zap is not needed, and fall back to zapping if needed. However, this would harm performance for older applications for which a zap is necessary, and thus we implement it with an explicit flag so newer applications can opt in.

When using RPC-style traffic with medium sized (tens of KB) RPCs, this change yields an efficency improvement of about 30% for QPS/CPU usage.

---
 include/uapi/linux/tcp.h |   2 +
 net/ipv4/tcp.c           | 147 ++++++++++++++++++++++++++-------------
 2 files changed, 99 insertions(+), 50 deletions(-)
include/uapi/linux/tcp.h +2 -0
index 62db78b9c1a0..13ceeb395eb8 100644
@@ -343,6 +343,7 @@ struct tcp_diag_md5sig {
 
 /* setsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, ...) */
 
+#define TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT 0x1
 struct tcp_zerocopy_receive {
 	__u64 address;		/* in: address of mapping */
 	__u32 length;		/* in/out: number of bytes to map/mapped */
@@ -351,5 +352,6 @@ struct tcp_zerocopy_receive {
 	__s32 err; /* out: socket error */
 	__u64 copybuf_address;	/* in: copybuf address (small reads) */
 	__s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */
+	__u32 flags; /* in: flags */
 };
 #endif /* _UAPI_LINUX_TCP_H */
net/ipv4/tcp.c +97 -50

206 lines not on this page · raw message

Signature · 1 line
2.29.2.576.ga3fc446d84-goog
author
#15 lore raw

PATCH v2 6/8 net-zerocopy: Introduce short-circuit small reads.

In reply to Arjun Roy

From: Arjun Roy <[email protected]>

Sometimes, we may call tcp receive zerocopy when inq is 0, or inq < PAGE_SIZE, or inq is generally small enough that it is cheaper to copy rather than remap pages.

In these cases, we may want to either return early (inq=0) or attempt to use the provided copy buffer to simply copy the received data.

This allows us to save both system call overhead and the latency of acquiring mmap_sem in read mode for cases where it would be useless to do so.

This patchset enables this behaviour by:
1. Returning quickly if inq is 0.
2. Attempting to perform a regular copy if a hybrid copybuffer is provided and it is large enough to absorb all available bytes.
3. Return quickly if no such buffer was provided and there are less than PAGE_SIZE bytes available.

For small RPC ping-pong workloads, normally we would have 1 getsockopt(), 1 recvmsg() and 1 sendmsg() call per RPC. With this change, we remove the recvmsg() call entirely, reducing the syscall overhead by about 33%. In testing with small (hundreds of bytes) RPC traffic, this yields a syscall reduction of about 33% and an efficiency gain of about 3-5% when defined as QPS/CPU Util.

---
 net/ipv4/tcp.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
net/ipv4/tcp.c +36 -0
index b2f24a5ec230..f67dd732a47b 100644
@@ -1785,6 +1785,39 @@ static int find_next_mappable_frag(const skb_frag_t *frag,
 	return offset;
 }
 
+static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
+			      int nonblock, int flags,
+			      struct scm_timestamping_internal *tss,
+			      int *cmsg_flags);
+static int receive_fallback_to_copy(struct sock *sk,
+				    struct tcp_zerocopy_receive *zc, int inq)
+{
+	unsigned long copy_address = (unsigned long)zc->copybuf_address;
+	struct scm_timestamping_internal tss_unused;
+	int err, cmsg_flags_unused;
+	struct msghdr msg = {};
+	struct iovec iov;
+
+	zc->length = 0;
+	zc->recv_skip_hint = 0;
+
+	if (copy_address != zc->copybuf_address)
+		return -EINVAL;
+
+	err = import_single_range(READ, (void __user *)copy_address,
+				  inq, &iov, &msg.msg_iter);
+	if (err)
+		return err;
+
+	err = tcp_recvmsg_locked(sk, &msg, inq, /*nonblock=*/1, /*flags=*/0,
+				 &tss_unused, &cmsg_flags_unused);
+	if (err < 0)
+		return err;
+
+	zc->copybuf_len = err;
+	return 0;
+}
+
 static int tcp_copy_straggler_data(struct tcp_zerocopy_receive *zc,
 				   struct sk_buff *skb, u32 copylen,
 				   u32 *offset, u32 *seq)
@@ -1889,6 +1922,9 @@ static int tcp_zerocopy_receive(struct sock *sk,
 
 	sock_rps_record_flow(sk);
 
+	if (inq && inq <= copybuf_len)
+		return receive_fallback_to_copy(sk, zc, inq);
+
 	if (inq < PAGE_SIZE) {
 		zc->length = 0;
 		zc->recv_skip_hint = inq;
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
#2 lore raw

Re: [net-next v2 1/8] net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.

In reply to Arjun Roy

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 355

Quoted 12 lines · On Wed, 2 Dec 2020 14:09:38 -0800 Arjun Roy <[email protected]> wrote:
include/uapi/linux/tcp.h +2 -0
index cfcb10b75483..62db78b9c1a0 100644
@@ -349,5 +349,7 @@ struct tcp_zerocopy_receive {
 	__u32 recv_skip_hint;	/* out: amount of bytes to skip */
 	__u32 inq; /* out: amount of bytes in read queue */
 	__s32 err; /* out: socket error */
+	__u64 copybuf_address;	/* in: copybuf address (small reads) */
+	__s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */
 };
 #endif /* _UAPI_LINUX_TCP_H */

You can't safely grow the size of a userspace API without handling the case of older applications. Logic in setsockopt() would have to handle both old and new sizes of the structure.

#3 lore raw

Re: [net-next v2 1/8] net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.

Quoted 4 lines · Stephen Hemminger

On Wed, 2 Dec 2020 14:09:38 -0800
Arjun Roy <[email protected]> wrote:

Quoted 16 lines

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 355

Quoted 12 lines
include/uapi/linux/tcp.h +2 -0
index cfcb10b75483..62db78b9c1a0 100644
@@ -349,5 +349,7 @@ struct tcp_zerocopy_receive {
      __u32 recv_skip_hint;   /* out: amount of bytes to skip */
      __u32 inq; /* out: amount of bytes in read queue */
      __s32 err; /* out: socket error */
+     __u64 copybuf_address;  /* in: copybuf address (small reads) */
+     __s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */
 };
 #endif /* _UAPI_LINUX_TCP_H */

You can't safely grow the size of a userspace API without handling the case of older applications. Logic in setsockopt() would have to handle both old and new sizes of the structure.

Acknowledged, but tcp zerocopy receive is a special case: it does not exist in setsockopt().
Regarding old applications in the getsockopt() case: the current layout should handle older and newer callers as is (it devolves to only use the features the application provides buffer space for).

Please note also v3 for this patchset; it's the same as this but I forgot the signed-off-by statements here in v2.

Thanks,
-Arjun

#4 lore raw

RE: [net-next v2 1/8] net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.

In reply to Stephen Hemminger

From: Stephen Hemminger

Quoted 5 lines

Sent: 03 December 2020 00:15

On Wed, 2 Dec 2020 14:09:38 -0800
Arjun Roy <[email protected]> wrote:

Quoted 10 lines

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 353

Quoted 10 lines
include/uapi/linux/tcp.h +2 -0
index cfcb10b75483..62db78b9c1a0 100644
@@ -349,5 +349,7 @@ struct tcp_zerocopy_receive {
 	__u32 recv_skip_hint;	/* out: amount of bytes to skip */
 	__u32 inq; /* out: amount of bytes in read queue */
 	__s32 err; /* out: socket error */
+	__u64 copybuf_address;	/* in: copybuf address (small reads) */
+	__s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */

You need to swap the order of the above fields to avoid padding and differing alignments for 32bit and 64bit apps.

Quoted 6 lines

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 355

Quoted 2 lines
include/uapi/linux/tcp.h +0 -0
 };
 #endif /* _UAPI_LINUX_TCP_H */

You can't safely grow the size of a userspace API without handling the case of older applications. Logic in setsockopt() would have to handle both old and new sizes of the structure.

You also have to allow for old (working) applications being recompiled with the new headers.
So you cannot rely on the fields being zero even if you are passed the size of the structure.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

Acked-by Soheil Hassas Yeganeh
#5 lore raw

Re: [net-next v2 1/8] net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.

Quoted 7 lines · David Laight

From: Stephen Hemminger

Quoted 5 lines

Sent: 03 December 2020 00:15

On Wed, 2 Dec 2020 14:09:38 -0800
Arjun Roy <[email protected]> wrote:

Quoted 13 lines
Quoted 10 lines

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 353

Quoted 10 lines
include/uapi/linux/tcp.h +2 -0
index cfcb10b75483..62db78b9c1a0 100644
@@ -349,5 +349,7 @@ struct tcp_zerocopy_receive {
    __u32 recv_skip_hint;   /* out: amount of bytes to skip */
    __u32 inq; /* out: amount of bytes in read queue */
    __s32 err; /* out: socket error */
+   __u64 copybuf_address;  /* in: copybuf address (small reads) */
+   __s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */

You need to swap the order of the above fields to avoid padding and differing alignments for 32bit and 64bit apps.

I do not think so. Please review this patch series carefully.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
include/uapi/linux/tcp.h +0 -0
 };
 #endif /* _UAPI_LINUX_TCP_H */
Quoted 8 lines
Quoted 3 lines

You can't safely grow the size of a userspace API without handling the case of older applications. Logic in setsockopt() would have to handle both old and new sizes of the structure.

You also have to allow for old (working) applications being recompiled with the new headers.
So you cannot rely on the fields being zero even if you are passed the size of the structure.

This is too late, there is precedent for this.

We already mentioned this in tcp_mmap.c reference program.

commit bf5525f3a8e3248be5aa5defe5aaadd60e1c1ba1

Author: Eric Dumazet <[email protected]>
Date:   Tue May 5 20:51:06 2020 -0700

selftests: net: tcp_mmap: clear whole tcp_zerocopy_receive struct

We added fields in tcp_zerocopy_receive structure,
so make sure to clear all fields to not pass garbage to the kernel.

We were lucky because recent additions added 'out' parameters, still we need to clean our reference implementation, before folks copy/paste it.

Fixes c8856c051454 ("tcp-zerocopy: Return inq along with tcp

receive zerocopy.")

    Fixes: 33946518d493 ("tcp-zerocopy: Return sk_err (if set) along

with tcp receive zerocopy.")

(quoted diff) +0 -0

b/tools/testing/selftests/net/tcp_mmap.c
index 35505b31e5cc092453ea7b72d9dba45bed2d6549..62171fd638c817dabe2d988f3cfae74522112584 100644

tools/testing/selftests/net/tcp_mmap.c +2 -1
@@ -165,9 +165,10 @@ void *child_thread(void *arg)
                        socklen_t zc_len = sizeof(zc);
                        int res;
 
+                       memset(&zc, 0, sizeof(zc));
                        zc.address = (__u64)((unsigned long)addr);
                        zc.length = chunk_size;
-                       zc.recv_skip_hint = 0;
+
                        res = getsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE,
                                         &zc, &zc_len);
                        if (res == -1)
Quoted 7 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#7 lore raw

Re: [net-next v2 1/8] net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.

In reply to David Laight
Quoted 7 lines · David Laight

From: Stephen Hemminger

Quoted 5 lines

Sent: 03 December 2020 00:15

On Wed, 2 Dec 2020 14:09:38 -0800
Arjun Roy <[email protected]> wrote:

Quoted 14 lines
Quoted 10 lines

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 353

Quoted 10 lines
include/uapi/linux/tcp.h +2 -0
index cfcb10b75483..62db78b9c1a0 100644
@@ -349,5 +349,7 @@ struct tcp_zerocopy_receive {
    __u32 recv_skip_hint;   /* out: amount of bytes to skip */
    __u32 inq; /* out: amount of bytes in read queue */
    __s32 err; /* out: socket error */
+   __u64 copybuf_address;  /* in: copybuf address (small reads) */
+   __s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */

You need to swap the order of the above fields to avoid padding and differing alignments for 32bit and 64bit apps.

Just to double check, are you referring to the order of copybuf_address and copybuf_len?
If so, it seems that the current ordering is not creating any alignment holes, but flipping it would: https://godbolt.org/z/bdxP6b

Quoted 12 lines
Quoted 6 lines

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 355

Quoted 2 lines
include/uapi/linux/tcp.h +0 -0
 };
 #endif /* _UAPI_LINUX_TCP_H */

You can't safely grow the size of a userspace API without handling the case of older applications. Logic in setsockopt() would have to handle both old and new sizes of the structure.

You also have to allow for old (working) applications being recompiled with the new headers.
So you cannot rely on the fields being zero even if you are passed the size of the structure.

I think this should already be taken care of in the current code; the full-sized struct with new fields is being zero-initialized, then we're getting the user-provided optlen, then copying from userspace only that much data. So the newer fields would be zero in that case, so this should handle the case of new kernels but old applications. Does this address the concern, or am I misunderstanding?

Thanks,
-Arjun

Quoted 6 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#8 lore raw

Re: [net-next v2 1/8] net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.

Quoted 43 lines · Arjun Roy
Quoted 7 lines · David Laight

From: Stephen Hemminger

Quoted 5 lines

Sent: 03 December 2020 00:15

On Wed, 2 Dec 2020 14:09:38 -0800
Arjun Roy <[email protected]> wrote:

Quoted 14 lines
Quoted 10 lines

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 353

Quoted 10 lines
include/uapi/linux/tcp.h +2 -0
index cfcb10b75483..62db78b9c1a0 100644
@@ -349,5 +349,7 @@ struct tcp_zerocopy_receive {
    __u32 recv_skip_hint;   /* out: amount of bytes to skip */
    __u32 inq; /* out: amount of bytes in read queue */
    __s32 err; /* out: socket error */
+   __u64 copybuf_address;  /* in: copybuf address (small reads) */
+   __s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */

You need to swap the order of the above fields to avoid padding and differing alignments for 32bit and 64bit apps.

Just to double check, are you referring to the order of copybuf_address and copybuf_len?
If so, it seems that the current ordering is not creating any alignment holes, but flipping it would: https://godbolt.org/z/bdxP6b

Quoted 12 lines
Quoted 6 lines

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 355

Quoted 2 lines
include/uapi/linux/tcp.h +0 -0
 };
 #endif /* _UAPI_LINUX_TCP_H */

You can't safely grow the size of a userspace API without handling the case of older applications. Logic in setsockopt() would have to handle both old and new sizes of the structure.

You also have to allow for old (working) applications being recompiled with the new headers.
So you cannot rely on the fields being zero even if you are passed the size of the structure.

Quoted 7 lines

I think this should already be taken care of in the current code; the full-sized struct with new fields is being zero-initialized, then we're getting the user-provided optlen, then copying from userspace only that much data. So the newer fields would be zero in that case, so this should handle the case of new kernels but old applications. Does this address the concern, or am I misunderstanding?

Actually, on closer read, perhaps the following is what you have in mind for the old application?

struct zerocopy_args args;
args.address = ...;
args.length = ...;
args.recv_skip_hint = ...;
args.inq = ...;
args.err = ...;
getsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, &args, sizeof(args)); // sizeof(args) is now bigger when recompiled with new headers, but we did not explicitly set the new fields to 0, therefore issues

-Arjun

Quoted 9 lines

Thanks,
-Arjun

Quoted 6 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#6 lore raw

RE: [net-next v2 1/8] net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.

In reply to Eric Dumazet

From: Eric

Quoted 24 lines

Sent: 03 December 2020 23:15

Quoted 7 lines · David Laight

From: Stephen Hemminger

Quoted 5 lines

Sent: 03 December 2020 00:15

On Wed, 2 Dec 2020 14:09:38 -0800
Arjun Roy <[email protected]> wrote:

Quoted 13 lines
Quoted 10 lines

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 353

Quoted 10 lines
include/uapi/linux/tcp.h +2 -0
index cfcb10b75483..62db78b9c1a0 100644
@@ -349,5 +349,7 @@ struct tcp_zerocopy_receive {
    __u32 recv_skip_hint;   /* out: amount of bytes to skip */
    __u32 inq; /* out: amount of bytes in read queue */
    __s32 err; /* out: socket error */
+   __u64 copybuf_address;  /* in: copybuf address (small reads) */
+   __s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */

You need to swap the order of the above fields to avoid padding and differing alignments for 32bit and 64bit apps.

Quoted 1 line

I do not think so. Please review this patch series carefully.

Late at night.
The actual problem is 'tail padding'.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#9 lore raw

RE: [net-next v2 1/8] net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.

In reply to Arjun Roy

From: Arjun Roy

Quoted 1 line

Sent: 03 December 2020 23:25

...

Quoted 17 lines
Quoted 6 lines
Quoted 5 lines

You also have to allow for old (working) applications being recompiled with the new headers.
So you cannot rely on the fields being zero even if you are passed the size of the structure.

Quoted 7 lines

I think this should already be taken care of in the current code; the full-sized struct with new fields is being zero-initialized, then we're getting the user-provided optlen, then copying from userspace only that much data. So the newer fields would be zero in that case, so this should handle the case of new kernels but old applications. Does this address the concern, or am I misunderstanding?

Actually, on closer read, perhaps the following is what you have in mind for the old application?

Quoted 9 lines

struct zerocopy_args args;
args.address = ...;
args.length = ...;
args.recv_skip_hint = ...;
args.inq = ...;
args.err = ...;
getsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, &args, sizeof(args)); // sizeof(args) is now bigger when recompiled with new headers, but we did not explicitly set the new fields to 0, therefore issues

That's the one...

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#10 lore raw

Re: [net-next v2 1/8] net-zerocopy: Copy straggler unaligned data for TCP Rx. zerocopy.

Quoted 31 lines · David Laight

From: Arjun Roy

Quoted 1 line

Sent: 03 December 2020 23:25

...

Quoted 17 lines
Quoted 6 lines
Quoted 5 lines

You also have to allow for old (working) applications being recompiled with the new headers.
So you cannot rely on the fields being zero even if you are passed the size of the structure.

Quoted 7 lines

I think this should already be taken care of in the current code; the full-sized struct with new fields is being zero-initialized, then we're getting the user-provided optlen, then copying from userspace only that much data. So the newer fields would be zero in that case, so this should handle the case of new kernels but old applications. Does this address the concern, or am I misunderstanding?

Actually, on closer read, perhaps the following is what you have in mind for the old application?

Quoted 9 lines

struct zerocopy_args args;
args.address = ...;
args.length = ...;
args.recv_skip_hint = ...;
args.inq = ...;
args.err = ...;
getsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE, &args, sizeof(args)); // sizeof(args) is now bigger when recompiled with new headers, but we did not explicitly set the new fields to 0, therefore issues

Quoted 2 lines

That's the one...

I'll defer in this case to Eric's previous response in this thread (paraphrased: that there is precedent for this in tcp_mmap.c).

-Arjun

Quoted 5 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)