From patchwork Mon Apr 25 11:18:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Stabellini X-Patchwork-Id: 8925541 Return-Path: X-Original-To: patchwork-xen-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 51016BF29F for ; Mon, 25 Apr 2016 11:20:49 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7D24F20173 for ; Mon, 25 Apr 2016 11:20:48 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8A70C20172 for ; Mon, 25 Apr 2016 11:20:47 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aueXH-000258-VV; Mon, 25 Apr 2016 11:18:39 +0000 Received: from mail6.bemta6.messagelabs.com ([85.158.143.247]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aueXG-00024z-R2 for xen-devel@lists.xenproject.org; Mon, 25 Apr 2016 11:18:38 +0000 Received: from [85.158.143.35] by server-1.bemta-6.messagelabs.com id B9/1A-18833-E0DFD175; Mon, 25 Apr 2016 11:18:38 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprLIsWRWlGSWpSXmKPExsVybKJshy7vX9l wg6a5ihbft0xmcmD0OPzhCksAYxRrZl5SfkUCa0bn/v+sBW8EK3Y8u8LYwPiBr4uRi0NIYCqj xNf3v1ggnE4mid7T91m7GDk5WAS0Jd7cncEGYrMJGEr8fbIJyObgkACyl3zmAAmLCChJ3Fs1m QnEZhaIlvg+/wwziC0s4ChxYOt9RhCbV8Bbon3THrCRogK6Eof+/WGDiAtKnJz5hAWiV0ti+f RtYLaEQIbEvJ45rBC2l8SiG5egbDWJq+c2MU9g5J+FpH0WkvYFjEyrGNWLU4vKUot0jfSSijL TM0pyEzNzdA0NzPRyU4uLE9NTcxKTivWS83M3MQKDjQEIdjAu++t0iFGSg0lJlNfhl2y4EF9S fkplRmJxRnxRaU5q8SFGGQ4OJQne2b+BcoJFqempFWmZOcCwh0lLcPAoifCuB0nzFhck5hZnp kOkTjEqSonzVoIkBEASGaV5cG2wWLvEKCslzMsIdIgQT0FqUW5mCar8K0ZxDkYlYd4DIFN4Mv NK4Ka/AlrMBLT48iGwxSWJCCmpBkZN9xDxk3e3PNnYsktv1i+1XKPlDduXeH/2L9j15uEE0Rk bjNbfu3NQeeLdFv5je4Q6Gl9YzW37K3RdaXvc8pXXfsn6V3rdvpeWPuXPyT2hTstT3k0+q/Bn C3vn3r++KrM+z0iULZmz7eyTpa0HGXTb+mKZLjI+nypct5wxMd7zZ/SaDbLG4lpxSizFGYmGW sxFxYkArYwec7ACAAA= X-Env-Sender: sstabellini@kernel.org X-Msg-Ref: server-2.tower-21.messagelabs.com!1461583116!1338742!1 X-Originating-IP: [198.145.29.136] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.34; banners=-,-,- X-VirusChecked: Checked Received: (qmail 48857 invoked from network); 25 Apr 2016 11:18:37 -0000 Received: from mail.kernel.org (HELO mail.kernel.org) (198.145.29.136) by server-2.tower-21.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 25 Apr 2016 11:18:37 -0000 Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 3431F20173; Mon, 25 Apr 2016 11:18:35 +0000 (UTC) Received: from [10.0.0.5] (107.238.189.80.dyn.plus.net [80.189.238.107]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3A225201D3; Mon, 25 Apr 2016 11:18:33 +0000 (UTC) Date: Mon, 25 Apr 2016 12:18:00 +0100 (BST) From: Stefano Stabellini X-X-Sender: sstabellini@sstabellini-ThinkPad-X260 To: xen-devel@lists.xenproject.org Message-ID: User-Agent: Alpine 2.10 (DEB 1266 2009-07-14) MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, 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 Cc: andrew.cooper3@citrix.com, sstabellini@kernel.org, Jan Beulich Subject: [Xen-devel] [PATCH] xen/time: fix gtime_to_gtsc for vtsc=1 PV guests X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Jan Beulich For vtsc=1 PV guests, rdtsc is trapped and calculated from get_s_time() using gtime_to_gtsc. Similarly the tsc_timestamp, part of struct vcpu_time_info, is calculated from stime_local_stamp using gtime_to_gtsc. However gtime_to_gtsc can return 0, if time < vtsc_offset, which can actually happen when gtime_to_gtsc is called passing stime_local_stamp (the caller function is __update_vcpu_system_time). In that case the pvclock protocol doesn't work properly and the guest is unable to calculate the system time correctly. As a consequence when the guest tries to set a timer event (for example calling the VCPUOP_set_singleshot_timer hypercall), the event will be in the past causing Linux to hang. The purpose of the pvclock protocol is to allow the guest to calculate the system_time in nanosec correctly. The guest calculates as follow: from_vtsc_scale(rdtsc - vcpu_time_info.tsc_timestamp) + vcpu_time_info.system_time Given that with vtsc=1: rdtsc = to_vtsc_scale(NOW() - vtsc_offset) vcpu_time_info.tsc_timestamp = to_vtsc_scale(vcpu_time_info.system_time - vtsc_offset) The expression evaluates to NOW(), which is what we want. However when stime_local_stamp < vtsc_offset, vcpu_time_info.tsc_timestamp is actually 0. As a consequence the calculated overall system_time is not correct. This patch fixes the issue by letting gtime_to_gtsc return a negative integer in the form of a wrapped around unsigned integer, thus when the guest subtracts vcpu_time_info.tsc_timestamp from rdtsc will calculate the right value. Signed-off-by: Jan Beulich Signed-off-by: Stefano Stabellini Reviewed-by: Andrew Cooper diff --git a/xen/arch/x86/time.c b/xen/arch/x86/time.c index 687e39b..6a77a90 100644 --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -1663,7 +1663,13 @@ custom_param("tsc", tsc_parse); u64 gtime_to_gtsc(struct domain *d, u64 time) { if ( !is_hvm_domain(d) ) + { time = max_t(s64, time - d->arch.vtsc_offset, 0); + if ( time < d->arch.vtsc_offset ) + return -scale_delta(d->arch.vtsc_offset - time, + &d->arch.ns_to_vtsc); + time -= d->arch.vtsc_offset; + } return scale_delta(time, &d->arch.ns_to_vtsc); }