* [PATCH] media: verisilicon: suspend the device before unregistering it on remove
@ 2026-11-06 9:04 Sergei Khomenkov via B4 Relay
2026-11-06 18:46 ` Nicolas Dufresne
2026-11-23 19:56 ` Nicolas Dufresne
0 siblings, 2 replies; 4+ messages in thread
From: Sergei Khomenkov via B4 Relay @ 2026-11-06 9:04 UTC (permalink / raw)
To: Nicolas Dufresne, Benjamin Gaignard, Philipp Zabel
Cc: Mauro Carvalho Chehab, Hans Verkuil, Heiko Stuebner, Frank Li,
Tharit Tangkijwanichakul, linux-media, linux-rockchip,
linux-arm-kernel, linux-kernel, stable, Sergei Khomenkov
From: Sergei Khomenkov <shomenkow@gmail.com>
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.
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.
Fixes: 556ab0b2741d ("media: hantro: release runtime resources when device_run fails")
Cc: stable@vger.kernel.org
Signed-off-by: Sergei Khomenkov <shomenkow@gmail.com>
---
drivers/media/platform/verisilicon/hantro_drv.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/media/platform/verisilicon/hantro_drv.c b/drivers/media/platform/verisilicon/hantro_drv.c
index 26e5d5547466..8af47375d471 100644
--- a/drivers/media/platform/verisilicon/hantro_drv.c
+++ b/drivers/media/platform/verisilicon/hantro_drv.c
@@ -1286,10 +1286,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)) {
+ clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
+ 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);
}
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261106-hantro-remove-9afbc9b601f9
Best regards,
--
Sergei Khomenkov <shomenkow@gmail.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] media: verisilicon: suspend the device before unregistering it on remove
2026-11-06 9:04 [PATCH] media: verisilicon: suspend the device before unregistering it on remove Sergei Khomenkov via B4 Relay
@ 2026-11-06 18:46 ` Nicolas Dufresne
2026-11-07 10:16 ` Sergei Khomenkov
2026-11-23 19:56 ` Nicolas Dufresne
1 sibling, 1 reply; 4+ messages in thread
From: Nicolas Dufresne @ 2026-11-06 18:46 UTC (permalink / raw)
To: shomenkow, Benjamin Gaignard, Philipp Zabel
Cc: Mauro Carvalho Chehab, Hans Verkuil, Heiko Stuebner, Frank Li,
Tharit Tangkijwanichakul, linux-media, linux-rockchip,
linux-arm-kernel, linux-kernel, stable
[-- Attachment #1: Type: text/plain, Size: 4210 bytes --]
Le vendredi 06 novembre 2026 à 12:04 +0300, Sergei Khomenkov via B4 Relay a écrit :
> From: Sergei Khomenkov <shomenkow@gmail.com>
>
> 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.
>
> 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.
>
> Fixes: 556ab0b2741d ("media: hantro: release runtime resources when device_run fails")
> Cc: stable@vger.kernel.org
> Signed-off-by: Sergei Khomenkov <shomenkow@gmail.com>
> ---
> drivers/media/platform/verisilicon/hantro_drv.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/media/platform/verisilicon/hantro_drv.c b/drivers/media/platform/verisilicon/hantro_drv.c
> index 26e5d5547466..8af47375d471 100644
> --- a/drivers/media/platform/verisilicon/hantro_drv.c
> +++ b/drivers/media/platform/verisilicon/hantro_drv.c
> @@ -1286,10 +1286,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)) {
> + clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
> + pm_runtime_set_suspended(vpu->dev);
> + }
> + v4l2_device_unregister(&vpu->v4l2_dev);
To be verified, but its not entirely logical here. My impression was that you
want to unregister the dev entry sooner, so that it prevent newly open, and turn
existing fh into a mode where all ioctl will fail.
Can you do some extra code inspection and report why you strictly need to modify
the clock states before you unregister the device?
p.s. are you sure outside of test build, that PM can be disabled on any of the
platform hantro driver runs ?
> + clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
> reset_control_assert(vpu->resets);
> }
>
>
> ---
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> change-id: 20261106-hantro-remove-9afbc9b601f9
>
> Best regards,
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] media: verisilicon: suspend the device before unregistering it on remove
2026-11-06 18:46 ` Nicolas Dufresne
@ 2026-11-07 10:16 ` Sergei Khomenkov
0 siblings, 0 replies; 4+ messages in thread
From: Sergei Khomenkov @ 2026-11-07 10:16 UTC (permalink / raw)
To: Nicolas Dufresne
Cc: Benjamin Gaignard, Philipp Zabel, Mauro Carvalho Chehab,
Hans Verkuil, Heiko Stuebner, Frank Li, Tharit Tangkijwanichakul,
linux-media, linux-rockchip, linux-arm-kernel, linux-kernel,
stable
Hi Nicolas,
> To be verified, but its not entirely logical here. My impression was that you
> want to unregister the dev entry sooner, so that it prevent newly open, and turn
> existing fh into a mode where all ioctl will fail.
That part stays first. media_device_unregister() and
hantro_remove_{dec,enc}_func() still run at the top of hantro_remove().
The patch only moves v4l2_device_unregister() and clk_bulk_unprepare()
below the runtime PM calls. v4l2_device_unregister() touches no node
here: it clears the driver data, which equals &vpu->v4l2_dev (the first
member of struct hantro_dev), and drops the device reference.
> Can you do some extra code inspection and report why you strictly need to modify
> the clock states before you unregister the device?
hantro_runtime_suspend() reads the driver data, and with power/control
at "auto" and the default 100 ms autosuspend delay remove() calls it.
The driver core resumes the device before remove(), and its put only
arms the autosuspend timer. So pm_runtime_dont_use_autosuspend()
suspends the device synchronously, the path in the 8e26d4c20ed2 trace.
With the current order the driver data is NULL by then: an unbind or
module unload oopses, after clk_bulk_unprepare() has warned
"Unpreparing enabled". (My reading of the code: with autosuspend_delay_ms
at 0 the device is already suspended when remove() runs; I did not test
that case.)
So two things are strict: runtime PM stopped before
v4l2_device_unregister(), and the clocks disabled before
clk_bulk_unprepare(). The new clk_bulk_disable() does not use the driver
data, so it only has to come before the unprepare.
I suspend through the PM core, not by hand, so that the runtime suspend,
with any genpd power-off, still runs before reset_control_assert(), as
8e26d4c20ed2 wants. I could only test on RK3026, whose VPU has no power
domain, resets or IOMMU. A run on RK3588 or i.MX8M would help.
> p.s. are you sure outside of test build, that PM can be disabled on any of the
> platform hantro driver runs ?
If you mean CONFIG_PM=n: not on Rockchip or i.MX8M, which select PM. On
sunxi, AT91 and STM32 PM is only a default (SUSPEND=y selects PM_SLEEP),
so it can be turned off, but then the driver never enables its clocks:
only hantro_runtime_resume() does. The IS_ENABLED(CONFIG_PM) test is for
such builds and COMPILE_TEST. There pm_runtime_status_suspended()
returns false, so unbind would disable clocks that were never enabled
and hit the "already disabled" WARN. I can drop it if you prefer.
The block itself is for runtime PM forbidden through sysfs
(power/control set to "on"), which root or a udev rule can do on any
platform. Then nothing suspends the device, and it is still active after
pm_runtime_disable(). I tested that case on the board. cedrus and
mali-c55 (2c9b9bcc2569) power off by hand in remove() the same way.
Thanks,
Sergei
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] media: verisilicon: suspend the device before unregistering it on remove
2026-11-06 9:04 [PATCH] media: verisilicon: suspend the device before unregistering it on remove Sergei Khomenkov via B4 Relay
2026-11-06 18:46 ` Nicolas Dufresne
@ 2026-11-23 19:56 ` Nicolas Dufresne
1 sibling, 0 replies; 4+ messages in thread
From: Nicolas Dufresne @ 2026-11-23 19:56 UTC (permalink / raw)
To: shomenkow, Benjamin Gaignard, Philipp Zabel
Cc: Mauro Carvalho Chehab, Hans Verkuil, Heiko Stuebner, Frank Li,
Tharit Tangkijwanichakul, linux-media, linux-rockchip,
linux-arm-kernel, linux-kernel, stable
[-- Attachment #1: Type: text/plain, Size: 4036 bytes --]
Hi,
ack on your previous feedback.
Le vendredi 06 novembre 2026 à 12:04 +0300, Sergei Khomenkov via B4 Relay a
écrit :
> From: Sergei Khomenkov <shomenkow@gmail.com>
>
> 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.
>
> 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.
I'll drop that paragraph.
>
> Fixes: 556ab0b2741d ("media: hantro: release runtime resources when device_run fails")
> Cc: stable@vger.kernel.org
> Signed-off-by: Sergei Khomenkov <shomenkow@gmail.com>
> ---
> drivers/media/platform/verisilicon/hantro_drv.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/media/platform/verisilicon/hantro_drv.c b/drivers/media/platform/verisilicon/hantro_drv.c
> index 26e5d5547466..8af47375d471 100644
> --- a/drivers/media/platform/verisilicon/hantro_drv.c
> +++ b/drivers/media/platform/verisilicon/hantro_drv.c
> @@ -1286,10 +1286,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)) {
> + clk_bulk_disable(vpu->variant->num_clocks, vpu->clocks);
> + pm_runtime_set_suspended(vpu->dev);
I will edit to:
hantro_runtime_suspend(vpu->dev);
Its exactly the same, it just keep the implementation in one place, its more
future proof.
Reviewed-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
> + }
> + v4l2_device_unregister(&vpu->v4l2_dev);
> + clk_bulk_unprepare(vpu->variant->num_clocks, vpu->clocks);
> reset_control_assert(vpu->resets);
> }
>
>
> ---
> base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
> change-id: 20261106-hantro-remove-9afbc9b601f9
>
> Best regards,
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-23 19:56 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-06 9:04 [PATCH] media: verisilicon: suspend the device before unregistering it on remove Sergei Khomenkov via B4 Relay
2026-11-06 18:46 ` Nicolas Dufresne
2026-11-07 10:16 ` Sergei Khomenkov
2026-11-23 19:56 ` Nicolas Dufresne
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®