From: Arun Iyengar Since commit bbd267daf4fc ("media: verisilicon: vp9: Allow to change resolution while streaming") the CAPTURE format of a decoder can grow while CAPTURE buffers are queued: by S_FMT on either queue, or when the HEVC, VP9 or AV1 controls of a request change the bit depth (AV1: also film grain). hantro_buf_prepare() checked those buffers against the old format, so the decoder or the post-processor is started on a buffer that is too small. Check the CAPTURE buffer against the format again in hantro_start_prepare_run(), after the request controls are applied, and fail the job if it does not fit. Fixes: bbd267daf4fc ("media: verisilicon: vp9: Allow to change resolution while streaming") Cc: stable@vger.kernel.org Signed-off-by: Arun Iyengar --- drivers/media/platform/verisilicon/hantro_drv.c | 28 +++++++++++++++++++++- .../platform/verisilicon/hantro_g1_mpeg2_dec.c | 7 +++++- .../media/platform/verisilicon/hantro_g1_vp8_dec.c | 7 +++++- .../media/platform/verisilicon/hantro_g2_vp9_dec.c | 5 +++- drivers/media/platform/verisilicon/hantro_h264.c | 5 +++- drivers/media/platform/verisilicon/hantro_hevc.c | 4 +++- drivers/media/platform/verisilicon/hantro_hw.h | 2 +- .../verisilicon/rockchip_vpu2_hw_mpeg2_dec.c | 7 +++++- .../verisilicon/rockchip_vpu2_hw_vp8_dec.c | 7 +++++- .../verisilicon/rockchip_vpu981_hw_av1_dec.c | 4 +++- 10 files changed, 66 insertions(+), 10 deletions(-) diff --git a/drivers/media/platform/verisilicon/hantro_drv.c b/drivers/media/platform/verisilicon/hantro_drv.c index 5c962847fdf5..6e2a90dfc639 100644 --- a/drivers/media/platform/verisilicon/hantro_drv.c +++ b/drivers/media/platform/verisilicon/hantro_drv.c @@ -44,6 +44,22 @@ void *hantro_get_ctrl(struct hantro_ctx *ctx, u32 id) return ctrl ? ctrl->p_cur.p : NULL; } +/* + * The CAPTURE buffers of a decoder are checked against the CAPTURE format when + * they are queued, but the format can change while they are queued. + */ +static bool hantro_dec_buf_fits(struct hantro_ctx *ctx, struct vb2_buffer *buf) +{ + unsigned int i; + + for (i = 0; i < ctx->dst_fmt.num_planes; i++) { + if (vb2_plane_size(buf, i) < ctx->dst_fmt.plane_fmt[i].sizeimage) + return false; + } + + return true; +} + dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts) { struct vb2_queue *q = v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx); @@ -128,7 +144,11 @@ void hantro_watchdog(struct work_struct *work) } } -void hantro_start_prepare_run(struct hantro_ctx *ctx) +/* + * Fails when the CAPTURE buffer is too small for the format that the controls + * of the request set; the caller then calls hantro_abort_prepare_run(). + */ +int hantro_start_prepare_run(struct hantro_ctx *ctx) { struct vb2_v4l2_buffer *src_buf; @@ -136,12 +156,18 @@ void hantro_start_prepare_run(struct hantro_ctx *ctx) v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req, &ctx->ctrl_handler); + if (!ctx->is_encoder && + !hantro_dec_buf_fits(ctx, &hantro_get_dst_buf(ctx)->vb2_buf)) + return -EINVAL; + if (!ctx->is_encoder && !ctx->dev->variant->late_postproc) { if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) hantro_postproc_enable(ctx); else hantro_postproc_disable(ctx); } + + return 0; } /** diff --git a/drivers/media/platform/verisilicon/hantro_g1_mpeg2_dec.c b/drivers/media/platform/verisilicon/hantro_g1_mpeg2_dec.c index 5f1ce21fa6e3..0f4724c6dee7 100644 --- a/drivers/media/platform/verisilicon/hantro_g1_mpeg2_dec.c +++ b/drivers/media/platform/verisilicon/hantro_g1_mpeg2_dec.c @@ -152,12 +152,17 @@ int hantro_g1_mpeg2_dec_run(struct hantro_ctx *ctx) const struct v4l2_ctrl_mpeg2_sequence *seq; const struct v4l2_ctrl_mpeg2_picture *pic; u32 reg; + int ret; src_buf = hantro_get_src_buf(ctx); dst_buf = hantro_get_dst_buf(ctx); /* Apply request controls if any */ - hantro_start_prepare_run(ctx); + ret = hantro_start_prepare_run(ctx); + if (ret) { + hantro_abort_prepare_run(ctx); + return ret; + } seq = hantro_get_ctrl(ctx, V4L2_CID_STATELESS_MPEG2_SEQUENCE); diff --git a/drivers/media/platform/verisilicon/hantro_g1_vp8_dec.c b/drivers/media/platform/verisilicon/hantro_g1_vp8_dec.c index e866ff86019a..041723fd4c91 100644 --- a/drivers/media/platform/verisilicon/hantro_g1_vp8_dec.c +++ b/drivers/media/platform/verisilicon/hantro_g1_vp8_dec.c @@ -438,8 +438,13 @@ int hantro_g1_vp8_dec_run(struct hantro_ctx *ctx) size_t width = ctx->dst_fmt.width; u32 mb_width, mb_height; u32 reg; + int ret; - hantro_start_prepare_run(ctx); + ret = hantro_start_prepare_run(ctx); + if (ret) { + hantro_abort_prepare_run(ctx); + return ret; + } hdr = hantro_get_ctrl(ctx, V4L2_CID_STATELESS_VP8_FRAME); if (WARN_ON(!hdr)) { diff --git a/drivers/media/platform/verisilicon/hantro_g2_vp9_dec.c b/drivers/media/platform/verisilicon/hantro_g2_vp9_dec.c index 78100d1c7e85..f161fd0c626d 100644 --- a/drivers/media/platform/verisilicon/hantro_g2_vp9_dec.c +++ b/drivers/media/platform/verisilicon/hantro_g2_vp9_dec.c @@ -30,9 +30,12 @@ static int start_prepare_run(struct hantro_ctx *ctx, const struct v4l2_ctrl_vp9_ struct hantro_vp9_dec_hw_ctx *vp9_ctx = &ctx->vp9_dec; struct v4l2_ctrl *ctrl; unsigned int fctx_idx; + int ret; /* v4l2-specific stuff */ - hantro_start_prepare_run(ctx); + ret = hantro_start_prepare_run(ctx); + if (ret) + return ret; ctrl = v4l2_ctrl_find(&ctx->ctrl_handler, V4L2_CID_STATELESS_VP9_FRAME); if (WARN_ON(!ctrl)) diff --git a/drivers/media/platform/verisilicon/hantro_h264.c b/drivers/media/platform/verisilicon/hantro_h264.c index 3da3083182fb..2c37d32addc6 100644 --- a/drivers/media/platform/verisilicon/hantro_h264.c +++ b/drivers/media/platform/verisilicon/hantro_h264.c @@ -440,8 +440,11 @@ int hantro_h264_dec_prepare_run(struct hantro_ctx *ctx) struct hantro_h264_dec_hw_ctx *h264_ctx = &ctx->h264_dec; struct hantro_h264_dec_ctrls *ctrls = &h264_ctx->ctrls; struct v4l2_h264_reflist_builder reflist_builder; + int ret; - hantro_start_prepare_run(ctx); + ret = hantro_start_prepare_run(ctx); + if (ret) + return ret; ctrls->scaling = hantro_get_ctrl(ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX); diff --git a/drivers/media/platform/verisilicon/hantro_hevc.c b/drivers/media/platform/verisilicon/hantro_hevc.c index 951ecc27ca0f..ef39d42cef1e 100644 --- a/drivers/media/platform/verisilicon/hantro_hevc.c +++ b/drivers/media/platform/verisilicon/hantro_hevc.c @@ -147,7 +147,9 @@ int hantro_hevc_dec_prepare_run(struct hantro_ctx *ctx) struct hantro_hevc_dec_ctrls *ctrls = &hevc_ctx->ctrls; int ret; - hantro_start_prepare_run(ctx); + ret = hantro_start_prepare_run(ctx); + if (ret) + return ret; ctrls->decode_params = hantro_get_ctrl(ctx, V4L2_CID_STATELESS_HEVC_DECODE_PARAMS); diff --git a/drivers/media/platform/verisilicon/hantro_hw.h b/drivers/media/platform/verisilicon/hantro_hw.h index 2d12e605cb55..38a9d38af134 100644 --- a/drivers/media/platform/verisilicon/hantro_hw.h +++ b/drivers/media/platform/verisilicon/hantro_hw.h @@ -433,7 +433,7 @@ extern const u32 hantro_vp8_dec_mc_filter[8][6]; void hantro_watchdog(struct work_struct *work); void hantro_irq_done(struct hantro_dev *vpu, enum vb2_buffer_state result); -void hantro_start_prepare_run(struct hantro_ctx *ctx); +int hantro_start_prepare_run(struct hantro_ctx *ctx); void hantro_end_prepare_run(struct hantro_ctx *ctx); void hantro_abort_prepare_run(struct hantro_ctx *ctx); diff --git a/drivers/media/platform/verisilicon/rockchip_vpu2_hw_mpeg2_dec.c b/drivers/media/platform/verisilicon/rockchip_vpu2_hw_mpeg2_dec.c index 57907dd5ea9c..1799f84b6519 100644 --- a/drivers/media/platform/verisilicon/rockchip_vpu2_hw_mpeg2_dec.c +++ b/drivers/media/platform/verisilicon/rockchip_vpu2_hw_mpeg2_dec.c @@ -155,11 +155,16 @@ int rockchip_vpu2_mpeg2_dec_run(struct hantro_ctx *ctx) const struct v4l2_ctrl_mpeg2_sequence *seq; const struct v4l2_ctrl_mpeg2_picture *pic; u32 reg; + int ret; src_buf = hantro_get_src_buf(ctx); dst_buf = hantro_get_dst_buf(ctx); - hantro_start_prepare_run(ctx); + ret = hantro_start_prepare_run(ctx); + if (ret) { + hantro_abort_prepare_run(ctx); + return ret; + } seq = hantro_get_ctrl(ctx, V4L2_CID_STATELESS_MPEG2_SEQUENCE); diff --git a/drivers/media/platform/verisilicon/rockchip_vpu2_hw_vp8_dec.c b/drivers/media/platform/verisilicon/rockchip_vpu2_hw_vp8_dec.c index 15f4872dd2bd..966a0f10cc6f 100644 --- a/drivers/media/platform/verisilicon/rockchip_vpu2_hw_vp8_dec.c +++ b/drivers/media/platform/verisilicon/rockchip_vpu2_hw_vp8_dec.c @@ -515,8 +515,13 @@ int rockchip_vpu2_vp8_dec_run(struct hantro_ctx *ctx) size_t width = ctx->dst_fmt.width; u32 mb_width, mb_height; u32 reg; + int ret; - hantro_start_prepare_run(ctx); + ret = hantro_start_prepare_run(ctx); + if (ret) { + hantro_abort_prepare_run(ctx); + return ret; + } hdr = hantro_get_ctrl(ctx, V4L2_CID_STATELESS_VP8_FRAME); if (WARN_ON(!hdr)) { diff --git a/drivers/media/platform/verisilicon/rockchip_vpu981_hw_av1_dec.c b/drivers/media/platform/verisilicon/rockchip_vpu981_hw_av1_dec.c index f143df99cdb5..8c5c2418de17 100644 --- a/drivers/media/platform/verisilicon/rockchip_vpu981_hw_av1_dec.c +++ b/drivers/media/platform/verisilicon/rockchip_vpu981_hw_av1_dec.c @@ -1379,7 +1379,9 @@ int rockchip_vpu981_av1_dec_run(struct hantro_ctx *ctx) struct vb2_v4l2_buffer *vb2_src; int ret; - hantro_start_prepare_run(ctx); + ret = hantro_start_prepare_run(ctx); + if (ret) + goto prepare_error; ret = hantro_av1_prepare_run(ctx); if (ret) -- 2.55.0