Received: by 10.223.176.5 with SMTP id f5csp405612wra; Thu, 1 Feb 2018 23:14:28 -0800 (PST) X-Google-Smtp-Source: AH8x227dlbmSQTLN9Sb5Qv7TSQgWssz6H4yygDWcGyOkDvcDEusvzqgjYyn5/ngBNBGiXhUfZ+/P X-Received: by 10.98.152.11 with SMTP id q11mr1189189pfd.131.1517555668372; Thu, 01 Feb 2018 23:14:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517555668; cv=none; d=google.com; s=arc-20160816; b=hQV5GLCl8254Je3th7JL3rj1ou/uyy1og2ww/hamvEFS1eChtFZAP9Ykj9ynHt44Le jRIgo6sLa7fcHvS0UJyOcQ1br4//9Oj4DuNJwR1XZ9izCN/gBO2gwcqGCPYQqvGarI1t FHsrin1ev8hC73XOz6N6gNq9YUWo1R7KDIPLRWkpXqCvxAoNyU4wrvAtel4uUu+F0uBE mTWsvUMNBPjyODaSCx4lNb+Yj4h/o8hVhWt6x9PIL75hFulJQvmaaST1Fhpq3LWjl7P2 wuARjYmgNG1l9xcqs4fOIDr98GctkuU7ZqTUi+hHXPiZGauo4tR1iziLSqSSiDooNehw svWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=NxCD+795F1DqCXD0o/9ofQqwUa+YyJGrq268FncvQjU=; b=EFbwMfycFaF3mhaZHOSccXvv+lUdHd48GRHauGXzXKMDJ2HbiTRBg7vr30pWMfM3tS twFvISTS+Fm6kNfSMz3AwKqJFl5yAwqNVCqVxYk40KHPTrqn4FbJ13aO5nOtsZooIBAT 20JsGVtb5hW/VsdlPyHZ7FBxxVpQH1M0bY6mbOK+WYdUPOB1uvsIjeuBJAL9w6pqR5PH 6xgWSGo+4azGag1n0m/wegXTj09DPj1RdsiW9NX+ilLfW6dFGbUvNnXpTFv77AteVCI6 FgUqLUcy0cZYb1Uycc0K3uruF6o3PQi8VeRDb0yC7VD9AgUxMcMUcENF6dUTECfMsVj0 1big== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Ve4dBykC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j7-v6si1237207plk.553.2018.02.01.23.14.13; Thu, 01 Feb 2018 23:14:28 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Ve4dBykC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751827AbeBBHMi (ORCPT + 99 others); Fri, 2 Feb 2018 02:12:38 -0500 Received: from mail-pl0-f65.google.com ([209.85.160.65]:32874 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751581AbeBBHMI (ORCPT ); Fri, 2 Feb 2018 02:12:08 -0500 Received: by mail-pl0-f65.google.com with SMTP id t4so5507646plo.0; Thu, 01 Feb 2018 23:12:07 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=NxCD+795F1DqCXD0o/9ofQqwUa+YyJGrq268FncvQjU=; b=Ve4dBykCWw6EXvNGJnKFFoDOkCA1QBFaZoUMRImKL9qkr9PzEKClCiUuZrcz0PLz5M zbJISyZmpU18VF4nhM9+NBtQBviHR2vSiOl2PkwkNNMeuv1xfHYae6cLuFKjVjkYHafT 5stpme/y0JM7uZq29DgpmSar17OaRMa8Lxi5W7eY2gcQU3CYfE0yKyLms4DEtxRUiUTi w+6iJog4lDq6mJ3Ovb0ixqgv0jkoW1GMK54qk/Iqo9vepKKMHdJpn7QczrI34z4pBuZg liqdwHVq+4kx8IrjpeCKCnOTSSDqQc9yMivsAKnQZbM+Q0ff74IEqbqh+M0RqMRxgCe8 CYYQ== 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:mime-version:content-transfer-encoding; bh=NxCD+795F1DqCXD0o/9ofQqwUa+YyJGrq268FncvQjU=; b=j3i/tXmuQQOG012djPejNf5Rh4VYZ0o7+fYL9BLAA15E8ClVqI1cWFllNStAx+jVrN mqqUooOEkNYABLEKs0flczFEWr9USR8uFKrw7KaektPK8pneiFqp355E0u9Zka8G5eUb tbx1BRWUHZ6yCn4ua5MSzrdrYoE8vjJHheU8AvcLsrUPuKjn7gYEWj3uPNbwJ0XcBL9J OjiwPqajg3gcdSF/qhgoqyo68b2DIAwnJLp/K4G6bgZadbcxvMIxsDuyYWe9BDciUrm1 72e5h4inLuRpyDtdzQT/v4QoEb4cdhQ6j9eMj++7M7IZpbtwNNrbrwqoBLf/avJqTTk2 Atiw== X-Gm-Message-State: AKwxytf7HaZU9uYbLjAdP77pTh0bhuDglP3MgxFlfuskm+NiNLGSU7mw qwFy6q+67unPsTmTB7tjQfaHsg== X-Received: by 2002:a17:902:6e8c:: with SMTP id v12-v6mr34967747plk.14.1517555527233; Thu, 01 Feb 2018 23:12:07 -0800 (PST) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id z5sm2297389pfi.81.2018.02.01.23.12.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 01 Feb 2018 23:12:06 -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?= Subject: [PATCH 2/2] KVM: X86: Add per-VM no-HLT-exiting capability Date: Thu, 1 Feb 2018 23:11:13 -0800 Message-Id: <1517555473-8313-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1517555473-8313-1-git-send-email-wanpengli@tencent.com> References: <1517555473-8313-1-git-send-email-wanpengli@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wanpeng Li If host CPUs are dedicated to a VM, we can avoid VM exits on HLT. This patch adds the per-VM non-HLT-exiting capability. Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- Documentation/virtual/kvm/api.txt | 11 +++++++++++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/vmx.c | 21 +++++++++++++++++++++ arch/x86/kvm/x86.c | 5 +++++ arch/x86/kvm/x86.h | 5 +++++ include/uapi/linux/kvm.h | 1 + 6 files changed, 45 insertions(+) diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt index e5f1743..573a3e5 100644 --- a/Documentation/virtual/kvm/api.txt +++ b/Documentation/virtual/kvm/api.txt @@ -4222,6 +4222,17 @@ enables QEMU to build error log and branch to guest kernel registered machine check handling routine. Without this capability KVM will branch to guests' 0x200 interrupt vector. +7.13 KVM_CAP_X86_GUEST_HLT + +Architectures: x86 +Parameters: none +Returns: 0 on success + +This capability indicates that a guest using HLT to stop a virtual CPU +will not cause a VM exit. As such, time spent while a virtual CPU is +halted in this way will then be accounted for as guest running time on +the host, KVM_FEATURE_PV_UNHALT should be disabled. + 8. Other capabilities. ---------------------- diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index dd6f57a..c566ea0 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -804,6 +804,8 @@ struct kvm_arch { gpa_t wall_clock; + bool hlt_in_guest; + bool ept_identity_pagetable_done; gpa_t ept_identity_map_addr; diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index b1e554a..6cfd8d3 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -2478,6 +2478,19 @@ static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit return 0; } +static void vmx_clear_hlt(struct kvm_vcpu *vcpu) +{ + /* + * Ensure that we clear the HLT state in the VMCS. We don't need to + * explicitly skip the instruction because if the HLT state is set, + * then the instruction is already executing and RIP has already been + * advanced. + */ + if (kvm_hlt_in_guest(vcpu->kvm) && + vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) + vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); +} + static void vmx_queue_exception(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); @@ -2508,6 +2521,8 @@ static void vmx_queue_exception(struct kvm_vcpu *vcpu) intr_info |= INTR_TYPE_HARD_EXCEPTION; vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); + + vmx_clear_hlt(vcpu); } static bool vmx_rdtscp_supported(void) @@ -5301,6 +5316,8 @@ static u32 vmx_exec_control(struct vcpu_vmx *vmx) exec_control |= CPU_BASED_CR3_STORE_EXITING | CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_INVLPG_EXITING; + if (kvm_hlt_in_guest(vmx->vcpu.kvm)) + exec_control &= ~CPU_BASED_HLT_EXITING; return exec_control; } @@ -5729,6 +5746,8 @@ static void vmx_inject_irq(struct kvm_vcpu *vcpu) } else intr |= INTR_TYPE_EXT_INTR; vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); + + vmx_clear_hlt(vcpu); } static void vmx_inject_nmi(struct kvm_vcpu *vcpu) @@ -5759,6 +5778,8 @@ static void vmx_inject_nmi(struct kvm_vcpu *vcpu) vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); + + vmx_clear_hlt(vcpu); } static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c13cd14..a508247 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2740,6 +2740,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_SET_BOOT_CPU_ID: case KVM_CAP_SPLIT_IRQCHIP: case KVM_CAP_IMMEDIATE_EXIT: + case KVM_CAP_X86_GUEST_HLT: r = 1; break; case KVM_CAP_ADJUST_CLOCK: @@ -4061,6 +4062,10 @@ static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r = 0; break; + case KVM_CAP_X86_GUEST_HLT: + kvm->arch.hlt_in_guest = cap->args[0]; + r = 0; + break; default: r = -EINVAL; break; diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index b91215d..96fe84e 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -270,4 +270,9 @@ static inline bool kvm_mwait_in_guest(void) !boot_cpu_has_bug(X86_BUG_MONITOR); } +static inline bool kvm_hlt_in_guest(struct kvm *kvm) +{ + return kvm->arch.hlt_in_guest; +} + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 62c564d..6cbf080 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -932,6 +932,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_HYPERV_SYNIC2 148 #define KVM_CAP_HYPERV_VP_INDEX 149 #define KVM_CAP_S390_AIS_MIGRATION 150 +#define KVM_CAP_X86_GUEST_HLT 151 #ifdef KVM_CAP_IRQ_ROUTING -- 2.7.4