Skip to content

Commit f31776a

Browse files
D. WytheKernel Patches Daemon
authored andcommitted
libbpf: fix error when st-prefix_ops and ops from differ btf
When a struct_ops named xxx_ops was registered by a module, and it will be used in both built-in modules and the module itself, so that the btf_type of xxx_ops will be present in btf_vmlinux instead of in btf_mod, which means that the btf_type of bpf_struct_ops_xxx_ops and xxx_ops will not be in the same btf. Here are four possible case: +--------+---------------+-------------+---------------------------------+ | | st_ops_xxx_ops| xxx_ops | | +--------+---------------+-------------+---------------------------------+ | case 0 | btf_vmlinux | bft_vmlinux | be used and reg only in vmlinux | +--------+---------------+-------------+---------------------------------+ | case 1 | btf_vmlinux | bpf_mod | INVALID | +--------+---------------+-------------+---------------------------------+ | case 2 | btf_mod | btf_vmlinux | reg in mod but be used both in | | | | | vmlinux and mod. | +--------+---------------+-------------+---------------------------------+ | case 3 | btf_mod | btf_mod | be used and reg only in mod | +--------+---------------+-------------+---------------------------------+ At present, cases 0, 1, and 3 can be correctly identified, because st_ops_xxx_ops is searched from the same btf with xxx_ops. In order to handle case 2 correctly without affecting other cases, we cannot simply change the search method for st_ops_xxx_ops from find_btf_by_prefix_kind() to find_ksym_btf_id(), because in this way, case 1 will not be recognized anymore. To address the issue, we always look for st_ops_xxx_ops first, figure out the btf, and then look for xxx_ops with the very btf to avoid such issue. Fixes: 590a008 ("bpf: libbpf: Add STRUCT_OPS support") Signed-off-by: D. Wythe <alibuda@linux.alibaba.com>
1 parent 3c8683a commit f31776a

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,35 +1005,40 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
10051005
const struct btf_member *kern_data_member;
10061006
struct btf *btf = NULL;
10071007
__s32 kern_vtype_id, kern_type_id;
1008-
char tname[256];
1008+
char tname[256], stname[256];
1009+
int ret;
10091010
__u32 i;
10101011

10111012
snprintf(tname, sizeof(tname), "%.*s",
10121013
(int)bpf_core_essential_name_len(tname_raw), tname_raw);
10131014

1014-
kern_type_id = find_ksym_btf_id(obj, tname, BTF_KIND_STRUCT,
1015+
ret = snprintf(stname, sizeof(stname), "%s%s", STRUCT_OPS_VALUE_PREFIX,
1016+
tname);
1017+
if (ret < 0 || ret >= sizeof(stname))
1018+
return -ENAMETOOLONG;
1019+
1020+
/* Look for the corresponding "map_value" type that will be used
1021+
* in map_update(BPF_MAP_TYPE_STRUCT_OPS) first, figure out the btf
1022+
* and the mod_btf.
1023+
* For example, find "struct bpf_struct_ops_tcp_congestion_ops".
1024+
*/
1025+
kern_vtype_id = find_ksym_btf_id(obj, stname, BTF_KIND_STRUCT,
10151026
&btf, mod_btf);
1027+
if (kern_vtype_id < 0) {
1028+
pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
1029+
stname);
1030+
return kern_vtype_id;
1031+
}
1032+
kern_vtype = btf__type_by_id(btf, kern_vtype_id);
1033+
1034+
kern_type_id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
10161035
if (kern_type_id < 0) {
10171036
pr_warn("struct_ops init_kern: struct %s is not found in kernel BTF\n",
10181037
tname);
10191038
return kern_type_id;
10201039
}
10211040
kern_type = btf__type_by_id(btf, kern_type_id);
10221041

1023-
/* Find the corresponding "map_value" type that will be used
1024-
* in map_update(BPF_MAP_TYPE_STRUCT_OPS). For example,
1025-
* find "struct bpf_struct_ops_tcp_congestion_ops" from the
1026-
* btf_vmlinux.
1027-
*/
1028-
kern_vtype_id = find_btf_by_prefix_kind(btf, STRUCT_OPS_VALUE_PREFIX,
1029-
tname, BTF_KIND_STRUCT);
1030-
if (kern_vtype_id < 0) {
1031-
pr_warn("struct_ops init_kern: struct %s%s is not found in kernel BTF\n",
1032-
STRUCT_OPS_VALUE_PREFIX, tname);
1033-
return kern_vtype_id;
1034-
}
1035-
kern_vtype = btf__type_by_id(btf, kern_vtype_id);
1036-
10371042
/* Find "struct tcp_congestion_ops" from
10381043
* struct bpf_struct_ops_tcp_congestion_ops {
10391044
* [ ... ]
@@ -1046,8 +1051,8 @@ find_struct_ops_kern_types(struct bpf_object *obj, const char *tname_raw,
10461051
break;
10471052
}
10481053
if (i == btf_vlen(kern_vtype)) {
1049-
pr_warn("struct_ops init_kern: struct %s data is not found in struct %s%s\n",
1050-
tname, STRUCT_OPS_VALUE_PREFIX, tname);
1054+
pr_warn("struct_ops init_kern: struct %s data is not found in struct %s\n",
1055+
tname, stname);
10511056
return -EINVAL;
10521057
}
10531058

0 commit comments

Comments
 (0)