From patchwork Wed Oct 8 11:05:10 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 38468 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f199.google.com (mail-wi0-f199.google.com [209.85.212.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 84EF620447 for ; Wed, 8 Oct 2014 11:07:19 +0000 (UTC) Received: by mail-wi0-f199.google.com with SMTP id d1sf3931898wiv.2 for ; Wed, 08 Oct 2014 04:07:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:subject:date:message-id:cc :precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:mime-version:sender:errors-to :x-original-sender:x-original-authentication-results:mailing-list :content-type:content-transfer-encoding; bh=XUzEk+TFhut/PhqiklHJWWFxvI1NK9PJnCzInXeOK6M=; b=gVm5LcxdlTI3JwR3j58QSevGzvNjHvuVV+wWn6Y672x1otI/fHET0tWn45XpUP4rHB wppzJ6u+bg4cDbkACU7/JC+1RhVjeq2fZR9ZtxCYAwhmOZSwMI9Ig4ANT+shGcus2QEz MqjwNeq/42++J98CbLeMfma23C55OafYTiGJg16e7johzjd1Ly7nQIahwwemiC7dKjZZ L7fFd0uBv/zGFVx71Tvii/JtBM7RlZBDRnoYU4ORoH/78MJifn9sOf2IW7nqlLdb9phE 2k11GZ/2o1ZCSAW5T6X8kWVfNkwzuuKcH2kpCuz0aNZRyyTR9GfX+EVAlkDpwgdTIUpg wqGg== X-Gm-Message-State: ALoCoQlJn2Hgc8P2Lra7BTTr7GOdeLKwzY8Jng4mQ52Qm9SbcRj24TPO4v2I3epIELuHjgHuvlwN X-Received: by 10.180.82.74 with SMTP id g10mr5100056wiy.0.1412766438657; Wed, 08 Oct 2014 04:07:18 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.5.166 with SMTP id t6ls56461lat.20.gmail; Wed, 08 Oct 2014 04:07:18 -0700 (PDT) X-Received: by 10.112.149.105 with SMTP id tz9mr2007859lbb.5.1412766438442; Wed, 08 Oct 2014 04:07:18 -0700 (PDT) Received: from mail-la0-f50.google.com (mail-la0-f50.google.com [209.85.215.50]) by mx.google.com with ESMTPS id pi7si32679156lbb.15.2014.10.08.04.07.18 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 08 Oct 2014 04:07:18 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.50 as permitted sender) client-ip=209.85.215.50; Received: by mail-la0-f50.google.com with SMTP id s18so8175820lam.37 for ; Wed, 08 Oct 2014 04:07:18 -0700 (PDT) X-Received: by 10.112.164.71 with SMTP id yo7mr10181469lbb.73.1412766438343; Wed, 08 Oct 2014 04:07:18 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.84.229 with SMTP id c5csp41151lbz; Wed, 8 Oct 2014 04:07:17 -0700 (PDT) X-Received: by 10.66.119.103 with SMTP id kt7mr9586778pab.95.1412766436821; Wed, 08 Oct 2014 04:07:16 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id ot1si6850579pdb.257.2014.10.08.04.07.16 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Oct 2014 04:07:16 -0700 (PDT) Received-SPF: none (google.com: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org does not designate permitted sender hosts) client-ip=2001:1868:205::9; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1Xbp3r-0005K1-UT; Wed, 08 Oct 2014 11:05:39 +0000 Received: from mail-wg0-f42.google.com ([74.125.82.42]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1Xbp3q-0005GC-2k for linux-arm-kernel@lists.infradead.org; Wed, 08 Oct 2014 11:05:38 +0000 Received: by mail-wg0-f42.google.com with SMTP id z12so11215237wgg.1 for ; Wed, 08 Oct 2014 04:05:15 -0700 (PDT) X-Received: by 10.181.5.42 with SMTP id cj10mr34723367wid.28.1412766315666; Wed, 08 Oct 2014 04:05:15 -0700 (PDT) Received: from ards-macbook-pro.local (cag06-7-83-153-85-71.fbx.proxad.net. [83.153.85.71]) by mx.google.com with ESMTPSA id k10sm24161644wjb.28.2014.10.08.04.05.14 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 08 Oct 2014 04:05:14 -0700 (PDT) From: Ard Biesheuvel To: christoffer.dall@linaro.org, marc.zyngier@arm.com Subject: [PATCH v2] arm/arm64: KVM: map MMIO regions at creation time Date: Wed, 8 Oct 2014 13:05:10 +0200 Message-Id: <1412766310-9835-1-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20141008_040538_267546_66F847CD X-CRM114-Status: GOOD ( 16.35 ) X-Spam-Score: -0.7 (/) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-0.7 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [74.125.82.42 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [74.125.82.42 listed in wl.mailspike.net] -0.0 RCVD_IN_MSPIKE_WL Mailspike good senders Cc: kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, Ard Biesheuvel X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ard.biesheuvel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.50 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 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 --- I have omitted the other 5 patches of the series of which this was #6, as Christoffer had indicated they could be merged separately. Changes since v1: - move this logic to kvm_arch_prepare_memory_region() so it can be invoked when moving memory regions as well as when creating memory regions - as we are reasoning about memory regions now instead of memslots, all data is retrieved from the 'mem' argument which points to a struct kvm_userspace_memory_region - minor tweaks to the logic flow Again, compile tested only, due to lack of test cases. arch/arm/kvm/mmu.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c index fe53c3a30383..1403d9dc1190 100644 --- a/arch/arm/kvm/mmu.c +++ b/arch/arm/kvm/mmu.c @@ -1151,7 +1151,57 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_userspace_memory_region *mem, enum kvm_mr_change change) { - return 0; + hva_t hva = mem->userspace_addr; + hva_t reg_end = hva + mem->memory_size; + phys_addr_t gpa = mem->guest_phys_addr; + int ret = 0; + + if (change != KVM_MR_CREATE && change != KVM_MR_MOVE) + return 0; + + /* + * A memory region 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 : | + * +---+---------+-------------------+--------------+----+ + * | memory region | + * +--------------------------------------------+ + */ + do { + struct vm_area_struct *vma = find_vma(current->mm, hva); + hva_t vm_end; + + if (!vma || vma->vm_start > hva) { + ret = -EFAULT; + break; + } + + vm_end = min(reg_end, vma->vm_end); + + if (vma->vm_flags & VM_PFNMAP) { + phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) + hva - + vma->vm_start; + bool writable = (vma->vm_flags & VM_WRITE) && + !(mem->flags & KVM_MEM_READONLY); + + ret = kvm_phys_addr_ioremap(kvm, gpa, pa, vm_end - hva, + writable); + if (ret) + break; + } + gpa += vm_end - hva; + hva = vm_end; + } while (hva < reg_end); + + if (ret) { + spin_lock(&kvm->mmu_lock); + unmap_stage2_range(kvm, mem->guest_phys_addr, mem->memory_size); + spin_unlock(&kvm->mmu_lock); + } + return ret; } void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,