pci_release_resource() releases the resource using release_resource() which detaches the resource from the resource tree together with its descendants. The descendants remain attached to the released resource and keep their addresses. Once the resource is assigned elsewhere, the descendants may end up outside of their parent, corrupting the resource tree.
None of the callers wants that. pbus_reassign_bridge_resources() only releases bridge windows that have no children. The BAR resize rollback releases the children before releasing the resource itself. The BARs released before BAR resize, however, are released even if they contain regions requested by a driver, and the resize may then move such a BAR from underneath those regions. Use release_leaf_resource() in pci_release_resource() to return -EBUSY if the resource has children. Remove a BAR that cannot be released from the saved resources so that the BAR resize rollback leaves it alone, and fail BAR resize if it is the resized BAR. pbus_reassign_bridge_resources() checks for children without holding resource_lock, so a child may still appear before the release. Remove such a bridge window from the saved resources too so that the rollback does not release the resources underneath it. As the release can now fail, log both outcomes. Signed-off-by: Ilpo Järvinen <[email protected]> --- drivers/pci/setup-bus.c | 10 ++++++++-- drivers/pci/setup-res.c | 12 ++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c index c1033191571c..e5bbde75337d 100644 --- a/drivers/pci/setup-bus.c +++ b/drivers/pci/setup-bus.c @@ -2330,7 +2330,8 @@ static int pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource * if (ret) return ret; - pci_release_resource(bridge, i); + if (pci_release_resource(bridge, i)) + pci_dev_res_remove_from_list(saved, res); } else { const char *res_name = pci_resource_name(bridge, i); @@ -2412,7 +2413,12 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size goto restore; continue; } - pci_release_resource(pdev, i); + ret = pci_release_resource(pdev, i); + if (ret) { + pci_dev_res_remove_from_list(&saved, r); + if (i == resno) + goto restore; + } } ret = pci_rebar_set_size(pdev, resno, size); diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c index 376f09630a4a..1317d645a178 100644 --- a/drivers/pci/setup-res.c +++ b/drivers/pci/setup-res.c @@ -472,11 +472,15 @@ int pci_release_resource(struct pci_dev *dev, int resno) if (!res->parent) return 0; - pci_info(dev, "%s %pR: releasing\n", res_name, res); - - ret = release_resource(res); - if (ret) + ret = release_leaf_resource(res); + if (ret) { + pci_info(dev, "%s %pR: can't release; contains resources in use\n", + res_name, res); return ret; + } + + pci_info(dev, "%s %pR: released\n", res_name, res); + res->end = resource_size(res) - 1; res->start = 0; res->flags |= IORESOURCE_UNSET; -- 2.47.3
