Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.68 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100170; b=qHXcp8b10MvQeKdKDi/fAesgymUCdgHtsstf+zCzCj4rbrICEyENU1Hl1Ck9wx3Q/6VO RpINmv9AAO3bxYlSvumPY1e0Gf7tQt+S0t6MKeLRvq8uD+hxEHP/xeAdUlSmpG+F7fyy3 PdHsDsVWvTgCn0Gw8+9IJ0Aro9dYrqspMRxfw/SrMJamzpTt6dg8Sk29KD1uBzMm/sIcR elLXy4tvcSdSgibvUzZEROxE/LlrQB9BafR4Y8yChMDhoALJxHmSdtGQzAmb47CbuExtn V97jElrUmXb0UhHYH7vZwEgzfNDDXA5bvXAf04t5qfV73gX/+mwUS4PoKGDDVtxJXfg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100170; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: X-Developer-Signature:X-Developer-Key:Content-Transfer-Encoding; bh=D+u3bTeyFmwgQTE3KDLOhTgatxBoD5ehrSl0FZKXqck=; b=pM8bF1ycqlcHNNalawvhZvyZklxYTVZnlo2+aa03OlD+lfGplzhFKlYs2gvXGTL+XGMT kX4MQQW+Fom0KUa1zUh5aWSz6RBAjeGPb6azIXgUvSzJ5fc8XsleWS3fSjfNLkr16oINx A2/NSqz821jGcir0i9Q5iLSn6JXM8tp2FxQiLCzB1HM45MlgYVRgUH7Zs8gMVai4RQ0/4 kU8XHhsC/lkBYIHF/ab1YoA/0ZLn9ThL1DKHc2wKNSBQNu6NaPYp/dIN7bTndGTJ5Ql6H +QDsbOSplEJzQvoEYcikTO74Usnv/8eeT2A+MZkY91Yjqsdk2O24VjqmV5YsGqqIpuQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=WRRHVi17; arc=none smtp.remote-ip=209.85.210.68 Received: from mail-ot1-f68.google.com (mail-ot1-f68.google.com [209.85.210.68]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7EC1606A31A for ; Tue, 1 Dec 2026 04:42:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D7EC1606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=WRRHVi17 Received: by mail-ot1-f68.google.com with SMTP id 46e09a7af769-86156cf8a7eso8707862a34.1 for ; Mon, 30 Nov 2026 20:42:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100169; x=1796704969; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=D+u3bTeyFmwgQTE3KDLOhTgatxBoD5ehrSl0FZKXqck=; b=WRRHVi17g9mQx0AGsuqkyJXbRnz/iZgmWI7zDoMR8z7CNOX/FFaOeBmqXciS/tdMtP Cz+xrub2bhRhMrMPhcqpgWwqF0wOH2HwvCuGK08eKKilbVbOVCzxaPjU5AC5+8Yp2c9x VLobUy4Syawo6hxoPPuHiVK74FfNdLeb0saDtCDU8Rh1Z67D/OX3Z9vDTEprWz/v54vu z4w5QzfkwWZt8qvRBAbmbYHB7ls56N/n0bXVPakRTF0uJ/cSR9UWUuUC1BJTmDiLMyW/ lXrTQA8ioSyZqMTtLnFM7a+sH17wAY8YXejmzlqu0Zge63vnBzX8RqerbwS3qsWyH4jF y2KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100169; x=1796704969; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=D+u3bTeyFmwgQTE3KDLOhTgatxBoD5ehrSl0FZKXqck=; b=sKyMGEoxbFwK3iKXAE7QHqHXu3+/wRUJ/p4s+Pkg89IHG7G08kDYLaTHOC/dNgwmTi HsxAoexh+8NK/W/l7CU0YrfKTxNosmfsBkc5K9SNuECVq3Yy2gvjO7Evd+QdfQKsaAuA y7NHDmEuGLZ5Mdu4O4GgC9tqkZGcsxzUPq9cPM9iR47g+NXV/XE+Bq/dWBJi+E7J2zk9 eoB95rOYRi5y6KCXTGdOrgYBxunu/dfxLQF9QpeSSBhuavVLPg5JGP2NHYamPodxea76 2Nt9vo9pjNdzA1yYA7G/YDNLn04DOOFDyTMhubBkH1HbZSB15OvnzC0fVKVfXnPhSLHI +nfQ== X-Gm-Message-State: APuU4Z867OClSjfKq4aDTGeXYisT9SJG2CuzU065Q05l1KoZBUuHl7ct P4Gj3nyoYEbn98ewx9bjjW1cyV33ZKhqizfY3GDHjXG+y5FdqKLO8+xlwaXiUMFrPKM= X-Gm-Gg: AZFdTZeNpydy+mervSbE3Y7y/IX50h6riaYWcZHe6ZNiPaydpRZdc6vSaWCAd9O992Q 2LO0abtp0B4Rs5vzAv9mXWmV3V9motDdlAPlwKvkcRN9STsTJEGStdPIe7nUa0xD811CuXrqXlA XS0hibpEPHl2wrxfjft5TiEdr2VC9g2Wy8J/zWwIQ2PKLxB+RFCCQLTTC//3JZRPHm9D+vJkM5y U4q/Q8mPqcDT/5bO+lVv0SQ5c88JUS1lMCkyUOHT29qF2Vli5aLi7XeblCz0uIBizTOK1tXJC5s yfSY1jPgOCvXZvNBT7Z936diU7gOv9FyrQ5WxctO6y0EwntbJ8i/LkBtv1/YHxrVM0tTh4QSrTs UFCC0LRvhbDVwg7EnvnaJHZAymorlU668xBwhEY5lXbwV9jWQbCQyDbqbBqjiWOHuXuiND0PonU 1WczuSztQBySTcIg6RvvRZhzBgsKu5xp3kLrZIoGwowV0ENeGTCUDl6mjjo1Cnx5hXY5iT+P1se RJL8nfvtvMZx4Up9kpeidoI22/etx2OGX+n4iP680u9CixI3RZoLrxN/vwDH+oHfcn5tviCJ/0e Ph+2WRW9RDCyFmtawNqxPsRwpZqU8jEs+poVob8FHjRXuo0+VQcCQvCWN74Brm9Z1mV9w+nUl2e +oq3nYznm9geFNVAywkMOQJysGlnVv0r7oYUu X-Received: by 2002:a05:6830:4c0b:b0:839:c62a:1661 with SMTP id 46e09a7af769-863091a9a86mr27233169a34.7.1796100168763; Mon, 30 Nov 2026 20:42:48 -0800 (PST) Received: from localhost ([2a03:2880:10ff:48::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863c11a7b3esm3280685a34.13.2026.11.30.20.42.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:42:47 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Emil Tsalapatis , Ihor Solodrai , kkd@meta.com, kernel-team@meta.com Subject: [PATCH bpf-next v1 54/73] bpf, x86: Put the landing of unwind_goto after the function Date: Tue, 1 Dec 2026 05:39:21 +0100 Message-ID: <3ff89e4cf0fd193cf30bbd455cf9567484c8f70e.1796099738.git.memxor@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=9383; h=from:subject:message-id; bh=M9eRSi4op/4Z+dJNefN4aPuEdhHuRfPqPPtU0oX33NU=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb5FR4YftOodrnDmn/+b7dfWXUe+ihUeWRLNmPBQq1/mx f+a6jlIWBjEuBlkxRZaS//uYjE9U/g60XcYNM4eVCWQIAxenAEyk2YKRgcG/bOpuJmuH+ZkFL4S5An 9NYPnE1Xf0SqOq8IJv/7Yc/cHIcLpi0unrHSe1Msptrx56aNksxXH60IbPiXffuv70WH/enhcA X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit Currently the x86-64 JIT emits the landing of an unwind_goto right after the call, and every call that returns jumps over it. With a private stack the code is: call foo pop r9 <- exit returns here jmp 1f pop r9 <- unwind_exit returns here jmp 1: So each call with a landing pays a jump, for code that runs only when the callee unwinds. A cancel_goto is going to get a landing too, and the verifier is going to add a cancel_goto after every call of a function that the kernel can cancel. Emit the landings after the last insn of the function instead, as the arm64 JIT does: call foo pop r9 <- exit returns here ... leave; ret pop r9 <- unwind_exit returns here jmp An unwind_goto now only records the bytes that follow the native call and its target, and do_jit() emits the recorded landings after the loop over the insns. An unwind_goto with nothing to redo has no landing, as before. The keys of the exception table entries do not change. Three points need care: - The landings have the same size in every pass of the JIT, because their jumps always have a 32 bit offset. So the image stops shrinking exactly when the body does. - The exception table entries are written only when the image is allocated. The landings then start at addrs[insn_cnt], where the pass before ended the body. - The length that do_jit() returns includes the landings, so jited_len covers them. A landing is entered by ret and left by a direct jump, so it needs no ENDBR. private_stack_unwinds of verifier_unwind.c expects the new layout. Signed-off-by: Kumar Kartikeya Dwivedi --- arch/x86/net/bpf_jit_comp.c | 101 ++++++++++++++---- .../selftests/bpf/progs/verifier_unwind.c | 7 +- 2 files changed, 87 insertions(+), 21 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 3a0143c4d212..19fb1b20c51d 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -288,6 +288,18 @@ int bpf_arch_text_invalidate(void *dst, size_t len) return IS_ERR_OR_NULL(text_poke_set(dst, 0xcc, len)); } +/* + * A LANDING: where unwind_exit of a callee comes back to when the call has code + * behind its native call, like 'pop r9'. That code, done once more, and a jump + * to the insn that unwind_goto goes to. The landings stand behind the body of + * the function, see do_jit(). + */ +struct jit_landing { + u8 code[16]; + u8 len; + int to; +}; + struct jit_context { int cleanup_addr; /* Epilogue code offset */ @@ -298,6 +310,14 @@ struct jit_context { */ int tail_call_direct_label; int tail_call_indirect_label; + + /* + * Room for one landing per unwind_goto of the prog. Each pass fills it + * again. It lives as long as addrs[]: the extra pass of a prog with + * subprogs has it from the first call of bpf_int_jit_compile(). + */ + struct jit_landing *landings; + int max_landings; }; /* Maximum number of bytes emitted while JITing one eBPF insn */ @@ -2452,6 +2472,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * /* the bytes emitted after the native call of the last call insn */ u8 after_call[16], *ret_addr; int after_call_len = 0; + /* the number of landings in this pass, and their size in bytes so far */ + int nr_landings = 0, tail = 0; int err; stack_depth = bpf_prog->aux->stack_depth; @@ -3459,38 +3481,47 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * /* unwind_goto. The verifier expanded may_goto. */ case BPF_JMP | BPF_JCOND: - case BPF_JMP32 | BPF_JCOND: + case BPF_JMP32 | BPF_JCOND: { + int to = i + bpf_jmp_offset(insn); + int target = addrs[to]; + struct jit_landing *landing; + /* an access to arena is in front: no call, nothing to redo */ if (BPF_CLASS(insn[-1].code) != BPF_JMP) after_call_len = 0; - if (!after_call_len) { - err = add_unwind_exentry(bpf_prog, image, rw_image, &excnt, - addrs[i - 1], - addrs[i + bpf_jmp_offset(insn)]); - if (err) - return err; - break; - } /* * What follows the native call, like 'pop r9', has to be - * done on the way to the pad too: + * done on the way to the pad too. Not in the way of what + * returns: the landing goes behind the body of the + * function, and the next insn follows the call. * call foo * pop r9 <- exit returns here - * jmp 1f + * ... + * leave; ret + * ... * pop r9 <- unwind_exit returns here * jmp pad - * 1: + * addrs[insn_cnt] is where the body ended in the pass before. + * The entry is written when no insn moves any more. */ + if (after_call_len) { + if (nr_landings >= ctx->max_landings) { + pr_err("bpf_jit: landings bug\n"); + return -EFAULT; + } + landing = &ctx->landings[nr_landings++]; + memcpy(landing->code, after_call, after_call_len); + landing->len = after_call_len; + landing->to = to; + target = addrs[insn_cnt] + tail; + tail += after_call_len + 5; + } err = add_unwind_exentry(bpf_prog, image, rw_image, &excnt, - addrs[i - 1] - after_call_len, addrs[i - 1] + 2); + addrs[i - 1] - after_call_len, target); if (err) return err; - EMIT2(0xEB, after_call_len + 5); - memcpy(prog, after_call, after_call_len); - prog += after_call_len; - EMIT1_off32(0xE9, addrs[i + bpf_jmp_offset(insn)] - - (addrs[i - 1] + 2 + after_call_len + 5)); break; + } case BPF_JMP | BPF_TAIL_CALL: if (imm32) @@ -3825,6 +3856,30 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * prog = temp; } + /* + * Emit the landings after the last insn. Their jumps always have a 32 + * bit offset, so they have the same size in every pass and the image + * stops shrinking when the body does. addrs[insn_cnt] stays the end of + * the body, and the returned length includes the landings. + */ + for (i = 0; i < nr_landings; i++) { + const struct jit_landing *landing = &ctx->landings[i]; + + memcpy(prog, landing->code, landing->len); + prog += landing->len; + EMIT1_off32(0xE9, addrs[landing->to] - (proglen + landing->len + 5)); + ilen = prog - temp; + if (image) { + if (unlikely(proglen + ilen > oldproglen)) { + pr_err("bpf_jit: fatal error\n"); + return -EFAULT; + } + memcpy(rw_image + proglen, temp, ilen); + } + proglen += ilen; + prog = temp; + } + if (image && excnt != bpf_prog->aux->num_exentries) { pr_err("extable is not populated\n"); return -EFAULT; @@ -4887,6 +4942,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr addrs = kvmalloc_objs(*addrs, prog->len + 1); if (!addrs) goto out_addrs; + for (i = 0; i < prog->len; i++) + ctx.max_landings += bpf_is_unwind_goto(&prog->insnsi[i]); + if (ctx.max_landings) { + ctx.landings = kvmalloc_objs(*ctx.landings, ctx.max_landings); + if (!ctx.landings) + goto out_addrs; + } /* * Before first pass, make a rough estimation of addrs[] @@ -4899,7 +4961,6 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr } ctx.cleanup_addr = proglen; skip_init_addrs: - /* * Jumps are sized by the insns they jump over, which start at * their maximum size, so no insn grows from one pass to the @@ -5019,6 +5080,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr bpf_prog_fill_jited_linfo(prog, addrs + 1); out_addrs: kvfree(addrs); + kvfree(ctx.landings); if (!image && priv_stack_ptr) { free_percpu(priv_stack_ptr); prog->aux->priv_stack_ptr = NULL; @@ -5123,6 +5185,7 @@ void bpf_jit_free(struct bpf_prog *prog) bpf_jit_binary_pack_finalize(jit_data->header, jit_data->rw_header); kvfree(jit_data->addrs); + kvfree(jit_data->ctx.landings); kfree(jit_data); } prog->bpf_func = (void *)prog->bpf_func - cfi_get_offset(); diff --git a/tools/testing/selftests/bpf/progs/verifier_unwind.c b/tools/testing/selftests/bpf/progs/verifier_unwind.c index b4d7dfdc675b..64fa6e1d3a32 100644 --- a/tools/testing/selftests/bpf/progs/verifier_unwind.c +++ b/tools/testing/selftests/bpf/progs/verifier_unwind.c @@ -1198,7 +1198,9 @@ static unsigned long may_unwind_big_stack(void) /* * x86-64 keeps the pointer to the private stack in r9 and saves it around a call. - * The value that is read back in the pad is there only when r9 is restored. + * The value that is read back in the pad is there only when r9 is restored: on the way of + * unwind_exit a landing behind the last insn of the prog does that. What returns goes on with + * the exit that follows the call. */ SEC("raw_tp") __description("unwind: private stack, the callee unwinds") @@ -1210,7 +1212,8 @@ __jited(" pushq %r9") __jited("...") /* call depth accounting */ __jited(" callq 0x{{.*}}") __jited(" popq %r9") -__jited(" jmp L{{.*}}") +__jited("L{{.*}}: leave") +__jited("...") __jited(" popq %r9") __jited(" jmp L{{.*}}") __arch_arm64 -- 2.53.1-Meta