vfio.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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/miscdevice.h>
  25. #include <linux/module.h>
  26. #include <linux/mutex.h>
  27. #include <linux/pci.h>
  28. #include <linux/rwsem.h>
  29. #include <linux/sched.h>
  30. #include <linux/slab.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/vfio.h>
  35. #include <linux/wait.h>
  36. #define DRIVER_VERSION "0.3"
  37. #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
  38. #define DRIVER_DESC "VFIO - User Level meta-driver"
  39. static struct vfio {
  40. struct class *class;
  41. struct list_head iommu_drivers_list;
  42. struct mutex iommu_drivers_lock;
  43. struct list_head group_list;
  44. struct idr group_idr;
  45. struct mutex group_lock;
  46. struct cdev group_cdev;
  47. dev_t group_devt;
  48. wait_queue_head_t release_q;
  49. } vfio;
  50. struct vfio_iommu_driver {
  51. const struct vfio_iommu_driver_ops *ops;
  52. struct list_head vfio_next;
  53. };
  54. struct vfio_container {
  55. struct kref kref;
  56. struct list_head group_list;
  57. struct rw_semaphore group_lock;
  58. struct vfio_iommu_driver *iommu_driver;
  59. void *iommu_data;
  60. };
  61. struct vfio_unbound_dev {
  62. struct device *dev;
  63. struct list_head unbound_next;
  64. };
  65. struct vfio_group {
  66. struct kref kref;
  67. int minor;
  68. atomic_t container_users;
  69. struct iommu_group *iommu_group;
  70. struct vfio_container *container;
  71. struct list_head device_list;
  72. struct mutex device_lock;
  73. struct device *dev;
  74. struct notifier_block nb;
  75. struct list_head vfio_next;
  76. struct list_head container_next;
  77. struct list_head unbound_list;
  78. struct mutex unbound_lock;
  79. atomic_t opened;
  80. };
  81. struct vfio_device {
  82. struct kref kref;
  83. struct device *dev;
  84. const struct vfio_device_ops *ops;
  85. struct vfio_group *group;
  86. struct list_head group_next;
  87. void *device_data;
  88. };
  89. /**
  90. * IOMMU driver registration
  91. */
  92. int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
  93. {
  94. struct vfio_iommu_driver *driver, *tmp;
  95. driver = kzalloc(sizeof(*driver), GFP_KERNEL);
  96. if (!driver)
  97. return -ENOMEM;
  98. driver->ops = ops;
  99. mutex_lock(&vfio.iommu_drivers_lock);
  100. /* Check for duplicates */
  101. list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
  102. if (tmp->ops == ops) {
  103. mutex_unlock(&vfio.iommu_drivers_lock);
  104. kfree(driver);
  105. return -EINVAL;
  106. }
  107. }
  108. list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
  109. mutex_unlock(&vfio.iommu_drivers_lock);
  110. return 0;
  111. }
  112. EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
  113. void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
  114. {
  115. struct vfio_iommu_driver *driver;
  116. mutex_lock(&vfio.iommu_drivers_lock);
  117. list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
  118. if (driver->ops == ops) {
  119. list_del(&driver->vfio_next);
  120. mutex_unlock(&vfio.iommu_drivers_lock);
  121. kfree(driver);
  122. return;
  123. }
  124. }
  125. mutex_unlock(&vfio.iommu_drivers_lock);
  126. }
  127. EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
  128. /**
  129. * Group minor allocation/free - both called with vfio.group_lock held
  130. */
  131. static int vfio_alloc_group_minor(struct vfio_group *group)
  132. {
  133. return idr_alloc(&vfio.group_idr, group, 0, MINORMASK + 1, GFP_KERNEL);
  134. }
  135. static void vfio_free_group_minor(int minor)
  136. {
  137. idr_remove(&vfio.group_idr, minor);
  138. }
  139. static int vfio_iommu_group_notifier(struct notifier_block *nb,
  140. unsigned long action, void *data);
  141. static void vfio_group_get(struct vfio_group *group);
  142. /**
  143. * Container objects - containers are created when /dev/vfio/vfio is
  144. * opened, but their lifecycle extends until the last user is done, so
  145. * it's freed via kref. Must support container/group/device being
  146. * closed in any order.
  147. */
  148. static void vfio_container_get(struct vfio_container *container)
  149. {
  150. kref_get(&container->kref);
  151. }
  152. static void vfio_container_release(struct kref *kref)
  153. {
  154. struct vfio_container *container;
  155. container = container_of(kref, struct vfio_container, kref);
  156. kfree(container);
  157. }
  158. static void vfio_container_put(struct vfio_container *container)
  159. {
  160. kref_put(&container->kref, vfio_container_release);
  161. }
  162. static void vfio_group_unlock_and_free(struct vfio_group *group)
  163. {
  164. mutex_unlock(&vfio.group_lock);
  165. /*
  166. * Unregister outside of lock. A spurious callback is harmless now
  167. * that the group is no longer in vfio.group_list.
  168. */
  169. iommu_group_unregister_notifier(group->iommu_group, &group->nb);
  170. kfree(group);
  171. }
  172. /**
  173. * Group objects - create, release, get, put, search
  174. */
  175. static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
  176. {
  177. struct vfio_group *group, *tmp;
  178. struct device *dev;
  179. int ret, minor;
  180. group = kzalloc(sizeof(*group), GFP_KERNEL);
  181. if (!group)
  182. return ERR_PTR(-ENOMEM);
  183. kref_init(&group->kref);
  184. INIT_LIST_HEAD(&group->device_list);
  185. mutex_init(&group->device_lock);
  186. INIT_LIST_HEAD(&group->unbound_list);
  187. mutex_init(&group->unbound_lock);
  188. atomic_set(&group->container_users, 0);
  189. atomic_set(&group->opened, 0);
  190. group->iommu_group = iommu_group;
  191. group->nb.notifier_call = vfio_iommu_group_notifier;
  192. /*
  193. * blocking notifiers acquire a rwsem around registering and hold
  194. * it around callback. Therefore, need to register outside of
  195. * vfio.group_lock to avoid A-B/B-A contention. Our callback won't
  196. * do anything unless it can find the group in vfio.group_list, so
  197. * no harm in registering early.
  198. */
  199. ret = iommu_group_register_notifier(iommu_group, &group->nb);
  200. if (ret) {
  201. kfree(group);
  202. return ERR_PTR(ret);
  203. }
  204. mutex_lock(&vfio.group_lock);
  205. /* Did we race creating this group? */
  206. list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
  207. if (tmp->iommu_group == iommu_group) {
  208. vfio_group_get(tmp);
  209. vfio_group_unlock_and_free(group);
  210. return tmp;
  211. }
  212. }
  213. minor = vfio_alloc_group_minor(group);
  214. if (minor < 0) {
  215. vfio_group_unlock_and_free(group);
  216. return ERR_PTR(minor);
  217. }
  218. dev = device_create(vfio.class, NULL,
  219. MKDEV(MAJOR(vfio.group_devt), minor),
  220. group, "%d", iommu_group_id(iommu_group));
  221. if (IS_ERR(dev)) {
  222. vfio_free_group_minor(minor);
  223. vfio_group_unlock_and_free(group);
  224. return (struct vfio_group *)dev; /* ERR_PTR */
  225. }
  226. group->minor = minor;
  227. group->dev = dev;
  228. list_add(&group->vfio_next, &vfio.group_list);
  229. mutex_unlock(&vfio.group_lock);
  230. return group;
  231. }
  232. /* called with vfio.group_lock held */
  233. static void vfio_group_release(struct kref *kref)
  234. {
  235. struct vfio_group *group = container_of(kref, struct vfio_group, kref);
  236. struct vfio_unbound_dev *unbound, *tmp;
  237. struct iommu_group *iommu_group = group->iommu_group;
  238. WARN_ON(!list_empty(&group->device_list));
  239. list_for_each_entry_safe(unbound, tmp,
  240. &group->unbound_list, unbound_next) {
  241. list_del(&unbound->unbound_next);
  242. kfree(unbound);
  243. }
  244. device_destroy(vfio.class, MKDEV(MAJOR(vfio.group_devt), group->minor));
  245. list_del(&group->vfio_next);
  246. vfio_free_group_minor(group->minor);
  247. vfio_group_unlock_and_free(group);
  248. iommu_group_put(iommu_group);
  249. }
  250. static void vfio_group_put(struct vfio_group *group)
  251. {
  252. kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
  253. }
  254. /* Assume group_lock or group reference is held */
  255. static void vfio_group_get(struct vfio_group *group)
  256. {
  257. kref_get(&group->kref);
  258. }
  259. /*
  260. * Not really a try as we will sleep for mutex, but we need to make
  261. * sure the group pointer is valid under lock and get a reference.
  262. */
  263. static struct vfio_group *vfio_group_try_get(struct vfio_group *group)
  264. {
  265. struct vfio_group *target = group;
  266. mutex_lock(&vfio.group_lock);
  267. list_for_each_entry(group, &vfio.group_list, vfio_next) {
  268. if (group == target) {
  269. vfio_group_get(group);
  270. mutex_unlock(&vfio.group_lock);
  271. return group;
  272. }
  273. }
  274. mutex_unlock(&vfio.group_lock);
  275. return NULL;
  276. }
  277. static
  278. struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group)
  279. {
  280. struct vfio_group *group;
  281. mutex_lock(&vfio.group_lock);
  282. list_for_each_entry(group, &vfio.group_list, vfio_next) {
  283. if (group->iommu_group == iommu_group) {
  284. vfio_group_get(group);
  285. mutex_unlock(&vfio.group_lock);
  286. return group;
  287. }
  288. }
  289. mutex_unlock(&vfio.group_lock);
  290. return NULL;
  291. }
  292. static struct vfio_group *vfio_group_get_from_minor(int minor)
  293. {
  294. struct vfio_group *group;
  295. mutex_lock(&vfio.group_lock);
  296. group = idr_find(&vfio.group_idr, minor);
  297. if (!group) {
  298. mutex_unlock(&vfio.group_lock);
  299. return NULL;
  300. }
  301. vfio_group_get(group);
  302. mutex_unlock(&vfio.group_lock);
  303. return group;
  304. }
  305. /**
  306. * Device objects - create, release, get, put, search
  307. */
  308. static
  309. struct vfio_device *vfio_group_create_device(struct vfio_group *group,
  310. struct device *dev,
  311. const struct vfio_device_ops *ops,
  312. void *device_data)
  313. {
  314. struct vfio_device *device;
  315. device = kzalloc(sizeof(*device), GFP_KERNEL);
  316. if (!device)
  317. return ERR_PTR(-ENOMEM);
  318. kref_init(&device->kref);
  319. device->dev = dev;
  320. device->group = group;
  321. device->ops = ops;
  322. device->device_data = device_data;
  323. dev_set_drvdata(dev, device);
  324. /* No need to get group_lock, caller has group reference */
  325. vfio_group_get(group);
  326. mutex_lock(&group->device_lock);
  327. list_add(&device->group_next, &group->device_list);
  328. mutex_unlock(&group->device_lock);
  329. return device;
  330. }
  331. static void vfio_device_release(struct kref *kref)
  332. {
  333. struct vfio_device *device = container_of(kref,
  334. struct vfio_device, kref);
  335. struct vfio_group *group = device->group;
  336. list_del(&device->group_next);
  337. mutex_unlock(&group->device_lock);
  338. dev_set_drvdata(device->dev, NULL);
  339. kfree(device);
  340. /* vfio_del_group_dev may be waiting for this device */
  341. wake_up(&vfio.release_q);
  342. }
  343. /* Device reference always implies a group reference */
  344. void vfio_device_put(struct vfio_device *device)
  345. {
  346. struct vfio_group *group = device->group;
  347. kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock);
  348. vfio_group_put(group);
  349. }
  350. EXPORT_SYMBOL_GPL(vfio_device_put);
  351. static void vfio_device_get(struct vfio_device *device)
  352. {
  353. vfio_group_get(device->group);
  354. kref_get(&device->kref);
  355. }
  356. static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
  357. struct device *dev)
  358. {
  359. struct vfio_device *device;
  360. mutex_lock(&group->device_lock);
  361. list_for_each_entry(device, &group->device_list, group_next) {
  362. if (device->dev == dev) {
  363. vfio_device_get(device);
  364. mutex_unlock(&group->device_lock);
  365. return device;
  366. }
  367. }
  368. mutex_unlock(&group->device_lock);
  369. return NULL;
  370. }
  371. /*
  372. * Some drivers, like pci-stub, are only used to prevent other drivers from
  373. * claiming a device and are therefore perfectly legitimate for a user owned
  374. * group. The pci-stub driver has no dependencies on DMA or the IOVA mapping
  375. * of the device, but it does prevent the user from having direct access to
  376. * the device, which is useful in some circumstances.
  377. *
  378. * We also assume that we can include PCI interconnect devices, ie. bridges.
  379. * IOMMU grouping on PCI necessitates that if we lack isolation on a bridge
  380. * then all of the downstream devices will be part of the same IOMMU group as
  381. * the bridge. Thus, if placing the bridge into the user owned IOVA space
  382. * breaks anything, it only does so for user owned devices downstream. Note
  383. * that error notification via MSI can be affected for platforms that handle
  384. * MSI within the same IOVA space as DMA.
  385. */
  386. static const char * const vfio_driver_whitelist[] = { "pci-stub" };
  387. static bool vfio_dev_whitelisted(struct device *dev, struct device_driver *drv)
  388. {
  389. int i;
  390. if (dev_is_pci(dev)) {
  391. struct pci_dev *pdev = to_pci_dev(dev);
  392. if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  393. return true;
  394. }
  395. for (i = 0; i < ARRAY_SIZE(vfio_driver_whitelist); i++) {
  396. if (!strcmp(drv->name, vfio_driver_whitelist[i]))
  397. return true;
  398. }
  399. return false;
  400. }
  401. /*
  402. * A vfio group is viable for use by userspace if all devices are in
  403. * one of the following states:
  404. * - driver-less
  405. * - bound to a vfio driver
  406. * - bound to a whitelisted driver
  407. * - a PCI interconnect device
  408. *
  409. * We use two methods to determine whether a device is bound to a vfio
  410. * driver. The first is to test whether the device exists in the vfio
  411. * group. The second is to test if the device exists on the group
  412. * unbound_list, indicating it's in the middle of transitioning from
  413. * a vfio driver to driver-less.
  414. */
  415. static int vfio_dev_viable(struct device *dev, void *data)
  416. {
  417. struct vfio_group *group = data;
  418. struct vfio_device *device;
  419. struct device_driver *drv = ACCESS_ONCE(dev->driver);
  420. struct vfio_unbound_dev *unbound;
  421. int ret = -EINVAL;
  422. mutex_lock(&group->unbound_lock);
  423. list_for_each_entry(unbound, &group->unbound_list, unbound_next) {
  424. if (dev == unbound->dev) {
  425. ret = 0;
  426. break;
  427. }
  428. }
  429. mutex_unlock(&group->unbound_lock);
  430. if (!ret || !drv || vfio_dev_whitelisted(dev, drv))
  431. return 0;
  432. device = vfio_group_get_device(group, dev);
  433. if (device) {
  434. vfio_device_put(device);
  435. return 0;
  436. }
  437. return ret;
  438. }
  439. /**
  440. * Async device support
  441. */
  442. static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev)
  443. {
  444. struct vfio_device *device;
  445. /* Do we already know about it? We shouldn't */
  446. device = vfio_group_get_device(group, dev);
  447. if (WARN_ON_ONCE(device)) {
  448. vfio_device_put(device);
  449. return 0;
  450. }
  451. /* Nothing to do for idle groups */
  452. if (!atomic_read(&group->container_users))
  453. return 0;
  454. /* TODO Prevent device auto probing */
  455. WARN("Device %s added to live group %d!\n", dev_name(dev),
  456. iommu_group_id(group->iommu_group));
  457. return 0;
  458. }
  459. static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev)
  460. {
  461. /* We don't care what happens when the group isn't in use */
  462. if (!atomic_read(&group->container_users))
  463. return 0;
  464. return vfio_dev_viable(dev, group);
  465. }
  466. static int vfio_iommu_group_notifier(struct notifier_block *nb,
  467. unsigned long action, void *data)
  468. {
  469. struct vfio_group *group = container_of(nb, struct vfio_group, nb);
  470. struct device *dev = data;
  471. struct vfio_unbound_dev *unbound;
  472. /*
  473. * Need to go through a group_lock lookup to get a reference or we
  474. * risk racing a group being removed. Ignore spurious notifies.
  475. */
  476. group = vfio_group_try_get(group);
  477. if (!group)
  478. return NOTIFY_OK;
  479. switch (action) {
  480. case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
  481. vfio_group_nb_add_dev(group, dev);
  482. break;
  483. case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
  484. /*
  485. * Nothing to do here. If the device is in use, then the
  486. * vfio sub-driver should block the remove callback until
  487. * it is unused. If the device is unused or attached to a
  488. * stub driver, then it should be released and we don't
  489. * care that it will be going away.
  490. */
  491. break;
  492. case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
  493. pr_debug("%s: Device %s, group %d binding to driver\n",
  494. __func__, dev_name(dev),
  495. iommu_group_id(group->iommu_group));
  496. break;
  497. case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
  498. pr_debug("%s: Device %s, group %d bound to driver %s\n",
  499. __func__, dev_name(dev),
  500. iommu_group_id(group->iommu_group), dev->driver->name);
  501. BUG_ON(vfio_group_nb_verify(group, dev));
  502. break;
  503. case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
  504. pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
  505. __func__, dev_name(dev),
  506. iommu_group_id(group->iommu_group), dev->driver->name);
  507. break;
  508. case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
  509. pr_debug("%s: Device %s, group %d unbound from driver\n",
  510. __func__, dev_name(dev),
  511. iommu_group_id(group->iommu_group));
  512. /*
  513. * XXX An unbound device in a live group is ok, but we'd
  514. * really like to avoid the above BUG_ON by preventing other
  515. * drivers from binding to it. Once that occurs, we have to
  516. * stop the system to maintain isolation. At a minimum, we'd
  517. * want a toggle to disable driver auto probe for this device.
  518. */
  519. mutex_lock(&group->unbound_lock);
  520. list_for_each_entry(unbound,
  521. &group->unbound_list, unbound_next) {
  522. if (dev == unbound->dev) {
  523. list_del(&unbound->unbound_next);
  524. kfree(unbound);
  525. break;
  526. }
  527. }
  528. mutex_unlock(&group->unbound_lock);
  529. break;
  530. }
  531. vfio_group_put(group);
  532. return NOTIFY_OK;
  533. }
  534. /**
  535. * VFIO driver API
  536. */
  537. int vfio_add_group_dev(struct device *dev,
  538. const struct vfio_device_ops *ops, void *device_data)
  539. {
  540. struct iommu_group *iommu_group;
  541. struct vfio_group *group;
  542. struct vfio_device *device;
  543. iommu_group = iommu_group_get(dev);
  544. if (!iommu_group)
  545. return -EINVAL;
  546. group = vfio_group_get_from_iommu(iommu_group);
  547. if (!group) {
  548. group = vfio_create_group(iommu_group);
  549. if (IS_ERR(group)) {
  550. iommu_group_put(iommu_group);
  551. return PTR_ERR(group);
  552. }
  553. } else {
  554. /*
  555. * A found vfio_group already holds a reference to the
  556. * iommu_group. A created vfio_group keeps the reference.
  557. */
  558. iommu_group_put(iommu_group);
  559. }
  560. device = vfio_group_get_device(group, dev);
  561. if (device) {
  562. WARN(1, "Device %s already exists on group %d\n",
  563. dev_name(dev), iommu_group_id(iommu_group));
  564. vfio_device_put(device);
  565. vfio_group_put(group);
  566. return -EBUSY;
  567. }
  568. device = vfio_group_create_device(group, dev, ops, device_data);
  569. if (IS_ERR(device)) {
  570. vfio_group_put(group);
  571. return PTR_ERR(device);
  572. }
  573. /*
  574. * Drop all but the vfio_device reference. The vfio_device holds
  575. * a reference to the vfio_group, which holds a reference to the
  576. * iommu_group.
  577. */
  578. vfio_group_put(group);
  579. return 0;
  580. }
  581. EXPORT_SYMBOL_GPL(vfio_add_group_dev);
  582. /**
  583. * Get a reference to the vfio_device for a device. Even if the
  584. * caller thinks they own the device, they could be racing with a
  585. * release call path, so we can't trust drvdata for the shortcut.
  586. * Go the long way around, from the iommu_group to the vfio_group
  587. * to the vfio_device.
  588. */
  589. struct vfio_device *vfio_device_get_from_dev(struct device *dev)
  590. {
  591. struct iommu_group *iommu_group;
  592. struct vfio_group *group;
  593. struct vfio_device *device;
  594. iommu_group = iommu_group_get(dev);
  595. if (!iommu_group)
  596. return NULL;
  597. group = vfio_group_get_from_iommu(iommu_group);
  598. iommu_group_put(iommu_group);
  599. if (!group)
  600. return NULL;
  601. device = vfio_group_get_device(group, dev);
  602. vfio_group_put(group);
  603. return device;
  604. }
  605. EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
  606. static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
  607. char *buf)
  608. {
  609. struct vfio_device *it, *device = NULL;
  610. mutex_lock(&group->device_lock);
  611. list_for_each_entry(it, &group->device_list, group_next) {
  612. if (!strcmp(dev_name(it->dev), buf)) {
  613. device = it;
  614. vfio_device_get(device);
  615. break;
  616. }
  617. }
  618. mutex_unlock(&group->device_lock);
  619. return device;
  620. }
  621. /*
  622. * Caller must hold a reference to the vfio_device
  623. */
  624. void *vfio_device_data(struct vfio_device *device)
  625. {
  626. return device->device_data;
  627. }
  628. EXPORT_SYMBOL_GPL(vfio_device_data);
  629. /* Given a referenced group, check if it contains the device */
  630. static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
  631. {
  632. struct vfio_device *device;
  633. device = vfio_group_get_device(group, dev);
  634. if (!device)
  635. return false;
  636. vfio_device_put(device);
  637. return true;
  638. }
  639. /*
  640. * Decrement the device reference count and wait for the device to be
  641. * removed. Open file descriptors for the device... */
  642. void *vfio_del_group_dev(struct device *dev)
  643. {
  644. struct vfio_device *device = dev_get_drvdata(dev);
  645. struct vfio_group *group = device->group;
  646. void *device_data = device->device_data;
  647. struct vfio_unbound_dev *unbound;
  648. unsigned int i = 0;
  649. long ret;
  650. bool interrupted = false;
  651. /*
  652. * The group exists so long as we have a device reference. Get
  653. * a group reference and use it to scan for the device going away.
  654. */
  655. vfio_group_get(group);
  656. /*
  657. * When the device is removed from the group, the group suddenly
  658. * becomes non-viable; the device has a driver (until the unbind
  659. * completes), but it's not present in the group. This is bad news
  660. * for any external users that need to re-acquire a group reference
  661. * in order to match and release their existing reference. To
  662. * solve this, we track such devices on the unbound_list to bridge
  663. * the gap until they're fully unbound.
  664. */
  665. unbound = kzalloc(sizeof(*unbound), GFP_KERNEL);
  666. if (unbound) {
  667. unbound->dev = dev;
  668. mutex_lock(&group->unbound_lock);
  669. list_add(&unbound->unbound_next, &group->unbound_list);
  670. mutex_unlock(&group->unbound_lock);
  671. }
  672. WARN_ON(!unbound);
  673. vfio_device_put(device);
  674. /*
  675. * If the device is still present in the group after the above
  676. * 'put', then it is in use and we need to request it from the
  677. * bus driver. The driver may in turn need to request the
  678. * device from the user. We send the request on an arbitrary
  679. * interval with counter to allow the driver to take escalating
  680. * measures to release the device if it has the ability to do so.
  681. */
  682. do {
  683. device = vfio_group_get_device(group, dev);
  684. if (!device)
  685. break;
  686. if (device->ops->request)
  687. device->ops->request(device_data, i++);
  688. vfio_device_put(device);
  689. if (interrupted) {
  690. ret = wait_event_timeout(vfio.release_q,
  691. !vfio_dev_present(group, dev), HZ * 10);
  692. } else {
  693. ret = wait_event_interruptible_timeout(vfio.release_q,
  694. !vfio_dev_present(group, dev), HZ * 10);
  695. if (ret == -ERESTARTSYS) {
  696. interrupted = true;
  697. dev_warn(dev,
  698. "Device is currently in use, task"
  699. " \"%s\" (%d) "
  700. "blocked until device is released",
  701. current->comm, task_pid_nr(current));
  702. }
  703. }
  704. } while (ret <= 0);
  705. vfio_group_put(group);
  706. return device_data;
  707. }
  708. EXPORT_SYMBOL_GPL(vfio_del_group_dev);
  709. /**
  710. * VFIO base fd, /dev/vfio/vfio
  711. */
  712. static long vfio_ioctl_check_extension(struct vfio_container *container,
  713. unsigned long arg)
  714. {
  715. struct vfio_iommu_driver *driver;
  716. long ret = 0;
  717. down_read(&container->group_lock);
  718. driver = container->iommu_driver;
  719. switch (arg) {
  720. /* No base extensions yet */
  721. default:
  722. /*
  723. * If no driver is set, poll all registered drivers for
  724. * extensions and return the first positive result. If
  725. * a driver is already set, further queries will be passed
  726. * only to that driver.
  727. */
  728. if (!driver) {
  729. mutex_lock(&vfio.iommu_drivers_lock);
  730. list_for_each_entry(driver, &vfio.iommu_drivers_list,
  731. vfio_next) {
  732. if (!try_module_get(driver->ops->owner))
  733. continue;
  734. ret = driver->ops->ioctl(NULL,
  735. VFIO_CHECK_EXTENSION,
  736. arg);
  737. module_put(driver->ops->owner);
  738. if (ret > 0)
  739. break;
  740. }
  741. mutex_unlock(&vfio.iommu_drivers_lock);
  742. } else
  743. ret = driver->ops->ioctl(container->iommu_data,
  744. VFIO_CHECK_EXTENSION, arg);
  745. }
  746. up_read(&container->group_lock);
  747. return ret;
  748. }
  749. /* hold write lock on container->group_lock */
  750. static int __vfio_container_attach_groups(struct vfio_container *container,
  751. struct vfio_iommu_driver *driver,
  752. void *data)
  753. {
  754. struct vfio_group *group;
  755. int ret = -ENODEV;
  756. list_for_each_entry(group, &container->group_list, container_next) {
  757. ret = driver->ops->attach_group(data, group->iommu_group);
  758. if (ret)
  759. goto unwind;
  760. }
  761. return ret;
  762. unwind:
  763. list_for_each_entry_continue_reverse(group, &container->group_list,
  764. container_next) {
  765. driver->ops->detach_group(data, group->iommu_group);
  766. }
  767. return ret;
  768. }
  769. static long vfio_ioctl_set_iommu(struct vfio_container *container,
  770. unsigned long arg)
  771. {
  772. struct vfio_iommu_driver *driver;
  773. long ret = -ENODEV;
  774. down_write(&container->group_lock);
  775. /*
  776. * The container is designed to be an unprivileged interface while
  777. * the group can be assigned to specific users. Therefore, only by
  778. * adding a group to a container does the user get the privilege of
  779. * enabling the iommu, which may allocate finite resources. There
  780. * is no unset_iommu, but by removing all the groups from a container,
  781. * the container is deprivileged and returns to an unset state.
  782. */
  783. if (list_empty(&container->group_list) || container->iommu_driver) {
  784. up_write(&container->group_lock);
  785. return -EINVAL;
  786. }
  787. mutex_lock(&vfio.iommu_drivers_lock);
  788. list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
  789. void *data;
  790. if (!try_module_get(driver->ops->owner))
  791. continue;
  792. /*
  793. * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
  794. * so test which iommu driver reported support for this
  795. * extension and call open on them. We also pass them the
  796. * magic, allowing a single driver to support multiple
  797. * interfaces if they'd like.
  798. */
  799. if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
  800. module_put(driver->ops->owner);
  801. continue;
  802. }
  803. /* module reference holds the driver we're working on */
  804. mutex_unlock(&vfio.iommu_drivers_lock);
  805. data = driver->ops->open(arg);
  806. if (IS_ERR(data)) {
  807. ret = PTR_ERR(data);
  808. module_put(driver->ops->owner);
  809. goto skip_drivers_unlock;
  810. }
  811. ret = __vfio_container_attach_groups(container, driver, data);
  812. if (!ret) {
  813. container->iommu_driver = driver;
  814. container->iommu_data = data;
  815. } else {
  816. driver->ops->release(data);
  817. module_put(driver->ops->owner);
  818. }
  819. goto skip_drivers_unlock;
  820. }
  821. mutex_unlock(&vfio.iommu_drivers_lock);
  822. skip_drivers_unlock:
  823. up_write(&container->group_lock);
  824. return ret;
  825. }
  826. static long vfio_fops_unl_ioctl(struct file *filep,
  827. unsigned int cmd, unsigned long arg)
  828. {
  829. struct vfio_container *container = filep->private_data;
  830. struct vfio_iommu_driver *driver;
  831. void *data;
  832. long ret = -EINVAL;
  833. if (!container)
  834. return ret;
  835. switch (cmd) {
  836. case VFIO_GET_API_VERSION:
  837. ret = VFIO_API_VERSION;
  838. break;
  839. case VFIO_CHECK_EXTENSION:
  840. ret = vfio_ioctl_check_extension(container, arg);
  841. break;
  842. case VFIO_SET_IOMMU:
  843. ret = vfio_ioctl_set_iommu(container, arg);
  844. break;
  845. default:
  846. down_read(&container->group_lock);
  847. driver = container->iommu_driver;
  848. data = container->iommu_data;
  849. if (driver) /* passthrough all unrecognized ioctls */
  850. ret = driver->ops->ioctl(data, cmd, arg);
  851. up_read(&container->group_lock);
  852. }
  853. return ret;
  854. }
  855. #ifdef CONFIG_COMPAT
  856. static long vfio_fops_compat_ioctl(struct file *filep,
  857. unsigned int cmd, unsigned long arg)
  858. {
  859. arg = (unsigned long)compat_ptr(arg);
  860. return vfio_fops_unl_ioctl(filep, cmd, arg);
  861. }
  862. #endif /* CONFIG_COMPAT */
  863. static int vfio_fops_open(struct inode *inode, struct file *filep)
  864. {
  865. struct vfio_container *container;
  866. container = kzalloc(sizeof(*container), GFP_KERNEL);
  867. if (!container)
  868. return -ENOMEM;
  869. INIT_LIST_HEAD(&container->group_list);
  870. init_rwsem(&container->group_lock);
  871. kref_init(&container->kref);
  872. filep->private_data = container;
  873. return 0;
  874. }
  875. static int vfio_fops_release(struct inode *inode, struct file *filep)
  876. {
  877. struct vfio_container *container = filep->private_data;
  878. filep->private_data = NULL;
  879. vfio_container_put(container);
  880. return 0;
  881. }
  882. /*
  883. * Once an iommu driver is set, we optionally pass read/write/mmap
  884. * on to the driver, allowing management interfaces beyond ioctl.
  885. */
  886. static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
  887. size_t count, loff_t *ppos)
  888. {
  889. struct vfio_container *container = filep->private_data;
  890. struct vfio_iommu_driver *driver;
  891. ssize_t ret = -EINVAL;
  892. down_read(&container->group_lock);
  893. driver = container->iommu_driver;
  894. if (likely(driver && driver->ops->read))
  895. ret = driver->ops->read(container->iommu_data,
  896. buf, count, ppos);
  897. up_read(&container->group_lock);
  898. return ret;
  899. }
  900. static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
  901. size_t count, loff_t *ppos)
  902. {
  903. struct vfio_container *container = filep->private_data;
  904. struct vfio_iommu_driver *driver;
  905. ssize_t ret = -EINVAL;
  906. down_read(&container->group_lock);
  907. driver = container->iommu_driver;
  908. if (likely(driver && driver->ops->write))
  909. ret = driver->ops->write(container->iommu_data,
  910. buf, count, ppos);
  911. up_read(&container->group_lock);
  912. return ret;
  913. }
  914. static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  915. {
  916. struct vfio_container *container = filep->private_data;
  917. struct vfio_iommu_driver *driver;
  918. int ret = -EINVAL;
  919. down_read(&container->group_lock);
  920. driver = container->iommu_driver;
  921. if (likely(driver && driver->ops->mmap))
  922. ret = driver->ops->mmap(container->iommu_data, vma);
  923. up_read(&container->group_lock);
  924. return ret;
  925. }
  926. static const struct file_operations vfio_fops = {
  927. .owner = THIS_MODULE,
  928. .open = vfio_fops_open,
  929. .release = vfio_fops_release,
  930. .read = vfio_fops_read,
  931. .write = vfio_fops_write,
  932. .unlocked_ioctl = vfio_fops_unl_ioctl,
  933. #ifdef CONFIG_COMPAT
  934. .compat_ioctl = vfio_fops_compat_ioctl,
  935. #endif
  936. .mmap = vfio_fops_mmap,
  937. };
  938. /**
  939. * VFIO Group fd, /dev/vfio/$GROUP
  940. */
  941. static void __vfio_group_unset_container(struct vfio_group *group)
  942. {
  943. struct vfio_container *container = group->container;
  944. struct vfio_iommu_driver *driver;
  945. down_write(&container->group_lock);
  946. driver = container->iommu_driver;
  947. if (driver)
  948. driver->ops->detach_group(container->iommu_data,
  949. group->iommu_group);
  950. group->container = NULL;
  951. list_del(&group->container_next);
  952. /* Detaching the last group deprivileges a container, remove iommu */
  953. if (driver && list_empty(&container->group_list)) {
  954. driver->ops->release(container->iommu_data);
  955. module_put(driver->ops->owner);
  956. container->iommu_driver = NULL;
  957. container->iommu_data = NULL;
  958. }
  959. up_write(&container->group_lock);
  960. vfio_container_put(container);
  961. }
  962. /*
  963. * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
  964. * if there was no container to unset. Since the ioctl is called on
  965. * the group, we know that still exists, therefore the only valid
  966. * transition here is 1->0.
  967. */
  968. static int vfio_group_unset_container(struct vfio_group *group)
  969. {
  970. int users = atomic_cmpxchg(&group->container_users, 1, 0);
  971. if (!users)
  972. return -EINVAL;
  973. if (users != 1)
  974. return -EBUSY;
  975. __vfio_group_unset_container(group);
  976. return 0;
  977. }
  978. /*
  979. * When removing container users, anything that removes the last user
  980. * implicitly removes the group from the container. That is, if the
  981. * group file descriptor is closed, as well as any device file descriptors,
  982. * the group is free.
  983. */
  984. static void vfio_group_try_dissolve_container(struct vfio_group *group)
  985. {
  986. if (0 == atomic_dec_if_positive(&group->container_users))
  987. __vfio_group_unset_container(group);
  988. }
  989. static int vfio_group_set_container(struct vfio_group *group, int container_fd)
  990. {
  991. struct fd f;
  992. struct vfio_container *container;
  993. struct vfio_iommu_driver *driver;
  994. int ret = 0;
  995. if (atomic_read(&group->container_users))
  996. return -EINVAL;
  997. f = fdget(container_fd);
  998. if (!f.file)
  999. return -EBADF;
  1000. /* Sanity check, is this really our fd? */
  1001. if (f.file->f_op != &vfio_fops) {
  1002. fdput(f);
  1003. return -EINVAL;
  1004. }
  1005. container = f.file->private_data;
  1006. WARN_ON(!container); /* fget ensures we don't race vfio_release */
  1007. down_write(&container->group_lock);
  1008. driver = container->iommu_driver;
  1009. if (driver) {
  1010. ret = driver->ops->attach_group(container->iommu_data,
  1011. group->iommu_group);
  1012. if (ret)
  1013. goto unlock_out;
  1014. }
  1015. group->container = container;
  1016. list_add(&group->container_next, &container->group_list);
  1017. /* Get a reference on the container and mark a user within the group */
  1018. vfio_container_get(container);
  1019. atomic_inc(&group->container_users);
  1020. unlock_out:
  1021. up_write(&container->group_lock);
  1022. fdput(f);
  1023. return ret;
  1024. }
  1025. static bool vfio_group_viable(struct vfio_group *group)
  1026. {
  1027. return (iommu_group_for_each_dev(group->iommu_group,
  1028. group, vfio_dev_viable) == 0);
  1029. }
  1030. static const struct file_operations vfio_device_fops;
  1031. static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
  1032. {
  1033. struct vfio_device *device;
  1034. struct file *filep;
  1035. int ret;
  1036. if (0 == atomic_read(&group->container_users) ||
  1037. !group->container->iommu_driver || !vfio_group_viable(group))
  1038. return -EINVAL;
  1039. device = vfio_device_get_from_name(group, buf);
  1040. if (!device)
  1041. return -ENODEV;
  1042. ret = device->ops->open(device->device_data);
  1043. if (ret) {
  1044. vfio_device_put(device);
  1045. return ret;
  1046. }
  1047. /*
  1048. * We can't use anon_inode_getfd() because we need to modify
  1049. * the f_mode flags directly to allow more than just ioctls
  1050. */
  1051. ret = get_unused_fd_flags(O_CLOEXEC);
  1052. if (ret < 0) {
  1053. device->ops->release(device->device_data);
  1054. vfio_device_put(device);
  1055. return ret;
  1056. }
  1057. filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
  1058. device, O_RDWR);
  1059. if (IS_ERR(filep)) {
  1060. put_unused_fd(ret);
  1061. ret = PTR_ERR(filep);
  1062. device->ops->release(device->device_data);
  1063. vfio_device_put(device);
  1064. return ret;
  1065. }
  1066. /*
  1067. * TODO: add an anon_inode interface to do this.
  1068. * Appears to be missing by lack of need rather than
  1069. * explicitly prevented. Now there's need.
  1070. */
  1071. filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1072. atomic_inc(&group->container_users);
  1073. fd_install(ret, filep);
  1074. return ret;
  1075. }
  1076. static long vfio_group_fops_unl_ioctl(struct file *filep,
  1077. unsigned int cmd, unsigned long arg)
  1078. {
  1079. struct vfio_group *group = filep->private_data;
  1080. long ret = -ENOTTY;
  1081. switch (cmd) {
  1082. case VFIO_GROUP_GET_STATUS:
  1083. {
  1084. struct vfio_group_status status;
  1085. unsigned long minsz;
  1086. minsz = offsetofend(struct vfio_group_status, flags);
  1087. if (copy_from_user(&status, (void __user *)arg, minsz))
  1088. return -EFAULT;
  1089. if (status.argsz < minsz)
  1090. return -EINVAL;
  1091. status.flags = 0;
  1092. if (vfio_group_viable(group))
  1093. status.flags |= VFIO_GROUP_FLAGS_VIABLE;
  1094. if (group->container)
  1095. status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
  1096. if (copy_to_user((void __user *)arg, &status, minsz))
  1097. return -EFAULT;
  1098. ret = 0;
  1099. break;
  1100. }
  1101. case VFIO_GROUP_SET_CONTAINER:
  1102. {
  1103. int fd;
  1104. if (get_user(fd, (int __user *)arg))
  1105. return -EFAULT;
  1106. if (fd < 0)
  1107. return -EINVAL;
  1108. ret = vfio_group_set_container(group, fd);
  1109. break;
  1110. }
  1111. case VFIO_GROUP_UNSET_CONTAINER:
  1112. ret = vfio_group_unset_container(group);
  1113. break;
  1114. case VFIO_GROUP_GET_DEVICE_FD:
  1115. {
  1116. char *buf;
  1117. buf = strndup_user((const char __user *)arg, PAGE_SIZE);
  1118. if (IS_ERR(buf))
  1119. return PTR_ERR(buf);
  1120. ret = vfio_group_get_device_fd(group, buf);
  1121. kfree(buf);
  1122. break;
  1123. }
  1124. }
  1125. return ret;
  1126. }
  1127. #ifdef CONFIG_COMPAT
  1128. static long vfio_group_fops_compat_ioctl(struct file *filep,
  1129. unsigned int cmd, unsigned long arg)
  1130. {
  1131. arg = (unsigned long)compat_ptr(arg);
  1132. return vfio_group_fops_unl_ioctl(filep, cmd, arg);
  1133. }
  1134. #endif /* CONFIG_COMPAT */
  1135. static int vfio_group_fops_open(struct inode *inode, struct file *filep)
  1136. {
  1137. struct vfio_group *group;
  1138. int opened;
  1139. group = vfio_group_get_from_minor(iminor(inode));
  1140. if (!group)
  1141. return -ENODEV;
  1142. /* Do we need multiple instances of the group open? Seems not. */
  1143. opened = atomic_cmpxchg(&group->opened, 0, 1);
  1144. if (opened) {
  1145. vfio_group_put(group);
  1146. return -EBUSY;
  1147. }
  1148. /* Is something still in use from a previous open? */
  1149. if (group->container) {
  1150. atomic_dec(&group->opened);
  1151. vfio_group_put(group);
  1152. return -EBUSY;
  1153. }
  1154. filep->private_data = group;
  1155. return 0;
  1156. }
  1157. static int vfio_group_fops_release(struct inode *inode, struct file *filep)
  1158. {
  1159. struct vfio_group *group = filep->private_data;
  1160. filep->private_data = NULL;
  1161. vfio_group_try_dissolve_container(group);
  1162. atomic_dec(&group->opened);
  1163. vfio_group_put(group);
  1164. return 0;
  1165. }
  1166. static const struct file_operations vfio_group_fops = {
  1167. .owner = THIS_MODULE,
  1168. .unlocked_ioctl = vfio_group_fops_unl_ioctl,
  1169. #ifdef CONFIG_COMPAT
  1170. .compat_ioctl = vfio_group_fops_compat_ioctl,
  1171. #endif
  1172. .open = vfio_group_fops_open,
  1173. .release = vfio_group_fops_release,
  1174. };
  1175. /**
  1176. * VFIO Device fd
  1177. */
  1178. static int vfio_device_fops_release(struct inode *inode, struct file *filep)
  1179. {
  1180. struct vfio_device *device = filep->private_data;
  1181. device->ops->release(device->device_data);
  1182. vfio_group_try_dissolve_container(device->group);
  1183. vfio_device_put(device);
  1184. return 0;
  1185. }
  1186. static long vfio_device_fops_unl_ioctl(struct file *filep,
  1187. unsigned int cmd, unsigned long arg)
  1188. {
  1189. struct vfio_device *device = filep->private_data;
  1190. if (unlikely(!device->ops->ioctl))
  1191. return -EINVAL;
  1192. return device->ops->ioctl(device->device_data, cmd, arg);
  1193. }
  1194. static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
  1195. size_t count, loff_t *ppos)
  1196. {
  1197. struct vfio_device *device = filep->private_data;
  1198. if (unlikely(!device->ops->read))
  1199. return -EINVAL;
  1200. return device->ops->read(device->device_data, buf, count, ppos);
  1201. }
  1202. static ssize_t vfio_device_fops_write(struct file *filep,
  1203. const char __user *buf,
  1204. size_t count, loff_t *ppos)
  1205. {
  1206. struct vfio_device *device = filep->private_data;
  1207. if (unlikely(!device->ops->write))
  1208. return -EINVAL;
  1209. return device->ops->write(device->device_data, buf, count, ppos);
  1210. }
  1211. static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  1212. {
  1213. struct vfio_device *device = filep->private_data;
  1214. if (unlikely(!device->ops->mmap))
  1215. return -EINVAL;
  1216. return device->ops->mmap(device->device_data, vma);
  1217. }
  1218. #ifdef CONFIG_COMPAT
  1219. static long vfio_device_fops_compat_ioctl(struct file *filep,
  1220. unsigned int cmd, unsigned long arg)
  1221. {
  1222. arg = (unsigned long)compat_ptr(arg);
  1223. return vfio_device_fops_unl_ioctl(filep, cmd, arg);
  1224. }
  1225. #endif /* CONFIG_COMPAT */
  1226. static const struct file_operations vfio_device_fops = {
  1227. .owner = THIS_MODULE,
  1228. .release = vfio_device_fops_release,
  1229. .read = vfio_device_fops_read,
  1230. .write = vfio_device_fops_write,
  1231. .unlocked_ioctl = vfio_device_fops_unl_ioctl,
  1232. #ifdef CONFIG_COMPAT
  1233. .compat_ioctl = vfio_device_fops_compat_ioctl,
  1234. #endif
  1235. .mmap = vfio_device_fops_mmap,
  1236. };
  1237. /**
  1238. * External user API, exported by symbols to be linked dynamically.
  1239. *
  1240. * The protocol includes:
  1241. * 1. do normal VFIO init operation:
  1242. * - opening a new container;
  1243. * - attaching group(s) to it;
  1244. * - setting an IOMMU driver for a container.
  1245. * When IOMMU is set for a container, all groups in it are
  1246. * considered ready to use by an external user.
  1247. *
  1248. * 2. User space passes a group fd to an external user.
  1249. * The external user calls vfio_group_get_external_user()
  1250. * to verify that:
  1251. * - the group is initialized;
  1252. * - IOMMU is set for it.
  1253. * If both checks passed, vfio_group_get_external_user()
  1254. * increments the container user counter to prevent
  1255. * the VFIO group from disposal before KVM exits.
  1256. *
  1257. * 3. The external user calls vfio_external_user_iommu_id()
  1258. * to know an IOMMU ID.
  1259. *
  1260. * 4. When the external KVM finishes, it calls
  1261. * vfio_group_put_external_user() to release the VFIO group.
  1262. * This call decrements the container user counter.
  1263. */
  1264. struct vfio_group *vfio_group_get_external_user(struct file *filep)
  1265. {
  1266. struct vfio_group *group = filep->private_data;
  1267. if (filep->f_op != &vfio_group_fops)
  1268. return ERR_PTR(-EINVAL);
  1269. if (!atomic_inc_not_zero(&group->container_users))
  1270. return ERR_PTR(-EINVAL);
  1271. if (!group->container->iommu_driver ||
  1272. !vfio_group_viable(group)) {
  1273. atomic_dec(&group->container_users);
  1274. return ERR_PTR(-EINVAL);
  1275. }
  1276. vfio_group_get(group);
  1277. return group;
  1278. }
  1279. EXPORT_SYMBOL_GPL(vfio_group_get_external_user);
  1280. void vfio_group_put_external_user(struct vfio_group *group)
  1281. {
  1282. vfio_group_put(group);
  1283. vfio_group_try_dissolve_container(group);
  1284. }
  1285. EXPORT_SYMBOL_GPL(vfio_group_put_external_user);
  1286. int vfio_external_user_iommu_id(struct vfio_group *group)
  1287. {
  1288. return iommu_group_id(group->iommu_group);
  1289. }
  1290. EXPORT_SYMBOL_GPL(vfio_external_user_iommu_id);
  1291. long vfio_external_check_extension(struct vfio_group *group, unsigned long arg)
  1292. {
  1293. return vfio_ioctl_check_extension(group->container, arg);
  1294. }
  1295. EXPORT_SYMBOL_GPL(vfio_external_check_extension);
  1296. /**
  1297. * Module/class support
  1298. */
  1299. static char *vfio_devnode(struct device *dev, umode_t *mode)
  1300. {
  1301. return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
  1302. }
  1303. static struct miscdevice vfio_dev = {
  1304. .minor = VFIO_MINOR,
  1305. .name = "vfio",
  1306. .fops = &vfio_fops,
  1307. .nodename = "vfio/vfio",
  1308. .mode = S_IRUGO | S_IWUGO,
  1309. };
  1310. static int __init vfio_init(void)
  1311. {
  1312. int ret;
  1313. idr_init(&vfio.group_idr);
  1314. mutex_init(&vfio.group_lock);
  1315. mutex_init(&vfio.iommu_drivers_lock);
  1316. INIT_LIST_HEAD(&vfio.group_list);
  1317. INIT_LIST_HEAD(&vfio.iommu_drivers_list);
  1318. init_waitqueue_head(&vfio.release_q);
  1319. ret = misc_register(&vfio_dev);
  1320. if (ret) {
  1321. pr_err("vfio: misc device register failed\n");
  1322. return ret;
  1323. }
  1324. /* /dev/vfio/$GROUP */
  1325. vfio.class = class_create(THIS_MODULE, "vfio");
  1326. if (IS_ERR(vfio.class)) {
  1327. ret = PTR_ERR(vfio.class);
  1328. goto err_class;
  1329. }
  1330. vfio.class->devnode = vfio_devnode;
  1331. ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK, "vfio");
  1332. if (ret)
  1333. goto err_alloc_chrdev;
  1334. cdev_init(&vfio.group_cdev, &vfio_group_fops);
  1335. ret = cdev_add(&vfio.group_cdev, vfio.group_devt, MINORMASK);
  1336. if (ret)
  1337. goto err_cdev_add;
  1338. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  1339. /*
  1340. * Attempt to load known iommu-drivers. This gives us a working
  1341. * environment without the user needing to explicitly load iommu
  1342. * drivers.
  1343. */
  1344. request_module_nowait("vfio_iommu_type1");
  1345. request_module_nowait("vfio_iommu_spapr_tce");
  1346. return 0;
  1347. err_cdev_add:
  1348. unregister_chrdev_region(vfio.group_devt, MINORMASK);
  1349. err_alloc_chrdev:
  1350. class_destroy(vfio.class);
  1351. vfio.class = NULL;
  1352. err_class:
  1353. misc_deregister(&vfio_dev);
  1354. return ret;
  1355. }
  1356. static void __exit vfio_cleanup(void)
  1357. {
  1358. WARN_ON(!list_empty(&vfio.group_list));
  1359. idr_destroy(&vfio.group_idr);
  1360. cdev_del(&vfio.group_cdev);
  1361. unregister_chrdev_region(vfio.group_devt, MINORMASK);
  1362. class_destroy(vfio.class);
  1363. vfio.class = NULL;
  1364. misc_deregister(&vfio_dev);
  1365. }
  1366. module_init(vfio_init);
  1367. module_exit(vfio_cleanup);
  1368. MODULE_VERSION(DRIVER_VERSION);
  1369. MODULE_LICENSE("GPL v2");
  1370. MODULE_AUTHOR(DRIVER_AUTHOR);
  1371. MODULE_DESCRIPTION(DRIVER_DESC);
  1372. MODULE_ALIAS_MISCDEV(VFIO_MINOR);
  1373. MODULE_ALIAS("devname:vfio/vfio");