From nobody Sun Oct 5 18:17:30 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 954EB4C92 for ; Fri, 1 Aug 2025 00:10:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754007015; cv=none; b=T9yE31/p/iEfjLi9g++JFtwVhq3m9JfGiAOKcwY9iZSCarf71QoyDtuc5C0MvFnbAF9K3sGOipxsKdG4fLq1XHHYqw/LjZkeox+tmdsDjx7U3bXA1JfO60vy8FKU81aYKJ7EuQp0oZZtrBw/KTT0mxUbkXuT0EBfpNV4IzLSPSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754007015; c=relaxed/simple; bh=bUNQlcSLssMdbHKmQmMRnOmVGMiEFxEip285SleFhmg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZgZKN19S3yKGywtyA++I9c7KtRdPwidcKHxR8zyKR6/RHJW2+q2RL5OdbQc3lwQIY8HJbPzeXoFkUXs2NuLbus2p/4sHS0Ra49b4HSiCfRr5/228qafMLoHTsrlIMRXDc4KmryUUGt0L+Vm5xWghbZCQ6L7IRphPKGwjgWJvmwc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fQ2uKQuV; arc=none smtp.client-ip=209.85.214.202 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--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fQ2uKQuV" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-23fed1492f6so14978915ad.2 for ; Thu, 31 Jul 2025 17:10:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1754007013; x=1754611813; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=b5iHHkCZp0P25Nw1yiUQv4bGhfUyY+YLuZAbEHBpOG4=; b=fQ2uKQuVZMw+NWea/QXeLMpm8F8OGXfVkhZiX++YqhzCgkIzXtIoloRBAkfsskz7UC kav50x8HMsJWL2w+oyA1qtSOAoXBJFi2nFq3y7UAOvdwjzzNRcpRaccGD2HvK8nrocmB TkXJ6WKD7uMl6rCd5QAFV/HD1InywlQz7tTds7JXpQFLit7Y7mIWkMUt30JfxwD8heeb a/YdVmbe+PFsnwiM/ErmdkPq0QT+g88hdU4PbeiQy+l3mg6XvbdDK5JGLOWzsO1/kcm8 49M4Y1vjUID+i+QicnK8CiXWbFyh8oqyzoFhHm/gbjqA0NfhiZIYPHfLQf5eDtitjfiX brjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754007013; x=1754611813; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=b5iHHkCZp0P25Nw1yiUQv4bGhfUyY+YLuZAbEHBpOG4=; b=EUlvcOtk1w0dXYk/VhPirbdmoYNJkqcA5r1FrLzNzkb6mc+geMd3lv0Aj9DF01PBXg jIajzUTisYgk9rKqk5rzc+5mqvSOXP9NJL6GLVHPRLEz6ppxWIfRyS/JyBR5objy6vij 63o7b8DcOeZRj8yGqJgcaPpLPshiIeKHdwPZIeuW7URW0ash43TQUl/VgTCymDuY6eY9 zpuj29gWEInOBoS/ilB4csm4LM39j60qVTbFI+2ZMhUfX1RXJrPhwmJDWuBHDcpkk6Ac cNY78EW1sJO7XU31w7cmTiQFm+iwwJjTqk7C2iWz7nKPBaC71W6c/iX1+calfdi+/snu LoEw== X-Forwarded-Encrypted: i=1; AJvYcCUIs0loRj7lugPwtHLzTuk2DLkpPx3ancIVNfLxWH/Xvj+jLSGdUMClPp1nzedEgOS8IvqBFQLbCojYbHM=@vger.kernel.org X-Gm-Message-State: AOJu0Yz4WDXE6WyK6u1SFcHey7YuB7fDVBq2aJ8Rq25KoKubCgaah6R8 CYNFCX0xTtzkeDZC4qM6+ojFTG3Th2KEe8gXQnC8ZRXBBQPvEZjXMcI/cRxnlIwtK9psYYnQeSm 7TbMxlrLyXnCYqbehfy6R0/V7Ae7WPA== X-Google-Smtp-Source: AGHT+IGEL+K834biNFo4yMv4E4uY+VY5Shz8+jtaGsyV2sasJARA8Z/IfB2j8/y81KRGALd/nCnzm3VLQkZzPcjhhZ8= X-Received: from plzt4.prod.google.com ([2002:a17:902:bc44:b0:240:3ee:1601]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e5cb:b0:234:ed31:fca7 with SMTP id d9443c01a7336-2422a6d803cmr8152855ad.48.1754007012792; Thu, 31 Jul 2025 17:10:12 -0700 (PDT) Date: Fri, 1 Aug 2025 00:10:07 +0000 In-Reply-To: <20250801001004.1859976-5-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250801001004.1859976-5-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=7400; i=samitolvanen@google.com; h=from:subject; bh=bUNQlcSLssMdbHKmQmMRnOmVGMiEFxEip285SleFhmg=; b=owGbwMvMwCUWxa662nLh8irG02pJDBk9rHdvPHrbJXfy09wPx6rYVZR2Meie+u169aKp2ZdTr 14krW6I7ShlYRDjYpAVU2Rp+bp66+7vTqmvPhdJwMxhZQIZwsDFKQATyS1jZPijv/PhNGFp6SnW H/kKUypYntbe12B1OcXifcXZ6ucjpVZGhsMRN16Y6Qf5XLz7Z7qH6UTjn0ktF/bvO5Vb1jZRKGj dRF4A X-Mailer: git-send-email 2.50.1.565.gc32cd1483b-goog Message-ID: <20250801001004.1859976-7-samitolvanen@google.com> Subject: [PATCH bpf-next v14 2/3] cfi: Move BPF CFI types and helpers to generic code From: Sami Tolvanen To: bpf@vger.kernel.org, Puranjay Mohan , Alexei Starovoitov , Daniel Borkmann Cc: Catalin Marinas , Will Deacon , Andrii Nakryiko , Mark Rutland , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Maxwell Bland , Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Instead of duplicating the same code for each architecture, move the CFI type hash variables for BPF function types and related helper functions to generic CFI code, and allow architectures to override the function definitions if needed. Signed-off-by: Sami Tolvanen --- arch/riscv/include/asm/cfi.h | 16 ------------ arch/riscv/kernel/cfi.c | 24 ------------------ arch/x86/include/asm/cfi.h | 10 ++------ arch/x86/kernel/alternative.c | 12 --------- include/linux/cfi.h | 47 +++++++++++++++++++++++++++++------ kernel/cfi.c | 15 +++++++++++ 6 files changed, 56 insertions(+), 68 deletions(-) diff --git a/arch/riscv/include/asm/cfi.h b/arch/riscv/include/asm/cfi.h index fb9696d7a3f2..4508aaa7a2fd 100644 --- a/arch/riscv/include/asm/cfi.h +++ b/arch/riscv/include/asm/cfi.h @@ -14,27 +14,11 @@ struct pt_regs; #ifdef CONFIG_CFI_CLANG enum bug_trap_type handle_cfi_failure(struct pt_regs *regs); #define __bpfcall -static inline int cfi_get_offset(void) -{ - return 4; -} - -#define cfi_get_offset cfi_get_offset -extern u32 cfi_bpf_hash; -extern u32 cfi_bpf_subprog_hash; -extern u32 cfi_get_func_hash(void *func); #else static inline enum bug_trap_type handle_cfi_failure(struct pt_regs *regs) { return BUG_TRAP_TYPE_NONE; } - -#define cfi_bpf_hash 0U -#define cfi_bpf_subprog_hash 0U -static inline u32 cfi_get_func_hash(void *func) -{ - return 0; -} #endif /* CONFIG_CFI_CLANG */ =20 #endif /* _ASM_RISCV_CFI_H */ diff --git a/arch/riscv/kernel/cfi.c b/arch/riscv/kernel/cfi.c index e7aec5f36dd5..6ec9dbd7292e 100644 --- a/arch/riscv/kernel/cfi.c +++ b/arch/riscv/kernel/cfi.c @@ -4,7 +4,6 @@ * * Copyright (C) 2023 Google LLC */ -#include #include #include =20 @@ -76,26 +75,3 @@ enum bug_trap_type handle_cfi_failure(struct pt_regs *re= gs) =20 return report_cfi_failure(regs, regs->epc, &target, type); } - -#ifdef CONFIG_CFI_CLANG -struct bpf_insn; - -/* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */ -extern unsigned int __bpf_prog_runX(const void *ctx, - const struct bpf_insn *insn); -DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX); - -/* Must match bpf_callback_t */ -extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64); -DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn); - -u32 cfi_get_func_hash(void *func) -{ - u32 hash; - - if (get_kernel_nofault(hash, func - cfi_get_offset())) - return 0; - - return hash; -} -#endif diff --git a/arch/x86/include/asm/cfi.h b/arch/x86/include/asm/cfi.h index 3e51ba459154..1751f1eb95ef 100644 --- a/arch/x86/include/asm/cfi.h +++ b/arch/x86/include/asm/cfi.h @@ -116,8 +116,6 @@ struct pt_regs; #ifdef CONFIG_CFI_CLANG enum bug_trap_type handle_cfi_failure(struct pt_regs *regs); #define __bpfcall -extern u32 cfi_bpf_hash; -extern u32 cfi_bpf_subprog_hash; =20 static inline int cfi_get_offset(void) { @@ -135,6 +133,8 @@ static inline int cfi_get_offset(void) #define cfi_get_offset cfi_get_offset =20 extern u32 cfi_get_func_hash(void *func); +#define cfi_get_func_hash cfi_get_func_hash + extern int cfi_get_func_arity(void *func); =20 #ifdef CONFIG_FINEIBT @@ -153,12 +153,6 @@ static inline enum bug_trap_type handle_cfi_failure(st= ruct pt_regs *regs) { return BUG_TRAP_TYPE_NONE; } -#define cfi_bpf_hash 0U -#define cfi_bpf_subprog_hash 0U -static inline u32 cfi_get_func_hash(void *func) -{ - return 0; -} static inline int cfi_get_func_arity(void *func) { return 0; diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index a555665b4d9c..9f6b7dab2d9a 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -2,7 +2,6 @@ #define pr_fmt(fmt) "SMP alternatives: " fmt =20 #include -#include #include #include #include @@ -1185,17 +1184,6 @@ bool cfi_bhi __ro_after_init =3D false; #endif =20 #ifdef CONFIG_CFI_CLANG -struct bpf_insn; - -/* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */ -extern unsigned int __bpf_prog_runX(const void *ctx, - const struct bpf_insn *insn); -DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX); - -/* Must match bpf_callback_t */ -extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64); -DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn); - u32 cfi_get_func_hash(void *func) { u32 hash; diff --git a/include/linux/cfi.h b/include/linux/cfi.h index 1db17ecbb86c..52a98886a455 100644 --- a/include/linux/cfi.h +++ b/include/linux/cfi.h @@ -11,16 +11,9 @@ #include #include =20 +#ifdef CONFIG_CFI_CLANG extern bool cfi_warn; =20 -#ifndef cfi_get_offset -static inline int cfi_get_offset(void) -{ - return 0; -} -#endif - -#ifdef CONFIG_CFI_CLANG enum bug_trap_type report_cfi_failure(struct pt_regs *regs, unsigned long = addr, unsigned long *target, u32 type); =20 @@ -29,6 +22,44 @@ static inline enum bug_trap_type report_cfi_failure_noad= dr(struct pt_regs *regs, { return report_cfi_failure(regs, addr, NULL, 0); } + +#ifndef cfi_get_offset +/* + * Returns the CFI prefix offset. By default, the compiler emits only + * a 4-byte CFI type hash before the function. If an architecture + * uses -fpatchable-function-entry=3DN,M where M>0 to change the prefix + * offset, they must override this function. + */ +static inline int cfi_get_offset(void) +{ + return 4; +} +#endif + +#ifndef cfi_get_func_hash +static inline u32 cfi_get_func_hash(void *func) +{ + u32 hash; + + if (get_kernel_nofault(hash, func - cfi_get_offset())) + return 0; + + return hash; +} +#endif + +/* CFI type hashes for BPF function types */ +extern u32 cfi_bpf_hash; +extern u32 cfi_bpf_subprog_hash; + +#else /* CONFIG_CFI_CLANG */ + +static inline int cfi_get_offset(void) { return 0; } +static inline u32 cfi_get_func_hash(void *func) { return 0; } + +#define cfi_bpf_hash 0U +#define cfi_bpf_subprog_hash 0U + #endif /* CONFIG_CFI_CLANG */ =20 #ifdef CONFIG_ARCH_USES_CFI_TRAPS diff --git a/kernel/cfi.c b/kernel/cfi.c index 422fa4f958ae..4dad04ead06c 100644 --- a/kernel/cfi.c +++ b/kernel/cfi.c @@ -5,6 +5,8 @@ * Copyright (C) 2022 Google LLC */ =20 +#include +#include #include =20 bool cfi_warn __ro_after_init =3D IS_ENABLED(CONFIG_CFI_PERMISSIVE); @@ -27,6 +29,19 @@ enum bug_trap_type report_cfi_failure(struct pt_regs *re= gs, unsigned long addr, return BUG_TRAP_TYPE_BUG; } =20 +/* + * Declare two non-existent functions with types that match bpf_func_t and + * bpf_callback_t pointers, and use DEFINE_CFI_TYPE to define type hash + * variables for each function type. The cfi_bpf_* variables are used by + * arch-specific BPF JIT implementations to ensure indirectly callable JIT + * code has matching CFI type hashes. + */ +extern typeof(*(bpf_func_t)0) __bpf_prog_runX; +DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX); + +extern typeof(*(bpf_callback_t)0) __bpf_callback_fn; +DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn); + #ifdef CONFIG_ARCH_USES_CFI_TRAPS static inline unsigned long trap_address(s32 *p) { --=20 2.50.1.565.gc32cd1483b-goog