arch, mm: set high_memory in free_area_init()
authorMike Rapoport (Microsoft) <rppt@kernel.org>
Thu, 13 Mar 2025 13:50:00 +0000 (15:50 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 18 Mar 2025 05:06:52 +0000 (22:06 -0700)
high_memory defines upper bound on the directly mapped memory.  This bound
is defined by the beginning of ZONE_HIGHMEM when a system has high memory
and by the end of memory otherwise.

All this is known to generic memory management initialization code that
can set high_memory while initializing core mm structures.

Add a generic calculation of high_memory to free_area_init() and remove
per-architecture calculation except for the architectures that set and use
high_memory earlier than that.

Link: https://lkml.kernel.org/r/20250313135003.836600-11-rppt@kernel.org
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com> [x86]
Tested-by: Mark Brown <broonie@kernel.org>
Cc: Alexander Gordeev <agordeev@linux.ibm.com>
Cc: Andreas Larsson <andreas@gaisler.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Borislav Betkov <bp@alien8.de>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Dinh Nguyen <dinguyen@kernel.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Gerald Schaefer <gerald.schaefer@linux.ibm.com>
Cc: Guo Ren (csky) <guoren@kernel.org>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Helge Deller <deller@gmx.de>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiaxun Yang <jiaxun.yang@flygoat.com>
Cc: Johannes Berg <johannes@sipsolutions.net>
Cc: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Matt Turner <mattst88@gmail.com>
Cc: Max Filippov <jcmvbkbc@gmail.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michal Simek <monstr@monstr.eu>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Richard Weinberger <richard@nod.at>
Cc: Russel King <linux@armlinux.org.uk>
Cc: Stafford Horne <shorne@gmail.com>
Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Cc: Thomas Gleinxer <tglx@linutronix.de>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Vineet Gupta <vgupta@kernel.org>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
25 files changed:
arch/alpha/mm/init.c
arch/arc/mm/init.c
arch/arm64/mm/init.c
arch/csky/mm/init.c
arch/hexagon/mm/init.c
arch/loongarch/kernel/numa.c
arch/loongarch/mm/init.c
arch/microblaze/mm/init.c
arch/mips/mm/init.c
arch/nios2/mm/init.c
arch/openrisc/mm/init.c
arch/parisc/mm/init.c
arch/riscv/mm/init.c
arch/s390/mm/init.c
arch/sh/mm/init.c
arch/sparc/mm/init_32.c
arch/sparc/mm/init_64.c
arch/um/kernel/um_arch.c
arch/x86/kernel/setup.c
arch/x86/mm/init_32.c
arch/x86/mm/numa_32.c
arch/xtensa/mm/init.c
mm/memory.c
mm/mm_init.c
mm/nommu.c

index ec0eeae9c653d85f9fb256a53e409f7c509e0f5a..3ab2d2f3c91790d9f4dd24efc2022c0432dc046f 100644 (file)
@@ -276,7 +276,6 @@ srm_paging_stop (void)
 void __init
 mem_init(void)
 {
-       high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
        memblock_free_all();
 }
 
index 7ef883d58dc17226965c13530e7cdec5bb27b30a..05025122e9652824a2a01b83253e05c164eabcb9 100644 (file)
@@ -150,8 +150,6 @@ void __init setup_arch_memory(void)
         */
        max_zone_pfn[ZONE_HIGHMEM] = max_high_pfn;
 
-       high_memory = (void *)(min_high_pfn << PAGE_SHIFT);
-
        arch_pfn_offset = min(min_low_pfn, min_high_pfn);
        kmap_init();
 #endif /* CONFIG_HIGHMEM */
index ccdef53872a0bf72f153ff2c9121fe393cd1b24a..53a0b105890bf430593f0992b8ca4cfb3a57351a 100644 (file)
@@ -309,8 +309,6 @@ void __init arm64_memblock_init(void)
        }
 
        early_init_fdt_scan_reserved_mem();
-
-       high_memory = __va(memblock_end_of_DRAM() - 1) + 1;
 }
 
 void __init bootmem_init(void)
