vfio.c 34 KB

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