vfio.c 40 KB

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