linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: ard.biesheuvel@linaro.org (Ard Biesheuvel)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 6/6] arm/arm64: KVM: map MMIO regions at creation time
Date: Wed, 17 Sep 2014 14:56:21 -0700	[thread overview]
Message-ID: <1410990981-665-7-git-send-email-ard.biesheuvel@linaro.org> (raw)
In-Reply-To: <1410990981-665-1-git-send-email-ard.biesheuvel@linaro.org>

There is really no point in faulting in memory regions page by page
if they are not backed by demand paged system RAM but by a linear
passthrough mapping of a host MMIO region.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 arch/arm/kvm/mmu.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c
index fe53c3a30383..b153ef0c6d9f 100644
--- a/arch/arm/kvm/mmu.c
+++ b/arch/arm/kvm/mmu.c
@@ -1162,7 +1162,55 @@ void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
 			    unsigned long npages)
 {
-	return 0;
+	hva_t hva = slot->userspace_addr;
+	phys_addr_t gpa = slot->base_gfn << PAGE_SHIFT;
+	phys_addr_t size = slot->npages << PAGE_SHIFT;
+	int ret = 0;
+
+	/*
+	 * A memslot could potentially cover multiple VMAs, so iterate
+	 * over all of them to find out if we can map any of them right now.
+	 *
+	 *     +--------------------------------------------+
+	 * +---+---------+-------------------+--------------+----+
+	 * |   : VMA 1   |       VMA 2       |       VMA 3  :    |
+	 * +---+---------+-------------------+--------------+----+
+	 *     |                   memslot                  |
+	 *     +--------------------------------------------+
+	 */
+	do {
+		struct vm_area_struct *vma = find_vma(current->mm, hva);
+		hva_t start, end;
+
+		if (!vma || vma->vm_start > hva) {
+			ret = -EFAULT;
+			break;
+		}
+
+		start = max(slot->userspace_addr, vma->vm_start);
+		end = min((hva_t)(slot->userspace_addr + size), vma->vm_end);
+
+		if (vma->vm_flags & VM_PFNMAP) {
+			phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) + start -
+					 vma->vm_start;
+			bool writable = vma->vm_flags & VM_WRITE &&
+					!(slot->flags & KVM_MEM_READONLY);
+
+			ret = kvm_phys_addr_ioremap(kvm, gpa, pa, end - start,
+						    writable);
+			if (ret)
+				break;
+		}
+		hva += end - start;
+		gpa += end - start;
+	} while (hva < slot->userspace_addr + size);
+
+	if (ret) {
+		spin_lock(&kvm->mmu_lock);
+		unmap_stage2_range(kvm, slot->base_gfn << PAGE_SHIFT, size);
+		spin_unlock(&kvm->mmu_lock);
+	}
+	return ret;
 }
 
 void kvm_arch_memslots_updated(struct kvm *kvm)
-- 
1.8.3.2

  parent reply	other threads:[~2014-09-17 21:56 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-17 21:56 [PATCH 0/6] KVM: handling of MMIO pass-through regions Ard Biesheuvel
2014-09-17 21:56 ` [PATCH 1/6] arm/arm64: KVM: use __GFP_ZERO not memset() to get zeroed pages Ard Biesheuvel
2014-09-29 13:02   ` Christoffer Dall
2014-10-09 12:59   ` Marc Zyngier
2014-09-17 21:56 ` [PATCH 2/6] arm/arm64: KVM: fix potential NULL dereference in user_mem_abort() Ard Biesheuvel
2014-09-29 13:01   ` Christoffer Dall
2014-10-09 13:05   ` Marc Zyngier
2014-10-09 13:10     ` Ard Biesheuvel
2014-10-09 13:11       ` Christoffer Dall
2014-10-09 13:13         ` Marc Zyngier
2014-09-17 21:56 ` [PATCH 3/6] arm/arm64: KVM: add 'writable' parameter to kvm_phys_addr_ioremap Ard Biesheuvel
2014-09-25  0:03   ` Mario Smarduch
2014-09-29 12:23     ` Christoffer Dall
2014-09-29 18:02       ` Mario Smarduch
2014-09-29 18:26         ` Christoffer Dall
2014-09-29 12:59   ` Christoffer Dall
2014-10-09 13:07   ` Marc Zyngier
2014-10-09 13:11     ` Ard Biesheuvel
2014-10-09 13:46       ` Marc Zyngier
2014-09-17 21:56 ` [PATCH 4/6] ARM: kvm: define PAGE_S2_DEVICE as read-only by default Ard Biesheuvel
2014-09-29 13:05   ` Christoffer Dall
2014-10-09 13:10   ` Marc Zyngier
2014-09-17 21:56 ` [PATCH 5/6] arm64: " Ard Biesheuvel
2014-09-29 13:06   ` Christoffer Dall
2014-09-29 13:34     ` Will Deacon
2014-10-09 13:10   ` Marc Zyngier
2014-09-17 21:56 ` Ard Biesheuvel [this message]
2014-09-29 12:52   ` [PATCH 6/6] arm/arm64: KVM: map MMIO regions at creation time Christoffer Dall
2014-09-29 13:07 ` [PATCH 0/6] KVM: handling of MMIO pass-through regions Christoffer Dall
2014-10-01  5:36   ` Ard Biesheuvel
2014-10-10 11:08 ` Christoffer Dall

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=1410990981-665-7-git-send-email-ard.biesheuvel@linaro.org \
    --to=ard.biesheuvel@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.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;
as well as URLs for NNTP newsgroup(s).