Skip to contentSkip to conversation

Change bpftool, libbpf, selftests to force GNU89 mode

PATCH In review Kumar Kartikeya Dwivedi 3 participants 13 messages Nov 5, 2021 – Nov 9, 2021

Subsystems (6)
Kselftest BPF BPF selftests libbpf +2 more subsystems
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

cover Change bpftool, libbpf, selftests to force GNU89 mode

Fix any remaining instances that fail the build in this mode. For selftests, we also need to separate CXXFLAGS from CFLAGS, since adding it to CFLAGS simply would generate a warning when used with g++.

This also cherry-picks Andrii's patch to fix the instance in libbpf. Also tested introducing new invalid usage of C99 features.

Andrii Nakryiko (1):
libbpf: fix non-C89 loop variable declaration in gen_loader.c

Kumar Kartikeya Dwivedi (5):
bpftool: Compile using -std=gnu89
libbpf: Compile using -std=gnu89
selftests/bpf: Fix non-C89 loop variable declaration instances selftests/bpf: Switch to non-unicode character in output selftests/bpf: Compile using -std=gnu89

 tools/bpf/bpftool/Makefile                           | 2 +-
 tools/lib/bpf/Makefile                               | 1 +
 tools/lib/bpf/gen_loader.c                           | 3 ++-
 tools/testing/selftests/bpf/Makefile                 | 5 ++++-
 tools/testing/selftests/bpf/bench.c                  | 6 +++---
 tools/testing/selftests/bpf/prog_tests/d_path.c      | 4 ++--
 tools/testing/selftests/bpf/prog_tests/timer_mim.c   | 6 +++---
 tools/testing/selftests/bpf/prog_tests/xdp_bonding.c | 4 ++--
 tools/testing/selftests/bpf/test_progs.c             | 2 +-
 9 files changed, 19 insertions(+), 14 deletions(-)
Signature · 1 line
2.33.1
author maintainer
#1 lore raw

PATCH 1/6 bpftool: Compile using -std=gnu89

The minimum supported C standard version is C89, with use of GNU extensions, hence make sure to catch any instances that would break the build for this mode by passing -std=gnu89.

---
 tools/bpf/bpftool/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/Makefile +1 -1
index c0c30e56988f..71a7b7194c2c 100644
@@ -65,7 +65,7 @@ $(LIBBPF_BOOTSTRAP)-clean: FORCE | $(LIBBPF_BOOTSTRAP_OUTPUT)
 prefix ?= /usr/local
 bash_compdir ?= /usr/share/bash-completion/completions
 
-CFLAGS += -O2
+CFLAGS += -std=gnu89 -O2
 CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
 CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
 CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
Signature · 1 line
2.33.1
author maintainer
#2 lore raw

PATCH 2/6 libbpf: fix non-C89 loop variable declaration in gen_loader.c

In reply to Kumar Kartikeya Dwivedi

From: Andrii Nakryiko <[email protected]>

Fix the int i declaration inside the for statement. This is non-C89 compliant. See [0] for user report breaking BCC build.

[0] https://github.com/libbpf/libbpf/issues/403

Fixes 18f4fccbf314 ("libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations")
---
 tools/lib/bpf/gen_loader.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/lib/bpf/gen_loader.c +2 -1
