Linux ARM-MSM sub-architecture
 help / color / mirror / Atom feed
From: Bryan O'Donoghue <bod@kernel.org>
To: Guangshuo Li <lgs201920130244@gmail.com>
Cc: Vikash Garodia <vikash.garodia@oss.qualcomm.com>,
	Dikshita Agarwal <dikshita.agarwal@oss.qualcomm.com>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Stanimir Varbanov <stanimir.varbanov@linaro.org>,
	Hans Verkuil <hans.verkuil@cisco.com>,
	linux-media@vger.kernel.org, linux-arm-msm@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] media: venus: venc: avoid double free on video register failure
Date: Tue, 19 May 2026 14:20:01 +0100	[thread overview]
Message-ID: <d7082ea8-3b3d-468d-ba27-4d3ba5103a3a@kernel.org> (raw)
In-Reply-To: <CANUHTR9g6vRkKfPeHBQ4_9YR-sZQ_UZBX3+8CiKPYp-XPcp1CQ@mail.gmail.com>

On 19/05/2026 13:51, Guangshuo Li wrote:
> Hi Bryan,
> 
> Thank you for the review and for pointing me to the mxc-jpeg cleanup
> path.
> 
> On Tue, 19 May 2026 at 18:09, Bryan O'Donoghue <bod@kernel.org> wrote:
>>
>> OK so this will get the same feedback as the Iris version which is
>> please fix the cleanup path.
>>
>> If we look at drivers/media/platform/nxp/imx-jpeg/mxc-jpeg.c we can see
>>
>>           ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_VIDEO, -1);
>>           if (ret) {
>>                   dev_err(dev, "failed to register video device\n");
>>                   goto err_vdev_register;
>>           }
>> <snip>
>>
>> err_vdev_register:
>>           /* Only release if allocation succeeded but registration failed */
>>           if (jpeg->dec_vdev)
>>                   video_device_release(jpeg->dec_vdev);
>>
>> So for Venus and Iris
>>
>> err_vdev_release:
>>          if(vdev)
>>                  video_device_release(vdev);
>>
>> i.e. only release the video device on the error path if the vdev pointer
>> is non-NULL.
>>
>> ---
>> bod
> 
> I agree that the mxc-jpeg pattern is useful for avoiding a double
> release when a video_device has been registered successfully and a later
> probe step fails. In that case the cleanup path does:
> 
>          video_unregister_device(jpeg->dec_vdev);
>          jpeg->dec_vdev = NULL;
> 
> and then falls through to:
> 
>          if (jpeg->dec_vdev)
>                  video_device_release(jpeg->dec_vdev);
> 
> So the NULL assignment prevents the already unregistered video_device
> from being released again by the later shared error label. That makes
> sense for the "registration succeeded, later cleanup failed" case.
> 
> However, the issue I am trying to fix in Venus happens before
> video_register_device() returns, when video_register_device() itself
> fails after reaching device_register().
> 
> The problematic path is:
> 
>          venc_probe()
>            -> video_register_device(vdev, VFL_TYPE_VIDEO, -1)
>               -> __video_register_device()
>                  -> device_register(&vdev->dev) fails
>                     -> put_device(&vdev->dev)
>                        -> v4l2_device_release()
>                           -> vdev->release(vdev)
>                              -> video_device_release(vdev)
> 
> At this point, video_register_device() returns an error to venc_probe().
> Then the current Venus error path continues with:
> 
>          venc_probe()
>            -> err_vdev_release
>               -> video_device_release(vdev)
> 
> So the same video_device can be released twice.
> 
> In this path, adding only:
> 
>          if (vdev)
>                  video_device_release(vdev);
> 
> would not avoid the double free, because vdev is a local pointer in
> venc_probe(). It is still non-NULL even if the object it points to has
> already been released through put_device(&vdev->dev) inside
> __video_register_device().
> 
> So I think the mxc-jpeg cleanup pattern handles a different ownership
> transition: it avoids releasing a video_device after a successful
> registration has later been undone with video_unregister_device(). The
> Venus issue here is about the ownership state when video_register_device()
> itself fails after device_register() has already taken and dropped the
> device reference.
> 
> This is why the patch temporarily uses video_device_release_empty() while
> calling video_register_device(). With that, if device_register() fails
> and the V4L2 core reaches vdev->release(vdev), it will not free vdev.
> Then the Venus err_vdev_release path can still release vdev exactly once:
> 
>          venc_probe()
>            -> video_register_device()
>               -> __video_register_device()
>                  -> device_register() fails
>                     -> put_device(&vdev->dev)
>                        -> v4l2_device_release()
>                           -> vdev->release(vdev)
>                              -> video_device_release_empty(vdev)
> 
>          venc_probe()
>            -> err_vdev_release
>               -> video_device_release(vdev)
> 
> After video_register_device() succeeds, the patch restores:
> 
>          vdev->release = video_device_release;
> 
> so the successfully registered device keeps the normal lifetime handling.
> 
> Please let me know if you think this should be solved differently, for
> example in the V4L2 core instead of in the Venus driver. I would be happy
> to rework the patch if there is a preferred approach.
> 
> Thanks,
> Guangshuo

Yes I take your point.

So what you are describing is an error in the software contract from 
video_register_device() - if we look throughout the usage of that 
function we see either the pattern we already have - not checking for 
NULL or checking for NULL - not the double free case you are addressing.

So really the fix - the place to litigate this is not in Venus or Iris 
but in video_register_device's cleanup path.

---
bod

  reply	other threads:[~2026-05-19 13:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <xMdPPQAJ2BbtNwnxmf1CN7FGbdhSJM7NIXkRCxzFvXv0g01tuvNPvAacsFJaDyBc3cIkIAEfi44ewZ3OGGAcDg==@protonmail.internalid>
2026-05-19  9:08 ` [PATCH] media: venus: venc: avoid double free on video register failure Guangshuo Li
2026-05-19 10:09   ` Bryan O'Donoghue
2026-05-19 12:51     ` Guangshuo Li
2026-05-19 13:20       ` Bryan O'Donoghue [this message]
2026-05-19 14:58         ` Guangshuo Li
2026-05-19 16:34           ` Bryan O'Donoghue
2026-05-20  2:31             ` Guangshuo Li
2026-05-20  6:10               ` Hans Verkuil
2026-05-20  8:19                 ` Guangshuo Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d7082ea8-3b3d-468d-ba27-4d3ba5103a3a@kernel.org \
    --to=bod@kernel.org \
    --cc=dikshita.agarwal@oss.qualcomm.com \
    --cc=hans.verkuil@cisco.com \
    --cc=lgs201920130244@gmail.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=stanimir.varbanov@linaro.org \
    --cc=vikash.garodia@oss.qualcomm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox