linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Hans Verkuil <hverkuil@xs4all.nl>
Cc: Sakari Ailus <sakari.ailus@linux.intel.com>,
	linux-media@vger.kernel.org, mchehab@osg.samsung.com,
	shuahkh@osg.samsung.com
Subject: Re: [RFC v4 14/21] media device: Get the media device driver's device
Date: Wed, 23 Nov 2016 00:16:08 +0200	[thread overview]
Message-ID: <3403508.4A2ibvd3Sz@avalon> (raw)
In-Reply-To: <29c9f484-8ae0-8ccf-7b9c-46bbe6f8955b@xs4all.nl>

On Tuesday 22 Nov 2016 11:58:43 Hans Verkuil wrote:
> On 22/11/16 10:58, Laurent Pinchart wrote:
> > Hi Hans,
> > 
> > On Tuesday 22 Nov 2016 10:46:31 Hans Verkuil wrote:
> >> On 08/11/16 14:55, Sakari Ailus wrote:
> >>> The struct device of the media device driver (i.e. not that of the media
> >>> devnode) is pointed to by the media device. The struct device pointer is
> >>> mostly used for debug prints.
> >>> 
> >>> Ensure it will stay around as long as the media device does.
> >>> 
> >>> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> >>> ---
> >>> 
> >>>  drivers/media/media-device.c | 9 ++++++++-
> >>>  1 file changed, 8 insertions(+), 1 deletion(-)
> >>> 
> >>> diff --git a/drivers/media/media-device.c b/drivers/media/media-device.c
> >>> index 2e52e44..648c64c 100644
> >>> --- a/drivers/media/media-device.c
> >>> +++ b/drivers/media/media-device.c
> >>> @@ -724,6 +724,7 @@ static void media_device_release(struct
> >>> media_devnode
> >>> *devnode)
> >>> 
> >>>  	mdev->entity_internal_idx_max = 0;
> >>>  	media_entity_graph_walk_cleanup(&mdev->pm_count_walk);
> >>>  	mutex_destroy(&mdev->graph_mutex);
> >>> 
> >>> +	put_device(mdev->dev);
> >>> 
> >>>  	kfree(mdev);
> >>>  
> >>>  }
> >>> 
> >>> @@ -732,9 +733,15 @@ struct media_device *media_device_alloc(struct
> >>> device
> >>> *dev)
> >>> 
> >>>  {
> >>>  
> >>>  	struct media_device *mdev;
> >>> 
> >>> +	dev = get_device(dev);
> >>> +	if (!dev)
> >>> +		return NULL;
> >> 
> >> I don't think this is right. When you allocate the media_device struct
> >> it should just be initialized, but not have any side effects until it is
> >> actually registered.
> >> 
> >> When the device is registered the device_add call will increase the
> >> parent's refcount as it should, thus ensuring it stays around for as
> >> long as is needed.
> > 
> > We're storing a pointer to dev in mdev a few lines below. As dev is
> > refcounted, we need to ensure that we take a reference appropriately. We
> > can either borrow a reference taken elsewhere or take our own reference.
> > 
> > Borrowing a reference is only valid if we know it will exist for at least
> > as long as we need to borrow it. That might be the case when creating the
> > media device as the driver performing the operation should hold a
> > reference to the struct device instance (especially given that allocation
> > and registration are usually - but not always - performed at probe time
> > for that driver), but it's harder to guarantee at unregistration time,
> > especially when userspace can keep device nodes open across
> > unregistration. This patch ensures that the pointer always remains valid
> > until we stop needing it.
> 
> I disagree. There is no reason to keep the parent device in memory once the
> media devnode is unregistered.

There's a very big one: the media device is accessed through a large number of 
APIs, not only through its own devnode. It can for instance be accessed 
through V4L2 devnodes, and thus has to live as long as *anything* can access 
it.

struct media_devnode was a very very bad idea. The original goal was to share 
the implementation with the V4L2 devnodes, but when that got rejected I really 
should have merged struct media_device and struct media_devnode into a single 
structure. We can keep media_devnode separate if that is believed to improve 
readability of the code, but there is absolutely no reason for allocating 
media_devnode separately from media_device.

> It seems to be pretty much only used for some debugging. I suspect that in
> almost all cases the debugging happens when the devnode is registered, and
> not when it is unregistered. But in that case you can also use &devnode.dev
> as the device pointer for dev_dbg, or use pr_debug.
> 
> Looking at what the CEC framework does I see that I pass a device pointer
> to the allocate function, but I really don't need to do that. It is not
> used anywhere until the register function, so the parent device pointer
> should be passed as an argument to the register function, not to the
> allocate function.
> 
> BTW, I would very much prefer it if mdev->dev is renamed to mdev->parent.
> Or better yet, just dropped completely since it is also available as
> mdev->devnode.parent. And even devnode.parent can be dropped and just
> use mdev->devnode.dev.parent.
> 
> I plan on posting such a patch for the cec framework as well, since
> it avoids having duplicates of the same device parent pointer in the
> data structures.

-- 
Regards,

Laurent Pinchart


  reply	other threads:[~2016-11-22 22:15 UTC|newest]

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

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=3403508.4A2ibvd3Sz@avalon \
    --to=laurent.pinchart@ideasonboard.com \
    --cc=hverkuil@xs4all.nl \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@osg.samsung.com \
    --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).