group.c 23 KB

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