Skip to content

Commit 5b84bd1

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
libbpf: Add support for BTF_KIND_TAG
Add BTF_KIND_TAG support for parsing and dedup. Also added sanitization for BTF_KIND_TAG. If BTF_KIND_TAG is not supported in the kernel, sanitize it to INTs. Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 30025e8 commit 5b84bd1

File tree

6 files changed

+118
-3
lines changed

6 files changed

+118
-3
lines changed

tools/lib/bpf/btf.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ static int btf_type_size(const struct btf_type *t)
304304
return base_size + sizeof(struct btf_var);
305305
case BTF_KIND_DATASEC:
306306
return base_size + vlen * sizeof(struct btf_var_secinfo);
307+
case BTF_KIND_TAG:
308+
return base_size + sizeof(struct btf_tag);
307309
default:
308310
pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
309311
return -EINVAL;
@@ -376,6 +378,9 @@ static int btf_bswap_type_rest(struct btf_type *t)
376378
v->size = bswap_32(v->size);
377379
}
378380
return 0;
381+
case BTF_KIND_TAG:
382+
btf_tag(t)->component_idx = bswap_32(btf_tag(t)->component_idx);
383+
return 0;
379384
default:
380385
pr_debug("Unsupported BTF_KIND:%u\n", btf_kind(t));
381386
return -EINVAL;
@@ -586,6 +591,7 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
586591
case BTF_KIND_CONST:
587592
case BTF_KIND_RESTRICT:
588593
case BTF_KIND_VAR:
594+
case BTF_KIND_TAG:
589595
type_id = t->type;
590596
break;
591597
case BTF_KIND_ARRAY:
@@ -2440,6 +2446,48 @@ int btf__add_datasec_var_info(struct btf *btf, int var_type_id, __u32 offset, __
24402446
return 0;
24412447
}
24422448

2449+
/*
2450+
* Append new BTF_KIND_TAG type with:
2451+
* - *value* - non-empty/non-NULL string;
2452+
* - *ref_type_id* - referenced type ID, it might not exist yet;
2453+
* - *component_idx* - -1 for tagging reference type, otherwise struct/union
2454+
* member or function argument index;
2455+
* Returns:
2456+
* - >0, type ID of newly added BTF type;
2457+
* - <0, on error.
2458+
*/
2459+
int btf__add_tag(struct btf *btf, const char *value, int ref_type_id,
2460+
int component_idx)
2461+
{
2462+
struct btf_type *t;
2463+
int sz, value_off;
2464+
2465+
if (!value || !value[0] || component_idx < -1)
2466+
return libbpf_err(-EINVAL);
2467+
2468+
if (validate_type_id(ref_type_id))
2469+
return libbpf_err(-EINVAL);
2470+
2471+
if (btf_ensure_modifiable(btf))
2472+
return libbpf_err(-ENOMEM);
2473+
2474+
sz = sizeof(struct btf_type) + sizeof(struct btf_tag);
2475+
t = btf_add_type_mem(btf, sz);
2476+
if (!t)
2477+
return libbpf_err(-ENOMEM);
2478+
2479+
value_off = btf__add_str(btf, value);
2480+
if (value_off < 0)
2481+
return value_off;
2482+
2483+
t->name_off = value_off;
2484+
t->info = btf_type_info(BTF_KIND_TAG, 0, false);
2485+
t->type = ref_type_id;
2486+
btf_tag(t)->component_idx = component_idx;
2487+
2488+
return btf_commit_type(btf, sz);
2489+
}
2490+
24432491
struct btf_ext_sec_setup_param {
24442492
__u32 off;
24452493
__u32 len;
@@ -3535,6 +3583,7 @@ static int btf_dedup_prep(struct btf_dedup *d)
35353583
h = btf_hash_common(t);
35363584
break;
35373585
case BTF_KIND_INT:
3586+
case BTF_KIND_TAG:
35383587
h = btf_hash_int_tag(t);
35393588
break;
35403589
case BTF_KIND_ENUM:
@@ -3590,6 +3639,7 @@ static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id)
35903639
case BTF_KIND_FUNC_PROTO:
35913640
case BTF_KIND_VAR:
35923641
case BTF_KIND_DATASEC:
3642+
case BTF_KIND_TAG:
35933643
return 0;
35943644

