Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: sashiko-reviews@lists.linux.dev
Cc: dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	 intel-gfx@lists.freedesktop.org, intel-xe@lists.freedesktop.org,
	 linux-pci@vger.kernel.org
Subject: Re: [PATCH v2 09/16] PCI: Do not release resources with children in pci_release_resource()
Date: Sat, 5 Dec 2026 03:29:03 +0200 (EET)	[thread overview]
Message-ID: <9ee27603-b691-ad50-c69b-04018e09970d@linux.intel.com> (raw)
In-Reply-To: <sashiko-outbox-250879@kernel.org>

[-- Attachment #1: Type: text/plain, Size: 5949 bytes --]

On Sat, 5 Dec 2026, sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 3 potential
> issues to consider:
> - [Medium] PCI: unchecked pci_release_resource() failure in
>   __assign_resources_sorted()
> - [Low] PCI: pci_release_resource() misreports -EINVAL as busy on parent
>   TOCTOU race
> - [Low] PCI: missing Fixes trailer for pci_release_resource() bug fix
> --
> 
> commit ed0359ff94feed20c50628f709669e28b10a5020
> Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> 
> > PCI: Do not release resources with children in pci_release_resource()
> >
> > pci_release_resource() releases the resource using release_resource()
> > which detaches the resource from the resource tree together with its
> > descendants. The descendants remain attached to the released resource
> > and keep their addresses. Once the resource is assigned elsewhere, the
> > descendants may end up outside of their parent, corrupting the resource
> > tree.
> >
> > None of the callers wants that. pbus_reassign_bridge_resources() only
> > releases bridge windows that have no children. The BAR resize rollback
> > releases the children before releasing the resource itself. The BARs
> > released before BAR resize, however, are released even if they contain
> > regions requested by a driver, and the resize may then move such a BAR
> > from underneath those regions.
> >
> > Use release_leaf_resource() in pci_release_resource() to return -EBUSY
> > if the resource has children. Remove a BAR that cannot be released from
> > the saved resources so that the BAR resize rollback leaves it alone, and
> > fail BAR resize if it is the resized BAR.
> >
> > pbus_reassign_bridge_resources() checks for children without holding
> > resource_lock, so a child may still appear before the release. Remove
> > such a bridge window from the saved resources too so that the rollback
> > does not release the resources underneath it.
> >
> > As the release can now fail, log both outcomes.
> >
> > Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> 
> [Severity: Low]
> This looks like a bug fix for pci_release_resource() detaching resources
> that still have children.  Should this include:
> 
>   Fixes: 8bb705e3e79d ("PCI: Add pci_resize_resource() for resizing BARs")
> 
> [ ... ]
> 
> > diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> > index 376f09630a4ab..1317d645a178a 100644
> > --- a/drivers/pci/setup-res.c
> > +++ b/drivers/pci/setup-res.c
> > @@ -472,11 +472,15 @@ int pci_release_resource(struct pci_dev *dev, int resno)
> >  	if (!res->parent)
> >  		return 0;
> >  
> > -	pci_info(dev, "%s %pR: releasing\n", res_name, res);
> > -
> > -	ret = release_resource(res);
> > -	if (ret)
> > +	ret = release_leaf_resource(res);
> 
> [Severity: Medium]
> Now that pci_release_resource() can return -EBUSY, does
> __assign_resources_sorted() need to handle a release failure too?
> 
> Its fallback path ignores the return value and leaves dev_res on head,
> save_head, and realloc_head:
> 
> drivers/pci/setup-bus.c:__assign_resources_sorted() {
> 	...
> 	/* Release assigned resource */
> 	list_for_each_entry(dev_res, head, list) {
> 		res = dev_res->res;
> 		dev = dev_res->dev;
> 
> 		pci_release_resource(dev, pci_resource_num(dev, res));
> 		pci_dev_res_restore(dev_res);
> 	}
> 	...
> }
> 
> Because pci_do_resource_release_and_resize() only holds a read lock on
> pci_bus_sem, a concurrent bus rescan can insert a child into a newly
> assigned bridge window before that fallback loop runs:
> 
> CPU0                                  CPU1
> pci_do_resource_release_and_resize()
>   down_read(&pci_bus_sem)
>   ...
>   __assign_resources_sorted()
>     assign_requested_resources_sorted()
>       /* assigns bridge window res,
>        * another resource fails */
>                                       pci_rescan_bus()
>                                         pci_assign_unassigned_bus_resources()
>                                           down_read(&pci_bus_sem)
>                                           ...
>                                           allocate_resource()
>                                             /* inserts child under res */
>     pci_release_resource(...)  <-- -EBUSY, ignored
>     pci_dev_res_restore(dev_res)  <-- WARN_ON_ONCE(resource_assigned(res))
>     assign_requested_resources_sorted()
>       pci_assign_resource(...)  <-- sets IORESOURCE_UNSET on live res
> 
> When release fails, res stays in the resource tree and hits
> WARN_ON_ONCE(resource_assigned(res)) in pci_dev_res_restore().  The retry
> then calls pci_assign_resource(), which sets IORESOURCE_UNSET before
> reallocate_resource() fails with -EBUSY, leaving the live window marked
> IORESOURCE_UNSET.
> 
> If a firmware address is available, pci_revert_fw_address() can also mutate
> res->start and res->end without resource_lock while res is still linked in
> the tree.  Removing dev_res from head, save_head, and realloc_head when
> pci_release_resource() fails would avoid this.
> 
> > +	if (ret) {
> > +		pci_info(dev, "%s %pR: can't release; contains resources in use\n",
> > +			 res_name, res);
> >  		return ret;
> > +	}
> 
> [Severity: Low]
> Should this distinguish -EINVAL from -EBUSY?
> 
> res->parent is checked without resource_lock, so if res is detached before
> release_leaf_resource() acquires the lock, __release_resource() returns
> -EINVAL instead of -EBUSY.  pci_release_resource() then logs that res
> contains resources in use and returns -EINVAL rather than 0, causing
> pci_do_resource_release_and_resize() to drop it from saved and abort the
> resize if it is the resized BAR.

Neither of the problems is in the scope for this series, see the cover 
letter for details (which sashiko clearly failed to do :-( ).

-- 
 i.

  reply	other threads:[~2026-12-05  1:29 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  0:39 [PATCH v2 00/16] PCI: Resource release fixes and improvements Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 01/16] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 02/16] PCI: Set new BAR Size only after releasing the BARs for BAR resize Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 03/16] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
2026-12-05  1:04   ` sashiko-bot
2026-12-05  1:25     ` Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 04/16] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
2026-12-05  1:04   ` sashiko-bot
2026-12-05  0:39 ` [PATCH v2 05/16] resource: Check that the resource is in the tree when releasing it Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 06/16] resource, kunit: Add test for releasing a resource not in the tree Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 07/16] resource: Add release_leaf_resource() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 08/16] resource, kunit: Add test for release_leaf_resource() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 09/16] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
2026-12-05  1:04   ` sashiko-bot
2026-12-05  1:29     ` Ilpo Järvinen [this message]
2026-12-05  0:39 ` [PATCH v2 10/16] resource: Rename next_resource() and for_each_resource() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 11/16] resource: Make resource_lock and resource tree traversal available Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 12/16] resource: Add release_resource_locked() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 13/16] PCI: Split pci_resource_post_release() out of pci_release_resource() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 14/16] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
2026-12-05  1:02   ` sashiko-bot
2026-12-05  1:12     ` Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 15/16] PCI: Keep the range of fixed resources when releasing them Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 16/16] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
2026-12-05  1:11   ` sashiko-bot
2026-12-05  1:57     ` Ilpo Järvinen
2026-12-05  0:50 ` ✗ CI.checkpatch: warning for PCI: Resource release fixes and improvements (rev2) Patchwork
2026-12-05  0:52 ` ✓ CI.KUnit: success " Patchwork
2026-12-05  1:27 ` ✓ Xe.CI.BAT: " Patchwork

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=9ee27603-b691-ad50-c69b-04018e09970d@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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