[PATCH bpf-next 3/9] uprobes/x86: Finish an unoptimization which failed half-way

From: Alexei Starovoitov

Date: Wed Oct 21 2026 - 21:59:31 EST


From: Alexei Starovoitov <ast@xxxxxxxxxx>

int3_update_unoptimize() restores optimized nop10 in three writes:

48 8d 64 24 80 e8 d0 d1 d2 d3 lea -0x80(%rsp),%rsp; call tramp
cc 8d 64 24 80 e8 d0 d1 d2 d3 1. trap new entries
cc 2e 0f 1f 84 e8 d0 d1 d2 d3 2. restore bytes 1..4
66 2e 0f 1f 84 e8 d0 d1 d2 d3 3. restore byte 0

uprobe_write() can fail with -ENOMEM, or with -EINTR when the task is being
killed. When the 2nd write fails the result of the 1st one stays. Byte 0 is
a breakpoint and it works. But the next set_orig_insn() doesn't find lea in
byte 0, takes it for a plain breakpoint and restores one byte:

66 8d 64 24 80 e8 d0 d1 d2 d3 lea -0x80(%rsp),%sp; call tramp

Nothing recognizes these bytes anymore. The process keeps the call into
uprobe trampoline, which can be unmapped already.

Teach is_optimized() to recognize the half-way state, and let the 1st write
of int3_update_unoptimize() skip what is already done.

The failure wasn't reproduced, but the state can be made by hand. Attach to
nop10, call it to get it optimized, write 0xcc over byte 0, detach.

"uprobes: Don't sleep on mmap_lock under dup_mmap_sem" needs this. There
fork() removes the breakpoint from its mm. When fork() is killed in
the middle register_for_each_vma() removes it for the second time.

Fixes: 554ba38456da ("uprobes/x86: Move optimized uprobe from nop5 to nop10")
Signed-off-by: Alexei Starovoitov <ast@xxxxxxxxxx>
---
arch/x86/kernel/uprobes.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/arch/x86/kernel/uprobes.c b/arch/x86/kernel/uprobes.c
index 65a2de82ecd2..62bbe41c6466 100644
--- a/arch/x86/kernel/uprobes.c
+++ b/arch/x86/kernel/uprobes.c
@@ -896,6 +896,9 @@ static int verify_insn(struct page *page, unsigned long vaddr, uprobe_opcode_t *
case EXPECT_OPTIMIZED:
if (is_opt_insns(&old_opcode[0]))
return 1;
+ /* An earlier attempt got this far and failed, nothing to write. */
+ if (is_swbp_opt_insns(&old_opcode[0]))
+ return 0;
break;
case EXPECT_SWBP_OPTIMIZED:
if (is_swbp_opt_insns(&old_opcode[0]))
@@ -1109,6 +1112,17 @@ static bool __is_optimized(struct mm_struct *mm, uprobe_opcode_t *insn, unsigned
s32 raddr;
} *call = (struct __arch_relative_insn *)(insn + LEA_INSN_SIZE);

+ /*
+ * INT3 followed by the rest of the optimized instructions is what is
+ * left if int3_update_optimize() or int3_update_unoptimize() failed
+ * half-way. It works as a breakpoint, but to restore the original
+ * instruction it has to be unoptimized like a completely optimized one.
+ * The same has to be done if a debugger has put its INT3 on top of an
+ * optimized uprobe. The trampoline can be gone already, so do not look
+ * at the target of the call.
+ */
+ if (is_swbp_opt_insns(insn))
+ return true;
if (!is_opt_insns(insn))
return false;
return __in_uprobe_trampoline(mm, vaddr + OPT_INSN_SIZE + call->raddr);
--
2.55.0