Skip to contentSkip to conversation

Speed up boot with faster linear map creation

PATCH Superseded Ryan Roberts 5 participants 19 messages Mar 26, 2024 – Mar 28, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Speed up boot with faster linear map creation

Hi All,

It turns out that creating the linear map can take a significant proportion of the total boot time, especially when rodata=full. And a large portion of the time it takes to create the linear map is issuing TLBIs. This series reworks the kernel pgtable generation code to significantly reduce the number of TLBIs. See each patch for details.

The below shows the execution time of map_mem() across a couple of different systems with different RAM configurations. We measure after applying each patch and show the improvement relative to base (v6.9-rc1):

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)

This series applies on top of v6.9-rc1. All mm selftests pass. I haven't yet tested all VA size configs (although I don't anticipate any issues); I'll do this as part of followup.

Thanks,
Ryan

Ryan Roberts (3):
arm64: mm: Don't remap pgtables per- cont(pte|pmd) block
arm64: mm: Don't remap pgtables for allocate vs populate
arm64: mm: Lazily clear pte table mappings from fixmap

 arch/arm64/include/asm/fixmap.h  |   5 +-
 arch/arm64/include/asm/mmu.h     |   8 +
 arch/arm64/include/asm/pgtable.h |   4 -
 arch/arm64/kernel/cpufeature.c   |  10 +-
 arch/arm64/mm/fixmap.c           |  11 +
 arch/arm64/mm/mmu.c              | 364 +++++++++++++++++++++++--------
 include/linux/pgtable.h          |   8 +
 7 files changed, 307 insertions(+), 103 deletions(-)

--
2.25.1

#1 lore raw

PATCH 1/3 arm64: mm: Don't remap pgtables per- cont(pte|pmd) block

A large part of the kernel boot time is creating the kernel linear map page tables. When rodata=full, all memory is mapped by pte. And when there is lots of physical ram, there are lots of pte tables to populate. The primary cost associated with this is mapping and unmapping the pte table memory in the fixmap; at unmap time, the TLB entry must be invalidated and this is expensive.

Previously, each pmd and pte table was fixmapped/fixunmapped for each cont(pte|pmd) block of mappings (16 entries with 4K granule). This means we ended up issuing 32 TLBIs per (pmd|pte) table during the population phase.

Let's fix that, and fixmap/fixunmap each page once per population, for a saving of 31 TLBIs per (pmd|pte) table. This gives a significant boot speedup.

Execution time of map_mem(), which creates the kernel linear map page tables, was measured on different machines with different RAM configs:

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
before         |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
after          |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
---
 arch/arm64/mm/mmu.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
arch/arm64/mm/mmu.c +18 -14
index 495b732d5af3..fd91b5bdb514 100644
@@ -172,12 +172,9 @@ bool pgattr_change_is_safe(u64 old, u64 new)
 	return ((old ^ new) & ~mask) == 0;
 }
 
-static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
-		     phys_addr_t phys, pgprot_t prot)
+static pte_t *init_pte(pte_t *ptep, unsigned long addr, unsigned long end,
+		       phys_addr_t phys, pgprot_t prot)
 {
-	pte_t *ptep;
-
-	ptep = pte_set_fixmap_offset(pmdp, addr);
 	do {
 		pte_t old_pte = __ptep_get(ptep);
 
@@ -193,7 +190,7 @@ 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();
+	return ptep;
 }
 
 static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
@@ -204,6 +201,7 @@ static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
 {
 	unsigned long next;
 	pmd_t pmd = READ_ONCE(*pmdp);
+	pte_t *ptep;
 
 	BUG_ON(pmd_sect(pmd));
 	if (pmd_none(pmd)) {
@@ -219,6 +217,7 @@ static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
 	}
 	BUG_ON(pmd_bad(pmd));
 
+	ptep = pte_set_fixmap_offset(pmdp, addr);
 	do {
 		pgprot_t __prot = prot;
 
@@ -229,20 +228,20 @@ 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);
+		ptep = init_pte(ptep, addr, next, phys, __prot);
 
 		phys += next - addr;
 	} while (addr = next, addr != end);
+
+	pte_clear_fixmap();
 }
 
