From: Bjorn Helgaas <helgaas@kernel.org>
To: weidongw@illinois.edu
Cc: "Damien Le Moal" <dlemoal@kernel.org>,
"Niklas Cassel" <cassel@kernel.org>,
linux-ide@vger.kernel.org,
"Philipp Stanner" <pstanner@redhat.com>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
linux-kernel@vger.kernel.org, stable@vger.kernel.org,
"Vaibhav Gupta" <vaibhavgupta40@gmail.com>
Subject: Re: [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume
Date: Thu, 3 Dec 2026 10:14:35 -0600 [thread overview]
Message-ID: <20261203161435.GA814283@bhelgaas> (raw)
In-Reply-To: <20261128-kbug-l080-v1-1-90f7309990ea@illinois.edu>
[+cc Vaibhav]
On Sat, Nov 28, 2026 at 09:11:28PM -0600, Weidong Wang via B4 Relay wrote:
> 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.
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/20200817092245.10478-1-vaibhavgupta40@gmail.com/
> 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>
>
>
next prev parent reply other threads:[~2026-12-03 16:14 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
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-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
2026-12-03 16:14 ` Bjorn Helgaas [this message]
2026-12-03 16:36 ` Weidong Wang
2026-12-04 19:06 ` Vaibhav Gupta
2026-12-04 21:09 ` Weidong Wang
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=20261203161435.GA814283@bhelgaas \
--to=helgaas@kernel.org \
--cc=bhelgaas@google.com \
--cc=cassel@kernel.org \
--cc=dlemoal@kernel.org \
--cc=kwilczynski@kernel.org \
--cc=linux-ide@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pstanner@redhat.com \
--cc=stable@vger.kernel.org \
--cc=vaibhavgupta40@gmail.com \
--cc=weidongw@illinois.edu \
/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