All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] riscv/stacktrace: fix stackframe without ra on the top
@ 2020-11-27  8:45 ` Chen Huang
  0 siblings, 0 replies; 6+ messages in thread
From: Chen Huang @ 2020-11-27  8:45 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: Andrew Morton, linux-riscv, linux-kernel

When a function doesn't have a callee, then it will not push ra
into the stack, such as lkdtm_BUG() function:

addi	sp,sp,-16
sd	s0,8(sp)
addi	s0,sp,16
ebreak

Then we use pt_regs as a parameter to walk_stackframe(), for the
struct stackframe use {fp,ra} to get information from stack, it
will get the wrong value. And the call trace will be:

[<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
---[ end trace 18da3fbdf08e25d5 ]---

It should be that:
[<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
[<ffffffe0008b24a4>] lkdtm_do_action+0x14/0x1c
[<ffffffe00066c372>] direct_entry+0xc0/0x10a
[<ffffffe000439f86>] full_proxy_write+0x42/0x6a
[<ffffffe000309626>] vfs_write+0x7e/0x214
[<ffffffe00030992a>] ksys_write+0x98/0xc0
[<ffffffe000309960>] sys_write+0xe/0x16
[<ffffffe0002014bc>] ret_from_syscall+0x0/0x2
---[ end trace 61917f3d9a9fadcd ]---

Signed-off-by: Chen Huang <chenhuang5@huawei.com>
---
 arch/riscv/kernel/stacktrace.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
index 595342910c3f..d1d4c18335c4 100644
--- a/arch/riscv/kernel/stacktrace.c
+++ b/arch/riscv/kernel/stacktrace.c
@@ -57,7 +57,14 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
 		/* Unwind stack frame */
 		frame = (struct stackframe *)fp - 1;
 		sp = fp;
-		fp = frame->fp;
+		if (regs && (frame->fp & 0x7) && (pc == regs->epc)) {
+			fp = frame->ra;
+			pc = regs->ra;
+			continue;
+		} else {
+			fp = frame->fp;
+		}
+
 		pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
 					   (unsigned long *)(fp - 8));
 	}
-- 
2.17.1


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

* [PATCH] riscv/stacktrace: fix stackframe without ra on the top
@ 2020-11-27  8:45 ` Chen Huang
  0 siblings, 0 replies; 6+ messages in thread
From: Chen Huang @ 2020-11-27  8:45 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: Andrew Morton, linux-riscv, linux-kernel

When a function doesn't have a callee, then it will not push ra
into the stack, such as lkdtm_BUG() function:

addi	sp,sp,-16
sd	s0,8(sp)
addi	s0,sp,16
ebreak

Then we use pt_regs as a parameter to walk_stackframe(), for the
struct stackframe use {fp,ra} to get information from stack, it
will get the wrong value. And the call trace will be:

[<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
---[ end trace 18da3fbdf08e25d5 ]---

It should be that:
[<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
[<ffffffe0008b24a4>] lkdtm_do_action+0x14/0x1c
[<ffffffe00066c372>] direct_entry+0xc0/0x10a
[<ffffffe000439f86>] full_proxy_write+0x42/0x6a
[<ffffffe000309626>] vfs_write+0x7e/0x214
[<ffffffe00030992a>] ksys_write+0x98/0xc0
[<ffffffe000309960>] sys_write+0xe/0x16
[<ffffffe0002014bc>] ret_from_syscall+0x0/0x2
---[ end trace 61917f3d9a9fadcd ]---

Signed-off-by: Chen Huang <chenhuang5@huawei.com>
---
 arch/riscv/kernel/stacktrace.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
index 595342910c3f..d1d4c18335c4 100644
--- a/arch/riscv/kernel/stacktrace.c
+++ b/arch/riscv/kernel/stacktrace.c
@@ -57,7 +57,14 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
 		/* Unwind stack frame */
 		frame = (struct stackframe *)fp - 1;
 		sp = fp;
-		fp = frame->fp;
+		if (regs && (frame->fp & 0x7) && (pc == regs->epc)) {
+			fp = frame->ra;
+			pc = regs->ra;
+			continue;
+		} else {
+			fp = frame->fp;
+		}
+
 		pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
 					   (unsigned long *)(fp - 8));
 	}
-- 
2.17.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* Re: [PATCH] riscv/stacktrace: fix stackframe without ra on the top
  2020-11-27  8:45 ` Chen Huang
@ 2020-11-27 11:43   ` Kefeng Wang
  -1 siblings, 0 replies; 6+ messages in thread
From: Kefeng Wang @ 2020-11-27 11:43 UTC (permalink / raw)
  To: Chen Huang, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: Andrew Morton, linux-riscv, linux-kernel


