acrn-kernel/arch/riscv/kernel
Guo Ren 676465bf75 riscv: ftrace: Reduce the detour code size to half
commit 6724a76cff upstream.

Use a temporary register to reduce the size of detour code from 16 bytes to
8 bytes. The previous implementation is from 'commit afc76b8b80 ("riscv:
Using PATCHABLE_FUNCTION_ENTRY instead of MCOUNT")'.

Before the patch:
<func_prolog>:
 0: REG_S  ra, -SZREG(sp)
 4: auipc  ra, ?
 8: jalr   ?(ra)
12: REG_L  ra, -SZREG(sp)
 (func_boddy)

After the patch:
<func_prolog>:
 0: auipc  t0, ?
 4: jalr   t0, ?(t0)
 (func_boddy)

This patch not just reduces the size of detour code, but also fixes an
important issue:

An Ftrace callback registered with FTRACE_OPS_FL_IPMODIFY flag can
actually change the instruction pointer, e.g. to "replace" the given
kernel function with a new one, which is needed for livepatching, etc.

In this case, the trampoline (ftrace_regs_caller) would not return to
<func_prolog+12> but would rather jump to the new function. So, "REG_L
ra, -SZREG(sp)" would not run and the original return address would not
be restored. The kernel is likely to hang or crash as a result.

This can be easily demonstrated if one tries to "replace", say,
cmdline_proc_show() with a new function with the same signature using
instruction_pointer_set(&fregs->regs, new_func_addr) in the Ftrace
callback.

Link: https://lore.kernel.org/linux-riscv/20221122075440.1165172-1-suagrfillet@gmail.com/
Link: https://lore.kernel.org/linux-riscv/d7d5730b-ebef-68e5-5046-e763e1ee6164@yadro.com/
Co-developed-by: Song Shuai <suagrfillet@gmail.com>
Signed-off-by: Song Shuai <suagrfillet@gmail.com>
Signed-off-by: Guo Ren <guoren@linux.alibaba.com>
Signed-off-by: Guo Ren <guoren@kernel.org>
Cc: Evgenii Shatokhin <e.shatokhin@yadro.com>
Reviewed-by: Evgenii Shatokhin <e.shatokhin@yadro.com>
Link: https://lore.kernel.org/r/20230112090603.1295340-4-guoren@kernel.org
Cc: stable@vger.kernel.org
Fixes: 10626c32e3 ("riscv/ftrace: Add basic support")
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2023-03-10 09:34:30 +01:00
..
compat_vdso
probes riscv: kprobe: Fixup misaligned load text 2023-02-14 19:11:53 +01:00
vdso
.gitignore
Makefile
alternative.c
asm-offsets.c
cacheinfo.c
compat_signal.c
compat_syscall_table.c
cpu-hotplug.c
cpu.c
cpu_ops.c
cpu_ops_sbi.c
cpu_ops_spinwait.c
cpufeature.c
crash_dump.c
crash_save_regs.S
efi-header.S
efi.c
elf_kexec.c
entry.S
fpu.S
ftrace.c riscv: ftrace: Reduce the detour code size to half 2023-03-10 09:34:30 +01:00
head.S
head.h
image-vars.h
irq.c
jump_label.c
kexec_relocate.S
kgdb.c
machine_kexec.c
machine_kexec_file.c
mcount-dyn.S riscv: ftrace: Reduce the detour code size to half 2023-03-10 09:34:30 +01:00
mcount.S
module-sections.c
module.c
patch.c
perf_callchain.c
perf_regs.c
process.c
ptrace.c
reset.c
riscv_ksyms.c
sbi.c
setup.c
signal.c
smp.c
smpboot.c riscv: Move call to init_cpu_topology() to later initialization stage 2023-02-01 08:34:48 +01:00
soc.c
stacktrace.c riscv: stacktrace: Fix missing the first frame 2023-02-14 19:11:49 +01:00
suspend.c
suspend_entry.S
sys_riscv.c
syscall_table.c
time.c RISC-V: time: initialize hrtimer based broadcast clock event device 2023-03-10 09:33:03 +01:00
trace_irq.c
trace_irq.h
traps.c riscv: Avoid enabling interrupts in die() 2023-03-10 09:34:29 +01:00
traps_misaligned.c
vdso.c
vmlinux-xip.lds.S
vmlinux.lds.S