Re: [PATCH] PCI: Don't report fully optional resources as assignment failures

From: Ilpo Järvinen

Date: Tue Jul 28 2026 - 07:07:09 EST


On Mon, 27 Jul 2026, Anthony Pighin wrote:

> v2: https://lore.kernel.org/linux-pci/20260727193714.598562-1-anthony.pighin@xxxxxxxxx/
>
> On Mon, 20 Jul 2026, Ilpo Järvinen wrote:
>
> > This part seems valid, the resource should not be left into enlarged
> > state if the assignment fails.
>
> Kept in v2. reassign_resources_sorted() saves the required size and
> restores it when the optional attempt fails.
>
> > > + if (fail_head && resource_size(res) &&
> > > + !pci_resource_is_optional(dev, pci_resource_num(dev, res))) {
> >
> > This, however, will break some cases I think.
> >
> > We first want to try fitting also optional resources, if those optional
> > resources never go into fail_head, there won't be new pass and not enough
> > upstream bridge windows will be released so the algorithm won't really end
> > up even trying to fit the optional resources after this change if FW left
> > some bridge window too small.
> >
> > Only after that has been tried, it should try to fallback into fitting
> > only required resources.
>
> You're right about the pci_resource_is_optional() check. It also covers
> IOV BARs and disabled ROMs, which have a real requested size, so it would
> have kept them out of fail_head and killed the release/retry rounds they
> depend on. The effect is worse in the root bus path, where a pass failing
> only on optional resources would break the loop before the iteration that
> passes add_list. v2 drops it and keeps only the zero-size check.
>
> The case where FW left a window too small should still work, though.
> pbus_size_mem() sizes the window from the required child resources, so
> such a window has a non-zero required size and still reaches fail_head
> and the retry round. So does a child BAR that fails to fit. Only a
> window with required size 0, where nothing downstream needs that type at
> all, is skipped.

So a window which contains only optional BARs would not result another
pass to retry? While probably rare, there are such bridge windows
because we recently hit that corner case in pbus_size_mem_optional() where
the entire window was optional with required size being zero, and I had to
fix the logic there. Even if there wouldn't be such devices, I think your
reasoning is still not entirely sound.

> reassign_resources_sorted() also still tries the enlarged window where it
> always did, so the optional fit is attempted as before; v2 only restores
> the size when that attempt fails. What v2 gives up in the size-0 case is
> the extra pass, which releases live sibling windows to win headroom
> nothing requested.
>
> > I don't even know what that issue is since you don't show what happens
> > with the extra pass after release and why it fails.
>
> Apologies, v1 omitted it. v2 carries the full sequence.

Unfortunately, v2 didn't tell me what happened to igb's BAR (in the end)
as you cut the log out after the driver failed.

> The extra pass
> does not fail to assign, it succeeds, and that is the problem. The
> failing reserve is on 00:03.5, but pci_prepare_next_assign_round()
> releases the window of its parent bus, so every non-prefetchable window
> on bus 00 goes with whole_subtree, along with every BAR below them, and
> each bridge is reprogrammed right away. The reassignment only covers the
> hotplugged bridge, so none of that is restored. 00:03.5 ends up with a
> 2MB window nothing asked for, and the igb loses its BARs while its driver
> is bound.

So the entire fitting and assignment operation moved the igb BAR or igb's
BAR is entirely gone (partial assignment failure)?

In any case, the resource fitting algorithm should not be touching BARs
that are in use! ...But we likely lack check for that, which should be
fixed. I'm not even entirely sure though how that can be checked, is it
enough the check if a BAR has at least one child resource?

I would not be very surprised if there are other similar problems that
can only occur in hotplug case where it goes to tear down something that
is in use. For bridge windows, there's some attempt to prevent releasing
windows that still have children, but for the actual BARs, I don't recall
seeing effort to prevent releasing them. Maybe it could be placed into
something generic such as pci_release_resource(), not all its callers seem
to handle errors from it though so some extra work to deal with those is
required as well.

And as mentioned in v2 email, release_child_resources() works on a wrong
level to be able to detect things properly so a PCI level replacement may
have to be added to be able to access PCI level structs to know what
resource is BAR, bridge window and what is a child resource for BAR.

--
i.