]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/vfio/vfio_iommu_type1.c
vfio: Fix handling of error returned by 'vfio_group_get_from_dev()'
[thirdparty/kernel/stable.git] / drivers / vfio / vfio_iommu_type1.c
CommitLineData
73fa0d10
AW
1/*
2 * VFIO: IOMMU DMA mapping support for Type1 IOMMU
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 *
15 * We arbitrarily define a Type1 IOMMU as one matching the below code.
16 * It could be called the x86 IOMMU as it's designed for AMD-Vi & Intel
17 * VT-d, but that makes it harder to re-use as theoretically anyone
18 * implementing a similar IOMMU could make use of this. We expect the
19 * IOMMU to support the IOMMU API and have few to no restrictions around
20 * the IOVA range that can be mapped. The Type1 IOMMU is currently
21 * optimized for relatively static mappings of a userspace process with
22 * userpsace pages pinned into memory. We also assume devices and IOMMU
23 * domains are PCI based as the IOMMU API is still centered around a
24 * device/bus interface rather than a group interface.
25 */
26
27#include <linux/compat.h>
28#include <linux/device.h>
29#include <linux/fs.h>
30#include <linux/iommu.h>
31#include <linux/module.h>
32#include <linux/mm.h>
cd9b2268 33#include <linux/rbtree.h>
73fa0d10
AW
34#include <linux/sched.h>
35#include <linux/slab.h>
36#include <linux/uaccess.h>
37#include <linux/vfio.h>
38#include <linux/workqueue.h>
8f0d5bb9 39#include <linux/pid_namespace.h>
a54eb550 40#include <linux/mdev.h>
c086de81 41#include <linux/notifier.h>
73fa0d10
AW
42
43#define DRIVER_VERSION "0.2"
44#define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
45#define DRIVER_DESC "Type1 IOMMU driver for VFIO"
46
47static bool allow_unsafe_interrupts;
48module_param_named(allow_unsafe_interrupts,
49 allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR);
50MODULE_PARM_DESC(allow_unsafe_interrupts,
51 "Enable VFIO IOMMU support for on platforms without interrupt remapping support.");
52
5c6c2b21
AW
53static bool disable_hugepages;
54module_param_named(disable_hugepages,
55 disable_hugepages, bool, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(disable_hugepages,
57 "Disable VFIO IOMMU support for IOMMU hugepages.");
58
73fa0d10 59struct vfio_iommu {
1ef3e2bc 60 struct list_head domain_list;
a54eb550 61 struct vfio_domain *external_domain; /* domain for external user */
73fa0d10 62 struct mutex lock;
cd9b2268 63 struct rb_root dma_list;
c086de81 64 struct blocking_notifier_head notifier;
f5c9eceb
WD
65 bool v2;
66 bool nesting;
1ef3e2bc
AW
67};
68
69struct vfio_domain {
70 struct iommu_domain *domain;
71 struct list_head next;
73fa0d10 72 struct list_head group_list;
1ef3e2bc 73 int prot; /* IOMMU_CACHE */
6fe1010d 74 bool fgsp; /* Fine-grained super pages */
73fa0d10
AW
75};
76
77struct vfio_dma {
cd9b2268 78 struct rb_node node;
73fa0d10
AW
79 dma_addr_t iova; /* Device address */
80 unsigned long vaddr; /* Process virtual addr */
166fd7d9 81 size_t size; /* Map size (bytes) */
73fa0d10 82 int prot; /* IOMMU_READ/WRITE */
a54eb550 83 bool iommu_mapped;
8f0d5bb9 84 struct task_struct *task;
a54eb550 85 struct rb_root pfn_list; /* Ex-user pinned pfn list */
73fa0d10
AW
86};
87
88struct vfio_group {
89 struct iommu_group *iommu_group;
90 struct list_head next;
91};
92
a54eb550
KW
93/*
94 * Guest RAM pinning working set or DMA target
95 */
96struct vfio_pfn {
97 struct rb_node node;
98 dma_addr_t iova; /* Device address */
99 unsigned long pfn; /* Host pfn */
100 atomic_t ref_count;
101};
102
103#define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu) \
104 (!list_empty(&iommu->domain_list))
105
106static int put_pfn(unsigned long pfn, int prot);
107
73fa0d10
AW
108/*
109 * This code handles mapping and unmapping of user data buffers
110 * into DMA'ble space using the IOMMU
111 */
112
cd9b2268
AW
113static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
114 dma_addr_t start, size_t size)
115{
116 struct rb_node *node = iommu->dma_list.rb_node;
117
118 while (node) {
119 struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node);
120
121 if (start + size <= dma->iova)
122 node = node->rb_left;
166fd7d9 123 else if (start >= dma->iova + dma->size)
cd9b2268
AW
124 node = node->rb_right;
125 else
126 return dma;
127 }
128
129 return NULL;
130}
131
1ef3e2bc 132static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
cd9b2268
AW
133{
134 struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL;
135 struct vfio_dma *dma;
136
137 while (*link) {
138 parent = *link;
139 dma = rb_entry(parent, struct vfio_dma, node);
140
166fd7d9 141 if (new->iova + new->size <= dma->iova)
cd9b2268
AW
142 link = &(*link)->rb_left;
143 else
144 link = &(*link)->rb_right;
145 }
146
147 rb_link_node(&new->node, parent, link);
148 rb_insert_color(&new->node, &iommu->dma_list);
149}
150
1ef3e2bc 151static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old)
cd9b2268
AW
152{
153 rb_erase(&old->node, &iommu->dma_list);
154}
155
a54eb550
KW
156/*
157 * Helper Functions for host iova-pfn list
158 */
159static struct vfio_pfn *vfio_find_vpfn(struct vfio_dma *dma, dma_addr_t iova)
160{
161 struct vfio_pfn *vpfn;
162 struct rb_node *node = dma->pfn_list.rb_node;
163
164 while (node) {
165 vpfn = rb_entry(node, struct vfio_pfn, node);
166
167 if (iova < vpfn->iova)
168 node = node->rb_left;
169 else if (iova > vpfn->iova)
170 node = node->rb_right;
171 else
172 return vpfn;
173 }
174 return NULL;
175}
176
177static void vfio_link_pfn(struct vfio_dma *dma,
178 struct vfio_pfn *new)
179{
180 struct rb_node **link, *parent = NULL;
181 struct vfio_pfn *vpfn;
182
183 link = &dma->pfn_list.rb_node;
184 while (*link) {
185 parent = *link;
186 vpfn = rb_entry(parent, struct vfio_pfn, node);
187
188 if (new->iova < vpfn->iova)
189 link = &(*link)->rb_left;
190 else
191 link = &(*link)->rb_right;
192 }
193
194 rb_link_node(&new->node, parent, link);
195 rb_insert_color(&new->node, &dma->pfn_list);
196}
197
198static void vfio_unlink_pfn(struct vfio_dma *dma, struct vfio_pfn *old)
199{
200 rb_erase(&old->node, &dma->pfn_list);
201}
202
203static int vfio_add_to_pfn_list(struct vfio_dma *dma, dma_addr_t iova,
204 unsigned long pfn)
205{
206 struct vfio_pfn *vpfn;
207
208 vpfn = kzalloc(sizeof(*vpfn), GFP_KERNEL);
209 if (!vpfn)
210 return -ENOMEM;
211
212 vpfn->iova = iova;
213 vpfn->pfn = pfn;
214 atomic_set(&vpfn->ref_count, 1);
215 vfio_link_pfn(dma, vpfn);
216 return 0;
217}
218
219static void vfio_remove_from_pfn_list(struct vfio_dma *dma,
220 struct vfio_pfn *vpfn)
221{
222 vfio_unlink_pfn(dma, vpfn);
223 kfree(vpfn);
224}
225
226static struct vfio_pfn *vfio_iova_get_vfio_pfn(struct vfio_dma *dma,
227 unsigned long iova)
228{
229 struct vfio_pfn *vpfn = vfio_find_vpfn(dma, iova);
230
231 if (vpfn)
232 atomic_inc(&vpfn->ref_count);
233 return vpfn;
234}
235
236static int vfio_iova_put_vfio_pfn(struct vfio_dma *dma, struct vfio_pfn *vpfn)
237{
238 int ret = 0;
239
240 if (atomic_dec_and_test(&vpfn->ref_count)) {
241 ret = put_pfn(vpfn->pfn, dma->prot);
242 vfio_remove_from_pfn_list(dma, vpfn);
243 }
244 return ret;
245}
246
73fa0d10
AW
247struct vwork {
248 struct mm_struct *mm;
249 long npage;
250 struct work_struct work;
251};
252
253/* delayed decrement/increment for locked_vm */
254static void vfio_lock_acct_bg(struct work_struct *work)
255{
256 struct vwork *vwork = container_of(work, struct vwork, work);
257 struct mm_struct *mm;
258
259 mm = vwork->mm;
260 down_write(&mm->mmap_sem);
261 mm->locked_vm += vwork->npage;
262 up_write(&mm->mmap_sem);
263 mmput(mm);
264 kfree(vwork);
265}
266
3624a248 267static void vfio_lock_acct(struct task_struct *task, long npage)
73fa0d10
AW
268{
269 struct vwork *vwork;
270 struct mm_struct *mm;
271
3624a248
KW
272 if (!npage)
273 return;
274
275 mm = get_task_mm(task);
276 if (!mm)
166fd7d9 277 return; /* process exited or nothing to do */
73fa0d10 278
3624a248
KW
279 if (down_write_trylock(&mm->mmap_sem)) {
280 mm->locked_vm += npage;
281 up_write(&mm->mmap_sem);
282 mmput(mm);
73fa0d10
AW
283 return;
284 }
285
286 /*
287 * Couldn't get mmap_sem lock, so must setup to update
288 * mm->locked_vm later. If locked_vm were atomic, we
289 * wouldn't need this silliness
290 */
291 vwork = kmalloc(sizeof(struct vwork), GFP_KERNEL);
3624a248
KW
292 if (!vwork) {
293 mmput(mm);
73fa0d10
AW
294 return;
295 }
296 INIT_WORK(&vwork->work, vfio_lock_acct_bg);
297 vwork->mm = mm;
298 vwork->npage = npage;
299 schedule_work(&vwork->work);
300}
301
302/*
303 * Some mappings aren't backed by a struct page, for example an mmap'd
304 * MMIO range for our own or another device. These use a different
305 * pfn conversion and shouldn't be tracked as locked pages.
306 */
307static bool is_invalid_reserved_pfn(unsigned long pfn)
308{
309 if (pfn_valid(pfn)) {
310 bool reserved;
311 struct page *tail = pfn_to_page(pfn);
668f9abb 312 struct page *head = compound_head(tail);
73fa0d10
AW
313 reserved = !!(PageReserved(head));
314 if (head != tail) {
315 /*
316 * "head" is not a dangling pointer
668f9abb 317 * (compound_head takes care of that)
73fa0d10
AW
318 * but the hugepage may have been split
319 * from under us (and we may not hold a
320 * reference count on the head page so it can
321 * be reused before we run PageReferenced), so
322 * we've to check PageTail before returning
323 * what we just read.
324 */
325 smp_rmb();
326 if (PageTail(tail))
327 return reserved;
328 }
329 return PageReserved(tail);
330 }
331
332 return true;
333}
334
335static int put_pfn(unsigned long pfn, int prot)
336{
337 if (!is_invalid_reserved_pfn(pfn)) {
338 struct page *page = pfn_to_page(pfn);
339 if (prot & IOMMU_WRITE)
340 SetPageDirty(page);
341 put_page(page);
342 return 1;
343 }
344 return 0;
345}
346
ea85cf35
KW
347static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr,
348 int prot, unsigned long *pfn)
73fa0d10
AW
349{
350 struct page *page[1];
351 struct vm_area_struct *vma;
ea85cf35 352 int ret;
73fa0d10 353
ea85cf35
KW
354 if (mm == current->mm) {
355 ret = get_user_pages_fast(vaddr, 1, !!(prot & IOMMU_WRITE),
356 page);
357 } else {
358 unsigned int flags = 0;
359
360 if (prot & IOMMU_WRITE)
361 flags |= FOLL_WRITE;
362
363 down_read(&mm->mmap_sem);
364 ret = get_user_pages_remote(NULL, mm, vaddr, 1, flags, page,
365 NULL);
366 up_read(&mm->mmap_sem);
367 }
368
369 if (ret == 1) {
73fa0d10
AW
370 *pfn = page_to_pfn(page[0]);
371 return 0;
372 }
373
ea85cf35 374 down_read(&mm->mmap_sem);
73fa0d10 375
ea85cf35 376 vma = find_vma_intersection(mm, vaddr, vaddr + 1);
73fa0d10
AW
377
378 if (vma && vma->vm_flags & VM_PFNMAP) {
379 *pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
380 if (is_invalid_reserved_pfn(*pfn))
381 ret = 0;
382 }
383
ea85cf35 384 up_read(&mm->mmap_sem);
73fa0d10
AW
385 return ret;
386}
387
166fd7d9
AW
388/*
389 * Attempt to pin pages. We really don't want to track all the pfns and
390 * the iommu can only map chunks of consecutive pfns anyway, so get the
391 * first page and all consecutive pages with the same locking.
392 */
8f0d5bb9 393static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,
a54eb550 394 long npage, unsigned long *pfn_base)
73fa0d10 395{
8f0d5bb9
KW
396 unsigned long limit;
397 bool lock_cap = ns_capable(task_active_pid_ns(dma->task)->user_ns,
398 CAP_IPC_LOCK);
399 struct mm_struct *mm;
a54eb550 400 long ret, i = 0, lock_acct = 0;
babbf176 401 bool rsvd;
a54eb550 402 dma_addr_t iova = vaddr - dma->vaddr + dma->iova;
73fa0d10 403
8f0d5bb9
KW
404 mm = get_task_mm(dma->task);
405 if (!mm)
166fd7d9 406 return -ENODEV;
73fa0d10 407
a54eb550 408 ret = vaddr_get_pfn(mm, vaddr, dma->prot, pfn_base);
166fd7d9 409 if (ret)
8f0d5bb9 410 goto pin_pg_remote_exit;
73fa0d10 411
babbf176 412 rsvd = is_invalid_reserved_pfn(*pfn_base);
8f0d5bb9 413 limit = task_rlimit(dma->task, RLIMIT_MEMLOCK) >> PAGE_SHIFT;
73fa0d10 414
a54eb550
KW
415 /*
416 * Reserved pages aren't counted against the user, externally pinned
417 * pages are already counted against the user.
418 */
419 if (!rsvd && !vfio_find_vpfn(dma, iova)) {
420 if (!lock_cap && mm->locked_vm + 1 > limit) {
421 put_pfn(*pfn_base, dma->prot);
422 pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", __func__,
423 limit << PAGE_SHIFT);
424 ret = -ENOMEM;
425 goto pin_pg_remote_exit;
426 }
427 lock_acct++;
5c6c2b21
AW
428 }
429
a54eb550
KW
430 i++;
431 if (likely(!disable_hugepages)) {
432 /* Lock all the consecutive pages from pfn_base */
433 for (vaddr += PAGE_SIZE, iova += PAGE_SIZE; i < npage;
434 i++, vaddr += PAGE_SIZE, iova += PAGE_SIZE) {
435 unsigned long pfn = 0;
73fa0d10 436
a54eb550
KW
437 ret = vaddr_get_pfn(mm, vaddr, dma->prot, &pfn);
438 if (ret)
439 break;
166fd7d9 440
a54eb550
KW
441 if (pfn != *pfn_base + i ||
442 rsvd != is_invalid_reserved_pfn(pfn)) {
443 put_pfn(pfn, dma->prot);
444 break;
445 }
73fa0d10 446
a54eb550
KW
447 if (!rsvd && !vfio_find_vpfn(dma, iova)) {
448 if (!lock_cap &&
449 mm->locked_vm + lock_acct + 1 > limit) {
450 put_pfn(pfn, dma->prot);
451 pr_warn("%s: RLIMIT_MEMLOCK (%ld) "
452 "exceeded\n", __func__,
453 limit << PAGE_SHIFT);
454 break;
455 }
456 lock_acct++;
457 }
166fd7d9
AW
458 }
459 }
460
a54eb550 461 vfio_lock_acct(dma->task, lock_acct);
8f0d5bb9 462 ret = i;
166fd7d9 463
8f0d5bb9
KW
464pin_pg_remote_exit:
465 mmput(mm);
466 return ret;
166fd7d9
AW
467}
468
a54eb550
KW
469static long vfio_unpin_pages_remote(struct vfio_dma *dma, dma_addr_t iova,
470 unsigned long pfn, long npage,
471 bool do_accounting)
166fd7d9 472{
a54eb550 473 long unlocked = 0, locked = 0;
166fd7d9
AW
474 long i;
475
a54eb550
KW
476 for (i = 0; i < npage; i++) {
477 if (put_pfn(pfn++, dma->prot)) {
478 unlocked++;
479 if (vfio_find_vpfn(dma, iova + (i << PAGE_SHIFT)))
480 locked++;
481 }
482 }
483
484 if (do_accounting)
485 vfio_lock_acct(dma->task, locked - unlocked);
486
487 return unlocked;
488}
489
490static int vfio_pin_page_external(struct vfio_dma *dma, unsigned long vaddr,
491 unsigned long *pfn_base, bool do_accounting)
492{
493 unsigned long limit;
494 bool lock_cap = ns_capable(task_active_pid_ns(dma->task)->user_ns,
495 CAP_IPC_LOCK);
496 struct mm_struct *mm;
497 int ret;
498 bool rsvd;
499
500 mm = get_task_mm(dma->task);
501 if (!mm)
502 return -ENODEV;
503
504 ret = vaddr_get_pfn(mm, vaddr, dma->prot, pfn_base);
505 if (ret)
506 goto pin_page_exit;
507
508 rsvd = is_invalid_reserved_pfn(*pfn_base);
509 limit = task_rlimit(dma->task, RLIMIT_MEMLOCK) >> PAGE_SHIFT;
510
511 if (!rsvd && !lock_cap && mm->locked_vm + 1 > limit) {
512 put_pfn(*pfn_base, dma->prot);
513 pr_warn("%s: Task %s (%d) RLIMIT_MEMLOCK (%ld) exceeded\n",
514 __func__, dma->task->comm, task_pid_nr(dma->task),
515 limit << PAGE_SHIFT);
516 ret = -ENOMEM;
517 goto pin_page_exit;
518 }
519
520 if (!rsvd && do_accounting)
521 vfio_lock_acct(dma->task, 1);
522 ret = 1;
523
524pin_page_exit:
525 mmput(mm);
526 return ret;
527}
528
529static int vfio_unpin_page_external(struct vfio_dma *dma, dma_addr_t iova,
530 bool do_accounting)
531{
532 int unlocked;
533 struct vfio_pfn *vpfn = vfio_find_vpfn(dma, iova);
534
535 if (!vpfn)
536 return 0;
537
538 unlocked = vfio_iova_put_vfio_pfn(dma, vpfn);
166fd7d9
AW
539
540 if (do_accounting)
8f0d5bb9 541 vfio_lock_acct(dma->task, -unlocked);
166fd7d9
AW
542
543 return unlocked;
544}
545
a54eb550
KW
546static int vfio_iommu_type1_pin_pages(void *iommu_data,
547 unsigned long *user_pfn,
548 int npage, int prot,
549 unsigned long *phys_pfn)
550{
551 struct vfio_iommu *iommu = iommu_data;
552 int i, j, ret;
553 unsigned long remote_vaddr;
554 struct vfio_dma *dma;
555 bool do_accounting;
556
557 if (!iommu || !user_pfn || !phys_pfn)
558 return -EINVAL;
559
560 /* Supported for v2 version only */
561 if (!iommu->v2)
562 return -EACCES;
563
564 mutex_lock(&iommu->lock);
565
c086de81
KW
566 /* Fail if notifier list is empty */
567 if ((!iommu->external_domain) || (!iommu->notifier.head)) {
a54eb550
KW
568 ret = -EINVAL;
569 goto pin_done;
570 }
571
572 /*
573 * If iommu capable domain exist in the container then all pages are
574 * already pinned and accounted. Accouting should be done if there is no
575 * iommu capable domain in the container.
576 */
577 do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu);
578
579 for (i = 0; i < npage; i++) {
580 dma_addr_t iova;
581 struct vfio_pfn *vpfn;
582
583 iova = user_pfn[i] << PAGE_SHIFT;
584 dma = vfio_find_dma(iommu, iova, 0);
585 if (!dma) {
586 ret = -EINVAL;
587 goto pin_unwind;
588 }
589
590 if ((dma->prot & prot) != prot) {
591 ret = -EPERM;
592 goto pin_unwind;
593 }
594
595 vpfn = vfio_iova_get_vfio_pfn(dma, iova);
596 if (vpfn) {
597 phys_pfn[i] = vpfn->pfn;
598 continue;
599 }
600
601 remote_vaddr = dma->vaddr + iova - dma->iova;
602 ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn[i],
603 do_accounting);
604 if (ret <= 0) {
605 WARN_ON(!ret);
606 goto pin_unwind;
607 }
608
609 ret = vfio_add_to_pfn_list(dma, iova, phys_pfn[i]);
610 if (ret) {
611 vfio_unpin_page_external(dma, iova, do_accounting);
612 goto pin_unwind;
613 }
614 }
615
616 ret = i;
617 goto pin_done;
618
619pin_unwind:
620 phys_pfn[i] = 0;
621 for (j = 0; j < i; j++) {
622 dma_addr_t iova;
623
624 iova = user_pfn[j] << PAGE_SHIFT;
625 dma = vfio_find_dma(iommu, iova, 0);
626 vfio_unpin_page_external(dma, iova, do_accounting);
627 phys_pfn[j] = 0;
628 }
629pin_done:
630 mutex_unlock(&iommu->lock);
631 return ret;
632}
633
634static int vfio_iommu_type1_unpin_pages(void *iommu_data,
635 unsigned long *user_pfn,
636 int npage)
637{
638 struct vfio_iommu *iommu = iommu_data;
639 bool do_accounting;
640 int i;
641
642 if (!iommu || !user_pfn)
643 return -EINVAL;
644
645 /* Supported for v2 version only */
646 if (!iommu->v2)
647 return -EACCES;
648
649 mutex_lock(&iommu->lock);
650
651 if (!iommu->external_domain) {
652 mutex_unlock(&iommu->lock);
653 return -EINVAL;
654 }
655
656 do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu);
657 for (i = 0; i < npage; i++) {
658 struct vfio_dma *dma;
659 dma_addr_t iova;
660
661 iova = user_pfn[i] << PAGE_SHIFT;
662 dma = vfio_find_dma(iommu, iova, 0);
663 if (!dma)
664 goto unpin_exit;
665 vfio_unpin_page_external(dma, iova, do_accounting);
666 }
667
668unpin_exit:
669 mutex_unlock(&iommu->lock);
670 return i > npage ? npage : (i > 0 ? i : -EINVAL);
671}
672
673static long vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma,
674 bool do_accounting)
166fd7d9 675{
1ef3e2bc
AW
676 dma_addr_t iova = dma->iova, end = dma->iova + dma->size;
677 struct vfio_domain *domain, *d;
166fd7d9
AW
678 long unlocked = 0;
679
1ef3e2bc 680 if (!dma->size)
a54eb550
KW
681 return 0;
682
683 if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
684 return 0;
685
1ef3e2bc
AW
686 /*
687 * We use the IOMMU to track the physical addresses, otherwise we'd
688 * need a much more complicated tracking system. Unfortunately that
689 * means we need to use one of the iommu domains to figure out the
690 * pfns to unpin. The rest need to be unmapped in advance so we have
691 * no iommu translations remaining when the pages are unpinned.
692 */
693 domain = d = list_first_entry(&iommu->domain_list,
694 struct vfio_domain, next);
695
c5e66887 696 list_for_each_entry_continue(d, &iommu->domain_list, next) {
1ef3e2bc 697 iommu_unmap(d->domain, dma->iova, dma->size);
c5e66887
AW
698 cond_resched();
699 }
1ef3e2bc 700
166fd7d9 701 while (iova < end) {
6fe1010d
AW
702 size_t unmapped, len;
703 phys_addr_t phys, next;
166fd7d9 704
1ef3e2bc 705 phys = iommu_iova_to_phys(domain->domain, iova);
166fd7d9
AW
706 if (WARN_ON(!phys)) {
707 iova += PAGE_SIZE;
708 continue;
73fa0d10 709 }
166fd7d9 710
6fe1010d
AW
711 /*
712 * To optimize for fewer iommu_unmap() calls, each of which
713 * may require hardware cache flushing, try to find the
714 * largest contiguous physical memory chunk to unmap.
715 */
716 for (len = PAGE_SIZE;
717 !domain->fgsp && iova + len < end; len += PAGE_SIZE) {
718 next = iommu_iova_to_phys(domain->domain, iova + len);
719 if (next != phys + len)
720 break;
721 }
722
723 unmapped = iommu_unmap(domain->domain, iova, len);
1ef3e2bc 724 if (WARN_ON(!unmapped))
166fd7d9
AW
725 break;
726
a54eb550
KW
727 unlocked += vfio_unpin_pages_remote(dma, iova,
728 phys >> PAGE_SHIFT,
2169037d 729 unmapped >> PAGE_SHIFT,
a54eb550 730 false);
166fd7d9 731 iova += unmapped;
c5e66887
AW
732
733 cond_resched();
73fa0d10 734 }
166fd7d9 735
a54eb550
KW
736 dma->iommu_mapped = false;
737 if (do_accounting) {
738 vfio_lock_acct(dma->task, -unlocked);
739 return 0;
740 }
741 return unlocked;
73fa0d10
AW
742}
743
1ef3e2bc 744static void vfio_remove_dma(struct vfio_iommu *iommu, struct vfio_dma *dma)
73fa0d10 745{
a54eb550 746 vfio_unmap_unpin(iommu, dma, true);
1ef3e2bc 747 vfio_unlink_dma(iommu, dma);
8f0d5bb9 748 put_task_struct(dma->task);
1ef3e2bc
AW
749 kfree(dma);
750}
73fa0d10 751
1ef3e2bc
AW
752static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu)
753{
754 struct vfio_domain *domain;
4644321f 755 unsigned long bitmap = ULONG_MAX;
166fd7d9 756
1ef3e2bc
AW
757 mutex_lock(&iommu->lock);
758 list_for_each_entry(domain, &iommu->domain_list, next)
d16e0faa 759 bitmap &= domain->domain->pgsize_bitmap;
1ef3e2bc 760 mutex_unlock(&iommu->lock);
73fa0d10 761
4644321f
EA
762 /*
763 * In case the IOMMU supports page sizes smaller than PAGE_SIZE
764 * we pretend PAGE_SIZE is supported and hide sub-PAGE_SIZE sizes.
765 * That way the user will be able to map/unmap buffers whose size/
766 * start address is aligned with PAGE_SIZE. Pinning code uses that
767 * granularity while iommu driver can use the sub-PAGE_SIZE size
768 * to map the buffer.
769 */
770 if (bitmap & ~PAGE_MASK) {
771 bitmap &= PAGE_MASK;
772 bitmap |= PAGE_SIZE;
773 }
774
1ef3e2bc 775 return bitmap;
73fa0d10
AW
776}
777
778static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
779 struct vfio_iommu_type1_dma_unmap *unmap)
780{
73fa0d10 781 uint64_t mask;
c086de81 782 struct vfio_dma *dma, *dma_last = NULL;
1ef3e2bc 783 size_t unmapped = 0;
c086de81 784 int ret = 0, retries = 0;
73fa0d10 785
1ef3e2bc 786 mask = ((uint64_t)1 << __ffs(vfio_pgsize_bitmap(iommu))) - 1;
73fa0d10
AW
787
788 if (unmap->iova & mask)
789 return -EINVAL;
f5bfdbf2 790 if (!unmap->size || unmap->size & mask)
73fa0d10
AW
791 return -EINVAL;
792
73fa0d10 793 WARN_ON(mask & PAGE_MASK);
c086de81 794again:
73fa0d10
AW
795 mutex_lock(&iommu->lock);
796
1ef3e2bc
AW
797 /*
798 * vfio-iommu-type1 (v1) - User mappings were coalesced together to
799 * avoid tracking individual mappings. This means that the granularity
800 * of the original mapping was lost and the user was allowed to attempt
801 * to unmap any range. Depending on the contiguousness of physical
802 * memory and page sizes supported by the IOMMU, arbitrary unmaps may
803 * or may not have worked. We only guaranteed unmap granularity
804 * matching the original mapping; even though it was untracked here,
805 * the original mappings are reflected in IOMMU mappings. This
806 * resulted in a couple unusual behaviors. First, if a range is not
807 * able to be unmapped, ex. a set of 4k pages that was mapped as a
808 * 2M hugepage into the IOMMU, the unmap ioctl returns success but with
809 * a zero sized unmap. Also, if an unmap request overlaps the first
810 * address of a hugepage, the IOMMU will unmap the entire hugepage.
811 * This also returns success and the returned unmap size reflects the
812 * actual size unmapped.
813 *
814 * We attempt to maintain compatibility with this "v1" interface, but
815 * we take control out of the hands of the IOMMU. Therefore, an unmap
816 * request offset from the beginning of the original mapping will
817 * return success with zero sized unmap. And an unmap request covering
818 * the first iova of mapping will unmap the entire range.
819 *
820 * The v2 version of this interface intends to be more deterministic.
821 * Unmap requests must fully cover previous mappings. Multiple
822 * mappings may still be unmaped by specifying large ranges, but there
823 * must not be any previous mappings bisected by the range. An error
824 * will be returned if these conditions are not met. The v2 interface
825 * will only return success and a size of zero if there were no
826 * mappings within the range.
827 */
828 if (iommu->v2) {
829 dma = vfio_find_dma(iommu, unmap->iova, 0);
830 if (dma && dma->iova != unmap->iova) {
831 ret = -EINVAL;
832 goto unlock;
833 }
834 dma = vfio_find_dma(iommu, unmap->iova + unmap->size - 1, 0);
835 if (dma && dma->iova + dma->size != unmap->iova + unmap->size) {
836 ret = -EINVAL;
837 goto unlock;
838 }
839 }
840
166fd7d9 841 while ((dma = vfio_find_dma(iommu, unmap->iova, unmap->size))) {
1ef3e2bc 842 if (!iommu->v2 && unmap->iova > dma->iova)
166fd7d9 843 break;
8f0d5bb9
KW
844 /*
845 * Task with same address space who mapped this iova range is
846 * allowed to unmap the iova range.
847 */
848 if (dma->task->mm != current->mm)
849 break;
c086de81
KW
850
851 if (!RB_EMPTY_ROOT(&dma->pfn_list)) {
852 struct vfio_iommu_type1_dma_unmap nb_unmap;
853
854 if (dma_last == dma) {
855 BUG_ON(++retries > 10);
856 } else {
857 dma_last = dma;
858 retries = 0;
859 }
860
861 nb_unmap.iova = dma->iova;
862 nb_unmap.size = dma->size;
863
864 /*
865 * Notify anyone (mdev vendor drivers) to invalidate and
866 * unmap iovas within the range we're about to unmap.
867 * Vendor drivers MUST unpin pages in response to an
868 * invalidation.
869 */
870 mutex_unlock(&iommu->lock);
871 blocking_notifier_call_chain(&iommu->notifier,
872 VFIO_IOMMU_NOTIFY_DMA_UNMAP,
873 &nb_unmap);
874 goto again;
875 }
1ef3e2bc
AW
876 unmapped += dma->size;
877 vfio_remove_dma(iommu, dma);
166fd7d9 878 }
cd9b2268 879
1ef3e2bc 880unlock:
73fa0d10 881 mutex_unlock(&iommu->lock);
166fd7d9 882
1ef3e2bc 883 /* Report how much was unmapped */
166fd7d9
AW
884 unmap->size = unmapped;
885
886 return ret;
887}
888
889/*
890 * Turns out AMD IOMMU has a page table bug where it won't map large pages
891 * to a region that previously mapped smaller pages. This should be fixed
892 * soon, so this is just a temporary workaround to break mappings down into
893 * PAGE_SIZE. Better to map smaller pages than nothing.
894 */
1ef3e2bc 895static int map_try_harder(struct vfio_domain *domain, dma_addr_t iova,
166fd7d9
AW
896 unsigned long pfn, long npage, int prot)
897{
898 long i;
089f1c6b 899 int ret = 0;
166fd7d9
AW
900
901 for (i = 0; i < npage; i++, pfn++, iova += PAGE_SIZE) {
1ef3e2bc 902 ret = iommu_map(domain->domain, iova,
166fd7d9 903 (phys_addr_t)pfn << PAGE_SHIFT,
1ef3e2bc 904 PAGE_SIZE, prot | domain->prot);
166fd7d9
AW
905 if (ret)
906 break;
907 }
908
909 for (; i < npage && i > 0; i--, iova -= PAGE_SIZE)
1ef3e2bc
AW
910 iommu_unmap(domain->domain, iova, PAGE_SIZE);
911
912 return ret;
913}
914
915static int vfio_iommu_map(struct vfio_iommu *iommu, dma_addr_t iova,
916 unsigned long pfn, long npage, int prot)
917{
918 struct vfio_domain *d;
919 int ret;
920
921 list_for_each_entry(d, &iommu->domain_list, next) {
922 ret = iommu_map(d->domain, iova, (phys_addr_t)pfn << PAGE_SHIFT,
923 npage << PAGE_SHIFT, prot | d->prot);
924 if (ret) {
925 if (ret != -EBUSY ||
926 map_try_harder(d, iova, pfn, npage, prot))
927 goto unwind;
928 }
c5e66887
AW
929
930 cond_resched();
1ef3e2bc
AW
931 }
932
933 return 0;
934
935unwind:
936 list_for_each_entry_continue_reverse(d, &iommu->domain_list, next)
937 iommu_unmap(d->domain, iova, npage << PAGE_SHIFT);
166fd7d9 938
cd9b2268 939 return ret;
73fa0d10
AW
940}
941
8f0d5bb9
KW
942static int vfio_pin_map_dma(struct vfio_iommu *iommu, struct vfio_dma *dma,
943 size_t map_size)
944{
945 dma_addr_t iova = dma->iova;
946 unsigned long vaddr = dma->vaddr;
947 size_t size = map_size;
948 long npage;
949 unsigned long pfn;
950 int ret = 0;
951
952 while (size) {
953 /* Pin a contiguous chunk of memory */
954 npage = vfio_pin_pages_remote(dma, vaddr + dma->size,
a54eb550 955 size >> PAGE_SHIFT, &pfn);
8f0d5bb9
KW
956 if (npage <= 0) {
957 WARN_ON(!npage);
958 ret = (int)npage;
959 break;
960 }
961
962 /* Map it! */
963 ret = vfio_iommu_map(iommu, iova + dma->size, pfn, npage,
964 dma->prot);
965 if (ret) {
a54eb550
KW
966 vfio_unpin_pages_remote(dma, iova + dma->size, pfn,
967 npage, true);
8f0d5bb9
KW
968 break;
969 }
970
971 size -= npage << PAGE_SHIFT;
972 dma->size += npage << PAGE_SHIFT;
973 }
974
a54eb550
KW
975 dma->iommu_mapped = true;
976
8f0d5bb9
KW
977 if (ret)
978 vfio_remove_dma(iommu, dma);
979
980 return ret;
981}
982
73fa0d10
AW
983static int vfio_dma_do_map(struct vfio_iommu *iommu,
984 struct vfio_iommu_type1_dma_map *map)
985{
c8dbca16 986 dma_addr_t iova = map->iova;
166fd7d9 987 unsigned long vaddr = map->vaddr;
73fa0d10
AW
988 size_t size = map->size;
989 int ret = 0, prot = 0;
990 uint64_t mask;
1ef3e2bc 991 struct vfio_dma *dma;
166fd7d9 992
c8dbca16
AW
993 /* Verify that none of our __u64 fields overflow */
994 if (map->size != size || map->vaddr != vaddr || map->iova != iova)
995 return -EINVAL;
73fa0d10 996
1ef3e2bc 997 mask = ((uint64_t)1 << __ffs(vfio_pgsize_bitmap(iommu))) - 1;
73fa0d10 998
c8dbca16
AW
999 WARN_ON(mask & PAGE_MASK);
1000
73fa0d10
AW
1001 /* READ/WRITE from device perspective */
1002 if (map->flags & VFIO_DMA_MAP_FLAG_WRITE)
1003 prot |= IOMMU_WRITE;
1004 if (map->flags & VFIO_DMA_MAP_FLAG_READ)
1005 prot |= IOMMU_READ;
1006
c8dbca16 1007 if (!prot || !size || (size | iova | vaddr) & mask)
73fa0d10
AW
1008 return -EINVAL;
1009
c8dbca16
AW
1010 /* Don't allow IOVA or virtual address wrap */
1011 if (iova + size - 1 < iova || vaddr + size - 1 < vaddr)
73fa0d10
AW
1012 return -EINVAL;
1013
1014 mutex_lock(&iommu->lock);
1015
c8dbca16 1016 if (vfio_find_dma(iommu, iova, size)) {
8f0d5bb9
KW
1017 ret = -EEXIST;
1018 goto out_unlock;
73fa0d10
AW
1019 }
1020
1ef3e2bc
AW
1021 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
1022 if (!dma) {
8f0d5bb9
KW
1023 ret = -ENOMEM;
1024 goto out_unlock;
1ef3e2bc
AW
1025 }
1026
c8dbca16
AW
1027 dma->iova = iova;
1028 dma->vaddr = vaddr;
1ef3e2bc 1029 dma->prot = prot;
8f0d5bb9
KW
1030 get_task_struct(current);
1031 dma->task = current;
a54eb550 1032 dma->pfn_list = RB_ROOT;
166fd7d9 1033
1ef3e2bc
AW
1034 /* Insert zero-sized and grow as we map chunks of it */
1035 vfio_link_dma(iommu, dma);
166fd7d9 1036
a54eb550
KW
1037 /* Don't pin and map if container doesn't contain IOMMU capable domain*/
1038 if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
1039 dma->size = size;
1040 else
1041 ret = vfio_pin_map_dma(iommu, dma, size);
1042
8f0d5bb9 1043out_unlock:
1ef3e2bc
AW
1044 mutex_unlock(&iommu->lock);
1045 return ret;
1046}
1047
1048static int vfio_bus_type(struct device *dev, void *data)
1049{
1050 struct bus_type **bus = data;
1051
1052 if (*bus && *bus != dev->bus)
1053 return -EINVAL;
1054
1055 *bus = dev->bus;
1056
1057 return 0;
1058}
1059
1060static int vfio_iommu_replay(struct vfio_iommu *iommu,
1061 struct vfio_domain *domain)
1062{
1063 struct vfio_domain *d;
1064 struct rb_node *n;
1065 int ret;
1066
1067 /* Arbitrarily pick the first domain in the list for lookups */
1068 d = list_first_entry(&iommu->domain_list, struct vfio_domain, next);
1069 n = rb_first(&iommu->dma_list);
1070
1ef3e2bc
AW
1071 for (; n; n = rb_next(n)) {
1072 struct vfio_dma *dma;
1073 dma_addr_t iova;
1074
1075 dma = rb_entry(n, struct vfio_dma, node);
1076 iova = dma->iova;
1077
1078 while (iova < dma->iova + dma->size) {
a54eb550 1079 phys_addr_t phys;
1ef3e2bc 1080 size_t size;
73fa0d10 1081
a54eb550
KW
1082 if (dma->iommu_mapped) {
1083 phys_addr_t p;
1084 dma_addr_t i;
1085
1086 phys = iommu_iova_to_phys(d->domain, iova);
1087
1088 if (WARN_ON(!phys)) {
1089 iova += PAGE_SIZE;
1090 continue;
1091 }
1092
1093 size = PAGE_SIZE;
1094 p = phys + size;
1095 i = iova + size;
1096 while (i < dma->iova + dma->size &&
1097 p == iommu_iova_to_phys(d->domain, i)) {
1098 size += PAGE_SIZE;
1099 p += PAGE_SIZE;
1100 i += PAGE_SIZE;
1101 }
1102 } else {
1103 unsigned long pfn;
1104 unsigned long vaddr = dma->vaddr +
1105 (iova - dma->iova);
1106 size_t n = dma->iova + dma->size - iova;
1107 long npage;
1108
1109 npage = vfio_pin_pages_remote(dma, vaddr,
1110 n >> PAGE_SHIFT,
1111 &pfn);
1112 if (npage <= 0) {
1113 WARN_ON(!npage);
1114 ret = (int)npage;
1115 return ret;
1116 }
1117
1118 phys = pfn << PAGE_SHIFT;
1119 size = npage << PAGE_SHIFT;
166fd7d9
AW
1120 }
1121
1ef3e2bc
AW
1122 ret = iommu_map(domain->domain, iova, phys,
1123 size, dma->prot | domain->prot);
1124 if (ret)
1125 return ret;
d93b3ac0 1126
1ef3e2bc
AW
1127 iova += size;
1128 }
a54eb550 1129 dma->iommu_mapped = true;
166fd7d9 1130 }
1ef3e2bc 1131 return 0;
73fa0d10
AW
1132}
1133
6fe1010d
AW
1134/*
1135 * We change our unmap behavior slightly depending on whether the IOMMU
1136 * supports fine-grained superpages. IOMMUs like AMD-Vi will use a superpage
1137 * for practically any contiguous power-of-two mapping we give it. This means
1138 * we don't need to look for contiguous chunks ourselves to make unmapping
1139 * more efficient. On IOMMUs with coarse-grained super pages, like Intel VT-d
1140 * with discrete 2M/1G/512G/1T superpages, identifying contiguous chunks
1141 * significantly boosts non-hugetlbfs mappings and doesn't seem to hurt when
1142 * hugetlbfs is in use.
1143 */
1144static void vfio_test_domain_fgsp(struct vfio_domain *domain)
1145{
1146 struct page *pages;
1147 int ret, order = get_order(PAGE_SIZE * 2);
1148
1149 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
1150 if (!pages)
1151 return;
1152
1153 ret = iommu_map(domain->domain, 0, page_to_phys(pages), PAGE_SIZE * 2,
1154 IOMMU_READ | IOMMU_WRITE | domain->prot);
1155 if (!ret) {
1156 size_t unmapped = iommu_unmap(domain->domain, 0, PAGE_SIZE);
1157
1158 if (unmapped == PAGE_SIZE)
1159 iommu_unmap(domain->domain, PAGE_SIZE, PAGE_SIZE);
1160 else
1161 domain->fgsp = true;
1162 }
1163
1164 __free_pages(pages, order);
1165}
1166
7896c998
KW
1167static struct vfio_group *find_iommu_group(struct vfio_domain *domain,
1168 struct iommu_group *iommu_group)
1169{
1170 struct vfio_group *g;
1171
1172 list_for_each_entry(g, &domain->group_list, next) {
1173 if (g->iommu_group == iommu_group)
1174 return g;
1175 }
1176
1177 return NULL;
1178}
1179
73fa0d10
AW
1180static int vfio_iommu_type1_attach_group(void *iommu_data,
1181 struct iommu_group *iommu_group)
1182{
1183 struct vfio_iommu *iommu = iommu_data;
7896c998 1184 struct vfio_group *group;
1ef3e2bc 1185 struct vfio_domain *domain, *d;
a54eb550 1186 struct bus_type *bus = NULL, *mdev_bus;
73fa0d10
AW
1187 int ret;
1188
73fa0d10
AW
1189 mutex_lock(&iommu->lock);
1190
1ef3e2bc 1191 list_for_each_entry(d, &iommu->domain_list, next) {
7896c998 1192 if (find_iommu_group(d, iommu_group)) {
73fa0d10 1193 mutex_unlock(&iommu->lock);
73fa0d10
AW
1194 return -EINVAL;
1195 }
1196 }
1197
a54eb550
KW
1198 if (iommu->external_domain) {
1199 if (find_iommu_group(iommu->external_domain, iommu_group)) {
1200 mutex_unlock(&iommu->lock);
1201 return -EINVAL;
1202 }
1203 }
1204
1ef3e2bc
AW
1205 group = kzalloc(sizeof(*group), GFP_KERNEL);
1206 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
1207 if (!group || !domain) {
1208 ret = -ENOMEM;
1209 goto out_free;
1210 }
1211
1212 group->iommu_group = iommu_group;
1213
1214 /* Determine bus_type in order to allocate a domain */
1215 ret = iommu_group_for_each_dev(iommu_group, &bus, vfio_bus_type);
1216 if (ret)
1217 goto out_free;
1218
a54eb550
KW
1219 mdev_bus = symbol_get(mdev_bus_type);
1220
1221 if (mdev_bus) {
1222 if ((bus == mdev_bus) && !iommu_present(bus)) {
1223 symbol_put(mdev_bus_type);
1224 if (!iommu->external_domain) {
1225 INIT_LIST_HEAD(&domain->group_list);
1226 iommu->external_domain = domain;
1227 } else
1228 kfree(domain);
1229
1230 list_add(&group->next,
1231 &iommu->external_domain->group_list);
1232 mutex_unlock(&iommu->lock);
1233 return 0;
1234 }
1235 symbol_put(mdev_bus_type);
1236 }
1237
1ef3e2bc
AW
1238 domain->domain = iommu_domain_alloc(bus);
1239 if (!domain->domain) {
1240 ret = -EIO;
1241 goto out_free;
1242 }
1243
f5c9eceb
WD
1244 if (iommu->nesting) {
1245 int attr = 1;
1246
1247 ret = iommu_domain_set_attr(domain->domain, DOMAIN_ATTR_NESTING,
1248 &attr);
1249 if (ret)
1250 goto out_domain;
1251 }
1252
1ef3e2bc
AW
1253 ret = iommu_attach_group(domain->domain, iommu_group);
1254 if (ret)
1255 goto out_domain;
1256
1257 INIT_LIST_HEAD(&domain->group_list);
1258 list_add(&group->next, &domain->group_list);
1259
1260 if (!allow_unsafe_interrupts &&
eb165f05 1261 !iommu_capable(bus, IOMMU_CAP_INTR_REMAP)) {
1ef3e2bc
AW
1262 pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",
1263 __func__);
1264 ret = -EPERM;
1265 goto out_detach;
1266 }
1267
eb165f05 1268 if (iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
1ef3e2bc
AW
1269 domain->prot |= IOMMU_CACHE;
1270
73fa0d10 1271 /*
1ef3e2bc
AW
1272 * Try to match an existing compatible domain. We don't want to
1273 * preclude an IOMMU driver supporting multiple bus_types and being
1274 * able to include different bus_types in the same IOMMU domain, so
1275 * we test whether the domains use the same iommu_ops rather than
1276 * testing if they're on the same bus_type.
73fa0d10 1277 */
1ef3e2bc
AW
1278 list_for_each_entry(d, &iommu->domain_list, next) {
1279 if (d->domain->ops == domain->domain->ops &&
1280 d->prot == domain->prot) {
1281 iommu_detach_group(domain->domain, iommu_group);
1282 if (!iommu_attach_group(d->domain, iommu_group)) {
1283 list_add(&group->next, &d->group_list);
1284 iommu_domain_free(domain->domain);
1285 kfree(domain);
1286 mutex_unlock(&iommu->lock);
1287 return 0;
1288 }
1289
1290 ret = iommu_attach_group(domain->domain, iommu_group);
1291 if (ret)
1292 goto out_domain;
1293 }
73fa0d10
AW
1294 }
1295
6fe1010d
AW
1296 vfio_test_domain_fgsp(domain);
1297
1ef3e2bc
AW
1298 /* replay mappings on new domains */
1299 ret = vfio_iommu_replay(iommu, domain);
1300 if (ret)
1301 goto out_detach;
1302
1303 list_add(&domain->next, &iommu->domain_list);
73fa0d10
AW
1304
1305 mutex_unlock(&iommu->lock);
1306
1307 return 0;
1ef3e2bc
AW
1308
1309out_detach:
1310 iommu_detach_group(domain->domain, iommu_group);
1311out_domain:
1312 iommu_domain_free(domain->domain);
1313out_free:
1314 kfree(domain);
1315 kfree(group);
1316 mutex_unlock(&iommu->lock);
1317 return ret;
1318}
1319
1320static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu)
1321{
1322 struct rb_node *node;
1323
1324 while ((node = rb_first(&iommu->dma_list)))
1325 vfio_remove_dma(iommu, rb_entry(node, struct vfio_dma, node));
73fa0d10
AW
1326}
1327
a54eb550
KW
1328static void vfio_iommu_unmap_unpin_reaccount(struct vfio_iommu *iommu)
1329{
1330 struct rb_node *n, *p;
1331
1332 n = rb_first(&iommu->dma_list);
1333 for (; n; n = rb_next(n)) {
1334 struct vfio_dma *dma;
1335 long locked = 0, unlocked = 0;
1336
1337 dma = rb_entry(n, struct vfio_dma, node);
1338 unlocked += vfio_unmap_unpin(iommu, dma, false);
1339 p = rb_first(&dma->pfn_list);
1340 for (; p; p = rb_next(p)) {
1341 struct vfio_pfn *vpfn = rb_entry(p, struct vfio_pfn,
1342 node);
1343
1344 if (!is_invalid_reserved_pfn(vpfn->pfn))
1345 locked++;
1346 }
1347 vfio_lock_acct(dma->task, locked - unlocked);
1348 }
1349}
1350
1351static void vfio_sanity_check_pfn_list(struct vfio_iommu *iommu)
1352{
1353 struct rb_node *n;
1354
1355 n = rb_first(&iommu->dma_list);
1356 for (; n; n = rb_next(n)) {
1357 struct vfio_dma *dma;
1358
1359 dma = rb_entry(n, struct vfio_dma, node);
1360
1361 if (WARN_ON(!RB_EMPTY_ROOT(&dma->pfn_list)))
1362 break;
1363 }
1364}
1365
73fa0d10
AW
1366static void vfio_iommu_type1_detach_group(void *iommu_data,
1367 struct iommu_group *iommu_group)
1368{
1369 struct vfio_iommu *iommu = iommu_data;
1ef3e2bc 1370 struct vfio_domain *domain;
73fa0d10
AW
1371 struct vfio_group *group;
1372
1373 mutex_lock(&iommu->lock);
1374
a54eb550
KW
1375 if (iommu->external_domain) {
1376 group = find_iommu_group(iommu->external_domain, iommu_group);
1377 if (group) {
1378 list_del(&group->next);
1379 kfree(group);
1380
1381 if (list_empty(&iommu->external_domain->group_list)) {
1382 vfio_sanity_check_pfn_list(iommu);
1383
1384 if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
1385 vfio_iommu_unmap_unpin_all(iommu);
1386
1387 kfree(iommu->external_domain);
1388 iommu->external_domain = NULL;
1389 }
1390 goto detach_group_done;
1391 }
1392 }
1393
1ef3e2bc 1394 list_for_each_entry(domain, &iommu->domain_list, next) {
7896c998
KW
1395 group = find_iommu_group(domain, iommu_group);
1396 if (!group)
1397 continue;
1ef3e2bc 1398
7896c998
KW
1399 iommu_detach_group(domain->domain, iommu_group);
1400 list_del(&group->next);
1401 kfree(group);
1402 /*
a54eb550
KW
1403 * Group ownership provides privilege, if the group list is
1404 * empty, the domain goes away. If it's the last domain with
1405 * iommu and external domain doesn't exist, then all the
1406 * mappings go away too. If it's the last domain with iommu and
1407 * external domain exist, update accounting
7896c998
KW
1408 */
1409 if (list_empty(&domain->group_list)) {
a54eb550
KW
1410 if (list_is_singular(&iommu->domain_list)) {
1411 if (!iommu->external_domain)
1412 vfio_iommu_unmap_unpin_all(iommu);
1413 else
1414 vfio_iommu_unmap_unpin_reaccount(iommu);
1415 }
7896c998
KW
1416 iommu_domain_free(domain->domain);
1417 list_del(&domain->next);
1418 kfree(domain);
73fa0d10 1419 }
a54eb550 1420 break;
73fa0d10
AW
1421 }
1422
a54eb550 1423detach_group_done:
73fa0d10
AW
1424 mutex_unlock(&iommu->lock);
1425}
1426
1427static void *vfio_iommu_type1_open(unsigned long arg)
1428{
1429 struct vfio_iommu *iommu;
1430
73fa0d10
AW
1431 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
1432 if (!iommu)
1433 return ERR_PTR(-ENOMEM);
1434
f5c9eceb
WD
1435 switch (arg) {
1436 case VFIO_TYPE1_IOMMU:
1437 break;
1438 case VFIO_TYPE1_NESTING_IOMMU:
1439 iommu->nesting = true;
1440 case VFIO_TYPE1v2_IOMMU:
1441 iommu->v2 = true;
1442 break;
1443 default:
1444 kfree(iommu);
1445 return ERR_PTR(-EINVAL);
1446 }
1447
1ef3e2bc 1448 INIT_LIST_HEAD(&iommu->domain_list);
cd9b2268 1449 iommu->dma_list = RB_ROOT;
73fa0d10 1450 mutex_init(&iommu->lock);
c086de81 1451 BLOCKING_INIT_NOTIFIER_HEAD(&iommu->notifier);
73fa0d10
AW
1452
1453 return iommu;
1454}
1455
a54eb550
KW
1456static void vfio_release_domain(struct vfio_domain *domain, bool external)
1457{
1458 struct vfio_group *group, *group_tmp;
1459
1460 list_for_each_entry_safe(group, group_tmp,
1461 &domain->group_list, next) {
1462 if (!external)
1463 iommu_detach_group(domain->domain, group->iommu_group);
1464 list_del(&group->next);
1465 kfree(group);
1466 }
1467
1468 if (!external)
1469 iommu_domain_free(domain->domain);
1470}
1471
73fa0d10
AW
1472static void vfio_iommu_type1_release(void *iommu_data)
1473{
1474 struct vfio_iommu *iommu = iommu_data;
1ef3e2bc 1475 struct vfio_domain *domain, *domain_tmp;
a54eb550
KW
1476
1477 if (iommu->external_domain) {
1478 vfio_release_domain(iommu->external_domain, true);
1479 vfio_sanity_check_pfn_list(iommu);
1480 kfree(iommu->external_domain);
1481 }
73fa0d10 1482
1ef3e2bc 1483 vfio_iommu_unmap_unpin_all(iommu);
73fa0d10 1484
1ef3e2bc
AW
1485 list_for_each_entry_safe(domain, domain_tmp,
1486 &iommu->domain_list, next) {
a54eb550 1487 vfio_release_domain(domain, false);
1ef3e2bc
AW
1488 list_del(&domain->next);
1489 kfree(domain);
73fa0d10 1490 }
73fa0d10
AW
1491 kfree(iommu);
1492}
1493
aa429318
AW
1494static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu)
1495{
1496 struct vfio_domain *domain;
1497 int ret = 1;
1498
1499 mutex_lock(&iommu->lock);
1500 list_for_each_entry(domain, &iommu->domain_list, next) {
1501 if (!(domain->prot & IOMMU_CACHE)) {
1502 ret = 0;
f5bfdbf2 1503 break;
aa429318 1504 }
73fa0d10 1505 }
aa429318 1506 mutex_unlock(&iommu->lock);
73fa0d10 1507
aa429318 1508 return ret;
73fa0d10
AW
1509}
1510
1511static long vfio_iommu_type1_ioctl(void *iommu_data,
1512 unsigned int cmd, unsigned long arg)
1513{
1514 struct vfio_iommu *iommu = iommu_data;
1515 unsigned long minsz;
1516
1517 if (cmd == VFIO_CHECK_EXTENSION) {
1518 switch (arg) {
1519 case VFIO_TYPE1_IOMMU:
1ef3e2bc 1520 case VFIO_TYPE1v2_IOMMU:
f5c9eceb 1521 case VFIO_TYPE1_NESTING_IOMMU:
73fa0d10 1522 return 1;
aa429318
AW
1523 case VFIO_DMA_CC_IOMMU:
1524 if (!iommu)
1525 return 0;
1526 return vfio_domains_have_iommu_cache(iommu);
73fa0d10
AW
1527 default:
1528 return 0;
1529 }
1530 } else if (cmd == VFIO_IOMMU_GET_INFO) {
1531 struct vfio_iommu_type1_info info;
1532
1533 minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes);
1534
1535 if (copy_from_user(&info, (void __user *)arg, minsz))
1536 return -EFAULT;
1537
1538 if (info.argsz < minsz)
1539 return -EINVAL;
1540
d4f50ee2 1541 info.flags = VFIO_IOMMU_INFO_PGSIZES;
73fa0d10 1542
1ef3e2bc 1543 info.iova_pgsizes = vfio_pgsize_bitmap(iommu);
73fa0d10 1544
8160c4e4
MT
1545 return copy_to_user((void __user *)arg, &info, minsz) ?
1546 -EFAULT : 0;
73fa0d10
AW
1547
1548 } else if (cmd == VFIO_IOMMU_MAP_DMA) {
1549 struct vfio_iommu_type1_dma_map map;
1550 uint32_t mask = VFIO_DMA_MAP_FLAG_READ |
1551 VFIO_DMA_MAP_FLAG_WRITE;
1552
1553 minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
1554
1555 if (copy_from_user(&map, (void __user *)arg, minsz))
1556 return -EFAULT;
1557
1558 if (map.argsz < minsz || map.flags & ~mask)
1559 return -EINVAL;
1560
1561 return vfio_dma_do_map(iommu, &map);
1562
1563 } else if (cmd == VFIO_IOMMU_UNMAP_DMA) {
1564 struct vfio_iommu_type1_dma_unmap unmap;
166fd7d9 1565 long ret;
73fa0d10
AW
1566
1567 minsz = offsetofend(struct vfio_iommu_type1_dma_unmap, size);
1568
1569 if (copy_from_user(&unmap, (void __user *)arg, minsz))
1570 return -EFAULT;
1571
1572 if (unmap.argsz < minsz || unmap.flags)
1573 return -EINVAL;
1574
166fd7d9
AW
1575 ret = vfio_dma_do_unmap(iommu, &unmap);
1576 if (ret)
1577 return ret;
1578
8160c4e4
MT
1579 return copy_to_user((void __user *)arg, &unmap, minsz) ?
1580 -EFAULT : 0;
73fa0d10
AW
1581 }
1582
1583 return -ENOTTY;
1584}
1585
c086de81
KW
1586static int vfio_iommu_type1_register_notifier(void *iommu_data,
1587 struct notifier_block *nb)
1588{
1589 struct vfio_iommu *iommu = iommu_data;
1590
1591 return blocking_notifier_chain_register(&iommu->notifier, nb);
1592}
1593
1594static int vfio_iommu_type1_unregister_notifier(void *iommu_data,
1595 struct notifier_block *nb)
1596{
1597 struct vfio_iommu *iommu = iommu_data;
1598
1599 return blocking_notifier_chain_unregister(&iommu->notifier, nb);
1600}
1601
73fa0d10 1602static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
c086de81
KW
1603 .name = "vfio-iommu-type1",
1604 .owner = THIS_MODULE,
1605 .open = vfio_iommu_type1_open,
1606 .release = vfio_iommu_type1_release,
1607 .ioctl = vfio_iommu_type1_ioctl,
1608 .attach_group = vfio_iommu_type1_attach_group,
1609 .detach_group = vfio_iommu_type1_detach_group,
1610 .pin_pages = vfio_iommu_type1_pin_pages,
1611 .unpin_pages = vfio_iommu_type1_unpin_pages,
1612 .register_notifier = vfio_iommu_type1_register_notifier,
1613 .unregister_notifier = vfio_iommu_type1_unregister_notifier,
73fa0d10
AW
1614};
1615
1616static int __init vfio_iommu_type1_init(void)
1617{
73fa0d10
AW
1618 return vfio_register_iommu_driver(&vfio_iommu_driver_ops_type1);
1619}
1620
1621static void __exit vfio_iommu_type1_cleanup(void)
1622{
1623 vfio_unregister_iommu_driver(&vfio_iommu_driver_ops_type1);
1624}
1625
1626module_init(vfio_iommu_type1_init);
1627module_exit(vfio_iommu_type1_cleanup);
1628
1629MODULE_VERSION(DRIVER_VERSION);
1630MODULE_LICENSE("GPL v2");
1631MODULE_AUTHOR(DRIVER_AUTHOR);
1632MODULE_DESCRIPTION(DRIVER_DESC);