mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: He Kuang <hekuang@huawei.com>
To: <ast@plumgrid.com>, <rostedt@goodmis.org>,
	<masami.hiramatsu.pt@hitachi.com>, <mingo@redhat.com>,
	<acme@redhat.com>, <a.p.zijlstra@chello.nl>, <jolsa@kernel.org>,
	<namhyung@kernel.org>
Cc: <wangnan0@huawei.com>, <linux-kernel@vger.kernel.org>
Subject: [RFC PATCH 3/5] tracing/kprobe: Separate inc recursion count out of perf_trace_buf_prepare
Date: Wed, 1 Jul 2015 02:57:33 +0000	[thread overview]
Message-ID: <1435719455-91155-4-git-send-email-hekuang@huawei.com> (raw)
In-Reply-To: <1435719455-91155-1-git-send-email-hekuang@huawei.com>

Inside perf_trace_buf_prepare(), a recursion count is increased, the
count was first introduced by commit 444a2a3bcd6d ("tracing,
perf_events: Protect the buffer from recursion in perf") to protect
the percpu data buffer from being overwritten.

For future patch to enable eBPF saving data into perf extra trace
buffer and prevent data buffer being filled recursively, the recursion
count is increased outside before entering trace_call_bpf() and
decreased in case of error. In this condition, we use the new function
perf_trace_buf_prepare_rctx() for not increasing the recursion count a
second time.

Signed-off-by: He Kuang <hekuang@huawei.com>
---
 include/linux/ftrace_event.h    |  2 ++
 kernel/trace/trace_event_perf.c | 27 ++++++++++++++++++++++-----
 kernel/trace/trace_kprobe.c     | 28 ++++++++++++++++++++++------
 3 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/include/linux/ftrace_event.h b/include/linux/ftrace_event.h
index b6cf5a3..085d236 100644
--- a/include/linux/ftrace_event.h
+++ b/include/linux/ftrace_event.h
@@ -615,6 +615,8 @@ extern int  ftrace_profile_set_filter(struct perf_event *event, int event_id,
 extern void ftrace_profile_free_filter(struct perf_event *event);
 extern void *perf_trace_buf_prepare(int size, unsigned short type,
 				    struct pt_regs **regs, int *rctxp);
+extern void *perf_trace_buf_prepare_rctx(int size, unsigned short type,
+					 struct pt_regs **regs, int rctx);
 extern void *perf_extra_trace_buf_prepare(int size, int rctx);
 extern void *get_perf_extra_trace_buf(int rctx);
 
diff --git a/kernel/trace/trace_event_perf.c b/kernel/trace/trace_event_perf.c
index 603b3da..e683cb6 100644
--- a/kernel/trace/trace_event_perf.c
+++ b/kernel/trace/trace_event_perf.c
@@ -306,8 +306,9 @@ void *get_perf_extra_trace_buf(int rctx)
 EXPORT_SYMBOL_GPL(get_perf_extra_trace_buf);
 NOKPROBE_SYMBOL(get_perf_extra_trace_buf);
 
-void *perf_trace_buf_prepare(int size, unsigned short type,
-			     struct pt_regs **regs, int *rctxp)
+static void *__perf_trace_buf_prepare(int size, unsigned short type,
+			       struct pt_regs **regs, int *rctxp,
+			       bool update_rctx)
 {
 	struct trace_entry *entry;
 	unsigned long flags;
@@ -322,9 +323,11 @@ void *perf_trace_buf_prepare(int size, unsigned short type,
 
 	pc = preempt_count();
 
-	*rctxp = perf_swevent_get_recursion_context();
-	if (*rctxp < 0)
-		return NULL;
+	if (update_rctx) {
+		*rctxp = perf_swevent_get_recursion_context();
+		if (*rctxp < 0)
+			return NULL;
+	}
 
 	if (regs)
 		*regs = this_cpu_ptr(&__perf_regs[*rctxp]);
@@ -340,9 +343,23 @@ void *perf_trace_buf_prepare(int size, unsigned short type,
 
 	return raw_data;
 }
+
+void *perf_trace_buf_prepare(int size, unsigned short type,
+			     struct pt_regs **regs, int *rctxp)
+{
+	return __perf_trace_buf_prepare(size, type, regs, rctxp, true);
+}
 EXPORT_SYMBOL_GPL(perf_trace_buf_prepare);
 NOKPROBE_SYMBOL(perf_trace_buf_prepare);
 
+void *perf_trace_buf_prepare_rctx(int size, unsigned short type,
+			     struct pt_regs **regs, int rctx)
+{
+	return __perf_trace_buf_prepare(size, type, regs, &rctx, false);
+}
+EXPORT_SYMBOL_GPL(perf_trace_buf_prepare_rctx);
+NOKPROBE_SYMBOL(perf_trace_buf_prepare_rctx);
+
 #ifdef CONFIG_FUNCTION_TRACER
 static void
 perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip,
diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
index 5600df8..16ad88e 100644
--- a/kernel/trace/trace_kprobe.c
+++ b/kernel/trace/trace_kprobe.c
@@ -1145,22 +1145,30 @@ kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
 	if (hlist_empty(head))
 		return;
 
-	if (prog && !trace_call_bpf(prog, regs))
+	rctx = perf_swevent_get_recursion_context();
+	if (rctx < 0)
 		return;
 
+	if (prog && !trace_call_bpf(prog, regs))
+		goto out;
+
 	dsize = __get_data_size(&tk->tp, regs);
 	__size = sizeof(*entry) + tk->tp.size + dsize;
 	size = ALIGN(__size + sizeof(u32), sizeof(u64));
 	size -= sizeof(u32);
 
-	entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
+	entry = perf_trace_buf_prepare_rctx(size, call->event.type, NULL, rctx);
 	if (!entry)
-		return;
+		goto out;
 
 	entry->ip = (unsigned long)tk->rp.kp.addr;
 	memset(&entry[1], 0, dsize);
 	store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
 	perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
+
+	return;
+out:
+	perf_swevent_put_recursion_context(rctx);
 }
 NOKPROBE_SYMBOL(kprobe_perf_func);
 
@@ -1180,22 +1188,30 @@ kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
 	if (hlist_empty(head))
 		return;
 
-	if (prog && !trace_call_bpf(prog, regs))
+	rctx = perf_swevent_get_recursion_context();
+	if (rctx < 0)
 		return;
 
+	if (prog && !trace_call_bpf(prog, regs))
+		goto out;
+
 	dsize = __get_data_size(&tk->tp, regs);
 	__size = sizeof(*entry) + tk->tp.size + dsize;
 	size = ALIGN(__size + sizeof(u32), sizeof(u64));
 	size -= sizeof(u32);
 
-	entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
+	entry = perf_trace_buf_prepare_rctx(size, call->event.type, NULL, rctx);
 	if (!entry)
-		return;
+		goto out;
 
 	entry->func = (unsigned long)tk->rp.kp.addr;
 	entry->ret_ip = (unsigned long)ri->ret_addr;
 	store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
 	perf_trace_buf_submit(entry, size, rctx, 0, 1, regs, head, NULL);
+
+	return;
+out:
+	perf_swevent_put_recursion_context(rctx);
 }
 NOKPROBE_SYMBOL(kretprobe_perf_func);
 #endif	/* CONFIG_PERF_EVENTS */
-- 
1.8.5.2


  parent reply	other threads:[~2015-07-01  2:59 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-01  2:57 [RFC PATCH 0/5] Make eBPF programs output data to perf event He Kuang
2015-07-01  2:57 ` [RFC PATCH 1/5] bpf: Put perf_events check ahead of bpf prog He Kuang
2015-07-02  3:50   ` Alexei Starovoitov
2015-07-02  5:52     ` Wangnan (F)
2015-07-02 18:02       ` Alexei Starovoitov
2015-07-01  2:57 ` [RFC PATCH 2/5] perf/trace: Add perf extra percpu trace buffer He Kuang
2015-07-01  2:57 ` He Kuang [this message]
2015-07-01  2:57 ` [RFC PATCH 4/5] bpf: Introduce function for outputing sample data to perf event He Kuang
2015-07-01  2:57 ` [RFC PATCH 5/5] tracing/kprobe: Combine extra trace buf into perf trace buf He Kuang
2015-07-01  5:44 ` [RFC PATCH 0/5] Make eBPF programs output data to perf event Peter Zijlstra
2015-07-01  6:21   ` Wangnan (F)
2015-07-01 11:58     ` Peter Zijlstra
2015-07-02  2:48       ` Alexei Starovoitov
2015-07-02  3:38         ` He Kuang
2015-07-02  3:52           ` Alexei Starovoitov
2015-07-02  9:24             ` Wangnan (F)
2015-07-02 18:37               ` Alexei Starovoitov
2015-07-02  9:31         ` Peter Zijlstra
2015-07-02 13:50     ` [RFC PATCH v2 0/4] " He Kuang
2015-07-02 13:50       ` [RFC PATCH v2 1/4] bpf: Put perf_events check ahead of bpf prog He Kuang
2015-07-02 18:41         ` Alexei Starovoitov
2015-07-02 13:50       ` [RFC PATCH v2 2/4] tracing/kprobe: Separate inc recursion count out of perf_trace_buf_prepare He Kuang
2015-07-02 13:50       ` [RFC PATCH v2 3/4] bpf: Introduce function for outputing data to perf event He Kuang
2015-07-02 13:50       ` [RFC PATCH v2 4/4] tracing/kprobe: Combine bpf output and perf event output He Kuang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1435719455-91155-4-git-send-email-hekuang@huawei.com \
    --to=hekuang@huawei.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@redhat.com \
    --cc=ast@plumgrid.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=wangnan0@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®