spi/mmc_spi: SPI bus locking API, using mutex
Subsystems (3)
PATCH v2 1/2 spi/mmc_spi: SPI bus locking API, using mutex
From: Ernst Schwab <[email protected]>
SPI bus locking API to allow exclusive access to the SPI bus, especially, but not limited to, for the mmc_spi driver.
Coded according to an outline from Grant Likely; here is his specification (accidentally swapped function names corrected):
It requires 3 things to be added to struct spi_master.
- 1 Mutex
- 1 spin lock
- 1 flag.
The mutex protects spi_sync, and provides sleeping "for free" The spinlock protects the atomic spi_async call.
The flag is set when the lock is obtained, and checked while holding the spinlock in spi_async(). If the flag is checked, then spi_async() must fail immediately.
The current runtime API looks like this:spi_async(struct spi_device*, struct spi_message*); spi_sync(struct spi_device*, struct spi_message*);
The API needs to be extended to this:spi_async(struct spi_device*, struct spi_message*) spi_sync(struct spi_device*, struct spi_message*) spi_bus_lock(struct spi_master*) /* although struct spi_device* might be easier */spi_bus_unlock(struct spi_master*)spi_async_locked(struct spi_device*, struct spi_message*) spi_sync_locked(struct spi_device*, struct spi_message*)
Drivers can only call the last two if they already hold the spi_master_lock().
spi_bus_lock() obtains the mutex, obtains the spin lock, sets the flag, and releases the spin lock before returning. It doesn't even need to sleep while waiting for "in-flight" spi_transactions to complete because its purpose is to guarantee no additional transactions are added. It does not guarantee that the bus is idle.
spi_bus_unlock() clears the flag and releases the mutex, which will wake up any waiters.
The difference between spi_async() and spi_async_locked() is that the locked version bypasses the check of the lock flag. Both versions need to obtain the spinlock.
The difference between spi_sync() and spi_sync_locked() is that spi_sync() must hold the mutex while enqueuing a new transfer. spi_sync_locked() doesn't because the mutex is already held. Note however that spi_sync must *not* continue to hold the mutex while waiting for the transfer to complete, otherwise only one transfer could be queued up at a time!
Almost no code needs to be written. The current spi_async() and spi_sync() can probably be renamed to __spi_async() and __spi_sync() so that spi_async(), spi_sync(), spi_async_locked() and spi_sync_locked() can just become wrappers around the common code.
spi_sync() is protected by a mutex because it can sleep spi_async() needs to be protected with a flag and a spinlock because it can be called atomically and must not sleep
---
V2: integrated improvements suggested by Grant Likely
drivers/spi/spi.c +190 -33
@@ -524,6 +524,10 @@ int spi_register_master(struct spi_maste dynamic =1 ; } +spin_lock_init (&master->bus_lock_spinlock); +mutex_init (&master->bus_lock_mutex); + master->bus_lock_flag =0 ; +/* register the device, then userspace will see it. * registration fails if the bus ID is in use. */
@@ -663,6 +667,35 @@ int spi_setup(struct spi_device *spi) }EXPORT_SYMBOL_GPL (spi_setup); +static int __spi_async (struct spi_device *spi,struct spi_message *message) +{ +struct spi_master *master = spi->master; + +/* Half-duplex links include original MicroWire, and ones with +* only one data pin like SPI_3WIRE (switches direction) or where +* either MOSI or MISO is missing. They can also be caused by +* software limitations. +*/ +if ((master->flags &SPI_MASTER_HALF_DUPLEX ) + || (spi->mode &SPI_3WIRE )) { +struct spi_transfer *xfer; +unsigned flags = master->flags; + +list_for_each_entry (xfer, &message->transfers, transfer_list) { +if (xfer->rx_buf && xfer->tx_buf) +return -EINVAL ; +if ((flags &SPI_MASTER_NO_TX ) && xfer->tx_buf) +return -EINVAL ; +if ((flags &SPI_MASTER_NO_RX ) && xfer->rx_buf) +return -EINVAL ; + } + } + + message->spi = spi; + message->status = -EINPROGRESS ; +return master->transfer (spi, message); +} +/** * spi_async - asynchronous SPI transfer * @spi: device with which data will be exchanged
@@ -695,33 +728,68 @@ EXPORT_SYMBOL_GPL(spi_setup);int spi_async (struct spi_device *spi,struct spi_message *message) {struct spi_master *master = spi->master; +int ret; +unsigned long flags; -/* Half-duplex links include original MicroWire, and ones with -* only one data pin like SPI_3WIRE (switches direction) or where -* either MOSI or MISO is missing. They can also be caused by -* software limitations. -*/ -if ((master->flags &SPI_MASTER_HALF_DUPLEX ) - || (spi->mode &SPI_3WIRE )) { -struct spi_transfer *xfer; -unsigned flags = master->flags; +spin_lock_irqsave (&master->bus_lock_spinlock, flags); -list_for_each_entry (xfer, &message->transfers, transfer_list) { -if (xfer->rx_buf && xfer->tx_buf) -return -EINVAL ; -if ((flags &SPI_MASTER_NO_TX ) && xfer->tx_buf) -return -EINVAL ; -if ((flags &SPI_MASTER_NO_RX ) && xfer->rx_buf) -return -EINVAL ; - } - } +if (master->bus_lock_flag) + ret = -EBUSY ; +else + ret =__spi_async (spi, message); - message->spi = spi; - message->status = -EINPROGRESS ; -return master->transfer (spi, message); +spin_unlock_irqrestore (&master->bus_lock_spinlock, flags); + +return ret; }EXPORT_SYMBOL_GPL (spi_async); +/** +* spi_async_locked - version of spi_async with exclusive bus usage +* @spi: device with which data will be exchanged +* @message: describes the data transfers, including completion callback +* Context: any (irqs may be blocked, etc) +* +* This call may be used in_irq and other contexts which can't sleep, +* as well as from task contexts which can sleep. +* +* The completion callback is invoked in a context which can't sleep. +* Before that invocation, the value of message->status is undefined. +* When the callback is issued, message->status holds either zero (to +* indicate complete success) or a negative error code. After that +* callback returns, the driver which issued the transfer request may +* deallocate the associated memory; it's no longer in use by any SPI +* core or controller driver code. +* +* Note that although all messages to a spi_device are handled in +* FIFO order, messages may go to different devices in other orders. +* Some device might be higher priority, or have various "hard" access +* time requirements, for example. +* +* On detection of any fault during the transfer, processing of +* the entire message is aborted, and the device is deselected. +* Until returning from the associated message completion callback, +* no other spi_message queued to that device will be processed. +* (This rule applies equally to all the synchronous transfer calls, +* which are wrappers around this core asynchronous primitive.) +*/ +int spi_async_locked (struct spi_device *spi,struct spi_message *message) +{ +struct spi_master *master = spi->master; +int ret; +unsigned long flags; + +spin_lock_irqsave (&master->bus_lock_spinlock, flags); + + ret =__spi_async (spi, message); + +spin_unlock_irqrestore (&master->bus_lock_spinlock, flags); + +return ret; + +} +EXPORT_SYMBOL_GPL (spi_async_locked); +/*-------------------------------------------------------------------------*/
@@ -735,6 +803,32 @@ static void spi_complete(void *arg)complete (arg); } +static int __spi_sync (struct spi_device *spi,struct spi_message *message, +int bus_locked) +{ +DECLARE_COMPLETION_ONSTACK (done); +int status; +struct spi_master *master = spi->master; + + message->complete = spi_complete; + message->context = &done; + +if (!bus_locked) +mutex_lock (&master->bus_lock_mutex); + + status =spi_async_locked (spi, message); + +if (!bus_locked) +mutex_unlock (&master->bus_lock_mutex); + +if (status ==0 ) { +wait_for_completion (&done); + status = message->status; + } + message->context =NULL ; +return status; +} +/** * spi_sync - blocking/synchronous SPI data transfers * @spi: device with which data will be exchanged
@@ -758,21 +852,84 @@ static void spi_complete(void *arg)*/ int spi_sync (struct spi_device *spi,struct spi_message *message) { -DECLARE_COMPLETION_ONSTACK (done); -int status; - - message->complete = spi_complete; - message->context = &done; - status =spi_async (spi, message); -if (status ==0 ) { -wait_for_completion (&done); - status = message->status; - } - message->context =NULL ; -return status; +__spi_sync (spi, message,0 ); }EXPORT_SYMBOL_GPL (spi_sync); +/** +* spi_sync_locked - version of spi_sync with exclusive bus usage +* @spi: device with which data will be exchanged +* @message: describes the data transfers +* Context: can sleep +* +* This call may only be used from a context that may sleep. The sleep +* is non-interruptible, and has no timeout. Low-overhead controller +* drivers may DMA directly into and out of the message buffers. +* +* This call should be used by drivers that require exclusive access to the +* SPI bus. It has to be preceeded by a spi_bus_lock call. The SPI bus must +* be released by a spi_bus_unlock call when the exclusive access is over. +* +* It returns zero on success, else a negative error code. +*/ +int spi_sync_locked (struct spi_device *spi,struct spi_message *message) +{ +__spi_sync (spi, message,1 ); +} +EXPORT_SYMBOL_GPL (spi_sync_locked); + +/** +* spi_bus_lock - obtain a lock for exclusive SPI bus usage +* @master: SPI bus master that should be locked for exclusive bus access +* Context: can sleep +* +* This call may only be used from a context that may sleep. The sleep +* is non-interruptible, and has no timeout. +* +* This call should be used by drivers that require exclusive access to the +* SPI bus. The SPI bus must be released by a spi_bus_unlock call when the +* exclusive access is over. Data transfer must be done by spi_sync_locked +* and spi_async_locked calls when the SPI bus lock is held. +* +* It returns zero on success, else a negative error code. +*/ +int spi_bus_lock (struct spi_master *master) +{ +mutex_lock (&master->bus_lock_mutex); + +spin_lock (&master->bus_lock_spinlock); + master->bus_lock_flag =1 ; +spin_unlock (&master->bus_lock_spinlock); + +/* mutex remains locked until spi_bus_unlock is called */ + +return 0 ; +} +EXPORT_SYMBOL_GPL (spi_bus_lock); + +/** +* spi_bus_unlock - release the lock for exclusive SPI bus usage +* @master: SPI bus master that was locked for exclusive bus access +* Context: can sleep +* +* This call may only be used from a context that may sleep. The sleep +* is non-interruptible, and has no timeout. +* +* This call releases an SPI bus lock previously obtained by an spi_bus_lock +* call. +* +* It returns zero on success, else a negative error code. +*/ +int spi_bus_unlock (struct spi_master *master) +{ + master->bus_lock_flag =0 ; + +mutex_unlock (&master->bus_lock_mutex); + +return 0 ; +} +EXPORT_SYMBOL_GPL (spi_bus_unlock); +/* portable code must never pass more than 32 bytes */ #define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
include/linux/spi/spi.h +12 -0
@@ -261,6 +261,13 @@ struct spi_master {#define SPI_MASTER_NO_RX BIT(1)/* can't do buffer read */ #define SPI_MASTER_NO_TX BIT(2)/* can't do buffer write */ +/* lock and mutex for SPI bus locking */ +spinlock_t bus_lock_spinlock; +struct mutex bus_lock_mutex; + +/* flag indicating that the SPI bus is locked for exclusive use */ +u8 bus_lock_flag; +/* Setup mode and clock, etc (spi driver may call many times). * * IMPORTANT: this may be called when transfers to another
@@ -541,6 +548,8 @@ static inline void spi_message_free(struextern int spi_setup (struct spi_device *spi);extern int spi_async (struct spi_device *spi,struct spi_message *message); +extern int spi_async_locked (struct spi_device *spi, +struct spi_message *message);/*---------------------------------------------------------------------------*/
@@ -550,6 +559,9 @@ extern int spi_async(struct spi_device **/ extern int spi_sync (struct spi_device *spi,struct spi_message *message); +extern int spi_sync_locked (struct spi_device *spi,struct spi_message *message); +extern int spi_bus_lock (struct spi_master *master); +extern int spi_bus_unlock (struct spi_master *master);/** * spi_write - SPI synchronous write
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
A message that is not in the archive (sent off-list or not yet received): 20100218124425.599c4d76.eschwab-BGeptl67XyCzQB+pC5nmwQ@public.gmane.org
PATCH v2 2/2 spi/mmc_spi: mmc_spi adaptations for SPI bus locking API
From: Ernst Schwab <[email protected]>
Modification of the mmc_spi driver to use the SPI bus locking API. With this, the mmc_spi driver can be used together with other SPI devices on the same SPI bus. The exclusive access to the SPI bus is now managed in the SPI layer. The counting of chip selects in the probe function is no longer needed.
---
V2: pass spi_master* instead of spi* to spi_bus_lock
drivers/mmc/host/mmc_spi.c +11 -48
@@ -181,7 +181,7 @@ mmc_spi_readbytes(struct mmc_spi_host *h host->data_dma,sizeof (*host->data), DMA_FROM_DEVICE); - status =spi_sync (host->spi, &host->readback); + status =spi_sync_locked (host->spi, &host->readback);if (host->dma_dev) dma_sync_single_for_cpu(host->dma_dev,
@@ -540,7 +540,7 @@ mmc_spi_command_send(struct mmc_spi_host host->data_dma,sizeof (*host->data), DMA_BIDIRECTIONAL); } - status =spi_sync (host->spi, &host->m); + status =spi_sync_locked (host->spi, &host->m);if (host->dma_dev) dma_sync_single_for_cpu(host->dma_dev,
@@ -684,7 +684,7 @@ mmc_spi_writeblock(struct mmc_spi_host * host->data_dma,sizeof (*scratch), DMA_BIDIRECTIONAL); - status =spi_sync (spi, &host->m); + status =spi_sync_locked (spi, &host->m);if (status !=0 ) {dev_dbg (&spi->dev,"write error (%d)\n" , status);
@@ -821,7 +821,7 @@ mmc_spi_readblock(struct mmc_spi_host *h DMA_FROM_DEVICE); } - status =spi_sync (spi, &host->m); + status =spi_sync_locked (spi, &host->m);if (host->dma_dev) { dma_sync_single_for_cpu(host->dma_dev,
@@ -1017,7 +1017,7 @@ mmc_spi_data_do(struct mmc_spi_host *hos host->data_dma,sizeof (*scratch), DMA_BIDIRECTIONAL); - tmp =spi_sync (spi, &host->m); + tmp =spi_sync_locked (spi, &host->m);if (host->dma_dev) dma_sync_single_for_cpu(host->dma_dev,
@@ -1083,6 +1083,9 @@ static void mmc_spi_request(struct mmc_h }#endif +/* request exclusive bus access */ +spi_bus_lock (host->spi->master); +/* issue command; then optionally data and stop */ status =mmc_spi_command_send (host, mrq, mrq->cmd, mrq->data !=NULL );if (status ==0 && mrq->data) {
@@ -1093,6 +1096,9 @@ static void mmc_spi_request(struct mmc_hmmc_cs_off (host); } +/* release the bus */ +spi_bus_unlock (host->spi->master); +mmc_request_done (host->mmc, mrq); }
@@ -1289,23 +1295,6 @@ mmc_spi_detect_irq(int irq, void *mmc)return IRQ_HANDLED ; } -struct count_children { -unsigned n; -struct bus_type *bus; -}; - -static int maybe_count_child (struct device *dev,void *c) -{ -struct count_children *ccp = c; - -if (dev->bus == ccp->bus) { -if (ccp->n) -return -EBUSY ; - ccp->n++; - } -return 0 ; -} -static int mmc_spi_probe (struct spi_device *spi) {void *ones;
@@ -1337,32 +1326,6 @@ static int mmc_spi_probe(struct spi_devireturn status; } -/* We can use the bus safely iff nobody else will interfere with us. -* Most commands consist of one SPI message to issue a command, then -* several more to collect its response, then possibly more for data -* transfer. Clocking access to other devices during that period will -* corrupt the command execution. -* -* Until we have software primitives which guarantee non-interference, -* we'll aim for a hardware-level guarantee. -* -* REVISIT we can't guarantee another device won't be added later... -*/ -if (spi->master->num_chipselect >1 ) { -struct count_children cc; - - cc.n =0 ; - cc.bus = spi->dev.bus; - status =device_for_each_child (spi->dev.parent, &cc, - maybe_count_child); -if (status <0 ) { -dev_err (&spi->dev,"can't share SPI bus\n" ); -return status; - } - -dev_warn (&spi->dev,"ASSUMING SPI bus stays unshared!\n" ); - } -/* We need a supply of ones to transmit. This is the only time * the CPU touches these, so cache coherency isn't a concern. *
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
I integrated your changes in V2, I hope I did not miss anything.
On Wed, 17 Feb 2010 13:03:04 -0700
Grant Likely <grant.likely-s3s/[email protected]> wrote:
[...]
Quoted 5 lines
spi_sync()andspi_sync_locked()are largely idenical. Only difference is thatspi_sync()needs to obtain the mutex. Renamespi_sync()to__spi_sync()and add a 'get_lock' flag parameter. Then makespi_sync()andspi_sync_locked()call__spi_sync()with the flag set appropriately.
I made the parameter 'bus_locked' instead of 'get_lock' because we do not only get the lock but release it after use.
[...]
Quoted 8 lines
Review
Quoted 4 lines
extern int
spi_setup(struct spi_device *spi);
extern intspi_async(struct spi_device *spi, struct spi_message *message);(quoted diff) +2
-0+extern int +spi_async_locked(struct spi_device *spi, struct spi_message *message);Please keep return parameters and annotations on the same line as the function name (makes grep results more useful). You can break lines between parameters.
Hm, the prototype for spi_alloc_master in the same file let me assume, that this should be the style used in this header file. Normally, I wouldn't do a linebreak there, ever...
Something more important:
I have concerns on the use of the spin lock around the __spi_async() call, as Mike Frysinger already remarked.
Why do we 'need' this spin lock:
- by definition, spi_async can be called from any context
" * Context: any (irqs may be blocked, etc) * * This call may be used in_irq and other contexts which can't sleep, * as well as from task contexts which can sleep. "
- we are not sure that spi_async isn't used reentrant (e.g. interrupt service routine and normal context
- __spi_async calls the master->transfer() function which is not required to be reentrant (or is it?)
But: This all is true for the original design, there seems to be no protection against accidental reentrant use of spi_async().
Maybe we should check if there is any current call of spi_async() from context that cannot sleep, and change the definition where it is allowed to be called, if possible.
Regards
Ernst
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
A message that is not in the archive (sent off-list or not yet received): 20100218131057.40f9e623.eschwab-BGeptl67XyCzQB+pC5nmwQ@public.gmane.org
Hey Ernst. Good patch, thank you.
The merge window is going to open any moment now, and this patch hasn't had any linux-next exposure, so I'm not going to pick it up for 2.6.34. Instead, I'll wait until after the merge window closes and add it to my next branch so that it gets lots of exposure before the 2.6.35 merge window. Since this is a pretty core change, I'd like to have as much feedback and testing on it as possible before I commit to pushing it into mainline.
Anyone who is following along and is interested, please test these two patches and let me know if they work for you.
Ping me after Linus tags v2.6.34-rc1 to make sure I don't forget to put it in my -next branch.
Some comments below...
Quoted 13 lines · Ernst Schwab
I integrated your changes in V2, I hope I did not miss anything.
Quoted 8 lines · On Wed, 17 Feb 2010 13:03:04 -0700 Grant Likely…
Review
Quoted 4 lines
extern int
spi_setup(struct spi_device *spi);
extern intspi_async(struct spi_device *spi, struct spi_message *message);(quoted diff) +2
-0+extern int +spi_async_locked(struct spi_device *spi, struct spi_message *message);Please keep return parameters and annotations on the same line as the function name (makes grep results more useful). You can break lines between parameters.
Quoted 3 lines
Hm, the prototype for
spi_alloc_masterin the same file let me assume, that this should be the style used in this header file. Normally, I wouldn't do a linebreak there, ever...
Yeah, things are inconsistent, but in this case both styles are present in the file, and when grepping I find it more important to have the annotations than the parameters immediately visable.
Quoted 6 lines
Something more important:
I have concerns on the use of the spin lock around the
__spi_async()call, as Mike Frysinger already remarked.
Quoted 12 lines
Why do we 'need' this spin lock:
- by definition,spi_asynccan be called from any context
"
* Context: any (irqs may be blocked, etc)
*
* This call may be usedin_irqand other contexts which can't sleep,
* as well as from task contexts which can sleep.
"
- we are not sure thatspi_asyncisn't used reentrant (e.g. interrupt service routine and normal context
-__spi_asynccalls themaster->transfer()function which is not required to be reentrant (or is it?)
Um, I'm not sure I follow the argument. spin locks are safe to obtain in any context. Mutexes are the ones that cannot be held at atomic context.
The spin lock does more than just protect the bus_is_locked flag. It also prevent overlap between process and IRQ context running in the relevant code. Basically the spin_lock_irqsave() either defers all IRQ processing in uniprocessor, or has other processors spin in SMP. I think the spinlock should always be held when calling the bus drivers ->transfer() hook. That will also allow us to remove driver-specific locking from the bus drivers. Otherwise we've got an inconsistent locking scheme which is asking for trouble down the road.
Quoted 3 lines
But: This all is true for the original design, there seems to be no protection against accidental reentrant use of
spi_async().
spi_async() is intended to be callable from atomic context so that IRQ handers can trigger a new transfer without waiting around for it to complete. It is also totally valid for both process and IRQ contexts to call spi_async() at the same time, which is exactly why the spinlock should be required.
Hmmm. Are you concerned about a code path where an spi_async() call will call the bus driver, which will end up calling spi_async() on itself? I cannot think of a use case where that would be desirable.
g.
Signature · 9 lines
Grant Likely, B.Sc., P.Eng. Secret Lab Technologies Ltd. ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev
A message that is not in the archive (sent off-list or not yet received): fa686aa41002241232x6b6d92fci73ce7597c6e38b0-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 1 line · On Wed, 24 Feb 2010 13:32:43 -0700 Grant Likely…
On Thu, Feb 18, 2010 at 5:10 AM, Ernst Schwab <[email protected]> wrote:
[...]
Quoted 2 lines
Quoted 2 lines
I have concerns on the use of the spin lock around the
__spi_async()call, as Mike Frysinger already remarked.
[...]
Quoted 4 lines
I think the spinlock should always be held when calling the bus drivers ->transfer() hook. That will also allow us to remove driver-specific locking from the bus drivers. Otherwise we've got an inconsistent locking scheme which is asking for trouble down the road.
Hm, I do not agree to that. I think it is a bad idea to have a spinlock around a function call that may do a lot of things (we don't know about it in the spi.c module) and may block multiple CPU cores or disable interrupts for an indefinite time. The list_for_each_entry(xfer,...) loop may process many elements, during that time, all other cores are locked off without need.
The implementation present in the kernel (without patches) apparently made the assumption, that master->transfer() itself can be called from process and interrupt context concurrently and does its own locking. This is true for the (few) drivers I looked it up.
The list_for_each_entry(xfer, &message->transfers, transfer_list) loop seems to be safe (although I'm not absolutely sure about it) without locking, since it only seems to access data stored in the message passed as a function argument.
With our additional spinlocking around the whole stuff we add significant additional locking time without a need.
I suggest to remove the spinlocking in spi_async (see patch below).
Quoted 8 lines
Quoted 2 lines
But: This all is true for the original design, there seems to be no protection against accidental reentrant use of
spi_async().
spi_async()is intended to be callable from atomic context so that IRQ handers can trigger a new transfer without waiting around for it to complete. It is also totally valid for both process and IRQ contexts to callspi_async()at the same time, which is exactly why the spinlock should be required.
Seems the spinlock is already in master->transfer() (I missed that, because I checked the wrong function...), so it is valid that multiple contexts (process, IRQ) call master->transfer() concurrently.
Quoted 3 lines
Hmmm. Are you concerned about a code path where an
spi_async()call will call the bus driver, which will end up callingspi_async()on itself? I cannot think of a use case where that would be desirable.
No, I didn't think of this - this isn't desirable, I agree.
Regards
Ernst
=8<=====================================================
From: Ernst Schwab <[email protected]>
Followup-Patch: Remove spin lock around __spi_async call.
---
drivers/spi/spi.c +0 -4
@@ -731,15 +731,11 @@ int spi_async(struct spi_device *spi, stint ret;unsigned long flags; -spin_lock_irqsave (&master->bus_lock_spinlock, flags); -if (master->bus_lock_flag) ret = -EBUSY ;else ret =__spi_async (spi, message); -spin_unlock_irqrestore (&master->bus_lock_spinlock, flags); -return ret; }EXPORT_SYMBOL_GPL (spi_async);
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
A message that is not in the archive (sent off-list or not yet received): 20100225113601.fc8cf2c1.eschwab-BGeptl67XyCzQB+pC5nmwQ@public.gmane.org
Quoted 12 lines · Ernst Schwab
Quoted 1 line · On Wed, 24 Feb 2010 13:32:43 -0700 Grant Likely <[email protected]>…
On Thu, Feb 18, 2010 at 5:10 AM, Ernst Schwab <[email protected]> wrote:
[...]
Quoted 2 lines
Quoted 2 lines
I have concerns on the use of the spin lock around the
__spi_async()call, as Mike Frysinger already remarked.[...]
Quoted 4 lines
I think the spinlock should always be held when calling the bus drivers ->transfer() hook. That will also allow us to remove driver-specific locking from the bus drivers. Otherwise we've got an inconsistent locking scheme which is asking for trouble down the road.
Quoted 14 lines
Hm, I do not agree to that. I think it is a bad idea to have a spinlock around a function call that may do a lot of things (we don't know about it in the
spi.cmodule) and may block multiple CPU cores or disable interrupts for an indefinite time. Thelist_for_each_entry(xfer,...)loop may process many elements, during that time, all other cores are locked off without need.
The implementation present in the kernel (without patches) apparently made the assumption, thatmaster->transfer()itself can be called from process and interrupt context concurrently and does its own locking. This is true for the (few) drivers I looked it up.
Thelist_for_each_entry(xfer, &message->transfers, transfer_list)loop seems to be safe (although I'm not absolutely sure about it) without locking, since it only seems to access data stored in the message passed as a function argument.
I looked at them all this morning. Without exception, every single driver follows this pattern:
- [maybe check some stuff]
- spin_lock_irqsave()
- list_add_tail()
- <trigger workqueue or other way to kick off transfer>
- spin_unlock_irqrestore()
Yet every driver implements its own transfer list and spin lock in the private data structure. That says to me that the spi layer is abstracting at the wrong level. I'm cautious about taking away the ability for drivers to make their own decisions about how to manage transfers, but it seems like the transfer queue and spin lock should be provided by the common code. The trick is how to do it while providing a migration path for drivers.
One option is to deprecate the ->transfer hook and allow drivers to provide a .request() hook to signal the driver that new transfers have been added to the list. Something like how the block layer works. That would keep the critical region in spi_async() small. Another option is to let drivers provide a .prepare() hook that validates and prepares a message before entering the critical region. The .prepare() option is probably the simplest to implement and has the lowest impact on existing device drivers to use the new functionality, but the .request() option might be better in the long run.
Quoted 3 lines
With our additional spinlocking around the whole stuff we add significant additional locking time without a need.
I suggest to remove the spinlocking inspi_async(see patch below).
The spinlock is absolutely required. Otherwise the test of bus_lock_flag is racy. For example, in the mmc case, a non-mmc message could get queued after the mmc code obtains the bus lock. Consider two threads, A & B. A is running mmc code. Consider the following sequence of events:
A1: obtain mutex
A2: obtain spinlock
A3: set flag
A4: release spinlock
A5: call async_transfer_locked()
A6: call async_transfer_locked()
A7: call async_transfer_locked()
A8: clear flag
A9: release mutex
B1: obtain spinlock
B2: check flag; bail if set
B3: call .transfer() hook
B4: release spinlock
In this case, there is no way for the B-3 transfer to occur between A-2 and A8. However, if B1 and B4 are omitted, you can get this sequence:
A1: obtain mutex
A2: obtain spinlock
B2: check flag; bail if set
A3: set flag
A4: release spinlock
A5: call async_transfer_locked()
B3: call .transfer() hook
A6: call async_transfer_locked()
A7: call async_transfer_locked()
A8: clear flag
A9: release mutex
See? Without the spinlock there is nothing preventing B3 from getting called in the midst of A's bus lock.
Quoted 12 lines
Quoted 8 lines
Quoted 2 lines
But: This all is true for the original design, there seems to be no protection against accidental reentrant use of
spi_async().
spi_async()is intended to be callable from atomic context so that IRQ handers can trigger a new transfer without waiting around for it to complete. It is also totally valid for both process and IRQ contexts to callspi_async()at the same time, which is exactly why the spinlock should be required.Seems the spinlock is already in
master->transfer()(I missed that, because I checked the wrong function...), so it is valid that multiple contexts (process, IRQ) callmaster->transfer()concurrently.
Quoted 19 lines · Ernst Schwab
Quoted 2 lines · Ernst Schwab
int
spi_async(struct spi_device *spi, struct spi_message *message){[...]
Review
Quoted 7 lines
(quoted diff) +5
-0+ if (master->bus_lock_flag) + ret = -EBUSY; + else + ret = __spi_async(spi, message); + return ret; }I'm still not satisfied with this.
The code present in the kernel (without patches) allows
spi_async()to be called concurrently from interrupt context and process context, without any loss of data.
Quoted 6 lines
With the first patch attempt (bus lock implemented in the master driver) no messages were lost during the bus lock.
With the current patch, drivers calling
spi_async()from the interrupt context (are there any??) will get an error code, which they cannot handle without adding additional queuing code in the driver.
Yes there are. I've written drivers that submit new SPI messages at IRQ context.
Quoted 5 lines
A possible solution would be to add an extra queue, handled in the
spi.cmodule, stored in the master's data structure, that stores all messages coming in while the bus lock is active. The queue can be flushed by calling__spi_async()for each message as soon as the bus lock is removed.
That also works. In fact, this would work well with the .request() hook suggestion I made above because when the lock is removed, the delayed list could just be appended to the normal request list. A .prepare() hook would also be useful because the message can still be pre-checked for validity outside of the critical region before being added to the delay list.
If you want to attempt this, maintain it as a separate patch on top of the core bus locking patch.
Cheers,
g.
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
A message that is not in the archive (sent off-list or not yet received): fa686aa41002250838q3189d1e6q828428c521242742-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 16 lines · On Thu, 25 Feb 2010 09:38:48 -0700 Grant Likely…
I looked at them all this morning. Without exception, every single driver follows this pattern:
- [maybe check some stuff]
-spin_lock_irqsave()
-list_add_tail()
- <trigger workqueue or other way to kick off transfer>
-spin_unlock_irqrestore()Yet every driver implements its own transfer list and spin lock in the private data structure. That says to me that the spi layer is abstracting at the wrong level. I'm cautious about taking away the ability for drivers to make their own decisions about how to manage transfers, but it seems like the transfer queue and spin lock should be provided by the common code. The trick is how to do it while providing a migration path for drivers.
Quoted 9 lines
One option is to deprecate the ->transfer hook and allow drivers to provide a .request() hook to signal the driver that new transfers have been added to the list. Something like how the block layer works. That would keep the critical region in
spi_async()small. Another option is to let drivers provide a .prepare() hook that validates and prepares a message before entering the critical region. The .prepare() option is probably the simplest to implement and has the lowest impact on existing device drivers to use the new functionality, but the .request() option might be better in the long run.
I'd vote for the .request() hook.
Quoted 2 lines
The spinlock is absolutely required. Otherwise the test of
bus_lock_flagis racy.
[...]
Quoted 2 lines
See? Without the spinlock there is nothing preventing B3 from getting called in the midst of A's bus lock.
Ok, now, I see; an interrupt being executed on a different core than the process context will manage it to malfunction if the spinlock is missing.
Quoted 6 lines
Quoted 5 lines
A possible solution would be to add an extra queue, handled in the
spi.cmodule, stored in the master's data structure, that stores all messages coming in while the bus lock is active. The queue can be flushed by calling__spi_async()for each message as soon as the bus lock is removed.
Quoted 6 lines
That also works. In fact, this would work well with the .request() hook suggestion I made above because when the lock is removed, the delayed list could just be appended to the normal request list. A .prepare() hook would also be useful because the message can still be pre-checked for validity outside of the critical region before being added to the delay list.
Maybe all of the list/queue handling should be done in spi.c, and (inline) functions in spi.h; then, the driver could call a 'spi_get_next_message()' function and will get a message out of one of the two queues:
spi_sync_locked() and spi_async_locked()
will add to 'direct' queue, and call .request()spi_sync() and spi_async()
will add to 'delayed' queue if spi bus is locked will add to 'direct' queue if unlocked, and call .request()
spi_bus_unlock()
will append entire 'delayed' to 'direct' queue, and call .request()
spi_get_next_message()
will get next message from 'direct' queue
Quoted 2 lines
If you want to attempt this, maintain it as a separate patch on top of the core bus locking patch.
I will look at it if I find the time...
Regards
Ernst
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
A message that is not in the archive (sent off-list or not yet received): 20100225182556.10b5a3b8.eschwab-BGeptl67XyCzQB+pC5nmwQ@public.gmane.org
Quoted 28 lines · Ernst Schwab
Quoted 16 lines · On Thu, 25 Feb 2010 09:38:48 -0700 Grant Likely <[email protected]>…
I looked at them all this morning. Without exception, every single driver follows this pattern:
- [maybe check some stuff]
-spin_lock_irqsave()
-list_add_tail()
- <trigger workqueue or other way to kick off transfer>
-spin_unlock_irqrestore()Yet every driver implements its own transfer list and spin lock in the private data structure. That says to me that the spi layer is abstracting at the wrong level. I'm cautious about taking away the ability for drivers to make their own decisions about how to manage transfers, but it seems like the transfer queue and spin lock should be provided by the common code. The trick is how to do it while providing a migration path for drivers.
Quoted 9 lines
One option is to deprecate the ->transfer hook and allow drivers to provide a .request() hook to signal the driver that new transfers have been added to the list. Something like how the block layer works. That would keep the critical region in
spi_async()small. Another option is to let drivers provide a .prepare() hook that validates and prepares a message before entering the critical region. The .prepare() option is probably the simplest to implement and has the lowest impact on existing device drivers to use the new functionality, but the .request() option might be better in the long run.
Quoted 1 line
I'd vote for the .request() hook.
It may even be advantageous to provide both .prepare and .request hooks. .prepare would allow messages to be checked out of line before touching any critical regions at all. The two hooks are pretty much mutually independent.
Quoted 9 lines
Quoted 2 lines
The spinlock is absolutely required. Otherwise the test of
bus_lock_flagis racy.[...]
Quoted 2 lines
See? Without the spinlock there is nothing preventing B3 from getting called in the midst of A's bus lock.
Ok, now, I see; an interrupt being executed on a different core than the process context will manage it to malfunction if the spinlock is missing.
It is even broken on a single processor. If an IRQ occurs between B2 and B3, and the IRQ goes down an spi bus lock path, then things still get out of order.
Quoted 13 lines
Quoted 6 lines
Quoted 5 lines
A possible solution would be to add an extra queue, handled in the
spi.cmodule, stored in the master's data structure, that stores all messages coming in while the bus lock is active. The queue can be flushed by calling__spi_async()for each message as soon as the bus lock is removed.Quoted 6 lines
That also works. In fact, this would work well with the .request() hook suggestion I made above because when the lock is removed, the delayed list could just be appended to the normal request list. A .prepare() hook would also be useful because the message can still be pre-checked for validity outside of the critical region before being added to the delay list.
Quoted 3 lines
Maybe all of the list/queue handling should be done in
spi.c, and (inline) functions inspi.h; then, the driver could call a 'spi_get_next_message()' function and will get a message out of one of the two queues:
Try not to get too exotic here. It should be sufficient for the driver to only look at the 'direct' queue and let the core code decide when to move stuff from delayed to direct. In fact, the existing list_del_init() callsites will probably work as-is with just changing the pointer to the list head.
Quoted 8 lines
spi_sync_locked()andspi_async_locked()
will add to 'direct' queue, and call .request()spi_sync()andspi_async()
will add to 'delayed' queue if spi bus is locked will add to 'direct' queue if unlocked, and call .request()
spi_bus_unlock()
will append entire 'delayed' to 'direct' queue, and call .request()
Sounds appropriate. However, I think you'll want to avoid having to change every bus driver all at once. I think this can be done without breaking drivers. If you leave the .transfer() hook in place and add the .request() hook, then drivers can provide one or the other.
If a .transfer() hook is provided, then everything works fine. spi_async*() calls the transfer hook for each message received. If the bus is locked, then spi_async() adds them to the delayed queue without calling .transfer().
If transfer is NULL, then the setup code should set .transfer to a 'stock' transfer function that only adds the message to the direct queue. Drivers that migrate to this mode then take advantage of the nice short critical region provided by the stock .transfer() hook.
In either case, spi_bus_unlock() will walk the delayed queue and call .transfer() on each message. If .request is set, then it should be called after each .transfer() call and also at the end of spi_bus_unlock().
Once all the drivers are migrated to the .request() API, then the stock transfer hook can be folded directly into __spi_async() , the hook removed from struct spi_master, and the spi_bus_unlock() path can be simplified to append the delayed queue onto the direct queue.
Quoted 4 lines
Quoted 2 lines
If you want to attempt this, maintain it as a separate patch on top of the core bus locking patch.
I will look at it if I find the time...
Thanks.
g.
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
Quoted 28 lines · Grant Likely
Quoted 12 lines · Ernst Schwab
Quoted 1 line · On Wed, 24 Feb 2010 13:32:43 -0700 Grant Likely…
On Thu, Feb 18, 2010 at 5:10 AM, Ernst Schwab <[email protected]> wrote:
[...]
Quoted 2 lines
Quoted 2 lines
I have concerns on the use of the spin lock around the
__spi_async()call, as Mike Frysinger already remarked.[...]
Quoted 4 lines
I think the spinlock should always be held when calling the bus drivers ->transfer() hook. That will also allow us to remove driver-specific locking from the bus drivers. Otherwise we've got an inconsistent locking scheme which is asking for trouble down the road.
Quoted 14 lines
Hm, I do not agree to that. I think it is a bad idea to have a spinlock around a function call that may do a lot of things (we don't know about it in the
spi.cmodule) and may block multiple CPU cores or disable interrupts for an indefinite time. Thelist_for_each_entry(xfer,...)loop may process many elements, during that time, all other cores are locked off without need.
The implementation present in the kernel (without patches) apparently made the assumption, thatmaster->transfer()itself can be called from process and interrupt context concurrently and does its own locking. This is true for the (few) drivers I looked it up.
Thelist_for_each_entry(xfer, &message->transfers, transfer_list)loop seems to be safe (although I'm not absolutely sure about it) without locking, since it only seems to access data stored in the message passed as a function argument.
Quoted 7 lines
I looked at them all this morning. Without exception, every single driver follows this pattern:
- [maybe check some stuff]
-spin_lock_irqsave()
-list_add_tail()
- <trigger workqueue or other way to kick off transfer>
-spin_unlock_irqrestore()
I am only familiar with the px2xx_spi.c driver. This driver has a lock in ->transfer() that is constructed as above. The lock protects internal data structures that are shared between ->transfer(), a workqueue that pulls the next message from the queue, and a tasklet that prepares each transfer within a message for processing by the hardware and returns any completed messages. In addition to the list_add_tail() mentioned above, there is also private (perhaps could be public) message state information that gets set.
As a side note: I noticed that the data being protected in ->transfer() is only accessed in workqueue and tasklet context, never in interrupt context. Thus, in my (greatly extended) private version of pxa2xx_spi.c, I added a new lock to the private driver data and swapped the spinlock_irqsave for spinlock_bh in all places that protect the message queue and state, so that there would be less (however small) impact on hardware interrupts.
Quoted 7 lines
Yet every driver implements its own transfer list and spin lock in the private data structure. That says to me that the spi layer is abstracting at the wrong level. I'm cautious about taking away the ability for drivers to make their own decisions about how to manage transfers, but it seems like the transfer queue and spin lock should be provided by the common code. The trick is how to do it while providing a migration path for drivers.
I don't see how migrating the queue and locking to the core saves a lot.
You still need a queue per controller driver. It would provide
standardization of the function, but the function is not complicated, as you point out above. Most of the manipulation of the queue still exists inside the driver. The existing spi core design allows drivers to add whatever other state information they need within the same lock. I'm not sure if every driver's private state is standard enough that it could become public. If not, it is important (helpful, required?) that the state be updated atomically with the queue entry.
Quoted 9 lines
One option is to deprecate the ->transfer hook and allow drivers to provide a .request() hook to signal the driver that new transfers have been added to the list. Something like how the block layer works. That would keep the critical region in
spi_async()small. Another option is to let drivers provide a .prepare() hook that validates and prepares a message before entering the critical region. The .prepare() option is probably the simplest to implement and has the lowest impact on existing device drivers to use the new functionality, but the .request() option might be better in the long run.
You definitely need something like .prepare, if this change is made. As David Brownell has pointed out in the past, the driver is responsible for rejecting any message that it cannot implement, preferably at the time ->transfer() is called. This principal is not universally implemented in all drivers (pxa2xx_spi rejects some messages at the time it tries to execute a transfer, rather than checking the whole message when queued, as it should). Reasons for rejection include
unimplementable: clock speed, length, bits/word, etc. Only the driver knows the capability of the hardware; that function cannot be centralized.
Signature · 16 lines
Ned Forrester nforrester-/[email protected] Oceanographic Systems Lab 508-289-2226 Applied Ocean Physics and Engineering Dept. Woods Hole Oceanographic Institution Woods Hole, MA 02543, USA http://www.whoi.edu/ http://www.whoi.edu/sbl/liteSite.do?litesiteid=7212 http://www.whoi.edu/hpb/Site.do?id=1532 http://www.whoi.edu/page.do?pid=10079 ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev
Followup patch to correct missing return value.
---
Apparently I missed to look at the compiler warnings, sorry for that.
drivers/spi/spi.c +2 -2
@@ -852,7 +852,7 @@ static int __spi_sync(struct spi_device*/ int spi_sync (struct spi_device *spi,struct spi_message *message) { -__spi_sync (spi, message,0 ); +return __spi_sync (spi, message,0 ); }EXPORT_SYMBOL_GPL (spi_sync);
@@ -874,7 +874,7 @@ EXPORT_SYMBOL_GPL(spi_sync);*/ int spi_sync_locked (struct spi_device *spi,struct spi_message *message) { -__spi_sync (spi, message,1 ); +return __spi_sync (spi, message,1 ); }EXPORT_SYMBOL_GPL (spi_sync_locked);
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
Quoted 2 lines · Ernst Schwab
int
spi_async(struct spi_device *spi, struct spi_message *message){
[...]
drivers/spi/spi.c EXPORT_SYMBOL_GPL(spi_setup); · line 744
Quoted 7 lines
drivers/spi/spi.c +5
-0+if (master->bus_lock_flag) + ret = -EBUSY ; +else + ret =__spi_async (spi, message); +return ret; }
I'm still not satisfied with this.
The code present in the kernel (without patches) allows spi_async() to be called concurrently from interrupt context and process context, without any loss of data.
With the first patch attempt (bus lock implemented in the master driver) no messages were lost during the bus lock.
With the current patch, drivers calling spi_async() from the interrupt context (are there any??) will get an error code, which they cannot handle without adding additional queuing code in the driver.
A possible solution would be to add an extra queue, handled in the spi.c module, stored in the master's data structure, that stores all messages coming in while the bus lock is active. The queue can be flushed by calling __spi_async() for each message as soon as the bus lock is removed.
Regards
Ernst
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev