From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.10 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795648243; b=dyBhc8aPIvgmaxdyxmgyJaKmX6jbbqCyYL6EehaPH5L6n9+//tiy0igWsIO1Ekofg+st 4jv9KqJTly0p0r/mTm//us0LfehvO8p057KY0tlUEqMfdc/euFKzX6hIOrDPgR96T0Yys ZE1MOuGUY9YA5vk82ySyAVJ8It3cRuYTFQK67wvN9eP0tKDC73KrD+usDwbIH5mre0oT+ lZceFOdcvCdRtU/kuSSqr2i3xtdMnjqQvE0wvNL6zSgdnr/F2KM913/EzqpCftlE3Jd6L CquIc1kFGIyR93I3q7c1Y6A/46VHHdRSlM1fOpH8A50QXTuIPtJStMDRY32CqL0MiAQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795648243; 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=oTH0T+j9mxYF9pgNoL09Y/KCz0muKuE0+CC2lvJMHlc=; b=d/DpZ3dHaKqiz8cIpDN4rO/bJ92fRifoF3rtmr+xaFI9uM1E6f50C2YbAdklfNU0JRXF 4DT2HAE4Sp48cVG8d0RMomKIaLeKCvYHZPCoM5popUy5ngE3sdIGxSVdz+vkw9sQGT9mh oPb6YXEKnspBoYeixICR8AdoJaQ6DJUU1+rvcn5lq2hayj8sHbCLQUd55IBYcFhqCRHR/ e4QjxcnU2Uwam1XFc3EBd5WUTNQm44s8LVR4km/yKFx3TPTNgjUMEndjxG/fZQdi8miAy rpcJJKwx5KuOt6bgEGmQpp26mJdUbO9KRxgVDjIt+1iwkntgbb6Mit6B2tqfYrLr4Qw== 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=kPaYig3M; arc=none smtp.remote-ip=192.198.163.10 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 CFD4C606A321; Wed, 25 Nov 2026 23:10:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CFD4C606A321 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=kPaYig3M DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1795648243; x=1827184243; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eAZIIz6Vqb0Uazy7AggTrqHt708HzfClkhEbTdowxR0=; b=kPaYig3MV7xF8QGyEdEB2VFGeDAvUFcO/laTveO3KCDY/CSREg0NKnju Rp1I9QDwWXWIP0AHwtqQwfjXOON6m+Fg8TB981qzPTPpNWE31HpY5ora4 fnz2PLbTvdcnNV4qTQOvhgoPb10HNNRKNaKyRixWT4bOO7PJIOvYI6aca rH3MxfnHmUjwXPAqTrXhYyODe3vXsN1alJt7Otp27PfwhTEHyeieW3E2w /wFhgdRY7U71EfN9gDqysofnJYf75RrXIhzQvqSwZw+pLKauqP9ntnFcZ ZlWZe+pbwYWbOa4dFzhiWNIxuqEAgI3CE4+AFk4jIQA1V/Pos5TWu6pqd w==; X-CSE-ConnectionGUID: rjfaFzihTo+AMvJuqc4tAA== X-CSE-MsgGUID: H/AR18P7QvSQYU6L4R8Lxg== X-IronPort-AV: E=McAfee;i="6800,10657,11977"; a="4435193" X-IronPort-AV: E=Sophos;i="6.27,233,1787036400"; d="scan'208";a="4435193" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Nov 2026 15:10:42 -0800 X-CSE-ConnectionGUID: 0LIh+YUNRh+Q9qlKeUj3TQ== X-CSE-MsgGUID: SRk85ME0QHKNJgSvlLbI+w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,233,1787036400"; d="scan'208";a="12283493" Received: from ijarvine-mobl1.ger.corp.intel.com (HELO localhost) ([10.245.244.96]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Nov 2026 15:10:39 -0800 From: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= To: linux-pci@vger.kernel.org, Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , David Matlack , "Rafael J . Wysocki" , linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , stable@vger.kernel.org Subject: [PATCH v3 06/17] PCI: Hold pci_bus_sem across sizing and assignment entrypoints Date: Thu, 26 Nov 2026 01:09:05 +0200 Message-ID: <20261125230925.2441-7-ilpo.jarvinen@linux.intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261125230925.2441-1-ilpo.jarvinen@linux.intel.com> References: <20261125230925.2441-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 The bridge window sizing and resource assignment algorithm walks bus->devices and inserts resources of the found devices into the resource tree in many places. In addition, its internal datastructures hold references to devices and resources it encountered while walking. Most entrypoints to the algorithm do not hold pci_bus_sem, and pci_assign_unassigned_bus_resources() drops it before the assignment phase. pci_rescan_remove_lock is not held by all callers and cannot be taken by callers running under device_lock(). Protect the walks by holding pci_bus_sem for read across the entire sizing and assignment operation right at the entrypoints. Signed-off-by: Ilpo Järvinen Cc: stable@vger.kernel.org --- drivers/pci/setup-bus.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c index 7d135ddd93b2..966e0f243032 100644 --- a/drivers/pci/setup-bus.c +++ b/drivers/pci/setup-bus.c @@ -1459,6 +1459,7 @@ void __pci_bus_size_bridges(struct pci_bus *bus, struct list_head *realloc_head) void pci_bus_size_bridges(struct pci_bus *bus) { + guard(rwsem_read)(&pci_bus_sem); __pci_bus_size_bridges(bus, NULL); } EXPORT_SYMBOL(pci_bus_size_bridges); @@ -1551,6 +1552,7 @@ void __pci_bus_assign_resources(const struct pci_bus *bus, void pci_bus_assign_resources(const struct pci_bus *bus) { + guard(rwsem_read)(&pci_bus_sem); __pci_bus_assign_resources(bus, NULL, NULL); } EXPORT_SYMBOL(pci_bus_assign_resources); @@ -1839,7 +1841,7 @@ static enum enable_type pci_realloc_detect(struct pci_bus *bus, if (host->preserve_config) return auto_disabled; - pci_walk_bus(bus, iov_resources_unassigned, &unassigned); + pci_walk_bus_locked(bus, iov_resources_unassigned, &unassigned); if (unassigned) return auto_enabled; @@ -2192,6 +2194,8 @@ void pci_assign_unassigned_root_bus_resources(struct pci_bus *bus) int pci_try_num = 1; enum enable_type enable_local; + guard(rwsem_read)(&pci_bus_sem); + /* Don't realloc if asked to do so */ enable_local = pci_realloc_detect(bus, pci_realloc_enable); if (pci_realloc_enabled(enable_local)) { @@ -2260,14 +2264,15 @@ void pci_assign_unassigned_resources(void) } } -void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) +static void pci_bridge_size_and_assign_resources(struct pci_dev *bridge) { struct pci_bus *parent = bridge->subordinate; /* List of resources that want additional resources */ LIST_HEAD(add_list); int tried_times = 0; LIST_HEAD(fail_head); - int ret; + + guard(rwsem_read)(&pci_bus_sem); while (1) { __pci_bus_size_bridges(parent, &add_list); @@ -2294,6 +2299,13 @@ void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) pci_prepare_next_assign_round(&fail_head, tried_times, whole_subtree); } +} + +void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) +{ + int ret; + + pci_bridge_size_and_assign_resources(bridge); ret = pci_reenable_device(bridge); if (ret) @@ -2427,7 +2439,7 @@ static int __pci_do_resource_release_and_resize(struct pci_dev *pdev, goto restore; } else { /* No bridge window to adjust; let the core reassign the bus. */ - pci_bus_assign_resources(bus); + __pci_bus_assign_resources(bus, NULL, NULL); list_for_each_entry(dev_res, &saved, list) { if (!resource_assigned(dev_res->res)) { @@ -2504,7 +2516,7 @@ void pci_assign_unassigned_slot_resources(struct pci_bus *bus, u16 slot) /* List of resources that want additional resources */ LIST_HEAD(add_list); - down_read(&pci_bus_sem); + guard(rwsem_read)(&pci_bus_sem); for_each_pci_bridge(dev, bus) { if (slot != PCI_SLOT_ALL_DEVICES && PCI_SLOT(dev->devfn) != slot) @@ -2513,7 +2525,6 @@ void pci_assign_unassigned_slot_resources(struct pci_bus *bus, u16 slot) if (pci_has_subordinate(dev)) __pci_bus_size_bridges(dev->subordinate, &add_list); } - up_read(&pci_bus_sem); __pci_bus_assign_resources(bus, &add_list, NULL); } @@ -2525,7 +2536,8 @@ EXPORT_SYMBOL_GPL(pci_assign_unassigned_bus_resources); void pci_bus_size_and_assign_resources(struct pci_bus *bus) { - pci_bus_size_bridges(bus); + guard(rwsem_read)(&pci_bus_sem); + __pci_bus_size_bridges(bus, NULL); __pci_bus_assign_resources(bus, NULL, NULL); } EXPORT_SYMBOL_GPL(pci_bus_size_and_assign_resources); -- 2.47.3