kernel: add and enable MGLRU for Linux 5.15
[openwrt/staging/jow.git] / target / linux / generic / pending-5.15 / 020-01-mm-x86-add-CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG.patch
1 From f8b663bbfa30af5515e222fd74df20ea4e8393a2 Mon Sep 17 00:00:00 2001
2 From: Yu Zhao <yuzhao@google.com>
3 Date: Sat, 26 Sep 2020 21:17:18 -0600
4 Subject: [PATCH 02/10] mm: x86: add CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG
5
6 Some architectures support the accessed bit on non-leaf PMD entries,
7 e.g., x86_64 sets the accessed bit on a non-leaf PMD entry when using
8 it as part of linear address translation [1]. As an optimization, page
9 table walkers who are interested in the accessed bit can skip the PTEs
10 under a non-leaf PMD entry if the accessed bit is cleared on this PMD
11 entry.
12
13 Although an inline function may be preferable, this capability is
14 added as a configuration option to look consistent when used with the
15 existing macros.
16
17 [1]: Intel 64 and IA-32 Architectures Software Developer's Manual
18 Volume 3 (June 2021), section 4.8
19
20 Signed-off-by: Yu Zhao <yuzhao@google.com>
21 Tested-by: Konstantin Kharlamov <Hi-Angel@yandex.ru>
22 Change-Id: I1a17be3ae926f721f7b17ea1539e5c39e8c4f9a8
23 ---
24 arch/Kconfig | 9 +++++++++
25 arch/x86/Kconfig | 1 +
26 arch/x86/include/asm/pgtable.h | 3 ++-
27 arch/x86/mm/pgtable.c | 5 ++++-
28 include/linux/pgtable.h | 4 ++--
29 5 files changed, 18 insertions(+), 4 deletions(-)
30
31 --- a/arch/Kconfig
32 +++ b/arch/Kconfig
33 @@ -1295,6 +1295,15 @@ config ARCH_HAS_ELFCORE_COMPAT
34 config ARCH_HAS_PARANOID_L1D_FLUSH
35 bool
36
37 +config ARCH_HAS_NONLEAF_PMD_YOUNG
38 + bool
39 + depends on PGTABLE_LEVELS > 2
40 + help
41 + Architectures that select this are able to set the accessed bit on
42 + non-leaf PMD entries in addition to leaf PTE entries where pages are
43 + mapped. For them, page table walkers that clear the accessed bit may
44 + stop at non-leaf PMD entries if they do not see the accessed bit.
45 +
46 source "kernel/gcov/Kconfig"
47
48 source "scripts/gcc-plugins/Kconfig"
49 --- a/arch/x86/Kconfig
50 +++ b/arch/x86/Kconfig
51 @@ -84,6 +84,7 @@ config X86
52 select ARCH_HAS_PMEM_API if X86_64
53 select ARCH_HAS_PTE_DEVMAP if X86_64
54 select ARCH_HAS_PTE_SPECIAL
55 + select ARCH_HAS_NONLEAF_PMD_YOUNG if X86_64
56 select ARCH_HAS_UACCESS_FLUSHCACHE if X86_64
57 select ARCH_HAS_COPY_MC if X86_64
58 select ARCH_HAS_SET_MEMORY
59 --- a/arch/x86/include/asm/pgtable.h
60 +++ b/arch/x86/include/asm/pgtable.h
61 @@ -817,7 +817,8 @@ static inline unsigned long pmd_page_vad
62
63 static inline int pmd_bad(pmd_t pmd)
64 {
65 - return (pmd_flags(pmd) & ~_PAGE_USER) != _KERNPG_TABLE;
66 + return (pmd_flags(pmd) & ~(_PAGE_USER | _PAGE_ACCESSED)) !=
67 + (_KERNPG_TABLE & ~_PAGE_ACCESSED);
68 }
69
70 static inline unsigned long pages_to_mb(unsigned long npg)
71 --- a/arch/x86/mm/pgtable.c
72 +++ b/arch/x86/mm/pgtable.c
73 @@ -550,7 +550,7 @@ int ptep_test_and_clear_young(struct vm_
74 return ret;
75 }
76
77 -#ifdef CONFIG_TRANSPARENT_HUGEPAGE
78 +#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG)
79 int pmdp_test_and_clear_young(struct vm_area_struct *vma,
80 unsigned long addr, pmd_t *pmdp)
81 {
82 @@ -562,6 +562,9 @@ int pmdp_test_and_clear_young(struct vm_
83
84 return ret;
85 }
86 +#endif
87 +
88 +#ifdef CONFIG_TRANSPARENT_HUGEPAGE
89 int pudp_test_and_clear_young(struct vm_area_struct *vma,
90 unsigned long addr, pud_t *pudp)
91 {
92 --- a/include/linux/pgtable.h
93 +++ b/include/linux/pgtable.h
94 @@ -212,7 +212,7 @@ static inline int ptep_test_and_clear_yo
95 #endif
96
97 #ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
98 -#ifdef CONFIG_TRANSPARENT_HUGEPAGE
99 +#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG)
100 static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
101 unsigned long address,
102 pmd_t *pmdp)
103 @@ -233,7 +233,7 @@ static inline int pmdp_test_and_clear_yo
104 BUILD_BUG();
105 return 0;
106 }
107 -#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
108 +#endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG */
109 #endif
110
111 #ifndef __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH