From patchwork Wed Oct 14 13:10:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joerg Roedel X-Patchwork-Id: 7394851 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D76039F36A for ; Wed, 14 Oct 2015 13:11:35 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id EF99E206B7 for ; Wed, 14 Oct 2015 13:11:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id ECC3C2081A for ; Wed, 14 Oct 2015 13:11:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932123AbbJNNLL (ORCPT ); Wed, 14 Oct 2015 09:11:11 -0400 Received: from 8bytes.org ([81.169.241.247]:49537 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753477AbbJNNK5 (ORCPT ); Wed, 14 Oct 2015 09:10:57 -0400 Received: by theia.8bytes.org (Postfix, from userid 1000) id 150BA3C6; Wed, 14 Oct 2015 15:10:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1444828255; bh=MNsIE22FWw1GTaF9QgUA2VMb5ajKUfAEfbkJQ9UH5t0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MKAncPt9VdnJnVRtTpVIXBpjCLPbsAPcHCEAUv8gRX/oHVDfGWPMErsEf2SyfmnnB QOW1aONKXv9oskbPUq4WPqzerQB8FI9XAdMom2MQmpasU4IyHRXQWmyRAjcr99KSHb QpSz0XwFV38MbrCFlWVUvMAR6elgG+Azun+AB0qg3s2kJeI+/r9i7UajMZYZxZM3I+ iczkvCEU2YsbloGeJs4J+FO38gm6TCyYyAm5nbRf5W9qqog1i+ieW50UmiIRXbCpd6 TLePI/zRKjkZ6GLqwANSLRCvBIrBaHZ+z9436IvG8E5fqVr5Elwm/vvLb4M1dJbMOD 5wzU/BWFFTgoA== Date: Wed, 14 Oct 2015 15:10:54 +0200 From: Joerg Roedel To: Paolo Bonzini Cc: Gleb Natapov , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Joerg Roedel , Bandan Das , Dirk Mueller Subject: [PATCH v2] kvm: svm: Only propagate next_rip when guest supports it Message-ID: <20151014131054.GA5081@8bytes.org> References: <1444384276-23732-1-git-send-email-joro@8bytes.org> <5617A1BF.8080908@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <5617A1BF.8080908@redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID,T_RP_MATCHES_RCVD,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Fri, Oct 09, 2015 at 01:15:11PM +0200, Paolo Bonzini wrote: > This could be a bit expensive to do on every vmexit. Can you benchmark > it with kvm-unit-tests, or just cache the result in struct vcpu_svm? Yes, caching it is certainly a good idea. I updated the patch: From 94ee662c527683c26ea5fa98a5a8f2c798c58470 Mon Sep 17 00:00:00 2001 From: Joerg Roedel Date: Wed, 7 Oct 2015 13:38:19 +0200 Subject: [PATCH] kvm: svm: Only propagate next_rip when guest supports it Currently we always write the next_rip of the shadow vmcb to the guests vmcb when we emulate a vmexit. This could confuse the guest when its cpuid indicated no support for the next_rip feature. Fix this by only propagating next_rip if the guest actually supports it. Cc: Bandan Das Cc: Dirk Mueller Tested-By: Dirk Mueller Signed-off-by: Joerg Roedel --- arch/x86/kvm/cpuid.h | 21 +++++++++++++++++++++ arch/x86/kvm/svm.c | 11 ++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h index dd05b9c..effca1f 100644 --- a/arch/x86/kvm/cpuid.h +++ b/arch/x86/kvm/cpuid.h @@ -133,4 +133,25 @@ static inline bool guest_cpuid_has_mpx(struct kvm_vcpu *vcpu) best = kvm_find_cpuid_entry(vcpu, 7, 0); return best && (best->ebx & bit(X86_FEATURE_MPX)); } + +/* + * NRIPS is provided through cpuidfn 0x8000000a.edx bit 3 + */ +#define BIT_NRIPS 3 + +static inline bool guest_cpuid_has_nrips(struct kvm_vcpu *vcpu) +{ + struct kvm_cpuid_entry2 *best; + + best = kvm_find_cpuid_entry(vcpu, 0x8000000a, 0); + + /* + * NRIPS is a scattered cpuid feature, so we can't use + * X86_FEATURE_NRIPS here (X86_FEATURE_NRIPS would be bit + * position 8, not 3). + */ + return best && (best->edx & bit(BIT_NRIPS)); +} +#undef BIT_NRIPS + #endif diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c index 2f9ed1f..e9e3294 100644 --- a/arch/x86/kvm/svm.c +++ b/arch/x86/kvm/svm.c @@ -159,6 +159,9 @@ struct vcpu_svm { u32 apf_reason; u64 tsc_ratio; + + /* cached guest cpuid flags for faster access */ + bool nrips_enabled : 1; }; static DEFINE_PER_CPU(u64, current_tsc_ratio); @@ -2365,7 +2368,9 @@ static int nested_svm_vmexit(struct vcpu_svm *svm) nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info; nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err; - nested_vmcb->control.next_rip = vmcb->control.next_rip; + + if (svm->nrips_enabled) + nested_vmcb->control.next_rip = vmcb->control.next_rip; /* * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have @@ -4098,6 +4103,10 @@ static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) static void svm_cpuid_update(struct kvm_vcpu *vcpu) { + struct vcpu_svm *svm = to_svm(vcpu); + + /* Update nrips enabled cache */ + svm->nrips_enabled = !!guest_cpuid_has_nrips(&svm->vcpu); } static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)