Skip to contentSkip to conversation

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

RFC PATCH In review K.Prasad 2 participants 7 messages Oct 29, 2009 – Nov 22, 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/5 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/5 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 {
+	/*
+	 * The 'enabled' flag denotes if a breakpoint hit would in-turn invoke
+	 * the 'triggered' function. 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/5 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/5 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     |   18 ++++++
 kernel/perf_event.c            |  108 ++++++++++++++++++++++++++++++++++++++++-
 tools/perf/util/parse-events.c |   47 +++++++++++++++++
 3 files changed, 172 insertions(+), 1 deletion(-)
include/linux/perf_event.h +18 -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,11 @@ struct perf_event_attr {
 		__u32		wakeup_events;	  /* wakeup every n events */
 		__u32		wakeup_watermark; /* bytes before wakeup   */
 	};
+
+	/* Store the symbol name for breakpoint request */
+	/* TODO: ksym_name[KSYM_NAME_LEN] requires kallsyms.h to be included */
+	char			ksym_name[128];
+
 	__u32			__reserved_2;
 
 	__u64			__reserved_3;
@@ -476,6 +490,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 +107 -1

138 lines not on this page · raw message

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

89 lines not on this page · raw message

#5 lore raw

Re: [RFC Patch 4/5] PERF-HW_HBKPT: Enable perf-events to use hw-breakpoints

Quoted 4 lines · K.Prasad

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

I'm going to take the userspace part of this patch to integrate breakpoints in perf tools.

But I'm not sure passing the name of the symbol to the kernel and store it in the perf attr is a good idea.
We should probably resolve the name from userspace and pass the address to the kernel. Mostly because if we want to support breakpoints in userspace apps, we don't want conflicts between kernel and userspace symbols.

Thanks.

#6 lore raw

Re: [RFC Patch 4/5] PERF-HW_HBKPT: Enable perf-events to use hw-breakpoints

Quoted 11 lines · Frederic Weisbecker
Quoted 4 lines · K.Prasad

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

I'm going to take the userspace part of this patch to integrate breakpoints in perf tools.

Quoted 9 lines

But I'm not sure passing the name of the symbol to the kernel and store it in the perf attr is a good idea.
We should probably resolve the name from userspace and pass the address to the kernel. Mostly because if we want to support breakpoints in userspace apps, we don't want conflicts between kernel and userspace symbols.

Thanks.

Hmm, actually instead of memory-write/memory-readwrite I would rather see:

mem:symbol:[r][w][x]

like mem:pid_max:rw

We could default it to rw for now.

This will be more flexible I guess.
And later for ranges:

mem:symbol[[start]:[len]:[rwx]

This could be:
mem:array_name[0:5]:rwx

So, I'll need an ad-hoc parsing function for this one. This would be hard by integrating your patch.
I'll try something else. But I can probably take the 5th patch of your series.

Thanks.

author
#7 lore raw

RFC PATCH 5/5 PERF-HW_HBKPT: Display kernel symbol-name along with perf output

In reply to a message not in the archive

Enable the kernel symbol to be shown when perf statistics are reported. This enables the user to quickly recognise the hit counter with the symbol to which it corresponds to.

---
 tools/perf/builtin-report.c    |    4 ++--
 tools/perf/util/parse-events.c |   18 +++++++++++-------
 tools/perf/util/parse-events.h |    2 +-
 3 files changed, 14 insertions(+), 10 deletions(-)
tools/perf/util/parse-events.c +11 -7
Index: linux-2.6-tip.perf_hbkpt/tools/perf/util/parse-events.c
===================================================================
@@ -285,15 +285,14 @@ static char *event_cache_name(u8 cache_t
 
 const char *event_name(int counter)
 {
-	u64 config = attrs[counter].config;
-	int type = attrs[counter].type;
-
-	return __event_name(type, config);
+	return __event_name(&attrs[counter]);
 }
 
-const char *__event_name(int type, u64 config)
+const char *__event_name(const struct perf_event_attr *attr)
 {
-	static char buf[32];
+	int type = attr->type;
+	u64 config = attr->config;
+	static char buf[256];
 
 	if (type == PERF_TYPE_RAW) {
 		sprintf(buf, "raw 0x%llx", config);
@@ -336,7 +335,12 @@ const char *__event_name(int type, u64 c
 		return tracepoint_id_to_name(config);
 
 	case PERF_TYPE_BREAKPOINT:
-		return hbp_event_names[config];
+		/*
+		 * Separate the breakpoint event from the kernel symbol
+		 * using a ":" symbol
+		 */
+		sprintf(buf, "%s:%s", hbp_event_names[config], attr->ksym_name);
+			return buf;
 
 	default:
 		break;
tools/perf/util/parse-events.h +1 -1
Index: linux-2.6-tip.perf_hbkpt/tools/perf/util/parse-events.h
===================================================================
@@ -20,7 +20,7 @@ extern struct perf_event_attr attrs[MAX_
 extern char *filters[MAX_COUNTERS];
 
 extern const char *event_name(int ctr);
-extern const char *__event_name(int type, u64 config);
+extern const char *__event_name(const struct perf_event_attr *attr);
 
 extern int parse_events(const struct option *opt, const char *str, int unset);
 extern int parse_filter(const struct option *opt, const char *str, int unset);
tools/perf/builtin-report.c +2 -2
Index: linux-2.6-tip.perf_hbkpt/tools/perf/builtin-report.c
===================================================================
@@ -849,7 +849,7 @@ process_read_event(event_t *event, unsig
 	attr = perf_header__find_attr(event->read.id, header);
 
 	if (show_threads) {
-		const char *name = attr ? __event_name(attr->type, attr->config)
+		const char *name = attr ? __event_name(attr)
 				   : "unknown";
 		perf_read_values_add_value(&show_threads_values,
 					   event->read.pid, event->read.tid,
@@ -863,7 +863,7 @@ process_read_event(event_t *event, unsig
 			(void *)(long)(event->header.size),
 			event->read.pid,
 			event->read.tid,
-			attr ? __event_name(attr->type, attr->config)
+			attr ? __event_name(attr)
 			     : "FAIL",
 			event->read.value);