History

Merge branch 'sched/migrate-disable'

Peter Zijlstra committed 6 years ago

Commit 12fa97c2 parents b6d37a7 c777d8419 files · +1038 −227Browse files

Showing the changes to kernel (9 of 19 files). Show all

Unified · Split

9 files
  1. kernel/cpu.c +8 −1
  2. kernel/sched/core.c +739 −181
  3. kernel/sched/cpudeadline.c +2 −2
  4. kernel/sched/cpupri.c +2 −2
  5. kernel/sched/deadline.c +31 −15
  6. kernel/sched/rt.c +57 −18
  7. kernel/sched/sched.h +56 −3
  8. kernel/stop_machine.c +24 −3
  9. kernel/workqueue.c +4 −0
kernel/cpu.c+8−1
@@ -1602,7 +1602,7 @@ static struct cpuhp_step cpuhp_hp_states[] = { · 1601 unmodified lines
16021602 .name = "ap:online",
16031603 },
16041604 /*
1605− * Handled on controll processor until the plugged processor manages
1605+ * Handled on control processor until the plugged processor manages
16061606 * this itself.
16071607 */
16081608 [CPUHP_TEARDOWN_CPU] = {
@@ -1611,6 +1611,13 @@ static struct cpuhp_step cpuhp_hp_states[] = { · 2 unmodified lines
16111611 .teardown.single = takedown_cpu,
16121612 .cant_stop = true,
16131613 },
1614+
1615+ [CPUHP_AP_SCHED_WAIT_EMPTY] = {
1616+ .name = "sched:waitempty",
1617+ .startup.single = NULL,
1618+ .teardown.single = sched_cpu_wait_empty,
1619+ },
1620+
16141621 /* Handle smpboot threads park/unpark */
16151622 [CPUHP_AP_SMPBOOT_THREADS] = {
16161623 .name = "smpboot/threads:online",
kernel/sched/core.c+739−181
@@ -1696,6 +1696,80 @@ void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) · 1695 unmodified lines
16961696
16971697 #ifdef CONFIG_SMP
16981698
1699+#ifdef CONFIG_PREEMPT_RT
1700+
1701+static void
1702+__do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask, u32 flags);
1703+
1704+static int __set_cpus_allowed_ptr(struct task_struct *p,
1705+ const struct cpumask *new_mask,
1706+ u32 flags);
1707+
1708+static void migrate_disable_switch(struct rq *rq, struct task_struct *p)
1709+{
1710+ if (likely(!p->migration_disabled))
1711+ return;
1712+
1713+ if (p->cpus_ptr != &p->cpus_mask)
1714+ return;
1715+
1716+ /*
1717+ * Violates locking rules! see comment in __do_set_cpus_allowed().
1718+ */
1719+ __do_set_cpus_allowed(p, cpumask_of(rq->cpu), SCA_MIGRATE_DISABLE);
1720+}
1721+
1722+void migrate_disable(void)
1723+{
1724+ struct task_struct *p = current;
1725+
1726+ if (p->migration_disabled) {
1727+ p->migration_disabled++;
1728+ return;
1729+ }
1730+
1731+ preempt_disable();
1732+ this_rq()->nr_pinned++;
1733+ p->migration_disabled = 1;
1734+ preempt_enable();
1735+}
1736+EXPORT_SYMBOL_GPL(migrate_disable);
1737+
1738+void migrate_enable(void)
1739+{
1740+ struct task_struct *p = current;
1741+
1742+ if (p->migration_disabled > 1) {
1743+ p->migration_disabled--;
1744+ return;
1745+ }
1746+
1747+ /*
1748+ * Ensure stop_task runs either before or after this, and that
1749+ * __set_cpus_allowed_ptr(SCA_MIGRATE_ENABLE) doesn't schedule().
1750+ */
1751+ preempt_disable();
1752+ if (p->cpus_ptr != &p->cpus_mask)
1753+ __set_cpus_allowed_ptr(p, &p->cpus_mask, SCA_MIGRATE_ENABLE);
1754+ /*
1755+ * Mustn't clear migration_disabled() until cpus_ptr points back at the
1756+ * regular cpus_mask, otherwise things that race (eg.
1757+ * select_fallback_rq) get confused.
1758+ */
1759+ barrier();
1760+ p->migration_disabled = 0;
1761+ this_rq()->nr_pinned--;
1762+ preempt_enable();
1763+}
1764+EXPORT_SYMBOL_GPL(migrate_enable);
1765+
1766+static inline bool rq_has_pinned_tasks(struct rq *rq)
1767+{
1768+ return rq->nr_pinned;
1769+}
1770+
1771+#endif
1772+
16991773 /*
17001774 * Per-CPU kthreads are allowed to run on !active && online CPUs, see
17011775 * __set_cpus_allowed_ptr() and select_fallback_rq().
@@ -1705,7 +1779,7 @@ static inline bool is_cpu_allowed(struct task_struct *p, int cpu) · 3 unmodified lines
17051779 if (!cpumask_test_cpu(cpu, p->cpus_ptr))
17061780 return false;
17071781
1708− if (is_per_cpu_kthread(p))
1782+ if (is_per_cpu_kthread(p) || is_migration_disabled(p))
17091783 return cpu_online(cpu);
17101784
17111785 return cpu_active(cpu);
@@ -1750,8 +1824,16 @@ static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf, · 38 unmodified lines
17501824 }
17511825
17521826 struct migration_arg {
1753− struct task_struct *task;
1754− int dest_cpu;
1827+ struct task_struct *task;
1828+ int dest_cpu;
1829+ struct set_affinity_pending *pending;
1830+};
1831+
1832+struct set_affinity_pending {
1833+ refcount_t refs;
1834+ struct completion done;
1835+ struct cpu_stop_work stop_work;
1836+ struct migration_arg arg;
17551837 };
17561838
17571839 /*
@@ -1783,16 +1865,19 @@ static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf, · 25 unmodified lines
17831865 */
17841866 static int migration_cpu_stop(void *data)
17851867 {
1868+ struct set_affinity_pending *pending;
17861869 struct migration_arg *arg = data;
17871870 struct task_struct *p = arg->task;
1871+ int dest_cpu = arg->dest_cpu;
17881872 struct rq *rq = this_rq();
1873+ bool complete = false;
17891874 struct rq_flags rf;
17901875
17911876 /*
17921877 * The original target CPU might have gone down and we might
17931878 * be on another CPU but it doesn't matter.
17941879 */
1795− local_irq_disable();
1880+ local_irq_save(rf.flags);
17961881 /*
17971882 * We need to explicitly wake pending tasks before running
17981883 * __migrate_task() such that we will not miss enforcing cpus_ptr
@@ -1802,21 +1887,126 @@ static int migration_cpu_stop(void *data) · 3 unmodified lines
18021887
18031888 raw_spin_lock(&p->pi_lock);
18041889 rq_lock(rq, &rf);
1890+
1891+ pending = p->migration_pending;
18051892 /*
18061893 * If task_rq(p) != rq, it cannot be migrated here, because we're
18071894 * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
18081895 * we're holding p->pi_lock.
18091896 */
18101897 if (task_rq(p) == rq) {
1898+ if (is_migration_disabled(p))
1899+ goto out;
1900+
1901+ if (pending) {
1902+ p->migration_pending = NULL;
1903+ complete = true;
1904+ }
1905+
1906+ /* migrate_enable() -- we must not race against SCA */
1907+ if (dest_cpu < 0) {
1908+ /*
1909+ * When this was migrate_enable() but we no longer
1910+ * have a @pending, a concurrent SCA 'fixed' things
1911+ * and we should be valid again. Nothing to do.
1912+ */
1913+ if (!pending) {
1914+ WARN_ON_ONCE(!is_cpu_allowed(p, cpu_of(rq)));
1915+ goto out;
1916+ }
1917+
1918+ dest_cpu = cpumask_any_distribute(&p->cpus_mask);
1919+ }
1920+
18111921 if (task_on_rq_queued(p))
1812− rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
1922+ rq = __migrate_task(rq, &rf, p, dest_cpu);
18131923 else
1814− p->wake_cpu = arg->dest_cpu;
1924+ p->wake_cpu = dest_cpu;
1925+
1926+ } else if (dest_cpu < 0) {
1927+ /*
1928+ * This happens when we get migrated between migrate_enable()'s
1929+ * preempt_enable() and scheduling the stopper task. At that
1930+ * point we're a regular task again and not current anymore.
1931+ *
1932+ * A !PREEMPT kernel has a giant hole here, which makes it far
1933+ * more likely.
1934+ */
1935+
1936+ /*
1937+ * When this was migrate_enable() but we no longer have an
1938+ * @pending, a concurrent SCA 'fixed' things and we should be
1939+ * valid again. Nothing to do.
1940+ */
1941+ if (!pending) {
1942+ WARN_ON_ONCE(!is_cpu_allowed(p, cpu_of(rq)));
1943+ goto out;
1944+ }
1945+
1946+ /*
1947+ * When migrate_enable() hits a rq mis-match we can't reliably
1948+ * determine is_migration_disabled() and so have to chase after
1949+ * it.
1950+ */
1951+ task_rq_unlock(rq, p, &rf);
1952+ stop_one_cpu_nowait(task_cpu(p), migration_cpu_stop,
1953+ &pending->arg, &pending->stop_work);
1954+ return 0;
18151955 }
1816− rq_unlock(rq, &rf);
1817− raw_spin_unlock(&p->pi_lock);
1956+out:
1957+ task_rq_unlock(rq, p, &rf);
1958+
1959+ if (complete)
1960+ complete_all(&pending->done);
1961+
1962+ /* For pending->{arg,stop_work} */
1963+ pending = arg->pending;
1964+ if (pending && refcount_dec_and_test(&pending->refs))
1965+ wake_up_var(&pending->refs);
18181966
1819− local_irq_enable();
1967+ return 0;
1968+}
1969+
1970+int push_cpu_stop(void *arg)
1971+{
1972+ struct rq *lowest_rq = NULL, *rq = this_rq();
1973+ struct task_struct *p = arg;
1974+
1975+ raw_spin_lock_irq(&p->pi_lock);
1976+ raw_spin_lock(&rq->lock);
1977+
1978+ if (task_rq(p) != rq)
1979+ goto out_unlock;
1980+
1981+ if (is_migration_disabled(p)) {
1982+ p->migration_flags |= MDF_PUSH;
1983+ goto out_unlock;
1984+ }
1985+
1986+ p->migration_flags &= ~MDF_PUSH;
1987+
1988+ if (p->sched_class->find_lock_rq)
1989+ lowest_rq = p->sched_class->find_lock_rq(p, rq);
1990+
1991+ if (!lowest_rq)
1992+ goto out_unlock;
1993+
1994+ // XXX validate p is still the highest prio task
1995+ if (task_rq(p) == rq) {
1996+ deactivate_task(rq, p, 0);
1997+ set_task_cpu(p, lowest_rq->cpu);
1998+ activate_task(lowest_rq, p, 0);
1999+ resched_curr(lowest_rq);
2000+ }
2001+
2002+ double_unlock_balance(rq, lowest_rq);
2003+
2004+out_unlock:
2005+ rq->push_busy = false;
2006+ raw_spin_unlock(&rq->lock);
2007+ raw_spin_unlock_irq(&p->pi_lock);
2008+
2009+ put_task_struct(p);
18202010 return 0;
18212011 }
18222012
@@ -1824,18 +2014,39 @@ static int migration_cpu_stop(void *data) · 1 unmodified line
18242014 * sched_class::set_cpus_allowed must do the below, but is not required to
18252015 * actually call this function.
18262016 */
1827−void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask)
2017+void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask, u32 flags)
18282018 {
2019+ if (flags & (SCA_MIGRATE_ENABLE | SCA_MIGRATE_DISABLE)) {
2020+ p->cpus_ptr = new_mask;
2021+ return;
2022+ }
2023+
18292024 cpumask_copy(&p->cpus_mask, new_mask);
18302025 p->nr_cpus_allowed = cpumask_weight(new_mask);
18312026 }
18322027
1833−void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
2028+static void
2029+__do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask, u32 flags)
18342030 {
18352031 struct rq *rq = task_rq(p);
18362032 bool queued, running;
18372033
1838− lockdep_assert_held(&p->pi_lock);
2034+ /*
2035+ * This here violates the locking rules for affinity, since we're only
2036+ * supposed to change these variables while holding both rq->lock and
2037+ * p->pi_lock.
2038+ *
2039+ * HOWEVER, it magically works, because ttwu() is the only code that
2040+ * accesses these variables under p->pi_lock and only does so after
2041+ * smp_cond_load_acquire(&p->on_cpu, !VAL), and we're in __schedule()
2042+ * before finish_task().
2043+ *
2044+ * XXX do further audits, this smells like something putrid.
2045+ */
2046+ if (flags & SCA_MIGRATE_DISABLE)
2047+ SCHED_WARN_ON(!p->on_cpu);
2048+ else
2049+ lockdep_assert_held(&p->pi_lock);
18392050
18402051 queued = task_on_rq_queued(p);
18412052 running = task_current(rq, p);
@@ -1851,7 +2062,7 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) · 9 unmodified lines
18512062 if (running)
18522063 put_prev_task(rq, p);
18532064
1854− p->sched_class->set_cpus_allowed(p, new_mask);
2065+ p->sched_class->set_cpus_allowed(p, new_mask, flags);
18552066
18562067 if (queued)
18572068 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
@@ -1859,6 +2070,208 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) · 1 unmodified line
18592070 set_next_task(rq, p);
18602071 }
18612072
2073+void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
2074+{
2075+ __do_set_cpus_allowed(p, new_mask, 0);
2076+}
2077+
2078+/*
2079+ * This function is wildly self concurrent; here be dragons.
2080+ *
2081+ *
2082+ * When given a valid mask, __set_cpus_allowed_ptr() must block until the
2083+ * designated task is enqueued on an allowed CPU. If that task is currently
2084+ * running, we have to kick it out using the CPU stopper.
2085+ *
2086+ * Migrate-Disable comes along and tramples all over our nice sandcastle.
2087+ * Consider:
2088+ *
2089+ * Initial conditions: P0->cpus_mask = [0, 1]
2090+ *
2091+ * P0@CPU0 P1
2092+ *
2093+ * migrate_disable();
2094+ * <preempted>
2095+ * set_cpus_allowed_ptr(P0, [1]);
2096+ *
2097+ * P1 *cannot* return from this set_cpus_allowed_ptr() call until P0 executes
2098+ * its outermost migrate_enable() (i.e. it exits its Migrate-Disable region).
2099+ * This means we need the following scheme:
2100+ *
2101+ * P0@CPU0 P1
2102+ *
2103+ * migrate_disable();
2104+ * <preempted>
2105+ * set_cpus_allowed_ptr(P0, [1]);
2106+ * <blocks>
2107+ * <resumes>
2108+ * migrate_enable();
2109+ * __set_cpus_allowed_ptr();
2110+ * <wakes local stopper>
2111+ * `--> <woken on migration completion>
2112+ *
2113+ * Now the fun stuff: there may be several P1-like tasks, i.e. multiple
2114+ * concurrent set_cpus_allowed_ptr(P0, [*]) calls. CPU affinity changes of any
2115+ * task p are serialized by p->pi_lock, which we can leverage: the one that
2116+ * should come into effect at the end of the Migrate-Disable region is the last
2117+ * one. This means we only need to track a single cpumask (i.e. p->cpus_mask),
2118+ * but we still need to properly signal those waiting tasks at the appropriate
2119+ * moment.
2120+ *
2121+ * This is implemented using struct set_affinity_pending. The first
2122+ * __set_cpus_allowed_ptr() caller within a given Migrate-Disable region will
2123+ * setup an instance of that struct and install it on the targeted task_struct.
2124+ * Any and all further callers will reuse that instance. Those then wait for
2125+ * a completion signaled at the tail of the CPU stopper callback (1), triggered
2126+ * on the end of the Migrate-Disable region (i.e. outermost migrate_enable()).
2127+ *
2128+ *
2129+ * (1) In the cases covered above. There is one more where the completion is
2130+ * signaled within affine_move_task() itself: when a subsequent affinity request
2131+ * cancels the need for an active migration. Consider:
2132+ *
2133+ * Initial conditions: P0->cpus_mask = [0, 1]
2134+ *
2135+ * P0@CPU0 P1 P2
2136+ *
2137+ * migrate_disable();
2138+ * <preempted>
2139+ * set_cpus_allowed_ptr(P0, [1]);
2140+ * <blocks>
2141+ * set_cpus_allowed_ptr(P0, [0, 1]);
2142+ * <signal completion>
2143+ * <awakes>
2144+ *
2145+ * Note that the above is safe vs a concurrent migrate_enable(), as any
2146+ * pending affinity completion is preceded by an uninstallation of
2147+ * p->migration_pending done with p->pi_lock held.
2148+ */
2149+static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flags *rf,
2150+ int dest_cpu, unsigned int flags)
2151+{
2152+ struct set_affinity_pending my_pending = { }, *pending = NULL;
2153+ struct migration_arg arg = {
2154+ .task = p,
2155+ .dest_cpu = dest_cpu,
2156+ };
2157+ bool complete = false;
2158+
2159+ /* Can the task run on the task's current CPU? If so, we're done */
2160+ if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) {
2161+ struct task_struct *push_task = NULL;
2162+
2163+ if ((flags & SCA_MIGRATE_ENABLE) &&
2164+ (p->migration_flags & MDF_PUSH) && !rq->push_busy) {
2165+ rq->push_busy = true;
2166+ push_task = get_task_struct(p);
2167+ }
2168+
2169+ pending = p->migration_pending;
2170+ if (pending) {
2171+ refcount_inc(&pending->refs);
2172+ p->migration_pending = NULL;
2173+ complete = true;
2174+ }
2175+ task_rq_unlock(rq, p, rf);
2176+
2177+ if (push_task) {
2178+ stop_one_cpu_nowait(rq->cpu, push_cpu_stop,
2179+ p, &rq->push_work);
2180+ }
2181+
2182+ if (complete)
2183+ goto do_complete;
2184+
2185+ return 0;
2186+ }
2187+
2188+ if (!(flags & SCA_MIGRATE_ENABLE)) {
2189+ /* serialized by p->pi_lock */
2190+ if (!p->migration_pending) {
2191+ /* Install the request */
2192+ refcount_set(&my_pending.refs, 1);
2193+ init_completion(&my_pending.done);
2194+ p->migration_pending = &my_pending;
2195+ } else {
2196+ pending = p->migration_pending;
2197+ refcount_inc(&pending->refs);
2198+ }
2199+ }
2200+ pending = p->migration_pending;
2201+ /*
2202+ * - !MIGRATE_ENABLE:
2203+ * we'll have installed a pending if there wasn't one already.
2204+ *
2205+ * - MIGRATE_ENABLE:
2206+ * we're here because the current CPU isn't matching anymore,
2207+ * the only way that can happen is because of a concurrent
2208+ * set_cpus_allowed_ptr() call, which should then still be
2209+ * pending completion.
2210+ *
2211+ * Either way, we really should have a @pending here.
2212+ */
2213+ if (WARN_ON_ONCE(!pending)) {
2214+ task_rq_unlock(rq, p, rf);
2215+ return -EINVAL;
2216+ }
2217+
2218+ if (flags & SCA_MIGRATE_ENABLE) {
2219+
2220+ refcount_inc(&pending->refs); /* pending->{arg,stop_work} */
2221+ p->migration_flags &= ~MDF_PUSH;
2222+ task_rq_unlock(rq, p, rf);
2223+
2224+ pending->arg = (struct migration_arg) {
2225+ .task = p,
2226+ .dest_cpu = -1,
2227+ .pending = pending,
2228+ };
2229+
2230+ stop_one_cpu_nowait(cpu_of(rq), migration_cpu_stop,
2231+ &pending->arg, &pending->stop_work);
2232+
2233+ return 0;
2234+ }
2235+
2236+ if (task_running(rq, p) || p->state == TASK_WAKING) {
2237+ /*
2238+ * Lessen races (and headaches) by delegating
2239+ * is_migration_disabled(p) checks to the stopper, which will
2240+ * run on the same CPU as said p.
2241+ */
2242+ task_rq_unlock(rq, p, rf);
2243+ stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
2244+
2245+ } else {
2246+
2247+ if (!is_migration_disabled(p)) {
2248+ if (task_on_rq_queued(p))
2249+ rq = move_queued_task(rq, rf, p, dest_cpu);
2250+
2251+ p->migration_pending = NULL;
2252+ complete = true;
2253+ }
2254+ task_rq_unlock(rq, p, rf);
2255+
2256+do_complete:
2257+ if (complete)
2258+ complete_all(&pending->done);
2259+ }
2260+
2261+ wait_for_completion(&pending->done);
2262+
2263+ if (refcount_dec_and_test(&pending->refs))
2264+ wake_up_var(&pending->refs);
2265+
2266+ /*
2267+ * Block the original owner of &pending until all subsequent callers
2268+ * have seen the completion and decremented the refcount
2269+ */
2270+ wait_var_event(&my_pending.refs, !refcount_read(&my_pending.refs));
2271+
2272+ return 0;
2273+}
2274+
18622275 /*
18632276 * Change a given task's CPU affinity. Migrate the thread to a
18642277 * proper CPU and schedule it away if the CPU it's executing on
@@ -1869,7 +2282,8 @@ void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) · 4 unmodified lines
18692282 * call is not atomic; no spinlocks may be held.
18702283 */
18712284 static int __set_cpus_allowed_ptr(struct task_struct *p,
1872− const struct cpumask *new_mask, bool check)
2285+ const struct cpumask *new_mask,
2286+ u32 flags)
18732287 {
18742288 const struct cpumask *cpu_valid_mask = cpu_active_mask;
18752289 unsigned int dest_cpu;
@@ -1880,9 +2294,14 @@ static int __set_cpus_allowed_ptr(struct task_struct *p, · 4 unmodified lines
18802294 rq = task_rq_lock(p, &rf);
18812295 update_rq_clock(rq);
18822296
1883− if (p->flags & PF_KTHREAD) {
2297+ if (p->flags & PF_KTHREAD || is_migration_disabled(p)) {
18842298 /*
1885− * Kernel threads are allowed on online && !active CPUs
2299+ * Kernel threads are allowed on online && !active CPUs.
2300+ *
2301+ * Specifically, migration_disabled() tasks must not fail the
2302+ * cpumask_any_and_distribute() pick below, esp. so on
2303+ * SCA_MIGRATE_ENABLE, otherwise we'll not call
2304+ * set_cpus_allowed_common() and actually reset p->cpus_ptr.
18862305 */
18872306 cpu_valid_mask = cpu_online_mask;
18882307 }
@@ -1891,13 +2310,22 @@ static int __set_cpus_allowed_ptr(struct task_struct *p, · 2 unmodified lines
18912310 * Must re-check here, to close a race against __kthread_bind(),
18922311 * sched_setaffinity() is not guaranteed to observe the flag.
18932312 */
1894− if (check && (p->flags & PF_NO_SETAFFINITY)) {
2313+ if ((flags & SCA_CHECK) && (p->flags & PF_NO_SETAFFINITY)) {
18952314 ret = -EINVAL;
18962315 goto out;
18972316 }
18982317
1899− if (cpumask_equal(&p->cpus_mask, new_mask))
1900− goto out;
2318+ if (!(flags & SCA_MIGRATE_ENABLE)) {
2319+ if (cpumask_equal(&p->cpus_mask, new_mask))
2320+ goto out;
2321+
2322+ if (WARN_ON_ONCE(p == current &&
2323+ is_migration_disabled(p) &&
2324+ !cpumask_test_cpu(task_cpu(p), new_mask))) {
2325+ ret = -EBUSY;
2326+ goto out;
2327+ }
2328+ }
19012329
19022330 /*
19032331 * Picking a ~random cpu helps in cases where we are changing affinity
@@ -1910,7 +2338,7 @@ static int __set_cpus_allowed_ptr(struct task_struct *p, · 6 unmodified lines
19102338 goto out;
19112339 }
19122340
1913− do_set_cpus_allowed(p, new_mask);
2341+ __do_set_cpus_allowed(p, new_mask, flags);
19142342
19152343 if (p->flags & PF_KTHREAD) {
19162344 /*
@@ -1922,23 +2350,8 @@ static int __set_cpus_allowed_ptr(struct task_struct *p, · 5 unmodified lines
19222350 p->nr_cpus_allowed != 1);
19232351 }
19242352
1925− /* Can the task run on the task's current CPU? If so, we're done */
1926− if (cpumask_test_cpu(task_cpu(p), new_mask))
1927− goto out;
2353+ return affine_move_task(rq, p, &rf, dest_cpu, flags);
19282354
1929− if (task_running(rq, p) || p->state == TASK_WAKING) {
1930− struct migration_arg arg = { p, dest_cpu };
1931− /* Need help from migration thread: drop lock and wait. */
1932− task_rq_unlock(rq, p, &rf);
1933− stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
1934− return 0;
1935− } else if (task_on_rq_queued(p)) {
1936− /*
1937− * OK, since we're going to drop the lock immediately
1938− * afterwards anyway.
1939− */
1940− rq = move_queued_task(rq, &rf, p, dest_cpu);
1941− }
19422355 out:
19432356 task_rq_unlock(rq, p, &rf);
19442357
@@ -1947,7 +2360,7 @@ out: · 2 unmodified lines
19472360
19482361 int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
19492362 {
1950− return __set_cpus_allowed_ptr(p, new_mask, false);
2363+ return __set_cpus_allowed_ptr(p, new_mask, 0);
19512364 }
19522365 EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
19532366
@@ -1988,6 +2401,8 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu) · 34 unmodified lines
19882401 * Clearly, migrating tasks to offline CPUs is a fairly daft thing.
19892402 */
19902403 WARN_ON_ONCE(!cpu_online(new_cpu));
2404+
2405+ WARN_ON_ONCE(is_migration_disabled(p));
19912406 #endif
19922407
19932408 trace_sched_migrate_task(p, new_cpu);
@@ -2318,6 +2733,12 @@ static int select_fallback_rq(int cpu, struct task_struct *p) · 324 unmodified lines
23182733 }
23192734 fallthrough;
23202735 case possible:
2736+ /*
2737+ * XXX When called from select_task_rq() we only
2738+ * hold p->pi_lock and again violate locking order.
2739+ *
2740+ * More yuck to audit.
2741+ */
23212742 do_set_cpus_allowed(p, cpu_possible_mask);
23222743 state = fail;
23232744 break;
@@ -2352,7 +2773,7 @@ int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags) · 28 unmodified lines
23522773 {
23532774 lockdep_assert_held(&p->pi_lock);
23542775
2355− if (p->nr_cpus_allowed > 1)
2776+ if (p->nr_cpus_allowed > 1 && !is_migration_disabled(p))
23562777 cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
23572778 else
23582779 cpu = cpumask_any(p->cpus_ptr);
@@ -2375,6 +2796,7 @@ int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags) · 16 unmodified lines
23752796
23762797 void sched_set_stop_task(int cpu, struct task_struct *stop)
23772798 {
2799+ static struct lock_class_key stop_pi_lock;
23782800 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
23792801 struct task_struct *old_stop = cpu_rq(cpu)->stop;
23802802
@@ -2390,6 +2812,20 @@ void sched_set_stop_task(int cpu, struct task_struct *stop) · 9 unmodified lines
23902812 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
23912813
23922814 stop->sched_class = &stop_sched_class;
2815+
2816+ /*
2817+ * The PI code calls rt_mutex_setprio() with ->pi_lock held to
2818+ * adjust the effective priority of a task. As a result,
2819+ * rt_mutex_setprio() can trigger (RT) balancing operations,
2820+ * which can then trigger wakeups of the stop thread to push
2821+ * around the current task.
2822+ *
2823+ * The stop task itself will never be part of the PI-chain, it
2824+ * never blocks, therefore that ->pi_lock recursion is safe.
2825+ * Tell lockdep about this by placing the stop->pi_lock in its
2826+ * own class.
2827+ */
2828+ lockdep_set_class(&stop->pi_lock, &stop_pi_lock);
23932829 }
23942830
23952831 cpu_rq(cpu)->stop = stop;
@@ -2406,13 +2842,25 @@ void sched_set_stop_task(int cpu, struct task_struct *stop) · 10 unmodified lines
24062842 #else
24072843
24082844 static inline int __set_cpus_allowed_ptr(struct task_struct *p,
2409− const struct cpumask *new_mask, bool check)
2845+ const struct cpumask *new_mask,
2846+ u32 flags)
24102847 {
24112848 return set_cpus_allowed_ptr(p, new_mask);
24122849 }
24132850
24142851 #endif /* CONFIG_SMP */
24152852
2853+#if !defined(CONFIG_SMP) || !defined(CONFIG_PREEMPT_RT)
2854+
2855+static inline void migrate_disable_switch(struct rq *rq, struct task_struct *p) { }
2856+
2857+static inline bool rq_has_pinned_tasks(struct rq *rq)
2858+{
2859+ return false;
2860+}
2861+
2862+#endif
2863+
24162864 static void
24172865 ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
24182866 {
@@ -3098,6 +3546,7 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p) · 679 unmodified lines
30983546 init_numa_balancing(clone_flags, p);
30993547 #ifdef CONFIG_SMP
31003548 p->wake_entry.u_flags = CSD_TYPE_TTWU;
3549+ p->migration_pending = NULL;
31013550 #endif
31023551 }
31033552
@@ -3485,6 +3934,90 @@ static inline void finish_task(struct task_struct *prev) · 381 unmodified lines
34853934 #endif
34863935 }
34873936
3937+#ifdef CONFIG_SMP
3938+
3939+static void do_balance_callbacks(struct rq *rq, struct callback_head *head)
3940+{
3941+ void (*func)(struct rq *rq);
3942+ struct callback_head *next;
3943+
3944+ lockdep_assert_held(&rq->lock);
3945+
3946+ while (head) {
3947+ func = (void (*)(struct rq *))head->func;
3948+ next = head->next;
3949+ head->next = NULL;
3950+ head = next;
3951+
3952+ func(rq);
3953+ }
3954+}
3955+
3956+static inline struct callback_head *splice_balance_callbacks(struct rq *rq)
3957+{
3958+ struct callback_head *head = rq->balance_callback;
3959+
3960+ lockdep_assert_held(&rq->lock);
3961+ if (head) {
3962+ rq->balance_callback = NULL;
3963+ rq->balance_flags &= ~BALANCE_WORK;
3964+ }
3965+
3966+ return head;
3967+}
3968+
3969+static void __balance_callbacks(struct rq *rq)
3970+{
3971+ do_balance_callbacks(rq, splice_balance_callbacks(rq));
3972+}
3973+
3974+static inline void balance_callbacks(struct rq *rq, struct callback_head *head)
3975+{
3976+ unsigned long flags;
3977+
3978+ if (unlikely(head)) {
3979+ raw_spin_lock_irqsave(&rq->lock, flags);
3980+ do_balance_callbacks(rq, head);
3981+ raw_spin_unlock_irqrestore(&rq->lock, flags);
3982+ }
3983+}
3984+
3985+static void balance_push(struct rq *rq);
3986+
3987+static inline void balance_switch(struct rq *rq)
3988+{
3989+ if (likely(!rq->balance_flags))
3990+ return;
3991+
3992+ if (rq->balance_flags & BALANCE_PUSH) {
3993+ balance_push(rq);
3994+ return;
3995+ }
3996+
3997+ __balance_callbacks(rq);
3998+}
3999+
4000+#else
4001+
4002+static inline void __balance_callbacks(struct rq *rq)
4003+{
4004+}
4005+
4006+static inline struct callback_head *splice_balance_callbacks(struct rq *rq)
4007+{
4008+ return NULL;
4009+}
4010+
4011+static inline void balance_callbacks(struct rq *rq, struct callback_head *head)
4012+{
4013+}
4014+
4015+static inline void balance_switch(struct rq *rq)
4016+{
4017+}
4018+
4019+#endif
4020+
34884021 static inline void
34894022 prepare_lock_switch(struct rq *rq, struct task_struct *next, struct rq_flags *rf)
34904023 {
@@ -3510,6 +4043,7 @@ static inline void finish_lock_switch(struct rq *rq) · 19 unmodified lines
35104043 * prev into current:
35114044 */
35124045 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
4046+ balance_switch(rq);
35134047 raw_spin_unlock_irq(&rq->lock);
35144048 }
35154049
@@ -3651,43 +4185,6 @@ static struct rq *finish_task_switch(struct task_struct *prev) · 135 unmodified lines
36514185 return rq;
36524186 }
36534187
3654−#ifdef CONFIG_SMP
3655−
3656−/* rq->lock is NOT held, but preemption is disabled */
3657−static void __balance_callback(struct rq *rq)
3658−{
3659− struct callback_head *head, *next;
3660− void (*func)(struct rq *rq);
3661− unsigned long flags;
3662−
3663− raw_spin_lock_irqsave(&rq->lock, flags);
3664− head = rq->balance_callback;
3665− rq->balance_callback = NULL;
3666− while (head) {
3667− func = (void (*)(struct rq *))head->func;
3668− next = head->next;
3669− head->next = NULL;
3670− head = next;
3671−
3672− func(rq);
3673− }
3674− raw_spin_unlock_irqrestore(&rq->lock, flags);
3675−}
3676−
3677−static inline void balance_callback(struct rq *rq)
3678−{
3679− if (unlikely(rq->balance_callback))
3680− __balance_callback(rq);
3681−}
3682−
3683−#else
3684−
3685−static inline void balance_callback(struct rq *rq)
3686−{
3687−}
3688−
3689−#endif
3690−
36914188 /**
36924189 * schedule_tail - first thing a freshly forked thread must call.
36934190 * @prev: the thread we just switched away from.
@@ -3707,7 +4204,6 @@ asmlinkage __visible void schedule_tail(struct task_struct *prev) · 13 unmodified lines
37074204 */
37084205
37094206 rq = finish_task_switch(prev);
3710− balance_callback(rq);
37114207 preempt_enable();
37124208
37134209 if (current->set_child_tid)
@@ -4515,6 +5011,7 @@ static void __sched notrace __schedule(bool preempt) · 801 unmodified lines
45155011 */
45165012 ++*switch_count;
45175013
5014+ migrate_disable_switch(rq, prev);
45185015 psi_sched_switch(prev, next, !task_on_rq_queued(prev));
45195016
45205017 trace_sched_switch(preempt, prev, next);
@@ -4523,10 +5020,11 @@ static void __sched notrace __schedule(bool preempt) · 2 unmodified lines
45235020 rq = context_switch(rq, prev, next, &rf);
45245021 } else {
45255022 rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
4526− rq_unlock_irq(rq, &rf);
4527− }
45285023
4529− balance_callback(rq);
5024+ rq_unpin_lock(rq, &rf);
5025+ __balance_callbacks(rq);
5026+ raw_spin_unlock_irq(&rq->lock);
5027+ }
45305028 }
45315029
45325030 void __noreturn do_task_dead(void)
@@ -4937,9 +5435,11 @@ void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task) · 404 unmodified lines
49375435 out_unlock:
49385436 /* Avoid rq from going away on us: */
49395437 preempt_disable();
4940− __task_rq_unlock(rq, &rf);
49415438
4942− balance_callback(rq);
5439+ rq_unpin_lock(rq, &rf);
5440+ __balance_callbacks(rq);
5441+ raw_spin_unlock(&rq->lock);
5442+
49435443 preempt_enable();
49445444 }
49455445 #else
@@ -5213,6 +5713,7 @@ static int __sched_setscheduler(struct task_struct *p, · 267 unmodified lines
52135713 int retval, oldprio, oldpolicy = -1, queued, running;
52145714 int new_effective_prio, policy = attr->sched_policy;
52155715 const struct sched_class *prev_class;
5716+ struct callback_head *head;
52165717 struct rq_flags rf;
52175718 int reset_on_fork;
52185719 int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
@@ -5451,6 +5952,7 @@ change: · 232 unmodified lines
54515952
54525953 /* Avoid rq from going away on us: */
54535954 preempt_disable();
5955+ head = splice_balance_callbacks(rq);
54545956 task_rq_unlock(rq, p, &rf);
54555957
54565958 if (pi) {
@@ -5459,7 +5961,7 @@ change: · 2 unmodified lines
54595961 }
54605962
54615963 /* Run balance callbacks after we've adjusted the PI chain: */
5462− balance_callback(rq);
5964+ balance_callbacks(rq, head);
54635965 preempt_enable();
54645966
54655967 return 0;
@@ -5954,7 +6456,7 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) · 488 unmodified lines
59546456 }
59556457 #endif
59566458 again:
5957− retval = __set_cpus_allowed_ptr(p, new_mask, true);
6459+ retval = __set_cpus_allowed_ptr(p, new_mask, SCA_CHECK);
59586460
59596461 if (!retval) {
59606462 cpuset_cpus_allowed(p, cpus_allowed);
@@ -6443,6 +6945,7 @@ void sched_show_task(struct task_struct *p) · 482 unmodified lines
64436945 (unsigned long)task_thread_info(p)->flags);
64446946
64456947 print_worker_info(KERN_INFO, p);
6948+ print_stop_info(KERN_INFO, p);
64466949 show_stack(p, NULL, KERN_INFO);
64476950 put_task_stack(p);
64486951 }
@@ -6533,7 +7036,7 @@ void init_idle(struct task_struct *idle, int cpu) · 84 unmodified lines
65337036 *
65347037 * And since this is boot we can forgo the serialization.
65357038 */
6536− set_cpus_allowed_common(idle, cpumask_of(cpu));
7039+ set_cpus_allowed_common(idle, cpumask_of(cpu), 0);
65377040 #endif
65387041 /*
65397042 * We're having a chicken and egg problem, even though we are
@@ -6684,119 +7187,126 @@ void idle_task_exit(void) · 144 unmodified lines
66847187 /* finish_cpu(), as ran on the BP, will clean up the active_mm state */
66857188 }
66867189
6687−/*
6688− * Since this CPU is going 'away' for a while, fold any nr_active delta
6689− * we might have. Assumes we're called after migrate_tasks() so that the
6690− * nr_active count is stable. We need to take the teardown thread which
6691− * is calling this into account, so we hand in adjust = 1 to the load
6692− * calculation.
6693− *
6694− * Also see the comment "Global load-average calculations".
6695− */
6696−static void calc_load_migrate(struct rq *rq)
7190+static int __balance_push_cpu_stop(void *arg)
66977191 {
6698− long delta = calc_load_fold_active(rq, 1);
6699− if (delta)
6700− atomic_long_add(delta, &calc_load_tasks);
6701−}
7192+ struct task_struct *p = arg;
7193+ struct rq *rq = this_rq();
7194+ struct rq_flags rf;
7195+ int cpu;
67027196
6703−static struct task_struct *__pick_migrate_task(struct rq *rq)
6704−{
6705− const struct sched_class *class;
6706− struct task_struct *next;
7197+ raw_spin_lock_irq(&p->pi_lock);
7198+ rq_lock(rq, &rf);
67077199
6708− for_each_class(class) {
6709− next = class->pick_next_task(rq);
6710− if (next) {
6711− next->sched_class->put_prev_task(rq, next);
6712− return next;
6713− }
7200+ update_rq_clock(rq);
7201+
7202+ if (task_rq(p) == rq && task_on_rq_queued(p)) {
7203+ cpu = select_fallback_rq(rq->cpu, p);
7204+ rq = __migrate_task(rq, &rf, p, cpu);
67147205 }
67157206
6716− /* The idle class should always have a runnable task */
6717− BUG();
7207+ rq_unlock(rq, &rf);
7208+ raw_spin_unlock_irq(&p->pi_lock);
7209+
7210+ put_task_struct(p);
7211+
7212+ return 0;
67187213 }
67197214
7215+static DEFINE_PER_CPU(struct cpu_stop_work, push_work);
7216+
67207217 /*
6721− * Migrate all tasks from the rq, sleeping tasks will be migrated by
6722− * try_to_wake_up()->select_task_rq().
6723− *
6724− * Called with rq->lock held even though we'er in stop_machine() and
6725− * there's no concurrency possible, we hold the required locks anyway
6726− * because of lock validation efforts.
7218+ * Ensure we only run per-cpu kthreads once the CPU goes !active.
67277219 */
6728−static void migrate_tasks(struct rq *dead_rq, struct rq_flags *rf)
7220+static void balance_push(struct rq *rq)
67297221 {
6730− struct rq *rq = dead_rq;
6731− struct task_struct *next, *stop = rq->stop;
6732− struct rq_flags orf = *rf;
6733− int dest_cpu;
7222+ struct task_struct *push_task = rq->curr;
7223+
7224+ lockdep_assert_held(&rq->lock);
7225+ SCHED_WARN_ON(rq->cpu != smp_processor_id());
67347226
67357227 /*
6736− * Fudge the rq selection such that the below task selection loop
6737− * doesn't get stuck on the currently eligible stop task.
6738− *
6739− * We're currently inside stop_machine() and the rq is either stuck
6740− * in the stop_machine_cpu_stop() loop, or we're executing this code,
6741− * either way we should never end up calling schedule() until we're
6742− * done here.
7228+ * Both the cpu-hotplug and stop task are in this case and are
7229+ * required to complete the hotplug process.
67437230 */
6744− rq->stop = NULL;
7231+ if (is_per_cpu_kthread(push_task) || is_migration_disabled(push_task)) {
7232+ /*
7233+ * If this is the idle task on the outgoing CPU try to wake
7234+ * up the hotplug control thread which might wait for the
7235+ * last task to vanish. The rcuwait_active() check is
7236+ * accurate here because the waiter is pinned on this CPU
7237+ * and can't obviously be running in parallel.
7238+ *
7239+ * On RT kernels this also has to check whether there are
7240+ * pinned and scheduled out tasks on the runqueue. They
7241+ * need to leave the migrate disabled section first.
7242+ */
7243+ if (!rq->nr_running && !rq_has_pinned_tasks(rq) &&
7244+ rcuwait_active(&rq->hotplug_wait)) {
7245+ raw_spin_unlock(&rq->lock);
7246+ rcuwait_wake_up(&rq->hotplug_wait);
7247+ raw_spin_lock(&rq->lock);
7248+ }
7249+ return;
7250+ }
67457251
7252+ get_task_struct(push_task);
67467253 /*
6747− * put_prev_task() and pick_next_task() sched
6748− * class method both need to have an up-to-date
6749− * value of rq->clock[_task]
7254+ * Temporarily drop rq->lock such that we can wake-up the stop task.
7255+ * Both preemption and IRQs are still disabled.
67507256 */
6751− update_rq_clock(rq);
7257+ raw_spin_unlock(&rq->lock);
7258+ stop_one_cpu_nowait(rq->cpu, __balance_push_cpu_stop, push_task,
7259+ this_cpu_ptr(&push_work));
7260+ /*
7261+ * At this point need_resched() is true and we'll take the loop in
7262+ * schedule(). The next pick is obviously going to be the stop task
7263+ * which is_per_cpu_kthread() and will push this task away.
7264+ */
7265+ raw_spin_lock(&rq->lock);
7266+}
67527267
6753− for (;;) {
6754− /*
6755− * There's this thread running, bail when that's the only
6756− * remaining thread:
6757− */
6758− if (rq->nr_running == 1)
6759− break;
7268+static void balance_push_set(int cpu, bool on)
7269+{
7270+ struct rq *rq = cpu_rq(cpu);
7271+ struct rq_flags rf;
67607272
6761− next = __pick_migrate_task(rq);
7273+ rq_lock_irqsave(rq, &rf);
7274+ if (on)
7275+ rq->balance_flags |= BALANCE_PUSH;
7276+ else
7277+ rq->balance_flags &= ~BALANCE_PUSH;
7278+ rq_unlock_irqrestore(rq, &rf);
7279+}
67627280
6763− /*
6764− * Rules for changing task_struct::cpus_mask are holding
6765− * both pi_lock and rq->lock, such that holding either
6766− * stabilizes the mask.
6767− *
6768− * Drop rq->lock is not quite as disastrous as it usually is
6769− * because !cpu_active at this point, which means load-balance
6770− * will not interfere. Also, stop-machine.
6771− */
6772− rq_unlock(rq, rf);
6773− raw_spin_lock(&next->pi_lock);
6774− rq_relock(rq, rf);
7281+/*
7282+ * Invoked from a CPUs hotplug control thread after the CPU has been marked
7283+ * inactive. All tasks which are not per CPU kernel threads are either
7284+ * pushed off this CPU now via balance_push() or placed on a different CPU
7285+ * during wakeup. Wait until the CPU is quiescent.
7286+ */
7287+static void balance_hotplug_wait(void)
7288+{
7289+ struct rq *rq = this_rq();
67757290
6776− /*
6777− * Since we're inside stop-machine, _nothing_ should have
6778− * changed the task, WARN if weird stuff happened, because in
6779− * that case the above rq->lock drop is a fail too.
6780− */
6781− if (WARN_ON(task_rq(next) != rq || !task_on_rq_queued(next))) {
6782− raw_spin_unlock(&next->pi_lock);
6783− continue;
6784− }
7291+ rcuwait_wait_event(&rq->hotplug_wait,
7292+ rq->nr_running == 1 && !rq_has_pinned_tasks(rq),
7293+ TASK_UNINTERRUPTIBLE);
7294+}
67857295
6786− /* Find suitable destination for @next, with force if needed. */
6787− dest_cpu = select_fallback_rq(dead_rq->cpu, next);
6788− rq = __migrate_task(rq, rf, next, dest_cpu);
6789− if (rq != dead_rq) {
6790− rq_unlock(rq, rf);
6791− rq = dead_rq;
6792− *rf = orf;
6793− rq_relock(rq, rf);
6794− }
6795− raw_spin_unlock(&next->pi_lock);
6796− }
7296+#else
67977297
6798− rq->stop = stop;
7298+static inline void balance_push(struct rq *rq)
7299+{
67997300 }
7301+
7302+static inline void balance_push_set(int cpu, bool on)
7303+{
7304+}
7305+
7306+static inline void balance_hotplug_wait(void)
7307+{
7308+}
7309+
68007310 #endif /* CONFIG_HOTPLUG_CPU */
68017311
68027312 void set_rq_online(struct rq *rq)
@@ -6882,6 +7392,8 @@ int sched_cpu_activate(unsigned int cpu) · 79 unmodified lines
68827392 struct rq *rq = cpu_rq(cpu);
68837393 struct rq_flags rf;
68847394
7395+ balance_push_set(cpu, false);
7396+
68857397 #ifdef CONFIG_SCHED_SMT
68867398 /*
68877399 * When going up, increment the number of cores with SMT present.
@@ -6917,6 +7429,8 @@ int sched_cpu_activate(unsigned int cpu) · 29 unmodified lines
69177429
69187430 int sched_cpu_deactivate(unsigned int cpu)
69197431 {
7432+ struct rq *rq = cpu_rq(cpu);
7433+ struct rq_flags rf;
69207434 int ret;
69217435
69227436 set_cpu_active(cpu, false);
@@ -6929,6 +7443,16 @@ int sched_cpu_deactivate(unsigned int cpu) · 6 unmodified lines
69297443 */
69307444 synchronize_rcu();
69317445
7446+ balance_push_set(cpu, true);
7447+
7448+ rq_lock_irqsave(rq, &rf);
7449+ if (rq->rd) {
7450+ update_rq_clock(rq);
7451+ BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
7452+ set_rq_offline(rq);
7453+ }
7454+ rq_unlock_irqrestore(rq, &rf);
7455+
69327456 #ifdef CONFIG_SCHED_SMT
69337457 /*
69347458 * When going down, decrement the number of cores with SMT present.
@@ -6942,6 +7466,7 @@ int sched_cpu_deactivate(unsigned int cpu) · 7 unmodified lines
69427466
69437467 ret = cpuset_cpu_inactive(cpu);
69447468 if (ret) {
7469+ balance_push_set(cpu, false);
69457470 set_cpu_active(cpu, true);
69467471 return ret;
69477472 }
@@ -6965,6 +7490,41 @@ int sched_cpu_starting(unsigned int cpu) · 17 unmodified lines
69657490 }
69667491
69677492 #ifdef CONFIG_HOTPLUG_CPU
7493+
7494+/*
7495+ * Invoked immediately before the stopper thread is invoked to bring the
7496+ * CPU down completely. At this point all per CPU kthreads except the
7497+ * hotplug thread (current) and the stopper thread (inactive) have been
7498+ * either parked or have been unbound from the outgoing CPU. Ensure that
7499+ * any of those which might be on the way out are gone.
7500+ *
7501+ * If after this point a bound task is being woken on this CPU then the
7502+ * responsible hotplug callback has failed to do it's job.
7503+ * sched_cpu_dying() will catch it with the appropriate fireworks.
7504+ */
7505+int sched_cpu_wait_empty(unsigned int cpu)
7506+{
7507+ balance_hotplug_wait();
7508+ return 0;
7509+}
7510+
7511+/*
7512+ * Since this CPU is going 'away' for a while, fold any nr_active delta we
7513+ * might have. Called from the CPU stopper task after ensuring that the
7514+ * stopper is the last running task on the CPU, so nr_active count is
7515+ * stable. We need to take the teardown thread which is calling this into
7516+ * account, so we hand in adjust = 1 to the load calculation.
7517+ *
7518+ * Also see the comment "Global load-average calculations".
7519+ */
7520+static void calc_load_migrate(struct rq *rq)
7521+{
7522+ long delta = calc_load_fold_active(rq, 1);
7523+
7524+ if (delta)
7525+ atomic_long_add(delta, &calc_load_tasks);
7526+}
7527+
69687528 int sched_cpu_dying(unsigned int cpu)
69697529 {
69707530 struct rq *rq = cpu_rq(cpu);
@@ -6974,12 +7534,7 @@ int sched_cpu_dying(unsigned int cpu) · 3 unmodified lines
69747534 sched_tick_stop(cpu);
69757535
69767536 rq_lock_irqsave(rq, &rf);
6977− if (rq->rd) {
6978− BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
6979− set_rq_offline(rq);
6980− }
6981− migrate_tasks(rq, &rf);
6982− BUG_ON(rq->nr_running != 1);
7537+ BUG_ON(rq->nr_running != 1 || rq_has_pinned_tasks(rq));
69837538 rq_unlock_irqrestore(rq, &rf);
69847539
69857540 calc_load_migrate(rq);
@@ -7186,6 +7741,9 @@ void __init sched_init(void) · 200 unmodified lines
71867741
71877742 rq_csd_init(rq, &rq->nohz_csd, nohz_csd_func);
71887743 #endif
7744+#ifdef CONFIG_HOTPLUG_CPU
7745+ rcuwait_init(&rq->hotplug_wait);
7746+#endif
71897747 #endif /* CONFIG_SMP */
71907748 hrtick_rq_init(rq);
71917749 atomic_set(&rq->nr_iowait, 0);
kernel/sched/cpudeadline.c+2−2
@@ -120,7 +120,7 @@ int cpudl_find(struct cpudl *cp, struct task_struct *p, · 119 unmodified lines
120120 const struct sched_dl_entity *dl_se = &p->dl;
121121
122122 if (later_mask &&
123− cpumask_and(later_mask, cp->free_cpus, p->cpus_ptr)) {
123+ cpumask_and(later_mask, cp->free_cpus, &p->cpus_mask)) {
124124 unsigned long cap, max_cap = 0;
125125 int cpu, max_cpu = -1;
126126
@@ -151,7 +151,7 @@ int cpudl_find(struct cpudl *cp, struct task_struct *p, · 24 unmodified lines
151151
152152 WARN_ON(best_cpu != -1 && !cpu_present(best_cpu));
153153
154− if (cpumask_test_cpu(best_cpu, p->cpus_ptr) &&
154+ if (cpumask_test_cpu(best_cpu, &p->cpus_mask) &&
155155 dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
156156 if (later_mask)
157157 cpumask_set_cpu(best_cpu, later_mask);
kernel/sched/cpupri.c+2−2
@@ -97,11 +97,11 @@ static inline int __cpupri_find(struct cpupri *cp, struct task_struct *p, · 96 unmodified lines
9797 if (skip)
9898 return 0;
9999
100− if (cpumask_any_and(p->cpus_ptr, vec->mask) >= nr_cpu_ids)
100+ if (cpumask_any_and(&p->cpus_mask, vec->mask) >= nr_cpu_ids)
101101 return 0;
102102
103103 if (lowest_mask) {
104− cpumask_and(lowest_mask, p->cpus_ptr, vec->mask);
104+ cpumask_and(lowest_mask, &p->cpus_mask, vec->mask);
105105
106106 /*
107107 * We have to ensure that we have at least one bit
kernel/sched/deadline.c+31−15
@@ -559,7 +559,7 @@ static int push_dl_task(struct rq *rq); · 558 unmodified lines
559559
560560 static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
561561 {
562− return dl_task(prev);
562+ return rq->online && dl_task(prev);
563563 }
564564
565565 static DEFINE_PER_CPU(struct callback_head, dl_push_head);
@@ -1931,7 +1931,7 @@ static void task_fork_dl(struct task_struct *p) · 1365 unmodified lines
19311931 static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
19321932 {
19331933 if (!task_running(rq, p) &&
1934− cpumask_test_cpu(cpu, p->cpus_ptr))
1934+ cpumask_test_cpu(cpu, &p->cpus_mask))
19351935 return 1;
19361936 return 0;
19371937 }
@@ -2021,8 +2021,8 @@ static int find_later_rq(struct task_struct *task) · 83 unmodified lines
20212021 return this_cpu;
20222022 }
20232023
2024− best_cpu = cpumask_first_and(later_mask,
2025− sched_domain_span(sd));
2024+ best_cpu = cpumask_any_and_distribute(later_mask,
2025+ sched_domain_span(sd));
20262026 /*
20272027 * Last chance: if a CPU being in both later_mask
20282028 * and current sd span is valid, that becomes our
@@ -2044,7 +2044,7 @@ static int find_later_rq(struct task_struct *task) · 15 unmodified lines
20442044 if (this_cpu != -1)
20452045 return this_cpu;
20462046
2047− cpu = cpumask_any(later_mask);
2047+ cpu = cpumask_any_distribute(later_mask);
20482048 if (cpu < nr_cpu_ids)
20492049 return cpu;
20502050
@@ -2081,7 +2081,7 @@ static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq) · 30 unmodified lines
20812081 /* Retry if something changed. */
20822082 if (double_lock_balance(rq, later_rq)) {
20832083 if (unlikely(task_rq(task) != rq ||
2084− !cpumask_test_cpu(later_rq->cpu, task->cpus_ptr) ||
2084+ !cpumask_test_cpu(later_rq->cpu, &task->cpus_mask) ||
20852085 task_running(rq, task) ||
20862086 !dl_task(task) ||
20872087 !task_on_rq_queued(task))) {
@@ -2148,6 +2148,9 @@ static int push_dl_task(struct rq *rq) · 60 unmodified lines
21482148 return 0;
21492149
21502150 retry:
2151+ if (is_migration_disabled(next_task))
2152+ return 0;
2153+
21512154 if (WARN_ON(next_task == rq->curr))
21522155 return 0;
21532156
@@ -2225,7 +2228,7 @@ static void push_dl_tasks(struct rq *rq) · 71 unmodified lines
22252228 static void pull_dl_task(struct rq *this_rq)
22262229 {
22272230 int this_cpu = this_rq->cpu, cpu;
2228− struct task_struct *p;
2231+ struct task_struct *p, *push_task;
22292232 bool resched = false;
22302233 struct rq *src_rq;
22312234 u64 dmin = LONG_MAX;
@@ -2255,6 +2258,7 @@ static void pull_dl_task(struct rq *this_rq) · 23 unmodified lines
22552258 continue;
22562259
22572260 /* Might drop this_rq->lock */
2261+ push_task = NULL;
22582262 double_lock_balance(this_rq, src_rq);
22592263
22602264 /*
@@ -2286,17 +2290,27 @@ static void pull_dl_task(struct rq *this_rq) · 25 unmodified lines
22862290 src_rq->curr->dl.deadline))
22872291 goto skip;
22882292
2289− resched = true;
2290−
2291− deactivate_task(src_rq, p, 0);
2292− set_task_cpu(p, this_cpu);
2293− activate_task(this_rq, p, 0);
2294− dmin = p->dl.deadline;
2293+ if (is_migration_disabled(p)) {
2294+ push_task = get_push_task(src_rq);
2295+ } else {
2296+ deactivate_task(src_rq, p, 0);
2297+ set_task_cpu(p, this_cpu);
2298+ activate_task(this_rq, p, 0);
2299+ dmin = p->dl.deadline;
2300+ resched = true;
2301+ }
22952302
22962303 /* Is there any other task even earlier? */
22972304 }
22982305 skip:
22992306 double_unlock_balance(this_rq, src_rq);
2307+
2308+ if (push_task) {
2309+ raw_spin_unlock(&this_rq->lock);
2310+ stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
2311+ push_task, &src_rq->push_work);
2312+ raw_spin_lock(&this_rq->lock);
2313+ }
23002314 }
23012315
23022316 if (resched)
@@ -2320,7 +2334,8 @@ static void task_woken_dl(struct rq *rq, struct task_struct *p) · 17 unmodified lines
23202334 }
23212335
23222336 static void set_cpus_allowed_dl(struct task_struct *p,
2323− const struct cpumask *new_mask)
2337+ const struct cpumask *new_mask,
2338+ u32 flags)
23242339 {
23252340 struct root_domain *src_rd;
23262341 struct rq *rq;
@@ -2349,7 +2364,7 @@ static void set_cpus_allowed_dl(struct task_struct *p, · 22 unmodified lines
23492364 raw_spin_unlock(&src_dl_b->lock);
23502365 }
23512366
2352− set_cpus_allowed_common(p, new_mask);
2367+ set_cpus_allowed_common(p, new_mask, flags);
23532368 }
23542369
23552370 /* Assumes rq->lock is held */
@@ -2542,6 +2557,7 @@ DEFINE_SCHED_CLASS(dl) = { · 186 unmodified lines
25422557 .rq_online = rq_online_dl,
25432558 .rq_offline = rq_offline_dl,
25442559 .task_woken = task_woken_dl,
2560+ .find_lock_rq = find_lock_later_rq,
25452561 #endif
25462562
25472563 .task_tick = task_tick_dl,
kernel/sched/rt.c+57−18
@@ -265,7 +265,7 @@ static void pull_rt_task(struct rq *this_rq); · 264 unmodified lines
265265 static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
266266 {
267267 /* Try to pull RT tasks here if we lower this rq's prio */
268− return rq->rt.highest_prio.curr > prev->prio;
268+ return rq->online && rq->rt.highest_prio.curr > prev->prio;
269269 }
270270
271271 static inline int rt_overloaded(struct rq *rq)
@@ -1660,7 +1660,7 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p) · 1388 unmodified lines
16601660 static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
16611661 {
16621662 if (!task_running(rq, p) &&
1663− cpumask_test_cpu(cpu, p->cpus_ptr))
1663+ cpumask_test_cpu(cpu, &p->cpus_mask))
16641664 return 1;
16651665
16661666 return 0;
@@ -1754,8 +1754,8 @@ static int find_lowest_rq(struct task_struct *task) · 87 unmodified lines
17541754 return this_cpu;
17551755 }
17561756
1757− best_cpu = cpumask_first_and(lowest_mask,
1758− sched_domain_span(sd));
1757+ best_cpu = cpumask_any_and_distribute(lowest_mask,
1758+ sched_domain_span(sd));
17591759 if (best_cpu < nr_cpu_ids) {
17601760 rcu_read_unlock();
17611761 return best_cpu;
@@ -1772,7 +1772,7 @@ static int find_lowest_rq(struct task_struct *task) · 10 unmodified lines
17721772 if (this_cpu != -1)
17731773 return this_cpu;
17741774
1775− cpu = cpumask_any(lowest_mask);
1775+ cpu = cpumask_any_distribute(lowest_mask);
17761776 if (cpu < nr_cpu_ids)
17771777 return cpu;
17781778
@@ -1813,7 +1813,7 @@ static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq) · 34 unmodified lines
18131813 * Also make sure that it wasn't scheduled on its rq.
18141814 */
18151815 if (unlikely(task_rq(task) != rq ||
1816− !cpumask_test_cpu(lowest_rq->cpu, task->cpus_ptr) ||
1816+ !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_mask) ||
18171817 task_running(rq, task) ||
18181818 !rt_task(task) ||
18191819 !task_on_rq_queued(task))) {
@@ -1861,7 +1861,7 @@ static struct task_struct *pick_next_pushable_task(struct rq *rq) · 41 unmodified lines
18611861 * running task can migrate over to a CPU that is running a task
18621862 * of lesser priority.
18631863 */
1864−static int push_rt_task(struct rq *rq)
1864+static int push_rt_task(struct rq *rq, bool pull)
18651865 {
18661866 struct task_struct *next_task;
18671867 struct rq *lowest_rq;
@@ -1875,6 +1875,34 @@ static int push_rt_task(struct rq *rq) · 7 unmodified lines
18751875 return 0;
18761876
18771877 retry:
1878+ if (is_migration_disabled(next_task)) {
1879+ struct task_struct *push_task = NULL;
1880+ int cpu;
1881+
1882+ if (!pull || rq->push_busy)
1883+ return 0;
1884+
1885+ cpu = find_lowest_rq(rq->curr);
1886+ if (cpu == -1 || cpu == rq->cpu)
1887+ return 0;
1888+
1889+ /*
1890+ * Given we found a CPU with lower priority than @next_task,
1891+ * therefore it should be running. However we cannot migrate it
1892+ * to this other CPU, instead attempt to push the current
1893+ * running task on this CPU away.
1894+ */
1895+ push_task = get_push_task(rq);
1896+ if (push_task) {
1897+ raw_spin_unlock(&rq->lock);
1898+ stop_one_cpu_nowait(rq->cpu, push_cpu_stop,
1899+ push_task, &rq->push_work);
1900+ raw_spin_lock(&rq->lock);
1901+ }
1902+
1903+ return 0;
1904+ }
1905+
18781906 if (WARN_ON(next_task == rq->curr))
18791907 return 0;
18801908
@@ -1929,12 +1957,10 @@ retry: · 48 unmodified lines
19291957 deactivate_task(rq, next_task, 0);
19301958 set_task_cpu(next_task, lowest_rq->cpu);
19311959 activate_task(lowest_rq, next_task, 0);
1932− ret = 1;
1933−
19341960 resched_curr(lowest_rq);
1961+ ret = 1;
19351962
19361963 double_unlock_balance(rq, lowest_rq);
1937−
19381964 out:
19391965 put_task_struct(next_task);
19401966
@@ -1944,7 +1970,7 @@ out: · 3 unmodified lines
19441970 static void push_rt_tasks(struct rq *rq)
19451971 {
19461972 /* push_rt_task will return true if it moved an RT */
1947− while (push_rt_task(rq))
1973+ while (push_rt_task(rq, false))
19481974 ;
19491975 }
19501976
@@ -2097,7 +2123,8 @@ void rto_push_irq_work_func(struct irq_work *work) · 146 unmodified lines
20972123 */
20982124 if (has_pushable_tasks(rq)) {
20992125 raw_spin_lock(&rq->lock);
2100− push_rt_tasks(rq);
2126+ while (push_rt_task(rq, true))
2127+ ;
21012128 raw_spin_unlock(&rq->lock);
21022129 }
21032130
@@ -2122,7 +2149,7 @@ static void pull_rt_task(struct rq *this_rq) · 18 unmodified lines
21222149 {
21232150 int this_cpu = this_rq->cpu, cpu;
21242151 bool resched = false;
2125− struct task_struct *p;
2152+ struct task_struct *p, *push_task;
21262153 struct rq *src_rq;
21272154 int rt_overload_count = rt_overloaded(this_rq);
21282155
@@ -2169,6 +2196,7 @@ static void pull_rt_task(struct rq *this_rq) · 40 unmodified lines
21692196 * double_lock_balance, and another CPU could
21702197 * alter this_rq
21712198 */
2199+ push_task = NULL;
21722200 double_lock_balance(this_rq, src_rq);
21732201
21742202 /*
@@ -2196,11 +2224,14 @@ static void pull_rt_task(struct rq *this_rq) · 21 unmodified lines
21962224 if (p->prio < src_rq->curr->prio)
21972225 goto skip;
21982226
2199− resched = true;
2200−
2201− deactivate_task(src_rq, p, 0);
2202− set_task_cpu(p, this_cpu);
2203− activate_task(this_rq, p, 0);
2227+ if (is_migration_disabled(p)) {
2228+ push_task = get_push_task(src_rq);
2229+ } else {
2230+ deactivate_task(src_rq, p, 0);
2231+ set_task_cpu(p, this_cpu);
2232+ activate_task(this_rq, p, 0);
2233+ resched = true;
2234+ }
22042235 /*
22052236 * We continue with the search, just in
22062237 * case there's an even higher prio task
@@ -2210,6 +2241,13 @@ static void pull_rt_task(struct rq *this_rq) · 3 unmodified lines
22102241 }
22112242 skip:
22122243 double_unlock_balance(this_rq, src_rq);
2244+
2245+ if (push_task) {
2246+ raw_spin_unlock(&this_rq->lock);
2247+ stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
2248+ push_task, &src_rq->push_work);
2249+ raw_spin_lock(&this_rq->lock);
2250+ }
22132251 }
22142252
22152253 if (resched)
@@ -2451,6 +2489,7 @@ DEFINE_SCHED_CLASS(rt) = { · 235 unmodified lines
24512489 .rq_offline = rq_offline_rt,
24522490 .task_woken = task_woken_rt,
24532491 .switched_from = switched_from_rt,
2492+ .find_lock_rq = find_lock_lowest_rq,
24542493 #endif
24552494
24562495 .task_tick = task_tick_rt,
kernel/sched/sched.h+56−3
@@ -975,6 +975,7 @@ struct rq { · 974 unmodified lines
975975 unsigned long cpu_capacity_orig;
976976
977977 struct callback_head *balance_callback;
978+ unsigned char balance_flags;
978979
979980 unsigned char nohz_idle_balance;
980981 unsigned char idle_balance;
@@ -1005,6 +1006,10 @@ struct rq { · 24 unmodified lines
10051006
10061007 /* This is used to determine avg_idle's max value */
10071008 u64 max_idle_balance_cost;
1009+
1010+#ifdef CONFIG_HOTPLUG_CPU
1011+ struct rcuwait hotplug_wait;
1012+#endif
10081013 #endif /* CONFIG_SMP */
10091014
10101015 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
@@ -1050,6 +1055,12 @@ struct rq { · 39 unmodified lines
10501055 /* Must be inspected within a rcu lock section */
10511056 struct cpuidle_state *idle_state;
10521057 #endif
1058+
1059+#if defined(CONFIG_PREEMPT_RT) && defined(CONFIG_SMP)
1060+ unsigned int nr_pinned;
1061+#endif
1062+ unsigned int push_busy;
1063+ struct cpu_stop_work push_work;
10531064 };
10541065
10551066 #ifdef CONFIG_FAIR_GROUP_SCHED
@@ -1077,6 +1088,16 @@ static inline int cpu_of(struct rq *rq) · 21 unmodified lines
10771088 #endif
10781089 }
10791090
1091+#define MDF_PUSH 0x01
1092+
1093+static inline bool is_migration_disabled(struct task_struct *p)
1094+{
1095+#if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT_RT)
1096+ return p->migration_disabled;
1097+#else
1098+ return false;
1099+#endif
1100+}
10801101
10811102 #ifdef CONFIG_SCHED_SMT
10821103 extern void __update_idle_core(struct rq *rq);
@@ -1223,6 +1244,9 @@ static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf) · 140 unmodified lines
12231244 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
12241245 rf->clock_update_flags = 0;
12251246 #endif
1247+#ifdef CONFIG_SMP
1248+ SCHED_WARN_ON(rq->balance_callback);
1249+#endif
12261250 }
12271251
12281252 static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
@@ -1384,6 +1408,9 @@ init_numa_balancing(unsigned long clone_flags, struct task_struct *p) · 155 unmodified lines
13841408
13851409 #ifdef CONFIG_SMP
13861410
1411+#define BALANCE_WORK 0x01
1412+#define BALANCE_PUSH 0x02
1413+
13871414 static inline void
13881415 queue_balance_callback(struct rq *rq,
13891416 struct callback_head *head,
@@ -1391,12 +1418,13 @@ queue_balance_callback(struct rq *rq, · 1 unmodified line
13911418 {
13921419 lockdep_assert_held(&rq->lock);
13931420
1394− if (unlikely(head->next))
1421+ if (unlikely(head->next || (rq->balance_flags & BALANCE_PUSH)))
13951422 return;
13961423
13971424 head->func = (void (*)(struct callback_head *))func;
13981425 head->next = rq->balance_callback;
13991426 rq->balance_callback = head;
1427+ rq->balance_flags |= BALANCE_WORK;
14001428 }
14011429
14021430 #define rcu_dereference_check_sched_domain(p) \
@@ -1804,10 +1832,13 @@ struct sched_class { · 401 unmodified lines
18041832 void (*task_woken)(struct rq *this_rq, struct task_struct *task);
18051833
18061834 void (*set_cpus_allowed)(struct task_struct *p,
1807− const struct cpumask *newmask);
1835+ const struct cpumask *newmask,
1836+ u32 flags);
18081837
18091838 void (*rq_online)(struct rq *rq);
18101839 void (*rq_offline)(struct rq *rq);
1840+
1841+ struct rq *(*find_lock_rq)(struct task_struct *p, struct rq *rq);
18111842 #endif
18121843
18131844 void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
@@ -1905,13 +1936,35 @@ static inline bool sched_fair_runnable(struct rq *rq) · 91 unmodified lines
19051936 extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
19061937 extern struct task_struct *pick_next_task_idle(struct rq *rq);
19071938
1939+#define SCA_CHECK 0x01
1940+#define SCA_MIGRATE_DISABLE 0x02
1941+#define SCA_MIGRATE_ENABLE 0x04
1942+
19081943 #ifdef CONFIG_SMP
19091944
19101945 extern void update_group_capacity(struct sched_domain *sd, int cpu);
19111946
19121947 extern void trigger_load_balance(struct rq *rq);
19131948
1914−extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1949+extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask, u32 flags);
1950+
1951+static inline struct task_struct *get_push_task(struct rq *rq)
1952+{
1953+ struct task_struct *p = rq->curr;
1954+
1955+ lockdep_assert_held(&rq->lock);
1956+
1957+ if (rq->push_busy)
1958+ return NULL;
1959+
1960+ if (p->nr_cpus_allowed == 1)
1961+ return NULL;
1962+
1963+ rq->push_busy = true;
1964+ return get_task_struct(p);
1965+}
1966+
1967+extern int push_cpu_stop(void *arg);
19151968
19161969 #endif
19171970
kernel/stop_machine.c+24−3
@@ -42,11 +42,27 @@ struct cpu_stopper { · 41 unmodified lines
4242 struct list_head works; /* list of pending works */
4343
4444 struct cpu_stop_work stop_work; /* for stop_cpus */
45+ unsigned long caller;
46+ cpu_stop_fn_t fn;
4547 };
4648
4749 static DEFINE_PER_CPU(struct cpu_stopper, cpu_stopper);
4850 static bool stop_machine_initialized = false;
4951
52+void print_stop_info(const char *log_lvl, struct task_struct *task)
53+{
54+ /*
55+ * If @task is a stopper task, it cannot migrate and task_cpu() is
56+ * stable.
57+ */
58+ struct cpu_stopper *stopper = per_cpu_ptr(&cpu_stopper, task_cpu(task));
59+
60+ if (task != stopper->thread)
61+ return;
62+
63+ printk("%sStopper: %pS <- %pS\n", log_lvl, stopper->fn, (void *)stopper->caller);
64+}
65+
5066 /* static data for stop_cpus */
5167 static DEFINE_MUTEX(stop_cpus_mutex);
5268 static bool stop_cpus_in_progress;
@@ -123,7 +139,7 @@ static bool cpu_stop_queue_work(unsigned int cpu, struct cpu_stop_work *work) · 70 unmodified lines
123139 int stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg)
124140 {
125141 struct cpu_stop_done done;
126− struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done };
142+ struct cpu_stop_work work = { .fn = fn, .arg = arg, .done = &done, .caller = _RET_IP_ };
127143
128144 cpu_stop_init_done(&done, 1);
129145 if (!cpu_stop_queue_work(cpu, &work))
@@ -331,7 +347,8 @@ int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void * · 201 unmodified lines
331347 work1 = work2 = (struct cpu_stop_work){
332348 .fn = multi_cpu_stop,
333349 .arg = &msdata,
334− .done = &done
350+ .done = &done,
351+ .caller = _RET_IP_,
335352 };
336353
337354 cpu_stop_init_done(&done, 2);
@@ -367,7 +384,7 @@ int stop_two_cpus(unsigned int cpu1, unsigned int cpu2, cpu_stop_fn_t fn, void * · 29 unmodified lines
367384 bool stop_one_cpu_nowait(unsigned int cpu, cpu_stop_fn_t fn, void *arg,
368385 struct cpu_stop_work *work_buf)
369386 {
370− *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, };
387+ *work_buf = (struct cpu_stop_work){ .fn = fn, .arg = arg, .caller = _RET_IP_, };
371388 return cpu_stop_queue_work(cpu, work_buf);
372389 }
373390
@@ -487,6 +504,8 @@ repeat: · 113 unmodified lines
487504 int ret;
488505
489506 /* cpu stop callbacks must not sleep, make in_atomic() == T */
507+ stopper->caller = work->caller;
508+ stopper->fn = fn;
490509 preempt_count_inc();
491510 ret = fn(arg);
492511 if (done) {
@@ -495,6 +514,8 @@ repeat: · 2 unmodified lines
495514 cpu_stop_signal_done(done);
496515 }
497516 preempt_count_dec();
517+ stopper->fn = NULL;
518+ stopper->caller = 0;
498519 WARN_ONCE(preempt_count(),
499520 "cpu_stop: %ps(%p) leaked preempt count\n", fn, arg);
500521 goto repeat;
kernel/workqueue.c+4−0
@@ -4908,6 +4908,10 @@ static void unbind_workers(int cpu) · 4907 unmodified lines
49084908 pool->flags |= POOL_DISASSOCIATED;
49094909
49104910 raw_spin_unlock_irq(&pool->lock);
4911+
4912+ for_each_pool_worker(worker, pool)
4913+ WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_active_mask) < 0);
4914+
49114915 mutex_unlock(&wq_pool_attach_mutex);
49124916
49134917 /*

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