Skip to contentSkip to conversation

kprobes: x86: Remove jprobes related code

RFC PATCH In review Masami Hiramatsu 2 participants 7 messages May 8, 2018 – May 14, 2018

Subsystems (5)
Documentation x86 Tracing ftrace +1 more subsystems
lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

cover kprobes: x86: Remove jprobes related code

Hi,

This series is a core part of removing jprobes related code from generic part and x86 tree. This also simplifies the preparation code for function override by balancing preempt count and current kprobe setting inside kprobe_int3_handler and kprobe_ftrace_handler.

[1/4]: Trivial cleanup, remove a remaining "jps" word
from document.
[2/4]: Remove arch-independent jprobe code from
kernel/kprobes.c. This prevents link errors after removing arch dependent jprobe ports.
[3/4]: Remove x86 jprobe port.
[4/4]: Simplify preempt count and current kprobe setting
inside kprobe_int3_handler and kprobe_ftrace_handler.

I'm preparing similar patches for other archs. But I think this series can be (and should be) merged first, since the function override feature (which is the only in-tree component affected by this change) is implemented on x86.

Thank you,

---

Masami Hiramatsu (4):
Documentation/kprobes: Fix to remove remaining jprobe
kprobes: Remove jprobe generic code
kprobes: x86: Remove jprobe x86 port
bpf: error-inject: x86: Fix unbalanced preempt-count for function override

 Documentation/kprobes.txt        |    2 -
 arch/x86/include/asm/kprobes.h   |    3 -
 arch/x86/kernel/kprobes/core.c   |  113 ++++----------------------------------
 arch/x86/kernel/kprobes/ftrace.c |   16 +++--
 kernel/fail_function.c           |    3 -
 kernel/kprobes.c                 |   76 --------------------------
 kernel/trace/trace_kprobe.c      |   11 +---
 7 files changed, 24 insertions(+), 200 deletions(-)
Signature · 1 line
Masami Hiramatsu (Linaro) <[email protected]>
author maintainer
#1 lore raw

RFC PATCH 1/4 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

RFC PATCH 2/4 kprobes: Remove jprobe generic code

In reply to Masami Hiramatsu

Remove jprobe arch independent generic code
from kernel/kprobes.c.

---
 kernel/kprobes.c |   76 ------------------------------------------------------
 1 file changed, 76 deletions(-)
kernel/kprobes.c +0 -76
index ea619021d901..cb1c372eef62 100644
@@ -1812,77 +1812,6 @@ unsigned long __weak arch_deref_entry_point(void *entry)
 	return (unsigned long)entry;
 }
 
-#if 0
-int register_jprobes(struct jprobe **jps, int num)
-{
-	int ret = 0, i;
-
-	if (num <= 0)
-		return -EINVAL;
-
-	for (i = 0; i < num; i++) {
-		ret = register_jprobe(jps[i]);
-
-		if (ret < 0) {
-			if (i > 0)
-				unregister_jprobes(jps, i);
-			break;
-		}
-	}
-
-	return ret;
-}
-EXPORT_SYMBOL_GPL(register_jprobes);
-
-int register_jprobe(struct jprobe *jp)
-{
-	unsigned long addr, offset;
-	struct kprobe *kp = &jp->kp;
-
-	/*
-	 * Verify probepoint as well as the jprobe handler are
-	 * valid function entry points.
-	 */
-	addr = arch_deref_entry_point(jp->entry);
-
-	if (kallsyms_lookup_size_offset(addr, NULL, &offset) && offset == 0 &&
-	    kprobe_on_func_entry(kp->addr, kp->symbol_name, kp->offset)) {
-		kp->pre_handler = setjmp_pre_handler;
-		kp->break_handler = longjmp_break_handler;
-		return register_kprobe(kp);
-	}
-
-	return -EINVAL;
-}
-EXPORT_SYMBOL_GPL(register_jprobe);
-
-void unregister_jprobe(struct jprobe *jp)
-{
-	unregister_jprobes(&jp, 1);
-}
-EXPORT_SYMBOL_GPL(unregister_jprobe);
-
-void unregister_jprobes(struct jprobe **jps, int num)
-{
-	int i;
-
-	if (num <= 0)
-		return;
-	mutex_lock(&kprobe_mutex);
-	for (i = 0; i < num; i++)
-		if (__unregister_kprobe_top(&jps[i]->kp) < 0)
-			jps[i]->kp.addr = NULL;
-	mutex_unlock(&kprobe_mutex);
-
-	synchronize_sched();
-	for (i = 0; i < num; i++) {
-		if (jps[i]->kp.addr)
-			__unregister_kprobe_bottom(&jps[i]->kp);
-	}
-}
-EXPORT_SYMBOL_GPL(unregister_jprobes);
-#endif
-
 #ifdef CONFIG_KRETPROBES
 /*
  * This kprobe pre_handler is registered with every kretprobe. When probe
@@ -2329,8 +2258,6 @@ static void report_probe(struct seq_file *pi, struct kprobe *p,
 
 	if (p->pre_handler == pre_handler_kretprobe)
 		kprobe_type = "r";
-	else if (p->pre_handler == setjmp_pre_handler)
-		kprobe_type = "j";
 	else
 		kprobe_type = "k";
 
@@ -2637,6 +2564,3 @@ late_initcall(debugfs_kprobe_init);
 #endif /* CONFIG_DEBUG_FS */
 
 module_init(init_kprobes);
