Take an effort to recode the arm64 vdso code from assembler to C
previously submitted by Andrew Pinski <apin...@cavium.com>, 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_MONOTONIC_RAW to match up with support that
is available in arm64's vdso.

Signed-off-by: Mark Salyzyn <saly...@android.com>
Cc: James Morse <james.mo...@arm.com>
Cc: Russell King <li...@armlinux.org.uk>
Cc: Catalin Marinas <catalin.mari...@arm.com>
Cc: Will Deacon <will.dea...@arm.com>
Cc: Andy Lutomirski <l...@amacapital.net>
Cc: Dmitry Safonov <dsafo...@virtuozzo.com>
Cc: John Stultz <john.stu...@linaro.org>
Cc: Mark Rutland <mark.rutl...@arm.com>
Cc: Laura Abbott <labb...@redhat.com>
Cc: Kees Cook <keesc...@chromium.org>
Cc: Ard Biesheuvel <ard.biesheu...@linaro.org>
Cc: Andy Gross <andy.gr...@linaro.org>
Cc: Kevin Brodsky <kevin.brod...@arm.com>
Cc: Andrew Pinski <apin...@cavium.com>
Cc: Thomas Gleixner <t...@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Cc: linux-arm-ker...@lists.infradead.org
Cc: Jeremy Linton <jeremy.lin...@arm.com>

v2:
- split first CL into 6 of 7 pieces

v4:
- Move out ARCH_CLOCK_FIXED_MASK to later adjustment.
- update commit message to reflect overall reasoning.
- replace typeof() with type vdso_raw_time_sec_t.

v5:
- replace erroneous tk->raw_time.shift with tk->tkr_raw.shift

v6:
- fixup raw_time_sec and raw_time_nsec in vdso.c
---
 arch/arm/include/asm/vdso_datapage.h | 11 +++++++
 arch/arm/kernel/vdso.c               |  3 ++
 arch/arm/vdso/vgettimeofday.c        | 44 ++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+)

diff --git a/arch/arm/include/asm/vdso_datapage.h 
b/arch/arm/include/asm/vdso_datapage.h
index 8dd7303db4ec..1c6e6a5d5d9d 100644
--- a/arch/arm/include/asm/vdso_datapage.h
+++ b/arch/arm/include/asm/vdso_datapage.h
@@ -34,6 +34,11 @@ typedef u32 vdso_wtm_clock_nsec_t;
 typedef u32 vdso_xtime_clock_sec_t;
 #endif
 
+#ifndef _VDSO_RAW_TIME_SEC_T
+#define _VDSO_RAW_TIME_SEC_T
+typedef u32 vdso_raw_time_sec_t;
+#endif
+
 /* Try to be cache-friendly on systems that don't implement the
  * generic timer: fit the unconditionally updated fields in the first
  * 32 bytes.
@@ -58,6 +63,12 @@ struct vdso_data {
        u64 xtime_clock_snsec;  /* CLOCK_REALTIME sub-ns base */
        u32 tz_minuteswest;     /* timezone info for gettimeofday(2) */
        u32 tz_dsttime;
+
+       /* Raw clocksource multipler */
+       u32 cs_raw_mult;
+       /* Raw time */
+       vdso_raw_time_sec_t raw_time_sec;
+       u32 raw_time_nsec;
 };
 
 union vdso_data_store {
diff --git a/arch/arm/kernel/vdso.c b/arch/arm/kernel/vdso.c
index c2c57f6b8c60..9c13a32fa5f0 100644
--- a/arch/arm/kernel/vdso.c
+++ b/arch/arm/kernel/vdso.c
@@ -337,9 +337,12 @@ void update_vsyscall(struct timekeeper *tk)
 
        if (!vdso_data->use_syscall) {
                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;
                vdso_data->xtime_clock_sec      = tk->xtime_sec;
                vdso_data->xtime_clock_snsec    = tk->tkr_mono.xtime_nsec;
                vdso_data->cs_mono_mult         = tk->tkr_mono.mult;
+               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->cs_mask              = tk->tkr_mono.mask;
diff --git a/arch/arm/vdso/vgettimeofday.c b/arch/arm/vdso/vgettimeofday.c
index 59893fca03b3..a2c4db83edc4 100644
--- a/arch/arm/vdso/vgettimeofday.c
+++ b/arch/arm/vdso/vgettimeofday.c
@@ -194,6 +194,40 @@ static notrace int do_monotonic(const struct vdso_data 
*vd, struct timespec *ts)
        return 0;
 }
 
+static notrace int do_monotonic_raw(const struct vdso_data *vd,
+                                   struct timespec *ts)
+{
+       u32 seq, mult, shift;
+       u64 nsec, cycle_last;
+       u64 mask;
+       vdso_raw_time_sec_t sec;
+
+       do {
+               seq = vdso_read_begin(vd);
+
+               if (vd->use_syscall)
+                       return -1;
+
+               cycle_last = vd->cs_cycle_last;
+
+               mult = vd->cs_raw_mult;
+               shift = vd->cs_shift;
+               mask = vd->cs_mask;
+
+               sec = vd->raw_time_sec;
+               nsec = vd->raw_time_nsec;
+
+       } while (unlikely(vdso_read_retry(vd, seq)));
+
+       nsec += get_clock_shifted_nsec(cycle_last, mult, mask);
+       nsec >>= shift;
+       /* 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 /* CONFIG_ARM_ARCH_TIMER */
 
 static notrace int do_realtime(const struct vdso_data *vd, struct timespec *ts)
@@ -206,6 +240,12 @@ static notrace int do_monotonic(const struct vdso_data 
*vd, struct timespec *ts)
        return -1;
 }
 
+static notrace int do_monotonic_raw(const struct vdso_data *vd,
+                                   struct timespec *ts)
+{
+       return -1;
+}
+
 #endif /* CONFIG_ARM_ARCH_TIMER */
 
 notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts)
@@ -227,6 +267,10 @@ notrace int __vdso_clock_gettime(clockid_t clock, struct 
timespec *ts)
                if (do_monotonic(vd, ts))
                        goto fallback;
                break;
+       case CLOCK_MONOTONIC_RAW:
+               if (do_monotonic_raw(vd, ts))
+                       goto fallback;
+               break;
        default:
                goto fallback;
        }
-- 
2.18.0.rc1.244.gcf134e6275-goog

Reply via email to