public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
From: Stanislaw Gruszka <stanislaw.gruszka@linux.intel.com>
To: Ma Ke <make24@iscas.ac.cn>
Cc: mchehab@kernel.org, hverkuil@xs4all.nl,
	sebastian.fricke@collabora.com, ribalda@chromium.org,
	benjamin.gaignard@collabora.com, viro@zeniv.linux.org.uk,
	bartosz.golaszewski@linaro.org, hljunggr@cisco.com,
	sakari.ailus@linux.intel.com, linux-media@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v2] media: v4l2-dev: fix error handling in __video_register_device()
Date: Tue, 18 Mar 2025 14:18:45 +0100	[thread overview]
Message-ID: <Z9lytZ0bOk4Fjk83@linux.intel.com> (raw)
In-Reply-To: <20250318090945.1655458-1-make24@iscas.ac.cn>

On Tue, Mar 18, 2025 at 05:09:45PM +0800, Ma Ke wrote:
> Once device_register() failed, we should call put_device() to
> decrement reference count for cleanup. Or it could cause memory leak.
> And move callback function before put_device().
> 
> As comment of device_register() says, 'NOTE: _Never_ directly free
> @dev after calling this function, even if it returned an error! Always
> use put_device() to give up the reference initialized in this function
> instead.'
> 
> Found by code review.
> 
> Cc: stable@vger.kernel.org
> Fixes: baa057e29b58 ("media: v4l2-dev: use pr_foo() for printing messages")

This tag does not seems to be right, the commit just change printk calls.

Regards
Stanislaw


> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
> Changes in v2:
> - modified the patch as no callback function before put_device().
> ---
>  drivers/media/v4l2-core/v4l2-dev.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c
> index 5bcaeeba4d09..4a8fdf8115c0 100644
> --- a/drivers/media/v4l2-core/v4l2-dev.c
> +++ b/drivers/media/v4l2-core/v4l2-dev.c
> @@ -1054,17 +1054,16 @@ int __video_register_device(struct video_device *vdev,
>  	vdev->dev.class = &video_class;
>  	vdev->dev.devt = MKDEV(VIDEO_MAJOR, vdev->minor);
>  	vdev->dev.parent = vdev->dev_parent;
> +	vdev->dev.release = v4l2_device_release;
>  	dev_set_name(&vdev->dev, "%s%d", name_base, vdev->num);
>  	mutex_lock(&videodev_lock);
>  	ret = device_register(&vdev->dev);
>  	if (ret < 0) {
>  		mutex_unlock(&videodev_lock);
>  		pr_err("%s: device_register failed\n", __func__);
> -		goto cleanup;
> +		put_device(&vdev->dev);
> +		return ret;
>  	}
> -	/* Register the release callback that will be called when the last
> -	   reference to the device goes away. */
> -	vdev->dev.release = v4l2_device_release;
>  
>  	if (nr != -1 && nr != vdev->num && warn_if_nr_in_use)
>  		pr_warn("%s: requested %s%d, got %s\n", __func__,
> -- 
> 2.25.1
> 
> 

      parent reply	other threads:[~2025-03-18 13:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-18  9:09 [PATCH v2] media: v4l2-dev: fix error handling in __video_register_device() Ma Ke
2025-03-18 12:25 ` Sakari Ailus
2025-03-18 13:18 ` Stanislaw Gruszka [this message]

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=Z9lytZ0bOk4Fjk83@linux.intel.com \
    --to=stanislaw.gruszka@linux.intel.com \
    --cc=bartosz.golaszewski@linaro.org \
    --cc=benjamin.gaignard@collabora.com \
    --cc=hljunggr@cisco.com \
    --cc=hverkuil@xs4all.nl \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=make24@iscas.ac.cn \
    --cc=mchehab@kernel.org \
    --cc=ribalda@chromium.org \
    --cc=sakari.ailus@linux.intel.com \
    --cc=sebastian.fricke@collabora.com \
    --cc=stable@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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