Skip to contentSkip to conversation

Registering struct_ops types from modules.

RFC New [email protected] 1 participant 1 message Sep 7, 2023

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

RFC 1/1 Registering struct_ops types from modules.

From: Kui-Feng Lee <[email protected]>

Given the current constraints of the current implementation, struct_ops cannot be registered dynamically. This presents a significant limitation for modules like fuse-bpf, which seeks to implement a new struct_ops type. To address this issue, here it proposes the introduction of a new API. This API will enable the registering of new struct_ops types from modules.

The following code is an example of how to implement a new struct_ops type in a module with the proposed API. It adds a new type bpf_testmod_ops in the bpf_testmod module. And, call register_bpf_struct_ops() and unregister_bpf_struct_ops() when init and exit the module.

A module may forget or be unable to call unregister_bpf_struct_ops(). That may cause tragedies. So, the bpf_struct_ops subsystem should handle it automatically, as a fallback, when the module registered a struct_ops type is unloaded.

---
 kernel/bpf/btf.c                              |  4 ++
 kernel/bpf/sysfs_btf.c                        |  1 +
 kernel/module/main.c                          |  7 +++
 tools/lib/bpf/btf.c                           |  2 +
 tools/lib/bpf/libbpf.c                        | 17 ++++++
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   | 58 +++++++++++++++++++
 .../selftests/bpf/bpf_testmod/bpf_testmod.h   |  5 ++
 .../bpf/prog_tests/test_struct_ops_module.c   | 49 ++++++++++++++++
 .../selftests/bpf/progs/struct_ops_module.c   | 27 +++++++++
 9 files changed, 170 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_module.c
kernel/bpf/btf.c +4 -0
index ad8c2bbff47d..59a5372dec69 100644
@@ -7362,6 +7362,8 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
 	     op != MODULE_STATE_GOING))
 		goto out;
 
+	printk(KERN_CRIT "btf_module: %s %d\n", mod->name,
+	       op);
 	switch (op) {
 	case MODULE_STATE_COMING:
 		btf_mod = kzalloc(sizeof(*btf_mod), GFP_KERNEL);
@@ -7410,6 +7412,8 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
 			attr->read = btf_module_read;
 
 			err = sysfs_create_bin_file(btf_kobj, attr);
+			printk(KERN_CRIT "sysfs btf_module: %s %d %d\n", mod->name,
+			       op, err);
 			if (err) {
 				pr_warn("failed to register module [%s] BTF in sysfs: %d\n",
 					mod->name, err);
kernel/bpf/sysfs_btf.c +1 -0
index ef6911aee3bb..7224b58e92b2 100644
@@ -17,6 +17,7 @@ btf_vmlinux_read(struct file *file, struct kobject *kobj,
 		 struct bin_attribute *bin_attr,
 		 char *buf, loff_t off, size_t len)
 {
+	printk(KERN_CRIT "BTF: vmlinux BTF name %s off %d len %d\n", bin_attr->attr.name, (int)off, (int)len);
 	memcpy(buf, __start_BTF + off, len);
 	return len;
 }
kernel/module/main.c +7 -0
index 59b1d067e528..e398036835d7 100644
@@ -348,6 +348,7 @@ struct module *find_module_all(const char *name, size_t len,
 {
 	struct module *mod;
 
+	printk(KERN_CRIT "find_module_all %s\n", name);
 	module_assert_mutex_or_preempt();
 
 	list_for_each_entry_rcu(mod, &modules, list,
@@ -2403,6 +2404,8 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)
 	if (err)
 		return ERR_PTR(err);
 
+	printk(KERN_CRIT "Module %s.\n",
+	       info->name);
 	/* Module has been copied to its final place now: return it. */
 	mod = (void *)info->sechdrs[info->index.mod].sh_addr;
 	kmemleak_load_module(mod, info);
@@ -3136,6 +3139,7 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
 		info.hdr = buf;
 		info.len = len;
 	}
+	printk(KERN_CRIT "init_module: %s\n", info.name);
 
 	return load_module(&info, uargs, flags);
 }
@@ -3163,6 +3167,8 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
 	int err;
 	struct fd f;
 
+	printk(KERN_CRIT "finit_module: fd=%d, uargs=%p, flags=%d\n",
+	       fd, uargs, flags);
 	err = may_init_module();
 	if (err)
 		return err;
@@ -3177,6 +3183,7 @@ SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
 	f = fdget(fd);
 	err = idempotent_init_module(f.file, uargs, flags);
 	fdput(f);
+	printk(KERN_CRIT "finit_module: finished err %d\n", err);
 	return err;
 }
 
tools/lib/bpf/btf.c +2 -0
index 8484b563b53d..cc892d7f3262 100644
@@ -4794,6 +4794,7 @@ struct btf *btf__load_vmlinux_btf(void)
 		btf = btf__parse(path, NULL);
 		err = libbpf_get_error(btf);
 		pr_debug("loading kernel BTF '%s': %d\n", path, err);
+		printf("loading kernel BTF '%s': %d\n", path, err);
 		if (err)
 			continue;
 
@@ -4811,6 +4812,7 @@ struct btf *btf__load_module_btf(const char *module_name, struct btf *vmlinux_bt
 	char path[80];
 
 	snprintf(path, sizeof(path), "/sys/kernel/btf/%s", module_name);
+	printf("loading module BTF '%s'\n", path);
 	return btf__parse_split(path, vmlinux_btf);
 }
 
tools/lib/bpf/libbpf.c +17 -0
index 96ff1aa4bf6a..37d2f45280d0 100644
@@ -7910,6 +7910,8 @@ static int bpf_object_prepare_struct_ops(struct bpf_object *obj)
 	return 0;
 }
 
