vfio.c 42 KB

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