aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2019-05-16 14:02:27 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2019-05-16 14:02:27 -0400
commitd396360acdf7e57edcd9e2d080343b0353d65d63 (patch)
tree735baf5df67bceb65af02728948494f2451cbefd
parentb2c3dda6f8f06d825b9b6099f57b906c774141c0 (diff)
parent9d8d0294e78a164d407133dea05caf4b84247d6a (diff)
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 fixes from Ingo Molnar: "Misc fixes and updates: - a handful of MDS documentation/comment updates - a cleanup related to hweight interfaces - a SEV guest fix for large pages - a kprobes LTO fix - and a final cleanup commit for vDSO HPET support removal" * 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/speculation/mds: Improve CPU buffer clear documentation x86/speculation/mds: Revert CPU buffer clear on double fault exit x86/kconfig: Disable CONFIG_GENERIC_HWEIGHT and remove __HAVE_ARCH_SW_HWEIGHT x86/mm: Do not use set_{pud, pmd}_safe() when splitting a large page x86/kprobes: Make trampoline_handler() global and visible x86/vdso: Remove hpet_page from vDSO
-rw-r--r--Documentation/x86/mds.rst44
-rw-r--r--arch/x86/Kconfig3
-rw-r--r--arch/x86/entry/vdso/vdso2c.c3
-rw-r--r--arch/x86/include/asm/arch_hweight.h2
-rw-r--r--arch/x86/include/asm/vdso.h1
-rw-r--r--arch/x86/kernel/kprobes/core.c2
-rw-r--r--arch/x86/kernel/traps.c8
-rw-r--r--arch/x86/mm/init_64.c144
-rw-r--r--arch/x86/mm/mem_encrypt.c10
-rw-r--r--arch/x86/mm/mm_internal.h3
-rw-r--r--lib/hweight.c4
11 files changed, 121 insertions, 103 deletions
diff --git a/Documentation/x86/mds.rst b/Documentation/x86/mds.rst
index 534e9baa4e1d..5d4330be200f 100644
--- a/Documentation/x86/mds.rst
+++ b/Documentation/x86/mds.rst
@@ -142,45 +142,13 @@ Mitigation points
142 mds_user_clear. 142 mds_user_clear.
143 143
144 The mitigation is invoked in prepare_exit_to_usermode() which covers 144 The mitigation is invoked in prepare_exit_to_usermode() which covers
145 most of the kernel to user space transitions. There are a few exceptions 145 all but one of the kernel to user space transitions. The exception
146 which are not invoking prepare_exit_to_usermode() on return to user 146 is when we return from a Non Maskable Interrupt (NMI), which is
147 space. These exceptions use the paranoid exit code. 147 handled directly in do_nmi().
148 148
149 - Non Maskable Interrupt (NMI): 149 (The reason that NMI is special is that prepare_exit_to_usermode() can
150 150 enable IRQs. In NMI context, NMIs are blocked, and we don't want to
151 Access to sensible data like keys, credentials in the NMI context is 151 enable IRQs with NMIs blocked.)
152 mostly theoretical: The CPU can do prefetching or execute a
153 misspeculated code path and thereby fetching data which might end up
154 leaking through a buffer.
155
156 But for mounting other attacks the kernel stack address of the task is
157 already valuable information. So in full mitigation mode, the NMI is
158 mitigated on the return from do_nmi() to provide almost complete
159 coverage.
160
161 - Double fault (#DF):
162
163 A double fault is usually fatal, but the ESPFIX workaround, which can
164 be triggered from user space through modify_ldt(2) is a recoverable
165 double fault. #DF uses the paranoid exit path, so explicit mitigation
166 in the double fault handler is required.
167
168 - Machine Check Exception (#MC):
169
170 Another corner case is a #MC which hits between the CPU buffer clear
171 invocation and the actual return to user. As this still is in kernel
172 space it takes the paranoid exit path which does not clear the CPU
173 buffers. So the #MC handler repopulates the buffers to some
174 extent. Machine checks are not reliably controllable and the window is
175 extremly small so mitigation would just tick a checkbox that this
176 theoretical corner case is covered. To keep the amount of special
177 cases small, ignore #MC.
178
179 - Debug Exception (#DB):
180
181 This takes the paranoid exit path only when the INT1 breakpoint is in
182 kernel space. #DB on a user space address takes the regular exit path,
183 so no extra mitigation required.
184 152
185 153
1862. C-State transition 1542. C-State transition
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 21e9f2fac04b..2bbbd4d1ba31 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -270,9 +270,6 @@ config GENERIC_BUG
270config GENERIC_BUG_RELATIVE_POINTERS 270config GENERIC_BUG_RELATIVE_POINTERS
271 bool 271 bool
272 272
273config GENERIC_HWEIGHT
274 def_bool y
275
276config ARCH_MAY_HAVE_PC_FDC 273config ARCH_MAY_HAVE_PC_FDC
277 def_bool y 274 def_bool y
278 depends on ISA_DMA_API 275 depends on ISA_DMA_API
diff --git a/arch/x86/entry/vdso/vdso2c.c b/arch/x86/entry/vdso/vdso2c.c
index 8e470b018512..3a4d8d4d39f8 100644
--- a/arch/x86/entry/vdso/vdso2c.c
+++ b/arch/x86/entry/vdso/vdso2c.c
@@ -73,14 +73,12 @@ const char *outfilename;
73enum { 73enum {
74 sym_vvar_start, 74 sym_vvar_start,
75 sym_vvar_page, 75 sym_vvar_page,
76 sym_hpet_page,
77 sym_pvclock_page, 76 sym_pvclock_page,
78 sym_hvclock_page, 77 sym_hvclock_page,
79}; 78};
80 79
81const int special_pages[] = { 80const int special_pages[] = {
82 sym_vvar_page, 81 sym_vvar_page,
83 sym_hpet_page,
84 sym_pvclock_page, 82 sym_pvclock_page,
85 sym_hvclock_page, 83 sym_hvclock_page,
86}; 84};
@@ -93,7 +91,6 @@ struct vdso_sym {
93struct vdso_sym required_syms[] = { 91struct vdso_sym required_syms[] = {
94 [sym_vvar_start] = {"vvar_start", true}, 92 [sym_vvar_start] = {"vvar_start", true},
95 [sym_vvar_page] = {"vvar_page", true}, 93 [sym_vvar_page] = {"vvar_page", true},
96 [sym_hpet_page] = {"hpet_page", true},
97 [sym_pvclock_page] = {"pvclock_page", true}, 94 [sym_pvclock_page] = {"pvclock_page", true},
98 [sym_hvclock_page] = {"hvclock_page", true}, 95 [sym_hvclock_page] = {"hvclock_page", true},
99 {"VDSO32_NOTE_MASK", true}, 96 {"VDSO32_NOTE_MASK", true},
diff --git a/arch/x86/include/asm/arch_hweight.h b/arch/x86/include/asm/arch_hweight.h
index fc0693569f7a..ba88edd0d58b 100644
--- a/arch/x86/include/asm/arch_hweight.h
+++ b/arch/x86/include/asm/arch_hweight.h
@@ -12,8 +12,6 @@
12#define REG_OUT "a" 12#define REG_OUT "a"
13#endif 13#endif
14 14
15#define __HAVE_ARCH_SW_HWEIGHT
16
17static __always_inline unsigned int __arch_hweight32(unsigned int w) 15static __always_inline unsigned int __arch_hweight32(unsigned int w)
18{ 16{
19 unsigned int res; 17 unsigned int res;
diff --git a/arch/x86/include/asm/vdso.h b/arch/x86/include/asm/vdso.h
index 27566e57e87d..230474e2ddb5 100644
--- a/arch/x86/include/asm/vdso.h
+++ b/arch/x86/include/asm/vdso.h
@@ -19,7 +19,6 @@ struct vdso_image {
19 long sym_vvar_start; /* Negative offset to the vvar area */ 19 long sym_vvar_start; /* Negative offset to the vvar area */
20 20
21 long sym_vvar_page; 21 long sym_vvar_page;
22 long sym_hpet_page;
23 long sym_pvclock_page; 22 long sym_pvclock_page;
24 long sym_hvclock_page; 23 long sym_hvclock_page;
25 long sym_VDSO32_NOTE_MASK; 24 long sym_VDSO32_NOTE_MASK;
diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index cf52ee0d8711..9e4fa2484d10 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -768,7 +768,7 @@ static struct kprobe kretprobe_kprobe = {
768/* 768/*
769 * Called from kretprobe_trampoline 769 * Called from kretprobe_trampoline
770 */ 770 */
771static __used void *trampoline_handler(struct pt_regs *regs) 771__used __visible void *trampoline_handler(struct pt_regs *regs)
772{ 772{
773 struct kprobe_ctlblk *kcb; 773 struct kprobe_ctlblk *kcb;
774 struct kretprobe_instance *ri = NULL; 774 struct kretprobe_instance *ri = NULL;
diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
index 7de466eb960b..8b6d03e55d2f 100644
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -58,7 +58,6 @@
58#include <asm/alternative.h> 58#include <asm/alternative.h>
59#include <asm/fpu/xstate.h> 59#include <asm/fpu/xstate.h>
60#include <asm/trace/mpx.h> 60#include <asm/trace/mpx.h>
61#include <asm/nospec-branch.h>
62#include <asm/mpx.h> 61#include <asm/mpx.h>
63#include <asm/vm86.h> 62#include <asm/vm86.h>
64#include <asm/umip.h> 63#include <asm/umip.h>
@@ -368,13 +367,6 @@ dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
368 regs->ip = (unsigned long)general_protection; 367 regs->ip = (unsigned long)general_protection;
369 regs->sp = (unsigned long)&gpregs->orig_ax; 368 regs->sp = (unsigned long)&gpregs->orig_ax;
370 369
371 /*
372 * This situation can be triggered by userspace via
373 * modify_ldt(2) and the return does not take the regular
374 * user space exit, so a CPU buffer clear is required when
375 * MDS mitigation is enabled.
376 */
377 mds_user_clear_cpu_buffers();
378 return; 370 return;
379 } 371 }
380#endif 372#endif
diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c
index 20d14254b686..62fc457f3849 100644
--- a/arch/x86/mm/init_64.c
+++ b/arch/x86/mm/init_64.c
@@ -58,6 +58,37 @@
58 58
59#include "ident_map.c" 59#include "ident_map.c"
60 60
61#define DEFINE_POPULATE(fname, type1, type2, init) \
62static inline void fname##_init(struct mm_struct *mm, \
63 type1##_t *arg1, type2##_t *arg2, bool init) \
64{ \
65 if (init) \
66 fname##_safe(mm, arg1, arg2); \
67 else \
68 fname(mm, arg1, arg2); \
69}
70
71DEFINE_POPULATE(p4d_populate, p4d, pud, init)
72DEFINE_POPULATE(pgd_populate, pgd, p4d, init)
73DEFINE_POPULATE(pud_populate, pud, pmd, init)
74DEFINE_POPULATE(pmd_populate_kernel, pmd, pte, init)
75
76#define DEFINE_ENTRY(type1, type2, init) \
77static inline void set_##type1##_init(type1##_t *arg1, \
78 type2##_t arg2, bool init) \
79{ \
80 if (init) \
81 set_##type1##_safe(arg1, arg2); \
82 else \
83 set_##type1(arg1, arg2); \
84}
85
86DEFINE_ENTRY(p4d, p4d, init)
87DEFINE_ENTRY(pud, pud, init)
88DEFINE_ENTRY(pmd, pmd, init)
89DEFINE_ENTRY(pte, pte, init)
90
91
61/* 92/*
62 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the 93 * NOTE: pagetable_init alloc all the fixmap pagetables contiguous on the
63 * physical space so we can cache the place of the first one and move 94 * physical space so we can cache the place of the first one and move
@@ -414,7 +445,7 @@ void __init cleanup_highmap(void)
414 */ 445 */
415static unsigned long __meminit 446static unsigned long __meminit
416phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end, 447phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
417 pgprot_t prot) 448 pgprot_t prot, bool init)
418{ 449{
419 unsigned long pages = 0, paddr_next; 450 unsigned long pages = 0, paddr_next;
420 unsigned long paddr_last = paddr_end; 451 unsigned long paddr_last = paddr_end;
@@ -432,7 +463,7 @@ phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
432 E820_TYPE_RAM) && 463 E820_TYPE_RAM) &&
433 !e820__mapped_any(paddr & PAGE_MASK, paddr_next, 464 !e820__mapped_any(paddr & PAGE_MASK, paddr_next,
434 E820_TYPE_RESERVED_KERN)) 465 E820_TYPE_RESERVED_KERN))
435 set_pte_safe(pte, __pte(0)); 466 set_pte_init(pte, __pte(0), init);
436 continue; 467 continue;
437 } 468 }
438 469
@@ -452,7 +483,7 @@ phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
452 pr_info(" pte=%p addr=%lx pte=%016lx\n", pte, paddr, 483 pr_info(" pte=%p addr=%lx pte=%016lx\n", pte, paddr,
453 pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte); 484 pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL).pte);
454 pages++; 485 pages++;
455 set_pte_safe(pte, pfn_pte(paddr >> PAGE_SHIFT, prot)); 486 set_pte_init(pte, pfn_pte(paddr >> PAGE_SHIFT, prot), init);
456 paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE; 487 paddr_last = (paddr & PAGE_MASK) + PAGE_SIZE;
457 } 488 }
458 489
@@ -468,7 +499,7 @@ phys_pte_init(pte_t *pte_page, unsigned long paddr, unsigned long paddr_end,
468 */ 499 */
469static unsigned long __meminit 500static unsigned long __meminit
470phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end, 501phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
471 unsigned long page_size_mask, pgprot_t prot) 502 unsigned long page_size_mask, pgprot_t prot, bool init)
472{ 503{
473 unsigned long pages = 0, paddr_next; 504 unsigned long pages = 0, paddr_next;
474 unsigned long paddr_last = paddr_end; 505 unsigned long paddr_last = paddr_end;
@@ -487,7 +518,7 @@ phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
487 E820_TYPE_RAM) && 518 E820_TYPE_RAM) &&
488 !e820__mapped_any(paddr & PMD_MASK, paddr_next, 519 !e820__mapped_any(paddr & PMD_MASK, paddr_next,
489 E820_TYPE_RESERVED_KERN)) 520 E820_TYPE_RESERVED_KERN))
490 set_pmd_safe(pmd, __pmd(0)); 521 set_pmd_init(pmd, __pmd(0), init);
491 continue; 522 continue;
492 } 523 }
493 524
@@ -496,7 +527,8 @@ phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
496 spin_lock(&init_mm.page_table_lock); 527 spin_lock(&init_mm.page_table_lock);
497 pte = (pte_t *)pmd_page_vaddr(*pmd); 528 pte = (pte_t *)pmd_page_vaddr(*pmd);
498 paddr_last = phys_pte_init(pte, paddr, 529 paddr_last = phys_pte_init(pte, paddr,
499 paddr_end, prot); 530 paddr_end, prot,
531 init);
500 spin_unlock(&init_mm.page_table_lock); 532 spin_unlock(&init_mm.page_table_lock);
501 continue; 533 continue;
502 } 534 }
@@ -524,19 +556,20 @@ phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
524 if (page_size_mask & (1<<PG_LEVEL_2M)) { 556 if (page_size_mask & (1<<PG_LEVEL_2M)) {
525 pages++; 557 pages++;
526 spin_lock(&init_mm.page_table_lock); 558 spin_lock(&init_mm.page_table_lock);
527 set_pte_safe((pte_t *)pmd, 559 set_pte_init((pte_t *)pmd,
528 pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT, 560 pfn_pte((paddr & PMD_MASK) >> PAGE_SHIFT,
529 __pgprot(pgprot_val(prot) | _PAGE_PSE))); 561 __pgprot(pgprot_val(prot) | _PAGE_PSE)),
562 init);
530 spin_unlock(&init_mm.page_table_lock); 563 spin_unlock(&init_mm.page_table_lock);
531 paddr_last = paddr_next; 564 paddr_last = paddr_next;
532 continue; 565 continue;
533 } 566 }
534 567
535 pte = alloc_low_page(); 568 pte = alloc_low_page();
536 paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot); 569 paddr_last = phys_pte_init(pte, paddr, paddr_end, new_prot, init);
537 570
538 spin_lock(&init_mm.page_table_lock); 571 spin_lock(&init_mm.page_table_lock);
539 pmd_populate_kernel_safe(&init_mm, pmd, pte); 572 pmd_populate_kernel_init(&init_mm, pmd, pte, init);
540 spin_unlock(&init_mm.page_table_lock); 573 spin_unlock(&init_mm.page_table_lock);
541 } 574 }
542 update_page_count(PG_LEVEL_2M, pages); 575 update_page_count(PG_LEVEL_2M, pages);
@@ -551,7 +584,7 @@ phys_pmd_init(pmd_t *pmd_page, unsigned long paddr, unsigned long paddr_end,
551 */ 584 */
552static unsigned long __meminit 585static unsigned long __meminit
553phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end, 586phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
554 unsigned long page_size_mask) 587 unsigned long page_size_mask, bool init)
555{ 588{
556 unsigned long pages = 0, paddr_next; 589 unsigned long pages = 0, paddr_next;
557 unsigned long paddr_last = paddr_end; 590 unsigned long paddr_last = paddr_end;
@@ -573,7 +606,7 @@ phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
573 E820_TYPE_RAM) && 606 E820_TYPE_RAM) &&
574 !e820__mapped_any(paddr & PUD_MASK, paddr_next, 607 !e820__mapped_any(paddr & PUD_MASK, paddr_next,
575 E820_TYPE_RESERVED_KERN)) 608 E820_TYPE_RESERVED_KERN))
576 set_pud_safe(pud, __pud(0)); 609 set_pud_init(pud, __pud(0), init);
577 continue; 610 continue;
578 } 611 }
579 612
@@ -583,7 +616,7 @@ phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
583 paddr_last = phys_pmd_init(pmd, paddr, 616 paddr_last = phys_pmd_init(pmd, paddr,
584 paddr_end, 617 paddr_end,
585 page_size_mask, 618 page_size_mask,
586 prot); 619 prot, init);
587 continue; 620 continue;
588 } 621 }
589 /* 622 /*
@@ -610,9 +643,10 @@ phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
610 if (page_size_mask & (1<<PG_LEVEL_1G)) { 643 if (page_size_mask & (1<<PG_LEVEL_1G)) {
611 pages++; 644 pages++;
612 spin_lock(&init_mm.page_table_lock); 645 spin_lock(&init_mm.page_table_lock);
613 set_pte_safe((pte_t *)pud, 646 set_pte_init((pte_t *)pud,
614 pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT, 647 pfn_pte((paddr & PUD_MASK) >> PAGE_SHIFT,
615 PAGE_KERNEL_LARGE)); 648 PAGE_KERNEL_LARGE),
649 init);
616 spin_unlock(&init_mm.page_table_lock); 650 spin_unlock(&init_mm.page_table_lock);
617 paddr_last = paddr_next; 651 paddr_last = paddr_next;
618 continue; 652 continue;
@@ -620,10 +654,10 @@ phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
620 654
621 pmd = alloc_low_page(); 655 pmd = alloc_low_page();
622 paddr_last = phys_pmd_init(pmd, paddr, paddr_end, 656 paddr_last = phys_pmd_init(pmd, paddr, paddr_end,
623 page_size_mask, prot); 657 page_size_mask, prot, init);
624 658
625 spin_lock(&init_mm.page_table_lock); 659 spin_lock(&init_mm.page_table_lock);
626 pud_populate_safe(&init_mm, pud, pmd); 660 pud_populate_init(&init_mm, pud, pmd, init);
627 spin_unlock(&init_mm.page_table_lock); 661 spin_unlock(&init_mm.page_table_lock);
628 } 662 }
629 663
@@ -634,14 +668,15 @@ phys_pud_init(pud_t *pud_page, unsigned long paddr, unsigned long paddr_end,
634 668
635static unsigned long __meminit 669static unsigned long __meminit
636phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end, 670phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end,
637 unsigned long page_size_mask) 671 unsigned long page_size_mask, bool init)
638{ 672{
639 unsigned long paddr_next, paddr_last = paddr_end; 673 unsigned long paddr_next, paddr_last = paddr_end;
640 unsigned long vaddr = (unsigned long)__va(paddr); 674 unsigned long vaddr = (unsigned long)__va(paddr);
641 int i = p4d_index(vaddr); 675 int i = p4d_index(vaddr);
642 676
643 if (!pgtable_l5_enabled()) 677 if (!pgtable_l5_enabled())
644 return phys_pud_init((pud_t *) p4d_page, paddr, paddr_end, page_size_mask); 678 return phys_pud_init((pud_t *) p4d_page, paddr, paddr_end,
679 page_size_mask, init);
645 680
646 for (; i < PTRS_PER_P4D; i++, paddr = paddr_next) { 681 for (; i < PTRS_PER_P4D; i++, paddr = paddr_next) {
647 p4d_t *p4d; 682 p4d_t *p4d;
@@ -657,39 +692,34 @@ phys_p4d_init(p4d_t *p4d_page, unsigned long paddr, unsigned long paddr_end,
657 E820_TYPE_RAM) && 692 E820_TYPE_RAM) &&
658 !e820__mapped_any(paddr & P4D_MASK, paddr_next, 693 !e820__mapped_any(paddr & P4D_MASK, paddr_next,
659 E820_TYPE_RESERVED_KERN)) 694 E820_TYPE_RESERVED_KERN))
660 set_p4d_safe(p4d, __p4d(0)); 695 set_p4d_init(p4d, __p4d(0), init);
661 continue; 696 continue;
662 } 697 }
663 698
664 if (!p4d_none(*p4d)) { 699 if (!p4d_none(*p4d)) {
665 pud = pud_offset(p4d, 0); 700 pud = pud_offset(p4d, 0);
666 paddr_last = phys_pud_init(pud, paddr, 701 paddr_last = phys_pud_init(pud, paddr, paddr_end,
667 paddr_end, 702 page_size_mask, init);
668 page_size_mask);
669 continue; 703 continue;
670 } 704 }
671 705
672 pud = alloc_low_page(); 706 pud = alloc_low_page();
673 paddr_last = phys_pud_init(pud, paddr, paddr_end, 707 paddr_last = phys_pud_init(pud, paddr, paddr_end,
674 page_size_mask); 708 page_size_mask, init);
675 709
676 spin_lock(&init_mm.page_table_lock); 710 spin_lock(&init_mm.page_table_lock);
677 p4d_populate_safe(&init_mm, p4d, pud); 711 p4d_populate_init(&init_mm, p4d, pud, init);
678 spin_unlock(&init_mm.page_table_lock); 712 spin_unlock(&init_mm.page_table_lock);
679 } 713 }
680 714
681 return paddr_last; 715 return paddr_last;
682} 716}
683 717
684/* 718static unsigned long __meminit
685 * Create page table mapping for the physical memory for specific physical 719__kernel_physical_mapping_init(unsigned long paddr_start,
686 * addresses. The virtual and physical addresses have to be aligned on PMD level 720 unsigned long paddr_end,
687 * down. It returns the last physical address mapped. 721 unsigned long page_size_mask,
688 */ 722 bool init)
689unsigned long __meminit
690kernel_physical_mapping_init(unsigned long paddr_start,
691 unsigned long paddr_end,
692 unsigned long page_size_mask)
693{ 723{
694 bool pgd_changed = false; 724 bool pgd_changed = false;
695 unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last; 725 unsigned long vaddr, vaddr_start, vaddr_end, vaddr_next, paddr_last;
@@ -709,19 +739,22 @@ kernel_physical_mapping_init(unsigned long paddr_start,
709 p4d = (p4d_t *)pgd_page_vaddr(*pgd); 739 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
710 paddr_last = phys_p4d_init(p4d, __pa(vaddr), 740 paddr_last = phys_p4d_init(p4d, __pa(vaddr),
711 __pa(vaddr_end), 741 __pa(vaddr_end),
712 page_size_mask); 742 page_size_mask,
743 init);
713 continue; 744 continue;
714 } 745 }
715 746
716 p4d = alloc_low_page(); 747 p4d = alloc_low_page();
717 paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end), 748 paddr_last = phys_p4d_init(p4d, __pa(vaddr), __pa(vaddr_end),
718 page_size_mask); 749 page_size_mask, init);
719 750
720 spin_lock(&init_mm.page_table_lock); 751 spin_lock(&init_mm.page_table_lock);
721 if (pgtable_l5_enabled()) 752 if (pgtable_l5_enabled())
722 pgd_populate_safe(&init_mm, pgd, p4d); 753 pgd_populate_init(&init_mm, pgd, p4d, init);
723 else 754 else
724 p4d_populate_safe(&init_mm, p4d_offset(pgd, vaddr), (pud_t *) p4d); 755 p4d_populate_init(&init_mm, p4d_offset(pgd, vaddr),
756 (pud_t *) p4d, init);
757
725 spin_unlock(&init_mm.page_table_lock); 758 spin_unlock(&init_mm.page_table_lock);
726 pgd_changed = true; 759 pgd_changed = true;
727 } 760 }
@@ -732,6 +765,37 @@ kernel_physical_mapping_init(unsigned long paddr_start,
732 return paddr_last; 765 return paddr_last;
733} 766}
734 767
768
769/*
770 * Create page table mapping for the physical memory for specific physical
771 * addresses. Note that it can only be used to populate non-present entries.
772 * The virtual and physical addresses have to be aligned on PMD level
773 * down. It returns the last physical address mapped.
774 */
775unsigned long __meminit
776kernel_physical_mapping_init(unsigned long paddr_start,
777 unsigned long paddr_end,
778 unsigned long page_size_mask)
779{
780 return __kernel_physical_mapping_init(paddr_start, paddr_end,
781 page_size_mask, true);
782}
783
784/*
785 * This function is similar to kernel_physical_mapping_init() above with the
786 * exception that it uses set_{pud,pmd}() instead of the set_{pud,pte}_safe()
787 * when updating the mapping. The caller is responsible to flush the TLBs after
788 * the function returns.
789 */
790unsigned long __meminit
791kernel_physical_mapping_change(unsigned long paddr_start,
792 unsigned long paddr_end,
793 unsigned long page_size_mask)
794{
795 return __kernel_physical_mapping_init(paddr_start, paddr_end,
796 page_size_mask, false);
797}
798
735#ifndef CONFIG_NUMA 799#ifndef CONFIG_NUMA
736void __init initmem_init(void) 800void __init initmem_init(void)
737{ 801{
diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c
index 385afa2b9e17..51f50a7a07ef 100644
--- a/arch/x86/mm/mem_encrypt.c
+++ b/arch/x86/mm/mem_encrypt.c
@@ -301,9 +301,13 @@ static int __init early_set_memory_enc_dec(unsigned long vaddr,
301 else 301 else
302 split_page_size_mask = 1 << PG_LEVEL_2M; 302 split_page_size_mask = 1 << PG_LEVEL_2M;
303 303
304 kernel_physical_mapping_init(__pa(vaddr & pmask), 304 /*
305 __pa((vaddr_end & pmask) + psize), 305 * kernel_physical_mapping_change() does not flush the TLBs, so
306 split_page_size_mask); 306 * a TLB flush is required after we exit from the for loop.
307 */
308 kernel_physical_mapping_change(__pa(vaddr & pmask),
309 __pa((vaddr_end & pmask) + psize),
310 split_page_size_mask);
307 } 311 }
308 312
309 ret = 0; 313 ret = 0;
diff --git a/arch/x86/mm/mm_internal.h b/arch/x86/mm/mm_internal.h
index 319bde386d5f..eeae142062ed 100644
--- a/arch/x86/mm/mm_internal.h
+++ b/arch/x86/mm/mm_internal.h
@@ -13,6 +13,9 @@ void early_ioremap_page_table_range_init(void);
13unsigned long kernel_physical_mapping_init(unsigned long start, 13unsigned long kernel_physical_mapping_init(unsigned long start,
14 unsigned long end, 14 unsigned long end,
15 unsigned long page_size_mask); 15 unsigned long page_size_mask);
16unsigned long kernel_physical_mapping_change(unsigned long start,
17 unsigned long end,
18 unsigned long page_size_mask);
16void zone_sizes_init(void); 19void zone_sizes_init(void);
17 20
18extern int after_bootmem; 21extern int after_bootmem;
diff --git a/lib/hweight.c b/lib/hweight.c
index 7660d88fd496..c94586b62551 100644
--- a/lib/hweight.c
+++ b/lib/hweight.c
@@ -10,7 +10,6 @@
10 * The Hamming Weight of a number is the total number of bits set in it. 10 * The Hamming Weight of a number is the total number of bits set in it.
11 */ 11 */
12 12
13#ifndef __HAVE_ARCH_SW_HWEIGHT
14unsigned int __sw_hweight32(unsigned int w) 13unsigned int __sw_hweight32(unsigned int w)
15{ 14{
16#ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER 15#ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER
@@ -27,7 +26,6 @@ unsigned int __sw_hweight32(unsigned int w)
27#endif 26#endif
28} 27}
29EXPORT_SYMBOL(__sw_hweight32); 28EXPORT_SYMBOL(__sw_hweight32);
30#endif
31 29
32unsigned int __sw_hweight16(unsigned int w) 30unsigned int __sw_hweight16(unsigned int w)
33{ 31{
@@ -46,7 +44,6 @@ unsigned int __sw_hweight8(unsigned int w)
46} 44}
47EXPORT_SYMBOL(__sw_hweight8); 45EXPORT_SYMBOL(__sw_hweight8);
48 46
49#ifndef __HAVE_ARCH_SW_HWEIGHT
50unsigned long __sw_hweight64(__u64 w) 47unsigned long __sw_hweight64(__u64 w)
51{ 48{
52#if BITS_PER_LONG == 32 49#if BITS_PER_LONG == 32
@@ -69,4 +66,3 @@ unsigned long __sw_hweight64(__u64 w)
69#endif 66#endif
70} 67}
71EXPORT_SYMBOL(__sw_hweight64); 68EXPORT_SYMBOL(__sw_hweight64);
72#endif