vfio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VFIO-KVM bridge pseudo device
  4. *
  5. * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
  6. * Author: Alex Williamson <alex.williamson@redhat.com>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/file.h>
  10. #include <linux/kvm_host.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/slab.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/vfio.h>
  17. #include "vfio.h"
  18. #ifdef CONFIG_SPAPR_TCE_IOMMU
  19. #include <asm/kvm_ppc.h>
  20. #endif
  21. struct kvm_vfio_file {
  22. struct list_head node;
  23. struct file *file;
  24. #ifdef CONFIG_SPAPR_TCE_IOMMU
  25. struct iommu_group *iommu_group;
  26. #endif
  27. };
  28. struct kvm_vfio {
  29. struct list_head file_list;
  30. struct mutex lock;
  31. bool noncoherent;
  32. };
  33. static void kvm_vfio_file_set_kvm(struct file *file, struct kvm *kvm)
  34. {
  35. void (*fn)(struct file *file, struct kvm *kvm);
  36. fn = symbol_get(vfio_file_set_kvm);
  37. if (!fn)
  38. return;
  39. fn(file, kvm);
  40. symbol_put(vfio_file_set_kvm);
  41. }
  42. static bool kvm_vfio_file_enforced_coherent(struct file *file)
  43. {
  44. bool (*fn)(struct file *file);
  45. bool ret;
  46. fn = symbol_get(vfio_file_enforced_coherent);
  47. if (!fn)
  48. return false;
  49. ret = fn(file);
  50. symbol_put(vfio_file_enforced_coherent);
  51. return ret;
  52. }
  53. static bool kvm_vfio_file_is_valid(struct file *file)
  54. {
  55. bool (*fn)(struct file *file);
  56. bool ret;
  57. fn = symbol_get(vfio_file_is_valid);
  58. if (!fn)
  59. return false;
  60. ret = fn(file);
  61. symbol_put(vfio_file_is_valid);
  62. return ret;
  63. }
  64. #ifdef CONFIG_SPAPR_TCE_IOMMU
  65. static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file)
  66. {
  67. struct iommu_group *(*fn)(struct file *file);
  68. struct iommu_group *ret;
  69. fn = symbol_get(vfio_file_iommu_group);
  70. if (!fn)
  71. return NULL;
  72. ret = fn(file);
  73. symbol_put(vfio_file_iommu_group);
  74. return ret;
  75. }
  76. static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm,
  77. struct kvm_vfio_file *kvf)
  78. {
  79. if (WARN_ON_ONCE(!kvf->iommu_group))
  80. return;
  81. kvm_spapr_tce_release_iommu_group(kvm, kvf->iommu_group);
  82. iommu_group_put(kvf->iommu_group);
  83. kvf->iommu_group = NULL;
  84. }
  85. #endif
  86. /*
  87. * Groups/devices can use the same or different IOMMU domains. If the same
  88. * then adding a new group/device may change the coherency of groups/devices
  89. * we've previously been told about. We don't want to care about any of
  90. * that so we retest each group/device and bail as soon as we find one that's
  91. * noncoherent. This means we only ever [un]register_noncoherent_dma once
  92. * for the whole device.
  93. */
  94. static void kvm_vfio_update_coherency(struct kvm_device *dev)
  95. {
  96. struct kvm_vfio *kv = dev->private;
  97. bool noncoherent = false;
  98. struct kvm_vfio_file *kvf;
  99. list_for_each_entry(kvf, &kv->file_list, node) {
  100. if (!kvm_vfio_file_enforced_coherent(kvf->file)) {
  101. noncoherent = true;
  102. break;
  103. }
  104. }
  105. if (noncoherent != kv->noncoherent) {
  106. kv->noncoherent = noncoherent;
  107. if (kv->noncoherent)
  108. kvm_arch_register_noncoherent_dma(dev->kvm);
  109. else
  110. kvm_arch_unregister_noncoherent_dma(dev->kvm);
  111. }
  112. }
  113. static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
  114. {
  115. struct kvm_vfio *kv = dev->private;
  116. struct kvm_vfio_file *kvf;
  117. struct file *filp;
  118. int ret = 0;
  119. filp = fget(fd);
  120. if (!filp)
  121. return -EBADF;
  122. /* Ensure the FD is a vfio FD. */
  123. if (!kvm_vfio_file_is_valid(filp)) {
  124. ret = -EINVAL;
  125. goto out_fput;
  126. }
  127. mutex_lock(&kv->lock);
  128. list_for_each_entry(kvf, &kv->file_list, node) {
  129. if (kvf->file == filp) {
  130. ret = -EEXIST;
  131. goto out_unlock;
  132. }
  133. }
  134. kvf = kzalloc_obj(*kvf, GFP_KERNEL_ACCOUNT);
  135. if (!kvf) {
  136. ret = -ENOMEM;
  137. goto out_unlock;
  138. }
  139. kvf->file = get_file(filp);
  140. list_add_tail(&kvf->node, &kv->file_list);
  141. kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
  142. kvm_vfio_update_coherency(dev);
  143. out_unlock:
  144. mutex_unlock(&kv->lock);
  145. out_fput:
  146. fput(filp);
  147. return ret;
  148. }
  149. static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
  150. {
  151. struct kvm_vfio *kv = dev->private;
  152. struct kvm_vfio_file *kvf;
  153. CLASS(fd, f)(fd);
  154. int ret;
  155. if (fd_empty(f))
  156. return -EBADF;
  157. ret = -ENOENT;
  158. mutex_lock(&kv->lock);
  159. list_for_each_entry(kvf, &kv->file_list, node) {
  160. if (kvf->file != fd_file(f))
  161. continue;
  162. list_del(&kvf->node);
  163. #ifdef CONFIG_SPAPR_TCE_IOMMU
  164. kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
  165. #endif
  166. kvm_vfio_file_set_kvm(kvf->file, NULL);
  167. fput(kvf->file);
  168. kfree(kvf);
  169. ret = 0;
  170. break;
  171. }
  172. kvm_vfio_update_coherency(dev);
  173. mutex_unlock(&kv->lock);
  174. return ret;
  175. }
  176. #ifdef CONFIG_SPAPR_TCE_IOMMU
  177. static int kvm_vfio_file_set_spapr_tce(struct kvm_device *dev,
  178. void __user *arg)
  179. {
  180. struct kvm_vfio_spapr_tce param;
  181. struct kvm_vfio *kv = dev->private;
  182. struct kvm_vfio_file *kvf;
  183. int ret;
  184. if (copy_from_user(&param, arg, sizeof(struct kvm_vfio_spapr_tce)))
  185. return -EFAULT;
  186. CLASS(fd, f)(param.groupfd);
  187. if (fd_empty(f))
  188. return -EBADF;
  189. ret = -ENOENT;
  190. mutex_lock(&kv->lock);
  191. list_for_each_entry(kvf, &kv->file_list, node) {
  192. if (kvf->file != fd_file(f))
  193. continue;
  194. if (!kvf->iommu_group) {
  195. kvf->iommu_group = kvm_vfio_file_iommu_group(kvf->file);
  196. if (WARN_ON_ONCE(!kvf->iommu_group)) {
  197. ret = -EIO;
  198. goto err_fdput;
  199. }
  200. }
  201. ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd,
  202. kvf->iommu_group);
  203. break;
  204. }
  205. err_fdput:
  206. mutex_unlock(&kv->lock);
  207. return ret;
  208. }
  209. #endif
  210. static int kvm_vfio_set_file(struct kvm_device *dev, long attr,
  211. void __user *arg)
  212. {
  213. int32_t __user *argp = arg;
  214. int32_t fd;
  215. switch (attr) {
  216. case KVM_DEV_VFIO_FILE_ADD:
  217. if (get_user(fd, argp))
  218. return -EFAULT;
  219. return kvm_vfio_file_add(dev, fd);
  220. case KVM_DEV_VFIO_FILE_DEL:
  221. if (get_user(fd, argp))
  222. return -EFAULT;
  223. return kvm_vfio_file_del(dev, fd);
  224. #ifdef CONFIG_SPAPR_TCE_IOMMU
  225. case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
  226. return kvm_vfio_file_set_spapr_tce(dev, arg);
  227. #endif
  228. }
  229. return -ENXIO;
  230. }
  231. static int kvm_vfio_set_attr(struct kvm_device *dev,
  232. struct kvm_device_attr *attr)
  233. {
  234. switch (attr->group) {
  235. case KVM_DEV_VFIO_FILE:
  236. return kvm_vfio_set_file(dev, attr->attr,
  237. u64_to_user_ptr(attr->addr));
  238. }
  239. return -ENXIO;
  240. }
  241. static int kvm_vfio_has_attr(struct kvm_device *dev,
  242. struct kvm_device_attr *attr)
  243. {
  244. switch (attr->group) {
  245. case KVM_DEV_VFIO_FILE:
  246. switch (attr->attr) {
  247. case KVM_DEV_VFIO_FILE_ADD:
  248. case KVM_DEV_VFIO_FILE_DEL:
  249. #ifdef CONFIG_SPAPR_TCE_IOMMU
  250. case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
  251. #endif
  252. return 0;
  253. }
  254. break;
  255. }
  256. return -ENXIO;
  257. }
  258. static void kvm_vfio_release(struct kvm_device *dev)
  259. {
  260. struct kvm_vfio *kv = dev->private;
  261. struct kvm_vfio_file *kvf, *tmp;
  262. list_for_each_entry_safe(kvf, tmp, &kv->file_list, node) {
  263. #ifdef CONFIG_SPAPR_TCE_IOMMU
  264. kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
  265. #endif
  266. kvm_vfio_file_set_kvm(kvf->file, NULL);
  267. fput(kvf->file);
  268. list_del(&kvf->node);
  269. kfree(kvf);
  270. }
  271. kvm_vfio_update_coherency(dev);
  272. kfree(kv);
  273. kfree(dev); /* alloc by kvm_ioctl_create_device, free by .release */
  274. }
  275. static int kvm_vfio_create(struct kvm_device *dev, u32 type);
  276. static const struct kvm_device_ops kvm_vfio_ops = {
  277. .name = "kvm-vfio",
  278. .create = kvm_vfio_create,
  279. .release = kvm_vfio_release,
  280. .set_attr = kvm_vfio_set_attr,
  281. .has_attr = kvm_vfio_has_attr,
  282. };
  283. static int kvm_vfio_create(struct kvm_device *dev, u32 type)
  284. {
  285. struct kvm_device *tmp;
  286. struct kvm_vfio *kv;
  287. lockdep_assert_held(&dev->kvm->lock);
  288. /* Only one VFIO "device" per VM */
  289. list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
  290. if (tmp->ops == &kvm_vfio_ops)
  291. return -EBUSY;
  292. kv = kzalloc_obj(*kv, GFP_KERNEL_ACCOUNT);
  293. if (!kv)
  294. return -ENOMEM;
  295. INIT_LIST_HEAD(&kv->file_list);
  296. mutex_init(&kv->lock);
  297. dev->private = kv;
  298. return 0;
  299. }
  300. int kvm_vfio_ops_init(void)
  301. {
  302. return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
  303. }
  304. void kvm_vfio_ops_exit(void)
  305. {
  306. kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);
  307. }