OrangePi_CM5/arch/arm64/kernel
Mark Rutland bc28fde909 arm64: ftrace: consistently handle PLTs.
[ Upstream commit a6253579977e4c6f7818eeb05bf2bc65678a7187 ]

Sometimes it is necessary to use a PLT entry to call an ftrace
trampoline. This is handled by ftrace_make_call() and ftrace_make_nop(),
with each having *almost* identical logic, but this is not handled by
ftrace_modify_call() since its introduction in commit:

  3b23e4991f ("arm64: implement ftrace with regs")

Due to this, if we ever were to call ftrace_modify_call() for a callsite
which requires a PLT entry for a trampoline, then either:

a) If the old addr requires a trampoline, ftrace_modify_call() will use
   an out-of-range address to generate the 'old' branch instruction.
   This will result in warnings from aarch64_insn_gen_branch_imm() and
   ftrace_modify_code(), and no instructions will be modified. As
   ftrace_modify_call() will return an error, this will result in
   subsequent internal ftrace errors.

b) If the old addr does not require a trampoline, but the new addr does,
   ftrace_modify_call() will use an out-of-range address to generate the
   'new' branch instruction. This will result in warnings from
   aarch64_insn_gen_branch_imm(), and ftrace_modify_code() will replace
   the 'old' branch with a BRK. This will result in a kernel panic when
   this BRK is later executed.

Practically speaking, case (a) is vastly more likely than case (b), and
typically this will result in internal ftrace errors that don't
necessarily affect the rest of the system. This can be demonstrated with
an out-of-tree test module which triggers ftrace_modify_call(), e.g.

| # insmod test_ftrace.ko
| test_ftrace: Function test_function raw=0xffffb3749399201c, callsite=0xffffb37493992024
| branch_imm_common: offset out of range
| branch_imm_common: offset out of range
| ------------[ ftrace bug ]------------
| ftrace failed to modify
| [<ffffb37493992024>] test_function+0x8/0x38 [test_ftrace]
|  actual:   1d:00:00:94
| Updating ftrace call site to call a different ftrace function
| ftrace record flags: e0000002
|  (2) R
|  expected tramp: ffffb374ae42ed54
| ------------[ cut here ]------------
| WARNING: CPU: 0 PID: 165 at kernel/trace/ftrace.c:2085 ftrace_bug+0x280/0x2b0
| Modules linked in: test_ftrace(+)
| CPU: 0 PID: 165 Comm: insmod Not tainted 5.19.0-rc2-00002-g4d9ead8b45ce #13
| Hardware name: linux,dummy-virt (DT)
| pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
| pc : ftrace_bug+0x280/0x2b0
| lr : ftrace_bug+0x280/0x2b0
| sp : ffff80000839ba00
| x29: ffff80000839ba00 x28: 0000000000000000 x27: ffff80000839bcf0
| x26: ffffb37493994180 x25: ffffb374b0991c28 x24: ffffb374b0d70000
| x23: 00000000ffffffea x22: ffffb374afcc33b0 x21: ffffb374b08f9cc8
| x20: ffff572b8462c000 x19: ffffb374b08f9000 x18: ffffffffffffffff
| x17: 6c6c6163202c6331 x16: ffffb374ae5ad110 x15: ffffb374b0d51ee4
| x14: 0000000000000000 x13: 3435646532346561 x12: 3437336266666666
| x11: 203a706d61727420 x10: 6465746365707865 x9 : ffffb374ae5149e8
| x8 : 336266666666203a x7 : 706d617274206465 x6 : 00000000fffff167
| x5 : ffff572bffbc4a08 x4 : 00000000fffff167 x3 : 0000000000000000
| x2 : 0000000000000000 x1 : ffff572b84461e00 x0 : 0000000000000022
| Call trace:
|  ftrace_bug+0x280/0x2b0
|  ftrace_replace_code+0x98/0xa0
|  ftrace_modify_all_code+0xe0/0x144
|  arch_ftrace_update_code+0x14/0x20
|  ftrace_startup+0xf8/0x1b0
|  register_ftrace_function+0x38/0x90
|  test_ftrace_init+0xd0/0x1000 [test_ftrace]
|  do_one_initcall+0x50/0x2b0
|  do_init_module+0x50/0x1f0
|  load_module+0x17c8/0x1d64
|  __do_sys_finit_module+0xa8/0x100
|  __arm64_sys_finit_module+0x2c/0x3c
|  invoke_syscall+0x50/0x120
|  el0_svc_common.constprop.0+0xdc/0x100
|  do_el0_svc+0x3c/0xd0
|  el0_svc+0x34/0xb0
|  el0t_64_sync_handler+0xbc/0x140
|  el0t_64_sync+0x18c/0x190
| ---[ end trace 0000000000000000 ]---

We can solve this by consistently determining whether to use a PLT entry
for an address.

Note that since (the earlier) commit:

  f1a54ae9af ("arm64: module/ftrace: intialize PLT at load time")

... we can consistently determine the PLT address that a given callsite
will use, and therefore ftrace_make_nop() does not need to skip
validation when a PLT is in use.

This patch factors the existing logic out of ftrace_make_call() and
ftrace_make_nop() into a common ftrace_find_callable_addr() helper
function, which is used by ftrace_make_call(), ftrace_make_nop(), and
ftrace_modify_call(). In ftrace_make_nop() the patching is consistently
validated by ftrace_modify_code() as we can always determine what the
old instruction should have been.

