Thread (18 messages) 18 messages, 4 authors, 2021-09-13

Re: [PATCH v3 bpf-next 4/7] libbpf: use static const fmt string in __bpf_printk

From: Andrii Nakryiko <hidden>
Date: 2021-08-31 00:38:17
Also in: bpf

On Fri, Aug 27, 2021 at 10:20 PM Dave Marchevsky [off-list ref] wrote:
quoted hunk ↗ jump to hunk
The __bpf_printk convenience macro was using a 'char' fmt string holder
as it predates support for globals in libbpf. Move to more efficient
'static const char', but provide a fallback to the old way via
BPF_NO_GLOBAL_DATA so users on old kernels can still use the macro.

Signed-off-by: Dave Marchevsky <redacted>
---
 tools/lib/bpf/bpf_helpers.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
index 5f087306cdfe..a1d5ec6f285c 100644
--- a/tools/lib/bpf/bpf_helpers.h
+++ b/tools/lib/bpf/bpf_helpers.h
@@ -216,10 +216,16 @@ enum libbpf_tristate {
                     ___param, sizeof(___param));               \
 })

+#ifdef BPF_NO_GLOBAL_DATA
+#define BPF_PRINTK_FMT_TYPE char
+#else
+#define BPF_PRINTK_FMT_TYPE static const char
+#endif
+
 /* Helper macro to print out debug messages */
 #define __bpf_printk(fmt, ...)                         \
 ({                                                     \
-       char ____fmt[] = fmt;                           \
+       BPF_PRINTK_FMT_TYPE ____fmt[] = fmt;            \
personal preferences, of course, but I'd leave char right there (I
think it makes it a bit more obvious what's going on right there), and
s/BPF_PRINTK_FMT_TYPE/BPF_PRINTK_FMT_MOD/ and have it as either "" or
"static const".
        bpf_trace_printk(____fmt, sizeof(____fmt),      \
                         ##__VA_ARGS__);                \
 })
--
2.30.2
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help