vfio.c 34 KB

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