ntp: improve adjtimex frequency rounding
[safe/jmp/linux-2.6] / kernel / time / clocksource.c
index 193a079..093d4ac 100644 (file)
@@ -55,16 +55,18 @@ static DEFINE_SPINLOCK(clocksource_lock);
 static char override_name[32];
 static int finished_booting;
 
-/* clocksource_done_booting - Called near the end of bootup
+/* clocksource_done_booting - Called near the end of core bootup
  *
- * Hack to avoid lots of clocksource churn at boot time
+ * Hack to avoid lots of clocksource churn at boot time.
+ * We use fs_initcall because we want this to start before
+ * device_initcall but after subsys_initcall.
  */
 static int __init clocksource_done_booting(void)
 {
        finished_booting = 1;
        return 0;
 }
-late_initcall(clocksource_done_booting);
+fs_initcall(clocksource_done_booting);
 
 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
 static LIST_HEAD(watchdog_list);
@@ -72,22 +74,23 @@ static struct clocksource *watchdog;
 static struct timer_list watchdog_timer;
 static DEFINE_SPINLOCK(watchdog_lock);
 static cycle_t watchdog_last;
+static unsigned long watchdog_resumed;
+
 /*
- * Interval: 0.5sec Treshold: 0.0625s
+ * Interval: 0.5sec Threshold: 0.0625s
  */
 #define WATCHDOG_INTERVAL (HZ >> 1)
-#define WATCHDOG_TRESHOLD (NSEC_PER_SEC >> 4)
+#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
 
 static void clocksource_ratewd(struct clocksource *cs, int64_t delta)
 {
-       if (delta > -WATCHDOG_TRESHOLD && delta < WATCHDOG_TRESHOLD)
+       if (delta > -WATCHDOG_THRESHOLD && delta < WATCHDOG_THRESHOLD)
                return;
 
        printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
               cs->name, delta);
        cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
        clocksource_change_rating(cs, 0);
-       cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
        list_del(&cs->wd_list);
 }
 
@@ -96,15 +99,24 @@ static void clocksource_watchdog(unsigned long data)
        struct clocksource *cs, *tmp;
        cycle_t csnow, wdnow;
        int64_t wd_nsec, cs_nsec;
+       int resumed;
 
        spin_lock(&watchdog_lock);
 
+       resumed = test_and_clear_bit(0, &watchdog_resumed);
+
        wdnow = watchdog->read();
        wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask);
        watchdog_last = wdnow;
 
        list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
                csnow = cs->read();
+
+               if (unlikely(resumed)) {
+                       cs->wd_last = csnow;
+                       continue;
+               }
+
                /* Initialized ? */
                if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
                        if ((cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
@@ -129,11 +141,24 @@ static void clocksource_watchdog(unsigned long data)
        }
 
        if (!list_empty(&watchdog_list)) {
-               __mod_timer(&watchdog_timer,
-                           watchdog_timer.expires + WATCHDOG_INTERVAL);
+               /*
+                * Cycle through CPUs to check if the CPUs stay
+                * synchronized to each other.
+                */
+               int next_cpu = next_cpu_nr(raw_smp_processor_id(), cpu_online_map);
+
+               if (next_cpu >= nr_cpu_ids)
+                       next_cpu = first_cpu(cpu_online_map);
+               watchdog_timer.expires += WATCHDOG_INTERVAL;
+               add_timer_on(&watchdog_timer, next_cpu);
        }
        spin_unlock(&watchdog_lock);
 }
+static void clocksource_resume_watchdog(void)
+{
+       set_bit(0, &watchdog_resumed);
+}
+
 static void clocksource_check_watchdog(struct clocksource *cs)
 {
        struct clocksource *cse;
@@ -147,9 +172,11 @@ static void clocksource_check_watchdog(struct clocksource *cs)
                if (!started && watchdog) {
                        watchdog_last = watchdog->read();
                        watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
-                       add_timer(&watchdog_timer);
+                       add_timer_on(&watchdog_timer,
+                                    first_cpu(cpu_online_map));
                }
-       } else if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) {
+       } else {
+               if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
                        cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
 
                if (!watchdog || cs->rating > watchdog->rating) {
@@ -167,7 +194,8 @@ static void clocksource_check_watchdog(struct clocksource *cs)
                                watchdog_last = watchdog->read();
                                watchdog_timer.expires =
                                        jiffies + WATCHDOG_INTERVAL;
-                               add_timer(&watchdog_timer);
+                               add_timer_on(&watchdog_timer,
+                                            first_cpu(cpu_online_map));
                        }
                }
        }
@@ -179,9 +207,43 @@ static void clocksource_check_watchdog(struct clocksource *cs)
        if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
                cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
 }
