Skip to contentSkip to conversation

kprobes: Skip kretprobe hit in NMI context to avoid deadlock

PATCH Superseded Masami Hiramatsu 3 participants 4 messages Aug 1, 2014 – Aug 2, 2014 kprobes

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kprobes: Skip kretprobe hit in NMI context to avoid deadlock

Skip kretprobe hit in NMI context, because if an NMI happens inside the critical section protected by kretprobe_table.lock and another(or same) kretprobe hit, pre_kretprobe_handler tries to lock kretprobe_table.lock again.
Normal interrupts have no problem because they are disabled with the lock.

Cc Ananth N Mavinakayanahalli <[email protected]>
Cc "David S. Miller" <[email protected]>
---
 kernel/kprobes.c |    6 ++++++
 1 file changed, 6 insertions(+)
kernel/kprobes.c +6 -0
index 734e9a7..a537029 100644
@@ -1778,6 +1778,12 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
 	unsigned long hash, flags = 0;
 	struct kretprobe_instance *ri;
 
+	/* To avoid deadlock, prohibit return probing in NMI context */
+	if (in_nmi()) {
+		rp->nmissed++;
+		return 0;
+	}
+
 	/*TODO: consider to only swap the RA after the last pre_handler fired */
 	hash = hash_ptr(current, KPROBE_HASH_BITS);
 	raw_spin_lock_irqsave(&rp->lock, flags);
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Masami Hiramatsu

Skip kretprobe hit in NMI context, because if an NMI happens inside the critical section protected by kretprobe_table.lock and another(or same) kretprobe hit, pre_kretprobe_handler tries to lock kretprobe_table.lock again.
Normal interrupts have no problem because they are disabled with the lock.

#2 lore raw
In reply to Masami Hiramatsu
Quoted 19 lines · Masami Hiramatsu

Skip kretprobe hit in NMI context, because if an NMI happens inside the critical section protected by kretprobe_table.lock and another(or same) kretprobe hit, pre_kretprobe_handler tries to lock kretprobe_table.lock again.
Normal interrupts have no problem because they are disabled with the lock.

Cc Ananth N Mavinakayanahalli <[email protected]>
Cc "David S. Miller" <[email protected]>
---
 kernel/kprobes.c |    6 ++++++
 1 file changed, 6 insertions(+)
kernel/kprobes.c +0 -0
index 734e9a7..a537029 100644

kernel/kprobes.c static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) · line 1782

Quoted 5 lines
kernel/kprobes.c +2 -0
@@ -1778,6 +1778,12 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
 	unsigned long hash, flags = 0;
 	struct kretprobe_instance *ri;
 
+	/* To avoid deadlock, prohibit return probing in NMI context */
+	if (in_nmi()) {

Should be unlikely()?

kernel/kprobes.c static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) · line 1784

Quoted 2 lines
kernel/kprobes.c +2 -0
+		rp->nmissed++;
+		return 0;

In another place in this function we do:

        } else {
                rp->nmissed++;
                raw_spin_unlock_irqrestore(&rp->lock, flags);
        }

Is it safe to modify rp-> without locking?

kernel/kprobes.c static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) · line 1787

Quoted 3 lines
kernel/kprobes.c +2 -0
+	}
+
 	/*TODO: consider to only swap the RA after the last pre_handler fired */

Nit: That comment is oddly formatted.

Thanks,

Ingo

#3 lore raw
Quoted 25 lines · Ingo Molnar
Quoted 22 lines · Masami Hiramatsu

Skip kretprobe hit in NMI context, because if an NMI happens inside the critical section protected by kretprobe_table.lock and another(or same) kretprobe hit, pre_kretprobe_handler tries to lock kretprobe_table.lock again.
Normal interrupts have no problem because they are disabled with the lock.

Cc Ananth N Mavinakayanahalli <[email protected]>
Cc "David S. Miller" <[email protected]>
---
 kernel/kprobes.c |    6 ++++++
 1 file changed, 6 insertions(+)
kernel/kprobes.c +0 -0
index 734e9a7..a537029 100644
@@ -1778,6 +1778,12 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
 	unsigned long hash, flags = 0;
 	struct kretprobe_instance *ri;
 
Quoted 4 lines

kernel/kprobes.c static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) · line 1782

Quoted 2 lines
kernel/kprobes.c +2 -0
+	/* To avoid deadlock, prohibit return probing in NMI context */
+	if (in_nmi()) {

Should be unlikely()?

Ah, Indeed.

Quoted 6 lines

kernel/kprobes.c static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) · line 1784

Quoted 3 lines
kernel/kprobes.c +2 -0
+		rp->nmissed++;
+		return 0;

In another place in this function we do:

Quoted 6 lines
        } else {
                rp->nmissed++;
                raw_spin_unlock_irqrestore(&rp->lock, flags);
        }

Is it safe to modify rp-> without locking?

Yes, rp->nmissed is just for noticing the fault to users, not for controlling.:) If we need more accurate value, we'd better make it atomic_t.

Thank you,

Quoted 11 lines

kernel/kprobes.c static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) · line 1787

Quoted 4 lines
kernel/kprobes.c +2 -0
+	}
+
 	/*TODO: consider to only swap the RA after the last pre_handler fired */

Nit: That comment is oddly formatted.

Thanks,

Ingo

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]