mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: "Bjorn Helgaas" <bhelgaas@google.com>,
	"Lorenzo Pieralisi" <lorenzo.pieralisi@arm.com>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Wilczyński" <kw@linux.com>,
	"Alex Deucher" <alexander.deucher@amd.com>,
	"Christian König" <christian.koenig@amd.com>,
	"Jani Nikula" <jani.nikula@linux.intel.com>,
	"Joonas Lahtinen" <joonas.lahtinen@linux.intel.com>,
	"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
	"Tvrtko Ursulin" <tursulin@ursulin.net>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Michał Winiarski" <michal.winiarski@intel.com>,
	"Ville Syrjälä" <ville.syrjala@linux.intel.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	intel-gfx@lists.freedesktop.org, intel-xe@lists.freedesktop.org,
	"Anthony Pighin" <anthony.pighin@nokia.com>,
	"Yinghai Lu" <yinghai@kernel.org>,
	"Jesse Barnes" <jbarnes@virtuousgeek.org>,
	"Alex Chiang" <achiang@hp.com>,
	linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Subject: [PATCH v2 16/16] PCI: Release only PCI resources when releasing a resource tree
Date: Sat,  5 Dec 2026 02:39:51 +0200	[thread overview]
Message-ID: <20261205003957.3304-17-ilpo.jarvinen@linux.intel.com> (raw)
In-Reply-To: <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>

pci_bridge_release_resources() and the BAR resize rollback release
everything underneath a bridge window using release_child_resources().
Releasing children does not differentiate between PCI resources and
other resources, such as regions requested by a driver, so resources
that are in use can be ripped out. Unlike with pci_release_resource(),
IORESOURCE_UNSET is not set for the released resources, and the resource
core logs them only as "release child resource %pR", which gives no hint
of the PCI device they belong to.

Add pci_release_resource_tree() that releases a PCI resource and,
recursively, the resources of the devices on the subordinate bus that
reside underneath it. The resources of the devices behind a transparent
bridge may also reside directly in the window above the bridge. Walk the
PCI hierarchy to find those resources and compare their number with the
number of children in the resource tree. If anything else resides
underneath, such as a region requested by a driver or the BARs of
enabled VFs, fail without releasing anything. Also fail if any of the
resources is IORESOURCE_PCI_FIXED, as it cannot follow the window should
the window move. Hold resource_lock across the check and the release so
that no resource can be requested in between.

Update each released resource with pci_resource_post_release(), which
also logs the release with the PCI device and the resource name.

Use pci_release_resource_tree() in pci_bridge_release_resources() and in
the BAR resize rollback. All saved resources were released once before
the BAR resize, so releasing them in the rollback is not expected to
fail. Warn and skip restoring a resource if it does.

Fixes: 5009b46025ac ("PCI: add pci_bridge_release_resources and pci_bus_release_bridge_resources")
Link: https://lore.kernel.org/linux-pci/20260727193714.598562-1-anthony.pighin@nokia.com/
Reported-by: Anthony Pighin <anthony.pighin@nokia.com>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
 drivers/pci/pci.h       |   1 +
 drivers/pci/setup-bus.c |  12 ++---
 drivers/pci/setup-res.c | 107 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+), 6 deletions(-)

diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 9d0cbe6c8991..09457fc8e11d 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -604,6 +604,7 @@ struct resource *pbus_select_window(struct pci_bus *bus,
 				    const struct resource *res);
 void pci_reassigndev_resource_alignment(struct pci_dev *dev);
 void pci_disable_bridge_window(struct pci_dev *dev);
+int pci_release_resource_tree(struct pci_dev *dev, int resno);
 struct pci_bus *pci_bus_get(struct pci_bus *bus);
 void pci_bus_put(struct pci_bus *bus);
 
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 033f5fab7cc0..e14066c6aa8d 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -1666,10 +1666,7 @@ static void pci_bridge_release_resources(struct pci_bus *bus,
 
 	idx = pci_resource_num(dev, b_win);
 
-	/* If there are children, release them all */
-	release_child_resources(b_win);
-
-	ret = pci_release_resource(dev, idx);
+	ret = pci_release_resource_tree(dev, idx);
 	if (ret)
 		return;
 
@@ -2472,8 +2469,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 		if (!resource_assigned(res))
 			continue;
 
-		release_child_resources(res);
-		pci_release_resource(dev, i);
+		pci_release_resource_tree(dev, i);
 	}
 
 	/* Restore parents before their children */
@@ -2483,6 +2479,10 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 
 		i = pci_resource_num(dev, res);
 
+		/* All saved resources were released once before the resize */
+		if (WARN_ON_ONCE(resource_assigned(res)))
+			continue;
+
 		pci_dev_res_restore(dev_res);
 
 		if (pci_claim_resource(dev, i))
diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
index 82efd4a76c15..848d05fbe76f 100644
--- a/drivers/pci/setup-res.c
+++ b/drivers/pci/setup-res.c
@@ -502,6 +502,113 @@ int pci_release_resource(struct pci_dev *dev, int resno)
 }
 EXPORT_SYMBOL(pci_release_resource);
 
