Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04AA6C433E0 for ; Wed, 27 May 2020 08:29:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D42F42078B for ; Wed, 27 May 2020 08:29:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Bxgki3Zp" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729578AbgE0I3c (ORCPT ); Wed, 27 May 2020 04:29:32 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:59101 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729497AbgE0I3b (ORCPT ); Wed, 27 May 2020 04:29:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590568170; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=8QOC8GoFKK5IDy7Fbg6T/1YhJRpFsrglY/dRIt6Yu28=; b=Bxgki3ZpQ/qu8l1d7ix3l1JYyNPZ5hruJFsuUzKpiNLcFbARQ7hH8+/Abi2gsNOdatQNVM huEIhJVtL9mEBQuJJJ4RK3Ux+hYGc/E9Wx6MckiN79FXMYBMg2EBMVT88+0wD7BaiHmpNQ SpWxMTrWaHt4galD9Zqih1y3O7TYEgU= 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-3-5UPbVJK1OQ2nacCcycmRZQ-1; Wed, 27 May 2020 04:29:27 -0400 X-MC-Unique: 5UPbVJK1OQ2nacCcycmRZQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0133A1005512; Wed, 27 May 2020 08:29:26 +0000 (UTC) Received: from vitty.brq.redhat.com (unknown [10.40.192.209]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7FA8C5D9E5; Wed, 27 May 2020 08:29:23 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org, Paolo Bonzini Cc: Sean Christopherson , Wanpeng Li , Jim Mattson , Joerg Roedel , Cathy Avery , Maxim Levitsky Subject: [PATCH] KVM: nVMX: Preserve registers modifications done before nested_svm_vmexit() Date: Wed, 27 May 2020 10:29:21 +0200 Message-Id: <20200527082921.218601-1-vkuznets@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org L2 guest hang is observed after 'exit_required' was dropped and nSVM switched to check_nested_events() completely. The hang is a busy loop when e.g. KVM is emulating an instruction (e.g. L2 is accessing MMIO space and we drop to userspace). After nested_svm_vmexit() and when L1 is doing VMRUN nested guest's RIP is not advanced so KVM goes into emulating the same instruction which cased nested_svm_vmexit() and the loop continues. nested_svm_vmexit() is not new, however, with check_nested_events() we're now calling it later than before. In case by that time KVM has modified register state we may pick stale values from VMCS when trying to save nested guest state to nested VMCB. VMX code handles this case correctly: sync_vmcs02_to_vmcs12() called from nested_vmx_vmexit() does 'vmcs12->guest_rip = kvm_rip_read(vcpu)' and this ensures KVM-made modifications are preserved. Do the same for nVMX. Generally, nested_vmx_vmexit()/nested_svm_vmexit() need to pick up all nested guest state modifications done by KVM after vmexit. It would be great to find a way to express this in a way which would not require to manually track these changes, e.g. nested_{vmcb,vmcs}_get_field(). Co-debugged-with: Paolo Bonzini Signed-off-by: Vitaly Kuznetsov --- - To certain extent we're fixing currently-pending 'KVM: SVM: immediately inject INTR vmexit' commit but I'm not certain about that. We had so many problems with nested events before switching to check_nested_events() that what worked before could just be treated as a miracle. Miracles tend to appear and disappear all of a sudden. --- arch/x86/kvm/svm/nested.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 0f02521550b9..6b1049148c1b 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -537,9 +537,9 @@ int nested_svm_vmexit(struct vcpu_svm *svm) nested_vmcb->save.cr2 = vmcb->save.cr2; nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu); - nested_vmcb->save.rip = vmcb->save.rip; - nested_vmcb->save.rsp = vmcb->save.rsp; - nested_vmcb->save.rax = vmcb->save.rax; + nested_vmcb->save.rip = kvm_rip_read(&svm->vcpu); + nested_vmcb->save.rsp = kvm_rsp_read(&svm->vcpu); + nested_vmcb->save.rax = kvm_rax_read(&svm->vcpu); nested_vmcb->save.dr7 = vmcb->save.dr7; nested_vmcb->save.dr6 = svm->vcpu.arch.dr6; nested_vmcb->save.cpl = vmcb->save.cpl; -- 2.25.4