[PATCH 04/18] bpf-preload: Export and call bpf_obj_do_pin_kernel()

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

 



Export bpf_obj_do_pin_kernel(), so that the preload method of an eBPF
program (built-in or in a kernel module) can call this function to pin
links, maps and progs.

This removes the need of defining the bpf_preload_info structure, to hold
the links to be pinned by populate_bpffs().

Since after this patch the preload method has everything needed to pin
objects, that makes it possible to pin arbitrary objects of eBPF programs,
by generating the corresponding preload method in the light skeleton.

Signed-off-by: Roberto Sassu <roberto.sassu@xxxxxxxxxx>
---
 include/linux/bpf_preload.h           | 20 +++++++++++++-----
 kernel/bpf/inode.c                    | 30 +++++----------------------
 kernel/bpf/preload/bpf_preload_kern.c | 25 +++++++++++++++++-----
 3 files changed, 40 insertions(+), 35 deletions(-)

diff --git a/include/linux/bpf_preload.h b/include/linux/bpf_preload.h
index 09d55d9f1131..e604933b3daa 100644
--- a/include/linux/bpf_preload.h
+++ b/include/linux/bpf_preload.h
@@ -2,19 +2,29 @@
 #ifndef _BPF_PRELOAD_H
 #define _BPF_PRELOAD_H
 
-struct bpf_preload_info {
-	char link_name[16];
-	struct bpf_link *link;
+enum bpf_type {
+	BPF_TYPE_UNSPEC	= 0,
+	BPF_TYPE_PROG,
+	BPF_TYPE_MAP,
+	BPF_TYPE_LINK,
 };
 
 struct bpf_preload_ops {
-	int (*preload)(struct bpf_preload_info *);
+	int (*preload)(struct dentry *parent);
 	struct module *owner;
 };
 
 #ifdef CONFIG_BPF_SYSCALL
 extern struct bpf_preload_ops *bpf_preload_ops;
+
+int bpf_obj_do_pin_kernel(struct dentry *parent, const char *name, void *raw,
+			  enum bpf_type type);
+#else
+static inline int bpf_obj_do_pin_kernel(struct dentry *parent, const char *name,
+					void *raw, enum bpf_type type)
+{
+	return -EOPNOTSUPP;
+}
 #endif /*CONFIG_BPF_SYSCALL*/
 
-#define BPF_PRELOAD_LINKS 2
 #endif
diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
index a9d725db4cf4..bb8762abbf3d 100644
--- a/kernel/bpf/inode.c
+++ b/kernel/bpf/inode.c
@@ -22,13 +22,6 @@
 #include <linux/bpf_trace.h>
 #include <linux/bpf_preload.h>
 
-enum bpf_type {
-	BPF_TYPE_UNSPEC	= 0,
-	BPF_TYPE_PROG,
-	BPF_TYPE_MAP,
-	BPF_TYPE_LINK,
-};
-
 static void *bpf_any_get(void *raw, enum bpf_type type)
 {
 	switch (type) {
@@ -415,9 +408,8 @@ static const struct inode_operations bpf_dir_iops = {
 };
 
 /* pin object into bpffs */
-static int bpf_obj_do_pin_kernel(struct dentry *parent,
-				 const char *name, void *raw,
-				 enum bpf_type type)
+int bpf_obj_do_pin_kernel(struct dentry *parent, const char *name, void *raw,
+			  enum bpf_type type)
 {
 	umode_t mode = S_IFREG | S_IRUSR;
 	struct dentry *dentry;
@@ -449,6 +441,7 @@ static int bpf_obj_do_pin_kernel(struct dentry *parent,
 	inode_unlock(parent->d_inode);
 	return ret;
 }
+EXPORT_SYMBOL(bpf_obj_do_pin_kernel);
 
 static int bpf_obj_do_pin(const char __user *pathname, void *raw,
 			  enum bpf_type type)
@@ -724,8 +717,7 @@ static DEFINE_MUTEX(bpf_preload_lock);
 
 static int populate_bpffs(struct dentry *parent)
 {
-	struct bpf_preload_info objs[BPF_PRELOAD_LINKS] = {};
-	int err = 0, i;
+	int err = 0;
 
 	/* grab the mutex to make sure the kernel interactions with bpf_preload
 	 * are serialized
@@ -736,19 +728,7 @@ static int populate_bpffs(struct dentry *parent)
 	if (!bpf_preload_mod_get())
 		goto out;
 
-	err = bpf_preload_ops->preload(objs);
-	if (err)
-		goto out_put;
-	for (i = 0; i < BPF_PRELOAD_LINKS; i++) {
-		bpf_link_inc(objs[i].link);
-		err = bpf_obj_do_pin_kernel(parent, objs[i].link_name,
-					    objs[i].link, BPF_TYPE_LINK);
-		if (err) {
-			bpf_link_put(objs[i].link);
-			goto out_put;
-		}
-	}
-out_put:
+	err = bpf_preload_ops->preload(parent);
 	bpf_preload_mod_put();
 out:
 	mutex_unlock(&bpf_preload_lock);
diff --git a/kernel/bpf/preload/bpf_preload_kern.c b/kernel/bpf/preload/bpf_preload_kern.c
index f43391d1c49c..d70047108bb3 100644
--- a/kernel/bpf/preload/bpf_preload_kern.c
+++ b/kernel/bpf/preload/bpf_preload_kern.c
@@ -17,13 +17,28 @@ static void free_links_and_skel(void)
 	iterators_bpf__destroy(skel);
 }
 
-static int preload(struct bpf_preload_info *obj)
+static int preload(struct dentry *parent)
 {
-	strlcpy(obj[0].link_name, "maps.debug", sizeof(obj[0].link_name));
-	obj[0].link = maps_link;
-	strlcpy(obj[1].link_name, "progs.debug", sizeof(obj[1].link_name));
-	obj[1].link = progs_link;
+	int err;
+
+	bpf_link_inc(maps_link);
+	bpf_link_inc(progs_link);
+
+	err = bpf_obj_do_pin_kernel(parent, "maps.debug", maps_link,
+				    BPF_TYPE_LINK);
+	if (err)
+		goto undo;
+
+	err = bpf_obj_do_pin_kernel(parent, "progs.debug", progs_link,
+				    BPF_TYPE_LINK);
+	if (err)
+		goto undo;
+
 	return 0;
+undo:
+	bpf_link_put(maps_link);
+	bpf_link_put(progs_link);
+	return err;
 }
 
 static struct bpf_preload_ops ops = {
-- 
2.32.0




[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux