vfio.c 46 KB

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