Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.138 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100104; b=fr8qfQTVmMMJj7a8VzVx15hkodt24a5pJrkZTZwFUUggb4IszzB6Vago+jNydWRP3D/L 5ci7conXg/DNOfrGZ3WRRXmsGfLW7DukO2RKBP87LVtAS22ZyxhBgADnv3mF4jWIoTQOS 4Egm90QGDmFZ/2bB9zZpQkuOSTzqDZGkH2pL/7sN8jKH1Nlf7XxGcKlOSJGjdsUMxlu22 CRYDBpgVLAs819NXor4og0UYHZYjd39H7Ov5znQd6CBCkaVt7vAoaIXUmaFRHdKG9NfLQ E6aH4avpH9CACJhtqPl+TXJPGGfeu+3KGrDFqp9w9625nbWXCN/YfaFv3cnkoJGQ6fQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100104; 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=XZD4I1JSRSqGlyQAlf7Rahu7+X+RpZ80Y9IIsGuJmOs=; b=OlK4/RNAK0IiAypkRr+88CvU5xRud11kNqbtkSQhl+3Wsp9EoAgR6yDbnre1gcxKCTJ/ 1j0jwx86WWk07Shsz19K2RYf2LX/tiezeNqrigunhmeSVUVaXmUCJjoEsmxMba3+ffN9M Z5XARJG+X+ZMsPa7KBk245HyZKVrHeckUCqoN0tsZrOTq8u5z2SlCQzk8YtfyOG+Gun7P vV0BS1VBgeQibbOS1QxYgJOmQdswVr8Ybho9mSvpHmH42fgdPR5HOUQnlrKIaODc3ZOGS 2PbAJUnD0+nBaa2sfqaKlEUtYQ7lP/gl1MTZ/GP+XVGXjzC6OL3QI1fkyXS+2Fy/YOg== 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=R4tAEP4K; arc=none smtp.remote-ip=74.125.231.138 Received: from mail-oo2-f10.google.com (mail-oo2-f10.google.com [74.125.231.138]) (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 3C5DC606A31A for ; Tue, 1 Dec 2026 04:41:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3C5DC606A31A 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=R4tAEP4K Received: by mail-oo2-f10.google.com with SMTP id 006d021491bc7-71ad9f62e49so1295924eaf.0 for ; Mon, 30 Nov 2026 20:41:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796100103; x=1796704903; 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=XZD4I1JSRSqGlyQAlf7Rahu7+X+RpZ80Y9IIsGuJmOs=; b=R4tAEP4K8NposN+C3uzSr8U6qk9kRg51BRgESslcyQfkoAoqkWd39/rfSxafZkEXMx J0XmSk0PGs1IwUo+qbrJozwtojpRTyCVXz78qgslfuPah8ucvYqByhlQm1eWbJqf8fl5 NXnC9b06JLUTbAEW2uNNPRYqIoFJN3yDtaR8TCIt+QBrRsBkrNy1Fid09BUgSd8mIUXh ecxAzZL2qBIv9cSgkUv5Bhz+zlEAYjSVOWGk1nOF5IA+/BIIl6rlY5jlTHerwzjTQA/V v2LGNlc4+6fn511q51P5nx3iSZY6elnsfja7MgvGSs5WTiP9beX5cCbcDPwFT3rwERFB l/PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100103; x=1796704903; 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=XZD4I1JSRSqGlyQAlf7Rahu7+X+RpZ80Y9IIsGuJmOs=; b=CMr9SoZ3XxSavPuB4FRoGk2DbJHY/1B5Yhim+/B4xO4fCAWBhclIOFzqscQzJd0VC3 VarRqMrgjKYWwY7Mh3S5PHbo8RZHRaPs2r2qtLwGfQb+ju6lyoQP7CDYmE1RpfK8APdG VL4R+adXWs7jyO3IoARisOfvlJ6ACEuoGGPxklLPOoF5J+Y/qFEBdSh6DRpzdHvcopEu OC1jh/cfZRrApTxB0fBFc9zWUYwR4W3koNRmZu34xLxV+Ek8H3LIG98obW+RTHWRzllE AHaViCj+6itzSy9vdOnD5qlxId46NGp/tVMMpzqcvh2ihNPYxbZMYm8BL6KIFS75sYmd pOYQ== X-Gm-Message-State: APuU4Z/bZCvQ6l3QgkHYd9jKmdgnbeaJNn0PwXHmFRTR45JC6m3WvPWP ck0vaT/L0jpOVQuoo4uYXaKr8iloxLkYUcnSpCmq9LLwRUO6bkuZvMPElnbEVBz8dRo= X-Gm-Gg: AZFdTZdEVurHhPigMpOn/tSQoaxzZqDU0doRDzqLmGVGIdc2H/GNxo+0WLZhVPhr/Ze Wv1LEwHaSo5lMZ5nr1hWaGxKLumzqcOosUEJ0xaFoVFQdDMF7ZKObSmJnOWHcLAE4hX3CQXZ1gF OUi1uyIEm2GRJpmsOk1ZCZjnkQTKiTf2t1d3s07WHt677jwnvVXB3q2BB95L12sEE//I6yINhr5 e5PE7i+TVsJ/K3QYEdWeRVoKrZ4OBIfgmMPDUlzOLact7kz/kQl3MhNNHyN1tjXZ4bATRZiwZ8z pfT1IXMIDuWoo2i7xx/ltGfnaELf2JlLiT2055W5c4e4XpQbp7l/ee/7lPcDuR2dWLn58GR2Gak 0pCQ0ScymhcTLJG4ygVnkBui7cdxWXW2rKtpqbXrMTpvNf3jxRslwTIHsWLsMrNSS4sLAagfdLc 4NQgVHcHCWyoIauPfhLqUALRR8oeuZ4ngdI73HqueCEIS8CldUlrB/2GXpC62doza/IQIAaWzPl U1yKIb2hV7N7pFJX92+2055KJsx0qbG8mksMKlJnWqwpoE4ovlMwsz6l3pVgYHqyTxSvvcM/cHr MtD87Z7INHjPW1sutIMGQfXtQK0bRLnR7/8ez8QzuV+bukhZigvyFEVv6CXy9xlG5jK3XnFadX0 K3//vUXqgP60Z70KYhqBVE85955XsJBbHHkhTQzS23+w+jarkgiDLbRcIJQ== X-Received: by 2002:a05:6820:571c:10b0:6cd:3fdc:a932 with SMTP id 006d021491bc7-71b3958daaamr20172618eaf.79.1796100103140; Mon, 30 Nov 2026 20:41:43 -0800 (PST) Received: from localhost ([2a03:2880:10ff:5c::]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-71b3806ef20sm24286029eaf.7.2026.11.30.20.41.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:41:41 -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 33/73] bpf, x86: Go to the landing pad when an access to arena faults Date: Tue, 1 Dec 2026 05:39:00 +0100 Message-ID: <73c248bdef0ac4f7ffadffc76449d8caeb915bd7.1796099737.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=19139; h=from:subject:message-id; bh=MNn5STQ4QjYxsGRhM8cljJ2jWvIuz68/XGV225HCzrI=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZvPb/7t6rV6c//J+k3qeG6g/km2l89UKpD1lHOE3ZYtjLpn Oo06SlkYxLgYZMUUWUr+72MyPlH5O9B2GTfMHFYmkCEMXJwCMJHvYQz/rCYtKu/ftOe9q89SdbkT/Z eXV3GW7NnssuuVv8UmtYRuY4b/FaI3GnsWmBhf+DEveD6fNT+v3/yyhfP3bnwkHXKBl+E/KwA= X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit Currently ex_handler_bpf() skips an access to arena that faults. It reports the fault to the stream of the program, clears the destination register of a load and resumes after the access. Add what the x86-64 JIT and the fault handler need to cancel the program there instead. The verifier does not add unwind_goto after an access yet, so no program changes. An access to arena already has an exception table entry, which tells the handler how to skip it. unwind_goto after the access is a second entry and no code. Its key is the last byte of the native code of the access, E - 1, as after a call it is the return address minus one: r1 = *(u64 *)(r7 + 8) mov rdi, [r13 + r12 + 8] <- S unwind_goto pad no code <- E the table entry of the access { S, E - S, rdi, r13, 8 } the table entry of unwind_goto { E - 1, pad - E } At a fault ex_handler_bpf() calls the new bpf_arena_fault(), which reports the fault as before and returns where the program continues. If E - 1 has an entry of unwind_goto, that is the landing pad: R0-R9 stay as they are, and the report ends with ". The program is unwound". Otherwise it is E, and the access is skipped as today. bpf_arena_fault() looks the entry up with the new bpf_fault_unwind_target(). bpf_unwind_target() does not fit, because it warns when it finds no entry, and after an access a missing entry is no bug. The generic version returns E, so arm64 keeps skipping until its JIT gets the same. Two cases need care: - The landing pad has to get the registers as they were before the access. An access is one native insn, which writes nothing when it faults. A fetching AND, OR or XOR is the exception. It is a cmpxchg loop that keeps R0 in r10 (BPF_REG_AX) while rax serves the cmpxchg, and its last insn puts R0 back. Today the handler resumes at that insn. Now both table entries of the loop give its end as E, and a new bit in them, R0_IN_AX, tells the handler to copy r10 to rax itself. - After a call the JIT may emit a landing for unwind_goto, which redoes what follows the native call. After an access there is nothing to redo, so unwind_goto is the exception table entry alone. Co-developed-by: Alexei Starovoitov Signed-off-by: Alexei Starovoitov Signed-off-by: Kumar Kartikeya Dwivedi --- arch/arm64/net/bpf_jit_comp.c | 2 +- arch/x86/net/bpf_jit_comp.c | 97 +++++++++++++++++++++-------------- include/linux/bpf.h | 8 +-- include/linux/filter.h | 1 + kernel/bpf/arena.c | 25 ++++++--- kernel/bpf/core.c | 10 ++++ 6 files changed, 92 insertions(+), 51 deletions(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 00667ce5303c..d1bf402bc42b 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -1595,7 +1595,7 @@ bool ex_handler_bpf(const struct exception_table_entry *ex, if (is_arena) { addr = regs->regs[arena_reg] + off; - bpf_prog_report_arena_violation(is_write, addr, regs->pc); + bpf_arena_fault(is_write, addr, regs->pc, regs->pc + AARCH64_INSN_SIZE); } if (dst_reg != DONT_CLEAR) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 4d4dd004df20..0f35aed5df7e 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -1815,14 +1815,12 @@ static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size, * in @dst_reg and @src_reg. * * Both the load and the CMPXCHG can fault when accessing the arena, so their - * addresses are handed back in @fault[0] and @fault[1]. A fault has to resume - * at @resume, which is past the loop and past the move that delivers the old - * value, but before R0 is restored. Both are NULL for the non-arena case, - * which needs no fixup. + * addresses are handed back in @fault[0] and @fault[1]. @fault is NULL for the + * non-arena case, which needs no fixup. At a fault ex_handler_bpf() skips the + * rest of the sequence and restores R0 itself, see FIXUP_ARENA_R0_IN_AX. */ static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg, - u32 src_reg, int index_reg, - u8 **fault, u8 **resume) + u32 src_reg, int index_reg, u8 **fault) { bool is64 = BPF_SIZE(insn->code) == BPF_DW; u8 *branch_target, *prog = *pprog; @@ -1867,9 +1865,6 @@ static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg, /* Return the pre-modification value */ emit_mov_reg(&prog, is64, src_reg, BPF_REG_0); - if (resume) - *resume = prog; - /* Restore R0 after clobbering RAX */ emit_mov_reg(&prog, true, BPF_REG_0, BPF_REG_AX); @@ -1884,18 +1879,18 @@ static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg, * * Bit layout of `fixup` (32-bit): * - * +-----------+-------------+--------+-----------+---------+----------+ - * | 31 | 30 | 29-24 | 23-16 | 15-8 | 7-0 | - * | | | | | | | - * | ARENA_ACC | ARENA_WRITE | Unused | ARENA_REG | DST_REG | INSN_LEN | - * +-----------+-------------+--------+-----------+---------+----------+ + * +-----------+-------------+----------+--------+-----------+---------+----------+ + * | 31 | 30 | 29 | 28-24 | 23-16 | 15-8 | 7-0 | + * | | | | | | | | + * | ARENA_ACC | ARENA_WRITE | R0_IN_AX | Unused | ARENA_REG | DST_REG | INSN_LEN | + * +-----------+-------------+----------+--------+-----------+---------+----------+ * - * - INSN_LEN (8 bits): How far past the faulting insn to resume. That is its own length - * for a single-insn access, but the distance to the end of the whole - * sequence where one BPF insn became several, as for the CMPXCHG loop - * of a fetching AND/OR/XOR. A sequence long enough to overflow this - * field would first have to exceed BPF_MAX_INSN_SIZE, which do_jit() - * rejects with -EFAULT before the image is used. + * - INSN_LEN (8 bits): How far the end of the code of the BPF insn is from the faulting insn. + * That is its own length for a single-insn access, but the distance to + * the end of the whole sequence where one BPF insn became several, as for + * the CMPXCHG loop of a fetching AND/OR/XOR. A sequence long enough to + * overflow this field would first have to exceed BPF_MAX_INSN_SIZE, which + * do_jit() rejects with -EFAULT before the image is used. * - DST_REG (8 bits): Offset of dst_reg from reg2pt_regs[] (max offset = 112 (fits in 8 bits)). * This is set to DONT_CLEAR if the insn does not read into a register. * - ARENA_REG (8 bits): Offset of the register that is used to calculate the @@ -1904,6 +1899,9 @@ static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg, * It is independent of DST_REG, since a read-modify-write both writes to * memory and reads the old value into a register. * - ARENA_ACCESS (1 bit): This bit is set when the faulting instruction accessed the arena region. + * - R0_IN_AX (1 bit): R0 is in BPF_REG_AX while the faulting insn runs: the CMPXCHG loop of a + * fetching AND/OR/XOR keeps it there while RAX serves the CMPXCHG. The + * handler puts it back, since it leaves the sequence. * * Bit layout of `data` (32-bit): * @@ -1921,6 +1919,7 @@ static int emit_atomic_fetch_rmw(u8 **pprog, struct bpf_insn *insn, u32 dst_reg, #define FIXUP_INSN_LEN_MASK GENMASK(7, 0) #define FIXUP_REG_MASK GENMASK(15, 8) #define FIXUP_ARENA_REG_MASK GENMASK(23, 16) +#define FIXUP_ARENA_R0_IN_AX BIT(29) #define FIXUP_ARENA_WRITE BIT(30) #define FIXUP_ARENA_ACCESS BIT(31) #define DATA_ARENA_OFFSET_MASK GENMASK(31, 16) @@ -1931,6 +1930,7 @@ bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs) u32 insn_len = FIELD_GET(FIXUP_INSN_LEN_MASK, x->fixup); bool is_arena = !!(x->fixup & FIXUP_ARENA_ACCESS); bool is_write = !!(x->fixup & FIXUP_ARENA_WRITE); + unsigned long end = regs->ip + insn_len, pad = end; unsigned long addr; s16 off; u32 arena_reg; @@ -1939,13 +1939,18 @@ bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs) arena_reg = FIELD_GET(FIXUP_ARENA_REG_MASK, x->fixup); off = FIELD_GET(DATA_ARENA_OFFSET_MASK, x->data); addr = *(unsigned long *)((void *)regs + arena_reg) + off; - bpf_prog_report_arena_violation(is_write, addr, regs->ip); + pad = bpf_arena_fault(is_write, addr, regs->ip, end); } - /* jump over the faulting access, or the sequence it is part of, and clear dest register */ - if (reg != DONT_CLEAR) + /* + * Without a landing pad, jump over the faulting access, or the sequence it is part of, and + * clear dest register. A landing pad gets the registers as they were before the access. + */ + if (pad == end && reg != DONT_CLEAR) *(unsigned long *)((void *)regs + reg) = 0; - regs->ip += insn_len; + if (x->fixup & FIXUP_ARENA_R0_IN_AX) + regs->ax = regs->r10; + regs->ip = pad; return true; } @@ -1954,11 +1959,11 @@ bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs) * Record an arena access that may fault. @fault_ip is the address of the * faulting insn in the RO image, @resume_off how far past it execution has to * resume: for a multi-insn lowering that is the end of the whole sequence, not - * the end of the one insn. + * the end of the one insn. @flags are the FIXUP_ARENA_* bits of the entry. */ static int emit_arena_exentry(struct bpf_prog *bpf_prog, u8 *image, u8 *rw_image, int *excnt, u8 *fault_ip, u32 resume_off, - u32 fixup_reg, u32 arena_reg, bool is_write, s16 off) + u32 fixup_reg, u32 arena_reg, u32 flags, s16 off) { struct exception_table_entry *ex; s64 delta; @@ -1981,9 +1986,7 @@ static int emit_arena_exentry(struct bpf_prog *bpf_prog, u8 *image, u8 *rw_image ex->fixup = FIELD_PREP(FIXUP_INSN_LEN_MASK, resume_off) | FIELD_PREP(FIXUP_ARENA_REG_MASK, arena_reg) | FIELD_PREP(FIXUP_REG_MASK, fixup_reg) | - FIXUP_ARENA_ACCESS; - if (is_write) - ex->fixup |= FIXUP_ARENA_WRITE; + FIXUP_ARENA_ACCESS | flags; return 0; } @@ -2409,6 +2412,18 @@ u64 bpf_unwind_target(u64 ret_addr) return ret_addr; } +/* + * Return where a program continues after a fault in an access to arena whose + * code ends at @end: at the target of the unwind_goto after the access, or at + * @end when none follows it. A missing entry is not a bug here. + */ +u64 bpf_fault_unwind_target(u64 end) +{ + const struct exception_table_entry *e = search_bpf_extables(end - 1); + + return e ? end + e->fixup : end; +} + /* * unwind_exit: fix the return address of this frame. r0 and r2 are returned. * So is BPF_REG_ARENA_SP, when it's used. @@ -3121,8 +3136,9 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * err = emit_arena_exentry(bpf_prog, image, rw_image, &excnt, image + proglen + (start_of_ldx - temp), - prog - start_of_ldx, fixup_reg, - arena_reg, is_write, insn->off); + prog - start_of_ldx, fixup_reg, arena_reg, + is_write ? FIXUP_ARENA_WRITE : 0, + insn->off); if (err) return err; } @@ -3299,8 +3315,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * } if (is_atomic_fetch) { err = emit_atomic_fetch_rmw(&prog, insn, real_dst_reg, - real_src_reg, -1, - NULL, NULL); + real_src_reg, -1, NULL); if (err) return err; break; @@ -3328,7 +3343,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * case BPF_STX | BPF_PROBE_ATOMIC | BPF_DW: if (is_atomic_fetch_op(insn)) { u32 real_src_reg = src_reg, real_dst_reg = dst_reg; - u8 *fault[2], *resume; + u8 *fault[2]; int j; /* Will need RAX as a CMPXCHG operand so save R0 */ @@ -3339,25 +3354,26 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * real_dst_reg = BPF_REG_AX; err = emit_atomic_fetch_rmw(&prog, insn, real_dst_reg, - real_src_reg, X86_REG_R12, - fault, &resume); + real_src_reg, X86_REG_R12, fault); if (err) return err; /* * The page may be unmapped between the load and the * CMPXCHG, so both need an entry. Both report a write, - * since the BPF insn is a read-modify-write. + * since the BPF insn is a read-modify-write, and both + * say that R0 is in BPF_REG_AX. */ for (j = 0; j < 2; j++) { err = emit_arena_exentry(bpf_prog, image, rw_image, &excnt, image + proglen + (fault[j] - temp), - resume - fault[j], + prog - fault[j], reg2pt_regs[real_src_reg], reg2pt_regs[real_dst_reg], - true, insn->off); + FIXUP_ARENA_WRITE | + FIXUP_ARENA_R0_IN_AX, insn->off); if (err) return err; } @@ -3492,6 +3508,9 @@ 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: + /* 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], diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 915034e8acc2..f7ac105d9dd3 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3511,7 +3511,8 @@ void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data, enum bpf_dynptr_type type, u32 offset, u32 size); void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr); void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr); -void bpf_prog_report_arena_violation(bool write, unsigned long addr, unsigned long fault_ip); +unsigned long bpf_arena_fault(bool write, unsigned long addr, unsigned long fault_ip, + unsigned long end); static __always_inline u32 bpf_prog_run_array_sleepable(const struct bpf_prog_array *array, @@ -3856,9 +3857,10 @@ static inline void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr) { } -static inline void bpf_prog_report_arena_violation(bool write, unsigned long addr, - unsigned long fault_ip) +static inline unsigned long bpf_arena_fault(bool write, unsigned long addr, + unsigned long fault_ip, unsigned long end) { + return end; } #endif /* CONFIG_BPF_SYSCALL */ diff --git a/include/linux/filter.h b/include/linux/filter.h index 5e71c55f3307..3ce4609af05f 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1412,6 +1412,7 @@ bool bpf_jit_supports_kfunc_ret_reg_pair(void); bool bpf_jit_supports_unwind(void); bool bpf_jit_supports_hmul(void); u64 bpf_unwind_target(u64 ret_addr); +u64 bpf_fault_unwind_target(u64 end); bool bpf_jit_supports_stack_args(void); bool bpf_jit_supports_arena_args(void); bool bpf_jit_supports_far_kfunc_call(void); diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index 1c118f3da5a4..2890c65ad26a 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -1399,7 +1399,8 @@ static int __init kfunc_init(void) late_initcall(kfunc_init); static void __bpf_prog_report_arena_violation(struct bpf_prog *prog, bool write, - unsigned long addr, unsigned long fault_ip) + unsigned long addr, unsigned long fault_ip, + bool unwound) { struct bpf_stream_stage ss; u64 user_vm_start; @@ -1411,8 +1412,9 @@ static void __bpf_prog_report_arena_violation(struct bpf_prog *prog, bool write, addr += clear_lo32(user_vm_start); bpf_stream_stage(ss, prog, BPF_STDERR, ({ - bpf_stream_printk(ss, "ERROR: Arena %s access at unmapped address 0x%lx\n", - write ? "WRITE" : "READ", addr); + bpf_stream_printk(ss, "ERROR: Arena %s access at unmapped address 0x%lx%s\n", + write ? "WRITE" : "READ", addr, + unwound ? ". The program is unwound" : ""); bpf_stream_dump_stack(ss); })); } @@ -1450,7 +1452,7 @@ bool bpf_arena_handle_page_fault(unsigned long addr, bool is_write, unsigned lon if (prog && prog->aux->arena == arena) __bpf_prog_report_arena_violation(prog, is_write, - page_addr - kbase, fault_ip); + page_addr - kbase, fault_ip, false); else if (__ratelimit(&rs)) printk_deferred(KERN_ERR "BPF arena %u: %s fault at offset 0x%lx, IP %pS\n", arena->map.id, is_write ? "WRITE" : "READ", @@ -1461,8 +1463,15 @@ bool bpf_arena_handle_page_fault(unsigned long addr, bool is_write, unsigned lon return handled; } -void bpf_prog_report_arena_violation(bool write, unsigned long addr, unsigned long fault_ip) +/* + * JITed code faulted at @fault_ip, in an access to arena at @addr. The code of the BPF insn ends at + * @end. Report it. Returns where the program goes on: at the landing pad when unwind_goto follows + * the insn, else at @end: the access is skipped. + */ +unsigned long bpf_arena_fault(bool write, unsigned long addr, unsigned long fault_ip, + unsigned long end) { + unsigned long pad = bpf_fault_unwind_target(end); struct bpf_prog *prog; /* @@ -1473,7 +1482,7 @@ void bpf_prog_report_arena_violation(bool write, unsigned long addr, unsigned lo rcu_read_lock(); prog = bpf_prog_ksym_find(fault_ip); rcu_read_unlock(); - if (!prog) - return; - __bpf_prog_report_arena_violation(prog, write, addr, fault_ip); + if (prog) + __bpf_prog_report_arena_violation(prog, write, addr, fault_ip, pad != end); + return pad; } diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 737db347de97..0f7199c5c1b8 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -3845,6 +3845,16 @@ u64 __weak bpf_unwind_target(u64 ret_addr) return ret_addr; } +/* + * Where the program continues after a fault in an access to arena whose native code ends at @end: + * at the target of the unwind_goto after the access, or at @end, which skips the access. A JIT + * that cancels the program at a fault overrides this. + */ +u64 __weak bpf_fault_unwind_target(u64 end) +{ + return end; +} + /* * How this arch places a by-value kfunc argument, or NULL for one that has * not opted in and so only takes an argument of a single eightbyte, which -- 2.53.1-Meta