From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796314477; b=SlMq6lL/EC5+/u3Otyejn1Z/zI02MbFTjMxFxaGzu2r1slNZm+a4YMpu4bTsgCz1qBQs ohuN32AFOm35BZZZa+MmC9fWpGwGkBB4NZ4qbmRXAiedlJs+kjL1YrDl85MjWvmHut2E5 nRg1v3ARWHsKwVuXdR8FsMAFzl41Lswk6tTHHuGII+EKXzO+lAtU3yQLMEigL9OmWa/SZ dZPMR8xMoKmKPxd7SXFniXzqOn6EXA2IvAssde9xF7yizRD3oavQBvdSylgPKCQZd34R3 dYJOttxSZa5YtryNC+j9raPU4u5iA1ZGJHn+sO69+U7+ZZ6gSNrPIN3xAtFFdZPzeKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796314477; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=j73hOw7EXaegdJpLyqmRNkwRoAKGFJUwdk5jW4qjc5Q=; b=sqStDk3iOC5lDLzGafnNXa2ylp0zPQq+dy0vw4skmhQMK6zuxC1xa2hmiNcYu7TNKB/1 K1fRtAUB2em72KkN8PHotaLN+I/h8VbBufOgXRFnHay0H1yCBwm/6yXieNC8fq8iYV8pO t7Jw5yqQjQvcy3D37sHTYmxAs86rJGrwrjn28IFT6hkqX7YI0BIx+ymjc+1G4tGa1xCl1 s5jrYl2+CBZxgjluApv0gl9JSUj6NDYj1WnXbclOlv1wXE1IoNx2YQk2FyAJ0fkbcdozA iLPhKPJrznAU7XxZZbSBC2VnfrhxuKz2lGny7GwGudBC16lfLKtVFgqCYm6Fl9kP1Ag== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BRlC48Rw; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1FA91606A334; Thu, 3 Dec 2026 16:14:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BRlC48Rw Received: by smtp.kernel.org (Postfix) with ESMTPSA id 80CC11F00893; Thu, 3 Dec 2026 16:14:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796314476; bh=j73hOw7EXaegdJpLyqmRNkwRoAKGFJUwdk5jW4qjc5Q=; h=Date:From:To:Cc:Subject:In-Reply-To; b=BRlC48RwnrH8j3KNx2gQHVe5aFHDDtHUdmU7OYNWTm3oBl2qorJ2FfBfAXARwKXpO ulKJMfmWgwT6wbpnshWQCL5e7b+KtgMcOvfJCDjpr6KQ3C3EHgUw+g4hcY/4nXf2dL tahma4QbtC8/lnQpdLoCSwQRFVcxSawdgtqvWICDAu8gjaotQ5JqbYktA7Go3HGuDF PUb7yxEMLRLzIInnd1nOQYGeUB+9MfZb0Z3MQ2lM2klOpLLcG4g9xIN74sI8o0PEwI Pk72VrVsr2I0VVuG5Du3s7hVbueTKuvIFvIMKIDMzTzOQbwKbaONZFALTan1BXp/rp PtIr5hgxVlfHQ== Date: Thu, 3 Dec 2026 10:14:35 -0600 From: Bjorn Helgaas To: weidongw@illinois.edu Cc: Damien Le Moal , Niklas Cassel , linux-ide@vger.kernel.org, Philipp Stanner , Bjorn Helgaas , Krzysztof =?utf-8?Q?Wilczy=C5=84ski?= , linux-kernel@vger.kernel.org, stable@vger.kernel.org, Vaibhav Gupta Subject: Re: [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume Message-ID: <20261203161435.GA814283@bhelgaas> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 > > 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 > --- > 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 > >