linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86: profiling: Using generic unwinding in profile_pc
@ 2023-02-24  2:18 Chen Zhongjin
  2023-04-03  1:24 ` Chen Zhongjin
  0 siblings, 1 reply; 4+ messages in thread
From: Chen Zhongjin @ 2023-02-24  2:18 UTC (permalink / raw)
  To: x86, linux-kernel; +Cc: tglx, mingo, bp, dave.hansen, hpa, chenzhongjin, ak

Syzbot has been reporting the problem of stack-out-of-bounds in
profile_pc for a long time:
https://syzkaller.appspot.com/bug?extid=84fe685c02cd112a2ac3

profile_pc tries to get pc if current regs is inside lock function. For
!CONFIG_FRAME_POINTER it used a hack way to get the pc from stack, which
is not work with ORC. It makes profile_pc returns wrong result, and
frequently triggers KASAN.

This can be fixed by using the unwind_start, it will skip the first
regs frame and get the caller of lock function directly, or 0 if
unwind_get_return_address finds the unwinding failed. For all of FP, ORC
and guess unwinders it works.

Fixes: 0cb91a229364 ("[PATCH] i386: Account spinlocks to the caller during profiling for !FP kernels")
Reported-by: syzbot+84fe685c02cd112a2ac3@syzkaller.appspotmail.com
Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
---
 arch/x86/kernel/time.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/arch/x86/kernel/time.c b/arch/x86/kernel/time.c
index e42faa792c07..5e0446f49906 100644
--- a/arch/x86/kernel/time.c
+++ b/arch/x86/kernel/time.c
@@ -24,26 +24,18 @@
 #include <asm/timer.h>
 #include <asm/hpet.h>
 #include <asm/time.h>
+#include <asm/unwind.h>
 
 unsigned long profile_pc(struct pt_regs *regs)
 {
 	unsigned long pc = instruction_pointer(regs);
 
 	if (!user_mode(regs) && in_lock_functions(pc)) {
-#ifdef CONFIG_FRAME_POINTER
-		return *(unsigned long *)(regs->bp + sizeof(long));
-#else
-		unsigned long *sp = (unsigned long *)regs->sp;
-		/*
-		 * Return address is either directly at stack pointer
-		 * or above a saved flags. Eflags has bits 22-31 zero,
-		 * kernel addresses don't.
-		 */
-		if (sp[0] >> 22)
-			return sp[0];
-		if (sp[1] >> 22)
-			return sp[1];
-#endif
+		struct unwind_state state;
+
+		/* unwind_start will skip the first regs frame */
+		unwind_start(&state, current, regs, NULL);
+		pc = unwind_get_return_address(&state);
 	}
 	return pc;
 }
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86: profiling: Using generic unwinding in profile_pc
  2023-02-24  2:18 [PATCH] x86: profiling: Using generic unwinding in profile_pc Chen Zhongjin
@ 2023-04-03  1:24 ` Chen Zhongjin
  2023-04-03 18:29   ` Andi Kleen
  0 siblings, 1 reply; 4+ messages in thread
From: Chen Zhongjin @ 2023-04-03  1:24 UTC (permalink / raw)
  To: x86, linux-kernel; +Cc: tglx, mingo, bp, dave.hansen, hpa, ak

Just ping... Or has profile code already been obsoleted?

On 2023/2/24 10:18, chenzhongjin wrote:
> Syzbot has been reporting the problem of stack-out-of-bounds in
> profile_pc for a long time:
> https://syzkaller.appspot.com/bug?extid=84fe685c02cd112a2ac3
>
> profile_pc tries to get pc if current regs is inside lock function. For
> !CONFIG_FRAME_POINTER it used a hack way to get the pc from stack, which
> is not work with ORC. It makes profile_pc returns wrong result, and
> frequently triggers KASAN.
>
> This can be fixed by using the unwind_start, it will skip the first
> regs frame and get the caller of lock function directly, or 0 if
> unwind_get_return_address finds the unwinding failed. For all of FP, ORC
> and guess unwinders it works.
>
> Fixes: 0cb91a229364 ("[PATCH] i386: Account spinlocks to the caller during profiling for !FP kernels")
> Reported-by: syzbot+84fe685c02cd112a2ac3@syzkaller.appspotmail.com
> Signed-off-by: Chen Zhongjin <chenzhongjin@huawei.com>
> ---
>   arch/x86/kernel/time.c | 20 ++++++--------------
>   1 file changed, 6 insertions(+), 14 deletions(-)
>
> diff --git a/arch/x86/kernel/time.c b/arch/x86/kernel/time.c
> index e42faa792c07..5e0446f49906 100644
> --- a/arch/x86/kernel/time.c
> +++ b/arch/x86/kernel/time.c
> @@ -24,26 +24,18 @@
>   #include <asm/timer.h>
>   #include <asm/hpet.h>
>   #include <asm/time.h>
> +#include <asm/unwind.h>
>   
>   unsigned long profile_pc(struct pt_regs *regs)
>   {
>   	unsigned long pc = instruction_pointer(regs);
>   
>   	if (!user_mode(regs) && in_lock_functions(pc)) {
> -#ifdef CONFIG_FRAME_POINTER
> -		return *(unsigned long *)(regs->bp + sizeof(long));
> -#else
> -		unsigned long *sp = (unsigned long *)regs->sp;
> -		/*
> -		 * Return address is either directly at stack pointer
> -		 * or above a saved flags. Eflags has bits 22-31 zero,
> -		 * kernel addresses don't.
> -		 */
> -		if (sp[0] >> 22)
> -			return sp[0];
> -		if (sp[1] >> 22)
> -			return sp[1];
> -#endif
> +		struct unwind_state state;
> +
> +		/* unwind_start will skip the first regs frame */
> +		unwind_start(&state, current, regs, NULL);
> +		pc = unwind_get_return_address(&state);
>   	}
>   	return pc;
>   }

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86: profiling: Using generic unwinding in profile_pc
  2023-04-03  1:24 ` Chen Zhongjin
