vfio.c 35 KB

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