-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)
+static pmd_t *init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end,
+		       phys_addr_t phys, pgprot_t prot,
+		       phys_addr_t (*pgtable_alloc)(int), int flags)
 {
 	unsigned long next;
-	pmd_t *pmdp;
 
-	pmdp = pmd_set_fixmap_offset(pudp, addr);
 	do {
 		pmd_t old_pmd = READ_ONCE(*pmdp);
 
@@ -269,7 +268,7 @@ 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();
+	return pmdp;
 }
 
 static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
@@ -279,6 +278,7 @@ static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
 {
 	unsigned long next;
 	pud_t pud = READ_ONCE(*pudp);
+	pmd_t *pmdp;
 
 	/*
 	 * Check for initial section mappings in the pgd/pud.
@@ -297,6 +297,7 @@ static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
 	}
 	BUG_ON(pud_bad(pud));
 
+	pmdp = pmd_set_fixmap_offset(pudp, addr);
 	do {
 		pgprot_t __prot = prot;
 
@@ -307,10 +308,13 @@ 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);
+		pmdp = init_pmd(pmdp, addr, next, phys, __prot, pgtable_alloc,
+				flags);
 
 		phys += next - addr;
 	} while (addr = next, addr != end);
+
+	pmd_clear_fixmap();
 }
 
 static void alloc_init_pud(p4d_t *p4dp, unsigned long addr, unsigned long end,
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/3 arm64: mm: Don't remap pgtables for allocate vs populate

In reply to Ryan Roberts

The previous change reduced remapping in the fixmap during the population stage, but the code was still separately fixmapping/fixunmapping each table during allocation in order to clear the contents to zero. Which means each table still has 2 TLB invalidations issued against it. Let's fix this so that each table is only mapped/unmapped once, halving the number of TLBIs.

Achieve this by abstracting pgtable allocate, map and unmap operations out of the main pgtable population loop code and into a struct pgtable_ops function pointer structure. This allows us to formalize the semantics of "alloc" to mean "alloc and map", requiring an "unmap" when finished. So "map" is only performed (and also matched by "unmap") if the pgtable is already been allocated.

As a side effect of this refactoring, we no longer need to use the fixmap at all once pages have been mapped in the linear map because their "map" operation can simply do a __va() translation. So with this change, we are down to 1 TLBI per table when doing early pgtable manipulations, and 0 TLBIs when doing late pgtable manipulations.

Execution time of map_mem(), which creates the kernel linear map page tables, was measured on different machines with different RAM configs:

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
before         |   77   (0%) |  429   (0%) | 1753   (0%) |  3796   (0%)
after          |   77   (0%) |  375 (-13%) | 1532 (-13%) |  3366 (-11%)
---
 arch/arm64/include/asm/mmu.h   |   8 +
 arch/arm64/kernel/cpufeature.c |  10 +-
 arch/arm64/mm/mmu.c            | 308 ++++++++++++++++++++++++---------
 include/linux/pgtable.h        |   8 +
 4 files changed, 243 insertions(+), 91 deletions(-)
arch/arm64/include/asm/mmu.h +8 -0
index 65977c7783c5..ae44353010e8 100644
@@ -109,6 +109,14 @@ static inline bool kaslr_requires_kpti(void)
 	return true;
 }
 
+#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
+extern
+void create_kpti_ng_temp_pgd(pgd_t *pgdir, phys_addr_t phys, unsigned long virt,
+			     phys_addr_t size, pgprot_t prot,
+			     void *(*pgtable_alloc)(int, phys_addr_t *),
+			     int flags);
+#endif
+
 #define INIT_MM_CONTEXT(name)	\
 	.pgd = swapper_pg_dir,
 
arch/arm64/kernel/cpufeature.c +3 -7
index 56583677c1f2..9a70b1954706 100644
@@ -1866,17 +1866,13 @@ static bool has_lpa2(const struct arm64_cpu_capabilities *entry, int scope)
 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
 #define KPTI_NG_TEMP_VA		(-(1UL << PMD_SHIFT))
 
-extern
-void create_kpti_ng_temp_pgd(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);
-
 static phys_addr_t __initdata kpti_ng_temp_alloc;
 
-static phys_addr_t __init kpti_ng_pgd_alloc(int shift)
+static void *__init kpti_ng_pgd_alloc(int type, phys_addr_t *pa)
 {
 	kpti_ng_temp_alloc -= PAGE_SIZE;
-	return kpti_ng_temp_alloc;
+	*pa = kpti_ng_temp_alloc;
+	return __va(kpti_ng_temp_alloc);
 }
 
 static int __init __kpti_install_ng_mappings(void *__unused)
arch/arm64/mm/mmu.c +224 -84

514 lines not on this page · raw message

include/linux/pgtable.h +8 -0
index 85fc7554cd52..1d9e91847cd8 100644
@@ -83,6 +83,14 @@ static inline unsigned long pud_index(unsigned long address)
 #define pud_index pud_index
 #endif
 
+#ifndef p4d_index
+static inline unsigned long p4d_index(unsigned long address)
+{
+	return (address >> P4D_SHIFT) & (PTRS_PER_P4D - 1);
+}
+#define p4d_index p4d_index
+#endif
+
 #ifndef pgd_index
 /* Must be a compile-time constant, so implement it as a macro */
 #define pgd_index(a)  (((a) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v1 2/3] arm64: mm: Don't remap pgtables for allocate vs populate

kernel test robot noticed the following build errors:

Hi Ryan,

kernel test robot noticed the following build errors:

[auto build test ERROR on linus/master]
[also build test ERROR on v6.9-rc1 next-20240326]
[cannot apply to arm64/for-next/core arm-perf/for-next/perf arm/for-next arm/fixes kvmarm/next soc/for-next] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Ryan-Roberts/arm64-mm-Don-t-remap-pgtables-per-cont-pte-pmd-block/20240326-181754
base:   linus/master
patch link:    https://lore.kernel.org/r/20240326101448.3453626-3-ryan.roberts%40arm.com
patch subject: [PATCH v1 2/3] arm64: mm: Don't remap pgtables for allocate vs populate
config: x86_64-rhel-8.3 (https://download.01.org/0day-ci/archive/20240327/[email protected]/config)
compiler: gcc-12 (Ubuntu 12.3.0-9ubuntu2) 12.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240327/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from include/linux/mm.h:29,

                    from include/linux/memcontrol.h:21,
                    from include/linux/swap.h:9,
                    from include/linux/suspend.h:5,
                    from arch/x86/kernel/asm-offsets.c:14:
>> include/linux/pgtable.h:87:29: error: redefinition of 'p4d_index'
      87 | static inline unsigned long p4d_index(unsigned long address)
         |                             ^~~~~~~~~
   In file included from arch/x86/include/asm/tlbflush.h:16,
                    from arch/x86/include/asm/uaccess.h:17,
                    from include/linux/uaccess.h:11,
                    from include/linux/sched/task.h:13,
                    from include/linux/sched/signal.h:9,
                    from include/linux/rcuwait.h:6,
                    from include/linux/percpu-rwsem.h:7,
                    from include/linux/fs.h:33,
                    from include/linux/compat.h:17,
                    from arch/x86/include/asm/ia32.h:7,
                    from arch/x86/include/asm/elf.h:10,
                    from include/linux/elf.h:6,
                    from include/linux/module.h:19,
                    from include/crypto/aria.h:22,
                    from arch/x86/kernel/asm-offsets.c:10:

arch/x86/include/asm/pgtable.h:1134:29: note: previous definition of 'p4d_index' with type 'long unsigned int(long unsigned int)'
1134 | static inline unsigned long p4d_index(unsigned long address)
| ^~~~~~~~~
make[3]: *** [scripts/Makefile.build:117: arch/x86/kernel/asm-offsets.s] Error 1
make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1197: prepare0] Error 2
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:240: __sub-make] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:240: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

vim +/p4d_index +87 include/linux/pgtable.h

    85	
    86	#ifndef p4d_index
  > 87	static inline unsigned long p4d_index(unsigned long address)
    88	{
    89		return (address >> P4D_SHIFT) & (PTRS_PER_P4D - 1);
    90	}
    91	#define p4d_index p4d_index
    92	#endif
    93	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw

PATCH 3/3 arm64: mm: Lazily clear pte table mappings from fixmap

In reply to Ryan Roberts

With the pgtable operations nicely abstracted into struct pgtable_ops, the early pgtable alloc, map and unmap operations are nicely centralized. So let's enhance the implementation to speed up the clearing of pte table mappings in the fixmap.

Extend FIX_MAP so that we now have 16 slots in the fixmap dedicated for pte tables. At alloc/map time, we select the next slot in the series and map it. Or if we are at the end and no more slots are available, clear down all of the slots and start at the beginning again. Batching the clear like this means we can issue tlbis more efficiently.

Due to the batching, there may still be some slots mapped at the end, so address this by adding an optional cleanup() function to struct pgtable_ops. to handle this for us.

Execution time of map_mem(), which creates the kernel linear map page tables, was measured on different machines with different RAM configs:

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
before         |   77   (0%) |  375   (0%) | 1532   (0%) |  3366   (0%)
after          |   63 (-18%) |  330 (-12%) | 1312 (-14%) |  2929 (-13%)
---
 arch/arm64/include/asm/fixmap.h  |  5 +++-
 arch/arm64/include/asm/pgtable.h |  4 ---
 arch/arm64/mm/fixmap.c           | 11 ++++++++
 arch/arm64/mm/mmu.c              | 44 +++++++++++++++++++++++++++++---
 4 files changed, 56 insertions(+), 8 deletions(-)
arch/arm64/include/asm/fixmap.h +4 -1
index 87e307804b99..91fcd7c5c513 100644
@@ -84,7 +84,9 @@ enum fixed_addresses {
 	 * Used for kernel page table creation, so unmapped memory may be used
 	 * for tables.
 	 */
-	FIX_PTE,
+#define NR_PTE_SLOTS		16
+	FIX_PTE_END,
+	FIX_PTE_BEGIN = FIX_PTE_END + NR_PTE_SLOTS - 1,
 	FIX_PMD,
 	FIX_PUD,
 	FIX_P4D,
@@ -108,6 +110,7 @@ void __init early_fixmap_init(void);
 #define __late_clear_fixmap(idx) __set_fixmap((idx), 0, FIXMAP_PAGE_CLEAR)
 
 extern void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot);
+void __init clear_fixmap_nosync(enum fixed_addresses idx);
 
 #include <asm-generic/fixmap.h>
 
arch/arm64/include/asm/pgtable.h +0 -4
index afdd56d26ad7..bd5d02f3f0a3 100644
@@ -686,10 +686,6 @@ static inline unsigned long pmd_page_vaddr(pmd_t pmd)
 /* Find an entry in the third-level page table. */
 #define pte_offset_phys(dir,addr)	(pmd_page_paddr(READ_ONCE(*(dir))) + pte_index(addr) * sizeof(pte_t))
 
-#define pte_set_fixmap(addr)		((pte_t *)set_fixmap_offset(FIX_PTE, addr))
-#define pte_set_fixmap_offset(pmd, addr)	pte_set_fixmap(pte_offset_phys(pmd, addr))
-#define pte_clear_fixmap()		clear_fixmap(FIX_PTE)
-
 #define pmd_page(pmd)			phys_to_page(__pmd_to_phys(pmd))
 
 /* use ONLY for statically allocated translation tables */
arch/arm64/mm/fixmap.c +11 -0
index de1e09d986ad..f83385f6ab86 100644
@@ -131,6 +131,17 @@ void __set_fixmap(enum fixed_addresses idx,
 	}
 }
 
