From nobody Sun Oct 5 16:18:47 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 E45D023A6 for ; Fri, 1 Aug 2025 00:10:11 +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=1754007013; cv=none; b=pVBrOx+TrqB+m5x7HVwNSsGREQbYFVFC1MYai2Iu96KmVc8OxB3cYhS+2XHexDJNG0A5wwWxurGnB6tztqRcr4hI4xHIt7O4XCCA3XIDt8AC0OR0rcN1qLhaBYKkIsBCrLGfFWnN9OnJcq0HAZV3jB49W+n1X9c4dDksLqhGsyE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754007013; c=relaxed/simple; bh=LyCroxR8+8rO3Ep6ai+cbMkc1BZjRPZFl+oYPBxEvX4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JQR1/yDZ/y0WPOZYvlq45gXqCWASSGE2KVbeKotp3zkR5z2uwTjYd+RLmFkbC/mZJCvc9ChO72E/ud00A21xg12g9gkqVYdkPGjRUBArPH0Y+fT5sNHLg75S+iMZfuHTaW5jdFyiNXVZYlmo5brvGUaLcklEWXHYzfAA76iaVPY= 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=XkIwDE25; 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="XkIwDE25" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2403e4c82ddso7594495ad.1 for ; Thu, 31 Jul 2025 17:10:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1754007011; x=1754611811; 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=poiKJPDAvEHBQ9ZH/JWYvRgnnVIaGPMPEIr2//brmzk=; b=XkIwDE25e+ddVbIp4HF3GnT5i/CkhtUIDWUKFouDlnWLR6q/iyoHnORgypdQmj7piU aRKAmlN4bqf2k7noOg+PGqwiBOQM41/REF0sEQs/CrPaSA2G/YHVOOsmVcmauHnBlb+a HHgTEowHMiI5YLTtyHR24gSMwuEQTjaXvHpt13zcOTWncpmRQ9lnzMYEnqHD4Wy0BP6F lnRe+UpNiN5wa6T8yCgPHyertU7MUw/Td/TvXyF3/ADVWv63PhAraeubFjaoaXzy6adc K8pqhMtVOXylgbnfCSwn05HZ0inwWfpicLmZ4CfI/LzT/g5fV0MzULu8m7HACYzQSVN/ gYOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754007011; x=1754611811; 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=poiKJPDAvEHBQ9ZH/JWYvRgnnVIaGPMPEIr2//brmzk=; b=t0AhMnA8an4yaXrbT0fkJ1YJleozP/NnstOW5TMw5hV1fyvvz6F4Knj0NitVbvFcaP Yux8ZWyugKzNtQrS6d0NJg7S3UwxpC16OHBViV3dwOOq7ojYAX2pSLOQ+C8PYPx0HA/A PmKgmJ2ihkDJdQhPudn22YuzzwE5n6m/kG6b9mljncBcya5JJv3a3xCKdwOz4o5gNvjF WGqUELznuZt5sx5NGMzpW1Cz4rVt7fZxn9bfxbRRbAVHyCM4PjmkLiC2FBEmCjfaXc5q 2UmFfpqP3IgDm1TSGzhOL9YMfLyWKwU9sQ+hxkKI6Ruv5ffyiYrPW1SVCdox2NrMYsHX 28AA== X-Forwarded-Encrypted: i=1; AJvYcCUlnaRWOuEoC6kOAMXKOujr1rX7m7SFIGCbyCBT0fL+fBDk2RPKg9UwNMAZJKe9yoBUwMPGb6+ejrGeJGo=@vger.kernel.org X-Gm-Message-State: AOJu0YwUDVxfJNtLAeJIIzVODI5zpWBoFkiKtgMLGhOHHoZpDmJr/Nxe T6pK4E4+lHr7F7RODXLuDrB3AauEW7eUAzJyrNjn2MtOr6le8D1L22CXKHk2KzU5kuoCRNglOeW HBhhivLLccCEmwKDBvUyZGN5urwfSOw== X-Google-Smtp-Source: AGHT+IFKyym2jSAq6psdrRc/ipyIKLbA+9hAXRHr66syLBauUDoypbT6qP5GB3MHoJA1koWrGeXBIVJBOnLhYXTXqKI= X-Received: from plfn6.prod.google.com ([2002:a17:902:e546:b0:23f:8c3f:e0fc]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c402:b0:240:9dd8:2194 with SMTP id d9443c01a7336-2422a54c85amr9358745ad.22.1754007011127; Thu, 31 Jul 2025 17:10:11 -0700 (PDT) Date: Fri, 1 Aug 2025 00:10:06 +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=5301; i=samitolvanen@google.com; h=from:subject; bh=AqApxnnbLj039bMEAWEC+U8byw63lx5ks8eO2c5sGYg=; b=owGbwMvMwCUWxa662nLh8irG02pJDBk9rHe0q00PfWo2NbFYHRIjeEoqP1tIwui7lXFe845dk S0bl8h0lLIwiHExyIopsrR8Xb1193en1FefiyRg5rAygQxh4OIUgIkoMTIyfF/udTVT0czDROwk b2Gok0ZqoNyrFfFHOM32LFy5MF2MneG/Q+iJnTmpHudmrmy303ZbHrSw3r1z2+FHNY4p9QHuMyL YAA== X-Mailer: git-send-email 2.50.1.565.gc32cd1483b-goog Message-ID: <20250801001004.1859976-6-samitolvanen@google.com> Subject: [PATCH bpf-next v14 1/3] cfi: add C CFI type macro 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 , Dao Huang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Mark Rutland Currently x86 and riscv open-code 4 instances of the same logic to define a u32 variable with the KCFI typeid of a given function. Replace the duplicate logic with a common macro. Signed-off-by: Mark Rutland Co-developed-by: Maxwell Bland Signed-off-by: Maxwell Bland Co-developed-by: Sami Tolvanen Signed-off-by: Sami Tolvanen Tested-by: Dao Huang Acked-by: Will Deacon --- arch/riscv/kernel/cfi.c | 35 +++-------------------------------- arch/x86/kernel/alternative.c | 31 +++---------------------------- include/linux/cfi_types.h | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 60 deletions(-) diff --git a/arch/riscv/kernel/cfi.c b/arch/riscv/kernel/cfi.c index 64bdd3e1ab8c..e7aec5f36dd5 100644 --- a/arch/riscv/kernel/cfi.c +++ b/arch/riscv/kernel/cfi.c @@ -4,6 +4,7 @@ * * Copyright (C) 2023 Google LLC */ +#include #include #include =20 @@ -82,41 +83,11 @@ 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); - -/* - * Force a reference to the external symbol so the compiler generates - * __kcfi_typid. - */ -__ADDRESSABLE(__bpf_prog_runX); - -/* u32 __ro_after_init cfi_bpf_hash =3D __kcfi_typeid___bpf_prog_runX; */ -asm ( -" .pushsection .data..ro_after_init,\"aw\",@progbits \n" -" .type cfi_bpf_hash,@object \n" -" .globl cfi_bpf_hash \n" -" .p2align 2, 0x0 \n" -"cfi_bpf_hash: \n" -" .word __kcfi_typeid___bpf_prog_runX \n" -" .size cfi_bpf_hash, 4 \n" -" .popsection \n" -); +DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX); =20 /* Must match bpf_callback_t */ extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64); - -__ADDRESSABLE(__bpf_callback_fn); - -/* u32 __ro_after_init cfi_bpf_subprog_hash =3D __kcfi_typeid___bpf_callba= ck_fn; */ -asm ( -" .pushsection .data..ro_after_init,\"aw\",@progbits \n" -" .type cfi_bpf_subprog_hash,@object \n" -" .globl cfi_bpf_subprog_hash \n" -" .p2align 2, 0x0 \n" -"cfi_bpf_subprog_hash: \n" -" .word __kcfi_typeid___bpf_callback_fn \n" -" .size cfi_bpf_subprog_hash, 4 \n" -" .popsection \n" -); +DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn); =20 u32 cfi_get_func_hash(void *func) { diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index ea1d984166cd..a555665b4d9c 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -2,6 +2,7 @@ #define pr_fmt(fmt) "SMP alternatives: " fmt =20 #include +#include #include #include #include @@ -1189,37 +1190,11 @@ 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); - -KCFI_REFERENCE(__bpf_prog_runX); - -/* u32 __ro_after_init cfi_bpf_hash =3D __kcfi_typeid___bpf_prog_runX; */ -asm ( -" .pushsection .data..ro_after_init,\"aw\",@progbits \n" -" .type cfi_bpf_hash,@object \n" -" .globl cfi_bpf_hash \n" -" .p2align 2, 0x0 \n" -"cfi_bpf_hash: \n" -" .long __kcfi_typeid___bpf_prog_runX \n" -" .size cfi_bpf_hash, 4 \n" -" .popsection \n" -); +DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX); =20 /* Must match bpf_callback_t */ extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64); - -KCFI_REFERENCE(__bpf_callback_fn); - -/* u32 __ro_after_init cfi_bpf_subprog_hash =3D __kcfi_typeid___bpf_callba= ck_fn; */ -asm ( -" .pushsection .data..ro_after_init,\"aw\",@progbits \n" -" .type cfi_bpf_subprog_hash,@object \n" -" .globl cfi_bpf_subprog_hash \n" -" .p2align 2, 0x0 \n" -"cfi_bpf_subprog_hash: \n" -" .long __kcfi_typeid___bpf_callback_fn \n" -" .size cfi_bpf_subprog_hash, 4 \n" -" .popsection \n" -); +DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn); =20 u32 cfi_get_func_hash(void *func) { diff --git a/include/linux/cfi_types.h b/include/linux/cfi_types.h index 6b8713675765..685f7181780f 100644 --- a/include/linux/cfi_types.h +++ b/include/linux/cfi_types.h @@ -41,5 +41,28 @@ SYM_TYPED_START(name, SYM_L_GLOBAL, SYM_A_ALIGN) #endif =20 +#else /* __ASSEMBLY__ */ + +#ifdef CONFIG_CFI_CLANG +#define DEFINE_CFI_TYPE(name, func) \ + /* \ + * Force a reference to the function so the compiler generates \ + * __kcfi_typeid_. \ + */ \ + __ADDRESSABLE(func); \ + /* u32 name __ro_after_init =3D __kcfi_typeid_ */ \ + extern u32 name; \ + asm ( \ + " .pushsection .data..ro_after_init,\"aw\",\%progbits \n" \ + " .type " #name ",\%object \n" \ + " .globl " #name " \n" \ + " .p2align 2, 0x0 \n" \ + #name ": \n" \ + " .4byte __kcfi_typeid_" #func " \n" \ + " .size " #name ", 4 \n" \ + " .popsection \n" \ + ); +#endif + #endif /* __ASSEMBLY__ */ #endif /* _LINUX_CFI_TYPES_H */ --=20 2.50.1.565.gc32cd1483b-goog From nobody Sun Oct 5 16:18:47 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 From nobody Sun Oct 5 16:18:47 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 38A0123AD for ; Fri, 1 Aug 2025 00:10:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754007016; cv=none; b=aE0Rep3qVX9b1XaTk9qWJwrAC/jpUT2Hc7G1wMde+XtTd3RjnmpRXgXvvSNkcbBKM91bxDU0PUUfHnE80eB7Xz7Z5MXFJ5++CWsBTZduHQv//BvDRky3tqIOtTc/MxYgblI9utXDvfCb50WJ+1Ngcl6VVnzcNyameLGStbSiX+4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754007016; c=relaxed/simple; bh=PuRe2nGj0EpdYZQrojmZRs4VS2y7HRUovorqQS5Ex3w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DW/99zHxcSxxAEZX934z9kHeoWsU0vuQV5G+GjPWlTgugHYHS3IiW2XzFohik8HiBKh33yY8CwJAGYRFGSzix6GZqs9YgQkPFLd89aTNS04t4bBhGTxOAoKJLQt5oYvrwtXHBE47MI+xqdXySgWxFMG+BefSJM58oufsnZBQNLU= 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=sMEVM6f7; arc=none smtp.client-ip=209.85.216.73 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="sMEVM6f7" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-31366819969so1775964a91.0 for ; Thu, 31 Jul 2025 17:10:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1754007014; x=1754611814; 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=f5uDl85wDGhTjSIRJ3MDpBVcbv+/o9QKj57RG6DGCPk=; b=sMEVM6f7jcWDlIiMk1LjPmnnlnQpwhkmdbsrc9F61847XP669mRqvQs8RZv0EMOA+a SyBl24gOTZwScae4FbmtA3b4MnZMrHJIU8juX/Q6BgGiTxhPemsVdKuPo4DQ8xY9HB8I shzGsssUflLOazdINXuVYnK1z5CMN2bB6JiYiutDFWGGBPMIHeygMHkiUkDIPkLc6c1w +t2lIhgmg7qvMBgvQb7Ly28RPC0F2yLS62n/zpDdrhDK6farJvTB2dyo9wCbeti7dhJd JMAYD8jkD0tGoGD3C/bytP7S5vw/wATxU0SA1Xtz6Om7Oy4oIE1XFgaSlgwKKS7D07M5 yTDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754007014; x=1754611814; 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=f5uDl85wDGhTjSIRJ3MDpBVcbv+/o9QKj57RG6DGCPk=; b=Vhfly0EaRJs6t6OvJ2KJTpzbkLG+K+nbrcJIflQvEK9qoPZ1r68JM0iQgoESDG8FQP G3sOscLfYk/9TFCjpjp889iyCzYjJY8nySHpOfCcboAGtVoNPQr/szVTw4RIn9AemV6T Aom5kdS6eMa9E/QKk/TMHKAZfb2a3gMta62ZmsVhZakioTp3LHDh0GedNl8tfCr20yUZ 9FnaW0YGvWqGTxHSCwjHEv48HGkSCcU1W+cmgk6ROIPH5Al21LpPHyt5n2UMmTyjgIG7 WRnDdZUBpmNriH7F0NlBvikXJTGuXjLPU5bI7MMspyCRvN6zvNekHO1QLWzsbsua9q0K xNAQ== X-Forwarded-Encrypted: i=1; AJvYcCXO7luFpn1UNVoFR85jWxvYOdJuIywuo5pGO5JdY7TBa0F93+TLmnhf4ey3v8KapXpYGLj6defqhmsxjoM=@vger.kernel.org X-Gm-Message-State: AOJu0YxNd5Eh/s75zjmd10V/VOwmdNicjl/OSXcoNgvP/3/n2fGr2fbm Y6R8PdD4tXP6fQ2qSQtsx5sFBeraO9hFz/EqwO3bt0k3dY3zOAKCVsMjo2sH79N1V0zraokU3At KxtGKa+SbCroPeECDiNs9KC6nqMTIiQ== X-Google-Smtp-Source: AGHT+IGe6iTItosrIYdSiu53+Ci7qA2QFXWZJ90ywhAykW0k2WhvUSTmqFJV6z+v+U/Hxqi/jtBom1ufe6Z6w654MMc= X-Received: from pjx3.prod.google.com ([2002:a17:90b:5683:b0:311:6040:2c7a]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:35d1:b0:312:e6f1:c05d with SMTP id 98e67ed59e1d1-31f5dd891dcmr12128094a91.2.1754007014453; Thu, 31 Jul 2025 17:10:14 -0700 (PDT) Date: Fri, 1 Aug 2025 00:10:08 +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=4507; i=samitolvanen@google.com; h=from:subject; bh=hZabVFFCxKVAeJdp4nYEgQjtD7vc/y+UZPfwjcPcYes=; b=owGbwMvMwCUWxa662nLh8irG02pJDBk9rPdi03WyDi2s+j2TLXrpqwQv4byFyydv75kopqocs DiXUWVmRykLgxgXg6yYIkvL19Vbd393Sn31uUgCZg4rE8gQBi5OAZjI5U0M/+sPv9nwy1tTa8sK 7+iJU/REprzNuvh764M2XiPGqQp7Xrsz/JVLcNrHEN/pst8r23nVypj3X66827fl0ElBj7VHOT6 /cmcHAA== X-Mailer: git-send-email 2.50.1.565.gc32cd1483b-goog Message-ID: <20250801001004.1859976-8-samitolvanen@google.com> Subject: [PATCH bpf-next v14 3/3] arm64/cfi,bpf: Support kCFI + BPF on arm64 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 , Puranjay Mohan , Sami Tolvanen , Dao Huang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Puranjay Mohan Currently, bpf_dispatcher_*_func() is marked with `__nocfi` therefore calling BPF programs from this interface doesn't cause CFI warnings. When BPF programs are called directly from C: from BPF helpers or struct_ops, CFI warnings are generated. Implement proper CFI prologues for the BPF programs and callbacks and drop __nocfi for arm64. Fix the trampoline generation code to emit kCFI prologue when a struct_ops trampoline is being prepared. Signed-off-by: Puranjay Mohan Co-developed-by: Maxwell Bland Signed-off-by: Maxwell Bland Co-developed-by: Sami Tolvanen Signed-off-by: Sami Tolvanen Tested-by: Dao Huang Acked-by: Will Deacon --- arch/arm64/include/asm/cfi.h | 7 +++++++ arch/arm64/net/bpf_jit_comp.c | 30 +++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 arch/arm64/include/asm/cfi.h diff --git a/arch/arm64/include/asm/cfi.h b/arch/arm64/include/asm/cfi.h new file mode 100644 index 000000000000..ab90f0351b7a --- /dev/null +++ b/arch/arm64/include/asm/cfi.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_ARM64_CFI_H +#define _ASM_ARM64_CFI_H + +#define __bpfcall + +#endif /* _ASM_ARM64_CFI_H */ diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 97dfd5432809..52ffe115a8c4 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -114,6 +115,14 @@ static inline void emit(const u32 insn, struct jit_ctx= *ctx) ctx->idx++; } =20 +static inline void emit_u32_data(const u32 data, struct jit_ctx *ctx) +{ + if (ctx->image !=3D NULL && ctx->write) + ctx->image[ctx->idx] =3D data; + + ctx->idx++; +} + static inline void emit_a64_mov_i(const int is64, const int reg, const s32 val, struct jit_ctx *ctx) { @@ -174,6 +183,12 @@ static inline void emit_bti(u32 insn, struct jit_ctx *= ctx) emit(insn, ctx); } =20 +static inline void emit_kcfi(u32 hash, struct jit_ctx *ctx) +{ + if (IS_ENABLED(CONFIG_CFI_CLANG)) + emit_u32_data(hash, ctx); +} + /* * Kernel addresses in the vmalloc space use at most 48 bits, and the * remaining bits are guaranteed to be 0x1. So we can compose the address @@ -503,7 +518,6 @@ static int build_prologue(struct jit_ctx *ctx, bool ebp= f_from_cbpf) const u8 arena_vm_base =3D bpf2a64[ARENA_VM_START]; const u8 priv_sp =3D bpf2a64[PRIVATE_SP]; void __percpu *priv_stack_ptr; - const int idx0 =3D ctx->idx; int cur_offset; =20 /* @@ -529,6 +543,9 @@ static int build_prologue(struct jit_ctx *ctx, bool ebp= f_from_cbpf) * */ =20 + emit_kcfi(is_main_prog ? cfi_bpf_hash : cfi_bpf_subprog_hash, ctx); + const int idx0 =3D ctx->idx; + /* bpf function may be invoked by 3 instruction types: * 1. bl, attached via freplace to bpf prog via short jump * 2. br, attached via freplace to bpf prog via long jump @@ -2146,9 +2163,9 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog = *prog) jit_data->ro_header =3D ro_header; } =20 - prog->bpf_func =3D (void *)ctx.ro_image; + prog->bpf_func =3D (void *)ctx.ro_image + cfi_get_offset(); prog->jited =3D 1; - prog->jited_len =3D prog_size; + prog->jited_len =3D prog_size - cfi_get_offset(); =20 if (!prog->is_func || extra_pass) { int i; @@ -2527,6 +2544,12 @@ static int prepare_trampoline(struct jit_ctx *ctx, s= truct bpf_tramp_image *im, /* return address locates above FP */ retaddr_off =3D stack_size + 8; =20 + if (flags & BPF_TRAMP_F_INDIRECT) { + /* + * Indirect call for bpf_struct_ops + */ + emit_kcfi(cfi_get_func_hash(func_addr), ctx); + } /* bpf trampoline may be invoked by 3 instruction types: * 1. bl, attached to bpf prog or kernel function via short jump * 2. br, attached to bpf prog or kernel function via long jump @@ -3045,6 +3068,7 @@ void bpf_jit_free(struct bpf_prog *prog) sizeof(jit_data->header->size)); kfree(jit_data); } + prog->bpf_func -=3D cfi_get_offset(); hdr =3D bpf_jit_binary_pack_hdr(prog); bpf_jit_binary_pack_free(hdr, NULL); priv_stack_ptr =3D prog->aux->priv_stack_ptr; --=20 2.50.1.565.gc32cd1483b-goog