1. 09 4月, 2021 1 次提交
  2. 08 4月, 2021 1 次提交
    • W
      spi: Fix use-after-free with devm_spi_alloc_* · 794aaf01
      William A. Kennington III 提交于
      We can't rely on the contents of the devres list during
      spi_unregister_controller(), as the list is already torn down at the
      time we perform devres_find() for devm_spi_release_controller. This
      causes devices registered with devm_spi_alloc_{master,slave}() to be
      mistakenly identified as legacy, non-devm managed devices and have their
      reference counters decremented below 0.
      
      ------------[ cut here ]------------
      WARNING: CPU: 1 PID: 660 at lib/refcount.c:28 refcount_warn_saturate+0x108/0x174
      [<b0396f04>] (refcount_warn_saturate) from [<b03c56a4>] (kobject_put+0x90/0x98)
      [<b03c5614>] (kobject_put) from [<b0447b4c>] (put_device+0x20/0x24)
       r4:b6700140
      [<b0447b2c>] (put_device) from [<b07515e8>] (devm_spi_release_controller+0x3c/0x40)
      [<b07515ac>] (devm_spi_release_controller) from [<b045343c>] (release_nodes+0x84/0xc4)
       r5:b6700180 r4:b6700100
      [<b04533b8>] (release_nodes) from [<b0454160>] (devres_release_all+0x5c/0x60)
       r8:b1638c54 r7:b117ad94 r6:b1638c10 r5:b117ad94 r4:b163dc10
      [<b0454104>] (devres_release_all) from [<b044e41c>] (__device_release_driver+0x144/0x1ec)
       r5:b117ad94 r4:b163dc10
      [<b044e2d8>] (__device_release_driver) from [<b044f70c>] (device_driver_detach+0x84/0xa0)
       r9:00000000 r8:00000000 r7:b117ad94 r6:b163dc54 r5:b1638c10 r4:b163dc10
      [<b044f688>] (device_driver_detach) from [<b044d274>] (unbind_store+0xe4/0xf8)
      
      Instead, determine the devm allocation state as a flag on the
      controller which is guaranteed to be stable during cleanup.
      
      Fixes: 5e844cc3 ("spi: Introduce device-managed SPI controller allocation")
      Signed-off-by: NWilliam A. Kennington III <wak@google.com>
      Link: https://lore.kernel.org/r/20210407095527.2771582-1-wak@google.comSigned-off-by: NMark Brown <broonie@kernel.org>
      794aaf01
  3. 17 3月, 2021 1 次提交
  4. 16 3月, 2021 2 次提交
  5. 12 3月, 2021 1 次提交
  6. 12 2月, 2021 1 次提交
  7. 08 2月, 2021 2 次提交
  8. 28 1月, 2021 1 次提交
  9. 04 1月, 2021 1 次提交
  10. 30 12月, 2020 1 次提交
  11. 28 12月, 2020 1 次提交
  12. 11 12月, 2020 1 次提交
  13. 24 11月, 2020 1 次提交
  14. 21 11月, 2020 1 次提交
    • S
      spi: Take the SPI IO-mutex in the spi_setup() method · 4fae3a58
      Serge Semin 提交于
      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: NSerge Semin <Sergey.Semin@baikalelectronics.ru>
      Link: https://lore.kernel.org/r/20201117094517.5654-1-Sergey.Semin@baikalelectronics.ruSigned-off-by: NMark Brown <broonie@kernel.org>
      4fae3a58
  15. 20 11月, 2020 3 次提交
  16. 12 11月, 2020 1 次提交
    • L
      spi: Introduce device-managed SPI controller allocation · 5e844cc3
      Lukas Wunner 提交于
      SPI driver probing currently comprises two steps, whereas removal
      comprises only one step:
      
          spi_alloc_master()
          spi_register_controller()
      
          spi_unregister_controller()
      
      That's because spi_unregister_controller() calls device_unregister()
      instead of device_del(), thereby releasing the reference on the
      spi_controller which was obtained by spi_alloc_master().
      
      An SPI driver's private data is contained in the same memory allocation
      as the spi_controller struct.  Thus, once spi_unregister_controller()
      has been called, the private data is inaccessible.  But some drivers
      need to access it after spi_unregister_controller() to perform further
      teardown steps.
      
      Introduce devm_spi_alloc_master() and devm_spi_alloc_slave(), which
      release a reference on the spi_controller struct only after the driver
      has unbound, thereby keeping the memory allocation accessible.  Change
      spi_unregister_controller() to not release a reference if the
      spi_controller was allocated by one of these new devm functions.
      
      The present commit is small enough to be backportable to stable.
      It allows fixing drivers which use the private data in their ->remove()
      hook after it's been freed.  It also allows fixing drivers which neglect
      to release a reference on the spi_controller in the probe error path.
      
      Long-term, most SPI drivers shall be moved over to the devm functions
      introduced herein.  The few that can't shall be changed in a treewide
      commit to explicitly release the last reference on the controller.
      That commit shall amend spi_unregister_controller() to no longer release
      a reference, thereby completing the migration.
      
      As a result, the behaviour will be less surprising and more consistent
      with subsystems such as IIO, which also includes the private data in the
      allocation of the generic iio_dev struct, but calls device_del() in
      iio_device_unregister().
      Signed-off-by: NLukas Wunner <lukas@wunner.de>
      Link: https://lore.kernel.org/r/272bae2ef08abd21388c98e23729886663d19192.1605121038.git.lukas@wunner.deSigned-off-by: NMark Brown <broonie@kernel.org>
      5e844cc3
  17. 11 11月, 2020 1 次提交
    • S
      spi: fix client driver breakages when using GPIO descriptors · 766c6b63
      Sven Van Asbroeck 提交于
      Commit f3186dd8 ("spi: Optionally use GPIO descriptors for CS GPIOs")
      introduced the optional use of GPIO descriptors for chip selects.
      
      A side-effect of this change: when a SPI bus uses GPIO descriptors,
      all its client devices have SPI_CS_HIGH set in spi->mode. This flag is
      required for the SPI bus to operate correctly.
      
      This unfortunately breaks many client drivers, which use the following
      pattern to configure their underlying SPI bus:
      
      static int client_device_probe(struct spi_device *spi)
      {
      	...
      	spi->mode = SPI_MODE_0;
      	spi->bits_per_word = 8;
      	err = spi_setup(spi);
      	..
      }
      
      In short, many client drivers overwrite the SPI_CS_HIGH bit in
      spi->mode, and break the underlying SPI bus driver.
      
      This is especially true for Freescale/NXP imx ecspi, where large
      numbers of spi client drivers now no longer work.
      
      Proposed fix:
      -------------
      When using gpio descriptors, depend on gpiolib to handle CS polarity.
      Existing quirks in gpiolib ensure that this is handled correctly.
      
      Existing gpiolib behaviour will force the polarity of any chip-select
      gpiod to active-high (if 'spi-active-high' devicetree prop present) or
      active-low (if 'spi-active-high' absent). Irrespective of whether
      the gpio is marked GPIO_ACTIVE_[HIGH|LOW] in the devicetree.
      
      Loose ends:
      -----------
      If this fix is applied:
      - is commit 138c9c32
        ("spi: spidev: Fix CS polarity if GPIO descriptors are used")
        still necessary / correct ?
      
      Fixes: f3186dd8 ("spi: Optionally use GPIO descriptors for CS GPIOs")
      Signed-off-by: NSven Van Asbroeck <thesven73@gmail.com>
      Acked-by: NLinus Walleij <linus.walleij@linaro.org>
      Link: https://lore.kernel.org/r/20201106150706.29089-1-TheSven73@gmail.comSigned-off-by: NMark Brown <broonie@kernel.org>
      766c6b63
  18. 29 10月, 2020 1 次提交
  19. 09 9月, 2020 1 次提交
    • G
      spi: Fix memory leak on splited transfers · b59a7ca1
      Gustav Wiklander 提交于
      In the prepare_message callback the bus driver has the
      opportunity to split a transfer into smaller chunks.
      spi_map_msg is done after prepare_message.
      
      Function spi_res_release releases the splited transfers
      in the message. Therefore spi_res_release should be called
      after spi_map_msg.
      
      The previous try at this was commit c9ba7a16
      which released the splited transfers after
      spi_finalize_current_message had been called.
      This introduced a race since the message struct could be
      out of scope because the spi_sync call got completed.
      
      Fixes this leak on spi bus driver spi-bcm2835.c when transfer
      size is greater than 65532:
      
      Kmemleak:
      sg_alloc_table+0x28/0xc8
      spi_map_buf+0xa4/0x300
      __spi_pump_messages+0x370/0x748
      __spi_sync+0x1d4/0x270
      spi_sync+0x34/0x58
      spi_test_execute_msg+0x60/0x340 [spi_loopback_test]
      spi_test_run_iter+0x548/0x578 [spi_loopback_test]
      spi_test_run_test+0x94/0x140 [spi_loopback_test]
      spi_test_run_tests+0x150/0x180 [spi_loopback_test]
      spi_loopback_test_probe+0x50/0xd0 [spi_loopback_test]
      spi_drv_probe+0x84/0xe0
      Signed-off-by: NGustav Wiklander <gustavwi@axis.com>
      Link: https://lore.kernel.org/r/20200908151129.15915-1-gustav.wiklander@axis.comSigned-off-by: NMark Brown <broonie@kernel.org>
      b59a7ca1
  20. 03 8月, 2020 1 次提交
    • L
      spi: Prevent adding devices below an unregistering controller · ddf75be4
      Lukas Wunner 提交于
      CONFIG_OF_DYNAMIC and CONFIG_ACPI allow adding SPI devices at runtime
      using a DeviceTree overlay or DSDT patch.  CONFIG_SPI_SLAVE allows the
      same via sysfs.
      
      But there are no precautions to prevent adding a device below a
      controller that's being removed.  Such a device is unusable and may not
      even be able to unbind cleanly as it becomes inaccessible once the
      controller has been torn down.  E.g. it is then impossible to quiesce
      the device's interrupt.
      
      of_spi_notify() and acpi_spi_notify() do hold a ref on the controller,
      but otherwise run lockless against spi_unregister_controller().
      
      Fix by holding the spi_add_lock in spi_unregister_controller() and
      bailing out of spi_add_device() if the controller has been unregistered
      concurrently.
      
      Fixes: ce79d54a ("spi/of: Add OF notifier handler")
      Signed-off-by: NLukas Wunner <lukas@wunner.de>
      Cc: stable@vger.kernel.org # v3.19+
      Cc: Geert Uytterhoeven <geert+renesas@glider.be>
      Cc: Octavian Purdila <octavian.purdila@intel.com>
      Cc: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
      Link: https://lore.kernel.org/r/a8c3205088a969dc8410eec1eba9aface60f36af.1596451035.git.lukas@wunner.deSigned-off-by: NMark Brown <broonie@kernel.org>
      ddf75be4
  21. 17 7月, 2020 1 次提交
  22. 10 7月, 2020 1 次提交
  23. 02 7月, 2020 1 次提交
    • D
      spi: Avoid setting the chip select if we don't need to · d40f0b6f
      Douglas Anderson 提交于
      On some SPI controllers (like spi-geni-qcom) setting the chip select
      is a heavy operation.  For instance on spi-geni-qcom, with the current
      code, is was measured as taking upwards of 20 us.  Even on SPI
      controllers that aren't as heavy, setting the chip select is at least
      something like a MMIO operation over some peripheral bus which isn't
      as fast as a RAM access.
      
      While it would be good to find ways to mitigate problems like this in
      the drivers for those SPI controllers, it can also be noted that the
      SPI framework could also help out.  Specifically, in some situations,
      we can see the SPI framework calling the driver's set_cs() with the
      same parameter several times in a row.  This is specifically observed
      when looking at the way the Chrome OS EC SPI driver (cros_ec_spi)
      works but other drivers likely trip it to some extent.
      
      Let's solve this by caching the chip select state in the core and only
      calling into the controller if there was a change.  We check not only
      the "enable" state but also the chip select mode (active high or
      active low) since controllers may care about both the mode and the
      enable flag in their callback.
      Signed-off-by: NDouglas Anderson <dianders@chromium.org>
      Link: https://lore.kernel.org/r/20200629164103.1.Ied8e8ad8bbb2df7f947e3bc5ea1c315e041785a2@changeidSigned-off-by: NMark Brown <broonie@kernel.org>
      d40f0b6f
  24. 23 6月, 2020 1 次提交
  25. 15 6月, 2020 1 次提交
  26. 27 5月, 2020 1 次提交
  27. 25 5月, 2020 1 次提交
  28. 23 5月, 2020 1 次提交
  29. 20 5月, 2020 1 次提交
  30. 15 4月, 2020 1 次提交
  31. 14 4月, 2020 1 次提交
  32. 13 3月, 2020 1 次提交
  33. 12 3月, 2020 1 次提交
  34. 05 3月, 2020 1 次提交
    • V
      spi: Do spi_take_timestamp_pre for as many times as necessary · 6a726824
      Vladimir Oltean 提交于
      When dealing with a SPI controller driver that is sending more than 1
      byte at once (or the entire buffer at once), and the SPI peripheral
      driver has requested timestamping for a byte in the middle of the
      buffer, we find that spi_take_timestamp_pre never records a "pre"
      timestamp.
      
      This happens because the function currently expects to be called with
      the "progress" argument >= to what the peripheral has requested to be
      timestamped. But clearly there are cases when that isn't going to fly.
      
      And since we can't change the past when we realize that the opportunity
      to take a "pre" timestamp has just passed and there isn't going to be
      another one, the approach taken is to keep recording the "pre" timestamp
      on each call, overwriting the previously recorded one until the "post"
      timestamp is also taken.
      Signed-off-by: NVladimir Oltean <vladimir.oltean@nxp.com>
      Link: https://lore.kernel.org/r/20200304220044.11193-8-olteanv@gmail.comSigned-off-by: NMark Brown <broonie@kernel.org>
      6a726824
  35. 04 3月, 2020 1 次提交
  36. 29 2月, 2020 1 次提交