Skip to contentSkip to conversation

PERF-HW_BKPT: Allow per-cpu kernel-space Hardware Breakpoint requests

RFC PATCH New K.Prasad 1 participant 4 messages Oct 28, 2009 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

K.Prasad

author
#1 lore raw

RFC PATCH 1/4 PERF-HW_BKPT: Allow per-cpu kernel-space Hardware Breakpoint requests

Allow kernel-space hw-breakpoints to be restricted only for a subset of CPUs using a cpumask field in 'struct hw_breakpoint'.

---
 arch/x86/include/asm/hw_breakpoint.h |    1 
 arch/x86/kernel/hw_breakpoint.c      |   12 +++
 include/asm-generic/hw_breakpoint.h  |    2 
 kernel/hw_breakpoint.c               |  124 +++++++++++++++++++++++++----------
 4 files changed, 104 insertions(+), 35 deletions(-)
arch/x86/kernel/hw_breakpoint.c +11 -1
Index: linux-2.6-tip.perf_hbkpt/arch/x86/kernel/hw_breakpoint.c
===================================================================
@@ -68,6 +68,12 @@ static unsigned long encode_dr7(int drnu
 	return bp_info;
 }
 
+/* Disable breakpoints on the physical debug registers */
+void arch_disable_hw_breakpoint(void)
+{
+	set_debugreg(0UL, 7);
+}
+
 void arch_update_kernel_hw_breakpoint(void *unused)
 {
 	struct hw_breakpoint *bp;
@@ -78,7 +84,7 @@ void arch_update_kernel_hw_breakpoint(vo
 	set_debugreg(0UL, 7);
 
 	for (i = hbp_kernel_pos; i < HBP_NUM; i++) {
-		per_cpu(this_hbp_kernel[i], cpu) = bp = hbp_kernel[i];
+		bp = per_cpu(this_hbp_kernel[i], cpu);
 		if (bp) {
 			temp_kdr7 |= encode_dr7(i, bp->info.len, bp->info.type);
 			set_debugreg(bp->info.address, i);
@@ -207,6 +213,10 @@ int arch_validate_hwbkpt_settings(struct
 	unsigned int align;
 	int ret = -EINVAL;
 
+	/* User-space breakpoints cannot be restricted to a subset of CPUs */
+	if (tsk && bp->cpumask)
+		return ret;
+
 	switch (bp->info.type) {
 	/*
 	 * Ptrace-refactoring code
include/asm-generic/hw_breakpoint.h +2 -0
Index: linux-2.6-tip.perf_hbkpt/include/asm-generic/hw_breakpoint.h
===================================================================
@@ -8,6 +8,7 @@
 #ifdef	__KERNEL__
 #include <linux/list.h>
 #include <linux/types.h>
+#include <linux/cpumask.h>
 #include <linux/kallsyms.h>
 
 /**
@@ -102,6 +103,7 @@
  */
 struct hw_breakpoint {
 	void (*triggered)(struct hw_breakpoint *, struct pt_regs *);
+	const cpumask_t *cpumask;
 	struct arch_hw_breakpoint info;
 };
 
kernel/hw_breakpoint.c +90 -34

175 lines not on this page · raw message

arch/x86/include/asm/hw_breakpoint.h +1 -0
Index: linux-2.6-tip.perf_hbkpt/arch/x86/include/asm/hw_breakpoint.h
===================================================================
@@ -48,6 +48,7 @@ extern int arch_validate_hwbkpt_settings
 extern void arch_update_user_hw_breakpoint(int pos, struct task_struct *tsk);
 extern void arch_flush_thread_hw_breakpoint(struct task_struct *tsk);
 extern void arch_update_kernel_hw_breakpoint(void *);
+extern void arch_disable_hw_breakpoint(void);
 extern int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
 				     unsigned long val, void *data);
 #endif	/* __KERNEL__ */
author
#2 lore raw

RFC PATCH 2/4 PERF-HW_BKPT: Enable/disable the breakpoints when still registered

In reply to a message not in the archive

Allow breakpoints to be enabled/disabled without yielding the breakpoint request through new APIs - <enable><disable>_hw_breakpoint()

---
 arch/x86/kernel/hw_breakpoint.c     |   15 ++++++++++-----
 include/asm-generic/hw_breakpoint.h |   14 ++++++++++++++
 kernel/hw_breakpoint.c              |   31 ++++++++++++++++++++++++++++++-
 3 files changed, 54 insertions(+), 6 deletions(-)
arch/x86/kernel/hw_breakpoint.c +10 -5
Index: linux-2.6-tip.perf_hbkpt/arch/x86/kernel/hw_breakpoint.c
===================================================================
@@ -85,10 +85,11 @@ void arch_update_kernel_hw_breakpoint(vo
 
 	for (i = hbp_kernel_pos; i < HBP_NUM; i++) {
 		bp = per_cpu(this_hbp_kernel[i], cpu);
-		if (bp) {
+		if (!bp)
+			continue;
+		set_debugreg(bp->info.address, i);
+		if (atomic_read(&bp->enabled) == BP_ENABLED)
 			temp_kdr7 |= encode_dr7(i, bp->info.len, bp->info.type);
-			set_debugreg(bp->info.address, i);
-		}
 	}
 
 	/* No need to set DR6. Update the debug registers with kernel-space
@@ -288,8 +289,9 @@ void arch_update_user_hw_breakpoint(int 
 	thread->debugreg7 &= ~dr7_masks[pos];
 	if (bp) {
 		thread->debugreg[pos] = bp->info.address;
-		thread->debugreg7 |= encode_dr7(pos, bp->info.len,
-							bp->info.type);
+		if (atomic_read(&bp->enabled) == BP_ENABLED)
+			thread->debugreg7 |= encode_dr7(pos, bp->info.len,
+								bp->info.type);
 	} else
 		thread->debugreg[pos] = 0;
 }
@@ -377,6 +379,9 @@ static int __kprobes hw_breakpoint_handl
 		 */
 		if (!bp)
 			continue;
+		/* Ignore exceptions due to disabled breakpoints */
+		if (atomic_read(&bp->enabled) == BP_DISABLED)
+			continue;
 
 		(bp->triggered)(bp, args->regs);
 	}
include/asm-generic/hw_breakpoint.h +14 -0
Index: linux-2.6-tip.perf_hbkpt/include/asm-generic/hw_breakpoint.h
===================================================================
@@ -101,7 +101,19 @@
  *
  * ----------------------------------------------------------------------
  */
+
+enum bp_status {
+	BP_DISABLED	= 0,
+	BP_ENABLED	= 1
+};
+
 struct hw_breakpoint {
+	/*
+	 * Denotes if a breakpoint is currently enabled in physical debug
+	 * registers. Not to be set directly by the end-user. Must be
+	 * operated through <enable><disable>_hw_breakpoint() APIs only.
+	 */
+	atomic_t enabled;
 	void (*triggered)(struct hw_breakpoint *, struct pt_regs *);
 	const cpumask_t *cpumask;
 	struct arch_hw_breakpoint info;
@@ -135,6 +147,8 @@ extern void unregister_user_hw_breakpoin
 extern int register_kernel_hw_breakpoint(struct hw_breakpoint *bp);
 extern void unregister_kernel_hw_breakpoint(struct hw_breakpoint *bp);
 
+extern void enable_hw_breakpoint(struct hw_breakpoint *bp);
+extern void disable_hw_breakpoint(struct hw_breakpoint *bp);
 extern unsigned int hbp_kernel_pos;
 
 #endif	/* __KERNEL__ */
kernel/hw_breakpoint.c +30 -1
Index: linux-2.6-tip.perf_hbkpt/kernel/hw_breakpoint.c
===================================================================
@@ -229,8 +229,10 @@ int register_user_hw_breakpoint(struct t
 			break;
 		}
 	}
-	if (!rc)
+	if (!rc) {
 		set_tsk_thread_flag(tsk, TIF_DEBUG);
+		atomic_set(&bp->enabled, BP_ENABLED);
+	}
 
 	spin_unlock_bh(&hw_breakpoint_lock);
 	return rc;
@@ -351,6 +353,7 @@ int register_kernel_hw_breakpoint(struct
 		}
 	}
 
+	atomic_set(&bp->enabled, BP_ENABLED);
 	if (cpumask_test_cpu(smp_processor_id(), bp->cpumask))
 		update_each_cpu_kernel_hbp(bp);
 	smp_call_function_many(bp->cpumask, update_each_cpu_kernel_hbp, bp, 1);
@@ -420,6 +423,32 @@ ret_path:
 }
 EXPORT_SYMBOL_GPL(unregister_kernel_hw_breakpoint);
 
+/**
+ * enable_hw_breakpoint - re-enable a breakpoint previously disabled
+ * @bp: pointer to the breakpoint structure to be enabled
+ *
+ * Re-enable or disable a breakpoint, previously disabled using
+ * disable_hw_breakpoint()
+ */
+void enable_hw_breakpoint(struct hw_breakpoint *bp)
+{
+	atomic_set(&bp->enabled, BP_ENABLED);
+}
+EXPORT_SYMBOL_GPL(enable_hw_breakpoint);
+
+/**
+ * disable_hw_breakpoint - disable a breakpoint from raising breakpoint exceptions
+ * @bp: pointer to the breakpoint structure to be disabled
+ *
+ * Disable a breakpoint without actually losing the registration. Re-enable it
+ * again using enable_hw_breakpoint()
+ */
+void disable_hw_breakpoint(struct hw_breakpoint *bp)
+{
+	atomic_set(&bp->enabled, BP_DISABLED);
+}
+EXPORT_SYMBOL_GPL(disable_hw_breakpoint);
+
 static struct notifier_block hw_breakpoint_exceptions_nb = {
 	.notifier_call = hw_breakpoint_exceptions_notify,
 	/* we need to be notified first */
author
#3 lore raw

RFC PATCH 3/4 PERF-HW_BKPT: Fix traceback seen when resuming after suspend-to-ram

In reply to a message not in the archive

Load the debug registers (when a CPU turns online) through a tasklet. This helps to keep routines (using protected hw-breakpoint data) to be invoked only from non-interrupt context, thus making the spin_lock_bh() locking sufficient.

---
 kernel/hw_breakpoint.c |   26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
kernel/hw_breakpoint.c +19 -7
Index: linux-2.6-tip.perf_hbkpt/kernel/hw_breakpoint.c
===================================================================
@@ -23,6 +23,7 @@
  * This file contains the arch-independent routines.
  */
 
+#include <linux/interrupt.h>
 #include <linux/irqflags.h>
 #include <linux/kallsyms.h>
 #include <linux/notifier.h>
@@ -69,20 +70,19 @@ unsigned int hbp_user_refcount[HBP_NUM];
 static DEFINE_PER_CPU(int, hbp_consumed);
 
 /*
- * Load the debug registers during startup of a CPU.
+ * A tasklet to populate the debug register's values from the
+ * breakpoint structure
  */
-void load_debug_registers(void)
+static struct tasklet_struct hbp_tasklet;
+
+/* Tasklet function that populates the debug registers */
+void hbp_tasklet_function(unsigned long unused)
 {
-	unsigned long flags;
 	struct task_struct *tsk = current;
 
 	spin_lock_bh(&hw_breakpoint_lock);
 
-	/* Prevent IPIs for new kernel breakpoint updates */
-	local_irq_save(flags);
 	arch_update_kernel_hw_breakpoint(NULL);
-	local_irq_restore(flags);
-
 	if (test_tsk_thread_flag(tsk, TIF_DEBUG))
 		arch_install_thread_hw_breakpoint(tsk);
 
@@ -90,6 +90,18 @@ void load_debug_registers(void)
 }
 
 /*
+ * Load the debug registers during startup of a CPU.
+ */
+void load_debug_registers(void)
+{
+	unsigned long flags;
+
+	if (!hbp_tasklet.func)
+		tasklet_init(&hbp_tasklet, &hbp_tasklet_function, 0UL);
+	tasklet_schedule(&hbp_tasklet);
+}
+
+/*
  * Erase all the hardware breakpoint info associated with a thread.
  *
  * If tsk != current then tsk must not be usable (for example, a
author
#4 lore raw

RFC PATCH 4/4 PERF-HW_HBKPT: Enable perf-events to use hw-breakpoints

In reply to a message not in the archive

Enable perf-events to collect memory access statistics on kernel-space data in the context of a running process.

---
 include/linux/perf_event.h     |   17 ++++++
 kernel/perf_event.c            |  102 +++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/parse-events.c |   42 ++++++++++++++++
 3 files changed, 161 insertions(+)
include/linux/perf_event.h +17 -0
Index: linux-2.6-tip.perf_hbkpt/include/linux/perf_event.h
===================================================================
@@ -31,6 +31,7 @@ enum perf_type_id {
 	PERF_TYPE_TRACEPOINT			= 2,
 	PERF_TYPE_HW_CACHE			= 3,
 	PERF_TYPE_RAW				= 4,
+	PERF_TYPE_BREAKPOINT			= 5,
 
 	PERF_TYPE_MAX,				/* non-ABI */
 };
@@ -106,6 +107,14 @@ enum perf_sw_ids {
 	PERF_COUNT_SW_MAX,			/* non-ABI */
 };
 
+/* Various breakpoint types for monitoring accesses over variables */
+enum perf_bp_id {
+	PERF_COUNT_BP_WRITE		= 0,
+	PERF_COUNT_BP_RW		= 1,
+
+	PERF_COUNT_BP_MAX,		/* non-ABI */
+};
+
 /*
  * Bits that can be set in attr.sample_type to request information
  * in the overflow packets.
@@ -207,6 +216,10 @@ struct perf_event_attr {
 		__u32		wakeup_events;	  /* wakeup every n events */
 		__u32		wakeup_watermark; /* bytes before wakeup   */
 	};
+
+	/* Store the symbol name for breakpoint request */
+	char			ksym_name[128];
+
 	__u32			__reserved_2;
 
 	__u64			__reserved_3;
@@ -476,6 +489,10 @@ struct hw_perf_event {
 			s64		remaining;
 			struct hrtimer	hrtimer;
 		};
+		struct { /* hardware breakpoints */
+			struct hw_breakpoint	*bp;
+			int			counter;
+		};
 	};
 	atomic64_t			prev_count;
 	u64				sample_period;
kernel/perf_event.c +102 -0

126 lines not on this page · raw message

tools/perf/util/parse-events.c +42 -0

84 lines not on this page · raw message