vfio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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(sizeof(*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_arch_start_assignment(dev->kvm);
  142. kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
  143. kvm_vfio_update_coherency(dev);
  144. out_unlock:
  145. mutex_unlock(&kv->lock);
  146. out_fput:
  147. fput(filp);
  148. return ret;
  149. }
  150. static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
  151. {
  152. struct kvm_vfio *kv = dev->private;
  153. struct kvm_vfio_file *kvf;
  154. struct fd f;
  155. int ret;
  156. f = fdget(fd);
  157. if (!fd_file(f))
  158. return -EBADF;
  159. ret = -ENOENT;
  160. mutex_lock(&kv->lock);
  161. list_for_each_entry(kvf, &kv->file_list, node) {
  162. if (kvf->file != fd_file(f))
  163. continue;
  164. list_del(&kvf->node);
  165. kvm_arch_end_assignment(dev->kvm);
  166. #ifdef CONFIG_SPAPR_TCE_IOMMU
  167. kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
  168. #endif
  169. kvm_vfio_file_set_kvm(kvf->file, NULL);
  170. fput(kvf->file);
  171. kfree(kvf);
  172. ret = 0;
  173. break;
  174. }
  175. kvm_vfio_update_coherency(dev);
  176. mutex_unlock(&kv->lock);
  177. fdput(f);
  178. return ret;
  179. }
  180. #ifdef CONFIG_SPAPR_TCE_IOMMU
  181. static int kvm_vfio_file_set_spapr_tce(struct kvm_device *dev,
  182. void __user *arg)
  183. {
  184. struct kvm_vfio_spapr_tce param;
  185. struct kvm_vfio *kv = dev->private;
  186. struct kvm_vfio_file *kvf;
  187. struct fd f;
  188. int ret;
  189. if (copy_from_user(&param, arg, sizeof(struct kvm_vfio_spapr_tce)))
  190. return -EFAULT;
  191. f = fdget(param.groupfd);
  192. if (!fd_file(f))
  193. return -EBADF;
  194. ret = -ENOENT;
  195. mutex_lock(&kv->lock);
  196. list_for_each_entry(kvf, &kv->file_list, node) {
  197. if (kvf->file != fd_file(f))
  198. continue;
  199. if (!kvf->iommu_group) {
  200. kvf->iommu_group = kvm_vfio_file_iommu_group(kvf->file);
  201. if (WARN_ON_ONCE(!kvf->iommu_group)) {
  202. ret = -EIO;
  203. goto err_fdput;
  204. }
  205. }
  206. ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd,
  207. kvf->iommu_group);
  208. break;
  209. }
  210. err_fdput:
  211. mutex_unlock(&kv->lock);
  212. fdput(f);
  213. return ret;
  214. }
  215. #endif
  216. static int kvm_vfio_set_file(struct kvm_device *dev, long attr,
  217. void __user *arg)
  218. {
  219. int32_t __user *argp = arg;
  220. int32_t fd;
  221. switch (attr) {
  222. case KVM_DEV_VFIO_FILE_ADD:
  223. if (get_user(fd, argp))
  224. return -EFAULT;
  225. return kvm_vfio_file_add(dev, fd);
  226. case KVM_DEV_VFIO_FILE_DEL:
  227. if (get_user(fd, argp))
  228. return -EFAULT;
  229. return kvm_vfio_file_del(dev, fd);
  230. #ifdef CONFIG_SPAPR_TCE_IOMMU
  231. case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
  232. return kvm_vfio_file_set_spapr_tce(dev, arg);
  233. #endif
  234. }
  235. return -ENXIO;
  236. }
  237. static int kvm_vfio_set_attr(struct kvm_device *dev,
  238. struct kvm_device_attr *attr)
  239. {
  240. switch (attr->group) {
  241. case KVM_DEV_VFIO_FILE:
  242. return kvm_vfio_set_file(dev, attr->attr,
  243. u64_to_user_ptr(attr->addr));
  244. }
  245. return -ENXIO;
  246. }
  247. static int kvm_vfio_has_attr(struct kvm_device *dev,
  248. struct kvm_device_attr *attr)
  249. {
  250. switch (attr->group) {
  251. case KVM_DEV_VFIO_FILE:
  252. switch (attr->attr) {
  253. case KVM_DEV_VFIO_FILE_ADD:
  254. case KVM_DEV_VFIO_FILE_DEL:
  255. #ifdef CONFIG_SPAPR_TCE_IOMMU
  256. case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
  257. #endif
  258. return 0;
  259. }
  260. break;
  261. }
  262. return -ENXIO;
  263. }
  264. static void kvm_vfio_release(struct kvm_device *dev)
  265. {
  266. struct kvm_vfio *kv = dev->private;
  267. struct kvm_vfio_file *kvf, *tmp;
  268. list_for_each_entry_safe(kvf, tmp, &kv->file_list, node) {
  269. #ifdef CONFIG_SPAPR_TCE_IOMMU
  270. kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
  271. #endif
  272. kvm_vfio_file_set_kvm(kvf->file, NULL);
  273. fput(kvf->file);
  274. list_del(&kvf->node);
  275. kfree(kvf);
  276. kvm_arch_end_assignment(dev->kvm);
  277. }
  278. kvm_vfio_update_coherency(dev);
  279. kfree(kv);
  280. kfree(dev); /* alloc by kvm_ioctl_create_device, free by .release */
  281. }
  282. static int kvm_vfio_create(struct kvm_device *dev, u32 type);
  283. static struct kvm_device_ops kvm_vfio_ops = {
  284. .name = "kvm-vfio",
  285. .create = kvm_vfio_create,
  286. .release = kvm_vfio_release,
  287. .set_attr = kvm_vfio_set_attr,
  288. .has_attr = kvm_vfio_has_attr,
  289. };
  290. static int kvm_vfio_create(struct kvm_device *dev, u32 type)
  291. {
  292. struct kvm_device *tmp;
  293. struct kvm_vfio *kv;
  294. lockdep_assert_held(&dev->kvm->lock);
  295. /* Only one VFIO "device" per VM */
  296. list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
  297. if (tmp->ops == &kvm_vfio_ops)
  298. return -EBUSY;
  299. kv = kzalloc(sizeof(*kv), GFP_KERNEL_ACCOUNT);
  300. if (!kv)
  301. return -ENOMEM;
  302. INIT_LIST_HEAD(&kv->file_list);
  303. mutex_init(&kv->lock);
  304. dev->private = kv;
  305. return 0;
  306. }
  307. int kvm_vfio_ops_init(void)
  308. {
  309. return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
  310. }
  311. void kvm_vfio_ops_exit(void)
  312. {
  313. kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);
  314. }