Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Alex Williamson <alex@shazbot.org>
To: Chengwen Feng <fengchengwen@huawei.com>
Cc: <jgg@ziepe.ca>, <helgaas@kernel.org>,
	<wathsala.vithanage@arm.com>, <wei.huang2@amd.com>,
	<zhipingz@meta.com>, <wangzhou1@hisilicon.com>,
	<wangyushan12@huawei.com>, <liuyonglong@huawei.com>,
	<kvm@vger.kernel.org>, <linux-pci@vger.kernel.org>,
	alex@shazbot.org
Subject: Re: [PATCH v19 14/18] vfio/pci: Implement VFIO_DEVICE_FEATURE_TPH and valid TPH config write support
Date: Wed, 8 Jul 2026 17:30:36 -0600	[thread overview]
Message-ID: <20260708173036.6be23aae@shazbot.org> (raw)
In-Reply-To: <20260702124224.57168-15-fengchengwen@huawei.com>

On Thu, 2 Jul 2026 20:42:20 +0800
Chengwen Feng <fengchengwen@huawei.com> wrote:

> Implement vfio_pci_core_feature_tph() to handle VFIO_DEVICE_FEATURE_TPH:
> - SET permits TPH feature usage for the device;
> - GET reports available TPH capabilities according to tph_policy and
>   root-port DSM support.
> 
> Previously VFIO dropped all writes to the TPH capability register.
> Now implement vfio_tph_config_write() to support legitimate TPH_CTRL
> modification after TPH feature is permitted.
> 
> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> ---
>  drivers/vfio/pci/vfio_pci_config.c | 54 ++++++++++++++++++++++++++++++
>  drivers/vfio/pci/vfio_pci_core.c   | 36 ++++++++++++++++++++
>  include/linux/vfio_pci_core.h      |  1 +
>  3 files changed, 91 insertions(+)
> 
> diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
> index 06d7b2fbf866..388dd6fed16b 100644
> --- a/drivers/vfio/pci/vfio_pci_config.c
> +++ b/drivers/vfio/pci/vfio_pci_config.c
> @@ -1150,6 +1150,60 @@ 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 org_val = 0;
> +	bool extended;
> +	int ret;
> +
> +	if (!vdev->tph_permit)

We need to work on this variable name, tph_opt_in or something.

> +		return count;
> +
> +	down_write(&vdev->memory_lock);

Why is memory_lock involved in this sequence?  Taking the write lock
doesn't enable memory if it's already disabled, nor do we test the
memory state.

> +
> +	org_ctrl = le32_to_cpu(*(__le32 *)&vdev->vconfig[start + PCI_TPH_CTRL]);
> +	vfio_default_config_read(vdev, pos, count, perm, offset, &org_val);
> +
> +	ret = vfio_default_config_write(vdev, pos, count, perm, offset, val);
> +	if (ret != count)
> +		goto out;
> +
> +	new_ctrl = le32_to_cpu(*(__le32 *)&vdev->vconfig[start + PCI_TPH_CTRL]);
> +	if (new_ctrl == org_ctrl)
> +		goto out; /* Only care about changes in TPH_CTRL. */

We didn't need memory_lock for any of this.

> +
> +	cap = le32_to_cpu(*(__le32 *)&vdev->vconfig[start + PCI_TPH_CAP]);
> +	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_DS_MODE || !(cap & (1u << mode)) || req == 0x2 ||
> +		(req == PCI_TPH_REQ_EXT_TPH && !(cap & PCI_TPH_CAP_EXT_TPH)))
> +		goto restore; /* Drop invalid or unsupported write value */

Nor did we need memory_lock for this.

> +
> +	org_req = FIELD_GET(PCI_TPH_CTRL_REQ_EN_MASK, org_ctrl);
> +	if (req == org_req)
> +		goto out; /* Only care about requester enable */

What if mode is changed while enabled, don't we create an inconsistent
state?

> +
> +	ret = vfio_pci_set_power_state(vdev, PCI_D0);

We already did a runtime PM resume before calling vfio_pci_config_rw(),
why do we need this?

> +	if (ret)
> +		goto restore; /* Drop this write */
> +
> +	if (req == PCI_TPH_REQ_TPH_ONLY || req == PCI_TPH_REQ_EXT_TPH) {
> +		extended = !!(req == PCI_TPH_REQ_EXT_TPH);
> +		ret = pcie_enable_tph_explicit(pdev, mode, extended);
> +		if (ret)
> +			goto restore;
> +	} else if (req == PCI_TPH_REQ_DISABLE) {
> +		pcie_disable_tph(vdev->pdev);
> +	}

This sequence is really the only thing that appears to need memory_lock
and it should be using vfio_pci_memory_lock_and_enable() along with
vfio_pci_memory_unlock_and_restore().

