From patchwork Tue Oct 3 03:10:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jintack Lim X-Patchwork-Id: 9981543 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2E5C7602B8 for ; Tue, 3 Oct 2017 03:16:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1FD1D2887F for ; Tue, 3 Oct 2017 03:16:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 14A3F28885; Tue, 3 Oct 2017 03:16:54 +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=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_HI,RCVD_IN_SORBS_SPAM autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4116B28880 for ; Tue, 3 Oct 2017 03:16:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751430AbdJCDL5 (ORCPT ); Mon, 2 Oct 2017 23:11:57 -0400 Received: from mail-it0-f47.google.com ([209.85.214.47]:46899 "EHLO mail-it0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751469AbdJCDLs (ORCPT ); Mon, 2 Oct 2017 23:11:48 -0400 Received: by mail-it0-f47.google.com with SMTP id d192so9963771itd.1 for ; Mon, 02 Oct 2017 20:11:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bN116jzAJwrgpavNNWGV3XrViXg4lVbb8mbGqm39cEc=; b=fsm362S6Fy9b30bUgiUhdQftnZTbbCMQgMCy3NWQH1K433wOzuuZ7nvNvagPSH7Eiq NVN7oxE81FUY7j/1F7AfcZKgz7mflYyMbyulT6KUGtNnkffog00ao5BZBYmZ+LcNv8lq K08TU6EObfIBGuQnpFmQTfI3zwk7apsUClTTk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bN116jzAJwrgpavNNWGV3XrViXg4lVbb8mbGqm39cEc=; b=L4TRjL/Qu6BdjMFTESR7wQnVwWZct/baW/czFC3TLjvB/OjXMO/C9Qq29eo9YNSVvt Z2QrKHhRpPrJKWB0pddbwXWKXljfZKlDAotJIMoqt90xlR++zuPLBESrL60jEEDgEN2p cu75+wC83xmXVEWDiEZqNRWLZD3ASnjXi+na7CA/l4JqCvp/lOtTDKJ83/WZAcIZNQ+Q BZCktLS8QWerIDXMEFUmdI+Wy7kwbY6rG5Em2cvYgiQZqjJqgi48MFpkhkwX+s6qlqOB z+PyYjH8cSUVQBKn4wweVxHnLJsjRCWtNgeF9UN50asIlM0Q2bbpgbDdb8au+orkR6C2 6s0w== X-Gm-Message-State: AMCzsaWw8wCyCdb6PS7u2nj91S0OGl26gCVbHbDjAN7q23ZMDhyB3Yox zn8/cRZqP9NcGSRO+yESv827MQ== X-Google-Smtp-Source: AOwi7QCqLuvJVv402R7tDDygM9aQq7lEdXNb/A6J4QZ4ORqkrp99m/+iDjTw4cgoCiwzXE7VrriwZA== X-Received: by 10.36.93.82 with SMTP id w79mr21531746ita.119.1507000307396; Mon, 02 Oct 2017 20:11:47 -0700 (PDT) Received: from node.jintackl-qv28633.kvmarm-pg0.wisc.cloudlab.us (c220g1-031126.wisc.cloudlab.us. [128.104.222.76]) by smtp.gmail.com with ESMTPSA id h84sm5367193iod.72.2017.10.02.20.11.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 02 Oct 2017 20:11:46 -0700 (PDT) From: Jintack Lim To: christoffer.dall@linaro.org, marc.zyngier@arm.com, kvmarm@lists.cs.columbia.edu Cc: jintack@cs.columbia.edu, pbonzini@redhat.com, rkrcmar@redhat.com, catalin.marinas@arm.com, will.deacon@arm.com, linux@armlinux.org.uk, mark.rutland@arm.com, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jintack Lim Subject: [RFC PATCH v2 09/31] KVM: arm/arm64: Manage mmus for nested VMs Date: Mon, 2 Oct 2017 22:10:51 -0500 Message-Id: <1507000273-3735-7-git-send-email-jintack.lim@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1507000273-3735-1-git-send-email-jintack.lim@linaro.org> References: <1507000273-3735-1-git-send-email-jintack.lim@linaro.org> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Now that a hypervisor can run in the virtual EL2, the guest hypervisor can assign any VMID to its own VMs. To avoid conflicts between VMIDs among a host and guest(s), the host hypervisor maps each VMID from a guest hypervisor's view (i.e. virtual VMID) to an unique shadow VMID. It also manages a set of shadow stage-2 page tables for each shadow VMID. All this information is stored in kvm_nested_s2_mmu struct. A host hypervisor manages a list of kvm_nested_s2_mmu objects per VM. On a VM entry it searches an object in the list using a virtual VMID as a key. Signed-off-by: Jintack Lim --- Notes: v1-->v2: - This is a merged commit of [RFC 39/55] and [RFC 40/55]. - Updated the commit message and comments. - Defer creating a new nested mmu structure until we enter the VM with stage 2 paging enabled, which was previously done on vttbr_el2 write operations. - Use the existing kvm->mmu_lock when iterating nested mmus instead of creating one. arch/arm/include/asm/kvm_host.h | 12 ++++ arch/arm64/include/asm/kvm_emulate.h | 13 ++--- arch/arm64/include/asm/kvm_host.h | 25 ++++++++ arch/arm64/include/asm/kvm_mmu.h | 21 +++++++ arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/context.c | 2 +- arch/arm64/kvm/mmu-nested.c | 108 +++++++++++++++++++++++++++++++++++ virt/kvm/arm/arm.c | 1 + 8 files changed, 174 insertions(+), 9 deletions(-) create mode 100644 arch/arm64/kvm/mmu-nested.c diff --git a/arch/arm/include/asm/kvm_host.h b/arch/arm/include/asm/kvm_host.h index 33ccdbe..d84c1c1 100644 --- a/arch/arm/include/asm/kvm_host.h +++ b/arch/arm/include/asm/kvm_host.h @@ -67,6 +67,15 @@ struct kvm_s2_mmu { pgd_t *pgd; }; +/* Per shadow VMID mmu structure. This is only for nested virtualization */ +struct kvm_nested_s2_mmu { + struct kvm_s2_mmu mmu; + + u64 virtual_vttbr; + + struct list_head list; +}; + struct kvm_arch { /* Stage 2 paging state for the VM */ struct kvm_s2_mmu mmu; @@ -79,6 +88,9 @@ struct kvm_arch { * here. */ + /* Never used on arm but added to be compatible with arm64 */ + struct list_head nested_mmu_list; + /* Interrupt controller */ struct vgic_dist vgic; int max_vcpus; diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h index 71a3a04..f476576 100644 --- a/arch/arm64/include/asm/kvm_emulate.h +++ b/arch/arm64/include/asm/kvm_emulate.h @@ -199,6 +199,11 @@ static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu) return false; } +static inline bool vcpu_nested_stage2_enabled(const struct kvm_vcpu *vcpu) +{ + return (vcpu_sys_reg(vcpu, HCR_EL2) & HCR_VM); +} + static inline u32 kvm_vcpu_get_hsr(const struct kvm_vcpu *vcpu) { return vcpu->arch.fault.esr_el2; @@ -385,12 +390,4 @@ static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu, return data; /* Leave LE untouched */ } -static inline struct kvm_s2_vmid *vcpu_get_active_vmid(struct kvm_vcpu *vcpu) -{ - if (unlikely(is_hyp_ctxt(vcpu))) - return &vcpu->kvm->arch.mmu.el2_vmid; - - return &vcpu->kvm->arch.mmu.vmid; -} - #endif /* __ARM64_KVM_EMULATE_H__ */ diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index a7edf0e..0c37e49 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -65,6 +65,28 @@ struct kvm_s2_mmu { pgd_t *pgd; }; +/* Per shadow VMID mmu structure */ +struct kvm_nested_s2_mmu { + struct kvm_s2_mmu mmu; + + /* + * virtual_vttbr contains vttbr_el2 value from the guest hypervisor. + * We use vmid field as a key to search for this mmu object in the list, + * and ignore baddr field. + * + * Note that we may use both of vmid field and baddr field respectively + * to find a shadow VMID and a pointer to the shadow stage-2 page + * table, then combine them to set up hw_vttbr. The only benefit of + * doing that would be reusing shadow stage-2 page tables for different + * VMIDs, which is not usual. So, we choose the current design for the + * simplicity. + * + */ + u64 virtual_vttbr; + + struct list_head list; +}; + struct kvm_arch { /* Stage 2 paging state for the VM */ struct kvm_s2_mmu mmu; @@ -77,6 +99,9 @@ struct kvm_arch { /* Interrupt controller */ struct vgic_dist vgic; + + /* Stage 2 shadow paging contexts for nested L2 VM */ + struct list_head nested_mmu_list; }; #define KVM_NR_MEM_OBJS 40 diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h index bceaec1..452912f 100644 --- a/arch/arm64/include/asm/kvm_mmu.h +++ b/arch/arm64/include/asm/kvm_mmu.h @@ -112,6 +112,7 @@ #include #include #include +#include static inline unsigned long __kern_hyp_va(unsigned long v) { @@ -321,6 +322,10 @@ static inline unsigned int kvm_get_vmid_bits(void) return (cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR1_VMIDBITS_SHIFT) == 2) ? 16 : 8; } +struct kvm_nested_s2_mmu *get_nested_mmu(struct kvm_vcpu *vcpu, u64 vttbr); +struct kvm_s2_mmu *vcpu_get_active_s2_mmu(struct kvm_vcpu *vcpu); +void update_nested_s2_mmu(struct kvm_vcpu *vcpu); + static inline u64 kvm_get_vttbr(struct kvm_s2_vmid *vmid, struct kvm_s2_mmu *mmu) { @@ -332,5 +337,21 @@ static inline u64 kvm_get_vttbr(struct kvm_s2_vmid *vmid, return baddr | vmid_field; } +static inline u64 get_vmid(u64 vttbr) +{ + return (vttbr & VTTBR_VMID_MASK(get_kvm_vmid_bits())) >> + VTTBR_VMID_SHIFT; +} + +static inline struct kvm_s2_vmid *vcpu_get_active_vmid(struct kvm_vcpu *vcpu) +{ + struct kvm_s2_mmu *mmu = vcpu_get_active_s2_mmu(vcpu); + + if (unlikely(is_hyp_ctxt(vcpu))) + return &mmu->el2_vmid; + else + return &mmu->vmid; +} + #endif /* __ASSEMBLY__ */ #endif /* __ARM64_KVM_MMU_H__ */ diff --git a/arch/arm64/kvm/Makefile b/arch/arm64/kvm/Makefile index 0263ef0..5300db0 100644 --- a/arch/arm64/kvm/Makefile +++ b/arch/arm64/kvm/Makefile @@ -37,4 +37,5 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arch_timer.o kvm-$(CONFIG_KVM_ARM_PMU) += $(KVM)/arm/pmu.o kvm-$(CONFIG_KVM_ARM_HOST) += nested.o +kvm-$(CONFIG_KVM_ARM_HOST) += mmu-nested.o kvm-$(CONFIG_KVM_ARM_HOST) += emulate-nested.o diff --git a/arch/arm64/kvm/context.c b/arch/arm64/kvm/context.c index afd1702..762d4a5 100644 --- a/arch/arm64/kvm/context.c +++ b/arch/arm64/kvm/context.c @@ -177,7 +177,7 @@ static void flush_shadow_el1_sysregs(struct kvm_vcpu *vcpu) static void setup_s2_mmu(struct kvm_vcpu *vcpu) { - struct kvm_s2_mmu *mmu = &vcpu->kvm->arch.mmu; + struct kvm_s2_mmu *mmu = vcpu_get_active_s2_mmu(vcpu); struct kvm_s2_vmid *vmid = vcpu_get_active_vmid(vcpu); vcpu->arch.hw_vttbr = kvm_get_vttbr(vmid, mmu); diff --git a/arch/arm64/kvm/mmu-nested.c b/arch/arm64/kvm/mmu-nested.c new file mode 100644 index 0000000..c436daf --- /dev/null +++ b/arch/arm64/kvm/mmu-nested.c @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2017 - Columbia University and Linaro Ltd. + * Author: Jintack Lim + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include +#include +#include + +static struct kvm_nested_s2_mmu *lookup_nested_mmu(struct kvm_vcpu *vcpu, + u64 vttbr) +{ + struct kvm_nested_s2_mmu *mmu; + u64 virtual_vmid; + u64 target_vmid = get_vmid(vttbr); + struct list_head *nested_mmu_list = &vcpu->kvm->arch.nested_mmu_list; + + /* Search a mmu in the list using the virtual VMID as a key */ + list_for_each_entry_rcu(mmu, nested_mmu_list, list) { + virtual_vmid = get_vmid(mmu->virtual_vttbr); + if (target_vmid == virtual_vmid) + return mmu; + } + return NULL; +} + +/** + * create_nested_mmu - create mmu for the given virtual VMID + * + * Called from setup_s2_mmu before entering the nested VM to ensure the shadow + * stage 2 page table is allocated and it is valid to use. + */ +static struct kvm_nested_s2_mmu *create_nested_mmu(struct kvm_vcpu *vcpu, + u64 vttbr) +{ + struct kvm_nested_s2_mmu *nested_mmu, *tmp_mmu; + struct list_head *nested_mmu_list = &vcpu->kvm->arch.nested_mmu_list; + bool need_free = false; + int ret; + + nested_mmu = kzalloc(sizeof(struct kvm_nested_s2_mmu), GFP_KERNEL); + if (!nested_mmu) + return NULL; + + ret = __kvm_alloc_stage2_pgd(&nested_mmu->mmu); + if (ret) { + kfree(nested_mmu); + return NULL; + } + + spin_lock(&vcpu->kvm->mmu_lock); + tmp_mmu = lookup_nested_mmu(vcpu, vttbr); + if (!tmp_mmu) { + list_add_rcu(&nested_mmu->list, nested_mmu_list); + } else { + /* + * Somebody already put a new nested_mmu for this virtual VMID + * to the list behind our back. + */ + need_free = true; + } + spin_unlock(&vcpu->kvm->mmu_lock); + + if (need_free) { + __kvm_free_stage2_pgd(vcpu->kvm, &nested_mmu->mmu); + kfree(nested_mmu); + nested_mmu = tmp_mmu; + } + + /* The virtual VMID will be used as a key when searching a mmu */ + nested_mmu->virtual_vttbr = vttbr; + + return nested_mmu; +} + +static struct kvm_s2_mmu *get_s2_mmu_nested(struct kvm_vcpu *vcpu) +{ + u64 vttbr = vcpu_sys_reg(vcpu, VTTBR_EL2); + struct kvm_nested_s2_mmu *nested_mmu; + + nested_mmu = lookup_nested_mmu(vcpu, vttbr); + if (!nested_mmu) + nested_mmu = create_nested_mmu(vcpu, vttbr); + + return &nested_mmu->mmu; +} + +struct kvm_s2_mmu *vcpu_get_active_s2_mmu(struct kvm_vcpu *vcpu) +{ + if (is_hyp_ctxt(vcpu) || !vcpu_nested_stage2_enabled(vcpu)) + return &vcpu->kvm->arch.mmu; + + return get_s2_mmu_nested(vcpu); +} diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c index 63dd897..4548d77 100644 --- a/virt/kvm/arm/arm.c +++ b/virt/kvm/arm/arm.c @@ -145,6 +145,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) /* Mark the initial VMID generation invalid */ kvm->arch.mmu.vmid.vmid_gen = 0; kvm->arch.mmu.el2_vmid.vmid_gen = 0; + INIT_LIST_HEAD(&kvm->arch.nested_mmu_list); /* The maximum number of VCPUs is limited by the host's GIC model */ kvm->arch.max_vcpus = vgic_present ?