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