* [PATCH v22 00/13] vfio/pci: Add PCIe TPH support
@ 2026-11-23 2:36 Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
` (14 more replies)
0 siblings, 15 replies; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
This patchset enables userspace-configurable PCIe TPH support for VFIO in
the **device-to-host (D2H)** scenario, bringing performance benefits for
userspace workloads such as DPDK and SPDK.
The patchset is split into two logical parts:
1. The first eight patches fix and refactor core PCI/TPH kernel code to
export the required helper interfaces.
2. The second five patches implement the corresponding VFIO TPH
virtualization and uAPI step by step.
Since TLP TPH ST values are not validated by the IOMMU like TLP address
fields, there exists risk of cross-tenant cache interference if untrusted
VM can program arbitrary ST tags. We introduce two-level security
gating:
The first is the VFIO-PCI module parameter tph_policy, defined as:
Global TPH policy level (0 = default No-ST):
0 = No-ST mode: ST programming unavailable
1 = Add IV-ST mode: ST programming supports NONE / CPU sources
2 = Add DS-ST mode: RESOLVE returns ST for CPU sources;
ST programming supports NONE / CPU sources
Note: In this 'Add DS-ST mode', device-specific ST tables are not
preserved across PCI resets, userspace must re-establish ST
state after reset. The kernel cannot validate these ST
entries, so this mode is not recommended for general
virtualization usecases.
Note: IV-ST mode is considered safe for VM use because the ST table is
programmed exclusively via the kernel VFIO uAPI. VM cannot directly
write the architected ST table in MSI-X or config space. The hypervisor
(e.g. QEMU) will verify ST, and also when a target CPU transitions from
online to offline, the kernel automatically (add in this patchset) clears
all ST table entries mapped to that CPU, setting those ST values to zero.
The second gate is per-device opt-in. TPH programming is only permitted
after userspace successfully opt-in via the VFIO TPH feature.
Three VFIO device features are introduced in this series:
1. VFIO_DEVICE_FEATURE_TPH: control and query VFIO TPH feature
capabilities.
2. VFIO_DEVICE_FEATURE_TPH_PROGRAM: batch program architected steering tag
table entries.
3. VFIO_DEVICE_FEATURE_TPH_RESOLVE: resolve TPH attributes for a given CPU
source.
The intended userspace programming model is:
1. Read the virtualized PCI TPH capability via VFIO config space access to
discover underlying hardware TPH support.
2. Invoke VFIO_DEVICE_FEATURE_TPH SET to opt-in and obtain TPH permission.
3. Invoke VFIO_DEVICE_FEATURE_TPH GET to retrieve available TPH
capabilities; GET returns an error if opt-in has not been performed.
4. Write the TPH control register through VFIO config space to activate
hardware TPH.
5. Program architected ST table entries via
VFIO_DEVICE_FEATURE_TPH_PROGRAM if the device exposes architected ST
table.
6. Query the ST value for a given CPU ID via
VFIO_DEVICE_FEATURE_TPH_RESOLVE for DS-ST capable devices, userspace
may then configure the ST through device MMIO or firmware interfaces.
Key implementation details for capability virtualization:
1. The TPH capability register is virtualized according to the active
tph_policy:
a. No-ST policy: IV/DS mode bits, ST location and ST size fields are
masked. Only NO-ST capability is visible to userspace.
b. IV-ST policy: DS mode bits are masked. Userspace sees NO-ST and
IV-ST capabilities (if hardware supports IV-ST).
c. DS-ST policy: capability register is passed through unmodified.
2. Capability bits returned by VFIO_DEVICE_FEATURE_TPH GET follow these
rules:
a. If virtualized TPH exposes architected ST table:
VFIO_DEVICE_TPH_CAP_PROG_ST_NONE and VFIO_DEVICE_TPH_CAP_PROG_ST_CPU
are reported. These indicate userspace may use TPH_PROGRAM to update
ST.
b. If virtualized TPH supports DS mode:
VFIO_DEVICE_TPH_CAP_RESOLVE_CPU_ST is reported. This indicates
userspace can resolve CPU-ID associated ST values via TPH_RESOLVE.
Future P2P / DMABUF extension:
The uAPI is designed to easy extend to support device-to-device (P2P) and
DMABUF-backed scenarios on top of Zhiping's P2P TPH patchset:
1. Return VFIO_DEVICE_TPH_CAP_RESOLVE_DMABUF_PH in all tph_policy in
VFIO_DEVICE_FEATURE_TPH GET, so that userspace could resolve DMABUF's
TPH PH info
2. Return VFIO_DEVICE_TPH_CAP_PROG_ST_DMABUF if virtualized TPH has the
architect ST, so that userspace could program ST entries which
associated to DMABUFs' fd
3. Return VFIO_DEVICE_TPH_CAP_RESOLVE_DMABUF_ST if virtualized TPH
supports DS mode, so that userspace could retrieve ST value which
associated to DMABUF's fd
---
v22:
- Rebase latest linux-next to resolve uAPI conflict
- Reorder patch sequence: move core tph disable fixes ahead of vfio patches
- Fix Sashiko comments:
- Fix TPH config write path: disable existing TPH before re-enable to
keep state coherent
- Fix cpuhp race/UAF: reorder TPH teardown before cpuhp removal
- Add cpu_online() check in ST resolve routine
- Gate IV/DS capability exposure on TPH _DSM presence; hide IV/DS when
DSM unavailable
- Fix kernel-doc and redundant check in pcie_tph_dsm_supported() helper
v21:
- Focus only device-to-host (D2H) scenario
- Implement steering-tag to cpuid map to tracking cpu offline
- Refactor the implementation
v20:
- Address almost all comments from Alex about v19, not within Alex's
scope:
- Drop LITERAL policy support
- uAPI changes: cap have been split into fine-grained
- TPH config access: still based on perm mechanism, but restrict the
readable and writable length
- Debugfs add more fields
v19:
- Address all comments from Alex
- Drop CPU-to-ST translation of PCI/TPH
- Re-implement vfio part, please refer:
https://lore.kernel.org/linux-pci/20260626092232.53ed3a7c@shazbot.org/
- Add expose tph_policy via debugfs
Chengwen Feng (13):
PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction
PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing
PCI/TPH: Cache negotiated max requester type at probe time
PCI/TPH: Refactor pcie_enable_tph and add requester variant
PCI/TPH: Refactor pcie_tph_get_cpu_st & add requester variant
PCI/TPH: Add helpers to check standard and extended TPH support
PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming
helper
PCI/TPH: Add pcie_tph_dsm_supported() helper to detect device TPH ST
_DSM
vfio/pci: Reset PCIe TPH state on device open and close
vfio/pci: Add basic NO-ST TPH VFIO device feature support
vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming
support
vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability
vfio/pci: Expose per-device TPH state via debugfs
drivers/pci/tph.c | 240 ++++++++++++-----
drivers/vfio/pci/vfio_pci.c | 24 ++
drivers/vfio/pci/vfio_pci_config.c | 188 +++++++++++--
drivers/vfio/pci/vfio_pci_core.c | 412 ++++++++++++++++++++++++++++-
drivers/vfio/pci/vfio_pci_priv.h | 6 +
include/linux/pci-tph.h | 55 +++-
include/linux/pci.h | 6 +-
include/linux/vfio_pci_core.h | 7 +
include/uapi/linux/vfio.h | 129 +++++++++
9 files changed, 967 insertions(+), 100 deletions(-)
--
2.17.1
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v22 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
` (13 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
pcie_tph_get_st_table_loc() incorrectly uses FIELD_GET(), which shifts the
field value to bit 0. But the function is designed to return raw
PCI_TPH_LOC_* values as defined in the function comment.
This causes incorrect ST table location detection. Fix it by using bitwise
AND with PCI_TPH_CAP_LOC_MASK to return the unshifted field value matching
the function specification.
This doesn't make a difference to mlx5_st_create(), the lone external
caller, because it only checks for PCI_TPH_LOC_NONE (0), but will be needed
for callers that check for PCI_TPH_LOC_CAP or PCI_TPH_LOC_MSIX.
Also add tph_cap validation for pcie_tph_get_st_table_loc() to prevent
invalid PCI configuration space access when TPH is not supported. Add stub
functions for pcie_tph_get_st_table_size() and pcie_tph_get_st_table_loc()
when !CONFIG_PCIE_TPH.
Fixes: 0a61ec9cc51b ("PCI/TPH: Expose pcie_tph_get_st_table_size()")
Fixes: 7b8a8ec20cfc ("PCI/TPH: Expose pcie_tph_get_st_table_loc()")
Cc: stable@vger.kernel.org
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Reviewed-by: Alex Williamson <alex.williamson@nvidia.com>
Reviewed-by: Bjorn Helgaas <bhelgaas@google.com>
---
drivers/pci/tph.c | 12 +++++-------
include/linux/pci-tph.h | 6 ++++++
2 files changed, 11 insertions(+), 7 deletions(-)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index 4d064b86befc..c4ceb08204d6 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -167,11 +167,14 @@ static u8 get_st_modes(struct pci_dev *pdev)
*/
u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)
{
- u32 reg;
+ u32 reg = 0;
+
+ if (!pdev->tph_cap)
+ return PCI_TPH_LOC_NONE;
pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, ®);
- return FIELD_GET(PCI_TPH_CAP_LOC_MASK, reg);
+ return reg & PCI_TPH_CAP_LOC_MASK;
}
EXPORT_SYMBOL(pcie_tph_get_st_table_loc);
@@ -186,9 +189,6 @@ u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)
/* Check ST table location first */
loc = pcie_tph_get_st_table_loc(pdev);
-
- /* Convert loc to match with PCI_TPH_LOC_* defined in pci_regs.h */
- loc = FIELD_PREP(PCI_TPH_CAP_LOC_MASK, loc);
if (loc != PCI_TPH_LOC_CAP)
return 0;
@@ -317,8 +317,6 @@ int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
set_ctrl_reg_req_en(pdev, PCI_TPH_REQ_DISABLE);
loc = pcie_tph_get_st_table_loc(pdev);
- /* Convert loc to match with PCI_TPH_LOC_* */
- loc = FIELD_PREP(PCI_TPH_CAP_LOC_MASK, loc);
switch (loc) {
case PCI_TPH_LOC_MSIX:
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index be68cd17f2f8..980124dcb075 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -9,6 +9,8 @@
#ifndef LINUX_PCI_TPH_H
#define LINUX_PCI_TPH_H
+#include <linux/pci.h>
+
/*
* According to the ECN for PCI Firmware Spec, Steering Tag can be different
* depending on the memory type: Volatile Memory or Persistent Memory. When a
@@ -41,6 +43,10 @@ static inline int pcie_tph_get_cpu_st(struct pci_dev *dev,
static inline void pcie_disable_tph(struct pci_dev *pdev) { }
static inline int pcie_enable_tph(struct pci_dev *pdev, int mode)
{ return -EINVAL; }
+static inline u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)
+{ return 0; }
+static inline u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)
+{ return PCI_TPH_LOC_NONE; }
#endif
#endif /* LINUX_PCI_TPH_H */
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 03/13] PCI/TPH: Cache negotiated max requester type at probe time Chengwen Feng
` (12 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Split tph_enabled from shared pci_dev bitfield into spare bit of tph_cap's
u16: tph_cap is immutable post-enumeration (15 bits for offset), remaining
1 bit stores tph_enabled. Removes cross-bitfield concurrent write hazards
highlighted by Sashiko after VFIO TPH exposure. No functional changes.
Fixes: f69767a1ada3 ("PCI: Add TLP Processing Hints (TPH) support")
Cc: stable@vger.kernel.org
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
include/linux/pci.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 6482549a8417..6b577890fbb2 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -478,7 +478,6 @@ struct pci_dev {
unsigned int ats_enabled:1; /* Address Translation Svc */
unsigned int pasid_enabled:1; /* Process Address Space ID */
unsigned int pri_enabled:1; /* Page Request Interface */
- unsigned int tph_enabled:1; /* TLP Processing Hints */
unsigned int fm_enabled:1; /* Flit Mode (segment captured) */
unsigned int is_managed:1; /* Managed via devres */
unsigned int is_msi_managed:1; /* MSI release via devres installed */
@@ -596,7 +595,8 @@ struct pci_dev {
struct gpio_desc *wake; /* WAKE# GPIO */
#ifdef CONFIG_PCIE_TPH
- u16 tph_cap; /* TPH capability offset */
+ u16 tph_cap:15; /* TPH capability offset */
+ u16 tph_enabled:1; /* Whether TPH is enabled */
u8 tph_mode; /* TPH mode */
u8 tph_req_type; /* TPH requester type */
#endif
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 03/13] PCI/TPH: Cache negotiated max requester type at probe time
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant Chengwen Feng
` (11 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
pcie_enable_tph() recalculates the TPH requester capability and queries
root port completer type on every invocation, causing redundant config
space reads.
Calculate the negotiated maximum requester type once during pci_tph_init()
and cache it in a new pdev->tph_max_type bitfield. Simplify the enable path
by reusing the cached value to eliminate repeated hardware access.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/pci/tph.c | 45 ++++++++++++++++++++++++---------------------
include/linux/pci.h | 4 +++-
2 files changed, 27 insertions(+), 22 deletions(-)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index c4ceb08204d6..58f7ba41e1f7 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -375,9 +375,8 @@ EXPORT_SYMBOL(pcie_disable_tph);
* - PCI_TPH_ST_DS_MODE: Device Specific Mode
*
* Check whether the mode is actually supported by the device before enabling
- * and return an error if not. Additionally determine what types of requests,
- * TPH or extended TPH, can be issued by the device based on its TPH requester
- * capability and the Root Port's completer capability.
+ * and return an error if not. Automatically uses the pre-cached tph_max_type
+ * value, the negotiated maximum requester type between device and root port.
*
* Return: 0 on success, otherwise negative value (-errno)
*/
@@ -385,7 +384,6 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
{
u32 reg;
u8 dev_modes;
- u8 rp_req_type;
/* Honor "notph" kernel parameter */
if (pci_tph_disabled)
@@ -397,6 +395,9 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
if (pdev->tph_enabled)
return -EBUSY;
+ if (pdev->tph_max_type == PCI_TPH_REQ_DISABLE)
+ return -EINVAL;
+
/* Sanitize and check ST mode compatibility */
mode &= PCI_TPH_CTRL_MODE_SEL_MASK;
dev_modes = get_st_modes(pdev);
@@ -405,23 +406,7 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
pdev->tph_mode = mode;
- /* Get req_type supported by device and its Root Port */
- pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, ®);
- if (FIELD_GET(PCI_TPH_CAP_EXT_TPH, reg))
- pdev->tph_req_type = PCI_TPH_REQ_EXT_TPH;
- else
- pdev->tph_req_type = PCI_TPH_REQ_TPH_ONLY;
-
- /* Check if the device is behind a Root Port */
- if (pci_pcie_type(pdev) != PCI_EXP_TYPE_RC_END) {
- rp_req_type = get_rp_completer_type(pdev);
-
- /* Final req_type is the smallest value of two */
- pdev->tph_req_type = min(pdev->tph_req_type, rp_req_type);
- }
-
- if (pdev->tph_req_type == PCI_TPH_REQ_DISABLE)
- return -EINVAL;
+ pdev->tph_req_type = pdev->tph_max_type;
/* Write them into TPH control register */
pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, ®);
@@ -508,13 +493,31 @@ void pci_no_tph(void)
void pci_tph_init(struct pci_dev *pdev)
{
+ u8 tph_req_type, rp_req_type;
int num_entries;
u32 save_size;
+ u32 reg = 0;
pdev->tph_cap = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_TPH);
if (!pdev->tph_cap)
return;
+ /* Get req_type supported by device and its Root Port */
+ pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CAP, ®);
+ if (FIELD_GET(PCI_TPH_CAP_EXT_TPH, reg))
+ tph_req_type = PCI_TPH_REQ_EXT_TPH;
+ else
+ tph_req_type = PCI_TPH_REQ_TPH_ONLY;
+
+ /* Check if the device is behind a Root Port */
+ if (pci_pcie_type(pdev) != PCI_EXP_TYPE_RC_END) {
+ rp_req_type = get_rp_completer_type(pdev);
+ /* Final req_type is the smallest value of two */
+ tph_req_type = min(tph_req_type, rp_req_type);
+ }
+
+ pdev->tph_max_type = tph_req_type;
+
num_entries = pcie_tph_get_st_table_size(pdev);
save_size = sizeof(u32) + num_entries * sizeof(u16);
pci_add_ext_cap_save_buffer(pdev, PCI_EXT_CAP_ID_TPH, save_size);
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 6b577890fbb2..1dc3cb011dff 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -595,7 +595,9 @@ struct pci_dev {
struct gpio_desc *wake; /* WAKE# GPIO */
#ifdef CONFIG_PCIE_TPH
- u16 tph_cap:15; /* TPH capability offset */
+ u16 tph_cap:13; /* TPH capability offset */
+ u16 tph_max_type:2; /* Negotiated max TPH requester type
+ between device and root port */
u16 tph_enabled:1; /* Whether TPH is enabled */
u8 tph_mode; /* TPH mode */
u8 tph_req_type; /* TPH requester type */
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (2 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 03/13] PCI/TPH: Cache negotiated max requester type at probe time Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
` (10 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Refactor the core TPH enable logic by renaming the original function to
pcie_enable_tph_ext(), which accepts an explicit requester type argument.
- Keep the original pcie_enable_tph() as a compatibility wrapper; it uses
the pre-cached tph_max_type negotiated capability automatically, so
existing drivers calling pcie_enable_tph() require no modification.
- Export new pcie_enable_tph_ext() to support explicit requester type
selection, intended for use by the upcoming VFIO TPH series.
- Add requester type validation to reject values out of hardware negotiated
range or invalid reserved types.
Update pci-tph.h header and CONFIG_PCIE_TPH stub inline definitions
to match the new function signatures.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/pci/tph.c | 41 ++++++++++++++++++++++++++++++-----------
include/linux/pci-tph.h | 8 ++++++--
2 files changed, 36 insertions(+), 13 deletions(-)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index 58f7ba41e1f7..875c381e094f 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -366,21 +366,18 @@ void pcie_disable_tph(struct pci_dev *pdev)
EXPORT_SYMBOL(pcie_disable_tph);
/**
- * pcie_enable_tph - Enable TPH support for device using a specific ST mode
+ * pcie_enable_tph_ext - Enable TPH support with specific ST mode and
+ * requester type
* @pdev: PCI device
- * @mode: ST mode to enable. Current supported modes include:
- *
- * - PCI_TPH_ST_NS_MODE: NO ST Mode
- * - PCI_TPH_ST_IV_MODE: Interrupt Vector Mode
- * - PCI_TPH_ST_DS_MODE: Device Specific Mode
+ * @mode: ST mode to enable: PCI_TPH_ST_(NS_MODE / IV_MODE / DS_MODE)
+ * @req_type: Requester type to enable: PCI_TPH_REQ_(TPH_ONLY / EXT_TPH)
*
- * Check whether the mode is actually supported by the device before enabling
- * and return an error if not. Automatically uses the pre-cached tph_max_type
- * value, the negotiated maximum requester type between device and root port.
+ * Check whether the mode and requester type is actually supported by the
+ * device before enabling and return an error if not.
*
* Return: 0 on success, otherwise negative value (-errno)
*/
-int pcie_enable_tph(struct pci_dev *pdev, int mode)
+int pcie_enable_tph_ext(struct pci_dev *pdev, u8 mode, u8 req_type)
{
u32 reg;
u8 dev_modes;
@@ -398,6 +395,11 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
if (pdev->tph_max_type == PCI_TPH_REQ_DISABLE)
return -EINVAL;
+ /* Sanitize and check TPH requester type compatibility */
+ if (req_type == PCI_TPH_REQ_DISABLE || req_type == 2 ||
+ req_type > pdev->tph_max_type)
+ return -EINVAL;
+
/* Sanitize and check ST mode compatibility */
mode &= PCI_TPH_CTRL_MODE_SEL_MASK;
dev_modes = get_st_modes(pdev);
@@ -406,7 +408,7 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
pdev->tph_mode = mode;
- pdev->tph_req_type = pdev->tph_max_type;
+ pdev->tph_req_type = req_type;
/* Write them into TPH control register */
pci_read_config_dword(pdev, pdev->tph_cap + PCI_TPH_CTRL, ®);
@@ -420,6 +422,23 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)
return 0;
}
+EXPORT_SYMBOL(pcie_enable_tph_ext);
+
+/**
+ * pcie_enable_tph - Enable TPH support for device using a specific ST mode
+ * @pdev: PCI device
+ * @mode: ST mode to enable: PCI_TPH_ST_(NS_MODE / IV_MODE / DS_MODE)
+ *
+ * Wrapper for pcie_enable_tph_ext(), automatically uses the pre-cached
+ * tph_max_type value (negotiated maximum requester type between device and
+ * root port).
+ *
+ * Return: 0 on success, otherwise negative value (-errno)
+ */
+int pcie_enable_tph(struct pci_dev *pdev, u8 mode)
+{
+ return pcie_enable_tph_ext(pdev, mode, pdev->tph_max_type);
+}
EXPORT_SYMBOL(pcie_enable_tph);
void pci_restore_tph_state(struct pci_dev *pdev)
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index 980124dcb075..f021419251cc 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -29,7 +29,8 @@ int pcie_tph_get_cpu_st(struct pci_dev *dev,
enum tph_mem_type mem_type,
unsigned int cpu, u16 *tag);
void pcie_disable_tph(struct pci_dev *pdev);
-int pcie_enable_tph(struct pci_dev *pdev, int mode);
+int pcie_enable_tph_ext(struct pci_dev *pdev, u8 mode, u8 req_type);
+int pcie_enable_tph(struct pci_dev *pdev, u8 mode);
u16 pcie_tph_get_st_table_size(struct pci_dev *pdev);
u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev);
#else
@@ -41,7 +42,10 @@ static inline int pcie_tph_get_cpu_st(struct pci_dev *dev,
unsigned int cpu, u16 *tag)
{ return -EINVAL; }
static inline void pcie_disable_tph(struct pci_dev *pdev) { }
-static inline int pcie_enable_tph(struct pci_dev *pdev, int mode)
+static inline int pcie_enable_tph_ext(struct pci_dev *pdev, u8 mode,
+ u8 req_type)
+{ return -EINVAL; }
+static inline int pcie_enable_tph(struct pci_dev *pdev, u8 mode)
{ return -EINVAL; }
static inline u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)
{ return 0; }
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & add requester variant
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (3 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 06/13] PCI/TPH: Add helpers to check standard and extended TPH support Chengwen Feng
` (9 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Refactor pcie_tph_get_cpu_st() and add pcie_tph_get_cpu_st_ext() extended
variant with explicit requester type parameter.
- Preserve original pcie_tph_get_cpu_st() as a compatibility wrapper,
using the default negotiated tph_max_type automatically, so existing
callers require no modification.
- Add exported pcie_tph_get_cpu_st_ext() to accept an explicit requester
type argument for ST namespace selection, used by upcoming VFIO TPH
support.
- Add requester type validation to reject invalid values and enforce the
hardware negotiated tph_max_type capability limit.
Update pci-tph.h header and CONFIG_PCIE_TPH stub inline definitions
to match the new function signatures.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/pci/tph.c | 45 ++++++++++++++++++++++++++++++++---------
include/linux/pci-tph.h | 10 +++++++--
2 files changed, 44 insertions(+), 11 deletions(-)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index 875c381e094f..51a065d83104 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -233,20 +233,22 @@ static int write_tag_to_st_table(struct pci_dev *pdev, int index, u16 tag)
}
/**
- * pcie_tph_get_cpu_st() - Retrieve Steering Tag for a target memory associated
- * with a specific CPU
+ * pcie_tph_get_cpu_st_ext() - Retrieve Steering Tag for a target memory
+ * associated with a specific CPU, with explicit requester type selection
* @pdev: PCI device
* @mem_type: target memory type (volatile or persistent RAM)
+ * @req_type: TPH requester type to select ST namespace
* @cpu: associated CPU id
* @tag: Steering Tag to be returned
*
- * Return the Steering Tag for a target memory that is associated with a
- * specific CPU as indicated by cpu.
+ * Return the Steering Tag for a target memory associated with a specific CPU,
+ * using the specified TPH requester type to select the correct ST namespace.
+ * Validates requester type against hardware negotiated capability limits.
*
* Return: 0 if success, otherwise negative value (-errno)
*/
-int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
- unsigned int cpu, u16 *tag)
+int pcie_tph_get_cpu_st_ext(struct pci_dev *pdev, enum tph_mem_type mem_type,
+ u8 req_type, unsigned int cpu, u16 *tag)
{
#ifdef CONFIG_ACPI
struct pci_dev *rp;
@@ -255,6 +257,10 @@ int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
u32 cpu_uid;
int ret;
+ if (req_type == PCI_TPH_REQ_DISABLE || req_type == 2 ||
+ req_type > pdev->tph_max_type)
+ return -EINVAL;
+
ret = acpi_get_cpu_uid(cpu, &cpu_uid);
if (ret != 0)
return ret;
@@ -270,17 +276,38 @@ int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
return -EINVAL;
}
- *tag = tph_extract_tag(mem_type, pdev->tph_req_type, &info);
+ *tag = tph_extract_tag(mem_type, req_type, &info);
- pci_dbg(pdev, "get steering tag: mem_type=%s, cpu=%d, tag=%#04x\n",
+ pci_dbg(pdev, "get steering tag: mem_type=%s, req_type=%u, cpu=%d, tag=%#04x\n",
(mem_type == TPH_MEM_TYPE_VM) ? "volatile" : "persistent",
- cpu, *tag);
+ req_type, cpu, *tag);
return 0;
#else
return -ENODEV;
#endif
}
+EXPORT_SYMBOL(pcie_tph_get_cpu_st_ext);
+
+/**
+ * pcie_tph_get_cpu_st() - Retrieve Steering Tag for a target memory associated
+ * with a specific CPU
+ * @pdev: PCI device
+ * @mem_type: target memory type (volatile or persistent RAM)
+ * @cpu: associated CPU id
+ * @tag: Steering Tag to be returned
+ *
+ * Return the Steering Tag for a target memory associated with a specific CPU,
+ * using the default negotiated TPH requester type.
+ *
+ * Return: 0 if success, otherwise negative value (-errno)
+ */
+int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
+ unsigned int cpu, u16 *tag)
+{
+ return pcie_tph_get_cpu_st_ext(pdev, mem_type, pdev->tph_max_type,
+ cpu, tag);
+}
EXPORT_SYMBOL(pcie_tph_get_cpu_st);
/**
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index f021419251cc..04c93822ee6d 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -25,8 +25,9 @@ enum tph_mem_type {
#ifdef CONFIG_PCIE_TPH
int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag);
-int pcie_tph_get_cpu_st(struct pci_dev *dev,
- enum tph_mem_type mem_type,
+int pcie_tph_get_cpu_st_ext(struct pci_dev *dev, enum tph_mem_type mem_type,
+ u8 req_type, unsigned int cpu, u16 *tag);
+int pcie_tph_get_cpu_st(struct pci_dev *dev, enum tph_mem_type mem_type,
unsigned int cpu, u16 *tag);
void pcie_disable_tph(struct pci_dev *pdev);
int pcie_enable_tph_ext(struct pci_dev *pdev, u8 mode, u8 req_type);
@@ -37,6 +38,11 @@ u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev);
static inline int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag)
{ return -EINVAL; }
+static inline int pcie_tph_get_cpu_st_ext(struct pci_dev *dev,
+ enum tph_mem_type mem_type,
+ u8 req_type, unsigned int cpu,
+ u16 *tag)
+{ return -EINVAL; }
static inline int pcie_tph_get_cpu_st(struct pci_dev *dev,
enum tph_mem_type mem_type,
unsigned int cpu, u16 *tag)
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 06/13] PCI/TPH: Add helpers to check standard and extended TPH support
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (4 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:47 ` sashiko-bot
2026-11-23 2:36 ` [PATCH v22 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
` (8 subsequent siblings)
14 siblings, 1 reply; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Add two helper functions for TPH capability checking:
- pcie_std_tph_supported(): check basic standard TPH support
- pcie_ext_tph_supported(): check extended TPH requester support
They rely on the cached tph_max_type value validated during probe,
and provide consistent capability queries for subsequent VFIO TPH
virtualization code. Stub definitions are added for !CONFIG_PCIE_TPH.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
include/linux/pci-tph.h | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index 04c93822ee6d..5defe407ef75 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -23,6 +23,24 @@ enum tph_mem_type {
};
#ifdef CONFIG_PCIE_TPH
+/**
+ * pcie_std_tph_supported - check standard TPH requester support
+ * @pdev: PCI device
+ */
+static inline bool pcie_std_tph_supported(struct pci_dev *pdev)
+{
+ return pdev->tph_max_type >= PCI_TPH_REQ_TPH_ONLY;
+}
+
+/**
+ * pcie_ext_tph_supported - check extended TPH requester support
+ * @pdev: PCI device
+ */
+static inline bool pcie_ext_tph_supported(struct pci_dev *pdev)
+{
+ return pdev->tph_max_type == PCI_TPH_REQ_EXT_TPH;
+}
+
int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag);
int pcie_tph_get_cpu_st_ext(struct pci_dev *dev, enum tph_mem_type mem_type,
@@ -35,6 +53,10 @@ int pcie_enable_tph(struct pci_dev *pdev, u8 mode);
u16 pcie_tph_get_st_table_size(struct pci_dev *pdev);
u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev);
#else
+static inline bool pcie_std_tph_supported(struct pci_dev *pdev)
+{ return false; }
+static inline bool pcie_ext_tph_supported(struct pci_dev *pdev)
+{ return false; }
static inline int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag)
{ return -EINVAL; }
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (5 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 06/13] PCI/TPH: Add helpers to check standard and extended TPH support Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:48 ` sashiko-bot
2026-11-23 2:36 ` [PATCH v22 08/13] PCI/TPH: Add pcie_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
` (7 subsequent siblings)
14 siblings, 1 reply; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Add batch variant pcie_tph_set_st_entries() to write a contiguous range
of ST table entries, returning the number of successfully written entries
or negative errno.
- Keep original pcie_tph_set_st_entry() as a wrapper for backward
compatibility
- Disable/enable TPH once per batch instead of per entry for better
performance
- Add header declaration and fallback static inline version
- Add error logging for failed ST entry writes
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/pci/tph.c | 77 +++++++++++++++++++++++++----------------
include/linux/pci-tph.h | 6 ++++
2 files changed, 54 insertions(+), 29 deletions(-)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index 51a065d83104..be44b27bd1ce 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -311,21 +311,19 @@ int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
EXPORT_SYMBOL(pcie_tph_get_cpu_st);
/**
- * pcie_tph_set_st_entry() - Set Steering Tag in the ST table entry
+ * pcie_tph_set_st_entries - Batch set Steering Tag entries in the ST table
* @pdev: PCI device
- * @index: ST table entry index
- * @tag: Steering Tag to be written
+ * @start: starting ST table index
+ * @count: number of contiguous entries to write
+ * @tags: array of u16 tag values to program
*
- * Figure out the proper location of ST table, either in the MSI-X table or
- * in the TPH Extended Capability space, and write the Steering Tag into
- * the ST entry pointed by index.
- *
- * Return: 0 if success, otherwise negative value (-errno)
+ * Return: number of successfully written entries (>0), or negative errno
*/
-int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
+int pcie_tph_set_st_entries(struct pci_dev *pdev, unsigned int start,
+ unsigned int count, const u16 *tags)
{
- u32 loc;
int err = 0;
+ u32 loc, i;
if (!pdev->tph_cap)
return -EINVAL;
@@ -333,9 +331,12 @@ int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
if (!pdev->tph_enabled)
return -EINVAL;
+ if (!count)
+ return -EINVAL;
+
/* No need to write tag if device is in "No ST Mode" */
if (pdev->tph_mode == PCI_TPH_ST_NS_MODE)
- return 0;
+ return count;
/*
* Disable TPH before updating ST to avoid potential instability as
@@ -344,29 +345,47 @@ int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
set_ctrl_reg_req_en(pdev, PCI_TPH_REQ_DISABLE);
loc = pcie_tph_get_st_table_loc(pdev);
-
- switch (loc) {
- case PCI_TPH_LOC_MSIX:
- err = pci_msix_write_tph_tag(pdev, index, tag);
- break;
- case PCI_TPH_LOC_CAP:
- err = write_tag_to_st_table(pdev, index, tag);
- break;
- default:
- err = -EINVAL;
- }
-
- if (err) {
- pcie_disable_tph(pdev);
- return err;
+ for (i = 0; i < count; i++) {
+ switch (loc) {
+ case PCI_TPH_LOC_MSIX:
+ err = pci_msix_write_tph_tag(pdev, start + i, tags[i]);
+ break;
+ case PCI_TPH_LOC_CAP:
+ err = write_tag_to_st_table(pdev, start + i, tags[i]);
+ break;
+ default:
+ err = -EINVAL;
+ }
+ if (err != 0) {
+ pci_err(pdev, "failed to set steering tag: %s table, index=%u, tags=%u, ret=%d\n",
+ (loc == PCI_TPH_LOC_MSIX) ? "MSI-X" : "ST",
+ start + i, tags[i], err);
+ set_ctrl_reg_req_en(pdev, pdev->tph_req_type);
+ return i > 0 ? i : (err > 0 ? -EINVAL : err);
+ }
}
set_ctrl_reg_req_en(pdev, pdev->tph_req_type);
- pci_dbg(pdev, "set steering tag: %s table, index=%d, tag=%#04x\n",
- (loc == PCI_TPH_LOC_MSIX) ? "MSI-X" : "ST", index, tag);
+ pci_dbg(pdev, "batch set steering tag: %s table, start=%u, count=%u success!\n",
+ (loc == PCI_TPH_LOC_MSIX) ? "MSI-X" : "ST", start, count);
- return 0;
+ return count;
+}
+EXPORT_SYMBOL(pcie_tph_set_st_entries);
+
+/**
+ * pcie_tph_set_st_entry() - Set Steering Tag in the ST table entry
+ * @pdev: PCI device
+ * @index: ST table entry index
+ * @tag: Steering Tag to be written
+ *
+ * Return: 0 if success, otherwise negative value (-errno)
+ */
+int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
+{
+ int ret = pcie_tph_set_st_entries(pdev, index, 1, &tag);
+ return ret == 1 ? 0 : ret;
}
EXPORT_SYMBOL(pcie_tph_set_st_entry);
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index 5defe407ef75..703876d35ebe 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -43,6 +43,8 @@ static inline bool pcie_ext_tph_supported(struct pci_dev *pdev)
int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag);
+int pcie_tph_set_st_entries(struct pci_dev *pdev, unsigned int start,
+ unsigned int count, const u16 *tags);
int pcie_tph_get_cpu_st_ext(struct pci_dev *dev, enum tph_mem_type mem_type,
u8 req_type, unsigned int cpu, u16 *tag);
int pcie_tph_get_cpu_st(struct pci_dev *dev, enum tph_mem_type mem_type,
@@ -60,6 +62,10 @@ static inline bool pcie_ext_tph_supported(struct pci_dev *pdev)
static inline int pcie_tph_set_st_entry(struct pci_dev *pdev,
unsigned int index, u16 tag)
{ return -EINVAL; }
+static inline int pcie_tph_set_st_entries(struct pci_dev *pdev,
+ unsigned int start,
+ unsigned int count, const u16 *tags)
+{ return -EINVAL; }
static inline int pcie_tph_get_cpu_st_ext(struct pci_dev *dev,
enum tph_mem_type mem_type,
u8 req_type, unsigned int cpu,
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 08/13] PCI/TPH: Add pcie_tph_dsm_supported() helper to detect device TPH ST _DSM
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (6 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 09/13] vfio/pci: Reset PCIe TPH state on device open and close Chengwen Feng
` (6 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Add new helper pcie_tph_dsm_supported() to check whether the root port
of a given PCI device exposes the TPH ST ACPI _DSM method.
Wrap all ACPI logic under CONFIG_ACPI guard, return false directly
when ACPI is disabled.
This helper is prepared for follow-up VFIO TPH virtualization patches
to uniformly judge platform CPU TPH resolving support.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/pci/tph.c | 28 ++++++++++++++++++++++++++++
include/linux/pci-tph.h | 3 +++
2 files changed, 31 insertions(+)
diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
index be44b27bd1ce..eb19af6413cb 100644
--- a/drivers/pci/tph.c
+++ b/drivers/pci/tph.c
@@ -587,3 +587,31 @@ void pci_tph_init(struct pci_dev *pdev)
save_size = sizeof(u32) + num_entries * sizeof(u16);
pci_add_ext_cap_save_buffer(pdev, PCI_EXT_CAP_ID_TPH, save_size);
}
+
+/**
+ * pcie_tph_dsm_supported - Check if root port exposes TPH ST _DSM
+ * @pdev: target PCI device
+ *
+ * Return: true if root port of @pdev provides TPH ST DSM function,
+ * false otherwise (ACPI disabled or DSM missing).
+ */
+bool pcie_tph_dsm_supported(struct pci_dev *pdev)
+{
+#ifdef CONFIG_ACPI
+ struct pci_dev *rp = pcie_find_root_port(pdev);
+ acpi_handle handle;
+
+ if (!rp || !rp->bus || !rp->bus->bridge)
+ return false;
+
+ handle = ACPI_HANDLE(rp->bus->bridge);
+ if (!handle)
+ return false;
+
+ return acpi_check_dsm(handle, &pci_acpi_dsm_guid, 7,
+ BIT(TPH_ST_DSM_FUNC_INDEX));
+#else
+ return false;
+#endif
+}
+EXPORT_SYMBOL_GPL(pcie_tph_dsm_supported);
diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
index 703876d35ebe..ace65be00bc8 100644
--- a/include/linux/pci-tph.h
+++ b/include/linux/pci-tph.h
@@ -54,6 +54,7 @@ int pcie_enable_tph_ext(struct pci_dev *pdev, u8 mode, u8 req_type);
int pcie_enable_tph(struct pci_dev *pdev, u8 mode);
u16 pcie_tph_get_st_table_size(struct pci_dev *pdev);
u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev);
+bool pcie_tph_dsm_supported(struct pci_dev *pdev);
#else
static inline bool pcie_std_tph_supported(struct pci_dev *pdev)
{ return false; }
@@ -85,6 +86,8 @@ static inline u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)
{ return 0; }
static inline u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)
{ return PCI_TPH_LOC_NONE; }
+static inline bool pcie_tph_dsm_supported(struct pci_dev *pdev)
+{ return false; }
#endif
#endif /* LINUX_PCI_TPH_H */
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 09/13] vfio/pci: Reset PCIe TPH state on device open and close
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (7 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 08/13] PCI/TPH: Add pcie_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 10/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support Chengwen Feng
` (5 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Hardware resets clear PCI TPH registers, but PCI core software TPH state
may remain stale across VFIO device open/close cycles.
Invoke pcie_disable_tph() at enable and disable boundaries:
- In vfio_pci_core_enable(): pcie_disable_tph() runs before device reset,
to prevent stale TPH state from being saved/restored by PCI core reset
logic
- In vfio_pci_core_disable(): pcie_disable_tph() is placed before
vfio_pci_set_irqs_ioctl(), disabling TPH ahead of MSI-X state teardown.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/vfio/pci/vfio_pci_core.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index f2d5b6857925..8d2aa5585c1b 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -22,6 +22,7 @@
#include <linux/notifier.h>
#include <linux/pagemap.h>
#include <linux/pci.h>
+#include <linux/pci-tph.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/types.h>
@@ -595,6 +596,9 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
if (ret)
goto out_power;
+ /* Reset TPH state for new user session */
+ pcie_disable_tph(vdev->pdev);
+
/* If reset fails because of the device lock, fail this path entirely */
ret = pci_try_reset_function(pdev);
if (ret == -EAGAIN)
@@ -704,6 +708,9 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
/* Stop the device from further DMA */
pci_clear_master(pdev);
+ /* Reset TPH state on user session exit */
+ pcie_disable_tph(vdev->pdev);
+
vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
VFIO_IRQ_SET_ACTION_TRIGGER,
vdev->irq_type, 0, 0, NULL);
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 10/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (8 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 09/13] vfio/pci: Reset PCIe TPH state on device open and close Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 11/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support Chengwen Feng
` (4 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Implement basic PCIe TPH support restricted to NO-ST mode:
- Add a tph_policy module parameter, currently locked to NO-ST.
The parameter range is reserved for future TPH policies.
- Virtualize the TPH extended capability register: mask IV/DS
modes, ST location and ST size fields, only NO-ST capability
is visible to userspace.
- Gate all TPH control register writes behind tph_opt_in.
Opt-in is one-shot (0 -> 1 only), no reset path.
- Validate TPH control register writes: only NO-ST mode and
supported requester enable values are accepted; invalid writes
are dropped. CTRL reads always fetch hardware state directly.
- Add VFIO_DEVICE_FEATURE_TPH ioctl for opt-in (SET) and VFIO
usable capability query (GET). GET fails with -EINVAL if the
device has not been opted in.
- ST table accesses beyond the base TPH capability return zero
on read and writes are discarded.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/vfio/pci/vfio_pci.c | 16 +++
drivers/vfio/pci/vfio_pci_config.c | 170 ++++++++++++++++++++++++++---
drivers/vfio/pci/vfio_pci_core.c | 45 ++++++++
drivers/vfio/pci/vfio_pci_priv.h | 4 +
include/linux/vfio_pci_core.h | 3 +
include/uapi/linux/vfio.h | 22 ++++
6 files changed, 243 insertions(+), 17 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index e7c012febe91..77764eabb71a 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -60,6 +60,21 @@ static bool disable_denylist;
module_param(disable_denylist, bool, 0444);
MODULE_PARM_DESC(disable_denylist, "Disable use of device denylist. Disabling the denylist allows binding to devices with known errata that may lead to exploitable stability or security issues when accessed by untrusted users.");
+static unsigned int tph_policy;
+static int tph_policy_set(const char *val, const struct kernel_param *kp)
+{
+ return param_set_uint_minmax(val, kp, VFIO_PCI_TPH_POLICY_NO_ST,
+ VFIO_PCI_TPH_POLICY_NO_ST);
+}
+static const struct kernel_param_ops tph_param_ops = {
+ .set = tph_policy_set,
+ .get = param_get_uint,
+};
+module_param_cb(tph_policy, &tph_param_ops, &tph_policy, 0644);
+MODULE_PARM_DESC(tph_policy,
+ "Global TPH policy level (0=default No-ST):\n"
+ "0 = No-ST mode: ST programming unavailable\n");
+
static bool vfio_pci_dev_in_denylist(struct pci_dev *pdev)
{
switch (pdev->vendor) {
@@ -142,6 +157,7 @@ static int vfio_pci_init_dev(struct vfio_device *core_vdev)
#ifdef CONFIG_VFIO_PCI_VGA
vdev->disable_vga = disable_vga;
#endif
+ vdev->tph_policy = tph_policy;
return vfio_pci_core_init_dev(core_vdev);
}
diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
index 9914f3ac69ae..38f747de82d8 100644
--- a/drivers/vfio/pci/vfio_pci_config.c
+++ b/drivers/vfio/pci/vfio_pci_config.c
@@ -20,8 +20,10 @@
* must be negotiated with the underlying OS.
*/
+#include <linux/bitfield.h>
#include <linux/fs.h>
#include <linux/pci.h>
+#include <linux/pci-tph.h>
#include <linux/uaccess.h>
#include <linux/vfio.h>
#include <linux/slab.h>
@@ -1085,6 +1087,151 @@ static int __init init_pci_ext_cap_pwr_perm(struct perm_bits *perm)
return 0;
}
+static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos)
+{
+ u8 cap;
+ int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
+ PCI_STD_HEADER_SIZEOF;
+ cap = vdev->pci_config_map[pos];
+
+ if (cap == PCI_CAP_ID_BASIC)
+ return 0;
+
+ /* XXX Can we have to abutting capabilities of the same type? */
+ while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap)
+ pos--;
+
+ return pos;
+}
+
+/* Permissions for TPH extended capability */
+static int __init init_pci_ext_cap_tph_perm(struct perm_bits *perm)
+{
+ /*
+ * We only virtualize the TPH header (excluding the ST table).
+ * For the ST table (if any), the read operation returns 0 by
+ * default, and the write operation is discarded by default.
+ */
+ if (alloc_perm_bits(perm, PCI_TPH_BASE_SIZEOF))
+ 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);
+
+ return 0;
+}
+
+static void vfio_tph_vcap_rewrite(struct vfio_pci_core_device *vdev, int pos)
+{
+ __le32 *vptr = (__le32 *)&vdev->vconfig[pos + PCI_TPH_CAP];
+ struct pci_dev *pdev = vdev->pdev;
+ u32 val = le32_to_cpu(*vptr);
+
+ if (!pcie_ext_tph_supported(pdev)) {
+ /* Remove extended TPH if root-port doesn't support */
+ val &= ~PCI_TPH_CAP_EXT_TPH;
+ }
+
+ if (vdev->tph_policy == VFIO_PCI_TPH_POLICY_NO_ST) {
+ /*
+ * NO-ST policy: mask IV/DS mode, ST location and ST size fields.
+ * Userspace only sees NO-ST capability.
+ */
+ val &= ~(PCI_TPH_CAP_ST_IV | PCI_TPH_CAP_ST_DS |
+ PCI_TPH_CAP_LOC_MASK | PCI_TPH_CAP_ST_MASK);
+ }
+
+ *vptr = cpu_to_le32(val);
+
+ /* Cache the final virtualized TPH capability value */
+ vdev->tph_vcap = val;
+}
+
+static int vfio_tph_config_read(struct vfio_pci_core_device *vdev, int pos,
+ int count, struct perm_bits *perm,
+ int offset, __le32 *val)
+{
+ int ret;
+
+ if (offset >= PCI_TPH_BASE_SIZEOF) {
+ *val = cpu_to_le32(0);
+ return count;
+ }
+
+ if (offset < PCI_TPH_CTRL)
+ return vfio_default_config_read(vdev, pos, count, perm,
+ offset, val);
+
+ /* Read the actual device value for CTRL register. */
+ ret = vfio_user_config_read(vdev->pdev, pos, val, count);
+ return ret != 0 ? ret : count;
+}
+
+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;
+ u8 mode, org_mode, req, org_req;
+ u32 org_ctrl, new_ctrl, cap;
+ __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);
+ org_mode = FIELD_GET(PCI_TPH_CTRL_MODE_SEL_MASK, org_ctrl);
+ if (req == org_req && mode == org_mode)
+ goto out; /* No change to requester type or ST mode. */
+
+ if (req == PCI_TPH_REQ_TPH_ONLY || req == PCI_TPH_REQ_EXT_TPH) {
+ pcie_disable_tph(pdev);
+ pcie_enable_tph_ext(pdev, mode, req);
+ } else if (req == PCI_TPH_REQ_DISABLE) {
+ pcie_disable_tph(pdev);
+ }
+
+out:
+ vfio_pci_memory_unlock_and_restore(vdev, cmd);
+ return count;
+}
+
/*
* Initialize the shared permission tables
*/
@@ -1100,6 +1247,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)
@@ -1120,6 +1268,9 @@ 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].readfn = vfio_tph_config_read;
+ 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;
@@ -1129,23 +1280,6 @@ int __init vfio_pci_init_perm_bits(void)
return ret;
}
-static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos)
-{
- u8 cap;
- int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
- PCI_STD_HEADER_SIZEOF;
- cap = vdev->pci_config_map[pos];
-
- if (cap == PCI_CAP_ID_BASIC)
- return 0;
-
- /* XXX Can we have to abutting capabilities of the same type? */
- while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap)
- pos--;
-
- return pos;
-}
-
static int vfio_msi_config_read(struct vfio_pci_core_device *vdev, int pos,
int count, struct perm_bits *perm,
int offset, __le32 *val)
@@ -1702,6 +1836,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)
+ vfio_tph_vcap_rewrite(vdev, epos);
/*
* If we're just using this capability to anchor the list,
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 8d2aa5585c1b..be7d1921f5f3 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -633,6 +633,8 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
if (ret)
goto out_free_zdev;
+ vdev->tph_opt_in = 0;
+
msix_pos = pdev->msix_cap;
if (msix_pos) {
u16 flags;
@@ -1601,6 +1603,47 @@ static int vfio_pci_core_feature_token(struct vfio_pci_core_device *vdev,
return 0;
}
+static u32 vfio_pci_tph_get_capability(struct vfio_pci_core_device *vdev)
+{
+ /* NO-ST only, no capability bits exposed now */
+ 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;
+ int ret;
+
+ if (!pcie_std_tph_supported(vdev->pdev))
+ return -EOPNOTSUPP;
+
+ ret = vfio_check_feature(flags, argsz,
+ VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET,
+ sizeof(tph));
+ if (ret != 1)
+ return ret;
+
+ if (copy_from_user(&tph, arg, sizeof(tph)))
+ return -EFAULT;
+
+ if (flags & VFIO_DEVICE_FEATURE_SET) {
+ if (tph.flags != 0)
+ return -EINVAL;
+ vdev->tph_opt_in = 1;
+ return 0;
+ }
+
+ if (!vdev->tph_opt_in)
+ return -EINVAL;
+
+ tph.flags = vfio_pci_tph_get_capability(vdev);
+
+ 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 +1668,8 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
return vfio_pci_zdev_feature_fmb_enable(vdev, flags, arg, argsz);
case VFIO_DEVICE_FEATURE_ZPCI_FMB_READ:
return vfio_pci_zdev_feature_fmb_read(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/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index e04d7e9d0c30..eef76a401edb 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -11,6 +11,10 @@
/* Cap maximum number of ioeventfds per device (arbitrary) */
#define VFIO_PCI_IOEVENTFD_MAX 1000
+enum vfio_pci_tph_policy {
+ VFIO_PCI_TPH_POLICY_NO_ST = 0,
+};
+
struct vfio_pci_ioeventfd {
struct list_head next;
struct vfio_pci_core_device *vdev;
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index 2f3fa2787737..17c4ad490dc2 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -151,6 +151,9 @@ struct vfio_pci_core_device {
struct notifier_block nb;
struct rw_semaphore memory_lock;
struct list_head dmabufs;
+ u8 tph_policy;
+ u8 tph_opt_in;
+ u32 tph_vcap;
};
enum vfio_pci_io_width {
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 7e9adb61c5e1..52d324139644 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -1585,6 +1585,28 @@ struct vfio_device_feature_zpci_fmb_read {
__aligned_u64 data;
};
+/*
+ * VFIO_DEVICE_FEATURE_TPH - Control and query VFIO TPH feature capabilities
+ *
+ * SET: Opt-in to enable the VFIO TPH feature for the device; flags must be
+ * zero.
+ * GET: Return VFIO-usable TPH capability bits in flags (currently zero).
+ * Returns an error if SET opt-in has not been performed.
+ *
+ * Typical userspace sequence:
+ * 1. Read virtualized PCI TPH capability via VFIO config space access to
+ * discover underlying hardware PCI TPH support.
+ * 2. Invoke SET to opt-in for VFIO TPH feature permission.
+ * 3. Invoke GET to retrieve the set of TPH capabilities accessible via VFIO.
+ * 4. Write the TPH control register through VFIO config space to activate
+ * device TPH hardware.
+ */
+#define VFIO_DEVICE_FEATURE_TPH 16
+
+struct vfio_device_feature_tph {
+ __u32 flags;
+};
+
/* -------- API for Type1 VFIO IOMMU -------- */
/**
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 11/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (9 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 10/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:58 ` sashiko-bot
2026-11-23 2:36 ` [PATCH v22 12/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability Chengwen Feng
` (3 subsequent siblings)
14 siblings, 1 reply; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Extend the NO-ST baseline with an IV-ST policy and introduce
VFIO_DEVICE_FEATURE_TPH_PROGRAM for batch programming of architected ST
entries.
- Add VFIO_PCI_TPH_POLICY_IV_ST and extend the tph_policy module
parameter:
* IV-ST: expose IV mode and architected ST table access, hide DS
- Restrict TPH control register writes to NO-ST and IV-ST only;
block device-specific TPH mode
- VFIO TPH GET reports PROG_ST_NONE and PROG_ST_CPU capabilities after
opt-in
- Add VFIO_DEVICE_FEATURE_TPH_PROGRAM (SET-only) to batch program
architected ST entries for NONE and CPU (volatile/persistent) sources
- All ST programming is gated behind tph_opt_in and policy checks
- Register a per-device cpuhp offline callback: automatically clear ST
entries bound to an offlined CPU and set those ST values to zero
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/vfio/pci/vfio_pci.c | 5 +-
drivers/vfio/pci/vfio_pci_config.c | 20 +-
drivers/vfio/pci/vfio_pci_core.c | 295 ++++++++++++++++++++++++++++-
drivers/vfio/pci/vfio_pci_priv.h | 1 +
include/linux/vfio_pci_core.h | 4 +
include/uapi/linux/vfio.h | 74 +++++++-
6 files changed, 390 insertions(+), 9 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index 77764eabb71a..664775bb3526 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -64,7 +64,7 @@ static unsigned int tph_policy;
static int tph_policy_set(const char *val, const struct kernel_param *kp)
{
return param_set_uint_minmax(val, kp, VFIO_PCI_TPH_POLICY_NO_ST,
- VFIO_PCI_TPH_POLICY_NO_ST);
+ VFIO_PCI_TPH_POLICY_IV_ST);
}
static const struct kernel_param_ops tph_param_ops = {
.set = tph_policy_set,
@@ -73,7 +73,8 @@ static const struct kernel_param_ops tph_param_ops = {
module_param_cb(tph_policy, &tph_param_ops, &tph_policy, 0644);
MODULE_PARM_DESC(tph_policy,
"Global TPH policy level (0=default No-ST):\n"
- "0 = No-ST mode: ST programming unavailable\n");
+ "0 = No-ST mode: ST programming unavailable\n"
+ "1 = Add IV-ST mode: ST programming supports NONE/CPU sources\n");
static bool vfio_pci_dev_in_denylist(struct pci_dev *pdev)
{
diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
index 38f747de82d8..2bae386a8469 100644
--- a/drivers/vfio/pci/vfio_pci_config.c
+++ b/drivers/vfio/pci/vfio_pci_config.c
@@ -1135,6 +1135,16 @@ static void vfio_tph_vcap_rewrite(struct vfio_pci_core_device *vdev, int pos)
val &= ~PCI_TPH_CAP_EXT_TPH;
}
+ if (!pcie_tph_dsm_supported(pdev)) {
+ /*
+ * Mask IV/DS mode, ST location and ST size fields if
+ * DSM is not supported. So userspace only sees NO-ST
+ * capability.
+ */
+ val &= ~(PCI_TPH_CAP_ST_IV | PCI_TPH_CAP_ST_DS |
+ PCI_TPH_CAP_LOC_MASK | PCI_TPH_CAP_ST_MASK);
+ }
+
if (vdev->tph_policy == VFIO_PCI_TPH_POLICY_NO_ST) {
/*
* NO-ST policy: mask IV/DS mode, ST location and ST size fields.
@@ -1142,6 +1152,14 @@ static void vfio_tph_vcap_rewrite(struct vfio_pci_core_device *vdev, int pos)
*/
val &= ~(PCI_TPH_CAP_ST_IV | PCI_TPH_CAP_ST_DS |
PCI_TPH_CAP_LOC_MASK | PCI_TPH_CAP_ST_MASK);
+ } else if (vdev->tph_policy == VFIO_PCI_TPH_POLICY_IV_ST) {
+ /*
+ * Add IV policy: mask DS mode, drop ST location and size
+ * if device does not support IV mode.
+ */
+ val &= ~PCI_TPH_CAP_ST_DS;
+ if (!(val & PCI_TPH_CAP_ST_IV))
+ val &= ~(PCI_TPH_CAP_LOC_MASK | PCI_TPH_CAP_ST_MASK);
}
*vptr = cpu_to_le32(val);
@@ -1210,7 +1228,7 @@ static int vfio_tph_config_write(struct vfio_pci_core_device *vdev, int pos,
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)) ||
+ if (mode > PCI_TPH_ST_IV_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. */
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index be7d1921f5f3..83b81d1a6122 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -11,6 +11,7 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/aperture.h>
+#include <linux/cpuhotplug.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/eventfd.h>
@@ -576,6 +577,9 @@ static void vfio_pci_core_map_bars(struct vfio_pci_core_device *vdev)
}
}
+static int vfio_pci_tph_dev_init(struct vfio_pci_core_device *vdev);
+static void vfio_pci_tph_dev_deinit(struct vfio_pci_core_device *vdev);
+
int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
{
struct pci_dev *pdev = vdev->pdev;
@@ -633,7 +637,10 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
if (ret)
goto out_free_zdev;
- vdev->tph_opt_in = 0;
+ /* After identifying the capability, initialize TPH. */
+ ret = vfio_pci_tph_dev_init(vdev);
+ if (ret != 0)
+ goto out_free_config;
msix_pos = pdev->msix_cap;
if (msix_pos) {
@@ -659,6 +666,8 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
return 0;
+out_free_config:
+ vfio_config_free(vdev);
out_free_zdev:
vfio_pci_zdev_close_device(vdev);
out_free_state:
@@ -710,6 +719,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
/* Stop the device from further DMA */
pci_clear_master(pdev);
+ vfio_pci_tph_dev_deinit(vdev);
/* Reset TPH state on user session exit */
pcie_disable_tph(vdev->pdev);
@@ -1603,12 +1613,135 @@ static int vfio_pci_core_feature_token(struct vfio_pci_core_device *vdev,
return 0;
}
-static u32 vfio_pci_tph_get_capability(struct vfio_pci_core_device *vdev)
+static enum cpuhp_state vfio_tph_cpuhp_state;
+
+static int vfio_pci_tph_cpuhp_offline(unsigned int cpu, struct hlist_node *node)
{
- /* NO-ST only, no capability bits exposed now */
+ struct vfio_pci_core_device *vdev =
+ hlist_entry(node, struct vfio_pci_core_device, tph_cpuhp_node);
+ u16 cmd;
+ int i;
+
+ if (vdev->tph_st_cpu == NULL)
+ return 0;
+
+ mutex_lock(&vdev->tph_lock);
+ cmd = vfio_pci_memory_lock_and_enable(vdev);
+ for (i = 0; i < vdev->tph_st_size; i++) {
+ if (vdev->tph_st_cpu[i] != (s64)cpu)
+ continue;
+ pcie_tph_set_st_entry(vdev->pdev, i, 0);
+ vdev->tph_st_cpu[i] = (s64)-1;
+ }
+ vfio_pci_memory_unlock_and_restore(vdev, cmd);
+ mutex_unlock(&vdev->tph_lock);
+
return 0;
}
+static int vfio_pci_tph_cpuhp_init(void)
+{
+ int ret;
+
+ ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
+ "vfio/pci/tph:online",
+ NULL,
+ vfio_pci_tph_cpuhp_offline);
+ if (ret < 0)
+ return ret;
+
+ vfio_tph_cpuhp_state = ret;
+ return 0;
+}
+
+static void vfio_pci_tph_cpuhp_deinit(void)
+{
+ cpuhp_remove_multi_state(vfio_tph_cpuhp_state);
+}
+
+static u32 vfio_pci_tph_get_st_table_size(struct vfio_pci_core_device *vdev)
+{
+ struct pci_dev *pdev = vdev->pdev;
+ u32 loc = pcie_tph_get_st_table_loc(pdev);
+ int ret;
+
+ if (loc == PCI_TPH_LOC_CAP) {
+ return pcie_tph_get_st_table_size(pdev);
+ } else if (loc == PCI_TPH_LOC_MSIX) {
+ ret = pci_msix_vec_count(pdev);
+ if (ret < 0)
+ return 0;
+ return ret;
+ } else {
+ return 0;
+ }
+}
+
+static bool vfio_pci_tph_has_architected_st(struct vfio_pci_core_device *vdev)
+{
+ u32 loc = vdev->tph_vcap & PCI_TPH_CAP_LOC_MASK;
+
+ return (loc == PCI_TPH_LOC_CAP || loc == PCI_TPH_LOC_MSIX) &&
+ vdev->tph_st_size > 0;
+}
+
+static int vfio_pci_tph_dev_init(struct vfio_pci_core_device *vdev)
+{
+ bool has_arch_st;
+ u32 st_size;
+ int i, ret;
+
+ vdev->tph_opt_in = 0;
+ vdev->tph_st_cpu = NULL;
+ vdev->tph_st_size = vfio_pci_tph_get_st_table_size(vdev);
+
+ has_arch_st = vfio_pci_tph_has_architected_st(vdev);
+ if (!has_arch_st)
+ return 0;
+
+ st_size = vdev->tph_st_size;
+ vdev->tph_st_cpu = kcalloc(st_size, sizeof(*vdev->tph_st_cpu),
+ GFP_KERNEL);
+ if (vdev->tph_st_cpu == NULL)
+ return -ENOMEM;
+
+ for (i = 0; i < st_size; i++)
+ vdev->tph_st_cpu[i] = (s64)-1;
+
+ ret = cpuhp_state_add_instance_nocalls(vfio_tph_cpuhp_state,
+ &vdev->tph_cpuhp_node);
+ if (ret != 0) {
+ kfree(vdev->tph_st_cpu);
+ vdev->tph_st_cpu = NULL;
+ return ret;
+ }
+
+ return 0;
+}
+
+static void vfio_pci_tph_dev_deinit(struct vfio_pci_core_device *vdev)
+{
+ if (vdev->tph_st_cpu != NULL) {
+ cpuhp_state_remove_instance_nocalls(vfio_tph_cpuhp_state,
+ &vdev->tph_cpuhp_node);
+ kfree(vdev->tph_st_cpu);
+ vdev->tph_st_cpu = NULL;
+ }
+ vdev->tph_opt_in = 0;
+ vdev->tph_vcap = 0;
+}
+
+static u32 vfio_pci_tph_get_capability(struct vfio_pci_core_device *vdev)
+{
+ bool has_arch_st;
+
+ has_arch_st = vfio_pci_tph_has_architected_st(vdev);
+ if (!has_arch_st)
+ return 0;
+
+ return VFIO_DEVICE_TPH_CAP_PROG_ST_NONE | VFIO_DEVICE_TPH_CAP_PROG_ST_CPU;
+}
+
static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev,
u32 flags,
struct vfio_device_feature_tph __user *arg,
@@ -1644,6 +1777,144 @@ static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev,
return copy_to_user(arg, &tph, sizeof(tph)) ? -EFAULT : 0;
}
+static u32 vfio_pci_tph_get_src_mask(struct vfio_pci_core_device *vdev)
+{
+ u32 flags = vfio_pci_tph_get_capability(vdev);
+ u32 src_mask = 0;
+
+ if (flags & VFIO_DEVICE_TPH_CAP_PROG_ST_NONE)
+ src_mask |= VFIO_DEVICE_TPH_SRC_NONE;
+ if (flags & VFIO_DEVICE_TPH_CAP_PROG_ST_CPU)
+ src_mask |= VFIO_DEVICE_TPH_SRC_CPU_VOLATILE |
+ VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT;
+
+ return src_mask;
+}
+
+static int vfio_pci_tph_resolve_st_tag(struct pci_dev *pdev, u32 src_bits,
+ u32 src_handle, bool extended, u16 *st)
+{
+ u8 req_type = extended ? PCI_TPH_REQ_EXT_TPH : PCI_TPH_REQ_TPH_ONLY;
+ u32 cpu_srcs = VFIO_DEVICE_TPH_SRC_CPU_VOLATILE |
+ VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT;
+ int ret = 0;
+
+ if ((src_bits & cpu_srcs) && !cpu_online(src_handle)) {
+ *st = 0;
+ return -EINVAL;
+ }
+
+ if (src_bits & VFIO_DEVICE_TPH_SRC_NONE)
+ *st = 0;
+ else if (src_bits & VFIO_DEVICE_TPH_SRC_CPU_VOLATILE)
+ ret = pcie_tph_get_cpu_st_ext(pdev, TPH_MEM_TYPE_VM,
+ req_type, src_handle, st);
+ else if (src_bits & VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT)
+ ret = pcie_tph_get_cpu_st_ext(pdev, TPH_MEM_TYPE_PM,
+ req_type, src_handle, st);
+ else
+ ret = -EINVAL;
+
+ if (ret != 0)
+ *st = 0;
+
+ return ret;
+}
+
+static int vfio_pci_core_feature_tph_program(struct vfio_pci_core_device *vdev,
+ u32 flags,
+ struct vfio_device_feature_tph_program __user *arg,
+ size_t argsz)
+{
+ struct vfio_device_feature_tph_program program;
+ u32 user_flags, src_mask, st_size;
+ struct pci_dev *pdev = vdev->pdev;
+ bool extended, stop_on_zero;
+ u32 *src_handle = NULL;
+ int ret, i, k, written;
+ u16 *tags = NULL;
+ u16 cmd;
+
+ if (!vdev->tph_opt_in ||
+ !vfio_pci_tph_has_architected_st(vdev))
+ return -EOPNOTSUPP;
+
+ ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET,
+ sizeof(program));
+ if (ret != 1)
+ return ret;
+
+ if (copy_from_user(&program, arg, sizeof(program)))
+ return -EFAULT;
+
+ user_flags = program.flags;
+ extended = !!(user_flags & VFIO_DEVICE_TPH_EXTENDED);
+ if (extended && !pcie_ext_tph_supported(vdev->pdev))
+ return -EOPNOTSUPP;
+ user_flags &= ~VFIO_DEVICE_TPH_EXTENDED;
+
+ stop_on_zero = !!(user_flags & VFIO_DEVICE_TPH_REQUIRE_ST);
+ if (stop_on_zero && (user_flags & VFIO_DEVICE_TPH_SRC_NONE))
+ return -EINVAL;
+ user_flags &= ~VFIO_DEVICE_TPH_REQUIRE_ST;
+
+ src_mask = vfio_pci_tph_get_src_mask(vdev);
+ if (user_flags & ~src_mask || !is_power_of_2(user_flags))
+ return -EINVAL;
+
+ st_size = vdev->tph_st_size;
+ if (program.start >= st_size || program.count == 0 ||
+ program.count > st_size - program.start)
+ return -EINVAL;
+
+ if (!(user_flags & VFIO_DEVICE_TPH_SRC_NONE)) {
+ src_handle = memdup_array_user(u64_to_user_ptr(program.srcs),
+ program.count, sizeof(u32));
+ if (IS_ERR(src_handle))
+ return PTR_ERR(src_handle);
+ }
+ tags = kcalloc(program.count, sizeof(u16), GFP_KERNEL_ACCOUNT);
+ if (!tags) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ cpus_read_lock();
+
+ for (i = 0; i < program.count; i++) {
+ ret = vfio_pci_tph_resolve_st_tag(pdev, user_flags,
+ src_handle ? src_handle[i] : 0,
+ extended, &tags[i]);
+ if (ret || (stop_on_zero && tags[i] == 0))
+ break;
+ }
+
+ if (i > 0) {
+ mutex_lock(&vdev->tph_lock);
+
+ cmd = vfio_pci_memory_lock_and_enable(vdev);
+ ret = written = pcie_tph_set_st_entries(pdev, program.start, i,
+ tags);
+ vfio_pci_memory_unlock_and_restore(vdev, cmd);
+
+ if (written > 0) {
+ for (k = 0; k < written; k++)
+ vdev->tph_st_cpu[program.start + k] =
+ (tags[k] == 0) ?
+ (s64)-1 : (s64)src_handle[k];
+ }
+
+ mutex_unlock(&vdev->tph_lock);
+ }
+
+ cpus_read_unlock();
+
+out:
+ kfree(tags);
+ kfree(src_handle);
+ return ret;
+}
+
int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
void __user *arg, size_t argsz)
{
@@ -1670,6 +1941,9 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
return vfio_pci_zdev_feature_fmb_read(vdev, flags, arg, argsz);
case VFIO_DEVICE_FEATURE_TPH:
return vfio_pci_core_feature_tph(vdev, flags, arg, argsz);
+ case VFIO_DEVICE_FEATURE_TPH_PROGRAM:
+ return vfio_pci_core_feature_tph_program(vdev, flags,
+ arg, argsz);
default:
return -ENOTTY;
}
@@ -2232,6 +2506,7 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)
mutex_init(&vdev->igate);
spin_lock_init(&vdev->irqlock);
mutex_init(&vdev->ioeventfds_lock);
+ mutex_init(&vdev->tph_lock);
INIT_LIST_HEAD(&vdev->dummy_resources_list);
INIT_LIST_HEAD(&vdev->ioeventfds_list);
INIT_LIST_HEAD(&vdev->sriov_pfs_item);
@@ -2252,6 +2527,7 @@ void vfio_pci_core_release_dev(struct vfio_device *core_vdev)
container_of(core_vdev, struct vfio_pci_core_device, vdev);
mutex_destroy(&vdev->igate);
+ mutex_destroy(&vdev->tph_lock);
mutex_destroy(&vdev->ioeventfds_lock);
kfree(vdev->region);
kfree(vdev->pm_save);
@@ -2707,12 +2983,23 @@ static void vfio_pci_core_cleanup(void)
{
vfio_pci_uninit_perm_bits();
rcu_barrier();
+ vfio_pci_tph_cpuhp_deinit();
}
static int __init vfio_pci_core_init(void)
{
+ int ret;
+
+ ret = vfio_pci_tph_cpuhp_init();
+ if (ret != 0)
+ return ret;
+
/* Allocate shared config space permission data used by all devices */
- return vfio_pci_init_perm_bits();
+ ret = vfio_pci_init_perm_bits();
+ if (ret != 0)
+ vfio_pci_tph_cpuhp_deinit();
+
+ return ret;
}
module_init(vfio_pci_core_init);
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index eef76a401edb..6b48fa38e51b 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -13,6 +13,7 @@
enum vfio_pci_tph_policy {
VFIO_PCI_TPH_POLICY_NO_ST = 0,
+ VFIO_PCI_TPH_POLICY_IV_ST,
};
struct vfio_pci_ioeventfd {
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index 17c4ad490dc2..48e78a2d5ffb 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -153,7 +153,11 @@ struct vfio_pci_core_device {
struct list_head dmabufs;
u8 tph_policy;
u8 tph_opt_in;
+ u16 tph_st_size;
u32 tph_vcap;
+ s64 *tph_st_cpu;
+ struct hlist_node tph_cpuhp_node;
+ struct mutex tph_lock;
};
enum vfio_pci_io_width {
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 52d324139644..99aac7a1849f 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -1590,8 +1590,9 @@ struct vfio_device_feature_zpci_fmb_read {
*
* SET: Opt-in to enable the VFIO TPH feature for the device; flags must be
* zero.
- * GET: Return VFIO-usable TPH capability bits in flags (currently zero).
- * Returns an error if SET opt-in has not been performed.
+ * GET: Return VFIO-usable TPH capability bits in flags (see
+ * VFIO_DEVICE_TPH_CAP_*). Returns an error if SET opt-in has not been
+ * performed.
*
* Typical userspace sequence:
* 1. Read virtualized PCI TPH capability via VFIO config space access to
@@ -1600,6 +1601,8 @@ struct vfio_device_feature_zpci_fmb_read {
* 3. Invoke GET to retrieve the set of TPH capabilities accessible via VFIO.
* 4. Write the TPH control register through VFIO config space to activate
* device TPH hardware.
+ * 5. Program steering tag (ST) table entries by VFIO_DEVICE_FEATURE_TPH_PROGRAM
+ * if the device has an architected ST table.
*/
#define VFIO_DEVICE_FEATURE_TPH 16
@@ -1607,6 +1610,73 @@ struct vfio_device_feature_tph {
__u32 flags;
};
+/*
+ * Capability bits returned by VFIO_DEVICE_FEATURE_TPH GET
+ */
+
+/*
+ * Supports programming steering tag (ST) entries using the NONE source.
+ * This source resolves to a zero ST value.
+ */
+#define VFIO_DEVICE_TPH_CAP_PROG_ST_NONE (1u << 0)
+/*
+ * Supports programming steering tag (ST) entries using a CPU source handle,
+ * for either volatile or persistent memory type.
+ */
+#define VFIO_DEVICE_TPH_CAP_PROG_ST_CPU (1u << 1)
+
+/*
+ * VFIO_DEVICE_FEATURE_TPH_PROGRAM - Batch program architected steering tag
+ * (ST) table entries. This interface only operates on the architected ST
+ * table described by the PCIe TPH ST Table Location field; it does not
+ * manage device-specific (DS) ST storage or any vendor-private ST array.
+ *
+ * SET only
+ *
+ * Prerequisites before invoking this feature:
+ * 1. Call VFIO_DEVICE_FEATURE_TPH with SET to enable vfio TPH permission;
+ * 2. Write the TPH control register to activate device hardware TPH logic.
+ *
+ * @flags: IN - source type selector (see VFIO_DEVICE_TPH_SRC_*) and
+ * steering tag namespace selector (see VFIO_DEVICE_TPH_EXTENDED) and
+ * failure policy (see VFIO_DEVICE_TPH_REQUIRE_ST)
+ * Note: only one source type is permitted; combining source type bits
+ * is rejected.
+ * @start: IN - first architected ST table index
+ * @count: IN - number of contiguous entries to program
+ * @srcs: IN - user pointer to array of source u32 handles (CPU ID for CPU
+ * sources, ignored for SRC_NONE)
+ *
+ * Return value: >=0 = number of entries successfully programmed;
+ * <0 = negative errno
+ *
+ * CPU offline note: When a CPU goes offline, the kernel automatically clears
+ * all ST table entries mapped to that CPU, setting those ST values to zero.
+ */
+#define VFIO_DEVICE_FEATURE_TPH_PROGRAM 17
+
+struct vfio_device_feature_tph_program {
+ __u32 flags;
+ __u16 start;
+ __u16 count;
+ __aligned_u64 srcs;
+};
+
+/* Use extended 16-bit steering tag namespace */
+#define VFIO_DEVICE_TPH_EXTENDED (1u << 0)
+/*
+ * Stop the batch when an entry resolves to a zero steering tag.
+ * Programming halts at that entry; the returned count
+ * is the number of entries successfully programmed before the stop.
+ */
+#define VFIO_DEVICE_TPH_REQUIRE_ST (1u << 1)
+/* Source resolves to a zero steering tag (ST) */
+#define VFIO_DEVICE_TPH_SRC_NONE (1u << 2)
+/* Source holds a volatile memory CPU ID */
+#define VFIO_DEVICE_TPH_SRC_CPU_VOLATILE (1u << 3)
+/* Source holds a persistent memory CPU ID */
+#define VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT (1u << 4)
+
/* -------- API for Type1 VFIO IOMMU -------- */
/**
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 12/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (10 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 11/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:50 ` sashiko-bot
2026-11-23 2:36 ` [PATCH v22 13/13] vfio/pci: Expose per-device TPH state via debugfs Chengwen Feng
` (2 subsequent siblings)
14 siblings, 1 reply; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Extend the VFIO TPH policy model beyond NO-ST and IV-ST, add DS-ST
policy and introduce VFIO_DEVICE_FEATURE_TPH_RESOLVE for steering tag (ST)
lookup.
- Add VFIO_PCI_TPH_POLICY_DS_ST, extend module parameter valid range
* DS-ST: expose TPH_RESOLVE for CPU source steering tag lookup;
ST programming supports NONE and CPU sources
Note: In this 'DS-ST mode', device-specific ST tables are not preserved
across PCI resets, userspace must re-establish ST state after
reset. The kernel cannot validate these ST entries, so this mode
is not recommended for general virtualization usecases
- Relax TPH control register write check to allow NS/IV/DS mode selection
- Implement TPH_RESOLVE:
* Support CPU volatile / persistent memory source steering tag resolution
* Return resolved steering tag (ST) when capability is advertised
- Advertise TPH_RESOLVE capability only when hardware reports DS-ST support
- Update module parameter description to document DS-ST caveat
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/vfio/pci/vfio_pci.c | 11 +++++-
drivers/vfio/pci/vfio_pci_config.c | 2 +-
drivers/vfio/pci/vfio_pci_core.c | 61 ++++++++++++++++++++++++++++--
drivers/vfio/pci/vfio_pci_priv.h | 1 +
include/uapi/linux/vfio.h | 37 ++++++++++++++++++
5 files changed, 105 insertions(+), 7 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index 664775bb3526..0369069e7f6a 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -64,7 +64,7 @@ static unsigned int tph_policy;
static int tph_policy_set(const char *val, const struct kernel_param *kp)
{
return param_set_uint_minmax(val, kp, VFIO_PCI_TPH_POLICY_NO_ST,
- VFIO_PCI_TPH_POLICY_IV_ST);
+ VFIO_PCI_TPH_POLICY_DS_ST);
}
static const struct kernel_param_ops tph_param_ops = {
.set = tph_policy_set,
@@ -74,7 +74,14 @@ module_param_cb(tph_policy, &tph_param_ops, &tph_policy, 0644);
MODULE_PARM_DESC(tph_policy,
"Global TPH policy level (0=default No-ST):\n"
"0 = No-ST mode: ST programming unavailable\n"
- "1 = Add IV-ST mode: ST programming supports NONE/CPU sources\n");
+ "1 = Add IV-ST mode: ST programming supports NONE/CPU sources\n"
+ "2 = Add DS-ST mode: RESOLVE returns ST for CPU sources;\n"
+ " ST programming supports NONE/CPU sources\n"
+ " Note: In this 'Add DS-ST mode', device-specific ST tables are\n"
+ " not preserved across PCI resets, userspace must re-establish\n"
+ " ST state after reset. The kernel cannot validate these ST\n"
+ " entries, so this mode is not recommended for general\n"
+ " virtualization usecases.");
static bool vfio_pci_dev_in_denylist(struct pci_dev *pdev)
{
diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
index 2bae386a8469..3b507a96edfc 100644
--- a/drivers/vfio/pci/vfio_pci_config.c
+++ b/drivers/vfio/pci/vfio_pci_config.c
@@ -1228,7 +1228,7 @@ static int vfio_tph_config_write(struct vfio_pci_core_device *vdev, int pos,
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_IV_MODE || !(cap & (1u << mode)) ||
+ if (mode > PCI_TPH_ST_DS_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. */
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 83b81d1a6122..318b4f433256 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1734,12 +1734,17 @@ static void vfio_pci_tph_dev_deinit(struct vfio_pci_core_device *vdev)
static u32 vfio_pci_tph_get_capability(struct vfio_pci_core_device *vdev)
{
bool has_arch_st;
+ u32 cap = 0;
has_arch_st = vfio_pci_tph_has_architected_st(vdev);
- if (!has_arch_st)
- return 0;
+ if (has_arch_st)
+ cap |= VFIO_DEVICE_TPH_CAP_PROG_ST_NONE |
+ VFIO_DEVICE_TPH_CAP_PROG_ST_CPU;
- return VFIO_DEVICE_TPH_CAP_PROG_ST_NONE | VFIO_DEVICE_TPH_CAP_PROG_ST_CPU;
+ if (vdev->tph_vcap & PCI_TPH_CAP_ST_DS)
+ cap |= VFIO_DEVICE_TPH_CAP_RESOLVE_CPU_ST;
+
+ return cap;
}
static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev,
@@ -1784,7 +1789,8 @@ static u32 vfio_pci_tph_get_src_mask(struct vfio_pci_core_device *vdev)
if (flags & VFIO_DEVICE_TPH_CAP_PROG_ST_NONE)
src_mask |= VFIO_DEVICE_TPH_SRC_NONE;
- if (flags & VFIO_DEVICE_TPH_CAP_PROG_ST_CPU)
+ if (flags & (VFIO_DEVICE_TPH_CAP_PROG_ST_CPU |
+ VFIO_DEVICE_TPH_CAP_RESOLVE_CPU_ST))
src_mask |= VFIO_DEVICE_TPH_SRC_CPU_VOLATILE |
VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT;
@@ -1915,6 +1921,50 @@ static int vfio_pci_core_feature_tph_program(struct vfio_pci_core_device *vdev,
return ret;
}
+static int vfio_pci_core_feature_tph_resolve(struct vfio_pci_core_device *vdev,
+ u32 flags,
+ struct vfio_device_feature_tph_resolve __user *arg,
+ size_t argsz)
+{
+ struct vfio_device_feature_tph_resolve resolve;
+ u32 user_flags, src_mask;
+ bool extended;
+ u16 tag = 0;
+ int ret;
+
+ if (!vdev->tph_opt_in || !(vdev->tph_vcap & PCI_TPH_CAP_ST_DS))
+ return -EOPNOTSUPP;
+
+ ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET,
+ sizeof(resolve));
+ if (ret != 1)
+ return ret;
+
+ if (copy_from_user(&resolve, arg, sizeof(resolve)))
+ return -EFAULT;
+
+ user_flags = resolve.flags;
+ extended = !!(user_flags & VFIO_DEVICE_TPH_EXTENDED);
+ if (extended && !pcie_ext_tph_supported(vdev->pdev))
+ return -EOPNOTSUPP;
+ user_flags &= ~VFIO_DEVICE_TPH_EXTENDED;
+
+ src_mask = vfio_pci_tph_get_src_mask(vdev) & ~VFIO_DEVICE_TPH_SRC_NONE;
+ if (user_flags & ~src_mask || !is_power_of_2(user_flags))
+ return -EINVAL;
+
+ ret = vfio_pci_tph_resolve_st_tag(vdev->pdev, user_flags, resolve.src,
+ extended, &tag);
+ if (ret != 0)
+ return ret;
+
+ resolve.valid = VFIO_DEVICE_TPH_VALID_ST;
+ resolve.rsv = 0;
+ resolve.st = tag;
+
+ return copy_to_user(arg, &resolve, sizeof(resolve)) ? -EFAULT : 0;
+}
+
int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
void __user *arg, size_t argsz)
{
@@ -1944,6 +1994,9 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
case VFIO_DEVICE_FEATURE_TPH_PROGRAM:
return vfio_pci_core_feature_tph_program(vdev, flags,
arg, argsz);
+ case VFIO_DEVICE_FEATURE_TPH_RESOLVE:
+ return vfio_pci_core_feature_tph_resolve(vdev, flags,
+ arg, argsz);
default:
return -ENOTTY;
}
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index 6b48fa38e51b..6cc8e32d1686 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -14,6 +14,7 @@
enum vfio_pci_tph_policy {
VFIO_PCI_TPH_POLICY_NO_ST = 0,
VFIO_PCI_TPH_POLICY_IV_ST,
+ VFIO_PCI_TPH_POLICY_DS_ST,
};
struct vfio_pci_ioeventfd {
diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h
index 99aac7a1849f..b184a9fcd3b0 100644
--- a/include/uapi/linux/vfio.h
+++ b/include/uapi/linux/vfio.h
@@ -1603,6 +1603,9 @@ struct vfio_device_feature_zpci_fmb_read {
* device TPH hardware.
* 5. Program steering tag (ST) table entries by VFIO_DEVICE_FEATURE_TPH_PROGRAM
* if the device has an architected ST table.
+ * 6. Query the ST value for a given CPU ID via VFIO_DEVICE_FEATURE_TPH_RESOLVE
+ * for DS-ST capable devices, userspace may then configure the ST through
+ * device MMIO or firmware interfaces.
*/
#define VFIO_DEVICE_FEATURE_TPH 16
@@ -1624,6 +1627,10 @@ struct vfio_device_feature_tph {
* for either volatile or persistent memory type.
*/
#define VFIO_DEVICE_TPH_CAP_PROG_ST_CPU (1u << 1)
+/*
+ * Supports resolving a steering tag (ST) from a CPU source handle.
+ */
+#define VFIO_DEVICE_TPH_CAP_RESOLVE_CPU_ST (1u << 2)
/*
* VFIO_DEVICE_FEATURE_TPH_PROGRAM - Batch program architected steering tag
@@ -1677,6 +1684,36 @@ struct vfio_device_feature_tph_program {
/* Source holds a persistent memory CPU ID */
#define VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT (1u << 4)
+/*
+ * VFIO_DEVICE_FEATURE_TPH_RESOLVE - Resolve TPH attributes from given source
+ *
+ * GET only
+ *
+ * Requires prior successful SET on VFIO_DEVICE_FEATURE_TPH, otherwise
+ * this feature will return error.
+ *
+ * @flags: IN - source type selector (see VFIO_DEVICE_TPH_SRC_*)
+ * and steering tag namespace selector (see VFIO_DEVICE_TPH_EXTENDED)
+ * Note: only one source type is permitted (except SRC_NONE);
+ * combining source type bits is rejected.
+ * @src: IN - source identifier, e.g. CPU ID when SRC_CPU_VOLATILE is set
+ * in flags
+ * @valid: OUT - bitmap indicating valid output fields, see
+ * VFIO_DEVICE_TPH_VALID_*
+ * @rsv: OUT - Reserved for future extension, kernel returns zero
+ * @st: OUT - TPH steering tag (valid when VALID_ST is set in valid)
+ */
+#define VFIO_DEVICE_FEATURE_TPH_RESOLVE 18
+
+struct vfio_device_feature_tph_resolve {
+ __u32 flags;
+ __u32 src;
+ __u8 valid;
+#define VFIO_DEVICE_TPH_VALID_ST (1u << 0) /* st holds valid steering tag */
+ __u8 rsv;
+ __u16 st;
+};
+
/* -------- API for Type1 VFIO IOMMU -------- */
/**
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* [PATCH v22 13/13] vfio/pci: Expose per-device TPH state via debugfs
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (11 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 12/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability Chengwen Feng
@ 2026-11-23 2:36 ` Chengwen Feng
2026-11-23 2:52 ` [PATCH v22 00/13] vfio/pci: Add PCIe TPH support fengchengwen
2026-11-23 2:58 ` sashiko-bot
14 siblings, 0 replies; 20+ messages in thread
From: Chengwen Feng @ 2026-11-23 2:36 UTC (permalink / raw)
To: alex, jgg, helgaas
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci
Add read-only debugfs file "tph" to report per-device TPH runtime state:
- effective tph_policy mode
- virtual TPH capability value
- tph_opt-in user enable state
Align debugfs comments to include tph_policy and new tph debug entry.
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
drivers/vfio/pci/vfio_pci_core.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 318b4f433256..09a209079aac 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -130,10 +130,20 @@ static int vfio_pci_core_debugfs_disable_idle_d3(struct seq_file *seq,
return 0;
}
+static int vfio_pci_core_debugfs_tph(struct seq_file *seq,
+ void *data)
+{
+ struct vfio_pci_core_device *vdev = vfio_pci_core_debugfs_private(seq);
+
+ seq_printf(seq, "policy=%u cap-virt=0x%x opt-in=%u\n",
+ vdev->tph_policy, vdev->tph_vcap, vdev->tph_opt_in);
+ return 0;
+}
+
/*
- * disable_idle_d3 and nointxmask are writable module parameters latched
- * per device at init, so a device's effective value can differ from the
- * current parameter setting. Expose the per-device (read-only) values
+ * disable_idle_d3, nointxmask and tph_policy are writable module parameters
+ * latched per device at init, so a device's effective value can differ from
+ * the current parameter setting. Expose the per-device (read-only) values
* here for visibility; read-only parameters can't drift and are omitted.
*/
static void vfio_pci_core_debugfs_init(struct vfio_pci_core_device *vdev)
@@ -149,6 +159,8 @@ static void vfio_pci_core_debugfs_init(struct vfio_pci_core_device *vdev)
vfio_pci_core_debugfs_nointxmask);
debugfs_create_devm_seqfile(dev, "disable_idle_d3", pci_dir,
vfio_pci_core_debugfs_disable_idle_d3);
+ debugfs_create_devm_seqfile(dev, "tph", pci_dir,
+ vfio_pci_core_debugfs_tph);
}
#else
static inline void vfio_pci_core_debugfs_init(struct vfio_pci_core_device *vdev)
--
2.17.1
^ permalink raw reply related [flat|nested] 20+ messages in thread
* Re: [PATCH v22 06/13] PCI/TPH: Add helpers to check standard and extended TPH support
2026-11-23 2:36 ` [PATCH v22 06/13] PCI/TPH: Add helpers to check standard and extended TPH support Chengwen Feng
@ 2026-11-23 2:47 ` sashiko-bot
0 siblings, 0 replies; 20+ messages in thread
From: sashiko-bot @ 2026-11-23 2:47 UTC (permalink / raw)
To: Chengwen Feng; +Cc: kvm, linux-pci
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] PCI/TPH: pcie_std_tph_supported() ignores pci=notph
disablement
--
commit 27f6eee7fc0406491da1b19ccf2dc89f4e4551de
Author: Chengwen Feng <fengchengwen@huawei.com>
PCI/TPH: Add helpers to check standard and extended TPH support
This commit adds pcie_std_tph_supported() and pcie_ext_tph_supported()
helper functions to query standard and extended TPH requester support via
pdev->tph_max_type.
> diff --git a/include/linux/pci-tph.h b/include/linux/pci-tph.h
> index 04c93822ee6d6..5defe407ef75b 100644
> --- a/include/linux/pci-tph.h
> +++ b/include/linux/pci-tph.h
> @@ -23,6 +23,24 @@ enum tph_mem_type {
> };
>
> #ifdef CONFIG_PCIE_TPH
> +/**
> + * pcie_std_tph_supported - check standard TPH requester support
> + * @pdev: PCI device
> + */
> +static inline bool pcie_std_tph_supported(struct pci_dev *pdev)
> +{
> + return pdev->tph_max_type >= PCI_TPH_REQ_TPH_ONLY;
> +}
> +
> +/**
> + * pcie_ext_tph_supported - check extended TPH requester support
> + * @pdev: PCI device
> + */
> +static inline bool pcie_ext_tph_supported(struct pci_dev *pdev)
> +{
> + return pdev->tph_max_type == PCI_TPH_REQ_EXT_TPH;
> +}
[Severity: Medium]
Should pcie_std_tph_supported() and pcie_ext_tph_supported() return false
when the kernel is booted with pci=notph?
pci_tph_init() sets pdev->tph_max_type during enumeration without checking
pci_tph_disabled, while pcie_enable_tph_ext() rejects enabling TPH with
-EINVAL when pci_tph_disabled is true. When pci=notph is active, these
helpers still report TPH as supported, allowing the VFIO TPH feature in
the following patches to be enabled and exposed in virtual config space
even though guest writes to PCI_TPH_CTRL will fail in
pcie_enable_tph_ext().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123023628.7728-1-fengchengwen@huawei.com?part=6
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v22 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper
2026-11-23 2:36 ` [PATCH v22 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
@ 2026-11-23 2:48 ` sashiko-bot
0 siblings, 0 replies; 20+ messages in thread
From: sashiko-bot @ 2026-11-23 2:48 UTC (permalink / raw)
To: Chengwen Feng; +Cc: kvm, linux-pci
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] PCI/TPH: pcie_tph_set_st_entries() re-enables TPH on error
--
commit 3ea3b71b1fd03bc517b1fa817329d811df87ed27
Author: Chengwen Feng <fengchengwen@huawei.com>
PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper
This commit adds pcie_tph_set_st_entries() to program a contiguous range
of Steering Tag table entries in a batch, and reimplements
pcie_tph_set_st_entry() as a wrapper around it.
> diff --git a/drivers/pci/tph.c b/drivers/pci/tph.c
> index 51a065d83104b..be44b27bd1ce4 100644
> --- a/drivers/pci/tph.c
> +++ b/drivers/pci/tph.c
> @@ -311,21 +311,19 @@ int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
[ ... ]
> @@ -344,29 +345,47 @@ int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
> set_ctrl_reg_req_en(pdev, PCI_TPH_REQ_DISABLE);
>
> loc = pcie_tph_get_st_table_loc(pdev);
> -
> - switch (loc) {
> - case PCI_TPH_LOC_MSIX:
> - err = pci_msix_write_tph_tag(pdev, index, tag);
> - break;
> - case PCI_TPH_LOC_CAP:
> - err = write_tag_to_st_table(pdev, index, tag);
> - break;
> - default:
> - err = -EINVAL;
> - }
> -
> - if (err) {
> - pcie_disable_tph(pdev);
> - return err;
> + for (i = 0; i < count; i++) {
> + switch (loc) {
> + case PCI_TPH_LOC_MSIX:
> + err = pci_msix_write_tph_tag(pdev, start + i, tags[i]);
> + break;
> + case PCI_TPH_LOC_CAP:
> + err = write_tag_to_st_table(pdev, start + i, tags[i]);
> + break;
> + default:
> + err = -EINVAL;
> + }
> + if (err != 0) {
> + pci_err(pdev, "failed to set steering tag: %s table, index=%u, tags=%u, ret=%d\n",
> + (loc == PCI_TPH_LOC_MSIX) ? "MSI-X" : "ST",
> + start + i, tags[i], err);
> + set_ctrl_reg_req_en(pdev, pdev->tph_req_type);
> + return i > 0 ? i : (err > 0 ? -EINVAL : err);
> + }
> }
[Severity: Medium]
Should the error path here call pcie_disable_tph(pdev) instead of
re-enabling TPH with set_ctrl_reg_req_en()?
Before this change, pcie_tph_set_st_entry() called pcie_disable_tph(pdev)
on failure, clearing PCI_TPH_CTRL and pdev->tph_enabled.
pci_msix_write_tph_tag() relies on the caller to disable TPH when it
returns -EIO on an offline PCIe channel:
drivers/pci/msi/msi.c:pci_msix_write_tph_tag() {
...
/*
* The tag update below is a write to the MSI-X Table followed by a
* flush read, neither of which can be completed while the Link is
* down. Check as late as possible, as the Link can go down at any
* point. Let the caller disable TPH.
*/
if (pci_channel_offline(pdev))
return -EIO;
...
}
Re-enabling TPH on failure leaves hardware TPH active with an unprogrammed
or partially programmed ST table, and callers such as bnxt_request_irq()
that loop over vectors without checking pcie_tph_set_st_entry()'s return
value will keep issuing tag writes instead of short-circuiting on
!pdev->tph_enabled.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123023628.7728-1-fengchengwen@huawei.com?part=7
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v22 12/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability
2026-11-23 2:36 ` [PATCH v22 12/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability Chengwen Feng
@ 2026-11-23 2:50 ` sashiko-bot
0 siblings, 0 replies; 20+ messages in thread
From: sashiko-bot @ 2026-11-23 2:50 UTC (permalink / raw)
To: Chengwen Feng; +Cc: kvm, linux-pci
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] vfio/pci: out-of-bounds read in cpu_online() via unchecked CPU
ID
- [Medium] vfio/pci: vfio_tph_config_write() loses ST mode when TPH is
disabled
--
commit d6106eda03cc2bd8d1bcbc6a878dc5163e37fa33
Author: Chengwen Feng <fengchengwen@huawei.com>
vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability
This commit adds the VFIO_PCI_TPH_POLICY_DS_ST policy level to allow
PCI_TPH_ST_DS_MODE selection and introduces the
VFIO_DEVICE_FEATURE_TPH_RESOLVE ioctl feature to resolve steering tags from
CPU sources.
> diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
> index 2bae386a8469f..3b507a96edfc0 100644
> --- a/drivers/vfio/pci/vfio_pci_config.c
> +++ b/drivers/vfio/pci/vfio_pci_config.c
> @@ -1228,7 +1228,7 @@ static int vfio_tph_config_write(struct vfio_pci_core_device *vdev, int pos,
> 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_IV_MODE || !(cap & (1u << mode)) ||
> + if (mode > PCI_TPH_ST_DS_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. */
[Severity: Medium]
Can vfio_tph_config_write() lose the ST Mode Select field when TPH
Requester Enable is PCI_TPH_REQ_DISABLE?
When req == PCI_TPH_REQ_DISABLE, vfio_tph_config_write() calls
pcie_disable_tph(), which either returns without writing to hardware when
!pdev->tph_enabled or writes 0 to the entire 32-bit hardware PCI_TPH_CTRL
register when pdev->tph_enabled is set. Because vfio_tph_config_read()
reads PCI_TPH_CTRL directly from hardware and vfio_tph_config_write()
overwrites vdev->vconfig[start + PCI_TPH_CTRL] with the hardware register
on entry:
drivers/vfio/pci/vfio_pci_config.c:vfio_tph_config_write() {
...
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);
...
if (req == PCI_TPH_REQ_TPH_ONLY || req == PCI_TPH_REQ_EXT_TPH) {
pcie_disable_tph(pdev);
pcie_enable_tph_ext(pdev, mode, req);
} else if (req == PCI_TPH_REQ_DISABLE) {
pcie_disable_tph(pdev);
}
...
}
any ST mode written while the requester is disabled (or preserved when
disabling the requester to update ST entries) is lost on the next config
read or write, reverting the device to PCI_TPH_ST_NS_MODE when the
requester is later enabled.
> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index 83b81d1a61227..318b4f4332565 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
[ ... ]
> @@ -1915,6 +1921,50 @@ static int vfio_pci_core_feature_tph_program(struct vfio_pci_core_device *vdev,
> return ret;
> }
>
> +static int vfio_pci_core_feature_tph_resolve(struct vfio_pci_core_device *vdev,
> + u32 flags,
> + struct vfio_device_feature_tph_resolve __user *arg,
> + size_t argsz)
> +{
> + struct vfio_device_feature_tph_resolve resolve;
> + u32 user_flags, src_mask;
> + bool extended;
> + u16 tag = 0;
> + int ret;
> +
> + if (!vdev->tph_opt_in || !(vdev->tph_vcap & PCI_TPH_CAP_ST_DS))
> + return -EOPNOTSUPP;
> +
> + ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET,
> + sizeof(resolve));
> + if (ret != 1)
> + return ret;
> +
> + if (copy_from_user(&resolve, arg, sizeof(resolve)))
> + return -EFAULT;
> +
> + user_flags = resolve.flags;
> + extended = !!(user_flags & VFIO_DEVICE_TPH_EXTENDED);
> + if (extended && !pcie_ext_tph_supported(vdev->pdev))
> + return -EOPNOTSUPP;
> + user_flags &= ~VFIO_DEVICE_TPH_EXTENDED;
> +
> + src_mask = vfio_pci_tph_get_src_mask(vdev) & ~VFIO_DEVICE_TPH_SRC_NONE;
> + if (user_flags & ~src_mask || !is_power_of_2(user_flags))
> + return -EINVAL;
> +
> + ret = vfio_pci_tph_resolve_st_tag(vdev->pdev, user_flags, resolve.src,
> + extended, &tag);
[Severity: High]
Can a user-supplied resolve.src >= nr_cpu_ids cause an out-of-bounds read
in cpu_online()?
vfio_pci_core_feature_tph_resolve() passes the unvalidated 32-bit
resolve.src value from userspace to vfio_pci_tph_resolve_st_tag(), which
calls cpu_online(src_handle) without first checking src_handle < nr_cpu_ids:
drivers/vfio/pci/vfio_pci_core.c:vfio_pci_tph_resolve_st_tag() {
...
if ((src_bits & cpu_srcs) && !cpu_online(src_handle)) {
*st = 0;
return -EINVAL;
}
...
}
Because cpumask_test_cpu() does not clamp out-of-range CPU numbers,
passing a large resolve.src indexes past __cpu_online_mask in test_bit()
and can trigger an out-of-bounds read or kernel page fault. Checking
src_handle >= nr_cpu_ids before cpu_online(src_handle) in
vfio_pci_tph_resolve_st_tag() would also protect
vfio_pci_core_feature_tph_program().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123023628.7728-1-fengchengwen@huawei.com?part=12
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v22 00/13] vfio/pci: Add PCIe TPH support
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (12 preceding siblings ...)
2026-11-23 2:36 ` [PATCH v22 13/13] vfio/pci: Expose per-device TPH state via debugfs Chengwen Feng
@ 2026-11-23 2:52 ` fengchengwen
2026-11-23 2:58 ` sashiko-bot
14 siblings, 0 replies; 20+ messages in thread
From: fengchengwen @ 2026-11-23 2:52 UTC (permalink / raw)
To: alex
Cc: wathsala.vithanage, wei.huang2, zhipingz, wangzhou1, wangyushan12,
liuyonglong, kvm, linux-pci, jgg, helgaas
Hi Alex,
This v22 adapts the VFIO feature enum numbers to match the latest linux-next
and addresses Sashiko review comments on v21.
I previously invited Wathsala (ARM) and Wei Huang (AMD) to review the series,
particularly the TPH uAPI ownership and CPU offline lifecycle model. They are
copied here for this v22 update.
I'd appreciate your review when you have bandwidth.
Thanks,
Chengwen
On 11/23/2026 10:36 AM, Chengwen Feng wrote:
> This patchset enables userspace-configurable PCIe TPH support for VFIO in
> the **device-to-host (D2H)** scenario, bringing performance benefits for
> userspace workloads such as DPDK and SPDK.
>
> The patchset is split into two logical parts:
> 1. The first eight patches fix and refactor core PCI/TPH kernel code to
> export the required helper interfaces.
> 2. The second five patches implement the corresponding VFIO TPH
> virtualization and uAPI step by step.
>
>
> Since TLP TPH ST values are not validated by the IOMMU like TLP address
> fields, there exists risk of cross-tenant cache interference if untrusted
> VM can program arbitrary ST tags. We introduce two-level security
> gating:
>
> The first is the VFIO-PCI module parameter tph_policy, defined as:
> Global TPH policy level (0 = default No-ST):
> 0 = No-ST mode: ST programming unavailable
> 1 = Add IV-ST mode: ST programming supports NONE / CPU sources
> 2 = Add DS-ST mode: RESOLVE returns ST for CPU sources;
> ST programming supports NONE / CPU sources
> Note: In this 'Add DS-ST mode', device-specific ST tables are not
> preserved across PCI resets, userspace must re-establish ST
> state after reset. The kernel cannot validate these ST
> entries, so this mode is not recommended for general
> virtualization usecases.
>
> Note: IV-ST mode is considered safe for VM use because the ST table is
> programmed exclusively via the kernel VFIO uAPI. VM cannot directly
> write the architected ST table in MSI-X or config space. The hypervisor
> (e.g. QEMU) will verify ST, and also when a target CPU transitions from
> online to offline, the kernel automatically (add in this patchset) clears
> all ST table entries mapped to that CPU, setting those ST values to zero.
>
> The second gate is per-device opt-in. TPH programming is only permitted
> after userspace successfully opt-in via the VFIO TPH feature.
>
>
> Three VFIO device features are introduced in this series:
> 1. VFIO_DEVICE_FEATURE_TPH: control and query VFIO TPH feature
> capabilities.
> 2. VFIO_DEVICE_FEATURE_TPH_PROGRAM: batch program architected steering tag
> table entries.
> 3. VFIO_DEVICE_FEATURE_TPH_RESOLVE: resolve TPH attributes for a given CPU
> source.
>
> The intended userspace programming model is:
> 1. Read the virtualized PCI TPH capability via VFIO config space access to
> discover underlying hardware TPH support.
> 2. Invoke VFIO_DEVICE_FEATURE_TPH SET to opt-in and obtain TPH permission.
> 3. Invoke VFIO_DEVICE_FEATURE_TPH GET to retrieve available TPH
> capabilities; GET returns an error if opt-in has not been performed.
> 4. Write the TPH control register through VFIO config space to activate
> hardware TPH.
> 5. Program architected ST table entries via
> VFIO_DEVICE_FEATURE_TPH_PROGRAM if the device exposes architected ST
> table.
> 6. Query the ST value for a given CPU ID via
> VFIO_DEVICE_FEATURE_TPH_RESOLVE for DS-ST capable devices, userspace
> may then configure the ST through device MMIO or firmware interfaces.
>
>
> Key implementation details for capability virtualization:
> 1. The TPH capability register is virtualized according to the active
> tph_policy:
> a. No-ST policy: IV/DS mode bits, ST location and ST size fields are
> masked. Only NO-ST capability is visible to userspace.
> b. IV-ST policy: DS mode bits are masked. Userspace sees NO-ST and
> IV-ST capabilities (if hardware supports IV-ST).
> c. DS-ST policy: capability register is passed through unmodified.
> 2. Capability bits returned by VFIO_DEVICE_FEATURE_TPH GET follow these
> rules:
> a. If virtualized TPH exposes architected ST table:
> VFIO_DEVICE_TPH_CAP_PROG_ST_NONE and VFIO_DEVICE_TPH_CAP_PROG_ST_CPU
> are reported. These indicate userspace may use TPH_PROGRAM to update
> ST.
> b. If virtualized TPH supports DS mode:
> VFIO_DEVICE_TPH_CAP_RESOLVE_CPU_ST is reported. This indicates
> userspace can resolve CPU-ID associated ST values via TPH_RESOLVE.
>
>
> Future P2P / DMABUF extension:
> The uAPI is designed to easy extend to support device-to-device (P2P) and
> DMABUF-backed scenarios on top of Zhiping's P2P TPH patchset:
> 1. Return VFIO_DEVICE_TPH_CAP_RESOLVE_DMABUF_PH in all tph_policy in
> VFIO_DEVICE_FEATURE_TPH GET, so that userspace could resolve DMABUF's
> TPH PH info
> 2. Return VFIO_DEVICE_TPH_CAP_PROG_ST_DMABUF if virtualized TPH has the
> architect ST, so that userspace could program ST entries which
> associated to DMABUFs' fd
> 3. Return VFIO_DEVICE_TPH_CAP_RESOLVE_DMABUF_ST if virtualized TPH
> supports DS mode, so that userspace could retrieve ST value which
> associated to DMABUF's fd
>
> ---
> v22:
> - Rebase latest linux-next to resolve uAPI conflict
> - Reorder patch sequence: move core tph disable fixes ahead of vfio patches
> - Fix Sashiko comments:
> - Fix TPH config write path: disable existing TPH before re-enable to
> keep state coherent
> - Fix cpuhp race/UAF: reorder TPH teardown before cpuhp removal
> - Add cpu_online() check in ST resolve routine
> - Gate IV/DS capability exposure on TPH _DSM presence; hide IV/DS when
> DSM unavailable
> - Fix kernel-doc and redundant check in pcie_tph_dsm_supported() helper
> v21:
> - Focus only device-to-host (D2H) scenario
> - Implement steering-tag to cpuid map to tracking cpu offline
> - Refactor the implementation
> v20:
> - Address almost all comments from Alex about v19, not within Alex's
> scope:
> - Drop LITERAL policy support
> - uAPI changes: cap have been split into fine-grained
> - TPH config access: still based on perm mechanism, but restrict the
> readable and writable length
> - Debugfs add more fields
> v19:
> - Address all comments from Alex
> - Drop CPU-to-ST translation of PCI/TPH
> - Re-implement vfio part, please refer:
> https://lore.kernel.org/linux-pci/20260626092232.53ed3a7c@shazbot.org/
> - Add expose tph_policy via debugfs
>
> Chengwen Feng (13):
> PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction
> PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing
> PCI/TPH: Cache negotiated max requester type at probe time
> PCI/TPH: Refactor pcie_enable_tph and add requester variant
> PCI/TPH: Refactor pcie_tph_get_cpu_st & add requester variant
> PCI/TPH: Add helpers to check standard and extended TPH support
> PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming
> helper
> PCI/TPH: Add pcie_tph_dsm_supported() helper to detect device TPH ST
> _DSM
> vfio/pci: Reset PCIe TPH state on device open and close
> vfio/pci: Add basic NO-ST TPH VFIO device feature support
> vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming
> support
> vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability
> vfio/pci: Expose per-device TPH state via debugfs
>
> drivers/pci/tph.c | 240 ++++++++++++-----
> drivers/vfio/pci/vfio_pci.c | 24 ++
> drivers/vfio/pci/vfio_pci_config.c | 188 +++++++++++--
> drivers/vfio/pci/vfio_pci_core.c | 412 ++++++++++++++++++++++++++++-
> drivers/vfio/pci/vfio_pci_priv.h | 6 +
> include/linux/pci-tph.h | 55 +++-
> include/linux/pci.h | 6 +-
> include/linux/vfio_pci_core.h | 7 +
> include/uapi/linux/vfio.h | 129 +++++++++
> 9 files changed, 967 insertions(+), 100 deletions(-)
>
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v22 11/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support
2026-11-23 2:36 ` [PATCH v22 11/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support Chengwen Feng
@ 2026-11-23 2:58 ` sashiko-bot
0 siblings, 0 replies; 20+ messages in thread
From: sashiko-bot @ 2026-11-23 2:58 UTC (permalink / raw)
To: Chengwen Feng; +Cc: kvm, linux-pci
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] vfio/pci: OOB read in vfio_pci_tph_resolve_st_tag() on
cpu_online()
- [Medium] vfio/pci: ST table desync across TPH enable/mode and MSI-X
transitions
- [Medium] vfio/pci: ST table and config accesses while device is
suspended or in D3
- [Low] vfio/pci: unconditional memory_lock and per-entry ST writes on
CPU offline
--
commit e8463ab09aa5bab8dbaf6df575e6c544d6e783aa
Author: Chengwen Feng <fengchengwen@huawei.com>
vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support
This commit adds the VFIO_PCI_TPH_POLICY_IV_ST policy, introduces the
VFIO_DEVICE_FEATURE_TPH_PROGRAM ioctl to batch program architected
steering tag entries, and registers a CPU hotplug offline callback to
clear entries bound to offlined CPUs.
> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index be7d1921f5f31..83b81d1a61227 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
[ ... ]
> @@ -1603,12 +1613,135 @@ static int vfio_pci_core_feature_token(struct vfio_pci_core_device *vdev,
> return 0;
> }
>
> -static u32 vfio_pci_tph_get_capability(struct vfio_pci_core_device *vdev)
> +static enum cpuhp_state vfio_tph_cpuhp_state;
> +
> +static int vfio_pci_tph_cpuhp_offline(unsigned int cpu, struct hlist_node *node)
> {
> - /* NO-ST only, no capability bits exposed now */
> + struct vfio_pci_core_device *vdev =
> + hlist_entry(node, struct vfio_pci_core_device, tph_cpuhp_node);
> + u16 cmd;
> + int i;
> +
> + if (vdev->tph_st_cpu == NULL)
> + return 0;
> +
> + mutex_lock(&vdev->tph_lock);
> + cmd = vfio_pci_memory_lock_and_enable(vdev);
> + for (i = 0; i < vdev->tph_st_size; i++) {
> + if (vdev->tph_st_cpu[i] != (s64)cpu)
> + continue;
> + pcie_tph_set_st_entry(vdev->pdev, i, 0);
> + vdev->tph_st_cpu[i] = (s64)-1;
> + }
> + vfio_pci_memory_unlock_and_restore(vdev, cmd);
> + mutex_unlock(&vdev->tph_lock);
> +
> return 0;
> }
[Severity: Medium]
Can vfio_pci_tph_cpuhp_offline() and vfio_pci_core_feature_tph_program()
access config space and the MSI-X table BAR while the device is runtime
suspended or in PCI_D3hot?
Unlike VFIO file operations, vfio_pci_tph_cpuhp_offline() runs from a CPU
hotplug thread without calling pm_runtime_resume_and_get(). In both this
callback and vfio_pci_core_feature_tph_program(),
vfio_pci_memory_lock_and_enable() sets PCI_COMMAND_MEMORY without checking
or transitioning pdev->current_state out of guest-initiated PCI_D3hot, so
pcie_tph_set_st_entries() -> pci_msix_write_tph_tag() can issue MMIO
accesses to the MSI-X BAR while the device is in D3hot or D3cold.
Also, on devices with vdev->needs_pm_restore set,
vfio_pci_set_power_state() saves config space into vdev->pm_save on D3hot
entry and restores it via pci_restore_state() -> pci_restore_tph_state()
on return to PCI_D0, which overwrites any ST entries cleared on CPU
offline while in D3hot.
[Severity: Low]
Does vfio_pci_tph_cpuhp_offline() need to acquire vdev->memory_lock and
read/write PCI_COMMAND when no ST entry is bound to the offlined CPU?
Because vfio_pci_tph_dev_init() registers every open device with an
architected ST table, every CPU offline event takes vdev->memory_lock in
write mode and touches PCI_COMMAND even when no entries match cpu, and
calling pcie_tph_set_st_entry() per entry repeatedly toggles PCI_TPH_CTRL
and re-reads PCI_TPH_CAP for each matching entry.
[ ... ]
> @@ -1644,6 +1777,144 @@ static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev,
> return copy_to_user(arg, &tph, sizeof(tph)) ? -EFAULT : 0;
> }
>
> +static u32 vfio_pci_tph_get_src_mask(struct vfio_pci_core_device *vdev)
> +{
> + u32 flags = vfio_pci_tph_get_capability(vdev);
> + u32 src_mask = 0;
> +
> + if (flags & VFIO_DEVICE_TPH_CAP_PROG_ST_NONE)
> + src_mask |= VFIO_DEVICE_TPH_SRC_NONE;
> + if (flags & VFIO_DEVICE_TPH_CAP_PROG_ST_CPU)
> + src_mask |= VFIO_DEVICE_TPH_SRC_CPU_VOLATILE |
> + VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT;
> +
> + return src_mask;
> +}
> +
> +static int vfio_pci_tph_resolve_st_tag(struct pci_dev *pdev, u32 src_bits,
> + u32 src_handle, bool extended, u16 *st)
> +{
> + u8 req_type = extended ? PCI_TPH_REQ_EXT_TPH : PCI_TPH_REQ_TPH_ONLY;
> + u32 cpu_srcs = VFIO_DEVICE_TPH_SRC_CPU_VOLATILE |
> + VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT;
> + int ret = 0;
> +
> + if ((src_bits & cpu_srcs) && !cpu_online(src_handle)) {
> + *st = 0;
> + return -EINVAL;
> + }
[Severity: High]
Can a user-supplied src_handle >= nr_cpu_ids trigger an out-of-bounds read
in cpu_online(src_handle)?
vfio_pci_core_feature_tph_program() copies arbitrary u32 values from
program.srcs into src_handle[] and passes them here without bounds
checking. On SMP kernels, cpu_online() calls cpumask_test_cpu(), which
expects cpu < nr_cpu_ids and performs an unchecked test_bit() read relative
to __cpu_online_mask. Checking src_handle >= nr_cpu_ids before calling
cpu_online() avoids the out-of-bounds access.
> +
> + if (src_bits & VFIO_DEVICE_TPH_SRC_NONE)
> + *st = 0;
> + else if (src_bits & VFIO_DEVICE_TPH_SRC_CPU_VOLATILE)
> + ret = pcie_tph_get_cpu_st_ext(pdev, TPH_MEM_TYPE_VM,
> + req_type, src_handle, st);
> + else if (src_bits & VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT)
> + ret = pcie_tph_get_cpu_st_ext(pdev, TPH_MEM_TYPE_PM,
> + req_type, src_handle, st);
> + else
> + ret = -EINVAL;
> +
> + if (ret != 0)
> + *st = 0;
> +
> + return ret;
> +}
[ ... ]
> + if (i > 0) {
> + mutex_lock(&vdev->tph_lock);
> +
> + cmd = vfio_pci_memory_lock_and_enable(vdev);
> + ret = written = pcie_tph_set_st_entries(pdev, program.start, i,
> + tags);
> + vfio_pci_memory_unlock_and_restore(vdev, cmd);
> +
> + if (written > 0) {
> + for (k = 0; k < written; k++)
> + vdev->tph_st_cpu[program.start + k] =
> + (tags[k] == 0) ?
> + (s64)-1 : (s64)src_handle[k];
> + }
> +
> + mutex_unlock(&vdev->tph_lock);
> + }
[Severity: Medium]
Can vdev->tph_st_cpu[] become desynchronized from the hardware ST table
across PCI_TPH_CTRL mode/enable and MSI-X transitions?
In drivers/pci/tph.c, pcie_tph_set_st_entries() returns -EINVAL when
!pdev->tph_enabled and returns count without writing to hardware when
pdev->tph_mode == PCI_TPH_ST_NS_MODE (and pci_msix_write_tph_tag() returns
-ENXIO when !pdev->msix_enabled):
drivers/pci/tph.c:pcie_tph_set_st_entries() {
...
if (!pdev->tph_enabled)
return -EINVAL;
if (!count)
return -EINVAL;
/* No need to write tag if device is in "No ST Mode" */
if (pdev->tph_mode == PCI_TPH_ST_NS_MODE)
return count;
...
}
If userspace invokes VFIO_DEVICE_FEATURE_TPH_PROGRAM while TPH is enabled
in PCI_TPH_ST_NS_MODE, pcie_tph_set_st_entries() skips the hardware writes
and returns i, so vdev->tph_st_cpu[] and the return value report that the
entries were programmed even though the hardware ST table was not updated.
Conversely, if a bound CPU goes offline while userspace has temporarily
disabled TPH, switched to PCI_TPH_ST_NS_MODE, or disabled MSI-X,
pcie_tph_set_st_entry(vdev->pdev, i, 0) in vfio_pci_tph_cpuhp_offline()
does not clear the hardware entry, yet vdev->tph_st_cpu[i] is reset to -1,
leaving the offlined CPU's stale Steering Tag in hardware when IV-ST mode
or MSI-X is later re-enabled.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123023628.7728-1-fengchengwen@huawei.com?part=11
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v22 00/13] vfio/pci: Add PCIe TPH support
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
` (13 preceding siblings ...)
2026-11-23 2:52 ` [PATCH v22 00/13] vfio/pci: Add PCIe TPH support fengchengwen
@ 2026-11-23 2:58 ` sashiko-bot
14 siblings, 0 replies; 20+ messages in thread
From: sashiko-bot @ 2026-11-23 2:58 UTC (permalink / raw)
To: Chengwen Feng; +Cc: linux-pci
Sashiko has finished reviewing this 13-patch series:
- Patches 1-5, 8-10, and 13: no issues found.
- Patch 6 ("PCI/TPH: Add helpers to check standard and extended..."): 1
potential issue (1 Medium)
- Patch 7 ("PCI/TPH: Add pcie_tph_set_st_entries() batch ST entr..."): 1
potential issue (1 Medium)
- Patch 11 ("vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch..."):
4 potential issues (1 High, 2 Medium, 1 Low)
- Patch 12 ("vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capab..."):
2 potential issues (1 High, 1 Medium)
Detailed reviews have been sent as separate replies to patches 6, 7, 11,
and 12.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123023628.7728-1-fengchengwen@huawei.com
^ permalink raw reply [flat|nested] 20+ messages in thread
end of thread, other threads:[~2026-11-23 2:58 UTC | newest]
Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 03/13] PCI/TPH: Cache negotiated max requester type at probe time Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 06/13] PCI/TPH: Add helpers to check standard and extended TPH support Chengwen Feng
2026-11-23 2:47 ` sashiko-bot
2026-11-23 2:36 ` [PATCH v22 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
2026-11-23 2:48 ` sashiko-bot
2026-11-23 2:36 ` [PATCH v22 08/13] PCI/TPH: Add pcie_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 09/13] vfio/pci: Reset PCIe TPH state on device open and close Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 10/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support Chengwen Feng
2026-11-23 2:36 ` [PATCH v22 11/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support Chengwen Feng
2026-11-23 2:58 ` sashiko-bot
2026-11-23 2:36 ` [PATCH v22 12/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability Chengwen Feng
2026-11-23 2:50 ` sashiko-bot
2026-11-23 2:36 ` [PATCH v22 13/13] vfio/pci: Expose per-device TPH state via debugfs Chengwen Feng
2026-11-23 2:52 ` [PATCH v22 00/13] vfio/pci: Add PCIe TPH support fengchengwen
2026-11-23 2:58 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox