linux-stable/drivers
Stephen Boyd 60ff482c42 clk: Get runtime PM before walking tree during disable_unused
[ Upstream commit e581cf5d21 ]

Doug reported [1] the following hung task:

 INFO: task swapper/0:1 blocked for more than 122 seconds.
       Not tainted 5.15.149-21875-gf795ebc40eb8 #1
 "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
 task:swapper/0       state:D stack:    0 pid:    1 ppid:     0 flags:0x00000008
 Call trace:
  __switch_to+0xf4/0x1f4
  __schedule+0x418/0xb80
  schedule+0x5c/0x10c
  rpm_resume+0xe0/0x52c
  rpm_resume+0x178/0x52c
  __pm_runtime_resume+0x58/0x98
  clk_pm_runtime_get+0x30/0xb0
  clk_disable_unused_subtree+0x58/0x208
  clk_disable_unused_subtree+0x38/0x208
  clk_disable_unused_subtree+0x38/0x208
  clk_disable_unused_subtree+0x38/0x208
  clk_disable_unused_subtree+0x38/0x208
  clk_disable_unused+0x4c/0xe4
  do_one_initcall+0xcc/0x2d8
  do_initcall_level+0xa4/0x148
  do_initcalls+0x5c/0x9c
  do_basic_setup+0x24/0x30
  kernel_init_freeable+0xec/0x164
  kernel_init+0x28/0x120
  ret_from_fork+0x10/0x20
 INFO: task kworker/u16:0:9 blocked for more than 122 seconds.
       Not tainted 5.15.149-21875-gf795ebc40eb8 #1
 "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
 task:kworker/u16:0   state:D stack:    0 pid:    9 ppid:     2 flags:0x00000008
 Workqueue: events_unbound deferred_probe_work_func
 Call trace:
  __switch_to+0xf4/0x1f4
  __schedule+0x418/0xb80
  schedule+0x5c/0x10c
  schedule_preempt_disabled+0x2c/0x48
  __mutex_lock+0x238/0x488
  __mutex_lock_slowpath+0x1c/0x28
  mutex_lock+0x50/0x74
  clk_prepare_lock+0x7c/0x9c
  clk_core_prepare_lock+0x20/0x44
  clk_prepare+0x24/0x30
  clk_bulk_prepare+0x40/0xb0
  mdss_runtime_resume+0x54/0x1c8
  pm_generic_runtime_resume+0x30/0x44
  __genpd_runtime_resume+0x68/0x7c
  genpd_runtime_resume+0x108/0x1f4
  __rpm_callback+0x84/0x144
  rpm_callback+0x30/0x88
  rpm_resume+0x1f4/0x52c
  rpm_resume+0x178/0x52c
  __pm_runtime_resume+0x58/0x98
  __device_attach+0xe0/0x170
  device_initial_probe+0x1c/0x28
  bus_probe_device+0x3c/0x9c
  device_add+0x644/0x814
  mipi_dsi_device_register_full+0xe4/0x170
  devm_mipi_dsi_device_register_full+0x28/0x70
  ti_sn_bridge_probe+0x1dc/0x2c0
  auxiliary_bus_probe+0x4c/0x94
  really_probe+0xcc/0x2c8
  __driver_probe_device+0xa8/0x130
  driver_probe_device+0x48/0x110
  __device_attach_driver+0xa4/0xcc
  bus_for_each_drv+0x8c/0xd8
  __device_attach+0xf8/0x170
  device_initial_probe+0x1c/0x28
  bus_probe_device+0x3c/0x9c
  deferred_probe_work_func+0x9c/0xd8
  process_one_work+0x148/0x518
  worker_thread+0x138/0x350
  kthread+0x138/0x1e0
  ret_from_fork+0x10/0x20

The first thread is walking the clk tree and calling
clk_pm_runtime_get() to power on devices required to read the clk
hardware via struct clk_ops::is_enabled(). This thread holds the clk
prepare_lock, and is trying to runtime PM resume a device, when it finds
that the device is in the process of resuming so the thread schedule()s
away waiting for the device to finish resuming before continuing. The
second thread is runtime PM resuming the same device, but the runtime
resume callback is calling clk_prepare(), trying to grab the
prepare_lock waiting on the first thread.

This is a classic ABBA deadlock. To properly fix the deadlock, we must
never runtime PM resume or suspend a device with the clk prepare_lock
held. Actually doing that is near impossible today because the global
prepare_lock would have to be dropped in the middle of the tree, the
device runtime PM resumed/suspended, and then the prepare_lock grabbed
again to ensure consistency of the clk tree topology. If anything
changes with the clk tree in the meantime, we've lost and will need to
start the operation all over again.

