1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VFIO: IOMMU DMA mapping support for Type1 IOMMU
4  *
5  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
6  *     Author: Alex Williamson <alex.williamson@redhat.com>
7  *
8  * Derived from original vfio:
9  * Copyright 2010 Cisco Systems, Inc.  All rights reserved.
10  * Author: Tom Lyon, pugs@cisco.com
11  *
12  * We arbitrarily define a Type1 IOMMU as one matching the below code.
13  * It could be called the x86 IOMMU as it's designed for AMD-Vi & Intel
14  * VT-d, but that makes it harder to re-use as theoretically anyone
15  * implementing a similar IOMMU could make use of this.  We expect the
16  * IOMMU to support the IOMMU API and have few to no restrictions around
17  * the IOVA range that can be mapped.  The Type1 IOMMU is currently
18  * optimized for relatively static mappings of a userspace process with
19  * userspace pages pinned into memory.  We also assume devices and IOMMU
20  * domains are PCI based as the IOMMU API is still centered around a
21  * device/bus interface rather than a group interface.
22  */
23 
24 #include <linux/compat.h>
25 #include <linux/device.h>
26 #include <linux/fs.h>
27 #include <linux/highmem.h>
28 #include <linux/iommu.h>
29 #include <linux/module.h>
30 #include <linux/mm.h>
31 #include <linux/kthread.h>
32 #include <linux/rbtree.h>
33 #include <linux/sched/signal.h>
34 #include <linux/sched/mm.h>
35 #include <linux/slab.h>
36 #include <linux/uaccess.h>
37 #include <linux/vfio.h>
38 #include <linux/workqueue.h>
39 #include <linux/notifier.h>
40 #include "vfio.h"
41 
42 #define DRIVER_VERSION  "0.2"
43 #define DRIVER_AUTHOR   "Alex Williamson <alex.williamson@redhat.com>"
44 #define DRIVER_DESC     "Type1 IOMMU driver for VFIO"
45 
46 static bool allow_unsafe_interrupts;
47 module_param_named(allow_unsafe_interrupts,
48 		   allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR);
49 MODULE_PARM_DESC(allow_unsafe_interrupts,
50 		 "Enable VFIO IOMMU support for on platforms without interrupt remapping support.");
51 
52 static bool disable_hugepages;
53 module_param_named(disable_hugepages,
54 		   disable_hugepages, bool, S_IRUGO | S_IWUSR);
55 MODULE_PARM_DESC(disable_hugepages,
56 		 "Disable VFIO IOMMU support for IOMMU hugepages.");
57 
58 static unsigned int dma_entry_limit __read_mostly = U16_MAX;
59 module_param_named(dma_entry_limit, dma_entry_limit, uint, 0644);
60 MODULE_PARM_DESC(dma_entry_limit,
61 		 "Maximum number of user DMA mappings per container (65535).");
62 
63 struct vfio_iommu {
64 	struct list_head	domain_list;
65 	struct list_head	iova_list;
66 	struct mutex		lock;
67 	struct rb_root		dma_list;
68 	struct list_head	device_list;
69 	struct mutex		device_list_lock;
70 	unsigned int		dma_avail;
71 	unsigned int		vaddr_invalid_count;
72 	uint64_t		pgsize_bitmap;
73 	uint64_t		num_non_pinned_groups;
74 	bool			v2;
75 	bool			nesting;
76 	bool			dirty_page_tracking;
77 	struct list_head	emulated_iommu_groups;
78 };
79 
80 struct vfio_domain {
81 	struct iommu_domain	*domain;
82 	struct list_head	next;
83 	struct list_head	group_list;
84 	bool			fgsp : 1;	/* Fine-grained super pages */
85 	bool			enforce_cache_coherency : 1;
86 };
87 
88 struct vfio_dma {
89 	struct rb_node		node;
90 	dma_addr_t		iova;		/* Device address */
91 	unsigned long		vaddr;		/* Process virtual addr */
92 	size_t			size;		/* Map size (bytes) */
93 	int			prot;		/* IOMMU_READ/WRITE */
94 	bool			iommu_mapped;
95 	bool			lock_cap;	/* capable(CAP_IPC_LOCK) */
96 	bool			vaddr_invalid;
97 	struct task_struct	*task;
98 	struct rb_root		pfn_list;	/* Ex-user pinned pfn list */
99 	unsigned long		*bitmap;
100 	struct mm_struct	*mm;
101 	size_t			locked_vm;
102 };
103 
104 struct vfio_batch {
105 	struct page		**pages;	/* for pin_user_pages_remote */
106 	struct page		*fallback_page; /* if pages alloc fails */
107 	int			capacity;	/* length of pages array */
108 	int			size;		/* of batch currently */
109 	int			offset;		/* of next entry in pages */
110 };
111 
112 struct vfio_iommu_group {
113 	struct iommu_group	*iommu_group;
114 	struct list_head	next;
115 	bool			pinned_page_dirty_scope;
116 };
117 
118 struct vfio_iova {
119 	struct list_head	list;
120 	dma_addr_t		start;
121 	dma_addr_t		end;
122 };
123 
124 /*
125  * Guest RAM pinning working set or DMA target
126  */
127 struct vfio_pfn {
128 	struct rb_node		node;
129 	dma_addr_t		iova;		/* Device address */
130 	unsigned long		pfn;		/* Host pfn */
131 	unsigned int		ref_count;
132 };
133 
134 struct vfio_regions {
135 	struct list_head list;
136 	dma_addr_t iova;
137 	phys_addr_t phys;
138 	size_t len;
139 };
140 
141 #define DIRTY_BITMAP_BYTES(n)	(ALIGN(n, BITS_PER_TYPE(u64)) / BITS_PER_BYTE)
142 
143 /*
144  * Input argument of number of bits to bitmap_set() is unsigned integer, which
145  * further casts to signed integer for unaligned multi-bit operation,
146  * __bitmap_set().
147  * Then maximum bitmap size supported is 2^31 bits divided by 2^3 bits/byte,
148  * that is 2^28 (256 MB) which maps to 2^31 * 2^12 = 2^43 (8TB) on 4K page
149  * system.
150  */
151 #define DIRTY_BITMAP_PAGES_MAX	 ((u64)INT_MAX)
152 #define DIRTY_BITMAP_SIZE_MAX	 DIRTY_BITMAP_BYTES(DIRTY_BITMAP_PAGES_MAX)
153 
154 static int put_pfn(unsigned long pfn, int prot);
155 
156 static struct vfio_iommu_group*
157 vfio_iommu_find_iommu_group(struct vfio_iommu *iommu,
158 			    struct iommu_group *iommu_group);
159 
160 /*
161  * This code handles mapping and unmapping of user data buffers
162  * into DMA'ble space using the IOMMU
163  */
164 
vfio_find_dma(struct vfio_iommu * iommu,dma_addr_t start,size_t size)165 static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
166 				      dma_addr_t start, size_t size)
167 {
168 	struct rb_node *node = iommu->dma_list.rb_node;
169 
170 	while (node) {
171 		struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node);
172 
173 		if (start + size <= dma->iova)
174 			node = node->rb_left;
175 		else if (start >= dma->iova + dma->size)
176 			node = node->rb_right;
177 		else
178 			return dma;
179 	}
180 
181 	return NULL;
182 }
183 
vfio_find_dma_first_node(struct vfio_iommu * iommu,dma_addr_t start,u64 size)184 static struct rb_node *vfio_find_dma_first_node(struct vfio_iommu *iommu,
185 						dma_addr_t start, u64 size)
186 {
187 	struct rb_node *res = NULL;
188 	struct rb_node *node = iommu->dma_list.rb_node;
189 	struct vfio_dma *dma_res = NULL;
190 
191 	while (node) {
192 		struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node);
193 
194 		if (start < dma->iova + dma->size) {
195 			res = node;
196 			dma_res = dma;
197 			if (start >= dma->iova)
198 				break;
199 			node = node->rb_left;
200 		} else {
201 			node = node->rb_right;
202 		}
203 	}
204 	if (res && size && dma_res->iova >= start + size)
205 		res = NULL;
206 	return res;
207 }
208 
vfio_link_dma(struct vfio_iommu * iommu,struct vfio_dma * new)209 static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
210 {
211 	struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL;
212 	struct vfio_dma *dma;
213 
214 	while (*link) {
215 		parent = *link;
216 		dma = rb_entry(parent, struct vfio_dma, node);
217 
218 		if (new->iova + new->size <= dma->iova)
219 			link = &(*link)->rb_left;
220 		else
221 			link = &(*link)->rb_right;
222 	}
223 
224 	rb_link_node(&new->node, parent, link);
225 	rb_insert_color(&new->node, &iommu->dma_list);
226 }
227 
vfio_unlink_dma(struct vfio_iommu * iommu,struct vfio_dma * old)228 static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old)
229 {
230 	rb_erase(&old->node, &iommu->dma_list);
231 }
232 
233 
vfio_dma_bitmap_alloc(struct vfio_dma * dma,size_t pgsize)234 static int vfio_dma_bitmap_alloc(struct vfio_dma *dma, size_t pgsize)
235 {
236 	uint64_t npages = dma->size / pgsize;
237 
238 	if (npages > DIRTY_BITMAP_PAGES_MAX)
239 		return -EINVAL;
240 
241 	/*
242 	 * Allocate extra 64 bits that are used to calculate shift required for
243 	 * bitmap_shift_left() to manipulate and club unaligned number of pages
244 	 * in adjacent vfio_dma ranges.
245 	 */
246 	dma->bitmap = kvzalloc(DIRTY_BITMAP_BYTES(npages) + sizeof(u64),
247 			       GFP_KERNEL);
248 	if (!dma->bitmap)
249 		return -ENOMEM;
250 
251 	return 0;
252 }
253 
vfio_dma_bitmap_free(struct vfio_dma * dma)254 static void vfio_dma_bitmap_free(struct vfio_dma *dma)
255 {
256 	kvfree(dma->bitmap);
257 	dma->bitmap = NULL;
258 }
259 
vfio_dma_populate_bitmap(struct vfio_dma * dma,size_t pgsize)260 static void vfio_dma_populate_bitmap(struct vfio_dma *dma, size_t pgsize)
261 {
262 	struct rb_node *p;
263 	unsigned long pgshift = __ffs(pgsize);
264 
265 	for (p = rb_first(&dma->pfn_list); p; p = rb_next(p)) {
266 		struct vfio_pfn *vpfn = rb_entry(p, struct vfio_pfn, node);
267 
268 		bitmap_set(dma->bitmap, (vpfn->iova - dma->iova) >> pgshift, 1);
269 	}
270 }
271 
vfio_iommu_populate_bitmap_full(struct vfio_iommu * iommu)272 static void vfio_iommu_populate_bitmap_full(struct vfio_iommu *iommu)
273 {
274 	struct rb_node *n;
275 	unsigned long pgshift = __ffs(iommu->pgsize_bitmap);
276 
277 	for (n = rb_first(&iommu->dma_list); n; n = rb_next(n)) {
278 		struct vfio_dma *dma = rb_entry(n, struct vfio_dma, node);
279 
280 		bitmap_set(dma->bitmap, 0, dma->size >> pgshift);
281 	}
282 }
283 
vfio_dma_bitmap_alloc_all(struct vfio_iommu * iommu,size_t pgsize)284 static int vfio_dma_bitmap_alloc_all(struct vfio_iommu *iommu, size_t pgsize)
285 {
286 	struct rb_node *n;
287 
288 	for (n = rb_first(&iommu->dma_list); n; n = rb_next(n)) {
289 		struct vfio_dma *dma = rb_entry(n, struct vfio_dma, node);
290 		int ret;
291 
292 		ret = vfio_dma_bitmap_alloc(dma, pgsize);
293 		if (ret) {
294 			struct rb_node *p;
295 
296 			for (p = rb_prev(n); p; p = rb_prev(p)) {
297 				struct vfio_dma *dma = rb_entry(n,
298 							struct vfio_dma, node);
299 
300 				vfio_dma_bitmap_free(dma);
301 			}
302 			return ret;
303 		}
304 		vfio_dma_populate_bitmap(dma, pgsize);
305 	}
306 	return 0;
307 }
308 
vfio_dma_bitmap_free_all(struct vfio_iommu * iommu)309 static void vfio_dma_bitmap_free_all(struct vfio_iommu *iommu)
310 {
311 	struct rb_node *n;
312 
313 	for (n = rb_first(&iommu->dma_list); n; n = rb_next(n)) {
314 		struct vfio_dma *dma = rb_entry(n, struct vfio_dma, node);
315 
316 		vfio_dma_bitmap_free(dma);
317 	}
318 }
319 
320 /*
321  * Helper Functions for host iova-pfn list
322  */
vfio_find_vpfn(struct vfio_dma * dma,dma_addr_t iova)323 static struct vfio_pfn *vfio_find_vpfn(struct vfio_dma *dma, dma_addr_t iova)
324 {
325 	struct vfio_pfn *vpfn;
326 	struct rb_node *node = dma->pfn_list.rb_node;
327 
328 	while (node) {
329 		vpfn = rb_entry(node, struct vfio_pfn, node);
330 
331 		if (iova < vpfn->iova)
332 			node = node->rb_left;
333 		else if (iova > vpfn->iova)
334 			node = node->rb_right;
335 		else
336 			return vpfn;
337 	}
338 	return NULL;
339 }
340 
vfio_link_pfn(struct vfio_dma * dma,struct vfio_pfn * new)341 static void vfio_link_pfn(struct vfio_dma *dma,
342 			  struct vfio_pfn *new)
343 {
344 	struct rb_node **link, *parent = NULL;
345 	struct vfio_pfn *vpfn;
346 
347 	link = &dma->pfn_list.rb_node;
348 	while (*link) {
349 		parent = *link;
350 		vpfn = rb_entry(parent, struct vfio_pfn, node);
351 
352 		if (new->iova < vpfn->iova)
353 			link = &(*link)->rb_left;
354 		else
355 			link = &(*link)->rb_right;
356 	}
357 
358 	rb_link_node(&new->node, parent, link);
359 	rb_insert_color(&new->node, &dma->pfn_list);
360 }
361 
vfio_unlink_pfn(struct vfio_dma * dma,struct vfio_pfn * old)362 static void vfio_unlink_pfn(struct vfio_dma *dma, struct vfio_pfn *old)
363 {
364 	rb_erase(&old->node, &dma->pfn_list);
365 }
366 
vfio_add_to_pfn_list(struct vfio_dma * dma,dma_addr_t iova,unsigned long pfn)367 static int vfio_add_to_pfn_list(struct vfio_dma *dma, dma_addr_t iova,
368 				unsigned long pfn)
369 {
370 	struct vfio_pfn *vpfn;
371 
372 	vpfn = kzalloc(sizeof(*vpfn), GFP_KERNEL);
373 	if (!vpfn)
374 		return -ENOMEM;
375 
376 	vpfn->iova = iova;
377 	vpfn->pfn = pfn;
378 	vpfn->ref_count = 1;
379 	vfio_link_pfn(dma, vpfn);
380 	return 0;
381 }
382 
vfio_remove_from_pfn_list(struct vfio_dma * dma,struct vfio_pfn * vpfn)383 static void vfio_remove_from_pfn_list(struct vfio_dma *dma,
384 				      struct vfio_pfn *vpfn)
385 {
386 	vfio_unlink_pfn(dma, vpfn);
387 	kfree(vpfn);
388 }
389 
vfio_iova_get_vfio_pfn(struct vfio_dma * dma,unsigned long iova)390 static struct vfio_pfn *vfio_iova_get_vfio_pfn(struct vfio_dma *dma,
391 					       unsigned long iova)
392 {
393 	struct vfio_pfn *vpfn = vfio_find_vpfn(dma, iova);
394 
395 	if (vpfn)
396 		vpfn->ref_count++;
397 	return vpfn;
398 }
399 
vfio_iova_put_vfio_pfn(struct vfio_dma * dma,struct vfio_pfn * vpfn)400 static int vfio_iova_put_vfio_pfn(struct vfio_dma *dma, struct vfio_pfn *vpfn)
401 {
402 	int ret = 0;
403 
404 	vpfn->ref_count--;
405 	if (!vpfn->ref_count) {
406 		ret = put_pfn(vpfn->pfn, dma->prot);
407 		vfio_remove_from_pfn_list(dma, vpfn);
408 	}
409 	return ret;
410 }
411 
mm_lock_acct(struct task_struct * task,struct mm_struct * mm,bool lock_cap,long npage)412 static int mm_lock_acct(struct task_struct *task, struct mm_struct *mm,
413 			bool lock_cap, long npage)
414 {
415 	int ret = mmap_write_lock_killable(mm);
416 
417 	if (ret)
418 		return ret;
419 
420 	ret = __account_locked_vm(mm, abs(npage), npage > 0, task, lock_cap);
421 	mmap_write_unlock(mm);
422 	return ret;
423 }
424 
vfio_lock_acct(struct vfio_dma * dma,long npage,bool async)425 static int vfio_lock_acct(struct vfio_dma *dma, long npage, bool async)
426 {
427 	struct mm_struct *mm;
428 	int ret;
429 
430 	if (!npage)
431 		return 0;
432 
433 	mm = dma->mm;
434 	if (async && !mmget_not_zero(mm))
435 		return -ESRCH; /* process exited */
436 
437 	ret = mm_lock_acct(dma->task, mm, dma->lock_cap, npage);
438 	if (!ret)
439 		dma->locked_vm += npage;
440 
441 	if (async)
442 		mmput(mm);
443 
444 	return ret;
445 }
446 
447 /*
448  * Some mappings aren't backed by a struct page, for example an mmap'd
449  * MMIO range for our own or another device.  These use a different
450  * pfn conversion and shouldn't be tracked as locked pages.
451  * For compound pages, any driver that sets the reserved bit in head
452  * page needs to set the reserved bit in all subpages to be safe.
453  */
is_invalid_reserved_pfn(unsigned long pfn)454 static bool is_invalid_reserved_pfn(unsigned long pfn)
455 {
456 	if (pfn_valid(pfn))
457 		return PageReserved(pfn_to_page(pfn));
458 
459 	return true;
460 }
461 
put_pfn(unsigned long pfn,int prot)462 static int put_pfn(unsigned long pfn, int prot)
463 {
464 	if (!is_invalid_reserved_pfn(pfn)) {
465 		struct page *page = pfn_to_page(pfn);
466 
467 		unpin_user_pages_dirty_lock(&page, 1, prot & IOMMU_WRITE);
468 		return 1;
469 	}
470 	return 0;
471 }
472 
473 #define VFIO_BATCH_MAX_CAPACITY (PAGE_SIZE / sizeof(struct page *))
474 
vfio_batch_init(struct vfio_batch * batch)475 static void vfio_batch_init(struct vfio_batch *batch)
476 {
477 	batch->size = 0;
478 	batch->offset = 0;
479 
480 	if (unlikely(disable_hugepages))
481 		goto fallback;
482 
483 	batch->pages = (struct page **) __get_free_page(GFP_KERNEL);
484 	if (!batch->pages)
485 		goto fallback;
486 
487 	batch->capacity = VFIO_BATCH_MAX_CAPACITY;
488 	return;
489 
490 fallback:
491 	batch->pages = &batch->fallback_page;
492 	batch->capacity = 1;
493 }
494 
vfio_batch_unpin(struct vfio_batch * batch,struct vfio_dma * dma)495 static void vfio_batch_unpin(struct vfio_batch *batch, struct vfio_dma *dma)
496 {
497 	while (batch->size) {
498 		unsigned long pfn = page_to_pfn(batch->pages[batch->offset]);
499 
500 		put_pfn(pfn, dma->prot);
501 		batch->offset++;
502 		batch->size--;
503 	}
504 }
505 
vfio_batch_fini(struct vfio_batch * batch)506 static void vfio_batch_fini(struct vfio_batch *batch)
507 {
508 	if (batch->capacity == VFIO_BATCH_MAX_CAPACITY)
509 		free_page((unsigned long)batch->pages);
510 }
511 
follow_fault_pfn(struct vm_area_struct * vma,struct mm_struct * mm,unsigned long vaddr,unsigned long * pfn,bool write_fault)512 static int follow_fault_pfn(struct vm_area_struct *vma, struct mm_struct *mm,
513 			    unsigned long vaddr, unsigned long *pfn,
514 			    bool write_fault)
515 {
516 	pte_t *ptep;
517 	pte_t pte;
518 	spinlock_t *ptl;
519 	int ret;
520 
521 	ret = follow_pte(vma->vm_mm, vaddr, &ptep, &ptl);
522 	if (ret) {
523 		bool unlocked = false;
524 
525 		ret = fixup_user_fault(mm, vaddr,
526 				       FAULT_FLAG_REMOTE |
527 				       (write_fault ?  FAULT_FLAG_WRITE : 0),
528 				       &unlocked);
529 		if (unlocked)
530 			return -EAGAIN;
531 
532 		if (ret)
533 			return ret;
534 
535 		ret = follow_pte(vma->vm_mm, vaddr, &ptep, &ptl);
536 		if (ret)
537 			return ret;
538 	}
539 
540 	pte = ptep_get(ptep);
541 
542 	if (write_fault && !pte_write(pte))
543 		ret = -EFAULT;
544 	else
545 		*pfn = pte_pfn(pte);
546 
547 	pte_unmap_unlock(ptep, ptl);
548 	return ret;
549 }
550 
551 /*
552  * Returns the positive number of pfns successfully obtained or a negative
553  * error code.
554  */
vaddr_get_pfns(struct mm_struct * mm,unsigned long vaddr,long npages,int prot,unsigned long * pfn,struct page ** pages)555 static int vaddr_get_pfns(struct mm_struct *mm, unsigned long vaddr,
556 			  long npages, int prot, unsigned long *pfn,
557 			  struct page **pages)
558 {
559 	struct vm_area_struct *vma;
560 	unsigned int flags = 0;
561 	int ret;
562 
563 	if (prot & IOMMU_WRITE)
564 		flags |= FOLL_WRITE;
565 
566 	mmap_read_lock(mm);
567 	ret = pin_user_pages_remote(mm, vaddr, npages, flags | FOLL_LONGTERM,
568 				    pages, NULL);
569 	if (ret > 0) {
570 		int i;
571 
572 		/*
573 		 * The zero page is always resident, we don't need to pin it
574 		 * and it falls into our invalid/reserved test so we don't
575 		 * unpin in put_pfn().  Unpin all zero pages in the batch here.
576 		 */
577 		for (i = 0 ; i < ret; i++) {
578 			if (unlikely(is_zero_pfn(page_to_pfn(pages[i]))))
579 				unpin_user_page(pages[i]);
580 		}
581 
582 		*pfn = page_to_pfn(pages[0]);
583 		goto done;
584 	}
585 
586 	vaddr = untagged_addr_remote(mm, vaddr);
587 
588 retry:
589 	vma = vma_lookup(mm, vaddr);
590 
591 	if (vma && vma->vm_flags & VM_PFNMAP) {
592 		ret = follow_fault_pfn(vma, mm, vaddr, pfn, prot & IOMMU_WRITE);
593 		if (ret == -EAGAIN)
594 			goto retry;
595 
596 		if (!ret) {
597 			if (is_invalid_reserved_pfn(*pfn))
598 				ret = 1;
599 			else
600 				ret = -EFAULT;
601 		}
602 	}
603 done:
604 	mmap_read_unlock(mm);
605 	return ret;
606 }
607 
608 /*
609  * Attempt to pin pages.  We really don't want to track all the pfns and
610  * the iommu can only map chunks of consecutive pfns anyway, so get the
611  * first page and all consecutive pages with the same locking.
612  */
vfio_pin_pages_remote(struct vfio_dma * dma,unsigned long vaddr,long npage,unsigned long * pfn_base,unsigned long limit,struct vfio_batch * batch)613 static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,
614 				  long npage, unsigned long *pfn_base,
615 				  unsigned long limit, struct vfio_batch *batch)
616 {
617 	unsigned long pfn;
618 	struct mm_struct *mm = current->mm;
619 	long ret, pinned = 0, lock_acct = 0;
620 	bool rsvd;
621 	dma_addr_t iova = vaddr - dma->vaddr + dma->iova;
622 
623 	/* This code path is only user initiated */
624 	if (!mm)
625 		return -ENODEV;
626 
627 	if (batch->size) {
628 		/* Leftover pages in batch from an earlier call. */
629 		*pfn_base = page_to_pfn(batch->pages[batch->offset]);
630 		pfn = *pfn_base;
631 		rsvd = is_invalid_reserved_pfn(*pfn_base);
632 	} else {
633 		*pfn_base = 0;
634 	}
635 
636 	while (npage) {
637 		if (!batch->size) {
638 			/* Empty batch, so refill it. */
639 			long req_pages = min_t(long, npage, batch->capacity);
640 
641 			ret = vaddr_get_pfns(mm, vaddr, req_pages, dma->prot,
642 					     &pfn, batch->pages);
643 			if (ret < 0)
644 				goto unpin_out;
645 
646 			batch->size = ret;
647 			batch->offset = 0;
648 
649 			if (!*pfn_base) {
650 				*pfn_base = pfn;
651 				rsvd = is_invalid_reserved_pfn(*pfn_base);
652 			}
653 		}
654 
655 		/*
656 		 * pfn is preset for the first iteration of this inner loop and
657 		 * updated at the end to handle a VM_PFNMAP pfn.  In that case,
658 		 * batch->pages isn't valid (there's no struct page), so allow
659 		 * batch->pages to be touched only when there's more than one
660 		 * pfn to check, which guarantees the pfns are from a
661 		 * !VM_PFNMAP vma.
662 		 */
663 		while (true) {
664 			if (pfn != *pfn_base + pinned ||
665 			    rsvd != is_invalid_reserved_pfn(pfn))
666 				goto out;
667 
668 			/*
669 			 * Reserved pages aren't counted against the user,
670 			 * externally pinned pages are already counted against
671 			 * the user.
672 			 */
673 			if (!rsvd && !vfio_find_vpfn(dma, iova)) {
674 				if (!dma->lock_cap &&
675 				    mm->locked_vm + lock_acct + 1 > limit) {
676 					pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n",
677 						__func__, limit << PAGE_SHIFT);
678 					ret = -ENOMEM;
679 					goto unpin_out;
680 				}
681 				lock_acct++;
682 			}
683 
684 			pinned++;
685 			npage--;
686 			vaddr += PAGE_SIZE;
687 			iova += PAGE_SIZE;
688 			batch->offset++;
689 			batch->size--;
690 
691 			if (!batch->size)
692 				break;
693 
694 			pfn = page_to_pfn(batch->pages[batch->offset]);
695 		}
696 
697 		if (unlikely(disable_hugepages))
698 			break;
699 	}
700 
701 out:
702 	ret = vfio_lock_acct(dma, lock_acct, false);
703 
704 unpin_out:
705 	if (batch->size == 1 && !batch->offset) {
706 		/* May be a VM_PFNMAP pfn, which the batch can't remember. */
707 		put_pfn(pfn, dma->prot);
708 		batch->size = 0;
709 	}
710 
711 	if (ret < 0) {
712 		if (pinned && !rsvd) {
713 			for (pfn = *pfn_base ; pinned ; pfn++, pinned--)
714 				put_pfn(pfn, dma->prot);
715 		}
716 		vfio_batch_unpin(batch, dma);
717 
718 		return ret;
719 	}
720 
721 	return pinned;
722 }
723 
vfio_unpin_pages_remote(struct vfio_dma * dma,dma_addr_t iova,unsigned long pfn,long npage,bool do_accounting)724 static long vfio_unpin_pages_remote(struct vfio_dma *dma, dma_addr_t iova,
725 				    unsigned long pfn, long npage,
726 				    bool do_accounting)
727 {
728 	long unlocked = 0, locked = 0;
729 	long i;
730 
731 	for (i = 0; i < npage; i++, iova += PAGE_SIZE) {
732 		if (put_pfn(pfn++, dma->prot)) {
733 			unlocked++;
734 			if (vfio_find_vpfn(dma, iova))
735 				locked++;
736 		}
737 	}
738 
739 	if (do_accounting)
740 		vfio_lock_acct(dma, locked - unlocked, true);
741 
742 	return unlocked;
743 }
744 
vfio_pin_page_external(struct vfio_dma * dma,unsigned long vaddr,unsigned long * pfn_base,bool do_accounting)745 static int vfio_pin_page_external(struct vfio_dma *dma, unsigned long vaddr,
746 				  unsigned long *pfn_base, bool do_accounting)
747 {
748 	struct page *pages[1];
749 	struct mm_struct *mm;
750 	int ret;
751 
752 	mm = dma->mm;
753 	if (!mmget_not_zero(mm))
754 		return -ENODEV;
755 
756 	ret = vaddr_get_pfns(mm, vaddr, 1, dma->prot, pfn_base, pages);
757 	if (ret != 1)
758 		goto out;
759 
760 	ret = 0;
761 
762 	if (do_accounting && !is_invalid_reserved_pfn(*pfn_base)) {
763 		ret = vfio_lock_acct(dma, 1, false);
764 		if (ret) {
765 			put_pfn(*pfn_base, dma->prot);
766 			if (ret == -ENOMEM)
767 				pr_warn("%s: Task %s (%d) RLIMIT_MEMLOCK "
768 					"(%ld) exceeded\n", __func__,
769 					dma->task->comm, task_pid_nr(dma->task),
770 					task_rlimit(dma->task, RLIMIT_MEMLOCK));
771 		}
772 	}
773 
774 out:
775 	mmput(mm);
776 	return ret;
777 }
778 
vfio_unpin_page_external(struct vfio_dma * dma,dma_addr_t iova,bool do_accounting)779 static int vfio_unpin_page_external(struct vfio_dma *dma, dma_addr_t iova,
780 				    bool do_accounting)
781 {
782 	int unlocked;
783 	struct vfio_pfn *vpfn = vfio_find_vpfn(dma, iova);
784 
785 	if (!vpfn)
786 		return 0;
787 
788 	unlocked = vfio_iova_put_vfio_pfn(dma, vpfn);
789 
790 	if (do_accounting)
791 		vfio_lock_acct(dma, -unlocked, true);
792 
793 	return unlocked;
794 }
795 
vfio_iommu_type1_pin_pages(void * iommu_data,struct iommu_group * iommu_group,dma_addr_t user_iova,int npage,int prot,struct page ** pages)796 static int vfio_iommu_type1_pin_pages(void *iommu_data,
797 				      struct iommu_group *iommu_group,
798 				      dma_addr_t user_iova,
799 				      int npage, int prot,
800 				      struct page **pages)
801 {
802 	struct vfio_iommu *iommu = iommu_data;
803 	struct vfio_iommu_group *group;
804 	int i, j, ret;
805 	unsigned long remote_vaddr;
806 	struct vfio_dma *dma;
807 	bool do_accounting;
808 
809 	if (!iommu || !pages)
810 		return -EINVAL;
811 
812 	/* Supported for v2 version only */
813 	if (!iommu->v2)
814 		return -EACCES;
815 
816 	mutex_lock(&iommu->lock);
817 
818 	if (WARN_ONCE(iommu->vaddr_invalid_count,
819 		      "vfio_pin_pages not allowed with VFIO_UPDATE_VADDR\n")) {
820 		ret = -EBUSY;
821 		goto pin_done;
822 	}
823 
824 	/* Fail if no dma_umap notifier is registered */
825 	if (list_empty(&iommu->device_list)) {
826 		ret = -EINVAL;
827 		goto pin_done;
828 	}
829 
830 	/*
831 	 * If iommu capable domain exist in the container then all pages are
832 	 * already pinned and accounted. Accounting should be done if there is no
833 	 * iommu capable domain in the container.
834 	 */
835 	do_accounting = list_empty(&iommu->domain_list);
836 
837 	for (i = 0; i < npage; i++) {
838 		unsigned long phys_pfn;
839 		dma_addr_t iova;
840 		struct vfio_pfn *vpfn;
841 
842 		iova = user_iova + PAGE_SIZE * i;
843 		dma = vfio_find_dma(iommu, iova, PAGE_SIZE);
844 		if (!dma) {
845 			ret = -EINVAL;
846 			goto pin_unwind;
847 		}
848 
849 		if ((dma->prot & prot) != prot) {
850 			ret = -EPERM;
851 			goto pin_unwind;
852 		}
853 
854 		vpfn = vfio_iova_get_vfio_pfn(dma, iova);
855 		if (vpfn) {
856 			pages[i] = pfn_to_page(vpfn->pfn);
857 			continue;
858 		}
859 
860 		remote_vaddr = dma->vaddr + (iova - dma->iova);
861 		ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn,
862 					     do_accounting);
863 		if (ret)
864 			goto pin_unwind;
865 
866 		if (!pfn_valid(phys_pfn)) {
867 			ret = -EINVAL;
868 			goto pin_unwind;
869 		}
870 
871 		ret = vfio_add_to_pfn_list(dma, iova, phys_pfn);
872 		if (ret) {
873 			if (put_pfn(phys_pfn, dma->prot) && do_accounting)
874 				vfio_lock_acct(dma, -1, true);
875 			goto pin_unwind;
876 		}
877 
878 		pages[i] = pfn_to_page(phys_pfn);
879 
880 		if (iommu->dirty_page_tracking) {
881 			unsigned long pgshift = __ffs(iommu->pgsize_bitmap);
882 
883 			/*
884 			 * Bitmap populated with the smallest supported page
885 			 * size
886 			 */
887 			bitmap_set(dma->bitmap,
888 				   (iova - dma->iova) >> pgshift, 1);
889 		}
890 	}
891 	ret = i;
892 
893 	group = vfio_iommu_find_iommu_group(iommu, iommu_group);
894 	if (!group->pinned_page_dirty_scope) {
895 		group->pinned_page_dirty_scope = true;
896 		iommu->num_non_pinned_groups--;
897 	}
898 
899 	goto pin_done;
900 
901 pin_unwind:
902 	pages[i] = NULL;
903 	for (j = 0; j < i; j++) {
904 		dma_addr_t iova;
905 
906 		iova = user_iova + PAGE_SIZE * j;
907 		dma = vfio_find_dma(iommu, iova, PAGE_SIZE);
908 		vfio_unpin_page_external(dma, iova, do_accounting);
909 		pages[j] = NULL;
910 	}
911 pin_done:
912 	mutex_unlock(&iommu->lock);
913 	return ret;
914 }
915 
vfio_iommu_type1_unpin_pages(void * iommu_data,dma_addr_t user_iova,int npage)916 static void vfio_iommu_type1_unpin_pages(void *iommu_data,
917 					 dma_addr_t user_iova, int npage)
918 {
919 	struct vfio_iommu *iommu = iommu_data;
920 	bool do_accounting;
921 	int i;
922 
923 	/* Supported for v2 version only */
924 	if (WARN_ON(!iommu->v2))
925 		return;
926 
927 	mutex_lock(&iommu->lock);
928 
929 	do_accounting = list_empty(&iommu->domain_list);
930 	for (i = 0; i < npage; i++) {
931 		dma_addr_t iova = user_iova + PAGE_SIZE * i;
932 		struct vfio_dma *dma;
933 
934 		dma = vfio_find_dma(iommu, iova, PAGE_SIZE);
935 		if (!dma)
936 			break;
937 
938 		vfio_unpin_page_external(dma, iova, do_accounting);
939 	}
940 
941 	mutex_unlock(&iommu->lock);
942 
943 	WARN_ON(i != npage);
944 }
945 
vfio_sync_unpin(struct vfio_dma * dma,struct vfio_domain * domain,struct list_head * regions,struct iommu_iotlb_gather * iotlb_gather)946 static long vfio_sync_unpin(struct vfio_dma *dma, struct vfio_domain *domain,
947 			    struct list_head *regions,
948 			    struct iommu_iotlb_gather *iotlb_gather)
949 {
950 	long unlocked = 0;
951 	struct vfio_regions *entry, *next;
952 
953 	iommu_iotlb_sync(domain->domain, iotlb_gather);
954 
955 	list_for_each_entry_safe(entry, next, regions, list) {
956 		unlocked += vfio_unpin_pages_remote(dma,
957 						    entry->iova,
958 						    entry->phys >> PAGE_SHIFT,
959 						    entry->len >> PAGE_SHIFT,
960 						    false);
961 		list_del(&entry->list);
962 		kfree(entry);
963 	}
964 
965 	cond_resched();
966 
967 	return unlocked;
968 }
969 
970 /*
971  * Generally, VFIO needs to unpin remote pages after each IOTLB flush.
972  * Therefore, when using IOTLB flush sync interface, VFIO need to keep track
973  * of these regions (currently using a list).
974  *
975  * This value specifies maximum number of regions for each IOTLB flush sync.
976  */
977 #define VFIO_IOMMU_TLB_SYNC_MAX		512
978 
unmap_unpin_fast(struct vfio_domain * domain,struct vfio_dma * dma,dma_addr_t * iova,size_t len,phys_addr_t phys,long * unlocked,struct list_head * unmapped_list,int * unmapped_cnt,struct iommu_iotlb_gather * iotlb_gather)979 static size_t unmap_unpin_fast(struct vfio_domain *domain,
980 			       struct vfio_dma *dma, dma_addr_t *iova,
981 			       size_t len, phys_addr_t phys, long *unlocked,
982 			       struct list_head *unmapped_list,
983 			       int *unmapped_cnt,
984 			       struct iommu_iotlb_gather *iotlb_gather)
985 {
986 	size_t unmapped = 0;
987 	struct vfio_regions *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
988 
989 	if (entry) {
990 		unmapped = iommu_unmap_fast(domain->domain, *iova, len,
991 					    iotlb_gather);
992 
993 		if (!unmapped) {
994 			kfree(entry);
995 		} else {
996 			entry->iova = *iova;
997 			entry->phys = phys;
998 			entry->len  = unmapped;
999 			list_add_tail(&entry->list, unmapped_list);
1000 
1001 			*iova += unmapped;
1002 			(*unmapped_cnt)++;
1003 		}
1004 	}
1005 
1006 	/*
1007 	 * Sync if the number of fast-unmap regions hits the limit
1008 	 * or in case of errors.
1009 	 */
1010 	if (*unmapped_cnt >= VFIO_IOMMU_TLB_SYNC_MAX || !unmapped) {
1011 		*unlocked += vfio_sync_unpin(dma, domain, unmapped_list,
1012 					     iotlb_gather);
1013 		*unmapped_cnt = 0;
1014 	}
1015 
1016 	return unmapped;
1017 }
1018 
unmap_unpin_slow(struct vfio_domain * domain,struct vfio_dma * dma,dma_addr_t * iova,size_t len,phys_addr_t phys,long * unlocked)1019 static size_t unmap_unpin_slow(struct vfio_domain *domain,
1020 			       struct vfio_dma *dma, dma_addr_t *iova,
1021 			       size_t len, phys_addr_t phys,
1022 			       long *unlocked)
1023 {
1024 	size_t unmapped = iommu_unmap(domain->domain, *iova, len);
1025 
1026 	if (unmapped) {
1027 		*unlocked += vfio_unpin_pages_remote(dma, *iova,
1028 						     phys >> PAGE_SHIFT,
1029 						     unmapped >> PAGE_SHIFT,
1030 						     false);
1031 		*iova += unmapped;
1032 		cond_resched();
1033 	}
1034 	return unmapped;
1035 }
1036 
vfio_unmap_unpin(struct vfio_iommu * iommu,struct vfio_dma * dma,bool do_accounting)1037 static long vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma,
1038 			     bool do_accounting)
1039 {
1040 	dma_addr_t iova = dma->iova, end = dma->iova + dma->size;
1041 	struct vfio_domain *domain, *d;
1042 	LIST_HEAD(unmapped_region_list);
1043 	struct iommu_iotlb_gather iotlb_gather;
1044 	int unmapped_region_cnt = 0;
1045 	long unlocked = 0;
1046 
1047 	if (!dma->size)
1048 		return 0;
1049 
1050 	if (list_empty(&iommu->domain_list))
1051 		return 0;
1052 
1053 	/*
1054 	 * We use the IOMMU to track the physical addresses, otherwise we'd
1055 	 * need a much more complicated tracking system.  Unfortunately that
1056 	 * means we need to use one of the iommu domains to figure out the
1057 	 * pfns to unpin.  The rest need to be unmapped in advance so we have
1058 	 * no iommu translations remaining when the pages are unpinned.
1059 	 */
1060 	domain = d = list_first_entry(&iommu->domain_list,
1061 				      struct vfio_domain, next);
1062 
1063 	list_for_each_entry_continue(d, &iommu->domain_list, next) {
1064 		iommu_unmap(d->domain, dma->iova, dma->size);
1065 		cond_resched();
1066 	}
1067 
1068 	iommu_iotlb_gather_init(&iotlb_gather);
1069 	while (iova < end) {
1070 		size_t unmapped, len;
1071 		phys_addr_t phys, next;
1072 
1073 		phys = iommu_iova_to_phys(domain->domain, iova);
1074 		if (WARN_ON(!phys)) {
1075 			iova += PAGE_SIZE;
1076 			continue;
1077 		}
1078 
1079 		/*
1080 		 * To optimize for fewer iommu_unmap() calls, each of which
1081 		 * may require hardware cache flushing, try to find the
1082 		 * largest contiguous physical memory chunk to unmap.
1083 		 */
1084 		for (len = PAGE_SIZE;
1085 		     !domain->fgsp && iova + len < end; len += PAGE_SIZE) {
1086 			next = iommu_iova_to_phys(domain->domain, iova + len);
1087 			if (next != phys + len)
1088 				break;
1089 		}
1090 
1091 		/*
1092 		 * First, try to use fast unmap/unpin. In case of failure,
1093 		 * switch to slow unmap/unpin path.
1094 		 */
1095 		unmapped = unmap_unpin_fast(domain, dma, &iova, len, phys,
1096 					    &unlocked, &unmapped_region_list,
1097 					    &unmapped_region_cnt,
1098 					    &iotlb_gather);
1099 		if (!unmapped) {
1100 			unmapped = unmap_unpin_slow(domain, dma, &iova, len,
1101 						    phys, &unlocked);
1102 			if (WARN_ON(!unmapped))
1103 				break;
1104 		}
1105 	}
1106 
1107 	dma->iommu_mapped = false;
1108 
1109 	if (unmapped_region_cnt) {
1110 		unlocked += vfio_sync_unpin(dma, domain, &unmapped_region_list,
1111 					    &iotlb_gather);
1112 	}
1113 
1114 	if (do_accounting) {
1115 		vfio_lock_acct(dma, -unlocked, true);
1116 		return 0;
1117 	}
1118 	return unlocked;
1119 }
1120 
vfio_remove_dma(struct vfio_iommu * iommu,struct vfio_dma * dma)1121 static void vfio_remove_dma(struct vfio_iommu *iommu, struct vfio_dma *dma)
1122 {
1123 	WARN_ON(!RB_EMPTY_ROOT(&dma->pfn_list));
1124 	vfio_unmap_unpin(iommu, dma, true);
1125 	vfio_unlink_dma(iommu, dma);
1126 	put_task_struct(dma->task);
1127 	mmdrop(dma->mm);
1128 	vfio_dma_bitmap_free(dma);
1129 	if (dma->vaddr_invalid)
1130 		iommu->vaddr_invalid_count--;
1131 	kfree(dma);
1132 	iommu->dma_avail++;
1133 }
1134 
vfio_update_pgsize_bitmap(struct vfio_iommu * iommu)1135 static void vfio_update_pgsize_bitmap(struct vfio_iommu *iommu)
1136 {
1137 	struct vfio_domain *domain;
1138 
1139 	iommu->pgsize_bitmap = ULONG_MAX;
1140 
1141 	list_for_each_entry(domain, &iommu->domain_list, next)
1142 		iommu->pgsize_bitmap &= domain->domain->pgsize_bitmap;
1143 
1144 	/*
1145 	 * In case the IOMMU supports page sizes smaller than PAGE_SIZE
1146 	 * we pretend PAGE_SIZE is supported and hide sub-PAGE_SIZE sizes.
1147 	 * That way the user will be able to map/unmap buffers whose size/
1148 	 * start address is aligned with PAGE_SIZE. Pinning code uses that
1149 	 * granularity while iommu driver can use the sub-PAGE_SIZE size
1150 	 * to map the buffer.
1151 	 */
1152 	if (iommu->pgsize_bitmap & ~PAGE_MASK) {
1153 		iommu->pgsize_bitmap &= PAGE_MASK;
1154 		iommu->pgsize_bitmap |= PAGE_SIZE;
1155 	}
1156 }
1157 
update_user_bitmap(u64 __user * bitmap,struct vfio_iommu * iommu,struct vfio_dma * dma,dma_addr_t base_iova,size_t pgsize)1158 static int update_user_bitmap(u64 __user *bitmap, struct vfio_iommu *iommu,
1159 			      struct vfio_dma *dma, dma_addr_t base_iova,
1160 			      size_t pgsize)
1161 {
1162 	unsigned long pgshift = __ffs(pgsize);
1163 	unsigned long nbits = dma->size >> pgshift;
1164 	unsigned long bit_offset = (dma->iova - base_iova) >> pgshift;
1165 	unsigned long copy_offset = bit_offset / BITS_PER_LONG;
1166 	unsigned long shift = bit_offset % BITS_PER_LONG;
1167 	unsigned long leftover;
1168 
1169 	/*
1170 	 * mark all pages dirty if any IOMMU capable device is not able
1171 	 * to report dirty pages and all pages are pinned and mapped.
1172 	 */
1173 	if (iommu->num_non_pinned_groups && dma->iommu_mapped)
1174 		bitmap_set(dma->bitmap, 0, nbits);
1175 
1176 	if (shift) {
1177 		bitmap_shift_left(dma->bitmap, dma->bitmap, shift,
1178 				  nbits + shift);
1179 
1180 		if (copy_from_user(&leftover,
1181 				   (void __user *)(bitmap + copy_offset),
1182 				   sizeof(leftover)))
1183 			return -EFAULT;
1184 
1185 		bitmap_or(dma->bitmap, dma->bitmap, &leftover, shift);
1186 	}
1187 
1188 	if (copy_to_user((void __user *)(bitmap + copy_offset), dma->bitmap,
1189 			 DIRTY_BITMAP_BYTES(nbits + shift)))
1190 		return -EFAULT;
1191 
1192 	return 0;
1193 }
1194 
vfio_iova_dirty_bitmap(u64 __user * bitmap,struct vfio_iommu * iommu,dma_addr_t iova,size_t size,size_t pgsize)1195 static int vfio_iova_dirty_bitmap(u64 __user *bitmap, struct vfio_iommu *iommu,
1196 				  dma_addr_t iova, size_t size, size_t pgsize)
1197 {
1198 	struct vfio_dma *dma;
1199 	struct rb_node *n;
1200 	unsigned long pgshift = __ffs(pgsize);
1201 	int ret;
1202 
1203 	/*
1204 	 * GET_BITMAP request must fully cover vfio_dma mappings.  Multiple
1205 	 * vfio_dma mappings may be clubbed by specifying large ranges, but
1206 	 * there must not be any previous mappings bisected by the range.
1207 	 * An error will be returned if these conditions are not met.
1208 	 */
1209 	dma = vfio_find_dma(iommu, iova, 1);
1210 	if (dma && dma->iova != iova)
1211 		return -EINVAL;
1212 
1213 	dma = vfio_find_dma(iommu, iova + size - 1, 0);
1214 	if (dma && dma->iova + dma->size != iova + size)
1215 		return -EINVAL;
1216 
1217 	for (n = rb_first(&iommu->dma_list); n; n = rb_next(n)) {
1218 		struct vfio_dma *dma = rb_entry(n, struct vfio_dma, node);
1219 
1220 		if (dma->iova < iova)
1221 			continue;
1222 
1223 		if (dma->iova > iova + size - 1)
1224 			break;
1225 
1226 		ret = update_user_bitmap(bitmap, iommu, dma, iova, pgsize);
1227 		if (ret)
1228 			return ret;
1229 
1230 		/*
1231 		 * Re-populate bitmap to include all pinned pages which are
1232 		 * considered as dirty but exclude pages which are unpinned and
1233 		 * pages which are marked dirty by vfio_dma_rw()
1234 		 */
1235 		bitmap_clear(dma->bitmap, 0, dma->size >> pgshift);
1236 		vfio_dma_populate_bitmap(dma, pgsize);
1237 	}
1238 	return 0;
1239 }
1240 
verify_bitmap_size(uint64_t npages,uint64_t bitmap_size)1241 static int verify_bitmap_size(uint64_t npages, uint64_t bitmap_size)
1242 {
1243 	if (!npages || !bitmap_size || (bitmap_size > DIRTY_BITMAP_SIZE_MAX) ||
1244 	    (bitmap_size < DIRTY_BITMAP_BYTES(npages)))
1245 		return -EINVAL;
1246 
1247 	return 0;
1248 }
1249 
1250 /*
1251  * Notify VFIO drivers using vfio_register_emulated_iommu_dev() to invalidate
1252  * and unmap iovas within the range we're about to unmap. Drivers MUST unpin
1253  * pages in response to an invalidation.
1254  */
vfio_notify_dma_unmap(struct vfio_iommu * iommu,struct vfio_dma * dma)1255 static void vfio_notify_dma_unmap(struct vfio_iommu *iommu,
1256 				  struct vfio_dma *dma)
1257 {
1258 	struct vfio_device *device;
1259 
1260 	if (list_empty(&iommu->device_list))
1261 		return;
1262 
1263 	/*
1264 	 * The device is expected to call vfio_unpin_pages() for any IOVA it has
1265 	 * pinned within the range. Since vfio_unpin_pages() will eventually
1266 	 * call back down to this code and try to obtain the iommu->lock we must
1267 	 * drop it.
1268 	 */
1269 	mutex_lock(&iommu->device_list_lock);
1270 	mutex_unlock(&iommu->lock);
1271 
1272 	list_for_each_entry(device, &iommu->device_list, iommu_entry)
1273 		device->ops->dma_unmap(device, dma->iova, dma->size);
1274 
1275 	mutex_unlock(&iommu->device_list_lock);
1276 	mutex_lock(&iommu->lock);
1277 }
1278 
vfio_dma_do_unmap(struct vfio_iommu * iommu,struct vfio_iommu_type1_dma_unmap * unmap,struct vfio_bitmap * bitmap)1279 static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
1280 			     struct vfio_iommu_type1_dma_unmap *unmap,
1281 			     struct vfio_bitmap *bitmap)
1282 {
1283 	struct vfio_dma *dma, *dma_last = NULL;
1284 	size_t unmapped = 0, pgsize;
1285 	int ret = -EINVAL, retries = 0;
1286 	unsigned long pgshift;
1287 	dma_addr_t iova = unmap->iova;
1288 	u64 size = unmap->size;
1289 	bool unmap_all = unmap->flags & VFIO_DMA_UNMAP_FLAG_ALL;
1290 	bool invalidate_vaddr = unmap->flags & VFIO_DMA_UNMAP_FLAG_VADDR;
1291 	struct rb_node *n, *first_n;
1292 
1293 	mutex_lock(&iommu->lock);
1294 
1295 	/* Cannot update vaddr if mdev is present. */
1296 	if (invalidate_vaddr && !list_empty(&iommu->emulated_iommu_groups)) {
1297 		ret = -EBUSY;
1298 		goto unlock;
1299 	}
1300 
1301 	pgshift = __ffs(iommu->pgsize_bitmap);
1302 	pgsize = (size_t)1 << pgshift;
1303 
1304 	if (iova & (pgsize - 1))
1305 		goto unlock;
1306 
1307 	if (unmap_all) {
1308 		if (iova || size)
1309 			goto unlock;
1310 		size = U64_MAX;
1311 	} else if (!size || size & (pgsize - 1) ||
1312 		   iova + size - 1 < iova || size > SIZE_MAX) {
1313 		goto unlock;
1314 	}
1315 
1316 	/* When dirty tracking is enabled, allow only min supported pgsize */
1317 	if ((unmap->flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) &&
1318 	    (!iommu->dirty_page_tracking || (bitmap->pgsize != pgsize))) {
1319 		goto unlock;
1320 	}
1321 
1322 	WARN_ON((pgsize - 1) & PAGE_MASK);
1323 again:
1324 	/*
1325 	 * vfio-iommu-type1 (v1) - User mappings were coalesced together to
1326 	 * avoid tracking individual mappings.  This means that the granularity
1327 	 * of the original mapping was lost and the user was allowed to attempt
1328 	 * to unmap any range.  Depending on the contiguousness of physical
1329 	 * memory and page sizes supported by the IOMMU, arbitrary unmaps may
1330 	 * or may not have worked.  We only guaranteed unmap granularity
1331 	 * matching the original mapping; even though it was untracked here,
1332 	 * the original mappings are reflected in IOMMU mappings.  This
1333 	 * resulted in a couple unusual behaviors.  First, if a range is not
1334 	 * able to be unmapped, ex. a set of 4k pages that was mapped as a
1335 	 * 2M hugepage into the IOMMU, the unmap ioctl returns success but with
1336 	 * a zero sized unmap.  Also, if an unmap request overlaps the first
1337 	 * address of a hugepage, the IOMMU will unmap the entire hugepage.
1338 	 * This also returns success and the returned unmap size reflects the
1339 	 * actual size unmapped.
1340 	 *
1341 	 * We attempt to maintain compatibility with this "v1" interface, but
1342 	 * we take control out of the hands of the IOMMU.  Therefore, an unmap
1343 	 * request offset from the beginning of the original mapping will
1344 	 * return success with zero sized unmap.  And an unmap request covering
1345 	 * the first iova of mapping will unmap the entire range.
1346 	 *
1347 	 * The v2 version of this interface intends to be more deterministic.
1348 	 * Unmap requests must fully cover previous mappings.  Multiple
1349 	 * mappings may still be unmaped by specifying large ranges, but there
1350 	 * must not be any previous mappings bisected by the range.  An error
1351 	 * will be returned if these conditions are not met.  The v2 interface
1352 	 * will only return success and a size of zero if there were no
1353 	 * mappings within the range.
1354 	 */
1355 	if (iommu->v2 && !unmap_all) {
1356 		dma = vfio_find_dma(iommu, iova, 1);
1357 		if (dma && dma->iova != iova)
1358 			goto unlock;
1359 
1360 		dma = vfio_find_dma(iommu, iova + size - 1, 0);
1361 		if (dma && dma->iova + dma->size != iova + size)
1362 			goto unlock;
1363 	}
1364 
1365 	ret = 0;
1366 	n = first_n = vfio_find_dma_first_node(iommu, iova, size);
1367 
1368 	while (n) {
1369 		dma = rb_entry(n, struct vfio_dma, node);
1370 		if (dma->iova >= iova + size)
1371 			break;
1372 
1373 		if (!iommu->v2 && iova > dma->iova)
1374 			break;
1375 
1376 		if (invalidate_vaddr) {
1377 			if (dma->vaddr_invalid) {
1378 				struct rb_node *last_n = n;
1379 
1380 				for (n = first_n; n != last_n; n = rb_next(n)) {
1381 					dma = rb_entry(n,
1382 						       struct vfio_dma, node);
1383 					dma->vaddr_invalid = false;
1384 					iommu->vaddr_invalid_count--;
1385 				}
1386 				ret = -EINVAL;
1387 				unmapped = 0;
1388 				break;
1389 			}
1390 			dma->vaddr_invalid = true;
1391 			iommu->vaddr_invalid_count++;
1392 			unmapped += dma->size;
1393 			n = rb_next(n);
1394 			continue;
1395 		}
1396 
1397 		if (!RB_EMPTY_ROOT(&dma->pfn_list)) {
1398 			if (dma_last == dma) {
1399 				BUG_ON(++retries > 10);
1400 			} else {
1401 				dma_last = dma;
1402 				retries = 0;
1403 			}
1404 
1405 			vfio_notify_dma_unmap(iommu, dma);
1406 			goto again;
1407 		}
1408 
1409 		if (unmap->flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) {
1410 			ret = update_user_bitmap(bitmap->data, iommu, dma,
1411 						 iova, pgsize);
1412 			if (ret)
1413 				break;
1414 		}
1415 
1416 		unmapped += dma->size;
1417 		n = rb_next(n);
1418 		vfio_remove_dma(iommu, dma);
1419 	}
1420 
1421 unlock:
1422 	mutex_unlock(&iommu->lock);
1423 
1424 	/* Report how much was unmapped */
1425 	unmap->size = unmapped;
1426 
1427 	return ret;
1428 }
1429 
vfio_iommu_map(struct vfio_iommu * iommu,dma_addr_t iova,unsigned long pfn,long npage,int prot)1430 static int vfio_iommu_map(struct vfio_iommu *iommu, dma_addr_t iova,
1431 			  unsigned long pfn, long npage, int prot)
1432 {
1433 	struct vfio_domain *d;
1434 	int ret;
1435 
1436 	list_for_each_entry(d, &iommu->domain_list, next) {
1437 		ret = iommu_map(d->domain, iova, (phys_addr_t)pfn << PAGE_SHIFT,
1438 				npage << PAGE_SHIFT, prot | IOMMU_CACHE,
1439 				GFP_KERNEL_ACCOUNT);
1440 		if (ret)
1441 			goto unwind;
1442 
1443 		cond_resched();
1444 	}
1445 
1446 	return 0;
1447 
1448 unwind:
1449 	list_for_each_entry_continue_reverse(d, &iommu->domain_list, next) {
1450 		iommu_unmap(d->domain, iova, npage << PAGE_SHIFT);
1451 		cond_resched();
1452 	}
1453 
1454 	return ret;
1455 }
1456 
vfio_pin_map_dma(struct vfio_iommu * iommu,struct vfio_dma * dma,size_t map_size)1457 static int vfio_pin_map_dma(struct vfio_iommu *iommu, struct vfio_dma *dma,
1458 			    size_t map_size)
1459 {
1460 	dma_addr_t iova = dma->iova;
1461 	unsigned long vaddr = dma->vaddr;
1462 	struct vfio_batch batch;
1463 	size_t size = map_size;
1464 	long npage;
1465 	unsigned long pfn, limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1466 	int ret = 0;
1467 
1468 	vfio_batch_init(&batch);
1469 
1470 	while (size) {
1471 		/* Pin a contiguous chunk of memory */
1472 		npage = vfio_pin_pages_remote(dma, vaddr + dma->size,
1473 					      size >> PAGE_SHIFT, &pfn, limit,
1474 					      &batch);
1475 		if (npage <= 0) {
1476 			WARN_ON(!npage);
1477 			ret = (int)npage;
1478 			break;
1479 		}
1480 
1481 		/* Map it! */
1482 		ret = vfio_iommu_map(iommu, iova + dma->size, pfn, npage,
1483 				     dma->prot);
1484 		if (ret) {
1485 			vfio_unpin_pages_remote(dma, iova + dma->size, pfn,
1486 						npage, true);
1487 			vfio_batch_unpin(&batch, dma);
1488 			break;
1489 		}
1490 
1491 		size -= npage << PAGE_SHIFT;
1492 		dma->size += npage << PAGE_SHIFT;
1493 	}
1494 
1495 	vfio_batch_fini(&batch);
1496 	dma->iommu_mapped = true;
1497 
1498 	if (ret)
1499 		vfio_remove_dma(iommu, dma);
1500 
1501 	return ret;
1502 }
1503 
1504 /*
1505  * Check dma map request is within a valid iova range
1506  */
vfio_iommu_iova_dma_valid(struct vfio_iommu * iommu,dma_addr_t start,dma_addr_t end)1507 static bool vfio_iommu_iova_dma_valid(struct vfio_iommu *iommu,
1508 				      dma_addr_t start, dma_addr_t end)
1509 {
1510 	struct list_head *iova = &iommu->iova_list;
1511 	struct vfio_iova *node;
1512 
1513 	list_for_each_entry(node, iova, list) {
1514 		if (start >= node->start && end <= node->end)
1515 			return true;
1516 	}
1517 
1518 	/*
1519 	 * Check for list_empty() as well since a container with
1520 	 * a single mdev device will have an empty list.
1521 	 */
1522 	return list_empty(iova);
1523 }
1524 
vfio_change_dma_owner(struct vfio_dma * dma)1525 static int vfio_change_dma_owner(struct vfio_dma *dma)
1526 {
1527 	struct task_struct *task = current->group_leader;
1528 	struct mm_struct *mm = current->mm;
1529 	long npage = dma->locked_vm;
1530 	bool lock_cap;
1531 	int ret;
1532 
1533 	if (mm == dma->mm)
1534 		return 0;
1535 
1536 	lock_cap = capable(CAP_IPC_LOCK);
1537 	ret = mm_lock_acct(task, mm, lock_cap, npage);
1538 	if (ret)
1539 		return ret;
1540 
1541 	if (mmget_not_zero(dma->mm)) {
1542 		mm_lock_acct(dma->task, dma->mm, dma->lock_cap, -npage);
1543 		mmput(dma->mm);
1544 	}
1545 
1546 	if (dma->task != task) {
1547 		put_task_struct(dma->task);
1548 		dma->task = get_task_struct(task);
1549 	}
1550 	mmdrop(dma->mm);
1551 	dma->mm = mm;
1552 	mmgrab(dma->mm);
1553 	dma->lock_cap = lock_cap;
1554 	return 0;
1555 }
1556 
vfio_dma_do_map(struct vfio_iommu * iommu,struct vfio_iommu_type1_dma_map * map)1557 static int vfio_dma_do_map(struct vfio_iommu *iommu,
1558 			   struct vfio_iommu_type1_dma_map *map)
1559 {
1560 	bool set_vaddr = map->flags & VFIO_DMA_MAP_FLAG_VADDR;
1561 	dma_addr_t iova = map->iova;
1562 	unsigned long vaddr = map->vaddr;
1563 	size_t size = map->size;
1564 	int ret = 0, prot = 0;
1565 	size_t pgsize;
1566 	struct vfio_dma *dma;
1567 
1568 	/* Verify that none of our __u64 fields overflow */
1569 	if (map->size != size || map->vaddr != vaddr || map->iova != iova)
1570 		return -EINVAL;
1571 
1572 	/* READ/WRITE from device perspective */
1573 	if (map->flags & VFIO_DMA_MAP_FLAG_WRITE)
1574 		prot |= IOMMU_WRITE;
1575 	if (map->flags & VFIO_DMA_MAP_FLAG_READ)
1576 		prot |= IOMMU_READ;
1577 
1578 	if ((prot && set_vaddr) || (!prot && !set_vaddr))
1579 		return -EINVAL;
1580 
1581 	mutex_lock(&iommu->lock);
1582 
1583 	pgsize = (size_t)1 << __ffs(iommu->pgsize_bitmap);
1584 
1585 	WARN_ON((pgsize - 1) & PAGE_MASK);
1586 
1587 	if (!size || (size | iova | vaddr) & (pgsize - 1)) {
1588 		ret = -EINVAL;
1589 		goto out_unlock;
1590 	}
1591 
1592 	/* Don't allow IOVA or virtual address wrap */
1593 	if (iova + size - 1 < iova || vaddr + size - 1 < vaddr) {
1594 		ret = -EINVAL;
1595 		goto out_unlock;
1596 	}
1597 
1598 	dma = vfio_find_dma(iommu, iova, size);
1599 	if (set_vaddr) {
1600 		if (!dma) {
1601 			ret = -ENOENT;
1602 		} else if (!dma->vaddr_invalid || dma->iova != iova ||
1603 			   dma->size != size) {
1604 			ret = -EINVAL;
1605 		} else {
1606 			ret = vfio_change_dma_owner(dma);
1607 			if (ret)
1608 				goto out_unlock;
1609 			dma->vaddr = vaddr;
1610 			dma->vaddr_invalid = false;
1611 			iommu->vaddr_invalid_count--;
1612 		}
1613 		goto out_unlock;
1614 	} else if (dma) {
1615 		ret = -EEXIST;
1616 		goto out_unlock;
1617 	}
1618 
1619 	if (!iommu->dma_avail) {
1620 		ret = -ENOSPC;
1621 		goto out_unlock;
1622 	}
1623 
1624 	if (!vfio_iommu_iova_dma_valid(iommu, iova, iova + size - 1)) {
1625 		ret = -EINVAL;
1626 		goto out_unlock;
1627 	}
1628 
1629 	dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1630 	if (!dma) {
1631 		ret = -ENOMEM;
1632 		goto out_unlock;
1633 	}
1634 
1635 	iommu->dma_avail--;
1636 	dma->iova = iova;
1637 	dma->vaddr = vaddr;
1638 	dma->prot = prot;
1639 
1640 	/*
1641 	 * We need to be able to both add to a task's locked memory and test
1642 	 * against the locked memory limit and we need to be able to do both
1643 	 * outside of this call path as pinning can be asynchronous via the
1644 	 * external interfaces for mdev devices.  RLIMIT_MEMLOCK requires a
1645 	 * task_struct. Save the group_leader so that all DMA tracking uses
1646 	 * the same task, to make debugging easier.  VM locked pages requires
1647 	 * an mm_struct, so grab the mm in case the task dies.
1648 	 */
1649 	get_task_struct(current->group_leader);
1650 	dma->task = current->group_leader;
1651 	dma->lock_cap = capable(CAP_IPC_LOCK);
1652 	dma->mm = current->mm;
1653 	mmgrab(dma->mm);
1654 
1655 	dma->pfn_list = RB_ROOT;
1656 
1657 	/* Insert zero-sized and grow as we map chunks of it */
1658 	vfio_link_dma(iommu, dma);
1659 
1660 	/* Don't pin and map if container doesn't contain IOMMU capable domain*/
1661 	if (list_empty(&iommu->domain_list))
1662 		dma->size = size;
1663 	else
1664 		ret = vfio_pin_map_dma(iommu, dma, size);
1665 
1666 	if (!ret && iommu->dirty_page_tracking) {
1667 		ret = vfio_dma_bitmap_alloc(dma, pgsize);
1668 		if (ret)
1669 			vfio_remove_dma(iommu, dma);
1670 	}
1671 
1672 out_unlock:
1673 	mutex_unlock(&iommu->lock);
1674 	return ret;
1675 }
1676 
vfio_iommu_replay(struct vfio_iommu * iommu,struct vfio_domain * domain)1677 static int vfio_iommu_replay(struct vfio_iommu *iommu,
1678 			     struct vfio_domain *domain)
1679 {
1680 	struct vfio_batch batch;
1681 	struct vfio_domain *d = NULL;
1682 	struct rb_node *n;
1683 	unsigned long limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1684 	int ret;
1685 
1686 	/* Arbitrarily pick the first domain in the list for lookups */
1687 	if (!list_empty(&iommu->domain_list))
1688 		d = list_first_entry(&iommu->domain_list,
1689 				     struct vfio_domain, next);
1690 
1691 	vfio_batch_init(&batch);
1692 
1693 	n = rb_first(&iommu->dma_list);
1694 
1695 	for (; n; n = rb_next(n)) {
1696 		struct vfio_dma *dma;
1697 		dma_addr_t iova;
1698 
1699 		dma = rb_entry(n, struct vfio_dma, node);
1700 		iova = dma->iova;
1701 
1702 		while (iova < dma->iova + dma->size) {
1703 			phys_addr_t phys;
1704 			size_t size;
1705 
1706 			if (dma->iommu_mapped) {
1707 				phys_addr_t p;
1708 				dma_addr_t i;
1709 
1710 				if (WARN_ON(!d)) { /* mapped w/o a domain?! */
1711 					ret = -EINVAL;
1712 					goto unwind;
1713 				}
1714 
1715 				phys = iommu_iova_to_phys(d->domain, iova);
1716 
1717 				if (WARN_ON(!phys)) {
1718 					iova += PAGE_SIZE;
1719 					continue;
1720 				}
1721 
1722 				size = PAGE_SIZE;
1723 				p = phys + size;
1724 				i = iova + size;
1725 				while (i < dma->iova + dma->size &&
1726 				       p == iommu_iova_to_phys(d->domain, i)) {
1727 					size += PAGE_SIZE;
1728 					p += PAGE_SIZE;
1729 					i += PAGE_SIZE;
1730 				}
1731 			} else {
1732 				unsigned long pfn;
1733 				unsigned long vaddr = dma->vaddr +
1734 						     (iova - dma->iova);
1735 				size_t n = dma->iova + dma->size - iova;
1736 				long npage;
1737 
1738 				npage = vfio_pin_pages_remote(dma, vaddr,
1739 							      n >> PAGE_SHIFT,
1740 							      &pfn, limit,
1741 							      &batch);
1742 				if (npage <= 0) {
1743 					WARN_ON(!npage);
1744 					ret = (int)npage;
1745 					goto unwind;
1746 				}
1747 
1748 				phys = pfn << PAGE_SHIFT;
1749 				size = npage << PAGE_SHIFT;
1750 			}
1751 
1752 			ret = iommu_map(domain->domain, iova, phys, size,
1753 					dma->prot | IOMMU_CACHE,
1754 					GFP_KERNEL_ACCOUNT);
1755 			if (ret) {
1756 				if (!dma->iommu_mapped) {
1757 					vfio_unpin_pages_remote(dma, iova,
1758 							phys >> PAGE_SHIFT,
1759 							size >> PAGE_SHIFT,
1760 							true);
1761 					vfio_batch_unpin(&batch, dma);
1762 				}
1763 				goto unwind;
1764 			}
1765 
1766 			iova += size;
1767 		}
1768 	}
1769 
1770 	/* All dmas are now mapped, defer to second tree walk for unwind */
1771 	for (n = rb_first(&iommu->dma_list); n; n = rb_next(n)) {
1772 		struct vfio_dma *dma = rb_entry(n, struct vfio_dma, node);
1773 
1774 		dma->iommu_mapped = true;
1775 	}
1776 
1777 	vfio_batch_fini(&batch);
1778 	return 0;
1779 
1780 unwind:
1781 	for (; n; n = rb_prev(n)) {
1782 		struct vfio_dma *dma = rb_entry(n, struct vfio_dma, node);
1783 		dma_addr_t iova;
1784 
1785 		if (dma->iommu_mapped) {
1786 			iommu_unmap(domain->domain, dma->iova, dma->size);
1787 			continue;
1788 		}
1789 
1790 		iova = dma->iova;
1791 		while (iova < dma->iova + dma->size) {
1792 			phys_addr_t phys, p;
1793 			size_t size;
1794 			dma_addr_t i;
1795 
1796 			phys = iommu_iova_to_phys(domain->domain, iova);
1797 			if (!phys) {
1798 				iova += PAGE_SIZE;
1799 				continue;
1800 			}
1801 
1802 			size = PAGE_SIZE;
1803 			p = phys + size;
1804 			i = iova + size;
1805 			while (i < dma->iova + dma->size &&
1806 			       p == iommu_iova_to_phys(domain->domain, i)) {
1807 				size += PAGE_SIZE;
1808 				p += PAGE_SIZE;
1809 				i += PAGE_SIZE;
1810 			}
1811 
1812 			iommu_unmap(domain->domain, iova, size);
1813 			vfio_unpin_pages_remote(dma, iova, phys >> PAGE_SHIFT,
1814 						size >> PAGE_SHIFT, true);
1815 		}
1816 	}
1817 
1818 	vfio_batch_fini(&batch);
1819 	return ret;
1820 }
1821 
1822 /*
1823  * We change our unmap behavior slightly depending on whether the IOMMU
1824  * supports fine-grained superpages.  IOMMUs like AMD-Vi will use a superpage
1825  * for practically any contiguous power-of-two mapping we give it.  This means
1826  * we don't need to look for contiguous chunks ourselves to make unmapping
1827  * more efficient.  On IOMMUs with coarse-grained super pages, like Intel VT-d
1828  * with discrete 2M/1G/512G/1T superpages, identifying contiguous chunks
1829  * significantly boosts non-hugetlbfs mappings and doesn't seem to hurt when
1830  * hugetlbfs is in use.
1831  */
vfio_test_domain_fgsp(struct vfio_domain * domain,struct list_head * regions)1832 static void vfio_test_domain_fgsp(struct vfio_domain *domain, struct list_head *regions)
1833 {
1834 	int ret, order = get_order(PAGE_SIZE * 2);
1835 	struct vfio_iova *region;
1836 	struct page *pages;
1837 	dma_addr_t start;
1838 
1839 	pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
1840 	if (!pages)
1841 		return;
1842 
1843 	list_for_each_entry(region, regions, list) {
1844 		start = ALIGN(region->start, PAGE_SIZE * 2);
1845 		if (start >= region->end || (region->end - start < PAGE_SIZE * 2))
1846 			continue;
1847 
1848 		ret = iommu_map(domain->domain, start, page_to_phys(pages), PAGE_SIZE * 2,
1849 				IOMMU_READ | IOMMU_WRITE | IOMMU_CACHE,
1850 				GFP_KERNEL_ACCOUNT);
1851 		if (!ret) {
1852 			size_t unmapped = iommu_unmap(domain->domain, start, PAGE_SIZE);
1853 
1854 			if (unmapped == PAGE_SIZE)
1855 				iommu_unmap(domain->domain, start + PAGE_SIZE, PAGE_SIZE);
1856 			else
1857 				domain->fgsp = true;
1858 		}
1859 		break;
1860 	}
1861 
1862 	__free_pages(pages, order);
1863 }
1864 
find_iommu_group(struct vfio_domain * domain,struct iommu_group * iommu_group)1865 static struct vfio_iommu_group *find_iommu_group(struct vfio_domain *domain,
1866 						 struct iommu_group *iommu_group)
1867 {
1868 	struct vfio_iommu_group *g;
1869 
1870 	list_for_each_entry(g, &domain->group_list, next) {
1871 		if (g->iommu_group == iommu_group)
1872 			return g;
1873 	}
1874 
1875 	return NULL;
1876 }
1877 
1878 static struct vfio_iommu_group*
vfio_iommu_find_iommu_group(struct vfio_iommu * iommu,struct iommu_group * iommu_group)1879 vfio_iommu_find_iommu_group(struct vfio_iommu *iommu,
1880 			    struct iommu_group *iommu_group)
1881 {
1882 	struct vfio_iommu_group *group;
1883 	struct vfio_domain *domain;
1884 
1885 	list_for_each_entry(domain, &iommu->domain_list, next) {
1886 		group = find_iommu_group(domain, iommu_group);
1887 		if (group)
1888 			return group;
1889 	}
1890 
1891 	list_for_each_entry(group, &iommu->emulated_iommu_groups, next)
1892 		if (group->iommu_group == iommu_group)
1893 			return group;
1894 	return NULL;
1895 }
1896 
vfio_iommu_has_sw_msi(struct list_head * group_resv_regions,phys_addr_t * base)1897 static bool vfio_iommu_has_sw_msi(struct list_head *group_resv_regions,
1898 				  phys_addr_t *base)
1899 {
1900 	struct iommu_resv_region *region;
1901 	bool ret = false;
1902 
1903 	list_for_each_entry(region, group_resv_regions, list) {
1904 		/*
1905 		 * The presence of any 'real' MSI regions should take
1906 		 * precedence over the software-managed one if the
1907 		 * IOMMU driver happens to advertise both types.
1908 		 */
1909 		if (region->type == IOMMU_RESV_MSI) {
1910 			ret = false;
1911 			break;
1912 		}
1913 
1914 		if (region->type == IOMMU_RESV_SW_MSI) {
1915 			*base = region->start;
1916 			ret = true;
1917 		}
1918 	}
1919 
1920 	return ret;
1921 }
1922 
1923 /*
1924  * This is a helper function to insert an address range to iova list.
1925  * The list is initially created with a single entry corresponding to
1926  * the IOMMU domain geometry to which the device group is attached.
1927  * The list aperture gets modified when a new domain is added to the
1928  * container if the new aperture doesn't conflict with the current one
1929  * or with any existing dma mappings. The list is also modified to
1930  * exclude any reserved regions associated with the device group.
1931  */
vfio_iommu_iova_insert(struct list_head * head,dma_addr_t start,dma_addr_t end)1932 static int vfio_iommu_iova_insert(struct list_head *head,
1933 				  dma_addr_t start, dma_addr_t end)
1934 {
1935 	struct vfio_iova *region;
1936 
1937 	region = kmalloc(sizeof(*region), GFP_KERNEL);
1938 	if (!region)
1939 		return -ENOMEM;
1940 
1941 	INIT_LIST_HEAD(&region->list);
1942 	region->start = start;
1943 	region->end = end;
1944 
1945 	list_add_tail(&region->list, head);
1946 	return 0;
1947 }
1948 
1949 /*
1950  * Check the new iommu aperture conflicts with existing aper or with any
1951  * existing dma mappings.
1952  */
vfio_iommu_aper_conflict(struct vfio_iommu * iommu,dma_addr_t start,dma_addr_t end)1953 static bool vfio_iommu_aper_conflict(struct vfio_iommu *iommu,
1954 				     dma_addr_t start, dma_addr_t end)
1955 {
1956 	struct vfio_iova *first, *last;
1957 	struct list_head *iova = &iommu->iova_list;
1958 
1959 	if (list_empty(iova))
1960 		return false;
1961 
1962 	/* Disjoint sets, return conflict */
1963 	first = list_first_entry(iova, struct vfio_iova, list);
1964 	last = list_last_entry(iova, struct vfio_iova, list);
1965 	if (start > last->end || end < first->start)
1966 		return true;
1967 
1968 	/* Check for any existing dma mappings below the new start */
1969 	if (start > first->start) {
1970 		if (vfio_find_dma(iommu, first->start, start - first->start))
1971 			return true;
1972 	}
1973 
1974 	/* Check for any existing dma mappings beyond the new end */
1975 	if (end < last->end) {
1976 		if (vfio_find_dma(iommu, end + 1, last->end - end))
1977 			return true;
1978 	}
1979 
1980 	return false;
1981 }
1982 
1983 /*
1984  * Resize iommu iova aperture window. This is called only if the new
1985  * aperture has no conflict with existing aperture and dma mappings.
1986  */
vfio_iommu_aper_resize(struct list_head * iova,dma_addr_t start,dma_addr_t end)1987 static int vfio_iommu_aper_resize(struct list_head *iova,
1988 				  dma_addr_t start, dma_addr_t end)
1989 {
1990 	struct vfio_iova *node, *next;
1991 
1992 	if (list_empty(iova))
1993 		return vfio_iommu_iova_insert(iova, start, end);
1994 
1995 	/* Adjust iova list start */
1996 	list_for_each_entry_safe(node, next, iova, list) {
1997 		if (start < node->start)
1998 			break;
1999 		if (start >= node->start && start < node->end) {
2000 			node->start = start;
2001 			break;
2002 		}
2003 		/* Delete nodes before new start */
2004 		list_del(&node->list);
2005 		kfree(node);
2006 	}
2007 
2008 	/* Adjust iova list end */
2009 	list_for_each_entry_safe(node, next, iova, list) {
2010 		if (end > node->end)
2011 			continue;
2012 		if (end > node->start && end <= node->end) {
2013 			node->end = end;
2014 			continue;
2015 		}
2016 		/* Delete nodes after new end */
2017 		list_del(&node->list);
2018 		kfree(node);
2019 	}
2020 
2021 	return 0;
2022 }
2023 
2024 /*
2025  * Check reserved region conflicts with existing dma mappings
2026  */
vfio_iommu_resv_conflict(struct vfio_iommu * iommu,struct list_head * resv_regions)2027 static bool vfio_iommu_resv_conflict(struct vfio_iommu *iommu,
2028 				     struct list_head *resv_regions)
2029 {
2030 	struct iommu_resv_region *region;
2031 
2032 	/* Check for conflict with existing dma mappings */
2033 	list_for_each_entry(region, resv_regions, list) {
2034 		if (region->type == IOMMU_RESV_DIRECT_RELAXABLE)
2035 			continue;
2036 
2037 		if (vfio_find_dma(iommu, region->start, region->length))
2038 			return true;
2039 	}
2040 
2041 	return false;
2042 }
2043 
2044 /*
2045  * Check iova region overlap with  reserved regions and
2046  * exclude them from the iommu iova range
2047  */
vfio_iommu_resv_exclude(struct list_head * iova,struct list_head * resv_regions)2048 static int vfio_iommu_resv_exclude(struct list_head *iova,
2049 				   struct list_head *resv_regions)
2050 {
2051 	struct iommu_resv_region *resv;
2052 	struct vfio_iova *n, *next;
2053 
2054 	list_for_each_entry(resv, resv_regions, list) {
2055 		phys_addr_t start, end;
2056 
2057 		if (resv->type == IOMMU_RESV_DIRECT_RELAXABLE)
2058 			continue;
2059 
2060 		start = resv->start;
2061 		end = resv->start + resv->length - 1;
2062 
2063 		list_for_each_entry_safe(n, next, iova, list) {
2064 			int ret = 0;
2065 
2066 			/* No overlap */
2067 			if (start > n->end || end < n->start)
2068 				continue;
2069 			/*
2070 			 * Insert a new node if current node overlaps with the
2071 			 * reserve region to exclude that from valid iova range.
2072 			 * Note that, new node is inserted before the current
2073 			 * node and finally the current node is deleted keeping
2074 			 * the list updated and sorted.
2075 			 */
2076 			if (start > n->start)
2077 				ret = vfio_iommu_iova_insert(&n->list, n->start,
2078 							     start - 1);
2079 			if (!ret && end < n->end)
2080 				ret = vfio_iommu_iova_insert(&n->list, end + 1,
2081 							     n->end);
2082 			if (ret)
2083 				return ret;
2084 
2085 			list_del(&n->list);
2086 			kfree(n);
2087 		}
2088 	}
2089 
2090 	if (list_empty(iova))
2091 		return -EINVAL;
2092 
2093 	return 0;
2094 }
2095 
vfio_iommu_resv_free(struct list_head * resv_regions)2096 static void vfio_iommu_resv_free(struct list_head *resv_regions)
2097 {
2098 	struct iommu_resv_region *n, *next;
2099 
2100 	list_for_each_entry_safe(n, next, resv_regions, list) {
2101 		list_del(&n->list);
2102 		kfree(n);
2103 	}
2104 }
2105 
vfio_iommu_iova_free(struct list_head * iova)2106 static void vfio_iommu_iova_free(struct list_head *iova)
2107 {
2108 	struct vfio_iova *n, *next;
2109 
2110 	list_for_each_entry_safe(n, next, iova, list) {
2111 		list_del(&n->list);
2112 		kfree(n);
2113 	}
2114 }
2115 
vfio_iommu_iova_get_copy(struct vfio_iommu * iommu,struct list_head * iova_copy)2116 static int vfio_iommu_iova_get_copy(struct vfio_iommu *iommu,
2117 				    struct list_head *iova_copy)
2118 {
2119 	struct list_head *iova = &iommu->iova_list;
2120 	struct vfio_iova *n;
2121 	int ret;
2122 
2123 	list_for_each_entry(n, iova, list) {
2124 		ret = vfio_iommu_iova_insert(iova_copy, n->start, n->end);
2125 		if (ret)
2126 			goto out_free;
2127 	}
2128 
2129 	return 0;
2130 
2131 out_free:
2132 	vfio_iommu_iova_free(iova_copy);
2133 	return ret;
2134 }
2135 
vfio_iommu_iova_insert_copy(struct vfio_iommu * iommu,struct list_head * iova_copy)2136 static void vfio_iommu_iova_insert_copy(struct vfio_iommu *iommu,
2137 					struct list_head *iova_copy)
2138 {
2139 	struct list_head *iova = &iommu->iova_list;
2140 
2141 	vfio_iommu_iova_free(iova);
2142 
2143 	list_splice_tail(iova_copy, iova);
2144 }
2145 
vfio_iommu_domain_alloc(struct device * dev,void * data)2146 static int vfio_iommu_domain_alloc(struct device *dev, void *data)
2147 {
2148 	struct iommu_domain **domain = data;
2149 
2150 	*domain = iommu_domain_alloc(dev->bus);
2151 	return 1; /* Don't iterate */
2152 }
2153 
vfio_iommu_type1_attach_group(void * iommu_data,struct iommu_group * iommu_group,enum vfio_group_type type)2154 static int vfio_iommu_type1_attach_group(void *iommu_data,
2155 		struct iommu_group *iommu_group, enum vfio_group_type type)
2156 {
2157 	struct vfio_iommu *iommu = iommu_data;
2158 	struct vfio_iommu_group *group;
2159 	struct vfio_domain *domain, *d;
2160 	bool resv_msi;
2161 	phys_addr_t resv_msi_base = 0;
2162 	struct iommu_domain_geometry *geo;
2163 	LIST_HEAD(iova_copy);
2164 	LIST_HEAD(group_resv_regions);
2165 	int ret = -EBUSY;
2166 
2167 	mutex_lock(&iommu->lock);
2168 
2169 	/* Attach could require pinning, so disallow while vaddr is invalid. */
2170 	if (iommu->vaddr_invalid_count)
2171 		goto out_unlock;
2172 
2173 	/* Check for duplicates */
2174 	ret = -EINVAL;
2175 	if (vfio_iommu_find_iommu_group(iommu, iommu_group))
2176 		goto out_unlock;
2177 
2178 	ret = -ENOMEM;
2179 	group = kzalloc(sizeof(*group), GFP_KERNEL);
2180 	if (!group)
2181 		goto out_unlock;
2182 	group->iommu_group = iommu_group;
2183 
2184 	if (type == VFIO_EMULATED_IOMMU) {
2185 		list_add(&group->next, &iommu->emulated_iommu_groups);
2186 		/*
2187 		 * An emulated IOMMU group cannot dirty memory directly, it can
2188 		 * only use interfaces that provide dirty tracking.
2189 		 * The iommu scope can only be promoted with the addition of a
2190 		 * dirty tracking group.
2191 		 */
2192 		group->pinned_page_dirty_scope = true;
2193 		ret = 0;
2194 		goto out_unlock;
2195 	}
2196 
2197 	ret = -ENOMEM;
2198 	domain = kzalloc(sizeof(*domain), GFP_KERNEL);
2199 	if (!domain)
2200 		goto out_free_group;
2201 
2202 	/*
2203 	 * Going via the iommu_group iterator avoids races, and trivially gives
2204 	 * us a representative device for the IOMMU API call. We don't actually
2205 	 * want to iterate beyond the first device (if any).
2206 	 */
2207 	ret = -EIO;
2208 	iommu_group_for_each_dev(iommu_group, &domain->domain,
2209 				 vfio_iommu_domain_alloc);
2210 	if (!domain->domain)
2211 		goto out_free_domain;
2212 
2213 	if (iommu->nesting) {
2214 		ret = iommu_enable_nesting(domain->domain);
2215 		if (ret)
2216 			goto out_domain;
2217 	}
2218 
2219 	ret = iommu_attach_group(domain->domain, group->iommu_group);
2220 	if (ret)
2221 		goto out_domain;
2222 
2223 	/* Get aperture info */
2224 	geo = &domain->domain->geometry;
2225 	if (vfio_iommu_aper_conflict(iommu, geo->aperture_start,
2226 				     geo->aperture_end)) {
2227 		ret = -EINVAL;
2228 		goto out_detach;
2229 	}
2230 
2231 	ret = iommu_get_group_resv_regions(iommu_group, &group_resv_regions);
2232 	if (ret)
2233 		goto out_detach;
2234 
2235 	if (vfio_iommu_resv_conflict(iommu, &group_resv_regions)) {
2236 		ret = -EINVAL;
2237 		goto out_detach;
2238 	}
2239 
2240 	/*
2241 	 * We don't want to work on the original iova list as the list
2242 	 * gets modified and in case of failure we have to retain the
2243 	 * original list. Get a copy here.
2244 	 */
2245 	ret = vfio_iommu_iova_get_copy(iommu, &iova_copy);
2246 	if (ret)
2247 		goto out_detach;
2248 
2249 	ret = vfio_iommu_aper_resize(&iova_copy, geo->aperture_start,
2250 				     geo->aperture_end);
2251 	if (ret)
2252 		goto out_detach;
2253 
2254 	ret = vfio_iommu_resv_exclude(&iova_copy, &group_resv_regions);
2255 	if (ret)
2256 		goto out_detach;
2257 
2258 	resv_msi = vfio_iommu_has_sw_msi(&group_resv_regions, &resv_msi_base);
2259 
2260 	INIT_LIST_HEAD(&domain->group_list);
2261 	list_add(&group->next, &domain->group_list);
2262 
2263 	if (!allow_unsafe_interrupts &&
2264 	    !iommu_group_has_isolated_msi(iommu_group)) {
2265 		pr_warn("%s: No interrupt remapping support.  Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",
2266 		       __func__);
2267 		ret = -EPERM;
2268 		goto out_detach;
2269 	}
2270 
2271 	/*
2272 	 * If the IOMMU can block non-coherent operations (ie PCIe TLPs with
2273 	 * no-snoop set) then VFIO always turns this feature on because on Intel
2274 	 * platforms it optimizes KVM to disable wbinvd emulation.
2275 	 */
2276 	if (domain->domain->ops->enforce_cache_coherency)
2277 		domain->enforce_cache_coherency =
2278 			domain->domain->ops->enforce_cache_coherency(
2279 				domain->domain);
2280 
2281 	/*
2282 	 * Try to match an existing compatible domain.  We don't want to
2283 	 * preclude an IOMMU driver supporting multiple bus_types and being
2284 	 * able to include different bus_types in the same IOMMU domain, so
2285 	 * we test whether the domains use the same iommu_ops rather than
2286 	 * testing if they're on the same bus_type.
2287 	 */
2288 	list_for_each_entry(d, &iommu->domain_list, next) {
2289 		if (d->domain->ops == domain->domain->ops &&
2290 		    d->enforce_cache_coherency ==
2291 			    domain->enforce_cache_coherency) {
2292 			iommu_detach_group(domain->domain, group->iommu_group);
2293 			if (!iommu_attach_group(d->domain,
2294 						group->iommu_group)) {
2295 				list_add(&group->next, &d->group_list);
2296 				iommu_domain_free(domain->domain);
2297 				kfree(domain);
2298 				goto done;
2299 			}
2300 
2301 			ret = iommu_attach_group(domain->domain,
2302 						 group->iommu_group);
2303 			if (ret)
2304 				goto out_domain;
2305 		}
2306 	}
2307 
2308 	vfio_test_domain_fgsp(domain, &iova_copy);
2309 
2310 	/* replay mappings on new domains */
2311 	ret = vfio_iommu_replay(iommu, domain);
2312 	if (ret)
2313 		goto out_detach;
2314 
2315 	if (resv_msi) {
2316 		ret = iommu_get_msi_cookie(domain->domain, resv_msi_base);
2317 		if (ret && ret != -ENODEV)
2318 			goto out_detach;
2319 	}
2320 
2321 	list_add(&domain->next, &iommu->domain_list);
2322 	vfio_update_pgsize_bitmap(iommu);
2323 done:
2324 	/* Delete the old one and insert new iova list */
2325 	vfio_iommu_iova_insert_copy(iommu, &iova_copy);
2326 
2327 	/*
2328 	 * An iommu backed group can dirty memory directly and therefore
2329 	 * demotes the iommu scope until it declares itself dirty tracking
2330 	 * capable via the page pinning interface.
2331 	 */
2332 	iommu->num_non_pinned_groups++;
2333 	mutex_unlock(&iommu->lock);
2334 	vfio_iommu_resv_free(&group_resv_regions);
2335 
2336 	return 0;
2337 
2338 out_detach:
2339 	iommu_detach_group(domain->domain, group->iommu_group);
2340 out_domain:
2341 	iommu_domain_free(domain->domain);
2342 	vfio_iommu_iova_free(&iova_copy);
2343 	vfio_iommu_resv_free(&group_resv_regions);
2344 out_free_domain:
2345 	kfree(domain);
2346 out_free_group:
2347 	kfree(group);
2348 out_unlock:
2349 	mutex_unlock(&iommu->lock);
2350 	return ret;
2351 }
2352 
vfio_iommu_unmap_unpin_all(struct vfio_iommu * iommu)2353 static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu)
2354 {
2355 	struct rb_node *node;
2356 
2357 	while ((node = rb_first(&iommu->dma_list)))
2358 		vfio_remove_dma(iommu, rb_entry(node, struct vfio_dma, node));
2359 }
2360 
vfio_iommu_unmap_unpin_reaccount(struct vfio_iommu * iommu)2361 static void vfio_iommu_unmap_unpin_reaccount(struct vfio_iommu *iommu)
2362 {
2363 	struct rb_node *n, *p;
2364 
2365 	n = rb_first(&iommu->dma_list);
2366 	for (; n; n = rb_next(n)) {
2367 		struct vfio_dma *dma;
2368 		long locked = 0, unlocked = 0;
2369 
2370 		dma = rb_entry(n, struct vfio_dma, node);
2371 		unlocked += vfio_unmap_unpin(iommu, dma, false);
2372 		p = rb_first(&dma->pfn_list);
2373 		for (; p; p = rb_next(p)) {
2374 			struct vfio_pfn *vpfn = rb_entry(p, struct vfio_pfn,
2375 							 node);
2376 
2377 			if (!is_invalid_reserved_pfn(vpfn->pfn))
2378 				locked++;
2379 		}
2380 		vfio_lock_acct(dma, locked - unlocked, true);
2381 	}
2382 }
2383 
2384 /*
2385  * Called when a domain is removed in detach. It is possible that
2386  * the removed domain decided the iova aperture window. Modify the
2387  * iova aperture with the smallest window among existing domains.
2388  */
vfio_iommu_aper_expand(struct vfio_iommu * iommu,struct list_head * iova_copy)2389 static void vfio_iommu_aper_expand(struct vfio_iommu *iommu,
2390 				   struct list_head *iova_copy)
2391 {
2392 	struct vfio_domain *domain;
2393 	struct vfio_iova *node;
2394 	dma_addr_t start = 0;
2395 	dma_addr_t end = (dma_addr_t)~0;
2396 
2397 	if (list_empty(iova_copy))
2398 		return;
2399 
2400 	list_for_each_entry(domain, &iommu->domain_list, next) {
2401 		struct iommu_domain_geometry *geo = &domain->domain->geometry;
2402 
2403 		if (geo->aperture_start > start)
2404 			start = geo->aperture_start;
2405 		if (geo->aperture_end < end)
2406 			end = geo->aperture_end;
2407 	}
2408 
2409 	/* Modify aperture limits. The new aper is either same or bigger */
2410 	node = list_first_entry(iova_copy, struct vfio_iova, list);
2411 	node->start = start;
2412 	node = list_last_entry(iova_copy, struct vfio_iova, list);
2413 	node->end = end;
2414 }
2415 
2416 /*
2417  * Called when a group is detached. The reserved regions for that
2418  * group can be part of valid iova now. But since reserved regions
2419  * may be duplicated among groups, populate the iova valid regions
2420  * list again.
2421  */
vfio_iommu_resv_refresh(struct vfio_iommu * iommu,struct list_head * iova_copy)2422 static int vfio_iommu_resv_refresh(struct vfio_iommu *iommu,
2423 				   struct list_head *iova_copy)
2424 {
2425 	struct vfio_domain *d;
2426 	struct vfio_iommu_group *g;
2427 	struct vfio_iova *node;
2428 	dma_addr_t start, end;
2429 	LIST_HEAD(resv_regions);
2430 	int ret;
2431 
2432 	if (list_empty(iova_copy))
2433 		return -EINVAL;
2434 
2435 	list_for_each_entry(d, &iommu->domain_list, next) {
2436 		list_for_each_entry(g, &d->group_list, next) {
2437 			ret = iommu_get_group_resv_regions(g->iommu_group,
2438 							   &resv_regions);
2439 			if (ret)
2440 				goto done;
2441 		}
2442 	}
2443 
2444 	node = list_first_entry(iova_copy, struct vfio_iova, list);
2445 	start = node->start;
2446 	node = list_last_entry(iova_copy, struct vfio_iova, list);
2447 	end = node->end;
2448 
2449 	/* purge the iova list and create new one */
2450 	vfio_iommu_iova_free(iova_copy);
2451 
2452 	ret = vfio_iommu_aper_resize(iova_copy, start, end);
2453 	if (ret)
2454 		goto done;
2455 
2456 	/* Exclude current reserved regions from iova ranges */
2457 	ret = vfio_iommu_resv_exclude(iova_copy, &resv_regions);
2458 done:
2459 	vfio_iommu_resv_free(&resv_regions);
2460 	return ret;
2461 }
2462 
vfio_iommu_type1_detach_group(void * iommu_data,struct iommu_group * iommu_group)2463 static void vfio_iommu_type1_detach_group(void *iommu_data,
2464 					  struct iommu_group *iommu_group)
2465 {
2466 	struct vfio_iommu *iommu = iommu_data;
2467 	struct vfio_domain *domain;
2468 	struct vfio_iommu_group *group;
2469 	bool update_dirty_scope = false;
2470 	LIST_HEAD(iova_copy);
2471 
2472 	mutex_lock(&iommu->lock);
2473 	list_for_each_entry(group, &iommu->emulated_iommu_groups, next) {
2474 		if (group->iommu_group != iommu_group)
2475 			continue;
2476 		update_dirty_scope = !group->pinned_page_dirty_scope;
2477 		list_del(&group->next);
2478 		kfree(group);
2479 
2480 		if (list_empty(&iommu->emulated_iommu_groups) &&
2481 		    list_empty(&iommu->domain_list)) {
2482 			WARN_ON(!list_empty(&iommu->device_list));
2483 			vfio_iommu_unmap_unpin_all(iommu);
2484 		}
2485 		goto detach_group_done;
2486 	}
2487 
2488 	/*
2489 	 * Get a copy of iova list. This will be used to update
2490 	 * and to replace the current one later. Please note that
2491 	 * we will leave the original list as it is if update fails.
2492 	 */
2493 	vfio_iommu_iova_get_copy(iommu, &iova_copy);
2494 
2495 	list_for_each_entry(domain, &iommu->domain_list, next) {
2496 		group = find_iommu_group(domain, iommu_group);
2497 		if (!group)
2498 			continue;
2499 
2500 		iommu_detach_group(domain->domain, group->iommu_group);
2501 		update_dirty_scope = !group->pinned_page_dirty_scope;
2502 		list_del(&group->next);
2503 		kfree(group);
2504 		/*
2505 		 * Group ownership provides privilege, if the group list is
2506 		 * empty, the domain goes away. If it's the last domain with
2507 		 * iommu and external domain doesn't exist, then all the
2508 		 * mappings go away too. If it's the last domain with iommu and
2509 		 * external domain exist, update accounting
2510 		 */
2511 		if (list_empty(&domain->group_list)) {
2512 			if (list_is_singular(&iommu->domain_list)) {
2513 				if (list_empty(&iommu->emulated_iommu_groups)) {
2514 					WARN_ON(!list_empty(
2515 						&iommu->device_list));
2516 					vfio_iommu_unmap_unpin_all(iommu);
2517 				} else {
2518 					vfio_iommu_unmap_unpin_reaccount(iommu);
2519 				}
2520 			}
2521 			iommu_domain_free(domain->domain);
2522 			list_del(&domain->next);
2523 			kfree(domain);
2524 			vfio_iommu_aper_expand(iommu, &iova_copy);
2525 			vfio_update_pgsize_bitmap(iommu);
2526 		}
2527 		break;
2528 	}
2529 
2530 	if (!vfio_iommu_resv_refresh(iommu, &iova_copy))
2531 		vfio_iommu_iova_insert_copy(iommu, &iova_copy);
2532 	else
2533 		vfio_iommu_iova_free(&iova_copy);
2534 
2535 detach_group_done:
2536 	/*
2537 	 * Removal of a group without dirty tracking may allow the iommu scope
2538 	 * to be promoted.
2539 	 */
2540 	if (update_dirty_scope) {
2541 		iommu->num_non_pinned_groups--;
2542 		if (iommu->dirty_page_tracking)
2543 			vfio_iommu_populate_bitmap_full(iommu);
2544 	}
2545 	mutex_unlock(&iommu->lock);
2546 }
2547 
vfio_iommu_type1_open(unsigned long arg)2548 static void *vfio_iommu_type1_open(unsigned long arg)
2549 {
2550 	struct vfio_iommu *iommu;
2551 
2552 	iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
2553 	if (!iommu)
2554 		return ERR_PTR(-ENOMEM);
2555 
2556 	switch (arg) {
2557 	case VFIO_TYPE1_IOMMU:
2558 		break;
2559 	case VFIO_TYPE1_NESTING_IOMMU:
2560 		iommu->nesting = true;
2561 		fallthrough;
2562 	case VFIO_TYPE1v2_IOMMU:
2563 		iommu->v2 = true;
2564 		break;
2565 	default:
2566 		kfree(iommu);
2567 		return ERR_PTR(-EINVAL);
2568 	}
2569 
2570 	INIT_LIST_HEAD(&iommu->domain_list);
2571 	INIT_LIST_HEAD(&iommu->iova_list);
2572 	iommu->dma_list = RB_ROOT;
2573 	iommu->dma_avail = dma_entry_limit;
2574 	mutex_init(&iommu->lock);
2575 	mutex_init(&iommu->device_list_lock);
2576 	INIT_LIST_HEAD(&iommu->device_list);
2577 	iommu->pgsize_bitmap = PAGE_MASK;
2578 	INIT_LIST_HEAD(&iommu->emulated_iommu_groups);
2579 
2580 	return iommu;
2581 }
2582 
vfio_release_domain(struct vfio_domain * domain)2583 static void vfio_release_domain(struct vfio_domain *domain)
2584 {
2585 	struct vfio_iommu_group *group, *group_tmp;
2586 
2587 	list_for_each_entry_safe(group, group_tmp,
2588 				 &domain->group_list, next) {
2589 		iommu_detach_group(domain->domain, group->iommu_group);
2590 		list_del(&group->next);
2591 		kfree(group);
2592 	}
2593 
2594 	iommu_domain_free(domain->domain);
2595 }
2596 
vfio_iommu_type1_release(void * iommu_data)2597 static void vfio_iommu_type1_release(void *iommu_data)
2598 {
2599 	struct vfio_iommu *iommu = iommu_data;
2600 	struct vfio_domain *domain, *domain_tmp;
2601 	struct vfio_iommu_group *group, *next_group;
2602 
2603 	list_for_each_entry_safe(group, next_group,
2604 			&iommu->emulated_iommu_groups, next) {
2605 		list_del(&group->next);
2606 		kfree(group);
2607 	}
2608 
2609 	vfio_iommu_unmap_unpin_all(iommu);
2610 
2611 	list_for_each_entry_safe(domain, domain_tmp,
2612 				 &iommu->domain_list, next) {
2613 		vfio_release_domain(domain);
2614 		list_del(&domain->next);
2615 		kfree(domain);
2616 	}
2617 
2618 	vfio_iommu_iova_free(&iommu->iova_list);
2619 
2620 	kfree(iommu);
2621 }
2622 
vfio_domains_have_enforce_cache_coherency(struct vfio_iommu * iommu)2623 static int vfio_domains_have_enforce_cache_coherency(struct vfio_iommu *iommu)
2624 {
2625 	struct vfio_domain *domain;
2626 	int ret = 1;
2627 
2628 	mutex_lock(&iommu->lock);
2629 	list_for_each_entry(domain, &iommu->domain_list, next) {
2630 		if (!(domain->enforce_cache_coherency)) {
2631 			ret = 0;
2632 			break;
2633 		}
2634 	}
2635 	mutex_unlock(&iommu->lock);
2636 
2637 	return ret;
2638 }
2639 
vfio_iommu_has_emulated(struct vfio_iommu * iommu)2640 static bool vfio_iommu_has_emulated(struct vfio_iommu *iommu)
2641 {
2642 	bool ret;
2643 
2644 	mutex_lock(&iommu->lock);
2645 	ret = !list_empty(&iommu->emulated_iommu_groups);
2646 	mutex_unlock(&iommu->lock);
2647 	return ret;
2648 }
2649 
vfio_iommu_type1_check_extension(struct vfio_iommu * iommu,unsigned long arg)2650 static int vfio_iommu_type1_check_extension(struct vfio_iommu *iommu,
2651 					    unsigned long arg)
2652 {
2653 	switch (arg) {
2654 	case VFIO_TYPE1_IOMMU:
2655 	case VFIO_TYPE1v2_IOMMU:
2656 	case VFIO_TYPE1_NESTING_IOMMU:
2657 	case VFIO_UNMAP_ALL:
2658 		return 1;
2659 	case VFIO_UPDATE_VADDR:
2660 		/*
2661 		 * Disable this feature if mdevs are present.  They cannot
2662 		 * safely pin/unpin/rw while vaddrs are being updated.
2663 		 */
2664 		return iommu && !vfio_iommu_has_emulated(iommu);
2665 	case VFIO_DMA_CC_IOMMU:
2666 		if (!iommu)
2667 			return 0;
2668 		return vfio_domains_have_enforce_cache_coherency(iommu);
2669 	default:
2670 		return 0;
2671 	}
2672 }
2673 
vfio_iommu_iova_add_cap(struct vfio_info_cap * caps,struct vfio_iommu_type1_info_cap_iova_range * cap_iovas,size_t size)2674 static int vfio_iommu_iova_add_cap(struct vfio_info_cap *caps,
2675 		 struct vfio_iommu_type1_info_cap_iova_range *cap_iovas,
2676 		 size_t size)
2677 {
2678 	struct vfio_info_cap_header *header;
2679 	struct vfio_iommu_type1_info_cap_iova_range *iova_cap;
2680 
2681 	header = vfio_info_cap_add(caps, size,
2682 				   VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE, 1);
2683 	if (IS_ERR(header))
2684 		return PTR_ERR(header);
2685 
2686 	iova_cap = container_of(header,
2687 				struct vfio_iommu_type1_info_cap_iova_range,
2688 				header);
2689 	iova_cap->nr_iovas = cap_iovas->nr_iovas;
2690 	memcpy(iova_cap->iova_ranges, cap_iovas->iova_ranges,
2691 	       cap_iovas->nr_iovas * sizeof(*cap_iovas->iova_ranges));
2692 	return 0;
2693 }
2694 
vfio_iommu_iova_build_caps(struct vfio_iommu * iommu,struct vfio_info_cap * caps)2695 static int vfio_iommu_iova_build_caps(struct vfio_iommu *iommu,
2696 				      struct vfio_info_cap *caps)
2697 {
2698 	struct vfio_iommu_type1_info_cap_iova_range *cap_iovas;
2699 	struct vfio_iova *iova;
2700 	size_t size;
2701 	int iovas = 0, i = 0, ret;
2702 
2703 	list_for_each_entry(iova, &iommu->iova_list, list)
2704 		iovas++;
2705 
2706 	if (!iovas) {
2707 		/*
2708 		 * Return 0 as a container with a single mdev device
2709 		 * will have an empty list
2710 		 */
2711 		return 0;
2712 	}
2713 
2714 	size = struct_size(cap_iovas, iova_ranges, iovas);
2715 
2716 	cap_iovas = kzalloc(size, GFP_KERNEL);
2717 	if (!cap_iovas)
2718 		return -ENOMEM;
2719 
2720 	cap_iovas->nr_iovas = iovas;
2721 
2722 	list_for_each_entry(iova, &iommu->iova_list, list) {
2723 		cap_iovas->iova_ranges[i].start = iova->start;
2724 		cap_iovas->iova_ranges[i].end = iova->end;
2725 		i++;
2726 	}
2727 
2728 	ret = vfio_iommu_iova_add_cap(caps, cap_iovas, size);
2729 
2730 	kfree(cap_iovas);
2731 	return ret;
2732 }
2733 
vfio_iommu_migration_build_caps(struct vfio_iommu * iommu,struct vfio_info_cap * caps)2734 static int vfio_iommu_migration_build_caps(struct vfio_iommu *iommu,
2735 					   struct vfio_info_cap *caps)
2736 {
2737 	struct vfio_iommu_type1_info_cap_migration cap_mig = {};
2738 
2739 	cap_mig.header.id = VFIO_IOMMU_TYPE1_INFO_CAP_MIGRATION;
2740 	cap_mig.header.version = 1;
2741 
2742 	cap_mig.flags = 0;
2743 	/* support minimum pgsize */
2744 	cap_mig.pgsize_bitmap = (size_t)1 << __ffs(iommu->pgsize_bitmap);
2745 	cap_mig.max_dirty_bitmap_size = DIRTY_BITMAP_SIZE_MAX;
2746 
2747 	return vfio_info_add_capability(caps, &cap_mig.header, sizeof(cap_mig));
2748 }
2749 
vfio_iommu_dma_avail_build_caps(struct vfio_iommu * iommu,struct vfio_info_cap * caps)2750 static int vfio_iommu_dma_avail_build_caps(struct vfio_iommu *iommu,
2751 					   struct vfio_info_cap *caps)
2752 {
2753 	struct vfio_iommu_type1_info_dma_avail cap_dma_avail;
2754 
2755 	cap_dma_avail.header.id = VFIO_IOMMU_TYPE1_INFO_DMA_AVAIL;
2756 	cap_dma_avail.header.version = 1;
2757 
2758 	cap_dma_avail.avail = iommu->dma_avail;
2759 
2760 	return vfio_info_add_capability(caps, &cap_dma_avail.header,
2761 					sizeof(cap_dma_avail));
2762 }
2763 
vfio_iommu_type1_get_info(struct vfio_iommu * iommu,unsigned long arg)2764 static int vfio_iommu_type1_get_info(struct vfio_iommu *iommu,
2765 				     unsigned long arg)
2766 {
2767 	struct vfio_iommu_type1_info info = {};
2768 	unsigned long minsz;
2769 	struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
2770 	int ret;
2771 
2772 	minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes);
2773 
2774 	if (copy_from_user(&info, (void __user *)arg, minsz))
2775 		return -EFAULT;
2776 
2777 	if (info.argsz < minsz)
2778 		return -EINVAL;
2779 
2780 	minsz = min_t(size_t, info.argsz, sizeof(info));
2781 
2782 	mutex_lock(&iommu->lock);
2783 	info.flags = VFIO_IOMMU_INFO_PGSIZES;
2784 
2785 	info.iova_pgsizes = iommu->pgsize_bitmap;
2786 
2787 	ret = vfio_iommu_migration_build_caps(iommu, &caps);
2788 
2789 	if (!ret)
2790 		ret = vfio_iommu_dma_avail_build_caps(iommu, &caps);
2791 
2792 	if (!ret)
2793 		ret = vfio_iommu_iova_build_caps(iommu, &caps);
2794 
2795 	mutex_unlock(&iommu->lock);
2796 
2797 	if (ret)
2798 		return ret;
2799 
2800 	if (caps.size) {
2801 		info.flags |= VFIO_IOMMU_INFO_CAPS;
2802 
2803 		if (info.argsz < sizeof(info) + caps.size) {
2804 			info.argsz = sizeof(info) + caps.size;
2805 		} else {
2806 			vfio_info_cap_shift(&caps, sizeof(info));
2807 			if (copy_to_user((void __user *)arg +
2808 					sizeof(info), caps.buf,
2809 					caps.size)) {
2810 				kfree(caps.buf);
2811 				return -EFAULT;
2812 			}
2813 			info.cap_offset = sizeof(info);
2814 		}
2815 
2816 		kfree(caps.buf);
2817 	}
2818 
2819 	return copy_to_user((void __user *)arg, &info, minsz) ?
2820 			-EFAULT : 0;
2821 }
2822 
vfio_iommu_type1_map_dma(struct vfio_iommu * iommu,unsigned long arg)2823 static int vfio_iommu_type1_map_dma(struct vfio_iommu *iommu,
2824 				    unsigned long arg)
2825 {
2826 	struct vfio_iommu_type1_dma_map map;
2827 	unsigned long minsz;
2828 	uint32_t mask = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE |
2829 			VFIO_DMA_MAP_FLAG_VADDR;
2830 
2831 	minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
2832 
2833 	if (copy_from_user(&map, (void __user *)arg, minsz))
2834 		return -EFAULT;
2835 
2836 	if (map.argsz < minsz || map.flags & ~mask)
2837 		return -EINVAL;
2838 
2839 	return vfio_dma_do_map(iommu, &map);
2840 }
2841 
vfio_iommu_type1_unmap_dma(struct vfio_iommu * iommu,unsigned long arg)2842 static int vfio_iommu_type1_unmap_dma(struct vfio_iommu *iommu,
2843 				      unsigned long arg)
2844 {
2845 	struct vfio_iommu_type1_dma_unmap unmap;
2846 	struct vfio_bitmap bitmap = { 0 };
2847 	uint32_t mask = VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP |
2848 			VFIO_DMA_UNMAP_FLAG_VADDR |
2849 			VFIO_DMA_UNMAP_FLAG_ALL;
2850 	unsigned long minsz;
2851 	int ret;
2852 
2853 	minsz = offsetofend(struct vfio_iommu_type1_dma_unmap, size);
2854 
2855 	if (copy_from_user(&unmap, (void __user *)arg, minsz))
2856 		return -EFAULT;
2857 
2858 	if (unmap.argsz < minsz || unmap.flags & ~mask)
2859 		return -EINVAL;
2860 
2861 	if ((unmap.flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) &&
2862 	    (unmap.flags & (VFIO_DMA_UNMAP_FLAG_ALL |
2863 			    VFIO_DMA_UNMAP_FLAG_VADDR)))
2864 		return -EINVAL;
2865 
2866 	if (unmap.flags & VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP) {
2867 		unsigned long pgshift;
2868 
2869 		if (unmap.argsz < (minsz + sizeof(bitmap)))
2870 			return -EINVAL;
2871 
2872 		if (copy_from_user(&bitmap,
2873 				   (void __user *)(arg + minsz),
2874 				   sizeof(bitmap)))
2875 			return -EFAULT;
2876 
2877 		if (!access_ok((void __user *)bitmap.data, bitmap.size))
2878 			return -EINVAL;
2879 
2880 		pgshift = __ffs(bitmap.pgsize);
2881 		ret = verify_bitmap_size(unmap.size >> pgshift,
2882 					 bitmap.size);
2883 		if (ret)
2884 			return ret;
2885 	}
2886 
2887 	ret = vfio_dma_do_unmap(iommu, &unmap, &bitmap);
2888 	if (ret)
2889 		return ret;
2890 
2891 	return copy_to_user((void __user *)arg, &unmap, minsz) ?
2892 			-EFAULT : 0;
2893 }
2894 
vfio_iommu_type1_dirty_pages(struct vfio_iommu * iommu,unsigned long arg)2895 static int vfio_iommu_type1_dirty_pages(struct vfio_iommu *iommu,
2896 					unsigned long arg)
2897 {
2898 	struct vfio_iommu_type1_dirty_bitmap dirty;
2899 	uint32_t mask = VFIO_IOMMU_DIRTY_PAGES_FLAG_START |
2900 			VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP |
2901 			VFIO_IOMMU_DIRTY_PAGES_FLAG_GET_BITMAP;
2902 	unsigned long minsz;
2903 	int ret = 0;
2904 
2905 	if (!iommu->v2)
2906 		return -EACCES;
2907 
2908 	minsz = offsetofend(struct vfio_iommu_type1_dirty_bitmap, flags);
2909 
2910 	if (copy_from_user(&dirty, (void __user *)arg, minsz))
2911 		return -EFAULT;
2912 
2913 	if (dirty.argsz < minsz || dirty.flags & ~mask)
2914 		return -EINVAL;
2915 
2916 	/* only one flag should be set at a time */
2917 	if (__ffs(dirty.flags) != __fls(dirty.flags))
2918 		return -EINVAL;
2919 
2920 	if (dirty.flags & VFIO_IOMMU_DIRTY_PAGES_FLAG_START) {
2921 		size_t pgsize;
2922 
2923 		mutex_lock(&iommu->lock);
2924 		pgsize = 1 << __ffs(iommu->pgsize_bitmap);
2925 		if (!iommu->dirty_page_tracking) {
2926 			ret = vfio_dma_bitmap_alloc_all(iommu, pgsize);
2927 			if (!ret)
2928 				iommu->dirty_page_tracking = true;
2929 		}
2930 		mutex_unlock(&iommu->lock);
2931 		return ret;
2932 	} else if (dirty.flags & VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP) {
2933 		mutex_lock(&iommu->lock);
2934 		if (iommu->dirty_page_tracking) {
2935 			iommu->dirty_page_tracking = false;
2936 			vfio_dma_bitmap_free_all(iommu);
2937 		}
2938 		mutex_unlock(&iommu->lock);
2939 		return 0;
2940 	} else if (dirty.flags & VFIO_IOMMU_DIRTY_PAGES_FLAG_GET_BITMAP) {
2941 		struct vfio_iommu_type1_dirty_bitmap_get range;
2942 		unsigned long pgshift;
2943 		size_t data_size = dirty.argsz - minsz;
2944 		size_t iommu_pgsize;
2945 
2946 		if (!data_size || data_size < sizeof(range))
2947 			return -EINVAL;
2948 
2949 		if (copy_from_user(&range, (void __user *)(arg + minsz),
2950 				   sizeof(range)))
2951 			return -EFAULT;
2952 
2953 		if (range.iova + range.size < range.iova)
2954 			return -EINVAL;
2955 		if (!access_ok((void __user *)range.bitmap.data,
2956 			       range.bitmap.size))
2957 			return -EINVAL;
2958 
2959 		pgshift = __ffs(range.bitmap.pgsize);
2960 		ret = verify_bitmap_size(range.size >> pgshift,
2961 					 range.bitmap.size);
2962 		if (ret)
2963 			return ret;
2964 
2965 		mutex_lock(&iommu->lock);
2966 
2967 		iommu_pgsize = (size_t)1 << __ffs(iommu->pgsize_bitmap);
2968 
2969 		/* allow only smallest supported pgsize */
2970 		if (range.bitmap.pgsize != iommu_pgsize) {
2971 			ret = -EINVAL;
2972 			goto out_unlock;
2973 		}
2974 		if (range.iova & (iommu_pgsize - 1)) {
2975 			ret = -EINVAL;
2976 			goto out_unlock;
2977 		}
2978 		if (!range.size || range.size & (iommu_pgsize - 1)) {
2979 			ret = -EINVAL;
2980 			goto out_unlock;
2981 		}
2982 
2983 		if (iommu->dirty_page_tracking)
2984 			ret = vfio_iova_dirty_bitmap(range.bitmap.data,
2985 						     iommu, range.iova,
2986 						     range.size,
2987 						     range.bitmap.pgsize);
2988 		else
2989 			ret = -EINVAL;
2990 out_unlock:
2991 		mutex_unlock(&iommu->lock);
2992 
2993 		return ret;
2994 	}
2995 
2996 	return -EINVAL;
2997 }
2998 
vfio_iommu_type1_ioctl(void * iommu_data,unsigned int cmd,unsigned long arg)2999 static long vfio_iommu_type1_ioctl(void *iommu_data,
3000 				   unsigned int cmd, unsigned long arg)
3001 {
3002 	struct vfio_iommu *iommu = iommu_data;
3003 
3004 	switch (cmd) {
3005 	case VFIO_CHECK_EXTENSION:
3006 		return vfio_iommu_type1_check_extension(iommu, arg);
3007 	case VFIO_IOMMU_GET_INFO:
3008 		return vfio_iommu_type1_get_info(iommu, arg);
3009 	case VFIO_IOMMU_MAP_DMA:
3010 		return vfio_iommu_type1_map_dma(iommu, arg);
3011 	case VFIO_IOMMU_UNMAP_DMA:
3012 		return vfio_iommu_type1_unmap_dma(iommu, arg);
3013 	case VFIO_IOMMU_DIRTY_PAGES:
3014 		return vfio_iommu_type1_dirty_pages(iommu, arg);
3015 	default:
3016 		return -ENOTTY;
3017 	}
3018 }
3019 
vfio_iommu_type1_register_device(void * iommu_data,struct vfio_device * vdev)3020 static void vfio_iommu_type1_register_device(void *iommu_data,
3021 					     struct vfio_device *vdev)
3022 {
3023 	struct vfio_iommu *iommu = iommu_data;
3024 
3025 	if (!vdev->ops->dma_unmap)
3026 		return;
3027 
3028 	/*
3029 	 * list_empty(&iommu->device_list) is tested under the iommu->lock while
3030 	 * iteration for dma_unmap must be done under the device_list_lock.
3031 	 * Holding both locks here allows avoiding the device_list_lock in
3032 	 * several fast paths. See vfio_notify_dma_unmap()
3033 	 */
3034 	mutex_lock(&iommu->lock);
3035 	mutex_lock(&iommu->device_list_lock);
3036 	list_add(&vdev->iommu_entry, &iommu->device_list);
3037 	mutex_unlock(&iommu->device_list_lock);
3038 	mutex_unlock(&iommu->lock);
3039 }
3040 
vfio_iommu_type1_unregister_device(void * iommu_data,struct vfio_device * vdev)3041 static void vfio_iommu_type1_unregister_device(void *iommu_data,
3042 					       struct vfio_device *vdev)
3043 {
3044 	struct vfio_iommu *iommu = iommu_data;
3045 
3046 	if (!vdev->ops->dma_unmap)
3047 		return;
3048 
3049 	mutex_lock(&iommu->lock);
3050 	mutex_lock(&iommu->device_list_lock);
3051 	list_del(&vdev->iommu_entry);
3052 	mutex_unlock(&iommu->device_list_lock);
3053 	mutex_unlock(&iommu->lock);
3054 }
3055 
vfio_iommu_type1_dma_rw_chunk(struct vfio_iommu * iommu,dma_addr_t user_iova,void * data,size_t count,bool write,size_t * copied)3056 static int vfio_iommu_type1_dma_rw_chunk(struct vfio_iommu *iommu,
3057 					 dma_addr_t user_iova, void *data,
3058 					 size_t count, bool write,
3059 					 size_t *copied)
3060 {
3061 	struct mm_struct *mm;
3062 	unsigned long vaddr;
3063 	struct vfio_dma *dma;
3064 	bool kthread = current->mm == NULL;
3065 	size_t offset;
3066 
3067 	*copied = 0;
3068 
3069 	dma = vfio_find_dma(iommu, user_iova, 1);
3070 	if (!dma)
3071 		return -EINVAL;
3072 
3073 	if ((write && !(dma->prot & IOMMU_WRITE)) ||
3074 			!(dma->prot & IOMMU_READ))
3075 		return -EPERM;
3076 
3077 	mm = dma->mm;
3078 	if (!mmget_not_zero(mm))
3079 		return -EPERM;
3080 
3081 	if (kthread)
3082 		kthread_use_mm(mm);
3083 	else if (current->mm != mm)
3084 		goto out;
3085 
3086 	offset = user_iova - dma->iova;
3087 
3088 	if (count > dma->size - offset)
3089 		count = dma->size - offset;
3090 
3091 	vaddr = dma->vaddr + offset;
3092 
3093 	if (write) {
3094 		*copied = copy_to_user((void __user *)vaddr, data,
3095 					 count) ? 0 : count;
3096 		if (*copied && iommu->dirty_page_tracking) {
3097 			unsigned long pgshift = __ffs(iommu->pgsize_bitmap);
3098 			/*
3099 			 * Bitmap populated with the smallest supported page
3100 			 * size
3101 			 */
3102 			bitmap_set(dma->bitmap, offset >> pgshift,
3103 				   ((offset + *copied - 1) >> pgshift) -
3104 				   (offset >> pgshift) + 1);
3105 		}
3106 	} else
3107 		*copied = copy_from_user(data, (void __user *)vaddr,
3108 					   count) ? 0 : count;
3109 	if (kthread)
3110 		kthread_unuse_mm(mm);
3111 out:
3112 	mmput(mm);
3113 	return *copied ? 0 : -EFAULT;
3114 }
3115 
vfio_iommu_type1_dma_rw(void * iommu_data,dma_addr_t user_iova,void * data,size_t count,bool write)3116 static int vfio_iommu_type1_dma_rw(void *iommu_data, dma_addr_t user_iova,
3117 				   void *data, size_t count, bool write)
3118 {
3119 	struct vfio_iommu *iommu = iommu_data;
3120 	int ret = 0;
3121 	size_t done;
3122 
3123 	mutex_lock(&iommu->lock);
3124 
3125 	if (WARN_ONCE(iommu->vaddr_invalid_count,
3126 		      "vfio_dma_rw not allowed with VFIO_UPDATE_VADDR\n")) {
3127 		ret = -EBUSY;
3128 		goto out;
3129 	}
3130 
3131 	while (count > 0) {
3132 		ret = vfio_iommu_type1_dma_rw_chunk(iommu, user_iova, data,
3133 						    count, write, &done);
3134 		if (ret)
3135 			break;
3136 
3137 		count -= done;
3138 		data += done;
3139 		user_iova += done;
3140 	}
3141 
3142 out:
3143 	mutex_unlock(&iommu->lock);
3144 	return ret;
3145 }
3146 
3147 static struct iommu_domain *
vfio_iommu_type1_group_iommu_domain(void * iommu_data,struct iommu_group * iommu_group)3148 vfio_iommu_type1_group_iommu_domain(void *iommu_data,
3149 				    struct iommu_group *iommu_group)
3150 {
3151 	struct iommu_domain *domain = ERR_PTR(-ENODEV);
3152 	struct vfio_iommu *iommu = iommu_data;
3153 	struct vfio_domain *d;
3154 
3155 	if (!iommu || !iommu_group)
3156 		return ERR_PTR(-EINVAL);
3157 
3158 	mutex_lock(&iommu->lock);
3159 	list_for_each_entry(d, &iommu->domain_list, next) {
3160 		if (find_iommu_group(d, iommu_group)) {
3161 			domain = d->domain;
3162 			break;
3163 		}
3164 	}
3165 	mutex_unlock(&iommu->lock);
3166 
3167 	return domain;
3168 }
3169 
3170 static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
3171 	.name			= "vfio-iommu-type1",
3172 	.owner			= THIS_MODULE,
3173 	.open			= vfio_iommu_type1_open,
3174 	.release		= vfio_iommu_type1_release,
3175 	.ioctl			= vfio_iommu_type1_ioctl,
3176 	.attach_group		= vfio_iommu_type1_attach_group,
3177 	.detach_group		= vfio_iommu_type1_detach_group,
3178 	.pin_pages		= vfio_iommu_type1_pin_pages,
3179 	.unpin_pages		= vfio_iommu_type1_unpin_pages,
3180 	.register_device	= vfio_iommu_type1_register_device,
3181 	.unregister_device	= vfio_iommu_type1_unregister_device,
3182 	.dma_rw			= vfio_iommu_type1_dma_rw,
3183 	.group_iommu_domain	= vfio_iommu_type1_group_iommu_domain,
3184 };
3185 
vfio_iommu_type1_init(void)3186 static int __init vfio_iommu_type1_init(void)
3187 {
3188 	return vfio_register_iommu_driver(&vfio_iommu_driver_ops_type1);
3189 }
3190 
vfio_iommu_type1_cleanup(void)3191 static void __exit vfio_iommu_type1_cleanup(void)
3192 {
3193 	vfio_unregister_iommu_driver(&vfio_iommu_driver_ops_type1);
3194 }
3195 
3196 module_init(vfio_iommu_type1_init);
3197 module_exit(vfio_iommu_type1_cleanup);
3198 
3199 MODULE_VERSION(DRIVER_VERSION);
3200 MODULE_LICENSE("GPL v2");
3201 MODULE_AUTHOR(DRIVER_AUTHOR);
3202 MODULE_DESCRIPTION(DRIVER_DESC);
3203