Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1 | /* |
| 2 | * VFIO core |
| 3 | * |
| 4 | * Copyright (C) 2012 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 | * Derived from original vfio: |
| 12 | * Copyright 2010 Cisco Systems, Inc. All rights reserved. |
| 13 | * Author: Tom Lyon, pugs@cisco.com |
| 14 | */ |
| 15 | |
| 16 | #include <linux/cdev.h> |
| 17 | #include <linux/compat.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/file.h> |
| 20 | #include <linux/anon_inodes.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/idr.h> |
| 23 | #include <linux/iommu.h> |
| 24 | #include <linux/list.h> |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 25 | #include <linux/miscdevice.h> |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/mutex.h> |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 28 | #include <linux/rwsem.h> |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 29 | #include <linux/sched.h> |
| 30 | #include <linux/slab.h> |
Alex Williamson | 664e938 | 2013-04-30 15:42:28 -0600 | [diff] [blame] | 31 | #include <linux/stat.h> |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 32 | #include <linux/string.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/vfio.h> |
| 35 | #include <linux/wait.h> |
| 36 | |
| 37 | #define DRIVER_VERSION "0.3" |
| 38 | #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>" |
| 39 | #define DRIVER_DESC "VFIO - User Level meta-driver" |
| 40 | |
| 41 | static struct vfio { |
| 42 | struct class *class; |
| 43 | struct list_head iommu_drivers_list; |
| 44 | struct mutex iommu_drivers_lock; |
| 45 | struct list_head group_list; |
| 46 | struct idr group_idr; |
| 47 | struct mutex group_lock; |
| 48 | struct cdev group_cdev; |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 49 | dev_t group_devt; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 50 | wait_queue_head_t release_q; |
| 51 | } vfio; |
| 52 | |
| 53 | struct vfio_iommu_driver { |
| 54 | const struct vfio_iommu_driver_ops *ops; |
| 55 | struct list_head vfio_next; |
| 56 | }; |
| 57 | |
| 58 | struct vfio_container { |
| 59 | struct kref kref; |
| 60 | struct list_head group_list; |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 61 | struct rw_semaphore group_lock; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 62 | struct vfio_iommu_driver *iommu_driver; |
| 63 | void *iommu_data; |
| 64 | }; |
| 65 | |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 66 | struct vfio_unbound_dev { |
| 67 | struct device *dev; |
| 68 | struct list_head unbound_next; |
| 69 | }; |
| 70 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 71 | struct vfio_group { |
| 72 | struct kref kref; |
| 73 | int minor; |
| 74 | atomic_t container_users; |
| 75 | struct iommu_group *iommu_group; |
| 76 | struct vfio_container *container; |
| 77 | struct list_head device_list; |
| 78 | struct mutex device_lock; |
| 79 | struct device *dev; |
| 80 | struct notifier_block nb; |
| 81 | struct list_head vfio_next; |
| 82 | struct list_head container_next; |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 83 | struct list_head unbound_list; |
| 84 | struct mutex unbound_lock; |
Alex Williamson | 6d6768c | 2013-06-25 16:06:54 -0600 | [diff] [blame] | 85 | atomic_t opened; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | struct vfio_device { |
| 89 | struct kref kref; |
| 90 | struct device *dev; |
| 91 | const struct vfio_device_ops *ops; |
| 92 | struct vfio_group *group; |
| 93 | struct list_head group_next; |
| 94 | void *device_data; |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * IOMMU driver registration |
| 99 | */ |
| 100 | int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops) |
| 101 | { |
| 102 | struct vfio_iommu_driver *driver, *tmp; |
| 103 | |
| 104 | driver = kzalloc(sizeof(*driver), GFP_KERNEL); |
| 105 | if (!driver) |
| 106 | return -ENOMEM; |
| 107 | |
| 108 | driver->ops = ops; |
| 109 | |
| 110 | mutex_lock(&vfio.iommu_drivers_lock); |
| 111 | |
| 112 | /* Check for duplicates */ |
| 113 | list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) { |
| 114 | if (tmp->ops == ops) { |
| 115 | mutex_unlock(&vfio.iommu_drivers_lock); |
| 116 | kfree(driver); |
| 117 | return -EINVAL; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | list_add(&driver->vfio_next, &vfio.iommu_drivers_list); |
| 122 | |
| 123 | mutex_unlock(&vfio.iommu_drivers_lock); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | EXPORT_SYMBOL_GPL(vfio_register_iommu_driver); |
| 128 | |
| 129 | void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops) |
| 130 | { |
| 131 | struct vfio_iommu_driver *driver; |
| 132 | |
| 133 | mutex_lock(&vfio.iommu_drivers_lock); |
| 134 | list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) { |
| 135 | if (driver->ops == ops) { |
| 136 | list_del(&driver->vfio_next); |
| 137 | mutex_unlock(&vfio.iommu_drivers_lock); |
| 138 | kfree(driver); |
| 139 | return; |
| 140 | } |
| 141 | } |
| 142 | mutex_unlock(&vfio.iommu_drivers_lock); |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver); |
| 145 | |
| 146 | /** |
| 147 | * Group minor allocation/free - both called with vfio.group_lock held |
| 148 | */ |
| 149 | static int vfio_alloc_group_minor(struct vfio_group *group) |
| 150 | { |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 151 | return idr_alloc(&vfio.group_idr, group, 0, MINORMASK + 1, GFP_KERNEL); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void vfio_free_group_minor(int minor) |
| 155 | { |
| 156 | idr_remove(&vfio.group_idr, minor); |
| 157 | } |
| 158 | |
| 159 | static int vfio_iommu_group_notifier(struct notifier_block *nb, |
| 160 | unsigned long action, void *data); |
| 161 | static void vfio_group_get(struct vfio_group *group); |
| 162 | |
| 163 | /** |
| 164 | * Container objects - containers are created when /dev/vfio/vfio is |
| 165 | * opened, but their lifecycle extends until the last user is done, so |
| 166 | * it's freed via kref. Must support container/group/device being |
| 167 | * closed in any order. |
| 168 | */ |
| 169 | static void vfio_container_get(struct vfio_container *container) |
| 170 | { |
| 171 | kref_get(&container->kref); |
| 172 | } |
| 173 | |
| 174 | static void vfio_container_release(struct kref *kref) |
| 175 | { |
| 176 | struct vfio_container *container; |
| 177 | container = container_of(kref, struct vfio_container, kref); |
| 178 | |
| 179 | kfree(container); |
| 180 | } |
| 181 | |
| 182 | static void vfio_container_put(struct vfio_container *container) |
| 183 | { |
| 184 | kref_put(&container->kref, vfio_container_release); |
| 185 | } |
| 186 | |
Jiang Liu | 9df7b25 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 187 | static void vfio_group_unlock_and_free(struct vfio_group *group) |
| 188 | { |
| 189 | mutex_unlock(&vfio.group_lock); |
| 190 | /* |
| 191 | * Unregister outside of lock. A spurious callback is harmless now |
| 192 | * that the group is no longer in vfio.group_list. |
| 193 | */ |
| 194 | iommu_group_unregister_notifier(group->iommu_group, &group->nb); |
| 195 | kfree(group); |
| 196 | } |
| 197 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 198 | /** |
| 199 | * Group objects - create, release, get, put, search |
| 200 | */ |
| 201 | static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group) |
| 202 | { |
| 203 | struct vfio_group *group, *tmp; |
| 204 | struct device *dev; |
| 205 | int ret, minor; |
| 206 | |
| 207 | group = kzalloc(sizeof(*group), GFP_KERNEL); |
| 208 | if (!group) |
| 209 | return ERR_PTR(-ENOMEM); |
| 210 | |
| 211 | kref_init(&group->kref); |
| 212 | INIT_LIST_HEAD(&group->device_list); |
| 213 | mutex_init(&group->device_lock); |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 214 | INIT_LIST_HEAD(&group->unbound_list); |
| 215 | mutex_init(&group->unbound_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 216 | atomic_set(&group->container_users, 0); |
Alex Williamson | 6d6768c | 2013-06-25 16:06:54 -0600 | [diff] [blame] | 217 | atomic_set(&group->opened, 0); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 218 | group->iommu_group = iommu_group; |
| 219 | |
| 220 | group->nb.notifier_call = vfio_iommu_group_notifier; |
| 221 | |
| 222 | /* |
| 223 | * blocking notifiers acquire a rwsem around registering and hold |
| 224 | * it around callback. Therefore, need to register outside of |
| 225 | * vfio.group_lock to avoid A-B/B-A contention. Our callback won't |
| 226 | * do anything unless it can find the group in vfio.group_list, so |
| 227 | * no harm in registering early. |
| 228 | */ |
| 229 | ret = iommu_group_register_notifier(iommu_group, &group->nb); |
| 230 | if (ret) { |
| 231 | kfree(group); |
| 232 | return ERR_PTR(ret); |
| 233 | } |
| 234 | |
| 235 | mutex_lock(&vfio.group_lock); |
| 236 | |
| 237 | minor = vfio_alloc_group_minor(group); |
| 238 | if (minor < 0) { |
Jiang Liu | 9df7b25 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 239 | vfio_group_unlock_and_free(group); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 240 | return ERR_PTR(minor); |
| 241 | } |
| 242 | |
| 243 | /* Did we race creating this group? */ |
| 244 | list_for_each_entry(tmp, &vfio.group_list, vfio_next) { |
| 245 | if (tmp->iommu_group == iommu_group) { |
| 246 | vfio_group_get(tmp); |
| 247 | vfio_free_group_minor(minor); |
Jiang Liu | 9df7b25 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 248 | vfio_group_unlock_and_free(group); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 249 | return tmp; |
| 250 | } |
| 251 | } |
| 252 | |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 253 | dev = device_create(vfio.class, NULL, |
| 254 | MKDEV(MAJOR(vfio.group_devt), minor), |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 255 | group, "%d", iommu_group_id(iommu_group)); |
| 256 | if (IS_ERR(dev)) { |
| 257 | vfio_free_group_minor(minor); |
Jiang Liu | 9df7b25 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 258 | vfio_group_unlock_and_free(group); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 259 | return (struct vfio_group *)dev; /* ERR_PTR */ |
| 260 | } |
| 261 | |
| 262 | group->minor = minor; |
| 263 | group->dev = dev; |
| 264 | |
| 265 | list_add(&group->vfio_next, &vfio.group_list); |
| 266 | |
| 267 | mutex_unlock(&vfio.group_lock); |
| 268 | |
| 269 | return group; |
| 270 | } |
| 271 | |
Al Viro | 6d2cd3c | 2012-08-17 21:27:32 -0400 | [diff] [blame] | 272 | /* called with vfio.group_lock held */ |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 273 | static void vfio_group_release(struct kref *kref) |
| 274 | { |
| 275 | struct vfio_group *group = container_of(kref, struct vfio_group, kref); |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 276 | struct vfio_unbound_dev *unbound, *tmp; |
Alex Williamson | 4a68810 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 277 | struct iommu_group *iommu_group = group->iommu_group; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 278 | |
| 279 | WARN_ON(!list_empty(&group->device_list)); |
| 280 | |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 281 | list_for_each_entry_safe(unbound, tmp, |
| 282 | &group->unbound_list, unbound_next) { |
| 283 | list_del(&unbound->unbound_next); |
| 284 | kfree(unbound); |
| 285 | } |
| 286 | |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 287 | device_destroy(vfio.class, MKDEV(MAJOR(vfio.group_devt), group->minor)); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 288 | list_del(&group->vfio_next); |
| 289 | vfio_free_group_minor(group->minor); |
Jiang Liu | 9df7b25 | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 290 | vfio_group_unlock_and_free(group); |
Alex Williamson | 4a68810 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 291 | iommu_group_put(iommu_group); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static void vfio_group_put(struct vfio_group *group) |
| 295 | { |
Al Viro | 6d2cd3c | 2012-08-17 21:27:32 -0400 | [diff] [blame] | 296 | kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* Assume group_lock or group reference is held */ |
| 300 | static void vfio_group_get(struct vfio_group *group) |
| 301 | { |
| 302 | kref_get(&group->kref); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Not really a try as we will sleep for mutex, but we need to make |
| 307 | * sure the group pointer is valid under lock and get a reference. |
| 308 | */ |
| 309 | static struct vfio_group *vfio_group_try_get(struct vfio_group *group) |
| 310 | { |
| 311 | struct vfio_group *target = group; |
| 312 | |
| 313 | mutex_lock(&vfio.group_lock); |
| 314 | list_for_each_entry(group, &vfio.group_list, vfio_next) { |
| 315 | if (group == target) { |
| 316 | vfio_group_get(group); |
| 317 | mutex_unlock(&vfio.group_lock); |
| 318 | return group; |
| 319 | } |
| 320 | } |
| 321 | mutex_unlock(&vfio.group_lock); |
| 322 | |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | static |
| 327 | struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group) |
| 328 | { |
| 329 | struct vfio_group *group; |
| 330 | |
| 331 | mutex_lock(&vfio.group_lock); |
| 332 | list_for_each_entry(group, &vfio.group_list, vfio_next) { |
| 333 | if (group->iommu_group == iommu_group) { |
| 334 | vfio_group_get(group); |
| 335 | mutex_unlock(&vfio.group_lock); |
| 336 | return group; |
| 337 | } |
| 338 | } |
| 339 | mutex_unlock(&vfio.group_lock); |
| 340 | |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | static struct vfio_group *vfio_group_get_from_minor(int minor) |
| 345 | { |
| 346 | struct vfio_group *group; |
| 347 | |
| 348 | mutex_lock(&vfio.group_lock); |
| 349 | group = idr_find(&vfio.group_idr, minor); |
| 350 | if (!group) { |
| 351 | mutex_unlock(&vfio.group_lock); |
| 352 | return NULL; |
| 353 | } |
| 354 | vfio_group_get(group); |
| 355 | mutex_unlock(&vfio.group_lock); |
| 356 | |
| 357 | return group; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Device objects - create, release, get, put, search |
| 362 | */ |
| 363 | static |
| 364 | struct vfio_device *vfio_group_create_device(struct vfio_group *group, |
| 365 | struct device *dev, |
| 366 | const struct vfio_device_ops *ops, |
| 367 | void *device_data) |
| 368 | { |
| 369 | struct vfio_device *device; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 370 | |
| 371 | device = kzalloc(sizeof(*device), GFP_KERNEL); |
| 372 | if (!device) |
| 373 | return ERR_PTR(-ENOMEM); |
| 374 | |
| 375 | kref_init(&device->kref); |
| 376 | device->dev = dev; |
| 377 | device->group = group; |
| 378 | device->ops = ops; |
| 379 | device->device_data = device_data; |
Jean Delvare | 8283b49 | 2014-04-14 12:55:38 +0200 | [diff] [blame] | 380 | dev_set_drvdata(dev, device); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 381 | |
| 382 | /* No need to get group_lock, caller has group reference */ |
| 383 | vfio_group_get(group); |
| 384 | |
| 385 | mutex_lock(&group->device_lock); |
| 386 | list_add(&device->group_next, &group->device_list); |
| 387 | mutex_unlock(&group->device_lock); |
| 388 | |
| 389 | return device; |
| 390 | } |
| 391 | |
| 392 | static void vfio_device_release(struct kref *kref) |
| 393 | { |
| 394 | struct vfio_device *device = container_of(kref, |
| 395 | struct vfio_device, kref); |
| 396 | struct vfio_group *group = device->group; |
| 397 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 398 | list_del(&device->group_next); |
| 399 | mutex_unlock(&group->device_lock); |
| 400 | |
| 401 | dev_set_drvdata(device->dev, NULL); |
| 402 | |
| 403 | kfree(device); |
| 404 | |
| 405 | /* vfio_del_group_dev may be waiting for this device */ |
| 406 | wake_up(&vfio.release_q); |
| 407 | } |
| 408 | |
| 409 | /* Device reference always implies a group reference */ |
Vijay Mohan Pandarathil | 44f5071 | 2013-03-11 09:28:44 -0600 | [diff] [blame] | 410 | void vfio_device_put(struct vfio_device *device) |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 411 | { |
Al Viro | 934ad4c | 2012-08-17 19:49:09 -0400 | [diff] [blame] | 412 | struct vfio_group *group = device->group; |
Al Viro | 90b1253 | 2012-08-17 21:29:06 -0400 | [diff] [blame] | 413 | kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock); |
Al Viro | 934ad4c | 2012-08-17 19:49:09 -0400 | [diff] [blame] | 414 | vfio_group_put(group); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 415 | } |
Vijay Mohan Pandarathil | 44f5071 | 2013-03-11 09:28:44 -0600 | [diff] [blame] | 416 | EXPORT_SYMBOL_GPL(vfio_device_put); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 417 | |
| 418 | static void vfio_device_get(struct vfio_device *device) |
| 419 | { |
| 420 | vfio_group_get(device->group); |
| 421 | kref_get(&device->kref); |
| 422 | } |
| 423 | |
| 424 | static struct vfio_device *vfio_group_get_device(struct vfio_group *group, |
| 425 | struct device *dev) |
| 426 | { |
| 427 | struct vfio_device *device; |
| 428 | |
| 429 | mutex_lock(&group->device_lock); |
| 430 | list_for_each_entry(device, &group->device_list, group_next) { |
| 431 | if (device->dev == dev) { |
| 432 | vfio_device_get(device); |
| 433 | mutex_unlock(&group->device_lock); |
| 434 | return device; |
| 435 | } |
| 436 | } |
| 437 | mutex_unlock(&group->device_lock); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Whitelist some drivers that we know are safe (no dma) or just sit on |
| 443 | * a device. It's not always practical to leave a device within a group |
| 444 | * driverless as it could get re-bound to something unsafe. |
| 445 | */ |
Alex Williamson | 2b489a4 | 2013-02-14 14:02:13 -0700 | [diff] [blame] | 446 | static const char * const vfio_driver_whitelist[] = { "pci-stub", "pcieport" }; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 447 | |
| 448 | static bool vfio_whitelisted_driver(struct device_driver *drv) |
| 449 | { |
| 450 | int i; |
| 451 | |
| 452 | for (i = 0; i < ARRAY_SIZE(vfio_driver_whitelist); i++) { |
| 453 | if (!strcmp(drv->name, vfio_driver_whitelist[i])) |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | /* |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 461 | * A vfio group is viable for use by userspace if all devices are in |
| 462 | * one of the following states: |
| 463 | * - driver-less |
| 464 | * - bound to a vfio driver |
| 465 | * - bound to a whitelisted driver |
| 466 | * |
| 467 | * We use two methods to determine whether a device is bound to a vfio |
| 468 | * driver. The first is to test whether the device exists in the vfio |
| 469 | * group. The second is to test if the device exists on the group |
| 470 | * unbound_list, indicating it's in the middle of transitioning from |
| 471 | * a vfio driver to driver-less. |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 472 | */ |
| 473 | static int vfio_dev_viable(struct device *dev, void *data) |
| 474 | { |
| 475 | struct vfio_group *group = data; |
| 476 | struct vfio_device *device; |
Jiang Liu | de2b3ee | 2012-12-07 13:43:50 -0700 | [diff] [blame] | 477 | struct device_driver *drv = ACCESS_ONCE(dev->driver); |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 478 | struct vfio_unbound_dev *unbound; |
| 479 | int ret = -EINVAL; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 480 | |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 481 | mutex_lock(&group->unbound_lock); |
| 482 | list_for_each_entry(unbound, &group->unbound_list, unbound_next) { |
| 483 | if (dev == unbound->dev) { |
| 484 | ret = 0; |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | mutex_unlock(&group->unbound_lock); |
| 489 | |
| 490 | if (!ret || !drv || vfio_whitelisted_driver(drv)) |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 491 | return 0; |
| 492 | |
| 493 | device = vfio_group_get_device(group, dev); |
| 494 | if (device) { |
| 495 | vfio_device_put(device); |
| 496 | return 0; |
| 497 | } |
| 498 | |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 499 | return ret; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Async device support |
| 504 | */ |
| 505 | static int vfio_group_nb_add_dev(struct vfio_group *group, struct device *dev) |
| 506 | { |
| 507 | struct vfio_device *device; |
| 508 | |
| 509 | /* Do we already know about it? We shouldn't */ |
| 510 | device = vfio_group_get_device(group, dev); |
| 511 | if (WARN_ON_ONCE(device)) { |
| 512 | vfio_device_put(device); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /* Nothing to do for idle groups */ |
| 517 | if (!atomic_read(&group->container_users)) |
| 518 | return 0; |
| 519 | |
| 520 | /* TODO Prevent device auto probing */ |
| 521 | WARN("Device %s added to live group %d!\n", dev_name(dev), |
| 522 | iommu_group_id(group->iommu_group)); |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 527 | static int vfio_group_nb_verify(struct vfio_group *group, struct device *dev) |
| 528 | { |
| 529 | /* We don't care what happens when the group isn't in use */ |
| 530 | if (!atomic_read(&group->container_users)) |
| 531 | return 0; |
| 532 | |
| 533 | return vfio_dev_viable(dev, group); |
| 534 | } |
| 535 | |
| 536 | static int vfio_iommu_group_notifier(struct notifier_block *nb, |
| 537 | unsigned long action, void *data) |
| 538 | { |
| 539 | struct vfio_group *group = container_of(nb, struct vfio_group, nb); |
| 540 | struct device *dev = data; |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 541 | struct vfio_unbound_dev *unbound; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 542 | |
| 543 | /* |
Alex Williamson | c640193 | 2013-06-10 16:40:56 -0600 | [diff] [blame] | 544 | * Need to go through a group_lock lookup to get a reference or we |
| 545 | * risk racing a group being removed. Ignore spurious notifies. |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 546 | */ |
| 547 | group = vfio_group_try_get(group); |
Alex Williamson | c640193 | 2013-06-10 16:40:56 -0600 | [diff] [blame] | 548 | if (!group) |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 549 | return NOTIFY_OK; |
| 550 | |
| 551 | switch (action) { |
| 552 | case IOMMU_GROUP_NOTIFY_ADD_DEVICE: |
| 553 | vfio_group_nb_add_dev(group, dev); |
| 554 | break; |
| 555 | case IOMMU_GROUP_NOTIFY_DEL_DEVICE: |
Alex Williamson | de9c760 | 2013-06-10 16:40:56 -0600 | [diff] [blame] | 556 | /* |
| 557 | * Nothing to do here. If the device is in use, then the |
| 558 | * vfio sub-driver should block the remove callback until |
| 559 | * it is unused. If the device is unused or attached to a |
| 560 | * stub driver, then it should be released and we don't |
| 561 | * care that it will be going away. |
| 562 | */ |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 563 | break; |
| 564 | case IOMMU_GROUP_NOTIFY_BIND_DRIVER: |
| 565 | pr_debug("%s: Device %s, group %d binding to driver\n", |
| 566 | __func__, dev_name(dev), |
| 567 | iommu_group_id(group->iommu_group)); |
| 568 | break; |
| 569 | case IOMMU_GROUP_NOTIFY_BOUND_DRIVER: |
| 570 | pr_debug("%s: Device %s, group %d bound to driver %s\n", |
| 571 | __func__, dev_name(dev), |
| 572 | iommu_group_id(group->iommu_group), dev->driver->name); |
| 573 | BUG_ON(vfio_group_nb_verify(group, dev)); |
| 574 | break; |
| 575 | case IOMMU_GROUP_NOTIFY_UNBIND_DRIVER: |
| 576 | pr_debug("%s: Device %s, group %d unbinding from driver %s\n", |
| 577 | __func__, dev_name(dev), |
| 578 | iommu_group_id(group->iommu_group), dev->driver->name); |
| 579 | break; |
| 580 | case IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER: |
| 581 | pr_debug("%s: Device %s, group %d unbound from driver\n", |
| 582 | __func__, dev_name(dev), |
| 583 | iommu_group_id(group->iommu_group)); |
| 584 | /* |
| 585 | * XXX An unbound device in a live group is ok, but we'd |
| 586 | * really like to avoid the above BUG_ON by preventing other |
| 587 | * drivers from binding to it. Once that occurs, we have to |
| 588 | * stop the system to maintain isolation. At a minimum, we'd |
| 589 | * want a toggle to disable driver auto probe for this device. |
| 590 | */ |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 591 | |
| 592 | mutex_lock(&group->unbound_lock); |
| 593 | list_for_each_entry(unbound, |
| 594 | &group->unbound_list, unbound_next) { |
| 595 | if (dev == unbound->dev) { |
| 596 | list_del(&unbound->unbound_next); |
| 597 | kfree(unbound); |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | mutex_unlock(&group->unbound_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 602 | break; |
| 603 | } |
| 604 | |
| 605 | vfio_group_put(group); |
| 606 | return NOTIFY_OK; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * VFIO driver API |
| 611 | */ |
| 612 | int vfio_add_group_dev(struct device *dev, |
| 613 | const struct vfio_device_ops *ops, void *device_data) |
| 614 | { |
| 615 | struct iommu_group *iommu_group; |
| 616 | struct vfio_group *group; |
| 617 | struct vfio_device *device; |
| 618 | |
| 619 | iommu_group = iommu_group_get(dev); |
| 620 | if (!iommu_group) |
| 621 | return -EINVAL; |
| 622 | |
| 623 | group = vfio_group_get_from_iommu(iommu_group); |
| 624 | if (!group) { |
| 625 | group = vfio_create_group(iommu_group); |
| 626 | if (IS_ERR(group)) { |
| 627 | iommu_group_put(iommu_group); |
| 628 | return PTR_ERR(group); |
| 629 | } |
Alex Williamson | 4a68810 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 630 | } else { |
| 631 | /* |
| 632 | * A found vfio_group already holds a reference to the |
| 633 | * iommu_group. A created vfio_group keeps the reference. |
| 634 | */ |
| 635 | iommu_group_put(iommu_group); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | device = vfio_group_get_device(group, dev); |
| 639 | if (device) { |
| 640 | WARN(1, "Device %s already exists on group %d\n", |
| 641 | dev_name(dev), iommu_group_id(iommu_group)); |
| 642 | vfio_device_put(device); |
| 643 | vfio_group_put(group); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 644 | return -EBUSY; |
| 645 | } |
| 646 | |
| 647 | device = vfio_group_create_device(group, dev, ops, device_data); |
| 648 | if (IS_ERR(device)) { |
| 649 | vfio_group_put(group); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 650 | return PTR_ERR(device); |
| 651 | } |
| 652 | |
| 653 | /* |
Alex Williamson | 4a68810 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 654 | * Drop all but the vfio_device reference. The vfio_device holds |
| 655 | * a reference to the vfio_group, which holds a reference to the |
| 656 | * iommu_group. |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 657 | */ |
| 658 | vfio_group_put(group); |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | EXPORT_SYMBOL_GPL(vfio_add_group_dev); |
| 663 | |
Vijay Mohan Pandarathil | 44f5071 | 2013-03-11 09:28:44 -0600 | [diff] [blame] | 664 | /** |
| 665 | * Get a reference to the vfio_device for a device that is known to |
| 666 | * be bound to a vfio driver. The driver implicitly holds a |
| 667 | * vfio_device reference between vfio_add_group_dev and |
| 668 | * vfio_del_group_dev. We can therefore use drvdata to increment |
| 669 | * that reference from the struct device. This additional |
| 670 | * reference must be released by calling vfio_device_put. |
| 671 | */ |
| 672 | struct vfio_device *vfio_device_get_from_dev(struct device *dev) |
| 673 | { |
| 674 | struct vfio_device *device = dev_get_drvdata(dev); |
| 675 | |
| 676 | vfio_device_get(device); |
| 677 | |
| 678 | return device; |
| 679 | } |
| 680 | EXPORT_SYMBOL_GPL(vfio_device_get_from_dev); |
| 681 | |
| 682 | /* |
| 683 | * Caller must hold a reference to the vfio_device |
| 684 | */ |
| 685 | void *vfio_device_data(struct vfio_device *device) |
| 686 | { |
| 687 | return device->device_data; |
| 688 | } |
| 689 | EXPORT_SYMBOL_GPL(vfio_device_data); |
| 690 | |
Alex Williamson | e014e94 | 2013-02-14 14:02:13 -0700 | [diff] [blame] | 691 | /* Given a referenced group, check if it contains the device */ |
| 692 | static bool vfio_dev_present(struct vfio_group *group, struct device *dev) |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 693 | { |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 694 | struct vfio_device *device; |
| 695 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 696 | device = vfio_group_get_device(group, dev); |
Alex Williamson | e014e94 | 2013-02-14 14:02:13 -0700 | [diff] [blame] | 697 | if (!device) |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 698 | return false; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 699 | |
| 700 | vfio_device_put(device); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 701 | return true; |
| 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Decrement the device reference count and wait for the device to be |
| 706 | * removed. Open file descriptors for the device... */ |
| 707 | void *vfio_del_group_dev(struct device *dev) |
| 708 | { |
| 709 | struct vfio_device *device = dev_get_drvdata(dev); |
| 710 | struct vfio_group *group = device->group; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 711 | void *device_data = device->device_data; |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 712 | struct vfio_unbound_dev *unbound; |
Alex Williamson | 13060b6 | 2015-02-06 15:05:07 -0700 | [diff] [blame] | 713 | unsigned int i = 0; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 714 | |
Alex Williamson | e014e94 | 2013-02-14 14:02:13 -0700 | [diff] [blame] | 715 | /* |
| 716 | * The group exists so long as we have a device reference. Get |
| 717 | * a group reference and use it to scan for the device going away. |
| 718 | */ |
| 719 | vfio_group_get(group); |
| 720 | |
Alex Williamson | 60720a0 | 2015-02-06 15:05:06 -0700 | [diff] [blame] | 721 | /* |
| 722 | * When the device is removed from the group, the group suddenly |
| 723 | * becomes non-viable; the device has a driver (until the unbind |
| 724 | * completes), but it's not present in the group. This is bad news |
| 725 | * for any external users that need to re-acquire a group reference |
| 726 | * in order to match and release their existing reference. To |
| 727 | * solve this, we track such devices on the unbound_list to bridge |
| 728 | * the gap until they're fully unbound. |
| 729 | */ |
| 730 | unbound = kzalloc(sizeof(*unbound), GFP_KERNEL); |
| 731 | if (unbound) { |
| 732 | unbound->dev = dev; |
| 733 | mutex_lock(&group->unbound_lock); |
| 734 | list_add(&unbound->unbound_next, &group->unbound_list); |
| 735 | mutex_unlock(&group->unbound_lock); |
| 736 | } |
| 737 | WARN_ON(!unbound); |
| 738 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 739 | vfio_device_put(device); |
| 740 | |
Alex Williamson | 13060b6 | 2015-02-06 15:05:07 -0700 | [diff] [blame] | 741 | /* |
| 742 | * If the device is still present in the group after the above |
| 743 | * 'put', then it is in use and we need to request it from the |
| 744 | * bus driver. The driver may in turn need to request the |
| 745 | * device from the user. We send the request on an arbitrary |
| 746 | * interval with counter to allow the driver to take escalating |
| 747 | * measures to release the device if it has the ability to do so. |
| 748 | */ |
| 749 | do { |
| 750 | device = vfio_group_get_device(group, dev); |
| 751 | if (!device) |
| 752 | break; |
| 753 | |
| 754 | if (device->ops->request) |
| 755 | device->ops->request(device_data, i++); |
| 756 | |
| 757 | vfio_device_put(device); |
| 758 | |
| 759 | } while (wait_event_interruptible_timeout(vfio.release_q, |
| 760 | !vfio_dev_present(group, dev), |
| 761 | HZ * 10) <= 0); |
Alex Williamson | e014e94 | 2013-02-14 14:02:13 -0700 | [diff] [blame] | 762 | |
| 763 | vfio_group_put(group); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 764 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 765 | return device_data; |
| 766 | } |
| 767 | EXPORT_SYMBOL_GPL(vfio_del_group_dev); |
| 768 | |
| 769 | /** |
| 770 | * VFIO base fd, /dev/vfio/vfio |
| 771 | */ |
| 772 | static long vfio_ioctl_check_extension(struct vfio_container *container, |
| 773 | unsigned long arg) |
| 774 | { |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 775 | struct vfio_iommu_driver *driver; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 776 | long ret = 0; |
| 777 | |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 778 | down_read(&container->group_lock); |
| 779 | |
| 780 | driver = container->iommu_driver; |
| 781 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 782 | switch (arg) { |
| 783 | /* No base extensions yet */ |
| 784 | default: |
| 785 | /* |
| 786 | * If no driver is set, poll all registered drivers for |
| 787 | * extensions and return the first positive result. If |
| 788 | * a driver is already set, further queries will be passed |
| 789 | * only to that driver. |
| 790 | */ |
| 791 | if (!driver) { |
| 792 | mutex_lock(&vfio.iommu_drivers_lock); |
| 793 | list_for_each_entry(driver, &vfio.iommu_drivers_list, |
| 794 | vfio_next) { |
| 795 | if (!try_module_get(driver->ops->owner)) |
| 796 | continue; |
| 797 | |
| 798 | ret = driver->ops->ioctl(NULL, |
| 799 | VFIO_CHECK_EXTENSION, |
| 800 | arg); |
| 801 | module_put(driver->ops->owner); |
| 802 | if (ret > 0) |
| 803 | break; |
| 804 | } |
| 805 | mutex_unlock(&vfio.iommu_drivers_lock); |
| 806 | } else |
| 807 | ret = driver->ops->ioctl(container->iommu_data, |
| 808 | VFIO_CHECK_EXTENSION, arg); |
| 809 | } |
| 810 | |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 811 | up_read(&container->group_lock); |
| 812 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 813 | return ret; |
| 814 | } |
| 815 | |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 816 | /* hold write lock on container->group_lock */ |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 817 | static int __vfio_container_attach_groups(struct vfio_container *container, |
| 818 | struct vfio_iommu_driver *driver, |
| 819 | void *data) |
| 820 | { |
| 821 | struct vfio_group *group; |
| 822 | int ret = -ENODEV; |
| 823 | |
| 824 | list_for_each_entry(group, &container->group_list, container_next) { |
| 825 | ret = driver->ops->attach_group(data, group->iommu_group); |
| 826 | if (ret) |
| 827 | goto unwind; |
| 828 | } |
| 829 | |
| 830 | return ret; |
| 831 | |
| 832 | unwind: |
| 833 | list_for_each_entry_continue_reverse(group, &container->group_list, |
| 834 | container_next) { |
| 835 | driver->ops->detach_group(data, group->iommu_group); |
| 836 | } |
| 837 | |
| 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | static long vfio_ioctl_set_iommu(struct vfio_container *container, |
| 842 | unsigned long arg) |
| 843 | { |
| 844 | struct vfio_iommu_driver *driver; |
| 845 | long ret = -ENODEV; |
| 846 | |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 847 | down_write(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 848 | |
| 849 | /* |
| 850 | * The container is designed to be an unprivileged interface while |
| 851 | * the group can be assigned to specific users. Therefore, only by |
| 852 | * adding a group to a container does the user get the privilege of |
| 853 | * enabling the iommu, which may allocate finite resources. There |
| 854 | * is no unset_iommu, but by removing all the groups from a container, |
| 855 | * the container is deprivileged and returns to an unset state. |
| 856 | */ |
| 857 | if (list_empty(&container->group_list) || container->iommu_driver) { |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 858 | up_write(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 859 | return -EINVAL; |
| 860 | } |
| 861 | |
| 862 | mutex_lock(&vfio.iommu_drivers_lock); |
| 863 | list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) { |
| 864 | void *data; |
| 865 | |
| 866 | if (!try_module_get(driver->ops->owner)) |
| 867 | continue; |
| 868 | |
| 869 | /* |
| 870 | * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION, |
| 871 | * so test which iommu driver reported support for this |
| 872 | * extension and call open on them. We also pass them the |
| 873 | * magic, allowing a single driver to support multiple |
| 874 | * interfaces if they'd like. |
| 875 | */ |
| 876 | if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) { |
| 877 | module_put(driver->ops->owner); |
| 878 | continue; |
| 879 | } |
| 880 | |
| 881 | /* module reference holds the driver we're working on */ |
| 882 | mutex_unlock(&vfio.iommu_drivers_lock); |
| 883 | |
| 884 | data = driver->ops->open(arg); |
| 885 | if (IS_ERR(data)) { |
| 886 | ret = PTR_ERR(data); |
| 887 | module_put(driver->ops->owner); |
| 888 | goto skip_drivers_unlock; |
| 889 | } |
| 890 | |
| 891 | ret = __vfio_container_attach_groups(container, driver, data); |
| 892 | if (!ret) { |
| 893 | container->iommu_driver = driver; |
| 894 | container->iommu_data = data; |
| 895 | } else { |
| 896 | driver->ops->release(data); |
| 897 | module_put(driver->ops->owner); |
| 898 | } |
| 899 | |
| 900 | goto skip_drivers_unlock; |
| 901 | } |
| 902 | |
| 903 | mutex_unlock(&vfio.iommu_drivers_lock); |
| 904 | skip_drivers_unlock: |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 905 | up_write(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 906 | |
| 907 | return ret; |
| 908 | } |
| 909 | |
| 910 | static long vfio_fops_unl_ioctl(struct file *filep, |
| 911 | unsigned int cmd, unsigned long arg) |
| 912 | { |
| 913 | struct vfio_container *container = filep->private_data; |
| 914 | struct vfio_iommu_driver *driver; |
| 915 | void *data; |
| 916 | long ret = -EINVAL; |
| 917 | |
| 918 | if (!container) |
| 919 | return ret; |
| 920 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 921 | switch (cmd) { |
| 922 | case VFIO_GET_API_VERSION: |
| 923 | ret = VFIO_API_VERSION; |
| 924 | break; |
| 925 | case VFIO_CHECK_EXTENSION: |
| 926 | ret = vfio_ioctl_check_extension(container, arg); |
| 927 | break; |
| 928 | case VFIO_SET_IOMMU: |
| 929 | ret = vfio_ioctl_set_iommu(container, arg); |
| 930 | break; |
| 931 | default: |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 932 | down_read(&container->group_lock); |
| 933 | |
| 934 | driver = container->iommu_driver; |
| 935 | data = container->iommu_data; |
| 936 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 937 | if (driver) /* passthrough all unrecognized ioctls */ |
| 938 | ret = driver->ops->ioctl(data, cmd, arg); |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 939 | |
| 940 | up_read(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | #ifdef CONFIG_COMPAT |
| 947 | static long vfio_fops_compat_ioctl(struct file *filep, |
| 948 | unsigned int cmd, unsigned long arg) |
| 949 | { |
| 950 | arg = (unsigned long)compat_ptr(arg); |
| 951 | return vfio_fops_unl_ioctl(filep, cmd, arg); |
| 952 | } |
| 953 | #endif /* CONFIG_COMPAT */ |
| 954 | |
| 955 | static int vfio_fops_open(struct inode *inode, struct file *filep) |
| 956 | { |
| 957 | struct vfio_container *container; |
| 958 | |
| 959 | container = kzalloc(sizeof(*container), GFP_KERNEL); |
| 960 | if (!container) |
| 961 | return -ENOMEM; |
| 962 | |
| 963 | INIT_LIST_HEAD(&container->group_list); |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 964 | init_rwsem(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 965 | kref_init(&container->kref); |
| 966 | |
| 967 | filep->private_data = container; |
| 968 | |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | static int vfio_fops_release(struct inode *inode, struct file *filep) |
| 973 | { |
| 974 | struct vfio_container *container = filep->private_data; |
| 975 | |
| 976 | filep->private_data = NULL; |
| 977 | |
| 978 | vfio_container_put(container); |
| 979 | |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | /* |
| 984 | * Once an iommu driver is set, we optionally pass read/write/mmap |
| 985 | * on to the driver, allowing management interfaces beyond ioctl. |
| 986 | */ |
| 987 | static ssize_t vfio_fops_read(struct file *filep, char __user *buf, |
| 988 | size_t count, loff_t *ppos) |
| 989 | { |
| 990 | struct vfio_container *container = filep->private_data; |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 991 | struct vfio_iommu_driver *driver; |
| 992 | ssize_t ret = -EINVAL; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 993 | |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 994 | down_read(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 995 | |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 996 | driver = container->iommu_driver; |
| 997 | if (likely(driver && driver->ops->read)) |
| 998 | ret = driver->ops->read(container->iommu_data, |
| 999 | buf, count, ppos); |
| 1000 | |
| 1001 | up_read(&container->group_lock); |
| 1002 | |
| 1003 | return ret; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | static ssize_t vfio_fops_write(struct file *filep, const char __user *buf, |
| 1007 | size_t count, loff_t *ppos) |
| 1008 | { |
| 1009 | struct vfio_container *container = filep->private_data; |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 1010 | struct vfio_iommu_driver *driver; |
| 1011 | ssize_t ret = -EINVAL; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1012 | |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 1013 | down_read(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1014 | |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 1015 | driver = container->iommu_driver; |
| 1016 | if (likely(driver && driver->ops->write)) |
| 1017 | ret = driver->ops->write(container->iommu_data, |
| 1018 | buf, count, ppos); |
| 1019 | |
| 1020 | up_read(&container->group_lock); |
| 1021 | |
| 1022 | return ret; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | static int vfio_fops_mmap(struct file *filep, struct vm_area_struct *vma) |
| 1026 | { |
| 1027 | struct vfio_container *container = filep->private_data; |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 1028 | struct vfio_iommu_driver *driver; |
| 1029 | int ret = -EINVAL; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1030 | |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 1031 | down_read(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1032 | |
Alex Williamson | 0b43c08 | 2013-04-29 08:41:36 -0600 | [diff] [blame] | 1033 | driver = container->iommu_driver; |
| 1034 | if (likely(driver && driver->ops->mmap)) |
| 1035 | ret = driver->ops->mmap(container->iommu_data, vma); |
| 1036 | |
| 1037 | up_read(&container->group_lock); |
| 1038 | |
| 1039 | return ret; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static const struct file_operations vfio_fops = { |
| 1043 | .owner = THIS_MODULE, |
| 1044 | .open = vfio_fops_open, |
| 1045 | .release = vfio_fops_release, |
| 1046 | .read = vfio_fops_read, |
| 1047 | .write = vfio_fops_write, |
| 1048 | .unlocked_ioctl = vfio_fops_unl_ioctl, |
| 1049 | #ifdef CONFIG_COMPAT |
| 1050 | .compat_ioctl = vfio_fops_compat_ioctl, |
| 1051 | #endif |
| 1052 | .mmap = vfio_fops_mmap, |
| 1053 | }; |
| 1054 | |
| 1055 | /** |
| 1056 | * VFIO Group fd, /dev/vfio/$GROUP |
| 1057 | */ |
| 1058 | static void __vfio_group_unset_container(struct vfio_group *group) |
| 1059 | { |
| 1060 | struct vfio_container *container = group->container; |
| 1061 | struct vfio_iommu_driver *driver; |
| 1062 | |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 1063 | down_write(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1064 | |
| 1065 | driver = container->iommu_driver; |
| 1066 | if (driver) |
| 1067 | driver->ops->detach_group(container->iommu_data, |
| 1068 | group->iommu_group); |
| 1069 | |
| 1070 | group->container = NULL; |
| 1071 | list_del(&group->container_next); |
| 1072 | |
| 1073 | /* Detaching the last group deprivileges a container, remove iommu */ |
| 1074 | if (driver && list_empty(&container->group_list)) { |
| 1075 | driver->ops->release(container->iommu_data); |
| 1076 | module_put(driver->ops->owner); |
| 1077 | container->iommu_driver = NULL; |
| 1078 | container->iommu_data = NULL; |
| 1079 | } |
| 1080 | |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 1081 | up_write(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1082 | |
| 1083 | vfio_container_put(container); |
| 1084 | } |
| 1085 | |
| 1086 | /* |
| 1087 | * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or |
| 1088 | * if there was no container to unset. Since the ioctl is called on |
| 1089 | * the group, we know that still exists, therefore the only valid |
| 1090 | * transition here is 1->0. |
| 1091 | */ |
| 1092 | static int vfio_group_unset_container(struct vfio_group *group) |
| 1093 | { |
| 1094 | int users = atomic_cmpxchg(&group->container_users, 1, 0); |
| 1095 | |
| 1096 | if (!users) |
| 1097 | return -EINVAL; |
| 1098 | if (users != 1) |
| 1099 | return -EBUSY; |
| 1100 | |
| 1101 | __vfio_group_unset_container(group); |
| 1102 | |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * When removing container users, anything that removes the last user |
| 1108 | * implicitly removes the group from the container. That is, if the |
| 1109 | * group file descriptor is closed, as well as any device file descriptors, |
| 1110 | * the group is free. |
| 1111 | */ |
| 1112 | static void vfio_group_try_dissolve_container(struct vfio_group *group) |
| 1113 | { |
| 1114 | if (0 == atomic_dec_if_positive(&group->container_users)) |
| 1115 | __vfio_group_unset_container(group); |
| 1116 | } |
| 1117 | |
| 1118 | static int vfio_group_set_container(struct vfio_group *group, int container_fd) |
| 1119 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1120 | struct fd f; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1121 | struct vfio_container *container; |
| 1122 | struct vfio_iommu_driver *driver; |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1123 | int ret = 0; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1124 | |
| 1125 | if (atomic_read(&group->container_users)) |
| 1126 | return -EINVAL; |
| 1127 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1128 | f = fdget(container_fd); |
| 1129 | if (!f.file) |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1130 | return -EBADF; |
| 1131 | |
| 1132 | /* Sanity check, is this really our fd? */ |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1133 | if (f.file->f_op != &vfio_fops) { |
| 1134 | fdput(f); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1135 | return -EINVAL; |
| 1136 | } |
| 1137 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1138 | container = f.file->private_data; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1139 | WARN_ON(!container); /* fget ensures we don't race vfio_release */ |
| 1140 | |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 1141 | down_write(&container->group_lock); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1142 | |
| 1143 | driver = container->iommu_driver; |
| 1144 | if (driver) { |
| 1145 | ret = driver->ops->attach_group(container->iommu_data, |
| 1146 | group->iommu_group); |
| 1147 | if (ret) |
| 1148 | goto unlock_out; |
| 1149 | } |
| 1150 | |
| 1151 | group->container = container; |
| 1152 | list_add(&group->container_next, &container->group_list); |
| 1153 | |
| 1154 | /* Get a reference on the container and mark a user within the group */ |
| 1155 | vfio_container_get(container); |
| 1156 | atomic_inc(&group->container_users); |
| 1157 | |
| 1158 | unlock_out: |
Alex Williamson | 9587f44 | 2013-04-25 16:12:38 -0600 | [diff] [blame] | 1159 | up_write(&container->group_lock); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 1160 | fdput(f); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1161 | return ret; |
| 1162 | } |
| 1163 | |
| 1164 | static bool vfio_group_viable(struct vfio_group *group) |
| 1165 | { |
| 1166 | return (iommu_group_for_each_dev(group->iommu_group, |
| 1167 | group, vfio_dev_viable) == 0); |
| 1168 | } |
| 1169 | |
| 1170 | static const struct file_operations vfio_device_fops; |
| 1171 | |
| 1172 | static int vfio_group_get_device_fd(struct vfio_group *group, char *buf) |
| 1173 | { |
| 1174 | struct vfio_device *device; |
| 1175 | struct file *filep; |
| 1176 | int ret = -ENODEV; |
| 1177 | |
| 1178 | if (0 == atomic_read(&group->container_users) || |
| 1179 | !group->container->iommu_driver || !vfio_group_viable(group)) |
| 1180 | return -EINVAL; |
| 1181 | |
| 1182 | mutex_lock(&group->device_lock); |
| 1183 | list_for_each_entry(device, &group->device_list, group_next) { |
| 1184 | if (strcmp(dev_name(device->dev), buf)) |
| 1185 | continue; |
| 1186 | |
| 1187 | ret = device->ops->open(device->device_data); |
| 1188 | if (ret) |
| 1189 | break; |
| 1190 | /* |
| 1191 | * We can't use anon_inode_getfd() because we need to modify |
| 1192 | * the f_mode flags directly to allow more than just ioctls |
| 1193 | */ |
Alex Williamson | 5d042fb | 2013-08-22 10:33:41 -0600 | [diff] [blame] | 1194 | ret = get_unused_fd_flags(O_CLOEXEC); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1195 | if (ret < 0) { |
| 1196 | device->ops->release(device->device_data); |
| 1197 | break; |
| 1198 | } |
| 1199 | |
| 1200 | filep = anon_inode_getfile("[vfio-device]", &vfio_device_fops, |
| 1201 | device, O_RDWR); |
| 1202 | if (IS_ERR(filep)) { |
| 1203 | put_unused_fd(ret); |
| 1204 | ret = PTR_ERR(filep); |
| 1205 | device->ops->release(device->device_data); |
| 1206 | break; |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * TODO: add an anon_inode interface to do this. |
| 1211 | * Appears to be missing by lack of need rather than |
| 1212 | * explicitly prevented. Now there's need. |
| 1213 | */ |
| 1214 | filep->f_mode |= (FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE); |
| 1215 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1216 | vfio_device_get(device); |
| 1217 | atomic_inc(&group->container_users); |
Al Viro | 31605de | 2012-08-17 21:32:56 -0400 | [diff] [blame] | 1218 | |
| 1219 | fd_install(ret, filep); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1220 | break; |
| 1221 | } |
| 1222 | mutex_unlock(&group->device_lock); |
| 1223 | |
| 1224 | return ret; |
| 1225 | } |
| 1226 | |
| 1227 | static long vfio_group_fops_unl_ioctl(struct file *filep, |
| 1228 | unsigned int cmd, unsigned long arg) |
| 1229 | { |
| 1230 | struct vfio_group *group = filep->private_data; |
| 1231 | long ret = -ENOTTY; |
| 1232 | |
| 1233 | switch (cmd) { |
| 1234 | case VFIO_GROUP_GET_STATUS: |
| 1235 | { |
| 1236 | struct vfio_group_status status; |
| 1237 | unsigned long minsz; |
| 1238 | |
| 1239 | minsz = offsetofend(struct vfio_group_status, flags); |
| 1240 | |
| 1241 | if (copy_from_user(&status, (void __user *)arg, minsz)) |
| 1242 | return -EFAULT; |
| 1243 | |
| 1244 | if (status.argsz < minsz) |
| 1245 | return -EINVAL; |
| 1246 | |
| 1247 | status.flags = 0; |
| 1248 | |
| 1249 | if (vfio_group_viable(group)) |
| 1250 | status.flags |= VFIO_GROUP_FLAGS_VIABLE; |
| 1251 | |
| 1252 | if (group->container) |
| 1253 | status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET; |
| 1254 | |
| 1255 | if (copy_to_user((void __user *)arg, &status, minsz)) |
| 1256 | return -EFAULT; |
| 1257 | |
| 1258 | ret = 0; |
| 1259 | break; |
| 1260 | } |
| 1261 | case VFIO_GROUP_SET_CONTAINER: |
| 1262 | { |
| 1263 | int fd; |
| 1264 | |
| 1265 | if (get_user(fd, (int __user *)arg)) |
| 1266 | return -EFAULT; |
| 1267 | |
| 1268 | if (fd < 0) |
| 1269 | return -EINVAL; |
| 1270 | |
| 1271 | ret = vfio_group_set_container(group, fd); |
| 1272 | break; |
| 1273 | } |
| 1274 | case VFIO_GROUP_UNSET_CONTAINER: |
| 1275 | ret = vfio_group_unset_container(group); |
| 1276 | break; |
| 1277 | case VFIO_GROUP_GET_DEVICE_FD: |
| 1278 | { |
| 1279 | char *buf; |
| 1280 | |
| 1281 | buf = strndup_user((const char __user *)arg, PAGE_SIZE); |
| 1282 | if (IS_ERR(buf)) |
| 1283 | return PTR_ERR(buf); |
| 1284 | |
| 1285 | ret = vfio_group_get_device_fd(group, buf); |
| 1286 | kfree(buf); |
| 1287 | break; |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | return ret; |
| 1292 | } |
| 1293 | |
| 1294 | #ifdef CONFIG_COMPAT |
| 1295 | static long vfio_group_fops_compat_ioctl(struct file *filep, |
| 1296 | unsigned int cmd, unsigned long arg) |
| 1297 | { |
| 1298 | arg = (unsigned long)compat_ptr(arg); |
| 1299 | return vfio_group_fops_unl_ioctl(filep, cmd, arg); |
| 1300 | } |
| 1301 | #endif /* CONFIG_COMPAT */ |
| 1302 | |
| 1303 | static int vfio_group_fops_open(struct inode *inode, struct file *filep) |
| 1304 | { |
| 1305 | struct vfio_group *group; |
Alex Williamson | 6d6768c | 2013-06-25 16:06:54 -0600 | [diff] [blame] | 1306 | int opened; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1307 | |
| 1308 | group = vfio_group_get_from_minor(iminor(inode)); |
| 1309 | if (!group) |
| 1310 | return -ENODEV; |
| 1311 | |
Alex Williamson | 6d6768c | 2013-06-25 16:06:54 -0600 | [diff] [blame] | 1312 | /* Do we need multiple instances of the group open? Seems not. */ |
| 1313 | opened = atomic_cmpxchg(&group->opened, 0, 1); |
| 1314 | if (opened) { |
| 1315 | vfio_group_put(group); |
| 1316 | return -EBUSY; |
| 1317 | } |
| 1318 | |
| 1319 | /* Is something still in use from a previous open? */ |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1320 | if (group->container) { |
Alex Williamson | 6d6768c | 2013-06-25 16:06:54 -0600 | [diff] [blame] | 1321 | atomic_dec(&group->opened); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1322 | vfio_group_put(group); |
| 1323 | return -EBUSY; |
| 1324 | } |
| 1325 | |
| 1326 | filep->private_data = group; |
| 1327 | |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | static int vfio_group_fops_release(struct inode *inode, struct file *filep) |
| 1332 | { |
| 1333 | struct vfio_group *group = filep->private_data; |
| 1334 | |
| 1335 | filep->private_data = NULL; |
| 1336 | |
| 1337 | vfio_group_try_dissolve_container(group); |
| 1338 | |
Alex Williamson | 6d6768c | 2013-06-25 16:06:54 -0600 | [diff] [blame] | 1339 | atomic_dec(&group->opened); |
| 1340 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1341 | vfio_group_put(group); |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | static const struct file_operations vfio_group_fops = { |
| 1347 | .owner = THIS_MODULE, |
| 1348 | .unlocked_ioctl = vfio_group_fops_unl_ioctl, |
| 1349 | #ifdef CONFIG_COMPAT |
| 1350 | .compat_ioctl = vfio_group_fops_compat_ioctl, |
| 1351 | #endif |
| 1352 | .open = vfio_group_fops_open, |
| 1353 | .release = vfio_group_fops_release, |
| 1354 | }; |
| 1355 | |
| 1356 | /** |
| 1357 | * VFIO Device fd |
| 1358 | */ |
| 1359 | static int vfio_device_fops_release(struct inode *inode, struct file *filep) |
| 1360 | { |
| 1361 | struct vfio_device *device = filep->private_data; |
| 1362 | |
| 1363 | device->ops->release(device->device_data); |
| 1364 | |
| 1365 | vfio_group_try_dissolve_container(device->group); |
| 1366 | |
| 1367 | vfio_device_put(device); |
| 1368 | |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | static long vfio_device_fops_unl_ioctl(struct file *filep, |
| 1373 | unsigned int cmd, unsigned long arg) |
| 1374 | { |
| 1375 | struct vfio_device *device = filep->private_data; |
| 1376 | |
| 1377 | if (unlikely(!device->ops->ioctl)) |
| 1378 | return -EINVAL; |
| 1379 | |
| 1380 | return device->ops->ioctl(device->device_data, cmd, arg); |
| 1381 | } |
| 1382 | |
| 1383 | static ssize_t vfio_device_fops_read(struct file *filep, char __user *buf, |
| 1384 | size_t count, loff_t *ppos) |
| 1385 | { |
| 1386 | struct vfio_device *device = filep->private_data; |
| 1387 | |
| 1388 | if (unlikely(!device->ops->read)) |
| 1389 | return -EINVAL; |
| 1390 | |
| 1391 | return device->ops->read(device->device_data, buf, count, ppos); |
| 1392 | } |
| 1393 | |
| 1394 | static ssize_t vfio_device_fops_write(struct file *filep, |
| 1395 | const char __user *buf, |
| 1396 | size_t count, loff_t *ppos) |
| 1397 | { |
| 1398 | struct vfio_device *device = filep->private_data; |
| 1399 | |
| 1400 | if (unlikely(!device->ops->write)) |
| 1401 | return -EINVAL; |
| 1402 | |
| 1403 | return device->ops->write(device->device_data, buf, count, ppos); |
| 1404 | } |
| 1405 | |
| 1406 | static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma) |
| 1407 | { |
| 1408 | struct vfio_device *device = filep->private_data; |
| 1409 | |
| 1410 | if (unlikely(!device->ops->mmap)) |
| 1411 | return -EINVAL; |
| 1412 | |
| 1413 | return device->ops->mmap(device->device_data, vma); |
| 1414 | } |
| 1415 | |
| 1416 | #ifdef CONFIG_COMPAT |
| 1417 | static long vfio_device_fops_compat_ioctl(struct file *filep, |
| 1418 | unsigned int cmd, unsigned long arg) |
| 1419 | { |
| 1420 | arg = (unsigned long)compat_ptr(arg); |
| 1421 | return vfio_device_fops_unl_ioctl(filep, cmd, arg); |
| 1422 | } |
| 1423 | #endif /* CONFIG_COMPAT */ |
| 1424 | |
| 1425 | static const struct file_operations vfio_device_fops = { |
| 1426 | .owner = THIS_MODULE, |
| 1427 | .release = vfio_device_fops_release, |
| 1428 | .read = vfio_device_fops_read, |
| 1429 | .write = vfio_device_fops_write, |
| 1430 | .unlocked_ioctl = vfio_device_fops_unl_ioctl, |
| 1431 | #ifdef CONFIG_COMPAT |
| 1432 | .compat_ioctl = vfio_device_fops_compat_ioctl, |
| 1433 | #endif |
| 1434 | .mmap = vfio_device_fops_mmap, |
| 1435 | }; |
| 1436 | |
| 1437 | /** |
Alexey Kardashevskiy | 6cdd978 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 1438 | * External user API, exported by symbols to be linked dynamically. |
| 1439 | * |
| 1440 | * The protocol includes: |
| 1441 | * 1. do normal VFIO init operation: |
| 1442 | * - opening a new container; |
| 1443 | * - attaching group(s) to it; |
| 1444 | * - setting an IOMMU driver for a container. |
| 1445 | * When IOMMU is set for a container, all groups in it are |
| 1446 | * considered ready to use by an external user. |
| 1447 | * |
| 1448 | * 2. User space passes a group fd to an external user. |
| 1449 | * The external user calls vfio_group_get_external_user() |
| 1450 | * to verify that: |
| 1451 | * - the group is initialized; |
| 1452 | * - IOMMU is set for it. |
| 1453 | * If both checks passed, vfio_group_get_external_user() |
| 1454 | * increments the container user counter to prevent |
| 1455 | * the VFIO group from disposal before KVM exits. |
| 1456 | * |
| 1457 | * 3. The external user calls vfio_external_user_iommu_id() |
| 1458 | * to know an IOMMU ID. |
| 1459 | * |
| 1460 | * 4. When the external KVM finishes, it calls |
| 1461 | * vfio_group_put_external_user() to release the VFIO group. |
| 1462 | * This call decrements the container user counter. |
| 1463 | */ |
| 1464 | struct vfio_group *vfio_group_get_external_user(struct file *filep) |
| 1465 | { |
| 1466 | struct vfio_group *group = filep->private_data; |
| 1467 | |
| 1468 | if (filep->f_op != &vfio_group_fops) |
| 1469 | return ERR_PTR(-EINVAL); |
| 1470 | |
| 1471 | if (!atomic_inc_not_zero(&group->container_users)) |
| 1472 | return ERR_PTR(-EINVAL); |
| 1473 | |
| 1474 | if (!group->container->iommu_driver || |
| 1475 | !vfio_group_viable(group)) { |
| 1476 | atomic_dec(&group->container_users); |
| 1477 | return ERR_PTR(-EINVAL); |
| 1478 | } |
| 1479 | |
| 1480 | vfio_group_get(group); |
| 1481 | |
| 1482 | return group; |
| 1483 | } |
| 1484 | EXPORT_SYMBOL_GPL(vfio_group_get_external_user); |
| 1485 | |
| 1486 | void vfio_group_put_external_user(struct vfio_group *group) |
| 1487 | { |
| 1488 | vfio_group_put(group); |
| 1489 | vfio_group_try_dissolve_container(group); |
| 1490 | } |
| 1491 | EXPORT_SYMBOL_GPL(vfio_group_put_external_user); |
| 1492 | |
| 1493 | int vfio_external_user_iommu_id(struct vfio_group *group) |
| 1494 | { |
| 1495 | return iommu_group_id(group->iommu_group); |
| 1496 | } |
| 1497 | EXPORT_SYMBOL_GPL(vfio_external_user_iommu_id); |
| 1498 | |
Alex Williamson | 88d7ab8 | 2014-02-26 11:38:39 -0700 | [diff] [blame] | 1499 | long vfio_external_check_extension(struct vfio_group *group, unsigned long arg) |
| 1500 | { |
| 1501 | return vfio_ioctl_check_extension(group->container, arg); |
| 1502 | } |
| 1503 | EXPORT_SYMBOL_GPL(vfio_external_check_extension); |
| 1504 | |
Alexey Kardashevskiy | 6cdd978 | 2013-08-05 10:52:36 -0600 | [diff] [blame] | 1505 | /** |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1506 | * Module/class support |
| 1507 | */ |
| 1508 | static char *vfio_devnode(struct device *dev, umode_t *mode) |
| 1509 | { |
| 1510 | return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev)); |
| 1511 | } |
| 1512 | |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1513 | static struct miscdevice vfio_dev = { |
| 1514 | .minor = VFIO_MINOR, |
| 1515 | .name = "vfio", |
| 1516 | .fops = &vfio_fops, |
| 1517 | .nodename = "vfio/vfio", |
| 1518 | .mode = S_IRUGO | S_IWUGO, |
| 1519 | }; |
| 1520 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1521 | static int __init vfio_init(void) |
| 1522 | { |
| 1523 | int ret; |
| 1524 | |
| 1525 | idr_init(&vfio.group_idr); |
| 1526 | mutex_init(&vfio.group_lock); |
| 1527 | mutex_init(&vfio.iommu_drivers_lock); |
| 1528 | INIT_LIST_HEAD(&vfio.group_list); |
| 1529 | INIT_LIST_HEAD(&vfio.iommu_drivers_list); |
| 1530 | init_waitqueue_head(&vfio.release_q); |
| 1531 | |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1532 | ret = misc_register(&vfio_dev); |
| 1533 | if (ret) { |
| 1534 | pr_err("vfio: misc device register failed\n"); |
| 1535 | return ret; |
| 1536 | } |
| 1537 | |
| 1538 | /* /dev/vfio/$GROUP */ |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1539 | vfio.class = class_create(THIS_MODULE, "vfio"); |
| 1540 | if (IS_ERR(vfio.class)) { |
| 1541 | ret = PTR_ERR(vfio.class); |
| 1542 | goto err_class; |
| 1543 | } |
| 1544 | |
| 1545 | vfio.class->devnode = vfio_devnode; |
| 1546 | |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1547 | ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK, "vfio"); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1548 | if (ret) |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1549 | goto err_alloc_chrdev; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1550 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1551 | cdev_init(&vfio.group_cdev, &vfio_group_fops); |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1552 | ret = cdev_add(&vfio.group_cdev, vfio.group_devt, MINORMASK); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1553 | if (ret) |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1554 | goto err_cdev_add; |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1555 | |
| 1556 | pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); |
| 1557 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 1558 | /* |
| 1559 | * Attempt to load known iommu-drivers. This gives us a working |
| 1560 | * environment without the user needing to explicitly load iommu |
| 1561 | * drivers. |
| 1562 | */ |
| 1563 | request_module_nowait("vfio_iommu_type1"); |
Alexey Kardashevskiy | 5ffd229 | 2013-05-21 13:33:10 +1000 | [diff] [blame] | 1564 | request_module_nowait("vfio_iommu_spapr_tce"); |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 1565 | |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1566 | return 0; |
| 1567 | |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1568 | err_cdev_add: |
| 1569 | unregister_chrdev_region(vfio.group_devt, MINORMASK); |
| 1570 | err_alloc_chrdev: |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1571 | class_destroy(vfio.class); |
| 1572 | vfio.class = NULL; |
| 1573 | err_class: |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1574 | misc_deregister(&vfio_dev); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1575 | return ret; |
| 1576 | } |
| 1577 | |
| 1578 | static void __exit vfio_cleanup(void) |
| 1579 | { |
| 1580 | WARN_ON(!list_empty(&vfio.group_list)); |
| 1581 | |
| 1582 | idr_destroy(&vfio.group_idr); |
| 1583 | cdev_del(&vfio.group_cdev); |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1584 | unregister_chrdev_region(vfio.group_devt, MINORMASK); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1585 | class_destroy(vfio.class); |
| 1586 | vfio.class = NULL; |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1587 | misc_deregister(&vfio_dev); |
Alex Williamson | cba3345 | 2012-07-31 08:16:22 -0600 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | module_init(vfio_init); |
| 1591 | module_exit(vfio_cleanup); |
| 1592 | |
| 1593 | MODULE_VERSION(DRIVER_VERSION); |
| 1594 | MODULE_LICENSE("GPL v2"); |
| 1595 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1596 | MODULE_DESCRIPTION(DRIVER_DESC); |
Alex Williamson | d109990 | 2013-12-19 10:17:13 -0700 | [diff] [blame] | 1597 | MODULE_ALIAS_MISCDEV(VFIO_MINOR); |
| 1598 | MODULE_ALIAS("devname:vfio/vfio"); |