Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3755543pxb; Tue, 26 Jan 2021 04:03:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJyNJnAhESIGJZeHbRy7gaeNKCuQ0gsRpgX8fUcTjbgvOfEDs4wnVsj0RWMtKhKm2EduGy83 X-Received: by 2002:aa7:d401:: with SMTP id z1mr4259921edq.213.1611662630909; Tue, 26 Jan 2021 04:03:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611662630; cv=none; d=google.com; s=arc-20160816; b=Jff4iNdowrVqaY5NhH7nP5RCiGJm0brFYybSjgLJPOCZ30ALulwIM75twgpMOHWL+q gznR1DpRI2/rNoYIU9Gp914SfJJQBvJMdgvzMKpBQXRwFDa+emiGljBS6FrMN1jWZBRN Vq+Usyuk1v4Xw2ncvL2BBBpM5HpIL7le3QYIyDTcojkEhqrrVfos+Re9zAHQLcVdYGVX OBJtajpsbLEJdyUtGGNbpgXAj8x30QtxvDUcEitxwbLUOAuOT08YAZUgjsWreKnA8BER Da2PN1rTQ/tqT+QopVoJAL9YBaQCWkU2cMxhxgPh3AFfYdnaGUlCJQN2WU/sdWIv3FZU Brhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=0K+uxPNJ6B3Dq1KRGq+oPPt+0m7OL0bJ7iaA9EZ6ZgM=; b=S+79Rlm+SMX0UPEfSx8i+tk/ljW8/8kdHoF0DuqviaoHriKVO3h1fS16/ivEJCfaSM /IeCqxZ1sNLaN1U5/IBnrPQnidPS0i1o3R9+gXn2O6GjpDYHA/9p+uAV1RbK/3oh22z/ Cmy5RcCkypLx4GY8rPMrNxK9jECGPPgh1rt4jS/C1hbrQHhyGMXErtDAukdE68Ved22b inaCQRuu8mxloMsbNFUjANKiLBVWOy500P9v6HHCjLWDr8S9kgKTqo4hMQRbvY/9koyR Ckge1L5Fl8LU//UBO8d+aWUwZTYJxpukz9XGO9MYRV1sFo2aPb4vony85/+k02jOZ7EW vpSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=B9nMSuzr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cb22si8459445edb.112.2021.01.26.04.03.25; Tue, 26 Jan 2021 04:03:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=B9nMSuzr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405452AbhAZMBq (ORCPT + 99 others); Tue, 26 Jan 2021 07:01:46 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:52409 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405574AbhAZMBb (ORCPT ); Tue, 26 Jan 2021 07:01:31 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611662400; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0K+uxPNJ6B3Dq1KRGq+oPPt+0m7OL0bJ7iaA9EZ6ZgM=; b=B9nMSuzr5B85MRydWuaW8p7w0ZeGn1eQYBSK/ztxKrn9X2VotThT3aSSoFMoKOAQQwnBlp GOFGVd4axPNVknbYRHD58ZfTuj+gQ+WU+lJGFNXrcdd8ZHBhEQRNu7f3nHr6vIyv2yoDIO UAWnXeSeVUdcb1oF8m0fCNet9YWLuGY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-544-w5tS4UMfMiCqw4OIpTAjVw-1; Tue, 26 Jan 2021 06:59:58 -0500 X-MC-Unique: w5tS4UMfMiCqw4OIpTAjVw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4AACF180A086; Tue, 26 Jan 2021 11:59:56 +0000 (UTC) Received: from starship (unknown [10.35.206.204]) by smtp.corp.redhat.com (Postfix) with ESMTP id 71B6460C62; Tue, 26 Jan 2021 11:59:49 +0000 (UTC) Message-ID: Subject: Re: [PATCH v3 4/4] KVM: SVM: Support #GP handling for the case of nested on nested From: Maxim Levitsky To: Wei Huang , kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, pbonzini@redhat.com, vkuznets@redhat.com, seanjc@google.com, joro@8bytes.org, bp@alien8.de, tglx@linutronix.de, mingo@redhat.com, x86@kernel.org, jmattson@google.com, wanpengli@tencent.com, bsd@redhat.com, dgilbert@redhat.com, luto@amacapital.net Date: Tue, 26 Jan 2021 13:59:48 +0200 In-Reply-To: <20210126081831.570253-5-wei.huang2@amd.com> References: <20210126081831.570253-1-wei.huang2@amd.com> <20210126081831.570253-5-wei.huang2@amd.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2021-01-26 at 03:18 -0500, Wei Huang wrote: > Under the case of nested on nested (L0->L1->L2->L3), #GP triggered by > SVM instructions can be hided from L1. Instead the hypervisor can > inject the proper #VMEXIT to inform L1 of what is happening. Thus L1 > can avoid invoking the #GP workaround. For this reason we turns on > guest VM's X86_FEATURE_SVME_ADDR_CHK bit for KVM running inside VM to > receive the notification and change behavior. > > Similarly we check if vcpu is under guest mode before emulating the > vmware-backdoor instructions. For the case of nested on nested, we > let the guest handle it. > > Co-developed-by: Bandan Das > Signed-off-by: Bandan Das > Signed-off-by: Wei Huang > Tested-by: Maxim Levitsky > Reviewed-by: Maxim Levitsky > --- > arch/x86/kvm/svm/svm.c | 20 ++++++++++++++++++-- > 1 file changed, 18 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c > index f9233c79265b..83c401d2709f 100644 > --- a/arch/x86/kvm/svm/svm.c > +++ b/arch/x86/kvm/svm/svm.c > @@ -929,6 +929,9 @@ static __init void svm_set_cpu_caps(void) > > if (npt_enabled) > kvm_cpu_cap_set(X86_FEATURE_NPT); > + > + /* Nested VM can receive #VMEXIT instead of triggering #GP */ > + kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK); > } > > /* CPUID 0x80000008 */ > @@ -2198,6 +2201,11 @@ static int svm_instr_opcode(struct kvm_vcpu *vcpu) > > static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode) > { > + const int guest_mode_exit_codes[] = { > + [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN, > + [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD, > + [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE, > + }; > int (*const svm_instr_handlers[])(struct vcpu_svm *svm) = { > [SVM_INSTR_VMRUN] = vmrun_interception, > [SVM_INSTR_VMLOAD] = vmload_interception, > @@ -2205,7 +2213,14 @@ static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode) > }; > struct vcpu_svm *svm = to_svm(vcpu); > > - return svm_instr_handlers[opcode](svm); > + if (is_guest_mode(vcpu)) { > + svm->vmcb->control.exit_code = guest_mode_exit_codes[opcode]; > + svm->vmcb->control.exit_info_1 = 0; > + svm->vmcb->control.exit_info_2 = 0; > + > + return nested_svm_vmexit(svm); > + } else > + return svm_instr_handlers[opcode](svm); > } > > /* > @@ -2239,7 +2254,8 @@ static int gp_interception(struct vcpu_svm *svm) > * VMware backdoor emulation on #GP interception only handles > * IN{S}, OUT{S}, and RDPMC. > */ > - return kvm_emulate_instruction(vcpu, > + if (!is_guest_mode(vcpu)) > + return kvm_emulate_instruction(vcpu, > EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE); > } else > return emulate_svm_instr(vcpu, opcode); To be honest I expected the vmware backdoor fix to be in a separate patch, but I see that Paulo already took these patches so I guess it is too late. Anyway I am very happy to see this workaround merged, and see that bug disappear forever. Best regards, Maxim Levitsky