Skip to content

ata: Do not release the host resources twice on probe() failure

Niklas Cassel linux-arm-kernel linux-ide stable 3 versions – Applied

Status from this message: Applied to libata/linux.git (for-7.4), thanks!

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest6 patches 5 repliesApplied
  2. v45 patches 6 repliesSuperseded
  3. v33 patches 0 repliesSuperseded

Changes in v5 from the cover letter

- New patch 1, which stops ahci_st from ignoring the errors returned when getting its reset controls, most notably -EPROBE_DEFER. The other patches are renumbered accordingly.
- Patch 3 no longer touches the ata_host_activate() error path, it only fixes the use-after-free which the two device_create_file() calls can trigger, so it no longer depends on the rest of the series. Checking the return value of ata_host_activate(), and releasing hcr_base and host_priv when activating the host fails, is now done by patch 5, where it can be done without leaking them.
- Drop the extern from the ata_host_undo_start() declaration in patch 5.

Patches in v5 Applied

#Subject and filesSizeRepliesApplied
1/6ata: ahci_st: Do not ignore errors when getting the reset controlsdrivers/ata/ahci_st.c+14 -171 reply
2/6ata: ahci_st: Assert the power down reset in the probe() error pathdrivers/ata/ahci_st.c+30 -210 replies
3/6ata: sata_fsl: Fix use-after-free of host_priv on probe() failuredrivers/ata/sata_fsl.c+13 -52 replies
4/6ata: libahci: Free the IRQs when activating a multi-IRQ host failsdrivers/ata/libahci.c+24 -20 replies
5/6ata: libata: Do not leave ata_host_stop() registered when activation failsdrivers/ata/libahci.c drivers/ata/libahci_platform.c drivers/ata/libata-core.c +4 more+91 -101 reply
6/6ata: libata-core: Fix the ata_host_register() kdocdrivers/ata/libata-core.c+3 -30 replies

Reviews trailers per patch

All 6 patches have a Reviewed-by or Acked-by.

PatchDamien Le Moal
1/6R-b
2/6R-b
3/6R-b
4/6R-b
5/6R-b
6/6R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v5 thread

Cover letter [PATCH v5 0/6] ata: Do not release the host resources twice on probe() failure

ata_host_start() registers ata_host_stop() as a devres action as soon as it has succeeded:

	if (have_stop) {
		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
	...
	if (start_dr)
		devres_add(host->dev, start_dr);
	host->flags |= ATA_HOST_STARTED;

From that point on, releasing the host resources is owned by devres: when probe() fails, the driver core calls devres_release_all(), which calls ata_host_stop(), which calls ->port_stop() and ->host_stop().

ata_host_activate() and ahci_host_activate_multi_irqs() can however fail after ata_host_start() has succeeded - devm_kasprintf(), devm_request_irq() (a shared IRQ conflict) and ata_host_register() (scsi_add_host(), ata_tport_add()) can all fail - and they return the error with the devres action still registered. Since the caller cannot tell whether ata_host_start() succeeded, and since it has to release the resources for the failures happening before that, all the ahci-platform drivers release the host resources in their probe() error path, e.g. ahci_probe() calls ahci_platform_disable_resources() while ahci_host_stop() does the same through devres.

The clocks, regulators, resets and PHYs of the host are therefore released twice, which gives refcount underflow warnings from the clk, regulator and phy cores and, for shared resources, can disable resources which are still in use by other devices.

Patch 5 adds ata_host_undo_start(), which stops the ports and drops the devres action without calling ->host_stop(), and calls it from both activation helpers when they fail, so that "on failure, the caller releases what it acquired" holds for all of them. sata_qstor and sata_fsl are the only drivers which implement ->host_stop() while having no error handling at all for the activate host call, so they get some.

Patches 2 to 4 have to come first, as they fix error paths which patch 5 builds upon, and which are worth fixing on their own:

- ahci_st is the only ahci-platform driver whose ->host_stop() does more than its probe() error path, as it also asserts the "pwr-dwn" reset, so patch 5 would stop that reset from being asserted (patch 2).
- sata_fsl releases hcr_base and host_priv in its probe() error path although sata_fsl_host_stop() does that as well, which is a use-after-free that the two device_create_file() calls after ata_host_activate() can trigger today (patch 3).
- ahci_host_activate_multi_irqs() never frees the IRQs which it requested when it fails, so the IRQ handlers stay registered while the caller releases the resources of the host (patch 4).

Patch 1 is an independent ahci_st fix, reported by Sashiko:
st_ahci_probe_resets() treats every error from devm_reset_control_get() as "reset control not defined", including -EPROBE_DEFER, so probe() continues with the SATA IP held in reset and powered down instead of being deferred.

Patch 6 is an unrelated kdoc fix that I noticed while documenting the above.

Changes since v4:
- New patch 1, which stops ahci_st from ignoring the errors returned when getting its reset controls, most notably -EPROBE_DEFER. The other patches are renumbered accordingly.
- Patch 3 no longer touches the ata_host_activate() error path, it only fixes the use-after-free which the two device_create_file() calls can trigger, so it no longer depends on the rest of the series. Checking the return value of ata_host_activate(), and releasing hcr_base and host_priv when activating the host fails, is now done by patch 5, where it can be done without leaking them.
- Drop the extern from the ata_host_undo_start() declaration in patch 5.

Niklas Cassel (6):
ata: ahci_st: Do not ignore errors when getting the reset controls
ata: ahci_st: Assert the power down reset in the probe() error path
ata: sata_fsl: Fix use-after-free of host_priv on probe() failure
ata: libahci: Free the IRQs when activating a multi-IRQ host fails
ata: libata: Do not leave ata_host_stop() registered when activation
fails
ata: libata-core: Fix the ata_host_register() kdoc

 drivers/ata/ahci_st.c          | 78 +++++++++++++++++---------------
 drivers/ata/libahci.c          | 28 +++++++++++-
 drivers/ata/libahci_platform.c |  4 ++
 drivers/ata/libata-core.c      | 81 ++++++++++++++++++++++++++++++----
 drivers/ata/libata-sff.c       |  5 +++
 drivers/ata/sata_fsl.c         | 24 +++++++---
 drivers/ata/sata_qstor.c       |  8 +++-
 include/linux/libata.h         |  1 +
 8 files changed, 173 insertions(+), 56 deletions(-)
Signature · 1 line
2.55.0