Luckily, most of the time we're simply incrementing or decrementing the
runtime PM count on an active device, so we don't have the chance to
schedule away with the prepare_lock held. Let's fix this immediate
problem that can be triggered more easily by simply booting on Qualcomm
sc7180.

Introduce a list of clk_core structures that have been registered, or
are in the process of being registered, that require runtime PM to
operate. Iterate this list and call clk_pm_runtime_get() on each of them
without holding the prepare_lock during clk_disable_unused(). This way
we can be certain that the runtime PM state of the devices will be
active and resumed so we can't schedule away while walking the clk tree
with the prepare_lock held. Similarly, call clk_pm_runtime_put() without
the prepare_lock held to properly drop the runtime PM reference. We
remove the calls to clk_pm_runtime_{get,put}() in this path because
they're superfluous now that we know the devices are runtime resumed.

Reported-by: Douglas Anderson <dianders@chromium.org>
Closes: https://lore.kernel.org/all/20220922084322.RFC.2.I375b6b9e0a0a5348962f004beb3dafee6a12dfbb@changeid/ [1]
Closes: https://issuetracker.google.com/328070191
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Ulf Hansson <ulf.hansson@linaro.org>
Cc: Krzysztof Kozlowski <krzk@kernel.org>
Fixes: 9a34b45397 ("clk: Add support for runtime PM")
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
Link: https://lore.kernel.org/r/20240325184204.745706-5-sboyd@kernel.org
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-04-27 17:11:37 +02:00
..
accel accel/ivpu: Fix deadlock in context_xa 2024-04-17 11:19:34 +02:00
accessibility speakup: Fix 8bit characters from direct synth 2024-04-03 15:28:28 +02:00
acpi ACPI: scan: Do not increase dep_unmet for already met dependencies 2024-04-17 11:19:26 +02:00
amba
android binder: signal epoll threads of self-work 2024-02-23 09:25:04 +01:00
ata ata: libata-scsi: Fix ata_scsi_dev_rescan() error path 2024-04-17 11:19:25 +02:00
atm atm: idt77252: fix a memleak in open_card_ubr0 2024-02-16 19:10:49 +01:00
auxdisplay
base driver core: Introduce device_link_wait_removal() 2024-04-10 16:36:03 +02:00
bcma
block aoe: fix the potential use-after-free problem in aoecmd_cfg_pkts 2024-03-26 18:19:19 -04:00
bluetooth Bluetooth: btintel: Fixe build regression 2024-04-13 13:07:41 +02:00
bus bus: mhi: host: Add MHI_PM_SYS_ERR_FAIL state 2024-04-13 13:07:38 +02:00
cache cache: ax45mp_cache: Align end size to cache boundary in ax45mp_dma_cache_wback() 2024-03-01 13:35:08 +01:00
cdrom
cdx
char random: handle creditable entropy from atomic process context 2024-04-27 17:11:30 +02:00
clk clk: Get runtime PM before walking tree during disable_unused 2024-04-27 17:11:37 +02:00
clocksource clocksource/drivers/arm_global_timer: Fix maximum prescaler value 2024-04-03 15:28:50 +02:00
comedi comedi: comedi_test: Prevent timers rescheduling during deletion 2024-03-26 18:20:04 -04:00
connector connector/cn_proc: revert "connector: Fix proc_event_num_listeners count not cleared" 2024-02-23 09:25:01 +01:00
counter
cpufreq cpufreq: Don't unregister cpufreq cooling on CPU hotplug 2024-04-13 13:07:32 +02:00
cpuidle cpuidle: Avoid potential overflow in integer multiplication 2024-04-13 13:07:29 +02:00
crypto crypto: sun8i-ce - Fix use after free in unprepare 2024-04-03 15:28:40 +02:00
cxl cxl/core: Fix initialization of mbox_cmd.size_out in get event 2024-04-17 11:19:27 +02:00
dax
dca
devfreq PM / devfreq: Synchronize devfreq_monitor_[start/stop] 2024-02-05 20:14:15 +00:00
dio
dma dmaengine: tegra210-adma: Update dependency to ARCH_TEGRA 2024-03-26 18:19:43 -04:00
dma-buf dma-buf: Fix NULL pointer dereference in sanitycheck() 2024-04-10 16:35:40 +02:00
edac EDAC/thunderx: Fix possible out-of-bounds string access 2024-01-25 15:35:12 -08:00
eisa
extcon extcon: fix possible name leak in extcon_dev_register() 2024-02-05 20:14:31 +00:00
firewire firewire: ohci: prevent leak of left-over IRQ on unbind 2024-04-03 15:28:41 +02:00
firmware firmware: arm_scmi: Make raw debugfs entries non-seekable 2024-04-17 11:19:27 +02:00
fpga
fsi
gnss
gpio gpio: cdev: fix missed label sanitizing in debounce_setup() 2024-04-10 16:36:02 +02:00
gpu drm: panel-orientation-quirks: Add quirk for Lenovo Legion Go 2024-04-27 17:11:36 +02:00
greybus
hid HID: input: avoid polling stylus battery on Chromebook Pompom 2024-04-13 13:07:36 +02:00
hsi
hte
hv x86/hyperv: Use per cpu initial stack for vtl context 2024-03-26 18:20:06 -04:00
hwmon hwmon: (amc6821) add of_match table 2024-04-03 15:28:28 +02:00
hwspinlock
hwtracing hwtracing: hisi_ptt: Move type check to the beginning of hisi_ptt_pmu_event_init() 2024-03-26 18:20:06 -04:00
i2c i2c: designware: Fix RX FIFO depth define on Wangxun 10Gb NIC 2024-04-13 13:07:35 +02:00
i3c i3c: dw: Disable IBI IRQ depends on hot-join and SIR enabling 2024-03-26 18:20:01 -04:00
idle x86: Fix CPUIDLE_FLAG_IRQ_ENABLE leaking timer reprogram 2024-01-25 15:35:12 -08:00
iio iio: imu: inv_mpu6050: fix FIFO parsing when empty 2024-04-03 15:28:45 +02:00
infiniband RDMA/mlx5: Fix port number for counter query in multi-port configuration 2024-04-27 17:11:34 +02:00
input Input: xpad - add support for Snakebyte GAMEPADs 2024-04-13 13:07:37 +02:00
interconnect interconnect: Don't access req_list while it's being manipulated 2024-04-27 17:11:37 +02:00
iommu iommu/vt-d: Allocate local memory for page request queue 2024-04-17 11:19:33 +02:00
ipack
irqchip irqchip/renesas-rzg2l: Prevent spurious interrupts when setting trigger type 2024-04-03 15:28:52 +02:00
isdn
leds leds: trigger: netdev: Fix kernel panic on interface rename trig notify 2024-04-03 15:28:27 +02:00
macintosh
mailbox mailbox: arm_mhuv2: Fix a bug for mhuv2_sender_interrupt 2024-02-05 20:14:31 +00:00
mcb
md raid1: fix use-after-free for original bio in raid1_write_request() 2024-04-17 11:19:25 +02:00
media media: videobuf2: request more buffers for vb2_read 2024-04-27 17:11:30 +02:00
memory memory: tegra: Correct DLA client names 2024-03-26 18:19:32 -04:00
memstick
message
mfd mfd: cs42l43: Fix wrong GPIO_FN_SEL and SPI_CLK_CONFIG1 defaults 2024-03-26 18:19:55 -04:00
misc VMCI: Fix possible memcpy() run-time warning in vmci_datagram_invoke_guest_handler() 2024-04-13 13:07:41 +02:00
mmc mmc: omap: restore original power up/down steps 2024-04-17 11:19:26 +02:00
most
mtd mtd: rawnand: Constrain even more when continuous reads are enabled 2024-04-03 15:28:35 +02:00
mux
net net: ethernet: ti: am65-cpsw-nuss: cleanup DMA Channels before using them 2024-04-27 17:11:34 +02:00
nfc
ntb NTB: fix possible name leak in ntb_register_device() 2024-03-26 18:19:48 -04:00
nubus
nvdimm
nvme drivers/nvme: Add quirks for device 126f:2262 2024-04-13 13:07:39 +02:00
nvmem nvmem: meson-efuse: fix function pointer type mismatch 2024-04-03 15:28:28 +02:00
of of: module: prevent NULL pointer dereference in vsnprintf() 2024-04-10 16:36:06 +02:00
opp OPP: debugfs: Fix warning around icc_get_name() 2024-03-26 18:19:40 -04:00
parisc parisc/power: Fix power soft-off button emulation on qemu 2024-01-31 16:18:52 -08:00
parport parport: parport_serial: Add Brainboxes device IDs and geometry 2024-01-20 11:51:48 +01:00
pci PCI: Simplify pcie_capability_clear_and_set_word() to ..._clear_word() 2024-04-27 17:11:36 +02:00
pcmcia
peci
perf drivers/perf: hisi: Enable HiSilicon Erratum 162700402 quirk for HIP09 2024-04-13 13:07:36 +02:00
phy phy: tegra: xusb: Add API to retrieve the port number of phy 2024-04-03 15:28:28 +02:00
pinctrl pinctrl: renesas: checker: Limit cfg reg enum checks to provided IDs 2024-04-13 13:07:34 +02:00
platform platform/x86/amd/pmc: Extend Framework 13 quirk to more BIOSes 2024-04-27 17:11:37 +02:00
pmdomain pmdomain: imx8mp-blk-ctrl: imx8mp_blk: Add fdcc clock to hdmimix domain 2024-04-13 13:07:30 +02:00
pnp PNP: ACPI: fix fortify warning 2024-02-05 20:14:15 +00:00
power power: supply: bq27xxx-i2c: Do not free non existing IRQ 2024-03-06 14:48:37 +00:00
powercap powercap: intel_rapl_tpmi: Fix System Domain probing 2024-04-03 15:28:19 +02:00
pps
ps3
ptp
pwm pwm: img: fix pwm clock lookup 2024-04-03 15:28:52 +02:00
rapidio
ras
regulator regulator: userspace-consumer: add module device table 2024-03-26 18:19:34 -04:00
remoteproc remoteproc: virtio: Fix wdg cannot recovery remote processor 2024-04-03 15:28:16 +02:00
reset reset: hisilicon: hi6220: fix Wvoid-pointer-to-enum-cast warning 2024-01-20 11:51:44 +01:00
rpmsg rpmsg: virtio: Free driver_override when rpmsg_remove() 2024-01-31 16:18:50 -08:00
rtc rtc: mt6397: select IRQ_DOMAIN instead of depending on it 2024-03-26 18:20:06 -04:00
s390 s390/cio: fix race condition during online processing 2024-04-27 17:11:34 +02:00
sbus
scsi scsi: core: Fix handling of SCMD_FAIL_IF_RECOVERING 2024-04-27 17:11:30 +02:00
sh
siox
slimbus slimbus: core: Remove usage of the deprecated ida_simple_xx() API 2024-04-03 15:28:28 +02:00
soc soc: fsl: qbman: Use raw spinlock for cgr_lock 2024-04-03 15:28:32 +02:00
soundwire ASoC: Intel: common: DMI remap for rebranded Intel NUC M15 (LAPRC710) laptops 2024-04-13 13:07:34 +02:00
spi spi: mchp-pci1xxx: Fix a possible null pointer dereference in pci1xxx_spi_probe 2024-04-10 16:36:01 +02:00
spmi spmi: mediatek: Fix UAF on device remove 2024-02-05 20:14:32 +00:00
ssb
staging staging: vc04_services: fix information leak in create_component() 2024-04-03 15:28:59 +02:00
target scsi: target: pscsi: Fix bio_put() for error case 2024-03-01 13:34:59 +01:00
tc
tee tee: optee: Fix kernel panic caused by incorrect error handling 2024-04-03 15:28:44 +02:00
thermal thermal/of: Assume polling-delay(-passive) 0 when absent 2024-04-13 13:07:39 +02:00
thunderbolt thunderbolt: Reset topology created by the boot firmware 2024-04-27 17:11:35 +02:00
tty Revert "tty: serial: simplify qcom_geni_serial_send_chunk_fifo()" 2024-04-03 15:28:43 +02:00
ufs scsi: ufs: qcom: Add missing interconnect bandwidth values for Gear 5 2024-04-27 17:11:31 +02:00
uio uio: Fix use-after-free in uio_open 2024-01-20 11:51:48 +01:00
usb usb: new quirk to reduce the SET_ADDRESS request timeout 2024-04-27 17:11:36 +02:00
vdpa vdpa/mlx5: Allow CVQ size changes 2024-03-26 18:20:10 -04:00
vfio vfio/pds: Make sure migration file isn't accessed after reset 2024-04-03 15:28:59 +02:00
vhost vhost: Add smp_rmb() in vhost_enable_notify() 2024-04-17 11:19:35 +02:00
video fbmon: prevent division by zero in fb_videomode_from_videomode() 2024-04-13 13:07:40 +02:00
virt
virtio virtio: reenable config if freezing device failed 2024-04-03 15:28:36 +02:00
vlynq
w1
watchdog watchdog: stm32_iwdg: initialize default timeout 2024-03-26 18:20:02 -04:00
xen x86/xen: attempt to inflate the memory balloon on PVH 2024-04-13 13:07:39 +02:00
zorro
Kconfig
Makefile