linux-stable/kernel/power
Tetsuo Handa f7042cf9dd PM: hibernate: defer device probing when resuming from hibernation
[ Upstream commit 8386c414e2 ]

syzbot is reporting hung task at misc_open() [1], for there is a race
window of AB-BA deadlock which involves probe_count variable. Currently
wait_for_device_probe() from snapshot_open() from misc_open() can sleep
forever with misc_mtx held if probe_count cannot become 0.

When a device is probed by hub_event() work function, probe_count is
incremented before the probe function starts, and probe_count is
decremented after the probe function completed.

There are three cases that can prevent probe_count from dropping to 0.

  (a) A device being probed stopped responding (i.e. broken/malicious
      hardware).

  (b) A process emulating a USB device using /dev/raw-gadget interface
      stopped responding for some reason.

  (c) New device probe requests keeps coming in before existing device
      probe requests complete.

The phenomenon syzbot is reporting is (b). A process which is holding
system_transition_mutex and misc_mtx is waiting for probe_count to become
0 inside wait_for_device_probe(), but the probe function which is called
 from hub_event() work function is waiting for the processes which are
blocked at mutex_lock(&misc_mtx) to respond via /dev/raw-gadget interface.

This patch mitigates (b) by deferring wait_for_device_probe() from
snapshot_open() to snapshot_write() and snapshot_ioctl(). Please note that
the possibility of (b) remains as long as any thread which is emulating a
USB device via /dev/raw-gadget interface can be blocked by uninterruptible
blocking operations (e.g. mutex_lock()).

Please also note that (a) and (c) are not addressed. Regarding (c), we
should change the code to wait for only one device which contains the
image for resuming from hibernation. I don't know how to address (a), for
use of timeout for wait_for_device_probe() might result in loss of user
data in the image. Maybe we should require the userland to wait for the
image device before opening /dev/snapshot interface.

Link: https://syzkaller.appspot.com/bug?extid=358c9ab4c93da7b7238c [1]
Reported-by: syzbot <syzbot+358c9ab4c93da7b7238c@syzkaller.appspotmail.com>
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Tested-by: syzbot <syzbot+358c9ab4c93da7b7238c@syzkaller.appspotmail.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2022-08-17 15:14:06 +02:00
..
Kconfig
Makefile PM: sleep: Narrow down -DDEBUG on kernel/power/ files 2022-04-13 16:34:01 +02:00
autosleep.c
console.c
energy_model.c PM: EM: convert power field to micro-Watts precision and align drivers 2022-08-17 15:14:05 +02:00
hibernate.c PM: hibernate: Use kernel_can_power_off() 2022-06-21 20:57:30 +02:00
main.c cxl for 5.19 2022-05-27 21:24:19 -07:00
power.h PM: hibernate: Allow ACPI hardware signature to be honoured 2021-12-08 16:06:10 +01:00
poweroff.c
process.c PM: sleep: Narrow down -DDEBUG on kernel/power/ files 2022-04-13 16:34:01 +02:00
qos.c
snapshot.c PM: hibernate: Don't mark comment as kernel-doc 2022-04-13 17:09:37 +02:00
suspend.c PM: CXL: Disable suspend 2022-04-22 16:09:42 -07:00
suspend_test.c PM: suspend: fix return value of __setup handler 2022-03-01 18:55:07 +01:00
swap.c for-5.18/block-2022-03-18 2022-03-21 16:48:55 -07:00
user.c PM: hibernate: defer device probing when resuming from hibernation 2022-08-17 15:14:06 +02:00
wakelock.c PM: wakeup: simplify the output logic of pm_show_wakelocks() 2022-01-25 18:27:02 +01:00