[PATCH] bpf: fix dump_stack() type cast

Arnd Bergmann posted 1 patch 3 months ago
kernel/bpf/stream.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] bpf: fix dump_stack() type cast
Posted by Arnd Bergmann 3 months ago
From: Arnd Bergmann <arnd@arndb.de>

Passing a pointer as a 'u64' variable requires a double cast when
converting it back to a pointer:

kernel/bpf/stream.c: In function 'dump_stack_cb':
kernel/bpf/stream.c:505:64: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
  505 |         ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n", (void *)ip);
      |                                                                ^

Fixes: d7c431cafcb4 ("bpf: Add dump_stack() analogue to print to BPF stderr")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 kernel/bpf/stream.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/stream.c b/kernel/bpf/stream.c
index 8c842f845245..24433cdf6ede 100644
--- a/kernel/bpf/stream.c
+++ b/kernel/bpf/stream.c
@@ -498,11 +498,11 @@ static bool dump_stack_cb(void *cookie, u64 ip, u64 sp, u64 bp)
 		if (ret < 0)
 			goto end;
 		ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n  %s @ %s:%d\n",
-						    (void *)ip, line, file, num);
+					    (void *)(uintptr_t)ip, line, file, num);
 		return !ctxp->err;
 	}
 end:
-	ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n", (void *)ip);
+	ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n", (void *)(uintptr_t)ip);
 	return !ctxp->err;
 }
 
-- 
2.39.5
Re: [PATCH] bpf: fix dump_stack() type cast
Posted by Eduard Zingerman 3 months ago
On Tue, 2025-07-08 at 18:07 +0200, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
> 
> Passing a pointer as a 'u64' variable requires a double cast when
> converting it back to a pointer:
> 
> kernel/bpf/stream.c: In function 'dump_stack_cb':
> kernel/bpf/stream.c:505:64: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
>   505 |         ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n", (void *)ip);
>       |                                                                ^
> 
> Fixes: d7c431cafcb4 ("bpf: Add dump_stack() analogue to print to BPF stderr")
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---

This warning is already fixed in
bfa2bb9abd99be ("bpf: Fix improper int-to-ptr cast in dump_stack_cb"),
which landed in bpf-next yesterday.

[...]