[PATCH v2 2/7] media: verisilicon: AV1: Use alloc/free helpers for auxiliary buffers

Nicolas Dufresne nicolas.dufresne at collabora.com
Fri Nov 20 12:10:32 PST 2026


Le vendredi 20 novembre 2026 à 16:56 +0100, Philipp Zabel a écrit :
> On Fr, 2026-11-20 at 16:13 +0100, Benjamin Gaignard wrote:
> > Simplify and clean up the code by using the helpers.
> > 
> > Signed-off-by: Benjamin Gaignard <benjamin.gaignard at collabora.com>
> > Reviewed-by: Nicolas Dufresne <nicolas.dufresne at collabora.com>
> > ---
> >  .../media/platform/verisilicon/hantro_av1.c   | 177 ++++--------------
> >  1 file changed, 39 insertions(+), 138 deletions(-)
> > 
> > diff --git a/drivers/media/platform/verisilicon/hantro_av1.c b/drivers/media/platform/verisilicon/hantro_av1.c
> > index 2cde32f0e935..e05816c7dfa3 100644
> > --- a/drivers/media/platform/verisilicon/hantro_av1.c
> > +++ b/drivers/media/platform/verisilicon/hantro_av1.c
> > @@ -235,40 +235,17 @@ size_t hantro_av1_chroma_size(struct hantro_ctx *ctx)
> >  
> >  static void hantro_av1_tiles_free(struct hantro_ctx *ctx)
> >  {
> > -	struct hantro_dev *vpu = ctx->dev;
> >  	struct hantro_av1_dec_hw_ctx *av1_dec = &ctx->av1_dec;
> >  
> > -	if (av1_dec->db_data_col.cpu)
> > -		dma_free_coherent(vpu->dev, av1_dec->db_data_col.size,
> > -				  av1_dec->db_data_col.cpu,
> > -				  av1_dec->db_data_col.dma);
> > -	av1_dec->db_data_col.cpu = NULL;
> > -
> > -	if (av1_dec->db_ctrl_col.cpu)
> > -		dma_free_coherent(vpu->dev, av1_dec->db_ctrl_col.size,
> > -				  av1_dec->db_ctrl_col.cpu,
> > -				  av1_dec->db_ctrl_col.dma);
> > -	av1_dec->db_ctrl_col.cpu = NULL;
> > -
> > -	if (av1_dec->cdef_col.cpu)
> > -		dma_free_coherent(vpu->dev, av1_dec->cdef_col.size,
> > -				  av1_dec->cdef_col.cpu, av1_dec->cdef_col.dma);
> > -	av1_dec->cdef_col.cpu = NULL;
> > -
> > -	if (av1_dec->sr_col.cpu)
> > -		dma_free_coherent(vpu->dev, av1_dec->sr_col.size,
> > -				  av1_dec->sr_col.cpu, av1_dec->sr_col.dma);
> > -	av1_dec->sr_col.cpu = NULL;
> > -
> > -	if (av1_dec->lr_col.cpu)
> > -		dma_free_coherent(vpu->dev, av1_dec->lr_col.size,
> > -				  av1_dec->lr_col.cpu, av1_dec->lr_col.dma);
> > -	av1_dec->lr_col.cpu = NULL;
> > +	hantro_free_aux_buf(ctx, &av1_dec->db_data_col);
> > +	hantro_free_aux_buf(ctx, &av1_dec->db_ctrl_col);
> > +	hantro_free_aux_buf(ctx, &av1_dec->cdef_col);
> > +	hantro_free_aux_buf(ctx, &av1_dec->sr_col);
> > +	hantro_free_aux_buf(ctx, &av1_dec->lr_col);
> >  }
> >  
> >  static int hantro_av1_tiles_reallocate(struct hantro_ctx *ctx)
> >  {
> > -	struct hantro_dev *vpu = ctx->dev;
> >  	struct hantro_av1_dec_hw_ctx *av1_dec = &ctx->av1_dec;
> >  	struct hantro_av1_dec_ctrls *ctrls = &av1_dec->ctrls;
> >  	const struct v4l2_av1_tile_info *tile_info = &ctrls->frame->tile_info;
> > @@ -277,6 +254,7 @@ static int hantro_av1_tiles_reallocate(struct hantro_ctx *ctx)
> >  	unsigned int height_in_sb = height / 64;
> >  	unsigned int stripe_num = ((height + 8) + 63) / 64;
> >  	size_t size;
> > +	int ret;
> >  
> >  	if (av1_dec->db_data_col.size >=
> >  	    ALIGN(height * 12 * ctx->bit_depth / 8, 128) * num_tile_cols)
> > @@ -285,44 +263,22 @@ static int hantro_av1_tiles_reallocate(struct hantro_ctx *ctx)
> >  	hantro_av1_tiles_free(ctx);
> >  
> >  	size = ALIGN(height * 12 * ctx->bit_depth / 8, 128) * num_tile_cols;
> > -	av1_dec->db_data_col.cpu = dma_alloc_coherent(vpu->dev, size,
> > -						      &av1_dec->db_data_col.dma,
> > -						      GFP_KERNEL);
> > -	if (!av1_dec->db_data_col.cpu)
> > -		goto buffer_allocation_error;
> > -	av1_dec->db_data_col.size = size;
> > +	ret = hantro_allocate_aux_buf(ctx, &av1_dec->db_data_col, size);
> >  
> >  	size = ALIGN(height * 2 * 16 / 4, 128) * num_tile_cols;
> > -	av1_dec->db_ctrl_col.cpu = dma_alloc_coherent(vpu->dev, size,
> > -						      &av1_dec->db_ctrl_col.dma,
> > -						      GFP_KERNEL);
> > -	if (!av1_dec->db_ctrl_col.cpu)
> > -		goto buffer_allocation_error;
> > -	av1_dec->db_ctrl_col.size = size;
> > +	ret |= hantro_allocate_aux_buf(ctx, &av1_dec->db_ctrl_col, size);
> 
> Please don't |= error return values. Even if it happens to work here
> because hantro_allocate_aux_buf() is guaranteed to return the same
> error, it may inspire others to copy this in places where it's invalid.
> 
> Further, hantro_allocate_aux_buf() looks like it can return -EINVAL and
> -ENOMEM, not sure if that can happen inside reallocate. That would
> combine into -ENOENT, which makes no sense to return.

Fair enough, I reviewed too quickly and assumed it was boolean. Not very common,
but something I've seen in the past:

  	ret = func(params1, ret);
	ret = func(params2, ret);
	...

And then:

int func(params, int ret)
{
	if (!ret)
		return ret;
	
	...

	return ret;
}

That would allow moving the repetitive branch inside hantro_allocate_aux_buf()
and keep the proposed simplification.

Nicolas

> 
> regards
> Philipp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: This is a digitally signed message part
URL: <http://lists.infradead.org/pipermail/linux-rockchip/attachments/20261120/f1522f9d/attachment.sig>


More information about the Linux-rockchip mailing list