vfio/pci: Add PCIe TPH support
Subsystems (3)
v22 cover vfio/pci: Add PCIe TPH support
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/[email protected]/
- 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(-)
Signature · 1 line
2.17.1
PATCH v22 1/13 PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction
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.
--- drivers/pci/tph.c | 12 +++++------- include/linux/pci-tph.h | 6 ++++++ 2 files changed, 11 insertions(+), 7 deletions(-)
drivers/pci/tph.c +5 -7
@@ -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 :
include/linux/pci-tph.h +6 -0
@@ -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 */
Signature · 1 line
2.17.1
PATCH v22 2/13 PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing
In reply to Chengwen FengSplit 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.
--- include/linux/pci.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/pci.h +2 -2
@@ -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
Signature · 1 line
2.17.1
PATCH v22 3/13 PCI/TPH: Cache negotiated max requester type at probe time
In reply to Chengwen Fengpcie_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.
--- drivers/pci/tph.c | 45 ++++++++++++++++++++++++--------------------- include/linux/pci.h | 4 +++- 2 files changed, 27 insertions(+), 22 deletions(-)
drivers/pci/tph.c +24 -21
@@ -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);
include/linux/pci.h +3 -1
@@ -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 */
Signature · 1 line
2.17.1
PATCH v22 4/13 PCI/TPH: Refactor pcie_enable_tph and add requester variant
In reply to Chengwen FengRefactor 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.
--- drivers/pci/tph.c | 41 ++++++++++++++++++++++++++++++----------- include/linux/pci-tph.h | 8 ++++++-- 2 files changed, 36 insertions(+), 13 deletions(-)
drivers/pci/tph.c +30 -11
@@ -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)
include/linux/pci-tph.h +6 -2
@@ -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 ; }
Signature · 1 line
2.17.1
PATCH v22 5/13 PCI/TPH: Refactor pcie_tph_get_cpu_st & add requester variant
In reply to Chengwen FengRefactor 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.
--- drivers/pci/tph.c | 45 ++++++++++++++++++++++++++++++++--------- include/linux/pci-tph.h | 10 +++++++-- 2 files changed, 44 insertions(+), 11 deletions(-)
drivers/pci/tph.c +36 -9
@@ -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);/**
include/linux/pci-tph.h +8 -2
@@ -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)
Signature · 1 line
2.17.1
PATCH v22 6/13 PCI/TPH: Add helpers to check standard and extended TPH support
In reply to Chengwen FengAdd 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.
--- include/linux/pci-tph.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
include/linux/pci-tph.h +22 -0
@@ -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 ; }
Signature · 1 line
2.17.1
[email protected]
AI reviewRe: [PATCH v22 06/13] PCI/TPH: Add helpers to check standard and extended TPH support
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 --
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 <[email protected]>
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.
Quoted 15 lines
include/linux/pci-tph.h +7
-0@@ -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 ;
include/linux/pci-tph.h enum tph_mem_type { · line 42
Quoted 10 lines
include/linux/pci-tph.h +10
-0+} + +/** +* 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().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v22 7/13 PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper
In reply to Chengwen FengAdd 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
--- drivers/pci/tph.c | 77 +++++++++++++++++++++++++---------------- include/linux/pci-tph.h | 6 ++++ 2 files changed, 54 insertions(+), 29 deletions(-)
drivers/pci/tph.c +48 -29
@@ -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);
include/linux/pci-tph.h +6 -0
@@ -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,
Signature · 1 line
2.17.1
[email protected]
AI reviewRe: [PATCH v22 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper
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 --
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 <[email protected]>
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.
Quoted 5 lines
drivers/pci/tph.c +0
-0@@ -311,21 +311,19 @@ int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type,
[ ... ]
Quoted 28 lines
drivers/pci/tph.c +9
-15@@ -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 :
drivers/pci/tph.c int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag) · line 366
Quoted 10 lines
drivers/pci/tph.c +9
-0+ 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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v22 8/13 PCI/TPH: Add pcie_tph_dsm_supported() helper to detect device TPH ST _DSM
In reply to Chengwen FengAdd 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.
--- drivers/pci/tph.c | 28 ++++++++++++++++++++++++++++ include/linux/pci-tph.h | 3 +++ 2 files changed, 31 insertions(+)
drivers/pci/tph.c +28 -0
@@ -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);
include/linux/pci-tph.h +3 -0
@@ -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 */
Signature · 1 line
2.17.1
PATCH v22 9/13 vfio/pci: Reset PCIe TPH state on device open and close
In reply to Chengwen FengHardware 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.
--- drivers/vfio/pci/vfio_pci_core.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +7 -0
@@ -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 );
Signature · 1 line
2.17.1
PATCH v22 10/13 vfio/pci: Add basic NO-ST TPH VFIO device feature support
In reply to Chengwen FengImplement 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.
--- 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(-)
drivers/vfio/pci/vfio_pci.c +16 -0
@@ -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); }
drivers/vfio/pci/vfio_pci_config.c +153 -17
208 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_core.c +45 -0
63 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_priv.h +4 -0
@@ -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;
include/linux/vfio_pci_core.h +3 -0
@@ -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 {
include/uapi/linux/vfio.h +22 -0
@@ -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 -------- */ /**
Signature · 1 line
2.17.1
PATCH v22 11/13 vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support
In reply to Chengwen FengExtend 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
--- 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(-)
drivers/vfio/pci/vfio_pci.c +3 -2
@@ -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) {
drivers/vfio/pci/vfio_pci_config.c +19 -1
@@ -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. */
drivers/vfio/pci/vfio_pci_core.c +291 -4
370 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_priv.h +1 -0
@@ -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 {
include/linux/vfio_pci_core.h +4 -0
@@ -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 {
include/uapi/linux/vfio.h +72 -2
92 lines not on this page · raw message
Signature · 1 line
2.17.1
[email protected]
AI reviewRe: [PATCH v22 11/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support
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…
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 <[email protected]>
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.
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0
[ ... ]
Quoted 21 lines
drivers/vfio/pci/vfio_pci_core.c +14
-2@@ -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++) {
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_feature_token(struct vfio_pci_core_device *vdev, · line 1640
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+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.
[ ... ]
Quoted 20 lines
drivers/vfio/pci/vfio_pci_core.c +16
-0@@ -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)
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev, · line 1805
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+{ +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.
Quoted 17 lines
drivers/vfio/pci/vfio_pci_core.c +17
-0+ +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; +}
[ ... ]
Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0+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);
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev, · line 1908
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ +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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
PATCH v22 12/13 vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability
In reply to Chengwen FengExtend 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
--- 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(-)
drivers/vfio/pci/vfio_pci.c +9 -2
@@ -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) {
drivers/vfio/pci/vfio_pci_config.c +1 -1
@@ -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. */
drivers/vfio/pci/vfio_pci_core.c +57 -4
88 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_priv.h +1 -0
@@ -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 {
include/uapi/linux/vfio.h +37 -0
@@ -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 -------- */ /**
Signature · 1 line
2.17.1
[email protected]
AI reviewRe: [PATCH v22 12/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability
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…
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 <[email protected]>
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.
drivers/vfio/pci/vfio_pci_config.c static int vfio_tph_config_write(struct vfio_pci_core_device *vdev, int pos, · line 1234
Quoted 13 lines
drivers/vfio/pci/vfio_pci_config.c +1
-1@@ -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.
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0
[ ... ]
Quoted 28 lines
drivers/vfio/pci/vfio_pci_core.c +24
-0@@ -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 );
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_feature_tph_program(struct vfio_pci_core_device *vdev, · line 1957
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+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().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
PATCH v22 13/13 vfio/pci: Expose per-device TPH state via debugfs
In reply to Chengwen FengAdd 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.
--- drivers/vfio/pci/vfio_pci_core.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +15 -3
@@ -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)
Signature · 1 line
2.17.1
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
Quoted 155 lines · Chengwen Feng
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. InvokeVFIO_DEVICE_FEATURE_TPHSET to opt-in and obtain TPH permission.
3. InvokeVFIO_DEVICE_FEATURE_TPHGET 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 viaVFIO_DEVICE_FEATURE_TPH_PROGRAMif the device exposes architected ST table.
6. Query the ST value for a given CPU ID viaVFIO_DEVICE_FEATURE_TPH_RESOLVEfor 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 activetph_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 byVFIO_DEVICE_FEATURE_TPHGET follow these rules:
a. If virtualized TPH exposes architected ST table:
VFIO_DEVICE_TPH_CAP_PROG_ST_NONEandVFIO_DEVICE_TPH_CAP_PROG_ST_CPUare reported. These indicate userspace may useTPH_PROGRAMto update ST.
b. If virtualized TPH supports DS mode:
VFIO_DEVICE_TPH_CAP_RESOLVE_CPU_STis reported. This indicates userspace can resolve CPU-ID associated ST values viaTPH_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. ReturnVFIO_DEVICE_TPH_CAP_RESOLVE_DMABUF_PHin alltph_policyinVFIO_DEVICE_FEATURE_TPHGET, so that userspace could resolve DMABUF's TPH PH info
2. ReturnVFIO_DEVICE_TPH_CAP_PROG_ST_DMABUFif virtualized TPH has the architect ST, so that userspace could program ST entries which associated to DMABUFs' fd
3. ReturnVFIO_DEVICE_TPH_CAP_RESOLVE_DMABUF_STif 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
- Addcpu_online()check in ST resolve routine
- Gate IV/DS capability exposure on TPH_DSMpresence; hide IV/DS when DSM unavailable
- Fix kernel-doc and redundant check inpcie_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/[email protected]/- Add expose
tph_policyvia debugfsChengwen Feng (13):
PCI/TPH: Fixpcie_tph_get_st_table_loc()field extraction PCI/TPH: Fixtph_enabledconcurrent update race by bitfield packing PCI/TPH: Cache negotiated max requester type at probe time PCI/TPH: Refactorpcie_enable_tphand add requester variant PCI/TPH: Refactorpcie_tph_get_cpu_st& add requester variant PCI/TPH: Add helpers to check standard and extended TPH support PCI/TPH: Addpcie_tph_set_st_entries()batch ST entry programming
helper
PCI/TPH: Addpcie_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 andTPH_PROGRAMbatch programming
support
vfio/pci: Add DS-ST TPH policy andTPH_RESOLVEcapability vfio/pci: Expose per-device TPH state via debugfsdrivers/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(-)
[email protected]
AI reviewSashiko has finished reviewing this 13-patch series:
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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]