Skip to contentSkip to conversation

Allow arch-specific cleanup before breakpoint unregistration

PATCH In review K.Prasad 6 participants 18 messages May 25, 2010 – May 28, 2010 1 message not in the archive

Subsystems (2)
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

PATCH 1/4 Allow arch-specific cleanup before breakpoint unregistration

Certain architectures (such as PowerPC Book III S) have a need to cleanup data-structures before the breakpoint is unregistered. This patch introduces an arch-specific hook in release_bp_slot() along with a weak definition in the form of a stub funciton.

---
 kernel/hw_breakpoint.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)
kernel/hw_breakpoint.c +12 -0
Index: linux-2.6.ppc64_test/kernel/hw_breakpoint.c
===================================================================
@@ -242,6 +242,17 @@ toggle_bp_slot(struct perf_event *bp, bo
 }
 
 /*
+ * Function to perform processor-specific cleanup during unregistration
+ */
+__weak void arch_unregister_hw_breakpoint(struct perf_event *bp)
+{
+	/*
+	 * A weak stub function here for those archs that don't define
+	 * it inside arch/.../kernel/hw_breakpoint.c
+	 */
+}
+
+/*
  * Contraints to check before allowing this new breakpoint counter:
  *
  *  == Non-pinned counter == (Considered as pinned for now)
@@ -339,6 +350,7 @@ void release_bp_slot(struct perf_event *
 {
 	mutex_lock(&nr_bp_mutex);
 
+	arch_unregister_hw_breakpoint(bp);
 	__release_bp_slot(bp);
 
 	mutex_unlock(&nr_bp_mutex);
#2 lore raw

PATCH 1/4 Allow arch-specific cleanup before breakpoint unregistration

Quoted 9 lines · K.Prasad

Certain architectures (such as PowerPC Book III S) have a need to cleanup data-structures before the breakpoint is unregistered. This patch introduces an arch-specific hook in release_bp_slot() along with a weak definition in the form of a stub funciton.

---
 kernel/hw_breakpoint.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)

My understanding is weak function definitions must appear in a different C file than their call sites to work on some toolchains.

Andrew, can you confirm the above statement?

Quoted 24 lines

Index: linux-2.6.ppc64_test/kernel/hw_breakpoint.c
===================================================================

kernel/hw_breakpoint.c +11 -0
@@ -242,6 +242,17 @@  toggle_bp_slot(struct perf_event *bp, bo
 }
 
 /*
+ * Function to perform processor-specific cleanup during unregistration
+ */
+__weak void arch_unregister_hw_breakpoint(struct perf_event *bp)
+{
+	/*
+	 * A weak stub function here for those archs that don't define
+	 * it inside arch/.../kernel/hw_breakpoint.c
+	 */
+}
+
+/*
  * Contraints to check before allowing this new breakpoint counter:
  *
  *  == Non-pinned counter == (Considered as pinned for now)

kernel/hw_breakpoint.c void release_bp_slot(struct perf_event * · line 356

Quoted 7 lines
kernel/hw_breakpoint.c +1 -0
@@ -339,6 +350,7 @@  void release_bp_slot(struct perf_event *
 {
 	mutex_lock(&nr_bp_mutex);
 
+	arch_unregister_hw_breakpoint(bp);
 	__release_bp_slot(bp);
 
 	mutex_unlock(&nr_bp_mutex);

Since the weak version is empty, should it just be delcared (in a header, put the comment there) and not defined?

milton

author
#3 lore raw
Quoted 6 lines · Millton Miller
Quoted 5 lines · K.Prasad

Certain architectures (such as PowerPC Book III S) have a need to cleanup data-structures before the breakpoint is unregistered. This patch introduces an arch-specific hook in release_bp_slot() along with a weak definition in the form of a stub funciton.

Quoted 9 lines
Quoted 4 lines
---
 kernel/hw_breakpoint.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)

My understanding is weak function definitions must appear in a different C file than their call sites to work on some toolchains.

Atleast, there are quite a few precedents inside the Linux kernel for __weak functions being invoked from the file in which they are defined (arch_hwblk_init, arch_enable_nonboot_cpus_begin and hw_perf_disable to name a few).
Moreover the online GCC docs haven't any such constraints mentioned.

Quoted 31 lines

Andrew, can you confirm the above statement?

Quoted 29 lines

Index: linux-2.6.ppc64_test/kernel/hw_breakpoint.c
===================================================================

kernel/hw_breakpoint.c +12 -0
@@ -242,6 +242,17 @@  toggle_bp_slot(struct perf_event *bp, bo
 }
 
 /*
+ * Function to perform processor-specific cleanup during unregistration
+ */
+__weak void arch_unregister_hw_breakpoint(struct perf_event *bp)
+{
+	/*
+	 * A weak stub function here for those archs that don't define
+	 * it inside arch/.../kernel/hw_breakpoint.c
+	 */
+}
+
+/*
  * Contraints to check before allowing this new breakpoint counter:
  *
  *  == Non-pinned counter == (Considered as pinned for now)
@@ -339,6 +350,7 @@  void release_bp_slot(struct perf_event *
 {
 	mutex_lock(&nr_bp_mutex);
 
+	arch_unregister_hw_breakpoint(bp);
 	__release_bp_slot(bp);
 
Quoted 7 lines

kernel/hw_breakpoint.c void release_bp_slot(struct perf_event * · line 356

Quoted 2 lines
kernel/hw_breakpoint.c +0 -0
 	mutex_unlock(&nr_bp_mutex);

Since the weak version is empty, should it just be delcared (in a header, put the comment there) and not defined?

The initial thinking behind defining it in the .c file was, for one, the function need not be moved (from .h to .c) when other architectures have a need to populate them. Secondly, given that powerpc (which has a 'strong' definition for arch_unregister_hw_breakpoint()) includes the header file (in which this can be moved to) I wasn't sure about possible conflicts.

Quoted 5 lines

milton
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

Thanks,
K.Prasad

#4 lore raw
Quoted 9 lines · K.Prasad
Quoted 3 lines

My understanding is weak function definitions must appear in a different C file than their call sites to work on some toolchains.

Atleast, there are quite a few precedents inside the Linux kernel for __weak functions being invoked from the file in which they are defined (arch_hwblk_init, arch_enable_nonboot_cpus_begin and hw_perf_disable to name a few).
Moreover the online GCC docs haven't any such constraints mentioned.

I've seen problems in this area. gcc sometimes inlines a weak function that's in the same file as the call point.

David

#5 lore raw
Quoted 6 lines · David Howells
Quoted 4 lines · K.Prasad
Quoted 3 lines

My understanding is weak function definitions must appear in a different C file than their call sites to work on some toolchains.

Quoted 8 lines
Quoted 5 lines

Atleast, there are quite a few precedents inside the Linux kernel for __weak functions being invoked from the file in which they are defined (arch_hwblk_init, arch_enable_nonboot_cpus_begin and hw_perf_disable to name a few).
Moreover the online GCC docs haven't any such constraints mentioned.

I've seen problems in this area. gcc sometimes inlines a weak function that's in the same file as the call point.

See the functions in kernel/softirq.c for example, and commits 43a256322 and b2e2fe996 - though unhelpfully they don't mention the gcc version. A bit of googling suggests it was probably "gcc version 4.1.1 20060525 (Red Hat 4.1.1-1)" in that case.

But the example of hw_perf_enable() (which is weak in the same unit), suggests maybe this isn't a bug many people are hitting in practice anymore.

Having said that the #define foo foo pattern is reasonably neat and avoids the problem altogether, see eg. arch_setup_msi_irqs.

cheers

author
#6 lore raw
In reply to David Howells
Quoted 6 lines · David Howells
Quoted 4 lines · K.Prasad
Quoted 3 lines

My understanding is weak function definitions must appear in a different C file than their call sites to work on some toolchains.

Quoted 9 lines
Quoted 5 lines

Atleast, there are quite a few precedents inside the Linux kernel for __weak functions being invoked from the file in which they are defined (arch_hwblk_init, arch_enable_nonboot_cpus_begin and hw_perf_disable to name a few).
Moreover the online GCC docs haven't any such constraints mentioned.

I've seen problems in this area. gcc sometimes inlines a weak function that's in the same file as the call point.

We've seen such behaviour even otherwise....even with noinline attribute in place. I'm not sure if this gcc fix
(http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16922) helped correct the behaviour, but the lesson has been to not trust a function to be inlined/remain non-inline consistently.

Quoted 1 line

David

Thanks,
K.Prasad

#7 lore raw
Quoted 17 lines · K.Prasad
Quoted 6 lines · David Howells
Quoted 4 lines · K.Prasad
Quoted 3 lines

My understanding is weak function definitions must appear in a different C file than their call sites to work on some toolchains.

Quoted 9 lines
Quoted 5 lines

Atleast, there are quite a few precedents inside the Linux kernel for __weak functions being invoked from the file in which they are defined (arch_hwblk_init, arch_enable_nonboot_cpus_begin and hw_perf_disable to name a few).
Moreover the online GCC docs haven't any such constraints mentioned.

I've seen problems in this area. gcc sometimes inlines a weak function that's in the same file as the call point.

Quoted 5 lines

We've seen such behaviour even otherwise....even with noinline attribute in place. I'm not sure if this gcc fix
(http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16922) helped correct the behaviour, but the lesson has been to not trust a function to be inlined/remain non-inline consistently.

If we can't put the call to the function in the same file of its weak definition, then perf is totally screwed.

And in fact it makes __weak basically useless and unusable. I guess that happened in old gcc versions that have been fixed now.

Anyway, I'm personally fine with this patch (you can put my hack if you want).

Thanks.

author
#8 lore raw
Quoted 25 lines · Frederic Weisbecker
Quoted 17 lines · K.Prasad
Quoted 6 lines · David Howells
Quoted 4 lines · K.Prasad
Quoted 3 lines

My understanding is weak function definitions must appear in a different C file than their call sites to work on some toolchains.

Quoted 9 lines
Quoted 5 lines

Atleast, there are quite a few precedents inside the Linux kernel for __weak functions being invoked from the file in which they are defined (arch_hwblk_init, arch_enable_nonboot_cpus_begin and hw_perf_disable to name a few).
Moreover the online GCC docs haven't any such constraints mentioned.

I've seen problems in this area. gcc sometimes inlines a weak function that's in the same file as the call point.

Quoted 5 lines

We've seen such behaviour even otherwise....even with noinline attribute in place. I'm not sure if this gcc fix
(http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16922) helped correct the behaviour, but the lesson has been to not trust a function to be inlined/remain non-inline consistently.

Quoted 9 lines

If we can't put the call to the function in the same file of its weak definition, then perf is totally screwed.

And in fact it makes __weak basically useless and unusable. I guess that happened in old gcc versions that have been fixed now.

Anyway, I'm personally fine with this patch (you can put my hack if you want).

I guess you meant "Acked-by:" :-)

Thanks, I'll add the same.

--K.Prasad

#9 lore raw
Quoted 29 lines · K.Prasad
Quoted 28 lines · Frederic Weisbecker
Quoted 17 lines · K.Prasad
Quoted 6 lines · David Howells
Quoted 4 lines · K.Prasad
Quoted 3 lines

My understanding is weak function definitions must appear in a different C file than their call sites to work on some toolchains.

Quoted 9 lines
Quoted 5 lines

Atleast, there are quite a few precedents inside the Linux kernel for __weak functions being invoked from the file in which they are defined (arch_hwblk_init, arch_enable_nonboot_cpus_begin and hw_perf_disable to name a few).
Moreover the online GCC docs haven't any such constraints mentioned.

I've seen problems in this area. gcc sometimes inlines a weak function that's in the same file as the call point.

Quoted 5 lines

We've seen such behaviour even otherwise....even with noinline attribute in place. I'm not sure if this gcc fix
(http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16922) helped correct the behaviour, but the lesson has been to not trust a function to be inlined/remain non-inline consistently.

If we can't put the call to the function in the same file of its weak definition, then perf is totally screwed.

Quoted 8 lines
Quoted 6 lines

And in fact it makes __weak basically useless and unusable. I guess that happened in old gcc versions that have been fixed now.

Anyway, I'm personally fine with this patch (you can put my hack if you want).

I guess you meant "Acked-by:" :-)

Oops, right :)

author
#10 lore raw
In reply to K.Prasad
Quoted 13 lines · K.Prasad
Quoted 12 lines · David Howells
Quoted 9 lines · K.Prasad
Quoted 3 lines

My understanding is weak function definitions must appear in a different C file than their call sites to work on some toolchains.

Atleast, there are quite a few precedents inside the Linux kernel for __weak functions being invoked from the file in which they are defined (arch_hwblk_init, arch_enable_nonboot_cpus_begin and hw_perf_disable to name a few).
Moreover the online GCC docs haven't any such constraints mentioned.

Quoted 7 lines
Quoted 3 lines

I've seen problems in this area. gcc sometimes inlines a weak function that's in the same file as the call point.

We've seen such behaviour even otherwise....even with noinline attribute in place. I'm not sure if this gcc fix
(http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16922) helped correct the

Looks like I cited the wrong bug. The appropriate one is
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34563.

Thanks,
K.Prasad

author
#11 lore raw

PATCH 2/4 PPC64-HWBKPT: Implement hw-breakpoints for PowerPC BookIII S

In reply to a message not in the archive

Implement perf-events based hw-breakpoint interfaces for PowerPC Book III S processors. These interfaces help arbitrate requests from various users and schedules them as appropriate.

---
 arch/powerpc/Kconfig                     |    1 
 arch/powerpc/include/asm/cputable.h      |    4 
 arch/powerpc/include/asm/hw_breakpoint.h |   73 ++++++
 arch/powerpc/include/asm/processor.h     |    8 
 arch/powerpc/kernel/Makefile             |    1 
 arch/powerpc/kernel/hw_breakpoint.c      |  338 +++++++++++++++++++++++++++++++
 arch/powerpc/kernel/machine_kexec_64.c   |    3 
 arch/powerpc/kernel/process.c            |    6 
 arch/powerpc/kernel/ptrace.c             |   64 +++++
 arch/powerpc/lib/Makefile                |    1 
 10 files changed, 499 insertions(+)
arch/powerpc/include/asm/hw_breakpoint.h new file +73 -0
Index: linux-2.6.ppc64_test/arch/powerpc/include/asm/hw_breakpoint.h
===================================================================
@@ -0,0 +1,73 @@
+/*
+ * PowerPC BookIII S hardware breakpoint definitions
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * Copyright 2010, IBM Corporation.
+ * Author: K.Prasad <[email protected]>
+ *
+ */
+
+#ifndef _PPC_BOOK3S_64_HW_BREAKPOINT_H
+#define _PPC_BOOK3S_64_HW_BREAKPOINT_H
+
+#ifdef	__KERNEL__
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+
+struct arch_hw_breakpoint {
+	u8		len; /* length of the target data symbol */
+	int		type;
+	unsigned long	address;
+};
+
+#include <linux/kdebug.h>
+#include <asm/reg.h>
+#include <asm/system.h>
+
+static inline int hw_breakpoint_slots(int type)
+{
+	return HBP_NUM;
+}
+struct perf_event;
+struct pmu;
+struct perf_sample_data;
+
+#define HW_BREAKPOINT_ALIGN 0x7
+/* Maximum permissible length of any HW Breakpoint */
+#define HW_BREAKPOINT_LEN 0x8
+
+extern int arch_bp_generic_fields(int type, int *gen_bp_type);
+extern int arch_check_bp_in_kernelspace(struct perf_event *bp);
+extern int arch_validate_hwbkpt_settings(struct perf_event *bp);
+extern int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
+						unsigned long val, void *data);
+int arch_install_hw_breakpoint(struct perf_event *bp);
+void arch_uninstall_hw_breakpoint(struct perf_event *bp);
+void hw_breakpoint_pmu_read(struct perf_event *bp);
+extern void flush_ptrace_hw_breakpoint(struct task_struct *tsk);
+
+extern struct pmu perf_ops_bp;
+extern void ptrace_triggered(struct perf_event *bp, int nmi,
+			struct perf_sample_data *data, struct pt_regs *regs);
+static inline void hw_breakpoint_disable(void)
+{
+	set_dabr(0);
+}
+
+#else	/* CONFIG_HAVE_HW_BREAKPOINT */
+static inline void hw_breakpoint_disable(void) { }
+#endif	/* CONFIG_HAVE_HW_BREAKPOINT */
+#endif	/* __KERNEL__ */
+#endif	/* _PPC_BOOK3S_64_HW_BREAKPOINT_H */
arch/powerpc/kernel/hw_breakpoint.c new file +338 -0

