From nobody Mon Oct 6 15:17:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.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 D9302220F54 for ; Fri, 18 Jul 2025 22:33:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752878035; cv=none; b=R8nwF72xTSZ8Beg0OibJJQvdwAOi0JOpBsaH2cUuKV9f7d2IzleaIgrBdFaG1oFza5g0aA9GpRWZGZ63pxeLLiYg/QQpoWH7oDxbpUqDG85jv6HHDVn3485NZL9Ri2gfa/Ajl6yBj/ep2P1GF2YqfIcSvH5E82M1KS/uBKI6qMc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752878035; c=relaxed/simple; bh=YzklplaX66FRw2n+YpF5FgSFa2H+Ju4lGEEz8h3bWlQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AKKxjS33kvBevq4DEk+HOlBzRrFi9y9e2xEF6O4+GwHiJGcRHCRzqt4d2V3m8bSWs8QNAlnJtYGr/ciZUDO9s/HEb4OXYswGKL2cc5+FhfRs3Q9SObiKfvnDeLq7OrVog8Tkpnxi/JSL3+sWrccN1e0TSxmAyMMJMPle8BT8U6Q= 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=VH/Bhwu2; arc=none smtp.client-ip=209.85.216.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--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="VH/Bhwu2" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-313ff01d2a6so2048788a91.3 for ; Fri, 18 Jul 2025 15:33:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752878033; x=1753482833; 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=wLNwsWMuJI2qW6psZ/XSLZhNvSth+QDmozusgk4AwUU=; b=VH/Bhwu20Ww7uE/O8tYMChVO/ssI/gvMtBr4mo8+XeFn+3njB7byJvhhoj4QruEHAG WlDU2gzG7pvSZbBPFbHZQ56lmmnba8G6dNCeDjoNOwQOM4h7c+tZ+USJL11NccFGhxw/ KRhx0OVlMtzHDH+RS3Vc7/MlqwilU3fQFrDmZZyh7Ag1Y7UZXKIs6KpWZr8R2QxMmoZr 10ZJxejbzAZLdju5t7Wn0kn3Wl/qwSBYXAdhYGt/cS/hzffnYYx8fD9MiYo9YuDk/TOn 1TkNcpl66sD5RBotVmmmaXC1PD72DaHaufbiFmcDSi732UlFlJH/0/miZ13YWOuyDJBz 6oMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752878033; x=1753482833; 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=wLNwsWMuJI2qW6psZ/XSLZhNvSth+QDmozusgk4AwUU=; b=kBsXn+s6xCdwvbnmFK7qv/l69s7bc3w8nduZDsmw6soaD0d4b4j68I1mux9unfMbzG rZFuXy8KmzNfOMaPkjJVzpu11YgDndv5b/H5vDuFMhYYsUfye9P98xipxZHJLEHpLsAN 87X8jWrglcyC0wxBNtiGLxnX56MKdujGx+fpJMXxPa8DXMIF+IuO7GkNm8SKXX3XUveo G3kC4QV+CkkRKsCbEA1vxSqIRjt4a6QxDodPtg6u4p1Bwd6pxHA1ODQF1TJvrFzwne89 4U7vB6pehnlryRZNNtCr3w85uR4bbKg6KWhS4x9fF8CfDWsNk3PZdD0vzyquE+e4O96n /7Tg== X-Forwarded-Encrypted: i=1; AJvYcCWRMAE1/UXCfBC2s0MIit4kFt9Dc4DOkqB0e56UP63YFZtuVEQRDaO0m1qd54s/Fc3vzqpI3sSXIovIoRI=@vger.kernel.org X-Gm-Message-State: AOJu0YyQbhnfjnOWZMl5UH3lmiXw8cod0hzRr+/vOtxXXLZjBbZBtAC4 cwNy90k9DK0k9jeVy09ZSRjbWHdsgYl7zsVDIbhpNFkoexZGVLkXgctoHEptvY2pIB6ROzQ1xrd PvviPatFWktCf6Q0wbJnf7QjUNw75Ug== X-Google-Smtp-Source: AGHT+IEe+xTUihnDAkAc3U6ajgW2TM/Cn5Jl9867IKn9i7wvbqfMvGRzsT3rj30WxMWgL+wneVXowCfaUZatHchIzP0= X-Received: from pjbtc15.prod.google.com ([2002:a17:90b:540f:b0:31c:2fe4:33b4]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:564d:b0:312:e744:5b76 with SMTP id 98e67ed59e1d1-31cc2608a4cmr7672725a91.33.1752878033143; Fri, 18 Jul 2025 15:33:53 -0700 (PDT) Date: Fri, 18 Jul 2025 22:33:47 +0000 In-Reply-To: <20250718223345.1075521-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: <20250718223345.1075521-5-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5299; i=samitolvanen@google.com; h=from:subject; bh=FeGMABhm61nFtopaseiCFlvAnaScqh+g9nMMwrwAEJc=; b=owGbwMvMwCUWxa662nLh8irG02pJDBlVp095Stl4hFR9l0nZwn3S5aa/++KX9ZM521afeSC16 OmBl/78HaUsDGJcDLJiiiwtX1dv3f3dKfXV5yIJmDmsTCBDGLg4BWAif58y/HeNFk3zsDfjnRhn qFTWPYWpcn31hKfrnN/7q4e9YaqUu8Dw34UrJPrQIsmrR154NE+69Cb8nO0VCUnDYtGkx2e4Gu0 mMgMA X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250718223345.1075521-6-samitolvanen@google.com> Subject: [PATCH bpf-next v11 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..e5567c0fd0b3 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 15:17:31 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 7F66F22258E for ; Fri, 18 Jul 2025 22:33:55 +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=1752878037; cv=none; b=pvGU3PC0JCI1vpPbZvb1y9w1FMM5z408hwlKjOUzdAX73SPd6NScG4ws+q02qNjNXvlfWUDoPsA4XMU7EqSmQwtCifF8FzGcGX3XDqLdd99/SwIWuarEjK6zT1zZvwwkUdMPVUAqXSiUxYT90U18PHTOZ1bjayQeqazLW02LUg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752878037; c=relaxed/simple; bh=/Pp84iiKhYqnoXcwmrfh0wQnxZW4ZWvLGB2ZBYipHSs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sUF0nAMezTAHfjqWTPuoYHKk5qBCyWHAD4a1LWYcBRCLcPDRJ88VEqn6FQvT7/4saUBEapQKR4N17jm6RNuY+J8IhcIMKF06pajg2fgKVklZ4WWTkr3YIZzjocWd7wTAiQDgUE4eCp0C5Ezi09xpqlbJwRSbqIfJMcXgQlxgRKU= 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=nWkVRzTs; 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="nWkVRzTs" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-748e6457567so2262858b3a.1 for ; Fri, 18 Jul 2025 15:33:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752878035; x=1753482835; 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=N/LI2mE7zqMo++9mb9IpCNbm5MsNfp6DXDgfkTWDvxI=; b=nWkVRzTs+meIv5XnzoyMpwZTKEFihxsYLI3e+IrsntgMWwFPO05tDejXnE2Fvu7lct LKtDJ3YvTuB41krq2CQz0koNxZ5WtHbHHqXtBgRfbG7Xh1cfauPYm5OU1MV73o2j5ex/ ubWyWP1xCv0XCU7JKdlIsWM+Be7Lyrx9BG7pVdIhCS2dISMOaNfQhmNBIemS6FmyzsQG Un42gR+OgJ6VarD8Q8Trj+tkyJEFgEdzL7GmOFdCafmIQTcEaxuQod4i0A5L+cUDeyd5 aCxAjcFfTCFtcwPB9ouHXSM1W4fq2oD7Wl+TNBXJ1eGLpLadpCThQilTyKEVfQnqRtTX v03A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752878035; x=1753482835; 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=N/LI2mE7zqMo++9mb9IpCNbm5MsNfp6DXDgfkTWDvxI=; b=UYME5PRUZda0R/LVIKfOVt12kW/W5Jl7LwcHMtz3VHQ+w1M6geY9uKVWGCUnhvWlXC PRjD9ywcdRugtwDfF0KTwdgx/eUFNTDZBrSO9X6qyT+sQF+kREgaegFlb/8L3yprhCqt S7SK55PWeIjmWIOb75l/KvGv5fynuOFQXgtBNK69FyOytERFK4OZ3vPM2j3bpoMe4BZ3 +GQJ2kGTXdHvurUKsnJIzrgrS+aQrYadWMOgJRXQxPQeeVnqrGtEvIPP1pvyquc0WmOy DPzysoEIbbjWkdzsG4++nKhy+13cA2cNO38g/jaRbS76OgueBw/yuX/GDs72DSajAOwa ESCQ== X-Forwarded-Encrypted: i=1; AJvYcCWkJI0JsX1UcOYHD3GYYvYaVgaLBDX4tRm6+ghzCVGT7YNQeX64KgIKRzoItXxImlougN9c+zQoGFCmWq0=@vger.kernel.org X-Gm-Message-State: AOJu0YyI4HtrKL+H3bM8BTMnroj0ZazctGgLinF0CABOsoxLDahR4uki yyozLXbTULw9PlAD9x9OY3DpeOYm40g9LTL3t8MRrBWo2DXPWjY3gDTaZvQYG3M5LbPwYdFW0PE kA9LcQHqcbaY9+X5PhxLuhW0g3UjT0g== X-Google-Smtp-Source: AGHT+IGYsweP5RXtaeuhANIUviNkKF+PZe6ZB7Pqx3lnfK43GlSHJqBo4bHYLGMK7KP63CQvnlaCF3uNDtst5fF4o6w= X-Received: from pgcv16.prod.google.com ([2002:a05:6a02:5310:b0:b3f:3770:71fb]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:2d07:b0:1f3:3547:f21b with SMTP id adf61e73a8af0-2390c744ecemr15750188637.5.1752878034816; Fri, 18 Jul 2025 15:33:54 -0700 (PDT) Date: Fri, 18 Jul 2025 22:33:48 +0000 In-Reply-To: <20250718223345.1075521-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: <20250718223345.1075521-5-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=7386; i=samitolvanen@google.com; h=from:subject; bh=/Pp84iiKhYqnoXcwmrfh0wQnxZW4ZWvLGB2ZBYipHSs=; b=owGbwMvMwCUWxa662nLh8irG02pJDBlVp09t3PuwUN59x0cx7ymzuQ9IBU09K9HkH2HSw3y0S 9SLcbN0RykLgxgXg6yYIkvL19Vbd393Sn31uUgCZg4rE8gQBi5OAZjITC9Ghsvsm60+LeS1n1dQ HXN/+4Lr109XTms41qso6r2CQbXg9wWG/xGrEqsk6k5a/Xf8ebK0pcXenefHw6Klab2X5m25/cB enh8A X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250718223345.1075521-7-samitolvanen@google.com> Subject: [PATCH bpf-next v11 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..6f1e74ccc6b3 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. + */ +typeof(*(bpf_func_t)0) __bpf_prog_runX; +DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX); + +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 15:17:31 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 62C04224254 for ; Fri, 18 Jul 2025 22:33:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752878038; cv=none; b=DWWBht8y5iQ3g1Jd6G5IW5fv08Bq+1FE+XEuBnYOfbgdmh1EBxpj/MGwSvtIT6PJp5Q4dm8+/weeEpvkI7UImD4RkA1HnOW9Y9LEH7ZOkbJ8LXhm4wZ7Ocrkrh3jC30e8ZizP86enzYDFlTsAUmCivH21AhRdj8PIPrXFFu1TQo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752878038; c=relaxed/simple; bh=V+dt4wW9K74uYhg92zaTld9WHEncBcnDJyhO9sfR1hk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IKXtJwNoB866qh7eQ4GZNaJ664IQ/KOXzPynsiemRzWiNuIr7fOBdtrZtl7YvhvyYeERS/kKo6qt0FXpc7QdpFW5oT0YpVXAP3KX3URiXIFrsdGvog+s6gpMUuJq7KlYK+oK3s75yo6rvy3BixFgtqIc1nYVhcPugylS+33iyzQ= 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=oI7Bw7Th; arc=none smtp.client-ip=209.85.215.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="oI7Bw7Th" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b31c38d4063so1718552a12.3 for ; Fri, 18 Jul 2025 15:33:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752878036; x=1753482836; 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=oI7Bw7ThmTp4tRjgB/7JqO4/MSNI8ZimiQz7rj9hUuvgl9vGGfYSHMQVtKzfuJVh5b M8KvSVyKKvE2ZgyhdDLHOATl7AfEF6I0qCt1OfKSnhvWbVTtyI7tPq4qcrojd3ES+lER kgXNTDtbvhZ+2g+Sx2JD+fIFD4xW3SzjFOxV0lXtXqSg9k0k+rYn5hXXTgOT9ZEB0N1M Lrn5bTohRo/GYeqf35tm5oxOJy59/ahjdZttD/hR5FpCCghHs7uYt0pyUDOFAQZvy6Rc wf++/O+2lfvB9N17Osdc2MTKu+F1/u9qdq7w5lc5j9pMhOLOPfdgFEnDi7WI1I4z4JV7 vkHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752878036; x=1753482836; 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=NU68zkxuGmVxU7rh/JbcuH9xtTZpm54brW383ojpRi0zUCnCwA8aQsnzGklUeJOK8w IcEKfTaz5gPIazpZugjgTtO10HTxVdj/GGpsFgiJVZPjBnWSQFcC+rf0jpqbuZeEaLYo gGehyan5FkD19LeM2b9sbuPB35nglIJnWZtkxWaETgBGd8OBvgGhAuScnZ8Qi5A0txxc XtJKfykTkQE+l/G09nyzDhtZPr0wfvSzEzVTe/AQxzg77zpq4utXw0SHO7/Tyixa8/sn 3ETrdGqpIfefDENGJZ223SN64ww6qCvFB8y7h2R1WqEPMgIpny7OUKALM17tYNHLQ1AZ 1JEA== X-Forwarded-Encrypted: i=1; AJvYcCVtMxJ6EP7O4aBGEAv7whwPjCH2crpKvDMNyJhKXJZ1BnOiW2SJP8D5IA8Ezchz6xAoIvDGQj9wvasnyo0=@vger.kernel.org X-Gm-Message-State: AOJu0YwG8mtK9IOlm2+Vf0KeZ7ZA8xxO7sN9NxsM8dllg8IHFf3ato1h 7AzcoqiQ2qi7kFpMciTzbpRZUY03T9g5NvKQuo/esqgwv9lPiZqXTJ1YV9iDyUwpHNF+a9hpSoN bKF9rzaMXXByFQZrpQMYQAbu/KxwI+g== X-Google-Smtp-Source: AGHT+IH/zdK2zKLGKRZy6B1NJr3Oj3Ajj+FM6XNXJaqaMMi3F49NCdknto5RfR6BR+VXKXY2MJ7ChfHz+3y7/DnjrgM= X-Received: from pgbcw13.prod.google.com ([2002:a05:6a02:428d:b0:b3b:b53c:abce]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:7283:b0:220:b161:431 with SMTP id adf61e73a8af0-23811d5b52dmr20957669637.7.1752878036666; Fri, 18 Jul 2025 15:33:56 -0700 (PDT) Date: Fri, 18 Jul 2025 22:33:49 +0000 In-Reply-To: <20250718223345.1075521-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: <20250718223345.1075521-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=owGbwMvMwCUWxa662nLh8irG02pJDBlVp0+vOOYy/+r1p9FXT9/9bvhfYatWqmliIcvzpfss7 nEd6b5e0lHKwiDGxSArpsjS8nX11t3fnVJffS6SgJnDygQyhIGLUwAmYv2Q4b/Tz0kKatVhr094 /Mh1+8s362EK68bAg55SBpU3Z/W19b9h+MOxK2GeXUlXzJuM9M3uFzpnH7O2PvB9isca9m0a5pX XgngB X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250718223345.1075521-8-samitolvanen@google.com> Subject: [PATCH bpf-next v11 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