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 <[email protected]>

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/[email protected]/
Reported-by: Anthony Pighin <[email protected]>

--
 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;
> 

Reply via email to