Files
linux/include/linux
Jiri Olsa 020742f920 kretprobe: Prevent triggering kretprobe from within kprobe_flush_task
[ Upstream commit 9b38cc704e ]

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: ef53d9c5e4 ("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>
2023-05-16 08:20:37 +09:00
..
2023-04-21 13:52:38 +09:00
2015-10-07 18:08:15 +01:00
2016-07-08 16:23:11 +02:00
2023-05-15 17:28:23 +09:00
2016-08-25 11:26:48 -04:00
2017-10-08 10:26:06 +02:00
2018-04-24 09:34:18 +02:00
2018-04-12 09:54:41 -07:00
2016-08-11 09:41:35 -06:00
2018-03-31 18:42:34 +02:00
2018-02-28 10:18:33 +01:00
2018-05-30 13:19:56 +02:00
2023-05-15 12:06:31 +09:00
2016-06-07 13:41:38 -06:00
2016-05-17 15:48:12 -04:00
2018-04-12 09:54:41 -07:00
2023-05-15 15:11:10 +09:00
2017-07-12 15:01:02 +02:00
2017-12-10 17:13:13 +01:00
2023-05-15 17:27:48 +09:00
2018-04-12 09:54:41 -07:00
2023-05-15 17:11:10 +09:00
2018-03-14 20:21:31 -08:00
2016-07-22 09:07:02 +02:00
2016-09-24 10:48:18 +02:00
2015-11-25 09:22:00 -07:00
2016-02-11 09:59:22 -05:00
2018-04-09 11:39:17 -07:00
2016-10-20 15:51:28 +11:00
2016-09-16 09:34:15 +01:00
2016-09-14 09:18:09 -06:00
2018-01-02 20:45:15 +01:00
2016-05-11 22:37:54 +02:00
2015-06-25 12:06:45 +02:00
2016-01-28 14:19:12 -08:00
2023-05-15 08:33:22 +09:00
2016-08-10 11:23:44 -04:00
2017-10-30 09:27:09 +01:00
2016-03-22 15:36:02 -07:00
2015-07-28 08:50:42 +01:00
2023-05-15 17:12:36 +09:00
2016-01-15 17:56:32 -08:00
2016-09-15 16:49:39 +02:00
2023-05-16 08:20:16 +09:00
2016-09-27 12:33:47 +02:00
2017-08-24 17:12:19 -07:00
2018-04-17 17:58:08 -08:00
2015-06-24 17:49:41 -07:00
2015-07-21 10:39:05 -07:00
2016-04-25 15:09:11 -04:00
2016-02-16 13:04:58 -05:00
2016-10-19 11:36:22 -06:00
2016-05-02 09:00:56 -05:00
2018-03-22 09:54:47 +01:00
2016-02-11 18:35:48 -08:00
2016-03-14 15:43:11 -04:00
2023-05-15 17:09:21 +09:00
2017-08-24 17:12:21 -07:00
2016-10-14 11:36:59 -07:00
2016-07-12 19:25:38 -07:00
2016-09-27 21:52:00 -04:00
2016-09-08 15:01:10 -07:00
2016-03-17 15:09:34 -07:00
2016-07-06 10:51:14 +01:00
2016-03-22 15:36:02 -07:00
2016-07-26 16:19:19 -07:00
2016-09-08 22:15:25 -07:00
2023-05-15 17:14:27 +09:00
2023-05-15 17:12:28 +09:00
2017-08-30 10:21:40 +02:00
2023-05-15 14:57:24 +09:00
2016-08-28 23:44:55 -04:00
2016-10-05 18:23:36 -04:00
2023-05-15 12:21:56 +09:00
2018-05-30 13:19:56 +02:00
2023-05-15 09:23:01 +09:00
2023-05-15 13:46:50 +09:00
2017-03-17 13:14:32 +08:00
2015-10-01 09:57:59 -07:00
2016-07-19 17:43:38 +03:00
2023-05-15 17:27:48 +09:00
2016-05-23 17:04:14 -07:00
2023-05-15 09:51:54 +09:00
2016-04-07 16:53:29 -04:00
2017-04-21 09:31:21 +02:00
2015-11-23 09:44:58 +01:00
2016-07-26 16:19:19 -07:00
2016-05-20 17:58:30 -07:00
2017-12-25 14:23:37 +01:00
2018-04-03 17:37:41 -08:00
2023-05-15 17:12:32 +09:00
2016-09-30 10:54:03 +02:00
2015-12-03 07:24:29 -08:00
2023-05-15 10:05:34 +09:00
2015-09-08 15:35:28 -07:00