From nobody Tue Apr 7 12:52:16 2026 Received: from mail-dl1-f74.google.com (mail-dl1-f74.google.com [74.125.82.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED9E4364EB2 for ; Fri, 13 Mar 2026 06:19:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382747; cv=none; b=m0CVh8X++iRiXObr55Dy1MenUssd69va7Azzzm0ynGZxZRUfusmU9UODHAvwCYHOykgo5NfRdyZoZ298grH2FkRITENRoutkIy2zgqnuWZ5Ozb75I7/V8knhFwcMWkBMapXQ9A1e/cceHi4V+G/t10IZ4Id/PUbaSrh0FpA68Ug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382747; c=relaxed/simple; bh=PuZiPcov/2hiW/5o1LOGCL0q0YLro6bnt4DoSidCcmE=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=GKv+ZSN6P4H9f9DIfrHo/OKr83exbFcWCbLoeQsl1CmqOdldIoeciI4NGJZGQ6R3MFigZYCgd14qKHJgawRWFBg1EFwU6fhEk2uFFvurhDffEWUZMePKkn0aR21vZhf4lr5Raj69OEa2TtSVKbk6/xZozGpu3wPSTRjXK/vu7Yo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iKB2KmrJ; arc=none smtp.client-ip=74.125.82.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--cmllamas.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iKB2KmrJ" Received: by mail-dl1-f74.google.com with SMTP id a92af1059eb24-12737f276a2so2647532c88.1 for ; Thu, 12 Mar 2026 23:19:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773382745; x=1773987545; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=/7Avksm2DO+mVglS65ohDrmnD6ZqO7r/NBeBe/i4gQI=; b=iKB2KmrJN6TsKGBhxMH5BZPwFb5HPQRbhxf/5JBq2ZU1C/2ZV/fhvOwNLbF/WYkrZD Fjh3QUQEXCd1gjLSks2SPf5LAv/PrXnRtbXBpjWlTegXdelu5bXkfWWSOu9Nz3VSwJzh LI+jZWTlz4V0ROdyHHwYsnP+w2+BFxWrY4ftYRkBQxNU9/FEu0LfYYVfQLmrzmORe0Im kH8aA+/ipgdtAy8qLHyJgN3TVlLHVrdyXEfbqR09FE3CDqQ1C4CbgaN3aXpCcHqX1FsU pX7OeBn/CRrM2e7bGIPRrIfBRvkDuY2yIigE3UBRVc8sSmrh7rwwuyoReJCKp8LkpXc5 L6wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773382745; x=1773987545; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=/7Avksm2DO+mVglS65ohDrmnD6ZqO7r/NBeBe/i4gQI=; b=Kcxl3/MI2gre0BE0mepVhnKjFABlVjkH2es+QzgkkntTkYVg/l1xSiIYst7OyNV9po tDvqx8gbwwAfh2uRnfr9cZPMWwUFQvgWfRQnHnzSmDy60mtTOkde8cjbajiKlyRq0VVI EfwnucwTF1yftS5B6FoYBAAD7Q+Tm8D48P+3XXbt1/ERylsYWw3VdcnAkP/sOJNdgSBk AtZTHDgHZ7G4Hww8yqUvwtGRXVGoRYrdesXxfbwZLMB3kD217nEMvp+KCvC+K2r5XS1T Ffcn3ewIaSGrKrx7Spv9KoCmuNp3rSiLzXqsF6QpusgkGVrBarU4VZnnhQshtrOAEe7y 1uVQ== X-Forwarded-Encrypted: i=1; AJvYcCUC9dpAr+H93D6siibbV6VScQeDfsQ8lCsDI+btJzo8XVM+uHq+th8Wvs/A2lgKvi5z0FmB1J2jmt6+h+0=@vger.kernel.org X-Gm-Message-State: AOJu0YzLB5M2RK2xYGyDxEBcmLEMwMT7bJBE8n+3+uhb5kJuoz7i1igS yToDI+7iXOjkvIbkPqpeq2iuTtK99DthwGc1DW7kI7OjhpGvIpRMcS3gHEV38VNAkgwsHwFT7PT akBOFV9TBjOhvVw== X-Received: from dybse11.prod.google.com ([2002:a05:7301:490b:b0:2be:12e0:e206]) (user=cmllamas job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:458b:b0:11d:f44d:401d with SMTP id a92af1059eb24-128f3db298emr812133c88.22.1773382744700; Thu, 12 Mar 2026 23:19:04 -0700 (PDT) Date: Fri, 13 Mar 2026 06:18:52 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.53.0.880.g73c4285caa-goog Message-ID: <20260313061852.4025964-1-cmllamas@google.com> Subject: [PATCH v7] arm64: implement support for static call trampolines From: Carlos Llamas To: linux-arm-kernel@lists.infradead.org Cc: Sami Tolvanen , Catalin Marinas , Will Deacon , Peter Zijlstra , Josh Poimboeuf , Ard Biesheuvel , Mark Rutland , Kees Cook , Quentin Perret , Steven Rostedt , Will McVicker , Sean Christopherson , kernel-team@android.com, linux-kernel@vger.kernel.org, Carlos Llamas Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Implement arm64 support for the 'unoptimized' static call variety, which routes all calls through a single trampoline that is patched to perform a tail call to the selected function. Since static call targets may be located in modules loaded out of direct branching range, we need to use a ADRP/ADD pair to load the branch target into R16 and use a branch-to-register (BR) instruction to perform an indirect call. Unlike on x86, there is no pressing need on arm64 to avoid indirect calls at all cost, but hiding it from the compiler as is done here does have some benefits: - the literal is located in .rodata, which gives us the same robustness advantage that code patching does; - no performance hit on CFI enabled Clang builds that decorate compiler emitted indirect calls with branch target validity checks. Cc: Peter Zijlstra (Intel) Signed-off-by: Ard Biesheuvel Signed-off-by: Carlos Llamas Acked-by: Peter Zijlstra (Intel) Reviewed-by: Sami Tolvanen --- v7: - Took Ard's v3 patch (as it leaves the code patching logic out) and rebased it on top of mainline 7.0-rc3. - Dropped the changes to arch/arm64/lib/insn.c and instead switched to the (now) existing aarch64_insn_write_literal_u64(). - Added the RET0 trampoline define which points to the generic stub __static_call_return0. - Made the HAVE_STATIC_CALL conditional on CFI as suggested by Ard. - Added .type and .size sections to the trampoline definition to support ABI tools. arch/arm64/Kconfig | 1 + arch/arm64/include/asm/static_call.h | 33 ++++++++++++++++++++++++++++ arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/static_call.c | 20 +++++++++++++++++ arch/arm64/kernel/vmlinux.lds.S | 1 + 5 files changed, 56 insertions(+) create mode 100644 arch/arm64/include/asm/static_call.h create mode 100644 arch/arm64/kernel/static_call.c diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 38dba5f7e4d2..9ea19b74b6c3 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -252,6 +252,7 @@ config ARM64 select HAVE_RSEQ select HAVE_RUST if RUSTC_SUPPORTS_ARM64 select HAVE_STACKPROTECTOR + select HAVE_STATIC_CALL if CFI select HAVE_SYSCALL_TRACEPOINTS select HAVE_KPROBES select HAVE_KRETPROBES diff --git a/arch/arm64/include/asm/static_call.h b/arch/arm64/include/asm/= static_call.h new file mode 100644 index 000000000000..331580542fd4 --- /dev/null +++ b/arch/arm64/include/asm/static_call.h @@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_STATIC_CALL_H +#define _ASM_STATIC_CALL_H + +#define __ARCH_DEFINE_STATIC_CALL_TRAMP(name, target) \ + asm(" .pushsection .static_call.text, \"ax\" \n" \ + " .align 3 \n" \ + " .globl " STATIC_CALL_TRAMP_STR(name) " \n" \ + STATIC_CALL_TRAMP_STR(name) ": \n" \ + " hint 34 /* BTI C */ \n" \ + " adrp x16, 1f \n" \ + " ldr x16, [x16, :lo12:1f] \n" \ + " cbz x16, 0f \n" \ + " br x16 \n" \ + "0: ret \n" \ + " .type " STATIC_CALL_TRAMP_STR(name) ", %function \n" \ + " .size " STATIC_CALL_TRAMP_STR(name) ", . - " STATIC_CALL_TRAMP_STR(= name) " \n" \ + " .popsection \n" \ + " .pushsection .rodata, \"a\" \n" \ + " .align 3 \n" \ + "1: .quad " target " \n" \ + " .popsection \n") + +#define ARCH_DEFINE_STATIC_CALL_TRAMP(name, func) \ + __ARCH_DEFINE_STATIC_CALL_TRAMP(name, #func) + +#define ARCH_DEFINE_STATIC_CALL_NULL_TRAMP(name) \ + __ARCH_DEFINE_STATIC_CALL_TRAMP(name, "0x0") + +#define ARCH_DEFINE_STATIC_CALL_RET0_TRAMP(name) \ + ARCH_DEFINE_STATIC_CALL_TRAMP(name, __static_call_return0) + +#endif /* _ASM_STATIC_CALL_H */ diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index 76f32e424065..fe627100d199 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -46,6 +46,7 @@ obj-$(CONFIG_MODULES) +=3D module.o module-plts.o obj-$(CONFIG_PERF_EVENTS) +=3D perf_regs.o perf_callchain.o obj-$(CONFIG_HARDLOCKUP_DETECTOR_PERF) +=3D watchdog_hld.o obj-$(CONFIG_HAVE_HW_BREAKPOINT) +=3D hw_breakpoint.o +obj-$(CONFIG_HAVE_STATIC_CALL) +=3D static_call.o obj-$(CONFIG_CPU_PM) +=3D sleep.o suspend.o obj-$(CONFIG_KGDB) +=3D kgdb.o obj-$(CONFIG_EFI) +=3D efi.o efi-rt-wrapper.o diff --git a/arch/arm64/kernel/static_call.c b/arch/arm64/kernel/static_cal= l.c new file mode 100644 index 000000000000..944ecabb821f --- /dev/null +++ b/arch/arm64/kernel/static_call.c @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +void arch_static_call_transform(void *site, void *tramp, void *func, bool = tail) +{ + u64 literal; + int ret; + + /* decode the instructions to discover the literal address */ + literal =3D ALIGN_DOWN((u64)tramp + 4, SZ_4K) + + aarch64_insn_adrp_get_offset(le32_to_cpup(tramp + 4)) + + 8 * aarch64_insn_decode_immediate(AARCH64_INSN_IMM_12, + le32_to_cpup(tramp + 8)); + + ret =3D aarch64_insn_write_literal_u64((void *)literal, (u64)func); + WARN_ON_ONCE(ret); +} +EXPORT_SYMBOL_GPL(arch_static_call_transform); diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.ld= s.S index 2964aad0362e..2d1e75263f03 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -191,6 +191,7 @@ SECTIONS LOCK_TEXT KPROBES_TEXT HYPERVISOR_TEXT + STATIC_CALL_TEXT *(.gnu.warning) } =20 --=20 2.53.0.880.g73c4285caa-goog