Skip to content

Commit b98cca4

Browse files
Anshuman Khandualtorvalds
authored andcommitted
mm, kprobes: generalize and rename notify_page_fault() as kprobe_page_fault()
Architectures which support kprobes have very similar boilerplate around calling kprobe_fault_handler(). Use a helper function in kprobes.h to unify them, based on the x86 code. This changes the behaviour for other architectures when preemption is enabled. Previously, they would have disabled preemption while calling the kprobe handler. However, preemption would be disabled if this fault was due to a kprobe, so we know the fault was not due to a kprobe handler and can simply return failure. This behaviour was introduced in commit a980c0e ("x86/kprobes: Refactor kprobes_fault() like kprobe_exceptions_notify()") [anshuman.khandual@arm.com: export kprobe_fault_handler()] Link: http://lkml.kernel.org/r/1561133358-8876-1-git-send-email-anshuman.khandual@arm.com Link: http://lkml.kernel.org/r/1560420444-25737-1-git-send-email-anshuman.khandual@arm.com Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com> Reviewed-by: Dave Hansen <dave.hansen@linux.intel.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Christophe Leroy <christophe.leroy@c-s.fr> Cc: Stephen Rothwell <sfr@canb.auug.org.au> Cc: Andrey Konovalov <andreyknvl@google.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Paul Mackerras <paulus@samba.org> Cc: Russell King <linux@armlinux.org.uk> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will.deacon@arm.com> Cc: Tony Luck <tony.luck@intel.com> Cc: Fenghua Yu <fenghua.yu@intel.com> Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Heiko Carstens <heiko.carstens@de.ibm.com> Cc: Yoshinori Sato <ysato@users.sourceforge.jp> Cc: "David S. Miller" <davem@davemloft.net> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Andy Lutomirski <luto@kernel.org> Cc: Vineet Gupta <vgupta@synopsys.com> Cc: James Hogan <jhogan@kernel.org> Cc: Paul Burton <paul.burton@mips.com> Cc: Ralf Baechle <ralf@linux-mips.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 92bae78 commit b98cca4

11 files changed

Lines changed: 32 additions & 156 deletions

File tree

‎arch/arm/mm/fault.c‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,6 @@
2727

2828
#ifdef CONFIG_MMU
2929

30-
#ifdef CONFIG_KPROBES
31-
static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
32-
{
33-
int ret = 0;
34-
35-
if (!user_mode(regs)) {
36-
/* kprobe_running() needs smp_processor_id() */
37-
preempt_disable();
38-
if (kprobe_running() && kprobe_fault_handler(regs, fsr))
39-
ret = 1;
40-
preempt_enable();
41-
}
42-
43-
return ret;
44-
}
45-
#else
46-
static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
47-
{
48-
return 0;
49-
}
50-
#endif
51-
5230
/*
5331
* This is useful to dump out the page tables associated with
5432
* 'addr' in mm 'mm'.
@@ -265,7 +243,7 @@ do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
265243
vm_fault_t fault;
266244
unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
267245

268-
if (notify_page_fault(regs, fsr))
246+
if (kprobe_page_fault(regs, fsr))
269247
return 0;
270248

271249
tsk = current;

‎arch/arm64/mm/fault.c‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,6 @@ static inline const struct fault_info *esr_to_debug_fault_info(unsigned int esr)
5959
return debug_fault_info + DBG_ESR_EVT(esr);
6060
}
6161

62-
#ifdef CONFIG_KPROBES
63-
static inline int notify_page_fault(struct pt_regs *regs, unsigned int esr)
64-
{
65-
int ret = 0;
66-
67-
/* kprobe_running() needs smp_processor_id() */
68-
if (!user_mode(regs)) {
69-
preempt_disable();
70-
if (kprobe_running() && kprobe_fault_handler(regs, esr))
71-
ret = 1;
72-
preempt_enable();
73-
}
74-
75-
return ret;
76-
}
77-
#else
78-
static inline int notify_page_fault(struct pt_regs *regs, unsigned int esr)
79-
{
80-
return 0;
81-
}
82-
#endif
83-
8462
static void data_abort_decode(unsigned int esr)
8563
{
8664
pr_alert("Data abort info:\n");
@@ -434,7 +412,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
434412
unsigned long vm_flags = VM_READ | VM_WRITE;
435413
unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
436414

437-
if (notify_page_fault(regs, esr))
415+
if (kprobe_page_fault(regs, esr))
438416
return 0;
439417

440418
/*

‎arch/ia64/mm/fault.c‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,6 @@
2121

2222
extern int die(char *, struct pt_regs *, long);
2323

24-
#ifdef CONFIG_KPROBES
25-
static inline int notify_page_fault(struct pt_regs *regs, int trap)
26-
{
27-
int ret = 0;
28-
29-
if (!user_mode(regs)) {
30-
/* kprobe_running() needs smp_processor_id() */
31-
preempt_disable();
32-
if (kprobe_running() && kprobe_fault_handler(regs, trap))
33-
ret = 1;
34-
preempt_enable();
35-
}
36-
37-
return ret;
38-
}
39-
#else
40-
static inline int notify_page_fault(struct pt_regs *regs, int trap)
41-
{
42-
return 0;
43-
}
44-
#endif
45-
4624
/*
4725
* Return TRUE if ADDRESS points at a page in the kernel's mapped segment
4826
* (inside region 5, on ia64) and that page is present.
@@ -116,7 +94,7 @@ ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *re
11694
/*
11795
* This is to handle the kprobes on user space access instructions
11896
*/
119-
if (notify_page_fault(regs, TRAP_BRKPT))
97+
if (kprobe_page_fault(regs, TRAP_BRKPT))
12098
return;
12199