+
+static inline void clocksource_resume_watchdog(void) { }
 #endif
 
 /**
+ * clocksource_resume - resume the clocksource(s)
+ */
+void clocksource_resume(void)
+{
+       struct clocksource *cs;
+       unsigned long flags;
+
+       spin_lock_irqsave(&clocksource_lock, flags);
+
+       list_for_each_entry(cs, &clocksource_list, list) {
+               if (cs->resume)
+                       cs->resume();
+       }
+
+       clocksource_resume_watchdog();
+
+       spin_unlock_irqrestore(&clocksource_lock, flags);
+}
+
+/**
+ * clocksource_touch_watchdog - Update watchdog
+ *
+ * Update the watchdog after exception contexts such as kgdb so as not
+ * to incorrectly trip the watchdog.
+ *
+ */
+void clocksource_touch_watchdog(void)
+{
+       clocksource_resume_watchdog();
+}
+
+/**
  * clocksource_get_next - Returns the selected clocksource
  *
  */
@@ -290,6 +352,21 @@ void clocksource_change_rating(struct clocksource *cs, int rating)
        spin_unlock_irqrestore(&clocksource_lock, flags);
 }
 
+/**
+ * clocksource_unregister - remove a registered clocksource
+ */
+void clocksource_unregister(struct clocksource *cs)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&clocksource_lock, flags);
+       list_del(&cs->list);
+       if (clocksource_override == cs)
+               clocksource_override = NULL;
+       next_clocksource = select_clocksource();
+       spin_unlock_irqrestore(&clocksource_lock, flags);
+}
+
 #ifdef CONFIG_SYSFS
 /**
  * sysfs_show_current_clocksources - sysfs interface for current clocksource
@@ -299,17 +376,16 @@ void clocksource_change_rating(struct clocksource *cs, int rating)
  * Provides sysfs interface for listing current clocksource.
  */
 static ssize_t
-sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
+sysfs_show_current_clocksources(struct sys_device *dev,
+                               struct sysdev_attribute *attr, char *buf)
 {
-       char *curr = buf;
+       ssize_t count = 0;
 
        spin_lock_irq(&clocksource_lock);
-       curr += sprintf(curr, "%s ", curr_clocksource->name);
+       count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
        spin_unlock_irq(&clocksource_lock);
 
-       curr += sprintf(curr, "\n");
-
-       return curr - buf;
+       return count;
 }
 
 /**
@@ -322,10 +398,10 @@ sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
  * clocksource selction.
  */
 static ssize_t sysfs_override_clocksource(struct sys_device *dev,
+                                         struct sysdev_attribute *attr,
                                          const char *buf, size_t count)
 {
        struct clocksource *ovr = NULL;
-       struct list_head *tmp;
        size_t ret = count;
        int len;
 
@@ -345,12 +421,11 @@ static ssize_t sysfs_override_clocksource(struct sys_device *dev,
 
        len = strlen(override_name);
        if (len) {
+               struct clocksource *cs;
+
                ovr = clocksource_override;
                /* try to select it: */
-               list_for_each(tmp, &clocksource_list) {
-                       struct clocksource *cs;
-
-                       cs = list_entry(tmp, struct clocksource, list);
+               list_for_each_entry(cs, &clocksource_list, list) {
                        if (strlen(cs->name) == len &&
                            !strcmp(cs->name, override_name))
                                ovr = cs;
@@ -376,36 +451,38 @@ static ssize_t sysfs_override_clocksource(struct sys_device *dev,
  * Provides sysfs interface for listing registered clocksources
  */
 static ssize_t
-sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
+sysfs_show_available_clocksources(struct sys_device *dev,
+                                 struct sysdev_attribute *attr,
+                                 char *buf)
 {
-       struct list_head *tmp;
-       char *curr = buf;
+       struct clocksource *src;
+       ssize_t count = 0;
 
        spin_lock_irq(&clocksource_lock);
-       list_for_each(tmp, &clocksource_list) {
-               struct clocksource *src;
-
-               src = list_entry(tmp, struct clocksource, list);
-               curr += sprintf(curr, "%s ", src->name);
+       list_for_each_entry(src, &clocksource_list, list) {
+               count += snprintf(buf + count,
+                                 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
+                                 "%s ", src->name);
        }
        spin_unlock_irq(&clocksource_lock);
 
-       curr += sprintf(curr, "\n");
+       count += snprintf(buf + count,
+                         max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
 
-       return curr - buf;
+       return count;
 }
 
 /*
  * Sysfs setup bits:
  */
-static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
+static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
                   sysfs_override_clocksource);
 
-static SYSDEV_ATTR(available_clocksource, 0600,
+static SYSDEV_ATTR(available_clocksource, 0444,
                   sysfs_show_available_clocksources, NULL);
 
 static struct sysdev_class clocksource_sysclass = {
-       set_kset_name("clocksource"),
+       .name = "clocksource",
 };
 
 static struct sys_device device_clocksource = {