[PATCH v2 bpf-next 1/2] bpf: Fix macro redefined

Feng Yang posted 2 patches 2 months, 3 weeks ago
[PATCH v2 bpf-next 1/2] bpf: Fix macro redefined
Posted by Feng Yang 2 months, 3 weeks ago
From: Feng Yang <yangfeng@kylinos.cn>

When compiling a program that include <linux/bpf.h> and <bpf/bpf_helpers.h>, (For example: make samples/bpf)
the following warning will be generated:
In file included from tcp_dumpstats_kern.c:7:
samples/bpf/libbpf/include/bpf/bpf_helpers.h:321:9: warning: 'bpf_stream_printk' macro redefined [-Wmacro-redefined]
  321 | #define bpf_stream_printk(stream_id, fmt, args...)                              \
      |         ^
include/linux/bpf.h:3626:9: note: previous definition is here
 3626 | #define bpf_stream_printk(ss, ...) bpf_stream_stage_printk(&ss, __VA_ARGS__)
      |         ^

The main reason is due to below in sample/bpf/Makefile:

$(obj)/%.o: $(src)/%.c
         @echo "  CLANG-bpf " $@
         $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \
                 -I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \
                 -I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \
                 -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
                 -D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \
                 -Wno-gnu-variable-sized-type-not-at-end \
                 -Wno-address-of-packed-member -Wno-tautological-compare \
                 -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \
                 -fno-asynchronous-unwind-tables \
                 -I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \
                 -O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \
                 $(OPT) -O2 -mtriple=bpf-pc-linux | $(LLVM_DIS) | \
                 $(LLC) -march=bpf $(LLC_FLAGS) -filetype=obj -o $@

Here, some kernel data structure is needed for some particular architecture so
the initial from source to IR is compiled with native arch and after IR optimization
is done, it is switched to bpf.

So remove this line
   #define bpf_stream_printk(ss, ...) bpf_stream_stage_printk(&ss, __VA_ARGS__)
and directly use bpf_stream_stage_printk(&ss, ...)

Fixes: 21a3afc76a31 ("libbpf: Add bpf_stream_printk() macro")
Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
---
 include/linux/bpf.h     | 1 -
 kernel/bpf/core.c       | 2 +-
 kernel/bpf/rqspinlock.c | 8 ++++----
 3 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index bc887831eaa5..d010a0c4e374 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -3623,7 +3623,6 @@ int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog,
 			    enum bpf_stream_id stream_id);
 int bpf_stream_stage_dump_stack(struct bpf_stream_stage *ss);
 
-#define bpf_stream_printk(ss, ...) bpf_stream_stage_printk(&ss, __VA_ARGS__)
 #define bpf_stream_dump_stack(ss) bpf_stream_stage_dump_stack(&ss)
 
 #define bpf_stream_stage(ss, prog, stream_id, expr)            \
diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 61613785bdd0..cc8e076f6d54 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -3178,7 +3178,7 @@ static noinline void bpf_prog_report_may_goto_violation(void)
 	if (!prog)
 		return;
 	bpf_stream_stage(ss, prog, BPF_STDERR, ({
-		bpf_stream_printk(ss, "ERROR: Timeout detected for may_goto instruction\n");
+		bpf_stream_stage_printk(&ss, "ERROR: Timeout detected for may_goto instruction\n");
 		bpf_stream_dump_stack(ss);
 	}));
 #endif
diff --git a/kernel/bpf/rqspinlock.c b/kernel/bpf/rqspinlock.c
index 5ab354d55d82..b176a6f9e431 100644
--- a/kernel/bpf/rqspinlock.c
+++ b/kernel/bpf/rqspinlock.c
@@ -676,11 +676,11 @@ static void bpf_prog_report_rqspinlock_violation(const char *str, void *lock, bo
 	if (!prog)
 		return;
 	bpf_stream_stage(ss, prog, BPF_STDERR, ({
-		bpf_stream_printk(ss, "ERROR: %s for bpf_res_spin_lock%s\n", str, irqsave ? "_irqsave" : "");
-		bpf_stream_printk(ss, "Attempted lock   = 0x%px\n", lock);
-		bpf_stream_printk(ss, "Total held locks = %d\n", rqh->cnt);
+		bpf_stream_stage_printk(&ss, "ERROR: %s for bpf_res_spin_lock%s\n", str, irqsave ? "_irqsave" : "");
+		bpf_stream_stage_printk(&ss, "Attempted lock   = 0x%px\n", lock);
+		bpf_stream_stage_printk(&ss, "Total held locks = %d\n", rqh->cnt);
 		for (int i = 0; i < min(RES_NR_HELD, rqh->cnt); i++)
-			bpf_stream_printk(ss, "Held lock[%2d] = 0x%px\n", i, rqh->locks[i]);
+			bpf_stream_stage_printk(&ss, "Held lock[%2d] = 0x%px\n", i, rqh->locks[i]);
 		bpf_stream_dump_stack(ss);
 	}));
 }
-- 
2.43.0
Re: [PATCH v2 bpf-next 1/2] bpf: Fix macro redefined
Posted by Alexei Starovoitov 2 months, 3 weeks ago
On Wed, Jul 16, 2025 at 8:29 PM Feng Yang <yangfeng59949@163.com> wrote:
>
> From: Feng Yang <yangfeng@kylinos.cn>
>
> When compiling a program that include <linux/bpf.h> and <bpf/bpf_helpers.h>, (For example: make samples/bpf)
> the following warning will be generated:
> In file included from tcp_dumpstats_kern.c:7:
> samples/bpf/libbpf/include/bpf/bpf_helpers.h:321:9: warning: 'bpf_stream_printk' macro redefined [-Wmacro-redefined]
>   321 | #define bpf_stream_printk(stream_id, fmt, args...)                              \
>       |         ^
> include/linux/bpf.h:3626:9: note: previous definition is here
>  3626 | #define bpf_stream_printk(ss, ...) bpf_stream_stage_printk(&ss, __VA_ARGS__)
>       |         ^
>
> The main reason is due to below in sample/bpf/Makefile:

No. We're not going to change kernel code because of samples.

--
pw-bot: cr