linux-stable/arch
Matthew Bystrin 2ae3749f37 riscv: stacktrace: fixed walk_stackframe()
[ Upstream commit a2a4d4a6a0 ]

If the load access fault occures in a leaf function (with
CONFIG_FRAME_POINTER=y), when wrong stack trace will be displayed:

[<ffffffff804853c2>] regmap_mmio_read32le+0xe/0x1c
---[ end trace 0000000000000000 ]---

Registers dump:
    ra     0xffffffff80485758 <regmap_mmio_read+36>
    sp     0xffffffc80200b9a0
    fp     0xffffffc80200b9b0
    pc     0xffffffff804853ba <regmap_mmio_read32le+6>

Stack dump:
    0xffffffc80200b9a0:  0xffffffc80200b9e0  0xffffffc80200b9e0
    0xffffffc80200b9b0:  0xffffffff8116d7e8  0x0000000000000100
    0xffffffc80200b9c0:  0xffffffd8055b9400  0xffffffd8055b9400
    0xffffffc80200b9d0:  0xffffffc80200b9f0  0xffffffff8047c526
    0xffffffc80200b9e0:  0xffffffc80200ba30  0xffffffff8047fe9a

The assembler dump of the function preambula:
    add     sp,sp,-16
    sd      s0,8(sp)
    add     s0,sp,16

In the fist stack frame, where ra is not stored on the stack we can
observe:

        0(sp)                  8(sp)
        .---------------------------------------------.
    sp->|       frame->fp      | frame->ra (saved fp) |
        |---------------------------------------------|
    fp->|         ....         |         ....         |
        |---------------------------------------------|
        |                      |                      |

and in the code check is performed:
	if (regs && (regs->epc == pc) && (frame->fp & 0x7))

I see no reason to check frame->fp value at all, because it is can be
uninitialized value on the stack. A better way is to check frame->ra to
be an address on the stack. After the stacktrace shows as expect:

[<ffffffff804853c2>] regmap_mmio_read32le+0xe/0x1c
[<ffffffff80485758>] regmap_mmio_read+0x24/0x52
[<ffffffff8047c526>] _regmap_bus_reg_read+0x1a/0x22
[<ffffffff8047fe9a>] _regmap_read+0x5c/0xea
[<ffffffff80480376>] _regmap_update_bits+0x76/0xc0
...
---[ end trace 0000000000000000 ]---
As pointed by Samuel Holland it is incorrect to remove check of the stackframe
entirely.

Changes since v2 [2]:
 - Add accidentally forgotten curly brace

Changes since v1 [1]:
 - Instead of just dropping frame->fp check, replace it with validation of
   frame->ra, which should be a stack address.
 - Move frame pointer validation into the separate function.

[1] https://lore.kernel.org/linux-riscv/20240426072701.6463-1-dev.mbstr@gmail.com/
[2] https://lore.kernel.org/linux-riscv/20240521131314.48895-1-dev.mbstr@gmail.com/

Fixes: f766f77a74 ("riscv/stacktrace: Fix stack output without ra on the stack top")
Signed-off-by: Matthew Bystrin <dev.mbstr@gmail.com>
Reviewed-by: Samuel Holland <samuel.holland@sifive.com>
Link: https://lore.kernel.org/r/20240521191727.62012-1-dev.mbstr@gmail.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-06-12 11:03:52 +02:00
..
alpha rtc: Add support for configuring the UIP timeout for RTC reads 2024-01-31 16:17:01 -08:00
arc ARC: [plat-hsdk]: Remove misplaced interrupt-cells property 2024-05-02 16:29:23 +02:00
arm ARM: configs: sunxi: Enable DRM_DW_HDMI 2024-06-12 11:03:06 +02:00
arm64 arm64: asm-bug: Add .align 2 to the end of __BUG_ENTRY 2024-06-12 11:03:52 +02:00
csky work around gcc bugs with 'asm goto' with outputs 2024-02-23 09:12:28 +01:00
hexagon hexagon: vmlinux.lds.S: handle attributes section 2024-04-03 15:19:48 +02:00
ia64
loongarch LoongArch: Fix callchain parse error with kernel tracepoint events again 2024-06-12 11:03:43 +02:00
m68k m68k: mac: Fix reboot hang on Mac IIci 2024-06-12 11:03:17 +02:00
microblaze microblaze: Remove early printk call from cpuinfo-static.c 2024-06-12 11:03:39 +02:00
mips MIPS: scall: Save thread_info.syscall unconditionally on entry 2024-05-17 11:56:07 +02:00
nios2
openrisc
parisc parisc: add missing export of __cmpxchg_u8() 2024-06-12 11:03:04 +02:00
powerpc powerpc/pseries: Add failure related checks for h_get_mpp and h_get_ppp 2024-06-12 11:03:47 +02:00
riscv riscv: stacktrace: fixed walk_stackframe() 2024-06-12 11:03:52 +02:00
s390 s390/boot: Remove alt_stfle_fac_list from decompressor 2024-06-12 11:03:44 +02:00
sh Revert "sh: Handle calling csum_partial with misaligned data" 2024-06-12 11:03:15 +02:00
sparc sparc32: Fix parport build with sparc32 2024-04-03 15:19:33 +02:00
um um: Fix the declaration of kasan_map_memory 2024-06-12 11:03:48 +02:00
x86 x86/kconfig: Select ARCH_WANT_FRAME_POINTERS again when UNWINDER_FRAME_POINTER=y 2024-06-12 11:03:50 +02:00
xtensa work around gcc bugs with 'asm goto' with outputs 2024-02-23 09:12:28 +01:00
.gitignore
Kconfig cpu: Re-enable CPU mitigations by default for !X86 architectures 2024-05-02 16:29:28 +02:00