[PATCH v2] media: mediatek: vcodec: Initialize context lock before firmware setup

From: Runyu Xiao

Date: Thu Oct 08 2026 - 08:15:11 EST


The normal encoder open path cannot take dev_ctx_lock before
video_register_device(), but there is another path during probe.

On VPU-backed devices, mtk_vcodec_probe() selects firmware before it
initializes dev_ctx_lock. VPU firmware setup registers the encoder watchdog
callback during this selection. A watchdog timeout on the shared VPU
queues reset work, which invokes the registered callbacks. If it runs
before spin_lock_init(), the encoder callback takes the uninitialized lock.

With CONFIG_DEBUG_SPINLOCK=y, an x86_64 QEMU test used a zeroed lock,
initialized an empty context list, and invoked a callback with matching
lock and list operations. It reported:

[ 1.761312] BUG: spinlock bad magic on CPU#0, busybox/58

The lock dump showed .magic: 00000000.

The test reproduces the debug lock diagnostic, not a real VPU watchdog
timeout or its timing during probe. The context list is initialized but
remains empty until video registration, so this shows invalid lock use, not
list corruption or a user-visible failure.

Initialize dev_ctx_lock before firmware setup registers the callback.

Fixes: afaaf3a0f647 ("media: mediatek: vcodec: adding lock to protect encoder context list")
Cc: stable@xxxxxxxxxxxxxxx
Assisted-by: LLM
Signed-off-by: Runyu Xiao <runyu.xiao@xxxxxxxxxx>
---
.../media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c b/drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c
index 4e4541b2f..e6cd3817a 100644
--- a/drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c
+++ b/drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c
@@ -255,6 +255,7 @@ static int mtk_vcodec_probe(struct platform_device *pdev)

INIT_LIST_HEAD(&dev->ctx_list);
dev->plat_dev = pdev;
+ spin_lock_init(&dev->dev_ctx_lock);

if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vpu",
&rproc_phandle)) {
@@ -308,7 +309,6 @@ static int mtk_vcodec_probe(struct platform_device *pdev)

mutex_init(&dev->enc_mutex);
mutex_init(&dev->dev_mutex);
- spin_lock_init(&dev->dev_ctx_lock);
spin_lock_init(&dev->irqlock);

snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
--
2.34.1