* [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume
@ 2026-11-29 3:11 Weidong Wang via B4 Relay
2026-11-29 3:21 ` sashiko-bot
2026-11-30 13:00 ` pstanner
0 siblings, 2 replies; 7+ messages in thread
From: Weidong Wang via B4 Relay @ 2026-11-29 3:11 UTC (permalink / raw)
To: Damien Le Moal, Niklas Cassel
Cc: linux-ide, Philipp Stanner, Bjorn Helgaas,
Krzysztof Wilczyński, linux-kernel, stable, Weidong Wang
From: Weidong Wang <weidongw@illinois.edu>
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@vger.kernel.org
Signed-off-by: Weidong Wang <weidongw@illinois.edu>
---
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@illinois.edu>
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume
2026-11-29 3:11 [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume Weidong Wang via B4 Relay
@ 2026-11-29 3:21 ` sashiko-bot
2026-11-30 13:00 ` pstanner
1 sibling, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-29 3:21 UTC (permalink / raw)
To: Weidong Wang; +Cc: linux-ide, dlemoal, cassel
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 <weidongw@illinois.edu>
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.
> diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
> index f41ab71fc9e2b..587a393d27535 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);
[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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261128-kbug-l080-v1-1-90f7309990ea@illinois.edu?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume
2026-11-29 3:11 [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume Weidong Wang via B4 Relay
2026-11-29 3:21 ` sashiko-bot
@ 2026-11-30 13:00 ` pstanner
2026-12-01 20:15 ` Weidong Wang
1 sibling, 1 reply; 7+ messages in thread
From: pstanner @ 2026-11-30 13:00 UTC (permalink / raw)
To: weidongw, Damien Le Moal, Niklas Cassel
Cc: linux-ide, Bjorn Helgaas, Krzysztof Wilczyński, linux-kernel,
stable
Hi, thx for the patch.
On Sat, 2026-11-28 at 21:11 -0600, Weidong Wang via B4 Relay wrote:
> From: Weidong Wang <weidongw@illinois.edu>
>
[…]
>
> 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 :)
>
> Fixes: f748a07a0b64 ("PCI: Remove legacy pcim_release()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Weidong Wang <weidongw@illinois.edu>
> ---
> 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.
That's not a good idea. This intertwining makes the API inconsistent.
Instead, pcim_disable_device() should not exist / should be deprecated.
See below.
>
> 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);
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().
> 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
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.
> @@ -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);
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.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume
2026-11-30 13:00 ` pstanner
@ 2026-12-01 20:15 ` Weidong Wang
2026-12-03 13:27 ` Niklas Cassel
0 siblings, 1 reply; 7+ messages in thread
From: Weidong Wang @ 2026-12-01 20:15 UTC (permalink / raw)
To: pstanner@redhat.com
Cc: Damien Le Moal, Niklas Cassel, linux-ide, Bjorn Helgaas,
Krzysztof Wilczyński, linux-kernel, stable
On Mon, 2026-11-30 at 14:00 +0100, Philipp Stanner wrote:
> 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.
>> it would need a pci_is_enabled()
>> check in pcim_disable_device(). I can send that as a separate patch
>
> That's not a good idea. This intertwining makes the API inconsistent.
Understood, I will drop that idea.
> 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().
> 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.
> 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.
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume
2026-12-01 20:15 ` Weidong Wang
@ 2026-12-03 13:27 ` Niklas Cassel
2026-12-03 13:39 ` Niklas Cassel
0 siblings, 1 reply; 7+ messages in thread
From: Niklas Cassel @ 2026-12-03 13:27 UTC (permalink / raw)
To: Weidong Wang
Cc: pstanner@redhat.com, Damien Le Moal, linux-ide, Bjorn Helgaas,
Krzysztof Wilczyński, linux-kernel, stable
Hello Weidong,
On Tue, Dec 01, 2026 at 02:15:04PM -0600, Weidong Wang wrote:
> 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 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.
>
> > 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.
>
> 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.
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume
2026-12-03 13:27 ` Niklas Cassel
@ 2026-12-03 13:39 ` Niklas Cassel
2026-12-03 13:48 ` Niklas Cassel
0 siblings, 1 reply; 7+ messages in thread
From: Niklas Cassel @ 2026-12-03 13:39 UTC (permalink / raw)
To: Weidong Wang
Cc: pstanner@redhat.com, Damien Le Moal, linux-ide, Bjorn Helgaas,
Krzysztof Wilczyński, linux-kernel, stable
On Thu, Dec 03, 2026 at 02:27:53PM +0100, Niklas Cassel wrote:
>
> 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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume
2026-12-03 13:39 ` Niklas Cassel
@ 2026-12-03 13:48 ` Niklas Cassel
0 siblings, 0 replies; 7+ messages in thread
From: Niklas Cassel @ 2026-12-03 13:48 UTC (permalink / raw)
To: Weidong Wang
Cc: pstanner@redhat.com, Damien Le Moal, linux-ide, Bjorn Helgaas,
Krzysztof Wilczyński, linux-kernel, stable
On Thu, Dec 03, 2026 at 02:39:26PM +0100, Niklas Cassel wrote:
> On Thu, Dec 03, 2026 at 02:27:53PM +0100, Niklas Cassel wrote:
> >
> > 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.
See e.g.:
https://lore.kernel.org/linux-ide/20261201-s2p-d104-v1-1-df4a9ef2b79d@mbzuai.ac.ae/
which seemed to look at pata_macio only.
Kind regards,
Niklas
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-03 13:48 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 3:11 [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume Weidong Wang via B4 Relay
2026-11-29 3:21 ` sashiko-bot
2026-11-30 13:00 ` pstanner
2026-12-01 20:15 ` Weidong Wang
2026-12-03 13:27 ` Niklas Cassel
2026-12-03 13:39 ` Niklas Cassel
2026-12-03 13:48 ` Niklas Cassel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox