From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795922017; b=PUFKed9j5Xm+NJesill9G18mu0qXsEqKFnvlJwDZ8E9VQJpEyySFj8lDZR5TfN0LAivI mmzraNjMLZI6ytFn/1nVbz3Uh8dr4isX4kzorOzUlODt+hPg2zCFgrycNaP06UXaBl1mF /6obPfLtVTy5do+0yBy5peIvWITfmO1Jfb4wDe1XlpfjTg+0VXYv7OHdCfRrN+RTIrRt0 Tx/BG9VmheV5aw4Ev/7b5ot2hLEWPNm/Ex2rdpninB62b5QApJU7f2DsyxWOvehkKOVTX MJad87Bnza0VG/WROVGPmItfOLr2PDymeBdQVOtHqTFIbvYDzJ+PPq1WPtJ8bG6Lisg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795922017; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=KdKnv7f1PLkCfMr4fHGhQgHneXHUphcnh99JKIgV2OM=; b=hC4XgkfeU50Q9u267Cj4xA4gFz8+Zn74jDO3zARXOUSEbRen4FYyhFTeSH/DwQ697Bh4 UhYF3/V/WwKzdEk5EFa1hNlucapVO36/VBC5sIShKwemdIit725Vhmizhwd9LnxLEIFKu hU148dK4uNIB5gqHta9LGlPEpyG2w/u75BvuuzDkgUeYW0idka4/oo5UoUlr5F/fFGiSx JjBoWoiAh+1STmEQp6xNNuwdWxGrHp56lE26/axSjUKAGWWGCgdIfCWlzFcbio+8L9Dms VeJ/VfFZjpR16o/25yz/psrLGHYDBxylMRjRM8z08/TDvIpXG53yL7CpVd8P+N/nN9A== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kM8tLOBI; arc=pass header.oldest-pass=0 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 67C53606A31C; Sun, 29 Nov 2026 03:13:37 +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=kM8tLOBI Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795922017; b=iiliWYSec+cG8JYdrOUFOCebDLqxbej+0o0ckAymiCa+btn08w1RMRrkkf+FYoqbAq6l w2RlwVS8vKC5rcYH904Cbt9Qwtq4dzhT4yybPrylfUlrzPoVYLvjhd0PI3xb03EnxQnLO 8Yt9OsZw7tK+9kyIT6Uh09Gn2VF4X3m+0i3D4NCUrMNZa/jnKVjyWQQ0cQCoMLKb7CgcK 38lCdi+6gsYkGLEbN2s2LolcS+YGDD0IhmteGUxn5eAHlEGptw61exvnytWbApWRYYzuy X2A3MWWzrbuCv3J49VsgcjBcVB/r8+B5DHotTrcDQ7nnkua3dPJ9jaCnu3vHH6lgEAg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795922017; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=KdKnv7f1PLkCfMr4fHGhQgHneXHUphcnh99JKIgV2OM=; b=vp8PcKWRAjXJSxPPZUUsEyoKS1eXS3qNfQMwnJCay7LQcO1rcL9pWu/Qi8Xi3rQH1iPK 9rkeOlddMZ2sZIt54loNMj+zyIuaM5a4PSwDEXvT2xEn1Vg2tAIApE5JNiR1vd7ZuJpnu BPEXG0ZJoiEOMTAB1dpQFTG3ngJB5NHRM6PM7SGNPaFQbXFaICdGPONnEB20gdtWASXtd M4KikrCs239iwOPEhL0ygm+WIqiW7z+Ckq//7e5UU0Z9T0GSttbuwIVQcIDfJWtKgY1sg 5v9NxrTjL3OgD5fJWeBRg/u1zCC+tgodeOcw2V4RDS8EYjXv+5aBg5WWMkXCf0yIJlg== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 5EDC21F00896; Sun, 29 Nov 2026 03:13:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795922016; bh=KdKnv7f1PLkCfMr4fHGhQgHneXHUphcnh99JKIgV2OM=; h=From:Date:Subject:To:Cc:Reply-To; b=kM8tLOBIRDg5Pl2yUS++sj9sNwPc3gAqTuAV+7hkLzxMKm/RiHNJQrXNNwBaai6g9 +6ZTmMrA9nSGsAUo1J2ni37Ie/iPDWKCfG/8uUAWzKB5ZVGuFMaaArBQcrqrJdvnBt niIC4yzw4hc4SJid4a6xua7vm/IeleEfkZSZP6o021IkiDs4hp9g1d0xhZ37jYLhF3 gizVBH3tCXpPXXwCugvp6U6nk+ObH0sOnLpXGTLE5lW+I8o4nOQjQTdacxnfhZExbW dDX071aYp8dBKykEOxzOl5Qk9w3rH1s4CMVYFQiak0Y8SOzzz9ExQ548g2Q0jvZ5TK 1bbniH4aC6+hA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id B2FB04E30C; Sun, 29 Nov 2026 03:13:35 +0000 (UTC) From: Weidong Wang via B4 Relay Date: Sat, 28 Nov 2026 21:11:28 -0600 Subject: [PATCH] ata: libata-core: Do not use pcim_enable_device() on PCI resume X-Mailing-List: linux-kernel@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="utf-8" Message-Id: <20261128-kbug-l080-v1-1-90f7309990ea@illinois.edu> X-B4-Tracking: v=1; b=H4sIAN+XC2sC/yXM0QpAMBSA4VfRubbaRhteRS5mDg5CG1Ly7obLr /7+Czw6Qg9FdIHDgzwtc4CII7C9mTtk1ASD5FIJITM21nvHJp5xppXROlEy56mF0K8OWzq/V1n 99ns9oN3eAdz3A/pOkcttAAAA X-Change-ID: 20261128-kbug-l080-76a77362904c To: Damien Le Moal , Niklas Cassel Cc: linux-ide@vger.kernel.org, Philipp Stanner , Bjorn Helgaas , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , linux-kernel@vger.kernel.org, stable@vger.kernel.org, Weidong Wang X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795922015; l=5152; i=weidongw@illinois.edu; s=20261108; h=from:subject:message-id; bh=+PekEe/DCS+Da0lseHP312Y4/SnPNRazxK36WfbL9kk=; b=FT9gJnJM0cz1FC51BTxIolpZtxsj5uVN3/2DjEtNgX8k/vwVtU2mnVCRKlM7B+jZmku6Dbo7Q u/bSex2iVLoDeHFee2PXCpb/pTH/rj8/UIeiE6ML493Y7ZTV16QwHTR X-Developer-Key: i=weidongw@illinois.edu; a=ed25519; pk=DR2S1xPEa5sELrWPfOpvUphJQ2VHC+MDc/wJavE/ifg= X-Endpoint-Received: by B4 Relay for weidongw@illinois.edu/20261108 with auth_id=1449 X-Original-From: Weidong Wang Reply-To: weidongw@illinois.edu Content-Transfer-Encoding: 7bit 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. 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