From: fengchengwen <fengchengwen@huawei.com>
To: <sashiko-reviews@lists.linux.dev>
Cc: <kvm@vger.kernel.org>, <linux-pci@vger.kernel.org>
Subject: Re: [PATCH v21 09/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support
Date: Thu, 19 Nov 2026 16:21:11 +0800 [thread overview]
Message-ID: <191f1bc2-52db-4e1a-bd03-bf5e9fd5e553@huawei.com> (raw)
In-Reply-To: <sashiko-outbox-220757@kernel.org>
On 11/18/2026 6:51 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] vfio/pci: vfio_tph_config_write() fails when
> pdev->tph_enabled is already set
Fix by following:
1. Reorder the series, moving the "reset TPH state on open/close" patch earlier.
This clears `tph_enabled` when VFIO gains ownership of the device and resolves
state split after PCI reset or across user sessions.
2. Inside `vfio_tph_config_write()`, we check `pdev->tph_enabled` before enabling
a new requester mode. If TPH is already active, we call `pcie_disable_tph()`
first to avoid -EBUSY when userspace switches directly between TPH_ONLY and
EXT_TPH without an explicit DISABLE write.
Thanks
> --
>
> commit e4125940aba8b4e82c51ed4d75d938a51d421022
> Author: Chengwen Feng <fengchengwen@huawei.com>
>
> vfio/pci: Add basic NO-ST TPH VFIO device feature support
>
> This commit adds VFIO PCI support for PCIe TPH in NO-ST mode, virtualizing
> the TPH extended capability and exposing the VFIO_DEVICE_FEATURE_TPH ioctl
> for userspace opt-in.
>
>> diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
>> index 9914f3ac69aef..3946ed2ec0cfe 100644
>> --- a/drivers/vfio/pci/vfio_pci_config.c
>> +++ b/drivers/vfio/pci/vfio_pci_config.c
> [ ... ]
>> +static int vfio_tph_config_write(struct vfio_pci_core_device *vdev, int pos,
>> + int count, struct perm_bits *perm,
>> + int offset, __le32 val)
>> +{
>> + u16 start = vfio_find_cap_start(vdev, pos);
>> + struct pci_dev *pdev = vdev->pdev;
>> + u32 org_ctrl, new_ctrl, cap;
>> + u8 mode, req, org_req;
>> + __le32 ctrl_val = 0;
>> + u16 cmd;
>> + int ret;
>> +
>> + if (!vdev->tph_opt_in || offset >= PCI_TPH_BASE_SIZEOF ||
>> + offset < PCI_TPH_CTRL)
>> + return count;
>> +
>> + cmd = vfio_pci_memory_lock_and_enable(vdev);
>> +
>> + /*
>> + * Synchronize the CTRL value of the hardware to the virt. Otherwise,
>> + * the value will be inconsistent when the device is reset.
>> + */
>> + vfio_user_config_read(pdev, start + PCI_TPH_CTRL, &ctrl_val, 4);
>> + vfio_default_config_write(vdev, start + PCI_TPH_CTRL, 4, perm,
>> + PCI_TPH_CTRL, ctrl_val);
>> + org_ctrl = le32_to_cpu(ctrl_val);
>> +
>> + /* Do write operation to virt. */
>> + ret = vfio_default_config_write(vdev, pos, count, perm, offset, val);
>> + if (ret != count)
>> + goto out;
>> +
>> + /* Reads the TPH_CTRL value after the write operation. */
>> + new_ctrl = le32_to_cpu(*(__le32 *)&vdev->vconfig[start + PCI_TPH_CTRL]);
>> + if (new_ctrl == org_ctrl)
>> + goto out; /* Exit because there is no change. */
>> +
>> + cap = vdev->tph_vcap;
>> + mode = FIELD_GET(PCI_TPH_CTRL_MODE_SEL_MASK, new_ctrl);
>> + req = FIELD_GET(PCI_TPH_CTRL_REQ_EN_MASK, new_ctrl);
>> + if (mode != PCI_TPH_ST_NS_MODE || !(cap & (1u << mode)) ||
>> + req == 0x2 /* reserved */ ||
>> + (req == PCI_TPH_REQ_EXT_TPH && !(cap & PCI_TPH_CAP_EXT_TPH)))
>> + goto out; /* Drop invalid or unsupported write value. */
>> +
>> + org_req = FIELD_GET(PCI_TPH_CTRL_REQ_EN_MASK, org_ctrl);
>> + if (req == org_req)
>> + goto out; /* Only care about requester enable. */
>> +
>> + if (req == PCI_TPH_REQ_TPH_ONLY || req == PCI_TPH_REQ_EXT_TPH)
>> + pcie_enable_tph_ext(pdev, mode, req);
>> + else if (req == PCI_TPH_REQ_DISABLE)
>> + pcie_disable_tph(vdev->pdev);
>
> [Severity: Medium]
> Does pcie_enable_tph_ext() fail here if pdev->tph_enabled is already set?
>
> pcie_enable_tph_ext() returns -EBUSY whenever pdev->tph_enabled is 1, and
> its return value is ignored. If userspace switches directly between
> PCI_TPH_REQ_TPH_ONLY and PCI_TPH_REQ_EXT_TPH without an intermediate
> PCI_TPH_REQ_DISABLE write, the hardware PCI_TPH_CTRL register is never
> updated.
>
> Similarly, if a device reset clears the hardware PCI_TPH_CTRL register to 0
> while pdev->tph_enabled remains 1, subsequent writes of PCI_TPH_REQ_TPH_ONLY
> or PCI_TPH_REQ_EXT_TPH fail with -EBUSY, and writing PCI_TPH_REQ_DISABLE
> exits early at new_ctrl == org_ctrl without calling pcie_disable_tph(),
> leaving TPH unable to be re-enabled.
>
> Calling pcie_disable_tph(pdev) before pcie_enable_tph_ext() when
> pdev->tph_enabled is set would avoid both issues.
>
next prev parent reply other threads:[~2026-11-19 8:21 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-11-18 9:12 ` [PATCH v21 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
2026-11-18 9:29 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
2026-11-18 9:38 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 03/13] PCI/TPH: Cache negotiated max requester type at probe time Chengwen Feng
2026-11-18 9:45 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant Chengwen Feng
2026-11-18 9:48 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
2026-11-18 9:53 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 06/13] PCI/TPH: Add macros to check standard and extended TPH support Chengwen Feng
2026-11-18 10:06 ` sashiko-bot
2026-11-19 6:25 ` fengchengwen
2026-11-18 9:12 ` [PATCH v21 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
2026-11-18 10:15 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 08/13] PCI/TPH: Add pci_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
2026-11-18 10:29 ` sashiko-bot
2026-11-18 9:12 ` [PATCH v21 09/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support Chengwen Feng
2026-11-18 10:51 ` sashiko-bot
2026-11-19 8:21 ` fengchengwen [this message]
2026-11-19 7:50 ` kernel test robot
2026-11-18 9:12 ` [PATCH v21 10/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support Chengwen Feng
2026-11-18 11:21 ` sashiko-bot
2026-11-19 9:27 ` fengchengwen
2026-11-18 9:12 ` [PATCH v21 11/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability Chengwen Feng
2026-11-18 11:43 ` sashiko-bot
2026-11-19 11:17 ` fengchengwen
2026-11-18 9:12 ` [PATCH v21 12/13] vfio/pci: Reset PCIe TPH state on device open and close Chengwen Feng
2026-11-18 12:20 ` sashiko-bot
2026-11-19 11:30 ` fengchengwen
2026-11-18 9:12 ` [PATCH v21 13/13] vfio/pci: Expose per-device TPH state via debugfs Chengwen Feng
2026-11-18 12:27 ` 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=191f1bc2-52db-4e1a-bd03-bf5e9fd5e553@huawei.com \
--to=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 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.