338 lines not on this page · raw message

arch/powerpc/kernel/Makefile +1 -0
Index: linux-2.6.ppc64_test/arch/powerpc/kernel/Makefile
===================================================================
@@ -34,6 +34,7 @@ obj-y				+= vdso32/
 obj-$(CONFIG_PPC64)		+= setup_64.o sys_ppc32.o \
 				   signal_64.o ptrace32.o \
 				   paca.o nvram_64.o firmware.o
+obj-$(CONFIG_HAVE_HW_BREAKPOINT)	+= hw_breakpoint.o
 obj-$(CONFIG_PPC_BOOK3S_64)	+= cpu_setup_ppc970.o cpu_setup_pa6t.o
 obj64-$(CONFIG_RELOCATABLE)	+= reloc_64.o
 obj-$(CONFIG_PPC_BOOK3E_64)	+= exceptions-64e.o
arch/powerpc/include/asm/processor.h +8 -0
Index: linux-2.6.ppc64_test/arch/powerpc/include/asm/processor.h
===================================================================
@@ -209,6 +209,14 @@ struct thread_struct {
 #ifdef CONFIG_PPC64
 	unsigned long	start_tb;	/* Start purr when proc switched in */
 	unsigned long	accum_tb;	/* Total accumilated purr for process */
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+	struct perf_event *ptrace_bps[HBP_NUM];
+	/*
+	 * Helps identify source of single-step exception and subsequent
+	 * hw-breakpoint enablement
+	 */
+	struct perf_event *last_hit_ubp;
+#endif /* CONFIG_HAVE_HW_BREAKPOINT */
 #endif
 	unsigned long	dabr;		/* Data address breakpoint register */
 #ifdef CONFIG_ALTIVEC
arch/powerpc/kernel/ptrace.c +64 -0

85 lines not on this page · raw message

arch/powerpc/kernel/process.c +6 -0
Index: linux-2.6.ppc64_test/arch/powerpc/kernel/process.c
===================================================================
@@ -462,8 +462,14 @@ struct task_struct *__switch_to(struct t
 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
 	switch_booke_debug_regs(&new->thread);
 #else
+/*
+ * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
+ * schedule DABR
+ */
+#ifndef CONFIG_HAVE_HW_BREAKPOINT
 	if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr))
 		set_dabr(new->thread.dabr);
