vfio.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /*
  2. * VFIO core
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  5. * Author: Alex Williamson <alex.williamson@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Derived from original vfio:
  12. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  13. * Author: Tom Lyon, pugs@cisco.com
  14. */
  15. #include <linux/cdev.h>
  16. #include <linux/compat.h>
  17. #include <linux/device.h>
  18. #include <linux/file.h>
  19. #include <linux/anon_inodes.h>
  20. #include <linux/fs.h>
  21. #include <linux/idr.h>
  22. #include <linux/iommu.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/rwsem.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/stat.h>
  30. #include <linux/string.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/vfio.h>
  33. #include <linux/wait.h>
  34. #define DRIVER_VERSION "0.3"
  35. #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
  36. #define DRIVER_DESC "VFIO - User Level meta-driver"
  37. static struct vfio {
  38. struct class *class;
  39. struct list_head iommu_drivers_list;
  40. struct mutex iommu_drivers_lock;
  41. struct list_head group_list;
  42. struct idr group_idr;
  43. struct mutex group_lock;
  44. struct cdev group_cdev;
  45. struct device *dev;
  46. dev_t devt;
  47. struct cdev cdev;
  48. wait_queue_head_t release_q;
  49. } vfio;
  50. struct vfio_iommu_driver {
  51. const struct vfio_iommu_driver_ops *ops;
  52. struct list_head vfio_next;
  53. };
  54. struct vfio_container {
  55. struct kref kref;
  56. struct list_head group_list;
  57. struct rw_semaphore group_lock;
  58. struct vfio_iommu_driver *iommu_driver;
  59. void *iommu_data;
  60. };
  61. struct vfio_group {
  62. struct kref kref;
  63. int minor;
  64. atomic_t container_users;
  65. struct iommu_group *iommu_group;
  66. struct vfio_container *container;
  67. struct list_head device_list;
  68. struct mutex device_lock;
  69. struct device *dev;
  70. struct notifier_block nb;
  71. struct list_head vfio_next;
  72. struct list_head container_next;
  73. };
  74. struct vfio_device {
  75. struct kref kref;
  76. struct device *dev;
  77. const struct vfio_device_ops *ops;
  78. struct vfio_group *group;
  79. struct list_head group_next;
  80. void *device_data;
  81. };
  82. /**
  83. * IOMMU driver registration
  84. */
  85. int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
  86. {
  87. struct vfio_iommu_driver *driver, *tmp;
  88. driver = kzalloc(sizeof(*driver), GFP_KERNEL);
  89. if (!driver)
  90. return -ENOMEM;
  91. driver->ops = ops;
  92. mutex_lock(&vfio.iommu_drivers_lock);
  93. /* Check for duplicates */
  94. list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
  95. if (tmp->ops == ops) {
  96. mutex_unlock(&vfio.iommu_drivers_lock);
  97. kfree(driver);
  98. return -EINVAL;
  99. }
  100. }
  101. list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
  102. mutex_unlock(&vfio.iommu_drivers_lock);
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
  106. void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
  107. {
  108. struct vfio_iommu_driver *driver;
  109. mutex_lock(&vfio.iommu_drivers_lock);
  110. list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
  111. if (driver->ops == ops) {
  112. list_del(&driver->vfio_next);
  113. mutex_unlock(&vfio.iommu_drivers_lock);
  114. kfree(driver);
  115. return;
  116. }
  117. }
  118. mutex_unlock(&vfio.iommu_drivers_lock);
  119. }
  120. EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
  121. /**
  122. * Group minor allocation/free - both called with vfio.group_lock held
  123. */
  124. static int vfio_alloc_group_minor(struct vfio_group *group)
  125. {
  126. /* index 0 is used by /dev/vfio/vfio */
  127. return idr_alloc(&vfio.group_idr, group, 1, MINORMASK + 1, GFP_KERNEL);
  128. }
  129. static void vfio_free_group_minor(int minor)
  130. {
  131. idr_remove(&vfio.group_idr, minor);
  132. }
  133. static int vfio_iommu_group_notifier(struct notifier_block *nb,
  134. unsigned long action, void *data);
  135. static void vfio_group_get(struct vfio_group *group);
  136. /**
  137. * Container objects - containers are created when /dev/vfio/vfio is
  138. * opened, but their lifecycle extends until the last user is done, so
  139. * it's freed via kref. Must support container/group/device being
  140. * closed in any order.
  141. */
  142. static void vfio_container_get(struct vfio_container *container)
  143. {
  144. kref_get(&container->kref);
  145. }
  146. static void vfio_container_release(struct kref *kref)
  147. {
  148. struct vfio_container *container;
  149. container = container_of(kref, struct vfio_container, kref);
  150. kfree(container);
  151. }
  152. static void vfio_container_put(struct vfio_container *container)
  153. {
  154. kref_put(&container->kref, vfio_container_release);
  155. }
  156. static void vfio_group_unlock_and_free(struct vfio_group *group)
  157. {
  158. mutex_unlock(&vfio.group_lock);
  159. /*
  160. * Unregister outside of lock. A spurious callback is harmless now
  161. * that the group is no longer in vfio.group_list.
  162. */
  163. iommu_group_unregister_notifier(group->iommu_group, &group->nb);
  164. kfree(group);
  165. }
  166. /**
  167. * Group objects - create, release, get, put, search
  168. */
  169. static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
  170. {
  171. struct vfio_group *group, *tmp;
  172. struct device *dev;
  173. int ret, minor;
  174. group = kzalloc(sizeof(*group), GFP_KERNEL);
  175. if (!group)
  176. return ERR_PTR(-ENOMEM);
  177. kref_init(&group->kref);
  178. INIT_LIST_HEAD(&group->device_list);
  179. mutex_init(&group->device_lock);
  180. atomic_set(&group->container_users, 0);
  181. group->iommu_group = iommu_group;
  182. group->nb.notifier_call = vfio_iommu_group_notifier;
  183. /*
  184. * blocking notifiers acquire a rwsem around registering and hold
  185. * it around callback. Therefore, need to register outside of
  186. * vfio.group_lock to avoid A-B/B-A contention. Our callback won't
  187. * do anything unless it can find the group in vfio.group_list, so
  188. * no harm in registering early.
  189. */
  190. ret = iommu_group_register_notifier(iommu_group, &group->nb);
  191. if (ret) {
  192. kfree(group);
  193. return ERR_PTR(ret);
  194. }
  195. mutex_lock(&vfio.group_lock);
  196. minor = vfio_alloc_group_minor(group);
  197. if (minor < 0) {
  198. vfio_group_unlock_and_free(group);
  199. return ERR_PTR(minor);
  200. }
  201. /* Did we race creating this group? */
  202. list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
  203. if (tmp->iommu_group == iommu_group) {
  204. vfio_group_get(tmp);
  205. vfio_free_group_minor(minor);
  206. vfio_group_unlock_and_free(group);
  207. return tmp;
  208. }
  209. }
  210. dev = device_create(vfio.class, NULL, MKDEV(MAJOR(vfio.devt), minor),
  211. group, "%d", iommu_group_id(iommu_group));
  212. if (IS_ERR(dev)) {
  213. vfio_free_group_minor(minor);
  214. vfio_group_unlock_and_free(group);
  215. return (struct vfio_group *)dev; /* ERR_PTR */
  216. }
  217. group->minor = minor;
  218. group->dev = dev;
  219. list_add(&group->vfio_next, &vfio.group_list);
  220. mutex_unlock(&vfio.group_lock);
  221. return group;
  222. }
  223. /* called with vfio.group_lock held */
  224. static void vfio_group_release(struct kref *kref)
  225. {
  226. struct vfio_group *group = container_of(kref, struct vfio_group, kref);
  227. WARN_ON(!list_empty(&group->device_list));
  228. device_destroy(vfio.class, MKDEV(MAJOR(vfio.devt), group->minor));
  229. list_del(&group->vfio_next);
  230. vfio_free_group_minor(group->minor);
  231. vfio_group_unlock_and_free(group);
  232. }
  233. static void vfio_group_put(struct vfio_group *group)
  234. {
  235. kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
  236. }
  237. /* Assume group_lock or group reference is held */
  238. static void vfio_group_get(struct vfio_group *group)
  239. {
  240. kref_get(&group->kref);
  241. }
  242. /*
  243. * Not really a try as we will sleep for mutex, but we need to make
  244. * sure the group pointer is valid under lock and get a reference.
  245. */
  246. static struct vfio_group *vfio_group_try_get(struct vfio_group *group)
  247. {
  248. struct vfio_group *target = group;
  249. mutex_lock(&vfio.group_lock);
  250. list_for_each_entry(group, &vfio.group_list, vfio_next) {
  251. if (group == target) {
  252. vfio_group_get(group);
  253. mutex_unlock(&vfio.group_lock);
  254. return group;
  255. }
  256. }
  257. mutex_unlock(&vfio.group_lock);
  258. return NULL;
  259. }
  260. static
  261. struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group)
  262. {
  263. struct vfio_group *group;
  264. mutex_lock(&vfio.group_lock);
  265. list_for_each_entry(group, &vfio.group_list, vfio_next) {
  266. if (group->iommu_group == iommu_group) {
  267. vfio_group_get(group);
  268. mutex_unlock(&vfio.group_lock);
  269. return group;
  270. }
  271. }
  272. mutex_unlock(&vfio.group_lock);
  273. return NULL;
  274. }
  275. static struct vfio_group *vfio_group_get_from_minor(int minor)
  276. {
  277. struct vfio_group *group;
  278. mutex_lock(&vfio.group_lock);
  279. group = idr_find(&vfio.group_idr, minor);
  280. if (!group) {
  281. mutex_unlock(&vfio.group_lock);
  282. return NULL;
  283. }
  284. vfio_group_get(group);
  285. mutex_unlock(&vfio.group_lock);
  286. return group;
  287. }
  288. /**
  289. * Device objects - create, release, get, put, search
  290. */
  291. static
  292. struct vfio_device *vfio_group_create_device(struct vfio_group *group,
  293. struct device *dev,
  294. const struct vfio_device_ops *ops,
  295. void *device_data)
  296. {
  297. struct vfio_device *device;
  298. int ret;
  299. device = kzalloc(sizeof(*device), GFP_KERNEL);
  300. if (!device)
  301. return ERR_PTR(-ENOMEM);
  302. kref_init(&device->kref);
  303. device->dev = dev;
  304. device->group = group;
  305. device->ops = ops;
  306. device->device_data = device_data;
  307. ret = dev_set_drvdata(dev, device);
  308. if (ret) {
  309. kfree(device);
  310. return ERR_PTR(ret);
  311. }
  312. /* No need to get group_lock, caller has group reference */
  313. vfio_group_get(group);
  314. mutex_lock(&group->device_lock);
  315. list_add(&device->group_next, &group->device_list);
  316. mutex_unlock(&group->device_lock);
  317. return device;
  318. }
  319. static void vfio_device_release(struct kref *kref)
  320. {
  321. struct vfio_device *device = container_of(kref,
  322. struct vfio_device, kref);
  323. struct vfio_group *group = device->group;
  324. list_del(&device->group_next);
  325. mutex_unlock(&group->device_lock);
  326. dev_set_drvdata(device->dev, NULL);
  327. kfree(device);
  328. /* vfio_del_group_dev may be waiting for this device */
  329. wake_up(&vfio.release_q);
  330. }
  331. /* Device reference always implies a group reference */
  332. void vfio_device_put(struct vfio_device *device)
  333. {
  334. struct vfio_group *group = device->group;
  335. kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock);
  336. vfio_group_put(group);
  337. }
  338. EXPORT_SYMBOL_GPL(vfio_device_put);
  339. static void vfio_device_get(struct vfio_device *device)
  340. {
  341. vfio_group_get(device->group);
  342. kref_get(&device->kref);
  343. }
  344. static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
  345. struct device *dev)
  346. {
  347. struct vfio_device *device;
  348. mutex_lock(&group->device_lock);
  349. list_for_each_entry(device, &group->device_list, group_next) {
  350. if (device->dev == dev) {
  351. vfio_device_get(device);
  352. mutex_unlock(&group->device_lock);
  353. return device;
  354. }
  355. }
  356. mutex_unlock(&group->device_lock);
  357. return NULL;
  358. }
  359. /*
  360. * Whitelist some drivers that we know are safe (no dma) or just sit on
  361. * a device. It's not always practical to leave a device within a group
  362. * driverless as it could get re-bound to something unsafe.
  363. */
  364. static const char * const vfio_driver_whitelist[] = { "pci-stub", "pcieport" };
  365. static bool vfio_whitelisted_driver(struct device_driver *drv)
  366. {
  367. int i;
  368. for (i = 0; i < ARRAY_SIZE(vfio_driver_whitelist); i++) {
  369. if (!strcmp(drv->name, vfio_driver_whitelist[i]))
  370. return true;
  371. }
  372. return false;
  373. }
  374. /*
  375. * A vfio group is viable for use by userspace if all devices are either
  376. * driver-less or bound to a vfio or whitelisted driver. We test the
  377. * latter by the existence of a struct vfio_device matching the dev.
  378. */
  379. static int vfio_dev_viable(struct device *dev, void *data)
  380. {
  381. struct vfio_group *group = data;
  382. struct vfio_device *device;
  383. struct device_driver *drv = ACCESS_ONCE(dev->driver);
  384. if (!drv || vfio_whitelisted_driver(drv))
  385. return 0;
  386. device = vfio_group_get_device(group, dev);
  387. if (device) {
  388. vfio_device_put(device);
  389. return 0;
  390. }
  391. return -EINVAL;
  392. }
  393. /**
  394. * Async device support
  395. */
  396. static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev)
  397. {
  398. struct vfio_device *device;
  399. /* Do we already know about it? We shouldn't */
  400. device = vfio_group_get_device(group, dev);
  401. if (WARN_ON_ONCE(device)) {
  402. vfio_device_put(device);
  403. return 0;
  404. }
  405. /* Nothing to do for idle groups */
  406. if (!atomic_read(&group->container_users))
  407. return 0;
  408. /* TODO Prevent device auto probing */
  409. WARN("Device %s added to live group %d!\n", dev_name(dev),
  410. iommu_group_id(group->iommu_group));
  411. return 0;
  412. }
  413. static int vfio_group_nb_del_dev(struct vfio_group *group, struct device *dev)
  414. {
  415. struct vfio_device *device;
  416. /*
  417. * Expect to fall out here. If a device was in use, it would
  418. * have been bound to a vfio sub-driver, which would have blocked
  419. * in .remove at vfio_del_group_dev. Sanity check that we no
  420. * longer track the device, so it's safe to remove.
  421. */
  422. device = vfio_group_get_device(group, dev);
  423. if (likely(!device))
  424. return 0;
  425. WARN("Device %s removed from live group %d!\n", dev_name(dev),
  426. iommu_group_id(group->iommu_group));
  427. vfio_device_put(device);
  428. return 0;
  429. }
  430. static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev)
  431. {
  432. /* We don't care what happens when the group isn't in use */
  433. if (!atomic_read(&group->container_users))
  434. return 0;
  435. return vfio_dev_viable(dev, group);
  436. }
  437. static int vfio_iommu_group_notifier(struct notifier_block *nb,
  438. unsigned long action, void *data)
  439. {
  440. struct vfio_group *group = container_of(nb, struct vfio_group, nb);
  441. struct device *dev = data;
  442. /*
  443. * Need to go through a group_lock lookup to get a reference or
  444. * we risk racing a group being removed. Leave a WARN_ON for
  445. * debuging, but if the group no longer exists, a spurious notify
  446. * is harmless.
  447. */
  448. group = vfio_group_try_get(group);
  449. if (WARN_ON(!group))
  450. return NOTIFY_OK;
  451. switch (action) {
  452. case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
  453. vfio_group_nb_add_dev(group, dev);
  454. break;
  455. case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
  456. vfio_group_nb_del_dev(group, dev);
  457. break;
  458. case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
  459. pr_debug("%s: Device %s, group %d binding to driver\n",
  460. __func__, dev_name(dev),
  461. iommu_group_id(group->iommu_group));
  462. break;
  463. case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
  464. pr_debug("%s: Device %s, group %d bound to driver %s\n",
  465. __func__, dev_name(dev),
  466. iommu_group_id(group->iommu_group), dev->driver->name);
  467. BUG_ON(vfio_group_nb_verify(group, dev));
  468. break;
  469. case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
  470. pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
  471. __func__, dev_name(dev),
  472. iommu_group_id(group->iommu_group), dev->driver->name);
  473. break;
  474. case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
  475. pr_debug("%s: Device %s, group %d unbound from driver\n",
  476. __func__, dev_name(dev),
  477. iommu_group_id(group->iommu_group));
  478. /*
  479. * XXX An unbound device in a live group is ok, but we'd
  480. * really like to avoid the above BUG_ON by preventing other
  481. * drivers from binding to it. Once that occurs, we have to
  482. * stop the system to maintain isolation. At a minimum, we'd
  483. * want a toggle to disable driver auto probe for this device.
  484. */
  485. break;
  486. }
  487. vfio_group_put(group);
  488. return NOTIFY_OK;
  489. }
  490. /**
  491. * VFIO driver API
  492. */
  493. int vfio_add_group_dev(struct device *dev,
  494. const struct vfio_device_ops *ops, void *device_data)
  495. {
  496. struct iommu_group *iommu_group;
  497. struct vfio_group *group;
  498. struct vfio_device *device;
  499. iommu_group = iommu_group_get(dev);
  500. if (!iommu_group)
  501. return -EINVAL;
  502. group = vfio_group_get_from_iommu(iommu_group);
  503. if (!group) {
  504. group = vfio_create_group(iommu_group);
  505. if (IS_ERR(group)) {
  506. iommu_group_put(iommu_group);
  507. return PTR_ERR(group);
  508. }
  509. }
  510. device = vfio_group_get_device(group, dev);
  511. if (device) {
  512. WARN(1, "Device %s already exists on group %d\n",
  513. dev_name(dev), iommu_group_id(iommu_group));
  514. vfio_device_put(device);
  515. vfio_group_put(group);
  516. iommu_group_put(iommu_group);
  517. return -EBUSY;
  518. }
  519. device = vfio_group_create_device(group, dev, ops, device_data);
  520. if (IS_ERR(device)) {
  521. vfio_group_put(group);
  522. iommu_group_put(iommu_group);
  523. return PTR_ERR(device);
  524. }
  525. /*
  526. * Added device holds reference to iommu_group and vfio_device
  527. * (which in turn holds reference to vfio_group). Drop extra
  528. * group reference used while acquiring device.
  529. */
  530. vfio_group_put(group);
  531. return 0;
  532. }
  533. EXPORT_SYMBOL_GPL(vfio_add_group_dev);
  534. /**
  535. * Get a reference to the vfio_device for a device that is known to
  536. * be bound to a vfio driver. The driver implicitly holds a
  537. * vfio_device reference between vfio_add_group_dev and
  538. * vfio_del_group_dev. We can therefore use drvdata to increment
  539. * that reference from the struct device. This additional
  540. * reference must be released by calling vfio_device_put.
  541. */
  542. struct vfio_device *vfio_device_get_from_dev(struct device *dev)
  543. {
  544. struct vfio_device *device = dev_get_drvdata(dev);
  545. vfio_device_get(device);
  546. return device;
  547. }
  548. EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
  549. /*
  550. * Caller must hold a reference to the vfio_device
  551. */
  552. void *vfio_device_data(struct vfio_device *device)
  553. {
  554. return device->device_data;
  555. }
  556. EXPORT_SYMBOL_GPL(vfio_device_data);
  557. /* Given a referenced group, check if it contains the device */
  558. static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
  559. {
  560. struct vfio_device *device;
  561. device = vfio_group_get_device(group, dev);
  562. if (!device)
  563. return false;
  564. vfio_device_put(device);
  565. return true;
  566. }
  567. /*
  568. * Decrement the device reference count and wait for the device to be
  569. * removed. Open file descriptors for the device... */
  570. void *vfio_del_group_dev(struct device *dev)
  571. {
  572. struct vfio_device *device = dev_get_drvdata(dev);
  573. struct vfio_group *group = device->group;
  574. struct iommu_group *iommu_group = group->iommu_group;
  575. void *device_data = device->device_data;
  576. /*
  577. * The group exists so long as we have a device reference. Get
  578. * a group reference and use it to scan for the device going away.
  579. */
  580. vfio_group_get(group);
  581. vfio_device_put(device);
  582. /* TODO send a signal to encourage this to be released */
  583. wait_event(vfio.release_q, !vfio_dev_present(group, dev));
  584. vfio_group_put(group);
  585. iommu_group_put(iommu_group);
  586. return device_data;
  587. }
  588. EXPORT_SYMBOL_GPL(vfio_del_group_dev);
  589. /**
  590. * VFIO base fd, /dev/vfio/vfio
  591. */
  592. static long vfio_ioctl_check_extension(struct vfio_container *container,
  593. unsigned long arg)
  594. {
  595. struct vfio_iommu_driver *driver;
  596. long ret = 0;
  597. down_read(&container->group_lock);
  598. driver = container->iommu_driver;
  599. switch (arg) {
  600. /* No base extensions yet */
  601. default:
  602. /*
  603. * If no driver is set, poll all registered drivers for
  604. * extensions and return the first positive result. If
  605. * a driver is already set, further queries will be passed
  606. * only to that driver.
  607. */
  608. if (!driver) {
  609. mutex_lock(&vfio.iommu_drivers_lock);
  610. list_for_each_entry(driver, &vfio.iommu_drivers_list,
  611. vfio_next) {
  612. if (!try_module_get(driver->ops->owner))
  613. continue;
  614. ret = driver->ops->ioctl(NULL,
  615. VFIO_CHECK_EXTENSION,
  616. arg);
  617. module_put(driver->ops->owner);
  618. if (ret > 0)
  619. break;
  620. }
  621. mutex_unlock(&vfio.iommu_drivers_lock);
  622. } else
  623. ret = driver->ops->ioctl(container->iommu_data,
  624. VFIO_CHECK_EXTENSION, arg);
  625. }
  626. up_read(&container->group_lock);
  627. return ret;
  628. }
  629. /* hold write lock on container->group_lock */
  630. static int __vfio_container_attach_groups(struct vfio_container *container,
  631. struct vfio_iommu_driver *driver,
  632. void *data)
  633. {
  634. struct vfio_group *group;
  635. int ret = -ENODEV;
  636. list_for_each_entry(group, &container->group_list, container_next) {
  637. ret = driver->ops->attach_group(data, group->iommu_group);
  638. if (ret)
  639. goto unwind;
  640. }
  641. return ret;
  642. unwind:
  643. list_for_each_entry_continue_reverse(group, &container->group_list,
  644. container_next) {
  645. driver->ops->detach_group(data, group->iommu_group);
  646. }
  647. return ret;
  648. }
  649. static long vfio_ioctl_set_iommu(struct vfio_container *container,
  650. unsigned long arg)
  651. {
  652. struct vfio_iommu_driver *driver;
  653. long ret = -ENODEV;
  654. down_write(&container->group_lock);
  655. /*
  656. * The container is designed to be an unprivileged interface while
  657. * the group can be assigned to specific users. Therefore, only by
  658. * adding a group to a container does the user get the privilege of
  659. * enabling the iommu, which may allocate finite resources. There
  660. * is no unset_iommu, but by removing all the groups from a container,
  661. * the container is deprivileged and returns to an unset state.
  662. */
  663. if (list_empty(&container->group_list) || container->iommu_driver) {
  664. up_write(&container->group_lock);
  665. return -EINVAL;
  666. }
  667. mutex_lock(&vfio.iommu_drivers_lock);
  668. list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
  669. void *data;
  670. if (!try_module_get(driver->ops->owner))
  671. continue;
  672. /*
  673. * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
  674. * so test which iommu driver reported support for this
  675. * extension and call open on them. We also pass them the
  676. * magic, allowing a single driver to support multiple
  677. * interfaces if they'd like.
  678. */
  679. if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
  680. module_put(driver->ops->owner);
  681. continue;
  682. }
  683. /* module reference holds the driver we're working on */
  684. mutex_unlock(&vfio.iommu_drivers_lock);
  685. data = driver->ops->open(arg);
  686. if (IS_ERR(data)) {
  687. ret = PTR_ERR(data);
  688. module_put(driver->ops->owner);
  689. goto skip_drivers_unlock;
  690. }
  691. ret = __vfio_container_attach_groups(container, driver, data);
  692. if (!ret) {
  693. container->iommu_driver = driver;
  694. container->iommu_data = data;
  695. } else {
  696. driver->ops->release(data);
  697. module_put(driver->ops->owner);
  698. }
  699. goto skip_drivers_unlock;
  700. }
  701. mutex_unlock(&vfio.iommu_drivers_lock);
  702. skip_drivers_unlock:
  703. up_write(&container->group_lock);
  704. return ret;
  705. }
  706. static long vfio_fops_unl_ioctl(struct file *filep,
  707. unsigned int cmd, unsigned long arg)
  708. {
  709. struct vfio_container *container = filep->private_data;
  710. struct vfio_iommu_driver *driver;
  711. void *data;
  712. long ret = -EINVAL;
  713. if (!container)
  714. return ret;
  715. switch (cmd) {
  716. case VFIO_GET_API_VERSION:
  717. ret = VFIO_API_VERSION;
  718. break;
  719. case VFIO_CHECK_EXTENSION:
  720. ret = vfio_ioctl_check_extension(container, arg);
  721. break;
  722. case VFIO_SET_IOMMU:
  723. ret = vfio_ioctl_set_iommu(container, arg);
  724. break;
  725. default:
  726. down_read(&container->group_lock);
  727. driver = container->iommu_driver;
  728. data = container->iommu_data;
  729. if (driver) /* passthrough all unrecognized ioctls */
  730. ret = driver->ops->ioctl(data, cmd, arg);
  731. up_read(&container->group_lock);
  732. }
  733. return ret;
  734. }
  735. #ifdef CONFIG_COMPAT
  736. static long vfio_fops_compat_ioctl(struct file *filep,
  737. unsigned int cmd, unsigned long arg)
  738. {
  739. arg = (unsigned long)compat_ptr(arg);
  740. return vfio_fops_unl_ioctl(filep, cmd, arg);
  741. }
  742. #endif /* CONFIG_COMPAT */
  743. static int vfio_fops_open(struct inode *inode, struct file *filep)
  744. {
  745. struct vfio_container *container;
  746. container = kzalloc(sizeof(*container), GFP_KERNEL);
  747. if (!container)
  748. return -ENOMEM;
  749. INIT_LIST_HEAD(&container->group_list);
  750. init_rwsem(&container->group_lock);
  751. kref_init(&container->kref);
  752. filep->private_data = container;
  753. return 0;
  754. }
  755. static int vfio_fops_release(struct inode *inode, struct file *filep)
  756. {
  757. struct vfio_container *container = filep->private_data;
  758. filep->private_data = NULL;
  759. vfio_container_put(container);
  760. return 0;
  761. }
  762. /*
  763. * Once an iommu driver is set, we optionally pass read/write/mmap
  764. * on to the driver, allowing management interfaces beyond ioctl.
  765. */
  766. static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
  767. size_t count, loff_t *ppos)
  768. {
  769. struct vfio_container *container = filep->private_data;
  770. struct vfio_iommu_driver *driver;
  771. ssize_t ret = -EINVAL;
  772. down_read(&container->group_lock);
  773. driver = container->iommu_driver;
  774. if (likely(driver && driver->ops->read))
  775. ret = driver->ops->read(container->iommu_data,
  776. buf, count, ppos);
  777. up_read(&container->group_lock);
  778. return ret;
  779. }
  780. static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
  781. size_t count, loff_t *ppos)
  782. {
  783. struct vfio_container *container = filep->private_data;
  784. struct vfio_iommu_driver *driver;
  785. ssize_t ret = -EINVAL;
  786. down_read(&container->group_lock);
  787. driver = container->iommu_driver;
  788. if (likely(driver && driver->ops->write))
  789. ret = driver->ops->write(container->iommu_data,
  790. buf, count, ppos);
  791. up_read(&container->group_lock);
  792. return ret;
  793. }
  794. static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  795. {
  796. struct vfio_container *container = filep->private_data;
  797. struct vfio_iommu_driver *driver;
  798. int ret = -EINVAL;
  799. down_read(&container->group_lock);
  800. driver = container->iommu_driver;
  801. if (likely(driver && driver->ops->mmap))
  802. ret = driver->ops->mmap(container->iommu_data, vma);
  803. up_read(&container->group_lock);
  804. return ret;
  805. }
  806. static const struct file_operations vfio_fops = {
  807. .owner = THIS_MODULE,
  808. .open = vfio_fops_open,
  809. .release = vfio_fops_release,
  810. .read = vfio_fops_read,
  811. .write = vfio_fops_write,
  812. .unlocked_ioctl = vfio_fops_unl_ioctl,
  813. #ifdef CONFIG_COMPAT
  814. .compat_ioctl = vfio_fops_compat_ioctl,
  815. #endif
  816. .mmap = vfio_fops_mmap,
  817. };
  818. /**
  819. * VFIO Group fd, /dev/vfio/$GROUP
  820. */
  821. static void __vfio_group_unset_container(struct vfio_group *group)
  822. {
  823. struct vfio_container *container = group->container;
  824. struct vfio_iommu_driver *driver;
  825. down_write(&container->group_lock);
  826. driver = container->iommu_driver;
  827. if (driver)
  828. driver->ops->detach_group(container->iommu_data,
  829. group->iommu_group);
  830. group->container = NULL;
  831. list_del(&group->container_next);
  832. /* Detaching the last group deprivileges a container, remove iommu */
  833. if (driver && list_empty(&container->group_list)) {
  834. driver->ops->release(container->iommu_data);
  835. module_put(driver->ops->owner);
  836. container->iommu_driver = NULL;
  837. container->iommu_data = NULL;
  838. }
  839. up_write(&container->group_lock);
  840. vfio_container_put(container);
  841. }
  842. /*
  843. * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
  844. * if there was no container to unset. Since the ioctl is called on
  845. * the group, we know that still exists, therefore the only valid
  846. * transition here is 1->0.
  847. */
  848. static int vfio_group_unset_container(struct vfio_group *group)
  849. {
  850. int users = atomic_cmpxchg(&group->container_users, 1, 0);
  851. if (!users)
  852. return -EINVAL;
  853. if (users != 1)
  854. return -EBUSY;
  855. __vfio_group_unset_container(group);
  856. return 0;
  857. }
  858. /*
  859. * When removing container users, anything that removes the last user
  860. * implicitly removes the group from the container. That is, if the
  861. * group file descriptor is closed, as well as any device file descriptors,
  862. * the group is free.
  863. */
  864. static void vfio_group_try_dissolve_container(struct vfio_group *group)
  865. {
  866. if (0 == atomic_dec_if_positive(&group->container_users))
  867. __vfio_group_unset_container(group);
  868. }
  869. static int vfio_group_set_container(struct vfio_group *group, int container_fd)
  870. {
  871. struct fd f;
  872. struct vfio_container *container;
  873. struct vfio_iommu_driver *driver;
  874. int ret = 0;
  875. if (atomic_read(&group->container_users))
  876. return -EINVAL;
  877. f = fdget(container_fd);
  878. if (!f.file)
  879. return -EBADF;
  880. /* Sanity check, is this really our fd? */
  881. if (f.file->f_op != &vfio_fops) {
  882. fdput(f);
  883. return -EINVAL;
  884. }
  885. container = f.file->private_data;
  886. WARN_ON(!container); /* fget ensures we don't race vfio_release */
  887. down_write(&container->group_lock);
  888. driver = container->iommu_driver;
  889. if (driver) {
  890. ret = driver->ops->attach_group(container->iommu_data,
  891. group->iommu_group);
  892. if (ret)
  893. goto unlock_out;
  894. }
  895. group->container = container;
  896. list_add(&group->container_next, &container->group_list);
  897. /* Get a reference on the container and mark a user within the group */
  898. vfio_container_get(container);
  899. atomic_inc(&group->container_users);
  900. unlock_out:
  901. up_write(&container->group_lock);
  902. fdput(f);
  903. return ret;
  904. }
  905. static bool vfio_group_viable(struct vfio_group *group)
  906. {
  907. return (iommu_group_for_each_dev(group->iommu_group,
  908. group, vfio_dev_viable) == 0);
  909. }
  910. static const struct file_operations vfio_device_fops;
  911. static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
  912. {
  913. struct vfio_device *device;
  914. struct file *filep;
  915. int ret = -ENODEV;
  916. if (0 == atomic_read(&group->container_users) ||
  917. !group->container->iommu_driver || !vfio_group_viable(group))
  918. return -EINVAL;
  919. mutex_lock(&group->device_lock);
  920. list_for_each_entry(device, &group->device_list, group_next) {
  921. if (strcmp(dev_name(device->dev), buf))
  922. continue;
  923. ret = device->ops->open(device->device_data);
  924. if (ret)
  925. break;
  926. /*
  927. * We can't use anon_inode_getfd() because we need to modify
  928. * the f_mode flags directly to allow more than just ioctls
  929. */
  930. ret = get_unused_fd();
  931. if (ret < 0) {
  932. device->ops->release(device->device_data);
  933. break;
  934. }
  935. filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
  936. device, O_RDWR);
  937. if (IS_ERR(filep)) {
  938. put_unused_fd(ret);
  939. ret = PTR_ERR(filep);
  940. device->ops->release(device->device_data);
  941. break;
  942. }
  943. /*
  944. * TODO: add an anon_inode interface to do this.
  945. * Appears to be missing by lack of need rather than
  946. * explicitly prevented. Now there's need.
  947. */
  948. filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  949. vfio_device_get(device);
  950. atomic_inc(&group->container_users);
  951. fd_install(ret, filep);
  952. break;
  953. }
  954. mutex_unlock(&group->device_lock);
  955. return ret;
  956. }
  957. static long vfio_group_fops_unl_ioctl(struct file *filep,
  958. unsigned int cmd, unsigned long arg)
  959. {
  960. struct vfio_group *group = filep->private_data;
  961. long ret = -ENOTTY;
  962. switch (cmd) {
  963. case VFIO_GROUP_GET_STATUS:
  964. {
  965. struct vfio_group_status status;
  966. unsigned long minsz;
  967. minsz = offsetofend(struct vfio_group_status, flags);
  968. if (copy_from_user(&status, (void __user *)arg, minsz))
  969. return -EFAULT;
  970. if (status.argsz < minsz)
  971. return -EINVAL;
  972. status.flags = 0;
  973. if (vfio_group_viable(group))
  974. status.flags |= VFIO_GROUP_FLAGS_VIABLE;
  975. if (group->container)
  976. status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
  977. if (copy_to_user((void __user *)arg, &status, minsz))
  978. return -EFAULT;
  979. ret = 0;
  980. break;
  981. }
  982. case VFIO_GROUP_SET_CONTAINER:
  983. {
  984. int fd;
  985. if (get_user(fd, (int __user *)arg))
  986. return -EFAULT;
  987. if (fd < 0)
  988. return -EINVAL;
  989. ret = vfio_group_set_container(group, fd);
  990. break;
  991. }
  992. case VFIO_GROUP_UNSET_CONTAINER:
  993. ret = vfio_group_unset_container(group);
  994. break;
  995. case VFIO_GROUP_GET_DEVICE_FD:
  996. {
  997. char *buf;
  998. buf = strndup_user((const char __user *)arg, PAGE_SIZE);
  999. if (IS_ERR(buf))
  1000. return PTR_ERR(buf);
  1001. ret = vfio_group_get_device_fd(group, buf);
  1002. kfree(buf);
  1003. break;
  1004. }
  1005. }
  1006. return ret;
  1007. }
  1008. #ifdef CONFIG_COMPAT
  1009. static long vfio_group_fops_compat_ioctl(struct file *filep,
  1010. unsigned int cmd, unsigned long arg)
  1011. {
  1012. arg = (unsigned long)compat_ptr(arg);
  1013. return vfio_group_fops_unl_ioctl(filep, cmd, arg);
  1014. }
  1015. #endif /* CONFIG_COMPAT */
  1016. static int vfio_group_fops_open(struct inode *inode, struct file *filep)
  1017. {
  1018. struct vfio_group *group;
  1019. group = vfio_group_get_from_minor(iminor(inode));
  1020. if (!group)
  1021. return -ENODEV;
  1022. if (group->container) {
  1023. vfio_group_put(group);
  1024. return -EBUSY;
  1025. }
  1026. filep->private_data = group;
  1027. return 0;
  1028. }
  1029. static int vfio_group_fops_release(struct inode *inode, struct file *filep)
  1030. {
  1031. struct vfio_group *group = filep->private_data;
  1032. filep->private_data = NULL;
  1033. vfio_group_try_dissolve_container(group);
  1034. vfio_group_put(group);
  1035. return 0;
  1036. }
  1037. static const struct file_operations vfio_group_fops = {
  1038. .owner = THIS_MODULE,
  1039. .unlocked_ioctl = vfio_group_fops_unl_ioctl,
  1040. #ifdef CONFIG_COMPAT
  1041. .compat_ioctl = vfio_group_fops_compat_ioctl,
  1042. #endif
  1043. .open = vfio_group_fops_open,
  1044. .release = vfio_group_fops_release,
  1045. };
  1046. /**
  1047. * VFIO Device fd
  1048. */
  1049. static int vfio_device_fops_release(struct inode *inode, struct file *filep)
  1050. {
  1051. struct vfio_device *device = filep->private_data;
  1052. device->ops->release(device->device_data);
  1053. vfio_group_try_dissolve_container(device->group);
  1054. vfio_device_put(device);
  1055. return 0;
  1056. }
  1057. static long vfio_device_fops_unl_ioctl(struct file *filep,
  1058. unsigned int cmd, unsigned long arg)
  1059. {
  1060. struct vfio_device *device = filep->private_data;
  1061. if (unlikely(!device->ops->ioctl))
  1062. return -EINVAL;
  1063. return device->ops->ioctl(device->device_data, cmd, arg);
  1064. }
  1065. static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
  1066. size_t count, loff_t *ppos)
  1067. {
  1068. struct vfio_device *device = filep->private_data;
  1069. if (unlikely(!device->ops->read))
  1070. return -EINVAL;
  1071. return device->ops->read(device->device_data, buf, count, ppos);
  1072. }
  1073. static ssize_t vfio_device_fops_write(struct file *filep,
  1074. const char __user *buf,
  1075. size_t count, loff_t *ppos)
  1076. {
  1077. struct vfio_device *device = filep->private_data;
  1078. if (unlikely(!device->ops->write))
  1079. return -EINVAL;
  1080. return device->ops->write(device->device_data, buf, count, ppos);
  1081. }
  1082. static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  1083. {
  1084. struct vfio_device *device = filep->private_data;
  1085. if (unlikely(!device->ops->mmap))
  1086. return -EINVAL;
  1087. return device->ops->mmap(device->device_data, vma);
  1088. }
  1089. #ifdef CONFIG_COMPAT
  1090. static long vfio_device_fops_compat_ioctl(struct file *filep,
  1091. unsigned int cmd, unsigned long arg)
  1092. {
  1093. arg = (unsigned long)compat_ptr(arg);
  1094. return vfio_device_fops_unl_ioctl(filep, cmd, arg);
  1095. }
  1096. #endif /* CONFIG_COMPAT */
  1097. static const struct file_operations vfio_device_fops = {
  1098. .owner = THIS_MODULE,
  1099. .release = vfio_device_fops_release,
  1100. .read = vfio_device_fops_read,
  1101. .write = vfio_device_fops_write,
  1102. .unlocked_ioctl = vfio_device_fops_unl_ioctl,
  1103. #ifdef CONFIG_COMPAT
  1104. .compat_ioctl = vfio_device_fops_compat_ioctl,
  1105. #endif
  1106. .mmap = vfio_device_fops_mmap,
  1107. };
  1108. /**
  1109. * Module/class support
  1110. */
  1111. static char *vfio_devnode(struct device *dev, umode_t *mode)
  1112. {
  1113. if (MINOR(dev->devt) == 0)
  1114. *mode = S_IRUGO | S_IWUGO;
  1115. return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
  1116. }
  1117. static int __init vfio_init(void)
  1118. {
  1119. int ret;
  1120. idr_init(&vfio.group_idr);
  1121. mutex_init(&vfio.group_lock);
  1122. mutex_init(&vfio.iommu_drivers_lock);
  1123. INIT_LIST_HEAD(&vfio.group_list);
  1124. INIT_LIST_HEAD(&vfio.iommu_drivers_list);
  1125. init_waitqueue_head(&vfio.release_q);
  1126. vfio.class = class_create(THIS_MODULE, "vfio");
  1127. if (IS_ERR(vfio.class)) {
  1128. ret = PTR_ERR(vfio.class);
  1129. goto err_class;
  1130. }
  1131. vfio.class->devnode = vfio_devnode;
  1132. ret = alloc_chrdev_region(&vfio.devt, 0, MINORMASK, "vfio");
  1133. if (ret)
  1134. goto err_base_chrdev;
  1135. cdev_init(&vfio.cdev, &vfio_fops);
  1136. ret = cdev_add(&vfio.cdev, vfio.devt, 1);
  1137. if (ret)
  1138. goto err_base_cdev;
  1139. vfio.dev = device_create(vfio.class, NULL, vfio.devt, NULL, "vfio");
  1140. if (IS_ERR(vfio.dev)) {
  1141. ret = PTR_ERR(vfio.dev);
  1142. goto err_base_dev;
  1143. }
  1144. /* /dev/vfio/$GROUP */
  1145. cdev_init(&vfio.group_cdev, &vfio_group_fops);
  1146. ret = cdev_add(&vfio.group_cdev,
  1147. MKDEV(MAJOR(vfio.devt), 1), MINORMASK - 1);
  1148. if (ret)
  1149. goto err_groups_cdev;
  1150. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  1151. /*
  1152. * Attempt to load known iommu-drivers. This gives us a working
  1153. * environment without the user needing to explicitly load iommu
  1154. * drivers.
  1155. */
  1156. request_module_nowait("vfio_iommu_type1");
  1157. return 0;
  1158. err_groups_cdev:
  1159. device_destroy(vfio.class, vfio.devt);
  1160. err_base_dev:
  1161. cdev_del(&vfio.cdev);
  1162. err_base_cdev:
  1163. unregister_chrdev_region(vfio.devt, MINORMASK);
  1164. err_base_chrdev:
  1165. class_destroy(vfio.class);
  1166. vfio.class = NULL;
  1167. err_class:
  1168. return ret;
  1169. }
  1170. static void __exit vfio_cleanup(void)
  1171. {
  1172. WARN_ON(!list_empty(&vfio.group_list));
  1173. idr_destroy(&vfio.group_idr);
  1174. cdev_del(&vfio.group_cdev);
  1175. device_destroy(vfio.class, vfio.devt);
  1176. cdev_del(&vfio.cdev);
  1177. unregister_chrdev_region(vfio.devt, MINORMASK);
  1178. class_destroy(vfio.class);
  1179. vfio.class = NULL;
  1180. }
  1181. module_init(vfio_init);
  1182. module_exit(vfio_cleanup);
  1183. MODULE_VERSION(DRIVER_VERSION);
  1184. MODULE_LICENSE("GPL v2");
  1185. MODULE_AUTHOR(DRIVER_AUTHOR);
  1186. MODULE_DESCRIPTION(DRIVER_DESC);