[PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume

From: Weidong Wang via B4 Relay

Date: Sat Nov 28 2026 - 22:13:40 EST


From: Weidong Wang <weidongw@xxxxxxxxxxxx>

ata_pci_device_do_suspend() drops the enable reference taken at probe
time with pci_disable_device(), and ata_pci_device_do_resume() takes it
back with pcim_enable_device().

Since commit f748a07a0b64 ("PCI: Remove legacy pcim_release()"),
pcim_enable_device() adds a new pcim_disable_device() devres action on
every call instead of reusing a single devres entry. The action
registered at probe time is not removed by the pci_disable_device() on
suspend, so each suspend/resume cycle leaves one more disable action on
the device while the enable count stays at one.

When the driver is then detached (unbind, module unload, hot-unplug),
every action calls pci_disable_device(). The first one drops the enable
count to zero; each further one triggers

WARNING: disabling already-disabled device

in pci_disable_device() and makes the enable count negative. With a
negative count, a later bind of the device no longer disables it on
detach, as pci_disable_device() only does so when the count reaches
exactly zero.

Use pci_enable_device() in ata_pci_device_do_resume(). It balances the
pci_disable_device() of ata_pci_device_do_suspend() and leaves the
probe time devres action as the only one.

pata_cs5520 is the one user of ata_pci_device_do_resume() whose suspend
callback does not disable the device. It was only balanced because
every resume added both an enable reference and a disable action. Open
code its resume without the enable call so that its enable count does
not grow with the change above. As the count was already above zero
there, the enable call did not touch the hardware, so this does not
change what is done to the device.

This does not cover a detach while the device is still disabled, that
is after a failed ata_pci_device_do_resume() or a removal before the
resume callback ran. The probe time action then still calls
pci_disable_device() with a zero enable count. Before the commit below,
pcim_release() checked pci_is_enabled() for that; pcim_disable_device()
does not, and libata cannot cancel that action. This needs a change in
drivers/pci/devres.c and is left for a separate patch.

Found by code inspection with an automated source scanner. Reproduced
in QEMU with ata_piix: after two suspend/resume cycles with pm_test set
to "devices", unbinding the driver triggers the warning above and
leaves the enable count at -2. With this patch the unbind is clean and
the count is 0. The pata_cs5520 change is compile tested only.

The fix and this changelog were drafted with AI assistance (Claude Code,
with a second review by OpenAI Codex), then reviewed by the submitter.

Fixes: f748a07a0b64 ("PCI: Remove legacy pcim_release()")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Weidong Wang <weidongw@xxxxxxxxxxxx>
---
The case of a detach while the device is still disabled (failed resume)
is left out, see the commit message: it would need a pci_is_enabled()
check in pcim_disable_device(). I can send that as a separate patch if
the PCI side agrees.

pata_mpiix never enables the device at probe but uses these helpers;
that older imbalance is not touched here.

Testing:
- Compile-tested on x86_64 (allmodconfig, W=1):
drivers/ata/libata-core.o drivers/ata/pata_cs5520.o; no new warnings.
- checkpatch.pl --strict: no errors or warnings.
---
drivers/ata/libata-core.c | 8 +++++++-
drivers/ata/pata_cs5520.c | 12 ++++++++----
2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index f41ab71fc9e2..587a393d2753 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -6622,7 +6622,13 @@ int ata_pci_device_do_resume(struct pci_dev *pdev)
pci_set_power_state(pdev, PCI_D0);
pci_restore_state(pdev);

- rc = pcim_enable_device(pdev);
+ /*
+ * Balance the pci_disable_device() done on suspend. The devres action
+ * registered by pcim_enable_device() at probe time is still in place,
+ * so do not use pcim_enable_device() here or one more disable action
+ * would be added on every resume.
+ */
+ rc = pci_enable_device(pdev);
if (rc) {
dev_err(&pdev->dev,
"failed to enable device after resume (%d)\n", rc);
diff --git a/drivers/ata/pata_cs5520.c b/drivers/ata/pata_cs5520.c
index 3163c8d9cef5..4337db7f9359 100644
--- a/drivers/ata/pata_cs5520.c
+++ b/drivers/ata/pata_cs5520.c
@@ -225,11 +225,15 @@ static int cs5520_reinit_one(struct pci_dev *pdev)
{
struct ata_host *host = pci_get_drvdata(pdev);
u8 pcicfg;
- int rc;

- rc = ata_pci_device_do_resume(pdev);
- if (rc)
- return rc;
+ /*
+ * The device was not disabled on suspend, so do not enable it again
+ * as ata_pci_device_do_resume() would: that would leave the enable
+ * count unbalanced.
+ */
+ pci_set_power_state(pdev, PCI_D0);
+ pci_restore_state(pdev);
+ pci_set_master(pdev);

pci_read_config_byte(pdev, 0x60, &pcicfg);
if ((pcicfg & 0x40) == 0)

---
base-commit: 3391ac405dca60fb78c1ae129240848b631b11eb
change-id: 20261128-kbug-l080-76a77362904c

Best regards,
--
Weidong Wang <weidongw@xxxxxxxxxxxx>