Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp4037305imm; Mon, 18 Jun 2018 08:09:52 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIWXlKjnDt9xiBeAp8G+SssMhioe8a0F5pgXAm8/5CAZmKVQOXJiN2q3gIYzHLMMNx5tzw1 X-Received: by 2002:a62:5788:: with SMTP id i8-v6mr13776496pfj.175.1529334592329; Mon, 18 Jun 2018 08:09:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529334592; cv=none; d=google.com; s=arc-20160816; b=pFrgtRGWowiYy8T3xFXS0/+UIyYlQRAbbymvctexpn/godqLLPPwergzl3pKitIPH7 ouKobN6wdpIc7cHSG3vExxzvo5LQJ/plxPrSqfVKUphy8Ir00WhZUePDJux5PDXUzgEh cafyP7+UCekongTgvGOO8z8EwuMBFbN+o9Ipxx2UmE3GP0vb7MfvA+u46QjDA8wV6hUI tK6AnwHk+xNXaVt6ybRLiRflIt/gVv8AMoBOcF9EtVdf4NGT9cXn/TW0PcTc6mt2ovXv EFlJQ7ULboIDOEE1bb1HNumJlv8n27kr03vevkklWgCfjwekhojE4xeEGbCW+wR2N3a8 V1yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=Idk/JvW7nMrYo+XhQRaD5U9xm1eymgqbRSSjJNn+6FY=; b=wwkIf5GbR4Y85S7f5eh+zHL7jydk70mnZuet37lumirPTa0WfUk2rY3SRzyB3xDhXu 5C1TDxtYEwlFhCbqhlGfXfwL+4sm1k08KFTEFkc7LhWw0kyR74fUAEXPcB9o6ycxLaSE NNXegGFEDPhDDZo0AAogmQaDQXlLUGFkbcE9OOLE6lKExP64VzSrmVmcpOzcb/9UPBNz JFjdLDsRI24eb5pXYgHJZye9uRGw93SzO+WJuyuwH52NWWd+TbcqJOdhb33KAqF8YpRz /yBWJG8d4IIkT2oyA88I9+C9+XatRhxmwY0rFQJoVyXOBBidSieKFBY/9G7d65rr6tQ7 XvoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=rXzj5cLq; 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=pass (p=NONE sp=NONE dis=NONE) header.from=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y124-v6si12313657pgy.228.2018.06.18.08.09.38; Mon, 18 Jun 2018 08:09:52 -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; dkim=pass header.i=@android.com header.s=20161025 header.b=rXzj5cLq; 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=pass (p=NONE sp=NONE dis=NONE) header.from=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935677AbeFRPIf (ORCPT + 99 others); Mon, 18 Jun 2018 11:08:35 -0400 Received: from mail-pf0-f195.google.com ([209.85.192.195]:40835 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935611AbeFRPIc (ORCPT ); Mon, 18 Jun 2018 11:08:32 -0400 Received: by mail-pf0-f195.google.com with SMTP id z24-v6so8321363pfe.7 for ; Mon, 18 Jun 2018 08:08:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Idk/JvW7nMrYo+XhQRaD5U9xm1eymgqbRSSjJNn+6FY=; b=rXzj5cLqUGRi9z7Sk193mcsjs2SQlYiUGk0qoaUW5BTOMrVAO6Iy5SGGrV/BC03OFf 3PiRLlQMViEbNrPm2P0ultHINQLFlOjdEHlBpEXOdaswfwc7zqPAVkiQ8Vv2e6TRHbdj XTajFiPMw8tnxCGv9eau20wAVIfE/lHMfu8lnd6efthcis0KlcqyzwH4ddUEGFoFfkjy Y1PRTDQsZPQD0pwzDzQc8BmdGQRIkkz9Fmnr1ZX7E1PvxlWxV5/JpQObIMP+bJrKQ6Q1 22GhiduzMHKcB1Q9QwfI+RNmhRQMWx//fZuMW8hdjSiSne4hMY/5lTfVwvUEaEt4/KLy Pqtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Idk/JvW7nMrYo+XhQRaD5U9xm1eymgqbRSSjJNn+6FY=; b=sidE2mWGLOQnod0nHJ1qkj6SjfILJ5oUItWlNZcBwAtwkRwDQYiq30ol8IyMI6EfYE E2DoNGmLO6H0rGiPsKS65Zc3Sxxs1Fv01h3HFPrEKTpgjbrOq+faKUEie7n/DykEzPCe /b811zFnrr9vZjaqLEqrtQDQw9spYvUjfN3/TzhzcLtoMdoC7WXCcabaGUiVkMRLJrtg ph+LxWk7e9mu027j0xYAjM12Pj9LYzMDop55m5sLRgngUwazMyIvfclL7kGrD9mrI4d6 uey7zIL+QKgJI6dPgpd4jXY76UqSc5tiTnKeDBXpVfZLZrz6rZKLLCozdHLOMAZ+gl5V xplg== X-Gm-Message-State: APt69E1Ec6neKcv2F6nLBed806dD6cyjIah04AWMT6li3kRyJjW5ieJ5 jqAWyluy/76h82BE5jCWCZp6DqQL25U= X-Received: by 2002:a63:b609:: with SMTP id j9-v6mr11474754pgf.335.1529334511553; Mon, 18 Jun 2018 08:08:31 -0700 (PDT) Received: from nebulus.mtv.corp.google.com ([2620:0:1000:1611:6077:8eec:bc7e:d0f4]) by smtp.gmail.com with ESMTPSA id i7-v6sm54830660pfa.34.2018.06.18.08.08.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Jun 2018 08:08:31 -0700 (PDT) From: Mark Salyzyn To: linux-kernel@vger.kernel.org Cc: Mark Salyzyn , James Morse , Russell King , Catalin Marinas , Will Deacon , Andy Lutomirski , Dmitry Safonov , John Stultz , Mark Rutland , Laura Abbott , Kees Cook , Ard Biesheuvel , Andy Gross , Kevin Brodsky , Andrew Pinski , Thomas Gleixner , linux-arm-kernel@lists.infradead.org, Jeremy Linton , Jinbum Park , "Paul E. McKenney" , Ingo Molnar Subject: RESEND [PATCH v6 11/12] lib: vdso: Add support for CLOCK_BOOTTIME Date: Mon, 18 Jun 2018 08:05:57 -0700 Message-Id: <20180618150613.10322-12-salyzyn@android.com> X-Mailer: git-send-email 2.18.0.rc1.244.gcf134e6275-goog In-Reply-To: <20180618150613.10322-1-salyzyn@android.com> References: <20180618150613.10322-1-salyzyn@android.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Take an effort to recode the arm64 vdso code from assembler to C previously submitted by Andrew Pinski , rework it for use in both arm and arm64, overlapping any optimizations for each architecture. But instead of landing it in arm64, land the result into lib/vdso and unify both implementations to simplify future maintenance. Add a case for CLOCK_BOOTTIME as it is popular for measuring relative time on systems expected to suspend() or hibernate(). Android uses CLOCK_BOOTTIME for all relative time measurements and timeouts. Switching to vdso reduced CPU utilization and improves accuracy. There is also a desire by some partners to switch all logging over to CLOCK_BOOTTIME, and thus this operation alone would contribute to a near percentile CPU load. Signed-off-by: Mark Salyzyn Cc: James Morse Cc: Russell King Cc: Catalin Marinas Cc: Will Deacon Cc: Andy Lutomirski Cc: Dmitry Safonov Cc: John Stultz Cc: Mark Rutland Cc: Laura Abbott Cc: Kees Cook Cc: Ard Biesheuvel Cc: Andy Gross Cc: Kevin Brodsky Cc: Andrew Pinski Cc: Thomas Gleixner Cc: linux-kernel@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org Cc: Jeremy Linton v2: - rebased and changed from 3/3 to 10/10, fortified commit message. v3: - move arch/arm/vdso/vgettimeofday.c to lib/vdso/vgettimeofday.c. v4: - update commit message to reflect specific, and overall reasoning of patch series. - drop forced inline operations. - switch typeof() with __kernel_time_t. v5: - added comment about open coded timepsec_add_ns() for clarity. v6: - fix issue with __iter_div_u64_rem scaling by splitting sec & nsec --- arch/arm/include/asm/vdso_datapage.h | 2 + arch/arm/kernel/vdso.c | 4 ++ arch/arm64/include/asm/vdso_datapage.h | 2 + arch/arm64/kernel/vdso.c | 4 ++ lib/vdso/vgettimeofday.c | 56 ++++++++++++++++++++++++++ 5 files changed, 68 insertions(+) diff --git a/arch/arm/include/asm/vdso_datapage.h b/arch/arm/include/asm/vdso_datapage.h index 1c6e6a5d5d9d..0120852b6b12 100644 --- a/arch/arm/include/asm/vdso_datapage.h +++ b/arch/arm/include/asm/vdso_datapage.h @@ -64,6 +64,8 @@ struct vdso_data { u32 tz_minuteswest; /* timezone info for gettimeofday(2) */ u32 tz_dsttime; + u32 btm_sec; /* monotonic to boot time */ + u32 btm_nsec; /* Raw clocksource multipler */ u32 cs_raw_mult; /* Raw time */ diff --git a/arch/arm/kernel/vdso.c b/arch/arm/kernel/vdso.c index c299967df63c..51d8dcbd9952 100644 --- a/arch/arm/kernel/vdso.c +++ b/arch/arm/kernel/vdso.c @@ -337,6 +337,8 @@ void update_vsyscall(struct timekeeper *tk) vdso_data->wtm_clock_nsec = wtm->tv_nsec; if (!vdso_data->use_syscall) { + struct timespec btm = ktime_to_timespec(tk->offs_boot); + vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last; vdso_data->raw_time_sec = tk->raw_sec; vdso_data->raw_time_nsec = tk->tkr_raw.xtime_nsec; @@ -347,6 +349,8 @@ void update_vsyscall(struct timekeeper *tk) /* tkr_mono.shift == tkr_raw.shift */ vdso_data->cs_shift = tk->tkr_mono.shift; vdso_data->cs_mask = tk->tkr_mono.mask; + vdso_data->btm_sec = btm.tv_sec; + vdso_data->btm_nsec = btm.tv_nsec; } vdso_write_end(vdso_data); diff --git a/arch/arm64/include/asm/vdso_datapage.h b/arch/arm64/include/asm/vdso_datapage.h index 95f4a7abab80..348b9be9efe7 100644 --- a/arch/arm64/include/asm/vdso_datapage.h +++ b/arch/arm64/include/asm/vdso_datapage.h @@ -45,6 +45,8 @@ struct vdso_data { __u64 xtime_coarse_nsec; __u64 wtm_clock_sec; /* Wall to monotonic time */ vdso_wtm_clock_nsec_t wtm_clock_nsec; + __u32 btm_sec; /* monotonic to boot time */ + __u32 btm_nsec; __u32 tb_seq_count; /* Timebase sequence counter */ /* cs_* members must be adjacent and in this order (ldp accesses) */ __u32 cs_mono_mult; /* NTP-adjusted clocksource multiplier */ diff --git a/arch/arm64/kernel/vdso.c b/arch/arm64/kernel/vdso.c index 59f150c25889..8dd2ad220a0f 100644 --- a/arch/arm64/kernel/vdso.c +++ b/arch/arm64/kernel/vdso.c @@ -233,6 +233,8 @@ void update_vsyscall(struct timekeeper *tk) vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec; if (!use_syscall) { + struct timespec btm = ktime_to_timespec(tk->offs_boot); + /* tkr_mono.cycle_last == tkr_raw.cycle_last */ vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last; vdso_data->raw_time_sec = tk->raw_sec; @@ -243,6 +245,8 @@ void update_vsyscall(struct timekeeper *tk) vdso_data->cs_raw_mult = tk->tkr_raw.mult; /* tkr_mono.shift == tkr_raw.shift */ vdso_data->cs_shift = tk->tkr_mono.shift; + vdso_data->btm_sec = btm.tv_sec; + vdso_data->btm_nsec = btm.tv_nsec; } smp_wmb(); diff --git a/lib/vdso/vgettimeofday.c b/lib/vdso/vgettimeofday.c index 33c5917fe9f8..4c3af7bc6499 100644 --- a/lib/vdso/vgettimeofday.c +++ b/lib/vdso/vgettimeofday.c @@ -247,6 +247,51 @@ static notrace int do_monotonic_raw(const struct vdso_data *vd, return 0; } +static notrace int do_boottime(const struct vdso_data *vd, struct timespec *ts) +{ + u32 seq, mult, shift; + u64 nsec, cycle_last; + vdso_wtm_clock_nsec_t wtm_nsec; +#ifdef ARCH_CLOCK_FIXED_MASK + static const u64 mask = ARCH_CLOCK_FIXED_MASK; +#else + u64 mask; +#endif + __kernel_time_t sec; + + do { + seq = vdso_read_begin(vd); + + if (vd->use_syscall) + return -1; + + cycle_last = vd->cs_cycle_last; + + mult = vd->cs_mono_mult; + shift = vd->cs_shift; +#ifndef ARCH_CLOCK_FIXED_MASK + mask = vd->cs_mask; +#endif + + sec = vd->xtime_clock_sec; + nsec = vd->xtime_clock_snsec; + + sec += vd->wtm_clock_sec + vd->btm_sec; + wtm_nsec = vd->wtm_clock_nsec + vd->btm_nsec; + + } while (unlikely(vdso_read_retry(vd, seq))); + + nsec += get_clock_shifted_nsec(cycle_last, mult, mask); + nsec >>= shift; + nsec += wtm_nsec; + + /* open coding timespec_add_ns to save a ts->tv_nsec = 0 */ + ts->tv_sec = sec + __iter_div_u64_rem(nsec, NSEC_PER_SEC, &nsec); + ts->tv_nsec = nsec; + + return 0; +} + #else /* ARCH_PROVIDES_TIMER */ static notrace int do_realtime(const struct vdso_data *vd, struct timespec *ts) @@ -265,6 +310,12 @@ static notrace int do_monotonic_raw(const struct vdso_data *vd, return -1; } +static notrace int do_boottime(const struct vdso_data *vd, + struct timespec *ts) +{ + return -1; +} + #endif /* ARCH_PROVIDES_TIMER */ notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) @@ -290,6 +341,10 @@ notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) if (do_monotonic_raw(vd, ts)) goto fallback; break; + case CLOCK_BOOTTIME: + if (do_boottime(vd, ts)) + goto fallback; + break; default: goto fallback; } @@ -326,6 +381,7 @@ int __vdso_clock_getres(clockid_t clock, struct timespec *res) long nsec; if (clock == CLOCK_REALTIME || + clock == CLOCK_BOOTTIME || clock == CLOCK_MONOTONIC || clock == CLOCK_MONOTONIC_RAW) nsec = MONOTONIC_RES_NSEC; -- 2.18.0.rc1.244.gcf134e6275-goog