linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86-64: Add time to vDSO
@ 2011-05-19 13:24 Andy Lutomirski
  2011-05-19 13:53 ` Andi Kleen
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Lutomirski @ 2011-05-19 13:24 UTC (permalink / raw)
  To: x86, Ingo Molnar, Thomas Gleixner, linux-kernel
  Cc: Andi Kleen, Peter Zijlstra, Andy Lutomirski

The only fast implementation of time(2) we expose is through the
vsyscall page and we want to get userspace to stop using the
vsyscall page.  So make it available through the vDSO as well.

This is essentially a cut-n-paste job.

Signed-off-by: Andy Lutomirski <luto@mit.edu>
---

[Some cc's dropped from original thread because they probably only cared
 about the rdtsc stuff.]

This applies on top of my rdtsc patchkit.  It might apply without it
but it certainly won't work because it needs the new VVAR macro.

It survives light testing.  The return value of the new time function agrees
with glibc's time(2) and it takes about 3ns to call on Sandy Bridge.
(glibc's function takes twice as long I haven't tried to figure out why.)

 arch/x86/vdso/vclock_gettime.c |   35 ++++++++++++++++++++++++++++++++++-
 arch/x86/vdso/vdso.lds.S       |    2 ++
 2 files changed, 36 insertions(+), 1 deletions(-)

diff --git a/arch/x86/vdso/vclock_gettime.c b/arch/x86/vdso/vclock_gettime.c
index 28b2c00..e6e9f90 100644
--- a/arch/x86/vdso/vclock_gettime.c
+++ b/arch/x86/vdso/vclock_gettime.c
@@ -2,7 +2,7 @@
  * Copyright 2006 Andi Kleen, SUSE Labs.
  * Subject to the GNU Public License, v.2
  *
- * Fast user context implementation of clock_gettime and gettimeofday.
+ * Fast user context implementation of clock_gettime, gettimeofday, and time.
  *
  * The code should have no internal unresolved relocations.
  * Check with readelf after changing.
@@ -160,3 +160,36 @@ notrace int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
 }
 int gettimeofday(struct timeval *, struct timezone *)
 	__attribute__((weak, alias("__vdso_gettimeofday")));
+
+/* This will break when the xtime seconds get inaccurate, but that is
+ * unlikely */
+
+static __always_inline long time_syscall(long *t)
+{
+	long secs;
+	asm volatile("syscall"
+		     : "=a" (secs)
+		     : "0" (__NR_time), "D" (t) : "cc", "r11", "cx", "memory");
+	return secs;
+}
+
+notrace time_t __vdso_time(time_t *t)
+{
+	unsigned seq;
+	time_t result;
+	if (unlikely(!VVAR(vsyscall_gtod_data).sysctl_enabled))
+		return time_syscall(t);
+
+	do {
+		seq = read_seqbegin(&VVAR(vsyscall_gtod_data).lock);
+
+		result = VVAR(vsyscall_gtod_data).wall_time_sec;
+
+	} while (read_seqretry(&VVAR(vsyscall_gtod_data).lock, seq));
+
+	if (t)
+		*t = result;
+	return result;
+}
+int time(time_t *t)
+	__attribute__((weak, alias("__vdso_time")));
diff --git a/arch/x86/vdso/vdso.lds.S b/arch/x86/vdso/vdso.lds.S
index 81f2500..b96b267 100644
--- a/arch/x86/vdso/vdso.lds.S
+++ b/arch/x86/vdso/vdso.lds.S
@@ -23,6 +23,8 @@ VERSION {
 		__vdso_gettimeofday;
 		getcpu;
 		__vdso_getcpu;
+		time;
+		__vdso_time;
 	local: *;
 	};
 }
-- 
1.7.5.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] x86-64: Add time to vDSO
  2011-05-19 13:24 [PATCH] x86-64: Add time to vDSO Andy Lutomirski
@ 2011-05-19 13:53 ` Andi Kleen
  2011-05-19 14:11   ` Andrew Lutomirski
  0 siblings, 1 reply; 3+ messages in thread
From: Andi Kleen @ 2011-05-19 13:53 UTC (permalink / raw)
  To: Andy Lutomirski
  Cc: x86, Ingo Molnar, Thomas Gleixner, linux-kernel, Andi Kleen,
	Peter Zijlstra

On Thu, May 19, 2011 at 09:24:48AM -0400, Andy Lutomirski wrote:
> The only fast implementation of time(2) we expose is through the
> vsyscall page and we want to get userspace to stop using the
> vsyscall page.  So make it available through the vDSO as well.

User space can just use vgettimeofday() and only take the seconds.
Adding time() too was a mistake back then.

And BTW just for seconds alone you wouldn't need the seqlock.

-Andi

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] x86-64: Add time to vDSO
  2011-05-19 13:53 ` Andi Kleen
@ 2011-05-19 14:11   ` Andrew Lutomirski
  0 siblings, 0 replies; 3+ messages in thread
From: Andrew Lutomirski @ 2011-05-19 14:11 UTC (permalink / raw)
  To: Andi Kleen
  Cc: x86, Ingo Molnar, Thomas Gleixner, linux-kernel, Peter Zijlstra

On Thu, May 19, 2011 at 9:53 AM, Andi Kleen <andi@firstfloor.org> wrote:
> On Thu, May 19, 2011 at 09:24:48AM -0400, Andy Lutomirski wrote:
>> The only fast implementation of time(2) we expose is through the
>> vsyscall page and we want to get userspace to stop using the
>> vsyscall page.  So make it available through the vDSO as well.
>
> User space can just use vgettimeofday() and only take the seconds.
> Adding time() too was a mistake back then.
>
> And BTW just for seconds alone you wouldn't need the seqlock.

True.  I just copied this from the vsyscall code.  I can optimize it,
but maybe that should be a followup patch.  I'd rather not duplicate
code like this, but that would need some additional infrastructure.

Note that this is five times as fast as vgettimeofday on Sandy Bridge
with my patches.  Without the patches it's seven times as fast, and on
older machines it's more like 100 times faster.  I'm not sure that
having glibc use CLOCK_REALTIME_COARSE is a good idea -- some day we
might want to get rid of the periodic tick even under load and then
time() should do something clever.

--Andy

>
> -Andi
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-05-19 14:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-19 13:24 [PATCH] x86-64: Add time to vDSO Andy Lutomirski
2011-05-19 13:53 ` Andi Kleen
2011-05-19 14:11   ` Andrew Lutomirski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).