Received: by 10.192.165.156 with SMTP id m28csp2383489imm; Thu, 12 Apr 2018 13:23:08 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+gkzgJY00ruWKsNhAEgaggSRDnpxnfYg4WaWR4OfsPxOCNMLwa40vHHmb0Z1bSDKSpASgn X-Received: by 2002:a17:902:5a3:: with SMTP id f32-v6mr2470306plf.287.1523564588279; Thu, 12 Apr 2018 13:23:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523564588; cv=none; d=google.com; s=arc-20160816; b=GV0b1q4ZxYYZsREEM2g/Gr3H76DJHkvu7MDNVXgOtDakgk1hykaHgk9SXPvlIi9+/6 VG5FLVqhO44xlWtDSn4UkiLieDS65Id4R7omPAR+1MQR7annTSNqHg7bxpF2/Kh99on5 uwCrY0R1JWOJISQyJzHai9NX+0UO50Fzk80NuHBuzeWUuHNYaGHoXyQ7qbWHNQF0t6IM nU7EFHBZsHvJdlgqsEG2GsD2Wpj09NPmr3LHF2iPpFwOLnleNxX8C/dfmw++Oxuw37B5 njAyOHCCDXair8fIwpayZI/thliNLOvBSd0lhf3AxYOoe044OLQkdCUz3Lj1TdgfPhP7 +0Dg== 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 :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=p5vQpBM6jP7vCC/B75aH7KZh+xpmtoCDtSUSZvfCFeY=; b=xn5qBPsRlZcySx8Ph569pdKHx/UgJNKjfAaglD2K0L/CDATvbqQ50iM2Cnb4zeQK2y udrOhTIz3s3UkcW9rGBOr2p6DRZ/lViKD3BXKTAXwg8jML6mnre49ubyXEnuamOt1Dcw Xk+/DzCtJsL8kfjMQewrcwSVYAquhDi1A62/MwCj23VAHb7SXpXtfpxF6gEIEeg0vqiq MNCseNczes16IimCkn/VoqgUDgJ/TuizXJRSB6rDx3sI6QnZF9m0qQSaRwhWC/zvedcU lYy0cCiIG+ERM6fjHHzbXFDPQYN/dkXYsQTHl1i0pWvwH2ARD98Ve5I0FEyQE7HXg75+ RQ/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=MIm2Crf0; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h61-v6si3911041pld.152.2018.04.12.13.22.54; Thu, 12 Apr 2018 13:23:08 -0700 (PDT) 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=@amazon.de header.s=amazon201209 header.b=MIm2Crf0; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752696AbeDLUTY (ORCPT + 99 others); Thu, 12 Apr 2018 16:19:24 -0400 Received: from smtp-fw-4101.amazon.com ([72.21.198.25]:43546 "EHLO smtp-fw-4101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752098AbeDLUTX (ORCPT ); Thu, 12 Apr 2018 16:19:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1523564363; x=1555100363; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=p5vQpBM6jP7vCC/B75aH7KZh+xpmtoCDtSUSZvfCFeY=; b=MIm2Crf0Iu7cfH7adbX3//FH6s9yWYejueRcbX/j4UcMzP6sfQuXUJ1v u+Feszmk8KPO/PoHn3Ta2Ys7HIIHpEoLubhBt+c4TeIUv7YEGNqZnLtlc YP0WVH8s5huiGF1AlyTWMDyCNt9sSReW+AEH/V4AJDdZdYFZ78ue1VVoj c=; X-IronPort-AV: E=Sophos;i="5.48,443,1517875200"; d="scan'208";a="715754147" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1d-98acfc19.us-east-1.amazon.com) ([10.124.125.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 12 Apr 2018 20:19:21 +0000 Received: from u54e1ad5160425a4b64ea.ant.amazon.com (iad1-ws-svc-lb91-vlan3.amazon.com [10.0.103.150]) by email-inbound-relay-1d-98acfc19.us-east-1.amazon.com (8.14.7/8.14.7) with ESMTP id w3CKJEuQ025892 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 12 Apr 2018 20:19:16 GMT Received: from u54e1ad5160425a4b64ea.ant.amazon.com (localhost [127.0.0.1]) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id w3CKJCpr008997; Thu, 12 Apr 2018 22:19:13 +0200 Received: (from karahmed@localhost) by u54e1ad5160425a4b64ea.ant.amazon.com (8.15.2/8.15.2/Submit) id w3CKJCjl008996; Thu, 12 Apr 2018 22:19:12 +0200 From: KarimAllah Ahmed To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: KarimAllah Ahmed , Jim Mattson , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Subject: [PATCH v4 1/2] X86/KVM: Properly update 'tsc_offset' to represent the running guest Date: Thu, 12 Apr 2018 22:19:06 +0200 Message-Id: <1523564347-8896-1-git-send-email-karahmed@amazon.de> X-Mailer: git-send-email 2.7.4 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 Update 'tsc_offset' on vmenty/vmexit of L2 guests to ensure that it always captures the TSC_OFFSET of the running guest whether it is the L1 or L2 guest. Cc: Jim Mattson Cc: Paolo Bonzini Cc: Radim Krčmář Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Suggested-by: Paolo Bonzini Signed-off-by: KarimAllah Ahmed --- v1 -> v2: - Rewrote the patch to always update tsc_offset to represent the current guest (pbonzini@) --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx.c | 25 ++++++++++++++++++++----- arch/x86/kvm/x86.c | 9 ++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 9fa4f57..3bedfef 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1015,6 +1015,7 @@ struct kvm_x86_ops { bool (*has_wbinvd_exit)(void); + u64 (*read_l1_tsc_offset)(struct kvm_vcpu *vcpu); void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset); void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2); diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index cff2f50..9e7dd39 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -2895,6 +2895,17 @@ static u64 guest_read_tsc(struct kvm_vcpu *vcpu) return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset; } +static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) +{ + struct vmcs12 *vmcs12 = get_vmcs12(vcpu); + + if (is_guest_mode(vcpu) && + (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) + return vcpu->arch.tsc_offset - vmcs12->tsc_offset; + + return vcpu->arch.tsc_offset; +} + /* * writes 'offset' into guest's timestamp counter offset register */ @@ -11163,11 +11174,8 @@ static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); } - if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) - vmcs_write64(TSC_OFFSET, - vcpu->arch.tsc_offset + vmcs12->tsc_offset); - else - vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); + vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); + if (kvm_has_tsc_control) decache_tsc_multiplier(vmx); @@ -11469,6 +11477,9 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) if (enable_shadow_vmcs) copy_shadow_to_vmcs12(vmx); + if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) + vcpu->arch.tsc_offset += vmcs12->tsc_offset; + /* * The nested entry process starts with enforcing various prerequisites * on vmcs12 as required by the Intel SDM, and act appropriately when @@ -12015,6 +12026,9 @@ static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, leave_guest_mode(vcpu); + if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) + vcpu->arch.tsc_offset -= vmcs12->tsc_offset; + if (likely(!vmx->fail)) { if (exit_reason == -1) sync_vmcs12(vcpu, vmcs12); @@ -12688,6 +12702,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, + .read_l1_tsc_offset = vmx_read_l1_tsc_offset, .write_tsc_offset = vmx_write_tsc_offset, .set_tdp_cr3 = vmx_set_cr3, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ac42c85..3fb1353 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1532,7 +1532,14 @@ static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) { - return vcpu->arch.tsc_offset + kvm_scale_tsc(vcpu, host_tsc); + u64 tsc_offset; + + if (kvm_x86_ops->read_l1_tsc_offset) + tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu); + else + tsc_offset = vcpu->arch.tsc_offset; + + return tsc_offset + kvm_scale_tsc(vcpu, host_tsc); } EXPORT_SYMBOL_GPL(kvm_read_l1_tsc); -- 2.7.4