linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mauro Carvalho Chehab <mchehab@s-opensource.com>
To: Hans Verkuil <hverkuil@xs4all.nl>
Cc: Shuah Khan <shuahkh@osg.samsung.com>,
	Laurent Pinchart <laurent.pinchart@ideasonboard.com>,
	Sakari Ailus <sakari.ailus@iki.fi>,
	Sakari Ailus <sakari.ailus@linux.intel.com>,
	linux-media@vger.kernel.org
Subject: Re: [RFC v3 00/21] Make use of kref in media device, grab references as needed
Date: Fri, 16 Dec 2016 10:00:56 -0200	[thread overview]
Message-ID: <20161216100056.5f3fcb55@vento.lan> (raw)
In-Reply-To: <c654bffd-792c-f860-33b4-3c399984dbd4@xs4all.nl>

Em 
 escreveu:

> On 16/12/16 11:57, Mauro Carvalho Chehab wrote:
> > Em Fri, 16 Dec 2016 11:11:25 +0100
> > Hans Verkuil <hverkuil@xs4all.nl> escreveu:
> >  
> >>> Would it make sense to enforce that dependency. Can we tie /dev/media usecount
> >>> to /dev/video etc. usecount? In other words:
> >>>
> >>> /dev/video is opened, then open /dev/media.  
> >>
> >> When a device node is registered it should increase the refcount on the media_device
> >> (as I proposed, that would be mdev->dev). When a device node is unregistered and the
> >> last user disappeared, then it can decrease the media_device refcount.
> >>
> >> So as long as anyone is using a device node, the media_device will stick around as
> >> well.
> >>
> >> No need to take refcounts on open/close.  
> >
> > That makes sense. You're meaning something like the enclosed (untested)
> > patch?
> >  
> >> One note: as I mentioned, the video_device does not set the cdev parent correctly,
> >> so that bug needs to be fixed first for this to work.  
> >
> > Actually, __video_register_device() seems to be setting the parent
> > properly:
> >
> > 	if (vdev->dev_parent == NULL)
> > 		vdev->dev_parent = vdev->v4l2_dev->dev;  
> 
> No, I mean this code (from cec-core.c):
> 
> 
>         /* Part 2: Initialize and register the character device */
>          cdev_init(&devnode->cdev, &cec_devnode_fops);
>          devnode->cdev.kobj.parent = &devnode->dev.kobj;
>          devnode->cdev.owner = owner;
> 
>          ret = cdev_add(&devnode->cdev, devnode->dev.devt, 1);
>          if (ret < 0) {
>                  pr_err("%s: cdev_add failed\n", __func__);
>                  goto clr_bit;
>          }
> 
>          ret = device_add(&devnode->dev);
>          if (ret)
>                  goto cdev_del;
> 
> which sets cdev.kobj.parent. And that would indeed be vdev->dev_parent.

Ah! So, you're basically proposing to have a separate struct for
V4L2 devnode as well, right?

Makes sense.

> 
> >
> > Thanks,
> > Mauro
> >
> > [PATCH] Be sure that the media_device won't be freed too early
> >
> > This code snippet is untested.
> >
> > Signed-off-by: Mauro Carvalho chehab <mchehab@s-opensource.com>
> >
> > diff --git a/drivers/media/media-device.c b/drivers/media/media-device.c
> > index 8756275e9fc4..5fdeab382069 100644
> > --- a/drivers/media/media-device.c
> > +++ b/drivers/media/media-device.c
> > @@ -706,7 +706,7 @@ int __must_check __media_device_register(struct media_device *mdev,
> >  	struct media_devnode *devnode;
> >  	int ret;
> >
> > -	devnode = kzalloc(sizeof(*devnode), GFP_KERNEL);
> > +	devnode = devm_kzalloc(mdev->dev, sizeof(*devnode), GFP_KERNEL);  
> 
> I'm not sure about this change. I *think* this would work, but *only* if all
> the refcounting is 100% correct, and we know it isn't at the moment. So I
> think this should be postponed until we are confident everything is correct.

Yes, such change will require first to be sure that drivers would be
doing the right thing.

> 
> >  	if (!devnode)
> >  		return -ENOMEM;
> >
> > diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c
> > index 8be561ab2615..14a3c56dbcac 100644
> > --- a/drivers/media/v4l2-core/v4l2-dev.c
> > +++ b/drivers/media/v4l2-core/v4l2-dev.c
> > @@ -196,6 +196,7 @@ static void v4l2_device_release(struct device *cd)
> >  #if defined(CONFIG_MEDIA_CONTROLLER)
> >  	if (v4l2_dev->mdev) {
> >  		/* Remove interfaces and interface links */
> > +		put_device(v4l2_dev->mdev->dev);
> >  		media_devnode_remove(vdev->intf_devnode);
> >  		if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN)
> >  			media_device_unregister_entity(&vdev->entity);  
> 
> I think this is the wrong order: put_device should go after media_device_unregister_entity().

OK.

> 
> > @@ -810,6 +811,7 @@ static int video_register_media_controller(struct video_device *vdev, int type)
> >  			return -ENOMEM;
> >  		}
> >  	}
> > +	get_device(vdev->v4l2_dev->dev);  
> 
> You mean v4l2_dev->mdev->dev?

Yes, that's right (vdev->v4l2_dev->mdev->dev).

> 
> >
> >  	/* FIXME: how to create the other interface links? */
> >
> > @@ -1015,6 +1017,11 @@ void video_unregister_device(struct video_device *vdev)
> >  	if (!vdev || !video_is_registered(vdev))
> >  		return;
> >
> > +#if defined(CONFIG_MEDIA_CONTROLLER)
> > +	if (vdev->v4l2_dev->dev)
> > +		put_device(vdev->v4l2_dev->dev);  
> 
> Ditto.
> 
> > +#endif
> > +
> >  	mutex_lock(&videodev_lock);
> >  	/* This must be in a critical section to prevent a race with v4l2_open.
> >  	 * Once this bit has been cleared video_get may never be called again.
> > diff --git a/drivers/media/v4l2-core/v4l2-device.c b/drivers/media/v4l2-core/v4l2-device.c
> > index 62bbed76dbbc..53f42090c762 100644
> > --- a/drivers/media/v4l2-core/v4l2-device.c
> > +++ b/drivers/media/v4l2-core/v4l2-device.c
> > @@ -188,6 +188,7 @@ int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
> >  		err = media_device_register_entity(v4l2_dev->mdev, entity);
> >  		if (err < 0)
> >  			goto error_module;
> > +		get_device(v4l2_dev->mdev->dev);
> >  	}
> >  #endif
> >
> > @@ -205,6 +206,8 @@ int v4l2_device_register_subdev(struct v4l2_device *v4l2_dev,
> >
> >  error_unregister:
> >  #if defined(CONFIG_MEDIA_CONTROLLER)
> > +	if (v4l2_dev->mdev)
> > +		put_device(v4l2_dev->mdev->dev);
> >  	media_device_unregister_entity(entity);
> >  #endif
> >  error_module:
> > @@ -310,6 +313,7 @@ void v4l2_device_unregister_subdev(struct v4l2_subdev *sd)
> >  		 * links are removed by the function below, in the right order
> >  		 */
> >  		media_device_unregister_entity(&sd->entity);
> > +		put_device(v4l2_dev->mdev->dev);
> >  	}
> >  #endif
> >  	video_unregister_device(sd->devnode);
> >
> >
> >
> >  
> 
> Regards,
> 
> 	Hans



Thanks,
Mauro

  reply	other threads:[~2016-12-16 12:09 UTC|newest]

Thread overview: 89+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-26 23:43 [RFC v3 00/21] Make use of kref in media device, grab references as needed Sakari Ailus
2016-08-26 23:43 ` [RFC v3 01/21] Revert "[media] media: fix media devnode ioctl/syscall and unregister race" Sakari Ailus
2016-08-26 23:43 ` [RFC v3 02/21] Revert "[media] media: fix use-after-free in cdev_put() when app exits after driver unbind" Sakari Ailus
2016-08-26 23:43 ` [RFC v3 03/21] Revert "[media] media-device: dynamically allocate struct media_devnode" Sakari Ailus
2016-08-26 23:43 ` [RFC v3 04/21] media: Remove useless curly braces and parentheses Sakari Ailus
2016-08-26 23:43 ` [RFC v3 05/21] media: devnode: Rename mdev argument as devnode Sakari Ailus
2016-08-26 23:43 ` [RFC v3 06/21] media device: Drop nop release callback Sakari Ailus
2016-08-26 23:43 ` [RFC v3 07/21] media-device: Make devnode.dev->kobj parent of devnode.cdev Sakari Ailus
2016-08-26 23:43 ` [RFC v3 08/21] media: Enable allocating the media device dynamically Sakari Ailus
2016-08-26 23:43 ` [RFC v3 09/21] media: Split initialising and adding media devnode Sakari Ailus
2016-08-26 23:43 ` [RFC v3 10/21] media: Shuffle functions around Sakari Ailus
2016-08-26 23:43 ` [RFC v3 11/21] media device: Refcount the media device Sakari Ailus
2016-08-26 23:43 ` [RFC v3 12/21] media device: Initialise media devnode in media_device_init() Sakari Ailus
2016-08-26 23:43 ` [RFC v3 13/21] media device: Deprecate media_device_{init,cleanup}() for drivers Sakari Ailus
2016-08-26 23:43 ` [RFC v3 14/21] media device: Get the media device driver's device Sakari Ailus
2016-08-26 23:43 ` [RFC v3 15/21] media: Provide a way to the driver to set a private pointer Sakari Ailus
2016-08-26 23:43 ` [RFC v3 16/21] media: Add release callback for media device Sakari Ailus
2016-08-26 23:43 ` [RFC v3 17/21] v4l: Acquire a reference to the media device for every video device Sakari Ailus
2016-08-26 23:43 ` [RFC v3 18/21] media-device: Postpone graph object removal until free Sakari Ailus
2016-08-26 23:43 ` [RFC v3 19/21] omap3isp: Allocate the media device dynamically Sakari Ailus
2016-08-26 23:43 ` [RFC v3 20/21] omap3isp: Release the isp device struct by media device callback Sakari Ailus
2016-08-26 23:43 ` [RFC v3 21/21] omap3isp: Don't rely on devm for memory resource management Sakari Ailus
2016-12-15 11:23   ` Laurent Pinchart
2016-12-15 11:39     ` Sakari Ailus
2016-12-15 11:42       ` Laurent Pinchart
2016-12-15 11:45         ` Sakari Ailus
2016-12-15 11:57           ` Laurent Pinchart
2016-12-15 19:17             ` Shuah Khan
2016-12-16 13:32     ` Sakari Ailus
2016-12-16 14:39       ` Shuah Khan
2016-11-07 20:16 ` [RFC v3 00/21] Make use of kref in media device, grab references as needed Shuah Khan
2016-11-08  8:19   ` Sakari Ailus
2016-11-09 16:49     ` Shuah Khan
2016-11-09 17:00       ` Shuah Khan
2016-11-09 17:46         ` Mauro Carvalho Chehab
2016-11-14 13:27           ` Sakari Ailus
2016-11-22 17:44             ` Mauro Carvalho Chehab
2016-11-22 18:13               ` Hans Verkuil
2016-11-22 18:41                 ` Shuah Khan
2016-11-22 22:56               ` Shuah Khan
2016-11-28 10:45               ` Sakari Ailus
2016-11-29 11:13                 ` Mauro Carvalho Chehab
2016-12-13 10:53                   ` Sakari Ailus
2016-12-13 12:24                     ` Mauro Carvalho Chehab
2016-12-13 22:23                       ` Shuah Khan
2016-12-15 10:39                         ` Laurent Pinchart
2016-12-15 14:56                           ` Shuah Khan
2016-12-16 16:58                             ` Laurent Pinchart
2016-12-15 11:30                       ` Sakari Ailus
2016-12-15 12:56                         ` Laurent Pinchart
2016-12-15 14:03                           ` Hans Verkuil
2016-12-15 14:32                             ` Mauro Carvalho Chehab
2016-12-15 14:45                               ` Hans Verkuil
2016-12-15 15:45                                 ` Mauro Carvalho Chehab
2016-12-15 16:07                                   ` Hans Verkuil
2016-12-16 16:47                                   ` Laurent Pinchart
2016-12-16 16:43                               ` Laurent Pinchart
2016-12-15 14:45                             ` Shuah Khan
2016-12-15 15:26                               ` Hans Verkuil
2016-12-15 16:06                                 ` Shuah Khan
2016-12-15 16:28                                   ` Hans Verkuil
2016-12-15 17:09                                     ` Shuah Khan
2016-12-15 17:25                                       ` Mauro Carvalho Chehab
2016-12-15 17:51                                         ` Shuah Khan
2016-12-16 10:11                                           ` Hans Verkuil
2016-12-16 10:57                                             ` Mauro Carvalho Chehab
2016-12-16 11:27                                               ` Hans Verkuil
2016-12-16 12:00                                                 ` Mauro Carvalho Chehab [this message]
2016-12-16 14:45                                                   ` Hans Verkuil
2016-12-19  9:28                                                     ` Media summit in Feb? - Was: " Mauro Carvalho Chehab
2016-12-21  1:31                                                       ` Mauro Carvalho Chehab
2016-12-21 14:27                                                         ` Shuah Khan
2016-12-22 17:47                                                         ` Laurent Pinchart
2016-12-22 20:43                                                           ` Mauro Carvalho Chehab
2016-12-16 10:03                                       ` Hans Verkuil
2016-12-16 10:12                                         ` Mauro Carvalho Chehab
2016-12-23 18:13                                   ` Laurent Pinchart
2016-12-15 17:08                                 ` Mauro Carvalho Chehab
2016-12-23 17:55                                   ` Laurent Pinchart
2016-12-23 17:48                                 ` Laurent Pinchart
2016-12-23 17:27                               ` Laurent Pinchart
2016-12-16 15:07                             ` Sakari Ailus
2016-12-16 16:34                               ` Laurent Pinchart
2016-12-19  9:46                               ` Mauro Carvalho Chehab
2017-01-02  7:53                                 ` Sakari Ailus
2017-01-24 10:49                                   ` Mauro Carvalho Chehab
2017-01-25 11:02                                     ` Sakari Ailus
2017-01-26  9:10                                       ` Mauro Carvalho Chehab
2017-05-30 23:41                                         ` Shuah Khan

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=20161216100056.5f3fcb55@vento.lan \
    --to=mchehab@s-opensource.com \
    --cc=hverkuil@xs4all.nl \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    --cc=sakari.ailus@iki.fi \
    --cc=sakari.ailus@linux.intel.com \
    --cc=shuahkh@osg.samsung.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;
as well as URLs for NNTP newsgroup(s).