Intel-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>,
	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 16/16] PCI: Release only PCI resources when releasing a resource tree
Date: Sat, 05 Dec 2026 01:11:42 +0000	[thread overview]
Message-ID: <sashiko-outbox-250885@kernel.org> (raw)
In-Reply-To: <20261205003957.3304-17-ilpo.jarvinen@linux.intel.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] PCI: BAR resize rollback fails on IORESOURCE_PCI_FIXED
  resources
- [Medium] PCI: pci_release_resource_tree() walks bus->devices without
  pci_bus_sem
- [Medium] PCI: pci_release_resource_tree() calls pci_info() under
  resource_lock
- [Low] PCI: pci_bridge_release_resources() discards -EBUSY causing
  repeated rescans
--

commit b918bf89459fc478f5ebfe229a3a0bbeb92b6ca2
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

PCI: Release only PCI resources when releasing a resource tree

This commit adds pci_release_resource_tree() to check and release only PCI
resources underneath a bridge window under resource_lock, and switches
pci_bridge_release_resources() and BAR resize rollback to use it.

Link: https://lore.kernel.org/linux-pci/20260727193714.598562-1-anthony.pighin@nokia.com/

> diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> index 033f5fab7cc06..e14066c6aa8d8 100644
> --- a/drivers/pci/setup-bus.c
> +++ b/drivers/pci/setup-bus.c
> @@ -1666,10 +1666,7 @@ static void pci_bridge_release_resources(struct pci_bus *bus,
>  
>  	idx = pci_resource_num(dev, b_win);
>  
> -	/* If there are children, release them all */
> -	release_child_resources(b_win);
> -
> -	ret = pci_release_resource(dev, idx);
> +	ret = pci_release_resource_tree(dev, idx);
>  	if (ret)
>  		return;

[Severity: Low]
Should pci_bridge_release_resources() propagate the -EBUSY failure instead
of discarding ret?

When a descendant is pinned, pci_release_resource_tree() leaves b_win
assigned, so every ancestor bridge in pci_bus_release_bridge_resources(...,
whole_subtree) re-walks the same unreleased branch under resource_lock and
repeats the same pci_info() message.

Because b_win stays assigned, each remaining failed resource on that bus in
pci_prepare_next_assign_round() and each retry round in
pci_assign_unassigned_root_bus_resources() then repeats those rescans and
duplicate logs.

[ ... ]

> @@ -2472,8 +2469,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
>  		if (!resource_assigned(res))
>  			continue;
>  
> -		release_child_resources(res);
> -		pci_release_resource(dev, i);
> +		pci_release_resource_tree(dev, i);
>  	}
>  
>  	/* Restore parents before their children */
> @@ -2483,6 +2479,10 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
>  
>  		i = pci_resource_num(dev, res);
>  
> +		/* All saved resources were released once before the resize */
> +		if (WARN_ON_ONCE(resource_assigned(res)))
> +			continue;
> +
>  		pci_dev_res_restore(dev_res);
>  
>  		if (pci_claim_resource(dev, i))

[Severity: Medium]
Can BAR resize rollback fail here when an IORESOURCE_PCI_FIXED resource
shares the resized bridge window?

