group.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VFIO core
  4. *
  5. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  6. * Author: Alex Williamson <alex.williamson@redhat.com>
  7. *
  8. * Derived from original vfio:
  9. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  10. * Author: Tom Lyon, pugs@cisco.com
  11. */
  12. #include <linux/vfio.h>
  13. #include <linux/iommufd.h>
  14. #include <linux/anon_inodes.h>
  15. #include "vfio.h"
  16. static struct vfio {
  17. struct class *class;
  18. struct list_head group_list;
  19. struct mutex group_lock; /* locks group_list */
  20. struct ida group_ida;
  21. dev_t group_devt;
  22. } vfio;
  23. static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group,
  24. char *buf)
  25. {
  26. struct vfio_device *it, *device = ERR_PTR(-ENODEV);
  27. mutex_lock(&group->device_lock);
  28. list_for_each_entry(it, &group->device_list, group_next) {
  29. int ret;
  30. if (it->ops->match) {
  31. ret = it->ops->match(it, buf);
  32. if (ret < 0) {
  33. device = ERR_PTR(ret);
  34. break;
  35. }
  36. } else {
  37. ret = !strcmp(dev_name(it->dev), buf);
  38. }
  39. if (ret && vfio_device_try_get_registration(it)) {
  40. device = it;
  41. break;
  42. }
  43. }
  44. mutex_unlock(&group->device_lock);
  45. return device;
  46. }
  47. /*
  48. * VFIO Group fd, /dev/vfio/$GROUP
  49. */
  50. static bool vfio_group_has_iommu(struct vfio_group *group)
  51. {
  52. lockdep_assert_held(&group->group_lock);
  53. /*
  54. * There can only be users if there is a container, and if there is a
  55. * container there must be users.
  56. */
  57. WARN_ON(!group->container != !group->container_users);
  58. return group->container || group->iommufd;
  59. }
  60. /*
  61. * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or
  62. * if there was no container to unset. Since the ioctl is called on
  63. * the group, we know that still exists, therefore the only valid
  64. * transition here is 1->0.
  65. */
  66. static int vfio_group_ioctl_unset_container(struct vfio_group *group)
  67. {
  68. int ret = 0;
  69. mutex_lock(&group->group_lock);
  70. if (!vfio_group_has_iommu(group)) {
  71. ret = -EINVAL;
  72. goto out_unlock;
  73. }
  74. if (group->container) {
  75. if (group->container_users != 1) {
  76. ret = -EBUSY;
  77. goto out_unlock;
  78. }
  79. vfio_group_detach_container(group);
  80. }
  81. if (group->iommufd) {
  82. iommufd_ctx_put(group->iommufd);
  83. group->iommufd = NULL;
  84. }
  85. out_unlock:
  86. mutex_unlock(&group->group_lock);
  87. return ret;
  88. }
  89. static int vfio_group_ioctl_set_container(struct vfio_group *group,
  90. int __user *arg)
  91. {
  92. struct vfio_container *container;
  93. struct iommufd_ctx *iommufd;
  94. int ret;
  95. int fd;
  96. if (get_user(fd, arg))
  97. return -EFAULT;
  98. CLASS(fd, f)(fd);
  99. if (fd_empty(f))
  100. return -EBADF;
  101. mutex_lock(&group->group_lock);
  102. if (vfio_group_has_iommu(group)) {
  103. ret = -EINVAL;
  104. goto out_unlock;
  105. }
  106. if (!group->iommu_group) {
  107. ret = -ENODEV;
  108. goto out_unlock;
  109. }
  110. container = vfio_container_from_file(fd_file(f));
  111. if (container) {
  112. ret = vfio_container_attach_group(container, group);
  113. goto out_unlock;
  114. }
  115. iommufd = iommufd_ctx_from_file(fd_file(f));
  116. if (!IS_ERR(iommufd)) {
  117. if (IS_ENABLED(CONFIG_VFIO_NOIOMMU) &&
  118. group->type == VFIO_NO_IOMMU)
  119. ret = iommufd_vfio_compat_set_no_iommu(iommufd);
  120. else
  121. ret = iommufd_vfio_compat_ioas_create(iommufd);
  122. if (ret) {
  123. iommufd_ctx_put(iommufd);
  124. goto out_unlock;
  125. }
  126. group->iommufd = iommufd;
  127. goto out_unlock;
  128. }
  129. /* The FD passed is not recognized. */
  130. ret = -EBADFD;
  131. out_unlock:
  132. mutex_unlock(&group->group_lock);
  133. return ret;
  134. }
  135. static void vfio_device_group_get_kvm_safe(struct vfio_device *device)
  136. {
  137. spin_lock(&device->group->kvm_ref_lock);
  138. vfio_device_get_kvm_safe(device, device->group->kvm);
  139. spin_unlock(&device->group->kvm_ref_lock);
  140. }
  141. static int vfio_df_group_open(struct vfio_device_file *df)
  142. {
  143. struct vfio_device *device = df->device;
  144. int ret;
  145. mutex_lock(&device->group->group_lock);
  146. if (!vfio_group_has_iommu(device->group)) {
  147. ret = -EINVAL;
  148. goto out_unlock;
  149. }
  150. mutex_lock(&device->dev_set->lock);
  151. /*
  152. * Before the first device open, get the KVM pointer currently
  153. * associated with the group (if there is one) and obtain a reference
  154. * now that will be held until the open_count reaches 0 again. Save
  155. * the pointer in the device for use by drivers.
  156. */
  157. if (device->open_count == 0)
  158. vfio_device_group_get_kvm_safe(device);
  159. df->iommufd = device->group->iommufd;
  160. if (df->iommufd && vfio_device_is_noiommu(device) && device->open_count == 0) {
  161. /*
  162. * Require no compat ioas to be assigned to proceed. The basic
  163. * statement is that the user cannot have done something that
  164. * implies they expected translation to exist
  165. */
  166. if (!capable(CAP_SYS_RAWIO) ||
  167. vfio_iommufd_device_has_compat_ioas(device, df->iommufd)) {
  168. ret = -EPERM;
  169. goto out_put_kvm;
  170. }
  171. }
  172. ret = vfio_df_open(df);
  173. if (ret)
  174. goto out_put_kvm;
  175. if (df->iommufd && device->open_count == 1) {
  176. ret = vfio_iommufd_compat_attach_ioas(device, df->iommufd);
  177. if (ret)
  178. goto out_close_device;
  179. }
  180. /*
  181. * Paired with smp_load_acquire() in vfio_device_fops::ioctl/
  182. * read/write/mmap and vfio_file_has_device_access()
  183. */
  184. smp_store_release(&df->access_granted, true);
  185. mutex_unlock(&device->dev_set->lock);
  186. mutex_unlock(&device->group->group_lock);
  187. return 0;
  188. out_close_device:
  189. vfio_df_close(df);
  190. out_put_kvm:
  191. df->iommufd = NULL;
  192. if (device->open_count == 0)
  193. vfio_device_put_kvm(device);
  194. mutex_unlock(&device->dev_set->lock);
  195. out_unlock:
  196. mutex_unlock(&device->group->group_lock);
  197. return ret;
  198. }
  199. void vfio_df_group_close(struct vfio_device_file *df)
  200. {
  201. struct vfio_device *device = df->device;
  202. mutex_lock(&device->group->group_lock);
  203. mutex_lock(&device->dev_set->lock);
  204. vfio_df_close(df);
  205. df->iommufd = NULL;
  206. if (device->open_count == 0)
  207. vfio_device_put_kvm(device);
  208. mutex_unlock(&device->dev_set->lock);
  209. mutex_unlock(&device->group->group_lock);
  210. }
  211. static struct file *vfio_device_open_file(struct vfio_device *device)
  212. {
  213. struct vfio_device_file *df;
  214. struct file *filep;
  215. int ret;
  216. df = vfio_allocate_device_file(device);
  217. if (IS_ERR(df)) {
  218. ret = PTR_ERR(df);
  219. goto err_out;
  220. }
  221. df->group = device->group;
  222. ret = vfio_df_group_open(df);
  223. if (ret)
  224. goto err_free;
  225. filep = anon_inode_getfile_fmode("[vfio-device]", &vfio_device_fops,
  226. df, O_RDWR, FMODE_PREAD | FMODE_PWRITE);
  227. if (IS_ERR(filep)) {
  228. ret = PTR_ERR(filep);
  229. goto err_close_device;
  230. }
  231. /*
  232. * Use the pseudo fs inode on the device to link all mmaps
  233. * to the same address space, allowing us to unmap all vmas
  234. * associated to this device using unmap_mapping_range().
  235. */
  236. filep->f_mapping = device->inode->i_mapping;
  237. if (device->group->type == VFIO_NO_IOMMU)
  238. dev_warn(device->dev, "vfio-noiommu device opened by user "
  239. "(%s:%d)\n", current->comm, task_pid_nr(current));
  240. /*
  241. * On success the ref of device is moved to the file and
  242. * put in vfio_device_fops_release()
  243. */
  244. return filep;
  245. err_close_device:
  246. vfio_df_group_close(df);
  247. err_free:
  248. kfree(df);
  249. err_out:
  250. return ERR_PTR(ret);
  251. }
  252. static int vfio_group_ioctl_get_device_fd(struct vfio_group *group,
  253. char __user *arg)
  254. {
  255. struct vfio_device *device;
  256. char *buf;
  257. int fd;
  258. buf = strndup_user(arg, PAGE_SIZE);
  259. if (IS_ERR(buf))
  260. return PTR_ERR(buf);
  261. device = vfio_device_get_from_name(group, buf);
  262. kfree(buf);
  263. if (IS_ERR(device))
  264. return PTR_ERR(device);
  265. fd = FD_ADD(O_CLOEXEC, vfio_device_open_file(device));
  266. if (fd < 0)
  267. vfio_device_put_registration(device);
  268. return fd;
  269. }
  270. static int vfio_group_ioctl_get_status(struct vfio_group *group,
  271. struct vfio_group_status __user *arg)
  272. {
  273. unsigned long minsz = offsetofend(struct vfio_group_status, flags);
  274. struct vfio_group_status status;
  275. if (copy_from_user(&status, arg, minsz))
  276. return -EFAULT;
  277. if (status.argsz < minsz)
  278. return -EINVAL;
  279. status.flags = 0;
  280. mutex_lock(&group->group_lock);
  281. if (!group->iommu_group) {
  282. mutex_unlock(&group->group_lock);
  283. return -ENODEV;
  284. }
  285. /*
  286. * With the container FD the iommu_group_claim_dma_owner() is done
  287. * during SET_CONTAINER but for IOMMFD this is done during
  288. * VFIO_GROUP_GET_DEVICE_FD. Meaning that with iommufd
  289. * VFIO_GROUP_FLAGS_VIABLE could be set but GET_DEVICE_FD will fail due
  290. * to viability.
  291. */
  292. if (vfio_group_has_iommu(group))
  293. status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET |
  294. VFIO_GROUP_FLAGS_VIABLE;
  295. else if (!iommu_group_dma_owner_claimed(group->iommu_group))
  296. status.flags |= VFIO_GROUP_FLAGS_VIABLE;
  297. mutex_unlock(&group->group_lock);
  298. if (copy_to_user(arg, &status, minsz))
  299. return -EFAULT;
  300. return 0;
  301. }
  302. static long vfio_group_fops_unl_ioctl(struct file *filep,
  303. unsigned int cmd, unsigned long arg)
  304. {
  305. struct vfio_group *group = filep->private_data;
  306. void __user *uarg = (void __user *)arg;
  307. switch (cmd) {
  308. case VFIO_GROUP_GET_DEVICE_FD:
  309. return vfio_group_ioctl_get_device_fd(group, uarg);
  310. case VFIO_GROUP_GET_STATUS:
  311. return vfio_group_ioctl_get_status(group, uarg);
  312. case VFIO_GROUP_SET_CONTAINER:
  313. return vfio_group_ioctl_set_container(group, uarg);
  314. case VFIO_GROUP_UNSET_CONTAINER:
  315. return vfio_group_ioctl_unset_container(group);
  316. default:
  317. return -ENOTTY;
  318. }
  319. }
  320. int vfio_device_block_group(struct vfio_device *device)
  321. {
  322. struct vfio_group *group = device->group;
  323. int ret = 0;
  324. mutex_lock(&group->group_lock);
  325. if (group->opened_file) {
  326. ret = -EBUSY;
  327. goto out_unlock;
  328. }
  329. group->cdev_device_open_cnt++;
  330. out_unlock:
  331. mutex_unlock(&group->group_lock);
  332. return ret;
  333. }
  334. void vfio_device_unblock_group(struct vfio_device *device)
  335. {
  336. struct vfio_group *group = device->group;
  337. mutex_lock(&group->group_lock);
  338. group->cdev_device_open_cnt--;
  339. mutex_unlock(&group->group_lock);
  340. }
  341. static int vfio_group_fops_open(struct inode *inode, struct file *filep)
  342. {
  343. struct vfio_group *group =
  344. container_of(inode->i_cdev, struct vfio_group, cdev);
  345. int ret;
  346. mutex_lock(&group->group_lock);
  347. /*
  348. * drivers can be zero if this races with vfio_device_remove_group(), it
  349. * will be stable at 0 under the group rwsem
  350. */
  351. if (refcount_read(&group->drivers) == 0) {
  352. ret = -ENODEV;
  353. goto out_unlock;
  354. }
  355. if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) {
  356. ret = -EPERM;
  357. goto out_unlock;
  358. }
  359. if (group->cdev_device_open_cnt) {
  360. ret = -EBUSY;
  361. goto out_unlock;
  362. }
  363. /*
  364. * Do we need multiple instances of the group open? Seems not.
  365. */
  366. if (group->opened_file) {
  367. ret = -EBUSY;
  368. goto out_unlock;
  369. }
  370. group->opened_file = filep;
  371. filep->private_data = group;
  372. ret = 0;
  373. out_unlock:
  374. mutex_unlock(&group->group_lock);
  375. return ret;
  376. }
  377. static int vfio_group_fops_release(struct inode *inode, struct file *filep)
  378. {
  379. struct vfio_group *group = filep->private_data;
  380. filep->private_data = NULL;
  381. mutex_lock(&group->group_lock);
  382. /*
  383. * Device FDs hold a group file reference, therefore the group release
  384. * is only called when there are no open devices.
  385. */
  386. WARN_ON(group->notifier.head);
  387. if (group->container)
  388. vfio_group_detach_container(group);
  389. if (group->iommufd) {
  390. iommufd_ctx_put(group->iommufd);
  391. group->iommufd = NULL;
  392. }
  393. group->opened_file = NULL;
  394. mutex_unlock(&group->group_lock);
  395. return 0;
  396. }
  397. static const struct file_operations vfio_group_fops = {
  398. .owner = THIS_MODULE,
  399. .unlocked_ioctl = vfio_group_fops_unl_ioctl,
  400. .compat_ioctl = compat_ptr_ioctl,
  401. .open = vfio_group_fops_open,
  402. .release = vfio_group_fops_release,
  403. };
  404. /*
  405. * Group objects - create, release, get, put, search
  406. */
  407. static struct vfio_group *
  408. vfio_group_find_from_iommu(struct iommu_group *iommu_group)
  409. {
  410. struct vfio_group *group;
  411. lockdep_assert_held(&vfio.group_lock);
  412. /*
  413. * group->iommu_group from the vfio.group_list cannot be NULL
  414. * under the vfio.group_lock.
  415. */
  416. list_for_each_entry(group, &vfio.group_list, vfio_next) {
  417. if (group->iommu_group == iommu_group)
  418. return group;
  419. }
  420. return NULL;
  421. }
  422. static void vfio_group_release(struct device *dev)
  423. {
  424. struct vfio_group *group = container_of(dev, struct vfio_group, dev);
  425. mutex_destroy(&group->device_lock);
  426. mutex_destroy(&group->group_lock);
  427. WARN_ON(group->iommu_group);
  428. WARN_ON(group->cdev_device_open_cnt);
  429. ida_free(&vfio.group_ida, MINOR(group->dev.devt));
  430. kfree(group);
  431. }
  432. static struct vfio_group *vfio_group_alloc(struct iommu_group *iommu_group,
  433. enum vfio_group_type type)
  434. {
  435. struct vfio_group *group;
  436. int minor;
  437. group = kzalloc_obj(*group);
  438. if (!group)
  439. return ERR_PTR(-ENOMEM);
  440. minor = ida_alloc_max(&vfio.group_ida, MINORMASK, GFP_KERNEL);
  441. if (minor < 0) {
  442. kfree(group);
  443. return ERR_PTR(minor);
  444. }
  445. device_initialize(&group->dev);
  446. group->dev.devt = MKDEV(MAJOR(vfio.group_devt), minor);
  447. group->dev.class = vfio.class;
  448. group->dev.release = vfio_group_release;
  449. cdev_init(&group->cdev, &vfio_group_fops);
  450. group->cdev.owner = THIS_MODULE;
  451. refcount_set(&group->drivers, 1);
  452. mutex_init(&group->group_lock);
  453. spin_lock_init(&group->kvm_ref_lock);
  454. INIT_LIST_HEAD(&group->device_list);
  455. mutex_init(&group->device_lock);
  456. group->iommu_group = iommu_group;
  457. /* put in vfio_group_release() */
  458. iommu_group_ref_get(iommu_group);
  459. group->type = type;
  460. BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
  461. return group;
  462. }
  463. static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group,
  464. enum vfio_group_type type)
  465. {
  466. struct vfio_group *group;
  467. struct vfio_group *ret;
  468. int err;
  469. lockdep_assert_held(&vfio.group_lock);
  470. group = vfio_group_alloc(iommu_group, type);
  471. if (IS_ERR(group))
  472. return group;
  473. err = dev_set_name(&group->dev, "%s%d",
  474. group->type == VFIO_NO_IOMMU ? "noiommu-" : "",
  475. iommu_group_id(iommu_group));
  476. if (err) {
  477. ret = ERR_PTR(err);
  478. goto err_put;
  479. }
  480. err = cdev_device_add(&group->cdev, &group->dev);
  481. if (err) {
  482. ret = ERR_PTR(err);
  483. goto err_put;
  484. }
  485. list_add(&group->vfio_next, &vfio.group_list);
  486. return group;
  487. err_put:
  488. put_device(&group->dev);
  489. return ret;
  490. }
  491. static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev,
  492. enum vfio_group_type type)
  493. {
  494. struct iommu_group *iommu_group;
  495. struct vfio_group *group;
  496. int ret;
  497. iommu_group = iommu_group_alloc();
  498. if (IS_ERR(iommu_group))
  499. return ERR_CAST(iommu_group);
  500. ret = iommu_group_set_name(iommu_group, "vfio-noiommu");
  501. if (ret)
  502. goto out_put_group;
  503. ret = iommu_group_add_device(iommu_group, dev);
  504. if (ret)
  505. goto out_put_group;
  506. mutex_lock(&vfio.group_lock);
  507. group = vfio_create_group(iommu_group, type);
  508. mutex_unlock(&vfio.group_lock);
  509. if (IS_ERR(group)) {
  510. ret = PTR_ERR(group);
  511. goto out_remove_device;
  512. }
  513. iommu_group_put(iommu_group);
  514. return group;
  515. out_remove_device:
  516. iommu_group_remove_device(dev);
  517. out_put_group:
  518. iommu_group_put(iommu_group);
  519. return ERR_PTR(ret);
  520. }
  521. static bool vfio_group_has_device(struct vfio_group *group, struct device *dev)
  522. {
  523. struct vfio_device *device;
  524. mutex_lock(&group->device_lock);
  525. list_for_each_entry(device, &group->device_list, group_next) {
  526. if (device->dev == dev) {
  527. mutex_unlock(&group->device_lock);
  528. return true;
  529. }
  530. }
  531. mutex_unlock(&group->device_lock);
  532. return false;
  533. }
  534. static struct vfio_group *vfio_group_find_or_alloc(struct device *dev)
  535. {
  536. struct iommu_group *iommu_group;
  537. struct vfio_group *group;
  538. iommu_group = iommu_group_get(dev);
  539. if (!iommu_group && vfio_noiommu) {
  540. /*
  541. * With noiommu enabled, create an IOMMU group for devices that
  542. * don't already have one, implying no IOMMU hardware/driver
  543. * exists. Taint the kernel because we're about to give a DMA
  544. * capable device to a user without IOMMU protection.
  545. */
  546. group = vfio_noiommu_group_alloc(dev, VFIO_NO_IOMMU);
  547. if (!IS_ERR(group)) {
  548. add_taint(TAINT_USER, LOCKDEP_STILL_OK);
  549. dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n");
  550. }
  551. return group;
  552. }
  553. if (!iommu_group)
  554. return ERR_PTR(-EINVAL);
  555. mutex_lock(&vfio.group_lock);
  556. group = vfio_group_find_from_iommu(iommu_group);
  557. if (group) {
  558. if (WARN_ON(vfio_group_has_device(group, dev)))
  559. group = ERR_PTR(-EINVAL);
  560. else
  561. refcount_inc(&group->drivers);
  562. } else {
  563. group = vfio_create_group(iommu_group, VFIO_IOMMU);
  564. }
  565. mutex_unlock(&vfio.group_lock);
  566. /* The vfio_group holds a reference to the iommu_group */
  567. iommu_group_put(iommu_group);
  568. return group;
  569. }
  570. int vfio_device_set_group(struct vfio_device *device,
  571. enum vfio_group_type type)
  572. {
  573. struct vfio_group *group;
  574. if (type == VFIO_IOMMU)
  575. group = vfio_group_find_or_alloc(device->dev);
  576. else
  577. group = vfio_noiommu_group_alloc(device->dev, type);
  578. if (IS_ERR(group))
  579. return PTR_ERR(group);
  580. /* Our reference on group is moved to the device */
  581. device->group = group;
  582. return 0;
  583. }
  584. void vfio_device_remove_group(struct vfio_device *device)
  585. {
  586. struct vfio_group *group = device->group;
  587. struct iommu_group *iommu_group;
  588. if (group->type == VFIO_NO_IOMMU || group->type == VFIO_EMULATED_IOMMU)
  589. iommu_group_remove_device(device->dev);
  590. /* Pairs with vfio_create_group() / vfio_group_get_from_iommu() */
  591. if (!refcount_dec_and_mutex_lock(&group->drivers, &vfio.group_lock))
  592. return;
  593. list_del(&group->vfio_next);
  594. /*
  595. * We could concurrently probe another driver in the group that might
  596. * race vfio_device_remove_group() with vfio_get_group(), so we have to
  597. * ensure that the sysfs is all cleaned up under lock otherwise the
  598. * cdev_device_add() will fail due to the name aready existing.
  599. */
  600. cdev_device_del(&group->cdev, &group->dev);
  601. mutex_lock(&group->group_lock);
  602. /*
  603. * These data structures all have paired operations that can only be
  604. * undone when the caller holds a live reference on the device. Since
  605. * all pairs must be undone these WARN_ON's indicate some caller did not
  606. * properly hold the group reference.
  607. */
  608. WARN_ON(!list_empty(&group->device_list));
  609. WARN_ON(group->notifier.head);
  610. /*
  611. * Revoke all users of group->iommu_group. At this point we know there
  612. * are no devices active because we are unplugging the last one. Setting
  613. * iommu_group to NULL blocks all new users.
  614. */
  615. if (group->container)
  616. vfio_group_detach_container(group);
  617. iommu_group = group->iommu_group;
  618. group->iommu_group = NULL;
  619. mutex_unlock(&group->group_lock);
  620. mutex_unlock(&vfio.group_lock);
  621. iommu_group_put(iommu_group);
  622. put_device(&group->dev);
  623. }
  624. void vfio_device_group_register(struct vfio_device *device)
  625. {
  626. mutex_lock(&device->group->device_lock);
  627. list_add(&device->group_next, &device->group->device_list);
  628. mutex_unlock(&device->group->device_lock);
  629. }
  630. void vfio_device_group_unregister(struct vfio_device *device)
  631. {
  632. mutex_lock(&device->group->device_lock);
  633. list_del(&device->group_next);
  634. mutex_unlock(&device->group->device_lock);
  635. }
  636. int vfio_device_group_use_iommu(struct vfio_device *device)
  637. {
  638. struct vfio_group *group = device->group;
  639. int ret = 0;
  640. lockdep_assert_held(&group->group_lock);
  641. if (WARN_ON(!group->container))
  642. return -EINVAL;
  643. ret = vfio_group_use_container(group);
  644. if (ret)
  645. return ret;
  646. vfio_device_container_register(device);
  647. return 0;
  648. }
  649. void vfio_device_group_unuse_iommu(struct vfio_device *device)
  650. {
  651. struct vfio_group *group = device->group;
  652. lockdep_assert_held(&group->group_lock);
  653. if (WARN_ON(!group->container))
  654. return;
  655. vfio_device_container_unregister(device);
  656. vfio_group_unuse_container(group);
  657. }
  658. bool vfio_device_has_container(struct vfio_device *device)
  659. {
  660. return device->group->container;
  661. }
  662. struct vfio_group *vfio_group_from_file(struct file *file)
  663. {
  664. struct vfio_group *group = file->private_data;
  665. if (file->f_op != &vfio_group_fops)
  666. return NULL;
  667. return group;
  668. }
  669. /**
  670. * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file
  671. * @file: VFIO group file
  672. *
  673. * The returned iommu_group is valid as long as a ref is held on the file. This
  674. * returns a reference on the group. This function is deprecated, only the SPAPR
  675. * path in kvm should call it.
  676. */
  677. struct iommu_group *vfio_file_iommu_group(struct file *file)
  678. {
  679. struct vfio_group *group = vfio_group_from_file(file);
  680. struct iommu_group *iommu_group = NULL;
  681. if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU))
  682. return NULL;
  683. if (!group)
  684. return NULL;
  685. mutex_lock(&group->group_lock);
  686. if (group->iommu_group) {
  687. iommu_group = group->iommu_group;
  688. iommu_group_ref_get(iommu_group);
  689. }
  690. mutex_unlock(&group->group_lock);
  691. return iommu_group;
  692. }
  693. EXPORT_SYMBOL_GPL(vfio_file_iommu_group);
  694. /**
  695. * vfio_file_is_group - True if the file is a vfio group file
  696. * @file: VFIO group file
  697. */
  698. bool vfio_file_is_group(struct file *file)
  699. {
  700. return vfio_group_from_file(file);
  701. }
  702. EXPORT_SYMBOL_GPL(vfio_file_is_group);
  703. bool vfio_group_enforced_coherent(struct vfio_group *group)
  704. {
  705. struct vfio_device *device;
  706. bool ret = true;
  707. /*
  708. * If the device does not have IOMMU_CAP_ENFORCE_CACHE_COHERENCY then
  709. * any domain later attached to it will also not support it. If the cap
  710. * is set then the iommu_domain eventually attached to the device/group
  711. * must use a domain with enforce_cache_coherency().
  712. */
  713. mutex_lock(&group->device_lock);
  714. list_for_each_entry(device, &group->device_list, group_next) {
  715. if (!device_iommu_capable(device->dev,
  716. IOMMU_CAP_ENFORCE_CACHE_COHERENCY)) {
  717. ret = false;
  718. break;
  719. }
  720. }
  721. mutex_unlock(&group->device_lock);
  722. return ret;
  723. }
  724. void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
  725. {
  726. spin_lock(&group->kvm_ref_lock);
  727. group->kvm = kvm;
  728. spin_unlock(&group->kvm_ref_lock);
  729. }
  730. /**
  731. * vfio_file_has_dev - True if the VFIO file is a handle for device
  732. * @file: VFIO file to check
  733. * @device: Device that must be part of the file
  734. *
  735. * Returns true if given file has permission to manipulate the given device.
  736. */
  737. bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
  738. {
  739. struct vfio_group *group = vfio_group_from_file(file);
  740. if (!group)
  741. return false;
  742. return group == device->group;
  743. }
  744. EXPORT_SYMBOL_GPL(vfio_file_has_dev);
  745. static char *vfio_devnode(const struct device *dev, umode_t *mode)
  746. {
  747. return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
  748. }
  749. int __init vfio_group_init(void)
  750. {
  751. int ret;
  752. ida_init(&vfio.group_ida);
  753. mutex_init(&vfio.group_lock);
  754. INIT_LIST_HEAD(&vfio.group_list);
  755. ret = vfio_container_init();
  756. if (ret)
  757. return ret;
  758. /* /dev/vfio/$GROUP */
  759. vfio.class = class_create("vfio");
  760. if (IS_ERR(vfio.class)) {
  761. ret = PTR_ERR(vfio.class);
  762. goto err_group_class;
  763. }
  764. vfio.class->devnode = vfio_devnode;
  765. ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK + 1, "vfio");
  766. if (ret)
  767. goto err_alloc_chrdev;
  768. return 0;
  769. err_alloc_chrdev:
  770. class_destroy(vfio.class);
  771. vfio.class = NULL;
  772. err_group_class:
  773. vfio_container_cleanup();
  774. return ret;
  775. }
  776. void vfio_group_cleanup(void)
  777. {
  778. WARN_ON(!list_empty(&vfio.group_list));
  779. ida_destroy(&vfio.group_ida);
  780. unregister_chrdev_region(vfio.group_devt, MINORMASK + 1);
  781. class_destroy(vfio.class);
  782. vfio.class = NULL;
  783. vfio_container_cleanup();
  784. }