vfio/pci: Add PCIe TPH support
Subsystems (3)
v6 cover vfio/pci: Add PCIe TPH support
This patchset enables userspace control over PCIe TPH steering tags, motivated by the following considerations:
1. Why userspace needs the capability to control steering tags:
When PCIe devices are fully owned by userspace workloads such as DPDK and SPDK, only userspace has full knowledge of core binding policies and traffic distribution strategies. Without this series, userspace cannot enable TPH or configure steering tags, leaving built-in PCIe performance optimizations unused in high-throughput polling I/O scenarios.
2. Why this interface must be implemented in VFIO:
VFIO is the standard, secure community solution for granting full PCIe device ownership to userspace. Existing kernel TPH interfaces are designed purely for in-kernel drivers. For user-owned devices, VFIO provides the only isolated and correct path to expose per-device TPH management.
TPH supports both IV and DS modes. Since device-specific (DS) TPH mode introduces cross-VM isolation risks such as untrusted guests programming arbitrary steering tags to impact other domains, so a new module parameter enable_unsafe_tph_ds_mode is added. It defaults to off, and blocks all unsafe DS-mode TPH operations when disabled.
To restrict abuse of SET_ST and prevent arbitrary steering tag programming from userspace, the interface only accepts explicit CPU ID, memory type and index inputs. The kernel resolves the corresponding steering tag internally before programming, limiting userspace to controlled, index-based configuration.
Based on earlier RFC work by Wathsala Vithanage
v6:
- Address Alex's comment on [1/6] commit.
- Fix Sashiko review comments:
Add tph_cap validation for pcie_tph_get_st_modes/st_table_loc. Add argsz validation for each op cmd.
Move disable tph from ioctl-reset to register.
Verify reserved field for get/set ST op.
Fix ABI mismatch due to pointer arithmetic of get/st ST op.
v5:
- Fix pcie_tph_get_st_table_loc() field extraction bug
- Add disable TPH in vfio_pci_ioctl_reset() to clean software state v4:
- Address Jason's comment of restrict device-specific mode under module param control.
- Rename module param to enable_unsafe_tph_ds_mode
v3:
- Add module param enable_unsafe_tph_ds to guard unsafe usage of TPH device-specific mode with no ST table
v2:
- Export pcie_tph_get_st_modes()
- Add detailed comment for UAPI structures and operations
- Add batch entry limit VFIO_TPH_MAX_ENTRIES
- Improve robustness and error handling
Chengwen Feng (6):
PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction PCI/TPH: Export pcie_tph_get_st_modes() for external use vfio/pci: Add PCIe TPH interface with capability query vfio/pci: Add PCIe TPH enable/disable support vfio/pci: Add PCIe TPH GET_ST interface vfio/pci: Add PCIe TPH SET_ST interface
drivers/pci/tph.c | 26 +++- drivers/vfio/pci/vfio_pci.c | 13 +- drivers/vfio/pci/vfio_pci_core.c | 257 ++++++++++++++++++++++++++++++- include/linux/pci-tph.h | 7 + include/linux/vfio_pci_core.h | 3 +- include/uapi/linux/vfio.h | 131 ++++++++++++++++ 6 files changed, 426 insertions(+), 11 deletions(-)
Signature · 1 line
2.17.1
PATCH v6 1/6 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.
While this change appears to be a no-op within tph.c, the external caller mlx5_st_create() relies on the documented function behavior, making this fix necessary.
--- drivers/pci/tph.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/pci/tph.c +1 -7
@@ -170,7 +170,7 @@ u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)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);
@@ -183,11 +183,7 @@ u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)u32 reg;u32 loc; -/* 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 ;
@@ -316,8 +312,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 :
Signature · 1 line
2.17.1
Re: [PATCH v6 1/6] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction
Quoted 11 lines · Chengwen Feng
pcie_tph_get_st_table_loc()incorrectly usesFIELD_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_MASKto return the unshifted field value matching the function specification.While this change appears to be a no-op within
tph.c, the external callermlx5_st_create()relies on the documented function behavior, making this fix necessary.
Previously, pcie_tph_get_st_table_loc() returned 0x0 (for PCI_TPH_LOC_NONE), 0x2 (for PCI_TPH_LOC_CAP), or 0x4 (for PCI_TPH_LOC_MSIX).
mlx5_st_create() is currently the only external caller. It only checks for PCI_TPH_LOC_NONE, which is 0 regardless of the FIELD_GET(), so I don't think this actually fixes mlx5_st_create().
Probably still worth a stable backport because other drivers may call pcie_tph_get_st_table_loc() in the future, and if they depend on PCI_TPH_LOC_CAP or PCI_TPH_LOC_MSIX, and are backported to stable kernels, they will need this.
I might reword that last paragraph to avoid the implication that this actually fixes something in mlx5:
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.
Quoted 4 lines
Fixes d2e8a34876ce ("PCI/TPH: Add Steering Tag support")Signed-off-by Chengwen Feng <[email protected]>Reviewed-by Alex Williamson <[email protected]>
Feel free to merge along with the rest, I assume via the VFIO tree.
Quoted 41 lines
--- drivers/pci/tph.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)drivers/pci/tph.c +1
-7@@ -170,7 +170,7 @@ u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)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);@@ -183,11 +183,7 @@ u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)u32 reg;u32 loc; -/* 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 ;@@ -316,8 +312,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 :Signature · 1 line
2.17.1
Re: [PATCH v6 1/6] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction
Quoted 26 lines · Bjorn Helgaas
Quoted 11 lines · Chengwen Feng
pcie_tph_get_st_table_loc()incorrectly usesFIELD_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_MASKto return the unshifted field value matching the function specification.While this change appears to be a no-op within
tph.c, the external callermlx5_st_create()relies on the documented function behavior, making this fix necessary.Previously,
pcie_tph_get_st_table_loc()returned0x0(forPCI_TPH_LOC_NONE),0x2(forPCI_TPH_LOC_CAP), or0x4(forPCI_TPH_LOC_MSIX).
mlx5_st_create()is currently the only external caller. It only checks forPCI_TPH_LOC_NONE, which is 0 regardless of theFIELD_GET(), so I don't think this actually fixesmlx5_st_create().Probably still worth a stable backport because other drivers may call
pcie_tph_get_st_table_loc()in the future, and if they depend onPCI_TPH_LOC_CAPorPCI_TPH_LOC_MSIX, and are backported to stable kernels, they will need this.
Quoted 7 lines
I might reword that last paragraph to avoid the implication that this actually fixes something in mlx5:
This doesn't make a difference to
mlx5_st_create(), the lone external caller, because it only checks forPCI_TPH_LOC_NONE(0), but will be needed for callers that check forPCI_TPH_LOC_CAPorPCI_TPH_LOC_MSIX.
Right, we are running TPH in mlx5 regression and it works perfectly without "the fix".
Thanks
PATCH v6 2/6 PCI/TPH: Export pcie_tph_get_st_modes() for external use
In reply to Chengwen FengExport the helper to retrieve supported PCIe TPH steering tag modes so that drivers like VFIO can query and expose device capabilities to userspace.
Add stub functions for pcie_tph_get_st_table_size() and pcie_tph_get_st_table_loc() when !CONFIG_PCI_TPH.
Add tph_cap validation for pcie_tph_get_st_modes() and pcie_tph_get_st_table_loc() to prevent invalid PCI configuration space access when TPH is not supported.
--- drivers/pci/tph.c | 20 ++++++++++++++++++-- include/linux/pci-tph.h | 7 +++++++ 2 files changed, 25 insertions(+), 2 deletions(-)
drivers/pci/tph.c +18 -2
@@ -145,15 +145,27 @@ static void set_ctrl_reg_req_en(struct pci_dev *pdev, u8 req_type)pci_write_config_dword (pdev, pdev->tph_cap +PCI_TPH_CTRL , reg); } -static u8 get_st_modes (struct pci_dev *pdev) +/** +* pcie_tph_get_st_modes - Get supported Steering Tag modes +* @pdev: PCI device to query +* +* Return: +* Bitmask of supported ST modes (PCI_TPH_CAP_ST_NS, PCI_TPH_CAP_ST_IV, +* PCI_TPH_CAP_ST_DS) +*/ +u8 pcie_tph_get_st_modes (struct pci_dev *pdev) {u32 reg; +if (!pdev->tph_cap) +return 0 ; +pci_read_config_dword (pdev, pdev->tph_cap +PCI_TPH_CAP , ®); reg &=PCI_TPH_CAP_ST_NS |PCI_TPH_CAP_ST_IV |PCI_TPH_CAP_ST_DS ;return reg; } +EXPORT_SYMBOL (pcie_tph_get_st_modes);/** * pcie_tph_get_st_table_loc - Return the device's ST table location
@@ -168,6 +180,9 @@ u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev) {u32 reg; +if (!pdev->tph_cap) +return PCI_TPH_LOC_NONE ; +pci_read_config_dword (pdev, pdev->tph_cap +PCI_TPH_CAP , ®);return reg &PCI_TPH_CAP_LOC_MASK ;
@@ -183,6 +198,7 @@ u16 pcie_tph_get_st_table_size(struct pci_dev *pdev)u32 reg;u32 loc; +/* Check ST table location first */ loc =pcie_tph_get_st_table_loc (pdev);if (loc !=PCI_TPH_LOC_CAP )return 0 ;
@@ -394,7 +410,7 @@ int pcie_enable_tph(struct pci_dev *pdev, int mode)/* Sanitize and check ST mode compatibility */ mode &=PCI_TPH_CTRL_MODE_SEL_MASK ; - dev_modes =get_st_modes (pdev); + dev_modes =pcie_tph_get_st_modes (pdev);if (!((1 << mode) & dev_modes))return -EINVAL ;
include/linux/pci-tph.h +7 -0
@@ -30,6 +30,7 @@ void pcie_disable_tph(struct pci_dev *pdev);int pcie_enable_tph (struct pci_dev *pdev,int mode);u16 pcie_tph_get_st_table_size (struct pci_dev *pdev);u32 pcie_tph_get_st_table_loc (struct pci_dev *pdev); +u8 pcie_tph_get_st_modes (struct pci_dev *pdev);#else static inline int pcie_tph_set_st_entry (struct pci_dev *pdev,unsigned int index,u16 tag)
@@ -41,6 +42,12 @@ 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 0x7FF ;/* Values that do not appear in normal case */ } +static inline u8 pcie_tph_get_st_modes (struct pci_dev *pdev) +{return 0 ; }#endif #endif /* LINUX_PCI_TPH_H */
Signature · 1 line
2.17.1
[email protected]
AI reviewRe: [PATCH v6 2/6] PCI/TPH: Export pcie_tph_get_st_modes() for external use
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Missing error handling for pci_read_config_dword() in the newly exported pcie_tph_get_st_modes()…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Missing error handling for pci_read_config_dword() in the newly exported pcie_tph_get_st_modes() leads to falsely reporting all TPH modes as supported when a device is disconnected.
- [Low] The !CONFIG_PCI_TPH stub for pcie_tph_get_st_table_loc() returns an inconsistent out-of-bounds value (0x7FF) instead of PCI_TPH_LOC_NONE.
--
commit d23fc69219a490dd1458be7c3d6f50ae929f70cd
Author: Chengwen Feng <[email protected]>
PCI/TPH: Export pcie_tph_get_st_modes() for external use
This commit exports the pcie_tph_get_st_modes() helper so drivers like VFIO can query supported PCIe TPH steering tag modes. It also adds stub functions for when !CONFIG_PCI_TPH and tph_cap validation.
Quoted 4 lines
drivers/pci/tph.c +0
-0
[ ... ]
Quoted 8 lines
drivers/pci/tph.c +6
-1@@ -145,15 +145,27 @@ static void set_ctrl_reg_req_en(struct pci_dev *pdev, u8 req_type) -static u8 get_st_modes (struct pci_dev *pdev) +/** +* pcie_tph_get_st_modes - Get supported Steering Tag modes +* @pdev: PCI device to query +* +* Return: +* Bitmask of supported ST modes (PCI_TPH_CAP_ST_NS, PCI_TPH_CAP_ST_IV,
drivers/pci/tph.c static void set_ctrl_reg_req_en(struct pci_dev *pdev, u8 req_type) · line 160
Quoted 10 lines
drivers/pci/tph.c +6
-0+* PCI_TPH_CAP_ST_DS) +*/ +u8 pcie_tph_get_st_modes (struct pci_dev *pdev) {u32 reg; +if (!pdev->tph_cap) +return 0 ; +pci_read_config_dword (pdev, pdev->tph_cap +PCI_TPH_CAP , ®);
If pci_read_config_dword() fails (e.g., when a device is disconnected), it populates the output variable with an error response (~0 or 0xFFFFFFFF).
Since the return value is ignored here, will this masking operation:
drivers/pci/tph.c static void set_ctrl_reg_req_en(struct pci_dev *pdev, u8 req_type) · line 164
Quoted 1 line
drivers/pci/tph.c +0
-0reg &=PCI_TPH_CAP_ST_NS |PCI_TPH_CAP_ST_IV |PCI_TPH_CAP_ST_DS ;
evaluate to 0x07 and incorrectly report all three Steering Tag modes as supported during a device disconnection?
Quoted 4 lines
drivers/pci/tph.c +1
-0return reg; } +EXPORT_SYMBOL (pcie_tph_get_st_modes);
[ ... ]
Quoted 4 lines
include/linux/pci-tph.h +0
-0
[ ... ]
include/linux/pci-tph.h static inline int pcie_tph_get_cpu_st(struct pci_dev *dev, · line 48
Quoted 8 lines
include/linux/pci-tph.h +4
-0@@ -41,6 +42,12 @@ 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 0x7FF ;/* Values that do not appear in normal case */ }
When TPH is enabled but a device lacks TPH capabilities, the live function safely returns PCI_TPH_LOC_NONE.
Does returning 0x7FF in this stub introduce an inconsistency in the API that forces callers to handle arbitrary magic numbers instead of relying on PCI_TPH_LOC_NONE?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v6 3/6 vfio/pci: Add PCIe TPH interface with capability query
In reply to Chengwen FengAdd VFIO_DEVICE_PCI_TPH IOCTL to allow userspace to query device TPH capabilities, supported modes, and steering tag table information.
Add module parameter 'enable_unsafe_tph_ds_mode' to restrict unsafe device-specific TPH mode to trusted userspace only.
--- drivers/vfio/pci/vfio_pci.c | 13 ++- drivers/vfio/pci/vfio_pci_core.c | 55 ++++++++++++- include/linux/vfio_pci_core.h | 3 +- include/uapi/linux/vfio.h | 131 +++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+), 3 deletions(-)
drivers/vfio/pci/vfio_pci.c +12 -1
@@ -60,6 +60,12 @@ 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." ); +#ifdef CONFIG_PCIE_TPH +static bool enable_unsafe_tph_ds_mode; +module_param (enable_unsafe_tph_ds_mode, bool,0444 ); +MODULE_PARM_DESC (enable_unsafe_tph_ds_mode,"Enable UNSAFE TPH device-specific (DS) mode. This mode provides weak isolation, cannot be safely used for virtual machines. If you do not know what this is for, step away. (default: false)" ); +#endif +static bool vfio_pci_dev_in_denylist (struct pci_dev *pdev) {switch (pdev->vendor) {
@@ -257,12 +263,17 @@ static int __init vfio_pci_init(void) {int ret;bool is_disable_vga = true; +bool is_enable_unsafe_tph_ds_mode = false;#ifdef CONFIG_VFIO_PCI_VGA is_disable_vga = disable_vga;#endif +#ifdef CONFIG_PCIE_TPH + is_enable_unsafe_tph_ds_mode = enable_unsafe_tph_ds_mode; +#endif -vfio_pci_core_set_params (nointxmask, is_disable_vga, disable_idle_d3); +vfio_pci_core_set_params (nointxmask, is_disable_vga, disable_idle_d3, + is_enable_unsafe_tph_ds_mode);/* Register and scan for devices */ ret =pci_register_driver (&vfio_pci_driver);
drivers/vfio/pci/vfio_pci_core.c +54 -1
89 lines not on this page · raw message
include/linux/vfio_pci_core.h +2 -1
@@ -157,7 +157,8 @@ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev,const struct vfio_pci_regops *ops, size_t size, u32 flags, void *data);void vfio_pci_core_set_params (bool nointxmask,bool is_disable_vga, -bool is_disable_idle_d3); +bool is_disable_idle_d3, +bool is_enable_unsafe_tph_ds_mode);void vfio_pci_core_close_device (struct vfio_device *core_vdev);int vfio_pci_core_init_dev (struct vfio_device *core_vdev);void vfio_pci_core_release_dev (struct vfio_device *core_vdev);
include/uapi/linux/vfio.h +131 -0
137 lines not on this page · raw message
Signature · 1 line
2.17.1
[email protected]
AI reviewRe: [PATCH v6 3/6] vfio/pci: Add PCIe TPH interface with capability query
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The VFIO_DEVICE_PCI_TPH IOCTL reads PCI configuration space without ensuring the device is runtime…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The VFIO_DEVICE_PCI_TPH IOCTL reads PCI configuration space without ensuring the device is runtime resumed, potentially reporting bogus capabilities and causing hardware errors. --
commit c1b7fcc4c0c41ea45b0bcdd16ef1d416fcc244c3
Author: Chengwen Feng <[email protected]>
vfio/pci: Add PCIe TPH interface with capability query
This commit adds the VFIO_DEVICE_PCI_TPH IOCTL to allow userspace to query device TPH capabilities. It also introduces a module parameter to restrict unsafe device-specific TPH mode to trusted userspace.
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0
[ ... ]
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_ioeventfd(struct vfio_pci_core_device *vdev, · line 1471
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +6
-0@@ -1461,6 +1463,53 @@ static int vfio_pci_ioctl_ioeventfd(struct vfio_pci_core_device *vdev, ioeventfd.fd); } +static int vfio_pci_tph_get_cap (struct vfio_pci_core_device *vdev, +struct vfio_device_pci_tph_op *op, +void __user *uarg) +{ +struct pci_dev *pdev = vdev->pdev; +u8 mode =pcie_tph_get_st_modes (pdev);
Does this ioctl need to ensure the device is runtime resumed before reading its configuration space?
If the device is in a low-power state (like D3cold), won't the configuration reads in pcie_tph_get_st_modes() return 0xFFFFFFFF?
This could cause the kernel to falsely report that all TPH modes are supported and advertise an incorrect ST table size.
Should this handler (or vfio_pci_ioctl_tph) call pm_runtime_resume_and_get() before invoking hardware-touching operations, and pm_runtime_put() afterward?
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+struct vfio_pci_tph_cap cap = {0 }; + +if (op->argsz < offsetof(struct vfio_device_pci_tph_op , cap) + +sizeof (struct vfio_pci_tph_cap )) +return -EINVAL ; + +if (mode ==0 || mode ==PCI_TPH_CAP_ST_NS ) +return -EOPNOTSUPP ;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v6 4/6 vfio/pci: Add PCIe TPH enable/disable support
In reply to Chengwen FengAdd support to enable and disable TPH function with mode selection.
Restrict unsafe device-specific TPH mode to be allowed only when module parameter enable_unsafe_tph_ds_mode=1 is set.
Disable TPH when taking over ownership of the device.
--- drivers/vfio/pci/vfio_pci_core.c | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +43 -0
@@ -1492,6 +1492,43 @@ static int vfio_pci_tph_get_cap(struct vfio_pci_core_device *vdev,return 0 ; } +static int vfio_pci_tph_enable (struct vfio_pci_core_device *vdev, +struct vfio_device_pci_tph_op *op, +void __user *uarg) +{ +struct pci_dev *pdev = vdev->pdev; +struct vfio_pci_tph_ctrl ctrl; +int mode; + + if (op->argsz < offsetofend(struct vfio_device_pci_tph_op, ctrl) + +sizeof (struct vfio_pci_tph_ctrl )) +return -EINVAL ; + +if (copy_from_user (&ctrl, uarg,sizeof (ctrl))) +return -EFAULT ; + +if (ctrl.mode !=VFIO_PCI_TPH_MODE_IV && + ctrl.mode !=VFIO_PCI_TPH_MODE_DS ) +return -EINVAL ; + +if (ctrl.mode ==VFIO_PCI_TPH_MODE_DS && !enable_unsafe_tph_ds_mode) +return -EOPNOTSUPP ; + +/* Reserved must be zero */ +if (memchr_inv (ctrl.reserved,0 ,sizeof (ctrl.reserved))) +return -EINVAL ; + + mode = (ctrl.mode ==VFIO_PCI_TPH_MODE_IV ) ?PCI_TPH_ST_IV_MODE : +PCI_TPH_ST_DS_MODE ; +return pcie_enable_tph (pdev, mode); +} + +static int vfio_pci_tph_disable (struct vfio_pci_core_device *vdev) +{ +pcie_disable_tph (vdev->pdev); +return 0 ; +} +static int vfio_pci_ioctl_tph (struct vfio_pci_core_device *vdev,void __user *uarg) {
@@ -1504,6 +1541,10 @@ static int vfio_pci_ioctl_tph(struct vfio_pci_core_device *vdev,switch (op.op) {case VFIO_PCI_TPH_GET_CAP :return vfio_pci_tph_get_cap (vdev, &op, uarg + minsz); +case VFIO_PCI_TPH_ENABLE : +return vfio_pci_tph_enable (vdev, &op, uarg + minsz); +case VFIO_PCI_TPH_DISABLE : +return vfio_pci_tph_disable (vdev);default :/* Other ops are not implemented yet */ return -EINVAL ;
@@ -2259,6 +2300,8 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev) ret =vfio_register_group_dev (&vdev->vdev);if (ret) goto out_power; +/* Disable TPH when taking over ownership of the device */ +pcie_disable_tph (pdev);return 0 ; out_power:
Signature · 1 line
2.17.1
[email protected]
AI reviewRe: [PATCH v6 4/6] vfio/pci: Add PCIe TPH enable/disable support
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Incorrect `argsz` validation in `vfio_pci_tph_enable()` incorrectly adds the structure size twice…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Incorrect argsz validation in vfio_pci_tph_enable() incorrectly adds the structure size twice, breaking the UAPI.
- [High] Missing locking in vfio_pci_ioctl_tph() leads to config space and pci_dev bitfield data corruption.
- [High] Disabling TPH after registering the device exposes it to userspace races.
- [High] TPH state is not explicitly disabled when the VFIO device is closed, leading to state inconsistencies and subsequent failures. --
commit c8b020c06b71ba6da131d464466e50c2b03f622b
Author: Chengwen Feng <[email protected]>
vfio/pci: Add PCIe TPH enable/disable support
This patch adds support to enable and disable the TPH function with mode selection. It restricts the unsafe device-specific TPH mode via a module parameter and disables TPH when taking over device ownership.
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0
[ ... ]
Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +1
-0@@ -1492,6 +1492,43 @@ static int vfio_pci_tph_get_cap(struct vfio_pci_core_device *vdev,return 0 ; } +static int vfio_pci_tph_enable(struct vfio_pci_core_device *vdev,
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_tph_get_cap(struct vfio_pci_core_device *vdev, · line 1505
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+struct vfio_device_pci_tph_op *op, + void __user *uarg) +{ + struct pci_dev *pdev = vdev->pdev; +struct vfio_pci_tph_ctrl ctrl; +int mode; + + if (op->argsz < offsetofend(struct vfio_device_pci_tph_op, ctrl) + +sizeof (struct vfio_pci_tph_ctrl )) +return -EINVAL ;
Since offsetofend(TYPE, MEMBER) already includes the size of the ctrl member, does adding sizeof(struct vfio_pci_tph_ctrl) a second time require more bytes than the actual structure size?
Could this cause the check to reject valid userspace calls that provide the exact structure size?
Quoted 9 lines
drivers/vfio/pci/vfio_pci_core.c +9
-0+ +if (copy_from_user (&ctrl, uarg,sizeof (ctrl))) +return -EFAULT ; + +if (ctrl.mode !=VFIO_PCI_TPH_MODE_IV && + ctrl.mode !=VFIO_PCI_TPH_MODE_DS ) +return -EINVAL ; + +if (ctrl.mode ==VFIO_PCI_TPH_MODE_DS && !enable_unsafe_tph_ds_mode)
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_tph_get_cap(struct vfio_pci_core_device *vdev, · line 1524
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+return -EOPNOTSUPP ; + +/* Reserved must be zero */ +if (memchr_inv (ctrl.reserved,0 ,sizeof (ctrl.reserved))) +return -EINVAL ; + + mode = (ctrl.mode ==VFIO_PCI_TPH_MODE_IV ) ?PCI_TPH_ST_IV_MODE : +PCI_TPH_ST_DS_MODE ; +return pcie_enable_tph (pdev, mode); +}
If userspace enables TPH but crashes or closes the file descriptor without manually disabling it, is the TPH state left enabled since the release path does not call pcie_disable_tph()?
If the device is opened again, could subsequent attempts to enable TPH fail with -EBUSY because pcie_enable_tph() sees it already enabled?
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0+ +static int vfio_pci_tph_disable (struct vfio_pci_core_device *vdev) +{ +pcie_disable_tph (vdev->pdev); +return 0 ; +} +static int vfio_pci_ioctl_tph (struct vfio_pci_core_device *vdev,void __user *uarg) {
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_tph(struct vfio_pci_core_device *vdev, · line 1547
Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0@@ -1504,6 +1541,10 @@ static int vfio_pci_ioctl_tph(struct vfio_pci_core_device *vdev,switch (op.op) {case VFIO_PCI_TPH_GET_CAP :return vfio_pci_tph_get_cap (vdev, &op, uarg + minsz); +case VFIO_PCI_TPH_ENABLE : +return vfio_pci_tph_enable (vdev, &op, uarg + minsz); +case VFIO_PCI_TPH_DISABLE : +return vfio_pci_tph_disable (vdev);
Because vfio_pci_ioctl_tph() handles these ioctls locklessly without holding vdev->igate, does concurrently calling pcie_enable_tph() cause a data race on the pdev->tph_enabled bitfield?
Could this concurrently overwrite adjacent critical bitfields in struct pci_dev like msi_enabled or is_managed?
Quoted 3 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0default :/* Other ops are not implemented yet */ return -EINVAL ;
[ ... ]
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev) · line 2304
Quoted 6 lines
drivers/vfio/pci/vfio_pci_core.c +2
-0@@ -2259,6 +2300,8 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev) ret =vfio_register_group_dev (&vdev->vdev);if (ret) goto out_power; +/* Disable TPH when taking over ownership of the device */ +pcie_disable_tph (pdev);
Is it safe to call pcie_disable_tph() after vfio_register_group_dev()?
Since the device becomes visible to userspace once registered, could a userspace process instantly open the device and enable TPH, racing with this initialization thread and overwriting the user's configuration?
Should hardware state initialization happen before publishing the device to userspace?
Quoted 3 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0return 0 ; out_power:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v6 5/6 vfio/pci: Add PCIe TPH GET_ST interface
In reply to Chengwen FengAdd support to batch get CPU steering tags for device-specific TPH mode that does not implement an ST table. This interface requires enabling the 'enable_unsafe_tph_ds_mode' module parameter.
--- drivers/vfio/pci/vfio_pci_core.c | 73 ++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +73 -0
@@ -1529,6 +1529,77 @@ static int vfio_pci_tph_disable(struct vfio_pci_core_device *vdev)return 0 ; } +static int vfio_pci_tph_get_st (struct vfio_pci_core_device *vdev, +struct vfio_device_pci_tph_op *op, +void __user *uarg) +{ +struct pci_dev *pdev = vdev->pdev; +struct vfio_pci_tph_entry *ents; +struct vfio_pci_tph_st st; +enum tph_mem_type mtype; +size_t size, ents_off; +int i, err; + +if (!enable_unsafe_tph_ds_mode || +pcie_tph_get_st_table_loc (pdev) !=PCI_TPH_LOC_NONE ) +return -EOPNOTSUPP ; + +if (copy_from_user (&st, uarg,sizeof (st))) +return -EFAULT ; + +/* Check reserved fields are zero */ +if (memchr_inv (&st.reserved,0 ,sizeof (st.reserved))) +return -EINVAL ; + +if (!st.count || st.count >VFIO_PCI_TPH_MAX_ENTRIES ) +return -EINVAL ; + + size = st.count *sizeof (*ents); + if (op->argsz < offsetofend(struct vfio_device_pci_tph_op, st) + +sizeof (struct vfio_pci_tph_st ) + size) +return -EINVAL ; + + ents =kvmalloc (size,GFP_KERNEL ); +if (!ents) +return -ENOMEM ; + + ents_off = offsetof(struct vfio_pci_tph_st , ents); +if (copy_from_user (ents, uarg + ents_off, size)) { + err = -EFAULT ; + goto out; + } + +for (i =0 ; i < st.count; i++) { +/* Check reserved fields are zero */ +if (memchr_inv (&ents[i].reserved0,0 ,sizeof (ents[i].reserved0)) || +memchr_inv (&ents[i].reserved1,0 ,sizeof (ents[i].reserved1))) { + err = -EINVAL ; + goto out; + } + +if (ents[i].mem_type ==VFIO_PCI_TPH_MEM_TYPE_VM ) { + mtype =TPH_MEM_TYPE_VM ; + }else if (ents[i].mem_type ==VFIO_PCI_TPH_MEM_TYPE_PM ) { + mtype =TPH_MEM_TYPE_PM ; + }else { + err = -EINVAL ; + goto out; + } + + err =pcie_tph_get_cpu_st (pdev, mtype, ents[i].cpu, + &ents[i].st); +if (err) + goto out; + } + +if (copy_to_user (uarg + ents_off, ents, size)) + err = -EFAULT ; + +out: +kvfree (ents); +return err; +} +static int vfio_pci_ioctl_tph (struct vfio_pci_core_device *vdev,void __user *uarg) {
@@ -1545,6 +1616,8 @@ static int vfio_pci_ioctl_tph(struct vfio_pci_core_device *vdev,return vfio_pci_tph_enable (vdev, &op, uarg + minsz);case VFIO_PCI_TPH_DISABLE :return vfio_pci_tph_disable (vdev); +case VFIO_PCI_TPH_GET_ST : +return vfio_pci_tph_get_st (vdev, &op, uarg + minsz);default :/* Other ops are not implemented yet */ return -EINVAL ;
Signature · 1 line
2.17.1
[email protected]
AI reviewRe: [PATCH v6 5/6] vfio/pci: Add PCIe TPH GET_ST interface
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The `argsz` validation incorrectly double-counts the size of the union member, causing valid…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The argsz validation incorrectly double-counts the size of the union member, causing valid requests to be rejected.
- [High] Concurrent TPH ioctls lead to data races on unprotected PCIe TPH state variables and device configuration space.
- [Medium] The unused index field in struct vfio_pci_tph_entry is not validated to be zero during VFIO_PCI_TPH_GET_ST. --
commit 69a04a0a6e943f789b3703368fa523b4efd43755
Author: Chengwen Feng <[email protected]>
vfio/pci: Add PCIe TPH GET_ST interface
This commit adds support to batch get CPU steering tags for device-specific TPH mode that does not implement an ST table. This interface requires enabling the enable_unsafe_tph_ds_mode module parameter.
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0
[ ... ]
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0@@ -1529,6 +1529,77 @@ static int vfio_pci_tph_disable(struct vfio_pci_core_device *vdev)return 0 ; } +static int vfio_pci_tph_get_st (struct vfio_pci_core_device *vdev, +struct vfio_device_pci_tph_op *op, +void __user *uarg) +{
[ ... ]
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_tph_disable(struct vfio_pci_core_device *vdev) · line 1560
Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0+if (!st.count || st.count >VFIO_PCI_TPH_MAX_ENTRIES ) +return -EINVAL ; + + size = st.count *sizeof (*ents); + if (op->argsz < offsetofend(struct vfio_device_pci_tph_op, st) + +sizeof (struct vfio_pci_tph_st ) + size) +return -EINVAL ;
Is it possible the size calculation double-countssizeof(struct vfio_pci_tph_st)?
The macro offsetofend(TYPE, MEMBER) evaluates to offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER). For the st member, offsetofend() correctly calculates the end boundary. Adding sizeof(struct vfio_pci_tph_st) a second time makes the expected minimum argsz larger than it should be.
If userspace passes the properly calculated size, will the kernel incorrectly evaluate this as false and reject valid ioctl calls with -EINVAL?
(Note: The identical calculation error appears to be present in vfio_pci_tph_enable() introduced in a prior patch.)
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+ + ents =kvmalloc (size,GFP_KERNEL ); +if (!ents) +return -ENOMEM ; + + ents_off = offsetof(struct vfio_pci_tph_st , ents); +if (copy_from_user (ents, uarg + ents_off, size)) { + err = -EFAULT ;
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_tph_disable(struct vfio_pci_core_device *vdev) · line 1578
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ goto out; + } + +for (i =0 ; i < st.count; i++) { +/* Check reserved fields are zero */ +if (memchr_inv (&ents[i].reserved0,0 ,sizeof (ents[i].reserved0)) || +memchr_inv (&ents[i].reserved1,0 ,sizeof (ents[i].reserved1))) { + err = -EINVAL ; + goto out; + }
Does this code need to validate that the unused index field in struct vfio_pci_tph_entry is zero?
While reserved0 and reserved1 are checked, the index field appears to be ignored during GET_ST operations. Because userspace is not required to zero-fill unused arguments, this field may contain uninitialized garbage.
If a future kernel extension assigns meaning to index for GET_ST, will this lack of validation break backwards compatibility by making it impossible to distinguish intentional new values from uninitialized data in older applications?
[ ... ]
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_tph(struct vfio_pci_core_device *vdev, · line 1623
Quoted 12 lines
static int vfio_pci_ioctl_tph(struct
vfio_pci_core_device*vdev,drivers/vfio/pci/vfio_pci_core.c +2
-0void __user *uarg) {@@ -1545,6 +1616,8 @@ static int vfio_pci_ioctl_tph(struct vfio_pci_core_device *vdev,return vfio_pci_tph_enable (vdev, &op, uarg + minsz);case VFIO_PCI_TPH_DISABLE :return vfio_pci_tph_disable (vdev); +case VFIO_PCI_TPH_GET_ST : +return vfio_pci_tph_get_st (vdev, &op, uarg + minsz);default :/* Other ops are not implemented yet */ return -EINVAL ;
Are concurrent TPH ioctls adequately serialized here?
The vfio_pci_ioctl_tph() handler processes requests without acquiring any serialization locks (such as vdev->igate). If userspace issues multiple TPH ioctls concurrently, vfio_pci_tph_enable(), vfio_pci_tph_disable(), and vfio_pci_tph_get_st() can execute in parallel.
The underlying PCI core functions pcie_enable_tph() and pcie_disable_tph() perform lockless read-modify-write operations on the PCI_TPH_CTRL config register and mutate pdev->tph_mode, pdev->tph_req_type, and pdev->tph_enabled. Simultaneously, vfio_pci_tph_get_st() invokes pcie_tph_get_cpu_st(), which relies on these state variables.
Can this lead to data races on unprotected PCIe TPH state variables, resulting in corrupted device configuration space or inconsistent state reads?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v6 6/6 vfio/pci: Add PCIe TPH SET_ST interface
In reply to Chengwen FengAdd VFIO_PCI_TPH_SET_ST operation to support batch programming of steering tag entries. If any entry fails, roll back successfully programmed entries to 0 to prevent inconsistent device state.
--- drivers/vfio/pci/vfio_pci_core.c | 86 ++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +86 -0
@@ -1600,6 +1600,90 @@ static int vfio_pci_tph_get_st(struct vfio_pci_core_device *vdev,return err; } +static int vfio_pci_tph_set_st (struct vfio_pci_core_device *vdev, +struct vfio_device_pci_tph_op *op, +void __user *uarg) +{ +struct pci_dev *pdev = vdev->pdev; +struct vfio_pci_tph_entry *ents; +struct vfio_pci_tph_st st; +enum tph_mem_type mtype; +size_t size, ents_off; +int i =0 , j, err; +u32 tab_loc; +u16 st_val; + + tab_loc =pcie_tph_get_st_table_loc (pdev); +if (tab_loc !=PCI_TPH_LOC_CAP && tab_loc !=PCI_TPH_LOC_MSIX ) +return -EOPNOTSUPP ; + +if (copy_from_user (&st, uarg,sizeof (st))) +return -EFAULT ; + +if (!st.count || st.count >VFIO_PCI_TPH_MAX_ENTRIES ) +return -EINVAL ; + +/* Check reserved fields are zero */ +if (memchr_inv (&st.reserved,0 ,sizeof (st.reserved))) +return -EINVAL ; + + size = st.count *sizeof (*ents); + if (op->argsz < offsetofend(struct vfio_device_pci_tph_op, st) + +sizeof (struct vfio_pci_tph_st ) + size) +return -EINVAL ; + + ents =kvmalloc (size,GFP_KERNEL ); +if (!ents) +return -ENOMEM ; + + ents_off = offsetof(struct vfio_pci_tph_st , ents); +if (copy_from_user (ents, uarg + ents_off, size)) { + err = -EFAULT ; + goto out; + } + +for (; i < st.count; i++) { +/* Check reserved fields are zero */ +if (memchr_inv (&ents[i].reserved0,0 ,sizeof (ents[i].reserved0)) || +memchr_inv (&ents[i].reserved1,0 ,sizeof (ents[i].reserved1))) { + err = -EINVAL ; + goto out; + } + +if (ents[i].cpu ==U32_MAX ) { + err =pcie_tph_set_st_entry (pdev, ents[i].index,0 ); +if (err) + goto out; +continue ; + } + +if (ents[i].mem_type ==VFIO_PCI_TPH_MEM_TYPE_VM ) { + mtype =TPH_MEM_TYPE_VM ; + }else if (ents[i].mem_type ==VFIO_PCI_TPH_MEM_TYPE_PM ) { + mtype =TPH_MEM_TYPE_PM ; + }else { + err = -EINVAL ; + goto out; + } + + err =pcie_tph_get_cpu_st (pdev, mtype, ents[i].cpu, &st_val); +if (err) + goto out; + err =pcie_tph_set_st_entry (pdev, ents[i].index, st_val); +if (err) + goto out; + } + +out: +if (err) { +/* Roll back previously programmed entries to 0 */ +for (j =0 ; j < i; j++) +pcie_tph_set_st_entry (pdev, ents[j].index,0 ); + } +kvfree (ents); +return err; +} +static int vfio_pci_ioctl_tph (struct vfio_pci_core_device *vdev,void __user *uarg) {
@@ -1618,6 +1702,8 @@ static int vfio_pci_ioctl_tph(struct vfio_pci_core_device *vdev,return vfio_pci_tph_disable (vdev);case VFIO_PCI_TPH_GET_ST :return vfio_pci_tph_get_st (vdev, &op, uarg + minsz); +case VFIO_PCI_TPH_SET_ST : +return vfio_pci_tph_set_st (vdev, &op, uarg + minsz);default :/* Other ops are not implemented yet */ return -EINVAL ;
Signature · 1 line
2.17.1
[email protected]
AI reviewRe: [PATCH v6 6/6] vfio/pci: Add PCIe TPH SET_ST interface
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Incorrect `argsz` size validation unconditionally rejects valid ioctl requests. - [Medium] The…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Incorrect argsz size validation unconditionally rejects valid ioctl requests.
- [Medium] The unused st field in struct vfio_pci_tph_entry is not validated for being zero during SET_ST, breaking forward ABI compatibility. --
commit cad3760ab856857766102c878427138245e1feb6
Author: Chengwen Feng <[email protected]>
vfio/pci: Add PCIe TPH SET_ST interface
This commit adds the VFIO_PCI_TPH_SET_ST operation to support batch programming of steering tag entries. It also implements a rollback mechanism to clear successfully programmed entries if any entry fails to prevent inconsistent device state.
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0
[ ... ]
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0@@ -1600,6 +1600,90 @@ static int vfio_pci_tph_get_st(struct vfio_pci_core_device *vdev,return err; } +static int vfio_pci_tph_set_st (struct vfio_pci_core_device *vdev, +struct vfio_device_pci_tph_op *op, +void __user *uarg) +{ +struct pci_dev *pdev = vdev->pdev; +struct vfio_pci_tph_entry *ents; +struct vfio_pci_tph_st st;
[ ... ]
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_tph_get_st(struct vfio_pci_core_device *vdev, · line 1633
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0+ size = st.count *sizeof (*ents); + if (op->argsz < offsetofend(struct vfio_device_pci_tph_op, st) + +sizeof (struct vfio_pci_tph_st ) + size) +return -EINVAL ;
Does adding sizeof(struct vfio_pci_tph_st) artificially inflate the required buffer size?
The offsetofend() macro already includes the size of the st member. If a properly written userspace application computes its argsz exactly as sizeof(struct vfio_device_pci_tph_op) + size, this check would fail and reject the request with -EINVAL.
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+ + ents =kvmalloc (size,GFP_KERNEL ); +if (!ents) +return -ENOMEM ; + + ents_off = offsetof(struct vfio_pci_tph_st , ents); +if (copy_from_user (ents, uarg + ents_off, size)) { + err = -EFAULT ;
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_tph_get_st(struct vfio_pci_core_device *vdev, · line 1651
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ goto out; + } + +for (; i < st.count; i++) { +/* Check reserved fields are zero */ +if (memchr_inv (&ents[i].reserved0,0 ,sizeof (ents[i].reserved0)) || +memchr_inv (&ents[i].reserved1,0 ,sizeof (ents[i].reserved1))) { + err = -EINVAL ; + goto out; + }
During the SET_ST operation, the st field in struct vfio_pci_tph_entry appears to be unused, as the steering tag is instead dynamically fetched via pcie_tph_get_cpu_st() later in the loop.
Should the st field also be validated to be zero?
If userspace leaves this unused field uninitialized and passes garbage data, it might break forward ABI compatibility. A future kernel update that attempts to read explicit steering tags directly from this field wouldn't be able to distinguish between old programs sending uninitialized garbage and new programs sending valid data.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6