Skip to contentSkip to conversation

tile: add clock_gettime support to vDSO

PATCH In review Chris Metcalf 4 participants 10 messages Sep 30, 2014 – Oct 2, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tile: add clock_gettime support to vDSO

This change adds support for clock_gettime with CLOCK_REALTIME and CLOCK_MONOTONIC using vDSO. In addition, with this change we switch to use seqlocks instead of integer counters.

We also support the *_COARSE clockid_t, for apps that want speed but aren't concerned about fine-grained timestamps; this saves about 20 cycles per call (see http://lwn.net/Articles/342018/).

---
 arch/tile/include/asm/vdso.h          |   9 +-
 arch/tile/kernel/time.c               |  31 ++++---
 arch/tile/kernel/vdso/vdso.lds.S      |   2 +
 arch/tile/kernel/vdso/vgettimeofday.c | 166 +++++++++++++++++++++++++---------
 4 files changed, 153 insertions(+), 55 deletions(-)
arch/tile/include/asm/vdso.h +7 -2
index 9f6a78d665fa..4527701fcead 100644
@@ -15,6 +15,7 @@
 #ifndef __TILE_VDSO_H__
 #define __TILE_VDSO_H__
 
+#include <linux/seqlock.h>
 #include <linux/types.h>
 
 /*
@@ -26,8 +27,8 @@
  */
 
 struct vdso_data {
-	__u64 tz_update_count;  /* Timezone atomicity ctr             */
-	__u64 tb_update_count;  /* Timebase atomicity ctr             */
+	seqcount_t tz_seq;	/* Timezone seqlock                   */
+	seqcount_t tb_seq;	/* Timebase seqlock                   */
 	__u64 xtime_tod_stamp;  /* TOD clock for xtime                */
 	__u64 xtime_clock_sec;  /* Kernel time second                 */
 	__u64 xtime_clock_nsec; /* Kernel time nanosecond             */
@@ -37,6 +38,10 @@ struct vdso_data {
 	__u32 shift;            /* Cycle to nanosecond divisor (power of two) */
 	__u32 tz_minuteswest;   /* Minutes west of Greenwich          */
 	__u32 tz_dsttime;       /* Type of dst correction             */
+	__u64 xtime_clock_coarse_sec;	/* Coarse kernel time         */
+	__u64 xtime_clock_coarse_nsec;
+	__u64 wtom_clock_coarse_sec;    /* Coarse wall to monotonic time */
+	__u64 wtom_clock_coarse_nsec;
 };
 
 extern struct vdso_data *vdso_data;
arch/tile/kernel/time.c +19 -12
index 462dcd0c1700..77624b38bdb9 100644
@@ -249,13 +249,10 @@ cycles_t ns2cycles(unsigned long nsecs)
 
 void update_vsyscall_tz(void)
 {
-	/* Userspace gettimeofday will spin while this value is odd. */
-	++vdso_data->tz_update_count;
-	smp_wmb();
+	write_seqcount_begin(&vdso_data->tz_seq);
 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
-	smp_wmb();
-	++vdso_data->tz_update_count;
+	write_seqcount_end(&vdso_data->tz_seq);
 }
 
 void update_vsyscall(struct timekeeper *tk)
@@ -263,20 +260,30 @@ void update_vsyscall(struct timekeeper *tk)
 	struct timespec wall_time = tk_xtime(tk);
 	struct timespec *wtm = &tk->wall_to_monotonic;
 	struct clocksource *clock = tk->clock;
+	struct timespec ts;
 
 	if (clock != &cycle_counter_cs)
 		return;
 
-	/* Userspace gettimeofday will spin while this value is odd. */
-	++vdso_data->tb_update_count;
-	smp_wmb();
+	write_seqcount_begin(&vdso_data->tb_seq);
+
 	vdso_data->xtime_tod_stamp = clock->cycle_last;
 	vdso_data->xtime_clock_sec = wall_time.tv_sec;
 	vdso_data->xtime_clock_nsec = wall_time.tv_nsec;
-	vdso_data->wtom_clock_sec = wtm->tv_sec;
-	vdso_data->wtom_clock_nsec = wtm->tv_nsec;
+
+	ts = timespec_add(wall_time, *wtm);
+	vdso_data->wtom_clock_sec = ts.tv_sec;
+	vdso_data->wtom_clock_nsec = ts.tv_nsec;
+
 	vdso_data->mult = clock->mult;
 	vdso_data->shift = clock->shift;
-	smp_wmb();
-	++vdso_data->tb_update_count;
+
+	ts = __current_kernel_time();
+	vdso_data->xtime_clock_coarse_sec = ts.tv_sec;
+	vdso_data->xtime_clock_coarse_nsec = ts.tv_nsec;
+	ts = timespec_add(ts, *wtm);
+	vdso_data->wtom_clock_coarse_sec = ts.tv_sec;
+	vdso_data->wtom_clock_coarse_nsec = ts.tv_nsec;
+
+	write_seqcount_end(&vdso_data->tb_seq);
 }
arch/tile/kernel/vdso/vdso.lds.S +2 -0
index 041cd6c39c83..731529f3f06f 100644
@@ -82,6 +82,8 @@ VERSION
 		__vdso_rt_sigreturn;
 		__vdso_gettimeofday;
 		gettimeofday;
+		__vdso_clock_gettime;
+		clock_gettime;
 	local:*;
 	};
 }
arch/tile/kernel/vdso/vgettimeofday.c +125 -41

191 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 3 lines · Chris Metcalf

This change adds support for clock_gettime with CLOCK_REALTIME and CLOCK_MONOTONIC using vDSO. In addition, with this change we switch to use seqlocks instead of integer counters.

I'd rather split that into two patches. One changing the code to use the seqlock and the other to add the clock_gettime() stuff.

Quoted 26 lines

void update_vsyscall(struct timekeeper *tk)

arch/tile/kernel/time.c +8 -5
@@ -263,20 +260,30 @@ void update_vsyscall(struct timekeeper *tk)
 	struct timespec wall_time = tk_xtime(tk);
 	struct timespec *wtm = &tk->wall_to_monotonic;
 	struct clocksource *clock = tk->clock;
+	struct timespec ts;
 
 	if (clock != &cycle_counter_cs)
 		return;
 
-	/* Userspace gettimeofday will spin while this value is odd. */
-	++vdso_data->tb_update_count;
-	smp_wmb();
+	write_seqcount_begin(&vdso_data->tb_seq);
+
 	vdso_data->xtime_tod_stamp = clock->cycle_last;
 	vdso_data->xtime_clock_sec = wall_time.tv_sec;
 	vdso_data->xtime_clock_nsec = wall_time.tv_nsec;
-	vdso_data->wtom_clock_sec = wtm->tv_sec;
-	vdso_data->wtom_clock_nsec = wtm->tv_nsec;
+
+	ts = timespec_add(wall_time, *wtm);
+	vdso_data->wtom_clock_sec = ts.tv_sec;
+	vdso_data->wtom_clock_nsec = ts.tv_nsec;
+
 	vdso_data->mult = clock->mult;

arch/tile/kernel/time.c void update_vsyscall(struct timekeeper *tk) · line 286

Quoted 10 lines
arch/tile/kernel/time.c +7 -2
 	vdso_data->shift = clock->shift;
-	smp_wmb();
-	++vdso_data->tb_update_count;
+
+	ts = __current_kernel_time();
+	vdso_data->xtime_clock_coarse_sec = ts.tv_sec;
+	vdso_data->xtime_clock_coarse_nsec = ts.tv_nsec;
+	ts = timespec_add(ts, *wtm);
+	vdso_data->wtom_clock_coarse_sec = ts.tv_sec;
+	vdso_data->wtom_clock_coarse_nsec = ts.tv_nsec;

I'm fine with the code, but you might think about doing the math and preparation stuff outside of the seqcount protected region to a shadow struct and then do a simple memcpy inside of the seqcount protected region. Nothing significant, but it might be worthwhile as a follow up or preparatory change.

Quoted 5 lines
arch/tile/kernel/vdso/vgettimeofday.c +2 -0
+static inline int do_monotonic(struct vdso_data *vdso, struct timespec *ts)
 {
+	int count;
arch/tile/kernel/vdso/vgettimeofday.c +0 -1
 	cycles_t cycles;
-	unsigned long count, sec, ns;

arch/tile/kernel/vdso/vgettimeofday.c inline unsigned long get_datapage(void) · line 89

Quoted 10 lines
arch/tile/kernel/vdso/vgettimeofday.c +9 -1
-	volatile struct vdso_data *vdso_data;
+	unsigned long ns;
+
+	do {
+		count = read_seqcount_begin(&vdso->tb_seq);
+		cycles = get_cycles() - vdso->xtime_tod_stamp;
+		ns = (cycles * vdso->mult) >> vdso->shift;
+		ts->tv_sec = vdso->wtom_clock_sec;
+		ts->tv_nsec = vdso->wtom_clock_nsec;
+	} while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));

I doubt that the unlikely makes any difference.

Otherwise this looks good.

Thanks,

tglx

#2 lore raw
In reply to Chris Metcalf
Quoted 71 lines · Chris Metcalf

This change adds support for clock_gettime with CLOCK_REALTIME and CLOCK_MONOTONIC using vDSO. In addition, with this change we switch to use seqlocks instead of integer counters.

We also support the *_COARSE clockid_t, for apps that want speed but aren't concerned about fine-grained timestamps; this saves about 20 cycles per call (see http://lwn.net/Articles/342018/).

---
 arch/tile/include/asm/vdso.h          |   9 +-
 arch/tile/kernel/time.c               |  31 ++++---
 arch/tile/kernel/vdso/vdso.lds.S      |   2 +
 arch/tile/kernel/vdso/vgettimeofday.c | 166 +++++++++++++++++++++++++---------
 4 files changed, 153 insertions(+), 55 deletions(-)
arch/tile/include/asm/vdso.h +7 -2
index 9f6a78d665fa..4527701fcead 100644
@@ -15,6 +15,7 @@
 #ifndef __TILE_VDSO_H__
 #define __TILE_VDSO_H__
 
