Skip to contentSkip to conversation

Update a struct_ops link through a pinned path

PATCH In review Kui-Feng Lee 3 participants 5 messages Apr 18, 2024 – Apr 25, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover Update a struct_ops link through a pinned path

Applications already have the ability to update a struct_ops link with another struct_ops map. However, they were unable to open pinned paths of the links. This implies that updating a link through its pinned paths was not feasible. By allowing the "open" operator on pinned paths, applications can pin a struct_ops link and update the link through the pinned path later.

---
Changes from v1:

- Fix a link time error for the case that CONFIG_BPF_JIT is not enabled. (Reported by kernel test robot)

v1: https://lore.kernel.org/all/[email protected]/

Kui-Feng Lee (2):
bpf: enable the "open" operator on a pinned path of a struct_osp link. selftests/bpf: open a pinned path of a struct_ops link.

 include/linux/bpf.h                           |  6 ++
 kernel/bpf/bpf_struct_ops.c                   | 10 ++++
 kernel/bpf/inode.c                            | 11 +++-
 kernel/bpf/syscall.c                          | 16 +++++-
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  6 ++
 .../bpf/prog_tests/test_struct_ops_module.c   | 56 +++++++++++++++++++
 6 files changed, 101 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 bpf: enable the "open" operator on a pinned path of a struct_osp link.

Add the "open" operator for the inodes of BPF links to allow applications to obtain a file descriptor of a struct_ops link from a pinned path.

Applications have the ability to update a struct_ops link with another struct_ops map. However, they were unable to open pinned paths of the links with this patch. This implies that updating a link through its pinned paths was not feasible.

This patch adds the "open" operator to bpf_link_ops and uses bpf_link_ops as the i_fop for inodes of struct_ops links. "open" will be called to open the pinned path represented by an inode. Additionally, bpf_link_ops will be used as the f->f_ops of the opened "file" to provide operators for the "file".

---
 include/linux/bpf.h         |  6 ++++++
 kernel/bpf/bpf_struct_ops.c | 10 ++++++++++
 kernel/bpf/inode.c          | 11 ++++++++---
 kernel/bpf/syscall.c        | 16 +++++++++++++++-
 4 files changed, 39 insertions(+), 4 deletions(-)
include/linux/bpf.h +6 -0
index 5034c1b4ded7..a0c0234d754b 100644
@@ -2160,6 +2160,12 @@ extern const struct super_operations bpf_super_ops;
 extern const struct file_operations bpf_map_fops;
 extern const struct file_operations bpf_prog_fops;
 extern const struct file_operations bpf_iter_fops;
+extern const struct file_operations bpf_link_fops;
+
+#ifdef CONFIG_BPF_JIT
+/* Required by bpf_link_open() */
+int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp);
+#endif
 
 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
 	extern const struct bpf_prog_ops _name ## _prog_ops; \
kernel/bpf/bpf_struct_ops.c +10 -0
index 86c7884abaf8..8be4f755a182 100644
@@ -1198,3 +1198,13 @@ void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map
 
 	info->btf_vmlinux_id = btf_obj_id(st_map->btf);
 }
+
+int bpffs_struct_ops_link_open(struct inode *inode, struct file *filp)
+{
+	struct bpf_struct_ops_link *link = inode->i_private;
+
+	/* Paired with bpf_link_put_direct() in bpf_link_release(). */
+	bpf_link_inc(&link->link);
+	filp->private_data = link;
+	return 0;
+}
kernel/bpf/inode.c +8 -3
index af5d2ffadd70..b020d761ab0a 100644
@@ -360,11 +360,16 @@ static int bpf_mkmap(struct dentry *dentry, umode_t mode, void *arg)
 
 static int bpf_mklink(struct dentry *dentry, umode_t mode, void *arg)
 {
+	const struct file_operations *fops;
 	struct bpf_link *link = arg;
 
-	return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops,
-			     bpf_link_is_iter(link) ?
-			     &bpf_iter_fops : &bpffs_obj_fops);
+	if (bpf_link_is_iter(link))
+		fops = &bpf_iter_fops;
+	else if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
+		fops = &bpf_link_fops;
+	else
+		fops = &bpffs_obj_fops;
+	return bpf_mkobj_ops(dentry, mode, arg, &bpf_link_iops, fops);
 }
 
 static struct dentry *
