mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: He Kuang <hekuang@huawei.com>
To: <acme@kernel.org>, <peterz@infradead.org>, <mingo@redhat.com>,
	<jolsa@redhat.com>, <brendan.d.gregg@gmail.com>, <ast@kernel.org>,
	<alexander.shishkin@linux.intel.com>, <wangnan0@huawei.com>,
	<hekuang@huawei.com>
Cc: <linux-kernel@vger.kernel.org>
Subject: [RFC PATCH v2 15/26] perf bpf: Introduce the entity and engine for userspace bpf
Date: Sun, 26 Jun 2016 11:21:07 +0000	[thread overview]
Message-ID: <1466940078-65581-16-git-send-email-hekuang@huawei.com> (raw)
In-Reply-To: <1466940078-65581-1-git-send-email-hekuang@huawei.com>

Different from bpf for kernel which is loaded into kernel and
represented by the fd handle returned, userspace bpf prgrams should be
loaded each time we run it. The field 'insns' is stored in ubpf entry
since the vm execute interface __bpf_prog_run() needs that to run the
bpf prog.

Signed-off-by: He Kuang <hekuang@huawei.com>
Signed-off-by: Wang Nan <wangnan0@huawei.com>
---
 tools/lib/bpf/Build         |  1 +
 tools/lib/bpf/engine-ubpf.c | 97 +++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.c      |  1 +
 tools/lib/bpf/libbpf.h      |  6 +++
 4 files changed, 105 insertions(+)
 create mode 100644 tools/lib/bpf/engine-ubpf.c

diff --git a/tools/lib/bpf/Build b/tools/lib/bpf/Build
index 567dcfd..2f7e64c 100644
--- a/tools/lib/bpf/Build
+++ b/tools/lib/bpf/Build
@@ -1,2 +1,3 @@
 libbpf-y := libbpf.o bpf.o
 libbpf-y += engine-kbpf.o
+libbpf-$(CONFIG_UBPF) += engine-ubpf.o
diff --git a/tools/lib/bpf/engine-ubpf.c b/tools/lib/bpf/engine-ubpf.c
new file mode 100644
index 0000000..0ab3310
--- /dev/null
+++ b/tools/lib/bpf/engine-ubpf.c
@@ -0,0 +1,97 @@
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <linux/bpf.h>
+
+#include "bpf.h"
+#include "libbpf-internal.h"
+
+static int engine__init(struct bpf_program *prog)
+{
+	struct ubpf_entry *instances_entries;
+	int nr_instances = prog->instances.nr;
+
+	instances_entries = malloc(sizeof(struct ubpf_entry) * nr_instances);
+	if (!instances_entries) {
+		pr_warning("alloc memory failed for instances\n");
+		return -ENOMEM;
+	}
+
+	/* fill all entries with NULL */
+	memset(instances_entries, 0,
+	       sizeof(instances_entries[0]) * nr_instances);
+
+	prog->instances.entries = instances_entries;
+
+	return 0;
+}
+
+static int engine__get_nth(struct bpf_program *prog, int n, void *ret)
+{
+	struct ubpf_entry **p_vm = (struct ubpf_entry **)ret;
+
+	if (n >= prog->instances.nr || n < 0) {
+		pr_warning("Can't get the %dth vm from program %s: only %d instances\n",
+			   n, prog->section_name, prog->instances.nr);
+		return -EINVAL;
+	}
+
+	*p_vm = &((struct ubpf_entry *)prog->instances.entries)[n];
+
+	return 0;
+}
+
+static void engine__unload(struct bpf_program *prog, int index)
+{
+	struct ubpf_entry *v =
+		&((struct ubpf_entry *)prog->instances.entries)[index];
+
+	free(v->insns);
+}
+
+static int
+load_ubpf_program(struct bpf_insn *insns, int insns_cnt,
+		  struct ubpf_entry *entry)
+{
+	entry->insns = malloc(insns_cnt * sizeof(struct bpf_insn));
+	if (!entry->insns) {
+		pr_warning("Failed to create ubpf entry\n");
+		return -LIBBPF_ERRNO__LOADUBPF;
+	}
+
+	memcpy(entry->insns, &insns->code, insns_cnt * sizeof(struct bpf_insn));
+
+	return 0;
+}
+
+static int engine__load(struct bpf_program *prog, struct bpf_insn *insns,
+			int insns_cnt, char *license __maybe_unused,
+			u32 kern_version __maybe_unused, int index)
+{
+	int err = 0;
+	struct ubpf_entry entry;
+
+	if (!insns || !insns_cnt) {
+		((struct ubpf_entry **)prog->instances.entries)[index] = NULL;
+		pr_debug("Skip loading the %dth instance of program '%s'\n",
+			 index, prog->section_name);
+		return err;
+	}
+
+	err = load_ubpf_program(insns, insns_cnt, &entry);
+	if (!err)
+		((struct ubpf_entry *)prog->instances.entries)[index] = entry;
+	else
+		pr_warning("Loading the %dth instance of program '%s' failed\n",
+			   index, prog->section_name);
+
+	return err;
+}
+
+struct bpf_engine uengine = {
+	.init		= engine__init,
+	.load		= engine__load,
+	.unload		= engine__unload,
+	.get_nth	= engine__get_nth,
+};
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index ddbba35..b738ef2 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -66,6 +66,7 @@ static const char *libbpf_strerror_table[NR_ERRNO] = {
 	[ERRCODE_OFFSET(VERIFY)]	= "Kernel verifier blocks program loading",
 	[ERRCODE_OFFSET(PROG2BIG)]	= "Program too big",
 	[ERRCODE_OFFSET(KVER)]		= "Incorrect kernel version",
+	[ERRCODE_OFFSET(LOADUBPF)]	= "Failed to load user space BPF program",
 };
 
 int libbpf_strerror(int err, char *buf, size_t size)
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 55f65de..996862f 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -26,6 +26,7 @@ enum libbpf_errno {
 	LIBBPF_ERRNO__VERIFY,	/* Kernel verifier blocks program loading */
 	LIBBPF_ERRNO__PROG2BIG,	/* Program too big */
 	LIBBPF_ERRNO__KVER,	/* Incorrect kernel version */
+	LIBBPF_ERRNO__LOADUBPF, /* Failed to load user space BPF program */
 	__LIBBPF_ERRNO__END,
 };
 
