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