Skip to content

Commit 0ce5c24

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
Introduce a new IOMMUFD_OBJ_VDEVICE to represent a physical device (struct device) against a vIOMMU (struct iommufd_viommu) object in a VM. This vDEVICE object (and its structure) holds all the infos and attributes in the VM, regarding the device related to the vIOMMU. As an initial patch, add a per-vIOMMU virtual ID. This can be: - Virtual StreamID on a nested ARM SMMUv3, an index to a Stream Table - Virtual DeviceID on a nested AMD IOMMU, an index to a Device Table - Virtual RID on a nested Intel VT-D IOMMU, an index to a Context Table Potentially, this vDEVICE structure would hold some vData for Confidential Compute Architecture (CCA). Use this virtual ID to index an "vdevs" xarray that belongs to a vIOMMU object. Add a new ioctl for vDEVICE allocations. Since a vDEVICE is a connection of a device object and an iommufd_viommu object, take two refcounts in the ioctl handler. Link: https://patch.msgid.link/r/cda8fd2263166e61b8191a3b3207e0d2b08545bf.1730836308.git.nicolinc@nvidia.com Signed-off-by: Nicolin Chen <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 87210b1 commit 0ce5c24

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

drivers/iommu/iommufd/iommufd_private.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,26 @@ static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,
507507
return iommu_group_replace_domain(idev->igroup->group, hwpt->domain);
508508
}
509509

510+
static inline struct iommufd_viommu *
511+
iommufd_get_viommu(struct iommufd_ucmd *ucmd, u32 id)
512+
{
513+
return container_of(iommufd_get_object(ucmd->ictx, id,
514+
IOMMUFD_OBJ_VIOMMU),
515+
struct iommufd_viommu, obj);
516+
}
517+
510518
int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd);
511519
void iommufd_viommu_destroy(struct iommufd_object *obj);
520+
int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd);
521+
void iommufd_vdevice_destroy(struct iommufd_object *obj);
522+
523+
struct iommufd_vdevice {
524+
struct iommufd_object obj;
525+
struct iommufd_ctx *ictx;
526+
struct iommufd_viommu *viommu;
527+
struct device *dev;
528+
u64 id; /* per-vIOMMU virtual ID */
529+
};
512530

513531
#ifdef CONFIG_IOMMUFD_TEST
514532
int iommufd_test(struct iommufd_ucmd *ucmd);

drivers/iommu/iommufd/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ union ucmd_buffer {
308308
struct iommu_option option;
309309
struct iommu_vfio_ioas vfio_ioas;
310310
struct iommu_viommu_alloc viommu;
311+
struct iommu_vdevice_alloc vdev;
311312
#ifdef CONFIG_IOMMUFD_TEST
312313
struct iommu_test_cmd test;
313314
#endif
@@ -363,6 +364,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
363364
__reserved),
364365
IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl,
365366
struct iommu_viommu_alloc, out_viommu_id),
367+
IOCTL_OP(IOMMU_VDEVICE_ALLOC, iommufd_vdevice_alloc_ioctl,
368+
struct iommu_vdevice_alloc, virt_id),
366369
#ifdef CONFIG_IOMMUFD_TEST
367370
IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
368371
#endif
@@ -501,6 +504,9 @@ static const struct iommufd_object_ops iommufd_object_ops[] = {
501504
[IOMMUFD_OBJ_VIOMMU] = {
502505
.destroy = iommufd_viommu_destroy,
503506
},
507+
[IOMMUFD_OBJ_VDEVICE] = {
508+
.destroy = iommufd_vdevice_destroy,
509+
},
504510
#ifdef CONFIG_IOMMUFD_TEST
505511
[IOMMUFD_OBJ_SELFTEST] = {
506512
.destroy = iommufd_selftest_destroy,

drivers/iommu/iommufd/viommu.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ void iommufd_viommu_destroy(struct iommufd_object *obj)
1111
if (viommu->ops && viommu->ops->destroy)
1212
viommu->ops->destroy(viommu);
1313
refcount_dec(&viommu->hwpt->common.obj.users);
14+
xa_destroy(&viommu->vdevs);
1415
}
1516

1617
int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
@@ -53,6 +54,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
5354
goto out_put_hwpt;
5455
}
5556

57+
xa_init(&viommu->vdevs);
5658
viommu->type = cmd->type;
5759
viommu->ictx = ucmd->ictx;
5860
viommu->hwpt = hwpt_paging;
@@ -79,3 +81,77 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
7981
iommufd_put_object(ucmd->ictx, &idev->obj);
8082
return rc;
8183
}
84+
85+
void iommufd_vdevice_destroy(struct iommufd_object *obj)
86+
{
87+
struct iommufd_vdevice *vdev =
88+
container_of(obj, struct iommufd_vdevice, obj);
89+
struct iommufd_viommu *viommu = vdev->viommu;
90+
91+
/* xa_cmpxchg is okay to fail if alloc failed xa_cmpxchg previously */
92+
xa_cmpxchg(&viommu->vdevs, vdev->id, vdev, NULL, GFP_KERNEL);
93+
refcount_dec(&viommu->obj.users);
94+
put_device(vdev->dev);
95+
}
96+
97+
int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
98+
{
99+
struct iommu_vdevice_alloc *cmd = ucmd->cmd;
100+
struct iommufd_vdevice *vdev, *curr;
101+
struct iommufd_viommu *viommu;
102+
struct iommufd_device *idev;
103+
u64 virt_id = cmd->virt_id;
104+
int rc = 0;
105+
106+
/* virt_id indexes an xarray */
107+
if (virt_id > ULONG_MAX)
108+
return -EINVAL;
109+
110+
viommu = iommufd_get_viommu(ucmd, cmd->viommu_id);
111+
if (IS_ERR(viommu))
112+
return PTR_ERR(viommu);
113+
114+
idev = iommufd_get_device(ucmd, cmd->dev_id);
115+
if (IS_ERR(idev)) {
116+
rc = PTR_ERR(idev);
117+
goto out_put_viommu;
118+
}
119+
120+
if (viommu->iommu_dev != __iommu_get_iommu_dev(idev->dev)) {
121+
rc = -EINVAL;
122+
goto out_put_idev;
123+
}
124+
125+
vdev = iommufd_object_alloc(ucmd->ictx, vdev, IOMMUFD_OBJ_VDEVICE);
126+
if (IS_ERR(vdev)) {
127+
rc = PTR_ERR(vdev);
128+
goto out_put_idev;
129+
}
130+
131+
vdev->id = virt_id;
132+
vdev->dev = idev->dev;
133+
get_device(idev->dev);
134+
vdev->viommu = viommu;
135+
refcount_inc(&viommu->obj.users);
136+
137+
curr = xa_cmpxchg(&viommu->vdevs, virt_id, NULL, vdev, GFP_KERNEL);
138+
if (curr) {
139+
rc = xa_err(curr) ?: -EEXIST;
140+
goto out_abort;
141+
}
142+
143+
cmd->out_vdevice_id = vdev->obj.id;
144+
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
145+
if (rc)
146+
goto out_abort;
147+
iommufd_object_finalize(ucmd->ictx, &vdev->obj);
148+
goto out_put_idev;
149+
150+
out_abort:
151+
iommufd_object_abort_and_destroy(ucmd->ictx, &vdev->obj);
152+
out_put_idev:
153+
iommufd_put_object(ucmd->ictx, &idev->obj);
154+
out_put_viommu:
155+
iommufd_put_object(ucmd->ictx, &viommu->obj);
156+
return rc;
157+
}

include/linux/iommufd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/errno.h>
1111
#include <linux/refcount.h>
1212
#include <linux/types.h>
13+
#include <linux/xarray.h>
1314

1415
struct device;
1516
struct file;
@@ -31,6 +32,7 @@ enum iommufd_object_type {
3132
IOMMUFD_OBJ_ACCESS,
3233
IOMMUFD_OBJ_FAULT,
3334
IOMMUFD_OBJ_VIOMMU,
35+
IOMMUFD_OBJ_VDEVICE,
3436
#ifdef CONFIG_IOMMUFD_TEST
3537
IOMMUFD_OBJ_SELFTEST,
3638
#endif
@@ -89,6 +91,8 @@ struct iommufd_viommu {
8991

9092
const struct iommufd_viommu_ops *ops;
9193

94+
struct xarray vdevs;
95+
9296
unsigned int type;
9397
};
9498

include/uapi/linux/iommufd.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ enum {
5353
IOMMUFD_CMD_FAULT_QUEUE_ALLOC = 0x8e,
5454
IOMMUFD_CMD_IOAS_MAP_FILE = 0x8f,
5555
IOMMUFD_CMD_VIOMMU_ALLOC = 0x90,
56+
IOMMUFD_CMD_VDEVICE_ALLOC = 0x91,
5657
};
5758

5859
/**
@@ -864,4 +865,25 @@ struct iommu_viommu_alloc {
864865
__u32 out_viommu_id;
865866
};
866867
#define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC)
868+
869+
/**
870+
* struct iommu_vdevice_alloc - ioctl(IOMMU_VDEVICE_ALLOC)
871+
* @size: sizeof(struct iommu_vdevice_alloc)
872+
* @viommu_id: vIOMMU ID to associate with the virtual device
873+
* @dev_id: The physical device to allocate a virtual instance on the vIOMMU
874+
* @out_vdevice_id: Object handle for the vDevice. Pass to IOMMU_DESTORY
875+
* @virt_id: Virtual device ID per vIOMMU, e.g. vSID of ARM SMMUv3, vDeviceID
876+
* of AMD IOMMU, and vRID of a nested Intel VT-d to a Context Table
877+
*
878+
* Allocate a virtual device instance (for a physical device) against a vIOMMU.
879+
* This instance holds the device's information (related to its vIOMMU) in a VM.
880+
*/
881+
struct iommu_vdevice_alloc {
882+
__u32 size;
883+
__u32 viommu_id;
884+
__u32 dev_id;
885+
__u32 out_vdevice_id;
886+
__aligned_u64 virt_id;
887+
};
888+
#define IOMMU_VDEVICE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_ALLOC)
867889
#endif

0 commit comments

Comments
 (0)