Skip to content

Commit afaaf3a

Browse files
yunfei-mtkHans Verkuil
authored andcommitted
media: mediatek: vcodec: adding lock to protect encoder context list
Add a lock for the ctx_list, to avoid accessing a NULL pointer within the 'vpu_enc_ipi_handler' function when the ctx_list has been deleted due to an unexpected behavior on the SCP IP block. Fixes: 1972e32 ("media: mediatek: vcodec: Fix possible invalid memory access for encoder") 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 6467cda commit afaaf3a

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
@@ -65,12 +65,12 @@ static void mtk_vcodec_vpu_reset_enc_handler(void *priv)
6565

6666
dev_err(&dev->plat_dev->dev, "Watchdog timeout!!");
6767

68-
mutex_lock(&dev->dev_mutex);
68+
mutex_lock(&dev->dev_ctx_lock);
6969
list_for_each_entry(ctx, &dev->ctx_list, list) {
7070
ctx->state = MTK_STATE_ABORT;
7171
mtk_v4l2_vdec_dbg(0, ctx, "[%d] Change to state MTK_STATE_ABORT", ctx->id);
7272
}
73-
mutex_unlock(&dev->dev_mutex);
73+
mutex_unlock(&dev->dev_ctx_lock);
7474
}
7575

7676
static const struct mtk_vcodec_fw_ops mtk_vcodec_vpu_msg = {

drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ static int fops_vcodec_open(struct file *file)
177177
mtk_v4l2_venc_dbg(2, ctx, "Create instance [%d]@%p m2m_ctx=%p ",
178178
ctx->id, ctx, ctx->m2m_ctx);
179179

180+
mutex_lock(&dev->dev_ctx_lock);
180181
list_add(&ctx->list, &dev->ctx_list);
182+
mutex_unlock(&dev->dev_ctx_lock);
181183

182184
mutex_unlock(&dev->dev_mutex);
183185
mtk_v4l2_venc_dbg(0, ctx, "%s encoder [%d]", dev_name(&dev->plat_dev->dev),
@@ -212,7 +214,9 @@ static int fops_vcodec_release(struct file *file)
212214
v4l2_fh_exit(&ctx->fh);
213215
v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
214216

217+
mutex_lock(&dev->dev_ctx_lock);
215218
list_del_init(&ctx->list);
219+
mutex_unlock(&dev->dev_ctx_lock);
216220
kfree(ctx);
217221
mutex_unlock(&dev->dev_mutex);
218222
return 0;
@@ -294,6 +298,7 @@ static int mtk_vcodec_probe(struct platform_device *pdev)
294298

295299
mutex_init(&dev->enc_mutex);
296300
mutex_init(&dev->dev_mutex);
301+
mutex_init(&dev->dev_ctx_lock);
297302
spin_lock_init(&dev->irqlock);
298303

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

drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ struct mtk_vcodec_enc_ctx {
178178
*
179179
* @enc_mutex: encoder hardware lock.
180180
* @dev_mutex: video_device lock
181+
* @dev_ctx_lock: the lock of context list
181182
* @encode_workqueue: encode work queue
182183
*
183184
* @enc_irq: h264 encoder irq resource
@@ -205,6 +206,7 @@ struct mtk_vcodec_enc_dev {
205206
/* encoder hardware mutex lock */
206207
struct mutex enc_mutex;
207208
struct mutex dev_mutex;
209+
struct mutex dev_ctx_lock;
208210
struct workqueue_struct *encode_workqueue;
209211

210212
int enc_irq;

drivers/media/platform/mediatek/vcodec/encoder/venc_vpu_if.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ static bool vpu_enc_check_ap_inst(struct mtk_vcodec_enc_dev *enc_dev, struct ven
4747
struct mtk_vcodec_enc_ctx *ctx;
4848
int ret = false;
4949

50+
mutex_lock(&enc_dev->dev_ctx_lock);
5051
list_for_each_entry(ctx, &enc_dev->ctx_list, list) {
5152
if (!IS_ERR_OR_NULL(ctx) && ctx->vpu_inst == vpu) {
5253
ret = true;
5354
break;
5455
}
5556
}
57+
mutex_unlock(&enc_dev->dev_ctx_lock);
5658

5759
return ret;
5860
}

0 commit comments

Comments
 (0)