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