git: 1f10f8aa0fae - stable/13 - LinuxKPI: skbuff: add memlimit tunable for 64bit systems

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Fri, 03 Jun 2022 16:43:08 UTC
The branch stable/13 has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=1f10f8aa0fae57d44175169a5c9f42e9dbee399f

commit 1f10f8aa0fae57d44175169a5c9f42e9dbee399f
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2022-05-05 20:43:34 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2022-06-03 15:48:15 +0000

    LinuxKPI: skbuff: add memlimit tunable for 64bit systems
    
    Some drivers, such as Realtek's rtw88, require 32bit DMA in
    a single segment.  busdma(9) has a hard time providing this
    currently for 3-ish pages at large quantities
    (see lkpi_pci_nseg1_fail in linux_pci.c e86707418c8e8).
    Work around this for now by allowing a tunable to enforce
    physical addresses allocation limits on 64bit platforms (ignoring PAE)
    using "old-school" contigmalloc(9) to avoid bouncing.
    
    A patch needing a custom kernel compiled was tested in the last weeks
    by rtw88 users providing the 32bit limit only hardcoded.  The 36bit
    limit can be found in iwlwifi so is added as a testing option along.
    
    This is put in as a bandaid for now, so people no longer need to patch
    and compile their own kernels to use rtw88 and to allow us to MFC the
    driver as well before the amounts of commits to track increases by
    much more.
    
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit 6a50157090f2d0c5ab8c570d9cf2e2e3535dbdbf)
---
 sys/compat/linuxkpi/common/src/linux_skbuff.c | 48 ++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/sys/compat/linuxkpi/common/src/linux_skbuff.c b/sys/compat/linuxkpi/common/src/linux_skbuff.c
index df1f6439c694..fb0fcaf99239 100644
--- a/sys/compat/linuxkpi/common/src/linux_skbuff.c
+++ b/sys/compat/linuxkpi/common/src/linux_skbuff.c
@@ -53,17 +53,35 @@ __FBSDID("$FreeBSD$");
 #include <linux/skbuff.h>
 #include <linux/slab.h>
 #include <linux/gfp.h>
+#ifdef __LP64__
+#include <linux/log2.h>
+#endif
 
-#ifdef SKB_DEBUG
 SYSCTL_DECL(_compat_linuxkpi);
 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, skb, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
     "LinuxKPI skbuff");
 
+#ifdef SKB_DEBUG
 int linuxkpi_debug_skb;
 SYSCTL_INT(_compat_linuxkpi_skb, OID_AUTO, debug, CTLFLAG_RWTUN,
     &linuxkpi_debug_skb, 0, "SKB debug level");
 #endif
 
+#ifdef __LP64__
+/*
+ * Realtek wireless drivers (e.g., rtw88) require 32bit DMA in a single segment.
+ * busdma(9) has a hard time providing this currently for 3-ish pages at large
+ * quantities (see lkpi_pci_nseg1_fail in linux_pci.c).
+ * Work around this for now by allowing a tunable to enforce physical addresses
+ * allocation limits on 64bit platforms using "old-school" contigmalloc(9) to
+ * avoid bouncing.
+ */
+static int linuxkpi_skb_memlimit;
+SYSCTL_INT(_compat_linuxkpi_skb, OID_AUTO, mem_limit, CTLFLAG_RDTUN,
+    &linuxkpi_skb_memlimit, 0, "SKB memory limit: 0=no limit, "
+    "1=32bit, 2=36bit, other=undef (currently 32bit)");
+#endif
+
 static MALLOC_DEFINE(M_LKPISKB, "lkpiskb", "Linux KPI skbuff compat");
 
 struct sk_buff *
@@ -77,7 +95,28 @@ linuxkpi_alloc_skb(size_t size, gfp_t gfp)
 	 * Using our own type here not backing my kmalloc.
 	 * We assume no one calls kfree directly on the skb.
 	 */
+#ifdef __LP64__
+	if (__predict_true(linuxkpi_skb_memlimit == 0)) {
+		skb = malloc(len, M_LKPISKB, linux_check_m_flags(gfp) | M_ZERO);
+	} else {
+		vm_paddr_t high;
+
+		switch (linuxkpi_skb_memlimit) {
+		case 2:
+			high = (0xfffffffff);	/* 1<<36 really. */
+			break;
+		case 1:
+		default:
+			high = (0xffffffff);	/* 1<<32 really. */
+			break;
+		}
+		len = roundup_pow_of_two(len);
+		skb = contigmalloc(len, M_LKPISKB,
+		    linux_check_m_flags(gfp) | M_ZERO, 0, high, PAGE_SIZE, 0);
+	}
+#else
 	skb = malloc(len, M_LKPISKB, linux_check_m_flags(gfp) | M_ZERO);
+#endif
 	if (skb == NULL)
 		return (skb);
 	skb->_alloc_len = len;
@@ -194,7 +233,14 @@ linuxkpi_kfree_skb(struct sk_buff *skb)
 		}
 	}
 
+#ifdef __LP64__
+	if (__predict_true(linuxkpi_skb_memlimit == 0))
+		free(skb, M_LKPISKB);
+	else
+		contigfree(skb, skb->_alloc_len, M_LKPISKB);
+#else
 	free(skb, M_LKPISKB);
+#endif
 }
 
 #ifdef DDB