cpumask: avoid dereferencing struct cpumask
[safe/jmp/linux-2.6] / kernel / time / ntp.c
index 580a350..4800f93 100644 (file)
@@ -71,7 +71,8 @@ static long                   time_reftime;
 
 long                           time_adjust;
 
-static long                    ntp_tick_adj;
+/* constant (boot-param configurable) NTP tick adjustment (upscaled)   */
+static s64                     ntp_tick_adj;
 
 /*
  * NTP methods:
@@ -89,7 +90,7 @@ static void ntp_update_frequency(void)
        second_length            = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ)
                                                << NTP_SCALE_SHIFT;
 
-       second_length           += (s64)ntp_tick_adj << NTP_SCALE_SHIFT;
+       second_length           += ntp_tick_adj;
        second_length           += time_freq;
 
        tick_nsec                = div_u64(second_length, HZ) >> NTP_SCALE_SHIFT;
@@ -142,7 +143,7 @@ static void ntp_update_offset(long offset)
         * and in which mode (PLL or FLL).
         */
        secs = xtime.tv_sec - time_reftime;
-       if (unlikely(time_status & STA_FREQHOLD || time_reftime == 0))
+       if (unlikely(time_status & STA_FREQHOLD))
                secs = 0;
 
        time_reftime = xtime.tv_sec;
@@ -193,8 +194,7 @@ static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
        case TIME_OK:
                break;
        case TIME_INS:
-               xtime.tv_sec--;
-               wall_to_monotonic.tv_sec++;
+               timekeeping_leap_insert(-1);
                time_state = TIME_OOP;
                printk(KERN_NOTICE
                        "Clock: inserting leap second 23:59:60 UTC\n");
@@ -202,9 +202,8 @@ static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
                res = HRTIMER_RESTART;
                break;
        case TIME_DEL:
-               xtime.tv_sec++;
+               timekeeping_leap_insert(1);
                time_tai--;
-               wall_to_monotonic.tv_sec--;
                time_state = TIME_WAIT;
                printk(KERN_NOTICE
                        "Clock: deleting leap second 23:59:59 UTC\n");
@@ -218,7 +217,6 @@ static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
                        time_state = TIME_OK;
                break;
        }
-       update_vsyscall(&xtime, clock);
 
        write_sequnlock(&xtime_lock);
 
