Skip to contentSkip to conversation

kprobes: x86: Cleanup jprobe implementation on x86

PATCH Superseded Masami Hiramatsu 2 participants 16 messages May 16, 2018 – May 18, 2018

Subsystems (5)
Documentation x86 Tracing ftrace +1 more subsystems

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

v3 cover kprobes: x86: Cleanup jprobe implementation on x86

Hello,

Since we decided to remove jprobe from kernel last year, its APIs are disabled and we worked on moving in-kernel jprobe users to kprobes or trace-events. And now no jprobe users are here anymore.

This is the 3rd version of the series for removing jprobe from x86 and generic code. V2 is here;

http://lkml.org/lkml/2018/3/12/558

Changes from v2 are;
- [1/7] Imported from another series. Just a documentation fix.
- [6/7] Imported from another series. Simplifying execution path modifying behavior.
- [7/7] Modified based on [6/7], because it has done a half of changes what this patch does.

I am preparing other series which removes jprobe from each arch. After all those patches are merged, I will remove jprobes APIs and data structures, since changing those definitions will break build on other archs.

Thank you,

---

Masami Hiramatsu (7):
Documentation/kprobes: Fix to remove remaining jprobe
kprobes: Remove jprobe API implementation
x86: kprobes: Remove jprobe implementation
kprobes: Ignore break_handler
x86: kprobes: Ignore break_handler
bpf: error-inject: x86: Fix unbalanced preempt-count for function override
x86: kprobes: Do not disable preempt on int3 path

 Documentation/kprobes.txt        |   15 ++---
 arch/x86/include/asm/kprobes.h   |    3 -
 arch/x86/kernel/kprobes/common.h |   10 ---
 arch/x86/kernel/kprobes/core.c   |  116 ++------------------------------------
 arch/x86/kernel/kprobes/ftrace.c |   31 +++-------
 arch/x86/kernel/kprobes/opt.c    |    1 
 include/linux/kprobes.h          |    3 -
 kernel/fail_function.c           |    3 -
 kernel/kprobes.c                 |  115 ++------------------------------------
 kernel/test_kprobes.c            |   94 -------------------------------
 kernel/trace/trace_kprobe.c      |   11 +---
 11 files changed, 32 insertions(+), 370 deletions(-)

--
Masami Hiramatsu (Linaro)

author maintainer
#1 lore raw

PATCH v3 1/7 Documentation/kprobes: Fix to remove remaining jprobe

Remove jps from the document, since jprobe is removed.

---
 Documentation/kprobes.txt |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/kprobes.txt +1 -1
index 22208bf2386d..5ae80baf3921 100644
@@ -474,7 +474,7 @@ error occurs during registration, all probes in the array, up to
 the bad probe, are safely unregistered before the register_*probes
 function returns.
 
-- kps/rps/jps: an array of pointers to ``*probe`` data structures
+- kps/rps: an array of pointers to ``*probe`` data structures
 - num: the number of the array entries.
 
 .. note::
author maintainer
#2 lore raw

PATCH v3 2/7 kprobes: Remove jprobe API implementation

In reply to Masami Hiramatsu

Remove jprobe API implementations and test cases for those APIs which is no more used.

---
Changes in v3:
- Remove test cases.

---
 include/linux/kprobes.h |    3 --
 kernel/kprobes.c        |   78 +--------------------------------------
 kernel/test_kprobes.c   |   94 -----------------------------------------------
 3 files changed, 1 insertion(+), 174 deletions(-)
include/linux/kprobes.h +0 -3
index 9440a2fc8893..b520baa65682 100644
@@ -389,9 +389,6 @@ int register_kprobe(struct kprobe *p);
 void unregister_kprobe(struct kprobe *p);
 int register_kprobes(struct kprobe **kps, int num);
 void unregister_kprobes(struct kprobe **kps, int num);
-int setjmp_pre_handler(struct kprobe *, struct pt_regs *);
-int longjmp_break_handler(struct kprobe *, struct pt_regs *);
-void jprobe_return(void);
 unsigned long arch_deref_entry_point(void *);
 
 int register_kretprobe(struct kretprobe *rp);
kernel/kprobes.c +1 -77

99 lines not on this page · raw message

kernel/test_kprobes.c +0 -94

106 lines not on this page · raw message

author maintainer
#3 lore raw

PATCH v3 3/7 x86: kprobes: Remove jprobe implementation

