Re: [PATCH v20 01/11] x86/hw_breakpoints: Add arch_modify_local_hw_breakpoint_addr() API

From
sashiko-bot@kernel.org
Date
2026-11-16 05:47:58
Also in
linux-perf-users
Reply to
Masami Hiramatsu (Google), 2026-11-16
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] x86/hw_breakpoints: arch_modify_local_hw_breakpoint_addr()
  corrupts DRs
--

commit 490e341c5679ea8c1cf5b3a39b5b39ecc93de7d0
Author: Jinchao Wang [off-list ref]

x86/hw_breakpoints: Add arch_modify_local_hw_breakpoint_addr() API

This commit adds arch_modify_local_hw_breakpoint_addr() to update the
address of an installed per-CPU hardware breakpoint on the local CPU
without releasing and re-allocating its debug register slot.

Link: https://lore.kernel.org/all/59637b96946653393a7ad3c7de094094796b39c2.1785067572.git.wangjinchao600@gmail.com/ (local)
quoted hunk ↗ jump to hunk
diff --git a/arch/x86/kernel/hw_breakpoint.c b/arch/x86/kernel/hw_breakpoint.c
index f846c15f21ca1..3f63f7899f95e 100644
--- a/arch/x86/kernel/hw_breakpoint.c
+++ b/arch/x86/kernel/hw_breakpoint.c
@@ -133,6 +133,57 @@ int arch_install_hw_breakpoint(struct perf_event *bp)
 	return 0;
 }
 
+/*
+ * Modify the address of an installed perf counter breakpoint on local CPU.
+ *
+ * This updates the debug address register and per-CPU shadow state without
+ * changing slot allocation or DR7. The core layer (modify_local_hw_breakpoint_addr())
+ * manages updating bp->attr.bp_addr and counter_arch_bp(bp)->address.
+ *
+ * Atomic: called with IRQs disabled and handles variables and registers
+ * local to this CPU.
+ */
+int arch_modify_local_hw_breakpoint_addr(struct perf_event *bp,
+					 unsigned long addr)
+{
+	struct arch_hw_breakpoint *info = counter_arch_bp(bp);
+	struct arch_hw_breakpoint hw;
+	struct perf_event_attr attr = bp->attr;
+	unsigned long dr7;
+	int i, ret;
+
+	lockdep_assert_irqs_disabled();
+
+	attr.bp_addr = addr;
+	ret = hw_breakpoint_arch_parse(bp, &attr, &hw);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < HBP_NUM; i++) {
+		if (this_cpu_read(bp_per_reg[i]) == bp)
+			break;
+	}
+
+	if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot"))
+		return -ENOENT;
+
+	/*
+	 * Temporarily clear the breakpoint enable bit in DR7 before updating the
+	 * debug address register, and re-enable it afterward, to comply with the
+	 * x86 hardware specification and avoid spurious #DB exceptions.
+	 */
+	dr7 = this_cpu_read(cpu_dr7);
+	set_debugreg(dr7 & ~__encode_dr7(i, info->len, info->type), 7);
+
+	this_cpu_write(cpu_debugreg[i], addr);
+	barrier();
+	set_debugreg(addr, i);
+
+	set_debugreg(dr7, 7);
[Severity: High]
Can arch_modify_local_hw_breakpoint_addr() corrupt hardware debug
registers or prematurely re-enable hardware DR7 when host breakpoints are
temporarily disabled or switched to KVM guest state?

Several paths disable hardware DR7 (writing DR7_FIXED_1) or replace
hardware DR0-DR3 without clearing the per-CPU shadow cpu_dr7:

arch/x86/kvm/x86.c:vcpu_enter_guest() {
	...
	if (unlikely(vcpu->arch.switch_db_regs &&
		     !(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH))) {
		set_debugreg(DR7_FIXED_1, 7);
		set_debugreg(vcpu->arch.eff_db[0], 0);
		set_debugreg(vcpu->arch.eff_db[1], 1);
		set_debugreg(vcpu->arch.eff_db[2], 2);
		set_debugreg(vcpu->arch.eff_db[3], 3);
	}
	...
}

vcpu_enter_guest() runs in normal context with IRQs disabled and invokes
instrumentable functions and tracepoints (such as trace_kvm_entry and
trace_kvm_exit) before sync_dirty_debug_regs() and
hw_breakpoint_restore().  If a wprobe trigger calls
arch_modify_local_hw_breakpoint_addr() in this window, set_debugreg(addr,
i) overwrites the guest's hardware DR[i] with the host kernel address
(which sync_dirty_debug_regs() then saves into vcpu->arch.db[i], leaking
the host address to the guest), and set_debugreg(dr7, 7) activates host
cpu_dr7 while the remaining hardware DR[j] registers still hold guest
addresses.

Similarly, if called while use_temporary_mm() -> hw_breakpoint_disable()
or local_db_save() has suppressed hardware breakpoints, writing cpu_dr7
back to hardware DR7 re-enables the breakpoints prematurely while temp_mm
is loaded (with 0UL in the other DR registers) or while inside a #DB
exception context.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179480675130.38065.3086970856119381693.stgit@devnote2?part=1
Thread (17 messages) 17 messages, 2 authors, last reply 17h ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help