vfio.c 34 KB

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