linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shuah Khan <shuahkh@osg.samsung.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: m.chehab@samsung.com, akpm@linux-foundation.org,
	gregkh@linuxfoundation.org, crope@iki.fi, olebowle@gmx.com,
	dheitmueller@kernellabs.com, hverkuil@xs4all.nl,
	ramakrmu@cisco.com, sakari.ailus@linux.intel.com,
	laurent.pinchart@ideasonboard.com, perex@perex.cz,
	prabhakar.csengg@gmail.com, tim.gardner@canonical.com,
	linux@eikelenboom.it, linux-media@vger.kernel.org,
	alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/6] media: add media token device resource framework
Date: Thu, 16 Oct 2014 08:39:26 -0600	[thread overview]
Message-ID: <543FD89E.70106@osg.samsung.com> (raw)
In-Reply-To: <s5h4mv4w1wc.wl-tiwai@suse.de>

On 10/16/2014 08:00 AM, Takashi Iwai wrote:
> At Wed, 15 Oct 2014 18:53:28 -0600,
> Shuah Khan wrote:
>>
>> On 10/15/2014 11:05 AM, Takashi Iwai wrote:
>>
>>>> +#if defined(CONFIG_MEDIA_SUPPORT)
>>>> +extern int media_tknres_create(struct device *dev);
>>>> +extern int media_tknres_destroy(struct device *dev);
>>>> +
>>>> +extern int media_get_tuner_tkn(struct device *dev);
>>>> +extern int media_put_tuner_tkn(struct device *dev);
>>>> +
>>>> +extern int media_get_audio_tkn(struct device *dev);
>>>> +extern int media_put_audio_tkn(struct device *dev);
>>>
>>> The words "tknres" and "tkn" (especially latter) look ugly and not
>>> clear to me.  IMO, it should be "token".
>>
>> No problem. I can change that to media_token_create/destroy()
>> and expand token in other functions.
>>
>>>> +struct media_tkn {
>>>> +	spinlock_t lock;
>>>> +	unsigned int owner;	/* owner task pid */
>>>> +	unsigned int tgid;	/* owner task gid */
>>>> +	struct task_struct *task;
>>>> +};
>>>> +
>>>> +struct media_tknres {
>>>> +	struct media_tkn tuner;
>>>> +	struct media_tkn audio;
>>>> +};
>>>
>>> Why do you need to have both tuner and audio tokens?  If I understand
>>> correctly, no matter whether it's tuner or audio, if it's being used,
>>> the open must fail, right?
>>
>> As it evolved during development, it turns out at the moment I don't
>> have any use-cases that require holding audio and tuner separately.
>> It probably could be collapsed into just a media token. I have to
>> think about this some.
>>
>>>> +
>>>> +static int __media_get_tkn(struct media_tkn *tkn, char *tkn_str)
>>>> +{
>>>> +	int rc = 0;
>>>> +	unsigned tpid;
>>>> +	unsigned tgid;
>>>> +
>>>> +	spin_lock(&tkn->lock);
>>>
>>> You should use spin_lock_irqsave() here and in all other places.
>>> The trigger callback in usb-audio, for example, may be called in irq
>>> context.
>>
>> ok. Good point, will change that.
>>
>>>
>>>> +
>>>> +	tpid = task_pid_nr(current);
>>>> +	tgid = task_tgid_nr(current);
>>>> +
>>>> +	/* allow task in the same group id to release */
>>>
>>> IMO, it's not "release" but "steal"...  But what happens if the stolen
>>> owner calls put?  Then it'll be released although the stealing owner
>>> still thinks it's being held.
>>
>> Yeah it could be called a steal. :) Essentially tgid happens to be
>> the real owner. I am overwriting the pid with current pid when
>> tgid is same.
>>
>> media dvb and v4l apps start two or more threads that all share the
>> tgid and subsequent token gets should be allowed based on the tgid.
>>
>> Scenario 1:
>>
>> Please note that there are 3 device files in question and media
>> token resource is the lock for all. Hence the changes to v4l-core,
>> dvb-core, and snd-usb-audio to hold the token for exclusive access
>> when the task or tgid don't match.
>>
>> program starts:
>> - first thread opens device file in R/W mode - open gets the token
>>   or thread makes ioctls calls that clearly indicates intent to
>>   change tuner settings
>> - creates one thread for audio
>> - creates another for video or continues video function
>> - video thread does a close and re-opens the device file
>>
>>   In this case when thread tries to close, token put fails unless
>>   tasks with same tgid are allowed to release.
>>   ( I ran into this one of the media applications and it is a valid
>>     case to handle, thread can close the file and should be able to
>>     open again without running into token busy case )
>>
>> - or continue to just use the device file
>> - audio thread does snd_pcm_capture ops - trigger start
>>
>> program exits:
>> - video thread closes the device file
>> - audio thread does snd_pcm_capture ops - trigger stop
>>
>> This got me thinking about the scenario when an application
>> does a fork() as opposed to create a thread. I have to think
>> about this and see how I can address that.
>>
>>>
>>>> +	if (tkn->task && ((tkn->task != current) && (tkn->tgid != tgid))) {
>>>
>>> Shouldn't the second "&&" be "||" instead?
>>> And too many parentheses there.
>>
>> Right - this is my bad. The comment right above this conditional
>> is a give away that, at some point I did a copy and paste from
>> _put to _get and only changed the first task null check.
>> I am yelling at myself at the moment.
>>
>>>
>>>> +			rc = -EBUSY;
>>>
>>> Wrong indentation.
>>
>> Yes. Will fix that.
>>
>>>
>>> I have a feeling that the whole thing can be a bit more simplified in
>>> the end...
>>>
>>
>> Any ideas to simplify are welcome.
> 
> There are a few points to make things more consistent and simplified,
> IMO.  First of all, the whole concept can be more generalized.  It's
> not necessarily only for media and audio.  Rather we can make it a
> generic dev_token.  Then it'll become more convincing to land into
> lib/*.

Right. At the moment this is very media specific.

> 
> The media-specific handling is only about the pid and gid checks.
> This can be implemented as a callback that is passed at creating a
> token, for example.  This will reduce the core code in lib/*.
> 
> Also, get and put should handle a proper refcount.  The point I
> mentioned in my previous post is the possible unbalance, and you'll
> see unexpected unlock in the scenario.  In addition, with use of kref,
> the lock can be removed.

Yes. kref would eliminate the need for locks and potential for
unbalanced scenario.

> 
> So, essentially the lib code would be just a devres_alloc() for an
> object with kref, and dev_token_get() will take a kref with the
> exclusion check.
> 

I considered callback for media specific handling to make this token
construct generic. Talked myself out of it with the idea to get this
working for media use-case first and then extend it to be generic.

With this patch set covering media cases including non-media driver,
I think I am confident that the approach itself works to address the
issues and I don't mind pursuing a generic approach you are suggesting.
The current work isn't that far off and I can get the generic working
without many changes.

Thanks again for talking through the problem and ideas.

-- Shuah

-- 
Shuah Khan
Sr. Linux Kernel Developer
Samsung Research America (Silicon Valley)
shuahkh@osg.samsung.com | (970) 217-8978

  reply	other threads:[~2014-10-16 14:39 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-14 14:58 [PATCH v2 0/6] media token resource framework Shuah Khan
2014-10-14 14:58 ` [PATCH v2 1/6] media: add media token device " Shuah Khan
2014-10-15 17:05   ` Takashi Iwai
2014-10-16  0:53     ` Shuah Khan
2014-10-16 14:00       ` Takashi Iwai
2014-10-16 14:39         ` Shuah Khan [this message]
2014-10-21 10:46   ` Hans Verkuil
2014-10-21 11:51     ` Takashi Iwai
2014-10-21 11:58       ` Hans Verkuil
2014-10-21 13:07         ` Takashi Iwai
2014-11-18 21:33   ` Sakari Ailus
2014-10-14 14:58 ` [PATCH v2 2/6] media: v4l2-core changes to use media token api Shuah Khan
2014-10-14 14:58 ` [PATCH v2 3/6] media: au0828-video " Shuah Khan
2014-10-14 14:58 ` [PATCH v2 4/6] media: dvb-core " Shuah Khan
2014-10-14 14:58 ` [PATCH v2 5/6] sound/usb: pcm " Shuah Khan
2014-10-16 12:00   ` [alsa-devel] " Lars-Peter Clausen
2014-10-16 13:10     ` Shuah Khan
2014-10-16 14:01       ` Takashi Iwai
2014-10-16 14:10         ` Shuah Khan
2014-10-16 14:16           ` Takashi Iwai
2014-10-16 14:39             ` Shuah Khan
2014-10-16 14:48               ` Takashi Iwai
2014-10-16 14:59                 ` Shuah Khan
2014-10-18 18:49                   ` Mauro Carvalho Chehab
2014-10-19  9:00                     ` Takashi Iwai
2014-10-21 15:42                 ` Hans Verkuil
2014-10-21 16:05                   ` Takashi Iwai
2014-10-21 16:08                     ` Devin Heitmueller
2014-10-21 16:14                       ` Takashi Iwai
2014-10-22 19:26                       ` Pierre-Louis Bossart
2014-10-22 19:45                         ` Devin Heitmueller
2014-10-24 14:44                           ` Shuah Khan
2014-10-25 12:44                         ` Mauro Carvalho Chehab
2014-10-25 13:20                         ` Mauro Carvalho Chehab
2014-10-25 13:41                         ` Mauro Carvalho Chehab
2014-10-26  8:27                           ` Takashi Iwai
2014-10-27 12:52                             ` Mauro Carvalho Chehab
2014-10-28 21:15                               ` Shuah Khan
2014-10-28 23:42                                 ` [RFCv1] Media Token API needs - Was: " Mauro Carvalho Chehab
2014-10-29  0:00                                   ` Mauro Carvalho Chehab
2014-10-29 11:19                                     ` Mauro Carvalho Chehab
2014-10-29 16:06                                   ` Shuah Khan
2014-10-29 17:56                                     ` Mauro Carvalho Chehab
2014-11-04 23:08                                       ` [RFCv2] Media Token API Spec Shuah Khan
2014-11-10 14:15                                         ` Hans Verkuil
2014-11-18 21:15                                         ` Sakari Ailus
2014-11-18 21:27                                           ` Shuah Khan
2014-10-21 17:32                     ` [alsa-devel] [PATCH v2 5/6] sound/usb: pcm changes to use media token api Shuah Khan
2014-10-22 15:24                       ` Hans Verkuil
     [not found]                       ` <20141025085742.43e20bb5.m.chehab@samsung.com>
2014-10-27 15:47                         ` Shuah Khan
2014-10-14 14:58 ` [PATCH v2 6/6] media: au0828-core changes to create and destroy media Shuah Khan
2014-10-15 16:48 ` [PATCH v2 0/6] media token resource framework Takashi Iwai
2014-10-15 20:21   ` Shuah Khan
2014-10-16 14:02     ` Takashi Iwai
2014-10-29  9:17 ` Sakari Ailus
2014-10-29  9:33   ` Mauro Carvalho Chehab

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=543FD89E.70106@osg.samsung.com \
    --to=shuahkh@osg.samsung.com \
    --cc=akpm@linux-foundation.org \
    --cc=alsa-devel@alsa-project.org \
    --cc=crope@iki.fi \
    --cc=dheitmueller@kernellabs.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hverkuil@xs4all.nl \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux@eikelenboom.it \
    --cc=m.chehab@samsung.com \
    --cc=olebowle@gmx.com \
    --cc=perex@perex.cz \
    --cc=prabhakar.csengg@gmail.com \
    --cc=ramakrmu@cisco.com \
    --cc=sakari.ailus@linux.intel.com \
    --cc=tim.gardner@canonical.com \
    --cc=tiwai@suse.de \
    /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).