-
-/* defined in arch/.../kernel/kprobes.c */
-EXPORT_SYMBOL_GPL(jprobe_return);
#3 lore raw

Re: [RFC PATCH -tip 2/4] kprobes: Remove jprobe generic code

Quoted 2 lines · Masami Hiramatsu

Remove jprobe arch independent generic code
from kernel/kprobes.c.

# git grep register_jprobes
include/linux/kprobes.h:static inline int register_jprobes(struct jprobe **jps, int num)
include/linux/kprobes.h:static inline void unregister_jprobes(struct jprobe **jps, int num)
kernel/test_kprobes.c:  ret = register_jprobes(jps, 2);
kernel/test_kprobes.c:          pr_err("register_jprobes returned %d\n", ret);
kernel/test_kprobes.c:  unregister_jprobes(jps, 2);

You forgot to remove a few things ...

Thanks,

tglx

author maintainer
#4 lore raw

Re: [RFC PATCH -tip 2/4] kprobes: Remove jprobe generic code

Quoted 5 lines · On Sun, 13 May 2018 21:55:38 +0200 (CEST) Thomas Gleixner <[email protected]>…
Quoted 2 lines · Masami Hiramatsu

Remove jprobe arch independent generic code
from kernel/kprobes.c.

Quoted 8 lines
# git grep register_jprobes
include/linux/kprobes.h:static inline int register_jprobes(struct jprobe **jps, int num)
include/linux/kprobes.h:static inline void unregister_jprobes(struct jprobe **jps, int num)
kernel/test_kprobes.c:  ret = register_jprobes(jps, 2);
kernel/test_kprobes.c:          pr_err("register_jprobes returned %d\n", ret);
kernel/test_kprobes.c:  unregister_jprobes(jps, 2);

You forgot to remove a few things ...

Ah, thoes are already disabled (with #if 0) but it is better to remove it first.

Thanks!

Quoted 4 lines

Thanks,

tglx

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

RFC PATCH 3/4 kprobes: x86: Remove jprobe x86 port

In reply to Masami Hiramatsu

Remove jprobe related functions and ignore break_handler since the break_handler is used only from jprobe.

---
 arch/x86/include/asm/kprobes.h |    3 -
 arch/x86/kernel/kprobes/core.c |   94 ----------------------------------------
 2 files changed, 97 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 -94

106 lines not on this page · raw message

author maintainer
#6 lore raw

RFC PATCH 4/4 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 is implemented only on x86 at this moment.

---
 arch/x86/kernel/kprobes/core.c   |   19 +++++++++++--------
 arch/x86/kernel/kprobes/ftrace.c |   16 +++++++++-------
 kernel/fail_function.c           |    3 ---
 kernel/trace/trace_kprobe.c      |   11 +++--------
 4 files changed, 23 insertions(+), 26 deletions(-)
arch/x86/kernel/kprobes/core.c +11 -8
index 29c11c8d79ab..4bb03570b7bf 100644
@@ -683,16 +683,19 @@ int kprobe_int3_handler(struct pt_regs *regs)
 			set_current_kprobe(p, regs, kcb);
 			kcb->kprobe_status = KPROBE_HIT_ACTIVE;
 
-			/*
-			 * 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.
-			 */
 			if (!p->pre_handler || !p->pre_handler(p, regs))
 				setup_singlestep(p, regs, kcb, 0);
+			else {
+				/*
+				 * If pre_handler returns !0, this handler
+				 * modifies regs->ip and goes back to there
+				 * directly without single stepping.
+				 * So let's clear current kprobe and decrement
+				 * preempt count, which we set in this function.
+				 */
+				reset_current_kprobe();
+				preempt_enable_no_resched();
+			}
 			return 1;
 		}
 	} else if (*addr != BREAKPOINT_INSTRUCTION) {
arch/x86/kernel/kprobes/ftrace.c +9 -7
index 8dc0161cec8f..e6f0075bfefc 100644
@@ -75,18 +75,20 @@ 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. Preempt count
+			 * will be handled by ftrace correctly.
+			 */
+			__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;
 	}