1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * This file contains the functions which manage clocksource drivers. 4 * 5 * Copyright (C) 2004, 2005 IBM, John Stultz ([email protected]) 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/device.h> 11 #include <linux/clocksource.h> 12 #include <linux/init.h> 13 #include <linux/module.h> 14 #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */ 15 #include <linux/tick.h> 16 #include <linux/kthread.h> 17 #include <linux/prandom.h> 18 #include <linux/cpu.h> 19 20 #include "tick-internal.h" 21 #include "timekeeping_internal.h" 22 23 static noinline u64 cycles_to_nsec_safe(struct clocksource *cs, u64 start, u64 end) 24 { 25 u64 delta = clocksource_delta(end, start, cs->mask); 26 27 if (likely(delta < cs->max_cycles)) 28 return clocksource_cyc2ns(delta, cs->mult, cs->shift); 29 30 return mul_u64_u32_shr(delta, cs->mult, cs->shift); 31 } 32 33 /** 34 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks 35 * @mult: pointer to mult variable 36 * @shift: pointer to shift variable 37 * @from: frequency to convert from 38 * @to: frequency to convert to 39 * @maxsec: guaranteed runtime conversion range in seconds 40 * 41 * The function evaluates the shift/mult pair for the scaled math 42 * operations of clocksources and clockevents. 43 * 44 * @to and @from are frequency values in HZ. For clock sources @to is 45 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock 46 * event @to is the counter frequency and @from is NSEC_PER_SEC. 47 * 48 * The @maxsec conversion range argument controls the time frame in 49 * seconds which must be covered by the runtime conversion with the 50 * calculated mult and shift factors. This guarantees that no 64bit 51 * overflow happens when the input value of the conversion is 52 * multiplied with the calculated mult factor. Larger ranges may 53 * reduce the conversion accuracy by choosing smaller mult and shift 54 * factors. 55 */ 56 void 57 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec) 58 { 59 u64 tmp; 60 u32 sft, sftacc= 32; 61 62 /* 63 * Calculate the shift factor which is limiting the conversion 64 * range: 65 */ 66 tmp = ((u64)maxsec * from) >> 32; 67 while (tmp) { 68 tmp >>=1; 69 sftacc--; 70 } 71 72 /* 73 * Find the conversion shift/mult pair which has the best 74 * accuracy and fits the maxsec conversion range: 75 */ 76 for (sft = 32; sft > 0; sft--) { 77 tmp = (u64) to << sft; 78 tmp += from / 2; 79 do_div(tmp, from); 80 if ((tmp >> sftacc) == 0) 81 break; 82 } 83 *mult = tmp; 84 *shift = sft; 85 } 86 EXPORT_SYMBOL_GPL(clocks_calc_mult_shift); 87 88 /*[Clocksource internal variables]--------- 89 * curr_clocksource: 90 * currently selected clocksource. 91 * suspend_clocksource: 92 * used to calculate the suspend time. 93 * clocksource_list: 94 * linked list with the registered clocksources 95 * clocksource_mutex: 96 * protects manipulations to curr_clocksource and the clocksource_list 97 * override_name: 98 * Name of the user-specified clocksource. 99 */ 100 static struct clocksource *curr_clocksource; 101 static struct clocksource *suspend_clocksource; 102 static LIST_HEAD(clocksource_list); 103 static DEFINE_MUTEX(clocksource_mutex); 104 static char override_name[CS_NAME_LEN]; 105 static int finished_booting; 106 static u64 suspend_start; 107 108 /* 109 * Interval: 0.5sec. 110 */ 111 #define WATCHDOG_INTERVAL (HZ >> 1) 112 #define WATCHDOG_INTERVAL_MAX_NS ((2 * WATCHDOG_INTERVAL) * (NSEC_PER_SEC / HZ)) 113 114 /* 115 * Threshold: 0.0312s, when doubled: 0.0625s. 116 */ 117 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 5) 118 119 /* 120 * Maximum permissible delay between two readouts of the watchdog 121 * clocksource surrounding a read of the clocksource being validated. 122 * This delay could be due to SMIs, NMIs, or to VCPU preemptions. Used as 123 * a lower bound for cs->uncertainty_margin values when registering clocks. 124 * 125 * The default of 500 parts per million is based on NTP's limits. 126 * If a clocksource is good enough for NTP, it is good enough for us! 127 * 128 * In other words, by default, even if a clocksource is extremely 129 * precise (for example, with a sub-nanosecond period), the maximum 130 * permissible skew between the clocksource watchdog and the clocksource 131 * under test is not permitted to go below the 500ppm minimum defined 132 * by MAX_SKEW_USEC. This 500ppm minimum may be overridden using the 133 * CLOCKSOURCE_WATCHDOG_MAX_SKEW_US Kconfig option. 134 */ 135 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US 136 #define MAX_SKEW_USEC CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US 137 #else 138 #define MAX_SKEW_USEC (125 * WATCHDOG_INTERVAL / HZ) 139 #endif 140 141 /* 142 * Default for maximum permissible skew when cs->uncertainty_margin is 143 * not specified, and the lower bound even when cs->uncertainty_margin 144 * is specified. This is also the default that is used when registering 145 * clocks with unspecifed cs->uncertainty_margin, so this macro is used 146 * even in CONFIG_CLOCKSOURCE_WATCHDOG=n kernels. 147 */ 148 #define WATCHDOG_MAX_SKEW (MAX_SKEW_USEC * NSEC_PER_USEC) 149 150 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG 151 static void clocksource_watchdog_work(struct work_struct *work); 152 static void clocksource_select(void); 153 154 static LIST_HEAD(watchdog_list); 155 static struct clocksource *watchdog; 156 static struct timer_list watchdog_timer; 157 static DECLARE_WORK(watchdog_work, clocksource_watchdog_work); 158 static DEFINE_SPINLOCK(watchdog_lock); 159 static int watchdog_running; 160 static atomic_t watchdog_reset_pending; 161 static int64_t watchdog_max_interval; 162 163 static inline void clocksource_watchdog_lock(unsigned long *flags) 164 { 165 spin_lock_irqsave(&watchdog_lock, *flags); 166 } 167 168 static inline void clocksource_watchdog_unlock(unsigned long *flags) 169 { 170 spin_unlock_irqrestore(&watchdog_lock, *flags); 171 } 172 173 static int clocksource_watchdog_kthread(void *data); 174 static void __clocksource_change_rating(struct clocksource *cs, int rating); 175 176 static void clocksource_watchdog_work(struct work_struct *work) 177 { 178 /* 179 * We cannot directly run clocksource_watchdog_kthread() here, because 180 * clocksource_select() calls timekeeping_notify() which uses 181 * stop_machine(). One cannot use stop_machine() from a workqueue() due 182 * lock inversions wrt CPU hotplug. 183 * 184 * Also, we only ever run this work once or twice during the lifetime 185 * of the kernel, so there is no point in creating a more permanent 186 * kthread for this. 187 * 188 * If kthread_run fails the next watchdog scan over the 189 * watchdog_list will find the unstable clock again. 190 */ 191 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog"); 192 } 193 194 static void __clocksource_unstable(struct clocksource *cs) 195 { 196 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG); 197 cs->flags |= CLOCK_SOURCE_UNSTABLE; 198 199 /* 200 * If the clocksource is registered clocksource_watchdog_kthread() will 201 * re-rate and re-select. 202 */ 203 if (list_empty(&cs->list)) { 204 cs->rating = 0; 205 return; 206 } 207 208 if (cs->mark_unstable) 209 cs->mark_unstable(cs); 210 211 /* kick clocksource_watchdog_kthread() */ 212 if (finished_booting) 213 schedule_work(&watchdog_work); 214 } 215 216 /** 217 * clocksource_mark_unstable - mark clocksource unstable via watchdog 218 * @cs: clocksource to be marked unstable 219 * 220 * This function is called by the x86 TSC code to mark clocksources as unstable; 221 * it defers demotion and re-selection to a kthread. 222 */ 223 void clocksource_mark_unstable(struct clocksource *cs) 224 { 225 unsigned long flags; 226 227 spin_lock_irqsave(&watchdog_lock, flags); 228 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) { 229 if (!list_empty(&cs->list) && list_empty(&cs->wd_list)) 230 list_add(&cs->wd_list, &watchdog_list); 231 __clocksource_unstable(cs); 232 } 233 spin_unlock_irqrestore(&watchdog_lock, flags); 234 } 235 236 static int verify_n_cpus = 8; 237 module_param(verify_n_cpus, int, 0644); 238 239 enum wd_read_status { 240 WD_READ_SUCCESS, 241 WD_READ_UNSTABLE, 242 WD_READ_SKIP 243 }; 244 245 static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow) 246 { 247 unsigned int nretries, max_retries; 248 int64_t wd_delay, wd_seq_delay; 249 u64 wd_end, wd_end2; 250 251 max_retries = clocksource_get_max_watchdog_retry(); 252 for (nretries = 0; nretries <= max_retries; nretries++) { 253 local_irq_disable(); 254 *wdnow = watchdog->read(watchdog); 255 *csnow = cs->read(cs); 256 wd_end = watchdog->read(watchdog); 257 wd_end2 = watchdog->read(watchdog); 258 local_irq_enable(); 259 260 wd_delay = cycles_to_nsec_safe(watchdog, *wdnow, wd_end); 261 if (wd_delay <= WATCHDOG_MAX_SKEW) { 262 if (nretries > 1 && nretries >= max_retries) { 263 pr_warn("timekeeping watchdog on CPU%d: %s retried %d times before success\n", 264 smp_processor_id(), watchdog->name, nretries); 265 } 266 return WD_READ_SUCCESS; 267 } 268 269 /* 270 * Now compute delay in consecutive watchdog read to see if 271 * there is too much external interferences that cause 272 * significant delay in reading both clocksource and watchdog. 273 * 274 * If consecutive WD read-back delay > WATCHDOG_MAX_SKEW/2, 275 * report system busy, reinit the watchdog and skip the current 276 * watchdog test. 277 */ 278 wd_seq_delay = cycles_to_nsec_safe(watchdog, wd_end, wd_end2); 279 if (wd_seq_delay > WATCHDOG_MAX_SKEW/2) 280 goto skip_test; 281 } 282 283 pr_warn("timekeeping watchdog on CPU%d: wd-%s-wd excessive read-back delay of %lldns vs. limit of %ldns, wd-wd read-back delay only %lldns, attempt %d, marking %s unstable\n", 284 smp_processor_id(), cs->name, wd_delay, WATCHDOG_MAX_SKEW, wd_seq_delay, nretries, cs->name); 285 return WD_READ_UNSTABLE; 286 287 skip_test: 288 pr_info("timekeeping watchdog on CPU%d: %s wd-wd read-back delay of %lldns\n", 289 smp_processor_id(), watchdog->name, wd_seq_delay); 290 pr_info("wd-%s-wd read-back delay of %lldns, clock-skew test skipped!\n", 291 cs->name, wd_delay); 292 return WD_READ_SKIP; 293 } 294 295 static u64 csnow_mid; 296 static cpumask_t cpus_ahead; 297 static cpumask_t cpus_behind; 298 static cpumask_t cpus_chosen; 299 300 static void clocksource_verify_choose_cpus(void) 301 { 302 int cpu, i, n = verify_n_cpus; 303 304 if (n < 0) { 305 /* Check all of the CPUs. */ 306 cpumask_copy(&cpus_chosen, cpu_online_mask); 307 cpumask_clear_cpu(smp_processor_id(), &cpus_chosen); 308 return; 309 } 310 311 /* If no checking desired, or no other CPU to check, leave. */ 312 cpumask_clear(&cpus_chosen); 313 if (n == 0 || num_online_cpus() <= 1) 314 return; 315 316 /* Make sure to select at least one CPU other than the current CPU. */ 317 cpu = cpumask_first(cpu_online_mask); 318 if (cpu == smp_processor_id()) 319 cpu = cpumask_next(cpu, cpu_online_mask); 320 if (WARN_ON_ONCE(cpu >= nr_cpu_ids)) 321 return; 322 cpumask_set_cpu(cpu, &cpus_chosen); 323 324 /* Force a sane value for the boot parameter. */ 325 if (n > nr_cpu_ids) 326 n = nr_cpu_ids; 327 328 /* 329 * Randomly select the specified number of CPUs. If the same 330 * CPU is selected multiple times, that CPU is checked only once, 331 * and no replacement CPU is selected. This gracefully handles 332 * situations where verify_n_cpus is greater than the number of 333 * CPUs that are currently online. 334 */ 335 for (i = 1; i < n; i++) { 336 cpu = get_random_u32_below(nr_cpu_ids); 337 cpu = cpumask_next(cpu - 1, cpu_online_mask); 338 if (cpu >= nr_cpu_ids) 339 cpu = cpumask_first(cpu_online_mask); 340 if (!WARN_ON_ONCE(cpu >= nr_cpu_ids)) 341 cpumask_set_cpu(cpu, &cpus_chosen); 342 } 343 344 /* Don't verify ourselves. */ 345 cpumask_clear_cpu(smp_processor_id(), &cpus_chosen); 346 } 347 348 static void clocksource_verify_one_cpu(void *csin) 349 { 350 struct clocksource *cs = (struct clocksource *)csin; 351 352 csnow_mid = cs->read(cs); 353 } 354 355 void clocksource_verify_percpu(struct clocksource *cs) 356 { 357 int64_t cs_nsec, cs_nsec_max = 0, cs_nsec_min = LLONG_MAX; 358 u64 csnow_begin, csnow_end; 359 int cpu, testcpu; 360 s64 delta; 361 362 if (verify_n_cpus == 0) 363 return; 364 cpumask_clear(&cpus_ahead); 365 cpumask_clear(&cpus_behind); 366 cpus_read_lock(); 367 preempt_disable(); 368 clocksource_verify_choose_cpus(); 369 if (cpumask_empty(&cpus_chosen)) { 370 preempt_enable(); 371 cpus_read_unlock(); 372 pr_warn("Not enough CPUs to check clocksource '%s'.\n", cs->name); 373 return; 374 } 375 testcpu = smp_processor_id(); 376 pr_warn("Checking clocksource %s synchronization from CPU %d to CPUs %*pbl.\n", cs->name, testcpu, cpumask_pr_args(&cpus_chosen)); 377 for_each_cpu(cpu, &cpus_chosen) { 378 if (cpu == testcpu) 379 continue; 380 csnow_begin = cs->read(cs); 381 smp_call_function_single(cpu, clocksource_verify_one_cpu, cs, 1); 382 csnow_end = cs->read(cs); 383 delta = (s64)((csnow_mid - csnow_begin) & cs->mask); 384 if (delta < 0) 385 cpumask_set_cpu(cpu, &cpus_behind); 386 delta = (csnow_end - csnow_mid) & cs->mask; 387 if (delta < 0) 388 cpumask_set_cpu(cpu, &cpus_ahead); 389 cs_nsec = cycles_to_nsec_safe(cs, csnow_begin, csnow_end); 390 if (cs_nsec > cs_nsec_max) 391 cs_nsec_max = cs_nsec; 392 if (cs_nsec < cs_nsec_min) 393 cs_nsec_min = cs_nsec; 394 } 395 preempt_enable(); 396 cpus_read_unlock(); 397 if (!cpumask_empty(&cpus_ahead)) 398 pr_warn(" CPUs %*pbl ahead of CPU %d for clocksource %s.\n", 399 cpumask_pr_args(&cpus_ahead), testcpu, cs->name); 400 if (!cpumask_empty(&cpus_behind)) 401 pr_warn(" CPUs %*pbl behind CPU %d for clocksource %s.\n", 402 cpumask_pr_args(&cpus_behind), testcpu, cs->name); 403 if (!cpumask_empty(&cpus_ahead) || !cpumask_empty(&cpus_behind)) 404 pr_warn(" CPU %d check durations %lldns - %lldns for clocksource %s.\n", 405 testcpu, cs_nsec_min, cs_nsec_max, cs->name); 406 } 407 EXPORT_SYMBOL_GPL(clocksource_verify_percpu); 408 409 static inline void clocksource_reset_watchdog(void) 410 { 411 struct clocksource *cs; 412 413 list_for_each_entry(cs, &watchdog_list, wd_list) 414 cs->flags &= ~CLOCK_SOURCE_WATCHDOG; 415 } 416 417 418 static void clocksource_watchdog(struct timer_list *unused) 419 { 420 int64_t wd_nsec, cs_nsec, interval; 421 u64 csnow, wdnow, cslast, wdlast; 422 int next_cpu, reset_pending; 423 struct clocksource *cs; 424 enum wd_read_status read_ret; 425 unsigned long extra_wait = 0; 426 u32 md; 427 428 spin_lock(&watchdog_lock); 429 if (!watchdog_running) 430 goto out; 431 432 reset_pending = atomic_read(&watchdog_reset_pending); 433 434 list_for_each_entry(cs, &watchdog_list, wd_list) { 435 436 /* Clocksource already marked unstable? */ 437 if (cs->flags & CLOCK_SOURCE_UNSTABLE) { 438 if (finished_booting) 439 schedule_work(&watchdog_work); 440 continue; 441 } 442 443 read_ret = cs_watchdog_read(cs, &csnow, &wdnow); 444 445 if (read_ret == WD_READ_UNSTABLE) { 446 /* Clock readout unreliable, so give it up. */ 447 __clocksource_unstable(cs); 448 continue; 449 } 450 451 /* 452 * When WD_READ_SKIP is returned, it means the system is likely 453 * under very heavy load, where the latency of reading 454 * watchdog/clocksource is very big, and affect the accuracy of 455 * watchdog check. So give system some space and suspend the 456 * watchdog check for 5 minutes. 457 */ 458 if (read_ret == WD_READ_SKIP) { 459 /* 460 * As the watchdog timer will be suspended, and 461 * cs->last could keep unchanged for 5 minutes, reset 462 * the counters. 463 */ 464 clocksource_reset_watchdog(); 465 extra_wait = HZ * 300; 466 break; 467 } 468 469 /* Clocksource initialized ? */ 470 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) || 471 atomic_read(&watchdog_reset_pending)) { 472 cs->flags |= CLOCK_SOURCE_WATCHDOG; 473 cs->wd_last = wdnow; 474 cs->cs_last = csnow; 475 continue; 476 } 477 478 wd_nsec = cycles_to_nsec_safe(watchdog, cs->wd_last, wdnow); 479 cs_nsec = cycles_to_nsec_safe(cs, cs->cs_last, csnow); 480 wdlast = cs->wd_last; /* save these in case we print them */ 481 cslast = cs->cs_last; 482 cs->cs_last = csnow; 483 cs->wd_last = wdnow; 484 485 if (atomic_read(&watchdog_reset_pending)) 486 continue; 487 488 /* 489 * The processing of timer softirqs can get delayed (usually 490 * on account of ksoftirqd not getting to run in a timely 491 * manner), which causes the watchdog interval to stretch. 492 * Skew detection may fail for longer watchdog intervals 493 * on account of fixed margins being used. 494 * Some clocksources, e.g. acpi_pm, cannot tolerate 495 * watchdog intervals longer than a few seconds. 496 */ 497 interval = max(cs_nsec, wd_nsec); 498 if (unlikely(interval > WATCHDOG_INTERVAL_MAX_NS)) { 499 if (system_state > SYSTEM_SCHEDULING && 500 interval > 2 * watchdog_max_interval) { 501 watchdog_max_interval = interval; 502 pr_warn("Long readout interval, skipping watchdog check: cs_nsec: %lld wd_nsec: %lld\n", 503 cs_nsec, wd_nsec); 504 } 505 watchdog_timer.expires = jiffies; 506 continue; 507 } 508 509 /* Check the deviation from the watchdog clocksource. */ 510 md = cs->uncertainty_margin + watchdog->uncertainty_margin; 511 if (abs(cs_nsec - wd_nsec) > md) { 512 s64 cs_wd_msec; 513 s64 wd_msec; 514 u32 wd_rem; 515 516 pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n", 517 smp_processor_id(), cs->name); 518 pr_warn(" '%s' wd_nsec: %lld wd_now: %llx wd_last: %llx mask: %llx\n", 519 watchdog->name, wd_nsec, wdnow, wdlast, watchdog->mask); 520 pr_warn(" '%s' cs_nsec: %lld cs_now: %llx cs_last: %llx mask: %llx\n", 521 cs->name, cs_nsec, csnow, cslast, cs->mask); 522 cs_wd_msec = div_s64_rem(cs_nsec - wd_nsec, 1000 * 1000, &wd_rem); 523 wd_msec = div_s64_rem(wd_nsec, 1000 * 1000, &wd_rem); 524 pr_warn(" Clocksource '%s' skewed %lld ns (%lld ms) over watchdog '%s' interval of %lld ns (%lld ms)\n", 525 cs->name, cs_nsec - wd_nsec, cs_wd_msec, watchdog->name, wd_nsec, wd_msec); 526 if (curr_clocksource == cs) 527 pr_warn(" '%s' is current clocksource.\n", cs->name); 528 else if (curr_clocksource) 529 pr_warn(" '%s' (not '%s') is current clocksource.\n", curr_clocksource->name, cs->name); 530 else 531 pr_warn(" No current clocksource.\n"); 532 __clocksource_unstable(cs); 533 continue; 534 } 535 536 if (cs == curr_clocksource && cs->tick_stable) 537 cs->tick_stable(cs); 538 539 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && 540 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) && 541 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) { 542 /* Mark it valid for high-res. */ 543 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; 544 545 /* 546 * clocksource_done_booting() will sort it if 547 * finished_booting is not set yet. 548 */ 549 if (!finished_booting) 550 continue; 551 552 /* 553 * If this is not the current clocksource let 554 * the watchdog thread reselect it. Due to the 555 * change to high res this clocksource might 556 * be preferred now. If it is the current 557 * clocksource let the tick code know about 558 * that change. 559 */ 560 if (cs != curr_clocksource) { 561 cs->flags |= CLOCK_SOURCE_RESELECT; 562 schedule_work(&watchdog_work); 563 } else { 564 tick_clock_notify(); 565 } 566 } 567 } 568 569 /* 570 * We only clear the watchdog_reset_pending, when we did a 571 * full cycle through all clocksources. 572 */ 573 if (reset_pending) 574 atomic_dec(&watchdog_reset_pending); 575 576 /* 577 * Cycle through CPUs to check if the CPUs stay synchronized 578 * to each other. 579 */ 580 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask); 581 if (next_cpu >= nr_cpu_ids) 582 next_cpu = cpumask_first(cpu_online_mask); 583 584 /* 585 * Arm timer if not already pending: could race with concurrent 586 * pair clocksource_stop_watchdog() clocksource_start_watchdog(). 587 */ 588 if (!timer_pending(&watchdog_timer)) { 589 watchdog_timer.expires += WATCHDOG_INTERVAL + extra_wait; 590 add_timer_on(&watchdog_timer, next_cpu); 591 } 592 out: 593 spin_unlock(&watchdog_lock); 594 } 595 596 static inline void clocksource_start_watchdog(void) 597 { 598 if (watchdog_running || !watchdog || list_empty(&watchdog_list)) 599 return; 600 timer_setup(&watchdog_timer, clocksource_watchdog, 0); 601 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL; 602 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask)); 603 watchdog_running = 1; 604 } 605 606 static inline void clocksource_stop_watchdog(void) 607 { 608 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list))) 609 return; 610 del_timer(&watchdog_timer); 611 watchdog_running = 0; 612 } 613 614 static void clocksource_resume_watchdog(void) 615 { 616 atomic_inc(&watchdog_reset_pending); 617 } 618 619 static void clocksource_enqueue_watchdog(struct clocksource *cs) 620 { 621 INIT_LIST_HEAD(&cs->wd_list); 622 623 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { 624 /* cs is a clocksource to be watched. */ 625 list_add(&cs->wd_list, &watchdog_list); 626 cs->flags &= ~CLOCK_SOURCE_WATCHDOG; 627 } else { 628 /* cs is a watchdog. */ 629 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) 630 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; 631 } 632 } 633 634 static void clocksource_select_watchdog(bool fallback) 635 { 636 struct clocksource *cs, *old_wd; 637 unsigned long flags; 638 639 spin_lock_irqsave(&watchdog_lock, flags); 640 /* save current watchdog */ 641 old_wd = watchdog; 642 if (fallback) 643 watchdog = NULL; 644 645 list_for_each_entry(cs, &clocksource_list, list) { 646 /* cs is a clocksource to be watched. */ 647 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) 648 continue; 649 650 /* Skip current if we were requested for a fallback. */ 651 if (fallback && cs == old_wd) 652 continue; 653 654 /* Pick the best watchdog. */ 655 if (!watchdog || cs->rating > watchdog->rating) 656 watchdog = cs; 657 } 658 /* If we failed to find a fallback restore the old one. */ 659 if (!watchdog) 660 watchdog = old_wd; 661 662 /* If we changed the watchdog we need to reset cycles. */ 663 if (watchdog != old_wd) 664 clocksource_reset_watchdog(); 665 666 /* Check if the watchdog timer needs to be started. */ 667 clocksource_start_watchdog(); 668 spin_unlock_irqrestore(&watchdog_lock, flags); 669 } 670 671 static void clocksource_dequeue_watchdog(struct clocksource *cs) 672 { 673 if (cs != watchdog) { 674 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { 675 /* cs is a watched clocksource. */ 676 list_del_init(&cs->wd_list); 677 /* Check if the watchdog timer needs to be stopped. */ 678 clocksource_stop_watchdog(); 679 } 680 } 681 } 682 683 static int __clocksource_watchdog_kthread(void) 684 { 685 struct clocksource *cs, *tmp; 686 unsigned long flags; 687 int select = 0; 688 689 /* Do any required per-CPU skew verification. */ 690 if (curr_clocksource && 691 curr_clocksource->flags & CLOCK_SOURCE_UNSTABLE && 692 curr_clocksource->flags & CLOCK_SOURCE_VERIFY_PERCPU) 693 clocksource_verify_percpu(curr_clocksource); 694 695 spin_lock_irqsave(&watchdog_lock, flags); 696 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) { 697 if (cs->flags & CLOCK_SOURCE_UNSTABLE) { 698 list_del_init(&cs->wd_list); 699 __clocksource_change_rating(cs, 0); 700 select = 1; 701 } 702 if (cs->flags & CLOCK_SOURCE_RESELECT) { 703 cs->flags &= ~CLOCK_SOURCE_RESELECT; 704 select = 1; 705 } 706 } 707 /* Check if the watchdog timer needs to be stopped. */ 708 clocksource_stop_watchdog(); 709 spin_unlock_irqrestore(&watchdog_lock, flags); 710 711 return select; 712 } 713 714 static int clocksource_watchdog_kthread(void *data) 715 { 716 mutex_lock(&clocksource_mutex); 717 if (__clocksource_watchdog_kthread()) 718 clocksource_select(); 719 mutex_unlock(&clocksource_mutex); 720 return 0; 721 } 722 723 static bool clocksource_is_watchdog(struct clocksource *cs) 724 { 725 return cs == watchdog; 726 } 727 728 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */ 729 730 static void clocksource_enqueue_watchdog(struct clocksource *cs) 731 { 732 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) 733 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; 734 } 735 736 static void clocksource_select_watchdog(bool fallback) { } 737 static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { } 738 static inline void clocksource_resume_watchdog(void) { } 739 static inline int __clocksource_watchdog_kthread(void) { return 0; } 740 static bool clocksource_is_watchdog(struct clocksource *cs) { return false; } 741 void clocksource_mark_unstable(struct clocksource *cs) { } 742 743 static inline void clocksource_watchdog_lock(unsigned long *flags) { } 744 static inline void clocksource_watchdog_unlock(unsigned long *flags) { } 745 746 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */ 747 748 static bool clocksource_is_suspend(struct clocksource *cs) 749 { 750 return cs == suspend_clocksource; 751 } 752 753 static void __clocksource_suspend_select(struct clocksource *cs) 754 { 755 /* 756 * Skip the clocksource which will be stopped in suspend state. 757 */ 758 if (!(cs->flags & CLOCK_SOURCE_SUSPEND_NONSTOP)) 759 return; 760 761 /* 762 * The nonstop clocksource can be selected as the suspend clocksource to 763 * calculate the suspend time, so it should not supply suspend/resume 764 * interfaces to suspend the nonstop clocksource when system suspends. 765 */ 766 if (cs->suspend || cs->resume) { 767 pr_warn("Nonstop clocksource %s should not supply suspend/resume interfaces\n", 768 cs->name); 769 } 770 771 /* Pick the best rating. */ 772 if (!suspend_clocksource || cs->rating > suspend_clocksource->rating) 773 suspend_clocksource = cs; 774 } 775 776 /** 777 * clocksource_suspend_select - Select the best clocksource for suspend timing 778 * @fallback: if select a fallback clocksource 779 */ 780 static void clocksource_suspend_select(bool fallback) 781 { 782 struct clocksource *cs, *old_suspend; 783 784 old_suspend = suspend_clocksource; 785 if (fallback) 786 suspend_clocksource = NULL; 787 788 list_for_each_entry(cs, &clocksource_list, list) { 789 /* Skip current if we were requested for a fallback. */ 790 if (fallback && cs == old_suspend) 791 continue; 792 793 __clocksource_suspend_select(cs); 794 } 795 } 796 797 /** 798 * clocksource_start_suspend_timing - Start measuring the suspend timing 799 * @cs: current clocksource from timekeeping 800 * @start_cycles: current cycles from timekeeping 801 * 802 * This function will save the start cycle values of suspend timer to calculate 803 * the suspend time when resuming system. 804 * 805 * This function is called late in the suspend process from timekeeping_suspend(), 806 * that means processes are frozen, non-boot cpus and interrupts are disabled 807 * now. It is therefore possible to start the suspend timer without taking the 808 * clocksource mutex. 809 */ 810 void clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles) 811 { 812 if (!suspend_clocksource) 813 return; 814 815 /* 816 * If current clocksource is the suspend timer, we should use the 817 * tkr_mono.cycle_last value as suspend_start to avoid same reading 818 * from suspend timer. 819 */ 820 if (clocksource_is_suspend(cs)) { 821 suspend_start = start_cycles; 822 return; 823 } 824 825 if (suspend_clocksource->enable && 826 suspend_clocksource->enable(suspend_clocksource)) { 827 pr_warn_once("Failed to enable the non-suspend-able clocksource.\n"); 828 return; 829 } 830 831 suspend_start = suspend_clocksource->read(suspend_clocksource); 832 } 833 834 /** 835 * clocksource_stop_suspend_timing - Stop measuring the suspend timing 836 * @cs: current clocksource from timekeeping 837 * @cycle_now: current cycles from timekeeping 838 * 839 * This function will calculate the suspend time from suspend timer. 840 * 841 * Returns nanoseconds since suspend started, 0 if no usable suspend clocksource. 842 * 843 * This function is called early in the resume process from timekeeping_resume(), 844 * that means there is only one cpu, no processes are running and the interrupts 845 * are disabled. It is therefore possible to stop the suspend timer without 846 * taking the clocksource mutex. 847 */ 848 u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now) 849 { 850 u64 now, nsec = 0; 851 852 if (!suspend_clocksource) 853 return 0; 854 855 /* 856 * If current clocksource is the suspend timer, we should use the 857 * tkr_mono.cycle_last value from timekeeping as current cycle to 858 * avoid same reading from suspend timer. 859 */ 860 if (clocksource_is_suspend(cs)) 861 now = cycle_now; 862 else 863 now = suspend_clocksource->read(suspend_clocksource); 864 865 if (now > suspend_start) 866 nsec = cycles_to_nsec_safe(suspend_clocksource, suspend_start, now); 867 868 /* 869 * Disable the suspend timer to save power if current clocksource is 870 * not the suspend timer. 871 */ 872 if (!clocksource_is_suspend(cs) && suspend_clocksource->disable) 873 suspend_clocksource->disable(suspend_clocksource); 874 875 return nsec; 876 } 877 878 /** 879 * clocksource_suspend - suspend the clocksource(s) 880 */ 881 void clocksource_suspend(void) 882 { 883 struct clocksource *cs; 884 885 list_for_each_entry_reverse(cs, &clocksource_list, list) 886 if (cs->suspend) 887 cs->suspend(cs); 888 } 889 890 /** 891 * clocksource_resume - resume the clocksource(s) 892 */ 893 void clocksource_resume(void) 894 { 895 struct clocksource *cs; 896 897 list_for_each_entry(cs, &clocksource_list, list) 898 if (cs->resume) 899 cs->resume(cs); 900 901 clocksource_resume_watchdog(); 902 } 903 904 /** 905 * clocksource_touch_watchdog - Update watchdog 906 * 907 * Update the watchdog after exception contexts such as kgdb so as not 908 * to incorrectly trip the watchdog. This might fail when the kernel 909 * was stopped in code which holds watchdog_lock. 910 */ 911 void clocksource_touch_watchdog(void) 912 { 913 clocksource_resume_watchdog(); 914 } 915 916 /** 917 * clocksource_max_adjustment- Returns max adjustment amount 918 * @cs: Pointer to clocksource 919 * 920 */ 921 static u32 clocksource_max_adjustment(struct clocksource *cs) 922 { 923 u64 ret; 924 /* 925 * We won't try to correct for more than 11% adjustments (110,000 ppm), 926 */ 927 ret = (u64)cs->mult * 11; 928 do_div(ret,100); 929 return (u32)ret; 930 } 931 932 /** 933 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted 934 * @mult: cycle to nanosecond multiplier 935 * @shift: cycle to nanosecond divisor (power of two) 936 * @maxadj: maximum adjustment value to mult (~11%) 937 * @mask: bitmask for two's complement subtraction of non 64 bit counters 938 * @max_cyc: maximum cycle value before potential overflow (does not include 939 * any safety margin) 940 * 941 * NOTE: This function includes a safety margin of 50%, in other words, we 942 * return half the number of nanoseconds the hardware counter can technically 943 * cover. This is done so that we can potentially detect problems caused by 944 * delayed timers or bad hardware, which might result in time intervals that 945 * are larger than what the math used can handle without overflows. 946 */ 947 u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc) 948 { 949 u64 max_nsecs, max_cycles; 950 951 /* 952 * Calculate the maximum number of cycles that we can pass to the 953 * cyc2ns() function without overflowing a 64-bit result. 954 */ 955 max_cycles = ULLONG_MAX; 956 do_div(max_cycles, mult+maxadj); 957 958 /* 959 * The actual maximum number of cycles we can defer the clocksource is 960 * determined by the minimum of max_cycles and mask. 961 * Note: Here we subtract the maxadj to make sure we don't sleep for 962 * too long if there's a large negative adjustment. 963 */ 964 max_cycles = min(max_cycles, mask); 965 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift); 966 967 /* return the max_cycles value as well if requested */ 968 if (max_cyc) 969 *max_cyc = max_cycles; 970 971 /* Return 50% of the actual maximum, so we can detect bad values */ 972 max_nsecs >>= 1; 973 974 return max_nsecs; 975 } 976 977 /** 978 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles 979 * @cs: Pointer to clocksource to be updated 980 * 981 */ 982 static inline void clocksource_update_max_deferment(struct clocksource *cs) 983 { 984 cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift, 985 cs->maxadj, cs->mask, 986 &cs->max_cycles); 987 } 988 989 static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur) 990 { 991 struct clocksource *cs; 992 993 if (!finished_booting || list_empty(&clocksource_list)) 994 return NULL; 995 996 /* 997 * We pick the clocksource with the highest rating. If oneshot 998 * mode is active, we pick the highres valid clocksource with 999 * the best rating. 1000 */ 1001 list_for_each_entry(cs, &clocksource_list, list) { 1002 if (skipcur && cs == curr_clocksource) 1003 continue; 1004 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES)) 1005 continue; 1006 return cs; 1007 } 1008 return NULL; 1009 } 1010 1011 static void __clocksource_select(bool skipcur) 1012 { 1013 bool oneshot = tick_oneshot_mode_active(); 1014 struct clocksource *best, *cs; 1015 1016 /* Find the best suitable clocksource */ 1017 best = clocksource_find_best(oneshot, skipcur); 1018 if (!best) 1019 return; 1020 1021 if (!strlen(override_name)) 1022 goto found; 1023 1024 /* Check for the override clocksource. */ 1025 list_for_each_entry(cs, &clocksource_list, list) { 1026 if (skipcur && cs == curr_clocksource) 1027 continue; 1028 if (strcmp(cs->name, override_name) != 0) 1029 continue; 1030 /* 1031 * Check to make sure we don't switch to a non-highres 1032 * capable clocksource if the tick code is in oneshot 1033 * mode (highres or nohz) 1034 */ 1035 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) { 1036 /* Override clocksource cannot be used. */ 1037 if (cs->flags & CLOCK_SOURCE_UNSTABLE) { 1038 pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n", 1039 cs->name); 1040 override_name[0] = 0; 1041 } else { 1042 /* 1043 * The override cannot be currently verified. 1044 * Deferring to let the watchdog check. 1045 */ 1046 pr_info("Override clocksource %s is not currently HRT compatible - deferring\n", 1047 cs->name); 1048 } 1049 } else 1050 /* Override clocksource can be used. */ 1051 best = cs; 1052 break; 1053 } 1054 1055 found: 1056 if (curr_clocksource != best && !timekeeping_notify(best)) { 1057 pr_info("Switched to clocksource %s\n", best->name); 1058 curr_clocksource = best; 1059 } 1060 } 1061 1062 /** 1063 * clocksource_select - Select the best clocksource available 1064 * 1065 * Private function. Must hold clocksource_mutex when called. 1066 * 1067 * Select the clocksource with the best rating, or the clocksource, 1068 * which is selected by userspace override. 1069 */ 1070 static void clocksource_select(void) 1071 { 1072 __clocksource_select(false); 1073 } 1074 1075 static void clocksource_select_fallback(void) 1076 { 1077 __clocksource_select(true); 1078 } 1079 1080 /* 1081 * clocksource_done_booting - Called near the end of core bootup 1082 * 1083 * Hack to avoid lots of clocksource churn at boot time. 1084 * We use fs_initcall because we want this to start before 1085 * device_initcall but after subsys_initcall. 1086 */ 1087 static int __init clocksource_done_booting(void) 1088 { 1089 mutex_lock(&clocksource_mutex); 1090 curr_clocksource = clocksource_default_clock(); 1091 finished_booting = 1; 1092 /* 1093 * Run the watchdog first to eliminate unstable clock sources 1094 */ 1095 __clocksource_watchdog_kthread(); 1096 clocksource_select(); 1097 mutex_unlock(&clocksource_mutex); 1098 return 0; 1099 } 1100 fs_initcall(clocksource_done_booting); 1101 1102 /* 1103 * Enqueue the clocksource sorted by rating 1104 */ 1105 static void clocksource_enqueue(struct clocksource *cs) 1106 { 1107 struct list_head *entry = &clocksource_list; 1108 struct clocksource *tmp; 1109 1110 list_for_each_entry(tmp, &clocksource_list, list) { 1111 /* Keep track of the place, where to insert */ 1112 if (tmp->rating < cs->rating) 1113 break; 1114 entry = &tmp->list; 1115 } 1116 list_add(&cs->list, entry); 1117 } 1118 1119 /** 1120 * __clocksource_update_freq_scale - Used update clocksource with new freq 1121 * @cs: clocksource to be registered 1122 * @scale: Scale factor multiplied against freq to get clocksource hz 1123 * @freq: clocksource frequency (cycles per second) divided by scale 1124 * 1125 * This should only be called from the clocksource->enable() method. 1126 * 1127 * This *SHOULD NOT* be called directly! Please use the 1128 * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper 1129 * functions. 1130 */ 1131 void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq) 1132 { 1133 u64 sec; 1134 1135 /* 1136 * Default clocksources are *special* and self-define their mult/shift. 1137 * But, you're not special, so you should specify a freq value. 1138 */ 1139 if (freq) { 1140 /* 1141 * Calc the maximum number of seconds which we can run before 1142 * wrapping around. For clocksources which have a mask > 32-bit 1143 * we need to limit the max sleep time to have a good 1144 * conversion precision. 10 minutes is still a reasonable 1145 * amount. That results in a shift value of 24 for a 1146 * clocksource with mask >= 40-bit and f >= 4GHz. That maps to 1147 * ~ 0.06ppm granularity for NTP. 1148 */ 1149 sec = cs->mask; 1150 do_div(sec, freq); 1151 do_div(sec, scale); 1152 if (!sec) 1153 sec = 1; 1154 else if (sec > 600 && cs->mask > UINT_MAX) 1155 sec = 600; 1156 1157 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, 1158 NSEC_PER_SEC / scale, sec * scale); 1159 } 1160 1161 /* 1162 * If the uncertainty margin is not specified, calculate it. If 1163 * both scale and freq are non-zero, calculate the clock period, but 1164 * bound below at 2*WATCHDOG_MAX_SKEW, that is, 500ppm by default. 1165 * However, if either of scale or freq is zero, be very conservative 1166 * and take the tens-of-milliseconds WATCHDOG_THRESHOLD value 1167 * for the uncertainty margin. Allow stupidly small uncertainty 1168 * margins to be specified by the caller for testing purposes, 1169 * but warn to discourage production use of this capability. 1170 * 1171 * Bottom line: The sum of the uncertainty margins of the 1172 * watchdog clocksource and the clocksource under test will be at 1173 * least 500ppm by default. For more information, please see the 1174 * comment preceding CONFIG_CLOCKSOURCE_WATCHDOG_MAX_SKEW_US above. 1175 */ 1176 if (scale && freq && !cs->uncertainty_margin) { 1177 cs->uncertainty_margin = NSEC_PER_SEC / (scale * freq); 1178 if (cs->uncertainty_margin < 2 * WATCHDOG_MAX_SKEW) 1179 cs->uncertainty_margin = 2 * WATCHDOG_MAX_SKEW; 1180 } else if (!cs->uncertainty_margin) { 1181 cs->uncertainty_margin = WATCHDOG_THRESHOLD; 1182 } 1183 WARN_ON_ONCE(cs->uncertainty_margin < 2 * WATCHDOG_MAX_SKEW); 1184 1185 /* 1186 * Ensure clocksources that have large 'mult' values don't overflow 1187 * when adjusted. 1188 */ 1189 cs->maxadj = clocksource_max_adjustment(cs); 1190 while (freq && ((cs->mult + cs->maxadj < cs->mult) 1191 || (cs->mult - cs->maxadj > cs->mult))) { 1192 cs->mult >>= 1; 1193 cs->shift--; 1194 cs->maxadj = clocksource_max_adjustment(cs); 1195 } 1196 1197 /* 1198 * Only warn for *special* clocksources that self-define 1199 * their mult/shift values and don't specify a freq. 1200 */ 1201 WARN_ONCE(cs->mult + cs->maxadj < cs->mult, 1202 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n", 1203 cs->name); 1204 1205 clocksource_update_max_deferment(cs); 1206 1207 pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n", 1208 cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns); 1209 } 1210 EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale); 1211 1212 /** 1213 * __clocksource_register_scale - Used to install new clocksources 1214 * @cs: clocksource to be registered 1215 * @scale: Scale factor multiplied against freq to get clocksource hz 1216 * @freq: clocksource frequency (cycles per second) divided by scale 1217 * 1218 * Returns -EBUSY if registration fails, zero otherwise. 1219 * 1220 * This *SHOULD NOT* be called directly! Please use the 1221 * clocksource_register_hz() or clocksource_register_khz helper functions. 1222 */ 1223 int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq) 1224 { 1225 unsigned long flags; 1226 1227 clocksource_arch_init(cs); 1228 1229 if (WARN_ON_ONCE((unsigned int)cs->id >= CSID_MAX)) 1230 cs->id = CSID_GENERIC; 1231 if (cs->vdso_clock_mode < 0 || 1232 cs->vdso_clock_mode >= VDSO_CLOCKMODE_MAX) { 1233 pr_warn("clocksource %s registered with invalid VDSO mode %d. Disabling VDSO support.\n", 1234 cs->name, cs->vdso_clock_mode); 1235 cs->vdso_clock_mode = VDSO_CLOCKMODE_NONE; 1236 } 1237 1238 /* Initialize mult/shift and max_idle_ns */ 1239 __clocksource_update_freq_scale(cs, scale, freq); 1240 1241 /* Add clocksource to the clocksource list */ 1242 mutex_lock(&clocksource_mutex); 1243 1244 clocksource_watchdog_lock(&flags); 1245 clocksource_enqueue(cs); 1246 clocksource_enqueue_watchdog(cs); 1247 clocksource_watchdog_unlock(&flags); 1248 1249 clocksource_select(); 1250 clocksource_select_watchdog(false); 1251 __clocksource_suspend_select(cs); 1252 mutex_unlock(&clocksource_mutex); 1253 return 0; 1254 } 1255 EXPORT_SYMBOL_GPL(__clocksource_register_scale); 1256 1257 static void __clocksource_change_rating(struct clocksource *cs, int rating) 1258 { 1259 list_del(&cs->list); 1260 cs->rating = rating; 1261 clocksource_enqueue(cs); 1262 } 1263 1264 /** 1265 * clocksource_change_rating - Change the rating of a registered clocksource 1266 * @cs: clocksource to be changed 1267 * @rating: new rating 1268 */ 1269 void clocksource_change_rating(struct clocksource *cs, int rating) 1270 { 1271 unsigned long flags; 1272 1273 mutex_lock(&clocksource_mutex); 1274 clocksource_watchdog_lock(&flags); 1275 __clocksource_change_rating(cs, rating); 1276 clocksource_watchdog_unlock(&flags); 1277 1278 clocksource_select(); 1279 clocksource_select_watchdog(false); 1280 clocksource_suspend_select(false); 1281 mutex_unlock(&clocksource_mutex); 1282 } 1283 EXPORT_SYMBOL(clocksource_change_rating); 1284 1285 /* 1286 * Unbind clocksource @cs. Called with clocksource_mutex held 1287 */ 1288 static int clocksource_unbind(struct clocksource *cs) 1289 { 1290 unsigned long flags; 1291 1292 if (clocksource_is_watchdog(cs)) { 1293 /* Select and try to install a replacement watchdog. */ 1294 clocksource_select_watchdog(true); 1295 if (clocksource_is_watchdog(cs)) 1296 return -EBUSY; 1297 } 1298 1299 if (cs == curr_clocksource) { 1300 /* Select and try to install a replacement clock source */ 1301 clocksource_select_fallback(); 1302 if (curr_clocksource == cs) 1303 return -EBUSY; 1304 } 1305 1306 if (clocksource_is_suspend(cs)) { 1307 /* 1308 * Select and try to install a replacement suspend clocksource. 1309 * If no replacement suspend clocksource, we will just let the 1310 * clocksource go and have no suspend clocksource. 1311 */ 1312 clocksource_suspend_select(true); 1313 } 1314 1315 clocksource_watchdog_lock(&flags); 1316 clocksource_dequeue_watchdog(cs); 1317 list_del_init(&cs->list); 1318 clocksource_watchdog_unlock(&flags); 1319 1320 return 0; 1321 } 1322 1323 /** 1324 * clocksource_unregister - remove a registered clocksource 1325 * @cs: clocksource to be unregistered 1326 */ 1327 int clocksource_unregister(struct clocksource *cs) 1328 { 1329 int ret = 0; 1330 1331 mutex_lock(&clocksource_mutex); 1332 if (!list_empty(&cs->list)) 1333 ret = clocksource_unbind(cs); 1334 mutex_unlock(&clocksource_mutex); 1335 return ret; 1336 } 1337 EXPORT_SYMBOL(clocksource_unregister); 1338 1339 #ifdef CONFIG_SYSFS 1340 /** 1341 * current_clocksource_show - sysfs interface for current clocksource 1342 * @dev: unused 1343 * @attr: unused 1344 * @buf: char buffer to be filled with clocksource list 1345 * 1346 * Provides sysfs interface for listing current clocksource. 1347 */ 1348 static ssize_t current_clocksource_show(struct device *dev, 1349 struct device_attribute *attr, 1350 char *buf) 1351 { 1352 ssize_t count = 0; 1353 1354 mutex_lock(&clocksource_mutex); 1355 count = sysfs_emit(buf, "%s\n", curr_clocksource->name); 1356 mutex_unlock(&clocksource_mutex); 1357 1358 return count; 1359 } 1360 1361 ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt) 1362 { 1363 size_t ret = cnt; 1364 1365 /* strings from sysfs write are not 0 terminated! */ 1366 if (!cnt || cnt >= CS_NAME_LEN) 1367 return -EINVAL; 1368 1369 /* strip of \n: */ 1370 if (buf[cnt-1] == '\n') 1371 cnt--; 1372 if (cnt > 0) 1373 memcpy(dst, buf, cnt); 1374 dst[cnt] = 0; 1375 return ret; 1376 } 1377 1378 /** 1379 * current_clocksource_store - interface for manually overriding clocksource 1380 * @dev: unused 1381 * @attr: unused 1382 * @buf: name of override clocksource 1383 * @count: length of buffer 1384 * 1385 * Takes input from sysfs interface for manually overriding the default 1386 * clocksource selection. 1387 */ 1388 static ssize_t current_clocksource_store(struct device *dev, 1389 struct device_attribute *attr, 1390 const char *buf, size_t count) 1391 { 1392 ssize_t ret; 1393 1394 mutex_lock(&clocksource_mutex); 1395 1396 ret = sysfs_get_uname(buf, override_name, count); 1397 if (ret >= 0) 1398 clocksource_select(); 1399 1400 mutex_unlock(&clocksource_mutex); 1401 1402 return ret; 1403 } 1404 static DEVICE_ATTR_RW(current_clocksource); 1405 1406 /** 1407 * unbind_clocksource_store - interface for manually unbinding clocksource 1408 * @dev: unused 1409 * @attr: unused 1410 * @buf: unused 1411 * @count: length of buffer 1412 * 1413 * Takes input from sysfs interface for manually unbinding a clocksource. 1414 */ 1415 static ssize_t unbind_clocksource_store(struct device *dev, 1416 struct device_attribute *attr, 1417 const char *buf, size_t count) 1418 { 1419 struct clocksource *cs; 1420 char name[CS_NAME_LEN]; 1421 ssize_t ret; 1422 1423 ret = sysfs_get_uname(buf, name, count); 1424 if (ret < 0) 1425 return ret; 1426 1427 ret = -ENODEV; 1428 mutex_lock(&clocksource_mutex); 1429 list_for_each_entry(cs, &clocksource_list, list) { 1430 if (strcmp(cs->name, name)) 1431 continue; 1432 ret = clocksource_unbind(cs); 1433 break; 1434 } 1435 mutex_unlock(&clocksource_mutex); 1436 1437 return ret ? ret : count; 1438 } 1439 static DEVICE_ATTR_WO(unbind_clocksource); 1440 1441 /** 1442 * available_clocksource_show - sysfs interface for listing clocksource 1443 * @dev: unused 1444 * @attr: unused 1445 * @buf: char buffer to be filled with clocksource list 1446 * 1447 * Provides sysfs interface for listing registered clocksources 1448 */ 1449 static ssize_t available_clocksource_show(struct device *dev, 1450 struct device_attribute *attr, 1451 char *buf) 1452 { 1453 struct clocksource *src; 1454 ssize_t count = 0; 1455 1456 mutex_lock(&clocksource_mutex); 1457 list_for_each_entry(src, &clocksource_list, list) { 1458 /* 1459 * Don't show non-HRES clocksource if the tick code is 1460 * in one shot mode (highres=on or nohz=on) 1461 */ 1462 if (!tick_oneshot_mode_active() || 1463 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES)) 1464 count += snprintf(buf + count, 1465 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), 1466 "%s ", src->name); 1467 } 1468 mutex_unlock(&clocksource_mutex); 1469 1470 count += snprintf(buf + count, 1471 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n"); 1472 1473 return count; 1474 } 1475 static DEVICE_ATTR_RO(available_clocksource); 1476 1477 static struct attribute *clocksource_attrs[] = { 1478 &dev_attr_current_clocksource.attr, 1479 &dev_attr_unbind_clocksource.attr, 1480 &dev_attr_available_clocksource.attr, 1481 NULL 1482 }; 1483 ATTRIBUTE_GROUPS(clocksource); 1484 1485 static const struct bus_type clocksource_subsys = { 1486 .name = "clocksource", 1487 .dev_name = "clocksource", 1488 }; 1489 1490 static struct device device_clocksource = { 1491 .id = 0, 1492 .bus = &clocksource_subsys, 1493 .groups = clocksource_groups, 1494 }; 1495 1496 static int __init init_clocksource_sysfs(void) 1497 { 1498 int error = subsys_system_register(&clocksource_subsys, NULL); 1499 1500 if (!error) 1501 error = device_register(&device_clocksource); 1502 1503 return error; 1504 } 1505 1506 device_initcall(init_clocksource_sysfs); 1507 #endif /* CONFIG_SYSFS */ 1508 1509 /** 1510 * boot_override_clocksource - boot clock override 1511 * @str: override name 1512 * 1513 * Takes a clocksource= boot argument and uses it 1514 * as the clocksource override name. 1515 */ 1516 static int __init boot_override_clocksource(char* str) 1517 { 1518 mutex_lock(&clocksource_mutex); 1519 if (str) 1520 strscpy(override_name, str, sizeof(override_name)); 1521 mutex_unlock(&clocksource_mutex); 1522 return 1; 1523 } 1524 1525 __setup("clocksource=", boot_override_clocksource); 1526 1527 /** 1528 * boot_override_clock - Compatibility layer for deprecated boot option 1529 * @str: override name 1530 * 1531 * DEPRECATED! Takes a clock= boot argument and uses it 1532 * as the clocksource override name 1533 */ 1534 static int __init boot_override_clock(char* str) 1535 { 1536 if (!strcmp(str, "pmtmr")) { 1537 pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n"); 1538 return boot_override_clocksource("acpi_pm"); 1539 } 1540 pr_warn("clock= boot option is deprecated - use clocksource=xyz\n"); 1541 return boot_override_clocksource(str); 1542 } 1543 1544 __setup("clock=", boot_override_clock); 1545