In reply to Masami Hiramatsu

Remove arch dependent setjump/longjump functions
and unused fields in kprobe_ctlblk for jprobes.

---
 arch/x86/include/asm/kprobes.h |    3 -
 arch/x86/kernel/kprobes/core.c |   87 ----------------------------------------
 2 files changed, 90 deletions(-)
arch/x86/include/asm/kprobes.h +0 -3
index 367d99cff426..06782c2efa04 100644
@@ -111,9 +111,6 @@ struct kprobe_ctlblk {
 	unsigned long kprobe_status;
 	unsigned long kprobe_old_flags;
 	unsigned long kprobe_saved_flags;
-	unsigned long *jprobe_saved_sp;
-	struct pt_regs jprobe_saved_regs;
-	kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
 	struct prev_kprobe prev_kprobe;
 };
 
arch/x86/kernel/kprobes/core.c +0 -87

93 lines not on this page · raw message

author maintainer
#4 lore raw

PATCH v3 4/7 kprobes: Ignore break_handler

In reply to Masami Hiramatsu

Ignore break_handler related code because it was only used by jprobe and jprobe is removed.

---
 Documentation/kprobes.txt |    2 +-
 kernel/kprobes.c          |   39 +++++----------------------------------
 2 files changed, 6 insertions(+), 35 deletions(-)
Documentation/kprobes.txt +1 -1
index 5ae80baf3921..907a3017c0f2 100644
@@ -262,7 +262,7 @@ is optimized, that modification is ignored.  Thus, if you want to
 tweak the kernel's execution path, you need to suppress optimization,
 using one of the following techniques:
 
-- Specify an empty function for the kprobe's post_handler or break_handler.
+- Specify an empty function for the kprobe's post_handler.
 
 or
 
kernel/kprobes.c +5 -34
index 69de130595f7..536ab451e96d 100644
@@ -627,8 +627,8 @@ static void optimize_kprobe(struct kprobe *p)
 	    (kprobe_disabled(p) || kprobes_all_disarmed))
 		return;
 
-	/* Both of break_handler and post_handler are not supported. */
-	if (p->break_handler || p->post_handler)
+	/* kprobes with post_handler can not be optimized */
+	if (p->post_handler)
 		return;
 
 	op = container_of(p, struct optimized_kprobe, kp);
@@ -1116,20 +1116,6 @@ static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
 }
 NOKPROBE_SYMBOL(aggr_fault_handler);
 
-static int aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
-{
-	struct kprobe *cur = __this_cpu_read(kprobe_instance);
-	int ret = 0;
-
-	if (cur && cur->break_handler) {
-		if (cur->break_handler(cur, regs))
-			ret = 1;
-	}
-	reset_kprobe_instance();
-	return ret;
-}
-NOKPROBE_SYMBOL(aggr_break_handler);
-
 /* Walks the list and increments nmissed count for multiprobe case */
 void kprobes_inc_nmissed_count(struct kprobe *p)
 {
@@ -1270,24 +1256,15 @@ static void cleanup_rp_inst(struct kretprobe *rp)
 }
 NOKPROBE_SYMBOL(cleanup_rp_inst);
 
-/*
-* Add the new probe to ap->list. Fail if this is the
-* second break_handler at the address
-*/
+/* Add the new probe to ap->list */
 static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
 {
 	BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
 
-	if (p->break_handler || p->post_handler)
+	if (p->post_handler)
 		unoptimize_kprobe(ap, true);	/* Fall back to normal kprobe */
 
-	if (p->break_handler) {
-		if (ap->break_handler)
-			return -EEXIST;
-		list_add_tail_rcu(&p->list, &ap->list);
-		ap->break_handler = aggr_break_handler;
-	} else
-		list_add_rcu(&p->list, &ap->list);
+	list_add_rcu(&p->list, &ap->list);
 	if (p->post_handler && !ap->post_handler)
 		ap->post_handler = aggr_post_handler;
 
@@ -1310,8 +1287,6 @@ static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
 	/* We don't care the kprobe which has gone. */
 	if (p->post_handler && !kprobe_gone(p))
 		ap->post_handler = aggr_post_handler;
-	if (p->break_handler && !kprobe_gone(p))
-		ap->break_handler = aggr_break_handler;
 
 	INIT_LIST_HEAD(&ap->list);
 	INIT_HLIST_NODE(&ap->hlist);
@@ -1706,8 +1681,6 @@ static int __unregister_kprobe_top(struct kprobe *p)
 		goto disarmed;
 	else {
 		/* If disabling probe has special handlers, update aggrprobe */
-		if (p->break_handler && !kprobe_gone(p))
-			ap->break_handler = NULL;
 		if (p->post_handler && !kprobe_gone(p)) {
 			list_for_each_entry_rcu(list_p, &ap->list, list) {
 				if ((list_p != p) && (list_p->post_handler))
@@ -1911,7 +1884,6 @@ int register_kretprobe(struct kretprobe *rp)
 	rp->kp.pre_handler = pre_handler_kretprobe;
 	rp->kp.post_handler = NULL;
 	rp->kp.fault_handler = NULL;
-	rp->kp.break_handler = NULL;
 
 	/* Pre-allocate memory for max kretprobe instances */
 	if (rp->maxactive <= 0) {
@@ -2034,7 +2006,6 @@ static void kill_kprobe(struct kprobe *p)
 		list_for_each_entry_rcu(kp, &p->list, list)
 			kp->flags |= KPROBE_FLAG_GONE;
 		p->post_handler = NULL;
-		p->break_handler = NULL;
 		kill_optimized_kprobe(p);
 	}
 	/*
#5 lore raw

Re: [PATCH -tip v3 4/7] kprobes: Ignore break_handler

Quoted 8 lines · Masami Hiramatsu

Ignore break_handler related code because it was only used by jprobe and jprobe is removed.

---
 Documentation/kprobes.txt |    2 +-
 kernel/kprobes.c          |   39 +++++----------------------------------
 2 files changed, 6 insertions(+), 35 deletions(-)

Even after all these changes there's quite a bit of ->break_handler() use:

triton:~/tip> git grep -w break_handler

arch/arc/kernel/kprobes.c:              if (p->break_handler && p->break_handler(p, regs)) {
arch/arm/probes/kprobes/core.c:                  * for calling the break_handler below on re-entry,
arch/arm/probes/kprobes/core.c:         if (cur->break_handler && cur->break_handler(cur, regs)) {
arch/arm64/kernel/probes/kprobes.c:                      * for calling the break_handler below on re-entry,
arch/arm64/kernel/probes/kprobes.c:             if (cur_kprobe->break_handler  &&
arch/arm64/kernel/probes/kprobes.c:                  cur_kprobe->break_handler(cur_kprobe, regs)) {
arch/ia64/kernel/kprobes.c:                     if (p->break_handler && p->break_handler(p, regs)) {
arch/mips/kernel/kprobes.c:                     if (p->break_handler && p->break_handler(p, regs))
arch/powerpc/kernel/kprobes.c:                  if (p->break_handler && p->break_handler(p, regs)) {
arch/s390/kernel/kprobes.c:                      * for calling the break_handler below on re-entry
arch/s390/kernel/kprobes.c:             if (p->break_handler && p->break_handler(p, regs)) {
arch/s390/kernel/kprobes.c:                      * break_handler "returns" to the original
arch/sh/kernel/kprobes.c:                       if (p->break_handler && p->break_handler(p, regs)) {
arch/sh/kernel/kprobes.c:                                       if (p->break_handler &&
arch/sh/kernel/kprobes.c:                                           p->break_handler(p, args->regs))
arch/sparc/kernel/kprobes.c:                    if (p->break_handler && p->break_handler(p, regs))
arch/x86/include/asm/kprobes.h:  * a post_handler or break_handler).
include/linux/kprobes.h:        kprobe_break_handler_t break_handler;

Nothing appears to be actually _setting_ the break_handler, so all of this is stale code as well which should be removed?

Thanks,

Ingo

author maintainer
#6 lore raw

Re: [PATCH -tip v3 4/7] kprobes: Ignore break_handler

Quoted 34 lines · On Fri, 18 May 2018 08:16:36 +0200 Ingo Molnar <[email protected]> wrote:
Quoted 8 lines · Masami Hiramatsu

Ignore break_handler related code because it was only used by jprobe and jprobe is removed.

---
 Documentation/kprobes.txt |    2 +-
 kernel/kprobes.c          |   39 +++++----------------------------------
 2 files changed, 6 insertions(+), 35 deletions(-)

Even after all these changes there's quite a bit of ->break_handler() use:

triton:~/tip> git grep -w break_handler

arch/arc/kernel/kprobes.c:              if (p->break_handler && p->break_handler(p, regs)) {
arch/arm/probes/kprobes/core.c:                  * for calling the break_handler below on re-entry,
arch/arm/probes/kprobes/core.c:         if (cur->break_handler && cur->break_handler(cur, regs)) {
arch/arm64/kernel/probes/kprobes.c:                      * for calling the break_handler below on re-entry,
arch/arm64/kernel/probes/kprobes.c:             if (cur_kprobe->break_handler  &&
arch/arm64/kernel/probes/kprobes.c:                  cur_kprobe->break_handler(cur_kprobe, regs)) {
arch/ia64/kernel/kprobes.c:                     if (p->break_handler && p->break_handler(p, regs)) {
arch/mips/kernel/kprobes.c:                     if (p->break_handler && p->break_handler(p, regs))
arch/powerpc/kernel/kprobes.c:                  if (p->break_handler && p->break_handler(p, regs)) {
arch/s390/kernel/kprobes.c:                      * for calling the break_handler below on re-entry
arch/s390/kernel/kprobes.c:             if (p->break_handler && p->break_handler(p, regs)) {
arch/s390/kernel/kprobes.c:                      * break_handler "returns" to the original
arch/sh/kernel/kprobes.c:                       if (p->break_handler && p->break_handler(p, regs)) {
arch/sh/kernel/kprobes.c:                                       if (p->break_handler &&
arch/sh/kernel/kprobes.c:                                           p->break_handler(p, args->regs))
arch/sparc/kernel/kprobes.c:                    if (p->break_handler && p->break_handler(p, regs))
arch/x86/include/asm/kprobes.h:  * a post_handler or break_handler).
include/linux/kprobes.h:        kprobe_break_handler_t break_handler;
Quoted 2 lines

Nothing appears to be actually _setting_ the break_handler, so all of this is stale code as well which should be removed?

Yes, break_handler() is only for jprobe, not for user-defined handler. I already have a series of those patches, but I think I need to send it to each arch maintainer.

Thank you,

Quoted 4 lines

Thanks,

Ingo

Signature · 1 line
Masami Hiramatsu <[email protected]>
#7 lore raw

Re: [PATCH -tip v3 4/7] kprobes: Ignore break_handler

In reply to Masami Hiramatsu
Quoted 2 lines · Masami Hiramatsu

Ignore break_handler related code because it was only used by jprobe and jprobe is removed.

I changed this description to:

============
Subject: kprobes: Don't call the ->break_handler() in generic kprobes code

Don't call the ->break_handler() from the core kprobes code, because it was only used by jprobes which got removed.

( In a followup patch we'll remove the remaining calls in low level
arch handlers as well and remove the callback altogether. )
============

Please try to be a lot less vague in changelogs when it's possible and relevant!

I.e. saying "Ignore break_handler related code" is annoyingly vague, it doesn't explain things well at all. Saying "Don't call the ->break_handler()" is just as compact, yet it also makes it very clear what's done in the patch...

Thanks,

Ingo

author maintainer
#8 lore raw

Re: [PATCH -tip v3 4/7] kprobes: Ignore break_handler

Quoted 18 lines · On Fri, 18 May 2018 08:20:34 +0200 Ingo Molnar <[email protected]> wrote:
Quoted 2 lines · Masami Hiramatsu

Ignore break_handler related code because it was only used by jprobe and jprobe is removed.

I changed this description to:

============
Subject: kprobes: Don't call the ->break_handler() in generic kprobes code

Don't call the ->break_handler() from the core kprobes code, because it was only used by jprobes which got removed.

( In a followup patch we'll remove the remaining calls in low level
arch handlers as well and remove the callback altogether. )
============

Quoted 5 lines

Please try to be a lot less vague in changelogs when it's possible and relevant!

I.e. saying "Ignore break_handler related code" is annoyingly vague, it doesn't explain things well at all. Saying "Don't call the ->break_handler()" is just as compact, yet it also makes it very clear what's done in the patch...

OK, I'll try to describe more concretely and straightforwardly.

Thank you for telling me your advice!!

Quoted 4 lines

Thanks,

Ingo

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#9 lore raw

PATCH v3 5/7 x86: kprobes: Ignore break_handler

In reply to Masami Hiramatsu

Remove break_handler related code since that was used only for jprobe and jprobe is removed now.

---
 arch/x86/kernel/kprobes/common.h |   10 ----------
 arch/x86/kernel/kprobes/core.c   |   13 ++-----------
 arch/x86/kernel/kprobes/ftrace.c |   16 ++--------------
 3 files changed, 4 insertions(+), 35 deletions(-)
arch/x86/kernel/kprobes/common.h +0 -10
index ae38dccf0c8f..2b949f4fd4d8 100644
@@ -105,14 +105,4 @@ static inline unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf, unsig
 }
 #endif
 
-#ifdef CONFIG_KPROBES_ON_FTRACE
-extern int skip_singlestep(struct kprobe *p, struct pt_regs *regs,
-			   struct kprobe_ctlblk *kcb);
-#else
-static inline int skip_singlestep(struct kprobe *p, struct pt_regs *regs,
-				  struct kprobe_ctlblk *kcb)
-{
-	return 0;
-}
-#endif
 #endif
arch/x86/kernel/kprobes/core.c +2 -11
index 7d4b8e870635..4badf22bb11f 100644
@@ -690,10 +690,8 @@ int kprobe_int3_handler(struct pt_regs *regs)
 			/*
 			 * If we have no pre-handler or it returned 0, we
 			 * continue with normal processing.  If we have a
-			 * pre-handler and it returned non-zero, it prepped
-			 * for calling the break_handler below on re-entry
-			 * for jprobe processing, so get out doing nothing
-			 * more here.
+			 * pre-handler and it returned non-zero, it modified
+			 * regs->ip so no singlestep is needed.
 			 */
 			if (!p->pre_handler || !p->pre_handler(p, regs))
 				setup_singlestep(p, regs, kcb, 0);
@@ -712,13 +710,6 @@ int kprobe_int3_handler(struct pt_regs *regs)
 		regs->ip = (unsigned long)addr;
 		preempt_enable_no_resched();
 		return 1;
-	} else if (kprobe_running()) {
-		p = __this_cpu_read(current_kprobe);
-		if (p->break_handler && p->break_handler(p, regs)) {
-			if (!skip_singlestep(p, regs, kcb))
-				setup_singlestep(p, regs, kcb, 0);
-			return 1;
-		}
 	} /* else: not a kprobe fault; let the kernel handle it */
 
 	preempt_enable_no_resched();
arch/x86/kernel/kprobes/ftrace.c +2 -14
index 8dc0161cec8f..c8696f2a583f 100644
@@ -26,7 +26,7 @@
 #include "common.h"
 
 static nokprobe_inline
-void __skip_singlestep(struct kprobe *p, struct pt_regs *regs,
+void skip_singlestep(struct kprobe *p, struct pt_regs *regs,
 		      struct kprobe_ctlblk *kcb, unsigned long orig_ip)
 {
 	/*
@@ -43,18 +43,6 @@ void __skip_singlestep(struct kprobe *p, struct pt_regs *regs,
 		regs->ip = orig_ip;
 }
 
-int skip_singlestep(struct kprobe *p, struct pt_regs *regs,
-		    struct kprobe_ctlblk *kcb)
-{
-	if (kprobe_ftrace(p)) {
-		__skip_singlestep(p, regs, kcb, 0);
-		preempt_enable_no_resched();
-		return 1;
-	}
-	return 0;
-}
-NOKPROBE_SYMBOL(skip_singlestep);
-
 /* Ftrace callback handler for kprobes -- called under preepmt disabed */
 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
 			   struct ftrace_ops *ops, struct pt_regs *regs)
@@ -80,7 +68,7 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
 		__this_cpu_write(current_kprobe, p);
 		kcb->kprobe_status = KPROBE_HIT_ACTIVE;
 		if (!p->pre_handler || !p->pre_handler(p, regs)) {
-			__skip_singlestep(p, regs, kcb, orig_ip);
+			skip_singlestep(p, regs, kcb, orig_ip);
 			preempt_enable_no_resched();
 		}
 		/*
#10 lore raw

Re: [PATCH -tip v3 5/7] x86: kprobes: Ignore break_handler

Quoted 9 lines · Masami Hiramatsu

Remove break_handler related code since that was used only for jprobe and jprobe is removed now.

---
 arch/x86/kernel/kprobes/common.h |   10 ----------
 arch/x86/kernel/kprobes/core.c   |   13 ++-----------
 arch/x86/kernel/kprobes/ftrace.c |   16 ++--------------
 3 files changed, 4 insertions(+), 35 deletions(-)

Even after this change there's a stale reference to ->break_handler():

arch/x86/include/asm/kprobes.h: * a post_handler or break_handler).

Please use 'git grep'!

Also, there's no reason to send an incomplete series: please remove _all_ ->break_handler() references so that it's 100% gone! The pain of having and not having ->break_handler() is completely unnecessary.

Thanks,

Ingo

author maintainer
#11 lore raw

Re: [PATCH -tip v3 5/7] x86: kprobes: Ignore break_handler

Quoted 13 lines · On Fri, 18 May 2018 08:26:38 +0200 Ingo Molnar <[email protected]> wrote:
Quoted 9 lines · Masami Hiramatsu

Remove break_handler related code since that was used only for jprobe and jprobe is removed now.

---
 arch/x86/kernel/kprobes/common.h |   10 ----------
 arch/x86/kernel/kprobes/core.c   |   13 ++-----------
 arch/x86/kernel/kprobes/ftrace.c |   16 ++--------------
 3 files changed, 4 insertions(+), 35 deletions(-)
Quoted 5 lines

Even after this change there's a stale reference to ->break_handler():

arch/x86/include/asm/kprobes.h: * a post_handler or break_handler).

Please use 'git grep'!

Oops, I missed it. Thank you, I'll use git-grep next time.

Quoted 4 lines

Also, there's no reason to send an incomplete series: please remove _all_ ->break_handler() references so that it's 100% gone! The pain of having and not having ->break_handler() is completely unnecessary.

Would you mean a single patch for all arch?
I'm currently has arch-separated patches for removing it. Should I fold those?

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#12 lore raw

PATCH v3 6/7 bpf: error-inject: x86: Fix unbalanced preempt-count for function override

In reply to Masami Hiramatsu

Fix unbalanced preempt-count for function override using kprobes on x86.

Jprobe requires to keep preemption disabled and keep current kprobes until it recovers to original function entry. For this reason kprobe_int3_handler() makes preempt-count unbalanced when user handler returns !0.

After removing the jprobe, Kprobes does not need to keep preempt disabled even if user handler returns !0.

But since the function override handler is also returns !0 if it overrides a function, to balancing the preempt count, it enables preemption and reset current kprobe by itself.

That is a bad design that is very buggy. This fixes the unbalanced preempt-count in both kprobes and function override on x86.
Of course we have to fix same code of kprobes on other archs to keep the kprobes behavior consistent. But this is bisect-safe because the function override (CONFIG_FUNCTION_ERROR_INJECTION) implemented only on x86 at this moment.

---
 arch/x86/kernel/kprobes/core.c   |    4 ++++
 arch/x86/kernel/kprobes/ftrace.c |   15 ++++++++-------
 kernel/fail_function.c           |    3 ---
 kernel/trace/trace_kprobe.c      |   11 +++--------
 4 files changed, 15 insertions(+), 18 deletions(-)
arch/x86/kernel/kprobes/core.c +4 -0
index 4badf22bb11f..215a28bdd9df 100644
@@ -695,6 +695,10 @@ int kprobe_int3_handler(struct pt_regs *regs)
 			 */
 			if (!p->pre_handler || !p->pre_handler(p, regs))
 				setup_singlestep(p, regs, kcb, 0);
+			else {
+				reset_current_kprobe();
+				preempt_enable_no_resched();
+			}
 			return 1;
 		}
 	} else if (*addr != BREAKPOINT_INSTRUCTION) {
arch/x86/kernel/kprobes/ftrace.c +8 -7
index c8696f2a583f..310ef737b9d4 100644
@@ -63,18 +63,19 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
 		/* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
 		regs->ip = ip + sizeof(kprobe_opcode_t);
 
-		/* To emulate trap based kprobes, preempt_disable here */
-		preempt_disable();
 		__this_cpu_write(current_kprobe, p);
 		kcb->kprobe_status = KPROBE_HIT_ACTIVE;
 		if (!p->pre_handler || !p->pre_handler(p, regs)) {
 			skip_singlestep(p, regs, kcb, orig_ip);
-			preempt_enable_no_resched();
+		} else {
+			/*
+			 * If pre_handler returns !0, this handler
+			 * modifies regs->ip and goes back to there
+			 * directly without single stepping.
+			 * So let's just clear current kprobe.
+			 */
+			__this_cpu_write(current_kprobe, NULL);
 		}
-		/*
-		 * If pre_handler returns !0, it sets regs->ip and
-		 * resets current kprobe, and keep preempt count +1.
-		 */
 	}
 }
 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
kernel/fail_function.c +0 -3
index 1d5632d8bbcc..b090688df94f 100644
@@ -184,9 +184,6 @@ static int fei_kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
 	if (should_fail(&fei_fault_attr, 1)) {
 		regs_set_return_value(regs, attr->retval);
 		override_function_with_return(regs);
-		/* Kprobe specific fixup */
-		reset_current_kprobe();
-		preempt_enable_no_resched();
 		return 1;
 	}
 
kernel/trace/trace_kprobe.c +3 -8
index 02aed76e0978..b65cd6834450 100644
@@ -1217,16 +1217,11 @@ kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
 
 		/*
 		 * We need to check and see if we modified the pc of the
-		 * pt_regs, and if so clear the kprobe and return 1 so that we
-		 * don't do the single stepping.
-		 * The ftrace kprobe handler leaves it up to us to re-enable
-		 * preemption here before returning if we've modified the ip.
+		 * pt_regs, and if so return 1 so that we don't do the
+		 * single stepping.
 		 */
-		if (orig_ip != instruction_pointer(regs)) {
-			reset_current_kprobe();
-			preempt_enable_no_resched();
+		if (orig_ip != instruction_pointer(regs))
 			return 1;
-		}
 		if (!ret)
 			return 0;
 	}
