spi: Refactor spi_stop_queue()
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Fri, 10 May 2024 20:49:45 +0000 (23:49 +0300)
committerMark Brown <broonie@kernel.org>
Mon, 27 May 2024 00:33:42 +0000 (01:33 +0100)
The refactoring makes code less verbose and easier to read.
Besides that the binary size is also reduced, which sounds
like a win-win case:

  add/remove: 0/1 grow/shrink: 2/2 up/down: 210/-226 (-16)
  Function                            old     new   delta
  spi_destroy_queue                    42     156    +114
  spi_controller_suspend              101     197     +96
  spi_unregister_controller           346     319     -27
  spi_register_controller            1834    1794     -40
  spi_stop_queue                      159       -    -159
  Total: Before=49230, After=49214, chg -0.03%

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://msgid.link/r/20240510204945.2581944-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/spi/spi.c

index 26ef4e147681e460928ece53197704d392e6aa58..cc8bb7d5ba1a861a72f3b60f7f72478cbe064565 100644 (file)
@@ -2219,11 +2219,8 @@ static int spi_start_queue(struct spi_controller *ctlr)
 
 static int spi_stop_queue(struct spi_controller *ctlr)
 {
+       unsigned int limit = 500;
        unsigned long flags;
-       unsigned limit = 500;
-       int ret = 0;
-
-       spin_lock_irqsave(&ctlr->queue_lock, flags);
 
        /*
         * This is a bit lame, but is optimized for the common execution path.
@@ -2231,20 +2228,18 @@ static int spi_stop_queue(struct spi_controller *ctlr)
         * execution path (pump_messages) would be required to call wake_up or
         * friends on every SPI message. Do this instead.
         */
-       while ((!list_empty(&ctlr->queue) || ctlr->busy) && limit--) {
+       do {
+               spin_lock_irqsave(&ctlr->queue_lock, flags);
+               if (list_empty(&ctlr->queue) && !ctlr->busy) {
+                       ctlr->running = false;
+                       spin_unlock_irqrestore(&ctlr->queue_lock, flags);
+                       return 0;
+               }
                spin_unlock_irqrestore(&ctlr->queue_lock, flags);
                usleep_range(10000, 11000);
-               spin_lock_irqsave(&ctlr->queue_lock, flags);
-       }
-
-       if (!list_empty(&ctlr->queue) || ctlr->busy)
-               ret = -EBUSY;
-       else
-               ctlr->running = false;
+       } while (--limit);
 
-       spin_unlock_irqrestore(&ctlr->queue_lock, flags);
-
-       return ret;
+       return -EBUSY;
 }
 
 static int spi_destroy_queue(struct spi_controller *ctlr)