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