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