Skip to content

Commit ed61e8c

Browse files
Jiri OlsaSasha Levin
authored andcommitted
kretprobe: Prevent triggering kretprobe from within kprobe_flush_task
[ Upstream commit 9b38cc7 ] Ziqian reported lockup when adding retprobe on _raw_spin_lock_irqsave. My test was also able to trigger lockdep output: ============================================ WARNING: possible recursive locking detected 5.6.0-rc6+ #6 Not tainted -------------------------------------------- sched-messaging/2767 is trying to acquire lock: ffffffff9a492798 (&(kretprobe_table_locks[i].lock)){-.-.}, at: kretprobe_hash_lock+0x52/0xa0 but task is already holding lock: ffffffff9a491a18 (&(kretprobe_table_locks[i].lock)){-.-.}, at: kretprobe_trampoline+0x0/0x50 other info that might help us debug this: Possible unsafe locking scenario: CPU0 ---- lock(&(kretprobe_table_locks[i].lock)); lock(&(kretprobe_table_locks[i].lock)); *** DEADLOCK *** May be due to missing lock nesting notation 1 lock held by sched-messaging/2767: #0: ffffffff9a491a18 (&(kretprobe_table_locks[i].lock)){-.-.}, at: kretprobe_trampoline+0x0/0x50 stack backtrace: CPU: 3 PID: 2767 Comm: sched-messaging Not tainted 5.6.0-rc6+ #6 Call Trace: dump_stack+0x96/0xe0 __lock_acquire.cold.57+0x173/0x2b7 ? native_queued_spin_lock_slowpath+0x42b/0x9e0 ? lockdep_hardirqs_on+0x590/0x590 ? __lock_acquire+0xf63/0x4030 lock_acquire+0x15a/0x3d0 ? kretprobe_hash_lock+0x52/0xa0 _raw_spin_lock_irqsave+0x36/0x70 ? kretprobe_hash_lock+0x52/0xa0 kretprobe_hash_lock+0x52/0xa0 trampoline_handler+0xf8/0x940 ? kprobe_fault_handler+0x380/0x380 ? find_held_lock+0x3a/0x1c0 kretprobe_trampoline+0x25/0x50 ? lock_acquired+0x392/0xbc0 ? _raw_spin_lock_irqsave+0x50/0x70 ? __get_valid_kprobe+0x1f0/0x1f0 ? _raw_spin_unlock_irqrestore+0x3b/0x40 ? finish_task_switch+0x4b9/0x6d0 ? __switch_to_asm+0x34/0x70 ? __switch_to_asm+0x40/0x70 The code within the kretprobe handler checks for probe reentrancy, so we won't trigger any _raw_spin_lock_irqsave probe in there. The problem is in outside kprobe_flush_task, where we call: kprobe_flush_task kretprobe_table_lock raw_spin_lock_irqsave _raw_spin_lock_irqsave where _raw_spin_lock_irqsave triggers the kretprobe and installs kretprobe_trampoline handler on _raw_spin_lock_irqsave return. The kretprobe_trampoline handler is then executed with already locked kretprobe_table_locks, and first thing it does is to lock kretprobe_table_locks ;-) the whole lockup path like: kprobe_flush_task kretprobe_table_lock raw_spin_lock_irqsave _raw_spin_lock_irqsave ---> probe triggered, kretprobe_trampoline installed ---> kretprobe_table_locks locked kretprobe_trampoline trampoline_handler kretprobe_hash_lock(current, &head, &flags); <--- deadlock Adding kprobe_busy_begin/end helpers that mark code with fake probe installed to prevent triggering of another kprobe within this code. Using these helpers in kprobe_flush_task, so the probe recursion protection check is hit and the probe is never set to prevent above lockup. Link: http://lkml.kernel.org/r/158927059835.27680.7011202830041561604.stgit@devnote2 Fixes: ef53d9c ("kprobes: improve kretprobe scalability with hashed locking") Cc: Ingo Molnar <mingo@kernel.org> Cc: "Gustavo A . R . Silva" <gustavoars@kernel.org> Cc: Anders Roxell <anders.roxell@linaro.org> Cc: "Naveen N . Rao" <naveen.n.rao@linux.ibm.com> Cc: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> Cc: David Miller <davem@davemloft.net> Cc: Ingo Molnar <mingo@elte.hu> Cc: Peter Zijlstra <peterz@infradead.org> Cc: stable@vger.kernel.org Reported-by: "Ziqian SUN (Zamir)" <zsun@redhat.com> Acked-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Jiri Olsa <jolsa@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 45ac65d commit ed61e8c

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

