Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933619AbcLTU5g (ORCPT ); Tue, 20 Dec 2016 15:57:36 -0500 Received: from mail-bn3nam01on0136.outbound.protection.outlook.com ([104.47.33.136]:15862 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1765704AbcLTU5b (ORCPT ); Tue, 20 Dec 2016 15:57:31 -0500 From: KY Srinivasan To: Roman Kagan , Paolo Bonzini , =?iso-8859-2?Q?Radim_Kr=E8m=E1=F8?= , Vitaly Kuznetsov CC: Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , "x86@kernel.org" , "Haiyang Zhang" , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "devel@linuxdriverproject.org" , "Denis V . Lunev" Subject: RE: [PATCH 01/15] hyperv: consolidate TSC ref page definitions Thread-Topic: [PATCH 01/15] hyperv: consolidate TSC ref page definitions Thread-Index: AQHSWtmqIXo9+qu6+UqeQYAUiq0OHaERUDwg Date: Tue, 20 Dec 2016 20:57:28 +0000 Message-ID: References: <20161220155602.6298-1-rkagan@virtuozzo.com> <20161220155602.6298-2-rkagan@virtuozzo.com> In-Reply-To: <20161220155602.6298-2-rkagan@virtuozzo.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=kys@microsoft.com; x-originating-ip: [2601:600:8c00:1040:4157:63bb:4160:c267] x-ms-office365-filtering-correlation-id: 9e4858e6-f39b-40a6-e57a-08d4291acf4e x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001);SRVR:BN3PR03MB1415; x-microsoft-exchange-diagnostics: 1;BN3PR03MB1415;7:D31DFYaNw5ODsJ5UlT9RLyxc61UCcQjxj/ZGhT1a/ALzvb+9mdGUUkrYV1wn78yRwJNjFBESloWkSuC17yEVR7d2eBwMJod+lUKXD5+1Rerj/WZSBs/rLr0wjEjxx9qEkLIdUqES2lW77KiRgnJuqVxNT00Do8t2spt/bQD4u/giJZGAuPKOWLlfi2e+X+VwU8PlEWYXTbeMStnFWvFZrW19SAlXKzRm08JvPo9o82xX5nPPGciJOTKuS5wXJqhON5HMor72utWwTrtAmfvIFohNutyiJzf5YCruTuDAFtUWEGkS25EEGU64oLyUrTSuXz23zwKBMmGCjuV7J0YIsbwLI0/BEgdmEsy3CItdaB3Mgkff5KD7mRluLQhQzobOrWJfK+QttJ69o1tDFW2BLg8j6dnaTVnEZxTb5YMmyV6uSqJ8Ox+opSZpz7xN5ksaTtHzjq2cUs+G6eNwmlo/Rk8mrz152xMVzsyCx+iTygo= x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(9452136761055); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(61425038)(6040375)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(6055026)(61426038)(61427038)(6041248)(20161123560025)(20161123564025)(20161123562025)(20161123555025)(6072148)(6047074);SRVR:BN3PR03MB1415;BCL:0;PCL:0;RULEID:;SRVR:BN3PR03MB1415; x-forefront-prvs: 0162ACCC24 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(6009001)(7916002)(39860400002)(39840400002)(39450400003)(39850400002)(39410400002)(13464003)(377454003)(189002)(199003)(2950100002)(76176999)(189998001)(101416001)(7736002)(50986999)(54356999)(3660700001)(305945005)(7696004)(10090500001)(3280700002)(92566002)(8676002)(81156014)(81166006)(97736004)(2906002)(8936002)(33656002)(5001770100001)(25786008)(86612001)(5005710100001)(68736007)(76576001)(6506006)(6116002)(10290500002)(74316002)(102836003)(122556002)(6436002)(105586002)(7416002)(106356001)(9686002)(2900100001)(229853002)(8990500004)(77096006)(5660300001)(86362001)(106116001)(38730400001)(99286002)(4326007);DIR:OUT;SFP:1102;SCL:1;SRVR:BN3PR03MB1415;H:DM5PR03MB2490.namprd03.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-2" MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-originalarrivaltime: 20 Dec 2016 20:57:28.8363 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN3PR03MB1415 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by mail.home.local id uBKKvflZ029819 Content-Length: 5499 Lines: 182 > -----Original Message----- > From: Roman Kagan [mailto:rkagan@virtuozzo.com] > Sent: Tuesday, December 20, 2016 7:56 AM > To: Paolo Bonzini ; Radim Kr?m?? > ; KY Srinivasan ; Vitaly > Kuznetsov > Cc: Thomas Gleixner ; Ingo Molnar > ; H. Peter Anvin ; x86@kernel.org; > Haiyang Zhang ; kvm@vger.kernel.org; linux- > kernel@vger.kernel.org; devel@linuxdriverproject.org; Denis V . Lunev > ; Roman Kagan > Subject: [PATCH 01/15] hyperv: consolidate TSC ref page definitions > > Consolidate the guest-side and kvm-side definitions for Hyper-V TSC > reference page. > > While at this, rewrite read_hv_clock_tsc using the existing helpers. Why not beak this into separate patches. > > Signed-off-by: Roman Kagan > --- > arch/x86/include/asm/kvm_host.h | 2 +- > arch/x86/include/uapi/asm/hyperv.h | 4 +-- > drivers/hv/hyperv_vmbus.h | 8 ------ > arch/x86/kvm/hyperv.c | 4 +-- > drivers/hv/hv.c | 54 +++++++++++++++----------------------- > 5 files changed, 26 insertions(+), 46 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h > b/arch/x86/include/asm/kvm_host.h > index 2e25038..2b85f49 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -713,7 +713,7 @@ struct kvm_hv { > u64 hv_crash_param[HV_X64_MSR_CRASH_PARAMS]; > u64 hv_crash_ctl; > > - HV_REFERENCE_TSC_PAGE tsc_ref; > + struct hv_ref_tsc_page tsc_ref; > }; > > struct kvm_arch { > diff --git a/arch/x86/include/uapi/asm/hyperv.h > b/arch/x86/include/uapi/asm/hyperv.h > index 9b1a918..6098ab5 100644 > --- a/arch/x86/include/uapi/asm/hyperv.h > +++ b/arch/x86/include/uapi/asm/hyperv.h > @@ -252,12 +252,12 @@ > #define HV_STATUS_INVALID_CONNECTION_ID 18 > #define HV_STATUS_INSUFFICIENT_BUFFERS 19 > > -typedef struct _HV_REFERENCE_TSC_PAGE { > +struct hv_ref_tsc_page { > __u32 tsc_sequence; > __u32 res1; > __u64 tsc_scale; > __s64 tsc_offset; > -} HV_REFERENCE_TSC_PAGE, *PHV_REFERENCE_TSC_PAGE; > +}; > > /* Define the number of synthetic interrupt sources. */ > #define HV_SYNIC_SINT_COUNT (16) > diff --git a/drivers/hv/hyperv_vmbus.h b/drivers/hv/hyperv_vmbus.h > index 0675b39..4516498 100644 > --- a/drivers/hv/hyperv_vmbus.h > +++ b/drivers/hv/hyperv_vmbus.h > @@ -475,14 +475,6 @@ struct hv_context { > > extern struct hv_context hv_context; > > -struct ms_hyperv_tsc_page { > - volatile u32 tsc_sequence; > - u32 reserved1; > - volatile u64 tsc_scale; > - volatile s64 tsc_offset; > - u64 reserved2[509]; > -}; > - > struct hv_ring_buffer_debug_info { > u32 current_interrupt_mask; > u32 current_read_index; > diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c > index 1572c35..c7db112 100644 > --- a/arch/x86/kvm/hyperv.c > +++ b/arch/x86/kvm/hyperv.c > @@ -806,7 +806,7 @@ static int kvm_hv_msr_set_crash_data(struct > kvm_vcpu *vcpu, > * These two equivalencies are implemented in this function. > */ > static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info > *hv_clock, > - HV_REFERENCE_TSC_PAGE *tsc_ref) > + struct hv_ref_tsc_page *tsc_ref) > { > u64 max_mul; > > @@ -847,7 +847,7 @@ void kvm_hv_setup_tsc_page(struct kvm *kvm, > u64 gfn; > > BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv- > >tsc_ref.tsc_sequence)); > - BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, > tsc_sequence) != 0); > + BUILD_BUG_ON(offsetof(struct hv_ref_tsc_page, tsc_sequence) != > 0); > > if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)) > return; > diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c > index 446802a..a7256ec 100644 > --- a/drivers/hv/hv.c > +++ b/drivers/hv/hv.c > @@ -137,41 +137,29 @@ EXPORT_SYMBOL_GPL(hv_do_hypercall); > #ifdef CONFIG_X86_64 > static cycle_t read_hv_clock_tsc(struct clocksource *arg) > { > - cycle_t current_tick; > - struct ms_hyperv_tsc_page *tsc_pg = hv_context.tsc_page; > + struct hv_ref_tsc_page *tsc_pg = hv_context.tsc_page; > + u32 sequence; > + u64 scale; > + s64 offset; > + > + do { > + sequence = tsc_pg->tsc_sequence; > + virt_rmb(); > + > + if (!sequence) { > + /* fallback to MSR */ > + cycle_t current_tick; > + rdmsrl(HV_X64_MSR_TIME_REF_COUNT, > current_tick); > + return current_tick; > + } > > - if (tsc_pg->tsc_sequence != 0) { > - /* > - * Use the tsc page to compute the value. > - */ > + scale = tsc_pg->tsc_scale; > + offset = tsc_pg->tsc_offset; > > - while (1) { > - cycle_t tmp; > - u32 sequence = tsc_pg->tsc_sequence; > - u64 cur_tsc; > - u64 scale = tsc_pg->tsc_scale; > - s64 offset = tsc_pg->tsc_offset; > - > - rdtscll(cur_tsc); > - /* current_tick = ((cur_tsc *scale) >> 64) + offset */ > - asm("mulq %3" > - : "=d" (current_tick), "=a" (tmp) > - : "a" (cur_tsc), "r" (scale)); > - > - current_tick += offset; > - if (tsc_pg->tsc_sequence == sequence) > - return current_tick; > - > - if (tsc_pg->tsc_sequence != 0) > - continue; > - /* > - * Fallback using MSR method. > - */ > - break; > - } > - } > - rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick); > - return current_tick; > + virt_rmb(); > + } while (tsc_pg->tsc_sequence != sequence); > + > + return mul_u64_u64_shr(rdtsc_ordered(), scale, 64) + offset; > } > > static struct clocksource hyperv_cs_tsc = { > -- > 2.9.3