Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Srirangan Madhavan" <smadhavan@nvidia.com>,
	linux-cxl@vger.kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH v18 14/19] PCI/CXL: Reject reset with unsafe function scope
Date: Sat, 05 Dec 2026 04:43:42 +0000	[thread overview]
Message-ID: <sashiko-outbox-251095@kernel.org> (raw)
In-Reply-To: <20261205041453.3946529-15-smadhavan@nvidia.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] PCI/CXL: bus reset precedes cxl_bus and bypasses
  cxl_reset_bus_function()
- [Medium] PCI/CXL: !probe ARI guard falsely advertises cxl_reset and
  cxl_bus
- [Medium] PCI/CXL: unlocked list_is_singular() read of bus->devices
  without pci_bus_sem
- [Medium] PCI/CXL: probe-time scope check misses fn > 0 with MFD=0 and
  ARI fn 0 in sysfs
- [Low] PCI/CXL: unlocked pci_num_vf() read in reset_method_store()
  probe path
- [Low] PCI/CXL: duplicated function-scope guard in cxl.c and pci.c
- [Low] PCI/CXL: missing Fixes trailer for cxl_reset_bus_function()
  scope bug
--

commit edf4c40d8768b9c15e4a2d492055de063f8458bf
Author: Srirangan Madhavan <smadhavan@nvidia.com>

> PCI/CXL: Reject reset with unsafe function scope
>
> The per-device PCI reset interface must not reset sibling functions.
> Reject cxl_reset and cxl_bus for VFs, multifunction devices, including
> function 0 of an ARI device that has other functions, and PFs with enabled
> VFs.
>
> Suggested-by: Alex Williamson <alwilliamson@nvidia.com>
> Link: https://lore.kernel.org/linux-cxl/20260626160846.426e71b6@nvidia.com/
> Link: https://lore.kernel.org/linux-cxl/6a50441e2bb5e_3cabcb1003e@djbw-dev.notmuch/
> Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
> Acked-by: Bjorn Helgaas <bhelgaas@google.com>
> Reviewed-by: Richard Cheng <icheng@nvidia.com>

[Severity: Low]
This fixes a pre-existing bug in cxl_reset_bus_function(), which could
advertise cxl_bus and reset sibling functions on VFs, multifunction devices,
or PFs with enabled VFs.  Should this include:

  Fixes: 53c49b6e6dd2 ("PCI/CXL: Add 'cxl_bus' reset method for devices below CXL Ports")

> diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
> index c90715c5a6d52..0c60f6d7bb4fa 100644
> --- a/drivers/pci/cxl.c
> +++ b/drivers/pci/cxl.c
> @@ -579,6 +579,16 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)
>  	u16 dvsec, cap, ctrl;
>  	int rc;
>  
> +	/*
> +	 * ->multifunction is not set for function 0 of an ARI device, so look
> +	 * for siblings on the bus. Skip this when probing: @pdev is not on the
> +	 * bus list yet and its siblings are not enumerated.
> +	 */
> +	if (pdev->is_virtfn || pdev->multifunction || pci_num_vf(pdev) ||
> +	    (!probe && pci_ari_enabled(pdev->bus) &&
> +	     !list_is_singular(&pdev->bus->devices)))
> +		return -ENOTTY;
> +
>  	rc = cxl_pci_device_dvsec(pdev, &dvsec, &cap);
>  	if (rc)
>  		return rc;

[Severity: Medium]
Does gating the ARI sibling check on !probe cause cxl_reset and cxl_bus to
be falsely advertised on Function 0 of an ARI multifunction device?

Because !probe skips the check, pci_init_reset_methods() records cxl_reset
and cxl_bus in dev->reset_methods[], and post-enumeration writes to sysfs
reset_method still pass PCI_RESET_PROBE in reset_method_store() instead of
returning -EINVAL.  If no other reset method is available,
pci_reset_function() then runs pci_dev_save_and_disable() (calling
err_handler->reset_prepare(), forcing D0, and clearing PCI_COMMAND) before
__pci_reset_function_locked() returns -ENOTTY.

Checking PCI_ARI_CAP_NFN on ARI Function 0 (as next_ari_fn() does) and
scanning bus->devices for any entry other than dev (as
pci_parent_bus_reset() does) avoids the !probe bypass in both
cxl_reset_function() and cxl_reset_bus_function().

[Severity: Medium]
Can non-zero functions with PCI_HEADER_TYPE_MFD clear slip past this check
during initial probe?

In pci_scan_slot(), pci_scan_single_device() runs pci_device_add() ->
pci_init_reset_methods() before dev->multifunction = 1 is set for fn > 0:

drivers/pci/probe.c:pci_scan_slot() {
    ...
	dev = pci_scan_single_device(bus, devfn + fn);
	if (dev) {
		if (!pci_dev_is_added(dev))
			nr++;
		if (fn > 0)
			dev->multifunction = 1;
	}
    ...
}

For ARI sibling functions (where PCI_HEADER_TYPE_MFD is 0) and conventional
multifunction devices that leave PCI_HEADER_TYPE_MFD clear on fn > 0,
dev->multifunction is still 0 during PCI_RESET_PROBE.  Both
cxl_reset_function() and cxl_reset_bus_function() then walk DVSEC config
space unnecessarily and record unusable cxl_reset and cxl_bus methods in
dev->reset_methods[].  Rejecting non-zero function numbers directly would
catch this during initial enumeration.

[Severity: Medium]
Is list_is_singular(&pdev->bus->devices) safe without holding pci_bus_sem?

pci_device_add() and pci_destroy_dev() mutate bus->devices under
down_write(&pci_bus_sem) without taking device_lock on sibling functions,
while callers of __pci_reset_function_locked() hold device_lock(&dev->dev)
and not pci_bus_sem:

CPU0                                CPU1
pci_reset_function(fn0)
  device_lock(&fn0->dev)
  cxl_reset_function(fn0, false)
    list_is_singular(&bus->devices) <-- data race
                                    pci_scan_single_device(bus, fn1)
                                      pci_device_add(fn1)
                                        down_write(&pci_bus_sem)
                                        list_add_tail(..., &bus->devices)
                                        up_write(&pci_bus_sem)
    cxl_reset_execute(fn0, ...)     <-- resets active sibling fn1

This is a data race on bus->devices in both cxl_reset_function() and
cxl_reset_bus_function(), and also leaves a window during reset execution
where a newly rescanned or hot-added ARI sibling can be reset while active.
Taking down_read(&pci_bus_sem) around the bus->devices check is lock-order
safe under device_lock, as pci_bridge_wait_for_secondary_bus() already does
on the reset path.

[Severity: Low]
Can calling pci_num_vf() when probe is true race with sriov_numvfs_store()?

reset_method_store() calls pci_init_reset_methods() and the reset probe
callback without holding device_lock(&pdev->dev), whereas sriov_enable()
and sriov_disable() write pdev->sriov->num_VFs under device_lock(&pdev->dev)
(as documented in sriov_numvfs_show()).  Gating the dynamic pci_num_vf()
check on !probe, where __pci_reset_function_locked() holds device_lock,
would avoid this data race in both cxl_reset_function() and
cxl_reset_bus_function().

> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index 7c6f2aba81443..5a8338340d232 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -5064,6 +5064,19 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)
>  	u16 dvsec, reg, val;
>  	int rc;
>  
> +	/*
> +	 * Reject VFs and multifunction devices before walking the bridge's
> +	 * capabilities: every VF behind the same bridge would repeat the walk.
> +	 *
> +	 * ->multifunction is not set for function 0 of an ARI device, so look
> +	 * for siblings on the bus. Skip this when probing: @dev is not on the
> +	 * bus list yet and its siblings are not enumerated.
> +	 */
> +	if (dev->is_virtfn || dev->multifunction || pci_num_vf(dev) ||
> +	    (!probe && pci_ari_enabled(dev->bus) &&
> +	     !list_is_singular(&dev->bus->devices)))
> +		return -ENOTTY;
> +
>  	bridge = pci_upstream_bridge(dev);
>  	if (!bridge)
>  		return -ENOTTY;

