vfio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <[email protected]>
  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_group(struct file *file)
  54. {
  55. bool (*fn)(struct file *file);
  56. bool ret;
  57. fn = symbol_get(vfio_file_is_group);
  58. if (!fn)
  59. return false;
  60. ret = fn(file);
  61. symbol_put(vfio_file_is_group);
  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. mutex_lock(&kv->lock);
  100. list_for_each_entry(kvf, &kv->file_list, node) {
  101. if (!kvm_vfio_file_enforced_coherent(kvf->file)) {
  102. noncoherent = true;
  103. break;
  104. }
  105. }
  106. if (noncoherent != kv->noncoherent) {
  107. kv->noncoherent = noncoherent;
  108. if (kv->noncoherent)
  109. kvm_arch_register_noncoherent_dma(dev->kvm);
  110. else
  111. kvm_arch_unregister_noncoherent_dma(dev->kvm);
  112. }
  113. mutex_unlock(&kv->lock);
  114. }
  115. static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
  116. {
  117. struct kvm_vfio *kv = dev->private;
  118. struct kvm_vfio_file *kvf;
  119. struct file *filp;
  120. int ret;
  121. filp = fget(fd);
  122. if (!filp)
  123. return -EBADF;
  124. /* Ensure the FD is a vfio group FD.*/
  125. if (!kvm_vfio_file_is_group(filp)) {
  126. ret = -EINVAL;
  127. goto err_fput;
  128. }
  129. mutex_lock(&kv->lock);
  130. list_for_each_entry(kvf, &kv->file_list, node) {
  131. if (kvf->file == filp) {
  132. ret = -EEXIST;
  133. goto err_unlock;
  134. }
  135. }
  136. kvf = kzalloc(sizeof(*kvf), GFP_KERNEL_ACCOUNT);
  137. if (!kvf) {
  138. ret = -ENOMEM;
  139. goto err_unlock;
  140. }
  141. kvf->file = filp;
  142. list_add_tail(&kvf->node, &kv->file_list);
  143. kvm_arch_start_assignment(dev->kvm);
  144. kvm_vfio_file_set_kvm(kvf->file, dev->kvm);
  145. mutex_unlock(&kv->lock);
  146. kvm_vfio_update_coherency(dev);
  147. return 0;
  148. err_unlock:
  149. mutex_unlock(&kv->lock);
  150. err_fput:
  151. fput(filp);
  152. return ret;
  153. }
  154. static int kvm_vfio_file_del(struct kvm_device *dev, unsigned int fd)
  155. {
  156. struct kvm_vfio *kv = dev->private;
  157. struct kvm_vfio_file *kvf;
  158. struct fd f;
  159. int ret;
  160. f = fdget(fd);
  161. if (!f.file)
  162. return -EBADF;
  163. ret = -ENOENT;
  164. mutex_lock(&kv->lock);
  165. list_for_each_entry(kvf, &kv->file_list, node) {
  166. if (kvf->file != f.file)
  167. continue;
  168. list_del(&kvf->node);
  169. kvm_arch_end_assignment(dev->kvm);
  170. #ifdef CONFIG_SPAPR_TCE_IOMMU
  171. kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
  172. #endif
  173. kvm_vfio_file_set_kvm(kvf->file, NULL);
  174. fput(kvf->file);
  175. kfree(kvf);
  176. ret = 0;
  177. break;
  178. }
  179. mutex_unlock(&kv->lock);
  180. fdput(f);
  181. kvm_vfio_update_coherency(dev);
  182. return ret;
  183. }
  184. #ifdef CONFIG_SPAPR_TCE_IOMMU
  185. static int kvm_vfio_file_set_spapr_tce(struct kvm_device *dev,
  186. void __user *arg)
  187. {
  188. struct kvm_vfio_spapr_tce param;
  189. struct kvm_vfio *kv = dev->private;
  190. struct kvm_vfio_file *kvf;
  191. struct fd f;
  192. int ret;
  193. if (copy_from_user(&param, arg, sizeof(struct kvm_vfio_spapr_tce)))
  194. return -EFAULT;
  195. f = fdget(param.groupfd);
  196. if (!f.file)
  197. return -EBADF;
  198. ret = -ENOENT;
  199. mutex_lock(&kv->lock);
  200. list_for_each_entry(kvf, &kv->file_list, node) {
  201. if (kvf->file != f.file)
  202. continue;
  203. if (!kvf->iommu_group) {
  204. kvf->iommu_group = kvm_vfio_file_iommu_group(kvf->file);
  205. if (WARN_ON_ONCE(!kvf->iommu_group)) {
  206. ret = -EIO;
  207. goto err_fdput;
  208. }
  209. }
  210. ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd,
  211. kvf->iommu_group);
  212. break;
  213. }
  214. err_fdput:
  215. mutex_unlock(&kv->lock);
  216. fdput(f);
  217. return ret;
  218. }
  219. #endif
  220. static int kvm_vfio_set_file(struct kvm_device *dev, long attr,
  221. void __user *arg)
  222. {
  223. int32_t __user *argp = arg;
  224. int32_t fd;
  225. switch (attr) {
  226. case KVM_DEV_VFIO_GROUP_ADD:
  227. if (get_user(fd, argp))
  228. return -EFAULT;
  229. return kvm_vfio_file_add(dev, fd);
  230. case KVM_DEV_VFIO_GROUP_DEL:
  231. if (get_user(fd, argp))
  232. return -EFAULT;
  233. return kvm_vfio_file_del(dev, fd);
  234. #ifdef CONFIG_SPAPR_TCE_IOMMU
  235. case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
  236. return kvm_vfio_file_set_spapr_tce(dev, arg);
  237. #endif
  238. }
  239. return -ENXIO;
  240. }
  241. static int kvm_vfio_set_attr(struct kvm_device *dev,
  242. struct kvm_device_attr *attr)
  243. {
  244. switch (attr->group) {
  245. case KVM_DEV_VFIO_GROUP:
  246. return kvm_vfio_set_file(dev, attr->attr,
  247. u64_to_user_ptr(attr->addr));
  248. }
  249. return -ENXIO;
  250. }
  251. static int kvm_vfio_has_attr(struct kvm_device *dev,
  252. struct kvm_device_attr *attr)
  253. {
  254. switch (attr->group) {
  255. case KVM_DEV_VFIO_GROUP:
  256. switch (attr->attr) {
  257. case KVM_DEV_VFIO_GROUP_ADD:
  258. case KVM_DEV_VFIO_GROUP_DEL:
  259. #ifdef CONFIG_SPAPR_TCE_IOMMU
  260. case KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE:
  261. #endif
  262. return 0;
  263. }
  264. break;
  265. }
  266. return -ENXIO;
  267. }
  268. static void kvm_vfio_release(struct kvm_device *dev)
  269. {
  270. struct kvm_vfio *kv = dev->private;
  271. struct kvm_vfio_file *kvf, *tmp;
  272. list_for_each_entry_safe(kvf, tmp, &kv->file_list, node) {
  273. #ifdef CONFIG_SPAPR_TCE_IOMMU
  274. kvm_spapr_tce_release_vfio_group(dev->kvm, kvf);
  275. #endif
  276. kvm_vfio_file_set_kvm(kvf->file, NULL);
  277. fput(kvf->file);
  278. list_del(&kvf->node);
  279. kfree(kvf);
  280. kvm_arch_end_assignment(dev->kvm);
  281. }
  282. kvm_vfio_update_coherency(dev);
  283. kfree(kv);
  284. kfree(dev); /* alloc by kvm_ioctl_create_device, free by .release */
  285. }
  286. static int kvm_vfio_create(struct kvm_device *dev, u32 type);
  287. static struct kvm_device_ops kvm_vfio_ops = {
  288. .name = "kvm-vfio",
  289. .create = kvm_vfio_create,
  290. .release = kvm_vfio_release,
  291. .set_attr = kvm_vfio_set_attr,
  292. .has_attr = kvm_vfio_has_attr,
  293. };
  294. static int kvm_vfio_create(struct kvm_device *dev, u32 type)
  295. {
  296. struct kvm_device *tmp;
  297. struct kvm_vfio *kv;
  298. /* Only one VFIO "device" per VM */
  299. list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
  300. if (tmp->ops == &kvm_vfio_ops)
  301. return -EBUSY;
  302. kv = kzalloc(sizeof(*kv), GFP_KERNEL_ACCOUNT);
  303. if (!kv)
  304. return -ENOMEM;
  305. INIT_LIST_HEAD(&kv->file_list);
  306. mutex_init(&kv->lock);
  307. dev->private = kv;
  308. return 0;
  309. }
  310. int kvm_vfio_ops_init(void)
  311. {
  312. return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
  313. }
  314. void kvm_vfio_ops_exit(void)
  315. {
  316. kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);
  317. }