kprobes: Remove NMI context check
Subsystems (2)
Masami Hiramatsu
maintainerPATCH 1/2 kprobes: Remove NMI context check
commit e03b4a084ea6b0a18b0e874baec439e69090c168 upstream.
The in_nmi() check in pre_handler_kretprobe() is meant to avoid recursion, and blindly assumes that anything NMI is recursive.
However, since commit:
9b38cc704e84 ("kretprobe: Prevent triggering kretprobe from within kprobe_flush_task")
there is a better way to detect and avoid actual recursion.
By setting a dummy kprobe, any actual exceptions will terminate early (by trying to handle the dummy kprobe), and recursion will not happen.
Employ this to avoid the kretprobe_table_lock() recursion, replacing the over-eager in_nmi() check.
--- kernel/kprobes.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)
kernel/kprobes.c +4 -12
@@ -1359,7 +1359,8 @@ static void cleanup_rp_inst(struct kretprobe *rp)struct hlist_node *next;struct hlist_head *head; -/* No race here */ +/* To avoid recursive kretprobe by NMI, set kprobe busy here */ +kprobe_busy_begin ();for (hash =0 ; hash <KPROBE_TABLE_SIZE ; hash++) {kretprobe_table_lock (hash, &flags); head = &kretprobe_inst_table[hash];
@@ -1369,6 +1370,8 @@ static void cleanup_rp_inst(struct kretprobe *rp) }kretprobe_table_unlock (hash, &flags); } +kprobe_busy_end (); +free_rp_inst (rp); }NOKPROBE_SYMBOL (cleanup_rp_inst);
@@ -1937,17 +1940,6 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)unsigned long hash, flags =0 ;struct kretprobe_instance *ri; -/* -* To avoid deadlocks, prohibit return probing in NMI contexts, -* just skip the probe and increase the (inexact) 'nmissed' -* statistical counter, so that the user is informed that -* something happened: -*/ -if (unlikely (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);
Masami Hiramatsu
maintainerPATCH 2/2 kprobes: Tell lockdep about kprobe nesting
From: Steven Rostedt (VMware) <[email protected]>
commit 645f224e7ba2f4200bf163153d384ceb0de5462e upstream.
Since the kprobe handlers have protection that prohibits other handlers from executing in other contexts (like if an NMI comes in while processing a kprobe, and executes the same kprobe, it will get fail with a "busy" return). Lockdep is unaware of this protection. Use lockdep's nesting api to differentiate between locks taken in INT3 context and other context to suppress the false warnings.
--- kernel/kprobes.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)
kernel/kprobes.c +21 -4
@@ -1250,7 +1250,13 @@ __acquires(hlist_lock) *head = &kretprobe_inst_table[hash]; hlist_lock =kretprobe_table_lock_ptr (hash); -raw_spin_lock_irqsave (hlist_lock, *flags); +/* +* Nested is a workaround that will soon not be needed. +* There's other protections that make sure the same lock +* is not taken on the same CPU that lockdep is unaware of. +* Differentiate when it is taken in NMI context. +*/ +raw_spin_lock_irqsave_nested (hlist_lock, *flags, !!in_nmi ()); }NOKPROBE_SYMBOL (kretprobe_hash_lock);
@@ -1259,7 +1265,13 @@ static void kretprobe_table_lock(unsigned long hash,__acquires (hlist_lock) {raw_spinlock_t *hlist_lock =kretprobe_table_lock_ptr (hash); -raw_spin_lock_irqsave (hlist_lock, *flags); +/* +* Nested is a workaround that will soon not be needed. +* There's other protections that make sure the same lock +* is not taken on the same CPU that lockdep is unaware of. +* Differentiate when it is taken in NMI context. +*/ +raw_spin_lock_irqsave_nested (hlist_lock, *flags, !!in_nmi ()); }NOKPROBE_SYMBOL (kretprobe_table_lock);
@@ -1942,7 +1954,12 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)/* 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); +/* +* Nested is a workaround that will soon not be needed. +* There's other protections that make sure the same lock +* is not taken on the same CPU that lockdep is unaware of. +*/ +raw_spin_lock_irqsave_nested (&rp->lock, flags,1 );if (!hlist_empty (&rp->free_instances)) { ri =hlist_entry (rp->free_instances.first, struct kretprobe_instance, hlist);
@@ -1953,7 +1970,7 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs) ri->task = current;if (rp->entry_handler && rp->entry_handler (ri, regs)) { -raw_spin_lock_irqsave (&rp->lock, flags); +raw_spin_lock_irqsave_nested (&rp->lock, flags,1 );hlist_add_head (&ri->hlist, &rp->free_instances);raw_spin_unlock_irqrestore (&rp->lock, flags);return 0 ;
Quoted 1 line · Masami Hiramatsu
commit
e03b4a084ea6b0a18b0e874baec439e69090c168upstream.
Both patches now queued up, thanks.
greg k-h