35953645
case BTF_KIND_INT:
@@ -4210,6 +4260,23 @@ static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
42104260
}
42114261
break;
42124262

4263+
case BTF_KIND_TAG:
4264+
ref_type_id = btf_dedup_ref_type(d, t->type);
4265+
if (ref_type_id < 0)
4266+
return ref_type_id;
4267+
t->type = ref_type_id;
4268+
4269+
h = btf_hash_int_tag(t);
4270+
for_each_dedup_cand(d, hash_entry, h) {
4271+
cand_id = (__u32)(long)hash_entry->value;
4272+
cand = btf_type_by_id(d->btf, cand_id);
4273+
if (btf_equal_int_tag(t, cand)) {
4274+
new_id = cand_id;
4275+
break;
4276+
}
4277+
}
4278+
break;
4279+
42134280
case BTF_KIND_ARRAY: {
42144281
struct btf_array *info = btf_array(t);
42154282

@@ -4482,6 +4549,7 @@ int btf_type_visit_type_ids(struct btf_type *t, type_id_visit_fn visit, void *ct
44824549
case BTF_KIND_TYPEDEF:
44834550
case BTF_KIND_FUNC:
44844551
case BTF_KIND_VAR:
4552+
case BTF_KIND_TAG:
44854553
return visit(&t->type, ctx);
44864554

44874555
case BTF_KIND_ARRAY: {

tools/lib/bpf/btf.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz
143143
LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,
144144
__u32 offset, __u32 byte_sz);
145145

146+
/* tag construction API */
147+
LIBBPF_API int btf__add_tag(struct btf *btf, const char *value, int ref_type_id,
148+
int component_idx);
149+
146150
struct btf_dedup_opts {
147151
unsigned int dedup_table_size;
148152
bool dont_resolve_fwds;
@@ -330,6 +334,11 @@ static inline bool btf_is_float(const struct btf_type *t)
330334
return btf_kind(t) == BTF_KIND_FLOAT;
331335
}
332336

337+
static inline bool btf_is_tag(const struct btf_type *t)
338+
{
339+
return btf_kind(t) == BTF_KIND_TAG;
340+
}
341+
333342
static inline __u8 btf_int_encoding(const struct btf_type *t)
334343
{
335344
return BTF_INT_ENCODING(*(__u32 *)(t + 1));
@@ -398,6 +407,12 @@ btf_var_secinfos(const struct btf_type *t)
398407
return (struct btf_var_secinfo *)(t + 1);
399408
}
400409

410+
struct btf_tag;
411+
static inline struct btf_tag *btf_tag(const struct btf_type *t)
412+
{
413+
return (struct btf_tag *)(t + 1);
414+
}
415+
401416
#ifdef __cplusplus
402417
} /* extern "C" */
403418
#endif

tools/lib/bpf/btf_dump.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ static int btf_dump_mark_referenced(struct btf_dump *d)
316316
case BTF_KIND_TYPEDEF:
317317
case BTF_KIND_FUNC:
318318
case BTF_KIND_VAR:
319+
case BTF_KIND_TAG:
319320
d->type_states[t->type].referenced = 1;
320321
break;
321322

@@ -583,6 +584,7 @@ static int btf_dump_order_type(struct btf_dump *d, __u32 id, bool through_ptr)
583584
case BTF_KIND_FUNC:
584585
case BTF_KIND_VAR:
585586
case BTF_KIND_DATASEC:
587+
case BTF_KIND_TAG:
586588
d->type_states[id].order_state = ORDERED;
587589
return 0;
588590

@@ -2215,6 +2217,7 @@ static int btf_dump_dump_type_data(struct btf_dump *d,
22152217
case BTF_KIND_FWD:
22162218
case BTF_KIND_FUNC:
22172219
case BTF_KIND_FUNC_PROTO:
2220+
case BTF_KIND_TAG:
22182221
err = btf_dump_unsupported_data(d, t, id);
22192222
break;
22202223
case BTF_KIND_INT:

tools/lib/bpf/libbpf.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ enum kern_feature_id {
195195
FEAT_BTF_FLOAT,
196196
/* BPF perf link support */
197197
FEAT_PERF_LINK,
198+
/* BTF_KIND_TAG support */
199+
FEAT_BTF_TAG,
198200
__FEAT_CNT,
199201
};
200202

@@ -1986,6 +1988,7 @@ static const char *__btf_kind_str(__u16 kind)
19861988
case BTF_KIND_VAR: return "var";
19871989
case BTF_KIND_DATASEC: return "datasec";
19881990
case BTF_KIND_FLOAT: return "float";
1991+
case BTF_KIND_TAG: return "tag";
19891992
default: return "unknown";
19901993
}
19911994
}
@@ -2485,8 +2488,9 @@ static bool btf_needs_sanitization(struct bpf_object *obj)
24852488
bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
24862489
bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
24872490
bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
2491+
bool has_tag = kernel_supports(obj, FEAT_BTF_TAG);
24882492

