Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: fengchengwen <fengchengwen@huawei.com>
To: Alex Williamson <alex@shazbot.org>
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>
Subject: Re: [PATCH v19 11/18] vfio/pci: Virtualize PCIe TPH capability registers
Date: Thu, 9 Jul 2026 19:40:52 +0800	[thread overview]
Message-ID: <9092fc02-1ecb-46b7-870f-3560bdb426ca@huawei.com> (raw)
In-Reply-To: <20260708173128.687525c6@shazbot.org>

Hi Alex,

On 7/9/2026 7:31 AM, Alex Williamson wrote:
> On Thu, 2 Jul 2026 20:42:17 +0800
> Chengwen Feng <fengchengwen@huawei.com> wrote:
> 
>> Implement virtualization and policy masking for PCIe TPH extended
>> capability:
>> - Split TPH config space permissions: keep header read-only, mark
>>   TPH_CTRL and ST-table entries virtually writable.
>> - Adjust virtual TPH capability bits according to hardware capability
>>   and tph_policy, hiding unsupported modes.
>> - Silently discard all write operations.
>>
>> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
>> ---
>>  drivers/vfio/pci/vfio_pci_config.c | 72 ++++++++++++++++++++++++++++++
>>  1 file changed, 72 insertions(+)
>>
>> diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
>> index 5c6ab172df6c..06d7b2fbf866 100644
>> --- a/drivers/vfio/pci/vfio_pci_config.c
>> +++ b/drivers/vfio/pci/vfio_pci_config.c
>> @@ -1086,6 +1086,73 @@ static int __init init_pci_ext_cap_pwr_perm(struct perm_bits *perm)
>>  	return 0;
>>  }
>>  
>> +/* Permissions for TPH extended capability */
>> +static int __init init_pci_ext_cap_tph_perm(struct perm_bits *perm)
>> +{
>> +	int i;
>> +
>> +	if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_TPH]))
>> +		return -ENOMEM;
>> +
>> +	p_setd(perm, 0, ALL_VIRT, NO_WRITE);
>> +	p_setd(perm, PCI_TPH_CAP, ALL_VIRT, NO_WRITE);
>> +
>> +	p_setd(perm, PCI_TPH_CTRL, ALL_VIRT,
>> +	       PCI_TPH_CTRL_MODE_SEL_MASK | PCI_TPH_CTRL_REQ_EN_MASK);
> 
> This makes the control bits virtualized, writable with no backing
> support yet.

I will gate all TPH config space virtualization and masking logic behind
the VFIO_DEVICE_FEATURE_TPH SET opt-in check. Without explicit opt-in,
TPH_CTRL and ST table entries will remain read-only to avoid exposing
writable virtual bits with no functional backing.

> 
>> +
>> +	/* Per PCI specification: There is an upper limit of 64 entries
>> +	 * when the ST table is located in the TPH Requester Extended
>> +	 * Capability structure.
>> +	 * And the pci_ext_cap_length[PCI_EXT_CAP_ID_TPH] is 0xFF, so the
>> +	 * following operation is fine.
>> +	 */
> 
> Wrong multi-line comment style.  Also 0xFF for cap length doesn't
> really explain why this is fine.  You are however highlighting that the
> above perm bits alloc of size 0xFF is not fine.

I will try to fix the length

> 
>> +	for (i = 0; i < 64; i++)
>> +		p_setw(perm, PCI_TPH_BASE_SIZEOF + i * sizeof(u16),
>> +		       (u16)ALL_VIRT, (u16)ALL_WRITE);
>> +
>> +	return 0;
>> +}
>> +
>> +static void vfio_tph_capability_adjust(struct vfio_pci_core_device *vdev,
>> +				       int pos)
> 
> Precedent is vfio_update_tph_vconfig_bytes(...)

OK

