1. 07 Dec, 2020 16 commits
  2. 04 Dec, 2020 1 commit
  3. 27 Nov, 2020 2 commits
  4. 25 Nov, 2020 2 commits
  5. 24 Nov, 2020 2 commits
  6. 23 Nov, 2020 2 commits
  7. 20 Nov, 2020 4 commits
    • Serge Semin's avatar
      spi: Take the SPI IO-mutex in the spi_setup() method · 4fae3a58
      Serge Semin authored
      I've discovered that due to the recent commit 49d7d695 ("spi: dw:
      Explicitly de-assert CS on SPI transfer completion") a concurrent usage of
      the spidev devices with different chip-selects causes the "SPI transfer
      timed out" error. The root cause of the problem has turned to be in a race
      condition of the SPI-transfer execution procedure and the spi_setup()
      method being called at the same time. In particular in calling the
      spi_set_cs(false) while there is an SPI-transfer being executed. In my
      case due to the commit cited above all CSs get to be switched off by
      calling the spi_setup() for /dev/spidev0.1 while there is an concurrent
      SPI-transfer execution performed on /dev/spidev0.0. Of course a situation
      of the spi_setup() being called while there is an SPI-transfer being
      executed for two different SPI peripheral devices of the same controller
      may happen not only for the spidev driver, but for instance for MMC SPI +
      some another device, or spi_setup() being called from an SPI-peripheral
      probe method while some other device has already been probed and is being
      used by a corresponding driver...
      
      Of course I could have provided a fix affecting the DW APB SSI driver
      only, for instance, by creating a mutual exclusive access to the set_cs
      callback and setting/clearing only the bit responsible for the
      corresponding chip-select. But after a short research I've discovered that
      the problem most likely affects a lot of the other drivers:
      - drivers/spi/spi-sun4i.c - RMW the chip-select register;
      - drivers/spi/spi-rockchip.c - RMW the chip-select register;
      - drivers/spi/spi-qup.c - RMW a generic force-CS flag in a CSR.
      - drivers/spi/spi-sifive.c - set a generic CS-mode flag in a CSR.
      - drivers/spi/spi-bcm63xx-hsspi.c - uses an internal mutex to serialize
        the bus config changes, but still isn't protected from the race
        condition described above;
      - drivers/spi/spi-geni-qcom.c - RMW a chip-select internal flag and set the
        CS state in HW;
      - drivers/spi/spi-orion.c - RMW a chip-select register;
      - drivers/spi/spi-cadence.c - RMW a chip-select register;
      - drivers/spi/spi-armada-3700.c - RMW a chip-select register;
      - drivers/spi/spi-lantiq-ssc.c - overwrites the chip-select register;
      - drivers/spi/spi-sun6i.c - RMW a chip-select register;
      - drivers/spi/spi-synquacer.c - RMW a chip-select register;
      - drivers/spi/spi-altera.c - directly sets the chip-select state;
      - drivers/spi/spi-omap2-mcspi.c - RMW an internally cached CS state and
        writes it to HW;
      - drivers/spi/spi-mt65xx.c - RMW some CSR;
      - drivers/spi/spi-jcore.c - directly sets the chip-selects state;
      - drivers/spi/spi-mt7621.c - RMW a chip-select register;
      
      I could have missed some drivers, but a scale of the problem is obvious.
      As you can see most of the drivers perform an unprotected
      Read-modify-write chip-select register modification in the set_cs callback.
      Seeing the spi_setup() function is calling the spi_set_cs() and it can be
      executed concurrently with SPI-transfers exec procedure, which also calls
      spi_set_cs() in the SPI core spi_transfer_one_message() method, the race
      condition of the register modification turns to be obvious.
      
      To sum up the problem denoted above affects each driver for a controller
      having more than one chip-select lane and which:
      1) performs the RMW to some CS-related register with no serialization;
      2) directly disables any CS on spi_set_cs(dev, false).
      * the later is the case of the DW APB SSI driver.
      
      The controllers which equipped with a single CS theoretically can also
      experience the problem, but in practice will not since normally the
      spi_setup() isn't called concurrently with the SPI-transfers executed on
      the same SPI peripheral device.
      
      In order to generically fix the denoted bug I'd suggest to serialize an
      access to the controller IO by taking the IO mutex in the spi_setup()
      callback. The mutex is held while there is an SPI communication going on
      on the SPI-bus of the corresponding SPI-controller. So calling the
      spi_setup() method and disabling/updating the CS state within it would be
      safe while there is no any SPI-transfers being executed. Also note I
      suppose it would be safer to protect the spi_controller->setup() callback
      invocation too, seeing some of the SPI-controller drivers update a HW
      state in there.
      
      Fixes: 49d7d695 ("spi: dw: Explicitly de-assert CS on SPI transfer completion")
      Signed-off-by: default avatarSerge Semin <Sergey.Semin@baikalelectronics.ru>
      Link: https://lore.kernel.org/r/20201117094517.5654-1-Sergey.Semin@baikalelectronics.ruSigned-off-by: default avatarMark Brown <broonie@kernel.org>
      4fae3a58
    • Uwe Kleine-König's avatar
      spi: Warn when a driver's remove callback returns an error · 7795d475
      Uwe Kleine-König authored
      The driver core ignores the return value of struct device_driver::remove
      (because in general there is nothing that can be done about that). So
      add a warning when an spi driver returns an error.
      
      This simplifies the quest to make struct device_driver::remove return void.
      A consequent change would be to make struct spi_driver::remove return void,
      but I'm keeping this quest for later (or someone else).
      Signed-off-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
      Link: https://lore.kernel.org/r/20201119161604.2633521-3-u.kleine-koenig@pengutronix.deSigned-off-by: default avatarMark Brown <broonie@kernel.org>
      7795d475
    • Uwe Kleine-König's avatar
      spi: Use bus_type functions for probe, remove and shutdown · 9db34ee6
      Uwe Kleine-König authored
      The eventual goal is to get rid of the callbacks in struct
      device_driver. Other than not using driver callbacks there should be no
      side effect of this patch.
      Signed-off-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
      Link: https://lore.kernel.org/r/20201119161604.2633521-2-u.kleine-koenig@pengutronix.deSigned-off-by: default avatarMark Brown <broonie@kernel.org>
      9db34ee6
    • Uwe Kleine-König's avatar
      spi: fix resource leak for drivers without .remove callback · 440408db
      Uwe Kleine-König authored
      Consider an spi driver with a .probe but without a .remove callback (e.g.
      rtc-ds1347). The function spi_drv_probe() is called to bind a device and
      so dev_pm_domain_attach() is called. As there is no remove callback
      spi_drv_remove() isn't called at unbind time however and so calling
      dev_pm_domain_detach() is missed and the pm domain keeps active.
      
      To fix this always use both spi_drv_probe() and spi_drv_remove() and
      make them handle the respective callback not being set. This has the
      side effect that for a (hypothetical) driver that has neither .probe nor
      remove the clk and pm domain setup is done.
      
      Fixes: 33cf00e5 ("spi: attach/detach SPI device to the ACPI power domain")
      Signed-off-by: default avatarUwe Kleine-König <u.kleine-koenig@pengutronix.de>
      Link: https://lore.kernel.org/r/20201119161604.2633521-1-u.kleine-koenig@pengutronix.deSigned-off-by: default avatarMark Brown <broonie@kernel.org>
      440408db
  8. 18 Nov, 2020 1 commit
  9. 17 Nov, 2020 3 commits
  10. 16 Nov, 2020 4 commits
  11. 13 Nov, 2020 1 commit
  12. 12 Nov, 2020 2 commits
    • Mark Brown's avatar
      Merge series "Use-after-free be gone" from Lukas Wunner <lukas@wunner.de>: · c371dcf5
      Mark Brown authored
      Here's my proposal to fix the use-after-free bugs reported by
      Sascha Hauer and Florian Fainelli:
      
      I scrutinized all SPI drivers in the v5.10 tree:
      
      * There are 9 drivers with a use-after-free in the ->remove() hook
        caused by accessing driver private data after spi_unregister_controller().
      
      * There are 8 drivers which leak the spi_controller in the ->probe()
        error path because of a missing spi_controller_put().
      
      I'm introducing devm_spi_alloc_master/slave() which automatically
      calls spi_controller_put() on ->remove().  This fixes both classes
      of bugs while at the same time reducing code amount and complexity
      in the ->probe() hook.
      
      I propose that spi_controller_unregister() should no longer release
      a reference on the spi_controller.  Instead, drivers need to either
      do it themselves or use one of the devm functions introduced herein.
      The vast majority of drivers can be converted to the devm functions.
      See the commit message of patch [1/4] for the rationale and details.
      
      Enclosed are patches for 3 Broadcom drivers.
      Patches for the other drivers are on this branch:
      https://github.com/l1k/linux/commits/spi_fixes
      
      @Florian Fainelli:  Could you verify that there are no KASAN splats or
      leaks with these patches?  Unfortunately I do not have any SPI-capable
      hardware at my disposal right now, so can only compile-test.  You may
      want to augment spi_controller_release() with a printk() to log when
      the spi_controller is freed.
      
      @Mark Brown:  Patches [2/4] to [4/4] reference the SHA-1 of patch [1/4]
      in their stable tags.  Because the hash is unknown to me until you apply
      the patch, I've used "123456789abc" as a placeholder.  You'll have to
      replace the hash if/when applying.  Alternatively, only apply patch [1/4]
      and I'll repost the other patches with the hash fixed up.
      
      Thanks!
      
      Lukas Wunner (4):
        spi: Introduce device-managed SPI controller allocation
        spi: bcm2835: Fix use-after-free on unbind
        spi: bcm2835aux: Fix use-after-free on unbind
        spi: bcm-qspi: Fix use-after-free on unbind
      
       drivers/spi/spi-bcm-qspi.c   | 34 ++++++++-------------
       drivers/spi/spi-bcm2835.c    | 24 +++++----------
       drivers/spi/spi-bcm2835aux.c | 21 +++++--------
       drivers/spi/spi.c            | 58 +++++++++++++++++++++++++++++++++++-
       include/linux/spi/spi.h      | 19 ++++++++++++
       5 files changed, 103 insertions(+), 53 deletions(-)
      
      --
      2.28.0
      c371dcf5
    • Lukas Wunner's avatar
      spi: lpspi: Fix use-after-free on unbind · 4def49da
      Lukas Wunner authored
      Normally the last reference on an spi_controller is released by
      spi_unregister_controller().  In the case of the i.MX lpspi driver,
      the spi_controller is registered with devm_spi_register_controller(),
      so spi_unregister_controller() is invoked automatically after the driver
      has unbound.
      
      However the driver already releases the last reference in
      fsl_lpspi_remove() through a gratuitous call to spi_master_put(),
      causing a use-after-free when spi_unregister_controller() is
      subsequently invoked by the devres framework.
      
      Fix by dropping the superfluous spi_master_put().
      
      Fixes: 944c01a8 ("spi: lpspi: enable runtime pm for lpspi")
      Signed-off-by: default avatarLukas Wunner <lukas@wunner.de>
      Cc: <stable@vger.kernel.org> # v5.2+
      Cc: Han Xu <han.xu@nxp.com>
      Link: https://lore.kernel.org/r/ab3c0b18bd820501a12c85e440006e09ec0e275f.1604874488.git.lukas@wunner.deSigned-off-by: default avatarMark Brown <broonie@kernel.org>
      4def49da