From: Shuah Khan <shuahkh@osg.samsung.com>
To: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
Cc: Linux Media Mailing List <linux-media@vger.kernel.org>,
Mauro Carvalho Chehab <mchehab@infradead.org>,
Shuah Khan <shuahkh@osg.samsung.com>
Subject: Re: [PATCH 4/5] [media] media-device: use kref for media_device instance
Date: Wed, 16 Mar 2016 08:32:48 -0600 [thread overview]
Message-ID: <56E96E90.8040609@osg.samsung.com> (raw)
In-Reply-To: <20160316112540.37086aba@recife.lan>
On 03/16/2016 08:25 AM, Mauro Carvalho Chehab wrote:
> Em Wed, 16 Mar 2016 08:05:15 -0600
> Shuah Khan <shuahkh@osg.samsung.com> escreveu:
>
>> On 03/16/2016 06:04 AM, Mauro Carvalho Chehab wrote:
>>> Now that the media_device can be used by multiple drivers,
>>> via devres, we need to be sure that it will be dropped only
>>> when all drivers stop using it.
>>>
>>> Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
>>> ---
>>> drivers/media/media-device.c | 48 +++++++++++++++++++++++++++++++-------------
>>> include/media/media-device.h | 3 +++
>>> 2 files changed, 37 insertions(+), 14 deletions(-)
>>>
>>> diff --git a/drivers/media/media-device.c b/drivers/media/media-device.c
>>> index c32fa15cc76e..38e6c319fe6e 100644
>>> --- a/drivers/media/media-device.c
>>> +++ b/drivers/media/media-device.c
>>> @@ -721,6 +721,15 @@ int __must_check __media_device_register(struct media_device *mdev,
>>> {
>>> int ret;
>>>
>>> + /* Check if mdev was ever registered at all */
>>> + mutex_lock(&mdev->graph_mutex);
>>> + if (media_devnode_is_registered(&mdev->devnode)) {
>>> + kref_get(&mdev->kref);
>>> + mutex_unlock(&mdev->graph_mutex);
>>> + return 0;
>>> + }
>>> + kref_init(&mdev->kref);
>>> +
>>> /* Register the device node. */
>>> mdev->devnode.fops = &media_device_fops;
>>> mdev->devnode.parent = mdev->dev;
>>> @@ -730,8 +739,10 @@ int __must_check __media_device_register(struct media_device *mdev,
>>> mdev->topology_version = 0;
>>>
>>> ret = media_devnode_register(&mdev->devnode, owner);
>>> - if (ret < 0)
>>> + if (ret < 0) {
>>> + media_devnode_unregister(&mdev->devnode);
>>> return ret;
>>> + }
>>>
>>> ret = device_create_file(&mdev->devnode.dev, &dev_attr_model);
>>> if (ret < 0) {
>>> @@ -739,6 +750,7 @@ int __must_check __media_device_register(struct media_device *mdev,
>>> return ret;
>>> }
>>>
>>> + mutex_unlock(&mdev->graph_mutex);
>>> dev_dbg(mdev->dev, "Media device registered\n");
>>>
>>> return 0;
>>> @@ -773,23 +785,15 @@ void media_device_unregister_entity_notify(struct media_device *mdev,
>>> }
>>> EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify);
>>>
>>> -void media_device_unregister(struct media_device *mdev)
>>> +static void struct kref *kref)
>>> {
>>> + struct media_device *mdev;
>>> struct media_entity *entity;
>>> struct media_entity *next;
>>> struct media_interface *intf, *tmp_intf;
>>> struct media_entity_notify *notify, *nextp;
>>>
>>> - if (mdev == NULL)
>>> - return;
>>> -
>>> - mutex_lock(&mdev->graph_mutex);
>>> -
>>> - /* Check if mdev was ever registered at all */
>>> - if (!media_devnode_is_registered(&mdev->devnode)) {
>>> - mutex_unlock(&mdev->graph_mutex);
>>> - return;
>>> - }
>>> + mdev = container_of(kref, struct media_device, kref);
>>>
>>> /* Remove all entities from the media device */
>>> list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list)
>>> @@ -807,13 +811,26 @@ void media_device_unregister(struct media_device *mdev)
>>> kfree(intf);
>>> }
>>>
>>> - mutex_unlock(&mdev->graph_mutex);
>>> + /* Check if mdev devnode was registered */
>>> + if (!media_devnode_is_registered(&mdev->devnode))
>>> + return;
>>>
>>> device_remove_file(&mdev->devnode.dev, &dev_attr_model);
>>> media_devnode_unregister(&mdev->devnode);
>>
>> Patch looks good.
>>
>> Reviewed-by: Shuah Khan <shuahkh@osg.samsung.com>
>>
>> Please see a few comments below that aren't related to this patch.
>>
>> The above is unprotected and could be done twice when two drivers
>> call media_device_unregister(). I think we still mark the media
>> device unregistered in media_devnode_unregister(). We have to
>> protect these two steps still.
>>
>> I attempted to do this with a unregister in progress flag which
>> gets set at the beginning in media_device_unregister(). That
>> does ensure media_device_unregister() runs only once. If that
>> approach isn't desirable, we have to find another way.
>
> Do you mean do_media_device_unregister()? This is protected, as
> this function is only called via media_device_unregister(),
> with the mutex hold. I opted to take the mutex there, as
> it makes the return code simpler.
>
The below two steps are my concern. With the mutex changes in
do_media_device_unregister() closed critical windows, however
the below code path still concerns me.
device_remove_file(&mdev->devnode.dev, &dev_attr_model);
media_devnode_unregister(&mdev->devnode);
Especially since we do the clear MEDIA_FLAG_REGISTERED in
media_devnode_unregister(). This step is done while holding
media_devnode_lock - a different mutex
We rely on media_devnode_is_registered() check to determine
whether to start unregister or not. Hence, there is a window
where, we could potentially try to do the following twice:
device_remove_file(&mdev->devnode.dev, &dev_attr_model);
media_devnode_unregister(&mdev->devnode);
You will see this only when both au0828 and snd-usb-audio
try to unregister()
thanks,
-- Shuah
--
Shuah Khan
Sr. Linux Kernel Developer
Open Source Innovation Group
Samsung Research America (Silicon Valley)
shuahkh@osg.samsung.com | (970) 217-8978
next prev parent reply other threads:[~2016-03-16 14:32 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-16 12:04 [PATCH 1/5] [media] media-device: get rid of the spinlock Mauro Carvalho Chehab
2016-03-16 12:04 ` [PATCH 2/5] [media] media-device: Fix a comment Mauro Carvalho Chehab
2016-03-16 12:58 ` Javier Martinez Canillas
2016-03-16 12:04 ` [PATCH 3/5] [media] au0828: Unregister notifiers Mauro Carvalho Chehab
2016-03-16 13:11 ` Javier Martinez Canillas
2016-03-22 19:55 ` Shuah Khan
2016-03-16 12:04 ` [PATCH 4/5] [media] media-device: use kref for media_device instance Mauro Carvalho Chehab
2016-03-16 13:23 ` Javier Martinez Canillas
2016-03-16 14:05 ` Shuah Khan
2016-03-16 14:25 ` Mauro Carvalho Chehab
2016-03-16 14:32 ` Shuah Khan [this message]
2016-03-17 11:50 ` Sakari Ailus
2016-03-18 11:17 ` Mauro Carvalho Chehab
2016-03-18 13:10 ` Sakari Ailus
2016-03-22 19:56 ` Shuah Khan
2016-03-22 20:07 ` Shuah Khan
2016-03-16 12:04 ` [PATCH 5/5] [media] media-device: make media_device_cleanup() static Mauro Carvalho Chehab
2016-03-16 14:03 ` Javier Martinez Canillas
2016-03-16 14:36 ` Mauro Carvalho Chehab
2016-03-16 14:38 ` Javier Martinez Canillas
2016-03-22 19:57 ` Shuah Khan
2016-03-16 12:53 ` [PATCH 1/5] [media] media-device: get rid of the spinlock Javier Martinez Canillas
2016-03-16 13:10 ` Mauro Carvalho Chehab
2016-03-16 14:10 ` Sakari Ailus
2016-03-22 19:52 ` Shuah Khan
2016-03-24 22:11 ` 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=56E96E90.8040609@osg.samsung.com \
--to=shuahkh@osg.samsung.com \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@infradead.org \
--cc=mchehab@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