media: rkvdec: fix the clk leaks on unbind
Subsystems (3)
v5 cover media: rkvdec: fix the clk leaks on unbind
Hi,
This is v5, the one Nicolas was waiting for after my self-review of v4. v4 was a single patch; it is now three.
Patch 1 splits the V4L2 teardown in remove(): the media and video devices are unregistered first and the V4L2 device is released last. It is the split I proposed in my reply to v4, moved to the front so that no step of the series leaves /dev/videoN registered after the empty IOMMU domain is freed. Patch 2 is Jonas Karlman's fix for the clock reference leak on unbind, with his authorship, rebased on patch 1. Patch 3 makes remove() suspend the device also when something still holds a runtime PM usage count.
Patches 1 and 3 should fix both leaks without patch 2; I did not measure that combination. I kept patch 2 because it is Jonas's and it is the smaller fix. Patches 2 and 3 carry the Fixes tag and need patch 1: without it they call the suspend callback after the driver data is cleared.
Base: media-committers next at 04b69b3a8acc. Igor Velkov's "Do not keep the clocks enabled from probe" is not in it yet. I tested with it applied on top, rebuilt locally from the list posting. With it the idle enable count is 0, so the v4 numbers (1 -> 2 and 1 -> 1) no longer apply.
Test: ROCK 5B+ (RK3588), one decoder instance, fdc38000.video-codec, five clocks. After one unbind while idle and six unbinds with power/control set to "on", starting from 0, the final clk enable count is:
base (next + Velkov) 7 patches 1-2 6 patches 1-3 0
An unbind while idle runs remove() with the autosuspend timer still pending. The base leaves the clocks enabled there, and patch 2 fixes
it: the count is 0 after that unbind with patches 1-2. An unbind with a usage count held, which is what power/control "on" does, cannot suspend through autosuspend. That case still leaks one count per unbind with patches 1-2, and patch 3 fixes it.
With the device active the count reads 2, not 1, because the power domain also enables the clocks through its pm_clk list (3 on hclk and aclk, which the IOMMU uses too).
What I did not test:
- No decode was running during any unbind. A job already running when remove() starts is still not waited for.
- All tests ran with Velkov's change applied; I did not test the series on plain media next.
- CONFIG_PM=n: not built; arm64 selects PM through ARCH_ROCKCHIP. With PM=n pm_runtime_force_suspend() is a stub that returns 0.
- I did not exercise the error path of pm_runtime_force_suspend(), which turns runtime PM back on.
- One board only.
The driver builds with W=1 and no warnings after each patch.
Changes since v4:
- Rebased on media next (04b69b3a8acc).
- Split in three: the teardown split is new patch 1, Jonas's fix is patch 2, patch 3 is new.
- Dropped the Assisted-by tags. The tools are declared below.
- New measurements, redone on the new base.
v4: https://lore.kernel.org/all/[email protected]/
I used Claude (Anthropic) to draft the commit messages, the code of patches 1 and 3 and the measurement script. I reviewed every line, ran the tests on my board and checked the raw counts.
Francesco Saverio Pavone (2):
media: rkvdec: split the V4L2 teardown and unregister the devices
first
media: rkvdec: suspend the device in remove when a usage count is held
Jonas Karlman (1):
media: rkvdec: fix clk reference leak on unbind
drivers/media/platform/rockchip/rkvdec/rkvdec.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
base-commit: 04b69b3a8acc04460cebf03820e2ee1ec77b549b
Signature · 1 line
2.54.0
PATCH v5 1/3 media: rkvdec: split the V4L2 teardown and unregister the devices first
rkvdec_remove() tears the whole V4L2 side down in one call, before runtime PM is disabled and the empty IOMMU domain is freed.
The next patch lets the runtime suspend callback run during remove(). rkvdec_runtime_suspend() looks its state up with dev_get_drvdata(), and v4l2_device_unregister() clears it: struct rkvdec_dev has v4l2_device as its first member, so &rkvdec->v4l2_dev and rkvdec are the same address and the check in v4l2_device_disconnect() matches. The V4L2 device therefore has to be released after the PM teardown.
Moving all of rkvdec_v4l2_cleanup() there would leave /dev/videoN registered while the empty IOMMU domain is freed, and a job queued in that window would reach rkvdec_iommu_restore() with a freed domain.
Split the teardown instead. Unregister the media and video devices first, which stops new ioctls from queueing jobs, and release the m2m and V4L2 devices last. The PM calls stay as they are.
--- drivers/media/platform/rockchip/rkvdec/rkvdec.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/media/platform/rockchip/rkvdec/rkvdec.c +9 -2
@@ -1432,11 +1432,15 @@ static int rkvdec_v4l2_init(struct rkvdec_dev *rkvdec)return ret; } -static void rkvdec_v4l2_cleanup (struct rkvdec_dev *rkvdec) +static void rkvdec_v4l2_unregister (struct rkvdec_dev *rkvdec) {media_device_unregister (&rkvdec->mdev);v4l2_m2m_unregister_media_controller (rkvdec->m2m_dev);video_unregister_device (&rkvdec->vdev); +} + +static void rkvdec_v4l2_release (struct rkvdec_dev *rkvdec) +{media_device_cleanup (&rkvdec->mdev);v4l2_m2m_release (rkvdec->m2m_dev);v4l2_device_unregister (&rkvdec->v4l2_dev);
@@ -1897,12 +1901,15 @@ static void rkvdec_remove(struct platform_device *pdev)cancel_delayed_work_sync (&rkvdec->watchdog_work); -rkvdec_v4l2_cleanup (rkvdec); +rkvdec_v4l2_unregister (rkvdec); +pm_runtime_disable (&pdev->dev);pm_runtime_dont_use_autosuspend (&pdev->dev);if (rkvdec->empty_domain)iommu_domain_free (rkvdec->empty_domain); + +rkvdec_v4l2_release (rkvdec); }static int rkvdec_runtime_resume (struct device *dev)
Signature · 1 line
2.54.0
PATCH v5 2/3 media: rkvdec: fix clk reference leak on unbind
In reply to Francesco Saverio PavoneFrom: Jonas Karlman <[email protected]>
remove() calls pm_runtime_disable() before pm_runtime_dont_use_autosuspend(), so the second call can never suspend the device: it reaches rpm_idle(), which returns -EACCES once PM runtime is disabled. The probe error path has had the two the other way round since the driver was merged.
This shows up when the device is unbound while the 100ms autosuspend window is still open, which is what an rmmod right after a decode does. device_release_driver() calls pm_runtime_put_sync() before .remove(), and rpm_idle() adds RPM_AUTO on its own, so that put only arms the autosuspend timer. pm_runtime_disable() then cancels the timer, and pm_runtime_reinit() relabels the device suspended without calling the driver back. The clk_bulk reference taken by rkvdec_runtime_resume() is never dropped, and a later probe does not reclaim it, so every such unbind leaks one enable count.
Drop autosuspend first, so the callback still runs and drops the reference rkvdec_runtime_resume() took.
The callback needs the driver data, which v4l2_device_unregister() clears. The previous patch moved that call to the end of remove(), so the callback can run here. Swapping the two PM calls without it oopses whenever the device is still active at unbind.
[fsp: moved pm_runtime_disable() below iommu_domain_free() to fit the
teardown split, wrote the commit message]
--- drivers/media/platform/rockchip/rkvdec/rkvdec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/rockchip/rkvdec/rkvdec.c +1 -1
@@ -1903,12 +1903,12 @@ static void rkvdec_remove(struct platform_device *pdev)rkvdec_v4l2_unregister (rkvdec); -pm_runtime_disable (&pdev->dev);pm_runtime_dont_use_autosuspend (&pdev->dev);if (rkvdec->empty_domain)iommu_domain_free (rkvdec->empty_domain); +pm_runtime_disable (&pdev->dev);rkvdec_v4l2_release (rkvdec); }
Signature · 1 line
2.54.0
PATCH v5 3/3 media: rkvdec: suspend the device in remove when a usage count is held
In reply to Francesco Saverio Pavonerkvdec_remove() counts on pm_runtime_dont_use_autosuspend() to suspend the device before runtime PM is disabled. That works only when the usage count is zero. If something still holds a count, for example power/control set to "on", the device stays active, the driver core marks it suspended without calling the callback, and the clocks enabled in rkvdec_runtime_resume() are never disabled. Each unbind in that state leaves the enable count of the five clocks one higher.
Use pm_runtime_force_suspend() instead of pm_runtime_disable(). It disables runtime PM and runs the suspend callback if the device is still active.
The probe error path keeps pm_runtime_disable(): the driver has not resumed the device at that point.
--- drivers/media/platform/rockchip/rkvdec/rkvdec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/rockchip/rkvdec/rkvdec.c +1 -1
@@ -1908,7 +1908,7 @@ static void rkvdec_remove(struct platform_device *pdev)if (rkvdec->empty_domain)iommu_domain_free (rkvdec->empty_domain); -pm_runtime_disable (&pdev->dev); +pm_runtime_force_suspend (&pdev->dev);rkvdec_v4l2_release (rkvdec); }
Signature · 1 line
2.54.0