Skip to contentSkip to conversation

libbpf: fix BTF-defined map's __type macro handling of arrays

PATCH Applied Andrii Nakryiko 2 participants 2 messages Oct 4, 2019 – Oct 6, 2019

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: fix BTF-defined map's __type macro handling of arrays

Due to a quirky C syntax of declaring pointers to array or function prototype, existing __type() macro doesn't work with map key/value types that are array or function prototype. One has to create a typedef first and use it to specify key/value type for a BPF map. By using typeof(), pointer to type is now handled uniformly for all kinds of types. Convert one of self-tests as a demonstration.

---

N.B. This patch depends on patch set that moves bpf_helpers.h into libbpf to avoid conflicts.

 tools/lib/bpf/bpf_helpers.h                              | 2 +-
 tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)
tools/lib/bpf/bpf_helpers.h +1 -1
index 2d3d1f51cdd0..6f68e4d3aff7 100644
@@ -3,7 +3,7 @@
 #define __BPF_HELPERS__
 
 #define __uint(name, val) int (*name)[val]
-#define __type(name, val) val *name
+#define __type(name, val) typeof(val) *name
 
 /* Helper macro to print out debug messages */
 #define bpf_printk(fmt, ...)				\
tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c +1 -2
index f8ffa3f3d44b..6cc4479ac9df 100644
@@ -47,12 +47,11 @@ struct {
  * issue and avoid complicated C programming massaging.
  * This is an acceptable workaround since there is one entry here.
  */
-typedef __u64 raw_stack_trace_t[2 * MAX_STACK_RAWTP];
 struct {
 	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
 	__uint(max_entries, 1);
 	__type(key, __u32);
-	__type(value, raw_stack_trace_t);
+	__type(value, __u64[2 * MAX_STACK_RAWTP]);
 } rawdata_map SEC(".maps");
 
 SEC("raw_tracepoint/sys_enter")
Signature · 1 line
2.17.1
#1 lore raw
Quoted 9 lines · Andrii Nakryiko

Due to a quirky C syntax of declaring pointers to array or function prototype, existing __type() macro doesn't work with map key/value types that are array or function prototype. One has to create a typedef first and use it to specify key/value type for a BPF map. By using typeof(), pointer to type is now handled uniformly for all kinds of types. Convert one of self-tests as a demonstration.

Applied. Thanks