Skip to contentSkip to conversation

tun: avoid high-order page allocation for packet header

PATCH Superseded Tahsin Erdogan 3 participants 7 messages Jul 31, 2023 – Aug 8, 2023

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tun: avoid high-order page allocation for packet header

When GSO is not enabled and a packet is transmitted via writev(), all payload is treated as header which requires a contiguous memory allocation. This allocation request is harder to satisfy, and may even fail if there is enough fragmentation.

Note that sendmsg() code path limits the linear copy length, so this change makes writev() and sendmsg() more consistent.

---
v2: replace linear == 0 with !linear
v1: https://lore.kernel.org/all/[email protected]/

 drivers/net/tun.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/tun.c +4 -1
index d75456adc62a..4c57804f4cbd 100644
@@ -1523,7 +1523,7 @@ static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
 	int err;
 
 	/* Under a page?  Don't bother with paged skb. */
-	if (prepad + len < PAGE_SIZE || !linear)
+	if (prepad + len < PAGE_SIZE)
 		linear = len;
 
 	skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
@@ -1838,6 +1838,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 			 */
 			zerocopy = false;
 		} else {
+			if (!linear)
+				linear = min_t(size_t, good_linear, copylen);
+
 			skb = tun_alloc_skb(tfile, align, copylen, linear,
 					    noblock);
 		}
Signature · 1 line
2.41.0
Tested-by Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Tahsin Erdogan

When GSO is not enabled and a packet is transmitted via writev(), all payload is treated as header which requires a contiguous memory allocation. This allocation request is harder to satisfy, and may even fail if there is enough fragmentation.

Note that sendmsg() code path limits the linear copy length, so this change makes writev() and sendmsg() more consistent.

---

I will have to tweak one existing packetdrill test, nothing major.

Thanks.

#2 lore raw
Quoted 11 lines · Eric Dumazet
Quoted 9 lines · Tahsin Erdogan

When GSO is not enabled and a packet is transmitted via writev(), all payload is treated as header which requires a contiguous memory allocation. This allocation request is harder to satisfy, and may even fail if there is enough fragmentation.

Note that sendmsg() code path limits the linear copy length, so this change makes writev() and sendmsg() more consistent.

Quoted 7 lines
Quoted 2 lines

---

I will have to tweak one existing packetdrill test, nothing major.

I have to take this back, sorry.

We need to change alloc_skb_with_frags() and tun.c to attempt high-order allocations,
otherwise tun users sending very large buffers will regress. (Even if this _could_ fail as you pointed out if memory is tight/fragmented)

I am working to make the change in alloc_skb_with_frags() and in tun, we can apply your patch after this prereq.

maintainer
#3 lore raw
Quoted 22 lines · Eric Dumazet
Quoted 11 lines · Eric Dumazet
Quoted 9 lines · Tahsin Erdogan

When GSO is not enabled and a packet is transmitted via writev(), all payload is treated as header which requires a contiguous memory allocation. This allocation request is harder to satisfy, and may even fail if there is enough fragmentation.

Note that sendmsg() code path limits the linear copy length, so this change makes writev() and sendmsg() more consistent.

Quoted 7 lines
Quoted 2 lines

---

I will have to tweak one existing packetdrill test, nothing major.

I have to take this back, sorry.

Quoted 7 lines

We need to change alloc_skb_with_frags() and tun.c to attempt high-order allocations,
otherwise tun users sending very large buffers will regress. (Even if this _could_ fail as you pointed out if memory is tight/fragmented)

I am working to make the change in alloc_skb_with_frags() and in tun, we can apply your patch after this prereq.

This exactly same allocation logic also exists in packet_alloc_skb and tap_alloc_skb. If changing one of them, perhaps should address convert all at the same time, to keep behavior consistent.

#4 lore raw
Quoted 4 lines · Willem de Bruijn
Quoted 2 lines

This exactly same allocation logic also exists in packet_alloc_skb and

tap_alloc_skb. If changing one of them, perhaps should address convert all at the same time, to keep behavior consistent.

Sure, I can take care of them as well.

#5 lore raw
In reply to Eric Dumazet
Quoted 26 lines · Eric Dumazet
Quoted 14 lines · Eric Dumazet
Quoted 12 lines · Tahsin Erdogan

When GSO is not enabled and a packet is transmitted via writev(), all
payload is treated as header which requires a contiguous memory allocation.
This allocation request is harder to satisfy, and may even fail if there is
enough fragmentation.

Note that sendmsg() code path limits the linear copy length, so this change
makes writev() and sendmsg() more consistent.

Quoted 7 lines
Quoted 2 lines

---

I will have to tweak one existing packetdrill test, nothing major.

I have to take this back, sorry.

Quoted 8 lines

We need to change alloc_skb_with_frags() and tun.c to attempt high-order allocations,
otherwise tun users sending very large buffers will regress. (Even if this _could_ fail as you pointed out if memory is tight/fragmented)

I am working to make the change in alloc_skb_with_frags() and in tun, we can apply your patch after this prereq.

Hi Eric, I believe your changes are merged. Are we good to apply my patch next?

#6 lore raw
Quoted 2 lines · Tahsin

Hi Eric, I believe your changes are merged. Are we good to apply my patch next?

Sure thing, please rebase and resend.

Thanks.