From: Hao Luo <haoluo@google.com>
To: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>
Cc: Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
Yonghong Song <yhs@fb.com>, KP Singh <kpsingh@kernel.org>,
Shakeel Butt <shakeelb@google.com>,
Joe@google.com, Burton@google.com, jevburton.kernel@gmail.com,
Tejun Heo <tj@kernel.org>,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
Hao Luo <haoluo@google.com>
Subject: [PATCH RESEND RFC bpf-next v1 4/8] bpf: Support removing kernfs entries
Date: Wed, 12 Jan 2022 11:25:43 -0800 [thread overview]
Message-ID: <20220112192547.3054575-5-haoluo@google.com> (raw)
In-Reply-To: <20220112192547.3054575-1-haoluo@google.com>
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>
---
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
next prev parent reply other threads:[~2022-01-12 19:26 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-12 19:25 [PATCH RESEND RFC bpf-next v1 0/8] Pinning bpf objects outside bpffs Hao Luo
2022-01-12 19:25 ` [PATCH RESEND RFC bpf-next v1 1/8] bpf: Support pinning in non-bpf file system Hao Luo
2022-01-12 19:25 ` [PATCH RESEND RFC bpf-next v1 2/8] bpf: Record back pointer to the inode in bpffs Hao Luo
2022-01-12 19:25 ` [PATCH RESEND RFC bpf-next v1 3/8] bpf: Expose bpf object in kernfs Hao Luo
2022-01-12 19:25 ` Hao Luo [this message]
2022-01-12 19:25 ` [PATCH RESEND RFC bpf-next v1 5/8] bpf: Introduce a new program type bpf_view Hao Luo
2022-01-12 19:25 ` [PATCH RESEND RFC bpf-next v1 6/8] libbpf: Support of bpf_view prog type Hao Luo
2022-01-12 19:25 ` [PATCH RESEND RFC bpf-next v1 7/8] bpf: Add seq_show operation for bpf in cgroupfs Hao Luo
2022-01-12 19:25 ` [PATCH RESEND RFC bpf-next v1 8/8] selftests/bpf: Test exposing bpf objects in kernfs Hao Luo
2022-01-12 19:31 [PATCH RESEND RFC bpf-next v1 0/8] Pinning bpf objects outside bpffs Hao Luo
2022-01-12 19:31 ` [PATCH RESEND RFC bpf-next v1 4/8] bpf: Support removing kernfs entries Hao Luo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220112192547.3054575-5-haoluo@google.com \
--to=haoluo@google.com \
--cc=Burton@google.com \
--cc=Joe@google.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=jevburton.kernel@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=shakeelb@google.com \
--cc=songliubraving@fb.com \
--cc=tj@kernel.org \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome