From nobody Sun Oct 5 18:17:29 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 38A0123AD for ; Fri, 1 Aug 2025 00:10:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754007016; cv=none; b=aE0Rep3qVX9b1XaTk9qWJwrAC/jpUT2Hc7G1wMde+XtTd3RjnmpRXgXvvSNkcbBKM91bxDU0PUUfHnE80eB7Xz7Z5MXFJ5++CWsBTZduHQv//BvDRky3tqIOtTc/MxYgblI9utXDvfCb50WJ+1Ngcl6VVnzcNyameLGStbSiX+4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754007016; c=relaxed/simple; bh=PuRe2nGj0EpdYZQrojmZRs4VS2y7HRUovorqQS5Ex3w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DW/99zHxcSxxAEZX934z9kHeoWsU0vuQV5G+GjPWlTgugHYHS3IiW2XzFohik8HiBKh33yY8CwJAGYRFGSzix6GZqs9YgQkPFLd89aTNS04t4bBhGTxOAoKJLQt5oYvrwtXHBE47MI+xqdXySgWxFMG+BefSJM58oufsnZBQNLU= 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=sMEVM6f7; arc=none smtp.client-ip=209.85.216.73 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="sMEVM6f7" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-31366819969so1775964a91.0 for ; Thu, 31 Jul 2025 17:10:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1754007014; x=1754611814; 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=f5uDl85wDGhTjSIRJ3MDpBVcbv+/o9QKj57RG6DGCPk=; b=sMEVM6f7jcWDlIiMk1LjPmnnlnQpwhkmdbsrc9F61847XP669mRqvQs8RZv0EMOA+a SyBl24gOTZwScae4FbmtA3b4MnZMrHJIU8juX/Q6BgGiTxhPemsVdKuPo4DQ8xY9HB8I shzGsssUflLOazdINXuVYnK1z5CMN2bB6JiYiutDFWGGBPMIHeygMHkiUkDIPkLc6c1w +t2lIhgmg7qvMBgvQb7Ly28RPC0F2yLS62n/zpDdrhDK6farJvTB2dyo9wCbeti7dhJd JMAYD8jkD0tGoGD3C/bytP7S5vw/wATxU0SA1Xtz6Om7Oy4oIE1XFgaSlgwKKS7D07M5 yTDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754007014; x=1754611814; 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=f5uDl85wDGhTjSIRJ3MDpBVcbv+/o9QKj57RG6DGCPk=; b=Vhfly0EaRJs6t6OvJ2KJTpzbkLG+K+nbrcJIflQvEK9qoPZ1r68JM0iQgoESDG8FQP G3sOscLfYk/9TFCjpjp889iyCzYjJY8nySHpOfCcboAGtVoNPQr/szVTw4RIn9AemV6T Aom5kdS6eMa9E/QKk/TMHKAZfb2a3gMta62ZmsVhZakioTp3LHDh0GedNl8tfCr20yUZ 9FnaW0YGvWqGTxHSCwjHEv48HGkSCcU1W+cmgk6ROIPH5Al21LpPHyt5n2UMmTyjgIG7 WRnDdZUBpmNriH7F0NlBvikXJTGuXjLPU5bI7MMspyCRvN6zvNekHO1QLWzsbsua9q0K xNAQ== X-Forwarded-Encrypted: i=1; AJvYcCXO7luFpn1UNVoFR85jWxvYOdJuIywuo5pGO5JdY7TBa0F93+TLmnhf4ey3v8KapXpYGLj6defqhmsxjoM=@vger.kernel.org X-Gm-Message-State: AOJu0YxNd5Eh/s75zjmd10V/VOwmdNicjl/OSXcoNgvP/3/n2fGr2fbm Y6R8PdD4tXP6fQ2qSQtsx5sFBeraO9hFz/EqwO3bt0k3dY3zOAKCVsMjo2sH79N1V0zraokU3At KxtGKa+SbCroPeECDiNs9KC6nqMTIiQ== X-Google-Smtp-Source: AGHT+IGe6iTItosrIYdSiu53+Ci7qA2QFXWZJ90ywhAykW0k2WhvUSTmqFJV6z+v+U/Hxqi/jtBom1ufe6Z6w654MMc= X-Received: from pjx3.prod.google.com ([2002:a17:90b:5683:b0:311:6040:2c7a]) (user=samitolvanen job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:35d1:b0:312:e6f1:c05d with SMTP id 98e67ed59e1d1-31f5dd891dcmr12128094a91.2.1754007014453; Thu, 31 Jul 2025 17:10:14 -0700 (PDT) Date: Fri, 1 Aug 2025 00:10:08 +0000 In-Reply-To: <20250801001004.1859976-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: <20250801001004.1859976-5-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=4507; i=samitolvanen@google.com; h=from:subject; bh=hZabVFFCxKVAeJdp4nYEgQjtD7vc/y+UZPfwjcPcYes=; b=owGbwMvMwCUWxa662nLh8irG02pJDBk9rPdi03WyDi2s+j2TLXrpqwQv4byFyydv75kopqocs DiXUWVmRykLgxgXg6yYIkvL19Vbd393Sn31uUgCZg4rE8gQBi5OAZjI5U0M/+sPv9nwy1tTa8sK 7+iJU/REprzNuvh764M2XiPGqQp7Xrsz/JVLcNrHEN/pst8r23nVypj3X66827fl0ElBj7VHOT6 /cmcHAA== X-Mailer: git-send-email 2.50.1.565.gc32cd1483b-goog Message-ID: <20250801001004.1859976-8-samitolvanen@google.com> Subject: [PATCH bpf-next v14 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 | 30 +++++++++++++++++++++++++++--- 2 files changed, 34 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 97dfd5432809..52ffe115a8c4 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 @@ -114,6 +115,14 @@ static inline void emit(const u32 insn, struct jit_ctx= *ctx) ctx->idx++; } =20 +static inline void emit_u32_data(const u32 data, struct jit_ctx *ctx) +{ + if (ctx->image !=3D NULL && ctx->write) + ctx->image[ctx->idx] =3D data; + + ctx->idx++; +} + static inline void emit_a64_mov_i(const int is64, const int reg, const s32 val, struct jit_ctx *ctx) { @@ -174,6 +183,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_u32_data(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 @@ -503,7 +518,6 @@ static int build_prologue(struct jit_ctx *ctx, bool ebp= f_from_cbpf) const u8 arena_vm_base =3D bpf2a64[ARENA_VM_START]; const u8 priv_sp =3D bpf2a64[PRIVATE_SP]; void __percpu *priv_stack_ptr; - const int idx0 =3D ctx->idx; int cur_offset; =20 /* @@ -529,6 +543,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 @@ -2146,9 +2163,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; @@ -2527,6 +2544,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 @@ -3045,6 +3068,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); priv_stack_ptr =3D prog->aux->priv_stack_ptr; --=20 2.50.1.565.gc32cd1483b-goog