Before resizing, pci_do_resource_release_and_resize() releases pdev's BARs
with pci_release_resource(), which allows releasing IORESOURCE_PCI_FIXED
resources.  During the failed resize attempt,
__pci_bus_assign_resources_one() -> pdev_assign_fixed_resources() re-claims
that fixed BAR (or claims a sibling's fixed resource inside a moved window).

On rollback, pci_can_release_resource_tree() rejects fixed resources and
ancestor windows containing them, so pci_release_resource_tree() returns
-EBUSY.  The restore loop then hits WARN_ON_ONCE(resource_assigned(res))
and skips restoring the window, stranding other BARs if the window moved.

Also, releasing an IORESOURCE_PCI_FIXED BAR via pci_release_resource() before
the resize can unpin its parent bridge window even on a successful resize.

> diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> index 82efd4a76c153..848d05fbe76fe 100644
> --- a/drivers/pci/setup-res.c
> +++ b/drivers/pci/setup-res.c
> @@ -502,6 +502,113 @@ int pci_release_resource(struct pci_dev *dev, int resno)

[ ... ]

> +	if (bus) {
> +		pci_bus_for_each_window_resource(bus, res, child, r) {
> +			pci_children++;
> +			if (!pci_can_release_resource_tree(child, r))
> +				return false;
> +		}
> +	}

[Severity: Medium]
Is it safe to walk bus->devices here and in __pci_release_resource_tree()
without holding pci_bus_sem?

CPU0                              CPU1
pci_host_probe()
  ...
  pci_bridge_release_resources()
    pci_release_resource_tree()
      write_lock(&resource_lock)
                                  remove_store()
                                    ...
                                    pci_destroy_dev()
                                      down_write(&pci_bus_sem)
                                      list_del(&dev->bus_list)
                                      up_write(&pci_bus_sem)
      pci_bus_next_window_dev()
        reads dev->bus_list  <-- LIST_POISON1 / use-after-free

pci_do_resource_release_and_resize() holds down_read(&pci_bus_sem), but
pci_bridge_release_resources() does not, and callers like pci_host_probe()
or acpi_pci_root_add() reach it without pci_rescan_remove_lock either
(SR-IOV probe/remove paths also mutate bus->devices under pci_bus_sem).

pci_destroy_dev() unlinks dev->bus_list before acquiring resource_lock in
pci_free_resources(), so resource_lock does not prevent list corruption or
use-after-free.  Asserting lockdep_assert_held(&pci_bus_sem) here and
taking down_read(&pci_bus_sem) in pci_bridge_release_resources() fixes it.

[ ... ]

> +static void __pci_release_resource_tree(struct pci_dev *dev,
> +					struct resource *res)
> +{
> +	int resno = pci_resource_num(dev, res);
> +	struct pci_bus *bus = dev->subordinate;
> +	struct pci_dev *child;
> +	struct resource *r;
> +
> +	lockdep_assert_held_write(&resource_lock);
> +
> +	if (bus) {
> +		pci_bus_for_each_window_resource(bus, res, child, r)
> +			__pci_release_resource_tree(child, r);
> +	}
> +
> +	if (release_resource_locked(res)) {
> +		const char *res_name = pci_resource_name(dev, resno);
> +
> +		pci_dbg(dev, "%s %pR: can't release\n", res_name, res);
> +		return;
> +	}
> +
> +	pci_resource_post_release(dev, resno);
> +}

[ ... ]

> +int pci_release_resource_tree(struct pci_dev *dev, int resno)
> +{
> +	struct resource *res = pci_resource_n(dev, resno);
> +
> +	guard(write_lock)(&resource_lock);
> +
> +	if (!resource_assigned(res))
> +		return 0;
> +
> +	if (!pci_can_release_resource_tree(dev, res))
> +		return -EBUSY;
> +
> +	__pci_release_resource_tree(dev, res);
> +
> +	return 0;
> +}

[Severity: Medium]
Can holding write_lock(&resource_lock) across the pci_info() calls in
pci_resource_post_release() and pci_can_release_resource_tree() stall other
CPUs on slow consoles?

At default console_loglevel, LOGLEVEL_INFO messages flush synchronously on
legacy framebuffer or serial consoles via console_unlock(), holding the
spinning resource_lock with preemption disabled for tens to hundreds of
milliseconds while other CPUs spin in ioremap() or /proc/iomem.

Previously, release_child_resources() only logged at KERN_DEBUG under
resource_lock and pci_release_resource() called pci_info() after dropping
it.

Also, unlike pci_can_release_resource_tree(), __pci_release_resource_tree()
does not check res->child before entering pci_bus_for_each_window_resource(),
scanning subordinate devices even when res has no children.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205003957.3304-1-ilpo.jarvinen@linux.intel.com?part=16

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

Thread overview: 27+ 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
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 [this message]
2026-12-05  1:57     ` Ilpo Järvinen
2026-12-05  2:06 ` ✓ i915.CI.BAT: success for PCI: Resource release fixes and improvements (rev2) 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=sashiko-outbox-250885@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --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