From: Sakari Ailus <sakari.ailus@linux.intel.com>
To: Guangshuo Li <lgs201920130244@gmail.com>
Cc: Mauro Carvalho Chehab <mchehab@kernel.org>,
Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>,
Hans Verkuil <hverkuil+cisco@kernel.org>,
Kees Cook <kees@kernel.org>, Ma Ke <make24@iscas.ac.cn>,
linux-media@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] media: v4l2-dev: do not fire driver's release on __video_register_device() failure
Date: Wed, 20 May 2026 13:01:40 +0300 [thread overview]
Message-ID: <ag2GhHyuomEqUq88@kekkonen.localdomain> (raw)
In-Reply-To: <20260520090624.1071139-1-lgs201920130244@gmail.com>
Hi Guangshuo,
Thanks for the patch.
On Wed, May 20, 2026 at 05:06:24PM +0800, Guangshuo Li wrote:
> video_register_device() / __video_register_device() registers vdev->dev
> with device_register(). Before the call the video core sets
>
> vdev->dev.release = v4l2_device_release;
>
> v4l2_device_release() invokes vdev->release(vdev) as its last step, and
> the driver's vdev->release hook is commonly video_device_release(), which
> kfree()s the vdev that the driver allocated with video_device_alloc().
>
> When device_register() fails inside __video_register_device() the core
> does
>
> put_device(&vdev->dev);
> return ret;
>
> which drops the only reference and fires the v4l2_device_release()
> chain:
>
> __video_register_device()
> device_register() -> -E*
> put_device(&vdev->dev)
> -> v4l2_device_release()
> -> vdev->release(vdev)
> -> video_device_release(vdev) /* kfree(vdev), free #1 */
>
> video_register_device() returns the error to the driver. Drivers that
> follow the documented ownership contract release vdev on their own error
> path, e.g.
>
> driver_probe()
> if (video_register_device(vdev, ...))
> goto err_release_vdev;
> ...
> err_release_vdev:
> video_device_release(vdev); /* free #2 -- DOUBLE FREE */
>
> This is the contract documented in
> Documentation/driver-api/media/v4l2-dev.rst: the driver owns vdev and
> is responsible for releasing it if video_register_device() fails. As
> Hans Verkuil pointed out, the right place to fix this is the v4l2 core
> rather than every individual driver, because drivers are expected to
> follow the documented ownership contract.
>
> Neutralise vdev->release around put_device() in the device_register()
> failure path so the device core cleanup does not run the driver's
> release hook. The driver-supplied release is restored before returning
> so the caller can release vdev according to the documented contract.
> Successful registration is unchanged, so the normal teardown sequence
> continues to call the driver's release hook and free vdev exactly once on
> unregister.
May I ask how the issue was found?
>
> Fixes: 2a934fdb01db ("media: v4l2-dev: fix error handling in __video_register_device()")
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> ---
> drivers/media/v4l2-core/v4l2-dev.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c
> index 6ce623a1245a..73648549eb2a 100644
> --- a/drivers/media/v4l2-core/v4l2-dev.c
> +++ b/drivers/media/v4l2-core/v4l2-dev.c
> @@ -1075,9 +1075,14 @@ int __video_register_device(struct video_device *vdev,
> mutex_lock(&videodev_lock);
> ret = device_register(&vdev->dev);
> if (ret < 0) {
> + void (*release)(struct video_device *) = vdev->release;
> +
> mutex_unlock(&videodev_lock);
> pr_err("%s: device_register failed\n", __func__);
> +
> + vdev->release = video_device_release_empty;
> put_device(&vdev->dev);
> + vdev->release = release;
This would largely solve the problem but not quite. The release callback
may well do more than just release the the video device. This is the case
for e.g. the rp1-cfe driver.
I wonder what Hans thinks.
> return ret;
> }
>
--
Regards,
Sakari Ailus
next prev parent reply other threads:[~2026-05-20 10:01 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-20 9:06 Guangshuo Li
2026-05-20 9:34 ` Laurent Pinchart
2026-05-20 10:01 ` Hans Verkuil
2026-05-20 10:48 ` Laurent Pinchart
2026-05-20 11:26 ` Hans Verkuil
2026-05-20 12:02 ` Sakari Ailus
2026-05-20 12:41 ` Laurent Pinchart
2026-05-20 12:45 ` Hans Verkuil
2026-05-24 7:23 ` Guangshuo Li
2026-05-20 10:01 ` Sakari Ailus [this message]
2026-05-20 10:14 ` 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=ag2GhHyuomEqUq88@kekkonen.localdomain \
--to=sakari.ailus@linux.intel.com \
--cc=hverkuil+cisco@kernel.org \
--cc=kees@kernel.org \
--cc=laurent.pinchart+renesas@ideasonboard.com \
--cc=lgs201920130244@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=make24@iscas.ac.cn \
--cc=mchehab@kernel.org \
/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
all inboxes | Powered by JetHome®