use __create_pgd_mapping() to implement idmap and unify codes
v2 cover use __create_pgd_mapping() to implement idmap and unify codes
This series aim to share the pgtable manipulation code to head.S and trans_pgd_idmap_page().
The core of the idea is by redefinition the CONFIG_PGTABLE_LEVEL, two sets of pgtable manipulation code are generated, one as now is for swapper_pg_dir, the other one is for idmap. And a dedicated create_idmap() API is introduced.
The series can be grouped into two
[1~5/10] achieves porting trans_pgd_idmap_page() and introduce create_idmap() API
[6-10/10] replace head.S pgtable manipulation asm with calling to __create_pgd_mapping()
This series can success booting with the following configuration on Cavium ThunderX 88XX cpu :
PAGE_SIZE VA PA PGTABLE_LEVEL
4K 48 48 4
4K 39 48 3
16K 48 48 4
16K 47 48 3
64K 52 52 3
64K 42 52 2History
RFC:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
Cc: Catalin Marinas <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Marc Zyngier <[email protected]>
Cc: Kristina Martsenko <[email protected]>
Cc: James Morse <[email protected]>
Cc: Steven Price <[email protected]>
Cc: Jonathan Cameron <[email protected]>
Cc: Pavel Tatashin <[email protected]>
Cc: Anshuman Khandual <[email protected]>
Cc: Atish Patra <[email protected]>
Cc: Mike Rapoport <[email protected]>
Cc: Logan Gunthorpe <[email protected]>
Cc: Mark Brown <[email protected]>
To: [email protected]
Pingfan Liu (10):
arm64/mm: split out __create_pgd_mapping() routines arm64/mm: change __create_pgd_mapping() to accept nr_entries param and
introduce create_idmap()
arm64/mm: change __create_pgd_mapping() to accept extra parameter for
allocator
arm64/mm: enable __create_pgd_mapping() to run across different
pgtable
arm64/mm: port trans_pgd_idmap_page() onto create_idmap() arm64/mm: introduce pgtable allocator for idmap_pg_dir and init_pg_dir arm64/pgtable-prot.h: reorganize to cope with asm arm64/mmu_include.c: disable WARN_ON() and BUG_ON() when booting. arm64/mm: make __create_pgd_mapping() coped with pgtable's paddr arm64/head: convert idmap_pg_dir and init_pg_dir to
__create_pgd_mapping()
arch/arm64/Kconfig | 4 + arch/arm64/include/asm/pgalloc.h | 29 +++ arch/arm64/include/asm/pgtable-prot.h | 34 ++- arch/arm64/kernel/head.S | 196 ++++---------- arch/arm64/mm/Makefile | 2 + arch/arm64/mm/idmap_mmu.c | 39 +++ arch/arm64/mm/mmu.c | 362 ++++++-------------------- arch/arm64/mm/mmu_include.c | 320 +++++++++++++++++++++++ arch/arm64/mm/trans_pgd.c | 62 +++-- 9 files changed, 579 insertions(+), 469 deletions(-) create mode 100644 arch/arm64/mm/idmap_mmu.c create mode 100644 arch/arm64/mm/mmu_include.c
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 1/10 arm64/mm: split out __create_pgd_mapping() routines
If VA_BITS < 48, an additional table level should be used to allow an ID mapping to cover system RAM. At present, it is done by macro create_table_entry in head.S. But with second thoughts, this expansion can be easily achieved by redefinition of CONFIG_PGTABLE_LEVELS.
Split out the routines for __create_pgd_mapping(), so that two sets of code will be generated to manipulate pgtable under two different CONFIG_PGTABLE_LEVELS. Later the one generated under 'CONFIG_PGTABLE_LEVELS + 1' can be used for idmap.
---
RFC -> v2:
consider the config ARM64_16K_PAGES && ARM64_VA_BITS_36 and move redefinition of CONFIG_PGTABLE_LEVELS to the head ofidmap_mmu.c
--- arch/arm64/Kconfig | 4 + arch/arm64/mm/Makefile | 2 + arch/arm64/mm/idmap_mmu.c | 34 +++++ arch/arm64/mm/mmu.c | 263 +---------------------------------- arch/arm64/mm/mmu_include.c | 270 ++++++++++++++++++++++++++++++++++++ 5 files changed, 312 insertions(+), 261 deletions(-) create mode 100644 arch/arm64/mm/idmap_mmu.c create mode 100644 arch/arm64/mm/mmu_include.c
arch/arm64/Kconfig +4 -0
@@ -327,6 +327,10 @@ config PGTABLE_LEVELS default 3 if ARM64_16K_PAGES && ARM64_VA_BITS_47 default 4 if !ARM64_64K_PAGES && ARM64_VA_BITS_48 +config IDMAP_PGTABLE_EXPAND + def_bool y + depends on (ARM64_16K_PAGES && ARM64_VA_BITS_36) || (ARM64_64K_PAGES && ARM64_VA_BITS_42) || (ARM64_4K_PAGES && ARM64_VA_BITS_39) || (ARM64_16K_PAGES && ARM64_VA_BITS_47) + config ARCH_SUPPORTS_UPROBES def_bool y
arch/arm64/mm/Makefile +2 -0
@@ -3,6 +3,8 @@ obj-y := dma-mapping.o extable.o fault.o init.o \ cache.o copypage.o flush.o \ ioremap.o mmap.o pgd.o mmu.o \ context.o proc.o pageattr.o + +obj-$(CONFIG_IDMAP_PGTABLE_EXPAND) += idmap_mmu.o obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o obj-$(CONFIG_PTDUMP_CORE) += ptdump.o obj-$(CONFIG_PTDUMP_DEBUGFS) += ptdump_debugfs.o
arch/arm64/mm/idmap_mmu.c new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#ifdef CONFIG_IDMAP_PGTABLE_EXPAND + +#if CONFIG_PGTABLE_LEVELS ==2 +#define EXTEND_LEVEL 3 +#elif CONFIG_PGTABLE_LEVELS ==3 +#define EXTEND_LEVEL 4 +#endif + +#undef CONFIG_PGTABLE_LEVELS +#define CONFIG_PGTABLE_LEVELS EXTEND_LEVEL + +#include <linux/errno.h> + +#include <asm/barrier.h> +#include <asm/fixmap.h> +#include <asm/kernel-pgtable.h> +#include <asm/sections.h> +#include <asm/pgalloc.h> + +#include "./mmu_include.c" + +void __create_pgd_mapping_extend (pgd_t *pgdir,phys_addr_t phys, +unsigned long virt,phys_addr_t size, +pgprot_t prot, +phys_addr_t (*pgtable_alloc)(int ), +int flags) +{ +__create_pgd_mapping (pgdir, phys, virt, size, prot, pgtable_alloc, flags); +} +#endif + +
arch/arm64/mm/mmu.c +2 -261
281 lines not on this page · raw message
arch/arm64/mm/mmu_include.c new file +270 -0
270 lines not on this page · raw message
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 2/10 arm64/mm: change __create_pgd_mapping() to accept nr_entries param and introduce create_idmap()
In reply to Pingfan LiuAs idmap_ptrs_per_pgd may have pgd entries greater than PTRS_PER_PGD, the prototype of __create_pgd_mapping() needs change to cope with that to create idmap.
Now this adaption, create_idmap() API can be introduced to create idmap handly for all kinds of CONFIG_PGTABLE_LEVEL
--- arch/arm64/include/asm/pgalloc.h | 7 ++++++ arch/arm64/kernel/head.S | 3 +++ arch/arm64/mm/idmap_mmu.c | 16 ++++++++----- arch/arm64/mm/mmu.c | 41 ++++++++++++++++++++++++++------ arch/arm64/mm/mmu_include.c | 9 +++++-- 5 files changed, 61 insertions(+), 15 deletions(-)
arch/arm64/include/asm/pgalloc.h +7 -0
@@ -83,4 +83,11 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) }#define pmd_pgtable (pmd) pmd_page(pmd) +extern void __create_pgd_mapping_extend (pgd_t *pgdir, +unsigned int entries_cnt,phys_addr_t phys, +unsigned long virt,phys_addr_t size, +pgprot_t prot, +phys_addr_t (*pgtable_alloc)(int ), +int flags); +#endif
arch/arm64/kernel/head.S +3 -0
@@ -341,6 +341,9 @@ SYM_FUNC_START_LOCAL(__create_page_tables) #if VA_BITS != EXTRA_SHIFT #error "Mismatch between VA_BITS and page size/number of translation levels" #endif + adr_l x4, idmap_extend_pgtable + mov x5, #1 + str x5, [x4] //require expanded pagetable mov x4, EXTRA_PTRS create_table_entry x0, x3, EXTRA_SHIFT, x4, x5, x6
arch/arm64/mm/idmap_mmu.c +10 -6
@@ -21,13 +21,17 @@#include "./mmu_include.c" -void __create_pgd_mapping_extend (pgd_t *pgdir,phys_addr_t phys, -unsigned long virt,phys_addr_t size, -pgprot_t prot, -phys_addr_t (*pgtable_alloc)(int ), -int flags) +void __create_pgd_mapping_extend (pgd_t *pgdir, +unsigned int entries_cnt, +phys_addr_t phys, +unsigned long virt, +phys_addr_t size, +pgprot_t prot, +phys_addr_t (*pgtable_alloc)(int ), +int flags) { -__create_pgd_mapping (pgdir, phys, virt, size, prot, pgtable_alloc, flags); +__create_pgd_mapping (pgdir, entries_cnt, phys, virt, size, prot, + pgtable_alloc, flags); }#endif
arch/arm64/mm/mmu.c +34 -7
89 lines not on this page · raw message
arch/arm64/mm/mmu_include.c +7 -2
@@ -241,14 +241,19 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,pud_clear_fixmap (); } -static void __create_pgd_mapping (pgd_t *pgdir,phys_addr_t phys, +static void __create_pgd_mapping (pgd_t *pgdir,unsigned int entries_cnt,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot,phys_addr_t (*pgtable_alloc)(int ),int flags) {unsigned long addr, end, next; -pgd_t *pgdp =pgd_offset_pgd (pgdir, virt); +pgd_t *pgdp; + +if (likely (entries_cnt ==PTRS_PER_PGD )) + pgdp =pgd_offset_pgd (pgdir, virt); +else + pgdp = pgdir + ((virt >>PGDIR_SHIFT ) & (entries_cnt -1 ));/* * If the virtual and physical address don't have the same offset
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 3/10 arm64/mm: change __create_pgd_mapping() to accept extra parameter for allocator
In reply to Pingfan LiuThe current pgtable allocator only accepts the depth of pgtable as parameter. And the allocator function itself determines the memory pool info.
But incoming pgtable allocator needs an extra param to get local pool info, which directs the allocation. Here preparing the prototype for the incoming change.
--- arch/arm64/include/asm/pgalloc.h | 5 +++- arch/arm64/mm/idmap_mmu.c | 5 ++-- arch/arm64/mm/mmu.c | 31 +++++++++++++------------ arch/arm64/mm/mmu_include.c | 39 +++++++++++++++++++------------- 4 files changed, 46 insertions(+), 34 deletions(-)
arch/arm64/include/asm/pgalloc.h +4 -1
@@ -83,11 +83,14 @@ pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep) }#define pmd_pgtable (pmd) pmd_page(pmd) +typedef phys_addr_t (*pgtable_alloc )(unsigned long shift,void *data); +extern void __create_pgd_mapping_extend (pgd_t *pgdir,unsigned int entries_cnt,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot, -phys_addr_t (*pgtable_alloc)(int ), +pgtable_alloc allocator, +void *info,int flags);#endif
arch/arm64/mm/idmap_mmu.c +3 -2
@@ -27,11 +27,12 @@ void __create_pgd_mapping_extend(pgd_t *pgdir,unsigned long virt, phys_addr_t size, pgprot_t prot, - phys_addr_t (*pgtable_alloc)(int), + pgtable_alloc allocator, + void *info, int flags) {__create_pgd_mapping (pgdir ,entries_cnt ,phys ,virt ,size ,prot , -pgtable_alloc ,flags ); +allocator ,info ,flags ); }#endif
arch/arm64/mm/mmu.c +16 -15
@@ -86,7 +86,7 @@ pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, }EXPORT_SYMBOL (phys_mem_access_prot); -static phys_addr_t __init early_pgtable_alloc (int shift) +static phys_addr_t __init early_pgtable_alloc (unsigned long unused_a,void *unused_b) {phys_addr_t phys;void *ptr;
@@ -113,7 +113,7 @@ static phys_addr_t __init early_pgtable_alloc(int shift)return phys; } -static phys_addr_t __pgd_pgtable_alloc (int shift) +static phys_addr_t __pgd_pgtable_alloc (unsigned long unused_a,void *unused_b) {void *ptr = (void *)__get_free_page (GFP_PGTABLE_KERNEL );BUG_ON (!ptr);
@@ -123,9 +123,9 @@ static phys_addr_t __pgd_pgtable_alloc(int shift)return __pa (ptr); } -static phys_addr_t pgd_pgtable_alloc (int shift) +static phys_addr_t pgd_pgtable_alloc (unsigned long shift,void *unused) { -phys_addr_t pa =__pgd_pgtable_alloc (shift); +phys_addr_t pa =__pgd_pgtable_alloc (shift, unused);/* * Call proper page table ctor in case later we need to
@@ -154,7 +154,8 @@ int idmap_extend_pgtable;void create_idmap (pgd_t *pgdir,phys_addr_t phys,phys_addr_t size,pgprot_t prot, -phys_addr_t (*pgtable_alloc)(int ), +pgtable_alloc allocator, +void *info,int flags) {u64 ptrs_per_pgd = idmap_ptrs_per_pgd;
@@ -162,13 +163,13 @@ void create_idmap(pgd_t *pgdir, phys_addr_t phys,#ifdef CONFIG_IDMAP_PGTABLE_EXPAND if (idmap_extend_pgtable)__create_pgd_mapping_extend (pgdir, ptrs_per_pgd, - phys, phys, size, prot, pgtable_alloc, flags); + phys, phys, size, prot, allocator, info, flags);else __create_pgd_mapping (pgdir, ptrs_per_pgd, - phys, phys, size, prot, pgtable_alloc, flags); + phys, phys, size, prot, allocator, info, flags);#else __create_pgd_mapping (pgdir, ptrs_per_pgd, - phys, phys, size, prot, pgtable_alloc, flags); + phys, phys, size, prot, allocator, info, flags);#endif }
@@ -186,7 +187,7 @@ static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,return ; }__create_pgd_mapping (init_mm.pgd,PTRS_PER_PGD , phys, virt, size, prot,NULL , -NO_CONT_MAPPINGS ); +NULL ,NO_CONT_MAPPINGS ); } void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
@@ -201,7 +202,7 @@ void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys, flags =NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS ;__create_pgd_mapping (mm->pgd,PTRS_PER_PGD , phys, virt, size, prot, - pgd_pgtable_alloc, flags); + pgd_pgtable_alloc,NULL , flags); } static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
@@ -214,7 +215,7 @@ static void update_mapping_prot(phys_addr_t phys, unsigned long virt, }__create_pgd_mapping (init_mm.pgd,PTRS_PER_PGD , phys, virt, size, prot,NULL , -NO_CONT_MAPPINGS ); +NULL ,NO_CONT_MAPPINGS );/* flush the TLBs after updating live kernel mappings */ flush_tlb_kernel_range (virt, virt + size);
@@ -224,7 +225,7 @@ static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start, phys_addr_t end, pgprot_t prot, int flags) { __create_pgd_mapping(pgdp, PTRS_PER_PGD, start, __phys_to_virt(start), end - start, -prot ,early_pgtable_alloc ,flags ); +prot ,early_pgtable_alloc ,NULL ,flags ); }void __init mark_linear_text_alias_ro (void )
@@ -325,7 +326,7 @@ static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,BUG_ON (!PAGE_ALIGNED (size));__create_pgd_mapping (pgdp,PTRS_PER_PGD , pa_start, (unsigned long )va_start, size, prot, - early_pgtable_alloc, flags); + early_pgtable_alloc,NULL , flags);if (!(vm_flags &VM_NO_GUARD )) size +=PAGE_SIZE ;
@@ -369,7 +370,7 @@ static int __init map_entry_trampoline(void)/* Map only the text into the trampoline page table */ memset (tramp_pg_dir,0 ,PGD_SIZE );__create_pgd_mapping (tramp_pg_dir,PTRS_PER_PGD , pa_start,TRAMP_VALIAS ,PAGE_SIZE , - prot, __pgd_pgtable_alloc,0 ); + prot, __pgd_pgtable_alloc,NULL ,0 );/* Map both the text and data into the kernel page table */ __set_fixmap (FIX_ENTRY_TRAMP_TEXT , pa_start, prot);
@@ -1261,7 +1262,7 @@ int arch_add_memory(int nid, u64 start, u64 size, flags =NO_BLOCK_MAPPINGS |NO_CONT_MAPPINGS ;__create_pgd_mapping (swapper_pg_dir,PTRS_PER_PGD , start,__phys_to_virt (start), - size, params->pgprot, __pgd_pgtable_alloc, + size, params->pgprot, __pgd_pgtable_alloc,NULL , flags);memblock_clear_nomap (start, size);
arch/arm64/mm/mmu_include.c +23 -16
@@ -69,7 +69,8 @@ static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,static void alloc_init_cont_pte (pmd_t *pmdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot, -phys_addr_t (*pgtable_alloc)(int ), +pgtable_alloc allocator, +void *info,int flags) {unsigned long next;
@@ -79,8 +80,8 @@ static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,if (pmd_none (pmd)) {phys_addr_t pte_phys; -BUG_ON (!pgtable_alloc); - pte_phys =pgtable_alloc (PAGE_SHIFT ); +BUG_ON (!allocator); + pte_phys =allocator (PAGE_SHIFT , info);__pmd_populate (pmdp, pte_phys,PMD_TYPE_TABLE ); pmd =READ_ONCE (*pmdp); }
@@ -104,7 +105,9 @@ static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,static void init_pmd (pud_t *pudp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot, -phys_addr_t (*pgtable_alloc)(int ),int flags) +pgtable_alloc allocator, +void *info, +int flags) {unsigned long next;pmd_t *pmdp;
@@ -128,7 +131,7 @@ static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end, READ_ONCE(pmd_val(*pmdp)))); }else {alloc_init_cont_pte (pmdp, addr, next, phys, prot, - pgtable_alloc, flags); + allocator, info, flags);BUG_ON (pmd_val (old_pmd) !=0 &&pmd_val (old_pmd) !=READ_ONCE (pmd_val (*pmdp)));
@@ -142,7 +145,9 @@ static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,static void alloc_init_cont_pmd (pud_t *pudp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot, -phys_addr_t (*pgtable_alloc)(int ),int flags) +pgtable_alloc allocator, +void *info, +int flags) {unsigned long next;pud_t pud =READ_ONCE (*pudp);
@@ -154,8 +159,8 @@ static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,if (pud_none (pud)) {phys_addr_t pmd_phys; -BUG_ON (!pgtable_alloc); - pmd_phys =pgtable_alloc (PMD_SHIFT ); +BUG_ON (!allocator); + pmd_phys =allocator (PMD_SHIFT , info);__pud_populate (pudp, pmd_phys,PUD_TYPE_TABLE ); pud =READ_ONCE (*pudp); }
@@ -171,7 +176,7 @@ static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr, (flags & NO_CONT_MAPPINGS) == 0) __prot =__pgprot (pgprot_val (prot) |PTE_CONT ); -init_pmd (pudp, addr, next, phys, __prot, pgtable_alloc, flags); +init_pmd (pudp, addr, next, phys, __prot, allocator, info, flags); phys += next - addr; }while (addr = next, addr != end);
@@ -191,7 +196,8 @@ static inline bool use_1G_block(unsigned long addr, unsigned long next,static void alloc_init_pud (pgd_t *pgdp,unsigned long addr,unsigned long end,phys_addr_t phys,pgprot_t prot, -phys_addr_t (*pgtable_alloc)(int ), +pgtable_alloc allocator, +void *info,int flags) {unsigned long next;
@@ -202,8 +208,8 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,if (p4d_none (p4d)) {phys_addr_t pud_phys; -BUG_ON (!pgtable_alloc); - pud_phys =pgtable_alloc (PUD_SHIFT ); +BUG_ON (!allocator); + pud_phys =allocator (PUD_SHIFT , info);__p4d_populate (p4dp, pud_phys,PUD_TYPE_TABLE ); p4d =READ_ONCE (*p4dp); }
@@ -230,7 +236,7 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, READ_ONCE(pud_val(*pudp)))); }else {alloc_init_cont_pmd (pudp, addr, next, phys, prot, - pgtable_alloc, flags); + allocator, info, flags);BUG_ON (pud_val (old_pud) !=0 &&pud_val (old_pud) !=READ_ONCE (pud_val (*pudp)));
@@ -244,7 +250,8 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,static void __create_pgd_mapping (pgd_t *pgdir,unsigned int entries_cnt,phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot, -phys_addr_t (*pgtable_alloc)(int ), +pgtable_alloc allocator, +void *info,int flags) {unsigned long addr, end, next;
@@ -268,8 +275,8 @@ static void __create_pgd_mapping(pgd_t *pgdir, unsigned int entries_cnt, phys_addo { next =pgd_addr_end (addr, end); -alloc_init_pud (pgdp, addr, next, phys, prot, pgtable_alloc, - flags); +alloc_init_pud (pgdp, addr, next, phys, prot, allocator, + info, flags); phys += next - addr; }while (pgdp++, addr = next, addr != end); }
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 4/10 arm64/mm: enable __create_pgd_mapping() to run across different pgtable
In reply to Pingfan Liu__create_pgd_mapping() is planned not only to be used in boot process, but also after fully bootup. The latter means several callers can run __create_pgd_mapping() concurrenlty.
In this case, PUP/PMD/PTE fixmap should be not used, instead, the virtual addresss owned by each pgtable is used.
---
rfc -> v2:
change allocator return type back to phys_addr_t
--- arch/arm64/mm/mmu_include.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
arch/arm64/mm/mmu_include.c +21 -8
@@ -7,6 +7,7 @@#define NO_BLOCK_MAPPINGS BIT(0)#define NO_CONT_MAPPINGS BIT(1) +#define NO_FIXMAP BIT(2)static bool pgattr_change_is_safe (u64 old,u64 new) {
@@ -43,11 +44,14 @@ static bool pgattr_change_is_safe(u64 old, u64 new) }static void init_pte (pmd_t *pmdp,unsigned long addr,unsigned long end, -phys_addr_t phys,pgprot_t prot) +phys_addr_t phys,pgprot_t prot,int flags) {pte_t *ptep; - ptep =pte_set_fixmap_offset (pmdp, addr); +if (likely (!(flags &NO_FIXMAP ))) + ptep =pte_set_fixmap_offset (pmdp, addr); +else + ptep =pte_offset_kernel (pmdp, addr); do { pte_t old_pte = READ_ONCE(*ptep);
@@ -63,7 +67,8 @@ static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end, phys +=PAGE_SIZE ; }while (ptep++, addr +=PAGE_SIZE , addr != end); -pte_clear_fixmap (); +if (likely (!(flags &NO_FIXMAP ))) +pte_clear_fixmap (); } static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
@@ -97,7 +102,7 @@ static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr, (flags & NO_CONT_MAPPINGS) == 0) __prot =__pgprot (pgprot_val (prot) |PTE_CONT ); -init_pte (pmdp, addr, next, phys, __prot); +init_pte (pmdp, addr, next, phys, __prot, flags); phys += next - addr; }while (addr = next, addr != end);
@@ -112,7 +117,10 @@ static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,unsigned long next;pmd_t *pmdp; - pmdp =pmd_set_fixmap_offset (pudp, addr); +if (likely (!(flags &NO_FIXMAP ))) + pmdp =pmd_set_fixmap_offset (pudp, addr); +else + pmdp =pmd_offset (pudp, addr); do { pmd_t old_pmd = READ_ONCE(*pmdp);
@@ -139,7 +147,8 @@ static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end, phys += next - addr; }while (pmdp++, addr = next, addr != end); -pmd_clear_fixmap (); +if (likely (!(flags &NO_FIXMAP ))) +pmd_clear_fixmap (); } static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
@@ -215,7 +224,10 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, }BUG_ON (p4d_bad (p4d)); - pudp =pud_set_fixmap_offset (p4dp, addr); +if (likely (!(flags &NO_FIXMAP ))) + pudp =pud_set_fixmap_offset (p4dp, addr); +else + pudp =pud_offset (p4dp, addr); do { pud_t old_pud = READ_ONCE(*pudp);
@@ -244,7 +256,8 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, phys += next - addr; }while (pudp++, addr = next, addr != end); -pud_clear_fixmap (); +if (likely (!(flags &NO_FIXMAP ))) +pud_clear_fixmap (); } static void __create_pgd_mapping(pgd_t *pgdir, unsigned int entries_cnt, phys_addr_t phys,
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 5/10 arm64/mm: port trans_pgd_idmap_page() onto create_idmap()
In reply to Pingfan LiuAt present, trans_pgd_idmap_page() has its own logic to set up idmap. To share the common code, porting it onto create_idmap().
--- arch/arm64/include/asm/pgalloc.h | 13 +++++++ arch/arm64/mm/mmu_include.c | 4 --- arch/arm64/mm/trans_pgd.c | 62 ++++++++++++++++---------------- 3 files changed, 43 insertions(+), 36 deletions(-)
arch/arm64/include/asm/pgalloc.h +13 -0
@@ -93,4 +93,17 @@ extern void __create_pgd_mapping_extend(pgd_t *pgdir,void *info, int flags); +extern int idmap_extend_pgtable; + +extern void create_idmap (pgd_t *pgdir,phys_addr_t phys, +phys_addr_t size, +pgprot_t prot, +pgtable_alloc allocator, +void *info, +int flags); + +#define NO_BLOCK_MAPPINGS BIT(0) +#define NO_CONT_MAPPINGS BIT(1) +#define NO_FIXMAP BIT(2) +#endif
arch/arm64/mm/mmu_include.c +0 -4
@@ -5,10 +5,6 @@* and MMU-enable */ -#define NO_BLOCK_MAPPINGS BIT(0) -#define NO_CONT_MAPPINGS BIT(1) -#define NO_FIXMAP BIT(2) -static bool pgattr_change_is_safe (u64 old,u64 new) {/*
arch/arm64/mm/trans_pgd.c +30 -32
74 lines not on this page · raw message
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 6/10 arm64/mm: introduce pgtable allocator for idmap_pg_dir and init_pg_dir
In reply to Pingfan LiuFrom the view of __create_pgd_mapping(), both idmap_pg_dir and init_pg_dir can be treated as a memory pool. Introduce an allocator working on them, so __create_pgd_mapping() can create mapping.
--- arch/arm64/mm/mmu.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
arch/arm64/mm/mmu.c +22 -0
@@ -86,6 +86,28 @@ pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, }EXPORT_SYMBOL (phys_mem_access_prot); +struct mempool { +phys_addr_t start; +unsigned long size; +unsigned long next_idx; +}; + +struct mempool cur_pool; + +void set_cur_mempool (phys_addr_t start,unsigned long size) +{ + cur_pool.start = start; + cur_pool.size = size; + cur_pool.next_idx =0 ; +} + +phys_addr_t __init head_pgtable_alloc (unsigned long unused_a,void *unused_b) +{ +unsigned long idx = cur_pool.next_idx++; + +return cur_pool.start + (idx <<PAGE_SHIFT ); +} +static phys_addr_t __init early_pgtable_alloc (unsigned long unused_a,void *unused_b) {phys_addr_t phys;
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 7/10 arm64/pgtable-prot.h: reorganize to cope with asm
In reply to Pingfan LiuThis patch is one of the preparation for calling __create_pgd_mapping() from head.S.
In order to refer PAGE_KERNEL_EXEC in head.S, reorganize this file and move the needed part under #ifdef __ASSEMBLY__
--- arch/arm64/include/asm/pgtable-prot.h | 34 +++++++++++++++++---------- 1 file changed, 21 insertions(+), 13 deletions(-)
arch/arm64/include/asm/pgtable-prot.h +21 -13
@@ -33,9 +33,6 @@extern bool arm64_use_ng_mappings; -#define _PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED) -#define _PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S) -#define PTE_MAYBE_NG (arm64_use_ng_mappings ? PTE_NG : 0)#define PMD_MAYBE_NG (arm64_use_ng_mappings ? PMD_SECT_NG : 0)
@@ -49,6 +46,26 @@ extern bool arm64_use_ng_mappings;#define PTE_MAYBE_GP 0#endif +#define PAGE_S2_MEMATTR (attr) \ + ({ \ + u64 __val; \ + if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) \ + __val = PTE_S2_MEMATTR(MT_S2_FWB_ ## attr); \ + else \ + __val = PTE_S2_MEMATTR(MT_S2_ ## attr); \ + __val; \ + }) + +#endif /* __ASSEMBLY__ */ + +#ifdef __ASSEMBLY__ +#define PTE_MAYBE_NG 0 +#define __pgprot (x) (x) +#endif + +#define _PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED) +#define _PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S) +#define PROT_DEFAULT (_PROT_DEFAULT | PTE_MAYBE_NG)#define PROT_SECT_DEFAULT (_PROT_SECT_DEFAULT | PMD_MAYBE_NG)
@@ -71,15 +88,7 @@ extern bool arm64_use_ng_mappings;#define PAGE_KERNEL_EXEC __pgprot(PROT_NORMAL & ~PTE_PXN)#define PAGE_KERNEL_EXEC_CONT __pgprot((PROT_NORMAL & ~PTE_PXN) | PTE_CONT) -#define PAGE_S2_MEMATTR (attr) \ - ({ \ - u64 __val; \ - if (cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) \ - __val = PTE_S2_MEMATTR(MT_S2_FWB_ ## attr); \ - else \ - __val = PTE_S2_MEMATTR(MT_S2_ ## attr); \ - __val; \ - }) +#define PAGE_NONE __pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_PROT_NONE | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN)/* shared+writable pages are clean by default, hence PTE_RDONLY|PTE_WRITE */
@@ -106,6 +115,5 @@ extern bool arm64_use_ng_mappings;#define __S110 PAGE_SHARED_EXEC#define __S111 PAGE_SHARED_EXEC -#endif /* __ASSEMBLY__ */ #endif /* __ASM_PGTABLE_PROT_H */
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 8/10 arm64/mmu_include.c: disable WARN_ON() and BUG_ON() when booting.
In reply to Pingfan LiuThis patch is still one of the preparation for calling __create_pgd_mapping() from head.S
When calling from head.S, printk is not ready to work. Hence define SAFE_BUG_ON()/SAFE_WARN_ON(), wrapping around BUG_ON()/WARN_ON() to protect against early calling.
--- arch/arm64/include/asm/pgalloc.h | 1 + arch/arm64/mm/mmu_include.c | 36 +++++++++++++++++++------------- 2 files changed, 23 insertions(+), 14 deletions(-)
arch/arm64/include/asm/pgalloc.h +1 -0
@@ -105,5 +105,6 @@ extern void create_idmap(pgd_t *pgdir, phys_addr_t phys,#define NO_BLOCK_MAPPINGS BIT(0)#define NO_CONT_MAPPINGS BIT(1)#define NO_FIXMAP BIT(2) +#define BOOT_HEAD BIT(3)#endif
arch/arm64/mm/mmu_include.c +22 -14
@@ -5,6 +5,14 @@* and MMU-enable */ +/* +* printk is not ready in the very early stage. And this pair macro should be used +* instead +*/ +#define SAFE_BUG_ON (x, y) if (likely(!(x & BOOT_HEAD))) { BUG_ON(y); } +#define SAFE_WARN_ON (x, y) \ + ({ int _ret; _ret = (x & BOOT_HEAD) ? false : WARN_ON(y); }) +static bool pgattr_change_is_safe (u64 old,u64 new) {/*
@@ -57,7 +65,7 @@ static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,* After the PTE entry has been populated once, we * only allow updates to the permission attributes. */ -BUG_ON (!pgattr_change_is_safe (pte_val (old_pte), +SAFE_BUG_ON (flags, !pgattr_change_is_safe (pte_val (old_pte),READ_ONCE (pte_val (*ptep)))); phys +=PAGE_SIZE ;
@@ -77,16 +85,16 @@ static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,unsigned long next;pmd_t pmd =READ_ONCE (*pmdp); -BUG_ON (pmd_sect (pmd)); +SAFE_BUG_ON (flags,pmd_sect (pmd));if (pmd_none (pmd)) {phys_addr_t pte_phys; -BUG_ON (!allocator); +SAFE_BUG_ON (flags, !allocator); pte_phys =allocator (PAGE_SHIFT , info);__pmd_populate (pmdp, pte_phys,PMD_TYPE_TABLE ); pmd =READ_ONCE (*pmdp); } -BUG_ON (pmd_bad (pmd)); +SAFE_BUG_ON (flags,pmd_bad (pmd)); do { pgprot_t __prot = prot;
@@ -131,13 +139,13 @@ static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,* After the PMD entry has been populated once, we * only allow updates to the permission attributes. */ -BUG_ON (!pgattr_change_is_safe (pmd_val (old_pmd), +SAFE_BUG_ON (flags, !pgattr_change_is_safe (pmd_val (old_pmd),READ_ONCE (pmd_val (*pmdp)))); }else {alloc_init_cont_pte (pmdp, addr, next, phys, prot, allocator, info, flags); -BUG_ON (pmd_val (old_pmd) !=0 && +SAFE_BUG_ON (flags,pmd_val (old_pmd) !=0 &&pmd_val (old_pmd) !=READ_ONCE (pmd_val (*pmdp))); } phys += next - addr;
@@ -160,16 +168,16 @@ static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,/* * Check for initial section mappings in the pgd/pud. */ -BUG_ON (pud_sect (pud)); +SAFE_BUG_ON (flags,pud_sect (pud));if (pud_none (pud)) {phys_addr_t pmd_phys; -BUG_ON (!allocator); +SAFE_BUG_ON (flags, !allocator); pmd_phys =allocator (PMD_SHIFT , info);__pud_populate (pudp, pmd_phys,PUD_TYPE_TABLE ); pud =READ_ONCE (*pudp); } -BUG_ON (pud_bad (pud)); +SAFE_BUG_ON (flags,pud_bad (pud)); do { pgprot_t __prot = prot;
@@ -213,12 +221,12 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,if (p4d_none (p4d)) {phys_addr_t pud_phys; -BUG_ON (!allocator); +SAFE_BUG_ON (flags, !allocator); pud_phys =allocator (PUD_SHIFT , info);__p4d_populate (p4dp, pud_phys,PUD_TYPE_TABLE ); p4d =READ_ONCE (*p4dp); } -BUG_ON (p4d_bad (p4d)); +SAFE_BUG_ON (flags,p4d_bad (p4d));if (likely (!(flags &NO_FIXMAP ))) pudp =pud_set_fixmap_offset (p4dp, addr);
@@ -240,13 +248,13 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,* After the PUD entry has been populated once, we * only allow updates to the permission attributes. */ -BUG_ON (!pgattr_change_is_safe (pud_val (old_pud), +SAFE_BUG_ON (flags, !pgattr_change_is_safe (pud_val (old_pud),READ_ONCE (pud_val (*pudp)))); }else {alloc_init_cont_pmd (pudp, addr, next, phys, prot, allocator, info, flags); -BUG_ON (pud_val (old_pud) !=0 && +SAFE_BUG_ON (flags,pud_val (old_pud) !=0 &&pud_val (old_pud) !=READ_ONCE (pud_val (*pudp))); } phys += next - addr;
@@ -275,7 +283,7 @@ static void __create_pgd_mapping(pgd_t *pgdir, unsigned int entries_cnt, phys_ad* If the virtual and physical address don't have the same offset * within a page, we cannot map the region as the caller expects. */ -if (WARN_ON ((phys ^ virt) & ~PAGE_MASK )) +if (SAFE_WARN_ON (flags, (phys ^ virt) & ~PAGE_MASK ))return ; phys &=PAGE_MASK ;
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 9/10 arm64/mm: make __create_pgd_mapping() coped with pgtable's paddr
In reply to Pingfan LiuThis patch is the last preparation for calling __create_pgd_mapping() from head.S.
Under mmu-offset situation, pud_t */pmd_t */pte_t * points to paddr. During the building of pgtable, they should be carefully handled to avoid the involvement of __va().
--- arch/arm64/mm/mmu_include.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-)
arch/arm64/mm/mmu_include.c +25 -4
@@ -54,6 +54,9 @@ static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,if (likely (!(flags &NO_FIXMAP ))) ptep =pte_set_fixmap_offset (pmdp, addr); +else if (flags &BOOT_HEAD ) +/* for head.S, there is no __va() */ + ptep = (pte_t *)__pmd_to_phys (*pmdp) +pte_index (addr);else ptep =pte_offset_kernel (pmdp, addr); do {
@@ -121,10 +124,19 @@ static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,unsigned long next;pmd_t *pmdp; -if (likely (!(flags &NO_FIXMAP ))) +if (likely (!(flags &NO_FIXMAP ))) { pmdp =pmd_set_fixmap_offset (pudp, addr); -else + }else if (flags &BOOT_HEAD ) { +#if CONFIG_PGTABLE_LEVELS >2 +/* for head.S, there is no __va() */ + pmdp = (pmd_t *)__pud_to_phys (*pudp) +pmd_index (addr); +#else + pmdp = (pmd_t *)pudp; +#endif + }else { pmdp =pmd_offset (pudp, addr); + } + do { pmd_t old_pmd = READ_ONCE(*pmdp);
@@ -228,10 +240,19 @@ static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end, }SAFE_BUG_ON (flags,p4d_bad (p4d)); -if (likely (!(flags &NO_FIXMAP ))) +if (likely (!(flags &NO_FIXMAP ))) { pudp =pud_set_fixmap_offset (p4dp, addr); -else + }else if (flags &BOOT_HEAD ) { +#if CONFIG_PGTABLE_LEVELS >3 +/* for head.S, there is no __va() */ + pudp = (pud_t *)__p4d_to_phys (*p4dp) +pud_index (addr); +#else + pudp = (pud_t *)p4dp; +#endif + }else { pudp =pud_offset (p4dp, addr); + } + do { pud_t old_pud = READ_ONCE(*pudp);
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v2 10/10 arm64/head: convert idmap_pg_dir and init_pg_dir to __create_pgd_mapping()
In reply to Pingfan LiuNow, everything is ready for calling __create_pgd_mapping() from head.S. Switching to these C routine and remove the asm counterpart.
This patch has been successfully tested with the following config value:
PAGE_SIZE VA PA PGTABLE_LEVEL
4K 48 48 4
4K 39 48 3
16K 48 48 4
16K 47 48 3
64K 52 52 3
64K 42 52 2---
RFC -> V2:
correct the asm calling convention.
--- arch/arm64/include/asm/pgalloc.h | 5 + arch/arm64/kernel/head.S | 193 ++++++++----------------------- arch/arm64/mm/mmu.c | 13 +++ 3 files changed, 66 insertions(+), 145 deletions(-)
arch/arm64/include/asm/pgalloc.h +5 -0
@@ -8,6 +8,9 @@#ifndef __ASM_PGALLOC_H#define __ASM_PGALLOC_H +#include <vdso/bits.h> + +#ifndef __ASSEMBLY__#include <asm/pgtable-hwdef.h> #include <asm/processor.h> #include <asm/cacheflush.h>
@@ -102,6 +105,8 @@ extern void create_idmap(pgd_t *pgdir, phys_addr_t phys,void *info, int flags); +#endif +#define NO_BLOCK_MAPPINGS BIT(0)#define NO_CONT_MAPPINGS BIT(1)#define NO_FIXMAP BIT(2)
arch/arm64/kernel/head.S +48 -145
228 lines not on this page · raw message
arch/arm64/mm/mmu.c +13 -0
@@ -167,6 +167,19 @@ static phys_addr_t pgd_pgtable_alloc(unsigned long shift, void *unused)#include "./mmu_include.c" +void create_init_pgd_mapping (pgd_t *pgdir, +phys_addr_t phys, +unsigned long virt, +phys_addr_t size, +pgprot_t prot, +pgtable_alloc allocator, +void * info, +int flags) +{ +__create_pgd_mapping (pgdir,PTRS_PER_PGD , phys, virt, size, + prot, allocator, info, flags); +} +int idmap_extend_pgtable;/*
Signature · 7 lines
2.29.2 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel