GNU Linux-libre 4.9.326-gnu1
[releases.git] / arch / x86 / mm / fault.c
1 /*
2  *  Copyright (C) 1995  Linus Torvalds
3  *  Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
4  *  Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
5  */
6 #include <linux/sched.h>                /* test_thread_flag(), ...      */
7 #include <linux/kdebug.h>               /* oops_begin/end, ...          */
8 #include <linux/extable.h>              /* search_exception_tables      */
9 #include <linux/bootmem.h>              /* max_low_pfn                  */
10 #include <linux/kprobes.h>              /* NOKPROBE_SYMBOL, ...         */
11 #include <linux/mmiotrace.h>            /* kmmio_handler, ...           */
12 #include <linux/perf_event.h>           /* perf_sw_event                */
13 #include <linux/hugetlb.h>              /* hstate_index_to_shift        */
14 #include <linux/prefetch.h>             /* prefetchw                    */
15 #include <linux/context_tracking.h>     /* exception_enter(), ...       */
16 #include <linux/uaccess.h>              /* faulthandler_disabled()      */
17
18 #include <asm/cpufeature.h>             /* boot_cpu_has, ...            */
19 #include <asm/traps.h>                  /* dotraplinkage, ...           */
20 #include <asm/pgalloc.h>                /* pgd_*(), ...                 */
21 #include <asm/kmemcheck.h>              /* kmemcheck_*(), ...           */
22 #include <asm/fixmap.h>                 /* VSYSCALL_ADDR                */
23 #include <asm/vsyscall.h>               /* emulate_vsyscall             */
24 #include <asm/vm86.h>                   /* struct vm86                  */
25 #include <asm/mmu_context.h>            /* vma_pkey()                   */
26 #include <asm/sections.h>
27
28 #define CREATE_TRACE_POINTS
29 #include <asm/trace/exceptions.h>
30
31 /*
32  * Page fault error code bits:
33  *
34  *   bit 0 ==    0: no page found       1: protection fault
35  *   bit 1 ==    0: read access         1: write access
36  *   bit 2 ==    0: kernel-mode access  1: user-mode access
37  *   bit 3 ==                           1: use of reserved bit detected
38  *   bit 4 ==                           1: fault was an instruction fetch
39  *   bit 5 ==                           1: protection keys block access
40  */
41 enum x86_pf_error_code {
42
43         PF_PROT         =               1 << 0,
44         PF_WRITE        =               1 << 1,
45         PF_USER         =               1 << 2,
46         PF_RSVD         =               1 << 3,
47         PF_INSTR        =               1 << 4,
48         PF_PK           =               1 << 5,
49 };
50
51 /*
52  * Returns 0 if mmiotrace is disabled, or if the fault is not
53  * handled by mmiotrace:
54  */
55 static nokprobe_inline int
56 kmmio_fault(struct pt_regs *regs, unsigned long addr)
57 {
58         if (unlikely(is_kmmio_active()))
59                 if (kmmio_handler(regs, addr) == 1)
60                         return -1;
61         return 0;
62 }
63
64 static nokprobe_inline int kprobes_fault(struct pt_regs *regs)
65 {
66         int ret = 0;
67
68         /* kprobe_running() needs smp_processor_id() */
69         if (kprobes_built_in() && !user_mode(regs)) {
70                 preempt_disable();
71                 if (kprobe_running() && kprobe_fault_handler(regs, 14))
72                         ret = 1;
73                 preempt_enable();
74         }
75
76         return ret;
77 }
78
79 /*
80  * Prefetch quirks:
81  *
82  * 32-bit mode:
83  *
84  *   Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
85  *   Check that here and ignore it.
86  *
87  * 64-bit mode:
88  *
89  *   Sometimes the CPU reports invalid exceptions on prefetch.
90  *   Check that here and ignore it.
91  *
92  * Opcode checker based on code by Richard Brunner.
93  */
94 static inline int
95 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
96                       unsigned char opcode, int *prefetch)
97 {
98         unsigned char instr_hi = opcode & 0xf0;
99         unsigned char instr_lo = opcode & 0x0f;
100
101         switch (instr_hi) {
102         case 0x20:
103         case 0x30:
104                 /*
105                  * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
106                  * In X86_64 long mode, the CPU will signal invalid
107                  * opcode if some of these prefixes are present so
108                  * X86_64 will never get here anyway
109                  */
110                 return ((instr_lo & 7) == 0x6);
111 #ifdef CONFIG_X86_64
112         case 0x40:
113                 /*
114                  * In AMD64 long mode 0x40..0x4F are valid REX prefixes
115                  * Need to figure out under what instruction mode the
116                  * instruction was issued. Could check the LDT for lm,
117                  * but for now it's good enough to assume that long
118                  * mode only uses well known segments or kernel.
119                  */
120                 return (!user_mode(regs) || user_64bit_mode(regs));
121 #endif
122         case 0x60:
123                 /* 0x64 thru 0x67 are valid prefixes in all modes. */
124                 return (instr_lo & 0xC) == 0x4;
125         case 0xF0:
126                 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
127                 return !instr_lo || (instr_lo>>1) == 1;
128         case 0x00:
129                 /* Prefetch instruction is 0x0F0D or 0x0F18 */
130                 if (probe_kernel_address(instr, opcode))
131                         return 0;
132
133                 *prefetch = (instr_lo == 0xF) &&
134                         (opcode == 0x0D || opcode == 0x18);
135                 return 0;
136         default:
137                 return 0;
138         }
139 }
140
141 static int
142 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
143 {
144         unsigned char *max_instr;
145         unsigned char *instr;
146         int prefetch = 0;
147
148         /*
149          * If it was a exec (instruction fetch) fault on NX page, then
150          * do not ignore the fault:
151          */
152         if (error_code & PF_INSTR)
153                 return 0;
154
155         instr = (void *)convert_ip_to_linear(current, regs);
156         max_instr = instr + 15;
157
158         if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE_MAX)
159                 return 0;
160
161         while (instr < max_instr) {
162                 unsigned char opcode;
163
164                 if (probe_kernel_address(instr, opcode))
165                         break;
166
167                 instr++;
168
169                 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
170                         break;
171         }
172         return prefetch;
173 }
174
175 /*
176  * A protection key fault means that the PKRU value did not allow
177  * access to some PTE.  Userspace can figure out what PKRU was
178  * from the XSAVE state, and this function fills out a field in
179  * siginfo so userspace can discover which protection key was set
180  * on the PTE.
181  *
182  * If we get here, we know that the hardware signaled a PF_PK
183  * fault and that there was a VMA once we got in the fault
184  * handler.  It does *not* guarantee that the VMA we find here
185  * was the one that we faulted on.
186  *
187  * 1. T1   : mprotect_key(foo, PAGE_SIZE, pkey=4);
188  * 2. T1   : set PKRU to deny access to pkey=4, touches page
189  * 3. T1   : faults...
190  * 4.    T2: mprotect_key(foo, PAGE_SIZE, pkey=5);
191  * 5. T1   : enters fault handler, takes mmap_sem, etc...
192  * 6. T1   : reaches here, sees vma_pkey(vma)=5, when we really
193  *           faulted on a pte with its pkey=4.
194  */
195 static void fill_sig_info_pkey(int si_signo, int si_code, siginfo_t *info,
196                 u32 *pkey)
197 {
198         /* This is effectively an #ifdef */
199         if (!boot_cpu_has(X86_FEATURE_OSPKE))
200                 return;
201
202         /* Fault not from Protection Keys: nothing to do */
203         if ((si_code != SEGV_PKUERR) || (si_signo != SIGSEGV))
204                 return;
205         /*
206          * force_sig_info_fault() is called from a number of
207          * contexts, some of which have a VMA and some of which
208          * do not.  The PF_PK handing happens after we have a
209          * valid VMA, so we should never reach this without a
210          * valid VMA.
211          */
212         if (!pkey) {
213                 WARN_ONCE(1, "PKU fault with no VMA passed in");
214                 info->si_pkey = 0;
215                 return;
216         }
217         /*
218          * si_pkey should be thought of as a strong hint, but not
219          * absolutely guranteed to be 100% accurate because of
220          * the race explained above.
221          */
222         info->si_pkey = *pkey;
223 }
224
225 static void
226 force_sig_info_fault(int si_signo, int si_code, unsigned long address,
227                      struct task_struct *tsk, u32 *pkey, int fault)
228 {
229         unsigned lsb = 0;
230         siginfo_t info;
231
232         info.si_signo   = si_signo;
233         info.si_errno   = 0;
234         info.si_code    = si_code;
235         info.si_addr    = (void __user *)address;
236         if (fault & VM_FAULT_HWPOISON_LARGE)
237                 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault)); 
238         if (fault & VM_FAULT_HWPOISON)
239                 lsb = PAGE_SHIFT;
240         info.si_addr_lsb = lsb;
241
242         fill_sig_info_pkey(si_signo, si_code, &info, pkey);
243
244         force_sig_info(si_signo, &info, tsk);
245 }
246
247 DEFINE_SPINLOCK(pgd_lock);
248 LIST_HEAD(pgd_list);
249
250 #ifdef CONFIG_X86_32
251 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
252 {
253         unsigned index = pgd_index(address);
254         pgd_t *pgd_k;
255         pud_t *pud, *pud_k;
256         pmd_t *pmd, *pmd_k;
257
258         pgd += index;
259         pgd_k = init_mm.pgd + index;
260
261         if (!pgd_present(*pgd_k))
262                 return NULL;
263
264         /*
265          * set_pgd(pgd, *pgd_k); here would be useless on PAE
266          * and redundant with the set_pmd() on non-PAE. As would
267          * set_pud.
268          */
269         pud = pud_offset(pgd, address);
270         pud_k = pud_offset(pgd_k, address);
271         if (!pud_present(*pud_k))
272                 return NULL;
273
274         pmd = pmd_offset(pud, address);
275         pmd_k = pmd_offset(pud_k, address);
276
277         if (pmd_present(*pmd) != pmd_present(*pmd_k))
278                 set_pmd(pmd, *pmd_k);
279
280         if (!pmd_present(*pmd_k))
281                 return NULL;
282         else
283                 BUG_ON(pmd_pfn(*pmd) != pmd_pfn(*pmd_k));
284
285         return pmd_k;
286 }
287
288 static void vmalloc_sync(void)
289 {
290         unsigned long address;
291
292         if (SHARED_KERNEL_PMD)
293                 return;
294
295         for (address = VMALLOC_START & PMD_MASK;
296              address >= TASK_SIZE_MAX && address < FIXADDR_TOP;
297              address += PMD_SIZE) {
298                 struct page *page;
299
300                 spin_lock(&pgd_lock);
301                 list_for_each_entry(page, &pgd_list, lru) {
302                         spinlock_t *pgt_lock;
303
304                         /* the pgt_lock only for Xen */
305                         pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
306
307                         spin_lock(pgt_lock);
308                         vmalloc_sync_one(page_address(page), address);
309                         spin_unlock(pgt_lock);
310                 }
311                 spin_unlock(&pgd_lock);
312         }
313 }
314
315 void vmalloc_sync_mappings(void)
316 {
317         vmalloc_sync();
318 }
319
320 void vmalloc_sync_unmappings(void)
321 {
322         vmalloc_sync();
323 }
324
325 /*
326  * 32-bit:
327  *
328  *   Handle a fault on the vmalloc or module mapping area
329  */
330 static noinline int vmalloc_fault(unsigned long address)
331 {
332         unsigned long pgd_paddr;
333         pmd_t *pmd_k;
334         pte_t *pte_k;
335
336         /* Make sure we are in vmalloc area: */
337         if (!(address >= VMALLOC_START && address < VMALLOC_END))
338                 return -1;
339
340         /*
341          * Synchronize this task's top level page-table
342          * with the 'reference' page table.
343          *
344          * Do _not_ use "current" here. We might be inside
345          * an interrupt in the middle of a task switch..
346          */
347         pgd_paddr = read_cr3();
348         pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
349         if (!pmd_k)
350                 return -1;
351
352         if (pmd_large(*pmd_k))
353                 return 0;
354
355         pte_k = pte_offset_kernel(pmd_k, address);
356         if (!pte_present(*pte_k))
357                 return -1;
358
359         return 0;
360 }
361 NOKPROBE_SYMBOL(vmalloc_fault);
362
363 /*
364  * Did it hit the DOS screen memory VA from vm86 mode?
365  */
366 static inline void
367 check_v8086_mode(struct pt_regs *regs, unsigned long address,
368                  struct task_struct *tsk)
369 {
370 #ifdef CONFIG_VM86
371         unsigned long bit;
372
373         if (!v8086_mode(regs) || !tsk->thread.vm86)
374                 return;
375
376         bit = (address - 0xA0000) >> PAGE_SHIFT;
377         if (bit < 32)
378                 tsk->thread.vm86->screen_bitmap |= 1 << bit;
379 #endif
380 }
381
382 static bool low_pfn(unsigned long pfn)
383 {
384         return pfn < max_low_pfn;
385 }
386
387 static void dump_pagetable(unsigned long address)
388 {
389         pgd_t *base = __va(read_cr3());
390         pgd_t *pgd = &base[pgd_index(address)];
391         pmd_t *pmd;
392         pte_t *pte;
393
394 #ifdef CONFIG_X86_PAE
395         printk("*pdpt = %016Lx ", pgd_val(*pgd));
396         if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
397                 goto out;
398 #endif
399         pmd = pmd_offset(pud_offset(pgd, address), address);
400         printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
401
402         /*
403          * We must not directly access the pte in the highpte
404          * case if the page table is located in highmem.
405          * And let's rather not kmap-atomic the pte, just in case
406          * it's allocated already:
407          */
408         if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
409                 goto out;
410
411         pte = pte_offset_kernel(pmd, address);
412         printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
413 out:
414         printk("\n");
415 }
416
417 #else /* CONFIG_X86_64: */
418
419 void vmalloc_sync_mappings(void)
420 {
421         /*
422          * 64-bit mappings might allocate new p4d/pud pages
423          * that need to be propagated to all tasks' PGDs.
424          */
425         sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END, 0);
426 }
427
428 void vmalloc_sync_unmappings(void)
429 {
430         /*
431          * Unmappings never allocate or free p4d/pud pages.
432          * No work is required here.
433          */
434 }
435
436 /*
437  * 64-bit:
438  *
439  *   Handle a fault on the vmalloc area
440  */
441 static noinline int vmalloc_fault(unsigned long address)
442 {
443         pgd_t *pgd, *pgd_ref;
444         pud_t *pud, *pud_ref;
445         pmd_t *pmd, *pmd_ref;
446         pte_t *pte, *pte_ref;
447
448         /* Make sure we are in vmalloc area: */
449         if (!(address >= VMALLOC_START && address < VMALLOC_END))
450                 return -1;
451
452         /*
453          * Copy kernel mappings over when needed. This can also
454          * happen within a race in page table update. In the later
455          * case just flush:
456          */
457         pgd = (pgd_t *)__va(read_cr3()) + pgd_index(address);
458         pgd_ref = pgd_offset_k(address);
459         if (pgd_none(*pgd_ref))
460                 return -1;
461
462         if (pgd_none(*pgd)) {
463                 set_pgd(pgd, *pgd_ref);
464                 arch_flush_lazy_mmu_mode();
465         } else {
466                 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
467         }
468
469         /*
470          * Below here mismatches are bugs because these lower tables
471          * are shared:
472          */
473
474         pud = pud_offset(pgd, address);
475         pud_ref = pud_offset(pgd_ref, address);
476         if (pud_none(*pud_ref))
477                 return -1;
478
479         if (pud_none(*pud) || pud_pfn(*pud) != pud_pfn(*pud_ref))
480                 BUG();
481
482         if (pud_large(*pud))
483                 return 0;
484
485         pmd = pmd_offset(pud, address);
486         pmd_ref = pmd_offset(pud_ref, address);
487         if (pmd_none(*pmd_ref))
488                 return -1;
489
490         if (pmd_none(*pmd) || pmd_pfn(*pmd) != pmd_pfn(*pmd_ref))
491                 BUG();
492
493         if (pmd_large(*pmd))
494                 return 0;
495
496         pte_ref = pte_offset_kernel(pmd_ref, address);
497         if (!pte_present(*pte_ref))
498                 return -1;
499
500         pte = pte_offset_kernel(pmd, address);
501
502         /*
503          * Don't use pte_page here, because the mappings can point
504          * outside mem_map, and the NUMA hash lookup cannot handle
505          * that:
506          */
507         if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
508                 BUG();
509
510         return 0;
511 }
512 NOKPROBE_SYMBOL(vmalloc_fault);
513
514 #ifdef CONFIG_CPU_SUP_AMD
515 static const char errata93_warning[] =
516 KERN_ERR 
517 "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
518 "******* Working around it, but it may cause SEGVs or burn power.\n"
519 "******* Please consider a BIOS update.\n"
520 "******* Disabling USB legacy in the BIOS may also help.\n";
521 #endif
522
523 /*
524  * No vm86 mode in 64-bit mode:
525  */
526 static inline void
527 check_v8086_mode(struct pt_regs *regs, unsigned long address,
528                  struct task_struct *tsk)
529 {
530 }
531
532 static int bad_address(void *p)
533 {
534         unsigned long dummy;
535
536         return probe_kernel_address((unsigned long *)p, dummy);
537 }
538
539 static void dump_pagetable(unsigned long address)
540 {
541         pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
542         pgd_t *pgd = base + pgd_index(address);
543         pud_t *pud;
544         pmd_t *pmd;
545         pte_t *pte;
546
547         if (bad_address(pgd))
548                 goto bad;
549
550         printk("PGD %lx ", pgd_val(*pgd));
551
552         if (!pgd_present(*pgd))
553                 goto out;
554
555         pud = pud_offset(pgd, address);
556         if (bad_address(pud))
557                 goto bad;
558
559         printk("PUD %lx ", pud_val(*pud));
560         if (!pud_present(*pud) || pud_large(*pud))
561                 goto out;
562
563         pmd = pmd_offset(pud, address);
564         if (bad_address(pmd))
565                 goto bad;
566
567         printk("PMD %lx ", pmd_val(*pmd));
568         if (!pmd_present(*pmd) || pmd_large(*pmd))
569                 goto out;
570
571         pte = pte_offset_kernel(pmd, address);
572         if (bad_address(pte))
573                 goto bad;
574
575         printk("PTE %lx", pte_val(*pte));
576 out:
577         printk("\n");
578         return;
579 bad:
580         printk("BAD\n");
581 }
582
583 #endif /* CONFIG_X86_64 */
584
585 /*
586  * Workaround for K8 erratum #93 & buggy BIOS.
587  *
588  * BIOS SMM functions are required to use a specific workaround
589  * to avoid corruption of the 64bit RIP register on C stepping K8.
590  *
591  * A lot of BIOS that didn't get tested properly miss this.
592  *
593  * The OS sees this as a page fault with the upper 32bits of RIP cleared.
594  * Try to work around it here.
595  *
596  * Note we only handle faults in kernel here.
597  * Does nothing on 32-bit.
598  */
599 static int is_errata93(struct pt_regs *regs, unsigned long address)
600 {
601 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
602         if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
603             || boot_cpu_data.x86 != 0xf)
604                 return 0;
605
606         if (address != regs->ip)
607                 return 0;
608
609         if ((address >> 32) != 0)
610                 return 0;
611
612         address |= 0xffffffffUL << 32;
613         if ((address >= (u64)_stext && address <= (u64)_etext) ||
614             (address >= MODULES_VADDR && address <= MODULES_END)) {
615                 printk_once(errata93_warning);
616                 regs->ip = address;
617                 return 1;
618         }
619 #endif
620         return 0;
621 }
622
623 /*
624  * Work around K8 erratum #100 K8 in compat mode occasionally jumps
625  * to illegal addresses >4GB.
626  *
627  * We catch this in the page fault handler because these addresses
628  * are not reachable. Just detect this case and return.  Any code
629  * segment in LDT is compatibility mode.
630  */
631 static int is_errata100(struct pt_regs *regs, unsigned long address)
632 {
633 #ifdef CONFIG_X86_64
634         if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
635                 return 1;
636 #endif
637         return 0;
638 }
639
640 static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
641 {
642 #ifdef CONFIG_X86_F00F_BUG
643         unsigned long nr;
644
645         /*
646          * Pentium F0 0F C7 C8 bug workaround:
647          */
648         if (boot_cpu_has_bug(X86_BUG_F00F)) {
649                 nr = (address - idt_descr.address) >> 3;
650
651                 if (nr == 6) {
652                         do_invalid_op(regs, 0);
653                         return 1;
654                 }
655         }
656 #endif
657         return 0;
658 }
659
660 static const char nx_warning[] = KERN_CRIT
661 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
662 static const char smep_warning[] = KERN_CRIT
663 "unable to execute userspace code (SMEP?) (uid: %d)\n";
664
665 static void
666 show_fault_oops(struct pt_regs *regs, unsigned long error_code,
667                 unsigned long address)
668 {
669         if (!oops_may_print())
670                 return;
671
672         if (error_code & PF_INSTR) {
673                 unsigned int level;
674                 pgd_t *pgd;
675                 pte_t *pte;
676
677                 pgd = __va(read_cr3() & PHYSICAL_PAGE_MASK);
678                 pgd += pgd_index(address);
679
680                 pte = lookup_address_in_pgd(pgd, address, &level);
681
682                 if (pte && pte_present(*pte) && !pte_exec(*pte))
683                         printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
684                 if (pte && pte_present(*pte) && pte_exec(*pte) &&
685                                 (pgd_flags(*pgd) & _PAGE_USER) &&
686                                 (__read_cr4() & X86_CR4_SMEP))
687                         printk(smep_warning, from_kuid(&init_user_ns, current_uid()));
688         }
689
690         printk(KERN_ALERT "BUG: unable to handle kernel ");
691         if (address < PAGE_SIZE)
692                 printk(KERN_CONT "NULL pointer dereference");
693         else
694                 printk(KERN_CONT "paging request");
695
696         printk(KERN_CONT " at %p\n", (void *) address);
697         printk(KERN_ALERT "IP:");
698         printk_address(regs->ip);
699
700         dump_pagetable(address);
701 }
702
703 static noinline void
704 pgtable_bad(struct pt_regs *regs, unsigned long error_code,
705             unsigned long address)
706 {
707         struct task_struct *tsk;
708         unsigned long flags;
709         int sig;
710
711         flags = oops_begin();
712         tsk = current;
713         sig = SIGKILL;
714
715         printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
716                tsk->comm, address);
717         dump_pagetable(address);
718
719         tsk->thread.cr2         = address;
720         tsk->thread.trap_nr     = X86_TRAP_PF;
721         tsk->thread.error_code  = error_code;
722
723         if (__die("Bad pagetable", regs, error_code))
724                 sig = 0;
725
726         oops_end(flags, regs, sig);
727 }
728
729 static noinline void
730 no_context(struct pt_regs *regs, unsigned long error_code,
731            unsigned long address, int signal, int si_code)
732 {
733         struct task_struct *tsk = current;
734         unsigned long flags;
735         int sig;
736
737         /* Are we prepared to handle this kernel fault? */
738         if (fixup_exception(regs, X86_TRAP_PF)) {
739                 /*
740                  * Any interrupt that takes a fault gets the fixup. This makes
741                  * the below recursive fault logic only apply to a faults from
742                  * task context.
743                  */
744                 if (in_interrupt())
745                         return;
746
747                 /*
748                  * Per the above we're !in_interrupt(), aka. task context.
749                  *
750                  * In this case we need to make sure we're not recursively
751                  * faulting through the emulate_vsyscall() logic.
752                  */
753                 if (current->thread.sig_on_uaccess_err && signal) {
754                         tsk->thread.trap_nr = X86_TRAP_PF;
755                         tsk->thread.error_code = error_code | PF_USER;
756                         tsk->thread.cr2 = address;
757
758                         /* XXX: hwpoison faults will set the wrong code. */
759                         force_sig_info_fault(signal, si_code, address,
760                                              tsk, NULL, 0);
761                 }
762
763                 /*
764                  * Barring that, we can do the fixup and be happy.
765                  */
766                 return;
767         }
768
769 #ifdef CONFIG_VMAP_STACK
770         /*
771          * Stack overflow?  During boot, we can fault near the initial
772          * stack in the direct map, but that's not an overflow -- check
773          * that we're in vmalloc space to avoid this.
774          */
775         if (is_vmalloc_addr((void *)address) &&
776             (((unsigned long)tsk->stack - 1 - address < PAGE_SIZE) ||
777              address - ((unsigned long)tsk->stack + THREAD_SIZE) < PAGE_SIZE)) {
778                 register void *__sp asm("rsp");
779                 unsigned long stack = this_cpu_read(orig_ist.ist[DOUBLEFAULT_STACK]) - sizeof(void *);
780                 /*
781                  * We're likely to be running with very little stack space
782                  * left.  It's plausible that we'd hit this condition but
783                  * double-fault even before we get this far, in which case
784                  * we're fine: the double-fault handler will deal with it.
785                  *
786                  * We don't want to make it all the way into the oops code
787                  * and then double-fault, though, because we're likely to
788                  * break the console driver and lose most of the stack dump.
789                  */
790                 asm volatile ("movq %[stack], %%rsp\n\t"
791                               "call handle_stack_overflow\n\t"
792                               "1: jmp 1b"
793                               : "+r" (__sp)
794                               : "D" ("kernel stack overflow (page fault)"),
795                                 "S" (regs), "d" (address),
796                                 [stack] "rm" (stack));
797                 unreachable();
798         }
799 #endif
800
801         /*
802          * 32-bit:
803          *
804          *   Valid to do another page fault here, because if this fault
805          *   had been triggered by is_prefetch fixup_exception would have
806          *   handled it.
807          *
808          * 64-bit:
809          *
810          *   Hall of shame of CPU/BIOS bugs.
811          */
812         if (is_prefetch(regs, error_code, address))
813                 return;
814
815         if (is_errata93(regs, address))
816                 return;
817
818         /*
819          * Oops. The kernel tried to access some bad page. We'll have to
820          * terminate things with extreme prejudice:
821          */
822         flags = oops_begin();
823
824         show_fault_oops(regs, error_code, address);
825
826         if (task_stack_end_corrupted(tsk))
827                 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
828
829         tsk->thread.cr2         = address;
830         tsk->thread.trap_nr     = X86_TRAP_PF;
831         tsk->thread.error_code  = error_code;
832
833         sig = SIGKILL;
834         if (__die("Oops", regs, error_code))
835                 sig = 0;
836
837         /* Executive summary in case the body of the oops scrolled away */
838         printk(KERN_DEFAULT "CR2: %016lx\n", address);
839
840         oops_end(flags, regs, sig);
841 }
842
843 /*
844  * Print out info about fatal segfaults, if the show_unhandled_signals
845  * sysctl is set:
846  */
847 static inline void
848 show_signal_msg(struct pt_regs *regs, unsigned long error_code,
849                 unsigned long address, struct task_struct *tsk)
850 {
851         if (!unhandled_signal(tsk, SIGSEGV))
852                 return;
853
854         if (!printk_ratelimit())
855                 return;
856
857         printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
858                 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
859                 tsk->comm, task_pid_nr(tsk), address,
860                 (void *)regs->ip, (void *)regs->sp, error_code);
861
862         print_vma_addr(KERN_CONT " in ", regs->ip);
863
864         printk(KERN_CONT "\n");
865 }
866
867 static void
868 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
869                        unsigned long address, u32 *pkey, int si_code)
870 {
871         struct task_struct *tsk = current;
872
873         /* User mode accesses just cause a SIGSEGV */
874         if (error_code & PF_USER) {
875                 /*
876                  * It's possible to have interrupts off here:
877                  */
878                 local_irq_enable();
879
880                 /*
881                  * Valid to do another page fault here because this one came
882                  * from user space:
883                  */
884                 if (is_prefetch(regs, error_code, address))
885                         return;
886
887                 if (is_errata100(regs, address))
888                         return;
889
890 #ifdef CONFIG_X86_64
891                 /*
892                  * Instruction fetch faults in the vsyscall page might need
893                  * emulation.
894                  */
895                 if (unlikely((error_code & PF_INSTR) &&
896                              ((address & ~0xfff) == VSYSCALL_ADDR))) {
897                         if (emulate_vsyscall(regs, address))
898                                 return;
899                 }
900 #endif
901
902                 /*
903                  * To avoid leaking information about the kernel page table
904                  * layout, pretend that user-mode accesses to kernel addresses
905                  * are always protection faults.
906                  */
907                 if (address >= TASK_SIZE_MAX)
908                         error_code |= PF_PROT;
909
910                 if (likely(show_unhandled_signals))
911                         show_signal_msg(regs, error_code, address, tsk);
912
913                 tsk->thread.cr2         = address;
914                 tsk->thread.error_code  = error_code;
915                 tsk->thread.trap_nr     = X86_TRAP_PF;
916
917                 force_sig_info_fault(SIGSEGV, si_code, address, tsk, pkey, 0);
918
919                 return;
920         }
921
922         if (is_f00f_bug(regs, address))
923                 return;
924
925         no_context(regs, error_code, address, SIGSEGV, si_code);
926 }
927
928 static noinline void
929 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
930                      unsigned long address, u32 *pkey)
931 {
932         __bad_area_nosemaphore(regs, error_code, address, pkey, SEGV_MAPERR);
933 }
934
935 static void
936 __bad_area(struct pt_regs *regs, unsigned long error_code,
937            unsigned long address,  struct vm_area_struct *vma, int si_code)
938 {
939         struct mm_struct *mm = current->mm;
940         u32 pkey;
941
942         if (vma)
943                 pkey = vma_pkey(vma);
944
945         /*
946          * Something tried to access memory that isn't in our memory map..
947          * Fix it, but check if it's kernel or user first..
948          */
949         up_read(&mm->mmap_sem);
950
951         __bad_area_nosemaphore(regs, error_code, address,
952                                (vma) ? &pkey : NULL, si_code);
953 }
954
955 static noinline void
956 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
957 {
958         __bad_area(regs, error_code, address, NULL, SEGV_MAPERR);
959 }
960
961 static inline bool bad_area_access_from_pkeys(unsigned long error_code,
962                 struct vm_area_struct *vma)
963 {
964         /* This code is always called on the current mm */
965         bool foreign = false;
966
967         if (!boot_cpu_has(X86_FEATURE_OSPKE))
968                 return false;
969         if (error_code & PF_PK)
970                 return true;
971         /* this checks permission keys on the VMA: */
972         if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
973                                 (error_code & PF_INSTR), foreign))
974                 return true;
975         return false;
976 }
977
978 static noinline void
979 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
980                       unsigned long address, struct vm_area_struct *vma)
981 {
982         /*
983          * This OSPKE check is not strictly necessary at runtime.
984          * But, doing it this way allows compiler optimizations
985          * if pkeys are compiled out.
986          */
987         if (bad_area_access_from_pkeys(error_code, vma))
988                 __bad_area(regs, error_code, address, vma, SEGV_PKUERR);
989         else
990                 __bad_area(regs, error_code, address, vma, SEGV_ACCERR);
991 }
992
993 static void
994 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
995           u32 *pkey, unsigned int fault)
996 {
997         struct task_struct *tsk = current;
998         int code = BUS_ADRERR;
999
1000         /* Kernel mode? Handle exceptions or die: */
1001         if (!(error_code & PF_USER)) {
1002                 no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
1003                 return;
1004         }
1005
1006         /* User-space => ok to do another page fault: */
1007         if (is_prefetch(regs, error_code, address))
1008                 return;
1009
1010         tsk->thread.cr2         = address;
1011         tsk->thread.error_code  = error_code;
1012         tsk->thread.trap_nr     = X86_TRAP_PF;
1013
1014 #ifdef CONFIG_MEMORY_FAILURE
1015         if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
1016                 printk(KERN_ERR
1017         "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
1018                         tsk->comm, tsk->pid, address);
1019                 code = BUS_MCEERR_AR;
1020         }
1021 #endif
1022         force_sig_info_fault(SIGBUS, code, address, tsk, pkey, fault);
1023 }
1024
1025 static noinline void
1026 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
1027                unsigned long address, u32 *pkey, unsigned int fault)
1028 {
1029         if (fatal_signal_pending(current) && !(error_code & PF_USER)) {
1030                 no_context(regs, error_code, address, 0, 0);
1031                 return;
1032         }
1033
1034         if (fault & VM_FAULT_OOM) {
1035                 /* Kernel mode? Handle exceptions or die: */
1036                 if (!(error_code & PF_USER)) {
1037                         no_context(regs, error_code, address,
1038                                    SIGSEGV, SEGV_MAPERR);
1039                         return;
1040                 }
1041
1042                 /*
1043                  * We ran out of memory, call the OOM killer, and return the
1044                  * userspace (which will retry the fault, or kill us if we got
1045                  * oom-killed):
1046                  */
1047                 pagefault_out_of_memory();
1048         } else {
1049                 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
1050                              VM_FAULT_HWPOISON_LARGE))
1051                         do_sigbus(regs, error_code, address, pkey, fault);
1052                 else if (fault & VM_FAULT_SIGSEGV)
1053                         bad_area_nosemaphore(regs, error_code, address, pkey);
1054                 else
1055                         BUG();
1056         }
1057 }
1058
1059 static int spurious_fault_check(unsigned long error_code, pte_t *pte)
1060 {
1061         if ((error_code & PF_WRITE) && !pte_write(*pte))
1062                 return 0;
1063
1064         if ((error_code & PF_INSTR) && !pte_exec(*pte))
1065                 return 0;
1066         /*
1067          * Note: We do not do lazy flushing on protection key
1068          * changes, so no spurious fault will ever set PF_PK.
1069          */
1070         if ((error_code & PF_PK))
1071                 return 1;
1072
1073         return 1;
1074 }
1075
1076 /*
1077  * Handle a spurious fault caused by a stale TLB entry.
1078  *
1079  * This allows us to lazily refresh the TLB when increasing the
1080  * permissions of a kernel page (RO -> RW or NX -> X).  Doing it
1081  * eagerly is very expensive since that implies doing a full
1082  * cross-processor TLB flush, even if no stale TLB entries exist
1083  * on other processors.
1084  *
1085  * Spurious faults may only occur if the TLB contains an entry with
1086  * fewer permission than the page table entry.  Non-present (P = 0)
1087  * and reserved bit (R = 1) faults are never spurious.
1088  *
1089  * There are no security implications to leaving a stale TLB when
1090  * increasing the permissions on a page.
1091  *
1092  * Returns non-zero if a spurious fault was handled, zero otherwise.
1093  *
1094  * See Intel Developer's Manual Vol 3 Section 4.10.4.3, bullet 3
1095  * (Optional Invalidation).
1096  */
1097 static noinline int
1098 spurious_fault(unsigned long error_code, unsigned long address)
1099 {
1100         pgd_t *pgd;
1101         pud_t *pud;
1102         pmd_t *pmd;
1103         pte_t *pte;
1104         int ret;
1105
1106         /*
1107          * Only writes to RO or instruction fetches from NX may cause
1108          * spurious faults.
1109          *
1110          * These could be from user or supervisor accesses but the TLB
1111          * is only lazily flushed after a kernel mapping protection
1112          * change, so user accesses are not expected to cause spurious
1113          * faults.
1114          */
1115         if (error_code != (PF_WRITE | PF_PROT)
1116             && error_code != (PF_INSTR | PF_PROT))
1117                 return 0;
1118
1119         pgd = init_mm.pgd + pgd_index(address);
1120         if (!pgd_present(*pgd))
1121                 return 0;
1122
1123         pud = pud_offset(pgd, address);
1124         if (!pud_present(*pud))
1125                 return 0;
1126
1127         if (pud_large(*pud))
1128                 return spurious_fault_check(error_code, (pte_t *) pud);
1129
1130         pmd = pmd_offset(pud, address);
1131         if (!pmd_present(*pmd))
1132                 return 0;
1133
1134         if (pmd_large(*pmd))
1135                 return spurious_fault_check(error_code, (pte_t *) pmd);
1136
1137         pte = pte_offset_kernel(pmd, address);
1138         if (!pte_present(*pte))
1139                 return 0;
1140
1141         ret = spurious_fault_check(error_code, pte);
1142         if (!ret)
1143                 return 0;
1144
1145         /*
1146          * Make sure we have permissions in PMD.
1147          * If not, then there's a bug in the page tables:
1148          */
1149         ret = spurious_fault_check(error_code, (pte_t *) pmd);
1150         WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
1151
1152         return ret;
1153 }
1154 NOKPROBE_SYMBOL(spurious_fault);
1155
1156 int show_unhandled_signals = 1;
1157
1158 static inline int
1159 access_error(unsigned long error_code, struct vm_area_struct *vma)
1160 {
1161         /* This is only called for the current mm, so: */
1162         bool foreign = false;
1163
1164         /*
1165          * Read or write was blocked by protection keys.  This is
1166          * always an unconditional error and can never result in
1167          * a follow-up action to resolve the fault, like a COW.
1168          */
1169         if (error_code & PF_PK)
1170                 return 1;
1171
1172         /*
1173          * Make sure to check the VMA so that we do not perform
1174          * faults just to hit a PF_PK as soon as we fill in a
1175          * page.
1176          */
1177         if (!arch_vma_access_permitted(vma, (error_code & PF_WRITE),
1178                                 (error_code & PF_INSTR), foreign))
1179                 return 1;
1180
1181         if (error_code & PF_WRITE) {
1182                 /* write, present and write, not present: */
1183                 if (unlikely(!(vma->vm_flags & VM_WRITE)))
1184                         return 1;
1185                 return 0;
1186         }
1187
1188         /* read, present: */
1189         if (unlikely(error_code & PF_PROT))
1190                 return 1;
1191
1192         /* read, not present: */
1193         if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
1194                 return 1;
1195
1196         return 0;
1197 }
1198
1199 static int fault_in_kernel_space(unsigned long address)
1200 {
1201         return address >= TASK_SIZE_MAX;
1202 }
1203
1204 static inline bool smap_violation(int error_code, struct pt_regs *regs)
1205 {
1206         if (!IS_ENABLED(CONFIG_X86_SMAP))
1207                 return false;
1208
1209         if (!static_cpu_has(X86_FEATURE_SMAP))
1210                 return false;
1211
1212         if (error_code & PF_USER)
1213                 return false;
1214
1215         if (!user_mode(regs) && (regs->flags & X86_EFLAGS_AC))
1216                 return false;
1217
1218         return true;
1219 }
1220
1221 /*
1222  * This routine handles page faults.  It determines the address,
1223  * and the problem, and then passes it off to one of the appropriate
1224  * routines.
1225  *
1226  * This function must have noinline because both callers
1227  * {,trace_}do_page_fault() have notrace on. Having this an actual function
1228  * guarantees there's a function trace entry.
1229  */
1230 static noinline void
1231 __do_page_fault(struct pt_regs *regs, unsigned long error_code,
1232                 unsigned long address)
1233 {
1234         struct vm_area_struct *vma;
1235         struct task_struct *tsk;
1236         struct mm_struct *mm;
1237         int fault, major = 0;
1238         unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
1239         u32 pkey;
1240
1241         tsk = current;
1242         mm = tsk->mm;
1243
1244         /*
1245          * Detect and handle instructions that would cause a page fault for
1246          * both a tracked kernel page and a userspace page.
1247          */
1248         if (kmemcheck_active(regs))
1249                 kmemcheck_hide(regs);
1250         prefetchw(&mm->mmap_sem);
1251
1252         if (unlikely(kmmio_fault(regs, address)))
1253                 return;
1254
1255         /*
1256          * We fault-in kernel-space virtual memory on-demand. The
1257          * 'reference' page table is init_mm.pgd.
1258          *
1259          * NOTE! We MUST NOT take any locks for this case. We may
1260          * be in an interrupt or a critical region, and should
1261          * only copy the information from the master page table,
1262          * nothing more.
1263          *
1264          * This verifies that the fault happens in kernel space
1265          * (error_code & 4) == 0, and that the fault was not a
1266          * protection error (error_code & 9) == 0.
1267          */
1268         if (unlikely(fault_in_kernel_space(address))) {
1269                 if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
1270                         if (vmalloc_fault(address) >= 0)
1271                                 return;
1272
1273                         if (kmemcheck_fault(regs, address, error_code))
1274                                 return;
1275                 }
1276
1277                 /* Can handle a stale RO->RW TLB: */
1278                 if (spurious_fault(error_code, address))
1279                         return;
1280
1281                 /* kprobes don't want to hook the spurious faults: */
1282                 if (kprobes_fault(regs))
1283                         return;
1284                 /*
1285                  * Don't take the mm semaphore here. If we fixup a prefetch
1286                  * fault we could otherwise deadlock:
1287                  */
1288                 bad_area_nosemaphore(regs, error_code, address, NULL);
1289
1290                 return;
1291         }
1292
1293         /* kprobes don't want to hook the spurious faults: */
1294         if (unlikely(kprobes_fault(regs)))
1295                 return;
1296
1297         if (unlikely(error_code & PF_RSVD))
1298                 pgtable_bad(regs, error_code, address);
1299
1300         if (unlikely(smap_violation(error_code, regs))) {
1301                 bad_area_nosemaphore(regs, error_code, address, NULL);
1302                 return;
1303         }
1304
1305         /*
1306          * If we're in an interrupt, have no user context or are running
1307          * in a region with pagefaults disabled then we must not take the fault
1308          */
1309         if (unlikely(faulthandler_disabled() || !mm)) {
1310                 bad_area_nosemaphore(regs, error_code, address, NULL);
1311                 return;
1312         }
1313
1314         /*
1315          * It's safe to allow irq's after cr2 has been saved and the
1316          * vmalloc fault has been handled.
1317          *
1318          * User-mode registers count as a user access even for any
1319          * potential system fault or CPU buglet:
1320          */
1321         if (user_mode(regs)) {
1322                 local_irq_enable();
1323                 error_code |= PF_USER;
1324                 flags |= FAULT_FLAG_USER;
1325         } else {
1326                 if (regs->flags & X86_EFLAGS_IF)
1327                         local_irq_enable();
1328         }
1329
1330         perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
1331
1332         if (error_code & PF_WRITE)
1333                 flags |= FAULT_FLAG_WRITE;
1334         if (error_code & PF_INSTR)
1335                 flags |= FAULT_FLAG_INSTRUCTION;
1336
1337         /*
1338          * When running in the kernel we expect faults to occur only to
1339          * addresses in user space.  All other faults represent errors in
1340          * the kernel and should generate an OOPS.  Unfortunately, in the
1341          * case of an erroneous fault occurring in a code path which already
1342          * holds mmap_sem we will deadlock attempting to validate the fault
1343          * against the address space.  Luckily the kernel only validly
1344          * references user space from well defined areas of code, which are
1345          * listed in the exceptions table.
1346          *
1347          * As the vast majority of faults will be valid we will only perform
1348          * the source reference check when there is a possibility of a
1349          * deadlock. Attempt to lock the address space, if we cannot we then
1350          * validate the source. If this is invalid we can skip the address
1351          * space check, thus avoiding the deadlock:
1352          */
1353         if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1354                 if ((error_code & PF_USER) == 0 &&
1355                     !search_exception_tables(regs->ip)) {
1356                         bad_area_nosemaphore(regs, error_code, address, NULL);
1357                         return;
1358                 }
1359 retry:
1360                 down_read(&mm->mmap_sem);
1361         } else {
1362                 /*
1363                  * The above down_read_trylock() might have succeeded in
1364                  * which case we'll have missed the might_sleep() from
1365                  * down_read():
1366                  */
1367                 might_sleep();
1368         }
1369
1370         vma = find_vma(mm, address);
1371         if (unlikely(!vma)) {
1372                 bad_area(regs, error_code, address);
1373                 return;
1374         }
1375         if (likely(vma->vm_start <= address))
1376                 goto good_area;
1377         if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1378                 bad_area(regs, error_code, address);
1379                 return;
1380         }
1381         if (error_code & PF_USER) {
1382                 /*
1383                  * Accessing the stack below %sp is always a bug.
1384                  * The large cushion allows instructions like enter
1385                  * and pusha to work. ("enter $65535, $31" pushes
1386                  * 32 pointers and then decrements %sp by 65535.)
1387                  */
1388                 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1389                         bad_area(regs, error_code, address);
1390                         return;
1391                 }
1392         }
1393         if (unlikely(expand_stack(vma, address))) {
1394                 bad_area(regs, error_code, address);
1395                 return;
1396         }
1397
1398         /*
1399          * Ok, we have a good vm_area for this memory access, so
1400          * we can handle it..
1401          */
1402 good_area:
1403         if (unlikely(access_error(error_code, vma))) {
1404                 bad_area_access_error(regs, error_code, address, vma);
1405                 return;
1406         }
1407
1408         /*
1409          * If for any reason at all we couldn't handle the fault,
1410          * make sure we exit gracefully rather than endlessly redo
1411          * the fault.  Since we never set FAULT_FLAG_RETRY_NOWAIT, if
1412          * we get VM_FAULT_RETRY back, the mmap_sem has been unlocked.
1413          *
1414          * Note that handle_userfault() may also release and reacquire mmap_sem
1415          * (and not return with VM_FAULT_RETRY), when returning to userland to
1416          * repeat the page fault later with a VM_FAULT_NOPAGE retval
1417          * (potentially after handling any pending signal during the return to
1418          * userland). The return to userland is identified whenever
1419          * FAULT_FLAG_USER|FAULT_FLAG_KILLABLE are both set in flags.
1420          * Thus we have to be careful about not touching vma after handling the
1421          * fault, so we read the pkey beforehand.
1422          */
1423         pkey = vma_pkey(vma);
1424         fault = handle_mm_fault(vma, address, flags);
1425         major |= fault & VM_FAULT_MAJOR;
1426
1427         /*
1428          * If we need to retry the mmap_sem has already been released,
1429          * and if there is a fatal signal pending there is no guarantee
1430          * that we made any progress. Handle this case first.
1431          */
1432         if (unlikely(fault & VM_FAULT_RETRY)) {
1433                 /* Retry at most once */
1434                 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1435                         flags &= ~FAULT_FLAG_ALLOW_RETRY;
1436                         flags |= FAULT_FLAG_TRIED;
1437                         if (!fatal_signal_pending(tsk))
1438                                 goto retry;
1439                 }
1440
1441                 /* User mode? Just return to handle the fatal exception */
1442                 if (flags & FAULT_FLAG_USER)
1443                         return;
1444
1445                 /* Not returning to user mode? Handle exceptions or die: */
1446                 no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
1447                 return;
1448         }
1449
1450         up_read(&mm->mmap_sem);
1451         if (unlikely(fault & VM_FAULT_ERROR)) {
1452                 mm_fault_error(regs, error_code, address, &pkey, fault);
1453                 return;
1454         }
1455
1456         /*
1457          * Major/minor page fault accounting. If any of the events
1458          * returned VM_FAULT_MAJOR, we account it as a major fault.
1459          */
1460         if (major) {
1461                 tsk->maj_flt++;
1462                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
1463         } else {
1464                 tsk->min_flt++;
1465                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
1466         }
1467
1468         check_v8086_mode(regs, address, tsk);
1469 }
1470 NOKPROBE_SYMBOL(__do_page_fault);
1471
1472 dotraplinkage void notrace
1473 do_page_fault(struct pt_regs *regs, unsigned long error_code)
1474 {
1475         unsigned long address = read_cr2(); /* Get the faulting address */
1476         enum ctx_state prev_state;
1477
1478         /*
1479          * We must have this function tagged with __kprobes, notrace and call
1480          * read_cr2() before calling anything else. To avoid calling any kind
1481          * of tracing machinery before we've observed the CR2 value.
1482          *
1483          * exception_{enter,exit}() contain all sorts of tracepoints.
1484          */
1485
1486         prev_state = exception_enter();
1487         __do_page_fault(regs, error_code, address);
1488         exception_exit(prev_state);
1489 }
1490 NOKPROBE_SYMBOL(do_page_fault);
1491
1492 #ifdef CONFIG_TRACING
1493 static nokprobe_inline void
1494 trace_page_fault_entries(unsigned long address, struct pt_regs *regs,
1495                          unsigned long error_code)
1496 {
1497         if (user_mode(regs))
1498                 trace_page_fault_user(address, regs, error_code);
1499         else
1500                 trace_page_fault_kernel(address, regs, error_code);
1501 }
1502
1503 dotraplinkage void notrace
1504 trace_do_page_fault(struct pt_regs *regs, unsigned long error_code)
1505 {
1506         /*
1507          * The exception_enter and tracepoint processing could
1508          * trigger another page faults (user space callchain
1509          * reading) and destroy the original cr2 value, so read
1510          * the faulting address now.
1511          */
1512         unsigned long address = read_cr2();
1513         enum ctx_state prev_state;
1514
1515         prev_state = exception_enter();
1516         trace_page_fault_entries(address, regs, error_code);
1517         __do_page_fault(regs, error_code, address);
1518         exception_exit(prev_state);
1519 }
1520 NOKPROBE_SYMBOL(trace_do_page_fault);
1521 #endif /* CONFIG_TRACING */