Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755087AbdHYIcr (ORCPT ); Fri, 25 Aug 2017 04:32:47 -0400 Received: from mail-db5eur01on0078.outbound.protection.outlook.com ([104.47.2.78]:58016 "EHLO EUR01-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1755023AbdHYIcf (ORCPT ); Fri, 25 Aug 2017 04:32:35 -0400 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Florent.Revest@arm.com; From: Florent Revest To: linux-arm-kernel@lists.infradead.org Cc: matt@codeblueprint.co.uk, ard.biesheuvel@linaro.org, pbonzini@redhat.com, rkrcmar@redhat.com, christoffer.dall@linaro.org, catalin.marinas@arm.com, will.deacon@arm.com, mark.rutland@arm.com, marc.zyngier@arm.com, linux-efi@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, leif.lindholm@arm.com, revestflo@gmail.com, Florent Revest Subject: [RFC 04/11] KVM, arm, arm64: Offer PAs to IPAs idmapping to internal VMs Date: Fri, 25 Aug 2017 09:31:34 +0100 Message-Id: <1503649901-5834-5-git-send-email-florent.revest@arm.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1503649901-5834-1-git-send-email-florent.revest@arm.com> References: <1503649901-5834-1-git-send-email-florent.revest@arm.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [217.140.96.140] X-ClientProxiedBy: HE1PR09CA0051.eurprd09.prod.outlook.com (2603:10a6:7:3c::19) To HE1PR08MB0812.eurprd08.prod.outlook.com (2a01:111:e400:59b1::30) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e78dc109-54ff-4ffc-3374-08d4eb93cca6 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(48565401081)(300000503095)(300135400095)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095);SRVR:HE1PR08MB0812; X-Microsoft-Exchange-Diagnostics: 1;HE1PR08MB0812;3:n8pc5EHsf2n7wLw+ufbSHF6XYq/nSxAU+wPVsb7ufNIHp9uXlKqF6VDNav70YpVrI89c20sv5fvsNhAVDStbHxJdtpcSnal+IJEwthzTz0bH5wFMN37wGzDz/Z8L/n4lz5YRKYM68JMHHBhfmSSEeoItstNMOmA9Km6QSGG9PCmkXbeZJtDDdwegRVsI2njCClfAmWxADQ9DJ3tmmF3yrRX8XoJJ0/2z5CCyWK8qwPwWBk0vJ5VXji4EDegUqpZY;25:JIx1hgkK3wvT1awm0wDLKURdMCUqWEFAhc0OGmccWWy2eUwvV/9JKQ4qDy0Aanwabkesa0B8ZmIrYLn1A0tNnP9nkTRuKklx+1WgShIYcMN0zFVDGwLwUsnDU/Sz1aRr+Ll2kJftjKVj+A7N03bJf5QHYC1wXJFU1nEN3JbY6vvELqqDVoOY6POtlonVoqOLjiNVmXCjIX6OntcQHdXasOwS+OnJWk8Vvw+cSoHaJGR3SxEIDM3pcpObamDTERnVZE7Lgi0C8wQtWWmlHpbKQ6sLuxMv036V8J5hQn/FQ5NeDD2Fs3XhUl1h74ulNDgBwwOacD+zwPW/YMftZBsX/g==;31:EcHQ6nVKqCgKxGhpi021FEHgGEFWlMn5PFbDFFi81gBKmmE2YwBJ1P+W/DAjD5MBJAfRAs3sFGoXw1b4xE6y1OU0kNqAVKraXpy8agoovOA62kqxmWHVQdTAPnI1SFbdlngJdhwfS/jDOxRwEgDNQKjmsSPZmZiOkGZzs4d04B9ss6XQwp3wHxSXv76z5GgSO7UFZbRE0LEEXHHx+NDLm5R/XAgMLMT7oW3Ljsj+gBI= X-MS-TrafficTypeDiagnostic: HE1PR08MB0812: X-Microsoft-Exchange-Diagnostics: 1;HE1PR08MB0812;20:1npaqCzgDLdLM8T4XAAkJROltERLqXreETOgmSq/RoucmQeF7wB/8l79vHMOgc0bt7ewIWbjnww27M04zthLLf33Ed9q85npleXWNSOwjff3a27aE1LLRXtZizJYe/GIwtfIu8XEqrf96yFhjWG6A7YVWzSdONSnOFOcBYRncaOSFF2r3Bi2xkKIhYEVX6jJex/EkAZX/zMIcLhI+oF4hOrGgEI9ynZxvCKum8Z+QfoRXpObnBWXVgewluEk/ODXdJ35xY0/5nBzdr0Va7Kp7qv0unMwmGvlmmxeUnguWvmNSXkbLogbPOmFH49zx3TK2P9aQWcbGSJCaFDnG7czxwtJDKzFMC5ZjK6caPmFkvTd9W4kyW5Kmqz3vAK0xEnxyMWcy6oMThLuCeruw0kWqyyAkJQHgBayJgJ6GGLh6P1o7A6x+WOrm6lZyHFprVzBmAaCVvds0JLvwAsZS1G7bl9DMdw1odoiDjdXKpT+O7B4IIiDrwMlp6++XQCR/VKN;4:Bz5MijO8IS/tPxw+UHQj7VFQKenD7rKw+dgWoVqMU9JjDZk4epXhGOKrvg3G+MOh8USwWeZx/n/c3UzZ8WbV6/WwQ0RgUOFttrjJFV+ek4Qndx5dRBmql/2n/PRMnpqIlysw0727Q6Bd+KuSoa2+pY6bmh6d7YadTZdIv0BVygFwZfKHNOePQNizRqBDR4/CrXoHrD29CzUSN1ltzbDcfzz598zHowtg53QAlNdPrYfRq3v2DkPa11rvzEK4OjXu9GjQgk9NK7DKZoPprRR8be9lkrpr9wJM3U7plKVOqdg= X-Exchange-Antispam-Report-Test: UriScan:(180628864354917); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(93006095)(93001095)(100000703101)(100105400095)(6055026)(6041248)(20161123560025)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123564025)(20161123555025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:HE1PR08MB0812;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:HE1PR08MB0812; X-Forefront-PRVS: 041032FF37 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(7370300001)(6009001)(39860400002)(199003)(40434004)(189002)(478600001)(68736007)(76176999)(25786009)(5660300001)(97736004)(36756003)(6486002)(50226002)(47776003)(50986999)(50466002)(4326008)(48376002)(7350300001)(110136004)(66066001)(7416002)(8676002)(189998001)(305945005)(5890100001)(6916009)(2351001)(105586002)(42186005)(6116002)(2361001)(8746002)(3846002)(86362001)(2906002)(72206003)(81156014)(81166006)(2950100002)(106356001)(6666003)(7736002)(33646002)(101416001)(5003940100001)(53936002);DIR:OUT;SFP:1101;SCL:1;SRVR:HE1PR08MB0812;H:e106757-lin.cambridge.arm.com;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;HE1PR08MB0812;23:EDfBnBaPjWBnOm9SPnB5bTHFwDBn37X/StIIoclN5?= =?us-ascii?Q?zsQtfrKYgvBHJaA7sVdars9vKESocccLMsVQudToLpepC3GGOy7xizA8XxUG?= =?us-ascii?Q?mvG2WDufAq4KF0GyEGGyGxM6c+hUnw5/UK4xSgTmFMpZ1vQthJSbIRSuqVbD?= =?us-ascii?Q?L4P/fS872muZzFIMRCLMX+Pyrx1TZIvpaJndqyZvj8n4W8XXlyb19o1wzw85?= =?us-ascii?Q?RWUyOWRc00ppOYlpfAkIc9Ko4DgT3HMLUAKuhQnh2uQj6RhM6jtRmEfxCD14?= =?us-ascii?Q?332iqh89UZ6XZzVoKEjpqbNiuQDJu5N7+unCh9fKBSJKh7a242QQdLwlIiQT?= =?us-ascii?Q?iN9jV5YH3JvHPNokotaPdP6Dq4i5NGOjHoTUv3nKQRwwv3urr2ze4+CgZ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 1;HE1PR08MB0812;6:QBpQe8BLOJ17DtuMjwb6nq8MSotUTw/IPDGgKXW96OpG7MOVu7OAeE6u9S+7+u5GSMJyKxSc3UmADKv//E4bPyZW4De7W03hFozq5KpM6V7sxMVX2q/C6bTOuf4reecxTL3Y2TdzKeZZTAf0RPUYsfoCXqIlENLrDZzaMs6jPgjYZrncXRD1CLslTRUQRVUkmNHoLtT7uAeXGXlZv6uw1Q4OjZZwG5x4Qaswx+4L+cwEcBvzkM81pjsouvPVXB0uil2m7Zz0lHJjJTMWI1yu3Ug6t77S3YZRSqk54owLTdGYOO/15n/T/tODdWsoAXUfX3ksO0PfcSDkaHX7Vedibg==;5:3pwsxs5YEGYLmwDI68qZDv52AUWcYRj7vRpDn5tbOOCckos326KdZTQ5In/pqQE8tHMYBdr5yUa9ipcOXIlDmdXKC1bYmKzbOnTzfNajQO8a//tg7nxeSC3RFNxTH50wgya6m3Ct/IZoaLVoPhFn+w==;24:t+Y3rfwX85ZndZGXOr+tlZs8LLeb+vgBfz9URtwl6paMNv/jVGU3gl+fnCemYXP7LuIlqyvBiYa+2Pnz486uMz5iS4nvmNJSQuQjqwmUZNc=;7:yLdK6OlZpFKdE6G3D8Hjb7OxE5ZW6P6/dc8wkCzK15k2gXskFMPh/97+EIPmO/JplAFjQnm6XLyZ810gItJFmnXQxMWjGMEYUgCN/AmjsXi6lOtH0usJazd4IJypHGbP2Em0kg0NL0UMoDQqZkJT1EJDV9sa4Btimd6rhO5JsExxNaiNcuKOCn6W0MtN8eKMW/O48wQkEmqN6BCrxjmHCHpnTg6ZksFLOtEOdvUWN/U= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Aug 2017 08:32:18.1621 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR08MB0812 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by nfs id v7P8ZmJE009996 Content-Length: 7133 Lines: 186 Usual KVM virtual machines map guest's physical addresses from a process userspace memory. However, with the new concept of internal VMs, a virtual machine can be created from the kernel, without any link to a userspace context. Hence, some of the KVM's architecture-specific code needs to be modified to take this kind of VMs into account. The approach chosen with this patch is to let internal VMs idmap physical addresses into intermediary physical addresses by calling kvm_set_memory_region with a kvm_userspace_memory_region where the guest_phys_addr field points both to the original PAs and to the IPAs. The userspace_addr field of this struct is therefore ignored with internal VMs. This patch extends the capabilities of the arm and arm64 stage2 MMU code to handle internal VMs. Three things are changed: - Various parts of the MMU code which are related to a userspace context are now only executed if kvm->mm is present. - When this pointer is NULL, struct kvm_userspace_memory_regions are treated by internal_vm_prep_mem as idmaps of physical memory. - A set of 256 additional private memslots is now reserved on arm64 for the usage of internal VMs memory idmapping. Note: this patch should have pretty much no performance impact on the critical path of traditional VMs since only one unlikely branch had to be added to the page fault handler. Signed-off-by: Florent Revest --- arch/arm64/include/asm/kvm_host.h | 1 + virt/kvm/arm/mmu.c | 76 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index d686300..65aab35 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -32,6 +32,7 @@ #define __KVM_HAVE_ARCH_INTC_INITIALIZED #define KVM_USER_MEM_SLOTS 512 +#define KVM_PRIVATE_MEM_SLOTS 256 #define KVM_HALT_POLL_NS_DEFAULT 500000 #include diff --git a/virt/kvm/arm/mmu.c b/virt/kvm/arm/mmu.c index 2ea21da..1d2d3df 100644 --- a/virt/kvm/arm/mmu.c +++ b/virt/kvm/arm/mmu.c @@ -772,6 +772,11 @@ static void stage2_unmap_memslot(struct kvm *kvm, phys_addr_t size = PAGE_SIZE * memslot->npages; hva_t reg_end = hva + size; + if (unlikely(!kvm->mm)) { + unmap_stage2_range(kvm, addr, size); + return; + } + /* * A memory region could potentially cover multiple VMAs, and any holes * between them, so iterate over all of them to find out if we should @@ -819,7 +824,8 @@ void stage2_unmap_vm(struct kvm *kvm) int idx; idx = srcu_read_lock(&kvm->srcu); - down_read(¤t->mm->mmap_sem); + if (likely(kvm->mm)) + down_read(¤t->mm->mmap_sem); spin_lock(&kvm->mmu_lock); slots = kvm_memslots(kvm); @@ -827,7 +833,8 @@ void stage2_unmap_vm(struct kvm *kvm) stage2_unmap_memslot(kvm, memslot); spin_unlock(&kvm->mmu_lock); - up_read(¤t->mm->mmap_sem); + if (likely(kvm->mm)) + up_read(¤t->mm->mmap_sem); srcu_read_unlock(&kvm->srcu, idx); } @@ -1303,6 +1310,12 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, return -EFAULT; } + if (unlikely(!kvm->mm)) { + kvm_err("Unexpected internal VM page fault\n"); + kvm_inject_vabt(vcpu); + return 0; + } + /* Let's check if we will get back a huge page backed by hugetlbfs */ down_read(¤t->mm->mmap_sem); vma = find_vma_intersection(current->mm, hva, hva + 1); @@ -1850,6 +1863,54 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, kvm_mmu_wp_memory_region(kvm, mem->slot); } +/* + * internal_vm_prep_mem - maps a range of hpa to gpa at stage2 + * + * While userspace VMs manage gpas using hvas, internal virtual machines need a + * way to map physical addresses to a guest. In order to avoid code duplication, + * the kvm_set_memory_region call is kept for internal VMs, however it usually + * expects a struct kvm_userspace_memory_region with a userspace_addr field. + * With internal VMs, this field is ignored and physical memory memory pointed + * by guest_phys_addr can only be idmapped. + */ +static int internal_vm_prep_mem(struct kvm *kvm, + const struct kvm_userspace_memory_region *mem) +{ + phys_addr_t addr, end; + unsigned long pfn; + int ret; + struct kvm_mmu_memory_cache cache = { 0 }; + + end = mem->guest_phys_addr + mem->memory_size; + pfn = __phys_to_pfn(mem->guest_phys_addr); + addr = mem->guest_phys_addr; + + for (; addr < end; addr += PAGE_SIZE) { + pte_t pte = pfn_pte(pfn, PAGE_S2); + + pte = kvm_s2pte_mkwrite(pte); + + ret = mmu_topup_memory_cache(&cache, + KVM_MMU_CACHE_MIN_PAGES, + KVM_NR_MEM_OBJS); + if (ret) { + mmu_free_memory_cache(&cache); + return ret; + } + spin_lock(&kvm->mmu_lock); + ret = stage2_set_pte(kvm, &cache, addr, &pte, 0); + spin_unlock(&kvm->mmu_lock); + if (ret) { + mmu_free_memory_cache(&cache); + return ret; + } + + pfn++; + } + + return ret; +} + int kvm_arch_prepare_memory_region(struct kvm *kvm, struct kvm_memory_slot *memslot, const struct kvm_userspace_memory_region *mem, @@ -1872,6 +1933,13 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, (KVM_PHYS_SIZE >> PAGE_SHIFT)) return -EFAULT; + if (unlikely(!kvm->mm)) { + ret = internal_vm_prep_mem(kvm, mem); + if (ret) + goto out; + goto out_internal_vm; + } + down_read(¤t->mm->mmap_sem); /* * A memory region could potentially cover multiple VMAs, and any holes @@ -1930,6 +1998,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, hva = vm_end; } while (hva < reg_end); +out_internal_vm: if (change == KVM_MR_FLAGS_ONLY) goto out; @@ -1940,7 +2009,8 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, stage2_flush_memslot(kvm, memslot); spin_unlock(&kvm->mmu_lock); out: - up_read(¤t->mm->mmap_sem); + if (kvm->mm) + up_read(¤t->mm->mmap_sem); return ret; } -- 1.9.1 IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.