index 502dea53a742..2e10776b6d85 100644
@@ -584,8 +584,9 @@ void bpf_gen__record_extern(struct bpf_gen *gen, const char *name, bool is_weak,
 static struct ksym_desc *get_ksym_desc(struct bpf_gen *gen, struct ksym_relo_desc *relo)
 {
 	struct ksym_desc *kdesc;
+	int i;
 
-	for (int i = 0; i < gen->nr_ksyms; i++) {
+	for (i = 0; i < gen->nr_ksyms; i++) {
 		if (!strcmp(gen->ksyms[i].name, relo->name)) {
 			gen->ksyms[i].ref++;
 			return &gen->ksyms[i];
Signature · 1 line
2.33.1
author maintainer
#3 lore raw

PATCH 3/6 libbpf: Compile using -std=gnu89

In reply to Kumar Kartikeya Dwivedi

The minimum supported C standard version is C89, with use of GNU extensions, hence make sure to catch any instances that would break the build for this mode by passing -std=gnu89.

---
 tools/lib/bpf/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/lib/bpf/Makefile +1 -0
index b393b5e82380..5f7086fae31c 100644
@@ -84,6 +84,7 @@ else
 endif
 
 # Append required CFLAGS
+override CFLAGS += -std=gnu89
 override CFLAGS += $(EXTRA_WARNINGS) -Wno-switch-enum
 override CFLAGS += -Werror -Wall
 override CFLAGS += $(INCLUDES)
Signature · 1 line
2.33.1
author maintainer
#4 lore raw

PATCH 4/6 selftests/bpf: Fix non-C89 loop variable declaration instances

In reply to Kumar Kartikeya Dwivedi

Fix the int i declaration inside the for statement. This is non-C89 compliant. Doing all of them in this change since they are trivial.

---
 tools/testing/selftests/bpf/prog_tests/d_path.c      | 4 ++--
 tools/testing/selftests/bpf/prog_tests/timer_mim.c   | 6 +++---
 tools/testing/selftests/bpf/prog_tests/xdp_bonding.c | 4 ++--
 tools/testing/selftests/bpf/test_progs.c             | 2 +-
 4 files changed, 8 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/prog_tests/d_path.c +2 -2
index 0a577a248d34..cc787ad68081 100644
@@ -103,7 +103,7 @@ void test_d_path(void)
 {
 	struct test_d_path__bss *bss;
 	struct test_d_path *skel;
-	int err;
+	int err, i;
 
 	skel = test_d_path__open_and_load();
 	if (CHECK(!skel, "setup", "d_path skeleton failed\n"))
@@ -130,7 +130,7 @@ void test_d_path(void)
 		  "trampoline for filp_close was not called\n"))
 		goto cleanup;
 
-	for (int i = 0; i < MAX_FILES; i++) {
+	for (i = 0; i < MAX_FILES; i++) {
 		CHECK(strncmp(src.paths[i], bss->paths_stat[i], MAX_PATH_LEN),
 		      "check",
 		      "failed to get stat path[%d]: %s vs %s\n",
tools/testing/selftests/bpf/prog_tests/timer_mim.c +3 -3
index 949a0617869d..f12536c32e2d 100644
@@ -6,9 +6,9 @@
 
 static int timer_mim(struct timer_mim *timer_skel)
 {
+	int err, prog_fd, key1 = 1, i;
 	__u32 duration = 0, retval;
 	__u64 cnt1, cnt2;
-	int err, prog_fd, key1 = 1;
 
 	err = timer_mim__attach(timer_skel);
 	if (!ASSERT_OK(err, "timer_attach"))
@@ -23,7 +23,7 @@ static int timer_mim(struct timer_mim *timer_skel)
 
 	/* check that timer_cb[12] are incrementing 'cnt' */
 	cnt1 = READ_ONCE(timer_skel->bss->cnt);
-	for (int i = 0; i < 100; i++) {
+	for (i = 0; i < 100; i++) {
 		cnt2 = READ_ONCE(timer_skel->bss->cnt);
 		if (cnt2 != cnt1)
 			break;
@@ -41,7 +41,7 @@ static int timer_mim(struct timer_mim *timer_skel)
 
 	/* check that timer_cb[12] are no longer running */
 	cnt1 = READ_ONCE(timer_skel->bss->cnt);
-	for (int i = 0; i < 100; i++) {
+	for (i = 0; i < 100; i++) {
 		usleep(200); /* 100 times more than interval */
 		cnt2 = READ_ONCE(timer_skel->bss->cnt);
 		if (cnt2 == cnt1)
tools/testing/selftests/bpf/prog_tests/xdp_bonding.c +2 -2
index faa22b84f2ee..e5b8666e59eb 100644
@@ -335,7 +335,7 @@ static void test_xdp_bonding_redirect_multi(struct skeletons *skeletons)
 {
 	static const char * const ifaces[] = {"bond2", "veth2_1", "veth2_2"};
 	int veth1_1_rx, veth1_2_rx;
-	int err;
+	int err, i;
 
 	if (bonding_setup(skeletons, BOND_MODE_ROUNDROBIN, BOND_XMIT_POLICY_LAYER23,
 			  BOND_ONE_NO_ATTACH))
@@ -346,7 +346,7 @@ static void test_xdp_bonding_redirect_multi(struct skeletons *skeletons)
 		goto out;
 
 	/* populate the devmap with the relevant interfaces */
-	for (int i = 0; i < ARRAY_SIZE(ifaces); i++) {
+	for (i = 0; i < ARRAY_SIZE(ifaces); i++) {
 		int ifindex = if_nametoindex(ifaces[i]);
 		int map_fd = bpf_map__fd(skeletons->xdp_redirect_multi_kern->maps.map_all);
 
tools/testing/selftests/bpf/test_progs.c +1 -1
index c65986bd9d07..0096051e7560 100644
@@ -1146,7 +1146,7 @@ static int server_main(void)
 	/* run serial tests */
 	save_netns();
 
-	for (int i = 0; i < prog_test_cnt; i++) {
+	for (i = 0; i < prog_test_cnt; i++) {
 		struct prog_test_def *test = &prog_test_defs[i];
 		struct test_result *result = &test_results[i];
 
Signature · 1 line
2.33.1
author maintainer
#5 lore raw

PATCH 5/6 selftests/bpf: Switch to non-unicode character in output

In reply to Kumar Kartikeya Dwivedi

GNU89 doesn't allow unicode characters in printf output, and build gives the following warning:

warning: universal character names are only valid in C++ and C99

Fix by using ASCII +/-.

---
 tools/testing/selftests/bpf/bench.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/bench.c +3 -3
index cc4722f693e9..58a32ff2eb1b 100644
@@ -126,11 +126,11 @@ void hits_drops_report_final(struct bench_res res[], int res_cnt)
 		drops_stddev = sqrt(drops_stddev);
 		total_ops_stddev = sqrt(total_ops_stddev);
 	}
-	printf("Summary: hits %8.3lf \u00B1 %5.3lfM/s (%7.3lfM/prod), ",
+	printf("Summary: hits %8.3lf +/- %5.3lfM/s (%7.3lfM/prod), ",
 	       hits_mean, hits_stddev, hits_mean / env.producer_cnt);
-	printf("drops %8.3lf \u00B1 %5.3lfM/s, ",
+	printf("drops %8.3lf +/- %5.3lfM/s, ",
 	       drops_mean, drops_stddev);
-	printf("total operations %8.3lf \u00B1 %5.3lfM/s\n",
+	printf("total operations %8.3lf +/- %5.3lfM/s\n",
 	       total_ops_mean, total_ops_stddev);
 }
 
Signature · 1 line
2.33.1
author maintainer
#6 lore raw

PATCH 6/6 selftests/bpf: Compile using -std=gnu89

In reply to Kumar Kartikeya Dwivedi

The minimum supported C standard version is C89, with use of GNU extensions, hence make sure to catch any instances that would break the build for this mode by passing -std=gnu89.

Also, copy out CFLAGS for C++ test so that we don't end up passing -std=gnu89 to g++, otherwise the build generates a (harmless) warning:

cc1plus: warning: command-line option ‘-std=gnu90’ is valid for C/ObjC but not for C++

---
 tools/testing/selftests/bpf/Makefile | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/Makefile +4 -1
index 54b0a41a3775..6239e51c310f 100644
@@ -33,6 +33,9 @@ ifneq ($(LLVM),)
 CFLAGS += -Wno-unused-command-line-argument
 endif
 
+CXXFLAGS := $(CFLAGS)
+CFLAGS += -std=gnu89
+
 # Order correspond to 'make run_tests' order
 TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
 	test_verifier_log test_dev_cgroup \
@@ -519,7 +522,7 @@ $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT)
 # Make sure we are able to include and link libbpf against c++.
 $(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
 	$(call msg,CXX,,$@)
-	$(Q)$(CXX) $(CFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
+	$(Q)$(CXX) $(CXXFLAGS) $(filter %.a %.o %.cpp,$^) $(LDLIBS) -o $@
 
 # Benchmark runner
 $(OUTPUT)/bench_%.o: benchs/bench_%.c bench.h $(BPFOBJ)
Signature · 1 line
2.33.1
#7 lore raw
In reply to Kumar Kartikeya Dwivedi
Quoted 11 lines · Kumar Kartikeya Dwivedi

Fix any remaining instances that fail the build in this mode. For selftests, we also need to separate CXXFLAGS from CFLAGS, since adding it to CFLAGS simply would generate a warning when used with g++.

This also cherry-picks Andrii's patch to fix the instance in libbpf. Also tested introducing new invalid usage of C99 features.

Andrii Nakryiko (1):
libbpf: fix non-C89 loop variable declaration in gen_loader.c

Quoted 6 lines

Kumar Kartikeya Dwivedi (5):
bpftool: Compile using -std=gnu89
libbpf: Compile using -std=gnu89
selftests/bpf: Fix non-C89 loop variable declaration instances selftests/bpf: Switch to non-unicode character in output selftests/bpf: Compile using -std=gnu89

Please don't.
I'd rather go the other way and drop gnu89 from everywhere. for (int i = 0
is so much cleaner.

#8 lore raw
Quoted 20 lines · Alexei Starovoitov
Quoted 11 lines · Kumar Kartikeya Dwivedi

Fix any remaining instances that fail the build in this mode. For selftests, we also need to separate CXXFLAGS from CFLAGS, since adding it to CFLAGS simply would generate a warning when used with g++.

This also cherry-picks Andrii's patch to fix the instance in libbpf. Also tested introducing new invalid usage of C99 features.

Andrii Nakryiko (1):
libbpf: fix non-C89 loop variable declaration in gen_loader.c

Quoted 6 lines

Kumar Kartikeya Dwivedi (5):
bpftool: Compile using -std=gnu89
libbpf: Compile using -std=gnu89
selftests/bpf: Fix non-C89 loop variable declaration instances selftests/bpf: Switch to non-unicode character in output selftests/bpf: Compile using -std=gnu89

Quoted 4 lines

Please don't.
I'd rather go the other way and drop gnu89 from everywhere. for (int i = 0
is so much cleaner.

I agree that for (int i) is better, but it's kernel code style which we followed so far pretty closely for libbpf and bpftool. So I think this is the right move for bpftool and libbpf.

Selftests are less consistent in styling and lack of unicode character in bench is annoying, so I don't mind leaving selftest more permissive.

And even with all that, we've managed to keep BPF program code consistently in C89 here in selftests and in bcc/libbpf-tools. The code style uniformity is nice.

Whether to relax BCC compilation flags is a separate discussion (and I don't have any strong opinion). I'd still enforce -std=gnu89 for BPF source code for consistency across many BPF projects.

#9 lore raw
Quoted 23 lines · Andrii Nakryiko
Quoted 20 lines · Alexei Starovoitov
Quoted 17 lines · Kumar Kartikeya Dwivedi

Fix any remaining instances that fail the build in this mode. For selftests, we also need to separate CXXFLAGS from CFLAGS, since adding it to CFLAGS simply would generate a warning when used with g++.

This also cherry-picks Andrii's patch to fix the instance in libbpf. Also tested introducing new invalid usage of C99 features.

Andrii Nakryiko (1):
libbpf: fix non-C89 loop variable declaration in gen_loader.c

Kumar Kartikeya Dwivedi (5):
bpftool: Compile using -std=gnu89
libbpf: Compile using -std=gnu89
selftests/bpf: Fix non-C89 loop variable declaration instances selftests/bpf: Switch to non-unicode character in output selftests/bpf: Compile using -std=gnu89

Quoted 8 lines
Quoted 4 lines

Please don't.
I'd rather go the other way and drop gnu89 from everywhere. for (int i = 0
is so much cleaner.

I agree that for (int i) is better, but it's kernel code style which we followed so far pretty closely for libbpf and bpftool. So I think this is the right move for bpftool and libbpf.

The kernel coding style is not white and black. Certain style preferences are archaic to say the least. It's not the right move to follow it blindly.

#10 lore raw
Quoted 31 lines · Alexei Starovoitov
Quoted 28 lines · Andrii Nakryiko
Quoted 24 lines · Alexei Starovoitov
Quoted 11 lines · Kumar Kartikeya Dwivedi

Fix any remaining instances that fail the build in this mode. For selftests, we also need to separate CXXFLAGS from CFLAGS, since adding it to CFLAGS simply would generate a warning when used with g++.

This also cherry-picks Andrii's patch to fix the instance in libbpf. Also tested introducing new invalid usage of C99 features.

Andrii Nakryiko (1):
libbpf: fix non-C89 loop variable declaration in gen_loader.c

Quoted 6 lines

Kumar Kartikeya Dwivedi (5):
bpftool: Compile using -std=gnu89
libbpf: Compile using -std=gnu89
selftests/bpf: Fix non-C89 loop variable declaration instances selftests/bpf: Switch to non-unicode character in output selftests/bpf: Compile using -std=gnu89

Please don't.
I'd rather go the other way and drop gnu89 from everywhere. for (int i = 0
is so much cleaner.

Quoted 7 lines
Quoted 3 lines

I agree that for (int i) is better, but it's kernel code style which we followed so far pretty closely for libbpf and bpftool. So I think this is the right move for bpftool and libbpf.

The kernel coding style is not white and black. Certain style preferences are archaic to say the least. It's not the right move to follow it blindly.

Can we at least add -std=gnu89 for the libbpf? It's a library, so being conservative with compiler versions and language features makes sense there. I'll add a similar flag to Github's Makefile. I'd rather catch this at patch submission time rather than at the Github sync time.

#11 lore raw
Quoted 42 lines · Andrii Nakryiko
Quoted 31 lines · Alexei Starovoitov
Quoted 28 lines · Andrii Nakryiko
Quoted 24 lines · Alexei Starovoitov
Quoted 11 lines · Kumar Kartikeya Dwivedi

Fix any remaining instances that fail the build in this mode. For selftests, we also need to separate CXXFLAGS from CFLAGS, since adding it to CFLAGS simply would generate a warning when used with g++.

This also cherry-picks Andrii's patch to fix the instance in libbpf. Also tested introducing new invalid usage of C99 features.

Andrii Nakryiko (1):
libbpf: fix non-C89 loop variable declaration in gen_loader.c

Quoted 6 lines

Kumar Kartikeya Dwivedi (5):
bpftool: Compile using -std=gnu89
libbpf: Compile using -std=gnu89
selftests/bpf: Fix non-C89 loop variable declaration instances selftests/bpf: Switch to non-unicode character in output selftests/bpf: Compile using -std=gnu89

Please don't.
I'd rather go the other way and drop gnu89 from everywhere. for (int i = 0
is so much cleaner.

Quoted 7 lines
Quoted 3 lines

I agree that for (int i) is better, but it's kernel code style which we followed so far pretty closely for libbpf and bpftool. So I think this is the right move for bpftool and libbpf.

The kernel coding style is not white and black. Certain style preferences are archaic to say the least. It's not the right move to follow it blindly.

Quoted 5 lines

Can we at least add -std=gnu89 for the libbpf? It's a library, so being conservative with compiler versions and language features makes sense there. I'll add a similar flag to Github's Makefile. I'd rather catch this at patch submission time rather than at the Github sync time.

Sure. Applied Kumar's patch 3.
With CO-RE in the kernel the pieces of libbpf will be part of the kernel for real, so for libbpf as a whole would make sense to conform to the language standards as parts of libbpf have to do. As far as other parts of kernel git the language standard can be decided whichever way.
perf and libsubcmd (part of objtool) have no issue using 'for (int' while being part of the kernel tree.
We can adopt strong gnu89 in bpftool, but I'd rather not rush such a decision right now.
selftests are certainly not gnu89.
All bpf programs are written in C-2021 "standard".
Lots of C extensions in there.

#12 lore raw
Quoted 51 lines · Alexei Starovoitov
Quoted 42 lines · Andrii Nakryiko
Quoted 31 lines · Alexei Starovoitov
Quoted 28 lines · Andrii Nakryiko
Quoted 24 lines · Alexei Starovoitov
Quoted 11 lines · Kumar Kartikeya Dwivedi

Fix any remaining instances that fail the build in this mode. For selftests, we also need to separate CXXFLAGS from CFLAGS, since adding it to CFLAGS simply would generate a warning when used with g++.

This also cherry-picks Andrii's patch to fix the instance in libbpf. Also tested introducing new invalid usage of C99 features.

Andrii Nakryiko (1):
libbpf: fix non-C89 loop variable declaration in gen_loader.c

Quoted 6 lines

Kumar Kartikeya Dwivedi (5):
bpftool: Compile using -std=gnu89
libbpf: Compile using -std=gnu89
selftests/bpf: Fix non-C89 loop variable declaration instances selftests/bpf: Switch to non-unicode character in output selftests/bpf: Compile using -std=gnu89

Please don't.
I'd rather go the other way and drop gnu89 from everywhere. for (int i = 0
is so much cleaner.

Quoted 7 lines
Quoted 3 lines

I agree that for (int i) is better, but it's kernel code style which we followed so far pretty closely for libbpf and bpftool. So I think this is the right move for bpftool and libbpf.

The kernel coding style is not white and black. Certain style preferences are archaic to say the least. It's not the right move to follow it blindly.

Quoted 5 lines

Can we at least add -std=gnu89 for the libbpf? It's a library, so being conservative with compiler versions and language features makes sense there. I'll add a similar flag to Github's Makefile. I'd rather catch this at patch submission time rather than at the Github sync time.

Quoted 13 lines

Sure. Applied Kumar's patch 3.
With CO-RE in the kernel the pieces of libbpf will be part of the kernel for real, so for libbpf as a whole would make sense to conform to the language standards as parts of libbpf have to do. As far as other parts of kernel git the language standard can be decided whichever way.
perf and libsubcmd (part of objtool) have no issue using 'for (int' while being part of the kernel tree.
We can adopt strong gnu89 in bpftool, but I'd rather not rush such a decision right now.
selftests are certainly not gnu89.
All bpf programs are written in C-2021 "standard".
Lots of C extensions in there.

sgtm