From patchwork Fri Jan 26 17:18:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Salyzyn X-Patchwork-Id: 10186667 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C0A77602C8 for ; Fri, 26 Jan 2018 17:19:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B120F295EB for ; Fri, 26 Jan 2018 17:19:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A54FC29E62; Fri, 26 Jan 2018 17:19:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1E124295EB for ; Fri, 26 Jan 2018 17:19:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=ctvhiqhXn9alMjoOmZRExhtQdfBPdWrJOWXHQOMShQM=; b=nt9 +mZpS05A6t1qezgdBRSng8IqhMema2XyAZmrtBt8POWH5w0rEd6ygLfGqP4WlKOdCnbn2xMfU9Jv8 j34ctaL5lWoZRmhBFRw1dHPKXUUdhVo4FfBcezLeco0X1LoTvRFOsbEPEYTEf07wzgv1yo8bZn5AA EkfCeDLzAVT1q0XCWoboQeflURrcIqR0no1d/00dan16NPxewrkCnamxckvrxJr+spRhRw2RTlsXg 1PA9sXF8TmZyQLfV6VgjXKGlhaj5qta0Jj2uyU+vYpPsMIo0jK6eCKHoacajZCEcnP3VIvi7ryjl1 VhrOXrw9/Lw7K5qaNmRuB8R58ue5v8A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1ef7f8-000771-9j; Fri, 26 Jan 2018 17:19:38 +0000 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1ef7f4-00075b-4n for linux-arm-kernel@lists.infradead.org; Fri, 26 Jan 2018 17:19:36 +0000 Received: by mail-pf0-x242.google.com with SMTP id b25so668271pfd.9 for ; Fri, 26 Jan 2018 09:19:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=dhW3vNRJIcGOLxBkwbVcw0Rm1SLdKhEAoUKNB9BRMvE=; b=LL5fCH19MAEz2ThsQHNb2iVOtLJB67v4dqyfLI3f0NCr05gW2g3eUJRy8ShIxLsBCo nuMvOKn7r1pKl97wF8FNp4UrrhLvpEcYYw1+q/3rRBtubi3j9M0f+PJDMefTS1MjtkU6 Srvk23NQukuXT8h89MXjVtoG9VB3UJ/Ngz/EDzRK1vNyJwt51aQMHjjNXytyX4bFsZVI 49ZRj5vz94s2L7wYA8eHdhtVi2NmRKubEWQcYaaGt36YuzTFfKS6wWoJoMmOm3Wymz2T LhRIwai4IfHsjfUTISw/UjEPko8yyoy/dNj00xSqTBmxtxNzDbp0sE8syYOeQrlbgIjA dhpQ== 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; bh=dhW3vNRJIcGOLxBkwbVcw0Rm1SLdKhEAoUKNB9BRMvE=; b=rfKn5kBmrafnl3Qh/KaiCp6/xdVMBQhaUW8chWjJKEnat7FH8YXuLFTeeH3tsIiZLS gxbSOKWCVeZIsxFYqBZhXOwrSCKKuqUb+h6OLnRnylYFcL18wca9b4mE9iCrsytCjoRH ZOK4NHWVp2Xg63Xul5bcvseNU+NPJwRxb/qrzRWbNarjliltwx9dk41826fIdVk0ccyD VBayPoNOaVbx+Cc/c4Oe+x+W/gTJmAWSoZYJGyd/wxmIUoM+DGgee5/f+C0FZ+T+MB6s dEaCF1pfhvAtLhGh06aMWmPdL8MxbcNtNR0a5HUITORDdss3CgCoc98QxI+3Dy5Tl2iT ppWA== X-Gm-Message-State: AKwxytcAHvyn+ag67dftwBE6TxLbGER6ANDIeXPsSgkRzQnB4D/CxWAR OllddR6mNvzNlO8sDQZkISvwNg== X-Google-Smtp-Source: AH8x227D4jOlB6GSEquUx86AaYsxjAO/WqoUDiIHQetLekwu8lqguKvAOTIC57ypO0yceIYy2Xfk1Q== X-Received: by 10.98.220.195 with SMTP id c64mr19546552pfl.47.1516987162254; Fri, 26 Jan 2018 09:19:22 -0800 (PST) Received: from nebulus.mtv.corp.google.com ([2620:0:1000:1612:b4fb:6752:f21f:3502]) by smtp.gmail.com with ESMTPSA id 13sm20258382pfk.93.2018.01.26.09.19.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Jan 2018 09:19:21 -0800 (PST) From: Mark Salyzyn To: linux-kernel@vger.kernel.org Subject: [PATCH v6 11/12] lib: vdso: Add support for CLOCK_BOOTTIME Date: Fri, 26 Jan 2018 09:18:48 -0800 Message-Id: <20180126171907.87576-1-salyzyn@android.com> X-Mailer: git-send-email 2.16.0.rc1.238.g530d649a79-goog X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Laura Abbott , Kees Cook , Ard Biesheuvel , "Paul E. McKenney" , Catalin Marinas , Kevin Brodsky , Will Deacon , Russell King , Andy Lutomirski , James Morse , Andrew Pinski , Dmitry Safonov , Andy Gross , John Stultz , Thomas Gleixner , Mark Salyzyn , Ingo Molnar , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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 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 bab013d6c2e3..ce1b1483e4e2 100644 --- a/arch/arm/kernel/vdso.c +++ b/arch/arm/kernel/vdso.c @@ -335,6 +335,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; @@ -345,6 +347,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;