Thread (45 messages) 45 messages, 7 authors, 2022-04-06
STALE1517d

[PATCH 13/18] bpf-preload: Move pinned links and maps to a dedicated directory in bpffs

From: Roberto Sassu <roberto.sassu@huawei.com>
Date: 2022-03-28 17:54:51
Also in: bpf, linux-arm-kernel, linux-doc, linux-fsdevel, linux-integrity, linux-kselftest, linux-security-module, lkml
Subsystem: bpf [general] (safe dynamic programs and tools), the rest · Maintainers: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi, Linus Torvalds

With support for preloading multiple eBPF programs, any map, link or prog
will appear in the bpf filesystem. To identify which eBPF program a pinned
object belongs to, create a subdir for each eBPF program preloaded and
place the pinned object in the new subdir.

Keep the pinned objects of iterators_bpf in the root directory of bpffs,
for compatibility reasons.

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 kernel/bpf/inode.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
index 440ea517cc29..619cdef0ba54 100644
--- a/kernel/bpf/inode.c
+++ b/kernel/bpf/inode.c
@@ -740,9 +740,30 @@ static bool bpf_preload_list_mod_get(void)
 	return ret;
 }
 
+static struct dentry *create_subdir(struct dentry *parent, const char *name)
+{
+	struct dentry *dentry;
+	int err;
+
+	inode_lock(parent->d_inode);
+	dentry = lookup_one_len(name, parent, strlen(name));
+	if (IS_ERR(dentry))
+		goto out;
+
+	err = vfs_mkdir(&init_user_ns, parent->d_inode, dentry, 0755);
+	if (err) {
+		dput(dentry);
+		dentry = ERR_PTR(err);
+	}
+out:
+	inode_unlock(parent->d_inode);
+	return dentry;
+}
+
 static int bpf_preload_list(struct dentry *parent)
 {
 	struct bpf_preload_ops_item *cur;
+	struct dentry *cur_parent;
 	int err;
 
 	if (bpf_preload_ops) {
@@ -755,7 +776,19 @@ static int bpf_preload_list(struct dentry *parent)
 		if (!cur->ops)
 			continue;
 
-		err = cur->ops->preload(parent);
+		cur_parent = parent;
+
+		if (strcmp(cur->obj_name, "bpf_preload")) {
+			cur_parent = create_subdir(parent, cur->obj_name);
+			if (IS_ERR(cur_parent))
+				cur_parent = parent;
+		}
+
+		err = cur->ops->preload(cur_parent);
+
+		if (cur_parent != parent)
+			dput(cur_parent);
+
 		if (err)
 			return err;
 	}
-- 
2.32.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help