[PATCH 07/18] bpf-preload: Generate preload()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Generate preload(), to pin defined objects. For pinning, use the
bpf_obj_do_pin_kernel() function, just exported.

Signed-off-by: Roberto Sassu <roberto.sassu@xxxxxxxxxx>
---
 kernel/bpf/preload/bpf_preload_kern.c         | 24 -------
 .../bpf/preload/iterators/iterators.lskel.h   | 26 ++++++++
 tools/bpf/bpftool/gen.c                       | 64 +++++++++++++++++++
 3 files changed, 90 insertions(+), 24 deletions(-)

diff --git a/kernel/bpf/preload/bpf_preload_kern.c b/kernel/bpf/preload/bpf_preload_kern.c
index 8b49beb2b2d6..0869c889255c 100644
--- a/kernel/bpf/preload/bpf_preload_kern.c
+++ b/kernel/bpf/preload/bpf_preload_kern.c
@@ -5,30 +5,6 @@
 #include <linux/bpf_preload.h>
 #include "iterators/iterators.lskel.h"
 
-static int preload(struct dentry *parent)
-{
-	int err;
-
-	bpf_link_inc(dump_bpf_map_link);
-	bpf_link_inc(dump_bpf_prog_link);
-
-	err = bpf_obj_do_pin_kernel(parent, "maps.debug", dump_bpf_map_link,
-				    BPF_TYPE_LINK);
-	if (err)
-		goto undo;
-
-	err = bpf_obj_do_pin_kernel(parent, "progs.debug", dump_bpf_prog_link,
-				    BPF_TYPE_LINK);
-	if (err)
-		goto undo;
-
-	return 0;
-undo:
-	bpf_link_put(dump_bpf_map_link);
-	bpf_link_put(dump_bpf_prog_link);
-	return err;
-}
-
 static struct bpf_preload_ops ops = {
 	.preload = preload,
 	.owner = THIS_MODULE,
diff --git a/kernel/bpf/preload/iterators/iterators.lskel.h b/kernel/bpf/preload/iterators/iterators.lskel.h
index 4afd983ad40e..75b2e94b7547 100644
--- a/kernel/bpf/preload/iterators/iterators.lskel.h
+++ b/kernel/bpf/preload/iterators/iterators.lskel.h
@@ -448,4 +448,30 @@ static void free_objs_and_skel(void)
 	iterators_bpf__destroy(skel);
 }
 
+static int preload(struct dentry *parent)
+{
+	int err;
+
+	bpf_link_inc(dump_bpf_map_link);
+	bpf_link_inc(dump_bpf_prog_link);
+
+	err = bpf_obj_do_pin_kernel(parent, "maps.debug",
+				    dump_bpf_map_link,
+				    BPF_TYPE_LINK);
+	if (err)
+		goto undo;
+
+	err = bpf_obj_do_pin_kernel(parent, "progs.debug",
+				    dump_bpf_prog_link,
+				    BPF_TYPE_LINK);
+	if (err)
+		goto undo;
+
+	return 0;
+undo:
+	bpf_link_put(dump_bpf_map_link);
+	bpf_link_put(dump_bpf_prog_link);
+	return err;
+}
+
 #endif /* __ITERATORS_BPF_SKEL_H__ */
diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index e167aa092b7f..fa2c6022b80d 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -701,6 +701,69 @@ static void codegen_preload_free(struct bpf_object *obj, const char *obj_name)
 		", obj_name);
 }
 
+static void codegen_preload(struct bpf_object *obj, const char *obj_name)
+{
+	struct bpf_program *prog;
+	const char *link_name;
+
+	codegen("\
+		\n\
+		\n\
+		static int preload(struct dentry *parent)		    \n\
+		{							    \n\
+			int err;					    \n\
+		\n\
+		");
+
+	bpf_object__for_each_program(prog, obj) {
+		codegen("\
+			\n\
+				bpf_link_inc(%s_link);			    \n\
+			", bpf_program__name(prog));
+	}
+
+	bpf_object__for_each_program(prog, obj) {
+		link_name = bpf_program__name(prog);
+		/* These need to be hardcoded for compatibility reasons. */
+		if (!strcmp(obj_name, "iterators_bpf")) {
+			if (!strcmp(link_name, "dump_bpf_map"))
+				link_name = "maps.debug";
+			else if (!strcmp(link_name, "dump_bpf_prog"))
+				link_name = "progs.debug";
+		}
+
+		codegen("\
+			\n\
+			\n\
+				err = bpf_obj_do_pin_kernel(parent, \"%s\",	\n\
+							    %s_link,		\n\
+							    BPF_TYPE_LINK);	\n\
+				if (err)					\n\
+					goto undo;				\n\
+			", link_name, bpf_program__name(prog));
+	}
+
+	codegen("\
+		\n\
+		\n\
+			return 0;					    \n\
+		undo:							    \n\
+		");
+
+	bpf_object__for_each_program(prog, obj) {
+		codegen("\
+			\n\
+				bpf_link_put(%s_link);			    \n\
+			", bpf_program__name(prog));
+	}
+
+	codegen("\
+		\n\
+			return err;					    \n\
+		}							    \n\
+		");
+}
+
 static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *header_guard)
 {
 	DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
@@ -852,6 +915,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
 	if (gen_preload_methods) {
 		codegen_preload_vars(obj, obj_name);
 		codegen_preload_free(obj, obj_name);
+		codegen_preload(obj, obj_name);
 	}
 
 	codegen("\
-- 
2.32.0




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux Kernel]     [Linux Kernel Hardening]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux SCSI]

  Powered by Linux