.. | .. |
---|
54 | 54 | */ |
---|
55 | 55 | static void tick_do_update_jiffies64(ktime_t now) |
---|
56 | 56 | { |
---|
57 | | - unsigned long ticks = 0; |
---|
| 57 | + unsigned long ticks = 1; |
---|
58 | 58 | ktime_t delta; |
---|
59 | 59 | |
---|
60 | 60 | /* |
---|
61 | | - * Do a quick check without holding jiffies_lock: |
---|
62 | | - * The READ_ONCE() pairs with two updates done later in this function. |
---|
| 61 | + * Do a quick check without holding jiffies_lock. The READ_ONCE() |
---|
| 62 | + * pairs with the update done later in this function. |
---|
| 63 | + * |
---|
| 64 | + * This is also an intentional data race which is even safe on |
---|
| 65 | + * 32bit in theory. If there is a concurrent update then the check |
---|
| 66 | + * might give a random answer. It does not matter because if it |
---|
| 67 | + * returns then the concurrent update is already taking care, if it |
---|
| 68 | + * falls through then it will pointlessly contend on jiffies_lock. |
---|
| 69 | + * |
---|
| 70 | + * Though there is one nasty case on 32bit due to store tearing of |
---|
| 71 | + * the 64bit value. If the first 32bit store makes the quick check |
---|
| 72 | + * return on all other CPUs and the writing CPU context gets |
---|
| 73 | + * delayed to complete the second store (scheduled out on virt) |
---|
| 74 | + * then jiffies can become stale for up to ~2^32 nanoseconds |
---|
| 75 | + * without noticing. After that point all CPUs will wait for |
---|
| 76 | + * jiffies lock. |
---|
| 77 | + * |
---|
| 78 | + * OTOH, this is not any different than the situation with NOHZ=off |
---|
| 79 | + * where one CPU is responsible for updating jiffies and |
---|
| 80 | + * timekeeping. If that CPU goes out for lunch then all other CPUs |
---|
| 81 | + * will operate on stale jiffies until it decides to come back. |
---|
63 | 82 | */ |
---|
64 | | - delta = ktime_sub(now, READ_ONCE(last_jiffies_update)); |
---|
65 | | - if (delta < tick_period) |
---|
| 83 | + if (ktime_before(now, READ_ONCE(tick_next_period))) |
---|
66 | 84 | return; |
---|
67 | 85 | |
---|
68 | 86 | /* Reevaluate with jiffies_lock held */ |
---|
69 | 87 | raw_spin_lock(&jiffies_lock); |
---|
70 | | - write_seqcount_begin(&jiffies_seq); |
---|
71 | | - |
---|
72 | | - delta = ktime_sub(now, last_jiffies_update); |
---|
73 | | - if (delta >= tick_period) { |
---|
74 | | - |
---|
75 | | - delta = ktime_sub(delta, tick_period); |
---|
76 | | - /* Pairs with the lockless read in this function. */ |
---|
77 | | - WRITE_ONCE(last_jiffies_update, |
---|
78 | | - ktime_add(last_jiffies_update, tick_period)); |
---|
79 | | - |
---|
80 | | - /* Slow path for long timeouts */ |
---|
81 | | - if (unlikely(delta >= tick_period)) { |
---|
82 | | - s64 incr = ktime_to_ns(tick_period); |
---|
83 | | - |
---|
84 | | - ticks = ktime_divns(delta, incr); |
---|
85 | | - |
---|
86 | | - /* Pairs with the lockless read in this function. */ |
---|
87 | | - WRITE_ONCE(last_jiffies_update, |
---|
88 | | - ktime_add_ns(last_jiffies_update, |
---|
89 | | - incr * ticks)); |
---|
90 | | - } |
---|
91 | | - do_timer(++ticks); |
---|
92 | | - |
---|
93 | | - /* Keep the tick_next_period variable up to date */ |
---|
94 | | - tick_next_period = ktime_add(last_jiffies_update, tick_period); |
---|
95 | | - } else { |
---|
96 | | - write_seqcount_end(&jiffies_seq); |
---|
| 88 | + if (ktime_before(now, tick_next_period)) { |
---|
97 | 89 | raw_spin_unlock(&jiffies_lock); |
---|
98 | 90 | return; |
---|
99 | 91 | } |
---|
| 92 | + |
---|
| 93 | + write_seqcount_begin(&jiffies_seq); |
---|
| 94 | + |
---|
| 95 | + delta = ktime_sub(now, tick_next_period); |
---|
| 96 | + if (unlikely(delta >= TICK_NSEC)) { |
---|
| 97 | + /* Slow path for long idle sleep times */ |
---|
| 98 | + s64 incr = TICK_NSEC; |
---|
| 99 | + |
---|
| 100 | + ticks += ktime_divns(delta, incr); |
---|
| 101 | + |
---|
| 102 | + last_jiffies_update = ktime_add_ns(last_jiffies_update, |
---|
| 103 | + incr * ticks); |
---|
| 104 | + } else { |
---|
| 105 | + last_jiffies_update = ktime_add_ns(last_jiffies_update, |
---|
| 106 | + TICK_NSEC); |
---|
| 107 | + } |
---|
| 108 | + |
---|
| 109 | + do_timer(ticks); |
---|
| 110 | + |
---|
| 111 | + /* |
---|
| 112 | + * Keep the tick_next_period variable up to date. WRITE_ONCE() |
---|
| 113 | + * pairs with the READ_ONCE() in the lockless quick check above. |
---|
| 114 | + */ |
---|
| 115 | + WRITE_ONCE(tick_next_period, |
---|
| 116 | + ktime_add_ns(last_jiffies_update, TICK_NSEC)); |
---|
| 117 | + |
---|
100 | 118 | write_seqcount_end(&jiffies_seq); |
---|
101 | 119 | raw_spin_unlock(&jiffies_lock); |
---|
102 | 120 | update_wall_time(); |
---|
.. | .. |
---|
112 | 130 | raw_spin_lock(&jiffies_lock); |
---|
113 | 131 | write_seqcount_begin(&jiffies_seq); |
---|
114 | 132 | /* Did we start the jiffies update yet ? */ |
---|
115 | | - if (last_jiffies_update == 0) |
---|
| 133 | + if (last_jiffies_update == 0) { |
---|
| 134 | + u32 rem; |
---|
| 135 | + |
---|
| 136 | + /* |
---|
| 137 | + * Ensure that the tick is aligned to a multiple of |
---|
| 138 | + * TICK_NSEC. |
---|
| 139 | + */ |
---|
| 140 | + div_u64_rem(tick_next_period, TICK_NSEC, &rem); |
---|
| 141 | + if (rem) |
---|
| 142 | + tick_next_period += TICK_NSEC - rem; |
---|
| 143 | + |
---|
116 | 144 | last_jiffies_update = tick_next_period; |
---|
| 145 | + } |
---|
117 | 146 | period = last_jiffies_update; |
---|
118 | 147 | write_seqcount_end(&jiffies_seq); |
---|
119 | 148 | raw_spin_unlock(&jiffies_lock); |
---|
120 | 149 | return period; |
---|
121 | 150 | } |
---|
| 151 | + |
---|
| 152 | +#define MAX_STALLED_JIFFIES 5 |
---|
122 | 153 | |
---|
123 | 154 | static void tick_sched_do_timer(struct tick_sched *ts, ktime_t now) |
---|
124 | 155 | { |
---|
.. | .. |
---|
147 | 178 | if (tick_do_timer_cpu == cpu) { |
---|
148 | 179 | tick_do_update_jiffies64(now); |
---|
149 | 180 | trace_android_vh_jiffies_update(NULL); |
---|
| 181 | + } |
---|
| 182 | + |
---|
| 183 | + /* |
---|
| 184 | + * If jiffies update stalled for too long (timekeeper in stop_machine() |
---|
| 185 | + * or VMEXIT'ed for several msecs), force an update. |
---|
| 186 | + */ |
---|
| 187 | + if (ts->last_tick_jiffies != jiffies) { |
---|
| 188 | + ts->stalled_jiffies = 0; |
---|
| 189 | + ts->last_tick_jiffies = READ_ONCE(jiffies); |
---|
| 190 | + } else { |
---|
| 191 | + if (++ts->stalled_jiffies == MAX_STALLED_JIFFIES) { |
---|
| 192 | + tick_do_update_jiffies64(now); |
---|
| 193 | + ts->stalled_jiffies = 0; |
---|
| 194 | + ts->last_tick_jiffies = READ_ONCE(jiffies); |
---|
| 195 | + } |
---|
150 | 196 | } |
---|
151 | 197 | |
---|
152 | 198 | if (ts->inidle) |
---|
.. | .. |
---|
213 | 259 | |
---|
214 | 260 | if (val & TICK_DEP_MASK_RCU) { |
---|
215 | 261 | trace_tick_stop(0, TICK_DEP_MASK_RCU); |
---|
| 262 | + return true; |
---|
| 263 | + } |
---|
| 264 | + |
---|
| 265 | + if (val & TICK_DEP_MASK_RCU_EXP) { |
---|
| 266 | + trace_tick_stop(0, TICK_DEP_MASK_RCU_EXP); |
---|
216 | 267 | return true; |
---|
217 | 268 | } |
---|
218 | 269 | |
---|
.. | .. |
---|
429 | 480 | tick_nohz_full_running = true; |
---|
430 | 481 | } |
---|
431 | 482 | |
---|
432 | | -static int tick_nohz_cpu_down(unsigned int cpu) |
---|
| 483 | +bool tick_nohz_cpu_hotpluggable(unsigned int cpu) |
---|
433 | 484 | { |
---|
434 | 485 | /* |
---|
435 | 486 | * The tick_do_timer_cpu CPU handles housekeeping duty (unbound |
---|
.. | .. |
---|
437 | 488 | * CPUs. It must remain online when nohz full is enabled. |
---|
438 | 489 | */ |
---|
439 | 490 | if (tick_nohz_full_running && tick_do_timer_cpu == cpu) |
---|
440 | | - return -EBUSY; |
---|
441 | | - return 0; |
---|
| 491 | + return false; |
---|
| 492 | + return true; |
---|
| 493 | +} |
---|
| 494 | + |
---|
| 495 | +static int tick_nohz_cpu_down(unsigned int cpu) |
---|
| 496 | +{ |
---|
| 497 | + return tick_nohz_cpu_hotpluggable(cpu) ? 0 : -EBUSY; |
---|
442 | 498 | } |
---|
443 | 499 | |
---|
444 | 500 | void __init tick_nohz_init(void) |
---|
.. | .. |
---|
663 | 719 | hrtimer_set_expires(&ts->sched_timer, ts->last_tick); |
---|
664 | 720 | |
---|
665 | 721 | /* Forward the time to expire in the future */ |
---|
666 | | - hrtimer_forward(&ts->sched_timer, now, tick_period); |
---|
| 722 | + hrtimer_forward(&ts->sched_timer, now, TICK_NSEC); |
---|
667 | 723 | |
---|
668 | 724 | if (ts->nohz_mode == NOHZ_MODE_HIGHRES) { |
---|
669 | 725 | hrtimer_start_expires(&ts->sched_timer, |
---|
.. | .. |
---|
831 | 887 | if (unlikely(expires == KTIME_MAX)) { |
---|
832 | 888 | if (ts->nohz_mode == NOHZ_MODE_HIGHRES) |
---|
833 | 889 | hrtimer_cancel(&ts->sched_timer); |
---|
| 890 | + else |
---|
| 891 | + tick_program_event(KTIME_MAX, 1); |
---|
834 | 892 | return; |
---|
835 | 893 | } |
---|
836 | 894 | |
---|
.. | .. |
---|
1223 | 1281 | tick_sched_do_timer(ts, now); |
---|
1224 | 1282 | tick_sched_handle(ts, regs); |
---|
1225 | 1283 | |
---|
1226 | | - /* No need to reprogram if we are running tickless */ |
---|
1227 | | - if (unlikely(ts->tick_stopped)) |
---|
| 1284 | + if (unlikely(ts->tick_stopped)) { |
---|
| 1285 | + /* |
---|
| 1286 | + * The clockevent device is not reprogrammed, so change the |
---|
| 1287 | + * clock event device to ONESHOT_STOPPED to avoid spurious |
---|
| 1288 | + * interrupts on devices which might not be truly one shot. |
---|
| 1289 | + */ |
---|
| 1290 | + tick_program_event(KTIME_MAX, 1); |
---|
1228 | 1291 | return; |
---|
| 1292 | + } |
---|
1229 | 1293 | |
---|
1230 | | - hrtimer_forward(&ts->sched_timer, now, tick_period); |
---|
| 1294 | + hrtimer_forward(&ts->sched_timer, now, TICK_NSEC); |
---|
1231 | 1295 | tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1); |
---|
1232 | 1296 | } |
---|
1233 | 1297 | |
---|
.. | .. |
---|
1264 | 1328 | next = tick_init_jiffy_update(); |
---|
1265 | 1329 | |
---|
1266 | 1330 | hrtimer_set_expires(&ts->sched_timer, next); |
---|
1267 | | - hrtimer_forward_now(&ts->sched_timer, tick_period); |
---|
| 1331 | + hrtimer_forward_now(&ts->sched_timer, TICK_NSEC); |
---|
1268 | 1332 | tick_program_event(hrtimer_get_expires(&ts->sched_timer), 1); |
---|
1269 | 1333 | tick_nohz_activate(ts, NOHZ_MODE_LOWRES); |
---|
1270 | 1334 | } |
---|
.. | .. |
---|
1330 | 1394 | if (unlikely(ts->tick_stopped)) |
---|
1331 | 1395 | return HRTIMER_NORESTART; |
---|
1332 | 1396 | |
---|
1333 | | - hrtimer_forward(timer, now, tick_period); |
---|
| 1397 | + hrtimer_forward(timer, now, TICK_NSEC); |
---|
1334 | 1398 | |
---|
1335 | 1399 | return HRTIMER_RESTART; |
---|
1336 | 1400 | } |
---|
.. | .. |
---|
1364 | 1428 | |
---|
1365 | 1429 | /* Offset the tick to avert jiffies_lock contention. */ |
---|
1366 | 1430 | if (sched_skew_tick) { |
---|
1367 | | - u64 offset = ktime_to_ns(tick_period) >> 1; |
---|
| 1431 | + u64 offset = TICK_NSEC >> 1; |
---|
1368 | 1432 | do_div(offset, num_possible_cpus()); |
---|
1369 | 1433 | offset *= smp_processor_id(); |
---|
1370 | 1434 | hrtimer_add_expires_ns(&ts->sched_timer, offset); |
---|
1371 | 1435 | } |
---|
1372 | 1436 | |
---|
1373 | | - hrtimer_forward(&ts->sched_timer, now, tick_period); |
---|
| 1437 | + hrtimer_forward(&ts->sched_timer, now, TICK_NSEC); |
---|
1374 | 1438 | hrtimer_start_expires(&ts->sched_timer, HRTIMER_MODE_ABS_PINNED_HARD); |
---|
1375 | 1439 | tick_nohz_activate(ts, NOHZ_MODE_HIGHRES); |
---|
1376 | 1440 | } |
---|