• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2021 Intel Corporation
3  * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
4  *
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.
8  */
9 #define pr_fmt(fmt) "iommufd: " fmt
10 
11 #include <linux/bug.h>
12 #include <linux/file.h>
13 #include <linux/fs.h>
14 #include <linux/iommufd.h>
15 #include <linux/miscdevice.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <uapi/linux/iommufd.h>
20 
21 #include "io_pagetable.h"
22 #include "iommufd_private.h"
23 #include "iommufd_test.h"
24 
25 struct iommufd_object_ops {
26 	size_t file_offset;
27 	void (*destroy)(struct iommufd_object *obj);
28 	void (*abort)(struct iommufd_object *obj);
29 };
30 static const struct iommufd_object_ops iommufd_object_ops[];
31 static struct miscdevice vfio_misc_dev;
32 
_iommufd_object_alloc(struct iommufd_ctx * ictx,size_t size,enum iommufd_object_type type)33 struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
34 					     size_t size,
35 					     enum iommufd_object_type type)
36 {
37 	struct iommufd_object *obj;
38 	int rc;
39 
40 	obj = kzalloc(size, GFP_KERNEL_ACCOUNT);
41 	if (!obj)
42 		return ERR_PTR(-ENOMEM);
43 	obj->type = type;
44 	/* Starts out bias'd by 1 until it is removed from the xarray */
45 	refcount_set(&obj->shortterm_users, 1);
46 	refcount_set(&obj->users, 1);
47 
48 	/*
49 	 * Reserve an ID in the xarray but do not publish the pointer yet since
50 	 * the caller hasn't initialized it yet. Once the pointer is published
51 	 * in the xarray and visible to other threads we can't reliably destroy
52 	 * it anymore, so the caller must complete all errorable operations
53 	 * before calling iommufd_object_finalize().
54 	 */
55 	rc = xa_alloc(&ictx->objects, &obj->id, XA_ZERO_ENTRY,
56 		      xa_limit_31b, GFP_KERNEL_ACCOUNT);
57 	if (rc)
58 		goto out_free;
59 	return obj;
60 out_free:
61 	kfree(obj);
62 	return ERR_PTR(rc);
63 }
64 
65 /*
66  * Allow concurrent access to the object.
67  *
68  * Once another thread can see the object pointer it can prevent object
69  * destruction. Expect for special kernel-only objects there is no in-kernel way
70  * to reliably destroy a single object. Thus all APIs that are creating objects
71  * must use iommufd_object_abort() to handle their errors and only call
72  * iommufd_object_finalize() once object creation cannot fail.
73  */
iommufd_object_finalize(struct iommufd_ctx * ictx,struct iommufd_object * obj)74 void iommufd_object_finalize(struct iommufd_ctx *ictx,
75 			     struct iommufd_object *obj)
76 {
77 	void *old;
78 
79 	old = xa_store(&ictx->objects, obj->id, obj, GFP_KERNEL);
80 	/* obj->id was returned from xa_alloc() so the xa_store() cannot fail */
81 	WARN_ON(old);
82 }
83 
84 /* Undo _iommufd_object_alloc() if iommufd_object_finalize() was not called */
iommufd_object_abort(struct iommufd_ctx * ictx,struct iommufd_object * obj)85 void iommufd_object_abort(struct iommufd_ctx *ictx, struct iommufd_object *obj)
86 {
87 	void *old;
88 
89 	old = xa_erase(&ictx->objects, obj->id);
90 	WARN_ON(old);
91 	kfree(obj);
92 }
93 
94 /*
95  * Abort an object that has been fully initialized and needs destroy, but has
96  * not been finalized.
97  */
iommufd_object_abort_and_destroy(struct iommufd_ctx * ictx,struct iommufd_object * obj)98 void iommufd_object_abort_and_destroy(struct iommufd_ctx *ictx,
99 				      struct iommufd_object *obj)
100 {
101 	const struct iommufd_object_ops *ops = &iommufd_object_ops[obj->type];
102 
103 	if (ops->file_offset) {
104 		struct file **filep = ((void *)obj) + ops->file_offset;
105 
106 		/*
107 		 * A file should hold a users refcount while the file is open
108 		 * and put it back in its release. The file should hold a
109 		 * pointer to obj in their private data. Normal fput() is
110 		 * deferred to a workqueue and can get out of order with the
111 		 * following kfree(obj). Using the sync version ensures the
112 		 * release happens immediately. During abort we require the file
113 		 * refcount is one at this point - meaning the object alloc
114 		 * function cannot do anything to allow another thread to take a
115 		 * refcount prior to a guaranteed success.
116 		 */
117 		if (*filep)
118 			__fput_sync(*filep);
119 	}
120 
121 	if (ops->abort)
122 		ops->abort(obj);
123 	else
124 		ops->destroy(obj);
125 	iommufd_object_abort(ictx, obj);
126 }
127 
iommufd_get_object(struct iommufd_ctx * ictx,u32 id,enum iommufd_object_type type)128 struct iommufd_object *iommufd_get_object(struct iommufd_ctx *ictx, u32 id,
129 					  enum iommufd_object_type type)
130 {
131 	struct iommufd_object *obj;
132 
133 	if (iommufd_should_fail())
134 		return ERR_PTR(-ENOENT);
135 
136 	xa_lock(&ictx->objects);
137 	obj = xa_load(&ictx->objects, id);
138 	if (!obj || (type != IOMMUFD_OBJ_ANY && obj->type != type) ||
139 	    !iommufd_lock_obj(obj))
140 		obj = ERR_PTR(-ENOENT);
141 	xa_unlock(&ictx->objects);
142 	return obj;
143 }
144 
iommufd_object_dec_wait_shortterm(struct iommufd_ctx * ictx,struct iommufd_object * to_destroy)145 static int iommufd_object_dec_wait_shortterm(struct iommufd_ctx *ictx,
146 					     struct iommufd_object *to_destroy)
147 {
148 	if (refcount_dec_and_test(&to_destroy->shortterm_users))
149 		return 0;
150 
151 	if (wait_event_timeout(ictx->destroy_wait,
152 				refcount_read(&to_destroy->shortterm_users) ==
153 					0,
154 				msecs_to_jiffies(60000)))
155 		return 0;
156 
157 	pr_crit("Time out waiting for iommufd object to become free\n");
158 	refcount_inc(&to_destroy->shortterm_users);
159 	return -EBUSY;
160 }
161 
162 /*
163  * Remove the given object id from the xarray if the only reference to the
164  * object is held by the xarray.
165  */
iommufd_object_remove(struct iommufd_ctx * ictx,struct iommufd_object * to_destroy,u32 id,unsigned int flags)166 int iommufd_object_remove(struct iommufd_ctx *ictx,
167 			  struct iommufd_object *to_destroy, u32 id,
168 			  unsigned int flags)
169 {
170 	struct iommufd_object *obj;
171 	XA_STATE(xas, &ictx->objects, id);
172 	bool zerod_shortterm = false;
173 	int ret;
174 
175 	/*
176 	 * The purpose of the shortterm_users is to ensure deterministic
177 	 * destruction of objects used by external drivers and destroyed by this
178 	 * function. Any temporary increment of the refcount must increment
179 	 * shortterm_users, such as during ioctl execution.
180 	 */
181 	if (flags & REMOVE_WAIT_SHORTTERM) {
182 		ret = iommufd_object_dec_wait_shortterm(ictx, to_destroy);
183 		if (ret) {
184 			/*
185 			 * We have a bug. Put back the callers reference and
186 			 * defer cleaning this object until close.
187 			 */
188 			refcount_dec(&to_destroy->users);
189 			return ret;
190 		}
191 		zerod_shortterm = true;
192 	}
193 
194 	xa_lock(&ictx->objects);
195 	obj = xas_load(&xas);
196 	if (to_destroy) {
197 		/*
198 		 * If the caller is holding a ref on obj we put it here under
199 		 * the spinlock.
200 		 */
201 		refcount_dec(&obj->users);
202 
203 		if (WARN_ON(obj != to_destroy)) {
204 			ret = -ENOENT;
205 			goto err_xa;
206 		}
207 	} else if (xa_is_zero(obj) || !obj) {
208 		ret = -ENOENT;
209 		goto err_xa;
210 	}
211 
212 	if (!refcount_dec_if_one(&obj->users)) {
213 		ret = -EBUSY;
214 		goto err_xa;
215 	}
216 
217 	xas_store(&xas, NULL);
218 	if (ictx->vfio_ioas == container_of(obj, struct iommufd_ioas, obj))
219 		ictx->vfio_ioas = NULL;
220 	xa_unlock(&ictx->objects);
221 
222 	/*
223 	 * Since users is zero any positive users_shortterm must be racing
224 	 * iommufd_put_object(), or we have a bug.
225 	 */
226 	if (!zerod_shortterm) {
227 		ret = iommufd_object_dec_wait_shortterm(ictx, obj);
228 		if (WARN_ON(ret))
229 			return ret;
230 	}
231 
232 	iommufd_object_ops[obj->type].destroy(obj);
233 	kfree(obj);
234 	return 0;
235 
236 err_xa:
237 	if (zerod_shortterm) {
238 		/* Restore the xarray owned reference */
239 		refcount_set(&obj->shortterm_users, 1);
240 	}
241 	xa_unlock(&ictx->objects);
242 
243 	/* The returned object reference count is zero */
244 	return ret;
245 }
246 
iommufd_destroy(struct iommufd_ucmd * ucmd)247 static int iommufd_destroy(struct iommufd_ucmd *ucmd)
248 {
249 	struct iommu_destroy *cmd = ucmd->cmd;
250 
251 	return iommufd_object_remove(ucmd->ictx, NULL, cmd->id, 0);
252 }
253 
iommufd_fops_open(struct inode * inode,struct file * filp)254 static int iommufd_fops_open(struct inode *inode, struct file *filp)
255 {
256 	struct iommufd_ctx *ictx;
257 
258 	ictx = kzalloc(sizeof(*ictx), GFP_KERNEL_ACCOUNT);
259 	if (!ictx)
260 		return -ENOMEM;
261 
262 	/*
263 	 * For compatibility with VFIO when /dev/vfio/vfio is opened we default
264 	 * to the same rlimit accounting as vfio uses.
265 	 */
266 	if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER) &&
267 	    filp->private_data == &vfio_misc_dev) {
268 		ictx->account_mode = IOPT_PAGES_ACCOUNT_MM;
269 		pr_info_once("IOMMUFD is providing /dev/vfio/vfio, not VFIO.\n");
270 	}
271 
272 	xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT);
273 	xa_init(&ictx->groups);
274 	ictx->file = filp;
275 	init_waitqueue_head(&ictx->destroy_wait);
276 	filp->private_data = ictx;
277 	return 0;
278 }
279 
iommufd_fops_release(struct inode * inode,struct file * filp)280 static int iommufd_fops_release(struct inode *inode, struct file *filp)
281 {
282 	struct iommufd_ctx *ictx = filp->private_data;
283 	struct iommufd_object *obj;
284 
285 	/*
286 	 * The objects in the xarray form a graph of "users" counts, and we have
287 	 * to destroy them in a depth first manner. Leaf objects will reduce the
288 	 * users count of interior objects when they are destroyed.
289 	 *
290 	 * Repeatedly destroying all the "1 users" leaf objects will progress
291 	 * until the entire list is destroyed. If this can't progress then there
292 	 * is some bug related to object refcounting.
293 	 */
294 	while (!xa_empty(&ictx->objects)) {
295 		unsigned int destroyed = 0;
296 		unsigned long index;
297 
298 		xa_for_each(&ictx->objects, index, obj) {
299 			if (!refcount_dec_if_one(&obj->users))
300 				continue;
301 			destroyed++;
302 			xa_erase(&ictx->objects, index);
303 			iommufd_object_ops[obj->type].destroy(obj);
304 			kfree(obj);
305 		}
306 		/* Bug related to users refcount */
307 		if (WARN_ON(!destroyed))
308 			break;
309 	}
310 	WARN_ON(!xa_empty(&ictx->groups));
311 	kfree(ictx);
312 	return 0;
313 }
314 
iommufd_option(struct iommufd_ucmd * ucmd)315 static int iommufd_option(struct iommufd_ucmd *ucmd)
316 {
317 	struct iommu_option *cmd = ucmd->cmd;
318 	int rc;
319 
320 	if (cmd->__reserved)
321 		return -EOPNOTSUPP;
322 
323 	switch (cmd->option_id) {
324 	case IOMMU_OPTION_RLIMIT_MODE:
325 		rc = iommufd_option_rlimit_mode(cmd, ucmd->ictx);
326 		break;
327 	case IOMMU_OPTION_HUGE_PAGES:
328 		rc = iommufd_ioas_option(ucmd);
329 		break;
330 	default:
331 		return -EOPNOTSUPP;
332 	}
333 	if (rc)
334 		return rc;
335 	if (copy_to_user(&((struct iommu_option __user *)ucmd->ubuffer)->val64,
336 			 &cmd->val64, sizeof(cmd->val64)))
337 		return -EFAULT;
338 	return 0;
339 }
340 
341 union ucmd_buffer {
342 	struct iommu_destroy destroy;
343 	struct iommu_fault_alloc fault;
344 	struct iommu_hw_info info;
345 	struct iommu_hwpt_alloc hwpt;
346 	struct iommu_hwpt_get_dirty_bitmap get_dirty_bitmap;
347 	struct iommu_hwpt_invalidate cache;
348 	struct iommu_hwpt_set_dirty_tracking set_dirty_tracking;
349 	struct iommu_ioas_alloc alloc;
350 	struct iommu_ioas_allow_iovas allow_iovas;
351 	struct iommu_ioas_copy ioas_copy;
352 	struct iommu_ioas_iova_ranges iova_ranges;
353 	struct iommu_ioas_map map;
354 	struct iommu_ioas_unmap unmap;
355 	struct iommu_option option;
356 	struct iommu_vfio_ioas vfio_ioas;
357 #ifdef CONFIG_IOMMUFD_TEST
358 	struct iommu_test_cmd test;
359 #endif
360 };
361 
362 struct iommufd_ioctl_op {
363 	unsigned int size;
364 	unsigned int min_size;
365 	unsigned int ioctl_num;
366 	int (*execute)(struct iommufd_ucmd *ucmd);
367 };
368 
369 #define IOCTL_OP(_ioctl, _fn, _struct, _last)                                  \
370 	[_IOC_NR(_ioctl) - IOMMUFD_CMD_BASE] = {                               \
371 		.size = sizeof(_struct) +                                      \
372 			BUILD_BUG_ON_ZERO(sizeof(union ucmd_buffer) <          \
373 					  sizeof(_struct)),                    \
374 		.min_size = offsetofend(_struct, _last),                       \
375 		.ioctl_num = _ioctl,                                           \
376 		.execute = _fn,                                                \
377 	}
378 static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
379 	IOCTL_OP(IOMMU_DESTROY, iommufd_destroy, struct iommu_destroy, id),
380 	IOCTL_OP(IOMMU_FAULT_QUEUE_ALLOC, iommufd_fault_alloc, struct iommu_fault_alloc,
381 		 out_fault_fd),
382 	IOCTL_OP(IOMMU_GET_HW_INFO, iommufd_get_hw_info, struct iommu_hw_info,
383 		 __reserved),
384 	IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc,
385 		 __reserved),
386 	IOCTL_OP(IOMMU_HWPT_GET_DIRTY_BITMAP, iommufd_hwpt_get_dirty_bitmap,
387 		 struct iommu_hwpt_get_dirty_bitmap, data),
388 	IOCTL_OP(IOMMU_HWPT_INVALIDATE, iommufd_hwpt_invalidate,
389 		 struct iommu_hwpt_invalidate, __reserved),
390 	IOCTL_OP(IOMMU_HWPT_SET_DIRTY_TRACKING, iommufd_hwpt_set_dirty_tracking,
391 		 struct iommu_hwpt_set_dirty_tracking, __reserved),
392 	IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl,
393 		 struct iommu_ioas_alloc, out_ioas_id),
394 	IOCTL_OP(IOMMU_IOAS_ALLOW_IOVAS, iommufd_ioas_allow_iovas,
395 		 struct iommu_ioas_allow_iovas, allowed_iovas),
396 	IOCTL_OP(IOMMU_IOAS_COPY, iommufd_ioas_copy, struct iommu_ioas_copy,
397 		 src_iova),
398 	IOCTL_OP(IOMMU_IOAS_IOVA_RANGES, iommufd_ioas_iova_ranges,
399 		 struct iommu_ioas_iova_ranges, out_iova_alignment),
400 	IOCTL_OP(IOMMU_IOAS_MAP, iommufd_ioas_map, struct iommu_ioas_map,
401 		 iova),
402 	IOCTL_OP(IOMMU_IOAS_UNMAP, iommufd_ioas_unmap, struct iommu_ioas_unmap,
403 		 length),
404 	IOCTL_OP(IOMMU_OPTION, iommufd_option, struct iommu_option,
405 		 val64),
406 	IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct iommu_vfio_ioas,
407 		 __reserved),
408 #ifdef CONFIG_IOMMUFD_TEST
409 	IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
410 #endif
411 };
412 
iommufd_fops_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)413 static long iommufd_fops_ioctl(struct file *filp, unsigned int cmd,
414 			       unsigned long arg)
415 {
416 	struct iommufd_ctx *ictx = filp->private_data;
417 	const struct iommufd_ioctl_op *op;
418 	struct iommufd_ucmd ucmd = {};
419 	union ucmd_buffer buf;
420 	unsigned int nr;
421 	int ret;
422 
423 	nr = _IOC_NR(cmd);
424 	if (nr < IOMMUFD_CMD_BASE ||
425 	    (nr - IOMMUFD_CMD_BASE) >= ARRAY_SIZE(iommufd_ioctl_ops))
426 		return iommufd_vfio_ioctl(ictx, cmd, arg);
427 
428 	ucmd.ictx = ictx;
429 	ucmd.ubuffer = (void __user *)arg;
430 	ret = get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer);
431 	if (ret)
432 		return ret;
433 
434 	op = &iommufd_ioctl_ops[nr - IOMMUFD_CMD_BASE];
435 	if (op->ioctl_num != cmd)
436 		return -ENOIOCTLCMD;
437 	if (ucmd.user_size < op->min_size)
438 		return -EINVAL;
439 
440 	ucmd.cmd = &buf;
441 	ret = copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer,
442 				    ucmd.user_size);
443 	if (ret)
444 		return ret;
445 	ret = op->execute(&ucmd);
446 	return ret;
447 }
448 
449 static const struct file_operations iommufd_fops = {
450 	.owner = THIS_MODULE,
451 	.open = iommufd_fops_open,
452 	.release = iommufd_fops_release,
453 	.unlocked_ioctl = iommufd_fops_ioctl,
454 };
455 
456 /**
457  * iommufd_ctx_get - Get a context reference
458  * @ictx: Context to get
459  *
460  * The caller must already hold a valid reference to ictx.
461  */
iommufd_ctx_get(struct iommufd_ctx * ictx)462 void iommufd_ctx_get(struct iommufd_ctx *ictx)
463 {
464 	get_file(ictx->file);
465 }
466 EXPORT_SYMBOL_NS_GPL(iommufd_ctx_get, IOMMUFD);
467 
468 /**
469  * iommufd_ctx_from_file - Acquires a reference to the iommufd context
470  * @file: File to obtain the reference from
471  *
472  * Returns a pointer to the iommufd_ctx, otherwise ERR_PTR. The struct file
473  * remains owned by the caller and the caller must still do fput. On success
474  * the caller is responsible to call iommufd_ctx_put().
475  */
iommufd_ctx_from_file(struct file * file)476 struct iommufd_ctx *iommufd_ctx_from_file(struct file *file)
477 {
478 	struct iommufd_ctx *ictx;
479 
480 	if (file->f_op != &iommufd_fops)
481 		return ERR_PTR(-EBADFD);
482 	ictx = file->private_data;
483 	iommufd_ctx_get(ictx);
484 	return ictx;
485 }
486 EXPORT_SYMBOL_NS_GPL(iommufd_ctx_from_file, IOMMUFD);
487 
488 /**
489  * iommufd_ctx_from_fd - Acquires a reference to the iommufd context
490  * @fd: File descriptor to obtain the reference from
491  *
492  * Returns a pointer to the iommufd_ctx, otherwise ERR_PTR. On success
493  * the caller is responsible to call iommufd_ctx_put().
494  */
iommufd_ctx_from_fd(int fd)495 struct iommufd_ctx *iommufd_ctx_from_fd(int fd)
496 {
497 	struct file *file;
498 
499 	file = fget(fd);
500 	if (!file)
501 		return ERR_PTR(-EBADF);
502 
503 	if (file->f_op != &iommufd_fops) {
504 		fput(file);
505 		return ERR_PTR(-EBADFD);
506 	}
507 	/* fget is the same as iommufd_ctx_get() */
508 	return file->private_data;
509 }
510 EXPORT_SYMBOL_NS_GPL(iommufd_ctx_from_fd, IOMMUFD);
511 
512 /**
513  * iommufd_ctx_put - Put back a reference
514  * @ictx: Context to put back
515  */
iommufd_ctx_put(struct iommufd_ctx * ictx)516 void iommufd_ctx_put(struct iommufd_ctx *ictx)
517 {
518 	fput(ictx->file);
519 }
520 EXPORT_SYMBOL_NS_GPL(iommufd_ctx_put, IOMMUFD);
521 
522 #define IOMMUFD_FILE_OFFSET(_struct, _filep, _obj)                           \
523 	.file_offset = (offsetof(_struct, _filep) +                          \
524 			BUILD_BUG_ON_ZERO(!__same_type(                      \
525 				struct file *, ((_struct *)NULL)->_filep)) + \
526 			BUILD_BUG_ON_ZERO(offsetof(_struct, _obj)))
527 
528 static const struct iommufd_object_ops iommufd_object_ops[] = {
529 	[IOMMUFD_OBJ_ACCESS] = {
530 		.destroy = iommufd_access_destroy_object,
531 	},
532 	[IOMMUFD_OBJ_DEVICE] = {
533 		.destroy = iommufd_device_destroy,
534 	},
535 	[IOMMUFD_OBJ_IOAS] = {
536 		.destroy = iommufd_ioas_destroy,
537 	},
538 	[IOMMUFD_OBJ_HWPT_PAGING] = {
539 		.destroy = iommufd_hwpt_paging_destroy,
540 		.abort = iommufd_hwpt_paging_abort,
541 	},
542 	[IOMMUFD_OBJ_HWPT_NESTED] = {
543 		.destroy = iommufd_hwpt_nested_destroy,
544 		.abort = iommufd_hwpt_nested_abort,
545 	},
546 	[IOMMUFD_OBJ_FAULT] = {
547 		.destroy = iommufd_fault_destroy,
548 		IOMMUFD_FILE_OFFSET(struct iommufd_fault, filep, obj),
549 	},
550 #ifdef CONFIG_IOMMUFD_TEST
551 	[IOMMUFD_OBJ_SELFTEST] = {
552 		.destroy = iommufd_selftest_destroy,
553 	},
554 #endif
555 };
556 
557 static struct miscdevice iommu_misc_dev = {
558 	.minor = MISC_DYNAMIC_MINOR,
559 	.name = "iommu",
560 	.fops = &iommufd_fops,
561 	.nodename = "iommu",
562 	.mode = 0660,
563 };
564 
565 
566 static struct miscdevice vfio_misc_dev = {
567 	.minor = VFIO_MINOR,
568 	.name = "vfio",
569 	.fops = &iommufd_fops,
570 	.nodename = "vfio/vfio",
571 	.mode = 0666,
572 };
573 
iommufd_init(void)574 static int __init iommufd_init(void)
575 {
576 	int ret;
577 
578 	ret = misc_register(&iommu_misc_dev);
579 	if (ret)
580 		return ret;
581 
582 	if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)) {
583 		ret = misc_register(&vfio_misc_dev);
584 		if (ret)
585 			goto err_misc;
586 	}
587 	ret = iommufd_test_init();
588 	if (ret)
589 		goto err_vfio_misc;
590 	return 0;
591 
592 err_vfio_misc:
593 	if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER))
594 		misc_deregister(&vfio_misc_dev);
595 err_misc:
596 	misc_deregister(&iommu_misc_dev);
597 	return ret;
598 }
599 
iommufd_exit(void)600 static void __exit iommufd_exit(void)
601 {
602 	iommufd_test_exit();
603 	if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER))
604 		misc_deregister(&vfio_misc_dev);
605 	misc_deregister(&iommu_misc_dev);
606 }
607 
608 module_init(iommufd_init);
609 module_exit(iommufd_exit);
610 
611 #if IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)
612 MODULE_ALIAS_MISCDEV(VFIO_MINOR);
613 MODULE_ALIAS("devname:vfio/vfio");
614 #endif
615 MODULE_IMPORT_NS(IOMMUFD_INTERNAL);
616 MODULE_IMPORT_NS(IOMMUFD);
617 MODULE_DESCRIPTION("I/O Address Space Management for passthrough devices");
618 MODULE_LICENSE("GPL");
619