Skip to contentSkip to conversation

Registrating struct_ops types from modules

PATCH Superseded [email protected] 3 participants 39 messages Dec 9, 2023 – Dec 17, 2023

Subsystems (8)

v13 of 17 Superseded Latest is v17 →

lore
Share & tools
lore
#0 lore raw

v13 cover Registrating 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 coming fuse-bpf, which seeks to implement a new struct_ops type. To address this issue, a new API is introduced that allows the registration of new struct_ops types from modules.

Previously, struct_ops types were defined in bpf_struct_ops_types.h and collected as a static array. The new API lets callers add new struct_ops types dynamically. The static array has been removed and replaced by the per-btf struct_ops_tab.

The struct_ops subsystem relies on BTF to determine the layout of values in a struct_ops map and identify the subsystem that the struct_ops map registers to. However, the kernel BTF does not include the type information of struct_ops types defined by a module. The struct_ops subsystem requires knowledge of the corresponding module for a given struct_ops map and the utilization of BTF information from that module. We empower libbpf to determine the correct module for accessing the BTF information and pass an identity (FD) of the module btf to the kernel. The kernel looks up type information and registered struct_ops types directly from the given btf.

If a module exits while one or more struct_ops maps still refer to a struct_ops type defined by the module, it can lead to unforeseen complications. Therefore, it is crucial to ensure that a module remains intact as long as any struct_ops map is still linked to a struct_ops type defined by the module. To achieve this, every struct_ops map holds a reference to the module while being registered.

Changes from v12:

- Rebase to for-next to fix conflictions.

Changes from v11:

- bpf_struct_ops_maps hold only the refcnt to the module, but not btf. (patch 1)

- Fix warning messages. (patch 1, 9 and 10)

- Remove unnecessary conditional compiling of CONFIG_BPF_JIT. (patch 4, 9 and 10)

- Fix the commit log of the patch 7 to explain how a btf is pass from the user space and how the kernel handle it.

- bpf_struct_ops_maps hold the module defining it's type, but not btf. A map will hold the module through its life-span from allocating to being free. (patch 8)

- Change selftests and tracing __bpf_struct_ops_map_free() to wait for the release of the bpf_testmod module.

- Include btf_obj_id in bpf_map_info. (patch 14)

Changes from v10:

- Guard btf.c from CONFIG_BPF_JIT=n. This patchset has introduced symbols from bpf_struct_ops.c which is only built when CONFIG_BPF_JIT=y.

- Fix the warning of unused errout_free label by moving code that is leaked to patch 8 to patch 7.

Changes from v9:

- Remove the call_rcu_tasks_trace() changes from kern_sync_rcu().

- Trace btf_put() in the test case to ensure the release of kmod's btf, or the consequent tests may fail for using kmod's unloaded old btf instead the new one created after loading again. The kmod's btf may live for awhile after unloading the kmod, for a map being freed asynchronized is still holding the btf.

- Split "add struct_ops_tab to btf" into tow patches by adding "make struct_ops_map support btfs other than btf_vmlinux".

- Flip the order of "pass attached BTF to the bpf_struct_ops subsystem" and "hold module for bpf_struct_ops_map" to make it more reasonable.

- Fix the compile errors of a missing header file.

Changes from v8:

- Rename bpf_struct_ops_init_one() to bpf_struct_ops_desc_init().

- Move code that using BTF_ID_LIST to the newly added patch 2.

- Move code that lookup struct_ops types from a given module to the newly added patch 5.

- Store the pointers of btf at st_maps.

- Add test cases for the cases of modules being unload.

- Call bpf_struct_ops_init() in btf_add_struct_ops() to fix an inconsistent issue.

Changes from v7:

- Fix check_struct_ops_btf_id() to use attach btf if there is instead of btf_vmlinux.

Changes from v6:

- Change returned error code to -EINVAL for the case of bpf_try_get_module().

- Return an error code from bpf_struct_ops_init().

- Fix the dependency issue of testing_helpers.c and rcu_tasks_trace_gp.skel.h.

Changes from v5:

- As the 2nd patch, we introduce "bpf_struct_ops_desc". This change involves moving certain members of "bpf_struct_ops" to "bpf_struct_ops_desc", which becomes a part of "btf_struct_ops_tab". This ensures that these members remain accessible even when the owner module of a "bpf_struct_ops" is unloaded.

- Correct the order of arguments when calling
in the 3rd patch.

- Remove the owner argument from bpf_struct_ops_init_one(). Instead, callers should fill in st_ops->owner.

- Make sure to hold the owner module when calling bpf_struct_ops_find() and bpf_struct_ops_find_value() in the 6th patch.

- Merge the functions register_bpf_struct_ops_btf() and register_bpf_struct_ops() into a single function and relocate it to btf.c for better organization and clarity.

- Undo the name modifications made to find_kernel_btf_id() and find_ksym_btf_id() in the 8th patch.

Changes from v4:

- Fix the dependency between testing_helpers.o and rcu_tasks_trace_gp.skel.h.

Changes from v3:

- Fix according to the feedback for v3.

- Change of the order of arguments to make btf as the first argument.

- Use btf_try_get_module() instead of try_get_module() since the module pointed by st_ops->owner can gone while some one is still holding its btf.

- Move variables defined by BPF_STRUCT_OPS_COMMON_VALUE to struct bpf_struct_ops_common_value to validation easier.

- Register the struct_ops type defined by bpf_testmod in its init function.

- Rename field name to 'value_type_btf_obj_fd' to make it explicit.

- Fix leaking of btf objects on error.

- st_maps hold their modules to keep modules alive and prevent they from unloading.

- bpf_map of libbpf keeps mod_btf_fd instead of a pointer to module_btf.

- Do call_rcu_tasks_trace() in kern_sync_rcu() to ensure the bpf_testmod is unloaded properly. It uses rcu_tasks_trace_gp to trigger call_rcu_tasks_trace() in the kernel.

- Merge and reorder patches in a reasonable order.

Changes from v2:

- Remove struct_ops array, and add a per-btf (module) struct_ops_tab to collect registered struct_ops types.

- Validate value_type by checking member names and types.

---
v12: https://lore.kernel.org/all/[email protected]/
v11: https://lore.kernel.org/all/[email protected]/
v10: https://lore.kernel.org/all/[email protected]/
v9: https://lore.kernel.org/all/[email protected]/
v8: https://lore.kernel.org/all/[email protected]/
v7: https://lore.kernel.org/all/[email protected]/
v6: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/

Kui-Feng Lee (14):
bpf: refactory struct_ops type initialization to a function.
bpf: get type information with BPF_ID_LIST
bpf, net: introduce bpf_struct_ops_desc.
bpf: add struct_ops_tab to btf.
bpf: make struct_ops_map support btfs other than btf_vmlinux.
bpf: lookup struct_ops types from a given module BTF.
bpf: pass attached BTF to the bpf_struct_ops subsystem
bpf: hold module for bpf_struct_ops_map.
bpf: validate value_type
bpf, net: switch to dynamic registration
libbpf: Find correct module BTFs for struct_ops maps and progs.
bpf: export btf_ctx_access to modules.
selftests/bpf: test case for register_bpf_struct_ops().
bpf: pass btf object id in bpf_map_info.

 include/linux/bpf.h                           |  55 ++-
 include/linux/bpf_verifier.h                  |   1 +
 include/linux/btf.h                           |   8 +
 include/uapi/linux/bpf.h                      |   7 +-
 kernel/bpf/bpf_struct_ops.c                   | 421 ++++++++++--------
 kernel/bpf/bpf_struct_ops_types.h             |  12 -
 kernel/bpf/btf.c                              | 124 +++++-
 kernel/bpf/syscall.c                          |   4 +-
 kernel/bpf/verifier.c                         |  25 +-
 net/bpf/bpf_dummy_struct_ops.c                |  22 +-
 net/ipv4/bpf_tcp_ca.c                         |  22 +-
 tools/include/uapi/linux/bpf.h                |   7 +-
 tools/lib/bpf/bpf.c                           |   4 +-
 tools/lib/bpf/bpf.h                           |   5 +-
 tools/lib/bpf/libbpf.c                        |  38 +-
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  52 +++
 .../selftests/bpf/bpf_testmod/bpf_testmod.h   |   5 +
 .../bpf/prog_tests/test_struct_ops_module.c   |  92 ++++
 .../selftests/bpf/progs/struct_ops_module.c   |  30 ++
 .../selftests/bpf/progs/testmod_unload.c      |  25 ++
 20 files changed, 725 insertions(+), 234 deletions(-)
 delete mode 100644 kernel/bpf/bpf_struct_ops_types.h
 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
 create mode 100644 tools/testing/selftests/bpf/progs/testmod_unload.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v13 1/14 bpf: refactory struct_ops type initialization to a function.

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

Move the majority of the code to bpf_struct_ops_init_one(), which can then be utilized for the initialization of newly registered dynamically allocated struct_ops types in the following patches.

---
 include/linux/btf.h         |   1 +
 kernel/bpf/bpf_struct_ops.c | 157 +++++++++++++++++++-----------------
 kernel/bpf/btf.c            |   5 ++
 3 files changed, 89 insertions(+), 74 deletions(-)
include/linux/btf.h +1 -0
index 59d404e22814..1d852dad7473 100644
@@ -137,6 +137,7 @@ struct btf_struct_metas {
 
 extern const struct file_operations btf_fops;
 
+const char *btf_get_name(const struct btf *btf);
 void btf_get(struct btf *btf);
 void btf_put(struct btf *btf);
 int btf_new_fd(const union bpf_attr *attr, bpfptr_t uattr, u32 uattr_sz);
kernel/bpf/bpf_struct_ops.c +83 -74

185 lines not on this page · raw message

kernel/bpf/btf.c +5 -0
index 63cf4128fc05..6935a88ed190 100644
@@ -1707,6 +1707,11 @@ static void btf_free_rcu(struct rcu_head *rcu)
 	btf_free(btf);
 }
 
+const char *btf_get_name(const struct btf *btf)
+{
+	return btf->name;
+}
+
 void btf_get(struct btf *btf)
 {
 	refcount_inc(&btf->refcnt);
Signature · 1 line
2.34.1
#2 lore raw

PATCH v13 2/14 bpf: get type information with BPF_ID_LIST

In reply to [email protected]

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

Get ready to remove bpf_struct_ops_init() in the future. By using BPF_ID_LIST, it is possible to gather type information while building instead of runtime.

---
 kernel/bpf/bpf_struct_ops.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
kernel/bpf/bpf_struct_ops.c +8 -9
index 5714e7e54f9c..9f95e9fc00f3 100644
@@ -108,7 +108,12 @@ const struct bpf_prog_ops bpf_struct_ops_prog_ops = {
 #endif
 };
 
-static const struct btf_type *module_type;
+BTF_ID_LIST(st_ops_ids)
+BTF_ID(struct, module)
+
+enum {
+	IDX_MODULE_ID,
+};
 
 static void bpf_struct_ops_init_one(struct bpf_struct_ops *st_ops,
 				    struct btf *btf,
@@ -197,7 +202,6 @@ static void bpf_struct_ops_init_one(struct bpf_struct_ops *st_ops,
 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log)
 {
 	struct bpf_struct_ops *st_ops;
-	s32 module_id;
 	u32 i;
 
 	/* Ensure BTF type is emitted for "struct bpf_struct_ops_##_name" */
@@ -205,13 +209,6 @@ void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log)
 #include "bpf_struct_ops_types.h"
 #undef BPF_STRUCT_OPS_TYPE
 
-	module_id = btf_find_by_name_kind(btf, "module", BTF_KIND_STRUCT);
-	if (module_id < 0) {
-		pr_warn("Cannot find struct module in %s\n", btf_get_name(btf));
-		return;
-	}
-	module_type = btf_type_by_id(btf, module_id);
-
 	for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
 		st_ops = bpf_struct_ops[i];
 		bpf_struct_ops_init_one(st_ops, btf, log);
@@ -388,6 +385,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
 	struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
 	const struct bpf_struct_ops *st_ops = st_map->st_ops;
 	struct bpf_struct_ops_value *uvalue, *kvalue;
+	const struct btf_type *module_type;
 	const struct btf_member *member;
 	const struct btf_type *t = st_ops->type;
 	struct bpf_tramp_links *tlinks;
@@ -435,6 +433,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
 	image = st_map->image;
 	image_end = st_map->image + PAGE_SIZE;
 
+	module_type = btf_type_by_id(btf_vmlinux, st_ops_ids[IDX_MODULE_ID]);
 	for_each_member(i, t, member) {
 		const struct btf_type *mtype, *ptype;
 		struct bpf_prog *prog;
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH bpf-next v13 02/14] bpf: get type information with BPF_ID_LIST

Quoted 4 lines · On 12/8/23 4:26 PM, [email protected] wrote:

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

Get ready to remove bpf_struct_ops_init() in the future. By using BPF_ID_LIST, it is possible to gather type information while building

s/BPF_ID_LIST/BTF_ID_LIST.

The same for the patch subject.

Quoted 2 lines
kernel/bpf/bpf_struct_ops.c +2 -0
+BTF_ID_LIST(st_ops_ids)
+BTF_ID(struct, module)
#4 lore raw

PATCH v13 3/14 bpf, net: introduce bpf_struct_ops_desc.

In reply to [email protected]

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

Move some of members of bpf_struct_ops to bpf_struct_ops_desc. When we introduce the new API to register new bpf_struct_ops types from modules, bpf_struct_ops may destroyed when the module is unloaded. Moving these members to bpf_struct_ops_desc make these data available even when the module is unloaded.

type_id is unavailabe in bpf_struct_ops anymore. Modules should get it from the btf received by kmod's init function.

---
 include/linux/bpf.h            | 13 ++++--
 kernel/bpf/bpf_struct_ops.c    | 80 +++++++++++++++++-----------------
 kernel/bpf/verifier.c          |  8 ++--
 net/bpf/bpf_dummy_struct_ops.c |  9 +++-
 net/ipv4/bpf_tcp_ca.c          |  8 +++-
 5 files changed, 70 insertions(+), 48 deletions(-)
include/linux/bpf.h +9 -4
index c1a06263a4f3..fa43255f59bc 100644
@@ -1681,17 +1681,22 @@ struct bpf_struct_ops {
 	void (*unreg)(void *kdata);
 	int (*update)(void *kdata, void *old_kdata);
 	int (*validate)(void *kdata);
-	const struct btf_type *type;
-	const struct btf_type *value_type;
 	const char *name;
 	struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
+};
+
+struct bpf_struct_ops_desc {
+	struct bpf_struct_ops *st_ops;
+
+	const struct btf_type *type;
+	const struct btf_type *value_type;
 	u32 type_id;
 	u32 value_id;
 };
 
 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
-const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id);
+const struct bpf_struct_ops_desc *bpf_struct_ops_find(u32 type_id);
 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
 bool bpf_struct_ops_get(const void *kdata);
 void bpf_struct_ops_put(const void *kdata);
@@ -1734,7 +1739,7 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
 			    union bpf_attr __user *uattr);
 #endif
 #else
-static inline const struct bpf_struct_ops *bpf_struct_ops_find(u32 type_id)
+static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(u32 type_id)
 {
 	return NULL;
 }
kernel/bpf/bpf_struct_ops.c +41 -39

230 lines not on this page · raw message

kernel/bpf/verifier.c +5 -3
index fb690539d5f6..ce41bc17ac10 100644
@@ -20065,6 +20065,7 @@ static void print_verification_stats(struct bpf_verifier_env *env)
 static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 {
 	const struct btf_type *t, *func_proto;
+	const struct bpf_struct_ops_desc *st_ops_desc;
 	const struct bpf_struct_ops *st_ops;
 	const struct btf_member *member;
 	struct bpf_prog *prog = env->prog;
@@ -20077,14 +20078,15 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 	}
 
 	btf_id = prog->aux->attach_btf_id;
-	st_ops = bpf_struct_ops_find(btf_id);
-	if (!st_ops) {
+	st_ops_desc = bpf_struct_ops_find(btf_id);
+	if (!st_ops_desc) {
 		verbose(env, "attach_btf_id %u is not a supported struct\n",
 			btf_id);
 		return -ENOTSUPP;
 	}
+	st_ops = st_ops_desc->st_ops;
 
-	t = st_ops->type;
+	t = st_ops_desc->type;
 	member_idx = prog->expected_attach_type;
 	if (member_idx >= btf_type_vlen(t)) {
 		verbose(env, "attach to invalid member idx %u of struct %s\n",
net/bpf/bpf_dummy_struct_ops.c +8 -1
index 2748f9d77b18..bd753dbccaf6 100644
@@ -17,6 +17,8 @@ struct bpf_dummy_ops_test_args {
 	struct bpf_dummy_ops_state state;
 };
 
+static struct btf *bpf_dummy_ops_btf;
+
 static struct bpf_dummy_ops_test_args *
 dummy_ops_init_args(const union bpf_attr *kattr, unsigned int nr)
 {
@@ -85,9 +87,13 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
 	void *image = NULL;
 	unsigned int op_idx;
 	int prog_ret;
+	u32 type_id;
 	int err;
 
-	if (prog->aux->attach_btf_id != st_ops->type_id)
+	type_id = btf_find_by_name_kind(bpf_dummy_ops_btf,
+					bpf_bpf_dummy_ops.name,
+					BTF_KIND_STRUCT);
+	if (prog->aux->attach_btf_id != type_id)
 		return -EOPNOTSUPP;
 
 	func_proto = prog->aux->attach_func_proto;
@@ -142,6 +148,7 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
 
 static int bpf_dummy_init(struct btf *btf)
 {
+	bpf_dummy_ops_btf = btf;
 	return 0;
 }
 
net/ipv4/bpf_tcp_ca.c +7 -1
index c7bbd8f3c708..5bb56c9ad4e5 100644
@@ -20,6 +20,7 @@ static u32 unsupported_ops[] = {
 
 static const struct btf_type *tcp_sock_type;
 static u32 tcp_sock_id, sock_id;
+static const struct btf_type *tcp_congestion_ops_type;
 
 static int bpf_tcp_ca_init(struct btf *btf)
 {
@@ -36,6 +37,11 @@ static int bpf_tcp_ca_init(struct btf *btf)
 	tcp_sock_id = type_id;
 	tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
 
+	type_id = btf_find_by_name_kind(btf, "tcp_congestion_ops", BTF_KIND_STRUCT);
+	if (type_id < 0)
+		return -EINVAL;
+	tcp_congestion_ops_type = btf_type_by_id(btf, type_id);
+
 	return 0;
 }
 
@@ -149,7 +155,7 @@ static u32 prog_ops_moff(const struct bpf_prog *prog)
 	u32 midx;
 
 	midx = prog->expected_attach_type;
-	t = bpf_tcp_congestion_ops.type;
+	t = tcp_congestion_ops_type;
 	m = &btf_type_member(t)[midx];
 
 	return __btf_member_bit_offset(t, m) / 8;
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH bpf-next v13 03/14] bpf, net: introduce bpf_struct_ops_desc.

Quoted 14 lines · On 12/8/23 4:26 PM, [email protected] wrote:
net/bpf/bpf_dummy_struct_ops.c +2 -0
index 2748f9d77b18..bd753dbccaf6 100644
@@ -17,6 +17,8 @@ struct bpf_dummy_ops_test_args {
  	struct bpf_dummy_ops_state state;
  };
 
+static struct btf *bpf_dummy_ops_btf;
+
  static struct bpf_dummy_ops_test_args *
  dummy_ops_init_args(const union bpf_attr *kattr, unsigned int nr)
  {

net/bpf/bpf_dummy_struct_ops.c int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr, · line 90

Quoted 4 lines
net/bpf/bpf_dummy_struct_ops.c +1 -0
@@ -85,9 +87,13 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
  	void *image = NULL;
  	unsigned int op_idx;
  	int prog_ret;
+	u32 type_id;
	s32 type_id;

net/bpf/bpf_dummy_struct_ops.c int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr, · line 95

Quoted 6 lines
net/bpf/bpf_dummy_struct_ops.c +3 -1
  	int err;
 
-	if (prog->aux->attach_btf_id != st_ops->type_id)
+	type_id = btf_find_by_name_kind(bpf_dummy_ops_btf,
+					bpf_bpf_dummy_ops.name,
+					BTF_KIND_STRUCT);
	if (type_id < 0)
		return -EINVAL;
Quoted 45 lines
net/bpf/bpf_dummy_struct_ops.c +2 -0
+	if (prog->aux->attach_btf_id != type_id)
  		return -EOPNOTSUPP;
 
  	func_proto = prog->aux->attach_func_proto;
@@ -142,6 +148,7 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
 
  static int bpf_dummy_init(struct btf *btf)
  {
+	bpf_dummy_ops_btf = btf;
  	return 0;
  }
 
net/ipv4/bpf_tcp_ca.c +7 -1
index c7bbd8f3c708..5bb56c9ad4e5 100644
@@ -20,6 +20,7 @@ static u32 unsupported_ops[] = {
 
  static const struct btf_type *tcp_sock_type;
  static u32 tcp_sock_id, sock_id;
+static const struct btf_type *tcp_congestion_ops_type;
 
  static int bpf_tcp_ca_init(struct btf *btf)
  {
@@ -36,6 +37,11 @@ static int bpf_tcp_ca_init(struct btf *btf)
  	tcp_sock_id = type_id;
  	tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
 
+	type_id = btf_find_by_name_kind(btf, "tcp_congestion_ops", BTF_KIND_STRUCT);
+	if (type_id < 0)
+		return -EINVAL;
+	tcp_congestion_ops_type = btf_type_by_id(btf, type_id);
+
  	return 0;
  }
 
@@ -149,7 +155,7 @@ static u32 prog_ops_moff(const struct bpf_prog *prog)
  	u32 midx;
 
  	midx = prog->expected_attach_type;
-	t = bpf_tcp_congestion_ops.type;
+	t = tcp_congestion_ops_type;
  	m = &btf_type_member(t)[midx];
 
  	return __btf_member_bit_offset(t, m) / 8;
#6 lore raw

PATCH v13 4/14 bpf: add struct_ops_tab to btf.

In reply to [email protected]

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

Maintain a registry of registered struct_ops types in the per-btf (module) struct_ops_tab. This registry allows for easy lookup of struct_ops types that are registered by a specific module.

It is a preparation work for supporting kernel module struct_ops in a latter patch. Each struct_ops will be registered under its own kernel module btf and will be stored in the newly added btf->struct_ops_tab. The bpf verifier and bpf syscall (e.g. prog and map cmd) can find the struct_ops and its btf type/size/id... information from btf->struct_ops_tab.

---
 include/linux/btf.h |  5 ++++
 kernel/bpf/btf.c    | 73 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
include/linux/btf.h +5 -0
index 1d852dad7473..e2f4b85cf82a 100644
@@ -584,4 +584,9 @@ static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type
 	return btf_type_is_struct(t);
 }
 
+struct bpf_struct_ops_desc;
+
+const struct bpf_struct_ops_desc *
+btf_get_struct_ops(struct btf *btf, u32 *ret_cnt);
+
 #endif
kernel/bpf/btf.c +73 -0

99 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH bpf-next v13 04/14] bpf: add struct_ops_tab to btf.

kernel/bpf/btf.c bool btf_type_ids_nocast_alias(struct bpf_verifier_log *log, · line 8674

Quoted 5 lines · On 12/8/23 4:26 PM, [email protected] wrote:
kernel/bpf/btf.c +5 -0
+const struct bpf_struct_ops_desc *btf_get_struct_ops(struct btf *btf, u32 *ret_cnt)
+{
+	if (!btf)
+		return NULL;
+	if (!btf->struct_ops_tab)
		*ret_cnt = 0;

unless the later patch checks the return value NULL before using *ret_cnt. Anyway, better to set *ret_cnt to 0 if the btf has no struct_ops.

The same should go for the "!btf" case above but I suspect the above !btf check is unnecessary also and the caller should have checked for !btf itself instead of expecting a list of struct_ops from a NULL btf. Lets continue the review on the later patches for now to confirm where the above !btf case might happen.

Quoted 5 lines
kernel/bpf/btf.c +5 -0
+		return NULL;
+
+	*ret_cnt = btf->struct_ops_tab->cnt;
+	return (const struct bpf_struct_ops_desc *)btf->struct_ops_tab->ops;
+}
#8 lore raw

Re: [PATCH bpf-next v13 04/14] bpf: add struct_ops_tab to btf.

Quoted 10 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:26 PM, [email protected] wrote:

+const struct bpf_struct_ops_desc *btf_get_struct_ops(struct btf *btf, u32 *ret_cnt)
+{
+    if (!btf)
+        return NULL;
+    if (!btf->struct_ops_tab)

        *ret_cnt = 0;

Quoted 4 lines

unless the later patch checks the return value NULL before using *ret_cnt. Anyway, better to set *ret_cnt to 0 if the btf has no struct_ops.

The same should go for the "!btf" case above but I suspect the above !btf check is unnecessary also and the caller should have checked for !btf itself instead of expecting a list of struct_ops from a NULL btf. Lets continue the review on the later patches for now to confirm where the above !btf case might happen.

Checking callers, I didn't find anything that make btf here NULL so far. It is safe to remove !btf check. For the same reason as assigning *ret_cnt for safe, this check should be fine here as well, right?

I don't have strong opinion here. What I though is to keep the values as it is without any side-effect if the function call fails and if possible. And, the callers should not expect the callee to set some specific values when a call fails.

Quoted 7 lines
Quoted 6 lines

+        return NULL;

kernel/bpf/btf.c +4 -0
+
+    *ret_cnt = btf->struct_ops_tab->cnt;
+    return (const struct bpf_struct_ops_desc *)btf->struct_ops_tab->ops;
+}
#9 lore raw

Re: [PATCH bpf-next v13 04/14] bpf: add struct_ops_tab to btf.

Quoted 16 lines · Kui-Feng Lee
Quoted 13 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:26 PM, [email protected] wrote:

+const struct bpf_struct_ops_desc *btf_get_struct_ops(struct btf *btf, u32 *ret_cnt)
+{
+    if (!btf)
+        return NULL;
+    if (!btf->struct_ops_tab)

         *ret_cnt = 0;

unless the later patch checks the return value NULL before using *ret_cnt. Anyway, better to set *ret_cnt to 0 if the btf has no struct_ops.

Quoted 6 lines
Quoted 1 line

The same should go for the "!btf" case above but I suspect the above !btf check is unnecessary also and the caller should have checked for !btf itself instead of expecting a list of struct_ops from a NULL btf. Lets continue the review on the later patches for now to confirm where the above !btf case might happen.

Checking callers, I didn't find anything that make btf here NULL so far.

It is safe to remove !btf check. For the same reason as assigning *ret_cnt for safe, this check should be fine here as well, right?

If for safety, why ref_cnt is not checked for NULL also? The userspace passed-in btf should have been checked for NULL long time before reaching here. There is no need to be over protective here. It would really need a BUG_ON instead if btf was NULL here (don't add a BUG_ON though).

afaict, no function in btf.c is checking the btf argument for NULL also.

Quoted 5 lines

I don't have strong opinion here. What I though is to keep the values as it is without any side-effect if the function call fails and if possible. And, the callers should not expect the callee to set some specific values when a call fails.

For *ref_cnt stay uninit, there is a bug in patch 10 which exactly assumes 0 is set in *ret_cnt when there is no struct_ops. It is a good signal on how this function will be used.

I think it is arguable whether returning NULL here is failure. I would argue getting a 0 struct_ops_desc array is not a failure. It is why the !btf case confuses the return NULL case to mean a never would happen error instead of meaning there is no struct_ops. Taking out the !btf case, NULL means there is no struct_ops (instead of failure), so 0 cnt.

Anyhow, either assign 0 to *ret_cnt here, or fix patch 10 to init the local cnt 0 and write a warning comment here in btf_get_struct_ops() saying ret_cnt won't be set when there is no struct_ops in the btf.

When looking at it again, how about moving the bpf_struct_ops_find_*() to btf.c. Then it will avoid the need of the new btf_get_struct_ops() function. bpf_struct_ops_find_*() can directly use the btf->struct_ops_tab.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

+        return NULL;

kernel/bpf/btf.c +4 -0
+
+    *ret_cnt = btf->struct_ops_tab->cnt;
+    return (const struct bpf_struct_ops_desc *)btf->struct_ops_tab->ops;
+}
#10 lore raw

Re: [PATCH bpf-next v13 04/14] bpf: add struct_ops_tab to btf.

Quoted 34 lines · Martin KaFai Lau
Quoted 16 lines · Kui-Feng Lee
Quoted 13 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:26 PM, [email protected] wrote:

+const struct bpf_struct_ops_desc *btf_get_struct_ops(struct btf *btf, u32 *ret_cnt)
+{
+    if (!btf)
+        return NULL;
+    if (!btf->struct_ops_tab)

         *ret_cnt = 0;

unless the later patch checks the return value NULL before using *ret_cnt. Anyway, better to set *ret_cnt to 0 if the btf has no struct_ops.

Quoted 6 lines
Quoted 1 line

The same should go for the "!btf" case above but I suspect the above !btf check is unnecessary also and the caller should have checked for !btf itself instead of expecting a list of struct_ops from a NULL btf. Lets continue the review on the later patches for now to confirm where the above !btf case might happen.

Checking callers, I didn't find anything that make btf here NULL so far.

It is safe to remove !btf check. For the same reason as assigning *ret_cnt for safe, this check should be fine here as well, right?

If for safety, why ref_cnt is not checked for NULL also? The userspace passed-in btf should have been checked for NULL long time before reaching here. There is no need to be over protective here. It would really need a BUG_ON instead if btf was NULL here (don't add a BUG_ON though).

afaict, no function in btf.c is checking the btf argument for NULL also.

Quoted 5 lines

I don't have strong opinion here. What I though is to keep the values as it is without any side-effect if the function call fails and if possible. And, the callers should not expect the callee to set some specific values when a call fails.

Quoted 5 lines

For *ref_cnt stay uninit, there is a bug in patch 10 which exactly assumes 0 is set in *ret_cnt when there is no struct_ops. It is a good signal on how this function will be used.

I think it is arguable whether returning NULL here is failure. I would argue getting a 0 struct_ops_desc array is not a failure. It is why the !btf case confuses the return NULL case to mean a never would happen error instead of meaning there is no struct_ops. Taking out the !btf case, NULL means there is no struct_ops (instead of failure), so 0 cnt.

Anyhow, either assign 0 to *ret_cnt here, or fix patch 10 to init the local cnt 0 and write a warning comment here in btf_get_struct_ops() saying ret_cnt won't be set when there is no struct_ops in the btf.

I will fix at the patch 10 by setting local cnt 0.

Quoted 3 lines

When looking at it again, how about moving the bpf_struct_ops_find_*() to btf.c. Then it will avoid the need of the new btf_get_struct_ops() function. bpf_struct_ops_find_*() can directly use the btf->struct_ops_tab.

I prefer to keep them in bpf_struct_ops.c if it is ok to you. Fixing the initialization issue of bpf_struct_ops_find() should be enough.

Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

+        return NULL;
+
+    *ret_cnt = btf->struct_ops_tab->cnt;
+    return (const struct bpf_struct_ops_desc
*)btf->struct_ops_tab->ops;
+}

#11 lore raw

Re: [PATCH bpf-next v13 04/14] bpf: add struct_ops_tab to btf.

Quoted 47 lines · Kui-Feng Lee
Quoted 34 lines · Martin KaFai Lau
Quoted 16 lines · Kui-Feng Lee
Quoted 13 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:26 PM, [email protected] wrote:

+const struct bpf_struct_ops_desc *btf_get_struct_ops(struct btf *btf, u32 *ret_cnt)
+{
+    if (!btf)
+        return NULL;
+    if (!btf->struct_ops_tab)

         *ret_cnt = 0;

unless the later patch checks the return value NULL before using *ret_cnt. Anyway, better to set *ret_cnt to 0 if the btf has no struct_ops.

Quoted 6 lines
Quoted 1 line

The same should go for the "!btf" case above but I suspect the above !btf check is unnecessary also and the caller should have checked for !btf itself instead of expecting a list of struct_ops from a NULL btf. Lets continue the review on the later patches for now to confirm where the above !btf case might happen.

Checking callers, I didn't find anything that make btf here NULL so far.

It is safe to remove !btf check. For the same reason as assigning *ret_cnt for safe, this check should be fine here as well, right?

If for safety, why ref_cnt is not checked for NULL also? The userspace passed-in btf should have been checked for NULL long time before reaching here. There is no need to be over protective here. It would really need a BUG_ON instead if btf was NULL here (don't add a BUG_ON though).

afaict, no function in btf.c is checking the btf argument for NULL also.

Quoted 5 lines

I don't have strong opinion here. What I though is to keep the values as it is without any side-effect if the function call fails and if possible. And, the callers should not expect the callee to set some specific values when a call fails.

Quoted 5 lines

For *ref_cnt stay uninit, there is a bug in patch 10 which exactly assumes 0 is set in *ret_cnt when there is no struct_ops. It is a good signal on how this function will be used.

I think it is arguable whether returning NULL here is failure. I would argue getting a 0 struct_ops_desc array is not a failure. It is why the !btf case confuses the return NULL case to mean a never would happen error instead of meaning there is no struct_ops. Taking out the !btf case, NULL means there is no struct_ops (instead of failure), so 0 cnt.

Anyhow, either assign 0 to *ret_cnt here, or fix patch 10 to init the local cnt 0 and write a warning comment here in btf_get_struct_ops() saying ret_cnt won't be set when there is no struct_ops in the btf.

I will fix at the patch 10 by setting local cnt 0.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

When looking at it again, how about moving the bpf_struct_ops_find_*() to btf.c. Then it will avoid the need of the new btf_get_struct_ops() function. bpf_struct_ops_find_*() can directly use the btf->struct_ops_tab.

I prefer to keep them in bpf_struct_ops.c if it is ok to you. Fixing the initialization issue of bpf_struct_ops_find() should be enough.

If choosing between fixing the bug in patch 10 and moving them to btf.c, I would avoid patch 10 (and future) bug to begin with by moving them to btf.c. Moving them should be cheap unless there is other dependency that I have overlooked.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

+        return NULL;

kernel/bpf/btf.c +4 -0
+
+    *ret_cnt = btf->struct_ops_tab->cnt;
+    return (const struct bpf_struct_ops_desc *)btf->struct_ops_tab->ops;
+}
#12 lore raw

Re: [PATCH bpf-next v13 04/14] bpf: add struct_ops_tab to btf.

Quoted 48 lines · Martin KaFai Lau
Quoted 47 lines · Kui-Feng Lee
Quoted 34 lines · Martin KaFai Lau
Quoted 16 lines · Kui-Feng Lee
Quoted 13 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:26 PM, [email protected] wrote:

+const struct bpf_struct_ops_desc *btf_get_struct_ops(struct btf *btf, u32 *ret_cnt)
+{
+    if (!btf)
+        return NULL;
+    if (!btf->struct_ops_tab)

         *ret_cnt = 0;

unless the later patch checks the return value NULL before using *ret_cnt. Anyway, better to set *ret_cnt to 0 if the btf has no struct_ops.

Quoted 6 lines
Quoted 1 line

The same should go for the "!btf" case above but I suspect the above !btf check is unnecessary also and the caller should have checked for !btf itself instead of expecting a list of struct_ops from a NULL btf. Lets continue the review on the later patches for now to confirm where the above !btf case might happen.

Checking callers, I didn't find anything that make btf here NULL so far.

It is safe to remove !btf check. For the same reason as assigning *ret_cnt for safe, this check should be fine here as well, right?

If for safety, why ref_cnt is not checked for NULL also? The userspace passed-in btf should have been checked for NULL long time before reaching here. There is no need to be over protective here. It would really need a BUG_ON instead if btf was NULL here (don't add a BUG_ON though).

afaict, no function in btf.c is checking the btf argument for NULL also.

Quoted 5 lines

I don't have strong opinion here. What I though is to keep the values as it is without any side-effect if the function call fails and if possible. And, the callers should not expect the callee to set some specific values when a call fails.

Quoted 5 lines

For *ref_cnt stay uninit, there is a bug in patch 10 which exactly assumes 0 is set in *ret_cnt when there is no struct_ops. It is a good signal on how this function will be used.

I think it is arguable whether returning NULL here is failure. I would argue getting a 0 struct_ops_desc array is not a failure. It is why the !btf case confuses the return NULL case to mean a never would happen error instead of meaning there is no struct_ops. Taking out the !btf case, NULL means there is no struct_ops (instead of failure), so 0 cnt.

Anyhow, either assign 0 to *ret_cnt here, or fix patch 10 to init the local cnt 0 and write a warning comment here in btf_get_struct_ops() saying ret_cnt won't be set when there is no struct_ops in the btf.

I will fix at the patch 10 by setting local cnt 0

Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

When looking at it again, how about moving the bpf_struct_ops_find_*() to btf.c. Then it will avoid the need of the new btf_get_struct_ops() function. bpf_struct_ops_find_*() can directly use the btf->struct_ops_tab.

I prefer to keep them in bpf_struct_ops.c if it is ok to you. Fixing the initialization issue of bpf_struct_ops_find() should be enough.

If choosing between fixing the bug in patch 10 and moving them to btf.c, I would avoid patch 10 (and future) bug to begin with by moving them to btf.c. Moving them should be cheap unless there is other dependency that I have overlooked.

Ok! Got it!

Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

+        return NULL;
+
+    *ret_cnt = btf->struct_ops_tab->cnt;
+    return (const struct bpf_struct_ops_desc
*)btf->struct_ops_tab->ops;
+}

#13 lore raw

PATCH v13 5/14 bpf: make struct_ops_map support btfs other than btf_vmlinux.

In reply to [email protected]

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

Once new struct_ops can be registered from modules, btf_vmlinux is no longer the only btf that struct_ops_map would face. st_map should remember what btf it should use to get type information.

---
 kernel/bpf/bpf_struct_ops.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
kernel/bpf/bpf_struct_ops.c +15 -9
index 5fbf88e6f4e5..2b0c402740cc 100644
@@ -46,6 +46,8 @@ struct bpf_struct_ops_map {
 	 * "links[]".
 	 */
 	void *image;
+	/* The owner moduler's btf. */
+	struct btf *btf;
 	/* uvalue->data stores the kernel struct
 	 * (e.g. tcp_congestion_ops) that is more useful
 	 * to userspace than the kvalue.  For example,
@@ -314,7 +316,7 @@ static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map)
 	}
 }
 
-static int check_zero_holes(const struct btf_type *t, void *data)
+static int check_zero_holes(const struct btf *btf, const struct btf_type *t, void *data)
 {
 	const struct btf_member *member;
 	u32 i, moff, msize, prev_mend = 0;
@@ -326,8 +328,8 @@ static int check_zero_holes(const struct btf_type *t, void *data)
 		    memchr_inv(data + prev_mend, 0, moff - prev_mend))
 			return -EINVAL;
 
-		mtype = btf_type_by_id(btf_vmlinux, member->type);
-		mtype = btf_resolve_size(btf_vmlinux, mtype, &msize);
+		mtype = btf_type_by_id(btf, member->type);
+		mtype = btf_resolve_size(btf, mtype, &msize);
 		if (IS_ERR(mtype))
 			return PTR_ERR(mtype);
 		prev_mend = moff + msize;
@@ -402,12 +404,12 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
 	if (*(u32 *)key != 0)
 		return -E2BIG;
 
-	err = check_zero_holes(st_ops_desc->value_type, value);
+	err = check_zero_holes(st_map->btf, st_ops_desc->value_type, value);
 	if (err)
 		return err;
 
 	uvalue = value;
-	err = check_zero_holes(t, uvalue->data);
+	err = check_zero_holes(st_map->btf, t, uvalue->data);
 	if (err)
 		return err;
 
@@ -443,7 +445,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
 		u32 moff;
 
 		moff = __btf_member_bit_offset(t, member) / 8;
-		ptype = btf_type_resolve_ptr(btf_vmlinux, member->type, NULL);
+		ptype = btf_type_resolve_ptr(st_map->btf, member->type, NULL);
 		if (ptype == module_type) {
 			if (*(void **)(udata + moff))
 				goto reset_unlock;
@@ -468,8 +470,8 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
 		if (!ptype || !btf_type_is_func_proto(ptype)) {
 			u32 msize;
 
-			mtype = btf_type_by_id(btf_vmlinux, member->type);
-			mtype = btf_resolve_size(btf_vmlinux, mtype, &msize);
+			mtype = btf_type_by_id(st_map->btf, member->type);
+			mtype = btf_resolve_size(st_map->btf, mtype, &msize);
 			if (IS_ERR(mtype)) {
 				err = PTR_ERR(mtype);
 				goto reset_unlock;
@@ -607,6 +609,7 @@ static long bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key)
 static void bpf_struct_ops_map_seq_show_elem(struct bpf_map *map, void *key,
 					     struct seq_file *m)
 {
+	struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
 	void *value;
 	int err;
 
@@ -616,7 +619,8 @@ static void bpf_struct_ops_map_seq_show_elem(struct bpf_map *map, void *key,
 
 	err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
 	if (!err) {
-		btf_type_seq_show(btf_vmlinux, map->btf_vmlinux_value_type_id,
+		btf_type_seq_show(st_map->btf,
+				  map->btf_vmlinux_value_type_id,
 				  value, m);
 		seq_puts(m, "\n");
 	}
@@ -726,6 +730,8 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
 		return ERR_PTR(-ENOMEM);
 	}
 
+	st_map->btf = btf_vmlinux;
+
 	mutex_init(&st_map->lock);
 	bpf_map_init_from_attr(map, attr);
 
Signature · 1 line
2.34.1
#14 lore raw

PATCH v13 6/14 bpf: lookup struct_ops types from a given module BTF.

In reply to [email protected]

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

This is a preparation for searching for struct_ops types from a specified module. BTF is always btf_vmlinux now. This patch passes a pointer of BTF to bpf_struct_ops_find_value() and bpf_struct_ops_find(). Once the new registration API of struct_ops types is used, other BTFs besides btf_vmlinux can also be passed to them.

---
 include/linux/bpf.h         |  4 ++--
 kernel/bpf/bpf_struct_ops.c | 11 ++++++-----
 kernel/bpf/verifier.c       |  2 +-
 3 files changed, 9 insertions(+), 8 deletions(-)
include/linux/bpf.h +2 -2
index fa43255f59bc..91bcd62d6fcf 100644
@@ -1696,7 +1696,7 @@ struct bpf_struct_ops_desc {
 
 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
-const struct bpf_struct_ops_desc *bpf_struct_ops_find(u32 type_id);
+const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id);
 void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
 bool bpf_struct_ops_get(const void *kdata);
 void bpf_struct_ops_put(const void *kdata);
@@ -1739,7 +1739,7 @@ int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
 			    union bpf_attr __user *uattr);
 #endif
 #else
-static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(u32 type_id)
+static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id)
 {
 	return NULL;
 }
kernel/bpf/bpf_struct_ops.c +6 -5
index 2b0c402740cc..ed4d84a8437c 100644
@@ -221,11 +221,11 @@ void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log)
 extern struct btf *btf_vmlinux;
 
 static const struct bpf_struct_ops_desc *
-bpf_struct_ops_find_value(u32 value_id)
+bpf_struct_ops_find_value(struct btf *btf, u32 value_id)
 {
 	unsigned int i;
 
-	if (!value_id || !btf_vmlinux)
+	if (!value_id || !btf)
 		return NULL;
 
 	for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
@@ -236,11 +236,12 @@ bpf_struct_ops_find_value(u32 value_id)
 	return NULL;
 }
 
-const struct bpf_struct_ops_desc *bpf_struct_ops_find(u32 type_id)
+const struct bpf_struct_ops_desc *
+bpf_struct_ops_find(struct btf *btf, u32 type_id)
 {
 	unsigned int i;
 
-	if (!type_id || !btf_vmlinux)
+	if (!type_id || !btf)
 		return NULL;
 
 	for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
@@ -682,7 +683,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
 	struct bpf_map *map;
 	int ret;
 
-	st_ops_desc = bpf_struct_ops_find_value(attr->btf_vmlinux_value_type_id);
+	st_ops_desc = bpf_struct_ops_find_value(btf_vmlinux, attr->btf_vmlinux_value_type_id);
 	if (!st_ops_desc)
 		return ERR_PTR(-ENOTSUPP);
 
kernel/bpf/verifier.c +1 -1
index ce41bc17ac10..6b45f56f8d4c 100644
@@ -20078,7 +20078,7 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 	}
 
 	btf_id = prog->aux->attach_btf_id;
-	st_ops_desc = bpf_struct_ops_find(btf_id);
+	st_ops_desc = bpf_struct_ops_find(btf_vmlinux, btf_id);
 	if (!st_ops_desc) {
 		verbose(env, "attach_btf_id %u is not a supported struct\n",
 			btf_id);
Signature · 1 line
2.34.1
#15 lore raw

PATCH v13 7/14 bpf: pass attached BTF to the bpf_struct_ops subsystem

In reply to [email protected]

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

Pass the fd of a btf from the userspace to the bpf() syscall, and then convert the fd into a btf. The btf is generated from the module that defines the target BPF struct_ops type.

In order to inform the kernel about the module that defines the target struct_ops type, the userspace program needs to provide a btf fd for the respective module's btf. This btf contains essential information on the types defined within the module, including the target struct_ops type.

A btf fd must be provided to the kernel for struct_ops maps struct_ops and for the bpf programs attached to those maps.

In the case of the bpf programs, the attach_btf_obj_fd parameter is passed as part of the bpf_attr and is converted into a btf. This btf is then stored in the prog->aux->attach_btf field. Here, it just let the verifier access attach_btf directly.

In the case of struct_ops maps, a btf fd is passed as value_type_btf_obj_fd of bpf_attr. The bpf_struct_ops_map_alloc() function converts the fd to a btf and stores it as st_map->btf.

---
 include/uapi/linux/bpf.h       |  5 +++
 kernel/bpf/bpf_struct_ops.c    | 56 +++++++++++++++++++++++-----------
 kernel/bpf/syscall.c           |  2 +-
 kernel/bpf/verifier.c          |  9 ++++--
 tools/include/uapi/linux/bpf.h |  5 +++
 5 files changed, 56 insertions(+), 21 deletions(-)
include/uapi/linux/bpf.h +5 -0
index e0545201b55f..5c3838a97554 100644
@@ -1438,6 +1438,11 @@ union bpf_attr {
 		 */
 		__u64	map_extra;
 		__u32	map_token_fd;
+
+		__u32   value_type_btf_obj_fd;	/* fd pointing to a BTF
+						 * type data for
+						 * btf_vmlinux_value_type_id.
+						 */
 	};
 
 	struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
kernel/bpf/bpf_struct_ops.c +39 -17

101 lines not on this page · raw message

kernel/bpf/syscall.c +1 -1
index aff045eed375..4aced7e58904 100644
@@ -1126,7 +1126,7 @@ static bool bpf_net_capable(void)
 	return capable(CAP_NET_ADMIN) || capable(CAP_SYS_ADMIN);
 }
 
-#define BPF_MAP_CREATE_LAST_FIELD map_token_fd
+#define BPF_MAP_CREATE_LAST_FIELD value_type_btf_obj_fd
 /* called via syscall */
 static int map_create(union bpf_attr *attr)
 {
kernel/bpf/verifier.c +6 -3
index 6b45f56f8d4c..795c16f9cf57 100644
@@ -20070,6 +20070,7 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 	const struct btf_member *member;
 	struct bpf_prog *prog = env->prog;
 	u32 btf_id, member_idx;
+	struct btf *btf;
 	const char *mname;
 
 	if (!prog->gpl_compatible) {
@@ -20077,8 +20078,10 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 		return -EINVAL;
 	}
 
+	btf = prog->aux->attach_btf;
+
 	btf_id = prog->aux->attach_btf_id;
-	st_ops_desc = bpf_struct_ops_find(btf_vmlinux, btf_id);
+	st_ops_desc = bpf_struct_ops_find(btf, btf_id);
 	if (!st_ops_desc) {
 		verbose(env, "attach_btf_id %u is not a supported struct\n",
 			btf_id);
@@ -20095,8 +20098,8 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 	}
 
 	member = &btf_type_member(t)[member_idx];
-	mname = btf_name_by_offset(btf_vmlinux, member->name_off);
-	func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type,
+	mname = btf_name_by_offset(btf, member->name_off);
+	func_proto = btf_type_resolve_func_ptr(btf, member->type,
 					       NULL);
 	if (!func_proto) {
 		verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n",
tools/include/uapi/linux/bpf.h +5 -0
index e0545201b55f..5c3838a97554 100644
@@ -1438,6 +1438,11 @@ union bpf_attr {
 		 */
 		__u64	map_extra;
 		__u32	map_token_fd;
+
+		__u32   value_type_btf_obj_fd;	/* fd pointing to a BTF
+						 * type data for
+						 * btf_vmlinux_value_type_id.
+						 */
 	};
 
 	struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH bpf-next v13 07/14] bpf: pass attached BTF to the bpf_struct_ops subsystem

Quoted 5 lines · On 12/8/23 4:27 PM, [email protected] wrote:
kernel/bpf/bpf_struct_ops.c +1 -0
@@ -681,15 +682,30 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
  	struct bpf_struct_ops_map *st_map;
  	const struct btf_type *t, *vt;
  	struct bpf_map *map;
+	struct btf *btf;

kernel/bpf/bpf_struct_ops.c static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) · line 692

Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +5 -3
  	int ret;
 
-	st_ops_desc = bpf_struct_ops_find_value(btf_vmlinux, attr->btf_vmlinux_value_type_id);
-	if (!st_ops_desc)
-		return ERR_PTR(-ENOTSUPP);
+	if (attr->value_type_btf_obj_fd) {
+		/* The map holds btf for its whole life time. */
+		btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
+		if (IS_ERR(btf))
+			return ERR_PTR(PTR_ERR(btf));
			return ERR_CAST(btf);

It needs to check for btf_is_module:

		if (!btf_is_module(btf)) {
			btf_put(btf);
			return ERR_PTR(-EINVAL);
		}

kernel/bpf/bpf_struct_ops.c static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) · line 695

Quoted 3 lines
kernel/bpf/bpf_struct_ops.c +3 -0
+	} else {
+		btf = btf_vmlinux;
+		btf_get(btf);

btf_vmlinux could be NULL or a ERR_PTR? Lets take this chance to use bpf_get_btf_vmlinux():

		btf = bpf_get_btf_vmlinux();
	        if (IS_ERR(btf))
        	        return ERR_CAST(btf);

Going back to patch 4. This should be the only case that btf will be NULL or ERR_PTR?

will continue the remaining review later tonight.

pw-bot: cr

Quoted 18 lines
kernel/bpf/bpf_struct_ops.c +11 -2
+	}
+
+	st_ops_desc = bpf_struct_ops_find_value(btf, attr->btf_vmlinux_value_type_id);
+	if (!st_ops_desc) {
+		ret = -ENOTSUPP;
+		goto errout;
+	}
 
  	vt = st_ops_desc->value_type;
-	if (attr->value_size != vt->size)
-		return ERR_PTR(-EINVAL);
+	if (attr->value_size != vt->size) {
+		ret = -EINVAL;
+		goto errout;
+	}
 
  	t = st_ops_desc->type;
 
#17 lore raw

Re: [PATCH bpf-next v13 07/14] bpf: pass attached BTF to the bpf_struct_ops subsystem

Quoted 20 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:27 PM, [email protected] wrote:
kernel/bpf/bpf_struct_ops.c +1 -0
@@ -681,15 +682,30 @@ static struct bpf_map 
 *bpf_struct_ops_map_alloc(union bpf_attr *attr)
       struct bpf_struct_ops_map *st_map;
       const struct btf_type *t, *vt;
       struct bpf_map *map;
+    struct btf *btf;

kernel/bpf/bpf_struct_ops.c static struct bpf_map · line 693

Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +5 -3
       int ret;
-    st_ops_desc = bpf_struct_ops_find_value(btf_vmlinux, 
 attr->btf_vmlinux_value_type_id);
-    if (!st_ops_desc)
-        return ERR_PTR(-ENOTSUPP);
+    if (attr->value_type_btf_obj_fd) {
+        /* The map holds btf for its whole life time. */
+        btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
+        if (IS_ERR(btf))
+            return ERR_PTR(PTR_ERR(btf));

            return ERR_CAST(btf);

Quoted 6 lines

It needs to check for btf_is_module:

        if (!btf_is_module(btf)) {
            btf_put(btf);
            return ERR_PTR(-EINVAL);
        }

Even btf is btf_vmlinux the kernel's btf, it still works. Although libbpf pass 0 as the value of value_type_btf_obj_fd for btf_vmlinux now, it should be OK for a user space loader to pass a fd of btf_vmlinux.

WDYT?

Quoted 9 lines

kernel/bpf/bpf_struct_ops.c static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr) · line 695

Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +3 -0
+    } else {
+        btf = btf_vmlinux;
+        btf_get(btf);

btf_vmlinux could be NULL or a ERR_PTR? Lets take this chance to use bpf_get_btf_vmlinux():
        btf = bpf_get_btf_vmlinux();
            if (IS_ERR(btf))
                    return ERR_CAST(btf);

Sure!

Quoted 24 lines

Going back to patch 4. This should be the only case that btf will be NULL or ERR_PTR?

will continue the remaining review later tonight.

pw-bot: cr

Quoted 16 lines
kernel/bpf/bpf_struct_ops.c +11 -2
+    }
+
+    st_ops_desc = bpf_struct_ops_find_value(btf, 
 attr->btf_vmlinux_value_type_id);
+    if (!st_ops_desc) {
+        ret = -ENOTSUPP;
+        goto errout;
+    }
       vt = st_ops_desc->value_type;
-    if (attr->value_size != vt->size)
-        return ERR_PTR(-EINVAL);
+    if (attr->value_size != vt->size) {
+        ret = -EINVAL;
+        goto errout;
+    }
       t = st_ops_desc->type;
#18 lore raw

Re: [PATCH bpf-next v13 07/14] bpf: pass attached BTF to the bpf_struct_ops subsystem

Quoted 23 lines · Kui-Feng Lee
Quoted 20 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:27 PM, [email protected] wrote:
kernel/bpf/bpf_struct_ops.c +1 -0
@@ -681,15 +682,30 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union 
 bpf_attr *attr)
       struct bpf_struct_ops_map *st_map;
       const struct btf_type *t, *vt;
       struct bpf_map *map;
+    struct btf *btf;

kernel/bpf/bpf_struct_ops.c static struct bpf_map *bpf_struct_ops_map_alloc(union · line 693

Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +5 -3
       int ret;
-    st_ops_desc = bpf_struct_ops_find_value(btf_vmlinux, 
 attr->btf_vmlinux_value_type_id);
-    if (!st_ops_desc)
-        return ERR_PTR(-ENOTSUPP);
+    if (attr->value_type_btf_obj_fd) {
+        /* The map holds btf for its whole life time. */
+        btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
+        if (IS_ERR(btf))
+            return ERR_PTR(PTR_ERR(btf));

             return ERR_CAST(btf);

Quoted 8 lines
Quoted 6 lines

It needs to check for btf_is_module:

         if (!btf_is_module(btf)) {
             btf_put(btf);
             return ERR_PTR(-EINVAL);
         }

Even btf is btf_vmlinux the kernel's btf, it still works.

btf could be a bpf program's btf. It needs to ensure it is a kernel module btf here.

Quoted 5 lines

Although libbpf pass 0 as the value of value_type_btf_obj_fd for btf_vmlinux now, it should be OK for a user space loader to pass a fd of btf_vmlinux.

WDYT?

#19 lore raw

Re: [PATCH bpf-next v13 07/14] bpf: pass attached BTF to the bpf_struct_ops subsystem

Quoted 26 lines · Martin KaFai Lau
Quoted 25 lines · Kui-Feng Lee
Quoted 22 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:27 PM, [email protected] wrote:
kernel/bpf/bpf_struct_ops.c +1 -0
@@ -681,15 +682,30 @@ static struct bpf_map 
 *bpf_struct_ops_map_alloc(union bpf_attr *attr)
       struct bpf_struct_ops_map *st_map;
       const struct btf_type *t, *vt;
       struct bpf_map *map;
+    struct btf *btf;

kernel/bpf/bpf_struct_ops.c static struct bpf_map · line 693

Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +5 -3
       int ret;
-    st_ops_desc = bpf_struct_ops_find_value(btf_vmlinux, 
 attr->btf_vmlinux_value_type_id);
-    if (!st_ops_desc)
-        return ERR_PTR(-ENOTSUPP);
+    if (attr->value_type_btf_obj_fd) {
+        /* The map holds btf for its whole life time. */
+        btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
+        if (IS_ERR(btf))
+            return ERR_PTR(PTR_ERR(btf));

             return ERR_CAST(btf);

It needs to check for btf_is_module:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

         if (!btf_is_module(btf)) {
             btf_put(btf);
             return ERR_PTR(-EINVAL);
         }

Even btf is btf_vmlinux the kernel's btf, it still works.

btf could be a bpf program's btf. It needs to ensure it is a kernel module btf here.

Got it!

Quoted 7 lines
Quoted 6 lines

Although libbpf pass 0 as the value of value_type_btf_obj_fd for btf_vmlinux now, it should be OK for a user space loader to pass a fd of btf_vmlinux.

WDYT?

#20 lore raw

Re: [PATCH bpf-next v13 07/14] bpf: pass attached BTF to the bpf_struct_ops subsystem

Quoted 34 lines · Kui-Feng Lee
Quoted 31 lines · Martin KaFai Lau
Quoted 30 lines · Kui-Feng Lee
Quoted 26 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:27 PM, [email protected] wrote:
kernel/bpf/bpf_struct_ops.c +1 -0
@@ -681,15 +682,30 @@ static struct bpf_map 
 *bpf_struct_ops_map_alloc(union bpf_attr *attr)
       struct bpf_struct_ops_map *st_map;
       const struct btf_type *t, *vt;
       struct bpf_map *map;
+    struct btf *btf;

kernel/bpf/bpf_struct_ops.c static struct bpf_map · line 693

Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +5 -3
       int ret;
-    st_ops_desc = bpf_struct_ops_find_value(btf_vmlinux, 
 attr->btf_vmlinux_value_type_id);
-    if (!st_ops_desc)
-        return ERR_PTR(-ENOTSUPP);
+    if (attr->value_type_btf_obj_fd) {
+        /* The map holds btf for its whole life time. */
+        btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
+        if (IS_ERR(btf))
+            return ERR_PTR(PTR_ERR(btf));

             return ERR_CAST(btf);

It needs to check for btf_is_module:

         if (!btf_is_module(btf)) {
             btf_put(btf);
             return ERR_PTR(-EINVAL);
         }

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Even btf is btf_vmlinux the kernel's btf, it still works.

btf could be a bpf program's btf. It needs to ensure it is a kernel module btf here.

Got it!

Isn't btf_is_kernel() better here?
User space may pass a fd to btf_vmlinux.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Although libbpf pass 0 as the value of value_type_btf_obj_fd for btf_vmlinux now, it should be OK for a user space loader to pass a fd of btf_vmlinux.

WDYT?

#21 lore raw

Re: [PATCH bpf-next v13 07/14] bpf: pass attached BTF to the bpf_struct_ops subsystem

Quoted 37 lines · Kui-Feng Lee
Quoted 34 lines · Kui-Feng Lee
Quoted 31 lines · Martin KaFai Lau
Quoted 30 lines · Kui-Feng Lee
Quoted 26 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:27 PM, [email protected] wrote:
kernel/bpf/bpf_struct_ops.c +1 -0
@@ -681,15 +682,30 @@ static struct bpf_map 
 *bpf_struct_ops_map_alloc(union bpf_attr *attr)
       struct bpf_struct_ops_map *st_map;
       const struct btf_type *t, *vt;
       struct bpf_map *map;
+    struct btf *btf;

kernel/bpf/bpf_struct_ops.c static struct bpf_map · line 693

Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +5 -3
       int ret;
-    st_ops_desc = bpf_struct_ops_find_value(btf_vmlinux, 
 attr->btf_vmlinux_value_type_id);
-    if (!st_ops_desc)
-        return ERR_PTR(-ENOTSUPP);
+    if (attr->value_type_btf_obj_fd) {
+        /* The map holds btf for its whole life time. */
+        btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
+        if (IS_ERR(btf))
+            return ERR_PTR(PTR_ERR(btf));

             return ERR_CAST(btf);

It needs to check for btf_is_module:

         if (!btf_is_module(btf)) {
             btf_put(btf);
             return ERR_PTR(-EINVAL);
         }

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Even btf is btf_vmlinux the kernel's btf, it still works.

btf could be a bpf program's btf. It needs to ensure it is a kernel module btf here.

Got it!

Isn't btf_is_kernel() better here?
User space may pass a fd to btf_vmlinux.

Limit it to btf_is_module. What is the benefit of supporting btf_vmlinux as a fd while fd 0 already means btf_vmlinux?

kfunc does not support the btf_vmlinux as fd also, supporting in struct_ops alone is confusing. I don't think the major user (libbpf) does this either, so really not much usage other than a confusing API to have both fd == 0 and a btf_vmlinux's fd to mean btf_vmlinux.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

Although libbpf pass 0 as the value of value_type_btf_obj_fd for btf_vmlinux now, it should be OK for a user space loader to pass a fd of btf_vmlinux.

WDYT?

#22 lore raw

Re: [PATCH bpf-next v13 07/14] bpf: pass attached BTF to the bpf_struct_ops subsystem

Quoted 42 lines · Martin KaFai Lau
Quoted 41 lines · Kui-Feng Lee
Quoted 38 lines · Kui-Feng Lee
Quoted 34 lines · Martin KaFai Lau
Quoted 23 lines · Kui-Feng Lee
Quoted 20 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:27 PM, [email protected] wrote:
(quoted diff) +1 -0
@@ -681,15 +682,30 @@ static struct bpf_map 
 *bpf_struct_ops_map_alloc(union bpf_attr *attr)
       struct bpf_struct_ops_map *st_map;
       const struct btf_type *t, *vt;
       struct bpf_map *map;
+    struct btf *btf;

Review static struct bpf_map · line 693

Quoted 10 lines
(quoted diff) +5 -3
       int ret;
-    st_ops_desc = bpf_struct_ops_find_value(btf_vmlinux, 
 attr->btf_vmlinux_value_type_id);
-    if (!st_ops_desc)
-        return ERR_PTR(-ENOTSUPP);
+    if (attr->value_type_btf_obj_fd) {
+        /* The map holds btf for its whole life time. */
+        btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
+        if (IS_ERR(btf))
+            return ERR_PTR(PTR_ERR(btf));

             return ERR_CAST(btf);

Quoted 8 lines
Quoted 6 lines

It needs to check for btf_is_module:

         if (!btf_is_module(btf)) {
             btf_put(btf);
             return ERR_PTR(-EINVAL);
         }

Even btf is btf_vmlinux the kernel's btf, it still works.

btf could be a bpf program's btf. It needs to ensure it is a kernel module btf here.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Got it!

Isn't btf_is_kernel() better here?
User space may pass a fd to btf_vmlinux.

Limit it to btf_is_module. What is the benefit of supporting btf_vmlinux as a fd while fd 0 already means btf_vmlinux?

kfunc does not support the btf_vmlinux as fd also, supporting in struct_ops alone is confusing. I don't think the major user (libbpf) does this either, so really not much usage other than a confusing API to have both fd == 0 and a btf_vmlinux's fd to mean btf_vmlinux.

It is fair.

Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

Although libbpf pass 0 as the value of value_type_btf_obj_fd for btf_vmlinux now, it should be OK for a user space loader to pass a fd of btf_vmlinux.

WDYT?

#23 lore raw

PATCH v13 8/14 bpf: hold module for bpf_struct_ops_map.

In reply to [email protected]

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

To ensure that a module remains accessible whenever a struct_ops object of a struct_ops type provided by the module is still in use.

struct bpf_strct_ops_map doesn't hold a refcnt to btf anymore sicne a module will hold a refcnt to it's btf already. But, struct_ops programs are different. They hold their associated btf, not the module since they need only btf to assure their types (signatures).

---
 include/linux/bpf.h          |  1 +
 include/linux/bpf_verifier.h |  1 +
 kernel/bpf/bpf_struct_ops.c  | 28 +++++++++++++++++++++++-----
 kernel/bpf/verifier.c        | 10 ++++++++++
 4 files changed, 35 insertions(+), 5 deletions(-)
include/linux/bpf.h +1 -0
index 91bcd62d6fcf..c5c7cc4552f5 100644
@@ -1681,6 +1681,7 @@ struct bpf_struct_ops {
 	void (*unreg)(void *kdata);
 	int (*update)(void *kdata, void *old_kdata);
 	int (*validate)(void *kdata);
+	struct module *owner;
 	const char *name;
 	struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
 };
include/linux/bpf_verifier.h +1 -0
index 314b679fb494..01113bcdd479 100644
@@ -651,6 +651,7 @@ struct bpf_verifier_env {
 	u32 prev_insn_idx;
 	struct bpf_prog *prog;		/* eBPF program being verified */
 	const struct bpf_verifier_ops *ops;
+	struct module *attach_btf_mod;	/* The owner module of prog->aux->attach_btf */
 	struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
 	int stack_size;			/* number of states to be processed */
 	bool strict_alignment;		/* perform strict pointer alignment checks */
kernel/bpf/bpf_struct_ops.c +23 -5
index f943f8378e76..a838f7c7d583 100644
@@ -641,12 +641,15 @@ static void __bpf_struct_ops_map_free(struct bpf_map *map)
 		bpf_jit_uncharge_modmem(PAGE_SIZE);
 	}
 	bpf_map_area_free(st_map->uvalue);
-	btf_put(st_map->btf);
 	bpf_map_area_free(st_map);
 }
 
 static void bpf_struct_ops_map_free(struct bpf_map *map)
 {
+	struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
+
+	module_put(st_map->st_ops_desc->st_ops->owner);
+
 	/* The struct_ops's function may switch to another struct_ops.
 	 *
 	 * For example, bpf_tcp_cc_x->init() may switch to
@@ -681,6 +684,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
 	size_t st_map_size;
 	struct bpf_struct_ops_map *st_map;
 	const struct btf_type *t, *vt;
+	struct module *mod = NULL;
 	struct bpf_map *map;
 	struct btf *btf;
 	int ret;
@@ -690,10 +694,20 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
 		btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
 		if (IS_ERR(btf))
 			return ERR_PTR(PTR_ERR(btf));
-	} else {
+
+		if (btf != btf_vmlinux) {
+			mod = btf_try_get_module(btf);
+			if (!mod) {
+				btf_put(btf);
+				return ERR_PTR(-EINVAL);
+			}
+		}
+		/* mod (NULL for btf_vmlinux) holds a refcnt to btf. We
+		 * don't need an extra refcnt here.
+		 */
+		btf_put(btf);
+	} else
 		btf = btf_vmlinux;
-		btf_get(btf);
-	}
 
 	st_ops_desc = bpf_struct_ops_find_value(btf, attr->btf_vmlinux_value_type_id);
 	if (!st_ops_desc) {
@@ -756,7 +770,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
 errout_free:
 	__bpf_struct_ops_map_free(map);
 errout:
-	btf_put(btf);
+	module_put(mod);
 
 	return ERR_PTR(ret);
 }
@@ -886,6 +900,10 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map
 	if (!bpf_struct_ops_valid_to_reg(new_map))
 		return -EINVAL;
 
+	/* The old map is holding the refcount for the owner module.  The
+	 * ownership of the owner module refcount is going to be
+	 * transferred from the old map to the new map.
+	 */
 	if (!st_map->st_ops_desc->st_ops->update)
 		return -EOPNOTSUPP;
 
kernel/bpf/verifier.c +10 -0
index 795c16f9cf57..c303cf2fb5ff 100644
@@ -20079,6 +20079,14 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 	}
 
 	btf = prog->aux->attach_btf;
+	if (btf != btf_vmlinux) {
+		/* Make sure st_ops is valid through the lifetime of env */
+		env->attach_btf_mod = btf_try_get_module(btf);
+		if (!env->attach_btf_mod) {
+			verbose(env, "owner module of btf is not found\n");
+			return -ENOTSUPP;
+		}
+	}
 
 	btf_id = prog->aux->attach_btf_id;
 	st_ops_desc = bpf_struct_ops_find(btf, btf_id);
@@ -20792,6 +20800,8 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
 		env->prog->expected_attach_type = 0;
 
 	*prog = env->prog;
+
+	module_put(env->attach_btf_mod);
 err_unlock:
 	if (!is_priv)
 		mutex_unlock(&bpf_verifier_lock);
Signature · 1 line
2.34.1
#24 lore raw

Re: [PATCH bpf-next v13 08/14] bpf: hold module for bpf_struct_ops_map.

Quoted 6 lines · On 12/8/23 4:27 PM, [email protected] wrote:

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

To ensure that a module remains accessible whenever a struct_ops object of a struct_ops type provided by the module is still in use.

struct bpf_strct_ops_map doesn't hold a refcnt to btf anymore sicne a

s /bpf_strct_/bpf_struct_/

s/sicne/since/

Quoted 3 lines

module will hold a refcnt to it's btf already. But, struct_ops programs are different. They hold their associated btf, not the module since they need only btf to assure their types (signatures).

The patch subject is not accurate. The patch holds the module refcnt when verifying the bpf prog also. May be "hold module refcnt in struct_ops map creation and prog verification".

The commit message also is inaccurate on the prog load. It did not mention the module is also held when loading struct_ops prog but it is only held during the verification time. Please explain why it is only needed during the verification time.

Quoted 40 lines
---
  include/linux/bpf.h          |  1 +
  include/linux/bpf_verifier.h |  1 +
  kernel/bpf/bpf_struct_ops.c  | 28 +++++++++++++++++++++++-----
  kernel/bpf/verifier.c        | 10 ++++++++++
  4 files changed, 35 insertions(+), 5 deletions(-)
include/linux/bpf.h +1 -0
index 91bcd62d6fcf..c5c7cc4552f5 100644
@@ -1681,6 +1681,7 @@ struct bpf_struct_ops {
  	void (*unreg)(void *kdata);
  	int (*update)(void *kdata, void *old_kdata);
  	int (*validate)(void *kdata);
+	struct module *owner;
  	const char *name;
  	struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
  };
include/linux/bpf_verifier.h +1 -0
index 314b679fb494..01113bcdd479 100644
@@ -651,6 +651,7 @@ struct bpf_verifier_env {
  	u32 prev_insn_idx;
  	struct bpf_prog *prog;		/* eBPF program being verified */
  	const struct bpf_verifier_ops *ops;
+	struct module *attach_btf_mod;	/* The owner module of prog->aux->attach_btf */
  	struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
  	int stack_size;			/* number of states to be processed */
  	bool strict_alignment;		/* perform strict pointer alignment checks */
kernel/bpf/bpf_struct_ops.c +0 -0
index f943f8378e76..a838f7c7d583 100644
@@ -641,12 +641,15 @@ static void __bpf_struct_ops_map_free(struct bpf_map *map)
  		bpf_jit_uncharge_modmem(PAGE_SIZE);
  	}

kernel/bpf/bpf_struct_ops.c static void __bpf_struct_ops_map_free(struct bpf_map *map) · line 651

Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +3 -1
  	bpf_map_area_free(st_map->uvalue);
-	btf_put(st_map->btf);
  	bpf_map_area_free(st_map);
  }
 
  static void bpf_struct_ops_map_free(struct bpf_map *map)
  {
+	struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
+
+	module_put(st_map->st_ops_desc->st_ops->owner);

The module_get was not done on st_ops->owner when st_map->btf is btf_vmlinux (i.e. not module). Although it probably does not matter, I would feel more comfortable if it only releases for the things that it did acquire earlier.

	/* st_ops->owner was acquired during map_alloc to implicitly holds
	 * the btf's refcnt. The acquire was only done when btf_is_module()
	 * st_map->btf cannot be NULL here.
	 */
	if (btf_is_module(st_map->btf))
		module_put(st_map->st_ops_desc->st_ops->owner);
Quoted 46 lines
kernel/bpf/bpf_struct_ops.c +16 -4
+
  	/* The struct_ops's function may switch to another struct_ops.
  	 *
  	 * For example, bpf_tcp_cc_x->init() may switch to
@@ -681,6 +684,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
  	size_t st_map_size;
  	struct bpf_struct_ops_map *st_map;
  	const struct btf_type *t, *vt;
+	struct module *mod = NULL;
  	struct bpf_map *map;
  	struct btf *btf;
  	int ret;
@@ -690,10 +694,20 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
  		btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
  		if (IS_ERR(btf))
  			return ERR_PTR(PTR_ERR(btf));
-	} else {
+
+		if (btf != btf_vmlinux) {
+			mod = btf_try_get_module(btf);
+			if (!mod) {
+				btf_put(btf);
+				return ERR_PTR(-EINVAL);
+			}
+		}
+		/* mod (NULL for btf_vmlinux) holds a refcnt to btf. We
+		 * don't need an extra refcnt here.
+		 */
+		btf_put(btf);
+	} else
  		btf = btf_vmlinux;
-		btf_get(btf);
-	}
 
  	st_ops_desc = bpf_struct_ops_find_value(btf, attr->btf_vmlinux_value_type_id);
  	if (!st_ops_desc) {
@@ -756,7 +770,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
  errout_free:
  	__bpf_struct_ops_map_free(map);
  errout:
-	btf_put(btf);
+	module_put(mod);
 
  	return ERR_PTR(ret);
  }

kernel/bpf/bpf_struct_ops.c static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map · line 906

Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +4 -0
@@ -886,6 +900,10 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map
  	if (!bpf_struct_ops_valid_to_reg(new_map))
  		return -EINVAL;
 
+	/* The old map is holding the refcount for the owner module.  The
+	 * ownership of the owner module refcount is going to be
+	 * transferred from the old map to the new map.
+	 */

This part I don't understand. Both old and new map hold its own module's refcount at map_alloc time and release its own module refcnt during map_free(). Where the module refcount transfer happened?

kernel/bpf/verifier.c static int check_struct_ops_btf_id(struct bpf_verifier_env *env) · line 20082

Quoted 12 lines
kernel/bpf/bpf_struct_ops.c +0 -0
  	if (!st_map->st_ops_desc->st_ops->update)
  		return -EOPNOTSUPP;
 
kernel/bpf/verifier.c +1 -0
index 795c16f9cf57..c303cf2fb5ff 100644
@@ -20079,6 +20079,14 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
  	}
 
  	btf = prog->aux->attach_btf;
+	if (btf != btf_vmlinux) {
	if (btf_is_module(btf)) {
Quoted 19 lines
kernel/bpf/verifier.c +9 -0
+		/* Make sure st_ops is valid through the lifetime of env */
+		env->attach_btf_mod = btf_try_get_module(btf);
+		if (!env->attach_btf_mod) {
+			verbose(env, "owner module of btf is not found\n");
+			return -ENOTSUPP;
+		}
+	}
 
  	btf_id = prog->aux->attach_btf_id;
  	st_ops_desc = bpf_struct_ops_find(btf, btf_id);
@@ -20792,6 +20800,8 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, __u3
  		env->prog->expected_attach_type = 0;
 
  	*prog = env->prog;
+
+	module_put(env->attach_btf_mod);
  err_unlock:
  	if (!is_priv)
  		mutex_unlock(&bpf_verifier_lock);
#25 lore raw

Re: [PATCH bpf-next v13 08/14] bpf: hold module for bpf_struct_ops_map.

Quoted 75 lines · Martin KaFai Lau
Quoted 6 lines · On 12/8/23 4:27 PM, [email protected] wrote:

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

To ensure that a module remains accessible whenever a struct_ops object of a struct_ops type provided by the module is still in use.

struct bpf_strct_ops_map doesn't hold a refcnt to btf anymore sicne a

s /bpf_strct_/bpf_struct_/

s/sicne/since/

Quoted 3 lines

module will hold a refcnt to it's btf already. But, struct_ops programs are different. They hold their associated btf, not the module since they need only btf to assure their types (signatures).

The patch subject is not accurate. The patch holds the module refcnt when verifying the bpf prog also. May be "hold module refcnt in struct_ops map creation and prog verification".

The commit message also is inaccurate on the prog load. It did not mention the module is also held when loading struct_ops prog but it is only held during the verification time. Please explain why it is only needed during the verification time.

Quoted 42 lines

---
  include/linux/bpf.h          |  1 + include/linux/bpf_verifier.h |  1 + kernel/bpf/bpf_struct_ops.c  | 28 +++++++++++++++++++++++----- kernel/bpf/verifier.c        | 10 ++++++++++

  4 files changed, 35 insertions(+), 5 deletions(-)
include/linux/bpf.h +1 -0
index 91bcd62d6fcf..c5c7cc4552f5 100644
@@ -1681,6 +1681,7 @@ struct bpf_struct_ops {
       void (*unreg)(void *kdata);
       int (*update)(void *kdata, void *old_kdata);
       int (*validate)(void *kdata);
+    struct module *owner;
       const char *name;
       struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
   };
include/linux/bpf_verifier.h +0 -0
index 314b679fb494..01113bcdd479 100644
@@ -651,6 +651,7 @@ struct bpf_verifier_env {
       u32 prev_insn_idx;

      struct bpf_prog *prog;        /* eBPF program being verified */
      const struct bpf_verifier_ops *ops;
+    struct module *attach_btf_mod;    /* The owner module of prog->aux->attach_btf */
      struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */ int stack_size;            /* number of states to be processed */ bool strict_alignment;        /* perform strict pointer alignment checks */

kernel/bpf/bpf_struct_ops.c +0 -0
index f943f8378e76..a838f7c7d583 100644
@@ -641,12 +641,15 @@ static void __bpf_struct_ops_map_free(struct 
 bpf_map *map)
           bpf_jit_uncharge_modmem(PAGE_SIZE);
       }

kernel/bpf/bpf_struct_ops.c static void __bpf_struct_ops_map_free(struct · line 652

Quoted 10 lines
kernel/bpf/bpf_struct_ops.c +3 -1
       bpf_map_area_free(st_map->uvalue);
-    btf_put(st_map->btf);
       bpf_map_area_free(st_map);
   }
   static void bpf_struct_ops_map_free(struct bpf_map *map)
   {
+    struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map 
 *)map;
+
+    module_put(st_map->st_ops_desc->st_ops->owner);

The module_get was not done on st_ops->owner when st_map->btf is btf_vmlinux (i.e. not module). Although it probably does not matter, I would feel more comfortable if it only releases for the things that it did acquire earlier.

Quoted 6 lines

    /* st_ops->owner was acquired during map_alloc to implicitly holds
     * the btf's refcnt. The acquire was only done when btf_is_module()
     * st_map->btf cannot be NULL here.
     */
    if (btf_is_module(st_map->btf)) module_put(st_map->st_ops_desc->st_ops->owner);

Sure! I will update it.

Quoted 50 lines
Quoted 50 lines
kernel/bpf/bpf_struct_ops.c +1 -0
+
       /* The struct_ops's function may switch to another struct_ops.
        *

       * For example, bpf_tcp_cc_x->init() may switch to

kernel/bpf/bpf_struct_ops.c +1 -0
@@ -681,6 +684,7 @@ static struct bpf_map 
 *bpf_struct_ops_map_alloc(union bpf_attr *attr)
       size_t st_map_size;
       struct bpf_struct_ops_map *st_map;
       const struct btf_type *t, *vt;
+    struct module *mod = NULL;
       struct bpf_map *map;
       struct btf *btf;

      int ret;

kernel/bpf/bpf_struct_ops.c +13 -3
@@ -690,10 +694,20 @@ static struct bpf_map 
 *bpf_struct_ops_map_alloc(union bpf_attr *attr)
           btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
           if (IS_ERR(btf))
               return ERR_PTR(PTR_ERR(btf));
-    } else {
+
+        if (btf != btf_vmlinux) {
+            mod = btf_try_get_module(btf);
+            if (!mod) {
+                btf_put(btf);
+                return ERR_PTR(-EINVAL);
+            }
+        }
+        /* mod (NULL for btf_vmlinux) holds a refcnt to btf. We
+         * don't need an extra refcnt here.
+         */
+        btf_put(btf);
+    } else
           btf = btf_vmlinux;
-        btf_get(btf);
-    }
       st_ops_desc = bpf_struct_ops_find_value(btf, attr->btf_vmlinux_value_type_id);
       if (!st_ops_desc) {
@@ -756,7 +770,7 @@ static struct bpf_map 
 *bpf_struct_ops_map_alloc(union bpf_attr *attr)

  errout_free:
      __bpf_struct_ops_map_free(map);
  errout:
-    btf_put(btf);
+    module_put(mod);
      return ERR_PTR(ret);
  }

kernel/bpf/bpf_struct_ops.c +0 -0
@@ -886,6 +900,10 @@ static int bpf_struct_ops_map_link_update(struct 

bpf_link *link, struct bpf_map
      if (!bpf_struct_ops_valid_to_reg(new_map))

Quoted 8 lines
Quoted 5 lines

          return -EINVAL;
+    /* The old map is holding the refcount for the owner module.  The +     * ownership of the owner module refcount is going to be +     * transferred from the old map to the new map.
+     */

This part I don't understand. Both old and new map hold its own module's refcount at map_alloc time and release its own module refcnt during map_free(). Where the module refcount transfer happened?

Sorry! This comment is not more valid. I will remove it.

Quoted 15 lines

kernel/bpf/verifier.c static int check_struct_ops_btf_id(struct · line 20082

Quoted 12 lines

      if (!st_map->st_ops_desc->st_ops->update)
          return -EOPNOTSUPP;

kernel/bpf/verifier.c +1 -0
index 795c16f9cf57..c303cf2fb5ff 100644
@@ -20079,6 +20079,14 @@ static int check_struct_ops_btf_id(struct 
 bpf_verifier_env *env)
       }
       btf = prog->aux->attach_btf;
+    if (btf != btf_vmlinux) {

    if (btf_is_module(btf)) {

Got it!

Quoted 19 lines
Quoted 18 lines
kernel/bpf/verifier.c +7 -0
+        /* Make sure st_ops is valid through the lifetime of env */
+        env->attach_btf_mod = btf_try_get_module(btf);
+        if (!env->attach_btf_mod) {
+            verbose(env, "owner module of btf is not found\n");
+            return -ENOTSUPP;
+        }
+    }
       btf_id = prog->aux->attach_btf_id;
       st_ops_desc = bpf_struct_ops_find(btf, btf_id);
@@ -20792,6 +20800,8 @@ int bpf_check(struct bpf_prog **prog, union 

bpf_attr *attr, bpfptr_t uattr, __u3
          env->prog->expected_attach_type = 0;
      *prog = env->prog;
+
+    module_put(env->attach_btf_mod);
  err_unlock:
      if (!is_priv)
          mutex_unlock(&bpf_verifier_lock);

#26 lore raw

PATCH v13 9/14 bpf: validate value_type

In reply to [email protected]

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

A value_type should consist of three components: refcnt, state, and data. refcnt and state has been move to struct bpf_struct_ops_common_value to make it easier to check the value type.

---
 include/linux/bpf.h         | 12 +++++
 kernel/bpf/bpf_struct_ops.c | 93 ++++++++++++++++++++++++-------------
 2 files changed, 72 insertions(+), 33 deletions(-)
include/linux/bpf.h +12 -0
index c5c7cc4552f5..7384806ee74e 100644
@@ -3321,4 +3321,16 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog)
 	return prog->aux->func_idx != 0;
 }
 
+enum bpf_struct_ops_state {
+	BPF_STRUCT_OPS_STATE_INIT,
+	BPF_STRUCT_OPS_STATE_INUSE,
+	BPF_STRUCT_OPS_STATE_TOBEFREE,
+	BPF_STRUCT_OPS_STATE_READY,
+};
+
+struct bpf_struct_ops_common_value {
+	refcount_t refcnt;
+	enum bpf_struct_ops_state state;
+};
+
 #endif /* _LINUX_BPF_H */
kernel/bpf/bpf_struct_ops.c +60 -33

189 lines not on this page · raw message

Signature · 1 line
2.34.1
#27 lore raw

Re: [PATCH bpf-next v13 09/14] bpf: validate value_type

Quoted 9 lines · On 12/8/23 4:27 PM, [email protected] wrote:
include/linux/bpf.h +1 -0
index c5c7cc4552f5..7384806ee74e 100644
@@ -3321,4 +3321,16 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog)
  	return prog->aux->func_idx != 0;
  }
 
+enum bpf_struct_ops_state {

include/linux/bpf.h static inline bool bpf_is_subprog(const struct bpf_prog *prog) · line 3334

Quoted 10 lines
include/linux/bpf.h +10 -0
+	BPF_STRUCT_OPS_STATE_INIT,
+	BPF_STRUCT_OPS_STATE_INUSE,
+	BPF_STRUCT_OPS_STATE_TOBEFREE,
+	BPF_STRUCT_OPS_STATE_READY,
+};
+
+struct bpf_struct_ops_common_value {
+	refcount_t refcnt;
+	enum bpf_struct_ops_state state;
+};

nit. Move these up closer to the existing 'struct bpf_struct_ops' and related functions. Probably under 'struct bpf_struct_ops'.

#28 lore raw

Re: [PATCH bpf-next v13 09/14] bpf: validate value_type

Quoted 14 lines · Martin KaFai Lau
Quoted 13 lines · On 12/8/23 4:27 PM, [email protected] wrote:
include/linux/bpf.h +5 -0
index c5c7cc4552f5..7384806ee74e 100644
@@ -3321,4 +3321,16 @@ static inline bool bpf_is_subprog(const struct 
 bpf_prog *prog)
       return prog->aux->func_idx != 0;
   }
+enum bpf_struct_ops_state {
+    BPF_STRUCT_OPS_STATE_INIT,
+    BPF_STRUCT_OPS_STATE_INUSE,
+    BPF_STRUCT_OPS_STATE_TOBEFREE,
+    BPF_STRUCT_OPS_STATE_READY,
Quoted 9 lines

include/linux/bpf.h static inline bool bpf_is_subprog(const struct bpf_prog *prog) · line 3334

Quoted 6 lines
include/linux/bpf.h +6 -0
+};
+
+struct bpf_struct_ops_common_value {
+    refcount_t refcnt;
+    enum bpf_struct_ops_state state;
+};

nit. Move these up closer to the existing 'struct bpf_struct_ops' and related functions. Probably under 'struct bpf_struct_ops'.

Sure! I will move all following changes to the place closer to struct bpf_struct_ops.

#29 lore raw

PATCH v13 10/14 bpf, net: switch to dynamic registration

In reply to [email protected]

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

Replace the static list of struct_ops types with per-btf struct_ops_tab to enable dynamic registration.

Both bpf_dummy_ops and bpf_tcp_ca now utilize the registration function instead of being listed in bpf_struct_ops_types.h.

---
 include/linux/bpf.h               | 28 ++++++++--
 include/linux/btf.h               |  2 +
 kernel/bpf/bpf_struct_ops.c       | 90 ++++++++++---------------------
 kernel/bpf/bpf_struct_ops_types.h | 12 -----
 kernel/bpf/btf.c                  | 49 +++++++++++++++--
 net/bpf/bpf_dummy_struct_ops.c    | 13 ++++-
 net/ipv4/bpf_tcp_ca.c             | 14 +++--
 7 files changed, 119 insertions(+), 89 deletions(-)
 delete mode 100644 kernel/bpf/bpf_struct_ops_types.h
include/linux/bpf.h +23 -5
index 7384806ee74e..c881befa35f5 100644
@@ -1698,7 +1698,6 @@ struct bpf_struct_ops_desc {
 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
 const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id);
-void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
 bool bpf_struct_ops_get(const void *kdata);
 void bpf_struct_ops_put(const void *kdata);
 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
@@ -1744,10 +1743,6 @@ static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *
 {
 	return NULL;
 }
-static inline void bpf_struct_ops_init(struct btf *btf,
-				       struct bpf_verifier_log *log)
-{
-}
 static inline bool bpf_try_module_get(const void *data, struct module *owner)
 {
 	return try_module_get(owner);
@@ -3321,6 +3316,14 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog)
 	return prog->aux->func_idx != 0;
 }
 
+int register_bpf_struct_ops(struct bpf_struct_ops *st_ops);
+
+#define REGISTER_BPF_STRUCT_OPS(st_ops, type) \
+({					      \
+	BTF_STRUCT_OPS_TYPE_EMIT(type);	      \
+	register_bpf_struct_ops(st_ops);      \
+})
+
 enum bpf_struct_ops_state {
 	BPF_STRUCT_OPS_STATE_INIT,
 	BPF_STRUCT_OPS_STATE_INUSE,
@@ -3333,4 +3336,19 @@ struct bpf_struct_ops_common_value {
 	enum bpf_struct_ops_state state;
 };
 
+/* bpf_struct_ops_##_name (e.g. bpf_struct_ops_tcp_congestion_ops) is
+ * the map's value exposed to the userspace and its btf-type-id is
+ * stored at the map->btf_vmlinux_value_type_id.
+ *
+ */
+#define DEFINE_STRUCT_OPS_VALUE_TYPE(_name)			\
+struct bpf_struct_ops_##_name {					\
+	struct bpf_struct_ops_common_value common;		\
+	struct _name data ____cacheline_aligned_in_smp;		\
+}
+
+int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
+			     struct btf *btf,
+			     struct bpf_verifier_log *log);
+
 #endif /* _LINUX_BPF_H */
include/linux/btf.h +2 -0
index e2f4b85cf82a..cabab3db5216 100644
@@ -12,6 +12,8 @@
 #include <uapi/linux/bpf.h>
 
 #define BTF_TYPE_EMIT(type) ((void)(type *)0)
+#define BTF_STRUCT_OPS_TYPE_EMIT(type) \
+	((void)(struct bpf_struct_ops_##type *)0)
 #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
 
 /* These need to be macros, as the expressions are used in assembler input */
kernel/bpf/bpf_struct_ops.c +27 -63

176 lines not on this page · raw message

kernel/bpf/bpf_struct_ops_types.h deleted +0 -12
deleted file mode 100644
index 5678a9ddf817..000000000000
@@ -1,12 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/* internal file - do not include directly */
-
-#ifdef CONFIG_BPF_JIT
-#ifdef CONFIG_NET
-BPF_STRUCT_OPS_TYPE(bpf_dummy_ops)
-#endif
-#ifdef CONFIG_INET
-#include <net/tcp.h>
-BPF_STRUCT_OPS_TYPE(tcp_congestion_ops)
-#endif
-#endif
kernel/bpf/btf.c +45 -4

79 lines not on this page · raw message

net/bpf/bpf_dummy_struct_ops.c +11 -2
index bd753dbccaf6..2bb2625ffbbf 100644
@@ -7,7 +7,7 @@
 #include <linux/bpf.h>
 #include <linux/btf.h>
 
-extern struct bpf_struct_ops bpf_bpf_dummy_ops;
+static struct bpf_struct_ops bpf_bpf_dummy_ops;
 
 /* A common type for test_N with return value in bpf_dummy_ops */
 typedef int (*dummy_ops_test_ret_fn)(struct bpf_dummy_ops_state *state, ...);
@@ -222,11 +222,13 @@ static int bpf_dummy_reg(void *kdata)
 	return -EOPNOTSUPP;
 }
 
+DEFINE_STRUCT_OPS_VALUE_TYPE(bpf_dummy_ops);
+
 static void bpf_dummy_unreg(void *kdata)
 {
 }
 
-struct bpf_struct_ops bpf_bpf_dummy_ops = {
+static struct bpf_struct_ops bpf_bpf_dummy_ops = {
 	.verifier_ops = &bpf_dummy_verifier_ops,
 	.init = bpf_dummy_init,
 	.check_member = bpf_dummy_ops_check_member,
@@ -234,4 +236,11 @@ struct bpf_struct_ops bpf_bpf_dummy_ops = {
 	.reg = bpf_dummy_reg,
 	.unreg = bpf_dummy_unreg,
 	.name = "bpf_dummy_ops",
+	.owner = THIS_MODULE,
 };
+
+static int __init bpf_dummy_struct_ops_init(void)
+{
+	return REGISTER_BPF_STRUCT_OPS(&bpf_bpf_dummy_ops, bpf_dummy_ops);
+}
+late_initcall(bpf_dummy_struct_ops_init);
net/ipv4/bpf_tcp_ca.c +11 -3
index 5bb56c9ad4e5..6d2728dc600e 100644
@@ -12,7 +12,7 @@
 #include <net/bpf_sk_storage.h>
 
 /* "extern" is to avoid sparse warning.  It is only used in bpf_struct_ops.c. */
-extern struct bpf_struct_ops bpf_tcp_congestion_ops;
+static struct bpf_struct_ops bpf_tcp_congestion_ops;
 
 static u32 unsupported_ops[] = {
 	offsetof(struct tcp_congestion_ops, get_info),
@@ -277,7 +277,9 @@ static int bpf_tcp_ca_validate(void *kdata)
 	return tcp_validate_congestion_control(kdata);
 }
 
-struct bpf_struct_ops bpf_tcp_congestion_ops = {
+DEFINE_STRUCT_OPS_VALUE_TYPE(tcp_congestion_ops);
+
+static struct bpf_struct_ops bpf_tcp_congestion_ops = {
 	.verifier_ops = &bpf_tcp_ca_verifier_ops,
 	.reg = bpf_tcp_ca_reg,
 	.unreg = bpf_tcp_ca_unreg,
@@ -287,10 +289,16 @@ struct bpf_struct_ops bpf_tcp_congestion_ops = {
 	.init = bpf_tcp_ca_init,
 	.validate = bpf_tcp_ca_validate,
 	.name = "tcp_congestion_ops",
+	.owner = THIS_MODULE,
 };
 
 static int __init bpf_tcp_ca_kfunc_init(void)
 {
-	return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
+	int ret;
+
+	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
+	ret = ret ?: REGISTER_BPF_STRUCT_OPS(&bpf_tcp_congestion_ops, tcp_congestion_ops);
+
+	return ret;
 }
 late_initcall(bpf_tcp_ca_kfunc_init);
Signature · 1 line
2.34.1
#30 lore raw

Re: [PATCH bpf-next v13 10/14] bpf, net: switch to dynamic registration

Quoted 24 lines · On 12/8/23 4:27 PM, [email protected] wrote:
include/linux/bpf.h +0 -5
index 7384806ee74e..c881befa35f5 100644
@@ -1698,7 +1698,6 @@ struct bpf_struct_ops_desc {
  #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
  #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
  const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *btf, u32 type_id);
-void bpf_struct_ops_init(struct btf *btf, struct bpf_verifier_log *log);
  bool bpf_struct_ops_get(const void *kdata);
  void bpf_struct_ops_put(const void *kdata);
  int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
@@ -1744,10 +1743,6 @@ static inline const struct bpf_struct_ops_desc *bpf_struct_ops_find(struct btf *
  {
  	return NULL;
  }
-static inline void bpf_struct_ops_init(struct btf *btf,
-				       struct bpf_verifier_log *log)
-{
-}
  static inline bool bpf_try_module_get(const void *data, struct module *owner)
  {
  	return try_module_get(owner);

include/linux/bpf.h static inline bool bpf_is_subprog(const struct bpf_prog *prog) · line 3319

Quoted 4 lines
include/linux/bpf.h +1 -0
@@ -3321,6 +3316,14 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog)
  	return prog->aux->func_idx != 0;
  }
 
+int register_bpf_struct_ops(struct bpf_struct_ops *st_ops);

This probably should be in btf.h like register_btf_kfunc_id_set(). and should have an empty implementation when CONFIG_BPF_SYSCALL is not set.

include/linux/bpf.h static inline bool bpf_is_subprog(const struct bpf_prog *prog) · line 3321

Quoted 2 lines
include/linux/bpf.h +2 -0
+
+#define REGISTER_BPF_STRUCT_OPS(st_ops, type) \

Add comment here to suggest the module writer to use REGISTER_BPF_STRUCT_OPS instead of register_bpf_struct_ops().

include/linux/bpf.h static inline bool bpf_is_subprog(const struct bpf_prog *prog) · line 3323

Quoted 2 lines
include/linux/bpf.h +2 -0
+({					      \
+	BTF_STRUCT_OPS_TYPE_EMIT(type);	      \

Directly use BTF_TYPE_EMIT(struct bpf_struct_ops_##type) here.

Also, is it possible to do the DEFINE_STRUCT_OPS_VALUE_TYPE() type here such that the module writer does not need to. It will be nice if REGISTER_BPF_STRUCT_OPS does the define value type and emit value type together.

Quoted 15 lines
include/linux/bpf.h +8 -0
+	register_bpf_struct_ops(st_ops);      \
+})
+
  enum bpf_struct_ops_state {
  	BPF_STRUCT_OPS_STATE_INIT,
  	BPF_STRUCT_OPS_STATE_INUSE,
@@ -3333,4 +3336,19 @@ struct bpf_struct_ops_common_value {
  	enum bpf_struct_ops_state state;
  };
 
+/* bpf_struct_ops_##_name (e.g. bpf_struct_ops_tcp_congestion_ops) is
+ * the map's value exposed to the userspace and its btf-type-id is
+ * stored at the map->btf_vmlinux_value_type_id.
+ *
+ */

include/linux/bpf.h struct bpf_struct_ops_common_value { · line 3353

Quoted 10 lines
include/linux/bpf.h +10 -0
+#define DEFINE_STRUCT_OPS_VALUE_TYPE(_name)			\
+struct bpf_struct_ops_##_name {					\
+	struct bpf_struct_ops_common_value common;		\
+	struct _name data ____cacheline_aligned_in_smp;		\
+}
+
+int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
+			     struct btf *btf,
+			     struct bpf_verifier_log *log);
+

nit. same as the comment in previous patch 8. Move these up closer to other struct_ops structs and functions. bpf_struct_ops_desc_init is implemented in bpf_struct_ops.c and it should be in the same CONFIG_BPF_JIT guard earlier in this file. Also, where is the empty bpf_struct_ops_desc_init() when CONFIG_BPF_JIT is not set?

include/linux/btf.h line 16

Quoted 11 lines
include/linux/bpf.h +0 -0
  #endif /* _LINUX_BPF_H */
include/linux/btf.h +2 -0
index e2f4b85cf82a..cabab3db5216 100644
@@ -12,6 +12,8 @@
  #include <uapi/linux/bpf.h>
 
  #define BTF_TYPE_EMIT(type) ((void)(type *)0)
+#define BTF_STRUCT_OPS_TYPE_EMIT(type) \
+	((void)(struct bpf_struct_ops_##type *)0)

Remove this new macro. It is almost the same as BTF_TYPE_EMIT and it is only used once in REGISTER_BPF_STRUCT_OPS. module writer will use REGISTER_BPF_STRUCT_OPS and no need to figure out what type needs to be emitted.

Quoted 3 lines
include/linux/btf.h +0 -0
  #define BTF_TYPE_EMIT_ENUM(enum_val) ((void)enum_val)
 
  /* These need to be macros, as the expressions are used in assembler input */

[ ... ]

Quoted 22 lines
  static const struct bpf_struct_ops_desc *
  bpf_struct_ops_find_value(struct btf *btf, u32 value_id)
  {
include/linux/btf.h +7 -4
+	const struct bpf_struct_ops_desc *st_ops_list;
  	unsigned int i;
+	u32 cnt = 0;
 
-	if (!value_id || !btf)
+	if (!value_id)
  		return NULL;
 
-	for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
-		if (bpf_struct_ops[i].value_id == value_id)
-			return &bpf_struct_ops[i];
+	st_ops_list = btf_get_struct_ops(btf, &cnt);
+	for (i = 0; i < cnt; i++) {
+		if (st_ops_list[i].value_id == value_id)
+			return &st_ops_list[i];
  	}
 
  	return NULL;

include/linux/btf.h bpf_struct_ops_find_value(struct btf *btf, u32 value_id) · line 232

Quoted 6 lines
include/linux/btf.h +2 -0
@@ -266,14 +227,17 @@ bpf_struct_ops_find_value(struct btf *btf, u32 value_id)
  const struct bpf_struct_ops_desc *
  bpf_struct_ops_find(struct btf *btf, u32 type_id)
  {
+	const struct bpf_struct_ops_desc *st_ops_list;
  	unsigned int i;
+	u32 cnt;

cnt is not initialized here. The above bpf_struct_ops_find_value() did init cnt to 0 though.

include/linux/btf.h bpf_struct_ops_find_value(struct btf *btf, u32 value_id) · line 237

Quoted 10 lines
include/linux/btf.h +3 -4
-	if (!type_id || !btf)
+	if (!type_id)
  		return NULL;
 
-	for (i = 0; i < ARRAY_SIZE(bpf_struct_ops); i++) {
-		if (bpf_struct_ops[i].type_id == type_id)
-			return &bpf_struct_ops[i];
+	st_ops_list = btf_get_struct_ops(btf, &cnt);
+	for (i = 0; i < cnt; i++) {

If st_ops_list is NULL, cnt could be anything here. Lets fix patch 4 to set cnt to 0 when btf->struct_ops_tab is empty.

Quoted 46 lines
include/linux/btf.h +2 -0
+		if (st_ops_list[i].type_id == type_id)
+			return &st_ops_list[i];
  	}
 
  	return NULL;
kernel/bpf/bpf_struct_ops_types.h deleted +0 -12
deleted file mode 100644
index 5678a9ddf817..000000000000
@@ -1,12 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/* internal file - do not include directly */
-
-#ifdef CONFIG_BPF_JIT
-#ifdef CONFIG_NET
-BPF_STRUCT_OPS_TYPE(bpf_dummy_ops)
-#endif
-#ifdef CONFIG_INET
-#include <net/tcp.h>
-BPF_STRUCT_OPS_TYPE(tcp_congestion_ops)
-#endif
-#endif
kernel/bpf/btf.c +1 -2
index edbe3cbf2dcc..5545dee3ff54 100644
@@ -19,6 +19,7 @@
  #include <linux/bpf_verifier.h>
  #include <linux/btf.h>
  #include <linux/btf_ids.h>
+#include <linux/bpf.h>
  #include <linux/bpf_lsm.h>
  #include <linux/skmsg.h>
  #include <linux/perf_event.h>
@@ -5792,8 +5793,6 @@ struct btf *btf_parse_vmlinux(void)
  	/* btf_parse_vmlinux() runs under bpf_verifier_lock */
  	bpf_ctx_convert.t = btf_type_by_id(btf, bpf_ctx_convert_btf_id[0]);
 
-	bpf_struct_ops_init(btf, log);
-
  	refcount_set(&btf->refcnt, 1);
 
  	err = btf_alloc_id(btf);
@@ -8621,11 +8620,21 @@ bool btf_type_ids_nocast_alias(struct bpf_verifier_log *log,
  	return !strncmp(reg_name, arg_name, cmp_len);

kernel/bpf/btf.c bool btf_type_ids_nocast_alias(struct bpf_verifier_log *log, · line 8630

Quoted 10 lines
kernel/bpf/btf.c +8 -0
  }
 
+#ifndef CONFIG_BPF_JIT
+int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
+			     struct btf *btf,
+			     struct bpf_verifier_log *log)
+{
+	return -ENOTSUPP;
+}
+#endif /* CONFIG_BPF_JIT */

ah. It is here. This should be in bpf.h.

#31 lore raw

PATCH v13 11/14 libbpf: Find correct module BTFs for struct_ops maps and progs.

In reply to [email protected]

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

Locate the module BTFs for struct_ops maps and progs and pass them to the kernel. This ensures that the kernel correctly resolves type IDs from the appropriate module BTFs.

For the map of a struct_ops object, the FD of the module BTF is set to bpf_map to keep a reference to the module BTF. The FD is passed to the kernel as value_type_btf_obj_fd when the struct_ops object is loaded.

For a bpf_struct_ops prog, attach_btf_obj_fd of bpf_prog is the FD of a module BTF in the kernel.

---
 tools/lib/bpf/bpf.c    |  4 +++-
 tools/lib/bpf/bpf.h    |  5 +++--
 tools/lib/bpf/libbpf.c | 38 ++++++++++++++++++++++++++++----------
 3 files changed, 34 insertions(+), 13 deletions(-)
tools/lib/bpf/bpf.c +3 -1
index f4e1da3c6d5f..14ed20b73b86 100644
@@ -169,7 +169,8 @@ int bpf_map_create(enum bpf_map_type map_type,
 		   __u32 max_entries,
 		   const struct bpf_map_create_opts *opts)
 {
-	const size_t attr_sz = offsetofend(union bpf_attr, map_token_fd);
+	const size_t attr_sz = offsetofend(union bpf_attr,
+					   value_type_btf_obj_fd);
 	union bpf_attr attr;
 	int fd;
 
@@ -191,6 +192,7 @@ int bpf_map_create(enum bpf_map_type map_type,
 	attr.btf_key_type_id = OPTS_GET(opts, btf_key_type_id, 0);
 	attr.btf_value_type_id = OPTS_GET(opts, btf_value_type_id, 0);
 	attr.btf_vmlinux_value_type_id = OPTS_GET(opts, btf_vmlinux_value_type_id, 0);
+	attr.value_type_btf_obj_fd = OPTS_GET(opts, value_type_btf_obj_fd, 0);
 
 	attr.inner_map_fd = OPTS_GET(opts, inner_map_fd, 0);
 	attr.map_flags = OPTS_GET(opts, map_flags, 0);
tools/lib/bpf/bpf.h +3 -2
index 991b86bfe7e4..1cecfde0c732 100644
@@ -53,9 +53,10 @@ struct bpf_map_create_opts {
 	__u32 map_ifindex;
 
 	__u32 token_fd;
-	size_t :0;
+	__u32 value_type_btf_obj_fd;
+	size_t:0;
 };
-#define bpf_map_create_opts__last_field token_fd
+#define bpf_map_create_opts__last_field value_type_btf_obj_fd
 
 LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
 			      const char *map_name,
tools/lib/bpf/libbpf.c +28 -10
index ac54ebc0629f..d6709ebd1d8a 100644
@@ -527,6 +527,7 @@ struct bpf_map {
 	struct bpf_map_def def;
 	__u32 numa_node;
 	__u32 btf_var_idx;
+	int mod_btf_fd;
 	__u32 btf_key_type_id;
 	__u32 btf_value_type_id;
 	__u32 btf_vmlinux_value_type_id;
@@ -930,22 +931,29 @@ find_member_by_name(const struct btf *btf, const struct btf_type *t,
 	return NULL;
 }
 
+static int find_ksym_btf_id(struct bpf_object *obj, const char *ksym_name,
+			    __u16 kind, struct btf **res_btf,
+			    struct module_btf **res_mod_btf);
+
 #define STRUCT_OPS_VALUE_PREFIX "bpf_struct_ops_"
 static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
 				   const char *name, __u32 kind);
 
 static int
-find_struct_ops_kern_types(const struct btf *btf, const char *tname,
+find_struct_ops_kern_types(struct bpf_object *obj, const char *tname,
+			   struct module_btf **mod_btf,
 			   const struct btf_type **type, __u32 *type_id,
 			   const struct btf_type **vtype, __u32 *vtype_id,
 			   const struct btf_member **data_member)
 {
 	const struct btf_type *kern_type, *kern_vtype;
 	const struct btf_member *kern_data_member;
+	struct btf *btf;
 	__s32 kern_vtype_id, kern_type_id;
 	__u32 i;
 
-	kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
+	kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT,
+					&btf, mod_btf);
 	if (kern_type_id < 0) {
 		pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
 			tname);
@@ -999,14 +1007,16 @@ static bool bpf_map__is_struct_ops(const struct bpf_map *map)
 }
 
 /* Init the map's fields that depend on kern_btf */
-static int bpf_map__init_kern_struct_ops(struct bpf_map *map,
-					 const struct btf *btf,
-					 const struct btf *kern_btf)
+static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
 {
 	const struct btf_member *member, *kern_member, *kern_data_member;
 	const struct btf_type *type, *kern_type, *kern_vtype;
 	__u32 i, kern_type_id, kern_vtype_id, kern_data_off;
+	struct bpf_object *obj = map->obj;
+	const struct btf *btf = obj->btf;
 	struct bpf_struct_ops *st_ops;
+	const struct btf *kern_btf;
+	struct module_btf *mod_btf;
 	void *data, *kern_data;
 	const char *tname;
 	int err;
@@ -1014,16 +1024,19 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map,
 	st_ops = map->st_ops;
 	type = st_ops->type;
 	tname = st_ops->tname;
-	err = find_struct_ops_kern_types(kern_btf, tname,
+	err = find_struct_ops_kern_types(obj, tname, &mod_btf,
 					 &kern_type, &kern_type_id,
 					 &kern_vtype, &kern_vtype_id,
 					 &kern_data_member);
 	if (err)
 		return err;
 
+	kern_btf = mod_btf ? mod_btf->btf : obj->btf_vmlinux;
+
 	pr_debug("struct_ops init_kern %s: type_id:%u kern_type_id:%u kern_vtype_id:%u\n",
 		 map->name, st_ops->type_id, kern_type_id, kern_vtype_id);
 
+	map->mod_btf_fd = mod_btf ? mod_btf->fd : 0;
 	map->def.value_size = kern_vtype->size;
 	map->btf_vmlinux_value_type_id = kern_vtype_id;
 
@@ -1099,6 +1112,8 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map,
 				return -ENOTSUP;
 			}
 
+			if (mod_btf)
+				prog->attach_btf_obj_fd = mod_btf->fd;
 			prog->attach_btf_id = kern_type_id;
 			prog->expected_attach_type = kern_member_idx;
 
@@ -1141,8 +1156,7 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
 		if (!bpf_map__is_struct_ops(map))
 			continue;
 
-		err = bpf_map__init_kern_struct_ops(map, obj->btf,
-						    obj->btf_vmlinux);
+		err = bpf_map__init_kern_struct_ops(map);
 		if (err)
 			return err;
 	}
@@ -5212,8 +5226,10 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
 	create_attr.numa_node = map->numa_node;
 	create_attr.map_extra = map->map_extra;
 
-	if (bpf_map__is_struct_ops(map))
+	if (bpf_map__is_struct_ops(map)) {
 		create_attr.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
+		create_attr.value_type_btf_obj_fd = map->mod_btf_fd;
+	}
 
 	if (obj->btf && btf__fd(obj->btf) >= 0) {
 		create_attr.btf_fd = btf__fd(obj->btf);
@@ -9557,7 +9573,9 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, const char *attac
 		*btf_obj_fd = 0;
 		*btf_type_id = 1;
 	} else {
-		err = find_kernel_btf_id(prog->obj, attach_name, attach_type, btf_obj_fd, btf_type_id);
+		err = find_kernel_btf_id(prog->obj, attach_name,
+					 attach_type, btf_obj_fd,
+					 btf_type_id);
 	}
 	if (err) {
 		pr_warn("prog '%s': failed to find kernel BTF type ID of '%s': %d\n",
Signature · 1 line
2.34.1
#32 lore raw

PATCH v13 12/14 bpf: export btf_ctx_access to modules.

In reply to [email protected]

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

The module requires the use of btf_ctx_access() to invoke bpf_tracing_btf_ctx_access() from a module. This function is valuable for implementing validation functions that ensure proper access to ctx.

---
 kernel/bpf/btf.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/btf.c +1 -0
index 5545dee3ff54..d9cdf41e8f34 100644
@@ -6142,6 +6142,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
 		__btf_name_by_offset(btf, t->name_off));
 	return true;
 }
+EXPORT_SYMBOL_GPL(btf_ctx_access);
 
 enum bpf_struct_walk_result {
 	/* < 0 error */
Signature · 1 line
2.34.1
#33 lore raw

PATCH v13 13/14 selftests/bpf: test case for register_bpf_struct_ops().

In reply to [email protected]

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

Create a new struct_ops type called bpf_testmod_ops within the bpf_testmod module. When a struct_ops object is registered, the bpf_testmod module will invoke test_2 from the module.

---
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   | 52 +++++++++++
 .../selftests/bpf/bpf_testmod/bpf_testmod.h   |  5 +
 .../bpf/prog_tests/test_struct_ops_module.c   | 92 +++++++++++++++++++
 .../selftests/bpf/progs/struct_ops_module.c   | 30 ++++++
 .../selftests/bpf/progs/testmod_unload.c      | 25 +++++
 5 files changed, 204 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
 create mode 100644 tools/testing/selftests/bpf/progs/testmod_unload.c
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +52 -0
index 91907b321f91..804a67bbb479 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Facebook */
+#include <linux/bpf.h>
 #include <linux/btf.h>
 #include <linux/btf_ids.h>
 #include <linux/error-injection.h>
@@ -520,11 +521,61 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_test_static_unused_arg)
 BTF_ID_FLAGS(func, bpf_kfunc_call_test_offset)
 BTF_SET8_END(bpf_testmod_check_kfunc_ids)
 
+DEFINE_STRUCT_OPS_VALUE_TYPE(bpf_testmod_ops);
+
+static int bpf_testmod_ops_init(struct btf *btf)
+{
+	return 0;
+}
+
+static bool bpf_testmod_ops_is_valid_access(int off, int size,
+					    enum bpf_access_type type,
+					    const struct bpf_prog *prog,
+					    struct bpf_insn_access_aux *info)
+{
+	return bpf_tracing_btf_ctx_access(off, size, type, prog, info);
+}
+
+static int bpf_testmod_ops_init_member(const struct btf_type *t,
+				       const struct btf_member *member,
+				       void *kdata, const void *udata)
+{
+	return 0;
+}
+
 static const struct btf_kfunc_id_set bpf_testmod_kfunc_set = {
 	.owner = THIS_MODULE,
 	.set   = &bpf_testmod_check_kfunc_ids,
 };
 
+static const struct bpf_verifier_ops bpf_testmod_verifier_ops = {
+	.is_valid_access = bpf_testmod_ops_is_valid_access,
+};
+
+static int bpf_dummy_reg(void *kdata)
+{
+	struct bpf_testmod_ops *ops = kdata;
+	int r;
+
+	r = ops->test_2(4, 3);
+
+	return 0;
+}
+
+static void bpf_dummy_unreg(void *kdata)
+{
+}
+
+struct bpf_struct_ops bpf_bpf_testmod_ops = {
+	.verifier_ops = &bpf_testmod_verifier_ops,
+	.init = bpf_testmod_ops_init,
+	.init_member = bpf_testmod_ops_init_member,
+	.reg = bpf_dummy_reg,
+	.unreg = bpf_dummy_unreg,
+	.name = "bpf_testmod_ops",
+	.owner = THIS_MODULE,
+};
+
 extern int bpf_fentry_test1(int a);
 
 static int bpf_testmod_init(void)
@@ -535,6 +586,7 @@ static int bpf_testmod_init(void)
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_testmod_kfunc_set);
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_testmod_kfunc_set);
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &bpf_testmod_kfunc_set);
+	ret = ret ?: REGISTER_BPF_STRUCT_OPS(&bpf_bpf_testmod_ops, bpf_testmod_ops);
 	if (ret < 0)
 		return ret;
 	if (bpf_fentry_test1(0) < 0)
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 +92 -0

92 lines not on this page · raw message

tools/testing/selftests/bpf/progs/struct_ops_module.c new file +30 -0
new file mode 100644
index 000000000000..cb305d04342f
@@ -0,0 +1,30 @@
+// 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";
+
+int test_2_result = 0;
+
+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)
+{
+	test_2_result = a + b;
+	return a + b;
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_1 = {
+	.test_1 = (void *)test_1,
+	.test_2 = (void *)test_2,
+};
+
tools/testing/selftests/bpf/progs/testmod_unload.c new file +25 -0
new file mode 100644
index 000000000000..bb17914ecca3
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_core_read.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+int bpf_testmod_put = 0;
+
+SEC("fentry/__bpf_struct_ops_map_free")
+int BPF_PROG(trace_map_free, struct bpf_map *map)
+{
+	static const char name[] = "testmod_1";
+	int i;
+
+	for (i = 0; i < sizeof(name); i++) {
+		if (map->name[i] != name[i])
+			return 0;
+	}
+
+	bpf_testmod_put = 1;
+
+	return 0;
+}
Signature · 1 line
2.34.1
#34 lore raw

Re: [PATCH bpf-next v13 13/14] selftests/bpf: test case for register_bpf_struct_ops().

Quoted 70 lines · On 12/8/23 4:27 PM, [email protected] wrote:
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c new file +64 -0
new file mode 100644
index 000000000000..55a4c6ed92aa
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
+#include <test_progs.h>
+#include <time.h>
+
+#include "struct_ops_module.skel.h"
+#include "testmod_unload.skel.h"
+
+static void test_regular_load(void)
+{
+	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
+	struct struct_ops_module *skel;
+	struct testmod_unload *skel_unload;
+	struct bpf_link *link_map_free = NULL;
+	struct bpf_link *link;
+	int err, i;
+
+	skel = struct_ops_module__open_opts(&opts);
+	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
+		return;
+
+	err = struct_ops_module__load(skel);
+	if (!ASSERT_OK(err, "struct_ops_module_load"))
+		goto cleanup;
+
+	link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
+	ASSERT_OK_PTR(link, "attach_test_mod_1");
+
+	/* test_2() will be called from bpf_dummy_reg() in bpf_testmod.c */
+	ASSERT_EQ(skel->bss->test_2_result, 7, "test_2_result");
+
+	bpf_link__destroy(link);
+
+cleanup:
+	skel_unload = testmod_unload__open_and_load();
+
+	if (ASSERT_OK_PTR(skel_unload, "testmod_unload_open"))
+		link_map_free = bpf_program__attach(skel_unload->progs.trace_map_free);
+	struct_ops_module__destroy(skel);
+
+	if (!ASSERT_OK_PTR(link_map_free, "create_link_map_free"))
+		return;
+
+	/* Wait for the struct_ops map to be freed. Struct_ops maps hold a
+	 * refcount to the module btf. And, this function unloads and then
+	 * loads bpf_testmod. Without waiting the map to be freed, the next
+	 * test may fail to unload the bpf_testmod module since the map is
+	 * still holding a refcnt to the module.
+	 */
+	for (i = 0; i < 10; i++) {
+		if (skel_unload->bss->bpf_testmod_put)
+			break;
+		usleep(100000);
+	}
+	ASSERT_EQ(skel_unload->bss->bpf_testmod_put, 1, "map_free");
+
+	bpf_link__destroy(link_map_free);
+	testmod_unload__destroy(skel_unload);
+}
+
+static void test_load_without_module(void)
+{
+	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
+	struct struct_ops_module *skel;

tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c line 74

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c new file +10 -0
+	int err;
+
+	err = unload_bpf_testmod(false);
+	if (!ASSERT_OK(err, "unload_bpf_testmod"))
+		return;
+
+	skel = struct_ops_module__open_opts(&opts);
+	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
+		goto cleanup;
+	err = struct_ops_module__load(skel);

Both the module btf and the .ko itself are gone from the kernel now?
This is basically testing libbpf cannot find 'struct bpf_testmod_ops' from the running kernel?

How about create another struct_ops_module_notfound.c bpf program:
SEC(".struct_ops.link")

struct bpf_testmod_ops_notfound testmod_1 = {
	.test_1 = (void *)test_1,
	.test_2 = (void *)test_2,
};

and avoid the usleep() wait and the unload_bpf_testmod()?

Quoted 18 lines
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c new file +18 -0
+	ASSERT_ERR(err, "struct_ops_module_load");
+
+	struct_ops_module__destroy(skel);
+
+cleanup:
+	/* Without this, the next test may fail */
+	load_bpf_testmod(false);
+}
+
+void serial_test_struct_ops_module(void)
+{
+	if (test__start_subtest("regular_load"))
+		test_regular_load();
+
+	if (test__start_subtest("load_without_module"))
+		test_load_without_module();
+}
+
#35 lore raw

Re: [PATCH bpf-next v13 13/14] selftests/bpf: test case for register_bpf_struct_ops().

Quoted 77 lines · Martin KaFai Lau
Quoted 76 lines · On 12/8/23 4:27 PM, [email protected] wrote:
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c new file +38 -0
new file mode 100644
index 000000000000..55a4c6ed92aa
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
+#include <test_progs.h>
+#include <time.h>
+
+#include "struct_ops_module.skel.h"
+#include "testmod_unload.skel.h"
+
+static void test_regular_load(void)
+{
+    DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
+    struct struct_ops_module *skel;
+    struct testmod_unload *skel_unload;
+    struct bpf_link *link_map_free = NULL;
+    struct bpf_link *link;
+    int err, i;
+
+    skel = struct_ops_module__open_opts(&opts);
+    if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
+        return;
+
+    err = struct_ops_module__load(skel);
+    if (!ASSERT_OK(err, "struct_ops_module_load"))
+        goto cleanup;
+
+    link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
+    ASSERT_OK_PTR(link, "attach_test_mod_1");
+
+    /* test_2() will be called from bpf_dummy_reg() in bpf_testmod.c */
+    ASSERT_EQ(skel->bss->test_2_result, 7, "test_2_result");
+
+    bpf_link__destroy(link);
+
+cleanup:
+    skel_unload = testmod_unload__open_and_load();
+
+    if (ASSERT_OK_PTR(skel_unload, "testmod_unload_open"))
+        link_map_free = 

bpf_program__attach(skel_unload->progs.trace_map_free); +    struct_ops_module__destroy(skel);

tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c new file +30 -0
+
+    if (!ASSERT_OK_PTR(link_map_free, "create_link_map_free"))
+        return;
+
+    /* Wait for the struct_ops map to be freed. Struct_ops maps hold a
+     * refcount to the module btf. And, this function unloads and then
+     * loads bpf_testmod. Without waiting the map to be freed, the next
+     * test may fail to unload the bpf_testmod module since the map is
+     * still holding a refcnt to the module.
+     */
+    for (i = 0; i < 10; i++) {
+        if (skel_unload->bss->bpf_testmod_put)
+            break;
+        usleep(100000);
+    }
+    ASSERT_EQ(skel_unload->bss->bpf_testmod_put, 1, "map_free");
+
+    bpf_link__destroy(link_map_free);
+    testmod_unload__destroy(skel_unload);
+}
+
+static void test_load_without_module(void)
+{
+    DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
+    struct struct_ops_module *skel;
+    int err;
+
+    err = unload_bpf_testmod(false);
+    if (!ASSERT_OK(err, "unload_bpf_testmod"))
+        return;
Quoted 8 lines
Quoted 5 lines

+
+    skel = struct_ops_module__open_opts(&opts); +    if (!ASSERT_OK_PTR(skel, "struct_ops_module_open")) +        goto cleanup;
+    err = struct_ops_module__load(skel);

Both the module btf and the .ko itself are gone from the kernel now?
This is basically testing libbpf cannot find 'struct bpf_testmod_ops' from the running kernel?

Yes, you are right! So, I just rewrote this by calling bpf_map_create() instead of calling the skeleton. To simplify the test, I actually use bpf_map_info of an existing map created from the skeleton as inputs to bpf_map_create(). And, the btf_obj_id (or btf_vmlinux_id) is used and tested here.

Quoted 9 lines

How about create another struct_ops_module_notfound.c bpf program:
SEC(".struct_ops.link")
struct bpf_testmod_ops_notfound testmod_1 = {
    .test_1 = (void *)test_1,
    .test_2 = (void *)test_2,
};

and avoid the usleep() wait and the unload_bpf_testmod()?

In order to skip finding module btf for using bpf_map_create(), I use the skeleton to create a map first to get its bpf_map_info. So, it still needs to load and unload the same module.

Quoted 21 lines
Quoted 19 lines

+    ASSERT_ERR(err, "struct_ops_module_load");

tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c new file +17 -0
+
+    struct_ops_module__destroy(skel);
+
+cleanup:
+    /* Without this, the next test may fail */
+    load_bpf_testmod(false);
+}
+
+void serial_test_struct_ops_module(void)
+{
+    if (test__start_subtest("regular_load"))
+        test_regular_load();
+
+    if (test__start_subtest("load_without_module"))
+        test_load_without_module();
+}
+
#36 lore raw

PATCH v13 14/14 bpf: pass btf object id in bpf_map_info.

In reply to [email protected]

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

Include btf object id (btf_obj_id) in bpf_map_info so that tools (ex:
bpftools struct_ops dump) know the correct btf from the kernel to look up type information of struct_ops types.

Since struct_ops types can be defined and registered in a module. The type information of a struct_ops type are defined in the btf of the module defining it. The userspace tools need to know which btf is for the module defining a struct_ops type.

---
 include/linux/bpf.h            | 1 +
 include/uapi/linux/bpf.h       | 2 +-
 kernel/bpf/bpf_struct_ops.c    | 7 +++++++
 kernel/bpf/syscall.c           | 2 ++
 tools/include/uapi/linux/bpf.h | 2 +-
 5 files changed, 12 insertions(+), 2 deletions(-)
include/linux/bpf.h +1 -0
index c881befa35f5..26103d8a4374 100644
@@ -3350,5 +3350,6 @@ struct bpf_struct_ops_##_name {					\
 int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
 			     struct btf *btf,
 			     struct bpf_verifier_log *log);
+void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map);
 
 #endif /* _LINUX_BPF_H */
include/uapi/linux/bpf.h +1 -1
index 5c3838a97554..716c6b28764d 100644
@@ -6534,7 +6534,7 @@ struct bpf_map_info {
 	__u32 btf_id;
 	__u32 btf_key_type_id;
 	__u32 btf_value_type_id;
-	__u32 :32;	/* alignment pad */
+	__u32 btf_obj_id;
 	__u64 map_extra;
 } __attribute__((aligned(8)));
 
kernel/bpf/bpf_struct_ops.c +7 -0
index fd26716fa0f9..51c0de75aa85 100644
@@ -979,3 +979,10 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
 	kfree(link);
 	return err;
 }
+
+void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
+{
+	struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
+
+	info->btf_obj_id = btf_obj_id(st_map->btf);
+}
kernel/bpf/syscall.c +2 -0
index 4aced7e58904..3cab56cd02ff 100644
@@ -4715,6 +4715,8 @@ static int bpf_map_get_info_by_fd(struct file *file,
 		info.btf_value_type_id = map->btf_value_type_id;
 	}
 	info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
+	if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS)
+		bpf_map_struct_ops_info_fill(&info, map);
 
 	if (bpf_map_is_offloaded(map)) {
 		err = bpf_map_offload_info_fill(&info, map);
tools/include/uapi/linux/bpf.h +1 -1
index 5c3838a97554..716c6b28764d 100644
@@ -6534,7 +6534,7 @@ struct bpf_map_info {
 	__u32 btf_id;
 	__u32 btf_key_type_id;
 	__u32 btf_value_type_id;
-	__u32 :32;	/* alignment pad */
+	__u32 btf_obj_id;
 	__u64 map_extra;
 } __attribute__((aligned(8)));
 
Signature · 1 line
2.34.1
#37 lore raw

Re: [PATCH bpf-next v13 14/14] bpf: pass btf object id in bpf_map_info.

Quoted 25 lines · On 12/8/23 4:27 PM, [email protected] wrote:

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

Include btf object id (btf_obj_id) in bpf_map_info so that tools (ex:
bpftools struct_ops dump) know the correct btf from the kernel to look up type information of struct_ops types.

Since struct_ops types can be defined and registered in a module. The type information of a struct_ops type are defined in the btf of the module defining it. The userspace tools need to know which btf is for the module defining a struct_ops type.

---
  include/linux/bpf.h            | 1 +
  include/uapi/linux/bpf.h       | 2 +-
  kernel/bpf/bpf_struct_ops.c    | 7 +++++++
  kernel/bpf/syscall.c           | 2 ++
  tools/include/uapi/linux/bpf.h | 2 +-
  5 files changed, 12 insertions(+), 2 deletions(-)
include/linux/bpf.h +0 -0
index c881befa35f5..26103d8a4374 100644

include/linux/bpf.h struct bpf_struct_ops_##_name { \ · line 3353

Quoted 4 lines
include/linux/bpf.h +1 -0
@@ -3350,5 +3350,6 @@ struct bpf_struct_ops_##_name {					\
  int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
  			     struct btf *btf,
  			     struct bpf_verifier_log *log);
+void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map);

This needs to be in the CONFIG_BPF_JIT guard also.

include/uapi/linux/bpf.h struct bpf_map_info { · line 6537

Quoted 12 lines
include/linux/bpf.h +0 -0
  #endif /* _LINUX_BPF_H */
include/uapi/linux/bpf.h +1 -1
index 5c3838a97554..716c6b28764d 100644
@@ -6534,7 +6534,7 @@ struct bpf_map_info {
  	__u32 btf_id;
  	__u32 btf_key_type_id;
  	__u32 btf_value_type_id;
-	__u32 :32;	/* alignment pad */
+	__u32 btf_obj_id;

may be "btf_vmlinux_id" to make it clear it is a kernel btf and should be used with map_info->btf_vmlinux_value_type_id.

Quoted 23 lines
include/uapi/linux/bpf.h +0 -0
  	__u64 map_extra;
  } __attribute__((aligned(8)));
 
kernel/bpf/bpf_struct_ops.c +7 -0
index fd26716fa0f9..51c0de75aa85 100644
@@ -979,3 +979,10 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
  	kfree(link);
  	return err;
  }
+
+void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
+{
+	struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
+
+	info->btf_obj_id = btf_obj_id(st_map->btf);
+}
kernel/bpf/syscall.c +0 -0
index 4aced7e58904..3cab56cd02ff 100644

kernel/bpf/syscall.c static int bpf_map_get_info_by_fd(struct file *file, · line 4719

Quoted 5 lines
kernel/bpf/syscall.c +2 -0
@@ -4715,6 +4715,8 @@ static int bpf_map_get_info_by_fd(struct file *file,
  		info.btf_value_type_id = map->btf_value_type_id;
  	}
  	info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
+	if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS)
+		bpf_map_struct_ops_info_fill(&info, map);

This patch should be moved earlier in the set instead of after the selftest patch 13. May be after patch 5 where st_map->btf is added.

and where is the test for this?

Quoted 16 lines
kernel/bpf/syscall.c +0 -0
  	if (bpf_map_is_offloaded(map)) {
  		err = bpf_map_offload_info_fill(&info, map);
tools/include/uapi/linux/bpf.h +1 -1
index 5c3838a97554..716c6b28764d 100644
@@ -6534,7 +6534,7 @@ struct bpf_map_info {
  	__u32 btf_id;
  	__u32 btf_key_type_id;
  	__u32 btf_value_type_id;
-	__u32 :32;	/* alignment pad */
+	__u32 btf_obj_id;
  	__u64 map_extra;
  } __attribute__((aligned(8)));
 
#38 lore raw

Re: [PATCH bpf-next v13 14/14] bpf: pass btf object id in bpf_map_info.

Quoted 21 lines · Martin KaFai Lau
Quoted 20 lines · On 12/8/23 4:27 PM, [email protected] wrote:

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

Include btf object id (btf_obj_id) in bpf_map_info so that tools (ex:
bpftools struct_ops dump) know the correct btf from the kernel to look up type information of struct_ops types.

Since struct_ops types can be defined and registered in a module. The type information of a struct_ops type are defined in the btf of the module defining it.  The userspace tools need to know which btf is for the module defining a struct_ops type.

---
  include/linux/bpf.h            | 1 +
  include/uapi/linux/bpf.h       | 2 +-
  kernel/bpf/bpf_struct_ops.c    | 7 +++++++
  kernel/bpf/syscall.c           | 2 ++
  tools/include/uapi/linux/bpf.h | 2 +-

  5 files changed, 12 insertions(+), 2 deletions(-)
Quoted 13 lines

include/linux/bpf.h struct bpf_struct_ops_##_name · line 3355

Quoted 11 lines
include/linux/bpf.h +1 -0
index c881befa35f5..26103d8a4374 100644
@@ -3350,5 +3350,6 @@ struct bpf_struct_ops_##_name 
 {                    \
   int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
                    struct btf *btf,
                    struct bpf_verifier_log *log);
+void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct 
 bpf_map *map);

This needs to be in the CONFIG_BPF_JIT guard also.

Got it!

Quoted 14 lines

include/uapi/linux/bpf.h struct bpf_map_info { · line 6537

Quoted 12 lines

  #endif /* _LINUX_BPF_H */

include/uapi/linux/bpf.h +1 -1
index 5c3838a97554..716c6b28764d 100644
@@ -6534,7 +6534,7 @@ struct bpf_map_info {
       __u32 btf_id;
       __u32 btf_key_type_id;
       __u32 btf_value_type_id;
-    __u32 :32;    /* alignment pad */
+    __u32 btf_obj_id;

may be "btf_vmlinux_id" to make it clear it is a kernel btf and should be used with map_info->btf_vmlinux_value_type_id.

Sure!

Quoted 25 lines
Quoted 25 lines
include/uapi/linux/bpf.h +0 -0
       __u64 map_extra;
   } __attribute__((aligned(8)));
kernel/bpf/bpf_struct_ops.c +2 -0
index fd26716fa0f9..51c0de75aa85 100644
@@ -979,3 +979,10 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
       kfree(link);
       return err;
   }
+
+void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct 

bpf_map *map)
+{
+    struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;

kernel/bpf/bpf_struct_ops.c +3 -0
+
+    info->btf_obj_id = btf_obj_id(st_map->btf);
+}
kernel/bpf/syscall.c +0 -0
index 4aced7e58904..3cab56cd02ff 100644
@@ -4715,6 +4715,8 @@ static int bpf_map_get_info_by_fd(struct file 
Quoted 8 lines

kernel/bpf/syscall.c static int bpf_map_get_info_by_fd(struct file *file, · line 4719

Quoted 6 lines
kernel/bpf/syscall.c +2 -0
 *file,
           info.btf_value_type_id = map->btf_value_type_id;
       }
       info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
+    if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS)
+        bpf_map_struct_ops_info_fill(&info, map);

This patch should be moved earlier in the set instead of after the selftest patch 13. May be after patch 5 where st_map->btf is added.

No problem

Quoted 2 lines

and where is the test for this?

I use bpf_map_info as a part of calling bpf_map_create() while testmod.ko is unloaded. It check if this change work as well.

Quoted 16 lines
Quoted 15 lines
kernel/bpf/syscall.c +0 -0
       if (bpf_map_is_offloaded(map)) {
           err = bpf_map_offload_info_fill(&info, map);
tools/include/uapi/linux/bpf.h +1 -1
index 5c3838a97554..716c6b28764d 100644
@@ -6534,7 +6534,7 @@ struct bpf_map_info {
       __u32 btf_id;
       __u32 btf_key_type_id;
       __u32 btf_value_type_id;
-    __u32 :32;    /* alignment pad */
+    __u32 btf_obj_id;
       __u64 map_extra;
   } __attribute__((aligned(8)));