> +
> +	goto out;
> +
> +restore:
> +	vfio_default_config_write(vdev, pos, count, perm, offset, org_val);
> +out:
> +	up_write(&vdev->memory_lock);
>  	return count;
>  }
>  
> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index 3f11a9624b9c..14944d3ea86e 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
> @@ -30,6 +30,7 @@
>  #include <linux/sched/mm.h>
>  #include <linux/iommufd.h>
>  #include <linux/pci-p2pdma.h>
> +#include <linux/pci-tph.h>
>  #include <linux/seq_file.h>
>  #if IS_ENABLED(CONFIG_EEH)
>  #include <asm/eeh.h>
> @@ -610,6 +611,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
>  		goto out_disable_device;
>  
>  	vdev->reset_works = !ret;
> +	vdev->tph_permit = false;
>  	pci_save_state(pdev);
>  	vdev->pci_saved_state = pci_store_saved_state(pdev);
>  	if (!vdev->pci_saved_state)
> @@ -1607,6 +1609,38 @@ static int vfio_pci_core_feature_token(struct vfio_pci_core_device *vdev,
>  	return 0;
>  }
>  
> +static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev,
> +				     u32 flags,
> +				     struct vfio_device_feature_tph __user *arg,
> +				     size_t argsz)
> +{
> +	struct vfio_device_feature_tph tph = {0};
> +	int ret;
> +
> +	if (!pcie_tph_supported(vdev->pdev, false))
> +		return -EOPNOTSUPP;
> +
> +	ret = vfio_check_feature(flags, argsz,
> +			VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET,
> +			sizeof(tph));
> +	if (ret <= 0)
> +		return ret;

That's not the convention for testing vfio_check_feature() return.

> +
> +	if (flags & VFIO_DEVICE_FEATURE_SET) {

		if (tph.flags)
			return -EINVAL;

> +		vdev->tph_permit = 1;
> +		return 0;
> +	}
> +

	if (!vdev->tph_permit)
		return -EINVAL;

> +	tph.flags = VFIO_DEVICE_TPH_CAP_DMABUF;
> +	if (vdev->tph_policy != VFIO_PCI_TPH_POLICY_NO_ST &&
> +		pcie_tph_dsm_supported(vdev->pdev))
> +		tph.flags |= VFIO_DEVICE_TPH_CAP_CPU;
> +	if (vdev->tph_policy == VFIO_PCI_TPH_POLICY_LITERAL)
> +		tph.flags |= VFIO_DEVICE_TPH_CAP_LITERAL;

These should also account for the device capabilities, CPU is not
supported unless the device supports somewhere to program CPU values,
ie. IV or DS mode.  Same for LITERAL, not exposed unless the device has
a way to use it, (IV or DS) and POLICY_LITERAL.  Thanks,

Alex

> +
> +	return copy_to_user(arg, &tph, sizeof(tph)) ? -EFAULT : 0;
> +}
> +
>  int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
>  				void __user *arg, size_t argsz)
>  {
> @@ -1625,6 +1659,8 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
>  		return vfio_pci_core_feature_token(vdev, flags, arg, argsz);
>  	case VFIO_DEVICE_FEATURE_DMA_BUF:
>  		return vfio_pci_core_feature_dma_buf(vdev, flags, arg, argsz);
> +	case VFIO_DEVICE_FEATURE_TPH:
> +		return vfio_pci_core_feature_tph(vdev, flags, arg, argsz);
>  	default:
>  		return -ENOTTY;
>  	}
> diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
> index a0641286dd90..9a6e82c5c00c 100644
> --- a/include/linux/vfio_pci_core.h
> +++ b/include/linux/vfio_pci_core.h
> @@ -150,6 +150,7 @@ struct vfio_pci_core_device {
>  	struct rw_semaphore	memory_lock;
>  	struct list_head	dmabufs;
>  	u8			tph_policy;
> +	bool			tph_permit;
>  };
>  
>  enum vfio_pci_io_width {


  parent reply	other threads:[~2026-07-08 23:34 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-02 12:42 [PATCH v19 00/18] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-07-02 12:42 ` [PATCH v19 01/18] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
2026-07-02 12:51   ` sashiko-bot
2026-07-02 12:42 ` [PATCH v19 02/18] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
2026-07-02 12:51   ` sashiko-bot
2026-07-02 12:42 ` [PATCH v19 03/18] PCI/TPH: Cache TPH requester capability at probe time Chengwen Feng
2026-07-02 12:55   ` sashiko-bot
2026-07-02 12:42 ` [PATCH v19 04/18] PCI/TPH: Refactor pcie_enable_tph & add explicit requester variant Chengwen Feng
2026-07-02 12:50   ` sashiko-bot
2026-07-08 23:33   ` Alex Williamson
2026-07-09  9:22     ` fengchengwen
2026-07-02 12:42 ` [PATCH v19 05/18] PCI/TPH: Refactor pcie_tph_get_cpu_st & add explicit variant Chengwen Feng
2026-07-02 12:56   ` sashiko-bot
2026-07-02 12:42 ` [PATCH v19 06/18] PCI/TPH: Expose the enabled TPH requester type Chengwen Feng
2026-07-02 12:49   ` sashiko-bot
2026-07-08 23:33   ` Alex Williamson
2026-07-09  9:30     ` fengchengwen
2026-07-02 12:42 ` [PATCH v19 07/18] PCI/TPH: Add pcie_tph_supported() helper to check TPH capability attributes Chengwen Feng
2026-07-02 12:53   ` sashiko-bot
2026-07-03  0:39     ` fengchengwen
2026-07-08 23:32   ` Alex Williamson
2026-07-09  9:36     ` fengchengwen
2026-07-02 12:42 ` [PATCH v19 08/18] PCI/TPH: Add pci_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
2026-07-02 12:55   ` sashiko-bot
2026-07-02 12:42 ` [PATCH v19 09/18] vfio/pci: Hide TPH capability when TPH is unsupported Chengwen Feng
2026-07-02 13:00   ` sashiko-bot
2026-07-03  0:36     ` fengchengwen
2026-07-08 23:32   ` Alex Williamson
2026-07-09 11:00     ` fengchengwen
2026-07-02 12:42 ` [PATCH v19 10/18] vfio/pci: Introduce tph policy parameter for staged TPH feature enablement Chengwen Feng
2026-07-02 12:50   ` sashiko-bot
2026-07-08 23:31   ` Alex Williamson
2026-07-09 11:29     ` fengchengwen
2026-07-02 12:42 ` [PATCH v19 11/18] vfio/pci: Virtualize PCIe TPH capability registers Chengwen Feng
2026-07-02 13:04   ` sashiko-bot
2026-07-03  0:51     ` fengchengwen
2026-07-08 23:31   ` Alex Williamson
2026-07-09 11:40     ` fengchengwen
2026-07-02 12:42 ` [PATCH v19 12/18] vfio/pci: Add dmabuf TPH metadata storage and fd query helper Chengwen Feng
2026-07-02 12:56   ` sashiko-bot
2026-07-03  0:53     ` fengchengwen
2026-07-08 23:31   ` Alex Williamson
2026-07-09 11:49     ` fengchengwen
2026-07-09 18:50       ` Alex Williamson
2026-07-02 12:42 ` [PATCH v19 13/18] vfio/pci: Introduce VFIO_DEVICE_FEATURE_TPH family uapi for PCI TPH control Chengwen Feng
2026-07-02 13:01   ` sashiko-bot
2026-07-03  0:57     ` fengchengwen
2026-07-08 23:31   ` Alex Williamson
2026-07-09 11:56     ` fengchengwen
2026-07-02 12:42 ` [PATCH v19 14/18] vfio/pci: Implement VFIO_DEVICE_FEATURE_TPH and valid TPH config write support Chengwen Feng
2026-07-02 13:04   ` sashiko-bot
2026-07-03  1:16     ` fengchengwen
2026-07-08 23:30   ` Alex Williamson [this message]
2026-07-09 12:16     ` fengchengwen
2026-07-02 12:42 ` [PATCH v19 15/18] vfio/pci: Implement TPH_RESOLVE feature for DMABUF and CPU source resolving Chengwen Feng
2026-07-02 13:00   ` sashiko-bot
2026-07-03  1:26     ` fengchengwen
2026-07-08 23:31   ` Alex Williamson
2026-07-09 12:20     ` fengchengwen
2026-07-02 12:42 ` [PATCH v19 16/18] vfio/pci: Implement TPH_ST feature for batch ST table programming Chengwen Feng
2026-07-02 13:04   ` sashiko-bot
2026-07-03  1:42     ` fengchengwen
2026-07-08 23:29   ` Alex Williamson
2026-07-09 12:36     ` fengchengwen
2026-07-09 13:49       ` Alex Williamson
2026-07-02 12:42 ` [PATCH v19 17/18] vfio/pci: Reset hardware TPH state on device enable/disable Chengwen Feng
2026-07-02 13:00   ` sashiko-bot
2026-07-08 23:29   ` Alex Williamson
2026-07-09 12:39     ` fengchengwen
2026-07-02 12:42 ` [PATCH v19 18/18] vfio/pci: Expose tph_policy via debugfs Chengwen Feng
2026-07-02 12:59   ` 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=20260708173036.6be23aae@shazbot.org \
    --to=alex@shazbot.org \
    --cc=fengchengwen@huawei.com \
    --cc=helgaas@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=kvm@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=liuyonglong@huawei.com \
    --cc=wangyushan12@huawei.com \
    --cc=wangzhou1@hisilicon.com \
    --cc=wathsala.vithanage@arm.com \
    --cc=wei.huang2@amd.com \
    --cc=zhipingz@meta.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