mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Bjorn Helgaas <bhelgaas@google.com>,
	Mika Westerberg <mika.westerberg@linux.intel.com>,
	linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	Sashiko <sashiko-bot@kernel.org>,
	stable@vger.kernel.org
Subject: [PATCH 3/3] PCI: Honor window granularity during resource distribution
Date: Mon,  9 Nov 2026 16:45:53 +0200	[thread overview]
Message-ID: <20261109144553.2516-4-ilpo.jarvinen@linux.intel.com> (raw)
In-Reply-To: <20261109144553.2516-1-ilpo.jarvinen@linux.intel.com>

When a bus below a hotplug bridge has BARs smaller than the window
granularity, removing them leaves available[i].start unaligned.
pci_bus_distribute_available_resources() then aligns the start up for the
downstream bridge but keeps the end, so the size passed to
adjust_bridge_window() has a sub-granularity tail. The bridge window ends
up not being a multiple of the window granularity.

Align the size down to the window granularity after aligning the start.

Consider a hotplug Root Port (bus 01) with a Switch Upstream Port (bus 02)
below it that has an e1000 and an ivshmem with a 64k pref BAR as its other
Functions. When the switch is hot-added (emulated with a remove and
rescan), /proc/iomem before and after this change is:

   faa00000-fe9fffff : PCI Bus 0000:01
  -    faa00000-fe8200ff : PCI Bus 0000:02
  +    faa00000-fe7fffff : PCI Bus 0000:02
         faa00000-fc8fffff : PCI Bus 0000:03
           faa00000-faa000ff : 0000:03:00.0
         fc900000-fe6fffff : PCI Bus 0000:04
  -    fe820100-fe8201ff : 0000:01:00.2
  -    fe840000-fe85ffff : 0000:01:00.1
  +    fe800000-fe81ffff : 0000:01:00.1
  +    fe820000-fe8200ff : 0000:01:00.2
  ...
     100000000-13fffffff : PCI Bus 0000:01
  -    100000000-12000ffff : PCI Bus 0000:02
  +    100000000-11fffffff : PCI Bus 0000:02
         100000000-10fffffff : PCI Bus 0000:03
           100000000-10fffffff : 0000:03:00.0
         110000000-117ffffff : PCI Bus 0000:04
  -    120010000-12001ffff : 0000:01:00.2
  +    120000000-12000ffff : 0000:01:00.2

Before, the bus 02 windows end at fe8200ff and 12000ffff, which are not 1M
aligned and thus impossible for a bridge window. As such an end cannot be
written to the bridge, the resource tree does not match what the bridge
actually uses: it decodes up to fe8fffff and 1200fffff, covering the BARs
of 01:00.1 and 01:00.2.

Fixes: 9db0b9b6a142 ("PCI: Take other bus devices into account when distributing resources")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Cc: stable@vger.kernel.org
---
 drivers/pci/setup-bus.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 9d8b22d1e6ad..5651966f3029 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -1974,7 +1974,7 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
 	unsigned int normal_bridges = 0, hotplug_bridges = 0;
 	struct pci_dev *dev, *bridge = bus->self;
 	resource_size_t per_bridge[PCI_P2P_BRIDGE_RESOURCE_NUM];
-	resource_size_t align;
+	resource_size_t align, win_align;
 	int i;
 
 	for (i = 0; i < PCI_P2P_BRIDGE_RESOURCE_NUM; i++) {
@@ -1989,10 +1989,22 @@ static void pci_bus_distribute_available_resources(struct pci_bus *bus,
 		 * window.
 		 */
 		align = pci_resource_alignment(bridge, res);
-		if (!resource_assigned(res) && align)
+		if (!resource_assigned(res) && align) {
 			available[i].start = min(ALIGN(available[i].start, align),
 						 available[i].end + 1);
 
+			/*
+			 * Small BARs on the parent bus may result in a
+			 * size that is not a multiple of the window
+			 * granularity (usually 1M), which is impossible
+			 * for a bridge window.
+			 */
+			win_align = pci_min_window_alignment(bus, res->flags);
+			resource_set_size(&available[i],
+					  ALIGN_DOWN(resource_size(&available[i]),
+						     win_align));
+		}
+
 		/*
 		 * Now that we have adjusted for alignment, update the
 		 * bridge window resources to fill as much remaining
-- 
2.47.3


      parent reply	other threads:[~2026-11-09 14:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-09 14:45 [PATCH 0/3] PCI: Fix hotplug memory distribute Ilpo Järvinen
2026-11-09 14:45 ` [PATCH 1/3] PCI: Fix NULL dereference when shrinking window without optional size Ilpo Järvinen
2026-11-09 14:45 ` [PATCH 2/3] PCI: Fix start of available space for the next bridge during distribution Ilpo Järvinen
2026-11-09 14:45 ` Ilpo Järvinen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261109144553.2516-4-ilpo.jarvinen@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=bhelgaas@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=mika.westerberg@linux.intel.com \
    --cc=sashiko-bot@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®