+/*
+ * Check whether @res of @dev can be released together with everything
+ * underneath it. Directly underneath @res, only resources of the devices
+ * on the subordinate bus of @dev, or behind transparent bridges on it, are
+ * allowed, and the same applies to each of them in turn. Anything else,
+ * such as a region requested by a driver or the BARs of enabled VFs, pins
+ * @res in place. So does an IORESOURCE_PCI_FIXED resource anywhere in the
+ * tree.
+ */
+static bool pci_can_release_resource_tree(struct pci_dev *dev,
+					  struct resource *res)
+{
+	unsigned int children = 0, pci_children = 0;
+	struct pci_bus *bus = dev->subordinate;
+	struct pci_dev *child;
+	struct resource *r;
+
+	lockdep_assert_held_write(&resource_lock);
+
+	if (res->flags & IORESOURCE_PCI_FIXED) {
+		int resno = pci_resource_num(dev, res);
+		const char *res_name = pci_resource_name(dev, resno);
+
+		pci_info(dev, "%s %pR: can't release; fixed resource\n",
+			 res_name, res);
+		return false;
+	}
+
+	resource_tree_for_each(res, r, true)
+		children++;
+
+	if (!children)
+		return true;
+
+	if (bus) {
+		pci_bus_for_each_window_resource(bus, res, child, r) {
+			pci_children++;
+			if (!pci_can_release_resource_tree(child, r))
+				return false;
+		}
+	}
+
+	if (children != pci_children) {
+		int resno = pci_resource_num(dev, res);
+		const char *res_name = pci_resource_name(dev, resno);
+
+		pci_info(dev, "%s %pR: can't release; contains resources in use\n",
+			 res_name, res);
+		return false;
+	}
+
+	return true;
+}
+
+static void __pci_release_resource_tree(struct pci_dev *dev,
+					struct resource *res)
+{
+	int resno = pci_resource_num(dev, res);
+	struct pci_bus *bus = dev->subordinate;
+	struct pci_dev *child;
+	struct resource *r;
+
+	lockdep_assert_held_write(&resource_lock);
+
+	if (bus) {
+		pci_bus_for_each_window_resource(bus, res, child, r)
+			__pci_release_resource_tree(child, r);
+	}
+
+	if (release_resource_locked(res)) {
+		const char *res_name = pci_resource_name(dev, resno);
+
+		pci_dbg(dev, "%s %pR: can't release\n", res_name, res);
+		return;
+	}
+
+	pci_resource_post_release(dev, resno);
+}
+
+/**
+ * pci_release_resource_tree - Release a PCI resource and its PCI descendants
+ * @dev: PCI device
+ * @resno: Resource number
+ *
+ * Release @resno of @dev and, recursively, the resources of the devices
+ * on the subordinate bus underneath it, including those behind transparent
+ * bridges. If anything else resides underneath, nothing is released.
+ *
+ * Return: 0 on success, or -EBUSY if @resno could not be released.
+ */
+int pci_release_resource_tree(struct pci_dev *dev, int resno)
+{
+	struct resource *res = pci_resource_n(dev, resno);
+
+	guard(write_lock)(&resource_lock);
+
+	if (!resource_assigned(res))
+		return 0;
+
+	if (!pci_can_release_resource_tree(dev, res))
+		return -EBUSY;
+
+	__pci_release_resource_tree(dev, res);
+
+	return 0;
+}
+
 int pci_enable_resources(struct pci_dev *dev, int mask)
 {
 	u16 cmd, old_cmd;
-- 
2.47.3


      parent reply	other threads:[~2026-12-05  0:43 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  0:39 [PATCH v2 00/16] PCI: Resource release fixes and improvements Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 01/16] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 02/16] PCI: Set new BAR Size only after releasing the BARs for BAR resize Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 03/16] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 04/16] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 05/16] resource: Check that the resource is in the tree when releasing it Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 06/16] resource, kunit: Add test for releasing a resource not in the tree Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 07/16] resource: Add release_leaf_resource() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 08/16] resource, kunit: Add test for release_leaf_resource() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 09/16] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 10/16] resource: Rename next_resource() and for_each_resource() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 11/16] resource: Make resource_lock and resource tree traversal available Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 12/16] resource: Add release_resource_locked() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 13/16] PCI: Split pci_resource_post_release() out of pci_release_resource() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 14/16] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 15/16] PCI: Keep the range of fixed resources when releasing them Ilpo Järvinen
2026-12-05  0:39 ` Ilpo Järvinen [this message]

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=20261205003957.3304-17-ilpo.jarvinen@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=achiang@hp.com \
    --cc=airlied@gmail.com \
    --cc=alexander.deucher@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=anthony.pighin@nokia.com \
    --cc=bhelgaas@google.com \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=jani.nikula@linux.intel.com \
    --cc=jbarnes@virtuousgeek.org \
    --cc=joonas.lahtinen@linux.intel.com \
    --cc=kw@linux.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lorenzo.pieralisi@arm.com \
    --cc=matthew.brost@intel.com \
    --cc=michal.winiarski@intel.com \
    --cc=robh@kernel.org \
    --cc=rodrigo.vivi@intel.com \
    --cc=simona@ffwll.ch \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tursulin@ursulin.net \
    --cc=ville.syrjala@linux.intel.com \
    --cc=yinghai@kernel.org \
    /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

all inboxes | Powered by JetHome®