@@ -235,7 +233,7 @@ static enum hrtimer_restart ntp_leap_second(struct hrtimer *timer)
  */
 void second_overflow(void)
 {
-       s64 time_adj;
+       s64 delta;
 
        /* Bump the maxerror field */
        time_maxerror += MAXFREQ / NSEC_PER_USEC;
@@ -248,10 +246,11 @@ void second_overflow(void)
         * Compute the phase adjustment for the next second. The offset is
         * reduced by a fixed factor times the time constant.
         */
-       tick_length     = tick_length_base;
-       time_adj        = shift_right(time_offset, SHIFT_PLL + time_constant);
-       time_offset     -= time_adj;
-       tick_length     += time_adj;
+       tick_length      = tick_length_base;
+
+       delta            = shift_right(time_offset, SHIFT_PLL + time_constant);
+       time_offset     -= delta;
+       tick_length     += delta;
 
        if (!time_adjust)
                return;
@@ -333,6 +332,114 @@ static inline void notify_cmos_timer(void) { }
 #endif
 
 /*
+ * Start the leap seconds timer:
+ */
+static inline void ntp_start_leap_timer(struct timespec *ts)
+{
+       long now = ts->tv_sec;
+
+       if (time_status & STA_INS) {
+               time_state = TIME_INS;
+               now += 86400 - now % 86400;
+               hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
+
+               return;
+       }
+
+       if (time_status & STA_DEL) {
+               time_state = TIME_DEL;
+               now += 86400 - (now + 1) % 86400;
+               hrtimer_start(&leap_timer, ktime_set(now, 0), HRTIMER_MODE_ABS);
+       }
+}
+
+/*
+ * Propagate a new txc->status value into the NTP state:
+ */
+static inline void process_adj_status(struct timex *txc, struct timespec *ts)
+{
+       if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
+               time_state = TIME_OK;
+               time_status = STA_UNSYNC;
+       }
+
+       /*
+        * If we turn on PLL adjustments then reset the
+        * reference time to current time.
+        */
+       if (!(time_status & STA_PLL) && (txc->status & STA_PLL))
+               time_reftime = xtime.tv_sec;
+
+       /* only set allowed bits */
+       time_status &= STA_RONLY;
+       time_status |= txc->status & ~STA_RONLY;
+
+       switch (time_state) {
+       case TIME_OK:
+               ntp_start_leap_timer(ts);
+               break;
+       case TIME_INS:
+       case TIME_DEL:
+               time_state = TIME_OK;
+               ntp_start_leap_timer(ts);
+       case TIME_WAIT:
+               if (!(time_status & (STA_INS | STA_DEL)))
+                       time_state = TIME_OK;
+               break;
+       case TIME_OOP:
+               hrtimer_restart(&leap_timer);
+               break;
+       }
+}
+/*
+ * Called with the xtime lock held, so we can access and modify
+ * all the global NTP state:
+ */
+static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts)
+{
+       if (txc->modes & ADJ_STATUS)
+               process_adj_status(txc, ts);
+
+       if (txc->modes & ADJ_NANO)
+               time_status |= STA_NANO;
+
+       if (txc->modes & ADJ_MICRO)
+               time_status &= ~STA_NANO;
+
+       if (txc->modes & ADJ_FREQUENCY) {
+               time_freq = txc->freq * PPM_SCALE;
+               time_freq = min(time_freq, MAXFREQ_SCALED);
+               time_freq = max(time_freq, -MAXFREQ_SCALED);
+       }
+
+       if (txc->modes & ADJ_MAXERROR)
+               time_maxerror = txc->maxerror;
+
+       if (txc->modes & ADJ_ESTERROR)
+               time_esterror = txc->esterror;
+
+       if (txc->modes & ADJ_TIMECONST) {
+               time_constant = txc->constant;
+               if (!(time_status & STA_NANO))
+                       time_constant += 4;
+               time_constant = min(time_constant, (long)MAXTC);
+               time_constant = max(time_constant, 0l);
+       }
+
+       if (txc->modes & ADJ_TAI && txc->constant > 0)
+               time_tai = txc->constant;
+
+       if (txc->modes & ADJ_OFFSET)
+               ntp_update_offset(txc->offset);
+
+       if (txc->modes & ADJ_TICK)
+               tick_usec = txc->tick;
+
+       if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
+               ntp_update_frequency();
+}
+
+/*
  * adjtimex mainly allows reading (and writing, if superuser) of
  * kernel time-keeping variables. used by xntpd.
  */
@@ -361,7 +468,7 @@ int do_adjtimex(struct timex *txc)
                if (txc->modes & ADJ_TICK &&
                    (txc->tick <  900000/USER_HZ ||
                     txc->tick > 1100000/USER_HZ))
-                               return -EINVAL;
+                       return -EINVAL;
 
                if (txc->modes & ADJ_STATUS && time_state != TIME_OK)
                        hrtimer_cancel(&leap_timer);
@@ -371,7 +478,6 @@ int do_adjtimex(struct timex *txc)
 
        write_seqlock_irq(&xtime_lock);
 
-       /* If there are input parameters, then process them */
        if (txc->modes & ADJ_ADJTIME) {
                long save_adjust = time_adjust;
 
@@ -381,98 +487,24 @@ int do_adjtimex(struct timex *txc)
                        ntp_update_frequency();
                }
                txc->offset = save_adjust;
