From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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.subspace.kernel.org (Postfix) with ESMTPS id 878BC606A32D for ; Wed, 2 Dec 2026 00:23:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=HgpLo4/y Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 22D3A10FE41; Wed, 2 Dec 2026 00:23:12 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) by gabe.freedesktop.org (Postfix) with ESMTPS id C94C910E3D0; Wed, 2 Dec 2026 00:23:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796170986; x=1827706986; h=from:date:to:cc:subject:in-reply-to:message-id: references:mime-version; bh=WEUO+Ktf0St+4Pbt/qDLEUmz04W44hGZ8/NGGjs7VV4=; b=HgpLo4/yhyO67ab2tSDqvKB6HmAj2UYsQvgFoe7MDA1aqX65cDQZxLNZ f60fKiqdYCJPc608ooZb2vfwveFKVxrBSvJoF+AQ6a7D59BY/r3aT07LO 2Ud1mlNDeIp58v+jCW+kt3KwM9yH6NDsYgURSo6EjhSbkQzZMxFYILpzj eGbJn+4uk0I/MQKGqZhZhih4OULGxDeyAX5xHagnC5xhxHJgRad9tR9te NEC7f9OZC3j/gEr1YB20zqfD+1JnZCpTVS7opBV65RQcWZ+oM3YwrF8Cv 140GmCuSlm33uZUxKDcbmo+OwT5OWxn6s+VlrYVHE4KyCZADt0gyyNLBS w==; X-CSE-ConnectionGUID: FOOs/bBtR/Su4tHucwyd8A== X-CSE-MsgGUID: bSplsy7oRfWfK5r67A1/rg== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="4988658" X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="4988658" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 16:23:05 -0800 X-CSE-ConnectionGUID: LzlhNfbxTquYWdEylNN9LA== X-CSE-MsgGUID: 1W1GRjKUSsefNHgFrtG1+Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="13744493" Received: from pgcooper-mobl3.ger.corp.intel.com (HELO localhost) ([10.245.245.21]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 16:22:57 -0800 From: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= Date: Wed, 2 Dec 2026 02:22:54 +0200 (EET) To: Bjorn Helgaas , Anthony Pighin cc: Lorenzo Pieralisi , Rob Herring , =?ISO-8859-2?Q?Krzysztof_Wilczy=F1ski?= , Alex Deucher , =?ISO-8859-15?Q?Christian_K=F6nig?= , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , Matthew Brost , =?ISO-8859-15?Q?Thomas_Hellstr=F6m?= , =?ISO-8859-2?Q?Micha=B3_Winiarski?= , =?ISO-8859-15?Q?Ville_Syrj=E4l=E4?= , David Airlie , Simona Vetter , 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 Subject: Re: [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree In-Reply-To: <20261201155844.2278-16-ilpo.jarvinen@linux.intel.com> Message-ID: References: <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com> <20261201155844.2278-16-ilpo.jarvinen@linux.intel.com> X-Archived-By: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="8323328-740981936-1796170974=:26849" X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Intel-gfx" X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-740981936-1796170974=:26849 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE On Tue, 1 Dec 2026, Ilpo J=C3=A4rvinen 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. >=20 > 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. >=20 > Update each released resource with pci_resource_post_release(), which > also logs the release with the PCI device and the resource name. >=20 > 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. >=20 > Signed-off-by: Ilpo J=C3=A4rvinen While doing some follow-up work, I realized we've actually seen device=20 resources getting ripped off the resource tree not that far back. So here= =20 are a few extra tags related to that realization: Fixes: 5009b46025ac ("PCI: add pci_bridge_release_resources and pci_bus_rel= ease_bridge_resources") Link: https://lore.kernel.org/linux-pci/20260727193714.598562-1-anthony.pig= hin@nokia.com/ Reported-by: Anthony Pighin -- 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(-) >=20 > 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 *b= us, > =09=09=09=09 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); > =20 > 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 pc= i_bus *bus, > =20 > =09idx =3D pci_resource_num(dev, b_win); > =20 > -=09/* If there are children, release them all */ > -=09release_child_resources(b_win); > - > -=09ret =3D pci_release_resource(dev, idx); > +=09ret =3D pci_release_resource_tree(dev, idx); > =09if (ret) > =09=09return; > =20 > @@ -2471,8 +2468,7 @@ int pci_do_resource_release_and_resize(struct pci_d= ev *pdev, int resno, int size > =09=09if (!resource_assigned(res)) > =09=09=09continue; > =20 > -=09=09release_child_resources(res); > -=09=09pci_release_resource(dev, i); > +=09=09pci_release_resource_tree(dev, i); > =09} > =20 > =09/* Restore parents before their children */ > @@ -2482,6 +2478,10 @@ int pci_do_resource_release_and_resize(struct pci_= dev *pdev, int resno, int size > =20 > =09=09i =3D pci_resource_num(dev, res); > =20 > +=09=09/* All saved resources were released once before the resize */ > +=09=09if (WARN_ON_ONCE(resource_assigned(res))) > +=09=09=09continue; > + > =09=09pci_dev_res_restore(dev_res); > =20 > =09=09if (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 r= esno) > } > EXPORT_SYMBOL(pci_release_resource); > =20 > +/* > + * Check whether @res of @dev can be released together with everything > + * underneath it. Directly underneath @res, only resources of the device= s > + * 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, pi= ns > + * @res in place. > + */ > +static bool pci_can_release_resource_tree(struct pci_dev *dev, > +=09=09=09=09=09 struct resource *res) > +{ > +=09unsigned int children =3D 0, pci_children =3D 0; > +=09struct pci_bus *bus =3D dev->subordinate; > +=09struct pci_dev *child; > +=09struct resource *r; > + > +=09lockdep_assert_held_write(&resource_lock); > + > +=09resource_tree_for_each(res, r, true) > +=09=09children++; > + > +=09if (!children) > +=09=09return true; > + > +=09if (bus) { > +=09=09pci_bus_for_each_window_resource(bus, res, child, r) { > +=09=09=09pci_children++; > +=09=09=09if (!pci_can_release_resource_tree(child, r)) > +=09=09=09=09return false; > +=09=09} > +=09} > + > +=09if (children !=3D pci_children) { > +=09=09int resno =3D pci_resource_num(dev, res); > +=09=09const char *res_name =3D pci_resource_name(dev, resno); > + > +=09=09pci_info(dev, "%s %pR: can't release; contains resources in use\n"= , > +=09=09=09 res_name, res); > +=09=09return false; > +=09} > + > +=09return true; > +} > + > +static void __pci_release_resource_tree(struct pci_dev *dev, > +=09=09=09=09=09struct resource *res) > +{ > +=09int resno =3D pci_resource_num(dev, res); > +=09struct pci_bus *bus =3D dev->subordinate; > +=09struct pci_dev *child; > +=09struct resource *r; > + > +=09lockdep_assert_held_write(&resource_lock); > + > +=09if (bus) { > +=09=09pci_bus_for_each_window_resource(bus, res, child, r) > +=09=09=09__pci_release_resource_tree(child, r); > +=09} > + > +=09if (release_resource_locked(res)) { > +=09=09const char *res_name =3D pci_resource_name(dev, resno); > + > +=09=09pci_dbg(dev, "%s %pR: can't release\n", res_name, res); > +=09=09return; > +=09} > + > +=09pci_resource_post_release(dev, resno); > +} > + > +/** > + * pci_release_resource_tree - Release a PCI resource and its PCI descen= dants > + * @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 transpar= ent > + * 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) > +{ > +=09struct resource *res =3D pci_resource_n(dev, resno); > + > +=09guard(write_lock)(&resource_lock); > + > +=09if (!resource_assigned(res)) > +=09=09return 0; > + > +=09if (!pci_can_release_resource_tree(dev, res)) > +=09=09return -EBUSY; > + > +=09__pci_release_resource_tree(dev, res); > + > +=09return 0; > +} > + > int pci_enable_resources(struct pci_dev *dev, int mask) > { > =09u16 cmd, old_cmd; >=20 --8323328-740981936-1796170974=:26849--