.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
---|
1 | 2 | /* |
---|
2 | | - * linux/kernel/hrtimer.c |
---|
3 | | - * |
---|
4 | 3 | * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de> |
---|
5 | 4 | * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar |
---|
6 | 5 | * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner |
---|
7 | 6 | * |
---|
8 | 7 | * High-resolution kernel timers |
---|
9 | 8 | * |
---|
10 | | - * In contrast to the low-resolution timeout API implemented in |
---|
11 | | - * kernel/timer.c, hrtimers provide finer resolution and accuracy |
---|
12 | | - * depending on system configuration and capabilities. |
---|
13 | | - * |
---|
14 | | - * These timers are currently used for: |
---|
15 | | - * - itimers |
---|
16 | | - * - POSIX timers |
---|
17 | | - * - nanosleep |
---|
18 | | - * - precise in-kernel timing |
---|
| 9 | + * In contrast to the low-resolution timeout API, aka timer wheel, |
---|
| 10 | + * hrtimers provide finer resolution and accuracy depending on system |
---|
| 11 | + * configuration and capabilities. |
---|
19 | 12 | * |
---|
20 | 13 | * Started by: Thomas Gleixner and Ingo Molnar |
---|
21 | 14 | * |
---|
22 | 15 | * Credits: |
---|
23 | | - * based on kernel/timer.c |
---|
| 16 | + * Based on the original timer wheel code |
---|
24 | 17 | * |
---|
25 | 18 | * Help, testing, suggestions, bugfixes, improvements were |
---|
26 | 19 | * provided by: |
---|
27 | 20 | * |
---|
28 | 21 | * George Anzinger, Andrew Morton, Steven Rostedt, Roman Zippel |
---|
29 | 22 | * et. al. |
---|
30 | | - * |
---|
31 | | - * For licencing details see kernel-base/COPYING |
---|
32 | 23 | */ |
---|
33 | 24 | |
---|
34 | 25 | #include <linux/cpu.h> |
---|
.. | .. |
---|
39 | 30 | #include <linux/syscalls.h> |
---|
40 | 31 | #include <linux/interrupt.h> |
---|
41 | 32 | #include <linux/tick.h> |
---|
42 | | -#include <linux/seq_file.h> |
---|
43 | 33 | #include <linux/err.h> |
---|
44 | 34 | #include <linux/debugobjects.h> |
---|
45 | 35 | #include <linux/sched/signal.h> |
---|
.. | .. |
---|
145 | 135 | * timer->base->cpu_base |
---|
146 | 136 | */ |
---|
147 | 137 | static struct hrtimer_cpu_base migration_cpu_base = { |
---|
148 | | - .clock_base = { { .cpu_base = &migration_cpu_base, }, }, |
---|
| 138 | + .clock_base = { { |
---|
| 139 | + .cpu_base = &migration_cpu_base, |
---|
| 140 | + .seq = SEQCNT_RAW_SPINLOCK_ZERO(migration_cpu_base.seq, |
---|
| 141 | + &migration_cpu_base.lock), |
---|
| 142 | + }, }, |
---|
149 | 143 | }; |
---|
150 | 144 | |
---|
151 | 145 | #define migration_base migration_cpu_base.clock_base[0] |
---|
.. | .. |
---|
174 | 168 | struct hrtimer_clock_base *base; |
---|
175 | 169 | |
---|
176 | 170 | for (;;) { |
---|
177 | | - base = timer->base; |
---|
| 171 | + base = READ_ONCE(timer->base); |
---|
178 | 172 | if (likely(base != &migration_base)) { |
---|
179 | 173 | raw_spin_lock_irqsave(&base->cpu_base->lock, *flags); |
---|
180 | 174 | if (likely(base == timer->base)) |
---|
.. | .. |
---|
254 | 248 | return base; |
---|
255 | 249 | |
---|
256 | 250 | /* See the comment in lock_hrtimer_base() */ |
---|
257 | | - timer->base = &migration_base; |
---|
| 251 | + WRITE_ONCE(timer->base, &migration_base); |
---|
258 | 252 | raw_spin_unlock(&base->cpu_base->lock); |
---|
259 | 253 | raw_spin_lock(&new_base->cpu_base->lock); |
---|
260 | 254 | |
---|
.. | .. |
---|
263 | 257 | raw_spin_unlock(&new_base->cpu_base->lock); |
---|
264 | 258 | raw_spin_lock(&base->cpu_base->lock); |
---|
265 | 259 | new_cpu_base = this_cpu_base; |
---|
266 | | - timer->base = base; |
---|
| 260 | + WRITE_ONCE(timer->base, base); |
---|
267 | 261 | goto again; |
---|
268 | 262 | } |
---|
269 | | - timer->base = new_base; |
---|
| 263 | + WRITE_ONCE(timer->base, new_base); |
---|
270 | 264 | } else { |
---|
271 | 265 | if (new_cpu_base != this_cpu_base && |
---|
272 | 266 | hrtimer_check_target(timer, new_base)) { |
---|
.. | .. |
---|
321 | 315 | div >>= 1; |
---|
322 | 316 | } |
---|
323 | 317 | tmp >>= sft; |
---|
324 | | - do_div(tmp, (unsigned long) div); |
---|
| 318 | + do_div(tmp, (u32) div); |
---|
325 | 319 | return dclc < 0 ? -tmp : tmp; |
---|
326 | 320 | } |
---|
327 | 321 | EXPORT_SYMBOL_GPL(__ktime_divns); |
---|
.. | .. |
---|
348 | 342 | |
---|
349 | 343 | #ifdef CONFIG_DEBUG_OBJECTS_TIMERS |
---|
350 | 344 | |
---|
351 | | -static struct debug_obj_descr hrtimer_debug_descr; |
---|
| 345 | +static const struct debug_obj_descr hrtimer_debug_descr; |
---|
352 | 346 | |
---|
353 | 347 | static void *hrtimer_debug_hint(void *addr) |
---|
354 | 348 | { |
---|
.. | .. |
---|
383 | 377 | switch (state) { |
---|
384 | 378 | case ODEBUG_STATE_ACTIVE: |
---|
385 | 379 | WARN_ON(1); |
---|
386 | | - |
---|
| 380 | + fallthrough; |
---|
387 | 381 | default: |
---|
388 | 382 | return false; |
---|
389 | 383 | } |
---|
.. | .. |
---|
407 | 401 | } |
---|
408 | 402 | } |
---|
409 | 403 | |
---|
410 | | -static struct debug_obj_descr hrtimer_debug_descr = { |
---|
| 404 | +static const struct debug_obj_descr hrtimer_debug_descr = { |
---|
411 | 405 | .name = "hrtimer", |
---|
412 | 406 | .debug_hint = hrtimer_debug_hint, |
---|
413 | 407 | .fixup_init = hrtimer_fixup_init, |
---|
.. | .. |
---|
431 | 425 | debug_object_deactivate(timer, &hrtimer_debug_descr); |
---|
432 | 426 | } |
---|
433 | 427 | |
---|
434 | | -static inline void debug_hrtimer_free(struct hrtimer *timer) |
---|
435 | | -{ |
---|
436 | | - debug_object_free(timer, &hrtimer_debug_descr); |
---|
437 | | -} |
---|
438 | | - |
---|
439 | 428 | static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
---|
440 | 429 | enum hrtimer_mode mode); |
---|
441 | 430 | |
---|
.. | .. |
---|
446 | 435 | __hrtimer_init(timer, clock_id, mode); |
---|
447 | 436 | } |
---|
448 | 437 | EXPORT_SYMBOL_GPL(hrtimer_init_on_stack); |
---|
| 438 | + |
---|
| 439 | +static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl, |
---|
| 440 | + clockid_t clock_id, enum hrtimer_mode mode); |
---|
| 441 | + |
---|
| 442 | +void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl, |
---|
| 443 | + clockid_t clock_id, enum hrtimer_mode mode) |
---|
| 444 | +{ |
---|
| 445 | + debug_object_init_on_stack(&sl->timer, &hrtimer_debug_descr); |
---|
| 446 | + __hrtimer_init_sleeper(sl, clock_id, mode); |
---|
| 447 | +} |
---|
| 448 | +EXPORT_SYMBOL_GPL(hrtimer_init_sleeper_on_stack); |
---|
449 | 449 | |
---|
450 | 450 | void destroy_hrtimer_on_stack(struct hrtimer *timer) |
---|
451 | 451 | { |
---|
.. | .. |
---|
758 | 758 | retrigger_next_event(NULL); |
---|
759 | 759 | } |
---|
760 | 760 | |
---|
761 | | -static void clock_was_set_work(struct work_struct *work) |
---|
762 | | -{ |
---|
763 | | - clock_was_set(); |
---|
764 | | -} |
---|
765 | | - |
---|
766 | | -static DECLARE_WORK(hrtimer_work, clock_was_set_work); |
---|
767 | | - |
---|
768 | | -/* |
---|
769 | | - * Called from timekeeping and resume code to reprogram the hrtimer |
---|
770 | | - * interrupt device on all cpus. |
---|
771 | | - */ |
---|
772 | | -void clock_was_set_delayed(void) |
---|
773 | | -{ |
---|
774 | | - schedule_work(&hrtimer_work); |
---|
775 | | -} |
---|
776 | | - |
---|
777 | 761 | #else |
---|
778 | 762 | |
---|
779 | 763 | static inline int hrtimer_is_hres_enabled(void) { return 0; } |
---|
.. | .. |
---|
891 | 875 | timerfd_clock_was_set(); |
---|
892 | 876 | } |
---|
893 | 877 | |
---|
| 878 | +static void clock_was_set_work(struct work_struct *work) |
---|
| 879 | +{ |
---|
| 880 | + clock_was_set(); |
---|
| 881 | +} |
---|
| 882 | + |
---|
| 883 | +static DECLARE_WORK(hrtimer_work, clock_was_set_work); |
---|
| 884 | + |
---|
| 885 | +/* |
---|
| 886 | + * Called from timekeeping and resume code to reprogram the hrtimer |
---|
| 887 | + * interrupt device on all cpus and to notify timerfd. |
---|
| 888 | + */ |
---|
| 889 | +void clock_was_set_delayed(void) |
---|
| 890 | +{ |
---|
| 891 | + schedule_work(&hrtimer_work); |
---|
| 892 | +} |
---|
| 893 | + |
---|
894 | 894 | /* |
---|
895 | 895 | * During resume we might have to reprogram the high resolution timer |
---|
896 | 896 | * interrupt on all online CPUs. However, all other CPUs will be |
---|
.. | .. |
---|
966 | 966 | return orun; |
---|
967 | 967 | } |
---|
968 | 968 | EXPORT_SYMBOL_GPL(hrtimer_forward); |
---|
969 | | - |
---|
970 | | -void hrtimer_grab_expiry_lock(const struct hrtimer *timer) |
---|
971 | | -{ |
---|
972 | | - struct hrtimer_clock_base *base = READ_ONCE(timer->base); |
---|
973 | | - |
---|
974 | | - if (timer->is_soft && !is_migration_base(base)) { |
---|
975 | | - spin_lock(&base->cpu_base->softirq_expiry_lock); |
---|
976 | | - spin_unlock(&base->cpu_base->softirq_expiry_lock); |
---|
977 | | - } |
---|
978 | | -} |
---|
979 | 969 | |
---|
980 | 970 | /* |
---|
981 | 971 | * enqueue_hrtimer - internal function to (re)start a timer |
---|
.. | .. |
---|
1193 | 1183 | |
---|
1194 | 1184 | /* |
---|
1195 | 1185 | * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft |
---|
1196 | | - * match. |
---|
| 1186 | + * match on CONFIG_PREEMPT_RT = n. With PREEMPT_RT check the hard |
---|
| 1187 | + * expiry mode because unmarked timers are moved to softirq expiry. |
---|
1197 | 1188 | */ |
---|
1198 | | -#ifndef CONFIG_PREEMPT_RT_BASE |
---|
1199 | | - WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft); |
---|
1200 | | -#endif |
---|
| 1189 | + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) |
---|
| 1190 | + WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft); |
---|
| 1191 | + else |
---|
| 1192 | + WARN_ON_ONCE(!(mode & HRTIMER_MODE_HARD) ^ !timer->is_hard); |
---|
1201 | 1193 | |
---|
1202 | 1194 | base = lock_hrtimer_base(timer, &flags); |
---|
1203 | 1195 | |
---|
.. | .. |
---|
1213 | 1205 | * @timer: hrtimer to stop |
---|
1214 | 1206 | * |
---|
1215 | 1207 | * Returns: |
---|
1216 | | - * 0 when the timer was not active |
---|
1217 | | - * 1 when the timer was active |
---|
1218 | | - * -1 when the timer is currently executing the callback function and |
---|
| 1208 | + * |
---|
| 1209 | + * * 0 when the timer was not active |
---|
| 1210 | + * * 1 when the timer was active |
---|
| 1211 | + * * -1 when the timer is currently executing the callback function and |
---|
1219 | 1212 | * cannot be stopped |
---|
1220 | 1213 | */ |
---|
1221 | 1214 | int hrtimer_try_to_cancel(struct hrtimer *timer) |
---|
.. | .. |
---|
1245 | 1238 | } |
---|
1246 | 1239 | EXPORT_SYMBOL_GPL(hrtimer_try_to_cancel); |
---|
1247 | 1240 | |
---|
| 1241 | +#ifdef CONFIG_PREEMPT_RT |
---|
| 1242 | +static void hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base) |
---|
| 1243 | +{ |
---|
| 1244 | + spin_lock_init(&base->softirq_expiry_lock); |
---|
| 1245 | +} |
---|
| 1246 | + |
---|
| 1247 | +static void hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base) |
---|
| 1248 | +{ |
---|
| 1249 | + spin_lock(&base->softirq_expiry_lock); |
---|
| 1250 | +} |
---|
| 1251 | + |
---|
| 1252 | +static void hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base) |
---|
| 1253 | +{ |
---|
| 1254 | + spin_unlock(&base->softirq_expiry_lock); |
---|
| 1255 | +} |
---|
| 1256 | + |
---|
| 1257 | +/* |
---|
| 1258 | + * The counterpart to hrtimer_cancel_wait_running(). |
---|
| 1259 | + * |
---|
| 1260 | + * If there is a waiter for cpu_base->expiry_lock, then it was waiting for |
---|
| 1261 | + * the timer callback to finish. Drop expiry_lock and reaquire it. That |
---|
| 1262 | + * allows the waiter to acquire the lock and make progress. |
---|
| 1263 | + */ |
---|
| 1264 | +static void hrtimer_sync_wait_running(struct hrtimer_cpu_base *cpu_base, |
---|
| 1265 | + unsigned long flags) |
---|
| 1266 | +{ |
---|
| 1267 | + if (atomic_read(&cpu_base->timer_waiters)) { |
---|
| 1268 | + raw_spin_unlock_irqrestore(&cpu_base->lock, flags); |
---|
| 1269 | + spin_unlock(&cpu_base->softirq_expiry_lock); |
---|
| 1270 | + spin_lock(&cpu_base->softirq_expiry_lock); |
---|
| 1271 | + raw_spin_lock_irq(&cpu_base->lock); |
---|
| 1272 | + } |
---|
| 1273 | +} |
---|
| 1274 | + |
---|
| 1275 | +/* |
---|
| 1276 | + * This function is called on PREEMPT_RT kernels when the fast path |
---|
| 1277 | + * deletion of a timer failed because the timer callback function was |
---|
| 1278 | + * running. |
---|
| 1279 | + * |
---|
| 1280 | + * This prevents priority inversion: if the soft irq thread is preempted |
---|
| 1281 | + * in the middle of a timer callback, then calling del_timer_sync() can |
---|
| 1282 | + * lead to two issues: |
---|
| 1283 | + * |
---|
| 1284 | + * - If the caller is on a remote CPU then it has to spin wait for the timer |
---|
| 1285 | + * handler to complete. This can result in unbound priority inversion. |
---|
| 1286 | + * |
---|
| 1287 | + * - If the caller originates from the task which preempted the timer |
---|
| 1288 | + * handler on the same CPU, then spin waiting for the timer handler to |
---|
| 1289 | + * complete is never going to end. |
---|
| 1290 | + */ |
---|
| 1291 | +void hrtimer_cancel_wait_running(const struct hrtimer *timer) |
---|
| 1292 | +{ |
---|
| 1293 | + /* Lockless read. Prevent the compiler from reloading it below */ |
---|
| 1294 | + struct hrtimer_clock_base *base = READ_ONCE(timer->base); |
---|
| 1295 | + |
---|
| 1296 | + /* |
---|
| 1297 | + * Just relax if the timer expires in hard interrupt context or if |
---|
| 1298 | + * it is currently on the migration base. |
---|
| 1299 | + */ |
---|
| 1300 | + if (!timer->is_soft || is_migration_base(base)) { |
---|
| 1301 | + cpu_relax(); |
---|
| 1302 | + return; |
---|
| 1303 | + } |
---|
| 1304 | + |
---|
| 1305 | + /* |
---|
| 1306 | + * Mark the base as contended and grab the expiry lock, which is |
---|
| 1307 | + * held by the softirq across the timer callback. Drop the lock |
---|
| 1308 | + * immediately so the softirq can expire the next timer. In theory |
---|
| 1309 | + * the timer could already be running again, but that's more than |
---|
| 1310 | + * unlikely and just causes another wait loop. |
---|
| 1311 | + */ |
---|
| 1312 | + atomic_inc(&base->cpu_base->timer_waiters); |
---|
| 1313 | + spin_lock_bh(&base->cpu_base->softirq_expiry_lock); |
---|
| 1314 | + atomic_dec(&base->cpu_base->timer_waiters); |
---|
| 1315 | + spin_unlock_bh(&base->cpu_base->softirq_expiry_lock); |
---|
| 1316 | +} |
---|
| 1317 | +#else |
---|
| 1318 | +static inline void |
---|
| 1319 | +hrtimer_cpu_base_init_expiry_lock(struct hrtimer_cpu_base *base) { } |
---|
| 1320 | +static inline void |
---|
| 1321 | +hrtimer_cpu_base_lock_expiry(struct hrtimer_cpu_base *base) { } |
---|
| 1322 | +static inline void |
---|
| 1323 | +hrtimer_cpu_base_unlock_expiry(struct hrtimer_cpu_base *base) { } |
---|
| 1324 | +static inline void hrtimer_sync_wait_running(struct hrtimer_cpu_base *base, |
---|
| 1325 | + unsigned long flags) { } |
---|
| 1326 | +#endif |
---|
| 1327 | + |
---|
1248 | 1328 | /** |
---|
1249 | 1329 | * hrtimer_cancel - cancel a timer and wait for the handler to finish. |
---|
1250 | 1330 | * @timer: the timer to be cancelled |
---|
.. | .. |
---|
1255 | 1335 | */ |
---|
1256 | 1336 | int hrtimer_cancel(struct hrtimer *timer) |
---|
1257 | 1337 | { |
---|
1258 | | - for (;;) { |
---|
1259 | | - int ret = hrtimer_try_to_cancel(timer); |
---|
| 1338 | + int ret; |
---|
1260 | 1339 | |
---|
1261 | | - if (ret >= 0) |
---|
1262 | | - return ret; |
---|
1263 | | - hrtimer_grab_expiry_lock(timer); |
---|
1264 | | - } |
---|
| 1340 | + do { |
---|
| 1341 | + ret = hrtimer_try_to_cancel(timer); |
---|
| 1342 | + |
---|
| 1343 | + if (ret < 0) |
---|
| 1344 | + hrtimer_cancel_wait_running(timer); |
---|
| 1345 | + } while (ret < 0); |
---|
| 1346 | + return ret; |
---|
1265 | 1347 | } |
---|
1266 | 1348 | EXPORT_SYMBOL_GPL(hrtimer_cancel); |
---|
1267 | 1349 | |
---|
.. | .. |
---|
1357 | 1439 | static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
---|
1358 | 1440 | enum hrtimer_mode mode) |
---|
1359 | 1441 | { |
---|
1360 | | - bool softtimer; |
---|
1361 | | - int base; |
---|
| 1442 | + bool softtimer = !!(mode & HRTIMER_MODE_SOFT); |
---|
1362 | 1443 | struct hrtimer_cpu_base *cpu_base; |
---|
| 1444 | + int base; |
---|
1363 | 1445 | |
---|
1364 | | - softtimer = !!(mode & HRTIMER_MODE_SOFT); |
---|
1365 | | -#ifdef CONFIG_PREEMPT_RT_FULL |
---|
1366 | | - if (!softtimer && !(mode & HRTIMER_MODE_HARD)) |
---|
| 1446 | + /* |
---|
| 1447 | + * On PREEMPT_RT enabled kernels hrtimers which are not explicitely |
---|
| 1448 | + * marked for hard interrupt expiry mode are moved into soft |
---|
| 1449 | + * interrupt context for latency reasons and because the callbacks |
---|
| 1450 | + * can invoke functions which might sleep on RT, e.g. spin_lock(). |
---|
| 1451 | + */ |
---|
| 1452 | + if (IS_ENABLED(CONFIG_PREEMPT_RT) && !(mode & HRTIMER_MODE_HARD)) |
---|
1367 | 1453 | softtimer = true; |
---|
1368 | | -#endif |
---|
1369 | | - base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0; |
---|
1370 | 1454 | |
---|
1371 | 1455 | memset(timer, 0, sizeof(struct hrtimer)); |
---|
1372 | 1456 | |
---|
.. | .. |
---|
1380 | 1464 | if (clock_id == CLOCK_REALTIME && mode & HRTIMER_MODE_REL) |
---|
1381 | 1465 | clock_id = CLOCK_MONOTONIC; |
---|
1382 | 1466 | |
---|
| 1467 | + base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0; |
---|
1383 | 1468 | base += hrtimer_clockid_to_base(clock_id); |
---|
1384 | 1469 | timer->is_soft = softtimer; |
---|
| 1470 | + timer->is_hard = !!(mode & HRTIMER_MODE_HARD); |
---|
1385 | 1471 | timer->base = &cpu_base->clock_base[base]; |
---|
1386 | 1472 | timerqueue_init(&timer->node); |
---|
1387 | 1473 | } |
---|
.. | .. |
---|
1454 | 1540 | static void __run_hrtimer(struct hrtimer_cpu_base *cpu_base, |
---|
1455 | 1541 | struct hrtimer_clock_base *base, |
---|
1456 | 1542 | struct hrtimer *timer, ktime_t *now, |
---|
1457 | | - unsigned long flags) |
---|
| 1543 | + unsigned long flags) __must_hold(&cpu_base->lock) |
---|
1458 | 1544 | { |
---|
1459 | 1545 | enum hrtimer_restart (*fn)(struct hrtimer *); |
---|
| 1546 | + bool expires_in_hardirq; |
---|
1460 | 1547 | int restart; |
---|
1461 | 1548 | |
---|
1462 | 1549 | lockdep_assert_held(&cpu_base->lock); |
---|
.. | .. |
---|
1491 | 1578 | */ |
---|
1492 | 1579 | raw_spin_unlock_irqrestore(&cpu_base->lock, flags); |
---|
1493 | 1580 | trace_hrtimer_expire_entry(timer, now); |
---|
| 1581 | + expires_in_hardirq = lockdep_hrtimer_enter(timer); |
---|
| 1582 | + |
---|
1494 | 1583 | restart = fn(timer); |
---|
| 1584 | + |
---|
| 1585 | + lockdep_hrtimer_exit(expires_in_hardirq); |
---|
1495 | 1586 | trace_hrtimer_expire_exit(timer); |
---|
1496 | 1587 | raw_spin_lock_irq(&cpu_base->lock); |
---|
1497 | 1588 | |
---|
.. | .. |
---|
1554 | 1645 | break; |
---|
1555 | 1646 | |
---|
1556 | 1647 | __run_hrtimer(cpu_base, base, timer, &basenow, flags); |
---|
| 1648 | + if (active_mask == HRTIMER_ACTIVE_SOFT) |
---|
| 1649 | + hrtimer_sync_wait_running(cpu_base, flags); |
---|
1557 | 1650 | } |
---|
1558 | 1651 | } |
---|
1559 | 1652 | } |
---|
.. | .. |
---|
1564 | 1657 | unsigned long flags; |
---|
1565 | 1658 | ktime_t now; |
---|
1566 | 1659 | |
---|
1567 | | - spin_lock(&cpu_base->softirq_expiry_lock); |
---|
| 1660 | + hrtimer_cpu_base_lock_expiry(cpu_base); |
---|
1568 | 1661 | raw_spin_lock_irqsave(&cpu_base->lock, flags); |
---|
1569 | 1662 | |
---|
1570 | 1663 | now = hrtimer_update_base(cpu_base); |
---|
.. | .. |
---|
1574 | 1667 | hrtimer_update_softirq_timer(cpu_base, true); |
---|
1575 | 1668 | |
---|
1576 | 1669 | raw_spin_unlock_irqrestore(&cpu_base->lock, flags); |
---|
1577 | | - spin_unlock(&cpu_base->softirq_expiry_lock); |
---|
| 1670 | + hrtimer_cpu_base_unlock_expiry(cpu_base); |
---|
1578 | 1671 | } |
---|
1579 | 1672 | |
---|
1580 | 1673 | #ifdef CONFIG_HIGH_RES_TIMERS |
---|
.. | .. |
---|
1746 | 1839 | return HRTIMER_NORESTART; |
---|
1747 | 1840 | } |
---|
1748 | 1841 | |
---|
1749 | | -static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl, |
---|
1750 | | - clockid_t clock_id, |
---|
1751 | | - enum hrtimer_mode mode, |
---|
1752 | | - struct task_struct *task) |
---|
| 1842 | +/** |
---|
| 1843 | + * hrtimer_sleeper_start_expires - Start a hrtimer sleeper timer |
---|
| 1844 | + * @sl: sleeper to be started |
---|
| 1845 | + * @mode: timer mode abs/rel |
---|
| 1846 | + * |
---|
| 1847 | + * Wrapper around hrtimer_start_expires() for hrtimer_sleeper based timers |
---|
| 1848 | + * to allow PREEMPT_RT to tweak the delivery mode (soft/hardirq context) |
---|
| 1849 | + */ |
---|
| 1850 | +void hrtimer_sleeper_start_expires(struct hrtimer_sleeper *sl, |
---|
| 1851 | + enum hrtimer_mode mode) |
---|
1753 | 1852 | { |
---|
1754 | | -#ifdef CONFIG_PREEMPT_RT_FULL |
---|
1755 | | - if (!(mode & (HRTIMER_MODE_SOFT | HRTIMER_MODE_HARD))) { |
---|
1756 | | - if (task_is_realtime(current) || system_state != SYSTEM_RUNNING) |
---|
| 1853 | + /* |
---|
| 1854 | + * Make the enqueue delivery mode check work on RT. If the sleeper |
---|
| 1855 | + * was initialized for hard interrupt delivery, force the mode bit. |
---|
| 1856 | + * This is a special case for hrtimer_sleepers because |
---|
| 1857 | + * hrtimer_init_sleeper() determines the delivery mode on RT so the |
---|
| 1858 | + * fiddling with this decision is avoided at the call sites. |
---|
| 1859 | + */ |
---|
| 1860 | + if (IS_ENABLED(CONFIG_PREEMPT_RT) && sl->timer.is_hard) |
---|
| 1861 | + mode |= HRTIMER_MODE_HARD; |
---|
| 1862 | + |
---|
| 1863 | + hrtimer_start_expires(&sl->timer, mode); |
---|
| 1864 | +} |
---|
| 1865 | +EXPORT_SYMBOL_GPL(hrtimer_sleeper_start_expires); |
---|
| 1866 | + |
---|
| 1867 | +static void __hrtimer_init_sleeper(struct hrtimer_sleeper *sl, |
---|
| 1868 | + clockid_t clock_id, enum hrtimer_mode mode) |
---|
| 1869 | +{ |
---|
| 1870 | + /* |
---|
| 1871 | + * On PREEMPT_RT enabled kernels hrtimers which are not explicitely |
---|
| 1872 | + * marked for hard interrupt expiry mode are moved into soft |
---|
| 1873 | + * interrupt context either for latency reasons or because the |
---|
| 1874 | + * hrtimer callback takes regular spinlocks or invokes other |
---|
| 1875 | + * functions which are not suitable for hard interrupt context on |
---|
| 1876 | + * PREEMPT_RT. |
---|
| 1877 | + * |
---|
| 1878 | + * The hrtimer_sleeper callback is RT compatible in hard interrupt |
---|
| 1879 | + * context, but there is a latency concern: Untrusted userspace can |
---|
| 1880 | + * spawn many threads which arm timers for the same expiry time on |
---|
| 1881 | + * the same CPU. That causes a latency spike due to the wakeup of |
---|
| 1882 | + * a gazillion threads. |
---|
| 1883 | + * |
---|
| 1884 | + * OTOH, priviledged real-time user space applications rely on the |
---|
| 1885 | + * low latency of hard interrupt wakeups. If the current task is in |
---|
| 1886 | + * a real-time scheduling class, mark the mode for hard interrupt |
---|
| 1887 | + * expiry. |
---|
| 1888 | + */ |
---|
| 1889 | + if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
---|
| 1890 | + if (task_is_realtime(current) && !(mode & HRTIMER_MODE_SOFT)) |
---|
1757 | 1891 | mode |= HRTIMER_MODE_HARD; |
---|
1758 | | - else |
---|
1759 | | - mode |= HRTIMER_MODE_SOFT; |
---|
1760 | 1892 | } |
---|
1761 | | -#endif |
---|
| 1893 | + |
---|
1762 | 1894 | __hrtimer_init(&sl->timer, clock_id, mode); |
---|
1763 | 1895 | sl->timer.function = hrtimer_wakeup; |
---|
1764 | | - sl->task = task; |
---|
| 1896 | + sl->task = current; |
---|
1765 | 1897 | } |
---|
1766 | 1898 | |
---|
1767 | 1899 | /** |
---|
.. | .. |
---|
1769 | 1901 | * @sl: sleeper to be initialized |
---|
1770 | 1902 | * @clock_id: the clock to be used |
---|
1771 | 1903 | * @mode: timer mode abs/rel |
---|
1772 | | - * @task: the task to wake up |
---|
1773 | 1904 | */ |
---|
1774 | 1905 | void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id, |
---|
1775 | | - enum hrtimer_mode mode, struct task_struct *task) |
---|
| 1906 | + enum hrtimer_mode mode) |
---|
1776 | 1907 | { |
---|
1777 | 1908 | debug_init(&sl->timer, clock_id, mode); |
---|
1778 | | - __hrtimer_init_sleeper(sl, clock_id, mode, task); |
---|
| 1909 | + __hrtimer_init_sleeper(sl, clock_id, mode); |
---|
1779 | 1910 | |
---|
1780 | 1911 | } |
---|
1781 | 1912 | EXPORT_SYMBOL_GPL(hrtimer_init_sleeper); |
---|
1782 | | - |
---|
1783 | | -#ifdef CONFIG_DEBUG_OBJECTS_TIMERS |
---|
1784 | | -void hrtimer_init_sleeper_on_stack(struct hrtimer_sleeper *sl, |
---|
1785 | | - clockid_t clock_id, |
---|
1786 | | - enum hrtimer_mode mode, |
---|
1787 | | - struct task_struct *task) |
---|
1788 | | -{ |
---|
1789 | | - debug_object_init_on_stack(&sl->timer, &hrtimer_debug_descr); |
---|
1790 | | - __hrtimer_init_sleeper(sl, clock_id, mode, task); |
---|
1791 | | -} |
---|
1792 | | -EXPORT_SYMBOL_GPL(hrtimer_init_sleeper_on_stack); |
---|
1793 | | -#endif |
---|
1794 | 1913 | |
---|
1795 | 1914 | int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts) |
---|
1796 | 1915 | { |
---|
1797 | 1916 | switch(restart->nanosleep.type) { |
---|
1798 | 1917 | #ifdef CONFIG_COMPAT_32BIT_TIME |
---|
1799 | 1918 | case TT_COMPAT: |
---|
1800 | | - if (compat_put_timespec64(ts, restart->nanosleep.compat_rmtp)) |
---|
| 1919 | + if (put_old_timespec32(ts, restart->nanosleep.compat_rmtp)) |
---|
1801 | 1920 | return -EFAULT; |
---|
1802 | 1921 | break; |
---|
1803 | 1922 | #endif |
---|
.. | .. |
---|
1817 | 1936 | |
---|
1818 | 1937 | do { |
---|
1819 | 1938 | set_current_state(TASK_INTERRUPTIBLE); |
---|
1820 | | - hrtimer_start_expires(&t->timer, mode); |
---|
| 1939 | + hrtimer_sleeper_start_expires(t, mode); |
---|
1821 | 1940 | |
---|
1822 | 1941 | if (likely(t->task)) |
---|
1823 | 1942 | freezable_schedule(); |
---|
1824 | 1943 | |
---|
1825 | | - __set_current_state(TASK_RUNNING); |
---|
1826 | 1944 | hrtimer_cancel(&t->timer); |
---|
1827 | 1945 | mode = HRTIMER_MODE_ABS; |
---|
1828 | 1946 | |
---|
1829 | 1947 | } while (t->task && !signal_pending(current)); |
---|
1830 | 1948 | |
---|
| 1949 | + __set_current_state(TASK_RUNNING); |
---|
1831 | 1950 | |
---|
1832 | 1951 | if (!t->task) |
---|
1833 | 1952 | return 0; |
---|
.. | .. |
---|
1852 | 1971 | int ret; |
---|
1853 | 1972 | |
---|
1854 | 1973 | hrtimer_init_sleeper_on_stack(&t, restart->nanosleep.clockid, |
---|
1855 | | - HRTIMER_MODE_ABS, current); |
---|
| 1974 | + HRTIMER_MODE_ABS); |
---|
1856 | 1975 | hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); |
---|
1857 | 1976 | ret = do_nanosleep(&t, HRTIMER_MODE_ABS); |
---|
1858 | 1977 | destroy_hrtimer_on_stack(&t.timer); |
---|
1859 | 1978 | return ret; |
---|
1860 | 1979 | } |
---|
1861 | 1980 | |
---|
1862 | | -long hrtimer_nanosleep(const struct timespec64 *rqtp, |
---|
1863 | | - const enum hrtimer_mode mode, const clockid_t clockid) |
---|
| 1981 | +long hrtimer_nanosleep(ktime_t rqtp, const enum hrtimer_mode mode, |
---|
| 1982 | + const clockid_t clockid) |
---|
1864 | 1983 | { |
---|
1865 | 1984 | struct restart_block *restart; |
---|
1866 | 1985 | struct hrtimer_sleeper t; |
---|
.. | .. |
---|
1871 | 1990 | if (dl_task(current) || rt_task(current)) |
---|
1872 | 1991 | slack = 0; |
---|
1873 | 1992 | |
---|
1874 | | - hrtimer_init_sleeper_on_stack(&t, clockid, mode, current); |
---|
1875 | | - hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack); |
---|
| 1993 | + hrtimer_init_sleeper_on_stack(&t, clockid, mode); |
---|
| 1994 | + hrtimer_set_expires_range_ns(&t.timer, rqtp, slack); |
---|
1876 | 1995 | ret = do_nanosleep(&t, mode); |
---|
1877 | 1996 | if (ret != -ERESTART_RESTARTBLOCK) |
---|
1878 | 1997 | goto out; |
---|
.. | .. |
---|
1892 | 2011 | return ret; |
---|
1893 | 2012 | } |
---|
1894 | 2013 | |
---|
1895 | | -#if !defined(CONFIG_64BIT_TIME) || defined(CONFIG_64BIT) |
---|
| 2014 | +#ifdef CONFIG_64BIT |
---|
1896 | 2015 | |
---|
1897 | 2016 | SYSCALL_DEFINE2(nanosleep, struct __kernel_timespec __user *, rqtp, |
---|
1898 | 2017 | struct __kernel_timespec __user *, rmtp) |
---|
.. | .. |
---|
1905 | 2024 | if (!timespec64_valid(&tu)) |
---|
1906 | 2025 | return -EINVAL; |
---|
1907 | 2026 | |
---|
| 2027 | + current->restart_block.fn = do_no_restart_syscall; |
---|
1908 | 2028 | current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE; |
---|
1909 | 2029 | current->restart_block.nanosleep.rmtp = rmtp; |
---|
1910 | | - return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
---|
| 2030 | + return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL, |
---|
| 2031 | + CLOCK_MONOTONIC); |
---|
1911 | 2032 | } |
---|
1912 | 2033 | |
---|
1913 | 2034 | #endif |
---|
1914 | 2035 | |
---|
1915 | 2036 | #ifdef CONFIG_COMPAT_32BIT_TIME |
---|
1916 | 2037 | |
---|
1917 | | -COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp, |
---|
1918 | | - struct compat_timespec __user *, rmtp) |
---|
| 2038 | +SYSCALL_DEFINE2(nanosleep_time32, struct old_timespec32 __user *, rqtp, |
---|
| 2039 | + struct old_timespec32 __user *, rmtp) |
---|
1919 | 2040 | { |
---|
1920 | 2041 | struct timespec64 tu; |
---|
1921 | 2042 | |
---|
1922 | | - if (compat_get_timespec64(&tu, rqtp)) |
---|
| 2043 | + if (get_old_timespec32(&tu, rqtp)) |
---|
1923 | 2044 | return -EFAULT; |
---|
1924 | 2045 | |
---|
1925 | 2046 | if (!timespec64_valid(&tu)) |
---|
1926 | 2047 | return -EINVAL; |
---|
1927 | 2048 | |
---|
| 2049 | + current->restart_block.fn = do_no_restart_syscall; |
---|
1928 | 2050 | current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE; |
---|
1929 | 2051 | current->restart_block.nanosleep.compat_rmtp = rmtp; |
---|
1930 | | - return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC); |
---|
| 2052 | + return hrtimer_nanosleep(timespec64_to_ktime(tu), HRTIMER_MODE_REL, |
---|
| 2053 | + CLOCK_MONOTONIC); |
---|
1931 | 2054 | } |
---|
1932 | | -#endif |
---|
1933 | | - |
---|
1934 | | -#ifdef CONFIG_PREEMPT_RT_FULL |
---|
1935 | | -/* |
---|
1936 | | - * Sleep for 1 ms in hope whoever holds what we want will let it go. |
---|
1937 | | - */ |
---|
1938 | | -void cpu_chill(void) |
---|
1939 | | -{ |
---|
1940 | | - unsigned int freeze_flag = current->flags & PF_NOFREEZE; |
---|
1941 | | - struct task_struct *self = current; |
---|
1942 | | - ktime_t chill_time; |
---|
1943 | | - |
---|
1944 | | - raw_spin_lock_irq(&self->pi_lock); |
---|
1945 | | - self->saved_state = self->state; |
---|
1946 | | - __set_current_state_no_track(TASK_UNINTERRUPTIBLE); |
---|
1947 | | - raw_spin_unlock_irq(&self->pi_lock); |
---|
1948 | | - |
---|
1949 | | - chill_time = ktime_set(0, NSEC_PER_MSEC); |
---|
1950 | | - |
---|
1951 | | - current->flags |= PF_NOFREEZE; |
---|
1952 | | - sleeping_lock_inc(); |
---|
1953 | | - schedule_hrtimeout(&chill_time, HRTIMER_MODE_REL_HARD); |
---|
1954 | | - sleeping_lock_dec(); |
---|
1955 | | - if (!freeze_flag) |
---|
1956 | | - current->flags &= ~PF_NOFREEZE; |
---|
1957 | | - |
---|
1958 | | - raw_spin_lock_irq(&self->pi_lock); |
---|
1959 | | - __set_current_state_no_track(self->saved_state); |
---|
1960 | | - self->saved_state = TASK_RUNNING; |
---|
1961 | | - raw_spin_unlock_irq(&self->pi_lock); |
---|
1962 | | -} |
---|
1963 | | -EXPORT_SYMBOL(cpu_chill); |
---|
1964 | 2055 | #endif |
---|
1965 | 2056 | |
---|
1966 | 2057 | /* |
---|
.. | .. |
---|
1972 | 2063 | int i; |
---|
1973 | 2064 | |
---|
1974 | 2065 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { |
---|
1975 | | - cpu_base->clock_base[i].cpu_base = cpu_base; |
---|
1976 | | - timerqueue_init_head(&cpu_base->clock_base[i].active); |
---|
| 2066 | + struct hrtimer_clock_base *clock_b = &cpu_base->clock_base[i]; |
---|
| 2067 | + |
---|
| 2068 | + clock_b->cpu_base = cpu_base; |
---|
| 2069 | + seqcount_raw_spinlock_init(&clock_b->seq, &cpu_base->lock); |
---|
| 2070 | + timerqueue_init_head(&clock_b->active); |
---|
1977 | 2071 | } |
---|
1978 | 2072 | |
---|
1979 | 2073 | cpu_base->cpu = cpu; |
---|
.. | .. |
---|
1984 | 2078 | cpu_base->softirq_next_timer = NULL; |
---|
1985 | 2079 | cpu_base->expires_next = KTIME_MAX; |
---|
1986 | 2080 | cpu_base->softirq_expires_next = KTIME_MAX; |
---|
1987 | | - spin_lock_init(&cpu_base->softirq_expiry_lock); |
---|
| 2081 | + hrtimer_cpu_base_init_expiry_lock(cpu_base); |
---|
1988 | 2082 | return 0; |
---|
1989 | 2083 | } |
---|
1990 | 2084 | |
---|
.. | .. |
---|
2103 | 2197 | return -EINTR; |
---|
2104 | 2198 | } |
---|
2105 | 2199 | |
---|
2106 | | - hrtimer_init_sleeper_on_stack(&t, clock_id, mode, current); |
---|
| 2200 | + hrtimer_init_sleeper_on_stack(&t, clock_id, mode); |
---|
2107 | 2201 | hrtimer_set_expires_range_ns(&t.timer, *expires, delta); |
---|
2108 | | - |
---|
2109 | | - hrtimer_start_expires(&t.timer, mode); |
---|
| 2202 | + hrtimer_sleeper_start_expires(&t, mode); |
---|
2110 | 2203 | |
---|
2111 | 2204 | if (likely(t.task)) |
---|
2112 | 2205 | schedule(); |
---|
.. | .. |
---|
2118 | 2211 | |
---|
2119 | 2212 | return !t.task ? 0 : -EINTR; |
---|
2120 | 2213 | } |
---|
| 2214 | +EXPORT_SYMBOL_GPL(schedule_hrtimeout_range_clock); |
---|
2121 | 2215 | |
---|
2122 | 2216 | /** |
---|
2123 | 2217 | * schedule_hrtimeout_range - sleep until timeout |
---|