linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jon Hunter <jonathanh-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
To: Mark Brown <broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Cc: <linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	<linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	<linux-tegra-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	<vigneshr-l0cyMroinI0@public.gmane.org>
Subject: Re: [PATCH] spi: core: Fix deadlock when sending messages
Date: Tue, 8 Mar 2016 09:50:31 +0000	[thread overview]
Message-ID: <56DEA067.8040301@nvidia.com> (raw)
In-Reply-To: <1457430543-15179-1-git-send-email-jonathanh-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>

Adding Vignesh ...

On 08/03/16 09:49, Jon Hunter wrote:
> The function __spi_pump_messages() is called by spi_pump_messages() and
> __spi_sync(). The function __spi_sync() has an argument 'bus_locked'
> that indicates if it is called with the SPI bus mutex held or not. If
> 'bus_locked' is false then __spi_sync() will acquire the mutex itself.
> 
> Commit 556351f14e74 ("spi: introduce accelerated read support for spi
> flash devices") made a change to acquire the SPI bus mutex within
> __spi_pump_messages(). However, this change did not check to see if the
> mutex is already held. If __spi_sync() is called with the mutex held
> (ie. 'bus_locked' is true), then a deadlock occurs when
> __spi_pump_messages() is called.
> 
> Fix this deadlock by passing the 'bus_locked' state from __spi_sync() to
> __spi_pump_messages() and only acquire the mutex if not already held. In
> the case where __spi_pump_messages() is called from spi_pump_messages()
> it is assumed that the mutex is not held and so call
> __spi_pump_messages() with 'bus_locked' set to false. Finally, move the
> unlocking of the mutex to the end of the __spi_pump_messages() function
> to simplify the code and only call cond_resched() if there are no
> errors.
> 
> Fixes: 556351f14e74 ("spi: introduce accelerated read support for spi flash devices")
> 
> Signed-off-by: Jon Hunter <jonathanh-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
> ---
> 
> This deadlock is seen on the Tegra124 Nyan Big chromebook and prevents
> the board from booting -next.
> 
>  drivers/spi/spi.c | 28 ++++++++++++++++------------
>  1 file changed, 16 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
> index fe0196328aa0..e699fec9ddc5 100644
> --- a/drivers/spi/spi.c
> +++ b/drivers/spi/spi.c
> @@ -1062,7 +1062,8 @@ EXPORT_SYMBOL_GPL(spi_finalize_current_transfer);
>   * inside spi_sync(); the queue extraction handling at the top of the
>   * function should deal with this safely.
>   */
> -static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
> +static void __spi_pump_messages(struct spi_master *master, bool in_kthread,
> +				bool bus_locked)
>  {
>  	unsigned long flags;
>  	bool was_busy = false;
> @@ -1158,7 +1159,9 @@ static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
>  		}
>  	}
>  
> -	mutex_lock(&master->bus_lock_mutex);
> +	if (!bus_locked)
> +		mutex_lock(&master->bus_lock_mutex);
> +
>  	trace_spi_message_start(master->cur_msg);
>  
>  	if (master->prepare_message) {
> @@ -1168,8 +1171,7 @@ static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
>  				"failed to prepare message: %d\n", ret);
>  			master->cur_msg->status = ret;
>  			spi_finalize_current_message(master);
> -			mutex_unlock(&master->bus_lock_mutex);
> -			return;
> +			goto out;
>  		}
>  		master->cur_msg_prepared = true;
>  	}
> @@ -1178,21 +1180,23 @@ static void __spi_pump_messages(struct spi_master *master, bool in_kthread)
>  	if (ret) {
>  		master->cur_msg->status = ret;
>  		spi_finalize_current_message(master);
> -		mutex_unlock(&master->bus_lock_mutex);
> -		return;
> +		goto out;
>  	}
>  
>  	ret = master->transfer_one_message(master, master->cur_msg);
>  	if (ret) {
>  		dev_err(&master->dev,
>  			"failed to transfer one message from queue\n");
> -		mutex_unlock(&master->bus_lock_mutex);
> -		return;
> +		goto out;
>  	}
> -	mutex_unlock(&master->bus_lock_mutex);
> +
> +out:
> +	if (!bus_locked)
> +		mutex_unlock(&master->bus_lock_mutex);
>  
>  	/* Prod the scheduler in case transfer_one() was busy waiting */
> -	cond_resched();
> +	if (!ret)
> +		cond_resched();
>  }
>  
>  /**
> @@ -1204,7 +1208,7 @@ static void spi_pump_messages(struct kthread_work *work)
>  	struct spi_master *master =
>  		container_of(work, struct spi_master, pump_messages);
>  
> -	__spi_pump_messages(master, true);
> +	__spi_pump_messages(master, true, false);
>  }
>  
>  static int spi_init_queue(struct spi_master *master)
> @@ -2814,7 +2818,7 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message,
>  						       spi_sync_immediate);
>  			SPI_STATISTICS_INCREMENT_FIELD(&spi->statistics,
>  						       spi_sync_immediate);
> -			__spi_pump_messages(master, false);
> +			__spi_pump_messages(master, false, bus_locked);
>  		}
>  
>  		wait_for_completion(&done);
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2016-03-08  9:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-08  9:49 [PATCH] spi: core: Fix deadlock when sending messages Jon Hunter
     [not found] ` <1457430543-15179-1-git-send-email-jonathanh-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2016-03-08  9:50   ` Jon Hunter [this message]
2016-03-08 10:46   ` kbuild test robot
2016-03-08 12:28   ` [PATCH V2] " Jon Hunter
     [not found]     ` <1457440100-31546-1-git-send-email-jonathanh-DDmLM1+adcrQT0dZR+AlfA@public.gmane.org>
2016-03-09  2:34       ` Applied "spi: core: Fix deadlock when sending messages" to the spi tree Mark Brown

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=56DEA067.8040301@nvidia.com \
    --to=jonathanh-ddmlm1+adcrqt0dzr+alfa@public.gmane.org \
    --cc=broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-tegra-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=vigneshr-l0cyMroinI0@public.gmane.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 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).