mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jinke Han <jinkehan@didiglobal.com>
To: Dave Hansen <dave.hansen@intel.com>
Cc: <linux-kernel@vger.kernel.org>, <tglx@kernel.org>,
	<mingo@redhat.com>, <bp@alien8.de>, <dave.hansen@linux.intel.com>,
	<x86@kernel.org>, <hpa@zytor.com>, <nik.borisov@suse.com>,
	<rppt@kernel.org>, <peterz@infradead.org>, <mhiramat@redhat.com>
Subject: Re: [PATCH] x86/kprobes: Fix boundary check in insn_jump_into_range()
Date: Fri, 20 Nov 2026 08:36:21 +0800	[thread overview]
Message-ID: <20261120003621.GA13347@didi-ThinkCentre-M920t-N000> (raw)
In-Reply-To: <728b7746-b64d-439a-9c1a-a828b56650b1@intel.com>

On 11/19/26 11:41, Dave Hansen wrote:
> What is the end user visible effect of this issue, and of fixing it?

There is no correctness impact: before this change the probe simply stays
unoptimized and falls back to the int3-based path, which is functionally
correct but pays trap overhead on every hit. So the user-visible effect is
limited to probe overhead, and only for probe points that hit this false
positive.

I do have a concrete in-tree example, on queued_spin_lock_slowpath+17.

Original code:

  crash> dis queued_spin_lock_slowpath
  0xffffffff823ebdf0 <queued_spin_lock_slowpath>: endbr64
  0xffffffff823ebdf4 <queued_spin_lock_slowpath+4>:       push   %r14
  0xffffffff823ebdf6 <queued_spin_lock_slowpath+6>:       push   %r13
  0xffffffff823ebdf8 <queued_spin_lock_slowpath+8>:       push   %r12
  0xffffffff823ebdfa <queued_spin_lock_slowpath+10>:      push   %rbp
  0xffffffff823ebdfb <queued_spin_lock_slowpath+11>:      mov    %rdi,%rbp
  0xffffffff823ebdfe <queued_spin_lock_slowpath+14>:      push   %rbx
  0xffffffff823ebdff <queued_spin_lock_slowpath+15>:      xchg   %ax,%ax
  0xffffffff823ebe01 <queued_spin_lock_slowpath+17>:      mov    $0x1,%edx
  0xffffffff823ebe06 <queued_spin_lock_slowpath+22>:      mov    0x0(%rbp),%eax
  0xffffffff823ebe09 <queued_spin_lock_slowpath+25>:      test   %eax,%eax
  0xffffffff823ebe0b <queued_spin_lock_slowpath+27>:      jne    0xffffffff823ebe7d <queued_spin_lock_slowpath+141>
  0xffffffff823ebe0d <queued_spin_lock_slowpath+29>:      lock cmpxchg %edx,0x0(%rbp)

Further down in the same function there is a back edge that jumps to +22:

  0xffffffff823ebe7f <queued_spin_lock_slowpath+143>:     jmp    0xffffffff823ebe06 <queued_spin_lock_slowpath+22>
  0xffffffff823ebe81 <queued_spin_lock_slowpath+145>:     mov    (%rdi),%esi
  0xffffffff823ebe83 <queued_spin_lock_slowpath+147>:     mov    $0x200,%eax
  0xffffffff823ebe88 <queued_spin_lock_slowpath+152>:     cmp    $0x100,%esi

With paddr = queued_spin_lock_slowpath+17, the probed instruction
"mov $0x1,%edx" is exactly 5 bytes, so copy_optimized_instructions()
copies exactly JMP32_INSN_SIZE bytes and the overwritten region is
[paddr, paddr+5) = [+17, +22). The back edge at +143 therefore targets
paddr + JMP32_INSN_SIZE, i.e. exactly the address the detour buffer
returns to after executing the copied instruction.

can_optimize() checks

  [paddr + INT3_INSN_SIZE, paddr + INT3_INSN_SIZE + DISP32_SIZE]

