vhost-vdpa: Add support for NO-IOMMU mode
Subsystems (3)
v3 cover vhost-vdpa: Add support for NO-IOMMU mode
This patchset introduces support for an UNSAFE, no-IOMMU mode in the vhost-vdpa driver. When enabled, this mode provides no device isolation, no DMA translation, no host kernel protection, and cannot be used for device assignment to virtual machines. It requires RAWIO permissions and will taint the kernel.
This mode requires enabling the "enable_vhost_vdpa_unsafe_noiommu_mode" option on the vhost-vdpa driver and also negotiate the feature flag VHOST_BACKEND_F_NOIOMMU. This mode would be useful to get better performance on specifice low end machines and can be leveraged by embedded platforms where applications run in controlled environment.
First patch introduces a module parameter "enable_vhost_vdpa_unsafe_noiommu_mode", while the second patch introduces a feature flag VHOST_BACKEND_F_NOIOMMU to the vhost vdpa driver to support NO-IOMMU mode.
This feature flag indicates to userspace that the driver can safely operate in NO-IOMMU mode. If the flag is not present, userspace should assume NO-IOMMU mode is unsupported and must not proceed.
v1->v2:
- Introduced new feature flag to vhost backend features for negotiating NO-IOMMU feature.v1->v3:
- Resolved the sparse error reported by kernel test robot.
Srujana Challa (2):
vhost-vdpa: introduce module parameter for no-IOMMU mode
vhost-vdpa: introduce NO-IOMMU backend feature bit
drivers/vhost/vdpa.c | 39 +++++++++++++++++++++++++++++++- include/uapi/linux/vhost_types.h | 2 ++ 2 files changed, 40 insertions(+), 1 deletion(-)
Signature · 1 line
2.25.1
PATCH v3 1/2 vhost-vdpa: introduce module parameter for no-IOMMU mode
This commit introduces module parameter "enable_vhost_vdpa_unsafe_noiommu_mode" to enable an UNSAFE, no-IOMMU mode in the vhost-vdpa driver. When enabled, this mode provides no device isolation, no DMA translation, no host kernel protection, and cannot be used for device assignment to virtual machines. It requires RAWIO permissions and will taint the kernel.
--- drivers/vhost/vdpa.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
drivers/vhost/vdpa.c +28 -0
@@ -36,6 +36,11 @@ enum {#define VHOST_VDPA_IOTLB_BUCKETS 16 +static bool vhost_vdpa_noiommu; +module_param_named (enable_vhost_vdpa_unsafe_noiommu_mode, + vhost_vdpa_noiommu, bool,0644 ); +MODULE_PARM_DESC (enable_vhost_vdpa_unsafe_noiommu_mode,"Enable UNSAFE, no-IOMMU mode. This mode provides no device isolation, no DMA translation, no host kernel protection, cannot be used for device assignment to virtual machines, requires RAWIO permissions, and will taint the kernel. If you do not know what this is for, step away. (default: false)" ); +struct vhost_vdpa_as {struct hlist_node hash_link;struct vhost_iotlb iotlb;
@@ -60,6 +65,7 @@ struct vhost_vdpa {struct vdpa_iova_range range;u32 batch_asid;bool suspended; +bool noiommu_en; };static DEFINE_IDA (vhost_vdpa_ida);
@@ -910,6 +916,10 @@ static void vhost_vdpa_general_unmap(struct vhost_vdpa *v, {struct vdpa_device *vdpa = v->vdpa;const struct vdpa_config_ops *ops = vdpa->config; + +if (v->noiommu_en) +return ; +if (ops->dma_map) { ops->dma_unmap (vdpa, asid, map->start, map->size); }else if (ops->set_map ==NULL ) {
@@ -1003,6 +1013,9 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,if (r)return r; +if (v->noiommu_en) + goto skip_map; +if (ops->dma_map) { r = ops->dma_map (vdpa, asid, iova, size, pa, perm, opaque); }else if (ops->set_map) {
@@ -1018,6 +1031,7 @@ static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,return r; } +skip_map:if (!vdpa->use_va)atomic64_add (PFN_DOWN (size), &dev->mm->pinned_vm);
@@ -1321,12 +1335,26 @@ static int vhost_vdpa_alloc_domain(struct vhost_vdpa *v)struct vdpa_device *vdpa = v->vdpa;const struct vdpa_config_ops *ops = vdpa->config;struct device *dma_dev =vdpa_get_dma_dev (vdpa); +struct iommu_domain *domain;int ret;/* Device want to do DMA by itself */ if (ops->set_map || ops->dma_map)return 0 ; + domain =iommu_get_domain_for_dev (dma_dev); +if (!domain && vhost_vdpa_noiommu) { +if (!capable (CAP_SYS_RAWIO )) { +dev_warn_once (&v->dev,"No RAWIO permissions, couldn't support NOIOMMU\n" ); +return -ENOTSUPP ; + } +add_taint (TAINT_USER ,LOCKDEP_STILL_OK ); +dev_warn (&v->dev,"Adding kernel taint for noiommu on device\n" ); + v->noiommu_en = true; +return 0 ; + } + v->noiommu_en = false; +if (!device_iommu_capable (dma_dev,IOMMU_CAP_CACHE_COHERENCY )) {dev_warn_once (&v->dev,"Failed to allocate domain, device is not IOMMU cache coherent capable\n" );
Signature · 1 line
2.25.1
PATCH v3 2/2 vhost-vdpa: introduce NO-IOMMU backend feature bit
In reply to Srujana ChallaThis patch introduces the VHOST_BACKEND_F_NOIOMMU feature flag. This flag allows userspace to identify if the driver can operate without an IOMMU, providing more flexibility in environments where IOMMU is not available or desired.
Key changes include:
- Addition of the VHOST_BACKEND_F_NOIOMMU feature flag.
- Updates to vhost_vdpa_unlocked_ioctl to handle the NO-IOMMU feature.
The NO-IOMMU mode is enabled if:
- The vdpa device lacks an IOMMU domain.
- The system has the required RAWIO permissions.
- The vdpa device explicitly supports NO-IOMMU mode.
This feature flag indicates to userspace that the driver can operate in NO-IOMMU mode. If the flag is absent, userspace should treat NO-IOMMU mode as unsupported and refrain from proceeding when IOMMU is not present.
--- drivers/vhost/vdpa.c | 11 ++++++++++- include/uapi/linux/vhost_types.h | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/vhost/vdpa.c +10 -1
@@ -797,7 +797,8 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,BIT_ULL (VHOST_BACKEND_F_IOTLB_PERSIST ) |BIT_ULL (VHOST_BACKEND_F_SUSPEND ) |BIT_ULL (VHOST_BACKEND_F_RESUME ) | - BIT_ULL(VHOST_BACKEND_F_ENABLE_AFTER_DRIVER_OK))) +BIT_ULL (VHOST_BACKEND_F_ENABLE_AFTER_DRIVER_OK ) | + BIT_ULL(VHOST_BACKEND_F_NOIOMMU))) return -EOPNOTSUPP;if ((features &BIT_ULL (VHOST_BACKEND_F_SUSPEND )) && !vhost_vdpa_can_suspend (v))
@@ -814,6 +815,12 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep,if ((features &BIT_ULL (VHOST_BACKEND_F_IOTLB_PERSIST )) && !vhost_vdpa_has_persistent_map (v))return -EOPNOTSUPP ; +if ((features &BIT_ULL (VHOST_BACKEND_F_NOIOMMU )) && + !v->noiommu_en) +return -EOPNOTSUPP ; +if (!(features &BIT_ULL (VHOST_BACKEND_F_NOIOMMU )) && + v->noiommu_en) +return -EOPNOTSUPP ;vhost_set_backend_features (&v->vdev, features);return 0 ; }
@@ -871,6 +878,8 @@ static long vhost_vdpa_unlocked_ioctl(struct file *filep, features |=BIT_ULL (VHOST_BACKEND_F_DESC_ASID );if (vhost_vdpa_has_persistent_map (v)) features |=BIT_ULL (VHOST_BACKEND_F_IOTLB_PERSIST ); +if (v->noiommu_en) + features |=BIT_ULL (VHOST_BACKEND_F_NOIOMMU ); features |=vhost_vdpa_get_backend_features (v);if (copy_to_user (featurep, &features,sizeof (features))) r = -EFAULT ;
include/uapi/linux/vhost_types.h +2 -0
@@ -192,5 +192,7 @@ struct vhost_vdpa_iova_range {#define VHOST_BACKEND_F_DESC_ASID 0x7/* IOTLB don't flush memory mapping across device reset */ #define VHOST_BACKEND_F_IOTLB_PERSIST 0x8 +/* Enables the device to operate in NO-IOMMU mode as well */ +#define VHOST_BACKEND_F_NOIOMMU 0x9#endif
Signature · 1 line
2.25.1