+#include <linux/seqlock.h>
 #include <linux/types.h>
 
 /*
@@ -26,8 +27,8 @@
  */
 
 struct vdso_data {
-       __u64 tz_update_count;  /* Timezone atomicity ctr             */
-       __u64 tb_update_count;  /* Timebase atomicity ctr             */
+       seqcount_t tz_seq;      /* Timezone seqlock                   */
+       seqcount_t tb_seq;      /* Timebase seqlock                   */
        __u64 xtime_tod_stamp;  /* TOD clock for xtime                */
        __u64 xtime_clock_sec;  /* Kernel time second                 */
        __u64 xtime_clock_nsec; /* Kernel time nanosecond             */
@@ -37,6 +38,10 @@ struct vdso_data {
        __u32 shift;            /* Cycle to nanosecond divisor (power of two) */
        __u32 tz_minuteswest;   /* Minutes west of Greenwich          */
        __u32 tz_dsttime;       /* Type of dst correction             */
+       __u64 xtime_clock_coarse_sec;   /* Coarse kernel time         */
+       __u64 xtime_clock_coarse_nsec;
+       __u64 wtom_clock_coarse_sec;    /* Coarse wall to monotonic time */
+       __u64 wtom_clock_coarse_nsec;
 };
 
 extern struct vdso_data *vdso_data;
arch/tile/kernel/time.c +2 -5
index 462dcd0c1700..77624b38bdb9 100644
@@ -249,13 +249,10 @@ cycles_t ns2cycles(unsigned long nsecs)
 
 void update_vsyscall_tz(void)
 {
-       /* Userspace gettimeofday will spin while this value is odd. */
-       ++vdso_data->tz_update_count;
-       smp_wmb();
+       write_seqcount_begin(&vdso_data->tz_seq);
        vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
        vdso_data->tz_dsttime = sys_tz.tz_dsttime;
-       smp_wmb();
-       ++vdso_data->tz_update_count;
+       write_seqcount_end(&vdso_data->tz_seq);
 }
 
 void update_vsyscall(struct timekeeper *tk)

arch/tile/kernel/time.c void update_vsyscall(struct timekeeper *tk) · line 260

Quoted 1 line
arch/tile/kernel/time.c +0 -0
@@ -263,20 +260,30 @@ void update_vsyscall(struct timekeeper *tk)
        struct timespec wall_time = tk_xtime(tk);

So this looks like a pre-existing issue, but here the sub-ns stored value in the timekeeper is being truncated to nanoseconds. You probably want to preserve that all the way into the do_realtime()/etc calculation, otherwise the time could seem to jump backwards by 1ns after the update.

I'd take a look at what the core timekeeping logic does or the x86 implementation to get a sense of what you ought to do there to preserve the logic. It basically comes down to changing it from:
nsec + (mult *cycle_delta) >> shift
to:
(snsec + (mult *cycle_delta)) >> shift

thanks
-john

#3 lore raw

PATCH v2 1/2 tile: switch to using seqlocks for the vDSO time code

In reply to Chris Metcalf
---
 arch/tile/include/asm/vdso.h          |  5 ++--
 arch/tile/kernel/time.c               | 16 +++++-------
 arch/tile/kernel/vdso/vgettimeofday.c | 46 ++++++++++++-----------------------
 3 files changed, 24 insertions(+), 43 deletions(-)

v2: split apart seqlock and new functionality into separate commits

arch/tile/include/asm/vdso.h +3 -2
index 9f6a78d665fa..d64b0d58a7e9 100644
@@ -15,6 +15,7 @@
 #ifndef __TILE_VDSO_H__
 #define __TILE_VDSO_H__
 
+#include <linux/seqlock.h>
 #include <linux/types.h>
 
 /*
@@ -26,8 +27,8 @@
  */
 
 struct vdso_data {
-	__u64 tz_update_count;  /* Timezone atomicity ctr             */
-	__u64 tb_update_count;  /* Timebase atomicity ctr             */
+	seqcount_t tz_seq;	/* Timezone seqlock                   */
+	seqcount_t tb_seq;	/* Timebase seqlock                   */
 	__u64 xtime_tod_stamp;  /* TOD clock for xtime                */
 	__u64 xtime_clock_sec;  /* Kernel time second                 */
 	__u64 xtime_clock_nsec; /* Kernel time nanosecond             */
arch/tile/kernel/time.c +6 -10
index 462dcd0c1700..0661045f718d 100644
@@ -249,13 +249,10 @@ cycles_t ns2cycles(unsigned long nsecs)
 
 void update_vsyscall_tz(void)
 {
-	/* Userspace gettimeofday will spin while this value is odd. */
-	++vdso_data->tz_update_count;
-	smp_wmb();
+	write_seqcount_begin(&vdso_data->tz_seq);
 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
-	smp_wmb();
-	++vdso_data->tz_update_count;
+	write_seqcount_end(&vdso_data->tz_seq);
 }
 
 void update_vsyscall(struct timekeeper *tk)
@@ -267,9 +264,8 @@ void update_vsyscall(struct timekeeper *tk)
 	if (clock != &cycle_counter_cs)
 		return;
 
-	/* Userspace gettimeofday will spin while this value is odd. */
-	++vdso_data->tb_update_count;
-	smp_wmb();
+	write_seqcount_begin(&vdso_data->tb_seq);
+
 	vdso_data->xtime_tod_stamp = clock->cycle_last;
 	vdso_data->xtime_clock_sec = wall_time.tv_sec;
 	vdso_data->xtime_clock_nsec = wall_time.tv_nsec;
@@ -277,6 +273,6 @@ void update_vsyscall(struct timekeeper *tk)
 	vdso_data->wtom_clock_nsec = wtm->tv_nsec;
 	vdso_data->mult = clock->mult;
 	vdso_data->shift = clock->shift;
-	smp_wmb();
-	++vdso_data->tb_update_count;
+
+	write_seqcount_end(&vdso_data->tb_seq);
 }
arch/tile/kernel/vdso/vgettimeofday.c +15 -31

64 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#4 lore raw

PATCH v2 2/2 tile: add clock_gettime support to vDSO

In reply to Chris Metcalf

This change adds support for clock_gettime with CLOCK_REALTIME and CLOCK_MONOTONIC using vDSO. It also updates the algorithm used for the clocks to support sub-nanosecond times, using the x86 architecture code as a model.

We also support the *_COARSE clockid_t, for apps that want speed but aren't concerned about fine-grained timestamps; this saves about 20 cycles per call (see http://lwn.net/Articles/342018/).

---
v2: use the x86 code as a model to address both John Stultz's
observation about sub-nanosecond updates, as well as to address Thomas Gleixner's observation about minimizing time spent with the seqlock held. Note that I kept the "unlikely" that Thomas doubted would help, just for parallelism with the x86 code.

 arch/tile/include/asm/vdso.h          |  15 ++--
 arch/tile/kernel/time.c               |  45 ++++++++---
 arch/tile/kernel/vdso/vgettimeofday.c | 145 +++++++++++++++++++++++++++++-----
 3 files changed, 170 insertions(+), 36 deletions(-)
arch/tile/include/asm/vdso.h +10 -5
index d64b0d58a7e9..9b069692153f 100644
@@ -29,13 +29,18 @@
 struct vdso_data {
 	seqcount_t tz_seq;	/* Timezone seqlock                   */
 	seqcount_t tb_seq;	/* Timebase seqlock                   */
-	__u64 xtime_tod_stamp;  /* TOD clock for xtime                */
-	__u64 xtime_clock_sec;  /* Kernel time second                 */
-	__u64 xtime_clock_nsec; /* Kernel time nanosecond             */
-	__u64 wtom_clock_sec;   /* Wall to monotonic clock second     */
-	__u64 wtom_clock_nsec;  /* Wall to monotonic clock nanosecond */
+	__u64 cycle_last;       /* TOD clock for xtime                */
+	__u64 mask;             /* Cycle mask                         */
 	__u32 mult;             /* Cycle to nanosecond multiplier     */
 	__u32 shift;            /* Cycle to nanosecond divisor (power of two) */
+	__u64 wall_time_sec;
+	__u64 wall_time_snsec;
+	__u64 monotonic_time_sec;
+	__u64 monotonic_time_snsec;
+	__u64 wall_time_coarse_sec;
+	__u64 wall_time_coarse_nsec;
+	__u64 monotonic_time_coarse_sec;
+	__u64 monotonic_time_coarse_nsec;
 	__u32 tz_minuteswest;   /* Minutes west of Greenwich          */
 	__u32 tz_dsttime;       /* Type of dst correction             */
 };
arch/tile/kernel/time.c +33 -12

55 lines not on this page · raw message

arch/tile/kernel/vdso/vgettimeofday.c +126 -19

174 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#5 lore raw
In reply to Chris Metcalf
Quoted 9 lines · Chris Metcalf

This change adds support for clock_gettime with CLOCK_REALTIME and CLOCK_MONOTONIC using vDSO. In addition, with this change we switch to use seqlocks instead of integer counters.

We also support the *_COARSE clockid_t, for apps that want speed but aren't concerned about fine-grained timestamps; this saves about 20 cycles per call (see http://lwn.net/Articles/342018/).

What rev is this based on? When I apply it to Linus latest (aad7fb916), it fails in arch/tile/kernel/time.c and arch/tile/kernel/vdso/vgettimeofday.c (see below)

Quoted 62 lines
---
 arch/tile/include/asm/vdso.h          |   9 +-
 arch/tile/kernel/time.c               |  31 ++++---
 arch/tile/kernel/vdso/vdso.lds.S      |   2 +
 arch/tile/kernel/vdso/vgettimeofday.c | 166 +++++++++++++++++++++++++---------
 4 files changed, 153 insertions(+), 55 deletions(-)
arch/tile/include/asm/vdso.h +7 -2
index 9f6a78d665fa..4527701fcead 100644
@@ -15,6 +15,7 @@
 #ifndef __TILE_VDSO_H__
 #define __TILE_VDSO_H__
 
+#include <linux/seqlock.h>
 #include <linux/types.h>
 
 /*
@@ -26,8 +27,8 @@
  */
 
 struct vdso_data {
-	__u64 tz_update_count;  /* Timezone atomicity ctr             */
-	__u64 tb_update_count;  /* Timebase atomicity ctr             */
+	seqcount_t tz_seq;	/* Timezone seqlock                   */
+	seqcount_t tb_seq;	/* Timebase seqlock                   */
 	__u64 xtime_tod_stamp;  /* TOD clock for xtime                */
 	__u64 xtime_clock_sec;  /* Kernel time second                 */
 	__u64 xtime_clock_nsec; /* Kernel time nanosecond             */
@@ -37,6 +38,10 @@ struct vdso_data {
 	__u32 shift;            /* Cycle to nanosecond divisor (power of two) */
 	__u32 tz_minuteswest;   /* Minutes west of Greenwich          */
 	__u32 tz_dsttime;       /* Type of dst correction             */
+	__u64 xtime_clock_coarse_sec;	/* Coarse kernel time         */
+	__u64 xtime_clock_coarse_nsec;
+	__u64 wtom_clock_coarse_sec;    /* Coarse wall to monotonic time */
+	__u64 wtom_clock_coarse_nsec;
 };
 
 extern struct vdso_data *vdso_data;
arch/tile/kernel/time.c +2 -5
index 462dcd0c1700..77624b38bdb9 100644
@@ -249,13 +249,10 @@ cycles_t ns2cycles(unsigned long nsecs)
 
 void update_vsyscall_tz(void)
 {
-	/* Userspace gettimeofday will spin while this value is odd. */
-	++vdso_data->tz_update_count;
-	smp_wmb();
+	write_seqcount_begin(&vdso_data->tz_seq);
 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
-	smp_wmb();
-	++vdso_data->tz_update_count;
+	write_seqcount_end(&vdso_data->tz_seq);
 }
 
 void update_vsyscall(struct timekeeper *tk)

arch/tile/kernel/time.c void update_vsyscall(struct timekeeper *tk) · line 260

Quoted 1 line
arch/tile/kernel/time.c +0 -0
@@ -263,20 +260,30 @@ void update_vsyscall(struct timekeeper *tk)
 	struct timespec wall_time = tk_xtime(tk);

wall_time was removed by dc01c9fa ("tile: Convert VDSO timekeeping to the precise mechanism"). This causes hunk to fail.

Quoted 9 lines
arch/tile/kernel/time.c +1 -2
 	struct timespec *wtm = &tk->wall_to_monotonic;
 	struct clocksource *clock = tk->clock;
+	struct timespec ts;
 
 	if (clock != &cycle_counter_cs)
 		return;
 
-	/* Userspace gettimeofday will spin while this value is odd. */
-	++vdso_data->tb_update_count;

arch/tile/kernel/time.c void update_vsyscall(struct timekeeper *tk) · line 274

Quoted 10 lines
arch/tile/kernel/time.c +4 -3
-	smp_wmb();
+	write_seqcount_begin(&vdso_data->tb_seq);
+
 	vdso_data->xtime_tod_stamp = clock->cycle_last;
 	vdso_data->xtime_clock_sec = wall_time.tv_sec;
 	vdso_data->xtime_clock_nsec = wall_time.tv_nsec;
-	vdso_data->wtom_clock_sec = wtm->tv_sec;
-	vdso_data->wtom_clock_nsec = wtm->tv_nsec;
+
+	ts = timespec_add(wall_time, *wtm);

could you do something like

     ts = timespec_add(&tk->wall_to_monotonic, wtm);

instead?

Quoted 235 lines
arch/tile/kernel/time.c +12 -2
+	vdso_data->wtom_clock_sec = ts.tv_sec;
+	vdso_data->wtom_clock_nsec = ts.tv_nsec;
+
 	vdso_data->mult = clock->mult;
 	vdso_data->shift = clock->shift;
-	smp_wmb();
-	++vdso_data->tb_update_count;
+
+	ts = __current_kernel_time();
+	vdso_data->xtime_clock_coarse_sec = ts.tv_sec;
+	vdso_data->xtime_clock_coarse_nsec = ts.tv_nsec;
+	ts = timespec_add(ts, *wtm);
+	vdso_data->wtom_clock_coarse_sec = ts.tv_sec;
+	vdso_data->wtom_clock_coarse_nsec = ts.tv_nsec;
+
+	write_seqcount_end(&vdso_data->tb_seq);
 }
arch/tile/kernel/vdso/vdso.lds.S +2 -0
index 041cd6c39c83..731529f3f06f 100644
@@ -82,6 +82,8 @@ VERSION
 		__vdso_rt_sigreturn;
 		__vdso_gettimeofday;
 		gettimeofday;
+		__vdso_clock_gettime;
+		clock_gettime;
 	local:*;
 	};
 }
