2019-05-29 07:18:00 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-07-10 18:00:26 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Regents of the University of California
|
2019-06-28 13:36:21 -07:00
|
|
|
* Copyright (C) 2019 Western Digital Corporation or its affiliates.
|
2021-04-19 03:55:38 +03:00
|
|
|
* Copyright (C) 2020 FORTH-ICS/CARV
|
|
|
|
* Nick Kossifidis <mick@ics.forth.gr>
|
2017-07-10 18:00:26 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/memblock.h>
|
2018-10-30 15:09:49 -07:00
|
|
|
#include <linux/initrd.h>
|
2017-07-10 18:00:26 -07:00
|
|
|
#include <linux/swap.h>
|
2021-06-02 16:55:16 +08:00
|
|
|
#include <linux/swiotlb.h>
|
2018-01-16 09:37:50 +01:00
|
|
|
#include <linux/sizes.h>
|
2019-02-21 11:25:49 +05:30
|
|
|
#include <linux/of_fdt.h>
|
2021-04-19 03:55:39 +03:00
|
|
|
#include <linux/of_reserved_mem.h>
|
riscv: Fix memblock reservation for device tree blob
This fixes an error with how the FDT blob is reserved in memblock.
An incorrect physical address calculation exposed the FDT header to
unintended corruption, which typically manifested with of_fdt_raw_init()
faulting during late boot after fdt_totalsize() returned a wrong value.
Systems with smaller physical memory sizes more frequently trigger this
issue, as the kernel is more likely to allocate from the DMA32 zone
where bbl places the DTB after the kernel image.
Commit 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages")
changed the mapping of the DTB to reside in the fixmap area.
Consequently, early_init_fdt_reserve_self() cannot be used anymore in
setup_bootmem() since it relies on __pa() to derive a physical address,
which does not work with dtb_early_va that is no longer a valid kernel
logical address.
The reserved[0x1] region shows the effect of the pointer underflow
resulting from the __pa(initial_boot_params) offset subtraction:
[ 0.000000] MEMBLOCK configuration:
[ 0.000000] memory size = 0x000000001fe00000 reserved size = 0x0000000000a2e514
[ 0.000000] memory.cnt = 0x1
[ 0.000000] memory[0x0] [0x0000000080200000-0x000000009fffffff], 0x000000001fe00000 bytes flags: 0x0
[ 0.000000] reserved.cnt = 0x2
[ 0.000000] reserved[0x0] [0x0000000080200000-0x0000000080c2dfeb], 0x0000000000a2dfec bytes flags: 0x0
[ 0.000000] reserved[0x1] [0xfffffff080100000-0xfffffff080100527], 0x0000000000000528 bytes flags: 0x0
With the fix applied:
[ 0.000000] MEMBLOCK configuration:
[ 0.000000] memory size = 0x000000001fe00000 reserved size = 0x0000000000a2e514
[ 0.000000] memory.cnt = 0x1
[ 0.000000] memory[0x0] [0x0000000080200000-0x000000009fffffff], 0x000000001fe00000 bytes flags: 0x0
[ 0.000000] reserved.cnt = 0x2
[ 0.000000] reserved[0x0] [0x0000000080200000-0x0000000080c2dfeb], 0x0000000000a2dfec bytes flags: 0x0
[ 0.000000] reserved[0x1] [0x0000000080e00000-0x0000000080e00527], 0x0000000000000528 bytes flags: 0x0
Fixes: 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages")
Signed-off-by: Albert Ou <aou@eecs.berkeley.edu>
Tested-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Paul Walmsley <paul.walmsley@sifive.com>
2019-09-27 16:14:18 -07:00
|
|
|
#include <linux/libfdt.h>
|
2020-03-10 00:55:41 +08:00
|
|
|
#include <linux/set_memory.h>
|
2020-10-31 14:01:12 +08:00
|
|
|
#include <linux/dma-map-ops.h>
|
2021-04-19 03:55:38 +03:00
|
|
|
#include <linux/crash_dump.h>
|
2021-07-30 20:48:41 +08:00
|
|
|
#include <linux/hugetlb.h>
|
2023-06-06 15:04:44 +02:00
|
|
|
#include <linux/kfence.h>
|
2024-05-05 19:06:24 +03:00
|
|
|
#include <linux/execmem.h>
|
2017-07-10 18:00:26 -07:00
|
|
|
|
2019-01-07 20:57:01 +05:30
|
|
|
#include <asm/fixmap.h>
|
2017-07-10 18:00:26 -07:00
|
|
|
#include <asm/io.h>
|
2024-06-05 13:40:45 +02:00
|
|
|
#include <asm/kasan.h>
|
2024-10-26 10:13:57 -07:00
|
|
|
#include <asm/module.h>
|
2020-11-18 16:38:29 -08:00
|
|
|
#include <asm/numa.h>
|
2023-08-08 09:35:00 -07:00
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/sections.h>
|
|
|
|
#include <asm/soc.h>
|
2024-12-09 20:26:17 +08:00
|
|
|
#include <asm/sparsemem.h>
|
2023-08-08 09:35:00 -07:00
|
|
|
#include <asm/tlbflush.h>
|
2017-07-10 18:00:26 -07:00
|
|
|
|
2019-10-17 15:00:17 -07:00
|
|
|
#include "../kernel/head.h"
|
|
|
|
|
riscv: Stop emitting preventive sfence.vma for new vmalloc mappings
In 6.5, we removed the vmalloc fault path because that can't work (see
[1] [2]). Then in order to make sure that new page table entries were
seen by the page table walker, we had to preventively emit a sfence.vma
on all harts [3] but this solution is very costly since it relies on IPI.
And even there, we could end up in a loop of vmalloc faults if a vmalloc
allocation is done in the IPI path (for example if it is traced, see
[4]), which could result in a kernel stack overflow.
Those preventive sfence.vma needed to be emitted because:
- if the uarch caches invalid entries, the new mapping may not be
observed by the page table walker and an invalidation may be needed.
- if the uarch does not cache invalid entries, a reordered access
could "miss" the new mapping and traps: in that case, we would actually
only need to retry the access, no sfence.vma is required.
So this patch removes those preventive sfence.vma and actually handles
the possible (and unlikely) exceptions. And since the kernel stacks
mappings lie in the vmalloc area, this handling must be done very early
when the trap is taken, at the very beginning of handle_exception: this
also rules out the vmalloc allocations in the fault path.
Link: https://lore.kernel.org/linux-riscv/20230531093817.665799-1-bjorn@kernel.org/ [1]
Link: https://lore.kernel.org/linux-riscv/20230801090927.2018653-1-dylan@andestech.com [2]
Link: https://lore.kernel.org/linux-riscv/20230725132246.817726-1-alexghiti@rivosinc.com/ [3]
Link: https://lore.kernel.org/lkml/20200508144043.13893-1-joro@8bytes.org/ [4]
Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Reviewed-by: Yunhui Cui <cuiyunhui@bytedance.com>
Link: https://lore.kernel.org/r/20240717060125.139416-4-alexghiti@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2024-07-17 08:01:24 +02:00
|
|
|
u64 new_vmalloc[NR_CPUS / sizeof(u64) + 1];
|
|
|
|
|
2021-06-17 15:53:07 +02:00
|
|
|
struct kernel_mapping kernel_map __ro_after_init;
|
|
|
|
EXPORT_SYMBOL(kernel_map);
|
|
|
|
#ifdef CONFIG_XIP_KERNEL
|
|
|
|
#define kernel_map (*(struct kernel_mapping *)XIP_FIXUP(&kernel_map))
|
|
|
|
#endif
|
|
|
|
|
2021-12-06 11:46:51 +01:00
|
|
|
#ifdef CONFIG_64BIT
|
2022-02-22 09:40:52 -08:00
|
|
|
u64 satp_mode __ro_after_init = !IS_ENABLED(CONFIG_XIP_KERNEL) ? SATP_MODE_57 : SATP_MODE_39;
|
2021-12-06 11:46:51 +01:00
|
|
|
#else
|
2022-01-26 00:00:12 +08:00
|
|
|
u64 satp_mode __ro_after_init = SATP_MODE_32;
|
2021-12-06 11:46:51 +01:00
|
|
|
#endif
|
|
|
|
EXPORT_SYMBOL(satp_mode);
|
|
|
|
|
2023-08-29 21:39:19 -07:00
|
|
|
#ifdef CONFIG_64BIT
|
2024-03-20 14:47:12 +08:00
|
|
|
bool pgtable_l4_enabled __ro_after_init = !IS_ENABLED(CONFIG_XIP_KERNEL);
|
|
|
|
bool pgtable_l5_enabled __ro_after_init = !IS_ENABLED(CONFIG_XIP_KERNEL);
|
2021-12-06 11:46:51 +01:00
|
|
|
EXPORT_SYMBOL(pgtable_l4_enabled);
|
2022-01-27 10:48:41 +08:00
|
|
|
EXPORT_SYMBOL(pgtable_l5_enabled);
|
2023-08-29 21:39:19 -07:00
|
|
|
#endif
|
2021-12-06 11:46:51 +01:00
|
|
|
|
2021-07-21 09:59:35 +02:00
|
|
|
phys_addr_t phys_ram_base __ro_after_init;
|
|
|
|
EXPORT_SYMBOL(phys_ram_base);
|
|
|
|
|
2024-12-09 20:26:17 +08:00
|
|
|
#ifdef CONFIG_SPARSEMEM_VMEMMAP
|
|
|
|
#define VMEMMAP_ADDR_ALIGN (1ULL << SECTION_SIZE_BITS)
|
|
|
|
|
|
|
|
unsigned long vmemmap_start_pfn __ro_after_init;
|
|
|
|
EXPORT_SYMBOL(vmemmap_start_pfn);
|
|
|
|
#endif
|
|
|
|
|
2019-03-26 08:03:47 +00:00
|
|
|
unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
|
|
|
|
__page_aligned_bss;
|
|
|
|
EXPORT_SYMBOL(empty_zero_page);
|
|
|
|
|
2019-06-07 06:01:29 +00:00
|
|
|
extern char _start[];
|
2021-04-13 02:35:14 -04:00
|
|
|
void *_dtb_early_va __initdata;
|
|
|
|
uintptr_t _dtb_early_pa __initdata;
|
2019-06-07 06:01:29 +00:00
|
|
|
|
2023-09-14 11:31:41 +08:00
|
|
|
phys_addr_t dma32_phys_limit __initdata;
|
2020-10-31 14:01:12 +08:00
|
|
|
|
2017-07-10 18:00:26 -07:00
|
|
|
static void __init zone_sizes_init(void)
|
|
|
|
{
|
2018-01-16 09:37:50 +01:00
|
|
|
unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
|
2017-07-10 18:00:26 -07:00
|
|
|
|
2018-06-25 16:49:37 +08:00
|
|
|
#ifdef CONFIG_ZONE_DMA32
|
2020-10-31 14:01:12 +08:00
|
|
|
max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
|
2018-06-25 16:49:37 +08:00
|
|
|
#endif
|
2018-01-16 09:37:50 +01:00
|
|
|
max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
|
|
|
|
|
2020-06-03 15:57:10 -07:00
|
|
|
free_area_init(max_zone_pfns);
|
2017-07-10 18:00:26 -07:00
|
|
|
}
|
|
|
|
|
2020-05-14 19:53:35 +08:00
|
|
|
#if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
|
2021-12-06 11:46:54 +01:00
|
|
|
|
|
|
|
#define LOG2_SZ_1K ilog2(SZ_1K)
|
|
|
|
#define LOG2_SZ_1M ilog2(SZ_1M)
|
|
|
|
#define LOG2_SZ_1G ilog2(SZ_1G)
|
|
|
|
#define LOG2_SZ_1T ilog2(SZ_1T)
|
|
|
|
|
2019-11-18 05:58:34 +00:00
|
|
|
static inline void print_mlk(char *name, unsigned long b, unsigned long t)
|
|
|
|
{
|
|
|
|
pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t,
|
2021-12-06 11:46:54 +01:00
|
|
|
(((t) - (b)) >> LOG2_SZ_1K));
|
2019-11-18 05:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void print_mlm(char *name, unsigned long b, unsigned long t)
|
|
|
|
{
|
|
|
|
pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t,
|
2021-12-06 11:46:54 +01:00
|
|
|
(((t) - (b)) >> LOG2_SZ_1M));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void print_mlg(char *name, unsigned long b, unsigned long t)
|
|
|
|
{
|
|
|
|
pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld GB)\n", name, b, t,
|
|
|
|
(((t) - (b)) >> LOG2_SZ_1G));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_64BIT
|
|
|
|
static inline void print_mlt(char *name, unsigned long b, unsigned long t)
|
|
|
|
{
|
|
|
|
pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld TB)\n", name, b, t,
|
|
|
|
(((t) - (b)) >> LOG2_SZ_1T));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define print_mlt(n, b, t) do {} while (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline void print_ml(char *name, unsigned long b, unsigned long t)
|
|
|
|
{
|
|
|
|
unsigned long diff = t - b;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_64BIT) && (diff >> LOG2_SZ_1T) >= 10)
|
|
|
|
print_mlt(name, b, t);
|
|
|
|
else if ((diff >> LOG2_SZ_1G) >= 10)
|
|
|
|
print_mlg(name, b, t);
|
|
|
|
else if ((diff >> LOG2_SZ_1M) >= 10)
|
|
|
|
print_mlm(name, b, t);
|
|
|
|
else
|
|
|
|
print_mlk(name, b, t);
|
2019-11-18 05:58:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 02:22:21 +08:00
|
|
|
static void __init print_vm_layout(void)
|
2019-11-18 05:58:34 +00:00
|
|
|
{
|
|
|
|
pr_notice("Virtual kernel memory layout:\n");
|
2021-12-06 11:46:54 +01:00
|
|
|
print_ml("fixmap", (unsigned long)FIXADDR_START,
|
|
|
|
(unsigned long)FIXADDR_TOP);
|
|
|
|
print_ml("pci io", (unsigned long)PCI_IO_START,
|
|
|
|
(unsigned long)PCI_IO_END);
|
|
|
|
print_ml("vmemmap", (unsigned long)VMEMMAP_START,
|
|
|
|
(unsigned long)VMEMMAP_END);
|
|
|
|
print_ml("vmalloc", (unsigned long)VMALLOC_START,
|
|
|
|
(unsigned long)VMALLOC_END);
|
2022-08-11 15:41:48 +08:00
|
|
|
#ifdef CONFIG_64BIT
|
|
|
|
print_ml("modules", (unsigned long)MODULES_VADDR,
|
|
|
|
(unsigned long)MODULES_END);
|
|
|
|
#endif
|
2021-12-06 11:46:54 +01:00
|
|
|
print_ml("lowmem", (unsigned long)PAGE_OFFSET,
|
|
|
|
(unsigned long)high_memory);
|
2022-01-19 19:23:41 -08:00
|
|
|
if (IS_ENABLED(CONFIG_64BIT)) {
|
2021-12-06 11:46:45 +01:00
|
|
|
#ifdef CONFIG_KASAN
|
2021-12-06 11:46:54 +01:00
|
|
|
print_ml("kasan", KASAN_SHADOW_START, KASAN_SHADOW_END);
|
2021-04-11 12:41:44 -04:00
|
|
|
#endif
|
2022-01-19 19:23:41 -08:00
|
|
|
|
2023-03-29 06:53:26 +02:00
|
|
|
print_ml("kernel", (unsigned long)kernel_map.virt_addr,
|
2021-12-06 11:46:54 +01:00
|
|
|
(unsigned long)ADDRESS_SPACE_END);
|
2022-01-19 19:23:41 -08:00
|
|
|
}
|
2019-11-18 05:58:34 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void print_vm_layout(void) { }
|
|
|
|
#endif /* CONFIG_DEBUG_VM */
|
|
|
|
|
2025-03-13 15:50:02 +02:00
|
|
|
void __init arch_mm_preinit(void)
|
2017-07-10 18:00:26 -07:00
|
|
|
{
|
2024-03-25 19:00:36 +08:00
|
|
|
bool swiotlb = max_pfn > PFN_DOWN(dma32_phys_limit);
|
2017-07-10 18:00:26 -07:00
|
|
|
#ifdef CONFIG_FLATMEM
|
|
|
|
BUG_ON(!mem_map);
|
|
|
|
#endif /* CONFIG_FLATMEM */
|
|
|
|
|
2024-03-25 19:00:36 +08:00
|
|
|
if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb &&
|
|
|
|
dma_cache_alignment != 1) {
|
|
|
|
/*
|
|
|
|
* If no bouncing needed for ZONE_DMA, allocate 1MB swiotlb
|
|
|
|
* buffer per 1GB of RAM for kmalloc() bouncing on
|
|
|
|
* non-coherent platforms.
|
|
|
|
*/
|
|
|
|
unsigned long size =
|
|
|
|
DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
|
|
|
|
swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
|
|
|
|
swiotlb = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
|
2017-07-10 18:00:26 -07:00
|
|
|
|
2019-11-18 05:58:34 +00:00
|
|
|
print_vm_layout();
|
2017-07-10 18:00:26 -07:00
|
|
|
}
|
|
|
|
|
2021-12-06 11:46:45 +01:00
|
|
|
/* Limit the memory size via mem. */
|
|
|
|
static phys_addr_t memory_limit;
|
2023-12-12 14:01:12 +01:00
|
|
|
#ifdef CONFIG_XIP_KERNEL
|
|
|
|
#define memory_limit (*(phys_addr_t *)XIP_FIXUP(&memory_limit))
|
|
|
|
#endif /* CONFIG_XIP_KERNEL */
|
2021-06-02 16:55:17 +08:00
|
|
|
|
|
|
|
static int __init early_mem(char *p)
|
|
|
|
{
|
|
|
|
u64 size;
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
size = memparse(p, &p) & PAGE_MASK;
|
|
|
|
memory_limit = min_t(u64, size, memory_limit);
|
|
|
|
|
|
|
|
pr_notice("Memory limited to %lldMB\n", (u64)memory_limit >> 20);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_param("mem", early_mem);
|
|
|
|
|
2021-05-10 19:42:22 +08:00
|
|
|
static void __init setup_bootmem(void)
|
2019-02-21 11:25:49 +05:30
|
|
|
{
|
2020-01-02 11:12:40 +08:00
|
|
|
phys_addr_t vmlinux_end = __pa_symbol(&_end);
|
2021-12-06 23:03:50 +08:00
|
|
|
phys_addr_t max_mapped_addr;
|
2021-12-06 23:03:52 +08:00
|
|
|
phys_addr_t phys_ram_end, vmlinux_start;
|
2019-02-21 11:25:49 +05:30
|
|
|
|
2021-12-06 23:03:52 +08:00
|
|
|
if (IS_ENABLED(CONFIG_XIP_KERNEL))
|
|
|
|
vmlinux_start = __pa_symbol(&_sdata);
|
|
|
|
else
|
|
|
|
vmlinux_start = __pa_symbol(&_start);
|
2021-04-13 02:35:14 -04:00
|
|
|
|
2021-06-02 16:55:17 +08:00
|
|
|
memblock_enforce_memory_limit(memory_limit);
|
2019-02-21 11:25:49 +05:30
|
|
|
|
2021-04-29 17:05:00 +02:00
|
|
|
/*
|
|
|
|
* Make sure we align the reservation on PMD_SIZE since we will
|
2021-04-11 12:41:44 -04:00
|
|
|
* map the kernel in the linear mapping as read-only: we do not want
|
|
|
|
* any allocation to happen between _end and the next pmd aligned page.
|
|
|
|
*/
|
2021-12-06 23:03:50 +08:00
|
|
|
if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
|
|
|
|
vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK;
|
|
|
|
/*
|
|
|
|
* Reserve from the start of the kernel to the end of the kernel
|
|
|
|
*/
|
2021-04-29 17:05:00 +02:00
|
|
|
memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
|
2019-06-07 06:01:29 +00:00
|
|
|
|
2023-07-04 14:18:37 +02:00
|
|
|
/*
|
|
|
|
* Make sure we align the start of the memory on a PMD boundary so that
|
|
|
|
* at worst, we map the linear mapping with PMD mappings.
|
|
|
|
*/
|
2024-12-09 20:26:17 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_XIP_KERNEL)) {
|
2023-07-04 14:18:37 +02:00
|
|
|
phys_ram_base = memblock_start_of_DRAM() & PMD_MASK;
|
2024-12-09 20:26:17 +08:00
|
|
|
#ifdef CONFIG_SPARSEMEM_VMEMMAP
|
|
|
|
vmemmap_start_pfn = round_down(phys_ram_base, VMEMMAP_ADDR_ALIGN) >> PAGE_SHIFT;
|
|
|
|
#endif
|
|
|
|
}
|
2023-03-24 16:54:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In 64-bit, any use of __va/__pa before this point is wrong as we
|
|
|
|
* did not know the start of DRAM before.
|
|
|
|
*/
|
2024-02-26 16:34:47 -08:00
|
|
|
if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU))
|
2023-03-24 16:54:21 +01:00
|
|
|
kernel_map.va_pa_offset = PAGE_OFFSET - phys_ram_base;
|
|
|
|
|
riscv: Fix linear mapping checks for non-contiguous memory regions
The RISC-V kernel already has checks to ensure that memory which would
lie outside of the linear mapping is not used. However those checks
use memory_limit, which is used to implement the mem= kernel command
line option (to limit the total amount of memory, not its address
range). When memory is made up of two or more non-contiguous memory
banks this check is incorrect.
Two changes are made here:
- add a call in setup_bootmem() to memblock_cap_memory_range() which
will cause any memory which falls outside the linear mapping to be
removed from the memory regions.
- remove the check in create_linear_mapping_page_table() which was
intended to remove memory which is outside the liner mapping based
on memory_limit, as it is no longer needed. Note a check for
mapping more memory than memory_limit (to implement mem=) is
unnecessary because of the existing call to
memblock_enforce_memory_limit().
This issue was seen when booting on a SV39 platform with two memory
banks:
0x00,80000000 1GiB
0x20,00000000 32GiB
This memory range is 158GiB from top to bottom, but the linear mapping
is limited to 128GiB, so the lower block of RAM will be mapped at
PAGE_OFFSET, and the upper block straddles the top of the linear
mapping.
This causes the following Oops:
[ 0.000000] Linux version 6.10.0-rc2-gd3b8dd5b51dd-dirty (stuart.menefy@codasip.com) (riscv64-codasip-linux-gcc (GCC) 13.2.0, GNU ld (GNU Binutils) 2.41.0.20231213) #20 SMP Sat Jun 22 11:34:22 BST 2024
[ 0.000000] memblock_add: [0x0000000080000000-0x00000000bfffffff] early_init_dt_add_memory_arch+0x4a/0x52
[ 0.000000] memblock_add: [0x0000002000000000-0x00000027ffffffff] early_init_dt_add_memory_arch+0x4a/0x52
...
[ 0.000000] memblock_alloc_try_nid: 23724 bytes align=0x8 nid=-1 from=0x0000000000000000 max_addr=0x0000000000000000 early_init_dt_alloc_memory_arch+0x1e/0x48
[ 0.000000] memblock_reserve: [0x00000027ffff5350-0x00000027ffffaffb] memblock_alloc_range_nid+0xb8/0x132
[ 0.000000] Unable to handle kernel paging request at virtual address fffffffe7fff5350
[ 0.000000] Oops [#1]
[ 0.000000] Modules linked in:
[ 0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 6.10.0-rc2-gd3b8dd5b51dd-dirty #20
[ 0.000000] Hardware name: codasip,a70x (DT)
[ 0.000000] epc : __memset+0x8c/0x104
[ 0.000000] ra : memblock_alloc_try_nid+0x74/0x84
[ 0.000000] epc : ffffffff805e88c8 ra : ffffffff806148f6 sp : ffffffff80e03d50
[ 0.000000] gp : ffffffff80ec4158 tp : ffffffff80e0bec0 t0 : fffffffe7fff52f8
[ 0.000000] t1 : 00000027ffffb000 t2 : 5f6b636f6c626d65 s0 : ffffffff80e03d90
[ 0.000000] s1 : 0000000000005cac a0 : fffffffe7fff5350 a1 : 0000000000000000
[ 0.000000] a2 : 0000000000005cac a3 : fffffffe7fffaff8 a4 : 000000000000002c
[ 0.000000] a5 : ffffffff805e88c8 a6 : 0000000000005cac a7 : 0000000000000030
[ 0.000000] s2 : fffffffe7fff5350 s3 : ffffffffffffffff s4 : 0000000000000000
[ 0.000000] s5 : ffffffff8062347e s6 : 0000000000000000 s7 : 0000000000000001
[ 0.000000] s8 : 0000000000002000 s9 : 00000000800226d0 s10: 0000000000000000
[ 0.000000] s11: 0000000000000000 t3 : ffffffff8080a928 t4 : ffffffff8080a928
[ 0.000000] t5 : ffffffff8080a928 t6 : ffffffff8080a940
[ 0.000000] status: 0000000200000100 badaddr: fffffffe7fff5350 cause: 000000000000000f
[ 0.000000] [<ffffffff805e88c8>] __memset+0x8c/0x104
[ 0.000000] [<ffffffff8062349c>] early_init_dt_alloc_memory_arch+0x1e/0x48
[ 0.000000] [<ffffffff8043e892>] __unflatten_device_tree+0x52/0x114
[ 0.000000] [<ffffffff8062441e>] unflatten_device_tree+0x9e/0xb8
[ 0.000000] [<ffffffff806046fe>] setup_arch+0xd4/0x5bc
[ 0.000000] [<ffffffff806007aa>] start_kernel+0x76/0x81a
[ 0.000000] Code: b823 02b2 bc23 02b2 b023 04b2 b423 04b2 b823 04b2 (bc23) 04b2
[ 0.000000] ---[ end trace 0000000000000000 ]---
[ 0.000000] Kernel panic - not syncing: Attempted to kill the idle task!
[ 0.000000] ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]---
The problem is that memblock (unaware that some physical memory cannot
be used) has allocated memory from the top of memory but which is
outside the linear mapping region.
Signed-off-by: Stuart Menefy <stuart.menefy@codasip.com>
Fixes: c99127c45248 ("riscv: Make sure the linear mapping does not use the kernel mapping")
Reviewed-by: David McKay <david.mckay@codasip.com>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20240622114217.2158495-1-stuart.menefy@codasip.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2024-06-22 12:42:16 +01:00
|
|
|
/*
|
|
|
|
* The size of the linear page mapping may restrict the amount of
|
|
|
|
* usable RAM.
|
|
|
|
*/
|
2024-08-27 08:52:30 +02:00
|
|
|
if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU)) {
|
riscv: Fix linear mapping checks for non-contiguous memory regions
The RISC-V kernel already has checks to ensure that memory which would
lie outside of the linear mapping is not used. However those checks
use memory_limit, which is used to implement the mem= kernel command
line option (to limit the total amount of memory, not its address
range). When memory is made up of two or more non-contiguous memory
banks this check is incorrect.
Two changes are made here:
- add a call in setup_bootmem() to memblock_cap_memory_range() which
will cause any memory which falls outside the linear mapping to be
removed from the memory regions.
- remove the check in create_linear_mapping_page_table() which was
intended to remove memory which is outside the liner mapping based
on memory_limit, as it is no longer needed. Note a check for
mapping more memory than memory_limit (to implement mem=) is
unnecessary because of the existing call to
memblock_enforce_memory_limit().
This issue was seen when booting on a SV39 platform with two memory
banks:
0x00,80000000 1GiB
0x20,00000000 32GiB
This memory range is 158GiB from top to bottom, but the linear mapping
is limited to 128GiB, so the lower block of RAM will be mapped at
PAGE_OFFSET, and the upper block straddles the top of the linear
mapping.
This causes the following Oops:
[ 0.000000] Linux version 6.10.0-rc2-gd3b8dd5b51dd-dirty (stuart.menefy@codasip.com) (riscv64-codasip-linux-gcc (GCC) 13.2.0, GNU ld (GNU Binutils) 2.41.0.20231213) #20 SMP Sat Jun 22 11:34:22 BST 2024
[ 0.000000] memblock_add: [0x0000000080000000-0x00000000bfffffff] early_init_dt_add_memory_arch+0x4a/0x52
[ 0.000000] memblock_add: [0x0000002000000000-0x00000027ffffffff] early_init_dt_add_memory_arch+0x4a/0x52
...
[ 0.000000] memblock_alloc_try_nid: 23724 bytes align=0x8 nid=-1 from=0x0000000000000000 max_addr=0x0000000000000000 early_init_dt_alloc_memory_arch+0x1e/0x48
[ 0.000000] memblock_reserve: [0x00000027ffff5350-0x00000027ffffaffb] memblock_alloc_range_nid+0xb8/0x132
[ 0.000000] Unable to handle kernel paging request at virtual address fffffffe7fff5350
[ 0.000000] Oops [#1]
[ 0.000000] Modules linked in:
[ 0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 6.10.0-rc2-gd3b8dd5b51dd-dirty #20
[ 0.000000] Hardware name: codasip,a70x (DT)
[ 0.000000] epc : __memset+0x8c/0x104
[ 0.000000] ra : memblock_alloc_try_nid+0x74/0x84
[ 0.000000] epc : ffffffff805e88c8 ra : ffffffff806148f6 sp : ffffffff80e03d50
[ 0.000000] gp : ffffffff80ec4158 tp : ffffffff80e0bec0 t0 : fffffffe7fff52f8
[ 0.000000] t1 : 00000027ffffb000 t2 : 5f6b636f6c626d65 s0 : ffffffff80e03d90
[ 0.000000] s1 : 0000000000005cac a0 : fffffffe7fff5350 a1 : 0000000000000000
[ 0.000000] a2 : 0000000000005cac a3 : fffffffe7fffaff8 a4 : 000000000000002c
[ 0.000000] a5 : ffffffff805e88c8 a6 : 0000000000005cac a7 : 0000000000000030
[ 0.000000] s2 : fffffffe7fff5350 s3 : ffffffffffffffff s4 : 0000000000000000
[ 0.000000] s5 : ffffffff8062347e s6 : 0000000000000000 s7 : 0000000000000001
[ 0.000000] s8 : 0000000000002000 s9 : 00000000800226d0 s10: 0000000000000000
[ 0.000000] s11: 0000000000000000 t3 : ffffffff8080a928 t4 : ffffffff8080a928
[ 0.000000] t5 : ffffffff8080a928 t6 : ffffffff8080a940
[ 0.000000] status: 0000000200000100 badaddr: fffffffe7fff5350 cause: 000000000000000f
[ 0.000000] [<ffffffff805e88c8>] __memset+0x8c/0x104
[ 0.000000] [<ffffffff8062349c>] early_init_dt_alloc_memory_arch+0x1e/0x48
[ 0.000000] [<ffffffff8043e892>] __unflatten_device_tree+0x52/0x114
[ 0.000000] [<ffffffff8062441e>] unflatten_device_tree+0x9e/0xb8
[ 0.000000] [<ffffffff806046fe>] setup_arch+0xd4/0x5bc
[ 0.000000] [<ffffffff806007aa>] start_kernel+0x76/0x81a
[ 0.000000] Code: b823 02b2 bc23 02b2 b023 04b2 b423 04b2 b823 04b2 (bc23) 04b2
[ 0.000000] ---[ end trace 0000000000000000 ]---
[ 0.000000] Kernel panic - not syncing: Attempted to kill the idle task!
[ 0.000000] ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]---
The problem is that memblock (unaware that some physical memory cannot
be used) has allocated memory from the top of memory but which is
outside the linear mapping region.
Signed-off-by: Stuart Menefy <stuart.menefy@codasip.com>
Fixes: c99127c45248 ("riscv: Make sure the linear mapping does not use the kernel mapping")
Reviewed-by: David McKay <david.mckay@codasip.com>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20240622114217.2158495-1-stuart.menefy@codasip.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2024-06-22 12:42:16 +01:00
|
|
|
max_mapped_addr = __pa(PAGE_OFFSET) + KERN_VIRT_SIZE;
|
2024-08-14 14:26:25 +08:00
|
|
|
if (memblock_end_of_DRAM() > max_mapped_addr) {
|
|
|
|
memblock_cap_memory_range(phys_ram_base,
|
|
|
|
max_mapped_addr - phys_ram_base);
|
|
|
|
pr_warn("Physical memory overflows the linear mapping size: region above %pa removed",
|
|
|
|
&max_mapped_addr);
|
|
|
|
}
|
riscv: Fix linear mapping checks for non-contiguous memory regions
The RISC-V kernel already has checks to ensure that memory which would
lie outside of the linear mapping is not used. However those checks
use memory_limit, which is used to implement the mem= kernel command
line option (to limit the total amount of memory, not its address
range). When memory is made up of two or more non-contiguous memory
banks this check is incorrect.
Two changes are made here:
- add a call in setup_bootmem() to memblock_cap_memory_range() which
will cause any memory which falls outside the linear mapping to be
removed from the memory regions.
- remove the check in create_linear_mapping_page_table() which was
intended to remove memory which is outside the liner mapping based
on memory_limit, as it is no longer needed. Note a check for
mapping more memory than memory_limit (to implement mem=) is
unnecessary because of the existing call to
memblock_enforce_memory_limit().
This issue was seen when booting on a SV39 platform with two memory
banks:
0x00,80000000 1GiB
0x20,00000000 32GiB
This memory range is 158GiB from top to bottom, but the linear mapping
is limited to 128GiB, so the lower block of RAM will be mapped at
PAGE_OFFSET, and the upper block straddles the top of the linear
mapping.
This causes the following Oops:
[ 0.000000] Linux version 6.10.0-rc2-gd3b8dd5b51dd-dirty (stuart.menefy@codasip.com) (riscv64-codasip-linux-gcc (GCC) 13.2.0, GNU ld (GNU Binutils) 2.41.0.20231213) #20 SMP Sat Jun 22 11:34:22 BST 2024
[ 0.000000] memblock_add: [0x0000000080000000-0x00000000bfffffff] early_init_dt_add_memory_arch+0x4a/0x52
[ 0.000000] memblock_add: [0x0000002000000000-0x00000027ffffffff] early_init_dt_add_memory_arch+0x4a/0x52
...
[ 0.000000] memblock_alloc_try_nid: 23724 bytes align=0x8 nid=-1 from=0x0000000000000000 max_addr=0x0000000000000000 early_init_dt_alloc_memory_arch+0x1e/0x48
[ 0.000000] memblock_reserve: [0x00000027ffff5350-0x00000027ffffaffb] memblock_alloc_range_nid+0xb8/0x132
[ 0.000000] Unable to handle kernel paging request at virtual address fffffffe7fff5350
[ 0.000000] Oops [#1]
[ 0.000000] Modules linked in:
[ 0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 6.10.0-rc2-gd3b8dd5b51dd-dirty #20
[ 0.000000] Hardware name: codasip,a70x (DT)
[ 0.000000] epc : __memset+0x8c/0x104
[ 0.000000] ra : memblock_alloc_try_nid+0x74/0x84
[ 0.000000] epc : ffffffff805e88c8 ra : ffffffff806148f6 sp : ffffffff80e03d50
[ 0.000000] gp : ffffffff80ec4158 tp : ffffffff80e0bec0 t0 : fffffffe7fff52f8
[ 0.000000] t1 : 00000027ffffb000 t2 : 5f6b636f6c626d65 s0 : ffffffff80e03d90
[ 0.000000] s1 : 0000000000005cac a0 : fffffffe7fff5350 a1 : 0000000000000000
[ 0.000000] a2 : 0000000000005cac a3 : fffffffe7fffaff8 a4 : 000000000000002c
[ 0.000000] a5 : ffffffff805e88c8 a6 : 0000000000005cac a7 : 0000000000000030
[ 0.000000] s2 : fffffffe7fff5350 s3 : ffffffffffffffff s4 : 0000000000000000
[ 0.000000] s5 : ffffffff8062347e s6 : 0000000000000000 s7 : 0000000000000001
[ 0.000000] s8 : 0000000000002000 s9 : 00000000800226d0 s10: 0000000000000000
[ 0.000000] s11: 0000000000000000 t3 : ffffffff8080a928 t4 : ffffffff8080a928
[ 0.000000] t5 : ffffffff8080a928 t6 : ffffffff8080a940
[ 0.000000] status: 0000000200000100 badaddr: fffffffe7fff5350 cause: 000000000000000f
[ 0.000000] [<ffffffff805e88c8>] __memset+0x8c/0x104
[ 0.000000] [<ffffffff8062349c>] early_init_dt_alloc_memory_arch+0x1e/0x48
[ 0.000000] [<ffffffff8043e892>] __unflatten_device_tree+0x52/0x114
[ 0.000000] [<ffffffff8062441e>] unflatten_device_tree+0x9e/0xb8
[ 0.000000] [<ffffffff806046fe>] setup_arch+0xd4/0x5bc
[ 0.000000] [<ffffffff806007aa>] start_kernel+0x76/0x81a
[ 0.000000] Code: b823 02b2 bc23 02b2 b023 04b2 b423 04b2 b823 04b2 (bc23) 04b2
[ 0.000000] ---[ end trace 0000000000000000 ]---
[ 0.000000] Kernel panic - not syncing: Attempted to kill the idle task!
[ 0.000000] ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]---
The problem is that memblock (unaware that some physical memory cannot
be used) has allocated memory from the top of memory but which is
outside the linear mapping region.
Signed-off-by: Stuart Menefy <stuart.menefy@codasip.com>
Fixes: c99127c45248 ("riscv: Make sure the linear mapping does not use the kernel mapping")
Reviewed-by: David McKay <david.mckay@codasip.com>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20240622114217.2158495-1-stuart.menefy@codasip.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2024-06-22 12:42:16 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 15:45:02 -08:00
|
|
|
/*
|
2024-04-25 13:52:01 +02:00
|
|
|
* Reserve physical address space that would be mapped to virtual
|
|
|
|
* addresses greater than (void *)(-PAGE_SIZE) because:
|
|
|
|
* - This memory would overlap with ERR_PTR
|
|
|
|
* - This memory belongs to high memory, which is not supported
|
|
|
|
*
|
|
|
|
* This is not applicable to 64-bit kernel, because virtual addresses
|
|
|
|
* after (void *)(-PAGE_SIZE) are not linearly mapped: they are
|
|
|
|
* occupied by kernel mapping. Also it is unrealistic for high memory
|
|
|
|
* to exist on 64-bit platforms.
|
2021-01-11 15:45:02 -08:00
|
|
|
*/
|
2021-12-06 23:03:50 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_64BIT)) {
|
2024-04-25 13:52:01 +02:00
|
|
|
max_mapped_addr = __va_to_pa_nodebug(-PAGE_SIZE);
|
|
|
|
memblock_reserve(max_mapped_addr, (phys_addr_t)-max_mapped_addr);
|
2021-12-06 23:03:50 +08:00
|
|
|
}
|
2021-01-11 15:45:02 -08:00
|
|
|
|
riscv: Fix linear mapping checks for non-contiguous memory regions
The RISC-V kernel already has checks to ensure that memory which would
lie outside of the linear mapping is not used. However those checks
use memory_limit, which is used to implement the mem= kernel command
line option (to limit the total amount of memory, not its address
range). When memory is made up of two or more non-contiguous memory
banks this check is incorrect.
Two changes are made here:
- add a call in setup_bootmem() to memblock_cap_memory_range() which
will cause any memory which falls outside the linear mapping to be
removed from the memory regions.
- remove the check in create_linear_mapping_page_table() which was
intended to remove memory which is outside the liner mapping based
on memory_limit, as it is no longer needed. Note a check for
mapping more memory than memory_limit (to implement mem=) is
unnecessary because of the existing call to
memblock_enforce_memory_limit().
This issue was seen when booting on a SV39 platform with two memory
banks:
0x00,80000000 1GiB
0x20,00000000 32GiB
This memory range is 158GiB from top to bottom, but the linear mapping
is limited to 128GiB, so the lower block of RAM will be mapped at
PAGE_OFFSET, and the upper block straddles the top of the linear
mapping.
This causes the following Oops:
[ 0.000000] Linux version 6.10.0-rc2-gd3b8dd5b51dd-dirty (stuart.menefy@codasip.com) (riscv64-codasip-linux-gcc (GCC) 13.2.0, GNU ld (GNU Binutils) 2.41.0.20231213) #20 SMP Sat Jun 22 11:34:22 BST 2024
[ 0.000000] memblock_add: [0x0000000080000000-0x00000000bfffffff] early_init_dt_add_memory_arch+0x4a/0x52
[ 0.000000] memblock_add: [0x0000002000000000-0x00000027ffffffff] early_init_dt_add_memory_arch+0x4a/0x52
...
[ 0.000000] memblock_alloc_try_nid: 23724 bytes align=0x8 nid=-1 from=0x0000000000000000 max_addr=0x0000000000000000 early_init_dt_alloc_memory_arch+0x1e/0x48
[ 0.000000] memblock_reserve: [0x00000027ffff5350-0x00000027ffffaffb] memblock_alloc_range_nid+0xb8/0x132
[ 0.000000] Unable to handle kernel paging request at virtual address fffffffe7fff5350
[ 0.000000] Oops [#1]
[ 0.000000] Modules linked in:
[ 0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 6.10.0-rc2-gd3b8dd5b51dd-dirty #20
[ 0.000000] Hardware name: codasip,a70x (DT)
[ 0.000000] epc : __memset+0x8c/0x104
[ 0.000000] ra : memblock_alloc_try_nid+0x74/0x84
[ 0.000000] epc : ffffffff805e88c8 ra : ffffffff806148f6 sp : ffffffff80e03d50
[ 0.000000] gp : ffffffff80ec4158 tp : ffffffff80e0bec0 t0 : fffffffe7fff52f8
[ 0.000000] t1 : 00000027ffffb000 t2 : 5f6b636f6c626d65 s0 : ffffffff80e03d90
[ 0.000000] s1 : 0000000000005cac a0 : fffffffe7fff5350 a1 : 0000000000000000
[ 0.000000] a2 : 0000000000005cac a3 : fffffffe7fffaff8 a4 : 000000000000002c
[ 0.000000] a5 : ffffffff805e88c8 a6 : 0000000000005cac a7 : 0000000000000030
[ 0.000000] s2 : fffffffe7fff5350 s3 : ffffffffffffffff s4 : 0000000000000000
[ 0.000000] s5 : ffffffff8062347e s6 : 0000000000000000 s7 : 0000000000000001
[ 0.000000] s8 : 0000000000002000 s9 : 00000000800226d0 s10: 0000000000000000
[ 0.000000] s11: 0000000000000000 t3 : ffffffff8080a928 t4 : ffffffff8080a928
[ 0.000000] t5 : ffffffff8080a928 t6 : ffffffff8080a940
[ 0.000000] status: 0000000200000100 badaddr: fffffffe7fff5350 cause: 000000000000000f
[ 0.000000] [<ffffffff805e88c8>] __memset+0x8c/0x104
[ 0.000000] [<ffffffff8062349c>] early_init_dt_alloc_memory_arch+0x1e/0x48
[ 0.000000] [<ffffffff8043e892>] __unflatten_device_tree+0x52/0x114
[ 0.000000] [<ffffffff8062441e>] unflatten_device_tree+0x9e/0xb8
[ 0.000000] [<ffffffff806046fe>] setup_arch+0xd4/0x5bc
[ 0.000000] [<ffffffff806007aa>] start_kernel+0x76/0x81a
[ 0.000000] Code: b823 02b2 bc23 02b2 b023 04b2 b423 04b2 b823 04b2 (bc23) 04b2
[ 0.000000] ---[ end trace 0000000000000000 ]---
[ 0.000000] Kernel panic - not syncing: Attempted to kill the idle task!
[ 0.000000] ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]---
The problem is that memblock (unaware that some physical memory cannot
be used) has allocated memory from the top of memory but which is
outside the linear mapping region.
Signed-off-by: Stuart Menefy <stuart.menefy@codasip.com>
Fixes: c99127c45248 ("riscv: Make sure the linear mapping does not use the kernel mapping")
Reviewed-by: David McKay <david.mckay@codasip.com>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20240622114217.2158495-1-stuart.menefy@codasip.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2024-06-22 12:42:16 +01:00
|
|
|
phys_ram_end = memblock_end_of_DRAM();
|
2021-07-21 09:59:35 +02:00
|
|
|
min_low_pfn = PFN_UP(phys_ram_base);
|
|
|
|
max_low_pfn = max_pfn = PFN_DOWN(phys_ram_end);
|
2021-02-25 14:54:17 +08:00
|
|
|
|
2020-10-31 14:01:12 +08:00
|
|
|
dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn));
|
2019-02-21 11:25:49 +05:30
|
|
|
|
2021-01-15 13:46:06 +08:00
|
|
|
reserve_initrd_mem();
|
2023-03-29 10:19:30 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No allocation should be done before reserving the memory as defined
|
|
|
|
* in the device tree, otherwise the allocation could end up in a
|
|
|
|
* reserved region.
|
|
|
|
*/
|
|
|
|
early_init_fdt_scan_reserved_mem();
|
|
|
|
|
riscv: Fix memblock reservation for device tree blob
This fixes an error with how the FDT blob is reserved in memblock.
An incorrect physical address calculation exposed the FDT header to
unintended corruption, which typically manifested with of_fdt_raw_init()
faulting during late boot after fdt_totalsize() returned a wrong value.
Systems with smaller physical memory sizes more frequently trigger this
issue, as the kernel is more likely to allocate from the DMA32 zone
where bbl places the DTB after the kernel image.
Commit 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages")
changed the mapping of the DTB to reside in the fixmap area.
Consequently, early_init_fdt_reserve_self() cannot be used anymore in
setup_bootmem() since it relies on __pa() to derive a physical address,
which does not work with dtb_early_va that is no longer a valid kernel
logical address.
The reserved[0x1] region shows the effect of the pointer underflow
resulting from the __pa(initial_boot_params) offset subtraction:
[ 0.000000] MEMBLOCK configuration:
[ 0.000000] memory size = 0x000000001fe00000 reserved size = 0x0000000000a2e514
[ 0.000000] memory.cnt = 0x1
[ 0.000000] memory[0x0] [0x0000000080200000-0x000000009fffffff], 0x000000001fe00000 bytes flags: 0x0
[ 0.000000] reserved.cnt = 0x2
[ 0.000000] reserved[0x0] [0x0000000080200000-0x0000000080c2dfeb], 0x0000000000a2dfec bytes flags: 0x0
[ 0.000000] reserved[0x1] [0xfffffff080100000-0xfffffff080100527], 0x0000000000000528 bytes flags: 0x0
With the fix applied:
[ 0.000000] MEMBLOCK configuration:
[ 0.000000] memory size = 0x000000001fe00000 reserved size = 0x0000000000a2e514
[ 0.000000] memory.cnt = 0x1
[ 0.000000] memory[0x0] [0x0000000080200000-0x000000009fffffff], 0x000000001fe00000 bytes flags: 0x0
[ 0.000000] reserved.cnt = 0x2
[ 0.000000] reserved[0x0] [0x0000000080200000-0x0000000080c2dfeb], 0x0000000000a2dfec bytes flags: 0x0
[ 0.000000] reserved[0x1] [0x0000000080e00000-0x0000000080e00527], 0x0000000000000528 bytes flags: 0x0
Fixes: 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages")
Signed-off-by: Albert Ou <aou@eecs.berkeley.edu>
Tested-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Paul Walmsley <paul.walmsley@sifive.com>
2019-09-27 16:14:18 -07:00
|
|
|
/*
|
2021-01-16 01:49:48 +02:00
|
|
|
* If DTB is built in, no need to reserve its memblock.
|
|
|
|
* Otherwise, do reserve it but avoid using
|
|
|
|
* early_init_fdt_reserve_self() since __pa() does
|
riscv: Fix memblock reservation for device tree blob
This fixes an error with how the FDT blob is reserved in memblock.
An incorrect physical address calculation exposed the FDT header to
unintended corruption, which typically manifested with of_fdt_raw_init()
faulting during late boot after fdt_totalsize() returned a wrong value.
Systems with smaller physical memory sizes more frequently trigger this
issue, as the kernel is more likely to allocate from the DMA32 zone
where bbl places the DTB after the kernel image.
Commit 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages")
changed the mapping of the DTB to reside in the fixmap area.
Consequently, early_init_fdt_reserve_self() cannot be used anymore in
setup_bootmem() since it relies on __pa() to derive a physical address,
which does not work with dtb_early_va that is no longer a valid kernel
logical address.
The reserved[0x1] region shows the effect of the pointer underflow
resulting from the __pa(initial_boot_params) offset subtraction:
[ 0.000000] MEMBLOCK configuration:
[ 0.000000] memory size = 0x000000001fe00000 reserved size = 0x0000000000a2e514
[ 0.000000] memory.cnt = 0x1
[ 0.000000] memory[0x0] [0x0000000080200000-0x000000009fffffff], 0x000000001fe00000 bytes flags: 0x0
[ 0.000000] reserved.cnt = 0x2
[ 0.000000] reserved[0x0] [0x0000000080200000-0x0000000080c2dfeb], 0x0000000000a2dfec bytes flags: 0x0
[ 0.000000] reserved[0x1] [0xfffffff080100000-0xfffffff080100527], 0x0000000000000528 bytes flags: 0x0
With the fix applied:
[ 0.000000] MEMBLOCK configuration:
[ 0.000000] memory size = 0x000000001fe00000 reserved size = 0x0000000000a2e514
[ 0.000000] memory.cnt = 0x1
[ 0.000000] memory[0x0] [0x0000000080200000-0x000000009fffffff], 0x000000001fe00000 bytes flags: 0x0
[ 0.000000] reserved.cnt = 0x2
[ 0.000000] reserved[0x0] [0x0000000080200000-0x0000000080c2dfeb], 0x0000000000a2dfec bytes flags: 0x0
[ 0.000000] reserved[0x1] [0x0000000080e00000-0x0000000080e00527], 0x0000000000000528 bytes flags: 0x0
Fixes: 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages")
Signed-off-by: Albert Ou <aou@eecs.berkeley.edu>
Tested-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Paul Walmsley <paul.walmsley@sifive.com>
2019-09-27 16:14:18 -07:00
|
|
|
* not work for DTB pointers that are fixmap addresses
|
|
|
|
*/
|
2023-03-29 10:19:32 +02:00
|
|
|
if (!IS_ENABLED(CONFIG_BUILTIN_DTB))
|
|
|
|
memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
|
riscv: Fix memblock reservation for device tree blob
This fixes an error with how the FDT blob is reserved in memblock.
An incorrect physical address calculation exposed the FDT header to
unintended corruption, which typically manifested with of_fdt_raw_init()
faulting during late boot after fdt_totalsize() returned a wrong value.
Systems with smaller physical memory sizes more frequently trigger this
issue, as the kernel is more likely to allocate from the DMA32 zone
where bbl places the DTB after the kernel image.
Commit 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages")
changed the mapping of the DTB to reside in the fixmap area.
Consequently, early_init_fdt_reserve_self() cannot be used anymore in
setup_bootmem() since it relies on __pa() to derive a physical address,
which does not work with dtb_early_va that is no longer a valid kernel
logical address.
The reserved[0x1] region shows the effect of the pointer underflow
resulting from the __pa(initial_boot_params) offset subtraction:
[ 0.000000] MEMBLOCK configuration:
[ 0.000000] memory size = 0x000000001fe00000 reserved size = 0x0000000000a2e514
[ 0.000000] memory.cnt = 0x1
[ 0.000000] memory[0x0] [0x0000000080200000-0x000000009fffffff], 0x000000001fe00000 bytes flags: 0x0
[ 0.000000] reserved.cnt = 0x2
[ 0.000000] reserved[0x0] [0x0000000080200000-0x0000000080c2dfeb], 0x0000000000a2dfec bytes flags: 0x0
[ 0.000000] reserved[0x1] [0xfffffff080100000-0xfffffff080100527], 0x0000000000000528 bytes flags: 0x0
With the fix applied:
[ 0.000000] MEMBLOCK configuration:
[ 0.000000] memory size = 0x000000001fe00000 reserved size = 0x0000000000a2e514
[ 0.000000] memory.cnt = 0x1
[ 0.000000] memory[0x0] [0x0000000080200000-0x000000009fffffff], 0x000000001fe00000 bytes flags: 0x0
[ 0.000000] reserved.cnt = 0x2
[ 0.000000] reserved[0x0] [0x0000000080200000-0x0000000080c2dfeb], 0x0000000000a2dfec bytes flags: 0x0
[ 0.000000] reserved[0x1] [0x0000000080e00000-0x0000000080e00527], 0x0000000000000528 bytes flags: 0x0
Fixes: 671f9a3e2e24 ("RISC-V: Setup initial page tables in two stages")
Signed-off-by: Albert Ou <aou@eecs.berkeley.edu>
Tested-by: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Paul Walmsley <paul.walmsley@sifive.com>
2019-09-27 16:14:18 -07:00
|
|
|
|
2020-10-31 14:01:12 +08:00
|
|
|
dma_contiguous_reserve(dma32_phys_limit);
|
2021-07-30 20:48:41 +08:00
|
|
|
if (IS_ENABLED(CONFIG_64BIT))
|
|
|
|
hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
|
2019-02-21 11:25:49 +05:30
|
|
|
}
|
2019-02-13 16:38:36 +05:30
|
|
|
|
2024-10-26 10:13:55 -07:00
|
|
|
#ifdef CONFIG_RELOCATABLE
|
|
|
|
extern unsigned long __rela_dyn_start, __rela_dyn_end;
|
|
|
|
|
|
|
|
static void __init relocate_kernel(void)
|
|
|
|
{
|
2024-10-26 10:13:57 -07:00
|
|
|
Elf_Rela *rela = (Elf_Rela *)&__rela_dyn_start;
|
2024-10-26 10:13:55 -07:00
|
|
|
/*
|
|
|
|
* This holds the offset between the linked virtual address and the
|
|
|
|
* relocated virtual address.
|
|
|
|
*/
|
|
|
|
uintptr_t reloc_offset = kernel_map.virt_addr - KERNEL_LINK_ADDR;
|
|
|
|
/*
|
|
|
|
* This holds the offset between kernel linked virtual address and
|
|
|
|
* physical address.
|
|
|
|
*/
|
|
|
|
uintptr_t va_kernel_link_pa_offset = KERNEL_LINK_ADDR - kernel_map.phys_addr;
|
|
|
|
|
2024-10-26 10:13:57 -07:00
|
|
|
for ( ; rela < (Elf_Rela *)&__rela_dyn_end; rela++) {
|
|
|
|
Elf_Addr addr = (rela->r_offset - va_kernel_link_pa_offset);
|
|
|
|
Elf_Addr relocated_addr = rela->r_addend;
|
2024-10-26 10:13:55 -07:00
|
|
|
|
|
|
|
if (rela->r_info != R_RISCV_RELATIVE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure to not relocate vdso symbols like rt_sigreturn
|
|
|
|
* which are linked from the address 0 in vmlinux since
|
|
|
|
* vdso symbol addresses are actually used as an offset from
|
|
|
|
* mm->context.vdso in VDSO_OFFSET macro.
|
|
|
|
*/
|
|
|
|
if (relocated_addr >= KERNEL_LINK_ADDR)
|
|
|
|
relocated_addr += reloc_offset;
|
|
|
|
|
2024-10-26 10:13:57 -07:00
|
|
|
*(Elf_Addr *)addr = relocated_addr;
|
2024-10-26 10:13:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_RELOCATABLE */
|
|
|
|
|
2019-10-28 13:10:41 +01:00
|
|
|
#ifdef CONFIG_MMU
|
2024-06-05 13:40:46 +02:00
|
|
|
struct pt_alloc_ops pt_ops __meminitdata;
|
2021-04-13 02:35:14 -04:00
|
|
|
|
2019-02-13 16:38:36 +05:30
|
|
|
pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
|
2019-06-28 13:36:21 -07:00
|
|
|
pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
|
2021-05-16 21:15:56 +08:00
|
|
|
static pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
|
2019-02-13 16:38:36 +05:30
|
|
|
|
2019-06-28 13:36:21 -07:00
|
|
|
pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
|
2019-01-07 20:57:01 +05:30
|
|
|
|
2021-04-13 02:35:14 -04:00
|
|
|
#ifdef CONFIG_XIP_KERNEL
|
2021-12-06 23:03:53 +08:00
|
|
|
#define pt_ops (*(struct pt_alloc_ops *)XIP_FIXUP(&pt_ops))
|
2021-04-13 02:35:14 -04:00
|
|
|
#define trampoline_pg_dir ((pgd_t *)XIP_FIXUP(trampoline_pg_dir))
|
|
|
|
#define fixmap_pte ((pte_t *)XIP_FIXUP(fixmap_pte))
|
|
|
|
#define early_pg_dir ((pgd_t *)XIP_FIXUP(early_pg_dir))
|
|
|
|
#endif /* CONFIG_XIP_KERNEL */
|
|
|
|
|
2022-07-11 12:35:50 +05:30
|
|
|
static const pgprot_t protection_map[16] = {
|
|
|
|
[VM_NONE] = PAGE_NONE,
|
|
|
|
[VM_READ] = PAGE_READ,
|
|
|
|
[VM_WRITE] = PAGE_COPY,
|
|
|
|
[VM_WRITE | VM_READ] = PAGE_COPY,
|
|
|
|
[VM_EXEC] = PAGE_EXEC,
|
|
|
|
[VM_EXEC | VM_READ] = PAGE_READ_EXEC,
|
|
|
|
[VM_EXEC | VM_WRITE] = PAGE_COPY_EXEC,
|
2023-04-25 18:28:28 +08:00
|
|
|
[VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_EXEC,
|
2022-07-11 12:35:50 +05:30
|
|
|
[VM_SHARED] = PAGE_NONE,
|
|
|
|
[VM_SHARED | VM_READ] = PAGE_READ,
|
|
|
|
[VM_SHARED | VM_WRITE] = PAGE_SHARED,
|
|
|
|
[VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
|
|
|
|
[VM_SHARED | VM_EXEC] = PAGE_EXEC,
|
|
|
|
[VM_SHARED | VM_EXEC | VM_READ] = PAGE_READ_EXEC,
|
|
|
|
[VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED_EXEC,
|
|
|
|
[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_EXEC
|
|
|
|
};
|
|
|
|
DECLARE_VM_GET_PAGE_PROT
|
|
|
|
|
2019-01-07 20:57:01 +05:30
|
|
|
void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
|
|
|
|
{
|
|
|
|
unsigned long addr = __fix_to_virt(idx);
|
|
|
|
pte_t *ptep;
|
|
|
|
|
|
|
|
BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
|
|
|
|
|
|
|
|
ptep = &fixmap_pte[pte_index(addr)];
|
|
|
|
|
2020-08-04 11:02:05 +08:00
|
|
|
if (pgprot_val(prot))
|
2019-01-07 20:57:01 +05:30
|
|
|
set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
|
2020-08-04 11:02:05 +08:00
|
|
|
else
|
2019-01-07 20:57:01 +05:30
|
|
|
pte_clear(&init_mm, addr, ptep);
|
2020-08-04 11:02:05 +08:00
|
|
|
local_flush_tlb_page(addr);
|
2019-01-07 20:57:01 +05:30
|
|
|
}
|
|
|
|
|
2020-09-17 15:37:12 -07:00
|
|
|
static inline pte_t *__init get_pte_virt_early(phys_addr_t pa)
|
2019-06-28 13:36:21 -07:00
|
|
|
{
|
2020-09-17 15:37:12 -07:00
|
|
|
return (pte_t *)((uintptr_t)pa);
|
2019-06-28 13:36:21 -07:00
|
|
|
}
|
|
|
|
|
2020-09-17 15:37:12 -07:00
|
|
|
static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa)
|
|
|
|
{
|
|
|
|
clear_fixmap(FIX_PTE);
|
|
|
|
return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static inline pte_t *__meminit get_pte_virt_late(phys_addr_t pa)
|
2020-09-17 15:37:12 -07:00
|
|
|
{
|
|
|
|
return (pte_t *) __va(pa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline phys_addr_t __init alloc_pte_early(uintptr_t va)
|
2019-06-28 13:36:21 -07:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We only create PMD or PGD early mappings so we
|
|
|
|
* should never reach here with MMU disabled.
|
|
|
|
*/
|
2020-09-17 15:37:12 -07:00
|
|
|
BUG();
|
|
|
|
}
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2020-09-17 15:37:12 -07:00
|
|
|
static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va)
|
|
|
|
{
|
2019-06-28 13:36:21 -07:00
|
|
|
return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static phys_addr_t __meminit alloc_pte_late(uintptr_t va)
|
2020-09-17 15:37:12 -07:00
|
|
|
{
|
2023-08-07 16:05:08 -07:00
|
|
|
struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL & ~__GFP_HIGHMEM, 0);
|
2020-09-17 15:37:12 -07:00
|
|
|
|
2025-04-08 10:52:20 +01:00
|
|
|
/*
|
|
|
|
* We do not know which mm the PTE page is associated to at this point.
|
|
|
|
* Passing NULL to the ctor is the safe option, though it may result
|
|
|
|
* in unnecessary work (e.g. initialising the ptlock for init_mm).
|
|
|
|
*/
|
mm: pass mm down to pagetable_{pte,pmd}_ctor
Patch series "Always call constructor for kernel page tables", v2.
There has been much confusion around exactly when page table
constructors/destructors (pagetable_*_[cd]tor) are supposed to be called.
They were initially introduced for user PTEs only (to support split page
table locks), then at the PMD level for the same purpose. Accounting was
added later on, starting at the PTE level and then moving to higher levels
(PMD, PUD). Finally, with my earlier series "Account page tables at all
levels" [1], the ctor/dtor is run for all levels, all the way to PGD.
I thought this was the end of the story, and it hopefully is for user
pgtables, but I was wrong for what concerns kernel pgtables. The current
situation there makes very little sense:
* At the PTE level, the ctor/dtor is not called (at least in the generic
implementation). Specific helpers are used for kernel pgtables at this
level (pte_{alloc,free}_kernel()) and those have never called the
ctor/dtor, most likely because they were initially irrelevant in the
kernel case.
* At all other levels, the ctor/dtor is normally called. This is
potentially wasteful at the PMD level (more on that later).
This series aims to ensure that the ctor/dtor is always called for kernel
pgtables, as it already is for user pgtables. Besides consistency, the
main motivation is to guarantee that ctor/dtor hooks are systematically
called; this makes it possible to insert hooks to protect page tables [2],
for instance. There is however an extra challenge: split locks are not
used for kernel pgtables, and it would therefore be wasteful to initialise
them (ptlock_init()).
It is worth clarifying exactly when split locks are used. They clearly
are for user pgtables, but as illustrated in commit 61444cde9170 ("ARM:
8591/1: mm: use fully constructed struct pages for EFI pgd allocations"),
they also are for special page tables like efi_mm. The one case where
split locks are definitely unused is pgtables owned by init_mm; this is
consistent with the behaviour of apply_to_pte_range().
The approach chosen in this series is therefore to pass the mm associated
to the pgtables being constructed to pagetable_{pte,pmd}_ctor() (patch 1),
and skip ptlock_init() if mm == &init_mm (patch 3 and 7). This makes it
possible to call the PTE ctor/dtor from pte_{alloc,free}_kernel() without
unintended consequences (patch 3). As a result the accounting functions
are now called at all levels for kernel pgtables, and split locks are
never initialised.
In configurations where ptlocks are dynamically allocated (32-bit,
PREEMPT_RT, etc.) and ARCH_ENABLE_SPLIT_PMD_PTLOCK is selected, this
series results in the removal of a kmem_cache allocation for every kernel
PMD. Additionally, for certain architectures that do not use
<asm-generic/pgalloc.h> such as s390, the same optimisation occurs at the
PTE level.
===
Things get more complicated when it comes to special pgtable allocators
(patch 8-12). All architectures need such allocators to create initial
kernel pgtables; we are not concerned with those as the ctor cannot be
called so early in the boot sequence. However, those allocators may also
be used later in the boot sequence or during normal operations. There are
two main use-cases:
1. Mapping EFI memory: efi_mm (arm, arm64, riscv)
2. arch_add_memory(): init_mm
The ctor is already explicitly run (at the PTE/PMD level) in the first
case, as required for pgtables that are not associated with init_mm.
However the same allocators may also be used for the second use-case (or
others), and this is where it gets messy. Patch 1 calls the ctor with
NULL as mm in those situations, as the actual mm isn't available.
Practically this means that ptlocks will be unconditionally initialised.
This is fine on arm - create_mapping_late() is only used for the EFI
mapping. On arm64, __create_pgd_mapping() is also used by
arch_add_memory(); patch 8/9/11 ensure that ctors are called at all levels
with the appropriate mm. The situation is similar on riscv, but
propagating the mm down to the ctor would require significant refactoring.
Since they are already called unconditionally, this series leaves riscv
no worse off - patch 10 adds comments to clarify the situation.
From a cursory look at other architectures implementing arch_add_memory(),
s390 and x86 may also need a similar treatment to add constructor calls.
This is to be taken care of in a future version or as a follow-up.
===
The complications in those special pgtable allocators beg the question:
does it really make sense to treat efi_mm and init_mm differently in e.g.
apply_to_pte_range()? Maybe what we really need is a way to tell if an mm
corresponds to user memory or not, and never use split locks for non-user
mm's. Feedback and suggestions welcome!
This patch (of 12):
In preparation for calling constructors for all kernel page tables while
eliding unnecessary ptlock initialisation, let's pass down the associated
mm to the PTE/PMD level ctors. (These are the two levels where ptlocks
are used.)
In most cases the mm is already around at the point of calling the ctor so
we simply pass it down. This is however not the case for special page
table allocators:
* arch/arm/mm/mmu.c
* arch/arm64/mm/mmu.c
* arch/riscv/mm/init.c
In those cases, the page tables being allocated are either for standard
kernel memory (init_mm) or special page directories, which may not be
associated to any mm. For now let's pass NULL as mm; this will be refined
where possible in future patches.
No functional change in this patch.
Link: https://lore.kernel.org/linux-mm/20250103184415.2744423-1-kevin.brodsky@arm.com/ [1]
Link: https://lore.kernel.org/linux-hardening/20250203101839.1223008-1-kevin.brodsky@arm.com/ [2]
Link: https://lkml.kernel.org/r/20250408095222.860601-1-kevin.brodsky@arm.com
Link: https://lkml.kernel.org/r/20250408095222.860601-2-kevin.brodsky@arm.com
Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
Reviewed-by: Alexander Gordeev <agordeev@linux.ibm.com> [s390]
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Andreas Larsson <andreas@gaisler.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Kevin Brodsky <kevin.brodsky@arm.com>
Cc: Linus Waleij <linus.walleij@linaro.org>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Qi Zheng <zhengqi.arch@bytedance.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yang Shi <yang@os.amperecomputing.com>
Cc: <x86@kernel.org>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2025-04-08 10:52:11 +01:00
|
|
|
BUG_ON(!ptdesc || !pagetable_pte_ctor(NULL, ptdesc));
|
2023-08-07 16:05:08 -07:00
|
|
|
return __pa((pte_t *)ptdesc_address(ptdesc));
|
2020-09-17 15:37:12 -07:00
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static void __meminit create_pte_mapping(pte_t *ptep, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
|
|
|
|
pgprot_t prot)
|
2019-06-28 13:36:21 -07:00
|
|
|
{
|
2020-06-08 21:33:10 -07:00
|
|
|
uintptr_t pte_idx = pte_index(va);
|
2019-06-28 13:36:21 -07:00
|
|
|
|
|
|
|
BUG_ON(sz != PAGE_SIZE);
|
|
|
|
|
2020-06-08 21:33:10 -07:00
|
|
|
if (pte_none(ptep[pte_idx]))
|
|
|
|
ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot);
|
2019-06-28 13:36:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef __PAGETABLE_PMD_FOLDED
|
|
|
|
|
2021-05-16 21:15:56 +08:00
|
|
|
static pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
|
|
|
|
static pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
|
|
|
|
static pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2021-04-13 02:35:14 -04:00
|
|
|
#ifdef CONFIG_XIP_KERNEL
|
|
|
|
#define trampoline_pmd ((pmd_t *)XIP_FIXUP(trampoline_pmd))
|
|
|
|
#define fixmap_pmd ((pmd_t *)XIP_FIXUP(fixmap_pmd))
|
|
|
|
#define early_pmd ((pmd_t *)XIP_FIXUP(early_pmd))
|
|
|
|
#endif /* CONFIG_XIP_KERNEL */
|
|
|
|
|
2022-01-27 10:48:42 +08:00
|
|
|
static p4d_t trampoline_p4d[PTRS_PER_P4D] __page_aligned_bss;
|
|
|
|
static p4d_t fixmap_p4d[PTRS_PER_P4D] __page_aligned_bss;
|
|
|
|
static p4d_t early_p4d[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE);
|
|
|
|
|
|
|
|
#ifdef CONFIG_XIP_KERNEL
|
|
|
|
#define trampoline_p4d ((p4d_t *)XIP_FIXUP(trampoline_p4d))
|
|
|
|
#define fixmap_p4d ((p4d_t *)XIP_FIXUP(fixmap_p4d))
|
|
|
|
#define early_p4d ((p4d_t *)XIP_FIXUP(early_p4d))
|
|
|
|
#endif /* CONFIG_XIP_KERNEL */
|
|
|
|
|
2021-12-06 11:46:51 +01:00
|
|
|
static pud_t trampoline_pud[PTRS_PER_PUD] __page_aligned_bss;
|
|
|
|
static pud_t fixmap_pud[PTRS_PER_PUD] __page_aligned_bss;
|
|
|
|
static pud_t early_pud[PTRS_PER_PUD] __initdata __aligned(PAGE_SIZE);
|
|
|
|
|
|
|
|
#ifdef CONFIG_XIP_KERNEL
|
|
|
|
#define trampoline_pud ((pud_t *)XIP_FIXUP(trampoline_pud))
|
|
|
|
#define fixmap_pud ((pud_t *)XIP_FIXUP(fixmap_pud))
|
|
|
|
#define early_pud ((pud_t *)XIP_FIXUP(early_pud))
|
|
|
|
#endif /* CONFIG_XIP_KERNEL */
|
|
|
|
|
2020-09-17 15:37:12 -07:00
|
|
|
static pmd_t *__init get_pmd_virt_early(phys_addr_t pa)
|
2019-06-28 13:36:21 -07:00
|
|
|
{
|
2020-09-17 15:37:12 -07:00
|
|
|
/* Before MMU is enabled */
|
|
|
|
return (pmd_t *)((uintptr_t)pa);
|
2019-06-28 13:36:21 -07:00
|
|
|
}
|
|
|
|
|
2020-09-17 15:37:12 -07:00
|
|
|
static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa)
|
2019-06-28 13:36:21 -07:00
|
|
|
{
|
2020-09-17 15:37:12 -07:00
|
|
|
clear_fixmap(FIX_PMD);
|
|
|
|
return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
|
|
|
|
}
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static pmd_t *__meminit get_pmd_virt_late(phys_addr_t pa)
|
2020-09-17 15:37:12 -07:00
|
|
|
{
|
|
|
|
return (pmd_t *) __va(pa);
|
|
|
|
}
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2020-09-17 15:37:12 -07:00
|
|
|
static phys_addr_t __init alloc_pmd_early(uintptr_t va)
|
|
|
|
{
|
2021-12-06 11:46:51 +01:00
|
|
|
BUG_ON((va - kernel_map.virt_addr) >> PUD_SHIFT);
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2021-02-21 09:22:33 -05:00
|
|
|
return (uintptr_t)early_pmd;
|
2019-06-28 13:36:21 -07:00
|
|
|
}
|
|
|
|
|
2020-09-17 15:37:12 -07:00
|
|
|
static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va)
|
|
|
|
{
|
|
|
|
return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static phys_addr_t __meminit alloc_pmd_late(uintptr_t va)
|
2020-09-17 15:37:12 -07:00
|
|
|
{
|
2023-08-07 16:05:08 -07:00
|
|
|
struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL & ~__GFP_HIGHMEM, 0);
|
2021-09-27 11:03:25 +08:00
|
|
|
|
2025-04-08 10:52:20 +01:00
|
|
|
/* See comment in alloc_pte_late() regarding NULL passed the ctor */
|
mm: pass mm down to pagetable_{pte,pmd}_ctor
Patch series "Always call constructor for kernel page tables", v2.
There has been much confusion around exactly when page table
constructors/destructors (pagetable_*_[cd]tor) are supposed to be called.
They were initially introduced for user PTEs only (to support split page
table locks), then at the PMD level for the same purpose. Accounting was
added later on, starting at the PTE level and then moving to higher levels
(PMD, PUD). Finally, with my earlier series "Account page tables at all
levels" [1], the ctor/dtor is run for all levels, all the way to PGD.
I thought this was the end of the story, and it hopefully is for user
pgtables, but I was wrong for what concerns kernel pgtables. The current
situation there makes very little sense:
* At the PTE level, the ctor/dtor is not called (at least in the generic
implementation). Specific helpers are used for kernel pgtables at this
level (pte_{alloc,free}_kernel()) and those have never called the
ctor/dtor, most likely because they were initially irrelevant in the
kernel case.
* At all other levels, the ctor/dtor is normally called. This is
potentially wasteful at the PMD level (more on that later).
This series aims to ensure that the ctor/dtor is always called for kernel
pgtables, as it already is for user pgtables. Besides consistency, the
main motivation is to guarantee that ctor/dtor hooks are systematically
called; this makes it possible to insert hooks to protect page tables [2],
for instance. There is however an extra challenge: split locks are not
used for kernel pgtables, and it would therefore be wasteful to initialise
them (ptlock_init()).
It is worth clarifying exactly when split locks are used. They clearly
are for user pgtables, but as illustrated in commit 61444cde9170 ("ARM:
8591/1: mm: use fully constructed struct pages for EFI pgd allocations"),
they also are for special page tables like efi_mm. The one case where
split locks are definitely unused is pgtables owned by init_mm; this is
consistent with the behaviour of apply_to_pte_range().
The approach chosen in this series is therefore to pass the mm associated
to the pgtables being constructed to pagetable_{pte,pmd}_ctor() (patch 1),
and skip ptlock_init() if mm == &init_mm (patch 3 and 7). This makes it
possible to call the PTE ctor/dtor from pte_{alloc,free}_kernel() without
unintended consequences (patch 3). As a result the accounting functions
are now called at all levels for kernel pgtables, and split locks are
never initialised.
In configurations where ptlocks are dynamically allocated (32-bit,
PREEMPT_RT, etc.) and ARCH_ENABLE_SPLIT_PMD_PTLOCK is selected, this
series results in the removal of a kmem_cache allocation for every kernel
PMD. Additionally, for certain architectures that do not use
<asm-generic/pgalloc.h> such as s390, the same optimisation occurs at the
PTE level.
===
Things get more complicated when it comes to special pgtable allocators
(patch 8-12). All architectures need such allocators to create initial
kernel pgtables; we are not concerned with those as the ctor cannot be
called so early in the boot sequence. However, those allocators may also
be used later in the boot sequence or during normal operations. There are
two main use-cases:
1. Mapping EFI memory: efi_mm (arm, arm64, riscv)
2. arch_add_memory(): init_mm
The ctor is already explicitly run (at the PTE/PMD level) in the first
case, as required for pgtables that are not associated with init_mm.
However the same allocators may also be used for the second use-case (or
others), and this is where it gets messy. Patch 1 calls the ctor with
NULL as mm in those situations, as the actual mm isn't available.
Practically this means that ptlocks will be unconditionally initialised.
This is fine on arm - create_mapping_late() is only used for the EFI
mapping. On arm64, __create_pgd_mapping() is also used by
arch_add_memory(); patch 8/9/11 ensure that ctors are called at all levels
with the appropriate mm. The situation is similar on riscv, but
propagating the mm down to the ctor would require significant refactoring.
Since they are already called unconditionally, this series leaves riscv
no worse off - patch 10 adds comments to clarify the situation.
From a cursory look at other architectures implementing arch_add_memory(),
s390 and x86 may also need a similar treatment to add constructor calls.
This is to be taken care of in a future version or as a follow-up.
===
The complications in those special pgtable allocators beg the question:
does it really make sense to treat efi_mm and init_mm differently in e.g.
apply_to_pte_range()? Maybe what we really need is a way to tell if an mm
corresponds to user memory or not, and never use split locks for non-user
mm's. Feedback and suggestions welcome!
This patch (of 12):
In preparation for calling constructors for all kernel page tables while
eliding unnecessary ptlock initialisation, let's pass down the associated
mm to the PTE/PMD level ctors. (These are the two levels where ptlocks
are used.)
In most cases the mm is already around at the point of calling the ctor so
we simply pass it down. This is however not the case for special page
table allocators:
* arch/arm/mm/mmu.c
* arch/arm64/mm/mmu.c
* arch/riscv/mm/init.c
In those cases, the page tables being allocated are either for standard
kernel memory (init_mm) or special page directories, which may not be
associated to any mm. For now let's pass NULL as mm; this will be refined
where possible in future patches.
No functional change in this patch.
Link: https://lore.kernel.org/linux-mm/20250103184415.2744423-1-kevin.brodsky@arm.com/ [1]
Link: https://lore.kernel.org/linux-hardening/20250203101839.1223008-1-kevin.brodsky@arm.com/ [2]
Link: https://lkml.kernel.org/r/20250408095222.860601-1-kevin.brodsky@arm.com
Link: https://lkml.kernel.org/r/20250408095222.860601-2-kevin.brodsky@arm.com
Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
Reviewed-by: Alexander Gordeev <agordeev@linux.ibm.com> [s390]
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Andreas Larsson <andreas@gaisler.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Kevin Brodsky <kevin.brodsky@arm.com>
Cc: Linus Waleij <linus.walleij@linaro.org>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Qi Zheng <zhengqi.arch@bytedance.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Yang Shi <yang@os.amperecomputing.com>
Cc: <x86@kernel.org>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2025-04-08 10:52:11 +01:00
|
|
|
BUG_ON(!ptdesc || !pagetable_pmd_ctor(NULL, ptdesc));
|
2023-08-07 16:05:08 -07:00
|
|
|
return __pa((pmd_t *)ptdesc_address(ptdesc));
|
2020-09-17 15:37:12 -07:00
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static void __meminit create_pmd_mapping(pmd_t *pmdp,
|
|
|
|
uintptr_t va, phys_addr_t pa,
|
|
|
|
phys_addr_t sz, pgprot_t prot)
|
2019-06-28 13:36:21 -07:00
|
|
|
{
|
|
|
|
pte_t *ptep;
|
|
|
|
phys_addr_t pte_phys;
|
2020-06-08 21:33:10 -07:00
|
|
|
uintptr_t pmd_idx = pmd_index(va);
|
2019-06-28 13:36:21 -07:00
|
|
|
|
|
|
|
if (sz == PMD_SIZE) {
|
2020-06-08 21:33:10 -07:00
|
|
|
if (pmd_none(pmdp[pmd_idx]))
|
|
|
|
pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot);
|
2019-06-28 13:36:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:33:10 -07:00
|
|
|
if (pmd_none(pmdp[pmd_idx])) {
|
2020-09-17 15:37:12 -07:00
|
|
|
pte_phys = pt_ops.alloc_pte(va);
|
2020-06-08 21:33:10 -07:00
|
|
|
pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
|
2020-09-17 15:37:12 -07:00
|
|
|
ptep = pt_ops.get_pte_virt(pte_phys);
|
2019-06-28 13:36:21 -07:00
|
|
|
memset(ptep, 0, PAGE_SIZE);
|
|
|
|
} else {
|
2020-06-08 21:33:10 -07:00
|
|
|
pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx]));
|
2020-09-17 15:37:12 -07:00
|
|
|
ptep = pt_ops.get_pte_virt(pte_phys);
|
2019-06-28 13:36:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
create_pte_mapping(ptep, va, pa, sz, prot);
|
|
|
|
}
|
|
|
|
|
2021-12-06 11:46:51 +01:00
|
|
|
static pud_t *__init get_pud_virt_early(phys_addr_t pa)
|
|
|
|
{
|
|
|
|
return (pud_t *)((uintptr_t)pa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static pud_t *__init get_pud_virt_fixmap(phys_addr_t pa)
|
|
|
|
{
|
|
|
|
clear_fixmap(FIX_PUD);
|
|
|
|
return (pud_t *)set_fixmap_offset(FIX_PUD, pa);
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static pud_t *__meminit get_pud_virt_late(phys_addr_t pa)
|
2021-12-06 11:46:51 +01:00
|
|
|
{
|
|
|
|
return (pud_t *)__va(pa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static phys_addr_t __init alloc_pud_early(uintptr_t va)
|
|
|
|
{
|
|
|
|
/* Only one PUD is available for early mapping */
|
|
|
|
BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
|
|
|
|
|
|
|
|
return (uintptr_t)early_pud;
|
|
|
|
}
|
|
|
|
|
|
|
|
static phys_addr_t __init alloc_pud_fixmap(uintptr_t va)
|
|
|
|
{
|
|
|
|
return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static phys_addr_t __meminit alloc_pud_late(uintptr_t va)
|
2021-12-06 11:46:51 +01:00
|
|
|
{
|
2025-04-08 10:52:22 +01:00
|
|
|
struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL, 0);
|
2021-12-06 11:46:51 +01:00
|
|
|
|
2025-04-08 10:52:22 +01:00
|
|
|
BUG_ON(!ptdesc);
|
|
|
|
pagetable_pud_ctor(ptdesc);
|
|
|
|
return __pa((pud_t *)ptdesc_address(ptdesc));
|
2021-12-06 11:46:51 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 10:48:42 +08:00
|
|
|
static p4d_t *__init get_p4d_virt_early(phys_addr_t pa)
|
|
|
|
{
|
|
|
|
return (p4d_t *)((uintptr_t)pa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static p4d_t *__init get_p4d_virt_fixmap(phys_addr_t pa)
|
|
|
|
{
|
|
|
|
clear_fixmap(FIX_P4D);
|
|
|
|
return (p4d_t *)set_fixmap_offset(FIX_P4D, pa);
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static p4d_t *__meminit get_p4d_virt_late(phys_addr_t pa)
|
2022-01-27 10:48:42 +08:00
|
|
|
{
|
|
|
|
return (p4d_t *)__va(pa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static phys_addr_t __init alloc_p4d_early(uintptr_t va)
|
|
|
|
{
|
|
|
|
/* Only one P4D is available for early mapping */
|
|
|
|
BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
|
|
|
|
|
|
|
|
return (uintptr_t)early_p4d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static phys_addr_t __init alloc_p4d_fixmap(uintptr_t va)
|
|
|
|
{
|
|
|
|
return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static phys_addr_t __meminit alloc_p4d_late(uintptr_t va)
|
2022-01-27 10:48:42 +08:00
|
|
|
{
|
2025-04-08 10:52:22 +01:00
|
|
|
struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL, 0);
|
2022-01-27 10:48:42 +08:00
|
|
|
|
2025-04-08 10:52:22 +01:00
|
|
|
BUG_ON(!ptdesc);
|
|
|
|
pagetable_p4d_ctor(ptdesc);
|
|
|
|
return __pa((p4d_t *)ptdesc_address(ptdesc));
|
2022-01-27 10:48:42 +08:00
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static void __meminit create_pud_mapping(pud_t *pudp, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
|
|
|
|
pgprot_t prot)
|
2021-12-06 11:46:51 +01:00
|
|
|
{
|
|
|
|
pmd_t *nextp;
|
|
|
|
phys_addr_t next_phys;
|
|
|
|
uintptr_t pud_index = pud_index(va);
|
|
|
|
|
|
|
|
if (sz == PUD_SIZE) {
|
|
|
|
if (pud_val(pudp[pud_index]) == 0)
|
|
|
|
pudp[pud_index] = pfn_pud(PFN_DOWN(pa), prot);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pud_val(pudp[pud_index]) == 0) {
|
|
|
|
next_phys = pt_ops.alloc_pmd(va);
|
|
|
|
pudp[pud_index] = pfn_pud(PFN_DOWN(next_phys), PAGE_TABLE);
|
|
|
|
nextp = pt_ops.get_pmd_virt(next_phys);
|
|
|
|
memset(nextp, 0, PAGE_SIZE);
|
|
|
|
} else {
|
|
|
|
next_phys = PFN_PHYS(_pud_pfn(pudp[pud_index]));
|
|
|
|
nextp = pt_ops.get_pmd_virt(next_phys);
|
|
|
|
}
|
|
|
|
|
|
|
|
create_pmd_mapping(nextp, va, pa, sz, prot);
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static void __meminit create_p4d_mapping(p4d_t *p4dp, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
|
|
|
|
pgprot_t prot)
|
2022-01-27 10:48:42 +08:00
|
|
|
{
|
|
|
|
pud_t *nextp;
|
|
|
|
phys_addr_t next_phys;
|
|
|
|
uintptr_t p4d_index = p4d_index(va);
|
|
|
|
|
|
|
|
if (sz == P4D_SIZE) {
|
|
|
|
if (p4d_val(p4dp[p4d_index]) == 0)
|
|
|
|
p4dp[p4d_index] = pfn_p4d(PFN_DOWN(pa), prot);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p4d_val(p4dp[p4d_index]) == 0) {
|
|
|
|
next_phys = pt_ops.alloc_pud(va);
|
|
|
|
p4dp[p4d_index] = pfn_p4d(PFN_DOWN(next_phys), PAGE_TABLE);
|
|
|
|
nextp = pt_ops.get_pud_virt(next_phys);
|
|
|
|
memset(nextp, 0, PAGE_SIZE);
|
|
|
|
} else {
|
|
|
|
next_phys = PFN_PHYS(_p4d_pfn(p4dp[p4d_index]));
|
|
|
|
nextp = pt_ops.get_pud_virt(next_phys);
|
|
|
|
}
|
|
|
|
|
|
|
|
create_pud_mapping(nextp, va, pa, sz, prot);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define pgd_next_t p4d_t
|
|
|
|
#define alloc_pgd_next(__va) (pgtable_l5_enabled ? \
|
|
|
|
pt_ops.alloc_p4d(__va) : (pgtable_l4_enabled ? \
|
|
|
|
pt_ops.alloc_pud(__va) : pt_ops.alloc_pmd(__va)))
|
|
|
|
#define get_pgd_next_virt(__pa) (pgtable_l5_enabled ? \
|
|
|
|
pt_ops.get_p4d_virt(__pa) : (pgd_next_t *)(pgtable_l4_enabled ? \
|
|
|
|
pt_ops.get_pud_virt(__pa) : (pud_t *)pt_ops.get_pmd_virt(__pa)))
|
2019-06-28 13:36:21 -07:00
|
|
|
#define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
|
2022-01-27 10:48:42 +08:00
|
|
|
(pgtable_l5_enabled ? \
|
|
|
|
create_p4d_mapping(__nextp, __va, __pa, __sz, __prot) : \
|
2021-12-06 11:46:51 +01:00
|
|
|
(pgtable_l4_enabled ? \
|
2022-01-27 10:48:42 +08:00
|
|
|
create_pud_mapping((pud_t *)__nextp, __va, __pa, __sz, __prot) : \
|
|
|
|
create_pmd_mapping((pmd_t *)__nextp, __va, __pa, __sz, __prot)))
|
|
|
|
#define fixmap_pgd_next (pgtable_l5_enabled ? \
|
|
|
|
(uintptr_t)fixmap_p4d : (pgtable_l4_enabled ? \
|
|
|
|
(uintptr_t)fixmap_pud : (uintptr_t)fixmap_pmd))
|
|
|
|
#define trampoline_pgd_next (pgtable_l5_enabled ? \
|
|
|
|
(uintptr_t)trampoline_p4d : (pgtable_l4_enabled ? \
|
|
|
|
(uintptr_t)trampoline_pud : (uintptr_t)trampoline_pmd))
|
2019-06-28 13:36:21 -07:00
|
|
|
#else
|
|
|
|
#define pgd_next_t pte_t
|
2020-09-17 15:37:12 -07:00
|
|
|
#define alloc_pgd_next(__va) pt_ops.alloc_pte(__va)
|
|
|
|
#define get_pgd_next_virt(__pa) pt_ops.get_pte_virt(__pa)
|
2019-06-28 13:36:21 -07:00
|
|
|
#define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
|
|
|
|
create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
|
2021-12-06 11:46:51 +01:00
|
|
|
#define fixmap_pgd_next ((uintptr_t)fixmap_pte)
|
2022-04-19 18:12:01 -07:00
|
|
|
#define create_p4d_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
|
|
|
|
#define create_pud_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
|
|
|
|
#define create_pmd_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
|
2021-12-06 11:46:51 +01:00
|
|
|
#endif /* __PAGETABLE_PMD_FOLDED */
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
void __meminit create_pgd_mapping(pgd_t *pgdp, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
|
|
|
|
pgprot_t prot)
|
2019-06-28 13:36:21 -07:00
|
|
|
{
|
|
|
|
pgd_next_t *nextp;
|
|
|
|
phys_addr_t next_phys;
|
2020-06-08 21:33:10 -07:00
|
|
|
uintptr_t pgd_idx = pgd_index(va);
|
2019-06-28 13:36:21 -07:00
|
|
|
|
|
|
|
if (sz == PGDIR_SIZE) {
|
2020-06-08 21:33:10 -07:00
|
|
|
if (pgd_val(pgdp[pgd_idx]) == 0)
|
|
|
|
pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot);
|
2019-06-28 13:36:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:33:10 -07:00
|
|
|
if (pgd_val(pgdp[pgd_idx]) == 0) {
|
2019-06-28 13:36:21 -07:00
|
|
|
next_phys = alloc_pgd_next(va);
|
2020-06-08 21:33:10 -07:00
|
|
|
pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
|
2019-06-28 13:36:21 -07:00
|
|
|
nextp = get_pgd_next_virt(next_phys);
|
|
|
|
memset(nextp, 0, PAGE_SIZE);
|
|
|
|
} else {
|
2020-06-08 21:33:10 -07:00
|
|
|
next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx]));
|
2019-06-28 13:36:21 -07:00
|
|
|
nextp = get_pgd_next_virt(next_phys);
|
|
|
|
}
|
|
|
|
|
|
|
|
create_pgd_next_mapping(nextp, va, pa, sz, prot);
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static uintptr_t __meminit best_map_size(phys_addr_t pa, uintptr_t va, phys_addr_t size)
|
2019-06-28 13:36:21 -07:00
|
|
|
{
|
2024-05-15 07:50:39 +02:00
|
|
|
if (debug_pagealloc_enabled())
|
|
|
|
return PAGE_SIZE;
|
|
|
|
|
2023-11-08 08:59:29 +01:00
|
|
|
if (pgtable_l5_enabled &&
|
|
|
|
!(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >= P4D_SIZE)
|
2023-03-24 16:54:21 +01:00
|
|
|
return P4D_SIZE;
|
|
|
|
|
2023-11-08 08:59:29 +01:00
|
|
|
if (pgtable_l4_enabled &&
|
|
|
|
!(pa & (PUD_SIZE - 1)) && !(va & (PUD_SIZE - 1)) && size >= PUD_SIZE)
|
2023-03-24 16:54:21 +01:00
|
|
|
return PUD_SIZE;
|
|
|
|
|
2023-11-08 08:59:29 +01:00
|
|
|
if (IS_ENABLED(CONFIG_64BIT) &&
|
|
|
|
!(pa & (PMD_SIZE - 1)) && !(va & (PMD_SIZE - 1)) && size >= PMD_SIZE)
|
2022-11-28 10:36:43 +08:00
|
|
|
return PMD_SIZE;
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2022-11-28 10:36:43 +08:00
|
|
|
return PAGE_SIZE;
|
2019-06-28 13:36:21 -07:00
|
|
|
}
|
|
|
|
|
2021-04-13 02:35:14 -04:00
|
|
|
#ifdef CONFIG_XIP_KERNEL
|
2022-02-04 13:13:37 -08:00
|
|
|
#define phys_ram_base (*(phys_addr_t *)XIP_FIXUP(&phys_ram_base))
|
2021-12-06 23:03:53 +08:00
|
|
|
extern char _xiprom[], _exiprom[], __data_loc;
|
|
|
|
|
2021-04-13 02:35:14 -04:00
|
|
|
/* called from head.S with MMU off */
|
|
|
|
asmlinkage void __init __copy_data(void)
|
|
|
|
{
|
2021-10-11 11:14:14 +02:00
|
|
|
void *from = (void *)(&__data_loc);
|
2021-04-13 02:35:14 -04:00
|
|
|
void *to = (void *)CONFIG_PHYS_RAM_BASE;
|
2021-10-11 11:14:14 +02:00
|
|
|
size_t sz = (size_t)((uintptr_t)(&_end) - (uintptr_t)(&_sdata));
|
2021-04-13 02:35:14 -04:00
|
|
|
|
|
|
|
memcpy(to, from, sz);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-24 14:00:41 +02:00
|
|
|
#ifdef CONFIG_STRICT_KERNEL_RWX
|
2024-06-05 13:40:46 +02:00
|
|
|
static __meminit pgprot_t pgprot_from_va(uintptr_t va)
|
2021-06-24 14:00:41 +02:00
|
|
|
{
|
|
|
|
if (is_va_kernel_text(va))
|
|
|
|
return PAGE_KERNEL_READ_EXEC;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In 64-bit kernel, the kernel mapping is outside the linear mapping so
|
|
|
|
* we must protect its linear mapping alias from being executed and
|
|
|
|
* written.
|
|
|
|
* And rodata section is marked readonly in mark_rodata_ro.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_64BIT) && is_va_kernel_lm_alias_text(va))
|
|
|
|
return PAGE_KERNEL_READ;
|
|
|
|
|
|
|
|
return PAGE_KERNEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mark_rodata_ro(void)
|
|
|
|
{
|
|
|
|
set_kernel_memory(__start_rodata, _data, set_memory_ro);
|
|
|
|
if (IS_ENABLED(CONFIG_64BIT))
|
|
|
|
set_kernel_memory(lm_alias(__start_rodata), lm_alias(_data),
|
|
|
|
set_memory_ro);
|
|
|
|
}
|
|
|
|
#else
|
2024-06-05 13:40:46 +02:00
|
|
|
static __meminit pgprot_t pgprot_from_va(uintptr_t va)
|
2021-06-24 14:00:41 +02:00
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_64BIT) && !is_kernel_mapping(va))
|
|
|
|
return PAGE_KERNEL;
|
|
|
|
|
|
|
|
return PAGE_KERNEL_EXEC;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_STRICT_KERNEL_RWX */
|
|
|
|
|
2022-04-19 20:13:27 -07:00
|
|
|
#if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
|
2023-04-24 11:23:13 +02:00
|
|
|
u64 __pi_set_satp_mode_from_cmdline(uintptr_t dtb_pa);
|
|
|
|
|
2022-01-27 10:48:43 +08:00
|
|
|
static void __init disable_pgtable_l5(void)
|
|
|
|
{
|
|
|
|
pgtable_l5_enabled = false;
|
|
|
|
kernel_map.page_offset = PAGE_OFFSET_L4;
|
|
|
|
satp_mode = SATP_MODE_48;
|
|
|
|
}
|
|
|
|
|
2021-12-06 11:46:51 +01:00
|
|
|
static void __init disable_pgtable_l4(void)
|
|
|
|
{
|
|
|
|
pgtable_l4_enabled = false;
|
|
|
|
kernel_map.page_offset = PAGE_OFFSET_L3;
|
|
|
|
satp_mode = SATP_MODE_39;
|
|
|
|
}
|
|
|
|
|
2023-04-24 11:23:13 +02:00
|
|
|
static int __init print_no4lvl(char *p)
|
|
|
|
{
|
|
|
|
pr_info("Disabled 4-level and 5-level paging");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_param("no4lvl", print_no4lvl);
|
|
|
|
|
|
|
|
static int __init print_no5lvl(char *p)
|
|
|
|
{
|
|
|
|
pr_info("Disabled 5-level paging");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_param("no5lvl", print_no5lvl);
|
|
|
|
|
2023-09-29 21:11:58 +00:00
|
|
|
static void __init set_mmap_rnd_bits_max(void)
|
|
|
|
{
|
|
|
|
mmap_rnd_bits_max = MMAP_VA_BITS - PAGE_SHIFT - 3;
|
|
|
|
}
|
|
|
|
|
2021-12-06 11:46:51 +01:00
|
|
|
/*
|
|
|
|
* There is a simple way to determine if 4-level is supported by the
|
|
|
|
* underlying hardware: establish 1:1 mapping in 4-level page table mode
|
|
|
|
* then read SATP to see if the configuration was taken into account
|
|
|
|
* meaning sv48 is supported.
|
|
|
|
*/
|
2023-04-24 11:23:13 +02:00
|
|
|
static __init void set_satp_mode(uintptr_t dtb_pa)
|
2021-12-06 11:46:51 +01:00
|
|
|
{
|
|
|
|
u64 identity_satp, hw_satp;
|
2022-01-27 10:48:43 +08:00
|
|
|
uintptr_t set_satp_mode_pmd = ((unsigned long)set_satp_mode) & PMD_MASK;
|
2023-04-24 11:23:13 +02:00
|
|
|
u64 satp_mode_cmdline = __pi_set_satp_mode_from_cmdline(dtb_pa);
|
|
|
|
|
2024-10-26 10:13:58 -07:00
|
|
|
kernel_map.page_offset = PAGE_OFFSET_L5;
|
|
|
|
|
2023-04-24 11:23:13 +02:00
|
|
|
if (satp_mode_cmdline == SATP_MODE_57) {
|
|
|
|
disable_pgtable_l5();
|
|
|
|
} else if (satp_mode_cmdline == SATP_MODE_48) {
|
|
|
|
disable_pgtable_l5();
|
|
|
|
disable_pgtable_l4();
|
|
|
|
return;
|
|
|
|
}
|
2021-12-06 11:46:51 +01:00
|
|
|
|
2022-01-27 10:48:43 +08:00
|
|
|
create_p4d_mapping(early_p4d,
|
|
|
|
set_satp_mode_pmd, (uintptr_t)early_pud,
|
|
|
|
P4D_SIZE, PAGE_TABLE);
|
2021-12-06 11:46:51 +01:00
|
|
|
create_pud_mapping(early_pud,
|
|
|
|
set_satp_mode_pmd, (uintptr_t)early_pmd,
|
|
|
|
PUD_SIZE, PAGE_TABLE);
|
|
|
|
/* Handle the case where set_satp_mode straddles 2 PMDs */
|
|
|
|
create_pmd_mapping(early_pmd,
|
|
|
|
set_satp_mode_pmd, set_satp_mode_pmd,
|
|
|
|
PMD_SIZE, PAGE_KERNEL_EXEC);
|
|
|
|
create_pmd_mapping(early_pmd,
|
|
|
|
set_satp_mode_pmd + PMD_SIZE,
|
|
|
|
set_satp_mode_pmd + PMD_SIZE,
|
|
|
|
PMD_SIZE, PAGE_KERNEL_EXEC);
|
2022-01-27 10:48:43 +08:00
|
|
|
retry:
|
|
|
|
create_pgd_mapping(early_pg_dir,
|
|
|
|
set_satp_mode_pmd,
|
2023-04-24 11:23:13 +02:00
|
|
|
pgtable_l5_enabled ?
|
|
|
|
(uintptr_t)early_p4d : (uintptr_t)early_pud,
|
2022-01-27 10:48:43 +08:00
|
|
|
PGDIR_SIZE, PAGE_TABLE);
|
2021-12-06 11:46:51 +01:00
|
|
|
|
|
|
|
identity_satp = PFN_DOWN((uintptr_t)&early_pg_dir) | satp_mode;
|
|
|
|
|
|
|
|
local_flush_tlb_all();
|
|
|
|
csr_write(CSR_SATP, identity_satp);
|
|
|
|
hw_satp = csr_swap(CSR_SATP, 0ULL);
|
|
|
|
local_flush_tlb_all();
|
|
|
|
|
2022-01-27 10:48:43 +08:00
|
|
|
if (hw_satp != identity_satp) {
|
2023-04-24 11:23:13 +02:00
|
|
|
if (pgtable_l5_enabled) {
|
2022-01-27 10:48:43 +08:00
|
|
|
disable_pgtable_l5();
|
2022-04-12 09:03:35 +05:30
|
|
|
memset(early_pg_dir, 0, PAGE_SIZE);
|
2022-01-27 10:48:43 +08:00
|
|
|
goto retry;
|
|
|
|
}
|
2021-12-06 11:46:51 +01:00
|
|
|
disable_pgtable_l4();
|
2022-01-27 10:48:43 +08:00
|
|
|
}
|
2021-12-06 11:46:51 +01:00
|
|
|
|
|
|
|
memset(early_pg_dir, 0, PAGE_SIZE);
|
2022-01-27 10:48:43 +08:00
|
|
|
memset(early_p4d, 0, PAGE_SIZE);
|
2021-12-06 11:46:51 +01:00
|
|
|
memset(early_pud, 0, PAGE_SIZE);
|
|
|
|
memset(early_pmd, 0, PAGE_SIZE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-03-26 08:03:47 +00:00
|
|
|
/*
|
|
|
|
* setup_vm() is called from head.S with MMU-off.
|
|
|
|
*
|
|
|
|
* Following requirements should be honoured for setup_vm() to work
|
|
|
|
* correctly:
|
|
|
|
* 1) It should use PC-relative addressing for accessing kernel symbols.
|
|
|
|
* To achieve this we always use GCC cmodel=medany.
|
|
|
|
* 2) The compiler instrumentation for FTRACE will not work for setup_vm()
|
|
|
|
* so disable compiler instrumentation when FTRACE is enabled.
|
|
|
|
*
|
|
|
|
* Currently, the above requirements are honoured by using custom CFLAGS
|
|
|
|
* for init.o in mm/Makefile.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __riscv_cmodel_medany
|
2019-10-17 14:45:58 -07:00
|
|
|
#error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
|
2019-03-26 08:03:47 +00:00
|
|
|
#endif
|
|
|
|
|
2021-04-13 02:35:14 -04:00
|
|
|
#ifdef CONFIG_XIP_KERNEL
|
2021-07-23 15:01:25 +02:00
|
|
|
static void __init create_kernel_page_table(pgd_t *pgdir,
|
2021-06-24 14:00:41 +02:00
|
|
|
__always_unused bool early)
|
2021-04-13 02:35:14 -04:00
|
|
|
{
|
2024-06-07 22:22:12 +02:00
|
|
|
uintptr_t va, start_va, end_va;
|
2021-04-13 02:35:14 -04:00
|
|
|
|
|
|
|
/* Map the flash resident part */
|
2021-06-17 15:53:07 +02:00
|
|
|
end_va = kernel_map.virt_addr + kernel_map.xiprom_sz;
|
2021-07-23 15:01:25 +02:00
|
|
|
for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE)
|
2021-04-13 02:35:14 -04:00
|
|
|
create_pgd_mapping(pgdir, va,
|
2021-06-17 15:53:07 +02:00
|
|
|
kernel_map.xiprom + (va - kernel_map.virt_addr),
|
2021-07-23 15:01:25 +02:00
|
|
|
PMD_SIZE, PAGE_KERNEL_EXEC);
|
2021-04-13 02:35:14 -04:00
|
|
|
|
|
|
|
/* Map the data in RAM */
|
2024-06-07 22:22:12 +02:00
|
|
|
start_va = kernel_map.virt_addr + (uintptr_t)&_sdata - (uintptr_t)&_start;
|
2024-05-08 21:19:17 +02:00
|
|
|
end_va = kernel_map.virt_addr + kernel_map.size;
|
2024-06-07 22:22:12 +02:00
|
|
|
for (va = start_va; va < end_va; va += PMD_SIZE)
|
2021-04-13 02:35:14 -04:00
|
|
|
create_pgd_mapping(pgdir, va,
|
2024-06-07 22:22:12 +02:00
|
|
|
kernel_map.phys_addr + (va - start_va),
|
2021-07-23 15:01:25 +02:00
|
|
|
PMD_SIZE, PAGE_KERNEL);
|
2021-04-13 02:35:14 -04:00
|
|
|
}
|
|
|
|
#else
|
2021-07-23 15:01:25 +02:00
|
|
|
static void __init create_kernel_page_table(pgd_t *pgdir, bool early)
|
2021-04-11 12:41:44 -04:00
|
|
|
{
|
|
|
|
uintptr_t va, end_va;
|
|
|
|
|
2021-06-17 15:53:07 +02:00
|
|
|
end_va = kernel_map.virt_addr + kernel_map.size;
|
2021-07-23 15:01:25 +02:00
|
|
|
for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE)
|
2021-04-11 12:41:44 -04:00
|
|
|
create_pgd_mapping(pgdir, va,
|
2021-06-17 15:53:07 +02:00
|
|
|
kernel_map.phys_addr + (va - kernel_map.virt_addr),
|
2021-07-23 15:01:25 +02:00
|
|
|
PMD_SIZE,
|
2021-06-24 14:00:41 +02:00
|
|
|
early ?
|
|
|
|
PAGE_KERNEL_EXEC : pgprot_from_va(va));
|
2021-04-11 12:41:44 -04:00
|
|
|
}
|
2021-04-13 02:35:14 -04:00
|
|
|
#endif
|
2021-04-11 12:41:44 -04:00
|
|
|
|
2021-07-23 15:01:28 +02:00
|
|
|
/*
|
|
|
|
* Setup a 4MB mapping that encompasses the device tree: for 64-bit kernel,
|
|
|
|
* this means 2 PMD entries whereas for 32-bit kernel, this is only 1 PGDIR
|
|
|
|
* entry.
|
|
|
|
*/
|
2023-04-26 18:00:09 +08:00
|
|
|
static void __init create_fdt_early_page_table(uintptr_t fix_fdt_va,
|
2023-03-29 10:19:30 +02:00
|
|
|
uintptr_t dtb_pa)
|
2019-02-13 16:38:36 +05:30
|
|
|
{
|
2023-05-19 15:13:11 +02:00
|
|
|
#ifndef CONFIG_BUILTIN_DTB
|
2021-07-23 15:01:28 +02:00
|
|
|
uintptr_t pa = dtb_pa & ~(PMD_SIZE - 1);
|
|
|
|
|
2023-03-29 10:19:30 +02:00
|
|
|
/* Make sure the fdt fixmap address is always aligned on PMD size */
|
|
|
|
BUILD_BUG_ON(FIX_FDT % (PMD_SIZE / PAGE_SIZE));
|
2021-12-06 11:46:51 +01:00
|
|
|
|
2023-03-29 10:19:30 +02:00
|
|
|
/* In 32-bit only, the fdt lies in its own PGD */
|
|
|
|
if (!IS_ENABLED(CONFIG_64BIT)) {
|
|
|
|
create_pgd_mapping(early_pg_dir, fix_fdt_va,
|
|
|
|
pa, MAX_FDT_SIZE, PAGE_KERNEL);
|
|
|
|
} else {
|
|
|
|
create_pmd_mapping(fixmap_pmd, fix_fdt_va,
|
2021-07-23 15:01:28 +02:00
|
|
|
pa, PMD_SIZE, PAGE_KERNEL);
|
2023-03-29 10:19:30 +02:00
|
|
|
create_pmd_mapping(fixmap_pmd, fix_fdt_va + PMD_SIZE,
|
2021-07-23 15:01:28 +02:00
|
|
|
pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL);
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:19:30 +02:00
|
|
|
dtb_early_va = (void *)fix_fdt_va + (dtb_pa & (PMD_SIZE - 1));
|
2021-07-23 15:01:28 +02:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* For 64-bit kernel, __va can't be used since it would return a linear
|
|
|
|
* mapping address whereas dtb_early_va will be used before
|
|
|
|
* setup_vm_final installs the linear mapping. For 32-bit kernel, as the
|
|
|
|
* kernel is mapped in the linear mapping, that makes no difference.
|
|
|
|
*/
|
2023-12-12 14:01:12 +01:00
|
|
|
dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
|
2020-09-17 15:37:11 -07:00
|
|
|
#endif
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2021-07-23 15:01:28 +02:00
|
|
|
dtb_early_pa = dtb_pa;
|
|
|
|
}
|
|
|
|
|
2021-12-06 11:46:47 +01:00
|
|
|
/*
|
|
|
|
* MMU is not enabled, the page tables are allocated directly using
|
|
|
|
* early_pmd/pud/p4d and the address returned is the physical one.
|
|
|
|
*/
|
2022-05-16 22:32:04 +08:00
|
|
|
static void __init pt_ops_set_early(void)
|
2021-12-06 11:46:47 +01:00
|
|
|
{
|
|
|
|
pt_ops.alloc_pte = alloc_pte_early;
|
|
|
|
pt_ops.get_pte_virt = get_pte_virt_early;
|
|
|
|
#ifndef __PAGETABLE_PMD_FOLDED
|
|
|
|
pt_ops.alloc_pmd = alloc_pmd_early;
|
|
|
|
pt_ops.get_pmd_virt = get_pmd_virt_early;
|
2021-12-06 11:46:51 +01:00
|
|
|
pt_ops.alloc_pud = alloc_pud_early;
|
|
|
|
pt_ops.get_pud_virt = get_pud_virt_early;
|
2022-01-27 10:48:42 +08:00
|
|
|
pt_ops.alloc_p4d = alloc_p4d_early;
|
|
|
|
pt_ops.get_p4d_virt = get_p4d_virt_early;
|
2021-12-06 11:46:47 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MMU is enabled but page table setup is not complete yet.
|
|
|
|
* fixmap page table alloc functions must be used as a means to temporarily
|
|
|
|
* map the allocated physical pages since the linear mapping does not exist yet.
|
|
|
|
*
|
|
|
|
* Note that this is called with MMU disabled, hence kernel_mapping_pa_to_va,
|
|
|
|
* but it will be used as described above.
|
|
|
|
*/
|
2022-05-16 22:32:04 +08:00
|
|
|
static void __init pt_ops_set_fixmap(void)
|
2021-12-06 11:46:47 +01:00
|
|
|
{
|
2022-11-26 00:09:20 -06:00
|
|
|
pt_ops.alloc_pte = kernel_mapping_pa_to_va(alloc_pte_fixmap);
|
|
|
|
pt_ops.get_pte_virt = kernel_mapping_pa_to_va(get_pte_virt_fixmap);
|
2021-12-06 11:46:47 +01:00
|
|
|
#ifndef __PAGETABLE_PMD_FOLDED
|
2022-11-26 00:09:20 -06:00
|
|
|
pt_ops.alloc_pmd = kernel_mapping_pa_to_va(alloc_pmd_fixmap);
|
|
|
|
pt_ops.get_pmd_virt = kernel_mapping_pa_to_va(get_pmd_virt_fixmap);
|
|
|
|
pt_ops.alloc_pud = kernel_mapping_pa_to_va(alloc_pud_fixmap);
|
|
|
|
pt_ops.get_pud_virt = kernel_mapping_pa_to_va(get_pud_virt_fixmap);
|
|
|
|
pt_ops.alloc_p4d = kernel_mapping_pa_to_va(alloc_p4d_fixmap);
|
|
|
|
pt_ops.get_p4d_virt = kernel_mapping_pa_to_va(get_p4d_virt_fixmap);
|
2021-12-06 11:46:47 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MMU is enabled and page table setup is complete, so from now, we can use
|
|
|
|
* generic page allocation functions to setup page table.
|
|
|
|
*/
|
2022-05-16 22:32:04 +08:00
|
|
|
static void __init pt_ops_set_late(void)
|
2021-12-06 11:46:47 +01:00
|
|
|
{
|
|
|
|
pt_ops.alloc_pte = alloc_pte_late;
|
|
|
|
pt_ops.get_pte_virt = get_pte_virt_late;
|
|
|
|
#ifndef __PAGETABLE_PMD_FOLDED
|
|
|
|
pt_ops.alloc_pmd = alloc_pmd_late;
|
|
|
|
pt_ops.get_pmd_virt = get_pmd_virt_late;
|
2021-12-06 11:46:51 +01:00
|
|
|
pt_ops.alloc_pud = alloc_pud_late;
|
|
|
|
pt_ops.get_pud_virt = get_pud_virt_late;
|
2022-01-27 10:48:42 +08:00
|
|
|
pt_ops.alloc_p4d = alloc_p4d_late;
|
|
|
|
pt_ops.get_p4d_virt = get_p4d_virt_late;
|
2021-12-06 11:46:47 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-07-22 14:38:46 +02:00
|
|
|
#ifdef CONFIG_RANDOMIZE_BASE
|
|
|
|
extern bool __init __pi_set_nokaslr_from_cmdline(uintptr_t dtb_pa);
|
|
|
|
extern u64 __init __pi_get_kaslr_seed(uintptr_t dtb_pa);
|
2024-07-09 13:39:37 -04:00
|
|
|
extern u64 __init __pi_get_kaslr_seed_zkr(const uintptr_t dtb_pa);
|
2023-07-22 14:38:46 +02:00
|
|
|
|
|
|
|
static int __init print_nokaslr(char *p)
|
|
|
|
{
|
|
|
|
pr_info("Disabled KASLR");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_param("nokaslr", print_nokaslr);
|
|
|
|
|
|
|
|
unsigned long kaslr_offset(void)
|
|
|
|
{
|
|
|
|
return kernel_map.virt_offset;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-06-28 13:36:21 -07:00
|
|
|
asmlinkage void __init setup_vm(uintptr_t dtb_pa)
|
2019-02-13 16:38:36 +05:30
|
|
|
{
|
2021-07-23 15:01:26 +02:00
|
|
|
pmd_t __maybe_unused fix_bmap_spmd, fix_bmap_epmd;
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2023-07-22 14:38:46 +02:00
|
|
|
#ifdef CONFIG_RANDOMIZE_BASE
|
|
|
|
if (!__pi_set_nokaslr_from_cmdline(dtb_pa)) {
|
2024-07-09 13:39:37 -04:00
|
|
|
u64 kaslr_seed = __pi_get_kaslr_seed_zkr(dtb_pa);
|
2023-07-22 14:38:46 +02:00
|
|
|
u32 kernel_size = (uintptr_t)(&_end) - (uintptr_t)(&_start);
|
|
|
|
u32 nr_pos;
|
|
|
|
|
2024-07-09 13:39:37 -04:00
|
|
|
if (kaslr_seed == 0)
|
|
|
|
kaslr_seed = __pi_get_kaslr_seed(dtb_pa);
|
2023-07-22 14:38:46 +02:00
|
|
|
/*
|
|
|
|
* Compute the number of positions available: we are limited
|
|
|
|
* by the early page table that only has one PUD and we must
|
|
|
|
* be aligned on PMD_SIZE.
|
|
|
|
*/
|
|
|
|
nr_pos = (PUD_SIZE - kernel_size) / PMD_SIZE;
|
|
|
|
|
|
|
|
kernel_map.virt_offset = (kaslr_seed % nr_pos) * PMD_SIZE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
kernel_map.virt_addr = KERNEL_LINK_ADDR + kernel_map.virt_offset;
|
2021-06-17 15:53:07 +02:00
|
|
|
|
2021-04-13 02:35:14 -04:00
|
|
|
#ifdef CONFIG_XIP_KERNEL
|
2021-06-17 15:53:07 +02:00
|
|
|
kernel_map.xiprom = (uintptr_t)CONFIG_XIP_PHYS_ADDR;
|
|
|
|
kernel_map.xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom);
|
2021-04-13 02:35:14 -04:00
|
|
|
|
2021-07-21 09:59:35 +02:00
|
|
|
phys_ram_base = CONFIG_PHYS_RAM_BASE;
|
2024-12-09 20:26:17 +08:00
|
|
|
#ifdef CONFIG_SPARSEMEM_VMEMMAP
|
|
|
|
vmemmap_start_pfn = round_down(phys_ram_base, VMEMMAP_ADDR_ALIGN) >> PAGE_SHIFT;
|
|
|
|
#endif
|
2021-06-17 15:53:07 +02:00
|
|
|
kernel_map.phys_addr = (uintptr_t)CONFIG_PHYS_RAM_BASE;
|
2024-05-08 21:19:17 +02:00
|
|
|
kernel_map.size = (uintptr_t)(&_end) - (uintptr_t)(&_start);
|
2021-04-13 02:35:14 -04:00
|
|
|
|
2024-06-07 22:22:08 +02:00
|
|
|
kernel_map.va_kernel_xip_text_pa_offset = kernel_map.virt_addr - kernel_map.xiprom;
|
|
|
|
kernel_map.va_kernel_xip_data_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr
|
|
|
|
+ (uintptr_t)&_sdata - (uintptr_t)&_start;
|
2021-04-13 02:35:14 -04:00
|
|
|
#else
|
2021-06-17 15:53:07 +02:00
|
|
|
kernel_map.phys_addr = (uintptr_t)(&_start);
|
|
|
|
kernel_map.size = (uintptr_t)(&_end) - kernel_map.phys_addr;
|
2024-06-07 22:22:08 +02:00
|
|
|
kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr;
|
2021-04-13 02:35:14 -04:00
|
|
|
#endif
|
2021-12-06 11:46:51 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
|
2023-04-24 11:23:13 +02:00
|
|
|
set_satp_mode(dtb_pa);
|
2023-09-29 21:11:58 +00:00
|
|
|
set_mmap_rnd_bits_max();
|
2021-12-06 11:46:51 +01:00
|
|
|
#endif
|
|
|
|
|
2023-03-24 16:54:21 +01:00
|
|
|
/*
|
|
|
|
* In 64-bit, we defer the setup of va_pa_offset to setup_bootmem,
|
|
|
|
* where we have the system memory layout: this allows us to align
|
|
|
|
* the physical and virtual mappings and then make use of PUD/P4D/PGD
|
|
|
|
* for the linear mapping. This is only possible because the kernel
|
|
|
|
* mapping lies outside the linear mapping.
|
|
|
|
* In 32-bit however, as the kernel resides in the linear mapping,
|
|
|
|
* setup_vm_final can not change the mapping established here,
|
|
|
|
* otherwise the same kernel addresses would get mapped to different
|
|
|
|
* physical addresses (if the start of dram is different from the
|
|
|
|
* kernel physical address start).
|
|
|
|
*/
|
|
|
|
kernel_map.va_pa_offset = IS_ENABLED(CONFIG_64BIT) ?
|
|
|
|
0UL : PAGE_OFFSET - kernel_map.phys_addr;
|
2019-02-13 16:38:36 +05:30
|
|
|
|
2024-06-24 13:17:23 +01:00
|
|
|
memory_limit = KERN_VIRT_SIZE;
|
2021-12-06 11:46:45 +01:00
|
|
|
|
2019-02-13 16:38:36 +05:30
|
|
|
/* Sanity check alignment and size */
|
|
|
|
BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
|
2021-07-23 15:01:25 +02:00
|
|
|
BUG_ON((kernel_map.phys_addr % PMD_SIZE) != 0);
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2021-06-29 11:13:48 +02:00
|
|
|
#ifdef CONFIG_64BIT
|
|
|
|
/*
|
|
|
|
* The last 4K bytes of the addressable memory can not be mapped because
|
|
|
|
* of IS_ERR_VALUE macro.
|
|
|
|
*/
|
|
|
|
BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K);
|
2020-09-17 15:37:12 -07:00
|
|
|
#endif
|
2021-06-29 11:13:48 +02:00
|
|
|
|
2023-03-29 06:53:26 +02:00
|
|
|
#ifdef CONFIG_RELOCATABLE
|
|
|
|
/*
|
|
|
|
* Early page table uses only one PUD, which makes it possible
|
|
|
|
* to map PUD_SIZE aligned on PUD_SIZE: if the relocation offset
|
|
|
|
* makes the kernel cross over a PUD_SIZE boundary, raise a bug
|
|
|
|
* since a part of the kernel would not get mapped.
|
|
|
|
*/
|
2024-10-26 10:13:57 -07:00
|
|
|
if (IS_ENABLED(CONFIG_64BIT))
|
|
|
|
BUG_ON(PUD_SIZE - (kernel_map.virt_addr & (PUD_SIZE - 1)) < kernel_map.size);
|
2023-03-29 06:53:26 +02:00
|
|
|
relocate_kernel();
|
|
|
|
#endif
|
|
|
|
|
2022-05-11 21:29:21 +02:00
|
|
|
apply_early_boot_alternatives();
|
2021-12-06 11:46:47 +01:00
|
|
|
pt_ops_set_early();
|
|
|
|
|
2019-06-28 13:36:21 -07:00
|
|
|
/* Setup early PGD for fixmap */
|
|
|
|
create_pgd_mapping(early_pg_dir, FIXADDR_START,
|
2021-12-06 11:46:51 +01:00
|
|
|
fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
|
2019-02-13 16:38:36 +05:30
|
|
|
|
|
|
|
#ifndef __PAGETABLE_PMD_FOLDED
|
2022-01-27 10:48:42 +08:00
|
|
|
/* Setup fixmap P4D and PUD */
|
|
|
|
if (pgtable_l5_enabled)
|
|
|
|
create_p4d_mapping(fixmap_p4d, FIXADDR_START,
|
|
|
|
(uintptr_t)fixmap_pud, P4D_SIZE, PAGE_TABLE);
|
2021-12-06 11:46:51 +01:00
|
|
|
/* Setup fixmap PUD and PMD */
|
|
|
|
if (pgtable_l4_enabled)
|
|
|
|
create_pud_mapping(fixmap_pud, FIXADDR_START,
|
|
|
|
(uintptr_t)fixmap_pmd, PUD_SIZE, PAGE_TABLE);
|
2019-06-28 13:36:21 -07:00
|
|
|
create_pmd_mapping(fixmap_pmd, FIXADDR_START,
|
|
|
|
(uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
|
|
|
|
/* Setup trampoline PGD and PMD */
|
2021-06-17 15:53:07 +02:00
|
|
|
create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr,
|
2021-12-06 11:46:51 +01:00
|
|
|
trampoline_pgd_next, PGDIR_SIZE, PAGE_TABLE);
|
2022-01-27 10:48:42 +08:00
|
|
|
if (pgtable_l5_enabled)
|
|
|
|
create_p4d_mapping(trampoline_p4d, kernel_map.virt_addr,
|
|
|
|
(uintptr_t)trampoline_pud, P4D_SIZE, PAGE_TABLE);
|
2021-12-06 11:46:51 +01:00
|
|
|
if (pgtable_l4_enabled)
|
|
|
|
create_pud_mapping(trampoline_pud, kernel_map.virt_addr,
|
|
|
|
(uintptr_t)trampoline_pmd, PUD_SIZE, PAGE_TABLE);
|
2021-04-13 02:35:14 -04:00
|
|
|
#ifdef CONFIG_XIP_KERNEL
|
2021-06-17 15:53:07 +02:00
|
|
|
create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr,
|
|
|
|
kernel_map.xiprom, PMD_SIZE, PAGE_KERNEL_EXEC);
|
2021-04-13 02:35:14 -04:00
|
|
|
#else
|
2021-06-17 15:53:07 +02:00
|
|
|
create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr,
|
|
|
|
kernel_map.phys_addr, PMD_SIZE, PAGE_KERNEL_EXEC);
|
2021-04-13 02:35:14 -04:00
|
|
|
#endif
|
2019-06-28 13:36:21 -07:00
|
|
|
#else
|
|
|
|
/* Setup trampoline PGD */
|
2021-06-17 15:53:07 +02:00
|
|
|
create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr,
|
|
|
|
kernel_map.phys_addr, PGDIR_SIZE, PAGE_KERNEL_EXEC);
|
2019-06-28 13:36:21 -07:00
|
|
|
#endif
|
2019-02-13 16:38:36 +05:30
|
|
|
|
2019-06-28 13:36:21 -07:00
|
|
|
/*
|
2021-04-11 12:41:44 -04:00
|
|
|
* Setup early PGD covering entire kernel which will allow
|
2019-06-28 13:36:21 -07:00
|
|
|
* us to reach paging_init(). We map all memory banks later
|
|
|
|
* in setup_vm_final() below.
|
|
|
|
*/
|
2021-07-23 15:01:25 +02:00
|
|
|
create_kernel_page_table(early_pg_dir, true);
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2021-07-23 15:01:28 +02:00
|
|
|
/* Setup early mapping for FDT early scan */
|
2023-04-26 18:00:09 +08:00
|
|
|
create_fdt_early_page_table(__fix_to_virt(FIX_FDT), dtb_pa);
|
2020-09-17 15:37:11 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
|
|
|
|
* range can not span multiple pmds.
|
|
|
|
*/
|
2021-12-06 11:46:51 +01:00
|
|
|
BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
|
2020-09-17 15:37:11 -07:00
|
|
|
!= (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
|
|
|
|
|
|
|
|
#ifndef __PAGETABLE_PMD_FOLDED
|
|
|
|
/*
|
|
|
|
* Early ioremap fixmap is already created as it lies within first 2MB
|
|
|
|
* of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END
|
|
|
|
* FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn
|
|
|
|
* the user if not.
|
|
|
|
*/
|
|
|
|
fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];
|
|
|
|
fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];
|
|
|
|
if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {
|
|
|
|
WARN_ON(1);
|
|
|
|
pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
|
|
|
|
pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));
|
|
|
|
pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
|
|
|
|
fix_to_virt(FIX_BTMAP_BEGIN));
|
|
|
|
pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
|
|
|
|
fix_to_virt(FIX_BTMAP_END));
|
|
|
|
|
|
|
|
pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
|
|
|
|
pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-06 11:46:47 +01:00
|
|
|
|
|
|
|
pt_ops_set_fixmap();
|
2019-06-28 13:36:21 -07:00
|
|
|
}
|
2019-01-07 20:57:01 +05:30
|
|
|
|
2024-06-05 13:40:46 +02:00
|
|
|
static void __meminit create_linear_mapping_range(phys_addr_t start, phys_addr_t end,
|
2024-06-05 13:40:47 +02:00
|
|
|
uintptr_t fixed_map_size, const pgprot_t *pgprot)
|
2019-06-28 13:36:21 -07:00
|
|
|
{
|
2023-03-24 16:54:20 +01:00
|
|
|
phys_addr_t pa;
|
2019-06-28 13:36:21 -07:00
|
|
|
uintptr_t va, map_size;
|
|
|
|
|
2023-03-24 16:54:20 +01:00
|
|
|
for (pa = start; pa < end; pa += map_size) {
|
|
|
|
va = (uintptr_t)__va(pa);
|
2023-06-06 15:04:44 +02:00
|
|
|
map_size = fixed_map_size ? fixed_map_size :
|
2023-06-07 14:58:51 +02:00
|
|
|
best_map_size(pa, va, end - pa);
|
2023-03-24 16:54:20 +01:00
|
|
|
|
|
|
|
create_pgd_mapping(swapper_pg_dir, va, pa, map_size,
|
2024-06-05 13:40:47 +02:00
|
|
|
pgprot ? *pgprot : pgprot_from_va(va));
|
2023-03-24 16:54:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init create_linear_mapping_page_table(void)
|
|
|
|
{
|
|
|
|
phys_addr_t start, end;
|
2023-06-06 15:04:44 +02:00
|
|
|
phys_addr_t kfence_pool __maybe_unused;
|
2020-10-13 16:58:08 -07:00
|
|
|
u64 i;
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2023-03-24 16:54:21 +01:00
|
|
|
#ifdef CONFIG_STRICT_KERNEL_RWX
|
|
|
|
phys_addr_t ktext_start = __pa_symbol(_start);
|
|
|
|
phys_addr_t ktext_size = __init_data_begin - _start;
|
|
|
|
phys_addr_t krodata_start = __pa_symbol(__start_rodata);
|
|
|
|
phys_addr_t krodata_size = _data - __start_rodata;
|
|
|
|
|
|
|
|
/* Isolate kernel text and rodata so they don't get mapped with a PUD */
|
|
|
|
memblock_mark_nomap(ktext_start, ktext_size);
|
|
|
|
memblock_mark_nomap(krodata_start, krodata_size);
|
|
|
|
#endif
|
|
|
|
|
2023-06-06 15:04:44 +02:00
|
|
|
#ifdef CONFIG_KFENCE
|
|
|
|
/*
|
|
|
|
* kfence pool must be backed by PAGE_SIZE mappings, so allocate it
|
|
|
|
* before we setup the linear mapping so that we avoid using hugepages
|
|
|
|
* for this region.
|
|
|
|
*/
|
|
|
|
kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
|
|
|
|
BUG_ON(!kfence_pool);
|
|
|
|
|
|
|
|
memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE);
|
|
|
|
__kfence_pool = __va(kfence_pool);
|
|
|
|
#endif
|
|
|
|
|
2021-04-11 12:41:44 -04:00
|
|
|
/* Map all memory banks in the linear mapping */
|
2020-10-13 16:58:08 -07:00
|
|
|
for_each_mem_range(i, &start, &end) {
|
2019-06-28 13:36:21 -07:00
|
|
|
if (start >= end)
|
|
|
|
break;
|
|
|
|
if (start <= __pa(PAGE_OFFSET) &&
|
|
|
|
__pa(PAGE_OFFSET) < end)
|
|
|
|
start = __pa(PAGE_OFFSET);
|
|
|
|
|
2024-06-05 13:40:47 +02:00
|
|
|
create_linear_mapping_range(start, end, 0, NULL);
|
2019-02-13 16:38:36 +05:30
|
|
|
}
|
2023-03-24 16:54:21 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_STRICT_KERNEL_RWX
|
2024-06-05 13:40:47 +02:00
|
|
|
create_linear_mapping_range(ktext_start, ktext_start + ktext_size, 0, NULL);
|
|
|
|
create_linear_mapping_range(krodata_start, krodata_start + krodata_size, 0, NULL);
|
2023-03-24 16:54:21 +01:00
|
|
|
|
|
|
|
memblock_clear_nomap(ktext_start, ktext_size);
|
|
|
|
memblock_clear_nomap(krodata_start, krodata_size);
|
|
|
|
#endif
|
2023-06-06 15:04:44 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_KFENCE
|
2024-06-05 13:40:47 +02:00
|
|
|
create_linear_mapping_range(kfence_pool, kfence_pool + KFENCE_POOL_SIZE, PAGE_SIZE, NULL);
|
2023-06-06 15:04:44 +02:00
|
|
|
|
|
|
|
memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE);
|
|
|
|
#endif
|
2023-03-24 16:54:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __init setup_vm_final(void)
|
|
|
|
{
|
2019-06-28 13:36:21 -07:00
|
|
|
/* Setup swapper PGD for fixmap */
|
2023-03-29 10:19:30 +02:00
|
|
|
#if !defined(CONFIG_64BIT)
|
|
|
|
/*
|
|
|
|
* In 32-bit, the device tree lies in a pgd entry, so it must be copied
|
|
|
|
* directly in swapper_pg_dir in addition to the pgd entry that points
|
|
|
|
* to fixmap_pte.
|
|
|
|
*/
|
|
|
|
unsigned long idx = pgd_index(__fix_to_virt(FIX_FDT));
|
|
|
|
|
|
|
|
set_pgd(&swapper_pg_dir[idx], early_pg_dir[idx]);
|
|
|
|
#endif
|
2019-06-28 13:36:21 -07:00
|
|
|
create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
|
2020-01-02 11:12:40 +08:00
|
|
|
__pa_symbol(fixmap_pgd_next),
|
2019-06-28 13:36:21 -07:00
|
|
|
PGDIR_SIZE, PAGE_TABLE);
|
2019-01-07 20:57:01 +05:30
|
|
|
|
2023-03-24 16:54:20 +01:00
|
|
|
/* Map the linear mapping */
|
|
|
|
create_linear_mapping_page_table();
|
2019-01-07 20:57:01 +05:30
|
|
|
|
2021-04-11 12:41:44 -04:00
|
|
|
/* Map the kernel */
|
2021-12-06 23:03:50 +08:00
|
|
|
if (IS_ENABLED(CONFIG_64BIT))
|
|
|
|
create_kernel_page_table(swapper_pg_dir, false);
|
2021-04-11 12:41:44 -04:00
|
|
|
|
2021-12-06 11:46:46 +01:00
|
|
|
#ifdef CONFIG_KASAN
|
|
|
|
kasan_swapper_init();
|
|
|
|
#endif
|
|
|
|
|
2019-06-28 13:36:21 -07:00
|
|
|
/* Clear fixmap PTE and PMD mappings */
|
|
|
|
clear_fixmap(FIX_PTE);
|
|
|
|
clear_fixmap(FIX_PMD);
|
2021-12-06 11:46:51 +01:00
|
|
|
clear_fixmap(FIX_PUD);
|
2022-01-27 10:48:42 +08:00
|
|
|
clear_fixmap(FIX_P4D);
|
2019-06-28 13:36:21 -07:00
|
|
|
|
|
|
|
/* Move to swapper page table */
|
2021-12-06 11:46:51 +01:00
|
|
|
csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode);
|
2019-06-28 13:36:21 -07:00
|
|
|
local_flush_tlb_all();
|
2020-09-17 15:37:12 -07:00
|
|
|
|
2021-12-06 11:46:47 +01:00
|
|
|
pt_ops_set_late();
|
2019-06-28 13:36:21 -07:00
|
|
|
}
|
2019-10-28 13:10:41 +01:00
|
|
|
#else
|
|
|
|
asmlinkage void __init setup_vm(uintptr_t dtb_pa)
|
|
|
|
{
|
|
|
|
dtb_early_va = (void *)dtb_pa;
|
2020-10-01 12:04:56 -07:00
|
|
|
dtb_early_pa = dtb_pa;
|
2024-10-26 10:13:55 -07:00
|
|
|
|
|
|
|
#ifdef CONFIG_RELOCATABLE
|
|
|
|
kernel_map.virt_addr = (uintptr_t)_start;
|
|
|
|
kernel_map.phys_addr = (uintptr_t)_start;
|
|
|
|
relocate_kernel();
|
|
|
|
#endif
|
2019-10-28 13:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void setup_vm_final(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MMU */
|
2019-06-28 13:36:21 -07:00
|
|
|
|
2021-04-19 03:55:38 +03:00
|
|
|
/*
|
|
|
|
* reserve_crashkernel() - reserves memory for crash kernel
|
|
|
|
*
|
|
|
|
* This function reserves memory area given in "crashkernel=" kernel command
|
|
|
|
* line parameter. The memory reserved is used by dump capture kernel when
|
|
|
|
* primary kernel is crashing.
|
|
|
|
*/
|
2023-09-14 11:31:41 +08:00
|
|
|
static void __init arch_reserve_crashkernel(void)
|
2021-04-19 03:55:38 +03:00
|
|
|
{
|
2023-09-14 11:31:41 +08:00
|
|
|
unsigned long long low_size = 0;
|
|
|
|
unsigned long long crash_base, crash_size;
|
riscv: kdump: Implement crashkernel=X,[high,low]
On riscv, the current crash kernel allocation logic is trying to
allocate within 32bit addressible memory region by default, if
failed, try to allocate without 4G restriction.
In need of saving DMA zone memory while allocating a relatively large
crash kernel region, allocating the reserved memory top down in
high memory, without overlapping the DMA zone, is a mature solution.
Here introduce the parameter option crashkernel=X,[high,low].
One can reserve the crash kernel from high memory above DMA zone range
by explicitly passing "crashkernel=X,high"; or reserve a memory range
below 4G with "crashkernel=X,low".
Signed-off-by: Chen Jiahao <chenjiahao16@huawei.com>
Acked-by: Guo Ren <guoren@kernel.org>
Acked-by: Baoquan He <bhe@redhat.com>
Link: https://lore.kernel.org/r/20230726175000.2536220-2-chenjiahao16@huawei.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2023-07-26 17:49:59 +00:00
|
|
|
bool high = false;
|
2023-09-14 11:31:41 +08:00
|
|
|
int ret;
|
2021-04-19 03:55:38 +03:00
|
|
|
|
2024-01-24 13:12:52 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_CRASH_RESERVE))
|
2022-03-23 16:06:36 -07:00
|
|
|
return;
|
2021-04-19 03:55:39 +03:00
|
|
|
|
2025-01-31 17:08:25 +05:30
|
|
|
ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
|
2023-09-14 11:31:41 +08:00
|
|
|
&crash_size, &crash_base,
|
Add a new optional ",cma" suffix to the crashkernel= command line option
Patch series "kdump: crashkernel reservation from CMA", v5.
This series implements a way to reserve additional crash kernel memory
using CMA.
Currently, all the memory for the crash kernel is not usable by the 1st
(production) kernel. It is also unmapped so that it can't be corrupted by
the fault that will eventually trigger the crash. This makes sense for
the memory actually used by the kexec-loaded crash kernel image and initrd
and the data prepared during the load (vmcoreinfo, ...). However, the
reserved space needs to be much larger than that to provide enough
run-time memory for the crash kernel and the kdump userspace. Estimating
the amount of memory to reserve is difficult. Being too careful makes
kdump likely to end in OOM, being too generous takes even more memory from
the production system. Also, the reservation only allows reserving a
single contiguous block (or two with the "low" suffix). I've seen systems
where this fails because the physical memory is fragmented.
By reserving additional crashkernel memory from CMA, the main crashkernel
reservation can be just large enough to fit the kernel and initrd image,
minimizing the memory taken away from the production system. Most of the
run-time memory for the crash kernel will be memory previously available
to userspace in the production system. As this memory is no longer
wasted, the reservation can be done with a generous margin, making kdump
more reliable. Kernel memory that we need to preserve for dumping is
normally not allocated from CMA, unless it is explicitly allocated as
movable. Currently this is only the case for memory ballooning and zswap.
Such movable memory will be missing from the vmcore. User data is
typically not dumped by makedumpfile. When dumping of user data is
intended this new CMA reservation cannot be used.
There are five patches in this series:
The first adds a new ",cma" suffix to the recenly introduced generic
crashkernel parsing code. parse_crashkernel() takes one more argument to
store the cma reservation size.
The second patch implements reserve_crashkernel_cma() which performs the
reservation. If the requested size is not available in a single range,
multiple smaller ranges will be reserved.
The third patch updates Documentation/, explicitly mentioning the
potential DMA corruption of the CMA-reserved memory.
The fourth patch adds a short delay before booting the kdump kernel,
allowing pending DMA transfers to finish.
The fifth patch enables the functionality for x86 as a proof of
concept. There are just three things every arch needs to do:
- call reserve_crashkernel_cma()
- include the CMA-reserved ranges in the physical memory map
- exclude the CMA-reserved ranges from the memory available
through /proc/vmcore by excluding them from the vmcoreinfo
PT_LOAD ranges.
Adding other architectures is easy and I can do that as soon as this
series is merged.
With this series applied, specifying
crashkernel=100M craskhernel=1G,cma
on the command line will make a standard crashkernel reservation
of 100M, where kexec will load the kernel and initrd.
An additional 1G will be reserved from CMA, still usable by the production
system. The crash kernel will have 1.1G memory available. The 100M can
be reliably predicted based on the size of the kernel and initrd.
The new cma suffix is completely optional. When no
crashkernel=size,cma is specified, everything works as before.
This patch (of 5):
Add a new cma_size parameter to parse_crashkernel(). When not NULL, call
__parse_crashkernel to parse the CMA reservation size from
"crashkernel=size,cma" and store it in cma_size.
Set cma_size to NULL in all calls to parse_crashkernel().
Link: https://lkml.kernel.org/r/aEqnxxfLZMllMC8I@dwarf.suse.cz
Link: https://lkml.kernel.org/r/aEqoQckgoTQNULnh@dwarf.suse.cz
Signed-off-by: Jiri Bohac <jbohac@suse.cz>
Cc: Baoquan He <bhe@redhat.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Donald Dutile <ddutile@redhat.com>
Cc: Michal Hocko <mhocko@suse.cz>
Cc: Philipp Rudo <prudo@redhat.com>
Cc: Pingfan Liu <piliu@redhat.com>
Cc: Tao Liu <ltao@redhat.com>
Cc: Vivek Goyal <vgoyal@redhat.com>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2025-06-12 12:13:21 +02:00
|
|
|
&low_size, NULL, &high);
|
2023-09-14 11:31:41 +08:00
|
|
|
if (ret)
|
2021-04-19 03:55:38 +03:00
|
|
|
return;
|
|
|
|
|
2025-01-31 17:08:25 +05:30
|
|
|
reserve_crashkernel_generic(crash_size, crash_base, low_size, high);
|
2021-04-19 03:55:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-28 13:36:21 -07:00
|
|
|
void __init paging_init(void)
|
|
|
|
{
|
2021-05-10 19:42:22 +08:00
|
|
|
setup_bootmem();
|
2019-06-28 13:36:21 -07:00
|
|
|
setup_vm_final();
|
2023-06-14 21:19:07 +08:00
|
|
|
|
|
|
|
/* Depend on that Linear Mapping is ready */
|
|
|
|
memblock_allow_resize();
|
2020-11-18 16:38:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __init misc_mem_init(void)
|
|
|
|
{
|
2021-02-25 14:54:17 +08:00
|
|
|
early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
|
2020-11-18 16:38:29 -08:00
|
|
|
arch_numa_init();
|
2020-11-18 16:38:27 -08:00
|
|
|
sparse_init();
|
2024-01-17 22:03:33 +08:00
|
|
|
#ifdef CONFIG_SPARSEMEM_VMEMMAP
|
|
|
|
/* The entire VMEMMAP region has been populated. Flush TLB for this region */
|
|
|
|
local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END);
|
|
|
|
#endif
|
2019-06-28 13:36:21 -07:00
|
|
|
zone_sizes_init();
|
2023-09-14 11:31:41 +08:00
|
|
|
arch_reserve_crashkernel();
|
2020-11-18 16:38:29 -08:00
|
|
|
memblock_dump_all();
|
2019-02-13 16:38:36 +05:30
|
|
|
}
|
2019-08-28 15:40:54 -06:00
|
|
|
|
2019-10-23 11:23:02 +08:00
|
|
|
#ifdef CONFIG_SPARSEMEM_VMEMMAP
|
2023-12-14 14:29:35 +01:00
|
|
|
void __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node,
|
|
|
|
unsigned long addr, unsigned long next)
|
|
|
|
{
|
|
|
|
pmd_set_huge(pmd, virt_to_phys(p), PAGE_KERNEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node,
|
|
|
|
unsigned long addr, unsigned long next)
|
|
|
|
{
|
|
|
|
vmemmap_verify((pte_t *)pmdp, node, addr, next);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-08-28 15:40:54 -06:00
|
|
|
int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
|
|
|
|
struct vmem_altmap *altmap)
|
|
|
|
{
|
2023-12-14 14:29:35 +01:00
|
|
|
/*
|
|
|
|
* Note that SPARSEMEM_VMEMMAP is only selected for rv64 and that we
|
|
|
|
* can't use hugepage mappings for 2-level page table because in case of
|
|
|
|
* memory hotplug, we are not able to update all the page tables with
|
|
|
|
* the new PMDs.
|
|
|
|
*/
|
2024-06-05 13:40:44 +02:00
|
|
|
return vmemmap_populate_hugepages(start, end, node, altmap);
|
2019-08-28 15:40:54 -06:00
|
|
|
}
|
|
|
|
#endif
|
2023-05-31 11:38:17 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
|
|
|
|
/*
|
|
|
|
* Pre-allocates page-table pages for a specific area in the kernel
|
|
|
|
* page-table. Only the level which needs to be synchronized between
|
|
|
|
* all page-tables is allocated because the synchronization can be
|
|
|
|
* expensive.
|
|
|
|
*/
|
|
|
|
static void __init preallocate_pgd_pages_range(unsigned long start, unsigned long end,
|
|
|
|
const char *area)
|
|
|
|
{
|
|
|
|
unsigned long addr;
|
|
|
|
const char *lvl;
|
|
|
|
|
|
|
|
for (addr = start; addr < end && addr >= start; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
|
|
|
|
pgd_t *pgd = pgd_offset_k(addr);
|
|
|
|
p4d_t *p4d;
|
|
|
|
pud_t *pud;
|
|
|
|
pmd_t *pmd;
|
|
|
|
|
|
|
|
lvl = "p4d";
|
|
|
|
p4d = p4d_alloc(&init_mm, pgd, addr);
|
|
|
|
if (!p4d)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
if (pgtable_l5_enabled)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
lvl = "pud";
|
|
|
|
pud = pud_alloc(&init_mm, p4d, addr);
|
|
|
|
if (!pud)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
if (pgtable_l4_enabled)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
lvl = "pmd";
|
|
|
|
pmd = pmd_alloc(&init_mm, pud, addr);
|
|
|
|
if (!pmd)
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
/*
|
|
|
|
* The pages have to be there now or they will be missing in
|
|
|
|
* process page-tables later.
|
|
|
|
*/
|
|
|
|
panic("Failed to pre-allocate %s pages for %s area\n", lvl, area);
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:40:45 +02:00
|
|
|
#define PAGE_END KASAN_SHADOW_START
|
|
|
|
|
2023-05-31 11:38:17 +02:00
|
|
|
void __init pgtable_cache_init(void)
|
|
|
|
{
|
|
|
|
preallocate_pgd_pages_range(VMALLOC_START, VMALLOC_END, "vmalloc");
|
|
|
|
if (IS_ENABLED(CONFIG_MODULES))
|
|
|
|
preallocate_pgd_pages_range(MODULES_VADDR, MODULES_END, "bpf/modules");
|
2024-06-05 13:40:45 +02:00
|
|
|
if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) {
|
|
|
|
preallocate_pgd_pages_range(VMEMMAP_START, VMEMMAP_END, "vmemmap");
|
|
|
|
preallocate_pgd_pages_range(PAGE_OFFSET, PAGE_END, "direct map");
|
|
|
|
if (IS_ENABLED(CONFIG_KASAN))
|
|
|
|
preallocate_pgd_pages_range(KASAN_SHADOW_START, KASAN_SHADOW_END, "kasan");
|
|
|
|
}
|
2023-05-31 11:38:17 +02:00
|
|
|
}
|
|
|
|
#endif
|
2024-05-05 19:06:24 +03:00
|
|
|
|
|
|
|
#ifdef CONFIG_EXECMEM
|
|
|
|
#ifdef CONFIG_MMU
|
|
|
|
static struct execmem_info execmem_info __ro_after_init;
|
|
|
|
|
|
|
|
struct execmem_info __init *execmem_arch_setup(void)
|
|
|
|
{
|
|
|
|
execmem_info = (struct execmem_info){
|
|
|
|
.ranges = {
|
|
|
|
[EXECMEM_DEFAULT] = {
|
|
|
|
.start = MODULES_VADDR,
|
|
|
|
.end = MODULES_END,
|
|
|
|
.pgprot = PAGE_KERNEL,
|
|
|
|
.alignment = 1,
|
|
|
|
},
|
|
|
|
[EXECMEM_KPROBES] = {
|
|
|
|
.start = VMALLOC_START,
|
|
|
|
.end = VMALLOC_END,
|
|
|
|
.pgprot = PAGE_KERNEL_READ_EXEC,
|
|
|
|
.alignment = 1,
|
|
|
|
},
|
|
|
|
[EXECMEM_BPF] = {
|
|
|
|
.start = BPF_JIT_REGION_START,
|
|
|
|
.end = BPF_JIT_REGION_END,
|
|
|
|
.pgprot = PAGE_KERNEL,
|
|
|
|
.alignment = PAGE_SIZE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return &execmem_info;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MMU */
|
|
|
|
#endif /* CONFIG_EXECMEM */
|
2024-06-05 13:40:49 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_MEMORY_HOTPLUG
|
|
|
|
static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
|
|
|
|
{
|
|
|
|
struct page *page = pmd_page(*pmd);
|
|
|
|
struct ptdesc *ptdesc = page_ptdesc(page);
|
|
|
|
pte_t *pte;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PTRS_PER_PTE; i++) {
|
|
|
|
pte = pte_start + i;
|
|
|
|
if (!pte_none(*pte))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-08 14:57:23 +08:00
|
|
|
pagetable_dtor(ptdesc);
|
2024-06-05 13:40:49 +02:00
|
|
|
if (PageReserved(page))
|
|
|
|
free_reserved_page(page);
|
|
|
|
else
|
|
|
|
pagetable_free(ptdesc);
|
|
|
|
pmd_clear(pmd);
|
|
|
|
}
|
|
|
|
|
riscv: mm: Do not call pmd dtor on vmemmap page table teardown
The vmemmap's, which is used for RV64 with SPARSEMEM_VMEMMAP, page
tables are populated using pmd (page middle directory) hugetables.
However, the pmd allocation is not using the generic mechanism used by
the VMA code (e.g. pmd_alloc()), or the RISC-V specific
create_pgd_mapping()/alloc_pmd_late(). Instead, the vmemmap page table
code allocates a page, and calls vmemmap_set_pmd(). This results in
that the pmd ctor is *not* called, nor would it make sense to do so.
Now, when tearing down a vmemmap page table pmd, the cleanup code
would unconditionally, and incorrectly call the pmd dtor, which
results in a crash (best case).
This issue was found when running the HMM selftests:
| tools/testing/selftests/mm# ./test_hmm.sh smoke
| ... # when unloading the test_hmm.ko module
| page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x10915b
| flags: 0x1000000000000000(node=0|zone=1)
| raw: 1000000000000000 0000000000000000 dead000000000122 0000000000000000
| raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000
| page dumped because: VM_BUG_ON_PAGE(ptdesc->pmd_huge_pte)
| ------------[ cut here ]------------
| kernel BUG at include/linux/mm.h:3080!
| Kernel BUG [#1]
| Modules linked in: test_hmm(-) sch_fq_codel fuse drm drm_panel_orientation_quirks backlight dm_mod
| CPU: 1 UID: 0 PID: 514 Comm: modprobe Tainted: G W 6.12.0-00982-gf2a4f1682d07 #2
| Tainted: [W]=WARN
| Hardware name: riscv-virtio qemu/qemu, BIOS 2024.10 10/01/2024
| epc : remove_pgd_mapping+0xbec/0x1070
| ra : remove_pgd_mapping+0xbec/0x1070
| epc : ffffffff80010a68 ra : ffffffff80010a68 sp : ff20000000a73940
| gp : ffffffff827b2d88 tp : ff6000008785da40 t0 : ffffffff80fbce04
| t1 : 0720072007200720 t2 : 706d756420656761 s0 : ff20000000a73a50
| s1 : ff6000008915cff8 a0 : 0000000000000039 a1 : 0000000000000008
| a2 : ff600003fff0de20 a3 : 0000000000000000 a4 : 0000000000000000
| a5 : 0000000000000000 a6 : c0000000ffffefff a7 : ffffffff824469b8
| s2 : ff1c0000022456c0 s3 : ff1ffffffdbfffff s4 : ff6000008915c000
| s5 : ff6000008915c000 s6 : ff6000008915c000 s7 : ff1ffffffdc00000
| s8 : 0000000000000001 s9 : ff1ffffffdc00000 s10: ffffffff819a31f0
| s11: ffffffffffffffff t3 : ffffffff8000c950 t4 : ff60000080244f00
| t5 : ff60000080244000 t6 : ff20000000a73708
| status: 0000000200000120 badaddr: ffffffff80010a68 cause: 0000000000000003
| [<ffffffff80010a68>] remove_pgd_mapping+0xbec/0x1070
| [<ffffffff80fd238e>] vmemmap_free+0x14/0x1e
| [<ffffffff8032e698>] section_deactivate+0x220/0x452
| [<ffffffff8032ef7e>] sparse_remove_section+0x4a/0x58
| [<ffffffff802f8700>] __remove_pages+0x7e/0xba
| [<ffffffff803760d8>] memunmap_pages+0x2bc/0x3fe
| [<ffffffff02a3ca28>] dmirror_device_remove_chunks+0x2ea/0x518 [test_hmm]
| [<ffffffff02a3e026>] hmm_dmirror_exit+0x3e/0x1018 [test_hmm]
| [<ffffffff80102c14>] __riscv_sys_delete_module+0x15a/0x2a6
| [<ffffffff80fd020c>] do_trap_ecall_u+0x1f2/0x266
| [<ffffffff80fde0a2>] _new_vmalloc_restore_context_a0+0xc6/0xd2
| Code: bf51 7597 0184 8593 76a5 854a 4097 0029 80e7 2c00 (9002) 7597
| ---[ end trace 0000000000000000 ]---
| Kernel panic - not syncing: Fatal exception in interrupt
Add a check to avoid calling the pmd dtor, if the calling context is
vmemmap_free().
Fixes: c75a74f4ba19 ("riscv: mm: Add memory hotplugging support")
Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20241120131203.1859787-1-bjorn@kernel.org
Cc: stable@vger.kernel.org
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2024-11-20 14:12:02 +01:00
|
|
|
static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud, bool is_vmemmap)
|
2024-06-05 13:40:49 +02:00
|
|
|
{
|
|
|
|
struct page *page = pud_page(*pud);
|
|
|
|
struct ptdesc *ptdesc = page_ptdesc(page);
|
|
|
|
pmd_t *pmd;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PTRS_PER_PMD; i++) {
|
|
|
|
pmd = pmd_start + i;
|
|
|
|
if (!pmd_none(*pmd))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
riscv: mm: Do not call pmd dtor on vmemmap page table teardown
The vmemmap's, which is used for RV64 with SPARSEMEM_VMEMMAP, page
tables are populated using pmd (page middle directory) hugetables.
However, the pmd allocation is not using the generic mechanism used by
the VMA code (e.g. pmd_alloc()), or the RISC-V specific
create_pgd_mapping()/alloc_pmd_late(). Instead, the vmemmap page table
code allocates a page, and calls vmemmap_set_pmd(). This results in
that the pmd ctor is *not* called, nor would it make sense to do so.
Now, when tearing down a vmemmap page table pmd, the cleanup code
would unconditionally, and incorrectly call the pmd dtor, which
results in a crash (best case).
This issue was found when running the HMM selftests:
| tools/testing/selftests/mm# ./test_hmm.sh smoke
| ... # when unloading the test_hmm.ko module
| page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x10915b
| flags: 0x1000000000000000(node=0|zone=1)
| raw: 1000000000000000 0000000000000000 dead000000000122 0000000000000000
| raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000
| page dumped because: VM_BUG_ON_PAGE(ptdesc->pmd_huge_pte)
| ------------[ cut here ]------------
| kernel BUG at include/linux/mm.h:3080!
| Kernel BUG [#1]
| Modules linked in: test_hmm(-) sch_fq_codel fuse drm drm_panel_orientation_quirks backlight dm_mod
| CPU: 1 UID: 0 PID: 514 Comm: modprobe Tainted: G W 6.12.0-00982-gf2a4f1682d07 #2
| Tainted: [W]=WARN
| Hardware name: riscv-virtio qemu/qemu, BIOS 2024.10 10/01/2024
| epc : remove_pgd_mapping+0xbec/0x1070
| ra : remove_pgd_mapping+0xbec/0x1070
| epc : ffffffff80010a68 ra : ffffffff80010a68 sp : ff20000000a73940
| gp : ffffffff827b2d88 tp : ff6000008785da40 t0 : ffffffff80fbce04
| t1 : 0720072007200720 t2 : 706d756420656761 s0 : ff20000000a73a50
| s1 : ff6000008915cff8 a0 : 0000000000000039 a1 : 0000000000000008
| a2 : ff600003fff0de20 a3 : 0000000000000000 a4 : 0000000000000000
| a5 : 0000000000000000 a6 : c0000000ffffefff a7 : ffffffff824469b8
| s2 : ff1c0000022456c0 s3 : ff1ffffffdbfffff s4 : ff6000008915c000
| s5 : ff6000008915c000 s6 : ff6000008915c000 s7 : ff1ffffffdc00000
| s8 : 0000000000000001 s9 : ff1ffffffdc00000 s10: ffffffff819a31f0
| s11: ffffffffffffffff t3 : ffffffff8000c950 t4 : ff60000080244f00
| t5 : ff60000080244000 t6 : ff20000000a73708
| status: 0000000200000120 badaddr: ffffffff80010a68 cause: 0000000000000003
| [<ffffffff80010a68>] remove_pgd_mapping+0xbec/0x1070
| [<ffffffff80fd238e>] vmemmap_free+0x14/0x1e
| [<ffffffff8032e698>] section_deactivate+0x220/0x452
| [<ffffffff8032ef7e>] sparse_remove_section+0x4a/0x58
| [<ffffffff802f8700>] __remove_pages+0x7e/0xba
| [<ffffffff803760d8>] memunmap_pages+0x2bc/0x3fe
| [<ffffffff02a3ca28>] dmirror_device_remove_chunks+0x2ea/0x518 [test_hmm]
| [<ffffffff02a3e026>] hmm_dmirror_exit+0x3e/0x1018 [test_hmm]
| [<ffffffff80102c14>] __riscv_sys_delete_module+0x15a/0x2a6
| [<ffffffff80fd020c>] do_trap_ecall_u+0x1f2/0x266
| [<ffffffff80fde0a2>] _new_vmalloc_restore_context_a0+0xc6/0xd2
| Code: bf51 7597 0184 8593 76a5 854a 4097 0029 80e7 2c00 (9002) 7597
| ---[ end trace 0000000000000000 ]---
| Kernel panic - not syncing: Fatal exception in interrupt
Add a check to avoid calling the pmd dtor, if the calling context is
vmemmap_free().
Fixes: c75a74f4ba19 ("riscv: mm: Add memory hotplugging support")
Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20241120131203.1859787-1-bjorn@kernel.org
Cc: stable@vger.kernel.org
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2024-11-20 14:12:02 +01:00
|
|
|
if (!is_vmemmap)
|
2025-01-08 14:57:23 +08:00
|
|
|
pagetable_dtor(ptdesc);
|
2024-06-05 13:40:49 +02:00
|
|
|
if (PageReserved(page))
|
|
|
|
free_reserved_page(page);
|
|
|
|
else
|
|
|
|
pagetable_free(ptdesc);
|
|
|
|
pud_clear(pud);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d)
|
|
|
|
{
|
|
|
|
struct page *page = p4d_page(*p4d);
|
|
|
|
pud_t *pud;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < PTRS_PER_PUD; i++) {
|
|
|
|
pud = pud_start + i;
|
|
|
|
if (!pud_none(*pud))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PageReserved(page))
|
|
|
|
free_reserved_page(page);
|
|
|
|
else
|
|
|
|
free_pages((unsigned long)page_address(page), 0);
|
|
|
|
p4d_clear(p4d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __meminit free_vmemmap_storage(struct page *page, size_t size,
|
|
|
|
struct vmem_altmap *altmap)
|
|
|
|
{
|
|
|
|
int order = get_order(size);
|
|
|
|
|
|
|
|
if (altmap) {
|
|
|
|
vmem_altmap_free(altmap, size >> PAGE_SHIFT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PageReserved(page)) {
|
|
|
|
unsigned int nr_pages = 1 << order;
|
|
|
|
|
|
|
|
while (nr_pages--)
|
|
|
|
free_reserved_page(page++);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free_pages((unsigned long)page_address(page), order);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __meminit remove_pte_mapping(pte_t *pte_base, unsigned long addr, unsigned long end,
|
|
|
|
bool is_vmemmap, struct vmem_altmap *altmap)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
|
|
|
pte_t *ptep, pte;
|
|
|
|
|
|
|
|
for (; addr < end; addr = next) {
|
|
|
|
next = (addr + PAGE_SIZE) & PAGE_MASK;
|
|
|
|
if (next > end)
|
|
|
|
next = end;
|
|
|
|
|
|
|
|
ptep = pte_base + pte_index(addr);
|
|
|
|
pte = ptep_get(ptep);
|
|
|
|
if (!pte_present(*ptep))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pte_clear(&init_mm, addr, ptep);
|
|
|
|
if (is_vmemmap)
|
|
|
|
free_vmemmap_storage(pte_page(pte), PAGE_SIZE, altmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __meminit remove_pmd_mapping(pmd_t *pmd_base, unsigned long addr, unsigned long end,
|
|
|
|
bool is_vmemmap, struct vmem_altmap *altmap)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
|
|
|
pte_t *pte_base;
|
|
|
|
pmd_t *pmdp, pmd;
|
|
|
|
|
|
|
|
for (; addr < end; addr = next) {
|
|
|
|
next = pmd_addr_end(addr, end);
|
|
|
|
pmdp = pmd_base + pmd_index(addr);
|
|
|
|
pmd = pmdp_get(pmdp);
|
|
|
|
if (!pmd_present(pmd))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pmd_leaf(pmd)) {
|
|
|
|
pmd_clear(pmdp);
|
|
|
|
if (is_vmemmap)
|
|
|
|
free_vmemmap_storage(pmd_page(pmd), PMD_SIZE, altmap);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pte_base = (pte_t *)pmd_page_vaddr(*pmdp);
|
|
|
|
remove_pte_mapping(pte_base, addr, next, is_vmemmap, altmap);
|
|
|
|
free_pte_table(pte_base, pmdp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __meminit remove_pud_mapping(pud_t *pud_base, unsigned long addr, unsigned long end,
|
|
|
|
bool is_vmemmap, struct vmem_altmap *altmap)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
|
|
|
pud_t *pudp, pud;
|
|
|
|
pmd_t *pmd_base;
|
|
|
|
|
|
|
|
for (; addr < end; addr = next) {
|
|
|
|
next = pud_addr_end(addr, end);
|
|
|
|
pudp = pud_base + pud_index(addr);
|
|
|
|
pud = pudp_get(pudp);
|
|
|
|
if (!pud_present(pud))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pud_leaf(pud)) {
|
|
|
|
if (pgtable_l4_enabled) {
|
|
|
|
pud_clear(pudp);
|
|
|
|
if (is_vmemmap)
|
|
|
|
free_vmemmap_storage(pud_page(pud), PUD_SIZE, altmap);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pmd_base = pmd_offset(pudp, 0);
|
|
|
|
remove_pmd_mapping(pmd_base, addr, next, is_vmemmap, altmap);
|
|
|
|
|
|
|
|
if (pgtable_l4_enabled)
|
riscv: mm: Do not call pmd dtor on vmemmap page table teardown
The vmemmap's, which is used for RV64 with SPARSEMEM_VMEMMAP, page
tables are populated using pmd (page middle directory) hugetables.
However, the pmd allocation is not using the generic mechanism used by
the VMA code (e.g. pmd_alloc()), or the RISC-V specific
create_pgd_mapping()/alloc_pmd_late(). Instead, the vmemmap page table
code allocates a page, and calls vmemmap_set_pmd(). This results in
that the pmd ctor is *not* called, nor would it make sense to do so.
Now, when tearing down a vmemmap page table pmd, the cleanup code
would unconditionally, and incorrectly call the pmd dtor, which
results in a crash (best case).
This issue was found when running the HMM selftests:
| tools/testing/selftests/mm# ./test_hmm.sh smoke
| ... # when unloading the test_hmm.ko module
| page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x10915b
| flags: 0x1000000000000000(node=0|zone=1)
| raw: 1000000000000000 0000000000000000 dead000000000122 0000000000000000
| raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000
| page dumped because: VM_BUG_ON_PAGE(ptdesc->pmd_huge_pte)
| ------------[ cut here ]------------
| kernel BUG at include/linux/mm.h:3080!
| Kernel BUG [#1]
| Modules linked in: test_hmm(-) sch_fq_codel fuse drm drm_panel_orientation_quirks backlight dm_mod
| CPU: 1 UID: 0 PID: 514 Comm: modprobe Tainted: G W 6.12.0-00982-gf2a4f1682d07 #2
| Tainted: [W]=WARN
| Hardware name: riscv-virtio qemu/qemu, BIOS 2024.10 10/01/2024
| epc : remove_pgd_mapping+0xbec/0x1070
| ra : remove_pgd_mapping+0xbec/0x1070
| epc : ffffffff80010a68 ra : ffffffff80010a68 sp : ff20000000a73940
| gp : ffffffff827b2d88 tp : ff6000008785da40 t0 : ffffffff80fbce04
| t1 : 0720072007200720 t2 : 706d756420656761 s0 : ff20000000a73a50
| s1 : ff6000008915cff8 a0 : 0000000000000039 a1 : 0000000000000008
| a2 : ff600003fff0de20 a3 : 0000000000000000 a4 : 0000000000000000
| a5 : 0000000000000000 a6 : c0000000ffffefff a7 : ffffffff824469b8
| s2 : ff1c0000022456c0 s3 : ff1ffffffdbfffff s4 : ff6000008915c000
| s5 : ff6000008915c000 s6 : ff6000008915c000 s7 : ff1ffffffdc00000
| s8 : 0000000000000001 s9 : ff1ffffffdc00000 s10: ffffffff819a31f0
| s11: ffffffffffffffff t3 : ffffffff8000c950 t4 : ff60000080244f00
| t5 : ff60000080244000 t6 : ff20000000a73708
| status: 0000000200000120 badaddr: ffffffff80010a68 cause: 0000000000000003
| [<ffffffff80010a68>] remove_pgd_mapping+0xbec/0x1070
| [<ffffffff80fd238e>] vmemmap_free+0x14/0x1e
| [<ffffffff8032e698>] section_deactivate+0x220/0x452
| [<ffffffff8032ef7e>] sparse_remove_section+0x4a/0x58
| [<ffffffff802f8700>] __remove_pages+0x7e/0xba
| [<ffffffff803760d8>] memunmap_pages+0x2bc/0x3fe
| [<ffffffff02a3ca28>] dmirror_device_remove_chunks+0x2ea/0x518 [test_hmm]
| [<ffffffff02a3e026>] hmm_dmirror_exit+0x3e/0x1018 [test_hmm]
| [<ffffffff80102c14>] __riscv_sys_delete_module+0x15a/0x2a6
| [<ffffffff80fd020c>] do_trap_ecall_u+0x1f2/0x266
| [<ffffffff80fde0a2>] _new_vmalloc_restore_context_a0+0xc6/0xd2
| Code: bf51 7597 0184 8593 76a5 854a 4097 0029 80e7 2c00 (9002) 7597
| ---[ end trace 0000000000000000 ]---
| Kernel panic - not syncing: Fatal exception in interrupt
Add a check to avoid calling the pmd dtor, if the calling context is
vmemmap_free().
Fixes: c75a74f4ba19 ("riscv: mm: Add memory hotplugging support")
Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20241120131203.1859787-1-bjorn@kernel.org
Cc: stable@vger.kernel.org
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2024-11-20 14:12:02 +01:00
|
|
|
free_pmd_table(pmd_base, pudp, is_vmemmap);
|
2024-06-05 13:40:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __meminit remove_p4d_mapping(p4d_t *p4d_base, unsigned long addr, unsigned long end,
|
|
|
|
bool is_vmemmap, struct vmem_altmap *altmap)
|
|
|
|
{
|
|
|
|
unsigned long next;
|
|
|
|
p4d_t *p4dp, p4d;
|
|
|
|
pud_t *pud_base;
|
|
|
|
|
|
|
|
for (; addr < end; addr = next) {
|
|
|
|
next = p4d_addr_end(addr, end);
|
|
|
|
p4dp = p4d_base + p4d_index(addr);
|
|
|
|
p4d = p4dp_get(p4dp);
|
|
|
|
if (!p4d_present(p4d))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (p4d_leaf(p4d)) {
|
|
|
|
if (pgtable_l5_enabled) {
|
|
|
|
p4d_clear(p4dp);
|
|
|
|
if (is_vmemmap)
|
|
|
|
free_vmemmap_storage(p4d_page(p4d), P4D_SIZE, altmap);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pud_base = pud_offset(p4dp, 0);
|
|
|
|
remove_pud_mapping(pud_base, addr, next, is_vmemmap, altmap);
|
|
|
|
|
|
|
|
if (pgtable_l5_enabled)
|
|
|
|
free_pud_table(pud_base, p4dp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __meminit remove_pgd_mapping(unsigned long va, unsigned long end, bool is_vmemmap,
|
|
|
|
struct vmem_altmap *altmap)
|
|
|
|
{
|
|
|
|
unsigned long addr, next;
|
|
|
|
p4d_t *p4d_base;
|
|
|
|
pgd_t *pgd;
|
|
|
|
|
|
|
|
for (addr = va; addr < end; addr = next) {
|
|
|
|
next = pgd_addr_end(addr, end);
|
|
|
|
pgd = pgd_offset_k(addr);
|
|
|
|
|
|
|
|
if (!pgd_present(*pgd))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pgd_leaf(*pgd))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
p4d_base = p4d_offset(pgd, 0);
|
|
|
|
remove_p4d_mapping(p4d_base, addr, next, is_vmemmap, altmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
flush_tlb_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __meminit remove_linear_mapping(phys_addr_t start, u64 size)
|
|
|
|
{
|
|
|
|
unsigned long va = (unsigned long)__va(start);
|
|
|
|
unsigned long end = (unsigned long)__va(start + size);
|
|
|
|
|
|
|
|
remove_pgd_mapping(va, end, false, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct range arch_get_mappable_range(void)
|
|
|
|
{
|
|
|
|
struct range mhp_range;
|
|
|
|
|
|
|
|
mhp_range.start = __pa(PAGE_OFFSET);
|
|
|
|
mhp_range.end = __pa(PAGE_END - 1);
|
|
|
|
return mhp_range;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __ref arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
create_linear_mapping_range(start, start + size, 0, ¶ms->pgprot);
|
|
|
|
ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT, params);
|
|
|
|
if (ret) {
|
|
|
|
remove_linear_mapping(start, size);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_pfn = PFN_UP(start + size);
|
|
|
|
max_low_pfn = max_pfn;
|
|
|
|
|
|
|
|
out:
|
|
|
|
flush_tlb_all();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
|
|
|
|
{
|
|
|
|
__remove_pages(start >> PAGE_SHIFT, size >> PAGE_SHIFT, altmap);
|
|
|
|
remove_linear_mapping(start, size);
|
|
|
|
flush_tlb_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ref vmemmap_free(unsigned long start, unsigned long end, struct vmem_altmap *altmap)
|
|
|
|
{
|
|
|
|
remove_pgd_mapping(start, end, true, altmap);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MEMORY_HOTPLUG */
|