From nobody Mon Oct 6 17:09:57 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