Skip to contentSkip to conversation

Fix discrepancy between VDSO based gettimeofday() and sys_gettimeofday().

PATCH In review Tony Breeds 2 participants 2 messages Aug 31, 2007 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix discrepancy between VDSO based gettimeofday() and sys_gettimeofday().

On platforms that copy sys_tz into the vdso (currently only x86_64, soon to include powerpc), it is possible for the vdso to get out of sync if a user calls (admittedly unusual) settimeofday(NULL, ptr).

This patch adds a hook for architectures that set CONFIG_GENERIC_TIME_VSYSCALL to ensure when sys_tz is updated they can also updater their copy in the vdso.

Cc Andi Kleen <[email protected]>
Cc Tony Luck <[email protected]>
Cc John Stultz <[email protected]>

---

Only compile tested for IA64
Testcase at: http://bakeyournoodle.com/~tony/settimeofday-0.0/
I guess this is aimed at 2.6.24

 arch/ia64/kernel/time.c       |    5 +++++
 arch/x86_64/kernel/vsyscall.c |   11 ++++++++++-
 include/linux/clocksource.h   |    5 +++++
 kernel/time.c                 |    2 ++
 4 files changed, 22 insertions(+), 1 deletions(-)
arch/ia64/kernel/time.c +5 -0
index 98cfc90..2bb8421 100644
@@ -371,6 +371,11 @@ ia64_setup_printk_clock(void)
 		ia64_printk_clock = ia64_itc_printk_clock;
 }
 
+/* IA64 doesn't cache the timezone */
+void update_vsyscall_tz(void)
+{
+}
+
 void update_vsyscall(struct timespec *wall, struct clocksource *c)
 {
         unsigned long flags;
arch/x86_64/kernel/vsyscall.c +10 -1
index 06c3494..4ff78b0 100644
@@ -66,6 +66,16 @@ struct vsyscall_gtod_data __vsyscall_gtod_data __section_vsyscall_gtod_data =
 	.sysctl_enabled = 1,
 };
 
+void update_vsyscall_tz(void)
+{
+	unsigned long flags;
+
+	write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
+	/* sys_tz has changed */
+	vsyscall_gtod_data.sys_tz = sys_tz;
+	write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
+}
+
 void update_vsyscall(struct timespec *wall_time, struct clocksource *clock)
 {
 	unsigned long flags;
@@ -79,7 +89,6 @@ void update_vsyscall(struct timespec *wall_time, struct clocksource *clock)
 	vsyscall_gtod_data.clock.shift = clock->shift;
 	vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec;
 	vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
-	vsyscall_gtod_data.sys_tz = sys_tz;
 	vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
 	vsyscall_gtod_data.wall_to_monotonic = wall_to_monotonic;
 	write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
include/linux/clocksource.h +5 -0
index 16ea337..107787a 100644
@@ -221,10 +221,15 @@ extern void clocksource_resume(void);
 
 #ifdef CONFIG_GENERIC_TIME_VSYSCALL
 extern void update_vsyscall(struct timespec *ts, struct clocksource *c);
+extern void update_vsyscall_tz(void);
 #else
 static inline void update_vsyscall(struct timespec *ts, struct clocksource *c)
 {
 }
+
+static inline void update_vsyscall_tz(void)
+{
+}
 #endif
 
 #endif /* _LINUX_CLOCKSOURCE_H */
kernel/time.c +2 -0
index 2289a8d..9a4873e 100644
@@ -30,6 +30,7 @@
 #include <linux/module.h>
 #include <linux/timex.h>
 #include <linux/capability.h>
+#include <linux/clocksource.h>
 #include <linux/errno.h>
 #include <linux/syscalls.h>
 #include <linux/security.h>
@@ -163,6 +164,7 @@ int do_sys_settimeofday(struct timespec *tv, struct timezone *tz)
 	if (tz) {
 		/* SMP safe, global irq locking makes it work. */
 		sys_tz = *tz;
+		update_vsyscall_tz();
 		if (firsttime) {
 			firsttime = 0;
 			if (!tv)
Signature · 6 lines
1.5.2.5

Yours Tony

  linux.conf.au        http://linux.conf.au/ || http://lca2008.linux.org.au/
  Jan 28 - Feb 02 2008 The Australian Linux Technical Conference!
#1 lore raw
Quoted 6 lines · Tony Breeds

On platforms that copy sys_tz into the vdso (currently only x86_64, soon to include powerpc), it is possible for the vdso to get out of sync if a user calls (admittedly unusual) settimeofday(NULL, ptr).

This patch adds a hook for architectures that set CONFIG_GENERIC_TIME_VSYSCALL to ensure when sys_tz is updated they can also updater their copy in the vdso.

Ah. Yea, so after a sys_tz update, until the next timer tick, its possible to get the stale value. Good catch.

One little comment below.

Quoted 54 lines
Cc Andi Kleen <[email protected]>
Cc Tony Luck <[email protected]>
Cc John Stultz <[email protected]>

---

Only compile tested for IA64
Testcase at: http://bakeyournoodle.com/~tony/settimeofday-0.0/
I guess this is aimed at 2.6.24

 arch/ia64/kernel/time.c       |    5 +++++
 arch/x86_64/kernel/vsyscall.c |   11 ++++++++++-
 include/linux/clocksource.h   |    5 +++++
 kernel/time.c                 |    2 ++
 4 files changed, 22 insertions(+), 1 deletions(-)
arch/ia64/kernel/time.c +5 -0
index 98cfc90..2bb8421 100644
@@ -371,6 +371,11 @@ ia64_setup_printk_clock(void)
 		ia64_printk_clock = ia64_itc_printk_clock;
 }
 
+/* IA64 doesn't cache the timezone */
+void update_vsyscall_tz(void)
+{
+}
+
 void update_vsyscall(struct timespec *wall, struct clocksource *c)
 {
         unsigned long flags;
arch/x86_64/kernel/vsyscall.c +10 -0
index 06c3494..4ff78b0 100644
@@ -66,6 +66,16 @@ struct vsyscall_gtod_data __vsyscall_gtod_data __section_vsyscall_gtod_data =
 	.sysctl_enabled = 1,
 };
 
+void update_vsyscall_tz(void)
+{
+	unsigned long flags;
+
+	write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
+	/* sys_tz has changed */
+	vsyscall_gtod_data.sys_tz = sys_tz;
+	write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
+}
+
 void update_vsyscall(struct timespec *wall_time, struct clocksource *clock)
 {
 	unsigned long flags;

arch/x86_64/kernel/vsyscall.c void update_vsyscall(struct timespec *wall_time, struct clocksource *clock) · line 94

Quoted 7 lines
arch/x86_64/kernel/vsyscall.c +0 -1
@@ -79,7 +89,6 @@ void update_vsyscall(struct timespec *wall_time, struct clocksource *clock)
 	vsyscall_gtod_data.clock.shift = clock->shift;
 	vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec;
 	vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
-	vsyscall_gtod_data.sys_tz = sys_tz;
 	vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
 	vsyscall_gtod_data.wall_to_monotonic = wall_to_monotonic;
 	write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);

Hmm.. I might just extend update_vsyscall() so it takes a timezone and then it only updates bits that have non-null pointers. Then call it from sys_settimeofday(). This cuts down the functions each arch has to implement, and also makes sure we get the full update each tick.

Alternatively update_vsyscall_tz() could live in timekeeping.c, grab the appropriate locks and static data and call update_vsyscall(), also minimizing the per-arch logic.

But your way is correct, and fixes a bug, so I won't stand in the way of it.

-john