vfio.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  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_verify(struct vfio_group *group, struct device *dev)
  416. {
  417. /* We don't care what happens when the group isn't in use */
  418. if (!atomic_read(&group->container_users))
  419. return 0;
  420. return vfio_dev_viable(dev, group);
  421. }
  422. static int vfio_iommu_group_notifier(struct notifier_block *nb,
  423. unsigned long action, void *data)
  424. {
  425. struct vfio_group *group = container_of(nb, struct vfio_group, nb);
  426. struct device *dev = data;
  427. /*
  428. * Need to go through a group_lock lookup to get a reference or we
  429. * risk racing a group being removed. Ignore spurious notifies.
  430. */
  431. group = vfio_group_try_get(group);
  432. if (!group)
  433. return NOTIFY_OK;
  434. switch (action) {
  435. case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
  436. vfio_group_nb_add_dev(group, dev);
  437. break;
  438. case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
  439. /*
  440. * Nothing to do here. If the device is in use, then the
  441. * vfio sub-driver should block the remove callback until
  442. * it is unused. If the device is unused or attached to a
  443. * stub driver, then it should be released and we don't
  444. * care that it will be going away.
  445. */
  446. break;
  447. case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
  448. pr_debug("%s: Device %s, group %d binding to driver\n",
  449. __func__, dev_name(dev),
  450. iommu_group_id(group->iommu_group));
  451. break;
  452. case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
  453. pr_debug("%s: Device %s, group %d bound to driver %s\n",
  454. __func__, dev_name(dev),
  455. iommu_group_id(group->iommu_group), dev->driver->name);
  456. BUG_ON(vfio_group_nb_verify(group, dev));
  457. break;
  458. case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
  459. pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
  460. __func__, dev_name(dev),
  461. iommu_group_id(group->iommu_group), dev->driver->name);
  462. break;
  463. case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
  464. pr_debug("%s: Device %s, group %d unbound from driver\n",
  465. __func__, dev_name(dev),
  466. iommu_group_id(group->iommu_group));
  467. /*
  468. * XXX An unbound device in a live group is ok, but we'd
  469. * really like to avoid the above BUG_ON by preventing other
  470. * drivers from binding to it. Once that occurs, we have to
  471. * stop the system to maintain isolation. At a minimum, we'd
  472. * want a toggle to disable driver auto probe for this device.
  473. */
  474. break;
  475. }
  476. vfio_group_put(group);
  477. return NOTIFY_OK;
  478. }
  479. /**
  480. * VFIO driver API
  481. */
  482. int vfio_add_group_dev(struct device *dev,
  483. const struct vfio_device_ops *ops, void *device_data)
  484. {
  485. struct iommu_group *iommu_group;
  486. struct vfio_group *group;
  487. struct vfio_device *device;
  488. iommu_group = iommu_group_get(dev);
  489. if (!iommu_group)
  490. return -EINVAL;
  491. group = vfio_group_get_from_iommu(iommu_group);
  492. if (!group) {
  493. group = vfio_create_group(iommu_group);
  494. if (IS_ERR(group)) {
  495. iommu_group_put(iommu_group);
  496. return PTR_ERR(group);
  497. }
  498. }
  499. device = vfio_group_get_device(group, dev);
  500. if (device) {
  501. WARN(1, "Device %s already exists on group %d\n",
  502. dev_name(dev), iommu_group_id(iommu_group));
  503. vfio_device_put(device);
  504. vfio_group_put(group);
  505. iommu_group_put(iommu_group);
  506. return -EBUSY;
  507. }
  508. device = vfio_group_create_device(group, dev, ops, device_data);
  509. if (IS_ERR(device)) {
  510. vfio_group_put(group);
  511. iommu_group_put(iommu_group);
  512. return PTR_ERR(device);
  513. }
  514. /*
  515. * Added device holds reference to iommu_group and vfio_device
  516. * (which in turn holds reference to vfio_group). Drop extra
  517. * group reference used while acquiring device.
  518. */
  519. vfio_group_put(group);
  520. return 0;
  521. }
  522. EXPORT_SYMBOL_GPL(vfio_add_group_dev);
  523. /**
  524. * Get a reference to the vfio_device for a device that is known to
  525. * be bound to a vfio driver. The driver implicitly holds a
  526. * vfio_device reference between vfio_add_group_dev and
  527. * vfio_del_group_dev. We can therefore use drvdata to increment
  528. * that reference from the struct device. This additional
  529. * reference must be released by calling vfio_device_put.
  530. */
  531. struct vfio_device *vfio_device_get_from_dev(struct device *dev)
  532. {
  533. struct vfio_device *device = dev_get_drvdata(dev);
  534. vfio_device_get(device);
  535. return device;
  536. }
  537. EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
  538. /*
  539. * Caller must hold a reference to the vfio_device
  540. */
  541. void *vfio_device_data(struct vfio_device *device)
  542. {
  543. return device->device_data;
  544. }
  545. EXPORT_SYMBOL_GPL(vfio_device_data);
  546. /* Given a referenced group, check if it contains the device */
  547. static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
  548. {
  549. struct vfio_device *device;
  550. device = vfio_group_get_device(group, dev);
  551. if (!device)
  552. return false;
  553. vfio_device_put(device);
  554. return true;
  555. }
  556. /*
  557. * Decrement the device reference count and wait for the device to be
  558. * removed. Open file descriptors for the device... */
  559. void *vfio_del_group_dev(struct device *dev)
  560. {
  561. struct vfio_device *device = dev_get_drvdata(dev);
  562. struct vfio_group *group = device->group;
  563. struct iommu_group *iommu_group = group->iommu_group;
  564. void *device_data = device->device_data;
  565. /*
  566. * The group exists so long as we have a device reference. Get
  567. * a group reference and use it to scan for the device going away.
  568. */
  569. vfio_group_get(group);
  570. vfio_device_put(device);
  571. /* TODO send a signal to encourage this to be released */
  572. wait_event(vfio.release_q, !vfio_dev_present(group, dev));
  573. vfio_group_put(group);
  574. iommu_group_put(iommu_group);
  575. return device_data;
  576. }
  577. EXPORT_SYMBOL_GPL(vfio_del_group_dev);
  578. /**
  579. * VFIO base fd, /dev/vfio/vfio
  580. */
  581. static long vfio_ioctl_check_extension(struct vfio_container *container,
  582. unsigned long arg)
  583. {
  584. struct vfio_iommu_driver *driver;
  585. long ret = 0;
  586. down_read(&container->group_lock);
  587. driver = container->iommu_driver;
  588. switch (arg) {
  589. /* No base extensions yet */
  590. default:
  591. /*
  592. * If no driver is set, poll all registered drivers for
  593. * extensions and return the first positive result. If
  594. * a driver is already set, further queries will be passed
  595. * only to that driver.
  596. */
  597. if (!driver) {
  598. mutex_lock(&vfio.iommu_drivers_lock);
  599. list_for_each_entry(driver, &vfio.iommu_drivers_list,
  600. vfio_next) {
  601. if (!try_module_get(driver->ops->owner))
  602. continue;
  603. ret = driver->ops->ioctl(NULL,
  604. VFIO_CHECK_EXTENSION,
  605. arg);
  606. module_put(driver->ops->owner);
  607. if (ret > 0)
  608. break;
  609. }
  610. mutex_unlock(&vfio.iommu_drivers_lock);
  611. } else
  612. ret = driver->ops->ioctl(container->iommu_data,
  613. VFIO_CHECK_EXTENSION, arg);
  614. }
  615. up_read(&container->group_lock);
  616. return ret;
  617. }
  618. /* hold write lock on container->group_lock */
  619. static int __vfio_container_attach_groups(struct vfio_container *container,
  620. struct vfio_iommu_driver *driver,
  621. void *data)
  622. {
  623. struct vfio_group *group;
  624. int ret = -ENODEV;
  625. list_for_each_entry(group, &container->group_list, container_next) {
  626. ret = driver->ops->attach_group(data, group->iommu_group);
  627. if (ret)
  628. goto unwind;
  629. }
  630. return ret;
  631. unwind:
  632. list_for_each_entry_continue_reverse(group, &container->group_list,
  633. container_next) {
  634. driver->ops->detach_group(data, group->iommu_group);
  635. }
  636. return ret;
  637. }
  638. static long vfio_ioctl_set_iommu(struct vfio_container *container,
  639. unsigned long arg)
  640. {
  641. struct vfio_iommu_driver *driver;
  642. long ret = -ENODEV;
  643. down_write(&container->group_lock);
  644. /*
  645. * The container is designed to be an unprivileged interface while
  646. * the group can be assigned to specific users. Therefore, only by
  647. * adding a group to a container does the user get the privilege of
  648. * enabling the iommu, which may allocate finite resources. There
  649. * is no unset_iommu, but by removing all the groups from a container,
  650. * the container is deprivileged and returns to an unset state.
  651. */
  652. if (list_empty(&container->group_list) || container->iommu_driver) {
  653. up_write(&container->group_lock);
  654. return -EINVAL;
  655. }
  656. mutex_lock(&vfio.iommu_drivers_lock);
  657. list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
  658. void *data;
  659. if (!try_module_get(driver->ops->owner))
  660. continue;
  661. /*
  662. * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
  663. * so test which iommu driver reported support for this
  664. * extension and call open on them. We also pass them the
  665. * magic, allowing a single driver to support multiple
  666. * interfaces if they'd like.
  667. */
  668. if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
  669. module_put(driver->ops->owner);
  670. continue;
  671. }
  672. /* module reference holds the driver we're working on */
  673. mutex_unlock(&vfio.iommu_drivers_lock);
  674. data = driver->ops->open(arg);
  675. if (IS_ERR(data)) {
  676. ret = PTR_ERR(data);
  677. module_put(driver->ops->owner);
  678. goto skip_drivers_unlock;
  679. }
  680. ret = __vfio_container_attach_groups(container, driver, data);
  681. if (!ret) {
  682. container->iommu_driver = driver;
  683. container->iommu_data = data;
  684. } else {
  685. driver->ops->release(data);
  686. module_put(driver->ops->owner);
  687. }
  688. goto skip_drivers_unlock;
  689. }
  690. mutex_unlock(&vfio.iommu_drivers_lock);
  691. skip_drivers_unlock:
  692. up_write(&container->group_lock);
  693. return ret;
  694. }
  695. static long vfio_fops_unl_ioctl(struct file *filep,
  696. unsigned int cmd, unsigned long arg)
  697. {
  698. struct vfio_container *container = filep->private_data;
  699. struct vfio_iommu_driver *driver;
  700. void *data;
  701. long ret = -EINVAL;
  702. if (!container)
  703. return ret;
  704. switch (cmd) {
  705. case VFIO_GET_API_VERSION:
  706. ret = VFIO_API_VERSION;
  707. break;
  708. case VFIO_CHECK_EXTENSION:
  709. ret = vfio_ioctl_check_extension(container, arg);
  710. break;
  711. case VFIO_SET_IOMMU:
  712. ret = vfio_ioctl_set_iommu(container, arg);
  713. break;
  714. default:
  715. down_read(&container->group_lock);
  716. driver = container->iommu_driver;
  717. data = container->iommu_data;
  718. if (driver) /* passthrough all unrecognized ioctls */
  719. ret = driver->ops->ioctl(data, cmd, arg);
  720. up_read(&container->group_lock);
  721. }
  722. return ret;
  723. }
  724. #ifdef CONFIG_COMPAT
  725. static long vfio_fops_compat_ioctl(struct file *filep,
  726. unsigned int cmd, unsigned long arg)
  727. {
  728. arg = (unsigned long)compat_ptr(arg);
  729. return vfio_fops_unl_ioctl(filep, cmd, arg);
  730. }
  731. #endif /* CONFIG_COMPAT */
  732. static int vfio_fops_open(struct inode *inode, struct file *filep)
  733. {
  734. struct vfio_container *container;
  735. container = kzalloc(sizeof(*container), GFP_KERNEL);
  736. if (!container)
  737. return -ENOMEM;
  738. INIT_LIST_HEAD(&container->group_list);
  739. init_rwsem(&container->group_lock);
  740. kref_init(&container->kref);
  741. filep->private_data = container;
  742. return 0;
  743. }
  744. static int vfio_fops_release(struct inode *inode, struct file *filep)
  745. {
  746. struct vfio_container *container = filep->private_data;
  747. filep->private_data = NULL;
  748. vfio_container_put(container);
  749. return 0;
  750. }
  751. /*
  752. * Once an iommu driver is set, we optionally pass read/write/mmap
  753. * on to the driver, allowing management interfaces beyond ioctl.
  754. */
  755. static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
  756. size_t count, loff_t *ppos)
  757. {
  758. struct vfio_container *container = filep->private_data;
  759. struct vfio_iommu_driver *driver;
  760. ssize_t ret = -EINVAL;
  761. down_read(&container->group_lock);
  762. driver = container->iommu_driver;
  763. if (likely(driver && driver->ops->read))
  764. ret = driver->ops->read(container->iommu_data,
  765. buf, count, ppos);
  766. up_read(&container->group_lock);
  767. return ret;
  768. }
  769. static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
  770. size_t count, loff_t *ppos)
  771. {
  772. struct vfio_container *container = filep->private_data;
  773. struct vfio_iommu_driver *driver;
  774. ssize_t ret = -EINVAL;
  775. down_read(&container->group_lock);
  776. driver = container->iommu_driver;
  777. if (likely(driver && driver->ops->write))
  778. ret = driver->ops->write(container->iommu_data,
  779. buf, count, ppos);
  780. up_read(&container->group_lock);
  781. return ret;
  782. }
  783. static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  784. {
  785. struct vfio_container *container = filep->private_data;
  786. struct vfio_iommu_driver *driver;
  787. int ret = -EINVAL;
  788. down_read(&container->group_lock);
  789. driver = container->iommu_driver;
  790. if (likely(driver && driver->ops->mmap))
  791. ret = driver->ops->mmap(container->iommu_data, vma);
  792. up_read(&container->group_lock);
  793. return ret;
  794. }
  795. static const struct file_operations vfio_fops = {
  796. .owner = THIS_MODULE,
  797. .open = vfio_fops_open,
  798. .release = vfio_fops_release,
  799. .read = vfio_fops_read,
  800. .write = vfio_fops_write,
  801. .unlocked_ioctl = vfio_fops_unl_ioctl,
  802. #ifdef CONFIG_COMPAT
  803. .compat_ioctl = vfio_fops_compat_ioctl,
  804. #endif
  805. .mmap = vfio_fops_mmap,
  806. };
  807. /**
  808. * VFIO Group fd, /dev/vfio/$GROUP
  809. */
  810. static void __vfio_group_unset_container(struct vfio_group *group)
  811. {
  812. struct vfio_container *container = group->container;
  813. struct vfio_iommu_driver *driver;
  814. down_write(&container->group_lock);
  815. driver = container->iommu_driver;
  816. if (driver)
  817. driver->ops->detach_group(container->iommu_data,
  818. group->iommu_group);
  819. group->container = NULL;
  820. list_del(&group->container_next);
  821. /* Detaching the last group deprivileges a container, remove iommu */
  822. if (driver && list_empty(&container->group_list)) {
  823. driver->ops->release(container->iommu_data);
  824. module_put(driver->ops->owner);
  825. container->iommu_driver = NULL;
  826. container->iommu_data = NULL;
  827. }
  828. up_write(&container->group_lock);
  829. vfio_container_put(container);
  830. }
  831. /*
  832. * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
  833. * if there was no container to unset. Since the ioctl is called on
  834. * the group, we know that still exists, therefore the only valid
  835. * transition here is 1->0.
  836. */
  837. static int vfio_group_unset_container(struct vfio_group *group)
  838. {
  839. int users = atomic_cmpxchg(&group->container_users, 1, 0);
  840. if (!users)
  841. return -EINVAL;
  842. if (users != 1)
  843. return -EBUSY;
  844. __vfio_group_unset_container(group);
  845. return 0;
  846. }
  847. /*
  848. * When removing container users, anything that removes the last user
  849. * implicitly removes the group from the container. That is, if the
  850. * group file descriptor is closed, as well as any device file descriptors,
  851. * the group is free.
  852. */
  853. static void vfio_group_try_dissolve_container(struct vfio_group *group)
  854. {
  855. if (0 == atomic_dec_if_positive(&group->container_users))
  856. __vfio_group_unset_container(group);
  857. }
  858. static int vfio_group_set_container(struct vfio_group *group, int container_fd)
  859. {
  860. struct fd f;
  861. struct vfio_container *container;
  862. struct vfio_iommu_driver *driver;
  863. int ret = 0;
  864. if (atomic_read(&group->container_users))
  865. return -EINVAL;
  866. f = fdget(container_fd);
  867. if (!f.file)
  868. return -EBADF;
  869. /* Sanity check, is this really our fd? */
  870. if (f.file->f_op != &vfio_fops) {
  871. fdput(f);
  872. return -EINVAL;
  873. }
  874. container = f.file->private_data;
  875. WARN_ON(!container); /* fget ensures we don't race vfio_release */
  876. down_write(&container->group_lock);
  877. driver = container->iommu_driver;
  878. if (driver) {
  879. ret = driver->ops->attach_group(container->iommu_data,
  880. group->iommu_group);
  881. if (ret)
  882. goto unlock_out;
  883. }
  884. group->container = container;
  885. list_add(&group->container_next, &container->group_list);
  886. /* Get a reference on the container and mark a user within the group */
  887. vfio_container_get(container);
  888. atomic_inc(&group->container_users);
  889. unlock_out:
  890. up_write(&container->group_lock);
  891. fdput(f);
  892. return ret;
  893. }
  894. static bool vfio_group_viable(struct vfio_group *group)
  895. {
  896. return (iommu_group_for_each_dev(group->iommu_group,
  897. group, vfio_dev_viable) == 0);
  898. }
  899. static const struct file_operations vfio_device_fops;
  900. static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
  901. {
  902. struct vfio_device *device;
  903. struct file *filep;
  904. int ret = -ENODEV;
  905. if (0 == atomic_read(&group->container_users) ||
  906. !group->container->iommu_driver || !vfio_group_viable(group))
  907. return -EINVAL;
  908. mutex_lock(&group->device_lock);
  909. list_for_each_entry(device, &group->device_list, group_next) {
  910. if (strcmp(dev_name(device->dev), buf))
  911. continue;
  912. ret = device->ops->open(device->device_data);
  913. if (ret)
  914. break;
  915. /*
  916. * We can't use anon_inode_getfd() because we need to modify
  917. * the f_mode flags directly to allow more than just ioctls
  918. */
  919. ret = get_unused_fd_flags(O_CLOEXEC);
  920. if (ret < 0) {
  921. device->ops->release(device->device_data);
  922. break;
  923. }
  924. filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
  925. device, O_RDWR);
  926. if (IS_ERR(filep)) {
  927. put_unused_fd(ret);
  928. ret = PTR_ERR(filep);
  929. device->ops->release(device->device_data);
  930. break;
  931. }
  932. /*
  933. * TODO: add an anon_inode interface to do this.
  934. * Appears to be missing by lack of need rather than
  935. * explicitly prevented. Now there's need.
  936. */
  937. filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  938. vfio_device_get(device);
  939. atomic_inc(&group->container_users);
  940. fd_install(ret, filep);
  941. break;
  942. }
  943. mutex_unlock(&group->device_lock);
  944. return ret;
  945. }
  946. static long vfio_group_fops_unl_ioctl(struct file *filep,
  947. unsigned int cmd, unsigned long arg)
  948. {
  949. struct vfio_group *group = filep->private_data;
  950. long ret = -ENOTTY;
  951. switch (cmd) {
  952. case VFIO_GROUP_GET_STATUS:
  953. {
  954. struct vfio_group_status status;
  955. unsigned long minsz;
  956. minsz = offsetofend(struct vfio_group_status, flags);
  957. if (copy_from_user(&status, (void __user *)arg, minsz))
  958. return -EFAULT;
  959. if (status.argsz < minsz)
  960. return -EINVAL;
  961. status.flags = 0;
  962. if (vfio_group_viable(group))
  963. status.flags |= VFIO_GROUP_FLAGS_VIABLE;
  964. if (group->container)
  965. status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
  966. if (copy_to_user((void __user *)arg, &status, minsz))
  967. return -EFAULT;
  968. ret = 0;
  969. break;
  970. }
  971. case VFIO_GROUP_SET_CONTAINER:
  972. {
  973. int fd;
  974. if (get_user(fd, (int __user *)arg))
  975. return -EFAULT;
  976. if (fd < 0)
  977. return -EINVAL;
  978. ret = vfio_group_set_container(group, fd);
  979. break;
  980. }
  981. case VFIO_GROUP_UNSET_CONTAINER:
  982. ret = vfio_group_unset_container(group);
  983. break;
  984. case VFIO_GROUP_GET_DEVICE_FD:
  985. {
  986. char *buf;
  987. buf = strndup_user((const char __user *)arg, PAGE_SIZE);
  988. if (IS_ERR(buf))
  989. return PTR_ERR(buf);
  990. ret = vfio_group_get_device_fd(group, buf);
  991. kfree(buf);
  992. break;
  993. }
  994. }
  995. return ret;
  996. }
  997. #ifdef CONFIG_COMPAT
  998. static long vfio_group_fops_compat_ioctl(struct file *filep,
  999. unsigned int cmd, unsigned long arg)
  1000. {
  1001. arg = (unsigned long)compat_ptr(arg);
  1002. return vfio_group_fops_unl_ioctl(filep, cmd, arg);
  1003. }
  1004. #endif /* CONFIG_COMPAT */
  1005. static int vfio_group_fops_open(struct inode *inode, struct file *filep)
  1006. {
  1007. struct vfio_group *group;
  1008. int opened;
  1009. group = vfio_group_get_from_minor(iminor(inode));
  1010. if (!group)
  1011. return -ENODEV;
  1012. /* Do we need multiple instances of the group open? Seems not. */
  1013. opened = atomic_cmpxchg(&group->opened, 0, 1);
  1014. if (opened) {
  1015. vfio_group_put(group);
  1016. return -EBUSY;
  1017. }
  1018. /* Is something still in use from a previous open? */
  1019. if (group->container) {
  1020. atomic_dec(&group->opened);
  1021. vfio_group_put(group);
  1022. return -EBUSY;
  1023. }
  1024. filep->private_data = group;
  1025. return 0;
  1026. }
  1027. static int vfio_group_fops_release(struct inode *inode, struct file *filep)
  1028. {
  1029. struct vfio_group *group = filep->private_data;
  1030. filep->private_data = NULL;
  1031. vfio_group_try_dissolve_container(group);
  1032. atomic_dec(&group->opened);
  1033. vfio_group_put(group);
  1034. return 0;
  1035. }
  1036. static const struct file_operations vfio_group_fops = {
  1037. .owner = THIS_MODULE,
  1038. .unlocked_ioctl = vfio_group_fops_unl_ioctl,
  1039. #ifdef CONFIG_COMPAT
  1040. .compat_ioctl = vfio_group_fops_compat_ioctl,
  1041. #endif
  1042. .open = vfio_group_fops_open,
  1043. .release = vfio_group_fops_release,
  1044. };
  1045. /**
  1046. * VFIO Device fd
  1047. */
  1048. static int vfio_device_fops_release(struct inode *inode, struct file *filep)
  1049. {
  1050. struct vfio_device *device = filep->private_data;
  1051. device->ops->release(device->device_data);
  1052. vfio_group_try_dissolve_container(device->group);
  1053. vfio_device_put(device);
  1054. return 0;
  1055. }
  1056. static long vfio_device_fops_unl_ioctl(struct file *filep,
  1057. unsigned int cmd, unsigned long arg)
  1058. {
  1059. struct vfio_device *device = filep->private_data;
  1060. if (unlikely(!device->ops->ioctl))
  1061. return -EINVAL;
  1062. return device->ops->ioctl(device->device_data, cmd, arg);
  1063. }
  1064. static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
  1065. size_t count, loff_t *ppos)
  1066. {
  1067. struct vfio_device *device = filep->private_data;
  1068. if (unlikely(!device->ops->read))
  1069. return -EINVAL;
  1070. return device->ops->read(device->device_data, buf, count, ppos);
  1071. }
  1072. static ssize_t vfio_device_fops_write(struct file *filep,
  1073. const char __user *buf,
  1074. size_t count, loff_t *ppos)
  1075. {
  1076. struct vfio_device *device = filep->private_data;
  1077. if (unlikely(!device->ops->write))
  1078. return -EINVAL;
  1079. return device->ops->write(device->device_data, buf, count, ppos);
  1080. }
  1081. static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  1082. {
  1083. struct vfio_device *device = filep->private_data;
  1084. if (unlikely(!device->ops->mmap))
  1085. return -EINVAL;
  1086. return device->ops->mmap(device->device_data, vma);
  1087. }
  1088. #ifdef CONFIG_COMPAT
  1089. static long vfio_device_fops_compat_ioctl(struct file *filep,
  1090. unsigned int cmd, unsigned long arg)
  1091. {
  1092. arg = (unsigned long)compat_ptr(arg);
  1093. return vfio_device_fops_unl_ioctl(filep, cmd, arg);
  1094. }
  1095. #endif /* CONFIG_COMPAT */
  1096. static const struct file_operations vfio_device_fops = {
  1097. .owner = THIS_MODULE,
  1098. .release = vfio_device_fops_release,
  1099. .read = vfio_device_fops_read,
  1100. .write = vfio_device_fops_write,
  1101. .unlocked_ioctl = vfio_device_fops_unl_ioctl,
  1102. #ifdef CONFIG_COMPAT
  1103. .compat_ioctl = vfio_device_fops_compat_ioctl,
  1104. #endif
  1105. .mmap = vfio_device_fops_mmap,
  1106. };
  1107. /**
  1108. * External user API, exported by symbols to be linked dynamically.
  1109. *
  1110. * The protocol includes:
  1111. * 1. do normal VFIO init operation:
  1112. * - opening a new container;
  1113. * - attaching group(s) to it;
  1114. * - setting an IOMMU driver for a container.
  1115. * When IOMMU is set for a container, all groups in it are
  1116. * considered ready to use by an external user.
  1117. *
  1118. * 2. User space passes a group fd to an external user.
  1119. * The external user calls vfio_group_get_external_user()
  1120. * to verify that:
  1121. * - the group is initialized;
  1122. * - IOMMU is set for it.
  1123. * If both checks passed, vfio_group_get_external_user()
  1124. * increments the container user counter to prevent
  1125. * the VFIO group from disposal before KVM exits.
  1126. *
  1127. * 3. The external user calls vfio_external_user_iommu_id()
  1128. * to know an IOMMU ID.
  1129. *
  1130. * 4. When the external KVM finishes, it calls
  1131. * vfio_group_put_external_user() to release the VFIO group.
  1132. * This call decrements the container user counter.
  1133. */
  1134. struct vfio_group *vfio_group_get_external_user(struct file *filep)
  1135. {
  1136. struct vfio_group *group = filep->private_data;
  1137. if (filep->f_op != &vfio_group_fops)
  1138. return ERR_PTR(-EINVAL);
  1139. if (!atomic_inc_not_zero(&group->container_users))
  1140. return ERR_PTR(-EINVAL);
  1141. if (!group->container->iommu_driver ||
  1142. !vfio_group_viable(group)) {
  1143. atomic_dec(&group->container_users);
  1144. return ERR_PTR(-EINVAL);
  1145. }
  1146. vfio_group_get(group);
  1147. return group;
  1148. }
  1149. EXPORT_SYMBOL_GPL(vfio_group_get_external_user);
  1150. void vfio_group_put_external_user(struct vfio_group *group)
  1151. {
  1152. vfio_group_put(group);
  1153. vfio_group_try_dissolve_container(group);
  1154. }
  1155. EXPORT_SYMBOL_GPL(vfio_group_put_external_user);
  1156. int vfio_external_user_iommu_id(struct vfio_group *group)
  1157. {
  1158. return iommu_group_id(group->iommu_group);
  1159. }
  1160. EXPORT_SYMBOL_GPL(vfio_external_user_iommu_id);
  1161. /**
  1162. * Module/class support
  1163. */
  1164. static char *vfio_devnode(struct device *dev, umode_t *mode)
  1165. {
  1166. if (mode && (MINOR(dev->devt) == 0))
  1167. *mode = S_IRUGO | S_IWUGO;
  1168. return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
  1169. }
  1170. static int __init vfio_init(void)
  1171. {
  1172. int ret;
  1173. idr_init(&vfio.group_idr);
  1174. mutex_init(&vfio.group_lock);
  1175. mutex_init(&vfio.iommu_drivers_lock);
  1176. INIT_LIST_HEAD(&vfio.group_list);
  1177. INIT_LIST_HEAD(&vfio.iommu_drivers_list);
  1178. init_waitqueue_head(&vfio.release_q);
  1179. vfio.class = class_create(THIS_MODULE, "vfio");
  1180. if (IS_ERR(vfio.class)) {
  1181. ret = PTR_ERR(vfio.class);
  1182. goto err_class;
  1183. }
  1184. vfio.class->devnode = vfio_devnode;
  1185. ret = alloc_chrdev_region(&vfio.devt, 0, MINORMASK, "vfio");
  1186. if (ret)
  1187. goto err_base_chrdev;
  1188. cdev_init(&vfio.cdev, &vfio_fops);
  1189. ret = cdev_add(&vfio.cdev, vfio.devt, 1);
  1190. if (ret)
  1191. goto err_base_cdev;
  1192. vfio.dev = device_create(vfio.class, NULL, vfio.devt, NULL, "vfio");
  1193. if (IS_ERR(vfio.dev)) {
  1194. ret = PTR_ERR(vfio.dev);
  1195. goto err_base_dev;
  1196. }
  1197. /* /dev/vfio/$GROUP */
  1198. cdev_init(&vfio.group_cdev, &vfio_group_fops);
  1199. ret = cdev_add(&vfio.group_cdev,
  1200. MKDEV(MAJOR(vfio.devt), 1), MINORMASK - 1);
  1201. if (ret)
  1202. goto err_groups_cdev;
  1203. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  1204. /*
  1205. * Attempt to load known iommu-drivers. This gives us a working
  1206. * environment without the user needing to explicitly load iommu
  1207. * drivers.
  1208. */
  1209. request_module_nowait("vfio_iommu_type1");
  1210. request_module_nowait("vfio_iommu_spapr_tce");
  1211. return 0;
  1212. err_groups_cdev:
  1213. device_destroy(vfio.class, vfio.devt);
  1214. err_base_dev:
  1215. cdev_del(&vfio.cdev);
  1216. err_base_cdev:
  1217. unregister_chrdev_region(vfio.devt, MINORMASK);
  1218. err_base_chrdev:
  1219. class_destroy(vfio.class);
  1220. vfio.class = NULL;
  1221. err_class:
  1222. return ret;
  1223. }
  1224. static void __exit vfio_cleanup(void)
  1225. {
  1226. WARN_ON(!list_empty(&vfio.group_list));
  1227. idr_destroy(&vfio.group_idr);
  1228. cdev_del(&vfio.group_cdev);
  1229. device_destroy(vfio.class, vfio.devt);
  1230. cdev_del(&vfio.cdev);
  1231. unregister_chrdev_region(vfio.devt, MINORMASK);
  1232. class_destroy(vfio.class);
  1233. vfio.class = NULL;
  1234. }
  1235. module_init(vfio_init);
  1236. module_exit(vfio_cleanup);
  1237. MODULE_VERSION(DRIVER_VERSION);
  1238. MODULE_LICENSE("GPL v2");
  1239. MODULE_AUTHOR(DRIVER_AUTHOR);
  1240. MODULE_DESCRIPTION(DRIVER_DESC);