blob: b3cd5553ecbb9e481a247e2728bfa332cd6edd41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Peter Zijlstra391e43d2011-11-15 17:14:39 +01002 * kernel/sched/core.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
Mark Rutlande1b77c92016-03-09 14:08:18 -080029#include <linux/kasan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mm.h>
31#include <linux/module.h>
32#include <linux/nmi.h>
33#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020034#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/highmem.h>
Andy Lutomirskif98db602016-04-26 09:39:06 -070036#include <linux/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/security.h>
44#include <linux/notifier.h>
45#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080046#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080047#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/blkdev.h>
49#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070050#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/smp.h>
52#include <linux/threads.h>
53#include <linux/timer.h>
54#include <linux/rcupdate.h>
55#include <linux/cpu.h>
56#include <linux/cpuset.h>
57#include <linux/percpu.h>
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +040058#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020060#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/syscalls.h>
62#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070063#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080064#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070065#include <linux/delayacct.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020066#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020067#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010068#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070069#include <linux/tick.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020070#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020071#include <linux/ftrace.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090072#include <linux/slab.h>
Carsten Emdef1c6f1a2011-10-26 23:14:16 +020073#include <linux/init_task.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010074#include <linux/context_tracking.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070075#include <linux/compiler.h>
Josh Poimboeuf8e05e962016-02-28 22:22:38 -060076#include <linux/frame.h>
Giovanni Gherdovich60756202016-08-05 10:21:56 +020077#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
David Howells96f951e2012-03-28 18:30:03 +010079#include <asm/switch_to.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070080#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020081#include <asm/irq_regs.h>
Christian Borntraegerdb7e5272012-01-11 08:58:16 +010082#include <asm/mutex.h>
Glauber Costae6e66852011-07-11 15:28:17 -040083#ifdef CONFIG_PARAVIRT
84#include <asm/paravirt.h>
85#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Peter Zijlstra029632f2011-10-25 10:00:11 +020087#include "sched.h"
Tejun Heoea138442013-01-18 14:05:55 -080088#include "../workqueue_internal.h"
Thomas Gleixner29d5e042012-04-20 13:05:45 +000089#include "../smpboot.h"
Gregory Haskins6e0534f2008-05-12 21:21:01 +020090
Steven Rostedta8d154b2009-04-10 09:36:00 -040091#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040092#include <trace/events/sched.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040093
Peter Zijlstra029632f2011-10-25 10:00:11 +020094DEFINE_MUTEX(sched_domains_mutex);
95DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +020096
Peter Zijlstrafe44d622010-12-09 14:15:34 +010097static void update_rq_clock_task(struct rq *rq, s64 delta);
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -070098
Peter Zijlstra029632f2011-10-25 10:00:11 +020099void update_rq_clock(struct rq *rq)
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200100{
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100101 s64 delta;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700102
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100103 lockdep_assert_held(&rq->lock);
104
105 if (rq->clock_skip_update & RQCF_ACT_SKIP)
Mike Galbraithf26f9af2010-12-08 11:05:42 +0100106 return;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700107
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100108 delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
Mike Galbraith4036ac12014-06-24 07:49:40 +0200109 if (delta < 0)
110 return;
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100111 rq->clock += delta;
112 update_rq_clock_task(rq, delta);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200113}
114
Ingo Molnare436d802007-07-19 21:28:35 +0200115/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200116 * Debugging: various feature bits
117 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200118
119#define SCHED_FEAT(name, enabled) \
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200120 (1UL << __SCHED_FEAT_##name) * enabled |
121
122const_debug unsigned int sysctl_sched_features =
Peter Zijlstra391e43d2011-11-15 17:14:39 +0100123#include "features.h"
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200124 0;
125
126#undef SCHED_FEAT
127
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200128/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100129 * Number of tasks to iterate in a single balance run.
130 * Limited because this is done with IRQs disabled.
131 */
132const_debug unsigned int sysctl_sched_nr_migrate = 32;
133
134/*
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200135 * period over which we average the RT time consumption, measured
136 * in ms.
137 *
138 * default: 1s
139 */
140const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
141
142/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100143 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100144 * default: 1s
145 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100146unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100147
Peter Zijlstra029632f2011-10-25 10:00:11 +0200148__read_mostly int scheduler_running;
Ingo Molnar6892b752008-02-13 14:02:36 +0100149
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100150/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100151 * part of the period that we allow rt tasks to run in us.
152 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100153 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100154int sysctl_sched_rt_runtime = 950000;
155
Rik van Riel3fa08182015-03-09 12:12:07 -0400156/* cpus with isolated domains */
157cpumask_var_t cpu_isolated_map;
158
Dario Faggioli332ac172013-11-07 14:43:45 +0100159/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800160 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200162static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 __acquires(rq->lock)
164{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700165 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 local_irq_disable();
168 rq = this_rq();
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100169 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 return rq;
172}
173
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200174/*
175 * __task_rq_lock - lock the rq @p resides on.
176 */
Peter Zijlstraeb580752015-07-31 21:28:18 +0200177struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200178 __acquires(rq->lock)
179{
180 struct rq *rq;
181
182 lockdep_assert_held(&p->pi_lock);
183
184 for (;;) {
185 rq = task_rq(p);
186 raw_spin_lock(&rq->lock);
187 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +0200188 rf->cookie = lockdep_pin_lock(&rq->lock);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200189 return rq;
190 }
191 raw_spin_unlock(&rq->lock);
192
193 while (unlikely(task_on_rq_migrating(p)))
194 cpu_relax();
195 }
196}
197
198/*
199 * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
200 */
Peter Zijlstraeb580752015-07-31 21:28:18 +0200201struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200202 __acquires(p->pi_lock)
203 __acquires(rq->lock)
204{
205 struct rq *rq;
206
207 for (;;) {
Peter Zijlstraeb580752015-07-31 21:28:18 +0200208 raw_spin_lock_irqsave(&p->pi_lock, rf->flags);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200209 rq = task_rq(p);
210 raw_spin_lock(&rq->lock);
211 /*
212 * move_queued_task() task_rq_lock()
213 *
214 * ACQUIRE (rq->lock)
215 * [S] ->on_rq = MIGRATING [L] rq = task_rq()
216 * WMB (__set_task_cpu()) ACQUIRE (rq->lock);
217 * [S] ->cpu = new_cpu [L] task_rq()
218 * [L] ->on_rq
219 * RELEASE (rq->lock)
220 *
221 * If we observe the old cpu in task_rq_lock, the acquire of
222 * the old rq->lock will fully serialize against the stores.
223 *
224 * If we observe the new cpu in task_rq_lock, the acquire will
225 * pair with the WMB to ensure we must then also see migrating.
226 */
227 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +0200228 rf->cookie = lockdep_pin_lock(&rq->lock);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200229 return rq;
230 }
231 raw_spin_unlock(&rq->lock);
Peter Zijlstraeb580752015-07-31 21:28:18 +0200232 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200233
234 while (unlikely(task_on_rq_migrating(p)))
235 cpu_relax();
236 }
237}
238
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100239#ifdef CONFIG_SCHED_HRTICK
240/*
241 * Use HR-timers to deliver accurate preemption points.
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100242 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100243
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100244static void hrtick_clear(struct rq *rq)
245{
246 if (hrtimer_active(&rq->hrtick_timer))
247 hrtimer_cancel(&rq->hrtick_timer);
248}
249
250/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100251 * High-resolution timer tick.
252 * Runs from hardirq context with interrupts disabled.
253 */
254static enum hrtimer_restart hrtick(struct hrtimer *timer)
255{
256 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
257
258 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
259
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100260 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200261 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100262 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100263 raw_spin_unlock(&rq->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100264
265 return HRTIMER_NORESTART;
266}
267
Rabin Vincent95e904c2008-05-11 05:55:33 +0530268#ifdef CONFIG_SMP
Peter Zijlstra971ee282013-06-28 11:18:53 +0200269
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000270static void __hrtick_restart(struct rq *rq)
Peter Zijlstra971ee282013-06-28 11:18:53 +0200271{
272 struct hrtimer *timer = &rq->hrtick_timer;
Peter Zijlstra971ee282013-06-28 11:18:53 +0200273
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000274 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200275}
276
Peter Zijlstra31656512008-07-18 18:01:23 +0200277/*
278 * called from hardirq (IPI) context
279 */
280static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200281{
Peter Zijlstra31656512008-07-18 18:01:23 +0200282 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200283
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100284 raw_spin_lock(&rq->lock);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200285 __hrtick_restart(rq);
Peter Zijlstra31656512008-07-18 18:01:23 +0200286 rq->hrtick_csd_pending = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100287 raw_spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200288}
289
Peter Zijlstra31656512008-07-18 18:01:23 +0200290/*
291 * Called to set the hrtick timer state.
292 *
293 * called with rq->lock held and irqs disabled
294 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200295void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200296{
Peter Zijlstra31656512008-07-18 18:01:23 +0200297 struct hrtimer *timer = &rq->hrtick_timer;
xiaofeng.yan177ef2a2014-08-26 03:15:41 +0000298 ktime_t time;
299 s64 delta;
300
301 /*
302 * Don't schedule slices shorter than 10000ns, that just
303 * doesn't make sense and can cause timer DoS.
304 */
305 delta = max_t(s64, delay, 10000LL);
306 time = ktime_add_ns(timer->base->get_time(), delta);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200307
Arjan van de Vencc584b22008-09-01 15:02:30 -0700308 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +0200309
310 if (rq == this_rq()) {
Peter Zijlstra971ee282013-06-28 11:18:53 +0200311 __hrtick_restart(rq);
Peter Zijlstra31656512008-07-18 18:01:23 +0200312 } else if (!rq->hrtick_csd_pending) {
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100313 smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
Peter Zijlstra31656512008-07-18 18:01:23 +0200314 rq->hrtick_csd_pending = 1;
315 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200316}
317
Peter Zijlstra31656512008-07-18 18:01:23 +0200318#else
319/*
320 * Called to set the hrtick timer state.
321 *
322 * called with rq->lock held and irqs disabled
323 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200324void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstra31656512008-07-18 18:01:23 +0200325{
Wanpeng Li86893332014-11-26 08:44:06 +0800326 /*
327 * Don't schedule slices shorter than 10000ns, that just
328 * doesn't make sense. Rely on vruntime for fairness.
329 */
330 delay = max_t(u64, delay, 10000LL);
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000331 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
332 HRTIMER_MODE_REL_PINNED);
Peter Zijlstra31656512008-07-18 18:01:23 +0200333}
Rabin Vincent95e904c2008-05-11 05:55:33 +0530334#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200335
336static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100337{
Peter Zijlstra31656512008-07-18 18:01:23 +0200338#ifdef CONFIG_SMP
339 rq->hrtick_csd_pending = 0;
340
341 rq->hrtick_csd.flags = 0;
342 rq->hrtick_csd.func = __hrtick_start;
343 rq->hrtick_csd.info = rq;
344#endif
345
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100346 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
347 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100348}
Andrew Morton006c75f2008-09-22 14:55:46 -0700349#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100350static inline void hrtick_clear(struct rq *rq)
351{
352}
353
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100354static inline void init_rq_hrtick(struct rq *rq)
355{
356}
Andrew Morton006c75f2008-09-22 14:55:46 -0700357#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100358
Frederic Weisbecker55295782016-03-24 15:38:01 +0100359/*
360 * cmpxchg based fetch_or, macro so it works for different integer types
361 */
362#define fetch_or(ptr, mask) \
363 ({ \
364 typeof(ptr) _ptr = (ptr); \
365 typeof(mask) _mask = (mask); \
366 typeof(*_ptr) _old, _val = *_ptr; \
367 \
368 for (;;) { \
369 _old = cmpxchg(_ptr, _val, _val | _mask); \
370 if (_old == _val) \
371 break; \
372 _val = _old; \
373 } \
374 _old; \
375})
376
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700377#if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200378/*
379 * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
380 * this avoids any races wrt polling state changes and thereby avoids
381 * spurious IPIs.
382 */
383static bool set_nr_and_not_polling(struct task_struct *p)
384{
385 struct thread_info *ti = task_thread_info(p);
386 return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
387}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700388
389/*
390 * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
391 *
392 * If this returns true, then the idle task promises to call
393 * sched_ttwu_pending() and reschedule soon.
394 */
395static bool set_nr_if_polling(struct task_struct *p)
396{
397 struct thread_info *ti = task_thread_info(p);
Jason Low316c1608d2015-04-28 13:00:20 -0700398 typeof(ti->flags) old, val = READ_ONCE(ti->flags);
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700399
400 for (;;) {
401 if (!(val & _TIF_POLLING_NRFLAG))
402 return false;
403 if (val & _TIF_NEED_RESCHED)
404 return true;
405 old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED);
406 if (old == val)
407 break;
408 val = old;
409 }
410 return true;
411}
412
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200413#else
414static bool set_nr_and_not_polling(struct task_struct *p)
415{
416 set_tsk_need_resched(p);
417 return true;
418}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700419
420#ifdef CONFIG_SMP
421static bool set_nr_if_polling(struct task_struct *p)
422{
423 return false;
424}
425#endif
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200426#endif
427
Peter Zijlstra76751042015-05-01 08:27:50 -0700428void wake_q_add(struct wake_q_head *head, struct task_struct *task)
429{
430 struct wake_q_node *node = &task->wake_q;
431
432 /*
433 * Atomically grab the task, if ->wake_q is !nil already it means
434 * its already queued (either by us or someone else) and will get the
435 * wakeup due to that.
436 *
437 * This cmpxchg() implies a full barrier, which pairs with the write
Davidlohr Bueso58fe9c42016-05-08 20:58:10 -0700438 * barrier implied by the wakeup in wake_up_q().
Peter Zijlstra76751042015-05-01 08:27:50 -0700439 */
440 if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL))
441 return;
442
443 get_task_struct(task);
444
445 /*
446 * The head is context local, there can be no concurrency.
447 */
448 *head->lastp = node;
449 head->lastp = &node->next;
450}
451
452void wake_up_q(struct wake_q_head *head)
453{
454 struct wake_q_node *node = head->first;
455
456 while (node != WAKE_Q_TAIL) {
457 struct task_struct *task;
458
459 task = container_of(node, struct task_struct, wake_q);
460 BUG_ON(!task);
461 /* task can safely be re-inserted now */
462 node = node->next;
463 task->wake_q.next = NULL;
464
465 /*
466 * wake_up_process() implies a wmb() to pair with the queueing
467 * in wake_q_add() so as not to miss wakeups.
468 */
469 wake_up_process(task);
470 put_task_struct(task);
471 }
472}
473
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200474/*
Kirill Tkhai88751252014-06-29 00:03:57 +0400475 * resched_curr - mark rq's current task 'to be rescheduled now'.
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200476 *
477 * On UP this means the setting of the need_resched flag, on SMP it
478 * might also involve a cross-CPU call to trigger the scheduler on
479 * the target CPU.
480 */
Kirill Tkhai88751252014-06-29 00:03:57 +0400481void resched_curr(struct rq *rq)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200482{
Kirill Tkhai88751252014-06-29 00:03:57 +0400483 struct task_struct *curr = rq->curr;
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200484 int cpu;
485
Kirill Tkhai88751252014-06-29 00:03:57 +0400486 lockdep_assert_held(&rq->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200487
Kirill Tkhai88751252014-06-29 00:03:57 +0400488 if (test_tsk_need_resched(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200489 return;
490
Kirill Tkhai88751252014-06-29 00:03:57 +0400491 cpu = cpu_of(rq);
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200492
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200493 if (cpu == smp_processor_id()) {
Kirill Tkhai88751252014-06-29 00:03:57 +0400494 set_tsk_need_resched(curr);
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200495 set_preempt_need_resched();
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200496 return;
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200497 }
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200498
Kirill Tkhai88751252014-06-29 00:03:57 +0400499 if (set_nr_and_not_polling(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200500 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700501 else
502 trace_sched_wake_idle_without_ipi(cpu);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200503}
504
Peter Zijlstra029632f2011-10-25 10:00:11 +0200505void resched_cpu(int cpu)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200506{
507 struct rq *rq = cpu_rq(cpu);
508 unsigned long flags;
509
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100510 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200511 return;
Kirill Tkhai88751252014-06-29 00:03:57 +0400512 resched_curr(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100513 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200514}
Thomas Gleixner06d83082008-03-22 09:20:24 +0100515
Peter Zijlstrab021fe32013-09-17 09:30:55 +0200516#ifdef CONFIG_SMP
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200517#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixner06d83082008-03-22 09:20:24 +0100518/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700519 * In the semi idle case, use the nearest busy cpu for migrating timers
520 * from an idle cpu. This is good for power-savings.
521 *
522 * We don't do similar optimization for completely idle system, as
523 * selecting an idle cpu will add more delays to the timers than intended
524 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
525 */
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000526int get_nohz_timer_target(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700527{
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000528 int i, cpu = smp_processor_id();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700529 struct sched_domain *sd;
530
Vatika Harlalka9642d182015-09-01 16:50:59 +0200531 if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530532 return cpu;
533
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200534 rcu_read_lock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700535 for_each_domain(cpu, sd) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200536 for_each_cpu(i, sched_domain_span(sd)) {
Wanpeng Li44496922016-05-04 14:45:34 +0800537 if (cpu == i)
538 continue;
539
540 if (!idle_cpu(i) && is_housekeeping_cpu(i)) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200541 cpu = i;
542 goto unlock;
543 }
544 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700545 }
Vatika Harlalka9642d182015-09-01 16:50:59 +0200546
547 if (!is_housekeeping_cpu(cpu))
548 cpu = housekeeping_any_cpu();
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200549unlock:
550 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700551 return cpu;
552}
553/*
Thomas Gleixner06d83082008-03-22 09:20:24 +0100554 * When add_timer_on() enqueues a timer into the timer wheel of an
555 * idle CPU then this timer might expire before the next timer event
556 * which is scheduled to wake up that CPU. In case of a completely
557 * idle system the next event might even be infinite time into the
558 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
559 * leaves the inner idle loop so the newly added timer is taken into
560 * account when the CPU goes back to idle and evaluates the timer
561 * wheel for the next timer event.
562 */
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200563static void wake_up_idle_cpu(int cpu)
Thomas Gleixner06d83082008-03-22 09:20:24 +0100564{
565 struct rq *rq = cpu_rq(cpu);
566
567 if (cpu == smp_processor_id())
568 return;
569
Andy Lutomirski67b9ca72014-06-04 10:31:17 -0700570 if (set_nr_and_not_polling(rq->idle))
Thomas Gleixner06d83082008-03-22 09:20:24 +0100571 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700572 else
573 trace_sched_wake_idle_without_ipi(cpu);
Thomas Gleixner06d83082008-03-22 09:20:24 +0100574}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100575
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200576static bool wake_up_full_nohz_cpu(int cpu)
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200577{
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200578 /*
579 * We just need the target to call irq_exit() and re-evaluate
580 * the next tick. The nohz full kick at least implies that.
581 * If needed we can still optimize that later with an
582 * empty IRQ.
583 */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200584 if (tick_nohz_full_cpu(cpu)) {
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200585 if (cpu != smp_processor_id() ||
586 tick_nohz_tick_stopped())
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200587 tick_nohz_full_kick_cpu(cpu);
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200588 return true;
589 }
590
591 return false;
592}
593
594void wake_up_nohz_cpu(int cpu)
595{
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200596 if (!wake_up_full_nohz_cpu(cpu))
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200597 wake_up_idle_cpu(cpu);
598}
599
Suresh Siddhaca380622011-10-03 15:09:00 -0700600static inline bool got_nohz_idle_kick(void)
601{
Suresh Siddha1c792db2011-12-01 17:07:32 -0800602 int cpu = smp_processor_id();
Vincent Guittot873b4c62013-06-05 10:13:11 +0200603
604 if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)))
605 return false;
606
607 if (idle_cpu(cpu) && !need_resched())
608 return true;
609
610 /*
611 * We can't run Idle Load Balance on this CPU for this time so we
612 * cancel it and clear NOHZ_BALANCE_KICK
613 */
614 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu));
615 return false;
Suresh Siddhaca380622011-10-03 15:09:00 -0700616}
617
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200618#else /* CONFIG_NO_HZ_COMMON */
Suresh Siddhaca380622011-10-03 15:09:00 -0700619
620static inline bool got_nohz_idle_kick(void)
621{
622 return false;
623}
624
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200625#endif /* CONFIG_NO_HZ_COMMON */
Thomas Gleixner06d83082008-03-22 09:20:24 +0100626
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200627#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200628bool sched_can_stop_tick(struct rq *rq)
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200629{
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200630 int fifo_nr_running;
631
632 /* Deadline tasks, even if single, need the tick */
633 if (rq->dl.dl_nr_running)
634 return false;
635
Frederic Weisbecker3882ec62014-03-18 22:54:04 +0100636 /*
Peter Zijlstra2548d542016-04-21 18:03:15 +0200637 * If there are more than one RR tasks, we need the tick to effect the
638 * actual RR behaviour.
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500639 */
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200640 if (rq->rt.rr_nr_running) {
641 if (rq->rt.rr_nr_running == 1)
642 return true;
643 else
644 return false;
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500645 }
646
Peter Zijlstra2548d542016-04-21 18:03:15 +0200647 /*
648 * If there's no RR tasks, but FIFO tasks, we can skip the tick, no
649 * forced preemption between FIFO tasks.
650 */
651 fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running;
652 if (fifo_nr_running)
653 return true;
654
655 /*
656 * If there are no DL,RR/FIFO tasks, there must only be CFS tasks left;
657 * if there's more than one we need the tick for involuntary
658 * preemption.
659 */
660 if (rq->nr_running > 1)
Viresh Kumar541b8262014-06-24 14:04:12 +0530661 return false;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200662
Viresh Kumar541b8262014-06-24 14:04:12 +0530663 return true;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200664}
665#endif /* CONFIG_NO_HZ_FULL */
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200666
Peter Zijlstra029632f2011-10-25 10:00:11 +0200667void sched_avg_update(struct rq *rq)
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200668{
669 s64 period = sched_avg_period();
670
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200671 while ((s64)(rq_clock(rq) - rq->age_stamp) > period) {
Will Deacon0d98bb22010-05-24 12:11:43 -0700672 /*
673 * Inline assembly required to prevent the compiler
674 * optimising this loop into a divmod call.
675 * See __iter_div_u64_rem() for another example of this.
676 */
677 asm("" : "+rm" (rq->age_stamp));
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200678 rq->age_stamp += period;
679 rq->rt_avg /= 2;
680 }
681}
682
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200683#endif /* CONFIG_SMP */
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200684
Paul Turnera790de92011-07-21 09:43:29 -0700685#if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
686 (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
Peter Zijlstraeb755802008-08-19 12:33:05 +0200687/*
Paul Turner82774342011-07-21 09:43:35 -0700688 * Iterate task_group tree rooted at *from, calling @down when first entering a
689 * node and @up when leaving it for the final time.
690 *
691 * Caller must hold rcu_lock or sufficient equivalent.
Peter Zijlstraeb755802008-08-19 12:33:05 +0200692 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200693int walk_tg_tree_from(struct task_group *from,
Paul Turner82774342011-07-21 09:43:35 -0700694 tg_visitor down, tg_visitor up, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200695{
696 struct task_group *parent, *child;
697 int ret;
698
Paul Turner82774342011-07-21 09:43:35 -0700699 parent = from;
700
Peter Zijlstraeb755802008-08-19 12:33:05 +0200701down:
702 ret = (*down)(parent, data);
703 if (ret)
Paul Turner82774342011-07-21 09:43:35 -0700704 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200705 list_for_each_entry_rcu(child, &parent->children, siblings) {
706 parent = child;
707 goto down;
708
709up:
710 continue;
711 }
712 ret = (*up)(parent, data);
Paul Turner82774342011-07-21 09:43:35 -0700713 if (ret || parent == from)
714 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200715
716 child = parent;
717 parent = parent->parent;
718 if (parent)
719 goto up;
Paul Turner82774342011-07-21 09:43:35 -0700720out:
Peter Zijlstraeb755802008-08-19 12:33:05 +0200721 return ret;
722}
723
Peter Zijlstra029632f2011-10-25 10:00:11 +0200724int tg_nop(struct task_group *tg, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200725{
726 return 0;
727}
728#endif
729
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200730static void set_load_weight(struct task_struct *p)
731{
Nikhil Raof05998d2011-05-18 10:09:38 -0700732 int prio = p->static_prio - MAX_RT_PRIO;
733 struct load_weight *load = &p->se.load;
734
Ingo Molnardd41f592007-07-09 18:51:59 +0200735 /*
736 * SCHED_IDLE tasks get minimal weight:
737 */
Henrik Austad20f9cd22015-09-09 17:00:41 +0200738 if (idle_policy(p->policy)) {
Nikhil Raoc8b28112011-05-18 14:37:48 -0700739 load->weight = scale_load(WEIGHT_IDLEPRIO);
Nikhil Raof05998d2011-05-18 10:09:38 -0700740 load->inv_weight = WMULT_IDLEPRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +0200741 return;
742 }
743
Andi Kleened82b8a2015-11-29 20:59:43 -0800744 load->weight = scale_load(sched_prio_to_weight[prio]);
745 load->inv_weight = sched_prio_to_wmult[prio];
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200746}
747
Peter Zijlstra1de64442015-09-30 17:44:13 +0200748static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -0600749{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100750 update_rq_clock(rq);
Peter Zijlstra1de64442015-09-30 17:44:13 +0200751 if (!(flags & ENQUEUE_RESTORE))
752 sched_info_queued(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100753 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +0200754}
755
Peter Zijlstra1de64442015-09-30 17:44:13 +0200756static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +0200757{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100758 update_rq_clock(rq);
Peter Zijlstra1de64442015-09-30 17:44:13 +0200759 if (!(flags & DEQUEUE_SAVE))
760 sched_info_dequeued(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100761 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200762}
763
Peter Zijlstra029632f2011-10-25 10:00:11 +0200764void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100765{
766 if (task_contributes_to_load(p))
767 rq->nr_uninterruptible--;
768
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100769 enqueue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100770}
771
Peter Zijlstra029632f2011-10-25 10:00:11 +0200772void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100773{
774 if (task_contributes_to_load(p))
775 rq->nr_uninterruptible++;
776
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100777 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100778}
779
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100780static void update_rq_clock_task(struct rq *rq, s64 delta)
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700781{
Glauber Costa095c0aa2011-07-11 15:28:18 -0400782/*
783 * In theory, the compile should just see 0 here, and optimize out the call
784 * to sched_rt_avg_update. But I don't trust it...
785 */
786#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
787 s64 steal = 0, irq_delta = 0;
788#endif
789#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Peter Zijlstra8e92c202010-12-09 14:15:34 +0100790 irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100791
792 /*
793 * Since irq_time is only updated on {soft,}irq_exit, we might run into
794 * this case when a previous update_rq_clock() happened inside a
795 * {soft,}irq region.
796 *
797 * When this happens, we stop ->clock_task and only update the
798 * prev_irq_time stamp to account for the part that fit, so that a next
799 * update will consume the rest. This ensures ->clock_task is
800 * monotonic.
801 *
802 * It does however cause some slight miss-attribution of {soft,}irq
803 * time, a more accurate solution would be to update the irq_time using
804 * the current rq->clock timestamp, except that would require using
805 * atomic ops.
806 */
807 if (irq_delta > delta)
808 irq_delta = delta;
809
810 rq->prev_irq_time += irq_delta;
811 delta -= irq_delta;
Glauber Costa095c0aa2011-07-11 15:28:18 -0400812#endif
813#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
Ingo Molnarc5905af2012-02-24 08:31:31 +0100814 if (static_key_false((&paravirt_steal_rq_enabled))) {
Glauber Costa095c0aa2011-07-11 15:28:18 -0400815 steal = paravirt_steal_clock(cpu_of(rq));
816 steal -= rq->prev_steal_time_rq;
817
818 if (unlikely(steal > delta))
819 steal = delta;
820
Glauber Costa095c0aa2011-07-11 15:28:18 -0400821 rq->prev_steal_time_rq += steal;
Glauber Costa095c0aa2011-07-11 15:28:18 -0400822 delta -= steal;
823 }
824#endif
825
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100826 rq->clock_task += delta;
827
Glauber Costa095c0aa2011-07-11 15:28:18 -0400828#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -0400829 if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
Glauber Costa095c0aa2011-07-11 15:28:18 -0400830 sched_rt_avg_update(rq, irq_delta + steal);
831#endif
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700832}
833
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200834void sched_set_stop_task(int cpu, struct task_struct *stop)
835{
836 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
837 struct task_struct *old_stop = cpu_rq(cpu)->stop;
838
839 if (stop) {
840 /*
841 * Make it appear like a SCHED_FIFO task, its something
842 * userspace knows about and won't get confused about.
843 *
844 * Also, it will make PI more or less work without too
845 * much confusion -- but then, stop work should not
846 * rely on PI working anyway.
847 */
848 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
849
850 stop->sched_class = &stop_sched_class;
851 }
852
853 cpu_rq(cpu)->stop = stop;
854
855 if (old_stop) {
856 /*
857 * Reset it back to a normal scheduling class so that
858 * it can die in pieces.
859 */
860 old_stop->sched_class = &rt_sched_class;
861 }
862}
863
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100864/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200865 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200866 */
Ingo Molnar14531182007-07-09 18:51:59 +0200867static inline int __normal_prio(struct task_struct *p)
868{
Ingo Molnardd41f592007-07-09 18:51:59 +0200869 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +0200870}
871
872/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700873 * Calculate the expected normal priority: i.e. priority
874 * without taking RT-inheritance into account. Might be
875 * boosted by interactivity modifiers. Changes upon fork,
876 * setprio syscalls, and whenever the interactivity
877 * estimator recalculates.
878 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700879static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700880{
881 int prio;
882
Dario Faggioliaab03e02013-11-28 11:14:43 +0100883 if (task_has_dl_policy(p))
884 prio = MAX_DL_PRIO-1;
885 else if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -0700886 prio = MAX_RT_PRIO-1 - p->rt_priority;
887 else
888 prio = __normal_prio(p);
889 return prio;
890}
891
892/*
893 * Calculate the current priority, i.e. the priority
894 * taken into account by the scheduler. This value might
895 * be boosted by RT tasks, or might be boosted by
896 * interactivity modifiers. Will be RT if the task got
897 * RT-boosted. If not then it returns p->normal_prio.
898 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700899static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700900{
901 p->normal_prio = normal_prio(p);
902 /*
903 * If we are RT tasks or we were boosted to RT priority,
904 * keep the priority unchanged. Otherwise, update priority
905 * to the normal priority:
906 */
907 if (!rt_prio(p->prio))
908 return p->normal_prio;
909 return p->prio;
910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912/**
913 * task_curr - is this task currently executing on a CPU?
914 * @p: the task in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +0200915 *
916 * Return: 1 if the task is currently executing. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700918inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 return cpu_curr(task_cpu(p)) == p;
921}
922
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +0300923/*
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +0200924 * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock,
925 * use the balance_callback list if you want balancing.
926 *
927 * this means any call to check_class_changed() must be followed by a call to
928 * balance_callback().
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +0300929 */
Steven Rostedtcb469842008-01-25 21:08:22 +0100930static inline void check_class_changed(struct rq *rq, struct task_struct *p,
931 const struct sched_class *prev_class,
Peter Zijlstrada7a7352011-01-17 17:03:27 +0100932 int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +0100933{
934 if (prev_class != p->sched_class) {
935 if (prev_class->switched_from)
Peter Zijlstrada7a7352011-01-17 17:03:27 +0100936 prev_class->switched_from(rq, p);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +0200937
Peter Zijlstrada7a7352011-01-17 17:03:27 +0100938 p->sched_class->switched_to(rq, p);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100939 } else if (oldprio != p->prio || dl_task(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +0100940 p->sched_class->prio_changed(rq, p, oldprio);
Steven Rostedtcb469842008-01-25 21:08:22 +0100941}
942
Peter Zijlstra029632f2011-10-25 10:00:11 +0200943void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e5a7402010-10-31 12:37:04 +0100944{
945 const struct sched_class *class;
946
947 if (p->sched_class == rq->curr->sched_class) {
948 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
949 } else {
950 for_each_class(class) {
951 if (class == rq->curr->sched_class)
952 break;
953 if (class == p->sched_class) {
Kirill Tkhai88751252014-06-29 00:03:57 +0400954 resched_curr(rq);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +0100955 break;
956 }
957 }
958 }
959
960 /*
961 * A queue event has occurred, and we're going to schedule. In
962 * this case, we can save a useless back to back clock update.
963 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +0400964 if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100965 rq_clock_skip_update(rq, true);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +0100966}
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968#ifdef CONFIG_SMP
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200969/*
970 * This is how migration works:
971 *
972 * 1) we invoke migration_cpu_stop() on the target CPU using
973 * stop_one_cpu().
974 * 2) stopper starts to run (implicitly forcing the migrated thread
975 * off the CPU)
976 * 3) it checks whether the migrated task is still in the wrong runqueue.
977 * 4) if it's in the wrong runqueue then the migration thread removes
978 * it and puts it into the right queue.
979 * 5) stopper completes and stop_one_cpu() returns and the migration
980 * is done.
981 */
982
983/*
984 * move_queued_task - move a queued task to new rq.
985 *
986 * Returns (locked) new rq. Old rq's lock is released.
987 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +0200988static struct rq *move_queued_task(struct rq *rq, struct task_struct *p, int new_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200989{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200990 lockdep_assert_held(&rq->lock);
991
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200992 p->on_rq = TASK_ON_RQ_MIGRATING;
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800993 dequeue_task(rq, p, 0);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200994 set_task_cpu(p, new_cpu);
995 raw_spin_unlock(&rq->lock);
996
997 rq = cpu_rq(new_cpu);
998
999 raw_spin_lock(&rq->lock);
1000 BUG_ON(task_cpu(p) != new_cpu);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001001 enqueue_task(rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001002 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001003 check_preempt_curr(rq, p, 0);
1004
1005 return rq;
1006}
1007
1008struct migration_arg {
1009 struct task_struct *task;
1010 int dest_cpu;
1011};
1012
1013/*
1014 * Move (not current) task off this cpu, onto dest cpu. We're doing
1015 * this because either it can't run here any more (set_cpus_allowed()
1016 * away from this CPU, or CPU going down), or because we're
1017 * attempting to rebalance this task on exec (sched_exec).
1018 *
1019 * So we race with normal scheduler movements, but that's OK, as long
1020 * as the task is no longer on this CPU.
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001021 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001022static struct rq *__migrate_task(struct rq *rq, struct task_struct *p, int dest_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001023{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001024 if (unlikely(!cpu_active(dest_cpu)))
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001025 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001026
1027 /* Affinity changed (again). */
1028 if (!cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001029 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001030
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001031 rq = move_queued_task(rq, p, dest_cpu);
1032
1033 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001034}
1035
1036/*
1037 * migration_cpu_stop - this will be executed by a highprio stopper thread
1038 * and performs thread migration by bumping thread off CPU then
1039 * 'pushing' onto another runqueue.
1040 */
1041static int migration_cpu_stop(void *data)
1042{
1043 struct migration_arg *arg = data;
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001044 struct task_struct *p = arg->task;
1045 struct rq *rq = this_rq();
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001046
1047 /*
1048 * The original target cpu might have gone down and we might
1049 * be on another cpu but it doesn't matter.
1050 */
1051 local_irq_disable();
1052 /*
1053 * We need to explicitly wake pending tasks before running
1054 * __migrate_task() such that we will not miss enforcing cpus_allowed
1055 * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
1056 */
1057 sched_ttwu_pending();
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001058
1059 raw_spin_lock(&p->pi_lock);
1060 raw_spin_lock(&rq->lock);
1061 /*
1062 * If task_rq(p) != rq, it cannot be migrated here, because we're
1063 * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
1064 * we're holding p->pi_lock.
1065 */
Cheng Chaobf89a302016-09-14 10:01:50 +08001066 if (task_rq(p) == rq) {
1067 if (task_on_rq_queued(p))
1068 rq = __migrate_task(rq, p, arg->dest_cpu);
1069 else
1070 p->wake_cpu = arg->dest_cpu;
1071 }
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001072 raw_spin_unlock(&rq->lock);
1073 raw_spin_unlock(&p->pi_lock);
1074
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001075 local_irq_enable();
1076 return 0;
1077}
1078
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001079/*
1080 * sched_class::set_cpus_allowed must do the below, but is not required to
1081 * actually call this function.
1082 */
1083void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001084{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001085 cpumask_copy(&p->cpus_allowed, new_mask);
1086 p->nr_cpus_allowed = cpumask_weight(new_mask);
1087}
1088
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001089void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
1090{
Peter Zijlstra6c370672015-05-15 17:43:36 +02001091 struct rq *rq = task_rq(p);
1092 bool queued, running;
1093
Peter Zijlstra25834c72015-05-15 17:43:34 +02001094 lockdep_assert_held(&p->pi_lock);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001095
1096 queued = task_on_rq_queued(p);
1097 running = task_current(rq, p);
1098
1099 if (queued) {
1100 /*
1101 * Because __kthread_bind() calls this on blocked tasks without
1102 * holding rq->lock.
1103 */
1104 lockdep_assert_held(&rq->lock);
Peter Zijlstra1de64442015-09-30 17:44:13 +02001105 dequeue_task(rq, p, DEQUEUE_SAVE);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001106 }
1107 if (running)
1108 put_prev_task(rq, p);
1109
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001110 p->sched_class->set_cpus_allowed(p, new_mask);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001111
1112 if (running)
1113 p->sched_class->set_curr_task(rq);
1114 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02001115 enqueue_task(rq, p, ENQUEUE_RESTORE);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001116}
1117
1118/*
1119 * Change a given task's CPU affinity. Migrate the thread to a
1120 * proper CPU and schedule it away if the CPU it's executing on
1121 * is removed from the allowed bitmask.
1122 *
1123 * NOTE: the caller must have a valid reference to the task, the
1124 * task must not exit() & deallocate itself prematurely. The
1125 * call is not atomic; no spinlocks may be held.
1126 */
Peter Zijlstra25834c72015-05-15 17:43:34 +02001127static int __set_cpus_allowed_ptr(struct task_struct *p,
1128 const struct cpumask *new_mask, bool check)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001129{
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001130 const struct cpumask *cpu_valid_mask = cpu_active_mask;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001131 unsigned int dest_cpu;
Peter Zijlstraeb580752015-07-31 21:28:18 +02001132 struct rq_flags rf;
1133 struct rq *rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001134 int ret = 0;
1135
Peter Zijlstraeb580752015-07-31 21:28:18 +02001136 rq = task_rq_lock(p, &rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001137
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001138 if (p->flags & PF_KTHREAD) {
1139 /*
1140 * Kernel threads are allowed on online && !active CPUs
1141 */
1142 cpu_valid_mask = cpu_online_mask;
1143 }
1144
Peter Zijlstra25834c72015-05-15 17:43:34 +02001145 /*
1146 * Must re-check here, to close a race against __kthread_bind(),
1147 * sched_setaffinity() is not guaranteed to observe the flag.
1148 */
1149 if (check && (p->flags & PF_NO_SETAFFINITY)) {
1150 ret = -EINVAL;
1151 goto out;
1152 }
1153
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001154 if (cpumask_equal(&p->cpus_allowed, new_mask))
1155 goto out;
1156
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001157 if (!cpumask_intersects(new_mask, cpu_valid_mask)) {
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001158 ret = -EINVAL;
1159 goto out;
1160 }
1161
1162 do_set_cpus_allowed(p, new_mask);
1163
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001164 if (p->flags & PF_KTHREAD) {
1165 /*
1166 * For kernel threads that do indeed end up on online &&
1167 * !active we want to ensure they are strict per-cpu threads.
1168 */
1169 WARN_ON(cpumask_intersects(new_mask, cpu_online_mask) &&
1170 !cpumask_intersects(new_mask, cpu_active_mask) &&
1171 p->nr_cpus_allowed != 1);
1172 }
1173
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001174 /* Can the task run on the task's current CPU? If so, we're done */
1175 if (cpumask_test_cpu(task_cpu(p), new_mask))
1176 goto out;
1177
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001178 dest_cpu = cpumask_any_and(cpu_valid_mask, new_mask);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001179 if (task_running(rq, p) || p->state == TASK_WAKING) {
1180 struct migration_arg arg = { p, dest_cpu };
1181 /* Need help from migration thread: drop lock and wait. */
Peter Zijlstraeb580752015-07-31 21:28:18 +02001182 task_rq_unlock(rq, p, &rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001183 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
1184 tlb_migrate_finish(p->mm);
1185 return 0;
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001186 } else if (task_on_rq_queued(p)) {
1187 /*
1188 * OK, since we're going to drop the lock immediately
1189 * afterwards anyway.
1190 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001191 lockdep_unpin_lock(&rq->lock, rf.cookie);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001192 rq = move_queued_task(rq, p, dest_cpu);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001193 lockdep_repin_lock(&rq->lock, rf.cookie);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001194 }
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001195out:
Peter Zijlstraeb580752015-07-31 21:28:18 +02001196 task_rq_unlock(rq, p, &rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001197
1198 return ret;
1199}
Peter Zijlstra25834c72015-05-15 17:43:34 +02001200
1201int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1202{
1203 return __set_cpus_allowed_ptr(p, new_mask, false);
1204}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001205EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
1206
Ingo Molnardd41f592007-07-09 18:51:59 +02001207void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001208{
Peter Zijlstrae2912002009-12-16 18:04:36 +01001209#ifdef CONFIG_SCHED_DEBUG
1210 /*
1211 * We should never call set_task_cpu() on a blocked task,
1212 * ttwu() will sort out the placement.
1213 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01001214 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
Oleg Nesterove2336f62014-10-08 20:33:48 +02001215 !p->on_rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001216
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001217 /*
1218 * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING,
1219 * because schedstat_wait_{start,end} rebase migrating task's wait_start
1220 * time relying on p->on_rq.
1221 */
1222 WARN_ON_ONCE(p->state == TASK_RUNNING &&
1223 p->sched_class == &fair_sched_class &&
1224 (p->on_rq && !task_on_rq_migrating(p)));
1225
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001226#ifdef CONFIG_LOCKDEP
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001227 /*
1228 * The caller should hold either p->pi_lock or rq->lock, when changing
1229 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
1230 *
1231 * sched_move_task() holds both and thus holding either pins the cgroup,
Peter Zijlstra8323f262012-06-22 13:36:05 +02001232 * see task_group().
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001233 *
1234 * Furthermore, all task_rq users should acquire both locks, see
1235 * task_rq_lock().
1236 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001237 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
1238 lockdep_is_held(&task_rq(p)->lock)));
1239#endif
Peter Zijlstrae2912002009-12-16 18:04:36 +01001240#endif
1241
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08001242 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001243
Peter Zijlstra0c697742009-12-22 15:43:19 +01001244 if (task_cpu(p) != new_cpu) {
Paul Turner0a74bef2012-10-04 13:18:30 +02001245 if (p->sched_class->migrate_task_rq)
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08001246 p->sched_class->migrate_task_rq(p);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001247 p->se.nr_migrations++;
Peter Zijlstraff303e62015-04-17 20:05:30 +02001248 perf_event_task_migrate(p);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001249 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001250
1251 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001252}
1253
Peter Zijlstraac66f542013-10-07 11:29:16 +01001254static void __migrate_swap_task(struct task_struct *p, int cpu)
1255{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001256 if (task_on_rq_queued(p)) {
Peter Zijlstraac66f542013-10-07 11:29:16 +01001257 struct rq *src_rq, *dst_rq;
1258
1259 src_rq = task_rq(p);
1260 dst_rq = cpu_rq(cpu);
1261
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001262 p->on_rq = TASK_ON_RQ_MIGRATING;
Peter Zijlstraac66f542013-10-07 11:29:16 +01001263 deactivate_task(src_rq, p, 0);
1264 set_task_cpu(p, cpu);
1265 activate_task(dst_rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001266 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstraac66f542013-10-07 11:29:16 +01001267 check_preempt_curr(dst_rq, p, 0);
1268 } else {
1269 /*
1270 * Task isn't running anymore; make it appear like we migrated
1271 * it before it went to sleep. This means on wakeup we make the
Leo Yana1fd4652016-08-05 14:32:38 +08001272 * previous cpu our target instead of where it really is.
Peter Zijlstraac66f542013-10-07 11:29:16 +01001273 */
1274 p->wake_cpu = cpu;
1275 }
1276}
1277
1278struct migration_swap_arg {
1279 struct task_struct *src_task, *dst_task;
1280 int src_cpu, dst_cpu;
1281};
1282
1283static int migrate_swap_stop(void *data)
1284{
1285 struct migration_swap_arg *arg = data;
1286 struct rq *src_rq, *dst_rq;
1287 int ret = -EAGAIN;
1288
Peter Zijlstra62694cd2015-10-09 18:36:29 +02001289 if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu))
1290 return -EAGAIN;
1291
Peter Zijlstraac66f542013-10-07 11:29:16 +01001292 src_rq = cpu_rq(arg->src_cpu);
1293 dst_rq = cpu_rq(arg->dst_cpu);
1294
Peter Zijlstra74602312013-10-10 20:17:22 +02001295 double_raw_lock(&arg->src_task->pi_lock,
1296 &arg->dst_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001297 double_rq_lock(src_rq, dst_rq);
Peter Zijlstra62694cd2015-10-09 18:36:29 +02001298
Peter Zijlstraac66f542013-10-07 11:29:16 +01001299 if (task_cpu(arg->dst_task) != arg->dst_cpu)
1300 goto unlock;
1301
1302 if (task_cpu(arg->src_task) != arg->src_cpu)
1303 goto unlock;
1304
1305 if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task)))
1306 goto unlock;
1307
1308 if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task)))
1309 goto unlock;
1310
1311 __migrate_swap_task(arg->src_task, arg->dst_cpu);
1312 __migrate_swap_task(arg->dst_task, arg->src_cpu);
1313
1314 ret = 0;
1315
1316unlock:
1317 double_rq_unlock(src_rq, dst_rq);
Peter Zijlstra74602312013-10-10 20:17:22 +02001318 raw_spin_unlock(&arg->dst_task->pi_lock);
1319 raw_spin_unlock(&arg->src_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001320
1321 return ret;
1322}
1323
1324/*
1325 * Cross migrate two tasks
1326 */
1327int migrate_swap(struct task_struct *cur, struct task_struct *p)
1328{
1329 struct migration_swap_arg arg;
1330 int ret = -EINVAL;
1331
Peter Zijlstraac66f542013-10-07 11:29:16 +01001332 arg = (struct migration_swap_arg){
1333 .src_task = cur,
1334 .src_cpu = task_cpu(cur),
1335 .dst_task = p,
1336 .dst_cpu = task_cpu(p),
1337 };
1338
1339 if (arg.src_cpu == arg.dst_cpu)
1340 goto out;
1341
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02001342 /*
1343 * These three tests are all lockless; this is OK since all of them
1344 * will be re-checked with proper locks held further down the line.
1345 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01001346 if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
1347 goto out;
1348
1349 if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task)))
1350 goto out;
1351
1352 if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task)))
1353 goto out;
1354
Mel Gorman286549d2014-01-21 15:51:03 -08001355 trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001356 ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
1357
1358out:
Peter Zijlstraac66f542013-10-07 11:29:16 +01001359 return ret;
1360}
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 * wait_task_inactive - wait for a thread to unschedule.
1364 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001365 * If @match_state is nonzero, it's the @p->state value just checked and
1366 * not expected to change. If it changes, i.e. @p might have woken up,
1367 * then return zero. When we succeed in waiting for @p to be off its CPU,
1368 * we return a positive number (its total switch count). If a second call
1369 * a short while later returns the same number, the caller can be sure that
1370 * @p has remained unscheduled the whole time.
1371 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * The caller must ensure that the task *will* unschedule sometime soon,
1373 * else this function might spin for a *long* time. This function can't
1374 * be called with interrupts off, or it may introduce deadlock with
1375 * smp_call_function() if an IPI is sent by the same process we are
1376 * waiting to become inactive.
1377 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001378unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001380 int running, queued;
Peter Zijlstraeb580752015-07-31 21:28:18 +02001381 struct rq_flags rf;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001382 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001383 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Andi Kleen3a5c3592007-10-15 17:00:14 +02001385 for (;;) {
1386 /*
1387 * We do the initial early heuristics without holding
1388 * any task-queue locks at all. We'll only try to get
1389 * the runqueue lock when things look like they will
1390 * work out!
1391 */
1392 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001393
Andi Kleen3a5c3592007-10-15 17:00:14 +02001394 /*
1395 * If the task is actively running on another CPU
1396 * still, just relax and busy-wait without holding
1397 * any locks.
1398 *
1399 * NOTE! Since we don't hold any locks, it's not
1400 * even sure that "rq" stays as the right runqueue!
1401 * But we don't care, since "task_running()" will
1402 * return false if the runqueue has changed and p
1403 * is actually now running somewhere else!
1404 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001405 while (task_running(rq, p)) {
1406 if (match_state && unlikely(p->state != match_state))
1407 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02001408 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07001409 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001410
Andi Kleen3a5c3592007-10-15 17:00:14 +02001411 /*
1412 * Ok, time to look more closely! We need the rq
1413 * lock now, to be *sure*. If we're wrong, we'll
1414 * just go back and repeat.
1415 */
Peter Zijlstraeb580752015-07-31 21:28:18 +02001416 rq = task_rq_lock(p, &rf);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02001417 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001418 running = task_running(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001419 queued = task_on_rq_queued(p);
Roland McGrath85ba2d82008-07-25 19:45:58 -07001420 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07001421 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07001422 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Peter Zijlstraeb580752015-07-31 21:28:18 +02001423 task_rq_unlock(rq, p, &rf);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001424
Andi Kleen3a5c3592007-10-15 17:00:14 +02001425 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07001426 * If it changed from the expected state, bail out now.
1427 */
1428 if (unlikely(!ncsw))
1429 break;
1430
1431 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02001432 * Was it really running after all now that we
1433 * checked with the proper locks actually held?
1434 *
1435 * Oops. Go back and try again..
1436 */
1437 if (unlikely(running)) {
1438 cpu_relax();
1439 continue;
1440 }
1441
1442 /*
1443 * It's not enough that it's not actively running,
1444 * it must be off the runqueue _entirely_, and not
1445 * preempted!
1446 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00001447 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02001448 * running right now), it's preempted, and we should
1449 * yield - it could be a while.
1450 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001451 if (unlikely(queued)) {
Thomas Gleixner8eb90c32011-02-23 23:52:21 +00001452 ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
1453
1454 set_current_state(TASK_UNINTERRUPTIBLE);
1455 schedule_hrtimeout(&to, HRTIMER_MODE_REL);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001456 continue;
1457 }
1458
1459 /*
1460 * Ahh, all good. It wasn't running, and it wasn't
1461 * runnable, which means that it will never become
1462 * running in the future either. We're all done!
1463 */
1464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07001466
1467 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470/***
1471 * kick_process - kick a running thread to enter/exit the kernel
1472 * @p: the to-be-kicked thread
1473 *
1474 * Cause a process which is running on another CPU to enter
1475 * kernel-mode, without any delay. (to get signals handled.)
1476 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001477 * NOTE: this function doesn't have to take the runqueue lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 * because all it wants to ensure is that the remote task enters
1479 * the kernel. If the IPI races and the task has been migrated
1480 * to another CPU then no harm is done and the purpose has been
1481 * achieved as well.
1482 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001483void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 int cpu;
1486
1487 preempt_disable();
1488 cpu = task_cpu(p);
1489 if ((cpu != smp_processor_id()) && task_curr(p))
1490 smp_send_reschedule(cpu);
1491 preempt_enable();
1492}
Rusty Russellb43e3522009-06-12 22:27:00 -06001493EXPORT_SYMBOL_GPL(kick_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Oleg Nesterov30da6882010-03-15 10:10:19 +01001495/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001496 * ->cpus_allowed is protected by both rq->lock and p->pi_lock
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001497 *
1498 * A few notes on cpu_active vs cpu_online:
1499 *
1500 * - cpu_active must be a subset of cpu_online
1501 *
1502 * - on cpu-up we allow per-cpu kthreads on the online && !active cpu,
1503 * see __set_cpus_allowed_ptr(). At this point the newly online
1504 * cpu isn't yet part of the sched domains, and balancing will not
1505 * see it.
1506 *
1507 * - on cpu-down we clear cpu_active() to mask the sched domains and
1508 * avoid the load balancer to place new tasks on the to be removed
1509 * cpu. Existing tasks will remain running there and will be taken
1510 * off.
1511 *
1512 * This means that fallback selection must not select !active CPUs.
1513 * And can assume that any active CPU must be online. Conversely
1514 * select_task_rq() below may allow selection of !active CPUs in order
1515 * to satisfy the above rules.
Oleg Nesterov30da6882010-03-15 10:10:19 +01001516 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001517static int select_fallback_rq(int cpu, struct task_struct *p)
1518{
Tang Chenaa00d892013-02-22 16:33:33 -08001519 int nid = cpu_to_node(cpu);
1520 const struct cpumask *nodemask = NULL;
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001521 enum { cpuset, possible, fail } state = cpuset;
1522 int dest_cpu;
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001523
Tang Chenaa00d892013-02-22 16:33:33 -08001524 /*
1525 * If the node that the cpu is on has been offlined, cpu_to_node()
1526 * will return -1. There is no cpu on the node, and we should
1527 * select the cpu on the other node.
1528 */
1529 if (nid != -1) {
1530 nodemask = cpumask_of_node(nid);
1531
1532 /* Look for allowed, online CPU in same node. */
1533 for_each_cpu(dest_cpu, nodemask) {
Tang Chenaa00d892013-02-22 16:33:33 -08001534 if (!cpu_active(dest_cpu))
1535 continue;
1536 if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
1537 return dest_cpu;
1538 }
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001539 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001540
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001541 for (;;) {
1542 /* Any allowed, online CPU? */
Srivatsa S. Bhate3831ed2012-03-30 19:40:28 +05301543 for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) {
Tejun Heofeb245e2016-06-16 15:35:04 -04001544 if (!(p->flags & PF_KTHREAD) && !cpu_active(dest_cpu))
1545 continue;
1546 if (!cpu_online(dest_cpu))
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001547 continue;
1548 goto out;
1549 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001550
Oleg Nesterove73e85f2015-10-10 20:53:15 +02001551 /* No more Mr. Nice Guy. */
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001552 switch (state) {
1553 case cpuset:
Oleg Nesterove73e85f2015-10-10 20:53:15 +02001554 if (IS_ENABLED(CONFIG_CPUSETS)) {
1555 cpuset_cpus_allowed_fallback(p);
1556 state = possible;
1557 break;
1558 }
1559 /* fall-through */
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001560 case possible:
1561 do_set_cpus_allowed(p, cpu_possible_mask);
1562 state = fail;
1563 break;
1564
1565 case fail:
1566 BUG();
1567 break;
1568 }
1569 }
1570
1571out:
1572 if (state != cpuset) {
1573 /*
1574 * Don't tell them about moving exiting tasks or
1575 * kernel threads (both mm NULL), since they never
1576 * leave kernel.
1577 */
1578 if (p->mm && printk_ratelimit()) {
John Stultzaac74dc2014-06-04 16:11:40 -07001579 printk_deferred("process %d (%s) no longer affine to cpu%d\n",
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001580 task_pid_nr(p), p->comm, cpu);
1581 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001582 }
1583
1584 return dest_cpu;
1585}
1586
Peter Zijlstrae2912002009-12-16 18:04:36 +01001587/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001588 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01001589 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001590static inline
Peter Zijlstraac66f542013-10-07 11:29:16 +01001591int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001592{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001593 lockdep_assert_held(&p->pi_lock);
1594
Thomas Gleixner50605ff2016-05-11 14:23:31 +02001595 if (tsk_nr_cpus_allowed(p) > 1)
Wanpeng Li6c1d9412014-11-05 09:14:37 +08001596 cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001597 else
1598 cpu = cpumask_any(tsk_cpus_allowed(p));
Peter Zijlstrae2912002009-12-16 18:04:36 +01001599
1600 /*
1601 * In order not to call set_task_cpu() on a blocking task we need
1602 * to rely on ttwu() to place the task on a valid ->cpus_allowed
1603 * cpu.
1604 *
1605 * Since this is common to all placement strategies, this lives here.
1606 *
1607 * [ this allows ->select_task() to simply return task_cpu(p) and
1608 * not worry about this generic constraint ]
1609 */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001610 if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01001611 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001612 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01001613
1614 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001615}
Mike Galbraith09a40af2010-04-15 07:29:59 +02001616
1617static void update_avg(u64 *avg, u64 sample)
1618{
1619 s64 diff = sample - *avg;
1620 *avg += diff >> 3;
1621}
Peter Zijlstra25834c72015-05-15 17:43:34 +02001622
1623#else
1624
1625static inline int __set_cpus_allowed_ptr(struct task_struct *p,
1626 const struct cpumask *new_mask, bool check)
1627{
1628 return set_cpus_allowed_ptr(p, new_mask);
1629}
1630
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001631#endif /* CONFIG_SMP */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001632
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001633static void
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001634ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001635{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001636 struct rq *rq;
1637
1638 if (!schedstat_enabled())
1639 return;
1640
1641 rq = this_rq();
Tejun Heo9ed38112009-12-03 15:08:03 +09001642
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001643#ifdef CONFIG_SMP
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001644 if (cpu == rq->cpu) {
Josh Poimboeufae928822016-06-17 12:43:24 -05001645 schedstat_inc(rq->ttwu_local);
1646 schedstat_inc(p->se.statistics.nr_wakeups_local);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001647 } else {
1648 struct sched_domain *sd;
1649
Josh Poimboeufae928822016-06-17 12:43:24 -05001650 schedstat_inc(p->se.statistics.nr_wakeups_remote);
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001651 rcu_read_lock();
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05001652 for_each_domain(rq->cpu, sd) {
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001653 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Josh Poimboeufae928822016-06-17 12:43:24 -05001654 schedstat_inc(sd->ttwu_wake_remote);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001655 break;
1656 }
1657 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001658 rcu_read_unlock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001659 }
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001660
1661 if (wake_flags & WF_MIGRATED)
Josh Poimboeufae928822016-06-17 12:43:24 -05001662 schedstat_inc(p->se.statistics.nr_wakeups_migrate);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001663#endif /* CONFIG_SMP */
1664
Josh Poimboeufae928822016-06-17 12:43:24 -05001665 schedstat_inc(rq->ttwu_count);
1666 schedstat_inc(p->se.statistics.nr_wakeups);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001667
1668 if (wake_flags & WF_SYNC)
Josh Poimboeufae928822016-06-17 12:43:24 -05001669 schedstat_inc(p->se.statistics.nr_wakeups_sync);
Tejun Heo9ed38112009-12-03 15:08:03 +09001670}
1671
Peter Zijlstra1de64442015-09-30 17:44:13 +02001672static inline void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001673{
Tejun Heo9ed38112009-12-03 15:08:03 +09001674 activate_task(rq, p, en_flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001675 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrac2f71152011-04-13 13:28:56 +02001676
1677 /* if a worker is waking up, notify workqueue */
1678 if (p->flags & PF_WQ_WORKER)
1679 wq_worker_waking_up(p, cpu_of(rq));
Tejun Heo9ed38112009-12-03 15:08:03 +09001680}
1681
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001682/*
1683 * Mark the task runnable and perform wakeup-preemption.
1684 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001685static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags,
1686 struct pin_cookie cookie)
Tejun Heo9ed38112009-12-03 15:08:03 +09001687{
Tejun Heo9ed38112009-12-03 15:08:03 +09001688 check_preempt_curr(rq, p, wake_flags);
Tejun Heo9ed38112009-12-03 15:08:03 +09001689 p->state = TASK_RUNNING;
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02001690 trace_sched_wakeup(p);
1691
Tejun Heo9ed38112009-12-03 15:08:03 +09001692#ifdef CONFIG_SMP
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001693 if (p->sched_class->task_woken) {
1694 /*
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001695 * Our task @p is fully woken up and running; so its safe to
1696 * drop the rq->lock, hereafter rq is only used for statistics.
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001697 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001698 lockdep_unpin_lock(&rq->lock, cookie);
Tejun Heo9ed38112009-12-03 15:08:03 +09001699 p->sched_class->task_woken(rq, p);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001700 lockdep_repin_lock(&rq->lock, cookie);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001701 }
Tejun Heo9ed38112009-12-03 15:08:03 +09001702
Steven Rostedte69c6342010-12-06 17:10:31 -05001703 if (rq->idle_stamp) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001704 u64 delta = rq_clock(rq) - rq->idle_stamp;
Jason Low9bd721c2013-09-13 11:26:52 -07001705 u64 max = 2*rq->max_idle_balance_cost;
Tejun Heo9ed38112009-12-03 15:08:03 +09001706
Jason Lowabfafa52013-09-13 11:26:51 -07001707 update_avg(&rq->avg_idle, delta);
1708
1709 if (rq->avg_idle > max)
Tejun Heo9ed38112009-12-03 15:08:03 +09001710 rq->avg_idle = max;
Jason Lowabfafa52013-09-13 11:26:51 -07001711
Tejun Heo9ed38112009-12-03 15:08:03 +09001712 rq->idle_stamp = 0;
1713 }
1714#endif
1715}
1716
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001717static void
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001718ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags,
1719 struct pin_cookie cookie)
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001720{
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02001721 int en_flags = ENQUEUE_WAKEUP;
1722
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001723 lockdep_assert_held(&rq->lock);
1724
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001725#ifdef CONFIG_SMP
1726 if (p->sched_contributes_to_load)
1727 rq->nr_uninterruptible--;
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02001728
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02001729 if (wake_flags & WF_MIGRATED)
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001730 en_flags |= ENQUEUE_MIGRATED;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001731#endif
1732
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02001733 ttwu_activate(rq, p, en_flags);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001734 ttwu_do_wakeup(rq, p, wake_flags, cookie);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001735}
1736
1737/*
1738 * Called in case the task @p isn't fully descheduled from its runqueue,
1739 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
1740 * since all we need to do is flip p->state to TASK_RUNNING, since
1741 * the task is still ->on_rq.
1742 */
1743static int ttwu_remote(struct task_struct *p, int wake_flags)
1744{
Peter Zijlstraeb580752015-07-31 21:28:18 +02001745 struct rq_flags rf;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001746 struct rq *rq;
1747 int ret = 0;
1748
Peter Zijlstraeb580752015-07-31 21:28:18 +02001749 rq = __task_rq_lock(p, &rf);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001750 if (task_on_rq_queued(p)) {
Frederic Weisbecker1ad4ec02013-04-12 01:51:00 +02001751 /* check_preempt_curr() may use rq clock */
1752 update_rq_clock(rq);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001753 ttwu_do_wakeup(rq, p, wake_flags, rf.cookie);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001754 ret = 1;
1755 }
Peter Zijlstraeb580752015-07-31 21:28:18 +02001756 __task_rq_unlock(rq, &rf);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001757
1758 return ret;
1759}
1760
Peter Zijlstra317f3942011-04-05 17:23:58 +02001761#ifdef CONFIG_SMP
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001762void sched_ttwu_pending(void)
Peter Zijlstra317f3942011-04-05 17:23:58 +02001763{
1764 struct rq *rq = this_rq();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001765 struct llist_node *llist = llist_del_all(&rq->wake_list);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001766 struct pin_cookie cookie;
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001767 struct task_struct *p;
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001768 unsigned long flags;
Peter Zijlstra317f3942011-04-05 17:23:58 +02001769
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001770 if (!llist)
1771 return;
1772
1773 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001774 cookie = lockdep_pin_lock(&rq->lock);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001775
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001776 while (llist) {
Peter Zijlstrab7e7ade2016-05-23 11:19:07 +02001777 int wake_flags = 0;
1778
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001779 p = llist_entry(llist, struct task_struct, wake_entry);
1780 llist = llist_next(llist);
Peter Zijlstrab7e7ade2016-05-23 11:19:07 +02001781
1782 if (p->sched_remote_wakeup)
1783 wake_flags = WF_MIGRATED;
1784
1785 ttwu_do_activate(rq, p, wake_flags, cookie);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001786 }
1787
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001788 lockdep_unpin_lock(&rq->lock, cookie);
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001789 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001790}
1791
1792void scheduler_ipi(void)
1793{
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001794 /*
1795 * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
1796 * TIF_NEED_RESCHED remotely (for the first time) will also send
1797 * this IPI.
1798 */
Peter Zijlstra8cb75e02013-11-20 12:22:37 +01001799 preempt_fold_need_resched();
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001800
Frederic Weisbeckerfd2ac4f2014-03-18 21:12:53 +01001801 if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001802 return;
1803
1804 /*
1805 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
1806 * traditionally all their work was done from the interrupt return
1807 * path. Now that we actually do some work, we need to make sure
1808 * we do call them.
1809 *
1810 * Some archs already do call them, luckily irq_enter/exit nest
1811 * properly.
1812 *
1813 * Arguably we should visit all archs and update all handlers,
1814 * however a fair share of IPIs are still resched only so this would
1815 * somewhat pessimize the simple resched case.
1816 */
1817 irq_enter();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001818 sched_ttwu_pending();
Suresh Siddhaca380622011-10-03 15:09:00 -07001819
1820 /*
1821 * Check if someone kicked us for doing the nohz idle load balance.
1822 */
Vincent Guittot873b4c62013-06-05 10:13:11 +02001823 if (unlikely(got_nohz_idle_kick())) {
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001824 this_rq()->idle_balance = 1;
Suresh Siddhaca380622011-10-03 15:09:00 -07001825 raise_softirq_irqoff(SCHED_SOFTIRQ);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001826 }
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001827 irq_exit();
Peter Zijlstra317f3942011-04-05 17:23:58 +02001828}
1829
Peter Zijlstrab7e7ade2016-05-23 11:19:07 +02001830static void ttwu_queue_remote(struct task_struct *p, int cpu, int wake_flags)
Peter Zijlstra317f3942011-04-05 17:23:58 +02001831{
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001832 struct rq *rq = cpu_rq(cpu);
1833
Peter Zijlstrab7e7ade2016-05-23 11:19:07 +02001834 p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED);
1835
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001836 if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) {
1837 if (!set_nr_if_polling(rq->idle))
1838 smp_send_reschedule(cpu);
1839 else
1840 trace_sched_wake_idle_without_ipi(cpu);
1841 }
Peter Zijlstra317f3942011-04-05 17:23:58 +02001842}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001843
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001844void wake_up_if_idle(int cpu)
1845{
1846 struct rq *rq = cpu_rq(cpu);
1847 unsigned long flags;
1848
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001849 rcu_read_lock();
1850
1851 if (!is_idle_task(rcu_dereference(rq->curr)))
1852 goto out;
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001853
1854 if (set_nr_if_polling(rq->idle)) {
1855 trace_sched_wake_idle_without_ipi(cpu);
1856 } else {
1857 raw_spin_lock_irqsave(&rq->lock, flags);
1858 if (is_idle_task(rq->curr))
1859 smp_send_reschedule(cpu);
1860 /* Else cpu is not in idle, do nothing here */
1861 raw_spin_unlock_irqrestore(&rq->lock, flags);
1862 }
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001863
1864out:
1865 rcu_read_unlock();
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001866}
1867
Peter Zijlstra39be3502012-01-26 12:44:34 +01001868bool cpus_share_cache(int this_cpu, int that_cpu)
Peter Zijlstra518cd622011-12-07 15:07:31 +01001869{
1870 return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
1871}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001872#endif /* CONFIG_SMP */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001873
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02001874static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001875{
1876 struct rq *rq = cpu_rq(cpu);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001877 struct pin_cookie cookie;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001878
Daniel Hellstrom17d9f312011-05-20 04:01:10 +00001879#if defined(CONFIG_SMP)
Peter Zijlstra39be3502012-01-26 12:44:34 +01001880 if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
Peter Zijlstraf01114c2011-05-31 12:26:55 +02001881 sched_clock_cpu(cpu); /* sync clocks x-cpu */
Peter Zijlstrab7e7ade2016-05-23 11:19:07 +02001882 ttwu_queue_remote(p, cpu, wake_flags);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001883 return;
1884 }
1885#endif
1886
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001887 raw_spin_lock(&rq->lock);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001888 cookie = lockdep_pin_lock(&rq->lock);
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02001889 ttwu_do_activate(rq, p, wake_flags, cookie);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001890 lockdep_unpin_lock(&rq->lock, cookie);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001891 raw_spin_unlock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09001892}
1893
Peter Zijlstra8643cda2015-11-17 19:01:11 +01001894/*
1895 * Notes on Program-Order guarantees on SMP systems.
1896 *
1897 * MIGRATION
1898 *
1899 * The basic program-order guarantee on SMP systems is that when a task [t]
1900 * migrates, all its activity on its old cpu [c0] happens-before any subsequent
1901 * execution on its new cpu [c1].
1902 *
1903 * For migration (of runnable tasks) this is provided by the following means:
1904 *
1905 * A) UNLOCK of the rq(c0)->lock scheduling out task t
1906 * B) migration for t is required to synchronize *both* rq(c0)->lock and
1907 * rq(c1)->lock (if not at the same time, then in that order).
1908 * C) LOCK of the rq(c1)->lock scheduling in task
1909 *
1910 * Transitivity guarantees that B happens after A and C after B.
1911 * Note: we only require RCpc transitivity.
1912 * Note: the cpu doing B need not be c0 or c1
1913 *
1914 * Example:
1915 *
1916 * CPU0 CPU1 CPU2
1917 *
1918 * LOCK rq(0)->lock
1919 * sched-out X
1920 * sched-in Y
1921 * UNLOCK rq(0)->lock
1922 *
1923 * LOCK rq(0)->lock // orders against CPU0
1924 * dequeue X
1925 * UNLOCK rq(0)->lock
1926 *
1927 * LOCK rq(1)->lock
1928 * enqueue X
1929 * UNLOCK rq(1)->lock
1930 *
1931 * LOCK rq(1)->lock // orders against CPU2
1932 * sched-out Z
1933 * sched-in X
1934 * UNLOCK rq(1)->lock
1935 *
1936 *
1937 * BLOCKING -- aka. SLEEP + WAKEUP
1938 *
1939 * For blocking we (obviously) need to provide the same guarantee as for
1940 * migration. However the means are completely different as there is no lock
1941 * chain to provide order. Instead we do:
1942 *
1943 * 1) smp_store_release(X->on_cpu, 0)
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02001944 * 2) smp_cond_load_acquire(!X->on_cpu)
Peter Zijlstra8643cda2015-11-17 19:01:11 +01001945 *
1946 * Example:
1947 *
1948 * CPU0 (schedule) CPU1 (try_to_wake_up) CPU2 (schedule)
1949 *
1950 * LOCK rq(0)->lock LOCK X->pi_lock
1951 * dequeue X
1952 * sched-out X
1953 * smp_store_release(X->on_cpu, 0);
1954 *
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02001955 * smp_cond_load_acquire(&X->on_cpu, !VAL);
Peter Zijlstra8643cda2015-11-17 19:01:11 +01001956 * X->state = WAKING
1957 * set_task_cpu(X,2)
1958 *
1959 * LOCK rq(2)->lock
1960 * enqueue X
1961 * X->state = RUNNING
1962 * UNLOCK rq(2)->lock
1963 *
1964 * LOCK rq(2)->lock // orders against CPU1
1965 * sched-out Z
1966 * sched-in X
1967 * UNLOCK rq(2)->lock
1968 *
1969 * UNLOCK X->pi_lock
1970 * UNLOCK rq(0)->lock
1971 *
1972 *
1973 * However; for wakeups there is a second guarantee we must provide, namely we
1974 * must observe the state that lead to our wakeup. That is, not only must our
1975 * task observe its own prior state, it must also observe the stores prior to
1976 * its wakeup.
1977 *
1978 * This means that any means of doing remote wakeups must order the CPU doing
1979 * the wakeup against the CPU the task is going to end up running on. This,
1980 * however, is already required for the regular Program-Order guarantee above,
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02001981 * since the waking CPU is the one issueing the ACQUIRE (smp_cond_load_acquire).
Peter Zijlstra8643cda2015-11-17 19:01:11 +01001982 *
1983 */
1984
Tejun Heo9ed38112009-12-03 15:08:03 +09001985/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09001987 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09001989 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 *
1991 * Put it on the run-queue if it's not already there. The "current"
1992 * thread is always on the run-queue (except when the actual
1993 * re-schedule is in progress), and as such you're allowed to do
1994 * the simpler "current->state = TASK_RUNNING" to mark yourself
1995 * runnable without the overhead of this.
1996 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02001997 * Return: %true if @p was woken up, %false if it was already running.
Tejun Heo9ed38112009-12-03 15:08:03 +09001998 * or @state didn't match @p's state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002000static int
2001try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 unsigned long flags;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002004 int cpu, success = 0;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002005
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02002006 /*
2007 * If we are going to wake up a thread waiting for CONDITION we
2008 * need to ensure that CONDITION=1 done by the caller can not be
2009 * reordered with p->state check below. This pairs with mb() in
2010 * set_current_state() the waiting thread does.
2011 */
2012 smp_mb__before_spinlock();
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002013 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002014 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 goto out;
2016
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002017 trace_sched_waking(p);
2018
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002019 success = 1; /* we're going to change ->state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 cpu = task_cpu(p);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002021
Balbir Singh135e8c92016-09-05 13:16:40 +10002022 /*
2023 * Ensure we load p->on_rq _after_ p->state, otherwise it would
2024 * be possible to, falsely, observe p->on_rq == 0 and get stuck
2025 * in smp_cond_load_acquire() below.
2026 *
2027 * sched_ttwu_pending() try_to_wake_up()
2028 * [S] p->on_rq = 1; [L] P->state
2029 * UNLOCK rq->lock -----.
2030 * \
2031 * +--- RMB
2032 * schedule() /
2033 * LOCK rq->lock -----'
2034 * UNLOCK rq->lock
2035 *
2036 * [task p]
2037 * [S] p->state = UNINTERRUPTIBLE [L] p->on_rq
2038 *
2039 * Pairs with the UNLOCK+LOCK on rq->lock from the
2040 * last wakeup of our task and the schedule that got our task
2041 * current.
2042 */
2043 smp_rmb();
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002044 if (p->on_rq && ttwu_remote(p, wake_flags))
2045 goto stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047#ifdef CONFIG_SMP
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002048 /*
Peter Zijlstraecf7d012015-10-07 14:14:13 +02002049 * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
2050 * possible to, falsely, observe p->on_cpu == 0.
2051 *
2052 * One must be running (->on_cpu == 1) in order to remove oneself
2053 * from the runqueue.
2054 *
2055 * [S] ->on_cpu = 1; [L] ->on_rq
2056 * UNLOCK rq->lock
2057 * RMB
2058 * LOCK rq->lock
2059 * [S] ->on_rq = 0; [L] ->on_cpu
2060 *
2061 * Pairs with the full barrier implied in the UNLOCK+LOCK on rq->lock
2062 * from the consecutive calls to schedule(); the first switching to our
2063 * task, the second putting it to sleep.
2064 */
2065 smp_rmb();
2066
2067 /*
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002068 * If the owning (remote) cpu is still in the middle of schedule() with
2069 * this task as prev, wait until its done referencing the task.
Peter Zijlstrab75a2252015-10-06 14:36:17 +02002070 *
2071 * Pairs with the smp_store_release() in finish_lock_switch().
2072 *
2073 * This ensures that tasks getting woken will be fully ordered against
2074 * their previous state and preserve Program Order.
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002075 */
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02002076 smp_cond_load_acquire(&p->on_cpu, !VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Peter Zijlstraa8e4f2e2011-04-05 17:23:49 +02002078 p->sched_contributes_to_load = !!task_contributes_to_load(p);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002079 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002080
Peter Zijlstraac66f542013-10-07 11:29:16 +01002081 cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002082 if (task_cpu(p) != cpu) {
2083 wake_flags |= WF_MIGRATED;
Mike Galbraith055a0082009-11-12 11:07:44 +01002084 set_task_cpu(p, cpu);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002088 ttwu_queue(p, cpu, wake_flags);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002089stat:
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05002090 ttwu_stat(p, cpu, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091out:
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002092 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 return success;
2095}
2096
David Howells50fa6102009-04-28 15:01:38 +01002097/**
Tejun Heo21aa9af2010-06-08 21:40:37 +02002098 * try_to_wake_up_local - try to wake up a local task with rq lock held
2099 * @p: the thread to be awakened
Luis de Bethencourt9279e0d2016-07-10 15:00:26 +01002100 * @cookie: context's cookie for pinning
Tejun Heo21aa9af2010-06-08 21:40:37 +02002101 *
Peter Zijlstra2acca552011-04-05 17:23:50 +02002102 * Put @p on the run-queue if it's not already there. The caller must
Tejun Heo21aa9af2010-06-08 21:40:37 +02002103 * ensure that this_rq() is locked, @p is bound to this_rq() and not
Peter Zijlstra2acca552011-04-05 17:23:50 +02002104 * the current task.
Tejun Heo21aa9af2010-06-08 21:40:37 +02002105 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002106static void try_to_wake_up_local(struct task_struct *p, struct pin_cookie cookie)
Tejun Heo21aa9af2010-06-08 21:40:37 +02002107{
2108 struct rq *rq = task_rq(p);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002109
Tejun Heo383efcd2013-03-18 12:22:34 -07002110 if (WARN_ON_ONCE(rq != this_rq()) ||
2111 WARN_ON_ONCE(p == current))
2112 return;
2113
Tejun Heo21aa9af2010-06-08 21:40:37 +02002114 lockdep_assert_held(&rq->lock);
2115
Peter Zijlstra2acca552011-04-05 17:23:50 +02002116 if (!raw_spin_trylock(&p->pi_lock)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002117 /*
2118 * This is OK, because current is on_cpu, which avoids it being
2119 * picked for load-balance and preemption/IRQs are still
2120 * disabled avoiding further scheduler activity on it and we've
2121 * not yet picked a replacement task.
2122 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002123 lockdep_unpin_lock(&rq->lock, cookie);
Peter Zijlstra2acca552011-04-05 17:23:50 +02002124 raw_spin_unlock(&rq->lock);
2125 raw_spin_lock(&p->pi_lock);
2126 raw_spin_lock(&rq->lock);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002127 lockdep_repin_lock(&rq->lock, cookie);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002128 }
Peter Zijlstra2acca552011-04-05 17:23:50 +02002129
Tejun Heo21aa9af2010-06-08 21:40:37 +02002130 if (!(p->state & TASK_NORMAL))
Peter Zijlstra2acca552011-04-05 17:23:50 +02002131 goto out;
Tejun Heo21aa9af2010-06-08 21:40:37 +02002132
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002133 trace_sched_waking(p);
2134
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002135 if (!task_on_rq_queued(p))
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002136 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
2137
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002138 ttwu_do_wakeup(rq, p, 0, cookie);
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05002139 ttwu_stat(p, smp_processor_id(), 0);
Peter Zijlstra2acca552011-04-05 17:23:50 +02002140out:
2141 raw_spin_unlock(&p->pi_lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002142}
2143
2144/**
David Howells50fa6102009-04-28 15:01:38 +01002145 * wake_up_process - Wake up a specific process
2146 * @p: The process to be woken up.
2147 *
2148 * Attempt to wake up the nominated process and move it to the set of runnable
Yacine Belkadie69f6182013-07-12 20:45:47 +02002149 * processes.
2150 *
2151 * Return: 1 if the process was woken up, 0 if it was already running.
David Howells50fa6102009-04-28 15:01:38 +01002152 *
2153 * It may be assumed that this function implies a write memory barrier before
2154 * changing the task state if and only if any tasks are woken up.
2155 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002156int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Oleg Nesterov9067ac82013-01-21 20:48:17 +01002158 return try_to_wake_up(p, TASK_NORMAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160EXPORT_SYMBOL(wake_up_process);
2161
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002162int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 return try_to_wake_up(p, state, 0);
2165}
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167/*
Juri Lellia5e7be32014-09-19 10:22:39 +01002168 * This function clears the sched_dl_entity static params.
2169 */
2170void __dl_clear_params(struct task_struct *p)
2171{
2172 struct sched_dl_entity *dl_se = &p->dl;
2173
2174 dl_se->dl_runtime = 0;
2175 dl_se->dl_deadline = 0;
2176 dl_se->dl_period = 0;
2177 dl_se->flags = 0;
2178 dl_se->dl_bw = 0;
Peter Zijlstra40767b02015-01-28 15:08:03 +01002179
2180 dl_se->dl_throttled = 0;
Peter Zijlstra40767b02015-01-28 15:08:03 +01002181 dl_se->dl_yielded = 0;
Juri Lellia5e7be32014-09-19 10:22:39 +01002182}
2183
2184/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 * Perform scheduler related setup for a newly forked process p.
2186 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002187 *
2188 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 */
Rik van Riel5e1576e2013-10-07 11:29:26 +01002190static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002192 p->on_rq = 0;
2193
2194 p->se.on_rq = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002195 p->se.exec_start = 0;
2196 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002197 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002198 p->se.nr_migrations = 0;
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002199 p->se.vruntime = 0;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002200 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002201
Byungchul Parkad936d82015-10-24 01:16:19 +09002202#ifdef CONFIG_FAIR_GROUP_SCHED
2203 p->se.cfs_rq = NULL;
2204#endif
2205
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002206#ifdef CONFIG_SCHEDSTATS
Mel Gormancb251762016-02-05 09:08:36 +00002207 /* Even if schedstat is disabled, there should not be garbage */
Lucas De Marchi41acab82010-03-10 23:37:45 -03002208 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002209#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002210
Dario Faggioliaab03e02013-11-28 11:14:43 +01002211 RB_CLEAR_NODE(&p->dl.rb_node);
Peter Zijlstra40767b02015-01-28 15:08:03 +01002212 init_dl_task_timer(&p->dl);
Juri Lellia5e7be32014-09-19 10:22:39 +01002213 __dl_clear_params(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002214
Peter Zijlstrafa717062008-01-25 21:08:27 +01002215 INIT_LIST_HEAD(&p->rt.run_list);
Peter Zijlstraff77e462016-01-18 15:27:07 +01002216 p->rt.timeout = 0;
2217 p->rt.time_slice = sched_rr_timeslice;
2218 p->rt.on_rq = 0;
2219 p->rt.on_list = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002220
Avi Kivitye107be32007-07-26 13:40:43 +02002221#ifdef CONFIG_PREEMPT_NOTIFIERS
2222 INIT_HLIST_HEAD(&p->preempt_notifiers);
2223#endif
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002224
2225#ifdef CONFIG_NUMA_BALANCING
2226 if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002227 p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002228 p->mm->numa_scan_seq = 0;
2229 }
2230
Rik van Riel5e1576e2013-10-07 11:29:26 +01002231 if (clone_flags & CLONE_VM)
2232 p->numa_preferred_nid = current->numa_preferred_nid;
2233 else
2234 p->numa_preferred_nid = -1;
2235
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002236 p->node_stamp = 0ULL;
2237 p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
Peter Zijlstra4b96a292012-10-25 14:16:47 +02002238 p->numa_scan_period = sysctl_numa_balancing_scan_delay;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002239 p->numa_work.next = &p->numa_work;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002240 p->numa_faults = NULL;
Rik van Riel7e2703e2014-01-27 17:03:45 -05002241 p->last_task_numa_placement = 0;
2242 p->last_sum_exec_runtime = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002243
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002244 p->numa_group = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002245#endif /* CONFIG_NUMA_BALANCING */
Ingo Molnardd41f592007-07-09 18:51:59 +02002246}
2247
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302248DEFINE_STATIC_KEY_FALSE(sched_numa_balancing);
2249
Mel Gorman1a687c22012-11-22 11:16:36 +00002250#ifdef CONFIG_NUMA_BALANCING
Mel Gorman3105b862012-11-23 11:23:49 +00002251
2252void set_numabalancing_state(bool enabled)
2253{
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302254 if (enabled)
2255 static_branch_enable(&sched_numa_balancing);
2256 else
2257 static_branch_disable(&sched_numa_balancing);
Mel Gorman3105b862012-11-23 11:23:49 +00002258}
Andi Kleen54a43d52014-01-23 15:53:13 -08002259
2260#ifdef CONFIG_PROC_SYSCTL
2261int sysctl_numa_balancing(struct ctl_table *table, int write,
2262 void __user *buffer, size_t *lenp, loff_t *ppos)
2263{
2264 struct ctl_table t;
2265 int err;
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302266 int state = static_branch_likely(&sched_numa_balancing);
Andi Kleen54a43d52014-01-23 15:53:13 -08002267
2268 if (write && !capable(CAP_SYS_ADMIN))
2269 return -EPERM;
2270
2271 t = *table;
2272 t.data = &state;
2273 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2274 if (err < 0)
2275 return err;
2276 if (write)
2277 set_numabalancing_state(state);
2278 return err;
2279}
2280#endif
2281#endif
Mel Gorman1a687c22012-11-22 11:16:36 +00002282
Mel Gormancb251762016-02-05 09:08:36 +00002283#ifdef CONFIG_SCHEDSTATS
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002284
2285DEFINE_STATIC_KEY_FALSE(sched_schedstats);
2286static bool __initdata __sched_schedstats = false;
2287
Mel Gormancb251762016-02-05 09:08:36 +00002288static void set_schedstats(bool enabled)
2289{
2290 if (enabled)
2291 static_branch_enable(&sched_schedstats);
2292 else
2293 static_branch_disable(&sched_schedstats);
2294}
2295
2296void force_schedstat_enabled(void)
2297{
2298 if (!schedstat_enabled()) {
2299 pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n");
2300 static_branch_enable(&sched_schedstats);
2301 }
2302}
2303
2304static int __init setup_schedstats(char *str)
2305{
2306 int ret = 0;
2307 if (!str)
2308 goto out;
2309
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002310 /*
2311 * This code is called before jump labels have been set up, so we can't
2312 * change the static branch directly just yet. Instead set a temporary
2313 * variable so init_schedstats() can do it later.
2314 */
Mel Gormancb251762016-02-05 09:08:36 +00002315 if (!strcmp(str, "enable")) {
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002316 __sched_schedstats = true;
Mel Gormancb251762016-02-05 09:08:36 +00002317 ret = 1;
2318 } else if (!strcmp(str, "disable")) {
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002319 __sched_schedstats = false;
Mel Gormancb251762016-02-05 09:08:36 +00002320 ret = 1;
2321 }
2322out:
2323 if (!ret)
2324 pr_warn("Unable to parse schedstats=\n");
2325
2326 return ret;
2327}
2328__setup("schedstats=", setup_schedstats);
2329
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002330static void __init init_schedstats(void)
2331{
2332 set_schedstats(__sched_schedstats);
2333}
2334
Mel Gormancb251762016-02-05 09:08:36 +00002335#ifdef CONFIG_PROC_SYSCTL
2336int sysctl_schedstats(struct ctl_table *table, int write,
2337 void __user *buffer, size_t *lenp, loff_t *ppos)
2338{
2339 struct ctl_table t;
2340 int err;
2341 int state = static_branch_likely(&sched_schedstats);
2342
2343 if (write && !capable(CAP_SYS_ADMIN))
2344 return -EPERM;
2345
2346 t = *table;
2347 t.data = &state;
2348 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2349 if (err < 0)
2350 return err;
2351 if (write)
2352 set_schedstats(state);
2353 return err;
2354}
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002355#endif /* CONFIG_PROC_SYSCTL */
2356#else /* !CONFIG_SCHEDSTATS */
2357static inline void init_schedstats(void) {}
2358#endif /* CONFIG_SCHEDSTATS */
Mel Gormancb251762016-02-05 09:08:36 +00002359
Ingo Molnardd41f592007-07-09 18:51:59 +02002360/*
2361 * fork()/clone()-time setup:
2362 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002363int sched_fork(unsigned long clone_flags, struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02002364{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002365 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002366 int cpu = get_cpu();
2367
Rik van Riel5e1576e2013-10-07 11:29:26 +01002368 __sched_fork(clone_flags, p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002369 /*
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02002370 * We mark the process as NEW here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002371 * nobody will actually run it, and a signal or other external
2372 * event cannot wake it up and insert it on the runqueue either.
2373 */
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02002374 p->state = TASK_NEW;
Ingo Molnardd41f592007-07-09 18:51:59 +02002375
Ingo Molnarb29739f2006-06-27 02:54:51 -07002376 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02002377 * Make sure we do not leak PI boosting priority to the child.
2378 */
2379 p->prio = current->normal_prio;
2380
2381 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002382 * Revert to default priority/policy on fork if requested.
2383 */
2384 if (unlikely(p->sched_reset_on_fork)) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01002385 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002386 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002387 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02002388 p->rt_priority = 0;
2389 } else if (PRIO_TO_NICE(p->static_prio) < 0)
2390 p->static_prio = NICE_TO_PRIO(0);
2391
2392 p->prio = p->normal_prio = __normal_prio(p);
2393 set_load_weight(p);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002394
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002395 /*
2396 * We don't need the reset flag anymore after the fork. It has
2397 * fulfilled its duty:
2398 */
2399 p->sched_reset_on_fork = 0;
2400 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02002401
Dario Faggioliaab03e02013-11-28 11:14:43 +01002402 if (dl_prio(p->prio)) {
2403 put_cpu();
2404 return -EAGAIN;
2405 } else if (rt_prio(p->prio)) {
2406 p->sched_class = &rt_sched_class;
2407 } else {
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002408 p->sched_class = &fair_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01002409 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07002410
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02002411 init_entity_runnable_average(&p->se);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002412
Peter Zijlstra86951592010-06-22 11:44:53 +02002413 /*
2414 * The child is not yet in the pid-hash so no cgroup attach races,
2415 * and the cgroup is pinned to this child due to cgroup_fork()
2416 * is ran before sched_fork().
2417 *
2418 * Silence PROVE_RCU.
2419 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002420 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrae210bff2016-06-16 18:51:48 +02002421 /*
2422 * We're setting the cpu for the first time, we don't migrate,
2423 * so use __set_task_cpu().
2424 */
2425 __set_task_cpu(p, cpu);
2426 if (p->sched_class->task_fork)
2427 p->sched_class->task_fork(p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002428 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002429
Naveen N. Raof6db8342015-06-25 23:53:37 +05302430#ifdef CONFIG_SCHED_INFO
Ingo Molnardd41f592007-07-09 18:51:59 +02002431 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002432 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02002434#if defined(CONFIG_SMP)
2435 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07002436#endif
Peter Zijlstra01028742013-08-14 14:55:46 +02002437 init_task_preempt_count(p);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002438#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05002439 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002440 RB_CLEAR_NODE(&p->pushable_dl_tasks);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002441#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002442
Nick Piggin476d1392005-06-25 14:57:29 -07002443 put_cpu();
Dario Faggioliaab03e02013-11-28 11:14:43 +01002444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Dario Faggioli332ac172013-11-07 14:43:45 +01002447unsigned long to_ratio(u64 period, u64 runtime)
2448{
2449 if (runtime == RUNTIME_INF)
2450 return 1ULL << 20;
2451
2452 /*
2453 * Doing this here saves a lot of checks in all
2454 * the calling paths, and returning zero seems
2455 * safe for them anyway.
2456 */
2457 if (period == 0)
2458 return 0;
2459
2460 return div64_u64(runtime << 20, period);
2461}
2462
2463#ifdef CONFIG_SMP
2464inline struct dl_bw *dl_bw_of(int i)
2465{
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -07002466 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2467 "sched RCU must be held");
Dario Faggioli332ac172013-11-07 14:43:45 +01002468 return &cpu_rq(i)->rd->dl_bw;
2469}
2470
Peter Zijlstrade212f12013-12-19 11:54:45 +01002471static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002472{
Peter Zijlstrade212f12013-12-19 11:54:45 +01002473 struct root_domain *rd = cpu_rq(i)->rd;
2474 int cpus = 0;
2475
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -07002476 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2477 "sched RCU must be held");
Peter Zijlstrade212f12013-12-19 11:54:45 +01002478 for_each_cpu_and(i, rd->span, cpu_active_mask)
2479 cpus++;
2480
2481 return cpus;
Dario Faggioli332ac172013-11-07 14:43:45 +01002482}
2483#else
2484inline struct dl_bw *dl_bw_of(int i)
2485{
2486 return &cpu_rq(i)->dl.dl_bw;
2487}
2488
Peter Zijlstrade212f12013-12-19 11:54:45 +01002489static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002490{
2491 return 1;
2492}
2493#endif
2494
Dario Faggioli332ac172013-11-07 14:43:45 +01002495/*
2496 * We must be sure that accepting a new task (or allowing changing the
2497 * parameters of an existing one) is consistent with the bandwidth
2498 * constraints. If yes, this function also accordingly updates the currently
2499 * allocated bandwidth to reflect the new situation.
2500 *
2501 * This function is called while holding p's rq->lock.
Peter Zijlstra40767b02015-01-28 15:08:03 +01002502 *
2503 * XXX we should delay bw change until the task's 0-lag point, see
2504 * __setparam_dl().
Dario Faggioli332ac172013-11-07 14:43:45 +01002505 */
2506static int dl_overflow(struct task_struct *p, int policy,
2507 const struct sched_attr *attr)
2508{
2509
2510 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
Steven Rostedt4df16382014-02-19 13:53:35 -05002511 u64 period = attr->sched_period ?: attr->sched_deadline;
Dario Faggioli332ac172013-11-07 14:43:45 +01002512 u64 runtime = attr->sched_runtime;
2513 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
Peter Zijlstrade212f12013-12-19 11:54:45 +01002514 int cpus, err = -1;
Dario Faggioli332ac172013-11-07 14:43:45 +01002515
Xunlei Pangfec148c2016-04-14 20:19:28 +08002516 /* !deadline task may carry old deadline bandwidth */
2517 if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
Dario Faggioli332ac172013-11-07 14:43:45 +01002518 return 0;
2519
2520 /*
2521 * Either if a task, enters, leave, or stays -deadline but changes
2522 * its parameters, we may need to update accordingly the total
2523 * allocated bandwidth of the container.
2524 */
2525 raw_spin_lock(&dl_b->lock);
Peter Zijlstrade212f12013-12-19 11:54:45 +01002526 cpus = dl_bw_cpus(task_cpu(p));
Dario Faggioli332ac172013-11-07 14:43:45 +01002527 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2528 !__dl_overflow(dl_b, cpus, 0, new_bw)) {
2529 __dl_add(dl_b, new_bw);
2530 err = 0;
2531 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2532 !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
2533 __dl_clear(dl_b, p->dl.dl_bw);
2534 __dl_add(dl_b, new_bw);
2535 err = 0;
2536 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2537 __dl_clear(dl_b, p->dl.dl_bw);
2538 err = 0;
2539 }
2540 raw_spin_unlock(&dl_b->lock);
2541
2542 return err;
2543}
2544
2545extern void init_dl_bw(struct dl_bw *dl_b);
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547/*
2548 * wake_up_new_task - wake up a newly created task for the first time.
2549 *
2550 * This function will do some initial scheduler statistics housekeeping
2551 * that must be done for every newly created context, then puts the task
2552 * on the runqueue and wakes it.
2553 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002554void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Peter Zijlstraeb580752015-07-31 21:28:18 +02002556 struct rq_flags rf;
Ingo Molnardd41f592007-07-09 18:51:59 +02002557 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002558
Peter Zijlstraeb580752015-07-31 21:28:18 +02002559 raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02002560 p->state = TASK_RUNNING;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002561#ifdef CONFIG_SMP
2562 /*
2563 * Fork balancing, do it here and not earlier because:
2564 * - cpus_allowed can change in the fork path
2565 * - any previously selected cpu might disappear through hotplug
Peter Zijlstrae210bff2016-06-16 18:51:48 +02002566 *
2567 * Use __set_task_cpu() to avoid calling sched_class::migrate_task_rq,
2568 * as we're not fully set-up yet.
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002569 */
Peter Zijlstrae210bff2016-06-16 18:51:48 +02002570 __set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002571#endif
Peter Zijlstrab7fa30c2016-06-09 15:07:50 +02002572 rq = __task_rq_lock(p, &rf);
Yuyang Du2b8c41d2016-03-30 04:30:56 +08002573 post_init_entity_util_avg(&p->se);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002575 activate_task(rq, p, 0);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002576 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002577 trace_sched_wakeup_new(p);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002578 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002579#ifdef CONFIG_SMP
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02002580 if (p->sched_class->task_woken) {
2581 /*
2582 * Nothing relies on rq->lock after this, so its fine to
2583 * drop it.
2584 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002585 lockdep_unpin_lock(&rq->lock, rf.cookie);
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002586 p->sched_class->task_woken(rq, p);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002587 lockdep_repin_lock(&rq->lock, rf.cookie);
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02002588 }
Steven Rostedt9a897c52008-01-25 21:08:22 +01002589#endif
Peter Zijlstraeb580752015-07-31 21:28:18 +02002590 task_rq_unlock(rq, p, &rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Avi Kivitye107be32007-07-26 13:40:43 +02002593#ifdef CONFIG_PREEMPT_NOTIFIERS
2594
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002595static struct static_key preempt_notifier_key = STATIC_KEY_INIT_FALSE;
2596
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02002597void preempt_notifier_inc(void)
2598{
2599 static_key_slow_inc(&preempt_notifier_key);
2600}
2601EXPORT_SYMBOL_GPL(preempt_notifier_inc);
2602
2603void preempt_notifier_dec(void)
2604{
2605 static_key_slow_dec(&preempt_notifier_key);
2606}
2607EXPORT_SYMBOL_GPL(preempt_notifier_dec);
2608
Avi Kivitye107be32007-07-26 13:40:43 +02002609/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002610 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002611 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002612 */
2613void preempt_notifier_register(struct preempt_notifier *notifier)
2614{
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02002615 if (!static_key_false(&preempt_notifier_key))
2616 WARN(1, "registering preempt_notifier while notifiers disabled\n");
2617
Avi Kivitye107be32007-07-26 13:40:43 +02002618 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2619}
2620EXPORT_SYMBOL_GPL(preempt_notifier_register);
2621
2622/**
2623 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002624 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002625 *
Mathieu Desnoyersd84525a2015-05-17 12:53:10 -04002626 * This is *not* safe to call from within a preemption notifier.
Avi Kivitye107be32007-07-26 13:40:43 +02002627 */
2628void preempt_notifier_unregister(struct preempt_notifier *notifier)
2629{
2630 hlist_del(&notifier->link);
2631}
2632EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2633
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002634static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02002635{
2636 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002637
Sasha Levinb67bfe02013-02-27 17:06:00 -08002638 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002639 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2640}
2641
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002642static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2643{
2644 if (static_key_false(&preempt_notifier_key))
2645 __fire_sched_in_preempt_notifiers(curr);
2646}
2647
Avi Kivitye107be32007-07-26 13:40:43 +02002648static void
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002649__fire_sched_out_preempt_notifiers(struct task_struct *curr,
2650 struct task_struct *next)
Avi Kivitye107be32007-07-26 13:40:43 +02002651{
2652 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002653
Sasha Levinb67bfe02013-02-27 17:06:00 -08002654 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002655 notifier->ops->sched_out(notifier, next);
2656}
2657
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002658static __always_inline void
2659fire_sched_out_preempt_notifiers(struct task_struct *curr,
2660 struct task_struct *next)
2661{
2662 if (static_key_false(&preempt_notifier_key))
2663 __fire_sched_out_preempt_notifiers(curr, next);
2664}
2665
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002666#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002667
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002668static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02002669{
2670}
2671
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002672static inline void
Avi Kivitye107be32007-07-26 13:40:43 +02002673fire_sched_out_preempt_notifiers(struct task_struct *curr,
2674 struct task_struct *next)
2675{
2676}
2677
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002678#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002681 * prepare_task_switch - prepare to switch tasks
2682 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002683 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002684 * @next: the task we are going to switch to.
2685 *
2686 * This is called with the rq lock held and interrupts off. It must
2687 * be paired with a subsequent finish_task_switch after the context
2688 * switch.
2689 *
2690 * prepare_task_switch sets up locking and calls architecture specific
2691 * hooks.
2692 */
Avi Kivitye107be32007-07-26 13:40:43 +02002693static inline void
2694prepare_task_switch(struct rq *rq, struct task_struct *prev,
2695 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002696{
Michael S. Tsirkin43148952013-09-22 17:20:54 +03002697 sched_info_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002698 perf_event_task_sched_out(prev, next);
Avi Kivitye107be32007-07-26 13:40:43 +02002699 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002700 prepare_lock_switch(rq, next);
2701 prepare_arch_switch(next);
2702}
2703
2704/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 * finish_task_switch - clean up after a task-switch
2706 * @prev: the thread we just switched away from.
2707 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002708 * finish_task_switch must be called after the context switch, paired
2709 * with a prepare_task_switch call before the context switch.
2710 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2711 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 *
2713 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002714 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 * with the lock held can cause deadlocks; see schedule() for
2716 * details.)
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002717 *
2718 * The context switch have flipped the stack from under us and restored the
2719 * local variables which were saved when this task called schedule() in the
2720 * past. prev == current is still correct but we need to recalculate this_rq
2721 * because prev may have moved to another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002723static struct rq *finish_task_switch(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 __releases(rq->lock)
2725{
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002726 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002728 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Peter Zijlstra609ca062015-09-28 17:52:18 +02002730 /*
2731 * The previous task will have left us with a preempt_count of 2
2732 * because it left us after:
2733 *
2734 * schedule()
2735 * preempt_disable(); // 1
2736 * __schedule()
2737 * raw_spin_lock_irq(&rq->lock) // 2
2738 *
2739 * Also, see FORK_PREEMPT_COUNT.
2740 */
Peter Zijlstrae2bf1c4b2015-09-29 12:18:46 +02002741 if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
2742 "corrupted preempt_count: %s/%d/0x%x\n",
2743 current->comm, current->pid, preempt_count()))
2744 preempt_count_set(FORK_PREEMPT_COUNT);
Peter Zijlstra609ca062015-09-28 17:52:18 +02002745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 rq->prev_mm = NULL;
2747
2748 /*
2749 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002750 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002751 * schedule one last time. The schedule call will never return, and
2752 * the scheduled task must drop that reference.
Peter Zijlstra95913d92015-09-29 14:45:09 +02002753 *
2754 * We must observe prev->state before clearing prev->on_cpu (in
2755 * finish_lock_switch), otherwise a concurrent wakeup can get prev
2756 * running on another CPU and we could rave with its RUNNING -> DEAD
2757 * transition, resulting in a double drop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002759 prev_state = prev->state;
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +02002760 vtime_task_switch(prev);
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002761 perf_event_task_sched_in(prev, current);
Nick Piggin4866cde2005-06-25 14:57:23 -07002762 finish_lock_switch(rq, prev);
Catalin Marinas01f23e12011-11-27 21:43:10 +00002763 finish_arch_post_lock_switch();
Steven Rostedte8fa1362008-01-25 21:08:05 +01002764
Avi Kivitye107be32007-07-26 13:40:43 +02002765 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (mm)
2767 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002768 if (unlikely(prev_state == TASK_DEAD)) {
Dario Faggiolie6c390f2013-11-07 14:43:35 +01002769 if (prev->sched_class->task_dead)
2770 prev->sched_class->task_dead(prev);
2771
bibo maoc6fd91f2006-03-26 01:38:20 -08002772 /*
2773 * Remove function-return probe instances associated with this
2774 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002775 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002776 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002778 }
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +02002779
Frederic Weisbeckerde734f82015-06-11 18:07:12 +02002780 tick_nohz_task_switch();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002781 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782}
2783
Gregory Haskins3f029d32009-07-29 11:08:47 -04002784#ifdef CONFIG_SMP
2785
Gregory Haskins3f029d32009-07-29 11:08:47 -04002786/* rq->lock is NOT held, but preemption is disabled */
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002787static void __balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04002788{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002789 struct callback_head *head, *next;
2790 void (*func)(struct rq *rq);
2791 unsigned long flags;
Gregory Haskins3f029d32009-07-29 11:08:47 -04002792
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002793 raw_spin_lock_irqsave(&rq->lock, flags);
2794 head = rq->balance_callback;
2795 rq->balance_callback = NULL;
2796 while (head) {
2797 func = (void (*)(struct rq *))head->func;
2798 next = head->next;
2799 head->next = NULL;
2800 head = next;
Gregory Haskins3f029d32009-07-29 11:08:47 -04002801
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002802 func(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002803 }
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002804 raw_spin_unlock_irqrestore(&rq->lock, flags);
2805}
2806
2807static inline void balance_callback(struct rq *rq)
2808{
2809 if (unlikely(rq->balance_callback))
2810 __balance_callback(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002811}
2812
2813#else
2814
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002815static inline void balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04002816{
2817}
2818
2819#endif
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821/**
2822 * schedule_tail - first thing a freshly forked thread must call.
2823 * @prev: the thread we just switched away from.
2824 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002825asmlinkage __visible void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 __releases(rq->lock)
2827{
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002828 struct rq *rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002829
Peter Zijlstra609ca062015-09-28 17:52:18 +02002830 /*
2831 * New tasks start with FORK_PREEMPT_COUNT, see there and
2832 * finish_task_switch() for details.
2833 *
2834 * finish_task_switch() will drop rq->lock() and lower preempt_count
2835 * and the preempt_enable() will end up enabling preemption (on
2836 * PREEMPT_COUNT kernels).
2837 */
2838
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002839 rq = finish_task_switch(prev);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002840 balance_callback(rq);
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002841 preempt_enable();
Steven Rostedtda19ab52009-07-29 00:21:22 -04002842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002844 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
2847/*
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002848 * context_switch - switch to the new MM and the new thread's register state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 */
Josh Poimboeuf04936942016-02-28 22:22:39 -06002850static __always_inline struct rq *
Ingo Molnar70b97a72006-07-03 00:25:42 -07002851context_switch(struct rq *rq, struct task_struct *prev,
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002852 struct task_struct *next, struct pin_cookie cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
Ingo Molnardd41f592007-07-09 18:51:59 +02002854 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Avi Kivitye107be32007-07-26 13:40:43 +02002856 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002857
Ingo Molnardd41f592007-07-09 18:51:59 +02002858 mm = next->mm;
2859 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002860 /*
2861 * For paravirt, this is coupled with an exit in switch_to to
2862 * combine the page table reload and the switch backend into
2863 * one hypercall.
2864 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002865 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002866
Heiko Carstens31915ab2010-09-16 14:42:25 +02002867 if (!mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 next->active_mm = oldmm;
2869 atomic_inc(&oldmm->mm_count);
2870 enter_lazy_tlb(oldmm, next);
2871 } else
Andy Lutomirskif98db602016-04-26 09:39:06 -07002872 switch_mm_irqs_off(oldmm, mm, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Heiko Carstens31915ab2010-09-16 14:42:25 +02002874 if (!prev->mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 rq->prev_mm = oldmm;
2877 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002878 /*
2879 * Since the runqueue lock will be released by the next
2880 * task (which is an invalid locking op but in the case
2881 * of the scheduler it's an obvious special-case), so we
2882 * do an early lockdep release here:
2883 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002884 lockdep_unpin_lock(&rq->lock, cookie);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002885 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
2887 /* Here we just switch the register state and the stack. */
2888 switch_to(prev, next, prev);
Ingo Molnardd41f592007-07-09 18:51:59 +02002889 barrier();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002890
2891 return finish_task_switch(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
2894/*
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002895 * nr_running and nr_context_switches:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 *
2897 * externally visible scheduler statistics: current number of runnable
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002898 * threads, total number of context switches performed since bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 */
2900unsigned long nr_running(void)
2901{
2902 unsigned long i, sum = 0;
2903
2904 for_each_online_cpu(i)
2905 sum += cpu_rq(i)->nr_running;
2906
2907 return sum;
2908}
2909
Tim Chen2ee507c2014-07-31 10:29:48 -07002910/*
2911 * Check if only the current task is running on the cpu.
Dominik Dingel00cc16332015-09-18 11:27:45 +02002912 *
2913 * Caution: this function does not check that the caller has disabled
2914 * preemption, thus the result might have a time-of-check-to-time-of-use
2915 * race. The caller is responsible to use it correctly, for example:
2916 *
2917 * - from a non-preemptable section (of course)
2918 *
2919 * - from a thread that is bound to a single CPU
2920 *
2921 * - in a loop with very short iterations (e.g. a polling loop)
Tim Chen2ee507c2014-07-31 10:29:48 -07002922 */
2923bool single_task_running(void)
2924{
Dominik Dingel00cc16332015-09-18 11:27:45 +02002925 return raw_rq()->nr_running == 1;
Tim Chen2ee507c2014-07-31 10:29:48 -07002926}
2927EXPORT_SYMBOL(single_task_running);
2928
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929unsigned long long nr_context_switches(void)
2930{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002931 int i;
2932 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002934 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 sum += cpu_rq(i)->nr_switches;
2936
2937 return sum;
2938}
2939
2940unsigned long nr_iowait(void)
2941{
2942 unsigned long i, sum = 0;
2943
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002944 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2946
2947 return sum;
2948}
2949
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002950unsigned long nr_iowait_cpu(int cpu)
Arjan van de Ven69d25872009-09-21 17:04:08 -07002951{
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002952 struct rq *this = cpu_rq(cpu);
Arjan van de Ven69d25872009-09-21 17:04:08 -07002953 return atomic_read(&this->nr_iowait);
2954}
2955
Mel Gorman372ba8c2014-08-06 14:19:21 +01002956void get_iowait_load(unsigned long *nr_waiters, unsigned long *load)
2957{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02002958 struct rq *rq = this_rq();
2959 *nr_waiters = atomic_read(&rq->nr_iowait);
2960 *load = rq->load.weight;
Mel Gorman372ba8c2014-08-06 14:19:21 +01002961}
2962
Ingo Molnardd41f592007-07-09 18:51:59 +02002963#ifdef CONFIG_SMP
2964
Ingo Molnar48f24c42006-07-03 00:25:40 -07002965/*
Peter Zijlstra38022902009-12-16 18:04:37 +01002966 * sched_exec - execve() is a valuable balancing opportunity, because at
2967 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 */
Peter Zijlstra38022902009-12-16 18:04:37 +01002969void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Peter Zijlstra38022902009-12-16 18:04:37 +01002971 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01002973 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01002974
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002975 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstraac66f542013-10-07 11:29:16 +01002976 dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002977 if (dest_cpu == smp_processor_id())
2978 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01002979
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002980 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02002981 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07002982
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002983 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
2984 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 return;
2986 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01002987unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002988 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991#endif
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993DEFINE_PER_CPU(struct kernel_stat, kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002994DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996EXPORT_PER_CPU_SYMBOL(kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002997EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999/*
Giovanni Gherdovich60756202016-08-05 10:21:56 +02003000 * The function fair_sched_class.update_curr accesses the struct curr
3001 * and its field curr->exec_start; when called from task_sched_runtime(),
3002 * we observe a high rate of cache misses in practice.
3003 * Prefetching this data results in improved performance.
3004 */
3005static inline void prefetch_curr_exec_start(struct task_struct *p)
3006{
3007#ifdef CONFIG_FAIR_GROUP_SCHED
3008 struct sched_entity *curr = (&p->se)->cfs_rq->curr;
3009#else
3010 struct sched_entity *curr = (&task_rq(p)->cfs)->curr;
3011#endif
3012 prefetch(curr);
3013 prefetch(&curr->exec_start);
3014}
3015
3016/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003017 * Return accounted runtime for the task.
3018 * In case the task is currently running, return the runtime plus current's
3019 * pending runtime that have not been accounted yet.
3020 */
3021unsigned long long task_sched_runtime(struct task_struct *p)
3022{
Peter Zijlstraeb580752015-07-31 21:28:18 +02003023 struct rq_flags rf;
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003024 struct rq *rq;
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01003025 u64 ns;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003026
Peter Zijlstra911b2892013-11-11 18:21:56 +01003027#if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
3028 /*
3029 * 64-bit doesn't need locks to atomically read a 64bit value.
3030 * So we have a optimization chance when the task's delta_exec is 0.
3031 * Reading ->on_cpu is racy, but this is ok.
3032 *
3033 * If we race with it leaving cpu, we'll take a lock. So we're correct.
3034 * If we race with it entering cpu, unaccounted time is 0. This is
3035 * indistinguishable from the read occurring a few cycles earlier.
Mike Galbraith4036ac12014-06-24 07:49:40 +02003036 * If we see ->on_cpu without ->on_rq, the task is leaving, and has
3037 * been accounted, so we're correct here as well.
Peter Zijlstra911b2892013-11-11 18:21:56 +01003038 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003039 if (!p->on_cpu || !task_on_rq_queued(p))
Peter Zijlstra911b2892013-11-11 18:21:56 +01003040 return p->se.sum_exec_runtime;
3041#endif
3042
Peter Zijlstraeb580752015-07-31 21:28:18 +02003043 rq = task_rq_lock(p, &rf);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01003044 /*
3045 * Must be ->curr _and_ ->on_rq. If dequeued, we would
3046 * project cycles that may never be accounted to this
3047 * thread, breaking clock_gettime().
3048 */
3049 if (task_current(rq, p) && task_on_rq_queued(p)) {
Giovanni Gherdovich60756202016-08-05 10:21:56 +02003050 prefetch_curr_exec_start(p);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01003051 update_rq_clock(rq);
3052 p->sched_class->update_curr(rq);
3053 }
3054 ns = p->se.sum_exec_runtime;
Peter Zijlstraeb580752015-07-31 21:28:18 +02003055 task_rq_unlock(rq, p, &rf);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003056
3057 return ns;
3058}
3059
Balbir Singh49048622008-09-05 18:12:23 +02003060/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003061 * This function gets called by the timer code, with HZ frequency.
3062 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08003063 */
3064void scheduler_tick(void)
3065{
Christoph Lameter7835b982006-12-10 02:20:22 -08003066 int cpu = smp_processor_id();
3067 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003068 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003069
3070 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08003071
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003072 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003073 update_rq_clock(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003074 curr->sched_class->task_tick(rq, curr, 0);
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +02003075 cpu_load_update_active(rq);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02003076 calc_global_load_tick(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003077 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02003078
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003079 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02003080
Christoph Lametere418e1c2006-12-10 02:20:23 -08003081#ifdef CONFIG_SMP
Suresh Siddha6eb57e02011-10-03 15:09:01 -07003082 rq->idle_balance = idle_cpu(cpu);
Daniel Lezcano7caff662014-01-06 12:34:38 +01003083 trigger_load_balance(rq);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003084#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02003085 rq_last_tick_reset(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02003088#ifdef CONFIG_NO_HZ_FULL
3089/**
3090 * scheduler_tick_max_deferment
3091 *
3092 * Keep at least one tick per second when a single
3093 * active task is running because the scheduler doesn't
3094 * yet completely support full dynticks environment.
3095 *
3096 * This makes sure that uptime, CFS vruntime, load
3097 * balancing, etc... continue to move forward, even
3098 * with a very low granularity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003099 *
3100 * Return: Maximum deferment in nanoseconds.
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02003101 */
3102u64 scheduler_tick_max_deferment(void)
3103{
3104 struct rq *rq = this_rq();
Jason Low316c1608d2015-04-28 13:00:20 -07003105 unsigned long next, now = READ_ONCE(jiffies);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02003106
3107 next = rq->last_sched_tick + HZ;
3108
3109 if (time_before_eq(next, now))
3110 return 0;
3111
Kevin Hilman8fe8ff02014-01-15 14:51:38 +01003112 return jiffies_to_nsecs(next - now);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02003113}
3114#endif
3115
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05003116#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
3117 defined(CONFIG_PREEMPT_TRACER))
Steven Rostedt47252cf2016-03-21 11:23:39 -04003118/*
3119 * If the value passed in is equal to the current preempt count
3120 * then we just disabled preemption. Start timing the latency.
3121 */
3122static inline void preempt_latency_start(int val)
3123{
3124 if (preempt_count() == val) {
3125 unsigned long ip = get_lock_parent_ip();
3126#ifdef CONFIG_DEBUG_PREEMPT
3127 current->preempt_disable_ip = ip;
3128#endif
3129 trace_preempt_off(CALLER_ADDR0, ip);
3130 }
3131}
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05003132
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003133void preempt_count_add(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003135#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 /*
3137 * Underflow?
3138 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003139 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3140 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003141#endif
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003142 __preempt_count_add(val);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003143#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 /*
3145 * Spinlock count overflowing soon?
3146 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003147 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3148 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003149#endif
Steven Rostedt47252cf2016-03-21 11:23:39 -04003150 preempt_latency_start(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003152EXPORT_SYMBOL(preempt_count_add);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003153NOKPROBE_SYMBOL(preempt_count_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Steven Rostedt47252cf2016-03-21 11:23:39 -04003155/*
3156 * If the value passed in equals to the current preempt count
3157 * then we just enabled preemption. Stop timing the latency.
3158 */
3159static inline void preempt_latency_stop(int val)
3160{
3161 if (preempt_count() == val)
3162 trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
3163}
3164
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003165void preempt_count_sub(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003167#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 /*
3169 * Underflow?
3170 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01003171 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003172 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /*
3174 * Is the spinlock portion underflowing?
3175 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003176 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3177 !(preempt_count() & PREEMPT_MASK)))
3178 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003179#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003180
Steven Rostedt47252cf2016-03-21 11:23:39 -04003181 preempt_latency_stop(val);
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003182 __preempt_count_sub(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003184EXPORT_SYMBOL(preempt_count_sub);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003185NOKPROBE_SYMBOL(preempt_count_sub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Steven Rostedt47252cf2016-03-21 11:23:39 -04003187#else
3188static inline void preempt_latency_start(int val) { }
3189static inline void preempt_latency_stop(int val) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190#endif
3191
3192/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003193 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003195static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
Vegard Nossumd1c6d142016-07-23 09:46:39 +02003197 /* Save this before calling printk(), since that will clobber it */
3198 unsigned long preempt_disable_ip = get_preempt_disable_ip(current);
3199
Dave Jones664dfa62011-12-22 16:39:30 -05003200 if (oops_in_progress)
3201 return;
3202
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01003203 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3204 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02003205
Ingo Molnardd41f592007-07-09 18:51:59 +02003206 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07003207 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02003208 if (irqs_disabled())
3209 print_irqtrace_events(prev);
Vegard Nossumd1c6d142016-07-23 09:46:39 +02003210 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)
3211 && in_atomic_preempt_off()) {
Thomas Gleixner8f47b182014-02-07 20:58:39 +01003212 pr_err("Preemption disabled at:");
Vegard Nossumd1c6d142016-07-23 09:46:39 +02003213 print_ip_sym(preempt_disable_ip);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01003214 pr_cont("\n");
3215 }
Daniel Bristot de Oliveira748c7202016-06-03 17:10:18 -03003216 if (panic_on_warn)
3217 panic("scheduling while atomic\n");
3218
Stephen Boyd6135fc12012-03-28 17:10:47 -07003219 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +10303220 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Ingo Molnardd41f592007-07-09 18:51:59 +02003221}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Ingo Molnardd41f592007-07-09 18:51:59 +02003223/*
3224 * Various schedule()-time debugging checks and statistics:
3225 */
3226static inline void schedule_debug(struct task_struct *prev)
3227{
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01003228#ifdef CONFIG_SCHED_STACK_END_CHECK
Jann Horn29d64552016-06-01 11:55:07 +02003229 if (task_stack_end_corrupted(prev))
3230 panic("corrupted stack end detected inside scheduler\n");
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01003231#endif
Peter Zijlstrab99def82015-09-28 18:02:03 +02003232
Peter Zijlstra1dc0fff2015-09-28 17:57:39 +02003233 if (unlikely(in_atomic_preempt_off())) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003234 __schedule_bug(prev);
Peter Zijlstra1dc0fff2015-09-28 17:57:39 +02003235 preempt_count_set(PREEMPT_DISABLED);
3236 }
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003237 rcu_sleep_check();
Ingo Molnardd41f592007-07-09 18:51:59 +02003238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3240
Josh Poimboeufae928822016-06-17 12:43:24 -05003241 schedstat_inc(this_rq()->sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02003242}
3243
3244/*
3245 * Pick up the highest-prio task:
3246 */
3247static inline struct task_struct *
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003248pick_next_task(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie)
Ingo Molnardd41f592007-07-09 18:51:59 +02003249{
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003250 const struct sched_class *class = &fair_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003251 struct task_struct *p;
3252
3253 /*
3254 * Optimization: we know that if all tasks are in
3255 * the fair class we can call that function directly:
3256 */
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003257 if (likely(prev->sched_class == class &&
Peter Zijlstra38033c32014-01-23 20:32:21 +01003258 rq->nr_running == rq->cfs.h_nr_running)) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003259 p = fair_sched_class.pick_next_task(rq, prev, cookie);
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02003260 if (unlikely(p == RETRY_TASK))
3261 goto again;
3262
3263 /* assumes fair_sched_class->next == idle_sched_class */
3264 if (unlikely(!p))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003265 p = idle_sched_class.pick_next_task(rq, prev, cookie);
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02003266
3267 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02003268 }
3269
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003270again:
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003271 for_each_class(class) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003272 p = class->pick_next_task(rq, prev, cookie);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003273 if (p) {
3274 if (unlikely(p == RETRY_TASK))
3275 goto again;
Ingo Molnardd41f592007-07-09 18:51:59 +02003276 return p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003277 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003278 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003279
3280 BUG(); /* the idle class will always have a runnable task */
Ingo Molnardd41f592007-07-09 18:51:59 +02003281}
3282
3283/*
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003284 * __schedule() is the main scheduler function.
Pekka Enbergedde96e2012-08-04 11:49:47 +03003285 *
3286 * The main means of driving the scheduler and thus entering this function are:
3287 *
3288 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
3289 *
3290 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
3291 * paths. For example, see arch/x86/entry_64.S.
3292 *
3293 * To drive preemption between tasks, the scheduler sets the flag in timer
3294 * interrupt handler scheduler_tick().
3295 *
3296 * 3. Wakeups don't really cause entry into schedule(). They add a
3297 * task to the run-queue and that's it.
3298 *
3299 * Now, if the new task added to the run-queue preempts the current
3300 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
3301 * called on the nearest possible occasion:
3302 *
3303 * - If the kernel is preemptible (CONFIG_PREEMPT=y):
3304 *
3305 * - in syscall or exception context, at the next outmost
3306 * preempt_enable(). (this might be as soon as the wake_up()'s
3307 * spin_unlock()!)
3308 *
3309 * - in IRQ context, return from interrupt-handler to
3310 * preemptible context
3311 *
3312 * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
3313 * then at the next:
3314 *
3315 * - cond_resched() call
3316 * - explicit schedule() call
3317 * - return from syscall or exception to user-space
3318 * - return from interrupt-handler to user-space
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003319 *
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003320 * WARNING: must be called with preemption disabled!
Ingo Molnardd41f592007-07-09 18:51:59 +02003321 */
Peter Zijlstra499d7952015-09-28 18:52:36 +02003322static void __sched notrace __schedule(bool preempt)
Ingo Molnardd41f592007-07-09 18:51:59 +02003323{
3324 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003325 unsigned long *switch_count;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003326 struct pin_cookie cookie;
Ingo Molnardd41f592007-07-09 18:51:59 +02003327 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02003328 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02003329
Ingo Molnardd41f592007-07-09 18:51:59 +02003330 cpu = smp_processor_id();
3331 rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003332 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02003333
Ingo Molnardd41f592007-07-09 18:51:59 +02003334 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Peter Zijlstra31656512008-07-18 18:01:23 +02003336 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02003337 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003338
Paul E. McKenney46a5d162015-10-07 09:10:48 -07003339 local_irq_disable();
3340 rcu_note_context_switch();
3341
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02003342 /*
3343 * Make sure that signal_pending_state()->signal_pending() below
3344 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
3345 * done by the caller to avoid the race with signal_wake_up().
3346 */
3347 smp_mb__before_spinlock();
Paul E. McKenney46a5d162015-10-07 09:10:48 -07003348 raw_spin_lock(&rq->lock);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003349 cookie = lockdep_pin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003351 rq->clock_skip_update <<= 1; /* promote REQ to ACT */
3352
Oleg Nesterov246d86b2010-05-19 14:57:11 +02003353 switch_count = &prev->nivcsw;
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003354 if (!preempt && prev->state) {
Tejun Heo21aa9af2010-06-08 21:40:37 +02003355 if (unlikely(signal_pending_state(prev->state, prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003356 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02003357 } else {
Peter Zijlstra2acca552011-04-05 17:23:50 +02003358 deactivate_task(rq, prev, DEQUEUE_SLEEP);
3359 prev->on_rq = 0;
3360
Tejun Heo21aa9af2010-06-08 21:40:37 +02003361 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02003362 * If a worker went to sleep, notify and ask workqueue
3363 * whether it wants to wake up a task to maintain
3364 * concurrency.
Tejun Heo21aa9af2010-06-08 21:40:37 +02003365 */
3366 if (prev->flags & PF_WQ_WORKER) {
3367 struct task_struct *to_wakeup;
3368
Alexander Gordeev9b7f6592016-03-02 12:53:31 +01003369 to_wakeup = wq_worker_sleeping(prev);
Tejun Heo21aa9af2010-06-08 21:40:37 +02003370 if (to_wakeup)
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003371 try_to_wake_up_local(to_wakeup, cookie);
Tejun Heo21aa9af2010-06-08 21:40:37 +02003372 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02003373 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003374 switch_count = &prev->nvcsw;
3375 }
3376
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003377 if (task_on_rq_queued(prev))
Peter Zijlstra606dba22012-02-11 06:05:00 +01003378 update_rq_clock(rq);
3379
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003380 next = pick_next_task(rq, prev, cookie);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01003381 clear_tsk_need_resched(prev);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02003382 clear_preempt_need_resched();
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003383 rq->clock_skip_update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 rq->nr_switches++;
3387 rq->curr = next;
3388 ++*switch_count;
3389
Peter Zijlstrac73464b2015-09-28 18:06:56 +02003390 trace_sched_switch(preempt, prev, next);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003391 rq = context_switch(rq, prev, next, cookie); /* unlocks the rq */
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003392 } else {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003393 lockdep_unpin_lock(&rq->lock, cookie);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003394 raw_spin_unlock_irq(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003397 balance_callback(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398}
Josh Poimboeuf8e05e962016-02-28 22:22:38 -06003399STACK_FRAME_NON_STANDARD(__schedule); /* switch_to() */
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003400
Peter Zijlstra9af65282016-09-13 18:37:29 +02003401void __noreturn do_task_dead(void)
3402{
3403 /*
3404 * The setting of TASK_RUNNING by try_to_wake_up() may be delayed
3405 * when the following two conditions become true.
3406 * - There is race condition of mmap_sem (It is acquired by
3407 * exit_mm()), and
3408 * - SMI occurs before setting TASK_RUNINNG.
3409 * (or hypervisor of virtual machine switches to other guest)
3410 * As a result, we may become TASK_RUNNING after becoming TASK_DEAD
3411 *
3412 * To avoid it, we have to wait for releasing tsk->pi_lock which
3413 * is held by try_to_wake_up()
3414 */
3415 smp_mb();
3416 raw_spin_unlock_wait(&current->pi_lock);
3417
3418 /* causes final put_task_struct in finish_task_switch(). */
3419 __set_current_state(TASK_DEAD);
3420 current->flags |= PF_NOFREEZE; /* tell freezer to ignore us */
3421 __schedule(false);
3422 BUG();
3423 /* Avoid "noreturn function does return". */
3424 for (;;)
3425 cpu_relax(); /* For when BUG is null */
3426}
3427
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003428static inline void sched_submit_work(struct task_struct *tsk)
3429{
Thomas Gleixner3c7d5182011-07-17 20:46:52 +02003430 if (!tsk->state || tsk_is_pi_blocked(tsk))
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003431 return;
3432 /*
3433 * If we are going to sleep and we have plugged IO queued,
3434 * make sure to submit it to avoid deadlocks.
3435 */
3436 if (blk_needs_flush_plug(tsk))
3437 blk_schedule_flush_plug(tsk);
3438}
3439
Andi Kleen722a9f92014-05-02 00:44:38 +02003440asmlinkage __visible void __sched schedule(void)
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003441{
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003442 struct task_struct *tsk = current;
3443
3444 sched_submit_work(tsk);
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003445 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003446 preempt_disable();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003447 __schedule(false);
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003448 sched_preempt_enable_no_resched();
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003449 } while (need_resched());
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451EXPORT_SYMBOL(schedule);
3452
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01003453#ifdef CONFIG_CONTEXT_TRACKING
Andi Kleen722a9f92014-05-02 00:44:38 +02003454asmlinkage __visible void __sched schedule_user(void)
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003455{
3456 /*
3457 * If we come here after a random call to set_need_resched(),
3458 * or we have been woken up remotely but the IPI has not yet arrived,
3459 * we haven't yet exited the RCU idle mode. Do it here manually until
3460 * we find a better solution.
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003461 *
3462 * NB: There are buggy callers of this function. Ideally we
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +01003463 * should warn if prev_state != CONTEXT_USER, but that will trigger
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003464 * too frequently to make sense yet.
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003465 */
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003466 enum ctx_state prev_state = exception_enter();
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003467 schedule();
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003468 exception_exit(prev_state);
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003469}
3470#endif
3471
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01003472/**
3473 * schedule_preempt_disabled - called with preemption disabled
3474 *
3475 * Returns with preemption disabled. Note: preempt_count must be 1
3476 */
3477void __sched schedule_preempt_disabled(void)
3478{
Thomas Gleixnerba74c142011-03-21 13:32:17 +01003479 sched_preempt_enable_no_resched();
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01003480 schedule();
3481 preempt_disable();
3482}
3483
Frederic Weisbecker06b1f802015-02-16 19:20:07 +01003484static void __sched notrace preempt_schedule_common(void)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003485{
3486 do {
Steven Rostedt47252cf2016-03-21 11:23:39 -04003487 /*
3488 * Because the function tracer can trace preempt_count_sub()
3489 * and it also uses preempt_enable/disable_notrace(), if
3490 * NEED_RESCHED is set, the preempt_enable_notrace() called
3491 * by the function tracer will call this function again and
3492 * cause infinite recursion.
3493 *
3494 * Preemption must be disabled here before the function
3495 * tracer can trace. Break up preempt_disable() into two
3496 * calls. One to disable preemption without fear of being
3497 * traced. The other to still record the preemption latency,
3498 * which can also be traced by the function tracer.
3499 */
Peter Zijlstra499d7952015-09-28 18:52:36 +02003500 preempt_disable_notrace();
Steven Rostedt47252cf2016-03-21 11:23:39 -04003501 preempt_latency_start(1);
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003502 __schedule(true);
Steven Rostedt47252cf2016-03-21 11:23:39 -04003503 preempt_latency_stop(1);
Peter Zijlstra499d7952015-09-28 18:52:36 +02003504 preempt_enable_no_resched_notrace();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003505
3506 /*
3507 * Check again in case we missed a preemption opportunity
3508 * between schedule and now.
3509 */
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003510 } while (need_resched());
3511}
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513#ifdef CONFIG_PREEMPT
3514/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003515 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003516 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 * occur there and call schedule directly.
3518 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003519asmlinkage __visible void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 /*
3522 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003523 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 */
Frederic Weisbeckerfbb00b52013-06-19 23:56:22 +02003525 if (likely(!preemptible()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 return;
3527
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003528 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09003530NOKPROBE_SYMBOL(preempt_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531EXPORT_SYMBOL(preempt_schedule);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003532
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003533/**
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003534 * preempt_schedule_notrace - preempt_schedule called by tracing
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003535 *
3536 * The tracing infrastructure uses preempt_enable_notrace to prevent
3537 * recursion and tracing preempt enabling caused by the tracing
3538 * infrastructure itself. But as tracing can happen in areas coming
3539 * from userspace or just about to enter userspace, a preempt enable
3540 * can occur before user_exit() is called. This will cause the scheduler
3541 * to be called when the system is still in usermode.
3542 *
3543 * To prevent this, the preempt_enable_notrace will use this function
3544 * instead of preempt_schedule() to exit user context if needed before
3545 * calling the scheduler.
3546 */
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003547asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003548{
3549 enum ctx_state prev_ctx;
3550
3551 if (likely(!preemptible()))
3552 return;
3553
3554 do {
Steven Rostedt47252cf2016-03-21 11:23:39 -04003555 /*
3556 * Because the function tracer can trace preempt_count_sub()
3557 * and it also uses preempt_enable/disable_notrace(), if
3558 * NEED_RESCHED is set, the preempt_enable_notrace() called
3559 * by the function tracer will call this function again and
3560 * cause infinite recursion.
3561 *
3562 * Preemption must be disabled here before the function
3563 * tracer can trace. Break up preempt_disable() into two
3564 * calls. One to disable preemption without fear of being
3565 * traced. The other to still record the preemption latency,
3566 * which can also be traced by the function tracer.
3567 */
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003568 preempt_disable_notrace();
Steven Rostedt47252cf2016-03-21 11:23:39 -04003569 preempt_latency_start(1);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003570 /*
3571 * Needs preempt disabled in case user_exit() is traced
3572 * and the tracer calls preempt_enable_notrace() causing
3573 * an infinite recursion.
3574 */
3575 prev_ctx = exception_enter();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003576 __schedule(true);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003577 exception_exit(prev_ctx);
3578
Steven Rostedt47252cf2016-03-21 11:23:39 -04003579 preempt_latency_stop(1);
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003580 preempt_enable_no_resched_notrace();
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003581 } while (need_resched());
3582}
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003583EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003584
Thomas Gleixner32e475d2013-11-21 12:41:44 +01003585#endif /* CONFIG_PREEMPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003588 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 * off of irq context.
3590 * Note, that this is called and return with irqs disabled. This will
3591 * protect us against recursive calling from irq.
3592 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003593asmlinkage __visible void __sched preempt_schedule_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003595 enum ctx_state prev_state;
Ingo Molnar6478d882008-01-25 21:08:33 +01003596
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003597 /* Catch callers which need to be fixed */
Peter Zijlstraf27dde82013-08-14 14:55:31 +02003598 BUG_ON(preempt_count() || !irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003600 prev_state = exception_enter();
3601
Andi Kleen3a5c3592007-10-15 17:00:14 +02003602 do {
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003603 preempt_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003604 local_irq_enable();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003605 __schedule(true);
Andi Kleen3a5c3592007-10-15 17:00:14 +02003606 local_irq_disable();
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003607 sched_preempt_enable_no_resched();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003608 } while (need_resched());
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003609
3610 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
Peter Zijlstra63859d42009-09-15 19:14:42 +02003613int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003614 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615{
Peter Zijlstra63859d42009-09-15 19:14:42 +02003616 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618EXPORT_SYMBOL(default_wake_function);
3619
Ingo Molnarb29739f2006-06-27 02:54:51 -07003620#ifdef CONFIG_RT_MUTEXES
3621
3622/*
3623 * rt_mutex_setprio - set the current priority of a task
3624 * @p: task
3625 * @prio: prio value (kernel-internal form)
3626 *
3627 * This function changes the 'effective' priority of a task. It does
3628 * not touch ->normal_prio like __setscheduler().
3629 *
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003630 * Used by the rt_mutex code to implement priority inheritance
3631 * logic. Call site only calls if the priority of the task changed.
Ingo Molnarb29739f2006-06-27 02:54:51 -07003632 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003633void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003634{
Peter Zijlstraff77e462016-01-18 15:27:07 +01003635 int oldprio, queued, running, queue_flag = DEQUEUE_SAVE | DEQUEUE_MOVE;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003636 const struct sched_class *prev_class;
Peter Zijlstraeb580752015-07-31 21:28:18 +02003637 struct rq_flags rf;
3638 struct rq *rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003639
Dario Faggioliaab03e02013-11-28 11:14:43 +01003640 BUG_ON(prio > MAX_PRIO);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003641
Peter Zijlstraeb580752015-07-31 21:28:18 +02003642 rq = __task_rq_lock(p, &rf);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003643
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003644 /*
3645 * Idle task boosting is a nono in general. There is one
3646 * exception, when PREEMPT_RT and NOHZ is active:
3647 *
3648 * The idle task calls get_next_timer_interrupt() and holds
3649 * the timer wheel base->lock on the CPU and another CPU wants
3650 * to access the timer (probably to cancel it). We can safely
3651 * ignore the boosting request, as the idle CPU runs this code
3652 * with interrupts disabled and will complete the lock
3653 * protected section without being interrupted. So there is no
3654 * real need to boost.
3655 */
3656 if (unlikely(p == rq->idle)) {
3657 WARN_ON(p != rq->curr);
3658 WARN_ON(p->pi_blocked_on);
3659 goto out_unlock;
3660 }
3661
Steven Rostedta8027072010-09-20 15:13:34 -04003662 trace_sched_pi_setprio(p, prio);
Andrew Mortond5f9f942007-05-08 20:27:06 -07003663 oldprio = p->prio;
Peter Zijlstraff77e462016-01-18 15:27:07 +01003664
3665 if (oldprio == prio)
3666 queue_flag &= ~DEQUEUE_MOVE;
3667
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003668 prev_class = p->sched_class;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003669 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003670 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003671 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01003672 dequeue_task(rq, p, queue_flag);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003673 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003674 put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02003675
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003676 /*
3677 * Boosting condition are:
3678 * 1. -rt task is running and holds mutex A
3679 * --> -dl task blocks on mutex A
3680 *
3681 * 2. -dl task is running and holds mutex A
3682 * --> -dl task blocks on mutex A and could preempt the
3683 * running task
3684 */
3685 if (dl_prio(prio)) {
Oleg Nesterov466af292014-06-06 18:52:06 +02003686 struct task_struct *pi_task = rt_mutex_get_top_task(p);
3687 if (!dl_prio(p->normal_prio) ||
3688 (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003689 p->dl.dl_boosted = 1;
Peter Zijlstraff77e462016-01-18 15:27:07 +01003690 queue_flag |= ENQUEUE_REPLENISH;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003691 } else
3692 p->dl.dl_boosted = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003693 p->sched_class = &dl_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003694 } else if (rt_prio(prio)) {
3695 if (dl_prio(oldprio))
3696 p->dl.dl_boosted = 0;
3697 if (oldprio < prio)
Peter Zijlstraff77e462016-01-18 15:27:07 +01003698 queue_flag |= ENQUEUE_HEAD;
Ingo Molnardd41f592007-07-09 18:51:59 +02003699 p->sched_class = &rt_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003700 } else {
3701 if (dl_prio(oldprio))
3702 p->dl.dl_boosted = 0;
Brian Silverman746db942015-02-18 16:23:56 -08003703 if (rt_prio(oldprio))
3704 p->rt.timeout = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003705 p->sched_class = &fair_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003706 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003707
Ingo Molnarb29739f2006-06-27 02:54:51 -07003708 p->prio = prio;
3709
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003710 if (running)
3711 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003712 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01003713 enqueue_task(rq, p, queue_flag);
Steven Rostedtcb469842008-01-25 21:08:22 +01003714
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003715 check_class_changed(rq, p, prev_class, oldprio);
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003716out_unlock:
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003717 preempt_disable(); /* avoid rq from going away on us */
Peter Zijlstraeb580752015-07-31 21:28:18 +02003718 __task_rq_unlock(rq, &rf);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003719
3720 balance_callback(rq);
3721 preempt_enable();
Ingo Molnarb29739f2006-06-27 02:54:51 -07003722}
Ingo Molnarb29739f2006-06-27 02:54:51 -07003723#endif
Dario Faggiolid50dde52013-11-07 14:43:36 +01003724
Ingo Molnar36c8b582006-07-03 00:25:41 -07003725void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003727 int old_prio, delta, queued;
Peter Zijlstraeb580752015-07-31 21:28:18 +02003728 struct rq_flags rf;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003729 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Dongsheng Yang75e45d52014-02-11 15:34:50 +08003731 if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 return;
3733 /*
3734 * We have to be careful, if called from sys_setpriority(),
3735 * the task might be in the middle of scheduling on another CPU.
3736 */
Peter Zijlstraeb580752015-07-31 21:28:18 +02003737 rq = task_rq_lock(p, &rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 /*
3739 * The RT priorities are set via sched_setscheduler(), but we still
3740 * allow the 'normal' nice value to be set - but as expected
3741 * it wont have any effect on scheduling until the task is
Dario Faggioliaab03e02013-11-28 11:14:43 +01003742 * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003744 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 p->static_prio = NICE_TO_PRIO(nice);
3746 goto out_unlock;
3747 }
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003748 queued = task_on_rq_queued(p);
3749 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02003750 dequeue_task(rq, p, DEQUEUE_SAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003753 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003754 old_prio = p->prio;
3755 p->prio = effective_prio(p);
3756 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003758 if (queued) {
Peter Zijlstra1de64442015-09-30 17:44:13 +02003759 enqueue_task(rq, p, ENQUEUE_RESTORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07003761 * If the task increased its priority or is running and
3762 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003764 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Kirill Tkhai88751252014-06-29 00:03:57 +04003765 resched_curr(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 }
3767out_unlock:
Peter Zijlstraeb580752015-07-31 21:28:18 +02003768 task_rq_unlock(rq, p, &rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770EXPORT_SYMBOL(set_user_nice);
3771
Matt Mackalle43379f2005-05-01 08:59:00 -07003772/*
3773 * can_nice - check if a task can reduce its nice value
3774 * @p: task
3775 * @nice: nice value
3776 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003777int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07003778{
Matt Mackall024f4742005-08-18 11:24:19 -07003779 /* convert nice value [19,-20] to rlimit style value [1,40] */
Dongsheng Yang7aa2c012014-05-08 18:33:49 +09003780 int nice_rlim = nice_to_rlimit(nice);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003781
Jiri Slaby78d7d402010-03-05 13:42:54 -08003782 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07003783 capable(CAP_SYS_NICE));
3784}
3785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786#ifdef __ARCH_WANT_SYS_NICE
3787
3788/*
3789 * sys_nice - change the priority of the current process.
3790 * @increment: priority increment
3791 *
3792 * sys_setpriority is a more generic, but much slower function that
3793 * does similar things.
3794 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003795SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003797 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 /*
3800 * Setpriority might change our priority at the same moment.
3801 * We don't have to worry. Conceptually one call occurs first
3802 * and we have a single winner.
3803 */
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003804 increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003805 nice = task_nice(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003807 nice = clamp_val(nice, MIN_NICE, MAX_NICE);
Matt Mackalle43379f2005-05-01 08:59:00 -07003808 if (increment < 0 && !can_nice(current, nice))
3809 return -EPERM;
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 retval = security_task_setnice(current, nice);
3812 if (retval)
3813 return retval;
3814
3815 set_user_nice(current, nice);
3816 return 0;
3817}
3818
3819#endif
3820
3821/**
3822 * task_prio - return the priority value of a given task.
3823 * @p: the task in question.
3824 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003825 * Return: The priority value as seen by users in /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 * RT tasks are offset by -200. Normal tasks are centered
3827 * around 0, value goes from -16 to +15.
3828 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003829int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
3831 return p->prio - MAX_RT_PRIO;
3832}
3833
3834/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 * idle_cpu - is a given cpu idle currently?
3836 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003837 *
3838 * Return: 1 if the CPU is currently idle. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 */
3840int idle_cpu(int cpu)
3841{
Thomas Gleixner908a3282011-09-15 15:32:06 +02003842 struct rq *rq = cpu_rq(cpu);
3843
3844 if (rq->curr != rq->idle)
3845 return 0;
3846
3847 if (rq->nr_running)
3848 return 0;
3849
3850#ifdef CONFIG_SMP
3851 if (!llist_empty(&rq->wake_list))
3852 return 0;
3853#endif
3854
3855 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856}
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858/**
3859 * idle_task - return the idle task for a given cpu.
3860 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003861 *
3862 * Return: The idle task for the cpu @cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003864struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
3866 return cpu_rq(cpu)->idle;
3867}
3868
3869/**
3870 * find_process_by_pid - find a process with a matching PID value.
3871 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003872 *
3873 * The task of @pid, if found. %NULL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003875static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003877 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878}
3879
Dario Faggioliaab03e02013-11-28 11:14:43 +01003880/*
3881 * This function initializes the sched_dl_entity of a newly becoming
3882 * SCHED_DEADLINE task.
3883 *
3884 * Only the static values are considered here, the actual runtime and the
3885 * absolute deadline will be properly calculated when the task is enqueued
3886 * for the first time with its new policy.
3887 */
3888static void
3889__setparam_dl(struct task_struct *p, const struct sched_attr *attr)
3890{
3891 struct sched_dl_entity *dl_se = &p->dl;
3892
Dario Faggioliaab03e02013-11-28 11:14:43 +01003893 dl_se->dl_runtime = attr->sched_runtime;
3894 dl_se->dl_deadline = attr->sched_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003895 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003896 dl_se->flags = attr->sched_flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01003897 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
Peter Zijlstra40767b02015-01-28 15:08:03 +01003898
3899 /*
3900 * Changing the parameters of a task is 'tricky' and we're not doing
3901 * the correct thing -- also see task_dead_dl() and switched_from_dl().
3902 *
3903 * What we SHOULD do is delay the bandwidth release until the 0-lag
3904 * point. This would include retaining the task_struct until that time
3905 * and change dl_overflow() to not immediately decrement the current
3906 * amount.
3907 *
3908 * Instead we retain the current runtime/deadline and let the new
3909 * parameters take effect after the current reservation period lapses.
3910 * This is safe (albeit pessimistic) because the 0-lag point is always
3911 * before the current scheduling deadline.
3912 *
3913 * We can still have temporary overloads because we do not delay the
3914 * change in bandwidth until that time; so admission control is
3915 * not on the safe side. It does however guarantee tasks will never
3916 * consume more than promised.
3917 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003918}
3919
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003920/*
3921 * sched_setparam() passes in -1 for its policy, to let the functions
3922 * it calls know not to change it.
3923 */
3924#define SETPARAM_POLICY -1
3925
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003926static void __setscheduler_params(struct task_struct *p,
3927 const struct sched_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
Dario Faggiolid50dde52013-11-07 14:43:36 +01003929 int policy = attr->sched_policy;
3930
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003931 if (policy == SETPARAM_POLICY)
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003932 policy = p->policy;
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 p->policy = policy;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003935
Dario Faggioliaab03e02013-11-28 11:14:43 +01003936 if (dl_policy(policy))
3937 __setparam_dl(p, attr);
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003938 else if (fair_policy(policy))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003939 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
3940
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003941 /*
3942 * __sched_setscheduler() ensures attr->sched_priority == 0 when
3943 * !rt_policy. Always setting this ensures that things like
3944 * getparam()/getattr() don't report silly values for !rt tasks.
3945 */
3946 p->rt_priority = attr->sched_priority;
Steven Rostedt383afd02014-03-11 19:24:20 -04003947 p->normal_prio = normal_prio(p);
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003948 set_load_weight(p);
3949}
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003950
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003951/* Actually do priority change: must hold pi & rq lock. */
3952static void __setscheduler(struct rq *rq, struct task_struct *p,
Thomas Gleixner0782e632015-05-05 19:49:49 +02003953 const struct sched_attr *attr, bool keep_boost)
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003954{
3955 __setscheduler_params(p, attr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003956
Steven Rostedt383afd02014-03-11 19:24:20 -04003957 /*
Thomas Gleixner0782e632015-05-05 19:49:49 +02003958 * Keep a potential priority boosting if called from
3959 * sched_setscheduler().
Steven Rostedt383afd02014-03-11 19:24:20 -04003960 */
Thomas Gleixner0782e632015-05-05 19:49:49 +02003961 if (keep_boost)
3962 p->prio = rt_mutex_get_effective_prio(p, normal_prio(p));
3963 else
3964 p->prio = normal_prio(p);
Steven Rostedt383afd02014-03-11 19:24:20 -04003965
Dario Faggioliaab03e02013-11-28 11:14:43 +01003966 if (dl_prio(p->prio))
3967 p->sched_class = &dl_sched_class;
3968 else if (rt_prio(p->prio))
Peter Zijlstraffd44db2009-11-10 20:12:01 +01003969 p->sched_class = &rt_sched_class;
3970 else
3971 p->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
Dario Faggioliaab03e02013-11-28 11:14:43 +01003973
3974static void
3975__getparam_dl(struct task_struct *p, struct sched_attr *attr)
3976{
3977 struct sched_dl_entity *dl_se = &p->dl;
3978
3979 attr->sched_priority = p->rt_priority;
3980 attr->sched_runtime = dl_se->dl_runtime;
3981 attr->sched_deadline = dl_se->dl_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003982 attr->sched_period = dl_se->dl_period;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003983 attr->sched_flags = dl_se->flags;
3984}
3985
3986/*
3987 * This function validates the new parameters of a -deadline task.
3988 * We ask for the deadline not being zero, and greater or equal
Harald Gustafsson755378a2013-11-07 14:43:40 +01003989 * than the runtime, as well as the period of being zero or
Dario Faggioli332ac172013-11-07 14:43:45 +01003990 * greater than deadline. Furthermore, we have to be sure that
Juri Lellib08278192014-05-13 14:11:31 +02003991 * user parameters are above the internal resolution of 1us (we
3992 * check sched_runtime only since it is always the smaller one) and
3993 * below 2^63 ns (we have to check both sched_deadline and
3994 * sched_period, as the latter can be zero).
Dario Faggioliaab03e02013-11-28 11:14:43 +01003995 */
3996static bool
3997__checkparam_dl(const struct sched_attr *attr)
3998{
Juri Lellib08278192014-05-13 14:11:31 +02003999 /* deadline != 0 */
4000 if (attr->sched_deadline == 0)
4001 return false;
4002
4003 /*
4004 * Since we truncate DL_SCALE bits, make sure we're at least
4005 * that big.
4006 */
4007 if (attr->sched_runtime < (1ULL << DL_SCALE))
4008 return false;
4009
4010 /*
4011 * Since we use the MSB for wrap-around and sign issues, make
4012 * sure it's not set (mind that period can be equal to zero).
4013 */
4014 if (attr->sched_deadline & (1ULL << 63) ||
4015 attr->sched_period & (1ULL << 63))
4016 return false;
4017
4018 /* runtime <= deadline <= period (if period != 0) */
4019 if ((attr->sched_period != 0 &&
4020 attr->sched_period < attr->sched_deadline) ||
4021 attr->sched_deadline < attr->sched_runtime)
4022 return false;
4023
4024 return true;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004025}
4026
David Howellsc69e8d92008-11-14 10:39:19 +11004027/*
4028 * check the target process has a UID that matches the current process's
4029 */
4030static bool check_same_owner(struct task_struct *p)
4031{
4032 const struct cred *cred = current_cred(), *pcred;
4033 bool match;
4034
4035 rcu_read_lock();
4036 pcred = __task_cred(p);
Eric W. Biederman9c806aa2012-02-02 18:54:02 -08004037 match = (uid_eq(cred->euid, pcred->euid) ||
4038 uid_eq(cred->euid, pcred->uid));
David Howellsc69e8d92008-11-14 10:39:19 +11004039 rcu_read_unlock();
4040 return match;
4041}
4042
Wanpeng Li75381602014-11-26 08:44:04 +08004043static bool dl_param_changed(struct task_struct *p,
4044 const struct sched_attr *attr)
4045{
4046 struct sched_dl_entity *dl_se = &p->dl;
4047
4048 if (dl_se->dl_runtime != attr->sched_runtime ||
4049 dl_se->dl_deadline != attr->sched_deadline ||
4050 dl_se->dl_period != attr->sched_period ||
4051 dl_se->flags != attr->sched_flags)
4052 return true;
4053
4054 return false;
4055}
4056
Dario Faggiolid50dde52013-11-07 14:43:36 +01004057static int __sched_setscheduler(struct task_struct *p,
4058 const struct sched_attr *attr,
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004059 bool user, bool pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
Steven Rostedt383afd02014-03-11 19:24:20 -04004061 int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
4062 MAX_RT_PRIO - 1 - attr->sched_priority;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004063 int retval, oldprio, oldpolicy = -1, queued, running;
Thomas Gleixner0782e632015-05-05 19:49:49 +02004064 int new_effective_prio, policy = attr->sched_policy;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004065 const struct sched_class *prev_class;
Peter Zijlstraeb580752015-07-31 21:28:18 +02004066 struct rq_flags rf;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004067 int reset_on_fork;
Peter Zijlstraff77e462016-01-18 15:27:07 +01004068 int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE;
Peter Zijlstraeb580752015-07-31 21:28:18 +02004069 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Steven Rostedt66e53932006-06-27 02:54:44 -07004071 /* may grab non-irq protected spin_locks */
4072 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073recheck:
4074 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02004075 if (policy < 0) {
4076 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004078 } else {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004079 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
Lennart Poetteringca94c442009-06-15 17:17:47 +02004080
Henrik Austad20f9cd22015-09-09 17:00:41 +02004081 if (!valid_policy(policy))
Lennart Poetteringca94c442009-06-15 17:17:47 +02004082 return -EINVAL;
4083 }
4084
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004085 if (attr->sched_flags & ~(SCHED_FLAG_RESET_ON_FORK))
4086 return -EINVAL;
4087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 /*
4089 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004090 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4091 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 */
Peter Zijlstra0bb040a2014-01-15 17:15:13 +01004093 if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
Dario Faggiolid50dde52013-11-07 14:43:36 +01004094 (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 return -EINVAL;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004096 if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
4097 (rt_policy(policy) != (attr->sched_priority != 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 return -EINVAL;
4099
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004100 /*
4101 * Allow unprivileged RT tasks to decrease priority:
4102 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10004103 if (user && !capable(CAP_SYS_NICE)) {
Dario Faggiolid50dde52013-11-07 14:43:36 +01004104 if (fair_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05004105 if (attr->sched_nice < task_nice(p) &&
Peter Zijlstraeaad4512014-01-16 17:54:25 +01004106 !can_nice(p, attr->sched_nice))
Dario Faggiolid50dde52013-11-07 14:43:36 +01004107 return -EPERM;
4108 }
4109
Ingo Molnare05606d2007-07-09 18:51:59 +02004110 if (rt_policy(policy)) {
Oleg Nesterova44702e82010-06-11 01:09:44 +02004111 unsigned long rlim_rtprio =
4112 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004113
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004114 /* can't set/change the rt policy */
4115 if (policy != p->policy && !rlim_rtprio)
4116 return -EPERM;
4117
4118 /* can't increase priority */
Dario Faggiolid50dde52013-11-07 14:43:36 +01004119 if (attr->sched_priority > p->rt_priority &&
4120 attr->sched_priority > rlim_rtprio)
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004121 return -EPERM;
4122 }
Darren Hartc02aa732011-02-17 15:37:07 -08004123
Juri Lellid44753b2014-03-03 12:09:21 +01004124 /*
4125 * Can't set/change SCHED_DEADLINE policy at all for now
4126 * (safest behavior); in the future we would like to allow
4127 * unprivileged DL tasks to increase their relative deadline
4128 * or reduce their runtime (both ways reducing utilization)
4129 */
4130 if (dl_policy(policy))
4131 return -EPERM;
4132
Ingo Molnardd41f592007-07-09 18:51:59 +02004133 /*
Darren Hartc02aa732011-02-17 15:37:07 -08004134 * Treat SCHED_IDLE as nice 20. Only allow a switch to
4135 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02004136 */
Henrik Austad20f9cd22015-09-09 17:00:41 +02004137 if (idle_policy(p->policy) && !idle_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05004138 if (!can_nice(p, task_nice(p)))
Darren Hartc02aa732011-02-17 15:37:07 -08004139 return -EPERM;
4140 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004141
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004142 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11004143 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004144 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004145
4146 /* Normal users shall not reset the sched_reset_on_fork flag */
4147 if (p->sched_reset_on_fork && !reset_on_fork)
4148 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004151 if (user) {
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004152 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07004153 if (retval)
4154 return retval;
4155 }
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004158 * make sure no PI-waiters arrive (or leave) while we are
4159 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004160 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004161 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 * runqueue lock must be held.
4163 */
Peter Zijlstraeb580752015-07-31 21:28:18 +02004164 rq = task_rq_lock(p, &rf);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004165
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004166 /*
4167 * Changing the policy of the stop threads its a very bad idea
4168 */
4169 if (p == rq->stop) {
Peter Zijlstraeb580752015-07-31 21:28:18 +02004170 task_rq_unlock(rq, p, &rf);
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004171 return -EINVAL;
4172 }
4173
Dario Faggiolia51e9192011-03-24 14:00:18 +01004174 /*
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01004175 * If not changing anything there's no need to proceed further,
4176 * but store a possible modification of reset_on_fork.
Dario Faggiolia51e9192011-03-24 14:00:18 +01004177 */
Dario Faggiolid50dde52013-11-07 14:43:36 +01004178 if (unlikely(policy == p->policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05004179 if (fair_policy(policy) && attr->sched_nice != task_nice(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01004180 goto change;
4181 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
4182 goto change;
Wanpeng Li75381602014-11-26 08:44:04 +08004183 if (dl_policy(policy) && dl_param_changed(p, attr))
Dario Faggioliaab03e02013-11-28 11:14:43 +01004184 goto change;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004185
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01004186 p->sched_reset_on_fork = reset_on_fork;
Peter Zijlstraeb580752015-07-31 21:28:18 +02004187 task_rq_unlock(rq, p, &rf);
Dario Faggiolia51e9192011-03-24 14:00:18 +01004188 return 0;
4189 }
Dario Faggiolid50dde52013-11-07 14:43:36 +01004190change:
Dario Faggiolia51e9192011-03-24 14:00:18 +01004191
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004192 if (user) {
Dario Faggioli332ac172013-11-07 14:43:45 +01004193#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004194 /*
4195 * Do not allow realtime tasks into groups that have no runtime
4196 * assigned.
4197 */
4198 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01004199 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
4200 !task_group_is_autogroup(task_group(p))) {
Peter Zijlstraeb580752015-07-31 21:28:18 +02004201 task_rq_unlock(rq, p, &rf);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004202 return -EPERM;
4203 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004204#endif
Dario Faggioli332ac172013-11-07 14:43:45 +01004205#ifdef CONFIG_SMP
4206 if (dl_bandwidth_enabled() && dl_policy(policy)) {
4207 cpumask_t *span = rq->rd->span;
Dario Faggioli332ac172013-11-07 14:43:45 +01004208
4209 /*
4210 * Don't allow tasks with an affinity mask smaller than
4211 * the entire root_domain to become SCHED_DEADLINE. We
4212 * will also fail if there's no bandwidth available.
4213 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004214 if (!cpumask_subset(span, &p->cpus_allowed) ||
4215 rq->rd->dl_bw.bw == 0) {
Peter Zijlstraeb580752015-07-31 21:28:18 +02004216 task_rq_unlock(rq, p, &rf);
Dario Faggioli332ac172013-11-07 14:43:45 +01004217 return -EPERM;
4218 }
4219 }
4220#endif
4221 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 /* recheck policy now with rq lock held */
4224 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4225 policy = oldpolicy = -1;
Peter Zijlstraeb580752015-07-31 21:28:18 +02004226 task_rq_unlock(rq, p, &rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 goto recheck;
4228 }
Dario Faggioli332ac172013-11-07 14:43:45 +01004229
4230 /*
4231 * If setscheduling to SCHED_DEADLINE (or changing the parameters
4232 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
4233 * is available.
4234 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004235 if ((dl_policy(policy) || dl_task(p)) && dl_overflow(p, policy, attr)) {
Peter Zijlstraeb580752015-07-31 21:28:18 +02004236 task_rq_unlock(rq, p, &rf);
Dario Faggioli332ac172013-11-07 14:43:45 +01004237 return -EBUSY;
4238 }
4239
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004240 p->sched_reset_on_fork = reset_on_fork;
4241 oldprio = p->prio;
4242
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004243 if (pi) {
4244 /*
4245 * Take priority boosted tasks into account. If the new
4246 * effective priority is unchanged, we just store the new
4247 * normal parameters and do not touch the scheduler class and
4248 * the runqueue. This will be done when the task deboost
4249 * itself.
4250 */
4251 new_effective_prio = rt_mutex_get_effective_prio(p, newprio);
Peter Zijlstraff77e462016-01-18 15:27:07 +01004252 if (new_effective_prio == oldprio)
4253 queue_flags &= ~DEQUEUE_MOVE;
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004254 }
4255
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004256 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004257 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004258 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01004259 dequeue_task(rq, p, queue_flags);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004260 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04004261 put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004262
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004263 prev_class = p->sched_class;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004264 __setscheduler(rq, p, attr, pi);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004265
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004266 if (running)
4267 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004268 if (queued) {
Thomas Gleixner81a44c52014-02-07 20:58:41 +01004269 /*
4270 * We enqueue to tail when the priority of a task is
4271 * increased (user space view).
4272 */
Peter Zijlstraff77e462016-01-18 15:27:07 +01004273 if (oldprio < p->prio)
4274 queue_flags |= ENQUEUE_HEAD;
Peter Zijlstra1de64442015-09-30 17:44:13 +02004275
Peter Zijlstraff77e462016-01-18 15:27:07 +01004276 enqueue_task(rq, p, queue_flags);
Thomas Gleixner81a44c52014-02-07 20:58:41 +01004277 }
Steven Rostedtcb469842008-01-25 21:08:22 +01004278
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004279 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02004280 preempt_disable(); /* avoid rq from going away on us */
Peter Zijlstraeb580752015-07-31 21:28:18 +02004281 task_rq_unlock(rq, p, &rf);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004282
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004283 if (pi)
4284 rt_mutex_adjust_pi(p);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004285
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02004286 /*
4287 * Run balance callbacks after we've adjusted the PI chain.
4288 */
4289 balance_callback(rq);
4290 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
4292 return 0;
4293}
Rusty Russell961ccdd2008-06-23 13:55:38 +10004294
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004295static int _sched_setscheduler(struct task_struct *p, int policy,
4296 const struct sched_param *param, bool check)
4297{
4298 struct sched_attr attr = {
4299 .sched_policy = policy,
4300 .sched_priority = param->sched_priority,
4301 .sched_nice = PRIO_TO_NICE(p->static_prio),
4302 };
4303
Steven Rostedtc13db6b2014-07-23 11:28:26 -04004304 /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
4305 if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004306 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
4307 policy &= ~SCHED_RESET_ON_FORK;
4308 attr.sched_policy = policy;
4309 }
4310
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004311 return __sched_setscheduler(p, &attr, check, true);
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004312}
Rusty Russell961ccdd2008-06-23 13:55:38 +10004313/**
4314 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
4315 * @p: the task in question.
4316 * @policy: new policy.
4317 * @param: structure containing the new RT priority.
4318 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004319 * Return: 0 on success. An error code otherwise.
4320 *
Rusty Russell961ccdd2008-06-23 13:55:38 +10004321 * NOTE that the task may be already dead.
4322 */
4323int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07004324 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10004325{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004326 return _sched_setscheduler(p, policy, param, true);
Rusty Russell961ccdd2008-06-23 13:55:38 +10004327}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328EXPORT_SYMBOL_GPL(sched_setscheduler);
4329
Dario Faggiolid50dde52013-11-07 14:43:36 +01004330int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
4331{
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004332 return __sched_setscheduler(p, attr, true, true);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004333}
4334EXPORT_SYMBOL_GPL(sched_setattr);
4335
Rusty Russell961ccdd2008-06-23 13:55:38 +10004336/**
4337 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
4338 * @p: the task in question.
4339 * @policy: new policy.
4340 * @param: structure containing the new RT priority.
4341 *
4342 * Just like sched_setscheduler, only don't bother checking if the
4343 * current context has permission. For example, this is needed in
4344 * stop_machine(): we create temporary high priority worker threads,
4345 * but our caller might not have that capability.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004346 *
4347 * Return: 0 on success. An error code otherwise.
Rusty Russell961ccdd2008-06-23 13:55:38 +10004348 */
4349int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07004350 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10004351{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004352 return _sched_setscheduler(p, policy, param, false);
Rusty Russell961ccdd2008-06-23 13:55:38 +10004353}
Davidlohr Bueso84778472015-09-02 01:28:44 -07004354EXPORT_SYMBOL_GPL(sched_setscheduler_nocheck);
Rusty Russell961ccdd2008-06-23 13:55:38 +10004355
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004356static int
4357do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 struct sched_param lparam;
4360 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004361 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
4363 if (!param || pid < 0)
4364 return -EINVAL;
4365 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4366 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004367
4368 rcu_read_lock();
4369 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004371 if (p != NULL)
4372 retval = sched_setscheduler(p, policy, &lparam);
4373 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 return retval;
4376}
4377
Dario Faggiolid50dde52013-11-07 14:43:36 +01004378/*
4379 * Mimics kernel/events/core.c perf_copy_attr().
4380 */
4381static int sched_copy_attr(struct sched_attr __user *uattr,
4382 struct sched_attr *attr)
4383{
4384 u32 size;
4385 int ret;
4386
4387 if (!access_ok(VERIFY_WRITE, uattr, SCHED_ATTR_SIZE_VER0))
4388 return -EFAULT;
4389
4390 /*
4391 * zero the full structure, so that a short copy will be nice.
4392 */
4393 memset(attr, 0, sizeof(*attr));
4394
4395 ret = get_user(size, &uattr->size);
4396 if (ret)
4397 return ret;
4398
4399 if (size > PAGE_SIZE) /* silly large */
4400 goto err_size;
4401
4402 if (!size) /* abi compat */
4403 size = SCHED_ATTR_SIZE_VER0;
4404
4405 if (size < SCHED_ATTR_SIZE_VER0)
4406 goto err_size;
4407
4408 /*
4409 * If we're handed a bigger struct than we know of,
4410 * ensure all the unknown bits are 0 - i.e. new
4411 * user-space does not rely on any kernel feature
4412 * extensions we dont know about yet.
4413 */
4414 if (size > sizeof(*attr)) {
4415 unsigned char __user *addr;
4416 unsigned char __user *end;
4417 unsigned char val;
4418
4419 addr = (void __user *)uattr + sizeof(*attr);
4420 end = (void __user *)uattr + size;
4421
4422 for (; addr < end; addr++) {
4423 ret = get_user(val, addr);
4424 if (ret)
4425 return ret;
4426 if (val)
4427 goto err_size;
4428 }
4429 size = sizeof(*attr);
4430 }
4431
4432 ret = copy_from_user(attr, uattr, size);
4433 if (ret)
4434 return -EFAULT;
4435
4436 /*
4437 * XXX: do we want to be lenient like existing syscalls; or do we want
4438 * to be strict and return an error on out-of-bounds values?
4439 */
Dongsheng Yang75e45d52014-02-11 15:34:50 +08004440 attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004441
Michael Kerriske78c7bc2014-05-09 16:54:28 +02004442 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004443
4444err_size:
4445 put_user(sizeof(*attr), &uattr->size);
Michael Kerriske78c7bc2014-05-09 16:54:28 +02004446 return -E2BIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004447}
4448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449/**
4450 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4451 * @pid: the pid in question.
4452 * @policy: new policy.
4453 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004454 *
4455 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004457SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
4458 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459{
Jason Baronc21761f2006-01-18 17:43:03 -08004460 /* negative values for policy are not valid */
4461 if (policy < 0)
4462 return -EINVAL;
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return do_sched_setscheduler(pid, policy, param);
4465}
4466
4467/**
4468 * sys_sched_setparam - set/change the RT priority of a thread
4469 * @pid: the pid in question.
4470 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004471 *
4472 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004474SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475{
Steven Rostedtc13db6b2014-07-23 11:28:26 -04004476 return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
4479/**
Dario Faggiolid50dde52013-11-07 14:43:36 +01004480 * sys_sched_setattr - same as above, but with extended sched_attr
4481 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01004482 * @uattr: structure containing the extended parameters.
Masanari Iidadb66d752014-04-18 01:59:15 +09004483 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004484 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004485SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
4486 unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004487{
4488 struct sched_attr attr;
4489 struct task_struct *p;
4490 int retval;
4491
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004492 if (!uattr || pid < 0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004493 return -EINVAL;
4494
Michael Kerrisk143cf232014-05-09 16:54:15 +02004495 retval = sched_copy_attr(uattr, &attr);
4496 if (retval)
4497 return retval;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004498
Richard Weinbergerb14ed2c2014-06-02 22:38:34 +02004499 if ((int)attr.sched_policy < 0)
Peter Zijlstradbdb2272014-05-09 10:49:03 +02004500 return -EINVAL;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004501
4502 rcu_read_lock();
4503 retval = -ESRCH;
4504 p = find_process_by_pid(pid);
4505 if (p != NULL)
4506 retval = sched_setattr(p, &attr);
4507 rcu_read_unlock();
4508
4509 return retval;
4510}
4511
4512/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4514 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004515 *
4516 * Return: On success, the policy of the thread. Otherwise, a negative error
4517 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004519SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004521 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004522 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
4524 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004525 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
4527 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004528 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 p = find_process_by_pid(pid);
4530 if (p) {
4531 retval = security_task_getscheduler(p);
4532 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02004533 retval = p->policy
4534 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004536 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 return retval;
4538}
4539
4540/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02004541 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 * @pid: the pid in question.
4543 * @param: structure containing the RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004544 *
4545 * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
4546 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004548SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02004550 struct sched_param lp = { .sched_priority = 0 };
Ingo Molnar36c8b582006-07-03 00:25:41 -07004551 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004552 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
4554 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004555 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004557 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 p = find_process_by_pid(pid);
4559 retval = -ESRCH;
4560 if (!p)
4561 goto out_unlock;
4562
4563 retval = security_task_getscheduler(p);
4564 if (retval)
4565 goto out_unlock;
4566
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02004567 if (task_has_rt_policy(p))
4568 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004569 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
4571 /*
4572 * This one might sleep, we cannot do it with a spinlock held ...
4573 */
4574 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 return retval;
4577
4578out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004579 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 return retval;
4581}
4582
Dario Faggiolid50dde52013-11-07 14:43:36 +01004583static int sched_read_attr(struct sched_attr __user *uattr,
4584 struct sched_attr *attr,
4585 unsigned int usize)
4586{
4587 int ret;
4588
4589 if (!access_ok(VERIFY_WRITE, uattr, usize))
4590 return -EFAULT;
4591
4592 /*
4593 * If we're handed a smaller struct than we know of,
4594 * ensure all the unknown bits are 0 - i.e. old
4595 * user-space does not get uncomplete information.
4596 */
4597 if (usize < sizeof(*attr)) {
4598 unsigned char *addr;
4599 unsigned char *end;
4600
4601 addr = (void *)attr + usize;
4602 end = (void *)attr + sizeof(*attr);
4603
4604 for (; addr < end; addr++) {
4605 if (*addr)
Michael Kerrisk22400672014-05-09 16:54:33 +02004606 return -EFBIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004607 }
4608
4609 attr->size = usize;
4610 }
4611
Vegard Nossum4efbc452014-02-16 22:24:17 +01004612 ret = copy_to_user(uattr, attr, attr->size);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004613 if (ret)
4614 return -EFAULT;
4615
Michael Kerrisk22400672014-05-09 16:54:33 +02004616 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004617}
4618
4619/**
Dario Faggioliaab03e02013-11-28 11:14:43 +01004620 * sys_sched_getattr - similar to sched_getparam, but with sched_attr
Dario Faggiolid50dde52013-11-07 14:43:36 +01004621 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01004622 * @uattr: structure containing the extended parameters.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004623 * @size: sizeof(attr) for fwd/bwd comp.
Masanari Iidadb66d752014-04-18 01:59:15 +09004624 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004625 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004626SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
4627 unsigned int, size, unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004628{
4629 struct sched_attr attr = {
4630 .size = sizeof(struct sched_attr),
4631 };
4632 struct task_struct *p;
4633 int retval;
4634
4635 if (!uattr || pid < 0 || size > PAGE_SIZE ||
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004636 size < SCHED_ATTR_SIZE_VER0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004637 return -EINVAL;
4638
4639 rcu_read_lock();
4640 p = find_process_by_pid(pid);
4641 retval = -ESRCH;
4642 if (!p)
4643 goto out_unlock;
4644
4645 retval = security_task_getscheduler(p);
4646 if (retval)
4647 goto out_unlock;
4648
4649 attr.sched_policy = p->policy;
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004650 if (p->sched_reset_on_fork)
4651 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004652 if (task_has_dl_policy(p))
4653 __getparam_dl(p, &attr);
4654 else if (task_has_rt_policy(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01004655 attr.sched_priority = p->rt_priority;
4656 else
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05004657 attr.sched_nice = task_nice(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004658
4659 rcu_read_unlock();
4660
4661 retval = sched_read_attr(uattr, &attr, size);
4662 return retval;
4663
4664out_unlock:
4665 rcu_read_unlock();
4666 return retval;
4667}
4668
Rusty Russell96f874e22008-11-25 02:35:14 +10304669long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304671 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004672 struct task_struct *p;
4673 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004675 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 p = find_process_by_pid(pid);
4678 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004679 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return -ESRCH;
4681 }
4682
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004683 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004685 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Tejun Heo14a40ff2013-03-19 13:45:20 -07004687 if (p->flags & PF_NO_SETAFFINITY) {
4688 retval = -EINVAL;
4689 goto out_put_task;
4690 }
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304691 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
4692 retval = -ENOMEM;
4693 goto out_put_task;
4694 }
4695 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
4696 retval = -ENOMEM;
4697 goto out_free_cpus_allowed;
4698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 retval = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004700 if (!check_same_owner(p)) {
4701 rcu_read_lock();
4702 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
4703 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004704 goto out_free_new_mask;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004705 }
4706 rcu_read_unlock();
4707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004709 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07004710 if (retval)
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004711 goto out_free_new_mask;
David Quigleye7834f82006-06-23 02:03:59 -07004712
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004713
4714 cpuset_cpus_allowed(p, cpus_allowed);
4715 cpumask_and(new_mask, in_mask, cpus_allowed);
4716
Dario Faggioli332ac172013-11-07 14:43:45 +01004717 /*
4718 * Since bandwidth control happens on root_domain basis,
4719 * if admission test is enabled, we only admit -deadline
4720 * tasks allowed to run on all the CPUs in the task's
4721 * root_domain.
4722 */
4723#ifdef CONFIG_SMP
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004724 if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
4725 rcu_read_lock();
4726 if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01004727 retval = -EBUSY;
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004728 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004729 goto out_free_new_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01004730 }
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004731 rcu_read_unlock();
Dario Faggioli332ac172013-11-07 14:43:45 +01004732 }
4733#endif
Peter Zijlstra49246272010-10-17 21:46:10 +02004734again:
Peter Zijlstra25834c72015-05-15 17:43:34 +02004735 retval = __set_cpus_allowed_ptr(p, new_mask, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Paul Menage8707d8b2007-10-18 23:40:22 -07004737 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304738 cpuset_cpus_allowed(p, cpus_allowed);
4739 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07004740 /*
4741 * We must have raced with a concurrent cpuset
4742 * update. Just reset the cpus_allowed to the
4743 * cpuset's cpus_allowed
4744 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304745 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004746 goto again;
4747 }
4748 }
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004749out_free_new_mask:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304750 free_cpumask_var(new_mask);
4751out_free_cpus_allowed:
4752 free_cpumask_var(cpus_allowed);
4753out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 return retval;
4756}
4757
4758static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e22008-11-25 02:35:14 +10304759 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
Rusty Russell96f874e22008-11-25 02:35:14 +10304761 if (len < cpumask_size())
4762 cpumask_clear(new_mask);
4763 else if (len > cpumask_size())
4764 len = cpumask_size();
4765
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4767}
4768
4769/**
4770 * sys_sched_setaffinity - set the cpu affinity of a process
4771 * @pid: pid of the process
4772 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4773 * @user_mask_ptr: user-space pointer to the new cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004774 *
4775 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004777SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
4778 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304780 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 int retval;
4782
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304783 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4784 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304786 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
4787 if (retval == 0)
4788 retval = sched_setaffinity(pid, new_mask);
4789 free_cpumask_var(new_mask);
4790 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791}
4792
Rusty Russell96f874e22008-11-25 02:35:14 +10304793long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004795 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00004796 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004799 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
4801 retval = -ESRCH;
4802 p = find_process_by_pid(pid);
4803 if (!p)
4804 goto out_unlock;
4805
David Quigleye7834f82006-06-23 02:03:59 -07004806 retval = security_task_getscheduler(p);
4807 if (retval)
4808 goto out_unlock;
4809
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004810 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02004811 cpumask_and(mask, &p->cpus_allowed, cpu_active_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004812 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
4814out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004815 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Ulrich Drepper9531b622007-08-09 11:16:46 +02004817 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818}
4819
4820/**
4821 * sys_sched_getaffinity - get the cpu affinity of a process
4822 * @pid: pid of the process
4823 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4824 * @user_mask_ptr: user-space pointer to hold the current cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004825 *
Zev Weiss599b4842016-06-26 16:13:23 -05004826 * Return: size of CPU mask copied to user_mask_ptr on success. An
4827 * error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004829SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
4830 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
4832 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10304833 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
Anton Blanchard84fba5e2010-04-06 17:02:19 +10004835 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004836 return -EINVAL;
4837 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 return -EINVAL;
4839
Rusty Russellf17c8602008-11-25 02:35:11 +10304840 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
4841 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Rusty Russellf17c8602008-11-25 02:35:11 +10304843 ret = sched_getaffinity(pid, mask);
4844 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09004845 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004846
4847 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10304848 ret = -EFAULT;
4849 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004850 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10304851 }
4852 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Rusty Russellf17c8602008-11-25 02:35:11 +10304854 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855}
4856
4857/**
4858 * sys_sched_yield - yield the current processor to other threads.
4859 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004860 * This function yields the current CPU to other tasks. If there are no
4861 * other threads running on this CPU then this function will return.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004862 *
4863 * Return: 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004865SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004867 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
Josh Poimboeufae928822016-06-17 12:43:24 -05004869 schedstat_inc(rq->yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004870 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
4872 /*
4873 * Since we are going to call schedule() anyway, there's
4874 * no need to preempt or enable interrupts:
4875 */
4876 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004877 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01004878 do_raw_spin_unlock(&rq->lock);
Thomas Gleixnerba74c142011-03-21 13:32:17 +01004879 sched_preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881 schedule();
4882
4883 return 0;
4884}
4885
Peter Zijlstra35a773a2016-09-19 12:57:53 +02004886#ifndef CONFIG_PREEMPT
Herbert Xu02b67cc32008-01-25 21:08:28 +01004887int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004889 if (should_resched(0)) {
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004890 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 return 1;
4892 }
4893 return 0;
4894}
Herbert Xu02b67cc32008-01-25 21:08:28 +01004895EXPORT_SYMBOL(_cond_resched);
Peter Zijlstra35a773a2016-09-19 12:57:53 +02004896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004899 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 * call schedule, and on return reacquire the lock.
4901 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004902 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 * operations here to prevent schedule() from being called twice (once via
4904 * spin_unlock(), once by hand).
4905 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004906int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004908 int resched = should_resched(PREEMPT_LOCK_OFFSET);
Jan Kara6df3cec2005-06-13 15:52:32 -07004909 int ret = 0;
4910
Peter Zijlstraf607c662009-07-20 19:16:29 +02004911 lockdep_assert_held(lock);
4912
Paul E. McKenney4a81e832014-06-20 16:49:01 -07004913 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004915 if (resched)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004916 preempt_schedule_common();
Nick Piggin95c354f2008-01-30 13:31:20 +01004917 else
4918 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004919 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004922 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004924EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004926int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927{
4928 BUG_ON(!in_softirq());
4929
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004930 if (should_resched(SOFTIRQ_DISABLE_OFFSET)) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004931 local_bh_enable();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004932 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 local_bh_disable();
4934 return 1;
4935 }
4936 return 0;
4937}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004938EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940/**
4941 * yield - yield the current processor to other threads.
4942 *
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01004943 * Do not ever use this function, there's a 99% chance you're doing it wrong.
4944 *
4945 * The scheduler is at all times free to pick the calling task as the most
4946 * eligible task to run, if removing the yield() call from your code breaks
4947 * it, its already broken.
4948 *
4949 * Typical broken usage is:
4950 *
4951 * while (!event)
4952 * yield();
4953 *
4954 * where one assumes that yield() will let 'the other' process run that will
4955 * make event true. If the current task is a SCHED_FIFO task that will never
4956 * happen. Never use yield() as a progress guarantee!!
4957 *
4958 * If you want to use yield() to wait for something, use wait_event().
4959 * If you want to use yield() to be 'nice' for others, use cond_resched().
4960 * If you still want to use yield(), do not!
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 */
4962void __sched yield(void)
4963{
4964 set_current_state(TASK_RUNNING);
4965 sys_sched_yield();
4966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967EXPORT_SYMBOL(yield);
4968
Mike Galbraithd95f4122011-02-01 09:50:51 -05004969/**
4970 * yield_to - yield the current processor to another thread in
4971 * your thread group, or accelerate that thread toward the
4972 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07004973 * @p: target task
4974 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05004975 *
4976 * It's the caller's job to ensure that the target task struct
4977 * can't go away on us before we can do any checks.
4978 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004979 * Return:
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304980 * true (>0) if we indeed boosted the target task.
4981 * false (0) if we failed to boost the target.
4982 * -ESRCH if there's no task to yield to.
Mike Galbraithd95f4122011-02-01 09:50:51 -05004983 */
Dan Carpenterfa933842014-05-23 13:20:42 +03004984int __sched yield_to(struct task_struct *p, bool preempt)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004985{
4986 struct task_struct *curr = current;
4987 struct rq *rq, *p_rq;
4988 unsigned long flags;
Dan Carpenterc3c18642013-02-05 14:37:51 +03004989 int yielded = 0;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004990
4991 local_irq_save(flags);
4992 rq = this_rq();
4993
4994again:
4995 p_rq = task_rq(p);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304996 /*
4997 * If we're the only runnable task on the rq and target rq also
4998 * has only one task, there's absolutely no point in yielding.
4999 */
5000 if (rq->nr_running == 1 && p_rq->nr_running == 1) {
5001 yielded = -ESRCH;
5002 goto out_irq;
5003 }
5004
Mike Galbraithd95f4122011-02-01 09:50:51 -05005005 double_rq_lock(rq, p_rq);
Shigeru Yoshida39e24d8f2013-11-23 18:38:01 +09005006 if (task_rq(p) != p_rq) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05005007 double_rq_unlock(rq, p_rq);
5008 goto again;
5009 }
5010
5011 if (!curr->sched_class->yield_to_task)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305012 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05005013
5014 if (curr->sched_class != p->sched_class)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305015 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05005016
5017 if (task_running(p_rq, p) || p->state)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305018 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05005019
5020 yielded = curr->sched_class->yield_to_task(rq, p, preempt);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08005021 if (yielded) {
Josh Poimboeufae928822016-06-17 12:43:24 -05005022 schedstat_inc(rq->yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08005023 /*
5024 * Make p's CPU reschedule; pick_next_entity takes care of
5025 * fairness.
5026 */
5027 if (preempt && rq != p_rq)
Kirill Tkhai88751252014-06-29 00:03:57 +04005028 resched_curr(p_rq);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08005029 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05005030
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305031out_unlock:
Mike Galbraithd95f4122011-02-01 09:50:51 -05005032 double_rq_unlock(rq, p_rq);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305033out_irq:
Mike Galbraithd95f4122011-02-01 09:50:51 -05005034 local_irq_restore(flags);
5035
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305036 if (yielded > 0)
Mike Galbraithd95f4122011-02-01 09:50:51 -05005037 schedule();
5038
5039 return yielded;
5040}
5041EXPORT_SYMBOL_GPL(yield_to);
5042
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005044 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047long __sched io_schedule_timeout(long timeout)
5048{
NeilBrown9cff8ad2015-02-13 15:49:17 +11005049 int old_iowait = current->in_iowait;
5050 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 long ret;
5052
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07005053 current->in_iowait = 1;
Shaohua Li10d784e2015-05-08 10:51:29 -07005054 blk_schedule_flush_plug(current);
NeilBrown9cff8ad2015-02-13 15:49:17 +11005055
5056 delayacct_blkio_start();
5057 rq = raw_rq();
5058 atomic_inc(&rq->nr_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 ret = schedule_timeout(timeout);
NeilBrown9cff8ad2015-02-13 15:49:17 +11005060 current->in_iowait = old_iowait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005062 delayacct_blkio_end();
NeilBrown9cff8ad2015-02-13 15:49:17 +11005063
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 return ret;
5065}
NeilBrown9cff8ad2015-02-13 15:49:17 +11005066EXPORT_SYMBOL(io_schedule_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068/**
5069 * sys_sched_get_priority_max - return maximum RT priority.
5070 * @policy: scheduling class.
5071 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02005072 * Return: On success, this syscall returns the maximum
5073 * rt_priority that can be used by a given scheduling class.
5074 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005076SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077{
5078 int ret = -EINVAL;
5079
5080 switch (policy) {
5081 case SCHED_FIFO:
5082 case SCHED_RR:
5083 ret = MAX_USER_RT_PRIO-1;
5084 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01005085 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005087 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005088 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 ret = 0;
5090 break;
5091 }
5092 return ret;
5093}
5094
5095/**
5096 * sys_sched_get_priority_min - return minimum RT priority.
5097 * @policy: scheduling class.
5098 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02005099 * Return: On success, this syscall returns the minimum
5100 * rt_priority that can be used by a given scheduling class.
5101 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005103SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104{
5105 int ret = -EINVAL;
5106
5107 switch (policy) {
5108 case SCHED_FIFO:
5109 case SCHED_RR:
5110 ret = 1;
5111 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01005112 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005114 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005115 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 ret = 0;
5117 }
5118 return ret;
5119}
5120
5121/**
5122 * sys_sched_rr_get_interval - return the default timeslice of a process.
5123 * @pid: pid of the process.
5124 * @interval: userspace pointer to the timeslice value.
5125 *
5126 * this syscall writes the default timeslice value of a given process
5127 * into the user-space timespec buffer. A value of '0' means infinity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005128 *
5129 * Return: On success, 0 and the timeslice is in @interval. Otherwise,
5130 * an error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01005132SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01005133 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005135 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005136 unsigned int time_slice;
Peter Zijlstraeb580752015-07-31 21:28:18 +02005137 struct rq_flags rf;
5138 struct timespec t;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01005139 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005140 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
5142 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005143 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
5145 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005146 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 p = find_process_by_pid(pid);
5148 if (!p)
5149 goto out_unlock;
5150
5151 retval = security_task_getscheduler(p);
5152 if (retval)
5153 goto out_unlock;
5154
Peter Zijlstraeb580752015-07-31 21:28:18 +02005155 rq = task_rq_lock(p, &rf);
Peter Zijlstraa57beec2014-01-27 11:54:13 +01005156 time_slice = 0;
5157 if (p->sched_class->get_rr_interval)
5158 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstraeb580752015-07-31 21:28:18 +02005159 task_rq_unlock(rq, p, &rf);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005160
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005161 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005162 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00005167 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 return retval;
5169}
5170
Steven Rostedt7c731e02008-05-12 21:20:41 +02005171static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005172
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005173void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 unsigned long free = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08005176 int ppid;
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09005177 unsigned long state = p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09005179 if (state)
5180 state = __ffs(state) + 1;
Erik Gilling28d06862010-11-19 18:08:51 -08005181 printk(KERN_INFO "%-15.15s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005182 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005183#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005185 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005187 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188#else
5189 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005190 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005192 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193#endif
5194#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05005195 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196#endif
Oleg Nesterova90e9842014-12-10 15:45:21 -08005197 ppid = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08005198 rcu_read_lock();
Oleg Nesterova90e9842014-12-10 15:45:21 -08005199 if (pid_alive(p))
5200 ppid = task_pid_nr(rcu_dereference(p->real_parent));
Paul E. McKenney4e797522012-11-07 13:35:32 -08005201 rcu_read_unlock();
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005202 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
Paul E. McKenney4e797522012-11-07 13:35:32 -08005203 task_pid_nr(p), ppid,
David Rientjesaa47b7e2009-05-04 01:38:05 -07005204 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
Tejun Heo3d1cb202013-04-30 15:27:22 -07005206 print_worker_info(KERN_INFO, p);
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005207 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208}
5209
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005210void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005212 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
Ingo Molnar4bd77322007-07-11 21:21:47 +02005214#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005215 printk(KERN_INFO
5216 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005218 printk(KERN_INFO
5219 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02005221 rcu_read_lock();
Oleg Nesterov5d07f422014-08-13 21:19:53 +02005222 for_each_process_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 /*
5224 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005225 * console might take a lot of time:
Andrey Ryabinin57675cb2016-06-09 15:20:05 +03005226 * Also, reset softlockup watchdogs on all CPUs, because
5227 * another CPU might be blocked waiting for us to process
5228 * an IPI.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 */
5230 touch_nmi_watchdog();
Andrey Ryabinin57675cb2016-06-09 15:20:05 +03005231 touch_all_softlockup_watchdogs();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005232 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005233 sched_show_task(p);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02005234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Ingo Molnardd41f592007-07-09 18:51:59 +02005236#ifdef CONFIG_SCHED_DEBUG
Rabin Vincentfb90a6e2016-04-04 15:42:02 +02005237 if (!state_filter)
5238 sysrq_sched_debug_show();
Ingo Molnardd41f592007-07-09 18:51:59 +02005239#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02005240 rcu_read_unlock();
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005241 /*
5242 * Only show locks if all tasks are dumped:
5243 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02005244 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005245 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246}
5247
Paul Gortmaker0db06282013-06-19 14:53:51 -04005248void init_idle_bootup_task(struct task_struct *idle)
Ingo Molnar1df21052007-07-09 18:51:58 +02005249{
Ingo Molnardd41f592007-07-09 18:51:59 +02005250 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005251}
5252
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005253/**
5254 * init_idle - set up an idle thread for a given CPU
5255 * @idle: task in question
5256 * @cpu: cpu the idle task belongs to
5257 *
5258 * NOTE: this function does not set the idle thread's NEED_RESCHED
5259 * flag, to make booting more robust.
5260 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005261void init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005263 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 unsigned long flags;
5265
Peter Zijlstra25834c72015-05-15 17:43:34 +02005266 raw_spin_lock_irqsave(&idle->pi_lock, flags);
5267 raw_spin_lock(&rq->lock);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01005268
Rik van Riel5e1576e2013-10-07 11:29:26 +01005269 __sched_fork(0, idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01005270 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02005271 idle->se.exec_start = sched_clock();
5272
Mark Rutlande1b77c92016-03-09 14:08:18 -08005273 kasan_unpoison_task_stack(idle);
5274
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02005275#ifdef CONFIG_SMP
5276 /*
5277 * Its possible that init_idle() gets called multiple times on a task,
5278 * in that case do_set_cpus_allowed() will not do the right thing.
5279 *
5280 * And since this is boot we can forgo the serialization.
5281 */
5282 set_cpus_allowed_common(idle, cpumask_of(cpu));
5283#endif
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005284 /*
5285 * We're having a chicken and egg problem, even though we are
5286 * holding rq->lock, the cpu isn't yet set to this cpu so the
5287 * lockdep check in task_group() will fail.
5288 *
5289 * Similar case to sched_fork(). / Alternatively we could
5290 * use task_rq_lock() here and obtain the other rq->lock.
5291 *
5292 * Silence PROVE_RCU
5293 */
5294 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02005295 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005296 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 rq->curr = rq->idle = idle;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005299 idle->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02005300#ifdef CONFIG_SMP
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02005301 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07005302#endif
Peter Zijlstra25834c72015-05-15 17:43:34 +02005303 raw_spin_unlock(&rq->lock);
5304 raw_spin_unlock_irqrestore(&idle->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
5306 /* Set the preempt count _outside_ the spinlocks! */
Peter Zijlstra01028742013-08-14 14:55:46 +02005307 init_idle_preempt_count(idle, cpu);
Jonathan Corbet625f2a32011-04-22 11:19:10 -06005308
Ingo Molnardd41f592007-07-09 18:51:59 +02005309 /*
5310 * The idle tasks have their own, simple scheduling class:
5311 */
5312 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05005313 ftrace_graph_init_idle_task(idle, cpu);
Frederic Weisbecker45eacc62013-05-15 22:16:32 +02005314 vtime_init_idle(idle, cpu);
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02005315#ifdef CONFIG_SMP
Carsten Emdef1c6f1a2011-10-26 23:14:16 +02005316 sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
5317#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318}
5319
Juri Lellif82f8042014-10-07 09:52:11 +01005320int cpuset_cpumask_can_shrink(const struct cpumask *cur,
5321 const struct cpumask *trial)
5322{
5323 int ret = 1, trial_cpus;
5324 struct dl_bw *cur_dl_b;
5325 unsigned long flags;
5326
Mike Galbraithbb2bc552015-01-28 04:53:55 +01005327 if (!cpumask_weight(cur))
5328 return ret;
5329
Juri Lelli75e23e492014-10-28 11:54:46 +00005330 rcu_read_lock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01005331 cur_dl_b = dl_bw_of(cpumask_any(cur));
5332 trial_cpus = cpumask_weight(trial);
5333
5334 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
5335 if (cur_dl_b->bw != -1 &&
5336 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
5337 ret = 0;
5338 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00005339 rcu_read_unlock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01005340
5341 return ret;
5342}
5343
Juri Lelli7f514122014-09-19 10:22:40 +01005344int task_can_attach(struct task_struct *p,
5345 const struct cpumask *cs_cpus_allowed)
5346{
5347 int ret = 0;
5348
5349 /*
5350 * Kthreads which disallow setaffinity shouldn't be moved
5351 * to a new cpuset; we don't want to change their cpu
5352 * affinity and isolating such threads by their set of
5353 * allowed nodes is unnecessary. Thus, cpusets are not
5354 * applicable for such threads. This prevents checking for
5355 * success of set_cpus_allowed_ptr() on all attached tasks
5356 * before cpus_allowed may be changed.
5357 */
5358 if (p->flags & PF_NO_SETAFFINITY) {
5359 ret = -EINVAL;
5360 goto out;
5361 }
5362
5363#ifdef CONFIG_SMP
5364 if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
5365 cs_cpus_allowed)) {
5366 unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
5367 cs_cpus_allowed);
Juri Lelli75e23e492014-10-28 11:54:46 +00005368 struct dl_bw *dl_b;
Juri Lelli7f514122014-09-19 10:22:40 +01005369 bool overflow;
5370 int cpus;
5371 unsigned long flags;
5372
Juri Lelli75e23e492014-10-28 11:54:46 +00005373 rcu_read_lock_sched();
5374 dl_b = dl_bw_of(dest_cpu);
Juri Lelli7f514122014-09-19 10:22:40 +01005375 raw_spin_lock_irqsave(&dl_b->lock, flags);
5376 cpus = dl_bw_cpus(dest_cpu);
5377 overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
5378 if (overflow)
5379 ret = -EBUSY;
5380 else {
5381 /*
5382 * We reserve space for this task in the destination
5383 * root_domain, as we can't fail after this point.
5384 * We will free resources in the source root_domain
5385 * later on (see set_cpus_allowed_dl()).
5386 */
5387 __dl_add(dl_b, p->dl.dl_bw);
5388 }
5389 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00005390 rcu_read_unlock_sched();
Juri Lelli7f514122014-09-19 10:22:40 +01005391
5392 }
5393#endif
5394out:
5395 return ret;
5396}
5397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01005400static bool sched_smp_initialized __read_mostly;
5401
Mel Gormane6628d52013-10-07 11:29:02 +01005402#ifdef CONFIG_NUMA_BALANCING
5403/* Migrate current task p to target_cpu */
5404int migrate_task_to(struct task_struct *p, int target_cpu)
5405{
5406 struct migration_arg arg = { p, target_cpu };
5407 int curr_cpu = task_cpu(p);
5408
5409 if (curr_cpu == target_cpu)
5410 return 0;
5411
5412 if (!cpumask_test_cpu(target_cpu, tsk_cpus_allowed(p)))
5413 return -EINVAL;
5414
5415 /* TODO: This is not properly updating schedstats */
5416
Mel Gorman286549d2014-01-21 15:51:03 -08005417 trace_sched_move_numa(p, curr_cpu, target_cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01005418 return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
5419}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005420
5421/*
5422 * Requeue a task on a given node and accurately track the number of NUMA
5423 * tasks on the runqueues
5424 */
5425void sched_setnuma(struct task_struct *p, int nid)
5426{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005427 bool queued, running;
Peter Zijlstraeb580752015-07-31 21:28:18 +02005428 struct rq_flags rf;
5429 struct rq *rq;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005430
Peter Zijlstraeb580752015-07-31 21:28:18 +02005431 rq = task_rq_lock(p, &rf);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005432 queued = task_on_rq_queued(p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005433 running = task_current(rq, p);
5434
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005435 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02005436 dequeue_task(rq, p, DEQUEUE_SAVE);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005437 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04005438 put_prev_task(rq, p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005439
5440 p->numa_preferred_nid = nid;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005441
5442 if (running)
5443 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005444 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02005445 enqueue_task(rq, p, ENQUEUE_RESTORE);
Peter Zijlstraeb580752015-07-31 21:28:18 +02005446 task_rq_unlock(rq, p, &rf);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005447}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02005448#endif /* CONFIG_NUMA_BALANCING */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
5450#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar48f24c42006-07-03 00:25:40 -07005451/*
5452 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 * offline.
5454 */
5455void idle_task_exit(void)
5456{
5457 struct mm_struct *mm = current->active_mm;
5458
5459 BUG_ON(cpu_online(smp_processor_id()));
5460
Martin Schwidefskya53efe52012-10-26 17:17:44 +02005461 if (mm != &init_mm) {
Andy Lutomirskif98db602016-04-26 09:39:06 -07005462 switch_mm_irqs_off(mm, &init_mm, current);
Martin Schwidefskya53efe52012-10-26 17:17:44 +02005463 finish_arch_post_lock_switch();
5464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 mmdrop(mm);
5466}
5467
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005468/*
Peter Zijlstra5d180232012-08-20 11:26:57 +02005469 * Since this CPU is going 'away' for a while, fold any nr_active delta
5470 * we might have. Assumes we're called after migrate_tasks() so that the
Thomas Gleixnerd60585c2016-07-12 18:33:56 +02005471 * nr_active count is stable. We need to take the teardown thread which
5472 * is calling this into account, so we hand in adjust = 1 to the load
5473 * calculation.
Peter Zijlstra5d180232012-08-20 11:26:57 +02005474 *
5475 * Also see the comment "Global load-average calculations".
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005476 */
Peter Zijlstra5d180232012-08-20 11:26:57 +02005477static void calc_load_migrate(struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478{
Thomas Gleixnerd60585c2016-07-12 18:33:56 +02005479 long delta = calc_load_fold_active(rq, 1);
Peter Zijlstra5d180232012-08-20 11:26:57 +02005480 if (delta)
5481 atomic_long_add(delta, &calc_load_tasks);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005482}
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005483
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005484static void put_prev_task_fake(struct rq *rq, struct task_struct *prev)
5485{
5486}
5487
5488static const struct sched_class fake_sched_class = {
5489 .put_prev_task = put_prev_task_fake,
5490};
5491
5492static struct task_struct fake_task = {
5493 /*
5494 * Avoid pull_{rt,dl}_task()
5495 */
5496 .prio = MAX_PRIO + 1,
5497 .sched_class = &fake_sched_class,
5498};
5499
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005500/*
5501 * Migrate all tasks from the rq, sleeping tasks will be migrated by
5502 * try_to_wake_up()->select_task_rq().
5503 *
5504 * Called with rq->lock held even though we'er in stop_machine() and
5505 * there's no concurrency possible, we hold the required locks anyway
5506 * because of lock validation efforts.
5507 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005508static void migrate_tasks(struct rq *dead_rq)
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005509{
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005510 struct rq *rq = dead_rq;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005511 struct task_struct *next, *stop = rq->stop;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005512 struct pin_cookie cookie;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005513 int dest_cpu;
5514
5515 /*
5516 * Fudge the rq selection such that the below task selection loop
5517 * doesn't get stuck on the currently eligible stop task.
5518 *
5519 * We're currently inside stop_machine() and the rq is either stuck
5520 * in the stop_machine_cpu_stop() loop, or we're executing this code,
5521 * either way we should never end up calling schedule() until we're
5522 * done here.
5523 */
5524 rq->stop = NULL;
5525
Frederic Weisbecker77bd3972013-04-12 01:50:58 +02005526 /*
5527 * put_prev_task() and pick_next_task() sched
5528 * class method both need to have an up-to-date
5529 * value of rq->clock[_task]
5530 */
5531 update_rq_clock(rq);
5532
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005533 for (;;) {
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005534 /*
5535 * There's this thread running, bail when that's the only
5536 * remaining thread.
5537 */
5538 if (rq->nr_running == 1)
5539 break;
5540
Peter Zijlstracbce1a62015-06-11 14:46:54 +02005541 /*
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08005542 * pick_next_task assumes pinned rq->lock.
Peter Zijlstracbce1a62015-06-11 14:46:54 +02005543 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005544 cookie = lockdep_pin_lock(&rq->lock);
5545 next = pick_next_task(rq, &fake_task, cookie);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005546 BUG_ON(!next);
5547 next->sched_class->put_prev_task(rq, next);
5548
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08005549 /*
5550 * Rules for changing task_struct::cpus_allowed are holding
5551 * both pi_lock and rq->lock, such that holding either
5552 * stabilizes the mask.
5553 *
5554 * Drop rq->lock is not quite as disastrous as it usually is
5555 * because !cpu_active at this point, which means load-balance
5556 * will not interfere. Also, stop-machine.
5557 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005558 lockdep_unpin_lock(&rq->lock, cookie);
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08005559 raw_spin_unlock(&rq->lock);
5560 raw_spin_lock(&next->pi_lock);
5561 raw_spin_lock(&rq->lock);
5562
5563 /*
5564 * Since we're inside stop-machine, _nothing_ should have
5565 * changed the task, WARN if weird stuff happened, because in
5566 * that case the above rq->lock drop is a fail too.
5567 */
5568 if (WARN_ON(task_rq(next) != rq || !task_on_rq_queued(next))) {
5569 raw_spin_unlock(&next->pi_lock);
5570 continue;
5571 }
5572
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005573 /* Find suitable destination for @next, with force if needed. */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005574 dest_cpu = select_fallback_rq(dead_rq->cpu, next);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005575
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005576 rq = __migrate_task(rq, next, dest_cpu);
5577 if (rq != dead_rq) {
5578 raw_spin_unlock(&rq->lock);
5579 rq = dead_rq;
5580 raw_spin_lock(&rq->lock);
5581 }
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08005582 raw_spin_unlock(&next->pi_lock);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005583 }
5584
5585 rq->stop = stop;
5586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587#endif /* CONFIG_HOTPLUG_CPU */
5588
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005589static void set_rq_online(struct rq *rq)
5590{
5591 if (!rq->online) {
5592 const struct sched_class *class;
5593
Rusty Russellc6c49272008-11-25 02:35:05 +10305594 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005595 rq->online = 1;
5596
5597 for_each_class(class) {
5598 if (class->rq_online)
5599 class->rq_online(rq);
5600 }
5601 }
5602}
5603
5604static void set_rq_offline(struct rq *rq)
5605{
5606 if (rq->online) {
5607 const struct sched_class *class;
5608
5609 for_each_class(class) {
5610 if (class->rq_offline)
5611 class->rq_offline(rq);
5612 }
5613
Rusty Russellc6c49272008-11-25 02:35:05 +10305614 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005615 rq->online = 0;
5616 }
5617}
5618
Thomas Gleixner9cf72432016-03-10 12:54:09 +01005619static void set_cpu_rq_start_time(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620{
Tejun Heo969c7922010-05-06 18:49:21 +02005621 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
Corey Minyarda803f022014-05-08 13:47:39 -05005623 rq->age_stamp = sched_clock_cpu(cpu);
5624}
5625
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005626static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
5627
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005628#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005629
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005630static __read_mostly int sched_debug_enabled;
Mike Travisf6630112009-11-17 18:22:15 -06005631
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005632static int __init sched_debug_setup(char *str)
Mike Travisf6630112009-11-17 18:22:15 -06005633{
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005634 sched_debug_enabled = 1;
Mike Travisf6630112009-11-17 18:22:15 -06005635
5636 return 0;
5637}
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005638early_param("sched_debug", sched_debug_setup);
5639
5640static inline bool sched_debug(void)
5641{
5642 return sched_debug_enabled;
5643}
Mike Travisf6630112009-11-17 18:22:15 -06005644
Mike Travis7c16ec52008-04-04 18:11:11 -07005645static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e22008-11-25 02:35:14 +10305646 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005647{
5648 struct sched_group *group = sd->groups;
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005649
Rusty Russell96f874e22008-11-25 02:35:14 +10305650 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005651
5652 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5653
5654 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005655 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005656 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005657 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5658 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005659 return -1;
5660 }
5661
Tejun Heo333470e2015-02-13 14:37:28 -08005662 printk(KERN_CONT "span %*pbl level %s\n",
5663 cpumask_pr_args(sched_domain_span(sd)), sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005664
Rusty Russell758b2cd2008-11-25 02:35:04 +10305665 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005666 printk(KERN_ERR "ERROR: domain->span does not contain "
5667 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005668 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10305669 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005670 printk(KERN_ERR "ERROR: domain->groups does not contain"
5671 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005672 }
5673
5674 printk(KERN_DEBUG "%*s groups:", level + 1, "");
5675 do {
5676 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005677 printk("\n");
5678 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005679 break;
5680 }
5681
Rusty Russell758b2cd2008-11-25 02:35:04 +10305682 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005683 printk(KERN_CONT "\n");
5684 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005685 break;
5686 }
5687
Peter Zijlstracb83b622012-04-17 15:49:36 +02005688 if (!(sd->flags & SD_OVERLAP) &&
5689 cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005690 printk(KERN_CONT "\n");
5691 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005692 break;
5693 }
5694
Rusty Russell758b2cd2008-11-25 02:35:04 +10305695 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005696
Tejun Heo333470e2015-02-13 14:37:28 -08005697 printk(KERN_CONT " %*pbl",
5698 cpumask_pr_args(sched_group_cpus(group)));
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005699 if (group->sgc->capacity != SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005700 printk(KERN_CONT " (cpu_capacity = %d)",
5701 group->sgc->capacity);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305702 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005703
5704 group = group->next;
5705 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005706 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005707
Rusty Russell758b2cd2008-11-25 02:35:04 +10305708 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005709 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005710
Rusty Russell758b2cd2008-11-25 02:35:04 +10305711 if (sd->parent &&
5712 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005713 printk(KERN_ERR "ERROR: parent span is not a superset "
5714 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005715 return 0;
5716}
5717
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718static void sched_domain_debug(struct sched_domain *sd, int cpu)
5719{
5720 int level = 0;
5721
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005722 if (!sched_debug_enabled)
Mike Travisf6630112009-11-17 18:22:15 -06005723 return;
5724
Nick Piggin41c7ce92005-06-25 14:57:24 -07005725 if (!sd) {
5726 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5727 return;
5728 }
5729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5731
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005732 for (;;) {
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005733 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 level++;
5736 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005737 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005738 break;
5739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005741#else /* !CONFIG_SCHED_DEBUG */
Peter Zijlstraa18a5792016-09-20 11:05:31 +02005742
5743# define sched_debug_enabled 0
Ingo Molnar48f24c42006-07-03 00:25:40 -07005744# define sched_domain_debug(sd, cpu) do { } while (0)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005745static inline bool sched_debug(void)
5746{
5747 return false;
5748}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005749#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005751static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005752{
Rusty Russell758b2cd2008-11-25 02:35:04 +10305753 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005754 return 1;
5755
5756 /* Following flags need at least 2 groups */
5757 if (sd->flags & (SD_LOAD_BALANCE |
5758 SD_BALANCE_NEWIDLE |
5759 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005760 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005761 SD_SHARE_CPUCAPACITY |
Morten Rasmussen1f6e6c7c2016-07-25 14:34:22 +01005762 SD_ASYM_CPUCAPACITY |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005763 SD_SHARE_PKG_RESOURCES |
5764 SD_SHARE_POWERDOMAIN)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005765 if (sd->groups != sd->groups->next)
5766 return 0;
5767 }
5768
5769 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005770 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005771 return 0;
5772
5773 return 1;
5774}
5775
Ingo Molnar48f24c42006-07-03 00:25:40 -07005776static int
5777sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005778{
5779 unsigned long cflags = sd->flags, pflags = parent->flags;
5780
5781 if (sd_degenerate(parent))
5782 return 1;
5783
Rusty Russell758b2cd2008-11-25 02:35:04 +10305784 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005785 return 0;
5786
Suresh Siddha245af2c2005-06-25 14:57:25 -07005787 /* Flags needing groups don't count if only 1 group in parent */
5788 if (parent->groups == parent->groups->next) {
5789 pflags &= ~(SD_LOAD_BALANCE |
5790 SD_BALANCE_NEWIDLE |
5791 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005792 SD_BALANCE_EXEC |
Morten Rasmussen1f6e6c7c2016-07-25 14:34:22 +01005793 SD_ASYM_CPUCAPACITY |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005794 SD_SHARE_CPUCAPACITY |
Peter Zijlstra10866e622013-08-19 16:57:04 +02005795 SD_SHARE_PKG_RESOURCES |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005796 SD_PREFER_SIBLING |
5797 SD_SHARE_POWERDOMAIN);
Ken Chen54364992008-12-07 18:47:37 -08005798 if (nr_node_ids == 1)
5799 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005800 }
5801 if (~cflags & pflags)
5802 return 0;
5803
5804 return 1;
5805}
5806
Peter Zijlstradce840a2011-04-07 14:09:50 +02005807static void free_rootdomain(struct rcu_head *rcu)
Rusty Russellc6c49272008-11-25 02:35:05 +10305808{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005809 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
Peter Zijlstra047106a2009-11-16 10:28:09 +01005810
Rusty Russell68e74562008-11-25 02:35:13 +10305811 cpupri_cleanup(&rd->cpupri);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005812 cpudl_cleanup(&rd->cpudl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005813 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305814 free_cpumask_var(rd->rto_mask);
5815 free_cpumask_var(rd->online);
5816 free_cpumask_var(rd->span);
5817 kfree(rd);
5818}
5819
Gregory Haskins57d885f2008-01-25 21:08:18 +01005820static void rq_attach_root(struct rq *rq, struct root_domain *rd)
5821{
Ingo Molnara0490fa2009-02-12 11:35:40 +01005822 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005823 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005824
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005825 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005826
5827 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01005828 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005829
Rusty Russellc6c49272008-11-25 02:35:05 +10305830 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005831 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005832
Rusty Russellc6c49272008-11-25 02:35:05 +10305833 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01005834
Ingo Molnara0490fa2009-02-12 11:35:40 +01005835 /*
Shigeru Yoshida05159732013-11-17 12:12:36 +09005836 * If we dont want to free the old_rd yet then
Ingo Molnara0490fa2009-02-12 11:35:40 +01005837 * set old_rd to NULL to skip the freeing later
5838 * in this function:
5839 */
5840 if (!atomic_dec_and_test(&old_rd->refcount))
5841 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005842 }
5843
5844 atomic_inc(&rd->refcount);
5845 rq->rd = rd;
5846
Rusty Russellc6c49272008-11-25 02:35:05 +10305847 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04005848 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005849 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005850
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005851 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01005852
5853 if (old_rd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02005854 call_rcu_sched(&old_rd->rcu, free_rootdomain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005855}
5856
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005857static int init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005858{
5859 memset(rd, 0, sizeof(*rd));
5860
Xunlei Pang8295c692015-12-02 19:52:59 +08005861 if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08005862 goto out;
Xunlei Pang8295c692015-12-02 19:52:59 +08005863 if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305864 goto free_span;
Xunlei Pang8295c692015-12-02 19:52:59 +08005865 if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305866 goto free_online;
Xunlei Pang8295c692015-12-02 19:52:59 +08005867 if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
Juri Lelli1baca4c2013-11-07 14:43:38 +01005868 goto free_dlo_mask;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02005869
Dario Faggioli332ac172013-11-07 14:43:45 +01005870 init_dl_bw(&rd->dl_bw);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005871 if (cpudl_init(&rd->cpudl) != 0)
5872 goto free_dlo_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01005873
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005874 if (cpupri_init(&rd->cpupri) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10305875 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10305876 return 0;
5877
Rusty Russell68e74562008-11-25 02:35:13 +10305878free_rto_mask:
5879 free_cpumask_var(rd->rto_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005880free_dlo_mask:
5881 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305882free_online:
5883 free_cpumask_var(rd->online);
5884free_span:
5885 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08005886out:
Rusty Russellc6c49272008-11-25 02:35:05 +10305887 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005888}
5889
Peter Zijlstra029632f2011-10-25 10:00:11 +02005890/*
5891 * By default the system creates a single root-domain with all cpus as
5892 * members (mimicking the global state we have today).
5893 */
5894struct root_domain def_root_domain;
5895
Gregory Haskins57d885f2008-01-25 21:08:18 +01005896static void init_defrootdomain(void)
5897{
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005898 init_rootdomain(&def_root_domain);
Rusty Russellc6c49272008-11-25 02:35:05 +10305899
Gregory Haskins57d885f2008-01-25 21:08:18 +01005900 atomic_set(&def_root_domain.refcount, 1);
5901}
5902
Gregory Haskinsdc938522008-01-25 21:08:26 +01005903static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005904{
5905 struct root_domain *rd;
5906
5907 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
5908 if (!rd)
5909 return NULL;
5910
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005911 if (init_rootdomain(rd) != 0) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305912 kfree(rd);
5913 return NULL;
5914 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01005915
5916 return rd;
5917}
5918
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005919static void free_sched_groups(struct sched_group *sg, int free_sgc)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005920{
5921 struct sched_group *tmp, *first;
5922
5923 if (!sg)
5924 return;
5925
5926 first = sg;
5927 do {
5928 tmp = sg->next;
5929
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005930 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
5931 kfree(sg->sgc);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005932
5933 kfree(sg);
5934 sg = tmp;
5935 } while (sg != first);
5936}
5937
Peter Zijlstradce840a2011-04-07 14:09:50 +02005938static void free_sched_domain(struct rcu_head *rcu)
5939{
5940 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005941
5942 /*
5943 * If its an overlapping domain it has private groups, iterate and
5944 * nuke them all.
5945 */
5946 if (sd->flags & SD_OVERLAP) {
5947 free_sched_groups(sd->groups, 1);
5948 } else if (atomic_dec_and_test(&sd->groups->ref)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005949 kfree(sd->groups->sgc);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005950 kfree(sd->groups);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005951 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005952 kfree(sd);
5953}
5954
Peter Zijlstraf39180e2016-05-09 10:37:54 +02005955static void destroy_sched_domain(struct sched_domain *sd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02005956{
5957 call_rcu(&sd->rcu, free_sched_domain);
5958}
5959
Peter Zijlstraf39180e2016-05-09 10:37:54 +02005960static void destroy_sched_domains(struct sched_domain *sd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02005961{
5962 for (; sd; sd = sd->parent)
Peter Zijlstraf39180e2016-05-09 10:37:54 +02005963 destroy_sched_domain(sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005964}
5965
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966/*
Peter Zijlstra518cd622011-12-07 15:07:31 +01005967 * Keep a special pointer to the highest sched_domain that has
5968 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
5969 * allows us to avoid some pointer chasing select_idle_sibling().
5970 *
5971 * Also keep a unique ID per domain (we use the first cpu number in
5972 * the cpumask of the domain), this allows us to quickly tell if
Peter Zijlstra39be3502012-01-26 12:44:34 +01005973 * two cpus are in the same cache domain, see cpus_share_cache().
Peter Zijlstra518cd622011-12-07 15:07:31 +01005974 */
5975DEFINE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005976DEFINE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005977DEFINE_PER_CPU(int, sd_llc_id);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005978DEFINE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305979DEFINE_PER_CPU(struct sched_domain *, sd_busy);
5980DEFINE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005981
5982static void update_top_cache_domain(int cpu)
5983{
5984 struct sched_domain *sd;
Mel Gorman5d4cf992013-12-17 09:21:25 +00005985 struct sched_domain *busy_sd = NULL;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005986 int id = cpu;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005987 int size = 1;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005988
5989 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005990 if (sd) {
Peter Zijlstra518cd622011-12-07 15:07:31 +01005991 id = cpumask_first(sched_domain_span(sd));
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005992 size = cpumask_weight(sched_domain_span(sd));
Mel Gorman5d4cf992013-12-17 09:21:25 +00005993 busy_sd = sd->parent; /* sd_busy */
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005994 }
Mel Gorman5d4cf992013-12-17 09:21:25 +00005995 rcu_assign_pointer(per_cpu(sd_busy, cpu), busy_sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005996
5997 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005998 per_cpu(sd_llc_size, cpu) = size;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005999 per_cpu(sd_llc_id, cpu) = id;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01006000
6001 sd = lowest_flag_domain(cpu, SD_NUMA);
6002 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306003
6004 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
6005 rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01006006}
6007
6008/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006009 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 * hold the hotplug lock.
6011 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006012static void
6013cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006015 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006016 struct sched_domain *tmp;
6017
6018 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08006019 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006020 struct sched_domain *parent = tmp->parent;
6021 if (!parent)
6022 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08006023
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006024 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006025 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006026 if (parent->parent)
6027 parent->parent->child = tmp;
Peter Zijlstra10866e622013-08-19 16:57:04 +02006028 /*
6029 * Transfer SD_PREFER_SIBLING down in case of a
6030 * degenerate parent; the spans match for this
6031 * so the property transfers.
6032 */
6033 if (parent->flags & SD_PREFER_SIBLING)
6034 tmp->flags |= SD_PREFER_SIBLING;
Peter Zijlstraf39180e2016-05-09 10:37:54 +02006035 destroy_sched_domain(parent);
Li Zefanf29c9b12008-11-06 09:45:16 +08006036 } else
6037 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006038 }
6039
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006040 if (sd && sd_degenerate(sd)) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02006041 tmp = sd;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006042 sd = sd->parent;
Peter Zijlstraf39180e2016-05-09 10:37:54 +02006043 destroy_sched_domain(tmp);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006044 if (sd)
6045 sd->child = NULL;
6046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Peter Zijlstra4cb98832011-04-07 14:09:58 +02006048 sched_domain_debug(sd, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
Gregory Haskins57d885f2008-01-25 21:08:18 +01006050 rq_attach_root(rq, rd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006051 tmp = rq->sd;
Nick Piggin674311d2005-06-25 14:57:27 -07006052 rcu_assign_pointer(rq->sd, sd);
Peter Zijlstraf39180e2016-05-09 10:37:54 +02006053 destroy_sched_domains(tmp);
Peter Zijlstra518cd622011-12-07 15:07:31 +01006054
6055 update_top_cache_domain(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056}
6057
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058/* Setup the mask of cpus configured for isolated domains */
6059static int __init isolated_cpu_setup(char *str)
6060{
Prarit Bhargavaa6e44912016-02-04 09:38:00 -05006061 int ret;
6062
Rusty Russellbdddd292009-12-02 14:09:16 +10306063 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Prarit Bhargavaa6e44912016-02-04 09:38:00 -05006064 ret = cpulist_parse(str, cpu_isolated_map);
6065 if (ret) {
6066 pr_err("sched: Error, all isolcpus= values must be between 0 and %d\n", nr_cpu_ids);
6067 return 0;
6068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 return 1;
6070}
Ingo Molnar8927f492007-10-15 17:00:13 +02006071__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006073struct s_data {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006074 struct sched_domain ** __percpu sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006075 struct root_domain *rd;
6076};
6077
Andreas Herrmann2109b992009-08-18 12:53:00 +02006078enum s_alloc {
Andreas Herrmann2109b992009-08-18 12:53:00 +02006079 sa_rootdomain,
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006080 sa_sd,
Peter Zijlstradce840a2011-04-07 14:09:50 +02006081 sa_sd_storage,
Andreas Herrmann2109b992009-08-18 12:53:00 +02006082 sa_none,
6083};
6084
Peter Zijlstrac1174872012-05-31 14:47:33 +02006085/*
6086 * Build an iteration mask that can exclude certain CPUs from the upwards
6087 * domain traversal.
6088 *
6089 * Asymmetric node setups can result in situations where the domain tree is of
6090 * unequal depth, make sure to skip domains that already cover the entire
6091 * range.
6092 *
6093 * In that case build_sched_domains() will have terminated the iteration early
6094 * and our sibling sd spans will be empty. Domains should always include the
6095 * cpu they're built on, so check that.
6096 *
6097 */
6098static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
6099{
6100 const struct cpumask *span = sched_domain_span(sd);
6101 struct sd_data *sdd = sd->private;
6102 struct sched_domain *sibling;
6103 int i;
6104
6105 for_each_cpu(i, span) {
6106 sibling = *per_cpu_ptr(sdd->sd, i);
6107 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
6108 continue;
6109
6110 cpumask_set_cpu(i, sched_group_mask(sg));
6111 }
6112}
6113
6114/*
6115 * Return the canonical balance cpu for this group, this is the first cpu
6116 * of this group that's also in the iteration mask.
6117 */
6118int group_balance_cpu(struct sched_group *sg)
6119{
6120 return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
6121}
6122
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006123static int
6124build_overlap_sched_groups(struct sched_domain *sd, int cpu)
6125{
6126 struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
6127 const struct cpumask *span = sched_domain_span(sd);
6128 struct cpumask *covered = sched_domains_tmpmask;
6129 struct sd_data *sdd = sd->private;
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006130 struct sched_domain *sibling;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006131 int i;
6132
6133 cpumask_clear(covered);
6134
6135 for_each_cpu(i, span) {
6136 struct cpumask *sg_span;
6137
6138 if (cpumask_test_cpu(i, covered))
6139 continue;
6140
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006141 sibling = *per_cpu_ptr(sdd->sd, i);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006142
6143 /* See the comment near build_group_mask(). */
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006144 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
Peter Zijlstrac1174872012-05-31 14:47:33 +02006145 continue;
6146
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006147 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
Suresh Siddha4d78a222011-11-18 15:03:29 -08006148 GFP_KERNEL, cpu_to_node(cpu));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006149
6150 if (!sg)
6151 goto fail;
6152
6153 sg_span = sched_group_cpus(sg);
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006154 if (sibling->child)
6155 cpumask_copy(sg_span, sched_domain_span(sibling->child));
6156 else
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006157 cpumask_set_cpu(i, sg_span);
6158
6159 cpumask_or(covered, covered, sg_span);
6160
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006161 sg->sgc = *per_cpu_ptr(sdd->sgc, i);
6162 if (atomic_inc_return(&sg->sgc->ref) == 1)
Peter Zijlstrac1174872012-05-31 14:47:33 +02006163 build_group_mask(sd, sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006164
Peter Zijlstrac3decf02012-05-31 12:05:32 +02006165 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006166 * Initialize sgc->capacity such that even if we mess up the
Peter Zijlstrac3decf02012-05-31 12:05:32 +02006167 * domains and no possible iteration will get us here, we won't
6168 * die on a /0 trap.
6169 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006170 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006171
6172 /*
6173 * Make sure the first group of this domain contains the
6174 * canonical balance cpu. Otherwise the sched_domain iteration
6175 * breaks. See update_sg_lb_stats().
6176 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02006177 if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
Peter Zijlstrac1174872012-05-31 14:47:33 +02006178 group_balance_cpu(sg) == cpu)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006179 groups = sg;
6180
6181 if (!first)
6182 first = sg;
6183 if (last)
6184 last->next = sg;
6185 last = sg;
6186 last->next = first;
6187 }
6188 sd->groups = groups;
6189
6190 return 0;
6191
6192fail:
6193 free_sched_groups(first, 0);
6194
6195 return -ENOMEM;
6196}
6197
Peter Zijlstradce840a2011-04-07 14:09:50 +02006198static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006200 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
6201 struct sched_domain *child = sd->child;
6202
6203 if (child)
6204 cpu = cpumask_first(sched_domain_span(child));
6205
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006206 if (sg) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02006207 *sg = *per_cpu_ptr(sdd->sg, cpu);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006208 (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
6209 atomic_set(&(*sg)->sgc->ref, 1); /* for claim_allocations */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006210 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006211
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 return cpu;
6213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
Ingo Molnar48f24c42006-07-03 00:25:40 -07006215/*
Peter Zijlstradce840a2011-04-07 14:09:50 +02006216 * build_sched_groups will build a circular linked list of the groups
6217 * covered by the given span, and will set each group's ->cpumask correctly,
Nicolas Pitreced549f2014-05-26 18:19:38 -04006218 * and ->cpu_capacity to 0.
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006219 *
6220 * Assumes the sched_domain tree is fully constructed
Ingo Molnar48f24c42006-07-03 00:25:40 -07006221 */
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006222static int
6223build_sched_groups(struct sched_domain *sd, int cpu)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006224{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006225 struct sched_group *first = NULL, *last = NULL;
6226 struct sd_data *sdd = sd->private;
6227 const struct cpumask *span = sched_domain_span(sd);
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006228 struct cpumask *covered;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006229 int i;
6230
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006231 get_group(cpu, sdd, &sd->groups);
6232 atomic_inc(&sd->groups->ref);
6233
Viresh Kumar09366292013-06-11 16:32:43 +05306234 if (cpu != cpumask_first(span))
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006235 return 0;
6236
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006237 lockdep_assert_held(&sched_domains_mutex);
6238 covered = sched_domains_tmpmask;
6239
Peter Zijlstradce840a2011-04-07 14:09:50 +02006240 cpumask_clear(covered);
6241
6242 for_each_cpu(i, span) {
6243 struct sched_group *sg;
Viresh Kumarcd08e922013-06-11 16:32:44 +05306244 int group, j;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006245
6246 if (cpumask_test_cpu(i, covered))
6247 continue;
6248
Viresh Kumarcd08e922013-06-11 16:32:44 +05306249 group = get_group(i, sdd, &sg);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006250 cpumask_setall(sched_group_mask(sg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02006251
6252 for_each_cpu(j, span) {
6253 if (get_group(j, sdd, NULL) != group)
6254 continue;
6255
6256 cpumask_set_cpu(j, covered);
6257 cpumask_set_cpu(j, sched_group_cpus(sg));
6258 }
6259
6260 if (!first)
6261 first = sg;
6262 if (last)
6263 last->next = sg;
6264 last = sg;
6265 }
6266 last->next = first;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006267
6268 return 0;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006269}
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006270
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271/*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006272 * Initialize sched groups cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006273 *
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006274 * cpu_capacity indicates the capacity of sched group, which is used while
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006275 * distributing the load between different sched groups in a sched domain.
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006276 * Typically cpu_capacity for all the groups in a sched domain will be same
6277 * unless there are asymmetries in the topology. If there are asymmetries,
6278 * group having more cpu_capacity will pickup more load compared to the
6279 * group having less cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006280 */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006281static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006282{
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006283 struct sched_group *sg = sd->groups;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006284
Viresh Kumar94c95ba2013-06-11 16:32:45 +05306285 WARN_ON(!sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006286
6287 do {
6288 sg->group_weight = cpumask_weight(sched_group_cpus(sg));
6289 sg = sg->next;
6290 } while (sg != sd->groups);
6291
Peter Zijlstrac1174872012-05-31 14:47:33 +02006292 if (cpu != group_balance_cpu(sg))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006293 return;
6294
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006295 update_group_capacity(sd, cpu);
6296 atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006297}
6298
6299/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006300 * Initializers for schedule domains
6301 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6302 */
6303
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006304static int default_relax_domain_level = -1;
Peter Zijlstra60495e72011-04-07 14:10:04 +02006305int sched_domain_level_max;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006306
6307static int __init setup_relax_domain_level(char *str)
6308{
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006309 if (kstrtoint(str, 0, &default_relax_domain_level))
6310 pr_warn("Unable to set relax_domain_level\n");
Li Zefan30e0e172008-05-13 10:27:17 +08006311
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006312 return 1;
6313}
6314__setup("relax_domain_level=", setup_relax_domain_level);
6315
6316static void set_domain_attribute(struct sched_domain *sd,
6317 struct sched_domain_attr *attr)
6318{
6319 int request;
6320
6321 if (!attr || attr->relax_domain_level < 0) {
6322 if (default_relax_domain_level < 0)
6323 return;
6324 else
6325 request = default_relax_domain_level;
6326 } else
6327 request = attr->relax_domain_level;
6328 if (request < sd->level) {
6329 /* turn off idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006330 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006331 } else {
6332 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006333 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006334 }
6335}
6336
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006337static void __sdt_free(const struct cpumask *cpu_map);
6338static int __sdt_alloc(const struct cpumask *cpu_map);
6339
Andreas Herrmann2109b992009-08-18 12:53:00 +02006340static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
6341 const struct cpumask *cpu_map)
6342{
6343 switch (what) {
Andreas Herrmann2109b992009-08-18 12:53:00 +02006344 case sa_rootdomain:
Peter Zijlstra822ff792011-04-07 14:09:51 +02006345 if (!atomic_read(&d->rd->refcount))
6346 free_rootdomain(&d->rd->rcu); /* fall through */
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006347 case sa_sd:
6348 free_percpu(d->sd); /* fall through */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006349 case sa_sd_storage:
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006350 __sdt_free(cpu_map); /* fall through */
Andreas Herrmann2109b992009-08-18 12:53:00 +02006351 case sa_none:
6352 break;
6353 }
6354}
6355
6356static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
6357 const struct cpumask *cpu_map)
6358{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006359 memset(d, 0, sizeof(*d));
6360
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006361 if (__sdt_alloc(cpu_map))
6362 return sa_sd_storage;
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006363 d->sd = alloc_percpu(struct sched_domain *);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006364 if (!d->sd)
6365 return sa_sd_storage;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006366 d->rd = alloc_rootdomain();
Peter Zijlstradce840a2011-04-07 14:09:50 +02006367 if (!d->rd)
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006368 return sa_sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006369 return sa_rootdomain;
6370}
6371
Peter Zijlstradce840a2011-04-07 14:09:50 +02006372/*
6373 * NULL the sd_data elements we've used to build the sched_domain and
6374 * sched_group structure so that the subsequent __free_domain_allocs()
6375 * will not free the data we're using.
6376 */
6377static void claim_allocations(int cpu, struct sched_domain *sd)
6378{
6379 struct sd_data *sdd = sd->private;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006380
6381 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
6382 *per_cpu_ptr(sdd->sd, cpu) = NULL;
6383
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006384 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
Peter Zijlstradce840a2011-04-07 14:09:50 +02006385 *per_cpu_ptr(sdd->sg, cpu) = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006386
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006387 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
6388 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006389}
6390
Peter Zijlstracb83b622012-04-17 15:49:36 +02006391#ifdef CONFIG_NUMA
Peter Zijlstracb83b622012-04-17 15:49:36 +02006392static int sched_domains_numa_levels;
Rik van Riele3fe70b2014-10-17 03:29:50 -04006393enum numa_topology_type sched_numa_topology_type;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006394static int *sched_domains_numa_distance;
Rik van Riel9942f792014-10-17 03:29:49 -04006395int sched_max_numa_distance;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006396static struct cpumask ***sched_domains_numa_masks;
6397static int sched_domains_curr_level;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006398#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006399
Vincent Guittot143e1e22014-04-11 11:44:37 +02006400/*
6401 * SD_flags allowed in topology descriptions.
6402 *
Peter Zijlstra94f438c2016-08-15 12:54:59 +02006403 * These flags are purely descriptive of the topology and do not prescribe
6404 * behaviour. Behaviour is artificial and mapped in the below sd_init()
6405 * function:
Vincent Guittot143e1e22014-04-11 11:44:37 +02006406 *
Peter Zijlstra94f438c2016-08-15 12:54:59 +02006407 * SD_SHARE_CPUCAPACITY - describes SMT topologies
6408 * SD_SHARE_PKG_RESOURCES - describes shared caches
6409 * SD_NUMA - describes NUMA topologies
6410 * SD_SHARE_POWERDOMAIN - describes shared power domain
Morten Rasmussen1f6e6c7c2016-07-25 14:34:22 +01006411 * SD_ASYM_CPUCAPACITY - describes mixed capacity topologies
Peter Zijlstra94f438c2016-08-15 12:54:59 +02006412 *
6413 * Odd one out, which beside describing the topology has a quirk also
6414 * prescribes the desired behaviour that goes along with it:
6415 *
6416 * SD_ASYM_PACKING - describes SMT quirks
Vincent Guittot143e1e22014-04-11 11:44:37 +02006417 */
6418#define TOPOLOGY_SD_FLAGS \
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006419 (SD_SHARE_CPUCAPACITY | \
Vincent Guittot143e1e22014-04-11 11:44:37 +02006420 SD_SHARE_PKG_RESOURCES | \
6421 SD_NUMA | \
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006422 SD_ASYM_PACKING | \
Morten Rasmussen1f6e6c7c2016-07-25 14:34:22 +01006423 SD_ASYM_CPUCAPACITY | \
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006424 SD_SHARE_POWERDOMAIN)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006425
6426static struct sched_domain *
Morten Rasmussen3676b132016-07-25 14:34:23 +01006427sd_init(struct sched_domain_topology_level *tl,
6428 struct sched_domain *child, int cpu)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006429{
6430 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
Vincent Guittot143e1e22014-04-11 11:44:37 +02006431 int sd_weight, sd_flags = 0;
6432
6433#ifdef CONFIG_NUMA
6434 /*
6435 * Ugly hack to pass state to sd_numa_mask()...
6436 */
6437 sched_domains_curr_level = tl->numa_level;
6438#endif
6439
6440 sd_weight = cpumask_weight(tl->mask(cpu));
6441
6442 if (tl->sd_flags)
6443 sd_flags = (*tl->sd_flags)();
6444 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
6445 "wrong sd_flags in topology description\n"))
6446 sd_flags &= ~TOPOLOGY_SD_FLAGS;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006447
6448 *sd = (struct sched_domain){
6449 .min_interval = sd_weight,
6450 .max_interval = 2*sd_weight,
6451 .busy_factor = 32,
Peter Zijlstra870a0bb2012-05-11 00:26:27 +02006452 .imbalance_pct = 125,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006453
6454 .cache_nice_tries = 0,
6455 .busy_idx = 0,
6456 .idle_idx = 0,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006457 .newidle_idx = 0,
6458 .wake_idx = 0,
6459 .forkexec_idx = 0,
6460
6461 .flags = 1*SD_LOAD_BALANCE
6462 | 1*SD_BALANCE_NEWIDLE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006463 | 1*SD_BALANCE_EXEC
6464 | 1*SD_BALANCE_FORK
Peter Zijlstracb83b622012-04-17 15:49:36 +02006465 | 0*SD_BALANCE_WAKE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006466 | 1*SD_WAKE_AFFINE
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006467 | 0*SD_SHARE_CPUCAPACITY
Peter Zijlstracb83b622012-04-17 15:49:36 +02006468 | 0*SD_SHARE_PKG_RESOURCES
Vincent Guittot143e1e22014-04-11 11:44:37 +02006469 | 0*SD_SERIALIZE
Peter Zijlstracb83b622012-04-17 15:49:36 +02006470 | 0*SD_PREFER_SIBLING
Vincent Guittot143e1e22014-04-11 11:44:37 +02006471 | 0*SD_NUMA
6472 | sd_flags
Peter Zijlstracb83b622012-04-17 15:49:36 +02006473 ,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006474
Peter Zijlstracb83b622012-04-17 15:49:36 +02006475 .last_balance = jiffies,
6476 .balance_interval = sd_weight,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006477 .smt_gain = 0,
Jason Low2b4cfe62014-04-23 18:30:34 -07006478 .max_newidle_lb_cost = 0,
6479 .next_decay_max_lb_cost = jiffies,
Morten Rasmussen3676b132016-07-25 14:34:23 +01006480 .child = child,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006481#ifdef CONFIG_SCHED_DEBUG
6482 .name = tl->name,
6483#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006484 };
Peter Zijlstracb83b622012-04-17 15:49:36 +02006485
6486 /*
Vincent Guittot143e1e22014-04-11 11:44:37 +02006487 * Convert topological properties into behaviour.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006488 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006489
Morten Rasmussen9ee1cda2016-07-25 14:34:24 +01006490 if (sd->flags & SD_ASYM_CPUCAPACITY) {
6491 struct sched_domain *t = sd;
6492
6493 for_each_lower_domain(t)
6494 t->flags |= SD_BALANCE_WAKE;
6495 }
6496
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006497 if (sd->flags & SD_SHARE_CPUCAPACITY) {
Vincent Guittotcaff37e2015-02-27 16:54:13 +01006498 sd->flags |= SD_PREFER_SIBLING;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006499 sd->imbalance_pct = 110;
6500 sd->smt_gain = 1178; /* ~15% */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006501
6502 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
6503 sd->imbalance_pct = 117;
6504 sd->cache_nice_tries = 1;
6505 sd->busy_idx = 2;
6506
6507#ifdef CONFIG_NUMA
6508 } else if (sd->flags & SD_NUMA) {
6509 sd->cache_nice_tries = 2;
6510 sd->busy_idx = 3;
6511 sd->idle_idx = 2;
6512
6513 sd->flags |= SD_SERIALIZE;
6514 if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
6515 sd->flags &= ~(SD_BALANCE_EXEC |
6516 SD_BALANCE_FORK |
6517 SD_WAKE_AFFINE);
6518 }
6519
6520#endif
6521 } else {
6522 sd->flags |= SD_PREFER_SIBLING;
6523 sd->cache_nice_tries = 1;
6524 sd->busy_idx = 2;
6525 sd->idle_idx = 1;
6526 }
6527
6528 sd->private = &tl->data;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006529
6530 return sd;
6531}
6532
Vincent Guittot143e1e22014-04-11 11:44:37 +02006533/*
6534 * Topology list, bottom-up.
6535 */
6536static struct sched_domain_topology_level default_topology[] = {
6537#ifdef CONFIG_SCHED_SMT
6538 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
6539#endif
6540#ifdef CONFIG_SCHED_MC
6541 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
6542#endif
Vincent Guittot143e1e22014-04-11 11:44:37 +02006543 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
6544 { NULL, },
6545};
6546
Juergen Grossc6e1e7b2015-09-22 12:48:59 +02006547static struct sched_domain_topology_level *sched_domain_topology =
6548 default_topology;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006549
6550#define for_each_sd_topology(tl) \
6551 for (tl = sched_domain_topology; tl->mask; tl++)
6552
6553void set_sched_topology(struct sched_domain_topology_level *tl)
6554{
Tim Chen8f379612016-09-21 12:19:03 -07006555 if (WARN_ON_ONCE(sched_smp_initialized))
6556 return;
6557
Vincent Guittot143e1e22014-04-11 11:44:37 +02006558 sched_domain_topology = tl;
6559}
6560
6561#ifdef CONFIG_NUMA
6562
Peter Zijlstracb83b622012-04-17 15:49:36 +02006563static const struct cpumask *sd_numa_mask(int cpu)
6564{
6565 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
6566}
6567
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006568static void sched_numa_warn(const char *str)
6569{
6570 static int done = false;
6571 int i,j;
6572
6573 if (done)
6574 return;
6575
6576 done = true;
6577
6578 printk(KERN_WARNING "ERROR: %s\n\n", str);
6579
6580 for (i = 0; i < nr_node_ids; i++) {
6581 printk(KERN_WARNING " ");
6582 for (j = 0; j < nr_node_ids; j++)
6583 printk(KERN_CONT "%02d ", node_distance(i,j));
6584 printk(KERN_CONT "\n");
6585 }
6586 printk(KERN_WARNING "\n");
6587}
6588
Rik van Riel9942f792014-10-17 03:29:49 -04006589bool find_numa_distance(int distance)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006590{
6591 int i;
6592
6593 if (distance == node_distance(0, 0))
6594 return true;
6595
6596 for (i = 0; i < sched_domains_numa_levels; i++) {
6597 if (sched_domains_numa_distance[i] == distance)
6598 return true;
6599 }
6600
6601 return false;
6602}
6603
Rik van Riele3fe70b2014-10-17 03:29:50 -04006604/*
6605 * A system can have three types of NUMA topology:
6606 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
6607 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
6608 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
6609 *
6610 * The difference between a glueless mesh topology and a backplane
6611 * topology lies in whether communication between not directly
6612 * connected nodes goes through intermediary nodes (where programs
6613 * could run), or through backplane controllers. This affects
6614 * placement of programs.
6615 *
6616 * The type of topology can be discerned with the following tests:
6617 * - If the maximum distance between any nodes is 1 hop, the system
6618 * is directly connected.
6619 * - If for two nodes A and B, located N > 1 hops away from each other,
6620 * there is an intermediary node C, which is < N hops away from both
6621 * nodes A and B, the system is a glueless mesh.
6622 */
6623static void init_numa_topology_type(void)
6624{
6625 int a, b, c, n;
6626
6627 n = sched_max_numa_distance;
6628
Aravind Gopalakrishnane2378822015-08-10 20:20:48 -05006629 if (sched_domains_numa_levels <= 1) {
Rik van Riele3fe70b2014-10-17 03:29:50 -04006630 sched_numa_topology_type = NUMA_DIRECT;
Aravind Gopalakrishnane2378822015-08-10 20:20:48 -05006631 return;
6632 }
Rik van Riele3fe70b2014-10-17 03:29:50 -04006633
6634 for_each_online_node(a) {
6635 for_each_online_node(b) {
6636 /* Find two nodes furthest removed from each other. */
6637 if (node_distance(a, b) < n)
6638 continue;
6639
6640 /* Is there an intermediary node between a and b? */
6641 for_each_online_node(c) {
6642 if (node_distance(a, c) < n &&
6643 node_distance(b, c) < n) {
6644 sched_numa_topology_type =
6645 NUMA_GLUELESS_MESH;
6646 return;
6647 }
6648 }
6649
6650 sched_numa_topology_type = NUMA_BACKPLANE;
6651 return;
6652 }
6653 }
6654}
6655
Peter Zijlstracb83b622012-04-17 15:49:36 +02006656static void sched_init_numa(void)
6657{
6658 int next_distance, curr_distance = node_distance(0, 0);
6659 struct sched_domain_topology_level *tl;
6660 int level = 0;
6661 int i, j, k;
6662
Peter Zijlstracb83b622012-04-17 15:49:36 +02006663 sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
6664 if (!sched_domains_numa_distance)
6665 return;
6666
6667 /*
6668 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
6669 * unique distances in the node_distance() table.
6670 *
6671 * Assumes node_distance(0,j) includes all distances in
6672 * node_distance(i,j) in order to avoid cubic time.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006673 */
6674 next_distance = curr_distance;
6675 for (i = 0; i < nr_node_ids; i++) {
6676 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006677 for (k = 0; k < nr_node_ids; k++) {
6678 int distance = node_distance(i, k);
6679
6680 if (distance > curr_distance &&
6681 (distance < next_distance ||
6682 next_distance == curr_distance))
6683 next_distance = distance;
6684
6685 /*
6686 * While not a strong assumption it would be nice to know
6687 * about cases where if node A is connected to B, B is not
6688 * equally connected to A.
6689 */
6690 if (sched_debug() && node_distance(k, i) != distance)
6691 sched_numa_warn("Node-distance not symmetric");
6692
6693 if (sched_debug() && i && !find_numa_distance(distance))
6694 sched_numa_warn("Node-0 not representative");
6695 }
6696 if (next_distance != curr_distance) {
6697 sched_domains_numa_distance[level++] = next_distance;
6698 sched_domains_numa_levels = level;
6699 curr_distance = next_distance;
6700 } else break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006701 }
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006702
6703 /*
6704 * In case of sched_debug() we verify the above assumption.
6705 */
6706 if (!sched_debug())
6707 break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006708 }
Andrey Ryabininc1235882014-11-07 17:53:40 +03006709
6710 if (!level)
6711 return;
6712
Peter Zijlstracb83b622012-04-17 15:49:36 +02006713 /*
6714 * 'level' contains the number of unique distances, excluding the
6715 * identity distance node_distance(i,i).
6716 *
Viresh Kumar28b4a522013-04-05 16:26:46 +05306717 * The sched_domains_numa_distance[] array includes the actual distance
Peter Zijlstracb83b622012-04-17 15:49:36 +02006718 * numbers.
6719 */
6720
Tang Chen5f7865f2012-09-25 21:12:30 +08006721 /*
6722 * Here, we should temporarily reset sched_domains_numa_levels to 0.
6723 * If it fails to allocate memory for array sched_domains_numa_masks[][],
6724 * the array will contain less then 'level' members. This could be
6725 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
6726 * in other functions.
6727 *
6728 * We reset it to 'level' at the end of this function.
6729 */
6730 sched_domains_numa_levels = 0;
6731
Peter Zijlstracb83b622012-04-17 15:49:36 +02006732 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
6733 if (!sched_domains_numa_masks)
6734 return;
6735
6736 /*
6737 * Now for each level, construct a mask per node which contains all
6738 * cpus of nodes that are that many hops away from us.
6739 */
6740 for (i = 0; i < level; i++) {
6741 sched_domains_numa_masks[i] =
6742 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
6743 if (!sched_domains_numa_masks[i])
6744 return;
6745
6746 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstra2ea45802012-05-25 09:26:43 +02006747 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
Peter Zijlstracb83b622012-04-17 15:49:36 +02006748 if (!mask)
6749 return;
6750
6751 sched_domains_numa_masks[i][j] = mask;
6752
Raghavendra K T9c03ee12016-01-16 00:31:23 +05306753 for_each_node(k) {
Peter Zijlstradd7d8632012-05-11 00:56:20 +02006754 if (node_distance(j, k) > sched_domains_numa_distance[i])
Peter Zijlstracb83b622012-04-17 15:49:36 +02006755 continue;
6756
6757 cpumask_or(mask, mask, cpumask_of_node(k));
6758 }
6759 }
6760 }
6761
Vincent Guittot143e1e22014-04-11 11:44:37 +02006762 /* Compute default topology size */
6763 for (i = 0; sched_domain_topology[i].mask; i++);
6764
Vincent Guittotc515db82014-05-13 11:11:01 +02006765 tl = kzalloc((i + level + 1) *
Peter Zijlstracb83b622012-04-17 15:49:36 +02006766 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
6767 if (!tl)
6768 return;
6769
6770 /*
6771 * Copy the default topology bits..
6772 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006773 for (i = 0; sched_domain_topology[i].mask; i++)
6774 tl[i] = sched_domain_topology[i];
Peter Zijlstracb83b622012-04-17 15:49:36 +02006775
6776 /*
6777 * .. and append 'j' levels of NUMA goodness.
6778 */
6779 for (j = 0; j < level; i++, j++) {
6780 tl[i] = (struct sched_domain_topology_level){
Peter Zijlstracb83b622012-04-17 15:49:36 +02006781 .mask = sd_numa_mask,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006782 .sd_flags = cpu_numa_flags,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006783 .flags = SDTL_OVERLAP,
6784 .numa_level = j,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006785 SD_INIT_NAME(NUMA)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006786 };
6787 }
6788
6789 sched_domain_topology = tl;
Tang Chen5f7865f2012-09-25 21:12:30 +08006790
6791 sched_domains_numa_levels = level;
Rik van Riel9942f792014-10-17 03:29:49 -04006792 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
Rik van Riele3fe70b2014-10-17 03:29:50 -04006793
6794 init_numa_topology_type();
Peter Zijlstracb83b622012-04-17 15:49:36 +02006795}
Tang Chen301a5cb2012-09-25 21:12:31 +08006796
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006797static void sched_domains_numa_masks_set(unsigned int cpu)
Tang Chen301a5cb2012-09-25 21:12:31 +08006798{
Tang Chen301a5cb2012-09-25 21:12:31 +08006799 int node = cpu_to_node(cpu);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006800 int i, j;
Tang Chen301a5cb2012-09-25 21:12:31 +08006801
6802 for (i = 0; i < sched_domains_numa_levels; i++) {
6803 for (j = 0; j < nr_node_ids; j++) {
6804 if (node_distance(j, node) <= sched_domains_numa_distance[i])
6805 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
6806 }
6807 }
6808}
6809
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006810static void sched_domains_numa_masks_clear(unsigned int cpu)
Tang Chen301a5cb2012-09-25 21:12:31 +08006811{
6812 int i, j;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006813
Tang Chen301a5cb2012-09-25 21:12:31 +08006814 for (i = 0; i < sched_domains_numa_levels; i++) {
6815 for (j = 0; j < nr_node_ids; j++)
6816 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
6817 }
6818}
6819
Peter Zijlstracb83b622012-04-17 15:49:36 +02006820#else
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006821static inline void sched_init_numa(void) { }
6822static void sched_domains_numa_masks_set(unsigned int cpu) { }
6823static void sched_domains_numa_masks_clear(unsigned int cpu) { }
Peter Zijlstracb83b622012-04-17 15:49:36 +02006824#endif /* CONFIG_NUMA */
6825
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006826static int __sdt_alloc(const struct cpumask *cpu_map)
6827{
6828 struct sched_domain_topology_level *tl;
6829 int j;
6830
Viresh Kumar27723a62013-06-10 16:27:20 +05306831 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006832 struct sd_data *sdd = &tl->data;
6833
6834 sdd->sd = alloc_percpu(struct sched_domain *);
6835 if (!sdd->sd)
6836 return -ENOMEM;
6837
6838 sdd->sg = alloc_percpu(struct sched_group *);
6839 if (!sdd->sg)
6840 return -ENOMEM;
6841
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006842 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
6843 if (!sdd->sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006844 return -ENOMEM;
6845
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006846 for_each_cpu(j, cpu_map) {
6847 struct sched_domain *sd;
6848 struct sched_group *sg;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006849 struct sched_group_capacity *sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006850
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02006851 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006852 GFP_KERNEL, cpu_to_node(j));
6853 if (!sd)
6854 return -ENOMEM;
6855
6856 *per_cpu_ptr(sdd->sd, j) = sd;
6857
6858 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
6859 GFP_KERNEL, cpu_to_node(j));
6860 if (!sg)
6861 return -ENOMEM;
6862
Igor Mammedov30b4e9e2012-05-09 12:38:28 +02006863 sg->next = sg;
6864
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006865 *per_cpu_ptr(sdd->sg, j) = sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006866
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006867 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006868 GFP_KERNEL, cpu_to_node(j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006869 if (!sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006870 return -ENOMEM;
6871
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006872 *per_cpu_ptr(sdd->sgc, j) = sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006873 }
6874 }
6875
6876 return 0;
6877}
6878
6879static void __sdt_free(const struct cpumask *cpu_map)
6880{
6881 struct sched_domain_topology_level *tl;
6882 int j;
6883
Viresh Kumar27723a62013-06-10 16:27:20 +05306884 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006885 struct sd_data *sdd = &tl->data;
6886
6887 for_each_cpu(j, cpu_map) {
he, bofb2cf2c2012-04-25 19:59:21 +08006888 struct sched_domain *sd;
6889
6890 if (sdd->sd) {
6891 sd = *per_cpu_ptr(sdd->sd, j);
6892 if (sd && (sd->flags & SD_OVERLAP))
6893 free_sched_groups(sd->groups, 0);
6894 kfree(*per_cpu_ptr(sdd->sd, j));
6895 }
6896
6897 if (sdd->sg)
6898 kfree(*per_cpu_ptr(sdd->sg, j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006899 if (sdd->sgc)
6900 kfree(*per_cpu_ptr(sdd->sgc, j));
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006901 }
6902 free_percpu(sdd->sd);
he, bofb2cf2c2012-04-25 19:59:21 +08006903 sdd->sd = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006904 free_percpu(sdd->sg);
he, bofb2cf2c2012-04-25 19:59:21 +08006905 sdd->sg = NULL;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006906 free_percpu(sdd->sgc);
6907 sdd->sgc = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006908 }
6909}
6910
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006911struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306912 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6913 struct sched_domain *child, int cpu)
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006914{
Morten Rasmussen3676b132016-07-25 14:34:23 +01006915 struct sched_domain *sd = sd_init(tl, child, cpu);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006916
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006917 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
Peter Zijlstra60495e72011-04-07 14:10:04 +02006918 if (child) {
6919 sd->level = child->level + 1;
6920 sched_domain_level_max = max(sched_domain_level_max, sd->level);
Peter Zijlstrad069b912011-04-07 14:10:02 +02006921 child->parent = sd;
Peter Zijlstra6ae72df2014-07-22 11:47:40 +02006922
6923 if (!cpumask_subset(sched_domain_span(child),
6924 sched_domain_span(sd))) {
6925 pr_err("BUG: arch topology borken\n");
6926#ifdef CONFIG_SCHED_DEBUG
6927 pr_err(" the %s domain not a subset of the %s domain\n",
6928 child->name, sd->name);
6929#endif
6930 /* Fixup, ensure @sd has at least @child cpus. */
6931 cpumask_or(sched_domain_span(sd),
6932 sched_domain_span(sd),
6933 sched_domain_span(child));
6934 }
6935
Peter Zijlstra60495e72011-04-07 14:10:04 +02006936 }
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006937 set_domain_attribute(sd, attr);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006938
6939 return sd;
6940}
6941
Mike Travis7c16ec52008-04-04 18:11:11 -07006942/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006943 * Build sched domains for a given set of cpus and attach the sched domains
6944 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006946static int build_sched_domains(const struct cpumask *cpu_map,
6947 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948{
Viresh Kumar1c632162013-06-10 16:27:18 +05306949 enum s_alloc alloc_state;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006950 struct sched_domain *sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006951 struct s_data d;
Dietmar Eggemanncd92bfd2016-08-01 19:53:35 +01006952 struct rq *rq = NULL;
Peter Zijlstra822ff792011-04-07 14:09:51 +02006953 int i, ret = -ENOMEM;
Rusty Russell3404c8d2008-11-25 02:35:03 +10306954
Andreas Herrmann2109b992009-08-18 12:53:00 +02006955 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
6956 if (alloc_state != sa_rootdomain)
6957 goto error;
Mike Travis7c16ec52008-04-04 18:11:11 -07006958
Peter Zijlstradce840a2011-04-07 14:09:50 +02006959 /* Set up domains for cpus specified by the cpu_map. */
Rusty Russellabcd0832008-11-25 02:35:02 +10306960 for_each_cpu(i, cpu_map) {
Peter Zijlstraeb7a74e62011-04-07 14:10:00 +02006961 struct sched_domain_topology_level *tl;
6962
Peter Zijlstra3bd65a82011-04-07 14:09:54 +02006963 sd = NULL;
Viresh Kumar27723a62013-06-10 16:27:20 +05306964 for_each_sd_topology(tl) {
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306965 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
Viresh Kumar22da9562013-06-04 15:41:15 +05306966 if (tl == sched_domain_topology)
6967 *per_cpu_ptr(d.sd, i) = sd;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006968 if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
6969 sd->flags |= SD_OVERLAP;
Peter Zijlstrad1102352011-07-20 18:42:57 +02006970 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
6971 break;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006972 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006973 }
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006974
Peter Zijlstradce840a2011-04-07 14:09:50 +02006975 /* Build the groups for the domains */
6976 for_each_cpu(i, cpu_map) {
6977 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6978 sd->span_weight = cpumask_weight(sched_domain_span(sd));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006979 if (sd->flags & SD_OVERLAP) {
6980 if (build_overlap_sched_groups(sd, i))
6981 goto error;
6982 } else {
6983 if (build_sched_groups(sd, i))
6984 goto error;
6985 }
Peter Zijlstra1cf519022011-04-07 14:09:47 +02006986 }
Peter Zijlstraa06dadb2011-04-07 14:09:44 +02006987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988
Nicolas Pitreced549f2014-05-26 18:19:38 -04006989 /* Calculate CPU capacity for physical packages and nodes */
Peter Zijlstraa9c9a9b2011-04-07 14:09:49 +02006990 for (i = nr_cpumask_bits-1; i >= 0; i--) {
6991 if (!cpumask_test_cpu(i, cpu_map))
6992 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993
Peter Zijlstradce840a2011-04-07 14:09:50 +02006994 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6995 claim_allocations(i, sd);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006996 init_sched_groups_capacity(i, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006997 }
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006998 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006999
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 /* Attach the domains */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007001 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10307002 for_each_cpu(i, cpu_map) {
Dietmar Eggemanncd92bfd2016-08-01 19:53:35 +01007003 rq = cpu_rq(i);
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007004 sd = *per_cpu_ptr(d.sd, i);
Dietmar Eggemanncd92bfd2016-08-01 19:53:35 +01007005
7006 /* Use READ_ONCE()/WRITE_ONCE() to avoid load/store tearing: */
7007 if (rq->cpu_capacity_orig > READ_ONCE(d.rd->max_cpu_capacity))
7008 WRITE_ONCE(d.rd->max_cpu_capacity, rq->cpu_capacity_orig);
7009
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007010 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02007012 rcu_read_unlock();
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007013
Peter Zijlstraa18a5792016-09-20 11:05:31 +02007014 if (rq && sched_debug_enabled) {
Dietmar Eggemanncd92bfd2016-08-01 19:53:35 +01007015 pr_info("span: %*pbl (max cpu_capacity = %lu)\n",
7016 cpumask_pr_args(cpu_map), rq->rd->max_cpu_capacity);
7017 }
7018
Peter Zijlstra822ff792011-04-07 14:09:51 +02007019 ret = 0;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007020error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02007021 __free_domain_allocs(&d, alloc_state, cpu_map);
Peter Zijlstra822ff792011-04-07 14:09:51 +02007022 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023}
Paul Jackson029190c2007-10-18 23:40:20 -07007024
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307025static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07007026static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007027static struct sched_domain_attr *dattr_cur;
7028 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007029
7030/*
7031 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10307032 * cpumask) fails, then fallback to a single sched domain,
7033 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07007034 */
Rusty Russell42128232008-11-25 02:35:12 +10307035static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07007036
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007037/*
7038 * arch_update_cpu_topology lets virtualized architectures update the
7039 * cpu core maps. It is supposed to return 1 if the topology changed
7040 * or 0 if it stayed the same.
7041 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07007042int __weak arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01007043{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007044 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01007045}
7046
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307047cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
7048{
7049 int i;
7050 cpumask_var_t *doms;
7051
7052 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
7053 if (!doms)
7054 return NULL;
7055 for (i = 0; i < ndoms; i++) {
7056 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
7057 free_sched_domains(doms, i);
7058 return NULL;
7059 }
7060 }
7061 return doms;
7062}
7063
7064void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
7065{
7066 unsigned int i;
7067 for (i = 0; i < ndoms; i++)
7068 free_cpumask_var(doms[i]);
7069 kfree(doms);
7070}
7071
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007072/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007073 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007074 * For now this just excludes isolated cpus, but could be used to
7075 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007076 */
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007077static int init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007078{
Milton Miller73785472007-10-24 18:23:48 +02007079 int err;
7080
Heiko Carstens22e52b02008-03-12 18:31:59 +01007081 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007082 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307083 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07007084 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307085 doms_cur = &fallback_doms;
7086 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007087 err = build_sched_domains(doms_cur[0], NULL);
Milton Miller6382bc92007-10-15 17:00:19 +02007088 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007089
7090 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007091}
7092
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007093/*
7094 * Detach sched domains from a group of cpus specified in cpu_map
7095 * These cpus will now be attached to the NULL domain
7096 */
Rusty Russell96f874e22008-11-25 02:35:14 +10307097static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007098{
7099 int i;
7100
Peter Zijlstradce840a2011-04-07 14:09:50 +02007101 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10307102 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007103 cpu_attach_domain(NULL, &def_root_domain, i);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007104 rcu_read_unlock();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007105}
7106
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007107/* handle null as "default" */
7108static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7109 struct sched_domain_attr *new, int idx_new)
7110{
7111 struct sched_domain_attr tmp;
7112
7113 /* fast path */
7114 if (!new && !cur)
7115 return 1;
7116
7117 tmp = SD_ATTR_INIT;
7118 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7119 new ? (new + idx_new) : &tmp,
7120 sizeof(struct sched_domain_attr));
7121}
7122
Paul Jackson029190c2007-10-18 23:40:20 -07007123/*
7124 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007125 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007126 * doms_new[] to the current sched domain partitioning, doms_cur[].
7127 * It destroys each deleted domain and builds each new domain.
7128 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307129 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007130 * The masks don't intersect (don't overlap.) We should setup one
7131 * sched domain for each mask. CPUs not in any of the cpumasks will
7132 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007133 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7134 * it as it is.
7135 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307136 * The passed in 'doms_new' should be allocated using
7137 * alloc_sched_domains. This routine takes ownership of it and will
7138 * free_sched_domains it when done with it. If the caller failed the
7139 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
7140 * and partition_sched_domains() will fallback to the single partition
7141 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007142 *
Rusty Russell96f874e22008-11-25 02:35:14 +10307143 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08007144 * ndoms_new == 0 is a special case for destroying existing domains,
7145 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007146 *
Paul Jackson029190c2007-10-18 23:40:20 -07007147 * Call with hotplug lock held
7148 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307149void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007150 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007151{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007152 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007153 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07007154
Heiko Carstens712555e2008-04-28 11:33:07 +02007155 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007156
Milton Miller73785472007-10-24 18:23:48 +02007157 /* always unregister in case we don't destroy any domains */
7158 unregister_sched_domain_sysctl();
7159
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007160 /* Let architecture update cpu core mappings. */
7161 new_topology = arch_update_cpu_topology();
7162
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007163 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007164
7165 /* Destroy deleted domains */
7166 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007167 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307168 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007169 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007170 goto match1;
7171 }
7172 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307173 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07007174match1:
7175 ;
7176 }
7177
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007178 n = ndoms_cur;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007179 if (doms_new == NULL) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007180 n = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307181 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007182 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08007183 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007184 }
7185
Paul Jackson029190c2007-10-18 23:40:20 -07007186 /* Build new domains */
7187 for (i = 0; i < ndoms_new; i++) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007188 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307189 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007190 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007191 goto match2;
7192 }
7193 /* no match - add a new doms_new */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007194 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007195match2:
7196 ;
7197 }
7198
7199 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307200 if (doms_cur != &fallback_doms)
7201 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007202 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007203 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007204 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007205 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007206
7207 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007208
Heiko Carstens712555e2008-04-28 11:33:07 +02007209 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007210}
7211
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307212static int num_cpus_frozen; /* used to mark begin/end of suspend/resume */
7213
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214/*
Tejun Heo3a101d02010-06-08 21:40:36 +02007215 * Update cpusets according to cpu_active mask. If cpusets are
7216 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
7217 * around partition_sched_domains().
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307218 *
7219 * If we come here as part of a suspend/resume, don't touch cpusets because we
7220 * want to restore it back to its original state upon resume anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 */
Thomas Gleixner40190a72016-03-10 12:54:13 +01007222static void cpuset_cpu_active(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223{
Thomas Gleixner40190a72016-03-10 12:54:13 +01007224 if (cpuhp_tasks_frozen) {
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307225 /*
7226 * num_cpus_frozen tracks how many CPUs are involved in suspend
7227 * resume sequence. As long as this is not the last online
7228 * operation in the resume sequence, just build a single sched
7229 * domain, ignoring cpusets.
7230 */
7231 num_cpus_frozen--;
7232 if (likely(num_cpus_frozen)) {
7233 partition_sched_domains(1, NULL, NULL);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007234 return;
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307235 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307236 /*
7237 * This is the last CPU online operation. So fall through and
7238 * restore the original sched domains by considering the
7239 * cpuset configurations.
7240 */
Max Krasnyanskye761b772008-07-15 04:43:49 -07007241 }
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007242 cpuset_update_active_cpus(true);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007243}
Tejun Heo3a101d02010-06-08 21:40:36 +02007244
Thomas Gleixner40190a72016-03-10 12:54:13 +01007245static int cpuset_cpu_inactive(unsigned int cpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02007246{
Juri Lelli3c18d442015-03-31 09:53:37 +01007247 unsigned long flags;
Juri Lelli3c18d442015-03-31 09:53:37 +01007248 struct dl_bw *dl_b;
Omar Sandoval533445c2015-05-04 03:09:36 -07007249 bool overflow;
7250 int cpus;
Juri Lelli3c18d442015-03-31 09:53:37 +01007251
Thomas Gleixner40190a72016-03-10 12:54:13 +01007252 if (!cpuhp_tasks_frozen) {
Omar Sandoval533445c2015-05-04 03:09:36 -07007253 rcu_read_lock_sched();
7254 dl_b = dl_bw_of(cpu);
Juri Lelli3c18d442015-03-31 09:53:37 +01007255
Omar Sandoval533445c2015-05-04 03:09:36 -07007256 raw_spin_lock_irqsave(&dl_b->lock, flags);
7257 cpus = dl_bw_cpus(cpu);
7258 overflow = __dl_overflow(dl_b, cpus, 0, 0);
7259 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli3c18d442015-03-31 09:53:37 +01007260
Omar Sandoval533445c2015-05-04 03:09:36 -07007261 rcu_read_unlock_sched();
Juri Lelli3c18d442015-03-31 09:53:37 +01007262
Omar Sandoval533445c2015-05-04 03:09:36 -07007263 if (overflow)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007264 return -EBUSY;
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307265 cpuset_update_active_cpus(false);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007266 } else {
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307267 num_cpus_frozen++;
7268 partition_sched_domains(1, NULL, NULL);
Tejun Heo3a101d02010-06-08 21:40:36 +02007269 }
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007270 return 0;
Tejun Heo3a101d02010-06-08 21:40:36 +02007271}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007272
Thomas Gleixner40190a72016-03-10 12:54:13 +01007273int sched_cpu_activate(unsigned int cpu)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007274{
Thomas Gleixner7d976692016-03-10 12:54:17 +01007275 struct rq *rq = cpu_rq(cpu);
7276 unsigned long flags;
7277
Thomas Gleixner40190a72016-03-10 12:54:13 +01007278 set_cpu_active(cpu, true);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007279
Thomas Gleixner40190a72016-03-10 12:54:13 +01007280 if (sched_smp_initialized) {
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007281 sched_domains_numa_masks_set(cpu);
Thomas Gleixner40190a72016-03-10 12:54:13 +01007282 cpuset_cpu_active();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007283 }
Thomas Gleixner7d976692016-03-10 12:54:17 +01007284
7285 /*
7286 * Put the rq online, if not already. This happens:
7287 *
7288 * 1) In the early boot process, because we build the real domains
7289 * after all cpus have been brought up.
7290 *
7291 * 2) At runtime, if cpuset_cpu_active() fails to rebuild the
7292 * domains.
7293 */
7294 raw_spin_lock_irqsave(&rq->lock, flags);
7295 if (rq->rd) {
7296 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
7297 set_rq_online(rq);
7298 }
7299 raw_spin_unlock_irqrestore(&rq->lock, flags);
7300
7301 update_max_interval();
7302
Thomas Gleixner40190a72016-03-10 12:54:13 +01007303 return 0;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007304}
7305
Thomas Gleixner40190a72016-03-10 12:54:13 +01007306int sched_cpu_deactivate(unsigned int cpu)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007307{
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007308 int ret;
7309
Thomas Gleixner40190a72016-03-10 12:54:13 +01007310 set_cpu_active(cpu, false);
Peter Zijlstrab2454ca2016-03-10 12:54:14 +01007311 /*
7312 * We've cleared cpu_active_mask, wait for all preempt-disabled and RCU
7313 * users of this state to go away such that all new such users will
7314 * observe it.
7315 *
7316 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
7317 * not imply sync_sched(), so wait for both.
7318 *
7319 * Do sync before park smpboot threads to take care the rcu boost case.
7320 */
7321 if (IS_ENABLED(CONFIG_PREEMPT))
7322 synchronize_rcu_mult(call_rcu, call_rcu_sched);
7323 else
7324 synchronize_rcu();
Thomas Gleixner40190a72016-03-10 12:54:13 +01007325
7326 if (!sched_smp_initialized)
7327 return 0;
7328
7329 ret = cpuset_cpu_inactive(cpu);
7330 if (ret) {
7331 set_cpu_active(cpu, true);
7332 return ret;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007333 }
Thomas Gleixner40190a72016-03-10 12:54:13 +01007334 sched_domains_numa_masks_clear(cpu);
7335 return 0;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007336}
7337
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01007338static void sched_rq_cpu_starting(unsigned int cpu)
7339{
7340 struct rq *rq = cpu_rq(cpu);
7341
7342 rq->calc_load_update = calc_load_update;
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01007343 update_max_interval();
7344}
7345
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007346int sched_cpu_starting(unsigned int cpu)
7347{
7348 set_cpu_rq_start_time(cpu);
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01007349 sched_rq_cpu_starting(cpu);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007350 return 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007351}
7352
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01007353#ifdef CONFIG_HOTPLUG_CPU
7354int sched_cpu_dying(unsigned int cpu)
7355{
7356 struct rq *rq = cpu_rq(cpu);
7357 unsigned long flags;
7358
7359 /* Handle pending wakeups and then migrate everything off */
7360 sched_ttwu_pending();
7361 raw_spin_lock_irqsave(&rq->lock, flags);
7362 if (rq->rd) {
7363 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
7364 set_rq_offline(rq);
7365 }
7366 migrate_tasks(rq);
7367 BUG_ON(rq->nr_running != 1);
7368 raw_spin_unlock_irqrestore(&rq->lock, flags);
7369 calc_load_migrate(rq);
7370 update_max_interval();
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01007371 nohz_balance_exit_idle(cpu);
Thomas Gleixnere5ef27d2016-03-10 12:54:21 +01007372 hrtick_clear(rq);
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01007373 return 0;
7374}
7375#endif
7376
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007377void __init sched_init_smp(void)
7378{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 cpumask_var_t non_isolated_cpus;
7380
Christoph Lameter476f3532007-05-06 14:48:58 -07007381 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Ingo Molnardd41f592007-07-09 18:51:59 +02007382 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Mike Travis434d53b2008-04-04 18:11:04 -07007383
7384 sched_init_numa();
7385
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02007386 /*
7387 * There's no userspace yet to cause hotplug operations; hence all the
7388 * cpu masks are stable and all blatant races in the below code cannot
7389 * happen.
7390 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 mutex_lock(&sched_domains_mutex);
7392 init_sched_domains(cpu_active_mask);
7393 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7394 if (cpumask_empty(non_isolated_cpus))
7395 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
7396 mutex_unlock(&sched_domains_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397
Mike Travis434d53b2008-04-04 18:11:04 -07007398 /* Move init over to a non-isolated CPU */
7399 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
7400 BUG();
7401 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307402 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307403
Rusty Russell0e3900e2008-11-25 02:35:13 +10307404 init_sched_rt_class();
Juri Lelli1baca4c2013-11-07 14:43:38 +01007405 init_sched_dl_class();
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01007406 sched_smp_initialized = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407}
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01007408
7409static int __init migration_init(void)
7410{
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01007411 sched_rq_cpu_starting(smp_processor_id());
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01007412 return 0;
7413}
7414early_initcall(migration_init);
7415
Ingo Molnardd41f592007-07-09 18:51:59 +02007416#else
7417void __init sched_init_smp(void)
7418{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419 sched_init_granularity();
7420}
Peter Williams2dd73a42006-06-27 02:54:34 -07007421#endif /* CONFIG_SMP */
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007422
Avi Kivitye107be32007-07-26 13:40:43 +02007423int in_sched_functions(unsigned long addr)
7424{
7425 return in_lock_functions(addr) ||
7426 (addr >= (unsigned long)__sched_text_start
Christoph Lameterc9819f42006-12-10 02:20:25 -08007427 && addr < (unsigned long)__sched_text_end);
Christoph Lameter476f3532007-05-06 14:48:58 -07007428}
Christoph Lameterc9819f42006-12-10 02:20:25 -08007429
Peter Zijlstra029632f2011-10-25 10:00:11 +02007430#ifdef CONFIG_CGROUP_SCHED
Li Zefan27b4b932013-03-05 16:07:52 +08007431/*
7432 * Default task group.
7433 * Every task in system belongs to this group at bootup.
7434 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007435struct task_group root_task_group;
Mike Galbraith35cf4e52012-08-07 05:00:13 +02007436LIST_HEAD(task_groups);
Waiman Longb0367622015-12-02 13:41:49 -05007437
7438/* Cacheline aligned slab cache for task_group */
7439static struct kmem_cache *task_group_cache __read_mostly;
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007440#endif
7441
Joonsoo Kime6252c32013-04-23 17:27:41 +09007442DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443
7444void __init sched_init(void)
7445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007447 unsigned long alloc_size = 0, ptr;
7448
7449#ifdef CONFIG_FAIR_GROUP_SCHED
7450 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7451#endif
7452#ifdef CONFIG_RT_GROUP_SCHED
7453 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7454#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007455 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007456 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007457
7458#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007459 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007460 ptr += nr_cpu_ids * sizeof(void **);
7461
Yong Zhang07e06b02011-01-07 15:17:36 +08007462 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007463 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007464
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007465#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007466#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007467 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007468 ptr += nr_cpu_ids * sizeof(void **);
7469
Yong Zhang07e06b02011-01-07 15:17:36 +08007470 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007471 ptr += nr_cpu_ids * sizeof(void **);
7472
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007473#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007474 }
Alex Thorltonb74e6272014-12-18 12:44:30 -06007475#ifdef CONFIG_CPUMASK_OFFSTACK
7476 for_each_possible_cpu(i) {
7477 per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
7478 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
7479 }
7480#endif /* CONFIG_CPUMASK_OFFSTACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481
Dario Faggioli332ac172013-11-07 14:43:45 +01007482 init_rt_bandwidth(&def_rt_bandwidth,
7483 global_rt_period(), global_rt_runtime());
7484 init_dl_bandwidth(&def_dl_bandwidth,
Peter Zijlstra17248132013-12-17 12:44:49 +01007485 global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +01007486
Gregory Haskins57d885f2008-01-25 21:08:18 +01007487#ifdef CONFIG_SMP
7488 init_defrootdomain();
7489#endif
7490
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007491#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007492 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007493 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007494#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007495
Dhaval Giani7c941432010-01-20 13:26:18 +01007496#ifdef CONFIG_CGROUP_SCHED
Waiman Longb0367622015-12-02 13:41:49 -05007497 task_group_cache = KMEM_CACHE(task_group, 0);
7498
Yong Zhang07e06b02011-01-07 15:17:36 +08007499 list_add(&root_task_group.list, &task_groups);
7500 INIT_LIST_HEAD(&root_task_group.children);
Glauber Costaf4d6f6c2011-11-01 19:19:07 -02007501 INIT_LIST_HEAD(&root_task_group.siblings);
Mike Galbraith5091faa2010-11-30 14:18:03 +01007502 autogroup_init(&init_task);
Dhaval Giani7c941432010-01-20 13:26:18 +01007503#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007504
Ingo Molnardd41f592007-07-09 18:51:59 +02007505 for_each_possible_cpu(i) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007506 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507
7508 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007509 raw_spin_lock_init(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007511 rq->calc_load_active = 0;
7512 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007513 init_cfs_rq(&rq->cfs);
Abel Vesa07c54f72015-03-03 13:50:27 +02007514 init_rt_rq(&rq->rt);
7515 init_dl_rq(&rq->dl);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007516#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007517 root_task_group.shares = ROOT_TASK_GROUP_LOAD;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007518 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007519 /*
Yong Zhang07e06b02011-01-07 15:17:36 +08007520 * How much cpu bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02007521 *
7522 * In case of task-groups formed thr' the cgroup filesystem, it
7523 * gets 100% of the cpu resources in the system. This overall
7524 * system cpu resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08007525 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02007526 * based on each entity's (task or task-group's) weight
7527 * (se->load.weight).
7528 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007529 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02007530 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7531 * then A0's share of the cpu resource is:
7532 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007533 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007534 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007535 * We achieve this by letting root_task_group's tasks sit
7536 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02007537 */
Paul Turnerab84d312011-07-21 09:43:28 -07007538 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Yong Zhang07e06b02011-01-07 15:17:36 +08007539 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007540#endif /* CONFIG_FAIR_GROUP_SCHED */
7541
7542 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007543#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007544 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007545#endif
Ingo Molnar91368d72006-03-23 03:00:54 -08007546
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7548 rq->cpu_load[j] = 0;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07007549
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550#ifdef CONFIG_SMP
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007551 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007552 rq->rd = NULL;
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007553 rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02007554 rq->balance_callback = NULL;
Ingo Molnar3117df02006-12-13 00:34:43 -08007555 rq->active_balance = 0;
7556 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 rq->push_cpu = 0;
7558 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04007559 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007560 rq->idle_stamp = 0;
7561 rq->avg_idle = 2*sysctl_sched_migration_cost;
Jason Low9bd721c2013-09-13 11:26:52 -07007562 rq->max_idle_balance_cost = sysctl_sched_migration_cost;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007563
7564 INIT_LIST_HEAD(&rq->cfs_tasks);
7565
Gregory Haskinsdc938522008-01-25 21:08:26 +01007566 rq_attach_root(rq, &def_root_domain);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007567#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02007568 rq->last_load_update_tick = jiffies;
Suresh Siddha1c792db2011-12-01 17:07:32 -08007569 rq->nohz_flags = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007570#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02007571#ifdef CONFIG_NO_HZ_FULL
7572 rq->last_sched_tick = 0;
7573#endif
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02007574#endif /* CONFIG_SMP */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007575 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 }
Ingo Molnara0f98a12007-06-17 18:37:45 +02007578
Ingo Molnardd41f592007-07-09 18:51:59 +02007579 set_load_weight(&init_task);
7580
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 /*
7582 * The boot idle thread does lazy MMU switching as well:
7583 */
7584 atomic_inc(&init_mm.mm_count);
7585 enter_lazy_tlb(&init_mm, current);
7586
7587 /*
7588 * Make us the idle thread. Technically, schedule() should not be
7589 * called from this thread, however somewhere below it might be,
7590 * but because we are the idle thread, we just pick up running again
7591 * when this runqueue becomes "idle".
7592 */
7593 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007594
7595 calc_load_update = jiffies + LOAD_FREQ;
7596
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307597#ifdef CONFIG_SMP
Peter Zijlstra4cb98832011-04-07 14:09:58 +02007598 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
Rusty Russellbdddd292009-12-02 14:09:16 +10307599 /* May be allocated at isolcpus cmdline parse time */
7600 if (cpu_isolated_map == NULL)
7601 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Thomas Gleixner29d5e042012-04-20 13:05:45 +00007602 idle_thread_set_boot_cpu();
Thomas Gleixner9cf72432016-03-10 12:54:09 +01007603 set_cpu_rq_start_time(smp_processor_id());
Peter Zijlstra029632f2011-10-25 10:00:11 +02007604#endif
7605 init_sched_fair_class();
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307606
Josh Poimboeuf4698f882016-06-07 14:43:16 -05007607 init_schedstats();
7608
Ingo Molnar6892b752008-02-13 14:02:36 +01007609 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610}
7611
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02007612#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007613static inline int preempt_count_equals(int preempt_offset)
7614{
Peter Zijlstrada7142e2015-09-28 18:11:45 +02007615 int nested = preempt_count() + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007616
Arnd Bergmann4ba82162011-01-25 22:52:22 +01007617 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007618}
7619
Simon Kagstromd8948372009-12-23 11:08:18 +01007620void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621{
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007622 /*
7623 * Blocking primitives will set (and therefore destroy) current->state,
7624 * since we will exit with TASK_RUNNING make sure we enter with it,
7625 * otherwise we will destroy state.
7626 */
Linus Torvalds00845eb2015-02-01 12:23:32 -08007627 WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007628 "do not call blocking ops when !TASK_RUNNING; "
7629 "state=%lx set at [<%p>] %pS\n",
7630 current->state,
7631 (void *)current->task_state_change,
Linus Torvalds00845eb2015-02-01 12:23:32 -08007632 (void *)current->task_state_change);
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007633
Peter Zijlstra34274452014-09-24 10:18:56 +02007634 ___might_sleep(file, line, preempt_offset);
7635}
7636EXPORT_SYMBOL(__might_sleep);
7637
7638void ___might_sleep(const char *file, int line, int preempt_offset)
7639{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 static unsigned long prev_jiffy; /* ratelimiting */
Vegard Nossumd1c6d142016-07-23 09:46:39 +02007641 unsigned long preempt_disable_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07007643 rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
Thomas Gleixnerdb273be2014-02-07 20:58:38 +01007644 if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
7645 !is_idle_task(current)) ||
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007646 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007647 return;
7648 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7649 return;
7650 prev_jiffy = jiffies;
7651
Vegard Nossumd1c6d142016-07-23 09:46:39 +02007652 /* Save this before calling printk(), since that will clobber it */
7653 preempt_disable_ip = get_preempt_disable_ip(current);
7654
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007655 printk(KERN_ERR
7656 "BUG: sleeping function called from invalid context at %s:%d\n",
7657 file, line);
7658 printk(KERN_ERR
7659 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7660 in_atomic(), irqs_disabled(),
7661 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007662
Eric Sandeena8b686b2014-12-16 16:25:28 -06007663 if (task_stack_end_corrupted(current))
7664 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
7665
Ingo Molnaraef745f2008-08-28 11:34:43 +02007666 debug_show_held_locks(current);
7667 if (irqs_disabled())
7668 print_irqtrace_events(current);
Vegard Nossumd1c6d142016-07-23 09:46:39 +02007669 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)
7670 && !preempt_count_equals(preempt_offset)) {
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007671 pr_err("Preemption disabled at:");
Vegard Nossumd1c6d142016-07-23 09:46:39 +02007672 print_ip_sym(preempt_disable_ip);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007673 pr_cont("\n");
7674 }
Ingo Molnaraef745f2008-08-28 11:34:43 +02007675 dump_stack();
Vegard Nossumf0b22e32016-07-22 21:46:02 +02007676 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677}
Peter Zijlstra34274452014-09-24 10:18:56 +02007678EXPORT_SYMBOL(___might_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679#endif
7680
7681#ifdef CONFIG_MAGIC_SYSRQ
7682void normalize_rt_tasks(void)
7683{
7684 struct task_struct *g, *p;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007685 struct sched_attr attr = {
7686 .sched_policy = SCHED_NORMAL,
7687 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007689 read_lock(&tasklist_lock);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007690 for_each_process_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007691 /*
7692 * Only normalize user tasks:
7693 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007694 if (p->flags & PF_KTHREAD)
Ingo Molnar178be792007-10-15 17:00:18 +02007695 continue;
7696
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05007697 p->se.exec_start = 0;
7698 schedstat_set(p->se.statistics.wait_start, 0);
7699 schedstat_set(p->se.statistics.sleep_start, 0);
7700 schedstat_set(p->se.statistics.block_start, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02007701
Dario Faggioliaab03e02013-11-28 11:14:43 +01007702 if (!dl_task(p) && !rt_task(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007703 /*
7704 * Renice negative nice level userspace
7705 * tasks back to 0:
7706 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007707 if (task_nice(p) < 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02007708 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007712 __sched_setscheduler(p, &attr, false, false);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007713 }
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007714 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715}
7716
7717#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007718
Jason Wessel67fc4e02010-05-20 21:04:21 -05007719#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007720/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007721 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007722 *
7723 * They can only be called when the whole system has been
7724 * stopped - every CPU needs to be quiescent, and no scheduling
7725 * activity can take place. Using them for anything else would
7726 * be a serious bug, and as a result, they aren't even visible
7727 * under any other configuration.
7728 */
7729
7730/**
7731 * curr_task - return the current task for a given cpu.
7732 * @cpu: the processor in question.
7733 *
7734 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
Yacine Belkadie69f6182013-07-12 20:45:47 +02007735 *
7736 * Return: The current task for @cpu.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007737 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007738struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007739{
7740 return cpu_curr(cpu);
7741}
7742
Jason Wessel67fc4e02010-05-20 21:04:21 -05007743#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7744
7745#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07007746/**
7747 * set_curr_task - set the current task for a given cpu.
7748 * @cpu: the processor in question.
7749 * @p: the task pointer to set.
7750 *
7751 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007752 * are serviced on a separate stack. It allows the architecture to switch the
7753 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007754 * must be called with all CPU's synchronized, and interrupts disabled, the
7755 * and caller must save the original value of the current task (see
7756 * curr_task() above) and restore that value before reenabling interrupts and
7757 * re-starting the system.
7758 *
7759 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7760 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007761void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007762{
7763 cpu_curr(cpu) = p;
7764}
7765
7766#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007767
Dhaval Giani7c941432010-01-20 13:26:18 +01007768#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007769/* task_group_lock serializes the addition/removal of task groups */
7770static DEFINE_SPINLOCK(task_group_lock);
7771
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007772static void sched_free_group(struct task_group *tg)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007773{
7774 free_fair_sched_group(tg);
7775 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01007776 autogroup_free(tg);
Waiman Longb0367622015-12-02 13:41:49 -05007777 kmem_cache_free(task_group_cache, tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007778}
7779
7780/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007781struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007782{
7783 struct task_group *tg;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007784
Waiman Longb0367622015-12-02 13:41:49 -05007785 tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007786 if (!tg)
7787 return ERR_PTR(-ENOMEM);
7788
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007789 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007790 goto err;
7791
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007792 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007793 goto err;
7794
Li Zefanace783b2013-01-24 14:30:48 +08007795 return tg;
7796
7797err:
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007798 sched_free_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08007799 return ERR_PTR(-ENOMEM);
7800}
7801
7802void sched_online_group(struct task_group *tg, struct task_group *parent)
7803{
7804 unsigned long flags;
7805
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007806 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007807 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007808
7809 WARN_ON(!parent); /* root should already exist */
7810
7811 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007812 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08007813 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007814 spin_unlock_irqrestore(&task_group_lock, flags);
Peter Zijlstra8663e242016-06-22 14:58:02 +02007815
7816 online_fair_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007817}
7818
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007819/* rcu callback to free various structures associated with a task group */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007820static void sched_free_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007821{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007822 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007823 sched_free_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007824}
7825
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007826void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007827{
Li Zefanace783b2013-01-24 14:30:48 +08007828 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007829 call_rcu(&tg->rcu, sched_free_group_rcu);
Li Zefanace783b2013-01-24 14:30:48 +08007830}
7831
7832void sched_offline_group(struct task_group *tg)
7833{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007834 unsigned long flags;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007835
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007836 /* end participation in shares distribution */
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01007837 unregister_fair_sched_group(tg);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007838
7839 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007840 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007841 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007842 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007843}
7844
Vincent Guittotea86cb42016-06-17 13:38:55 +02007845static void sched_change_group(struct task_struct *tsk, int type)
7846{
7847 struct task_group *tg;
7848
7849 /*
7850 * All callers are synchronized by task_rq_lock(); we do not use RCU
7851 * which is pointless here. Thus, we pass "true" to task_css_check()
7852 * to prevent lockdep warnings.
7853 */
7854 tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
7855 struct task_group, css);
7856 tg = autogroup_task_group(tsk, tg);
7857 tsk->sched_task_group = tg;
7858
7859#ifdef CONFIG_FAIR_GROUP_SCHED
7860 if (tsk->sched_class->task_change_group)
7861 tsk->sched_class->task_change_group(tsk, type);
7862 else
7863#endif
7864 set_task_rq(tsk, task_cpu(tsk));
7865}
7866
7867/*
7868 * Change task's runqueue when it moves between groups.
7869 *
7870 * The caller of this function should have put the task in its new group by
7871 * now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect
7872 * its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007873 */
7874void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007875{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007876 int queued, running;
Peter Zijlstraeb580752015-07-31 21:28:18 +02007877 struct rq_flags rf;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007878 struct rq *rq;
7879
Peter Zijlstraeb580752015-07-31 21:28:18 +02007880 rq = task_rq_lock(tsk, &rf);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007881
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007882 running = task_current(rq, tsk);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007883 queued = task_on_rq_queued(tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007884
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007885 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01007886 dequeue_task(rq, tsk, DEQUEUE_SAVE | DEQUEUE_MOVE);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007887 if (unlikely(running))
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04007888 put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007889
Vincent Guittotea86cb42016-06-17 13:38:55 +02007890 sched_change_group(tsk, TASK_MOVE_GROUP);
Peter Zijlstra810b3812008-02-29 15:21:01 -05007891
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007892 if (unlikely(running))
7893 tsk->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007894 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01007895 enqueue_task(rq, tsk, ENQUEUE_RESTORE | ENQUEUE_MOVE);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007896
Peter Zijlstraeb580752015-07-31 21:28:18 +02007897 task_rq_unlock(rq, tsk, &rf);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007898}
Dhaval Giani7c941432010-01-20 13:26:18 +01007899#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007900
Paul Turnera790de92011-07-21 09:43:29 -07007901#ifdef CONFIG_RT_GROUP_SCHED
7902/*
7903 * Ensure that the real time constraints are schedulable.
7904 */
7905static DEFINE_MUTEX(rt_constraints_mutex);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007906
Dhaval Giani521f1a242008-02-28 15:21:56 +05307907/* Must be called with tasklist_lock held */
7908static inline int tg_has_rt_tasks(struct task_group *tg)
7909{
7910 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007911
Peter Zijlstra1fe89e12015-02-09 11:53:18 +01007912 /*
7913 * Autogroups do not have RT tasks; see autogroup_create().
7914 */
7915 if (task_group_is_autogroup(tg))
7916 return 0;
7917
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007918 for_each_process_thread(g, p) {
Oleg Nesterov8651c652014-09-21 21:33:36 +02007919 if (rt_task(p) && task_group(p) == tg)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307920 return 1;
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007921 }
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007922
Dhaval Giani521f1a242008-02-28 15:21:56 +05307923 return 0;
7924}
7925
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007926struct rt_schedulable_data {
7927 struct task_group *tg;
7928 u64 rt_period;
7929 u64 rt_runtime;
7930};
7931
Paul Turnera790de92011-07-21 09:43:29 -07007932static int tg_rt_schedulable(struct task_group *tg, void *data)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007933{
7934 struct rt_schedulable_data *d = data;
7935 struct task_group *child;
7936 unsigned long total, sum = 0;
7937 u64 period, runtime;
7938
7939 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7940 runtime = tg->rt_bandwidth.rt_runtime;
7941
7942 if (tg == d->tg) {
7943 period = d->rt_period;
7944 runtime = d->rt_runtime;
7945 }
7946
Peter Zijlstra4653f802008-09-23 15:33:44 +02007947 /*
7948 * Cannot have more runtime than the period.
7949 */
7950 if (runtime > period && runtime != RUNTIME_INF)
7951 return -EINVAL;
7952
7953 /*
7954 * Ensure we don't starve existing RT tasks.
7955 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007956 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
7957 return -EBUSY;
7958
7959 total = to_ratio(period, runtime);
7960
Peter Zijlstra4653f802008-09-23 15:33:44 +02007961 /*
7962 * Nobody can have more than the global setting allows.
7963 */
7964 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
7965 return -EINVAL;
7966
7967 /*
7968 * The sum of our children's runtime should not exceed our own.
7969 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007970 list_for_each_entry_rcu(child, &tg->children, siblings) {
7971 period = ktime_to_ns(child->rt_bandwidth.rt_period);
7972 runtime = child->rt_bandwidth.rt_runtime;
7973
7974 if (child == d->tg) {
7975 period = d->rt_period;
7976 runtime = d->rt_runtime;
7977 }
7978
7979 sum += to_ratio(period, runtime);
7980 }
7981
7982 if (sum > total)
7983 return -EINVAL;
7984
7985 return 0;
7986}
7987
7988static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
7989{
Paul Turner82774342011-07-21 09:43:35 -07007990 int ret;
7991
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007992 struct rt_schedulable_data data = {
7993 .tg = tg,
7994 .rt_period = period,
7995 .rt_runtime = runtime,
7996 };
7997
Paul Turner82774342011-07-21 09:43:35 -07007998 rcu_read_lock();
7999 ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
8000 rcu_read_unlock();
8001
8002 return ret;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008003}
8004
Paul Turnerab84d312011-07-21 09:43:28 -07008005static int tg_set_rt_bandwidth(struct task_group *tg,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008006 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008007{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008008 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008009
Peter Zijlstra2636ed52015-02-09 12:23:20 +01008010 /*
8011 * Disallowing the root group RT runtime is BAD, it would disallow the
8012 * kernel creating (and or operating) RT threads.
8013 */
8014 if (tg == &root_task_group && rt_runtime == 0)
8015 return -EINVAL;
8016
8017 /* No period doesn't make any sense. */
8018 if (rt_period == 0)
8019 return -EINVAL;
8020
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008021 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308022 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008023 err = __rt_schedulable(tg, rt_period, rt_runtime);
8024 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05308025 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008026
Thomas Gleixner0986b112009-11-17 15:32:06 +01008027 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008028 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8029 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008030
8031 for_each_possible_cpu(i) {
8032 struct rt_rq *rt_rq = tg->rt_rq[i];
8033
Thomas Gleixner0986b112009-11-17 15:32:06 +01008034 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008035 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01008036 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008037 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008038 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra49246272010-10-17 21:46:10 +02008039unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308040 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008041 mutex_unlock(&rt_constraints_mutex);
8042
8043 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008044}
8045
Li Zefan25cc7da2013-03-05 16:07:33 +08008046static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008047{
8048 u64 rt_runtime, rt_period;
8049
8050 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8051 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8052 if (rt_runtime_us < 0)
8053 rt_runtime = RUNTIME_INF;
8054
Paul Turnerab84d312011-07-21 09:43:28 -07008055 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008056}
8057
Li Zefan25cc7da2013-03-05 16:07:33 +08008058static long sched_group_rt_runtime(struct task_group *tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008059{
8060 u64 rt_runtime_us;
8061
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008062 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008063 return -1;
8064
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008065 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008066 do_div(rt_runtime_us, NSEC_PER_USEC);
8067 return rt_runtime_us;
8068}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008069
Nicholas Mc Guirece2f5fe2015-05-03 10:51:56 +02008070static int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008071{
8072 u64 rt_runtime, rt_period;
8073
Nicholas Mc Guirece2f5fe2015-05-03 10:51:56 +02008074 rt_period = rt_period_us * NSEC_PER_USEC;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008075 rt_runtime = tg->rt_bandwidth.rt_runtime;
8076
Paul Turnerab84d312011-07-21 09:43:28 -07008077 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008078}
8079
Li Zefan25cc7da2013-03-05 16:07:33 +08008080static long sched_group_rt_period(struct task_group *tg)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008081{
8082 u64 rt_period_us;
8083
8084 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8085 do_div(rt_period_us, NSEC_PER_USEC);
8086 return rt_period_us;
8087}
Dario Faggioli332ac172013-11-07 14:43:45 +01008088#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008089
Dario Faggioli332ac172013-11-07 14:43:45 +01008090#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008091static int sched_rt_global_constraints(void)
8092{
8093 int ret = 0;
8094
8095 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008096 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02008097 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008098 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008099 mutex_unlock(&rt_constraints_mutex);
8100
8101 return ret;
8102}
Dhaval Giani54e99122009-02-27 15:13:54 +05308103
Li Zefan25cc7da2013-03-05 16:07:33 +08008104static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
Dhaval Giani54e99122009-02-27 15:13:54 +05308105{
8106 /* Don't accept realtime tasks when there is no way for them to run */
8107 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
8108 return 0;
8109
8110 return 1;
8111}
8112
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008113#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008114static int sched_rt_global_constraints(void)
8115{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008116 unsigned long flags;
Muhammad Falak R Wani8c5e9552016-05-05 15:21:19 +05308117 int i;
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008118
Thomas Gleixner0986b112009-11-17 15:32:06 +01008119 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008120 for_each_possible_cpu(i) {
8121 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8122
Thomas Gleixner0986b112009-11-17 15:32:06 +01008123 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008124 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01008125 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008126 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008127 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008128
Muhammad Falak R Wani8c5e9552016-05-05 15:21:19 +05308129 return 0;
Dario Faggioli332ac172013-11-07 14:43:45 +01008130}
8131#endif /* CONFIG_RT_GROUP_SCHED */
8132
Wanpeng Lia1963b82015-03-17 19:15:31 +08008133static int sched_dl_global_validate(void)
Dario Faggioli332ac172013-11-07 14:43:45 +01008134{
Peter Zijlstra17248132013-12-17 12:44:49 +01008135 u64 runtime = global_rt_runtime();
8136 u64 period = global_rt_period();
Dario Faggioli332ac172013-11-07 14:43:45 +01008137 u64 new_bw = to_ratio(period, runtime);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008138 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01008139 int cpu, ret = 0;
Juri Lelli49516342014-02-11 09:24:27 +01008140 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01008141
8142 /*
8143 * Here we want to check the bandwidth not being set to some
8144 * value smaller than the currently allocated bandwidth in
8145 * any of the root_domains.
8146 *
8147 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
8148 * cycling on root_domains... Discussion on different/better
8149 * solutions is welcome!
8150 */
Peter Zijlstra17248132013-12-17 12:44:49 +01008151 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008152 rcu_read_lock_sched();
8153 dl_b = dl_bw_of(cpu);
Dario Faggioli332ac172013-11-07 14:43:45 +01008154
Juri Lelli49516342014-02-11 09:24:27 +01008155 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01008156 if (new_bw < dl_b->total_bw)
8157 ret = -EBUSY;
Juri Lelli49516342014-02-11 09:24:27 +01008158 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01008159
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008160 rcu_read_unlock_sched();
8161
Peter Zijlstra17248132013-12-17 12:44:49 +01008162 if (ret)
8163 break;
Dario Faggioli332ac172013-11-07 14:43:45 +01008164 }
8165
Peter Zijlstra17248132013-12-17 12:44:49 +01008166 return ret;
8167}
8168
8169static void sched_dl_do_global(void)
8170{
8171 u64 new_bw = -1;
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008172 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01008173 int cpu;
Juri Lelli49516342014-02-11 09:24:27 +01008174 unsigned long flags;
Peter Zijlstra17248132013-12-17 12:44:49 +01008175
8176 def_dl_bandwidth.dl_period = global_rt_period();
8177 def_dl_bandwidth.dl_runtime = global_rt_runtime();
8178
8179 if (global_rt_runtime() != RUNTIME_INF)
8180 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
8181
8182 /*
8183 * FIXME: As above...
8184 */
8185 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008186 rcu_read_lock_sched();
8187 dl_b = dl_bw_of(cpu);
Peter Zijlstra17248132013-12-17 12:44:49 +01008188
Juri Lelli49516342014-02-11 09:24:27 +01008189 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01008190 dl_b->bw = new_bw;
Juri Lelli49516342014-02-11 09:24:27 +01008191 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008192
8193 rcu_read_unlock_sched();
Peter Zijlstra17248132013-12-17 12:44:49 +01008194 }
8195}
8196
8197static int sched_rt_global_validate(void)
8198{
8199 if (sysctl_sched_rt_period <= 0)
8200 return -EINVAL;
8201
Juri Lellie9e7cb32014-02-11 09:24:26 +01008202 if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
8203 (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
Peter Zijlstra17248132013-12-17 12:44:49 +01008204 return -EINVAL;
8205
Dario Faggioli332ac172013-11-07 14:43:45 +01008206 return 0;
8207}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008208
Peter Zijlstra17248132013-12-17 12:44:49 +01008209static void sched_rt_do_global(void)
8210{
8211 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8212 def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
8213}
8214
8215int sched_rt_handler(struct ctl_table *table, int write,
8216 void __user *buffer, size_t *lenp,
8217 loff_t *ppos)
8218{
8219 int old_period, old_runtime;
8220 static DEFINE_MUTEX(mutex);
8221 int ret;
8222
8223 mutex_lock(&mutex);
8224 old_period = sysctl_sched_rt_period;
8225 old_runtime = sysctl_sched_rt_runtime;
8226
8227 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8228
8229 if (!ret && write) {
8230 ret = sched_rt_global_validate();
8231 if (ret)
8232 goto undo;
8233
Wanpeng Lia1963b82015-03-17 19:15:31 +08008234 ret = sched_dl_global_validate();
Peter Zijlstra17248132013-12-17 12:44:49 +01008235 if (ret)
8236 goto undo;
8237
Wanpeng Lia1963b82015-03-17 19:15:31 +08008238 ret = sched_rt_global_constraints();
Peter Zijlstra17248132013-12-17 12:44:49 +01008239 if (ret)
8240 goto undo;
8241
8242 sched_rt_do_global();
8243 sched_dl_do_global();
8244 }
8245 if (0) {
8246undo:
8247 sysctl_sched_rt_period = old_period;
8248 sysctl_sched_rt_runtime = old_runtime;
8249 }
8250 mutex_unlock(&mutex);
8251
8252 return ret;
8253}
8254
Clark Williamsce0dbbb2013-02-07 09:47:04 -06008255int sched_rr_handler(struct ctl_table *table, int write,
8256 void __user *buffer, size_t *lenp,
8257 loff_t *ppos)
8258{
8259 int ret;
8260 static DEFINE_MUTEX(mutex);
8261
8262 mutex_lock(&mutex);
8263 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8264 /* make sure that internally we keep jiffies */
8265 /* also, writing zero resets timeslice to default */
8266 if (!ret && write) {
8267 sched_rr_timeslice = sched_rr_timeslice <= 0 ?
8268 RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice);
8269 }
8270 mutex_unlock(&mutex);
8271 return ret;
8272}
8273
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008274#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008275
Tejun Heoa7c6d552013-08-08 20:11:23 -04008276static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008277{
Tejun Heoa7c6d552013-08-08 20:11:23 -04008278 return css ? container_of(css, struct task_group, css) : NULL;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008279}
8280
Tejun Heoeb954192013-08-08 20:11:23 -04008281static struct cgroup_subsys_state *
8282cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008283{
Tejun Heoeb954192013-08-08 20:11:23 -04008284 struct task_group *parent = css_tg(parent_css);
8285 struct task_group *tg;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008286
Tejun Heoeb954192013-08-08 20:11:23 -04008287 if (!parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008288 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08008289 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008290 }
8291
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008292 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008293 if (IS_ERR(tg))
8294 return ERR_PTR(-ENOMEM);
8295
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008296 sched_online_group(tg, parent);
8297
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008298 return &tg->css;
8299}
8300
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008301static void cpu_cgroup_css_released(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08008302{
Tejun Heoeb954192013-08-08 20:11:23 -04008303 struct task_group *tg = css_tg(css);
Li Zefanace783b2013-01-24 14:30:48 +08008304
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008305 sched_offline_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08008306}
8307
Tejun Heoeb954192013-08-08 20:11:23 -04008308static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008309{
Tejun Heoeb954192013-08-08 20:11:23 -04008310 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008311
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008312 /*
8313 * Relies on the RCU grace period between css_released() and this.
8314 */
8315 sched_free_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08008316}
8317
Vincent Guittotea86cb42016-06-17 13:38:55 +02008318/*
8319 * This is called before wake_up_new_task(), therefore we really only
8320 * have to set its group bits, all the other stuff does not apply.
8321 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05008322static void cpu_cgroup_fork(struct task_struct *task)
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008323{
Vincent Guittotea86cb42016-06-17 13:38:55 +02008324 struct rq_flags rf;
8325 struct rq *rq;
8326
8327 rq = task_rq_lock(task, &rf);
8328
8329 sched_change_group(task, TASK_SET_GROUP);
8330
8331 task_rq_unlock(rq, task, &rf);
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008332}
8333
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008334static int cpu_cgroup_can_attach(struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008335{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008336 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008337 struct cgroup_subsys_state *css;
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02008338 int ret = 0;
Tejun Heobb9d97b2011-12-12 18:12:21 -08008339
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008340 cgroup_taskset_for_each(task, css, tset) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008341#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heoeb954192013-08-08 20:11:23 -04008342 if (!sched_rt_can_attach(css_tg(css), task))
Tejun Heobb9d97b2011-12-12 18:12:21 -08008343 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008344#else
Tejun Heobb9d97b2011-12-12 18:12:21 -08008345 /* We don't support RT-tasks being in separate groups */
8346 if (task->sched_class != &fair_sched_class)
8347 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008348#endif
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02008349 /*
8350 * Serialize against wake_up_new_task() such that if its
8351 * running, we're sure to observe its full state.
8352 */
8353 raw_spin_lock_irq(&task->pi_lock);
8354 /*
8355 * Avoid calling sched_move_task() before wake_up_new_task()
8356 * has happened. This would lead to problems with PELT, due to
8357 * move wanting to detach+attach while we're not attached yet.
8358 */
8359 if (task->state == TASK_NEW)
8360 ret = -EINVAL;
8361 raw_spin_unlock_irq(&task->pi_lock);
8362
8363 if (ret)
8364 break;
Tejun Heobb9d97b2011-12-12 18:12:21 -08008365 }
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02008366 return ret;
Ben Blumbe367d02009-09-23 15:56:31 -07008367}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008368
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008369static void cpu_cgroup_attach(struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008370{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008371 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008372 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08008373
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008374 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008375 sched_move_task(task);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008376}
8377
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008378#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008379static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
8380 struct cftype *cftype, u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008381{
Tejun Heo182446d2013-08-08 20:11:24 -04008382 return sched_group_set_shares(css_tg(css), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008383}
8384
Tejun Heo182446d2013-08-08 20:11:24 -04008385static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
8386 struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008387{
Tejun Heo182446d2013-08-08 20:11:24 -04008388 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008389
Nikhil Raoc8b28112011-05-18 14:37:48 -07008390 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008391}
Paul Turnerab84d312011-07-21 09:43:28 -07008392
8393#ifdef CONFIG_CFS_BANDWIDTH
Paul Turnera790de92011-07-21 09:43:29 -07008394static DEFINE_MUTEX(cfs_constraints_mutex);
8395
Paul Turnerab84d312011-07-21 09:43:28 -07008396const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
8397const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
8398
Paul Turnera790de92011-07-21 09:43:29 -07008399static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
8400
Paul Turnerab84d312011-07-21 09:43:28 -07008401static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
8402{
Paul Turner56f570e2011-11-07 20:26:33 -08008403 int i, ret = 0, runtime_enabled, runtime_was_enabled;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008404 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnerab84d312011-07-21 09:43:28 -07008405
8406 if (tg == &root_task_group)
8407 return -EINVAL;
8408
8409 /*
8410 * Ensure we have at some amount of bandwidth every period. This is
8411 * to prevent reaching a state of large arrears when throttled via
8412 * entity_tick() resulting in prolonged exit starvation.
8413 */
8414 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
8415 return -EINVAL;
8416
8417 /*
8418 * Likewise, bound things on the otherside by preventing insane quota
8419 * periods. This also allows us to normalize in computing quota
8420 * feasibility.
8421 */
8422 if (period > max_cfs_quota_period)
8423 return -EINVAL;
8424
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008425 /*
8426 * Prevent race between setting of cfs_rq->runtime_enabled and
8427 * unthrottle_offline_cfs_rqs().
8428 */
8429 get_online_cpus();
Paul Turnera790de92011-07-21 09:43:29 -07008430 mutex_lock(&cfs_constraints_mutex);
8431 ret = __cfs_schedulable(tg, period, quota);
8432 if (ret)
8433 goto out_unlock;
8434
Paul Turner58088ad2011-07-21 09:43:31 -07008435 runtime_enabled = quota != RUNTIME_INF;
Paul Turner56f570e2011-11-07 20:26:33 -08008436 runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
Ben Segall1ee14e62013-10-16 11:16:12 -07008437 /*
8438 * If we need to toggle cfs_bandwidth_used, off->on must occur
8439 * before making related changes, and on->off must occur afterwards
8440 */
8441 if (runtime_enabled && !runtime_was_enabled)
8442 cfs_bandwidth_usage_inc();
Paul Turnerab84d312011-07-21 09:43:28 -07008443 raw_spin_lock_irq(&cfs_b->lock);
8444 cfs_b->period = ns_to_ktime(period);
8445 cfs_b->quota = quota;
Paul Turner58088ad2011-07-21 09:43:31 -07008446
Paul Turnera9cf55b2011-07-21 09:43:32 -07008447 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07008448 /* restart the period timer (if active) to handle new period expiry */
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02008449 if (runtime_enabled)
8450 start_cfs_bandwidth(cfs_b);
Paul Turnerab84d312011-07-21 09:43:28 -07008451 raw_spin_unlock_irq(&cfs_b->lock);
8452
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008453 for_each_online_cpu(i) {
Paul Turnerab84d312011-07-21 09:43:28 -07008454 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
Peter Zijlstra029632f2011-10-25 10:00:11 +02008455 struct rq *rq = cfs_rq->rq;
Paul Turnerab84d312011-07-21 09:43:28 -07008456
8457 raw_spin_lock_irq(&rq->lock);
Paul Turner58088ad2011-07-21 09:43:31 -07008458 cfs_rq->runtime_enabled = runtime_enabled;
Paul Turnerab84d312011-07-21 09:43:28 -07008459 cfs_rq->runtime_remaining = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07008460
Peter Zijlstra029632f2011-10-25 10:00:11 +02008461 if (cfs_rq->throttled)
Paul Turner671fd9d2011-07-21 09:43:34 -07008462 unthrottle_cfs_rq(cfs_rq);
Paul Turnerab84d312011-07-21 09:43:28 -07008463 raw_spin_unlock_irq(&rq->lock);
8464 }
Ben Segall1ee14e62013-10-16 11:16:12 -07008465 if (runtime_was_enabled && !runtime_enabled)
8466 cfs_bandwidth_usage_dec();
Paul Turnera790de92011-07-21 09:43:29 -07008467out_unlock:
8468 mutex_unlock(&cfs_constraints_mutex);
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008469 put_online_cpus();
Paul Turnerab84d312011-07-21 09:43:28 -07008470
Paul Turnera790de92011-07-21 09:43:29 -07008471 return ret;
Paul Turnerab84d312011-07-21 09:43:28 -07008472}
8473
8474int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
8475{
8476 u64 quota, period;
8477
Peter Zijlstra029632f2011-10-25 10:00:11 +02008478 period = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008479 if (cfs_quota_us < 0)
8480 quota = RUNTIME_INF;
8481 else
8482 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
8483
8484 return tg_set_cfs_bandwidth(tg, period, quota);
8485}
8486
8487long tg_get_cfs_quota(struct task_group *tg)
8488{
8489 u64 quota_us;
8490
Peter Zijlstra029632f2011-10-25 10:00:11 +02008491 if (tg->cfs_bandwidth.quota == RUNTIME_INF)
Paul Turnerab84d312011-07-21 09:43:28 -07008492 return -1;
8493
Peter Zijlstra029632f2011-10-25 10:00:11 +02008494 quota_us = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008495 do_div(quota_us, NSEC_PER_USEC);
8496
8497 return quota_us;
8498}
8499
8500int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
8501{
8502 u64 quota, period;
8503
8504 period = (u64)cfs_period_us * NSEC_PER_USEC;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008505 quota = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008506
Paul Turnerab84d312011-07-21 09:43:28 -07008507 return tg_set_cfs_bandwidth(tg, period, quota);
8508}
8509
8510long tg_get_cfs_period(struct task_group *tg)
8511{
8512 u64 cfs_period_us;
8513
Peter Zijlstra029632f2011-10-25 10:00:11 +02008514 cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008515 do_div(cfs_period_us, NSEC_PER_USEC);
8516
8517 return cfs_period_us;
8518}
8519
Tejun Heo182446d2013-08-08 20:11:24 -04008520static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
8521 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008522{
Tejun Heo182446d2013-08-08 20:11:24 -04008523 return tg_get_cfs_quota(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008524}
8525
Tejun Heo182446d2013-08-08 20:11:24 -04008526static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
8527 struct cftype *cftype, s64 cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008528{
Tejun Heo182446d2013-08-08 20:11:24 -04008529 return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008530}
8531
Tejun Heo182446d2013-08-08 20:11:24 -04008532static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
8533 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008534{
Tejun Heo182446d2013-08-08 20:11:24 -04008535 return tg_get_cfs_period(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008536}
8537
Tejun Heo182446d2013-08-08 20:11:24 -04008538static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
8539 struct cftype *cftype, u64 cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008540{
Tejun Heo182446d2013-08-08 20:11:24 -04008541 return tg_set_cfs_period(css_tg(css), cfs_period_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008542}
8543
Paul Turnera790de92011-07-21 09:43:29 -07008544struct cfs_schedulable_data {
8545 struct task_group *tg;
8546 u64 period, quota;
8547};
8548
8549/*
8550 * normalize group quota/period to be quota/max_period
8551 * note: units are usecs
8552 */
8553static u64 normalize_cfs_quota(struct task_group *tg,
8554 struct cfs_schedulable_data *d)
8555{
8556 u64 quota, period;
8557
8558 if (tg == d->tg) {
8559 period = d->period;
8560 quota = d->quota;
8561 } else {
8562 period = tg_get_cfs_period(tg);
8563 quota = tg_get_cfs_quota(tg);
8564 }
8565
8566 /* note: these should typically be equivalent */
8567 if (quota == RUNTIME_INF || quota == -1)
8568 return RUNTIME_INF;
8569
8570 return to_ratio(period, quota);
8571}
8572
8573static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
8574{
8575 struct cfs_schedulable_data *d = data;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008576 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008577 s64 quota = 0, parent_quota = -1;
8578
8579 if (!tg->parent) {
8580 quota = RUNTIME_INF;
8581 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02008582 struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008583
8584 quota = normalize_cfs_quota(tg, d);
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008585 parent_quota = parent_b->hierarchical_quota;
Paul Turnera790de92011-07-21 09:43:29 -07008586
8587 /*
8588 * ensure max(child_quota) <= parent_quota, inherit when no
8589 * limit is set
8590 */
8591 if (quota == RUNTIME_INF)
8592 quota = parent_quota;
8593 else if (parent_quota != RUNTIME_INF && quota > parent_quota)
8594 return -EINVAL;
8595 }
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008596 cfs_b->hierarchical_quota = quota;
Paul Turnera790de92011-07-21 09:43:29 -07008597
8598 return 0;
8599}
8600
8601static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
8602{
Paul Turner82774342011-07-21 09:43:35 -07008603 int ret;
Paul Turnera790de92011-07-21 09:43:29 -07008604 struct cfs_schedulable_data data = {
8605 .tg = tg,
8606 .period = period,
8607 .quota = quota,
8608 };
8609
8610 if (quota != RUNTIME_INF) {
8611 do_div(data.period, NSEC_PER_USEC);
8612 do_div(data.quota, NSEC_PER_USEC);
8613 }
8614
Paul Turner82774342011-07-21 09:43:35 -07008615 rcu_read_lock();
8616 ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
8617 rcu_read_unlock();
8618
8619 return ret;
Paul Turnera790de92011-07-21 09:43:29 -07008620}
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008621
Tejun Heo2da8ca82013-12-05 12:28:04 -05008622static int cpu_stats_show(struct seq_file *sf, void *v)
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008623{
Tejun Heo2da8ca82013-12-05 12:28:04 -05008624 struct task_group *tg = css_tg(seq_css(sf));
Peter Zijlstra029632f2011-10-25 10:00:11 +02008625 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008626
Tejun Heo44ffc752013-12-05 12:28:01 -05008627 seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
8628 seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
8629 seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008630
8631 return 0;
8632}
Paul Turnerab84d312011-07-21 09:43:28 -07008633#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008634#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008635
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008636#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008637static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
8638 struct cftype *cft, s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008639{
Tejun Heo182446d2013-08-08 20:11:24 -04008640 return sched_group_set_rt_runtime(css_tg(css), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008641}
8642
Tejun Heo182446d2013-08-08 20:11:24 -04008643static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
8644 struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008645{
Tejun Heo182446d2013-08-08 20:11:24 -04008646 return sched_group_rt_runtime(css_tg(css));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008647}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008648
Tejun Heo182446d2013-08-08 20:11:24 -04008649static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
8650 struct cftype *cftype, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008651{
Tejun Heo182446d2013-08-08 20:11:24 -04008652 return sched_group_set_rt_period(css_tg(css), rt_period_us);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008653}
8654
Tejun Heo182446d2013-08-08 20:11:24 -04008655static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
8656 struct cftype *cft)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008657{
Tejun Heo182446d2013-08-08 20:11:24 -04008658 return sched_group_rt_period(css_tg(css));
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008659}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008660#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008661
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008662static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008663#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008664 {
8665 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008666 .read_u64 = cpu_shares_read_u64,
8667 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008668 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008669#endif
Paul Turnerab84d312011-07-21 09:43:28 -07008670#ifdef CONFIG_CFS_BANDWIDTH
8671 {
8672 .name = "cfs_quota_us",
8673 .read_s64 = cpu_cfs_quota_read_s64,
8674 .write_s64 = cpu_cfs_quota_write_s64,
8675 },
8676 {
8677 .name = "cfs_period_us",
8678 .read_u64 = cpu_cfs_period_read_u64,
8679 .write_u64 = cpu_cfs_period_write_u64,
8680 },
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008681 {
8682 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05008683 .seq_show = cpu_stats_show,
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008684 },
Paul Turnerab84d312011-07-21 09:43:28 -07008685#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008686#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008687 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008688 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008689 .read_s64 = cpu_rt_runtime_read,
8690 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008691 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008692 {
8693 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008694 .read_u64 = cpu_rt_period_read_uint,
8695 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008696 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008697#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07008698 { } /* terminate */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008699};
8700
Tejun Heo073219e2014-02-08 10:36:58 -05008701struct cgroup_subsys cpu_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008702 .css_alloc = cpu_cgroup_css_alloc,
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008703 .css_released = cpu_cgroup_css_released,
Tejun Heo92fb9742012-11-19 08:13:38 -08008704 .css_free = cpu_cgroup_css_free,
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008705 .fork = cpu_cgroup_fork,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008706 .can_attach = cpu_cgroup_can_attach,
8707 .attach = cpu_cgroup_attach,
Tejun Heo55779642014-07-15 11:05:09 -04008708 .legacy_cftypes = cpu_files,
Tejun Heob38e42e2016-02-23 10:00:50 -05008709 .early_init = true,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008710};
8711
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008712#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008713
Paul E. McKenneyb637a322012-09-19 16:58:38 -07008714void dump_cpu_task(int cpu)
8715{
8716 pr_info("Task dump for CPU %d:\n", cpu);
8717 sched_show_task(cpu_curr(cpu));
8718}
Andi Kleened82b8a2015-11-29 20:59:43 -08008719
8720/*
8721 * Nice levels are multiplicative, with a gentle 10% change for every
8722 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
8723 * nice 1, it will get ~10% less CPU time than another CPU-bound task
8724 * that remained on nice 0.
8725 *
8726 * The "10% effect" is relative and cumulative: from _any_ nice level,
8727 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
8728 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
8729 * If a task goes up by ~10% and another task goes down by ~10% then
8730 * the relative distance between them is ~25%.)
8731 */
8732const int sched_prio_to_weight[40] = {
8733 /* -20 */ 88761, 71755, 56483, 46273, 36291,
8734 /* -15 */ 29154, 23254, 18705, 14949, 11916,
8735 /* -10 */ 9548, 7620, 6100, 4904, 3906,
8736 /* -5 */ 3121, 2501, 1991, 1586, 1277,
8737 /* 0 */ 1024, 820, 655, 526, 423,
8738 /* 5 */ 335, 272, 215, 172, 137,
8739 /* 10 */ 110, 87, 70, 56, 45,
8740 /* 15 */ 36, 29, 23, 18, 15,
8741};
8742
8743/*
8744 * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated.
8745 *
8746 * In cases where the weight does not change often, we can use the
8747 * precalculated inverse to speed up arithmetics by turning divisions
8748 * into multiplications:
8749 */
8750const u32 sched_prio_to_wmult[40] = {
8751 /* -20 */ 48388, 59856, 76040, 92818, 118348,
8752 /* -15 */ 147320, 184698, 229616, 287308, 360437,
8753 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
8754 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
8755 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
8756 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
8757 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
8758 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
8759};