+void __init clear_fixmap_nosync(enum fixed_addresses idx)
+{
+	unsigned long addr = __fix_to_virt(idx);
+	pte_t *ptep;
+
+	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
+
+	ptep = fixmap_pte(addr);
+	pte_clear(&init_mm, addr, ptep);
+}
+
 void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
 {
 	const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
arch/arm64/mm/mmu.c +41 -3

104 lines not on this page · raw message

Signature · 1 line
2.25.1
#5 lore raw
In reply to Ryan Roberts

Hi Ryan,

Quoted 23 lines · Ryan Roberts

Hi All,

It turns out that creating the linear map can take a significant proportion of the total boot time, especially when rodata=full. And a large portion of the time it takes to create the linear map is issuing TLBIs. This series reworks the kernel pgtable generation code to significantly reduce the number of TLBIs. See each patch for details.

The below shows the execution time of map_mem() across a couple of different systems with different RAM configurations. We measure after applying each patch and show the improvement relative to base (v6.9-rc1):

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)
Quoted 4 lines

This series applies on top of v6.9-rc1. All mm selftests pass. I haven't yet tested all VA size configs (although I don't anticipate any issues); I'll do this as part of followup.

These are very nice results!

Before digging into the details: do we still have a strong case for supporting contiguous PTEs and PMDs in these routines?

#6 lore raw
Quoted 31 lines · Ard Biesheuvel

Hi Ryan,

Quoted 23 lines · Ryan Roberts

Hi All,

It turns out that creating the linear map can take a significant proportion of the total boot time, especially when rodata=full. And a large portion of the time it takes to create the linear map is issuing TLBIs. This series reworks the kernel pgtable generation code to significantly reduce the number of TLBIs. See each patch for details.

The below shows the execution time of map_mem() across a couple of different systems with different RAM configurations. We measure after applying each patch and show the improvement relative to base (v6.9-rc1):

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)
Quoted 4 lines

This series applies on top of v6.9-rc1. All mm selftests pass. I haven't yet tested all VA size configs (although I don't anticipate any issues); I'll do this as part of followup.

Quoted 4 lines

These are very nice results!

Before digging into the details: do we still have a strong case for supporting contiguous PTEs and PMDs in these routines?

We are currently using contptes and pmds for the linear map when rodata=[on|off] IIRC? I don't see a need to remove the capability personally.

Also I was talking with Mark R yesterday and he suggested that an even better solution might be to create a temp pgtable that maps the linear map with pmds, switch to it, then create the real pgtable that maps the linear map with ptes, then switch to that. The benefit being that we can avoid the fixmap entirely when creating the second pgtable - we think this would likely be significantly faster still.

My second patch adds the infrastructure to make this possible. But your changes for LPA2 make it significantly more effort; since that change we are now using the swapper pgtable when we populate the linear map into it - the kernel is already mapped and that isn't done in paging_init() anymore. So I'm not quite sure how we can easily make that work at the moment.

Thanks,
Ryan

#7 lore raw
Quoted 33 lines · Ryan Roberts
Quoted 31 lines · Ard Biesheuvel

Hi Ryan,

Quoted 27 lines · Ryan Roberts

Hi All,

It turns out that creating the linear map can take a significant proportion of the total boot time, especially when rodata=full. And a large portion of the time it takes to create the linear map is issuing TLBIs. This series reworks the kernel pgtable generation code to significantly reduce the number of TLBIs. See each patch for details.

The below shows the execution time of map_mem() across a couple of different systems with different RAM configurations. We measure after applying each patch and show the improvement relative to base (v6.9-rc1):

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)

This series applies on top of v6.9-rc1. All mm selftests pass. I haven't yet tested all VA size configs (although I don't anticipate any issues); I'll do this as part of followup.

Quoted 7 lines
Quoted 4 lines

These are very nice results!

Before digging into the details: do we still have a strong case for supporting contiguous PTEs and PMDs in these routines?

We are currently using contptes and pmds for the linear map when rodata=[on|off] IIRC?

In principle, yes. In practice?

Quoted 2 lines

I don't see a need to remove the capability personally.

Since we are making changes here, it is a relevant question to ask imho.

Quoted 7 lines

Also I was talking with Mark R yesterday and he suggested that an even better solution might be to create a temp pgtable that maps the linear map with pmds, switch to it, then create the real pgtable that maps the linear map with ptes, then switch to that. The benefit being that we can avoid the fixmap entirely when creating the second pgtable - we think this would likely be significantly faster still.

If this is going to be a temporary mapping for the duration of the initial population of the linear map page tables, we might just as well use a 1:1 TTBR0 mapping here, which would be completely disjoint from swapper. And we'd only need to map memory that is being used for page tables, so on those large systems we'd need to map only a small slice. Maybe it's time to bring back the memblock alloc limit so we can manage this more easily?

Quoted 6 lines

My second patch adds the infrastructure to make this possible. But your changes for LPA2 make it significantly more effort; since that change we are now using the swapper pgtable when we populate the linear map into it - the kernel is already mapped and that isn't done in paging_init() anymore. So I'm not quite sure how we can easily make that work at the moment.

I think a mix of the fixmap approach with a 1:1 map could work here:
- use TTBR0 to create a temp 1:1 map of DRAM
- map page tables lazily as they are allocated but using a coarse mapping
- avoid all TLB maintenance except at the end when tearing down the 1:1 mapping.

#8 lore raw
Quoted 72 lines · Ard Biesheuvel
Quoted 33 lines · Ryan Roberts
Quoted 31 lines · Ard Biesheuvel

Hi Ryan,

Quoted 27 lines · Ryan Roberts

Hi All,

It turns out that creating the linear map can take a significant proportion of the total boot time, especially when rodata=full. And a large portion of the time it takes to create the linear map is issuing TLBIs. This series reworks the kernel pgtable generation code to significantly reduce the number of TLBIs. See each patch for details.

The below shows the execution time of map_mem() across a couple of different systems with different RAM configurations. We measure after applying each patch and show the improvement relative to base (v6.9-rc1):

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)

This series applies on top of v6.9-rc1. All mm selftests pass. I haven't yet tested all VA size configs (although I don't anticipate any issues); I'll do this as part of followup.

Quoted 7 lines
Quoted 4 lines

These are very nice results!

Before digging into the details: do we still have a strong case for supporting contiguous PTEs and PMDs in these routines?

We are currently using contptes and pmds for the linear map when rodata=[on|off] IIRC?

In principle, yes. In practice?

Quoted 2 lines

I don't see a need to remove the capability personally.

Since we are making changes here, it is a relevant question to ask imho.

Quoted 7 lines

Also I was talking with Mark R yesterday and he suggested that an even better solution might be to create a temp pgtable that maps the linear map with pmds, switch to it, then create the real pgtable that maps the linear map with ptes, then switch to that. The benefit being that we can avoid the fixmap entirely when creating the second pgtable - we think this would likely be significantly faster still.

If this is going to be a temporary mapping for the duration of the initial population of the linear map page tables, we might just as well use a 1:1 TTBR0 mapping here, which would be completely disjoint from swapper. And we'd only need to map memory that is being used for page tables, so on those large systems we'd need to map only a small slice. Maybe it's time to bring back the memblock alloc limit so we can manage this more easily?

Quoted 6 lines

My second patch adds the infrastructure to make this possible. But your changes for LPA2 make it significantly more effort; since that change we are now using the swapper pgtable when we populate the linear map into it - the kernel is already mapped and that isn't done in paging_init() anymore. So I'm not quite sure how we can easily make that work at the moment.

Quoted 4 lines

I think a mix of the fixmap approach with a 1:1 map could work here:
- use TTBR0 to create a temp 1:1 map of DRAM
- map page tables lazily as they are allocated but using a coarse mapping
- avoid all TLB maintenance except at the end when tearing down the 1:1 mapping.

Yes that could work I think. So to make sure I've understood:

- create a 1:1 map for all of DRAM using block and cont mappings where possible
- use memblock_phys_alloc_*() to allocate pgtable memory
- access via fixmap (should be minimal due to block mappings)
- install it in TTBR0
- create all the swapper mappings as normal (no block or cont mappings)
- use memblock_phys_alloc_*() to alloc pgtable memory
- phys address is also virtual address due to installed 1:1 map
- Remove 1:1 map from TTBR0
- memblock_phys_free() all the memory associated with 1:1 map

That sounds doable on top of the first 2 patches in this series - I'll have a crack. The only missing piece is depth-first 1:1 map traversal to free the tables. I'm guessing something already exists that I can repurpose?

Thanks,
Ryan

#9 lore raw
Quoted 5 lines · Ryan Roberts
Quoted 3 lines · Ard Biesheuvel
Quoted 2 lines · Ryan Roberts

On 27/03/2024 10:09, Ard Biesheuvel wrote:

...

Quoted 11 lines
Quoted 5 lines

I think a mix of the fixmap approach with a 1:1 map could work here:
- use TTBR0 to create a temp 1:1 map of DRAM
- map page tables lazily as they are allocated but using a coarse mapping
- avoid all TLB maintenance except at the end when tearing down the 1:1 mapping.

Yes that could work I think. So to make sure I've understood:

- create a 1:1 map for all of DRAM using block and cont mappings where possible
- use memblock_phys_alloc_*() to allocate pgtable memory
- access via fixmap (should be minimal due to block mappings)

Yes but you'd only need the fixmap for pages that are not in the 1:1 map yet, so after an initial ramp up you wouldn't need it at all, assuming locality of memblock allocations and the use of PMD mappings. The only tricky thing here is ensuring that we are not mapping memory that we shouldn't be touching.

Quoted 7 lines

- install it in TTBR0
- create all the swapper mappings as normal (no block or cont mappings)
- use memblock_phys_alloc_*() to alloc pgtable memory
- phys address is also virtual address due to installed 1:1 map
- Remove 1:1 map from TTBR0
- memblock_phys_free() all the memory associated with 1:1 map

Indeed.

Quoted 4 lines

That sounds doable on top of the first 2 patches in this series - I'll have a crack. The only missing piece is depth-first 1:1 map traversal to free the tables. I'm guessing something already exists that I can repurpose?

Not that I am aware of, but that doesn't sound too complicated.

#10 lore raw
Quoted 19 lines · Ard Biesheuvel
Quoted 5 lines · Ryan Roberts
Quoted 3 lines · Ard Biesheuvel
Quoted 2 lines · Ryan Roberts

On 27/03/2024 10:09, Ard Biesheuvel wrote:

...

Quoted 11 lines
Quoted 5 lines

I think a mix of the fixmap approach with a 1:1 map could work here:
- use TTBR0 to create a temp 1:1 map of DRAM
- map page tables lazily as they are allocated but using a coarse mapping
- avoid all TLB maintenance except at the end when tearing down the 1:1 mapping.

Yes that could work I think. So to make sure I've understood:

- create a 1:1 map for all of DRAM using block and cont mappings where possible
- use memblock_phys_alloc_*() to allocate pgtable memory
- access via fixmap (should be minimal due to block mappings)

Quoted 5 lines

Yes but you'd only need the fixmap for pages that are not in the 1:1 map yet, so after an initial ramp up you wouldn't need it at all, assuming locality of memblock allocations and the use of PMD mappings. The only tricky thing here is ensuring that we are not mapping memory that we shouldn't be touching.

That sounds a bit nasty though. I think it would be simpler to just reuse the machinery we have, doing the 1:1 map using blocks and fixmap; It should be a factor of 512 better than what we have, so probably not a problem at that point. That way, we can rely on memblock to tell us what to map. If its still problematic I can add a layer to support 1G mappings too.

Quoted 10 lines
Quoted 8 lines

- install it in TTBR0
- create all the swapper mappings as normal (no block or cont mappings)
- use memblock_phys_alloc_*() to alloc pgtable memory
- phys address is also virtual address due to installed 1:1 map
- Remove 1:1 map from TTBR0
- memblock_phys_free() all the memory associated with 1:1 map

Indeed.

One question on the state of TTBR0 at entrance to paging_init(); what is it? I need to know so I can set it back after.

Currently I'm thinking I can do:

cpu_install_ttbr0(my_dram_idmap, TCR_T0SZ(vabits_actual)); <create swapper>
cpu_set_reserved_ttbr0();
local_flush_tlb_all();

But is it ok to leave the reserved pdg in ttbr0, or is it expecting something else?

Quoted 7 lines
Quoted 5 lines

That sounds doable on top of the first 2 patches in this series - I'll have a crack. The only missing piece is depth-first 1:1 map traversal to free the tables. I'm guessing something already exists that I can repurpose?

Not that I am aware of, but that doesn't sound too complicated.

Tested-by on the v1 series
#11 lore raw
In reply to Ryan Roberts
Quoted 22 lines · Ryan Roberts

Hi All,

It turns out that creating the linear map can take a significant proportion of the total boot time, especially when rodata=full. And a large portion of the time it takes to create the linear map is issuing TLBIs. This series reworks the kernel pgtable generation code to significantly reduce the number of TLBIs. See each patch for details.

The below shows the execution time of map_mem() across a couple of different systems with different RAM configurations. We measure after applying each patch and show the improvement relative to base (v6.9-rc1):

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)
Quoted 3 lines

This series applies on top of v6.9-rc1. All mm selftests pass. I haven't yet tested all VA size configs (although I don't anticipate any issues); I'll do this as part of followup.

The series was applied cleanly on top of v6.9-rc1+ of Linus's master branch, and boots fine on M1 VM with 14GB of memory.

