Linux: Don't wake kswapd for larger spl_kvmalloc() attempts

When the ARC holds most of memory, which compaction can't move,
kswapd can make big contiguous blocks only by evicting a large part
of the ARC.  The amount grows exponentially with the order, while
the vmalloc fallback would succeed anyway at a much lower cost.
Newer kernels' vmalloc() also first tries high orders, waking
kswapd the same way.

For KM_SLEEP allocations whose order is above the midpoint between
PAGE_ALLOC_COSTLY_ORDER and spl_kmem_alloc_max, clear
__GFP_KSWAPD_RECLAIM for both the physical attempt and vmalloc().
Direct reclaim is kept, so allocations can't fail where they did
not before, and retries after a failure wake kswapd as usual.
To keep kswapd waking at the low watermark for the order-0 pages,
allocate and free one order-0 page that may wake it before such
requests.

Reviewed-by: Tony Hutter <hutter2@llnl.gov>
Signed-off-by: Alexander Motin <alexander.motin@TrueNAS.com>
Co-authored-by: Tigerblue77 <37409593+tigerblue77@users.noreply.github.com>
Closes #19935
diff --git a/include/os/linux/spl/sys/kmem.h b/include/os/linux/spl/sys/kmem.h
index 705f9c4..ac392e8 100644
--- a/include/os/linux/spl/sys/kmem.h
+++ b/include/os/linux/spl/sys/kmem.h
@@ -57,6 +57,7 @@
 
 static int spl_fstrans_check(void);
 void *spl_kvmalloc(size_t size, gfp_t flags);
+void spl_wake_kswapd_if_needed(void);
 
 /*
  * Convert a KM_* flags mask to its Linux GFP_* counterpart.  The conversion
diff --git a/man/man4/spl.4 b/man/man4/spl.4
index b5829ff..230a9ca 100644
--- a/man/man4/spl.4
+++ b/man/man4/spl.4
@@ -93,6 +93,11 @@
 but shift to
 .Fn vmalloc
 when exceeding this value.
+Contiguous allocations larger than the order half way between
+.Sy PAGE_ALLOC_COSTLY_ORDER
+and the order of this value will not wake kswapd to make them possible,
+since on systems with most of memory held by the ARC it may require
+evicting a large part of it.
 .
 .It Sy spl_kmem_cache_magazine_size Ns = Ns Sy 0 Pq uint
 Cache magazines are an optimization designed to minimize the cost of
diff --git a/module/os/linux/spl/spl-kmem-cache.c b/module/os/linux/spl/spl-kmem-cache.c
index 199872f..9cc8ac6 100644
--- a/module/os/linux/spl/spl-kmem-cache.c
+++ b/module/os/linux/spl/spl-kmem-cache.c
@@ -152,8 +152,10 @@
 	 * Slabs are only grown from spl_cache_grow_work(), which can sleep
 	 * and reclaim directly, so don't wake kswapd from here.  The order-0
 	 * allocations keep direct reclaim, so this cannot fail where it
-	 * would not have failed before.
+	 * would not have failed before.  Keep the order-0 wakeup at the low
+	 * watermark though.
 	 */
+	spl_wake_kswapd_if_needed();
 	ptr = spl_vmalloc(size, lflags & ~__GFP_KSWAPD_RECLAIM);
 
 	/* Resulting allocated memory will be page aligned */
diff --git a/module/os/linux/spl/spl-kmem.c b/module/os/linux/spl/spl-kmem.c
index 6524ddd..0a30ac6 100644
--- a/module/os/linux/spl/spl-kmem.c
+++ b/module/os/linux/spl/spl-kmem.c
@@ -130,6 +130,23 @@
 }
 EXPORT_SYMBOL(kmem_strfree);
 
+/*
+ * Without __GFP_KSWAPD_RECLAIM the order-0 pages backing a vmalloc() area
+ * don't wake kswapd at the low watermark, so free memory sinks to the min
+ * watermark and allocating threads have to reclaim it directly.  Since
+ * wakeup_kswapd() is not exported, allocate and free one order-0 page
+ * that may wake kswapd, but neither reclaims nor dips into reserves.
+ */
+void
+spl_wake_kswapd_if_needed(void)
+{
+	struct page *page = alloc_page(__GFP_KSWAPD_RECLAIM | __GFP_HIGHMEM |
+	    __GFP_NOWARN | __GFP_NOMEMALLOC);
+
+	if (page != NULL)
+		__free_page(page);
+}
+
 void *
 spl_kvmalloc(size_t size, gfp_t lflags)
 {
@@ -178,13 +195,13 @@
 	 * We first try kmalloc - even for big sizes - and fall back to
 	 * spl_vmalloc if that fails.
 	 *
-	 * Require both direct and kswapd reclaim permission in the original
-	 * mask for fallback.
-	 * Older kernels do not support nonblocking vmalloc allocations.
+	 * Require direct reclaim permission in the original mask for
+	 * fallback.  Older kernels do not support nonblocking vmalloc
+	 * allocations.
 	 */
 	void *ptr = kmalloc_node(size, kmalloc_lflags, NUMA_NO_NODE);
 	if (ptr || size <= PAGE_SIZE ||
-	    (lflags & __GFP_RECLAIM) != __GFP_RECLAIM) {
+	    !(lflags & __GFP_DIRECT_RECLAIM)) {
 		return (ptr);
 	}
 
@@ -230,6 +247,29 @@
 	 */
 	do {
 		/*
+		 * Don't wake kswapd for larger requests that can fall back to
+		 * vmalloc, nor from vmalloc, which on newer kernels first tries
+		 * high orders too.  When the ARC holds most of memory, which
+		 * compaction can't move, kswapd can make contiguous blocks
+		 * only by evicting a lot of it, growing exponentially with the
+		 * order, which soon costs more than vmalloc mapping and TLB
+		 * flush overheads.  Draw the line half way between the costly
+		 * order and spl_kmem_alloc_max in orders.  Direct reclaim is
+		 * kept, so this can't fail where it would not before, and the
+		 * retries below wake kswapd as usual.  Without the flag
+		 * spl_kvmalloc() skips kvmalloc(), which always wakes kswapd.
+		 * Keep the order-0 wakeup at the low watermark though.
+		 */
+		int mid_order = (PAGE_ALLOC_COSTLY_ORDER +
+		    get_order(spl_kmem_alloc_max)) / 2;
+		gfp_t vflags = lflags;
+		if ((lflags & (__GFP_RECLAIM | __GFP_RETRY_MAYFAIL)) ==
+		    __GFP_RECLAIM && get_order(size) > mid_order) {
+			vflags &= ~__GFP_KSWAPD_RECLAIM;
+			spl_wake_kswapd_if_needed();
+		}
+
+		/*
 		 * Calling kmalloc_node() when the size > spl_kmem_alloc_max
 		 * is unsafe.  This must fail for all kmem_alloc() and
 		 * kmem_zalloc() callers.
@@ -243,7 +283,7 @@
 		 */
 		if (size > spl_kmem_alloc_max) {
 			if (flags & KM_VMEM) {
-				ptr = spl_vmalloc(size, lflags);
+				ptr = spl_vmalloc(size, vflags);
 			} else {
 				return (NULL);
 			}
@@ -263,7 +303,7 @@
 #else
 			if ((flags & KM_VMEM) || !(flags & KM_NOSLEEP)) {
 #endif
-				ptr = spl_kvmalloc(size, lflags);
+				ptr = spl_kvmalloc(size, vflags);
 			} else {
 				ptr = kmalloc_node(size, lflags, node);
 			}