vfio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * VFIO-KVM bridge pseudo device
  3. *
  4. * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
  5. * Author: Alex Williamson <alex.williamson@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/file.h>
  13. #include <linux/kvm_host.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/vfio.h>
  20. #include "vfio.h"
  21. struct kvm_vfio_group {
  22. struct list_head node;
  23. struct vfio_group *vfio_group;
  24. };
  25. struct kvm_vfio {
  26. struct list_head group_list;
  27. struct mutex lock;
  28. bool noncoherent;
  29. };
  30. static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep)
  31. {
  32. struct vfio_group *vfio_group;
  33. struct vfio_group *(*fn)(struct file *);
  34. fn = symbol_get(vfio_group_get_external_user);
  35. if (!fn)
  36. return ERR_PTR(-EINVAL);
  37. vfio_group = fn(filep);
  38. symbol_put(vfio_group_get_external_user);
  39. return vfio_group;
  40. }
  41. static bool kvm_vfio_external_group_match_file(struct vfio_group *group,
  42. struct file *filep)
  43. {
  44. bool ret, (*fn)(struct vfio_group *, struct file *);
  45. fn = symbol_get(vfio_external_group_match_file);
  46. if (!fn)
  47. return false;
  48. ret = fn(group, filep);
  49. symbol_put(vfio_external_group_match_file);
  50. return ret;
  51. }
  52. static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group)
  53. {
  54. void (*fn)(struct vfio_group *);
  55. fn = symbol_get(vfio_group_put_external_user);
  56. if (!fn)
  57. return;
  58. fn(vfio_group);
  59. symbol_put(vfio_group_put_external_user);
  60. }
  61. static bool kvm_vfio_group_is_coherent(struct vfio_group *vfio_group)
  62. {
  63. long (*fn)(struct vfio_group *, unsigned long);
  64. long ret;
  65. fn = symbol_get(vfio_external_check_extension);
  66. if (!fn)
  67. return false;
  68. ret = fn(vfio_group, VFIO_DMA_CC_IOMMU);
  69. symbol_put(vfio_external_check_extension);
  70. return ret > 0;
  71. }
  72. /*
  73. * Groups can use the same or different IOMMU domains. If the same then
  74. * adding a new group may change the coherency of groups we've previously
  75. * been told about. We don't want to care about any of that so we retest
  76. * each group and bail as soon as we find one that's noncoherent. This
  77. * means we only ever [un]register_noncoherent_dma once for the whole device.
  78. */
  79. static void kvm_vfio_update_coherency(struct kvm_device *dev)
  80. {
  81. struct kvm_vfio *kv = dev->private;
  82. bool noncoherent = false;
  83. struct kvm_vfio_group *kvg;
  84. mutex_lock(&kv->lock);
  85. list_for_each_entry(kvg, &kv->group_list, node) {
  86. if (!kvm_vfio_group_is_coherent(kvg->vfio_group)) {
  87. noncoherent = true;
  88. break;
  89. }
  90. }
  91. if (noncoherent != kv->noncoherent) {
  92. kv->noncoherent = noncoherent;
  93. if (kv->noncoherent)
  94. kvm_arch_register_noncoherent_dma(dev->kvm);
  95. else
  96. kvm_arch_unregister_noncoherent_dma(dev->kvm);
  97. }
  98. mutex_unlock(&kv->lock);
  99. }
  100. static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg)
  101. {
  102. struct kvm_vfio *kv = dev->private;
  103. struct vfio_group *vfio_group;
  104. struct kvm_vfio_group *kvg;
  105. int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
  106. struct fd f;
  107. int32_t fd;
  108. int ret;
  109. switch (attr) {
  110. case KVM_DEV_VFIO_GROUP_ADD:
  111. if (get_user(fd, argp))
  112. return -EFAULT;
  113. f = fdget(fd);
  114. if (!f.file)
  115. return -EBADF;
  116. vfio_group = kvm_vfio_group_get_external_user(f.file);
  117. fdput(f);
  118. if (IS_ERR(vfio_group))
  119. return PTR_ERR(vfio_group);
  120. mutex_lock(&kv->lock);
  121. list_for_each_entry(kvg, &kv->group_list, node) {
  122. if (kvg->vfio_group == vfio_group) {
  123. mutex_unlock(&kv->lock);
  124. kvm_vfio_group_put_external_user(vfio_group);
  125. return -EEXIST;
  126. }
  127. }
  128. kvg = kzalloc(sizeof(*kvg), GFP_KERNEL);
  129. if (!kvg) {
  130. mutex_unlock(&kv->lock);
  131. kvm_vfio_group_put_external_user(vfio_group);
  132. return -ENOMEM;
  133. }
  134. list_add_tail(&kvg->node, &kv->group_list);
  135. kvg->vfio_group = vfio_group;
  136. kvm_arch_start_assignment(dev->kvm);
  137. mutex_unlock(&kv->lock);
  138. kvm_vfio_update_coherency(dev);
  139. return 0;
  140. case KVM_DEV_VFIO_GROUP_DEL:
  141. if (get_user(fd, argp))
  142. return -EFAULT;
  143. f = fdget(fd);
  144. if (!f.file)
  145. return -EBADF;
  146. ret = -ENOENT;
  147. mutex_lock(&kv->lock);
  148. list_for_each_entry(kvg, &kv->group_list, node) {
  149. if (!kvm_vfio_external_group_match_file(kvg->vfio_group,
  150. f.file))
  151. continue;
  152. list_del(&kvg->node);
  153. kvm_vfio_group_put_external_user(kvg->vfio_group);
  154. kfree(kvg);
  155. ret = 0;
  156. break;
  157. }
  158. kvm_arch_end_assignment(dev->kvm);
  159. mutex_unlock(&kv->lock);
  160. fdput(f);
  161. kvm_vfio_update_coherency(dev);
  162. return ret;
  163. }
  164. return -ENXIO;
  165. }
  166. static int kvm_vfio_set_attr(struct kvm_device *dev,
  167. struct kvm_device_attr *attr)
  168. {
  169. switch (attr->group) {
  170. case KVM_DEV_VFIO_GROUP:
  171. return kvm_vfio_set_group(dev, attr->attr, attr->addr);
  172. }
  173. return -ENXIO;
  174. }
  175. static int kvm_vfio_has_attr(struct kvm_device *dev,
  176. struct kvm_device_attr *attr)
  177. {
  178. switch (attr->group) {
  179. case KVM_DEV_VFIO_GROUP:
  180. switch (attr->attr) {
  181. case KVM_DEV_VFIO_GROUP_ADD:
  182. case KVM_DEV_VFIO_GROUP_DEL:
  183. return 0;
  184. }
  185. break;
  186. }
  187. return -ENXIO;
  188. }
  189. static void kvm_vfio_destroy(struct kvm_device *dev)
  190. {
  191. struct kvm_vfio *kv = dev->private;
  192. struct kvm_vfio_group *kvg, *tmp;
  193. list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) {
  194. kvm_vfio_group_put_external_user(kvg->vfio_group);
  195. list_del(&kvg->node);
  196. kfree(kvg);
  197. kvm_arch_end_assignment(dev->kvm);
  198. }
  199. kvm_vfio_update_coherency(dev);
  200. kfree(kv);
  201. kfree(dev); /* alloc by kvm_ioctl_create_device, free by .destroy */
  202. }
  203. static int kvm_vfio_create(struct kvm_device *dev, u32 type);
  204. static struct kvm_device_ops kvm_vfio_ops = {
  205. .name = "kvm-vfio",
  206. .create = kvm_vfio_create,
  207. .destroy = kvm_vfio_destroy,
  208. .set_attr = kvm_vfio_set_attr,
  209. .has_attr = kvm_vfio_has_attr,
  210. };
  211. static int kvm_vfio_create(struct kvm_device *dev, u32 type)
  212. {
  213. struct kvm_device *tmp;
  214. struct kvm_vfio *kv;
  215. /* Only one VFIO "device" per VM */
  216. list_for_each_entry(tmp, &dev->kvm->devices, vm_node)
  217. if (tmp->ops == &kvm_vfio_ops)
  218. return -EBUSY;
  219. kv = kzalloc(sizeof(*kv), GFP_KERNEL);
  220. if (!kv)
  221. return -ENOMEM;
  222. INIT_LIST_HEAD(&kv->group_list);
  223. mutex_init(&kv->lock);
  224. dev->private = kv;
  225. return 0;
  226. }
  227. int kvm_vfio_ops_init(void)
  228. {
  229. return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO);
  230. }
  231. void kvm_vfio_ops_exit(void)
  232. {
  233. kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO);
  234. }