mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: yupeng0921@gmail.com
To: linux-kernel@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, rostedt@goodmis.org,
	mingo@redhat.com, yupeng0921@gmail.com
Subject: [ftrace-bpf 2/5] add bpf prog interface for ftrace
Date: Sun, 12 Nov 2017 07:28:25 +0000	[thread overview]
Message-ID: <1510471708-12729-2-git-send-email-yupeng0921@gmail.com> (raw)
In-Reply-To: <1510471708-12729-1-git-send-email-yupeng0921@gmail.com>

create ftrace_bpf_filter in tracefs, to set a bpf filter, write a bpf
prog file descriptor to this file, to clean the bpf filter, write
empty string to this file.

Signed-off-by: yupeng0921@gmail.com
---
 include/linux/ftrace.h |  3 ++
 kernel/trace/ftrace.c  | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/trace/trace.c   |  6 ++++
 kernel/trace/trace.h   |  4 +++
 4 files changed, 87 insertions(+)

diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h
index e54d257..9959435 100644
--- a/include/linux/ftrace.h
+++ b/include/linux/ftrace.h
@@ -769,6 +769,9 @@ static inline void ftrace_init(void) { }
 struct ftrace_graph_ent {
 	unsigned long func; /* Current function */
 	int depth;
+#ifdef FTRACE_BPF_FILTER
+	struct ftrace_regs *ctx;
+#endif
 } __packed;
 
 /*
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 8319e09..acb73f6 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -33,6 +33,7 @@
 #include <linux/list.h>
 #include <linux/hash.h>
 #include <linux/rcupdate.h>
+#include <linux/bpf.h>
 
 #include <trace/events/sched.h>
 
@@ -6415,10 +6416,83 @@ static const struct file_operations ftrace_pid_fops = {
 	.release	= ftrace_pid_release,
 };
 
+#ifdef FTRACE_BPF_FILTER
+static int
+ftrace_bpf_open(struct inode *inode, struct file *file)
+{
+	struct trace_array *tr = inode->i_private;
+
+	if (trace_array_get(tr) < 0)
+		return -ENODEV;
+
+	file->private_data = tr;
+
+	return 0;
+}
+
+static int
+ftrace_bpf_release(struct inode *inode, struct file *file)
+{
+	struct trace_array *tr = inode->i_private;
+
+	trace_array_put(tr);
+	return 0;
+}
+
+static ssize_t
+ftrace_bpf_write(struct file *filp, const char __user *ubuf,
+		   size_t cnt, loff_t *ppos)
+{
+	struct trace_array *tr = filp->private_data;
+	unsigned long prog_fd;
+	struct bpf_prog *prog, *old_prog;
+	ssize_t ret = 0;
+
+	old_prog = NULL;
+	mutex_lock(&ftrace_lock);
+	if (!cnt) {
+		old_prog = tr->prog;
+		goto out;
+	}
+	ret = kstrtoul_from_user(ubuf, cnt, 10, &prog_fd);
+	if (ret)
+		goto out;
+
+	prog = bpf_prog_get(prog_fd);
+	if (IS_ERR(prog)) {
+		ret = PTR_ERR(prog);
+		goto out;
+	}
+
+	old_prog = tr->prog;
+	rcu_assign_pointer(tr->prog, prog);
+
+ out:
+	mutex_unlock(&ftrace_lock);
+	if (old_prog) {
+		synchronize_rcu();
+		bpf_prog_put(old_prog);
+	}
+	if (ret)
+		return ret;
+
+	return cnt;
+}
+
+static const struct file_operations ftrace_bpf_fops = {
+	.open		= ftrace_bpf_open,
+	.write		= ftrace_bpf_write,
+	.release	= ftrace_bpf_release,
+};
+#endif	/* FTRACE_BPF_FILTER */
 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
 {
 	trace_create_file("set_ftrace_pid", 0644, d_tracer,
 			    tr, &ftrace_pid_fops);
+#ifdef FTRACE_BPF_FILTER
+	trace_create_file("set_ftrace_bpf", 0644, d_tracer,
+				tr, &ftrace_bpf_fops);
+#endif
 }
 
 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 752e5da..09c75c7 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -7714,6 +7714,9 @@ static int instance_mkdir(const char *name)
 	raw_spin_lock_init(&tr->start_lock);
 
 	tr->max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
+#ifdef FTRACE_BPF_FILTER
+	tr->prog = NULL;
+#endif
 
 	tr->current_trace = &nop_trace;
 
@@ -8354,6 +8357,9 @@ __init static int tracer_alloc_buffers(void)
 	global_trace.current_trace = &nop_trace;
 
 	global_trace.max_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
+#ifdef FTRACE_BPF_FILTER
+	global_trace.prog = NULL;
+#endif
 
 	ftrace_init_global_array_ops(&global_trace);
 
diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
index 401b063..dedecd6 100644
--- a/kernel/trace/trace.h
+++ b/kernel/trace/trace.h
@@ -17,6 +17,7 @@
 #include <linux/compiler.h>
 #include <linux/trace_seq.h>
 #include <linux/glob.h>
+#include <linux/filter.h>
 
 #ifdef CONFIG_FTRACE_SYSCALLS
 #include <asm/unistd.h>		/* For NR_SYSCALLS	     */
@@ -261,6 +262,9 @@ struct trace_array {
 	struct list_head	events;
 	cpumask_var_t		tracing_cpumask; /* only trace on set CPUs */
 	int			ref;
+#ifdef FTRACE_BPF_FILTER
+	struct bpf_prog __rcu *prog;
+#endif
 #ifdef CONFIG_FUNCTION_TRACER
 	struct ftrace_ops	*ops;
 	struct trace_pid_list	__rcu *function_pids;
-- 
2.7.4

  reply	other threads:[~2017-11-12  7:28 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-12  7:28 [ftrace-bpf 1/5] add BPF_PROG_TYPE_FTRACE to bpf yupeng0921
2017-11-12  7:28 ` yupeng0921 [this message]
2017-11-12  7:28 ` [ftrace-bpf 3/5] add bpf prog filter for graph trace yupeng0921
2017-11-12  7:28 ` [ftrace-bpf 4/5] enable bpf filter for graph trace in x86-64 arch yupeng0921
2017-11-12  7:28 ` [ftrace-bpf 5/5] add BPF_PROG_TYPE_FTRACE support for samples/bpf yupeng0921
2017-11-13  1:02 ` [ftrace-bpf 1/5] add BPF_PROG_TYPE_FTRACE to bpf Alexei Starovoitov
2017-11-13  8:06   ` peng yu
2017-11-17 23:48     ` Alexei Starovoitov
2017-11-14 17:47 ` kbuild test robot
2017-11-14 21:01 ` kbuild test robot

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=1510471708-12729-2-git-send-email-yupeng0921@gmail.com \
    --to=yupeng0921@gmail.com \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    /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®