All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chengwen Feng" <fengchengwen@huawei.com>
Cc: linux-pci@vger.kernel.org
Subject: Re: [PATCH v11 4/5] vfio/pci: Add PCIe TPH configuration space virtualization
Date: Mon, 18 May 2026 08:48:09 +0000	[thread overview]
Message-ID: <20260518084809.E78BDC2BCC6@smtp.kernel.org> (raw)
In-Reply-To: <20260518071701.25177-5-fengchengwen@huawei.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] Unsafe PCI configuration space access during PM state transition (Race condition leading to Unsupported Request).
- [High] Dropped configuration writes to the TPH capability break ST Table and Control Register virtualization.
- [High] Host kernel API overrides the guest's explicit TPH Request Enable state, preventing the guest from disabling TPH requests.
--

commit 5ada31d58b6a34d641ac5392c15e54c3404870a2
Author: Chengwen Feng <fengchengwen@huawei.com>

vfio/pci: Add PCIe TPH configuration space virtualization

This commit adds support for virtualizing the PCIe TPH control register.
It introduces a module parameter to globally control the TPH feature and
mediates TPH control register writes to only allow valid mode settings.

> diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
> --- a/drivers/vfio/pci/vfio_pci_config.c
> +++ b/drivers/vfio/pci/vfio_pci_config.c
[ ... ]
> @@ -313,6 +316,30 @@ static int vfio_virt_config_read(struct vfio_pci_core_device *vdev, int pos,
>  	return count;
>  }
>  
> +static int vfio_pci_tph_config_write(struct vfio_pci_core_device *vdev, int pos,
> +				     int count, struct perm_bits *perm,
> +				     int offset, __le32 val)
> +{
> +	u32 data = le32_to_cpu(val);
> +
> +	guard(mutex)(&vdev->tph_lock);
> +
> +	if (!enable_unsafe_tph)
> +		return count;
> +
> +	if (offset != PCI_TPH_CTRL)
> +		return count;

Does this function silently discard ST Table and other TPH capability writes?

By unconditionally returning count without actually modifying the virtual
configuration space (vconfig) or the physical hardware, guest writes to the ST
Table, the ST Table Select field, or unaligned byte-writes to the control
register are dropped.

Since there is no .readfn, reads will fetch directly from the physical hardware
returning unconfigured values, which could break guest OS configurations.

> +
> +	/* Only permit write TPH mode. */
> +	data &= PCI_TPH_CTRL_MODE_SEL_MASK;
> +	if (data == PCI_TPH_ST_IV_MODE || data == PCI_TPH_ST_DS_MODE)
> +		pcie_enable_tph(vdev->pdev, data);

If the guest clears the 'TPH Requester Enable' field but leaves a valid mode
in PCI_TPH_CTRL_MODE_SEL_MASK, does this call to pcie_enable_tph() override
the guest's request?

pcie_enable_tph() force-enables the feature in physical hardware, which prevents
the guest from disabling it.

Additionally, if the guest changes active modes, pcie_enable_tph() will return
-EBUSY and fail silently without informing the user.

> +	else if (data == PCI_TPH_ST_NS_MODE)
> +		pcie_disable_tph(vdev->pdev);
> +
> +	return count;
> +}
> +
>  static struct perm_bits direct_ro_perms = {
[ ... ]
> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
[ ... ]
> @@ -736,6 +738,11 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)
>  #endif
>  	vfio_pci_dma_buf_cleanup(vdev);
>  
> +	/* Disable TPH when userspace closes the device FD */
> +	mutex_lock(&vdev->tph_lock);
> +	pcie_disable_tph(vdev->pdev);
> +	mutex_unlock(&vdev->tph_lock);
> +
>  	vfio_pci_core_disable(vdev);

Is it safe to access the PCI config space here?

A malicious or buggy guest could have left the device in the suspended D3 state.

vfio_pci_core_disable() wakes up the device via pm_runtime_resume(), but
calling pcie_disable_tph() before that could access a suspended device. This
would lead to an Unsupported Request (UR) and potentially a host kernel panic.

[ ... ]
> @@ -2205,6 +2214,9 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)
>  	if (!disable_idle_d3)
>  		pm_runtime_put(dev);
>  
> +	/* Disable TPH when taking over ownership of the device */
> +	pcie_disable_tph(pdev);
> +

Could calling pcie_disable_tph() after pm_runtime_put() risk accessing a
suspended device?

pm_runtime_put() drops the runtime PM reference, allowing the PM core to
asynchronously suspend the device to D3. Accessing the config space of a
suspended device violates PCIe semantics.

>  	ret = vfio_register_group_dev(&vdev->vdev);

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

  reply	other threads:[~2026-05-18  8:48 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-18  7:16 [PATCH v11 0/5] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-05-18  7:16 ` [PATCH v11 1/5] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
2026-05-18  7:16 ` [PATCH v11 2/5] PCI/TPH: Export pcie_tph_get_st_modes() for external use Chengwen Feng
2026-05-18  7:57   ` sashiko-bot
2026-05-22  4:10   ` Alex Williamson
2026-05-18  7:16 ` [PATCH v11 3/5] PCI/TPH: Add pcie_tph_enabled_mode() helper Chengwen Feng
2026-05-18  8:14   ` sashiko-bot
2026-05-22  4:10   ` Alex Williamson
2026-05-22  9:18     ` fengchengwen
2026-05-22 14:00       ` Alex Williamson
2026-05-18  7:17 ` [PATCH v11 4/5] vfio/pci: Add PCIe TPH configuration space virtualization Chengwen Feng
2026-05-18  8:48   ` sashiko-bot [this message]
2026-05-22  4:10   ` Alex Williamson
2026-05-22  9:39     ` fengchengwen
2026-05-22 14:09       ` Alex Williamson
2026-05-18  7:17 ` [PATCH v11 5/5] vfio/pci: Add VFIO_DEVICE_FEATURE_TPH_ST for TPH ST entry management Chengwen Feng
2026-05-18  9:22   ` sashiko-bot
2026-05-22  4:10   ` Alex Williamson
2026-05-22 10:04     ` fengchengwen
2026-05-22 14:27       ` Alex Williamson
2026-05-26  6:29         ` fengchengwen
2026-05-22  1:31 ` [PATCH v11 0/5] vfio/pci: Add PCIe TPH support fengchengwen

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=20260518084809.E78BDC2BCC6@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=fengchengwen@huawei.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.