From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796431422; b=WFzTT/zW0aOP2f9lpO/LgUkd7DWgSKqmJM+ImkDtSO/x6frHOscuYJGy+fRzo3ekaCgd yebzHpxkpkGaNAT9n97BLa+pOW+90F8v3HMbsJTEWqz6am3bpCmFKdp++1CLCcVv4e1k9 yg6jVi02Y+NybHxx5szK7/s8QLK7VLyrnQo1eg+xaW7fTwCFjvh8gL7KnpzTLjtvBwcia ULF4OB59JiNKS6BRCH7HlaiwttP4u9ih3/Fn5pfrxrwrG9QDLcUR70c56kzp1poQRDt6j DQLMQbdKU70xQMG2r8r8ygMx/e2/CJW4mkFkLuMeZrx7QplCppR+g3rXhcihOB+b1cg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796431422; 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=MaJLEyuweDNP8PQIxMwy1Kk6ZQfEdBl8K7k/4RIS9wE=; b=g3wTXZiayjYbfLr6nbbUt6wiI45wj/P/p2+rqhl6V8AW6Zq9S1QFgDDMGX4rWHcBali7 jf++Y0OfWPcF0CfPhO7fy31kUi86NuDPpEtRYVWeK7ars6kWsRJ1D19uQTx725Ts+7UGb CLSMG6WpNNrGfnT7fAWq0QWvTPlNK8rqIRqQRhxsuTAf9TXrJJSRci/Y3lherEfdZcPNk eunmg4t0h2PxUbLDKc3HB172+JqZPwwE770ZHyjnfdnXMKre6+lYWFKCxOJAnp8O8mgyL 7adPhc9AFco3ikV38+4yaYt8tvAlCGxZpDPRq6mCsuQvzQ6Z9kxmTf+UUJnO2LioYpQ== 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=jlF0Omot; arc=none smtp.remote-ip=192.198.163.18 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.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 07F38606A32C; Sat, 5 Dec 2026 00:43:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 07F38606A32C 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=jlF0Omot DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796431423; x=1827967423; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MaJLEyuweDNP8PQIxMwy1Kk6ZQfEdBl8K7k/4RIS9wE=; b=jlF0Omot2ZiXiLAKf7UFVqtHbDJIXMDSQzvmlhu1umFts7H9rZvcSwLq j8e+dtDge2EeLlS5p+pSYfs9F55ljMlRwdKCBMBQ07u8OuFFINg7RTDDD 6lu1F+RVkER2m+ecnxYvcD1znpLlZiACMTMD/BczNZlElnnAwckuzAUrC FgytAMOIAefq8UGk0252xjHhd3z6SwOYtvLXNM4KIK5TrPNel4l5BR0rf FGr/u0UqMFU92kK1m0gCG6m0PnuHiOUjA+jlDlmPTY3wo+4q18msUD9CZ KtjyusLFl0eD5OHxIkNxwkUcAtR73WmDh9lZ12/Mn9J6Vt6O1C6fy09QC Q==; X-CSE-ConnectionGUID: gDN9mkADQLej8Z/ymNS7hQ== X-CSE-MsgGUID: YhrPX9aiSkemy7Re7007gg== X-IronPort-AV: E=McAfee;i="6800,10657,11986"; a="5429796" X-IronPort-AV: E=Sophos;i="6.29,79,1794902400"; d="scan'208";a="5429796" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 16:43:42 -0800 X-CSE-ConnectionGUID: KHCdOu98Ssa472IY9mWC+g== X-CSE-MsgGUID: kpyP6r1xRAiXVn7NGMjCCQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,79,1794902400"; d="scan'208";a="14102077" Received: from ijarvine-mobl1.ger.corp.intel.com (HELO localhost) ([10.245.245.47]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 16:43:34 -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, Anthony Pighin , Yinghai Lu , Jesse Barnes , Alex Chiang , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= Subject: [PATCH v2 16/16] PCI: Release only PCI resources when releasing a resource tree Date: Sat, 5 Dec 2026 02:39:51 +0200 Message-ID: <20261205003957.3304-17-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com> References: <20261205003957.3304-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. Also fail if any of the resources is IORESOURCE_PCI_FIXED, as it cannot follow the window should the window move. 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. Fixes: 5009b46025ac ("PCI: add pci_bridge_release_resources and pci_bus_release_bridge_resources") Link: https://lore.kernel.org/linux-pci/20260727193714.598562-1-anthony.pighin@nokia.com/ Reported-by: Anthony Pighin Signed-off-by: Ilpo Järvinen --- drivers/pci/pci.h | 1 + drivers/pci/setup-bus.c | 12 ++--- drivers/pci/setup-res.c | 107 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 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 033f5fab7cc0..e14066c6aa8d 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; @@ -2472,8 +2469,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 */ @@ -2483,6 +2479,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 82efd4a76c15..848d05fbe76f 100644 --- a/drivers/pci/setup-res.c +++ b/drivers/pci/setup-res.c @@ -502,6 +502,113 @@ 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. So does an IORESOURCE_PCI_FIXED resource anywhere in the + * tree. + */ +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); + + if (res->flags & IORESOURCE_PCI_FIXED) { + int resno = pci_resource_num(dev, res); + const char *res_name = pci_resource_name(dev, resno); + + pci_info(dev, "%s %pR: can't release; fixed resource\n", + res_name, res); + return false; + } + + 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