vfio/ccw: replace vfio_init_device with _alloc_
[linux-block.git] / drivers / vfio / vfio_main.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
cba3345c
AW
2/*
3 * VFIO core
4 *
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
7 *
cba3345c
AW
8 * Derived from original vfio:
9 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
10 * Author: Tom Lyon, pugs@cisco.com
11 */
12
13#include <linux/cdev.h>
14#include <linux/compat.h>
15#include <linux/device.h>
16#include <linux/file.h>
17#include <linux/anon_inodes.h>
18#include <linux/fs.h>
19#include <linux/idr.h>
20#include <linux/iommu.h>
21#include <linux/list.h>
d1099901 22#include <linux/miscdevice.h>
cba3345c
AW
23#include <linux/module.h>
24#include <linux/mutex.h>
5f096b14 25#include <linux/pci.h>
9587f44a 26#include <linux/rwsem.h>
cba3345c
AW
27#include <linux/sched.h>
28#include <linux/slab.h>
664e9386 29#include <linux/stat.h>
cba3345c
AW
30#include <linux/string.h>
31#include <linux/uaccess.h>
32#include <linux/vfio.h>
33#include <linux/wait.h>
41be3e26 34#include <linux/sched/signal.h>
8e5c6995 35#include <linux/pm_runtime.h>
80c4b92a
YH
36#include <linux/interval_tree.h>
37#include <linux/iova_bitmap.h>
8cc02d22 38#include "vfio.h"
cba3345c
AW
39
40#define DRIVER_VERSION "0.3"
41#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
42#define DRIVER_DESC "VFIO - User Level meta-driver"
43
44static struct vfio {
45 struct class *class;
cba3345c 46 struct list_head group_list;
9cef7391
JG
47 struct mutex group_lock; /* locks group_list */
48 struct ida group_ida;
d1099901 49 dev_t group_devt;
3c28a761
YL
50 struct class *device_class;
51 struct ida device_ida;
cba3345c
AW
52} vfio;
53
2fd585f4 54static DEFINE_XARRAY(vfio_device_set_xa);
9cef7391 55static const struct file_operations vfio_group_fops;
2fd585f4
JG
56
57int vfio_assign_device_set(struct vfio_device *device, void *set_id)
58{
59 unsigned long idx = (unsigned long)set_id;
60 struct vfio_device_set *new_dev_set;
61 struct vfio_device_set *dev_set;
62
63 if (WARN_ON(!set_id))
64 return -EINVAL;
65
66 /*
67 * Atomically acquire a singleton object in the xarray for this set_id
68 */
69 xa_lock(&vfio_device_set_xa);
70 dev_set = xa_load(&vfio_device_set_xa, idx);
71 if (dev_set)
72 goto found_get_ref;
73 xa_unlock(&vfio_device_set_xa);
74
75 new_dev_set = kzalloc(sizeof(*new_dev_set), GFP_KERNEL);
76 if (!new_dev_set)
77 return -ENOMEM;
78 mutex_init(&new_dev_set->lock);
79 INIT_LIST_HEAD(&new_dev_set->device_list);
80 new_dev_set->set_id = set_id;
81
82 xa_lock(&vfio_device_set_xa);
83 dev_set = __xa_cmpxchg(&vfio_device_set_xa, idx, NULL, new_dev_set,
84 GFP_KERNEL);
85 if (!dev_set) {
86 dev_set = new_dev_set;
87 goto found_get_ref;
88 }
89
90 kfree(new_dev_set);
91 if (xa_is_err(dev_set)) {
92 xa_unlock(&vfio_device_set_xa);
93 return xa_err(dev_set);
94 }
95
96found_get_ref:
97 dev_set->device_count++;
98 xa_unlock(&vfio_device_set_xa);
99 mutex_lock(&dev_set->lock);
100 device->dev_set = dev_set;
101 list_add_tail(&device->dev_set_list, &dev_set->device_list);
102 mutex_unlock(&dev_set->lock);
103 return 0;
104}
105EXPORT_SYMBOL_GPL(vfio_assign_device_set);
106
107static void vfio_release_device_set(struct vfio_device *device)
108{
109 struct vfio_device_set *dev_set = device->dev_set;
110
111 if (!dev_set)
112 return;
113
114 mutex_lock(&dev_set->lock);
115 list_del(&device->dev_set_list);
116 mutex_unlock(&dev_set->lock);
117
118 xa_lock(&vfio_device_set_xa);
119 if (!--dev_set->device_count) {
120 __xa_erase(&vfio_device_set_xa,
121 (unsigned long)dev_set->set_id);
122 mutex_destroy(&dev_set->lock);
123 kfree(dev_set);
124 }
125 xa_unlock(&vfio_device_set_xa);
126}
127
3b9a2d57 128/*
cba3345c
AW
129 * Group objects - create, release, get, put, search
130 */
1ceabade
JG
131static struct vfio_group *
132__vfio_group_get_from_iommu(struct iommu_group *iommu_group)
133{
134 struct vfio_group *group;
135
3dd59a7d
JG
136 /*
137 * group->iommu_group from the vfio.group_list cannot be NULL
138 * under the vfio.group_lock.
139 */
1ceabade
JG
140 list_for_each_entry(group, &vfio.group_list, vfio_next) {
141 if (group->iommu_group == iommu_group) {
ca5f21b2 142 refcount_inc(&group->drivers);
1ceabade
JG
143 return group;
144 }
145 }
146 return NULL;
147}
148
149static struct vfio_group *
150vfio_group_get_from_iommu(struct iommu_group *iommu_group)
151{
152 struct vfio_group *group;
153
154 mutex_lock(&vfio.group_lock);
155 group = __vfio_group_get_from_iommu(iommu_group);
156 mutex_unlock(&vfio.group_lock);
157 return group;
158}
159
9cef7391 160static void vfio_group_release(struct device *dev)
cba3345c 161{
9cef7391 162 struct vfio_group *group = container_of(dev, struct vfio_group, dev);
9cef7391
JG
163
164 mutex_destroy(&group->device_lock);
c82e81ab 165 mutex_destroy(&group->group_lock);
3dd59a7d 166 WARN_ON(group->iommu_group);
9cef7391
JG
167 ida_free(&vfio.group_ida, MINOR(group->dev.devt));
168 kfree(group);
169}
170
171static struct vfio_group *vfio_group_alloc(struct iommu_group *iommu_group,
172 enum vfio_group_type type)
173{
174 struct vfio_group *group;
175 int minor;
cba3345c
AW
176
177 group = kzalloc(sizeof(*group), GFP_KERNEL);
178 if (!group)
179 return ERR_PTR(-ENOMEM);
180
9cef7391
JG
181 minor = ida_alloc_max(&vfio.group_ida, MINORMASK, GFP_KERNEL);
182 if (minor < 0) {
183 kfree(group);
184 return ERR_PTR(minor);
185 }
186
187 device_initialize(&group->dev);
188 group->dev.devt = MKDEV(MAJOR(vfio.group_devt), minor);
189 group->dev.class = vfio.class;
190 group->dev.release = vfio_group_release;
191 cdev_init(&group->cdev, &vfio_group_fops);
192 group->cdev.owner = THIS_MODULE;
193
ca5f21b2 194 refcount_set(&group->drivers, 1);
c82e81ab 195 mutex_init(&group->group_lock);
cba3345c
AW
196 INIT_LIST_HEAD(&group->device_list);
197 mutex_init(&group->device_lock);
cba3345c 198 group->iommu_group = iommu_group;
9cef7391 199 /* put in vfio_group_release() */
325a31c9 200 iommu_group_ref_get(iommu_group);
c68ea0d0 201 group->type = type;
ccd46dba 202 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
cba3345c 203
9cef7391
JG
204 return group;
205}
206
207static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
208 enum vfio_group_type type)
209{
210 struct vfio_group *group;
211 struct vfio_group *ret;
212 int err;
213
214 group = vfio_group_alloc(iommu_group, type);
215 if (IS_ERR(group))
216 return group;
217
218 err = dev_set_name(&group->dev, "%s%d",
219 group->type == VFIO_NO_IOMMU ? "noiommu-" : "",
220 iommu_group_id(iommu_group));
221 if (err) {
222 ret = ERR_PTR(err);
223 goto err_put;
224 }
225
cba3345c
AW
226 mutex_lock(&vfio.group_lock);
227
cba3345c 228 /* Did we race creating this group? */
9cef7391
JG
229 ret = __vfio_group_get_from_iommu(iommu_group);
230 if (ret)
231 goto err_unlock;
2f51bf4b 232
9cef7391
JG
233 err = cdev_device_add(&group->cdev, &group->dev);
234 if (err) {
235 ret = ERR_PTR(err);
236 goto err_unlock;
cba3345c
AW
237 }
238
cba3345c
AW
239 list_add(&group->vfio_next, &vfio.group_list);
240
241 mutex_unlock(&vfio.group_lock);
cba3345c 242 return group;
9cef7391
JG
243
244err_unlock:
245 mutex_unlock(&vfio.group_lock);
9cef7391
JG
246err_put:
247 put_device(&group->dev);
248 return ret;
cba3345c
AW
249}
250
ca5f21b2
JG
251static void vfio_device_remove_group(struct vfio_device *device)
252{
253 struct vfio_group *group = device->group;
3dd59a7d 254 struct iommu_group *iommu_group;
ca5f21b2
JG
255
256 if (group->type == VFIO_NO_IOMMU || group->type == VFIO_EMULATED_IOMMU)
257 iommu_group_remove_device(device->dev);
258
259 /* Pairs with vfio_create_group() / vfio_group_get_from_iommu() */
260 if (!refcount_dec_and_mutex_lock(&group->drivers, &vfio.group_lock))
2b678aa2 261 return;
ca5f21b2
JG
262 list_del(&group->vfio_next);
263
264 /*
265 * We could concurrently probe another driver in the group that might
266 * race vfio_device_remove_group() with vfio_get_group(), so we have to
267 * ensure that the sysfs is all cleaned up under lock otherwise the
268 * cdev_device_add() will fail due to the name aready existing.
269 */
270 cdev_device_del(&group->cdev, &group->dev);
ca5f21b2 271
3dd59a7d 272 mutex_lock(&group->group_lock);
63b150fd
JG
273 /*
274 * These data structures all have paired operations that can only be
3dd59a7d
JG
275 * undone when the caller holds a live reference on the device. Since
276 * all pairs must be undone these WARN_ON's indicate some caller did not
63b150fd
JG
277 * properly hold the group reference.
278 */
cba3345c 279 WARN_ON(!list_empty(&group->device_list));
65b1adeb 280 WARN_ON(group->notifier.head);
3dd59a7d
JG
281
282 /*
283 * Revoke all users of group->iommu_group. At this point we know there
284 * are no devices active because we are unplugging the last one. Setting
285 * iommu_group to NULL blocks all new users.
286 */
287 if (group->container)
288 vfio_group_detach_container(group);
289 iommu_group = group->iommu_group;
ca5f21b2 290 group->iommu_group = NULL;
3dd59a7d
JG
291 mutex_unlock(&group->group_lock);
292 mutex_unlock(&vfio.group_lock);
9cef7391 293
3dd59a7d 294 iommu_group_put(iommu_group);
9cef7391 295 put_device(&group->dev);
cba3345c
AW
296}
297
3b9a2d57 298/*
cba3345c
AW
299 * Device objects - create, release, get, put, search
300 */
cba3345c 301/* Device reference always implies a group reference */
4a725b8d 302static void vfio_device_put_registration(struct vfio_device *device)
cba3345c 303{
5e42c999
JG
304 if (refcount_dec_and_test(&device->refcount))
305 complete(&device->comp);
cba3345c
AW
306}
307
4a725b8d 308static bool vfio_device_try_get_registration(struct vfio_device *device)
cba3345c 309{
5e42c999 310 return refcount_inc_not_zero(&device->refcount);
cba3345c
AW
311}
312
313static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
314 struct device *dev)
315{
316 struct vfio_device *device;
317
318 mutex_lock(&group->device_lock);
319 list_for_each_entry(device, &group->device_list, group_next) {
4a725b8d
KT
320 if (device->dev == dev &&
321 vfio_device_try_get_registration(device)) {
cba3345c
AW
322 mutex_unlock(&group->device_lock);
323 return device;
324 }
325 }
326 mutex_unlock(&group->device_lock);
327 return NULL;
328}
329
3b9a2d57 330/*
cba3345c
AW
331 * VFIO driver API
332 */
cb9ff3f3 333/* Release helper called by vfio_put_device() */
3c28a761 334static void vfio_device_release(struct device *dev)
cb9ff3f3
KT
335{
336 struct vfio_device *device =
3c28a761 337 container_of(dev, struct vfio_device, device);
cb9ff3f3 338
ebb72b76 339 vfio_release_device_set(device);
3c28a761 340 ida_free(&vfio.device_ida, device->index);
cb9ff3f3
KT
341
342 /*
343 * kvfree() cannot be done here due to a life cycle mess in
344 * vfio-ccw. Before the ccw part is fixed all drivers are
345 * required to support @release and call vfio_free_device()
346 * from there.
347 */
348 device->ops->release(device);
349}
cb9ff3f3 350
d1104f93
EF
351static int vfio_init_device(struct vfio_device *device, struct device *dev,
352 const struct vfio_device_ops *ops);
353
cb9ff3f3
KT
354/*
355 * Allocate and initialize vfio_device so it can be registered to vfio
356 * core.
357 *
358 * Drivers should use the wrapper vfio_alloc_device() for allocation.
359 * @size is the size of the structure to be allocated, including any
360 * private data used by the driver.
361 *
362 * Driver may provide an @init callback to cover device private data.
363 *
364 * Use vfio_put_device() to release the structure after success return.
365 */
366struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev,
367 const struct vfio_device_ops *ops)
368{
369 struct vfio_device *device;
370 int ret;
371
372 if (WARN_ON(size < sizeof(struct vfio_device)))
373 return ERR_PTR(-EINVAL);
374
375 device = kvzalloc(size, GFP_KERNEL);
376 if (!device)
377 return ERR_PTR(-ENOMEM);
378
379 ret = vfio_init_device(device, dev, ops);
380 if (ret)
381 goto out_free;
382 return device;
383
384out_free:
385 kvfree(device);
386 return ERR_PTR(ret);
387}
388EXPORT_SYMBOL_GPL(_vfio_alloc_device);
389
390/*
391 * Initialize a vfio_device so it can be registered to vfio core.
cb9ff3f3 392 */
d1104f93
EF
393static int vfio_init_device(struct vfio_device *device, struct device *dev,
394 const struct vfio_device_ops *ops)
cb9ff3f3
KT
395{
396 int ret;
397
3c28a761
YL
398 ret = ida_alloc_max(&vfio.device_ida, MINORMASK, GFP_KERNEL);
399 if (ret < 0) {
400 dev_dbg(dev, "Error to alloc index\n");
401 return ret;
402 }
403
404 device->index = ret;
ebb72b76
KT
405 init_completion(&device->comp);
406 device->dev = dev;
407 device->ops = ops;
cb9ff3f3
KT
408
409 if (ops->init) {
410 ret = ops->init(device);
411 if (ret)
412 goto out_uninit;
413 }
414
3c28a761
YL
415 device_initialize(&device->device);
416 device->device.release = vfio_device_release;
417 device->device.class = vfio.device_class;
418 device->device.parent = device->dev;
cb9ff3f3
KT
419 return 0;
420
421out_uninit:
ebb72b76 422 vfio_release_device_set(device);
3c28a761 423 ida_free(&vfio.device_ida, device->index);
cb9ff3f3
KT
424 return ret;
425}
cb9ff3f3
KT
426
427/*
428 * The helper called by driver @release callback to free the device
429 * structure. Drivers which don't have private data to clean can
430 * simply use this helper as its @release.
431 */
432void vfio_free_device(struct vfio_device *device)
433{
434 kvfree(device);
435}
436EXPORT_SYMBOL_GPL(vfio_free_device);
437
c68ea0d0
CH
438static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,
439 enum vfio_group_type type)
1362591f
CH
440{
441 struct iommu_group *iommu_group;
442 struct vfio_group *group;
3af91771
CH
443 int ret;
444
445 iommu_group = iommu_group_alloc();
446 if (IS_ERR(iommu_group))
447 return ERR_CAST(iommu_group);
448
1c61d51e
LN
449 ret = iommu_group_set_name(iommu_group, "vfio-noiommu");
450 if (ret)
451 goto out_put_group;
3af91771
CH
452 ret = iommu_group_add_device(iommu_group, dev);
453 if (ret)
454 goto out_put_group;
1362591f 455
c68ea0d0 456 group = vfio_create_group(iommu_group, type);
3af91771
CH
457 if (IS_ERR(group)) {
458 ret = PTR_ERR(group);
459 goto out_remove_device;
460 }
325a31c9 461 iommu_group_put(iommu_group);
3af91771
CH
462 return group;
463
464out_remove_device:
465 iommu_group_remove_device(dev);
466out_put_group:
467 iommu_group_put(iommu_group);
468 return ERR_PTR(ret);
469}
3af91771
CH
470
471static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
472{
473 struct iommu_group *iommu_group;
474 struct vfio_group *group;
475
476 iommu_group = iommu_group_get(dev);
444d43ec 477 if (!iommu_group && vfio_noiommu) {
3af91771
CH
478 /*
479 * With noiommu enabled, create an IOMMU group for devices that
a77109ff
RM
480 * don't already have one, implying no IOMMU hardware/driver
481 * exists. Taint the kernel because we're about to give a DMA
3af91771
CH
482 * capable device to a user without IOMMU protection.
483 */
c68ea0d0 484 group = vfio_noiommu_group_alloc(dev, VFIO_NO_IOMMU);
3af91771
CH
485 if (!IS_ERR(group)) {
486 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
487 dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n");
488 }
489 return group;
490 }
444d43ec 491
1362591f
CH
492 if (!iommu_group)
493 return ERR_PTR(-EINVAL);
494
afe4e376
JG
495 /*
496 * VFIO always sets IOMMU_CACHE because we offer no way for userspace to
497 * restore cache coherency. It has to be checked here because it is only
498 * valid for cases where we are using iommu groups.
499 */
a9cf69d0 500 if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) {
afe4e376
JG
501 iommu_group_put(iommu_group);
502 return ERR_PTR(-EINVAL);
503 }
504
1362591f 505 group = vfio_group_get_from_iommu(iommu_group);
325a31c9
JG
506 if (!group)
507 group = vfio_create_group(iommu_group, VFIO_IOMMU);
1362591f 508
325a31c9 509 /* The vfio_group holds a reference to the iommu_group */
1362591f
CH
510 iommu_group_put(iommu_group);
511 return group;
512}
513
c68ea0d0
CH
514static int __vfio_register_dev(struct vfio_device *device,
515 struct vfio_group *group)
0bfc6a4e
JG
516{
517 struct vfio_device *existing_device;
3c28a761 518 int ret;
c68ea0d0 519
ca5f21b2
JG
520 /*
521 * In all cases group is the output of one of the group allocation
522 * functions and we have group->drivers incremented for us.
523 */
c68ea0d0
CH
524 if (IS_ERR(group))
525 return PTR_ERR(group);
cba3345c 526
2fd585f4
JG
527 /*
528 * If the driver doesn't specify a set then the device is added to a
529 * singleton set just for itself.
530 */
531 if (!device->dev_set)
532 vfio_assign_device_set(device, device);
533
0bfc6a4e
JG
534 existing_device = vfio_group_get_device(group, device->dev);
535 if (existing_device) {
3dd59a7d
JG
536 /*
537 * group->iommu_group is non-NULL because we hold the drivers
538 * refcount.
539 */
0bfc6a4e 540 dev_WARN(device->dev, "Device already exists on group %d\n",
1362591f 541 iommu_group_id(group->iommu_group));
4a725b8d 542 vfio_device_put_registration(existing_device);
3c28a761
YL
543 ret = -EBUSY;
544 goto err_out;
cba3345c
AW
545 }
546
0bfc6a4e
JG
547 /* Our reference on group is moved to the device */
548 device->group = group;
549
3c28a761
YL
550 ret = dev_set_name(&device->device, "vfio%d", device->index);
551 if (ret)
552 goto err_out;
553
554 ret = device_add(&device->device);
555 if (ret)
556 goto err_out;
557
0bfc6a4e
JG
558 /* Refcounting can't start until the driver calls register */
559 refcount_set(&device->refcount, 1);
560
561 mutex_lock(&group->device_lock);
562 list_add(&device->group_next, &group->device_list);
0bfc6a4e
JG
563 mutex_unlock(&group->device_lock);
564
565 return 0;
3c28a761 566err_out:
ca5f21b2 567 vfio_device_remove_group(device);
3c28a761 568 return ret;
0bfc6a4e 569}
c68ea0d0
CH
570
571int vfio_register_group_dev(struct vfio_device *device)
572{
573 return __vfio_register_dev(device,
574 vfio_group_find_or_alloc(device->dev));
575}
0bfc6a4e
JG
576EXPORT_SYMBOL_GPL(vfio_register_group_dev);
577
c68ea0d0
CH
578/*
579 * Register a virtual device without IOMMU backing. The user of this
580 * device must not be able to directly trigger unmediated DMA.
581 */
582int vfio_register_emulated_iommu_dev(struct vfio_device *device)
583{
584 return __vfio_register_dev(device,
585 vfio_noiommu_group_alloc(device->dev, VFIO_EMULATED_IOMMU));
586}
587EXPORT_SYMBOL_GPL(vfio_register_emulated_iommu_dev);
588
4bc94d5d
AW
589static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
590 char *buf)
591{
5f3874c2 592 struct vfio_device *it, *device = ERR_PTR(-ENODEV);
4bc94d5d
AW
593
594 mutex_lock(&group->device_lock);
e324fc82 595 list_for_each_entry(it, &group->device_list, group_next) {
5f3874c2
AW
596 int ret;
597
598 if (it->ops->match) {
6df62c5b 599 ret = it->ops->match(it, buf);
5f3874c2
AW
600 if (ret < 0) {
601 device = ERR_PTR(ret);
602 break;
603 }
604 } else {
605 ret = !strcmp(dev_name(it->dev), buf);
606 }
607
4a725b8d 608 if (ret && vfio_device_try_get_registration(it)) {
e324fc82 609 device = it;
4bc94d5d
AW
610 break;
611 }
612 }
613 mutex_unlock(&group->device_lock);
614
615 return device;
616}
617
cba3345c
AW
618/*
619 * Decrement the device reference count and wait for the device to be
620 * removed. Open file descriptors for the device... */
0bfc6a4e 621void vfio_unregister_group_dev(struct vfio_device *device)
cba3345c 622{
cba3345c 623 struct vfio_group *group = device->group;
13060b64 624 unsigned int i = 0;
db7d4d7f 625 bool interrupted = false;
5e42c999 626 long rc;
cba3345c 627
4a725b8d 628 vfio_device_put_registration(device);
5e42c999
JG
629 rc = try_wait_for_completion(&device->comp);
630 while (rc <= 0) {
13060b64 631 if (device->ops->request)
6df62c5b 632 device->ops->request(device, i++);
13060b64 633
db7d4d7f 634 if (interrupted) {
5e42c999
JG
635 rc = wait_for_completion_timeout(&device->comp,
636 HZ * 10);
db7d4d7f 637 } else {
5e42c999
JG
638 rc = wait_for_completion_interruptible_timeout(
639 &device->comp, HZ * 10);
640 if (rc < 0) {
db7d4d7f 641 interrupted = true;
0bfc6a4e 642 dev_warn(device->dev,
db7d4d7f
AW
643 "Device is currently in use, task"
644 " \"%s\" (%d) "
645 "blocked until device is released",
646 current->comm, task_pid_nr(current));
647 }
648 }
5e42c999 649 }
e014e944 650
5e42c999
JG
651 mutex_lock(&group->device_lock);
652 list_del(&device->group_next);
5e42c999 653 mutex_unlock(&group->device_lock);
41be3e26 654
3c28a761
YL
655 /* Balances device_add in register path */
656 device_del(&device->device);
657
ca5f21b2 658 vfio_device_remove_group(device);
0bfc6a4e
JG
659}
660EXPORT_SYMBOL_GPL(vfio_unregister_group_dev);
661
3b9a2d57 662/*
cba3345c
AW
663 * VFIO Group fd, /dev/vfio/$GROUP
664 */
cba3345c
AW
665/*
666 * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
667 * if there was no container to unset. Since the ioctl is called on
668 * the group, we know that still exists, therefore the only valid
669 * transition here is 1->0.
670 */
b3b43590 671static int vfio_group_ioctl_unset_container(struct vfio_group *group)
cba3345c 672{
b3b43590 673 int ret = 0;
cba3345c 674
c82e81ab 675 mutex_lock(&group->group_lock);
b3b43590
JG
676 if (!group->container) {
677 ret = -EINVAL;
678 goto out_unlock;
679 }
680 if (group->container_users != 1) {
681 ret = -EBUSY;
682 goto out_unlock;
683 }
429a781c 684 vfio_group_detach_container(group);
b3b43590
JG
685
686out_unlock:
c82e81ab 687 mutex_unlock(&group->group_lock);
b3b43590 688 return ret;
cba3345c
AW
689}
690
03e650f6
JG
691static int vfio_group_ioctl_set_container(struct vfio_group *group,
692 int __user *arg)
693{
694 struct vfio_container *container;
695 struct fd f;
696 int ret;
697 int fd;
698
699 if (get_user(fd, arg))
700 return -EFAULT;
701
702 f = fdget(fd);
703 if (!f.file)
704 return -EBADF;
705
c82e81ab 706 mutex_lock(&group->group_lock);
03e650f6
JG
707 if (group->container || WARN_ON(group->container_users)) {
708 ret = -EINVAL;
709 goto out_unlock;
710 }
3dd59a7d
JG
711 if (!group->iommu_group) {
712 ret = -ENODEV;
713 goto out_unlock;
714 }
715
03e650f6
JG
716 container = vfio_container_from_file(f.file);
717 ret = -EINVAL;
718 if (container) {
719 ret = vfio_container_attach_group(container, group);
720 goto out_unlock;
721 }
722
723out_unlock:
c82e81ab 724 mutex_unlock(&group->group_lock);
2903ff01 725 fdput(f);
cba3345c
AW
726 return ret;
727}
728
cba3345c
AW
729static const struct file_operations vfio_device_fops;
730
eadd86f8 731/* true if the vfio_device has open_device() called but not close_device() */
cdc71fe4 732bool vfio_assert_device_open(struct vfio_device *device)
32f55d83 733{
eadd86f8
JG
734 return !WARN_ON_ONCE(!READ_ONCE(device->open_count));
735}
736
805bb6c1 737static struct file *vfio_device_open(struct vfio_device *device)
cba3345c 738{
cba3345c 739 struct file *filep;
805bb6c1 740 int ret;
03a76b60 741
c82e81ab 742 mutex_lock(&device->group->group_lock);
805bb6c1 743 ret = vfio_device_assign_container(device);
c82e81ab 744 mutex_unlock(&device->group->group_lock);
805bb6c1
JG
745 if (ret)
746 return ERR_PTR(ret);
cba3345c 747
9dcf01d9 748 if (!try_module_get(device->dev->driver->owner)) {
2fd585f4 749 ret = -ENODEV;
805bb6c1 750 goto err_unassign_container;
9dcf01d9
MG
751 }
752
2fd585f4
JG
753 mutex_lock(&device->dev_set->lock);
754 device->open_count++;
421cfe65
MR
755 if (device->open_count == 1) {
756 /*
757 * Here we pass the KVM pointer with the group under the read
758 * lock. If the device driver will use it, it must obtain a
759 * reference and release it during close_device.
760 */
c82e81ab 761 mutex_lock(&device->group->group_lock);
421cfe65
MR
762 device->kvm = device->group->kvm;
763
764 if (device->ops->open_device) {
765 ret = device->ops->open_device(device);
766 if (ret)
767 goto err_undo_count;
768 }
9446162e 769 vfio_device_container_register(device);
c82e81ab 770 mutex_unlock(&device->group->group_lock);
2fd585f4
JG
771 }
772 mutex_unlock(&device->dev_set->lock);
773
4bc94d5d
AW
774 /*
775 * We can't use anon_inode_getfd() because we need to modify
776 * the f_mode flags directly to allow more than just ioctls
777 */
4bc94d5d
AW
778 filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
779 device, O_RDWR);
780 if (IS_ERR(filep)) {
4bc94d5d 781 ret = PTR_ERR(filep);
805bb6c1 782 goto err_close_device;
4bc94d5d
AW
783 }
784
785 /*
786 * TODO: add an anon_inode interface to do this.
787 * Appears to be missing by lack of need rather than
788 * explicitly prevented. Now there's need.
789 */
54ef7a47 790 filep->f_mode |= (FMODE_PREAD | FMODE_PWRITE);
cba3345c 791
805bb6c1 792 if (device->group->type == VFIO_NO_IOMMU)
03a76b60
AW
793 dev_warn(device->dev, "vfio-noiommu device opened by user "
794 "(%s:%d)\n", current->comm, task_pid_nr(current));
805bb6c1
JG
795 /*
796 * On success the ref of device is moved to the file and
797 * put in vfio_device_fops_release()
798 */
799 return filep;
03a76b60 800
2fd585f4
JG
801err_close_device:
802 mutex_lock(&device->dev_set->lock);
c82e81ab 803 mutex_lock(&device->group->group_lock);
ce4b4657 804 if (device->open_count == 1 && device->ops->close_device) {
2fd585f4 805 device->ops->close_device(device);
ce4b4657 806
9446162e 807 vfio_device_container_unregister(device);
ce4b4657 808 }
2fd585f4 809err_undo_count:
c82e81ab 810 mutex_unlock(&device->group->group_lock);
2fd585f4 811 device->open_count--;
421cfe65
MR
812 if (device->open_count == 0 && device->kvm)
813 device->kvm = NULL;
2fd585f4
JG
814 mutex_unlock(&device->dev_set->lock);
815 module_put(device->dev->driver->owner);
805bb6c1 816err_unassign_container:
b76c0eed 817 vfio_device_unassign_container(device);
805bb6c1
JG
818 return ERR_PTR(ret);
819}
820
150ee2f9
JG
821static int vfio_group_ioctl_get_device_fd(struct vfio_group *group,
822 char __user *arg)
805bb6c1
JG
823{
824 struct vfio_device *device;
825 struct file *filep;
150ee2f9 826 char *buf;
805bb6c1
JG
827 int fdno;
828 int ret;
829
150ee2f9
JG
830 buf = strndup_user(arg, PAGE_SIZE);
831 if (IS_ERR(buf))
832 return PTR_ERR(buf);
833
805bb6c1 834 device = vfio_device_get_from_name(group, buf);
150ee2f9 835 kfree(buf);
805bb6c1
JG
836 if (IS_ERR(device))
837 return PTR_ERR(device);
838
839 fdno = get_unused_fd_flags(O_CLOEXEC);
840 if (fdno < 0) {
841 ret = fdno;
842 goto err_put_device;
843 }
844
845 filep = vfio_device_open(device);
846 if (IS_ERR(filep)) {
847 ret = PTR_ERR(filep);
848 goto err_put_fdno;
849 }
850
851 fd_install(fdno, filep);
852 return fdno;
853
854err_put_fdno:
855 put_unused_fd(fdno);
856err_put_device:
4a725b8d 857 vfio_device_put_registration(device);
cba3345c
AW
858 return ret;
859}
860
99a27c08
JG
861static int vfio_group_ioctl_get_status(struct vfio_group *group,
862 struct vfio_group_status __user *arg)
863{
864 unsigned long minsz = offsetofend(struct vfio_group_status, flags);
865 struct vfio_group_status status;
866
867 if (copy_from_user(&status, arg, minsz))
868 return -EFAULT;
869
870 if (status.argsz < minsz)
871 return -EINVAL;
872
873 status.flags = 0;
874
c82e81ab 875 mutex_lock(&group->group_lock);
3dd59a7d
JG
876 if (!group->iommu_group) {
877 mutex_unlock(&group->group_lock);
878 return -ENODEV;
879 }
880
99a27c08
JG
881 if (group->container)
882 status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET |
883 VFIO_GROUP_FLAGS_VIABLE;
884 else if (!iommu_group_dma_owner_claimed(group->iommu_group))
885 status.flags |= VFIO_GROUP_FLAGS_VIABLE;
c82e81ab 886 mutex_unlock(&group->group_lock);
99a27c08
JG
887
888 if (copy_to_user(arg, &status, minsz))
889 return -EFAULT;
890 return 0;
891}
892
cba3345c
AW
893static long vfio_group_fops_unl_ioctl(struct file *filep,
894 unsigned int cmd, unsigned long arg)
895{
896 struct vfio_group *group = filep->private_data;
150ee2f9 897 void __user *uarg = (void __user *)arg;
cba3345c
AW
898
899 switch (cmd) {
150ee2f9
JG
900 case VFIO_GROUP_GET_DEVICE_FD:
901 return vfio_group_ioctl_get_device_fd(group, uarg);
cba3345c 902 case VFIO_GROUP_GET_STATUS:
99a27c08 903 return vfio_group_ioctl_get_status(group, uarg);
cba3345c 904 case VFIO_GROUP_SET_CONTAINER:
67671f15 905 return vfio_group_ioctl_set_container(group, uarg);
cba3345c 906 case VFIO_GROUP_UNSET_CONTAINER:
b3b43590 907 return vfio_group_ioctl_unset_container(group);
99a27c08
JG
908 default:
909 return -ENOTTY;
cba3345c 910 }
cba3345c
AW
911}
912
cba3345c
AW
913static int vfio_group_fops_open(struct inode *inode, struct file *filep)
914{
9cef7391
JG
915 struct vfio_group *group =
916 container_of(inode->i_cdev, struct vfio_group, cdev);
c6f4860e 917 int ret;
cba3345c 918
c82e81ab 919 mutex_lock(&group->group_lock);
cba3345c 920
912b74d2
JG
921 /*
922 * drivers can be zero if this races with vfio_device_remove_group(), it
923 * will be stable at 0 under the group rwsem
924 */
925 if (refcount_read(&group->drivers) == 0) {
c6f4860e 926 ret = -ENODEV;
912b74d2 927 goto out_unlock;
03a76b60
AW
928 }
929
c6f4860e
JG
930 if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) {
931 ret = -EPERM;
912b74d2 932 goto out_unlock;
6d6768c6
AW
933 }
934
c6f4860e
JG
935 /*
936 * Do we need multiple instances of the group open? Seems not.
c6f4860e 937 */
b76c0eed 938 if (group->opened_file) {
c6f4860e 939 ret = -EBUSY;
912b74d2 940 goto out_unlock;
cba3345c 941 }
b76c0eed 942 group->opened_file = filep;
cba3345c 943 filep->private_data = group;
912b74d2
JG
944 ret = 0;
945out_unlock:
c82e81ab 946 mutex_unlock(&group->group_lock);
c6f4860e 947 return ret;
cba3345c
AW
948}
949
950static int vfio_group_fops_release(struct inode *inode, struct file *filep)
951{
952 struct vfio_group *group = filep->private_data;
953
954 filep->private_data = NULL;
955
c82e81ab 956 mutex_lock(&group->group_lock);
b76c0eed
JG
957 /*
958 * Device FDs hold a group file reference, therefore the group release
959 * is only called when there are no open devices.
960 */
961 WARN_ON(group->notifier.head);
429a781c
JG
962 if (group->container)
963 vfio_group_detach_container(group);
b76c0eed 964 group->opened_file = NULL;
c82e81ab 965 mutex_unlock(&group->group_lock);
cba3345c
AW
966 return 0;
967}
968
969static const struct file_operations vfio_group_fops = {
970 .owner = THIS_MODULE,
971 .unlocked_ioctl = vfio_group_fops_unl_ioctl,
407e9ef7 972 .compat_ioctl = compat_ptr_ioctl,
cba3345c
AW
973 .open = vfio_group_fops_open,
974 .release = vfio_group_fops_release,
975};
976
8e5c6995
AS
977/*
978 * Wrapper around pm_runtime_resume_and_get().
979 * Return error code on failure or 0 on success.
980 */
981static inline int vfio_device_pm_runtime_get(struct vfio_device *device)
982{
983 struct device *dev = device->dev;
984
985 if (dev->driver && dev->driver->pm) {
986 int ret;
987
988 ret = pm_runtime_resume_and_get(dev);
989 if (ret) {
990 dev_info_ratelimited(dev,
991 "vfio: runtime resume failed %d\n", ret);
992 return -EIO;
993 }
994 }
995
996 return 0;
997}
998
999/*
1000 * Wrapper around pm_runtime_put().
1001 */
1002static inline void vfio_device_pm_runtime_put(struct vfio_device *device)
1003{
1004 struct device *dev = device->dev;
1005
1006 if (dev->driver && dev->driver->pm)
1007 pm_runtime_put(dev);
1008}
1009
3b9a2d57 1010/*
cba3345c
AW
1011 * VFIO Device fd
1012 */
1013static int vfio_device_fops_release(struct inode *inode, struct file *filep)
1014{
1015 struct vfio_device *device = filep->private_data;
1016
2fd585f4 1017 mutex_lock(&device->dev_set->lock);
eadd86f8 1018 vfio_assert_device_open(device);
c82e81ab 1019 mutex_lock(&device->group->group_lock);
eadd86f8 1020 if (device->open_count == 1 && device->ops->close_device)
2fd585f4 1021 device->ops->close_device(device);
ce4b4657 1022
9446162e 1023 vfio_device_container_unregister(device);
c82e81ab 1024 mutex_unlock(&device->group->group_lock);
eadd86f8 1025 device->open_count--;
421cfe65
MR
1026 if (device->open_count == 0)
1027 device->kvm = NULL;
2fd585f4 1028 mutex_unlock(&device->dev_set->lock);
cba3345c 1029
9dcf01d9
MG
1030 module_put(device->dev->driver->owner);
1031
b76c0eed 1032 vfio_device_unassign_container(device);
cba3345c 1033
4a725b8d 1034 vfio_device_put_registration(device);
cba3345c
AW
1035
1036 return 0;
1037}
1038
115dcec6
JG
1039/*
1040 * vfio_mig_get_next_state - Compute the next step in the FSM
1041 * @cur_fsm - The current state the device is in
1042 * @new_fsm - The target state to reach
1043 * @next_fsm - Pointer to the next step to get to new_fsm
1044 *
1045 * Return 0 upon success, otherwise -errno
1046 * Upon success the next step in the state progression between cur_fsm and
1047 * new_fsm will be set in next_fsm.
1048 *
1049 * This breaks down requests for combination transitions into smaller steps and
1050 * returns the next step to get to new_fsm. The function may need to be called
1051 * multiple times before reaching new_fsm.
1052 *
1053 */
1054int vfio_mig_get_next_state(struct vfio_device *device,
1055 enum vfio_device_mig_state cur_fsm,
1056 enum vfio_device_mig_state new_fsm,
1057 enum vfio_device_mig_state *next_fsm)
1058{
8cb3d83b 1059 enum { VFIO_DEVICE_NUM_STATES = VFIO_DEVICE_STATE_RUNNING_P2P + 1 };
115dcec6 1060 /*
8cb3d83b
JG
1061 * The coding in this table requires the driver to implement the
1062 * following FSM arcs:
115dcec6 1063 * RESUMING -> STOP
115dcec6 1064 * STOP -> RESUMING
115dcec6
JG
1065 * STOP -> STOP_COPY
1066 * STOP_COPY -> STOP
1067 *
8cb3d83b
JG
1068 * If P2P is supported then the driver must also implement these FSM
1069 * arcs:
1070 * RUNNING -> RUNNING_P2P
1071 * RUNNING_P2P -> RUNNING
1072 * RUNNING_P2P -> STOP
1073 * STOP -> RUNNING_P2P
1074 * Without P2P the driver must implement:
1075 * RUNNING -> STOP
1076 * STOP -> RUNNING
1077 *
1078 * The coding will step through multiple states for some combination
1079 * transitions; if all optional features are supported, this means the
1080 * following ones:
1081 * RESUMING -> STOP -> RUNNING_P2P
1082 * RESUMING -> STOP -> RUNNING_P2P -> RUNNING
115dcec6 1083 * RESUMING -> STOP -> STOP_COPY
8cb3d83b
JG
1084 * RUNNING -> RUNNING_P2P -> STOP
1085 * RUNNING -> RUNNING_P2P -> STOP -> RESUMING
1086 * RUNNING -> RUNNING_P2P -> STOP -> STOP_COPY
1087 * RUNNING_P2P -> STOP -> RESUMING
1088 * RUNNING_P2P -> STOP -> STOP_COPY
1089 * STOP -> RUNNING_P2P -> RUNNING
115dcec6 1090 * STOP_COPY -> STOP -> RESUMING
8cb3d83b
JG
1091 * STOP_COPY -> STOP -> RUNNING_P2P
1092 * STOP_COPY -> STOP -> RUNNING_P2P -> RUNNING
115dcec6
JG
1093 */
1094 static const u8 vfio_from_fsm_table[VFIO_DEVICE_NUM_STATES][VFIO_DEVICE_NUM_STATES] = {
1095 [VFIO_DEVICE_STATE_STOP] = {
1096 [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_STOP,
8cb3d83b 1097 [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_RUNNING_P2P,
115dcec6
JG
1098 [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_STOP_COPY,
1099 [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_RESUMING,
8cb3d83b 1100 [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_RUNNING_P2P,
115dcec6
JG
1101 [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR,
1102 },
1103 [VFIO_DEVICE_STATE_RUNNING] = {
8cb3d83b 1104 [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_RUNNING_P2P,
115dcec6 1105 [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_RUNNING,
8cb3d83b
JG
1106 [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_RUNNING_P2P,
1107 [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_RUNNING_P2P,
1108 [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_RUNNING_P2P,
115dcec6
JG
1109 [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR,
1110 },
1111 [VFIO_DEVICE_STATE_STOP_COPY] = {
1112 [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_STOP,
1113 [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_STOP,
1114 [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_STOP_COPY,
1115 [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_STOP,
8cb3d83b 1116 [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_STOP,
115dcec6
JG
1117 [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR,
1118 },
1119 [VFIO_DEVICE_STATE_RESUMING] = {
1120 [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_STOP,
1121 [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_STOP,
1122 [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_STOP,
1123 [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_RESUMING,
8cb3d83b
JG
1124 [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_STOP,
1125 [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR,
1126 },
1127 [VFIO_DEVICE_STATE_RUNNING_P2P] = {
1128 [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_STOP,
1129 [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_RUNNING,
1130 [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_STOP,
1131 [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_STOP,
1132 [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_RUNNING_P2P,
115dcec6
JG
1133 [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR,
1134 },
1135 [VFIO_DEVICE_STATE_ERROR] = {
1136 [VFIO_DEVICE_STATE_STOP] = VFIO_DEVICE_STATE_ERROR,
1137 [VFIO_DEVICE_STATE_RUNNING] = VFIO_DEVICE_STATE_ERROR,
1138 [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_DEVICE_STATE_ERROR,
1139 [VFIO_DEVICE_STATE_RESUMING] = VFIO_DEVICE_STATE_ERROR,
8cb3d83b 1140 [VFIO_DEVICE_STATE_RUNNING_P2P] = VFIO_DEVICE_STATE_ERROR,
115dcec6
JG
1141 [VFIO_DEVICE_STATE_ERROR] = VFIO_DEVICE_STATE_ERROR,
1142 },
1143 };
1144
8cb3d83b
JG
1145 static const unsigned int state_flags_table[VFIO_DEVICE_NUM_STATES] = {
1146 [VFIO_DEVICE_STATE_STOP] = VFIO_MIGRATION_STOP_COPY,
1147 [VFIO_DEVICE_STATE_RUNNING] = VFIO_MIGRATION_STOP_COPY,
1148 [VFIO_DEVICE_STATE_STOP_COPY] = VFIO_MIGRATION_STOP_COPY,
1149 [VFIO_DEVICE_STATE_RESUMING] = VFIO_MIGRATION_STOP_COPY,
1150 [VFIO_DEVICE_STATE_RUNNING_P2P] =
1151 VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_P2P,
1152 [VFIO_DEVICE_STATE_ERROR] = ~0U,
1153 };
1154
1155 if (WARN_ON(cur_fsm >= ARRAY_SIZE(vfio_from_fsm_table) ||
1156 (state_flags_table[cur_fsm] & device->migration_flags) !=
1157 state_flags_table[cur_fsm]))
115dcec6
JG
1158 return -EINVAL;
1159
8cb3d83b
JG
1160 if (new_fsm >= ARRAY_SIZE(vfio_from_fsm_table) ||
1161 (state_flags_table[new_fsm] & device->migration_flags) !=
1162 state_flags_table[new_fsm])
115dcec6
JG
1163 return -EINVAL;
1164
8cb3d83b
JG
1165 /*
1166 * Arcs touching optional and unsupported states are skipped over. The
1167 * driver will instead see an arc from the original state to the next
1168 * logical state, as per the above comment.
1169 */
115dcec6 1170 *next_fsm = vfio_from_fsm_table[cur_fsm][new_fsm];
8cb3d83b
JG
1171 while ((state_flags_table[*next_fsm] & device->migration_flags) !=
1172 state_flags_table[*next_fsm])
1173 *next_fsm = vfio_from_fsm_table[*next_fsm][new_fsm];
1174
115dcec6
JG
1175 return (*next_fsm != VFIO_DEVICE_STATE_ERROR) ? 0 : -EINVAL;
1176}
1177EXPORT_SYMBOL_GPL(vfio_mig_get_next_state);
1178
1179/*
1180 * Convert the drivers's struct file into a FD number and return it to userspace
1181 */
1182static int vfio_ioct_mig_return_fd(struct file *filp, void __user *arg,
1183 struct vfio_device_feature_mig_state *mig)
1184{
1185 int ret;
1186 int fd;
1187
1188 fd = get_unused_fd_flags(O_CLOEXEC);
1189 if (fd < 0) {
1190 ret = fd;
1191 goto out_fput;
1192 }
1193
1194 mig->data_fd = fd;
1195 if (copy_to_user(arg, mig, sizeof(*mig))) {
1196 ret = -EFAULT;
1197 goto out_put_unused;
1198 }
1199 fd_install(fd, filp);
1200 return 0;
1201
1202out_put_unused:
1203 put_unused_fd(fd);
1204out_fput:
1205 fput(filp);
1206 return ret;
1207}
1208
1209static int
1210vfio_ioctl_device_feature_mig_device_state(struct vfio_device *device,
1211 u32 flags, void __user *arg,
1212 size_t argsz)
1213{
1214 size_t minsz =
1215 offsetofend(struct vfio_device_feature_mig_state, data_fd);
1216 struct vfio_device_feature_mig_state mig;
1217 struct file *filp = NULL;
1218 int ret;
1219
6e97eba8 1220 if (!device->mig_ops)
115dcec6
JG
1221 return -ENOTTY;
1222
1223 ret = vfio_check_feature(flags, argsz,
1224 VFIO_DEVICE_FEATURE_SET |
1225 VFIO_DEVICE_FEATURE_GET,
1226 sizeof(mig));
1227 if (ret != 1)
1228 return ret;
1229
1230 if (copy_from_user(&mig, arg, minsz))
1231 return -EFAULT;
1232
1233 if (flags & VFIO_DEVICE_FEATURE_GET) {
1234 enum vfio_device_mig_state curr_state;
1235
6e97eba8
YH
1236 ret = device->mig_ops->migration_get_state(device,
1237 &curr_state);
115dcec6
JG
1238 if (ret)
1239 return ret;
1240 mig.device_state = curr_state;
1241 goto out_copy;
1242 }
1243
1244 /* Handle the VFIO_DEVICE_FEATURE_SET */
6e97eba8 1245 filp = device->mig_ops->migration_set_state(device, mig.device_state);
115dcec6
JG
1246 if (IS_ERR(filp) || !filp)
1247 goto out_copy;
1248
1249 return vfio_ioct_mig_return_fd(filp, arg, &mig);
1250out_copy:
1251 mig.data_fd = -1;
1252 if (copy_to_user(arg, &mig, sizeof(mig)))
1253 return -EFAULT;
1254 if (IS_ERR(filp))
1255 return PTR_ERR(filp);
1256 return 0;
1257}
1258
1259static int vfio_ioctl_device_feature_migration(struct vfio_device *device,
1260 u32 flags, void __user *arg,
1261 size_t argsz)
1262{
1263 struct vfio_device_feature_migration mig = {
8cb3d83b 1264 .flags = device->migration_flags,
115dcec6
JG
1265 };
1266 int ret;
1267
6e97eba8 1268 if (!device->mig_ops)
115dcec6
JG
1269 return -ENOTTY;
1270
1271 ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET,
1272 sizeof(mig));
1273 if (ret != 1)
1274 return ret;
1275 if (copy_to_user(arg, &mig, sizeof(mig)))
1276 return -EFAULT;
1277 return 0;
1278}
1279
80c4b92a
YH
1280/* Ranges should fit into a single kernel page */
1281#define LOG_MAX_RANGES \
1282 (PAGE_SIZE / sizeof(struct vfio_device_feature_dma_logging_range))
1283
1284static int
1285vfio_ioctl_device_feature_logging_start(struct vfio_device *device,
1286 u32 flags, void __user *arg,
1287 size_t argsz)
1288{
1289 size_t minsz =
1290 offsetofend(struct vfio_device_feature_dma_logging_control,
1291 ranges);
1292 struct vfio_device_feature_dma_logging_range __user *ranges;
1293 struct vfio_device_feature_dma_logging_control control;
1294 struct vfio_device_feature_dma_logging_range range;
1295 struct rb_root_cached root = RB_ROOT_CACHED;
1296 struct interval_tree_node *nodes;
1297 u64 iova_end;
1298 u32 nnodes;
1299 int i, ret;
1300
1301 if (!device->log_ops)
1302 return -ENOTTY;
1303
1304 ret = vfio_check_feature(flags, argsz,
1305 VFIO_DEVICE_FEATURE_SET,
1306 sizeof(control));
1307 if (ret != 1)
1308 return ret;
1309
1310 if (copy_from_user(&control, arg, minsz))
1311 return -EFAULT;
1312
1313 nnodes = control.num_ranges;
1314 if (!nnodes)
1315 return -EINVAL;
1316
1317 if (nnodes > LOG_MAX_RANGES)
1318 return -E2BIG;
1319
1320 ranges = u64_to_user_ptr(control.ranges);
1321 nodes = kmalloc_array(nnodes, sizeof(struct interval_tree_node),
1322 GFP_KERNEL);
1323 if (!nodes)
1324 return -ENOMEM;
1325
1326 for (i = 0; i < nnodes; i++) {
1327 if (copy_from_user(&range, &ranges[i], sizeof(range))) {
1328 ret = -EFAULT;
1329 goto end;
1330 }
1331 if (!IS_ALIGNED(range.iova, control.page_size) ||
1332 !IS_ALIGNED(range.length, control.page_size)) {
1333 ret = -EINVAL;
1334 goto end;
1335 }
1336
1337 if (check_add_overflow(range.iova, range.length, &iova_end) ||
1338 iova_end > ULONG_MAX) {
1339 ret = -EOVERFLOW;
1340 goto end;
1341 }
1342
1343 nodes[i].start = range.iova;
1344 nodes[i].last = range.iova + range.length - 1;
1345 if (interval_tree_iter_first(&root, nodes[i].start,
1346 nodes[i].last)) {
1347 /* Range overlapping */
1348 ret = -EINVAL;
1349 goto end;
1350 }
1351 interval_tree_insert(nodes + i, &root);
1352 }
1353
1354 ret = device->log_ops->log_start(device, &root, nnodes,
1355 &control.page_size);
1356 if (ret)
1357 goto end;
1358
1359 if (copy_to_user(arg, &control, sizeof(control))) {
1360 ret = -EFAULT;
1361 device->log_ops->log_stop(device);
1362 }
1363
1364end:
1365 kfree(nodes);
1366 return ret;
1367}
1368
1369static int
1370vfio_ioctl_device_feature_logging_stop(struct vfio_device *device,
1371 u32 flags, void __user *arg,
1372 size_t argsz)
1373{
1374 int ret;
1375
1376 if (!device->log_ops)
1377 return -ENOTTY;
1378
1379 ret = vfio_check_feature(flags, argsz,
1380 VFIO_DEVICE_FEATURE_SET, 0);
1381 if (ret != 1)
1382 return ret;
1383
1384 return device->log_ops->log_stop(device);
1385}
1386
1387static int vfio_device_log_read_and_clear(struct iova_bitmap *iter,
1388 unsigned long iova, size_t length,
1389 void *opaque)
1390{
1391 struct vfio_device *device = opaque;
1392
1393 return device->log_ops->log_read_and_clear(device, iova, length, iter);
1394}
1395
1396static int
1397vfio_ioctl_device_feature_logging_report(struct vfio_device *device,
1398 u32 flags, void __user *arg,
1399 size_t argsz)
1400{
1401 size_t minsz =
1402 offsetofend(struct vfio_device_feature_dma_logging_report,
1403 bitmap);
1404 struct vfio_device_feature_dma_logging_report report;
1405 struct iova_bitmap *iter;
1406 u64 iova_end;
1407 int ret;
1408
1409 if (!device->log_ops)
1410 return -ENOTTY;
1411
1412 ret = vfio_check_feature(flags, argsz,
1413 VFIO_DEVICE_FEATURE_GET,
1414 sizeof(report));
1415 if (ret != 1)
1416 return ret;
1417
1418 if (copy_from_user(&report, arg, minsz))
1419 return -EFAULT;
1420
1421 if (report.page_size < SZ_4K || !is_power_of_2(report.page_size))
1422 return -EINVAL;
1423
1424 if (check_add_overflow(report.iova, report.length, &iova_end) ||
1425 iova_end > ULONG_MAX)
1426 return -EOVERFLOW;
1427
1428 iter = iova_bitmap_alloc(report.iova, report.length,
1429 report.page_size,
1430 u64_to_user_ptr(report.bitmap));
1431 if (IS_ERR(iter))
1432 return PTR_ERR(iter);
1433
1434 ret = iova_bitmap_for_each(iter, device,
1435 vfio_device_log_read_and_clear);
1436
1437 iova_bitmap_free(iter);
1438 return ret;
1439}
1440
445ad495
JG
1441static int vfio_ioctl_device_feature(struct vfio_device *device,
1442 struct vfio_device_feature __user *arg)
1443{
1444 size_t minsz = offsetofend(struct vfio_device_feature, flags);
1445 struct vfio_device_feature feature;
1446
1447 if (copy_from_user(&feature, arg, minsz))
1448 return -EFAULT;
1449
1450 if (feature.argsz < minsz)
1451 return -EINVAL;
1452
1453 /* Check unknown flags */
1454 if (feature.flags &
1455 ~(VFIO_DEVICE_FEATURE_MASK | VFIO_DEVICE_FEATURE_SET |
1456 VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_PROBE))
1457 return -EINVAL;
1458
1459 /* GET & SET are mutually exclusive except with PROBE */
1460 if (!(feature.flags & VFIO_DEVICE_FEATURE_PROBE) &&
1461 (feature.flags & VFIO_DEVICE_FEATURE_SET) &&
1462 (feature.flags & VFIO_DEVICE_FEATURE_GET))
1463 return -EINVAL;
1464
1465 switch (feature.flags & VFIO_DEVICE_FEATURE_MASK) {
115dcec6
JG
1466 case VFIO_DEVICE_FEATURE_MIGRATION:
1467 return vfio_ioctl_device_feature_migration(
1468 device, feature.flags, arg->data,
1469 feature.argsz - minsz);
1470 case VFIO_DEVICE_FEATURE_MIG_DEVICE_STATE:
1471 return vfio_ioctl_device_feature_mig_device_state(
1472 device, feature.flags, arg->data,
1473 feature.argsz - minsz);
80c4b92a
YH
1474 case VFIO_DEVICE_FEATURE_DMA_LOGGING_START:
1475 return vfio_ioctl_device_feature_logging_start(
1476 device, feature.flags, arg->data,
1477 feature.argsz - minsz);
1478 case VFIO_DEVICE_FEATURE_DMA_LOGGING_STOP:
1479 return vfio_ioctl_device_feature_logging_stop(
1480 device, feature.flags, arg->data,
1481 feature.argsz - minsz);
1482 case VFIO_DEVICE_FEATURE_DMA_LOGGING_REPORT:
1483 return vfio_ioctl_device_feature_logging_report(
1484 device, feature.flags, arg->data,
1485 feature.argsz - minsz);
445ad495
JG
1486 default:
1487 if (unlikely(!device->ops->device_feature))
1488 return -EINVAL;
1489 return device->ops->device_feature(device, feature.flags,
1490 arg->data,
1491 feature.argsz - minsz);
1492 }
1493}
1494
cba3345c
AW
1495static long vfio_device_fops_unl_ioctl(struct file *filep,
1496 unsigned int cmd, unsigned long arg)
1497{
1498 struct vfio_device *device = filep->private_data;
8e5c6995
AS
1499 int ret;
1500
1501 ret = vfio_device_pm_runtime_get(device);
1502 if (ret)
1503 return ret;
cba3345c 1504
445ad495
JG
1505 switch (cmd) {
1506 case VFIO_DEVICE_FEATURE:
8e5c6995
AS
1507 ret = vfio_ioctl_device_feature(device, (void __user *)arg);
1508 break;
1509
445ad495
JG
1510 default:
1511 if (unlikely(!device->ops->ioctl))
8e5c6995
AS
1512 ret = -EINVAL;
1513 else
1514 ret = device->ops->ioctl(device, cmd, arg);
1515 break;
445ad495 1516 }
8e5c6995
AS
1517
1518 vfio_device_pm_runtime_put(device);
1519 return ret;
cba3345c
AW
1520}
1521
1522static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
1523 size_t count, loff_t *ppos)
1524{
1525 struct vfio_device *device = filep->private_data;
1526
1527 if (unlikely(!device->ops->read))
1528 return -EINVAL;
1529
6df62c5b 1530 return device->ops->read(device, buf, count, ppos);
cba3345c
AW
1531}
1532
1533static ssize_t vfio_device_fops_write(struct file *filep,
1534 const char __user *buf,
1535 size_t count, loff_t *ppos)
1536{
1537 struct vfio_device *device = filep->private_data;
1538
1539 if (unlikely(!device->ops->write))
1540 return -EINVAL;
1541
6df62c5b 1542 return device->ops->write(device, buf, count, ppos);
cba3345c
AW
1543}
1544
1545static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
1546{
1547 struct vfio_device *device = filep->private_data;
1548
1549 if (unlikely(!device->ops->mmap))
1550 return -EINVAL;
1551
6df62c5b 1552 return device->ops->mmap(device, vma);
cba3345c
AW
1553}
1554
cba3345c
AW
1555static const struct file_operations vfio_device_fops = {
1556 .owner = THIS_MODULE,
1557 .release = vfio_device_fops_release,
1558 .read = vfio_device_fops_read,
1559 .write = vfio_device_fops_write,
1560 .unlocked_ioctl = vfio_device_fops_unl_ioctl,
407e9ef7 1561 .compat_ioctl = compat_ptr_ioctl,
cba3345c
AW
1562 .mmap = vfio_device_fops_mmap,
1563};
1564
50d63b5b
JG
1565/**
1566 * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file
1567 * @file: VFIO group file
6cdd9782 1568 *
819da99a
JG
1569 * The returned iommu_group is valid as long as a ref is held on the file. This
1570 * returns a reference on the group. This function is deprecated, only the SPAPR
1571 * path in kvm should call it.
6cdd9782 1572 */
50d63b5b 1573struct iommu_group *vfio_file_iommu_group(struct file *file)
6cdd9782 1574{
50d63b5b 1575 struct vfio_group *group = file->private_data;
3dd59a7d 1576 struct iommu_group *iommu_group = NULL;
6cdd9782 1577
4b22ef04
JG
1578 if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU))
1579 return NULL;
1580
1581 if (!vfio_file_is_group(file))
50d63b5b 1582 return NULL;
3dd59a7d
JG
1583
1584 mutex_lock(&group->group_lock);
1585 if (group->iommu_group) {
1586 iommu_group = group->iommu_group;
1587 iommu_group_ref_get(iommu_group);
1588 }
1589 mutex_unlock(&group->group_lock);
1590 return iommu_group;
6cdd9782 1591}
50d63b5b 1592EXPORT_SYMBOL_GPL(vfio_file_iommu_group);
6cdd9782 1593
4b22ef04
JG
1594/**
1595 * vfio_file_is_group - True if the file is usable with VFIO aPIS
1596 * @file: VFIO group file
1597 */
1598bool vfio_file_is_group(struct file *file)
1599{
1600 return file->f_op == &vfio_group_fops;
1601}
1602EXPORT_SYMBOL_GPL(vfio_file_is_group);
1603
a905ad04
JG
1604/**
1605 * vfio_file_enforced_coherent - True if the DMA associated with the VFIO file
1606 * is always CPU cache coherent
1607 * @file: VFIO group file
c0560f51 1608 *
a905ad04
JG
1609 * Enforced coherency means that the IOMMU ignores things like the PCIe no-snoop
1610 * bit in DMA transactions. A return of false indicates that the user has
1611 * rights to access additional instructions such as wbinvd on x86.
c0560f51 1612 */
a905ad04 1613bool vfio_file_enforced_coherent(struct file *file)
c0560f51 1614{
a905ad04
JG
1615 struct vfio_group *group = file->private_data;
1616 bool ret;
c0560f51 1617
b1b8132a 1618 if (!vfio_file_is_group(file))
a905ad04 1619 return true;
c0560f51 1620
c82e81ab 1621 mutex_lock(&group->group_lock);
e0e29bdb 1622 if (group->container) {
1408640d
JG
1623 ret = vfio_container_ioctl_check_extension(group->container,
1624 VFIO_DMA_CC_IOMMU);
e0e29bdb
JG
1625 } else {
1626 /*
1627 * Since the coherency state is determined only once a container
1628 * is attached the user must do so before they can prove they
1629 * have permission.
1630 */
1631 ret = true;
c0560f51 1632 }
c82e81ab 1633 mutex_unlock(&group->group_lock);
a905ad04 1634 return ret;
c0560f51 1635}
a905ad04 1636EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent);
c0560f51 1637
ba70a89f
JG
1638/**
1639 * vfio_file_set_kvm - Link a kvm with VFIO drivers
1640 * @file: VFIO group file
1641 * @kvm: KVM to link
1642 *
421cfe65
MR
1643 * When a VFIO device is first opened the KVM will be available in
1644 * device->kvm if one was associated with the group.
ba70a89f
JG
1645 */
1646void vfio_file_set_kvm(struct file *file, struct kvm *kvm)
6cdd9782 1647{
ba70a89f 1648 struct vfio_group *group = file->private_data;
6cdd9782 1649
b1b8132a 1650 if (!vfio_file_is_group(file))
ba70a89f 1651 return;
5d6dee80 1652
c82e81ab 1653 mutex_lock(&group->group_lock);
ba70a89f 1654 group->kvm = kvm;
c82e81ab 1655 mutex_unlock(&group->group_lock);
5d6dee80 1656}
ba70a89f 1657EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
5d6dee80 1658
6a985ae8
JG
1659/**
1660 * vfio_file_has_dev - True if the VFIO file is a handle for device
1661 * @file: VFIO file to check
1662 * @device: Device that must be part of the file
1663 *
1664 * Returns true if given file has permission to manipulate the given device.
1665 */
1666bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
6cdd9782 1667{
6a985ae8 1668 struct vfio_group *group = file->private_data;
6cdd9782 1669
b1b8132a 1670 if (!vfio_file_is_group(file))
6a985ae8
JG
1671 return false;
1672
1673 return group == device->group;
88d7ab89 1674}
6a985ae8 1675EXPORT_SYMBOL_GPL(vfio_file_has_dev);
88d7ab89 1676
3b9a2d57 1677/*
d7a8d5ed
AW
1678 * Sub-module support
1679 */
1680/*
1681 * Helper for managing a buffer of info chain capabilities, allocate or
1682 * reallocate a buffer with additional @size, filling in @id and @version
1683 * of the capability. A pointer to the new capability is returned.
1684 *
1685 * NB. The chain is based at the head of the buffer, so new entries are
1686 * added to the tail, vfio_info_cap_shift() should be called to fixup the
1687 * next offsets prior to copying to the user buffer.
1688 */
1689struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
1690 size_t size, u16 id, u16 version)
1691{
1692 void *buf;
1693 struct vfio_info_cap_header *header, *tmp;
1694
1695 buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL);
1696 if (!buf) {
1697 kfree(caps->buf);
6641085e 1698 caps->buf = NULL;
d7a8d5ed
AW
1699 caps->size = 0;
1700 return ERR_PTR(-ENOMEM);
1701 }
1702
1703 caps->buf = buf;
1704 header = buf + caps->size;
1705
1706 /* Eventually copied to user buffer, zero */
1707 memset(header, 0, size);
1708
1709 header->id = id;
1710 header->version = version;
1711
1712 /* Add to the end of the capability chain */
5ba6de98 1713 for (tmp = buf; tmp->next; tmp = buf + tmp->next)
d7a8d5ed
AW
1714 ; /* nothing */
1715
1716 tmp->next = caps->size;
1717 caps->size += size;
1718
1719 return header;
1720}
1721EXPORT_SYMBOL_GPL(vfio_info_cap_add);
1722
1723void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
1724{
1725 struct vfio_info_cap_header *tmp;
5ba6de98 1726 void *buf = (void *)caps->buf;
d7a8d5ed 1727
5ba6de98 1728 for (tmp = buf; tmp->next; tmp = buf + tmp->next - offset)
d7a8d5ed
AW
1729 tmp->next += offset;
1730}
b3c0a866 1731EXPORT_SYMBOL(vfio_info_cap_shift);
d7a8d5ed 1732
dda01f78
AW
1733int vfio_info_add_capability(struct vfio_info_cap *caps,
1734 struct vfio_info_cap_header *cap, size_t size)
b3c0a866
KW
1735{
1736 struct vfio_info_cap_header *header;
b3c0a866 1737
dda01f78 1738 header = vfio_info_cap_add(caps, size, cap->id, cap->version);
b3c0a866
KW
1739 if (IS_ERR(header))
1740 return PTR_ERR(header);
1741
dda01f78 1742 memcpy(header + 1, cap + 1, size - sizeof(*header));
b3c0a866 1743
b3c0a866
KW
1744 return 0;
1745}
b3c0a866 1746EXPORT_SYMBOL(vfio_info_add_capability);
2169037d 1747
c747f08a
KW
1748int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs,
1749 int max_irq_type, size_t *data_size)
1750{
1751 unsigned long minsz;
1752 size_t size;
1753
1754 minsz = offsetofend(struct vfio_irq_set, count);
1755
1756 if ((hdr->argsz < minsz) || (hdr->index >= max_irq_type) ||
1757 (hdr->count >= (U32_MAX - hdr->start)) ||
1758 (hdr->flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
1759 VFIO_IRQ_SET_ACTION_TYPE_MASK)))
1760 return -EINVAL;
1761
1762 if (data_size)
1763 *data_size = 0;
1764
1765 if (hdr->start >= num_irqs || hdr->start + hdr->count > num_irqs)
1766 return -EINVAL;
1767
1768 switch (hdr->flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
1769 case VFIO_IRQ_SET_DATA_NONE:
1770 size = 0;
1771 break;
1772 case VFIO_IRQ_SET_DATA_BOOL:
1773 size = sizeof(uint8_t);
1774 break;
1775 case VFIO_IRQ_SET_DATA_EVENTFD:
1776 size = sizeof(int32_t);
1777 break;
1778 default:
1779 return -EINVAL;
1780 }
1781
1782 if (size) {
1783 if (hdr->argsz - minsz < hdr->count * size)
1784 return -EINVAL;
1785
1786 if (!data_size)
1787 return -EINVAL;
1788
1789 *data_size = hdr->count * size;
1790 }
1791
1792 return 0;
1793}
1794EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
1795
3b9a2d57 1796/*
cba3345c
AW
1797 * Module/class support
1798 */
1799static char *vfio_devnode(struct device *dev, umode_t *mode)
1800{
1801 return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
1802}
1803
c41da462
JG
1804static int __init vfio_init(void)
1805{
1806 int ret;
1807
1808 ida_init(&vfio.group_ida);
1809 ida_init(&vfio.device_ida);
1810 mutex_init(&vfio.group_lock);
1811 INIT_LIST_HEAD(&vfio.group_list);
1812
1813 ret = vfio_container_init();
1814 if (ret)
1815 return ret;
1816
d1099901 1817 /* /dev/vfio/$GROUP */
cba3345c
AW
1818 vfio.class = class_create(THIS_MODULE, "vfio");
1819 if (IS_ERR(vfio.class)) {
1820 ret = PTR_ERR(vfio.class);
3c28a761 1821 goto err_group_class;
cba3345c
AW
1822 }
1823
1824 vfio.class->devnode = vfio_devnode;
1825
3c28a761
YL
1826 /* /sys/class/vfio-dev/vfioX */
1827 vfio.device_class = class_create(THIS_MODULE, "vfio-dev");
1828 if (IS_ERR(vfio.device_class)) {
1829 ret = PTR_ERR(vfio.device_class);
1830 goto err_dev_class;
1831 }
1832
8bcb64a5 1833 ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK + 1, "vfio");
cba3345c 1834 if (ret)
d1099901 1835 goto err_alloc_chrdev;
cba3345c 1836
a13b1e47 1837 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
cba3345c
AW
1838 return 0;
1839
d1099901 1840err_alloc_chrdev:
3c28a761
YL
1841 class_destroy(vfio.device_class);
1842 vfio.device_class = NULL;
1843err_dev_class:
cba3345c
AW
1844 class_destroy(vfio.class);
1845 vfio.class = NULL;
3c28a761 1846err_group_class:
c41da462 1847 vfio_container_cleanup();
cba3345c
AW
1848 return ret;
1849}
1850
1851static void __exit vfio_cleanup(void)
1852{
1853 WARN_ON(!list_empty(&vfio.group_list));
1854
3c28a761 1855 ida_destroy(&vfio.device_ida);
9cef7391 1856 ida_destroy(&vfio.group_ida);
8bcb64a5 1857 unregister_chrdev_region(vfio.group_devt, MINORMASK + 1);
3c28a761
YL
1858 class_destroy(vfio.device_class);
1859 vfio.device_class = NULL;
cba3345c 1860 class_destroy(vfio.class);
c41da462 1861 vfio_container_cleanup();
cba3345c 1862 vfio.class = NULL;
2fd585f4 1863 xa_destroy(&vfio_device_set_xa);
cba3345c
AW
1864}
1865
1866module_init(vfio_init);
1867module_exit(vfio_cleanup);
1868
1869MODULE_VERSION(DRIVER_VERSION);
1870MODULE_LICENSE("GPL v2");
1871MODULE_AUTHOR(DRIVER_AUTHOR);
1872MODULE_DESCRIPTION(DRIVER_DESC);
d1099901
AW
1873MODULE_ALIAS_MISCDEV(VFIO_MINOR);
1874MODULE_ALIAS("devname:vfio/vfio");
0ca582fd 1875MODULE_SOFTDEP("post: vfio_iommu_type1 vfio_iommu_spapr_tce");