Skip to contentSkip to conversation

libbpf: make LIBBPF_OPTS macro strictly a variable declaration

PATCH Superseded Andrii Nakryiko 5 participants 7 messages Oct 21, 2019 – Oct 22, 2019

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: make LIBBPF_OPTS macro strictly a variable declaration

LIBBPF_OPTS is implemented as a mix of field declaration and memset
+ assignment. This makes it neither variable declaration nor purely statements, which is a problem, because you can't mix it with either other variable declarations nor other function statements, because C90 compiler mode emits warning on mixing all that together.

This patch changes LIBBPF_OPTS into a strictly declaration of variable and solves this problem, as can be seen in case of bpftool, which previously would emit compiler warning, if done this way (LIBBPF_OPTS as part of function variables declaration block).

---
 tools/bpf/bpftool/prog.c |  6 +++---
 tools/lib/bpf/libbpf.h   | 13 +++++++------
 2 files changed, 10 insertions(+), 9 deletions(-)
tools/bpf/bpftool/prog.c +3 -3
index 27da96a797ab..1a7e8ddf8232 100644
@@ -1093,6 +1093,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 {
 	struct bpf_object_load_attr load_attr = { 0 };
 	enum bpf_prog_type common_prog_type = BPF_PROG_TYPE_UNSPEC;
+	LIBBPF_OPTS(bpf_object_open_opts, open_opts,
+		.relaxed_maps = relaxed_maps,
+	);
 	enum bpf_attach_type expected_attach_type;
 	struct map_replace *map_replace = NULL;
 	struct bpf_program *prog = NULL, *pos;
@@ -1106,9 +1109,6 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 	const char *file;
 	int idx, err;
 
-	LIBBPF_OPTS(bpf_object_open_opts, open_opts,
-		.relaxed_maps = relaxed_maps,
-	);
 
 	if (!REQ_ARGS(2))
 		return -1;
tools/lib/bpf/libbpf.h +7 -6
index 0fdf086beba7..bf105e9e866f 100644
@@ -77,12 +77,13 @@ struct bpf_object_open_attr {
  * bytes, but that's the best way I've found and it seems to work in practice.
  */
 #define LIBBPF_OPTS(TYPE, NAME, ...)					    \
-	struct TYPE NAME;						    \
-	memset(&NAME, 0, sizeof(struct TYPE));				    \
-	NAME = (struct TYPE) {						    \
-		.sz = sizeof(struct TYPE),				    \
-		__VA_ARGS__						    \
-	}
+	struct TYPE NAME = ({ 						    \
+		memset(&NAME, 0, sizeof(struct TYPE));			    \
+		(struct TYPE) {						    \
+			.sz = sizeof(struct TYPE),			    \
+			__VA_ARGS__					    \
+		};							    \
+	})
 
 struct bpf_object_open_opts {
 	/* size of this struct, for forward/backward compatiblity */
Signature · 1 line
2.17.1
#1 lore raw
Quoted 49 lines · Andrii Nakryiko

LIBBPF_OPTS is implemented as a mix of field declaration and memset
+ assignment. This makes it neither variable declaration nor purely statements, which is a problem, because you can't mix it with either other variable declarations nor other function statements, because C90 compiler mode emits warning on mixing all that together.

This patch changes LIBBPF_OPTS into a strictly declaration of variable and solves this problem, as can be seen in case of bpftool, which previously would emit compiler warning, if done this way (LIBBPF_OPTS as part of function variables declaration block).

---
 tools/bpf/bpftool/prog.c |  6 +++---
 tools/lib/bpf/libbpf.h   | 13 +++++++------
 2 files changed, 10 insertions(+), 9 deletions(-)
tools/bpf/bpftool/prog.c +3 -3
index 27da96a797ab..1a7e8ddf8232 100644
@@ -1093,6 +1093,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 {
 	struct bpf_object_load_attr load_attr = { 0 };
 	enum bpf_prog_type common_prog_type = BPF_PROG_TYPE_UNSPEC;
+	LIBBPF_OPTS(bpf_object_open_opts, open_opts,
+		.relaxed_maps = relaxed_maps,
+	);
 	enum bpf_attach_type expected_attach_type;
 	struct map_replace *map_replace = NULL;
 	struct bpf_program *prog = NULL, *pos;
@@ -1106,9 +1109,6 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 	const char *file;
 	int idx, err;
 
-	LIBBPF_OPTS(bpf_object_open_opts, open_opts,
-		.relaxed_maps = relaxed_maps,
-	);
 
 	if (!REQ_ARGS(2))
 		return -1;
tools/lib/bpf/libbpf.h +0 -0
index 0fdf086beba7..bf105e9e866f 100644
@@ -77,12 +77,13 @@ struct bpf_object_open_attr {
  * bytes, but that's the best way I've found and it seems to work in practice.
  */
 #define LIBBPF_OPTS(TYPE, NAME, ...)					    \

tools/lib/bpf/libbpf.h struct bpf_object_open_attr { · line 83

Quoted 10 lines
tools/lib/bpf/libbpf.h +4 -6
-	struct TYPE NAME;						    \
-	memset(&NAME, 0, sizeof(struct TYPE));				    \
-	NAME = (struct TYPE) {						    \
-		.sz = sizeof(struct TYPE),				    \
-		__VA_ARGS__						    \
-	}
+	struct TYPE NAME = ({ 						    \
+		memset(&NAME, 0, sizeof(struct TYPE));			    \
+		(struct TYPE) {						    \
+			.sz = sizeof(struct TYPE),			    \

Wait, you can stick arbitrary code inside a variable initialisation block like this? How does that work? Is everything before the (struct type) just ignored (and is that a cast)?

-Toke

#2 lore raw
Quoted 16 lines · Toke Høiland-Jørgensen
Quoted 13 lines · Andrii Nakryiko

LIBBPF_OPTS is implemented as a mix of field declaration and memset
+ assignment. This makes it neither variable declaration nor purely statements, which is a problem, because you can't mix it with either other variable declarations nor other function statements, because C90 compiler mode emits warning on mixing all that together.

This patch changes LIBBPF_OPTS into a strictly declaration of variable and solves this problem, as can be seen in case of bpftool, which previously would emit compiler warning, if done this way (LIBBPF_OPTS as part of function variables declaration block).

---

[...]

Quoted 14 lines
Quoted 14 lines
tools/lib/bpf/libbpf.h +0 -6
index 0fdf086beba7..bf105e9e866f 100644
@@ -77,12 +77,13 @@ struct bpf_object_open_attr {
  * bytes, but that's the best way I've found and it seems to work in practice.
  */
 #define LIBBPF_OPTS(TYPE, NAME, ...)                                     \
-     struct TYPE NAME;                                                   \
-     memset(&NAME, 0, sizeof(struct TYPE));                              \
-     NAME = (struct TYPE) {                                              \
-             .sz = sizeof(struct TYPE),                                  \
-             __VA_ARGS__                                                 \
-     }
Quoted 8 lines

tools/lib/bpf/libbpf.h struct bpf_object_open_attr { · line 83

Quoted 4 lines
tools/lib/bpf/libbpf.h +4 -0
+     struct TYPE NAME = ({                                               \
+             memset(&NAME, 0, sizeof(struct TYPE));                      \
+             (struct TYPE) {                                             \
+                     .sz = sizeof(struct TYPE),                          \

Wait, you can stick arbitrary code inside a variable initialisation block like this? How does that work? Is everything before the (struct type) just ignored (and is that a cast)?

Well, you definitely can still arbitrary code into a ({ }) expression block, that's not that surprising.
The surprising bit that I discovered just recently was that stuff like this compiles and works correctly, try it:

        void *x = &x;
        printf("%lx == %lx\n", x, &x);

So I'm using the fact that variable address is available inside variable initialization block.

Beyond that, it's just a fancy, but standard (struct bla){ ... initializer list ...} syntax (it's not a struct initializer syntax, mind you, it's a struct assignment from struct literal). Fancy for sure, but it works and solves problems I mentioned in commit description.

Quoted 3 lines

-Toke

Acked-by
#3 lore raw
Quoted 30 lines · Andrii Nakryiko
Quoted 16 lines · Toke Høiland-Jørgensen
Quoted 13 lines · Andrii Nakryiko

LIBBPF_OPTS is implemented as a mix of field declaration and memset
+ assignment. This makes it neither variable declaration nor purely statements, which is a problem, because you can't mix it with either other variable declarations nor other function statements, because C90 compiler mode emits warning on mixing all that together.

This patch changes LIBBPF_OPTS into a strictly declaration of variable and solves this problem, as can be seen in case of bpftool, which previously would emit compiler warning, if done this way (LIBBPF_OPTS as part of function variables declaration block).

---

[...]

Quoted 10 lines
Quoted 10 lines
tools/lib/bpf/libbpf.h +0 -2
index 0fdf086beba7..bf105e9e866f 100644
@@ -77,12 +77,13 @@ struct bpf_object_open_attr {
   * bytes, but that's the best way I've found and it seems to work in practice.
   */
  #define LIBBPF_OPTS(TYPE, NAME, ...)                                     \
-     struct TYPE NAME;                                                   \
-     memset(&NAME, 0, sizeof(struct TYPE));                              \
Quoted 8 lines
Quoted 8 lines

tools/lib/bpf/libbpf.h struct bpf_object_open_attr { · line 83

Quoted 8 lines
tools/lib/bpf/libbpf.h +4 -4
-     NAME = (struct TYPE) {                                              \
-             .sz = sizeof(struct TYPE),                                  \
-             __VA_ARGS__                                                 \
-     }
+     struct TYPE NAME = ({                                               \
+             memset(&NAME, 0, sizeof(struct TYPE));                      \
+             (struct TYPE) {                                             \
+                     .sz = sizeof(struct TYPE),                          \

This ({ statements; ...; value; }) is used by bcc rewriter as well.

Quoted 5 lines
Quoted 4 lines

Wait, you can stick arbitrary code inside a variable initialisation block like this? How does that work? Is everything before the (struct type) just ignored (and is that a cast)?

Quoted 7 lines

Well, you definitely can still arbitrary code into a ({ }) expression block, that's not that surprising.
The surprising bit that I discovered just recently was that stuff like this compiles and works correctly, try it:

         void *x = &x;
         printf("%lx == %lx\n", x, &x);

'void *x' just takes the address of the 'x' in the current scope. It may looks like a use before define. but it actually works.

LGTM.

Quoted 13 lines

So I'm using the fact that variable address is available inside variable initialization block.

Beyond that, it's just a fancy, but standard (struct bla){ ... initializer list ...} syntax (it's not a struct initializer syntax, mind you, it's a struct assignment from struct literal). Fancy for sure, but it works and solves problems I mentioned in commit description.

Quoted 3 lines

-Toke

#4 lore raw
In reply to Andrii Nakryiko
Quoted 6 lines · Andrii Nakryiko

LIBBPF_OPTS is implemented as a mix of field declaration and memset
+ assignment. This makes it neither variable declaration nor purely statements, which is a problem, because you can't mix it with either other variable declarations nor other function statements, because C90 compiler mode emits warning on mixing all that together.

Quoted 7 lines

This patch changes LIBBPF_OPTS into a strictly declaration of variable and solves this problem, as can be seen in case of bpftool, which previously would emit compiler warning, if done this way (LIBBPF_OPTS as part of function variables declaration block).

---

Just a suggestion - macro helpers like this usually have DECLARE in their name. At least in the kernel. For instance DECLARE_COMPLETION.

-Jakub

#5 lore raw
Quoted 13 lines · Jakub Sitnicki
Quoted 11 lines · Andrii Nakryiko

LIBBPF_OPTS is implemented as a mix of field declaration and memset
+ assignment. This makes it neither variable declaration nor purely statements, which is a problem, because you can't mix it with either other variable declarations nor other function statements, because C90 compiler mode emits warning on mixing all that together.

This patch changes LIBBPF_OPTS into a strictly declaration of variable and solves this problem, as can be seen in case of bpftool, which previously would emit compiler warning, if done this way (LIBBPF_OPTS as part of function variables declaration block).

Quoted 5 lines
Quoted 2 lines

---

Just a suggestion - macro helpers like this usually have DECLARE in their name. At least in the kernel. For instance DECLARE_COMPLETION.

Yes, it makes sense. This will cause some extra code churn, but it's not too late. Will rename in v2 and fix current usages.

Quoted 2 lines

-Jakub

#6 lore raw
Quoted 14 lines · Andrii Nakryiko
Quoted 13 lines · Jakub Sitnicki
Quoted 11 lines · Andrii Nakryiko

LIBBPF_OPTS is implemented as a mix of field declaration and memset
+ assignment. This makes it neither variable declaration nor purely statements, which is a problem, because you can't mix it with either other variable declarations nor other function statements, because C90 compiler mode emits warning on mixing all that together.

This patch changes LIBBPF_OPTS into a strictly declaration of variable and solves this problem, as can be seen in case of bpftool, which previously would emit compiler warning, if done this way (LIBBPF_OPTS as part of function variables declaration block).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

---

Just a suggestion - macro helpers like this usually have DECLARE in their name. At least in the kernel. For instance DECLARE_COMPLETION.

Yes, it makes sense. This will cause some extra code churn, but it's not too late. Will rename in v2 and fix current usages.

While you're respinning, maybe add a comment explaining what it is you're doing? It certainly broke the C parser in my head, so maybe a hint would be good for others as well :)

-Toke