Skip to contentSkip to conversation

spi: Unlock a spinlock before calling into the controller driver.

PATCH In review Bryan Freed 5 participants 7 messages Jun 22, 2012 – Apr 1, 2013 SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: Unlock a spinlock before calling into the controller driver.

spi_pump_messages() calls into a controller driver with unprepare_transfer_hardware() which is documented as "This may sleep". As in the prepare_transfer_hardware() call below, we should release the queue_lock spinlock before making the call.
Rework the logic a bit to hold queue_lock to protect the 'busy' flag, then release it to call unprepare_transfer_hardware().

---
 drivers/spi/spi.c |   15 +++++++--------
 1 files changed, 7 insertions(+), 8 deletions(-)
drivers/spi/spi.c +7 -8
index fc0da39..f7f9df9 100644
@@ -532,17 +532,16 @@ static void spi_pump_messages(struct kthread_work *work)
 	/* Lock queue and check for queue work */
 	spin_lock_irqsave(&master->queue_lock, flags);
 	if (list_empty(&master->queue) || !master->running) {
-		if (master->busy && master->unprepare_transfer_hardware) {
-			ret = master->unprepare_transfer_hardware(master);
-			if (ret) {
-				spin_unlock_irqrestore(&master->queue_lock, flags);
-				dev_err(&master->dev,
-					"failed to unprepare transfer hardware\n");
-				return;
-			}
+		if (!master->busy) {
+			spin_unlock_irqrestore(&master->queue_lock, flags);
+			return;
 		}
 		master->busy = false;
 		spin_unlock_irqrestore(&master->queue_lock, flags);
+		if (master->unprepare_transfer_hardware &&
+		    master->unprepare_transfer_hardware(master))
+			dev_err(&master->dev,
+				"failed to unprepare transfer hardware\n");
 		return;
 	}
 
Signature · 1 line
1.7.7.3
#1 lore raw

Hi,

(Adding Linus Walleij and Mark Brown since they were the ones doing the queue changes).

Quoted 29 lines · Bryan Freed

spi_pump_messages() calls into a controller driver with unprepare_transfer_hardware() which is documented as "This may sleep". As in the prepare_transfer_hardware() call below, we should release the queue_lock spinlock before making the call.
Rework the logic a bit to hold queue_lock to protect the 'busy' flag, then release it to call unprepare_transfer_hardware().

---
 drivers/spi/spi.c |   15 +++++++--------

 1 files changed, 7 insertions(+), 8 deletions(-)
drivers/spi/spi.c +0 -8
index fc0da39..f7f9df9 100644
@@ -532,17 +532,16 @@ static void spi_pump_messages(struct kthread_work *work)
        /* Lock queue and check for queue work */
        spin_lock_irqsave(&master->queue_lock, flags);
        if (list_empty(&master->queue) || !master->running) {
-               if (master->busy && master->unprepare_transfer_hardware) {
-                       ret = master->unprepare_transfer_hardware(master);
-                       if (ret) {
-                               spin_unlock_irqrestore(&master->queue_lock, flags);
-                               dev_err(&master->dev,
-                                       "failed to unprepare transfer hardware\n");
-                               return;
-                       }

drivers/spi/spi.c static void spi_pump_messages(struct kthread_work *work) · line 544

Quoted 10 lines
drivers/spi/spi.c +7 -0
+               if (!master->busy) {
+                       spin_unlock_irqrestore(&master->queue_lock, flags);
+                       return;
                }
                master->busy = false;
                spin_unlock_irqrestore(&master->queue_lock, flags);
+               if (master->unprepare_transfer_hardware &&
+                   master->unprepare_transfer_hardware(master))
+                       dev_err(&master->dev,
+                               "failed to unprepare transfer hardware\n");

I'm not sure this is safe to do. The lock is dropped for the prepare side, but in that case we can be sure that the above code can't come in and unprepare at the same time since there is per definition a request on the queue at that time.

On the other hand, between the lock drop and the call to unprepare above, another code path can come in and queue up a request and either not do prepare properly, or there will be a prepare that races with the unprepare.

It might make more sense to use a workqueue here and schedule a unprepare call that way instead (and cancelling appropriately before the prepare call if needed). I'm open for other suggestions as well.

-Olof

#2 lore raw

Olof,

Quoted 4 lines · Olof Johansson

I'm not sure this is safe to do. The lock is dropped for the prepare side, but in that case we can be sure that the above code can't come in and unprepare at the same time since there is per definition a request on the queue at that time.

I spent a bunch of time staring at the code and I'm pretty sure that there's no race and that the code is safe (if not super obvious).

Specifically there should be only one instance of spi_pump_messages() running at a time per master.  That's because it's a kthread work function.  ...so we can't possibly get a prepare in the middle of the unprepare when prepare is called because the only caller to prepare/unprepare is spi_pump_messages().

I can't comment on whether it's better to do something like add a workqueue (which might be more obvious / less fragile) or just to add a comment.  I will let others comment on that.  :)

-Doug

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

#3 lore raw
Quoted 5 lines · Doug Anderson

Specifically there should be only one instance of spi_pump_messages() running at a time per master.  That's because it's a kthread work function.  ...so we can't possibly get a prepare in the middle of the unprepare when prepare is called because the only caller to prepare/unprepare is spi_pump_messages().

Yes that's how the message pump is designed.

Quoted 3 lines

I can't comment on whether it's better to do something like add a workqueue (which might be more obvious / less fragile) or just to add a comment.  I will let others comment on that.  :)

The message pump initially used a workqueue, but was converted to a kthread because we needed to push the queue to run as realtime for some important low-latency workloads across SPI. The code is basically a tweaked workqueue if you dive down in the implementation.

Yours,
Linus Walleij

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

Acked-by on v1 1/1
#4 lore raw
In reply to Bryan Freed
Quoted 8 lines · Bryan Freed

spi_pump_messages() calls into a controller driver with unprepare_transfer_hardware() which is documented as "This may sleep". As in the prepare_transfer_hardware() call below, we should release the queue_lock spinlock before making the call.
Rework the logic a bit to hold queue_lock to protect the 'busy' flag, then release it to call unprepare_transfer_hardware().

Yes, this looks correct to me! Good catch.

Yours,
Linus Walleij

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/

#5 lore raw

PATCH 1/1 spi: Unlock a spinlock before calling into the controller driver.

In reply to Bryan Freed

From: Bryan Freed <[email protected]>

spi_pump_messages() calls into a controller driver with unprepare_transfer_hardware() which is documented as "This may sleep". As in the prepare_transfer_hardware() call below, we should release the queue_lock spinlock before making the call.
Rework the logic a bit to hold queue_lock to protect the 'busy' flag, then release it to call unprepare_transfer_hardware().

---
During a rebase we noticed that this old patch never actually landed anywhere. I haven't gone through an reproduced the original bug on ToT but I believe that this patch is still required. Perhaps it could land somewhere? ;) Thanks!

 drivers/spi/spi.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/spi/spi.c +7 -8
index f996c60..5b96250 100644
@@ -543,17 +543,16 @@ static void spi_pump_messages(struct kthread_work *work)
 	/* Lock queue and check for queue work */
 	spin_lock_irqsave(&master->queue_lock, flags);
 	if (list_empty(&master->queue) || !master->running) {
-		if (master->busy && master->unprepare_transfer_hardware) {
-			ret = master->unprepare_transfer_hardware(master);
-			if (ret) {
-				spin_unlock_irqrestore(&master->queue_lock, flags);
-				dev_err(&master->dev,
-					"failed to unprepare transfer hardware\n");
-				return;
-			}
+		if (!master->busy) {
+			spin_unlock_irqrestore(&master->queue_lock, flags);
+			return;
 		}
 		master->busy = false;
 		spin_unlock_irqrestore(&master->queue_lock, flags);
+		if (master->unprepare_transfer_hardware &&
+		    master->unprepare_transfer_hardware(master))
+			dev_err(&master->dev,
+				"failed to unprepare transfer hardware\n");
 		return;
 	}
 
Signature · 1 line
1.8.1.3
Applied
#6 lore raw
Quoted 8 lines · Doug Anderson

From: Bryan Freed <[email protected]>

spi_pump_messages() calls into a controller driver with unprepare_transfer_hardware() which is documented as "This may sleep". As in the prepare_transfer_hardware() call below, we should release the queue_lock spinlock before making the call.
Rework the logic a bit to hold queue_lock to protect the 'busy' flag, then release it to call unprepare_transfer_hardware().

Applied, thanks. However...

drivers/spi/spi.c static void spi_pump_messages(struct kthread_work *work) · line 555

Quoted 5 lines
drivers/spi/spi.c +4 -0
 		spin_unlock_irqrestore(&master->queue_lock, flags);
+		if (master->unprepare_transfer_hardware &&
+		    master->unprepare_transfer_hardware(master))
+			dev_err(&master->dev,
+				"failed to unprepare transfer hardware\n");

...it'd be nicer to pay attention to and log the error code if we fail to unprepare.