[PATCH v2] media: verisilicon: suspend the device before unregistering it on remove
From: Nicolas Dufresne
Date: Mon Nov 23 2026 - 15:17:46 EST
From: Sergei Khomenkov <shomenkow@xxxxxxxxx>
The clocks are enabled and disabled by the runtime PM callbacks.
hantro_remove() calls v4l2_device_unregister() and clk_bulk_unprepare()
first and only then pm_runtime_dont_use_autosuspend(), which suspends
an active device right away. By then v4l2_device_unregister() has
cleared the driver data (it points to the v4l2_device, the first member
of struct hantro_dev), so hantro_runtime_suspend() dereferences NULL.
The device is always active at that point: the driver core resumes it
before calling remove(), and its put only starts the 100 ms autosuspend
timer. So every unbind and every module unload, with or without a
decode before it, gives an "Unpreparing enabled" warning for each clock
and then that oops.
Stop runtime PM before the unregister and the unprepare. If runtime PM
is forbidden through sysfs (power/control set to "on"), the device is
still active after that, so disable its clocks and mark it suspended by
hand.
The probe error path unwinds in the same order. It can only hit this if
a job runs between the encoder node registration and a later probe
failure, so it is left for a follow-up.
Fixes: 556ab0b2741d ("media: hantro: release runtime resources when device_run fails")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Sergei Khomenkov <shomenkow@xxxxxxxxx>
Reviewed-by: Nicolas Dufresne <nicolas.dufresne@xxxxxxxxxxxxx>
Signed-off-by: Nicolas Dufresne <nicolas.dufresne@xxxxxxxxxxxxx>
---
Changes in v2 (maintainer update):
- Slimmed commit message
- Re-use hantro_runtime_suspend() instead of open coding
- This required moving code around, hence posting it to be clear
Moved outside of the commit message:
Found with an LLM coding assistant while porting mainline Linux to a
Rockchip RK3026 board (G1, ID 0x6731); the assistant also drafted this
change. Tested on that board. Without this change, unloading the idle
module (runtime status "suspended") gives the warnings and the oops.
With it, the idle module unloads with no warning or oops, and so it does
right after an H.264 or VP8 decode with the autosuspend delay raised to
10 s through sysfs; the clock enable and prepare counts are back to
their values before the load. The same holds with power/control set to
"on", over three load, decode and unload cycles on one boot. kmemleak
reports nothing after five load, decode and unload cycles. Built with
W=1 with and without CONFIG_PM. Not tested on other SoCs or with a
power domain.
.../media/platform/verisilicon/hantro_drv.c | 26 ++++++++++++-------
1 file changed, 16 insertions(+), 10 deletions(-)
diff --git a/drivers/media/platform/verisilicon/hantro_drv.c b/drivers/media/platform/verisilicon/hantro_drv.c
index 70a2bb39777f3..55480ff1ca57f 100644
--- a/drivers/media/platform/verisilicon/hantro_drv.c
+++ b/drivers/media/platform/verisilicon/hantro_drv.c
@@ -1285,6 +1285,14 @@ static int hantro_probe(struct platform_device *pdev)
return ret;
}
+static int hantro_runtime_suspend(struct device *dev)
+{
+ struct hantro_dev *vpu = dev_get_drvdata(dev);
+
+ clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+ return 0;
+}
+
static void hantro_remove(struct platform_device *pdev)
{
struct hantro_dev *vpu = platform_get_drvdata(pdev);
@@ -1296,10 +1304,16 @@ static void hantro_remove(struct platform_device *pdev)
hantro_remove_enc_func(vpu);
media_device_cleanup(&vpu->mdev);
v4l2_m2m_put(vpu->m2m_dev);
- v4l2_device_unregister(&vpu->v4l2_dev);
- clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
+ /* Suspend first: v4l2_device_unregister() clears the driver data. */
pm_runtime_dont_use_autosuspend(vpu->dev);
pm_runtime_disable(vpu->dev);
+ /* Runtime PM forbidden through sysfs: the device is still active. */
+ if (IS_ENABLED(CONFIG_PM) && !pm_runtime_status_suspended(vpu->dev)) {
+ hantro_runtime_suspend(vpu->dev);
+ pm_runtime_set_suspended(vpu->dev);
+ }
+ v4l2_device_unregister(&vpu->v4l2_dev);
+ clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
reset_control_assert(vpu->resets);
}
@@ -1325,14 +1339,6 @@ static int hantro_runtime_resume(struct device *dev)
return ret;
}
-static int hantro_runtime_suspend(struct device *dev)
-{
- struct hantro_dev *vpu = dev_get_drvdata(dev);
-
- clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
- return 0;
-}
-
static DEFINE_RUNTIME_DEV_PM_OPS(hantro_pm_ops, hantro_runtime_suspend,
hantro_runtime_resume, NULL);
--
2.56.1
Attachment:
signature.asc
Description: This is a digitally signed message part