Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 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 | |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/file.h> |
| 14 | #include <linux/kvm_host.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/uaccess.h> |
| 20 | #include <linux/vfio.h> |
Paolo Bonzini | 3c3c29f | 2014-09-24 13:02:46 +0200 | [diff] [blame] | 21 | #include "vfio.h" |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 22 | |
| 23 | struct kvm_vfio_group { |
| 24 | struct list_head node; |
| 25 | struct vfio_group *vfio_group; |
| 26 | }; |
| 27 | |
| 28 | struct kvm_vfio { |
| 29 | struct list_head group_list; |
| 30 | struct mutex lock; |
Alex Williamson | e0f0bbc | 2013-10-30 11:02:30 -0600 | [diff] [blame] | 31 | bool noncoherent; |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static struct vfio_group *kvm_vfio_group_get_external_user(struct file *filep) |
| 35 | { |
| 36 | struct vfio_group *vfio_group; |
| 37 | struct vfio_group *(*fn)(struct file *); |
| 38 | |
| 39 | fn = symbol_get(vfio_group_get_external_user); |
| 40 | if (!fn) |
| 41 | return ERR_PTR(-EINVAL); |
| 42 | |
| 43 | vfio_group = fn(filep); |
| 44 | |
| 45 | symbol_put(vfio_group_get_external_user); |
| 46 | |
| 47 | return vfio_group; |
| 48 | } |
| 49 | |
| 50 | static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group) |
| 51 | { |
| 52 | void (*fn)(struct vfio_group *); |
| 53 | |
| 54 | fn = symbol_get(vfio_group_put_external_user); |
| 55 | if (!fn) |
| 56 | return; |
| 57 | |
| 58 | fn(vfio_group); |
| 59 | |
| 60 | symbol_put(vfio_group_put_external_user); |
| 61 | } |
| 62 | |
Jike Song | 2fc1bec | 2016-12-01 13:20:07 +0800 | [diff] [blame] | 63 | static void kvm_vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) |
| 64 | { |
| 65 | void (*fn)(struct vfio_group *, struct kvm *); |
| 66 | |
| 67 | fn = symbol_get(vfio_group_set_kvm); |
| 68 | if (!fn) |
| 69 | return; |
| 70 | |
| 71 | fn(group, kvm); |
| 72 | |
| 73 | symbol_put(vfio_group_set_kvm); |
| 74 | } |
| 75 | |
Alex Williamson | 9d830d4 | 2014-02-26 11:38:40 -0700 | [diff] [blame] | 76 | static bool kvm_vfio_group_is_coherent(struct vfio_group *vfio_group) |
| 77 | { |
| 78 | long (*fn)(struct vfio_group *, unsigned long); |
| 79 | long ret; |
| 80 | |
| 81 | fn = symbol_get(vfio_external_check_extension); |
| 82 | if (!fn) |
| 83 | return false; |
| 84 | |
| 85 | ret = fn(vfio_group, VFIO_DMA_CC_IOMMU); |
| 86 | |
| 87 | symbol_put(vfio_external_check_extension); |
| 88 | |
| 89 | return ret > 0; |
| 90 | } |
| 91 | |
Alex Williamson | e0f0bbc | 2013-10-30 11:02:30 -0600 | [diff] [blame] | 92 | /* |
| 93 | * Groups can use the same or different IOMMU domains. If the same then |
| 94 | * adding a new group may change the coherency of groups we've previously |
| 95 | * been told about. We don't want to care about any of that so we retest |
| 96 | * each group and bail as soon as we find one that's noncoherent. This |
| 97 | * means we only ever [un]register_noncoherent_dma once for the whole device. |
| 98 | */ |
| 99 | static void kvm_vfio_update_coherency(struct kvm_device *dev) |
| 100 | { |
| 101 | struct kvm_vfio *kv = dev->private; |
| 102 | bool noncoherent = false; |
| 103 | struct kvm_vfio_group *kvg; |
| 104 | |
| 105 | mutex_lock(&kv->lock); |
| 106 | |
| 107 | list_for_each_entry(kvg, &kv->group_list, node) { |
Alex Williamson | 9d830d4 | 2014-02-26 11:38:40 -0700 | [diff] [blame] | 108 | if (!kvm_vfio_group_is_coherent(kvg->vfio_group)) { |
| 109 | noncoherent = true; |
| 110 | break; |
| 111 | } |
Alex Williamson | e0f0bbc | 2013-10-30 11:02:30 -0600 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | if (noncoherent != kv->noncoherent) { |
| 115 | kv->noncoherent = noncoherent; |
| 116 | |
| 117 | if (kv->noncoherent) |
| 118 | kvm_arch_register_noncoherent_dma(dev->kvm); |
| 119 | else |
| 120 | kvm_arch_unregister_noncoherent_dma(dev->kvm); |
| 121 | } |
| 122 | |
| 123 | mutex_unlock(&kv->lock); |
| 124 | } |
| 125 | |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 126 | static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg) |
| 127 | { |
| 128 | struct kvm_vfio *kv = dev->private; |
| 129 | struct vfio_group *vfio_group; |
| 130 | struct kvm_vfio_group *kvg; |
Paul Bolle | e81d1ad | 2014-01-10 01:28:46 +0100 | [diff] [blame] | 131 | int32_t __user *argp = (int32_t __user *)(unsigned long)arg; |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 132 | struct fd f; |
| 133 | int32_t fd; |
| 134 | int ret; |
| 135 | |
| 136 | switch (attr) { |
| 137 | case KVM_DEV_VFIO_GROUP_ADD: |
Paul Bolle | e81d1ad | 2014-01-10 01:28:46 +0100 | [diff] [blame] | 138 | if (get_user(fd, argp)) |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 139 | return -EFAULT; |
| 140 | |
| 141 | f = fdget(fd); |
| 142 | if (!f.file) |
| 143 | return -EBADF; |
| 144 | |
| 145 | vfio_group = kvm_vfio_group_get_external_user(f.file); |
| 146 | fdput(f); |
| 147 | |
| 148 | if (IS_ERR(vfio_group)) |
| 149 | return PTR_ERR(vfio_group); |
| 150 | |
| 151 | mutex_lock(&kv->lock); |
| 152 | |
| 153 | list_for_each_entry(kvg, &kv->group_list, node) { |
| 154 | if (kvg->vfio_group == vfio_group) { |
| 155 | mutex_unlock(&kv->lock); |
| 156 | kvm_vfio_group_put_external_user(vfio_group); |
| 157 | return -EEXIST; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | kvg = kzalloc(sizeof(*kvg), GFP_KERNEL); |
| 162 | if (!kvg) { |
| 163 | mutex_unlock(&kv->lock); |
| 164 | kvm_vfio_group_put_external_user(vfio_group); |
| 165 | return -ENOMEM; |
| 166 | } |
| 167 | |
| 168 | list_add_tail(&kvg->node, &kv->group_list); |
| 169 | kvg->vfio_group = vfio_group; |
| 170 | |
Paolo Bonzini | 5544eb9 | 2015-07-07 15:41:58 +0200 | [diff] [blame] | 171 | kvm_arch_start_assignment(dev->kvm); |
| 172 | |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 173 | mutex_unlock(&kv->lock); |
| 174 | |
Jike Song | 2fc1bec | 2016-12-01 13:20:07 +0800 | [diff] [blame] | 175 | kvm_vfio_group_set_kvm(vfio_group, dev->kvm); |
| 176 | |
Alex Williamson | e0f0bbc | 2013-10-30 11:02:30 -0600 | [diff] [blame] | 177 | kvm_vfio_update_coherency(dev); |
| 178 | |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 179 | return 0; |
| 180 | |
| 181 | case KVM_DEV_VFIO_GROUP_DEL: |
Paul Bolle | e81d1ad | 2014-01-10 01:28:46 +0100 | [diff] [blame] | 182 | if (get_user(fd, argp)) |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 183 | return -EFAULT; |
| 184 | |
| 185 | f = fdget(fd); |
| 186 | if (!f.file) |
| 187 | return -EBADF; |
| 188 | |
| 189 | vfio_group = kvm_vfio_group_get_external_user(f.file); |
| 190 | fdput(f); |
| 191 | |
| 192 | if (IS_ERR(vfio_group)) |
| 193 | return PTR_ERR(vfio_group); |
| 194 | |
| 195 | ret = -ENOENT; |
| 196 | |
| 197 | mutex_lock(&kv->lock); |
| 198 | |
| 199 | list_for_each_entry(kvg, &kv->group_list, node) { |
| 200 | if (kvg->vfio_group != vfio_group) |
| 201 | continue; |
| 202 | |
| 203 | list_del(&kvg->node); |
| 204 | kvm_vfio_group_put_external_user(kvg->vfio_group); |
| 205 | kfree(kvg); |
| 206 | ret = 0; |
| 207 | break; |
| 208 | } |
| 209 | |
Paolo Bonzini | 5544eb9 | 2015-07-07 15:41:58 +0200 | [diff] [blame] | 210 | kvm_arch_end_assignment(dev->kvm); |
| 211 | |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 212 | mutex_unlock(&kv->lock); |
| 213 | |
Jike Song | 2fc1bec | 2016-12-01 13:20:07 +0800 | [diff] [blame] | 214 | kvm_vfio_group_set_kvm(vfio_group, NULL); |
| 215 | |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 216 | kvm_vfio_group_put_external_user(vfio_group); |
| 217 | |
Alex Williamson | e0f0bbc | 2013-10-30 11:02:30 -0600 | [diff] [blame] | 218 | kvm_vfio_update_coherency(dev); |
| 219 | |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | return -ENXIO; |
| 224 | } |
| 225 | |
| 226 | static int kvm_vfio_set_attr(struct kvm_device *dev, |
| 227 | struct kvm_device_attr *attr) |
| 228 | { |
| 229 | switch (attr->group) { |
| 230 | case KVM_DEV_VFIO_GROUP: |
| 231 | return kvm_vfio_set_group(dev, attr->attr, attr->addr); |
| 232 | } |
| 233 | |
| 234 | return -ENXIO; |
| 235 | } |
| 236 | |
| 237 | static int kvm_vfio_has_attr(struct kvm_device *dev, |
| 238 | struct kvm_device_attr *attr) |
| 239 | { |
| 240 | switch (attr->group) { |
| 241 | case KVM_DEV_VFIO_GROUP: |
| 242 | switch (attr->attr) { |
| 243 | case KVM_DEV_VFIO_GROUP_ADD: |
| 244 | case KVM_DEV_VFIO_GROUP_DEL: |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | return -ENXIO; |
| 252 | } |
| 253 | |
| 254 | static void kvm_vfio_destroy(struct kvm_device *dev) |
| 255 | { |
| 256 | struct kvm_vfio *kv = dev->private; |
| 257 | struct kvm_vfio_group *kvg, *tmp; |
| 258 | |
| 259 | list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) { |
Jike Song | 2fc1bec | 2016-12-01 13:20:07 +0800 | [diff] [blame] | 260 | kvm_vfio_group_set_kvm(kvg->vfio_group, NULL); |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 261 | kvm_vfio_group_put_external_user(kvg->vfio_group); |
| 262 | list_del(&kvg->node); |
| 263 | kfree(kvg); |
Paolo Bonzini | 5544eb9 | 2015-07-07 15:41:58 +0200 | [diff] [blame] | 264 | kvm_arch_end_assignment(dev->kvm); |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 265 | } |
| 266 | |
Alex Williamson | e0f0bbc | 2013-10-30 11:02:30 -0600 | [diff] [blame] | 267 | kvm_vfio_update_coherency(dev); |
| 268 | |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 269 | kfree(kv); |
| 270 | kfree(dev); /* alloc by kvm_ioctl_create_device, free by .destroy */ |
| 271 | } |
| 272 | |
Will Deacon | 80ce163 | 2014-09-02 10:27:36 +0100 | [diff] [blame] | 273 | static int kvm_vfio_create(struct kvm_device *dev, u32 type); |
| 274 | |
| 275 | static struct kvm_device_ops kvm_vfio_ops = { |
| 276 | .name = "kvm-vfio", |
| 277 | .create = kvm_vfio_create, |
| 278 | .destroy = kvm_vfio_destroy, |
| 279 | .set_attr = kvm_vfio_set_attr, |
| 280 | .has_attr = kvm_vfio_has_attr, |
| 281 | }; |
| 282 | |
Alex Williamson | ec53500 | 2013-10-30 11:02:17 -0600 | [diff] [blame] | 283 | static int kvm_vfio_create(struct kvm_device *dev, u32 type) |
| 284 | { |
| 285 | struct kvm_device *tmp; |
| 286 | struct kvm_vfio *kv; |
| 287 | |
| 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 | |
| 293 | kv = kzalloc(sizeof(*kv), GFP_KERNEL); |
| 294 | if (!kv) |
| 295 | return -ENOMEM; |
| 296 | |
| 297 | INIT_LIST_HEAD(&kv->group_list); |
| 298 | mutex_init(&kv->lock); |
| 299 | |
| 300 | dev->private = kv; |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
Paolo Bonzini | 3c3c29f | 2014-09-24 13:02:46 +0200 | [diff] [blame] | 305 | int kvm_vfio_ops_init(void) |
Will Deacon | 80ce163 | 2014-09-02 10:27:36 +0100 | [diff] [blame] | 306 | { |
| 307 | return kvm_register_device_ops(&kvm_vfio_ops, KVM_DEV_TYPE_VFIO); |
| 308 | } |
Wanpeng Li | 571ee1b | 2014-10-09 18:30:08 +0800 | [diff] [blame] | 309 | |
| 310 | void kvm_vfio_ops_exit(void) |
| 311 | { |
| 312 | kvm_unregister_device_ops(KVM_DEV_TYPE_VFIO); |
| 313 | } |