From nobody Mon Oct 6 12:02:03 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 02D5323A9A0 for ; Mon, 21 Jul 2025 20:20:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753129228; cv=none; b=pQPjQ4HN8egziAhfaa9yEpdOxIydmfK0eWQleiZzSr7fCWuZjD99t4uWL3AEesI7cD+Vb30UeaMTG0LtZbdBSU4APf/2T+jN1v1yoWnlUmLDr56AwuRLrM5YHw1+UX+yn8YsZYDX4x0tdX8OKC5sZXrZ7Esd06I1/QJGU9pt7+U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753129228; c=relaxed/simple; bh=zd5DznBVI4sVvso/l/68ScJA/RSIQeiYIJRJ5CEjGgY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DgUBCyahNrfALJz3tSy5GkJH4xs9nJ7tOiNzDe31N2MVxtdDjb76aZQhfjiE7b81Rc4G78vr/URKgNpwF2vRQv2AenchFYANEA0odTWjYg5BEojCZsTckY5a463Cj46CVhyVOMgVS3mQpPiO49oTjE/FSjJrwgHxRR9L2QWqv58= 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=ZB4XF3Xm; arc=none smtp.client-ip=209.85.210.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="ZB4XF3Xm" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-740774348f6so4355086b3a.1 for ; Mon, 21 Jul 2025 13:20:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753129226; x=1753734026; 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=ATtT7J3eGVlX0DAsIWIni7wioYwQuHnqhrVZz3llpO4=; b=ZB4XF3Xmr2M3vs/FIVHi6GZJE43YF4K6NUd7aWqZxQf3CsUzPWmDMXyzWQA3miZB8v MMaLiNLpw8gLHEECbVxtBDzUUSX7ap9LlIzMoHsO1FA7m+U0NBCN5UbgOjMGAwarm9sQ JzzkGWSXsQreAytInRXOxWmhGIUf7rZApMsZXWWbWGlCraSMIOWsJgCIqgXybfrXBl80 ne87qZgpeu9Yt7WXLyB5pj6QCxtUyWDYiUALljc0wN8NmbzAgX5FK2vP5VNsAx8MxGpX U28FVo6/PZLpX+8u1dbZFJAnv3GIUQssVhQmosBK001aEwObLuMWoiIyNi5BrTtQAIDm zm/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753129226; x=1753734026; 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=ATtT7J3eGVlX0DAsIWIni7wioYwQuHnqhrVZz3llpO4=; b=uqZjcJwCXnmHTnOsOYyWE/igQHaWAwzFSSWRgIjB5jjj/79TCvvRaSPNtkRkW0Juge I7lDLmDv5387MqjdsWgqLxLiExkHXxawrbe6Osi2q38gWDsucTNEwZpHp1zNK02KDkMN pYbj3WQE+7e14FeOi917yfUL21hxJ+MAfm9SZ9SwIfQ8adt1GATtB/VL62KFNVkNiQyA z2C/mll33VOhmXu2qyyt9LWx+Y6U2SkLVEbcrGGjPaYk5KLohXKixVCDOxfsz3RWi+F0 XpGvbJKLn3A/4t7J7WDPcppouKV/kHQ5X8z3CFBsJ3R4maZeirGsKvUa72Y9bGqiAaO6 Zl1Q== X-Forwarded-Encrypted: i=1; AJvYcCVtdCKSQkwkFtM8hCsBFA1uz2HEMsYN2loJPaui+z9FjcSS+INf3s6qDPeLP6Hh9S2ewxr5e7z03zSI7nI=@vger.kernel.org X-Gm-Message-State: AOJu0YwpLztdLljT6e/fwuTL/bzJ7hFfwwY8DOJuy7eyxCmHISHP9Ak/ daFYZRTw0ENS2KE+3XgrP+IG+HeAWTDMWbbL2HVrjS+LxAYml1UumyT14kSfkr00tG7JraH0OYE CnBTiS9F3O3uBeYEfqKi5//OLBAuGbA== X-Google-Smtp-Source: AGHT+IFMHr6borf7kwzwjf/sDWkpfr6CqD7C7zKnwE9LtBZhs0jVXDx6hG0voSrPzJ9O9xdehYJ43YU69oZLT1yhuXc= X-Received: from pfbdh6.prod.google.com ([2002:a05:6a00:4786:b0:746:2a27:3025]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:179f:b0:749:4fd7:3513 with SMTP id d2e1a72fcca58-759ad9e5239mr17855621b3a.16.1753129226170; Mon, 21 Jul 2025 13:20:26 -0700 (PDT) Date: Mon, 21 Jul 2025 20:20:17 +0000 In-Reply-To: <20250721202015.3530876-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: <20250721202015.3530876-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=Dmv7MBdL2t4YlIN+ZApIs9QGn/40RNGjIGDoX876EIY=; b=owGbwMvMwCUWxa662nLh8irG02pJDBl1CxnqNh54s6quo15jb5tZrkxbpfL6Jv22VLWgfzd6F GTfOOzoKGVhEONikBVTZGn5unrr7u9Oqa8+F0nAzGFlAhnCwMUpABM5wMbI8DcknJ0t5F/vvaom pj9+Gsy/7gdvUpzI6b5azZuD3SiihpFhl8XUw5Z+xseCHzuuWyhu37zwzZEfQi8i4+30jbZYzj/ CDwA= X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250721202015.3530876-6-samitolvanen@google.com> Subject: [PATCH bpf-next v12 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.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 12:02:03 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 B00C923C513 for ; Mon, 21 Jul 2025 20:20:28 +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=1753129230; cv=none; b=mGgy9921qXZNEALswuwznkelH+Tz4MPV9snzNxP8gS691bbH5/0K69CgjsMBlPyTdbXnfRSOUDScPSpsQSzqNoGWjQWkC4DTJxKahdZ1J0CAVGHAwTYx1QoUWgDdhy6xVFouCADhCkVjvdc2W5TMXHu5iODqYubGqkUxpFZ19ak= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753129230; c=relaxed/simple; bh=8sqyVESEJUMNiJl/uCjzhPfl9OVRxgVUJjpXrAl5Bgs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MtNrtNxUvz3X6B4QlsaVUpJGJlgzW4i5mlSHkXvsVVweIdqbpPVnMxtuuruKrTaHA0Hv273unBGLIyO9is6SvTiImgy25GAJbBkkYanej/Ft2wsbqgctdzHbrs6CR79MBeM5azgrUo71Q+hXuxV3XRnDVObTF7s6r6RYNAy19IA= 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=h5UPIvgu; 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="h5UPIvgu" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2354ba59eb6so66769895ad.1 for ; Mon, 21 Jul 2025 13:20:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753129228; x=1753734028; 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=FfCLGywMG5rCOsx3g823MlhxRQ/vXUvg0nXF69NJcN8=; b=h5UPIvgu6tgiAZRh1o9lV5y6qcifmRdz8mT6VrRHPOJ4ViWuPFi7WdbB91m6MWFWzQ yvGuM6dpIrc6LF0jcqD7kOkvYFaxPdh1SKwc4GZod/bhRXXMWeSFB6u4r6/r+lTIk9M4 psIblFYFj6qNuOawe03FhXfdJ2wP8+dJS/Cm8QOpJ4+B7+ms7OmKHtE9p6wcSD80S8cf U229xDSdJG5eEzywWtAzsdLoda0DfZp/ViEnYPzhWPa++FQG8ojtLSZ1W1zOrwENbewB GQN7w/dCAbpx9L9mcf8IxPhkwMA7Wzq8l9fb0K+wrt7plre0k62ymcTKhUDke4rJpGAb DDWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753129228; x=1753734028; 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=FfCLGywMG5rCOsx3g823MlhxRQ/vXUvg0nXF69NJcN8=; b=CaTf8fJyBUXEn8yBzMLqGNX9Dv3D9Rc6s5CbuHmsV/YUaWWwW7/sCYZErd0KQ2z+ZR 7zISIvswpU/qnwsaYNx4puLBgOOX1qndWXcFsU9Zu5jeQtduPYEmJlZu6npn38aVvSq9 ZgorFhPTGGfH3cxI4wbarT8nT2KTjkKHydyblzytPxSerFzXtz132sF6nQrNd/M3ttrV oIAysk/tjWVR51gIE2+zAW8cHR9O6hGll7CxKhprBtZ5U3UDmwiI+bMTFpUi12abngIJ 0pLLuHrgpwdKazEfnC1AOB0Ts8VmGsdtCvtSddZwbvPJX82sv2oTPtRJGevvjMtPRyEq u0fQ== X-Forwarded-Encrypted: i=1; AJvYcCUjCL6oqQJOaiH9isz8ann607Ajz11NdeEadpLiArc75IWF4gNM9q/aZntiaf46DOG2fsU+q4Ah9EjyJT4=@vger.kernel.org X-Gm-Message-State: AOJu0YyhcnYDXZMdiKjkzlniysSxaNzifrMKug6zVSvwmteG3/NLH247 860fmr3Gk7vlb3cN9KNb4NRcZPm/9fPQWCvYiueW7atNqLSHC2/01m4AuaXysdc9GSMDZJMqbfQ EZvFOIB+anIsK3g57fy+XlkBjWhNBgw== X-Google-Smtp-Source: AGHT+IF9TN23l4Ltc5Ne0W9DXoqr4LrShXyYJ09L4HPKeDWk0WXzGvv99/JjdmYIPAHjl12wDtMdJ0tsHdbTsMDzT4M= X-Received: from pjbph15.prod.google.com ([2002:a17:90b:3bcf:b0:313:285a:5547]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:228a:b0:235:e942:cb9c with SMTP id d9443c01a7336-23e2566aa35mr324880815ad.5.1753129228021; Mon, 21 Jul 2025 13:20:28 -0700 (PDT) Date: Mon, 21 Jul 2025 20:20:18 +0000 In-Reply-To: <20250721202015.3530876-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: <20250721202015.3530876-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=8sqyVESEJUMNiJl/uCjzhPfl9OVRxgVUJjpXrAl5Bgs=; b=owGbwMvMwCUWxa662nLh8irG02pJDBl1CxnubhOs9bG86TVvvUGxxO87f2/x30p4zuy/IZlJf ENdZCxLRykLgxgXg6yYIkvL19Vbd393Sn31uUgCZg4rE8gQBi5OAZjI10BGhmtPP/KsOczfuyr6 cPaq3d0dBsJhr6SqDrZOmfo7ucNuQQrDf5fUL8VPJE2edc5cGlTUvqzpS/Xyl8o865YopAY+Ulg bzwgA X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250721202015.3530876-7-samitolvanen@google.com> Subject: [PATCH bpf-next v12 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.0.727.gbf7dc18ff4-goog From nobody Mon Oct 6 12:02:03 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 6583323D2A8 for ; Mon, 21 Jul 2025 20:20:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753129231; cv=none; b=HxDCR+mg9peF6Y/F6HQNeSkaDpa5/HYKmZdbUVXzWwAPcDPAaWjr4yQzeeoy3pqGWeO14FDABqFCwXT2ZgmN4CWBHV8DLH8vRizfDFDzHaQ+Gwi17wX3h/DE4J3owpXb/bhJjVlBdCQPRtAeLmueJ6DB3uMEeI86Mz6ZfzWJOvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753129231; c=relaxed/simple; bh=V+dt4wW9K74uYhg92zaTld9WHEncBcnDJyhO9sfR1hk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lX2K17lFEErHwF7stwyiWt6j5vRqDl1nqom4R2EngOXpq9YqFCgxlzCiiuKytW+ZKx7uOBjJf9v7Vb+Tv6iBLM7bd08Jf3rd/vxshiIZq7Od6rSm8jY2+no8W6/q6Gdv3AHqOltRX50RM1EFgTy5vqiR3Wh6JkZuBrKmsO598/U= 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=pFO0Xg4f; arc=none smtp.client-ip=209.85.210.201 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="pFO0Xg4f" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-74b4d2f67d5so4060999b3a.3 for ; Mon, 21 Jul 2025 13:20:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753129229; x=1753734029; 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=ZHownloYScgx/peEPOAu+NanD7LJponwsGoBJ5e+uKQ=; b=pFO0Xg4fPSzNYFcBv9oPAheEJAcbw4BlR5Q9ei+mA8sFfAMF7YmXmDbyL945XKYT04 8CC/xEaINTnxhZ6AoPfDPBWT4gMmJjkljlQrUj+QPWvrmwxIkWWRLXPNpZxjSsU09kyp nYrD3dGy1IuHm+E55HnPtOHWQJYehwPG3G4lz43h87amMU1eVK4OqixTZK/mwX0O4WTy zSleQ+ShLgYDzWbKfuxeSZAlGeYrLsZUjVPdlVOl4/8qFKpmqRl4FLcmdCstbJ7mdWb0 WLgLJniDbk8fFJN5LC+qwbKHBL66EmsY4PRsYWhB+Xi5+qMSM5ueGTylun4MoZ+vqG9w BfXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753129229; x=1753734029; 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=ZHownloYScgx/peEPOAu+NanD7LJponwsGoBJ5e+uKQ=; b=uWqpbNadX8hYFiCkSm3rFApAL6/bvP8yDwygbH2TtsC1vVtkHaIOfgFz3pRDAtdb9F PULv+xZ3jUTVMN5VE9d3jD5OktfpbI0co9jMZhnn9HpvnJXjx4LAlLyYS7V9BgWVW1WR RN6FhghLd1ryUgwA5HWiZiePWrIMtgLJiZWmwIKnL8bortTMRdS7U5uCPOusC46Buwni W9lcSuaBfoxnA4DFfp43VfDqCP98X9mGcYkhOfApOIhPSHA7KMsrCQTT5bvwtkpZctXG cvSidS0lzsS9MvrcmWzwqVnxeT3j/FfNjvKGgr+LoaZoc/ZqrpiEJoK3qvNwa/TwihUi OVTA== X-Forwarded-Encrypted: i=1; AJvYcCXrzFrQNxD8vMBzoZ2QNVULtShc6NetCTOuh5mZdE3PyvZ43TzHSfLsDINpaKUUseB3E8/kH0wh1mtzyLg=@vger.kernel.org X-Gm-Message-State: AOJu0YzxiMKlRUcwmP84F3kP0tn/CyqXF2HPe/KGOuZC+jZSBhqyrEZR qzx4W7vNOeVa4FkEzvgACMvkVexPKspz0eWwOmxX/rDd6y4KuP4oPA29A3jugJrB/7UnHxdy0tK tAWzoU0FSJI4Y5EcRiW5Dk1pV93SNAw== X-Google-Smtp-Source: AGHT+IH7KB4Embbv+x/j7/TiNXBRPpel5ppAcMFteDc0+/hNDLuiR64hBw5sgTN0lkM+CXbz8tU6Wnv96FwrgIwShQg= X-Received: from pfbca26.prod.google.com ([2002:a05:6a00:419a:b0:746:2524:8aab]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e91:b0:758:b81:603a with SMTP id d2e1a72fcca58-759ab63965bmr19047414b3a.2.1753129229517; Mon, 21 Jul 2025 13:20:29 -0700 (PDT) Date: Mon, 21 Jul 2025 20:20:19 +0000 In-Reply-To: <20250721202015.3530876-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: <20250721202015.3530876-5-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=4115; i=samitolvanen@google.com; h=from:subject; bh=Btyu244psEehuoYDLzh4LQVWud/Fy+nWITFJv1APkz4=; b=owGbwMvMwCUWxa662nLh8irG02pJDBl1CxlXHHOZf/X60+irp+9+N/yvsFUr1TSxkOX50n0W9 7iOdF8v6ShlYRDjYpAVU2Rp+bp66+7vTqmvPhdJwMxhZQIZwsDFKQATqV7OyPDk61XxZz43n97W XXkldZeiValAjU3Lg8/xfy1qL8w9+WYfI8Pd4OSJn26/Y/iUunOV4+en2xlUi8St5SW8Z8e1Kmp P3sUNAA== X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250721202015.3530876-8-samitolvanen@google.com> Subject: [PATCH bpf-next v12 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 | 22 +++++++++++++++++++--- 2 files changed, 26 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 89b1b8c248c6..f4a98c1a1583 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 @@ -166,6 +167,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(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 @@ -476,7 +483,6 @@ static int build_prologue(struct jit_ctx *ctx, bool ebp= f_from_cbpf) const bool is_main_prog =3D !bpf_is_subprog(prog); const u8 fp =3D bpf2a64[BPF_REG_FP]; const u8 arena_vm_base =3D bpf2a64[ARENA_VM_START]; - const int idx0 =3D ctx->idx; int cur_offset; =20 /* @@ -502,6 +508,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 @@ -2055,9 +2064,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; @@ -2426,6 +2435,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 @@ -2942,6 +2957,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); WARN_ON_ONCE(!bpf_prog_kallsyms_verify_off(prog)); --=20 2.50.0.727.gbf7dc18ff4-goog