vfio.c 34 KB

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