index ba6694d6170a0dc2e12c19aca3f76e6106a8003b..a22801aa503a061894d60e9a6504aba0c1a81536 100644 (file)
@@ -47,7 +47,6 @@ void __init mem_init(void)
 #ifdef CONFIG_HIGHMEM
        unsigned long tmp;
 #endif
-       high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
 
        memblock_free_all();
 
index 508bb6a8dcc9057cb471834858e6d2ad95777d2f..d412c2314509b049af9412712498f6b0679ae1f7 100644 (file)
@@ -100,12 +100,6 @@ static void __init paging_init(void)
         * initial kernel segment table's physical address.
         */
        init_mm.context.ptbase = __pa(init_mm.pgd);
-
-       /*
-        * Start of high memory area.  Will probably need something more
-        * fancy if we...  get more fancy.
-        */
-       high_memory = (void *)((bootmem_lastpg + 1) << PAGE_SHIFT);
 }
 
 #ifndef DMA_RESERVE
index 84fe7f8548209eb7118d431b6d637cbcc25cf477..8eb489725b1a891b3d5dde5806fcf56d3ad58976 100644 (file)
@@ -389,7 +389,6 @@ void __init paging_init(void)
 
 void __init mem_init(void)
 {
-       high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
        memblock_free_all();
 }
 
index 00449df50db102d3d20318c9686288f0167fe32d..6affa3609188a1269942338e1a00ac64a1fb53ac 100644 (file)
@@ -78,8 +78,6 @@ void __init paging_init(void)
 
 void __init mem_init(void)
 {
-       high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
-
        memblock_free_all();
 }
 #endif /* !CONFIG_NUMA */
index 857cd2b44bcfedc742dfd110f4ac138b9d0c942f..7e2e342e84c52da4e3caff55bd3216ab0ff5a3ff 100644 (file)
@@ -120,8 +120,6 @@ void __init setup_memory(void)
 
 void __init mem_init(void)
 {
-       high_memory = (void *)__va(memory_start + lowmem_size - 1);
-
        /* this will put all memory onto the freelists */
        memblock_free_all();
 #ifdef CONFIG_HIGHMEM
index eb61a73520a0428b633a83477f2b6b466b50847c..ed9dde6a00f7fec4a5588444707c697b25878292 100644 (file)
@@ -417,7 +417,6 @@ void __init paging_init(void)
                max_zone_pfns[ZONE_HIGHMEM] = max_low_pfn;
        }
 #endif
-       high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
 
        free_area_init(max_zone_pfns);
 }
@@ -469,7 +468,6 @@ void __init mem_init(void)
 #else  /* CONFIG_NUMA */
 void __init mem_init(void)
 {
-       high_memory = (void *) __va(get_num_physpages() << PAGE_SHIFT);
        setup_zero_pages();     /* This comes from node 0 */
        memblock_free_all();
 }
index 3cafa87ead9e6cedace4cee4c83bfe6528e0e81d..4ba8dfa0d238ad4eff74b15c4c2b5dbaf58881d8 100644 (file)
@@ -62,12 +62,6 @@ void __init paging_init(void)
 
 void __init mem_init(void)
 {
-       unsigned long end_mem   = memory_end; /* this must not include
-                                               kernel stack at top */
-
-       end_mem &= PAGE_MASK;
-       high_memory = __va(end_mem);
-
        /* this will put all memory onto the freelists */
        memblock_free_all();
 }
