Skip to contentSkip to conversation

ARM: probes: Don't stop the machine if we're in the debugger

PATCH In review Douglas Anderson 3 participants 5 messages Aug 24, 2015 – Aug 25, 2015 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: probes: Don't stop the machine if we're in the debugger

If we're in kgdb then the machine is already stopped. Trying to stop it again will cause us to try to sleep, which is not allowed while in kgdb. To avoid this problem, only stop the machine when we're not in kgdb.

---
 arch/arm/kernel/patch.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm/kernel/patch.c +6 -1
index 69bda1a..abf30ec 100644
@@ -1,5 +1,6 @@
 #include <linux/kernel.h>
 #include <linux/spinlock.h>
+#include <linux/kgdb.h>
 #include <linux/kprobes.h>
 #include <linux/mm.h>
 #include <linux/stop_machine.h>
@@ -124,6 +125,9 @@ void __kprobes patch_text(void *addr, unsigned int insn)
 		.insn = insn,
 	};
 
-	stop_machine(patch_text_stop_machine, &patch, NULL);
+	/* Stop machine before patching; but not if in the debugger */
+	if (unlikely(in_dbg_master()))
+		patch_text_stop_machine(&patch);
+	else
+		stop_machine(patch_text_stop_machine, &patch, NULL);
 }
Signature · 1 line
2.5.0.457.gab17608
#1 lore raw
Quoted 9 lines · Douglas Anderson

If we're in kgdb then the machine is already stopped. Trying to stop it again will cause us to try to sleep, which is not allowed while in kgdb. To avoid this problem, only stop the machine when we're not in kgdb.

---

Can you add the backtrace?

Quoted 15 lines
  arch/arm/kernel/patch.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm/kernel/patch.c +1 -0
index 69bda1a..abf30ec 100644
@@ -1,5 +1,6 @@
  #include <linux/kernel.h>
  #include <linux/spinlock.h>
+#include <linux/kgdb.h>
  #include <linux/kprobes.h>
  #include <linux/mm.h>
  #include <linux/stop_machine.h>

arch/arm/kernel/patch.c void __kprobes patch_text(void *addr, unsigned int insn) · line 133

Quoted 10 lines
arch/arm/kernel/patch.c +5 -1
@@ -124,6 +125,9 @@ void __kprobes patch_text(void *addr, unsigned int insn)
  		.insn = insn,
  	};
 
-	stop_machine(patch_text_stop_machine, &patch, NULL);
+	/* Stop machine before patching; but not if in the debugger */
+	if (unlikely(in_dbg_master()))
+		patch_text_stop_machine(&patch);
+	else
+		stop_machine(patch_text_stop_machine, &patch, NULL);
  }

Perhaps it would be better to add a different function for the kgdb call site? Then it's explicit what's going on without us having to figure out when in_dbg_master() is true.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw
Quoted 9 lines · Stephen Boyd
Quoted 8 lines · Douglas Anderson

If we're in kgdb then the machine is already stopped. Trying to stop it again will cause us to try to sleep, which is not allowed while in kgdb. To avoid this problem, only stop the machine when we're not in kgdb.

I actually suggested using in_atomic_preempt_off() which is I think a better catch-all. Could you use that instead, please?

-Kees

Quoted 41 lines
Quoted 2 lines

---

Can you add the backtrace?

Quoted 15 lines
  arch/arm/kernel/patch.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm/kernel/patch.c +1 -0
index 69bda1a..abf30ec 100644
@@ -1,5 +1,6 @@
  #include <linux/kernel.h>
  #include <linux/spinlock.h>
+#include <linux/kgdb.h>
  #include <linux/kprobes.h>
  #include <linux/mm.h>
  #include <linux/stop_machine.h>

arch/arm/kernel/patch.c void __kprobes patch_text(void *addr, unsigned int · line 133

Quoted 10 lines
arch/arm/kernel/patch.c +5 -0
@@ -124,6 +125,9 @@ void __kprobes patch_text(void *addr, unsigned int
 insn)
                .insn = insn,
        };
  -     stop_machine(patch_text_stop_machine, &patch, NULL);
+       /* Stop machine before patching; but not if in the debugger */
+       if (unlikely(in_dbg_master()))
+               patch_text_stop_machine(&patch);
+       else
+               stop_machine(patch_text_stop_machine, &patch, NULL);
  }

Perhaps it would be better to add a different function for the kgdb call site? Then it's explicit what's going on without us having to figure out when in_dbg_master() is true.

--
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

Signature · 2 lines
Kees Cook
Chrome OS Security
#3 lore raw

Kees,

Quoted 8 lines · Kees Cook
Quoted 7 lines · Stephen Boyd
Quoted 6 lines · Douglas Anderson

If we're in kgdb then the machine is already stopped. Trying to stop it again will cause us to try to sleep, which is not allowed while in kgdb. To avoid this problem, only stop the machine when we're not in kgdb.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

I actually suggested using in_atomic_preempt_off() which is I think a better catch-all. Could you use that instead, please?

I chose not to use that because (I think) in_atomic_preempt_off() doesn't guarantee that stop_machine() is not needed. Said another
way: if in_atomic_preempt_off() returns true then we can't sleep on the current CPU. ...but other CPUs may be running. In this case technically we need to call stop_machine(), but we'd need to do it in a way that didn't require sleeping.

In the case of kgdb we know that the machine is stopped and we're atomic too. ...so we could certainly fix kgdb by checking for "atomic", but that would mean there would be other places where we'd get no warning and (I think) invalid behavior.

Stephen Boyd suggested that I just change this so that we expose a new function and that makes everything a bit cleaner. I'll try that.

-Doug

#4 lore raw
In reply to Stephen Boyd

Hi,

Quoted 1 line · Stephen Boyd

Can you add the backtrace?

Good point. Done.

Quoted 3 lines

Perhaps it would be better to add a different function for the kgdb call site? Then it's explicit what's going on without us having to figure out when in_dbg_master() is true.

Actually, there was already a function to patch the text directly without stopping the machine. :)

Adding a bit of extra noise here to point to my new patch since it has a very different title than this one (so might be hard to find). See
<https://patchwork.kernel.org/patch/7073441/>.

-Doug