From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795980762; b=pbpiXQZWdpFsbIrmlwGL7EMJU1Nibl4TCxzhOYwvcHA8RguncHjSbMZ0SbGw8baVFqTN M2v85f5mI5uN9R1oWxF/2xJXpHN0fAAS11f0apCtLpNehxN8ex+t226ngCwRzFfVQmr+N lUQeBuoHgeo6CZD/KWC6BbjCkTyD96mNJz5hTajTsCIZDqwXA9oeACkIt2DobxCoqxET/ uVzl04IwcxYs6tZIz/1A4nXAJ4/KQc9Jymq/Xh5V4/6xx0PERSlmV0UafKkKINnMjbwZu w9hUsBjsY4BAh/HNcjG3HCXrdozBaXkIGcRCor6m20YOwRF53gWAlUToVZn0ZuYBmOA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795980762; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=W3E3YhPtXyA+WVhhQic4gejo8FIb4OB77vOdM50Wk2o=; b=KsxLSvjwzMfbQbYJ8uJGyqFOd8Kn+picW4OPLASrQg6tO5OSGjvSwk98SED3hkTI9EQr oHw7dNh5g9SA5+yjna/CYD2z4L+qvwZYJTIM7vKpeLR0UbwjK/2rZanQIMXKBcdhgLAlL qcN9kupZHC1Y6EoD3eIGlFXB1Q36GLFRm8kd1MIibyDL6vmwSDLgwk0UxE/3twIbDwKOH 2myOR+Zv090cqY9CE2f7QRsqmZ5FjniWUXdCgMURAzkAozqAyB+2F7SmCxg7Kq1qLNAms IuQO4aFsPBVIAyN8LRVKYluM6uOyo/vIDRHmEesvu4ZQtTz1vUMwER5H4fVGfymztbg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="FUmba/JX"; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3523606A31C; Sun, 29 Nov 2026 19:32:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=FUmba/JX Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795980761; b=ceDuyFS8XdZt71bwWgIaRkjAkQwszoeD94wQ/CPb8abwk7tK9ASmYieIdmeoUgsMyXdN V+456QLSDDWcUr19jc28DT4cQkUvN/Z9SKTRCclL5oSTyD7QzCGU0DIG+X8BYZz/EXcl3 a16yZHS9GtCwU0vPPwdCeV8RnoMnYbjNWSvygOCgVRIrnjqCZxGQy69bW5fCdxFJBvds7 656ji+ak2JHt9cc8D2YCwAejy4x/GMKLfmiJFwfD5iumGOlT8EE4WxfCXli70s11Tkp/b djFE0dXEjvda4EDyMMU7XGg4LjqNh5haArnHGBDsTCpX3nzpY5mPIb/VouK184tQO5g== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795980761; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=W3E3YhPtXyA+WVhhQic4gejo8FIb4OB77vOdM50Wk2o=; b=Xxna3ixIlm/VjYuFo2Fcw+Wpmjy3gAtB6V26uIaX+c1KUjECn/C2o37Gl0+9XJC//EWe X8Qo+90axgTqw213yOdGh8TNMslDkispbHXcs+maW16J4pTP8YVtlJulRdNbcpKqVVRpn wJQB6acQR+6y9/YmsQtDhU2ygVU1K/YKkG+CFrO6wfh3KLXMnhMHN4hVPDq3yDidInoBw IrCwv3BxoQQmGbCKtkduByIlPaPmSfFq6Z+vgwaCUTOl5t8TJsDMGnDw5kKfza/UcvGLj T7zuJuGzeAymiIIKz+a0fO89MGXBjlGqxT6WuPhQGs6bDPWee6pLzA2yCNraOq2A9Cw== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id E69F71F00896; Sun, 29 Nov 2026 19:32:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795980761; bh=W3E3YhPtXyA+WVhhQic4gejo8FIb4OB77vOdM50Wk2o=; h=From:Date:Subject:To:Cc:Reply-To; b=FUmba/JXouNQahV0hYqoBqXbx9Y/Kbo8AwRofyUqiqVY6U/OnzEd1YgNwwdoFUafT 66SCl9VQFP9Xe90bs6aOltYqyQAvui0lZiAwwQsvdSfdwWzKrh5wY1B0O6XbbGoos/ bpk7mFgB46eeh7V25g2/x1pubZqN7n/j2Q5OuJsRqovFf7GABSxy9GGIO2qtLX3sCT sN61X/IM5uzYF2W5mxB8FTtON92sRIdeWZPq3i5kR1FMb+nA1a0AD+V3NkaIJ6wGk8 u9lyzSBQfaeF9sQlJhKxeedMFnhDQ2j/AS/+MpORyksR8XqAxGein1dFaCACkxOJsW KqlkUnEtXTeXw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 4895E4E577; Sun, 29 Nov 2026 19:32:37 +0000 (UTC) From: Arun Iyengar via B4 Relay Date: Sun, 29 Nov 2026 19:32:37 +0000 Subject: [PATCH RFC] media: verisilicon: hevc: Find the reference buffers by timestamp X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261129-hantro-hevc-ref-ts-v1-1-e0ef7325d3f3@gmail.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/yWMwQqCUBBFf0XuugHnCWZtgz6gbbTI55jTQuPNS wLx35tseQ6cs8AkqRiOxYIks5pOowPvCsThPj6EtHNGKEPNHA7kMqeJBpkjJekpG+25rppQlZE bhocv9/rZpldczifc/tLe7VNi/u2wrl9Hs/d1ewAAAA== X-Change-ID: 20261129-hantro-hevc-ref-ts-71638230c181 To: Nicolas Dufresne , Benjamin Gaignard , Philipp Zabel , Mauro Carvalho Chehab , Ezequiel Garcia , Hans Verkuil Cc: linux-media@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, Arun Iyengar X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795980757; l=6454; i=aiyengar1990@gmail.com; s=20261110; h=from:subject:message-id; bh=gbhT4T8ndZl23qHsXs6tR5Dz9P1+GvWOxUxgA52GChk=; b=7H0LeMrZxORYhk38W+8qnYIIAfpeKaaUlpqag67lCZ1Zi5ZOCfSfE1PWq9WiDpqEk0sKScSlK NaL2GmqFSUwDgIzJHxrbJV3TmCEbsm04VSsAL41KBotP3XxYTLcVGJn X-Developer-Key: i=aiyengar1990@gmail.com; a=ed25519; pk=zXpRgUTrhaFe6jLyPqbBy3ccfkj04w0g2690Wv3mg+0= X-Endpoint-Received: by B4 Relay for aiyengar1990@gmail.com/20261110 with auth_id=1480 X-Original-From: Arun Iyengar Reply-To: aiyengar1990@gmail.com Content-Transfer-Encoding: 7bit From: Arun Iyengar The G2 HEVC decoder keeps a table of CAPTURE buffer addresses keyed by POC. Entries that left the DPB stay in the table and still match a POC, so a DPB entry can get the address of a buffer that was requeued or removed with VIDIOC_REMOVE_BUFS, and the hardware then reads freed memory. Look the references up by their DPB timestamp instead, like the other codecs do, and drop the table. Fixes: cb5dd5a0fa51 ("media: hantro: Introduce G2/HEVC decoder") Signed-off-by: Arun Iyengar --- Found by code reading. The G2 HEVC decoder (i.MX8MQ, i.MX8MM, i.MX8MP) can reach this through the DPB controls: an entry whose picture was not decoded through the table matches a stale slot. RFC because it is compile-tested only (arm64 allmodconfig, W=1); I have no i.MX8M board. Developed with Claude (Anthropic), which wrote the code and the commit messages. I reviewed every change. --- .../platform/verisilicon/hantro_g2_hevc_dec.c | 8 +--- drivers/media/platform/verisilicon/hantro_hevc.c | 44 ---------------------- drivers/media/platform/verisilicon/hantro_hw.h | 11 ------ 3 files changed, 1 insertion(+), 62 deletions(-) diff --git a/drivers/media/platform/verisilicon/hantro_g2_hevc_dec.c b/drivers/media/platform/verisilicon/hantro_g2_hevc_dec.c index e7a7c7a42467..c988dd579b2f 100644 --- a/drivers/media/platform/verisilicon/hantro_g2_hevc_dec.c +++ b/drivers/media/platform/verisilicon/hantro_g2_hevc_dec.c @@ -448,14 +448,11 @@ static int set_ref(struct hantro_ctx *ctx) set_ref_pic_list(ctx); - /* We will only keep the reference pictures that are still used */ - hantro_hevc_ref_init(ctx); - /* Set up addresses of DPB buffers */ dpb_longterm_e = 0; for (i = 0; i < decode_params->num_active_dpb_entries && i < (V4L2_HEVC_DPB_ENTRIES_NUM_MAX - 1); i++) { - luma_addr = hantro_hevc_get_ref_buf(ctx, dpb[i].pic_order_cnt_val); + luma_addr = hantro_get_ref(ctx, dpb[i].timestamp); if (!luma_addr) return -ENOMEM; @@ -480,9 +477,6 @@ static int set_ref(struct hantro_ctx *ctx) if (!luma_addr) return -ENOMEM; - if (hantro_hevc_add_ref_buf(ctx, decode_params->pic_order_cnt_val, luma_addr)) - return -EINVAL; - chroma_addr = luma_addr + cr_offset; mv_addr = luma_addr + mv_offset; compress_luma_addr = luma_addr + compress_luma_offset; diff --git a/drivers/media/platform/verisilicon/hantro_hevc.c b/drivers/media/platform/verisilicon/hantro_hevc.c index 83cd12b0ddd6..7a38012cbb32 100644 --- a/drivers/media/platform/verisilicon/hantro_hevc.c +++ b/drivers/media/platform/verisilicon/hantro_hevc.c @@ -30,48 +30,6 @@ module_param_named(hevc_use_compression, hevc_use_compression, bool, 0644); MODULE_PARM_DESC(hevc_use_compression, "Use reference frame compression for HEVC"); -void hantro_hevc_ref_init(struct hantro_ctx *ctx) -{ - struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec; - - hevc_dec->ref_bufs_used = 0; -} - -dma_addr_t hantro_hevc_get_ref_buf(struct hantro_ctx *ctx, - s32 poc) -{ - struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec; - int i; - - /* Find the reference buffer in already known ones */ - for (i = 0; i < NUM_REF_PICTURES; i++) { - if (hevc_dec->ref_bufs_poc[i] == poc) { - hevc_dec->ref_bufs_used |= 1 << i; - return hevc_dec->ref_bufs[i].dma; - } - } - - return 0; -} - -int hantro_hevc_add_ref_buf(struct hantro_ctx *ctx, int poc, dma_addr_t addr) -{ - struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec; - int i; - - /* Add a new reference buffer */ - for (i = 0; i < NUM_REF_PICTURES; i++) { - if (!(hevc_dec->ref_bufs_used & 1 << i)) { - hevc_dec->ref_bufs_used |= 1 << i; - hevc_dec->ref_bufs_poc[i] = poc; - hevc_dec->ref_bufs[i].dma = addr; - return 0; - } - } - - return -EINVAL; -} - static int tile_buffer_reallocate(struct hantro_ctx *ctx) { struct hantro_dev *vpu = ctx->dev; @@ -278,8 +236,6 @@ int hantro_hevc_dec_init(struct hantro_ctx *ctx) hevc_dec->scaling_lists.size = SCALING_LIST_SIZE; - hantro_hevc_ref_init(ctx); - hevc_dec->use_compression = hevc_use_compression & hantro_needs_postproc(ctx, ctx->vpu_dst_fmt); diff --git a/drivers/media/platform/verisilicon/hantro_hw.h b/drivers/media/platform/verisilicon/hantro_hw.h index 13e573f1f19d..87f0eaf31869 100644 --- a/drivers/media/platform/verisilicon/hantro_hw.h +++ b/drivers/media/platform/verisilicon/hantro_hw.h @@ -36,8 +36,6 @@ #define FMT_4K_WIDTH 4096 #define FMT_4K_HEIGHT 2304 -#define NUM_REF_PICTURES (V4L2_HEVC_DPB_ENTRIES_NUM_MAX + 1) - #define AV1_MAX_FRAME_BUF_COUNT (V4L2_AV1_TOTAL_REFS_PER_FRAME + 1) #define MAX_POSTPROC_BUFFERS 64 @@ -145,10 +143,7 @@ struct hantro_hevc_dec_ctrls { * @tile_filter: Tile vertical filter buffer * @tile_sao: Tile SAO buffer * @tile_bsd: Tile BSD control buffer - * @ref_bufs: Internal reference buffers * @scaling_lists: Scaling lists buffer - * @ref_bufs_poc: Internal reference buffers picture order count - * @ref_bufs_used: Bitfield of used reference buffers * @ctrls: V4L2 controls attached to a run * @num_tile_cols_allocated: number of allocated tiles * @use_compression: use reference buffer compression @@ -158,10 +153,7 @@ struct hantro_hevc_dec_hw_ctx { struct hantro_aux_buf tile_filter; struct hantro_aux_buf tile_sao; struct hantro_aux_buf tile_bsd; - struct hantro_aux_buf ref_bufs[NUM_REF_PICTURES]; struct hantro_aux_buf scaling_lists; - s32 ref_bufs_poc[NUM_REF_PICTURES]; - u32 ref_bufs_used; struct hantro_hevc_dec_ctrls ctrls; unsigned int num_tile_cols_allocated; bool use_compression; @@ -454,9 +446,6 @@ int hantro_hevc_dec_init(struct hantro_ctx *ctx); void hantro_hevc_dec_exit(struct hantro_ctx *ctx); int hantro_g2_hevc_dec_run(struct hantro_ctx *ctx); int hantro_hevc_dec_prepare_run(struct hantro_ctx *ctx); -void hantro_hevc_ref_init(struct hantro_ctx *ctx); -dma_addr_t hantro_hevc_get_ref_buf(struct hantro_ctx *ctx, s32 poc); -int hantro_hevc_add_ref_buf(struct hantro_ctx *ctx, int poc, dma_addr_t addr); int rockchip_vpu981_av1_dec_init(struct hantro_ctx *ctx); void rockchip_vpu981_av1_dec_exit(struct hantro_ctx *ctx); --- base-commit: fb8b2c10a588f1384b98280b1c32307da9db4747 change-id: 20261129-hantro-hevc-ref-ts-71638230c181 Best regards, -- Arun Iyengar