On 2020/11/27 16:45, Chen Huang wrote:
> When a function doesn't have a callee, then it will not push ra
> into the stack, such as lkdtm_BUG() function:
>
> addi	sp,sp,-16
> sd	s0,8(sp)
> addi	s0,sp,16
> ebreak
>
> Then we use pt_regs as a parameter to walk_stackframe(), for the
> struct stackframe use {fp,ra} to get information from stack, it
> will get the wrong value. And the call trace will be:
>
> [<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
> ---[ end trace 18da3fbdf08e25d5 ]---
>
> It should be that:
> [<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
> [<ffffffe0008b24a4>] lkdtm_do_action+0x14/0x1c
> [<ffffffe00066c372>] direct_entry+0xc0/0x10a
> [<ffffffe000439f86>] full_proxy_write+0x42/0x6a
> [<ffffffe000309626>] vfs_write+0x7e/0x214
> [<ffffffe00030992a>] ksys_write+0x98/0xc0
> [<ffffffe000309960>] sys_write+0xe/0x16
> [<ffffffe0002014bc>] ret_from_syscall+0x0/0x2
> ---[ end trace 61917f3d9a9fadcd ]---
>
> Signed-off-by: Chen Huang <chenhuang5@huawei.com>
> ---
>   arch/riscv/kernel/stacktrace.c | 9 ++++++++-
>   1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
> index 595342910c3f..d1d4c18335c4 100644
> --- a/arch/riscv/kernel/stacktrace.c
> +++ b/arch/riscv/kernel/stacktrace.c
> @@ -57,7 +57,14 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
>   		/* Unwind stack frame */
>   		frame = (struct stackframe *)fp - 1;
>   		sp = fp;
> -		fp = frame->fp;
> +		if (regs && (frame->fp & 0x7) && (pc == regs->epc)) {
> +			fp = frame->ra;
> +			pc = regs->ra;
> +			continue;
> +		} else {
> +			fp = frame->fp;
> +		}
> +
>   		pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
>   					   (unsigned long *)(fp - 8));

Better to move above code into else branch

>   	}

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

* Re: [PATCH] riscv/stacktrace: fix stackframe without ra on the top
@ 2020-11-27 11:43   ` Kefeng Wang
  0 siblings, 0 replies; 6+ messages in thread
From: Kefeng Wang @ 2020-11-27 11:43 UTC (permalink / raw)
  To: Chen Huang, Paul Walmsley, Palmer Dabbelt, Albert Ou
  Cc: Andrew Morton, linux-riscv, linux-kernel


On 2020/11/27 16:45, Chen Huang wrote:
> When a function doesn't have a callee, then it will not push ra
> into the stack, such as lkdtm_BUG() function:
>
> addi	sp,sp,-16
> sd	s0,8(sp)
> addi	s0,sp,16
> ebreak
>
> Then we use pt_regs as a parameter to walk_stackframe(), for the
> struct stackframe use {fp,ra} to get information from stack, it
> will get the wrong value. And the call trace will be:
>
> [<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
> ---[ end trace 18da3fbdf08e25d5 ]---
>
> It should be that:
> [<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
> [<ffffffe0008b24a4>] lkdtm_do_action+0x14/0x1c
> [<ffffffe00066c372>] direct_entry+0xc0/0x10a
> [<ffffffe000439f86>] full_proxy_write+0x42/0x6a
> [<ffffffe000309626>] vfs_write+0x7e/0x214
> [<ffffffe00030992a>] ksys_write+0x98/0xc0
> [<ffffffe000309960>] sys_write+0xe/0x16
> [<ffffffe0002014bc>] ret_from_syscall+0x0/0x2
> ---[ end trace 61917f3d9a9fadcd ]---
>
> Signed-off-by: Chen Huang <chenhuang5@huawei.com>
> ---
>   arch/riscv/kernel/stacktrace.c | 9 ++++++++-
>   1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
> index 595342910c3f..d1d4c18335c4 100644
> --- a/arch/riscv/kernel/stacktrace.c
> +++ b/arch/riscv/kernel/stacktrace.c
> @@ -57,7 +57,14 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
>   		/* Unwind stack frame */
>   		frame = (struct stackframe *)fp - 1;
>   		sp = fp;
> -		fp = frame->fp;
> +		if (regs && (frame->fp & 0x7) && (pc == regs->epc)) {
> +			fp = frame->ra;
> +			pc = regs->ra;
> +			continue;
> +		} else {
> +			fp = frame->fp;
> +		}
> +
>   		pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
>   					   (unsigned long *)(fp - 8));

Better to move above code into else branch

>   	}

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

* [PATCH v2] riscv: stacktrace: fix stackframe without ra on the top
  2020-11-27 11:43   ` Kefeng Wang
