arch, mm: set max_mapnr when allocating memory map for FLATMEM
authorMike Rapoport (Microsoft) <rppt@kernel.org>
Thu, 13 Mar 2025 13:49:59 +0000 (15:49 +0200)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 18 Mar 2025 05:06:52 +0000 (22:06 -0700)
max_mapnr is essentially the size of the memory map for systems that use
FLATMEM. There is no reason to calculate it in each and every architecture
when it's anyway calculated in alloc_node_mem_map().

Drop setting of max_mapnr from architecture code and set it once in
alloc_node_mem_map().

While on it, move definition of mem_map and max_mapnr to mm/mm_init.c so
there won't be two copies for MMU and !MMU variants.

Link: https://lkml.kernel.org/r/20250313135003.836600-10-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>
26 files changed:
arch/alpha/mm/init.c
arch/arc/mm/init.c
arch/arm/mm/init.c
arch/csky/mm/init.c
arch/loongarch/mm/init.c
arch/microblaze/mm/init.c
arch/mips/mm/init.c
arch/nios2/kernel/setup.c
arch/nios2/mm/init.c
arch/openrisc/mm/init.c
arch/parisc/mm/init.c
arch/powerpc/kernel/setup-common.c
arch/riscv/mm/init.c
arch/s390/mm/init.c
arch/sh/mm/init.c
arch/sparc/mm/init_32.c
arch/um/include/shared/mem_user.h
arch/um/kernel/physmem.c
arch/um/kernel/um_arch.c
arch/x86/mm/init_32.c
arch/xtensa/mm/init.c
include/asm-generic/memory_model.h
include/linux/mm.h
mm/memory.c
mm/mm_init.c
mm/nommu.c

index 61c2198..ec0eeae 100644 (file)
@@ -276,7 +276,6 @@ srm_paging_stop (void)
 void __init
 mem_init(void)
 {
-       set_max_mapnr(max_low_pfn);
        high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
        memblock_free_all();
 }
index 6a71b23..7ef883d 100644 (file)
@@ -154,11 +154,6 @@ void __init setup_arch_memory(void)
 
        arch_pfn_offset = min(min_low_pfn, min_high_pfn);
        kmap_init();
-
-#else /* CONFIG_HIGHMEM */
-       /* pfn_valid() uses this when FLATMEM=y and HIGHMEM=n */
-       max_mapnr = max_low_pfn - min_low_pfn;
-
 #endif /* CONFIG_HIGHMEM */
 
        free_area_init(max_zone_pfn);
index 9aec1cb..d4bcc74 100644 (file)
@@ -275,8 +275,6 @@ void __init mem_init(void)
        swiotlb_init(max_pfn > arm_dma_pfn_limit, SWIOTLB_VERBOSE);
 #endif
 
-       set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
-
 #ifdef CONFIG_SA1111
        /* now that our DMA memory is actually so designated, we can free it */
        memblock_phys_free(PHYS_OFFSET, __pa(swapper_pg_dir) - PHYS_OFFSET);
index ab51acb..ba6694d 100644 (file)
@@ -46,10 +46,6 @@ void __init mem_init(void)
 {
 #ifdef CONFIG_HIGHMEM
        unsigned long tmp;
-
-       set_max_mapnr(highend_pfn - ARCH_PFN_OFFSET);
-#else
-       set_max_mapnr(max_low_pfn - ARCH_PFN_OFFSET);
 #endif
        high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
 
index ca5aa5f..00449df 100644 (file)
@@ -78,7 +78,6 @@ void __init paging_init(void)
 
 void __init mem_init(void)
 {
-       max_mapnr = max_low_pfn;
        high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
 
        memblock_free_all();
index 4520c57..857cd2b 100644 (file)
@@ -104,17 +104,13 @@ void __init setup_memory(void)
         *
         * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
         * max_low_pfn
-        * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn)
         */
 
        /* memory start is from the kernel end (aligned) to higher addr */
        min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */
-       /* RAM is assumed contiguous */
-       max_mapnr = memory_size >> PAGE_SHIFT;
        max_low_pfn = ((u64)memory_start + (u64)lowmem_size) >> PAGE_SHIFT;
        max_pfn = ((u64)memory_start + (u64)memory_size) >> PAGE_SHIFT;
 
-       pr_info("%s: max_mapnr: %#lx\n", __func__, max_mapnr);
        pr_info("%s: min_low_pfn: %#lx\n", __func__, min_low_pfn);
        pr_info("%s: max_low_pfn: %#lx\n", __func__, max_low_pfn);
        pr_info("%s: max_pfn: %#lx\n", __func__, max_pfn);
index 820e35a..eb61a73 100644 (file)
@@ -415,15 +415,7 @@ void __init paging_init(void)
                       " %ldk highmem ignored\n",
                       (highend_pfn - max_low_pfn) << (PAGE_SHIFT - 10));
                max_zone_pfns[ZONE_HIGHMEM] = max_low_pfn;
