Skip to content

Commit 8afb259

Browse files
author
Alexei Starovoitov
committed
Merge branch 'strip-mods-from-global-vars'
Andrii Nakryiko says: ==================== Fix bpftool logic of stripping away const/volatile modifiers for all global variables during BPF skeleton generation. See patch raspberrypi#1 for details on when existing logic breaks and why it's important. Support special .strip_mods=true mode in btf_dump__emit_type_decl. Recent example of when this has caused problems can be found in [0]. [0] iovisor/bcc#2994 (comment) ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 207a573 + 0b20933 commit 8afb259

5 files changed

Lines changed: 28 additions & 21 deletions

File tree

‎tools/bpf/bpftool/gen.c‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static const char *get_map_ident(const struct bpf_map *map)
8888
return NULL;
8989
}
9090

91-
static void codegen_btf_dump_printf(void *ct, const char *fmt, va_list args)
91+
static void codegen_btf_dump_printf(void *ctx, const char *fmt, va_list args)
9292
{
9393
vprintf(fmt, args);
9494
}
@@ -104,17 +104,20 @@ static int codegen_datasec_def(struct bpf_object *obj,
104104
int i, err, off = 0, pad_cnt = 0, vlen = btf_vlen(sec);
105105
const char *sec_ident;
106106
char var_ident[256];
107+
bool strip_mods = false;
107108

108-
if (strcmp(sec_name, ".data") == 0)
109+
if (strcmp(sec_name, ".data") == 0) {
109110
sec_ident = "data";
110-
else if (strcmp(sec_name, ".bss") == 0)
111+
} else if (strcmp(sec_name, ".bss") == 0) {
111112
sec_ident = "bss";
112-
else if (strcmp(sec_name, ".rodata") == 0)
113+
} else if (strcmp(sec_name, ".rodata") == 0) {
113114
sec_ident = "rodata";
114-
else if (strcmp(sec_name, ".kconfig") == 0)
115+
strip_mods = true;
116+
} else if (strcmp(sec_name, ".kconfig") == 0) {
115117
sec_ident = "kconfig";
116-
else
118+
} else {
117119
return 0;
120+
}
118121

119122
printf(" struct %s__%s {\n", obj_name, sec_ident);
120123
for (i = 0; i < vlen; i++, sec_var++) {
@@ -123,16 +126,10 @@ static int codegen_datasec_def(struct bpf_object *obj,
123126
DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts,
124127
.field_name = var_ident,
125128
.indent_level = 2,
129+
.strip_mods = strip_mods,
126130
);
127131
int need_off = sec_var->offset, align_off, align;
128132
__u32 var_type_id = var->type;
129-
const struct btf_type *t;
130-
131-
t = btf__type_by_id(btf, var_type_id);
132-
while (btf_is_mod(t)) {
133-
var_type_id = t->type;
134-
t = btf__type_by_id(btf, var_type_id);
135-
}
136133

137134
if (off > need_off) {
138135
p_err("Something is wrong for %s's variable #%d: need offset %d, already at %d.\n",

‎tools/lib/bpf/btf.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ struct btf_dump_emit_type_decl_opts {
144144
* necessary indentation already
145145
*/
146146
int indent_level;
147+
/* strip all the const/volatile/restrict mods */
148+
bool strip_mods;
147149
};
148-
#define btf_dump_emit_type_decl_opts__last_field indent_level
150+
#define btf_dump_emit_type_decl_opts__last_field strip_mods
149151

150152
LIBBPF_API int
151153
btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,

‎tools/lib/bpf/btf_dump.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ struct btf_dump {
6060
const struct btf_ext *btf_ext;
6161
btf_dump_printf_fn_t printf_fn;
6262
struct btf_dump_opts opts;
63+
bool strip_mods;
6364

6465
/* per-type auxiliary state */
6566
struct btf_dump_type_aux_state *type_states;
@@ -1032,7 +1033,9 @@ int btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
10321033

10331034
fname = OPTS_GET(opts, field_name, "");
10341035
lvl = OPTS_GET(opts, indent_level, 0);
1036+
d->strip_mods = OPTS_GET(opts, strip_mods, false);
10351037
btf_dump_emit_type_decl(d, id, fname, lvl);
1038+
d->strip_mods = false;
10361039
return 0;
10371040
}
10381041

@@ -1045,6 +1048,10 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
10451048

10461049
stack_start = d->decl_stack_cnt;
10471050
for (;;) {
1051+
t = btf__type_by_id(d->btf, id);
1052+
if (d->strip_mods && btf_is_mod(t))
1053+
goto skip_mod;
1054+
10481055
err = btf_dump_push_decl_stack_id(d, id);
10491056
if (err < 0) {
10501057
/*
@@ -1056,12 +1063,11 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
10561063
d->decl_stack_cnt = stack_start;
10571064
return;
10581065
}
1059-
1066+
skip_mod:
10601067
/* VOID */
10611068
if (id == 0)
10621069
break;
10631070

1064-
t = btf__type_by_id(d->btf, id);
10651071
switch (btf_kind(t)) {
10661072
case BTF_KIND_PTR:
10671073
case BTF_KIND_VOLATILE:

‎tools/testing/selftests/bpf/prog_tests/skeleton.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ void test_skeleton(void)
4141
CHECK(bss->in4 != 0, "in4", "got %lld != exp %lld\n", bss->in4, 0LL);
4242
CHECK(bss->out4 != 0, "out4", "got %lld != exp %lld\n", bss->out4, 0LL);
4343

44-
CHECK(rodata->in6 != 0, "in6", "got %d != exp %d\n", rodata->in6, 0);
44+
CHECK(rodata->in.in6 != 0, "in6", "got %d != exp %d\n", rodata->in.in6, 0);
4545
CHECK(bss->out6 != 0, "out6", "got %d != exp %d\n", bss->out6, 0);
4646

4747
/* validate we can pre-setup global variables, even in .bss */
4848
data->in1 = 10;
4949
data->in2 = 11;
5050
bss->in3 = 12;
5151
bss->in4 = 13;
52-
rodata->in6 = 14;
52+
rodata->in.in6 = 14;
5353

5454
err = test_skeleton__load(skel);
5555
if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
@@ -60,7 +60,7 @@ void test_skeleton(void)
6060
CHECK(data->in2 != 11, "in2", "got %lld != exp %lld\n", data->in2, 11LL);
6161
CHECK(bss->in3 != 12, "in3", "got %d != exp %d\n", bss->in3, 12);
6262
CHECK(bss->in4 != 13, "in4", "got %lld != exp %lld\n", bss->in4, 13LL);
63-
CHECK(rodata->in6 != 14, "in6", "got %d != exp %d\n", rodata->in6, 14);
63+
CHECK(rodata->in.in6 != 14, "in6", "got %d != exp %d\n", rodata->in.in6, 14);
6464

6565
/* now set new values and attach to get them into outX variables */
6666
data->in1 = 1;

‎tools/testing/selftests/bpf/progs/test_skeleton.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ long long in4 __attribute__((aligned(64))) = 0;
2020
struct s in5 = {};
2121

2222
/* .rodata section */
23-
const volatile int in6 = 0;
23+
const volatile struct {
24+
const int in6;
25+
} in = {};
2426

2527
/* .data section */
2628
int out1 = -1;
@@ -46,7 +48,7 @@ int handler(const void *ctx)
4648
out3 = in3;
4749
out4 = in4;
4850
out5 = in5;
49-
out6 = in6;
51+
out6 = in.in6;
5052

5153
bpf_syscall = CONFIG_BPF_SYSCALL;
5254
kern_ver = LINUX_KERNEL_VERSION;

0 commit comments

Comments
 (0)