lkml.org 
[lkml]   [2022]   [Jan]   [12]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
Subject[PATCH RESEND RFC bpf-next v1 2/8] bpf: Record back pointer to the inode in bpffs
From
When an object is pinned in bpffs, record the bpffs inode in the object.
The previous patch introduced bpf_watch_inode(), which can also be used
to watch the bpffs inode. This capability will be used in the following
patches to expose bpf objects to file systems where the nodes in the
file system are not backed by an inode.

Signed-off-by: Hao Luo <haoluo@google.com>
---
include/linux/bpf.h | 5 +++-
kernel/bpf/inode.c | 60 ++++++++++++++++++++++++++++++++++++++++++++-
kernel/bpf/inode.h | 9 +++++++
3 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 6e947cd91152..2ec693c3d6f6 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -184,7 +184,8 @@ struct bpf_map {
char name[BPF_OBJ_NAME_LEN];
bool bypass_spec_v1;
bool frozen; /* write-once; write-protected by freeze_mutex */
- /* 14 bytes hole */
+ struct inode *backing_inode; /* back pointer to the inode in bpffs */
+ /* 6 bytes hole */

/* The 3rd and 4th cacheline with misc members to avoid false sharing
* particularly with refcounting.
@@ -991,6 +992,7 @@ struct bpf_prog_aux {
struct work_struct work;
struct rcu_head rcu;
};
+ struct inode *backing_inode; /* back pointer to the inode in bpffs */
};

struct bpf_array_aux {
@@ -1018,6 +1020,7 @@ struct bpf_link {
const struct bpf_link_ops *ops;
struct bpf_prog *prog;
struct work_struct work;
+ struct inode *backing_inode; /* back pointer to the inode in bpffs */
};

struct bpf_link_ops {
diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
index b4066dd986a8..9ba10912cbf8 100644
--- a/kernel/bpf/inode.c
+++ b/kernel/bpf/inode.c
@@ -226,6 +226,57 @@ static int bpf_inode_type(const struct inode *inode, enum bpf_type *type)
return 0;
}

+/* Conditionally set an object's backing inode. */
+static void cond_set_backing_inode(void *obj, enum bpf_type type,
+ struct inode *old, struct inode *new)
+{
+ struct inode **ptr;
+
+ if (type == BPF_TYPE_PROG) {
+ struct bpf_prog *prog = obj;
+ ptr = &prog->aux->backing_inode;
+ } else if (type == BPF_TYPE_MAP) {
+ struct bpf_map *map = obj;
+ ptr = &map->backing_inode;
+ } else if (type == BPF_TYPE_LINK) {
+ struct bpf_link *link = obj;
+ ptr = &link->backing_inode;
+ } else {
+ return;
+ }
+
+ if (*ptr == old)
+ *ptr = new;
+}
+
+struct inode *get_backing_inode(void *obj, enum bpf_type type)
+{
+ struct inode *inode = NULL;
+
+ if (type == BPF_TYPE_PROG) {
+ struct bpf_prog *prog = obj;
+ inode = prog->aux->backing_inode;
+ } else if (type == BPF_TYPE_MAP) {
+ struct bpf_map *map = obj;
+ inode = map->backing_inode;
+ } else if (type == BPF_TYPE_LINK) {
+ struct bpf_link *link = obj;
+ inode = link->backing_inode;
+ }
+
+ if (!inode)
+ return NULL;
+
+ spin_lock(&inode->i_lock);
+ if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
+ spin_unlock(&inode->i_lock);
+ return NULL;
+ }
+ __iget(inode);
+ spin_unlock(&inode->i_lock);
+ return inode;
+}
+
static void bpf_dentry_finalize(struct dentry *dentry, struct inode *inode,
struct inode *dir)
{
@@ -418,6 +469,8 @@ static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw,
{
struct inode *dir = dentry->d_parent->d_inode;
struct inode *inode = bpf_get_inode(dir->i_sb, dir, mode);
+ enum bpf_type type;
+
if (IS_ERR(inode))
return PTR_ERR(inode);

@@ -425,6 +478,9 @@ static int bpf_mkobj_ops(struct dentry *dentry, umode_t mode, void *raw,
inode->i_fop = fops;
inode->i_private = raw;

+ if (!bpf_inode_type(inode, &type))
+ cond_set_backing_inode(raw, type, NULL, inode);
+
bpf_dentry_finalize(dentry, inode, dir);
return 0;
}
@@ -703,8 +759,10 @@ static void bpf_free_inode(struct inode *inode)

if (S_ISLNK(inode->i_mode))
kfree(inode->i_link);
- if (!bpf_inode_type(inode, &type))
+ if (!bpf_inode_type(inode, &type)) {
+ cond_set_backing_inode(inode->i_private, type, inode, NULL);
bpf_any_put(inode->i_private, type);
+ }
free_inode_nonrcu(inode);
}

diff --git a/kernel/bpf/inode.h b/kernel/bpf/inode.h
index 3f53a4542028..e7fe8137be80 100644
--- a/kernel/bpf/inode.h
+++ b/kernel/bpf/inode.h
@@ -30,4 +30,13 @@ int bpf_watch_inode(struct inode *inode, const struct notify_ops *ops,
}
#endif // CONFIG_FSNOTIFY

+/* Get the backing inode of a bpf object. When an object is pinned in bpf
+ * file system, an inode is associated with the object. This function returns
+ * that inode.
+ *
+ * On success, the inode is returned with refcnt incremented.
+ * On failure, NULL is returned.
+ */
+struct inode *get_backing_inode(void *obj, enum bpf_type);
+
#endif // __BPF_INODE_H_
--
2.34.1.448.ga2b2bfdf31-goog
\
 
 \ /
  Last update: 2022-01-12 20:37    [W:0.654 / U:0.444 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site