arch/tile/kernel/vdso/vgettimeofday.c +125 -41
index 51ec8e46f5f9..a09043492d1e 100644
@@ -15,8 +15,14 @@
 #define VDSO_BUILD  /* avoid some shift warnings for -m32 in <asm/page.h> */
 #include <linux/time.h>
 #include <asm/timex.h>
+#include <asm/unistd.h>
 #include <asm/vdso.h>
 
+struct syscall_return_value {
+	long value;
+	long error;
+};
+
 #if CHIP_HAS_SPLIT_CYCLE()
 static inline cycles_t get_cycles_inline(void)
 {
@@ -50,58 +56,136 @@ inline unsigned long get_datapage(void)
 	return ret;
 }
 
-int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
+static inline int do_realtime(struct vdso_data *vdso, struct timespec *ts)
+{
+	int count;
+	cycles_t cycles;
+	unsigned long ns;
+
+	do {
+		count = read_seqcount_begin(&vdso->tb_seq);
+		cycles = get_cycles() - vdso->xtime_tod_stamp;
+		ns = (cycles * vdso->mult) >> vdso->shift;
+		ts->tv_sec = vdso->xtime_clock_sec;
+		ts->tv_nsec = vdso->xtime_clock_nsec;
+	} while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));
+
+	timespec_add_ns(ts, ns);
+	return 0;
+}
+
+static inline int do_monotonic(struct vdso_data *vdso, struct timespec *ts)
 {
+	int count;
 	cycles_t cycles;
-	unsigned long count, sec, ns;
-	volatile struct vdso_data *vdso_data;
+	unsigned long ns;
+
+	do {
+		count = read_seqcount_begin(&vdso->tb_seq);
+		cycles = get_cycles() - vdso->xtime_tod_stamp;
+		ns = (cycles * vdso->mult) >> vdso->shift;
+		ts->tv_sec = vdso->wtom_clock_sec;
+		ts->tv_nsec = vdso->wtom_clock_nsec;
+	} while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));
+
+	timespec_add_ns(ts, ns);
+	return 0;
+}
+
+static inline int do_realtime_coarse(struct vdso_data *vdso,
+				     struct timespec *ts)
+{
+	unsigned long count;
+
+	do {
+		count = read_seqcount_begin(&vdso->tb_seq);
+		ts->tv_sec = vdso->xtime_clock_coarse_sec;
+		ts->tv_nsec = vdso->xtime_clock_coarse_nsec;
+	} while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));
+
+	return 0;
+}
+
+static inline int do_monotonic_coarse(struct vdso_data *vdso,
+				      struct timespec *ts)
+{
+	unsigned long count;
+
+	do {
+		count = read_seqcount_begin(&vdso->tb_seq);
+		ts->tv_sec = vdso->wtom_clock_coarse_sec;
+		ts->tv_nsec = vdso->wtom_clock_coarse_nsec;
+	} while (unlikely(read_seqcount_retry(&vdso->tb_seq, count)));
+
+	return 0;
+}
+
+struct syscall_return_value __vdso_gettimeofday(struct timeval *tv,
+						struct timezone *tz)
+{
+	struct syscall_return_value ret = { 0, 0 };
+	unsigned long count;
+	struct vdso_data *vdso = (struct vdso_data *)get_datapage();
 
-	vdso_data = (struct vdso_data *)get_datapage();
 	/* The use of the timezone is obsolete, normally tz is NULL. */
 	if (unlikely(tz != NULL)) {
-		while (1) {
-			/* Spin until the update finish. */
-			count = vdso_data->tz_update_count;
-			if (count & 1)
-				continue;
-
-			tz->tz_minuteswest = vdso_data->tz_minuteswest;
-			tz->tz_dsttime = vdso_data->tz_dsttime;
-
-			/* Check whether updated, read again if so. */
-			if (count == vdso_data->tz_update_count)
-				break;
-		}
+		do {
+			count = read_seqcount_begin(&vdso->tz_seq);
+			tz->tz_minuteswest = vdso->tz_minuteswest;
+			tz->tz_dsttime = vdso->tz_dsttime;
+		} while (unlikely(read_seqcount_retry(&vdso->tz_seq, count)));
 	}
 
 	if (unlikely(tv == NULL))
-		return 0;
-
-	while (1) {
-		/* Spin until the update finish. */
-		count = vdso_data->tb_update_count;
-		if (count & 1)
-			continue;
-
-		cycles = (get_cycles() - vdso_data->xtime_tod_stamp);
-		ns = (cycles * vdso_data->mult) >> vdso_data->shift;
-		sec = vdso_data->xtime_clock_sec;
-		ns += vdso_data->xtime_clock_nsec;
-		if (ns >= NSEC_PER_SEC) {
-			ns -= NSEC_PER_SEC;
-			sec += 1;
-		}
-
-		/* Check whether updated, read again if so. */
-		if (count == vdso_data->tb_update_count)
-			break;
-	}
+		return ret;
 
-	tv->tv_sec = sec;
-	tv->tv_usec = ns / 1000;
+	do_realtime(vdso, (struct timespec *)tv);
+	tv->tv_usec /= 1000;
 
-	return 0;
+	return ret;
 }
 
 int gettimeofday(struct timeval *tv, struct timezone *tz)
 	__attribute__((weak, alias("__vdso_gettimeofday")));
