Introduce new VFIO features for zPCI devices to provide FMB passthrough to userspace. Allow the user to enable or disable the FMB using the SET-only feature VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE. Likewise allow the user to read the latest FMB using the GET-only feature VFIO_DEVICE_FEATURE_ZPCI_FMB_READ in the case where the FMB is enabled. Additionally, when initializing the FMB kmem_cache, allow copy to userspace within the FMB's structure range. Signed-off-by: Omar Elghoul --- arch/s390/pci/pci.c | 8 ++++- drivers/vfio/pci/vfio_pci_core.c | 4 +++ drivers/vfio/pci/vfio_pci_priv.h | 18 +++++++++++ drivers/vfio/pci/vfio_pci_zdev.c | 55 ++++++++++++++++++++++++++++++++ include/uapi/linux/vfio.h | 29 +++++++++++++++++ 5 files changed, 113 insertions(+), 1 deletion(-) diff --git a/arch/s390/pci/pci.c b/arch/s390/pci/pci.c index 21d3fccac789..104a924f8e4b 100644 --- a/arch/s390/pci/pci.c +++ b/arch/s390/pci/pci.c @@ -1134,8 +1134,14 @@ static int zpci_mem_init(void) BUILD_BUG_ON((CONFIG_ILLEGAL_POINTER_VALUE + 0x10000 > ZPCI_IOMAP_ADDR_BASE) && (CONFIG_ILLEGAL_POINTER_VALUE <= ZPCI_IOMAP_ADDR_MAX)); + struct kmem_cache_args fmb_cache_args = { + .align = __alignof__(struct zpci_fmb), + .useroffset = 0, + .usersize = sizeof(struct zpci_fmb) + }; + zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), - __alignof__(struct zpci_fmb), 0, NULL); + &fmb_cache_args, 0); if (!zdev_fmb_cache) goto error_fmb; diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 050e7542952e..44e8e5526eae 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1569,6 +1569,10 @@ int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, return vfio_pci_core_feature_token(vdev, flags, arg, argsz); case VFIO_DEVICE_FEATURE_DMA_BUF: return vfio_pci_core_feature_dma_buf(vdev, flags, arg, argsz); + case VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE: + return vfio_pci_zdev_feature_fmb_enable(vdev, flags, arg, argsz); + case VFIO_DEVICE_FEATURE_ZPCI_FMB_READ: + return vfio_pci_zdev_feature_fmb_read(vdev, flags, arg, argsz); default: return -ENOTTY; } diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h index fca9d0dfac90..b7db064a6a95 100644 --- a/drivers/vfio/pci/vfio_pci_priv.h +++ b/drivers/vfio/pci/vfio_pci_priv.h @@ -93,6 +93,10 @@ int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev, struct vfio_info_cap *caps); int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev); void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev); +int vfio_pci_zdev_feature_fmb_enable(struct vfio_pci_core_device *vdev, u32 flags, + void __user *arg, size_t argsz); +int vfio_pci_zdev_feature_fmb_read(struct vfio_pci_core_device *vdev, u32 flags, + void __user *arg, size_t argsz); #else static inline int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev, struct vfio_info_cap *caps) @@ -107,6 +111,20 @@ static inline int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev) static inline void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev) {} + +static inline int vfio_pci_zdev_feature_fmb_enable(struct vfio_pci_core_device *vdev, + u32 flags, void __user *arg, + size_t argsz) +{ + return -ENOTTY; +} + +static inline int vfio_pci_zdev_feature_fmb_read(struct vfio_pci_core_device *vdev, + u32 flags, void __user *arg, + size_t argsz) +{ + return -ENOTTY; +} #endif static inline bool vfio_pci_is_vga(struct pci_dev *pdev) diff --git a/drivers/vfio/pci/vfio_pci_zdev.c b/drivers/vfio/pci/vfio_pci_zdev.c index 0990fdb146b7..ad1bcaf52ff2 100644 --- a/drivers/vfio/pci/vfio_pci_zdev.c +++ b/drivers/vfio/pci/vfio_pci_zdev.c @@ -167,3 +167,58 @@ void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev) if (zpci_kvm_hook.kvm_unregister) zpci_kvm_hook.kvm_unregister(zdev); } + +int vfio_pci_zdev_feature_fmb_enable(struct vfio_pci_core_device *vdev, u32 flags, + void __user *arg, size_t argsz) +{ + struct zpci_dev *zdev; + struct vfio_device_feature_zpci_fmb_enable fmb_enable; + int ret; + + ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, sizeof(fmb_enable)); + if (ret != 1) + return ret; + + zdev = to_zpci(vdev->pdev); + if (!zdev) + return -ENODEV; + + if (copy_from_user(&fmb_enable, arg, sizeof(fmb_enable))) + return -EFAULT; + + guard(mutex)(&zdev->fmb_lock); + + if (fmb_enable.enabled) + return zpci_fmb_enable_device(zdev); + return zpci_fmb_disable_device(zdev); +} + +int vfio_pci_zdev_feature_fmb_read(struct vfio_pci_core_device *vdev, u32 flags, + void __user *arg, size_t argsz) +{ + struct zpci_dev *zdev; + struct vfio_device_feature_zpci_fmb_read fmb_read; + int ret; + + ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET, sizeof(fmb_read)); + if (ret != 1) + return ret; + + zdev = to_zpci(vdev->pdev); + if (!zdev) + return -ENODEV; + + guard(mutex)(&zdev->fmb_lock); + + if (!zdev->fmb) + return -ENOMSG; + if (copy_from_user(&fmb_read, arg, sizeof(fmb_read))) + return -EFAULT; + if (!fmb_read.data) + return -EINVAL; + + if (copy_to_user((struct zpci_fmb __user *) fmb_read.data, zdev->fmb, zdev->fmb_length)) + return -EFAULT; + + return 0; +} diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 5de618a3a5ee..97e0f857fe4f 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -1534,6 +1534,35 @@ struct vfio_device_feature_dma_buf { */ #define VFIO_DEVICE_FEATURE_MIG_PRECOPY_INFOv2 12 +/** + * Upon VFIO_DEVICE_FEATURE_SET, enable or disable FMB for the VFIO zPCI device. + * + * enabled is treated as a bool, so any non-zero value evaluates to true. This + * feature fails on attempt to double enable/disable. + * + * Returns: 0 on success, -1 and errno set appropriately on error. + */ +#define VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE 13 + +struct vfio_device_feature_zpci_fmb_enable { + __u8 enabled; +}; + +/** + * Upon VFIO_DEVICE_FEATURE_GET, provide FMB passthrough for VFIO zPCI devices. + * + * The user-provided buffer must be at least fmb_length large, where fmb_length + * is reported in VFIO_DEVICE_INFO_CAP_ZPCI_BASE. + * + * Returns: 0 on success, -1 and errno set appropriately on error. errno==ENOMSG + * when the FMB is not enabled. + */ +#define VFIO_DEVICE_FEATURE_ZPCI_FMB_READ 14 + +struct vfio_device_feature_zpci_fmb_read { + __aligned_u64 data; +}; + /* -------- API for Type1 VFIO IOMMU -------- */ /** -- 2.54.0