+#endif /* CONFIG_HAVE_HW_BREAKPOINT */
 #endif
 
 
arch/powerpc/include/asm/cputable.h +4 -0
Index: linux-2.6.ppc64_test/arch/powerpc/include/asm/cputable.h
===================================================================
@@ -516,6 +516,10 @@ static inline int cpu_has_feature(unsign
 		& feature);
 }
 
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+#define HBP_NUM 1
+#endif /* CONFIG_HAVE_HW_BREAKPOINT */
+
 #endif /* !__ASSEMBLY__ */
 
 #endif /* __KERNEL__ */
arch/powerpc/kernel/machine_kexec_64.c +3 -0
Index: linux-2.6.ppc64_test/arch/powerpc/kernel/machine_kexec_64.c
===================================================================
@@ -25,6 +25,7 @@
 #include <asm/sections.h>	/* _end */
 #include <asm/prom.h>
 #include <asm/smp.h>
+#include <asm/hw_breakpoint.h>
 
 int default_machine_kexec_prepare(struct kimage *image)
 {
@@ -165,6 +166,7 @@ static void kexec_smp_down(void *arg)
 	while(kexec_all_irq_disabled == 0)
 		cpu_relax();
 	mb(); /* make sure all irqs are disabled before this */
+	hw_breakpoint_disable();
 	/*
 	 * Now every CPU has IRQs off, we can clear out any pending
 	 * IPIs and be sure that no more will come in after this.
@@ -180,6 +182,7 @@ static void kexec_prepare_cpus_wait(int 
 {
 	int my_cpu, i, notified=-1;
 
+	hw_breakpoint_disable();
 	my_cpu = get_cpu();
 	/* Make sure each CPU has atleast made it to the state we need */
 	for (i=0; i < NR_CPUS; i++) {
arch/powerpc/lib/Makefile +1 -0
Index: linux-2.6.ppc64_test/arch/powerpc/lib/Makefile
===================================================================
@@ -20,6 +20,7 @@ obj-$(CONFIG_PPC64)	+= copypage_64.o cop
 			   memcpy_64.o usercopy_64.o mem_64.o string.o
 obj-$(CONFIG_XMON)	+= sstep.o
 obj-$(CONFIG_KPROBES)	+= sstep.o
+obj-$(CONFIG_HAVE_HW_BREAKPOINT)	+= sstep.o
 
 ifeq ($(CONFIG_PPC64),y)
 obj-$(CONFIG_SMP)	+= locks.o
arch/powerpc/Kconfig +1 -0
Index: linux-2.6.ppc64_test/arch/powerpc/Kconfig
===================================================================
@@ -141,6 +141,7 @@ config PPC
 	select GENERIC_ATOMIC64 if PPC32
 	select HAVE_PERF_EVENTS
 	select HAVE_REGS_AND_STACK_ACCESS_API
+	select HAVE_HW_BREAKPOINT if PERF_EVENTS && PPC_BOOK3S_64
 
 config EARLY_PRINTK
 	bool
#12 lore raw

Re: [Patch 2/4] PPC64-HWBKPT: Implement hw-breakpoints for PowerPC BookIII S

Quoted 3 lines · K.Prasad

Implement perf-events based hw-breakpoint interfaces for PowerPC Book III S processors. These interfaces help arbitrate requests from various users and schedules them as appropriate.

A few comments on the code below...

Quoted 31 lines
arch/powerpc/kernel/hw_breakpoint.c new file +31 -0
+int __kprobes hw_breakpoint_handler(struct die_args *args)
+{
+	bool is_ptrace_bp = false;
+	int rc = NOTIFY_STOP;
+	struct perf_event *bp;
+	struct pt_regs *regs = args->regs;
+	unsigned long dar = regs->dar;
+	int stepped = 1;
+	struct arch_hw_breakpoint *info;
+
+	/* Disable breakpoints during exception handling */
+	set_dabr(0);
+	/*
+	 * The counter may be concurrently released but that can only
+	 * occur from a call_rcu() path. We can then safely fetch
+	 * the breakpoint, use its callback, touch its counter
+	 * while we are in an rcu_read_lock() path.
+	 */
+	rcu_read_lock();
+
+	bp = __get_cpu_var(bp_per_reg);
+	if (!bp)
+		goto out;
+	info = counter_arch_bp(bp);
+	is_ptrace_bp = (bp->overflow_handler == ptrace_triggered) ?
+			true : false;
+
+	/*
+	 * Verify if dar lies within the address range occupied by the symbol
+	 * being watched to filter extraneous exceptions.
+	 */

arch/powerpc/kernel/hw_breakpoint.c line 221

Quoted 10 lines
arch/powerpc/kernel/hw_breakpoint.c new file +10 -0
+	if (!((bp->attr.bp_addr <= dar) &&
+	    (dar <= (bp->attr.bp_addr + bp->attr.bp_len))) &&
+	    (!is_ptrace_bp))
+		/*
+		 * This exception is triggered not because of a memory access on
+		 * the monitored variable but in the double-word address range
+		 * in which it is contained. We will consume this exception,
+		 * considering it as 'noise'.
+		 */
+		goto restore_bp;

At this point we have to do the single-stepping, because the NIP is still pointing at the instruction that caused the exception, and if we just return to it with DABR set we won't make any progress, we'll just take the same exception again immediately.

Quoted 25 lines
arch/powerpc/kernel/hw_breakpoint.c new file +25 -0
+/*
+ * Handle single-step exceptions following a DABR hit.
+ */
+int __kprobes single_step_dabr_instruction(struct die_args *args)
+{
+	struct pt_regs *regs = args->regs;
+	struct perf_event *bp = NULL;
+	struct arch_hw_breakpoint *bp_info;
+
+	bp = current->thread.last_hit_ubp;
+	/*
+	 * Check if we are single-stepping as a result of a
+	 * previous HW Breakpoint exception
+	 */
+	if (!bp)
+		return NOTIFY_DONE;
+
+	bp_info = counter_arch_bp(bp);
+
+	/*
+	 * We shall invoke the user-defined callback function in the single
+	 * stepping handler to confirm to 'trigger-after-execute' semantics
+	 */
+	perf_bp_event(bp, regs);
+

arch/powerpc/kernel/hw_breakpoint.c line 301

Quoted 10 lines
arch/powerpc/kernel/hw_breakpoint.c new file +10 -0
+	/*
+	 * Do not disable MSR_SE if the process was already in
+	 * single-stepping mode.
+	 */
+	if (!test_thread_flag(TIF_SINGLESTEP))
+		regs->msr &= ~MSR_SE;
+
+	set_dabr(bp_info->address | bp_info->type | DABR_TRANSLATION);
+	return NOTIFY_STOP;
+}

Nowhere in here do we reset current->thread.last_hit_ubp, yet other parts of the code assume that .last_hit_ubp != NULL means that we are currently single-stepping. I think we need to clear .last_hit_ubp here.

Quoted 7 lines

Index: linux-2.6.ppc64_test/arch/powerpc/kernel/process.c
===================================================================

arch/powerpc/kernel/process.c +0 -0
@@ -462,8 +462,14 @@ struct task_struct *__switch_to(struct t
 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
 	switch_booke_debug_regs(&new->thread);

arch/powerpc/kernel/process.c struct task_struct *__switch_to(struct t · line 473

Quoted 10 lines
arch/powerpc/kernel/process.c +6 -0
 #else
+/*
+ * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
+ * schedule DABR
+ */
+#ifndef CONFIG_HAVE_HW_BREAKPOINT
 	if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr))
 		set_dabr(new->thread.dabr);
+#endif /* CONFIG_HAVE_HW_BREAKPOINT */
 #endif

Have you checked all the places that set_dabr is called to see whether they are still needed with CONFIG_HAVE_HW_BREAKPOINT?

arch/powerpc/include/asm/cputable.h static inline int cpu_has_feature(unsign · line 521

Quoted 11 lines

Index: linux-2.6.ppc64_test/arch/powerpc/include/asm/cputable.h
===================================================================

arch/powerpc/include/asm/cputable.h +3 -0
@@ -516,6 +516,10 @@ static inline int cpu_has_feature(unsign
 		& feature);
 }
 
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+#define HBP_NUM 1
+#endif /* CONFIG_HAVE_HW_BREAKPOINT */

Why is this defined here, not in <asm/hw_breakpoint.h> ?

Paul.

author
#13 lore raw

Re: [Patch 2/4] PPC64-HWBKPT: Implement hw-breakpoints for PowerPC BookIII S

Quoted 8 lines · Paul Mackerras
Quoted 3 lines · K.Prasad

Implement perf-events based hw-breakpoint interfaces for PowerPC Book III S processors. These interfaces help arbitrate requests from various users and schedules them as appropriate.

A few comments on the code below...

I've posted a new patchset that addresses almost all of your comments. Please find them here: linuxppc-dev message-id:
[email protected]

Quoted 38 lines
Quoted 38 lines
arch/powerpc/kernel/hw_breakpoint.c new file +38 -0
+int __kprobes hw_breakpoint_handler(struct die_args *args)
+{
+	bool is_ptrace_bp = false;
+	int rc = NOTIFY_STOP;
+	struct perf_event *bp;
+	struct pt_regs *regs = args->regs;
+	unsigned long dar = regs->dar;
+	int stepped = 1;
+	struct arch_hw_breakpoint *info;
+
+	/* Disable breakpoints during exception handling */
+	set_dabr(0);
+	/*
+	 * The counter may be concurrently released but that can only
+	 * occur from a call_rcu() path. We can then safely fetch
+	 * the breakpoint, use its callback, touch its counter
+	 * while we are in an rcu_read_lock() path.
+	 */
+	rcu_read_lock();
+
+	bp = __get_cpu_var(bp_per_reg);
+	if (!bp)
+		goto out;
+	info = counter_arch_bp(bp);
+	is_ptrace_bp = (bp->overflow_handler == ptrace_triggered) ?
+			true : false;
+
+	/*
+	 * Verify if dar lies within the address range occupied by the symbol
+	 * being watched to filter extraneous exceptions.
+	 */
+	if (!((bp->attr.bp_addr <= dar) &&
+	    (dar <= (bp->attr.bp_addr + bp->attr.bp_len))) &&
+	    (!is_ptrace_bp))
+		/*
+		 * This exception is triggered not because of a memory access on
+		 * the monitored variable but in the double-word address range
+		 * in which it is contained. We will consume this exception,
Quoted 9 lines

arch/powerpc/kernel/hw_breakpoint.c line 221

Quoted 3 lines
arch/powerpc/kernel/hw_breakpoint.c new file +3 -0
+		 * considering it as 'noise'.
+		 */
+		goto restore_bp;

At this point we have to do the single-stepping, because the NIP is still pointing at the instruction that caused the exception, and if we just return to it with DABR set we won't make any progress, we'll just take the same exception again immediately.

I don't know how I convinced myself earlier that this would work :-)

Given that the instructions needs to be emulated in the same manner as others, I've re-used the ptrace_bps[] member in 'thread_struct' as a flag to indicate such breakpoints. This will be later checked in single_step_dabr_instruction() to prevent invocation of perf_event_bp().

Quoted 32 lines
Quoted 32 lines
arch/powerpc/kernel/hw_breakpoint.c new file +32 -0
+/*
+ * Handle single-step exceptions following a DABR hit.
+ */
+int __kprobes single_step_dabr_instruction(struct die_args *args)
+{
+	struct pt_regs *regs = args->regs;
+	struct perf_event *bp = NULL;
+	struct arch_hw_breakpoint *bp_info;
+
+	bp = current->thread.last_hit_ubp;
+	/*
+	 * Check if we are single-stepping as a result of a
+	 * previous HW Breakpoint exception
+	 */
+	if (!bp)
+		return NOTIFY_DONE;
+
+	bp_info = counter_arch_bp(bp);
+
+	/*
+	 * We shall invoke the user-defined callback function in the single
+	 * stepping handler to confirm to 'trigger-after-execute' semantics
+	 */
+	perf_bp_event(bp, regs);
+
+	/*
+	 * Do not disable MSR_SE if the process was already in
+	 * single-stepping mode.
+	 */
+	if (!test_thread_flag(TIF_SINGLESTEP))
+		regs->msr &= ~MSR_SE;
+
Quoted 9 lines

arch/powerpc/kernel/hw_breakpoint.c line 301

Quoted 3 lines
arch/powerpc/kernel/hw_breakpoint.c new file +3 -0
+	set_dabr(bp_info->address | bp_info->type | DABR_TRANSLATION);
+	return NOTIFY_STOP;
+}

Nowhere in here do we reset current->thread.last_hit_ubp, yet other parts of the code assume that .last_hit_ubp != NULL means that we are currently single-stepping. I think we need to clear .last_hit_ubp here.

True, made the change.

Quoted 12 lines
Quoted 12 lines

Index: linux-2.6.ppc64_test/arch/powerpc/kernel/process.c
===================================================================

arch/powerpc/kernel/process.c +4 -0
@@ -462,8 +462,14 @@ struct task_struct *__switch_to(struct t
 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
 	switch_booke_debug_regs(&new->thread);
 #else
+/*
+ * For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
+ * schedule DABR
+ */
Quoted 9 lines

arch/powerpc/kernel/process.c struct task_struct *__switch_to(struct t · line 473

Quoted 5 lines
arch/powerpc/kernel/process.c +2 -0
+#ifndef CONFIG_HAVE_HW_BREAKPOINT
 	if (unlikely(__get_cpu_var(current_dabr) != new->thread.dabr))
 		set_dabr(new->thread.dabr);
+#endif /* CONFIG_HAVE_HW_BREAKPOINT */
 #endif

Have you checked all the places that set_dabr is called to see whether they are still needed with CONFIG_HAVE_HW_BREAKPOINT?

Yes. All invocations of set_dabr() or their caller functions are enclosed within #ifdef CONFIG_HAVE_HW_BREAKPOINT.

Quoted 8 lines
Quoted 8 lines

Index: linux-2.6.ppc64_test/arch/powerpc/include/asm/cputable.h
===================================================================

arch/powerpc/include/asm/cputable.h +0 -0
@@ -516,6 +516,10 @@ static inline int cpu_has_feature(unsign
 		& feature);
 }
 
Quoted 6 lines

arch/powerpc/include/asm/cputable.h static inline int cpu_has_feature(unsign · line 521

Quoted 3 lines
arch/powerpc/include/asm/cputable.h +3 -0
+#ifdef CONFIG_HAVE_HW_BREAKPOINT
+#define HBP_NUM 1
+#endif /* CONFIG_HAVE_HW_BREAKPOINT */

Why is this defined here, not in <asm/hw_breakpoint.h> ?

We need HBP_NUM value for arch/powerpc/include/asm/processor.h and if asm/hw_breakpoint.h is included there, it caused recursive dependancies. After more discussions with the community (as in
linuxppc-dev: message-id: [email protected]) we finally decided to put it in cputable.h

We will have more related definitions to accompany this when support for BookIII E is brought in.

Thanks,
K.Prasad

author
#14 lore raw

PATCH 3/4 PPC64-HWBKPT: Handle concurrent alignment interrupts

In reply to a message not in the archive

An alignment interrupt may intervene between a DSI/hw-breakpoint exception and the single-step exception. Enable the alignment interrupt (through modifications to emulate_single_step()) to notify the single-step exception handler for proper restoration of hw-breakpoints.

---
 arch/powerpc/kernel/traps.c |    7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
arch/powerpc/kernel/traps.c +2 -5
Index: linux-2.6.ppc64_test/arch/powerpc/kernel/traps.c
===================================================================
@@ -602,7 +602,7 @@ void RunModeException(struct pt_regs *re
 
 void __kprobes single_step_exception(struct pt_regs *regs)
 {
-	regs->msr &= ~(MSR_SE | MSR_BE);  /* Turn off 'trace' bits */
+	clear_single_step(regs);
 
 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
 					5, SIGTRAP) == NOTIFY_STOP)
@@ -621,10 +621,7 @@ void __kprobes single_step_exception(str
  */
 static void emulate_single_step(struct pt_regs *regs)
 {
-	if (single_stepping(regs)) {
-		clear_single_step(regs);
-		_exception(SIGTRAP, regs, TRAP_TRACE, 0);
-	}
+	single_step_exception(regs);
 }
 
 static inline int __parse_fpscr(unsigned long fpscr)
#15 lore raw

Re: [Patch 3/4] PPC64-HWBKPT: Handle concurrent alignment interrupts

Quoted 24 lines · K.Prasad

An alignment interrupt may intervene between a DSI/hw-breakpoint exception and the single-step exception. Enable the alignment interrupt (through modifications to emulate_single_step()) to notify the single-step exception handler for proper restoration of hw-breakpoints.

---
 arch/powerpc/kernel/traps.c |    7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

Index: linux-2.6.ppc64_test/arch/powerpc/kernel/traps.c
===================================================================

arch/powerpc/kernel/traps.c +1 -1
@@ -602,7 +602,7 @@ void RunModeException(struct pt_regs *re
 
 void __kprobes single_step_exception(struct pt_regs *regs)
 {
-	regs->msr &= ~(MSR_SE | MSR_BE);  /* Turn off 'trace' bits */
+	clear_single_step(regs);
 
 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
 					5, SIGTRAP) == NOTIFY_STOP)

arch/powerpc/kernel/traps.c void __kprobes single_step_exception(str · line 625

Quoted 9 lines
arch/powerpc/kernel/traps.c +1 -4
@@ -621,10 +621,7 @@ void __kprobes single_step_exception(str
  */
 static void emulate_single_step(struct pt_regs *regs)
 {
-	if (single_stepping(regs)) {
-		clear_single_step(regs);
-		_exception(SIGTRAP, regs, TRAP_TRACE, 0);
-	}
+	single_step_exception(regs);
 }

We still need the if (single_stepping(regs)) in emulate_single_step. We don't want to send the process a SIGTRAP every time it gets an alignment interrupt. :)

Paul.

author
#16 lore raw

Re: [Patch 3/4] PPC64-HWBKPT: Handle concurrent alignment interrupts

Quoted 40 lines · Paul Mackerras
Quoted 24 lines · K.Prasad

An alignment interrupt may intervene between a DSI/hw-breakpoint exception and the single-step exception. Enable the alignment interrupt (through modifications to emulate_single_step()) to notify the single-step exception handler for proper restoration of hw-breakpoints.

---
 arch/powerpc/kernel/traps.c |    7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

Index: linux-2.6.ppc64_test/arch/powerpc/kernel/traps.c
===================================================================

arch/powerpc/kernel/traps.c +1 -1
@@ -602,7 +602,7 @@ void RunModeException(struct pt_regs *re
 
 void __kprobes single_step_exception(struct pt_regs *regs)
 {
-	regs->msr &= ~(MSR_SE | MSR_BE);  /* Turn off 'trace' bits */
+	clear_single_step(regs);
 
 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
 					5, SIGTRAP) == NOTIFY_STOP)

arch/powerpc/kernel/traps.c void __kprobes single_step_exception(str · line 625

Quoted 9 lines
arch/powerpc/kernel/traps.c +1 -4
@@ -621,10 +621,7 @@ void __kprobes single_step_exception(str
  */
 static void emulate_single_step(struct pt_regs *regs)
 {
-	if (single_stepping(regs)) {
-		clear_single_step(regs);
-		_exception(SIGTRAP, regs, TRAP_TRACE, 0);
-	}
+	single_step_exception(regs);
 }

We still need the if (single_stepping(regs)) in emulate_single_step. We don't want to send the process a SIGTRAP every time it gets an alignment interrupt. :)

Quoted 1 line

Paul.

Agreed, and made changes to that effect in version XXII (as seen in patch linuxppc-dev message-id: [email protected]).

Thanks,
K.Prasad

author
#17 lore raw

PATCH 4/4 PPC64-HWBKPT: Enable hw-breakpoints while handling intervening signals

In reply to a message not in the archive

A signal delivered between a hw_breakpoint_handler() and the single_step_dabr_instruction() will not have the breakpoint active during signal handling (since breakpoint will not be restored through single-stepping due to absence of MSR_SE bit on the signal frame). Enable breakpoints before signal delivery.

Restore hw-breakpoints if the user-context is altered in the signal handler.

---
 arch/powerpc/include/asm/hw_breakpoint.h |    2 ++
 arch/powerpc/kernel/hw_breakpoint.c      |   16 ++++++++++++++++
 arch/powerpc/kernel/signal.c             |    3 +++
 arch/powerpc/kernel/signal_32.c          |    2 ++
 arch/powerpc/kernel/signal_64.c          |    2 ++
 5 files changed, 25 insertions(+)
arch/powerpc/include/asm/hw_breakpoint.h +2 -0
Index: linux-2.6.ppc64_test/arch/powerpc/include/asm/hw_breakpoint.h
===================================================================
@@ -65,9 +65,11 @@ static inline void hw_breakpoint_disable
 {
 	set_dabr(0);
 }
+extern void thread_change_pc(struct task_struct *tsk);
 
 #else	/* CONFIG_HAVE_HW_BREAKPOINT */
 static inline void hw_breakpoint_disable(void) { }
+static inline void thread_change_pc(struct task_struct *tsk) { }
 #endif	/* CONFIG_HAVE_HW_BREAKPOINT */
 #endif	/* __KERNEL__ */
 #endif	/* _PPC_BOOK3S_64_HW_BREAKPOINT_H */
arch/powerpc/kernel/hw_breakpoint.c +16 -0
Index: linux-2.6.ppc64_test/arch/powerpc/kernel/hw_breakpoint.c
===================================================================
@@ -176,6 +176,22 @@ int arch_validate_hwbkpt_settings(struct
 }
 
 /*
+ * Restores the breakpoint on the debug registers.
+ * Invoke this function if it is known that the execution context is about to
+ * change to cause loss of MSR_SE settings.
+ */
+void thread_change_pc(struct task_struct *tsk)
+{
+	struct arch_hw_breakpoint *info;
+
+	if (likely(!tsk->thread.last_hit_ubp))
+		return;
+
+	info = counter_arch_bp(tsk->thread.last_hit_ubp);
+	set_dabr(info->address | info->type | DABR_TRANSLATION);
+}
+
+/*
  * Handle debug exception notifications.
  */
 int __kprobes hw_breakpoint_handler(struct die_args *args)
arch/powerpc/kernel/signal.c +3 -0
Index: linux-2.6.ppc64_test/arch/powerpc/kernel/signal.c
===================================================================
@@ -11,6 +11,7 @@
 
 #include <linux/tracehook.h>
 #include <linux/signal.h>
+#include <asm/hw_breakpoint.h>
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
 
@@ -149,6 +150,8 @@ static int do_signal_pending(sigset_t *o
 	if (current->thread.dabr)
 		set_dabr(current->thread.dabr);
 #endif
+	 /* Re-enable the breakpoints for the signal stack */
+	thread_change_pc(current);
 
 	if (is32) {
         	if (ka.sa.sa_flags & SA_SIGINFO)
arch/powerpc/kernel/signal_64.c +2 -0
Index: linux-2.6.ppc64_test/arch/powerpc/kernel/signal_64.c
===================================================================
@@ -33,6 +33,7 @@
 #include <asm/cacheflush.h>
 #include <asm/syscalls.h>
 #include <asm/vdso.h>
+#include <asm/hw_breakpoint.h>
 
 #include "signal.h"
 
@@ -312,6 +313,7 @@ int sys_swapcontext(struct ucontext __us
 		    || __copy_to_user(&old_ctx->uc_sigmask,
 				      &current->blocked, sizeof(sigset_t)))
 			return -EFAULT;
+		thread_change_pc(current);
 	}
 	if (new_ctx == NULL)
 		return 0;
arch/powerpc/kernel/signal_32.c +2 -0
Index: linux-2.6.ppc64_test/arch/powerpc/kernel/signal_32.c
===================================================================
@@ -42,6 +42,7 @@
 #include <asm/syscalls.h>
 #include <asm/sigcontext.h>
 #include <asm/vdso.h>
+#include <asm/hw_breakpoint.h>
 #ifdef CONFIG_PPC64
 #include "ppc32.h"
 #include <asm/unistd.h>
@@ -996,6 +997,7 @@ long sys_swapcontext(struct ucontext __u
 		    || put_sigset_t(&old_ctx->uc_sigmask, &current->blocked)
 		    || __put_user(to_user_ptr(mctx), &old_ctx->uc_regs))
 			return -EFAULT;
+		thread_change_pc(current);
 	}
 	if (new_ctx == NULL)
 		return 0;
#18 lore raw

Re: [Patch 4/4] PPC64-HWBKPT: Enable hw-breakpoints while handling intervening signals

Quoted 7 lines · K.Prasad

A signal delivered between a hw_breakpoint_handler() and the single_step_dabr_instruction() will not have the breakpoint active during signal handling (since breakpoint will not be restored through single-stepping due to absence of MSR_SE bit on the signal frame). Enable breakpoints before signal delivery.

Restore hw-breakpoints if the user-context is altered in the signal handler.

...

Quoted 5 lines

/*
+ * Restores the breakpoint on the debug registers.
+ * Invoke this function if it is known that the execution context is about to
+ * change to cause loss of MSR_SE settings.
+ */

arch/powerpc/kernel/hw_breakpoint.c int arch_validate_hwbkpt_settings(struct · line 192

Quoted 10 lines
arch/powerpc/kernel/hw_breakpoint.c +10 -0
+void thread_change_pc(struct task_struct *tsk)
+{
+	struct arch_hw_breakpoint *info;
+
+	if (likely(!tsk->thread.last_hit_ubp))
+		return;
+
+	info = counter_arch_bp(tsk->thread.last_hit_ubp);
+	set_dabr(info->address | info->type | DABR_TRANSLATION);
+}

I think this function needs to clear current->thread.last_hit_ubp. You also need to pass the regs to it so it can clear MSR_SE from regs->msr.

Quoted 13 lines

Index: linux-2.6.ppc64_test/arch/powerpc/kernel/signal_64.c
===================================================================

arch/powerpc/kernel/signal_64.c +1 -0
@@ -33,6 +33,7 @@
 #include <asm/cacheflush.h>
 #include <asm/syscalls.h>
 #include <asm/vdso.h>
+#include <asm/hw_breakpoint.h>
 
 #include "signal.h"
 

arch/powerpc/kernel/signal_64.c int sys_swapcontext(struct ucontext __us · line 316

Quoted 4 lines
arch/powerpc/kernel/signal_64.c +1 -0
@@ -312,6 +313,7 @@ int sys_swapcontext(struct ucontext __us
 		    || __copy_to_user(&old_ctx->uc_sigmask,
 				      &current->blocked, sizeof(sigset_t)))
 			return -EFAULT;
+		thread_change_pc(current);

This is in the part of the code that is saving the old context, after the old context has been saved. We need to do it before saving the old context so that the saved context doesn't have the MSR_SE bit set in its MSR image. And similarly in the 32-bit version.

In fact, we probably don't need to do anything here. We should never get into a system call (such as sys_swapcontext or sys_sigreturn) when single-stepping a load or store that caused a DABR match. If we do, something very strange is happening. So I would leave out the swapcontext changes for this version.

Paul.