From patchwork Fri Oct 10 15:00:32 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 38590 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f71.google.com (mail-la0-f71.google.com [209.85.215.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 766C4205D8 for ; Fri, 10 Oct 2014 15:09:26 +0000 (UTC) Received: by mail-la0-f71.google.com with SMTP id gi9sf2287451lab.2 for ; Fri, 10 Oct 2014 08:09:25 -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=XlCLM1Jj/kPi4nY8l7v/acVLYhXmhYCQk+lCrqF/agM=; b=hnfrca7PKeCJ07pCaB9pTk+qo5SZ7GPWwu4dkvTT5z9qQvf9pmGqf+68s/Ro70xfhL 8TI3Ik9rTpNgWCCr9GMoCLb+NXpxmYFavyOQ0cA7TuAfYoOnTJjCS1Z3jir3tv9HTtqL qaVoGpL1BKBlk3N3r8pC4x6wqfddRUW6GFIwgRMeNhps51Gr0zSMWn/w+Uj5noIE+AqT 1g3ujoFsh/bp8fdk4dvHQZNIhUDtub9p1dLkfRCmS5JSqiHgbgRJ1+lWdvEm3PZ/zl+J 31UEYZ6sEob6szc+BvyTyD1c7RgTWSMc+rA7gTv5HV6kFgh6bRA1ETo9waV3HkwFMH2g 9BWw== X-Gm-Message-State: ALoCoQnPMY7x4FpNFqhGEHZhGRMR8nPGLLMnaNG+a2I2y92LtQ//usvgSFE8bxgO4dujeE4PuV0O X-Received: by 10.112.158.73 with SMTP id ws9mr11526lbb.22.1412953373900; Fri, 10 Oct 2014 08:02:53 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.21.135 with SMTP id v7ls294940lae.40.gmail; Fri, 10 Oct 2014 08:02:53 -0700 (PDT) X-Received: by 10.112.140.137 with SMTP id rg9mr4047072lbb.93.1412953373652; Fri, 10 Oct 2014 08:02:53 -0700 (PDT) Received: from mail-lb0-f173.google.com (mail-lb0-f173.google.com [209.85.217.173]) by mx.google.com with ESMTPS id xm7si9626289lbb.97.2014.10.10.08.02.53 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 10 Oct 2014 08:02:53 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.173 as permitted sender) client-ip=209.85.217.173; Received: by mail-lb0-f173.google.com with SMTP id 10so3258299lbg.4 for ; Fri, 10 Oct 2014 08:02:53 -0700 (PDT) X-Received: by 10.112.14.34 with SMTP id m2mr5283951lbc.74.1412953373562; Fri, 10 Oct 2014 08:02:53 -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 c5csp444342lbz; Fri, 10 Oct 2014 08:02:52 -0700 (PDT) X-Received: by 10.68.221.36 with SMTP id qb4mr3102403pbc.159.1412953370016; Fri, 10 Oct 2014 08:02:50 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id so6si3980323pac.164.2014.10.10.08.02.49 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Oct 2014 08:02:50 -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 1Xcbgo-00071P-Ms; Fri, 10 Oct 2014 15:01:06 +0000 Received: from mail-wg0-f50.google.com ([74.125.82.50]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1Xcbgj-0006yO-KN for linux-arm-kernel@lists.infradead.org; Fri, 10 Oct 2014 15:01:02 +0000 Received: by mail-wg0-f50.google.com with SMTP id a1so4177230wgh.9 for ; Fri, 10 Oct 2014 08:00:39 -0700 (PDT) X-Received: by 10.194.184.111 with SMTP id et15mr5516356wjc.14.1412953239330; Fri, 10 Oct 2014 08:00:39 -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 bj7sm7282059wjc.33.2014.10.10.08.00.37 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 10 Oct 2014 08:00:38 -0700 (PDT) From: Ard Biesheuvel To: christoffer.dall@linaro.org, marc.zyngier@arm.com Subject: [PATCH v4] arm/arm64: KVM: map MMIO regions at creation time Date: Fri, 10 Oct 2014 17:00:32 +0200 Message-Id: <1412953232-11699-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-20141010_080101_822534_6F63B737 X-CRM114-Status: GOOD ( 19.43 ) 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.50 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [74.125.82.50 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record -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.217.173 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. So instead, detect such regions at setup time and install the mappings for the backing all at once. Signed-off-by: Ard Biesheuvel Reviewed-by: Christoffer Dall Acked-by: Marc Zyngier --- Changes since v3: - reject the case where any of the VMAs being covered by a read-write memory region are read-only themselves Changes since v2: - moved the unmapping of moved/deleted regions to kvm_arch_flush_shadow_memslot so it occurs before parts of the new regions may be mapped in kvm_arch_prepare_memory_region - allow memory regions with holes 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 My test case (UEFI under QEMU/KVM) still executes correctly with this patch, but more thorough testing with actual passthrough device regions is in order. arch/arm/kvm/mmu.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/arch/arm/kvm/mmu.c b/arch/arm/kvm/mmu.c index 37c1b35f90ad..fd7b1ebc5d32 100644 --- a/arch/arm/kvm/mmu.c +++ b/arch/arm/kvm/mmu.c @@ -1132,13 +1132,6 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, const struct kvm_memory_slot *old, enum kvm_mr_change change) { - gpa_t gpa = old->base_gfn << PAGE_SHIFT; - phys_addr_t size = old->npages << PAGE_SHIFT; - if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) { - spin_lock(&kvm->mmu_lock); - unmap_stage2_range(kvm, gpa, size); - spin_unlock(&kvm->mmu_lock); - } } int kvm_arch_prepare_memory_region(struct kvm *kvm, @@ -1146,7 +1139,69 @@ 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; + bool writable = !(mem->flags & KVM_MEM_READONLY); + int ret = 0; + + if (change != KVM_MR_CREATE && change != KVM_MR_MOVE) + return 0; + + /* + * A memory region could potentially cover multiple VMAs, and any holes + * between them, 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_start, vm_end; + + if (!vma || vma->vm_start >= reg_end) + break; + + /* + * Mapping a read-only VMA is only allowed if the + * memory region is configured as read-only. + */ + if (writable && !(vma->vm_flags & VM_WRITE)) { + ret = -EPERM; + break; + } + + /* + * Take the intersection of this VMA with the memory region + */ + vm_start = max(hva, vma->vm_start); + vm_end = min(reg_end, vma->vm_end); + + if (vma->vm_flags & VM_PFNMAP) { + gpa_t gpa = mem->guest_phys_addr + + (vm_start - mem->userspace_addr); + phys_addr_t pa = (vma->vm_pgoff << PAGE_SHIFT) + + vm_start - vma->vm_start; + + ret = kvm_phys_addr_ioremap(kvm, gpa, pa, + vm_end - vm_start, + writable); + if (ret) + break; + } + 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, @@ -1171,4 +1226,10 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm) void kvm_arch_flush_shadow_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) { + gpa_t gpa = slot->base_gfn << PAGE_SHIFT; + phys_addr_t size = slot->npages << PAGE_SHIFT; + + spin_lock(&kvm->mmu_lock); + unmap_stage2_range(kvm, gpa, size); + spin_unlock(&kvm->mmu_lock); }