Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Steve Sistare <steven.sistare@oracle.com>
To: kvm@vger.kernel.org
Cc: Alex Williamson <alex.williamson@redhat.com>,
	Cornelia Huck <cohuck@redhat.com>,
	Jason Gunthorpe <jgg@nvidia.com>,
	Kevin Tian <kevin.tian@intel.com>,
	Steve Sistare <steven.sistare@oracle.com>
Subject: [PATCH V6 4/7] vfio/type1: restore locked_vm
Date: Fri, 16 Dec 2022 10:50:37 -0800	[thread overview]
Message-ID: <1671216640-157935-5-git-send-email-steven.sistare@oracle.com> (raw)
In-Reply-To: <1671216640-157935-1-git-send-email-steven.sistare@oracle.com>

When a vfio container is preserved across exec or fork-exec, the new
task's mm has a locked_vm count of 0.  After a dma vaddr is updated using
VFIO_DMA_MAP_FLAG_VADDR, locked_vm remains 0, and the pinned memory does
not count against the task's RLIMIT_MEMLOCK.

To restore the correct locked_vm count, when VFIO_DMA_MAP_FLAG_VADDR is
used and the dma's mm has changed, add the dma's locked_vm count to
the new mm->locked_vm, subject to the rlimit.

Fixes: c3cbab24db38 ("vfio/type1: implement interfaces to update vaddr")
Cc: stable@vger.kernel.org
Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
 drivers/vfio/vfio_iommu_type1.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 889e920..4e79bce 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -1590,6 +1590,35 @@ static bool vfio_iommu_iova_dma_valid(struct vfio_iommu *iommu,
 	return list_empty(iova);
 }
 
+static int vfio_change_dma_owner(struct vfio_dma *dma)
+{
+	int ret = 0;
+	struct task_struct *task = current->group_leader;
+	struct mm_struct *mm = current->mm;
+
+	if (task->mm != dma->mm) {
+		long npage = dma->locked_vm;
+		bool lock_cap = capable(CAP_IPC_LOCK);
+
+		ret = task_lock_acct(task, mm, lock_cap, npage, false);
+		if (ret)
+			return ret;
+
+		task_lock_acct(dma->task, dma->mm, dma->lock_cap, -npage, true);
+
+		if (dma->task != task) {
+			put_task_struct(dma->task);
+			dma->task = get_task_struct(task);
+		}
+		mmdrop(dma->mm);
+		dma->mm = mm;
+		mmgrab(dma->mm);
+		dma->lock_cap = lock_cap;
+	}
+
+	return ret;
+}
+
 static int vfio_dma_do_map(struct vfio_iommu *iommu,
 			   struct vfio_iommu_type1_dma_map *map)
 {
@@ -1639,6 +1668,9 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu,
 			   dma->size != size) {
 			ret = -EINVAL;
 		} else {
+			ret = vfio_change_dma_owner(dma);
+			if (ret)
+				goto out_unlock;
 			dma->vaddr = vaddr;
 			dma->vaddr_invalid = false;
 			iommu->vaddr_invalid_count--;
-- 
1.8.3.1


  parent reply	other threads:[~2022-12-16 18:51 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-16 18:50 [PATCH V6 0/7] fixes for virtual address update Steve Sistare
2022-12-16 18:50 ` [PATCH V6 1/7] vfio/type1: exclude mdevs from VFIO_UPDATE_VADDR Steve Sistare
2022-12-16 18:50 ` [PATCH V6 2/7] vfio/type1: prevent underflow of locked_vm via exec() Steve Sistare
2022-12-19  7:48   ` Tian, Kevin
2022-12-20 15:01     ` Steven Sistare
2022-12-20 21:59       ` Alex Williamson
2022-12-20 22:06         ` Steven Sistare
2022-12-16 18:50 ` [PATCH V6 3/7] vfio/type1: track locked_vm per dma Steve Sistare
2022-12-19  7:51   ` Tian, Kevin
2022-12-16 18:50 ` Steve Sistare [this message]
2022-12-19  7:54   ` [PATCH V6 4/7] vfio/type1: restore locked_vm Tian, Kevin
2022-12-16 18:50 ` [PATCH V6 5/7] vfio/type1: revert "block on invalid vaddr" Steve Sistare
2022-12-19  7:54   ` Tian, Kevin
2022-12-16 18:50 ` [PATCH V6 6/7] vfio/type1: revert "implement notify callback" Steve Sistare
2022-12-19  7:55   ` Tian, Kevin
2022-12-16 18:50 ` [PATCH V6 7/7] vfio: revert "iommu driver " Steve Sistare
2022-12-19  7:55   ` Tian, Kevin
2022-12-19 18:42 ` [PATCH V6 0/7] fixes for virtual address update Steven Sistare
2022-12-19 20:55   ` Alex Williamson

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=1671216640-157935-5-git-send-email-steven.sistare@oracle.com \
    --to=steven.sistare@oracle.com \
    --cc=alex.williamson@redhat.com \
    --cc=cohuck@redhat.com \
    --cc=jgg@nvidia.com \
    --cc=kevin.tian@intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox