All of lore.kernel.org
 help / color / mirror / Atom feed
From: Shreyas NC <shreyas.nc@intel.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: alsa-devel@alsa-project.org, patches.audio@intel.com,
	gregkh@linuxfoundation.org, pierre-louis.bossart@linux.intel.com,
	vkoul@kernel.org, sanyog.r.kale@intel.com
Subject: Re: [PATCH v4 3/7] soundwire: Add support to lock across	bus instances
Date: Tue, 26 Jun 2018 14:53:59 +0530	[thread overview]
Message-ID: <20180626092359.GD15119@snc-desk> (raw)
In-Reply-To: <s5hy3f2aquu.wl-tiwai@suse.de>

On Tue, Jun 26, 2018 at 10:34:17AM +0200, Takashi Iwai wrote:
> On Tue, 26 Jun 2018 10:22:01 +0200,
> Shreyas NC wrote:
> > 
> > > > +/**
> > > > + * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s)
> > > > + *
> > > > + * @stream: SoundWire stream
> > > > + *
> > > > + * Acquire bus_lock for each of the master runtime(m_rt) part of this
> > > > + * stream to reconfigure the bus.
> > > > + */
> > > > +static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
> > > > +{
> > > > +	struct sdw_master_runtime *m_rt = NULL;
> > > > +	struct sdw_bus *bus = NULL;
> > > > +
> > > > +	/* Iterate for all Master(s) in Master list */
> > > > +	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
> > > > +		bus = m_rt->bus;
> > > > +
> > > > +		mutex_lock(&bus->bus_lock);
> > > > +	}
> > > > +}
> > > 
> > > So it's nested locks?  Then you'd need some more trick to deal with
> > > the lockdep.  I guess you'll get the false-positive deadlock detection
> > > by this code when the mutex lock debug is enabled.
> > > 
> > > Also, is the linked order assured not to lead to a real deadlock?
> > >
> > 
> > Hi Takashi,
> > 
> > Thanks for the review :)
> > 
> > A multi link SoundWire stream consists of a list of Master runtimes and
> > more importantly only one master runtime per SoundWire bus instance.
> > 
> > So, these mutexes are actually different mutex locks(one per bus instance)
> > and are not nested.
> 
> You take a mutex lock inside a mutex lock, so they are nested.
> If they take the very same lock, it's called a "deadlock" instead.
> 

Ok, myy bad, I misunderstood the comment :(

I forgot to add that I did check with mutex debug enabled and lockdep did
not complain though :)

> > In SDW we have a bus instance per Master (link). In multi-link case, a
> > stream may have multiple Masters, thus we need to lock all bus instances
> > before we operate on them.
> > 
> > Now since these are invoked from a stream (pcm ops) they will be always
> > serialized and DPCM ensures we are never racing.
> > 
> > We did add this note here and in Documentation to make it explicit.
> 
> Well, my question is whether the order to take the multiple locks is
> always assured.  You're calling like:
> 
> 	list_for_each_entry(m_rt, &stream->master_list, stream_node)
> 		mutex_lock();
> 
> And it's a linked-list.  If a stream has a link of masters like
> M1->M2->M3 while another stream has a link like M2->M1->M3, it'll lead
> to a deadlock with the concurrent calls above.
> 

These are called from PCM stream ops context and the DPCM holds
lock(fe->card->mutex) which serializes these operations.
So, in the scenario you have mentioned, we would not have
concurrent calls to this function.

> > > > +/**
> > > > + * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
> > > > + *
> > > > + * @stream: SoundWire stream
> > > > + *
> > > > + * Release the previously held bus_lock after reconfiguring the bus.
> > > > + */
> > > > +static void sdw_release_bus_lock(struct sdw_stream_runtime *stream)
> > > > +{
> > > > +	struct sdw_master_runtime *m_rt = NULL;
> > > > +	struct sdw_bus *bus = NULL;
> > > > +
> > > > +	/* Iterate for all Master(s) in Master list */
> > > > +	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
> > > > +		bus = m_rt->bus;
> > > > +		mutex_unlock(&bus->bus_lock);
> > > > +	}
> > > 
> > > ... and this looks bad.  The loop for unlocking should be traversed
> > > reversely.
> > > 
> > 
> > Yes in principle I agree locking should be in reverse, but as explained
> > above in this case, it does not matter much :)
> 
> It does matter when you dealing with the multiple nested mutexes...
> 

Ok

--Shreyas

-- 

  reply	other threads:[~2018-06-26  9:25 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-25 10:58 [PATCH v4 0/7] soundwire: Add multi link support Shreyas NC
2018-06-25 10:58 ` [PATCH v4 1/7] Documentation: soundwire: Add documentation for multi link Shreyas NC
2018-06-25 10:58 ` [PATCH v4 2/7] soundwire: Initialize completion for defer messages Shreyas NC
2018-06-25 10:58 ` [PATCH v4 3/7] soundwire: Add support to lock across bus instances Shreyas NC
2018-06-25 12:38   ` Takashi Iwai
2018-06-26  8:22     ` Shreyas NC
2018-06-26  8:34       ` Takashi Iwai
2018-06-26  9:23         ` Shreyas NC [this message]
2018-06-26  9:46           ` Takashi Iwai
2018-06-26  9:59             ` Shreyas NC
2018-06-26 10:16               ` Takashi Iwai
2018-06-26 10:22                 ` Shreyas NC
2018-06-26 10:38                   ` Takashi Iwai
2018-06-25 10:58 ` [PATCH v4 4/7] soundwire: Handle multiple master instances in a stream Shreyas NC
2018-07-02 20:22   ` Pierre-Louis Bossart
2018-07-03  1:13     ` Shreyas NC
2018-07-03 15:03       ` Pierre-Louis Bossart
2018-07-03 16:03         ` Nc, Shreyas
2018-07-03 18:59           ` Pierre-Louis Bossart
2018-07-04  0:17             ` Nc, Shreyas
2018-07-04  4:24               ` Vinod
2018-06-25 10:58 ` [PATCH v4 5/7] soundwire: keep track of Masters " Shreyas NC
2018-06-25 10:58 ` [PATCH v4 6/7] soundwire: Add support for multi link bank switch Shreyas NC
2018-06-25 10:59 ` [PATCH v4 7/7] soundwire: intel: Add pre/post bank switch ops Shreyas NC

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=20180626092359.GD15119@snc-desk \
    --to=shreyas.nc@intel.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=patches.audio@intel.com \
    --cc=pierre-louis.bossart@linux.intel.com \
    --cc=sanyog.r.kale@intel.com \
    --cc=tiwai@suse.de \
    --cc=vkoul@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.