Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp86756pxu; Tue, 1 Dec 2020 06:45:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJx7BLx7VD/ibhJPT3WgMXQSnNwBDD/hh2gJv8TEyzEbRgiv2+keBgU/SuadVdzUR7qXKlUQ X-Received: by 2002:a17:906:e086:: with SMTP id gh6mr3172238ejb.95.1606833919885; Tue, 01 Dec 2020 06:45:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606833919; cv=none; d=google.com; s=arc-20160816; b=eoHs1zQ0fnSwmYmmVXtSZvQ+fb6YK7QJGSOMmBEA6ZK6vgycKDfcLkZ80JqB/l+TuQ NFIrykxpQfTTg7FDyNj4gxxk31ra2GfQesGAJG0rojQBIDTFQcNQY7VxP2OdUFYV1n40 9usC8vDSkvaanY/zZrzJB0ssWVwgrJFeESuxMkDgjUbBiVxwqen5HZPZEC329wkMDZzT lozEQ+CXA/Tzb1fM6+lGPL6qTb0Osdv0QaL/JKFGO0B5ZGrno7YTY4/8Yq9EsB/vX5/k zKHA+/FURE6H3O2jKngTwsQhC6NG2DruB6iqFLoSBRFvUsp+5uNTSdAbs7HoFK8s6MFR 6vng== 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:cc:to:from:dkim-signature; bh=Ld3EJGeffzGbwS28b30MuPmMHEkVkjE+LANi/PtEf7w=; b=U+fwZhW7kowVtxUnlA6DNyHQq3/VyAoBGYvggBxmWNlLk2gWeb8daZAzkxuMg9lu1g vpKjGSne3UVauxc/6WHpz2ahTF7AD59v1vAr5tdMVlPHPt0IOQT4AzmWYpOMQISlCkjp 5RPmiDr6zNAUoos9+YEjD5vRgAx7sY33myIopX1/AjEwp5Jp0VC9TMMdrOYSM8t8RDfP kRemR6dIz02/46B4r42D7HcbJkqgv/x89nuJxsg66eT/ucTuEkMO4RzQxO0RaHS9XBKn OhevKCvFllrYkywIjSD7HLa6SLCnExH7gwPHDBDi8Za7rvtf7kITF2onUMi6xoN58MCS wN3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=R+msJP2x; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a15si13639eju.565.2020.12.01.06.44.56; Tue, 01 Dec 2020 06:45:19 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=R+msJP2x; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391636AbgLAOkV (ORCPT + 99 others); Tue, 1 Dec 2020 09:40:21 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:42762 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389462AbgLAOkV (ORCPT ); Tue, 1 Dec 2020 09:40:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606833535; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Ld3EJGeffzGbwS28b30MuPmMHEkVkjE+LANi/PtEf7w=; b=R+msJP2xtHNpFn/o8rczq4VmIeOGAc4dDcwcWGvAEpI9s6YR5xHDEzxcZ6s2mxk/+1gWei pwZtCw3Igc83TOPkFPUF2MXqvAhr7QIzgph/LctmNzOJ/EzsRrxvSP/eJWpjI6xb3hESdB A0BqHSO0awRQHAHDPwzzThGhQWWJwww= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-228-kdQZuW_kOi2c4cBB98Hy5Q-1; Tue, 01 Dec 2020 09:38:51 -0500 X-MC-Unique: kdQZuW_kOi2c4cBB98Hy5Q-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6E352CF984; Tue, 1 Dec 2020 14:38:38 +0000 (UTC) Received: from holly.tpb.lab.eng.brq.redhat.com (holly.tpb.lab.eng.brq.redhat.com [10.43.134.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id DAC9C6EF57; Tue, 1 Dec 2020 14:38:36 +0000 (UTC) From: Miroslav Lichvar To: linux-kernel@vger.kernel.org Cc: Miroslav Lichvar , Thomas Gleixner , John Stultz , Prarit Bhargava , Jason Gunthorpe Subject: [PATCH] rtc: adapt allowed RTC update error Date: Tue, 1 Dec 2020 15:38:35 +0100 Message-Id: <20201201143835.2054508-1-mlichvar@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When the system clock is marked as synchronized via adjtimex(), the kernel is expected to copy the system time to the RTC every 11 minutes. There are reports that it doesn't always work reliably. It seems the current requirement for the RTC update to happen within 5 ticks of the target time in some cases can consistently fail for hours or even days. It is better to set the RTC with a larger error than let it drift for too long. Add a static variable to rtc_tv_nsec_ok() to count the checks. With each failed check, relax the requirement by one jiffie, and reset the counter when it finally succeeds. This should allow the RTC update to happen in a minute at most. Signed-off-by: Miroslav Lichvar Cc: Thomas Gleixner Cc: John Stultz Cc: Prarit Bhargava Cc: Jason Gunthorpe --- include/linux/rtc.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/include/linux/rtc.h b/include/linux/rtc.h index 22d1575e4991..8d105f10ef6a 100644 --- a/include/linux/rtc.h +++ b/include/linux/rtc.h @@ -218,21 +218,30 @@ static inline bool rtc_tv_nsec_ok(s64 set_offset_nsec, struct timespec64 *to_set, const struct timespec64 *now) { - /* Allowed error in tv_nsec, arbitarily set to 5 jiffies in ns. */ - const unsigned long TIME_SET_NSEC_FUZZ = TICK_NSEC * 5; struct timespec64 delay = {.tv_sec = 0, .tv_nsec = set_offset_nsec}; + unsigned long time_set_nsec_fuzz; + static unsigned int attempt; *to_set = timespec64_add(*now, delay); - if (to_set->tv_nsec < TIME_SET_NSEC_FUZZ) { + /* + * Determine allowed error in tv_nsec. Start at 5 jiffies and add a + * jiffie with each failed attempt to make sure the RTC will be set at + * some point, even if the update cannot be scheduled very accurately. + */ + time_set_nsec_fuzz = (5 + attempt++) * TICK_NSEC; + + if (to_set->tv_nsec < time_set_nsec_fuzz) { to_set->tv_nsec = 0; + attempt = 0; return true; } - if (to_set->tv_nsec > NSEC_PER_SEC - TIME_SET_NSEC_FUZZ) { + if (to_set->tv_nsec > NSEC_PER_SEC - time_set_nsec_fuzz) { to_set->tv_sec++; to_set->tv_nsec = 0; + attempt = 0; return true; } return false; -- 2.26.2