container.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  4. *
  5. * VFIO container (/dev/vfio/vfio)
  6. */
  7. #include <linux/file.h>
  8. #include <linux/slab.h>
  9. #include <linux/fs.h>
  10. #include <linux/capability.h>
  11. #include <linux/iommu.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/vfio.h>
  14. #include <uapi/linux/vfio.h>
  15. #include "vfio.h"
  16. struct vfio_container {
  17. struct kref kref;
  18. struct list_head group_list;
  19. struct rw_semaphore group_lock;
  20. struct vfio_iommu_driver *iommu_driver;
  21. void *iommu_data;
  22. bool noiommu;
  23. };
  24. static struct vfio {
  25. struct list_head iommu_drivers_list;
  26. struct mutex iommu_drivers_lock;
  27. } vfio;
  28. #ifdef CONFIG_VFIO_NOIOMMU
  29. bool vfio_noiommu __read_mostly;
  30. module_param_named(enable_unsafe_noiommu_mode,
  31. vfio_noiommu, bool, S_IRUGO | S_IWUSR);
  32. MODULE_PARM_DESC(enable_unsafe_noiommu_mode, "Enable UNSAFE, no-IOMMU mode. This mode provides no device isolation, no DMA translation, no host kernel protection, cannot be used for device assignment to virtual machines, requires RAWIO permissions, and will taint the kernel. If you do not know what this is for, step away. (default: false)");
  33. #endif
  34. static void *vfio_noiommu_open(unsigned long arg)
  35. {
  36. if (arg != VFIO_NOIOMMU_IOMMU)
  37. return ERR_PTR(-EINVAL);
  38. if (!capable(CAP_SYS_RAWIO))
  39. return ERR_PTR(-EPERM);
  40. return NULL;
  41. }
  42. static void vfio_noiommu_release(void *iommu_data)
  43. {
  44. }
  45. static long vfio_noiommu_ioctl(void *iommu_data,
  46. unsigned int cmd, unsigned long arg)
  47. {
  48. if (cmd == VFIO_CHECK_EXTENSION)
  49. return vfio_noiommu && (arg == VFIO_NOIOMMU_IOMMU) ? 1 : 0;
  50. return -ENOTTY;
  51. }
  52. static int vfio_noiommu_attach_group(void *iommu_data,
  53. struct iommu_group *iommu_group, enum vfio_group_type type)
  54. {
  55. return 0;
  56. }
  57. static void vfio_noiommu_detach_group(void *iommu_data,
  58. struct iommu_group *iommu_group)
  59. {
  60. }
  61. static const struct vfio_iommu_driver_ops vfio_noiommu_ops = {
  62. .name = "vfio-noiommu",
  63. .owner = THIS_MODULE,
  64. .open = vfio_noiommu_open,
  65. .release = vfio_noiommu_release,
  66. .ioctl = vfio_noiommu_ioctl,
  67. .attach_group = vfio_noiommu_attach_group,
  68. .detach_group = vfio_noiommu_detach_group,
  69. };
  70. /*
  71. * Only noiommu containers can use vfio-noiommu and noiommu containers can only
  72. * use vfio-noiommu.
  73. */
  74. static bool vfio_iommu_driver_allowed(struct vfio_container *container,
  75. const struct vfio_iommu_driver *driver)
  76. {
  77. if (!IS_ENABLED(CONFIG_VFIO_NOIOMMU))
  78. return true;
  79. return container->noiommu == (driver->ops == &vfio_noiommu_ops);
  80. }
  81. /*
  82. * IOMMU driver registration
  83. */
  84. int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops)
  85. {
  86. struct vfio_iommu_driver *driver, *tmp;
  87. if (WARN_ON(!ops->register_device != !ops->unregister_device))
  88. return -EINVAL;
  89. driver = kzalloc(sizeof(*driver), GFP_KERNEL);
  90. if (!driver)
  91. return -ENOMEM;
  92. driver->ops = ops;
  93. mutex_lock(&vfio.iommu_drivers_lock);
  94. /* Check for duplicates */
  95. list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) {
  96. if (tmp->ops == ops) {
  97. mutex_unlock(&vfio.iommu_drivers_lock);
  98. kfree(driver);
  99. return -EINVAL;
  100. }
  101. }
  102. list_add(&driver->vfio_next, &vfio.iommu_drivers_list);
  103. mutex_unlock(&vfio.iommu_drivers_lock);
  104. return 0;
  105. }
  106. EXPORT_SYMBOL_GPL(vfio_register_iommu_driver);
  107. void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops)
  108. {
  109. struct vfio_iommu_driver *driver;
  110. mutex_lock(&vfio.iommu_drivers_lock);
  111. list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
  112. if (driver->ops == ops) {
  113. list_del(&driver->vfio_next);
  114. mutex_unlock(&vfio.iommu_drivers_lock);
  115. kfree(driver);
  116. return;
  117. }
  118. }
  119. mutex_unlock(&vfio.iommu_drivers_lock);
  120. }
  121. EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver);
  122. /*
  123. * Container objects - containers are created when /dev/vfio/vfio is
  124. * opened, but their lifecycle extends until the last user is done, so
  125. * it's freed via kref. Must support container/group/device being
  126. * closed in any order.
  127. */
  128. static void vfio_container_release(struct kref *kref)
  129. {
  130. struct vfio_container *container;
  131. container = container_of(kref, struct vfio_container, kref);
  132. kfree(container);
  133. }
  134. static void vfio_container_get(struct vfio_container *container)
  135. {
  136. kref_get(&container->kref);
  137. }
  138. static void vfio_container_put(struct vfio_container *container)
  139. {
  140. kref_put(&container->kref, vfio_container_release);
  141. }
  142. void vfio_device_container_register(struct vfio_device *device)
  143. {
  144. struct vfio_iommu_driver *iommu_driver =
  145. device->group->container->iommu_driver;
  146. if (iommu_driver && iommu_driver->ops->register_device)
  147. iommu_driver->ops->register_device(
  148. device->group->container->iommu_data, device);
  149. }
  150. void vfio_device_container_unregister(struct vfio_device *device)
  151. {
  152. struct vfio_iommu_driver *iommu_driver =
  153. device->group->container->iommu_driver;
  154. if (iommu_driver && iommu_driver->ops->unregister_device)
  155. iommu_driver->ops->unregister_device(
  156. device->group->container->iommu_data, device);
  157. }
  158. long vfio_container_ioctl_check_extension(struct vfio_container *container,
  159. unsigned long arg)
  160. {
  161. struct vfio_iommu_driver *driver;
  162. long ret = 0;
  163. down_read(&container->group_lock);
  164. driver = container->iommu_driver;
  165. switch (arg) {
  166. /* No base extensions yet */
  167. default:
  168. /*
  169. * If no driver is set, poll all registered drivers for
  170. * extensions and return the first positive result. If
  171. * a driver is already set, further queries will be passed
  172. * only to that driver.
  173. */
  174. if (!driver) {
  175. mutex_lock(&vfio.iommu_drivers_lock);
  176. list_for_each_entry(driver, &vfio.iommu_drivers_list,
  177. vfio_next) {
  178. if (!list_empty(&container->group_list) &&
  179. !vfio_iommu_driver_allowed(container,
  180. driver))
  181. continue;
  182. if (!try_module_get(driver->ops->owner))
  183. continue;
  184. ret = driver->ops->ioctl(NULL,
  185. VFIO_CHECK_EXTENSION,
  186. arg);
  187. module_put(driver->ops->owner);
  188. if (ret > 0)
  189. break;
  190. }
  191. mutex_unlock(&vfio.iommu_drivers_lock);
  192. } else
  193. ret = driver->ops->ioctl(container->iommu_data,
  194. VFIO_CHECK_EXTENSION, arg);
  195. }
  196. up_read(&container->group_lock);
  197. return ret;
  198. }
  199. /* hold write lock on container->group_lock */
  200. static int __vfio_container_attach_groups(struct vfio_container *container,
  201. struct vfio_iommu_driver *driver,
  202. void *data)
  203. {
  204. struct vfio_group *group;
  205. int ret = -ENODEV;
  206. list_for_each_entry(group, &container->group_list, container_next) {
  207. ret = driver->ops->attach_group(data, group->iommu_group,
  208. group->type);
  209. if (ret)
  210. goto unwind;
  211. }
  212. return ret;
  213. unwind:
  214. list_for_each_entry_continue_reverse(group, &container->group_list,
  215. container_next) {
  216. driver->ops->detach_group(data, group->iommu_group);
  217. }
  218. return ret;
  219. }
  220. static long vfio_ioctl_set_iommu(struct vfio_container *container,
  221. unsigned long arg)
  222. {
  223. struct vfio_iommu_driver *driver;
  224. long ret = -ENODEV;
  225. down_write(&container->group_lock);
  226. /*
  227. * The container is designed to be an unprivileged interface while
  228. * the group can be assigned to specific users. Therefore, only by
  229. * adding a group to a container does the user get the privilege of
  230. * enabling the iommu, which may allocate finite resources. There
  231. * is no unset_iommu, but by removing all the groups from a container,
  232. * the container is deprivileged and returns to an unset state.
  233. */
  234. if (list_empty(&container->group_list) || container->iommu_driver) {
  235. up_write(&container->group_lock);
  236. return -EINVAL;
  237. }
  238. mutex_lock(&vfio.iommu_drivers_lock);
  239. list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
  240. void *data;
  241. if (!vfio_iommu_driver_allowed(container, driver))
  242. continue;
  243. if (!try_module_get(driver->ops->owner))
  244. continue;
  245. /*
  246. * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION,
  247. * so test which iommu driver reported support for this
  248. * extension and call open on them. We also pass them the
  249. * magic, allowing a single driver to support multiple
  250. * interfaces if they'd like.
  251. */
  252. if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) {
  253. module_put(driver->ops->owner);
  254. continue;
  255. }
  256. data = driver->ops->open(arg);
  257. if (IS_ERR(data)) {
  258. ret = PTR_ERR(data);
  259. module_put(driver->ops->owner);
  260. continue;
  261. }
  262. ret = __vfio_container_attach_groups(container, driver, data);
  263. if (ret) {
  264. driver->ops->release(data);
  265. module_put(driver->ops->owner);
  266. continue;
  267. }
  268. container->iommu_driver = driver;
  269. container->iommu_data = data;
  270. break;
  271. }
  272. mutex_unlock(&vfio.iommu_drivers_lock);
  273. up_write(&container->group_lock);
  274. return ret;
  275. }
  276. static long vfio_fops_unl_ioctl(struct file *filep,
  277. unsigned int cmd, unsigned long arg)
  278. {
  279. struct vfio_container *container = filep->private_data;
  280. struct vfio_iommu_driver *driver;
  281. void *data;
  282. long ret = -EINVAL;
  283. if (!container)
  284. return ret;
  285. switch (cmd) {
  286. case VFIO_GET_API_VERSION:
  287. ret = VFIO_API_VERSION;
  288. break;
  289. case VFIO_CHECK_EXTENSION:
  290. ret = vfio_container_ioctl_check_extension(container, arg);
  291. break;
  292. case VFIO_SET_IOMMU:
  293. ret = vfio_ioctl_set_iommu(container, arg);
  294. break;
  295. default:
  296. driver = container->iommu_driver;
  297. data = container->iommu_data;
  298. if (driver) /* passthrough all unrecognized ioctls */
  299. ret = driver->ops->ioctl(data, cmd, arg);
  300. }
  301. return ret;
  302. }
  303. static int vfio_fops_open(struct inode *inode, struct file *filep)
  304. {
  305. struct vfio_container *container;
  306. container = kzalloc(sizeof(*container), GFP_KERNEL);
  307. if (!container)
  308. return -ENOMEM;
  309. INIT_LIST_HEAD(&container->group_list);
  310. init_rwsem(&container->group_lock);
  311. kref_init(&container->kref);
  312. filep->private_data = container;
  313. return 0;
  314. }
  315. static int vfio_fops_release(struct inode *inode, struct file *filep)
  316. {
  317. struct vfio_container *container = filep->private_data;
  318. struct vfio_iommu_driver *driver = container->iommu_driver;
  319. if (driver && driver->ops->notify)
  320. driver->ops->notify(container->iommu_data,
  321. VFIO_IOMMU_CONTAINER_CLOSE);
  322. filep->private_data = NULL;
  323. vfio_container_put(container);
  324. return 0;
  325. }
  326. static const struct file_operations vfio_fops = {
  327. .owner = THIS_MODULE,
  328. .open = vfio_fops_open,
  329. .release = vfio_fops_release,
  330. .unlocked_ioctl = vfio_fops_unl_ioctl,
  331. .compat_ioctl = compat_ptr_ioctl,
  332. };
  333. struct vfio_container *vfio_container_from_file(struct file *file)
  334. {
  335. struct vfio_container *container;
  336. /* Sanity check, is this really our fd? */
  337. if (file->f_op != &vfio_fops)
  338. return NULL;
  339. container = file->private_data;
  340. WARN_ON(!container); /* fget ensures we don't race vfio_release */
  341. return container;
  342. }
  343. static struct miscdevice vfio_dev = {
  344. .minor = VFIO_MINOR,
  345. .name = "vfio",
  346. .fops = &vfio_fops,
  347. .nodename = "vfio/vfio",
  348. .mode = S_IRUGO | S_IWUGO,
  349. };
  350. int vfio_container_attach_group(struct vfio_container *container,
  351. struct vfio_group *group)
  352. {
  353. struct vfio_iommu_driver *driver;
  354. int ret = 0;
  355. lockdep_assert_held(&group->group_lock);
  356. if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO))
  357. return -EPERM;
  358. down_write(&container->group_lock);
  359. /* Real groups and fake groups cannot mix */
  360. if (!list_empty(&container->group_list) &&
  361. container->noiommu != (group->type == VFIO_NO_IOMMU)) {
  362. ret = -EPERM;
  363. goto out_unlock_container;
  364. }
  365. if (group->type == VFIO_IOMMU) {
  366. ret = iommu_group_claim_dma_owner(group->iommu_group, group);
  367. if (ret)
  368. goto out_unlock_container;
  369. }
  370. driver = container->iommu_driver;
  371. if (driver) {
  372. ret = driver->ops->attach_group(container->iommu_data,
  373. group->iommu_group,
  374. group->type);
  375. if (ret) {
  376. if (group->type == VFIO_IOMMU)
  377. iommu_group_release_dma_owner(
  378. group->iommu_group);
  379. goto out_unlock_container;
  380. }
  381. }
  382. group->container = container;
  383. group->container_users = 1;
  384. container->noiommu = (group->type == VFIO_NO_IOMMU);
  385. list_add(&group->container_next, &container->group_list);
  386. /* Get a reference on the container and mark a user within the group */
  387. vfio_container_get(container);
  388. out_unlock_container:
  389. up_write(&container->group_lock);
  390. return ret;
  391. }
  392. void vfio_group_detach_container(struct vfio_group *group)
  393. {
  394. struct vfio_container *container = group->container;
  395. struct vfio_iommu_driver *driver;
  396. lockdep_assert_held(&group->group_lock);
  397. WARN_ON(group->container_users != 1);
  398. down_write(&container->group_lock);
  399. driver = container->iommu_driver;
  400. if (driver)
  401. driver->ops->detach_group(container->iommu_data,
  402. group->iommu_group);
  403. if (group->type == VFIO_IOMMU)
  404. iommu_group_release_dma_owner(group->iommu_group);
  405. group->container = NULL;
  406. group->container_users = 0;
  407. list_del(&group->container_next);
  408. /* Detaching the last group deprivileges a container, remove iommu */
  409. if (driver && list_empty(&container->group_list)) {
  410. driver->ops->release(container->iommu_data);
  411. module_put(driver->ops->owner);
  412. container->iommu_driver = NULL;
  413. container->iommu_data = NULL;
  414. }
  415. up_write(&container->group_lock);
  416. vfio_container_put(container);
  417. }
  418. int vfio_device_assign_container(struct vfio_device *device)
  419. {
  420. struct vfio_group *group = device->group;
  421. lockdep_assert_held(&group->group_lock);
  422. if (!group->container || !group->container->iommu_driver ||
  423. WARN_ON(!group->container_users))
  424. return -EINVAL;
  425. if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO))
  426. return -EPERM;
  427. get_file(group->opened_file);
  428. group->container_users++;
  429. return 0;
  430. }
  431. void vfio_device_unassign_container(struct vfio_device *device)
  432. {
  433. mutex_lock(&device->group->group_lock);
  434. WARN_ON(device->group->container_users <= 1);
  435. device->group->container_users--;
  436. fput(device->group->opened_file);
  437. mutex_unlock(&device->group->group_lock);
  438. }
  439. /*
  440. * Pin contiguous user pages and return their associated host pages for local
  441. * domain only.
  442. * @device [in] : device
  443. * @iova [in] : starting IOVA of user pages to be pinned.
  444. * @npage [in] : count of pages to be pinned. This count should not
  445. * be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
  446. * @prot [in] : protection flags
  447. * @pages[out] : array of host pages
  448. * Return error or number of pages pinned.
  449. *
  450. * A driver may only call this function if the vfio_device was created
  451. * by vfio_register_emulated_iommu_dev().
  452. */
  453. int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
  454. int npage, int prot, struct page **pages)
  455. {
  456. struct vfio_container *container;
  457. struct vfio_group *group = device->group;
  458. struct vfio_iommu_driver *driver;
  459. int ret;
  460. if (!pages || !npage || !vfio_assert_device_open(device))
  461. return -EINVAL;
  462. if (npage > VFIO_PIN_PAGES_MAX_ENTRIES)
  463. return -E2BIG;
  464. /* group->container cannot change while a vfio device is open */
  465. container = group->container;
  466. driver = container->iommu_driver;
  467. if (likely(driver && driver->ops->pin_pages))
  468. ret = driver->ops->pin_pages(container->iommu_data,
  469. group->iommu_group, iova,
  470. npage, prot, pages);
  471. else
  472. ret = -ENOTTY;
  473. return ret;
  474. }
  475. EXPORT_SYMBOL(vfio_pin_pages);
  476. /*
  477. * Unpin contiguous host pages for local domain only.
  478. * @device [in] : device
  479. * @iova [in] : starting address of user pages to be unpinned.
  480. * @npage [in] : count of pages to be unpinned. This count should not
  481. * be greater than VFIO_PIN_PAGES_MAX_ENTRIES.
  482. */
  483. void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage)
  484. {
  485. struct vfio_container *container;
  486. struct vfio_iommu_driver *driver;
  487. if (WARN_ON(npage <= 0 || npage > VFIO_PIN_PAGES_MAX_ENTRIES))
  488. return;
  489. if (WARN_ON(!vfio_assert_device_open(device)))
  490. return;
  491. /* group->container cannot change while a vfio device is open */
  492. container = device->group->container;
  493. driver = container->iommu_driver;
  494. driver->ops->unpin_pages(container->iommu_data, iova, npage);
  495. }
  496. EXPORT_SYMBOL(vfio_unpin_pages);
  497. /*
  498. * This interface allows the CPUs to perform some sort of virtual DMA on
  499. * behalf of the device.
  500. *
  501. * CPUs read/write from/into a range of IOVAs pointing to user space memory
  502. * into/from a kernel buffer.
  503. *
  504. * As the read/write of user space memory is conducted via the CPUs and is
  505. * not a real device DMA, it is not necessary to pin the user space memory.
  506. *
  507. * @device [in] : VFIO device
  508. * @iova [in] : base IOVA of a user space buffer
  509. * @data [in] : pointer to kernel buffer
  510. * @len [in] : kernel buffer length
  511. * @write : indicate read or write
  512. * Return error code on failure or 0 on success.
  513. */
  514. int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data,
  515. size_t len, bool write)
  516. {
  517. struct vfio_container *container;
  518. struct vfio_iommu_driver *driver;
  519. int ret = 0;
  520. if (!data || len <= 0 || !vfio_assert_device_open(device))
  521. return -EINVAL;
  522. /* group->container cannot change while a vfio device is open */
  523. container = device->group->container;
  524. driver = container->iommu_driver;
  525. if (likely(driver && driver->ops->dma_rw))
  526. ret = driver->ops->dma_rw(container->iommu_data,
  527. iova, data, len, write);
  528. else
  529. ret = -ENOTTY;
  530. return ret;
  531. }
  532. EXPORT_SYMBOL(vfio_dma_rw);
  533. int __init vfio_container_init(void)
  534. {
  535. int ret;
  536. mutex_init(&vfio.iommu_drivers_lock);
  537. INIT_LIST_HEAD(&vfio.iommu_drivers_list);
  538. ret = misc_register(&vfio_dev);
  539. if (ret) {
  540. pr_err("vfio: misc device register failed\n");
  541. return ret;
  542. }
  543. if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) {
  544. ret = vfio_register_iommu_driver(&vfio_noiommu_ops);
  545. if (ret)
  546. goto err_misc;
  547. }
  548. return 0;
  549. err_misc:
  550. misc_deregister(&vfio_dev);
  551. return ret;
  552. }
  553. void vfio_container_cleanup(void)
  554. {
  555. if (IS_ENABLED(CONFIG_VFIO_NOIOMMU))
  556. vfio_unregister_iommu_driver(&vfio_noiommu_ops);
  557. misc_deregister(&vfio_dev);
  558. mutex_destroy(&vfio.iommu_drivers_lock);
  559. }