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