On Mon, 23 Nov 2026, David Matlack wrote: > On Fri Nov 20, 2026 at 5:47 AM PST, Ilpo Järvinen wrote: > > +/* > > + * Serializes bridge window sizing, resource fitting, and assignment. > > + * pci_rescan_remove_lock cannot be used for this because BAR resize and > > + * some drivers run these operations under device_lock() during probe. > > + * > > + * Context: > > + * - Nests inside pci_rescan_remove_lock and device_lock(). > > + * - Nests outside pci_bus_sem. > > + */ > > +static DEFINE_MUTEX(pci_res_setup_lock); > > Hi Ilpo, > > Could this lock live in struct pci_host_bridge instead of being global? > > As far as I can tell, a sizing and assignment pass never goes above a > root bus, and root bus windows are only allocated from, never modified. > So passes on different host bridges never touch the same resources. > Every entrypoint has a pci_bus or pci_dev to look up the host bridge > with, and pci_assign_unassigned_resources() already goes one root bus at > a time. Hi David, I don't immediately see anything preventing from moving it into the host bridge struct, I'll look into this. pci_assign_unassigned_resources() does process all root busses but sequentially so it should work too. Not sure if it really helps much currently because pci_bus_sem is also held but that might eventually change (there's the series which attempted to split that into a separate lock in each bus, I don't remember why that work stalled). > To be clear, the lock wouldn't protect any fields of struct > pci_host_bridge itself. It would protect the resource[] entries of the > devices below the host bridge, and the BAR, bridge window and Resizable > BAR registers they mirror. The host bridge is just the smallest object > that contains a whole pass. > > My motivation isn't performance. It's that global locks make it hard to > tell what data they protect. pci_rescan_remove_lock is a good example: > it's held around runtime rescans, but not around enumeration of ACPI > host bridges at boot, Not yet. ;-) ...I already have another (unsubmitted) series that adds pci_rescan_remove_lock around host bridge setups because they too can race with a sysfs /rescan leading to adding devices more than once, etc. Early enumeration obviously won't need it but to be able to add lockdep asserts to strategic places, I want to cover early init paths too. The host bridge setup locking series requires though that pci_rescan_remove_lock is reentrant (the v16 series inflight) because there are a few cases that face lock-divergent callers (VMD, some HP driver, some arch code that will be partially fixed by that v16 series, and a few others I don't now recall). The races that this series is fixing, however, are easier to hit than host bridge setup ones, because, e.g., ThunderBolt hotplug racing with some probe() initiated BAR resize does not depend on root triggering a racing rescan call through sysfs. > and that was confusing when I was working on PCI > Live Update. This is a different lock, of course, but it would be nice > to avoid adding another global lock. Now I actually started to wonder why couldn't pci_rescan_remove_lock also be per host bridge?? (+ a separate lock to protect root buses list) ...I think I'll take a look at this as well. > I tried it on top of this series to see how much it changes. Only this > patch is affected. It adds a mutex to struct pci_host_bridge, > initializes it in pci_init_host_bridge(), and uses a small helper in > setup-bus.c: > > static struct mutex *pci_res_setup_lock(struct pci_bus *bus) > { > return &pci_find_host_bridge(bus)->res_setup_lock; > } > > All of the guard() sites and most of the lockdep asserts already have a > bus or dev to pass in. __assign_resources_sorted() only gets a list, so > it would have to assert using the first entry. I can just move that assert one level up to avoid this problem. > Putting the lock in the host bridge would also make it reachable from > remove.c. pci_destroy_dev() drops pci_bus_sem after list_del() and then > calls pci_free_resources() without pci_res_setup_lock. I couldn't find a > path where this can cause bugs, but that seems like just a lucky > side-effect of the current code. It would be nice to have the new lock > explicitly cover that case too. I don't think remove can cause problems because we also hold pci_bus_sem over the entire sizing and assignment operation on purpose to keep the bus devices from coming and going underneath us. So either: 1. list_del() has already removed the dev and sizing nor assigned never sees it. 2. pci_destroy_dev() cannot remove it from list until the sizing and assignment algorithm has finished. ...So to me it looks it works as designed, and luck has nothing to do with it. :-) ...I think what you were actually suggesting is to extend this new lock to do something else than serialize sizing and assignment calls. :-) It is not meant for that, and even less so competing with pci_bus_sem's role in protecting devices in buses. > This is not a blocker by any means, just wanted to get your thoughts. I really appreciate you taking a look on this. -- i.