public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
From: Sylwester Nawrocki <s.nawrocki@samsung.com>
To: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>,
	Hans Verkuil <hverkuil@xs4all.nl>
Cc: Mauro Carvalho Chehab <m.chehab@samsung.com>,
	Kyungmin Park <kyungmin.park@samsung.com>,
	Kukjin Kim <kgene.kim@samsung.com>,
	Pawel Osciak <pawel@osciak.com>,
	Marek Szyprowski <m.szyprowski@samsung.com>,
	"open list:SAMSUNG S5P/EXYNO..." <linux-media@vger.kernel.org>,
	"moderated list:ARM/S5P EXYNOS AR..."
	<linux-arm-kernel@lists.infradead.org>,
	"moderated list:ARM/S5P EXYNOS AR..."
	<linux-samsung-soc@vger.kernel.org>
Subject: Re: [PATCH v4] videobuf2: Add missing lock held on vb2_fop_relase
Date: Mon, 04 Nov 2013 15:12:26 +0100	[thread overview]
Message-ID: <5277AB4A.5050007@samsung.com> (raw)
In-Reply-To: <CAPybu_0cKxMxhXoSqbK2nTyX3DGCVZdUZPt2bTE6aZR65xDG=w@mail.gmail.com>

Hi,

On 04/11/13 14:54, Ricardo Ribalda Delgado wrote:
> Hello Hans
> 
> Thanks for your comments.
> 
> Please take a look to v4 of this patch
> https://patchwork.linuxtv.org/patch/20529/

We're discussing v4 actually ;)

> On Mon, Nov 4, 2013 at 1:37 PM, Hans Verkuil <hverkuil@xs4all.nl> wrote:
>> On 11/02/2013 10:53 AM, Ricardo Ribalda Delgado wrote:
>>> From: Ricardo Ribalda <ricardo.ribalda@gmail.com>
>>>
>>> vb2_fop_relase does not held the lock although it is modifying the
>>> queue->owner field.
>>>
>>> This could lead to race conditions on the vb2_perform_io function
>>> when multiple applications are accessing the video device via
>>> read/write API:
>>
>> It's also called directly by drivers/media/usb/em28xx/em28xx-video.c!
>>
> 
> em28xx-video does not hold the lock, therefore it can call the normal
> function. On v2 we made a internal function that should be called if
> the funciton is called directly by the driver. Please take a look to
> the old comments. https://patchwork.linuxtv.org/patch/20460/
[...]
>>> -int vb2_fop_release(struct file *file)
>>> +static int _vb2_fop_release(struct file *file, bool lock_is_held)
>>>  {
>>>       struct video_device *vdev = video_devdata(file);
>>> +     struct mutex *lock;
>>>
>>>       if (file->private_data == vdev->queue->owner) {
>>> +             if (lock_is_held)
>>> +                     lock = NULL;
>>> +             else
>>> +                     lock = vdev->queue->lock ?
>>> +                             vdev->queue->lock : vdev->lock;
>>> +             if (lock)
>>> +                     mutex_lock(lock);
>>>               vb2_queue_release(vdev->queue);
>>>               vdev->queue->owner = NULL;
>>> +             if (lock)
>>> +                     mutex_unlock(lock);
>>>       }
>>>       return v4l2_fh_release(file);
>>>  }
>>> +
>>> +int vb2_fop_release(struct file *file)
>>> +{
>>> +     return _vb2_fop_release(file, false);
>>> +}
>>>  EXPORT_SYMBOL_GPL(vb2_fop_release);
>>>
>>> +int __vb2_fop_release(struct file *file)
>>> +{
>>> +     return _vb2_fop_release(file, true);
>>> +}
>>> +EXPORT_SYMBOL_GPL(__vb2_fop_release);
>>
>> Sorry for introducing yet another opinion, but I think this is very confusing.
> 
> It is confusing the lock_held parameter or the __ naming for unlocked versions?
> 
>>
>> I would do this:
>>
>> static int __vb2_fop_release(struct file *file, struct mutex *lock)
>> {
>>         struct video_device *vdev = video_devdata(file);
>>
>>         if (file->private_data == vdev->queue->owner) {
>>                 if (lock)
>>                         mutex_lock(lock);
>>                 vb2_queue_release(vdev->queue);
>>                 vdev->queue->owner = NULL;
>>                 if (lock)
>>                         mutex_unlock(lock);
>>         }
>>         return v4l2_fh_release(file);
>> }
>>
>> int vb2_fop_release(struct file *file)
>> {
>>         struct video_device *vdev = video_devdata(file);
>>         struct mutex *lock = vdev->queue->lock ?
>>                                 vdev->queue->lock : vdev->lock;
>>
>>         return __vb2_fop_release(file, lock);
>> }
>> EXPORT_SYMBOL_GPL(vb2_fop_release);
>>
>> int vb2_fop_release_unlock(struct file *file)
>> {
>>         return __vb2_fop_release(file, NULL);
>> }
>> EXPORT_SYMBOL_GPL(vb2_fop_release_unlock);
>>
>> Optionally, __vb2_fop_release can be exported and then vb2_fop_release_unlock
>> isn't necessary.
>>
> 
> i dont have any strong opinion in any direction. All I really care is
> that the oops is fixed :).
> 
> If your concern about the patch is the is_lock_held function, I can
> make a patch with the params on your proposal and the __naming as
> Sylvester suggested, so everyone is happy.
> 
> Sylvester, Hanshat do you think?

I like Hans' proposal, probably it's better to export __vb2_fop_release(),
so it can be also used in cases like em28xx, to make things a bit more
clear. But I'm fine with vb2_fop_release_unlock() as well, don't really
have strong preference. It's up to you.

--
Thanks,
Sylwester

  reply	other threads:[~2013-11-04 14:12 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-02  9:53 [PATCH v4] videobuf2: Add missing lock held on vb2_fop_relase Ricardo Ribalda Delgado
2013-11-04 12:37 ` Hans Verkuil
2013-11-04 13:54   ` Ricardo Ribalda Delgado
2013-11-04 14:12     ` Sylwester Nawrocki [this message]
2013-11-04 14:12     ` Hans Verkuil
2013-11-04 14:24       ` Sylwester Nawrocki
2013-11-04 15:19         ` Hans Verkuil
2013-11-06  8:26           ` Ricardo Ribalda Delgado
2013-11-06  8:40             ` Ricardo Ribalda Delgado

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=5277AB4A.5050007@samsung.com \
    --to=s.nawrocki@samsung.com \
    --cc=hverkuil@xs4all.nl \
    --cc=kgene.kim@samsung.com \
    --cc=kyungmin.park@samsung.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-samsung-soc@vger.kernel.org \
    --cc=m.chehab@samsung.com \
    --cc=m.szyprowski@samsung.com \
    --cc=pawel@osciak.com \
    --cc=ricardo.ribalda@gmail.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