+
+static struct syscall_return_value vdso_fallback_gettime(long clock,
+							 struct timespec *ts)
+{
+	struct syscall_return_value ret;
+	__asm__ __volatile__ (
+		"swint1"
+		: "=R00" (ret.value), "=R01" (ret.error)
+		: "R10" (__NR_clock_gettime), "R00" (clock), "R01" (ts)
+		: "r2", "r3", "r4", "r5", "r6", "r7",
+		"r8",  "r9", "r11", "r12", "r13", "r14", "r15",
+		"r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
+		"r24", "r25", "r26", "r27", "r28", "r29", "memory");
+	return ret;
+}
+
+struct syscall_return_value __vdso_clock_gettime(clockid_t clock,
+						 struct timespec *ts)
+{
+	struct vdso_data *vdso = (struct vdso_data *)get_datapage();
+	struct syscall_return_value ret = { 0, 0 };
+
+	switch (clock) {
+	case CLOCK_REALTIME:
+		do_realtime(vdso, ts);
+		return ret;
+	case CLOCK_MONOTONIC:
+		do_monotonic(vdso, ts);
+		return ret;
+	case CLOCK_REALTIME_COARSE:
+		do_realtime_coarse(vdso, ts);
+		return ret;
+	case CLOCK_MONOTONIC_COARSE:
+		do_monotonic_coarse(vdso, ts);
+		return ret;
+	default:
+		return vdso_fallback_gettime(clock, ts);
+	}
+}
+
+int clock_gettime(clockid_t clock, struct timespec *ts)
+	__attribute__((weak, alias("__vdso_clock_gettime")));
Signature · 7 lines
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
Signature · 1 line
Henrik Austad
#6 lore raw
Quoted 5 lines · Henrik Austad
Quoted 4 lines · Chris Metcalf
Quoted 4 lines

This change adds support for clock_gettime with CLOCK_REALTIME and CLOCK_MONOTONIC using vDSO. In addition, with this change we switch to use seqlocks instead of integer counters.

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

We also support the *_COARSE clockid_t, for apps that want speed but aren't concerned about fine-grained timestamps; this saves about 20 cycles per call (seehttp://lwn.net/Articles/342018/).

What rev is this based on? When I apply it to Linus latest (aad7fb916), it fails in arch/tile/kernel/time.c and arch/tile/kernel/vdso/vgettimeofday.c (see below)

It's based on the linux-tile tree (on kernel.org under cmetcalf/linux-tile.git), which was last synced up at 3.16-rc7. I'll refresh the tree to pick up Thomas's recent changes that were queued for 3.17. Thanks.

Signature · 2 lines
Chris Metcalf, Tilera Corp.
http://www.tilera.com
#7 lore raw

PATCH v3 1/2 tile: switch to using seqlocks for the vDSO time code

In reply to Henrik Austad
---
 arch/tile/include/asm/vdso.h          |  5 ++--
 arch/tile/kernel/time.c               | 16 +++++-------
 arch/tile/kernel/vdso/vgettimeofday.c | 47 +++++++++++------------------------
 3 files changed, 24 insertions(+), 44 deletions(-)

v2: split apart seqlock and new functionality into separate commits
v3: rebase onto 3.17-rc7

arch/tile/include/asm/vdso.h +3 -2
index 9f6a78d665fa..d64b0d58a7e9 100644
@@ -15,6 +15,7 @@
 #ifndef __TILE_VDSO_H__
 #define __TILE_VDSO_H__
 
+#include <linux/seqlock.h>
 #include <linux/types.h>
 
 /*
@@ -26,8 +27,8 @@
  */
 
 struct vdso_data {
-	__u64 tz_update_count;  /* Timezone atomicity ctr             */
-	__u64 tb_update_count;  /* Timebase atomicity ctr             */
+	seqcount_t tz_seq;	/* Timezone seqlock                   */
+	seqcount_t tb_seq;	/* Timebase seqlock                   */
 	__u64 xtime_tod_stamp;  /* TOD clock for xtime                */
 	__u64 xtime_clock_sec;  /* Kernel time second                 */
 	__u64 xtime_clock_nsec; /* Kernel time nanosecond             */
arch/tile/kernel/time.c +6 -10
index d8fbc289e680..2fe8323db77e 100644
@@ -249,13 +249,10 @@ cycles_t ns2cycles(unsigned long nsecs)
 
 void update_vsyscall_tz(void)
 {
-	/* Userspace gettimeofday will spin while this value is odd. */
-	++vdso_data->tz_update_count;
-	smp_wmb();
+	write_seqcount_begin(&vdso_data->tz_seq);
 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
 	vdso_data->tz_dsttime = sys_tz.tz_dsttime;
-	smp_wmb();
-	++vdso_data->tz_update_count;
+	write_seqcount_end(&vdso_data->tz_seq);
 }
 
 void update_vsyscall(struct timekeeper *tk)
@@ -266,9 +263,8 @@ void update_vsyscall(struct timekeeper *tk)
 	if (clock != &cycle_counter_cs)
 		return;
 
-	/* Userspace gettimeofday will spin while this value is odd. */
-	++vdso_data->tb_update_count;
-	smp_wmb();
+	write_seqcount_begin(&vdso_data->tb_seq);
+
 	vdso_data->xtime_tod_stamp = tk->tkr.cycle_last;
 	vdso_data->xtime_clock_sec = tk->xtime_sec;
 	vdso_data->xtime_clock_nsec = tk->tkr.xtime_nsec;
@@ -276,6 +272,6 @@ void update_vsyscall(struct timekeeper *tk)
 	vdso_data->wtom_clock_nsec = wtm->tv_nsec;
 	vdso_data->mult = tk->tkr.mult;
 	vdso_data->shift = tk->tkr.shift;
-	smp_wmb();
-	++vdso_data->tb_update_count;
+
+	write_seqcount_end(&vdso_data->tb_seq);
 }