Just out of curiosity, how did you measure the boot time and obtain the breakdown of the execution times of each phase?

Thanks,
Itaru.

Quoted 22 lines

Thanks,
Ryan

Ryan Roberts (3):
arm64: mm: Don't remap pgtables per- cont(pte|pmd) block
arm64: mm: Don't remap pgtables for allocate vs populate
arm64: mm: Lazily clear pte table mappings from fixmap

 arch/arm64/include/asm/fixmap.h  |   5 +-
 arch/arm64/include/asm/mmu.h     |   8 +
 arch/arm64/include/asm/pgtable.h |   4 -
 arch/arm64/kernel/cpufeature.c   |  10 +-
 arch/arm64/mm/fixmap.c           |  11 +
 arch/arm64/mm/mmu.c              | 364 +++++++++++++++++++++++--------
 include/linux/pgtable.h          |   8 +
 7 files changed, 307 insertions(+), 103 deletions(-)

--
2.25.1

#12 lore raw
Quoted 27 lines · Itaru Kitayama
Quoted 22 lines · Ryan Roberts

Hi All,

It turns out that creating the linear map can take a significant proportion of the total boot time, especially when rodata=full. And a large portion of the time it takes to create the linear map is issuing TLBIs. This series reworks the kernel pgtable generation code to significantly reduce the number of TLBIs. See each patch for details.

The below shows the execution time of map_mem() across a couple of different systems with different RAM configurations. We measure after applying each patch and show the improvement relative to base (v6.9-rc1):

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)
Quoted 3 lines

This series applies on top of v6.9-rc1. All mm selftests pass. I haven't yet tested all VA size configs (although I don't anticipate any issues); I'll do this as part of followup.

Quoted 5 lines

The series was applied cleanly on top of v6.9-rc1+ of Linus's master branch, and boots fine on M1 VM with 14GB of memory.

Just out of curiosity, how did you measure the boot time and obtain the breakdown of the execution times of each phase?

arch/arm64/mm/mmu.c +7 -0
index 495b732d5af3..8a9d47115784 100644
@@ -792,7 +792,14 @@ static void __init create_idmap(void)
 
 void __init paging_init(void)
 {
+       u64 start, end;
+
+       start = __arch_counter_get_cntvct();
        map_mem(swapper_pg_dir);
+       end = __arch_counter_get_cntvct();
+
+       pr_err("map_mem: time=%llu us\n",
+               ((end - start) * 1000000) / arch_timer_get_cntfrq());
 
        memblock_allow_resize();
 
Quoted 2 lines

Thanks!

Quoted 26 lines

Thanks,
Itaru.

Quoted 22 lines

Thanks,
Ryan

Ryan Roberts (3):
arm64: mm: Don't remap pgtables per- cont(pte|pmd) block
arm64: mm: Don't remap pgtables for allocate vs populate
arm64: mm: Lazily clear pte table mappings from fixmap

 arch/arm64/include/asm/fixmap.h  |   5 +-
 arch/arm64/include/asm/mmu.h     |   8 +
 arch/arm64/include/asm/pgtable.h |   4 -
 arch/arm64/kernel/cpufeature.c   |  10 +-
 arch/arm64/mm/fixmap.c           |  11 +
 arch/arm64/mm/mmu.c              | 364 +++++++++++++++++++++++--------
 include/linux/pgtable.h          |   8 +
 7 files changed, 307 insertions(+), 103 deletions(-)

--
2.25.1

#13 lore raw

PATCH 1/1 arm64: mm: Batch dsb and isb when populating pgtables

In reply to Ryan Roberts

After removing uneccessary TLBIs, the next bottleneck when creating the page tables for the linear map is DSB and ISB, which were previously issued per-pte in __set_pte(). Since we are writing multiple ptes in a given pte table, we can elide these barriers and insert them once we have finished writing to the table.

---
 arch/arm64/include/asm/pgtable.h |  7 ++++++-
 arch/arm64/mm/mmu.c              | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)
arch/arm64/include/asm/pgtable.h +6 -1
index bd5d02f3f0a3..81e427b23b3f 100644
@@ -271,9 +271,14 @@ static inline pte_t pte_mkdevmap(pte_t pte)
 	return set_pte_bit(pte, __pgprot(PTE_DEVMAP | PTE_SPECIAL));
 }
 
-static inline void __set_pte(pte_t *ptep, pte_t pte)
+static inline void ___set_pte(pte_t *ptep, pte_t pte)
 {
 	WRITE_ONCE(*ptep, pte);
+}
+
+static inline void __set_pte(pte_t *ptep, pte_t pte)
+{
+	___set_pte(ptep, pte);
 
 	/*
 	 * Only if the new pte is valid and kernel, otherwise TLB maintenance
arch/arm64/mm/mmu.c +12 -1
index 1b2a2a2d09b7..c6d5a76732d4 100644
@@ -301,7 +301,11 @@ static pte_t *init_pte(pte_t *ptep, unsigned long addr, unsigned long end,
 	do {
 		pte_t old_pte = __ptep_get(ptep);
 
-		__set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
+		/*
+		 * Required barriers to make this visible to the table walker
+		 * are deferred to the end of alloc_init_cont_pte().
+		 */
+		___set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
 
 		/*
 		 * After the PTE entry has been populated once, we
@@ -358,6 +362,13 @@ static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
 	} while (addr = next, addr != end);
 
 	ops->unmap(TYPE_PTE);
+
+	/*
+	 * Ensure all previous pgtable writes are visible to the table walker.
+	 * See init_pte().
+	 */
+	dsb(ishst);
+	isb();
 }
 
 static pmd_t *init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end,

--
2.25.1

#14 lore raw

Re: [PATCH v1] arm64: mm: Batch dsb and isb when populating pgtables

Quoted 7 lines · Ryan Roberts

After removing uneccessary TLBIs, the next bottleneck when creating the page tables for the linear map is DSB and ISB, which were previously issued per-pte in __set_pte(). Since we are writing multiple ptes in a given pte table, we can elide these barriers and insert them once we have finished writing to the table.

