Skip to content

Commit 5fcc269

Browse files
yiliu1765awilliam
authored andcommitted
vfio: Add VFIO_DEVICE_BIND_IOMMUFD
This adds ioctl for userspace to bind device cdev fd to iommufd. VFIO_DEVICE_BIND_IOMMUFD: bind device to an iommufd, hence gain DMA control provided by the iommufd. open_device op is called after bind_iommufd op. Tested-by: Nicolin Chen <[email protected]> Tested-by: Matthew Rosato <[email protected]> Tested-by: Yanting Jiang <[email protected]> Tested-by: Shameer Kolothum <[email protected]> Tested-by: Terrence Xu <[email protected]> Tested-by: Zhenzhong Duan <[email protected]> Signed-off-by: Yi Liu <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent ca9e45b commit 5fcc269

File tree

5 files changed

+155
-2
lines changed

5 files changed

+155
-2
lines changed

drivers/vfio/device_cdev.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2023 Intel Corporation.
44
*/
55
#include <linux/vfio.h>
6+
#include <linux/iommufd.h>
67

78
#include "vfio.h"
89

@@ -45,6 +46,112 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
4546
return ret;
4647
}
4748

49+
static void vfio_df_get_kvm_safe(struct vfio_device_file *df)
50+
{
51+
spin_lock(&df->kvm_ref_lock);
52+
vfio_device_get_kvm_safe(df->device, df->kvm);
53+
spin_unlock(&df->kvm_ref_lock);
54+
}
55+
56+
long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
57+
struct vfio_device_bind_iommufd __user *arg)
58+
{
59+
struct vfio_device *device = df->device;
60+
struct vfio_device_bind_iommufd bind;
61+
unsigned long minsz;
62+
int ret;
63+
64+
static_assert(__same_type(arg->out_devid, df->devid));
65+
66+
minsz = offsetofend(struct vfio_device_bind_iommufd, out_devid);
67+
68+
if (copy_from_user(&bind, arg, minsz))
69+
return -EFAULT;
70+
71+
if (bind.argsz < minsz || bind.flags || bind.iommufd < 0)
72+
return -EINVAL;
73+
74+
/* BIND_IOMMUFD only allowed for cdev fds */
75+
if (df->group)
76+
return -EINVAL;
77+
78+
ret = vfio_device_block_group(device);
79+
if (ret)
80+
return ret;
81+
82+
mutex_lock(&device->dev_set->lock);
83+
/* one device cannot be bound twice */
84+
if (df->access_granted) {
85+
ret = -EINVAL;
86+
goto out_unlock;
87+
}
88+
89+
df->iommufd = iommufd_ctx_from_fd(bind.iommufd);
90+
if (IS_ERR(df->iommufd)) {
91+
ret = PTR_ERR(df->iommufd);
92+
df->iommufd = NULL;
93+
goto out_unlock;
94+
}
95+
96+
/*
97+
* Before the device open, get the KVM pointer currently
98+
* associated with the device file (if there is) and obtain
99+
* a reference. This reference is held until device closed.
100+
* Save the pointer in the device for use by drivers.
101+
*/
102+
vfio_df_get_kvm_safe(df);
103+
104+
ret = vfio_df_open(df);
105+
if (ret)
106+
goto out_put_kvm;
107+
108+
ret = copy_to_user(&arg->out_devid, &df->devid,
109+
sizeof(df->devid)) ? -EFAULT : 0;
110+
if (ret)
111+
goto out_close_device;
112+
113+
device->cdev_opened = true;
114+
/*
115+
* Paired with smp_load_acquire() in vfio_device_fops::ioctl/
116+
* read/write/mmap
117+
*/
118+
smp_store_release(&df->access_granted, true);
119+
mutex_unlock(&device->dev_set->lock);
120+
return 0;
121+
122+
out_close_device:
123+
vfio_df_close(df);
124+
out_put_kvm:
125+
vfio_device_put_kvm(device);
126+
iommufd_ctx_put(df->iommufd);
127+
df->iommufd = NULL;
128+
out_unlock:
129+
mutex_unlock(&device->dev_set->lock);
130+
vfio_device_unblock_group(device);
131+
return ret;
132+
}
133+
134+
void vfio_df_unbind_iommufd(struct vfio_device_file *df)
135+
{
136+
struct vfio_device *device = df->device;
137+
138+
/*
139+
* In the time of close, there is no contention with another one
140+
* changing this flag. So read df->access_granted without lock
141+
* and no smp_load_acquire() is ok.
142+
*/
143+
if (!df->access_granted)
144+
return;
145+
146+
mutex_lock(&device->dev_set->lock);
147+
vfio_df_close(df);
148+
vfio_device_put_kvm(device);
149+
iommufd_ctx_put(df->iommufd);
150+
device->cdev_opened = false;
151+
mutex_unlock(&device->dev_set->lock);
152+
vfio_device_unblock_group(device);
153+
}
154+
48155
static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
49156
{
50157
return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));

