History

Merge branch 'x86-mm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull x86 mm updates from Ingo Molnar: "The changes in here are: - text_poke() fixes and an extensive set of executability lockdowns, to (hopefully) eliminate the last residual circumstances under which we are using W|X mappings even temporarily on x86 kernels. This required a broad range of surgery in text patching facilities, module loading, trampoline handling and other bits. - tweak page fault messages to be more informative and more structured. - remove DISCONTIGMEM support on x86-32 and make SPARSEMEM the default. - reduce KASLR granularity on 5-level paging kernels from 512 GB to 1 GB. - misc other changes and updates" * 'x86-mm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (36 commits) x86/mm: Initialize PGD cache during mm initialization x86/alternatives: Add comment about module removal races x86/kprobes: Use vmalloc special flag x86/ftrace: Use vmalloc special flag bpf: Use vmalloc special flag modules: Use vmalloc special flag mm/vmalloc: Add flag for freeing of special permsissions mm/hibernation: Make hibernation handle unmapped pages x86/mm/cpa: Add set_direct_map_*() functions x86/alternatives: Remove the return value of text_poke_*() x86/jump-label: Remove support for custom text poker x86/modules: Avoid breaking W^X while loading modules x86/kprobes: Set instruction page as executable x86/ftrace: Set trampoline pages as executable x86/kgdb: Avoid redundant comparison of patched code x86/alternatives: Use temporary mm for text poking x86/alternatives: Initialize temporary mm for patching fork: Provide a function for copying init_mm uprobes: Initialize uprobes earlier x86/mm: Save debug registers when loading a temporary mm ...

Linus Torvalds committed 7 years ago

Commit 0bc40e52 parents e913c4a caa841340 files · +711 −343Browse files

Unified · Split

40 files
  1. Documentation/x86/x86_64/mm.txt +3 −3
  2. arch/Kconfig +4 −0
  3. arch/x86/Kconfig +4 −7
  4. arch/x86/include/asm/fixmap.h +0 −2
  5. arch/x86/include/asm/mmu_context.h +56 −0
  6. arch/x86/include/asm/pgtable.h +3 −0
  7. arch/x86/include/asm/set_memory.h +3 −0
  8. arch/x86/include/asm/text-patching.h +5 −2
  9. arch/x86/include/asm/tlbflush.h +2 −0
  10. arch/x86/kernel/alternative.c +154 −47
  11. arch/x86/kernel/ftrace.c +14 −8
  12. arch/x86/kernel/jump_label.c +10 −11
  13. arch/x86/kernel/kgdb.c +7 −18
  14. arch/x86/kernel/kprobes/core.c +15 −4
  15. arch/x86/kernel/module.c +1 −1
  16. arch/x86/kernel/vmlinux.lds.S +3 −3
  17. arch/x86/mm/fault.c +18 −37
  18. arch/x86/mm/init.c +37 −0
  19. arch/x86/mm/kaslr.c +39 −55
  20. arch/x86/mm/pageattr.c +10 −6
  21. arch/x86/mm/pgtable.c +6 −4
  22. arch/x86/mm/tlb.c +82 −34
  23. arch/x86/xen/mmu_pv.c +0 −2
  24. include/asm-generic/pgtable.h +2 −0
  25. include/asm-generic/tlb.h +9 −0
  26. include/linux/filter.h +4 −14
  27. include/linux/mm.h +6 −12
  28. include/linux/sched/task.h +1 −0
  29. include/linux/set_memory.h +11 −0
  30. include/linux/uprobes.h +5 −0
  31. include/linux/vmalloc.h +15 −0
  32. init/main.c +6 −0
  33. kernel/bpf/core.c +0 −1
  34. kernel/events/uprobes.c +3 −5
  35. kernel/fork.c +19 −6
  36. kernel/module.c +44 −38
  37. kernel/power/snapshot.c +3 −2
  38. kernel/trace/bpf_trace.c +8 −0
  39. mm/page_alloc.c +5 −2
  40. mm/vmalloc.c +94 −19
