vfio.c 40 KB

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