From patchwork Wed Nov 29 06:07:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 10081451 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 C84746020B for ; Wed, 29 Nov 2017 06:08:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BA814295BA for ; Wed, 29 Nov 2017 06:08:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AF4ED295C5; Wed, 29 Nov 2017 06:08: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=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI 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 64128295BA for ; Wed, 29 Nov 2017 06:08:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751908AbdK2GIB (ORCPT ); Wed, 29 Nov 2017 01:08:01 -0500 Received: from mail-pf0-f194.google.com ([209.85.192.194]:33909 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751054AbdK2GH7 (ORCPT ); Wed, 29 Nov 2017 01:07:59 -0500 Received: by mail-pf0-f194.google.com with SMTP id a90so1109085pfk.1; Tue, 28 Nov 2017 22:07:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vVopUP8aUiNu0A78VWlZVZ5qfb3HYpBo0xxTp/fDh94=; b=GwD5ByfkBadMsfZgyp2r7nzDOLRjb9/vLoybHmXlJsOCos7djoMAMSz35eQBQjIRk4 ey9tUFPRdkPc1Xmhs0ME6+D1GsoNdzZL5Dz41TXAeEc3M4qb3ZJlM0KMna3KDS+z5suL EJNFWtJAKrRHzZhJ5RRrYIHjt47p/iLpv0v+arFNEIOw0bCJcTiU4mSZ7mPkn/GJaGGK f0KetuDscDer0ZgyFOJTy4KWD1xC0qXoJm3nHdTEI6n+ZLlupTQpJNelST3VGxyjfRIR OE12w0Q78YiM8T1x6KI5Mwu3Q+2kamj7NTXrJSQv61Iaf2iYU9X+UdBqpl3U5FQW5GcM Q5jA== 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:mime-version :content-transfer-encoding; bh=vVopUP8aUiNu0A78VWlZVZ5qfb3HYpBo0xxTp/fDh94=; b=ofneVOIam+sXqI/DZV7Iehph3Vf974EpKtHsTTHZmDnUoineyxJMVzcAtQ4FGLYBc6 FafJhTAhB3+BNvMOkQUx2LPpIchE9F4ENFrcIVBbQQAkPsoSi2D9vYYWuyde0/4wK4NX pZo3A4U2cBKAgFr4BB/PEKEIESJptlkpuBecCpxqIq8S1UWumL5M4AOtLjYfyAM8V+HP Nf8ULzHnsak3oSTr1UL9TFljxUaIypbZlM/F1zqObb6twAUaDelrIqVK7DI8xGOXwGIg fx16ibnRMQDHkUZucs0U8H4Tdz+PkJ19vZ56kV0cgP5wGScM8HE2mEGJFPd0SCMTqKTu S32Q== X-Gm-Message-State: AJaThX6VToYoEop9lbJpJez+K5cb8AVz4zoGLnn3wLOIgY7J6aox8e3F TTV/a39fPvURgqWJxgXpdasMKw== X-Google-Smtp-Source: AGs4zMYTIMOQ2pU0QIufDJ2NuR3YSl5MyvNfp8Mk5U6yeYjAUdFzkOiG8hXAFld5yW/kd2AmkWgM3w== X-Received: by 10.99.61.66 with SMTP id k63mr1725076pga.421.1511935678851; Tue, 28 Nov 2017 22:07:58 -0800 (PST) Received: from localhost ([203.205.141.123]) by smtp.gmail.com with ESMTPSA id b187sm1286622pfg.109.2017.11.28.22.07.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Nov 2017 22:07:58 -0800 (PST) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Wanpeng Li , Jim Mattson Subject: [PATCH] KVM: VMX: Cache IA32_DEBUGCTL in memory Date: Tue, 28 Nov 2017 22:07:53 -0800 Message-Id: <1511935673-7371-1-git-send-email-wanpeng.li@hotmail.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wanpeng Li MSR_IA32_DEBUGCTLMSR is zeroed on VMEXIT, so it is saved/restored each time during world switch. Jim from Google pointed out that when running schbench in L2, vmx_vcpu_run will occupy 4% cpu time, and the 25% of vmx_vcpu_run cpu time is occupied by get_debugctlmsr(). This patch caches the host IA32_DEBUGCTL MSR and saves/restores the host IA32_DEBUGCTL msr when guest/host switches to avoid to save/restore each time during world switch. Suggested-by: Jim Mattson Cc: Jim Mattson Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx.c | 11 +++++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 63d34bc..c904250 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -619,6 +619,7 @@ struct kvm_vcpu_arch { unsigned long dr7; unsigned long eff_db[KVM_NR_DB_REGS]; unsigned long guest_debug_dr7; + unsigned long debugctlmsr; u64 msr_platform_info; u64 msr_misc_features_enables; diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index 8c7e816..b167bba 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -2326,6 +2326,7 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) vmx_vcpu_pi_load(vcpu, cpu); vmx->host_pkru = read_pkru(); + vcpu->arch.debugctlmsr = get_debugctlmsr(); } static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) @@ -2347,6 +2348,9 @@ static void vmx_vcpu_put(struct kvm_vcpu *vcpu) vmx_vcpu_pi_put(vcpu); __vmx_load_host_state(to_vmx(vcpu)); + /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ + if (vcpu->arch.debugctlmsr) + update_debugctlmsr(vcpu->arch.debugctlmsr); } static bool emulation_required(struct kvm_vcpu *vcpu) @@ -9346,7 +9350,7 @@ static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); - unsigned long debugctlmsr, cr3, cr4; + unsigned long cr3, cr4; /* Record the guest's net vcpu time for enforced NMI injections. */ if (unlikely(!enable_vnmi && @@ -9399,7 +9403,6 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) __write_pkru(vcpu->arch.pkru); atomic_switch_perf_msrs(vmx); - debugctlmsr = get_debugctlmsr(); vmx_arm_hv_timer(vcpu); @@ -9509,10 +9512,6 @@ static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) #endif ); - /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ - if (debugctlmsr) - update_debugctlmsr(debugctlmsr); - #ifndef CONFIG_X86_64 /* * The sysexit path does not restore ds/es, so we must set them to