ata: libata-core: Do not use pcim_enable_device() on PCI resume
Subsystems (2)
PATCH 1/1 ata: libata-core: Do not use pcim_enable_device() on PCI resume
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.
---
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(-)
drivers/ata/libata-core.c +7 -1
@@ -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);
drivers/ata/pata_cs5520.c +8 -4
@@ -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,
Signature · 1 line
Weidong Wang <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] ata: pata_triflex: unbalanced pci_enable_device() on resume --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] ata: pata_triflex: unbalanced pci_enable_device() on resume --
commit ec840bfcc0d665f55fd42682dab5f06844d5702d
Author: Weidong Wang <[email protected]>
ata: libata-core: Do not use pcim_enable_device() on PCI resume
This commit switches ata_pci_device_do_resume() from pcim_enable_device() to pci_enable_device() so that resume does not register a duplicate devres disable action, and open-codes cs5520_reinit_one() to avoid an enable count imbalance on pata_cs5520.
Quoted 9 lines
drivers/ata/libata-core.c +0
-1@@ -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);
drivers/ata/libata-core.c int ata_pci_device_do_resume(struct pci_dev *pdev) · line 6634
Quoted 10 lines
drivers/ata/libata-core.c +7
-0+/* +* 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);
[Severity: Medium]
Does this leave pdev->enable_cnt unbalanced on pata_triflex after suspend and resume?
Like pata_cs5520, pata_triflex does not disable the PCI device on suspend, but still uses ata_pci_device_resume() -> ata_pci_device_do_resume() for resume:
drivers/ata/pata_triflex.c:triflex_ata_pci_device_suspend() {
...
/*
* We must not disable or powerdown the device.
* APM bios refuses to suspend if IDE is not accessible.
*/
pci_save_state(pdev);
return 0;
}With pci_enable_device() here, each resume of pata_triflex increments pdev->enable_cnt without a matching disable on suspend or devres action on detach, so the single probe-time pcim_disable_device() action no longer brings enable_cnt to zero on driver unbind or module unload and leaves the PCI device enabled.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi, thx for the patch.
Quoted 2 lines · On Sat, 2026-11-28 at 21:11 -0600, Weidong Wang via B4 Relay wrote:
From: Weidong Wang <[email protected]>
[…]
Quoted 3 lines
The fix and this changelog were drafted with AI assistance (Claude Code, with a second review by OpenAI Codex), then reviewed by the submitter.
That's evident from the *very* verbose commit message, which I think should be slimmed down a bit :)
Quoted 9 lines
Fixes f748a07a0b64 ("PCI: Remove legacy pcim_release()")Signed-off-by Weidong Wang <[email protected]>---
The case of a detach while the device is still disabled (failed resume) is left out, see the commit message: it would need apci_is_enabled()check inpcim_disable_device(). I can send that as a separate patch if the PCI side agrees.
That's not a good idea. This intertwining makes the API inconsistent.
Instead, pcim_disable_device() should not exist / should be deprecated. See below.
Quoted 21 lines
pata_mpiixnever enables the device at probe but uses these helpers; that older imbalance is not touched here.Testing:
- Compile-tested onx86_64(allmodconfig, W=1):
drivers/ata/libata-core.odrivers/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(-)drivers/ata/libata-core.c +0
-0@@ -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);
drivers/ata/libata-core.c int ata_pci_device_do_resume(struct pci_dev *pdev) · line 6631
Quoted 8 lines
drivers/ata/libata-core.c +7
-1- 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);
So will pcim_enable_device() still be called somewhere else?
I have thought about the pcim_ API a while ago, and my opinion by now is that drivers must either fully use pcim_, never undoing the actions, or should *only* use the pci_ functions.
IOW, who wants unmanaged behavior should not use the managed functions.
Especially, functions like pcim_disable_device() should not exist IMO. If you want to disable the device at any other point in time than driver unbind, then use pci_enable_device() + pci_disable_device().
drivers/ata/pata_cs5520.c
Quoted 7 lines
if (rc) {
dev_err(&pdev->dev,
"failed to enable device after resume (%d)\n", rc);drivers/ata/pata_cs5520.c +0
-0
ata_pci_device_do_resume() is used in 28 files. Will the parties other than cs5520 be affected by this change? That seems quite likely.
Quoted 7 lines
drivers/ata/pata_cs5520.c +0
-1@@ -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;drivers/ata/pata_cs5520.c +0
-1- rc =ata_pci_device_do_resume (pdev);
drivers/ata/pata_cs5520.c static int cs5520_reinit_one(struct pci_dev *pdev) · line 236
Quoted 10 lines
drivers/ata/pata_cs5520.c +8
-2-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);
That change looks suspicious. I would expect it to be unnecessary if all that were changed in ATA were replacing the pcim_ functions with the pci_ ones.
Regards
P.
Quoted 2 lines · Philipp Stanner
That's evident from the *very* verbose commit message, which I think should be slimmed down a bit :)
Fair point, thanks for the review. v2 will have a much shorter one.
Quoted 4 lines
Quoted 2 lines
it would need a
pci_is_enabled()
check inpcim_disable_device(). I can send that as a separate patchThat's not a good idea. This intertwining makes the API inconsistent.
Understood, I will drop that idea.
Quoted 1 line
So will
pcim_enable_device()still be called somewhere else?
Yes. The probe-time enable stays managed: ata_pci_init_one() in libata-sff.c (used through ata_pci_bmdma_init_one() and ata_pci_sff_init_one()) and the probe functions of the other drivers still call pcim_enable_device(). Only the re-enable on resume changes, so that it balances the pci_disable_device() in ata_pci_device_do_suspend().
Quoted 2 lines
ata_pci_device_do_resume()is used in 28 files. Will the parties other than cs5520 be affected by this change? That seems quite likely.
Most of them suspend through ata_pci_device_suspend(), which disables the device, so the enable on resume matches it. The exceptions are drivers whose suspend leaves the device enabled: pata_cs5520, pata_triflex, and the broken-suspend path of ata_piix. ata_piix already skips ata_pci_device_do_resume() there and uses pci_reenable_device(). I missed pata_triflex (sashiko pointed it out); v2 will handle it the same way as pata_cs5520.
Quoted 3 lines
That change looks suspicious. I would expect it to be unnecessary if all that were changed in ATA were replacing the
pcim_functions with thepci_ones.
pata_cs5520 does not disable the device on suspend, so any resume path that calls pci_enable_device() raises the enable count without a matching disable, whether probe used pcim_ or pci_. That is why its resume must not go through ata_pci_device_do_resume().
Moving all of libata to the unmanaged API would touch the probe and remove paths of every PCI ATA driver, so I kept the fix to the resume path. Damien, Niklas, let me know if you would prefer that instead.
Weidong
Niklas Cassel
maintainerHello Weidong,
Quoted 14 lines · Weidong Wang
On Mon, 2026-11-30 at 14:00 +0100, Philipp Stanner wrote:
Most of them suspend through
ata_pci_device_suspend(), which disables the device, so the enable on resume matches it. The exceptions are drivers whose suspend leaves the device enabled:pata_cs5520,pata_triflex, and the broken-suspend path ofata_piix.ata_piixalready skipsata_pci_device_do_resume()there and usespci_reenable_device(). I missedpata_triflex(sashiko pointed it out); v2 will handle it the same way aspata_cs5520.Quoted 3 lines
That change looks suspicious. I would expect it to be unnecessary if all that were changed in ATA were replacing the
pcim_functions with thepci_ones.
Quoted 8 lines
pata_cs5520does not disable the device on suspend, so any resume path that callspci_enable_device()raises the enable count without a matching disable, whether probe usedpcim_orpci_. That is why its resume must not go throughata_pci_device_do_resume().Moving all of libata to the unmanaged API would touch the probe and remove paths of every PCI ATA driver, so I kept the fix to the resume path. Damien, Niklas, let me know if you would prefer that instead.
Philipp is the author of f748a07a0b64 ("PCI: Remove legacy pcim_release()"), so I trust his judgement.
But to me it makes sense to make libata consistent, either fully use the pci_* APIs or fully use the pcim_* APIs.
Without looking at the patches, it is hard to know if it is more wise to convert everything to pci_* or to convert everything to pcim_*.
(I assume that there was some weird corner case that caused someone to mix the APIs in the first place... So personally I would prefer the solution which untangles this mess with the most logical/beautiful end result.)
Kind regards,
Niklas
Niklas Cassel
maintainerQuoted 10 lines · Niklas Cassel
But to me it makes sense to make libata consistent, either fully use the pci_* APIs or fully use the pcim_* APIs.
Without looking at the patches, it is hard to know if it is more wise to convert everything to pci_* or to convert everything to pcim_*.
(I assume that there was some weird corner case that caused someone to mix the APIs in the first place... So personally I would prefer the solution which untangles this mess with the most logical/beautiful end result.)
Untangling a mix of APIs because of some weird corner case, sounds like a perfect job for an LLM, since they are good at finding the weird corner cases by being able to follow the call chain of all the libata drivers.
If you don't say anything, I will wait for your findings.
I have access to some LLM models and could help out if you don't have time.
Kind regards,
Niklas
Niklas Cassel
maintainerQuoted 8 lines · Niklas Cassel
Quoted 7 lines · Niklas Cassel
But to me it makes sense to make libata consistent, either fully use the pci_* APIs or fully use the pcim_* APIs.
Without looking at the patches, it is hard to know if it is more wise to convert everything to pci_* or to convert everything to pcim_*.
Quoted 7 lines
Quoted 3 lines
(I assume that there was some weird corner case that caused someone to mix the APIs in the first place... So personally I would prefer the solution which untangles this mess with the most logical/beautiful end result.)
Untangling a mix of APIs because of some weird corner case, sounds like a perfect job for an LLM, since they are good at finding the weird corner cases by being able to follow the call chain of all the libata drivers.
See e.g.:
https://lore.kernel.org/linux-ide/[email protected]/
which seemed to look at pata_macio only.
Kind regards,
Niklas
[+cc Vaibhav]
Quoted 44 lines · On Sat, Nov 28, 2026 at 09:11:28PM -0600, Weidong Wang via B4 Relay wrote:
From: Weidong Wang <[email protected]>
ata_pci_device_do_suspend()drops the enable reference taken at probe time withpci_disable_device(), andata_pci_device_do_resume()takes it back withpcim_enable_device().Since commit
f748a07a0b64("PCI: Remove legacypcim_release()"),pcim_enable_device()adds a newpcim_disable_device()devres action on every call instead of reusing a single devres entry. The action registered at probe time is not removed by thepci_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 triggersWARNING: 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, aspci_disable_device()only does so when the count reaches exactly zero.Use
pci_enable_device()inata_pci_device_do_resume(). It balances thepci_disable_device()ofata_pci_device_do_suspend()and leaves the probe time devres action as the only one.
pata_cs5520is the one user ofata_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 callspci_disable_device()with a zero enable count. Before the commit below,pcim_release()checkedpci_is_enabled()for that;pcim_disable_device()does not, and libata cannot cancel that action. This needs a change indrivers/pci/devres.cand is left for a separate patch.
Quoted 8 lines
Found by code inspection with an automated source scanner. Reproduced in QEMU with
ata_piix: after two suspend/resume cycles withpm_testset 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. Thepata_cs5520change 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.
I think a better fix would be to remove ata_pci_device_do_resume() and a lot of the related PCI power management completely. Most of that should be done by the PCI core itself, without involving the drivers.
Vaibhav actually did this work several years ago, but it wasn't merged for lack of testing:
https://lore.kernel.org/lkml/[email protected]/
Quoted 74 lines
Fixes f748a07a0b64 ("PCI: Remove legacy pcim_release()")Signed-off-by Weidong Wang <[email protected]>---
The case of a detach while the device is still disabled (failed resume) is left out, see the commit message: it would need apci_is_enabled()check inpcim_disable_device(). I can send that as a separate patch if the PCI side agrees.
pata_mpiixnever enables the device at probe but uses these helpers; that older imbalance is not touched here.Testing:
- Compile-tested onx86_64(allmodconfig, W=1):
drivers/ata/libata-core.odrivers/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(-)drivers/ata/libata-core.c +7
-1@@ -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);drivers/ata/pata_cs5520.c +8
-5@@ -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-76a77362904cBest regards,
Signature · 1 line
Weidong Wang <[email protected]>
Quoted 3 lines · Bjorn Helgaas
I think a better fix would be to remove
ata_pci_device_do_resume()and a lot of the related PCI power management completely. Most of that should be done by the PCI core itself, without involving the drivers.
Thanks Bjorn, and thanks Niklas for the offer to help. Moving libata to generic power management sounds like the better fix. It touches most of the PCI ATA drivers, and without PATA hardware to test on I don't think I can do it justice, so I'll leave it to someone who can test it rather than send a v2 of this one.
Weidong
Quoted 5 lines · Weidong Wang
Quoted 3 lines · Bjorn Helgaas
I think a better fix would be to remove
ata_pci_device_do_resume()and a lot of the related PCI power management completely. Most of that should be done by the PCI core itself, without involving the drivers.
Quoted 7 lines
Thanks Bjorn, and thanks Niklas for the offer to help. Moving libata to generic power management sounds like the better fix. It touches most of the PCI ATA drivers, and without PATA hardware to test on I don't think I can do it justice, so I'll leave it to someone who can test it rather than send a v2 of this one.
Weidong
Hello Bjorn and Weidong!
Thanks for the cc Bjorn!
Weidong, thanks for the patch. As Bjorn mentioned above, I tried to move ata drivers to generic PM but the blocker was testing. One good starting point would be to test my patches on a VM via QEMU device. If you would like to, you can do that testing.
Vaibhav
Quoted 3 lines · Vaibhav Gupta
One good starting
point would be to test my patches on a VM via QEMU device. If you would like to, you can do that testing.
Thanks for the offer, Vaibhav. My setup can only emulate the PIIX controller, and I'm afraid I won't have time to take on the testing either, so I'll have to pass on this one.
Weidong