diff options
Diffstat (limited to 'kernel/hrtimer.c')
-rw-r--r-- | kernel/hrtimer.c | 342 |
1 files changed, 293 insertions, 49 deletions
diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c index b8e4dce80a74..47e63349d1b2 100644 --- a/kernel/hrtimer.c +++ b/kernel/hrtimer.c | |||
@@ -517,7 +517,7 @@ static void hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base) | |||
517 | if (!base->first) | 517 | if (!base->first) |
518 | continue; | 518 | continue; |
519 | timer = rb_entry(base->first, struct hrtimer, node); | 519 | timer = rb_entry(base->first, struct hrtimer, node); |
520 | expires = ktime_sub(timer->expires, base->offset); | 520 | expires = ktime_sub(hrtimer_get_expires(timer), base->offset); |
521 | if (expires.tv64 < cpu_base->expires_next.tv64) | 521 | if (expires.tv64 < cpu_base->expires_next.tv64) |
522 | cpu_base->expires_next = expires; | 522 | cpu_base->expires_next = expires; |
523 | } | 523 | } |
@@ -539,10 +539,10 @@ static int hrtimer_reprogram(struct hrtimer *timer, | |||
539 | struct hrtimer_clock_base *base) | 539 | struct hrtimer_clock_base *base) |
540 | { | 540 | { |
541 | ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next; | 541 | ktime_t *expires_next = &__get_cpu_var(hrtimer_bases).expires_next; |
542 | ktime_t expires = ktime_sub(timer->expires, base->offset); | 542 | ktime_t expires = ktime_sub(hrtimer_get_expires(timer), base->offset); |
543 | int res; | 543 | int res; |
544 | 544 | ||
545 | WARN_ON_ONCE(timer->expires.tv64 < 0); | 545 | WARN_ON_ONCE(hrtimer_get_expires_tv64(timer) < 0); |
546 | 546 | ||
547 | /* | 547 | /* |
548 | * When the callback is running, we do not reprogram the clock event | 548 | * When the callback is running, we do not reprogram the clock event |
@@ -664,25 +664,17 @@ static inline int hrtimer_enqueue_reprogram(struct hrtimer *timer, | |||
664 | 664 | ||
665 | /* Timer is expired, act upon the callback mode */ | 665 | /* Timer is expired, act upon the callback mode */ |
666 | switch(timer->cb_mode) { | 666 | switch(timer->cb_mode) { |
667 | case HRTIMER_CB_IRQSAFE_NO_RESTART: | 667 | case HRTIMER_CB_IRQSAFE_PERCPU: |
668 | debug_hrtimer_deactivate(timer); | 668 | case HRTIMER_CB_IRQSAFE_UNLOCKED: |
669 | /* | ||
670 | * We can call the callback from here. No restart | ||
671 | * happens, so no danger of recursion | ||
672 | */ | ||
673 | BUG_ON(timer->function(timer) != HRTIMER_NORESTART); | ||
674 | return 1; | ||
675 | case HRTIMER_CB_IRQSAFE_NO_SOFTIRQ: | ||
676 | /* | 669 | /* |
677 | * This is solely for the sched tick emulation with | 670 | * This is solely for the sched tick emulation with |
678 | * dynamic tick support to ensure that we do not | 671 | * dynamic tick support to ensure that we do not |
679 | * restart the tick right on the edge and end up with | 672 | * restart the tick right on the edge and end up with |
680 | * the tick timer in the softirq ! The calling site | 673 | * the tick timer in the softirq ! The calling site |
681 | * takes care of this. | 674 | * takes care of this. Also used for hrtimer sleeper ! |
682 | */ | 675 | */ |
683 | debug_hrtimer_deactivate(timer); | 676 | debug_hrtimer_deactivate(timer); |
684 | return 1; | 677 | return 1; |
685 | case HRTIMER_CB_IRQSAFE: | ||
686 | case HRTIMER_CB_SOFTIRQ: | 678 | case HRTIMER_CB_SOFTIRQ: |
687 | /* | 679 | /* |
688 | * Move everything else into the softirq pending list ! | 680 | * Move everything else into the softirq pending list ! |
@@ -794,7 +786,7 @@ u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) | |||
794 | u64 orun = 1; | 786 | u64 orun = 1; |
795 | ktime_t delta; | 787 | ktime_t delta; |
796 | 788 | ||
797 | delta = ktime_sub(now, timer->expires); | 789 | delta = ktime_sub(now, hrtimer_get_expires(timer)); |
798 | 790 | ||
799 | if (delta.tv64 < 0) | 791 | if (delta.tv64 < 0) |
800 | return 0; | 792 | return 0; |
@@ -806,8 +798,8 @@ u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) | |||
806 | s64 incr = ktime_to_ns(interval); | 798 | s64 incr = ktime_to_ns(interval); |
807 | 799 | ||
808 | orun = ktime_divns(delta, incr); | 800 | orun = ktime_divns(delta, incr); |
809 | timer->expires = ktime_add_ns(timer->expires, incr * orun); | 801 | hrtimer_add_expires_ns(timer, incr * orun); |
810 | if (timer->expires.tv64 > now.tv64) | 802 | if (hrtimer_get_expires_tv64(timer) > now.tv64) |
811 | return orun; | 803 | return orun; |
812 | /* | 804 | /* |
813 | * This (and the ktime_add() below) is the | 805 | * This (and the ktime_add() below) is the |
@@ -815,7 +807,7 @@ u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) | |||
815 | */ | 807 | */ |
816 | orun++; | 808 | orun++; |
817 | } | 809 | } |
818 | timer->expires = ktime_add_safe(timer->expires, interval); | 810 | hrtimer_add_expires(timer, interval); |
819 | 811 | ||
820 | return orun; | 812 | return orun; |
821 | } | 813 | } |
@@ -847,7 +839,8 @@ static void enqueue_hrtimer(struct hrtimer *timer, | |||
847 | * We dont care about collisions. Nodes with | 839 | * We dont care about collisions. Nodes with |
848 | * the same expiry time stay together. | 840 | * the same expiry time stay together. |
849 | */ | 841 | */ |
850 | if (timer->expires.tv64 < entry->expires.tv64) { | 842 | if (hrtimer_get_expires_tv64(timer) < |
843 | hrtimer_get_expires_tv64(entry)) { | ||
851 | link = &(*link)->rb_left; | 844 | link = &(*link)->rb_left; |
852 | } else { | 845 | } else { |
853 | link = &(*link)->rb_right; | 846 | link = &(*link)->rb_right; |
@@ -944,9 +937,10 @@ remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base) | |||
944 | } | 937 | } |
945 | 938 | ||
946 | /** | 939 | /** |
947 | * hrtimer_start - (re)start an relative timer on the current CPU | 940 | * hrtimer_start_range_ns - (re)start an hrtimer on the current CPU |
948 | * @timer: the timer to be added | 941 | * @timer: the timer to be added |
949 | * @tim: expiry time | 942 | * @tim: expiry time |
943 | * @delta_ns: "slack" range for the timer | ||
950 | * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL) | 944 | * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL) |
951 | * | 945 | * |
952 | * Returns: | 946 | * Returns: |
@@ -954,7 +948,8 @@ remove_hrtimer(struct hrtimer *timer, struct hrtimer_clock_base *base) | |||
954 | * 1 when the timer was active | 948 | * 1 when the timer was active |
955 | */ | 949 | */ |
956 | int | 950 | int |
957 | hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode) | 951 | hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim, unsigned long delta_ns, |
952 | const enum hrtimer_mode mode) | ||
958 | { | 953 | { |
959 | struct hrtimer_clock_base *base, *new_base; | 954 | struct hrtimer_clock_base *base, *new_base; |
960 | unsigned long flags; | 955 | unsigned long flags; |
@@ -982,7 +977,7 @@ hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode) | |||
982 | #endif | 977 | #endif |
983 | } | 978 | } |
984 | 979 | ||
985 | timer->expires = tim; | 980 | hrtimer_set_expires_range_ns(timer, tim, delta_ns); |
986 | 981 | ||
987 | timer_stats_hrtimer_set_start_info(timer); | 982 | timer_stats_hrtimer_set_start_info(timer); |
988 | 983 | ||
@@ -1015,8 +1010,26 @@ hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode) | |||
1015 | 1010 | ||
1016 | return ret; | 1011 | return ret; |
1017 | } | 1012 | } |
1013 | EXPORT_SYMBOL_GPL(hrtimer_start_range_ns); | ||
1014 | |||
1015 | /** | ||
1016 | * hrtimer_start - (re)start an hrtimer on the current CPU | ||
1017 | * @timer: the timer to be added | ||
1018 | * @tim: expiry time | ||
1019 | * @mode: expiry mode: absolute (HRTIMER_ABS) or relative (HRTIMER_REL) | ||
1020 | * | ||
1021 | * Returns: | ||
1022 | * 0 on success | ||
1023 | * 1 when the timer was active | ||
1024 | */ | ||
1025 | int | ||
1026 | hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode) | ||
1027 | { | ||
1028 | return hrtimer_start_range_ns(timer, tim, 0, mode); | ||
1029 | } | ||
1018 | EXPORT_SYMBOL_GPL(hrtimer_start); | 1030 | EXPORT_SYMBOL_GPL(hrtimer_start); |
1019 | 1031 | ||
1032 | |||
1020 | /** | 1033 | /** |
1021 | * hrtimer_try_to_cancel - try to deactivate a timer | 1034 | * hrtimer_try_to_cancel - try to deactivate a timer |
1022 | * @timer: hrtimer to stop | 1035 | * @timer: hrtimer to stop |
@@ -1076,7 +1089,7 @@ ktime_t hrtimer_get_remaining(const struct hrtimer *timer) | |||
1076 | ktime_t rem; | 1089 | ktime_t rem; |
1077 | 1090 | ||
1078 | base = lock_hrtimer_base(timer, &flags); | 1091 | base = lock_hrtimer_base(timer, &flags); |
1079 | rem = ktime_sub(timer->expires, base->get_time()); | 1092 | rem = hrtimer_expires_remaining(timer); |
1080 | unlock_hrtimer_base(timer, &flags); | 1093 | unlock_hrtimer_base(timer, &flags); |
1081 | 1094 | ||
1082 | return rem; | 1095 | return rem; |
@@ -1108,7 +1121,7 @@ ktime_t hrtimer_get_next_event(void) | |||
1108 | continue; | 1121 | continue; |
1109 | 1122 | ||
1110 | timer = rb_entry(base->first, struct hrtimer, node); | 1123 | timer = rb_entry(base->first, struct hrtimer, node); |
1111 | delta.tv64 = timer->expires.tv64; | 1124 | delta.tv64 = hrtimer_get_expires_tv64(timer); |
1112 | delta = ktime_sub(delta, base->get_time()); | 1125 | delta = ktime_sub(delta, base->get_time()); |
1113 | if (delta.tv64 < mindelta.tv64) | 1126 | if (delta.tv64 < mindelta.tv64) |
1114 | mindelta.tv64 = delta.tv64; | 1127 | mindelta.tv64 = delta.tv64; |
@@ -1187,6 +1200,7 @@ static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base) | |||
1187 | enum hrtimer_restart (*fn)(struct hrtimer *); | 1200 | enum hrtimer_restart (*fn)(struct hrtimer *); |
1188 | struct hrtimer *timer; | 1201 | struct hrtimer *timer; |
1189 | int restart; | 1202 | int restart; |
1203 | int emulate_hardirq_ctx = 0; | ||
1190 | 1204 | ||
1191 | timer = list_entry(cpu_base->cb_pending.next, | 1205 | timer = list_entry(cpu_base->cb_pending.next, |
1192 | struct hrtimer, cb_entry); | 1206 | struct hrtimer, cb_entry); |
@@ -1195,10 +1209,24 @@ static void run_hrtimer_pending(struct hrtimer_cpu_base *cpu_base) | |||
1195 | timer_stats_account_hrtimer(timer); | 1209 | timer_stats_account_hrtimer(timer); |
1196 | 1210 | ||
1197 | fn = timer->function; | 1211 | fn = timer->function; |
1212 | /* | ||
1213 | * A timer might have been added to the cb_pending list | ||
1214 | * when it was migrated during a cpu-offline operation. | ||
1215 | * Emulate hardirq context for such timers. | ||
1216 | */ | ||
1217 | if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU || | ||
1218 | timer->cb_mode == HRTIMER_CB_IRQSAFE_UNLOCKED) | ||
1219 | emulate_hardirq_ctx = 1; | ||
1220 | |||
1198 | __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0); | 1221 | __remove_hrtimer(timer, timer->base, HRTIMER_STATE_CALLBACK, 0); |
1199 | spin_unlock_irq(&cpu_base->lock); | 1222 | spin_unlock_irq(&cpu_base->lock); |
1200 | 1223 | ||
1201 | restart = fn(timer); | 1224 | if (unlikely(emulate_hardirq_ctx)) { |
1225 | local_irq_disable(); | ||
1226 | restart = fn(timer); | ||
1227 | local_irq_enable(); | ||
1228 | } else | ||
1229 | restart = fn(timer); | ||
1202 | 1230 | ||
1203 | spin_lock_irq(&cpu_base->lock); | 1231 | spin_lock_irq(&cpu_base->lock); |
1204 | 1232 | ||
@@ -1245,7 +1273,8 @@ static void __run_hrtimer(struct hrtimer *timer) | |||
1245 | timer_stats_account_hrtimer(timer); | 1273 | timer_stats_account_hrtimer(timer); |
1246 | 1274 | ||
1247 | fn = timer->function; | 1275 | fn = timer->function; |
1248 | if (timer->cb_mode == HRTIMER_CB_IRQSAFE_NO_SOFTIRQ) { | 1276 | if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU || |
1277 | timer->cb_mode == HRTIMER_CB_IRQSAFE_UNLOCKED) { | ||
1249 | /* | 1278 | /* |
1250 | * Used for scheduler timers, avoid lock inversion with | 1279 | * Used for scheduler timers, avoid lock inversion with |
1251 | * rq->lock and tasklist_lock. | 1280 | * rq->lock and tasklist_lock. |
@@ -1308,10 +1337,23 @@ void hrtimer_interrupt(struct clock_event_device *dev) | |||
1308 | 1337 | ||
1309 | timer = rb_entry(node, struct hrtimer, node); | 1338 | timer = rb_entry(node, struct hrtimer, node); |
1310 | 1339 | ||
1311 | if (basenow.tv64 < timer->expires.tv64) { | 1340 | /* |
1341 | * The immediate goal for using the softexpires is | ||
1342 | * minimizing wakeups, not running timers at the | ||
1343 | * earliest interrupt after their soft expiration. | ||
1344 | * This allows us to avoid using a Priority Search | ||
1345 | * Tree, which can answer a stabbing querry for | ||
1346 | * overlapping intervals and instead use the simple | ||
1347 | * BST we already have. | ||
1348 | * We don't add extra wakeups by delaying timers that | ||
1349 | * are right-of a not yet expired timer, because that | ||
1350 | * timer will have to trigger a wakeup anyway. | ||
1351 | */ | ||
1352 | |||
1353 | if (basenow.tv64 < hrtimer_get_softexpires_tv64(timer)) { | ||
1312 | ktime_t expires; | 1354 | ktime_t expires; |
1313 | 1355 | ||
1314 | expires = ktime_sub(timer->expires, | 1356 | expires = ktime_sub(hrtimer_get_expires(timer), |
1315 | base->offset); | 1357 | base->offset); |
1316 | if (expires.tv64 < expires_next.tv64) | 1358 | if (expires.tv64 < expires_next.tv64) |
1317 | expires_next = expires; | 1359 | expires_next = expires; |
@@ -1347,6 +1389,30 @@ void hrtimer_interrupt(struct clock_event_device *dev) | |||
1347 | raise_softirq(HRTIMER_SOFTIRQ); | 1389 | raise_softirq(HRTIMER_SOFTIRQ); |
1348 | } | 1390 | } |
1349 | 1391 | ||
1392 | /** | ||
1393 | * hrtimer_peek_ahead_timers -- run soft-expired timers now | ||
1394 | * | ||
1395 | * hrtimer_peek_ahead_timers will peek at the timer queue of | ||
1396 | * the current cpu and check if there are any timers for which | ||
1397 | * the soft expires time has passed. If any such timers exist, | ||
1398 | * they are run immediately and then removed from the timer queue. | ||
1399 | * | ||
1400 | */ | ||
1401 | void hrtimer_peek_ahead_timers(void) | ||
1402 | { | ||
1403 | struct tick_device *td; | ||
1404 | unsigned long flags; | ||
1405 | |||
1406 | if (!hrtimer_hres_active()) | ||
1407 | return; | ||
1408 | |||
1409 | local_irq_save(flags); | ||
1410 | td = &__get_cpu_var(tick_cpu_device); | ||
1411 | if (td && td->evtdev) | ||
1412 | hrtimer_interrupt(td->evtdev); | ||
1413 | local_irq_restore(flags); | ||
1414 | } | ||
1415 | |||
1350 | static void run_hrtimer_softirq(struct softirq_action *h) | 1416 | static void run_hrtimer_softirq(struct softirq_action *h) |
1351 | { | 1417 | { |
1352 | run_hrtimer_pending(&__get_cpu_var(hrtimer_bases)); | 1418 | run_hrtimer_pending(&__get_cpu_var(hrtimer_bases)); |
@@ -1401,9 +1467,7 @@ void hrtimer_run_queues(void) | |||
1401 | if (!base->first) | 1467 | if (!base->first) |
1402 | continue; | 1468 | continue; |
1403 | 1469 | ||
1404 | if (base->get_softirq_time) | 1470 | if (gettime) { |
1405 | base->softirq_time = base->get_softirq_time(); | ||
1406 | else if (gettime) { | ||
1407 | hrtimer_get_softirq_time(cpu_base); | 1471 | hrtimer_get_softirq_time(cpu_base); |
1408 | gettime = 0; | 1472 | gettime = 0; |
1409 | } | 1473 | } |
@@ -1414,7 +1478,8 @@ void hrtimer_run_queues(void) | |||
1414 | struct hrtimer *timer; | 1478 | struct hrtimer *timer; |
1415 | 1479 | ||
1416 | timer = rb_entry(node, struct hrtimer, node); | 1480 | timer = rb_entry(node, struct hrtimer, node); |
1417 | if (base->softirq_time.tv64 <= timer->expires.tv64) | 1481 | if (base->softirq_time.tv64 <= |
1482 | hrtimer_get_expires_tv64(timer)) | ||
1418 | break; | 1483 | break; |
1419 | 1484 | ||
1420 | if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) { | 1485 | if (timer->cb_mode == HRTIMER_CB_SOFTIRQ) { |
@@ -1452,7 +1517,7 @@ void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task) | |||
1452 | sl->timer.function = hrtimer_wakeup; | 1517 | sl->timer.function = hrtimer_wakeup; |
1453 | sl->task = task; | 1518 | sl->task = task; |
1454 | #ifdef CONFIG_HIGH_RES_TIMERS | 1519 | #ifdef CONFIG_HIGH_RES_TIMERS |
1455 | sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; | 1520 | sl->timer.cb_mode = HRTIMER_CB_IRQSAFE_UNLOCKED; |
1456 | #endif | 1521 | #endif |
1457 | } | 1522 | } |
1458 | 1523 | ||
@@ -1462,7 +1527,7 @@ static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mod | |||
1462 | 1527 | ||
1463 | do { | 1528 | do { |
1464 | set_current_state(TASK_INTERRUPTIBLE); | 1529 | set_current_state(TASK_INTERRUPTIBLE); |
1465 | hrtimer_start(&t->timer, t->timer.expires, mode); | 1530 | hrtimer_start_expires(&t->timer, mode); |
1466 | if (!hrtimer_active(&t->timer)) | 1531 | if (!hrtimer_active(&t->timer)) |
1467 | t->task = NULL; | 1532 | t->task = NULL; |
1468 | 1533 | ||
@@ -1484,7 +1549,7 @@ static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp) | |||
1484 | struct timespec rmt; | 1549 | struct timespec rmt; |
1485 | ktime_t rem; | 1550 | ktime_t rem; |
1486 | 1551 | ||
1487 | rem = ktime_sub(timer->expires, timer->base->get_time()); | 1552 | rem = hrtimer_expires_remaining(timer); |
1488 | if (rem.tv64 <= 0) | 1553 | if (rem.tv64 <= 0) |
1489 | return 0; | 1554 | return 0; |
1490 | rmt = ktime_to_timespec(rem); | 1555 | rmt = ktime_to_timespec(rem); |
@@ -1503,7 +1568,7 @@ long __sched hrtimer_nanosleep_restart(struct restart_block *restart) | |||
1503 | 1568 | ||
1504 | hrtimer_init_on_stack(&t.timer, restart->nanosleep.index, | 1569 | hrtimer_init_on_stack(&t.timer, restart->nanosleep.index, |
1505 | HRTIMER_MODE_ABS); | 1570 | HRTIMER_MODE_ABS); |
1506 | t.timer.expires.tv64 = restart->nanosleep.expires; | 1571 | hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); |
1507 | 1572 | ||
1508 | if (do_nanosleep(&t, HRTIMER_MODE_ABS)) | 1573 | if (do_nanosleep(&t, HRTIMER_MODE_ABS)) |
1509 | goto out; | 1574 | goto out; |
@@ -1528,9 +1593,14 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp, | |||
1528 | struct restart_block *restart; | 1593 | struct restart_block *restart; |
1529 | struct hrtimer_sleeper t; | 1594 | struct hrtimer_sleeper t; |
1530 | int ret = 0; | 1595 | int ret = 0; |
1596 | unsigned long slack; | ||
1597 | |||
1598 | slack = current->timer_slack_ns; | ||
1599 | if (rt_task(current)) | ||
1600 | slack = 0; | ||
1531 | 1601 | ||
1532 | hrtimer_init_on_stack(&t.timer, clockid, mode); | 1602 | hrtimer_init_on_stack(&t.timer, clockid, mode); |
1533 | t.timer.expires = timespec_to_ktime(*rqtp); | 1603 | hrtimer_set_expires_range_ns(&t.timer, timespec_to_ktime(*rqtp), slack); |
1534 | if (do_nanosleep(&t, mode)) | 1604 | if (do_nanosleep(&t, mode)) |
1535 | goto out; | 1605 | goto out; |
1536 | 1606 | ||
@@ -1550,7 +1620,7 @@ long hrtimer_nanosleep(struct timespec *rqtp, struct timespec __user *rmtp, | |||
1550 | restart->fn = hrtimer_nanosleep_restart; | 1620 | restart->fn = hrtimer_nanosleep_restart; |
1551 | restart->nanosleep.index = t.timer.base->index; | 1621 | restart->nanosleep.index = t.timer.base->index; |
1552 | restart->nanosleep.rmtp = rmtp; | 1622 | restart->nanosleep.rmtp = rmtp; |
1553 | restart->nanosleep.expires = t.timer.expires.tv64; | 1623 | restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer); |
1554 | 1624 | ||
1555 | ret = -ERESTART_RESTARTBLOCK; | 1625 | ret = -ERESTART_RESTARTBLOCK; |
1556 | out: | 1626 | out: |
@@ -1591,49 +1661,123 @@ static void __cpuinit init_hrtimers_cpu(int cpu) | |||
1591 | 1661 | ||
1592 | #ifdef CONFIG_HOTPLUG_CPU | 1662 | #ifdef CONFIG_HOTPLUG_CPU |
1593 | 1663 | ||
1594 | static void migrate_hrtimer_list(struct hrtimer_clock_base *old_base, | 1664 | static int migrate_hrtimer_list(struct hrtimer_clock_base *old_base, |
1595 | struct hrtimer_clock_base *new_base) | 1665 | struct hrtimer_clock_base *new_base, int dcpu) |
1596 | { | 1666 | { |
1597 | struct hrtimer *timer; | 1667 | struct hrtimer *timer; |
1598 | struct rb_node *node; | 1668 | struct rb_node *node; |
1669 | int raise = 0; | ||
1599 | 1670 | ||
1600 | while ((node = rb_first(&old_base->active))) { | 1671 | while ((node = rb_first(&old_base->active))) { |
1601 | timer = rb_entry(node, struct hrtimer, node); | 1672 | timer = rb_entry(node, struct hrtimer, node); |
1602 | BUG_ON(hrtimer_callback_running(timer)); | 1673 | BUG_ON(hrtimer_callback_running(timer)); |
1603 | debug_hrtimer_deactivate(timer); | 1674 | debug_hrtimer_deactivate(timer); |
1604 | __remove_hrtimer(timer, old_base, HRTIMER_STATE_INACTIVE, 0); | 1675 | |
1676 | /* | ||
1677 | * Should not happen. Per CPU timers should be | ||
1678 | * canceled _before_ the migration code is called | ||
1679 | */ | ||
1680 | if (timer->cb_mode == HRTIMER_CB_IRQSAFE_PERCPU) { | ||
1681 | __remove_hrtimer(timer, old_base, | ||
1682 | HRTIMER_STATE_INACTIVE, 0); | ||
1683 | WARN(1, "hrtimer (%p %p)active but cpu %d dead\n", | ||
1684 | timer, timer->function, dcpu); | ||
1685 | continue; | ||
1686 | } | ||
1687 | |||
1688 | /* | ||
1689 | * Mark it as STATE_MIGRATE not INACTIVE otherwise the | ||
1690 | * timer could be seen as !active and just vanish away | ||
1691 | * under us on another CPU | ||
1692 | */ | ||
1693 | __remove_hrtimer(timer, old_base, HRTIMER_STATE_MIGRATE, 0); | ||
1605 | timer->base = new_base; | 1694 | timer->base = new_base; |
1606 | /* | 1695 | /* |
1607 | * Enqueue the timer. Allow reprogramming of the event device | 1696 | * Enqueue the timer. Allow reprogramming of the event device |
1608 | */ | 1697 | */ |
1609 | enqueue_hrtimer(timer, new_base, 1); | 1698 | enqueue_hrtimer(timer, new_base, 1); |
1699 | |||
1700 | #ifdef CONFIG_HIGH_RES_TIMERS | ||
1701 | /* | ||
1702 | * Happens with high res enabled when the timer was | ||
1703 | * already expired and the callback mode is | ||
1704 | * HRTIMER_CB_IRQSAFE_UNLOCKED (hrtimer_sleeper). The | ||
1705 | * enqueue code does not move them to the soft irq | ||
1706 | * pending list for performance/latency reasons, but | ||
1707 | * in the migration state, we need to do that | ||
1708 | * otherwise we end up with a stale timer. | ||
1709 | */ | ||
1710 | if (timer->state == HRTIMER_STATE_MIGRATE) { | ||
1711 | timer->state = HRTIMER_STATE_PENDING; | ||
1712 | list_add_tail(&timer->cb_entry, | ||
1713 | &new_base->cpu_base->cb_pending); | ||
1714 | raise = 1; | ||
1715 | } | ||
1716 | #endif | ||
1717 | /* Clear the migration state bit */ | ||
1718 | timer->state &= ~HRTIMER_STATE_MIGRATE; | ||
1719 | } | ||
1720 | return raise; | ||
1721 | } | ||
1722 | |||
1723 | #ifdef CONFIG_HIGH_RES_TIMERS | ||
1724 | static int migrate_hrtimer_pending(struct hrtimer_cpu_base *old_base, | ||
1725 | struct hrtimer_cpu_base *new_base) | ||
1726 | { | ||
1727 | struct hrtimer *timer; | ||
1728 | int raise = 0; | ||
1729 | |||
1730 | while (!list_empty(&old_base->cb_pending)) { | ||
1731 | timer = list_entry(old_base->cb_pending.next, | ||
1732 | struct hrtimer, cb_entry); | ||
1733 | |||
1734 | __remove_hrtimer(timer, timer->base, HRTIMER_STATE_PENDING, 0); | ||
1735 | timer->base = &new_base->clock_base[timer->base->index]; | ||
1736 | list_add_tail(&timer->cb_entry, &new_base->cb_pending); | ||
1737 | raise = 1; | ||
1610 | } | 1738 | } |
1739 | return raise; | ||
1740 | } | ||
1741 | #else | ||
1742 | static int migrate_hrtimer_pending(struct hrtimer_cpu_base *old_base, | ||
1743 | struct hrtimer_cpu_base *new_base) | ||
1744 | { | ||
1745 | return 0; | ||
1611 | } | 1746 | } |
1747 | #endif | ||
1612 | 1748 | ||
1613 | static void migrate_hrtimers(int cpu) | 1749 | static void migrate_hrtimers(int cpu) |
1614 | { | 1750 | { |
1615 | struct hrtimer_cpu_base *old_base, *new_base; | 1751 | struct hrtimer_cpu_base *old_base, *new_base; |
1616 | int i; | 1752 | int i, raise = 0; |
1617 | 1753 | ||
1618 | BUG_ON(cpu_online(cpu)); | 1754 | BUG_ON(cpu_online(cpu)); |
1619 | old_base = &per_cpu(hrtimer_bases, cpu); | 1755 | old_base = &per_cpu(hrtimer_bases, cpu); |
1620 | new_base = &get_cpu_var(hrtimer_bases); | 1756 | new_base = &get_cpu_var(hrtimer_bases); |
1621 | 1757 | ||
1622 | tick_cancel_sched_timer(cpu); | 1758 | tick_cancel_sched_timer(cpu); |
1623 | 1759 | /* | |
1624 | local_irq_disable(); | 1760 | * The caller is globally serialized and nobody else |
1625 | spin_lock(&new_base->lock); | 1761 | * takes two locks at once, deadlock is not possible. |
1762 | */ | ||
1763 | spin_lock_irq(&new_base->lock); | ||
1626 | spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); | 1764 | spin_lock_nested(&old_base->lock, SINGLE_DEPTH_NESTING); |
1627 | 1765 | ||
1628 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { | 1766 | for (i = 0; i < HRTIMER_MAX_CLOCK_BASES; i++) { |
1629 | migrate_hrtimer_list(&old_base->clock_base[i], | 1767 | if (migrate_hrtimer_list(&old_base->clock_base[i], |
1630 | &new_base->clock_base[i]); | 1768 | &new_base->clock_base[i], cpu)) |
1769 | raise = 1; | ||
1631 | } | 1770 | } |
1632 | 1771 | ||
1772 | if (migrate_hrtimer_pending(old_base, new_base)) | ||
1773 | raise = 1; | ||
1774 | |||
1633 | spin_unlock(&old_base->lock); | 1775 | spin_unlock(&old_base->lock); |
1634 | spin_unlock(&new_base->lock); | 1776 | spin_unlock_irq(&new_base->lock); |
1635 | local_irq_enable(); | ||
1636 | put_cpu_var(hrtimer_bases); | 1777 | put_cpu_var(hrtimer_bases); |
1778 | |||
1779 | if (raise) | ||
1780 | hrtimer_raise_softirq(); | ||
1637 | } | 1781 | } |
1638 | #endif /* CONFIG_HOTPLUG_CPU */ | 1782 | #endif /* CONFIG_HOTPLUG_CPU */ |
1639 | 1783 | ||
@@ -1678,3 +1822,103 @@ void __init hrtimers_init(void) | |||
1678 | #endif | 1822 | #endif |
1679 | } | 1823 | } |
1680 | 1824 | ||
1825 | /** | ||
1826 | * schedule_hrtimeout_range - sleep until timeout | ||
1827 | * @expires: timeout value (ktime_t) | ||
1828 | * @delta: slack in expires timeout (ktime_t) | ||
1829 | * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL | ||
1830 | * | ||
1831 | * Make the current task sleep until the given expiry time has | ||
1832 | * elapsed. The routine will return immediately unless | ||
1833 | * the current task state has been set (see set_current_state()). | ||
1834 | * | ||
1835 | * The @delta argument gives the kernel the freedom to schedule the | ||
1836 | * actual wakeup to a time that is both power and performance friendly. | ||
1837 | * The kernel give the normal best effort behavior for "@expires+@delta", | ||
1838 | * but may decide to fire the timer earlier, but no earlier than @expires. | ||
1839 | * | ||
1840 | * You can set the task state as follows - | ||
1841 | * | ||
1842 | * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to | ||
1843 | * pass before the routine returns. | ||
1844 | * | ||
1845 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | ||
1846 | * delivered to the current task. | ||
1847 | * | ||
1848 | * The current task state is guaranteed to be TASK_RUNNING when this | ||
1849 | * routine returns. | ||
1850 | * | ||
1851 | * Returns 0 when the timer has expired otherwise -EINTR | ||
1852 | */ | ||
1853 | int __sched schedule_hrtimeout_range(ktime_t *expires, unsigned long delta, | ||
1854 | const enum hrtimer_mode mode) | ||
1855 | { | ||
1856 | struct hrtimer_sleeper t; | ||
1857 | |||
1858 | /* | ||
1859 | * Optimize when a zero timeout value is given. It does not | ||
1860 | * matter whether this is an absolute or a relative time. | ||
1861 | */ | ||
1862 | if (expires && !expires->tv64) { | ||
1863 | __set_current_state(TASK_RUNNING); | ||
1864 | return 0; | ||
1865 | } | ||
1866 | |||
1867 | /* | ||
1868 | * A NULL parameter means "inifinte" | ||
1869 | */ | ||
1870 | if (!expires) { | ||
1871 | schedule(); | ||
1872 | __set_current_state(TASK_RUNNING); | ||
1873 | return -EINTR; | ||
1874 | } | ||
1875 | |||
1876 | hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC, mode); | ||
1877 | hrtimer_set_expires_range_ns(&t.timer, *expires, delta); | ||
1878 | |||
1879 | hrtimer_init_sleeper(&t, current); | ||
1880 | |||
1881 | hrtimer_start_expires(&t.timer, mode); | ||
1882 | if (!hrtimer_active(&t.timer)) | ||
1883 | t.task = NULL; | ||
1884 | |||
1885 | if (likely(t.task)) | ||
1886 | schedule(); | ||
1887 | |||
1888 | hrtimer_cancel(&t.timer); | ||
1889 | destroy_hrtimer_on_stack(&t.timer); | ||
1890 | |||
1891 | __set_current_state(TASK_RUNNING); | ||
1892 | |||
1893 | return !t.task ? 0 : -EINTR; | ||
1894 | } | ||
1895 | EXPORT_SYMBOL_GPL(schedule_hrtimeout_range); | ||
1896 | |||
1897 | /** | ||
1898 | * schedule_hrtimeout - sleep until timeout | ||
1899 | * @expires: timeout value (ktime_t) | ||
1900 | * @mode: timer mode, HRTIMER_MODE_ABS or HRTIMER_MODE_REL | ||
1901 | * | ||
1902 | * Make the current task sleep until the given expiry time has | ||
1903 | * elapsed. The routine will return immediately unless | ||
1904 | * the current task state has been set (see set_current_state()). | ||
1905 | * | ||
1906 | * You can set the task state as follows - | ||
1907 | * | ||
1908 | * %TASK_UNINTERRUPTIBLE - at least @timeout time is guaranteed to | ||
1909 | * pass before the routine returns. | ||
1910 | * | ||
1911 | * %TASK_INTERRUPTIBLE - the routine may return early if a signal is | ||
1912 | * delivered to the current task. | ||
1913 | * | ||
1914 | * The current task state is guaranteed to be TASK_RUNNING when this | ||
1915 | * routine returns. | ||
1916 | * | ||
1917 | * Returns 0 when the timer has expired otherwise -EINTR | ||
1918 | */ | ||
1919 | int __sched schedule_hrtimeout(ktime_t *expires, | ||
1920 | const enum hrtimer_mode mode) | ||
1921 | { | ||
1922 | return schedule_hrtimeout_range(expires, 0, mode); | ||
1923 | } | ||
1924 | EXPORT_SYMBOL_GPL(schedule_hrtimeout); | ||