vfio.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  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. wait_queue_head_t container_q;
  82. bool noiommu;
  83. struct kvm *kvm;
  84. struct blocking_notifier_head notifier;
  85. };
  86. struct vfio_device {
  87. struct kref kref;
  88. struct device *dev;
  89. const struct vfio_device_ops *ops;
  90. struct vfio_group *group;
  91. struct list_head group_next;
  92. void *device_data;
  93. };
  94. #ifdef CONFIG_VFIO_NOIOMMU
  95. static bool noiommu __read_mostly;
  96. module_param_named(enable_unsafe_noiommu_mode,
  97. noiommu, bool, S_IRUGO | S_IWUSR);
  98. 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)");
  99. #endif
  100. /*
  101. * vfio_iommu_group_{get,put} are only intended for VFIO bus driver probe
  102. * and remove functions, any use cases other than acquiring the first
  103. * reference for the purpose of calling vfio_add_group_dev() or removing
  104. * that symmetric reference after vfio_del_group_dev() should use the raw
  105. * iommu_group_{get,put} functions. In particular, vfio_iommu_group_put()
  106. * removes the device from the dummy group and cannot be nested.
  107. */
  108. struct iommu_group *vfio_iommu_group_get(struct device *dev)
  109. {
  110. struct iommu_group *group;
  111. int __maybe_unused ret;
  112. group = iommu_group_get(dev);
  113. #ifdef CONFIG_VFIO_NOIOMMU
  114. /*
  115. * With noiommu enabled, an IOMMU group will be created for a device
  116. * that doesn't already have one and doesn't have an iommu_ops on their
  117. * bus. We set iommudata simply to be able to identify these groups
  118. * as special use and for reclamation later.
  119. */
  120. if (group || !noiommu || iommu_present(dev->bus))
  121. return group;
  122. group = iommu_group_alloc();
  123. if (IS_ERR(group))
  124. return NULL;
  125. iommu_group_set_name(group, "vfio-noiommu");
  126. iommu_group_set_iommudata(group, &noiommu, NULL);
  127. ret = iommu_group_add_device(group, dev);
  128. if (ret) {
  129. iommu_group_put(group);
  130. return NULL;
  131. }
  132. /*
  133. * Where to taint? At this point we've added an IOMMU group for a
  134. * device that is not backed by iommu_ops, therefore any iommu_
  135. * callback using iommu_ops can legitimately Oops. So, while we may
  136. * be about to give a DMA capable device to a user without IOMMU
  137. * protection, which is clearly taint-worthy, let's go ahead and do
  138. * it here.
  139. */
  140. add_taint(TAINT_USER, LOCKDEP_STILL_OK);
  141. dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n");
  142. #endif
  143. return group;
  144. }
  145. EXPORT_SYMBOL_GPL(vfio_iommu_group_get);
  146. void vfio_iommu_group_put(struct iommu_group *group, struct device *dev)
  147. {
  148. #ifdef CONFIG_VFIO_NOIOMMU
  149. if (iommu_group_get_iommudata(group) == &noiommu)
  150. iommu_group_remove_device(dev);
  151. #endif
  152. iommu_group_put(group);
  153. }
  154. EXPORT_SYMBOL_GPL(vfio_iommu_group_put);
  155. #ifdef CONFIG_VFIO_NOIOMMU
  156. static void *vfio_noiommu_open(unsigned long arg)
  157. {
  158. if (arg != VFIO_NOIOMMU_IOMMU)
  159. return ERR_PTR(-EINVAL);
  160. if (!capable(CAP_SYS_RAWIO))
  161. return ERR_PTR(-EPERM);
  162. return NULL;
  163. }
  164. static void vfio_noiommu_release(void *iommu_data)
  165. {
  166. }
  167. static long vfio_noiommu_ioctl(void *iommu_data,
  168. unsigned int cmd, unsigned long arg)
  169. {
  170. if (cmd == VFIO_CHECK_EXTENSION)
  171. return noiommu && (arg == VFIO_NOIOMMU_IOMMU) ? 1 : 0;
  172. return -ENOTTY;
  173. }
  174. static int vfio_noiommu_attach_group(void *iommu_data,
  175. struct iommu_group *iommu_group)
  176. {
  177. return iommu_group_get_iommudata(iommu_group) == &noiommu ? 0 : -EINVAL;
  178. }
  179. static void vfio_noiommu_detach_group(void *iommu_data,
  180. struct iommu_group *iommu_group)
  181. {
  182. }
  183. static const 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. #endif
  193. /**
  194. * IOMMU driver registration
  195. */
  196. int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
  197. {
  198. struct vfio_iommu_driver *driver, *tmp;
  199. driver = kzalloc(sizeof(*driver), GFP_KERNEL);
  200. if (!driver)
  201. return -ENOMEM;
  202. driver->ops = ops;
  203. mutex_lock(&vfio.iommu_drivers_lock);
  204. /* Check for duplicates */
  205. list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
  206. if (tmp->ops == ops) {
  207. mutex_unlock(&vfio.iommu_drivers_lock);
  208. kfree(driver);
  209. return -EINVAL;
  210. }
  211. }
  212. list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
  213. mutex_unlock(&vfio.iommu_drivers_lock);
  214. return 0;
  215. }
  216. EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
  217. void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
  218. {
  219. struct vfio_iommu_driver *driver;
  220. mutex_lock(&vfio.iommu_drivers_lock);
  221. list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
  222. if (driver->ops == ops) {
  223. list_del(&driver->vfio_next);
  224. mutex_unlock(&vfio.iommu_drivers_lock);
  225. kfree(driver);
  226. return;
  227. }
  228. }
  229. mutex_unlock(&vfio.iommu_drivers_lock);
  230. }
  231. EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
  232. /**
  233. * Group minor allocation/free - both called with vfio.group_lock held
  234. */
  235. static int vfio_alloc_group_minor(struct vfio_group *group)
  236. {
  237. return idr_alloc(&vfio.group_idr, group, 0, MINORMASK + 1, GFP_KERNEL);
  238. }
  239. static void vfio_free_group_minor(int minor)
  240. {
  241. idr_remove(&vfio.group_idr, minor);
  242. }
  243. static int vfio_iommu_group_notifier(struct notifier_block *nb,
  244. unsigned long action, void *data);
  245. static void vfio_group_get(struct vfio_group *group);
  246. /**
  247. * Container objects - containers are created when /dev/vfio/vfio is
  248. * opened, but their lifecycle extends until the last user is done, so
  249. * it's freed via kref. Must support container/group/device being
  250. * closed in any order.
  251. */
  252. static void vfio_container_get(struct vfio_container *container)
  253. {
  254. kref_get(&container->kref);
  255. }
  256. static void vfio_container_release(struct kref *kref)
  257. {
  258. struct vfio_container *container;
  259. container = container_of(kref, struct vfio_container, kref);
  260. kfree(container);
  261. }
  262. static void vfio_container_put(struct vfio_container *container)
  263. {
  264. kref_put(&container->kref, vfio_container_release);
  265. }
  266. static void vfio_group_unlock_and_free(struct vfio_group *group)
  267. {
  268. mutex_unlock(&vfio.group_lock);
  269. /*
  270. * Unregister outside of lock. A spurious callback is harmless now
  271. * that the group is no longer in vfio.group_list.
  272. */
  273. iommu_group_unregister_notifier(group->iommu_group, &group->nb);
  274. kfree(group);
  275. }
  276. /**
  277. * Group objects - create, release, get, put, search
  278. */
  279. static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
  280. {
  281. struct vfio_group *group, *tmp;
  282. struct device *dev;
  283. int ret, minor;
  284. group = kzalloc(sizeof(*group), GFP_KERNEL);
  285. if (!group)
  286. return ERR_PTR(-ENOMEM);
  287. kref_init(&group->kref);
  288. INIT_LIST_HEAD(&group->device_list);
  289. mutex_init(&group->device_lock);
  290. INIT_LIST_HEAD(&group->unbound_list);
  291. mutex_init(&group->unbound_lock);
  292. atomic_set(&group->container_users, 0);
  293. atomic_set(&group->opened, 0);
  294. init_waitqueue_head(&group->container_q);
  295. group->iommu_group = iommu_group;
  296. #ifdef CONFIG_VFIO_NOIOMMU
  297. group->noiommu = (iommu_group_get_iommudata(iommu_group) == &noiommu);
  298. #endif
  299. BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
  300. group->nb.notifier_call = vfio_iommu_group_notifier;
  301. /*
  302. * blocking notifiers acquire a rwsem around registering and hold
  303. * it around callback. Therefore, need to register outside of
  304. * vfio.group_lock to avoid A-B/B-A contention. Our callback won't
  305. * do anything unless it can find the group in vfio.group_list, so
  306. * no harm in registering early.
  307. */
  308. ret = iommu_group_register_notifier(iommu_group, &group->nb);
  309. if (ret) {
  310. kfree(group);
  311. return ERR_PTR(ret);
  312. }
  313. mutex_lock(&vfio.group_lock);
  314. /* Did we race creating this group? */
  315. list_for_each_entry(tmp, &vfio.group_list, vfio_next) {
  316. if (tmp->iommu_group == iommu_group) {
  317. vfio_group_get(tmp);
  318. vfio_group_unlock_and_free(group);
  319. return tmp;
  320. }
  321. }
  322. minor = vfio_alloc_group_minor(group);
  323. if (minor < 0) {
  324. vfio_group_unlock_and_free(group);
  325. return ERR_PTR(minor);
  326. }
  327. dev = device_create(vfio.class, NULL,
  328. MKDEV(MAJOR(vfio.group_devt), minor),
  329. group, "%s%d", group->noiommu ? "noiommu-" : "",
  330. iommu_group_id(iommu_group));
  331. if (IS_ERR(dev)) {
  332. vfio_free_group_minor(minor);
  333. vfio_group_unlock_and_free(group);
  334. return ERR_CAST(dev);
  335. }
  336. group->minor = minor;
  337. group->dev = dev;
  338. list_add(&group->vfio_next, &vfio.group_list);
  339. mutex_unlock(&vfio.group_lock);
  340. return group;
  341. }
  342. /* called with vfio.group_lock held */
  343. static void vfio_group_release(struct kref *kref)
  344. {
  345. struct vfio_group *group = container_of(kref, struct vfio_group, kref);
  346. struct vfio_unbound_dev *unbound, *tmp;
  347. struct iommu_group *iommu_group = group->iommu_group;
  348. WARN_ON(!list_empty(&group->device_list));
  349. WARN_ON(group->notifier.head);
  350. list_for_each_entry_safe(unbound, tmp,
  351. &group->unbound_list, unbound_next) {
  352. list_del(&unbound->unbound_next);
  353. kfree(unbound);
  354. }
  355. device_destroy(vfio.class, MKDEV(MAJOR(vfio.group_devt), group->minor));
  356. list_del(&group->vfio_next);
  357. vfio_free_group_minor(group->minor);
  358. vfio_group_unlock_and_free(group);
  359. iommu_group_put(iommu_group);
  360. }
  361. static void vfio_group_put(struct vfio_group *group)
  362. {
  363. kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock);
  364. }
  365. struct vfio_group_put_work {
  366. struct work_struct work;
  367. struct vfio_group *group;
  368. };
  369. static void vfio_group_put_bg(struct work_struct *work)
  370. {
  371. struct vfio_group_put_work *do_work;
  372. do_work = container_of(work, struct vfio_group_put_work, work);
  373. vfio_group_put(do_work->group);
  374. kfree(do_work);
  375. }
  376. static void vfio_group_schedule_put(struct vfio_group *group)
  377. {
  378. struct vfio_group_put_work *do_work;
  379. do_work = kmalloc(sizeof(*do_work), GFP_KERNEL);
  380. if (WARN_ON(!do_work))
  381. return;
  382. INIT_WORK(&do_work->work, vfio_group_put_bg);
  383. do_work->group = group;
  384. schedule_work(&do_work->work);
  385. }
  386. /* Assume group_lock or group reference is held */
  387. static void vfio_group_get(struct vfio_group *group)
  388. {
  389. kref_get(&group->kref);
  390. }
  391. /*
  392. * Not really a try as we will sleep for mutex, but we need to make
  393. * sure the group pointer is valid under lock and get a reference.
  394. */
  395. static struct vfio_group *vfio_group_try_get(struct vfio_group *group)
  396. {
  397. struct vfio_group *target = group;
  398. mutex_lock(&vfio.group_lock);
  399. list_for_each_entry(group, &vfio.group_list, vfio_next) {
  400. if (group == target) {
  401. vfio_group_get(group);
  402. mutex_unlock(&vfio.group_lock);
  403. return group;
  404. }
  405. }
  406. mutex_unlock(&vfio.group_lock);
  407. return NULL;
  408. }
  409. static
  410. struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group)
  411. {
  412. struct vfio_group *group;
  413. mutex_lock(&vfio.group_lock);
  414. list_for_each_entry(group, &vfio.group_list, vfio_next) {
  415. if (group->iommu_group == iommu_group) {
  416. vfio_group_get(group);
  417. mutex_unlock(&vfio.group_lock);
  418. return group;
  419. }
  420. }
  421. mutex_unlock(&vfio.group_lock);
  422. return NULL;
  423. }
  424. static struct vfio_group *vfio_group_get_from_minor(int minor)
  425. {
  426. struct vfio_group *group;
  427. mutex_lock(&vfio.group_lock);
  428. group = idr_find(&vfio.group_idr, minor);
  429. if (!group) {
  430. mutex_unlock(&vfio.group_lock);
  431. return NULL;
  432. }
  433. vfio_group_get(group);
  434. mutex_unlock(&vfio.group_lock);
  435. return group;
  436. }
  437. static struct vfio_group *vfio_group_get_from_dev(struct device *dev)
  438. {
  439. struct iommu_group *iommu_group;
  440. struct vfio_group *group;
  441. iommu_group = iommu_group_get(dev);
  442. if (!iommu_group)
  443. return NULL;
  444. group = vfio_group_get_from_iommu(iommu_group);
  445. iommu_group_put(iommu_group);
  446. return group;
  447. }
  448. /**
  449. * Device objects - create, release, get, put, search
  450. */
  451. static
  452. struct vfio_device *vfio_group_create_device(struct vfio_group *group,
  453. struct device *dev,
  454. const struct vfio_device_ops *ops,
  455. void *device_data)
  456. {
  457. struct vfio_device *device;
  458. device = kzalloc(sizeof(*device), GFP_KERNEL);
  459. if (!device)
  460. return ERR_PTR(-ENOMEM);
  461. kref_init(&device->kref);
  462. device->dev = dev;
  463. device->group = group;
  464. device->ops = ops;
  465. device->device_data = device_data;
  466. dev_set_drvdata(dev, device);
  467. /* No need to get group_lock, caller has group reference */
  468. vfio_group_get(group);
  469. mutex_lock(&group->device_lock);
  470. list_add(&device->group_next, &group->device_list);
  471. mutex_unlock(&group->device_lock);
  472. return device;
  473. }
  474. static void vfio_device_release(struct kref *kref)
  475. {
  476. struct vfio_device *device = container_of(kref,
  477. struct vfio_device, kref);
  478. struct vfio_group *group = device->group;
  479. list_del(&device->group_next);
  480. mutex_unlock(&group->device_lock);
  481. dev_set_drvdata(device->dev, NULL);
  482. kfree(device);
  483. /* vfio_del_group_dev may be waiting for this device */
  484. wake_up(&vfio.release_q);
  485. }
  486. /* Device reference always implies a group reference */
  487. void vfio_device_put(struct vfio_device *device)
  488. {
  489. struct vfio_group *group = device->group;
  490. kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock);
  491. vfio_group_put(group);
  492. }
  493. EXPORT_SYMBOL_GPL(vfio_device_put);
  494. static void vfio_device_get(struct vfio_device *device)
  495. {
  496. vfio_group_get(device->group);
  497. kref_get(&device->kref);
  498. }
  499. static struct vfio_device *vfio_group_get_device(struct vfio_group *group,
  500. struct device *dev)
  501. {
  502. struct vfio_device *device;
  503. mutex_lock(&group->device_lock);
  504. list_for_each_entry(device, &group->device_list, group_next) {
  505. if (device->dev == dev) {
  506. vfio_device_get(device);
  507. mutex_unlock(&group->device_lock);
  508. return device;
  509. }
  510. }
  511. mutex_unlock(&group->device_lock);
  512. return NULL;
  513. }
  514. /*
  515. * Some drivers, like pci-stub, are only used to prevent other drivers from
  516. * claiming a device and are therefore perfectly legitimate for a user owned
  517. * group. The pci-stub driver has no dependencies on DMA or the IOVA mapping
  518. * of the device, but it does prevent the user from having direct access to
  519. * the device, which is useful in some circumstances.
  520. *
  521. * We also assume that we can include PCI interconnect devices, ie. bridges.
  522. * IOMMU grouping on PCI necessitates that if we lack isolation on a bridge
  523. * then all of the downstream devices will be part of the same IOMMU group as
  524. * the bridge. Thus, if placing the bridge into the user owned IOVA space
  525. * breaks anything, it only does so for user owned devices downstream. Note
  526. * that error notification via MSI can be affected for platforms that handle
  527. * MSI within the same IOVA space as DMA.
  528. */
  529. static const char * const vfio_driver_whitelist[] = { "pci-stub" };
  530. static bool vfio_dev_whitelisted(struct device *dev, struct device_driver *drv)
  531. {
  532. if (dev_is_pci(dev)) {
  533. struct pci_dev *pdev = to_pci_dev(dev);
  534. if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  535. return true;
  536. }
  537. return match_string(vfio_driver_whitelist,
  538. ARRAY_SIZE(vfio_driver_whitelist),
  539. drv->name) >= 0;
  540. }
  541. /*
  542. * A vfio group is viable for use by userspace if all devices are in
  543. * one of the following states:
  544. * - driver-less
  545. * - bound to a vfio driver
  546. * - bound to a whitelisted driver
  547. * - a PCI interconnect device
  548. *
  549. * We use two methods to determine whether a device is bound to a vfio
  550. * driver. The first is to test whether the device exists in the vfio
  551. * group. The second is to test if the device exists on the group
  552. * unbound_list, indicating it's in the middle of transitioning from
  553. * a vfio driver to driver-less.
  554. */
  555. static int vfio_dev_viable(struct device *dev, void *data)
  556. {
  557. struct vfio_group *group = data;
  558. struct vfio_device *device;
  559. struct device_driver *drv = READ_ONCE(dev->driver);
  560. struct vfio_unbound_dev *unbound;
  561. int ret = -EINVAL;
  562. mutex_lock(&group->unbound_lock);
  563. list_for_each_entry(unbound, &group->unbound_list, unbound_next) {
  564. if (dev == unbound->dev) {
  565. ret = 0;
  566. break;
  567. }
  568. }
  569. mutex_unlock(&group->unbound_lock);
  570. if (!ret || !drv || vfio_dev_whitelisted(dev, drv))
  571. return 0;
  572. device = vfio_group_get_device(group, dev);
  573. if (device) {
  574. vfio_device_put(device);
  575. return 0;
  576. }
  577. return ret;
  578. }
  579. /**
  580. * Async device support
  581. */
  582. static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev)
  583. {
  584. struct vfio_device *device;
  585. /* Do we already know about it? We shouldn't */
  586. device = vfio_group_get_device(group, dev);
  587. if (WARN_ON_ONCE(device)) {
  588. vfio_device_put(device);
  589. return 0;
  590. }
  591. /* Nothing to do for idle groups */
  592. if (!atomic_read(&group->container_users))
  593. return 0;
  594. /* TODO Prevent device auto probing */
  595. WARN(1, "Device %s added to live group %d!\n", dev_name(dev),
  596. iommu_group_id(group->iommu_group));
  597. return 0;
  598. }
  599. static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev)
  600. {
  601. /* We don't care what happens when the group isn't in use */
  602. if (!atomic_read(&group->container_users))
  603. return 0;
  604. return vfio_dev_viable(dev, group);
  605. }
  606. static int vfio_iommu_group_notifier(struct notifier_block *nb,
  607. unsigned long action, void *data)
  608. {
  609. struct vfio_group *group = container_of(nb, struct vfio_group, nb);
  610. struct device *dev = data;
  611. struct vfio_unbound_dev *unbound;
  612. /*
  613. * Need to go through a group_lock lookup to get a reference or we
  614. * risk racing a group being removed. Ignore spurious notifies.
  615. */
  616. group = vfio_group_try_get(group);
  617. if (!group)
  618. return NOTIFY_OK;
  619. switch (action) {
  620. case IOMMU_GROUP_NOTIFY_ADD_DEVICE:
  621. vfio_group_nb_add_dev(group, dev);
  622. break;
  623. case IOMMU_GROUP_NOTIFY_DEL_DEVICE:
  624. /*
  625. * Nothing to do here. If the device is in use, then the
  626. * vfio sub-driver should block the remove callback until
  627. * it is unused. If the device is unused or attached to a
  628. * stub driver, then it should be released and we don't
  629. * care that it will be going away.
  630. */
  631. break;
  632. case IOMMU_GROUP_NOTIFY_BIND_DRIVER:
  633. pr_debug("%s: Device %s, group %d binding to driver\n",
  634. __func__, dev_name(dev),
  635. iommu_group_id(group->iommu_group));
  636. break;
  637. case IOMMU_GROUP_NOTIFY_BOUND_DRIVER:
  638. pr_debug("%s: Device %s, group %d bound to driver %s\n",
  639. __func__, dev_name(dev),
  640. iommu_group_id(group->iommu_group), dev->driver->name);
  641. BUG_ON(vfio_group_nb_verify(group, dev));
  642. break;
  643. case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER:
  644. pr_debug("%s: Device %s, group %d unbinding from driver %s\n",
  645. __func__, dev_name(dev),
  646. iommu_group_id(group->iommu_group), dev->driver->name);
  647. break;
  648. case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER:
  649. pr_debug("%s: Device %s, group %d unbound from driver\n",
  650. __func__, dev_name(dev),
  651. iommu_group_id(group->iommu_group));
  652. /*
  653. * XXX An unbound device in a live group is ok, but we'd
  654. * really like to avoid the above BUG_ON by preventing other
  655. * drivers from binding to it. Once that occurs, we have to
  656. * stop the system to maintain isolation. At a minimum, we'd
  657. * want a toggle to disable driver auto probe for this device.
  658. */
  659. mutex_lock(&group->unbound_lock);
  660. list_for_each_entry(unbound,
  661. &group->unbound_list, unbound_next) {
  662. if (dev == unbound->dev) {
  663. list_del(&unbound->unbound_next);
  664. kfree(unbound);
  665. break;
  666. }
  667. }
  668. mutex_unlock(&group->unbound_lock);
  669. break;
  670. }
  671. /*
  672. * If we're the last reference to the group, the group will be
  673. * released, which includes unregistering the iommu group notifier.
  674. * We hold a read-lock on that notifier list, unregistering needs
  675. * a write-lock... deadlock. Release our reference asynchronously
  676. * to avoid that situation.
  677. */
  678. vfio_group_schedule_put(group);
  679. return NOTIFY_OK;
  680. }
  681. /**
  682. * VFIO driver API
  683. */
  684. int vfio_add_group_dev(struct device *dev,
  685. const struct vfio_device_ops *ops, void *device_data)
  686. {
  687. struct iommu_group *iommu_group;
  688. struct vfio_group *group;
  689. struct vfio_device *device;
  690. iommu_group = iommu_group_get(dev);
  691. if (!iommu_group)
  692. return -EINVAL;
  693. group = vfio_group_get_from_iommu(iommu_group);
  694. if (!group) {
  695. group = vfio_create_group(iommu_group);
  696. if (IS_ERR(group)) {
  697. iommu_group_put(iommu_group);
  698. return PTR_ERR(group);
  699. }
  700. } else {
  701. /*
  702. * A found vfio_group already holds a reference to the
  703. * iommu_group. A created vfio_group keeps the reference.
  704. */
  705. iommu_group_put(iommu_group);
  706. }
  707. device = vfio_group_get_device(group, dev);
  708. if (device) {
  709. WARN(1, "Device %s already exists on group %d\n",
  710. dev_name(dev), iommu_group_id(iommu_group));
  711. vfio_device_put(device);
  712. vfio_group_put(group);
  713. return -EBUSY;
  714. }
  715. device = vfio_group_create_device(group, dev, ops, device_data);
  716. if (IS_ERR(device)) {
  717. vfio_group_put(group);
  718. return PTR_ERR(device);
  719. }
  720. /*
  721. * Drop all but the vfio_device reference. The vfio_device holds
  722. * a reference to the vfio_group, which holds a reference to the
  723. * iommu_group.
  724. */
  725. vfio_group_put(group);
  726. return 0;
  727. }
  728. EXPORT_SYMBOL_GPL(vfio_add_group_dev);
  729. /**
  730. * Get a reference to the vfio_device for a device. Even if the
  731. * caller thinks they own the device, they could be racing with a
  732. * release call path, so we can't trust drvdata for the shortcut.
  733. * Go the long way around, from the iommu_group to the vfio_group
  734. * to the vfio_device.
  735. */
  736. struct vfio_device *vfio_device_get_from_dev(struct device *dev)
  737. {
  738. struct vfio_group *group;
  739. struct vfio_device *device;
  740. group = vfio_group_get_from_dev(dev);
  741. if (!group)
  742. return NULL;
  743. device = vfio_group_get_device(group, dev);
  744. vfio_group_put(group);
  745. return device;
  746. }
  747. EXPORT_SYMBOL_GPL(vfio_device_get_from_dev);
  748. static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
  749. char *buf)
  750. {
  751. struct vfio_device *it, *device = NULL;
  752. mutex_lock(&group->device_lock);
  753. list_for_each_entry(it, &group->device_list, group_next) {
  754. if (!strcmp(dev_name(it->dev), buf)) {
  755. device = it;
  756. vfio_device_get(device);
  757. break;
  758. }
  759. }
  760. mutex_unlock(&group->device_lock);
  761. return device;
  762. }
  763. /*
  764. * Caller must hold a reference to the vfio_device
  765. */
  766. void *vfio_device_data(struct vfio_device *device)
  767. {
  768. return device->device_data;
  769. }
  770. EXPORT_SYMBOL_GPL(vfio_device_data);
  771. /* Given a referenced group, check if it contains the device */
  772. static bool vfio_dev_present(struct vfio_group *group, struct device *dev)
  773. {
  774. struct vfio_device *device;
  775. device = vfio_group_get_device(group, dev);
  776. if (!device)
  777. return false;
  778. vfio_device_put(device);
  779. return true;
  780. }
  781. /*
  782. * Decrement the device reference count and wait for the device to be
  783. * removed. Open file descriptors for the device... */
  784. void *vfio_del_group_dev(struct device *dev)
  785. {
  786. struct vfio_device *device = dev_get_drvdata(dev);
  787. struct vfio_group *group = device->group;
  788. void *device_data = device->device_data;
  789. struct vfio_unbound_dev *unbound;
  790. unsigned int i = 0;
  791. long ret;
  792. bool interrupted = false;
  793. /*
  794. * The group exists so long as we have a device reference. Get
  795. * a group reference and use it to scan for the device going away.
  796. */
  797. vfio_group_get(group);
  798. /*
  799. * When the device is removed from the group, the group suddenly
  800. * becomes non-viable; the device has a driver (until the unbind
  801. * completes), but it's not present in the group. This is bad news
  802. * for any external users that need to re-acquire a group reference
  803. * in order to match and release their existing reference. To
  804. * solve this, we track such devices on the unbound_list to bridge
  805. * the gap until they're fully unbound.
  806. */
  807. unbound = kzalloc(sizeof(*unbound), GFP_KERNEL);
  808. if (unbound) {
  809. unbound->dev = dev;
  810. mutex_lock(&group->unbound_lock);
  811. list_add(&unbound->unbound_next, &group->unbound_list);
  812. mutex_unlock(&group->unbound_lock);
  813. }
  814. WARN_ON(!unbound);
  815. vfio_device_put(device);
  816. /*
  817. * If the device is still present in the group after the above
  818. * 'put', then it is in use and we need to request it from the
  819. * bus driver. The driver may in turn need to request the
  820. * device from the user. We send the request on an arbitrary
  821. * interval with counter to allow the driver to take escalating
  822. * measures to release the device if it has the ability to do so.
  823. */
  824. do {
  825. device = vfio_group_get_device(group, dev);
  826. if (!device)
  827. break;
  828. if (device->ops->request)
  829. device->ops->request(device_data, i++);
  830. vfio_device_put(device);
  831. if (interrupted) {
  832. ret = wait_event_timeout(vfio.release_q,
  833. !vfio_dev_present(group, dev), HZ * 10);
  834. } else {
  835. ret = wait_event_interruptible_timeout(vfio.release_q,
  836. !vfio_dev_present(group, dev), HZ * 10);
  837. if (ret == -ERESTARTSYS) {
  838. interrupted = true;
  839. dev_warn(dev,
  840. "Device is currently in use, task"
  841. " \"%s\" (%d) "
  842. "blocked until device is released",
  843. current->comm, task_pid_nr(current));
  844. }
  845. }
  846. } while (ret <= 0);
  847. /*
  848. * In order to support multiple devices per group, devices can be
  849. * plucked from the group while other devices in the group are still
  850. * in use. The container persists with this group and those remaining
  851. * devices still attached. If the user creates an isolation violation
  852. * by binding this device to another driver while the group is still in
  853. * use, that's their fault. However, in the case of removing the last,
  854. * or potentially the only, device in the group there can be no other
  855. * in-use devices in the group. The user has done their due diligence
  856. * and we should lay no claims to those devices. In order to do that,
  857. * we need to make sure the group is detached from the container.
  858. * Without this stall, we're potentially racing with a user process
  859. * that may attempt to immediately bind this device to another driver.
  860. */
  861. if (list_empty(&group->device_list))
  862. wait_event(group->container_q, !group->container);
  863. vfio_group_put(group);
  864. return device_data;
  865. }
  866. EXPORT_SYMBOL_GPL(vfio_del_group_dev);
  867. /**
  868. * VFIO base fd, /dev/vfio/vfio
  869. */
  870. static long vfio_ioctl_check_extension(struct vfio_container *container,
  871. unsigned long arg)
  872. {
  873. struct vfio_iommu_driver *driver;
  874. long ret = 0;
  875. down_read(&container->group_lock);
  876. driver = container->iommu_driver;
  877. switch (arg) {
  878. /* No base extensions yet */
  879. default:
  880. /*
  881. * If no driver is set, poll all registered drivers for
  882. * extensions and return the first positive result. If
  883. * a driver is already set, further queries will be passed
  884. * only to that driver.
  885. */
  886. if (!driver) {
  887. mutex_lock(&vfio.iommu_drivers_lock);
  888. list_for_each_entry(driver, &vfio.iommu_drivers_list,
  889. vfio_next) {
  890. #ifdef CONFIG_VFIO_NOIOMMU
  891. if (!list_empty(&container->group_list) &&
  892. (container->noiommu !=
  893. (driver->ops == &vfio_noiommu_ops)))
  894. continue;
  895. #endif
  896. if (!try_module_get(driver->ops->owner))
  897. continue;
  898. ret = driver->ops->ioctl(NULL,
  899. VFIO_CHECK_EXTENSION,
  900. arg);
  901. module_put(driver->ops->owner);
  902. if (ret > 0)
  903. break;
  904. }
  905. mutex_unlock(&vfio.iommu_drivers_lock);
  906. } else
  907. ret = driver->ops->ioctl(container->iommu_data,
  908. VFIO_CHECK_EXTENSION, arg);
  909. }
  910. up_read(&container->group_lock);
  911. return ret;
  912. }
  913. /* hold write lock on container->group_lock */
  914. static int __vfio_container_attach_groups(struct vfio_container *container,
  915. struct vfio_iommu_driver *driver,
  916. void *data)
  917. {
  918. struct vfio_group *group;
  919. int ret = -ENODEV;
  920. list_for_each_entry(group, &container->group_list, container_next) {
  921. ret = driver->ops->attach_group(data, group->iommu_group);
  922. if (ret)
  923. goto unwind;
  924. }
  925. return ret;
  926. unwind:
  927. list_for_each_entry_continue_reverse(group, &container->group_list,
  928. container_next) {
  929. driver->ops->detach_group(data, group->iommu_group);
  930. }
  931. return ret;
  932. }
  933. static long vfio_ioctl_set_iommu(struct vfio_container *container,
  934. unsigned long arg)
  935. {
  936. struct vfio_iommu_driver *driver;
  937. long ret = -ENODEV;
  938. down_write(&container->group_lock);
  939. /*
  940. * The container is designed to be an unprivileged interface while
  941. * the group can be assigned to specific users. Therefore, only by
  942. * adding a group to a container does the user get the privilege of
  943. * enabling the iommu, which may allocate finite resources. There
  944. * is no unset_iommu, but by removing all the groups from a container,
  945. * the container is deprivileged and returns to an unset state.
  946. */
  947. if (list_empty(&container->group_list) || container->iommu_driver) {
  948. up_write(&container->group_lock);
  949. return -EINVAL;
  950. }
  951. mutex_lock(&vfio.iommu_drivers_lock);
  952. list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
  953. void *data;
  954. #ifdef CONFIG_VFIO_NOIOMMU
  955. /*
  956. * Only noiommu containers can use vfio-noiommu and noiommu
  957. * containers can only use vfio-noiommu.
  958. */
  959. if (container->noiommu != (driver->ops == &vfio_noiommu_ops))
  960. continue;
  961. #endif
  962. if (!try_module_get(driver->ops->owner))
  963. continue;
  964. /*
  965. * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
  966. * so test which iommu driver reported support for this
  967. * extension and call open on them. We also pass them the
  968. * magic, allowing a single driver to support multiple
  969. * interfaces if they'd like.
  970. */
  971. if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
  972. module_put(driver->ops->owner);
  973. continue;
  974. }
  975. data = driver->ops->open(arg);
  976. if (IS_ERR(data)) {
  977. ret = PTR_ERR(data);
  978. module_put(driver->ops->owner);
  979. continue;
  980. }
  981. ret = __vfio_container_attach_groups(container, driver, data);
  982. if (ret) {
  983. driver->ops->release(data);
  984. module_put(driver->ops->owner);
  985. continue;
  986. }
  987. container->iommu_driver = driver;
  988. container->iommu_data = data;
  989. break;
  990. }
  991. mutex_unlock(&vfio.iommu_drivers_lock);
  992. up_write(&container->group_lock);
  993. return ret;
  994. }
  995. static long vfio_fops_unl_ioctl(struct file *filep,
  996. unsigned int cmd, unsigned long arg)
  997. {
  998. struct vfio_container *container = filep->private_data;
  999. struct vfio_iommu_driver *driver;
  1000. void *data;
  1001. long ret = -EINVAL;
  1002. if (!container)
  1003. return ret;
  1004. switch (cmd) {
  1005. case VFIO_GET_API_VERSION:
  1006. ret = VFIO_API_VERSION;
  1007. break;
  1008. case VFIO_CHECK_EXTENSION:
  1009. ret = vfio_ioctl_check_extension(container, arg);
  1010. break;
  1011. case VFIO_SET_IOMMU:
  1012. ret = vfio_ioctl_set_iommu(container, arg);
  1013. break;
  1014. default:
  1015. driver = container->iommu_driver;
  1016. data = container->iommu_data;
  1017. if (driver) /* passthrough all unrecognized ioctls */
  1018. ret = driver->ops->ioctl(data, cmd, arg);
  1019. }
  1020. return ret;
  1021. }
  1022. #ifdef CONFIG_COMPAT
  1023. static long vfio_fops_compat_ioctl(struct file *filep,
  1024. unsigned int cmd, unsigned long arg)
  1025. {
  1026. arg = (unsigned long)compat_ptr(arg);
  1027. return vfio_fops_unl_ioctl(filep, cmd, arg);
  1028. }
  1029. #endif /* CONFIG_COMPAT */
  1030. static int vfio_fops_open(struct inode *inode, struct file *filep)
  1031. {
  1032. struct vfio_container *container;
  1033. container = kzalloc(sizeof(*container), GFP_KERNEL);
  1034. if (!container)
  1035. return -ENOMEM;
  1036. INIT_LIST_HEAD(&container->group_list);
  1037. init_rwsem(&container->group_lock);
  1038. kref_init(&container->kref);
  1039. filep->private_data = container;
  1040. return 0;
  1041. }
  1042. static int vfio_fops_release(struct inode *inode, struct file *filep)
  1043. {
  1044. struct vfio_container *container = filep->private_data;
  1045. filep->private_data = NULL;
  1046. vfio_container_put(container);
  1047. return 0;
  1048. }
  1049. /*
  1050. * Once an iommu driver is set, we optionally pass read/write/mmap
  1051. * on to the driver, allowing management interfaces beyond ioctl.
  1052. */
  1053. static ssize_t vfio_fops_read(struct file *filep, char __user *buf,
  1054. size_t count, loff_t *ppos)
  1055. {
  1056. struct vfio_container *container = filep->private_data;
  1057. struct vfio_iommu_driver *driver;
  1058. ssize_t ret = -EINVAL;
  1059. driver = container->iommu_driver;
  1060. if (likely(driver && driver->ops->read))
  1061. ret = driver->ops->read(container->iommu_data,
  1062. buf, count, ppos);
  1063. return ret;
  1064. }
  1065. static ssize_t vfio_fops_write(struct file *filep, const char __user *buf,
  1066. size_t count, loff_t *ppos)
  1067. {
  1068. struct vfio_container *container = filep->private_data;
  1069. struct vfio_iommu_driver *driver;
  1070. ssize_t ret = -EINVAL;
  1071. driver = container->iommu_driver;
  1072. if (likely(driver && driver->ops->write))
  1073. ret = driver->ops->write(container->iommu_data,
  1074. buf, count, ppos);
  1075. return ret;
  1076. }
  1077. static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  1078. {
  1079. struct vfio_container *container = filep->private_data;
  1080. struct vfio_iommu_driver *driver;
  1081. int ret = -EINVAL;
  1082. driver = container->iommu_driver;
  1083. if (likely(driver && driver->ops->mmap))
  1084. ret = driver->ops->mmap(container->iommu_data, vma);
  1085. return ret;
  1086. }
  1087. static const struct file_operations vfio_fops = {
  1088. .owner = THIS_MODULE,
  1089. .open = vfio_fops_open,
  1090. .release = vfio_fops_release,
  1091. .read = vfio_fops_read,
  1092. .write = vfio_fops_write,
  1093. .unlocked_ioctl = vfio_fops_unl_ioctl,
  1094. #ifdef CONFIG_COMPAT
  1095. .compat_ioctl = vfio_fops_compat_ioctl,
  1096. #endif
  1097. .mmap = vfio_fops_mmap,
  1098. };
  1099. /**
  1100. * VFIO Group fd, /dev/vfio/$GROUP
  1101. */
  1102. static void __vfio_group_unset_container(struct vfio_group *group)
  1103. {
  1104. struct vfio_container *container = group->container;
  1105. struct vfio_iommu_driver *driver;
  1106. down_write(&container->group_lock);
  1107. driver = container->iommu_driver;
  1108. if (driver)
  1109. driver->ops->detach_group(container->iommu_data,
  1110. group->iommu_group);
  1111. group->container = NULL;
  1112. wake_up(&group->container_q);
  1113. list_del(&group->container_next);
  1114. /* Detaching the last group deprivileges a container, remove iommu */
  1115. if (driver && list_empty(&container->group_list)) {
  1116. driver->ops->release(container->iommu_data);
  1117. module_put(driver->ops->owner);
  1118. container->iommu_driver = NULL;
  1119. container->iommu_data = NULL;
  1120. }
  1121. up_write(&container->group_lock);
  1122. vfio_container_put(container);
  1123. }
  1124. /*
  1125. * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
  1126. * if there was no container to unset. Since the ioctl is called on
  1127. * the group, we know that still exists, therefore the only valid
  1128. * transition here is 1->0.
  1129. */
  1130. static int vfio_group_unset_container(struct vfio_group *group)
  1131. {
  1132. int users = atomic_cmpxchg(&group->container_users, 1, 0);
  1133. if (!users)
  1134. return -EINVAL;
  1135. if (users != 1)
  1136. return -EBUSY;
  1137. __vfio_group_unset_container(group);
  1138. return 0;
  1139. }
  1140. /*
  1141. * When removing container users, anything that removes the last user
  1142. * implicitly removes the group from the container. That is, if the
  1143. * group file descriptor is closed, as well as any device file descriptors,
  1144. * the group is free.
  1145. */
  1146. static void vfio_group_try_dissolve_container(struct vfio_group *group)
  1147. {
  1148. if (0 == atomic_dec_if_positive(&group->container_users))
  1149. __vfio_group_unset_container(group);
  1150. }
  1151. static int vfio_group_set_container(struct vfio_group *group, int container_fd)
  1152. {
  1153. struct fd f;
  1154. struct vfio_container *container;
  1155. struct vfio_iommu_driver *driver;
  1156. int ret = 0;
  1157. if (atomic_read(&group->container_users))
  1158. return -EINVAL;
  1159. if (group->noiommu && !capable(CAP_SYS_RAWIO))
  1160. return -EPERM;
  1161. f = fdget(container_fd);
  1162. if (!f.file)
  1163. return -EBADF;
  1164. /* Sanity check, is this really our fd? */
  1165. if (f.file->f_op != &vfio_fops) {
  1166. fdput(f);
  1167. return -EINVAL;
  1168. }
  1169. container = f.file->private_data;
  1170. WARN_ON(!container); /* fget ensures we don't race vfio_release */
  1171. down_write(&container->group_lock);
  1172. /* Real groups and fake groups cannot mix */
  1173. if (!list_empty(&container->group_list) &&
  1174. container->noiommu != group->noiommu) {
  1175. ret = -EPERM;
  1176. goto unlock_out;
  1177. }
  1178. driver = container->iommu_driver;
  1179. if (driver) {
  1180. ret = driver->ops->attach_group(container->iommu_data,
  1181. group->iommu_group);
  1182. if (ret)
  1183. goto unlock_out;
  1184. }
  1185. group->container = container;
  1186. container->noiommu = group->noiommu;
  1187. list_add(&group->container_next, &container->group_list);
  1188. /* Get a reference on the container and mark a user within the group */
  1189. vfio_container_get(container);
  1190. atomic_inc(&group->container_users);
  1191. unlock_out:
  1192. up_write(&container->group_lock);
  1193. fdput(f);
  1194. return ret;
  1195. }
  1196. static bool vfio_group_viable(struct vfio_group *group)
  1197. {
  1198. return (iommu_group_for_each_dev(group->iommu_group,
  1199. group, vfio_dev_viable) == 0);
  1200. }
  1201. static int vfio_group_add_container_user(struct vfio_group *group)
  1202. {
  1203. if (!atomic_inc_not_zero(&group->container_users))
  1204. return -EINVAL;
  1205. if (group->noiommu) {
  1206. atomic_dec(&group->container_users);
  1207. return -EPERM;
  1208. }
  1209. if (!group->container->iommu_driver || !vfio_group_viable(group)) {
  1210. atomic_dec(&group->container_users);
  1211. return -EINVAL;
  1212. }
  1213. return 0;
  1214. }
  1215. static const struct file_operations vfio_device_fops;
  1216. static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
  1217. {
  1218. struct vfio_device *device;
  1219. struct file *filep;
  1220. int ret;
  1221. if (0 == atomic_read(&group->container_users) ||
  1222. !group->container->iommu_driver || !vfio_group_viable(group))
  1223. return -EINVAL;
  1224. if (group->noiommu && !capable(CAP_SYS_RAWIO))
  1225. return -EPERM;
  1226. device = vfio_device_get_from_name(group, buf);
  1227. if (!device)
  1228. return -ENODEV;
  1229. ret = device->ops->open(device->device_data);
  1230. if (ret) {
  1231. vfio_device_put(device);
  1232. return ret;
  1233. }
  1234. /*
  1235. * We can't use anon_inode_getfd() because we need to modify
  1236. * the f_mode flags directly to allow more than just ioctls
  1237. */
  1238. ret = get_unused_fd_flags(O_CLOEXEC);
  1239. if (ret < 0) {
  1240. device->ops->release(device->device_data);
  1241. vfio_device_put(device);
  1242. return ret;
  1243. }
  1244. filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops,
  1245. device, O_RDWR);
  1246. if (IS_ERR(filep)) {
  1247. put_unused_fd(ret);
  1248. ret = PTR_ERR(filep);
  1249. device->ops->release(device->device_data);
  1250. vfio_device_put(device);
  1251. return ret;
  1252. }
  1253. /*
  1254. * TODO: add an anon_inode interface to do this.
  1255. * Appears to be missing by lack of need rather than
  1256. * explicitly prevented. Now there's need.
  1257. */
  1258. filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1259. atomic_inc(&group->container_users);
  1260. fd_install(ret, filep);
  1261. if (group->noiommu)
  1262. dev_warn(device->dev, "vfio-noiommu device opened by user "
  1263. "(%s:%d)\n", current->comm, task_pid_nr(current));
  1264. return ret;
  1265. }
  1266. static long vfio_group_fops_unl_ioctl(struct file *filep,
  1267. unsigned int cmd, unsigned long arg)
  1268. {
  1269. struct vfio_group *group = filep->private_data;
  1270. long ret = -ENOTTY;
  1271. switch (cmd) {
  1272. case VFIO_GROUP_GET_STATUS:
  1273. {
  1274. struct vfio_group_status status;
  1275. unsigned long minsz;
  1276. minsz = offsetofend(struct vfio_group_status, flags);
  1277. if (copy_from_user(&status, (void __user *)arg, minsz))
  1278. return -EFAULT;
  1279. if (status.argsz < minsz)
  1280. return -EINVAL;
  1281. status.flags = 0;
  1282. if (vfio_group_viable(group))
  1283. status.flags |= VFIO_GROUP_FLAGS_VIABLE;
  1284. if (group->container)
  1285. status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET;
  1286. if (copy_to_user((void __user *)arg, &status, minsz))
  1287. return -EFAULT;
  1288. ret = 0;
  1289. break;
  1290. }
  1291. case VFIO_GROUP_SET_CONTAINER:
  1292. {
  1293. int fd;
  1294. if (get_user(fd, (int __user *)arg))
  1295. return -EFAULT;
  1296. if (fd < 0)
  1297. return -EINVAL;
  1298. ret = vfio_group_set_container(group, fd);
  1299. break;
  1300. }
  1301. case VFIO_GROUP_UNSET_CONTAINER:
  1302. ret = vfio_group_unset_container(group);
  1303. break;
  1304. case VFIO_GROUP_GET_DEVICE_FD:
  1305. {
  1306. char *buf;
  1307. buf = strndup_user((const char __user *)arg, PAGE_SIZE);
  1308. if (IS_ERR(buf))
  1309. return PTR_ERR(buf);
  1310. ret = vfio_group_get_device_fd(group, buf);
  1311. kfree(buf);
  1312. break;
  1313. }
  1314. }
  1315. return ret;
  1316. }
  1317. #ifdef CONFIG_COMPAT
  1318. static long vfio_group_fops_compat_ioctl(struct file *filep,
  1319. unsigned int cmd, unsigned long arg)
  1320. {
  1321. arg = (unsigned long)compat_ptr(arg);
  1322. return vfio_group_fops_unl_ioctl(filep, cmd, arg);
  1323. }
  1324. #endif /* CONFIG_COMPAT */
  1325. static int vfio_group_fops_open(struct inode *inode, struct file *filep)
  1326. {
  1327. struct vfio_group *group;
  1328. int opened;
  1329. group = vfio_group_get_from_minor(iminor(inode));
  1330. if (!group)
  1331. return -ENODEV;
  1332. if (group->noiommu && !capable(CAP_SYS_RAWIO)) {
  1333. vfio_group_put(group);
  1334. return -EPERM;
  1335. }
  1336. /* Do we need multiple instances of the group open? Seems not. */
  1337. opened = atomic_cmpxchg(&group->opened, 0, 1);
  1338. if (opened) {
  1339. vfio_group_put(group);
  1340. return -EBUSY;
  1341. }
  1342. /* Is something still in use from a previous open? */
  1343. if (group->container) {
  1344. atomic_dec(&group->opened);
  1345. vfio_group_put(group);
  1346. return -EBUSY;
  1347. }
  1348. /* Warn if previous user didn't cleanup and re-init to drop them */
  1349. if (WARN_ON(group->notifier.head))
  1350. BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
  1351. filep->private_data = group;
  1352. return 0;
  1353. }
  1354. static int vfio_group_fops_release(struct inode *inode, struct file *filep)
  1355. {
  1356. struct vfio_group *group = filep->private_data;
  1357. filep->private_data = NULL;
  1358. vfio_group_try_dissolve_container(group);
  1359. atomic_dec(&group->opened);
  1360. vfio_group_put(group);
  1361. return 0;
  1362. }
  1363. static const struct file_operations vfio_group_fops = {
  1364. .owner = THIS_MODULE,
  1365. .unlocked_ioctl = vfio_group_fops_unl_ioctl,
  1366. #ifdef CONFIG_COMPAT
  1367. .compat_ioctl = vfio_group_fops_compat_ioctl,
  1368. #endif
  1369. .open = vfio_group_fops_open,
  1370. .release = vfio_group_fops_release,
  1371. };
  1372. /**
  1373. * VFIO Device fd
  1374. */
  1375. static int vfio_device_fops_release(struct inode *inode, struct file *filep)
  1376. {
  1377. struct vfio_device *device = filep->private_data;
  1378. device->ops->release(device->device_data);
  1379. vfio_group_try_dissolve_container(device->group);
  1380. vfio_device_put(device);
  1381. return 0;
  1382. }
  1383. static long vfio_device_fops_unl_ioctl(struct file *filep,
  1384. unsigned int cmd, unsigned long arg)
  1385. {
  1386. struct vfio_device *device = filep->private_data;
  1387. if (unlikely(!device->ops->ioctl))
  1388. return -EINVAL;
  1389. return device->ops->ioctl(device->device_data, cmd, arg);
  1390. }
  1391. static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf,
  1392. size_t count, loff_t *ppos)
  1393. {
  1394. struct vfio_device *device = filep->private_data;
  1395. if (unlikely(!device->ops->read))
  1396. return -EINVAL;
  1397. return device->ops->read(device->device_data, buf, count, ppos);
  1398. }
  1399. static ssize_t vfio_device_fops_write(struct file *filep,
  1400. const char __user *buf,
  1401. size_t count, loff_t *ppos)
  1402. {
  1403. struct vfio_device *device = filep->private_data;
  1404. if (unlikely(!device->ops->write))
  1405. return -EINVAL;
  1406. return device->ops->write(device->device_data, buf, count, ppos);
  1407. }
  1408. static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  1409. {
  1410. struct vfio_device *device = filep->private_data;
  1411. if (unlikely(!device->ops->mmap))
  1412. return -EINVAL;
  1413. return device->ops->mmap(device->device_data, vma);
  1414. }
  1415. #ifdef CONFIG_COMPAT
  1416. static long vfio_device_fops_compat_ioctl(struct file *filep,
  1417. unsigned int cmd, unsigned long arg)
  1418. {
  1419. arg = (unsigned long)compat_ptr(arg);
  1420. return vfio_device_fops_unl_ioctl(filep, cmd, arg);
  1421. }
  1422. #endif /* CONFIG_COMPAT */
  1423. static const struct file_operations vfio_device_fops = {
  1424. .owner = THIS_MODULE,
  1425. .release = vfio_device_fops_release,
  1426. .read = vfio_device_fops_read,
  1427. .write = vfio_device_fops_write,
  1428. .unlocked_ioctl = vfio_device_fops_unl_ioctl,
  1429. #ifdef CONFIG_COMPAT
  1430. .compat_ioctl = vfio_device_fops_compat_ioctl,
  1431. #endif
  1432. .mmap = vfio_device_fops_mmap,
  1433. };
  1434. /**
  1435. * External user API, exported by symbols to be linked dynamically.
  1436. *
  1437. * The protocol includes:
  1438. * 1. do normal VFIO init operation:
  1439. * - opening a new container;
  1440. * - attaching group(s) to it;
  1441. * - setting an IOMMU driver for a container.
  1442. * When IOMMU is set for a container, all groups in it are
  1443. * considered ready to use by an external user.
  1444. *
  1445. * 2. User space passes a group fd to an external user.
  1446. * The external user calls vfio_group_get_external_user()
  1447. * to verify that:
  1448. * - the group is initialized;
  1449. * - IOMMU is set for it.
  1450. * If both checks passed, vfio_group_get_external_user()
  1451. * increments the container user counter to prevent
  1452. * the VFIO group from disposal before KVM exits.
  1453. *
  1454. * 3. The external user calls vfio_external_user_iommu_id()
  1455. * to know an IOMMU ID.
  1456. *
  1457. * 4. When the external KVM finishes, it calls
  1458. * vfio_group_put_external_user() to release the VFIO group.
  1459. * This call decrements the container user counter.
  1460. */
  1461. struct vfio_group *vfio_group_get_external_user(struct file *filep)
  1462. {
  1463. struct vfio_group *group = filep->private_data;
  1464. int ret;
  1465. if (filep->f_op != &vfio_group_fops)
  1466. return ERR_PTR(-EINVAL);
  1467. ret = vfio_group_add_container_user(group);
  1468. if (ret)
  1469. return ERR_PTR(ret);
  1470. vfio_group_get(group);
  1471. return group;
  1472. }
  1473. EXPORT_SYMBOL_GPL(vfio_group_get_external_user);
  1474. void vfio_group_put_external_user(struct vfio_group *group)
  1475. {
  1476. vfio_group_try_dissolve_container(group);
  1477. vfio_group_put(group);
  1478. }
  1479. EXPORT_SYMBOL_GPL(vfio_group_put_external_user);
  1480. bool vfio_external_group_match_file(struct vfio_group *test_group,
  1481. struct file *filep)
  1482. {
  1483. struct vfio_group *group = filep->private_data;
  1484. return (filep->f_op == &vfio_group_fops) && (group == test_group);
  1485. }
  1486. EXPORT_SYMBOL_GPL(vfio_external_group_match_file);
  1487. int vfio_external_user_iommu_id(struct vfio_group *group)
  1488. {
  1489. return iommu_group_id(group->iommu_group);
  1490. }
  1491. EXPORT_SYMBOL_GPL(vfio_external_user_iommu_id);
  1492. long vfio_external_check_extension(struct vfio_group *group, unsigned long arg)
  1493. {
  1494. return vfio_ioctl_check_extension(group->container, arg);
  1495. }
  1496. EXPORT_SYMBOL_GPL(vfio_external_check_extension);
  1497. /**
  1498. * Sub-module support
  1499. */
  1500. /*
  1501. * Helper for managing a buffer of info chain capabilities, allocate or
  1502. * reallocate a buffer with additional @size, filling in @id and @version
  1503. * of the capability. A pointer to the new capability is returned.
  1504. *
  1505. * NB. The chain is based at the head of the buffer, so new entries are
  1506. * added to the tail, vfio_info_cap_shift() should be called to fixup the
  1507. * next offsets prior to copying to the user buffer.
  1508. */
  1509. struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
  1510. size_t size, u16 id, u16 version)
  1511. {
  1512. void *buf;
  1513. struct vfio_info_cap_header *header, *tmp;
  1514. buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL);
  1515. if (!buf) {
  1516. kfree(caps->buf);
  1517. caps->size = 0;
  1518. return ERR_PTR(-ENOMEM);
  1519. }
  1520. caps->buf = buf;
  1521. header = buf + caps->size;
  1522. /* Eventually copied to user buffer, zero */
  1523. memset(header, 0, size);
  1524. header->id = id;
  1525. header->version = version;
  1526. /* Add to the end of the capability chain */
  1527. for (tmp = buf; tmp->next; tmp = buf + tmp->next)
  1528. ; /* nothing */
  1529. tmp->next = caps->size;
  1530. caps->size += size;
  1531. return header;
  1532. }
  1533. EXPORT_SYMBOL_GPL(vfio_info_cap_add);
  1534. void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset)
  1535. {
  1536. struct vfio_info_cap_header *tmp;
  1537. void *buf = (void *)caps->buf;
  1538. for (tmp = buf; tmp->next; tmp = buf + tmp->next - offset)
  1539. tmp->next += offset;
  1540. }
  1541. EXPORT_SYMBOL(vfio_info_cap_shift);
  1542. int vfio_info_add_capability(struct vfio_info_cap *caps,
  1543. struct vfio_info_cap_header *cap, size_t size)
  1544. {
  1545. struct vfio_info_cap_header *header;
  1546. header = vfio_info_cap_add(caps, size, cap->id, cap->version);
  1547. if (IS_ERR(header))
  1548. return PTR_ERR(header);
  1549. memcpy(header + 1, cap + 1, size - sizeof(*header));
  1550. return 0;
  1551. }
  1552. EXPORT_SYMBOL(vfio_info_add_capability);
  1553. int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs,
  1554. int max_irq_type, size_t *data_size)
  1555. {
  1556. unsigned long minsz;
  1557. size_t size;
  1558. minsz = offsetofend(struct vfio_irq_set, count);
  1559. if ((hdr->argsz < minsz) || (hdr->index >= max_irq_type) ||
  1560. (hdr->count >= (U32_MAX - hdr->start)) ||
  1561. (hdr->flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
  1562. VFIO_IRQ_SET_ACTION_TYPE_MASK)))
  1563. return -EINVAL;
  1564. if (data_size)
  1565. *data_size = 0;
  1566. if (hdr->start >= num_irqs || hdr->start + hdr->count > num_irqs)
  1567. return -EINVAL;
  1568. switch (hdr->flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
  1569. case VFIO_IRQ_SET_DATA_NONE:
  1570. size = 0;
  1571. break;
  1572. case VFIO_IRQ_SET_DATA_BOOL:
  1573. size = sizeof(uint8_t);
  1574. break;
  1575. case VFIO_IRQ_SET_DATA_EVENTFD:
  1576. size = sizeof(int32_t);
  1577. break;
  1578. default:
  1579. return -EINVAL;
  1580. }
  1581. if (size) {
  1582. if (hdr->argsz - minsz < hdr->count * size)
  1583. return -EINVAL;
  1584. if (!data_size)
  1585. return -EINVAL;
  1586. *data_size = hdr->count * size;
  1587. }
  1588. return 0;
  1589. }
  1590. EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare);
  1591. /*
  1592. * Pin a set of guest PFNs and return their associated host PFNs for local
  1593. * domain only.
  1594. * @dev [in] : device
  1595. * @user_pfn [in]: array of user/guest PFNs to be pinned.
  1596. * @npage [in] : count of elements in user_pfn array. This count should not
  1597. * be greater VFIO_PIN_PAGES_MAX_ENTRIES.
  1598. * @prot [in] : protection flags
  1599. * @phys_pfn[out]: array of host PFNs
  1600. * Return error or number of pages pinned.
  1601. */
  1602. int vfio_pin_pages(struct device *dev, unsigned long *user_pfn, int npage,
  1603. int prot, unsigned long *phys_pfn)
  1604. {
  1605. struct vfio_container *container;
  1606. struct vfio_group *group;
  1607. struct vfio_iommu_driver *driver;
  1608. int ret;
  1609. if (!dev || !user_pfn || !phys_pfn || !npage)
  1610. return -EINVAL;
  1611. if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
  1612. return -E2BIG;
  1613. group = vfio_group_get_from_dev(dev);
  1614. if (!group)
  1615. return -ENODEV;
  1616. ret = vfio_group_add_container_user(group);
  1617. if (ret)
  1618. goto err_pin_pages;
  1619. container = group->container;
  1620. driver = container->iommu_driver;
  1621. if (likely(driver && driver->ops->pin_pages))
  1622. ret = driver->ops->pin_pages(container->iommu_data, user_pfn,
  1623. npage, prot, phys_pfn);
  1624. else
  1625. ret = -ENOTTY;
  1626. vfio_group_try_dissolve_container(group);
  1627. err_pin_pages:
  1628. vfio_group_put(group);
  1629. return ret;
  1630. }
  1631. EXPORT_SYMBOL(vfio_pin_pages);
  1632. /*
  1633. * Unpin set of host PFNs for local domain only.
  1634. * @dev [in] : device
  1635. * @user_pfn [in]: array of user/guest PFNs to be unpinned. Number of user/guest
  1636. * PFNs should not be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
  1637. * @npage [in] : count of elements in user_pfn array. This count should not
  1638. * be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
  1639. * Return error or number of pages unpinned.
  1640. */
  1641. int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn, int npage)
  1642. {
  1643. struct vfio_container *container;
  1644. struct vfio_group *group;
  1645. struct vfio_iommu_driver *driver;
  1646. int ret;
  1647. if (!dev || !user_pfn || !npage)
  1648. return -EINVAL;
  1649. if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
  1650. return -E2BIG;
  1651. group = vfio_group_get_from_dev(dev);
  1652. if (!group)
  1653. return -ENODEV;
  1654. ret = vfio_group_add_container_user(group);
  1655. if (ret)
  1656. goto err_unpin_pages;
  1657. container = group->container;
  1658. driver = container->iommu_driver;
  1659. if (likely(driver && driver->ops->unpin_pages))
  1660. ret = driver->ops->unpin_pages(container->iommu_data, user_pfn,
  1661. npage);
  1662. else
  1663. ret = -ENOTTY;
  1664. vfio_group_try_dissolve_container(group);
  1665. err_unpin_pages:
  1666. vfio_group_put(group);
  1667. return ret;
  1668. }
  1669. EXPORT_SYMBOL(vfio_unpin_pages);
  1670. static int vfio_register_iommu_notifier(struct vfio_group *group,
  1671. unsigned long *events,
  1672. struct notifier_block *nb)
  1673. {
  1674. struct vfio_container *container;
  1675. struct vfio_iommu_driver *driver;
  1676. int ret;
  1677. ret = vfio_group_add_container_user(group);
  1678. if (ret)
  1679. return -EINVAL;
  1680. container = group->container;
  1681. driver = container->iommu_driver;
  1682. if (likely(driver && driver->ops->register_notifier))
  1683. ret = driver->ops->register_notifier(container->iommu_data,
  1684. events, nb);
  1685. else
  1686. ret = -ENOTTY;
  1687. vfio_group_try_dissolve_container(group);
  1688. return ret;
  1689. }
  1690. static int vfio_unregister_iommu_notifier(struct vfio_group *group,
  1691. struct notifier_block *nb)
  1692. {
  1693. struct vfio_container *container;
  1694. struct vfio_iommu_driver *driver;
  1695. int ret;
  1696. ret = vfio_group_add_container_user(group);
  1697. if (ret)
  1698. return -EINVAL;
  1699. container = group->container;
  1700. driver = container->iommu_driver;
  1701. if (likely(driver && driver->ops->unregister_notifier))
  1702. ret = driver->ops->unregister_notifier(container->iommu_data,
  1703. nb);
  1704. else
  1705. ret = -ENOTTY;
  1706. vfio_group_try_dissolve_container(group);
  1707. return ret;
  1708. }
  1709. void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
  1710. {
  1711. group->kvm = kvm;
  1712. blocking_notifier_call_chain(&group->notifier,
  1713. VFIO_GROUP_NOTIFY_SET_KVM, kvm);
  1714. }
  1715. EXPORT_SYMBOL_GPL(vfio_group_set_kvm);
  1716. static int vfio_register_group_notifier(struct vfio_group *group,
  1717. unsigned long *events,
  1718. struct notifier_block *nb)
  1719. {
  1720. int ret;
  1721. bool set_kvm = false;
  1722. if (*events & VFIO_GROUP_NOTIFY_SET_KVM)
  1723. set_kvm = true;
  1724. /* clear known events */
  1725. *events &= ~VFIO_GROUP_NOTIFY_SET_KVM;
  1726. /* refuse to continue if still events remaining */
  1727. if (*events)
  1728. return -EINVAL;
  1729. ret = vfio_group_add_container_user(group);
  1730. if (ret)
  1731. return -EINVAL;
  1732. ret = blocking_notifier_chain_register(&group->notifier, nb);
  1733. /*
  1734. * The attaching of kvm and vfio_group might already happen, so
  1735. * here we replay once upon registration.
  1736. */
  1737. if (!ret && set_kvm && group->kvm)
  1738. blocking_notifier_call_chain(&group->notifier,
  1739. VFIO_GROUP_NOTIFY_SET_KVM, group->kvm);
  1740. vfio_group_try_dissolve_container(group);
  1741. return ret;
  1742. }
  1743. static int vfio_unregister_group_notifier(struct vfio_group *group,
  1744. struct notifier_block *nb)
  1745. {
  1746. int ret;
  1747. ret = vfio_group_add_container_user(group);
  1748. if (ret)
  1749. return -EINVAL;
  1750. ret = blocking_notifier_chain_unregister(&group->notifier, nb);
  1751. vfio_group_try_dissolve_container(group);
  1752. return ret;
  1753. }
  1754. int vfio_register_notifier(struct device *dev, enum vfio_notify_type type,
  1755. unsigned long *events, struct notifier_block *nb)
  1756. {
  1757. struct vfio_group *group;
  1758. int ret;
  1759. if (!dev || !nb || !events || (*events == 0))
  1760. return -EINVAL;
  1761. group = vfio_group_get_from_dev(dev);
  1762. if (!group)
  1763. return -ENODEV;
  1764. switch (type) {
  1765. case VFIO_IOMMU_NOTIFY:
  1766. ret = vfio_register_iommu_notifier(group, events, nb);
  1767. break;
  1768. case VFIO_GROUP_NOTIFY:
  1769. ret = vfio_register_group_notifier(group, events, nb);
  1770. break;
  1771. default:
  1772. ret = -EINVAL;
  1773. }
  1774. vfio_group_put(group);
  1775. return ret;
  1776. }
  1777. EXPORT_SYMBOL(vfio_register_notifier);
  1778. int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
  1779. struct notifier_block *nb)
  1780. {
  1781. struct vfio_group *group;
  1782. int ret;
  1783. if (!dev || !nb)
  1784. return -EINVAL;
  1785. group = vfio_group_get_from_dev(dev);
  1786. if (!group)
  1787. return -ENODEV;
  1788. switch (type) {
  1789. case VFIO_IOMMU_NOTIFY:
  1790. ret = vfio_unregister_iommu_notifier(group, nb);
  1791. break;
  1792. case VFIO_GROUP_NOTIFY:
  1793. ret = vfio_unregister_group_notifier(group, nb);
  1794. break;
  1795. default:
  1796. ret = -EINVAL;
  1797. }
  1798. vfio_group_put(group);
  1799. return ret;
  1800. }
  1801. EXPORT_SYMBOL(vfio_unregister_notifier);
  1802. /**
  1803. * Module/class support
  1804. */
  1805. static char *vfio_devnode(struct device *dev, umode_t *mode)
  1806. {
  1807. return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
  1808. }
  1809. static struct miscdevice vfio_dev = {
  1810. .minor = VFIO_MINOR,
  1811. .name = "vfio",
  1812. .fops = &vfio_fops,
  1813. .nodename = "vfio/vfio",
  1814. .mode = S_IRUGO | S_IWUGO,
  1815. };
  1816. static int __init vfio_init(void)
  1817. {
  1818. int ret;
  1819. idr_init(&vfio.group_idr);
  1820. mutex_init(&vfio.group_lock);
  1821. mutex_init(&vfio.iommu_drivers_lock);
  1822. INIT_LIST_HEAD(&vfio.group_list);
  1823. INIT_LIST_HEAD(&vfio.iommu_drivers_list);
  1824. init_waitqueue_head(&vfio.release_q);
  1825. ret = misc_register(&vfio_dev);
  1826. if (ret) {
  1827. pr_err("vfio: misc device register failed\n");
  1828. return ret;
  1829. }
  1830. /* /dev/vfio/$GROUP */
  1831. vfio.class = class_create(THIS_MODULE, "vfio");
  1832. if (IS_ERR(vfio.class)) {
  1833. ret = PTR_ERR(vfio.class);
  1834. goto err_class;
  1835. }
  1836. vfio.class->devnode = vfio_devnode;
  1837. ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK, "vfio");
  1838. if (ret)
  1839. goto err_alloc_chrdev;
  1840. cdev_init(&vfio.group_cdev, &vfio_group_fops);
  1841. ret = cdev_add(&vfio.group_cdev, vfio.group_devt, MINORMASK);
  1842. if (ret)
  1843. goto err_cdev_add;
  1844. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  1845. #ifdef CONFIG_VFIO_NOIOMMU
  1846. vfio_register_iommu_driver(&vfio_noiommu_ops);
  1847. #endif
  1848. return 0;
  1849. err_cdev_add:
  1850. unregister_chrdev_region(vfio.group_devt, MINORMASK);
  1851. err_alloc_chrdev:
  1852. class_destroy(vfio.class);
  1853. vfio.class = NULL;
  1854. err_class:
  1855. misc_deregister(&vfio_dev);
  1856. return ret;
  1857. }
  1858. static void __exit vfio_cleanup(void)
  1859. {
  1860. WARN_ON(!list_empty(&vfio.group_list));
  1861. #ifdef CONFIG_VFIO_NOIOMMU
  1862. vfio_unregister_iommu_driver(&vfio_noiommu_ops);
  1863. #endif
  1864. idr_destroy(&vfio.group_idr);
  1865. cdev_del(&vfio.group_cdev);
  1866. unregister_chrdev_region(vfio.group_devt, MINORMASK);
  1867. class_destroy(vfio.class);
  1868. vfio.class = NULL;
  1869. misc_deregister(&vfio_dev);
  1870. }
  1871. module_init(vfio_init);
  1872. module_exit(vfio_cleanup);
  1873. MODULE_VERSION(DRIVER_VERSION);
  1874. MODULE_LICENSE("GPL v2");
  1875. MODULE_AUTHOR(DRIVER_AUTHOR);
  1876. MODULE_DESCRIPTION(DRIVER_DESC);
  1877. MODULE_ALIAS_MISCDEV(VFIO_MINOR);
  1878. MODULE_ALIAS("devname:vfio/vfio");
  1879. MODULE_SOFTDEP("post: vfio_iommu_type1 vfio_iommu_spapr_tce");