1 From bcb5ffbfba8c6c557ad536eb9084040b8e52923e Mon Sep 17 00:00:00 2001
2 From: Andy Lutomirski <luto@kernel.org>
3 Date: Mon, 4 Dec 2017 15:07:42 +0100
4 Subject: [PATCH 196/241] x86/mm/pti: Add functions to clone kernel PMDs
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
11 Provide infrastructure to:
13 - find a kernel PMD for a mapping which must be visible to user space for
14 the entry/exit code to work.
16 - walk an address range and share the kernel PMD with it.
18 This reuses a small part of the original KAISER patches to populate the
19 user space page table.
21 [ tglx: Made it universally usable so it can be used for any kind of shared
22 mapping. Add a mechanism to clear specific bits in the user space
23 visible PMD entry. Folded Andys simplifactions ]
25 Originally-by: Dave Hansen <dave.hansen@linux.intel.com>
26 Signed-off-by: Andy Lutomirski <luto@kernel.org>
27 Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
28 Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com>
29 Cc: Borislav Petkov <bp@alien8.de>
30 Cc: Borislav Petkov <bpetkov@suse.de>
31 Cc: Brian Gerst <brgerst@gmail.com>
32 Cc: Dave Hansen <dave.hansen@linux.intel.com>
33 Cc: David Laight <David.Laight@aculab.com>
34 Cc: Denys Vlasenko <dvlasenk@redhat.com>
35 Cc: Eduardo Valentin <eduval@amazon.com>
36 Cc: Greg KH <gregkh@linuxfoundation.org>
37 Cc: H. Peter Anvin <hpa@zytor.com>
38 Cc: Josh Poimboeuf <jpoimboe@redhat.com>
39 Cc: Juergen Gross <jgross@suse.com>
40 Cc: Linus Torvalds <torvalds@linux-foundation.org>
41 Cc: Peter Zijlstra <peterz@infradead.org>
42 Cc: Will Deacon <will.deacon@arm.com>
43 Cc: aliguori@amazon.com
44 Cc: daniel.gruss@iaik.tugraz.at
46 Cc: keescook@google.com
47 Signed-off-by: Ingo Molnar <mingo@kernel.org>
48 (cherry picked from commit 03f4424f348e8be95eb1bbeba09461cd7b867828)
49 Signed-off-by: Andy Whitcroft <apw@canonical.com>
50 Signed-off-by: Kleber Sacilotto de Souza <kleber.souza@canonical.com>
51 (cherry picked from commit 262ab7e8665e88581d20ccaefa107340457224bb)
52 Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
54 arch/x86/mm/pti.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
55 1 file changed, 127 insertions(+)
57 diff --git a/arch/x86/mm/pti.c b/arch/x86/mm/pti.c
58 index 69a983365392..d58bcee470fc 100644
59 --- a/arch/x86/mm/pti.c
60 +++ b/arch/x86/mm/pti.c
63 #define pr_fmt(fmt) "Kernel/User page tables isolation: " fmt
65 +/* Backporting helper */
66 +#ifndef __GFP_NOTRACK
67 +#define __GFP_NOTRACK 0
70 static void __init pti_print_if_insecure(const char *reason)
72 if (boot_cpu_has_bug(X86_BUG_CPU_INSECURE))
73 @@ -137,6 +142,128 @@ pgd_t __pti_set_user_pgd(pgd_t *pgdp, pgd_t pgd)
78 + * Walk the user copy of the page tables (optionally) trying to allocate
79 + * page table pages on the way down.
81 + * Returns a pointer to a P4D on success, or NULL on failure.
83 +static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
85 + pgd_t *pgd = kernel_to_user_pgdp(pgd_offset_k(address));
86 + gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
88 + if (address < PAGE_OFFSET) {
89 + WARN_ONCE(1, "attempt to walk user address\n");
93 + if (pgd_none(*pgd)) {
94 + unsigned long new_p4d_page = __get_free_page(gfp);
98 + if (pgd_none(*pgd)) {
99 + set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page)));
103 + free_page(new_p4d_page);
105 + BUILD_BUG_ON(pgd_large(*pgd) != 0);
107 + return p4d_offset(pgd, address);
111 + * Walk the user copy of the page tables (optionally) trying to allocate
112 + * page table pages on the way down.
114 + * Returns a pointer to a PMD on success, or NULL on failure.
116 +static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
118 + gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
119 + p4d_t *p4d = pti_user_pagetable_walk_p4d(address);
122 + BUILD_BUG_ON(p4d_large(*p4d) != 0);
123 + if (p4d_none(*p4d)) {
124 + unsigned long new_pud_page = __get_free_page(gfp);
128 + if (p4d_none(*p4d)) {
129 + set_p4d(p4d, __p4d(_KERNPG_TABLE | __pa(new_pud_page)));
133 + free_page(new_pud_page);
136 + pud = pud_offset(p4d, address);
137 + /* The user page tables do not use large mappings: */
138 + if (pud_large(*pud)) {
142 + if (pud_none(*pud)) {
143 + unsigned long new_pmd_page = __get_free_page(gfp);
147 + if (pud_none(*pud)) {
148 + set_pud(pud, __pud(_KERNPG_TABLE | __pa(new_pmd_page)));
152 + free_page(new_pmd_page);
155 + return pmd_offset(pud, address);
159 +pti_clone_pmds(unsigned long start, unsigned long end, pmdval_t clear)
161 + unsigned long addr;
164 + * Clone the populated PMDs which cover start to end. These PMD areas
167 + for (addr = start; addr < end; addr += PMD_SIZE) {
168 + pmd_t *pmd, *target_pmd;
173 + pgd = pgd_offset_k(addr);
174 + if (WARN_ON(pgd_none(*pgd)))
176 + p4d = p4d_offset(pgd, addr);
177 + if (WARN_ON(p4d_none(*p4d)))
179 + pud = pud_offset(p4d, addr);
180 + if (pud_none(*pud))
182 + pmd = pmd_offset(pud, addr);
183 + if (pmd_none(*pmd))
186 + target_pmd = pti_user_pagetable_walk_pmd(addr);
187 + if (WARN_ON(!target_pmd))
191 + * Copy the PMD. That is, the kernelmode and usermode
192 + * tables will share the last-level page tables of this
195 + *target_pmd = pmd_clear_flags(*pmd, clear);
200 * Initialize kernel page table isolation