git: 9df5f29caf1a - main - LinuxKPI: skbuff: handle dev_alloc_skb() correctly

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Thu, 07 Apr 2022 20:57:00 UTC
The branch main has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=9df5f29caf1a818999d0e908cab95c83bd1f4323

commit 9df5f29caf1a818999d0e908cab95c83bd1f4323
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2022-04-07 18:06:55 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2022-04-07 20:55:53 +0000

    LinuxKPI: skbuff: handle dev_alloc_skb() correctly
    
    dev_alloc_skb() comapred to alloc_skb() reserves some headroom
    at the beginning of the skb which is used by drivers.
    Split the code for the two cases and reserve NET_SKB_PAD space,
    which should at least be 32 octets.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      3 days
---
 sys/compat/linuxkpi/common/include/linux/skbuff.h |  8 +++++---
 sys/compat/linuxkpi/common/src/linux_skbuff.c     | 21 +++++++++++++++++++--
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/skbuff.h b/sys/compat/linuxkpi/common/include/linux/skbuff.h
index 21ea7ee560e2..79d65c0169b3 100644
--- a/sys/compat/linuxkpi/common/include/linux/skbuff.h
+++ b/sys/compat/linuxkpi/common/include/linux/skbuff.h
@@ -38,6 +38,7 @@
 #ifndef	_LINUXKPI_LINUX_SKBUFF_H
 #define	_LINUXKPI_LINUX_SKBUFF_H
 
+#include <linux/kernel.h>
 #include <linux/page.h>
 #include <linux/dma-mapping.h>
 #include <linux/netdev_features.h>
@@ -84,7 +85,7 @@ enum sk_buff_pkt_type {
 	PACKET_OTHERHOST,
 };
 
-#define	NET_SKB_PAD		CACHE_LINE_SIZE		/* ? */
+#define	NET_SKB_PAD		max(CACHE_LINE_SIZE, 32)
 
 struct sk_buff_head {
 		/* XXX TODO */
@@ -168,6 +169,7 @@ struct sk_buff {
 /* -------------------------------------------------------------------------- */
 
 struct sk_buff *linuxkpi_alloc_skb(size_t, gfp_t);
+struct sk_buff *linuxkpi_dev_alloc_skb(size_t, gfp_t);
 void linuxkpi_kfree_skb(struct sk_buff *);
 
 /* -------------------------------------------------------------------------- */
@@ -187,7 +189,7 @@ __dev_alloc_skb(size_t len, gfp_t gfp)
 {
 	struct sk_buff *skb;
 
-	skb = alloc_skb(len, gfp);
+	skb = linuxkpi_dev_alloc_skb(len, gfp);
 	SKB_IMPROVE();
 	SKB_TRACE(skb);
 	return (skb);
@@ -198,7 +200,7 @@ dev_alloc_skb(size_t len)
 {
 	struct sk_buff *skb;
 
-	skb = alloc_skb(len, GFP_NOWAIT);
+	skb = __dev_alloc_skb(len, GFP_NOWAIT);
 	SKB_IMPROVE();
 	SKB_TRACE(skb);
 	return (skb);
diff --git a/sys/compat/linuxkpi/common/src/linux_skbuff.c b/sys/compat/linuxkpi/common/src/linux_skbuff.c
index efb97fa2f126..dfa9dfb2ceb4 100644
--- a/sys/compat/linuxkpi/common/src/linux_skbuff.c
+++ b/sys/compat/linuxkpi/common/src/linux_skbuff.c
@@ -80,7 +80,7 @@ linuxkpi_alloc_skb(size_t size, gfp_t gfp)
 	skb = malloc(len, M_LKPISKB, linux_check_m_flags(gfp) | M_ZERO);
 	if (skb == NULL)
 		return (skb);
-	skb->_alloc_len = size;
+	skb->_alloc_len = len;
 	skb->truesize = size;
 
 	skb->head = skb->data = skb->tail = (uint8_t *)(skb+1);
@@ -90,7 +90,24 @@ linuxkpi_alloc_skb(size_t size, gfp_t gfp)
 
 	skb->shinfo = (struct skb_shared_info *)(skb->end);
 
-	SKB_TRACE_FMT(skb, "data %p size %zu", skb->data, size);
+	SKB_TRACE_FMT(skb, "data %p size %zu", (skb) ? skb->data : NULL, size);
+	return (skb);
+}
+
+struct sk_buff *
+linuxkpi_dev_alloc_skb(size_t size, gfp_t gfp)
+{
+	struct sk_buff *skb;
+	size_t len;
+
+	len = size + NET_SKB_PAD;
+	skb = linuxkpi_alloc_skb(len, gfp);
+
+	if (skb != NULL)
+		skb_reserve(skb, NET_SKB_PAD);
+
+	SKB_TRACE_FMT(skb, "data %p size %zu len %zu",
+	    (skb) ? skb->data : NULL, size, len);
 	return (skb);
 }