mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] trace/trace_uprobe: Only invoke uprobe ebpf handler when event matches.
@ 2022-11-04 13:07 David Wang
  2022-11-04 21:30 ` kernel test robot
  0 siblings, 1 reply; 3+ messages in thread
From: David Wang @ 2022-11-04 13:07 UTC (permalink / raw)
  To: linux-kernel

Only invoke uprobe ebpf handler when event matches.
    
uprobe ebpf handler would be called even the event dose not
match any registered perf event, following steps would be
used to generate a unregistered perf event.
1. register a uprobe event on a specified pid <pid-a>
2. <pid-a> invokes syscall `clone` (via pthread_create),
    new process <pid-b> generated. (Maybe it is a bug here, the
uprobe breakpoint is inherited from <pid-a>
3. <pid-b> invokes the function which is uprobed in step 1.
4. perf event generated...
    
Ebpf handler would be invoked even the event happened on <pid-b>,
but the default perf event handler make further check and ignore
the event because no registered perf event match on <pid-b>.

The patch means to fix the inconsistent behavior between ebpf and the default.
Before invoke uprobe ebpf handler, make sure current event match.
    
Signed-off-by: David Wang <00107082@163.com>
--
diff --git a/kernel/trace/trace_uprobe.c b/kernel/trace/trace_uprobe.c
index fb58e86dd117..6f13163c0c0f 100644
--- a/kernel/trace/trace_uprobe.c
+++ b/kernel/trace/trace_uprobe.c
@@ -1346,27 +1346,20 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
 	void *data;
 	int size, esize;
 	int rctx;
+	bool trace_event_match = true;
 
-#ifdef CONFIG_BPF_EVENTS
-	if (bpf_prog_array_valid(call)) {
-		u32 ret;
 
-		ret = bpf_prog_run_array_sleepable(call->prog_array, regs, bpf_prog_run);
-		if (!ret)
-			return;
+	preempt_disable();
+	head = this_cpu_ptr(call->perf_events);
+	if (hlist_empty(head)) {
+		trace_event_match = false;
+		goto out;
 	}
-#endif /* CONFIG_BPF_EVENTS */
 
 	esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
-
 	size = esize + tu->tp.size + dsize;
 	size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
 	if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
-		return;
-
-	preempt_disable();
-	head = this_cpu_ptr(call->perf_events);
-	if (hlist_empty(head))
 		goto out;
 
 	entry = perf_trace_buf_alloc(size, NULL, &rctx);
@@ -1389,11 +1382,21 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
 
 		memset(data + len, 0, size - esize - len);
 	}
-
 	perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
 			      head, NULL);
  out:
 	preempt_enable();
+
+#ifdef CONFIG_BPF_EVENTS
+	if (trace_event_match && bpf_prog_array_valid(call)) {
+		u32 ret;
+
+		ret = bpf_prog_run_array_sleepable(call->prog_array, regs, bpf_prog_run);
+		if (!ret)
+			return;
+	}
+#endif /* CONFIG_BPF_EVENTS */
+
 }
 
 /* uprobe profile handler */
--

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-11-05  6:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-04 13:07 [PATCH] trace/trace_uprobe: Only invoke uprobe ebpf handler when event matches David Wang
2022-11-04 21:30 ` kernel test robot
2022-11-05  6:38   ` David Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®