Skip to contentSkip to conversation

Improvements and minor fixes to HW Breakpoint interface

PATCH In review K.Prasad 3 participants 8 messages May 19, 2009 – May 20, 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): 20090519161720.374172804@prasadkr_t60p.in.ibm.com

K.Prasad

author
#1 lore raw

PATCH 1/2 Improvements and minor fixes to HW Breakpoint interface

This patch brings a couple of changes to the HW Breakpoint infrastructure:

- Set/clear TIF_DEBUG task flag in <un>register_user_hw_breakpoint() instead of being done by users of this interface (such as ptrace).

- Modify return code of hw_breakpoint_handler() to NOTIFY_STOP if triggered due to lazy debug register switching.

---
 arch/x86/kernel/hw_breakpoint.c |    4 +++-
 arch/x86/kernel/ptrace.c        |    4 +---
 kernel/hw_breakpoint.c          |   16 +++++++++++++---
 3 files changed, 17 insertions(+), 7 deletions(-)
arch/x86/kernel/hw_breakpoint.c +3 -1
Index: linux-2.6-tip.hbkpt/arch/x86/kernel/hw_breakpoint.c
===================================================================
@@ -346,8 +346,10 @@ int __kprobes hw_breakpoint_handler(stru
 		 * or due to the delay between updates of hbp_kernel_pos
 		 * and this_hbp_kernel.
 		 */
-		if (!bp)
+		if (!bp) {
+			rc = NOTIFY_STOP;
 			continue;
+		}
 
 		(bp->triggered)(bp, args->regs);
 		/*
arch/x86/kernel/ptrace.c +1 -3
Index: linux-2.6-tip.hbkpt/arch/x86/kernel/ptrace.c
===================================================================
@@ -530,9 +530,7 @@ restore:
 				bp->info.len = len;
 				bp->info.type = type;
 				rc = register_user_hw_breakpoint(tsk, bp);
-				if (!rc)
-					set_tsk_thread_flag(tsk, TIF_DEBUG);
-				else
+				if (rc)
 					kfree(bp);
 			}
 		} else
kernel/hw_breakpoint.c +13 -3
Index: linux-2.6-tip.hbkpt/kernel/hw_breakpoint.c
===================================================================
@@ -233,6 +233,8 @@ int register_user_hw_breakpoint(struct t
 			break;
 		}
 	}
+	if (!rc)
+		set_tsk_thread_flag(tsk, TIF_DEBUG);
 
 	spin_unlock_bh(&hw_breakpoint_lock);
 	return rc;
@@ -272,15 +274,23 @@ void unregister_user_hw_breakpoint(struc
 						struct hw_breakpoint *bp)
 {
 	struct thread_struct *thread = &(tsk->thread);
-	int i;
+	int i, pos = -1, clear_tsk_debug_counter = 0;
 
 	spin_lock_bh(&hw_breakpoint_lock);
 	for (i = 0; i < hbp_kernel_pos; i++) {
+		if (thread->hbp[i])
+			clear_tsk_debug_counter++;
 		if (bp == thread->hbp[i]) {
-			__unregister_user_hw_breakpoint(i, tsk);
-			break;
+			clear_tsk_debug_counter--;
+			pos = i;
 		}
 	}
+	if (pos >= 0)
+		__unregister_user_hw_breakpoint(pos, tsk);
+
+	if (!clear_tsk_debug_counter)
+		clear_tsk_thread_flag(tsk, TIF_DEBUG);
+
 	spin_unlock_bh(&hw_breakpoint_lock);
 }
 EXPORT_SYMBOL_GPL(unregister_user_hw_breakpoint);
#2 lore raw
Quoted 21 lines · K.Prasad

This patch brings a couple of changes to the HW Breakpoint infrastructure:

- Set/clear TIF_DEBUG task flag in <un>register_user_hw_breakpoint() instead of being done by users of this interface (such as ptrace).

- Modify return code of hw_breakpoint_handler() to NOTIFY_STOP if triggered due to lazy debug register switching.

---
 arch/x86/kernel/hw_breakpoint.c |    4 +++-
 arch/x86/kernel/ptrace.c        |    4 +---
 kernel/hw_breakpoint.c          |   16 +++++++++++++---
 3 files changed, 17 insertions(+), 7 deletions(-)

Index: linux-2.6-tip.hbkpt/arch/x86/kernel/hw_breakpoint.c
===================================================================

arch/x86/kernel/hw_breakpoint.c +0 -0
@@ -346,8 +346,10 @@ int __kprobes hw_breakpoint_handler(stru
 		 * or due to the delay between updates of hbp_kernel_pos

arch/x86/kernel/hw_breakpoint.c int __kprobes hw_breakpoint_handler(stru · line 355

Quoted 10 lines
arch/x86/kernel/hw_breakpoint.c +3 -1
 		 * and this_hbp_kernel.
 		 */
-		if (!bp)
+		if (!bp) {
+			rc = NOTIFY_STOP;
 			continue;
+		}
 
 		(bp->triggered)(bp, args->regs);
 		/*

This doesn't look right. You want to return NOTIFY_DONE if any breakpoints were triggered or any non-breakpoint bits were set in DR6, right? Otherwise return NOTIFY_STOP. So this code should be:

		if (!bp)
			continue;

		rc = NOTIFY_DONE;
		(bp->triggered)(bp, args->regs);

and take out the "rc = NOTIFY_DONE" line in the i < hbp_kernel_pos case earlier.

It also would be a good idea to add a comment near the start of the function explaining the return value (just paraphrase what I wrote above).

Quoted 29 lines

Index: linux-2.6-tip.hbkpt/arch/x86/kernel/ptrace.c
===================================================================

arch/x86/kernel/ptrace.c +1 -3
@@ -530,9 +530,7 @@ restore:
 				bp->info.len = len;
 				bp->info.type = type;
 				rc = register_user_hw_breakpoint(tsk, bp);
-				if (!rc)
-					set_tsk_thread_flag(tsk, TIF_DEBUG);
-				else
+				if (rc)
 					kfree(bp);
 			}
 		} else

Index: linux-2.6-tip.hbkpt/kernel/hw_breakpoint.c
===================================================================

kernel/hw_breakpoint.c +2 -0
@@ -233,6 +233,8 @@ int register_user_hw_breakpoint(struct t
 			break;
 		}
 	}
+	if (!rc)
+		set_tsk_thread_flag(tsk, TIF_DEBUG);
 
 	spin_unlock_bh(&hw_breakpoint_lock);
 	return rc;

kernel/hw_breakpoint.c void unregister_user_hw_breakpoint(struc · line 277

Quoted 5 lines
kernel/hw_breakpoint.c +1 -1
@@ -272,15 +274,23 @@ void unregister_user_hw_breakpoint(struc
 						struct hw_breakpoint *bp)
 {
 	struct thread_struct *thread = &(tsk->thread);
-	int i;
+	int i, pos = -1, clear_tsk_debug_counter = 0;

I think "hbp_counter" would be a better name. It's up to you.

Quoted 7 lines
kernel/hw_breakpoint.c +2 -1
 	spin_lock_bh(&hw_breakpoint_lock);
 	for (i = 0; i < hbp_kernel_pos; i++) {
+		if (thread->hbp[i])
+			clear_tsk_debug_counter++;
 		if (bp == thread->hbp[i]) {
-			__unregister_user_hw_breakpoint(i, tsk);

kernel/hw_breakpoint.c void unregister_user_hw_breakpoint(struc · line 292

Quoted 10 lines
kernel/hw_breakpoint.c +7 -1
-			break;
+			clear_tsk_debug_counter--;
+			pos = i;
 		}
 	}
+	if (pos >= 0)
+		__unregister_user_hw_breakpoint(pos, tsk);
+
+	if (!clear_tsk_debug_counter)
+		clear_tsk_thread_flag(tsk, TIF_DEBUG);

This logic could be rearranged a little:

	for (i = 0; i < hbp_kernel_pos; i++) {
		if (thread->hbp[i])
			clear_tsk_debug_counter++;
		if (bp == thread->hbp[i])
			pos = i;
	}
	if (pos >= 0) {
		__unregister_user_hw_breakpoint(pos, tsk);
		clear_tsk_debug_counter--;
	}
	if (!clear_tsk_debug_counter)
		clear_tsk_thread_flag(tsk, TIF_DEBUG);

Not a big deal; this way is a little more robust in case two thread->hbp[] entries somehow contain the same pointer.

kernel/hw_breakpoint.c void unregister_user_hw_breakpoint(struc · line 296

Quoted 4 lines
kernel/hw_breakpoint.c +1 -0
+
 	spin_unlock_bh(&hw_breakpoint_lock);
 }
 EXPORT_SYMBOL_GPL(unregister_user_hw_breakpoint);

Alan Stern

author
#3 lore raw
Quoted 31 lines · Alan Stern
Quoted 29 lines · K.Prasad

This patch brings a couple of changes to the HW Breakpoint infrastructure:

- Set/clear TIF_DEBUG task flag in <un>register_user_hw_breakpoint() instead of being done by users of this interface (such as ptrace).

- Modify return code of hw_breakpoint_handler() to NOTIFY_STOP if triggered due to lazy debug register switching.

---
 arch/x86/kernel/hw_breakpoint.c |    4 +++-
 arch/x86/kernel/ptrace.c        |    4 +---
 kernel/hw_breakpoint.c          |   16 +++++++++++++---
 3 files changed, 17 insertions(+), 7 deletions(-)

Index: linux-2.6-tip.hbkpt/arch/x86/kernel/hw_breakpoint.c
===================================================================

arch/x86/kernel/hw_breakpoint.c +3 -1
@@ -346,8 +346,10 @@ int __kprobes hw_breakpoint_handler(stru
 		 * or due to the delay between updates of hbp_kernel_pos
 		 * and this_hbp_kernel.
 		 */
-		if (!bp)
+		if (!bp) {
+			rc = NOTIFY_STOP;
 			continue;
+		}
 
Quoted 7 lines

arch/x86/kernel/hw_breakpoint.c int __kprobes hw_breakpoint_handler(stru · line 355

Quoted 2 lines
arch/x86/kernel/hw_breakpoint.c +0 -0
 		(bp->triggered)(bp, args->regs);
 		/*

This doesn't look right. You want to return NOTIFY_DONE if any breakpoints were triggered or any non-breakpoint bits were set in DR6, right? Otherwise return NOTIFY_STOP. So this code should be:

Agreed, we want to return NOTIFY_DONE in the above two cases and the patch does the same.

If there are any more breakpoints (basically more trap bits in DR6 set) that require handling, the "continue" statement allows it to be done.

If the non-breakpoint bits were set in DR6, then "rc" value is over-written in hw_breakpoint_handler() below the "if (!bp)" check.

	if (dr6 & (~DR_TRAP_BITS))
		rc = NOTIFY_DONE;

The issue in the original hw_breakpoint_handler() is the return of NOTIFY_DONE during lazy debug register switching or mismatched this_hbp_kernel[], that may cause SIGTRAP signal for user-space in do_debug() function.

Quoted 9 lines
		if (!bp)
			continue;

		rc = NOTIFY_DONE;
		(bp->triggered)(bp, args->regs);

and take out the "rc = NOTIFY_DONE" line in the i < hbp_kernel_pos case earlier.

This change will unconditionally return NOTIFY_DONE even if exception was triggered due to a single kernel-space HW breakpoint.

Quoted 5 lines

It also would be a good idea to add a comment near the start of the function explaining the return value (just paraphrase what I wrote above).

I've added a descriptive comment. Let me know if it can be improved.

Quoted 26 lines
Quoted 26 lines

Index: linux-2.6-tip.hbkpt/arch/x86/kernel/ptrace.c
===================================================================

arch/x86/kernel/ptrace.c +1 -3
@@ -530,9 +530,7 @@ restore:
 				bp->info.len = len;
 				bp->info.type = type;
 				rc = register_user_hw_breakpoint(tsk, bp);
-				if (!rc)
-					set_tsk_thread_flag(tsk, TIF_DEBUG);
-				else
+				if (rc)
 					kfree(bp);
 			}
 		} else

Index: linux-2.6-tip.hbkpt/kernel/hw_breakpoint.c
===================================================================

kernel/hw_breakpoint.c +2 -0
@@ -233,6 +233,8 @@ int register_user_hw_breakpoint(struct t
 			break;
 		}
 	}
+	if (!rc)
+		set_tsk_thread_flag(tsk, TIF_DEBUG);
 
Quoted 11 lines

kernel/hw_breakpoint.c void unregister_user_hw_breakpoint(struc · line 277

Quoted 8 lines
kernel/hw_breakpoint.c +1 -1
 	spin_unlock_bh(&hw_breakpoint_lock);
 	return rc;
@@ -272,15 +274,23 @@ void unregister_user_hw_breakpoint(struc
 						struct hw_breakpoint *bp)
 {
 	struct thread_struct *thread = &(tsk->thread);
-	int i;
+	int i, pos = -1, clear_tsk_debug_counter = 0;

I think "hbp_counter" would be a better name. It's up to you.

Changed.

Quoted 33 lines
Quoted 7 lines
kernel/hw_breakpoint.c +2 -1
 	spin_lock_bh(&hw_breakpoint_lock);
 	for (i = 0; i < hbp_kernel_pos; i++) {
+		if (thread->hbp[i])
+			clear_tsk_debug_counter++;
 		if (bp == thread->hbp[i]) {
-			__unregister_user_hw_breakpoint(i, tsk);

kernel/hw_breakpoint.c void unregister_user_hw_breakpoint(struc · line 291

Quoted 10 lines
kernel/hw_breakpoint.c +7 -1
-			break;
+			clear_tsk_debug_counter--;
+			pos = i;
 		}
 	}
+	if (pos >= 0)
+		__unregister_user_hw_breakpoint(pos, tsk);
+
+	if (!clear_tsk_debug_counter)
+		clear_tsk_thread_flag(tsk, TIF_DEBUG);

This logic could be rearranged a little:

	for (i = 0; i < hbp_kernel_pos; i++) {
		if (thread->hbp[i])
			clear_tsk_debug_counter++;
		if (bp == thread->hbp[i])
			pos = i;
	}
	if (pos >= 0) {
		__unregister_user_hw_breakpoint(pos, tsk);
		clear_tsk_debug_counter--;
	}
	if (!clear_tsk_debug_counter)
		clear_tsk_thread_flag(tsk, TIF_DEBUG);
Quoted 3 lines

Not a big deal; this way is a little more robust in case two thread->hbp[] entries somehow contain the same pointer.

Ok. I have changed it.

Quoted 7 lines

kernel/hw_breakpoint.c void unregister_user_hw_breakpoint(struc · line 295

Quoted 4 lines
kernel/hw_breakpoint.c +1 -0
+
 	spin_unlock_bh(&hw_breakpoint_lock);
 }
 EXPORT_SYMBOL_GPL(unregister_user_hw_breakpoint);

Alan Stern

Please find the updated patch below and kindly let me know your comments on the same.

Improvements and minor fixes to HW Breakpoint interface

This patch brings a couple of changes to the HW Breakpoint infrastructure:

- Set/clear TIF_DEBUG task flag in <un>register_user_hw_breakpoint() instead of being done by users of this interface (such as ptrace).

- Modify return code of hw_breakpoint_handler() to NOTIFY_STOP if triggered due to lazy debug register switching.

---
 arch/x86/kernel/hw_breakpoint.c |   17 ++++++++++++++++-
 arch/x86/kernel/ptrace.c        |    4 +---
 kernel/hw_breakpoint.c          |   19 ++++++++++++++-----
 3 files changed, 31 insertions(+), 9 deletions(-)
arch/x86/kernel/hw_breakpoint.c +16 -1
Index: linux-2.6-tip.hbkpt/arch/x86/kernel/hw_breakpoint.c
===================================================================
@@ -296,6 +296,19 @@ void arch_flush_thread_hw_breakpoint(str
 
 /*
  * Handle debug exception notifications.
+ *
+ * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below.
+ *
+ * NOTIFY_DONE returned if one of the following conditions is true.
+ * i) When the causative address is from user-space and the exception
+ * is a valid one i.e. not triggered a result of lazy debug register
+ * switching
+ * ii) When there are more bits than trap<n> set in DR6 register (such
+ * as BD, BS or BT) indicating that more than one debug condition is
+ * met and requires some more action in do_debug().
+ *
+ * NOTIFY_STOP returned for all other cases
+ *
  */
 int __kprobes hw_breakpoint_handler(struct die_args *args)
 {
@@ -346,8 +359,10 @@ int __kprobes hw_breakpoint_handler(stru
 		 * or due to the delay between updates of hbp_kernel_pos
 		 * and this_hbp_kernel.
 		 */
-		if (!bp)
+		if (!bp) {
+			rc = NOTIFY_STOP;
 			continue;
+		}
 
 		(bp->triggered)(bp, args->regs);
 		/*
arch/x86/kernel/ptrace.c +1 -3
Index: linux-2.6-tip.hbkpt/arch/x86/kernel/ptrace.c
===================================================================
@@ -530,9 +530,7 @@ restore:
 				bp->info.len = len;
 				bp->info.type = type;
 				rc = register_user_hw_breakpoint(tsk, bp);
-				if (!rc)
-					set_tsk_thread_flag(tsk, TIF_DEBUG);
-				else
+				if (rc)
 					kfree(bp);
 			}
 		} else
kernel/hw_breakpoint.c +14 -5
Index: linux-2.6-tip.hbkpt/kernel/hw_breakpoint.c
===================================================================
@@ -233,6 +233,8 @@ int register_user_hw_breakpoint(struct t
 			break;
 		}
 	}
+	if (!rc)
+		set_tsk_thread_flag(tsk, TIF_DEBUG);
 
 	spin_unlock_bh(&hw_breakpoint_lock);
 	return rc;
@@ -272,15 +274,22 @@ void unregister_user_hw_breakpoint(struc
 						struct hw_breakpoint *bp)
 {
 	struct thread_struct *thread = &(tsk->thread);
-	int i;
+	int i, pos = -1, hbp_counter = 0;
 
 	spin_lock_bh(&hw_breakpoint_lock);
 	for (i = 0; i < hbp_kernel_pos; i++) {
-		if (bp == thread->hbp[i]) {
-			__unregister_user_hw_breakpoint(i, tsk);
-			break;
-		}
+		if (thread->hbp[i])
+			hbp_counter++;
+		if (bp == thread->hbp[i])
+			pos = i;
 	}
+	if (pos >= 0) {
+		__unregister_user_hw_breakpoint(pos, tsk);
+		hbp_counter--;
+	}
+	if (!hbp_counter)
+		clear_tsk_thread_flag(tsk, TIF_DEBUG);
+
 	spin_unlock_bh(&hw_breakpoint_lock);
 }
 EXPORT_SYMBOL_GPL(unregister_user_hw_breakpoint);
#4 lore raw

arch/x86/kernel/hw_breakpoint.c void arch_flush_thread_hw_breakpoint(str · line 304

Quoted 12 lines · K.Prasad
arch/x86/kernel/hw_breakpoint.c +6 -0
@@ -296,6 +296,19 @@ void arch_flush_thread_hw_breakpoint(str
 
 /*
  * Handle debug exception notifications.
+ *
+ * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below.
+ *
+ * NOTIFY_DONE returned if one of the following conditions is true.
+ * i) When the causative address is from user-space and the exception
+ * is a valid one i.e. not triggered a result of lazy debug register

--------------------^----^ missing ',' characters ---------------------------------------^ missing "as"

Quoted 11 lines
arch/x86/kernel/hw_breakpoint.c +7 -0
+ * switching
+ * ii) When there are more bits than trap<n> set in DR6 register (such
+ * as BD, BS or BT) indicating that more than one debug condition is
+ * met and requires some more action in do_debug().
+ *
+ * NOTIFY_STOP returned for all other cases
+ *
  */
 int __kprobes hw_breakpoint_handler(struct die_args *args)
 {

arch/x86/kernel/hw_breakpoint.c int __kprobes hw_breakpoint_handler(stru · line 367

Quoted 10 lines
arch/x86/kernel/hw_breakpoint.c +3 -1
@@ -346,8 +359,10 @@ int __kprobes hw_breakpoint_handler(stru
 		 * or due to the delay between updates of hbp_kernel_pos
 		 * and this_hbp_kernel.
 		 */
-		if (!bp)
+		if (!bp) {
+			rc = NOTIFY_STOP;
 			continue;
+		}
 
 		(bp->triggered)(bp, args->regs);

This is the same mistake as before. If rc had previously been set to NOTIFY_DONE then you will unconditionally change it back to NOTIFY_STOP. You should make the code do what the comment says, set rc to NOTIFY_DONE when a non-NULL user breakpoint occurs:

		if (i >= hbp_kernel_pos) {
			bp = per_cpu(this_hbp_kernel[i], cpu);
		} else {
			bp = current->thread.hbp[i];
			if (bp)
				rc = NOTIFY_DONE;
		}
		/*
		 * bp can be NULL due to lazy debug register switching
		 * or due to the delay between updates of hbp_kernel_pos
		 * and this_hbp_kernel.
		 */
		if (!bp)
			continue;

		(bp->triggered)(bp, args->regs);

The rest of the changes are okay.

Alan Stern

author
#5 lore raw
Quoted 62 lines · Alan Stern

arch/x86/kernel/hw_breakpoint.c void arch_flush_thread_hw_breakpoint(str · line 304

Quoted 12 lines · K.Prasad
arch/x86/kernel/hw_breakpoint.c +6 -0
@@ -296,6 +296,19 @@ void arch_flush_thread_hw_breakpoint(str
 
 /*
  * Handle debug exception notifications.
+ *
+ * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below.
+ *
+ * NOTIFY_DONE returned if one of the following conditions is true.
+ * i) When the causative address is from user-space and the exception
+ * is a valid one i.e. not triggered a result of lazy debug register

--------------------^----^ missing ',' characters ---------------------------------------^ missing "as"

Quoted 11 lines
arch/x86/kernel/hw_breakpoint.c +7 -0
+ * switching
+ * ii) When there are more bits than trap<n> set in DR6 register (such
+ * as BD, BS or BT) indicating that more than one debug condition is
+ * met and requires some more action in do_debug().
+ *
+ * NOTIFY_STOP returned for all other cases
+ *
  */
 int __kprobes hw_breakpoint_handler(struct die_args *args)
 {

arch/x86/kernel/hw_breakpoint.c int __kprobes hw_breakpoint_handler(stru · line 367

Quoted 10 lines
arch/x86/kernel/hw_breakpoint.c +3 -1
@@ -346,8 +359,10 @@ int __kprobes hw_breakpoint_handler(stru
 		 * or due to the delay between updates of hbp_kernel_pos
 		 * and this_hbp_kernel.
 		 */
-		if (!bp)
+		if (!bp) {
+			rc = NOTIFY_STOP;
 			continue;
+		}
 
 		(bp->triggered)(bp, args->regs);

This is the same mistake as before. If rc had previously been set to NOTIFY_DONE then you will unconditionally change it back to NOTIFY_STOP. You should make the code do what the comment says, set rc to NOTIFY_DONE when a non-NULL user breakpoint occurs:

		if (i >= hbp_kernel_pos) {
			bp = per_cpu(this_hbp_kernel[i], cpu);
		} else {
			bp = current->thread.hbp[i];
			if (bp)
				rc = NOTIFY_DONE;
		}
		/*
		 * bp can be NULL due to lazy debug register switching
		 * or due to the delay between updates of hbp_kernel_pos
		 * and this_hbp_kernel.
		 */
		if (!bp)
			continue;

		(bp->triggered)(bp, args->regs);
Quoted 4 lines

The rest of the changes are okay.

Alan Stern

Thanks. I am pasting the hw_breakpoint_handler() function completely below (as opposed to patch form which may be difficult to follow). Upon your consent with the changes, I intend to send a consolidated patchset to Ingo (merging the patches over hw_breakpoint.c) for inclusion in -tip tree.

I have also moved the "(*dr6_p) &= ~(DR_TRAP0 << i)" before the "if (!bp)" check.

/*
* Handle debug exception notifications.
*
* Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below. *
* NOTIFY_DONE returned if one of the following conditions is true.
* i) When the causative address is from user-space and the exception
* is a valid one, i.e. not triggered as a result of lazy debug register
* switching
* ii) When there are more bits than trap<n> set in DR6 register (such
* as BD, BS or BT) indicating that more than one debug condition is
* met and requires some more action in do_debug().
*
* NOTIFY_STOP returned for all other cases
*
*/
int __kprobes hw_breakpoint_handler(struct die_args *args)

{
	int i, cpu, rc = NOTIFY_STOP;
	struct hw_breakpoint *bp;
	unsigned long dr7, dr6;
	unsigned long *dr6_p;

	/* The DR6 value is pointed by args->err */
	dr6_p = (unsigned long *)ERR_PTR(args->err);
	dr6 = *dr6_p;

	/* Do an early return if no trap bits are set in DR6 */
	if ((dr6 & DR_TRAP_BITS) == 0)
		return NOTIFY_DONE;

	/* Lazy debug register switching */
	if (!test_tsk_thread_flag(current, TIF_DEBUG))
		arch_uninstall_thread_hw_breakpoint();

	get_debugreg(dr7, 7);
	/* Disable breakpoints during exception handling */
	set_debugreg(0UL, 7);
	/*
	 * Assert that local interrupts are disabled
	 * Reset the DRn bits in the virtualized register value.
	 * The ptrace trigger routine will add in whatever is needed.
	 */
	current->thread.debugreg6 &= ~DR_TRAP_BITS;
	cpu = get_cpu();

	/* Handle all the breakpoints that were triggered */
	for (i = 0; i < HBP_NUM; ++i) {
		if (likely(!(dr6 & (DR_TRAP0 << i))))
			continue;
		/*
		 * Find the corresponding hw_breakpoint structure and
		 * invoke its triggered callback.
		 */
		if (i >= hbp_kernel_pos)
			bp = per_cpu(this_hbp_kernel[i], cpu);
		else {
			bp = current->thread.hbp[i];
			if (bp)
				rc = NOTIFY_DONE;
		}

		/*
		 * Reset the 'i'th TRAP bit in dr6 to denote completion of
		 * exception handling
		 */
		(*dr6_p) &= ~(DR_TRAP0 << i);
		/*
		 * bp can be NULL due to lazy debug register switching
		 * or due to the delay between updates of hbp_kernel_pos
		 * and this_hbp_kernel.
		 */
		if (!bp)
			continue;

		(bp->triggered)(bp, args->regs);
	}
	if (dr6 & (~DR_TRAP_BITS))
		rc = NOTIFY_DONE;

	set_debugreg(dr7, 7);
	put_cpu_no_resched();
	return rc;
}

Thanks,
K.Prasad

#6 lore raw
Quoted 85 lines · K.Prasad

Thanks. I am pasting the hw_breakpoint_handler() function completely below (as opposed to patch form which may be difficult to follow). Upon your consent with the changes, I intend to send a consolidated patchset to Ingo (merging the patches over hw_breakpoint.c) for inclusion in -tip tree.

I have also moved the "(*dr6_p) &= ~(DR_TRAP0 << i)" before the "if (!bp)" check.

/*
* Handle debug exception notifications.
*
* Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below. *
* NOTIFY_DONE returned if one of the following conditions is true.
* i) When the causative address is from user-space and the exception
* is a valid one, i.e. not triggered as a result of lazy debug register
* switching
* ii) When there are more bits than trap<n> set in DR6 register (such
* as BD, BS or BT) indicating that more than one debug condition is
* met and requires some more action in do_debug().
*
* NOTIFY_STOP returned for all other cases
*
*/
int __kprobes hw_breakpoint_handler(struct die_args *args)

{
	int i, cpu, rc = NOTIFY_STOP;
	struct hw_breakpoint *bp;
	unsigned long dr7, dr6;
	unsigned long *dr6_p;

	/* The DR6 value is pointed by args->err */
	dr6_p = (unsigned long *)ERR_PTR(args->err);
	dr6 = *dr6_p;

	/* Do an early return if no trap bits are set in DR6 */
	if ((dr6 & DR_TRAP_BITS) == 0)
		return NOTIFY_DONE;

	/* Lazy debug register switching */
	if (!test_tsk_thread_flag(current, TIF_DEBUG))
		arch_uninstall_thread_hw_breakpoint();

	get_debugreg(dr7, 7);
	/* Disable breakpoints during exception handling */
	set_debugreg(0UL, 7);
	/*
	 * Assert that local interrupts are disabled
	 * Reset the DRn bits in the virtualized register value.
	 * The ptrace trigger routine will add in whatever is needed.
	 */
	current->thread.debugreg6 &= ~DR_TRAP_BITS;
	cpu = get_cpu();

	/* Handle all the breakpoints that were triggered */
	for (i = 0; i < HBP_NUM; ++i) {
		if (likely(!(dr6 & (DR_TRAP0 << i))))
			continue;
		/*
		 * Find the corresponding hw_breakpoint structure and
		 * invoke its triggered callback.
		 */
		if (i >= hbp_kernel_pos)
			bp = per_cpu(this_hbp_kernel[i], cpu);
		else {
			bp = current->thread.hbp[i];
			if (bp)
				rc = NOTIFY_DONE;
		}

		/*
		 * Reset the 'i'th TRAP bit in dr6 to denote completion of
		 * exception handling
		 */
		(*dr6_p) &= ~(DR_TRAP0 << i);
		/*
		 * bp can be NULL due to lazy debug register switching
		 * or due to the delay between updates of hbp_kernel_pos
		 * and this_hbp_kernel.
		 */
		if (!bp)
			continue;

		(bp->triggered)(bp, args->regs);
Quoted 8 lines
	}
	if (dr6 & (~DR_TRAP_BITS))
		rc = NOTIFY_DONE;

	set_debugreg(dr7, 7);
	put_cpu_no_resched();
	return rc;
}

This looks okay.

Alan Stern

author
#7 lore raw

PATCH 2/2 Fix the style issue seen in ksym tracer ftrace plugin

In reply to a message not in the archive

Fix the style issue seen in ksym tracer ftrace plugin

Move the extern 'process_new_ksym_entry' declaration from trace_selftest.c to trace.h file due to a reported style issue.

---
 kernel/trace/trace.h          |    1 +
 kernel/trace/trace_selftest.c |    1 -
 2 files changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace.h +1 -0
Index: linux-2.6-tip.hbkpt/kernel/trace/trace.h
===================================================================
@@ -213,6 +213,7 @@ struct syscall_trace_exit {
 };
 
 #define KSYM_SELFTEST_ENTRY "ksym_selftest_dummy"
+extern int process_new_ksym_entry(char *ksymname, int op, unsigned long addr);
 
 struct trace_ksym {
 	struct trace_entry	ent;
kernel/trace/trace_selftest.c +0 -1
Index: linux-2.6-tip.hbkpt/kernel/trace/trace_selftest.c
===================================================================
@@ -810,7 +810,6 @@ trace_selftest_startup_hw_branches(struc
 #endif /* CONFIG_HW_BRANCH_TRACER */
 
 #ifdef CONFIG_KSYM_TRACER
-extern int process_new_ksym_entry(char *ksymname, int op, unsigned long addr);
 static int ksym_selftest_dummy;
 
 int
Acked-by on v1 2/2
#8 lore raw

Re: [Patch 2/2] Fix the style issue seen in ksym tracer ftrace plugin

Quoted 6 lines · K.Prasad

Fix the style issue seen in ksym tracer ftrace plugin

Move the extern 'process_new_ksym_entry' declaration from trace_selftest.c to trace.h file due to a reported style issue.

Quoted 30 lines
---
 kernel/trace/trace.h          |    1 +
 kernel/trace/trace_selftest.c |    1 -
 2 files changed, 1 insertion(+), 1 deletion(-)

Index: linux-2.6-tip.hbkpt/kernel/trace/trace.h
===================================================================

kernel/trace/trace.h +1 -0
@@ -213,6 +213,7 @@ struct syscall_trace_exit {
 };
 
 #define KSYM_SELFTEST_ENTRY "ksym_selftest_dummy"
+extern int process_new_ksym_entry(char *ksymname, int op, unsigned long addr);
 
 struct trace_ksym {
 	struct trace_entry	ent;

Index: linux-2.6-tip.hbkpt/kernel/trace/trace_selftest.c
===================================================================

kernel/trace/trace_selftest.c +0 -1
@@ -810,7 +810,6 @@ trace_selftest_startup_hw_branches(struc
 #endif /* CONFIG_HW_BRANCH_TRACER */
 
 #ifdef CONFIG_KSYM_TRACER
-extern int process_new_ksym_entry(char *ksymname, int op, unsigned long addr);
 static int ksym_selftest_dummy;
 
 int