2489-
return !has_func || !has_datasec || !has_func_global || !has_float;
2493+
return !has_func || !has_datasec || !has_func_global || !has_float || !has_tag;
24902494
}
24912495

24922496
static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
@@ -2495,14 +2499,15 @@ static void bpf_object__sanitize_btf(struct bpf_object *obj, struct btf *btf)
24952499
bool has_datasec = kernel_supports(obj, FEAT_BTF_DATASEC);
24962500
bool has_float = kernel_supports(obj, FEAT_BTF_FLOAT);
24972501
bool has_func = kernel_supports(obj, FEAT_BTF_FUNC);
2502+
bool has_tag = kernel_supports(obj, FEAT_BTF_TAG);
24982503
struct btf_type *t;
24992504
int i, j, vlen;
25002505

25012506
for (i = 1; i <= btf__get_nr_types(btf); i++) {
25022507
t = (struct btf_type *)btf__type_by_id(btf, i);
25032508

2504-
if (!has_datasec && btf_is_var(t)) {
2505-
/* replace VAR with INT */
2509+
if ((!has_datasec && btf_is_var(t)) || (!has_tag && btf_is_tag(t))) {
2510+
/* replace VAR/TAG with INT */
25062511
t->info = BTF_INFO_ENC(BTF_KIND_INT, 0, 0);
25072512
/*
25082513
* using size = 1 is the safest choice, 4 will be too
@@ -4212,6 +4217,23 @@ static int probe_kern_btf_float(void)
42124217
strs, sizeof(strs)));
42134218
}
42144219

4220+
static int probe_kern_btf_tag(void)
4221+
{
4222+
static const char strs[] = "\0tag";
4223+
__u32 types[] = {
4224+
/* int */
4225+
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
4226+
/* VAR x */ /* [2] */
4227+
BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
4228+
BTF_VAR_STATIC,
4229+
/* attr */
4230+
BTF_TYPE_TAG_ENC(1, 2, -1),
4231+
};
4232+
4233+
return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
4234+
strs, sizeof(strs)));
4235+
}
4236+
42154237
static int probe_kern_array_mmap(void)
42164238
{
42174239
struct bpf_create_map_attr attr = {
@@ -4428,6 +4450,9 @@ static struct kern_feature_desc {
44284450
[FEAT_PERF_LINK] = {
44294451
"BPF perf link support", probe_perf_link,
44304452
},
4453+
[FEAT_BTF_TAG] = {
4454+
"BTF_KIND_TAG support", probe_kern_btf_tag,
4455+
},
44314456
};
44324457

44334458
static bool kernel_supports(const struct bpf_object *obj, enum kern_feature_id feat_id)

tools/lib/bpf/libbpf.map

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,6 @@ LIBBPF_0.5.0 {
388388
} LIBBPF_0.4.0;
389389

390390
LIBBPF_0.6.0 {
391+
global:
392+
btf__add_tag;
391393
} LIBBPF_0.5.0;

tools/lib/bpf/libbpf_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
#define BTF_VAR_SECINFO_ENC(type, offset, size) (type), (offset), (size)
7070
#define BTF_TYPE_FLOAT_ENC(name, sz) \
7171
BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)
72+
#define BTF_TYPE_TAG_ENC(value, type, component_idx) \
73+
BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), type), (component_idx)
7274

7375
#ifndef likely
7476
#define likely(x) __builtin_expect(!!(x), 1)

0 commit comments

Comments
 (0)