author maintainer
#13 lore raw

PATCH v3 7/7 x86: kprobes: Do not disable preempt on int3 path

In reply to Masami Hiramatsu

Since int3 and debug exception(for singlestep) are run with IRQ disabled and while running single stepping we drop IF from regs->flags, that path must not be preemptible. So we can remove the preempt disable/enable calls from that path.

---
Changes in v3:
- Split user-side changes to another patch
Changes in v2:
- Include user-side changes.

---
 Documentation/kprobes.txt      |   11 +++++------
 arch/x86/kernel/kprobes/core.c |   18 ++++--------------
 arch/x86/kernel/kprobes/opt.c  |    1 -
 3 files changed, 9 insertions(+), 21 deletions(-)
Documentation/kprobes.txt +5 -6
index 907a3017c0f2..3e9e99ea751b 100644
@@ -566,12 +566,11 @@ the same handler) may run concurrently on different CPUs.
 Kprobes does not use mutexes or allocate memory except during
 registration and unregistration.
 
-Probe handlers are run with preemption disabled.  Depending on the
-architecture and optimization state, handlers may also run with
-interrupts disabled (e.g., kretprobe handlers and optimized kprobe
-handlers run without interrupt disabled on x86/x86-64).  In any case,
-your handler should not yield the CPU (e.g., by attempting to acquire
-a semaphore).
+Probe handlers are run with preemption disabled or interrupt disabled,
+which depends on the architecture and optimization state.  (e.g.,
+kretprobe handlers and optimized kprobe handlers run without interrupt
+disabled on x86/x86-64).  In any case, your handler should not yield
+the CPU (e.g., by attempting to acquire a semaphore, or waiting I/O).
 
 Since a return probe is implemented by replacing the return
 address with the trampoline's address, stack backtraces and calls
