Linux ATA/IDE development
 help / color / mirror / Atom feed
* [PATCH v5 0/6] ata: Do not release the host resources twice on probe() failure
@ 2026-09-15  8:41 Niklas Cassel
  2026-09-15  8:41 ` [PATCH v5 1/6] ata: ahci_st: Do not ignore errors when getting the reset controls Niklas Cassel
                   ` (6 more replies)
  0 siblings, 7 replies; 12+ messages in thread
From: Niklas Cassel @ 2026-09-15  8:41 UTC (permalink / raw)
  To: Patrice Chotard, Damien Le Moal, Niklas Cassel, Hans de Goede,
	Philipp Zabel, Alexandre Torgue, Lee Jones, Tejun Heo,
	Sergei Shtylyov, Baokun Li, Dan Williams, Brian Norris,
	Jeff Garzik
  Cc: Damien Le Moal, linux-arm-kernel, linux-ide

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(-)

-- 
2.55.0


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-09-15  9:59 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-15  8:41 [PATCH v5 0/6] ata: Do not release the host resources twice on probe() failure Niklas Cassel
2026-09-15  8:41 ` [PATCH v5 1/6] ata: ahci_st: Do not ignore errors when getting the reset controls Niklas Cassel
2026-09-15  8:48   ` Damien Le Moal
2026-09-15  8:41 ` [PATCH v5 2/6] ata: ahci_st: Assert the power down reset in the probe() error path Niklas Cassel
2026-09-15  8:41 ` [PATCH v5 3/6] ata: sata_fsl: Fix use-after-free of host_priv on probe() failure Niklas Cassel
2026-09-15  8:50   ` Damien Le Moal
2026-09-15  9:49     ` Niklas Cassel
2026-09-15  8:41 ` [PATCH v5 4/6] ata: libahci: Free the IRQs when activating a multi-IRQ host fails Niklas Cassel
2026-09-15  8:41 ` [PATCH v5 5/6] ata: libata: Do not leave ata_host_stop() registered when activation fails Niklas Cassel
2026-09-15  8:52   ` Damien Le Moal
2026-09-15  8:41 ` [PATCH v5 6/6] ata: libata-core: Fix the ata_host_register() kdoc Niklas Cassel
2026-09-15  9:58 ` [PATCH v5 0/6] ata: Do not release the host resources twice on probe() failure Niklas Cassel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox