Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1059237pxk; Mon, 31 Aug 2020 08:43:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJjlWDAFGnYPrqf7J+NNYbgp9i+dbHfCLUCJnlQpDlaIscNXBoMQpLiTHL9e6mhvBxpuyW X-Received: by 2002:a17:906:e50:: with SMTP id q16mr1746564eji.544.1598888594539; Mon, 31 Aug 2020 08:43:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598888594; cv=none; d=google.com; s=arc-20160816; b=cZm7KkGxOyRtl7ByTzYlkWBqwoezvJ1NbRHBCRWGN5cpz2xTweGa9XSiV6glwyJOTn +aNED+LkMTR7ywB0wHqcUfJhnBziAZhEQewuEwSKcQlws3lCwzg8e0rY9JhoHbW5inb2 aEcbMmLB/22aJTGteN/DbV3/WFvvSfOHomH132FbmyWsNQ4eWA013CezBNWKslbdkyPY Wo8LlpLFEf72ujvY1CPv7Sp/Zyc2tpltP32NaMFgasQvLS6Oex331UbkdB+p1Mn9IlJY jst3mgSmLEZNRgfkY36mniTe3r5hksl3Zz8uYMF3mWZuU8I8uMczn9dJ3MBkQMPk6hJ7 IB2Q== 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; bh=M3BAuooGNbOrp9Dg0+DF+6BVVz0ONfuLuMgzsn2xSOs=; b=0bMc6uLFvG/sGC7+ZVV2Q/S1xv4Uihh9Xo5pNZOqlGhlpq6HkwjzeN/ljF/CRukzWr xVu1JiC9JuhgBeKiEUCFqUojpm4ojfXg8gm/wcg60eAcxA2J2P5USsmwMG9umbSE9lrH rgVQEVnbPtjNGNAkDzt5pSdMOxSEfXSNDWBpQooFhnRdgCmDo7ncRDUZpkK5y5ARKtJE ryy9PwqY1Cph/VvXUPYQgeZkDcUkx1cmmnW99sGf9bQP2iIFMhqc7Lzgdm2zV8KZidfJ Uz9TwvtldLYWZxP76qhZks+wX358QduA6jFl/JhNDc3EkkvdBgCBcMgHirp/s9Z9CWj2 +Tkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="o8/3CVJ6"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b3si5861391edn.98.2020.08.31.08.42.51; Mon, 31 Aug 2020 08:43:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="o8/3CVJ6"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729385AbgHaPjF (ORCPT + 99 others); Mon, 31 Aug 2020 11:39:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:39368 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728536AbgHaPaK (ORCPT ); Mon, 31 Aug 2020 11:30:10 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2AAD421531; Mon, 31 Aug 2020 15:30:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598887809; bh=hJ4CKVWnrnDzcY+E+SwF6btxG/3tO217+leRyRg10yA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o8/3CVJ6Bx/ejsxnGeYz7XitWR6UzSq6/EzNqBiZQ8I3SI86fNMU6EH/vKSZkpp/z Vv697R3J7zbwwoSEzWtdvH530r9/DnO8QkHZjAuN31Li3XA04mwpb0k8eouRqHs+lF 2o37GT/nuq6NXOUFbRAO/kKJgLCMTwTy870Soq00= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Vineeth Pillai , Michael Kelley , Wei Liu , Sasha Levin , linux-hyperv@vger.kernel.org Subject: [PATCH AUTOSEL 5.8 23/42] hv_utils: return error if host timesysnc update is stale Date: Mon, 31 Aug 2020 11:29:15 -0400 Message-Id: <20200831152934.1023912-23-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200831152934.1023912-1-sashal@kernel.org> References: <20200831152934.1023912-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Vineeth Pillai [ Upstream commit 90b125f4cd2697f949f5877df723a0b710693dd0 ] If for any reason, host timesync messages were not processed by the guest, hv_ptp_gettime() returns a stale value and the caller (clock_gettime, PTP ioctl etc) has no means to know this now. Return an error so that the caller knows about this. Signed-off-by: Vineeth Pillai Reviewed-by: Michael Kelley Link: https://lore.kernel.org/r/20200821152523.99364-1-viremana@linux.microsoft.com Signed-off-by: Wei Liu Signed-off-by: Sasha Levin --- drivers/hv/hv_util.c | 46 +++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/drivers/hv/hv_util.c b/drivers/hv/hv_util.c index 92ee0fe4c919e..1f86e8d9b018d 100644 --- a/drivers/hv/hv_util.c +++ b/drivers/hv/hv_util.c @@ -282,26 +282,52 @@ static struct { spinlock_t lock; } host_ts; -static struct timespec64 hv_get_adj_host_time(void) +static inline u64 reftime_to_ns(u64 reftime) { - struct timespec64 ts; - u64 newtime, reftime; + return (reftime - WLTIMEDELTA) * 100; +} + +/* + * Hard coded threshold for host timesync delay: 600 seconds + */ +static const u64 HOST_TIMESYNC_DELAY_THRESH = 600 * (u64)NSEC_PER_SEC; + +static int hv_get_adj_host_time(struct timespec64 *ts) +{ + u64 newtime, reftime, timediff_adj; unsigned long flags; + int ret = 0; spin_lock_irqsave(&host_ts.lock, flags); reftime = hv_read_reference_counter(); - newtime = host_ts.host_time + (reftime - host_ts.ref_time); - ts = ns_to_timespec64((newtime - WLTIMEDELTA) * 100); + + /* + * We need to let the caller know that last update from host + * is older than the max allowable threshold. clock_gettime() + * and PTP ioctl do not have a documented error that we could + * return for this specific case. Use ESTALE to report this. + */ + timediff_adj = reftime - host_ts.ref_time; + if (timediff_adj * 100 > HOST_TIMESYNC_DELAY_THRESH) { + pr_warn_once("TIMESYNC IC: Stale time stamp, %llu nsecs old\n", + (timediff_adj * 100)); + ret = -ESTALE; + } + + newtime = host_ts.host_time + timediff_adj; + *ts = ns_to_timespec64(reftime_to_ns(newtime)); spin_unlock_irqrestore(&host_ts.lock, flags); - return ts; + return ret; } static void hv_set_host_time(struct work_struct *work) { - struct timespec64 ts = hv_get_adj_host_time(); - do_settimeofday64(&ts); + struct timespec64 ts; + + if (!hv_get_adj_host_time(&ts)) + do_settimeofday64(&ts); } /* @@ -622,9 +648,7 @@ static int hv_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) static int hv_ptp_gettime(struct ptp_clock_info *info, struct timespec64 *ts) { - *ts = hv_get_adj_host_time(); - - return 0; + return hv_get_adj_host_time(ts); } static struct ptp_clock_info ptp_hyperv_info = { -- 2.25.1