> 
>> +{
>> +	__le32 *vptr = (__le32 *)&vdev->vconfig[pos + PCI_TPH_CAP];
>> +	struct pci_dev *pdev = vdev->pdev;
>> +	u32 val = le32_to_cpu(*vptr);
>> +	bool need_adjust = false;
> 
> Just write it back unconditionally, not worth tracking.

OK

> 
> However, any modification to the capability needs to be gated by the
> device feature opt-in, so this might be better handled in a readfn
> rather than managed through permission bits.

OK, I will try that in v20

> 
>> +
>> +	if (!pcie_tph_supported(pdev, true)) {
>> +		/* Remove extend TPH if root-port don't support */
>> +		val &= ~PCI_TPH_CAP_EXT_TPH;
>> +		need_adjust = true;
>> +	}
>> +
>> +	if (vdev->tph_policy == VFIO_PCI_TPH_POLICY_NO_ST) {
>> +		/* Report only No-ST mode supported */
>> +		val &= ~(PCI_TPH_CAP_ST_IV | PCI_TPH_CAP_ST_DS |
>> +			 PCI_TPH_CAP_LOC_MASK | PCI_TPH_CAP_ST_MASK);
>> +		need_adjust = true;
>> +	} else if (vdev->tph_policy == VFIO_PCI_TPH_POLICY_IV_ST) {
>> +		/* Report only No-ST and IV modes supported */
>> +		val &= ~PCI_TPH_CAP_ST_DS;
>> +		/* Remove ST location and size if dev don't support IV mode */
> 
> s/don't/doesn't/

ok

> 
>> +		if (!(val & PCI_TPH_CAP_ST_IV))
>> +			val &= ~(PCI_TPH_CAP_LOC_MASK | PCI_TPH_CAP_ST_MASK);
>> +		need_adjust = true;
>> +	}
>> +
>> +	if (need_adjust)
>> +		*vptr = cpu_to_le32(val);
>> +}
>> +
>> +static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos);
> 
> ??

I will move vfio_find_cap_start in front of this new function.

> 
>> +static int vfio_tph_config_write(struct vfio_pci_core_device *vdev, int pos,
>> +				 int count, struct perm_bits *perm,
>> +				 int offset, __le32 val)
>> +{
>> +	return count;
>> +}
>> +
>>  /*
>>   * Initialize the shared permission tables
>>   */
>> @@ -1101,6 +1168,7 @@ void vfio_pci_uninit_perm_bits(void)
>>  
>>  	free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
>>  	free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
>> +	free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_TPH]);
>>  }
>>  
>>  int __init vfio_pci_init_perm_bits(void)
>> @@ -1121,6 +1189,8 @@ int __init vfio_pci_init_perm_bits(void)
>>  	/* Extended capabilities */
>>  	ret |= init_pci_ext_cap_err_perm(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
>>  	ret |= init_pci_ext_cap_pwr_perm(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
>> +	ret |= init_pci_ext_cap_tph_perm(&ecap_perms[PCI_EXT_CAP_ID_TPH]);
>> +	ecap_perms[PCI_EXT_CAP_ID_TPH].writefn = vfio_tph_config_write;
>>  	ecap_perms[PCI_EXT_CAP_ID_VNDR].writefn = vfio_raw_config_write;
>>  	ecap_perms[PCI_EXT_CAP_ID_DVSEC].writefn = vfio_raw_config_write;
>>  
>> @@ -1704,6 +1774,8 @@ static int vfio_ecap_init(struct vfio_pci_core_device *vdev)
>>  		ret = vfio_fill_vconfig_bytes(vdev, epos, len);
>>  		if (ret)
>>  			return ret;
>> +		if (ecap == PCI_EXT_CAP_ID_TPH && !hidden)
> 
> Never hidden.  Thanks,

OK

Thanks

> 
> Alex
> 
>> +			vfio_tph_capability_adjust(vdev, epos);
>>  
>>  		/*
>>  		 * If we're just using this capability to anchor the list,
> 


  reply	other threads:[~2026-07-09 11:40 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 [this message]
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
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=9092fc02-1ecb-46b7-870f-3560bdb426ca@huawei.com \
    --to=fengchengwen@huawei.com \
    --cc=alex@shazbot.org \
    --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