From: Niklas Cassel <cassel@kernel.org>
To: Patrice Chotard <patrice.chotard@foss.st.com>,
Damien Le Moal <dlemoal@kernel.org>,
Niklas Cassel <cassel@kernel.org>,
Hans de Goede <hansg@kernel.org>,
Philipp Zabel <p.zabel@pengutronix.de>,
Alexandre Torgue <alexandre.torgue@st.com>,
Lee Jones <lee@kernel.org>, Tejun Heo <tj@kernel.org>,
Sergei Shtylyov <sergei.shtylyov@gmail.com>,
Baokun Li <libaokun1@huawei.com>, Dan Williams <djbw@kernel.org>,
Brian Norris <computersforpeace@gmail.com>,
Jeff Garzik <jgarzik@redhat.com>
Cc: Damien Le Moal <damien.lemoal@opensource.wdc.com>,
linux-arm-kernel@lists.infradead.org, linux-ide@vger.kernel.org
Subject: [PATCH v5 0/6] ata: Do not release the host resources twice on probe() failure
Date: Tue, 15 Sep 2026 10:41:26 +0200 [thread overview]
Message-ID: <20260915084127.692494-8-cassel@kernel.org> (raw)
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
next reply other threads:[~2026-09-15 8:42 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-15 8:41 Niklas Cassel [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260915084127.692494-8-cassel@kernel.org \
--to=cassel@kernel.org \
--cc=alexandre.torgue@st.com \
--cc=computersforpeace@gmail.com \
--cc=damien.lemoal@opensource.wdc.com \
--cc=djbw@kernel.org \
--cc=dlemoal@kernel.org \
--cc=hansg@kernel.org \
--cc=jgarzik@redhat.com \
--cc=lee@kernel.org \
--cc=libaokun1@huawei.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-ide@vger.kernel.org \
--cc=p.zabel@pengutronix.de \
--cc=patrice.chotard@foss.st.com \
--cc=sergei.shtylyov@gmail.com \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox