History

vfio: Include No-IOMMU mode

There is really no way to safely give a user full access to a DMA capable device without an IOMMU to protect the host system. There is also no way to provide DMA translation, for use cases such as device assignment to virtual machines. However, there are still those users that want userspace drivers even under those conditions. The UIO driver exists for this use case, but does not provide the degree of device access and programming that VFIO has. In an effort to avoid code duplication, this introduces a No-IOMMU mode for VFIO. This mode requires building VFIO with CONFIG_VFIO_NOIOMMU and enabling the "enable_unsafe_noiommu_mode" option on the vfio driver. This should make it very clear that this mode is not safe. Additionally, CAP_SYS_RAWIO privileges are necessary to work with groups and containers using this mode. Groups making use of this support are named /dev/vfio/noiommu-$GROUP and can only make use of the special VFIO_NOIOMMU_IOMMU for the container. Use of this mode, specifically binding a device without a native IOMMU group to a VFIO bus driver will taint the kernel and should therefore not be considered supported. This patch includes no-iommu support for the vfio-pci bus driver only.

Alex Williamson committed 10 years ago

Commit 03a76b61 parent 77d6bd45 files · +210 −7Browse files

Unified · Split

5 files
  1. drivers/vfio/Kconfig +15 −0
  2. drivers/vfio/pci/vfio_pci.c +4 −4
  3. drivers/vfio/vfio.c +181 −3
  4. include/linux/vfio.h +3 −0
  5. include/uapi/linux/vfio.h +7 −0
drivers/vfio/Kconfig+15−0
@@ -31,6 +31,21 @@ menuconfig VFIO · 30 unmodified lines
3131
3232 If you don't know what to do here, say N.
3333
34+menuconfig VFIO_NOIOMMU
35+ bool "VFIO No-IOMMU support"
36+ depends on VFIO
37+ help
38+ VFIO is built on the ability to isolate devices using the IOMMU.
39+ Only with an IOMMU can userspace access to DMA capable devices be
40+ considered secure. VFIO No-IOMMU mode enables IOMMU groups for
41+ devices without IOMMU backing for the purpose of re-using the VFIO
42+ infrastructure in a non-secure mode. Use of this mode will result
43+ in an unsupportable kernel and will therefore taint the kernel.
44+ Device assignment to virtual machines is also not possible with
45+ this mode since there is no IOMMU to provide DMA translation.
46+
47+ If you don't know what to do here, say N.
48+
3449 source "drivers/vfio/pci/Kconfig"
3550 source "drivers/vfio/platform/Kconfig"
3651 source "virt/lib/Kconfig"
drivers/vfio/pci/vfio_pci.c+4−4
@@ -940,13 +940,13 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · 939 unmodified lines
940940 if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
941941 return -EINVAL;
942942
943− group = iommu_group_get(&pdev->dev);
943+ group = vfio_iommu_group_get(&pdev->dev);
944944 if (!group)
945945 return -EINVAL;
946946
947947 vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
948948 if (!vdev) {
949− iommu_group_put(group);
949+ vfio_iommu_group_put(group, &pdev->dev);
950950 return -ENOMEM;
951951 }
952952
@@ -957,7 +957,7 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · 4 unmodified lines
957957
958958 ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
959959 if (ret) {
960− iommu_group_put(group);
960+ vfio_iommu_group_put(group, &pdev->dev);
961961 kfree(vdev);
962962 return ret;
963963 }
@@ -993,7 +993,7 @@ static void vfio_pci_remove(struct pci_dev *pdev) · 29 unmodified lines
993993 if (!vdev)
994994 return;
995995
996− iommu_group_put(pdev->dev.iommu_group);
996+ vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
997997 kfree(vdev);
998998
999999 if (vfio_pci_is_vga(pdev)) {
drivers/vfio/vfio.c+181−3
@@ -62,6 +62,7 @@ struct vfio_container { · 61 unmodified lines
6262 struct rw_semaphore group_lock;
6363 struct vfio_iommu_driver *iommu_driver;
6464 void *iommu_data;
65+ bool noiommu;
6566 };
6667
6768 struct vfio_unbound_dev {
@@ -84,6 +85,7 @@ struct vfio_group { · 16 unmodified lines
8485 struct list_head unbound_list;
8586 struct mutex unbound_lock;
8687 atomic_t opened;
88+ bool noiommu;
8789 };
8890
8991 struct vfio_device {
@@ -95,6 +97,128 @@ struct vfio_device { · 5 unmodified lines
9597 void *device_data;
9698 };
9799
100+#ifdef CONFIG_VFIO_NOIOMMU
101+static bool noiommu __read_mostly;
102+module_param_named(enable_unsafe_noiommu_mode,
103+ noiommu, bool, S_IRUGO | S_IWUSR);
104+MODULE_PARM_DESC(enable_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)");
105+#endif
106+
107+/*
108+ * vfio_iommu_group_{get,put} are only intended for VFIO bus driver probe
109+ * and remove functions, any use cases other than acquiring the first
110+ * reference for the purpose of calling vfio_add_group_dev() or removing
111+ * that symmetric reference after vfio_del_group_dev() should use the raw
112+ * iommu_group_{get,put} functions. In particular, vfio_iommu_group_put()
113+ * removes the device from the dummy group and cannot be nested.
114+ */
115+struct iommu_group *vfio_iommu_group_get(struct device *dev)
116+{
117+ struct iommu_group *group;
118+ int __maybe_unused ret;
119+
120+ group = iommu_group_get(dev);
121+
122+#ifdef CONFIG_VFIO_NOIOMMU
123+ /*
124+ * With noiommu enabled, an IOMMU group will be created for a device
125+ * that doesn't already have one and doesn't have an iommu_ops on their
126+ * bus. We use iommu_present() again in the main code to detect these
127+ * fake groups.
128+ */
129+ if (group || !noiommu || iommu_present(dev->bus))
130+ return group;
131+
132+ group = iommu_group_alloc();
133+ if (IS_ERR(group))
134+ return NULL;
135+
136+ iommu_group_set_name(group, "vfio-noiommu");
137+ ret = iommu_group_add_device(group, dev);
138+ iommu_group_put(group);
139+ if (ret)
140+ return NULL;
141+
142+ /*
143+ * Where to taint? At this point we've added an IOMMU group for a
144+ * device that is not backed by iommu_ops, therefore any iommu_
145+ * callback using iommu_ops can legitimately Oops. So, while we may
146+ * be about to give a DMA capable device to a user without IOMMU
147+ * protection, which is clearly taint-worthy, let's go ahead and do
148+ * it here.
149+ */
150+ add_taint(TAINT_USER, LOCKDEP_STILL_OK);
151+ dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n");
152+#endif
153+
154+ return group;
155+}
156+EXPORT_SYMBOL_GPL(vfio_iommu_group_get);
157+
158+void vfio_iommu_group_put(struct iommu_group *group, struct device *dev)
159+{
160+#ifdef CONFIG_VFIO_NOIOMMU
161+ if (!iommu_present(dev->bus))
162+ iommu_group_remove_device(dev);
163+#endif
164+
165+ iommu_group_put(group);
166+}
167+EXPORT_SYMBOL_GPL(vfio_iommu_group_put);
168+
169+#ifdef CONFIG_VFIO_NOIOMMU
170+static void *vfio_noiommu_open(unsigned long arg)
171+{
172+ if (arg != VFIO_NOIOMMU_IOMMU)
173+ return ERR_PTR(-EINVAL);
174+ if (!capable(CAP_SYS_RAWIO))
175+ return ERR_PTR(-EPERM);
176+
177+ return NULL;
178+}
179+
180+static void vfio_noiommu_release(void *iommu_data)
181+{
182+}
183+
184+static long vfio_noiommu_ioctl(void *iommu_data,
185+ unsigned int cmd, unsigned long arg)
186+{
187+ if (cmd == VFIO_CHECK_EXTENSION)
188+ return noiommu && (arg == VFIO_NOIOMMU_IOMMU) ? 1 : 0;
189+
190+ return -ENOTTY;
191+}
192+
193+static int vfio_iommu_present(struct device *dev, void *unused)
194+{
195+ return iommu_present(dev->bus) ? 1 : 0;
196+}
197+
198+static int vfio_noiommu_attach_group(void *iommu_data,
199+ struct iommu_group *iommu_group)
200+{
201+ return iommu_group_for_each_dev(iommu_group, NULL,
202+ vfio_iommu_present) ? -EINVAL : 0;
203+}
204+
205+static void vfio_noiommu_detach_group(void *iommu_data,
206+ struct iommu_group *iommu_group)
207+{
208+}
209+
210+static const struct vfio_iommu_driver_ops vfio_noiommu_ops = {
211+ .name = "vfio-noiommu",
212+ .owner = THIS_MODULE,
213+ .open = vfio_noiommu_open,
214+ .release = vfio_noiommu_release,
215+ .ioctl = vfio_noiommu_ioctl,
216+ .attach_group = vfio_noiommu_attach_group,
217+ .detach_group = vfio_noiommu_detach_group,
218+};
219+#endif
220+
221+
98222 /**
99223 * IOMMU driver registration
100224 */
@@ -199,7 +323,8 @@ static void vfio_group_unlock_and_free(struct vfio_group *group) · 98 unmodified lines
199323 /**
200324 * Group objects - create, release, get, put, search
201325 */
202−static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
326+static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
327+ bool iommu_present)
203328 {
204329 struct vfio_group *group, *tmp;
205330 struct device *dev;
@@ -217,6 +342,7 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group) · 11 unmodified lines
217342 atomic_set(&group->container_users, 0);
218343 atomic_set(&group->opened, 0);
219344 group->iommu_group = iommu_group;
345+ group->noiommu = !iommu_present;
220346
221347 group->nb.notifier_call = vfio_iommu_group_notifier;
222348
@@ -252,7 +378,8 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group) · 29 unmodified lines
252378
253379 dev = device_create(vfio.class, NULL,
254380 MKDEV(MAJOR(vfio.group_devt), minor),
255− group, "%d", iommu_group_id(iommu_group));
381+ group, "%s%d", group->noiommu ? "noiommu-" : "",
382+ iommu_group_id(iommu_group));
256383 if (IS_ERR(dev)) {
257384 vfio_free_group_minor(minor);
258385 vfio_group_unlock_and_free(group);
@@ -640,7 +767,7 @@ int vfio_add_group_dev(struct device *dev, · 381 unmodified lines
640767
641768 group = vfio_group_get_from_iommu(iommu_group);
642769 if (!group) {
643− group = vfio_create_group(iommu_group);
770+ group = vfio_create_group(iommu_group, iommu_present(dev->bus));
644771 if (IS_ERR(group)) {
645772 iommu_group_put(iommu_group);
646773 return PTR_ERR(group);
@@ -854,6 +981,14 @@ static long vfio_ioctl_check_extension(struct vfio_container *container, · 207 unmodified lines
854981 mutex_lock(&vfio.iommu_drivers_lock);
855982 list_for_each_entry(driver, &vfio.iommu_drivers_list,
856983 vfio_next) {
984+
985+#ifdef CONFIG_VFIO_NOIOMMU
986+ if (!list_empty(&container->group_list) &&
987+ (container->noiommu !=
988+ (driver->ops == &vfio_noiommu_ops)))
989+ continue;
990+#endif
991+
857992 if (!try_module_get(driver->ops->owner))
858993 continue;
859994
@@ -925,6 +1060,15 @@ static long vfio_ioctl_set_iommu(struct vfio_container *container, · 65 unmodified lines
9251060 list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
9261061 void *data;
9271062
1063+#ifdef CONFIG_VFIO_NOIOMMU
1064+ /*
1065+ * Only noiommu containers can use vfio-noiommu and noiommu
1066+ * containers can only use vfio-noiommu.
1067+ */
1068+ if (container->noiommu != (driver->ops == &vfio_noiommu_ops))
1069+ continue;
1070+#endif
1071+
9281072 if (!try_module_get(driver->ops->owner))
9291073 continue;
9301074
@@ -1187,6 +1331,9 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd) · 256 unmodified lines
11871331 if (atomic_read(&group->container_users))
11881332 return -EINVAL;
11891333
1334+ if (group->noiommu && !capable(CAP_SYS_RAWIO))
1335+ return -EPERM;
1336+
11901337 f = fdget(container_fd);
11911338 if (!f.file)
11921339 return -EBADF;
@@ -1202,6 +1349,13 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd) · 9 unmodified lines
12021349
12031350 down_write(&container->group_lock);
12041351
1352+ /* Real groups and fake groups cannot mix */
1353+ if (!list_empty(&container->group_list) &&
1354+ container->noiommu != group->noiommu) {
1355+ ret = -EPERM;
1356+ goto unlock_out;
1357+ }
1358+
12051359 driver = container->iommu_driver;
12061360 if (driver) {
12071361 ret = driver->ops->attach_group(container->iommu_data,
@@ -1211,6 +1365,7 @@ static int vfio_group_set_container(struct vfio_group *group, int container_fd) · 3 unmodified lines
12111365 }
12121366
12131367 group->container = container;
1368+ container->noiommu = group->noiommu;
12141369 list_add(&group->container_next, &container->group_list);
12151370
12161371 /* Get a reference on the container and mark a user within the group */
@@ -1241,6 +1396,9 @@ static int vfio_group_get_device_fd(struct vfio_group *group, char *buf) · 24 unmodified lines
12411396 !group->container->iommu_driver || !vfio_group_viable(group))
12421397 return -EINVAL;
12431398
1399+ if (group->noiommu && !capable(CAP_SYS_RAWIO))
1400+ return -EPERM;
1401+
12441402 device = vfio_device_get_from_name(group, buf);
12451403 if (!device)
12461404 return -ENODEV;
@@ -1283,6 +1441,10 @@ static int vfio_group_get_device_fd(struct vfio_group *group, char *buf) · 36 unmodified lines
12831441
12841442 fd_install(ret, filep);
12851443
1444+ if (group->noiommu)
1445+ dev_warn(device->dev, "vfio-noiommu device opened by user "
1446+ "(%s:%d)\n", current->comm, task_pid_nr(current));
1447+
12861448 return ret;
12871449 }
12881450
@@ -1371,6 +1533,11 @@ static int vfio_group_fops_open(struct inode *inode, struct file *filep) · 82 unmodified lines
13711533 if (!group)
13721534 return -ENODEV;
13731535
1536+ if (group->noiommu && !capable(CAP_SYS_RAWIO)) {
1537+ vfio_group_put(group);
1538+ return -EPERM;
1539+ }
1540+
13741541 /* Do we need multiple instances of the group open? Seems not. */
13751542 opened = atomic_cmpxchg(&group->opened, 0, 1);
13761543 if (opened) {
@@ -1533,6 +1700,11 @@ struct vfio_group *vfio_group_get_external_user(struct file *filep) · 156 unmodified lines
15331700 if (!atomic_inc_not_zero(&group->container_users))
15341701 return ERR_PTR(-EINVAL);
15351702
1703+ if (group->noiommu) {
1704+ atomic_dec(&group->container_users);
1705+ return ERR_PTR(-EPERM);
1706+ }
1707+
15361708 if (!group->container->iommu_driver ||
15371709 !vfio_group_viable(group)) {
15381710 atomic_dec(&group->container_users);
@@ -1625,6 +1797,9 @@ static int __init vfio_init(void) · 86 unmodified lines
16251797 request_module_nowait("vfio_iommu_type1");
16261798 request_module_nowait("vfio_iommu_spapr_tce");
16271799
1800+#ifdef CONFIG_VFIO_NOIOMMU
1801+ vfio_register_iommu_driver(&vfio_noiommu_ops);
1802+#endif
16281803 return 0;
16291804
16301805 err_cdev_add:
@@ -1641,6 +1816,9 @@ static void __exit vfio_cleanup(void) · 10 unmodified lines
16411816 {
16421817 WARN_ON(!list_empty(&vfio.group_list));
16431818
1819+#ifdef CONFIG_VFIO_NOIOMMU
1820+ vfio_unregister_iommu_driver(&vfio_noiommu_ops);
1821+#endif
16441822 idr_destroy(&vfio.group_idr);
16451823 cdev_del(&vfio.group_cdev);
16461824 unregister_chrdev_region(vfio.group_devt, MINORMASK);
include/linux/vfio.h+3−0
@@ -44,6 +44,9 @@ struct vfio_device_ops { · 43 unmodified lines
4444 void (*request)(void *device_data, unsigned int count);
4545 };
4646
47+extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
48+extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
49+
4750 extern int vfio_add_group_dev(struct device *dev,
4851 const struct vfio_device_ops *ops,
4952 void *device_data);
include/uapi/linux/vfio.h+7−0
@@ -38,6 +38,13 @@ · 37 unmodified lines
3838
3939 #define VFIO_SPAPR_TCE_v2_IOMMU 7
4040
41+/*
42+ * The No-IOMMU IOMMU offers no translation or isolation for devices and
43+ * supports no ioctls outside of VFIO_CHECK_EXTENSION. Use of VFIO's No-IOMMU
44+ * code will taint the host kernel and should be used with extreme caution.
45+ */
46+#define VFIO_NOIOMMU_IOMMU 8
47+
4148 /*
4249 * The IOCTL interface is designed for extensibility by embedding the
4350 * structure length (argsz) and flags into structures passed between

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t