186 lines
5.7 KiB
Diff
186 lines
5.7 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Tyler Smith <74526874+tylertms@users.noreply.github.com>
|
|
Date: Tue, 3 Nov 2026 04:41:28 +0000
|
|
Subject: [PATCH] refactor(mm): share boot hash sizing with native allocators
|
|
|
|
---
|
|
mm/mm_init.c | 61 +++-----------------------------------
|
|
mm/system_hash.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++
|
|
2 files changed, 80 insertions(+), 57 deletions(-)
|
|
create mode 100644 mm/system_hash.h
|
|
|
|
diff --git a/mm/mm_init.c b/mm/mm_init.c
|
|
index d50a54dbb1064bc1dd1e8c14831a8c2480925c66..3553c421fdcfc718787ee0185d2c6bf4d2ea7186 100644
|
|
--- a/mm/mm_init.c
|
|
+++ b/mm/mm_init.c
|
|
@@ -33,6 +33,7 @@
|
|
#include <linux/kexec_handover.h>
|
|
#include <linux/hugetlb.h>
|
|
#include "internal.h"
|
|
+#include "system_hash.h"
|
|
#include "slab.h"
|
|
#include "shuffle.h"
|
|
|
|
@@ -2362,21 +2363,6 @@ void __init page_alloc_init_late(void)
|
|
page_alloc_sysctl_init();
|
|
}
|
|
|
|
-/*
|
|
- * Adaptive scale is meant to reduce sizes of hash tables on large memory
|
|
- * machines. As memory size is increased the scale is also increased but at
|
|
- * slower pace. Starting from ADAPT_SCALE_BASE (64G), every time memory
|
|
- * quadruples the scale is increased by one, which means the size of hash table
|
|
- * only doubles, instead of quadrupling as well.
|
|
- * Because 32-bit systems cannot have large physical memory, where this scaling
|
|
- * makes sense, it is disabled on such platforms.
|
|
- */
|
|
-#if __BITS_PER_LONG > 32
|
|
-#define ADAPT_SCALE_BASE (64ul << 30)
|
|
-#define ADAPT_SCALE_SHIFT 2
|
|
-#define ADAPT_SCALE_NPAGES (ADAPT_SCALE_BASE >> PAGE_SHIFT)
|
|
-#endif
|
|
-
|
|
/*
|
|
* allocate a large system hash table from bootmem
|
|
* - it is assumed that the hash table must contain an exact power-of-2
|
|
@@ -2393,54 +2379,15 @@ void *__init alloc_large_system_hash(const char *tablename,
|
|
unsigned long low_limit,
|
|
unsigned long high_limit)
|
|
{
|
|
- unsigned long long max = high_limit;
|
|
unsigned long log2qty, size;
|
|
void *table;
|
|
gfp_t gfp_flags;
|
|
bool virt;
|
|
bool huge;
|
|
|
|
- /* allow the kernel cmdline to have a say */
|
|
- if (!numentries) {
|
|
- /* round applicable memory size up to nearest megabyte */
|
|
- numentries = nr_kernel_pages;
|
|
-
|
|
- /* It isn't necessary when PAGE_SIZE >= 1MB */
|
|
- if (PAGE_SIZE < SZ_1M)
|
|
- numentries = round_up(numentries, SZ_1M / PAGE_SIZE);
|
|
-
|
|
-#if __BITS_PER_LONG > 32
|
|
- if (!high_limit) {
|
|
- unsigned long adapt;
|
|
-
|
|
- for (adapt = ADAPT_SCALE_NPAGES; adapt < numentries;
|
|
- adapt <<= ADAPT_SCALE_SHIFT)
|
|
- scale++;
|
|
- }
|
|
-#endif
|
|
-
|
|
- /* limit to 1 bucket per 2^scale bytes of low memory */
|
|
- if (scale > PAGE_SHIFT)
|
|
- numentries >>= (scale - PAGE_SHIFT);
|
|
- else
|
|
- numentries <<= (PAGE_SHIFT - scale);
|
|
-
|
|
- if (unlikely((numentries * bucketsize) < PAGE_SIZE))
|
|
- numentries = PAGE_SIZE / bucketsize;
|
|
- }
|
|
- numentries = roundup_pow_of_two(numentries);
|
|
-
|
|
- /* limit allocation size to 1/16 total memory by default */
|
|
- if (max == 0) {
|
|
- max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
|
|
- max = div64_ul(max, bucketsize);
|
|
- }
|
|
- max = min(max, 0x80000000ULL);
|
|
-
|
|
- if (numentries < low_limit)
|
|
- numentries = low_limit;
|
|
- if (numentries > max)
|
|
- numentries = max;
|
|
+ numentries = system_hash_entries(bucketsize, numentries, scale,
|
|
+ low_limit, high_limit, nr_kernel_pages,
|
|
+ nr_all_pages);
|
|
|
|
log2qty = ilog2(numentries);
|
|
|
|
diff --git a/mm/system_hash.h b/mm/system_hash.h
|
|
new file mode 100644
|
|
index 0000000000000000000000000000000000000000..d66b0df97ab577cd9ec650b9a9712ffc0f194e9e
|
|
--- /dev/null
|
|
+++ b/mm/system_hash.h
|
|
@@ -0,0 +1,76 @@
|
|
+/* SPDX-License-Identifier: GPL-2.0-only */
|
|
+#ifndef _MM_SYSTEM_HASH_H
|
|
+#define _MM_SYSTEM_HASH_H
|
|
+
|
|
+#include <linux/log2.h>
|
|
+#include <linux/mm.h>
|
|
+#include <linux/sizes.h>
|
|
+
|
|
+/*
|
|
+ * Adaptive scale is meant to reduce sizes of hash tables on large memory
|
|
+ * machines. As memory size is increased the scale is also increased but at
|
|
+ * slower pace. Starting from ADAPT_SCALE_BASE (64G), every time memory
|
|
+ * quadruples the scale is increased by one, which means the size of hash table
|
|
+ * only doubles, instead of quadrupling as well.
|
|
+ * Because 32-bit systems cannot have large physical memory, where this scaling
|
|
+ * makes sense, it is disabled on such platforms.
|
|
+ */
|
|
+#if __BITS_PER_LONG > 32
|
|
+#define ADAPT_SCALE_BASE (64ul << 30)
|
|
+#define ADAPT_SCALE_SHIFT 2
|
|
+#define ADAPT_SCALE_NPAGES (ADAPT_SCALE_BASE >> PAGE_SHIFT)
|
|
+#endif
|
|
+
|
|
+static inline unsigned long system_hash_entries(unsigned long bucketsize,
|
|
+ unsigned long numentries, int scale, unsigned long low_limit,
|
|
+ unsigned long high_limit, unsigned long nr_kernel_pages,
|
|
+ unsigned long nr_all_pages)
|
|
+{
|
|
+ unsigned long long max = high_limit;
|
|
+
|
|
+ /* allow the kernel cmdline to have a say */
|
|
+ if (!numentries) {
|
|
+ /* round applicable memory size up to nearest megabyte */
|
|
+ numentries = nr_kernel_pages;
|
|
+
|
|
+ /* It isn't necessary when PAGE_SIZE >= 1MB */
|
|
+ if (PAGE_SIZE < SZ_1M)
|
|
+ numentries = round_up(numentries, SZ_1M / PAGE_SIZE);
|
|
+
|
|
+#if __BITS_PER_LONG > 32
|
|
+ if (!high_limit) {
|
|
+ unsigned long adapt;
|
|
+
|
|
+ for (adapt = ADAPT_SCALE_NPAGES; adapt < numentries;
|
|
+ adapt <<= ADAPT_SCALE_SHIFT)
|
|
+ scale++;
|
|
+ }
|
|
+#endif
|
|
+
|
|
+ /* limit to 1 bucket per 2^scale bytes of low memory */
|
|
+ if (scale > PAGE_SHIFT)
|
|
+ numentries >>= (scale - PAGE_SHIFT);
|
|
+ else
|
|
+ numentries <<= (PAGE_SHIFT - scale);
|
|
+
|
|
+ if (unlikely((numentries * bucketsize) < PAGE_SIZE))
|
|
+ numentries = PAGE_SIZE / bucketsize;
|
|
+ }
|
|
+ numentries = roundup_pow_of_two(numentries);
|
|
+
|
|
+ /* limit allocation size to 1/16 total memory by default */
|
|
+ if (max == 0) {
|
|
+ max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
|
|
+ max = div64_ul(max, bucketsize);
|
|
+ }
|
|
+ max = min(max, 0x80000000ULL);
|
|
+
|
|
+ if (numentries < low_limit)
|
|
+ numentries = low_limit;
|
|
+ if (numentries > max)
|
|
+ numentries = max;
|
|
+
|
|
+ return numentries;
|
|
+}
|
|
+
|
|
+#endif
|