drivers/vfio/vfio.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ static inline void vfio_device_del(struct vfio_device *device)
287287
}
288288

289289
int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep);
290+
long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
291+
struct vfio_device_bind_iommufd __user *arg);
292+
void vfio_df_unbind_iommufd(struct vfio_device_file *df);
290293
int vfio_cdev_init(struct class *device_class);
291294
void vfio_cdev_cleanup(void);
292295
#else
@@ -310,6 +313,16 @@ static inline int vfio_device_fops_cdev_open(struct inode *inode,
310313
return 0;
311314
}
312315

316+
static inline long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
317+
struct vfio_device_bind_iommufd __user *arg)
318+
{
319+
return -ENOTTY;
320+
}
321+
322+
static inline void vfio_df_unbind_iommufd(struct vfio_device_file *df)
323+
{
324+
}
325+
313326
static inline int vfio_cdev_init(struct class *device_class)
314327
{
315328
return 0;

drivers/vfio/vfio_main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
575575

576576
if (df->group)
577577
vfio_df_group_close(df);
578+
else
579+
vfio_df_unbind_iommufd(df);
578580

579581
vfio_device_put_registration(device);
580582

@@ -1149,6 +1151,9 @@ static long vfio_device_fops_unl_ioctl(struct file *filep,
11491151
void __user *uptr = (void __user *)arg;
11501152
int ret;
11511153

1154+
if (cmd == VFIO_DEVICE_BIND_IOMMUFD)
1155+
return vfio_df_ioctl_bind_iommufd(df, uptr);
1156+
11521157
/* Paired with smp_store_release() following vfio_df_open() */
11531158
if (!smp_load_acquire(&df->access_granted))
11541159
return -EINVAL;

include/linux/vfio.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ struct vfio_device {
6464
void (*put_kvm)(struct kvm *kvm);
6565
#if IS_ENABLED(CONFIG_IOMMUFD)
6666
struct iommufd_device *iommufd_device;
67-
bool iommufd_attached;
67+
u8 iommufd_attached:1;
6868
#endif
69+
u8 cdev_opened:1;
6970
};
7071

7172
/**
@@ -168,7 +169,7 @@ vfio_iommufd_get_dev_id(struct vfio_device *vdev, struct iommufd_ctx *ictx)
168169

169170
static inline bool vfio_device_cdev_opened(struct vfio_device *device)
170171
{
171-
return false;
172+
return device->cdev_opened;
172173
}
173174

174175
/**

include/uapi/linux/vfio.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,33 @@ struct vfio_device_feature {
897897

898898
#define VFIO_DEVICE_FEATURE _IO(VFIO_TYPE, VFIO_BASE + 17)
899899

900+
/*
901+
* VFIO_DEVICE_BIND_IOMMUFD - _IOR(VFIO_TYPE, VFIO_BASE + 18,
902+
* struct vfio_device_bind_iommufd)
903+
* @argsz: User filled size of this data.
904+
* @flags: Must be 0.
905+
* @iommufd: iommufd to bind.
906+
* @out_devid: The device id generated by this bind. devid is a handle for
907+
* this device/iommufd bond and can be used in IOMMUFD commands.
908+
*
909+
* Bind a vfio_device to the specified iommufd.
910+
*
911+
* User is restricted from accessing the device before the binding operation
912+
* is completed. Only allowed on cdev fds.
913+
*
914+
* Unbind is automatically conducted when device fd is closed.
915+
*
916+
* Return: 0 on success, -errno on failure.
917+
*/
918+
struct vfio_device_bind_iommufd {
919+
__u32 argsz;
920+
__u32 flags;
921+
__s32 iommufd;
922+
__u32 out_devid;
923+
};
924+
925+
#define VFIO_DEVICE_BIND_IOMMUFD _IO(VFIO_TYPE, VFIO_BASE + 18)
926+
900927
/*
901928
* Provide support for setting a PCI VF Token, which is used as a shared
902929
* secret between PF and VF drivers. This feature may only be set on a

0 commit comments

Comments
 (0)