From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Bjorn Helgaas <bhelgaas@google.com>,
Anthony Pighin <anthony.pighin@nokia.com>
Cc: "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,
linux-pci@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree
Date: Wed, 2 Dec 2026 02:22:54 +0200 (EET) [thread overview]
Message-ID: <eff11e97-affb-4b46-367d-032dab48f166@linux.intel.com> (raw)
In-Reply-To: <20261201155844.2278-16-ilpo.jarvinen@linux.intel.com>
[-- Attachment #1: Type: text/plain, Size: 7532 bytes --]
On Tue, 1 Dec 2026, Ilpo Järvinen wrote:
> 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. 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.
>
> Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
While doing some follow-up work, I realized we've actually seen device
resources getting ripped off the resource tree not that far back. So here
are a few extra tags related to that realization:
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>
--
i.
> ---
> drivers/pci/pci.h | 1 +
> drivers/pci/setup-bus.c | 12 ++---
> drivers/pci/setup-res.c | 97 +++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 104 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 e5bbde75337d..18c8db2a6f28 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;
>
> @@ -2471,8 +2468,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 */
> @@ -2482,6 +2478,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 a56e0cc3db58..8440c780bc92 100644
> --- a/drivers/pci/setup-res.c
> +++ b/drivers/pci/setup-res.c
> @@ -498,6 +498,103 @@ 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.
> + */
> +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);
> +
> + 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;
>
prev parent reply other threads:[~2026-12-02 0:23 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 01/15] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 02/15] PCI: Set new BAR Size only after releasing the BARs for BAR resize Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 03/15] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 04/15] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 05/15] resource: Check that the resource is in the tree when releasing it Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 06/15] resource: Add KUnit test for releasing a resource not in the tree Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 07/15] resource: Add release_leaf_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 08/15] resource: Add KUnit test for release_leaf_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 09/15] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 10/15] resource: Rename next_resource() and for_each_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 11/15] resource: Make resource_lock and resource tree traversal available Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 12/15] resource: Add release_resource_locked() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 13/15] PCI: Split pci_resource_post_release() out of pci_release_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 14/15] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
2026-12-02 0:22 ` 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=eff11e97-affb-4b46-367d-032dab48f166@linux.intel.com \
--to=ilpo.jarvinen@linux.intel.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=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 \
/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®