[Severity: Low]
This isn't a bug, but this function-scope guard and comment are duplicated
verbatim between cxl_reset_function() and cxl_reset_bus_function().
Extracting a small shared helper in drivers/pci/pci.h would keep both CXL
reset methods in sync.

[Severity: High]
Can generic bus reset still bypass cxl_reset_bus_function() and these scope
checks when SBR is already unmasked on the upstream CXL Port?

In drivers/pci/pci.c, pci_reset_fn_methods[] places pci_reset_bus_function
ahead of cxl_reset_bus_function, and pci_reset_bus_function() only returns
-ENOTTY when cxl_sbr_masked() is true:

drivers/pci/pci.c:pci_reset_bus_function() {
    ...
	if (bridge && pcie_is_cxl(bridge) && cxl_sbr_masked(bridge))
		return -ENOTTY;
    ...
}

drivers/pci/pci.c:pci_reset_fn_methods[] {
    ...
	{ pci_reset_bus_function, .name = "bus" },
	{ cxl_reset_bus_function, .name = "cxl_bus" },
}

When PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR is already set (for example by
firmware or an administrator), cxl_sbr_masked() returns false, so generic
bus reset runs first and never reaches cxl_reset_bus_function().  That
bypasses the scope checks added here, allowing hotplug slot resets of VFs,
PFs with enabled VFs, and ARI Function 0 with siblings via
pci_dev_reset_slot_function(), and also bypasses any CXL-specific reset
handling in cxl_reset_bus_function().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205041453.3946529-1-smadhavan@nvidia.com?part=14

  reply	other threads:[~2026-12-05  4:43 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 01/19] cxl: Clarify decoder interleave limit Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 02/19] cxl: Share CXL port upstream PCI device lookup Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 03/19] cxl: Move decoder declarations to shared header Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 04/19] cxl: Embed decoder configuration in a standalone structure Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 05/19] cxl: Introduce endpoint HDM decoder settings Srirangan Madhavan
2026-12-05  4:30   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 06/19] cxl: Move HDM decoder helpers to built-in code Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 07/19] cxl: Share HDM decoder register unpacking Srirangan Madhavan
2026-12-05  4:30   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 08/19] cxl: Move cxl_rwsem into built-in code Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 09/19] cxl: Refresh cached PCI HDM decoder settings Srirangan Madhavan
2026-12-05  4:38   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 10/19] cxl: Build register block helpers into the kernel Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 11/19] PCI/CXL: Cache endpoint HDM state during PCI enumeration Srirangan Madhavan
2026-12-05  4:44   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 12/19] PCI/CXL: Add CXL Device Reset sequencing Srirangan Madhavan
2026-12-05  4:38   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around reset Srirangan Madhavan
2026-12-05  4:47   ` sashiko-bot
2026-12-05  4:14 ` [PATCH v18 14/19] PCI/CXL: Reject reset with unsafe function scope Srirangan Madhavan
2026-12-05  4:43   ` sashiko-bot [this message]
2026-12-05  4:14 ` [PATCH v18 15/19] PCI/CXL: Restore CXL state after PCI reset Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 16/19] PCI/CXL: Expose CXL Reset as a PCI reset method Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 17/19] PCI/CXL: Restore CXL state after CXL bus reset Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 18/19] PCI/CXL: Flush host and device caches for " Srirangan Madhavan
2026-12-05  4:14 ` [PATCH v18 19/19] cxl: Add KUnit checks for HDM decoder unpacking Srirangan Madhavan
2026-12-05  4:38   ` sashiko-bot

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-251095@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=smadhavan@nvidia.com \
    /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