hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/arch/arm/mm/fault.c
....@@ -1,12 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/arch/arm/mm/fault.c
34 *
45 * Copyright (C) 1995 Linus Torvalds
56 * Modifications for ARM processor (c) 1995-2004 Russell King
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License version 2 as
9
- * published by the Free Software Foundation.
107 */
118 #include <linux/extable.h>
129 #include <linux/signal.h>
....@@ -21,7 +18,6 @@
2118 #include <linux/highmem.h>
2219 #include <linux/perf_event.h>
2320
24
-#include <asm/pgtable.h>
2521 #include <asm/system_misc.h>
2622 #include <asm/system_info.h>
2723 #include <asm/tlbflush.h>
....@@ -30,58 +26,37 @@
3026
3127 #ifdef CONFIG_MMU
3228
33
-#ifdef CONFIG_KPROBES
34
-static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
35
-{
36
- int ret = 0;
37
-
38
- if (!user_mode(regs)) {
39
- /* kprobe_running() needs smp_processor_id() */
40
- preempt_disable();
41
- if (kprobe_running() && kprobe_fault_handler(regs, fsr))
42
- ret = 1;
43
- preempt_enable();
44
- }
45
-
46
- return ret;
47
-}
48
-#else
49
-static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
50
-{
51
- return 0;
52
-}
53
-#endif
54
-
5529 /*
5630 * This is useful to dump out the page tables associated with
5731 * 'addr' in mm 'mm'.
5832 */
59
-void show_pte(struct mm_struct *mm, unsigned long addr)
33
+void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr)
6034 {
6135 pgd_t *pgd;
6236
6337 if (!mm)
6438 mm = &init_mm;
6539
66
- pr_alert("pgd = %p\n", mm->pgd);
40
+ printk("%spgd = %p\n", lvl, mm->pgd);
6741 pgd = pgd_offset(mm, addr);
68
- pr_alert("[%08lx] *pgd=%08llx",
69
- addr, (long long)pgd_val(*pgd));
42
+ printk("%s[%08lx] *pgd=%08llx", lvl, addr, (long long)pgd_val(*pgd));
7043
7144 do {
45
+ p4d_t *p4d;
7246 pud_t *pud;
7347 pmd_t *pmd;
7448 pte_t *pte;
7549
76
- if (pgd_none(*pgd))
50
+ p4d = p4d_offset(pgd, addr);
51
+ if (p4d_none(*p4d))
7752 break;
7853
79
- if (pgd_bad(*pgd)) {
54
+ if (p4d_bad(*p4d)) {
8055 pr_cont("(bad)");
8156 break;
8257 }
8358
84
- pud = pud_offset(pgd, addr);
59
+ pud = pud_offset(p4d, addr);
8560 if (PTRS_PER_PUD != 1)
8661 pr_cont(", *pud=%08llx", (long long)pud_val(*pud));
8762
....@@ -121,7 +96,7 @@
12196 pr_cont("\n");
12297 }
12398 #else /* CONFIG_MMU */
124
-void show_pte(struct mm_struct *mm, unsigned long addr)
99
+void show_pte(const char *lvl, struct mm_struct *mm, unsigned long addr)
125100 { }
126101 #endif /* CONFIG_MMU */
127102
....@@ -142,11 +117,12 @@
142117 * No handler, we'll have to terminate things with extreme prejudice.
143118 */
144119 bust_spinlocks(1);
120
+ pr_alert("8<--- cut here ---\n");
145121 pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
146122 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
147123 "paging request", addr);
148124
149
- show_pte(mm, addr);
125
+ show_pte(KERN_ALERT, mm, addr);
150126 die("Oops", regs, fsr);
151127 bust_spinlocks(0);
152128 do_exit(SIGKILL);
....@@ -157,35 +133,35 @@
157133 * User mode accesses just cause a SIGSEGV
158134 */
159135 static void
160
-__do_user_fault(struct task_struct *tsk, unsigned long addr,
161
- unsigned int fsr, unsigned int sig, int code,
162
- struct pt_regs *regs)
136
+__do_user_fault(unsigned long addr, unsigned int fsr, unsigned int sig,
137
+ int code, struct pt_regs *regs)
163138 {
164
- struct siginfo si;
139
+ struct task_struct *tsk = current;
165140
166141 if (addr > TASK_SIZE)
167142 harden_branch_predictor();
168143
169
- clear_siginfo(&si);
170
-
171144 #ifdef CONFIG_DEBUG_USER
172145 if (((user_debug & UDBG_SEGV) && (sig == SIGSEGV)) ||
173146 ((user_debug & UDBG_BUS) && (sig == SIGBUS))) {
174
- printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
147
+ pr_err("8<--- cut here ---\n");
148
+ pr_err("%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
175149 tsk->comm, sig, addr, fsr);
176
- show_pte(tsk->mm, addr);
150
+ show_pte(KERN_ERR, tsk->mm, addr);
177151 show_regs(regs);
178152 }
153
+#endif
154
+#ifndef CONFIG_KUSER_HELPERS
155
+ if ((sig == SIGSEGV) && ((addr & PAGE_MASK) == 0xffff0000))
156
+ printk_ratelimited(KERN_DEBUG
157
+ "%s: CONFIG_KUSER_HELPERS disabled at 0x%08lx\n",
158
+ tsk->comm, addr);
179159 #endif
180160
181161 tsk->thread.address = addr;
182162 tsk->thread.error_code = fsr;
183163 tsk->thread.trap_no = 14;
184
- si.si_signo = sig;
185
- si.si_errno = 0;
186
- si.si_code = code;
187
- si.si_addr = (void __user *)addr;
188
- force_sig_info(sig, &si, tsk);
164
+ force_sig_fault(sig, code, (void __user *)addr);
189165 }
190166
191167 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
....@@ -198,7 +174,7 @@
198174 * have no context to handle this fault with.
199175 */
200176 if (user_mode(regs))
201
- __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
177
+ __do_user_fault(addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
202178 else
203179 __do_kernel_fault(mm, addr, fsr, regs);
204180 }
....@@ -214,7 +190,7 @@
214190 */
215191 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
216192 {
217
- unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
193
+ unsigned int mask = VM_ACCESS_FLAGS;
218194
219195 if ((fsr & FSR_WRITE) && !(fsr & FSR_CM))
220196 mask = VM_WRITE;
....@@ -226,7 +202,8 @@
226202
227203 static vm_fault_t __kprobes
228204 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
229
- unsigned int flags, struct task_struct *tsk)
205
+ unsigned int flags, struct task_struct *tsk,
206
+ struct pt_regs *regs)
230207 {
231208 struct vm_area_struct *vma;
232209 vm_fault_t fault;
....@@ -248,7 +225,7 @@
248225 goto out;
249226 }
250227
251
- return handle_mm_fault(vma, addr & PAGE_MASK, flags);
228
+ return handle_mm_fault(vma, addr & PAGE_MASK, flags, regs);
252229
253230 check_stack:
254231 /* Don't allow expansion below FIRST_USER_ADDRESS */
....@@ -266,9 +243,9 @@
266243 struct mm_struct *mm;
267244 int sig, code;
268245 vm_fault_t fault;
269
- unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
246
+ unsigned int flags = FAULT_FLAG_DEFAULT;
270247
271
- if (notify_page_fault(regs, fsr))
248
+ if (kprobe_page_fault(regs, fsr))
272249 return 0;
273250
274251 tsk = current;
....@@ -290,16 +267,18 @@
290267 if ((fsr & FSR_WRITE) && !(fsr & FSR_CM))
291268 flags |= FAULT_FLAG_WRITE;
292269
270
+ perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
271
+
293272 /*
294273 * As per x86, we may deadlock here. However, since the kernel only
295274 * validly references user space from well defined areas of the code,
296275 * we can bug out early if this is from code which shouldn't.
297276 */
298
- if (!down_read_trylock(&mm->mmap_sem)) {
277
+ if (!mmap_read_trylock(mm)) {
299278 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
300279 goto no_context;
301280 retry:
302
- down_read(&mm->mmap_sem);
281
+ mmap_read_lock(mm);
303282 } else {
304283 /*
305284 * The above down_read_trylock() might have succeeded in
....@@ -314,45 +293,26 @@
314293 #endif
315294 }
316295
317
- fault = __do_page_fault(mm, addr, fsr, flags, tsk);
296
+ fault = __do_page_fault(mm, addr, fsr, flags, tsk, regs);
318297
319298 /* If we need to retry but a fatal signal is pending, handle the
320
- * signal first. We do not need to release the mmap_sem because
299
+ * signal first. We do not need to release the mmap_lock because
321300 * it would already be released in __lock_page_or_retry in
322301 * mm/filemap.c. */
323
- if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
302
+ if (fault_signal_pending(fault, regs)) {
324303 if (!user_mode(regs))
325304 goto no_context;
326305 return 0;
327306 }
328307
329
- /*
330
- * Major/minor page fault accounting is only done on the
331
- * initial attempt. If we go through a retry, it is extremely
332
- * likely that the page will be found in page cache at that point.
333
- */
334
-
335
- perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
336308 if (!(fault & VM_FAULT_ERROR) && flags & FAULT_FLAG_ALLOW_RETRY) {
337
- if (fault & VM_FAULT_MAJOR) {
338
- tsk->maj_flt++;
339
- perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
340
- regs, addr);
341
- } else {
342
- tsk->min_flt++;
343
- perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
344
- regs, addr);
345
- }
346309 if (fault & VM_FAULT_RETRY) {
347
- /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
348
- * of starvation. */
349
- flags &= ~FAULT_FLAG_ALLOW_RETRY;
350310 flags |= FAULT_FLAG_TRIED;
351311 goto retry;
352312 }
353313 }
354314
355
- up_read(&mm->mmap_sem);
315
+ mmap_read_unlock(mm);
356316
357317 /*
358318 * Handle the "normal" case first - VM_FAULT_MAJOR
....@@ -394,7 +354,7 @@
394354 SEGV_ACCERR : SEGV_MAPERR;
395355 }
396356
397
- __do_user_fault(tsk, addr, fsr, sig, code, regs);
357
+ __do_user_fault(addr, fsr, sig, code, regs);
398358 return 0;
399359
400360 no_context:
....@@ -433,14 +393,12 @@
433393 {
434394 unsigned int index;
435395 pgd_t *pgd, *pgd_k;
396
+ p4d_t *p4d, *p4d_k;
436397 pud_t *pud, *pud_k;
437398 pmd_t *pmd, *pmd_k;
438399
439400 if (addr < TASK_SIZE)
440401 return do_page_fault(addr, fsr, regs);
441
-
442
- if (interrupts_enabled(regs))
443
- local_irq_enable();
444402
445403 if (user_mode(regs))
446404 goto bad_area;
....@@ -450,13 +408,16 @@
450408 pgd = cpu_get_pgd() + index;
451409 pgd_k = init_mm.pgd + index;
452410
453
- if (pgd_none(*pgd_k))
454
- goto bad_area;
455
- if (!pgd_present(*pgd))
456
- set_pgd(pgd, *pgd_k);
411
+ p4d = p4d_offset(pgd, addr);
412
+ p4d_k = p4d_offset(pgd_k, addr);
457413
458
- pud = pud_offset(pgd, addr);
459
- pud_k = pud_offset(pgd_k, addr);
414
+ if (p4d_none(*p4d_k))
415
+ goto bad_area;
416
+ if (!p4d_present(*p4d))
417
+ set_p4d(p4d, *p4d_k);
418
+
419
+ pud = pud_offset(p4d, addr);
420
+ pud_k = pud_offset(p4d_k, addr);
460421
461422 if (pud_none(*pud_k))
462423 goto bad_area;
....@@ -509,9 +470,6 @@
509470 static int
510471 do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
511472 {
512
- if (interrupts_enabled(regs))
513
- local_irq_enable();
514
-
515473 do_bad_area(addr, fsr, regs);
516474 return 0;
517475 }
....@@ -560,21 +518,17 @@
560518 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
561519 {
562520 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
563
- struct siginfo info;
564521
565522 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
566523 return;
567524
525
+ pr_alert("8<--- cut here ---\n");
568526 pr_alert("Unhandled fault: %s (0x%03x) at 0x%08lx\n",
569527 inf->name, fsr, addr);
570
- show_pte(current->mm, addr);
528
+ show_pte(KERN_ALERT, current->mm, addr);
571529
572
- clear_siginfo(&info);
573
- info.si_signo = inf->sig;
574
- info.si_errno = 0;
575
- info.si_code = inf->code;
576
- info.si_addr = (void __user *)addr;
577
- arm_notify_die("", regs, &info, fsr, 0);
530
+ arm_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
531
+ fsr, 0);
578532 }
579533
580534 void __init
....@@ -594,7 +548,6 @@
594548 do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
595549 {
596550 const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
597
- struct siginfo info;
598551
599552 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
600553 return;
....@@ -602,12 +555,8 @@
602555 pr_alert("Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
603556 inf->name, ifsr, addr);
604557
605
- clear_siginfo(&info);
606
- info.si_signo = inf->sig;
607
- info.si_errno = 0;
608
- info.si_code = inf->code;
609
- info.si_addr = (void __user *)addr;
610
- arm_notify_die("", regs, &info, ifsr, 0);
558
+ arm_notify_die("", regs, inf->sig, inf->code, (void __user *)addr,
559
+ ifsr, 0);
611560 }
612561
613562 /*