|
| 1 | +From e3264035bdac67898d685423ffb2f3a9c3a5964a Mon Sep 17 00:00:00 2001 |
| 2 | + |
| 3 | +Date: Wed, 4 Aug 2021 01:31:34 -0600 |
| 4 | +Subject: [PATCH 01/14] mm: x86, arm64: add arch_has_hw_pte_young() |
| 5 | +MIME-Version: 1.0 |
| 6 | +Content-Type: text/plain; charset=UTF-8 |
| 7 | +Content-Transfer-Encoding: 8bit |
| 8 | + |
| 9 | +Some architectures automatically set the accessed bit in PTEs, e.g., |
| 10 | +x86 and arm64 v8.2. On architectures that do not have this capability, |
| 11 | +clearing the accessed bit in a PTE usually triggers a page fault |
| 12 | +following the TLB miss of this PTE (to emulate the accessed bit). |
| 13 | + |
| 14 | +Being aware of this capability can help make better decisions, e.g., |
| 15 | +whether to spread the work out over a period of time to reduce bursty |
| 16 | +page faults when trying to clear the accessed bit in many PTEs. |
| 17 | + |
| 18 | +Note that theoretically this capability can be unreliable, e.g., |
| 19 | +hotplugged CPUs might be different from builtin ones. Therefore it |
| 20 | +should not be used in architecture-independent code that involves |
| 21 | +correctness, e.g., to determine whether TLB flushes are required (in |
| 22 | +combination with the accessed bit). |
| 23 | + |
| 24 | +Signed-off-by: Yu Zhao < [email protected]> |
| 25 | +Reviewed-by: Barry Song < [email protected]> |
| 26 | +Acked-by: Brian Geffon < [email protected]> |
| 27 | +Acked-by: Jan Alexander Steffens (heftig) < [email protected]> |
| 28 | +Acked-by: Oleksandr Natalenko < [email protected]> |
| 29 | +Acked-by: Steven Barrett < [email protected]> |
| 30 | +Acked-by: Suleiman Souhlal < [email protected]> |
| 31 | +Acked-by: Will Deacon < [email protected]> |
| 32 | +Tested-by: Daniel Byrne < [email protected]> |
| 33 | +Tested-by: Donald Carr < [email protected]> |
| 34 | +Tested-by: Holger Hoffstätte < [email protected]> |
| 35 | +Tested-by: Konstantin Kharlamov < [email protected]> |
| 36 | +Tested-by: Shuang Zhai < [email protected]> |
| 37 | +Tested-by: Sofia Trinh < [email protected]> |
| 38 | +Tested-by: Vaibhav Jain < [email protected]> |
| 39 | +Change-Id: Ib49b44fb56df3333a2ff1fcc496fb1980b976e7a |
| 40 | +--- |
| 41 | + arch/arm64/include/asm/pgtable.h | 15 ++------------- |
| 42 | + arch/x86/include/asm/pgtable.h | 6 +++--- |
| 43 | + include/linux/pgtable.h | 13 +++++++++++++ |
| 44 | + mm/memory.c | 14 +------------- |
| 45 | + 4 files changed, 19 insertions(+), 29 deletions(-) |
| 46 | + |
| 47 | +diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h |
| 48 | +index b5df82aa99e6..71a1af42f0e8 100644 |
| 49 | +--- a/arch/arm64/include/asm/pgtable.h |
| 50 | ++++ b/arch/arm64/include/asm/pgtable.h |
| 51 | +@@ -1082,24 +1082,13 @@ static inline void update_mmu_cache(struct vm_area_struct *vma, |
| 52 | + * page after fork() + CoW for pfn mappings. We don't always have a |
| 53 | + * hardware-managed access flag on arm64. |
| 54 | + */ |
| 55 | +-static inline bool arch_faults_on_old_pte(void) |
| 56 | +-{ |
| 57 | +- /* The register read below requires a stable CPU to make any sense */ |
| 58 | +- cant_migrate(); |
| 59 | +- |
| 60 | +- return !cpu_has_hw_af(); |
| 61 | +-} |
| 62 | +-#define arch_faults_on_old_pte arch_faults_on_old_pte |
| 63 | ++#define arch_has_hw_pte_young cpu_has_hw_af |
| 64 | + |
| 65 | + /* |
| 66 | + * Experimentally, it's cheap to set the access flag in hardware and we |
| 67 | + * benefit from prefaulting mappings as 'old' to start with. |
| 68 | + */ |
| 69 | +-static inline bool arch_wants_old_prefaulted_pte(void) |
| 70 | +-{ |
| 71 | +- return !arch_faults_on_old_pte(); |
| 72 | +-} |
| 73 | +-#define arch_wants_old_prefaulted_pte arch_wants_old_prefaulted_pte |
| 74 | ++#define arch_wants_old_prefaulted_pte cpu_has_hw_af |
| 75 | + |
| 76 | + static inline bool pud_sect_supported(void) |
| 77 | + { |
| 78 | +diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h |
| 79 | +index 44e2d6f1dbaa..dc5f7d8ef68a 100644 |
| 80 | +--- a/arch/x86/include/asm/pgtable.h |
| 81 | ++++ b/arch/x86/include/asm/pgtable.h |
| 82 | +@@ -1431,10 +1431,10 @@ static inline bool arch_has_pfn_modify_check(void) |
| 83 | + return boot_cpu_has_bug(X86_BUG_L1TF); |
| 84 | + } |
| 85 | + |
| 86 | +-#define arch_faults_on_old_pte arch_faults_on_old_pte |
| 87 | +-static inline bool arch_faults_on_old_pte(void) |
| 88 | ++#define arch_has_hw_pte_young arch_has_hw_pte_young |
| 89 | ++static inline bool arch_has_hw_pte_young(void) |
| 90 | + { |
| 91 | +- return false; |
| 92 | ++ return true; |
| 93 | + } |
| 94 | + |
| 95 | + #ifdef CONFIG_PAGE_TABLE_CHECK |
| 96 | +diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h |
| 97 | +index 014ee8f0fbaa..95f408df4695 100644 |
| 98 | +--- a/include/linux/pgtable.h |
| 99 | ++++ b/include/linux/pgtable.h |
| 100 | +@@ -260,6 +260,19 @@ static inline int pmdp_clear_flush_young(struct vm_area_struct *vma, |
| 101 | + #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 102 | + #endif |
| 103 | + |
| 104 | ++#ifndef arch_has_hw_pte_young |
| 105 | ++/* |
| 106 | ++ * Return whether the accessed bit is supported on the local CPU. |
| 107 | ++ * |
| 108 | ++ * This stub assumes accessing through an old PTE triggers a page fault. |
| 109 | ++ * Architectures that automatically set the access bit should overwrite it. |
| 110 | ++ */ |
| 111 | ++static inline bool arch_has_hw_pte_young(void) |
| 112 | ++{ |
| 113 | ++ return false; |
| 114 | ++} |
| 115 | ++#endif |
| 116 | ++ |
| 117 | + #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR |
| 118 | + static inline pte_t ptep_get_and_clear(struct mm_struct *mm, |
| 119 | + unsigned long address, |
| 120 | +diff --git a/mm/memory.c b/mm/memory.c |
| 121 | +index a78814413ac0..68294ce1cb06 100644 |
| 122 | +--- a/mm/memory.c |
| 123 | ++++ b/mm/memory.c |
| 124 | +@@ -125,18 +125,6 @@ int randomize_va_space __read_mostly = |
| 125 | + 2; |
| 126 | + #endif |
| 127 | + |
| 128 | +-#ifndef arch_faults_on_old_pte |
| 129 | +-static inline bool arch_faults_on_old_pte(void) |
| 130 | +-{ |
| 131 | +- /* |
| 132 | +- * Those arches which don't have hw access flag feature need to |
| 133 | +- * implement their own helper. By default, "true" means pagefault |
| 134 | +- * will be hit on old pte. |
| 135 | +- */ |
| 136 | +- return true; |
| 137 | +-} |
| 138 | +-#endif |
| 139 | +- |
| 140 | + #ifndef arch_wants_old_prefaulted_pte |
| 141 | + static inline bool arch_wants_old_prefaulted_pte(void) |
| 142 | + { |
| 143 | +@@ -2870,7 +2858,7 @@ static inline bool __wp_page_copy_user(struct page *dst, struct page *src, |
| 144 | + * On architectures with software "accessed" bits, we would |
| 145 | + * take a double page fault, so mark it accessed here. |
| 146 | + */ |
| 147 | +- if (arch_faults_on_old_pte() && !pte_young(vmf->orig_pte)) { |
| 148 | ++ if (!arch_has_hw_pte_young() && !pte_young(vmf->orig_pte)) { |
| 149 | + pte_t entry; |
| 150 | + |
| 151 | + vmf->pte = pte_offset_map_lock(mm, vmf->pmd, addr, &vmf->ptl); |
| 152 | +-- |
| 153 | +2.17.1 |
| 154 | + |
0 commit comments