1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2021 Intel Corporation
3 * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
5 * iommufd provides control over the IOMMU HW objects created by IOMMU kernel
6 * drivers. IOMMU HW objects revolve around IO page tables that map incoming DMA
7 * addresses (IOVA) to CPU addresses.
9 #define pr_fmt(fmt) "iommufd: " fmt
11 #include <linux/file.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/miscdevice.h>
16 #include <linux/mutex.h>
17 #include <linux/bug.h>
18 #include <uapi/linux/iommufd.h>
19 #include <linux/iommufd.h>
21 #include "io_pagetable.h"
22 #include "iommufd_private.h"
23 #include "iommufd_test.h"
25 struct iommufd_object_ops {
26 void (*destroy)(struct iommufd_object *obj);
27 void (*abort)(struct iommufd_object *obj);
29 static const struct iommufd_object_ops iommufd_object_ops[];
30 static struct miscdevice vfio_misc_dev;
32 struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
34 enum iommufd_object_type type)
36 static struct lock_class_key obj_keys[IOMMUFD_OBJ_MAX];
37 struct iommufd_object *obj;
40 obj = kzalloc(size, GFP_KERNEL_ACCOUNT);
42 return ERR_PTR(-ENOMEM);
45 * In most cases the destroy_rwsem is obtained with try so it doesn't
46 * interact with lockdep, however on destroy we have to sleep. This
47 * means if we have to destroy an object while holding a get on another
48 * object it triggers lockdep. Using one locking class per object type
49 * is a simple and reasonable way to avoid this.
51 __init_rwsem(&obj->destroy_rwsem, "iommufd_object::destroy_rwsem",
53 refcount_set(&obj->users, 1);
56 * Reserve an ID in the xarray but do not publish the pointer yet since
57 * the caller hasn't initialized it yet. Once the pointer is published
58 * in the xarray and visible to other threads we can't reliably destroy
59 * it anymore, so the caller must complete all errorable operations
60 * before calling iommufd_object_finalize().
62 rc = xa_alloc(&ictx->objects, &obj->id, XA_ZERO_ENTRY,
63 xa_limit_31b, GFP_KERNEL_ACCOUNT);
73 * Allow concurrent access to the object.
75 * Once another thread can see the object pointer it can prevent object
76 * destruction. Expect for special kernel-only objects there is no in-kernel way
77 * to reliably destroy a single object. Thus all APIs that are creating objects
78 * must use iommufd_object_abort() to handle their errors and only call
79 * iommufd_object_finalize() once object creation cannot fail.
81 void iommufd_object_finalize(struct iommufd_ctx *ictx,
82 struct iommufd_object *obj)
86 old = xa_store(&ictx->objects, obj->id, obj, GFP_KERNEL);
87 /* obj->id was returned from xa_alloc() so the xa_store() cannot fail */
91 /* Undo _iommufd_object_alloc() if iommufd_object_finalize() was not called */
92 void iommufd_object_abort(struct iommufd_ctx *ictx, struct iommufd_object *obj)
96 old = xa_erase(&ictx->objects, obj->id);
102 * Abort an object that has been fully initialized and needs destroy, but has
103 * not been finalized.
105 void iommufd_object_abort_and_destroy(struct iommufd_ctx *ictx,
106 struct iommufd_object *obj)
108 if (iommufd_object_ops[obj->type].abort)
109 iommufd_object_ops[obj->type].abort(obj);
111 iommufd_object_ops[obj->type].destroy(obj);
112 iommufd_object_abort(ictx, obj);
115 struct iommufd_object *iommufd_get_object(struct iommufd_ctx *ictx, u32 id,
116 enum iommufd_object_type type)
118 struct iommufd_object *obj;
120 if (iommufd_should_fail())
121 return ERR_PTR(-ENOENT);
123 xa_lock(&ictx->objects);
124 obj = xa_load(&ictx->objects, id);
125 if (!obj || (type != IOMMUFD_OBJ_ANY && obj->type != type) ||
126 !iommufd_lock_obj(obj))
127 obj = ERR_PTR(-ENOENT);
128 xa_unlock(&ictx->objects);
133 * Remove the given object id from the xarray if the only reference to the
134 * object is held by the xarray. The caller must call ops destroy().
136 static struct iommufd_object *iommufd_object_remove(struct iommufd_ctx *ictx,
137 u32 id, bool extra_put)
139 struct iommufd_object *obj;
140 XA_STATE(xas, &ictx->objects, id);
142 xa_lock(&ictx->objects);
143 obj = xas_load(&xas);
144 if (xa_is_zero(obj) || !obj) {
145 obj = ERR_PTR(-ENOENT);
150 * If the caller is holding a ref on obj we put it here under the
154 refcount_dec(&obj->users);
156 if (!refcount_dec_if_one(&obj->users)) {
157 obj = ERR_PTR(-EBUSY);
161 xas_store(&xas, NULL);
162 if (ictx->vfio_ioas == container_of(obj, struct iommufd_ioas, obj))
163 ictx->vfio_ioas = NULL;
166 xa_unlock(&ictx->objects);
168 /* The returned object reference count is zero */
173 * The caller holds a users refcount and wants to destroy the object. Returns
174 * true if the object was destroyed. In all cases the caller no longer has a
177 void __iommufd_object_destroy_user(struct iommufd_ctx *ictx,
178 struct iommufd_object *obj, bool allow_fail)
180 struct iommufd_object *ret;
183 * The purpose of the destroy_rwsem is to ensure deterministic
184 * destruction of objects used by external drivers and destroyed by this
185 * function. Any temporary increment of the refcount must hold the read
186 * side of this, such as during ioctl execution.
188 down_write(&obj->destroy_rwsem);
189 ret = iommufd_object_remove(ictx, obj->id, true);
190 up_write(&obj->destroy_rwsem);
192 if (allow_fail && IS_ERR(ret))
196 * If there is a bug and we couldn't destroy the object then we did put
197 * back the caller's refcount and will eventually try to free it again
200 if (WARN_ON(IS_ERR(ret)))
203 iommufd_object_ops[obj->type].destroy(obj);
207 static int iommufd_destroy(struct iommufd_ucmd *ucmd)
209 struct iommu_destroy *cmd = ucmd->cmd;
210 struct iommufd_object *obj;
212 obj = iommufd_object_remove(ucmd->ictx, cmd->id, false);
215 iommufd_object_ops[obj->type].destroy(obj);
220 static int iommufd_fops_open(struct inode *inode, struct file *filp)
222 struct iommufd_ctx *ictx;
224 ictx = kzalloc(sizeof(*ictx), GFP_KERNEL_ACCOUNT);
229 * For compatibility with VFIO when /dev/vfio/vfio is opened we default
230 * to the same rlimit accounting as vfio uses.
232 if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER) &&
233 filp->private_data == &vfio_misc_dev) {
234 ictx->account_mode = IOPT_PAGES_ACCOUNT_MM;
235 pr_info_once("IOMMUFD is providing /dev/vfio/vfio, not VFIO.\n");
238 xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT);
239 xa_init(&ictx->groups);
241 filp->private_data = ictx;
245 static int iommufd_fops_release(struct inode *inode, struct file *filp)
247 struct iommufd_ctx *ictx = filp->private_data;
248 struct iommufd_object *obj;
251 * The objects in the xarray form a graph of "users" counts, and we have
252 * to destroy them in a depth first manner. Leaf objects will reduce the
253 * users count of interior objects when they are destroyed.
255 * Repeatedly destroying all the "1 users" leaf objects will progress
256 * until the entire list is destroyed. If this can't progress then there
257 * is some bug related to object refcounting.
259 while (!xa_empty(&ictx->objects)) {
260 unsigned int destroyed = 0;
263 xa_for_each(&ictx->objects, index, obj) {
264 if (!refcount_dec_if_one(&obj->users))
267 xa_erase(&ictx->objects, index);
268 iommufd_object_ops[obj->type].destroy(obj);
271 /* Bug related to users refcount */
272 if (WARN_ON(!destroyed))
275 WARN_ON(!xa_empty(&ictx->groups));
280 static int iommufd_option(struct iommufd_ucmd *ucmd)
282 struct iommu_option *cmd = ucmd->cmd;
288 switch (cmd->option_id) {
289 case IOMMU_OPTION_RLIMIT_MODE:
290 rc = iommufd_option_rlimit_mode(cmd, ucmd->ictx);
292 case IOMMU_OPTION_HUGE_PAGES:
293 rc = iommufd_ioas_option(ucmd);
300 if (copy_to_user(&((struct iommu_option __user *)ucmd->ubuffer)->val64,
301 &cmd->val64, sizeof(cmd->val64)))
307 struct iommu_destroy destroy;
308 struct iommu_hw_info info;
309 struct iommu_hwpt_alloc hwpt;
310 struct iommu_ioas_alloc alloc;
311 struct iommu_ioas_allow_iovas allow_iovas;
312 struct iommu_ioas_copy ioas_copy;
313 struct iommu_ioas_iova_ranges iova_ranges;
314 struct iommu_ioas_map map;
315 struct iommu_ioas_unmap unmap;
316 struct iommu_option option;
317 struct iommu_vfio_ioas vfio_ioas;
318 #ifdef CONFIG_IOMMUFD_TEST
319 struct iommu_test_cmd test;
323 struct iommufd_ioctl_op {
325 unsigned int min_size;
326 unsigned int ioctl_num;
327 int (*execute)(struct iommufd_ucmd *ucmd);
330 #define IOCTL_OP(_ioctl, _fn, _struct, _last) \
331 [_IOC_NR(_ioctl) - IOMMUFD_CMD_BASE] = { \
332 .size = sizeof(_struct) + \
333 BUILD_BUG_ON_ZERO(sizeof(union ucmd_buffer) < \
335 .min_size = offsetofend(_struct, _last), \
336 .ioctl_num = _ioctl, \
339 static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
340 IOCTL_OP(IOMMU_DESTROY, iommufd_destroy, struct iommu_destroy, id),
341 IOCTL_OP(IOMMU_GET_HW_INFO, iommufd_get_hw_info, struct iommu_hw_info,
343 IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc,
345 IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl,
346 struct iommu_ioas_alloc, out_ioas_id),
347 IOCTL_OP(IOMMU_IOAS_ALLOW_IOVAS, iommufd_ioas_allow_iovas,
348 struct iommu_ioas_allow_iovas, allowed_iovas),
349 IOCTL_OP(IOMMU_IOAS_COPY, iommufd_ioas_copy, struct iommu_ioas_copy,
351 IOCTL_OP(IOMMU_IOAS_IOVA_RANGES, iommufd_ioas_iova_ranges,
352 struct iommu_ioas_iova_ranges, out_iova_alignment),
353 IOCTL_OP(IOMMU_IOAS_MAP, iommufd_ioas_map, struct iommu_ioas_map,
355 IOCTL_OP(IOMMU_IOAS_UNMAP, iommufd_ioas_unmap, struct iommu_ioas_unmap,
357 IOCTL_OP(IOMMU_OPTION, iommufd_option, struct iommu_option,
359 IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct iommu_vfio_ioas,
361 #ifdef CONFIG_IOMMUFD_TEST
362 IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
366 static long iommufd_fops_ioctl(struct file *filp, unsigned int cmd,
369 struct iommufd_ctx *ictx = filp->private_data;
370 const struct iommufd_ioctl_op *op;
371 struct iommufd_ucmd ucmd = {};
372 union ucmd_buffer buf;
377 if (nr < IOMMUFD_CMD_BASE ||
378 (nr - IOMMUFD_CMD_BASE) >= ARRAY_SIZE(iommufd_ioctl_ops))
379 return iommufd_vfio_ioctl(ictx, cmd, arg);
382 ucmd.ubuffer = (void __user *)arg;
383 ret = get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer);
387 op = &iommufd_ioctl_ops[nr - IOMMUFD_CMD_BASE];
388 if (op->ioctl_num != cmd)
390 if (ucmd.user_size < op->min_size)
394 ret = copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer,
398 ret = op->execute(&ucmd);
402 static const struct file_operations iommufd_fops = {
403 .owner = THIS_MODULE,
404 .open = iommufd_fops_open,
405 .release = iommufd_fops_release,
406 .unlocked_ioctl = iommufd_fops_ioctl,
410 * iommufd_ctx_get - Get a context reference
411 * @ictx: Context to get
413 * The caller must already hold a valid reference to ictx.
415 void iommufd_ctx_get(struct iommufd_ctx *ictx)
417 get_file(ictx->file);
419 EXPORT_SYMBOL_NS_GPL(iommufd_ctx_get, IOMMUFD);
422 * iommufd_ctx_from_file - Acquires a reference to the iommufd context
423 * @file: File to obtain the reference from
425 * Returns a pointer to the iommufd_ctx, otherwise ERR_PTR. The struct file
426 * remains owned by the caller and the caller must still do fput. On success
427 * the caller is responsible to call iommufd_ctx_put().
429 struct iommufd_ctx *iommufd_ctx_from_file(struct file *file)
431 struct iommufd_ctx *ictx;
433 if (file->f_op != &iommufd_fops)
434 return ERR_PTR(-EBADFD);
435 ictx = file->private_data;
436 iommufd_ctx_get(ictx);
439 EXPORT_SYMBOL_NS_GPL(iommufd_ctx_from_file, IOMMUFD);
442 * iommufd_ctx_from_fd - Acquires a reference to the iommufd context
443 * @fd: File descriptor to obtain the reference from
445 * Returns a pointer to the iommufd_ctx, otherwise ERR_PTR. On success
446 * the caller is responsible to call iommufd_ctx_put().
448 struct iommufd_ctx *iommufd_ctx_from_fd(int fd)
454 return ERR_PTR(-EBADF);
456 if (file->f_op != &iommufd_fops) {
458 return ERR_PTR(-EBADFD);
460 /* fget is the same as iommufd_ctx_get() */
461 return file->private_data;
463 EXPORT_SYMBOL_NS_GPL(iommufd_ctx_from_fd, IOMMUFD);
466 * iommufd_ctx_put - Put back a reference
467 * @ictx: Context to put back
469 void iommufd_ctx_put(struct iommufd_ctx *ictx)
473 EXPORT_SYMBOL_NS_GPL(iommufd_ctx_put, IOMMUFD);
475 static const struct iommufd_object_ops iommufd_object_ops[] = {
476 [IOMMUFD_OBJ_ACCESS] = {
477 .destroy = iommufd_access_destroy_object,
479 [IOMMUFD_OBJ_DEVICE] = {
480 .destroy = iommufd_device_destroy,
482 [IOMMUFD_OBJ_IOAS] = {
483 .destroy = iommufd_ioas_destroy,
485 [IOMMUFD_OBJ_HW_PAGETABLE] = {
486 .destroy = iommufd_hw_pagetable_destroy,
487 .abort = iommufd_hw_pagetable_abort,
489 #ifdef CONFIG_IOMMUFD_TEST
490 [IOMMUFD_OBJ_SELFTEST] = {
491 .destroy = iommufd_selftest_destroy,
496 static struct miscdevice iommu_misc_dev = {
497 .minor = MISC_DYNAMIC_MINOR,
499 .fops = &iommufd_fops,
505 static struct miscdevice vfio_misc_dev = {
508 .fops = &iommufd_fops,
509 .nodename = "vfio/vfio",
513 static int __init iommufd_init(void)
517 ret = misc_register(&iommu_misc_dev);
521 if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)) {
522 ret = misc_register(&vfio_misc_dev);
526 ret = iommufd_test_init();
532 if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER))
533 misc_deregister(&vfio_misc_dev);
535 misc_deregister(&iommu_misc_dev);
539 static void __exit iommufd_exit(void)
542 if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER))
543 misc_deregister(&vfio_misc_dev);
544 misc_deregister(&iommu_misc_dev);
547 module_init(iommufd_init);
548 module_exit(iommufd_exit);
550 #if IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)
551 MODULE_ALIAS_MISCDEV(VFIO_MINOR);
552 MODULE_ALIAS("devname:vfio/vfio");
554 MODULE_IMPORT_NS(IOMMUFD_INTERNAL);
555 MODULE_DESCRIPTION("I/O Address Space Management for passthrough devices");
556 MODULE_LICENSE("GPL");