arch/x86/kernel/kprobes/core.c +4 -14
index 215a28bdd9df..a2896ff47ebc 100644
@@ -596,7 +596,6 @@ static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
 		 * stepping.
 		 */
 		regs->ip = (unsigned long)p->ainsn.insn;
-		preempt_enable_no_resched();
 		return;
 	}
 #endif
@@ -669,12 +668,10 @@ int kprobe_int3_handler(struct pt_regs *regs)
 
 	addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
 	/*
-	 * We don't want to be preempted for the entire
-	 * duration of kprobe processing. We conditionally
-	 * re-enable preemption at the end of this function,
-	 * and also in reenter_kprobe() and setup_singlestep().
+	 * We don't want to be preempted for the entire duration of kprobe
+	 * processing. Since int3 and debug trap disables irqs and we clear
+	 * IF while singlestepping, it must be no preemptible.
 	 */
-	preempt_disable();
 
 	kcb = get_kprobe_ctlblk();
 	p = get_kprobe(addr);
@@ -695,10 +692,8 @@ int kprobe_int3_handler(struct pt_regs *regs)
 			 */
 			if (!p->pre_handler || !p->pre_handler(p, regs))
 				setup_singlestep(p, regs, kcb, 0);
-			else {
+			else
 				reset_current_kprobe();
-				preempt_enable_no_resched();
-			}
 			return 1;
 		}
 	} else if (*addr != BREAKPOINT_INSTRUCTION) {
@@ -712,11 +707,9 @@ int kprobe_int3_handler(struct pt_regs *regs)
 		 * the original instruction.
 		 */
 		regs->ip = (unsigned long)addr;
-		preempt_enable_no_resched();
 		return 1;
 	} /* else: not a kprobe fault; let the kernel handle it */
 
