History

hrtimer: Update softirq_expires_next correctly after __hrtimer_get_next_event()

hrtimer_force_reprogram() and hrtimer_interrupt() invokes __hrtimer_get_next_event() to find the earliest expiry time of hrtimer bases. __hrtimer_get_next_event() does not update cpu_base::[softirq_]_expires_next to preserve reprogramming logic. That needs to be done at the callsites. hrtimer_force_reprogram() updates cpu_base::softirq_expires_next only when the first expiring timer is a softirq timer and the soft interrupt is not activated. That's wrong because cpu_base::softirq_expires_next is left stale when the first expiring timer of all bases is a timer which expires in hard interrupt context. hrtimer_interrupt() does never update cpu_base::softirq_expires_next which is wrong too. That becomes a problem when clock_settime() sets CLOCK_REALTIME forward and the first soft expiring timer is in the CLOCK_REALTIME_SOFT base. Setting CLOCK_REALTIME forward moves the clock MONOTONIC based expiry time of that timer before the stale cpu_base::softirq_expires_next. cpu_base::softirq_expires_next is cached to make the check for raising the soft interrupt fast. In the above case the soft interrupt won't be raised until clock monotonic reaches the stale cpu_base::softirq_expires_next value. That's incorrect, but what's worse it that if the softirq timer becomes the first expiring timer of all clock bases after the hard expiry timer has been handled the reprogramming of the clockevent from hrtimer_interrupt() will result in an interrupt storm. That happens because the reprogramming does not use cpu_base::softirq_expires_next, it uses __hrtimer_get_next_event() which returns the actual expiry time. Once clock MONOTONIC reaches cpu_base::softirq_expires_next the soft interrupt is raised and the storm subsides. Change the logic in hrtimer_force_reprogram() to evaluate the soft and hard bases seperately, update softirq_expires_next and handle the case when a soft expiring timer is the first of all bases by comparing the expiry times and updating the required cpu base fields. Split this functionality into a separate function to be able to use it in hrtimer_interrupt() as well without copy paste.

Anna-Maria Behnsen committed 5 years ago

Commit 46eb1701 parent a38fd871 file · +39 −21Browse files

Unified · Split

kernel/time/hrtimer.c+39−21
@@ -546,8 +546,11 @@ static ktime_t __hrtimer_next_event_base(struct hrtimer_cpu_base *cpu_base, · 545 unmodified lines
546}546}
547 547
548/*548/*
549 * Recomputes cpu_base::*next_timer and returns the earliest expires_next but549 * Recomputes cpu_base::*next_timer and returns the earliest expires_next
550 * does not set cpu_base::*expires_next, that is done by hrtimer_reprogram.550 * but does not set cpu_base::*expires_next, that is done by
551 * hrtimer[_force]_reprogram and hrtimer_interrupt only. When updating
552 * cpu_base::*expires_next right away, reprogramming logic would no longer
553 * work.
551 *554 *
552 * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,555 * When a softirq is pending, we can ignore the HRTIMER_ACTIVE_SOFT bases,
553 * those timers will get run whenever the softirq gets handled, at the end of556 * those timers will get run whenever the softirq gets handled, at the end of
@@ -588,6 +591,37 @@ __hrtimer_get_next_event(struct hrtimer_cpu_base *cpu_base, unsigned int active_ · 34 unmodified lines
588 return expires_next;591 return expires_next;
589}592}
590 593
594static ktime_t hrtimer_update_next_event(struct hrtimer_cpu_base *cpu_base)
595{
596 ktime_t expires_next, soft = KTIME_MAX;
597
598 /*
599 * If the soft interrupt has already been activated, ignore the
600 * soft bases. They will be handled in the already raised soft
601 * interrupt.
602 */
603 if (!cpu_base->softirq_activated) {
604 soft = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_SOFT);
605 /*
606 * Update the soft expiry time. clock_settime() might have
607 * affected it.
608 */
609 cpu_base->softirq_expires_next = soft;
610 }
611
612 expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_HARD);
613 /*
614 * If a softirq timer is expiring first, update cpu_base->next_timer
615 * and program the hardware with the soft expiry time.
616 */
617 if (expires_next > soft) {
618 cpu_base->next_timer = cpu_base->softirq_next_timer;
619 expires_next = soft;
620 }
621
622 return expires_next;
623}
624
591static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)625static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
592{626{
593 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;627 ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
@@ -628,23 +662,7 @@ hrtimer_force_reprogram(struct hrtimer_cpu_base *cpu_base, int skip_equal) · 34 unmodified lines
628{662{
629 ktime_t expires_next;663 ktime_t expires_next;
630 664
631 /*665 expires_next = hrtimer_update_next_event(cpu_base);
632 * Find the current next expiration time.
633 */
634 expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);
635
636 if (cpu_base->next_timer && cpu_base->next_timer->is_soft) {
637 /*
638 * When the softirq is activated, hrtimer has to be
639 * programmed with the first hard hrtimer because soft
640 * timer interrupt could occur too late.
641 */
642 if (cpu_base->softirq_activated)
643 expires_next = __hrtimer_get_next_event(cpu_base,
644 HRTIMER_ACTIVE_HARD);
645 else
646 cpu_base->softirq_expires_next = expires_next;
647 }
648 666
649 if (skip_equal && expires_next == cpu_base->expires_next)667 if (skip_equal && expires_next == cpu_base->expires_next)
650 return;668 return;
@@ -1644,8 +1662,8 @@ retry: · 993 unmodified lines
1644 1662
1645 __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);1663 __hrtimer_run_queues(cpu_base, now, flags, HRTIMER_ACTIVE_HARD);
1646 1664
1647 /* Reevaluate the clock bases for the next expiry */1665 /* Reevaluate the clock bases for the [soft] next expiry */
1648 expires_next = __hrtimer_get_next_event(cpu_base, HRTIMER_ACTIVE_ALL);1666 expires_next = hrtimer_update_next_event(cpu_base);
1649 /*1667 /*
1650 * Store the new expiry value so the migration code can verify1668 * Store the new expiry value so the migration code can verify
1651 * against it.1669 * against it.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t