riscv: add CALLER_ADDRx support
PATCH 1/1 riscv: add CALLER_ADDRx support
CALLER_ADDRx returns caller's address at specified level, they are used for several tracers. These macros eventually use __builtin_return_address(n) to get the caller's address if arch doesn't define their own implementation.
In RISC-V, __builtin_return_address(n) only works when n == 0, we need to walk the stack frame to get the caller's address at specified level.
data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows:
callsite -> return_address -> arch_stack_walk -> walk_stackframe
| | | | level 3 level 2 level 1 level 0
--- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 arch/riscv/kernel/return_address.c
arch/riscv/include/asm/ftrace.h +5 -0
@@ -25,6 +25,11 @@#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__ + +extern void *return_address (unsigned int level); + +#define ftrace_return_address (n) return_address(n) +void MCOUNT_NAME (void );static inline unsigned long ftrace_call_adjust (unsigned long addr) {
arch/riscv/kernel/Makefile +2 -0
@@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)
@@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.o
arch/riscv/kernel/return_address.c new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 116 lines · Zong Li
CALLER_ADDRxreturns caller's address at specified level, they are used for several tracers. These macros eventually use__builtin_return_address(n)to get the caller's address if arch doesn't define their own implementation.In RISC-V,
__builtin_return_address(n)only works when n == 0, we need to walk the stack frame to get the caller's address at specified level.data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows:
callsite ->
return_address->arch_stack_walk->walk_stackframe| | | | level 3 level 2 level 1 level 0Signed-off-by Zong Li <[email protected]>--- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 arch/riscv/kernel/return_address.carch/riscv/include/asm/ftrace.h +5
-0@@ -25,6 +25,11 @@#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__ + +extern void *return_address (unsigned int level); + +#define ftrace_return_address (n) return_address(n) +void MCOUNT_NAME (void );static inline unsigned long ftrace_call_adjust (unsigned long addr) {arch/riscv/kernel/Makefile +2
-0@@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)@@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.oarch/riscv/kernel/return_address.c new file +48
-1@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* This code come from arch/arm64/kernel/return_address.c +* +* Copyright (C) 2023 SiFive. +*/ + +#include <linux/export.h> +#include <linux/kprobes.h> +#include <linux/stacktrace.h> + +struct return_address_data { +unsigned int level; +void *addr; +}; + +static bool save_return_addr (void *d,unsigned long pc) +{ +struct return_address_data *data = d; + +if (!data->level) { + data->addr = (void *)pc; +return false; + } + + --data->level; + +return true; +} +NOKPROBE_SYMBOL (save_return_addr); + +void *return_address (unsigned int level) +{ +struct return_address_data data; + + data.level = level +3 ; + data.addr =NULL ; + +arch_stack_walk (save_return_addr, &data, current,NULL ); + +if (!data.level) +return data.addr; +else +return NULL ; + +} +EXPORT_SYMBOL_GPL (return_address); +NOKPROBE_SYMBOL (return_address); --
Quoted 2 lines
2.17.1
Hi Palmer and all,
I was wondering whether this patch is good for everyone? Thanks
Quoted 115 lines · Zong Li
Quoted 113 lines · Zong Li
CALLER_ADDRxreturns caller's address at specified level, they are used for several tracers. These macros eventually use__builtin_return_address(n)to get the caller's address if arch doesn't define their own implementation.In RISC-V,
__builtin_return_address(n)only works when n == 0, we need to walk the stack frame to get the caller's address at specified level.data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows:
callsite ->
return_address->arch_stack_walk->walk_stackframe| | | | level 3 level 2 level 1 level 0Signed-off-by Zong Li <[email protected]>--- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 arch/riscv/kernel/return_address.carch/riscv/include/asm/ftrace.h +5
-0@@ -25,6 +25,11 @@#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__ + +extern void *return_address (unsigned int level); + +#define ftrace_return_address (n) return_address(n) +void MCOUNT_NAME (void );static inline unsigned long ftrace_call_adjust (unsigned long addr) {arch/riscv/kernel/Makefile +2
-0@@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)@@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.oarch/riscv/kernel/return_address.c new file +46
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* This code come from arch/arm64/kernel/return_address.c +* +* Copyright (C) 2023 SiFive. +*/ + +#include <linux/export.h> +#include <linux/kprobes.h> +#include <linux/stacktrace.h> + +struct return_address_data { +unsigned int level; +void *addr; +}; + +static bool save_return_addr (void *d,unsigned long pc) +{ +struct return_address_data *data = d; + +if (!data->level) { + data->addr = (void *)pc; +return false; + } + + --data->level; + +return true; +} +NOKPROBE_SYMBOL (save_return_addr); + +void *return_address (unsigned int level) +{ +struct return_address_data data; + + data.level = level +3 ; + data.addr =NULL ; + +arch_stack_walk (save_return_addr, &data, current,NULL ); + +if (!data.level) +return data.addr; +else +return NULL ; + +}
Quoted 8 lines
Quoted 5 lines
arch/riscv/kernel/return_address.c new file +2
-1+EXPORT_SYMBOL_GPL (return_address); +NOKPROBE_SYMBOL (return_address); --2.17.1
Hi Palmer and all,
I was wondering whether this patch is good for everyone? Thanks
Hi Palmer,
Is there any chance to include this patch in 6.8-rc1? Thanks
Quoted 126 lines · Zong Li
Quoted 115 lines · Zong Li
Quoted 113 lines · Zong Li
CALLER_ADDRxreturns caller's address at specified level, they are used for several tracers. These macros eventually use__builtin_return_address(n)to get the caller's address if arch doesn't define their own implementation.In RISC-V,
__builtin_return_address(n)only works when n == 0, we need to walk the stack frame to get the caller's address at specified level.data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows:
callsite ->
return_address->arch_stack_walk->walk_stackframe| | | | level 3 level 2 level 1 level 0Signed-off-by Zong Li <[email protected]>--- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 arch/riscv/kernel/return_address.carch/riscv/include/asm/ftrace.h +5
-0@@ -25,6 +25,11 @@#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__ + +extern void *return_address (unsigned int level); + +#define ftrace_return_address (n) return_address(n) +void MCOUNT_NAME (void );static inline unsigned long ftrace_call_adjust (unsigned long addr) {arch/riscv/kernel/Makefile +2
-0@@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)@@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.oarch/riscv/kernel/return_address.c new file +46
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* This code come from arch/arm64/kernel/return_address.c +* +* Copyright (C) 2023 SiFive. +*/ + +#include <linux/export.h> +#include <linux/kprobes.h> +#include <linux/stacktrace.h> + +struct return_address_data { +unsigned int level; +void *addr; +}; + +static bool save_return_addr (void *d,unsigned long pc) +{ +struct return_address_data *data = d; + +if (!data->level) { + data->addr = (void *)pc; +return false; + } + + --data->level; + +return true; +} +NOKPROBE_SYMBOL (save_return_addr); + +void *return_address (unsigned int level) +{ +struct return_address_data data; + + data.level = level +3 ; + data.addr =NULL ; + +arch_stack_walk (save_return_addr, &data, current,NULL ); + +if (!data.level) +return data.addr; +else +return NULL ; + +}Quoted 8 lines
Quoted 5 lines
arch/riscv/kernel/return_address.c new file +2
-1+EXPORT_SYMBOL_GPL (return_address); +NOKPROBE_SYMBOL (return_address); --2.17.1
Hi Palmer and all,
I was wondering whether this patch is good for everyone? Thanks
Quoted 2 lines
Hi Palmer,
Is there any chance to include this patch in 6.8-rc1? Thanks
Hi Palmer,
I'm not sure if this patch is a feature or bug fix, would you consider it for 6.8-rcX? Thanks
Quoted 128 lines · Zong Li
Quoted 126 lines · Zong Li
Quoted 123 lines · Zong Li
Quoted 116 lines · Zong Li
CALLER_ADDRxreturns caller's address at specified level, they are used for several tracers. These macros eventually use__builtin_return_address(n)to get the caller's address if arch doesn't define their own implementation.In RISC-V,
__builtin_return_address(n)only works when n == 0, we need to walk the stack frame to get the caller's address at specified level.data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows:
callsite ->
return_address->arch_stack_walk->walk_stackframe| | | | level 3 level 2 level 1 level 0Signed-off-by Zong Li <[email protected]>--- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 arch/riscv/kernel/return_address.carch/riscv/include/asm/ftrace.h +5
-0@@ -25,6 +25,11 @@#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__ + +extern void *return_address (unsigned int level); + +#define ftrace_return_address (n) return_address(n) +void MCOUNT_NAME (void );static inline unsigned long ftrace_call_adjust (unsigned long addr) {arch/riscv/kernel/Makefile +2
-0@@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)@@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.oarch/riscv/kernel/return_address.c new file +48
-1@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* This code come from arch/arm64/kernel/return_address.c +* +* Copyright (C) 2023 SiFive. +*/ + +#include <linux/export.h> +#include <linux/kprobes.h> +#include <linux/stacktrace.h> + +struct return_address_data { +unsigned int level; +void *addr; +}; + +static bool save_return_addr (void *d,unsigned long pc) +{ +struct return_address_data *data = d; + +if (!data->level) { + data->addr = (void *)pc; +return false; + } + + --data->level; + +return true; +} +NOKPROBE_SYMBOL (save_return_addr); + +void *return_address (unsigned int level) +{ +struct return_address_data data; + + data.level = level +3 ; + data.addr =NULL ; + +arch_stack_walk (save_return_addr, &data, current,NULL ); + +if (!data.level) +return data.addr; +else +return NULL ; + +} +EXPORT_SYMBOL_GPL (return_address); +NOKPROBE_SYMBOL (return_address); --Quoted 2 lines
2.17.1
Hi Palmer and all,
I was wondering whether this patch is good for everyone? Thanks
Quoted 6 lines
Quoted 2 lines
Hi Palmer,
Is there any chance to include this patch in 6.8-rc1? ThanksHi Palmer,
I'm not sure if this patch is a feature or bug fix, would you consider it for 6.8-rcX? Thanks
Hi Palmer,
Sorry for pinging you again. I'm not sure if you saw this patch. The irqsoff and wakeup tracer will use CALLER_ADDR1 and CALLER_ADDR2 to obtain the caller's return address, but we are currently encountering an issue in the RISC-V port where the wrong caller is identified. I guess you can easily reproduce the situation using ftrace. Could you share your thoughts on this when you have the time to take a look? Thanks
Alexandre Ghiti
reviewerHi Zong,
Quoted 128 lines · Zong Li
Quoted 122 lines · Zong Li
Quoted 121 lines · Zong Li
Quoted 120 lines · Zong Li
Quoted 115 lines · Zong Li
CALLER_ADDRxreturns caller's address at specified level, they are used for several tracers. These macros eventually use__builtin_return_address(n)to get the caller's address if arch doesn't define their own implementation.In RISC-V,
__builtin_return_address(n)only works when n == 0, we need to walk the stack frame to get the caller's address at specified level.data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows:
callsite ->
return_address->arch_stack_walk->walk_stackframe| | | | level 3 level 2 level 1 level 0Signed-off-by Zong Li <[email protected]>--- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+)create mode 100644
arch/riscv/kernel/return_address.carch/riscv/include/asm/ftrace.h +5
-0@@ -25,6 +25,11 @@#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__ + +extern void *return_address (unsigned int level); + +#define ftrace_return_address (n) return_address(n) +void MCOUNT_NAME (void );static inline unsigned long ftrace_call_adjust (unsigned long addr) {arch/riscv/kernel/Makefile +2
-0@@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)@@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.oarch/riscv/kernel/return_address.c new file +48
-1@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* This code come from arch/arm64/kernel/return_address.c +* +* Copyright (C) 2023 SiFive. +*/ + +#include <linux/export.h> +#include <linux/kprobes.h> +#include <linux/stacktrace.h> + +struct return_address_data { +unsigned int level; +void *addr; +}; + +static bool save_return_addr (void *d,unsigned long pc) +{ +struct return_address_data *data = d; + +if (!data->level) { + data->addr = (void *)pc; +return false; + } + + --data->level; + +return true; +} +NOKPROBE_SYMBOL (save_return_addr); + +void *return_address (unsigned int level) +{ +struct return_address_data data; + + data.level = level +3 ; + data.addr =NULL ; + +arch_stack_walk (save_return_addr, &data, current,NULL ); + +if (!data.level) +return data.addr; +else +return NULL ; + +} +EXPORT_SYMBOL_GPL (return_address); +NOKPROBE_SYMBOL (return_address); --Quoted 2 lines
2.17.1
Hi Palmer and all,
I was wondering whether this patch is good for everyone? ThanksQuoted 5 lines
Quoted 2 lines
Hi Palmer,
Is there any chance to include this patch in 6.8-rc1? ThanksHi Palmer,
I'm not sure if this patch is a feature or bug fix, would you consider it for 6.8-rcX? Thanks
Quoted 8 lines
Hi Palmer,
Sorry for pinging you again. I'm not sure if you saw this patch. The irqsoff and wakeup tracer will useCALLER_ADDR1andCALLER_ADDR2to obtain the caller's return address, but we are currently encountering an issue in the RISC-V port where the wrong caller is identified. I guess you can easily reproduce the situation using ftrace. Could you share your thoughts on this when you have the time to take a look? Thanks
I'm not Palmer but I'll take a look at your patch today :)
Thanks,
Alex
Quoted 5 lines
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 142 lines · Alexandre Ghiti
Hi Zong,
Quoted 128 lines · Zong Li
Quoted 122 lines · Zong Li
Quoted 121 lines · Zong Li
Quoted 120 lines · Zong Li
Quoted 115 lines · Zong Li
CALLER_ADDRxreturns caller's address at specified level, they are used for several tracers. These macros eventually use__builtin_return_address(n)to get the caller's address if arch doesn't define their own implementation.In RISC-V,
__builtin_return_address(n)only works when n == 0, we need to walk the stack frame to get the caller's address at specified level.data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows:
callsite ->
return_address->arch_stack_walk->walk_stackframe| | | | level 3 level 2 level 1 level 0Signed-off-by Zong Li <[email protected]>--- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+)create mode 100644
arch/riscv/kernel/return_address.carch/riscv/include/asm/ftrace.h +5
-0@@ -25,6 +25,11 @@#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__ + +extern void *return_address (unsigned int level); + +#define ftrace_return_address (n) return_address(n) +void MCOUNT_NAME (void );static inline unsigned long ftrace_call_adjust (unsigned long addr) {arch/riscv/kernel/Makefile +2
-0@@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)@@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.oarch/riscv/kernel/return_address.c new file +48
-1@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* This code come from arch/arm64/kernel/return_address.c +* +* Copyright (C) 2023 SiFive. +*/ + +#include <linux/export.h> +#include <linux/kprobes.h> +#include <linux/stacktrace.h> + +struct return_address_data { +unsigned int level; +void *addr; +}; + +static bool save_return_addr (void *d,unsigned long pc) +{ +struct return_address_data *data = d; + +if (!data->level) { + data->addr = (void *)pc; +return false; + } + + --data->level; + +return true; +} +NOKPROBE_SYMBOL (save_return_addr); + +void *return_address (unsigned int level) +{ +struct return_address_data data; + + data.level = level +3 ; + data.addr =NULL ; + +arch_stack_walk (save_return_addr, &data, current,NULL ); + +if (!data.level) +return data.addr; +else +return NULL ; + +} +EXPORT_SYMBOL_GPL (return_address); +NOKPROBE_SYMBOL (return_address); --Quoted 2 lines
2.17.1
Hi Palmer and all,
I was wondering whether this patch is good for everyone? ThanksQuoted 5 lines
Quoted 2 lines
Hi Palmer,
Is there any chance to include this patch in 6.8-rc1? ThanksHi Palmer,
I'm not sure if this patch is a feature or bug fix, would you consider it for 6.8-rcX? ThanksQuoted 8 lines
Hi Palmer,
Sorry for pinging you again. I'm not sure if you saw this patch. The irqsoff and wakeup tracer will useCALLER_ADDR1andCALLER_ADDR2to obtain the caller's return address, but we are currently encountering an issue in the RISC-V port where the wrong caller is identified. I guess you can easily reproduce the situation using ftrace. Could you share your thoughts on this when you have the time to take a look? Thanks
Quoted 2 lines
I'm not Palmer but I'll take a look at your patch today :)
Hi Alexandre,
I appreciate your assistance in reviewing this patch, Thanks a lot. :)
Quoted 10 lines
Thanks,
Alex
Quoted 5 lines
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 7 lines · Zong Li
CALLER_ADDRxreturns caller's address at specified level, they are used for several tracers. These macros eventually use__builtin_return_address(n)to get the caller's address if arch doesn't define their own implementation.In RISC-V,
__builtin_return_address(n)only works when n == 0, we need to walk the stack frame to get the caller's address at specified level.
Is that a bug in gcc or something expected for riscv in general?
Quoted 81 lines
data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows:
callsite ->
return_address->arch_stack_walk->walk_stackframe| | | | level 3 level 2 level 1 level 0Signed-off-by Zong Li <[email protected]>--- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+)create mode 100644
arch/riscv/kernel/return_address.carch/riscv/include/asm/ftrace.h +5
-0@@ -25,6 +25,11 @@#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__ + +extern void *return_address (unsigned int level); + +#define ftrace_return_address (n) return_address(n) +void MCOUNT_NAME (void );static inline unsigned long ftrace_call_adjust (unsigned long addr) {arch/riscv/kernel/Makefile +2
-0@@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)@@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.oarch/riscv/kernel/return_address.c new file +22
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* This code come from arch/arm64/kernel/return_address.c +* +* Copyright (C) 2023 SiFive. +*/ + +#include <linux/export.h> +#include <linux/kprobes.h> +#include <linux/stacktrace.h> + +struct return_address_data { +unsigned int level; +void *addr; +}; + +static bool save_return_addr (void *d,unsigned long pc) +{ +struct return_address_data *data = d; + +if (!data->level) { + data->addr = (void *)pc;
arch/riscv/kernel/return_address.c line 32
Quoted 10 lines
arch/riscv/kernel/return_address.c new file +10
-0+return false; + } + + --data->level; + +return true; +} +NOKPROBE_SYMBOL (save_return_addr); + +void *return_address (unsigned int level)
Maybe return_address() should be noinline to make sure it's not inlined as it would break the "+ 3"? Not sure it's necessary though.
Quoted 6 lines
arch/riscv/kernel/return_address.c new file +6
-0+{ +struct return_address_data data; + + data.level = level +3 ; + data.addr =NULL ; +
arch/riscv/kernel/return_address.c line 48
Quoted 10 lines
arch/riscv/kernel/return_address.c new file +10
-0+arch_stack_walk (save_return_addr, &data, current,NULL ); + +if (!data.level) +return data.addr; +else +return NULL ; + +} +EXPORT_SYMBOL_GPL (return_address); +NOKPROBE_SYMBOL (return_address);
And I see that with this patch, ftrace_return_address() is now defined whether CONFIG_FRAME_POINTER is set or not, is that correct?
This looks like a fix to me so that should go into -fixes with a Fixes tag (but we'll have to make sure that the "+ 3" is correct on all backports...):
And you can finally add for your v2 (or not if you don't respin):
Thanks and sorry for the delay!
Alex
Quoted 7 lines · Alexandre Ghiti
Quoted 5 lines · Zong Li
CALLER_ADDRxreturns caller's address at specified level, they are used for several tracers. These macros eventually use__builtin_return_address(n)to get the caller's address if arch doesn't define their own implementation.
Quoted 6 lines
Quoted 2 lines
In RISC-V,
__builtin_return_address(n)only works when n == 0, we need to walk the stack frame to get the caller's address at specified level.Is that a bug in gcc or something expected for riscv in general?
I think it isn't supported for riscv in general.
Quoted 88 lines
Quoted 88 lines
data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows:
callsite ->
return_address->arch_stack_walk->walk_stackframe| | | | level 3 level 2 level 1 level 0Signed-off-by Zong Li <[email protected]>--- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+)create mode 100644
arch/riscv/kernel/return_address.carch/riscv/include/asm/ftrace.h +5
-0@@ -25,6 +25,11 @@#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__ + +extern void *return_address (unsigned int level); + +#define ftrace_return_address (n) return_address(n) +void MCOUNT_NAME (void );static inline unsigned long ftrace_call_adjust (unsigned long addr) {arch/riscv/kernel/Makefile +2
-0@@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)@@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.oarch/riscv/kernel/return_address.c new file +28
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* This code come from arch/arm64/kernel/return_address.c +* +* Copyright (C) 2023 SiFive. +*/ + +#include <linux/export.h> +#include <linux/kprobes.h> +#include <linux/stacktrace.h> + +struct return_address_data { +unsigned int level; +void *addr; +}; + +static bool save_return_addr (void *d,unsigned long pc) +{ +struct return_address_data *data = d; + +if (!data->level) { + data->addr = (void *)pc; +return false; + } + + --data->level; + +return true;
Quoted 8 lines
arch/riscv/kernel/return_address.c line 32
Quoted 4 lines
arch/riscv/kernel/return_address.c new file +4
-0+} +NOKPROBE_SYMBOL (save_return_addr); + +void *return_address (unsigned int level)Maybe
return_address()should be noinline to make sure it's not inlined as it would break the "+ 3"? Not sure it's necessary though.
Yes, thanks for pointing it out. We should ensure it's not inlined in any case. I will send the next version.
Quoted 14 lines
Quoted 14 lines
arch/riscv/kernel/return_address.c new file +12
-0+{ +struct return_address_data data; + + data.level = level +3 ; + data.addr =NULL ; + +arch_stack_walk (save_return_addr, &data, current,NULL ); + +if (!data.level) +return data.addr; +else +return NULL ;
Quoted 8 lines
arch/riscv/kernel/return_address.c line 48
Quoted 4 lines
arch/riscv/kernel/return_address.c new file +4
-0+ +} +EXPORT_SYMBOL_GPL (return_address); +NOKPROBE_SYMBOL (return_address);And I see that with this patch,
ftrace_return_address()is now defined whetherCONFIG_FRAME_POINTERis set or not, is that correct?
Yes, that is what I understand. In this patch, the ftrace_return_address() is still defined to return_address() when CONFIG_FRAME_POINTER isn't enabled, and return_address still works because riscv port can walk stackframe without fp.
Quoted 6 lines
This looks like a fix to me so that should go into -fixes with a Fixes tag (but we'll have to make sure that the "+ 3" is correct on all backports...):
Fixes 10626c32e382 ("riscv/ftrace: Add basic support")
The return_address() invokes arch_stack_walk(), which enabled by the '5cb0080f1bfd ("riscv: Enable ARCH_STACKWALK")'
I guess that we are not able to apply it on all backports. Is this right? "+3" is correct after enabling ARCH_STACKWALK.
Quoted 4 lines
And you can finally add for your v2 (or not if you don't respin):
Reviewed-by Alexandre Ghiti <[email protected]>
Thanks for the review, Alexandre. I will add it in v2:)
Quoted 5 lines
Thanks and sorry for the delay!
Alex
Alexandre Ghiti
reviewerQuoted 139 lines · Zong Li
Quoted 11 lines · Alexandre Ghiti
Quoted 7 lines · Zong Li
CALLER_ADDRxreturns caller's address at specified level, they are used for several tracers. These macros eventually use__builtin_return_address(n)to get the caller's address if arch doesn't define their own implementation.In RISC-V,
__builtin_return_address(n)only works when n == 0, we need to walk the stack frame to get the caller's address at specified level.Is that a bug in gcc or something expected for riscv in general?
I think it isn't supported for riscv in general.
Quoted 85 lines
Quoted 85 lines
data.level started from 'level + 3' due to the call flow of getting caller's address in RISC-V implementation. If we don't have additional three iteration, the level is corresponding to follows:
callsite ->
return_address->arch_stack_walk->walk_stackframe| | | | level 3 level 2 level 1 level 0Signed-off-by Zong Li <[email protected]>--- arch/riscv/include/asm/ftrace.h | 5 ++++ arch/riscv/kernel/Makefile | 2 ++ arch/riscv/kernel/return_address.c | 48 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+)create mode 100644
arch/riscv/kernel/return_address.carch/riscv/include/asm/ftrace.h +5
-0@@ -25,6 +25,11 @@#define ARCH_SUPPORTS_FTRACE_OPS 1#ifndef __ASSEMBLY__ + +extern void *return_address (unsigned int level); + +#define ftrace_return_address (n) return_address(n) +void MCOUNT_NAME (void );static inline unsigned long ftrace_call_adjust (unsigned long addr) {arch/riscv/kernel/Makefile +2
-0@@ -7,6 +7,7 @@ ifdef CONFIG_FTRACE CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_patch.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi.o = $(CC_FLAGS_FTRACE) +CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) endif CFLAGS_syscall_table.o += $(call cc-option,-Wno-override-init,) CFLAGS_compat_syscall_table.o += $(call cc-option,-Wno-override-init,)@@ -46,6 +47,7 @@ obj-y += irq.o obj-y += process.o obj-y += ptrace.o obj-y += reset.o +obj-y += return_address.o obj-y += setup.o obj-y += signal.o obj-y += syscall_table.oarch/riscv/kernel/return_address.c new file +27
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* This code come from arch/arm64/kernel/return_address.c +* +* Copyright (C) 2023 SiFive. +*/ + +#include <linux/export.h> +#include <linux/kprobes.h> +#include <linux/stacktrace.h> + +struct return_address_data { +unsigned int level; +void *addr; +}; + +static bool save_return_addr (void *d,unsigned long pc) +{ +struct return_address_data *data = d; + +if (!data->level) { + data->addr = (void *)pc; +return false; + } + + --data->level; +Quoted 8 lines
arch/riscv/kernel/return_address.c line 32
Quoted 5 lines
arch/riscv/kernel/return_address.c new file +5
-0+return true; +} +NOKPROBE_SYMBOL (save_return_addr); + +void *return_address (unsigned int level)Maybe
return_address()should be noinline to make sure it's not inlined as it would break the "+ 3"? Not sure it's necessary though.Yes, thanks for pointing it out. We should ensure it's not inlined in any case. I will send the next version.
Quoted 12 lines
Quoted 12 lines
arch/riscv/kernel/return_address.c new file +11
-0+{ +struct return_address_data data; + + data.level = level +3 ; + data.addr =NULL ; + +arch_stack_walk (save_return_addr, &data, current,NULL ); + +if (!data.level) +return data.addr; +else Quoted 8 lines
arch/riscv/kernel/return_address.c line 48
Quoted 5 lines
arch/riscv/kernel/return_address.c new file +5
-0+return NULL ; + +} +EXPORT_SYMBOL_GPL (return_address); +NOKPROBE_SYMBOL (return_address);And I see that with this patch,
ftrace_return_address()is now defined whetherCONFIG_FRAME_POINTERis set or not, is that correct?Yes, that is what I understand. In this patch, the
ftrace_return_address()is still defined toreturn_address()whenCONFIG_FRAME_POINTERisn't enabled, andreturn_addressstill works because riscv port can walk stackframe without fp.Quoted 4 lines
This looks like a fix to me so that should go into -fixes with a Fixes tag (but we'll have to make sure that the "+ 3" is correct on all backports...):
Quoted 6 lines
Quoted 1 line
Fixes 10626c32e382 ("riscv/ftrace: Add basic support")The
return_address()invokesarch_stack_walk(), which enabled by the '5cb0080f1bfd("riscv: EnableARCH_STACKWALK")'I guess that we are not able to apply it on all backports. Is this right? "+3" is correct after enabling
ARCH_STACKWALK.
5cb0080f1bfd was introduced in v5.11, so that will make the backport possible up to 5.15, I guess that's ok, nobody will ever use a riscv kernel that old (?).
So I'd add the Fixes tag I proposed above, and let the backport fail for < 5.15. @Conor any opinion?
Quoted 5 lines
Quoted 4 lines
And you can finally add for your v2 (or not if you don't respin):
Reviewed-by Alexandre Ghiti <[email protected]>Thanks for the review, Alexandre. I will add it in v2:)
Thanks,
Alex
Quoted 9 lines
Quoted 5 lines
Thanks and sorry for the delay!
Alex
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 3 lines · Alexandre Ghiti
5cb0080f1bfdwas introduced in v5.11, so that will make the backport possible up to 5.15, I guess that's ok, nobody will ever use a riscv kernel that old (?).So I'd add the Fixes tag I proposed above, and let the backport fail for < 5.15. @Conor any opinion?
The stable kernels are 5.10 and 5.15, so there's no kernels that matter which have the bad commit and are < 5.15.