index 9093c336e158a7304a00082ee4be7009d77bcc61..72c5952607ac9364eee8820ee156773cb5c98020 100644 (file)
@@ -193,8 +193,6 @@ void __init mem_init(void)
 {
        BUG_ON(!mem_map);
 
-       high_memory = (void *)__va(max_low_pfn * PAGE_SIZE);
-
        /* clear the zero-page */
        memset((void *)empty_zero_page, 0, PAGE_SIZE);
 
index 2cdfc0b1195cac182b34a78cdebd54275b7dc7d9..4fbe354dc9b432453877d765d9756ff20b535446 100644 (file)
@@ -562,7 +562,6 @@ void __init mem_init(void)
        BUILD_BUG_ON(TMPALIAS_MAP_START >= 0x80000000);
 #endif
 
-       high_memory = __va((max_pfn << PAGE_SHIFT));
        memblock_free_all();
 
 #ifdef CONFIG_PA11
index 157c9ca51541125dc1f291a7cde29d3b342ade69..ac6d41e86243f7d3c2653da4f6db86b2b201f238 100644 (file)
@@ -295,7 +295,6 @@ static void __init setup_bootmem(void)
        phys_ram_end = memblock_end_of_DRAM();
        min_low_pfn = PFN_UP(phys_ram_base);
        max_low_pfn = max_pfn = PFN_DOWN(phys_ram_end);
-       high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
 
        dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn));
 
index ad567e2100b74be368107dbc7fb0c0aa07a4526c..4bd6f316d71f7cb0a4949e624fe84c376151362c 100644 (file)
@@ -159,8 +159,6 @@ void __init mem_init(void)
        cpumask_set_cpu(0, &init_mm.context.cpu_attach_mask);
        cpumask_set_cpu(0, mm_cpumask(&init_mm));
 
-        high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
-
        pv_init();
        kfence_split_mapping();
 
index 72aea5cd1b856453b3c6bda0655e2621f4de5ce5..6d459ffba4bc29c2e1b722003e5938c852daea1e 100644 (file)
@@ -330,13 +330,6 @@ unsigned int mem_init_done = 0;
 
 void __init mem_init(void)
 {
-       pg_data_t *pgdat;
-
-       high_memory = NULL;
-       for_each_online_pgdat(pgdat)
-               high_memory = max_t(void *, high_memory,
-                                   __va(pgdat_end_pfn(pgdat) << PAGE_SHIFT));
-
        memblock_free_all();
 
        /* Set this up early, so we can take care of the zero page */
index 6b58da14edc6b7ae121839e57e9f3ec5761d25e9..81a468a9c223e0027c3e289555ddd6bd13a029a4 100644 (file)
@@ -275,7 +275,6 @@ void __init mem_init(void)
 
        taint_real_pages();
 
-       high_memory = __va(max_low_pfn << PAGE_SHIFT);
        memblock_free_all();
 
        for (i = 0; sp_banks[i].num_bytes != 0; i++) {
index 05882bca5b7326b94ae1c38113eef1fa710a5daf..34d46adb9571cf12005ad220448b9bc140817c3e 100644 (file)
@@ -2505,8 +2505,6 @@ static void __init register_page_bootmem_info(void)
 }
 void __init mem_init(void)
 {
-       high_memory = __va(last_valid_pfn << PAGE_SHIFT);
-
        memblock_free_all();
 
        /*
index 6414cbf005727b004e30b4b7be1f8b365f157f8f..f24a3ce37ab7f67d0c47a1ebd2f901154e5e8709 100644 (file)
@@ -385,7 +385,6 @@ int __init linux_main(int argc, char **argv, char **envp)
 
        high_physmem = uml_physmem + physmem_size;
        end_iomem = high_physmem + iomem_size;
-       high_memory = (void *) end_iomem;
 
        start_vm = VMALLOC_START;
 
index ff8604007b087b6fe077f18cd0d843b3b0a13e9f..74ac686d441a73e674b046f7c19e563ffb259c37 100644 (file)
@@ -972,8 +972,6 @@ void __init setup_arch(char **cmdline_p)
                max_low_pfn = e820__end_of_low_ram_pfn();
        else
                max_low_pfn = max_pfn;
-
-       high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
 #endif
 
        /* Find and reserve MPTABLE area */
index 6d2f8cb9451edb538330abb0bbe207a41dcf09ae..801b659ead0c08a20688b38c598bac54d31e52d0 100644 (file)
@@ -643,9 +643,6 @@ void __init initmem_init(void)
                highstart_pfn = max_low_pfn;
        printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
                pages_to_mb(highend_pfn - highstart_pfn));
-       high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
-#else
-       high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
 #endif
 
        memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
index 65fda406e6f265a1072d675bf70533439015720f..442ef3facff013da7704e1782c0ac74eb3522dbf 100644 (file)
@@ -41,9 +41,6 @@ void __init initmem_init(void)
                highstart_pfn = max_low_pfn;
        printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
               pages_to_mb(highend_pfn - highstart_pfn));
-       high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
-#else
-       high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
 #endif
        printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
                        pages_to_mb(max_low_pfn));
index 9f1b0d5fccc776f204dfd6e2bfabd598501c88fa..9b662477b3d48e472554f234abb9c0c9beb62930 100644 (file)
@@ -164,8 +164,6 @@ void __init mem_init(void)
 {
        free_highpages();
 
-       high_memory = (void *)__va(max_low_pfn << PAGE_SHIFT);
-
        memblock_free_all();
 }
 
index a1d7664855f2518df53737c62f4e6b7faec05a9a..3900225d99c5f1fdf0e758ea432bb03c16198f56 100644 (file)
@@ -113,14 +113,6 @@ static __always_inline bool vmf_orig_pte_uffd_wp(struct vm_fault *vmf)
        return pte_marker_uffd_wp(vmf->orig_pte);
 }
 
-/*
- * A number of key systems in x86 including ioremap() rely on the assumption
- * that high_memory defines the upper bound on direct map memory, then end
- * of ZONE_NORMAL.
- */
-void *high_memory;
-EXPORT_SYMBOL(high_memory);
-
 /*
  * Randomize the address space (stacks, mmaps, brk, etc.).
  *
index 7fd48d2d5064ca23c0f9877ab2691e8543a65b2e..bd7071c32a44a002000b9a2cfefa402b83847789 100644 (file)
@@ -45,6 +45,13 @@ struct page *mem_map;
 EXPORT_SYMBOL(mem_map);
 #endif
 
+/*
+ * high_memory defines the upper bound on direct map memory, then end
+ * of ZONE_NORMAL.
+ */
+void *high_memory;
+EXPORT_SYMBOL(high_memory);
+
 #ifdef CONFIG_DEBUG_MEMORY_INIT
 int __meminitdata mminit_loglevel;
 
@@ -1778,6 +1785,27 @@ static bool arch_has_descending_max_zone_pfns(void)
        return IS_ENABLED(CONFIG_ARC) && !IS_ENABLED(CONFIG_ARC_HAS_PAE40);
 }
 
+static void set_high_memory(void)
+{
+       phys_addr_t highmem = memblock_end_of_DRAM();
+
+       /*
+        * Some architectures (e.g. ARM) set high_memory very early and
+        * use it in arch setup code.
+        * If an architecture already set high_memory don't overwrite it
+        */
+       if (high_memory)
+               return;
+
+#ifdef CONFIG_HIGHMEM
+       if (arch_has_descending_max_zone_pfns() ||
+           highmem > PFN_PHYS(arch_zone_lowest_possible_pfn[ZONE_HIGHMEM]))
+               highmem = PFN_PHYS(arch_zone_lowest_possible_pfn[ZONE_HIGHMEM]);
+#endif
+
+       high_memory = phys_to_virt(highmem - 1) + 1;
+}
+
 /**
  * free_area_init - Initialise all pg_data_t and zone data
  * @max_zone_pfn: an array of max PFNs for each zone
@@ -1900,6 +1928,8 @@ void __init free_area_init(unsigned long *max_zone_pfn)
 
        /* disable hash distribution for systems with a single node */
        fixup_hashdist();
+
+       set_high_memory();
 }
 
 /**
index 43751726f977d7c2f01f9b0e703232a600b0a65f..15a396ce2553daf2d74babc02ff027e57a54c34b 100644 (file)
@@ -42,8 +42,6 @@
 #include <asm/mmu_context.h>
 #include "internal.h"
 
-void *high_memory;
-EXPORT_SYMBOL(high_memory);
 unsigned long highest_memmap_pfn;
 int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
 int heap_stack_gap = 0;