From: Steve Sistare <steven.sistare@oracle.com>
To: kvm@vger.kernel.org
Cc: Alex Williamson <alex.williamson@redhat.com>,
Cornelia Huck <cohuck@redhat.com>,
Steve Sistare <steven.sistare@oracle.com>
Subject: [PATCH V2 2/5] vfio/type1: prevent locked_vm underflow
Date: Tue, 13 Dec 2022 11:40:56 -0800 [thread overview]
Message-ID: <1670960459-415264-3-git-send-email-steven.sistare@oracle.com> (raw)
In-Reply-To: <1670960459-415264-1-git-send-email-steven.sistare@oracle.com>
When a vfio container is preserved across exec using the VFIO_UPDATE_VADDR
interfaces, locked_vm of the new mm becomes 0. If the user later unmaps a
dma mapping, locked_vm underflows to a large unsigned value, and a
subsequent dma map request fails with ENOMEM in __account_locked_vm.
To avoid underflow, do not decrement locked_vm during unmap if the
dma's mm has changed. To restore the correct locked_vm count, when
VFIO_DMA_MAP_FLAG_VADDR is used and the dma's mm has changed, add
the mapping's pinned page count to the new mm->locked_vm, subject
to the rlimit. Now that mediated devices are excluded when using
VFIO_UPDATE_VADDR, the amount of pinned memory equals the size of
the mapping.
Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
drivers/vfio/vfio_iommu_type1.c | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 80bdb4d..35a1a52 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -100,6 +100,7 @@ struct vfio_dma {
struct task_struct *task;
struct rb_root pfn_list; /* Ex-user pinned pfn list */
unsigned long *bitmap;
+ struct mm_struct *mm;
};
struct vfio_batch {
@@ -1165,7 +1166,7 @@ static long vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma,
&iotlb_gather);
}
- if (do_accounting) {
+ if (do_accounting && current->mm == dma->mm) {
vfio_lock_acct(dma, -unlocked, true);
return 0;
}
@@ -1178,6 +1179,7 @@ static void vfio_remove_dma(struct vfio_iommu *iommu, struct vfio_dma *dma)
vfio_unmap_unpin(iommu, dma, true);
vfio_unlink_dma(iommu, dma);
put_task_struct(dma->task);
+ mmdrop(dma->mm);
vfio_dma_bitmap_free(dma);
if (dma->vaddr_invalid) {
iommu->vaddr_invalid_count--;
@@ -1623,9 +1625,20 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
dma->size != size) {
ret = -EINVAL;
} else {
- dma->vaddr = vaddr;
- dma->vaddr_invalid = false;
- iommu->vaddr_invalid_count--;
+ if (current->mm != dma->mm) {
+ ret = vfio_lock_acct(dma, size >> PAGE_SHIFT,
+ 0);
+ if (!ret) {
+ mmdrop(dma->mm);
+ dma->mm = current->mm;
+ mmgrab(dma->mm);
+ }
+ }
+ if (!ret) {
+ dma->vaddr = vaddr;
+ dma->vaddr_invalid = false;
+ iommu->vaddr_invalid_count--;
+ }
wake_up_all(&iommu->vaddr_wait);
}
goto out_unlock;
@@ -1683,6 +1696,8 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
get_task_struct(current->group_leader);
dma->task = current->group_leader;
dma->lock_cap = capable(CAP_IPC_LOCK);
+ dma->mm = dma->task->mm;
+ mmgrab(dma->mm);
dma->pfn_list = RB_ROOT;
--
1.8.3.1
next prev parent reply other threads:[~2022-12-13 19:41 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-13 19:40 [PATCH V2 0/5] fixes for virtual address update Steve Sistare
2022-12-13 19:40 ` [PATCH V2 1/5] vfio/type1: exclude mdevs from VFIO_UPDATE_VADDR Steve Sistare
2022-12-13 20:22 ` Alex Williamson
2022-12-13 20:37 ` Steven Sistare
2022-12-13 20:59 ` Alex Williamson
2022-12-13 21:16 ` Steven Sistare
2022-12-13 21:26 ` Alex Williamson
2022-12-13 19:40 ` Steve Sistare [this message]
2022-12-13 20:23 ` [PATCH V2 2/5] vfio/type1: prevent locked_vm underflow Alex Williamson
2022-12-13 21:01 ` Steven Sistare
2022-12-13 21:31 ` Alex Williamson
2022-12-13 21:56 ` Steven Sistare
2022-12-13 19:40 ` [PATCH V2 3/5] vfio/type1: revert "block on invalid vaddr" Steve Sistare
2022-12-13 19:40 ` [PATCH V2 4/5] vfio/type1: revert "implement notify callback" Steve Sistare
2022-12-13 19:40 ` [PATCH V2 5/5] vfio: revert "iommu driver " Steve Sistare
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1670960459-415264-3-git-send-email-steven.sistare@oracle.com \
--to=steven.sistare@oracle.com \
--cc=alex.williamson@redhat.com \
--cc=cohuck@redhat.com \
--cc=kvm@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.