vfio.c 37 KB

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