Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3612231imm; Mon, 4 Jun 2018 06:36:41 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKatCvLJX4rblcrspCPJ9yrHoM88WcBlnEKcRpmcVAcaloWSZ90f+8fBJeQ7hRB6CYOki7K X-Received: by 2002:a17:902:ac89:: with SMTP id h9-v6mr5597434plr.311.1528119401360; Mon, 04 Jun 2018 06:36:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528119401; cv=none; d=google.com; s=arc-20160816; b=OIrbx9ucZunoBrWvI0WipkJYD42BXxadnsUxJEgT/3AMAzXVrJFQ7Ic31c/oay6ke0 n4Ly5Z17LMImUVOVGFqDTH1WYZPwpo24FJwglPkpEmsfh/iNP8pqQDrVQTJQIl/YSFx2 xyuAjIjV/MRlqg8LYvaOs9kgHtVC+AqIFvxYjTNrcy/aQ9CI41J9Rzrta63/pcccqvRR nh1yVV04DUxPO0aqqdQD/HMIml+DX8MvaSSypfNW+c30HOuR1hdQE5cOxIh2Leg9Zp7/ PTgy5nEug07W4mfQxfztnrZpS0O4P/eJZsWXmOY7p1ehAVPf38lLTULpOsFJe4a2jVML wHzg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=3RVYlZxniv3y0JJQrHDujjbqzcD4bWxrzr7llfoQero=; b=Pvyj+7eTZJ5maVUDZeqcl+EO8gorBRoKXZjPg0McjP8kCws2nYhhHDjqxXjJ4RXJjI dmAcI+KaHJghjh33/5/30kw+Ncu/9Jm2ZB9V2FnjgKeV9WQgLxmn6sH+9/CYjOCbJixZ akcLHX23tzGPmOUYLJbow9l234GZ3QWD/z9aJgu+urzls+96WHcacL6mGDZoKMW1HwKf gcg1+Nk5NlqV3Gt8wc9v9yM/pSXsu4ApLpG74ufzUt4ZXJSMoHFt6oMbhZJqdRfW+ses ytR9FZXRbWkkJwllTvBIHn9BabuJIVkIeylhtwuYmFc7ZXq7WbK3sHVaJRDRoTjpWeFa fmWA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t69-v6si20218879pgd.55.2018.06.04.06.36.26; Mon, 04 Jun 2018 06:36:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753243AbeFDNeZ (ORCPT + 99 others); Mon, 4 Jun 2018 09:34:25 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:45314 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753036AbeFDNeY (ORCPT ); Mon, 4 Jun 2018 09:34:24 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C222D8424B; Mon, 4 Jun 2018 13:34:23 +0000 (UTC) Received: from holly.tpb.lab.eng.brq.redhat.com (unknown [10.43.134.11]) by smtp.corp.redhat.com (Postfix) with ESMTP id BF44920357CA; Mon, 4 Jun 2018 13:34:22 +0000 (UTC) From: Miroslav Lichvar To: linux-kernel@vger.kernel.org Cc: Miroslav Lichvar , Thomas Gleixner , John Stultz , Richard Cochran , Prarit Bhargava Subject: [PATCHv2] timekeeping: Update multiplier when NTP frequency is set directly Date: Mon, 4 Jun 2018 15:34:21 +0200 Message-Id: <20180604133421.19559-1-mlichvar@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Mon, 04 Jun 2018 13:34:23 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Mon, 04 Jun 2018 13:34:23 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'mlichvar@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When the NTP frequency is set directly from userspace using the ADJ_FREQUENCY or ADJ_TICK timex mode, immediately update the timekeeper's multiplier instead of waiting for the next tick. This removes a hidden non-deterministic delay in setting of the frequency and allows an extremely tight control of the system clock with update rates close to or even exceeding the kernel HZ. The update is limited to archs using modern timekeeping (!ARCH_USES_GETTIMEOFFSET). Cc: Thomas Gleixner Cc: John Stultz Cc: Richard Cochran Cc: Prarit Bhargava Signed-off-by: Miroslav Lichvar --- Notes: RFC->v1: - added a new parameter to force the update of the timekeeper to the current NTP tick length only from adjtimex() - added timekeeping_advance() to keep the parameter local to timekeeping.c v1->v2: - replaced bool parameter with enum - changed timekeeping_advance() to not make any updates for TK_ADV_FREQ with !ARCH_USES_GETTIMEOFFSET (an alternative might be to update with zero offset) kernel/time/timekeeping.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 49cbceef5deb..a08ef3771ab5 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -34,6 +34,14 @@ #define TK_MIRROR (1 << 1) #define TK_CLOCK_WAS_SET (1 << 2) +enum timekeeping_adv_mode { + /* Update timekeeper when a tick has passed */ + TK_ADV_TICK, + + /* Update timekeeper on a direct frequency change */ + TK_ADV_FREQ +}; + /* * The most important data for readout fits into a single 64 byte * cache line. @@ -2021,11 +2029,11 @@ static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset, return offset; } -/** - * update_wall_time - Uses the current clocksource to increment the wall time - * +/* + * timekeeping_advance - Updates the timekeeper to the current time and + * current NTP tick length */ -void update_wall_time(void) +static void timekeeping_advance(enum timekeeping_adv_mode mode) { struct timekeeper *real_tk = &tk_core.timekeeper; struct timekeeper *tk = &shadow_timekeeper; @@ -2042,14 +2050,17 @@ void update_wall_time(void) #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET offset = real_tk->cycle_interval; + + if (mode != TK_ADV_TICK) + goto out; #else offset = clocksource_delta(tk_clock_read(&tk->tkr_mono), tk->tkr_mono.cycle_last, tk->tkr_mono.mask); -#endif /* Check if there's really nothing to do */ - if (offset < real_tk->cycle_interval) + if (offset < real_tk->cycle_interval && mode == TK_ADV_TICK) goto out; +#endif /* Do some additional sanity checking */ timekeeping_check_update(tk, offset); @@ -2105,6 +2116,15 @@ void update_wall_time(void) clock_was_set_delayed(); } +/** + * update_wall_time - Uses the current clocksource to increment the wall time + * + */ +void update_wall_time(void) +{ + timekeeping_advance(TK_ADV_TICK); +} + /** * getboottime64 - Return the real time of system boot. * @ts: pointer to the timespec64 to be set @@ -2332,6 +2352,10 @@ int do_adjtimex(struct timex *txc) write_seqcount_end(&tk_core.seq); raw_spin_unlock_irqrestore(&timekeeper_lock, flags); + /* Update the multiplier immediately if frequency was set directly */ + if (txc->modes & (ADJ_FREQUENCY | ADJ_TICK)) + timekeeping_advance(TK_ADV_FREQ); + if (tai != orig_tai) clock_was_set(); -- 2.14.3