@ 2020-11-28  6:22     ` Chen Huang
  -1 siblings, 0 replies; 6+ messages in thread
From: Chen Huang @ 2020-11-28  6:22 UTC (permalink / raw)
  To: wangkefeng.wang
  Cc: akpm, aou, chenhuang5, linux-kernel, linux-riscv, palmer, paul.walmsley

When a function doesn't have a callee, then it will not push ra
into the stack, such as lkdtm_BUG() function:

addi	sp,sp,-16
sd		s0,8(sp)
addi	s0,sp,16
ebreak

The struct stackframe use {fp,ra} to get information from
stack, if walk_stackframe() with pr_regs, we will obtain
wrong value and bad stacktrace,

[<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
---[ end trace 18da3fbdf08e25d5 ]---

Correct the next fp and pc, after that, full stacktrace
shown as expects,

[<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
[<ffffffe0008b24a4>] lkdtm_do_action+0x14/0x1c
[<ffffffe00066c372>] direct_entry+0xc0/0x10a
[<ffffffe000439f86>] full_proxy_write+0x42/0x6a
[<ffffffe000309626>] vfs_write+0x7e/0x214
[<ffffffe00030992a>] ksys_write+0x98/0xc0
[<ffffffe000309960>] sys_write+0xe/0x16
[<ffffffe0002014bc>] ret_from_syscall+0x0/0x2
---[ end trace 61917f3d9a9fadcd ]---

Signed-off-by: Chen Huang <chenhuang5@huawei.com>
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 arch/riscv/kernel/stacktrace.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
index 595342910c3f..3842fac26e85 100644
--- a/arch/riscv/kernel/stacktrace.c
+++ b/arch/riscv/kernel/stacktrace.c
@@ -57,9 +57,14 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
 		/* Unwind stack frame */
 		frame = (struct stackframe *)fp - 1;
 		sp = fp;
-		fp = frame->fp;
-		pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
-					   (unsigned long *)(fp - 8));
+		if (regs && (pc == regs->epc) && (frame->fp & 0x7)) {
+			fp = frame->ra;
+			pc = regs->ra;
+		} else {
+			fp = frame->fp;
+			pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
+						(unsigned long *)(fp - 8));
+		}
 	}
 }
 
-- 
2.17.1


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

* [PATCH v2] riscv: stacktrace: fix stackframe without ra on the top
@ 2020-11-28  6:22     ` Chen Huang
  0 siblings, 0 replies; 6+ messages in thread
From: Chen Huang @ 2020-11-28  6:22 UTC (permalink / raw)
  To: wangkefeng.wang
  Cc: aou, chenhuang5, linux-kernel, palmer, paul.walmsley, linux-riscv, akpm

When a function doesn't have a callee, then it will not push ra
into the stack, such as lkdtm_BUG() function:

addi	sp,sp,-16
sd		s0,8(sp)
addi	s0,sp,16
ebreak

The struct stackframe use {fp,ra} to get information from
stack, if walk_stackframe() with pr_regs, we will obtain
wrong value and bad stacktrace,

[<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
---[ end trace 18da3fbdf08e25d5 ]---

Correct the next fp and pc, after that, full stacktrace
shown as expects,

[<ffffffe00066c56c>] lkdtm_BUG+0x6/0x8
[<ffffffe0008b24a4>] lkdtm_do_action+0x14/0x1c
[<ffffffe00066c372>] direct_entry+0xc0/0x10a
[<ffffffe000439f86>] full_proxy_write+0x42/0x6a
[<ffffffe000309626>] vfs_write+0x7e/0x214
[<ffffffe00030992a>] ksys_write+0x98/0xc0
[<ffffffe000309960>] sys_write+0xe/0x16
[<ffffffe0002014bc>] ret_from_syscall+0x0/0x2
---[ end trace 61917f3d9a9fadcd ]---

Signed-off-by: Chen Huang <chenhuang5@huawei.com>
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 arch/riscv/kernel/stacktrace.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c
index 595342910c3f..3842fac26e85 100644
--- a/arch/riscv/kernel/stacktrace.c
+++ b/arch/riscv/kernel/stacktrace.c
@@ -57,9 +57,14 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs,
 		/* Unwind stack frame */
 		frame = (struct stackframe *)fp - 1;
 		sp = fp;
-		fp = frame->fp;
-		pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
-					   (unsigned long *)(fp - 8));
+		if (regs && (pc == regs->epc) && (frame->fp & 0x7)) {
+			fp = frame->ra;
+			pc = regs->ra;
+		} else {
+			fp = frame->fp;
+			pc = ftrace_graph_ret_addr(current, NULL, frame->ra,
+						(unsigned long *)(fp - 8));
+		}
 	}
 }
 
-- 
2.17.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

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

end of thread, other threads:[~2020-11-28 22:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-27  8:45 [PATCH] riscv/stacktrace: fix stackframe without ra on the top Chen Huang
2020-11-27  8:45 ` Chen Huang
2020-11-27 11:43 ` Kefeng Wang
2020-11-27 11:43   ` Kefeng Wang
2020-11-28  6:22   ` [PATCH v2] riscv: stacktrace: " Chen Huang
2020-11-28  6:22     ` Chen Huang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.