Nice!

Quoted 11 lines
---
 arch/arm64/include/asm/pgtable.h |  7 ++++++-
 arch/arm64/mm/mmu.c              | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index bd5d02f3f0a3..81e427b23b3f 100644

arch/arm64/include/asm/pgtable.h static inline pte_t pte_mkdevmap(pte_t pte) · line 274

Quoted 5 lines
arch/arm64/include/asm/pgtable.h +1 -1
@@ -271,9 +271,14 @@ static inline pte_t pte_mkdevmap(pte_t pte)
        return set_pte_bit(pte, __pgprot(PTE_DEVMAP | PTE_SPECIAL));
 }
 
-static inline void __set_pte(pte_t *ptep, pte_t pte)
+static inline void ___set_pte(pte_t *ptep, pte_t pte)

IMHO, we should either use WRITE_ONCE() directly in the caller, or find a better name.

Quoted 44 lines
arch/arm64/include/asm/pgtable.h +5 -0
 {
        WRITE_ONCE(*ptep, pte);
+}
+
+static inline void __set_pte(pte_t *ptep, pte_t pte)
+{
+       ___set_pte(ptep, pte);
 
        /*
         * Only if the new pte is valid and kernel, otherwise TLB maintenance
arch/arm64/mm/mmu.c +12 -2
index 1b2a2a2d09b7..c6d5a76732d4 100644
@@ -301,7 +301,11 @@ static pte_t *init_pte(pte_t *ptep, unsigned long addr, unsigned long end,
        do {
                pte_t old_pte = __ptep_get(ptep);
 
-               __set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
+               /*
+                * Required barriers to make this visible to the table walker
+                * are deferred to the end of alloc_init_cont_pte().
+                */
+               ___set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
 
                /*
                 * After the PTE entry has been populated once, we
@@ -358,6 +362,13 @@ static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
        } while (addr = next, addr != end);
 
        ops->unmap(TYPE_PTE);
+
+       /*
+        * Ensure all previous pgtable writes are visible to the table walker.
+        * See init_pte().
+        */
+       dsb(ishst);
+       isb();
 }
 
 static pmd_t *init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end,
--

2.25.1

#15 lore raw

Re: [PATCH v1] arm64: mm: Batch dsb and isb when populating pgtables

Quoted 25 lines · Ard Biesheuvel
Quoted 7 lines · Ryan Roberts

After removing uneccessary TLBIs, the next bottleneck when creating the page tables for the linear map is DSB and ISB, which were previously issued per-pte in __set_pte(). Since we are writing multiple ptes in a given pte table, we can elide these barriers and insert them once we have finished writing to the table.

Nice!

Quoted 14 lines
---
 arch/arm64/include/asm/pgtable.h |  7 ++++++-
 arch/arm64/mm/mmu.c              | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index bd5d02f3f0a3..81e427b23b3f 100644
@@ -271,9 +271,14 @@ static inline pte_t pte_mkdevmap(pte_t pte)
        return set_pte_bit(pte, __pgprot(PTE_DEVMAP | PTE_SPECIAL));
 }
 
Quoted 5 lines

arch/arm64/include/asm/pgtable.h static inline pte_t pte_mkdevmap(pte_t pte) · line 274

Quoted 2 lines
arch/arm64/include/asm/pgtable.h +1 -1
-static inline void __set_pte(pte_t *ptep, pte_t pte)
+static inline void ___set_pte(pte_t *ptep, pte_t pte)

IMHO, we should either use WRITE_ONCE() directly in the caller, or find a better name.

How about __set_pte_nosync() ?

Quoted 45 lines
Quoted 45 lines
arch/arm64/include/asm/pgtable.h +5 -0
 {
        WRITE_ONCE(*ptep, pte);
+}
+
+static inline void __set_pte(pte_t *ptep, pte_t pte)
+{
+       ___set_pte(ptep, pte);
 
        /*
         * Only if the new pte is valid and kernel, otherwise TLB maintenance
arch/arm64/mm/mmu.c +12 -2
index 1b2a2a2d09b7..c6d5a76732d4 100644
@@ -301,7 +301,11 @@ static pte_t *init_pte(pte_t *ptep, unsigned long addr, unsigned long end,
        do {
                pte_t old_pte = __ptep_get(ptep);
 
-               __set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
+               /*
+                * Required barriers to make this visible to the table walker
+                * are deferred to the end of alloc_init_cont_pte().
+                */
+               ___set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
 
                /*
                 * After the PTE entry has been populated once, we
@@ -358,6 +362,13 @@ static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
        } while (addr = next, addr != end);
 
        ops->unmap(TYPE_PTE);
+
+       /*
+        * Ensure all previous pgtable writes are visible to the table walker.
+        * See init_pte().
+        */
+       dsb(ishst);
+       isb();
 }
 
 static pmd_t *init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end,
--

2.25.1

#16 lore raw

Re: [PATCH v1] arm64: mm: Batch dsb and isb when populating pgtables

Quoted 27 lines · Ryan Roberts
Quoted 25 lines · Ard Biesheuvel
Quoted 7 lines · Ryan Roberts

After removing uneccessary TLBIs, the next bottleneck when creating the page tables for the linear map is DSB and ISB, which were previously issued per-pte in __set_pte(). Since we are writing multiple ptes in a given pte table, we can elide these barriers and insert them once we have finished writing to the table.

Nice!

Quoted 14 lines
---
 arch/arm64/include/asm/pgtable.h |  7 ++++++-
 arch/arm64/mm/mmu.c              | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -0
index bd5d02f3f0a3..81e427b23b3f 100644
@@ -271,9 +271,14 @@ static inline pte_t pte_mkdevmap(pte_t pte)
        return set_pte_bit(pte, __pgprot(PTE_DEVMAP | PTE_SPECIAL));
 }
 
Quoted 8 lines
Quoted 5 lines

