vfio.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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 that is known to
  567. * be bound to a vfio driver. The driver implicitly holds a
  568. * vfio_device reference between vfio_add_group_dev and
  569. * vfio_del_group_dev. We can therefore use drvdata to increment
  570. * that reference from the struct device. This additional
  571. * reference must be released by calling vfio_device_put.
  572. */
  573. struct vfio_device *vfio_device_get_from_dev(struct device *dev)
  574. {
  575. struct vfio_device *device = dev_get_drvdata(dev);
  576. vfio_device_get(device);
  577. return device;
  578. }
  579. EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
  580. /*
  581. * Caller must hold a reference to the vfio_device
  582. */
  583. void *vfio_device_data(struct vfio_device *device)
  584. {
  585. return device->device_data;
  586. }
  587. EXPORT_SYMBOL_GPL(vfio_device_data);
  588. /* Given a referenced group, check if it contains the device */
  589. static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
  590. {
  591. struct vfio_device *device;
  592. device = vfio_group_get_device(group, dev);
  593. if (!device)
  594. return false;
  595. vfio_device_put(device);
  596. return true;
  597. }
  598. /*
  599. * Decrement the device reference count and wait for the device to be
  600. * removed. Open file descriptors for the device... */
  601. void *vfio_del_group_dev(struct device *dev)
  602. {
  603. struct vfio_device *device = dev_get_drvdata(dev);
  604. struct vfio_group *group = device->group;
  605. void *device_data = device->device_data;
  606. struct vfio_unbound_dev *unbound;
  607. unsigned int i = 0;
  608. long ret;
  609. bool interrupted = false;
  610. /*
  611. * The group exists so long as we have a device reference. Get
  612. * a group reference and use it to scan for the device going away.
  613. */
  614. vfio_group_get(group);
  615. /*
  616. * When the device is removed from the group, the group suddenly
  617. * becomes non-viable; the device has a driver (until the unbind
  618. * completes), but it's not present in the group. This is bad news
  619. * for any external users that need to re-acquire a group reference
  620. * in order to match and release their existing reference. To
  621. * solve this, we track such devices on the unbound_list to bridge
  622. * the gap until they're fully unbound.
  623. */
  624. unbound = kzalloc(sizeof(*unbound), GFP_KERNEL);
  625. if (unbound) {
  626. unbound->dev = dev;
  627. mutex_lock(&group->unbound_lock);
  628. list_add(&unbound->unbound_next, &group->unbound_list);
  629. mutex_unlock(&group->unbound_lock);
  630. }
  631. WARN_ON(!unbound);
  632. vfio_device_put(device);
  633. /*
  634. * If the device is still present in the group after the above
  635. * 'put', then it is in use and we need to request it from the
  636. * bus driver. The driver may in turn need to request the
  637. * device from the user. We send the request on an arbitrary
  638. * interval with counter to allow the driver to take escalating
  639. * measures to release the device if it has the ability to do so.
  640. */
  641. do {
  642. device = vfio_group_get_device(group, dev);
  643. if (!device)
  644. break;
  645. if (device->ops->request)
  646. device->ops->request(device_data, i++);
  647. vfio_device_put(device);
  648. if (interrupted) {
  649. ret = wait_event_timeout(vfio.release_q,
  650. !vfio_dev_present(group, dev), HZ * 10);
  651. } else {
  652. ret = wait_event_interruptible_timeout(vfio.release_q,
  653. !vfio_dev_present(group, dev), HZ * 10);
  654. if (ret == -ERESTARTSYS) {
  655. interrupted = true;
  656. dev_warn(dev,
  657. "Device is currently in use, task"
  658. " \"%s\" (%d) "
  659. "blocked until device is released",
  660. current->comm, task_pid_nr(current));
  661. }
  662. }
  663. } while (ret <= 0);
  664. vfio_group_put(group);
  665. return device_data;
  666. }
  667. EXPORT_SYMBOL_GPL(vfio_del_group_dev);
  668. /**
  669. * VFIO base fd, /dev/vfio/vfio
  670. */
  671. static long vfio_ioctl_check_extension(struct vfio_container *container,
  672. unsigned long arg)
  673. {
  674. struct vfio_iommu_driver *driver;
  675. long ret = 0;
  676. down_read(&container->group_lock);
  677. driver = container->iommu_driver;
  678. switch (arg) {
  679. /* No base extensions yet */
  680. default:
  681. /*
  682. * If no driver is set, poll all registered drivers for
  683. * extensions and return the first positive result. If
  684. * a driver is already set, further queries will be passed
  685. * only to that driver.
  686. */
  687. if (!driver) {
  688. mutex_lock(&vfio.iommu_drivers_lock);
  689. list_for_each_entry(driver, &vfio.iommu_drivers_list,
  690. vfio_next) {
  691. if (!try_module_get(driver->ops->owner))
  692. continue;
  693. ret = driver->ops->ioctl(NULL,
  694. VFIO_CHECK_EXTENSION,
  695. arg);
  696. module_put(driver->ops->owner);
  697. if (ret > 0)
  698. break;
  699. }
  700. mutex_unlock(&vfio.iommu_drivers_lock);
  701. } else
  702. ret = driver->ops->ioctl(container->iommu_data,
  703. VFIO_CHECK_EXTENSION, arg);
  704. }
  705. up_read(&container->group_lock);
  706. return ret;
  707. }
  708. /* hold write lock on container->group_lock */
  709. static int __vfio_container_attach_groups(struct vfio_container *container,
  710. struct vfio_iommu_driver *driver,
  711. void *data)
  712. {
  713. struct vfio_group *group;
  714. int ret = -ENODEV;
  715. list_for_each_entry(group, &container->group_list, container_next) {
  716. ret = driver->ops->attach_group(data, group->iommu_group);
  717. if (ret)
  718. goto unwind;
  719. }
  720. return ret;
  721. unwind:
  722. list_for_each_entry_continue_reverse(group, &container->group_list,
  723. container_next) {
  724. driver->ops->detach_group(data, group->iommu_group);
  725. }
  726. return ret;
  727. }
  728. static long vfio_ioctl_set_iommu(struct vfio_container *container,
  729. unsigned long arg)
  730. {
  731. struct vfio_iommu_driver *driver;
  732. long ret = -ENODEV;
  733. down_write(&container->group_lock);
  734. /*
  735. * The container is designed to be an unprivileged interface while
  736. * the group can be assigned to specific users. Therefore, only by
  737. * adding a group to a container does the user get the privilege of
  738. * enabling the iommu, which may allocate finite resources. There
  739. * is no unset_iommu, but by removing all the groups from a container,
  740. * the container is deprivileged and returns to an unset state.
  741. */
  742. if (list_empty(&container->group_list) || container->iommu_driver) {
  743. up_write(&container->group_lock);
  744. return -EINVAL;
  745. }
  746. mutex_lock(&vfio.iommu_drivers_lock);
  747. list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
  748. void *data;
  749. if (!try_module_get(driver->ops->owner))
  750. continue;
  751. /*
  752. * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
  753. * so test which iommu driver reported support for this
  754. * extension and call open on them. We also pass them the
  755. * magic, allowing a single driver to support multiple
  756. * interfaces if they'd like.
  757. */
  758. if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
  759. module_put(driver->ops->owner);
  760. continue;
  761. }
  762. /* module reference holds the driver we're working on */
  763. mutex_unlock(&vfio.iommu_drivers_lock);
  764. data = driver->ops->open(arg);
  765. if (IS_ERR(data)) {
  766. ret = PTR_ERR(data);
  767. module_put(driver->ops->owner);
  768. goto skip_drivers_unlock;
  769. }
  770. ret = __vfio_container_attach_groups(container, driver, data);
  771. if (!ret) {
  772. container->iommu_driver = driver;
  773. container->iommu_data = data;
  774. } else {
  775. driver->ops->release(data);
  776. module_put(driver->ops->owner);
  777. }
  778. goto skip_drivers_unlock;
  779. }
  780. mutex_unlock(&vfio.iommu_drivers_lock);
  781. skip_drivers_unlock:
  782. up_write(&container->group_lock);
  783. return ret;
  784. }
  785. static long vfio_fops_unl_ioctl(struct file *filep,
  786. unsigned int cmd, unsigned long arg)
  787. {
  788. struct vfio_container *container = filep->private_data;
  789. struct vfio_iommu_driver *driver;
  790. void *data;
  791. long ret = -EINVAL;
  792. if (!container)
  793. return ret;
  794. switch (cmd) {
  795. case VFIO_GET_API_VERSION:
  796. ret = VFIO_API_VERSION;
  797. break;
  798. case VFIO_CHECK_EXTENSION:
  799. ret = vfio_ioctl_check_extension(container, arg);
  800. break;
  801. case VFIO_SET_IOMMU:
  802. ret = vfio_ioctl_set_iommu(container, arg);
  803. break;
  804. default:
  805. down_read(&container->group_lock);
  806. driver = container->iommu_driver;
  807. data = container->iommu_data;
  808. if (driver) /* passthrough all unrecognized ioctls */
  809. ret = driver->ops->ioctl(data, cmd, arg);
  810. up_read(&container->group_lock);
  811. }
  812. return ret;
  813. }
  814. #ifdef CONFIG_COMPAT
  815. static long vfio_fops_compat_ioctl(struct file *filep,
  816. unsigned int cmd, unsigned long arg)
  817. {
  818. arg = (unsigned long)compat_ptr(arg);
  819. return vfio_fops_unl_ioctl(filep, cmd, arg);
  820. }
  821. #endif /* CONFIG_COMPAT */
  822. static int vfio_fops_open(struct inode *inode, struct file *filep)
  823. {
  824. struct vfio_container *container;
  825. container = kzalloc(sizeof(*container), GFP_KERNEL);
  826. if (!container)
  827. return -ENOMEM;
  828. INIT_LIST_HEAD(&container->group_list);
  829. init_rwsem(&container->group_lock);
  830. kref_init(&container->kref);
  831. filep->private_data = container;
  832. return 0;
  833. }
  834. static int vfio_fops_release(struct inode *inode, struct file *filep)
  835. {
  836. struct vfio_container *container = filep->private_data;
  837. filep->private_data = NULL;
  838. vfio_container_put(container);
  839. return 0;
  840. }
  841. /*
  842. * Once an iommu driver is set, we optionally pass read/write/mmap
  843. * on to the driver, allowing management interfaces beyond ioctl.
  844. */
  845. static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
  846. size_t count, loff_t *ppos)
  847. {
  848. struct vfio_container *container = filep->private_data;
  849. struct vfio_iommu_driver *driver;
  850. ssize_t ret = -EINVAL;
  851. down_read(&container->group_lock);
  852. driver = container->iommu_driver;
  853. if (likely(driver && driver->ops->read))
  854. ret = driver->ops->read(container->iommu_data,
  855. buf, count, ppos);
  856. up_read(&container->group_lock);
  857. return ret;
  858. }
  859. static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
  860. size_t count, loff_t *ppos)
  861. {
  862. struct vfio_container *container = filep->private_data;
  863. struct vfio_iommu_driver *driver;
  864. ssize_t ret = -EINVAL;
  865. down_read(&container->group_lock);
  866. driver = container->iommu_driver;
  867. if (likely(driver && driver->ops->write))
  868. ret = driver->ops->write(container->iommu_data,
  869. buf, count, ppos);
  870. up_read(&container->group_lock);
  871. return ret;
  872. }
  873. static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  874. {
  875. struct vfio_container *container = filep->private_data;
  876. struct vfio_iommu_driver *driver;
  877. int ret = -EINVAL;
  878. down_read(&container->group_lock);
  879. driver = container->iommu_driver;
  880. if (likely(driver && driver->ops->mmap))
  881. ret = driver->ops->mmap(container->iommu_data, vma);
  882. up_read(&container->group_lock);
  883. return ret;
  884. }
  885. static const struct file_operations vfio_fops = {
  886. .owner = THIS_MODULE,
  887. .open = vfio_fops_open,
  888. .release = vfio_fops_release,
  889. .read = vfio_fops_read,
  890. .write = vfio_fops_write,
  891. .unlocked_ioctl = vfio_fops_unl_ioctl,
  892. #ifdef CONFIG_COMPAT
  893. .compat_ioctl = vfio_fops_compat_ioctl,
  894. #endif
  895. .mmap = vfio_fops_mmap,
  896. };
  897. /**
  898. * VFIO Group fd, /dev/vfio/$GROUP
  899. */
  900. static void __vfio_group_unset_container(struct vfio_group *group)
  901. {
  902. struct vfio_container *container = group->container;
  903. struct vfio_iommu_driver *driver;
  904. down_write(&container->group_lock);
  905. driver = container->iommu_driver;
  906. if (driver)
  907. driver->ops->detach_group(container->iommu_data,
  908. group->iommu_group);
  909. group->container = NULL;
  910. list_del(&group->container_next);
  911. /* Detaching the last group deprivileges a container, remove iommu */
  912. if (driver && list_empty(&container->group_list)) {
  913. driver->ops->release(container->iommu_data);
  914. module_put(driver->ops->owner);
  915. container->iommu_driver = NULL;
  916. container->iommu_data = NULL;
  917. }
  918. up_write(&container->group_lock);
  919. vfio_container_put(container);
  920. }
  921. /*
  922. * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
  923. * if there was no container to unset. Since the ioctl is called on
  924. * the group, we know that still exists, therefore the only valid
  925. * transition here is 1->0.
  926. */
  927. static int vfio_group_unset_container(struct vfio_group *group)
  928. {
  929. int users = atomic_cmpxchg(&group->container_users, 1, 0);
  930. if (!users)
  931. return -EINVAL;
  932. if (users != 1)
  933. return -EBUSY;
  934. __vfio_group_unset_container(group);
  935. return 0;
  936. }
  937. /*
  938. * When removing container users, anything that removes the last user
  939. * implicitly removes the group from the container. That is, if the
  940. * group file descriptor is closed, as well as any device file descriptors,
  941. * the group is free.
  942. */
  943. static void vfio_group_try_dissolve_container(struct vfio_group *group)
  944. {
  945. if (0 == atomic_dec_if_positive(&group->container_users))
  946. __vfio_group_unset_container(group);
  947. }
  948. static int vfio_group_set_container(struct vfio_group *group, int container_fd)
  949. {
  950. struct fd f;
  951. struct vfio_container *container;
  952. struct vfio_iommu_driver *driver;
  953. int ret = 0;
  954. if (atomic_read(&group->container_users))
  955. return -EINVAL;
  956. f = fdget(container_fd);
  957. if (!f.file)
  958. return -EBADF;
  959. /* Sanity check, is this really our fd? */
  960. if (f.file->f_op != &vfio_fops) {
  961. fdput(f);
  962. return -EINVAL;
  963. }
  964. container = f.file->private_data;
  965. WARN_ON(!container); /* fget ensures we don't race vfio_release */
  966. down_write(&container->group_lock);
  967. driver = container->iommu_driver;
  968. if (driver) {
  969. ret = driver->ops->attach_group(container->iommu_data,
  970. group->iommu_group);
  971. if (ret)
  972. goto unlock_out;
  973. }
  974. group->container = container;
  975. list_add(&group->container_next, &container->group_list);
  976. /* Get a reference on the container and mark a user within the group */
  977. vfio_container_get(container);
  978. atomic_inc(&group->container_users);
  979. unlock_out:
  980. up_write(&container->group_lock);
  981. fdput(f);
  982. return ret;
  983. }
  984. static bool vfio_group_viable(struct vfio_group *group)
  985. {
  986. return (iommu_group_for_each_dev(group->iommu_group,
  987. group, vfio_dev_viable) == 0);
  988. }
  989. static const struct file_operations vfio_device_fops;
  990. static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
  991. {
  992. struct vfio_device *device;
  993. struct file *filep;
  994. int ret = -ENODEV;
  995. if (0 == atomic_read(&group->container_users) ||
  996. !group->container->iommu_driver || !vfio_group_viable(group))
  997. return -EINVAL;
  998. mutex_lock(&group->device_lock);
  999. list_for_each_entry(device, &group->device_list, group_next) {
  1000. if (strcmp(dev_name(device->dev), buf))
  1001. continue;
  1002. ret = device->ops->open(device->device_data);
  1003. if (ret)
  1004. break;
  1005. /*
  1006. * We can't use anon_inode_getfd() because we need to modify
  1007. * the f_mode flags directly to allow more than just ioctls
  1008. */
  1009. ret = get_unused_fd_flags(O_CLOEXEC);
  1010. if (ret < 0) {
  1011. device->ops->release(device->device_data);
  1012. break;
  1013. }
  1014. filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
  1015. device, O_RDWR);
  1016. if (IS_ERR(filep)) {
  1017. put_unused_fd(ret);
  1018. ret = PTR_ERR(filep);
  1019. device->ops->release(device->device_data);
  1020. break;
  1021. }
  1022. /*
  1023. * TODO: add an anon_inode interface to do this.
  1024. * Appears to be missing by lack of need rather than
  1025. * explicitly prevented. Now there's need.
  1026. */
  1027. filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1028. vfio_device_get(device);
  1029. atomic_inc(&group->container_users);
  1030. fd_install(ret, filep);
  1031. break;
  1032. }
  1033. mutex_unlock(&group->device_lock);
  1034. return ret;
  1035. }
  1036. static long vfio_group_fops_unl_ioctl(struct file *filep,
  1037. unsigned int cmd, unsigned long arg)
  1038. {
  1039. struct vfio_group *group = filep->private_data;
  1040. long ret = -ENOTTY;
  1041. switch (cmd) {
  1042. case VFIO_GROUP_GET_STATUS:
  1043. {
  1044. struct vfio_group_status status;
  1045. unsigned long minsz;
  1046. minsz = offsetofend(struct vfio_group_status, flags);
  1047. if (copy_from_user(&status, (void __user *)arg, minsz))
  1048. return -EFAULT;
  1049. if (status.argsz < minsz)
  1050. return -EINVAL;
  1051. status.flags = 0;
  1052. if (vfio_group_viable(group))
  1053. status.flags |= VFIO_GROUP_FLAGS_VIABLE;
  1054. if (group->container)
  1055. status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
  1056. if (copy_to_user((void __user *)arg, &status, minsz))
  1057. return -EFAULT;
  1058. ret = 0;
  1059. break;
  1060. }
  1061. case VFIO_GROUP_SET_CONTAINER:
  1062. {
  1063. int fd;
  1064. if (get_user(fd, (int __user *)arg))
  1065. return -EFAULT;
  1066. if (fd < 0)
  1067. return -EINVAL;
  1068. ret = vfio_group_set_container(group, fd);
  1069. break;
  1070. }
  1071. case VFIO_GROUP_UNSET_CONTAINER:
  1072. ret = vfio_group_unset_container(group);
  1073. break;
  1074. case VFIO_GROUP_GET_DEVICE_FD:
  1075. {
  1076. char *buf;
  1077. buf = strndup_user((const char __user *)arg, PAGE_SIZE);
  1078. if (IS_ERR(buf))
  1079. return PTR_ERR(buf);
  1080. ret = vfio_group_get_device_fd(group, buf);
  1081. kfree(buf);
  1082. break;
  1083. }
  1084. }
  1085. return ret;
  1086. }
  1087. #ifdef CONFIG_COMPAT
  1088. static long vfio_group_fops_compat_ioctl(struct file *filep,
  1089. unsigned int cmd, unsigned long arg)
  1090. {
  1091. arg = (unsigned long)compat_ptr(arg);
  1092. return vfio_group_fops_unl_ioctl(filep, cmd, arg);
  1093. }
  1094. #endif /* CONFIG_COMPAT */
  1095. static int vfio_group_fops_open(struct inode *inode, struct file *filep)
  1096. {
  1097. struct vfio_group *group;
  1098. int opened;
  1099. group = vfio_group_get_from_minor(iminor(inode));
  1100. if (!group)
  1101. return -ENODEV;
  1102. /* Do we need multiple instances of the group open? Seems not. */
  1103. opened = atomic_cmpxchg(&group->opened, 0, 1);
  1104. if (opened) {
  1105. vfio_group_put(group);
  1106. return -EBUSY;
  1107. }
  1108. /* Is something still in use from a previous open? */
  1109. if (group->container) {
  1110. atomic_dec(&group->opened);
  1111. vfio_group_put(group);
  1112. return -EBUSY;
  1113. }
  1114. filep->private_data = group;
  1115. return 0;
  1116. }
  1117. static int vfio_group_fops_release(struct inode *inode, struct file *filep)
  1118. {
  1119. struct vfio_group *group = filep->private_data;
  1120. filep->private_data = NULL;
  1121. vfio_group_try_dissolve_container(group);
  1122. atomic_dec(&group->opened);
  1123. vfio_group_put(group);
  1124. return 0;
  1125. }
  1126. static const struct file_operations vfio_group_fops = {
  1127. .owner = THIS_MODULE,
  1128. .unlocked_ioctl = vfio_group_fops_unl_ioctl,
  1129. #ifdef CONFIG_COMPAT
  1130. .compat_ioctl = vfio_group_fops_compat_ioctl,
  1131. #endif
  1132. .open = vfio_group_fops_open,
  1133. .release = vfio_group_fops_release,
  1134. };
  1135. /**
  1136. * VFIO Device fd
  1137. */
  1138. static int vfio_device_fops_release(struct inode *inode, struct file *filep)
  1139. {
  1140. struct vfio_device *device = filep->private_data;
  1141. device->ops->release(device->device_data);
  1142. vfio_group_try_dissolve_container(device->group);
  1143. vfio_device_put(device);
  1144. return 0;
  1145. }
  1146. static long vfio_device_fops_unl_ioctl(struct file *filep,
  1147. unsigned int cmd, unsigned long arg)
  1148. {
  1149. struct vfio_device *device = filep->private_data;
  1150. if (unlikely(!device->ops->ioctl))
  1151. return -EINVAL;
  1152. return device->ops->ioctl(device->device_data, cmd, arg);
  1153. }
  1154. static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
  1155. size_t count, loff_t *ppos)
  1156. {
  1157. struct vfio_device *device = filep->private_data;
  1158. if (unlikely(!device->ops->read))
  1159. return -EINVAL;
  1160. return device->ops->read(device->device_data, buf, count, ppos);
  1161. }
  1162. static ssize_t vfio_device_fops_write(struct file *filep,
  1163. const char __user *buf,
  1164. size_t count, loff_t *ppos)
  1165. {
  1166. struct vfio_device *device = filep->private_data;
  1167. if (unlikely(!device->ops->write))
  1168. return -EINVAL;
  1169. return device->ops->write(device->device_data, buf, count, ppos);
  1170. }
  1171. static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  1172. {
  1173. struct vfio_device *device = filep->private_data;
  1174. if (unlikely(!device->ops->mmap))
  1175. return -EINVAL;
  1176. return device->ops->mmap(device->device_data, vma);
  1177. }
  1178. #ifdef CONFIG_COMPAT
  1179. static long vfio_device_fops_compat_ioctl(struct file *filep,
  1180. unsigned int cmd, unsigned long arg)
  1181. {
  1182. arg = (unsigned long)compat_ptr(arg);
  1183. return vfio_device_fops_unl_ioctl(filep, cmd, arg);
  1184. }
  1185. #endif /* CONFIG_COMPAT */
  1186. static const struct file_operations vfio_device_fops = {
  1187. .owner = THIS_MODULE,
  1188. .release = vfio_device_fops_release,
  1189. .read = vfio_device_fops_read,
  1190. .write = vfio_device_fops_write,
  1191. .unlocked_ioctl = vfio_device_fops_unl_ioctl,
  1192. #ifdef CONFIG_COMPAT
  1193. .compat_ioctl = vfio_device_fops_compat_ioctl,
  1194. #endif
  1195. .mmap = vfio_device_fops_mmap,
  1196. };
  1197. /**
  1198. * External user API, exported by symbols to be linked dynamically.
  1199. *
  1200. * The protocol includes:
  1201. * 1. do normal VFIO init operation:
  1202. * - opening a new container;
  1203. * - attaching group(s) to it;
  1204. * - setting an IOMMU driver for a container.
  1205. * When IOMMU is set for a container, all groups in it are
  1206. * considered ready to use by an external user.
  1207. *
  1208. * 2. User space passes a group fd to an external user.
  1209. * The external user calls vfio_group_get_external_user()
  1210. * to verify that:
  1211. * - the group is initialized;
  1212. * - IOMMU is set for it.
  1213. * If both checks passed, vfio_group_get_external_user()
  1214. * increments the container user counter to prevent
  1215. * the VFIO group from disposal before KVM exits.
  1216. *
  1217. * 3. The external user calls vfio_external_user_iommu_id()
  1218. * to know an IOMMU ID.
  1219. *
  1220. * 4. When the external KVM finishes, it calls
  1221. * vfio_group_put_external_user() to release the VFIO group.
  1222. * This call decrements the container user counter.
  1223. */
  1224. struct vfio_group *vfio_group_get_external_user(struct file *filep)
  1225. {
  1226. struct vfio_group *group = filep->private_data;
  1227. if (filep->f_op != &vfio_group_fops)
  1228. return ERR_PTR(-EINVAL);
  1229. if (!atomic_inc_not_zero(&group->container_users))
  1230. return ERR_PTR(-EINVAL);
  1231. if (!group->container->iommu_driver ||
  1232. !vfio_group_viable(group)) {
  1233. atomic_dec(&group->container_users);
  1234. return ERR_PTR(-EINVAL);
  1235. }
  1236. vfio_group_get(group);
  1237. return group;
  1238. }
  1239. EXPORT_SYMBOL_GPL(vfio_group_get_external_user);
  1240. void vfio_group_put_external_user(struct vfio_group *group)
  1241. {
  1242. vfio_group_put(group);
  1243. vfio_group_try_dissolve_container(group);
  1244. }
  1245. EXPORT_SYMBOL_GPL(vfio_group_put_external_user);
  1246. int vfio_external_user_iommu_id(struct vfio_group *group)
  1247. {
  1248. return iommu_group_id(group->iommu_group);
  1249. }
  1250. EXPORT_SYMBOL_GPL(vfio_external_user_iommu_id);
  1251. long vfio_external_check_extension(struct vfio_group *group, unsigned long arg)
  1252. {
  1253. return vfio_ioctl_check_extension(group->container, arg);
  1254. }
  1255. EXPORT_SYMBOL_GPL(vfio_external_check_extension);
  1256. /**
  1257. * Module/class support
  1258. */
  1259. static char *vfio_devnode(struct device *dev, umode_t *mode)
  1260. {
  1261. return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
  1262. }
  1263. static struct miscdevice vfio_dev = {
  1264. .minor = VFIO_MINOR,
  1265. .name = "vfio",
  1266. .fops = &vfio_fops,
  1267. .nodename = "vfio/vfio",
  1268. .mode = S_IRUGO | S_IWUGO,
  1269. };
  1270. static int __init vfio_init(void)
  1271. {
  1272. int ret;
  1273. idr_init(&vfio.group_idr);
  1274. mutex_init(&vfio.group_lock);
  1275. mutex_init(&vfio.iommu_drivers_lock);
  1276. INIT_LIST_HEAD(&vfio.group_list);
  1277. INIT_LIST_HEAD(&vfio.iommu_drivers_list);
  1278. init_waitqueue_head(&vfio.release_q);
  1279. ret = misc_register(&vfio_dev);
  1280. if (ret) {
  1281. pr_err("vfio: misc device register failed\n");
  1282. return ret;
  1283. }
  1284. /* /dev/vfio/$GROUP */
  1285. vfio.class = class_create(THIS_MODULE, "vfio");
  1286. if (IS_ERR(vfio.class)) {
  1287. ret = PTR_ERR(vfio.class);
  1288. goto err_class;
  1289. }
  1290. vfio.class->devnode = vfio_devnode;
  1291. ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK, "vfio");
  1292. if (ret)
  1293. goto err_alloc_chrdev;
  1294. cdev_init(&vfio.group_cdev, &vfio_group_fops);
  1295. ret = cdev_add(&vfio.group_cdev, vfio.group_devt, MINORMASK);
  1296. if (ret)
  1297. goto err_cdev_add;
  1298. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  1299. /*
  1300. * Attempt to load known iommu-drivers. This gives us a working
  1301. * environment without the user needing to explicitly load iommu
  1302. * drivers.
  1303. */
  1304. request_module_nowait("vfio_iommu_type1");
  1305. request_module_nowait("vfio_iommu_spapr_tce");
  1306. return 0;
  1307. err_cdev_add:
  1308. unregister_chrdev_region(vfio.group_devt, MINORMASK);
  1309. err_alloc_chrdev:
  1310. class_destroy(vfio.class);
  1311. vfio.class = NULL;
  1312. err_class:
  1313. misc_deregister(&vfio_dev);
  1314. return ret;
  1315. }
  1316. static void __exit vfio_cleanup(void)
  1317. {
  1318. WARN_ON(!list_empty(&vfio.group_list));
  1319. idr_destroy(&vfio.group_idr);
  1320. cdev_del(&vfio.group_cdev);
  1321. unregister_chrdev_region(vfio.group_devt, MINORMASK);
  1322. class_destroy(vfio.class);
  1323. vfio.class = NULL;
  1324. misc_deregister(&vfio_dev);
  1325. }
  1326. module_init(vfio_init);
  1327. module_exit(vfio_cleanup);
  1328. MODULE_VERSION(DRIVER_VERSION);
  1329. MODULE_LICENSE("GPL v2");
  1330. MODULE_AUTHOR(DRIVER_AUTHOR);
  1331. MODULE_DESCRIPTION(DRIVER_DESC);
  1332. MODULE_ALIAS_MISCDEV(VFIO_MINOR);
  1333. MODULE_ALIAS("devname:vfio/vfio");