BPF Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables
@ 2020-07-01  6:45 Andrii Nakryiko
  2020-07-01  6:45 ` [PATCH bpf-next 1/3] libbpf: support stripping modifiers for btf_dump Andrii Nakryiko
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Andrii Nakryiko @ 2020-07-01  6:45 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Anton Protopopov

Fix bpftool logic of stripping away const/volatile modifiers for all global
variables during BPF skeleton generation. See patch #1 for details on when
existing logic breaks and why it's important. Support special .strip_mods=true
mode in btf_dump. Add selftests validating that everything works as expected.

Recent example of when this has caused problems can be found in [0].

  [0] https://github.com/iovisor/bcc/pull/2994#issuecomment-650588533

Cc: Anton Protopopov <a.s.protopopov@gmail.com>

Andrii Nakryiko (3):
  libbpf: support stripping modifiers for btf_dump
  selftests/bpf: add selftest validating btf_dump's mod-stripping output
  tools/bpftool: strip away modifiers from global variables

 tools/bpf/bpftool/gen.c                       | 13 ++---
 tools/lib/bpf/btf.h                           |  6 +++
 tools/lib/bpf/btf_dump.c                      | 18 +++++--
 .../selftests/bpf/prog_tests/btf_dump.c       |  5 +-
 .../selftests/bpf/prog_tests/skeleton.c       |  6 +--
 .../bpf/progs/btf_dump_test_case_strip_mods.c | 50 +++++++++++++++++++
 .../selftests/bpf/progs/test_skeleton.c       |  6 ++-
 7 files changed, 84 insertions(+), 20 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_strip_mods.c

-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH bpf-next 1/3] libbpf: support stripping modifiers for btf_dump
  2020-07-01  6:45 [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables Andrii Nakryiko
@ 2020-07-01  6:45 ` Andrii Nakryiko
  2020-07-01  6:45 ` [PATCH bpf-next 2/3] selftests/bpf: add selftest testin btf_dump's mod-stripping output Andrii Nakryiko
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 10+ messages in thread
From: Andrii Nakryiko @ 2020-07-01  6:45 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Anton Protopopov,
	Daniel Xu

One important use case when emitting const/volatile/restrict is undesirable is
BPF skeleton generation of DATASEC layout. These are further memory-mapped and
can be written/read from user-space directly.

For important case of .rodata variables, bpftool strips away first-level
modifiers, to make their use on user-space side simple and not requiring extra
type casts to override compiler complaining about writing to const variables.

This logic works mostly fine, but breaks in some more complicated cases. E.g.:

    const volatile int params[10];

Because in BTF it's a chain of ARRAY -> CONST -> VOLATILE -> INT, bpftool
stops at ARRAY and doesn't strip CONST and VOLATILE. In skeleton this variable
will be emitted as is. So when used from user-space, compiler will complain
about writing to const array. This is problematic, as also mentioned in [0].

To solve this for arrays and other non-trivial cases (e.g., inner
const/volatile fields inside the struct), teach btf_dump to strip away any
modifier, when requested.

This patch converts existing struct btf_dump_opts to modern opts "framework"
with size field and easily extensible in the future with backwards/forward
compatibility. While this is a breaking change, there are only two known
clients of this API: bpftool and bpftrace. bpftool hasn't used opts and just
passed NULL, so is not affected and subsequent patch makes it use using
DECLARE_LIBBPF_OPTS() macro. bpftrace does use opts and I'll work with
bpftrace maintainers to adopt to a new opts style. While a bit painful, it
seems like a better strategy long-term, instead of maintaining two sets of
btf_dump opts and constructors.

  [0] https://github.com/iovisor/bcc/pull/2994#issuecomment-650588533

Cc: Daniel Xu <dlxu@fb.com>
Reported-by: Anton Protopopov <a.s.protopopov@gmail.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/btf.h      |  6 ++++++
 tools/lib/bpf/btf_dump.c | 18 +++++++++++++-----
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h
index 06cd1731c154..5c2acca8d7f4 100644
--- a/tools/lib/bpf/btf.h
+++ b/tools/lib/bpf/btf.h
@@ -115,8 +115,14 @@ LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
 struct btf_dump;
 
 struct btf_dump_opts {
+	/* size of this struct, for backward/forward compatibility */
+	size_t sz;
+	/* extra context passed to print callback */
 	void *ctx;
+	/* strip all the const/volatile/restrict mods */
+	bool strip_mods;
 };
+#define btf_dump_opts__last_field strip_mods
 
 typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
 
diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c
index bbb430317260..4b843bbd8657 100644
--- a/tools/lib/bpf/btf_dump.c
+++ b/tools/lib/bpf/btf_dump.c
@@ -59,7 +59,8 @@ struct btf_dump {
 	const struct btf *btf;
 	const struct btf_ext *btf_ext;
 	btf_dump_printf_fn_t printf_fn;
-	struct btf_dump_opts opts;
+	void *print_ctx;
+	bool strip_mods;
 
 	/* per-type auxiliary state */
 	struct btf_dump_type_aux_state *type_states;
@@ -115,7 +116,7 @@ static void btf_dump_printf(const struct btf_dump *d, const char *fmt, ...)
 	va_list args;
 
 	va_start(args, fmt);
-	d->printf_fn(d->opts.ctx, fmt, args);
+	d->printf_fn(d->print_ctx, fmt, args);
 	va_end(args);
 }
 
@@ -129,6 +130,9 @@ struct btf_dump *btf_dump__new(const struct btf *btf,
 	struct btf_dump *d;
 	int err;
 
+	if (!OPTS_VALID(opts, btf_dump_opts))
+		return ERR_PTR(-EINVAL);
+
 	d = calloc(1, sizeof(struct btf_dump));
 	if (!d)
 		return ERR_PTR(-ENOMEM);
@@ -136,7 +140,8 @@ struct btf_dump *btf_dump__new(const struct btf *btf,
 	d->btf = btf;
 	d->btf_ext = btf_ext;
 	d->printf_fn = printf_fn;
-	d->opts.ctx = opts ? opts->ctx : NULL;
+	d->print_ctx = OPTS_GET(opts, ctx, NULL);
+	d->strip_mods = OPTS_GET(opts, strip_mods, false);
 
 	d->type_names = hashmap__new(str_hash_fn, str_equal_fn, NULL);
 	if (IS_ERR(d->type_names)) {
@@ -1045,6 +1050,10 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
 
 	stack_start = d->decl_stack_cnt;
 	for (;;) {
+		t = btf__type_by_id(d->btf, id);
+		if (d->strip_mods && btf_is_mod(t))
+			goto skip_mod;
+
 		err = btf_dump_push_decl_stack_id(d, id);
 		if (err < 0) {
 			/*
@@ -1056,12 +1065,11 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
 			d->decl_stack_cnt = stack_start;
 			return;
 		}
-
+skip_mod:
 		/* VOID */
 		if (id == 0)
 			break;
 
-		t = btf__type_by_id(d->btf, id);
 		switch (btf_kind(t)) {
 		case BTF_KIND_PTR:
 		case BTF_KIND_VOLATILE:
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH bpf-next 2/3] selftests/bpf: add selftest testin btf_dump's mod-stripping output
  2020-07-01  6:45 [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables Andrii Nakryiko
  2020-07-01  6:45 ` [PATCH bpf-next 1/3] libbpf: support stripping modifiers for btf_dump Andrii Nakryiko
