From nobody Mon Oct 6 11:51:56 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 B227A285412 for ; Tue, 22 Jul 2025 20:54:04 +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=1753217646; cv=none; b=cxlJSQyZqrEde8nwaqZH/Q/g3QlXyCrfXbupVCm9QTfFKGX80961tiV9IS3LBQI5J9uKj7iNZU2gs5uisepsUuxrSNK9g6OwCZItM9ILruj1i9oYBN9cAJBoz6zrnVAbXVfT4SLNKZYR2QO/k0iXwOCS+9UoXY1xyC3qxZvOvRA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753217646; 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=KLLy/QdYDw/j6+ekJfUrUdbOvcjCF0agDSR+t/eE1sfSOzzB97FkrjBDI1wE4x4ylykHGv9C90xWcVu4eezjl4YKxhJkOR89xqahyYbiPkoiX1jQTADnVrdi4xt0oEFlh64Unrrft/QUT92iRt/ieKdRkIcC9Ewxod34pcD6/a8= 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=t1m50MBX; 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="t1m50MBX" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b3beafa8d60so7409512a12.3 for ; Tue, 22 Jul 2025 13:54:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1753217644; x=1753822444; 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=t1m50MBXZZaal5IVGJPr1t0vSbd5dNShxxt06B3FoyuXs6zdkCdof3r9sAl3WN/Oxx yZkHUfCAWmDg+Uz2QO93PDRT47gB/DAiXqZQoWGbhiIKqdTu/W2IyzBet+UlpsJL8h2t 3r6qtnMaCbpdgluyfy27Gr6Upm4Q979JeJO+MkhtFwk2vLk6yYRwuewZ0MrKWAEGZAoW C9iPPDRLQGbpoEOMaRhrJuRYIpUpBNwd1CEZo1VboLkZcrpZkDMhrZTtGXlcKiTb2SqO 1w7VpahmcbGkQYR2SXc4SzWPMah6uNCesyJck9jO7UbI4UEAgEKuZW7q4IKO3dyfmJIA zEJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753217644; x=1753822444; 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=iix5M7Y9lTLKg1R69SBUrxSNEF+mCwWY7M8pCvrJkt6qv7eyiqpAb5MKPjy9fzlwNX NMXdYzAcZRtZChUVNgidmP5s/Dwj+keF8nkFjHOc1dMTn7UcNmbKfL87NGSXEnTlnzOF jj5sCEJW45nZnTF10Nlfzy4kz+lXv0R6jfchM7LcMvwHEVzzVVkgVuLJfYVXfpGlOx2P TtgGb9zPHXitlprxgYRLSXU3AJmPd1nFdPfxPHUerWtLqbZlNm4jfne1LHhD/qJS3Mf5 rAM8p1aqFY1Q+TatDN0fyWN4CwdJeSGhI8TDSRU3EMqGCiw71jukpnFLZJXBR0JsWMxZ Af3Q== X-Forwarded-Encrypted: i=1; AJvYcCWmJnbjdse2TRASDyVARCt2oAk1FIb/JN0PcWMRnXnFk38GCzFgPzmYnfPXUmYIRowzRd8VC4Y1Is9qM6U=@vger.kernel.org X-Gm-Message-State: AOJu0Yzk52cQeARVKNnTnPm2B7bYLvkigzPOUQr4WiRJBpxxGag5Vspw VK+KPHKniOkqjyHETJjnBuvA0vQE4m7QHTvucqwQ4pfuGR+JnU2Ha/P7USDk02qd1rUCKrUyXsQ 8MvtLD3Ai1HEmlTu96vyH5NdJH9W54w== X-Google-Smtp-Source: AGHT+IGcJ1yJH0fRzYxlDDUqZBhJPeXzC6DrCDd4ckApy9RMyQ4dsnkHLjAoOoLgwzZvQEvUMtYym8FpF7QRiypohqI= X-Received: from pfbbe3.prod.google.com ([2002:a05:6a00:1f03:b0:748:e071:298a]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:b95:b0:220:94b1:f1b8 with SMTP id adf61e73a8af0-23d48e6859amr495572637.0.1753217643988; Tue, 22 Jul 2025 13:54:03 -0700 (PDT) Date: Tue, 22 Jul 2025 20:53:59 +0000 In-Reply-To: <20250722205357.3347626-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: <20250722205357.3347626-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=owGbwMvMwCUWxa662nLh8irG02pJDBn1v9LqNh54s6quo15jb5tZrkxbpfL6Jv22VLWgfzd6F GTfOOzoKGVhEONikBVTZGn5unrr7u9Oqa8+F0nAzGFlAhnCwMUpABOZcoLhn80avWDtm/9ExOK3 JJcwrNkVbJq/Snrm8S09a9+8MG0wZWNk+CabYXP+em13pVXuzycBPey/Km8+XfDejO2F1Ak+b8+ F3AA= X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250722205357.3347626-6-samitolvanen@google.com> Subject: [PATCH bpf-next v13 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