Alsa-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Stas Sergeev <stsp@aknet.ru>
Cc: alsa-devel@alsa-project.org
Subject: Re: snd_pcsp locking mess
Date: Thu, 29 May 2008 08:03:26 +0200	[thread overview]
Message-ID: <s5h63sxd4up.wl%tiwai@suse.de> (raw)
In-Reply-To: <483DBBC0.1080001@aknet.ru>

At Thu, 29 May 2008 00:08:32 +0400,
Stas Sergeev wrote:
> 
> Hello.
> 
> Takashi Iwai wrote:
> > OTOH, the update of the whole PCM via snd_pcm_period_elapsed() is a
> > slow path.  Especially in the case of hrtimer, it brings the lock
> > mess, too.
> Unless the entire callback is done in
> a softirq, as it currently is, no?
> 
> > Issuing softirq at each hrtimer callback isn't optimal from
> > performance perspective.  For RT-kernels, it doesn't matter, but for
> > normal kernels, it does.
> Oh. But in that case I guess it would
> really be better if just snd_pcm_period_elapsed()
> would raise a softirq. That's something
> you proposed in the past IIRC.
> Is this acceptable to have that change
> not in the driver, or do you think it
> is snd-pcsp-specific, and no other driver
> will benefit?

It would be good in general.
However, better to be the next step after changing pcsp.

> Btw, could you please point me to where
> to read about the softirq expense? I
> thought they are very cheap, almost zero
> overhead.

One big disadvantage of the softirq is its (possible) timing
inaccuracy.  (Note that this is about the normal non-preempt kernel.)
The thing like pcsp driver does, flipping the bits at a high rate,
isn't for softirq per se.

> And yes, if the sotfirqs are expensive,
> then indeed I see why you propose all
> these tricks you do... But if they are
> not and we can keep the entire callback
> in a softirq as it currently is, then
> there might be a simpler solutions.
>
> >> the chip->substream_lock for that (why?),
> > Because the pointer callback and the hrtimer callback can race about
> I asked only why have you chosen
> chip->substream_lock - IIRC it was
> intended not for this.
> 
> >>> +	pcsp_stop_playing(chip);
> >> This will set chip->timer_active to 0,
> >> but who knows if the callback on another
> >> CPU have already passed the check or not?
> > If it passed, then we simply wait until the next hrtimer callback
> > finished.  That's what hrtimer_cancel() does.  So, after that, it is
> > guaranteed that neither hrtimer and tasklet remains.
> Yes, but the state doesn't seem to be
> guaranteed. Running the handler after
> the pcsp_stop_playing(chip) called, looks
> nasty.

This is there because pcsp_stop_playing() doesn't sync.
Both hrtimer_cancel() and tasklet_kill() are basically just for sync,
not for killing the object.

> The timer will be in a different
> mode. Nothing bad will likely to happen,
> but... esp with the nforce workaround,
> where the callback is supposed to be
> called even amount of times. Doesn't
> look very good to me, even if nothing
> bad can happen...

Well, it can be done in another away if you are so nervous about it.

Simply add chip->timer_running atomic_t and reset it when the callback
finished with HRTIMER_NORESTART.  In another side, wait until
chip->timer_running becomes zero.
But, it's just an uneeded addition of the code.

> > Because the substream lock isn't necessary!  It just makes things more
> > complicated -- if we take substream lock, AB/BA deadlocks occur again.
> Even if the callback is called within
> the softirq?

The runtime callbacks, pointer and trigger, are called already inside they
substream lock held by the PCM core.  Thus it doesn't matter whether
it's in a hard or a softirq.

> >> And
> >> if right now it is barely usefull, then
> >> why it can't be improved to simply not
> >> require a second lock that guards a
> >> substream itself?
> > Because it won't be simpler, as you already tried.
> I haven't tried that. :)
> I have tried the snd_pcm_stream_lock(),
> and yes, its nasty. But to me its nastiness
> is only because it needs another lock to
> protect the substream itself.

No, you don't need to protect the substream at all.
Remember that hrtimer callback requires only the DMA-buffer pointer
during its operation.  No other thing is needed from the substream.
This can be guaranteed with a simpler logic.

> So it is
> barely usefull. But it can be improved
> (or an alternative provided), the way the
> second lock is not needed. Then I will
> need only that - a single lock, nothing
> more. This might not be even difficult to
> do at all.

Well, my version is almost i8259_lock only.
As mentioned, chip->substream_lock can be removed safely with a
barrier.


Takashi

  reply	other threads:[~2008-05-29  6:03 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20080518172258.D0DFB108060@picon.linux-foundation.org>
2008-05-18 18:20 ` snd_pcsp locking mess Stas Sergeev
2008-05-19  5:50   ` Takashi Iwai
2008-05-19 17:01     ` Stas Sergeev
2008-05-21 12:33       ` Takashi Iwai
2008-05-22 20:28         ` Stas Sergeev
2008-05-23 10:51           ` Takashi Iwai
2008-05-27 13:46             ` Stas Sergeev
2008-05-27 13:47             ` Stas Sergeev
2008-05-27 15:50               ` Takashi Iwai
2008-05-27 17:40                 ` Stas Sergeev
2008-05-28 10:13                   ` Takashi Iwai
2008-05-28 20:08                     ` Stas Sergeev
2008-05-29  6:03                       ` Takashi Iwai [this message]
2008-05-29 17:07                         ` Stas Sergeev
2008-06-02  9:36                           ` Takashi Iwai
2008-08-21  8:06                 ` Stas Sergeev
2008-08-21  9:06                   ` Takashi Iwai
2008-08-21 10:25                     ` Stas Sergeev
2008-10-20 13:05                       ` Takashi Iwai
2008-10-20 21:51                         ` Stas Sergeev
2008-10-21  6:27                           ` Takashi Iwai
2008-10-21  7:08                             ` Stas Sergeev
2008-10-21  7:16                               ` Takashi Iwai

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=s5h63sxd4up.wl%tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=stsp@aknet.ru \
    /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