Documentation/x86/x86_64/mm.txt+3−3
@@ -72,7 +72,7 @@ Complete virtual memory map with 5-level page tables · 71 unmodified lines
7272 Notes:
7373
7474 - With 56-bit addresses, user-space memory gets expanded by a factor of 512x,
75− from 0.125 PB to 64 PB. All kernel mappings shift down to the -64 PT starting
75+ from 0.125 PB to 64 PB. All kernel mappings shift down to the -64 PB starting
7676 offset and many of the regions expand to support the much larger physical
7777 memory supported.
7878
@@ -83,7 +83,7 @@ Notes: · 4 unmodified lines
8383 0000000000000000 | 0 | 00ffffffffffffff | 64 PB | user-space virtual memory, different per mm
8484 __________________|____________|__________________|_________|___________________________________________________________
8585 | | | |
86− 0000800000000000 | +64 PB | ffff7fffffffffff | ~16K PB | ... huge, still almost 64 bits wide hole of non-canonical
86+ 0100000000000000 | +64 PB | feffffffffffffff | ~16K PB | ... huge, still almost 64 bits wide hole of non-canonical
8787 | | | | virtual memory addresses up to the -64 PB
8888 | | | | starting offset of kernel mappings.
8989 __________________|____________|__________________|_________|___________________________________________________________
@@ -99,7 +99,7 @@ ____________________________________________________________|___________________ · 9 unmodified lines
9999 ffd2000000000000 | -11.5 PB | ffd3ffffffffffff | 0.5 PB | ... unused hole
100100 ffd4000000000000 | -11 PB | ffd5ffffffffffff | 0.5 PB | virtual memory map (vmemmap_base)
101101 ffd6000000000000 | -10.5 PB | ffdeffffffffffff | 2.25 PB | ... unused hole
102− ffdf000000000000 | -8.25 PB | fffffdffffffffff | ~8 PB | KASAN shadow memory
102+ ffdf000000000000 | -8.25 PB | fffffbffffffffff | ~8 PB | KASAN shadow memory
103103 __________________|____________|__________________|_________|____________________________________________________________
104104 |
105105 | Identical layout to the 47-bit one from here on:
arch/Kconfig+4−0
@@ -249,6 +249,10 @@ config ARCH_HAS_FORTIFY_SOURCE · 248 unmodified lines
249249 config ARCH_HAS_SET_MEMORY
250250 bool
251251
252+# Select if arch has all set_direct_map_invalid/default() functions
253+config ARCH_HAS_SET_DIRECT_MAP
254+ bool
255+
252256 # Select if arch init_task must go in the __init_task_data section
253257 config ARCH_TASK_STRUCT_ON_STACK
254258 bool
arch/x86/Kconfig+4−7
@@ -65,6 +65,7 @@ config X86 · 64 unmodified lines
6565 select ARCH_HAS_UACCESS_FLUSHCACHE if X86_64
6666 select ARCH_HAS_UACCESS_MCSAFE if X86_64 && X86_MCE
6767 select ARCH_HAS_SET_MEMORY
68+ select ARCH_HAS_SET_DIRECT_MAP
6869 select ARCH_HAS_STRICT_KERNEL_RWX
6970 select ARCH_HAS_STRICT_MODULE_RWX
7071 select ARCH_HAS_SYNC_CORE_BEFORE_USERMODE
@@ -1592,12 +1593,9 @@ config ARCH_FLATMEM_ENABLE · 1521 unmodified lines
15921593 depends on X86_32 && !NUMA
15931594
15941595 config ARCH_DISCONTIGMEM_ENABLE
1595− def_bool y
1596− depends on NUMA && X86_32
1597−
1598−config ARCH_DISCONTIGMEM_DEFAULT
1599− def_bool y
1596+ def_bool n
16001597 depends on NUMA && X86_32
1598+ depends on BROKEN
16011599
16021600 config ARCH_SPARSEMEM_ENABLE
16031601 def_bool y
@@ -1606,8 +1604,7 @@ config ARCH_SPARSEMEM_ENABLE · 2 unmodified lines
16061604 select SPARSEMEM_VMEMMAP_ENABLE if X86_64
16071605
16081606 config ARCH_SPARSEMEM_DEFAULT
1609− def_bool y
1610− depends on X86_64
1607+ def_bool X86_64 || (NUMA && X86_32)
16111608
16121609 config ARCH_SELECT_MEMORY_MODEL
16131610 def_bool y
arch/x86/include/asm/fixmap.h+0−2
@@ -103,8 +103,6 @@ enum fixed_addresses { · 102 unmodified lines
103103 #ifdef CONFIG_PARAVIRT
104104 FIX_PARAVIRT_BOOTMAP,
105105 #endif
106− FIX_TEXT_POKE1, /* reserve 2 pages for text_poke() */
107− FIX_TEXT_POKE0, /* first page is last, because allocation is backward */
108106 #ifdef CONFIG_X86_INTEL_MID
109107 FIX_LNW_VRTC,
110108 #endif
arch/x86/include/asm/mmu_context.h+56−0
@@ -13,6 +13,7 @@ · 12 unmodified lines
1313 #include <asm/tlbflush.h>
1414 #include <asm/paravirt.h>
1515 #include <asm/mpx.h>
16+#include <asm/debugreg.h>
1617
1718 extern atomic64_t last_mm_ctx_id;
1819
@@ -356,4 +357,59 @@ static inline unsigned long __get_current_cr3_fast(void) · 337 unmodified lines
356357 return cr3;
357358 }
358359
360+typedef struct {
361+ struct mm_struct *mm;
362+} temp_mm_state_t;
363+
364+/*
365+ * Using a temporary mm allows to set temporary mappings that are not accessible
366+ * by other CPUs. Such mappings are needed to perform sensitive memory writes
367+ * that override the kernel memory protections (e.g., W^X), without exposing the
368+ * temporary page-table mappings that are required for these write operations to
369+ * other CPUs. Using a temporary mm also allows to avoid TLB shootdowns when the
370+ * mapping is torn down.
371+ *
372+ * Context: The temporary mm needs to be used exclusively by a single core. To
373+ * harden security IRQs must be disabled while the temporary mm is
374+ * loaded, thereby preventing interrupt handler bugs from overriding
375+ * the kernel memory protection.
376+ */
377+static inline temp_mm_state_t use_temporary_mm(struct mm_struct *mm)
378+{
379+ temp_mm_state_t temp_state;
380+
381+ lockdep_assert_irqs_disabled();
382+ temp_state.mm = this_cpu_read(cpu_tlbstate.loaded_mm);
383+ switch_mm_irqs_off(NULL, mm, current);
384+
385+ /*
386+ * If breakpoints are enabled, disable them while the temporary mm is
387+ * used. Userspace might set up watchpoints on addresses that are used
388+ * in the temporary mm, which would lead to wrong signals being sent or
389+ * crashes.
390+ *
391+ * Note that breakpoints are not disabled selectively, which also causes
392+ * kernel breakpoints (e.g., perf's) to be disabled. This might be
393+ * undesirable, but still seems reasonable as the code that runs in the
394+ * temporary mm should be short.
395+ */
396+ if (hw_breakpoint_active())
397+ hw_breakpoint_disable();
398+
399+ return temp_state;
400+}
401+
402+static inline void unuse_temporary_mm(temp_mm_state_t prev_state)
403+{
404+ lockdep_assert_irqs_disabled();
405+ switch_mm_irqs_off(NULL, prev_state.mm, current);
406+
407+ /*
408+ * Restore the breakpoints if they were disabled before the temporary mm
409+ * was loaded.
410+ */
411+ if (hw_breakpoint_active())
412+ hw_breakpoint_restore();
413+}
414+
359415 #endif /* _ASM_X86_MMU_CONTEXT_H */
arch/x86/include/asm/pgtable.h+3−0
@@ -1021,6 +1021,9 @@ static inline void __meminit init_trampoline_default(void) · 1020 unmodified lines
10211021 /* Default trampoline pgd value */
10221022 trampoline_pgd_entry = init_top_pgt[pgd_index(__PAGE_OFFSET)];
10231023 }
1024+
1025+void __init poking_init(void);
1026+
10241027 # ifdef CONFIG_RANDOMIZE_MEMORY
10251028 void __meminit init_trampoline(void);
10261029 # else
arch/x86/include/asm/set_memory.h+3−0
@@ -85,6 +85,9 @@ int set_pages_nx(struct page *page, int numpages); · 84 unmodified lines
8585 int set_pages_ro(struct page *page, int numpages);
8686 int set_pages_rw(struct page *page, int numpages);
8787
88+int set_direct_map_invalid_noflush(struct page *page);
89+int set_direct_map_default_noflush(struct page *page);
90+
8891 extern int kernel_set_to_readonly;
8992 void set_kernel_text_rw(void);
9093 void set_kernel_text_ro(void);
arch/x86/include/asm/text-patching.h+5−2
@@ -18,7 +18,7 @@ static inline void apply_paravirt(struct paravirt_patch_site *start, · 17 unmodified lines
1818 #define __parainstructions_end NULL
1919 #endif
2020
21−extern void *text_poke_early(void *addr, const void *opcode, size_t len);
21+extern void text_poke_early(void *addr, const void *opcode, size_t len);
2222
2323 /*
2424 * Clear and restore the kernel write-protection flag on the local CPU.
@@ -35,8 +35,11 @@ extern void *text_poke_early(void *addr, const void *opcode, size_t len); · 10 unmodified lines
3535 * inconsistent instruction while you patch.
3636 */
3737 extern void *text_poke(void *addr, const void *opcode, size_t len);
38+extern void *text_poke_kgdb(void *addr, const void *opcode, size_t len);
3839 extern int poke_int3_handler(struct pt_regs *regs);
39−extern void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
40+extern void text_poke_bp(void *addr, const void *opcode, size_t len, void *handler);
4041 extern int after_bootmem;
42+extern __ro_after_init struct mm_struct *poking_mm;
43+extern __ro_after_init unsigned long poking_addr;
4144
4245 #endif /* _ASM_X86_TEXT_PATCHING_H */
arch/x86/include/asm/tlbflush.h+2−0
@@ -274,6 +274,8 @@ static inline bool nmi_uaccess_okay(void) · 273 unmodified lines
274274 return true;
275275 }
276276
277+#define nmi_uaccess_okay nmi_uaccess_okay
278+
277279 /* Initialize cr4 shadow for this CPU. */
278280 static inline void cr4_init_shadow(void)
279281 {
arch/x86/kernel/alternative.c+154−47
@@ -12,6 +12,7 @@ · 11 unmodified lines
1212 #include <linux/slab.h>
1313 #include <linux/kdebug.h>
1414 #include <linux/kprobes.h>
15+#include <linux/mmu_context.h>
1516 #include <asm/text-patching.h>
1617 #include <asm/alternative.h>
1718 #include <asm/sections.h>
@@ -264,7 +265,7 @@ static void __init_or_module add_nops(void *insns, unsigned int len) · 246 unmodified lines
264265
265266 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
266267 extern s32 __smp_locks[], __smp_locks_end[];
267−void *text_poke_early(void *addr, const void *opcode, size_t len);
268+void text_poke_early(void *addr, const void *opcode, size_t len);
268269
269270 /*
270271 * Are we looking at a near JMP with a 1 or 4-byte displacement.
@@ -666,16 +667,136 @@ void __init alternative_instructions(void) · 395 unmodified lines
666667 * instructions. And on the local CPU you need to be protected again NMI or MCE
667668 * handlers seeing an inconsistent instruction while you patch.
668669 */
669−void *__init_or_module text_poke_early(void *addr, const void *opcode,
670− size_t len)
670+void __init_or_module text_poke_early(void *addr, const void *opcode,
671+ size_t len)
671672 {
672673 unsigned long flags;
674+
675+ if (boot_cpu_has(X86_FEATURE_NX) &&
676+ is_module_text_address((unsigned long)addr)) {
677+ /*
678+ * Modules text is marked initially as non-executable, so the
679+ * code cannot be running and speculative code-fetches are
680+ * prevented. Just change the code.
681+ */
682+ memcpy(addr, opcode, len);
683+ } else {
684+ local_irq_save(flags);
685+ memcpy(addr, opcode, len);
686+ local_irq_restore(flags);
687+ sync_core();
688+
689+ /*
690+ * Could also do a CLFLUSH here to speed up CPU recovery; but
691+ * that causes hangs on some VIA CPUs.
692+ */
693+ }
694+}
695+
696+__ro_after_init struct mm_struct *poking_mm;
697+__ro_after_init unsigned long poking_addr;
698+
699+static void *__text_poke(void *addr, const void *opcode, size_t len)
700+{
701+ bool cross_page_boundary = offset_in_page(addr) + len > PAGE_SIZE;
702+ struct page *pages[2] = {NULL};
703+ temp_mm_state_t prev;
704+ unsigned long flags;
705+ pte_t pte, *ptep;
706+ spinlock_t *ptl;
707+ pgprot_t pgprot;
708+
709+ /*
710+ * While boot memory allocator is running we cannot use struct pages as
711+ * they are not yet initialized. There is no way to recover.
712+ */
713+ BUG_ON(!after_bootmem);
714+
715+ if (!core_kernel_text((unsigned long)addr)) {
716+ pages[0] = vmalloc_to_page(addr);
717+ if (cross_page_boundary)
718+ pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
719+ } else {
720+ pages[0] = virt_to_page(addr);
721+ WARN_ON(!PageReserved(pages[0]));
722+ if (cross_page_boundary)
723+ pages[1] = virt_to_page(addr + PAGE_SIZE);
724+ }
725+ /*
726+ * If something went wrong, crash and burn since recovery paths are not
727+ * implemented.
728+ */
729+ BUG_ON(!pages[0] || (cross_page_boundary && !pages[1]));
730+
673731 local_irq_save(flags);
674− memcpy(addr, opcode, len);
732+
733+ /*
734+ * Map the page without the global bit, as TLB flushing is done with
735+ * flush_tlb_mm_range(), which is intended for non-global PTEs.
736+ */
737+ pgprot = __pgprot(pgprot_val(PAGE_KERNEL) & ~_PAGE_GLOBAL);
738+
739+ /*
740+ * The lock is not really needed, but this allows to avoid open-coding.
741+ */
742+ ptep = get_locked_pte(poking_mm, poking_addr, &ptl);
743+
744+ /*
745+ * This must not fail; preallocated in poking_init().
746+ */
747+ VM_BUG_ON(!ptep);
748+
749+ pte = mk_pte(pages[0], pgprot);
750+ set_pte_at(poking_mm, poking_addr, ptep, pte);
751+
752+ if (cross_page_boundary) {
753+ pte = mk_pte(pages[1], pgprot);
754+ set_pte_at(poking_mm, poking_addr + PAGE_SIZE, ptep + 1, pte);
755+ }
756+
757+ /*
758+ * Loading the temporary mm behaves as a compiler barrier, which
759+ * guarantees that the PTE will be set at the time memcpy() is done.
760+ */
761+ prev = use_temporary_mm(poking_mm);
762+
763+ kasan_disable_current();
764+ memcpy((u8 *)poking_addr + offset_in_page(addr), opcode, len);
765+ kasan_enable_current();
766+
767+ /*
768+ * Ensure that the PTE is only cleared after the instructions of memcpy
769+ * were issued by using a compiler barrier.
770+ */
771+ barrier();
772+
773+ pte_clear(poking_mm, poking_addr, ptep);
774+ if (cross_page_boundary)
775+ pte_clear(poking_mm, poking_addr + PAGE_SIZE, ptep + 1);
776+
777+ /*
778+ * Loading the previous page-table hierarchy requires a serializing
779+ * instruction that already allows the core to see the updated version.
780+ * Xen-PV is assumed to serialize execution in a similar manner.
781+ */
782+ unuse_temporary_mm(prev);
783+
784+ /*
785+ * Flushing the TLB might involve IPIs, which would require enabled
786+ * IRQs, but not if the mm is not used, as it is in this point.
787+ */
788+ flush_tlb_mm_range(poking_mm, poking_addr, poking_addr +
789+ (cross_page_boundary ? 2 : 1) * PAGE_SIZE,
790+ PAGE_SHIFT, false);
791+
792+ /*
793+ * If the text does not match what we just wrote then something is
794+ * fundamentally screwy; there's nothing we can really do about that.
795+ */
796+ BUG_ON(memcmp(addr, opcode, len));
797+
798+ pte_unmap_unlock(ptep, ptl);
675799 local_irq_restore(flags);
676− sync_core();
677− /* Could also do a CLFLUSH here to speed up CPU recovery; but
678− that causes hangs on some VIA CPUs. */
679800 return addr;
680801 }
681802
@@ -689,48 +810,36 @@ void *__init_or_module text_poke_early(void *addr, const void *opcode, · 7 unmodified lines
689810 * It means the size must be writable atomically and the address must be aligned
690811 * in a way that permits an atomic write. It also makes sure we fit on a single
691812 * page.
813+ *
814+ * Note that the caller must ensure that if the modified code is part of a
815+ * module, the module would not be removed during poking. This can be achieved
816+ * by registering a module notifier, and ordering module removal and patching
817+ * trough a mutex.
692818 */
693819 void *text_poke(void *addr, const void *opcode, size_t len)
694820 {
695− unsigned long flags;
696− char *vaddr;
697− struct page *pages[2];
698− int i;
699−
700− /*
701− * While boot memory allocator is runnig we cannot use struct
702− * pages as they are not yet initialized.
703− */
704− BUG_ON(!after_bootmem);
705−
706821 lockdep_assert_held(&text_mutex);
707822
708− if (!core_kernel_text((unsigned long)addr)) {
709− pages[0] = vmalloc_to_page(addr);
710− pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
711− } else {
712− pages[0] = virt_to_page(addr);
713− WARN_ON(!PageReserved(pages[0]));
714− pages[1] = virt_to_page(addr + PAGE_SIZE);
715− }
716− BUG_ON(!pages[0]);
717− local_irq_save(flags);
718− set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
719− if (pages[1])
720− set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
721− vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
722− memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
723− clear_fixmap(FIX_TEXT_POKE0);
724− if (pages[1])
725− clear_fixmap(FIX_TEXT_POKE1);
726− local_flush_tlb();
727− sync_core();
728− /* Could also do a CLFLUSH here to speed up CPU recovery; but
729− that causes hangs on some VIA CPUs. */
730− for (i = 0; i < len; i++)
731− BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
732− local_irq_restore(flags);
733− return addr;
823+ return __text_poke(addr, opcode, len);
824+}
825+
826+/**
827+ * text_poke_kgdb - Update instructions on a live kernel by kgdb
828+ * @addr: address to modify
829+ * @opcode: source of the copy
830+ * @len: length to copy
831+ *
832+ * Only atomic text poke/set should be allowed when not doing early patching.
833+ * It means the size must be writable atomically and the address must be aligned
834+ * in a way that permits an atomic write. It also makes sure we fit on a single
835+ * page.
836+ *
837+ * Context: should only be used by kgdb, which ensures no other core is running,
838+ * despite the fact it does not hold the text_mutex.
839+ */
840+void *text_poke_kgdb(void *addr, const void *opcode, size_t len)
841+{
842+ return __text_poke(addr, opcode, len);
734843 }
735844
736845 static void do_sync_core(void *info)
@@ -788,7 +897,7 @@ NOKPROBE_SYMBOL(poke_int3_handler); · 51 unmodified lines
788897 * replacing opcode
789898 * - sync cores
790899 */
791−void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
900+void text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
792901 {
793902 unsigned char int3 = 0xcc;
794903
@@ -830,7 +939,5 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler) · 35 unmodified lines
830939 * the writing of the new instruction.
831940 */
832941 bp_patching_in_progress = false;
833−
834− return addr;
835942 }
836943
arch/x86/kernel/ftrace.c+14−8
@@ -678,12 +678,8 @@ static inline void *alloc_tramp(unsigned long size) · 677 unmodified lines
678678 {
679679 return module_alloc(size);
680680 }
681−static inline void tramp_free(void *tramp, int size)
681+static inline void tramp_free(void *tramp)
682682 {
683− int npages = PAGE_ALIGN(size) >> PAGE_SHIFT;
684−
685− set_memory_nx((unsigned long)tramp, npages);
686− set_memory_rw((unsigned long)tramp, npages);
687683 module_memfree(tramp);
688684 }
689685 #else
@@ -692,7 +688,7 @@ static inline void *alloc_tramp(unsigned long size) · 2 unmodified lines
692688 {
693689 return NULL;
694690 }
695−static inline void tramp_free(void *tramp, int size) { }
691+static inline void tramp_free(void *tramp) { }
696692 #endif
697693
698694 /* Defined as markers to the end of the ftrace default trampolines */
@@ -730,6 +726,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) · 31 unmodified lines
730726 unsigned long end_offset;
731727 unsigned long op_offset;
732728 unsigned long offset;
729+ unsigned long npages;
733730 unsigned long size;
734731 unsigned long retq;
735732 unsigned long *ptr;
@@ -762,6 +759,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) · 26 unmodified lines
762759 return 0;
763760
764761 *tramp_size = size + RET_SIZE + sizeof(void *);
762+ npages = DIV_ROUND_UP(*tramp_size, PAGE_SIZE);
765763
766764 /* Copy ftrace_caller onto the trampoline memory */
767765 ret = probe_kernel_read(trampoline, (void *)start_offset, size);
@@ -806,9 +804,17 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size) · 38 unmodified lines
806804 /* ALLOC_TRAMP flags lets us know we created it */
807805 ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
808806
807+ set_vm_flush_reset_perms(trampoline);
808+
809+ /*
810+ * Module allocation needs to be completed by making the page
811+ * executable. The page is still writable, which is a security hazard,
812+ * but anyhow ftrace breaks W^X completely.
813+ */
814+ set_memory_x((unsigned long)trampoline, npages);
809815 return (unsigned long)trampoline;
810816 fail:
811− tramp_free(trampoline, *tramp_size);
817+ tramp_free(trampoline);
812818 return 0;
813819 }
814820
@@ -939,7 +945,7 @@ void arch_ftrace_trampoline_free(struct ftrace_ops *ops) · 124 unmodified lines
939945 if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
940946 return;
941947
942− tramp_free((void *)ops->trampoline, ops->trampoline_size);
948+ tramp_free((void *)ops->trampoline);
943949 ops->trampoline = 0;
944950 }
945951
arch/x86/kernel/jump_label.c+10−11
@@ -37,7 +37,6 @@ static void bug_at(unsigned char *ip, int line) · 36 unmodified lines
3737
3838 static void __ref __jump_label_transform(struct jump_entry *entry,
3939 enum jump_label_type type,
40− void *(*poker)(void *, const void *, size_t),
4140 int init)
4241 {
4342 union jump_code_union jmp;
@@ -50,9 +49,6 @@ static void __ref __jump_label_transform(struct jump_entry *entry, · 6 unmodified lines
5049 jmp.offset = jump_entry_target(entry) -
5150 (jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE);
5251
53− if (early_boot_irqs_disabled)
54− poker = text_poke_early;
55−
5652 if (type == JUMP_LABEL_JMP) {
5753 if (init) {
5854 expect = default_nop; line = __LINE__;
@@ -75,16 +71,19 @@ static void __ref __jump_label_transform(struct jump_entry *entry, · 16 unmodified lines
7571 bug_at((void *)jump_entry_code(entry), line);
7672
7773 /*
78− * Make text_poke_bp() a default fallback poker.
74+ * As long as only a single processor is running and the code is still
75+ * not marked as RO, text_poke_early() can be used; Checking that
76+ * system_state is SYSTEM_BOOTING guarantees it. It will be set to
77+ * SYSTEM_SCHEDULING before other cores are awaken and before the
78+ * code is write-protected.
7979 *
8080 * At the time the change is being done, just ignore whether we
8181 * are doing nop -> jump or jump -> nop transition, and assume
8282 * always nop being the 'currently valid' instruction
83− *
8483 */
85− if (poker) {
86− (*poker)((void *)jump_entry_code(entry), code,
87− JUMP_LABEL_NOP_SIZE);
84+ if (init || system_state == SYSTEM_BOOTING) {
85+ text_poke_early((void *)jump_entry_code(entry), code,
86+ JUMP_LABEL_NOP_SIZE);
8887 return;
8988 }
9089
@@ -96,7 +95,7 @@ void arch_jump_label_transform(struct jump_entry *entry, · 5 unmodified lines
9695 enum jump_label_type type)
9796 {
9897 mutex_lock(&text_mutex);
99− __jump_label_transform(entry, type, NULL, 0);
98+ __jump_label_transform(entry, type, 0);
10099 mutex_unlock(&text_mutex);
101100 }
102101
@@ -126,5 +125,5 @@ __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry, · 23 unmodified lines
126125 jlstate = JL_STATE_NO_UPDATE;
127126 }
128127 if (jlstate == JL_STATE_UPDATE)
129− __jump_label_transform(entry, type, text_poke_early, 1);
128+ __jump_label_transform(entry, type, 1);
130129 }
arch/x86/kernel/kgdb.c+7−18
@@ -747,7 +747,6 @@ void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip) · 746 unmodified lines
747747 int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
748748 {
749749 int err;
750− char opc[BREAK_INSTR_SIZE];
751750
752751 bpt->type = BP_BREAKPOINT;
753752 err = probe_kernel_read(bpt->saved_instr, (char *)bpt->bpt_addr,
@@ -759,18 +758,13 @@ int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) · 5 unmodified lines
759758 if (!err)
760759 return err;
761760 /*
762− * It is safe to call text_poke() because normal kernel execution
761+ * It is safe to call text_poke_kgdb() because normal kernel execution
763762 * is stopped on all cores, so long as the text_mutex is not locked.
764763 */
765764 if (mutex_is_locked(&text_mutex))
766765 return -EBUSY;
767− text_poke((void *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr,
768− BREAK_INSTR_SIZE);
769− err = probe_kernel_read(opc, (char *)bpt->bpt_addr, BREAK_INSTR_SIZE);
770− if (err)
771− return err;
772− if (memcmp(opc, arch_kgdb_ops.gdb_bpt_instr, BREAK_INSTR_SIZE))
773− return -EINVAL;
766+ text_poke_kgdb((void *)bpt->bpt_addr, arch_kgdb_ops.gdb_bpt_instr,
767+ BREAK_INSTR_SIZE);
774768 bpt->type = BP_POKE_BREAKPOINT;
775769
776770 return err;
@@ -778,22 +772,17 @@ int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt) · 1 unmodified line
778772
779773 int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
780774 {
781− int err;
782− char opc[BREAK_INSTR_SIZE];
783−
784775 if (bpt->type != BP_POKE_BREAKPOINT)
785776 goto knl_write;
786777 /*
787− * It is safe to call text_poke() because normal kernel execution
778+ * It is safe to call text_poke_kgdb() because normal kernel execution
788779 * is stopped on all cores, so long as the text_mutex is not locked.
789780 */
790781 if (mutex_is_locked(&text_mutex))
791782 goto knl_write;
792− text_poke((void *)bpt->bpt_addr, bpt->saved_instr, BREAK_INSTR_SIZE);
793− err = probe_kernel_read(opc, (char *)bpt->bpt_addr, BREAK_INSTR_SIZE);
794− if (err || memcmp(opc, bpt->saved_instr, BREAK_INSTR_SIZE))
795− goto knl_write;
796− return err;
783+ text_poke_kgdb((void *)bpt->bpt_addr, bpt->saved_instr,
784+ BREAK_INSTR_SIZE);
785+ return 0;
797786
798787 knl_write:
799788 return probe_kernel_write((char *)bpt->bpt_addr,
arch/x86/kernel/kprobes/core.c+15−4
@@ -431,8 +431,21 @@ void *alloc_insn_page(void) · 430 unmodified lines
431431 void *page;
432432
433433 page = module_alloc(PAGE_SIZE);
434− if (page)
435− set_memory_ro((unsigned long)page & PAGE_MASK, 1);
434+ if (!page)
435+ return NULL;
436+
437+ set_vm_flush_reset_perms(page);
438+ /*
439+ * First make the page read-only, and only then make it executable to
440+ * prevent it from being W+X in between.
441+ */
442+ set_memory_ro((unsigned long)page, 1);
443+
444+ /*
445+ * TODO: Once additional kernel code protection mechanisms are set, ensure
446+ * that the page was not maliciously altered and it is still zeroed.
447+ */
448+ set_memory_x((unsigned long)page, 1);
436449
437450 return page;
438451 }
@@ -440,8 +453,6 @@ void *alloc_insn_page(void) · 1 unmodified line
440453 /* Recover page to RW mode before releasing it */
441454 void free_insn_page(void *page)
442455 {
443− set_memory_nx((unsigned long)page & PAGE_MASK, 1);
444− set_memory_rw((unsigned long)page & PAGE_MASK, 1);
445456 module_memfree(page);
446457 }
447458
arch/x86/kernel/module.c+1−1
@@ -87,7 +87,7 @@ void *module_alloc(unsigned long size) · 86 unmodified lines
8787 p = __vmalloc_node_range(size, MODULE_ALIGN,
8888 MODULES_VADDR + get_module_load_offset(),
8989 MODULES_END, GFP_KERNEL,
90− PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
90+ PAGE_KERNEL, 0, NUMA_NO_NODE,
9191 __builtin_return_address(0));
9292 if (p && (kasan_module_alloc(p, size) < 0)) {
9393 vfree(p);
arch/x86/kernel/vmlinux.lds.S+3−3
@@ -141,11 +141,11 @@ SECTIONS · 140 unmodified lines
141141 *(.text.__x86.indirect_thunk)
142142 __indirect_thunk_end = .;
143143 #endif
144−
145− /* End of text section */
146− _etext = .;
147144 } :text = 0x9090
148145
146+ /* End of text section */
147+ _etext = .;
148+
149149 NOTES :text :note
150150
151151 EXCEPTION_TABLE(16) :text = 0x9090
arch/x86/mm/fault.c+18−37
@@ -360,8 +360,6 @@ static noinline int vmalloc_fault(unsigned long address) · 359 unmodified lines
360360 if (!(address >= VMALLOC_START && address < VMALLOC_END))
361361 return -1;
362362
363− WARN_ON_ONCE(in_nmi());
364−
365363 /*
366364 * Copy kernel mappings over when needed. This can also
367365 * happen within a race in page table update. In the later
@@ -604,24 +602,9 @@ static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index) · 236 unmodified lines
604602 name, index, addr, (desc.limit0 | (desc.limit1 << 16)));
605603 }
606604
607−/*
608− * This helper function transforms the #PF error_code bits into
609− * "[PROT] [USER]" type of descriptive, almost human-readable error strings:
610− */
611−static void err_str_append(unsigned long error_code, char *buf, unsigned long mask, const char *txt)
612−{
613− if (error_code & mask) {
614− if (buf[0])
615− strcat(buf, " ");
616− strcat(buf, txt);
617− }
618−}
619−
620605 static void
621606 show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address)
622607 {
623− char err_txt[64];
624−
625608 if (!oops_may_print())
626609 return;
627610
@@ -645,31 +628,29 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad · 17 unmodified lines
645628 from_kuid(&init_user_ns, current_uid()));
646629 }
647630
648− pr_alert("BUG: unable to handle kernel %s at %px\n",
649− address < PAGE_SIZE ? "NULL pointer dereference" : "paging request",
650− (void *)address);
651−
652− err_txt[0] = 0;
653−
654− /*
655− * Note: length of these appended strings including the separation space and the
656− * zero delimiter must fit into err_txt[].
657− */
658− err_str_append(error_code, err_txt, X86_PF_PROT, "[PROT]" );
659− err_str_append(error_code, err_txt, X86_PF_WRITE, "[WRITE]");
660− err_str_append(error_code, err_txt, X86_PF_USER, "[USER]" );
661− err_str_append(error_code, err_txt, X86_PF_RSVD, "[RSVD]" );
662− err_str_append(error_code, err_txt, X86_PF_INSTR, "[INSTR]");
663− err_str_append(error_code, err_txt, X86_PF_PK, "[PK]" );
664−
665− pr_alert("#PF error: %s\n", error_code ? err_txt : "[normal kernel read fault]");
631+ if (address < PAGE_SIZE && !user_mode(regs))
632+ pr_alert("BUG: kernel NULL pointer dereference, address: %px\n",
633+ (void *)address);
634+ else
635+ pr_alert("BUG: unable to handle page fault for address: %px\n",
636+ (void *)address);
637+
638+ pr_alert("#PF: %s %s in %s mode\n",
639+ (error_code & X86_PF_USER) ? "user" : "supervisor",
640+ (error_code & X86_PF_INSTR) ? "instruction fetch" :
641+ (error_code & X86_PF_WRITE) ? "write access" :
642+ "read access",
643+ user_mode(regs) ? "user" : "kernel");
644+ pr_alert("#PF: error_code(0x%04lx) - %s\n", error_code,
645+ !(error_code & X86_PF_PROT) ? "not-present page" :
646+ (error_code & X86_PF_RSVD) ? "reserved bit violation" :
647+ (error_code & X86_PF_PK) ? "protection keys violation" :
648+ "permissions violation");
666649
667650 if (!(error_code & X86_PF_USER) && user_mode(regs)) {
668651 struct desc_ptr idt, gdt;
669652 u16 ldtr, tr;
670653
671− pr_alert("This was a system access from user code\n");
672−
673654 /*
674655 * This can happen for quite a few reasons. The more obvious
675656 * ones are faults accessing the GDT, or LDT. Perhaps
arch/x86/mm/init.c+37−0
@@ -6,6 +6,7 @@ · 5 unmodified lines
66 #include <linux/swapfile.h>
77 #include <linux/swapops.h>
88 #include <linux/kmemleak.h>
9+#include <linux/sched/task.h>
910
1011 #include <asm/set_memory.h>
1112 #include <asm/e820/api.h>
@@ -23,6 +24,7 @@ · 11 unmodified lines
2324 #include <asm/hypervisor.h>
2425 #include <asm/cpufeature.h>
2526 #include <asm/pti.h>
27+#include <asm/text-patching.h>
2628
2729 /*
2830 * We need to define the tracepoints somewhere, and tlb.c
@@ -701,6 +703,41 @@ void __init init_mem_mapping(void) · 672 unmodified lines
701703 early_memtest(0, max_pfn_mapped << PAGE_SHIFT);
702704 }
703705
706+/*
707+ * Initialize an mm_struct to be used during poking and a pointer to be used
708+ * during patching.
709+ */
710+void __init poking_init(void)
711+{
712+ spinlock_t *ptl;
713+ pte_t *ptep;
714+
715+ poking_mm = copy_init_mm();
716+ BUG_ON(!poking_mm);
717+
718+ /*
719+ * Randomize the poking address, but make sure that the following page
720+ * will be mapped at the same PMD. We need 2 pages, so find space for 3,
721+ * and adjust the address if the PMD ends after the first one.
722+ */
723+ poking_addr = TASK_UNMAPPED_BASE;
724+ if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
725+ poking_addr += (kaslr_get_random_long("Poking") & PAGE_MASK) %
726+ (TASK_SIZE - TASK_UNMAPPED_BASE - 3 * PAGE_SIZE);
727+
728+ if (((poking_addr + PAGE_SIZE) & ~PMD_MASK) == 0)
729+ poking_addr += PAGE_SIZE;
730+
731+ /*
732+ * We need to trigger the allocation of the page-tables that will be
733+ * needed for poking now. Later, poking may be performed in an atomic
734+ * section, which might cause allocation to fail.
735+ */
736+ ptep = get_locked_pte(poking_mm, poking_addr, &ptl);
737+ BUG_ON(!ptep);
738+ pte_unmap_unlock(ptep, ptl);
739+}
740+
704741 /*
705742 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
706743 * is valid. The argument is a physical page number.
arch/x86/mm/kaslr.c+39−55
@@ -125,10 +125,7 @@ void __init kernel_randomize_memory(void) · 124 unmodified lines
125125 */
126126 entropy = remain_entropy / (ARRAY_SIZE(kaslr_regions) - i);
127127 prandom_bytes_state(&rand_state, &rand, sizeof(rand));
128− if (pgtable_l5_enabled())
129− entropy = (rand % (entropy + 1)) & P4D_MASK;
130− else
131− entropy = (rand % (entropy + 1)) & PUD_MASK;
128+ entropy = (rand % (entropy + 1)) & PUD_MASK;
132129 vaddr += entropy;
133130 *kaslr_regions[i].base = vaddr;
134131
@@ -137,84 +134,71 @@ void __init kernel_randomize_memory(void) · 2 unmodified lines
137134 * randomization alignment.
138135 */
139136 vaddr += get_padding(&kaslr_regions[i]);
140− if (pgtable_l5_enabled())
141− vaddr = round_up(vaddr + 1, P4D_SIZE);
142− else
143− vaddr = round_up(vaddr + 1, PUD_SIZE);
137+ vaddr = round_up(vaddr + 1, PUD_SIZE);
144138 remain_entropy -= entropy;
145139 }
146140 }
147141
148142 static void __meminit init_trampoline_pud(void)
149143 {
150− unsigned long paddr, paddr_next;
144+ pud_t *pud_page_tramp, *pud, *pud_tramp;
145+ p4d_t *p4d_page_tramp, *p4d, *p4d_tramp;
146+ unsigned long paddr, vaddr;
151147 pgd_t *pgd;
152− pud_t *pud_page, *pud_page_tramp;
153− int i;
154148
155149 pud_page_tramp = alloc_low_page();
156150
151+ /*
152+ * There are two mappings for the low 1MB area, the direct mapping
153+ * and the 1:1 mapping for the real mode trampoline:
154+ *
155+ * Direct mapping: virt_addr = phys_addr + PAGE_OFFSET
156+ * 1:1 mapping: virt_addr = phys_addr
157+ */
157158 paddr = 0;
158− pgd = pgd_offset_k((unsigned long)__va(paddr));
159− pud_page = (pud_t *) pgd_page_vaddr(*pgd);
160−
161− for (i = pud_index(paddr); i < PTRS_PER_PUD; i++, paddr = paddr_next) {
162− pud_t *pud, *pud_tramp;
163− unsigned long vaddr = (unsigned long)__va(paddr);
159+ vaddr = (unsigned long)__va(paddr);
160+ pgd = pgd_offset_k(vaddr);
164161
165− pud_tramp = pud_page_tramp + pud_index(paddr);
166− pud = pud_page + pud_index(vaddr);
167− paddr_next = (paddr & PUD_MASK) + PUD_SIZE;
168−
169− *pud_tramp = *pud;
170− }
162+ p4d = p4d_offset(pgd, vaddr);
163+ pud = pud_offset(p4d, vaddr);
171164
172− set_pgd(&trampoline_pgd_entry,
173− __pgd(_KERNPG_TABLE | __pa(pud_page_tramp)));
174−}
175−
176−static void __meminit init_trampoline_p4d(void)
177−{
178− unsigned long paddr, paddr_next;
179− pgd_t *pgd;
180− p4d_t *p4d_page, *p4d_page_tramp;
181− int i;
165+ pud_tramp = pud_page_tramp + pud_index(paddr);
166+ *pud_tramp = *pud;
182167
183− p4d_page_tramp = alloc_low_page();
184−
185− paddr = 0;
186− pgd = pgd_offset_k((unsigned long)__va(paddr));
187− p4d_page = (p4d_t *) pgd_page_vaddr(*pgd);
188−
189− for (i = p4d_index(paddr); i < PTRS_PER_P4D; i++, paddr = paddr_next) {
190− p4d_t *p4d, *p4d_tramp;
191− unsigned long vaddr = (unsigned long)__va(paddr);
168+ if (pgtable_l5_enabled()) {
169+ p4d_page_tramp = alloc_low_page();
192170
193171 p4d_tramp = p4d_page_tramp + p4d_index(paddr);
194− p4d = p4d_page + p4d_index(vaddr);
195− paddr_next = (paddr & P4D_MASK) + P4D_SIZE;
196172
197− *p4d_tramp = *p4d;
198− }
173+ set_p4d(p4d_tramp,
174+ __p4d(_KERNPG_TABLE | __pa(pud_page_tramp)));
199175
200− set_pgd(&trampoline_pgd_entry,
201− __pgd(_KERNPG_TABLE | __pa(p4d_page_tramp)));
176+ set_pgd(&trampoline_pgd_entry,
177+ __pgd(_KERNPG_TABLE | __pa(p4d_page_tramp)));
178+ } else {
179+ set_pgd(&trampoline_pgd_entry,
180+ __pgd(_KERNPG_TABLE | __pa(pud_page_tramp)));
181+ }
202182 }
203183
204184 /*
205− * Create PGD aligned trampoline table to allow real mode initialization
206− * of additional CPUs. Consume only 1 low memory page.
185+ * The real mode trampoline, which is required for bootstrapping CPUs
186+ * occupies only a small area under the low 1MB. See reserve_real_mode()
187+ * for details.
188+ *
189+ * If KASLR is disabled the first PGD entry of the direct mapping is copied
190+ * to map the real mode trampoline.
191+ *
192+ * If KASLR is enabled, copy only the PUD which covers the low 1MB
193+ * area. This limits the randomization granularity to 1GB for both 4-level
194+ * and 5-level paging.
207195 */
208196 void __meminit init_trampoline(void)
209197 {
210−
211198 if (!kaslr_memory_enabled()) {
212199 init_trampoline_default();
213200 return;
214201 }
215202
216− if (pgtable_l5_enabled())
217− init_trampoline_p4d();
218− else
219− init_trampoline_pud();
203+ init_trampoline_pud();
220204 }
arch/x86/mm/pageattr.c+10−6
@@ -2209,8 +2209,6 @@ int set_pages_rw(struct page *page, int numpages) · 2208 unmodified lines
22092209 return set_memory_rw(addr, numpages);
22102210 }
22112211
2212−#ifdef CONFIG_DEBUG_PAGEALLOC
2213−
22142212 static int __set_pages_p(struct page *page, int numpages)
22152213 {
22162214 unsigned long tempaddr = (unsigned long) page_address(page);
@@ -2249,6 +2247,16 @@ static int __set_pages_np(struct page *page, int numpages) · 32 unmodified lines
22492247 return __change_page_attr_set_clr(&cpa, 0);
22502248 }
22512249
2250+int set_direct_map_invalid_noflush(struct page *page)
2251+{
2252+ return __set_pages_np(page, 1);
2253+}
2254+
2255+int set_direct_map_default_noflush(struct page *page)
2256+{
2257+ return __set_pages_p(page, 1);
2258+}
2259+
22522260 void __kernel_map_pages(struct page *page, int numpages, int enable)
22532261 {
22542262 if (PageHighMem(page))
@@ -2282,7 +2290,6 @@ void __kernel_map_pages(struct page *page, int numpages, int enable) · 27 unmodified lines
22822290 }
22832291
22842292 #ifdef CONFIG_HIBERNATION
2285−
22862293 bool kernel_page_present(struct page *page)
22872294 {
22882295 unsigned int level;
@@ -2294,11 +2301,8 @@ bool kernel_page_present(struct page *page) · 5 unmodified lines
22942301 pte = lookup_address((unsigned long)page_address(page), &level);
22952302 return (pte_val(*pte) & _PAGE_PRESENT);
22962303 }
2297−
22982304 #endif /* CONFIG_HIBERNATION */
22992305
2300−#endif /* CONFIG_DEBUG_PAGEALLOC */
2301−
23022306 int __init kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
23032307 unsigned numpages, unsigned long page_flags)
23042308 {
arch/x86/mm/pgtable.c+6−4
@@ -373,14 +373,14 @@ static void pgd_prepopulate_user_pmd(struct mm_struct *mm, · 372 unmodified lines
373373
374374 static struct kmem_cache *pgd_cache;
375375
376−static int __init pgd_cache_init(void)
376+void __init pgd_cache_init(void)
377377 {
378378 /*
379379 * When PAE kernel is running as a Xen domain, it does not use
380380 * shared kernel pmd. And this requires a whole page for pgd.
381381 */
382382 if (!SHARED_KERNEL_PMD)
383− return 0;
383+ return;
384384
385385 /*
386386 * when PAE kernel is not running as a Xen domain, it uses
@@ -390,9 +390,7 @@ static int __init pgd_cache_init(void) · 3 unmodified lines
390390 */
391391 pgd_cache = kmem_cache_create("pgd_cache", PGD_SIZE, PGD_ALIGN,
392392 SLAB_PANIC, NULL);
393− return 0;
394393 }
395−core_initcall(pgd_cache_init);
396394
397395 static inline pgd_t *_pgd_alloc(void)
398396 {
@@ -420,6 +418,10 @@ static inline void _pgd_free(pgd_t *pgd) · 21 unmodified lines
420418 }
421419 #else
422420
421+void __init pgd_cache_init(void)
422+{
423+}
424+
423425 static inline pgd_t *_pgd_alloc(void)
424426 {
425427 return (pgd_t *)__get_free_pages(PGALLOC_GFP, PGD_ALLOCATION_ORDER);
arch/x86/mm/tlb.c+82−34
@@ -634,7 +634,7 @@ static void flush_tlb_func_common(const struct flush_tlb_info *f, · 633 unmodified lines
634634 this_cpu_write(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen, mm_tlb_gen);
635635 }
636636
637−static void flush_tlb_func_local(void *info, enum tlb_flush_reason reason)
637+static void flush_tlb_func_local(const void *info, enum tlb_flush_reason reason)
638638 {
639639 const struct flush_tlb_info *f = info;
640640
@@ -722,43 +722,81 @@ void native_flush_tlb_others(const struct cpumask *cpumask, · 81 unmodified lines
722722 */
723723 unsigned long tlb_single_page_flush_ceiling __read_mostly = 33;
724724
725+static DEFINE_PER_CPU_SHARED_ALIGNED(struct flush_tlb_info, flush_tlb_info);
726+
727+#ifdef CONFIG_DEBUG_VM
728+static DEFINE_PER_CPU(unsigned int, flush_tlb_info_idx);
729+#endif
730+
731+static inline struct flush_tlb_info *get_flush_tlb_info(struct mm_struct *mm,
732+ unsigned long start, unsigned long end,
733+ unsigned int stride_shift, bool freed_tables,
734+ u64 new_tlb_gen)
735+{
736+ struct flush_tlb_info *info = this_cpu_ptr(&flush_tlb_info);
737+
738+#ifdef CONFIG_DEBUG_VM
739+ /*
740+ * Ensure that the following code is non-reentrant and flush_tlb_info
741+ * is not overwritten. This means no TLB flushing is initiated by
742+ * interrupt handlers and machine-check exception handlers.
743+ */
744+ BUG_ON(this_cpu_inc_return(flush_tlb_info_idx) != 1);
745+#endif
746+
747+ info->start = start;
748+ info->end = end;
749+ info->mm = mm;
750+ info->stride_shift = stride_shift;
751+ info->freed_tables = freed_tables;
752+ info->new_tlb_gen = new_tlb_gen;
753+
754+ return info;
755+}
756+
757+static inline void put_flush_tlb_info(void)
758+{
759+#ifdef CONFIG_DEBUG_VM
760+ /* Complete reentrency prevention checks */
761+ barrier();
762+ this_cpu_dec(flush_tlb_info_idx);
763+#endif
764+}
765+
725766 void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
726767 unsigned long end, unsigned int stride_shift,
727768 bool freed_tables)
728769 {
770+ struct flush_tlb_info *info;
771+ u64 new_tlb_gen;
729772 int cpu;
730773
731− struct flush_tlb_info info = {
732− .mm = mm,
733− .stride_shift = stride_shift,
734− .freed_tables = freed_tables,
735− };
736−
737774 cpu = get_cpu();
738775
739− /* This is also a barrier that synchronizes with switch_mm(). */
740− info.new_tlb_gen = inc_mm_tlb_gen(mm);
741−
742776 /* Should we flush just the requested range? */
743− if ((end != TLB_FLUSH_ALL) &&
744− ((end - start) >> stride_shift) <= tlb_single_page_flush_ceiling) {
745− info.start = start;
746− info.end = end;
747− } else {
748− info.start = 0UL;
749− info.end = TLB_FLUSH_ALL;
777+ if ((end == TLB_FLUSH_ALL) ||
778+ ((end - start) >> stride_shift) > tlb_single_page_flush_ceiling) {
779+ start = 0;
780+ end = TLB_FLUSH_ALL;
750781 }
751782
783+ /* This is also a barrier that synchronizes with switch_mm(). */
784+ new_tlb_gen = inc_mm_tlb_gen(mm);
785+
786+ info = get_flush_tlb_info(mm, start, end, stride_shift, freed_tables,
787+ new_tlb_gen);
788+
752789 if (mm == this_cpu_read(cpu_tlbstate.loaded_mm)) {
753− VM_WARN_ON(irqs_disabled());
790+ lockdep_assert_irqs_enabled();
754791 local_irq_disable();
755− flush_tlb_func_local(&info, TLB_LOCAL_MM_SHOOTDOWN);
792+ flush_tlb_func_local(info, TLB_LOCAL_MM_SHOOTDOWN);
756793 local_irq_enable();
757794 }
758795
759796 if (cpumask_any_but(mm_cpumask(mm), cpu) < nr_cpu_ids)
760− flush_tlb_others(mm_cpumask(mm), &info);
797+ flush_tlb_others(mm_cpumask(mm), info);
761798
799+ put_flush_tlb_info();
762800 put_cpu();
763801 }
764802
@@ -787,38 +825,48 @@ static void do_kernel_range_flush(void *info) · 22 unmodified lines
787825
788826 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
789827 {
790−
791828 /* Balance as user space task's flush, a bit conservative */
792829 if (end == TLB_FLUSH_ALL ||
793830 (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) {
794831 on_each_cpu(do_flush_tlb_all, NULL, 1);
795832 } else {
796− struct flush_tlb_info info;
797− info.start = start;
798− info.end = end;
799− on_each_cpu(do_kernel_range_flush, &info, 1);
833+ struct flush_tlb_info *info;
834+
835+ preempt_disable();
836+ info = get_flush_tlb_info(NULL, start, end, 0, false, 0);
837+
838+ on_each_cpu(do_kernel_range_flush, info, 1);
839+
840+ put_flush_tlb_info();
841+ preempt_enable();
800842 }
801843 }
802844
845+/*
846+ * arch_tlbbatch_flush() performs a full TLB flush regardless of the active mm.
847+ * This means that the 'struct flush_tlb_info' that describes which mappings to
848+ * flush is actually fixed. We therefore set a single fixed struct and use it in
849+ * arch_tlbbatch_flush().
850+ */
851+static const struct flush_tlb_info full_flush_tlb_info = {
852+ .mm = NULL,
853+ .start = 0,
854+ .end = TLB_FLUSH_ALL,
855+};
856+
803857 void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
804858 {
805− struct flush_tlb_info info = {
806− .mm = NULL,
807− .start = 0UL,
808− .end = TLB_FLUSH_ALL,
809− };
810−
811859 int cpu = get_cpu();
812860
813861 if (cpumask_test_cpu(cpu, &batch->cpumask)) {
814− VM_WARN_ON(irqs_disabled());
862+ lockdep_assert_irqs_enabled();
815863 local_irq_disable();
816− flush_tlb_func_local(&info, TLB_LOCAL_SHOOTDOWN);
864+ flush_tlb_func_local(&full_flush_tlb_info, TLB_LOCAL_SHOOTDOWN);
817865 local_irq_enable();
818866 }
819867
820868 if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids)
821− flush_tlb_others(&batch->cpumask, &info);
869+ flush_tlb_others(&batch->cpumask, &full_flush_tlb_info);
822870
823871 cpumask_clear(&batch->cpumask);
824872
arch/x86/xen/mmu_pv.c+0−2
@@ -2318,8 +2318,6 @@ static void xen_set_fixmap(unsigned idx, phys_addr_t phys, pgprot_t prot) · 2317 unmodified lines
23182318 #elif defined(CONFIG_X86_VSYSCALL_EMULATION)
23192319 case VSYSCALL_PAGE:
23202320 #endif
2321− case FIX_TEXT_POKE0:
2322− case FIX_TEXT_POKE1:
23232321 /* All local page mappings */
23242322 pte = pfn_pte(phys, prot);
23252323 break;
include/asm-generic/pgtable.h+2−0
@@ -1126,6 +1126,8 @@ int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn, · 1125 unmodified lines
11261126 static inline void init_espfix_bsp(void) { }
11271127 #endif
11281128
1129+extern void __init pgd_cache_init(void);
1130+
11291131 #ifndef __HAVE_ARCH_PFN_MODIFY_ALLOWED
11301132 static inline bool pfn_modify_allowed(unsigned long pfn, pgprot_t prot)
11311133 {
include/asm-generic/tlb.h+9−0
@@ -21,6 +21,15 @@ · 20 unmodified lines
2121 #include <asm/tlbflush.h>
2222 #include <asm/cacheflush.h>
2323
24+/*
25+ * Blindly accessing user memory from NMI context can be dangerous
26+ * if we're in the middle of switching the current user task or switching
27+ * the loaded mm.
28+ */
29+#ifndef nmi_uaccess_okay
30+# define nmi_uaccess_okay() true
31+#endif
32+
2433 #ifdef CONFIG_MMU
2534
2635 /*
include/linux/filter.h+4−14
@@ -20,6 +20,7 @@ · 19 unmodified lines
2020 #include <linux/set_memory.h>
2121 #include <linux/kallsyms.h>
2222 #include <linux/if_vlan.h>
23+#include <linux/vmalloc.h>
2324
2425 #include <net/sch_generic.h>
2526
@@ -503,7 +504,6 @@ struct bpf_prog { · 477 unmodified lines
503504 u16 pages; /* Number of allocated pages */
504505 u16 jited:1, /* Is our filter JIT'ed? */
505506 jit_requested:1,/* archs need to JIT the prog */
506− undo_set_mem:1, /* Passed set_memory_ro() checkpoint */
507507 gpl_compatible:1, /* Is filter GPL compatible? */
508508 cb_access:1, /* Is control block accessed? */
509509 dst_needed:1, /* Do we need dst entry? */
@@ -733,24 +733,15 @@ bpf_ctx_narrow_access_ok(u32 off, u32 size, u32 size_default) · 223 unmodified lines
733733
734734 static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
735735 {
736− fp->undo_set_mem = 1;
736+ set_vm_flush_reset_perms(fp);
737737 set_memory_ro((unsigned long)fp, fp->pages);
738738 }
739739
740−static inline void bpf_prog_unlock_ro(struct bpf_prog *fp)
741−{
742− if (fp->undo_set_mem)
743− set_memory_rw((unsigned long)fp, fp->pages);
744−}
745−
746740 static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
747741 {
742+ set_vm_flush_reset_perms(hdr);
748743 set_memory_ro((unsigned long)hdr, hdr->pages);
749−}
750−
751−static inline void bpf_jit_binary_unlock_ro(struct bpf_binary_header *hdr)
752−{
753− set_memory_rw((unsigned long)hdr, hdr->pages);
744+ set_memory_x((unsigned long)hdr, hdr->pages);
754745 }
755746
756747 static inline struct bpf_binary_header *
@@ -788,7 +779,6 @@ void __bpf_prog_free(struct bpf_prog *fp); · 31 unmodified lines
788779
789780 static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
790781 {
791− bpf_prog_unlock_ro(fp);
792782 __bpf_prog_free(fp);
793783 }
794784
include/linux/mm.h+6−12
@@ -2610,37 +2610,31 @@ static inline void kernel_poison_pages(struct page *page, int numpages, · 2609 unmodified lines
26102610 int enable) { }
26112611 #endif
26122612
2613−#ifdef CONFIG_DEBUG_PAGEALLOC
26142613 extern bool _debug_pagealloc_enabled;
2615−extern void __kernel_map_pages(struct page *page, int numpages, int enable);
26162614
26172615 static inline bool debug_pagealloc_enabled(void)
26182616 {
2619− return _debug_pagealloc_enabled;
2617+ return IS_ENABLED(CONFIG_DEBUG_PAGEALLOC) && _debug_pagealloc_enabled;
26202618 }
26212619
2620+#if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_ARCH_HAS_SET_DIRECT_MAP)
2621+extern void __kernel_map_pages(struct page *page, int numpages, int enable);
2622+
26222623 static inline void
26232624 kernel_map_pages(struct page *page, int numpages, int enable)
26242625 {
2625− if (!debug_pagealloc_enabled())
2626− return;
2627−
26282626 __kernel_map_pages(page, numpages, enable);
26292627 }
26302628 #ifdef CONFIG_HIBERNATION
26312629 extern bool kernel_page_present(struct page *page);
26322630 #endif /* CONFIG_HIBERNATION */
2633−#else /* CONFIG_DEBUG_PAGEALLOC */
2631+#else /* CONFIG_DEBUG_PAGEALLOC || CONFIG_ARCH_HAS_SET_DIRECT_MAP */
26342632 static inline void
26352633 kernel_map_pages(struct page *page, int numpages, int enable) {}
26362634 #ifdef CONFIG_HIBERNATION
26372635 static inline bool kernel_page_present(struct page *page) { return true; }
26382636 #endif /* CONFIG_HIBERNATION */
2639−static inline bool debug_pagealloc_enabled(void)
2640−{
2641− return false;
2642−}
2643−#endif /* CONFIG_DEBUG_PAGEALLOC */
2637+#endif /* CONFIG_DEBUG_PAGEALLOC || CONFIG_ARCH_HAS_SET_DIRECT_MAP */
26442638
26452639 #ifdef __HAVE_ARCH_GATE_AREA
26462640 extern struct vm_area_struct *get_gate_vma(struct mm_struct *mm);
include/linux/sched/task.h+1−0
@@ -76,6 +76,7 @@ extern void exit_itimers(struct signal_struct *); · 75 unmodified lines
7676 extern long _do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *, unsigned long);
7777 extern long do_fork(unsigned long, unsigned long, unsigned long, int __user *, int __user *);
7878 struct task_struct *fork_idle(int);
79+struct mm_struct *copy_init_mm(void);
7980 extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
8081 extern long kernel_wait4(pid_t, int __user *, int, struct rusage *);
8182
include/linux/set_memory.h+11−0
@@ -17,6 +17,17 @@ static inline int set_memory_x(unsigned long addr, int numpages) { return 0; } · 16 unmodified lines
1717 static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; }
1818 #endif
1919
20+#ifndef CONFIG_ARCH_HAS_SET_DIRECT_MAP
21+static inline int set_direct_map_invalid_noflush(struct page *page)
22+{
23+ return 0;
24+}
25+static inline int set_direct_map_default_noflush(struct page *page)
26+{
27+ return 0;
28+}
29+#endif
30+
2031 #ifndef set_mce_nospec
2132 static inline int set_mce_nospec(unsigned long pfn)
2233 {
include/linux/uprobes.h+5−0
@@ -115,6 +115,7 @@ struct uprobes_state { · 114 unmodified lines
115115 struct xol_area *xol_area;
116116 };
117117
118+extern void __init uprobes_init(void);
118119 extern int set_swbp(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
119120 extern int set_orig_insn(struct arch_uprobe *aup, struct mm_struct *mm, unsigned long vaddr);
120121 extern bool is_swbp_insn(uprobe_opcode_t *insn);
@@ -154,6 +155,10 @@ extern void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, · 33 unmodified lines
154155 struct uprobes_state {
155156 };
156157
158+static inline void uprobes_init(void)
159+{
160+}
161+
157162 #define uprobe_get_trap_addr(regs) instruction_pointer(regs)
158163
159164 static inline int
include/linux/vmalloc.h+15−0
@@ -21,6 +21,11 @@ struct notifier_block; /* in notifier.h */ · 20 unmodified lines
2121 #define VM_UNINITIALIZED 0x00000020 /* vm_struct is not fully initialized */
2222 #define VM_NO_GUARD 0x00000040 /* don't add guard page */
2323 #define VM_KASAN 0x00000080 /* has allocated kasan shadow memory */
24+/*
25+ * Memory with VM_FLUSH_RESET_PERMS cannot be freed in an interrupt or with
26+ * vfree_atomic().
27+ */
28+#define VM_FLUSH_RESET_PERMS 0x00000100 /* Reset direct map and flush TLB on unmap */
2429 /* bits [20..32] reserved for arch specific ioremap internals */
2530
2631 /*
@@ -142,6 +147,13 @@ extern int map_kernel_range_noflush(unsigned long start, unsigned long size, · 115 unmodified lines
142147 pgprot_t prot, struct page **pages);
143148 extern void unmap_kernel_range_noflush(unsigned long addr, unsigned long size);
144149 extern void unmap_kernel_range(unsigned long addr, unsigned long size);
150+static inline void set_vm_flush_reset_perms(void *addr)
151+{
152+ struct vm_struct *vm = find_vm_area(addr);
153+
154+ if (vm)
155+ vm->flags |= VM_FLUSH_RESET_PERMS;
156+}
145157 #else
146158 static inline int
147159 map_kernel_range_noflush(unsigned long start, unsigned long size,
@@ -157,6 +169,9 @@ static inline void · 9 unmodified lines
157169 unmap_kernel_range(unsigned long addr, unsigned long size)
158170 {
159171 }
172+static inline void set_vm_flush_reset_perms(void *addr)
173+{
174+}
160175 #endif
161176
162177 /* Allocate/destroy a 'vmalloc' VM area. */
init/main.c+6−0
@@ -504,6 +504,10 @@ void __init __weak thread_stack_cache_init(void) · 503 unmodified lines
504504
505505 void __init __weak mem_encrypt_init(void) { }
506506
507+void __init __weak poking_init(void) { }
508+
509+void __init __weak pgd_cache_init(void) { }
510+
507511 bool initcall_debug;
508512 core_param(initcall_debug, initcall_debug, bool, 0644);
509513
@@ -535,6 +539,7 @@ static void __init mm_init(void) · 25 unmodified lines
535539 init_espfix_bsp();
536540 /* Should be run after espfix64 is set up. */
537541 pti_init();
542+ pgd_cache_init();
538543 }
539544
540545 void __init __weak arch_call_rest_init(void)
@@ -737,6 +742,7 @@ asmlinkage __visible void __init start_kernel(void) · 196 unmodified lines
737742 taskstats_init_early();
738743 delayacct_init();
739744
745+ poking_init();
740746 check_bugs();
741747
742748 acpi_subsystem_init();
kernel/bpf/core.c+0−1
@@ -848,7 +848,6 @@ void __weak bpf_jit_free(struct bpf_prog *fp) · 847 unmodified lines
848848 if (fp->jited) {
849849 struct bpf_binary_header *hdr = bpf_jit_binary_hdr(fp);
850850
851− bpf_jit_binary_unlock_ro(hdr);
852851 bpf_jit_binary_free(hdr);
853852
854853 WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(fp));
kernel/events/uprobes.c+3−5
@@ -2294,16 +2294,14 @@ static struct notifier_block uprobe_exception_nb = { · 2293 unmodified lines
22942294 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
22952295 };
22962296
2297−static int __init init_uprobes(void)
2297+void __init uprobes_init(void)
22982298 {
22992299 int i;
23002300
23012301 for (i = 0; i < UPROBES_HASH_SZ; i++)
23022302 mutex_init(&uprobes_mmap_mutex[i]);
23032303
2304− if (percpu_init_rwsem(&dup_mmap_sem))
2305− return -ENOMEM;
2304+ BUG_ON(percpu_init_rwsem(&dup_mmap_sem));
23062305
2307− return register_die_notifier(&uprobe_exception_nb);
2306+ BUG_ON(register_die_notifier(&uprobe_exception_nb));
23082307 }
2309−__initcall(init_uprobes);
kernel/fork.c+19−6
@@ -815,6 +815,7 @@ void __init fork_init(void) · 814 unmodified lines
815815 #endif
816816
817817 lockdep_init_task(&init_task);
818+ uprobes_init();
818819 }
819820
820821 int __weak arch_dup_task_struct(struct task_struct *dst,
@@ -1298,13 +1299,20 @@ void mm_release(struct task_struct *tsk, struct mm_struct *mm) · 477 unmodified lines
12981299 complete_vfork_done(tsk);
12991300 }
13001301
1301−/*
1302− * Allocate a new mm structure and copy contents from the
1303− * mm structure of the passed in task structure.
1302+/**
1303+ * dup_mm() - duplicates an existing mm structure
1304+ * @tsk: the task_struct with which the new mm will be associated.
1305+ * @oldmm: the mm to duplicate.
1306+ *
1307+ * Allocates a new mm structure and duplicates the provided @oldmm structure
1308+ * content into it.
1309+ *
1310+ * Return: the duplicated mm or NULL on failure.
13041311 */
1305−static struct mm_struct *dup_mm(struct task_struct *tsk)
1312+static struct mm_struct *dup_mm(struct task_struct *tsk,
1313+ struct mm_struct *oldmm)
13061314 {
1307− struct mm_struct *mm, *oldmm = current->mm;
1315+ struct mm_struct *mm;
13081316 int err;
13091317
13101318 mm = allocate_mm();
@@ -1371,7 +1379,7 @@ static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) · 60 unmodified lines
13711379 }
13721380
13731381 retval = -ENOMEM;
1374− mm = dup_mm(tsk);
1382+ mm = dup_mm(tsk, current->mm);
13751383 if (!mm)
13761384 goto fail_nomem;
13771385
@@ -2186,6 +2194,11 @@ struct task_struct *fork_idle(int cpu) · 808 unmodified lines
21862194 return task;
21872195 }
21882196
2197+struct mm_struct *copy_init_mm(void)
2198+{
2199+ return dup_mm(NULL, &init_mm);
2200+}
2201+
21892202 /*
21902203 * Ok, this is the main fork-routine.
21912204 *
kernel/module.c+44−38
@@ -98,6 +98,10 @@ DEFINE_MUTEX(module_mutex); · 97 unmodified lines
9898 EXPORT_SYMBOL_GPL(module_mutex);
9999 static LIST_HEAD(modules);
100100
101+/* Work queue for freeing init sections in success case */
102+static struct work_struct init_free_wq;
103+static struct llist_head init_free_list;
104+
101105 #ifdef CONFIG_MODULES_TREE_LOOKUP
102106
103107 /*
@@ -1949,9 +1953,16 @@ void module_enable_ro(const struct module *mod, bool after_init) · 1845 unmodified lines
19491953 if (!rodata_enabled)
19501954 return;
19511955
1956+ set_vm_flush_reset_perms(mod->core_layout.base);
1957+ set_vm_flush_reset_perms(mod->init_layout.base);
19521958 frob_text(&mod->core_layout, set_memory_ro);
1959+ frob_text(&mod->core_layout, set_memory_x);
1960+
19531961 frob_rodata(&mod->core_layout, set_memory_ro);
1962+
19541963 frob_text(&mod->init_layout, set_memory_ro);
1964+ frob_text(&mod->init_layout, set_memory_x);
1965+
19551966 frob_rodata(&mod->init_layout, set_memory_ro);
19561967
19571968 if (after_init)
@@ -1967,15 +1978,6 @@ static void module_enable_nx(const struct module *mod) · 9 unmodified lines
19671978 frob_writable_data(&mod->init_layout, set_memory_nx);
19681979 }
19691980
1970−static void module_disable_nx(const struct module *mod)
1971−{
1972− frob_rodata(&mod->core_layout, set_memory_x);
1973− frob_ro_after_init(&mod->core_layout, set_memory_x);
1974− frob_writable_data(&mod->core_layout, set_memory_x);
1975− frob_rodata(&mod->init_layout, set_memory_x);
1976− frob_writable_data(&mod->init_layout, set_memory_x);
1977−}
1978−
19791981 /* Iterate through all modules and set each module's text as RW */
19801982 void set_all_modules_text_rw(void)
19811983 {
@@ -2019,23 +2021,8 @@ void set_all_modules_text_ro(void) · 37 unmodified lines
20192021 }
20202022 mutex_unlock(&module_mutex);
20212023 }
2022−
2023−static void disable_ro_nx(const struct module_layout *layout)
2024−{
2025− if (rodata_enabled) {
2026− frob_text(layout, set_memory_rw);
2027− frob_rodata(layout, set_memory_rw);
2028− frob_ro_after_init(layout, set_memory_rw);
2029− }
2030− frob_rodata(layout, set_memory_x);
2031− frob_ro_after_init(layout, set_memory_x);
2032− frob_writable_data(layout, set_memory_x);
2033−}
2034−
20352024 #else
2036−static void disable_ro_nx(const struct module_layout *layout) { }
20372025 static void module_enable_nx(const struct module *mod) { }
2038−static void module_disable_nx(const struct module *mod) { }
20392026 #endif
20402027
20412028 #ifdef CONFIG_LIVEPATCH
@@ -2115,6 +2102,11 @@ static void free_module_elf(struct module *mod) · 73 unmodified lines
21152102
21162103 void __weak module_memfree(void *module_region)
21172104 {
2105+ /*
2106+ * This memory may be RO, and freeing RO memory in an interrupt is not
2107+ * supported by vmalloc.
2108+ */
2109+ WARN_ON(in_interrupt());
21182110 vfree(module_region);
21192111 }
21202112
@@ -2166,7 +2158,6 @@ static void free_module(struct module *mod) · 45 unmodified lines
21662158 mutex_unlock(&module_mutex);
21672159
21682160 /* This may be empty, but that's OK */
2169− disable_ro_nx(&mod->init_layout);
21702161 module_arch_freeing_init(mod);
21712162 module_memfree(mod->init_layout.base);
21722163 kfree(mod->args);
@@ -2176,7 +2167,6 @@ static void free_module(struct module *mod) · 3 unmodified lines
21762167 lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
21772168
21782169 /* Finally, free the core (containing the module structure) */
2179− disable_ro_nx(&mod->core_layout);
21802170 module_memfree(mod->core_layout.base);
21812171 }
21822172
@@ -3415,17 +3405,34 @@ static void do_mod_ctors(struct module *mod) · 1232 unmodified lines
34153405
34163406 /* For freeing module_init on success, in case kallsyms traversing */
34173407 struct mod_initfree {
3418− struct rcu_head rcu;
3408+ struct llist_node node;
34193409 void *module_init;
34203410 };
34213411
3422−static void do_free_init(struct rcu_head *head)
3412+static void do_free_init(struct work_struct *w)
34233413 {
3424− struct mod_initfree *m = container_of(head, struct mod_initfree, rcu);
3425− module_memfree(m->module_init);
3426− kfree(m);
3414+ struct llist_node *pos, *n, *list;
3415+ struct mod_initfree *initfree;
3416+
3417+ list = llist_del_all(&init_free_list);
3418+
3419+ synchronize_rcu();
3420+
3421+ llist_for_each_safe(pos, n, list) {
3422+ initfree = container_of(pos, struct mod_initfree, node);
3423+ module_memfree(initfree->module_init);
3424+ kfree(initfree);
3425+ }
34273426 }
34283427
3428+static int __init modules_wq_init(void)
3429+{
3430+ INIT_WORK(&init_free_wq, do_free_init);
3431+ init_llist_head(&init_free_list);
3432+ return 0;
3433+}
3434+module_init(modules_wq_init);
3435+
34293436 /*
34303437 * This is where the real work happens.
34313438 *
@@ -3502,7 +3509,6 @@ static noinline int do_init_module(struct module *mod) · 70 unmodified lines
35023509 #endif
35033510 module_enable_ro(mod, true);
35043511 mod_tree_remove_init(mod);
3505− disable_ro_nx(&mod->init_layout);
35063512 module_arch_freeing_init(mod);
35073513 mod->init_layout.base = NULL;
35083514 mod->init_layout.size = 0;
@@ -3513,14 +3519,18 @@ static noinline int do_init_module(struct module *mod) · 4 unmodified lines
35133519 * We want to free module_init, but be aware that kallsyms may be
35143520 * walking this with preempt disabled. In all the failure paths, we
35153521 * call synchronize_rcu(), but we don't want to slow down the success
3516− * path, so use actual RCU here.
3522+ * path. module_memfree() cannot be called in an interrupt, so do the
3523+ * work and call synchronize_rcu() in a work queue.
3524+ *
35173525 * Note that module_alloc() on most architectures creates W+X page
35183526 * mappings which won't be cleaned up until do_free_init() runs. Any
35193527 * code such as mark_rodata_ro() which depends on those mappings to
35203528 * be cleaned up needs to sync with the queued work - ie
35213529 * rcu_barrier()
35223530 */
3523− call_rcu(&freeinit->rcu, do_free_init);
3531+ if (llist_add(&freeinit->node, &init_free_list))
3532+ schedule_work(&init_free_wq);
3533+
35243534 mutex_unlock(&module_mutex);
35253535 wake_up_all(&module_wq);
35263536
@@ -3817,10 +3827,6 @@ static int load_module(struct load_info *info, const char __user *uargs, · 290 unmodified lines
38173827 module_bug_cleanup(mod);
38183828 mutex_unlock(&module_mutex);
38193829
3820− /* we can't deallocate the module until we clear memory protection */
3821− module_disable_ro(mod);
3822− module_disable_nx(mod);
3823−
38243830 ddebug_cleanup:
38253831 ftrace_release_mod(mod);
38263832 dynamic_debug_remove(mod, info->debug);
kernel/power/snapshot.c+3−2
@@ -1342,8 +1342,9 @@ static inline void do_copy_page(long *dst, long *src) · 1341 unmodified lines
13421342 * safe_copy_page - Copy a page in a safe way.
13431343 *
13441344 * Check if the page we are going to copy is marked as present in the kernel
1345− * page tables (this always is the case if CONFIG_DEBUG_PAGEALLOC is not set
1346− * and in that case kernel_page_present() always returns 'true').
1345+ * page tables. This always is the case if CONFIG_DEBUG_PAGEALLOC or
1346+ * CONFIG_ARCH_HAS_SET_DIRECT_MAP is not set. In that case kernel_page_present()
1347+ * always returns 'true'.
13471348 */
13481349 static void safe_copy_page(void *dst, struct page *s_page)
13491350 {
kernel/trace/bpf_trace.c+8−0
@@ -14,6 +14,8 @@ · 13 unmodified lines
1414 #include <linux/syscalls.h>
1515 #include <linux/error-injection.h>
1616
17+#include <asm/tlb.h>
18+
1719 #include "trace_probe.h"
1820 #include "trace.h"
1921
@@ -163,6 +165,10 @@ BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src, · 143 unmodified lines
163165 * access_ok() should prevent writing to non-user memory, but in
164166 * some situations (nommu, temporary switch, etc) access_ok() does
165167 * not provide enough validation, hence the check on KERNEL_DS.
168+ *
169+ * nmi_uaccess_okay() ensures the probe is not run in an interim
170+ * state, when the task or mm are switched. This is specifically
171+ * required to prevent the use of temporary mm.
166172 */
167173
168174 if (unlikely(in_interrupt() ||
@@ -170,6 +176,8 @@ BPF_CALL_3(bpf_probe_write_user, void *, unsafe_ptr, const void *, src, · 1 unmodified line
170176 return -EPERM;
171177 if (unlikely(uaccess_kernel()))
172178 return -EPERM;
179+ if (unlikely(!nmi_uaccess_okay()))
180+ return -EPERM;
173181 if (!access_ok(unsafe_ptr, size))
174182 return -EPERM;
175183
mm/page_alloc.c+5−2
@@ -1144,7 +1144,9 @@ static __always_inline bool free_pages_prepare(struct page *page, · 1143 unmodified lines
11441144 }
11451145 arch_free_page(page, order);
11461146 kernel_poison_pages(page, 1 << order, 0);
1147− kernel_map_pages(page, 1 << order, 0);
1147+ if (debug_pagealloc_enabled())
1148+ kernel_map_pages(page, 1 << order, 0);
1149+
11481150 kasan_free_nondeferred_pages(page, order);
11491151
11501152 return true;
@@ -2014,7 +2016,8 @@ inline void post_alloc_hook(struct page *page, unsigned int order, · 863 unmodified lines
20142016 set_page_refcounted(page);
20152017
20162018 arch_alloc_page(page, order);
2017− kernel_map_pages(page, 1 << order, 1);
2019+ if (debug_pagealloc_enabled())
2020+ kernel_map_pages(page, 1 << order, 1);
20182021 kasan_alloc_pages(page, order);
20192022 kernel_poison_pages(page, 1 << order, 1);
20202023 set_page_owner(page, order, gfp_flags);
mm/vmalloc.c+94−19
@@ -18,6 +18,7 @@ · 17 unmodified lines
1818 #include <linux/interrupt.h>
1919 #include <linux/proc_fs.h>
2020 #include <linux/seq_file.h>
21+#include <linux/set_memory.h>
2122 #include <linux/debugobjects.h>
2223 #include <linux/kallsyms.h>
2324 #include <linux/list.h>
@@ -1059,24 +1060,9 @@ static void vb_free(const void *addr, unsigned long size) · 1035 unmodified lines
10591060 spin_unlock(&vb->lock);
10601061 }
10611062
1062−/**
1063− * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1064− *
1065− * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1066− * to amortize TLB flushing overheads. What this means is that any page you
1067− * have now, may, in a former life, have been mapped into kernel virtual
1068− * address by the vmap layer and so there might be some CPUs with TLB entries
1069− * still referencing that page (additional to the regular 1:1 kernel mapping).
1070− *
1071− * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1072− * be sure that none of the pages we have control over will have any aliases
1073− * from the vmap layer.
1074− */
1075−void vm_unmap_aliases(void)
1063+static void _vm_unmap_aliases(unsigned long start, unsigned long end, int flush)
10761064 {
1077− unsigned long start = ULONG_MAX, end = 0;
10781065 int cpu;
1079− int flush = 0;
10801066
10811067 if (unlikely(!vmap_initialized))
10821068 return;
@@ -1113,6 +1099,27 @@ void vm_unmap_aliases(void) · 30 unmodified lines
11131099 flush_tlb_kernel_range(start, end);
11141100 mutex_unlock(&vmap_purge_lock);
11151101 }
1102+
1103+/**
1104+ * vm_unmap_aliases - unmap outstanding lazy aliases in the vmap layer
1105+ *
1106+ * The vmap/vmalloc layer lazily flushes kernel virtual mappings primarily
1107+ * to amortize TLB flushing overheads. What this means is that any page you
1108+ * have now, may, in a former life, have been mapped into kernel virtual
1109+ * address by the vmap layer and so there might be some CPUs with TLB entries
1110+ * still referencing that page (additional to the regular 1:1 kernel mapping).
1111+ *
1112+ * vm_unmap_aliases flushes all such lazy mappings. After it returns, we can
1113+ * be sure that none of the pages we have control over will have any aliases
1114+ * from the vmap layer.
1115+ */
1116+void vm_unmap_aliases(void)
1117+{
1118+ unsigned long start = ULONG_MAX, end = 0;
1119+ int flush = 0;
1120+
1121+ _vm_unmap_aliases(start, end, flush);
1122+}
11161123 EXPORT_SYMBOL_GPL(vm_unmap_aliases);
11171124
11181125 /**
@@ -1505,6 +1512,72 @@ struct vm_struct *remove_vm_area(const void *addr) · 386 unmodified lines
15051512 return NULL;
15061513 }
15071514
1515+static inline void set_area_direct_map(const struct vm_struct *area,
1516+ int (*set_direct_map)(struct page *page))
1517+{
1518+ int i;
1519+
1520+ for (i = 0; i < area->nr_pages; i++)
1521+ if (page_address(area->pages[i]))
1522+ set_direct_map(area->pages[i]);
1523+}
1524+
1525+/* Handle removing and resetting vm mappings related to the vm_struct. */
1526+static void vm_remove_mappings(struct vm_struct *area, int deallocate_pages)
1527+{
1528+ unsigned long addr = (unsigned long)area->addr;
1529+ unsigned long start = ULONG_MAX, end = 0;
1530+ int flush_reset = area->flags & VM_FLUSH_RESET_PERMS;
1531+ int i;
1532+
1533+ /*
1534+ * The below block can be removed when all architectures that have
1535+ * direct map permissions also have set_direct_map_() implementations.
1536+ * This is concerned with resetting the direct map any an vm alias with
1537+ * execute permissions, without leaving a RW+X window.
1538+ */
1539+ if (flush_reset && !IS_ENABLED(CONFIG_ARCH_HAS_SET_DIRECT_MAP)) {
1540+ set_memory_nx(addr, area->nr_pages);
1541+ set_memory_rw(addr, area->nr_pages);
1542+ }
1543+
1544+ remove_vm_area(area->addr);
1545+
1546+ /* If this is not VM_FLUSH_RESET_PERMS memory, no need for the below. */
1547+ if (!flush_reset)
1548+ return;
1549+
1550+ /*
1551+ * If not deallocating pages, just do the flush of the VM area and
1552+ * return.
1553+ */
1554+ if (!deallocate_pages) {
1555+ vm_unmap_aliases();
1556+ return;
1557+ }
1558+
1559+ /*
1560+ * If execution gets here, flush the vm mapping and reset the direct
1561+ * map. Find the start and end range of the direct mappings to make sure
1562+ * the vm_unmap_aliases() flush includes the direct map.
1563+ */
1564+ for (i = 0; i < area->nr_pages; i++) {
1565+ if (page_address(area->pages[i])) {
1566+ start = min(addr, start);
1567+ end = max(addr, end);
1568+ }
1569+ }
1570+
1571+ /*
1572+ * Set direct map to something invalid so that it won't be cached if
1573+ * there are any accesses after the TLB flush, then flush the TLB and
1574+ * reset the direct map permissions to the default.
1575+ */
1576+ set_area_direct_map(area, set_direct_map_invalid_noflush);
1577+ _vm_unmap_aliases(start, end, 1);
1578+ set_area_direct_map(area, set_direct_map_default_noflush);
1579+}
1580+
15081581 static void __vunmap(const void *addr, int deallocate_pages)
15091582 {
15101583 struct vm_struct *area;
@@ -1526,7 +1599,8 @@ static void __vunmap(const void *addr, int deallocate_pages) · 15 unmodified lines
15261599 debug_check_no_locks_freed(area->addr, get_vm_area_size(area));
15271600 debug_check_no_obj_freed(area->addr, get_vm_area_size(area));
15281601
1529− remove_vm_area(addr);
1602+ vm_remove_mappings(area, deallocate_pages);
1603+
15301604 if (deallocate_pages) {
15311605 int i;
15321606
@@ -1961,8 +2035,9 @@ EXPORT_SYMBOL(vzalloc_node); · 428 unmodified lines
19612035 */
19622036 void *vmalloc_exec(unsigned long size)
19632037 {
1964− return __vmalloc_node(size, 1, GFP_KERNEL, PAGE_KERNEL_EXEC,
1965− NUMA_NO_NODE, __builtin_return_address(0));
2038+ return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END,
2039+ GFP_KERNEL, PAGE_KERNEL_EXEC, VM_FLUSH_RESET_PERMS,
2040+ NUMA_NO_NODE, __builtin_return_address(0));
19662041 }
19672042
19682043 #if defined(CONFIG_64BIT) && defined(CONFIG_ZONE_DMA32)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t