122100
if (user_mode(regs))

‎arch/mips/include/asm/kprobes.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ do { \
4141
#define kretprobe_blacklist_size 0
4242

4343
void arch_remove_kprobe(struct kprobe *p);
44+
int kprobe_fault_handler(struct pt_regs *regs, int trapnr);
4445

4546
/* Architecture specific copy of original instruction*/
4647
struct arch_specific_insn {

‎arch/mips/kernel/kprobes.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ static inline int post_kprobe_handler(struct pt_regs *regs)
398398
return 1;
399399
}
400400

401-
static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
401+
int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
402402
{
403403
struct kprobe *cur = kprobe_running();
404404
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();

‎arch/powerpc/mm/fault.c‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,6 @@
4242
#include <asm/debug.h>
4343
#include <asm/kup.h>
4444

45-
static inline bool notify_page_fault(struct pt_regs *regs)
46-
{
47-
bool ret = false;
48-
49-
#ifdef CONFIG_KPROBES
50-
/* kprobe_running() needs smp_processor_id() */
51-
if (!user_mode(regs)) {
52-
preempt_disable();
53-
if (kprobe_running() && kprobe_fault_handler(regs, 11))
54-
ret = true;
55-
preempt_enable();
56-
}
57-
#endif /* CONFIG_KPROBES */
58-
59-
if (unlikely(debugger_fault_handler(regs)))
60-
ret = true;
61-
62-
return ret;
63-
}
64-
6545
/*
6646
* Check whether the instruction inst is a store using
6747
* an update addressing form which will update r1.
@@ -461,8 +441,9 @@ static int __do_page_fault(struct pt_regs *regs, unsigned long address,
461441
int is_write = page_fault_is_write(error_code);
462442
vm_fault_t fault, major = 0;
463443
bool must_retry = false;
444+
bool kprobe_fault = kprobe_page_fault(regs, 11);
464445

465-
if (notify_page_fault(regs))
446+
if (unlikely(debugger_fault_handler(regs) || kprobe_fault))
466447
return 0;
467448

468449
if (unlikely(page_fault_is_bad(error_code))) {

‎arch/s390/mm/fault.c‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,6 @@ static int __init fault_init(void)
6767
}
6868
early_initcall(fault_init);
6969

70-
static inline int notify_page_fault(struct pt_regs *regs)
71-
{
72-
int ret = 0;
73-
74-
/* kprobe_running() needs smp_processor_id() */
75-
if (kprobes_built_in() && !user_mode(regs)) {
76-
preempt_disable();
77-
if (kprobe_running() && kprobe_fault_handler(regs, 14))
78-
ret = 1;
79-
preempt_enable();
80-
}
81-
return ret;
82-
}
83-
8470
/*
8571
* Find out which address space caused the exception.
8672
*/
@@ -412,7 +398,7 @@ static inline vm_fault_t do_exception(struct pt_regs *regs, int access)
412398
*/
413399
clear_pt_regs_flag(regs, PIF_PER_TRAP);
414400

415-
if (notify_page_fault(regs))
401+
if (kprobe_page_fault(regs, 14))
416402
return 0;
417403

418404
mm = tsk->mm;

‎arch/sh/mm/fault.c‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,6 @@
2424
#include <asm/tlbflush.h>
2525
#include <asm/traps.h>
2626

27-
static inline int notify_page_fault(struct pt_regs *regs, int trap)
28-
{
29-
int ret = 0;
30-
31-
if (kprobes_built_in() && !user_mode(regs)) {
32-
preempt_disable();
33-
if (kprobe_running() && kprobe_fault_handler(regs, trap))
34-
ret = 1;
35-
preempt_enable();
36-
}
37-
38-
return ret;
39-
}
40-
4127
static void
4228
force_sig_info_fault(int si_signo, int si_code, unsigned long address)
4329
{
@@ -412,14 +398,14 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
412398
if (unlikely(fault_in_kernel_space(address))) {
413399
if (vmalloc_fault(address) >= 0)
414400
return;
415-
if (notify_page_fault(regs, vec))
401+
if (kprobe_page_fault(regs, vec))
416402
return;
417403

418404
bad_area_nosemaphore(regs, error_code, address);
419405
return;
420406
}
421407

422-
if (unlikely(notify_page_fault(regs, vec)))
408+
if (unlikely(kprobe_page_fault(regs, vec)))
423409
return;
424410

425411
/* Only enable interrupts if they were on before the fault */

‎arch/sparc/mm/fault_64.c‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,6 @@
3838

3939
int show_unhandled_signals = 1;
4040

41-
static inline __kprobes int notify_page_fault(struct pt_regs *regs)
42-
{
43-
int ret = 0;
44-
45-
/* kprobe_running() needs smp_processor_id() */
46-
if (kprobes_built_in() && !user_mode(regs)) {
47-
preempt_disable();
48-
if (kprobe_running() && kprobe_fault_handler(regs, 0))
49-
ret = 1;
50-
preempt_enable();
51-
}
52-
return ret;
53-
}
54-
5541
static void __kprobes unhandled_fault(unsigned long address,
5642
struct task_struct *tsk,
5743
struct pt_regs *regs)
@@ -285,7 +271,7 @@ asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
285271

286272
fault_code = get_thread_fault_code();
287273

288-
if (notify_page_fault(regs))
274+
if (kprobe_page_fault(regs, 0))
289275
goto exit_exception;
290276

291277
si_code = SEGV_MAPERR;

‎arch/x86/mm/fault.c‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,6 @@ kmmio_fault(struct pt_regs *regs, unsigned long addr)
4646
return 0;
4747
}
4848

49-
static nokprobe_inline int kprobes_fault(struct pt_regs *regs)
50-
{
51-
if (!kprobes_built_in())
52-
return 0;
53-
if (user_mode(regs))
54-
return 0;
55-
/*
56-
* To be potentially processing a kprobe fault and to be allowed to call
57-
* kprobe_running(), we have to be non-preemptible.
58-
*/
59-
if (preemptible())
60-
return 0;
61-
if (!kprobe_running())
62-
return 0;
63-
return kprobe_fault_handler(regs, X86_TRAP_PF);
64-
}
65-
6649
/*
6750
* Prefetch quirks:
6851
*
@@ -1282,7 +1265,7 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code,
12821265
return;
12831266

12841267
/* kprobes don't want to hook the spurious faults: */
1285-
if (kprobes_fault(regs))
1268+
if (kprobe_page_fault(regs, X86_TRAP_PF))
12861269
return;
12871270

12881271
/*
@@ -1313,7 +1296,7 @@ void do_user_addr_fault(struct pt_regs *regs,
13131296
mm = tsk->mm;
13141297

13151298
/* kprobes don't want to hook the spurious faults: */
1316-
if (unlikely(kprobes_fault(regs)))
1299+
if (unlikely(kprobe_page_fault(regs, X86_TRAP_PF)))
13171300
return;
13181301

13191302
/*

0 commit comments

Comments
 (0)