@@ -185,4 +186,9 @@ int bpf_map__set_priv(struct bpf_map *map, void *priv,
 		      bpf_map_clear_priv_t clear_priv);
 void *bpf_map__priv(struct bpf_map *map);
 
+/* The entity of ubpf program */
+struct ubpf_entry {
+	struct bpf_insn *insns;
+};
+
 #endif
-- 
1.8.5.2

  parent reply	other threads:[~2016-06-26 11:24 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-26 11:20 [RFC PATCH v2 00/26] perf tools: Support uBPF script He Kuang
2016-06-26 11:20 ` [RFC PATCH v2 01/26] tools include: Adopt byte ordering macros from byteorder/generic.h He Kuang
2016-06-26 11:20 ` [RFC PATCH v2 02/26] tools include: Fix wrong macro definitions for cpu_to_le* for big endian He Kuang
2016-06-26 11:20 ` [RFC PATCH v2 03/26] bpf: split __bpf_prog_run code into new file He Kuang
2016-06-26 11:20 ` [RFC PATCH v2 04/26] bpf: extract jmp and default handler and introduce UBPF_BUILD flag He Kuang
2016-06-26 11:20 ` [RFC PATCH v2 05/26] tools include: Sync math64.h and div64.h He Kuang
2016-06-26 21:08   ` Nilay Vaish
2016-06-27  2:21     ` Hekuang
2016-06-27 18:13       ` Arnaldo Carvalho de Melo
2016-06-26 11:20 ` [RFC PATCH v2 06/26] tools include: Add (atomic|atomic64)_add implementation from the kernel sources He Kuang
2016-06-26 11:20 ` [RFC PATCH v2 07/26] perf bpf: Add map related BPF helper He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 08/26] perf bpf: Add UBPF flags and makefile options He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 09/26] perf bpf: Implement empty instruction handler and build bpf-vm He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 10/26] perf bpf: Remove unused code in libbpf He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 11/26] perf bpf: Store arbitrary entries instread fd array in bpf_program He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 12/26] perf bpf: Add libbpf-internal.h header file He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 13/26] perf bpf: Add abstraction for bpf program methods He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 14/26] perf bpf: Add -Wextra to cflags for more warnings and fix them He Kuang
2016-06-26 11:21 ` He Kuang [this message]
2016-06-26 11:21 ` [RFC PATCH v2 16/26] perf bpf: Add method for fetching nth ubpf vm He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 17/26] perf bpf: Add methods to set/check ubpf engine for bpf programs He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 18/26] perf bpf: Add ubpf helper function slots and set/get methods He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 19/26] perf tools: Register basic uBPF helpers He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 20/26] perf bpf: Accept uBPF programs He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 21/26] bpf: Support bpf load/store boundary check for ubpf He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 22/26] perf bpf: Implement boundary check code in ubpf He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 23/26] perf record: Add uBPF hooks at beginning and end of perf record He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 24/26] perf bpf: Fillup bpf jmp_call handler He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 25/26] perf bpf: Implement run_ubpf_program He Kuang
2016-06-26 11:21 ` [RFC PATCH v2 26/26] perf tests: Add uBPF test case He Kuang
2016-06-26 20:48 ` [RFC PATCH v2 00/26] perf tools: Support uBPF script Alexei Starovoitov
2016-06-27  2:10   ` Hekuang
2016-06-28 11:47   ` Hekuang
2016-06-28 14:57     ` Alexei Starovoitov
2016-06-29 10:15       ` Hekuang
2016-06-29 10:35         ` Wangnan (F)
2016-06-29 12:37           ` Alexei Starovoitov
2016-06-29 13:03             ` pi3orama

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=1466940078-65581-16-git-send-email-hekuang@huawei.com \
    --to=hekuang@huawei.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=ast@kernel.org \
    --cc=brendan.d.gregg@gmail.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.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®