Merge branch 'timers-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip

* 'timers-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip:
clocksource, acpi_pm.c: check for monotonicity
clocksource, acpi_pm.c: use proper read function also in errata mode
ntp: fix calculation of the next jiffie to trigger RTC sync
x86: HPET: read back compare register before reading counter
x86: HPET fix moronic 32/64bit thinko
clockevents: broadcast fixup possible waiters
HPET: make minimum reprogramming delta useful
clockevents: prevent endless loop lockup
clockevents: prevent multiple init/shutdown
clockevents: enforce reprogram in oneshot setup
clockevents: prevent endless loop in periodic broadcast handler
clockevents: prevent clockevent event_handler ending up handler_noop

+154 -59
+13 -6
arch/x86/kernel/hpet.c
··· 210 210 /* Calculate the min / max delta */ 211 211 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, 212 212 &hpet_clockevent); 213 - hpet_clockevent.min_delta_ns = clockevent_delta2ns(0x30, 214 - &hpet_clockevent); 213 + /* 5 usec minimum reprogramming delta. */ 214 + hpet_clockevent.min_delta_ns = 5000; 215 215 216 216 /* 217 217 * Start hpet with the boot cpu mask and make it ··· 270 270 } 271 271 272 272 static int hpet_legacy_next_event(unsigned long delta, 273 - struct clock_event_device *evt) 273 + struct clock_event_device *evt) 274 274 { 275 - unsigned long cnt; 275 + u32 cnt; 276 276 277 277 cnt = hpet_readl(HPET_COUNTER); 278 - cnt += delta; 278 + cnt += (u32) delta; 279 279 hpet_writel(cnt, HPET_T0_CMP); 280 280 281 - return ((long)(hpet_readl(HPET_COUNTER) - cnt ) > 0) ? -ETIME : 0; 281 + /* 282 + * We need to read back the CMP register to make sure that 283 + * what we wrote hit the chip before we compare it to the 284 + * counter. 285 + */ 286 + WARN_ON((u32)hpet_readl(HPET_T0_CMP) != cnt); 287 + 288 + return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0; 282 289 } 283 290 284 291 /*
+33 -21
drivers/clocksource/acpi_pm.c
··· 21 21 #include <linux/errno.h> 22 22 #include <linux/init.h> 23 23 #include <linux/pci.h> 24 + #include <linux/delay.h> 24 25 #include <asm/io.h> 25 26 26 27 /* ··· 152 151 */ 153 152 static int verify_pmtmr_rate(void) 154 153 { 155 - u32 value1, value2; 154 + cycle_t value1, value2; 156 155 unsigned long count, delta; 157 156 158 157 mach_prepare_counter(); 159 - value1 = read_pmtmr(); 158 + value1 = clocksource_acpi_pm.read(); 160 159 mach_countup(&count); 161 - value2 = read_pmtmr(); 160 + value2 = clocksource_acpi_pm.read(); 162 161 delta = (value2 - value1) & ACPI_PM_MASK; 163 162 164 163 /* Check that the PMTMR delta is within 5% of what we expect */ ··· 176 175 #define verify_pmtmr_rate() (0) 177 176 #endif 178 177 178 + /* Number of monotonicity checks to perform during initialization */ 179 + #define ACPI_PM_MONOTONICITY_CHECKS 10 180 + 179 181 static int __init init_acpi_pm_clocksource(void) 180 182 { 181 - u32 value1, value2; 182 - unsigned int i; 183 + cycle_t value1, value2; 184 + unsigned int i, j, good = 0; 183 185 184 186 if (!pmtmr_ioport) 185 187 return -ENODEV; ··· 191 187 clocksource_acpi_pm.shift); 192 188 193 189 /* "verify" this timing source: */ 194 - value1 = read_pmtmr(); 195 - for (i = 0; i < 10000; i++) { 196 - value2 = read_pmtmr(); 197 - if (value2 == value1) 198 - continue; 199 - if (value2 > value1) 200 - goto pm_good; 201 - if ((value2 < value1) && ((value2) < 0xFFF)) 202 - goto pm_good; 203 - printk(KERN_INFO "PM-Timer had inconsistent results:" 204 - " 0x%#x, 0x%#x - aborting.\n", value1, value2); 205 - return -EINVAL; 190 + for (j = 0; j < ACPI_PM_MONOTONICITY_CHECKS; j++) { 191 + value1 = clocksource_acpi_pm.read(); 192 + for (i = 0; i < 10000; i++) { 193 + value2 = clocksource_acpi_pm.read(); 194 + if (value2 == value1) 195 + continue; 196 + if (value2 > value1) 197 + good++; 198 + break; 199 + if ((value2 < value1) && ((value2) < 0xFFF)) 200 + good++; 201 + break; 202 + printk(KERN_INFO "PM-Timer had inconsistent results:" 203 + " 0x%#llx, 0x%#llx - aborting.\n", 204 + value1, value2); 205 + return -EINVAL; 206 + } 207 + udelay(300 * i); 206 208 } 207 - printk(KERN_INFO "PM-Timer had no reasonable result:" 208 - " 0x%#x - aborting.\n", value1); 209 - return -ENODEV; 210 209 211 - pm_good: 210 + if (good != ACPI_PM_MONOTONICITY_CHECKS) { 211 + printk(KERN_INFO "PM-Timer failed consistency check " 212 + " (0x%#llx) - aborting.\n", value1); 213 + return -ENODEV; 214 + } 215 + 212 216 if (verify_pmtmr_rate() != 0) 213 217 return -ENODEV; 214 218
+2
include/linux/clockchips.h
··· 127 127 extern int clockevents_program_event(struct clock_event_device *dev, 128 128 ktime_t expires, ktime_t now); 129 129 130 + extern void clockevents_handle_noop(struct clock_event_device *dev); 131 + 130 132 #ifdef CONFIG_GENERIC_CLOCKEVENTS 131 133 extern void clockevents_notify(unsigned long reason, void *arg); 132 134 #else
+1 -2
kernel/time/clockevents.c
··· 177 177 /* 178 178 * Noop handler when we shut down an event device 179 179 */ 180 - static void clockevents_handle_noop(struct clock_event_device *dev) 180 + void clockevents_handle_noop(struct clock_event_device *dev) 181 181 { 182 182 } 183 183 ··· 199 199 * released list and do a notify add later. 200 200 */ 201 201 if (old) { 202 - old->event_handler = clockevents_handle_noop; 203 202 clockevents_set_mode(old, CLOCK_EVT_MODE_UNUSED); 204 203 list_del(&old->list); 205 204 list_add(&old->list, &clockevents_released);
+1 -1
kernel/time/ntp.c
··· 245 245 if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2) 246 246 fail = update_persistent_clock(now); 247 247 248 - next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec; 248 + next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec - (TICK_NSEC / 2); 249 249 if (next.tv_nsec <= 0) 250 250 next.tv_nsec += NSEC_PER_SEC; 251 251
+57 -19
kernel/time/tick-broadcast.c
··· 175 175 */ 176 176 static void tick_handle_periodic_broadcast(struct clock_event_device *dev) 177 177 { 178 + ktime_t next; 179 + 178 180 tick_do_periodic_broadcast(); 179 181 180 182 /* ··· 187 185 188 186 /* 189 187 * Setup the next period for devices, which do not have 190 - * periodic mode: 188 + * periodic mode. We read dev->next_event first and add to it 189 + * when the event alrady expired. clockevents_program_event() 190 + * sets dev->next_event only when the event is really 191 + * programmed to the device. 191 192 */ 192 - for (;;) { 193 - ktime_t next = ktime_add(dev->next_event, tick_period); 193 + for (next = dev->next_event; ;) { 194 + next = ktime_add(next, tick_period); 194 195 195 196 if (!clockevents_program_event(dev, next, ktime_get())) 196 197 return; ··· 210 205 struct clock_event_device *bc, *dev; 211 206 struct tick_device *td; 212 207 unsigned long flags, *reason = why; 213 - int cpu; 208 + int cpu, bc_stopped; 214 209 215 210 spin_lock_irqsave(&tick_broadcast_lock, flags); 216 211 ··· 227 222 228 223 if (!tick_device_is_functional(dev)) 229 224 goto out; 225 + 226 + bc_stopped = cpus_empty(tick_broadcast_mask); 230 227 231 228 switch (*reason) { 232 229 case CLOCK_EVT_NOTIFY_BROADCAST_ON: ··· 252 245 break; 253 246 } 254 247 255 - if (cpus_empty(tick_broadcast_mask)) 256 - clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN); 257 - else { 248 + if (cpus_empty(tick_broadcast_mask)) { 249 + if (!bc_stopped) 250 + clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN); 251 + } else if (bc_stopped) { 258 252 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) 259 253 tick_broadcast_start_periodic(bc); 260 254 else ··· 372 364 static int tick_broadcast_set_event(ktime_t expires, int force) 373 365 { 374 366 struct clock_event_device *bc = tick_broadcast_device.evtdev; 375 - ktime_t now = ktime_get(); 376 - int res; 377 367 378 - for(;;) { 379 - res = clockevents_program_event(bc, expires, now); 380 - if (!res || !force) 381 - return res; 382 - now = ktime_get(); 383 - expires = ktime_add(now, ktime_set(0, bc->min_delta_ns)); 384 - } 368 + return tick_dev_program_event(bc, expires, force); 385 369 } 386 370 387 371 int tick_resume_broadcast_oneshot(struct clock_event_device *bc) ··· 491 491 cpu_clear(cpu, tick_broadcast_oneshot_mask); 492 492 } 493 493 494 + static void tick_broadcast_init_next_event(cpumask_t *mask, ktime_t expires) 495 + { 496 + struct tick_device *td; 497 + int cpu; 498 + 499 + for_each_cpu_mask_nr(cpu, *mask) { 500 + td = &per_cpu(tick_cpu_device, cpu); 501 + if (td->evtdev) 502 + td->evtdev->next_event = expires; 503 + } 504 + } 505 + 494 506 /** 495 507 * tick_broadcast_setup_oneshot - setup the broadcast device 496 508 */ 497 509 void tick_broadcast_setup_oneshot(struct clock_event_device *bc) 498 510 { 499 - bc->event_handler = tick_handle_oneshot_broadcast; 500 - clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT); 501 - bc->next_event.tv64 = KTIME_MAX; 511 + /* Set it up only once ! */ 512 + if (bc->event_handler != tick_handle_oneshot_broadcast) { 513 + int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC; 514 + int cpu = smp_processor_id(); 515 + cpumask_t mask; 516 + 517 + bc->event_handler = tick_handle_oneshot_broadcast; 518 + clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT); 519 + 520 + /* Take the do_timer update */ 521 + tick_do_timer_cpu = cpu; 522 + 523 + /* 524 + * We must be careful here. There might be other CPUs 525 + * waiting for periodic broadcast. We need to set the 526 + * oneshot_mask bits for those and program the 527 + * broadcast device to fire. 528 + */ 529 + mask = tick_broadcast_mask; 530 + cpu_clear(cpu, mask); 531 + cpus_or(tick_broadcast_oneshot_mask, 532 + tick_broadcast_oneshot_mask, mask); 533 + 534 + if (was_periodic && !cpus_empty(mask)) { 535 + tick_broadcast_init_next_event(&mask, tick_next_period); 536 + tick_broadcast_set_event(tick_next_period, 1); 537 + } else 538 + bc->next_event.tv64 = KTIME_MAX; 539 + } 502 540 } 503 541 504 542 /*
+1
kernel/time/tick-common.c
··· 161 161 } else { 162 162 handler = td->evtdev->event_handler; 163 163 next_event = td->evtdev->next_event; 164 + td->evtdev->event_handler = clockevents_handle_noop; 164 165 } 165 166 166 167 td->evtdev = newdev;
+2
kernel/time/tick-internal.h
··· 17 17 extern void tick_setup_oneshot(struct clock_event_device *newdev, 18 18 void (*handler)(struct clock_event_device *), 19 19 ktime_t nextevt); 20 + extern int tick_dev_program_event(struct clock_event_device *dev, 21 + ktime_t expires, int force); 20 22 extern int tick_program_event(ktime_t expires, int force); 21 23 extern void tick_oneshot_notify(void); 22 24 extern int tick_switch_to_oneshot(void (*handler)(struct clock_event_device *));
+44 -10
kernel/time/tick-oneshot.c
··· 23 23 #include "tick-internal.h" 24 24 25 25 /** 26 + * tick_program_event internal worker function 27 + */ 28 + int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires, 29 + int force) 30 + { 31 + ktime_t now = ktime_get(); 32 + int i; 33 + 34 + for (i = 0;;) { 35 + int ret = clockevents_program_event(dev, expires, now); 36 + 37 + if (!ret || !force) 38 + return ret; 39 + 40 + /* 41 + * We tried 2 times to program the device with the given 42 + * min_delta_ns. If that's not working then we double it 43 + * and emit a warning. 44 + */ 45 + if (++i > 2) { 46 + printk(KERN_WARNING "CE: __tick_program_event of %s is " 47 + "stuck %llx %llx\n", dev->name ? dev->name : "?", 48 + now.tv64, expires.tv64); 49 + printk(KERN_WARNING 50 + "CE: increasing min_delta_ns %ld to %ld nsec\n", 51 + dev->min_delta_ns, dev->min_delta_ns << 1); 52 + WARN_ON(1); 53 + 54 + /* Double the min. delta and try again */ 55 + if (!dev->min_delta_ns) 56 + dev->min_delta_ns = 5000; 57 + else 58 + dev->min_delta_ns <<= 1; 59 + i = 0; 60 + } 61 + 62 + now = ktime_get(); 63 + expires = ktime_add_ns(now, dev->min_delta_ns); 64 + } 65 + } 66 + 67 + /** 26 68 * tick_program_event 27 69 */ 28 70 int tick_program_event(ktime_t expires, int force) 29 71 { 30 72 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev; 31 - ktime_t now = ktime_get(); 32 73 33 - while (1) { 34 - int ret = clockevents_program_event(dev, expires, now); 35 - 36 - if (!ret || !force) 37 - return ret; 38 - now = ktime_get(); 39 - expires = ktime_add(now, ktime_set(0, dev->min_delta_ns)); 40 - } 74 + return tick_dev_program_event(dev, expires, force); 41 75 } 42 76 43 77 /** ··· 95 61 { 96 62 newdev->event_handler = handler; 97 63 clockevents_set_mode(newdev, CLOCK_EVT_MODE_ONESHOT); 98 - clockevents_program_event(newdev, next_event, ktime_get()); 64 + tick_dev_program_event(newdev, next_event, 1); 99 65 } 100 66 101 67 /**