-	preempt_enable_no_resched();
 	return 0;
 }
 NOKPROBE_SYMBOL(kprobe_int3_handler);
@@ -967,8 +960,6 @@ int kprobe_debug_handler(struct pt_regs *regs)
 	}
 	reset_current_kprobe();
 out:
-	preempt_enable_no_resched();
-
 	/*
 	 * if somebody else is singlestepping across a probe point, flags
 	 * will have TF set, in which case, continue the remaining processing
@@ -1015,7 +1006,6 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
 			restore_previous_kprobe(kcb);
 		else
 			reset_current_kprobe();
-		preempt_enable_no_resched();
 	} else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
 		   kcb->kprobe_status == KPROBE_HIT_SSDONE) {
 		/*
arch/x86/kernel/kprobes/opt.c +0 -1
index 203d398802a3..eaf02f2e7300 100644
@@ -491,7 +491,6 @@ int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter)
 		regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
 		if (!reenter)
 			reset_current_kprobe();
-		preempt_enable_no_resched();
 		return 1;
 	}
 	return 0;
#14 lore raw
In reply to Masami Hiramatsu
Quoted 3 lines · Masami Hiramatsu

x86: kprobes: Remove jprobe implementation
x86: kprobes: Ignore break_handler
x86: kprobes: Do not disable preempt on int3 path

So this is another annoyance. Do you ever compare the changelogs and titles you write with the ones I actually commit? They are almost never the same, for example the titles to x86 level kprobes code are always trying to use this kind of prefix:

b664d57f39d0: kprobes/x86: Remove IRQ disabling from jprobe handlers
ee213fc72fd6: kprobes/x86: Set up frame pointer in kprobe trampoline
a19b2e3d7839: kprobes/x86: Remove IRQ disabling from ftrace-based/optimized kprobes 5bb4fc2d8641: kprobes/x86: Disable preemption in ftrace-based jprobes 9a09f261a4fa: kprobes/x86: Disable preemption in optprobe
cd52edad55fb: kprobes/x86: Move the get_kprobe_ctlblk() into irq-disabled block
a8976fc84b64: kprobes/x86: Remove addressof() operators

Not "x86: kprobes:" which is the wrong order anyway...

Could you please be more careful about all this in the future?

Thanks,

Ingo

author maintainer
#15 lore raw
Quoted 19 lines · On Fri, 18 May 2018 08:23:43 +0200 Ingo Molnar <[email protected]> wrote:
Quoted 3 lines · Masami Hiramatsu

x86: kprobes: Remove jprobe implementation
x86: kprobes: Ignore break_handler
x86: kprobes: Do not disable preempt on int3 path

So this is another annoyance. Do you ever compare the changelogs and titles you write with the ones I actually commit? They are almost never the same, for example the titles to x86 level kprobes code are always trying to use this kind of prefix:

b664d57f39d0: kprobes/x86: Remove IRQ disabling from jprobe handlers
ee213fc72fd6: kprobes/x86: Set up frame pointer in kprobe trampoline
a19b2e3d7839: kprobes/x86: Remove IRQ disabling from ftrace-based/optimized kprobes 5bb4fc2d8641: kprobes/x86: Disable preemption in ftrace-based jprobes 9a09f261a4fa: kprobes/x86: Disable preemption in optprobe
cd52edad55fb: kprobes/x86: Move the get_kprobe_ctlblk() into irq-disabled block
a8976fc84b64: kprobes/x86: Remove addressof() operators

Quoted 1 line

Not "x86: kprobes:" which is the wrong order anyway...

OK, I got it. And sorry for the prefix change.

Quoted 2 lines

Could you please be more careful about all this in the future?

Yes, I promise it, use kprobes/x86: prefix for kprobes x86 part.

Thank you,

Quoted 4 lines

Thanks,

Ingo

Signature · 1 line
Masami Hiramatsu <[email protected]>