which is [+18, +22] with an inclusive upper bound, so the +143 jump was
counted as jumping into the optimized region and the probe was left
unoptimized:

  crash> dis queued_spin_lock_slowpath
  0xffffffff823ebdf0 <queued_spin_lock_slowpath>: endbr64
  0xffffffff823ebdf4 <queued_spin_lock_slowpath+4>:       push   %r14
  0xffffffff823ebdf6 <queued_spin_lock_slowpath+6>:       push   %r13
  0xffffffff823ebdf8 <queued_spin_lock_slowpath+8>:       push   %r12
  0xffffffff823ebdfa <queued_spin_lock_slowpath+10>:      push   %rbp
  0xffffffff823ebdfb <queued_spin_lock_slowpath+11>:      mov    %rdi,%rbp
  0xffffffff823ebdfe <queued_spin_lock_slowpath+14>:      push   %rbx
  0xffffffff823ebdff <queued_spin_lock_slowpath+15>:      xchg   %ax,%ax
  0xffffffff823ebe01 <queued_spin_lock_slowpath+17>:      int3
  0xffffffff823ebe02 <queued_spin_lock_slowpath+18>:      add    %eax,(%rax)
  0xffffffff823ebe04 <queued_spin_lock_slowpath+20>:      add    %al,(%rax)
  0xffffffff823ebe06 <queued_spin_lock_slowpath+22>:      mov    0x0(%rbp),%eax
  0xffffffff823ebe09 <queued_spin_lock_slowpath+25>:      test   %eax,%eax
  0xffffffff823ebe0b <queued_spin_lock_slowpath+27>:      jne    0xffffffff823ebe7d <queued_spin_lock_slowpath+141>
  0xffffffff823ebe0d <queued_spin_lock_slowpath+29>:      lock cmpxchg %edx,0x0(%rbp)

(The +18/+20 "add" instructions are just the remaining 4 bytes of the
5-byte "mov $0x1,%edx" being misdecoded after the int3 overwrite.)

With the fix the checked range becomes half-open [+18, +22), the +143
target is no longer rejected, and the probe is optimized:

  crash> dis queued_spin_lock_slowpath
  0xffffffff823ebdf0 <queued_spin_lock_slowpath>: endbr64
  0xffffffff823ebdf4 <queued_spin_lock_slowpath+4>:       push   %r14
  0xffffffff823ebdf6 <queued_spin_lock_slowpath+6>:       push   %r13
  0xffffffff823ebdf8 <queued_spin_lock_slowpath+8>:       push   %r12
  0xffffffff823ebdfa <queued_spin_lock_slowpath+10>:      push   %rbp
  0xffffffff823ebdfb <queued_spin_lock_slowpath+11>:      mov    %rdi,%rbp
  0xffffffff823ebdfe <queued_spin_lock_slowpath+14>:      push   %rbx
  0xffffffff823ebdff <queued_spin_lock_slowpath+15>:      xchg   %ax,%ax
  0xffffffff823ebe01 <queued_spin_lock_slowpath+17>:      jmp    0xffffffffc0401000
  0xffffffff823ebe06 <queued_spin_lock_slowpath+22>:      mov    0x0(%rbp),%eax
  0xffffffff823ebe09 <queued_spin_lock_slowpath+25>:      test   %eax,%eax
  0xffffffff823ebe0b <queued_spin_lock_slowpath+27>:      jne    0xffffffff823ebe7d <queued_spin_lock_slowpath+141>
  0xffffffff823ebe0d <queued_spin_lock_slowpath+29>:      lock cmpxchg %edx,0x0(%rbp)

So the effect is: the same probe point now uses an optimized jump into
the detour buffer instead of an int3 trap, which is measurable under
lock contention since queued_spin_lock_slowpath is hot. Semantics are
unchanged; the +143 back edge bypasses the probe point in both cases, so
the hit count is the same.

Regarding the generality of the false positive: it needs a jump whose
target is exactly paddr + JMP32_INSN_SIZE while the copied length is
also exactly JMP32_INSN_SIZE. When the copied length is larger,
paddr + JMP32_INSN_SIZE lands inside an instruction and cannot be a real
jump target, so allowing it does not let a jump into the overwritten
region slip through.

Since there is no reported failure, I'm fine with dropping the Fixes:
tag if you'd prefer to treat this as a cleanup. I can also post a
separate change that makes the check use the actual optimized length
instead of the open-coded DISP32_SIZE, if you think that's the better
direction, and add a regression case under
tools/testing/selftests/kprobes.

Thanks,
Jinke Han

  reply	other threads:[~2026-11-20  0:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 12:25 Jinke Han
2026-11-19 19:41 ` Dave Hansen
2026-11-20  0:36   ` Jinke Han [this message]
2026-11-20  0:41     ` Dave Hansen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120003621.GA13347@didi-ThinkCentre-M920t-N000 \
    --to=jinkehan@didiglobal.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhiramat@redhat.com \
    --cc=mingo@redhat.com \
    --cc=nik.borisov@suse.com \
    --cc=peterz@infradead.org \
    --cc=rppt@kernel.org \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®