arch/tile/kernel/vdso/vgettimeofday.c +15 -32

65 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#8 lore raw

PATCH v3 2/2 tile: add clock_gettime support to vDSO

In reply to Henrik Austad

This change adds support for clock_gettime with CLOCK_REALTIME and CLOCK_MONOTONIC using vDSO. It also updates the vdso struct nomenclature used for the clocks to match the x86 code to keep it easier to update going forward.

We also support the *_COARSE clockid_t, for apps that want speed but aren't concerned about fine-grained timestamps; this saves about 20 cycles per call (see http://lwn.net/Articles/342018/).

---
 arch/tile/include/asm/vdso.h          |  15 ++--
 arch/tile/kernel/time.c               |  45 ++++++++---
 arch/tile/kernel/vdso/vdso.lds.S      |   2 +
 arch/tile/kernel/vdso/vgettimeofday.c | 145 +++++++++++++++++++++++++++++-----
 4 files changed, 172 insertions(+), 35 deletions(-)

v2: use the x86 code as a model to address both John Stultz's
observation about sub-nanosecond updates, as well as to address Thomas Gleixner's observation about minimizing time spent with the seqlock held. Note that I kept the "unlikely" that Thomas doubted would help, just for parallelism with the x86 code.
v3: rebase onto 3.17-rc7

arch/tile/include/asm/vdso.h +10 -5
index d64b0d58a7e9..9b069692153f 100644
@@ -29,13 +29,18 @@
 struct vdso_data {
 	seqcount_t tz_seq;	/* Timezone seqlock                   */
 	seqcount_t tb_seq;	/* Timebase seqlock                   */
-	__u64 xtime_tod_stamp;  /* TOD clock for xtime                */
-	__u64 xtime_clock_sec;  /* Kernel time second                 */
-	__u64 xtime_clock_nsec; /* Kernel time nanosecond             */
-	__u64 wtom_clock_sec;   /* Wall to monotonic clock second     */
-	__u64 wtom_clock_nsec;  /* Wall to monotonic clock nanosecond */
+	__u64 cycle_last;       /* TOD clock for xtime                */
+	__u64 mask;             /* Cycle mask                         */
 	__u32 mult;             /* Cycle to nanosecond multiplier     */
 	__u32 shift;            /* Cycle to nanosecond divisor (power of two) */
+	__u64 wall_time_sec;
+	__u64 wall_time_snsec;
+	__u64 monotonic_time_sec;
+	__u64 monotonic_time_snsec;
+	__u64 wall_time_coarse_sec;
+	__u64 wall_time_coarse_nsec;
+	__u64 monotonic_time_coarse_sec;
+	__u64 monotonic_time_coarse_nsec;
 	__u32 tz_minuteswest;   /* Minutes west of Greenwich          */
 	__u32 tz_dsttime;       /* Type of dst correction             */
 };
arch/tile/kernel/time.c +34 -11

55 lines not on this page · raw message

arch/tile/kernel/vdso/vdso.lds.S +2 -0
index 041cd6c39c83..731529f3f06f 100644
@@ -82,6 +82,8 @@ VERSION
 		__vdso_rt_sigreturn;
 		__vdso_gettimeofday;
 		gettimeofday;
+		__vdso_clock_gettime;
+		clock_gettime;
 	local:*;
 	};
 }
arch/tile/kernel/vdso/vgettimeofday.c +126 -19

174 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Acked-by on v3 2/2
#9 lore raw
Quoted 17 lines · Chris Metcalf

This change adds support for clock_gettime with CLOCK_REALTIME and CLOCK_MONOTONIC using vDSO. It also updates the vdso struct nomenclature used for the clocks to match the x86 code to keep it easier to update going forward.

We also support the *_COARSE clockid_t, for apps that want speed but aren't concerned about fine-grained timestamps; this saves about 20 cycles per call (see http://lwn.net/Articles/342018/).

---
 arch/tile/include/asm/vdso.h          |  15 ++--
 arch/tile/kernel/time.c               |  45 ++++++++---
 arch/tile/kernel/vdso/vdso.lds.S      |   2 +
 arch/tile/kernel/vdso/vgettimeofday.c | 145 +++++++++++++++++++++++++++++-----
 4 files changed, 172 insertions(+), 35 deletions(-)
Quoted 6 lines

v2: use the x86 code as a model to address both John Stultz's
observation about sub-nanosecond updates, as well as to address Thomas Gleixner's observation about minimizing time spent with the seqlock held. Note that I kept the "unlikely" that Thomas doubted would help, just for parallelism with the x86 code.
v3: rebase onto 3.17-rc7

Looks ok to me.

thanks
-john