kernel/bpf/syscall.c +15 -1
index 7d392ec83655..265e2faf317d 100644
@@ -3108,7 +3108,21 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
 }
 #endif
 
-static const struct file_operations bpf_link_fops = {
+/* Support opening pinned links */
+static int bpf_link_open(struct inode *inode, struct file *filp)
+{
+#ifdef CONFIG_BPF_JIT
+	struct bpf_link *link = inode->i_private;
+
+	if (link->type == BPF_LINK_TYPE_STRUCT_OPS)
+		return bpffs_struct_ops_link_open(inode, filp);
+#endif
+
+	return -EOPNOTSUPP;
+}
+
+const struct file_operations bpf_link_fops = {
+	.open = bpf_link_open,
 #ifdef CONFIG_PROC_FS
 	.show_fdinfo	= bpf_link_show_fdinfo,
 #endif
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH bpf-next v2 1/2] bpf: enable the "open" operator on a pinned path of a struct_osp link.

Quoted 15 lines · Kui-Feng Lee

Add the "open" operator for the inodes of BPF links to allow applications to obtain a file descriptor of a struct_ops link from a pinned path.

Applications have the ability to update a struct_ops link with another struct_ops map. However, they were unable to open pinned paths of the links with this patch. This implies that updating a link through its pinned paths was not feasible.

This patch adds the "open" operator to bpf_link_ops and uses bpf_link_ops as the i_fop for inodes of struct_ops links. "open" will be called to open the pinned path represented by an inode. Additionally, bpf_link_ops will be used as the f->f_ops of the opened "file" to provide operators for the "file".

Quoted 8 lines
---
 include/linux/bpf.h         |  6 ++++++
 kernel/bpf/bpf_struct_ops.c | 10 ++++++++++
 kernel/bpf/inode.c          | 11 ++++++++---
 kernel/bpf/syscall.c        | 16 +++++++++++++++-
 4 files changed, 39 insertions(+), 4 deletions(-)

This is already supported, but you don't do it with open() syscall. bpf() syscall provides BPF_OBJ_GET as a counterpart to BPF_OBJ_PIN. So what you/your users want to do should be already supported through libbpf's bpf_obj_get()/bpf_obj_get_ops() APIs. Have you tried that?

Quoted 10 lines
include/linux/bpf.h +2 -0
index 5034c1b4ded7..a0c0234d754b 100644
@@ -2160,6 +2160,12 @@ extern const struct super_operations bpf_super_ops;
 extern const struct file_operations bpf_map_fops;
 extern const struct file_operations bpf_prog_fops;
 extern const struct file_operations bpf_iter_fops;
+extern const struct file_operations bpf_link_fops;
+

[...]

#3 lore raw

PATCH v2 2/2 selftests/bpf: open a pinned path of a struct_ops link.

In reply to Kui-Feng Lee

Ensure that a pinned path of a struct_ops link can be opened to obtain a file descriptor, which applications can then utilize to update the link.

---
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  6 ++
 .../bpf/prog_tests/test_struct_ops_module.c   | 56 +++++++++++++++++++
 2 files changed, 62 insertions(+)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +6 -0
index 39ad96a18123..c4acd4ec630c 100644
@@ -579,6 +579,11 @@ static void bpf_dummy_unreg(void *kdata)
 {
 }
 
+static int bpf_dummy_update(void *kdata, void *old_kdata)
+{
+	return bpf_dummy_reg(kdata);
+}
+
 static int bpf_testmod_test_1(void)
 {
 	return 0;
@@ -606,6 +611,7 @@ struct bpf_struct_ops bpf_bpf_testmod_ops = {
 	.init_member = bpf_testmod_ops_init_member,
 	.reg = bpf_dummy_reg,
 	.unreg = bpf_dummy_unreg,
+	.update = bpf_dummy_update,
 	.cfi_stubs = &__bpf_testmod_ops,
 	.name = "bpf_testmod_ops",
 	.owner = THIS_MODULE,
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +56 -0

67 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer Reviewed-by on the v2 series
#4 lore raw
In reply to Kui-Feng Lee
Quoted 8 lines · Kui-Feng Lee

Applications already have the ability to update a struct_ops link with another struct_ops map. However, they were unable to open pinned paths of the links. This implies that updating a link through its pinned paths was not feasible. By allowing the "open" operator on pinned paths, applications can pin a struct_ops link and update the link through the pinned path later.

---

Not an expert in how bpffs is tied together, but this patch-set seems to be fine.