-
-               max_mapnr = max_low_pfn;
-       } else if (highend_pfn) {
-               max_mapnr = highend_pfn;
-       } else {
-               max_mapnr = max_low_pfn;
        }
-#else
-       max_mapnr = max_low_pfn;
 #endif
        high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
 
index a4cffbf..2a40150 100644 (file)
@@ -158,7 +158,6 @@ void __init setup_arch(char **cmdline_p)
        *cmdline_p = boot_command_line;
 
        find_limits(&min_low_pfn, &max_low_pfn, &max_pfn);
-       max_mapnr = max_low_pfn;
 
        memblock_reserve(__pa_symbol(_stext), _end - _stext);
 #ifdef CONFIG_BLK_DEV_INITRD
index aa692ad..3cafa87 100644 (file)
@@ -51,7 +51,7 @@ void __init paging_init(void)
        pagetable_init();
        pgd_current = swapper_pg_dir;
 
-       max_zone_pfn[ZONE_NORMAL] = max_mapnr;
+       max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
 
        /* pass the memory from the bootmem allocator to the main allocator */
        free_area_init(max_zone_pfn);
index d0cb1a0..9093c33 100644 (file)
@@ -193,7 +193,6 @@ void __init mem_init(void)
 {
        BUG_ON(!mem_map);
 
-       max_mapnr = max_low_pfn;
        high_memory = (void *)__va(max_low_pfn * PAGE_SIZE);
 
        /* clear the zero-page */
index 61c0a24..2cdfc0b 100644 (file)
@@ -563,7 +563,6 @@ void __init mem_init(void)
 #endif
 
        high_memory = __va((max_pfn << PAGE_SHIFT));
-       set_max_mapnr(max_low_pfn);
        memblock_free_all();
 
 #ifdef CONFIG_PA11
index a08b0ed..68d47c5 100644 (file)
@@ -957,8 +957,6 @@ void __init setup_arch(char **cmdline_p)
 
        /* Parse memory topology */
        mem_topology_setup();
-       /* Set max_mapnr before paging_init() */
-       set_max_mapnr(max_pfn);
        high_memory = (void *)__va(max_low_pfn * PAGE_SIZE);
 
        /*
index 15b2eda..157c9ca 100644 (file)
@@ -298,7 +298,6 @@ static void __init setup_bootmem(void)
        high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
 
        dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn));
-       set_max_mapnr(max_low_pfn - ARCH_PFN_OFFSET);
 
        reserve_initrd_mem();
 
index 2b41dc9..ad567e2 100644 (file)
@@ -159,7 +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));
 
-       set_max_mapnr(max_low_pfn);
         high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
 
        pv_init();
index 289a2fe..72aea5c 100644 (file)
@@ -290,7 +290,6 @@ void __init paging_init(void)
         */
        max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
        min_low_pfn = __MEMORY_START >> PAGE_SHIFT;
-       set_max_mapnr(max_low_pfn - min_low_pfn);
 
        nodes_clear(node_online_map);
 
index d96a14f..6b58da1 100644 (file)
@@ -275,7 +275,6 @@ void __init mem_init(void)
 
        taint_real_pages();
 
-       max_mapnr = last_valid_pfn - pfn_base;
        high_memory = __va(max_low_pfn << PAGE_SHIFT);
        memblock_free_all();
 
index adfa080..d4727ef 100644 (file)
@@ -47,7 +47,6 @@ extern int iomem_size;
 #define ROUND_4M(n) ((((unsigned long) (n)) + (1 << 22)) & ~((1 << 22) - 1))
 
 extern unsigned long find_iomem(char *driver, unsigned long *len_out);
-extern void mem_total_pages(unsigned long physmem, unsigned long iomem);
 extern void setup_physmem(unsigned long start, unsigned long usable,
                          unsigned long len);
 extern void map_memory(unsigned long virt, unsigned long phys,
index a74f17b..af02b5f 100644 (file)
@@ -22,18 +22,6 @@ static int physmem_fd = -1;
 unsigned long high_physmem;
 EXPORT_SYMBOL(high_physmem);
 
-void __init mem_total_pages(unsigned long physmem, unsigned long iomem)
-{
-       unsigned long phys_pages, iomem_pages, total_pages;
-
-       phys_pages  = physmem >> PAGE_SHIFT;
-       iomem_pages = iomem   >> PAGE_SHIFT;
-
-       total_pages = phys_pages + iomem_pages;
-
-       max_mapnr = total_pages;
-}
-
 void map_memory(unsigned long virt, unsigned long phys, unsigned long len,
                int r, int w, int x)
 {
index 79ea97d..6414cbf 100644 (file)
@@ -419,7 +419,6 @@ void __init setup_arch(char **cmdline_p)
 
        stack_protections((unsigned long) init_task.stack);
        setup_physmem(uml_physmem, uml_reserved, physmem_size);
-       mem_total_pages(physmem_size, iomem_size);
        uml_dtb_init();
        read_initrd();
 
index ac41b1e..6d2f8cb 100644 (file)
@@ -650,9 +650,6 @@ void __init initmem_init(void)
 
        memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
 
-#ifdef CONFIG_FLATMEM
-       max_mapnr = IS_ENABLED(CONFIG_HIGHMEM) ? highend_pfn : max_low_pfn;
-#endif
        __vmalloc_start_set = true;
 
        printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
index 01577d3..9f1b0d5 100644 (file)
@@ -164,7 +164,6 @@ void __init mem_init(void)
 {
        free_highpages();
 
-       max_mapnr = max_pfn - ARCH_PFN_OFFSET;
        high_memory = (void *)__va(max_low_pfn << PAGE_SHIFT);
 
        memblock_free_all();
index 6d1fb61..a3b5029 100644 (file)
 #define __page_to_pfn(page)    ((unsigned long)((page) - mem_map) + \
                                 ARCH_PFN_OFFSET)
 
+/* avoid <linux/mm.h> include hell */
+extern unsigned long max_mapnr;
+
 #ifndef pfn_valid
 static inline int pfn_valid(unsigned long pfn)
 {
-       /* avoid <linux/mm.h> include hell */
-       extern unsigned long max_mapnr;
        unsigned long pfn_offset = ARCH_PFN_OFFSET;
 
        return pfn >= pfn_offset && (pfn - pfn_offset) < max_mapnr;
index 82776b4..8c4cb8c 100644 (file)
@@ -46,17 +46,6 @@ extern int sysctl_page_lock_unfairness;
 void mm_core_init(void);
 void init_mm_internals(void);
 
-#ifndef CONFIG_NUMA            /* Don't use mapnrs, do it properly */
-extern unsigned long max_mapnr;
-
-static inline void set_max_mapnr(unsigned long limit)
-{
-       max_mapnr = limit;
-}
-#else
-static inline void set_max_mapnr(unsigned long limit) { }
-#endif
-
 extern atomic_long_t _totalram_pages;
 static inline unsigned long totalram_pages(void)
 {
index 8873b7a..a1d7664 100644 (file)
 #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_cpupid.
 #endif
 
-#ifndef CONFIG_NUMA
-unsigned long max_mapnr;
-EXPORT_SYMBOL(max_mapnr);
-
-struct page *mem_map;
-EXPORT_SYMBOL(mem_map);
-#endif
-
 static vm_fault_t do_fault(struct vm_fault *vmf);
 static vm_fault_t do_anonymous_page(struct vm_fault *vmf);
 static bool vmf_pte_changed(struct vm_fault *vmf);
index 133640a..7fd48d2 100644 (file)
 
 #include <asm/setup.h>
 
+#ifndef CONFIG_NUMA
+unsigned long max_mapnr;
+EXPORT_SYMBOL(max_mapnr);
+
+struct page *mem_map;
+EXPORT_SYMBOL(mem_map);
+#endif
+
 #ifdef CONFIG_DEBUG_MEMORY_INIT
 int __meminitdata mminit_loglevel;
 
@@ -1639,7 +1647,7 @@ static void __init alloc_node_mem_map(struct pglist_data *pgdat)
        start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
        offset = pgdat->node_start_pfn - start;
        /*
-                * The zone's endpoints aren't required to be MAX_PAGE_ORDER
+        * The zone's endpoints aren't required to be MAX_PAGE_ORDER
         * aligned but the node_mem_map endpoints must be in order
         * for the buddy allocator to function correctly.
         */
@@ -1655,14 +1663,15 @@ static void __init alloc_node_mem_map(struct pglist_data *pgdat)
        pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n",
                 __func__, pgdat->node_id, (unsigned long)pgdat,
                 (unsigned long)pgdat->node_mem_map);
-#ifndef CONFIG_NUMA
+
        /* the global mem_map is just set as node 0's */
-       if (pgdat == NODE_DATA(0)) {
-               mem_map = NODE_DATA(0)->node_mem_map;
-               if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
-                       mem_map -= offset;
-       }
-#endif
+       WARN_ON(pgdat != NODE_DATA(0));
+
+       mem_map = pgdat->node_mem_map;
+       if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
+               mem_map -= offset;
+
+       max_mapnr = end - start;
 }
 #else
 static inline void alloc_node_mem_map(struct pglist_data *pgdat) { }
index 8b31d83..4375172 100644 (file)
 
 void *high_memory;
 EXPORT_SYMBOL(high_memory);
-struct page *mem_map;
-unsigned long max_mapnr;
-EXPORT_SYMBOL(max_mapnr);
 unsigned long highest_memmap_pfn;
 int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
 int heap_stack_gap = 0;
 
 atomic_long_t mmap_pages_allocated;
 
-EXPORT_SYMBOL(mem_map);
 
 /* list of mapped, potentially shareable regions */
 static struct kmem_cache *vm_region_jar;