1616#include <linux/hrtimer.h>
1717#include <linux/capability.h>
1818#include <linux/math64.h>
19+ #include <linux/clocksource.h>
1920#include <asm/timex.h>
2021
2122/*
@@ -26,6 +27,8 @@ unsigned long tick_nsec; /* ACTHZ period (nsec) */
2627u64 tick_length ;
2728static u64 tick_length_base ;
2829
30+ static struct hrtimer leap_timer ;
31+
2932#define MAX_TICKADJ 500 /* microsecs */
3033#define MAX_TICKADJ_SCALED (((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \
3134 NTP_SCALE_SHIFT) / NTP_INTERVAL_FREQ)
@@ -120,64 +123,70 @@ void ntp_clear(void)
120123}
121124
122125/*
123- * this routine handles the overflow of the microsecond field
124- *
125- * The tricky bits of code to handle the accurate clock support
126- * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
127- * They were originally developed for SUN and DEC kernels.
128- * All the kudos should go to Dave for this stuff.
126+ * Leap second processing. If in leap-insert state at the end of the
127+ * day, the system clock is set back one second; if in leap-delete
128+ * state, the system clock is set ahead one second.
129129 */
130- void second_overflow ( void )
130+ static enum hrtimer_restart ntp_leap_second ( struct hrtimer * timer )
131131{
132- s64 time_adj ;
132+ enum hrtimer_restart res = HRTIMER_NORESTART ;
133133
134- /* Bump the maxerror field */
135- time_maxerror += MAXFREQ / NSEC_PER_USEC ;
136- if (time_maxerror > NTP_PHASE_LIMIT ) {
137- time_maxerror = NTP_PHASE_LIMIT ;
138- time_status |= STA_UNSYNC ;
139- }
134+ write_seqlock_irq (& xtime_lock );
140135
141- /*
142- * Leap second processing. If in leap-insert state at the end of the
143- * day, the system clock is set back one second; if in leap-delete
144- * state, the system clock is set ahead one second. The microtime()
145- * routine or external clock driver will insure that reported time is
146- * always monotonic. The ugly divides should be replaced.
147- */
148136 switch (time_state ) {
149137 case TIME_OK :
150- if (time_status & STA_INS )
151- time_state = TIME_INS ;
152- else if (time_status & STA_DEL )
153- time_state = TIME_DEL ;
154138 break ;
155139 case TIME_INS :
156- if (xtime .tv_sec % 86400 == 0 ) {
157- xtime .tv_sec -- ;
158- wall_to_monotonic .tv_sec ++ ;
159- time_state = TIME_OOP ;
160- printk (KERN_NOTICE "Clock: inserting leap second "
161- "23:59:60 UTC\n" );
162- }
140+ xtime .tv_sec -- ;
141+ wall_to_monotonic .tv_sec ++ ;
142+ time_state = TIME_OOP ;
143+ printk (KERN_NOTICE "Clock: "
144+ "inserting leap second 23:59:60 UTC\n" );
145+ leap_timer .expires = ktime_add_ns (leap_timer .expires ,
146+ NSEC_PER_SEC );
147+ res = HRTIMER_RESTART ;
163148 break ;
164149 case TIME_DEL :
165- if ((xtime .tv_sec + 1 ) % 86400 == 0 ) {
166- xtime .tv_sec ++ ;
167- time_tai -- ;
168- wall_to_monotonic .tv_sec -- ;
169- time_state = TIME_WAIT ;
170- printk (KERN_NOTICE "Clock: deleting leap second "
171- "23:59:59 UTC\n" );
172- }
150+ xtime .tv_sec ++ ;
151+ time_tai -- ;
152+ wall_to_monotonic .tv_sec -- ;
153+ time_state = TIME_WAIT ;
154+ printk (KERN_NOTICE "Clock: "
155+ "deleting leap second 23:59:59 UTC\n" );
173156 break ;
174157 case TIME_OOP :
175158 time_tai ++ ;
176159 time_state = TIME_WAIT ;
177- break ;
160+ /* fall through */
178161 case TIME_WAIT :
179162 if (!(time_status & (STA_INS | STA_DEL )))
180163 time_state = TIME_OK ;
164+ break ;
165+ }
166+ update_vsyscall (& xtime , clock );
167+
168+ write_sequnlock_irq (& xtime_lock );
169+
170+ return res ;
171+ }
172+
173+ /*
174+ * this routine handles the overflow of the microsecond field
175+ *
176+ * The tricky bits of code to handle the accurate clock support
177+ * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
178+ * They were originally developed for SUN and DEC kernels.
179+ * All the kudos should go to Dave for this stuff.
180+ */
181+ void second_overflow (void )
182+ {
183+ s64 time_adj ;
184+
185+ /* Bump the maxerror field */
186+ time_maxerror += MAXFREQ / NSEC_PER_USEC ;
187+ if (time_maxerror > NTP_PHASE_LIMIT ) {
188+ time_maxerror = NTP_PHASE_LIMIT ;
189+ time_status |= STA_UNSYNC ;
181190 }
182191
183192 /*
@@ -268,7 +277,7 @@ static inline void notify_cmos_timer(void) { }
268277int do_adjtimex (struct timex * txc )
269278{
270279 struct timespec ts ;
271- long save_adjust ;
280+ long save_adjust , sec ;
272281 int result ;
273282
274283 /* In order to modify anything, you gotta be super-user! */
@@ -289,6 +298,10 @@ int do_adjtimex(struct timex *txc)
289298 txc -> tick > 1100000 /USER_HZ )
290299 return - EINVAL ;
291300
301+ if (time_state != TIME_OK && txc -> modes & ADJ_STATUS )
302+ hrtimer_cancel (& leap_timer );
303+ getnstimeofday (& ts );
304+
292305 write_seqlock_irq (& xtime_lock );
293306
294307 /* Save for later - semantics of adjtime is to return old value */
@@ -305,6 +318,34 @@ int do_adjtimex(struct timex *txc)
305318 /* only set allowed bits */
306319 time_status &= STA_RONLY ;
307320 time_status |= txc -> status & ~STA_RONLY ;
321+
322+ switch (time_state ) {
323+ case TIME_OK :
324+ start_timer :
325+ sec = ts .tv_sec ;
326+ if (time_status & STA_INS ) {
327+ time_state = TIME_INS ;
328+ sec += 86400 - sec % 86400 ;
329+ hrtimer_start (& leap_timer , ktime_set (sec , 0 ), HRTIMER_MODE_ABS );
330+ } else if (time_status & STA_DEL ) {
331+ time_state = TIME_DEL ;
332+ sec += 86400 - (sec + 1 ) % 86400 ;
333+ hrtimer_start (& leap_timer , ktime_set (sec , 0 ), HRTIMER_MODE_ABS );
334+ }
335+ break ;
336+ case TIME_INS :
337+ case TIME_DEL :
338+ time_state = TIME_OK ;
339+ goto start_timer ;
340+ break ;
341+ case TIME_WAIT :
342+ if (!(time_status & (STA_INS | STA_DEL )))
343+ time_state = TIME_OK ;
344+ break ;
345+ case TIME_OOP :
346+ hrtimer_restart (& leap_timer );
347+ break ;
348+ }
308349 }
309350
310351 if (txc -> modes & ADJ_NANO )
@@ -384,7 +425,6 @@ int do_adjtimex(struct timex *txc)
384425 txc -> stbcnt = 0 ;
385426 write_sequnlock_irq (& xtime_lock );
386427
387- getnstimeofday (& ts );
388428 txc -> time .tv_sec = ts .tv_sec ;
389429 txc -> time .tv_usec = ts .tv_nsec ;
390430 if (!(time_status & STA_NANO ))
@@ -402,3 +442,10 @@ static int __init ntp_tick_adj_setup(char *str)
402442}
403443
404444__setup ("ntp_tick_adj=" , ntp_tick_adj_setup );
445+
446+ void __init ntp_init (void )
447+ {
448+ ntp_clear ();
449+ hrtimer_init (& leap_timer , CLOCK_REALTIME , HRTIMER_MODE_ABS );
450+ leap_timer .function = ntp_leap_second ;
451+ }
0 commit comments