From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.14 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796170986; b=q5Rm5cmrqAUIvYh4nj4tgX6rNTSxX/6yaJOWES8MISyHk8vl8LhJW+MXfos//UtJYnxt 5ZKhq8h+WNN0dyZ3qZFv1ryzT/dtBR9LjrJXlrwxamispcadBVR+91fAOGqrqJLzUwr16 4yKG5K15vNG6Om5vckyfgOUaWHk08RoQPl9ZcDMl7UOUMVPxq+XoGa5l7KNiNa2vdG7sk rzHow5lQ4Mi/QAidbAMcyGKuTHu8JuC8JKl7zDnni7xzT9AFulbIiVbDT4GV3m5tHN/6Q GqJ9r+iELwryIUr04Cy4F5y6omlgaw1m52TtKpZWvmsA3i8boTLqZBe+mYL7pT5qnHQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796170986; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:Date:To:cc: Subject:In-Reply-To:Message-ID:References:MIME-Version:Content-Type; bh=WEUO+Ktf0St+4Pbt/qDLEUmz04W44hGZ8/NGGjs7VV4=; b=n6Qa9zQR6yGejO/8I//TbT3IfbwE4Tl1fPW9VaLtZirGb7+cdqBpkS7oLFLQYqqZw69k 2GuTbNKM8db8mwKrB67Ie9F3hFKKUWwd9yWM7kqmy7DGWcCB8Wmp8cT1mwWvO5kinYUHo Ez4lxla25dck19ASy7NOSmITeU4hbMC9k5ehGl8zXmU0FaKFK0vAjSs2UrW90ucKSelLL XowrOc5PkLqbwi/oyizK9VCJnjnHEMymZJFx7nZI/cDUnaog2fJGiCDeDDBNHhD+qVEL6 cv2w8A40CAamgQ81rFMNzgmnqg9PswYQ0SXVwNYSVkwDNSg8E2FDPah4XqzHbSzpZeg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=IqG2oj9y; arc=none smtp.remote-ip=198.175.65.14 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D07F1606A33D; Wed, 2 Dec 2026 00:23:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D07F1606A33D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com 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=IqG2oj9y DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796170985; x=1827706985; h=from:date:to:cc:subject:in-reply-to:message-id: references:mime-version; bh=WEUO+Ktf0St+4Pbt/qDLEUmz04W44hGZ8/NGGjs7VV4=; b=IqG2oj9yyXWF8jT6EgArHVwEpHf52LtONVXWyCOsWfC8d7/ZMp25XNzP 3KhC7M/XK1iOz23NRSDRmev7SUcOgjN3HfYc6WmaGLAgCwElVLQy9AGR3 sna5d4G1VHC1Qa0qn/nPCpZjjltgwbbrfSkmJDKGJvTDhB7JpMxIc3KWV Eo9W3zmGe7tS5l5IlxzDsx7+tEcD598Dg39WQd1WcOosZLp+3nC90fpuJ 6ksMyhX/IC4X31gBHDkoRV9Mf7HrFa4cfEnhds2GVGTEl94Wj+IZU2SuK FM1n5ASB9tbUU1I2ga7rX3FJrHgOy4DQpeO2cHNO/a5fxaHdETxRlW7lX g==; X-CSE-ConnectionGUID: p+Ej+BUoRCavtUvDitTsKg== X-CSE-MsgGUID: uXz55lnuSQKmTRGO/ygWJA== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="4988657" X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="4988657" 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-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="8323328-740981936-1796170974=:26849" 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--