kprobes: Remove kprobe::fault_handler
Subsystems (15)
Peter Zijlstra
maintainercover kprobes: Remove kprobe::fault_handler
The reason for kprobe::fault_handler(), as given by their comment:
* We come here because instructions in the pre/post
* handler caused the page_fault, this could happen
* if handler tries to access user space by
* copy_from_user(), get_user() etc. Let the
* user-specified handler try to fix it first.
Is just plain bad. Those other handlers are ran from non-preemptible context and had better use _nofault() functions. Also, there is no upstream usage of this.
The corollary of this change is that no tracing/probing/whatever can consume faults.
Peter Zijlstra
maintainerPATCH 1/2 kprobes: Remove kprobe::fault_handler
The reason for kprobe::fault_handler(), as given by their comment:
* We come here because instructions in the pre/post
* handler caused the page_fault, this could happen
* if handler tries to access user space by
* copy_from_user(), get_user() etc. Let the
* user-specified handler try to fix it first.
Is just plain bad. Those other handlers are ran from non-preemptible context and had better use _nofault() functions. Also, there is no upstream usage of this.
--- Documentation/trace/kprobes.rst | 24 +++++------------------- arch/arc/kernel/kprobes.c | 10 ---------- arch/arm/probes/kprobes/core.c | 9 --------- arch/arm64/kernel/probes/kprobes.c | 10 ---------- arch/csky/kernel/probes/kprobes.c | 10 ---------- arch/ia64/kernel/kprobes.c | 9 --------- arch/mips/kernel/kprobes.c | 3 --- arch/powerpc/kernel/kprobes.c | 10 ---------- arch/riscv/kernel/probes/kprobes.c | 10 ---------- arch/s390/kernel/kprobes.c | 10 ---------- arch/sh/kernel/kprobes.c | 10 ---------- arch/sparc/kernel/kprobes.c | 10 ---------- arch/x86/kernel/kprobes/core.c | 10 ---------- include/linux/kprobes.h | 8 -------- kernel/kprobes.c | 19 ------------------- samples/kprobes/kprobe_example.c | 15 --------------- 16 files changed, 5 insertions(+), 172 deletions(-)
Documentation/trace/kprobes.rst +5 -19
@@ -362,14 +362,11 @@ register_kprobe #include <linux/kprobes.h> int register_kprobe(struct kprobe *kp); -Sets a breakpoint at the address kp->addr. When the breakpoint is -hit, Kprobes calls kp->pre_handler. After the probed instruction -is single-stepped, Kprobe calls kp->post_handler. If a fault -occurs during execution of kp->pre_handler or kp->post_handler, -or during single-stepping of the probed instruction, Kprobes calls -kp->fault_handler. Any or all handlers can be NULL. If kp->flags -is set KPROBE_FLAG_DISABLED, that kp will be registered but disabled, -so, its handlers aren't hit until calling enable_kprobe(kp). +Sets a breakpoint at the address kp->addr. When the breakpoint is hit, Kprobes +calls kp->pre_handler. After the probed instruction is single-stepped, Kprobe +calls kp->post_handler. Any or all handlers can be NULL. If kp->flags is set +KPROBE_FLAG_DISABLED, that kp will be registered but disabled, so, its handlers +aren't hit until calling enable_kprobe(kp). .. note::
@@ -415,17 +412,6 @@ the breakpoint was hit. Return 0 here u p and regs are as described for the pre_handler. flags always seems to be zero. -User's fault-handler (kp->fault_handler):: - - #include <linux/kprobes.h> - #include <linux/ptrace.h> - int fault_handler(struct kprobe *p, struct pt_regs *regs, int trapnr); - -p and regs are as described for the pre_handler. trapnr is the -architecture-specific trap number associated with the fault (e.g., -on i386, 13 for a general protection fault or 14 for a page fault). -Returns 1 if it successfully handled the exception. - register_kretprobe ------------------
arch/arc/kernel/kprobes.c +0 -10
@@ -324,16 +324,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned zero, * try to fix up. */
arch/arm/probes/kprobes/core.c +0 -9
@@ -358,15 +358,6 @@ int __kprobes kprobe_fault_handler(struc*/ kprobes_inc_nmissed_count (cur); -/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, fsr)) -return 1 ;break ;default :
arch/arm64/kernel/probes/kprobes.c +0 -10
@@ -283,16 +283,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, fsr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/csky/kernel/probes/kprobes.c +0 -10
@@ -302,16 +302,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/ia64/kernel/kprobes.c +0 -9
@@ -851,15 +851,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/mips/kernel/kprobes.c +0 -3
@@ -403,9 +403,6 @@ int kprobe_fault_handler(struct pt_regsstruct kprobe *cur =kprobe_running ();struct kprobe_ctlblk *kcb =get_kprobe_ctlblk (); -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; -if (kcb->kprobe_status &KPROBE_HIT_SS ) {resume_execution (cur, regs, kcb); regs->cp0_status |= kcb->kprobe_old_SR;
arch/powerpc/kernel/kprobes.c +0 -10
@@ -509,16 +509,6 @@ int kprobe_fault_handler(struct pt_regskprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/riscv/kernel/probes/kprobes.c +0 -10
@@ -275,16 +275,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/s390/kernel/kprobes.c +0 -10
@@ -453,16 +453,6 @@ static int kprobe_trap_handler(struct ptkprobes_inc_nmissed_count (p);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (p->fault_handler && p->fault_handler (p, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/sh/kernel/kprobes.c +0 -10
@@ -390,16 +390,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/sparc/kernel/kprobes.c +0 -10
@@ -353,16 +353,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/x86/kernel/kprobes/core.c +0 -10
@@ -947,16 +947,6 @@ int kprobe_fault_handler(struct pt_regs* these specific fault cases. */ kprobes_inc_nmissed_count (cur); - -/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; }return 0 ;
include/linux/kprobes.h +0 -8
@@ -54,8 +54,6 @@ struct kretprobe_instance;typedef int (*kprobe_pre_handler_t ) (struct kprobe *,struct pt_regs *);typedef void (*kprobe_post_handler_t ) (struct kprobe *,struct pt_regs *,unsigned long flags); -typedef int (*kprobe_fault_handler_t ) (struct kprobe *,struct pt_regs *, -int trapnr);typedef int (*kretprobe_handler_t ) (struct kretprobe_instance *,struct pt_regs *);
@@ -83,12 +81,6 @@ struct kprobe {/* Called after addr is executed, unless... */ kprobe_post_handler_t post_handler; -/* -* ... called if executing addr causes a fault (eg. page fault). -* Return 1 if it handled fault, otherwise kernel will see it. -*/ -kprobe_fault_handler_t fault_handler; -/* Saved opcode (which has been replaced with breakpoint) */ kprobe_opcode_t opcode;
kernel/kprobes.c +0 -19
@@ -1183,23 +1183,6 @@ static void aggr_post_handler(struct kpr }NOKPROBE_SYMBOL (aggr_post_handler); -static int aggr_fault_handler (struct kprobe *p,struct pt_regs *regs, -int trapnr) -{ -struct kprobe *cur =__this_cpu_read (kprobe_instance); - -/* -* if we faulted "during" the execution of a user specified -* probe handler, invoke just that probe's fault handler -*/ -if (cur && cur->fault_handler) { -if (cur->fault_handler (cur, regs, trapnr)) -return 1 ; - } -return 0 ; -} -NOKPROBE_SYMBOL (aggr_fault_handler); -/* Walks the list and increments nmissed count for multiprobe case */ void kprobes_inc_nmissed_count (struct kprobe *p) {
@@ -1330,7 +1313,6 @@ static void init_aggr_kprobe(struct kpro ap->addr = p->addr; ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED ; ap->pre_handler = aggr_pre_handler; - ap->fault_handler = aggr_fault_handler;/* We don't care the kprobe which has gone. */ if (p->post_handler && !kprobe_gone (p)) ap->post_handler = aggr_post_handler;
@@ -2014,7 +1996,6 @@ int register_kretprobe(struct kretprobe rp->kp.pre_handler = pre_handler_kretprobe; rp->kp.post_handler =NULL ; - rp->kp.fault_handler =NULL ;/* Pre-allocate memory for max kretprobe instances */ if (rp->maxactive <=0 ) {
samples/kprobes/kprobe_example.c +0 -15
@@ -86,26 +86,11 @@ static void __kprobes handler_post(struc#endif } -/* -* fault_handler: this is called if an exception is generated for any -* instruction within the pre- or post-handler, or when Kprobes -* single-steps the probed instruction. -*/ -static int handler_fault (struct kprobe *p,struct pt_regs *regs,int trapnr) -{ -pr_info ("fault_handler: p->addr = 0x%p, trap #%dn" , p->addr, trapnr); -/* Return 0 because we don't handle the fault. */ -return 0 ; -} -/* NOKPROBE_SYMBOL() is also available */ -NOKPROBE_SYMBOL (handler_fault); -static int __init kprobe_init (void ) {int ret; kp.pre_handler = handler_pre; kp.post_handler = handler_post; - kp.fault_handler = handler_fault; ret =register_kprobe (&kp);if (ret <0 ) {
Quoted 11 lines · On Tue, 25 May 2021 09:25:19 +0200 Peter Zijlstra <[email protected]> wrote:
The reason for
kprobe::fault_handler(), as given by their comment:* We come here because instructions in the pre/post
* handler caused thepage_fault, this could happen
* if handler tries to access user space by
*copy_from_user(),get_user()etc. Let the
* user-specified handler try to fix it first.Is just plain bad. Those other handlers are ran from non-preemptible context and had better use
_nofault()functions. Also, there is no upstream usage of this.
OK, this looks good to me.
Thanks,
Quoted 378 lines
Signed-off-by Peter Zijlstra (Intel) <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>--- Documentation/trace/kprobes.rst | 24 +++++------------------- arch/arc/kernel/kprobes.c | 10 ---------- arch/arm/probes/kprobes/core.c | 9 --------- arch/arm64/kernel/probes/kprobes.c | 10 ---------- arch/csky/kernel/probes/kprobes.c | 10 ---------- arch/ia64/kernel/kprobes.c | 9 --------- arch/mips/kernel/kprobes.c | 3 --- arch/powerpc/kernel/kprobes.c | 10 ---------- arch/riscv/kernel/probes/kprobes.c | 10 ---------- arch/s390/kernel/kprobes.c | 10 ---------- arch/sh/kernel/kprobes.c | 10 ---------- arch/sparc/kernel/kprobes.c | 10 ---------- arch/x86/kernel/kprobes/core.c | 10 ---------- include/linux/kprobes.h | 8 -------- kernel/kprobes.c | 19 ------------------- samples/kprobes/kprobe_example.c | 15 --------------- 16 files changed, 5 insertions(+), 172 deletions(-)Documentation/trace/kprobes.rst +5
-19@@ -362,14 +362,11 @@ register_kprobe #include <linux/kprobes.h> int register_kprobe(struct kprobe *kp); -Sets a breakpoint at the address kp->addr. When the breakpoint is -hit, Kprobes calls kp->pre_handler. After the probed instruction -is single-stepped, Kprobe calls kp->post_handler. If a fault -occurs during execution of kp->pre_handler or kp->post_handler, -or during single-stepping of the probed instruction, Kprobes calls -kp->fault_handler. Any or all handlers can be NULL. If kp->flags -is set KPROBE_FLAG_DISABLED, that kp will be registered but disabled, -so, its handlers aren't hit until calling enable_kprobe(kp). +Sets a breakpoint at the address kp->addr. When the breakpoint is hit, Kprobes +calls kp->pre_handler. After the probed instruction is single-stepped, Kprobe +calls kp->post_handler. Any or all handlers can be NULL. If kp->flags is set +KPROBE_FLAG_DISABLED, that kp will be registered but disabled, so, its handlers +aren't hit until calling enable_kprobe(kp). .. note::@@ -415,17 +412,6 @@ the breakpoint was hit. Return 0 here u p and regs are as described for the pre_handler. flags always seems to be zero. -User's fault-handler (kp->fault_handler):: - - #include <linux/kprobes.h> - #include <linux/ptrace.h> - int fault_handler(struct kprobe *p, struct pt_regs *regs, int trapnr); - -p and regs are as described for the pre_handler. trapnr is the -architecture-specific trap number associated with the fault (e.g., -on i386, 13 for a general protection fault or 14 for a page fault). -Returns 1 if it successfully handled the exception. - register_kretprobe ------------------arch/arc/kernel/kprobes.c +0
-10@@ -324,16 +324,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned zero, * try to fix up. */ arch/arm/probes/kprobes/core.c +0
-9@@ -358,15 +358,6 @@ int __kprobes kprobe_fault_handler(struc*/ kprobes_inc_nmissed_count (cur); -/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, fsr)) -return 1 ;break ;default :arch/arm64/kernel/probes/kprobes.c +0
-10@@ -283,16 +283,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, fsr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */ arch/csky/kernel/probes/kprobes.c +0
-10@@ -302,16 +302,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */ arch/ia64/kernel/kprobes.c +0
-9@@ -851,15 +851,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; -/* * In case the user-specified fault handler returned * zero, try to fix up. */ arch/mips/kernel/kprobes.c +0
-3@@ -403,9 +403,6 @@ int kprobe_fault_handler(struct pt_regsstruct kprobe *cur =kprobe_running ();struct kprobe_ctlblk *kcb =get_kprobe_ctlblk (); -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; -if (kcb->kprobe_status &KPROBE_HIT_SS ) {resume_execution (cur, regs, kcb); regs->cp0_status |= kcb->kprobe_old_SR;arch/powerpc/kernel/kprobes.c +0
-10@@ -509,16 +509,6 @@ int kprobe_fault_handler(struct pt_regskprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */ arch/riscv/kernel/probes/kprobes.c +0
-10@@ -275,16 +275,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */ arch/s390/kernel/kprobes.c +0
-10@@ -453,16 +453,6 @@ static int kprobe_trap_handler(struct ptkprobes_inc_nmissed_count (p);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (p->fault_handler && p->fault_handler (p, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */ arch/sh/kernel/kprobes.c +0
-10@@ -390,16 +390,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */ arch/sparc/kernel/kprobes.c +0
-10@@ -353,16 +353,6 @@ int __kprobes kprobe_fault_handler(struckprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */ arch/x86/kernel/kprobes/core.c +0
-10@@ -947,16 +947,6 @@ int kprobe_fault_handler(struct pt_regs* these specific fault cases. */ kprobes_inc_nmissed_count (cur); - -/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; }return 0 ;include/linux/kprobes.h +0
-8@@ -54,8 +54,6 @@ struct kretprobe_instance;typedef int (*kprobe_pre_handler_t ) (struct kprobe *,struct pt_regs *);typedef void (*kprobe_post_handler_t ) (struct kprobe *,struct pt_regs *,unsigned long flags); -typedef int (*kprobe_fault_handler_t ) (struct kprobe *,struct pt_regs *, -int trapnr);typedef int (*kretprobe_handler_t ) (struct kretprobe_instance *,struct pt_regs *);@@ -83,12 +81,6 @@ struct kprobe {/* Called after addr is executed, unless... */ kprobe_post_handler_t post_handler; -/* -* ... called if executing addr causes a fault (eg. page fault). -* Return 1 if it handled fault, otherwise kernel will see it. -*/ -kprobe_fault_handler_t fault_handler; -/* Saved opcode (which has been replaced with breakpoint) */ kprobe_opcode_t opcode;kernel/kprobes.c +0
-19@@ -1183,23 +1183,6 @@ static void aggr_post_handler(struct kpr }NOKPROBE_SYMBOL (aggr_post_handler); -static int aggr_fault_handler (struct kprobe *p,struct pt_regs *regs, -int trapnr) -{ -struct kprobe *cur =__this_cpu_read (kprobe_instance); - -/* -* if we faulted "during" the execution of a user specified -* probe handler, invoke just that probe's fault handler -*/ -if (cur && cur->fault_handler) { -if (cur->fault_handler (cur, regs, trapnr)) -return 1 ; - } -return 0 ; -} -NOKPROBE_SYMBOL (aggr_fault_handler); -/* Walks the list and increments nmissed count for multiprobe case */ void kprobes_inc_nmissed_count (struct kprobe *p) {@@ -1330,7 +1313,6 @@ static void init_aggr_kprobe(struct kpro ap->addr = p->addr; ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED ; ap->pre_handler = aggr_pre_handler; - ap->fault_handler = aggr_fault_handler;/* We don't care the kprobe which has gone. */ if (p->post_handler && !kprobe_gone (p)) ap->post_handler = aggr_post_handler;@@ -2014,7 +1996,6 @@ int register_kretprobe(struct kretprobe rp->kp.pre_handler = pre_handler_kretprobe; rp->kp.post_handler =NULL ; - rp->kp.fault_handler =NULL ;/* Pre-allocate memory for max kretprobe instances */ if (rp->maxactive <=0 ) {samples/kprobes/kprobe_example.c +0
-15@@ -86,26 +86,11 @@ static void __kprobes handler_post(struc#endif } -/* -* fault_handler: this is called if an exception is generated for any -* instruction within the pre- or post-handler, or when Kprobes -* single-steps the probed instruction. -*/ -static int handler_fault (struct kprobe *p,struct pt_regs *regs,int trapnr) -{ -pr_info ("fault_handler: p->addr = 0x%p, trap #%dn" , p->addr, trapnr); -/* Return 0 because we don't handle the fault. */ -return 0 ; -} -/* NOKPROBE_SYMBOL() is also available */ -NOKPROBE_SYMBOL (handler_fault); -static int __init kprobe_init (void ) {int ret; kp.pre_handler = handler_pre; kp.post_handler = handler_post; - kp.fault_handler = handler_fault; ret =register_kprobe (&kp);if (ret <0 ) {
Signature · 1 line
Masami Hiramatsu <[email protected]>
Quoted 33 lines · Peter Zijlstra
The reason for
kprobe::fault_handler(), as given by their comment:* We come here because instructions in the pre/post
* handler caused thepage_fault, this could happen
* if handler tries to access user space by
*copy_from_user(),get_user()etc. Let the
* user-specified handler try to fix it first.Is just plain bad. Those other handlers are ran from non-preemptible context and had better use
_nofault()functions. Also, there is no upstream usage of this.Signed-off-by Peter Zijlstra (Intel) <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>--- Documentation/trace/kprobes.rst | 24 +++++------------------- arch/arc/kernel/kprobes.c | 10 ---------- arch/arm/probes/kprobes/core.c | 9 --------- arch/arm64/kernel/probes/kprobes.c | 10 ---------- arch/csky/kernel/probes/kprobes.c | 10 ---------- arch/ia64/kernel/kprobes.c | 9 --------- arch/mips/kernel/kprobes.c | 3 --- arch/powerpc/kernel/kprobes.c | 10 ---------- arch/riscv/kernel/probes/kprobes.c | 10 ---------- arch/s390/kernel/kprobes.c | 10 ---------- arch/sh/kernel/kprobes.c | 10 ---------- arch/sparc/kernel/kprobes.c | 10 ---------- arch/x86/kernel/kprobes/core.c | 10 ---------- include/linux/kprobes.h | 8 -------- kernel/kprobes.c | 19 ------------------- samples/kprobes/kprobe_example.c | 15 --------------- 16 files changed, 5 insertions(+), 172 deletions(-)
<snip>
arch/x86/kernel/kprobes/core.c int kprobe_fault_handler(struct pt_regs · line 949
Quoted 6 lines
arch/x86/kernel/kprobes/core.c +0
-0@@ -947,16 +947,6 @@ int kprobe_fault_handler(struct pt_regs* these specific fault cases. */ kprobes_inc_nmissed_count (cur);
Not necessarily related, but I'm wondering why we're incrementing the probe miss count here. Unlike what the comment above indicates, this is not a 'fault' counter, but just a count of the number of times the probe handler wasn't called.
arch/x86/kernel/kprobes/core.c int kprobe_fault_handler(struct pt_regs · line 950
Quoted 11 lines
arch/x86/kernel/kprobes/core.c +0
-10- -/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; }
- Naveen
Masami Hiramatsu
maintainerQuoted 37 lines · On Wed, 26 May 2021 16:20:25 +0530 "Naveen N. Rao"…
Quoted 33 lines · Peter Zijlstra
The reason for
kprobe::fault_handler(), as given by their comment:* We come here because instructions in the pre/post
* handler caused thepage_fault, this could happen
* if handler tries to access user space by
*copy_from_user(),get_user()etc. Let the
* user-specified handler try to fix it first.Is just plain bad. Those other handlers are ran from non-preemptible context and had better use
_nofault()functions. Also, there is no upstream usage of this.Signed-off-by Peter Zijlstra (Intel) <[email protected]>Reviewed-by Christoph Hellwig <[email protected]>--- Documentation/trace/kprobes.rst | 24 +++++------------------- arch/arc/kernel/kprobes.c | 10 ---------- arch/arm/probes/kprobes/core.c | 9 --------- arch/arm64/kernel/probes/kprobes.c | 10 ---------- arch/csky/kernel/probes/kprobes.c | 10 ---------- arch/ia64/kernel/kprobes.c | 9 --------- arch/mips/kernel/kprobes.c | 3 --- arch/powerpc/kernel/kprobes.c | 10 ---------- arch/riscv/kernel/probes/kprobes.c | 10 ---------- arch/s390/kernel/kprobes.c | 10 ---------- arch/sh/kernel/kprobes.c | 10 ---------- arch/sparc/kernel/kprobes.c | 10 ---------- arch/x86/kernel/kprobes/core.c | 10 ---------- include/linux/kprobes.h | 8 -------- kernel/kprobes.c | 19 ------------------- samples/kprobes/kprobe_example.c | 15 --------------- 16 files changed, 5 insertions(+), 172 deletions(-)<snip>
Quoted 11 lines
arch/x86/kernel/kprobes/core.c int kprobe_fault_handler(struct pt_regs · line 949
Quoted 6 lines
arch/x86/kernel/kprobes/core.c +0
-0@@ -947,16 +947,6 @@ int kprobe_fault_handler(struct pt_regs* these specific fault cases. */ kprobes_inc_nmissed_count (cur);Not necessarily related, but I'm wondering why we're incrementing the probe miss count here. Unlike what the comment above indicates, this is not a 'fault' counter, but just a count of the number of times the probe handler wasn't called.
Good catch! Indeed, we have no ned to count these fault because it anyway gets back to the user handler. (so no user_handler is skipped) Hmm, we need to clean up these countings too.
Thank you,
Quoted 16 lines
arch/x86/kernel/kprobes/core.c int kprobe_fault_handler(struct pt_regs · line 950
Quoted 12 lines
arch/x86/kernel/kprobes/core.c +0
-10- -/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; }- Naveen
Signature · 1 line
Masami Hiramatsu <[email protected]>
PATCH 1/1 kprobes: Do not increment probe miss count in the fault handler
In reply to Peter ZijlstraKprobes has a counter 'nmissed', that is used to count the number of times a probe handler was not called. This generally happens when we hit a kprobe while handling another kprobe.
However, if one of the probe handlers causes a fault, we are currently incrementing 'nmissed'. The comment in fault handler indicates that this can be used to account faults taken by the probe handlers. But, this has never been the intention as is evident from the comment above 'nmissed' in 'struct kprobe':
/*count the number of times this probe was temporarily disarmed */ unsigned long nmissed;
---
I'm posting this here so that these can go together, if the patch is ok otherwise.
Thanks,
- Naveen
arch/arc/kernel/kprobes.c | 6 ------ arch/arm/probes/kprobes/core.c | 14 -------------- arch/arm64/kernel/probes/kprobes.c | 7 ------- arch/csky/kernel/probes/kprobes.c | 7 ------- arch/ia64/kernel/kprobes.c | 7 ------- arch/powerpc/kernel/kprobes.c | 7 ------- arch/riscv/kernel/probes/kprobes.c | 7 ------- arch/s390/kernel/kprobes.c | 7 ------- arch/sh/kernel/kprobes.c | 7 ------- arch/sparc/kernel/kprobes.c | 7 ------- arch/x86/kernel/kprobes/core.c | 8 -------- 11 files changed, 84 deletions(-)
arch/arc/kernel/kprobes.c +0 -6
@@ -317,12 +317,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned long trapnr)* caused the fault. */ -/* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); -/* * In case the user-specified fault handler returned zero, * try to fix up.
arch/arm/probes/kprobes/core.c +0 -14
@@ -348,20 +348,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)reset_current_kprobe (); }break ; - -case KPROBE_HIT_ACTIVE : -case KPROBE_HIT_SSDONE : -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); - -break ; - -default : -break ; }return 0 ;
arch/arm64/kernel/probes/kprobes.c +0 -7
@@ -276,13 +276,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)break ;case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE : -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); -/* * In case the user-specified fault handler returned * zero, try to fix up.
arch/csky/kernel/probes/kprobes.c +0 -7
@@ -294,13 +294,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)break ;case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE : -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); -/* * In case the user-specified fault handler returned * zero, try to fix up.
arch/ia64/kernel/kprobes.c +0 -7
@@ -843,13 +843,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)break ;case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE : -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); -/* * In case the user-specified fault handler returned * zero, try to fix up.
arch/powerpc/kernel/kprobes.c +0 -7
@@ -501,13 +501,6 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)break ;case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE : -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); -/* * In case the user-specified fault handler returned * zero, try to fix up.
arch/riscv/kernel/probes/kprobes.c +0 -7
@@ -278,13 +278,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)break ;case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE : -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); -/* * In case the user-specified fault handler returned * zero, try to fix up.
arch/s390/kernel/kprobes.c +0 -7
@@ -445,13 +445,6 @@ static int kprobe_trap_handler(struct pt_regs *regs, int trapnr)break ;case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE : -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (p); -/* * In case the user-specified fault handler returned * zero, try to fix up.
arch/sh/kernel/kprobes.c +0 -7
@@ -382,13 +382,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)break ;case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE : -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); -/* * In case the user-specified fault handler returned * zero, try to fix up.
arch/sparc/kernel/kprobes.c +0 -7
@@ -345,13 +345,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)break ;case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE : -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); -/* * In case the user-specified fault handler returned * zero, try to fix up.
arch/x86/kernel/kprobes/core.c +0 -8
@@ -1102,14 +1102,6 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)restore_previous_kprobe (kcb); elsereset_current_kprobe (); - }else if (kcb->kprobe_status ==KPROBE_HIT_ACTIVE || - kcb->kprobe_status ==KPROBE_HIT_SSDONE ) { -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); }return 0 ;
base-commit: c2131f7e73c9e9365613e323d65c7b9e5b910f56
prerequisite-patch-id: 78c74d348e7072bc263e19753ca25e04dda45981
Signature · 1 line
2.31.1
Peter Zijlstra
maintainerRe: [PATCH] kprobes: Do not increment probe miss count in the fault handler
Quoted 10 lines · Naveen N. Rao
Kprobes has a counter 'nmissed', that is used to count the number of times a probe handler was not called. This generally happens when we hit a kprobe while handling another kprobe.
However, if one of the probe handlers causes a fault, we are currently incrementing 'nmissed'. The comment in fault handler indicates that this can be used to account faults taken by the probe handlers. But, this has never been the intention as is evident from the comment above 'nmissed' in 'struct kprobe':
Quoted 7 lines
/*count the number of times this probe was temporarily disarmed */ unsigned long nmissed;Signed-off-by Naveen N. Rao <[email protected]>---
I'm posting this here so that these can go together, if the patch is ok otherwise.
I had the other two queued in perf/core and was about to push then to tip, Masami are you good with adding this on top?
Re: [PATCH] kprobes: Do not increment probe miss count in the fault handler
Quoted 14 lines · On Tue, 1 Jun 2021 15:20:32 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 13 lines · Naveen N. Rao
Kprobes has a counter 'nmissed', that is used to count the number of times a probe handler was not called. This generally happens when we hit a kprobe while handling another kprobe.
However, if one of the probe handlers causes a fault, we are currently incrementing 'nmissed'. The comment in fault handler indicates that this can be used to account faults taken by the probe handlers. But, this has never been the intention as is evident from the comment above 'nmissed' in 'struct kprobe':
/*count the number of times this probe was temporarily disarmed */ unsigned long nmissed;
Quoted 7 lines
Quoted 4 lines
Signed-off-by Naveen N. Rao <[email protected]>---
I'm posting this here so that these can go together, if the patch is ok otherwise.I had the other two queued in perf/core and was about to push then to tip, Masami are you good with adding this on top?
Yes, those looks good to me too.
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
[tip: perf/core] kprobes: Do not increment probe miss count in the fault handler
In reply to Naveen N. RaoThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 2e38eb04c95e5546b71bb86ee699a891c7d212b5 Gitweb: https://git.kernel.org/tip/2e38eb04c95e5546b71bb86ee699a891c7d212b5 Author: Naveen N. Rao <[email protected]> AuthorDate: Tue, 01 Jun 2021 17:31:50 +05:30 Committer: Peter Zijlstra <[email protected]> CommitterDate: Thu, 03 Jun 2021 15:47:26 +02:00
kprobes: Do not increment probe miss count in the fault handler
Kprobes has a counter 'nmissed', that is used to count the number of times a probe handler was not called. This generally happens when we hit a kprobe while handling another kprobe.
However, if one of the probe handlers causes a fault, we are currently incrementing 'nmissed'. The comment in fault handler indicates that this can be used to account faults taken by the probe handlers. But, this has never been the intention as is evident from the comment above 'nmissed' in 'struct kprobe':
/*count the number of times this probe was temporarily disarmed */ unsigned long nmissed;
--- arch/arc/kernel/kprobes.c | 6 ------ arch/arm/probes/kprobes/core.c | 14 -------------- arch/arm64/kernel/probes/kprobes.c | 7 ------- arch/csky/kernel/probes/kprobes.c | 7 ------- arch/ia64/kernel/kprobes.c | 7 ------- arch/powerpc/kernel/kprobes.c | 7 ------- arch/riscv/kernel/probes/kprobes.c | 7 ------- arch/s390/kernel/kprobes.c | 7 ------- arch/sh/kernel/kprobes.c | 7 ------- arch/sparc/kernel/kprobes.c | 7 ------- arch/x86/kernel/kprobes/core.c | 8 -------- 11 files changed, 84 deletions(-)
arch/arc/kernel/kprobes.c +0 -6
@@ -317,12 +317,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned long trapnr)* caused the fault. */ -/* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); -/* * In case the user-specified fault handler returned zero, * try to fix up.
arch/arm/probes/kprobes/core.c +0 -14
@@ -348,20 +348,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)reset_current_kprobe (); }break ; - -case KPROBE_HIT_ACTIVE : -case KPROBE_HIT_SSDONE : -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); - -break ; - -default : -break ; }return 0 ;
arch/arm64/kernel/probes/kprobes.c +0 -7
@@ -277,13 +277,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE :/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/csky/kernel/probes/kprobes.c +0 -7
@@ -295,13 +295,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE :/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/ia64/kernel/kprobes.c +0 -7
@@ -844,13 +844,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE :/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/powerpc/kernel/kprobes.c +0 -7
@@ -502,13 +502,6 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE :/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/riscv/kernel/probes/kprobes.c +0 -7
@@ -277,13 +277,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE :/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/s390/kernel/kprobes.c +0 -7
@@ -446,13 +446,6 @@ static int kprobe_trap_handler(struct pt_regs *regs, int trapnr)case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE :/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (p); - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/sh/kernel/kprobes.c +0 -7
@@ -383,13 +383,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE :/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/sparc/kernel/kprobes.c +0 -7
@@ -346,13 +346,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE :/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/x86/kernel/kprobes/core.c +0 -8
@@ -1102,14 +1102,6 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)restore_previous_kprobe (kcb); elsereset_current_kprobe (); - }else if (kcb->kprobe_status ==KPROBE_HIT_ACTIVE || - kcb->kprobe_status ==KPROBE_HIT_SSDONE ) { -/* -* We increment the nmissed count for accounting, -* we can also use npre/npostfault count for accounting -* these specific fault cases. -*/ -kprobes_inc_nmissed_count (cur); }return 0 ;
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: ec6aba3d2be1ed75b3f4c894bb64a36d40db1f55 Gitweb: https://git.kernel.org/tip/ec6aba3d2be1ed75b3f4c894bb64a36d40db1f55 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 25 May 2021 09:25:19 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 01 Jun 2021 16:00:08 +02:00
kprobes: Remove kprobe::fault_handler
The reason for kprobe::fault_handler(), as given by their comment:
* We come here because instructions in the pre/post
* handler caused the page_fault, this could happen
* if handler tries to access user space by
* copy_from_user(), get_user() etc. Let the
* user-specified handler try to fix it first.
Is just plain bad. Those other handlers are ran from non-preemptible context and had better use _nofault() functions. Also, there is no upstream usage of this.
--- Documentation/trace/kprobes.rst | 24 +++++------------------- arch/arc/kernel/kprobes.c | 10 ---------- arch/arm/probes/kprobes/core.c | 9 --------- arch/arm64/kernel/probes/kprobes.c | 10 ---------- arch/csky/kernel/probes/kprobes.c | 10 ---------- arch/ia64/kernel/kprobes.c | 9 --------- arch/mips/kernel/kprobes.c | 3 --- arch/powerpc/kernel/kprobes.c | 10 ---------- arch/riscv/kernel/probes/kprobes.c | 10 ---------- arch/s390/kernel/kprobes.c | 10 ---------- arch/sh/kernel/kprobes.c | 10 ---------- arch/sparc/kernel/kprobes.c | 10 ---------- arch/x86/kernel/kprobes/core.c | 10 ---------- include/linux/kprobes.h | 8 -------- kernel/kprobes.c | 19 ------------------- samples/kprobes/kprobe_example.c | 15 --------------- 16 files changed, 5 insertions(+), 172 deletions(-)
Documentation/trace/kprobes.rst +5 -19
@@ -362,14 +362,11 @@ register_kprobe #include <linux/kprobes.h> int register_kprobe(struct kprobe *kp); -Sets a breakpoint at the address kp->addr. When the breakpoint is -hit, Kprobes calls kp->pre_handler. After the probed instruction -is single-stepped, Kprobe calls kp->post_handler. If a fault -occurs during execution of kp->pre_handler or kp->post_handler, -or during single-stepping of the probed instruction, Kprobes calls -kp->fault_handler. Any or all handlers can be NULL. If kp->flags -is set KPROBE_FLAG_DISABLED, that kp will be registered but disabled, -so, its handlers aren't hit until calling enable_kprobe(kp). +Sets a breakpoint at the address kp->addr. When the breakpoint is hit, Kprobes +calls kp->pre_handler. After the probed instruction is single-stepped, Kprobe +calls kp->post_handler. Any or all handlers can be NULL. If kp->flags is set +KPROBE_FLAG_DISABLED, that kp will be registered but disabled, so, its handlers +aren't hit until calling enable_kprobe(kp). .. note::
@@ -415,17 +412,6 @@ User's post-handler (kp->post_handler):: p and regs are as described for the pre_handler. flags always seems to be zero. -User's fault-handler (kp->fault_handler):: - - #include <linux/kprobes.h> - #include <linux/ptrace.h> - int fault_handler(struct kprobe *p, struct pt_regs *regs, int trapnr); - -p and regs are as described for the pre_handler. trapnr is the -architecture-specific trap number associated with the fault (e.g., -on i386, 13 for a general protection fault or 14 for a page fault). -Returns 1 if it successfully handled the exception. - register_kretprobe ------------------
arch/arc/kernel/kprobes.c +0 -10
@@ -324,16 +324,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned long trapnr)kprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned zero, * try to fix up. */
arch/arm/probes/kprobes/core.c +0 -9
@@ -358,15 +358,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)*/ kprobes_inc_nmissed_count (cur); -/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, fsr)) -return 1 ;break ;default :
arch/arm64/kernel/probes/kprobes.c +0 -10
@@ -284,16 +284,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)kprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, fsr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/csky/kernel/probes/kprobes.c +0 -10
@@ -302,16 +302,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)kprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/ia64/kernel/kprobes.c +0 -9
@@ -851,15 +851,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)kprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/mips/kernel/kprobes.c +0 -3
@@ -403,9 +403,6 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)struct kprobe *cur =kprobe_running ();struct kprobe_ctlblk *kcb =get_kprobe_ctlblk (); -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; -if (kcb->kprobe_status &KPROBE_HIT_SS ) {resume_execution (cur, regs, kcb); regs->cp0_status |= kcb->kprobe_old_SR;
arch/powerpc/kernel/kprobes.c +0 -10
@@ -509,16 +509,6 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)kprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/riscv/kernel/probes/kprobes.c +0 -10
@@ -284,16 +284,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)kprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/s390/kernel/kprobes.c +0 -10
@@ -453,16 +453,6 @@ static int kprobe_trap_handler(struct pt_regs *regs, int trapnr)kprobes_inc_nmissed_count (p);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (p->fault_handler && p->fault_handler (p, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/sh/kernel/kprobes.c +0 -10
@@ -390,16 +390,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)kprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/sparc/kernel/kprobes.c +0 -10
@@ -353,16 +353,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)kprobes_inc_nmissed_count (cur);/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; - -/* * In case the user-specified fault handler returned * zero, try to fix up. */
arch/x86/kernel/kprobes/core.c +0 -10
@@ -1110,16 +1110,6 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)* these specific fault cases. */ kprobes_inc_nmissed_count (cur); - -/* -* We come here because instructions in the pre/post -* handler caused the page_fault, this could happen -* if handler tries to access user space by -* copy_from_user(), get_user() etc. Let the -* user-specified handler try to fix it first. -*/ -if (cur->fault_handler && cur->fault_handler (cur, regs, trapnr)) -return 1 ; }return 0 ;
include/linux/kprobes.h +0 -8
@@ -54,8 +54,6 @@ struct kretprobe_instance;typedef int (*kprobe_pre_handler_t ) (struct kprobe *,struct pt_regs *);typedef void (*kprobe_post_handler_t ) (struct kprobe *,struct pt_regs *,unsigned long flags); -typedef int (*kprobe_fault_handler_t ) (struct kprobe *,struct pt_regs *, -int trapnr);typedef int (*kretprobe_handler_t ) (struct kretprobe_instance *,struct pt_regs *);
@@ -83,12 +81,6 @@ struct kprobe {/* Called after addr is executed, unless... */ kprobe_post_handler_t post_handler; -/* -* ... called if executing addr causes a fault (eg. page fault). -* Return 1 if it handled fault, otherwise kernel will see it. -*/ -kprobe_fault_handler_t fault_handler; -/* Saved opcode (which has been replaced with breakpoint) */ kprobe_opcode_t opcode;
kernel/kprobes.c +0 -19
@@ -1183,23 +1183,6 @@ static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs, }NOKPROBE_SYMBOL (aggr_post_handler); -static int aggr_fault_handler (struct kprobe *p,struct pt_regs *regs, -int trapnr) -{ -struct kprobe *cur =__this_cpu_read (kprobe_instance); - -/* -* if we faulted "during" the execution of a user specified -* probe handler, invoke just that probe's fault handler -*/ -if (cur && cur->fault_handler) { -if (cur->fault_handler (cur, regs, trapnr)) -return 1 ; - } -return 0 ; -} -NOKPROBE_SYMBOL (aggr_fault_handler); -/* Walks the list and increments nmissed count for multiprobe case */ void kprobes_inc_nmissed_count (struct kprobe *p) {
@@ -1330,7 +1313,6 @@ static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p) ap->addr = p->addr; ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED ; ap->pre_handler = aggr_pre_handler; - ap->fault_handler = aggr_fault_handler;/* We don't care the kprobe which has gone. */ if (p->post_handler && !kprobe_gone (p)) ap->post_handler = aggr_post_handler;
@@ -2014,7 +1996,6 @@ int register_kretprobe(struct kretprobe *rp) rp->kp.pre_handler = pre_handler_kretprobe; rp->kp.post_handler =NULL ; - rp->kp.fault_handler =NULL ;/* Pre-allocate memory for max kretprobe instances */ if (rp->maxactive <=0 ) {
samples/kprobes/kprobe_example.c +0 -15
@@ -94,26 +94,11 @@ static void __kprobes handler_post(struct kprobe *p, struct pt_regs *regs,#endif } -/* -* fault_handler: this is called if an exception is generated for any -* instruction within the pre- or post-handler, or when Kprobes -* single-steps the probed instruction. -*/ -static int handler_fault (struct kprobe *p,struct pt_regs *regs,int trapnr) -{ -pr_info ("fault_handler: p->addr = 0x%p, trap #%dn" , p->addr, trapnr); -/* Return 0 because we don't handle the fault. */ -return 0 ; -} -/* NOKPROBE_SYMBOL() is also available */ -NOKPROBE_SYMBOL (handler_fault); -static int __init kprobe_init (void ) {int ret; kp.pre_handler = handler_pre; kp.post_handler = handler_post; - kp.fault_handler = handler_fault; ret =register_kprobe (&kp);if (ret <0 ) {
Peter Zijlstra
maintainerPATCH 2/2 x86,kprobes: WARN if kprobes tries to handle a fault
In reply to Peter ZijlstraWith the removal of kprobe::handle_fault there is no reason left that kprobe_page_fault() would ever return true on x86, make sure it doesn't happen by accident.
--- arch/x86/mm/fault.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/mm/fault.c +2 -2
@@ -1186,7 +1186,7 @@ do_kern_addr_fault(struct pt_regs *regs,return ;/* kprobes don't want to hook the spurious faults: */ -if (kprobe_page_fault (regs,X86_TRAP_PF )) +if (WARN_ON_ONCE (kprobe_page_fault (regs,X86_TRAP_PF )))return ;/*
@@ -1239,7 +1239,7 @@ void do_user_addr_fault(struct pt_regs * }/* kprobes don't want to hook the spurious faults: */ -if (unlikely (kprobe_page_fault (regs,X86_TRAP_PF ))) +if (WARN_ON_ONCE (kprobe_page_fault (regs,X86_TRAP_PF )))return ;/*
Re: [PATCH 2/2] x86,kprobes: WARN if kprobes tries to handle a fault
Quoted 3 lines · On Tue, 25 May 2021 09:25:20 +0200 Peter Zijlstra <[email protected]> wrote:
With the removal of
kprobe::handle_faultthere is no reason left thatkprobe_page_fault()would ever return true on x86, make sure it doesn't happen by accident.
OK, this is reasonable to me.
Thank you!
Quoted 28 lines
Signed-off-by Peter Zijlstra (Intel) <[email protected]>--- arch/x86/mm/fault.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/mm/fault.c +2
-2@@ -1186,7 +1186,7 @@ do_kern_addr_fault(struct pt_regs *regs,return ;/* kprobes don't want to hook the spurious faults: */ -if (kprobe_page_fault (regs,X86_TRAP_PF )) +if (WARN_ON_ONCE (kprobe_page_fault (regs,X86_TRAP_PF )))return ;/* @@ -1239,7 +1239,7 @@ void do_user_addr_fault(struct pt_regs * }/* kprobes don't want to hook the spurious faults: */ -if (unlikely (kprobe_page_fault (regs,X86_TRAP_PF ))) +if (WARN_ON_ONCE (kprobe_page_fault (regs,X86_TRAP_PF )))return ;/*
Signature · 1 line
Masami Hiramatsu <[email protected]>
[tip: perf/core] x86,kprobes: WARN if kprobes tries to handle a fault
In reply to Peter ZijlstraThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 00afe83098f59d3091a800d0db188ca495b2bc02 Gitweb: https://git.kernel.org/tip/00afe83098f59d3091a800d0db188ca495b2bc02 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 25 May 2021 09:25:20 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 01 Jun 2021 16:00:09 +02:00
x86,kprobes: WARN if kprobes tries to handle a fault
With the removal of kprobe::handle_fault there is no reason left that kprobe_page_fault() would ever return true on x86, make sure it doesn't happen by accident.
--- arch/x86/mm/fault.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/mm/fault.c +2 -2
@@ -1186,7 +1186,7 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code,return ;/* kprobes don't want to hook the spurious faults: */ -if (kprobe_page_fault (regs,X86_TRAP_PF )) +if (WARN_ON_ONCE (kprobe_page_fault (regs,X86_TRAP_PF )))return ;/*
@@ -1239,7 +1239,7 @@ void do_user_addr_fault(struct pt_regs *regs, }/* kprobes don't want to hook the spurious faults: */ -if (unlikely (kprobe_page_fault (regs,X86_TRAP_PF ))) +if (WARN_ON_ONCE (kprobe_page_fault (regs,X86_TRAP_PF )))return ;/*