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 18BA623AB85 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-74d12fa4619so4029095b3a.0 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=o0qTjrQ+k1Q1SKiv625O/qyHsiEvJREPNKntzVaoXeAzcNbgqKkQNMuzT7TRBTnful qmSmjFKfT91PYivkG/Tk0YqVAnNuQiJO5gGtFrE97j1I2h2/ml/F6SNXK1LPBt/1B6CU fa5hEQ4I9Zcq6mWXxHKtKL2acs2MbiKJJO+xXcbJaweQrvtPa5k9znSKUQfb7FD7yTWI BvIg4fGHGaQY+HI6a8TTEdumpECbfQfLHe7tDvzEAopettgbh69xvCp+7gYKEnnhH8jf dDxvc27bWpXvHONG/ED50Mn+b5SEI2d5WItII3xUtA67o/t+K7IflDg9yB6i+9kEpe4u 5YbA== X-Gm-Message-State: AOJu0YzF9Bre9ofTCh6jmIhmnych0/EO73zYnbfVYTi+F31z/cM8iYbP 0Qk4iHWMoaWXmNWMcHCjGyhRsX+f0bcVINwRAru1FUVXkfsIXzsl2s1+f4vAlQ6QtpZQPJtiK2j jXDTyfwkPc/Uy1uLZyrclA4J4IGAvl6u1ck9WK0MoTtFLqvvfJ4lxL/i4eRx1/XsldMWDa+Y3sv QfDjvWkiXgER/S1482ocHPaZcFs9xiAxulks4JxI/BOKmSpjBiBDuM5X2P15utP6IN 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: bpf@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-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 @@ -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 = __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); /* 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 = __kcfi_typeid___bpf_callback_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); 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 #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 = __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); /* 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 = __kcfi_typeid___bpf_callback_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); 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 +#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 = __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 */ -- 2.50.0.727.gbf7dc18ff4-goog