Skip to content

Commit

Permalink
bpf: Support removing kernfs entries
Browse files Browse the repository at this point in the history
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@google.com>
  • Loading branch information
haoluo1022 authored and intel-lab-lkp committed Jan 6, 2022
1 parent fb44543 commit 2d08aaa
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions kernel/bpf/kernfs_node.c
Expand Up @@ -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)
{
Expand All @@ -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. */
Expand Down

0 comments on commit 2d08aaa

Please sign in to comment.