From d1b1b60065e8b8f547cc44874509050a8c8d8b7b Mon Sep 17 00:00:00 2001 From: Neel Natu Date: Tue, 20 Jan 2015 03:54:30 +0000 Subject: [PATCH] Update the vdso timehands only via tc_windup(). Prior to this change CLOCK_MONOTONIC could go backwards when the timecounter hardware was changed via 'sysctl kern.timecounter.hardware'. This happened because the vdso timehands update was missing the special treatment in tc_windup() when changing timecounters. Reviewed by: kib --- sys/kern/kern_tc.c | 15 +++++++++++---- sys/kern/subr_dummy_vdso_tc.c | 5 +++-- sys/sys/vdso.h | 8 ++++++-- sys/x86/x86/tsc.c | 13 +++++++------ 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/sys/kern/kern_tc.c b/sys/kern/kern_tc.c index 7a6b4e6d959b..1c29041b1f29 100644 --- a/sys/kern/kern_tc.c +++ b/sys/kern/kern_tc.c @@ -1424,7 +1424,15 @@ sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS) (void)newtc->tc_get_timecount(newtc); timecounter = newtc; - timekeep_push_vdso(); + + /* + * The vdso timehands update is deferred until the next + * 'tc_windup()'. + * + * This is prudent given that 'timekeep_push_vdso()' does not + * use any locking and that it can be called in hard interrupt + * context via 'tc_windup()'. + */ return (0); } return (EINVAL); @@ -1982,7 +1990,6 @@ sysctl_fast_gettime(SYSCTL_HANDLER_ARGS) if (error != 0) return (error); vdso_th_enable = old_vdso_th_enable; - timekeep_push_vdso(); return (0); } SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime, @@ -2002,7 +2009,7 @@ tc_fill_vdso_timehands(struct vdso_timehands *vdso_th) vdso_th->th_counter_mask = th->th_counter->tc_counter_mask; vdso_th->th_offset = th->th_offset; vdso_th->th_boottime = boottimebin; - enabled = cpu_fill_vdso_timehands(vdso_th); + enabled = cpu_fill_vdso_timehands(vdso_th, th->th_counter); if (!vdso_th_enable) enabled = 0; return (enabled); @@ -2024,7 +2031,7 @@ tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32) *(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac; vdso_th32->th_boottime.sec = boottimebin.sec; *(uint64_t *)&vdso_th32->th_boottime.frac[0] = boottimebin.frac; - enabled = cpu_fill_vdso_timehands32(vdso_th32); + enabled = cpu_fill_vdso_timehands32(vdso_th32, th->th_counter); if (!vdso_th_enable) enabled = 0; return (enabled); diff --git a/sys/kern/subr_dummy_vdso_tc.c b/sys/kern/subr_dummy_vdso_tc.c index 9c845014f50f..d1c36b74ead0 100644 --- a/sys/kern/subr_dummy_vdso_tc.c +++ b/sys/kern/subr_dummy_vdso_tc.c @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD$"); #include uint32_t -cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th) +cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th, struct timecounter *tc) { return (0); @@ -41,7 +41,8 @@ cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th) #ifdef COMPAT_FREEBSD32 uint32_t -cpu_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32) +cpu_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32, + struct timecounter *tc) { return (0); diff --git a/sys/sys/vdso.h b/sys/sys/vdso.h index f584315b9561..d905304ac68a 100644 --- a/sys/sys/vdso.h +++ b/sys/sys/vdso.h @@ -69,6 +69,8 @@ int __vdso_gettimekeep(struct vdso_timekeep **tk); #ifdef _KERNEL +struct timecounter; + void timekeep_push_vdso(void); uint32_t tc_fill_vdso_timehands(struct vdso_timehands *vdso_th); @@ -81,7 +83,8 @@ uint32_t tc_fill_vdso_timehands(struct vdso_timehands *vdso_th); * global sysctl enable override is handled by machine-independed code * after cpu_fill_vdso_timehands() call is made. */ -uint32_t cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th); +uint32_t cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th, + struct timecounter *tc); #define VDSO_TH_NUM 4 @@ -110,7 +113,8 @@ struct vdso_timekeep32 { }; uint32_t tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32); -uint32_t cpu_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32); +uint32_t cpu_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32, + struct timecounter *tc); #endif #endif diff --git a/sys/x86/x86/tsc.c b/sys/x86/x86/tsc.c index c84f28a3e603..e2ae73517fa1 100644 --- a/sys/x86/x86/tsc.c +++ b/sys/x86/x86/tsc.c @@ -720,21 +720,22 @@ tsc_get_timecount_low_mfence(struct timecounter *tc) } uint32_t -cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th) +cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th, struct timecounter *tc) { - vdso_th->th_x86_shift = (int)(intptr_t)timecounter->tc_priv; + vdso_th->th_x86_shift = (int)(intptr_t)tc->tc_priv; bzero(vdso_th->th_res, sizeof(vdso_th->th_res)); - return (timecounter == &tsc_timecounter); + return (tc == &tsc_timecounter); } #ifdef COMPAT_FREEBSD32 uint32_t -cpu_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32) +cpu_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32, + struct timecounter *tc) { - vdso_th32->th_x86_shift = (int)(intptr_t)timecounter->tc_priv; + vdso_th32->th_x86_shift = (int)(intptr_t)tc->tc_priv; bzero(vdso_th32->th_res, sizeof(vdso_th32->th_res)); - return (timecounter == &tsc_timecounter); + return (tc == &tsc_timecounter); } #endif