arch/arm64/include/asm/pgtable.h static inline pte_t pte_mkdevmap(pte_t pte) · line 274

Quoted 2 lines
arch/arm64/include/asm/pgtable.h +1 -1
-static inline void __set_pte(pte_t *ptep, pte_t pte)
+static inline void ___set_pte(pte_t *ptep, pte_t pte)

IMHO, we should either use WRITE_ONCE() directly in the caller, or find a better name.

How about __set_pte_nosync() ?

Works for me.

#17 lore raw
In reply to Ryan Roberts
Quoted 12 lines · Ryan Roberts

Hi All,

It turns out that creating the linear map can take a significant proportion of the total boot time, especially when rodata=full. And a large portion of the time it takes to create the linear map is issuing TLBIs. This series reworks the kernel pgtable generation code to significantly reduce the number of TLBIs. See each patch for details.

The below shows the execution time of map_mem() across a couple of different systems with different RAM configurations. We measure after applying each patch and show the improvement relative to base (v6.9-rc1):

Quoted 9 lines
               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)

I've just appended an additional patch to this series. This takes us to a ~95% reduction overall:

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)
batch-barriers |   11 (-93%) |   61 (-97%) |  261 (-97%) |   837 (-95%)

Don't believe the intermediate block-based pgtable idea will now be neccessary so I don't intend to persue that. It might be that we choose to drop the middle two patchs; I'm keen to hear opinions.

Thanks,
Ryan

Quoted 26 lines

This series applies on top of v6.9-rc1. All mm selftests pass. I haven't yet tested all VA size configs (although I don't anticipate any issues); I'll do this as part of followup.

Thanks,
Ryan

Ryan Roberts (3):
arm64: mm: Don't remap pgtables per- cont(pte|pmd) block
arm64: mm: Don't remap pgtables for allocate vs populate
arm64: mm: Lazily clear pte table mappings from fixmap

 arch/arm64/include/asm/fixmap.h  |   5 +-
 arch/arm64/include/asm/mmu.h     |   8 +
 arch/arm64/include/asm/pgtable.h |   4 -
 arch/arm64/kernel/cpufeature.c   |  10 +-
 arch/arm64/mm/fixmap.c           |  11 +
 arch/arm64/mm/mmu.c              | 364 +++++++++++++++++++++++--------
 include/linux/pgtable.h          |   8 +
 7 files changed, 307 insertions(+), 103 deletions(-)

--
2.25.1

Tested-by
#18 lore raw
Quoted 37 lines · Ryan Roberts
Quoted 12 lines · Ryan Roberts

Hi All,

It turns out that creating the linear map can take a significant proportion of the total boot time, especially when rodata=full. And a large portion of the time it takes to create the linear map is issuing TLBIs. This series reworks the kernel pgtable generation code to significantly reduce the number of TLBIs. See each patch for details.

The below shows the execution time of map_mem() across a couple of different systems with different RAM configurations. We measure after applying each patch and show the improvement relative to base (v6.9-rc1):

Quoted 9 lines
               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)

I've just appended an additional patch to this series. This takes us to a ~95% reduction overall:

               | Apple M2 VM | Ampere Altra| Ampere Altra| Ampere Altra
               | VM, 16G     | VM, 64G     | VM, 256G    | Metal, 512G
---------------|-------------|-------------|-------------|-------------
               |   ms    (%) |   ms    (%) |   ms    (%) |    ms    (%)
---------------|-------------|-------------|-------------|-------------
base           |  151   (0%) | 2191   (0%) | 8990   (0%) | 17443   (0%)
no-cont-remap  |   77 (-49%) |  429 (-80%) | 1753 (-80%) |  3796 (-78%)
no-alloc-remap |   77 (-49%) |  375 (-83%) | 1532 (-83%) |  3366 (-81%)
lazy-unmap     |   63 (-58%) |  330 (-85%) | 1312 (-85%) |  2929 (-83%)
batch-barriers |   11 (-93%) |   61 (-97%) |  261 (-97%) |   837 (-95%)
Quoted 4 lines

Don't believe the intermediate block-based pgtable idea will now be neccessary so I don't intend to persue that. It might be that we choose to drop the middle two patchs; I'm keen to hear opinions.

Applied on v6.9-rc1, I have much shorter base timing on a similar machine (Ampere HR350A). no-alloc-remap didn't show much difference either.

               | SA8775p-ride | Ampere HR350A|
               | VM, 36G      | Metal, 256G  |
---------------|--------------|--------------|
               |   ms     (%) |   ms     (%) |
---------------|--------------|--------------|
base           |  358    (0%) | 2213    (0%) |
no-cont-remap  |  232  (-35%) | 1283  (-42%) |
no-alloc-remap |  228  (-36%) | 1282  (-42%) |
lazy-unmap     |  231  (-35%) | 1248  (-44%) |
batch-barriers |   25  (-93%) |  204  (-91%) |
Quoted 26 lines
Quoted 25 lines

This series applies on top of v6.9-rc1. All mm selftests pass. I haven't yet tested all VA size configs (although I don't anticipate any issues); I'll do this as part of followup.

Thanks,
Ryan

Ryan Roberts (3):
arm64: mm: Don't remap pgtables per- cont(pte|pmd) block
arm64: mm: Don't remap pgtables for allocate vs populate
arm64: mm: Lazily clear pte table mappings from fixmap

 arch/arm64/include/asm/fixmap.h  |   5 +-
 arch/arm64/include/asm/mmu.h     |   8 +
 arch/arm64/include/asm/pgtable.h |   4 -
 arch/arm64/kernel/cpufeature.c   |  10 +-
 arch/arm64/mm/fixmap.c           |  11 +
 arch/arm64/mm/mmu.c              | 364 +++++++++++++++++++++++--------
 include/linux/pgtable.h          |   8 +
 7 files changed, 307 insertions(+), 103 deletions(-)

--
2.25.1

Signature · 1 line
Eric Chanudet