Skip to content

Commit 6467cda

Browse files
yunfei-mtkHans Verkuil
authored andcommitted
media: mediatek: vcodec: adding lock to protect decoder context list
Add a lock for the ctx_list, to avoid accessing a NULL pointer within the 'vpu_dec_ipi_handler' function when the ctx_list has been deleted due to an unexpected behavior on the SCP IP block. Hardware name: Google juniper sku16 board (DT) pstate: 20400005 (nzCv daif +PAN -UAO -TCO BTYPE=--) pc : vpu_dec_ipi_handler+0x58/0x1f8 [mtk_vcodec_dec] lr : scp_ipi_handler+0xd0/0x194 [mtk_scp] sp : ffffffc0131dbbd0 x29: ffffffc0131dbbd0 x28: 0000000000000000 x27: ffffff9bb277f348 x26: ffffff9bb242ad00 x25: ffffffd2d440d3b8 x24: ffffffd2a13ff1d4 x23: ffffff9bb7fe85a0 x22: ffffffc0133fbdb0 x21: 0000000000000010 x20: ffffff9b050ea328 x19: ffffffc0131dbc08 x18: 0000000000001000 x17: 0000000000000000 x16: ffffffd2d461c6e0 x15: 0000000000000242 x14: 000000000000018f x13: 000000000000004d x12: 0000000000000000 x11: 0000000000000001 x10: fffffffffffffff0 x9 : ffffff9bb6e793a8 x8 : 0000000000000000 x7 : 0000000000000000 x6 : 000000000000003f x5 : 0000000000000040 x4 : fffffffffffffff0 x3 : 0000000000000020 x2 : ffffff9bb6e79080 x1 : 0000000000000010 x0 : ffffffc0131dbc08 Call trace: vpu_dec_ipi_handler+0x58/0x1f8 [mtk_vcodec_dec (HASH:6c3f 2)] scp_ipi_handler+0xd0/0x194 [mtk_scp (HASH:7046 3)] mt8183_scp_irq_handler+0x44/0x88 [mtk_scp (HASH:7046 3)] scp_irq_handler+0x48/0x90 [mtk_scp (HASH:7046 3)] irq_thread_fn+0x38/0x94 irq_thread+0x100/0x1c0 kthread+0x140/0x1fc ret_from_fork+0x10/0x30 Code: 54000088 f94ca50a eb14015f 54000060 (f9400108) ---[ end trace ace43ce36cbd5c93 ]--- Kernel panic - not syncing: Oops: Fatal exception SMP: stopping secondary CPUs Kernel Offset: 0x12c4000000 from 0xffffffc010000000 PHYS_OFFSET: 0xffffffe580000000 CPU features: 0x08240002,2188200c Memory Limit: none Fixes: 655b86e ("media: mediatek: vcodec: Fix possible invalid memory access for decoder") Signed-off-by: Yunfei Dong <yunfei.dong@mediatek.com> Reviewed-by: Nicolas Dufresne <nicolas.dufresne@collabora.com> Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> Signed-off-by: Sebastian Fricke <sebastian.fricke@collabora.com> Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
1 parent 97c75ee commit 6467cda

4 files changed

Lines changed: 11 additions & 2 deletions

File tree

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vpu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ static void mtk_vcodec_vpu_reset_dec_handler(void *priv)
5050

5151
dev_err(&dev->plat_dev->dev, "Watchdog timeout!!");
5252

53-
mutex_lock(&dev->dev_mutex);
53+
mutex_lock(&dev->dev_ctx_lock);
5454
list_for_each_entry(ctx, &dev->ctx_list, list) {
5555
ctx->state = MTK_STATE_ABORT;
5656
mtk_v4l2_vdec_dbg(0, ctx, "[%d] Change to state MTK_STATE_ABORT", ctx->id);
5757
}
58-
mutex_unlock(&dev->dev_mutex);
58+
mutex_unlock(&dev->dev_ctx_lock);
5959
}
6060

6161
static void mtk_vcodec_vpu_reset_enc_handler(void *priv)

drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ static int fops_vcodec_open(struct file *file)
268268

269269
ctx->dev->vdec_pdata->init_vdec_params(ctx);
270270

271+
mutex_lock(&dev->dev_ctx_lock);
271272
list_add(&ctx->list, &dev->ctx_list);
273+
mutex_unlock(&dev->dev_ctx_lock);
272274
mtk_vcodec_dbgfs_create(ctx);
273275

274276
mutex_unlock(&dev->dev_mutex);
@@ -311,7 +313,9 @@ static int fops_vcodec_release(struct file *file)
311313
v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
312314

313315
mtk_vcodec_dbgfs_remove(dev, ctx->id);
316+
mutex_lock(&dev->dev_ctx_lock);
314317
list_del_init(&ctx->list);
318+
mutex_unlock(&dev->dev_ctx_lock);
315319
kfree(ctx);
316320
mutex_unlock(&dev->dev_mutex);
317321
return 0;
@@ -404,6 +408,7 @@ static int mtk_vcodec_probe(struct platform_device *pdev)
404408
for (i = 0; i < MTK_VDEC_HW_MAX; i++)
405409
mutex_init(&dev->dec_mutex[i]);
406410
mutex_init(&dev->dev_mutex);
411+
mutex_init(&dev->dev_ctx_lock);
407412
spin_lock_init(&dev->irqlock);
408413

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

drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ struct mtk_vcodec_dec_ctx {
241241
*
242242
* @dec_mutex: decoder hardware lock
243243
* @dev_mutex: video_device lock
244+
* @dev_ctx_lock: the lock of context list
244245
* @decode_workqueue: decode work queue
245246
*
246247
* @irqlock: protect data access by irq handler and work thread
@@ -282,6 +283,7 @@ struct mtk_vcodec_dec_dev {
282283
/* decoder hardware mutex lock */
283284
struct mutex dec_mutex[MTK_VDEC_HW_MAX];
284285
struct mutex dev_mutex;
286+
struct mutex dev_ctx_lock;
285287
struct workqueue_struct *decode_workqueue;
286288

287289
spinlock_t irqlock;

drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ static bool vpu_dec_check_ap_inst(struct mtk_vcodec_dec_dev *dec_dev, struct vde
7777
struct mtk_vcodec_dec_ctx *ctx;
7878
int ret = false;
7979

80+
mutex_lock(&dec_dev->dev_ctx_lock);
8081
list_for_each_entry(ctx, &dec_dev->ctx_list, list) {
8182
if (!IS_ERR_OR_NULL(ctx) && ctx->vpu_inst == vpu) {
8283
ret = true;
8384
break;
8485
}
8586
}
87+
mutex_unlock(&dec_dev->dev_ctx_lock);
8688

8789
return ret;
8890
}

0 commit comments

Comments
 (0)