Skip to contentSkip to conversation

tun: avoid high-order page allocation for packet header

PATCH Superseded Tahsin Erdogan 2 participants 3 messages Jul 26, 2023 – Jul 31, 2023

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 drivers/net/tun.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/net/tun.c +4 -1
index d75456adc62a..b9d111fbea63 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 == 0)
+				linear = min_t(size_t, good_linear, copylen);
+
 			skb = tun_alloc_skb(tfile, align, copylen, linear,
 					    noblock);
 		}
Signature · 1 line
2.41.0
maintainer
#1 lore raw

drivers/net/tun.c static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, · line 1842

Quoted 6 lines · On Tue, 25 Jul 2023 20:09:36 -0700 Tahsin Erdogan wrote:
drivers/net/tun.c +2 -0
@@ -1838,6 +1838,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
 			 */
 			zerocopy = false;
 		} else {
+			if (linear == 0)
+				linear = min_t(size_t, good_linear, copylen);

nit: would you mind changing to !linear instead of linear == 0 ?

Also - I don't see linear explicitly getting set to 0. What guarantees that? What's the story there?

Otherwise seems reasonable. One more allocation but hopefully nobody will notice.

Signature · 1 line
pw-bot: cr
#2 lore raw

Thanks Jakub for considering this patch.

Quoted 11 lines · Jakub Kicinski

drivers/net/tun.c static ssize_t tun_get_user(struct tun_struct · line 1844

Quoted 8 lines · On Tue, 25 Jul 2023 20:09:36 -0700 Tahsin Erdogan wrote:
drivers/net/tun.c +2 -0
@@ -1838,6 +1838,9 @@ static ssize_t tun_get_user(struct tun_struct
 *tun, struct tun_file *tfile,
                       */
                      zerocopy = false;
              } else {
+                     if (linear == 0)
+                             linear = min_t(size_t, good_linear,
 copylen);

nit: would you mind changing to !linear instead of linear == 0 ?

Yes, I will fix this in v2.

Quoted 3 lines

Also - I don't see linear explicitly getting set to 0. What guarantees
that? What's the story there?

linear is set to 0 in the earlier if block. When gso.hdr_len is 0, linear also becomes 0:

        if (!zerocopy) {
                copylen = len;
                if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
                        linear = good_linear;
                else
                        linear = tun16_to_cpu(tun, gso.hdr_len);
        }