arch/x86/kernel/kprobes/core.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -740,16 +740,11 @@ asm(
740740
NOKPROBE_SYMBOL(kretprobe_trampoline);
741741
STACK_FRAME_NON_STANDARD(kretprobe_trampoline);
742742

743-
static struct kprobe kretprobe_kprobe = {
744-
.addr = (void *)kretprobe_trampoline,
745-
};
746-
747743
/*
748744
* Called from kretprobe_trampoline
749745
*/
750746
__visible __used void *trampoline_handler(struct pt_regs *regs)
751747
{
752-
struct kprobe_ctlblk *kcb;
753748
struct kretprobe_instance *ri = NULL;
754749
struct hlist_head *head, empty_rp;
755750
struct hlist_node *tmp;
@@ -759,16 +754,12 @@ __visible __used void *trampoline_handler(struct pt_regs *regs)
759754
void *frame_pointer;
760755
bool skipped = false;
761756

762-
preempt_disable();
763-
764757
/*
765758
* Set a dummy kprobe for avoiding kretprobe recursion.
766759
* Since kretprobe never run in kprobe handler, kprobe must not
767760
* be running at this point.
768761
*/
769-
kcb = get_kprobe_ctlblk();
770-
__this_cpu_write(current_kprobe, &kretprobe_kprobe);
771-
kcb->kprobe_status = KPROBE_HIT_ACTIVE;
762+
kprobe_busy_begin();
772763

773764
INIT_HLIST_HEAD(&empty_rp);
774765
kretprobe_hash_lock(current, &head, &flags);
@@ -847,7 +838,7 @@ __visible __used void *trampoline_handler(struct pt_regs *regs)
847838
__this_cpu_write(current_kprobe, &ri->rp->kp);
848839
ri->ret_addr = correct_ret_addr;
849840
ri->rp->handler(ri, regs);
850-
__this_cpu_write(current_kprobe, &kretprobe_kprobe);
841+
__this_cpu_write(current_kprobe, &kprobe_busy);
851842
}
852843

853844
recycle_rp_inst(ri, &empty_rp);
@@ -863,8 +854,7 @@ __visible __used void *trampoline_handler(struct pt_regs *regs)
863854

864855
kretprobe_hash_unlock(current, &flags);
865856

866-
__this_cpu_write(current_kprobe, NULL);
867-
preempt_enable();
857+
kprobe_busy_end();
868858

869859
hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
870860
hlist_del(&ri->hlist);

include/linux/kprobes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
366366
return this_cpu_ptr(&kprobe_ctlblk);
367367
}
368368

369+
extern struct kprobe kprobe_busy;
370+
void kprobe_busy_begin(void);
371+
void kprobe_busy_end(void);
372+
369373
int register_kprobe(struct kprobe *p);
370374
void unregister_kprobe(struct kprobe *p);
371375
int register_kprobes(struct kprobe **kps, int num);

kernel/kprobes.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,26 @@ __releases(hlist_lock)
11501150
}
11511151
NOKPROBE_SYMBOL(kretprobe_table_unlock);
11521152

1153+
struct kprobe kprobe_busy = {
1154+
.addr = (void *) get_kprobe,
1155+
};
1156+
1157+
void kprobe_busy_begin(void)
1158+
{
1159+
struct kprobe_ctlblk *kcb;
1160+
1161+
preempt_disable();
1162+
__this_cpu_write(current_kprobe, &kprobe_busy);
1163+
kcb = get_kprobe_ctlblk();
1164+
kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1165+
}
1166+
1167+
void kprobe_busy_end(void)
1168+
{
1169+
__this_cpu_write(current_kprobe, NULL);
1170+
preempt_enable();
1171+
}
1172+
11531173
/*
11541174
* This function is called from finish_task_switch when task tk becomes dead,
11551175
* so that we can recycle any function-return probe instances associated
@@ -1167,6 +1187,8 @@ void kprobe_flush_task(struct task_struct *tk)
11671187
/* Early boot. kretprobe_table_locks not yet initialized. */
11681188
return;
11691189

1190+
kprobe_busy_begin();
1191+
11701192
INIT_HLIST_HEAD(&empty_rp);
11711193
hash = hash_ptr(tk, KPROBE_HASH_BITS);
11721194
head = &kretprobe_inst_table[hash];
@@ -1180,6 +1202,8 @@ void kprobe_flush_task(struct task_struct *tk)
11801202
hlist_del(&ri->hlist);
11811203
kfree(ri);
11821204
}
1205+
1206+
kprobe_busy_end();
11831207
}
11841208
NOKPROBE_SYMBOL(kprobe_flush_task);
11851209

0 commit comments

Comments
 (0)