Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3410043rdh; Mon, 27 Nov 2023 13:35:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IHDZrdEZMThcIz5QpNmiUgMTl50O49TOpEwkd8QXQe+zS0Dm/AD4278yFZGtXvKFscb36tI X-Received: by 2002:a05:6a00:21c9:b0:6cb:8ec7:ad01 with SMTP id t9-20020a056a0021c900b006cb8ec7ad01mr16380383pfj.10.1701120942936; Mon, 27 Nov 2023 13:35:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701120942; cv=none; d=google.com; s=arc-20160816; b=dFqdO6/apYHaoyJ96jg3wqdAxCc6sThYJ6v1NkMLZOT+l0ymBznxIRTA1B1eTv9awM fd6P8uGFNe0A49beb06M0w7cH4CGGUc1rIlsIiG2spF0FNTUmh01PmFUUMz/w628lHav tgQ+jePtuKR7XJE7zVKaFrMtaScd4mKa3eje8XqOQAoEUtSWLXh3nVPhwo/SB2FmjHV/ 2DoE5zyKJy+6BOs4Fkja04+DzroidI5YrvOjcNcz8bojaPtbq2Ck+/uBJzb6RgtbBPlz mTjudoBOtpUU0Una6HZA0y4oQFJL7zZ+DML09nNFAAech8Lb3xPkRUW8ETTO6lNUgeaO EW7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:to:from:dkim-signature:dkim-filter; bh=hzoqjq9Mvw6g9m/hIiSmGN1wXGQ/SBJMNB23AWgjIeg=; fh=D0dEYwF2SykGDjxCVzd6FsqwsRxMfBK56N4a2dJB8CY=; b=qi5HaI3zBy55Cvn2Jd0A9+/2D3kZ82SnWLf5cmRKckP9hNPAOCSIw0NZ72Lx0EDZl2 i3W//n2CndffxiJhNySm/yUXEnekyiGoZhXs6EHqkjZp7YWFkdzuQ+n0u9hyRX3BvyPd mjRbpdid2v5tXi4eDaH6/7foaxz3jb3Aez/KYTwf6GIbXnKEkwZG4lEr0Hp5QHSaJawo MTdGqKWrpy62EizJmtS1+wFWAeJt28T5R3ymV5jmv8s1PizHX6QVloh+RnChA8BJWwkO mYCLCCm5ST1e74oFFLkTBmraMsjqDFNtbT4x+FBhWFkPkumTjF8kH9Z/2XUgJjb/7elY BD8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=l1pMr25Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id s8-20020a056a0008c800b006b5c34a0c8csi10933326pfu.251.2023.11.27.13.35.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 13:35:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=l1pMr25Z; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 8C687809E8EA; Mon, 27 Nov 2023 13:35:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233309AbjK0VfU (ORCPT + 99 others); Mon, 27 Nov 2023 16:35:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230330AbjK0VfS (ORCPT ); Mon, 27 Nov 2023 16:35:18 -0500 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6999F1B6; Mon, 27 Nov 2023 13:35:25 -0800 (PST) Received: from pmartincic-dev0.corp.microsoft.com (unknown [131.107.159.0]) by linux.microsoft.com (Postfix) with ESMTPSA id BF60D20B74C0; Mon, 27 Nov 2023 13:35:24 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com BF60D20B74C0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1701120924; bh=hzoqjq9Mvw6g9m/hIiSmGN1wXGQ/SBJMNB23AWgjIeg=; h=From:To:Subject:Date:From; b=l1pMr25ZRg/p8Gjj6k84zE69YJqN6NdbZvA+s9oW1dw9U7T13ZNC1BbZRQ7tC/672 XcBj6z5iFz4Nr+HUTOqyNR+STZEktXDAZ76McRqJ+ugZ3LiDYd2q5EgUcOXy+ssCMn 9RS06hXZYPQwlajq+DDI95HaOM3oSpk/JeVl8d0U= From: pmartincic@linux.microsoft.com To: "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] hv_utils: Allow implicit ICTIMESYNCFLAG_SYNC Date: Mon, 27 Nov 2023 13:35:24 -0800 Message-Id: <20231127213524.52783-1-pmartincic@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,TVD_PH_BODY_ACCOUNTS_PRE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 27 Nov 2023 13:35:40 -0800 (PST) From: Peter Martincic Hyper-V hosts can omit the _SYNC flag to due a bug on resume from modern suspend. In such a case, the guest may fail to update its time-of-day to account for the period when it was suspended, and could proceed with a significantly wrong time-of-day. In such a case when the guest is significantly behind, fix it by treating a _SAMPLE the same as if _SYNC was received so that the guest time-of-day is updated. This is hidden behind param hv_utils.timesync_implicit. Signed-off-by: Peter Martincic --- drivers/hv/hv_util.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/drivers/hv/hv_util.c b/drivers/hv/hv_util.c index 42aec2c5606a..9c97c4065fe7 100644 --- a/drivers/hv/hv_util.c +++ b/drivers/hv/hv_util.c @@ -296,6 +296,11 @@ static struct { spinlock_t lock; } host_ts; +static bool timesync_implicit; + +module_param(timesync_implicit, bool, 0644); +MODULE_PARM_DESC(timesync_implicit, "If set treat SAMPLE as SYNC when clock is behind"); + static inline u64 reftime_to_ns(u64 reftime) { return (reftime - WLTIMEDELTA) * 100; @@ -344,6 +349,29 @@ static void hv_set_host_time(struct work_struct *work) do_settimeofday64(&ts); } +/* + * Due to a bug on Hyper-V hosts, the sync flag may not always be sent on resume. + * Force a sync if the guest is behind. + */ +static inline bool hv_implicit_sync(u64 host_time) +{ + struct timespec64 new_ts; + struct timespec64 threshold_ts; + + new_ts = ns_to_timespec64(reftime_to_ns(host_time)); + ktime_get_real_ts64(&threshold_ts); + + threshold_ts.tv_sec += 5; + + /* + * If guest behind the host by 5 or more seconds. + */ + if (timespec64_compare(&new_ts, &threshold_ts) >= 0) + return true; + + return false; +} + /* * Synchronize time with host after reboot, restore, etc. * @@ -384,7 +412,8 @@ static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 adj_flags) spin_unlock_irqrestore(&host_ts.lock, flags); /* Schedule work to do do_settimeofday64() */ - if (adj_flags & ICTIMESYNCFLAG_SYNC) + if ((adj_flags & ICTIMESYNCFLAG_SYNC) || + (timesync_implicit && hv_implicit_sync(host_ts.host_time))) schedule_work(&adj_time_work); } -- 2.34.1