Skip to content

Commit 60a6707

Browse files
Merge patch series "riscv: Memory Hot(Un)Plug support"
Björn Töpel <[email protected]> says: From: Björn Töpel <[email protected]> ================================================================ Memory Hot(Un)Plug support (and ZONE_DEVICE) for the RISC-V port ================================================================ Introduction ============ To quote "Documentation/admin-guide/mm/memory-hotplug.rst": "Memory hot(un)plug allows for increasing and decreasing the size of physical memory available to a machine at runtime." This series adds memory hot(un)plugging, and ZONE_DEVICE support for the RISC-V Linux port. MM configuration ================ RISC-V MM has the following configuration: * Memory blocks are 128M, analogous to x86-64. It uses PMD ("hugepage") vmemmaps. From that follows that 2M (PMD) worth of vmemmap spans 32768 pages á 4K which gets us 128M. * The pageblock size is the minimum minimum virtio_mem size, and on RISC-V it's 2M (2^9 * 4K). Implementation ============== The PGD table on RISC-V is shared/copied between for all processes. To avoid doing page table synchronization, the first patch (patch 1) pre-allocated the PGD entries for vmemmap/direct map. By doing that the init_mm PGD will be fixed at kernel init, and synchronization can be avoided all together. The following two patches (patch 2-3) does some preparations, followed by the actual MHP implementation (patch 4-5). Then, MHP and virtio-mem are enabled (patch 6-7), and finally ZONE_DEVICE support is added (patch 8). MHP and locking =============== TL;DR: The MHP does not step on any toes, except for ptdump. Additional locking is required for ptdump. Long version: For v2 I spent some time digging into init_mm synchronization/update. Here are my findings, and I'd love them to be corrected if incorrect. It's been a gnarly path... The `init_mm` structure is a special mm (perhaps not a "real" one). It's a "lazy context" that tracks kernel page table resources, e.g., the kernel page table (swapper_pg_dir), a kernel page_table_lock (more about the usage below), mmap_lock, and such. `init_mm` does not track/contain any VMAs. Having the `init_mm` is convenient, so that the regular kernel page table walk/modify functions can be used. Now, `init_mm` being special means that the locking for kernel page tables are special as well. On RISC-V the PGD (top-level page table structure), similar to x86, is shared (copied) with user processes. If the kernel PGD is modified, it has to be synched to user-mode processes PGDs. This is avoided by pre-populating the PGD, so it'll be fixed from boot. The in-kernel pgd regions are documented in `Documentation/arch/riscv/vm-layout.rst`. The distinct regions are: * vmemmap * vmalloc/ioremap space * direct mapping of all physical memory * kasan * modules, BPF * kernel Memory hotplug is the process of adding/removing memory to/from the kernel. Adding is done in two phases: 1. Add the memory to the kernel 2. Online memory, making it available to the page allocator. Step 1 is partially architecture dependent, and updates the init_mm page table: * Update the direct map page tables. The direct map is a linear map, representing all physical memory: `virt = phys + PAGE_OFFSET` * Add a `struct page` for each added page of memory. Update the vmemmap (virtual mapping to the `struct page`, so we can easily transform a kernel virtual address to a `struct page *` address. From an MHP perspective, there are two regions of the PGD that are updated: * vmemmap * direct mapping of all physical memory The `struct mm_struct` has a couple of locks in play: * `spinlock_t page_table_lock` protects the page table, and some counters * `struct rw_semaphore mmap_lock` protect an mm's VMAs Note again that `init_mm` does not contain any VMAs, but still uses the mmap_lock in some places. The `page_table_lock` was originally used to to protect all pages tables, but more recently a split page table lock has been introduced. The split lock has a per-table lock for the PTE and PMD tables. If split lock is disabled, all tables are guarded by `mm->page_table_lock` (for user processes). Split page table locks are not used for init_mm. MHP operations is typically synchronized using `DEFINE_STATIC_PERCPU_RWSEM(mem_hotplug_lock)`. Actors ------ The following non-MHP actors in the kernel traverses (read), and/or modifies the kernel PGD. * `ptdump` Walks the entire `init_mm`, via `ptdump_walk_pgd()` with the `mmap_write_lock(init_mm)` taken. Observation: ptdump can race with MHP, and needs additional locking to avoid crashes/races. * `set_direct_*` / `arch/riscv/mm/pageattr.c` The `set_direct_*` functionality is used to "synchronize" the direct map to other kernel mappings, e.g. modules/kernel text. The direct map is using "as large huge table mappings as possible", which means that the `set_direct_*` might need to split the direct map. The `set_direct_*` functions operates with the `mmap_write_lock(init_mm)` taken. Observation: `set_direct_*` uses the direct map, but will never modify the same entry as MHP. If there is a mapping, that entry will never race with MHP. Further, MHP acts when memory is offline. * HVO / `mm/hugetlb_vmemmap` HVO optimizes the backing `struct page` for hugetlb pages, which means changing the "vmemmap" region. HVO can split (merge?) a vmemmap pmd. However, it will never race with MHP, since HVO only operates at online memory. HVO cannot touch memory being MHP added or removed. * `apply_to_page_range` Walks a range, creates pages and applies a callback (setting permissions) for the page. When creating a table, it might use `int __pte_alloc_kernel(pmd_t *pmd)` which takes the `init_mm.page_table_lock` to synchronize pmd populate. Used by: `mm/vmalloc.c` and `mm/kasan/shadow.c`. The KASAN callback takes the `init_mm.page_table_lock` to synchronize pte creation. Observations: `apply_to_page_range` applies to the "vmalloc/ioremap space" region, and "kasan" region. *Not* affected by MHP. * `apply_to_existing_page_range` Walks a range, applies a callback (setting permissions) for the page (no page creation). Used by: `kernel/bpf/arena.c` and `mm/kasan/shadow.c`. The KASAN callback takes the `init_mm.page_table_lock` to synchronize pte creation. *Not* affected by MHP regions. * `apply_to_existing_page_range` applies to the "vmalloc/ioremap space" region, and "kasan" region. *Not* affected by MHP regions. * `ioremap_page_range` and `vmap_page_range` Uses the same internal function, and might create table entries at the "vmalloc/ioremap space" region. Can call `__pte_alloc_kernel()` which takes the `init_mm.page_table_lock` synchronizing pmd populate in the region. *Not* affected by MHP regions. Summary: * MHP add will never modify the same page table entries, as any of the other actors. * MHP remove is done when memory is offlined, and will not clash with any of the actors. * Functions that walk the entire kernel page table need synchronization * It's sufficient to add the MHP lock ptdump. Testing ======= This series adds basic DT supported hotplugging. There is a QEMU series enabling MHP for the RISC-V "virt" machine here: [1] ACPI/MSI support is still in the making for RISC-V, and prior proper (ACPI) PCI MSI support lands [2] and NUMA SRAT support [3], it hard to try it out. I've prepared a QEMU branch with proper ACPI GED/PC-DIMM support [4], and a this series with the required prerequisites [5] (AIA, ACPI AIA MADT, ACPI NUMA SRAT). To test with virtio-mem, e.g.: | qemu-system-riscv64 \ | -machine virt,aia=aplic-imsic \ | -cpu rv64,v=true,vlen=256,elen=64,h=true,zbkb=on,zbkc=on,zbkx=on,zkr=on,zkt=on,svinval=on,svnapot=on,svpbmt=on \ | -nodefaults \ | -nographic -smp 8 -kernel rv64-u-boot.bin \ | -drive file=rootfs.img,format=raw,if=virtio \ | -device virtio-rng-pci \ | -m 16G,slots=3,maxmem=32G \ | -object memory-backend-ram,id=mem0,size=16G \ | -numa node,nodeid=0,memdev=mem0 \ | -serial chardev:char0 \ | -mon chardev=char0,mode=readline \ | -chardev stdio,mux=on,id=char0 \ | -device pci-serial,id=serial0,chardev=char0 \ | -object memory-backend-ram,id=vmem0,size=2G \ | -device virtio-mem-pci,id=vm0,memdev=vmem0,node=0 where "rv64-u-boot.bin" is U-boot with EFI/ACPI-support (use [6] if you're lazy). In the QEMU monitor: | (qemu) info memory-devices | (qemu) qom-set vm0 requested-size 1G ...to test DAX/KMEM, use the follow QEMU parameters: | -object memory-backend-file,id=mem1,share=on,mem-path=virtio_pmem.img,size=4G \ | -device virtio-pmem-pci,memdev=mem1,id=nv1 and the regular ndctl/daxctl dance. If you're brave to try the ACPI branch, add "acpi=on" to "-machine virt", and test PC-DIMM MHP (in addition to virtio-{p},mem): In the QEMU monitor: | (qemu) object_add memory-backend-ram,id=mem1,size=1G | (qemu) device_add pc-dimm,id=dimm1,memdev=mem1 You can also try hot-remove with some QEMU options, say: | -object memory-backend-file,id=mem-1,size=256M,mem-path=/pagesize-2MB | -device pc-dimm,id=mem1,memdev=mem-1 | -object memory-backend-file,id=mem-2,size=1G,mem-path=/pagesize-1GB | -device pc-dimm,id=mem2,memdev=mem-2 | -object memory-backend-file,id=mem-3,size=256M,mem-path=/pagesize-2MB | -device pc-dimm,id=mem3,memdev=mem-3 Remove "acpi=on" to run with DT. Thanks to Alex, Andrew, David, and Oscar for all comments/tests/fixups. References ========== [1] https://lore.kernel.org/qemu-devel/[email protected]/ [2] https://lore.kernel.org/linux-riscv/[email protected]/ [3] https://lore.kernel.org/linux-riscv/[email protected]/ [4] https://github.com/bjoto/qemu/commits/virtio-mem-pc-dimm-mhp-acpi-v2/ [5] https://github.com/bjoto/linux/commits/mhp-v4-acpi [6] https://github.com/bjoto/riscv-rootfs-utils/tree/acpi * b4-shazam-merge: riscv: Enable DAX VMEMMAP optimization riscv: mm: Add support for ZONE_DEVICE virtio-mem: Enable virtio-mem for RISC-V riscv: Enable memory hotplugging for RISC-V riscv: mm: Take memory hotplug read-lock during kernel page table dump riscv: mm: Add memory hotplugging support riscv: mm: Add pfn_to_kaddr() implementation riscv: mm: Refactor create_linear_mapping_range() for memory hot add riscv: mm: Change attribute from __init to __meminit for page functions riscv: mm: Pre-allocate vmemmap/direct map/kasan PGD entries riscv: mm: Properly forward vmemmap_populate() altmap parameter Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
2 parents d6ecd18 + 4705c15 commit 60a6707

File tree

10 files changed

+364
-46
lines changed

10 files changed

+364
-46
lines changed

arch/riscv/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ config RISCV
1616
select ACPI_REDUCED_HARDWARE_ONLY if ACPI
1717
select ARCH_DMA_DEFAULT_COHERENT
1818
select ARCH_ENABLE_HUGEPAGE_MIGRATION if HUGETLB_PAGE && MIGRATION
19+
select ARCH_ENABLE_MEMORY_HOTPLUG if SPARSEMEM_VMEMMAP
20+
select ARCH_ENABLE_MEMORY_HOTREMOVE if MEMORY_HOTPLUG
1921
select ARCH_ENABLE_SPLIT_PMD_PTLOCK if PGTABLE_LEVELS > 2
2022
select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE
2123
select ARCH_HAS_BINFMT_FLAT
@@ -35,6 +37,7 @@ config RISCV
3537
select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
3638
select ARCH_HAS_PMEM_API
3739
select ARCH_HAS_PREPARE_SYNC_CORE_CMD
40+
select ARCH_HAS_PTE_DEVMAP if 64BIT && MMU
3841
select ARCH_HAS_PTE_SPECIAL
3942
select ARCH_HAS_SET_DIRECT_MAP if MMU
4043
select ARCH_HAS_SET_MEMORY if MMU
@@ -46,6 +49,7 @@ config RISCV
4649
select ARCH_HAS_UBSAN
4750
select ARCH_HAS_VDSO_DATA
4851
select ARCH_KEEP_MEMBLOCK if ACPI
52+
select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE if 64BIT && MMU
4953
select ARCH_OPTIONAL_KERNEL_RWX if ARCH_HAS_STRICT_KERNEL_RWX
5054
select ARCH_OPTIONAL_KERNEL_RWX_DEFAULT
5155
select ARCH_STACKWALK
@@ -69,6 +73,7 @@ config RISCV
6973
select ARCH_WANT_GENERAL_HUGETLB if !RISCV_ISA_SVNAPOT
7074
select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
7175
select ARCH_WANT_LD_ORPHAN_WARN if !XIP_KERNEL
76+
select ARCH_WANT_OPTIMIZE_DAX_VMEMMAP
7277
select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP
7378
select ARCH_WANTS_NO_INSTR
7479
select ARCH_WANTS_THP_SWAP if HAVE_ARCH_TRANSPARENT_HUGEPAGE

arch/riscv/include/asm/kasan.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
#ifndef __ASSEMBLY__
88

9-
#ifdef CONFIG_KASAN
10-
119
/*
1210
* The following comment was copied from arm64:
1311
* KASAN_SHADOW_START: beginning of the kernel virtual addresses.
@@ -34,6 +32,8 @@
3432
*/
3533
#define KASAN_SHADOW_START ((KASAN_SHADOW_END - KASAN_SHADOW_SIZE) & PGDIR_MASK)
3634
#define KASAN_SHADOW_END MODULES_LOWEST_VADDR
35+
36+
#ifdef CONFIG_KASAN
3737
#define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
3838

3939
void kasan_init(void);

arch/riscv/include/asm/mmu.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ typedef struct {
3131
#define cntx2asid(cntx) ((cntx) & SATP_ASID_MASK)
3232
#define cntx2version(cntx) ((cntx) & ~SATP_ASID_MASK)
3333

34-
void __init create_pgd_mapping(pgd_t *pgdp, uintptr_t va, phys_addr_t pa,
35-
phys_addr_t sz, pgprot_t prot);
34+
void __meminit create_pgd_mapping(pgd_t *pgdp, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
35+
pgprot_t prot);
3636
#endif /* __ASSEMBLY__ */
3737

3838
#endif /* _ASM_RISCV_MMU_H */

arch/riscv/include/asm/page.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ extern phys_addr_t __phys_addr_symbol(unsigned long x);
188188

189189
unsigned long kaslr_offset(void);
190190

191+
static __always_inline void *pfn_to_kaddr(unsigned long pfn)
192+
{
193+
return __va(pfn << PAGE_SHIFT);
194+
}
195+
191196
#endif /* __ASSEMBLY__ */
192197

193198
#define virt_addr_valid(vaddr) ({ \

arch/riscv/include/asm/pgtable-64.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,24 @@ static inline struct page *pgd_page(pgd_t pgd)
398398
#define p4d_offset p4d_offset
399399
p4d_t *p4d_offset(pgd_t *pgd, unsigned long address);
400400

401+
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
402+
static inline int pte_devmap(pte_t pte);
403+
static inline pte_t pmd_pte(pmd_t pmd);
404+
405+
static inline int pmd_devmap(pmd_t pmd)
406+
{
407+
return pte_devmap(pmd_pte(pmd));
408+
}
409+
410+
static inline int pud_devmap(pud_t pud)
411+
{
412+
return 0;
413+
}
414+
415+
static inline int pgd_devmap(pgd_t pgd)
416+
{
417+
return 0;
418+
}
419+
#endif
420+
401421
#endif /* _ASM_RISCV_PGTABLE_64_H */

arch/riscv/include/asm/pgtable-bits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define _PAGE_SOFT (3 << 8) /* Reserved for software */
2020

2121
#define _PAGE_SPECIAL (1 << 8) /* RSW: 0x1 */
22+
#define _PAGE_DEVMAP (1 << 9) /* RSW, devmap */
2223
#define _PAGE_TABLE _PAGE_PRESENT
2324

2425
/*

arch/riscv/include/asm/pgtable.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ struct pt_alloc_ops {
165165
#endif
166166
};
167167

168-
extern struct pt_alloc_ops pt_ops __initdata;
168+
extern struct pt_alloc_ops pt_ops __meminitdata;
169169

170170
#ifdef CONFIG_MMU
171171
/* Number of PGD entries that a user-mode program can use */
@@ -403,6 +403,13 @@ static inline int pte_special(pte_t pte)
403403
return pte_val(pte) & _PAGE_SPECIAL;
404404
}
405405

406+
#ifdef CONFIG_ARCH_HAS_PTE_DEVMAP
407+
static inline int pte_devmap(pte_t pte)
408+
{
409+
return pte_val(pte) & _PAGE_DEVMAP;
410+
}
411+
#endif
412+
406413
/* static inline pte_t pte_rdprotect(pte_t pte) */
407414

408415
static inline pte_t pte_wrprotect(pte_t pte)
@@ -444,6 +451,11 @@ static inline pte_t pte_mkspecial(pte_t pte)
444451
return __pte(pte_val(pte) | _PAGE_SPECIAL);
445452
}
446453

454+
static inline pte_t pte_mkdevmap(pte_t pte)
455+
{
456+
return __pte(pte_val(pte) | _PAGE_DEVMAP);
457+
}
458+
447459
static inline pte_t pte_mkhuge(pte_t pte)
448460
{
449461
return pte;
@@ -734,6 +746,11 @@ static inline pmd_t pmd_mkdirty(pmd_t pmd)
734746
return pte_pmd(pte_mkdirty(pmd_pte(pmd)));
735747
}
736748

749+
static inline pmd_t pmd_mkdevmap(pmd_t pmd)
750+
{
751+
return pte_pmd(pte_mkdevmap(pmd_pte(pmd)));
752+
}
753+
737754
static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
738755
pmd_t *pmdp, pmd_t pmd)
739756
{

0 commit comments

Comments
 (0)