From patchwork Fri Jul 12 08:22:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: KarimAllah Ahmed X-Patchwork-Id: 11041867 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2F12313A4 for ; Fri, 12 Jul 2019 08:23:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1AF3C28BC9 for ; Fri, 12 Jul 2019 08:23:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 08CD028BC6; Fri, 12 Jul 2019 08:23:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.4 required=2.0 tests=BAYES_00,DKIM_ADSP_ALL, DKIM_SIGNED,DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id F089A28BC6 for ; Fri, 12 Jul 2019 08:23:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=zA3G3BM6sCPkxclfr0W25Gpjpzn/IUCXdvjBezndnC4=; b=LBi bWscK+qxNLDEaIFMzHuVB6k05r0Q9TCUu32x+uTvtYS6HagSsA9dDYaRZKyF2XDuaSKaKVYUO1ZME 50XGbmv+KizcSL1K+zfJvKY96rZxfHXU7jpWW8rQ9CJtiKjYggoZUCgnEguDb4nPd+CEYm8i40k2q 4RYc0e9fdZi/VWLB327TGzfDvDN7hBSx55E7BtdAEwqAHcM/9EchSJAlgfLjyH40z3V3gB4pV7Owa h2NEWGFOmu3J5GBfSqe2yekqt/rCqf+bIuA2slPr/34Z5PdVlT4qydhhMFEOQM2I0L+6IvjIOuYIi S5JDd+yyx1urN0/0LTduoFQqsWoqhZA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hlqpc-0008PC-9N; Fri, 12 Jul 2019 08:23:04 +0000 Received: from smtp-fw-6001.amazon.com ([52.95.48.154]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hlqpZ-0008OU-0G for linux-arm-kernel@lists.infradead.org; Fri, 12 Jul 2019 08:23:02 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1562919780; x=1594455780; h=from:to:cc:subject:date:message-id; bh=2GHXvodd5mYiOI7g2dcIp8dLyBwMPtlaooER9mc1rHw=; b=MgrpSQioI9pnDPyv6io4SECNunYVgwSHkwxipE5C15aT0ppHC4t2/o3Y bU6opfOJe7elcvIf0oQMK9EN/KneZBnYQbNpzCcvk5OMtbMYAqdOl1bbL g4aAin14NhGjseCQdu3EAOnXFwQb6KJHoMowo0tPYnDEmoCxtgbeaBqwh Y=; X-IronPort-AV: E=Sophos;i="5.62,481,1554768000"; d="scan'208";a="404647834" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1a-af6a10df.us-east-1.amazon.com) ([10.124.125.6]) by smtp-border-fw-out-6001.iad6.amazon.com with ESMTP; 12 Jul 2019 08:22:57 +0000 Received: from u54e1ad5160425a4b64ea.ant.amazon.com (iad7-ws-svc-lb50-vlan3.amazon.com [10.0.93.214]) by email-inbound-relay-1a-af6a10df.us-east-1.amazon.com (Postfix) with ESMTPS id 8BFEEA252A; Fri, 12 Jul 2019 08:22:53 +0000 (UTC) Received: from u54e1ad5160425a4b64ea.ant.amazon.com (localhost [127.0.0.1]) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id x6C8MoPk016257; Fri, 12 Jul 2019 10:22:50 +0200 Received: (from karahmed@localhost) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Submit) id x6C8MnRH016242; Fri, 12 Jul 2019 10:22:49 +0200 From: KarimAllah Ahmed To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH] KVM: arm/arm64: Properly check for MMIO regions Date: Fri, 12 Jul 2019 10:22:08 +0200 Message-Id: <1562919728-642-1-git-send-email-karahmed@amazon.de> X-Mailer: git-send-email 2.7.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190712_012301_172493_E01006C8 X-CRM114-Status: GOOD ( 11.59 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Suzuki K Pouloze , KarimAllah Ahmed , Julien Thierry , Marc Zyngier , James Morse , kvmarm@lists.cs.columbia.edu MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Valid RAM can live outside kernel control (e.g. using "mem=" command-line parameter). This memory can still be used as valid guest memory for KVM. So ensure that we validate that this memory is definitely not "RAM" before assuming that it is an MMIO region. One way to use memory outside kernel control is: 1- Pass 'mem=' in the kernel command-line to limit the amount of memory managed by the kernel. 2- Map this physical memory you want to give to the guest with: mmap("/dev/mem", physical_address_offset, ..) 3- Use the user-space virtual address as the "userspace_addr" field in KVM_SET_USER_MEMORY_REGION ioctl. One of the limitations of the current /dev/mem for ARM is that it would map this memory as uncached without this patch: https://lkml.org/lkml/2019/7/11/684 This work is similar to the work done on x86 here: https://lkml.org/lkml/2019/1/31/933 Cc: Marc Zyngier Cc: James Morse Cc: Julien Thierry Cc: Suzuki K Pouloze Cc: linux-arm-kernel@lists.infradead.org Cc: kvmarm@lists.cs.columbia.edu Cc: linux-kernel@vger.kernel.org Signed-off-by: KarimAllah Ahmed --- virt/kvm/arm/mmu.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/virt/kvm/arm/mmu.c b/virt/kvm/arm/mmu.c index 06180c9..2105134 100644 --- a/virt/kvm/arm/mmu.c +++ b/virt/kvm/arm/mmu.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -89,7 +90,7 @@ static void kvm_flush_dcache_pud(struct kvm *kvm, static bool kvm_is_device_pfn(unsigned long pfn) { - return !pfn_valid(pfn); + return !memblock_is_memory(__pfn_to_phys(pfn)); } /** @@ -949,6 +950,7 @@ static void stage2_unmap_memslot(struct kvm *kvm, do { struct vm_area_struct *vma = find_vma(current->mm, hva); hva_t vm_start, vm_end; + gpa_t gpa; if (!vma || vma->vm_start >= reg_end) break; @@ -959,11 +961,14 @@ static void stage2_unmap_memslot(struct kvm *kvm, vm_start = max(hva, vma->vm_start); vm_end = min(reg_end, vma->vm_end); - if (!(vma->vm_flags & VM_PFNMAP)) { - gpa_t gpa = addr + (vm_start - memslot->userspace_addr); - unmap_stage2_range(kvm, gpa, vm_end - vm_start); - } hva = vm_end; + + if ((vma->vm_flags & VM_PFNMAP) && + !memblock_is_memory(__pfn_to_phys(vma->vm_pgoff))) + continue; + + gpa = addr + (vm_start - memslot->userspace_addr); + unmap_stage2_range(kvm, gpa, vm_end - vm_start); } while (hva < reg_end); } @@ -2329,7 +2334,8 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, vm_start = max(hva, vma->vm_start); vm_end = min(reg_end, vma->vm_end); - if (vma->vm_flags & VM_PFNMAP) { + if ((vma->vm_flags & VM_PFNMAP) && + !memblock_is_memory(__pfn_to_phys(vma->vm_pgoff))) { gpa_t gpa = mem->guest_phys_addr + (vm_start - mem->userspace_addr); phys_addr_t pa;