| .. | .. |
|---|
| 150 | 150 | |
|---|
| 151 | 151 | #define migration_base migration_cpu_base.clock_base[0] |
|---|
| 152 | 152 | |
|---|
| 153 | +static inline bool is_migration_base(struct hrtimer_clock_base *base) |
|---|
| 154 | +{ |
|---|
| 155 | + return base == &migration_base; |
|---|
| 156 | +} |
|---|
| 157 | + |
|---|
| 153 | 158 | /* |
|---|
| 154 | 159 | * We are using hashed locking: holding per_cpu(hrtimer_bases)[n].lock |
|---|
| 155 | 160 | * means that all timers which are tied to this base via timer->base are |
|---|
| .. | .. |
|---|
| 273 | 278 | } |
|---|
| 274 | 279 | |
|---|
| 275 | 280 | #else /* CONFIG_SMP */ |
|---|
| 281 | + |
|---|
| 282 | +static inline bool is_migration_base(struct hrtimer_clock_base *base) |
|---|
| 283 | +{ |
|---|
| 284 | + return false; |
|---|
| 285 | +} |
|---|
| 276 | 286 | |
|---|
| 277 | 287 | static inline struct hrtimer_clock_base * |
|---|
| 278 | 288 | lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags) |
|---|
| .. | .. |
|---|
| 957 | 967 | } |
|---|
| 958 | 968 | EXPORT_SYMBOL_GPL(hrtimer_forward); |
|---|
| 959 | 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 | + |
|---|
| 960 | 980 | /* |
|---|
| 961 | 981 | * enqueue_hrtimer - internal function to (re)start a timer |
|---|
| 962 | 982 | * |
|---|
| .. | .. |
|---|
| 1175 | 1195 | * Check whether the HRTIMER_MODE_SOFT bit and hrtimer.is_soft |
|---|
| 1176 | 1196 | * match. |
|---|
| 1177 | 1197 | */ |
|---|
| 1198 | +#ifndef CONFIG_PREEMPT_RT_BASE |
|---|
| 1178 | 1199 | WARN_ON_ONCE(!(mode & HRTIMER_MODE_SOFT) ^ !timer->is_soft); |
|---|
| 1200 | +#endif |
|---|
| 1179 | 1201 | |
|---|
| 1180 | 1202 | base = lock_hrtimer_base(timer, &flags); |
|---|
| 1181 | 1203 | |
|---|
| .. | .. |
|---|
| 1238 | 1260 | |
|---|
| 1239 | 1261 | if (ret >= 0) |
|---|
| 1240 | 1262 | return ret; |
|---|
| 1241 | | - cpu_relax(); |
|---|
| 1263 | + hrtimer_grab_expiry_lock(timer); |
|---|
| 1242 | 1264 | } |
|---|
| 1243 | 1265 | } |
|---|
| 1244 | 1266 | EXPORT_SYMBOL_GPL(hrtimer_cancel); |
|---|
| .. | .. |
|---|
| 1335 | 1357 | static void __hrtimer_init(struct hrtimer *timer, clockid_t clock_id, |
|---|
| 1336 | 1358 | enum hrtimer_mode mode) |
|---|
| 1337 | 1359 | { |
|---|
| 1338 | | - bool softtimer = !!(mode & HRTIMER_MODE_SOFT); |
|---|
| 1339 | | - int base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0; |
|---|
| 1360 | + bool softtimer; |
|---|
| 1361 | + int base; |
|---|
| 1340 | 1362 | struct hrtimer_cpu_base *cpu_base; |
|---|
| 1363 | + |
|---|
| 1364 | + softtimer = !!(mode & HRTIMER_MODE_SOFT); |
|---|
| 1365 | +#ifdef CONFIG_PREEMPT_RT_FULL |
|---|
| 1366 | + if (!softtimer && !(mode & HRTIMER_MODE_HARD)) |
|---|
| 1367 | + softtimer = true; |
|---|
| 1368 | +#endif |
|---|
| 1369 | + base = softtimer ? HRTIMER_MAX_CLOCK_BASES / 2 : 0; |
|---|
| 1341 | 1370 | |
|---|
| 1342 | 1371 | memset(timer, 0, sizeof(struct hrtimer)); |
|---|
| 1343 | 1372 | |
|---|
| .. | .. |
|---|
| 1535 | 1564 | unsigned long flags; |
|---|
| 1536 | 1565 | ktime_t now; |
|---|
| 1537 | 1566 | |
|---|
| 1567 | + spin_lock(&cpu_base->softirq_expiry_lock); |
|---|
| 1538 | 1568 | raw_spin_lock_irqsave(&cpu_base->lock, flags); |
|---|
| 1539 | 1569 | |
|---|
| 1540 | 1570 | now = hrtimer_update_base(cpu_base); |
|---|
| .. | .. |
|---|
| 1544 | 1574 | hrtimer_update_softirq_timer(cpu_base, true); |
|---|
| 1545 | 1575 | |
|---|
| 1546 | 1576 | raw_spin_unlock_irqrestore(&cpu_base->lock, flags); |
|---|
| 1577 | + spin_unlock(&cpu_base->softirq_expiry_lock); |
|---|
| 1547 | 1578 | } |
|---|
| 1548 | 1579 | |
|---|
| 1549 | 1580 | #ifdef CONFIG_HIGH_RES_TIMERS |
|---|
| .. | .. |
|---|
| 1715 | 1746 | return HRTIMER_NORESTART; |
|---|
| 1716 | 1747 | } |
|---|
| 1717 | 1748 | |
|---|
| 1718 | | -void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task) |
|---|
| 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) |
|---|
| 1719 | 1753 | { |
|---|
| 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) |
|---|
| 1757 | + mode |= HRTIMER_MODE_HARD; |
|---|
| 1758 | + else |
|---|
| 1759 | + mode |= HRTIMER_MODE_SOFT; |
|---|
| 1760 | + } |
|---|
| 1761 | +#endif |
|---|
| 1762 | + __hrtimer_init(&sl->timer, clock_id, mode); |
|---|
| 1720 | 1763 | sl->timer.function = hrtimer_wakeup; |
|---|
| 1721 | 1764 | sl->task = task; |
|---|
| 1722 | 1765 | } |
|---|
| 1766 | + |
|---|
| 1767 | +/** |
|---|
| 1768 | + * hrtimer_init_sleeper - initialize sleeper to the given clock |
|---|
| 1769 | + * @sl: sleeper to be initialized |
|---|
| 1770 | + * @clock_id: the clock to be used |
|---|
| 1771 | + * @mode: timer mode abs/rel |
|---|
| 1772 | + * @task: the task to wake up |
|---|
| 1773 | + */ |
|---|
| 1774 | +void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, clockid_t clock_id, |
|---|
| 1775 | + enum hrtimer_mode mode, struct task_struct *task) |
|---|
| 1776 | +{ |
|---|
| 1777 | + debug_init(&sl->timer, clock_id, mode); |
|---|
| 1778 | + __hrtimer_init_sleeper(sl, clock_id, mode, task); |
|---|
| 1779 | + |
|---|
| 1780 | +} |
|---|
| 1723 | 1781 | 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 |
|---|
| 1724 | 1794 | |
|---|
| 1725 | 1795 | int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts) |
|---|
| 1726 | 1796 | { |
|---|
| .. | .. |
|---|
| 1745 | 1815 | { |
|---|
| 1746 | 1816 | struct restart_block *restart; |
|---|
| 1747 | 1817 | |
|---|
| 1748 | | - hrtimer_init_sleeper(t, current); |
|---|
| 1749 | | - |
|---|
| 1750 | 1818 | do { |
|---|
| 1751 | 1819 | set_current_state(TASK_INTERRUPTIBLE); |
|---|
| 1752 | 1820 | hrtimer_start_expires(&t->timer, mode); |
|---|
| .. | .. |
|---|
| 1754 | 1822 | if (likely(t->task)) |
|---|
| 1755 | 1823 | freezable_schedule(); |
|---|
| 1756 | 1824 | |
|---|
| 1825 | + __set_current_state(TASK_RUNNING); |
|---|
| 1757 | 1826 | hrtimer_cancel(&t->timer); |
|---|
| 1758 | 1827 | mode = HRTIMER_MODE_ABS; |
|---|
| 1759 | 1828 | |
|---|
| 1760 | 1829 | } while (t->task && !signal_pending(current)); |
|---|
| 1761 | 1830 | |
|---|
| 1762 | | - __set_current_state(TASK_RUNNING); |
|---|
| 1763 | 1831 | |
|---|
| 1764 | 1832 | if (!t->task) |
|---|
| 1765 | 1833 | return 0; |
|---|
| .. | .. |
|---|
| 1783 | 1851 | struct hrtimer_sleeper t; |
|---|
| 1784 | 1852 | int ret; |
|---|
| 1785 | 1853 | |
|---|
| 1786 | | - hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid, |
|---|
| 1787 | | - HRTIMER_MODE_ABS); |
|---|
| 1854 | + hrtimer_init_sleeper_on_stack(&t, restart->nanosleep.clockid, |
|---|
| 1855 | + HRTIMER_MODE_ABS, current); |
|---|
| 1788 | 1856 | hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires); |
|---|
| 1789 | | - |
|---|
| 1790 | 1857 | ret = do_nanosleep(&t, HRTIMER_MODE_ABS); |
|---|
| 1791 | 1858 | destroy_hrtimer_on_stack(&t.timer); |
|---|
| 1792 | 1859 | return ret; |
|---|
| .. | .. |
|---|
| 1804 | 1871 | if (dl_task(current) || rt_task(current)) |
|---|
| 1805 | 1872 | slack = 0; |
|---|
| 1806 | 1873 | |
|---|
| 1807 | | - hrtimer_init_on_stack(&t.timer, clockid, mode); |
|---|
| 1874 | + hrtimer_init_sleeper_on_stack(&t, clockid, mode, current); |
|---|
| 1808 | 1875 | hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack); |
|---|
| 1809 | 1876 | ret = do_nanosleep(&t, mode); |
|---|
| 1810 | 1877 | if (ret != -ERESTART_RESTARTBLOCK) |
|---|
| .. | .. |
|---|
| 1864 | 1931 | } |
|---|
| 1865 | 1932 | #endif |
|---|
| 1866 | 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 | +#endif |
|---|
| 1965 | + |
|---|
| 1867 | 1966 | /* |
|---|
| 1868 | 1967 | * Functions related to boot-time initialization: |
|---|
| 1869 | 1968 | */ |
|---|
| .. | .. |
|---|
| 1885 | 1984 | cpu_base->softirq_next_timer = NULL; |
|---|
| 1886 | 1985 | cpu_base->expires_next = KTIME_MAX; |
|---|
| 1887 | 1986 | cpu_base->softirq_expires_next = KTIME_MAX; |
|---|
| 1987 | + spin_lock_init(&cpu_base->softirq_expiry_lock); |
|---|
| 1888 | 1988 | return 0; |
|---|
| 1889 | 1989 | } |
|---|
| 1890 | 1990 | |
|---|
| .. | .. |
|---|
| 2003 | 2103 | return -EINTR; |
|---|
| 2004 | 2104 | } |
|---|
| 2005 | 2105 | |
|---|
| 2006 | | - hrtimer_init_on_stack(&t.timer, clock_id, mode); |
|---|
| 2106 | + hrtimer_init_sleeper_on_stack(&t, clock_id, mode, current); |
|---|
| 2007 | 2107 | hrtimer_set_expires_range_ns(&t.timer, *expires, delta); |
|---|
| 2008 | | - |
|---|
| 2009 | | - hrtimer_init_sleeper(&t, current); |
|---|
| 2010 | 2108 | |
|---|
| 2011 | 2109 | hrtimer_start_expires(&t.timer, mode); |
|---|
| 2012 | 2110 | |
|---|