[PATCH RESEND RFC bpf-next v1 4/8] bpf: Support removing kernfs entries

From: Hao Luo
Date: Wed Jan 12 2022 - 14:26:51 EST


When a bpf object has been exposed in kernfs, there should be a way
to remove it. Kernfs doesn't implement unlink, therefore one can not
remove the entry in a normal way. To remove the file, we can allow
writing a special command to the new entry, which can trigger a
remove_self() for removal.

So far there are two ways to remove an entry that is created by pinning
bpf objects in kernfs:

1. unpin the object from bpffs.
2. write a special command to the kernfs entry.

Signed-off-by: Hao Luo <haoluo@xxxxxxxxxx>
---
kernel/bpf/kernfs_node.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)

diff --git a/kernel/bpf/kernfs_node.c b/kernel/bpf/kernfs_node.c
index c1c45f7b948b..3d331d8357db 100644
--- a/kernel/bpf/kernfs_node.c
+++ b/kernel/bpf/kernfs_node.c
@@ -9,6 +9,9 @@

/* file_operations for kernfs file system */

+/* Command for removing a kernfs entry */
+#define REMOVE_CMD "rm"
+
/* Handler when the watched inode is freed. */
static void kn_watch_free_inode(void *obj, enum bpf_type type, void *kn)
{
@@ -22,8 +25,27 @@ static const struct notify_ops notify_ops = {
.free_inode = kn_watch_free_inode,
};

+static ssize_t bpf_generic_write(struct kernfs_open_file *of, char *buf,
+ size_t bytes, loff_t off)
+{
+ if (sysfs_streq(buf, REMOVE_CMD)) {
+ kernfs_remove_self(of->kn);
+ return bytes;
+ }
+
+ return -EINVAL;
+}
+
+static ssize_t bpf_generic_read(struct kernfs_open_file *of, char *buf,
+ size_t bytes, loff_t off)
+{
+ return -EIO;
+}
+
/* Kernfs file operations for bpf created files. */
static const struct kernfs_ops bpf_generic_ops = {
+ .write = bpf_generic_write,
+ .read = bpf_generic_read,
};

/* Test whether a given dentry is a kernfs entry. */
--
2.34.1.448.ga2b2bfdf31-goog