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