+int turnon_kk = false;
+
 static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const char *target_btf_path)
 {
 	int err, i;
@@ -7926,13 +7928,25 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch
 		bpf_gen__init(obj->gen_loader, extra_log_level, obj->nr_programs, obj->nr_maps);
 
 	err = bpf_object__probe_loading(obj);
+	if (turnon_kk)
+		printf("bpf_object__probe_loading err=%d\n", err);
 	err = err ? : bpf_object__load_vmlinux_btf(obj, false);
+	if (turnon_kk)
+		printf("bpf_object__probe_loading err=%d\n", err);
 	err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);
 	err = err ? : bpf_object__sanitize_and_load_btf(obj);
 	err = err ? : bpf_object__sanitize_maps(obj);
+	if (turnon_kk)
+		printf("bpf_object__probe_loading err=%d\n", err);
 	err = err ? : bpf_object__init_kern_struct_ops_maps(obj);
+	if (turnon_kk)
+		printf("bpf_object__probe_loading err=%d\n", err);
 	err = err ? : bpf_object__create_maps(obj);
+	if (turnon_kk)
+		printf("bpf_object__probe_loading err=%d\n", err);
 	err = err ? : bpf_object__relocate(obj, obj->btf_custom_path ? : target_btf_path);
+	if (turnon_kk)
+		printf("bpf_object__probe_loading err=%d\n", err);
 	err = err ? : bpf_object__load_progs(obj, extra_log_level);
 	err = err ? : bpf_object_init_prog_arrays(obj);
 	err = err ? : bpf_object_prepare_struct_ops(obj);
@@ -13105,7 +13119,9 @@ int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
 {
 	int i, err;
 
+	printf("Loading BPF skeleton '%s'...\n", s->name);
 	err = bpf_object__load(*s->obj);
+	printf("bpf_object__load\n");
 	if (err) {
 		pr_warn("failed to load BPF skeleton '%s': %d\n", s->name, err);
 		return libbpf_err(err);
@@ -13150,6 +13166,7 @@ int bpf_object__load_skeleton(struct bpf_object_skeleton *s)
 		}
 	}
 
+	printf("BPF skeleton '%s' loaded successfully\n", s->name);
 	return 0;
 }
 
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +58 -0

86 lines not on this page · raw message

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +5 -0
index f32793efe095..ca5435751c79 100644
@@ -28,4 +28,9 @@ struct bpf_iter_testmod_seq {
 	int cnt;
 };
 
+struct bpf_testmod_ops {
+	int (*test_1)(void);
+	int (*test_2)(int a, int b);
+};
+
 #endif /* _BPF_TESTMOD_H */
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c new file +49 -0

49 lines not on this page · raw message

tools/testing/selftests/bpf/progs/struct_ops_module.c new file +27 -0
new file mode 100644
index 000000000000..e77d29ae29a1
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "../bpf_testmod/bpf_testmod.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops/test_1")
+int BPF_PROG(test_1)
+{
+	return 0xdeadbeef;
+}
+
+SEC("struct_ops/test_2")
+int BPF_PROG(test_2, int a, int b)
+{
+	return a + b;
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_1 = {
+	.test_1 = (void *)test_1,
+	.test_2 = (void *)test_2,
+};
+
Signature · 1 line
2.34.1