@ 2023-04-03 18:29   ` Andi Kleen
  2023-04-04  2:03     ` Chen Zhongjin
  0 siblings, 1 reply; 4+ messages in thread
From: Andi Kleen @ 2023-04-03 18:29 UTC (permalink / raw)
  To: Chen Zhongjin, x86, linux-kernel; +Cc: tglx, mingo, bp, dave.hansen, hpa


On 4/2/2023 6:24 PM, Chen Zhongjin wrote:
> Just ping... Or has profile code already been obsoleted?


I think it would be reasonable to remove the locked functions hack since 
lock profiling can be handled with much better other tools these days.

I wouldn't make it depend on the generic unwinder since such a low level 
facility is likely better off without complex dependencies that could break.

-Andi



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86: profiling: Using generic unwinding in profile_pc
  2023-04-03 18:29   ` Andi Kleen
@ 2023-04-04  2:03     ` Chen Zhongjin
  0 siblings, 0 replies; 4+ messages in thread
From: Chen Zhongjin @ 2023-04-04  2:03 UTC (permalink / raw)
  To: Andi Kleen, x86, linux-kernel; +Cc: tglx, mingo, bp, dave.hansen, hpa


On 2023/4/4 2:29, Andi Kleen wrote:
>
> On 4/2/2023 6:24 PM, Chen Zhongjin wrote:
>> Just ping... Or has profile code already been obsoleted?
>
>
> I think it would be reasonable to remove the locked functions hack 
> since lock profiling can be handled with much better other tools these 
> days.
>
> I wouldn't make it depend on the generic unwinder since such a low 
> level facility is likely better off without complex dependencies that 
> could break.
>
> -Andi
>
>
Although now the generic unwinder on x86 is quite stable... I think it's 
acceptable to remove the locked functions unwinding
for !FP case and leave the FP part as is.

I'll send a new patch for this with another bugfix.

Thanks for review and best,
Chen

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-04-04  2:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-24  2:18 [PATCH] x86: profiling: Using generic unwinding in profile_pc Chen Zhongjin
2023-04-03  1:24 ` Chen Zhongjin
2023-04-03 18:29   ` Andi Kleen
2023-04-04  2:03     ` Chen Zhongjin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).