From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E0477FA374F for ; Fri, 2 Jan 2026 15:02:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=PjHUFzrsqk2p5SI+Seak6yB69TLkO5KnxAZlltu8VN0=; b=bLesO3Br0cB3rVLy4tcBT/sVcT vihdWnoSb5WcpSumRMXNKENbKGy1uRv3w8RTC1GtKtCmbJ94gQZ9KcQkSL55wIO+BFcTldL4TnG0h JaCsLB/3Koybz8lr7gfFi7jK6lcEZycByKh+pD+yO3YKXqcoGyE4jm4yotrcOI5gBHhXvUdKyWSuz KKEVK0/hfsyOneIiLk8FISCHsir9cDwERK5PA24+0j9+OS8zYN5Qp1icqagUAd/JpyhBKG4AW6LO4 4ZcD2Y4L0PMoEfeokkS78+OH3I7vGyMERn0zGZ83Bhhb+2U76RTrnH5QcArnAErEoFpLLBRplAwmc x27NCPOg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1vbgfM-00000008Onr-3kSd; Fri, 02 Jan 2026 15:02:12 +0000 Received: from out-187.mta1.migadu.com ([2001:41d0:203:375::bb]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1vbgfI-00000008OmU-40bG for linux-arm-kernel@lists.infradead.org; Fri, 02 Jan 2026 15:02:11 +0000 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1767366126; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PjHUFzrsqk2p5SI+Seak6yB69TLkO5KnxAZlltu8VN0=; b=CiljkX+cqHhZJB7ZlqBXKzmzY3XQReM24Odz2Ch2tN65WE9slo5Z85mQE+ILAgJPqr2Im7 mLE/Ag4NWpe9g7232/7M/CRvmIwLuJRidJGsjRGDnVfIVpeZZGxi8PO4qDpg2KZgZj8UMG VZhWjy1LqrYgYwt/EVPSAbm1/EWuItM= From: Leon Hwang To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Puranjay Mohan , Xu Kuohai , Catalin Marinas , Will Deacon , "David S . Miller" , David Ahern , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Andrew Morton , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, kernel-patches-bot@fb.com, Leon Hwang Subject: [PATCH bpf-next 3/4] bpf, arm64: tailcall: Eliminate max_entries and bpf_func access at runtime Date: Fri, 2 Jan 2026 23:00:31 +0800 Message-ID: <20260102150032.53106-4-leon.hwang@linux.dev> In-Reply-To: <20260102150032.53106-1-leon.hwang@linux.dev> References: <20260102150032.53106-1-leon.hwang@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260102_070209_277067_29BB8008 X-CRM114-Status: GOOD ( 16.34 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Apply the same tail call optimization to arm64 as done for x86_64. When the prog array map is known at verification time (dyn_array=false): - Embed max_entries as an immediate value instead of loading from memory - Use the precomputed target from array->ptrs[max_entries + index] - Jump directly to the cached target without dereferencing prog->bpf_func When the map is dynamically determined (dyn_array=true): - Load max_entries from the array at runtime - Look up prog from array->ptrs[index] and compute the target address Implement bpf_arch_tail_call_prologue_offset() returning "PROLOGUE_OFFSET * 4" to convert the instruction count to bytes. Signed-off-by: Leon Hwang --- arch/arm64/net/bpf_jit_comp.c | 71 +++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 0c4d44bcfbf4..bcd890bff36a 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -620,8 +620,10 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf) return 0; } -static int emit_bpf_tail_call(struct jit_ctx *ctx) +static int emit_bpf_tail_call(struct jit_ctx *ctx, u32 map_index, bool dyn_array) { + struct bpf_map *map = ctx->prog->aux->used_maps[map_index]; + /* bpf_tail_call(void *prog_ctx, struct bpf_array *array, u64 index) */ const u8 r2 = bpf2a64[BPF_REG_2]; const u8 r3 = bpf2a64[BPF_REG_3]; @@ -638,9 +640,13 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx) /* if (index >= array->map.max_entries) * goto out; */ - off = offsetof(struct bpf_array, map.max_entries); - emit_a64_mov_i64(tmp, off, ctx); - emit(A64_LDR32(tmp, r2, tmp), ctx); + if (dyn_array) { + off = offsetof(struct bpf_array, map.max_entries); + emit_a64_mov_i64(tmp, off, ctx); + emit(A64_LDR32(tmp, r2, tmp), ctx); + } else { + emit_a64_mov_i64(tmp, map->max_entries, ctx); + } emit(A64_MOV(0, r3, r3), ctx); emit(A64_CMP(0, r3, tmp), ctx); branch1 = ctx->image + ctx->idx; @@ -659,15 +665,26 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx) /* (*tail_call_cnt_ptr)++; */ emit(A64_ADD_I(1, tcc, tcc, 1), ctx); - /* prog = array->ptrs[index]; - * if (prog == NULL) - * goto out; - */ - off = offsetof(struct bpf_array, ptrs); - emit_a64_mov_i64(tmp, off, ctx); - emit(A64_ADD(1, tmp, r2, tmp), ctx); - emit(A64_LSL(1, prg, r3, 3), ctx); - emit(A64_LDR64(prg, tmp, prg), ctx); + if (dyn_array) { + /* prog = array->ptrs[index]; + * if (prog == NULL) + * goto out; + */ + off = offsetof(struct bpf_array, ptrs); + emit_a64_mov_i64(tmp, off, ctx); + emit(A64_ADD(1, tmp, r2, tmp), ctx); + emit(A64_LSL(1, prg, r3, 3), ctx); + emit(A64_LDR64(prg, tmp, prg), ctx); + } else { + /* tgt = array->ptrs[max_entries + index]; + * if (tgt == 0) + * goto out; + */ + emit(A64_LSL(1, prg, r3, 3), ctx); + off = offsetof(struct bpf_array, ptrs) + map->max_entries * sizeof(void *); + emit_a64_add_i(1, prg, prg, tmp, off, ctx); + emit(A64_LDR64(prg, r2, prg), ctx); + } branch3 = ctx->image + ctx->idx; emit(A64_NOP, ctx); @@ -680,12 +697,17 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx) pop_callee_regs(ctx); - /* goto *(prog->bpf_func + prologue_offset); */ - off = offsetof(struct bpf_prog, bpf_func); - emit_a64_mov_i64(tmp, off, ctx); - emit(A64_LDR64(tmp, prg, tmp), ctx); - emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx); - emit(A64_BR(tmp), ctx); + if (dyn_array) { + /* goto *(prog->bpf_func + prologue_offset); */ + off = offsetof(struct bpf_prog, bpf_func); + emit_a64_mov_i64(tmp, off, ctx); + emit(A64_LDR64(tmp, prg, tmp), ctx); + emit(A64_ADD_I(1, tmp, tmp, sizeof(u32) * PROLOGUE_OFFSET), ctx); + emit(A64_BR(tmp), ctx); + } else { + /* goto *tgt; */ + emit(A64_BR(prg), ctx); + } if (ctx->image) { off = &ctx->image[ctx->idx] - branch1; @@ -701,6 +723,12 @@ static int emit_bpf_tail_call(struct jit_ctx *ctx) return 0; } +int bpf_arch_tail_call_prologue_offset(void) +{ + /* offset is in instructions, convert to bytes */ + return PROLOGUE_OFFSET * 4; +} + static int emit_atomic_ld_st(const struct bpf_insn *insn, struct jit_ctx *ctx) { const s32 imm = insn->imm; @@ -1617,7 +1645,10 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, } /* tail call */ case BPF_JMP | BPF_TAIL_CALL: - if (emit_bpf_tail_call(ctx)) + bool dynamic_array = (insn->imm >> 8) & 0xFF; + u32 map_index = insn->imm & 0xFF; + + if (emit_bpf_tail_call(ctx, map_index, dynamic_array)) return -EFAULT; break; /* function return */ -- 2.52.0