Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chengwen Feng" <fengchengwen@huawei.com>
Cc: linux-pci@vger.kernel.org, kvm@vger.kernel.org
Subject: Re: [PATCH v20 15/16] vfio/pci: Sync core PCI TPH software state across device lifecycle and resets
Date: Tue, 14 Jul 2026 11:28:34 +0000	[thread overview]
Message-ID: <20260714112834.AFB681F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260714104325.54444-16-fengchengwen@huawei.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] VFIO_DEVICE_PCI_HOT_RESET fails to synchronize software TPH state for all devices in the affected reset group.
- [High] Unlocked calls to `pcie_disable_tph()` in VFIO ioctl reset paths cause TOCTOU data races with concurrent TPH configuration.
- [High] TPH virtual configuration space (`vconfig`) remains stale after reset ioctls, permanently breaking future TPH enablement.
- [High] pcie_disable_tph() accesses PCI configuration space without holding a runtime PM reference, which can race with device suspension.
--

--- Patch [15]: [PATCH v20 15/16] vfio/pci: Sync core PCI TPH software state across device lifecycle and resets ---
commit 0b983b3e1196f13bb26ca2c1d7517ec00ad08a77
Author: Chengwen Feng <fengchengwen@huawei.com>

vfio/pci: Sync core PCI TPH software state across device lifecycle and resets

Hardware resets clear PCI TPH registers, but core PCI driver software TPH
state can become stale across device open/close and reset paths. This patch
calls pcie_disable_tph() during device enable/disable and after reset ioctls
to synchronize the software tracking state with the hardware.

> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index ae2e0e2252a1e..c509adb76955d 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
[ ... ]
> @@ -1573,9 +1579,15 @@ long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
>  	case VFIO_DEVICE_IOEVENTFD:
>  		return vfio_pci_ioctl_ioeventfd(vdev, uarg);
>  	case VFIO_DEVICE_PCI_HOT_RESET:
> -		return vfio_pci_ioctl_pci_hot_reset(vdev, uarg);
> +		ret = vfio_pci_ioctl_pci_hot_reset(vdev, uarg);
> +		if (!ret)
> +			pcie_disable_tph(vdev->pdev);

[Severity: High]
Does this successfully clear the software state for all devices in the reset
group?

When userspace invokes VFIO_DEVICE_PCI_HOT_RESET, the hardware TPH state
is cleared for all devices in the reset group. However, only the targeted
vdev has pcie_disable_tph() called.

Will other devices in the reset group retain pdev->tph_enabled == 1,
causing pcie_enable_tph_ext() to fail with -EBUSY when userspace attempts
to re-enable TPH later?


[Severity: High]
Can pcie_disable_tph() race with device suspension here?

vfio_pci_ioctl_pci_hot_reset() acquires PM runtime references for all devices
but drops them via pm_runtime_put() before returning. If the device is
configured for runtime PM, it may immediately begin suspending.

Since pcie_disable_tph() performs an unlocked pci_write_config_dword(),
does it risk accessing the PCI configuration space of a suspended device,
potentially leading to PCIe Completion Timeouts or dropped writes?

> +		return ret;
>  	case VFIO_DEVICE_RESET:
> -		return vfio_pci_ioctl_reset(vdev, uarg);
> +		ret = vfio_pci_ioctl_reset(vdev, uarg);
> +		if (!ret)
> +			pcie_disable_tph(vdev->pdev);

[Severity: High]
Is there a data race with concurrent TPH configuration?

The reset handlers drop dev_set->lock and vdev->memory_lock before
returning, allowing pcie_disable_tph() to run locklessly.

If a concurrent userspace thread writes to the TPH virtual config space, it
takes memory_lock and calls pcie_enable_tph_ext(). Can these two threads
concurrently modify PCI_TPH_CTRL and pdev->tph_enabled, corrupting the
tracking state?


[Severity: High]
Does this leave the virtual configuration space (vconfig) stale?

While pcie_disable_tph() clears the hardware state and pdev->tph_enabled,
it doesn't update vdev->vconfig.

If the guest OS later rewrites the config space to re-enable TPH,
vfio_tph_config_write() will see:

    new_ctrl = le32_to_cpu(*(__le32 *)&vdev->vconfig[start + PCI_TPH_CTRL]);
    if (new_ctrl == org_ctrl)
        goto out;

Since vconfig still contains the enabled state from before the reset, won't
this treat the write as a no-op, leaving TPH permanently disabled in hardware?

> +		return ret;
>  	case VFIO_DEVICE_SET_IRQS:
>  		return vfio_pci_ioctl_set_irqs(vdev, uarg);
>  	default:

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260714104325.54444-1-fengchengwen@huawei.com?part=15

  reply	other threads:[~2026-07-14 11:28 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-14 10:43 [PATCH v20 00/16] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-07-14 10:43 ` [PATCH v20 01/16] PCI/TPH: Add requester/completer type helpers Chengwen Feng
2026-07-14 10:54   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 02/16] dma-buf: add optional get_pci_tph() callback Chengwen Feng
2026-07-14 10:50   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 03/16] vfio/pci: implement get_pci_tph and DMA_BUF_TPH feature Chengwen Feng
2026-07-14 10:50   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 04/16] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
2026-07-14 10:54   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 05/16] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
2026-07-14 11:00   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 06/16] PCI/TPH: Cache negotiated max requester type at probe time Chengwen Feng
2026-07-14 10:57   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 07/16] PCI/TPH: Refactor pcie_enable_tph and add requester variant Chengwen Feng
2026-07-14 10:58   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 08/16] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
2026-07-14 11:00   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 09/16] PCI/TPH: Add macros to check standard and extended TPH support Chengwen Feng
2026-07-14 11:07   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 10/16] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
2026-07-14 11:11   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 11/16] PCI/TPH: Add pci_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
2026-07-14 11:27   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 12/16] vfio/pci: Add basic NO-ST TPH VFIO device feature support Chengwen Feng
2026-07-14 11:10   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 13/16] vfio/pci: Add IV-ST TPH policy and VFIO TPH_ST batch programming support Chengwen Feng
2026-07-14 11:14   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 14/16] vfio/pci: Add DS-ST TPH policy and extend TPH_RESOLVE capabilities Chengwen Feng
2026-07-14 11:12   ` sashiko-bot
2026-07-14 10:43 ` [PATCH v20 15/16] vfio/pci: Sync core PCI TPH software state across device lifecycle and resets Chengwen Feng
2026-07-14 11:28   ` sashiko-bot [this message]
2026-07-14 10:43 ` [PATCH v20 16/16] vfio/pci: Expose per-device TPH state via debugfs Chengwen Feng
2026-07-14 11:17   ` 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=20260714112834.AFB681F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=fengchengwen@huawei.com \
    --cc=kvm@vger.kernel.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