From patchwork Tue Dec 15 04:08:35 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zachary Amsden X-Patchwork-Id: 67477 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id nBF4QSLq016846 for ; Tue, 15 Dec 2009 04:26:31 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932425AbZLOELd (ORCPT ); Mon, 14 Dec 2009 23:11:33 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1759242AbZLOEL1 (ORCPT ); Mon, 14 Dec 2009 23:11:27 -0500 Received: from mx1.redhat.com ([209.132.183.28]:28574 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757263AbZLOEHL (ORCPT ); Mon, 14 Dec 2009 23:07:11 -0500 Received: from int-mx08.intmail.prod.int.phx2.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id nBF47979019568 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 14 Dec 2009 23:07:09 -0500 Received: from localhost.localdomain (vpn-9-100.rdu.redhat.com [10.11.9.100]) by int-mx08.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id nBF46m92031119; Mon, 14 Dec 2009 23:07:07 -0500 From: Zachary Amsden To: kvm@vger.kernel.org Cc: Zachary Amsden , Avi Kivity , Marcelo Tosatti , Joerg Roedel , linux-kernel@vger.kernel.org, Dor Laor Subject: [PATCH RFC: kvm tsc virtualization 08/20] Export the reference TSC from KVM module Date: Mon, 14 Dec 2009 18:08:35 -1000 Message-Id: <1260850127-9766-9-git-send-email-zamsden@redhat.com> In-Reply-To: <1260850127-9766-8-git-send-email-zamsden@redhat.com> References: <1260850127-9766-1-git-send-email-zamsden@redhat.com> <1260850127-9766-2-git-send-email-zamsden@redhat.com> <1260850127-9766-3-git-send-email-zamsden@redhat.com> <1260850127-9766-4-git-send-email-zamsden@redhat.com> <1260850127-9766-5-git-send-email-zamsden@redhat.com> <1260850127-9766-6-git-send-email-zamsden@redhat.com> <1260850127-9766-7-git-send-email-zamsden@redhat.com> <1260850127-9766-8-git-send-email-zamsden@redhat.com> Organization: Frobozz Magic Timekeeping Company X-Scanned-By: MIMEDefang 2.67 on 10.5.11.21 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 144a820..170fd5d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -742,6 +742,7 @@ static DEFINE_PER_CPU(int, cpu_tsc_shift); static DEFINE_PER_CPU(s64, cpu_tsc_offset); static DEFINE_PER_CPU(u64, cpu_tsc_measure_base); static DEFINE_PER_CPU(atomic_t, cpu_tsc_synchronized); +static DEFINE_PER_CPU(int, cpu_tsc_generation); static int tsc_base_cpu = -1; static unsigned long ref_tsc_khz; @@ -813,6 +814,29 @@ static inline u64 compute_ref_tsc(int cpu) return tsc + per_cpu(cpu_tsc_offset, cpu); } +/* + * kvm_get_ref_tsc returns a reference TSC value which we attempt to keep + * stable across all CPUs, regardless of frequency shift, CPU hotplug, or + * pre-existing TSC offsets. + */ +u64 kvm_get_ref_tsc(void) +{ + int cpu, gen; + u64 tsc; + + cpu = get_cpu(); +again: + gen = per_cpu(cpu_tsc_generation, cpu); + smp_rmb(); + tsc = compute_ref_tsc(cpu); + smp_rmb(); + if (unlikely(gen != per_cpu(cpu_tsc_generation, cpu))) + goto again; + put_cpu(); + return tsc; +} +EXPORT_SYMBOL_GPL(kvm_get_ref_tsc); + #define SYNC_TRIES 64 /* @@ -929,6 +953,7 @@ static void kvm_sync_tsc(void *cpup) accumulator -= delta[i+SYNC_TRIES]; accumulator = accumulator / (SYNC_TRIES*2-12); per_cpu(cpu_tsc_offset, new_cpu) = accumulator; + ++per_cpu(cpu_tsc_generation, new_cpu); atomic_set(&per_cpu(cpu_tsc_synchronized, new_cpu), 1); pr_debug("%s: OUT, cpu = %d, cpu_tsc_offset = %lld, cpu_tsc_multiplier=%ld, cpu_tsc_shift=%d\n", __func__, raw_smp_processor_id(), per_cpu(cpu_tsc_offset, new_cpu), per_cpu(cpu_tsc_multiplier, new_cpu), per_cpu(cpu_tsc_shift, new_cpu)); }