@ 2020-07-01  6:45 ` Andrii Nakryiko
  2020-07-01  6:47   ` Andrii Nakryiko
  2020-07-01  6:45 ` [PATCH bpf-next 2/3] selftests/bpf: add selftest validating " Andrii Nakryiko
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: Andrii Nakryiko @ 2020-07-01  6:45 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Anton Protopopov

Add selftest validating that .strip_mods=true works.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 .../selftests/bpf/prog_tests/btf_dump.c       |  5 +-
 .../bpf/progs/btf_dump_test_case_strip_mods.c | 50 +++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_strip_mods.c

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
index cb33a7ee4e04..112b653b9c80 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
@@ -21,6 +21,8 @@ static struct btf_dump_test_case {
 	{"btf_dump: bitfields", "btf_dump_test_case_bitfields", {}},
 	{"btf_dump: multidim", "btf_dump_test_case_multidim", {}},
 	{"btf_dump: namespacing", "btf_dump_test_case_namespacing", {}},
+	{"btf_dump: strip mods", "btf_dump_test_case_strip_mods",
+	 { .strip_mods = true }},
 };
 
 static int btf_dump_all_types(const struct btf *btf,
@@ -125,6 +127,7 @@ void test_btf_dump() {
 		if (!test__start_subtest(t->name))
 			continue;
 
-		test_btf_dump_case(i, &btf_dump_test_cases[i]);
+		t->opts.sz = sizeof(t->opts);
+		test_btf_dump_case(i, t);
 	}
 }
diff --git a/tools/testing/selftests/bpf/progs/btf_dump_test_case_strip_mods.c b/tools/testing/selftests/bpf/progs/btf_dump_test_case_strip_mods.c
new file mode 100644
index 000000000000..1a6ba26d5d75
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf_dump_test_case_strip_mods.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+/* Copyright (c) 2020 Facebook */
+
+struct s {
+	const int a;
+	volatile int * const b;
+	const volatile struct {
+		int * const volatile c1;
+		const int (* const c2)(volatile int x, const void * const y);
+		const volatile int c3[10];
+	} c;
+	const union {
+		const int * restrict d1;
+		const volatile int * const volatile restrict d2;
+	} d[2];
+	const struct {
+		const volatile int *e1[5];
+	} e;
+	const volatile int * const * volatile * restrict *f;
+	const void * volatile * (*g)(const int x, const void * restrict y);
+};
+
+/* ----- START-EXPECTED-OUTPUT ----- */
+/*
+ *struct s {
+ *	int a;
+ *	int *b;
+ *	struct {
+ *		int *c1;
+ *		int (*c2)(int, void *);
+ *		int c3[10];
+ *	} c;
+ *	union {
+ *		int *d1;
+ *		int *d2;
+ *	} d[2];
+ *	struct {
+ *		int *e1[5];
+ *	} e;
+ *	int ****f;
+ *	void ** (*g)(int, void *);
+ *};
+ *
+ */
+/* ------ END-EXPECTED-OUTPUT ------ */
+
+int f(struct s *s)
+{
+	return 0;
+}
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH bpf-next 2/3] selftests/bpf: add selftest validating btf_dump's mod-stripping output
  2020-07-01  6:45 [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables Andrii Nakryiko
  2020-07-01  6:45 ` [PATCH bpf-next 1/3] libbpf: support stripping modifiers for btf_dump Andrii Nakryiko
  2020-07-01  6:45 ` [PATCH bpf-next 2/3] selftests/bpf: add selftest testin btf_dump's mod-stripping output Andrii Nakryiko
@ 2020-07-01  6:45 ` Andrii Nakryiko
  2020-07-01  6:45 ` [PATCH bpf-next 3/3] tools/bpftool: strip away modifiers from global variables Andrii Nakryiko
  2020-07-01 15:01 ` [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton " Alexei Starovoitov
  4 siblings, 0 replies; 10+ messages in thread
From: Andrii Nakryiko @ 2020-07-01  6:45 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Anton Protopopov

Add selftest validating that .strip_mods=true mode works correctly.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 .../selftests/bpf/prog_tests/btf_dump.c       |  5 +-
 .../bpf/progs/btf_dump_test_case_strip_mods.c | 50 +++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/btf_dump_test_case_strip_mods.c

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
index cb33a7ee4e04..112b653b9c80 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
@@ -21,6 +21,8 @@ static struct btf_dump_test_case {
 	{"btf_dump: bitfields", "btf_dump_test_case_bitfields", {}},
 	{"btf_dump: multidim", "btf_dump_test_case_multidim", {}},
 	{"btf_dump: namespacing", "btf_dump_test_case_namespacing", {}},
+	{"btf_dump: strip mods", "btf_dump_test_case_strip_mods",
+	 { .strip_mods = true }},
 };
 
 static int btf_dump_all_types(const struct btf *btf,
@@ -125,6 +127,7 @@ void test_btf_dump() {
 		if (!test__start_subtest(t->name))
 			continue;
 
-		test_btf_dump_case(i, &btf_dump_test_cases[i]);
+		t->opts.sz = sizeof(t->opts);
+		test_btf_dump_case(i, t);
 	}
 }
diff --git a/tools/testing/selftests/bpf/progs/btf_dump_test_case_strip_mods.c b/tools/testing/selftests/bpf/progs/btf_dump_test_case_strip_mods.c
new file mode 100644
index 000000000000..1a6ba26d5d75
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/btf_dump_test_case_strip_mods.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+/* Copyright (c) 2020 Facebook */
+
+struct s {
+	const int a;
+	volatile int * const b;
+	const volatile struct {
+		int * const volatile c1;
+		const int (* const c2)(volatile int x, const void * const y);
+		const volatile int c3[10];
+	} c;
+	const union {
+		const int * restrict d1;
+		const volatile int * const volatile restrict d2;
+	} d[2];
+	const struct {
+		const volatile int *e1[5];
+	} e;
+	const volatile int * const * volatile * restrict *f;
+	const void * volatile * (*g)(const int x, const void * restrict y);
+};
+
+/* ----- START-EXPECTED-OUTPUT ----- */
+/*
+ *struct s {
+ *	int a;
+ *	int *b;
+ *	struct {
+ *		int *c1;
+ *		int (*c2)(int, void *);
+ *		int c3[10];
+ *	} c;
+ *	union {
+ *		int *d1;
+ *		int *d2;
+ *	} d[2];
+ *	struct {
+ *		int *e1[5];
+ *	} e;
+ *	int ****f;
+ *	void ** (*g)(int, void *);
+ *};
+ *
+ */
+/* ------ END-EXPECTED-OUTPUT ------ */
+
+int f(struct s *s)
+{
+	return 0;
+}
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH bpf-next 3/3] tools/bpftool: strip away modifiers from global variables
  2020-07-01  6:45 [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables Andrii Nakryiko
                   ` (2 preceding siblings ...)
  2020-07-01  6:45 ` [PATCH bpf-next 2/3] selftests/bpf: add selftest validating " Andrii Nakryiko
@ 2020-07-01  6:45 ` Andrii Nakryiko
  2020-07-01 15:01 ` [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton " Alexei Starovoitov
  4 siblings, 0 replies; 10+ messages in thread
From: Andrii Nakryiko @ 2020-07-01  6:45 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Anton Protopopov

Reliably remove all the type modifiers from global variable definitions,
including cases of inner field const modifiers and arrays of const values.

Also modify one of selftests to ensure that const volatile struct doesn't
prevent user-space from modifying .rodata variable.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/bpf/bpftool/gen.c                           | 13 ++++---------
 tools/testing/selftests/bpf/prog_tests/skeleton.c |  6 +++---
 tools/testing/selftests/bpf/progs/test_skeleton.c |  6 ++++--
 3 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index 10de76b296ba..2a13114896c2 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -88,7 +88,7 @@ static const char *get_map_ident(const struct bpf_map *map)
 		return NULL;
 }
 
-static void codegen_btf_dump_printf(void *ct, const char *fmt, va_list args)
+static void codegen_btf_dump_printf(void *ctx, const char *fmt, va_list args)
 {
 	vprintf(fmt, args);
 }
@@ -126,13 +126,6 @@ static int codegen_datasec_def(struct bpf_object *obj,
 		);
 		int need_off = sec_var->offset, align_off, align;
 		__u32 var_type_id = var->type;
-		const struct btf_type *t;
-
-		t = btf__type_by_id(btf, var_type_id);
-		while (btf_is_mod(t)) {
-			var_type_id = t->type;
-			t = btf__type_by_id(btf, var_type_id);
-		}
 
 		if (off > need_off) {
 			p_err("Something is wrong for %s's variable #%d: need offset %d, already at %d.\n",
@@ -176,12 +169,14 @@ static int codegen_datasec_def(struct bpf_object *obj,
 
 static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
 {
+	/* strip out const/volatile/restrict modifiers for datasecs */
+	DECLARE_LIBBPF_OPTS(btf_dump_opts, opts, .strip_mods = true);
 	struct btf *btf = bpf_object__btf(obj);
 	int n = btf__get_nr_types(btf);
 	struct btf_dump *d;
 	int i, err = 0;
 
-	d = btf_dump__new(btf, NULL, NULL, codegen_btf_dump_printf);
+	d = btf_dump__new(btf, NULL, &opts, codegen_btf_dump_printf);
 	if (IS_ERR(d))
 		return PTR_ERR(d);
 
diff --git a/tools/testing/selftests/bpf/prog_tests/skeleton.c b/tools/testing/selftests/bpf/prog_tests/skeleton.c
index fa153cf67b1b..fe87b77af459 100644
--- a/tools/testing/selftests/bpf/prog_tests/skeleton.c
+++ b/tools/testing/selftests/bpf/prog_tests/skeleton.c
@@ -41,7 +41,7 @@ void test_skeleton(void)
 	CHECK(bss->in4 != 0, "in4", "got %lld != exp %lld\n", bss->in4, 0LL);
 	CHECK(bss->out4 != 0, "out4", "got %lld != exp %lld\n", bss->out4, 0LL);
 
-	CHECK(rodata->in6 != 0, "in6", "got %d != exp %d\n", rodata->in6, 0);
+	CHECK(rodata->in.in6 != 0, "in6", "got %d != exp %d\n", rodata->in.in6, 0);
 	CHECK(bss->out6 != 0, "out6", "got %d != exp %d\n", bss->out6, 0);
 
 	/* validate we can pre-setup global variables, even in .bss */
@@ -49,7 +49,7 @@ void test_skeleton(void)
 	data->in2 = 11;
 	bss->in3 = 12;
 	bss->in4 = 13;
-	rodata->in6 = 14;
+	rodata->in.in6 = 14;
 
 	err = test_skeleton__load(skel);
 	if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
@@ -60,7 +60,7 @@ void test_skeleton(void)
 	CHECK(data->in2 != 11, "in2", "got %lld != exp %lld\n", data->in2, 11LL);
 	CHECK(bss->in3 != 12, "in3", "got %d != exp %d\n", bss->in3, 12);
 	CHECK(bss->in4 != 13, "in4", "got %lld != exp %lld\n", bss->in4, 13LL);
-	CHECK(rodata->in6 != 14, "in6", "got %d != exp %d\n", rodata->in6, 14);
+	CHECK(rodata->in.in6 != 14, "in6", "got %d != exp %d\n", rodata->in.in6, 14);
 
 	/* now set new values and attach to get them into outX variables */
 	data->in1 = 1;
diff --git a/tools/testing/selftests/bpf/progs/test_skeleton.c b/tools/testing/selftests/bpf/progs/test_skeleton.c
index 77ae86f44db5..374ccef704e1 100644
--- a/tools/testing/selftests/bpf/progs/test_skeleton.c
+++ b/tools/testing/selftests/bpf/progs/test_skeleton.c
@@ -20,7 +20,9 @@ long long in4 __attribute__((aligned(64))) = 0;
 struct s in5 = {};
 
 /* .rodata section */
-const volatile int in6 = 0;
+const volatile struct {
+	const int in6;
+} in = {};
 
 /* .data section */
 int out1 = -1;
@@ -46,7 +48,7 @@ int handler(const void *ctx)
 	out3 = in3;
 	out4 = in4;
 	out5 = in5;
-	out6 = in6;
+	out6 = in.in6;
 
 	bpf_syscall = CONFIG_BPF_SYSCALL;
 	kern_ver = LINUX_KERNEL_VERSION;
-- 
2.24.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH bpf-next 2/3] selftests/bpf: add selftest testin btf_dump's mod-stripping output
  2020-07-01  6:45 ` [PATCH bpf-next 2/3] selftests/bpf: add selftest testin btf_dump's mod-stripping output Andrii Nakryiko
@ 2020-07-01  6:47   ` Andrii Nakryiko
  0 siblings, 0 replies; 10+ messages in thread
From: Andrii Nakryiko @ 2020-07-01  6:47 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Networking, Alexei Starovoitov, Daniel Borkmann,
	Kernel Team, Anton Protopopov

On Tue, Jun 30, 2020 at 11:45 PM Andrii Nakryiko <andriin@fb.com> wrote:
>
> Add selftest validating that .strip_mods=true works.
>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> ---

Please ignore this patch, it is a stale copy with a typo in the subject.

[...]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables
  2020-07-01  6:45 [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables Andrii Nakryiko
                   ` (3 preceding siblings ...)
  2020-07-01  6:45 ` [PATCH bpf-next 3/3] tools/bpftool: strip away modifiers from global variables Andrii Nakryiko
@ 2020-07-01 15:01 ` Alexei Starovoitov
  2020-07-01 16:08   ` Andrii Nakryiko
  4 siblings, 1 reply; 10+ messages in thread
From: Alexei Starovoitov @ 2020-07-01 15:01 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Network Development, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, Kernel Team, Anton Protopopov

On Tue, Jun 30, 2020 at 11:46 PM Andrii Nakryiko <andriin@fb.com> wrote:
>
> Fix bpftool logic of stripping away const/volatile modifiers for all global
> variables during BPF skeleton generation. See patch #1 for details on when
> existing logic breaks and why it's important. Support special .strip_mods=true
> mode in btf_dump. Add selftests validating that everything works as expected.

Why bother with the flag?
It looks like bugfix to me.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables
  2020-07-01 15:01 ` [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton " Alexei Starovoitov
@ 2020-07-01 16:08   ` Andrii Nakryiko
  2020-07-01 16:36     ` Alexei Starovoitov
  0 siblings, 1 reply; 10+ messages in thread
From: Andrii Nakryiko @ 2020-07-01 16:08 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Andrii Nakryiko, bpf, Network Development, Alexei Starovoitov,
	Daniel Borkmann, Kernel Team, Anton Protopopov

On Wed, Jul 1, 2020 at 8:02 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Tue, Jun 30, 2020 at 11:46 PM Andrii Nakryiko <andriin@fb.com> wrote:
> >
> > Fix bpftool logic of stripping away const/volatile modifiers for all global
> > variables during BPF skeleton generation. See patch #1 for details on when
> > existing logic breaks and why it's important. Support special .strip_mods=true
> > mode in btf_dump. Add selftests validating that everything works as expected.
>
> Why bother with the flag?

You mean btf_dump should do this always? That's a bit too invasive a
change, I don't like it.

> It looks like bugfix to me.

It can be considered a bug fix for bpftool's skeleton generation, but
it depends on non-trivial changes in libbpf, which are not bug fix per
se, so should probably better go through bpf-next.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables
  2020-07-01 16:08   ` Andrii Nakryiko
@ 2020-07-01 16:36     ` Alexei Starovoitov
  2020-07-01 18:15       ` Andrii Nakryiko
  0 siblings, 1 reply; 10+ messages in thread
From: Alexei Starovoitov @ 2020-07-01 16:36 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Andrii Nakryiko, bpf, Network Development, Alexei Starovoitov,
	Daniel Borkmann, Kernel Team, Anton Protopopov

On Wed, Jul 01, 2020 at 09:08:45AM -0700, Andrii Nakryiko wrote:
> On Wed, Jul 1, 2020 at 8:02 AM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
> >
> > On Tue, Jun 30, 2020 at 11:46 PM Andrii Nakryiko <andriin@fb.com> wrote:
> > >
> > > Fix bpftool logic of stripping away const/volatile modifiers for all global
> > > variables during BPF skeleton generation. See patch #1 for details on when
> > > existing logic breaks and why it's important. Support special .strip_mods=true
> > > mode in btf_dump. Add selftests validating that everything works as expected.
> >
> > Why bother with the flag?
> 
> You mean btf_dump should do this always? That's a bit too invasive a
> change, I don't like it.
> 
> > It looks like bugfix to me.
> 
> It can be considered a bug fix for bpftool's skeleton generation, but
> it depends on non-trivial changes in libbpf, which are not bug fix per
> se, so should probably better go through bpf-next.

I'm not following.
Without tweaking opts and introducing new flag the actual fix is only
two hunks in patch 1:

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

 	stack_start = d->decl_stack_cnt;
 	for (;;) {
+		t = btf__type_by_id(d->btf, id);
+		if (btf_is_mod(t))
+			goto skip_mod;
+
 		err = btf_dump_push_decl_stack_id(d, id);
 		if (err < 0) {
 			/*
@@ -1056,12 +1065,11 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
 			d->decl_stack_cnt = stack_start;
 			return;
 		}
-
+skip_mod:
 		/* VOID */
 		if (id == 0)
 			break;

-		t = btf__type_by_id(d->btf, id);


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables
  2020-07-01 16:36     ` Alexei Starovoitov
@ 2020-07-01 18:15       ` Andrii Nakryiko
  0 siblings, 0 replies; 10+ messages in thread
From: Andrii Nakryiko @ 2020-07-01 18:15 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Andrii Nakryiko, bpf, Network Development, Alexei Starovoitov,
	Daniel Borkmann, Kernel Team, Anton Protopopov

On Wed, Jul 1, 2020 at 9:36 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Wed, Jul 01, 2020 at 09:08:45AM -0700, Andrii Nakryiko wrote:
> > On Wed, Jul 1, 2020 at 8:02 AM Alexei Starovoitov
> > <alexei.starovoitov@gmail.com> wrote:
> > >
> > > On Tue, Jun 30, 2020 at 11:46 PM Andrii Nakryiko <andriin@fb.com> wrote:
> > > >
> > > > Fix bpftool logic of stripping away const/volatile modifiers for all global
> > > > variables during BPF skeleton generation. See patch #1 for details on when
> > > > existing logic breaks and why it's important. Support special .strip_mods=true
> > > > mode in btf_dump. Add selftests validating that everything works as expected.
> > >
> > > Why bother with the flag?
> >
> > You mean btf_dump should do this always? That's a bit too invasive a
> > change, I don't like it.
> >
> > > It looks like bugfix to me.
> >
> > It can be considered a bug fix for bpftool's skeleton generation, but
> > it depends on non-trivial changes in libbpf, which are not bug fix per
> > se, so should probably better go through bpf-next.
>
> I'm not following.
> Without tweaking opts and introducing new flag the actual fix is only
> two hunks in patch 1:

Right, but from the btf_dump point of view this is not a bug fix, its
current behavior is correct and precise. So this change is a change in
behavior and not universally correct for all the possible use cases.
So I can't just make it always strip modifiers, it's changing
generated output. It has to be an optional feature.


>
> @@ -1045,6 +1050,10 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
>
>         stack_start = d->decl_stack_cnt;
>         for (;;) {
> +               t = btf__type_by_id(d->btf, id);
> +               if (btf_is_mod(t))
> +                       goto skip_mod;
> +
>                 err = btf_dump_push_decl_stack_id(d, id);
>                 if (err < 0) {
>                         /*
> @@ -1056,12 +1065,11 @@ static void btf_dump_emit_type_decl(struct btf_dump *d, __u32 id,
>                         d->decl_stack_cnt = stack_start;
>                         return;
>                 }
> -
> +skip_mod:
>                 /* VOID */
>                 if (id == 0)
>                         break;
>
> -               t = btf__type_by_id(d->btf, id);
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, back to index

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-01  6:45 [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton global variables Andrii Nakryiko
2020-07-01  6:45 ` [PATCH bpf-next 1/3] libbpf: support stripping modifiers for btf_dump Andrii Nakryiko
2020-07-01  6:45 ` [PATCH bpf-next 2/3] selftests/bpf: add selftest testin btf_dump's mod-stripping output Andrii Nakryiko
2020-07-01  6:47   ` Andrii Nakryiko
2020-07-01  6:45 ` [PATCH bpf-next 2/3] selftests/bpf: add selftest validating " Andrii Nakryiko
2020-07-01  6:45 ` [PATCH bpf-next 3/3] tools/bpftool: strip away modifiers from global variables Andrii Nakryiko
2020-07-01 15:01 ` [PATCH bpf-next 0/3] Strip away modifiers from BPF skeleton " Alexei Starovoitov
2020-07-01 16:08   ` Andrii Nakryiko
2020-07-01 16:36     ` Alexei Starovoitov
2020-07-01 18:15       ` Andrii Nakryiko

BPF Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/bpf/0 bpf/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 bpf bpf/ https://lore.kernel.org/bpf \
		bpf@vger.kernel.org
	public-inbox-index bpf

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.bpf


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git