From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796140967; b=Kx7Fof+n7g5dQQEi+n1Zq7WYS7CuAKcM1vql5HGzhrU1eyKxSfGbv3toLoxOEvVTQO9Z KgpWYVpvL/igSZb2JE7uMjcqonZPaByLxuwa86wbbLoPv/17/EppAwHpQoX21Oo+N938Z xhUQuPBxovHUKo0aArsAfXZFZaP0O3TvmsN4bO3tpAWpMOFazhoyK2dLdZwCMME8P4g7c TUhB9zX3As/N4aiGdP5OwT5N3FKvziJEl0cdG06S696f7lShQpJwFxu4uirJ049XSp/mA VfMC/kMahOxsPAvNegbjC5c0zGdiS/ncC0KsY0rCthO8ALPgSpO0s8uPFay6qjdUYzg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796140967; 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:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=T5Gqe7w0ujForzUqnGnXHOxk7uyR06PIbGIxm+9OS3Y=; b=XEF+Zl8r4z0buKsMcyXAV6Cus1WHFdjIt3VG5ZC4kS59L6/jA+M7H/4XeD70+MwsAAKu iJsvl7t2rRoPGoLthf+Msxw2VRguD8GcSWurgoC2PD/9GqaqGgtp19VJwffd7mNAcQm3E 9c/t50e9t1O3J60y5Ftd/V0U7tEQlx4oAWRliUi7A7AXmOk8L6XnMrd+9yqwZY0k0NlvS av3/pHiKx1WkDCVE9KZAIiWsxDOOCLI+TzMfCT3C4bgy/EgwLkoGS1AvTLd+vtBpY+19b 8GxUJnI8zsMD0cMKHw2gQwanQ85MfUmGTiEaMyUpo+F1NCBhwPX6ZEzHKUofhAuhfsA== 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=f5OjSQlg; arc=none smtp.remote-ip=198.175.65.18 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) (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 ADBBA606A31A; Tue, 1 Dec 2026 16:02:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org ADBBA606A31A 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=f5OjSQlg DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796140967; x=1827676967; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=T5Gqe7w0ujForzUqnGnXHOxk7uyR06PIbGIxm+9OS3Y=; b=f5OjSQlgaQvIkq+l52WhgvJ9e9k8totH3AgvFDyKnMi8eG1qaAFCi4VZ 1naF1H288K9v0OWrtdC73Pi7ESO2gQobFjGVYlCWzdykWI1Ep6O1UxB2Y 9z0qZ9pYLsmbktiLqBV+JUPEH66q0g/2DP3lxFg9nArq5+PaiAg2b1CcR hApW5Z7hqFy7Gii1OEvdi8Ek10eeBHxUxOVwtnhlqKX1a80oQdmJjq7tu SuanXf3/DIsx4Ah6i531GYAhyCAA9s5lRIO6Hsh+8aU3Eq4QpeHbyrRCd eUapj7jbwauU2cMXIzGSdNrb8WGK/kmAp395swVT3owMy4pudK2h2Ov6H Q==; X-CSE-ConnectionGUID: tqKznTxRSvWtTxWbgWzFMA== X-CSE-MsgGUID: e7+sM3JpR2+qDbOUjDgn/Q== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="5033893" X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="5033893" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 08:02:15 -0800 X-CSE-ConnectionGUID: 8zDvb+TBQ0qNSEHhYuIZxA== X-CSE-MsgGUID: Uc1XK+eeTdKu3TQsYic1GQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,245,1787036400"; d="scan'208";a="13140794" 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 08:02:08 -0800 From: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= To: Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Alex Deucher , =?UTF-8?q?Christian=20K=C3=B6nig?= , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , Matthew Brost , =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= , =?UTF-8?q?Micha=C5=82=20Winiarski?= , =?UTF-8?q?Ville=20Syrj=C3=A4l=C3=A4?= , 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, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= Subject: [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree Date: Tue, 1 Dec 2026 17:58:39 +0200 Message-ID: <20261201155844.2278-16-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com> References: <20261201155844.2278-1-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: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- 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; -- 2.47.3