Keeping PTE-level mappings is interesting for arm64 as it allows mapping/unmapping pages from the kernel direct map without the risk of splitting blocks which, under the break-before-make rule, may trigger page-faults the kernel can't handle.
However, mapping the entire direct map at PTE-level is costly. So instead, create a new memblock flag MEMBLOCK_PTEMAP to selectively apply a PTE-level mapping.
Signed-off-by: Vincent Donnefort vdonnefort@google.com --- include/linux/memblock.h | 9 +++++++++ mm/memblock.c | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 40e6e0aa2f8d..67ef20a1c5e2 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -52,6 +52,7 @@ extern unsigned long long max_possible_pfn; * know is good to use. It is the only memory that allocations may happen from * in this phase. * @MEMBLOCK_RSRV_HUGETLB: memory is reserved for hugetlb pages + * @MEMBLOCK_PTEMAP: memory region to be mapped using PTE-level mapping */ enum memblock_flags { MEMBLOCK_NONE = 0x0, /* No special request */ @@ -63,6 +64,7 @@ enum memblock_flags { MEMBLOCK_RSRV_KERN = 0x20, /* memory reserved for kernel use */ MEMBLOCK_KHO_NOPRSRV = 0x40, /* memory with no KHO preservations */ MEMBLOCK_RSRV_HUGETLB = 0x80, /* memory reserved for hugetlb pages */ + MEMBLOCK_PTEMAP = 0x100,/* PTE-level mapping */ };
/** @@ -160,6 +162,8 @@ int memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size); int memblock_reserved_mark_kern(phys_addr_t base, phys_addr_t size); int memblock_mark_kho_noprsrv(phys_addr_t base, phys_addr_t size); int memblock_clear_kho_noprsrv(phys_addr_t base, phys_addr_t size); +int memblock_mark_ptemap(phys_addr_t base, phys_addr_t size); +int memblock_clear_ptemap(phys_addr_t base, phys_addr_t size);
void memblock_free(void *ptr, size_t size);
@@ -305,6 +309,11 @@ static inline bool memblock_is_kho_noprsrv(struct memblock_region *m) return m->flags & MEMBLOCK_KHO_NOPRSRV; }
+static inline bool memblock_is_ptemap(struct memblock_region *m) +{ + return m->flags & MEMBLOCK_PTEMAP; +} + int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn, unsigned long *end_pfn); void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn, diff --git a/mm/memblock.c b/mm/memblock.c index a450092a80fb..b8b6e762917a 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -1068,10 +1068,15 @@ static int __init_memblock memblock_setclr_flag(struct memblock_type *type, for (i = start_rgn; i < end_rgn; i++) { struct memblock_region *r = &type->regions[i];
- if (set) + if (set) { + enum memblock_flags map_flags = + MEMBLOCK_NOMAP | MEMBLOCK_PTEMAP; + r->flags |= flag; - else + WARN_ON_ONCE((r->flags & map_flags) == map_flags); + } else { r->flags &= ~flag; + } }
memblock_merge_regions(type, start_rgn, end_rgn); @@ -1221,6 +1226,35 @@ __init int memblock_clear_kho_noprsrv(phys_addr_t base, phys_addr_t size) MEMBLOCK_KHO_NOPRSRV); }
+/** + * memblock_mark_ptemap - Mark a memory region with flag MEMBLOCK_PTEMAP. + * @base: the base phys addr of the region + * @size: the size of the region + * + * If supported by the architecture, such region is mapped using PTE-level + * mappings in the kernel direct map. + * + * Return: 0 on success, -errno on failure. + */ +int __init_memblock memblock_mark_ptemap(phys_addr_t base, phys_addr_t size) +{ + return memblock_setclr_flag(&memblock.memory, base, size, 1, + MEMBLOCK_PTEMAP); +} + +/** + * memblock_clear_ptemap - Clear flag MEMBLOCK_PTEMAP for a specified region. + * @base: the base phys addr of the region + * @size: the size of the region + * + * Return: 0 on success, -errno on failure. + */ +int __init_memblock memblock_clear_ptemap(phys_addr_t base, phys_addr_t size) +{ + return memblock_setclr_flag(&memblock.memory, base, size, 0, + MEMBLOCK_PTEMAP); +} + static bool should_skip_region(struct memblock_type *type, struct memblock_region *m, int nid, int flags) @@ -2854,6 +2888,7 @@ static const char * const flagname[] = { [ilog2(MEMBLOCK_RSRV_KERN)] = "RSV_KERN", [ilog2(MEMBLOCK_KHO_NOPRSRV)] = "KHO_NOPRSRV", [ilog2(MEMBLOCK_RSRV_HUGETLB)] = "RSV_HUGETLB", + [ilog2(MEMBLOCK_PTEMAP)] = "PTEMAP", };
static int memblock_debug_show(struct seq_file *m, void *private)