Skip to contentSkip to conversation

net: core: propagate unreadable flag in skb_zerocopy

PATCH Superseded Mina Almasry 2 participants 8 messages Aug 1 – Aug 3

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 net: core: propagate unreadable flag in skb_zerocopy

When skb_zerocopy() copies devmem payload fragments, it fails to update the target skb's unreadable flag. This causes the target to appear as readable memory.

Propagate the unreadable flag if any devmem fragments were copied from the source.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Cc Pavel Begunkov <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Bobby Eshleman <[email protected]>
Cc Florian Westphal <[email protected]>
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +3 -0
index ba3dbac80fb49..aebe8ea74776a 100644
@@ -3928,6 +3928,9 @@ skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
 	}
 	skb_shinfo(to)->nr_frags = j;
 
+	if (i > 0 && from->unreadable)
+		to->unreadable = 1;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(skb_zerocopy);
Signature · 1 line
2.55.0.571.g244d577d93-goog
#1 lore raw

PATCH 2/3 net: devmem: return EMSGSIZE on type mismatch

When a devmem payload mixes with a standard page payload, return -EMSGSIZE instead of -EFAULT. This enables tcp_sendmsg to seamlessly fall back to creating a new segment instead of failing the socket send.

Fixes bd61848900bff ("net: devmem: Implement TX path")
Cc Pavel Begunkov <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Bobby Eshleman <[email protected]>
---
 net/core/datagram.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/datagram.c +2 -2
index 173b5d97bd409..6f8ffd61bcab4 100644
@@ -638,7 +638,7 @@ int zerocopy_fill_skb_from_iter(struct sk_buff *skb,
 	int frag = skb_shinfo(skb)->nr_frags;
 
 	if (!skb_frags_readable(skb))
-		return -EFAULT;
+		return -EMSGSIZE;
 
 	while (length && iov_iter_count(from)) {
 		struct page *head, *last_head = NULL;
@@ -713,7 +713,7 @@ zerocopy_fill_skb_from_devmem(struct sk_buff *skb, struct iov_iter *from,
 	struct net_iov *niov;
 
 	if (i && skb_frags_readable(skb))
-		return -EFAULT;
+		return -EMSGSIZE;
 
 	/* Devmem filling works by taking an IOVEC from the user where the
 	 * iov_addrs are interpreted as an offset in bytes into the dma-buf to
Signature · 1 line
2.55.0.571.g244d577d93-goog
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH net v1 2/3] net: devmem: return EMSGSIZE on type mismatch

Quoted 9 lines · Mina Almasry

When a devmem payload mixes with a standard page payload, return -EMSGSIZE instead of -EFAULT. This enables tcp_sendmsg to seamlessly fall back to creating a new segment instead of failing the socket send.

Fixes bd61848900bff ("net: devmem: Implement TX path")
Cc Pavel Begunkov <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Bobby Eshleman <[email protected]>
Signature · 1 line
Pavel Begunkov
#3 lore raw

PATCH 3/3 net: tcp: block standard payload injection into devmem skbs

In reply to Mina Almasry

Protect tcp_sendmsg_locked() from mistakenly appending non-zerocopy page fragments to unreadable devmem skbs. Create a new segment instead.

Fixes bd61848900bff ("net: devmem: Implement TX path")
Cc Pavel Begunkov <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Bobby Eshleman <[email protected]>
---
 net/ipv4/tcp.c | 5 +++++
 1 file changed, 5 insertions(+)
net/ipv4/tcp.c +5 -0
index 455441f1b6949..186a36c698798 100644
@@ -1278,6 +1278,11 @@ 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 != MSG_ZEROCOPY && unlikely(!skb_frags_readable(skb))) {
+			tcp_mark_push(tp, skb);
+			goto new_segment;
+		}
+
 		if (zc == 0) {
 			bool merge = true;
 			int i = skb_shinfo(skb)->nr_frags;
Signature · 1 line
2.55.0.571.g244d577d93-goog
Reviewed-by on v1 3/3
#4 lore raw

Re: [PATCH net v1 3/3] net: tcp: block standard payload injection into devmem skbs

Quoted 2 lines · Mina Almasry

Protect tcp_sendmsg_locked() from mistakenly appending non-zerocopy page fragments to unreadable devmem skbs. Create a new segment instead.

Signature · 1 line
Pavel Begunkov
Reviewed-by on v1 1/3
#5 lore raw
In reply to Mina Almasry
Quoted 6 lines · Mina Almasry

When skb_zerocopy() copies devmem payload fragments, it fails to update the target skb's unreadable flag. This causes the target to appear as readable memory.

Propagate the unreadable flag if any devmem fragments were copied from the source.

From a quick look, it doesn't seem like the function would work reliably with netmems even with this patch, but that's a different kind of issue we can return to later. Curious, did you hit this in prod?

Quoted 9 lines
Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Cc Pavel Begunkov <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Bobby Eshleman <[email protected]>
Cc Florian Westphal <[email protected]>
---
  net/core/skbuff.c | 3 +++
  1 file changed, 3 insertions(+)
Signature · 1 line
Pavel Begunkov
#6 lore raw
Quoted 11 lines · Pavel Begunkov
Quoted 6 lines · Mina Almasry

When skb_zerocopy() copies devmem payload fragments, it fails to update the target skb's unreadable flag. This causes the target to appear as readable memory.

Propagate the unreadable flag if any devmem fragments were copied from the source.

From a quick look, it doesn't seem like the function would work reliably
with netmems even with this patch, but that's a different kind of issue we can return to later. Curious, did you hit this in prod?

skb_shinfo(to)->nr_frags = j;

+ if (i > 0 && from->unreadable)
+ to->unreadable = 1;

Taking another look, if j != i, it'd also have the mixing problem, and that can happen if it hits that head-to-frag copy.

if (!skb_frags_readable(from) && j > 0 && len)

	// goto fault;

Maybe add sth like this before the frag assignment loop?

Signature · 1 line
Pavel Begunkov
#7 lore raw
In reply to Pavel Begunkov
Quoted 6 lines · Pavel Begunkov
Quoted 4 lines · Mina Almasry

When skb_zerocopy() copies devmem payload fragments, it fails to update the target skb's unreadable flag. This causes the target to appear as readable memory.

Quoted 7 lines
Quoted 2 lines

Propagate the unreadable flag if any devmem fragments were copied from the source.

From a quick look, it doesn't seem like the function would work reliably
with netmems even with this patch, but that's a different kind of issue we can return to later. Curious, did you hit this in prod?

No, this is sashiko report on another patch.

I think you may be right about the need for another check. I'll double check and if this partial fix doesn't get merged as-is i'll submit a v2. Thanks!

Signature · 2 lines
Thanks,
Mina