Fixes: 3b23e4991f ("arm64: implement ftrace with regs")
Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Will Deacon <will@kernel.org>
Tested-by: "Ivan T. Ivanov" <iivanov@suse.de>
Reviewed-by: Chengming Zhou <zhouchengming@bytedance.com>
Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20220614080944.1349146-3-mark.rutland@arm.com
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2022-06-22 14:13:17 +02:00
..
probes arm64: kprobes: Restore local irqflag if kprobes is cancelled 2021-04-28 13:40:02 +02:00
vdso arm64/vdso: Discard .note.gnu.property sections in vDSO 2021-05-11 14:47:33 +02:00
vdso32 arm64: vdso32: require CROSS_COMPILE_COMPAT for gcc+bfd 2021-12-29 12:25:53 +01:00
.gitignore
…
Makefile
…
acpi.c
…
acpi_numa.c
…
acpi_parking_protocol.c
…
alternative.c arm64: alternatives: mark patch_alternative() as `noinstr` 2022-04-20 09:23:22 +02:00
armv8_deprecated.c
…
asm-offsets.c
…
cacheinfo.c drivers: base: cacheinfo: Get rid of DEFINE_SMP_CALL_CACHE_FUNCTION() 2021-09-26 14:08:59 +02:00
cpu-reset.S
…
cpu-reset.h
…
cpu_errata.c arm64: Enable repeat tlbi workaround on KRYO4XX gold CPUs 2022-05-25 09:18:01 +02:00
cpu_ops.c
…
cpufeature.c arm64: Use the clearbhb instruction in mitigations 2022-03-11 12:11:53 +01:00
cpuidle.c cpuidle: PSCI: Move the `has_lpi` check to the beginning of the function 2022-04-20 09:23:09 +02:00
cpuinfo.c arm64: cpufeature: add HWCAP for FEAT_RPRES 2022-03-11 12:11:51 +01:00
crash_core.c
…
crash_dump.c
…
debug-monitors.c
…
efi-entry.S
…
efi-header.S
…
efi-rt-wrapper.S
…
efi.c
…
entry-common.c arm64: entry: always set GIC_PRIO_PSR_I_SET during entry 2021-05-19 10:13:07 +02:00
entry-fpsimd.S
…
entry-ftrace.S arm64: ftrace: add missing BTIs 2021-12-08 09:03:24 +01:00
entry.S arm64: Use the clearbhb instruction in mitigations 2022-03-11 12:11:53 +01:00
fpsimd.c arm64/sve: Use correct size when reinitialising SVE state 2021-09-22 12:27:54 +02:00
ftrace.c arm64: ftrace: consistently handle PLTs. 2022-06-22 14:13:17 +02:00
head.S arm64: head: avoid over-mapping in map_memory 2021-09-18 13:40:08 +02:00
hibernate-asm.S
…
hibernate.c
…
hw_breakpoint.c
…
hyp-stub.S
…
image-vars.h
…
image.h
…
insn.c arm64: patch_text: Fixup last cpu should be master 2022-04-13 21:01:08 +02:00
io.c
…
irq.c
…
jump_label.c
…
kaslr.c
…
kexec_image.c
…
kgdb.c
…
kuser32.S
…
machine_kexec.c
…
machine_kexec_file.c
…
module-plts.c
…
module.c
…
mte.c arm64: mte: Ensure the cleared tags are visible before setting the PTE 2022-05-25 09:17:56 +02:00
paravirt.c arm64: paravirt: Use RCU read locks to guard stolen_time 2022-05-25 09:17:56 +02:00
pci.c
…
perf_callchain.c perf: Protect perf_guest_cbs with RCU 2022-01-20 09:17:50 +01:00
perf_event.c arm64: perf: Convert snprintf to sysfs_emit 2021-07-14 16:55:52 +02:00
perf_regs.c
…
pointer_auth.c
…
process.c arm64: errata: Fix exec handling in erratum 1418040 workaround 2022-02-01 17:25:40 +01:00
proton-pack.c arm64: Add part number for Arm Cortex-A78AE 2022-04-13 21:01:07 +02:00
psci.c
…
ptrace.c arm64: fix compat syscall return truncation 2021-08-12 13:22:20 +02:00
reloc_test_core.c
…
reloc_test_syms.S
…
relocate_kernel.S
…
return_address.c
…
scs.c
…
sdei.c
…
setup.c arm64/mm: Fix ttbr0 values stored in struct thread_info for software-pan 2021-07-14 16:56:07 +02:00
signal.c arm64: signal: nofpsimd: Do not allocate fp/simd context when not available 2022-04-08 14:39:54 +02:00
signal32.c
…
sigreturn32.S
…
sleep.S
…
smccc-call.S
…
smp.c sched/core: Initialize the idle task with preemption disabled 2021-07-14 16:55:50 +02:00
smp_spin_table.c
…
stacktrace.c arm64: stacktrace: avoid tracing arch_stack_walk() 2021-08-12 13:22:12 +02:00
suspend.c
…
sys.c
…
sys32.c
…
sys_compat.c arm64: compat: Do not treat syscall number as ESR_ELx for a bad syscall 2022-06-09 10:20:53 +02:00
syscall.c arm64: fix compat syscall return truncation 2021-08-12 13:22:20 +02:00
time.c
…
topology.c
…
trace-events-emulation.h
…
traps.c
…
vdso.c
…
vmlinux.lds.S arm64: entry: Allow the trampoline text to occupy multiple pages 2022-03-11 12:11:52 +01:00