-               goto adj_done;
-       }
-       if (txc->modes) {
-               long sec;
-
-               if (txc->modes & ADJ_STATUS) {
-                       if ((time_status & STA_PLL) &&
-                           !(txc->status & STA_PLL)) {
-                               time_state = TIME_OK;
-                               time_status = STA_UNSYNC;
-                       }
-                       /* only set allowed bits */
-                       time_status &= STA_RONLY;
-                       time_status |= txc->status & ~STA_RONLY;
-
-                       switch (time_state) {
-                       case TIME_OK:
-                       start_timer:
-                               sec = ts.tv_sec;
-                               if (time_status & STA_INS) {
-                                       time_state = TIME_INS;
-                                       sec += 86400 - sec % 86400;
-                                       hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
-                               } else if (time_status & STA_DEL) {
-                                       time_state = TIME_DEL;
-                                       sec += 86400 - (sec + 1) % 86400;
-                                       hrtimer_start(&leap_timer, ktime_set(sec, 0), HRTIMER_MODE_ABS);
-                               }
-                               break;
-                       case TIME_INS:
-                       case TIME_DEL:
-                               time_state = TIME_OK;
-                               goto start_timer;
-                               break;
-                       case TIME_WAIT:
-                               if (!(time_status & (STA_INS | STA_DEL)))
-                                       time_state = TIME_OK;
-                               break;
-                       case TIME_OOP:
-                               hrtimer_restart(&leap_timer);
-                               break;
-                       }
-               }
-
-               if (txc->modes & ADJ_NANO)
-                       time_status |= STA_NANO;
-               if (txc->modes & ADJ_MICRO)
-                       time_status &= ~STA_NANO;
-
-               if (txc->modes & ADJ_FREQUENCY) {
-                       time_freq = (s64)txc->freq * PPM_SCALE;
-                       time_freq = min(time_freq, MAXFREQ_SCALED);
-                       time_freq = max(time_freq, -MAXFREQ_SCALED);
-               }
-
-               if (txc->modes & ADJ_MAXERROR)
-                       time_maxerror = txc->maxerror;
-               if (txc->modes & ADJ_ESTERROR)
-                       time_esterror = txc->esterror;
-
-               if (txc->modes & ADJ_TIMECONST) {
-                       time_constant = txc->constant;
-                       if (!(time_status & STA_NANO))
-                               time_constant += 4;
-                       time_constant = min(time_constant, (long)MAXTC);
-                       time_constant = max(time_constant, 0l);
-               }
-
-               if (txc->modes & ADJ_TAI && txc->constant > 0)
-                       time_tai = txc->constant;
-
-               if (txc->modes & ADJ_OFFSET)
-                       ntp_update_offset(txc->offset);
-               if (txc->modes & ADJ_TICK)
-                       tick_usec = txc->tick;
+       } else {
 
-               if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
-                       ntp_update_frequency();
-       }
+               /* If there are input parameters, then process them: */
+               if (txc->modes)
+                       process_adjtimex_modes(txc, &ts);
 
-       txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
+               txc->offset = shift_right(time_offset * NTP_INTERVAL_FREQ,
                                  NTP_SCALE_SHIFT);
-       if (!(time_status & STA_NANO))
-               txc->offset /= NSEC_PER_USEC;
+               if (!(time_status & STA_NANO))
+                       txc->offset /= NSEC_PER_USEC;
+       }
 
-adj_done:
        result = time_state;    /* mostly `TIME_OK' */
        if (time_status & (STA_UNSYNC|STA_CLOCKERR))
                result = TIME_ERROR;
 
        txc->freq          = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
-                                        (s64)PPM_SCALE_INV, NTP_SCALE_SHIFT);
+                                        PPM_SCALE_INV, NTP_SCALE_SHIFT);
        txc->maxerror      = time_maxerror;
        txc->esterror      = time_esterror;
        txc->status        = time_status;
@@ -491,6 +523,7 @@ adj_done:
        txc->calcnt        = 0;
        txc->errcnt        = 0;
        txc->stbcnt        = 0;
+
        write_sequnlock_irq(&xtime_lock);
 
        txc->time.tv_sec = ts.tv_sec;
@@ -506,6 +539,8 @@ adj_done:
 static int __init ntp_tick_adj_setup(char *str)
 {
        ntp_tick_adj = simple_strtol(str, NULL, 0);
+       ntp_tick_adj <<= NTP_SCALE_SHIFT;
+
        return 1;
 }