device_cdev.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Intel Corporation.
  4. */
  5. #include <linux/vfio.h>
  6. #include <linux/iommufd.h>
  7. #include "vfio.h"
  8. static dev_t device_devt;
  9. void vfio_init_device_cdev(struct vfio_device *device)
  10. {
  11. device->device.devt = MKDEV(MAJOR(device_devt), device->index);
  12. cdev_init(&device->cdev, &vfio_device_fops);
  13. device->cdev.owner = THIS_MODULE;
  14. }
  15. /*
  16. * device access via the fd opened by this function is blocked until
  17. * .open_device() is called successfully during BIND_IOMMUFD.
  18. */
  19. int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
  20. {
  21. struct vfio_device *device = container_of(inode->i_cdev,
  22. struct vfio_device, cdev);
  23. struct vfio_device_file *df;
  24. int ret;
  25. /* Paired with the put in vfio_device_fops_release() */
  26. if (!vfio_device_try_get_registration(device))
  27. return -ENODEV;
  28. df = vfio_allocate_device_file(device);
  29. if (IS_ERR(df)) {
  30. ret = PTR_ERR(df);
  31. goto err_put_registration;
  32. }
  33. filep->private_data = df;
  34. /*
  35. * Use the pseudo fs inode on the device to link all mmaps
  36. * to the same address space, allowing us to unmap all vmas
  37. * associated to this device using unmap_mapping_range().
  38. */
  39. filep->f_mapping = device->inode->i_mapping;
  40. return 0;
  41. err_put_registration:
  42. vfio_device_put_registration(device);
  43. return ret;
  44. }
  45. static void vfio_df_get_kvm_safe(struct vfio_device_file *df)
  46. {
  47. spin_lock(&df->kvm_ref_lock);
  48. vfio_device_get_kvm_safe(df->device, df->kvm);
  49. spin_unlock(&df->kvm_ref_lock);
  50. }
  51. static int vfio_df_check_token(struct vfio_device *device,
  52. const struct vfio_device_bind_iommufd *bind)
  53. {
  54. uuid_t uuid;
  55. if (!device->ops->match_token_uuid) {
  56. if (bind->flags & VFIO_DEVICE_BIND_FLAG_TOKEN)
  57. return -EINVAL;
  58. return 0;
  59. }
  60. if (!(bind->flags & VFIO_DEVICE_BIND_FLAG_TOKEN))
  61. return device->ops->match_token_uuid(device, NULL);
  62. if (copy_from_user(&uuid, u64_to_user_ptr(bind->token_uuid_ptr),
  63. sizeof(uuid)))
  64. return -EFAULT;
  65. return device->ops->match_token_uuid(device, &uuid);
  66. }
  67. long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
  68. struct vfio_device_bind_iommufd __user *arg)
  69. {
  70. const u32 VALID_FLAGS = VFIO_DEVICE_BIND_FLAG_TOKEN;
  71. struct vfio_device *device = df->device;
  72. struct vfio_device_bind_iommufd bind;
  73. unsigned long minsz;
  74. u32 user_size;
  75. int ret;
  76. static_assert(__same_type(arg->out_devid, df->devid));
  77. minsz = offsetofend(struct vfio_device_bind_iommufd, out_devid);
  78. ret = get_user(user_size, &arg->argsz);
  79. if (ret)
  80. return ret;
  81. if (user_size < minsz)
  82. return -EINVAL;
  83. ret = copy_struct_from_user(&bind, sizeof(bind), arg, user_size);
  84. if (ret)
  85. return ret;
  86. if (bind.iommufd < 0 || bind.flags & ~VALID_FLAGS)
  87. return -EINVAL;
  88. /* BIND_IOMMUFD only allowed for cdev fds */
  89. if (df->group)
  90. return -EINVAL;
  91. ret = vfio_device_block_group(device);
  92. if (ret)
  93. return ret;
  94. mutex_lock(&device->dev_set->lock);
  95. /* one device cannot be bound twice */
  96. if (df->access_granted) {
  97. ret = -EINVAL;
  98. goto out_unlock;
  99. }
  100. ret = vfio_df_check_token(device, &bind);
  101. if (ret)
  102. goto out_unlock;
  103. df->iommufd = iommufd_ctx_from_fd(bind.iommufd);
  104. if (IS_ERR(df->iommufd)) {
  105. ret = PTR_ERR(df->iommufd);
  106. df->iommufd = NULL;
  107. goto out_unlock;
  108. }
  109. /*
  110. * Before the device open, get the KVM pointer currently
  111. * associated with the device file (if there is) and obtain
  112. * a reference. This reference is held until device closed.
  113. * Save the pointer in the device for use by drivers.
  114. */
  115. vfio_df_get_kvm_safe(df);
  116. ret = vfio_df_open(df);
  117. if (ret)
  118. goto out_put_kvm;
  119. ret = copy_to_user(&arg->out_devid, &df->devid,
  120. sizeof(df->devid)) ? -EFAULT : 0;
  121. if (ret)
  122. goto out_close_device;
  123. device->cdev_opened = true;
  124. /*
  125. * Paired with smp_load_acquire() in vfio_device_fops::ioctl/
  126. * read/write/mmap
  127. */
  128. smp_store_release(&df->access_granted, true);
  129. mutex_unlock(&device->dev_set->lock);
  130. return 0;
  131. out_close_device:
  132. vfio_df_close(df);
  133. out_put_kvm:
  134. vfio_device_put_kvm(device);
  135. iommufd_ctx_put(df->iommufd);
  136. df->iommufd = NULL;
  137. out_unlock:
  138. mutex_unlock(&device->dev_set->lock);
  139. vfio_device_unblock_group(device);
  140. return ret;
  141. }
  142. void vfio_df_unbind_iommufd(struct vfio_device_file *df)
  143. {
  144. struct vfio_device *device = df->device;
  145. /*
  146. * In the time of close, there is no contention with another one
  147. * changing this flag. So read df->access_granted without lock
  148. * and no smp_load_acquire() is ok.
  149. */
  150. if (!df->access_granted)
  151. return;
  152. mutex_lock(&device->dev_set->lock);
  153. vfio_df_close(df);
  154. vfio_device_put_kvm(device);
  155. iommufd_ctx_put(df->iommufd);
  156. device->cdev_opened = false;
  157. mutex_unlock(&device->dev_set->lock);
  158. vfio_device_unblock_group(device);
  159. }
  160. int vfio_df_ioctl_attach_pt(struct vfio_device_file *df,
  161. struct vfio_device_attach_iommufd_pt __user *arg)
  162. {
  163. struct vfio_device_attach_iommufd_pt attach;
  164. struct vfio_device *device = df->device;
  165. unsigned long minsz, xend = 0;
  166. int ret;
  167. minsz = offsetofend(struct vfio_device_attach_iommufd_pt, pt_id);
  168. if (copy_from_user(&attach, arg, minsz))
  169. return -EFAULT;
  170. if (attach.argsz < minsz)
  171. return -EINVAL;
  172. if (attach.flags & ~VFIO_DEVICE_ATTACH_PASID)
  173. return -EINVAL;
  174. if (attach.flags & VFIO_DEVICE_ATTACH_PASID) {
  175. if (!device->ops->pasid_attach_ioas)
  176. return -EOPNOTSUPP;
  177. xend = offsetofend(struct vfio_device_attach_iommufd_pt, pasid);
  178. }
  179. if (xend) {
  180. if (attach.argsz < xend)
  181. return -EINVAL;
  182. if (copy_from_user((void *)&attach + minsz,
  183. (void __user *)arg + minsz, xend - minsz))
  184. return -EFAULT;
  185. }
  186. mutex_lock(&device->dev_set->lock);
  187. if (attach.flags & VFIO_DEVICE_ATTACH_PASID)
  188. ret = device->ops->pasid_attach_ioas(device,
  189. attach.pasid,
  190. &attach.pt_id);
  191. else
  192. ret = device->ops->attach_ioas(device, &attach.pt_id);
  193. if (ret)
  194. goto out_unlock;
  195. if (copy_to_user(&arg->pt_id, &attach.pt_id, sizeof(attach.pt_id))) {
  196. ret = -EFAULT;
  197. goto out_detach;
  198. }
  199. mutex_unlock(&device->dev_set->lock);
  200. return 0;
  201. out_detach:
  202. device->ops->detach_ioas(device);
  203. out_unlock:
  204. mutex_unlock(&device->dev_set->lock);
  205. return ret;
  206. }
  207. int vfio_df_ioctl_detach_pt(struct vfio_device_file *df,
  208. struct vfio_device_detach_iommufd_pt __user *arg)
  209. {
  210. struct vfio_device_detach_iommufd_pt detach;
  211. struct vfio_device *device = df->device;
  212. unsigned long minsz, xend = 0;
  213. minsz = offsetofend(struct vfio_device_detach_iommufd_pt, flags);
  214. if (copy_from_user(&detach, arg, minsz))
  215. return -EFAULT;
  216. if (detach.argsz < minsz)
  217. return -EINVAL;
  218. if (detach.flags & ~VFIO_DEVICE_DETACH_PASID)
  219. return -EINVAL;
  220. if (detach.flags & VFIO_DEVICE_DETACH_PASID) {
  221. if (!device->ops->pasid_detach_ioas)
  222. return -EOPNOTSUPP;
  223. xend = offsetofend(struct vfio_device_detach_iommufd_pt, pasid);
  224. }
  225. if (xend) {
  226. if (detach.argsz < xend)
  227. return -EINVAL;
  228. if (copy_from_user((void *)&detach + minsz,
  229. (void __user *)arg + minsz, xend - minsz))
  230. return -EFAULT;
  231. }
  232. mutex_lock(&device->dev_set->lock);
  233. if (detach.flags & VFIO_DEVICE_DETACH_PASID)
  234. device->ops->pasid_detach_ioas(device, detach.pasid);
  235. else
  236. device->ops->detach_ioas(device);
  237. mutex_unlock(&device->dev_set->lock);
  238. return 0;
  239. }
  240. static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
  241. {
  242. return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
  243. }
  244. int vfio_cdev_init(struct class *device_class)
  245. {
  246. device_class->devnode = vfio_device_devnode;
  247. return alloc_chrdev_region(&device_devt, 0,
  248. MINORMASK + 1, "vfio-dev");
  249. }
  250. void vfio_cdev_cleanup(void)
  251. {
  252. unregister_chrdev_region(device_devt, MINORMASK + 1);
  253. }