blob: a9a65ed772e384779c0e436ff6afb747a42eddda [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/mmu_context.h>
37#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
David Howells96f951e2012-03-28 18:30:03 +010078#include <asm/switch_to.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070079#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020080#include <asm/irq_regs.h>
Christian Borntraegerdb7e5272012-01-11 08:58:16 +010081#include <asm/mutex.h>
Glauber Costae6e66852011-07-11 15:28:17 -040082#ifdef CONFIG_PARAVIRT
83#include <asm/paravirt.h>
84#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Peter Zijlstra029632f2011-10-25 10:00:11 +020086#include "sched.h"
Tejun Heoea138442013-01-18 14:05:55 -080087#include "../workqueue_internal.h"
Thomas Gleixner29d5e042012-04-20 13:05:45 +000088#include "../smpboot.h"
Gregory Haskins6e0534f2008-05-12 21:21:01 +020089
Steven Rostedta8d154b2009-04-10 09:36:00 -040090#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040091#include <trace/events/sched.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040092
Peter Zijlstra029632f2011-10-25 10:00:11 +020093DEFINE_MUTEX(sched_domains_mutex);
94DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +020095
Peter Zijlstrafe44d622010-12-09 14:15:34 +010096static void update_rq_clock_task(struct rq *rq, s64 delta);
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -070097
Peter Zijlstra029632f2011-10-25 10:00:11 +020098void update_rq_clock(struct rq *rq)
Peter Zijlstra3e51f332008-05-03 18:29:28 +020099{
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100100 s64 delta;
Venkatesh Pallipadi305e6832010-10-04 17:03:21 -0700101
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100102 lockdep_assert_held(&rq->lock);
103
104 if (rq->clock_skip_update & RQCF_ACT_SKIP)
Mike Galbraithf26f9af2010-12-08 11:05:42 +0100105 return;
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700106
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100107 delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
Mike Galbraith4036ac12014-06-24 07:49:40 +0200108 if (delta < 0)
109 return;
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100110 rq->clock += delta;
111 update_rq_clock_task(rq, delta);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200112}
113
Ingo Molnare436d802007-07-19 21:28:35 +0200114/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200115 * Debugging: various feature bits
116 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200117
118#define SCHED_FEAT(name, enabled) \
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200119 (1UL << __SCHED_FEAT_##name) * enabled |
120
121const_debug unsigned int sysctl_sched_features =
Peter Zijlstra391e43d2011-11-15 17:14:39 +0100122#include "features.h"
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200123 0;
124
125#undef SCHED_FEAT
126
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200127/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100128 * Number of tasks to iterate in a single balance run.
129 * Limited because this is done with IRQs disabled.
130 */
131const_debug unsigned int sysctl_sched_nr_migrate = 32;
132
133/*
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200134 * period over which we average the RT time consumption, measured
135 * in ms.
136 *
137 * default: 1s
138 */
139const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
140
141/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100142 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100143 * default: 1s
144 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100145unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100146
Peter Zijlstra029632f2011-10-25 10:00:11 +0200147__read_mostly int scheduler_running;
Ingo Molnar6892b752008-02-13 14:02:36 +0100148
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100149/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100150 * part of the period that we allow rt tasks to run in us.
151 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100152 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100153int sysctl_sched_rt_runtime = 950000;
154
Rik van Riel3fa08182015-03-09 12:12:07 -0400155/* cpus with isolated domains */
156cpumask_var_t cpu_isolated_map;
157
Dario Faggioli332ac172013-11-07 14:43:45 +0100158/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800159 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200161static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 __acquires(rq->lock)
163{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700164 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 local_irq_disable();
167 rq = this_rq();
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100168 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 return rq;
171}
172
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100173#ifdef CONFIG_SCHED_HRTICK
174/*
175 * Use HR-timers to deliver accurate preemption points.
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100176 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100177
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100178static void hrtick_clear(struct rq *rq)
179{
180 if (hrtimer_active(&rq->hrtick_timer))
181 hrtimer_cancel(&rq->hrtick_timer);
182}
183
184/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100185 * High-resolution timer tick.
186 * Runs from hardirq context with interrupts disabled.
187 */
188static enum hrtimer_restart hrtick(struct hrtimer *timer)
189{
190 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
191
192 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
193
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100194 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200195 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100196 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100197 raw_spin_unlock(&rq->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100198
199 return HRTIMER_NORESTART;
200}
201
Rabin Vincent95e904c2008-05-11 05:55:33 +0530202#ifdef CONFIG_SMP
Peter Zijlstra971ee282013-06-28 11:18:53 +0200203
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000204static void __hrtick_restart(struct rq *rq)
Peter Zijlstra971ee282013-06-28 11:18:53 +0200205{
206 struct hrtimer *timer = &rq->hrtick_timer;
Peter Zijlstra971ee282013-06-28 11:18:53 +0200207
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000208 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200209}
210
Peter Zijlstra31656512008-07-18 18:01:23 +0200211/*
212 * called from hardirq (IPI) context
213 */
214static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200215{
Peter Zijlstra31656512008-07-18 18:01:23 +0200216 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200217
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100218 raw_spin_lock(&rq->lock);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200219 __hrtick_restart(rq);
Peter Zijlstra31656512008-07-18 18:01:23 +0200220 rq->hrtick_csd_pending = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100221 raw_spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200222}
223
Peter Zijlstra31656512008-07-18 18:01:23 +0200224/*
225 * Called to set the hrtick timer state.
226 *
227 * called with rq->lock held and irqs disabled
228 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200229void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200230{
Peter Zijlstra31656512008-07-18 18:01:23 +0200231 struct hrtimer *timer = &rq->hrtick_timer;
xiaofeng.yan177ef2a2014-08-26 03:15:41 +0000232 ktime_t time;
233 s64 delta;
234
235 /*
236 * Don't schedule slices shorter than 10000ns, that just
237 * doesn't make sense and can cause timer DoS.
238 */
239 delta = max_t(s64, delay, 10000LL);
240 time = ktime_add_ns(timer->base->get_time(), delta);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200241
Arjan van de Vencc584b22008-09-01 15:02:30 -0700242 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +0200243
244 if (rq == this_rq()) {
Peter Zijlstra971ee282013-06-28 11:18:53 +0200245 __hrtick_restart(rq);
Peter Zijlstra31656512008-07-18 18:01:23 +0200246 } else if (!rq->hrtick_csd_pending) {
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100247 smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
Peter Zijlstra31656512008-07-18 18:01:23 +0200248 rq->hrtick_csd_pending = 1;
249 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200250}
251
252static int
253hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
254{
255 int cpu = (int)(long)hcpu;
256
257 switch (action) {
258 case CPU_UP_CANCELED:
259 case CPU_UP_CANCELED_FROZEN:
260 case CPU_DOWN_PREPARE:
261 case CPU_DOWN_PREPARE_FROZEN:
262 case CPU_DEAD:
263 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +0200264 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200265 return NOTIFY_OK;
266 }
267
268 return NOTIFY_DONE;
269}
270
Rakib Mullickfa748202008-09-22 14:55:45 -0700271static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200272{
273 hotcpu_notifier(hotplug_hrtick, 0);
274}
Peter Zijlstra31656512008-07-18 18:01:23 +0200275#else
276/*
277 * Called to set the hrtick timer state.
278 *
279 * called with rq->lock held and irqs disabled
280 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200281void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstra31656512008-07-18 18:01:23 +0200282{
Wanpeng Li86893332014-11-26 08:44:06 +0800283 /*
284 * Don't schedule slices shorter than 10000ns, that just
285 * doesn't make sense. Rely on vruntime for fairness.
286 */
287 delay = max_t(u64, delay, 10000LL);
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000288 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
289 HRTIMER_MODE_REL_PINNED);
Peter Zijlstra31656512008-07-18 18:01:23 +0200290}
291
Andrew Morton006c75f2008-09-22 14:55:46 -0700292static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +0200293{
294}
Rabin Vincent95e904c2008-05-11 05:55:33 +0530295#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200296
297static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100298{
Peter Zijlstra31656512008-07-18 18:01:23 +0200299#ifdef CONFIG_SMP
300 rq->hrtick_csd_pending = 0;
301
302 rq->hrtick_csd.flags = 0;
303 rq->hrtick_csd.func = __hrtick_start;
304 rq->hrtick_csd.info = rq;
305#endif
306
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100307 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
308 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100309}
Andrew Morton006c75f2008-09-22 14:55:46 -0700310#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100311static inline void hrtick_clear(struct rq *rq)
312{
313}
314
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100315static inline void init_rq_hrtick(struct rq *rq)
316{
317}
318
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200319static inline void init_hrtick(void)
320{
321}
Andrew Morton006c75f2008-09-22 14:55:46 -0700322#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100323
Frederic Weisbecker55295782016-03-24 15:38:01 +0100324/*
325 * cmpxchg based fetch_or, macro so it works for different integer types
326 */
327#define fetch_or(ptr, mask) \
328 ({ \
329 typeof(ptr) _ptr = (ptr); \
330 typeof(mask) _mask = (mask); \
331 typeof(*_ptr) _old, _val = *_ptr; \
332 \
333 for (;;) { \
334 _old = cmpxchg(_ptr, _val, _val | _mask); \
335 if (_old == _val) \
336 break; \
337 _val = _old; \
338 } \
339 _old; \
340})
341
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700342#if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200343/*
344 * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
345 * this avoids any races wrt polling state changes and thereby avoids
346 * spurious IPIs.
347 */
348static bool set_nr_and_not_polling(struct task_struct *p)
349{
350 struct thread_info *ti = task_thread_info(p);
351 return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
352}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700353
354/*
355 * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
356 *
357 * If this returns true, then the idle task promises to call
358 * sched_ttwu_pending() and reschedule soon.
359 */
360static bool set_nr_if_polling(struct task_struct *p)
361{
362 struct thread_info *ti = task_thread_info(p);
Jason Low316c1608d2015-04-28 13:00:20 -0700363 typeof(ti->flags) old, val = READ_ONCE(ti->flags);
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700364
365 for (;;) {
366 if (!(val & _TIF_POLLING_NRFLAG))
367 return false;
368 if (val & _TIF_NEED_RESCHED)
369 return true;
370 old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED);
371 if (old == val)
372 break;
373 val = old;
374 }
375 return true;
376}
377
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200378#else
379static bool set_nr_and_not_polling(struct task_struct *p)
380{
381 set_tsk_need_resched(p);
382 return true;
383}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700384
385#ifdef CONFIG_SMP
386static bool set_nr_if_polling(struct task_struct *p)
387{
388 return false;
389}
390#endif
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200391#endif
392
Peter Zijlstra76751042015-05-01 08:27:50 -0700393void wake_q_add(struct wake_q_head *head, struct task_struct *task)
394{
395 struct wake_q_node *node = &task->wake_q;
396
397 /*
398 * Atomically grab the task, if ->wake_q is !nil already it means
399 * its already queued (either by us or someone else) and will get the
400 * wakeup due to that.
401 *
402 * This cmpxchg() implies a full barrier, which pairs with the write
403 * barrier implied by the wakeup in wake_up_list().
404 */
405 if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL))
406 return;
407
408 get_task_struct(task);
409
410 /*
411 * The head is context local, there can be no concurrency.
412 */
413 *head->lastp = node;
414 head->lastp = &node->next;
415}
416
417void wake_up_q(struct wake_q_head *head)
418{
419 struct wake_q_node *node = head->first;
420
421 while (node != WAKE_Q_TAIL) {
422 struct task_struct *task;
423
424 task = container_of(node, struct task_struct, wake_q);
425 BUG_ON(!task);
426 /* task can safely be re-inserted now */
427 node = node->next;
428 task->wake_q.next = NULL;
429
430 /*
431 * wake_up_process() implies a wmb() to pair with the queueing
432 * in wake_q_add() so as not to miss wakeups.
433 */
434 wake_up_process(task);
435 put_task_struct(task);
436 }
437}
438
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200439/*
Kirill Tkhai88751252014-06-29 00:03:57 +0400440 * resched_curr - mark rq's current task 'to be rescheduled now'.
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200441 *
442 * On UP this means the setting of the need_resched flag, on SMP it
443 * might also involve a cross-CPU call to trigger the scheduler on
444 * the target CPU.
445 */
Kirill Tkhai88751252014-06-29 00:03:57 +0400446void resched_curr(struct rq *rq)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200447{
Kirill Tkhai88751252014-06-29 00:03:57 +0400448 struct task_struct *curr = rq->curr;
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200449 int cpu;
450
Kirill Tkhai88751252014-06-29 00:03:57 +0400451 lockdep_assert_held(&rq->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200452
Kirill Tkhai88751252014-06-29 00:03:57 +0400453 if (test_tsk_need_resched(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200454 return;
455
Kirill Tkhai88751252014-06-29 00:03:57 +0400456 cpu = cpu_of(rq);
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200457
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200458 if (cpu == smp_processor_id()) {
Kirill Tkhai88751252014-06-29 00:03:57 +0400459 set_tsk_need_resched(curr);
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200460 set_preempt_need_resched();
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200461 return;
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200462 }
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200463
Kirill Tkhai88751252014-06-29 00:03:57 +0400464 if (set_nr_and_not_polling(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200465 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700466 else
467 trace_sched_wake_idle_without_ipi(cpu);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200468}
469
Peter Zijlstra029632f2011-10-25 10:00:11 +0200470void resched_cpu(int cpu)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200471{
472 struct rq *rq = cpu_rq(cpu);
473 unsigned long flags;
474
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100475 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200476 return;
Kirill Tkhai88751252014-06-29 00:03:57 +0400477 resched_curr(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100478 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200479}
Thomas Gleixner06d83082008-03-22 09:20:24 +0100480
Peter Zijlstrab021fe32013-09-17 09:30:55 +0200481#ifdef CONFIG_SMP
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200482#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixner06d83082008-03-22 09:20:24 +0100483/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700484 * In the semi idle case, use the nearest busy cpu for migrating timers
485 * from an idle cpu. This is good for power-savings.
486 *
487 * We don't do similar optimization for completely idle system, as
488 * selecting an idle cpu will add more delays to the timers than intended
489 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
490 */
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000491int get_nohz_timer_target(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700492{
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000493 int i, cpu = smp_processor_id();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700494 struct sched_domain *sd;
495
Vatika Harlalka9642d182015-09-01 16:50:59 +0200496 if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530497 return cpu;
498
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200499 rcu_read_lock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700500 for_each_domain(cpu, sd) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200501 for_each_cpu(i, sched_domain_span(sd)) {
Vatika Harlalka9642d182015-09-01 16:50:59 +0200502 if (!idle_cpu(i) && is_housekeeping_cpu(cpu)) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200503 cpu = i;
504 goto unlock;
505 }
506 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700507 }
Vatika Harlalka9642d182015-09-01 16:50:59 +0200508
509 if (!is_housekeeping_cpu(cpu))
510 cpu = housekeeping_any_cpu();
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200511unlock:
512 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700513 return cpu;
514}
515/*
Thomas Gleixner06d83082008-03-22 09:20:24 +0100516 * When add_timer_on() enqueues a timer into the timer wheel of an
517 * idle CPU then this timer might expire before the next timer event
518 * which is scheduled to wake up that CPU. In case of a completely
519 * idle system the next event might even be infinite time into the
520 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
521 * leaves the inner idle loop so the newly added timer is taken into
522 * account when the CPU goes back to idle and evaluates the timer
523 * wheel for the next timer event.
524 */
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200525static void wake_up_idle_cpu(int cpu)
Thomas Gleixner06d83082008-03-22 09:20:24 +0100526{
527 struct rq *rq = cpu_rq(cpu);
528
529 if (cpu == smp_processor_id())
530 return;
531
Andy Lutomirski67b9ca72014-06-04 10:31:17 -0700532 if (set_nr_and_not_polling(rq->idle))
Thomas Gleixner06d83082008-03-22 09:20:24 +0100533 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700534 else
535 trace_sched_wake_idle_without_ipi(cpu);
Thomas Gleixner06d83082008-03-22 09:20:24 +0100536}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100537
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200538static bool wake_up_full_nohz_cpu(int cpu)
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200539{
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200540 /*
541 * We just need the target to call irq_exit() and re-evaluate
542 * the next tick. The nohz full kick at least implies that.
543 * If needed we can still optimize that later with an
544 * empty IRQ.
545 */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200546 if (tick_nohz_full_cpu(cpu)) {
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200547 if (cpu != smp_processor_id() ||
548 tick_nohz_tick_stopped())
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200549 tick_nohz_full_kick_cpu(cpu);
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200550 return true;
551 }
552
553 return false;
554}
555
556void wake_up_nohz_cpu(int cpu)
557{
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200558 if (!wake_up_full_nohz_cpu(cpu))
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200559 wake_up_idle_cpu(cpu);
560}
561
Suresh Siddhaca380622011-10-03 15:09:00 -0700562static inline bool got_nohz_idle_kick(void)
563{
Suresh Siddha1c792db2011-12-01 17:07:32 -0800564 int cpu = smp_processor_id();
Vincent Guittot873b4c62013-06-05 10:13:11 +0200565
566 if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)))
567 return false;
568
569 if (idle_cpu(cpu) && !need_resched())
570 return true;
571
572 /*
573 * We can't run Idle Load Balance on this CPU for this time so we
574 * cancel it and clear NOHZ_BALANCE_KICK
575 */
576 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu));
577 return false;
Suresh Siddhaca380622011-10-03 15:09:00 -0700578}
579
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200580#else /* CONFIG_NO_HZ_COMMON */
Suresh Siddhaca380622011-10-03 15:09:00 -0700581
582static inline bool got_nohz_idle_kick(void)
583{
584 return false;
585}
586
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200587#endif /* CONFIG_NO_HZ_COMMON */
Thomas Gleixner06d83082008-03-22 09:20:24 +0100588
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200589#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200590bool sched_can_stop_tick(struct rq *rq)
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200591{
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200592 int fifo_nr_running;
593
594 /* Deadline tasks, even if single, need the tick */
595 if (rq->dl.dl_nr_running)
596 return false;
597
Frederic Weisbecker3882ec62014-03-18 22:54:04 +0100598 /*
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200599 * FIFO realtime policy runs the highest priority task (after DEADLINE).
600 * Other runnable tasks are of a lower priority. The scheduler tick
601 * isn't needed.
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500602 */
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200603 fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running;
604 if (fifo_nr_running)
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500605 return true;
606
607 /*
608 * Round-robin realtime tasks time slice with other tasks at the same
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200609 * realtime priority.
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500610 */
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200611 if (rq->rt.rr_nr_running) {
612 if (rq->rt.rr_nr_running == 1)
613 return true;
614 else
615 return false;
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500616 }
617
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200618 /* Normal multitasking need periodic preemption checks */
619 if (rq->cfs.nr_running > 1)
Viresh Kumar541b8262014-06-24 14:04:12 +0530620 return false;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200621
Viresh Kumar541b8262014-06-24 14:04:12 +0530622 return true;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200623}
624#endif /* CONFIG_NO_HZ_FULL */
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200625
Peter Zijlstra029632f2011-10-25 10:00:11 +0200626void sched_avg_update(struct rq *rq)
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200627{
628 s64 period = sched_avg_period();
629
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200630 while ((s64)(rq_clock(rq) - rq->age_stamp) > period) {
Will Deacon0d98bb22010-05-24 12:11:43 -0700631 /*
632 * Inline assembly required to prevent the compiler
633 * optimising this loop into a divmod call.
634 * See __iter_div_u64_rem() for another example of this.
635 */
636 asm("" : "+rm" (rq->age_stamp));
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200637 rq->age_stamp += period;
638 rq->rt_avg /= 2;
639 }
640}
641
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200642#endif /* CONFIG_SMP */
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200643
Paul Turnera790de92011-07-21 09:43:29 -0700644#if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
645 (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
Peter Zijlstraeb755802008-08-19 12:33:05 +0200646/*
Paul Turner82774342011-07-21 09:43:35 -0700647 * Iterate task_group tree rooted at *from, calling @down when first entering a
648 * node and @up when leaving it for the final time.
649 *
650 * Caller must hold rcu_lock or sufficient equivalent.
Peter Zijlstraeb755802008-08-19 12:33:05 +0200651 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200652int walk_tg_tree_from(struct task_group *from,
Paul Turner82774342011-07-21 09:43:35 -0700653 tg_visitor down, tg_visitor up, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200654{
655 struct task_group *parent, *child;
656 int ret;
657
Paul Turner82774342011-07-21 09:43:35 -0700658 parent = from;
659
Peter Zijlstraeb755802008-08-19 12:33:05 +0200660down:
661 ret = (*down)(parent, data);
662 if (ret)
Paul Turner82774342011-07-21 09:43:35 -0700663 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200664 list_for_each_entry_rcu(child, &parent->children, siblings) {
665 parent = child;
666 goto down;
667
668up:
669 continue;
670 }
671 ret = (*up)(parent, data);
Paul Turner82774342011-07-21 09:43:35 -0700672 if (ret || parent == from)
673 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200674
675 child = parent;
676 parent = parent->parent;
677 if (parent)
678 goto up;
Paul Turner82774342011-07-21 09:43:35 -0700679out:
Peter Zijlstraeb755802008-08-19 12:33:05 +0200680 return ret;
681}
682
Peter Zijlstra029632f2011-10-25 10:00:11 +0200683int tg_nop(struct task_group *tg, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200684{
685 return 0;
686}
687#endif
688
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200689static void set_load_weight(struct task_struct *p)
690{
Nikhil Raof05998d2011-05-18 10:09:38 -0700691 int prio = p->static_prio - MAX_RT_PRIO;
692 struct load_weight *load = &p->se.load;
693
Ingo Molnardd41f592007-07-09 18:51:59 +0200694 /*
695 * SCHED_IDLE tasks get minimal weight:
696 */
Henrik Austad20f9cd22015-09-09 17:00:41 +0200697 if (idle_policy(p->policy)) {
Nikhil Raoc8b28112011-05-18 14:37:48 -0700698 load->weight = scale_load(WEIGHT_IDLEPRIO);
Nikhil Raof05998d2011-05-18 10:09:38 -0700699 load->inv_weight = WMULT_IDLEPRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +0200700 return;
701 }
702
Andi Kleened82b8a2015-11-29 20:59:43 -0800703 load->weight = scale_load(sched_prio_to_weight[prio]);
704 load->inv_weight = sched_prio_to_wmult[prio];
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200705}
706
Peter Zijlstra1de64442015-09-30 17:44:13 +0200707static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -0600708{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100709 update_rq_clock(rq);
Peter Zijlstra1de64442015-09-30 17:44:13 +0200710 if (!(flags & ENQUEUE_RESTORE))
711 sched_info_queued(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100712 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +0200713}
714
Peter Zijlstra1de64442015-09-30 17:44:13 +0200715static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +0200716{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100717 update_rq_clock(rq);
Peter Zijlstra1de64442015-09-30 17:44:13 +0200718 if (!(flags & DEQUEUE_SAVE))
719 sched_info_dequeued(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100720 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200721}
722
Peter Zijlstra029632f2011-10-25 10:00:11 +0200723void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100724{
725 if (task_contributes_to_load(p))
726 rq->nr_uninterruptible--;
727
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100728 enqueue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100729}
730
Peter Zijlstra029632f2011-10-25 10:00:11 +0200731void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100732{
733 if (task_contributes_to_load(p))
734 rq->nr_uninterruptible++;
735
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100736 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100737}
738
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100739static void update_rq_clock_task(struct rq *rq, s64 delta)
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700740{
Glauber Costa095c0aa2011-07-11 15:28:18 -0400741/*
742 * In theory, the compile should just see 0 here, and optimize out the call
743 * to sched_rt_avg_update. But I don't trust it...
744 */
745#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
746 s64 steal = 0, irq_delta = 0;
747#endif
748#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Peter Zijlstra8e92c202010-12-09 14:15:34 +0100749 irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100750
751 /*
752 * Since irq_time is only updated on {soft,}irq_exit, we might run into
753 * this case when a previous update_rq_clock() happened inside a
754 * {soft,}irq region.
755 *
756 * When this happens, we stop ->clock_task and only update the
757 * prev_irq_time stamp to account for the part that fit, so that a next
758 * update will consume the rest. This ensures ->clock_task is
759 * monotonic.
760 *
761 * It does however cause some slight miss-attribution of {soft,}irq
762 * time, a more accurate solution would be to update the irq_time using
763 * the current rq->clock timestamp, except that would require using
764 * atomic ops.
765 */
766 if (irq_delta > delta)
767 irq_delta = delta;
768
769 rq->prev_irq_time += irq_delta;
770 delta -= irq_delta;
Glauber Costa095c0aa2011-07-11 15:28:18 -0400771#endif
772#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
Ingo Molnarc5905af2012-02-24 08:31:31 +0100773 if (static_key_false((&paravirt_steal_rq_enabled))) {
Glauber Costa095c0aa2011-07-11 15:28:18 -0400774 steal = paravirt_steal_clock(cpu_of(rq));
775 steal -= rq->prev_steal_time_rq;
776
777 if (unlikely(steal > delta))
778 steal = delta;
779
Glauber Costa095c0aa2011-07-11 15:28:18 -0400780 rq->prev_steal_time_rq += steal;
Glauber Costa095c0aa2011-07-11 15:28:18 -0400781 delta -= steal;
782 }
783#endif
784
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100785 rq->clock_task += delta;
786
Glauber Costa095c0aa2011-07-11 15:28:18 -0400787#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -0400788 if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
Glauber Costa095c0aa2011-07-11 15:28:18 -0400789 sched_rt_avg_update(rq, irq_delta + steal);
790#endif
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700791}
792
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200793void sched_set_stop_task(int cpu, struct task_struct *stop)
794{
795 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
796 struct task_struct *old_stop = cpu_rq(cpu)->stop;
797
798 if (stop) {
799 /*
800 * Make it appear like a SCHED_FIFO task, its something
801 * userspace knows about and won't get confused about.
802 *
803 * Also, it will make PI more or less work without too
804 * much confusion -- but then, stop work should not
805 * rely on PI working anyway.
806 */
807 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
808
809 stop->sched_class = &stop_sched_class;
810 }
811
812 cpu_rq(cpu)->stop = stop;
813
814 if (old_stop) {
815 /*
816 * Reset it back to a normal scheduling class so that
817 * it can die in pieces.
818 */
819 old_stop->sched_class = &rt_sched_class;
820 }
821}
822
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100823/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200824 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200825 */
Ingo Molnar14531182007-07-09 18:51:59 +0200826static inline int __normal_prio(struct task_struct *p)
827{
Ingo Molnardd41f592007-07-09 18:51:59 +0200828 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +0200829}
830
831/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700832 * Calculate the expected normal priority: i.e. priority
833 * without taking RT-inheritance into account. Might be
834 * boosted by interactivity modifiers. Changes upon fork,
835 * setprio syscalls, and whenever the interactivity
836 * estimator recalculates.
837 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700838static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700839{
840 int prio;
841
Dario Faggioliaab03e02013-11-28 11:14:43 +0100842 if (task_has_dl_policy(p))
843 prio = MAX_DL_PRIO-1;
844 else if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -0700845 prio = MAX_RT_PRIO-1 - p->rt_priority;
846 else
847 prio = __normal_prio(p);
848 return prio;
849}
850
851/*
852 * Calculate the current priority, i.e. the priority
853 * taken into account by the scheduler. This value might
854 * be boosted by RT tasks, or might be boosted by
855 * interactivity modifiers. Will be RT if the task got
856 * RT-boosted. If not then it returns p->normal_prio.
857 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700858static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700859{
860 p->normal_prio = normal_prio(p);
861 /*
862 * If we are RT tasks or we were boosted to RT priority,
863 * keep the priority unchanged. Otherwise, update priority
864 * to the normal priority:
865 */
866 if (!rt_prio(p->prio))
867 return p->normal_prio;
868 return p->prio;
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/**
872 * task_curr - is this task currently executing on a CPU?
873 * @p: the task in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +0200874 *
875 * Return: 1 if the task is currently executing. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700877inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 return cpu_curr(task_cpu(p)) == p;
880}
881
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +0300882/*
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +0200883 * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock,
884 * use the balance_callback list if you want balancing.
885 *
886 * this means any call to check_class_changed() must be followed by a call to
887 * balance_callback().
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +0300888 */
Steven Rostedtcb469842008-01-25 21:08:22 +0100889static inline void check_class_changed(struct rq *rq, struct task_struct *p,
890 const struct sched_class *prev_class,
Peter Zijlstrada7a7352011-01-17 17:03:27 +0100891 int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +0100892{
893 if (prev_class != p->sched_class) {
894 if (prev_class->switched_from)
Peter Zijlstrada7a7352011-01-17 17:03:27 +0100895 prev_class->switched_from(rq, p);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +0200896
Peter Zijlstrada7a7352011-01-17 17:03:27 +0100897 p->sched_class->switched_to(rq, p);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100898 } else if (oldprio != p->prio || dl_task(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +0100899 p->sched_class->prio_changed(rq, p, oldprio);
Steven Rostedtcb469842008-01-25 21:08:22 +0100900}
901
Peter Zijlstra029632f2011-10-25 10:00:11 +0200902void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e5a7402010-10-31 12:37:04 +0100903{
904 const struct sched_class *class;
905
906 if (p->sched_class == rq->curr->sched_class) {
907 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
908 } else {
909 for_each_class(class) {
910 if (class == rq->curr->sched_class)
911 break;
912 if (class == p->sched_class) {
Kirill Tkhai88751252014-06-29 00:03:57 +0400913 resched_curr(rq);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +0100914 break;
915 }
916 }
917 }
918
919 /*
920 * A queue event has occurred, and we're going to schedule. In
921 * this case, we can save a useless back to back clock update.
922 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +0400923 if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100924 rq_clock_skip_update(rq, true);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +0100925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927#ifdef CONFIG_SMP
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200928/*
929 * This is how migration works:
930 *
931 * 1) we invoke migration_cpu_stop() on the target CPU using
932 * stop_one_cpu().
933 * 2) stopper starts to run (implicitly forcing the migrated thread
934 * off the CPU)
935 * 3) it checks whether the migrated task is still in the wrong runqueue.
936 * 4) if it's in the wrong runqueue then the migration thread removes
937 * it and puts it into the right queue.
938 * 5) stopper completes and stop_one_cpu() returns and the migration
939 * is done.
940 */
941
942/*
943 * move_queued_task - move a queued task to new rq.
944 *
945 * Returns (locked) new rq. Old rq's lock is released.
946 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +0200947static struct rq *move_queued_task(struct rq *rq, struct task_struct *p, int new_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200948{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200949 lockdep_assert_held(&rq->lock);
950
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200951 p->on_rq = TASK_ON_RQ_MIGRATING;
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800952 dequeue_task(rq, p, 0);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200953 set_task_cpu(p, new_cpu);
954 raw_spin_unlock(&rq->lock);
955
956 rq = cpu_rq(new_cpu);
957
958 raw_spin_lock(&rq->lock);
959 BUG_ON(task_cpu(p) != new_cpu);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200960 enqueue_task(rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -0800961 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200962 check_preempt_curr(rq, p, 0);
963
964 return rq;
965}
966
967struct migration_arg {
968 struct task_struct *task;
969 int dest_cpu;
970};
971
972/*
973 * Move (not current) task off this cpu, onto dest cpu. We're doing
974 * this because either it can't run here any more (set_cpus_allowed()
975 * away from this CPU, or CPU going down), or because we're
976 * attempting to rebalance this task on exec (sched_exec).
977 *
978 * So we race with normal scheduler movements, but that's OK, as long
979 * as the task is no longer on this CPU.
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200980 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +0200981static struct rq *__migrate_task(struct rq *rq, struct task_struct *p, int dest_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200982{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200983 if (unlikely(!cpu_active(dest_cpu)))
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +0200984 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200985
986 /* Affinity changed (again). */
987 if (!cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +0200988 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200989
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +0200990 rq = move_queued_task(rq, p, dest_cpu);
991
992 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +0200993}
994
995/*
996 * migration_cpu_stop - this will be executed by a highprio stopper thread
997 * and performs thread migration by bumping thread off CPU then
998 * 'pushing' onto another runqueue.
999 */
1000static int migration_cpu_stop(void *data)
1001{
1002 struct migration_arg *arg = data;
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001003 struct task_struct *p = arg->task;
1004 struct rq *rq = this_rq();
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001005
1006 /*
1007 * The original target cpu might have gone down and we might
1008 * be on another cpu but it doesn't matter.
1009 */
1010 local_irq_disable();
1011 /*
1012 * We need to explicitly wake pending tasks before running
1013 * __migrate_task() such that we will not miss enforcing cpus_allowed
1014 * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
1015 */
1016 sched_ttwu_pending();
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001017
1018 raw_spin_lock(&p->pi_lock);
1019 raw_spin_lock(&rq->lock);
1020 /*
1021 * If task_rq(p) != rq, it cannot be migrated here, because we're
1022 * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
1023 * we're holding p->pi_lock.
1024 */
1025 if (task_rq(p) == rq && task_on_rq_queued(p))
1026 rq = __migrate_task(rq, p, arg->dest_cpu);
1027 raw_spin_unlock(&rq->lock);
1028 raw_spin_unlock(&p->pi_lock);
1029
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001030 local_irq_enable();
1031 return 0;
1032}
1033
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001034/*
1035 * sched_class::set_cpus_allowed must do the below, but is not required to
1036 * actually call this function.
1037 */
1038void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001039{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001040 cpumask_copy(&p->cpus_allowed, new_mask);
1041 p->nr_cpus_allowed = cpumask_weight(new_mask);
1042}
1043
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001044void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
1045{
Peter Zijlstra6c370672015-05-15 17:43:36 +02001046 struct rq *rq = task_rq(p);
1047 bool queued, running;
1048
Peter Zijlstra25834c72015-05-15 17:43:34 +02001049 lockdep_assert_held(&p->pi_lock);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001050
1051 queued = task_on_rq_queued(p);
1052 running = task_current(rq, p);
1053
1054 if (queued) {
1055 /*
1056 * Because __kthread_bind() calls this on blocked tasks without
1057 * holding rq->lock.
1058 */
1059 lockdep_assert_held(&rq->lock);
Peter Zijlstra1de64442015-09-30 17:44:13 +02001060 dequeue_task(rq, p, DEQUEUE_SAVE);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001061 }
1062 if (running)
1063 put_prev_task(rq, p);
1064
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001065 p->sched_class->set_cpus_allowed(p, new_mask);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001066
1067 if (running)
1068 p->sched_class->set_curr_task(rq);
1069 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02001070 enqueue_task(rq, p, ENQUEUE_RESTORE);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001071}
1072
1073/*
1074 * Change a given task's CPU affinity. Migrate the thread to a
1075 * proper CPU and schedule it away if the CPU it's executing on
1076 * is removed from the allowed bitmask.
1077 *
1078 * NOTE: the caller must have a valid reference to the task, the
1079 * task must not exit() & deallocate itself prematurely. The
1080 * call is not atomic; no spinlocks may be held.
1081 */
Peter Zijlstra25834c72015-05-15 17:43:34 +02001082static int __set_cpus_allowed_ptr(struct task_struct *p,
1083 const struct cpumask *new_mask, bool check)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001084{
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001085 const struct cpumask *cpu_valid_mask = cpu_active_mask;
1086 unsigned int dest_cpu;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001087 unsigned long flags;
1088 struct rq *rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001089 int ret = 0;
1090
1091 rq = task_rq_lock(p, &flags);
1092
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001093 if (p->flags & PF_KTHREAD) {
1094 /*
1095 * Kernel threads are allowed on online && !active CPUs
1096 */
1097 cpu_valid_mask = cpu_online_mask;
1098 }
1099
Peter Zijlstra25834c72015-05-15 17:43:34 +02001100 /*
1101 * Must re-check here, to close a race against __kthread_bind(),
1102 * sched_setaffinity() is not guaranteed to observe the flag.
1103 */
1104 if (check && (p->flags & PF_NO_SETAFFINITY)) {
1105 ret = -EINVAL;
1106 goto out;
1107 }
1108
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001109 if (cpumask_equal(&p->cpus_allowed, new_mask))
1110 goto out;
1111
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001112 if (!cpumask_intersects(new_mask, cpu_valid_mask)) {
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001113 ret = -EINVAL;
1114 goto out;
1115 }
1116
1117 do_set_cpus_allowed(p, new_mask);
1118
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001119 if (p->flags & PF_KTHREAD) {
1120 /*
1121 * For kernel threads that do indeed end up on online &&
1122 * !active we want to ensure they are strict per-cpu threads.
1123 */
1124 WARN_ON(cpumask_intersects(new_mask, cpu_online_mask) &&
1125 !cpumask_intersects(new_mask, cpu_active_mask) &&
1126 p->nr_cpus_allowed != 1);
1127 }
1128
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001129 /* Can the task run on the task's current CPU? If so, we're done */
1130 if (cpumask_test_cpu(task_cpu(p), new_mask))
1131 goto out;
1132
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001133 dest_cpu = cpumask_any_and(cpu_valid_mask, new_mask);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001134 if (task_running(rq, p) || p->state == TASK_WAKING) {
1135 struct migration_arg arg = { p, dest_cpu };
1136 /* Need help from migration thread: drop lock and wait. */
1137 task_rq_unlock(rq, p, &flags);
1138 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
1139 tlb_migrate_finish(p->mm);
1140 return 0;
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001141 } else if (task_on_rq_queued(p)) {
1142 /*
1143 * OK, since we're going to drop the lock immediately
1144 * afterwards anyway.
1145 */
1146 lockdep_unpin_lock(&rq->lock);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001147 rq = move_queued_task(rq, p, dest_cpu);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001148 lockdep_pin_lock(&rq->lock);
1149 }
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001150out:
1151 task_rq_unlock(rq, p, &flags);
1152
1153 return ret;
1154}
Peter Zijlstra25834c72015-05-15 17:43:34 +02001155
1156int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1157{
1158 return __set_cpus_allowed_ptr(p, new_mask, false);
1159}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001160EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
1161
Ingo Molnardd41f592007-07-09 18:51:59 +02001162void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001163{
Peter Zijlstrae2912002009-12-16 18:04:36 +01001164#ifdef CONFIG_SCHED_DEBUG
1165 /*
1166 * We should never call set_task_cpu() on a blocked task,
1167 * ttwu() will sort out the placement.
1168 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01001169 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
Oleg Nesterove2336f62014-10-08 20:33:48 +02001170 !p->on_rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001171
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001172 /*
1173 * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING,
1174 * because schedstat_wait_{start,end} rebase migrating task's wait_start
1175 * time relying on p->on_rq.
1176 */
1177 WARN_ON_ONCE(p->state == TASK_RUNNING &&
1178 p->sched_class == &fair_sched_class &&
1179 (p->on_rq && !task_on_rq_migrating(p)));
1180
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001181#ifdef CONFIG_LOCKDEP
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001182 /*
1183 * The caller should hold either p->pi_lock or rq->lock, when changing
1184 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
1185 *
1186 * sched_move_task() holds both and thus holding either pins the cgroup,
Peter Zijlstra8323f262012-06-22 13:36:05 +02001187 * see task_group().
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001188 *
1189 * Furthermore, all task_rq users should acquire both locks, see
1190 * task_rq_lock().
1191 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001192 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
1193 lockdep_is_held(&task_rq(p)->lock)));
1194#endif
Peter Zijlstrae2912002009-12-16 18:04:36 +01001195#endif
1196
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08001197 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001198
Peter Zijlstra0c697742009-12-22 15:43:19 +01001199 if (task_cpu(p) != new_cpu) {
Paul Turner0a74bef2012-10-04 13:18:30 +02001200 if (p->sched_class->migrate_task_rq)
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08001201 p->sched_class->migrate_task_rq(p);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001202 p->se.nr_migrations++;
Peter Zijlstraff303e62015-04-17 20:05:30 +02001203 perf_event_task_migrate(p);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001204 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001205
1206 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001207}
1208
Peter Zijlstraac66f542013-10-07 11:29:16 +01001209static void __migrate_swap_task(struct task_struct *p, int cpu)
1210{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001211 if (task_on_rq_queued(p)) {
Peter Zijlstraac66f542013-10-07 11:29:16 +01001212 struct rq *src_rq, *dst_rq;
1213
1214 src_rq = task_rq(p);
1215 dst_rq = cpu_rq(cpu);
1216
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001217 p->on_rq = TASK_ON_RQ_MIGRATING;
Peter Zijlstraac66f542013-10-07 11:29:16 +01001218 deactivate_task(src_rq, p, 0);
1219 set_task_cpu(p, cpu);
1220 activate_task(dst_rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001221 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstraac66f542013-10-07 11:29:16 +01001222 check_preempt_curr(dst_rq, p, 0);
1223 } else {
1224 /*
1225 * Task isn't running anymore; make it appear like we migrated
1226 * it before it went to sleep. This means on wakeup we make the
1227 * previous cpu our targer instead of where it really is.
1228 */
1229 p->wake_cpu = cpu;
1230 }
1231}
1232
1233struct migration_swap_arg {
1234 struct task_struct *src_task, *dst_task;
1235 int src_cpu, dst_cpu;
1236};
1237
1238static int migrate_swap_stop(void *data)
1239{
1240 struct migration_swap_arg *arg = data;
1241 struct rq *src_rq, *dst_rq;
1242 int ret = -EAGAIN;
1243
Peter Zijlstra62694cd2015-10-09 18:36:29 +02001244 if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu))
1245 return -EAGAIN;
1246
Peter Zijlstraac66f542013-10-07 11:29:16 +01001247 src_rq = cpu_rq(arg->src_cpu);
1248 dst_rq = cpu_rq(arg->dst_cpu);
1249
Peter Zijlstra74602312013-10-10 20:17:22 +02001250 double_raw_lock(&arg->src_task->pi_lock,
1251 &arg->dst_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001252 double_rq_lock(src_rq, dst_rq);
Peter Zijlstra62694cd2015-10-09 18:36:29 +02001253
Peter Zijlstraac66f542013-10-07 11:29:16 +01001254 if (task_cpu(arg->dst_task) != arg->dst_cpu)
1255 goto unlock;
1256
1257 if (task_cpu(arg->src_task) != arg->src_cpu)
1258 goto unlock;
1259
1260 if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task)))
1261 goto unlock;
1262
1263 if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task)))
1264 goto unlock;
1265
1266 __migrate_swap_task(arg->src_task, arg->dst_cpu);
1267 __migrate_swap_task(arg->dst_task, arg->src_cpu);
1268
1269 ret = 0;
1270
1271unlock:
1272 double_rq_unlock(src_rq, dst_rq);
Peter Zijlstra74602312013-10-10 20:17:22 +02001273 raw_spin_unlock(&arg->dst_task->pi_lock);
1274 raw_spin_unlock(&arg->src_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001275
1276 return ret;
1277}
1278
1279/*
1280 * Cross migrate two tasks
1281 */
1282int migrate_swap(struct task_struct *cur, struct task_struct *p)
1283{
1284 struct migration_swap_arg arg;
1285 int ret = -EINVAL;
1286
Peter Zijlstraac66f542013-10-07 11:29:16 +01001287 arg = (struct migration_swap_arg){
1288 .src_task = cur,
1289 .src_cpu = task_cpu(cur),
1290 .dst_task = p,
1291 .dst_cpu = task_cpu(p),
1292 };
1293
1294 if (arg.src_cpu == arg.dst_cpu)
1295 goto out;
1296
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02001297 /*
1298 * These three tests are all lockless; this is OK since all of them
1299 * will be re-checked with proper locks held further down the line.
1300 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01001301 if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
1302 goto out;
1303
1304 if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task)))
1305 goto out;
1306
1307 if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task)))
1308 goto out;
1309
Mel Gorman286549d2014-01-21 15:51:03 -08001310 trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001311 ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
1312
1313out:
Peter Zijlstraac66f542013-10-07 11:29:16 +01001314 return ret;
1315}
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * wait_task_inactive - wait for a thread to unschedule.
1319 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001320 * If @match_state is nonzero, it's the @p->state value just checked and
1321 * not expected to change. If it changes, i.e. @p might have woken up,
1322 * then return zero. When we succeed in waiting for @p to be off its CPU,
1323 * we return a positive number (its total switch count). If a second call
1324 * a short while later returns the same number, the caller can be sure that
1325 * @p has remained unscheduled the whole time.
1326 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 * The caller must ensure that the task *will* unschedule sometime soon,
1328 * else this function might spin for a *long* time. This function can't
1329 * be called with interrupts off, or it may introduce deadlock with
1330 * smp_call_function() if an IPI is sent by the same process we are
1331 * waiting to become inactive.
1332 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001333unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001336 int running, queued;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001337 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001338 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Andi Kleen3a5c3592007-10-15 17:00:14 +02001340 for (;;) {
1341 /*
1342 * We do the initial early heuristics without holding
1343 * any task-queue locks at all. We'll only try to get
1344 * the runqueue lock when things look like they will
1345 * work out!
1346 */
1347 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001348
Andi Kleen3a5c3592007-10-15 17:00:14 +02001349 /*
1350 * If the task is actively running on another CPU
1351 * still, just relax and busy-wait without holding
1352 * any locks.
1353 *
1354 * NOTE! Since we don't hold any locks, it's not
1355 * even sure that "rq" stays as the right runqueue!
1356 * But we don't care, since "task_running()" will
1357 * return false if the runqueue has changed and p
1358 * is actually now running somewhere else!
1359 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001360 while (task_running(rq, p)) {
1361 if (match_state && unlikely(p->state != match_state))
1362 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02001363 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07001364 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001365
Andi Kleen3a5c3592007-10-15 17:00:14 +02001366 /*
1367 * Ok, time to look more closely! We need the rq
1368 * lock now, to be *sure*. If we're wrong, we'll
1369 * just go back and repeat.
1370 */
1371 rq = task_rq_lock(p, &flags);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02001372 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001373 running = task_running(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001374 queued = task_on_rq_queued(p);
Roland McGrath85ba2d82008-07-25 19:45:58 -07001375 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07001376 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07001377 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001378 task_rq_unlock(rq, p, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001379
Andi Kleen3a5c3592007-10-15 17:00:14 +02001380 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07001381 * If it changed from the expected state, bail out now.
1382 */
1383 if (unlikely(!ncsw))
1384 break;
1385
1386 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02001387 * Was it really running after all now that we
1388 * checked with the proper locks actually held?
1389 *
1390 * Oops. Go back and try again..
1391 */
1392 if (unlikely(running)) {
1393 cpu_relax();
1394 continue;
1395 }
1396
1397 /*
1398 * It's not enough that it's not actively running,
1399 * it must be off the runqueue _entirely_, and not
1400 * preempted!
1401 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00001402 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02001403 * running right now), it's preempted, and we should
1404 * yield - it could be a while.
1405 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001406 if (unlikely(queued)) {
Thomas Gleixner8eb90c32011-02-23 23:52:21 +00001407 ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
1408
1409 set_current_state(TASK_UNINTERRUPTIBLE);
1410 schedule_hrtimeout(&to, HRTIMER_MODE_REL);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001411 continue;
1412 }
1413
1414 /*
1415 * Ahh, all good. It wasn't running, and it wasn't
1416 * runnable, which means that it will never become
1417 * running in the future either. We're all done!
1418 */
1419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07001421
1422 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425/***
1426 * kick_process - kick a running thread to enter/exit the kernel
1427 * @p: the to-be-kicked thread
1428 *
1429 * Cause a process which is running on another CPU to enter
1430 * kernel-mode, without any delay. (to get signals handled.)
1431 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001432 * NOTE: this function doesn't have to take the runqueue lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 * because all it wants to ensure is that the remote task enters
1434 * the kernel. If the IPI races and the task has been migrated
1435 * to another CPU then no harm is done and the purpose has been
1436 * achieved as well.
1437 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001438void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 int cpu;
1441
1442 preempt_disable();
1443 cpu = task_cpu(p);
1444 if ((cpu != smp_processor_id()) && task_curr(p))
1445 smp_send_reschedule(cpu);
1446 preempt_enable();
1447}
Rusty Russellb43e3522009-06-12 22:27:00 -06001448EXPORT_SYMBOL_GPL(kick_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Oleg Nesterov30da6882010-03-15 10:10:19 +01001450/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001451 * ->cpus_allowed is protected by both rq->lock and p->pi_lock
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001452 *
1453 * A few notes on cpu_active vs cpu_online:
1454 *
1455 * - cpu_active must be a subset of cpu_online
1456 *
1457 * - on cpu-up we allow per-cpu kthreads on the online && !active cpu,
1458 * see __set_cpus_allowed_ptr(). At this point the newly online
1459 * cpu isn't yet part of the sched domains, and balancing will not
1460 * see it.
1461 *
1462 * - on cpu-down we clear cpu_active() to mask the sched domains and
1463 * avoid the load balancer to place new tasks on the to be removed
1464 * cpu. Existing tasks will remain running there and will be taken
1465 * off.
1466 *
1467 * This means that fallback selection must not select !active CPUs.
1468 * And can assume that any active CPU must be online. Conversely
1469 * select_task_rq() below may allow selection of !active CPUs in order
1470 * to satisfy the above rules.
Oleg Nesterov30da6882010-03-15 10:10:19 +01001471 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001472static int select_fallback_rq(int cpu, struct task_struct *p)
1473{
Tang Chenaa00d892013-02-22 16:33:33 -08001474 int nid = cpu_to_node(cpu);
1475 const struct cpumask *nodemask = NULL;
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001476 enum { cpuset, possible, fail } state = cpuset;
1477 int dest_cpu;
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001478
Tang Chenaa00d892013-02-22 16:33:33 -08001479 /*
1480 * If the node that the cpu is on has been offlined, cpu_to_node()
1481 * will return -1. There is no cpu on the node, and we should
1482 * select the cpu on the other node.
1483 */
1484 if (nid != -1) {
1485 nodemask = cpumask_of_node(nid);
1486
1487 /* Look for allowed, online CPU in same node. */
1488 for_each_cpu(dest_cpu, nodemask) {
Tang Chenaa00d892013-02-22 16:33:33 -08001489 if (!cpu_active(dest_cpu))
1490 continue;
1491 if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
1492 return dest_cpu;
1493 }
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001494 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001495
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001496 for (;;) {
1497 /* Any allowed, online CPU? */
Srivatsa S. Bhate3831ed2012-03-30 19:40:28 +05301498 for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) {
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001499 if (!cpu_active(dest_cpu))
1500 continue;
1501 goto out;
1502 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001503
Oleg Nesterove73e85f2015-10-10 20:53:15 +02001504 /* No more Mr. Nice Guy. */
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001505 switch (state) {
1506 case cpuset:
Oleg Nesterove73e85f2015-10-10 20:53:15 +02001507 if (IS_ENABLED(CONFIG_CPUSETS)) {
1508 cpuset_cpus_allowed_fallback(p);
1509 state = possible;
1510 break;
1511 }
1512 /* fall-through */
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001513 case possible:
1514 do_set_cpus_allowed(p, cpu_possible_mask);
1515 state = fail;
1516 break;
1517
1518 case fail:
1519 BUG();
1520 break;
1521 }
1522 }
1523
1524out:
1525 if (state != cpuset) {
1526 /*
1527 * Don't tell them about moving exiting tasks or
1528 * kernel threads (both mm NULL), since they never
1529 * leave kernel.
1530 */
1531 if (p->mm && printk_ratelimit()) {
John Stultzaac74dc2014-06-04 16:11:40 -07001532 printk_deferred("process %d (%s) no longer affine to cpu%d\n",
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001533 task_pid_nr(p), p->comm, cpu);
1534 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001535 }
1536
1537 return dest_cpu;
1538}
1539
Peter Zijlstrae2912002009-12-16 18:04:36 +01001540/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001541 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01001542 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001543static inline
Peter Zijlstraac66f542013-10-07 11:29:16 +01001544int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001545{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001546 lockdep_assert_held(&p->pi_lock);
1547
Wanpeng Li6c1d9412014-11-05 09:14:37 +08001548 if (p->nr_cpus_allowed > 1)
1549 cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001550 else
1551 cpu = cpumask_any(tsk_cpus_allowed(p));
Peter Zijlstrae2912002009-12-16 18:04:36 +01001552
1553 /*
1554 * In order not to call set_task_cpu() on a blocking task we need
1555 * to rely on ttwu() to place the task on a valid ->cpus_allowed
1556 * cpu.
1557 *
1558 * Since this is common to all placement strategies, this lives here.
1559 *
1560 * [ this allows ->select_task() to simply return task_cpu(p) and
1561 * not worry about this generic constraint ]
1562 */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001563 if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01001564 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001565 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01001566
1567 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001568}
Mike Galbraith09a40af2010-04-15 07:29:59 +02001569
1570static void update_avg(u64 *avg, u64 sample)
1571{
1572 s64 diff = sample - *avg;
1573 *avg += diff >> 3;
1574}
Peter Zijlstra25834c72015-05-15 17:43:34 +02001575
1576#else
1577
1578static inline int __set_cpus_allowed_ptr(struct task_struct *p,
1579 const struct cpumask *new_mask, bool check)
1580{
1581 return set_cpus_allowed_ptr(p, new_mask);
1582}
1583
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001584#endif /* CONFIG_SMP */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001585
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001586static void
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001587ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001588{
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001589#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001590 struct rq *rq = this_rq();
Tejun Heo9ed38112009-12-03 15:08:03 +09001591
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001592#ifdef CONFIG_SMP
1593 int this_cpu = smp_processor_id();
Tejun Heo9ed38112009-12-03 15:08:03 +09001594
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001595 if (cpu == this_cpu) {
1596 schedstat_inc(rq, ttwu_local);
1597 schedstat_inc(p, se.statistics.nr_wakeups_local);
1598 } else {
1599 struct sched_domain *sd;
1600
1601 schedstat_inc(p, se.statistics.nr_wakeups_remote);
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001602 rcu_read_lock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001603 for_each_domain(this_cpu, sd) {
1604 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
1605 schedstat_inc(sd, ttwu_wake_remote);
1606 break;
1607 }
1608 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001609 rcu_read_unlock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001610 }
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001611
1612 if (wake_flags & WF_MIGRATED)
1613 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
1614
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001615#endif /* CONFIG_SMP */
1616
1617 schedstat_inc(rq, ttwu_count);
1618 schedstat_inc(p, se.statistics.nr_wakeups);
1619
1620 if (wake_flags & WF_SYNC)
1621 schedstat_inc(p, se.statistics.nr_wakeups_sync);
1622
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001623#endif /* CONFIG_SCHEDSTATS */
Tejun Heo9ed38112009-12-03 15:08:03 +09001624}
1625
Peter Zijlstra1de64442015-09-30 17:44:13 +02001626static inline void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001627{
Tejun Heo9ed38112009-12-03 15:08:03 +09001628 activate_task(rq, p, en_flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001629 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrac2f71152011-04-13 13:28:56 +02001630
1631 /* if a worker is waking up, notify workqueue */
1632 if (p->flags & PF_WQ_WORKER)
1633 wq_worker_waking_up(p, cpu_of(rq));
Tejun Heo9ed38112009-12-03 15:08:03 +09001634}
1635
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001636/*
1637 * Mark the task runnable and perform wakeup-preemption.
1638 */
Peter Zijlstra89363382011-04-05 17:23:42 +02001639static void
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001640ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001641{
Tejun Heo9ed38112009-12-03 15:08:03 +09001642 check_preempt_curr(rq, p, wake_flags);
Tejun Heo9ed38112009-12-03 15:08:03 +09001643 p->state = TASK_RUNNING;
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02001644 trace_sched_wakeup(p);
1645
Tejun Heo9ed38112009-12-03 15:08:03 +09001646#ifdef CONFIG_SMP
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001647 if (p->sched_class->task_woken) {
1648 /*
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001649 * Our task @p is fully woken up and running; so its safe to
1650 * drop the rq->lock, hereafter rq is only used for statistics.
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001651 */
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001652 lockdep_unpin_lock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09001653 p->sched_class->task_woken(rq, p);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001654 lockdep_pin_lock(&rq->lock);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001655 }
Tejun Heo9ed38112009-12-03 15:08:03 +09001656
Steven Rostedte69c6342010-12-06 17:10:31 -05001657 if (rq->idle_stamp) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001658 u64 delta = rq_clock(rq) - rq->idle_stamp;
Jason Low9bd721c2013-09-13 11:26:52 -07001659 u64 max = 2*rq->max_idle_balance_cost;
Tejun Heo9ed38112009-12-03 15:08:03 +09001660
Jason Lowabfafa52013-09-13 11:26:51 -07001661 update_avg(&rq->avg_idle, delta);
1662
1663 if (rq->avg_idle > max)
Tejun Heo9ed38112009-12-03 15:08:03 +09001664 rq->avg_idle = max;
Jason Lowabfafa52013-09-13 11:26:51 -07001665
Tejun Heo9ed38112009-12-03 15:08:03 +09001666 rq->idle_stamp = 0;
1667 }
1668#endif
1669}
1670
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001671static void
1672ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)
1673{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001674 lockdep_assert_held(&rq->lock);
1675
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001676#ifdef CONFIG_SMP
1677 if (p->sched_contributes_to_load)
1678 rq->nr_uninterruptible--;
1679#endif
1680
1681 ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);
1682 ttwu_do_wakeup(rq, p, wake_flags);
1683}
1684
1685/*
1686 * Called in case the task @p isn't fully descheduled from its runqueue,
1687 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
1688 * since all we need to do is flip p->state to TASK_RUNNING, since
1689 * the task is still ->on_rq.
1690 */
1691static int ttwu_remote(struct task_struct *p, int wake_flags)
1692{
1693 struct rq *rq;
1694 int ret = 0;
1695
1696 rq = __task_rq_lock(p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001697 if (task_on_rq_queued(p)) {
Frederic Weisbecker1ad4ec02013-04-12 01:51:00 +02001698 /* check_preempt_curr() may use rq clock */
1699 update_rq_clock(rq);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001700 ttwu_do_wakeup(rq, p, wake_flags);
1701 ret = 1;
1702 }
1703 __task_rq_unlock(rq);
1704
1705 return ret;
1706}
1707
Peter Zijlstra317f3942011-04-05 17:23:58 +02001708#ifdef CONFIG_SMP
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001709void sched_ttwu_pending(void)
Peter Zijlstra317f3942011-04-05 17:23:58 +02001710{
1711 struct rq *rq = this_rq();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001712 struct llist_node *llist = llist_del_all(&rq->wake_list);
1713 struct task_struct *p;
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001714 unsigned long flags;
Peter Zijlstra317f3942011-04-05 17:23:58 +02001715
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001716 if (!llist)
1717 return;
1718
1719 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001720 lockdep_pin_lock(&rq->lock);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001721
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001722 while (llist) {
1723 p = llist_entry(llist, struct task_struct, wake_entry);
1724 llist = llist_next(llist);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001725 ttwu_do_activate(rq, p, 0);
1726 }
1727
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001728 lockdep_unpin_lock(&rq->lock);
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001729 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001730}
1731
1732void scheduler_ipi(void)
1733{
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001734 /*
1735 * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
1736 * TIF_NEED_RESCHED remotely (for the first time) will also send
1737 * this IPI.
1738 */
Peter Zijlstra8cb75e02013-11-20 12:22:37 +01001739 preempt_fold_need_resched();
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001740
Frederic Weisbeckerfd2ac4f2014-03-18 21:12:53 +01001741 if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001742 return;
1743
1744 /*
1745 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
1746 * traditionally all their work was done from the interrupt return
1747 * path. Now that we actually do some work, we need to make sure
1748 * we do call them.
1749 *
1750 * Some archs already do call them, luckily irq_enter/exit nest
1751 * properly.
1752 *
1753 * Arguably we should visit all archs and update all handlers,
1754 * however a fair share of IPIs are still resched only so this would
1755 * somewhat pessimize the simple resched case.
1756 */
1757 irq_enter();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001758 sched_ttwu_pending();
Suresh Siddhaca380622011-10-03 15:09:00 -07001759
1760 /*
1761 * Check if someone kicked us for doing the nohz idle load balance.
1762 */
Vincent Guittot873b4c62013-06-05 10:13:11 +02001763 if (unlikely(got_nohz_idle_kick())) {
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001764 this_rq()->idle_balance = 1;
Suresh Siddhaca380622011-10-03 15:09:00 -07001765 raise_softirq_irqoff(SCHED_SOFTIRQ);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001766 }
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001767 irq_exit();
Peter Zijlstra317f3942011-04-05 17:23:58 +02001768}
1769
1770static void ttwu_queue_remote(struct task_struct *p, int cpu)
1771{
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001772 struct rq *rq = cpu_rq(cpu);
1773
1774 if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) {
1775 if (!set_nr_if_polling(rq->idle))
1776 smp_send_reschedule(cpu);
1777 else
1778 trace_sched_wake_idle_without_ipi(cpu);
1779 }
Peter Zijlstra317f3942011-04-05 17:23:58 +02001780}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001781
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001782void wake_up_if_idle(int cpu)
1783{
1784 struct rq *rq = cpu_rq(cpu);
1785 unsigned long flags;
1786
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001787 rcu_read_lock();
1788
1789 if (!is_idle_task(rcu_dereference(rq->curr)))
1790 goto out;
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001791
1792 if (set_nr_if_polling(rq->idle)) {
1793 trace_sched_wake_idle_without_ipi(cpu);
1794 } else {
1795 raw_spin_lock_irqsave(&rq->lock, flags);
1796 if (is_idle_task(rq->curr))
1797 smp_send_reschedule(cpu);
1798 /* Else cpu is not in idle, do nothing here */
1799 raw_spin_unlock_irqrestore(&rq->lock, flags);
1800 }
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001801
1802out:
1803 rcu_read_unlock();
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001804}
1805
Peter Zijlstra39be3502012-01-26 12:44:34 +01001806bool cpus_share_cache(int this_cpu, int that_cpu)
Peter Zijlstra518cd622011-12-07 15:07:31 +01001807{
1808 return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
1809}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001810#endif /* CONFIG_SMP */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001811
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001812static void ttwu_queue(struct task_struct *p, int cpu)
1813{
1814 struct rq *rq = cpu_rq(cpu);
1815
Daniel Hellstrom17d9f312011-05-20 04:01:10 +00001816#if defined(CONFIG_SMP)
Peter Zijlstra39be3502012-01-26 12:44:34 +01001817 if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
Peter Zijlstraf01114c2011-05-31 12:26:55 +02001818 sched_clock_cpu(cpu); /* sync clocks x-cpu */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001819 ttwu_queue_remote(p, cpu);
1820 return;
1821 }
1822#endif
1823
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001824 raw_spin_lock(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001825 lockdep_pin_lock(&rq->lock);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001826 ttwu_do_activate(rq, p, 0);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001827 lockdep_unpin_lock(&rq->lock);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001828 raw_spin_unlock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09001829}
1830
Peter Zijlstra8643cda2015-11-17 19:01:11 +01001831/*
1832 * Notes on Program-Order guarantees on SMP systems.
1833 *
1834 * MIGRATION
1835 *
1836 * The basic program-order guarantee on SMP systems is that when a task [t]
1837 * migrates, all its activity on its old cpu [c0] happens-before any subsequent
1838 * execution on its new cpu [c1].
1839 *
1840 * For migration (of runnable tasks) this is provided by the following means:
1841 *
1842 * A) UNLOCK of the rq(c0)->lock scheduling out task t
1843 * B) migration for t is required to synchronize *both* rq(c0)->lock and
1844 * rq(c1)->lock (if not at the same time, then in that order).
1845 * C) LOCK of the rq(c1)->lock scheduling in task
1846 *
1847 * Transitivity guarantees that B happens after A and C after B.
1848 * Note: we only require RCpc transitivity.
1849 * Note: the cpu doing B need not be c0 or c1
1850 *
1851 * Example:
1852 *
1853 * CPU0 CPU1 CPU2
1854 *
1855 * LOCK rq(0)->lock
1856 * sched-out X
1857 * sched-in Y
1858 * UNLOCK rq(0)->lock
1859 *
1860 * LOCK rq(0)->lock // orders against CPU0
1861 * dequeue X
1862 * UNLOCK rq(0)->lock
1863 *
1864 * LOCK rq(1)->lock
1865 * enqueue X
1866 * UNLOCK rq(1)->lock
1867 *
1868 * LOCK rq(1)->lock // orders against CPU2
1869 * sched-out Z
1870 * sched-in X
1871 * UNLOCK rq(1)->lock
1872 *
1873 *
1874 * BLOCKING -- aka. SLEEP + WAKEUP
1875 *
1876 * For blocking we (obviously) need to provide the same guarantee as for
1877 * migration. However the means are completely different as there is no lock
1878 * chain to provide order. Instead we do:
1879 *
1880 * 1) smp_store_release(X->on_cpu, 0)
1881 * 2) smp_cond_acquire(!X->on_cpu)
1882 *
1883 * Example:
1884 *
1885 * CPU0 (schedule) CPU1 (try_to_wake_up) CPU2 (schedule)
1886 *
1887 * LOCK rq(0)->lock LOCK X->pi_lock
1888 * dequeue X
1889 * sched-out X
1890 * smp_store_release(X->on_cpu, 0);
1891 *
1892 * smp_cond_acquire(!X->on_cpu);
1893 * X->state = WAKING
1894 * set_task_cpu(X,2)
1895 *
1896 * LOCK rq(2)->lock
1897 * enqueue X
1898 * X->state = RUNNING
1899 * UNLOCK rq(2)->lock
1900 *
1901 * LOCK rq(2)->lock // orders against CPU1
1902 * sched-out Z
1903 * sched-in X
1904 * UNLOCK rq(2)->lock
1905 *
1906 * UNLOCK X->pi_lock
1907 * UNLOCK rq(0)->lock
1908 *
1909 *
1910 * However; for wakeups there is a second guarantee we must provide, namely we
1911 * must observe the state that lead to our wakeup. That is, not only must our
1912 * task observe its own prior state, it must also observe the stores prior to
1913 * its wakeup.
1914 *
1915 * This means that any means of doing remote wakeups must order the CPU doing
1916 * the wakeup against the CPU the task is going to end up running on. This,
1917 * however, is already required for the regular Program-Order guarantee above,
1918 * since the waking CPU is the one issueing the ACQUIRE (smp_cond_acquire).
1919 *
1920 */
1921
Tejun Heo9ed38112009-12-03 15:08:03 +09001922/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09001924 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09001926 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 *
1928 * Put it on the run-queue if it's not already there. The "current"
1929 * thread is always on the run-queue (except when the actual
1930 * re-schedule is in progress), and as such you're allowed to do
1931 * the simpler "current->state = TASK_RUNNING" to mark yourself
1932 * runnable without the overhead of this.
1933 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02001934 * Return: %true if @p was woken up, %false if it was already running.
Tejun Heo9ed38112009-12-03 15:08:03 +09001935 * or @state didn't match @p's state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001937static int
1938try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 unsigned long flags;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001941 int cpu, success = 0;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001942
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02001943 /*
1944 * If we are going to wake up a thread waiting for CONDITION we
1945 * need to ensure that CONDITION=1 done by the caller can not be
1946 * reordered with p->state check below. This pairs with mb() in
1947 * set_current_state() the waiting thread does.
1948 */
1949 smp_mb__before_spinlock();
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001950 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001951 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 goto out;
1953
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02001954 trace_sched_waking(p);
1955
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001956 success = 1; /* we're going to change ->state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 cpu = task_cpu(p);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001958
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001959 if (p->on_rq && ttwu_remote(p, wake_flags))
1960 goto stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962#ifdef CONFIG_SMP
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001963 /*
Peter Zijlstraecf7d012015-10-07 14:14:13 +02001964 * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
1965 * possible to, falsely, observe p->on_cpu == 0.
1966 *
1967 * One must be running (->on_cpu == 1) in order to remove oneself
1968 * from the runqueue.
1969 *
1970 * [S] ->on_cpu = 1; [L] ->on_rq
1971 * UNLOCK rq->lock
1972 * RMB
1973 * LOCK rq->lock
1974 * [S] ->on_rq = 0; [L] ->on_cpu
1975 *
1976 * Pairs with the full barrier implied in the UNLOCK+LOCK on rq->lock
1977 * from the consecutive calls to schedule(); the first switching to our
1978 * task, the second putting it to sleep.
1979 */
1980 smp_rmb();
1981
1982 /*
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001983 * If the owning (remote) cpu is still in the middle of schedule() with
1984 * this task as prev, wait until its done referencing the task.
Peter Zijlstrab75a2252015-10-06 14:36:17 +02001985 *
1986 * Pairs with the smp_store_release() in finish_lock_switch().
1987 *
1988 * This ensures that tasks getting woken will be fully ordered against
1989 * their previous state and preserve Program Order.
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001990 */
Peter Zijlstrab3e0b1b2015-10-16 14:39:38 +02001991 smp_cond_acquire(!p->on_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Peter Zijlstraa8e4f2e2011-04-05 17:23:49 +02001993 p->sched_contributes_to_load = !!task_contributes_to_load(p);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001994 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01001995
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001996 if (p->sched_class->task_waking)
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02001997 p->sched_class->task_waking(p);
Peter Zijlstraab19cb22009-11-27 15:44:43 +01001998
Peter Zijlstraac66f542013-10-07 11:29:16 +01001999 cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002000 if (task_cpu(p) != cpu) {
2001 wake_flags |= WF_MIGRATED;
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002002 set_task_cpu(p, cpu);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002006 ttwu_queue(p, cpu);
2007stat:
Mel Gormancb251762016-02-05 09:08:36 +00002008 if (schedstat_enabled())
2009 ttwu_stat(p, cpu, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010out:
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002011 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 return success;
2014}
2015
David Howells50fa6102009-04-28 15:01:38 +01002016/**
Tejun Heo21aa9af2010-06-08 21:40:37 +02002017 * try_to_wake_up_local - try to wake up a local task with rq lock held
2018 * @p: the thread to be awakened
2019 *
Peter Zijlstra2acca552011-04-05 17:23:50 +02002020 * Put @p on the run-queue if it's not already there. The caller must
Tejun Heo21aa9af2010-06-08 21:40:37 +02002021 * ensure that this_rq() is locked, @p is bound to this_rq() and not
Peter Zijlstra2acca552011-04-05 17:23:50 +02002022 * the current task.
Tejun Heo21aa9af2010-06-08 21:40:37 +02002023 */
2024static void try_to_wake_up_local(struct task_struct *p)
2025{
2026 struct rq *rq = task_rq(p);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002027
Tejun Heo383efcd2013-03-18 12:22:34 -07002028 if (WARN_ON_ONCE(rq != this_rq()) ||
2029 WARN_ON_ONCE(p == current))
2030 return;
2031
Tejun Heo21aa9af2010-06-08 21:40:37 +02002032 lockdep_assert_held(&rq->lock);
2033
Peter Zijlstra2acca552011-04-05 17:23:50 +02002034 if (!raw_spin_trylock(&p->pi_lock)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002035 /*
2036 * This is OK, because current is on_cpu, which avoids it being
2037 * picked for load-balance and preemption/IRQs are still
2038 * disabled avoiding further scheduler activity on it and we've
2039 * not yet picked a replacement task.
2040 */
2041 lockdep_unpin_lock(&rq->lock);
Peter Zijlstra2acca552011-04-05 17:23:50 +02002042 raw_spin_unlock(&rq->lock);
2043 raw_spin_lock(&p->pi_lock);
2044 raw_spin_lock(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002045 lockdep_pin_lock(&rq->lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002046 }
Peter Zijlstra2acca552011-04-05 17:23:50 +02002047
Tejun Heo21aa9af2010-06-08 21:40:37 +02002048 if (!(p->state & TASK_NORMAL))
Peter Zijlstra2acca552011-04-05 17:23:50 +02002049 goto out;
Tejun Heo21aa9af2010-06-08 21:40:37 +02002050
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002051 trace_sched_waking(p);
2052
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002053 if (!task_on_rq_queued(p))
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002054 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
2055
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02002056 ttwu_do_wakeup(rq, p, 0);
Mel Gormancb251762016-02-05 09:08:36 +00002057 if (schedstat_enabled())
2058 ttwu_stat(p, smp_processor_id(), 0);
Peter Zijlstra2acca552011-04-05 17:23:50 +02002059out:
2060 raw_spin_unlock(&p->pi_lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002061}
2062
2063/**
David Howells50fa6102009-04-28 15:01:38 +01002064 * wake_up_process - Wake up a specific process
2065 * @p: The process to be woken up.
2066 *
2067 * Attempt to wake up the nominated process and move it to the set of runnable
Yacine Belkadie69f6182013-07-12 20:45:47 +02002068 * processes.
2069 *
2070 * Return: 1 if the process was woken up, 0 if it was already running.
David Howells50fa6102009-04-28 15:01:38 +01002071 *
2072 * It may be assumed that this function implies a write memory barrier before
2073 * changing the task state if and only if any tasks are woken up.
2074 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002075int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Oleg Nesterov9067ac82013-01-21 20:48:17 +01002077 return try_to_wake_up(p, TASK_NORMAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079EXPORT_SYMBOL(wake_up_process);
2080
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002081int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 return try_to_wake_up(p, state, 0);
2084}
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086/*
Juri Lellia5e7be32014-09-19 10:22:39 +01002087 * This function clears the sched_dl_entity static params.
2088 */
2089void __dl_clear_params(struct task_struct *p)
2090{
2091 struct sched_dl_entity *dl_se = &p->dl;
2092
2093 dl_se->dl_runtime = 0;
2094 dl_se->dl_deadline = 0;
2095 dl_se->dl_period = 0;
2096 dl_se->flags = 0;
2097 dl_se->dl_bw = 0;
Peter Zijlstra40767b02015-01-28 15:08:03 +01002098
2099 dl_se->dl_throttled = 0;
Peter Zijlstra40767b02015-01-28 15:08:03 +01002100 dl_se->dl_yielded = 0;
Juri Lellia5e7be32014-09-19 10:22:39 +01002101}
2102
2103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 * Perform scheduler related setup for a newly forked process p.
2105 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002106 *
2107 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 */
Rik van Riel5e1576e2013-10-07 11:29:26 +01002109static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002111 p->on_rq = 0;
2112
2113 p->se.on_rq = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002114 p->se.exec_start = 0;
2115 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002116 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002117 p->se.nr_migrations = 0;
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002118 p->se.vruntime = 0;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002119 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002120
Byungchul Parkad936d82015-10-24 01:16:19 +09002121#ifdef CONFIG_FAIR_GROUP_SCHED
2122 p->se.cfs_rq = NULL;
2123#endif
2124
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002125#ifdef CONFIG_SCHEDSTATS
Mel Gormancb251762016-02-05 09:08:36 +00002126 /* Even if schedstat is disabled, there should not be garbage */
Lucas De Marchi41acab82010-03-10 23:37:45 -03002127 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002128#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002129
Dario Faggioliaab03e02013-11-28 11:14:43 +01002130 RB_CLEAR_NODE(&p->dl.rb_node);
Peter Zijlstra40767b02015-01-28 15:08:03 +01002131 init_dl_task_timer(&p->dl);
Juri Lellia5e7be32014-09-19 10:22:39 +01002132 __dl_clear_params(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002133
Peter Zijlstrafa717062008-01-25 21:08:27 +01002134 INIT_LIST_HEAD(&p->rt.run_list);
Peter Zijlstraff77e462016-01-18 15:27:07 +01002135 p->rt.timeout = 0;
2136 p->rt.time_slice = sched_rr_timeslice;
2137 p->rt.on_rq = 0;
2138 p->rt.on_list = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002139
Avi Kivitye107be32007-07-26 13:40:43 +02002140#ifdef CONFIG_PREEMPT_NOTIFIERS
2141 INIT_HLIST_HEAD(&p->preempt_notifiers);
2142#endif
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002143
2144#ifdef CONFIG_NUMA_BALANCING
2145 if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002146 p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002147 p->mm->numa_scan_seq = 0;
2148 }
2149
Rik van Riel5e1576e2013-10-07 11:29:26 +01002150 if (clone_flags & CLONE_VM)
2151 p->numa_preferred_nid = current->numa_preferred_nid;
2152 else
2153 p->numa_preferred_nid = -1;
2154
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002155 p->node_stamp = 0ULL;
2156 p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
Peter Zijlstra4b96a292012-10-25 14:16:47 +02002157 p->numa_scan_period = sysctl_numa_balancing_scan_delay;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002158 p->numa_work.next = &p->numa_work;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002159 p->numa_faults = NULL;
Rik van Riel7e2703e2014-01-27 17:03:45 -05002160 p->last_task_numa_placement = 0;
2161 p->last_sum_exec_runtime = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002162
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002163 p->numa_group = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002164#endif /* CONFIG_NUMA_BALANCING */
Ingo Molnardd41f592007-07-09 18:51:59 +02002165}
2166
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302167DEFINE_STATIC_KEY_FALSE(sched_numa_balancing);
2168
Mel Gorman1a687c22012-11-22 11:16:36 +00002169#ifdef CONFIG_NUMA_BALANCING
Mel Gorman3105b862012-11-23 11:23:49 +00002170
2171void set_numabalancing_state(bool enabled)
2172{
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302173 if (enabled)
2174 static_branch_enable(&sched_numa_balancing);
2175 else
2176 static_branch_disable(&sched_numa_balancing);
Mel Gorman3105b862012-11-23 11:23:49 +00002177}
Andi Kleen54a43d52014-01-23 15:53:13 -08002178
2179#ifdef CONFIG_PROC_SYSCTL
2180int sysctl_numa_balancing(struct ctl_table *table, int write,
2181 void __user *buffer, size_t *lenp, loff_t *ppos)
2182{
2183 struct ctl_table t;
2184 int err;
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302185 int state = static_branch_likely(&sched_numa_balancing);
Andi Kleen54a43d52014-01-23 15:53:13 -08002186
2187 if (write && !capable(CAP_SYS_ADMIN))
2188 return -EPERM;
2189
2190 t = *table;
2191 t.data = &state;
2192 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2193 if (err < 0)
2194 return err;
2195 if (write)
2196 set_numabalancing_state(state);
2197 return err;
2198}
2199#endif
2200#endif
Mel Gorman1a687c22012-11-22 11:16:36 +00002201
Mel Gormancb251762016-02-05 09:08:36 +00002202DEFINE_STATIC_KEY_FALSE(sched_schedstats);
2203
2204#ifdef CONFIG_SCHEDSTATS
2205static void set_schedstats(bool enabled)
2206{
2207 if (enabled)
2208 static_branch_enable(&sched_schedstats);
2209 else
2210 static_branch_disable(&sched_schedstats);
2211}
2212
2213void force_schedstat_enabled(void)
2214{
2215 if (!schedstat_enabled()) {
2216 pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n");
2217 static_branch_enable(&sched_schedstats);
2218 }
2219}
2220
2221static int __init setup_schedstats(char *str)
2222{
2223 int ret = 0;
2224 if (!str)
2225 goto out;
2226
2227 if (!strcmp(str, "enable")) {
2228 set_schedstats(true);
2229 ret = 1;
2230 } else if (!strcmp(str, "disable")) {
2231 set_schedstats(false);
2232 ret = 1;
2233 }
2234out:
2235 if (!ret)
2236 pr_warn("Unable to parse schedstats=\n");
2237
2238 return ret;
2239}
2240__setup("schedstats=", setup_schedstats);
2241
2242#ifdef CONFIG_PROC_SYSCTL
2243int sysctl_schedstats(struct ctl_table *table, int write,
2244 void __user *buffer, size_t *lenp, loff_t *ppos)
2245{
2246 struct ctl_table t;
2247 int err;
2248 int state = static_branch_likely(&sched_schedstats);
2249
2250 if (write && !capable(CAP_SYS_ADMIN))
2251 return -EPERM;
2252
2253 t = *table;
2254 t.data = &state;
2255 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2256 if (err < 0)
2257 return err;
2258 if (write)
2259 set_schedstats(state);
2260 return err;
2261}
2262#endif
2263#endif
2264
Ingo Molnardd41f592007-07-09 18:51:59 +02002265/*
2266 * fork()/clone()-time setup:
2267 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002268int sched_fork(unsigned long clone_flags, struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02002269{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002270 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002271 int cpu = get_cpu();
2272
Rik van Riel5e1576e2013-10-07 11:29:26 +01002273 __sched_fork(clone_flags, p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002274 /*
Peter Zijlstra0017d732010-03-24 18:34:10 +01002275 * We mark the process as running here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002276 * nobody will actually run it, and a signal or other external
2277 * event cannot wake it up and insert it on the runqueue either.
2278 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002279 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002280
Ingo Molnarb29739f2006-06-27 02:54:51 -07002281 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02002282 * Make sure we do not leak PI boosting priority to the child.
2283 */
2284 p->prio = current->normal_prio;
2285
2286 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002287 * Revert to default priority/policy on fork if requested.
2288 */
2289 if (unlikely(p->sched_reset_on_fork)) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01002290 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002291 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002292 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02002293 p->rt_priority = 0;
2294 } else if (PRIO_TO_NICE(p->static_prio) < 0)
2295 p->static_prio = NICE_TO_PRIO(0);
2296
2297 p->prio = p->normal_prio = __normal_prio(p);
2298 set_load_weight(p);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002299
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002300 /*
2301 * We don't need the reset flag anymore after the fork. It has
2302 * fulfilled its duty:
2303 */
2304 p->sched_reset_on_fork = 0;
2305 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02002306
Dario Faggioliaab03e02013-11-28 11:14:43 +01002307 if (dl_prio(p->prio)) {
2308 put_cpu();
2309 return -EAGAIN;
2310 } else if (rt_prio(p->prio)) {
2311 p->sched_class = &rt_sched_class;
2312 } else {
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002313 p->sched_class = &fair_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01002314 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07002315
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002316 if (p->sched_class->task_fork)
2317 p->sched_class->task_fork(p);
2318
Peter Zijlstra86951592010-06-22 11:44:53 +02002319 /*
2320 * The child is not yet in the pid-hash so no cgroup attach races,
2321 * and the cgroup is pinned to this child due to cgroup_fork()
2322 * is ran before sched_fork().
2323 *
2324 * Silence PROVE_RCU.
2325 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002326 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002327 set_task_cpu(p, cpu);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002328 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002329
Naveen N. Raof6db8342015-06-25 23:53:37 +05302330#ifdef CONFIG_SCHED_INFO
Ingo Molnardd41f592007-07-09 18:51:59 +02002331 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002332 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02002334#if defined(CONFIG_SMP)
2335 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07002336#endif
Peter Zijlstra01028742013-08-14 14:55:46 +02002337 init_task_preempt_count(p);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002338#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05002339 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002340 RB_CLEAR_NODE(&p->pushable_dl_tasks);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002341#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002342
Nick Piggin476d1392005-06-25 14:57:29 -07002343 put_cpu();
Dario Faggioliaab03e02013-11-28 11:14:43 +01002344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346
Dario Faggioli332ac172013-11-07 14:43:45 +01002347unsigned long to_ratio(u64 period, u64 runtime)
2348{
2349 if (runtime == RUNTIME_INF)
2350 return 1ULL << 20;
2351
2352 /*
2353 * Doing this here saves a lot of checks in all
2354 * the calling paths, and returning zero seems
2355 * safe for them anyway.
2356 */
2357 if (period == 0)
2358 return 0;
2359
2360 return div64_u64(runtime << 20, period);
2361}
2362
2363#ifdef CONFIG_SMP
2364inline struct dl_bw *dl_bw_of(int i)
2365{
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -07002366 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2367 "sched RCU must be held");
Dario Faggioli332ac172013-11-07 14:43:45 +01002368 return &cpu_rq(i)->rd->dl_bw;
2369}
2370
Peter Zijlstrade212f12013-12-19 11:54:45 +01002371static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002372{
Peter Zijlstrade212f12013-12-19 11:54:45 +01002373 struct root_domain *rd = cpu_rq(i)->rd;
2374 int cpus = 0;
2375
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -07002376 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2377 "sched RCU must be held");
Peter Zijlstrade212f12013-12-19 11:54:45 +01002378 for_each_cpu_and(i, rd->span, cpu_active_mask)
2379 cpus++;
2380
2381 return cpus;
Dario Faggioli332ac172013-11-07 14:43:45 +01002382}
2383#else
2384inline struct dl_bw *dl_bw_of(int i)
2385{
2386 return &cpu_rq(i)->dl.dl_bw;
2387}
2388
Peter Zijlstrade212f12013-12-19 11:54:45 +01002389static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002390{
2391 return 1;
2392}
2393#endif
2394
Dario Faggioli332ac172013-11-07 14:43:45 +01002395/*
2396 * We must be sure that accepting a new task (or allowing changing the
2397 * parameters of an existing one) is consistent with the bandwidth
2398 * constraints. If yes, this function also accordingly updates the currently
2399 * allocated bandwidth to reflect the new situation.
2400 *
2401 * This function is called while holding p's rq->lock.
Peter Zijlstra40767b02015-01-28 15:08:03 +01002402 *
2403 * XXX we should delay bw change until the task's 0-lag point, see
2404 * __setparam_dl().
Dario Faggioli332ac172013-11-07 14:43:45 +01002405 */
2406static int dl_overflow(struct task_struct *p, int policy,
2407 const struct sched_attr *attr)
2408{
2409
2410 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
Steven Rostedt4df16382014-02-19 13:53:35 -05002411 u64 period = attr->sched_period ?: attr->sched_deadline;
Dario Faggioli332ac172013-11-07 14:43:45 +01002412 u64 runtime = attr->sched_runtime;
2413 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
Peter Zijlstrade212f12013-12-19 11:54:45 +01002414 int cpus, err = -1;
Dario Faggioli332ac172013-11-07 14:43:45 +01002415
2416 if (new_bw == p->dl.dl_bw)
2417 return 0;
2418
2419 /*
2420 * Either if a task, enters, leave, or stays -deadline but changes
2421 * its parameters, we may need to update accordingly the total
2422 * allocated bandwidth of the container.
2423 */
2424 raw_spin_lock(&dl_b->lock);
Peter Zijlstrade212f12013-12-19 11:54:45 +01002425 cpus = dl_bw_cpus(task_cpu(p));
Dario Faggioli332ac172013-11-07 14:43:45 +01002426 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2427 !__dl_overflow(dl_b, cpus, 0, new_bw)) {
2428 __dl_add(dl_b, new_bw);
2429 err = 0;
2430 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2431 !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
2432 __dl_clear(dl_b, p->dl.dl_bw);
2433 __dl_add(dl_b, new_bw);
2434 err = 0;
2435 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2436 __dl_clear(dl_b, p->dl.dl_bw);
2437 err = 0;
2438 }
2439 raw_spin_unlock(&dl_b->lock);
2440
2441 return err;
2442}
2443
2444extern void init_dl_bw(struct dl_bw *dl_b);
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446/*
2447 * wake_up_new_task - wake up a newly created task for the first time.
2448 *
2449 * This function will do some initial scheduler statistics housekeeping
2450 * that must be done for every newly created context, then puts the task
2451 * on the runqueue and wakes it.
2452 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002453void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
2455 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002456 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002457
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002458 raw_spin_lock_irqsave(&p->pi_lock, flags);
Morten Rasmussen98d8fd82015-08-14 17:23:14 +01002459 /* Initialize new task's runnable average */
2460 init_entity_runnable_average(&p->se);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002461#ifdef CONFIG_SMP
2462 /*
2463 * Fork balancing, do it here and not earlier because:
2464 * - cpus_allowed can change in the fork path
2465 * - any previously selected cpu might disappear through hotplug
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002466 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01002467 set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002470 rq = __task_rq_lock(p);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002471 activate_task(rq, p, 0);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002472 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002473 trace_sched_wakeup_new(p);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002474 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002475#ifdef CONFIG_SMP
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02002476 if (p->sched_class->task_woken) {
2477 /*
2478 * Nothing relies on rq->lock after this, so its fine to
2479 * drop it.
2480 */
2481 lockdep_unpin_lock(&rq->lock);
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002482 p->sched_class->task_woken(rq, p);
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02002483 lockdep_pin_lock(&rq->lock);
2484 }
Steven Rostedt9a897c52008-01-25 21:08:22 +01002485#endif
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002486 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Avi Kivitye107be32007-07-26 13:40:43 +02002489#ifdef CONFIG_PREEMPT_NOTIFIERS
2490
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002491static struct static_key preempt_notifier_key = STATIC_KEY_INIT_FALSE;
2492
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02002493void preempt_notifier_inc(void)
2494{
2495 static_key_slow_inc(&preempt_notifier_key);
2496}
2497EXPORT_SYMBOL_GPL(preempt_notifier_inc);
2498
2499void preempt_notifier_dec(void)
2500{
2501 static_key_slow_dec(&preempt_notifier_key);
2502}
2503EXPORT_SYMBOL_GPL(preempt_notifier_dec);
2504
Avi Kivitye107be32007-07-26 13:40:43 +02002505/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002506 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002507 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002508 */
2509void preempt_notifier_register(struct preempt_notifier *notifier)
2510{
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02002511 if (!static_key_false(&preempt_notifier_key))
2512 WARN(1, "registering preempt_notifier while notifiers disabled\n");
2513
Avi Kivitye107be32007-07-26 13:40:43 +02002514 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2515}
2516EXPORT_SYMBOL_GPL(preempt_notifier_register);
2517
2518/**
2519 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002520 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002521 *
Mathieu Desnoyersd84525a2015-05-17 12:53:10 -04002522 * This is *not* safe to call from within a preemption notifier.
Avi Kivitye107be32007-07-26 13:40:43 +02002523 */
2524void preempt_notifier_unregister(struct preempt_notifier *notifier)
2525{
2526 hlist_del(&notifier->link);
2527}
2528EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2529
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002530static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02002531{
2532 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002533
Sasha Levinb67bfe02013-02-27 17:06:00 -08002534 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002535 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2536}
2537
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002538static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2539{
2540 if (static_key_false(&preempt_notifier_key))
2541 __fire_sched_in_preempt_notifiers(curr);
2542}
2543
Avi Kivitye107be32007-07-26 13:40:43 +02002544static void
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002545__fire_sched_out_preempt_notifiers(struct task_struct *curr,
2546 struct task_struct *next)
Avi Kivitye107be32007-07-26 13:40:43 +02002547{
2548 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002549
Sasha Levinb67bfe02013-02-27 17:06:00 -08002550 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002551 notifier->ops->sched_out(notifier, next);
2552}
2553
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002554static __always_inline void
2555fire_sched_out_preempt_notifiers(struct task_struct *curr,
2556 struct task_struct *next)
2557{
2558 if (static_key_false(&preempt_notifier_key))
2559 __fire_sched_out_preempt_notifiers(curr, next);
2560}
2561
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002562#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002563
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002564static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02002565{
2566}
2567
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002568static inline void
Avi Kivitye107be32007-07-26 13:40:43 +02002569fire_sched_out_preempt_notifiers(struct task_struct *curr,
2570 struct task_struct *next)
2571{
2572}
2573
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002574#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002577 * prepare_task_switch - prepare to switch tasks
2578 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002579 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002580 * @next: the task we are going to switch to.
2581 *
2582 * This is called with the rq lock held and interrupts off. It must
2583 * be paired with a subsequent finish_task_switch after the context
2584 * switch.
2585 *
2586 * prepare_task_switch sets up locking and calls architecture specific
2587 * hooks.
2588 */
Avi Kivitye107be32007-07-26 13:40:43 +02002589static inline void
2590prepare_task_switch(struct rq *rq, struct task_struct *prev,
2591 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002592{
Michael S. Tsirkin43148952013-09-22 17:20:54 +03002593 sched_info_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002594 perf_event_task_sched_out(prev, next);
Avi Kivitye107be32007-07-26 13:40:43 +02002595 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002596 prepare_lock_switch(rq, next);
2597 prepare_arch_switch(next);
2598}
2599
2600/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 * finish_task_switch - clean up after a task-switch
2602 * @prev: the thread we just switched away from.
2603 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002604 * finish_task_switch must be called after the context switch, paired
2605 * with a prepare_task_switch call before the context switch.
2606 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2607 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 *
2609 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002610 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 * with the lock held can cause deadlocks; see schedule() for
2612 * details.)
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002613 *
2614 * The context switch have flipped the stack from under us and restored the
2615 * local variables which were saved when this task called schedule() in the
2616 * past. prev == current is still correct but we need to recalculate this_rq
2617 * because prev may have moved to another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002619static struct rq *finish_task_switch(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 __releases(rq->lock)
2621{
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002622 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002624 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Peter Zijlstra609ca062015-09-28 17:52:18 +02002626 /*
2627 * The previous task will have left us with a preempt_count of 2
2628 * because it left us after:
2629 *
2630 * schedule()
2631 * preempt_disable(); // 1
2632 * __schedule()
2633 * raw_spin_lock_irq(&rq->lock) // 2
2634 *
2635 * Also, see FORK_PREEMPT_COUNT.
2636 */
Peter Zijlstrae2bf1c4b2015-09-29 12:18:46 +02002637 if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
2638 "corrupted preempt_count: %s/%d/0x%x\n",
2639 current->comm, current->pid, preempt_count()))
2640 preempt_count_set(FORK_PREEMPT_COUNT);
Peter Zijlstra609ca062015-09-28 17:52:18 +02002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 rq->prev_mm = NULL;
2643
2644 /*
2645 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002646 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002647 * schedule one last time. The schedule call will never return, and
2648 * the scheduled task must drop that reference.
Peter Zijlstra95913d92015-09-29 14:45:09 +02002649 *
2650 * We must observe prev->state before clearing prev->on_cpu (in
2651 * finish_lock_switch), otherwise a concurrent wakeup can get prev
2652 * running on another CPU and we could rave with its RUNNING -> DEAD
2653 * transition, resulting in a double drop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002655 prev_state = prev->state;
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +02002656 vtime_task_switch(prev);
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002657 perf_event_task_sched_in(prev, current);
Nick Piggin4866cde2005-06-25 14:57:23 -07002658 finish_lock_switch(rq, prev);
Catalin Marinas01f23e12011-11-27 21:43:10 +00002659 finish_arch_post_lock_switch();
Steven Rostedte8fa1362008-01-25 21:08:05 +01002660
Avi Kivitye107be32007-07-26 13:40:43 +02002661 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (mm)
2663 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002664 if (unlikely(prev_state == TASK_DEAD)) {
Dario Faggiolie6c390f2013-11-07 14:43:35 +01002665 if (prev->sched_class->task_dead)
2666 prev->sched_class->task_dead(prev);
2667
bibo maoc6fd91f2006-03-26 01:38:20 -08002668 /*
2669 * Remove function-return probe instances associated with this
2670 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002671 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002672 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002674 }
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +02002675
Frederic Weisbeckerde734f82015-06-11 18:07:12 +02002676 tick_nohz_task_switch();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002677 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
2679
Gregory Haskins3f029d32009-07-29 11:08:47 -04002680#ifdef CONFIG_SMP
2681
Gregory Haskins3f029d32009-07-29 11:08:47 -04002682/* rq->lock is NOT held, but preemption is disabled */
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002683static void __balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04002684{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002685 struct callback_head *head, *next;
2686 void (*func)(struct rq *rq);
2687 unsigned long flags;
Gregory Haskins3f029d32009-07-29 11:08:47 -04002688
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002689 raw_spin_lock_irqsave(&rq->lock, flags);
2690 head = rq->balance_callback;
2691 rq->balance_callback = NULL;
2692 while (head) {
2693 func = (void (*)(struct rq *))head->func;
2694 next = head->next;
2695 head->next = NULL;
2696 head = next;
Gregory Haskins3f029d32009-07-29 11:08:47 -04002697
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002698 func(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002699 }
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002700 raw_spin_unlock_irqrestore(&rq->lock, flags);
2701}
2702
2703static inline void balance_callback(struct rq *rq)
2704{
2705 if (unlikely(rq->balance_callback))
2706 __balance_callback(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002707}
2708
2709#else
2710
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002711static inline void balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04002712{
2713}
2714
2715#endif
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717/**
2718 * schedule_tail - first thing a freshly forked thread must call.
2719 * @prev: the thread we just switched away from.
2720 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002721asmlinkage __visible void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 __releases(rq->lock)
2723{
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002724 struct rq *rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002725
Peter Zijlstra609ca062015-09-28 17:52:18 +02002726 /*
2727 * New tasks start with FORK_PREEMPT_COUNT, see there and
2728 * finish_task_switch() for details.
2729 *
2730 * finish_task_switch() will drop rq->lock() and lower preempt_count
2731 * and the preempt_enable() will end up enabling preemption (on
2732 * PREEMPT_COUNT kernels).
2733 */
2734
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002735 rq = finish_task_switch(prev);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002736 balance_callback(rq);
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002737 preempt_enable();
Steven Rostedtda19ab52009-07-29 00:21:22 -04002738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002740 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
2743/*
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002744 * context_switch - switch to the new MM and the new thread's register state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 */
Josh Poimboeuf04936942016-02-28 22:22:39 -06002746static __always_inline struct rq *
Ingo Molnar70b97a72006-07-03 00:25:42 -07002747context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002748 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Ingo Molnardd41f592007-07-09 18:51:59 +02002750 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Avi Kivitye107be32007-07-26 13:40:43 +02002752 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002753
Ingo Molnardd41f592007-07-09 18:51:59 +02002754 mm = next->mm;
2755 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002756 /*
2757 * For paravirt, this is coupled with an exit in switch_to to
2758 * combine the page table reload and the switch backend into
2759 * one hypercall.
2760 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002761 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002762
Heiko Carstens31915ab2010-09-16 14:42:25 +02002763 if (!mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 next->active_mm = oldmm;
2765 atomic_inc(&oldmm->mm_count);
2766 enter_lazy_tlb(oldmm, next);
2767 } else
2768 switch_mm(oldmm, mm, next);
2769
Heiko Carstens31915ab2010-09-16 14:42:25 +02002770 if (!prev->mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 rq->prev_mm = oldmm;
2773 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002774 /*
2775 * Since the runqueue lock will be released by the next
2776 * task (which is an invalid locking op but in the case
2777 * of the scheduler it's an obvious special-case), so we
2778 * do an early lockdep release here:
2779 */
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002780 lockdep_unpin_lock(&rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002781 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 /* Here we just switch the register state and the stack. */
2784 switch_to(prev, next, prev);
Ingo Molnardd41f592007-07-09 18:51:59 +02002785 barrier();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002786
2787 return finish_task_switch(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
2790/*
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002791 * nr_running and nr_context_switches:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 *
2793 * externally visible scheduler statistics: current number of runnable
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002794 * threads, total number of context switches performed since bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 */
2796unsigned long nr_running(void)
2797{
2798 unsigned long i, sum = 0;
2799
2800 for_each_online_cpu(i)
2801 sum += cpu_rq(i)->nr_running;
2802
2803 return sum;
2804}
2805
Tim Chen2ee507c2014-07-31 10:29:48 -07002806/*
2807 * Check if only the current task is running on the cpu.
Dominik Dingel00cc16332015-09-18 11:27:45 +02002808 *
2809 * Caution: this function does not check that the caller has disabled
2810 * preemption, thus the result might have a time-of-check-to-time-of-use
2811 * race. The caller is responsible to use it correctly, for example:
2812 *
2813 * - from a non-preemptable section (of course)
2814 *
2815 * - from a thread that is bound to a single CPU
2816 *
2817 * - in a loop with very short iterations (e.g. a polling loop)
Tim Chen2ee507c2014-07-31 10:29:48 -07002818 */
2819bool single_task_running(void)
2820{
Dominik Dingel00cc16332015-09-18 11:27:45 +02002821 return raw_rq()->nr_running == 1;
Tim Chen2ee507c2014-07-31 10:29:48 -07002822}
2823EXPORT_SYMBOL(single_task_running);
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825unsigned long long nr_context_switches(void)
2826{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002827 int i;
2828 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002830 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 sum += cpu_rq(i)->nr_switches;
2832
2833 return sum;
2834}
2835
2836unsigned long nr_iowait(void)
2837{
2838 unsigned long i, sum = 0;
2839
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002840 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2842
2843 return sum;
2844}
2845
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002846unsigned long nr_iowait_cpu(int cpu)
Arjan van de Ven69d25872009-09-21 17:04:08 -07002847{
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002848 struct rq *this = cpu_rq(cpu);
Arjan van de Ven69d25872009-09-21 17:04:08 -07002849 return atomic_read(&this->nr_iowait);
2850}
2851
Mel Gorman372ba8c2014-08-06 14:19:21 +01002852void get_iowait_load(unsigned long *nr_waiters, unsigned long *load)
2853{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02002854 struct rq *rq = this_rq();
2855 *nr_waiters = atomic_read(&rq->nr_iowait);
2856 *load = rq->load.weight;
Mel Gorman372ba8c2014-08-06 14:19:21 +01002857}
2858
Ingo Molnardd41f592007-07-09 18:51:59 +02002859#ifdef CONFIG_SMP
2860
Ingo Molnar48f24c42006-07-03 00:25:40 -07002861/*
Peter Zijlstra38022902009-12-16 18:04:37 +01002862 * sched_exec - execve() is a valuable balancing opportunity, because at
2863 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 */
Peter Zijlstra38022902009-12-16 18:04:37 +01002865void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
Peter Zijlstra38022902009-12-16 18:04:37 +01002867 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01002869 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01002870
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002871 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstraac66f542013-10-07 11:29:16 +01002872 dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002873 if (dest_cpu == smp_processor_id())
2874 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01002875
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002876 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02002877 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07002878
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002879 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
2880 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return;
2882 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01002883unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002884 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885}
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887#endif
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889DEFINE_PER_CPU(struct kernel_stat, kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002890DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892EXPORT_PER_CPU_SYMBOL(kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002893EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002896 * Return accounted runtime for the task.
2897 * In case the task is currently running, return the runtime plus current's
2898 * pending runtime that have not been accounted yet.
2899 */
2900unsigned long long task_sched_runtime(struct task_struct *p)
2901{
2902 unsigned long flags;
2903 struct rq *rq;
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002904 u64 ns;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002905
Peter Zijlstra911b2892013-11-11 18:21:56 +01002906#if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
2907 /*
2908 * 64-bit doesn't need locks to atomically read a 64bit value.
2909 * So we have a optimization chance when the task's delta_exec is 0.
2910 * Reading ->on_cpu is racy, but this is ok.
2911 *
2912 * If we race with it leaving cpu, we'll take a lock. So we're correct.
2913 * If we race with it entering cpu, unaccounted time is 0. This is
2914 * indistinguishable from the read occurring a few cycles earlier.
Mike Galbraith4036ac12014-06-24 07:49:40 +02002915 * If we see ->on_cpu without ->on_rq, the task is leaving, and has
2916 * been accounted, so we're correct here as well.
Peter Zijlstra911b2892013-11-11 18:21:56 +01002917 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002918 if (!p->on_cpu || !task_on_rq_queued(p))
Peter Zijlstra911b2892013-11-11 18:21:56 +01002919 return p->se.sum_exec_runtime;
2920#endif
2921
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002922 rq = task_rq_lock(p, &flags);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002923 /*
2924 * Must be ->curr _and_ ->on_rq. If dequeued, we would
2925 * project cycles that may never be accounted to this
2926 * thread, breaking clock_gettime().
2927 */
2928 if (task_current(rq, p) && task_on_rq_queued(p)) {
2929 update_rq_clock(rq);
2930 p->sched_class->update_curr(rq);
2931 }
2932 ns = p->se.sum_exec_runtime;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002933 task_rq_unlock(rq, p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002934
2935 return ns;
2936}
2937
Balbir Singh49048622008-09-05 18:12:23 +02002938/*
Christoph Lameter7835b982006-12-10 02:20:22 -08002939 * This function gets called by the timer code, with HZ frequency.
2940 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08002941 */
2942void scheduler_tick(void)
2943{
Christoph Lameter7835b982006-12-10 02:20:22 -08002944 int cpu = smp_processor_id();
2945 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002946 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002947
2948 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08002949
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002950 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002951 update_rq_clock(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01002952 curr->sched_class->task_tick(rq, curr, 0);
Alex Shi83dfd522013-06-20 10:18:49 +08002953 update_cpu_load_active(rq);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02002954 calc_global_load_tick(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002955 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02002956
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002957 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002958
Christoph Lametere418e1c2006-12-10 02:20:23 -08002959#ifdef CONFIG_SMP
Suresh Siddha6eb57e02011-10-03 15:09:01 -07002960 rq->idle_balance = idle_cpu(cpu);
Daniel Lezcano7caff662014-01-06 12:34:38 +01002961 trigger_load_balance(rq);
Christoph Lametere418e1c2006-12-10 02:20:23 -08002962#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002963 rq_last_tick_reset(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002966#ifdef CONFIG_NO_HZ_FULL
2967/**
2968 * scheduler_tick_max_deferment
2969 *
2970 * Keep at least one tick per second when a single
2971 * active task is running because the scheduler doesn't
2972 * yet completely support full dynticks environment.
2973 *
2974 * This makes sure that uptime, CFS vruntime, load
2975 * balancing, etc... continue to move forward, even
2976 * with a very low granularity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02002977 *
2978 * Return: Maximum deferment in nanoseconds.
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002979 */
2980u64 scheduler_tick_max_deferment(void)
2981{
2982 struct rq *rq = this_rq();
Jason Low316c1608d2015-04-28 13:00:20 -07002983 unsigned long next, now = READ_ONCE(jiffies);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002984
2985 next = rq->last_sched_tick + HZ;
2986
2987 if (time_before_eq(next, now))
2988 return 0;
2989
Kevin Hilman8fe8ff02014-01-15 14:51:38 +01002990 return jiffies_to_nsecs(next - now);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002991}
2992#endif
2993
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05002994#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
2995 defined(CONFIG_PREEMPT_TRACER))
2996
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002997void preempt_count_add(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002999#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 /*
3001 * Underflow?
3002 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003003 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3004 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003005#endif
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003006 __preempt_count_add(val);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003007#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 /*
3009 * Spinlock count overflowing soon?
3010 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003011 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3012 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003013#endif
Thomas Gleixner8f47b182014-02-07 20:58:39 +01003014 if (preempt_count() == val) {
Sebastian Andrzej Siewiorf904f582016-02-26 14:54:56 +01003015 unsigned long ip = get_lock_parent_ip();
Thomas Gleixner8f47b182014-02-07 20:58:39 +01003016#ifdef CONFIG_DEBUG_PREEMPT
3017 current->preempt_disable_ip = ip;
3018#endif
3019 trace_preempt_off(CALLER_ADDR0, ip);
3020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003022EXPORT_SYMBOL(preempt_count_add);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003023NOKPROBE_SYMBOL(preempt_count_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003025void preempt_count_sub(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003027#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 /*
3029 * Underflow?
3030 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01003031 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 /*
3034 * Is the spinlock portion underflowing?
3035 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003036 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3037 !(preempt_count() & PREEMPT_MASK)))
3038 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003039#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003040
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003041 if (preempt_count() == val)
Sebastian Andrzej Siewiorf904f582016-02-26 14:54:56 +01003042 trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003043 __preempt_count_sub(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003045EXPORT_SYMBOL(preempt_count_sub);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003046NOKPROBE_SYMBOL(preempt_count_sub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048#endif
3049
3050/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003051 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003053static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
Dave Jones664dfa62011-12-22 16:39:30 -05003055 if (oops_in_progress)
3056 return;
3057
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01003058 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3059 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02003060
Ingo Molnardd41f592007-07-09 18:51:59 +02003061 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07003062 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02003063 if (irqs_disabled())
3064 print_irqtrace_events(prev);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01003065#ifdef CONFIG_DEBUG_PREEMPT
3066 if (in_atomic_preempt_off()) {
3067 pr_err("Preemption disabled at:");
3068 print_ip_sym(current->preempt_disable_ip);
3069 pr_cont("\n");
3070 }
3071#endif
Stephen Boyd6135fc12012-03-28 17:10:47 -07003072 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +10303073 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Ingo Molnardd41f592007-07-09 18:51:59 +02003074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Ingo Molnardd41f592007-07-09 18:51:59 +02003076/*
3077 * Various schedule()-time debugging checks and statistics:
3078 */
3079static inline void schedule_debug(struct task_struct *prev)
3080{
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01003081#ifdef CONFIG_SCHED_STACK_END_CHECK
Geliang Tangce03e412015-10-05 21:26:05 +08003082 BUG_ON(task_stack_end_corrupted(prev));
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01003083#endif
Peter Zijlstrab99def82015-09-28 18:02:03 +02003084
Peter Zijlstra1dc0fff2015-09-28 17:57:39 +02003085 if (unlikely(in_atomic_preempt_off())) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003086 __schedule_bug(prev);
Peter Zijlstra1dc0fff2015-09-28 17:57:39 +02003087 preempt_count_set(PREEMPT_DISABLED);
3088 }
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003089 rcu_sleep_check();
Ingo Molnardd41f592007-07-09 18:51:59 +02003090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3092
Ingo Molnar2d723762007-10-15 17:00:12 +02003093 schedstat_inc(this_rq(), sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02003094}
3095
3096/*
3097 * Pick up the highest-prio task:
3098 */
3099static inline struct task_struct *
Peter Zijlstra606dba22012-02-11 06:05:00 +01003100pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02003101{
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003102 const struct sched_class *class = &fair_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003103 struct task_struct *p;
3104
3105 /*
3106 * Optimization: we know that if all tasks are in
3107 * the fair class we can call that function directly:
3108 */
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003109 if (likely(prev->sched_class == class &&
Peter Zijlstra38033c32014-01-23 20:32:21 +01003110 rq->nr_running == rq->cfs.h_nr_running)) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01003111 p = fair_sched_class.pick_next_task(rq, prev);
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02003112 if (unlikely(p == RETRY_TASK))
3113 goto again;
3114
3115 /* assumes fair_sched_class->next == idle_sched_class */
3116 if (unlikely(!p))
3117 p = idle_sched_class.pick_next_task(rq, prev);
3118
3119 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02003120 }
3121
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003122again:
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003123 for_each_class(class) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01003124 p = class->pick_next_task(rq, prev);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003125 if (p) {
3126 if (unlikely(p == RETRY_TASK))
3127 goto again;
Ingo Molnardd41f592007-07-09 18:51:59 +02003128 return p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003129 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003130 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003131
3132 BUG(); /* the idle class will always have a runnable task */
Ingo Molnardd41f592007-07-09 18:51:59 +02003133}
3134
3135/*
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003136 * __schedule() is the main scheduler function.
Pekka Enbergedde96e2012-08-04 11:49:47 +03003137 *
3138 * The main means of driving the scheduler and thus entering this function are:
3139 *
3140 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
3141 *
3142 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
3143 * paths. For example, see arch/x86/entry_64.S.
3144 *
3145 * To drive preemption between tasks, the scheduler sets the flag in timer
3146 * interrupt handler scheduler_tick().
3147 *
3148 * 3. Wakeups don't really cause entry into schedule(). They add a
3149 * task to the run-queue and that's it.
3150 *
3151 * Now, if the new task added to the run-queue preempts the current
3152 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
3153 * called on the nearest possible occasion:
3154 *
3155 * - If the kernel is preemptible (CONFIG_PREEMPT=y):
3156 *
3157 * - in syscall or exception context, at the next outmost
3158 * preempt_enable(). (this might be as soon as the wake_up()'s
3159 * spin_unlock()!)
3160 *
3161 * - in IRQ context, return from interrupt-handler to
3162 * preemptible context
3163 *
3164 * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
3165 * then at the next:
3166 *
3167 * - cond_resched() call
3168 * - explicit schedule() call
3169 * - return from syscall or exception to user-space
3170 * - return from interrupt-handler to user-space
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003171 *
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003172 * WARNING: must be called with preemption disabled!
Ingo Molnardd41f592007-07-09 18:51:59 +02003173 */
Peter Zijlstra499d7952015-09-28 18:52:36 +02003174static void __sched notrace __schedule(bool preempt)
Ingo Molnardd41f592007-07-09 18:51:59 +02003175{
3176 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003177 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02003178 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02003179 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02003180
Ingo Molnardd41f592007-07-09 18:51:59 +02003181 cpu = smp_processor_id();
3182 rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003183 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02003184
Peter Zijlstrab99def82015-09-28 18:02:03 +02003185 /*
3186 * do_exit() calls schedule() with preemption disabled as an exception;
3187 * however we must fix that up, otherwise the next task will see an
3188 * inconsistent (higher) preempt count.
3189 *
3190 * It also avoids the below schedule_debug() test from complaining
3191 * about this.
3192 */
3193 if (unlikely(prev->state == TASK_DEAD))
3194 preempt_enable_no_resched_notrace();
3195
Ingo Molnardd41f592007-07-09 18:51:59 +02003196 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Peter Zijlstra31656512008-07-18 18:01:23 +02003198 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02003199 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003200
Paul E. McKenney46a5d162015-10-07 09:10:48 -07003201 local_irq_disable();
3202 rcu_note_context_switch();
3203
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02003204 /*
3205 * Make sure that signal_pending_state()->signal_pending() below
3206 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
3207 * done by the caller to avoid the race with signal_wake_up().
3208 */
3209 smp_mb__before_spinlock();
Paul E. McKenney46a5d162015-10-07 09:10:48 -07003210 raw_spin_lock(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003211 lockdep_pin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003213 rq->clock_skip_update <<= 1; /* promote REQ to ACT */
3214
Oleg Nesterov246d86b2010-05-19 14:57:11 +02003215 switch_count = &prev->nivcsw;
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003216 if (!preempt && prev->state) {
Tejun Heo21aa9af2010-06-08 21:40:37 +02003217 if (unlikely(signal_pending_state(prev->state, prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003218 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02003219 } else {
Peter Zijlstra2acca552011-04-05 17:23:50 +02003220 deactivate_task(rq, prev, DEQUEUE_SLEEP);
3221 prev->on_rq = 0;
3222
Tejun Heo21aa9af2010-06-08 21:40:37 +02003223 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02003224 * If a worker went to sleep, notify and ask workqueue
3225 * whether it wants to wake up a task to maintain
3226 * concurrency.
Tejun Heo21aa9af2010-06-08 21:40:37 +02003227 */
3228 if (prev->flags & PF_WQ_WORKER) {
3229 struct task_struct *to_wakeup;
3230
Alexander Gordeev9b7f6592016-03-02 12:53:31 +01003231 to_wakeup = wq_worker_sleeping(prev);
Tejun Heo21aa9af2010-06-08 21:40:37 +02003232 if (to_wakeup)
3233 try_to_wake_up_local(to_wakeup);
3234 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02003235 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003236 switch_count = &prev->nvcsw;
3237 }
3238
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003239 if (task_on_rq_queued(prev))
Peter Zijlstra606dba22012-02-11 06:05:00 +01003240 update_rq_clock(rq);
3241
3242 next = pick_next_task(rq, prev);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01003243 clear_tsk_need_resched(prev);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02003244 clear_preempt_need_resched();
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003245 rq->clock_skip_update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 rq->nr_switches++;
3249 rq->curr = next;
3250 ++*switch_count;
3251
Peter Zijlstrac73464b2015-09-28 18:06:56 +02003252 trace_sched_switch(preempt, prev, next);
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003253 rq = context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003254 } else {
3255 lockdep_unpin_lock(&rq->lock);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003256 raw_spin_unlock_irq(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003259 balance_callback(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
Josh Poimboeuf8e05e962016-02-28 22:22:38 -06003261STACK_FRAME_NON_STANDARD(__schedule); /* switch_to() */
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003262
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003263static inline void sched_submit_work(struct task_struct *tsk)
3264{
Thomas Gleixner3c7d5182011-07-17 20:46:52 +02003265 if (!tsk->state || tsk_is_pi_blocked(tsk))
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003266 return;
3267 /*
3268 * If we are going to sleep and we have plugged IO queued,
3269 * make sure to submit it to avoid deadlocks.
3270 */
3271 if (blk_needs_flush_plug(tsk))
3272 blk_schedule_flush_plug(tsk);
3273}
3274
Andi Kleen722a9f92014-05-02 00:44:38 +02003275asmlinkage __visible void __sched schedule(void)
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003276{
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003277 struct task_struct *tsk = current;
3278
3279 sched_submit_work(tsk);
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003280 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003281 preempt_disable();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003282 __schedule(false);
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003283 sched_preempt_enable_no_resched();
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003284 } while (need_resched());
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003285}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286EXPORT_SYMBOL(schedule);
3287
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01003288#ifdef CONFIG_CONTEXT_TRACKING
Andi Kleen722a9f92014-05-02 00:44:38 +02003289asmlinkage __visible void __sched schedule_user(void)
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003290{
3291 /*
3292 * If we come here after a random call to set_need_resched(),
3293 * or we have been woken up remotely but the IPI has not yet arrived,
3294 * we haven't yet exited the RCU idle mode. Do it here manually until
3295 * we find a better solution.
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003296 *
3297 * NB: There are buggy callers of this function. Ideally we
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +01003298 * should warn if prev_state != CONTEXT_USER, but that will trigger
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003299 * too frequently to make sense yet.
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003300 */
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003301 enum ctx_state prev_state = exception_enter();
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003302 schedule();
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003303 exception_exit(prev_state);
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003304}
3305#endif
3306
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01003307/**
3308 * schedule_preempt_disabled - called with preemption disabled
3309 *
3310 * Returns with preemption disabled. Note: preempt_count must be 1
3311 */
3312void __sched schedule_preempt_disabled(void)
3313{
Thomas Gleixnerba74c142011-03-21 13:32:17 +01003314 sched_preempt_enable_no_resched();
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01003315 schedule();
3316 preempt_disable();
3317}
3318
Frederic Weisbecker06b1f802015-02-16 19:20:07 +01003319static void __sched notrace preempt_schedule_common(void)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003320{
3321 do {
Peter Zijlstra499d7952015-09-28 18:52:36 +02003322 preempt_disable_notrace();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003323 __schedule(true);
Peter Zijlstra499d7952015-09-28 18:52:36 +02003324 preempt_enable_no_resched_notrace();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003325
3326 /*
3327 * Check again in case we missed a preemption opportunity
3328 * between schedule and now.
3329 */
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003330 } while (need_resched());
3331}
3332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333#ifdef CONFIG_PREEMPT
3334/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003335 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003336 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 * occur there and call schedule directly.
3338 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003339asmlinkage __visible void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 /*
3342 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003343 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 */
Frederic Weisbeckerfbb00b52013-06-19 23:56:22 +02003345 if (likely(!preemptible()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return;
3347
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003348 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09003350NOKPROBE_SYMBOL(preempt_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351EXPORT_SYMBOL(preempt_schedule);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003352
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003353/**
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003354 * preempt_schedule_notrace - preempt_schedule called by tracing
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003355 *
3356 * The tracing infrastructure uses preempt_enable_notrace to prevent
3357 * recursion and tracing preempt enabling caused by the tracing
3358 * infrastructure itself. But as tracing can happen in areas coming
3359 * from userspace or just about to enter userspace, a preempt enable
3360 * can occur before user_exit() is called. This will cause the scheduler
3361 * to be called when the system is still in usermode.
3362 *
3363 * To prevent this, the preempt_enable_notrace will use this function
3364 * instead of preempt_schedule() to exit user context if needed before
3365 * calling the scheduler.
3366 */
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003367asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003368{
3369 enum ctx_state prev_ctx;
3370
3371 if (likely(!preemptible()))
3372 return;
3373
3374 do {
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003375 preempt_disable_notrace();
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003376 /*
3377 * Needs preempt disabled in case user_exit() is traced
3378 * and the tracer calls preempt_enable_notrace() causing
3379 * an infinite recursion.
3380 */
3381 prev_ctx = exception_enter();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003382 __schedule(true);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003383 exception_exit(prev_ctx);
3384
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003385 preempt_enable_no_resched_notrace();
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003386 } while (need_resched());
3387}
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003388EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003389
Thomas Gleixner32e475d2013-11-21 12:41:44 +01003390#endif /* CONFIG_PREEMPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
3392/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003393 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 * off of irq context.
3395 * Note, that this is called and return with irqs disabled. This will
3396 * protect us against recursive calling from irq.
3397 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003398asmlinkage __visible void __sched preempt_schedule_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003400 enum ctx_state prev_state;
Ingo Molnar6478d882008-01-25 21:08:33 +01003401
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003402 /* Catch callers which need to be fixed */
Peter Zijlstraf27dde82013-08-14 14:55:31 +02003403 BUG_ON(preempt_count() || !irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003405 prev_state = exception_enter();
3406
Andi Kleen3a5c3592007-10-15 17:00:14 +02003407 do {
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003408 preempt_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003409 local_irq_enable();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003410 __schedule(true);
Andi Kleen3a5c3592007-10-15 17:00:14 +02003411 local_irq_disable();
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003412 sched_preempt_enable_no_resched();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003413 } while (need_resched());
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003414
3415 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Peter Zijlstra63859d42009-09-15 19:14:42 +02003418int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003419 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
Peter Zijlstra63859d42009-09-15 19:14:42 +02003421 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423EXPORT_SYMBOL(default_wake_function);
3424
Ingo Molnarb29739f2006-06-27 02:54:51 -07003425#ifdef CONFIG_RT_MUTEXES
3426
3427/*
3428 * rt_mutex_setprio - set the current priority of a task
3429 * @p: task
3430 * @prio: prio value (kernel-internal form)
3431 *
3432 * This function changes the 'effective' priority of a task. It does
3433 * not touch ->normal_prio like __setscheduler().
3434 *
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003435 * Used by the rt_mutex code to implement priority inheritance
3436 * logic. Call site only calls if the priority of the task changed.
Ingo Molnarb29739f2006-06-27 02:54:51 -07003437 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003438void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003439{
Peter Zijlstraff77e462016-01-18 15:27:07 +01003440 int oldprio, queued, running, queue_flag = DEQUEUE_SAVE | DEQUEUE_MOVE;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003441 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003442 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003443
Dario Faggioliaab03e02013-11-28 11:14:43 +01003444 BUG_ON(prio > MAX_PRIO);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003445
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003446 rq = __task_rq_lock(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003447
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003448 /*
3449 * Idle task boosting is a nono in general. There is one
3450 * exception, when PREEMPT_RT and NOHZ is active:
3451 *
3452 * The idle task calls get_next_timer_interrupt() and holds
3453 * the timer wheel base->lock on the CPU and another CPU wants
3454 * to access the timer (probably to cancel it). We can safely
3455 * ignore the boosting request, as the idle CPU runs this code
3456 * with interrupts disabled and will complete the lock
3457 * protected section without being interrupted. So there is no
3458 * real need to boost.
3459 */
3460 if (unlikely(p == rq->idle)) {
3461 WARN_ON(p != rq->curr);
3462 WARN_ON(p->pi_blocked_on);
3463 goto out_unlock;
3464 }
3465
Steven Rostedta8027072010-09-20 15:13:34 -04003466 trace_sched_pi_setprio(p, prio);
Andrew Mortond5f9f942007-05-08 20:27:06 -07003467 oldprio = p->prio;
Peter Zijlstraff77e462016-01-18 15:27:07 +01003468
3469 if (oldprio == prio)
3470 queue_flag &= ~DEQUEUE_MOVE;
3471
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003472 prev_class = p->sched_class;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003473 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003474 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003475 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01003476 dequeue_task(rq, p, queue_flag);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003477 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003478 put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02003479
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003480 /*
3481 * Boosting condition are:
3482 * 1. -rt task is running and holds mutex A
3483 * --> -dl task blocks on mutex A
3484 *
3485 * 2. -dl task is running and holds mutex A
3486 * --> -dl task blocks on mutex A and could preempt the
3487 * running task
3488 */
3489 if (dl_prio(prio)) {
Oleg Nesterov466af292014-06-06 18:52:06 +02003490 struct task_struct *pi_task = rt_mutex_get_top_task(p);
3491 if (!dl_prio(p->normal_prio) ||
3492 (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003493 p->dl.dl_boosted = 1;
Peter Zijlstraff77e462016-01-18 15:27:07 +01003494 queue_flag |= ENQUEUE_REPLENISH;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003495 } else
3496 p->dl.dl_boosted = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003497 p->sched_class = &dl_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003498 } else if (rt_prio(prio)) {
3499 if (dl_prio(oldprio))
3500 p->dl.dl_boosted = 0;
3501 if (oldprio < prio)
Peter Zijlstraff77e462016-01-18 15:27:07 +01003502 queue_flag |= ENQUEUE_HEAD;
Ingo Molnardd41f592007-07-09 18:51:59 +02003503 p->sched_class = &rt_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003504 } else {
3505 if (dl_prio(oldprio))
3506 p->dl.dl_boosted = 0;
Brian Silverman746db942015-02-18 16:23:56 -08003507 if (rt_prio(oldprio))
3508 p->rt.timeout = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003509 p->sched_class = &fair_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003510 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003511
Ingo Molnarb29739f2006-06-27 02:54:51 -07003512 p->prio = prio;
3513
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003514 if (running)
3515 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003516 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01003517 enqueue_task(rq, p, queue_flag);
Steven Rostedtcb469842008-01-25 21:08:22 +01003518
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003519 check_class_changed(rq, p, prev_class, oldprio);
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003520out_unlock:
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003521 preempt_disable(); /* avoid rq from going away on us */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003522 __task_rq_unlock(rq);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003523
3524 balance_callback(rq);
3525 preempt_enable();
Ingo Molnarb29739f2006-06-27 02:54:51 -07003526}
Ingo Molnarb29739f2006-06-27 02:54:51 -07003527#endif
Dario Faggiolid50dde52013-11-07 14:43:36 +01003528
Ingo Molnar36c8b582006-07-03 00:25:41 -07003529void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003531 int old_prio, delta, queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003533 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Dongsheng Yang75e45d52014-02-11 15:34:50 +08003535 if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 return;
3537 /*
3538 * We have to be careful, if called from sys_setpriority(),
3539 * the task might be in the middle of scheduling on another CPU.
3540 */
3541 rq = task_rq_lock(p, &flags);
3542 /*
3543 * The RT priorities are set via sched_setscheduler(), but we still
3544 * allow the 'normal' nice value to be set - but as expected
3545 * it wont have any effect on scheduling until the task is
Dario Faggioliaab03e02013-11-28 11:14:43 +01003546 * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003548 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 p->static_prio = NICE_TO_PRIO(nice);
3550 goto out_unlock;
3551 }
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003552 queued = task_on_rq_queued(p);
3553 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02003554 dequeue_task(rq, p, DEQUEUE_SAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003557 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003558 old_prio = p->prio;
3559 p->prio = effective_prio(p);
3560 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003562 if (queued) {
Peter Zijlstra1de64442015-09-30 17:44:13 +02003563 enqueue_task(rq, p, ENQUEUE_RESTORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07003565 * If the task increased its priority or is running and
3566 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003568 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Kirill Tkhai88751252014-06-29 00:03:57 +04003569 resched_curr(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
3571out_unlock:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003572 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574EXPORT_SYMBOL(set_user_nice);
3575
Matt Mackalle43379f2005-05-01 08:59:00 -07003576/*
3577 * can_nice - check if a task can reduce its nice value
3578 * @p: task
3579 * @nice: nice value
3580 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003581int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07003582{
Matt Mackall024f4742005-08-18 11:24:19 -07003583 /* convert nice value [19,-20] to rlimit style value [1,40] */
Dongsheng Yang7aa2c012014-05-08 18:33:49 +09003584 int nice_rlim = nice_to_rlimit(nice);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003585
Jiri Slaby78d7d402010-03-05 13:42:54 -08003586 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07003587 capable(CAP_SYS_NICE));
3588}
3589
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590#ifdef __ARCH_WANT_SYS_NICE
3591
3592/*
3593 * sys_nice - change the priority of the current process.
3594 * @increment: priority increment
3595 *
3596 * sys_setpriority is a more generic, but much slower function that
3597 * does similar things.
3598 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003599SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003601 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
3603 /*
3604 * Setpriority might change our priority at the same moment.
3605 * We don't have to worry. Conceptually one call occurs first
3606 * and we have a single winner.
3607 */
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003608 increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003609 nice = task_nice(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003611 nice = clamp_val(nice, MIN_NICE, MAX_NICE);
Matt Mackalle43379f2005-05-01 08:59:00 -07003612 if (increment < 0 && !can_nice(current, nice))
3613 return -EPERM;
3614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 retval = security_task_setnice(current, nice);
3616 if (retval)
3617 return retval;
3618
3619 set_user_nice(current, nice);
3620 return 0;
3621}
3622
3623#endif
3624
3625/**
3626 * task_prio - return the priority value of a given task.
3627 * @p: the task in question.
3628 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003629 * Return: The priority value as seen by users in /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 * RT tasks are offset by -200. Normal tasks are centered
3631 * around 0, value goes from -16 to +15.
3632 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003633int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
3635 return p->prio - MAX_RT_PRIO;
3636}
3637
3638/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 * idle_cpu - is a given cpu idle currently?
3640 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003641 *
3642 * Return: 1 if the CPU is currently idle. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 */
3644int idle_cpu(int cpu)
3645{
Thomas Gleixner908a3282011-09-15 15:32:06 +02003646 struct rq *rq = cpu_rq(cpu);
3647
3648 if (rq->curr != rq->idle)
3649 return 0;
3650
3651 if (rq->nr_running)
3652 return 0;
3653
3654#ifdef CONFIG_SMP
3655 if (!llist_empty(&rq->wake_list))
3656 return 0;
3657#endif
3658
3659 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660}
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662/**
3663 * idle_task - return the idle task for a given cpu.
3664 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003665 *
3666 * Return: The idle task for the cpu @cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003668struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
3670 return cpu_rq(cpu)->idle;
3671}
3672
3673/**
3674 * find_process_by_pid - find a process with a matching PID value.
3675 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003676 *
3677 * The task of @pid, if found. %NULL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003679static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003681 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682}
3683
Dario Faggioliaab03e02013-11-28 11:14:43 +01003684/*
3685 * This function initializes the sched_dl_entity of a newly becoming
3686 * SCHED_DEADLINE task.
3687 *
3688 * Only the static values are considered here, the actual runtime and the
3689 * absolute deadline will be properly calculated when the task is enqueued
3690 * for the first time with its new policy.
3691 */
3692static void
3693__setparam_dl(struct task_struct *p, const struct sched_attr *attr)
3694{
3695 struct sched_dl_entity *dl_se = &p->dl;
3696
Dario Faggioliaab03e02013-11-28 11:14:43 +01003697 dl_se->dl_runtime = attr->sched_runtime;
3698 dl_se->dl_deadline = attr->sched_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003699 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003700 dl_se->flags = attr->sched_flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01003701 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
Peter Zijlstra40767b02015-01-28 15:08:03 +01003702
3703 /*
3704 * Changing the parameters of a task is 'tricky' and we're not doing
3705 * the correct thing -- also see task_dead_dl() and switched_from_dl().
3706 *
3707 * What we SHOULD do is delay the bandwidth release until the 0-lag
3708 * point. This would include retaining the task_struct until that time
3709 * and change dl_overflow() to not immediately decrement the current
3710 * amount.
3711 *
3712 * Instead we retain the current runtime/deadline and let the new
3713 * parameters take effect after the current reservation period lapses.
3714 * This is safe (albeit pessimistic) because the 0-lag point is always
3715 * before the current scheduling deadline.
3716 *
3717 * We can still have temporary overloads because we do not delay the
3718 * change in bandwidth until that time; so admission control is
3719 * not on the safe side. It does however guarantee tasks will never
3720 * consume more than promised.
3721 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003722}
3723
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003724/*
3725 * sched_setparam() passes in -1 for its policy, to let the functions
3726 * it calls know not to change it.
3727 */
3728#define SETPARAM_POLICY -1
3729
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003730static void __setscheduler_params(struct task_struct *p,
3731 const struct sched_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
Dario Faggiolid50dde52013-11-07 14:43:36 +01003733 int policy = attr->sched_policy;
3734
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003735 if (policy == SETPARAM_POLICY)
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003736 policy = p->policy;
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 p->policy = policy;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003739
Dario Faggioliaab03e02013-11-28 11:14:43 +01003740 if (dl_policy(policy))
3741 __setparam_dl(p, attr);
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003742 else if (fair_policy(policy))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003743 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
3744
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003745 /*
3746 * __sched_setscheduler() ensures attr->sched_priority == 0 when
3747 * !rt_policy. Always setting this ensures that things like
3748 * getparam()/getattr() don't report silly values for !rt tasks.
3749 */
3750 p->rt_priority = attr->sched_priority;
Steven Rostedt383afd02014-03-11 19:24:20 -04003751 p->normal_prio = normal_prio(p);
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003752 set_load_weight(p);
3753}
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003754
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003755/* Actually do priority change: must hold pi & rq lock. */
3756static void __setscheduler(struct rq *rq, struct task_struct *p,
Thomas Gleixner0782e632015-05-05 19:49:49 +02003757 const struct sched_attr *attr, bool keep_boost)
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003758{
3759 __setscheduler_params(p, attr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003760
Steven Rostedt383afd02014-03-11 19:24:20 -04003761 /*
Thomas Gleixner0782e632015-05-05 19:49:49 +02003762 * Keep a potential priority boosting if called from
3763 * sched_setscheduler().
Steven Rostedt383afd02014-03-11 19:24:20 -04003764 */
Thomas Gleixner0782e632015-05-05 19:49:49 +02003765 if (keep_boost)
3766 p->prio = rt_mutex_get_effective_prio(p, normal_prio(p));
3767 else
3768 p->prio = normal_prio(p);
Steven Rostedt383afd02014-03-11 19:24:20 -04003769
Dario Faggioliaab03e02013-11-28 11:14:43 +01003770 if (dl_prio(p->prio))
3771 p->sched_class = &dl_sched_class;
3772 else if (rt_prio(p->prio))
Peter Zijlstraffd44db2009-11-10 20:12:01 +01003773 p->sched_class = &rt_sched_class;
3774 else
3775 p->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776}
Dario Faggioliaab03e02013-11-28 11:14:43 +01003777
3778static void
3779__getparam_dl(struct task_struct *p, struct sched_attr *attr)
3780{
3781 struct sched_dl_entity *dl_se = &p->dl;
3782
3783 attr->sched_priority = p->rt_priority;
3784 attr->sched_runtime = dl_se->dl_runtime;
3785 attr->sched_deadline = dl_se->dl_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003786 attr->sched_period = dl_se->dl_period;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003787 attr->sched_flags = dl_se->flags;
3788}
3789
3790/*
3791 * This function validates the new parameters of a -deadline task.
3792 * We ask for the deadline not being zero, and greater or equal
Harald Gustafsson755378a2013-11-07 14:43:40 +01003793 * than the runtime, as well as the period of being zero or
Dario Faggioli332ac172013-11-07 14:43:45 +01003794 * greater than deadline. Furthermore, we have to be sure that
Juri Lellib08278192014-05-13 14:11:31 +02003795 * user parameters are above the internal resolution of 1us (we
3796 * check sched_runtime only since it is always the smaller one) and
3797 * below 2^63 ns (we have to check both sched_deadline and
3798 * sched_period, as the latter can be zero).
Dario Faggioliaab03e02013-11-28 11:14:43 +01003799 */
3800static bool
3801__checkparam_dl(const struct sched_attr *attr)
3802{
Juri Lellib08278192014-05-13 14:11:31 +02003803 /* deadline != 0 */
3804 if (attr->sched_deadline == 0)
3805 return false;
3806
3807 /*
3808 * Since we truncate DL_SCALE bits, make sure we're at least
3809 * that big.
3810 */
3811 if (attr->sched_runtime < (1ULL << DL_SCALE))
3812 return false;
3813
3814 /*
3815 * Since we use the MSB for wrap-around and sign issues, make
3816 * sure it's not set (mind that period can be equal to zero).
3817 */
3818 if (attr->sched_deadline & (1ULL << 63) ||
3819 attr->sched_period & (1ULL << 63))
3820 return false;
3821
3822 /* runtime <= deadline <= period (if period != 0) */
3823 if ((attr->sched_period != 0 &&
3824 attr->sched_period < attr->sched_deadline) ||
3825 attr->sched_deadline < attr->sched_runtime)
3826 return false;
3827
3828 return true;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003829}
3830
David Howellsc69e8d92008-11-14 10:39:19 +11003831/*
3832 * check the target process has a UID that matches the current process's
3833 */
3834static bool check_same_owner(struct task_struct *p)
3835{
3836 const struct cred *cred = current_cred(), *pcred;
3837 bool match;
3838
3839 rcu_read_lock();
3840 pcred = __task_cred(p);
Eric W. Biederman9c806aa2012-02-02 18:54:02 -08003841 match = (uid_eq(cred->euid, pcred->euid) ||
3842 uid_eq(cred->euid, pcred->uid));
David Howellsc69e8d92008-11-14 10:39:19 +11003843 rcu_read_unlock();
3844 return match;
3845}
3846
Wanpeng Li75381602014-11-26 08:44:04 +08003847static bool dl_param_changed(struct task_struct *p,
3848 const struct sched_attr *attr)
3849{
3850 struct sched_dl_entity *dl_se = &p->dl;
3851
3852 if (dl_se->dl_runtime != attr->sched_runtime ||
3853 dl_se->dl_deadline != attr->sched_deadline ||
3854 dl_se->dl_period != attr->sched_period ||
3855 dl_se->flags != attr->sched_flags)
3856 return true;
3857
3858 return false;
3859}
3860
Dario Faggiolid50dde52013-11-07 14:43:36 +01003861static int __sched_setscheduler(struct task_struct *p,
3862 const struct sched_attr *attr,
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003863 bool user, bool pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864{
Steven Rostedt383afd02014-03-11 19:24:20 -04003865 int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
3866 MAX_RT_PRIO - 1 - attr->sched_priority;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003867 int retval, oldprio, oldpolicy = -1, queued, running;
Thomas Gleixner0782e632015-05-05 19:49:49 +02003868 int new_effective_prio, policy = attr->sched_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003870 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003871 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003872 int reset_on_fork;
Peter Zijlstraff77e462016-01-18 15:27:07 +01003873 int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Steven Rostedt66e53932006-06-27 02:54:44 -07003875 /* may grab non-irq protected spin_locks */
3876 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877recheck:
3878 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02003879 if (policy < 0) {
3880 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003882 } else {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003883 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
Lennart Poetteringca94c442009-06-15 17:17:47 +02003884
Henrik Austad20f9cd22015-09-09 17:00:41 +02003885 if (!valid_policy(policy))
Lennart Poetteringca94c442009-06-15 17:17:47 +02003886 return -EINVAL;
3887 }
3888
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003889 if (attr->sched_flags & ~(SCHED_FLAG_RESET_ON_FORK))
3890 return -EINVAL;
3891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 /*
3893 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02003894 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
3895 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 */
Peter Zijlstra0bb040a2014-01-15 17:15:13 +01003897 if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
Dario Faggiolid50dde52013-11-07 14:43:36 +01003898 (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return -EINVAL;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003900 if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
3901 (rt_policy(policy) != (attr->sched_priority != 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return -EINVAL;
3903
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003904 /*
3905 * Allow unprivileged RT tasks to decrease priority:
3906 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10003907 if (user && !capable(CAP_SYS_NICE)) {
Dario Faggiolid50dde52013-11-07 14:43:36 +01003908 if (fair_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003909 if (attr->sched_nice < task_nice(p) &&
Peter Zijlstraeaad4512014-01-16 17:54:25 +01003910 !can_nice(p, attr->sched_nice))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003911 return -EPERM;
3912 }
3913
Ingo Molnare05606d2007-07-09 18:51:59 +02003914 if (rt_policy(policy)) {
Oleg Nesterova44702e82010-06-11 01:09:44 +02003915 unsigned long rlim_rtprio =
3916 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003917
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003918 /* can't set/change the rt policy */
3919 if (policy != p->policy && !rlim_rtprio)
3920 return -EPERM;
3921
3922 /* can't increase priority */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003923 if (attr->sched_priority > p->rt_priority &&
3924 attr->sched_priority > rlim_rtprio)
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003925 return -EPERM;
3926 }
Darren Hartc02aa732011-02-17 15:37:07 -08003927
Juri Lellid44753b2014-03-03 12:09:21 +01003928 /*
3929 * Can't set/change SCHED_DEADLINE policy at all for now
3930 * (safest behavior); in the future we would like to allow
3931 * unprivileged DL tasks to increase their relative deadline
3932 * or reduce their runtime (both ways reducing utilization)
3933 */
3934 if (dl_policy(policy))
3935 return -EPERM;
3936
Ingo Molnardd41f592007-07-09 18:51:59 +02003937 /*
Darren Hartc02aa732011-02-17 15:37:07 -08003938 * Treat SCHED_IDLE as nice 20. Only allow a switch to
3939 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02003940 */
Henrik Austad20f9cd22015-09-09 17:00:41 +02003941 if (idle_policy(p->policy) && !idle_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003942 if (!can_nice(p, task_nice(p)))
Darren Hartc02aa732011-02-17 15:37:07 -08003943 return -EPERM;
3944 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003945
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003946 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11003947 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003948 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003949
3950 /* Normal users shall not reset the sched_reset_on_fork flag */
3951 if (p->sched_reset_on_fork && !reset_on_fork)
3952 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003955 if (user) {
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003956 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003957 if (retval)
3958 return retval;
3959 }
3960
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07003962 * make sure no PI-waiters arrive (or leave) while we are
3963 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003964 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003965 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 * runqueue lock must be held.
3967 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003968 rq = task_rq_lock(p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003969
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003970 /*
3971 * Changing the policy of the stop threads its a very bad idea
3972 */
3973 if (p == rq->stop) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003974 task_rq_unlock(rq, p, &flags);
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003975 return -EINVAL;
3976 }
3977
Dario Faggiolia51e9192011-03-24 14:00:18 +01003978 /*
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003979 * If not changing anything there's no need to proceed further,
3980 * but store a possible modification of reset_on_fork.
Dario Faggiolia51e9192011-03-24 14:00:18 +01003981 */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003982 if (unlikely(policy == p->policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003983 if (fair_policy(policy) && attr->sched_nice != task_nice(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003984 goto change;
3985 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
3986 goto change;
Wanpeng Li75381602014-11-26 08:44:04 +08003987 if (dl_policy(policy) && dl_param_changed(p, attr))
Dario Faggioliaab03e02013-11-28 11:14:43 +01003988 goto change;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003989
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003990 p->sched_reset_on_fork = reset_on_fork;
Namhyung Kim45afb172012-07-07 16:49:02 +09003991 task_rq_unlock(rq, p, &flags);
Dario Faggiolia51e9192011-03-24 14:00:18 +01003992 return 0;
3993 }
Dario Faggiolid50dde52013-11-07 14:43:36 +01003994change:
Dario Faggiolia51e9192011-03-24 14:00:18 +01003995
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003996 if (user) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003997#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003998 /*
3999 * Do not allow realtime tasks into groups that have no runtime
4000 * assigned.
4001 */
4002 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01004003 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
4004 !task_group_is_autogroup(task_group(p))) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004005 task_rq_unlock(rq, p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004006 return -EPERM;
4007 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004008#endif
Dario Faggioli332ac172013-11-07 14:43:45 +01004009#ifdef CONFIG_SMP
4010 if (dl_bandwidth_enabled() && dl_policy(policy)) {
4011 cpumask_t *span = rq->rd->span;
Dario Faggioli332ac172013-11-07 14:43:45 +01004012
4013 /*
4014 * Don't allow tasks with an affinity mask smaller than
4015 * the entire root_domain to become SCHED_DEADLINE. We
4016 * will also fail if there's no bandwidth available.
4017 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004018 if (!cpumask_subset(span, &p->cpus_allowed) ||
4019 rq->rd->dl_bw.bw == 0) {
Dario Faggioli332ac172013-11-07 14:43:45 +01004020 task_rq_unlock(rq, p, &flags);
4021 return -EPERM;
4022 }
4023 }
4024#endif
4025 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02004026
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 /* recheck policy now with rq lock held */
4028 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4029 policy = oldpolicy = -1;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004030 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 goto recheck;
4032 }
Dario Faggioli332ac172013-11-07 14:43:45 +01004033
4034 /*
4035 * If setscheduling to SCHED_DEADLINE (or changing the parameters
4036 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
4037 * is available.
4038 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004039 if ((dl_policy(policy) || dl_task(p)) && dl_overflow(p, policy, attr)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01004040 task_rq_unlock(rq, p, &flags);
4041 return -EBUSY;
4042 }
4043
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004044 p->sched_reset_on_fork = reset_on_fork;
4045 oldprio = p->prio;
4046
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004047 if (pi) {
4048 /*
4049 * Take priority boosted tasks into account. If the new
4050 * effective priority is unchanged, we just store the new
4051 * normal parameters and do not touch the scheduler class and
4052 * the runqueue. This will be done when the task deboost
4053 * itself.
4054 */
4055 new_effective_prio = rt_mutex_get_effective_prio(p, newprio);
Peter Zijlstraff77e462016-01-18 15:27:07 +01004056 if (new_effective_prio == oldprio)
4057 queue_flags &= ~DEQUEUE_MOVE;
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004058 }
4059
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004060 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004061 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004062 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01004063 dequeue_task(rq, p, queue_flags);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004064 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04004065 put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004066
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004067 prev_class = p->sched_class;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004068 __setscheduler(rq, p, attr, pi);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004069
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004070 if (running)
4071 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004072 if (queued) {
Thomas Gleixner81a44c52014-02-07 20:58:41 +01004073 /*
4074 * We enqueue to tail when the priority of a task is
4075 * increased (user space view).
4076 */
Peter Zijlstraff77e462016-01-18 15:27:07 +01004077 if (oldprio < p->prio)
4078 queue_flags |= ENQUEUE_HEAD;
Peter Zijlstra1de64442015-09-30 17:44:13 +02004079
Peter Zijlstraff77e462016-01-18 15:27:07 +01004080 enqueue_task(rq, p, queue_flags);
Thomas Gleixner81a44c52014-02-07 20:58:41 +01004081 }
Steven Rostedtcb469842008-01-25 21:08:22 +01004082
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004083 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02004084 preempt_disable(); /* avoid rq from going away on us */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004085 task_rq_unlock(rq, p, &flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004086
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004087 if (pi)
4088 rt_mutex_adjust_pi(p);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004089
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02004090 /*
4091 * Run balance callbacks after we've adjusted the PI chain.
4092 */
4093 balance_callback(rq);
4094 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
4096 return 0;
4097}
Rusty Russell961ccdd2008-06-23 13:55:38 +10004098
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004099static int _sched_setscheduler(struct task_struct *p, int policy,
4100 const struct sched_param *param, bool check)
4101{
4102 struct sched_attr attr = {
4103 .sched_policy = policy,
4104 .sched_priority = param->sched_priority,
4105 .sched_nice = PRIO_TO_NICE(p->static_prio),
4106 };
4107
Steven Rostedtc13db6b2014-07-23 11:28:26 -04004108 /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
4109 if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004110 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
4111 policy &= ~SCHED_RESET_ON_FORK;
4112 attr.sched_policy = policy;
4113 }
4114
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004115 return __sched_setscheduler(p, &attr, check, true);
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004116}
Rusty Russell961ccdd2008-06-23 13:55:38 +10004117/**
4118 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
4119 * @p: the task in question.
4120 * @policy: new policy.
4121 * @param: structure containing the new RT priority.
4122 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004123 * Return: 0 on success. An error code otherwise.
4124 *
Rusty Russell961ccdd2008-06-23 13:55:38 +10004125 * NOTE that the task may be already dead.
4126 */
4127int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07004128 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10004129{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004130 return _sched_setscheduler(p, policy, param, true);
Rusty Russell961ccdd2008-06-23 13:55:38 +10004131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132EXPORT_SYMBOL_GPL(sched_setscheduler);
4133
Dario Faggiolid50dde52013-11-07 14:43:36 +01004134int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
4135{
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004136 return __sched_setscheduler(p, attr, true, true);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004137}
4138EXPORT_SYMBOL_GPL(sched_setattr);
4139
Rusty Russell961ccdd2008-06-23 13:55:38 +10004140/**
4141 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
4142 * @p: the task in question.
4143 * @policy: new policy.
4144 * @param: structure containing the new RT priority.
4145 *
4146 * Just like sched_setscheduler, only don't bother checking if the
4147 * current context has permission. For example, this is needed in
4148 * stop_machine(): we create temporary high priority worker threads,
4149 * but our caller might not have that capability.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004150 *
4151 * Return: 0 on success. An error code otherwise.
Rusty Russell961ccdd2008-06-23 13:55:38 +10004152 */
4153int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07004154 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10004155{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004156 return _sched_setscheduler(p, policy, param, false);
Rusty Russell961ccdd2008-06-23 13:55:38 +10004157}
Davidlohr Bueso84778472015-09-02 01:28:44 -07004158EXPORT_SYMBOL_GPL(sched_setscheduler_nocheck);
Rusty Russell961ccdd2008-06-23 13:55:38 +10004159
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004160static int
4161do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 struct sched_param lparam;
4164 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004165 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
4167 if (!param || pid < 0)
4168 return -EINVAL;
4169 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4170 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004171
4172 rcu_read_lock();
4173 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004175 if (p != NULL)
4176 retval = sched_setscheduler(p, policy, &lparam);
4177 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 return retval;
4180}
4181
Dario Faggiolid50dde52013-11-07 14:43:36 +01004182/*
4183 * Mimics kernel/events/core.c perf_copy_attr().
4184 */
4185static int sched_copy_attr(struct sched_attr __user *uattr,
4186 struct sched_attr *attr)
4187{
4188 u32 size;
4189 int ret;
4190
4191 if (!access_ok(VERIFY_WRITE, uattr, SCHED_ATTR_SIZE_VER0))
4192 return -EFAULT;
4193
4194 /*
4195 * zero the full structure, so that a short copy will be nice.
4196 */
4197 memset(attr, 0, sizeof(*attr));
4198
4199 ret = get_user(size, &uattr->size);
4200 if (ret)
4201 return ret;
4202
4203 if (size > PAGE_SIZE) /* silly large */
4204 goto err_size;
4205
4206 if (!size) /* abi compat */
4207 size = SCHED_ATTR_SIZE_VER0;
4208
4209 if (size < SCHED_ATTR_SIZE_VER0)
4210 goto err_size;
4211
4212 /*
4213 * If we're handed a bigger struct than we know of,
4214 * ensure all the unknown bits are 0 - i.e. new
4215 * user-space does not rely on any kernel feature
4216 * extensions we dont know about yet.
4217 */
4218 if (size > sizeof(*attr)) {
4219 unsigned char __user *addr;
4220 unsigned char __user *end;
4221 unsigned char val;
4222
4223 addr = (void __user *)uattr + sizeof(*attr);
4224 end = (void __user *)uattr + size;
4225
4226 for (; addr < end; addr++) {
4227 ret = get_user(val, addr);
4228 if (ret)
4229 return ret;
4230 if (val)
4231 goto err_size;
4232 }
4233 size = sizeof(*attr);
4234 }
4235
4236 ret = copy_from_user(attr, uattr, size);
4237 if (ret)
4238 return -EFAULT;
4239
4240 /*
4241 * XXX: do we want to be lenient like existing syscalls; or do we want
4242 * to be strict and return an error on out-of-bounds values?
4243 */
Dongsheng Yang75e45d52014-02-11 15:34:50 +08004244 attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004245
Michael Kerriske78c7bc2014-05-09 16:54:28 +02004246 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004247
4248err_size:
4249 put_user(sizeof(*attr), &uattr->size);
Michael Kerriske78c7bc2014-05-09 16:54:28 +02004250 return -E2BIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004251}
4252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253/**
4254 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4255 * @pid: the pid in question.
4256 * @policy: new policy.
4257 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004258 *
4259 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004261SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
4262 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
Jason Baronc21761f2006-01-18 17:43:03 -08004264 /* negative values for policy are not valid */
4265 if (policy < 0)
4266 return -EINVAL;
4267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 return do_sched_setscheduler(pid, policy, param);
4269}
4270
4271/**
4272 * sys_sched_setparam - set/change the RT priority of a thread
4273 * @pid: the pid in question.
4274 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004275 *
4276 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004278SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
Steven Rostedtc13db6b2014-07-23 11:28:26 -04004280 return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281}
4282
4283/**
Dario Faggiolid50dde52013-11-07 14:43:36 +01004284 * sys_sched_setattr - same as above, but with extended sched_attr
4285 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01004286 * @uattr: structure containing the extended parameters.
Masanari Iidadb66d752014-04-18 01:59:15 +09004287 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004288 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004289SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
4290 unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004291{
4292 struct sched_attr attr;
4293 struct task_struct *p;
4294 int retval;
4295
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004296 if (!uattr || pid < 0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004297 return -EINVAL;
4298
Michael Kerrisk143cf232014-05-09 16:54:15 +02004299 retval = sched_copy_attr(uattr, &attr);
4300 if (retval)
4301 return retval;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004302
Richard Weinbergerb14ed2c2014-06-02 22:38:34 +02004303 if ((int)attr.sched_policy < 0)
Peter Zijlstradbdb2272014-05-09 10:49:03 +02004304 return -EINVAL;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004305
4306 rcu_read_lock();
4307 retval = -ESRCH;
4308 p = find_process_by_pid(pid);
4309 if (p != NULL)
4310 retval = sched_setattr(p, &attr);
4311 rcu_read_unlock();
4312
4313 return retval;
4314}
4315
4316/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4318 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004319 *
4320 * Return: On success, the policy of the thread. Otherwise, a negative error
4321 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004323SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004325 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004326 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004329 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004332 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 p = find_process_by_pid(pid);
4334 if (p) {
4335 retval = security_task_getscheduler(p);
4336 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02004337 retval = p->policy
4338 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004340 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 return retval;
4342}
4343
4344/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02004345 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 * @pid: the pid in question.
4347 * @param: structure containing the RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004348 *
4349 * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
4350 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004352SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353{
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02004354 struct sched_param lp = { .sched_priority = 0 };
Ingo Molnar36c8b582006-07-03 00:25:41 -07004355 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004356 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
4358 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004361 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 p = find_process_by_pid(pid);
4363 retval = -ESRCH;
4364 if (!p)
4365 goto out_unlock;
4366
4367 retval = security_task_getscheduler(p);
4368 if (retval)
4369 goto out_unlock;
4370
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02004371 if (task_has_rt_policy(p))
4372 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004373 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
4375 /*
4376 * This one might sleep, we cannot do it with a spinlock held ...
4377 */
4378 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 return retval;
4381
4382out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004383 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 return retval;
4385}
4386
Dario Faggiolid50dde52013-11-07 14:43:36 +01004387static int sched_read_attr(struct sched_attr __user *uattr,
4388 struct sched_attr *attr,
4389 unsigned int usize)
4390{
4391 int ret;
4392
4393 if (!access_ok(VERIFY_WRITE, uattr, usize))
4394 return -EFAULT;
4395
4396 /*
4397 * If we're handed a smaller struct than we know of,
4398 * ensure all the unknown bits are 0 - i.e. old
4399 * user-space does not get uncomplete information.
4400 */
4401 if (usize < sizeof(*attr)) {
4402 unsigned char *addr;
4403 unsigned char *end;
4404
4405 addr = (void *)attr + usize;
4406 end = (void *)attr + sizeof(*attr);
4407
4408 for (; addr < end; addr++) {
4409 if (*addr)
Michael Kerrisk22400672014-05-09 16:54:33 +02004410 return -EFBIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004411 }
4412
4413 attr->size = usize;
4414 }
4415
Vegard Nossum4efbc452014-02-16 22:24:17 +01004416 ret = copy_to_user(uattr, attr, attr->size);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004417 if (ret)
4418 return -EFAULT;
4419
Michael Kerrisk22400672014-05-09 16:54:33 +02004420 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004421}
4422
4423/**
Dario Faggioliaab03e02013-11-28 11:14:43 +01004424 * sys_sched_getattr - similar to sched_getparam, but with sched_attr
Dario Faggiolid50dde52013-11-07 14:43:36 +01004425 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01004426 * @uattr: structure containing the extended parameters.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004427 * @size: sizeof(attr) for fwd/bwd comp.
Masanari Iidadb66d752014-04-18 01:59:15 +09004428 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004429 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004430SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
4431 unsigned int, size, unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004432{
4433 struct sched_attr attr = {
4434 .size = sizeof(struct sched_attr),
4435 };
4436 struct task_struct *p;
4437 int retval;
4438
4439 if (!uattr || pid < 0 || size > PAGE_SIZE ||
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004440 size < SCHED_ATTR_SIZE_VER0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004441 return -EINVAL;
4442
4443 rcu_read_lock();
4444 p = find_process_by_pid(pid);
4445 retval = -ESRCH;
4446 if (!p)
4447 goto out_unlock;
4448
4449 retval = security_task_getscheduler(p);
4450 if (retval)
4451 goto out_unlock;
4452
4453 attr.sched_policy = p->policy;
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004454 if (p->sched_reset_on_fork)
4455 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004456 if (task_has_dl_policy(p))
4457 __getparam_dl(p, &attr);
4458 else if (task_has_rt_policy(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01004459 attr.sched_priority = p->rt_priority;
4460 else
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05004461 attr.sched_nice = task_nice(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004462
4463 rcu_read_unlock();
4464
4465 retval = sched_read_attr(uattr, &attr, size);
4466 return retval;
4467
4468out_unlock:
4469 rcu_read_unlock();
4470 return retval;
4471}
4472
Rusty Russell96f874e22008-11-25 02:35:14 +10304473long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304475 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004476 struct task_struct *p;
4477 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004479 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
4481 p = find_process_by_pid(pid);
4482 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004483 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return -ESRCH;
4485 }
4486
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004487 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004489 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
Tejun Heo14a40ff2013-03-19 13:45:20 -07004491 if (p->flags & PF_NO_SETAFFINITY) {
4492 retval = -EINVAL;
4493 goto out_put_task;
4494 }
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304495 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
4496 retval = -ENOMEM;
4497 goto out_put_task;
4498 }
4499 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
4500 retval = -ENOMEM;
4501 goto out_free_cpus_allowed;
4502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 retval = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004504 if (!check_same_owner(p)) {
4505 rcu_read_lock();
4506 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
4507 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004508 goto out_free_new_mask;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004509 }
4510 rcu_read_unlock();
4511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004513 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07004514 if (retval)
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004515 goto out_free_new_mask;
David Quigleye7834f82006-06-23 02:03:59 -07004516
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004517
4518 cpuset_cpus_allowed(p, cpus_allowed);
4519 cpumask_and(new_mask, in_mask, cpus_allowed);
4520
Dario Faggioli332ac172013-11-07 14:43:45 +01004521 /*
4522 * Since bandwidth control happens on root_domain basis,
4523 * if admission test is enabled, we only admit -deadline
4524 * tasks allowed to run on all the CPUs in the task's
4525 * root_domain.
4526 */
4527#ifdef CONFIG_SMP
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004528 if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
4529 rcu_read_lock();
4530 if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01004531 retval = -EBUSY;
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004532 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004533 goto out_free_new_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01004534 }
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004535 rcu_read_unlock();
Dario Faggioli332ac172013-11-07 14:43:45 +01004536 }
4537#endif
Peter Zijlstra49246272010-10-17 21:46:10 +02004538again:
Peter Zijlstra25834c72015-05-15 17:43:34 +02004539 retval = __set_cpus_allowed_ptr(p, new_mask, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
Paul Menage8707d8b2007-10-18 23:40:22 -07004541 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304542 cpuset_cpus_allowed(p, cpus_allowed);
4543 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07004544 /*
4545 * We must have raced with a concurrent cpuset
4546 * update. Just reset the cpus_allowed to the
4547 * cpuset's cpus_allowed
4548 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304549 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004550 goto again;
4551 }
4552 }
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004553out_free_new_mask:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304554 free_cpumask_var(new_mask);
4555out_free_cpus_allowed:
4556 free_cpumask_var(cpus_allowed);
4557out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 return retval;
4560}
4561
4562static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e22008-11-25 02:35:14 +10304563 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564{
Rusty Russell96f874e22008-11-25 02:35:14 +10304565 if (len < cpumask_size())
4566 cpumask_clear(new_mask);
4567 else if (len > cpumask_size())
4568 len = cpumask_size();
4569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4571}
4572
4573/**
4574 * sys_sched_setaffinity - set the cpu affinity of a process
4575 * @pid: pid of the process
4576 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4577 * @user_mask_ptr: user-space pointer to the new cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004578 *
4579 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004581SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
4582 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304584 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 int retval;
4586
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304587 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4588 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304590 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
4591 if (retval == 0)
4592 retval = sched_setaffinity(pid, new_mask);
4593 free_cpumask_var(new_mask);
4594 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595}
4596
Rusty Russell96f874e22008-11-25 02:35:14 +10304597long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004599 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00004600 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004603 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605 retval = -ESRCH;
4606 p = find_process_by_pid(pid);
4607 if (!p)
4608 goto out_unlock;
4609
David Quigleye7834f82006-06-23 02:03:59 -07004610 retval = security_task_getscheduler(p);
4611 if (retval)
4612 goto out_unlock;
4613
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004614 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02004615 cpumask_and(mask, &p->cpus_allowed, cpu_active_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004616 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004619 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
Ulrich Drepper9531b622007-08-09 11:16:46 +02004621 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622}
4623
4624/**
4625 * sys_sched_getaffinity - get the cpu affinity of a process
4626 * @pid: pid of the process
4627 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4628 * @user_mask_ptr: user-space pointer to hold the current cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004629 *
4630 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004632SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
4633 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
4635 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10304636 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Anton Blanchard84fba5e2010-04-06 17:02:19 +10004638 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004639 return -EINVAL;
4640 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 return -EINVAL;
4642
Rusty Russellf17c8602008-11-25 02:35:11 +10304643 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
4644 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Rusty Russellf17c8602008-11-25 02:35:11 +10304646 ret = sched_getaffinity(pid, mask);
4647 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09004648 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004649
4650 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10304651 ret = -EFAULT;
4652 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004653 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10304654 }
4655 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Rusty Russellf17c8602008-11-25 02:35:11 +10304657 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658}
4659
4660/**
4661 * sys_sched_yield - yield the current processor to other threads.
4662 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004663 * This function yields the current CPU to other tasks. If there are no
4664 * other threads running on this CPU then this function will return.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004665 *
4666 * Return: 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004668SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004670 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Ingo Molnar2d723762007-10-15 17:00:12 +02004672 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004673 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
4675 /*
4676 * Since we are going to call schedule() anyway, there's
4677 * no need to preempt or enable interrupts:
4678 */
4679 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004680 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01004681 do_raw_spin_unlock(&rq->lock);
Thomas Gleixnerba74c142011-03-21 13:32:17 +01004682 sched_preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
4684 schedule();
4685
4686 return 0;
4687}
4688
Herbert Xu02b67cc32008-01-25 21:08:28 +01004689int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004691 if (should_resched(0)) {
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004692 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 return 1;
4694 }
4695 return 0;
4696}
Herbert Xu02b67cc32008-01-25 21:08:28 +01004697EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
4699/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004700 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 * call schedule, and on return reacquire the lock.
4702 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004703 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 * operations here to prevent schedule() from being called twice (once via
4705 * spin_unlock(), once by hand).
4706 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004707int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004709 int resched = should_resched(PREEMPT_LOCK_OFFSET);
Jan Kara6df3cec2005-06-13 15:52:32 -07004710 int ret = 0;
4711
Peter Zijlstraf607c662009-07-20 19:16:29 +02004712 lockdep_assert_held(lock);
4713
Paul E. McKenney4a81e832014-06-20 16:49:01 -07004714 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004716 if (resched)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004717 preempt_schedule_common();
Nick Piggin95c354f2008-01-30 13:31:20 +01004718 else
4719 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004720 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004725EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004727int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728{
4729 BUG_ON(!in_softirq());
4730
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004731 if (should_resched(SOFTIRQ_DISABLE_OFFSET)) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004732 local_bh_enable();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004733 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 local_bh_disable();
4735 return 1;
4736 }
4737 return 0;
4738}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004739EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741/**
4742 * yield - yield the current processor to other threads.
4743 *
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01004744 * Do not ever use this function, there's a 99% chance you're doing it wrong.
4745 *
4746 * The scheduler is at all times free to pick the calling task as the most
4747 * eligible task to run, if removing the yield() call from your code breaks
4748 * it, its already broken.
4749 *
4750 * Typical broken usage is:
4751 *
4752 * while (!event)
4753 * yield();
4754 *
4755 * where one assumes that yield() will let 'the other' process run that will
4756 * make event true. If the current task is a SCHED_FIFO task that will never
4757 * happen. Never use yield() as a progress guarantee!!
4758 *
4759 * If you want to use yield() to wait for something, use wait_event().
4760 * If you want to use yield() to be 'nice' for others, use cond_resched().
4761 * If you still want to use yield(), do not!
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 */
4763void __sched yield(void)
4764{
4765 set_current_state(TASK_RUNNING);
4766 sys_sched_yield();
4767}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768EXPORT_SYMBOL(yield);
4769
Mike Galbraithd95f4122011-02-01 09:50:51 -05004770/**
4771 * yield_to - yield the current processor to another thread in
4772 * your thread group, or accelerate that thread toward the
4773 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07004774 * @p: target task
4775 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05004776 *
4777 * It's the caller's job to ensure that the target task struct
4778 * can't go away on us before we can do any checks.
4779 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004780 * Return:
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304781 * true (>0) if we indeed boosted the target task.
4782 * false (0) if we failed to boost the target.
4783 * -ESRCH if there's no task to yield to.
Mike Galbraithd95f4122011-02-01 09:50:51 -05004784 */
Dan Carpenterfa933842014-05-23 13:20:42 +03004785int __sched yield_to(struct task_struct *p, bool preempt)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004786{
4787 struct task_struct *curr = current;
4788 struct rq *rq, *p_rq;
4789 unsigned long flags;
Dan Carpenterc3c18642013-02-05 14:37:51 +03004790 int yielded = 0;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004791
4792 local_irq_save(flags);
4793 rq = this_rq();
4794
4795again:
4796 p_rq = task_rq(p);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304797 /*
4798 * If we're the only runnable task on the rq and target rq also
4799 * has only one task, there's absolutely no point in yielding.
4800 */
4801 if (rq->nr_running == 1 && p_rq->nr_running == 1) {
4802 yielded = -ESRCH;
4803 goto out_irq;
4804 }
4805
Mike Galbraithd95f4122011-02-01 09:50:51 -05004806 double_rq_lock(rq, p_rq);
Shigeru Yoshida39e24d8f2013-11-23 18:38:01 +09004807 if (task_rq(p) != p_rq) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004808 double_rq_unlock(rq, p_rq);
4809 goto again;
4810 }
4811
4812 if (!curr->sched_class->yield_to_task)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304813 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004814
4815 if (curr->sched_class != p->sched_class)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304816 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004817
4818 if (task_running(p_rq, p) || p->state)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304819 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004820
4821 yielded = curr->sched_class->yield_to_task(rq, p, preempt);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004822 if (yielded) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004823 schedstat_inc(rq, yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004824 /*
4825 * Make p's CPU reschedule; pick_next_entity takes care of
4826 * fairness.
4827 */
4828 if (preempt && rq != p_rq)
Kirill Tkhai88751252014-06-29 00:03:57 +04004829 resched_curr(p_rq);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004830 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05004831
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304832out_unlock:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004833 double_rq_unlock(rq, p_rq);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304834out_irq:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004835 local_irq_restore(flags);
4836
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304837 if (yielded > 0)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004838 schedule();
4839
4840 return yielded;
4841}
4842EXPORT_SYMBOL_GPL(yield_to);
4843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004845 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848long __sched io_schedule_timeout(long timeout)
4849{
NeilBrown9cff8ad2015-02-13 15:49:17 +11004850 int old_iowait = current->in_iowait;
4851 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 long ret;
4853
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004854 current->in_iowait = 1;
Shaohua Li10d784e2015-05-08 10:51:29 -07004855 blk_schedule_flush_plug(current);
NeilBrown9cff8ad2015-02-13 15:49:17 +11004856
4857 delayacct_blkio_start();
4858 rq = raw_rq();
4859 atomic_inc(&rq->nr_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 ret = schedule_timeout(timeout);
NeilBrown9cff8ad2015-02-13 15:49:17 +11004861 current->in_iowait = old_iowait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004863 delayacct_blkio_end();
NeilBrown9cff8ad2015-02-13 15:49:17 +11004864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 return ret;
4866}
NeilBrown9cff8ad2015-02-13 15:49:17 +11004867EXPORT_SYMBOL(io_schedule_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
4869/**
4870 * sys_sched_get_priority_max - return maximum RT priority.
4871 * @policy: scheduling class.
4872 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004873 * Return: On success, this syscall returns the maximum
4874 * rt_priority that can be used by a given scheduling class.
4875 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004877SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878{
4879 int ret = -EINVAL;
4880
4881 switch (policy) {
4882 case SCHED_FIFO:
4883 case SCHED_RR:
4884 ret = MAX_USER_RT_PRIO-1;
4885 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004886 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004888 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004889 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 ret = 0;
4891 break;
4892 }
4893 return ret;
4894}
4895
4896/**
4897 * sys_sched_get_priority_min - return minimum RT priority.
4898 * @policy: scheduling class.
4899 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004900 * Return: On success, this syscall returns the minimum
4901 * rt_priority that can be used by a given scheduling class.
4902 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004904SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
4906 int ret = -EINVAL;
4907
4908 switch (policy) {
4909 case SCHED_FIFO:
4910 case SCHED_RR:
4911 ret = 1;
4912 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004913 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004915 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004916 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 ret = 0;
4918 }
4919 return ret;
4920}
4921
4922/**
4923 * sys_sched_rr_get_interval - return the default timeslice of a process.
4924 * @pid: pid of the process.
4925 * @interval: userspace pointer to the timeslice value.
4926 *
4927 * this syscall writes the default timeslice value of a given process
4928 * into the user-space timespec buffer. A value of '0' means infinity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004929 *
4930 * Return: On success, 0 and the timeslice is in @interval. Otherwise,
4931 * an error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01004933SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01004934 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004936 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004937 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004938 unsigned long flags;
4939 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004940 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
4943 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004944 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
4946 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004947 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 p = find_process_by_pid(pid);
4949 if (!p)
4950 goto out_unlock;
4951
4952 retval = security_task_getscheduler(p);
4953 if (retval)
4954 goto out_unlock;
4955
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004956 rq = task_rq_lock(p, &flags);
Peter Zijlstraa57beec2014-01-27 11:54:13 +01004957 time_slice = 0;
4958 if (p->sched_class->get_rr_interval)
4959 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004960 task_rq_unlock(rq, p, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004961
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004962 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004963 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004968 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 return retval;
4970}
4971
Steven Rostedt7c731e02008-05-12 21:20:41 +02004972static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004973
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004974void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 unsigned long free = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004977 int ppid;
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09004978 unsigned long state = p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09004980 if (state)
4981 state = __ffs(state) + 1;
Erik Gilling28d06862010-11-19 18:08:51 -08004982 printk(KERN_INFO "%-15.15s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004983 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02004984#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004986 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004988 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989#else
4990 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004991 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004993 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994#endif
4995#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05004996 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997#endif
Oleg Nesterova90e9842014-12-10 15:45:21 -08004998 ppid = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004999 rcu_read_lock();
Oleg Nesterova90e9842014-12-10 15:45:21 -08005000 if (pid_alive(p))
5001 ppid = task_pid_nr(rcu_dereference(p->real_parent));
Paul E. McKenney4e797522012-11-07 13:35:32 -08005002 rcu_read_unlock();
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005003 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
Paul E. McKenney4e797522012-11-07 13:35:32 -08005004 task_pid_nr(p), ppid,
David Rientjesaa47b7e2009-05-04 01:38:05 -07005005 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
Tejun Heo3d1cb202013-04-30 15:27:22 -07005007 print_worker_info(KERN_INFO, p);
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005008 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
5010
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005011void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005013 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Ingo Molnar4bd77322007-07-11 21:21:47 +02005015#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005016 printk(KERN_INFO
5017 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005019 printk(KERN_INFO
5020 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02005022 rcu_read_lock();
Oleg Nesterov5d07f422014-08-13 21:19:53 +02005023 for_each_process_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 /*
5025 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005026 * console might take a lot of time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 */
5028 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005029 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005030 sched_show_task(p);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02005031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005033 touch_all_softlockup_watchdogs();
5034
Ingo Molnardd41f592007-07-09 18:51:59 +02005035#ifdef CONFIG_SCHED_DEBUG
5036 sysrq_sched_debug_show();
5037#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02005038 rcu_read_unlock();
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005039 /*
5040 * Only show locks if all tasks are dumped:
5041 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02005042 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005043 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044}
5045
Paul Gortmaker0db06282013-06-19 14:53:51 -04005046void init_idle_bootup_task(struct task_struct *idle)
Ingo Molnar1df21052007-07-09 18:51:58 +02005047{
Ingo Molnardd41f592007-07-09 18:51:59 +02005048 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005049}
5050
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005051/**
5052 * init_idle - set up an idle thread for a given CPU
5053 * @idle: task in question
5054 * @cpu: cpu the idle task belongs to
5055 *
5056 * NOTE: this function does not set the idle thread's NEED_RESCHED
5057 * flag, to make booting more robust.
5058 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005059void init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005061 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 unsigned long flags;
5063
Peter Zijlstra25834c72015-05-15 17:43:34 +02005064 raw_spin_lock_irqsave(&idle->pi_lock, flags);
5065 raw_spin_lock(&rq->lock);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01005066
Rik van Riel5e1576e2013-10-07 11:29:26 +01005067 __sched_fork(0, idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01005068 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02005069 idle->se.exec_start = sched_clock();
5070
Mark Rutlande1b77c92016-03-09 14:08:18 -08005071 kasan_unpoison_task_stack(idle);
5072
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02005073#ifdef CONFIG_SMP
5074 /*
5075 * Its possible that init_idle() gets called multiple times on a task,
5076 * in that case do_set_cpus_allowed() will not do the right thing.
5077 *
5078 * And since this is boot we can forgo the serialization.
5079 */
5080 set_cpus_allowed_common(idle, cpumask_of(cpu));
5081#endif
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005082 /*
5083 * We're having a chicken and egg problem, even though we are
5084 * holding rq->lock, the cpu isn't yet set to this cpu so the
5085 * lockdep check in task_group() will fail.
5086 *
5087 * Similar case to sched_fork(). / Alternatively we could
5088 * use task_rq_lock() here and obtain the other rq->lock.
5089 *
5090 * Silence PROVE_RCU
5091 */
5092 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02005093 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005094 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 rq->curr = rq->idle = idle;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005097 idle->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02005098#ifdef CONFIG_SMP
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02005099 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07005100#endif
Peter Zijlstra25834c72015-05-15 17:43:34 +02005101 raw_spin_unlock(&rq->lock);
5102 raw_spin_unlock_irqrestore(&idle->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
5104 /* Set the preempt count _outside_ the spinlocks! */
Peter Zijlstra01028742013-08-14 14:55:46 +02005105 init_idle_preempt_count(idle, cpu);
Jonathan Corbet625f2a32011-04-22 11:19:10 -06005106
Ingo Molnardd41f592007-07-09 18:51:59 +02005107 /*
5108 * The idle tasks have their own, simple scheduling class:
5109 */
5110 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05005111 ftrace_graph_init_idle_task(idle, cpu);
Frederic Weisbecker45eacc62013-05-15 22:16:32 +02005112 vtime_init_idle(idle, cpu);
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02005113#ifdef CONFIG_SMP
Carsten Emdef1c6f1a2011-10-26 23:14:16 +02005114 sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
5115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116}
5117
Juri Lellif82f8042014-10-07 09:52:11 +01005118int cpuset_cpumask_can_shrink(const struct cpumask *cur,
5119 const struct cpumask *trial)
5120{
5121 int ret = 1, trial_cpus;
5122 struct dl_bw *cur_dl_b;
5123 unsigned long flags;
5124
Mike Galbraithbb2bc552015-01-28 04:53:55 +01005125 if (!cpumask_weight(cur))
5126 return ret;
5127
Juri Lelli75e23e492014-10-28 11:54:46 +00005128 rcu_read_lock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01005129 cur_dl_b = dl_bw_of(cpumask_any(cur));
5130 trial_cpus = cpumask_weight(trial);
5131
5132 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
5133 if (cur_dl_b->bw != -1 &&
5134 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
5135 ret = 0;
5136 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00005137 rcu_read_unlock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01005138
5139 return ret;
5140}
5141
Juri Lelli7f514122014-09-19 10:22:40 +01005142int task_can_attach(struct task_struct *p,
5143 const struct cpumask *cs_cpus_allowed)
5144{
5145 int ret = 0;
5146
5147 /*
5148 * Kthreads which disallow setaffinity shouldn't be moved
5149 * to a new cpuset; we don't want to change their cpu
5150 * affinity and isolating such threads by their set of
5151 * allowed nodes is unnecessary. Thus, cpusets are not
5152 * applicable for such threads. This prevents checking for
5153 * success of set_cpus_allowed_ptr() on all attached tasks
5154 * before cpus_allowed may be changed.
5155 */
5156 if (p->flags & PF_NO_SETAFFINITY) {
5157 ret = -EINVAL;
5158 goto out;
5159 }
5160
5161#ifdef CONFIG_SMP
5162 if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
5163 cs_cpus_allowed)) {
5164 unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
5165 cs_cpus_allowed);
Juri Lelli75e23e492014-10-28 11:54:46 +00005166 struct dl_bw *dl_b;
Juri Lelli7f514122014-09-19 10:22:40 +01005167 bool overflow;
5168 int cpus;
5169 unsigned long flags;
5170
Juri Lelli75e23e492014-10-28 11:54:46 +00005171 rcu_read_lock_sched();
5172 dl_b = dl_bw_of(dest_cpu);
Juri Lelli7f514122014-09-19 10:22:40 +01005173 raw_spin_lock_irqsave(&dl_b->lock, flags);
5174 cpus = dl_bw_cpus(dest_cpu);
5175 overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
5176 if (overflow)
5177 ret = -EBUSY;
5178 else {
5179 /*
5180 * We reserve space for this task in the destination
5181 * root_domain, as we can't fail after this point.
5182 * We will free resources in the source root_domain
5183 * later on (see set_cpus_allowed_dl()).
5184 */
5185 __dl_add(dl_b, p->dl.dl_bw);
5186 }
5187 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00005188 rcu_read_unlock_sched();
Juri Lelli7f514122014-09-19 10:22:40 +01005189
5190 }
5191#endif
5192out:
5193 return ret;
5194}
5195
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01005198static bool sched_smp_initialized __read_mostly;
5199
Mel Gormane6628d52013-10-07 11:29:02 +01005200#ifdef CONFIG_NUMA_BALANCING
5201/* Migrate current task p to target_cpu */
5202int migrate_task_to(struct task_struct *p, int target_cpu)
5203{
5204 struct migration_arg arg = { p, target_cpu };
5205 int curr_cpu = task_cpu(p);
5206
5207 if (curr_cpu == target_cpu)
5208 return 0;
5209
5210 if (!cpumask_test_cpu(target_cpu, tsk_cpus_allowed(p)))
5211 return -EINVAL;
5212
5213 /* TODO: This is not properly updating schedstats */
5214
Mel Gorman286549d2014-01-21 15:51:03 -08005215 trace_sched_move_numa(p, curr_cpu, target_cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01005216 return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
5217}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005218
5219/*
5220 * Requeue a task on a given node and accurately track the number of NUMA
5221 * tasks on the runqueues
5222 */
5223void sched_setnuma(struct task_struct *p, int nid)
5224{
5225 struct rq *rq;
5226 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005227 bool queued, running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005228
5229 rq = task_rq_lock(p, &flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005230 queued = task_on_rq_queued(p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005231 running = task_current(rq, p);
5232
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005233 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02005234 dequeue_task(rq, p, DEQUEUE_SAVE);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005235 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04005236 put_prev_task(rq, p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005237
5238 p->numa_preferred_nid = nid;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005239
5240 if (running)
5241 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005242 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02005243 enqueue_task(rq, p, ENQUEUE_RESTORE);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005244 task_rq_unlock(rq, p, &flags);
5245}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02005246#endif /* CONFIG_NUMA_BALANCING */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
5248#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar48f24c42006-07-03 00:25:40 -07005249/*
5250 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 * offline.
5252 */
5253void idle_task_exit(void)
5254{
5255 struct mm_struct *mm = current->active_mm;
5256
5257 BUG_ON(cpu_online(smp_processor_id()));
5258
Martin Schwidefskya53efe52012-10-26 17:17:44 +02005259 if (mm != &init_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 switch_mm(mm, &init_mm, current);
Martin Schwidefskya53efe52012-10-26 17:17:44 +02005261 finish_arch_post_lock_switch();
5262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 mmdrop(mm);
5264}
5265
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005266/*
Peter Zijlstra5d180232012-08-20 11:26:57 +02005267 * Since this CPU is going 'away' for a while, fold any nr_active delta
5268 * we might have. Assumes we're called after migrate_tasks() so that the
5269 * nr_active count is stable.
5270 *
5271 * Also see the comment "Global load-average calculations".
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005272 */
Peter Zijlstra5d180232012-08-20 11:26:57 +02005273static void calc_load_migrate(struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274{
Peter Zijlstra5d180232012-08-20 11:26:57 +02005275 long delta = calc_load_fold_active(rq);
5276 if (delta)
5277 atomic_long_add(delta, &calc_load_tasks);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005278}
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005279
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005280static void put_prev_task_fake(struct rq *rq, struct task_struct *prev)
5281{
5282}
5283
5284static const struct sched_class fake_sched_class = {
5285 .put_prev_task = put_prev_task_fake,
5286};
5287
5288static struct task_struct fake_task = {
5289 /*
5290 * Avoid pull_{rt,dl}_task()
5291 */
5292 .prio = MAX_PRIO + 1,
5293 .sched_class = &fake_sched_class,
5294};
5295
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005296/*
5297 * Migrate all tasks from the rq, sleeping tasks will be migrated by
5298 * try_to_wake_up()->select_task_rq().
5299 *
5300 * Called with rq->lock held even though we'er in stop_machine() and
5301 * there's no concurrency possible, we hold the required locks anyway
5302 * because of lock validation efforts.
5303 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005304static void migrate_tasks(struct rq *dead_rq)
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005305{
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005306 struct rq *rq = dead_rq;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005307 struct task_struct *next, *stop = rq->stop;
5308 int dest_cpu;
5309
5310 /*
5311 * Fudge the rq selection such that the below task selection loop
5312 * doesn't get stuck on the currently eligible stop task.
5313 *
5314 * We're currently inside stop_machine() and the rq is either stuck
5315 * in the stop_machine_cpu_stop() loop, or we're executing this code,
5316 * either way we should never end up calling schedule() until we're
5317 * done here.
5318 */
5319 rq->stop = NULL;
5320
Frederic Weisbecker77bd3972013-04-12 01:50:58 +02005321 /*
5322 * put_prev_task() and pick_next_task() sched
5323 * class method both need to have an up-to-date
5324 * value of rq->clock[_task]
5325 */
5326 update_rq_clock(rq);
5327
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005328 for (;;) {
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005329 /*
5330 * There's this thread running, bail when that's the only
5331 * remaining thread.
5332 */
5333 if (rq->nr_running == 1)
5334 break;
5335
Peter Zijlstracbce1a62015-06-11 14:46:54 +02005336 /*
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08005337 * pick_next_task assumes pinned rq->lock.
Peter Zijlstracbce1a62015-06-11 14:46:54 +02005338 */
5339 lockdep_pin_lock(&rq->lock);
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005340 next = pick_next_task(rq, &fake_task);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005341 BUG_ON(!next);
5342 next->sched_class->put_prev_task(rq, next);
5343
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08005344 /*
5345 * Rules for changing task_struct::cpus_allowed are holding
5346 * both pi_lock and rq->lock, such that holding either
5347 * stabilizes the mask.
5348 *
5349 * Drop rq->lock is not quite as disastrous as it usually is
5350 * because !cpu_active at this point, which means load-balance
5351 * will not interfere. Also, stop-machine.
5352 */
5353 lockdep_unpin_lock(&rq->lock);
5354 raw_spin_unlock(&rq->lock);
5355 raw_spin_lock(&next->pi_lock);
5356 raw_spin_lock(&rq->lock);
5357
5358 /*
5359 * Since we're inside stop-machine, _nothing_ should have
5360 * changed the task, WARN if weird stuff happened, because in
5361 * that case the above rq->lock drop is a fail too.
5362 */
5363 if (WARN_ON(task_rq(next) != rq || !task_on_rq_queued(next))) {
5364 raw_spin_unlock(&next->pi_lock);
5365 continue;
5366 }
5367
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005368 /* Find suitable destination for @next, with force if needed. */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005369 dest_cpu = select_fallback_rq(dead_rq->cpu, next);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005370
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005371 rq = __migrate_task(rq, next, dest_cpu);
5372 if (rq != dead_rq) {
5373 raw_spin_unlock(&rq->lock);
5374 rq = dead_rq;
5375 raw_spin_lock(&rq->lock);
5376 }
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08005377 raw_spin_unlock(&next->pi_lock);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005378 }
5379
5380 rq->stop = stop;
5381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382#endif /* CONFIG_HOTPLUG_CPU */
5383
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005384static void set_rq_online(struct rq *rq)
5385{
5386 if (!rq->online) {
5387 const struct sched_class *class;
5388
Rusty Russellc6c49272008-11-25 02:35:05 +10305389 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005390 rq->online = 1;
5391
5392 for_each_class(class) {
5393 if (class->rq_online)
5394 class->rq_online(rq);
5395 }
5396 }
5397}
5398
5399static void set_rq_offline(struct rq *rq)
5400{
5401 if (rq->online) {
5402 const struct sched_class *class;
5403
5404 for_each_class(class) {
5405 if (class->rq_offline)
5406 class->rq_offline(rq);
5407 }
5408
Rusty Russellc6c49272008-11-25 02:35:05 +10305409 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005410 rq->online = 0;
5411 }
5412}
5413
Thomas Gleixner9cf72432016-03-10 12:54:09 +01005414static void set_cpu_rq_start_time(unsigned int cpu)
Corey Minyarda803f022014-05-08 13:47:39 -05005415{
Corey Minyarda803f022014-05-08 13:47:39 -05005416 struct rq *rq = cpu_rq(cpu);
Thomas Gleixner9cf72432016-03-10 12:54:09 +01005417
Corey Minyarda803f022014-05-08 13:47:39 -05005418 rq->age_stamp = sched_clock_cpu(cpu);
5419}
5420
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005421static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
5422
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005423#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005424
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005425static __read_mostly int sched_debug_enabled;
Mike Travisf6630112009-11-17 18:22:15 -06005426
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005427static int __init sched_debug_setup(char *str)
Mike Travisf6630112009-11-17 18:22:15 -06005428{
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005429 sched_debug_enabled = 1;
Mike Travisf6630112009-11-17 18:22:15 -06005430
5431 return 0;
5432}
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005433early_param("sched_debug", sched_debug_setup);
5434
5435static inline bool sched_debug(void)
5436{
5437 return sched_debug_enabled;
5438}
Mike Travisf6630112009-11-17 18:22:15 -06005439
Mike Travis7c16ec52008-04-04 18:11:11 -07005440static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e22008-11-25 02:35:14 +10305441 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005442{
5443 struct sched_group *group = sd->groups;
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005444
Rusty Russell96f874e22008-11-25 02:35:14 +10305445 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005446
5447 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5448
5449 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005450 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005451 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005452 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5453 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005454 return -1;
5455 }
5456
Tejun Heo333470e2015-02-13 14:37:28 -08005457 printk(KERN_CONT "span %*pbl level %s\n",
5458 cpumask_pr_args(sched_domain_span(sd)), sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005459
Rusty Russell758b2cd2008-11-25 02:35:04 +10305460 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005461 printk(KERN_ERR "ERROR: domain->span does not contain "
5462 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005463 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10305464 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005465 printk(KERN_ERR "ERROR: domain->groups does not contain"
5466 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005467 }
5468
5469 printk(KERN_DEBUG "%*s groups:", level + 1, "");
5470 do {
5471 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005472 printk("\n");
5473 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005474 break;
5475 }
5476
Rusty Russell758b2cd2008-11-25 02:35:04 +10305477 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005478 printk(KERN_CONT "\n");
5479 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005480 break;
5481 }
5482
Peter Zijlstracb83b622012-04-17 15:49:36 +02005483 if (!(sd->flags & SD_OVERLAP) &&
5484 cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005485 printk(KERN_CONT "\n");
5486 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005487 break;
5488 }
5489
Rusty Russell758b2cd2008-11-25 02:35:04 +10305490 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005491
Tejun Heo333470e2015-02-13 14:37:28 -08005492 printk(KERN_CONT " %*pbl",
5493 cpumask_pr_args(sched_group_cpus(group)));
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005494 if (group->sgc->capacity != SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005495 printk(KERN_CONT " (cpu_capacity = %d)",
5496 group->sgc->capacity);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305497 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005498
5499 group = group->next;
5500 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005501 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005502
Rusty Russell758b2cd2008-11-25 02:35:04 +10305503 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005504 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005505
Rusty Russell758b2cd2008-11-25 02:35:04 +10305506 if (sd->parent &&
5507 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005508 printk(KERN_ERR "ERROR: parent span is not a superset "
5509 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005510 return 0;
5511}
5512
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513static void sched_domain_debug(struct sched_domain *sd, int cpu)
5514{
5515 int level = 0;
5516
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005517 if (!sched_debug_enabled)
Mike Travisf6630112009-11-17 18:22:15 -06005518 return;
5519
Nick Piggin41c7ce92005-06-25 14:57:24 -07005520 if (!sd) {
5521 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5522 return;
5523 }
5524
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5526
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005527 for (;;) {
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005528 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 level++;
5531 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005532 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005533 break;
5534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005536#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005537# define sched_domain_debug(sd, cpu) do { } while (0)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005538static inline bool sched_debug(void)
5539{
5540 return false;
5541}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005542#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005544static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005545{
Rusty Russell758b2cd2008-11-25 02:35:04 +10305546 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005547 return 1;
5548
5549 /* Following flags need at least 2 groups */
5550 if (sd->flags & (SD_LOAD_BALANCE |
5551 SD_BALANCE_NEWIDLE |
5552 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005553 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005554 SD_SHARE_CPUCAPACITY |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005555 SD_SHARE_PKG_RESOURCES |
5556 SD_SHARE_POWERDOMAIN)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005557 if (sd->groups != sd->groups->next)
5558 return 0;
5559 }
5560
5561 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005562 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005563 return 0;
5564
5565 return 1;
5566}
5567
Ingo Molnar48f24c42006-07-03 00:25:40 -07005568static int
5569sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005570{
5571 unsigned long cflags = sd->flags, pflags = parent->flags;
5572
5573 if (sd_degenerate(parent))
5574 return 1;
5575
Rusty Russell758b2cd2008-11-25 02:35:04 +10305576 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005577 return 0;
5578
Suresh Siddha245af2c2005-06-25 14:57:25 -07005579 /* Flags needing groups don't count if only 1 group in parent */
5580 if (parent->groups == parent->groups->next) {
5581 pflags &= ~(SD_LOAD_BALANCE |
5582 SD_BALANCE_NEWIDLE |
5583 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005584 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005585 SD_SHARE_CPUCAPACITY |
Peter Zijlstra10866e622013-08-19 16:57:04 +02005586 SD_SHARE_PKG_RESOURCES |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005587 SD_PREFER_SIBLING |
5588 SD_SHARE_POWERDOMAIN);
Ken Chen54364992008-12-07 18:47:37 -08005589 if (nr_node_ids == 1)
5590 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005591 }
5592 if (~cflags & pflags)
5593 return 0;
5594
5595 return 1;
5596}
5597
Peter Zijlstradce840a2011-04-07 14:09:50 +02005598static void free_rootdomain(struct rcu_head *rcu)
Rusty Russellc6c49272008-11-25 02:35:05 +10305599{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005600 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
Peter Zijlstra047106a2009-11-16 10:28:09 +01005601
Rusty Russell68e74562008-11-25 02:35:13 +10305602 cpupri_cleanup(&rd->cpupri);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005603 cpudl_cleanup(&rd->cpudl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005604 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305605 free_cpumask_var(rd->rto_mask);
5606 free_cpumask_var(rd->online);
5607 free_cpumask_var(rd->span);
5608 kfree(rd);
5609}
5610
Gregory Haskins57d885f2008-01-25 21:08:18 +01005611static void rq_attach_root(struct rq *rq, struct root_domain *rd)
5612{
Ingo Molnara0490fa2009-02-12 11:35:40 +01005613 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005614 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005615
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005616 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005617
5618 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01005619 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005620
Rusty Russellc6c49272008-11-25 02:35:05 +10305621 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005622 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005623
Rusty Russellc6c49272008-11-25 02:35:05 +10305624 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01005625
Ingo Molnara0490fa2009-02-12 11:35:40 +01005626 /*
Shigeru Yoshida05159732013-11-17 12:12:36 +09005627 * If we dont want to free the old_rd yet then
Ingo Molnara0490fa2009-02-12 11:35:40 +01005628 * set old_rd to NULL to skip the freeing later
5629 * in this function:
5630 */
5631 if (!atomic_dec_and_test(&old_rd->refcount))
5632 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005633 }
5634
5635 atomic_inc(&rd->refcount);
5636 rq->rd = rd;
5637
Rusty Russellc6c49272008-11-25 02:35:05 +10305638 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04005639 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005640 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005641
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005642 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01005643
5644 if (old_rd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02005645 call_rcu_sched(&old_rd->rcu, free_rootdomain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005646}
5647
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005648static int init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005649{
5650 memset(rd, 0, sizeof(*rd));
5651
Xunlei Pang8295c692015-12-02 19:52:59 +08005652 if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08005653 goto out;
Xunlei Pang8295c692015-12-02 19:52:59 +08005654 if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305655 goto free_span;
Xunlei Pang8295c692015-12-02 19:52:59 +08005656 if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305657 goto free_online;
Xunlei Pang8295c692015-12-02 19:52:59 +08005658 if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
Juri Lelli1baca4c2013-11-07 14:43:38 +01005659 goto free_dlo_mask;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02005660
Dario Faggioli332ac172013-11-07 14:43:45 +01005661 init_dl_bw(&rd->dl_bw);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005662 if (cpudl_init(&rd->cpudl) != 0)
5663 goto free_dlo_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01005664
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005665 if (cpupri_init(&rd->cpupri) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10305666 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10305667 return 0;
5668
Rusty Russell68e74562008-11-25 02:35:13 +10305669free_rto_mask:
5670 free_cpumask_var(rd->rto_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005671free_dlo_mask:
5672 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305673free_online:
5674 free_cpumask_var(rd->online);
5675free_span:
5676 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08005677out:
Rusty Russellc6c49272008-11-25 02:35:05 +10305678 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005679}
5680
Peter Zijlstra029632f2011-10-25 10:00:11 +02005681/*
5682 * By default the system creates a single root-domain with all cpus as
5683 * members (mimicking the global state we have today).
5684 */
5685struct root_domain def_root_domain;
5686
Gregory Haskins57d885f2008-01-25 21:08:18 +01005687static void init_defrootdomain(void)
5688{
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005689 init_rootdomain(&def_root_domain);
Rusty Russellc6c49272008-11-25 02:35:05 +10305690
Gregory Haskins57d885f2008-01-25 21:08:18 +01005691 atomic_set(&def_root_domain.refcount, 1);
5692}
5693
Gregory Haskinsdc938522008-01-25 21:08:26 +01005694static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005695{
5696 struct root_domain *rd;
5697
5698 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
5699 if (!rd)
5700 return NULL;
5701
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005702 if (init_rootdomain(rd) != 0) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305703 kfree(rd);
5704 return NULL;
5705 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01005706
5707 return rd;
5708}
5709
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005710static void free_sched_groups(struct sched_group *sg, int free_sgc)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005711{
5712 struct sched_group *tmp, *first;
5713
5714 if (!sg)
5715 return;
5716
5717 first = sg;
5718 do {
5719 tmp = sg->next;
5720
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005721 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
5722 kfree(sg->sgc);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005723
5724 kfree(sg);
5725 sg = tmp;
5726 } while (sg != first);
5727}
5728
Peter Zijlstradce840a2011-04-07 14:09:50 +02005729static void free_sched_domain(struct rcu_head *rcu)
5730{
5731 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005732
5733 /*
5734 * If its an overlapping domain it has private groups, iterate and
5735 * nuke them all.
5736 */
5737 if (sd->flags & SD_OVERLAP) {
5738 free_sched_groups(sd->groups, 1);
5739 } else if (atomic_dec_and_test(&sd->groups->ref)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005740 kfree(sd->groups->sgc);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005741 kfree(sd->groups);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005742 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005743 kfree(sd);
5744}
5745
5746static void destroy_sched_domain(struct sched_domain *sd, int cpu)
5747{
5748 call_rcu(&sd->rcu, free_sched_domain);
5749}
5750
5751static void destroy_sched_domains(struct sched_domain *sd, int cpu)
5752{
5753 for (; sd; sd = sd->parent)
5754 destroy_sched_domain(sd, cpu);
5755}
5756
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757/*
Peter Zijlstra518cd622011-12-07 15:07:31 +01005758 * Keep a special pointer to the highest sched_domain that has
5759 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
5760 * allows us to avoid some pointer chasing select_idle_sibling().
5761 *
5762 * Also keep a unique ID per domain (we use the first cpu number in
5763 * the cpumask of the domain), this allows us to quickly tell if
Peter Zijlstra39be3502012-01-26 12:44:34 +01005764 * two cpus are in the same cache domain, see cpus_share_cache().
Peter Zijlstra518cd622011-12-07 15:07:31 +01005765 */
5766DEFINE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005767DEFINE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005768DEFINE_PER_CPU(int, sd_llc_id);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005769DEFINE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305770DEFINE_PER_CPU(struct sched_domain *, sd_busy);
5771DEFINE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005772
5773static void update_top_cache_domain(int cpu)
5774{
5775 struct sched_domain *sd;
Mel Gorman5d4cf992013-12-17 09:21:25 +00005776 struct sched_domain *busy_sd = NULL;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005777 int id = cpu;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005778 int size = 1;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005779
5780 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005781 if (sd) {
Peter Zijlstra518cd622011-12-07 15:07:31 +01005782 id = cpumask_first(sched_domain_span(sd));
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005783 size = cpumask_weight(sched_domain_span(sd));
Mel Gorman5d4cf992013-12-17 09:21:25 +00005784 busy_sd = sd->parent; /* sd_busy */
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005785 }
Mel Gorman5d4cf992013-12-17 09:21:25 +00005786 rcu_assign_pointer(per_cpu(sd_busy, cpu), busy_sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005787
5788 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005789 per_cpu(sd_llc_size, cpu) = size;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005790 per_cpu(sd_llc_id, cpu) = id;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005791
5792 sd = lowest_flag_domain(cpu, SD_NUMA);
5793 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305794
5795 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
5796 rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005797}
5798
5799/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01005800 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 * hold the hotplug lock.
5802 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01005803static void
5804cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005806 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005807 struct sched_domain *tmp;
5808
5809 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08005810 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005811 struct sched_domain *parent = tmp->parent;
5812 if (!parent)
5813 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08005814
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005815 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005816 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005817 if (parent->parent)
5818 parent->parent->child = tmp;
Peter Zijlstra10866e622013-08-19 16:57:04 +02005819 /*
5820 * Transfer SD_PREFER_SIBLING down in case of a
5821 * degenerate parent; the spans match for this
5822 * so the property transfers.
5823 */
5824 if (parent->flags & SD_PREFER_SIBLING)
5825 tmp->flags |= SD_PREFER_SIBLING;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005826 destroy_sched_domain(parent, cpu);
Li Zefanf29c9b12008-11-06 09:45:16 +08005827 } else
5828 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005829 }
5830
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005831 if (sd && sd_degenerate(sd)) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02005832 tmp = sd;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005833 sd = sd->parent;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005834 destroy_sched_domain(tmp, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005835 if (sd)
5836 sd->child = NULL;
5837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005839 sched_domain_debug(sd, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
Gregory Haskins57d885f2008-01-25 21:08:18 +01005841 rq_attach_root(rq, rd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005842 tmp = rq->sd;
Nick Piggin674311d2005-06-25 14:57:27 -07005843 rcu_assign_pointer(rq->sd, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005844 destroy_sched_domains(tmp, cpu);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005845
5846 update_top_cache_domain(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847}
5848
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849/* Setup the mask of cpus configured for isolated domains */
5850static int __init isolated_cpu_setup(char *str)
5851{
Prarit Bhargavaa6e44912016-02-04 09:38:00 -05005852 int ret;
5853
Rusty Russellbdddd292009-12-02 14:09:16 +10305854 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Prarit Bhargavaa6e44912016-02-04 09:38:00 -05005855 ret = cpulist_parse(str, cpu_isolated_map);
5856 if (ret) {
5857 pr_err("sched: Error, all isolcpus= values must be between 0 and %d\n", nr_cpu_ids);
5858 return 0;
5859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 return 1;
5861}
Ingo Molnar8927f492007-10-15 17:00:13 +02005862__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
Andreas Herrmann49a02c52009-08-18 12:51:52 +02005864struct s_data {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02005865 struct sched_domain ** __percpu sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02005866 struct root_domain *rd;
5867};
5868
Andreas Herrmann2109b992009-08-18 12:53:00 +02005869enum s_alloc {
Andreas Herrmann2109b992009-08-18 12:53:00 +02005870 sa_rootdomain,
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02005871 sa_sd,
Peter Zijlstradce840a2011-04-07 14:09:50 +02005872 sa_sd_storage,
Andreas Herrmann2109b992009-08-18 12:53:00 +02005873 sa_none,
5874};
5875
Peter Zijlstrac1174872012-05-31 14:47:33 +02005876/*
5877 * Build an iteration mask that can exclude certain CPUs from the upwards
5878 * domain traversal.
5879 *
5880 * Asymmetric node setups can result in situations where the domain tree is of
5881 * unequal depth, make sure to skip domains that already cover the entire
5882 * range.
5883 *
5884 * In that case build_sched_domains() will have terminated the iteration early
5885 * and our sibling sd spans will be empty. Domains should always include the
5886 * cpu they're built on, so check that.
5887 *
5888 */
5889static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
5890{
5891 const struct cpumask *span = sched_domain_span(sd);
5892 struct sd_data *sdd = sd->private;
5893 struct sched_domain *sibling;
5894 int i;
5895
5896 for_each_cpu(i, span) {
5897 sibling = *per_cpu_ptr(sdd->sd, i);
5898 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
5899 continue;
5900
5901 cpumask_set_cpu(i, sched_group_mask(sg));
5902 }
5903}
5904
5905/*
5906 * Return the canonical balance cpu for this group, this is the first cpu
5907 * of this group that's also in the iteration mask.
5908 */
5909int group_balance_cpu(struct sched_group *sg)
5910{
5911 return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
5912}
5913
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005914static int
5915build_overlap_sched_groups(struct sched_domain *sd, int cpu)
5916{
5917 struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
5918 const struct cpumask *span = sched_domain_span(sd);
5919 struct cpumask *covered = sched_domains_tmpmask;
5920 struct sd_data *sdd = sd->private;
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005921 struct sched_domain *sibling;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005922 int i;
5923
5924 cpumask_clear(covered);
5925
5926 for_each_cpu(i, span) {
5927 struct cpumask *sg_span;
5928
5929 if (cpumask_test_cpu(i, covered))
5930 continue;
5931
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005932 sibling = *per_cpu_ptr(sdd->sd, i);
Peter Zijlstrac1174872012-05-31 14:47:33 +02005933
5934 /* See the comment near build_group_mask(). */
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005935 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
Peter Zijlstrac1174872012-05-31 14:47:33 +02005936 continue;
5937
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005938 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
Suresh Siddha4d78a222011-11-18 15:03:29 -08005939 GFP_KERNEL, cpu_to_node(cpu));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005940
5941 if (!sg)
5942 goto fail;
5943
5944 sg_span = sched_group_cpus(sg);
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005945 if (sibling->child)
5946 cpumask_copy(sg_span, sched_domain_span(sibling->child));
5947 else
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005948 cpumask_set_cpu(i, sg_span);
5949
5950 cpumask_or(covered, covered, sg_span);
5951
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005952 sg->sgc = *per_cpu_ptr(sdd->sgc, i);
5953 if (atomic_inc_return(&sg->sgc->ref) == 1)
Peter Zijlstrac1174872012-05-31 14:47:33 +02005954 build_group_mask(sd, sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005955
Peter Zijlstrac3decf02012-05-31 12:05:32 +02005956 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005957 * Initialize sgc->capacity such that even if we mess up the
Peter Zijlstrac3decf02012-05-31 12:05:32 +02005958 * domains and no possible iteration will get us here, we won't
5959 * die on a /0 trap.
5960 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005961 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
Peter Zijlstrac1174872012-05-31 14:47:33 +02005962
5963 /*
5964 * Make sure the first group of this domain contains the
5965 * canonical balance cpu. Otherwise the sched_domain iteration
5966 * breaks. See update_sg_lb_stats().
5967 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02005968 if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
Peter Zijlstrac1174872012-05-31 14:47:33 +02005969 group_balance_cpu(sg) == cpu)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005970 groups = sg;
5971
5972 if (!first)
5973 first = sg;
5974 if (last)
5975 last->next = sg;
5976 last = sg;
5977 last->next = first;
5978 }
5979 sd->groups = groups;
5980
5981 return 0;
5982
5983fail:
5984 free_sched_groups(first, 0);
5985
5986 return -ENOMEM;
5987}
5988
Peter Zijlstradce840a2011-04-07 14:09:50 +02005989static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005991 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
5992 struct sched_domain *child = sd->child;
5993
5994 if (child)
5995 cpu = cpumask_first(sched_domain_span(child));
5996
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005997 if (sg) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02005998 *sg = *per_cpu_ptr(sdd->sg, cpu);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005999 (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
6000 atomic_set(&(*sg)->sgc->ref, 1); /* for claim_allocations */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006001 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006002
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 return cpu;
6004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Ingo Molnar48f24c42006-07-03 00:25:40 -07006006/*
Peter Zijlstradce840a2011-04-07 14:09:50 +02006007 * build_sched_groups will build a circular linked list of the groups
6008 * covered by the given span, and will set each group's ->cpumask correctly,
Nicolas Pitreced549f2014-05-26 18:19:38 -04006009 * and ->cpu_capacity to 0.
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006010 *
6011 * Assumes the sched_domain tree is fully constructed
Ingo Molnar48f24c42006-07-03 00:25:40 -07006012 */
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006013static int
6014build_sched_groups(struct sched_domain *sd, int cpu)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006015{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006016 struct sched_group *first = NULL, *last = NULL;
6017 struct sd_data *sdd = sd->private;
6018 const struct cpumask *span = sched_domain_span(sd);
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006019 struct cpumask *covered;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006020 int i;
6021
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006022 get_group(cpu, sdd, &sd->groups);
6023 atomic_inc(&sd->groups->ref);
6024
Viresh Kumar09366292013-06-11 16:32:43 +05306025 if (cpu != cpumask_first(span))
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006026 return 0;
6027
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006028 lockdep_assert_held(&sched_domains_mutex);
6029 covered = sched_domains_tmpmask;
6030
Peter Zijlstradce840a2011-04-07 14:09:50 +02006031 cpumask_clear(covered);
6032
6033 for_each_cpu(i, span) {
6034 struct sched_group *sg;
Viresh Kumarcd08e922013-06-11 16:32:44 +05306035 int group, j;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006036
6037 if (cpumask_test_cpu(i, covered))
6038 continue;
6039
Viresh Kumarcd08e922013-06-11 16:32:44 +05306040 group = get_group(i, sdd, &sg);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006041 cpumask_setall(sched_group_mask(sg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02006042
6043 for_each_cpu(j, span) {
6044 if (get_group(j, sdd, NULL) != group)
6045 continue;
6046
6047 cpumask_set_cpu(j, covered);
6048 cpumask_set_cpu(j, sched_group_cpus(sg));
6049 }
6050
6051 if (!first)
6052 first = sg;
6053 if (last)
6054 last->next = sg;
6055 last = sg;
6056 }
6057 last->next = first;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006058
6059 return 0;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006060}
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006061
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062/*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006063 * Initialize sched groups cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006064 *
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006065 * cpu_capacity indicates the capacity of sched group, which is used while
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006066 * distributing the load between different sched groups in a sched domain.
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006067 * Typically cpu_capacity for all the groups in a sched domain will be same
6068 * unless there are asymmetries in the topology. If there are asymmetries,
6069 * group having more cpu_capacity will pickup more load compared to the
6070 * group having less cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006071 */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006072static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006073{
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006074 struct sched_group *sg = sd->groups;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006075
Viresh Kumar94c95ba2013-06-11 16:32:45 +05306076 WARN_ON(!sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006077
6078 do {
6079 sg->group_weight = cpumask_weight(sched_group_cpus(sg));
6080 sg = sg->next;
6081 } while (sg != sd->groups);
6082
Peter Zijlstrac1174872012-05-31 14:47:33 +02006083 if (cpu != group_balance_cpu(sg))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006084 return;
6085
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006086 update_group_capacity(sd, cpu);
6087 atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006088}
6089
6090/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006091 * Initializers for schedule domains
6092 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6093 */
6094
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006095static int default_relax_domain_level = -1;
Peter Zijlstra60495e72011-04-07 14:10:04 +02006096int sched_domain_level_max;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006097
6098static int __init setup_relax_domain_level(char *str)
6099{
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006100 if (kstrtoint(str, 0, &default_relax_domain_level))
6101 pr_warn("Unable to set relax_domain_level\n");
Li Zefan30e0e172008-05-13 10:27:17 +08006102
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006103 return 1;
6104}
6105__setup("relax_domain_level=", setup_relax_domain_level);
6106
6107static void set_domain_attribute(struct sched_domain *sd,
6108 struct sched_domain_attr *attr)
6109{
6110 int request;
6111
6112 if (!attr || attr->relax_domain_level < 0) {
6113 if (default_relax_domain_level < 0)
6114 return;
6115 else
6116 request = default_relax_domain_level;
6117 } else
6118 request = attr->relax_domain_level;
6119 if (request < sd->level) {
6120 /* turn off idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006121 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006122 } else {
6123 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006124 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006125 }
6126}
6127
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006128static void __sdt_free(const struct cpumask *cpu_map);
6129static int __sdt_alloc(const struct cpumask *cpu_map);
6130
Andreas Herrmann2109b992009-08-18 12:53:00 +02006131static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
6132 const struct cpumask *cpu_map)
6133{
6134 switch (what) {
Andreas Herrmann2109b992009-08-18 12:53:00 +02006135 case sa_rootdomain:
Peter Zijlstra822ff792011-04-07 14:09:51 +02006136 if (!atomic_read(&d->rd->refcount))
6137 free_rootdomain(&d->rd->rcu); /* fall through */
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006138 case sa_sd:
6139 free_percpu(d->sd); /* fall through */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006140 case sa_sd_storage:
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006141 __sdt_free(cpu_map); /* fall through */
Andreas Herrmann2109b992009-08-18 12:53:00 +02006142 case sa_none:
6143 break;
6144 }
6145}
6146
6147static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
6148 const struct cpumask *cpu_map)
6149{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006150 memset(d, 0, sizeof(*d));
6151
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006152 if (__sdt_alloc(cpu_map))
6153 return sa_sd_storage;
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006154 d->sd = alloc_percpu(struct sched_domain *);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006155 if (!d->sd)
6156 return sa_sd_storage;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006157 d->rd = alloc_rootdomain();
Peter Zijlstradce840a2011-04-07 14:09:50 +02006158 if (!d->rd)
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006159 return sa_sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006160 return sa_rootdomain;
6161}
6162
Peter Zijlstradce840a2011-04-07 14:09:50 +02006163/*
6164 * NULL the sd_data elements we've used to build the sched_domain and
6165 * sched_group structure so that the subsequent __free_domain_allocs()
6166 * will not free the data we're using.
6167 */
6168static void claim_allocations(int cpu, struct sched_domain *sd)
6169{
6170 struct sd_data *sdd = sd->private;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006171
6172 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
6173 *per_cpu_ptr(sdd->sd, cpu) = NULL;
6174
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006175 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
Peter Zijlstradce840a2011-04-07 14:09:50 +02006176 *per_cpu_ptr(sdd->sg, cpu) = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006177
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006178 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
6179 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006180}
6181
Peter Zijlstracb83b622012-04-17 15:49:36 +02006182#ifdef CONFIG_NUMA
Peter Zijlstracb83b622012-04-17 15:49:36 +02006183static int sched_domains_numa_levels;
Rik van Riele3fe70b2014-10-17 03:29:50 -04006184enum numa_topology_type sched_numa_topology_type;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006185static int *sched_domains_numa_distance;
Rik van Riel9942f792014-10-17 03:29:49 -04006186int sched_max_numa_distance;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006187static struct cpumask ***sched_domains_numa_masks;
6188static int sched_domains_curr_level;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006189#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006190
Vincent Guittot143e1e22014-04-11 11:44:37 +02006191/*
6192 * SD_flags allowed in topology descriptions.
6193 *
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006194 * SD_SHARE_CPUCAPACITY - describes SMT topologies
Vincent Guittot143e1e22014-04-11 11:44:37 +02006195 * SD_SHARE_PKG_RESOURCES - describes shared caches
6196 * SD_NUMA - describes NUMA topologies
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006197 * SD_SHARE_POWERDOMAIN - describes shared power domain
Vincent Guittot143e1e22014-04-11 11:44:37 +02006198 *
6199 * Odd one out:
6200 * SD_ASYM_PACKING - describes SMT quirks
6201 */
6202#define TOPOLOGY_SD_FLAGS \
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006203 (SD_SHARE_CPUCAPACITY | \
Vincent Guittot143e1e22014-04-11 11:44:37 +02006204 SD_SHARE_PKG_RESOURCES | \
6205 SD_NUMA | \
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006206 SD_ASYM_PACKING | \
6207 SD_SHARE_POWERDOMAIN)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006208
6209static struct sched_domain *
Vincent Guittot143e1e22014-04-11 11:44:37 +02006210sd_init(struct sched_domain_topology_level *tl, int cpu)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006211{
6212 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
Vincent Guittot143e1e22014-04-11 11:44:37 +02006213 int sd_weight, sd_flags = 0;
6214
6215#ifdef CONFIG_NUMA
6216 /*
6217 * Ugly hack to pass state to sd_numa_mask()...
6218 */
6219 sched_domains_curr_level = tl->numa_level;
6220#endif
6221
6222 sd_weight = cpumask_weight(tl->mask(cpu));
6223
6224 if (tl->sd_flags)
6225 sd_flags = (*tl->sd_flags)();
6226 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
6227 "wrong sd_flags in topology description\n"))
6228 sd_flags &= ~TOPOLOGY_SD_FLAGS;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006229
6230 *sd = (struct sched_domain){
6231 .min_interval = sd_weight,
6232 .max_interval = 2*sd_weight,
6233 .busy_factor = 32,
Peter Zijlstra870a0bb2012-05-11 00:26:27 +02006234 .imbalance_pct = 125,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006235
6236 .cache_nice_tries = 0,
6237 .busy_idx = 0,
6238 .idle_idx = 0,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006239 .newidle_idx = 0,
6240 .wake_idx = 0,
6241 .forkexec_idx = 0,
6242
6243 .flags = 1*SD_LOAD_BALANCE
6244 | 1*SD_BALANCE_NEWIDLE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006245 | 1*SD_BALANCE_EXEC
6246 | 1*SD_BALANCE_FORK
Peter Zijlstracb83b622012-04-17 15:49:36 +02006247 | 0*SD_BALANCE_WAKE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006248 | 1*SD_WAKE_AFFINE
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006249 | 0*SD_SHARE_CPUCAPACITY
Peter Zijlstracb83b622012-04-17 15:49:36 +02006250 | 0*SD_SHARE_PKG_RESOURCES
Vincent Guittot143e1e22014-04-11 11:44:37 +02006251 | 0*SD_SERIALIZE
Peter Zijlstracb83b622012-04-17 15:49:36 +02006252 | 0*SD_PREFER_SIBLING
Vincent Guittot143e1e22014-04-11 11:44:37 +02006253 | 0*SD_NUMA
6254 | sd_flags
Peter Zijlstracb83b622012-04-17 15:49:36 +02006255 ,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006256
Peter Zijlstracb83b622012-04-17 15:49:36 +02006257 .last_balance = jiffies,
6258 .balance_interval = sd_weight,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006259 .smt_gain = 0,
Jason Low2b4cfe62014-04-23 18:30:34 -07006260 .max_newidle_lb_cost = 0,
6261 .next_decay_max_lb_cost = jiffies,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006262#ifdef CONFIG_SCHED_DEBUG
6263 .name = tl->name,
6264#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006265 };
Peter Zijlstracb83b622012-04-17 15:49:36 +02006266
6267 /*
Vincent Guittot143e1e22014-04-11 11:44:37 +02006268 * Convert topological properties into behaviour.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006269 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006270
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006271 if (sd->flags & SD_SHARE_CPUCAPACITY) {
Vincent Guittotcaff37e2015-02-27 16:54:13 +01006272 sd->flags |= SD_PREFER_SIBLING;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006273 sd->imbalance_pct = 110;
6274 sd->smt_gain = 1178; /* ~15% */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006275
6276 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
6277 sd->imbalance_pct = 117;
6278 sd->cache_nice_tries = 1;
6279 sd->busy_idx = 2;
6280
6281#ifdef CONFIG_NUMA
6282 } else if (sd->flags & SD_NUMA) {
6283 sd->cache_nice_tries = 2;
6284 sd->busy_idx = 3;
6285 sd->idle_idx = 2;
6286
6287 sd->flags |= SD_SERIALIZE;
6288 if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
6289 sd->flags &= ~(SD_BALANCE_EXEC |
6290 SD_BALANCE_FORK |
6291 SD_WAKE_AFFINE);
6292 }
6293
6294#endif
6295 } else {
6296 sd->flags |= SD_PREFER_SIBLING;
6297 sd->cache_nice_tries = 1;
6298 sd->busy_idx = 2;
6299 sd->idle_idx = 1;
6300 }
6301
6302 sd->private = &tl->data;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006303
6304 return sd;
6305}
6306
Vincent Guittot143e1e22014-04-11 11:44:37 +02006307/*
6308 * Topology list, bottom-up.
6309 */
6310static struct sched_domain_topology_level default_topology[] = {
6311#ifdef CONFIG_SCHED_SMT
6312 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
6313#endif
6314#ifdef CONFIG_SCHED_MC
6315 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
6316#endif
Vincent Guittot143e1e22014-04-11 11:44:37 +02006317 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
6318 { NULL, },
6319};
6320
Juergen Grossc6e1e7b2015-09-22 12:48:59 +02006321static struct sched_domain_topology_level *sched_domain_topology =
6322 default_topology;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006323
6324#define for_each_sd_topology(tl) \
6325 for (tl = sched_domain_topology; tl->mask; tl++)
6326
6327void set_sched_topology(struct sched_domain_topology_level *tl)
6328{
6329 sched_domain_topology = tl;
6330}
6331
6332#ifdef CONFIG_NUMA
6333
Peter Zijlstracb83b622012-04-17 15:49:36 +02006334static const struct cpumask *sd_numa_mask(int cpu)
6335{
6336 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
6337}
6338
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006339static void sched_numa_warn(const char *str)
6340{
6341 static int done = false;
6342 int i,j;
6343
6344 if (done)
6345 return;
6346
6347 done = true;
6348
6349 printk(KERN_WARNING "ERROR: %s\n\n", str);
6350
6351 for (i = 0; i < nr_node_ids; i++) {
6352 printk(KERN_WARNING " ");
6353 for (j = 0; j < nr_node_ids; j++)
6354 printk(KERN_CONT "%02d ", node_distance(i,j));
6355 printk(KERN_CONT "\n");
6356 }
6357 printk(KERN_WARNING "\n");
6358}
6359
Rik van Riel9942f792014-10-17 03:29:49 -04006360bool find_numa_distance(int distance)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006361{
6362 int i;
6363
6364 if (distance == node_distance(0, 0))
6365 return true;
6366
6367 for (i = 0; i < sched_domains_numa_levels; i++) {
6368 if (sched_domains_numa_distance[i] == distance)
6369 return true;
6370 }
6371
6372 return false;
6373}
6374
Rik van Riele3fe70b2014-10-17 03:29:50 -04006375/*
6376 * A system can have three types of NUMA topology:
6377 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
6378 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
6379 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
6380 *
6381 * The difference between a glueless mesh topology and a backplane
6382 * topology lies in whether communication between not directly
6383 * connected nodes goes through intermediary nodes (where programs
6384 * could run), or through backplane controllers. This affects
6385 * placement of programs.
6386 *
6387 * The type of topology can be discerned with the following tests:
6388 * - If the maximum distance between any nodes is 1 hop, the system
6389 * is directly connected.
6390 * - If for two nodes A and B, located N > 1 hops away from each other,
6391 * there is an intermediary node C, which is < N hops away from both
6392 * nodes A and B, the system is a glueless mesh.
6393 */
6394static void init_numa_topology_type(void)
6395{
6396 int a, b, c, n;
6397
6398 n = sched_max_numa_distance;
6399
Aravind Gopalakrishnane2378822015-08-10 20:20:48 -05006400 if (sched_domains_numa_levels <= 1) {
Rik van Riele3fe70b2014-10-17 03:29:50 -04006401 sched_numa_topology_type = NUMA_DIRECT;
Aravind Gopalakrishnane2378822015-08-10 20:20:48 -05006402 return;
6403 }
Rik van Riele3fe70b2014-10-17 03:29:50 -04006404
6405 for_each_online_node(a) {
6406 for_each_online_node(b) {
6407 /* Find two nodes furthest removed from each other. */
6408 if (node_distance(a, b) < n)
6409 continue;
6410
6411 /* Is there an intermediary node between a and b? */
6412 for_each_online_node(c) {
6413 if (node_distance(a, c) < n &&
6414 node_distance(b, c) < n) {
6415 sched_numa_topology_type =
6416 NUMA_GLUELESS_MESH;
6417 return;
6418 }
6419 }
6420
6421 sched_numa_topology_type = NUMA_BACKPLANE;
6422 return;
6423 }
6424 }
6425}
6426
Peter Zijlstracb83b622012-04-17 15:49:36 +02006427static void sched_init_numa(void)
6428{
6429 int next_distance, curr_distance = node_distance(0, 0);
6430 struct sched_domain_topology_level *tl;
6431 int level = 0;
6432 int i, j, k;
6433
Peter Zijlstracb83b622012-04-17 15:49:36 +02006434 sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
6435 if (!sched_domains_numa_distance)
6436 return;
6437
6438 /*
6439 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
6440 * unique distances in the node_distance() table.
6441 *
6442 * Assumes node_distance(0,j) includes all distances in
6443 * node_distance(i,j) in order to avoid cubic time.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006444 */
6445 next_distance = curr_distance;
6446 for (i = 0; i < nr_node_ids; i++) {
6447 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006448 for (k = 0; k < nr_node_ids; k++) {
6449 int distance = node_distance(i, k);
6450
6451 if (distance > curr_distance &&
6452 (distance < next_distance ||
6453 next_distance == curr_distance))
6454 next_distance = distance;
6455
6456 /*
6457 * While not a strong assumption it would be nice to know
6458 * about cases where if node A is connected to B, B is not
6459 * equally connected to A.
6460 */
6461 if (sched_debug() && node_distance(k, i) != distance)
6462 sched_numa_warn("Node-distance not symmetric");
6463
6464 if (sched_debug() && i && !find_numa_distance(distance))
6465 sched_numa_warn("Node-0 not representative");
6466 }
6467 if (next_distance != curr_distance) {
6468 sched_domains_numa_distance[level++] = next_distance;
6469 sched_domains_numa_levels = level;
6470 curr_distance = next_distance;
6471 } else break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006472 }
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006473
6474 /*
6475 * In case of sched_debug() we verify the above assumption.
6476 */
6477 if (!sched_debug())
6478 break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006479 }
Andrey Ryabininc1235882014-11-07 17:53:40 +03006480
6481 if (!level)
6482 return;
6483
Peter Zijlstracb83b622012-04-17 15:49:36 +02006484 /*
6485 * 'level' contains the number of unique distances, excluding the
6486 * identity distance node_distance(i,i).
6487 *
Viresh Kumar28b4a522013-04-05 16:26:46 +05306488 * The sched_domains_numa_distance[] array includes the actual distance
Peter Zijlstracb83b622012-04-17 15:49:36 +02006489 * numbers.
6490 */
6491
Tang Chen5f7865f2012-09-25 21:12:30 +08006492 /*
6493 * Here, we should temporarily reset sched_domains_numa_levels to 0.
6494 * If it fails to allocate memory for array sched_domains_numa_masks[][],
6495 * the array will contain less then 'level' members. This could be
6496 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
6497 * in other functions.
6498 *
6499 * We reset it to 'level' at the end of this function.
6500 */
6501 sched_domains_numa_levels = 0;
6502
Peter Zijlstracb83b622012-04-17 15:49:36 +02006503 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
6504 if (!sched_domains_numa_masks)
6505 return;
6506
6507 /*
6508 * Now for each level, construct a mask per node which contains all
6509 * cpus of nodes that are that many hops away from us.
6510 */
6511 for (i = 0; i < level; i++) {
6512 sched_domains_numa_masks[i] =
6513 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
6514 if (!sched_domains_numa_masks[i])
6515 return;
6516
6517 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstra2ea45802012-05-25 09:26:43 +02006518 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
Peter Zijlstracb83b622012-04-17 15:49:36 +02006519 if (!mask)
6520 return;
6521
6522 sched_domains_numa_masks[i][j] = mask;
6523
Raghavendra K T9c03ee12016-01-16 00:31:23 +05306524 for_each_node(k) {
Peter Zijlstradd7d8632012-05-11 00:56:20 +02006525 if (node_distance(j, k) > sched_domains_numa_distance[i])
Peter Zijlstracb83b622012-04-17 15:49:36 +02006526 continue;
6527
6528 cpumask_or(mask, mask, cpumask_of_node(k));
6529 }
6530 }
6531 }
6532
Vincent Guittot143e1e22014-04-11 11:44:37 +02006533 /* Compute default topology size */
6534 for (i = 0; sched_domain_topology[i].mask; i++);
6535
Vincent Guittotc515db82014-05-13 11:11:01 +02006536 tl = kzalloc((i + level + 1) *
Peter Zijlstracb83b622012-04-17 15:49:36 +02006537 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
6538 if (!tl)
6539 return;
6540
6541 /*
6542 * Copy the default topology bits..
6543 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006544 for (i = 0; sched_domain_topology[i].mask; i++)
6545 tl[i] = sched_domain_topology[i];
Peter Zijlstracb83b622012-04-17 15:49:36 +02006546
6547 /*
6548 * .. and append 'j' levels of NUMA goodness.
6549 */
6550 for (j = 0; j < level; i++, j++) {
6551 tl[i] = (struct sched_domain_topology_level){
Peter Zijlstracb83b622012-04-17 15:49:36 +02006552 .mask = sd_numa_mask,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006553 .sd_flags = cpu_numa_flags,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006554 .flags = SDTL_OVERLAP,
6555 .numa_level = j,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006556 SD_INIT_NAME(NUMA)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006557 };
6558 }
6559
6560 sched_domain_topology = tl;
Tang Chen5f7865f2012-09-25 21:12:30 +08006561
6562 sched_domains_numa_levels = level;
Rik van Riel9942f792014-10-17 03:29:49 -04006563 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
Rik van Riele3fe70b2014-10-17 03:29:50 -04006564
6565 init_numa_topology_type();
Peter Zijlstracb83b622012-04-17 15:49:36 +02006566}
Tang Chen301a5cb2012-09-25 21:12:31 +08006567
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006568static void sched_domains_numa_masks_set(unsigned int cpu)
Tang Chen301a5cb2012-09-25 21:12:31 +08006569{
Tang Chen301a5cb2012-09-25 21:12:31 +08006570 int node = cpu_to_node(cpu);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006571 int i, j;
6572
Tang Chen301a5cb2012-09-25 21:12:31 +08006573 for (i = 0; i < sched_domains_numa_levels; i++) {
6574 for (j = 0; j < nr_node_ids; j++) {
6575 if (node_distance(j, node) <= sched_domains_numa_distance[i])
6576 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
6577 }
6578 }
6579}
6580
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006581static void sched_domains_numa_masks_clear(unsigned int cpu)
Tang Chen301a5cb2012-09-25 21:12:31 +08006582{
6583 int i, j;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006584
Tang Chen301a5cb2012-09-25 21:12:31 +08006585 for (i = 0; i < sched_domains_numa_levels; i++) {
6586 for (j = 0; j < nr_node_ids; j++)
6587 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
6588 }
6589}
6590
Peter Zijlstracb83b622012-04-17 15:49:36 +02006591#else
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006592static inline void sched_init_numa(void) { }
6593static void sched_domains_numa_masks_set(unsigned int cpu) { }
6594static void sched_domains_numa_masks_clear(unsigned int cpu) { }
Peter Zijlstracb83b622012-04-17 15:49:36 +02006595#endif /* CONFIG_NUMA */
6596
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006597static int __sdt_alloc(const struct cpumask *cpu_map)
6598{
6599 struct sched_domain_topology_level *tl;
6600 int j;
6601
Viresh Kumar27723a62013-06-10 16:27:20 +05306602 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006603 struct sd_data *sdd = &tl->data;
6604
6605 sdd->sd = alloc_percpu(struct sched_domain *);
6606 if (!sdd->sd)
6607 return -ENOMEM;
6608
6609 sdd->sg = alloc_percpu(struct sched_group *);
6610 if (!sdd->sg)
6611 return -ENOMEM;
6612
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006613 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
6614 if (!sdd->sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006615 return -ENOMEM;
6616
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006617 for_each_cpu(j, cpu_map) {
6618 struct sched_domain *sd;
6619 struct sched_group *sg;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006620 struct sched_group_capacity *sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006621
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02006622 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006623 GFP_KERNEL, cpu_to_node(j));
6624 if (!sd)
6625 return -ENOMEM;
6626
6627 *per_cpu_ptr(sdd->sd, j) = sd;
6628
6629 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
6630 GFP_KERNEL, cpu_to_node(j));
6631 if (!sg)
6632 return -ENOMEM;
6633
Igor Mammedov30b4e9e2012-05-09 12:38:28 +02006634 sg->next = sg;
6635
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006636 *per_cpu_ptr(sdd->sg, j) = sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006637
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006638 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006639 GFP_KERNEL, cpu_to_node(j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006640 if (!sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006641 return -ENOMEM;
6642
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006643 *per_cpu_ptr(sdd->sgc, j) = sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006644 }
6645 }
6646
6647 return 0;
6648}
6649
6650static void __sdt_free(const struct cpumask *cpu_map)
6651{
6652 struct sched_domain_topology_level *tl;
6653 int j;
6654
Viresh Kumar27723a62013-06-10 16:27:20 +05306655 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006656 struct sd_data *sdd = &tl->data;
6657
6658 for_each_cpu(j, cpu_map) {
he, bofb2cf2c2012-04-25 19:59:21 +08006659 struct sched_domain *sd;
6660
6661 if (sdd->sd) {
6662 sd = *per_cpu_ptr(sdd->sd, j);
6663 if (sd && (sd->flags & SD_OVERLAP))
6664 free_sched_groups(sd->groups, 0);
6665 kfree(*per_cpu_ptr(sdd->sd, j));
6666 }
6667
6668 if (sdd->sg)
6669 kfree(*per_cpu_ptr(sdd->sg, j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006670 if (sdd->sgc)
6671 kfree(*per_cpu_ptr(sdd->sgc, j));
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006672 }
6673 free_percpu(sdd->sd);
he, bofb2cf2c2012-04-25 19:59:21 +08006674 sdd->sd = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006675 free_percpu(sdd->sg);
he, bofb2cf2c2012-04-25 19:59:21 +08006676 sdd->sg = NULL;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006677 free_percpu(sdd->sgc);
6678 sdd->sgc = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006679 }
6680}
6681
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006682struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306683 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6684 struct sched_domain *child, int cpu)
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006685{
Vincent Guittot143e1e22014-04-11 11:44:37 +02006686 struct sched_domain *sd = sd_init(tl, cpu);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006687 if (!sd)
Peter Zijlstrad069b912011-04-07 14:10:02 +02006688 return child;
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006689
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006690 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
Peter Zijlstra60495e72011-04-07 14:10:04 +02006691 if (child) {
6692 sd->level = child->level + 1;
6693 sched_domain_level_max = max(sched_domain_level_max, sd->level);
Peter Zijlstrad069b912011-04-07 14:10:02 +02006694 child->parent = sd;
Viresh Kumarc75e0122013-06-10 16:27:19 +05306695 sd->child = child;
Peter Zijlstra6ae72df2014-07-22 11:47:40 +02006696
6697 if (!cpumask_subset(sched_domain_span(child),
6698 sched_domain_span(sd))) {
6699 pr_err("BUG: arch topology borken\n");
6700#ifdef CONFIG_SCHED_DEBUG
6701 pr_err(" the %s domain not a subset of the %s domain\n",
6702 child->name, sd->name);
6703#endif
6704 /* Fixup, ensure @sd has at least @child cpus. */
6705 cpumask_or(sched_domain_span(sd),
6706 sched_domain_span(sd),
6707 sched_domain_span(child));
6708 }
6709
Peter Zijlstra60495e72011-04-07 14:10:04 +02006710 }
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006711 set_domain_attribute(sd, attr);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006712
6713 return sd;
6714}
6715
Mike Travis7c16ec52008-04-04 18:11:11 -07006716/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006717 * Build sched domains for a given set of cpus and attach the sched domains
6718 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006720static int build_sched_domains(const struct cpumask *cpu_map,
6721 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722{
Viresh Kumar1c632162013-06-10 16:27:18 +05306723 enum s_alloc alloc_state;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006724 struct sched_domain *sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006725 struct s_data d;
Peter Zijlstra822ff792011-04-07 14:09:51 +02006726 int i, ret = -ENOMEM;
Rusty Russell3404c8d2008-11-25 02:35:03 +10306727
Andreas Herrmann2109b992009-08-18 12:53:00 +02006728 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
6729 if (alloc_state != sa_rootdomain)
6730 goto error;
Mike Travis7c16ec52008-04-04 18:11:11 -07006731
Peter Zijlstradce840a2011-04-07 14:09:50 +02006732 /* Set up domains for cpus specified by the cpu_map. */
Rusty Russellabcd0832008-11-25 02:35:02 +10306733 for_each_cpu(i, cpu_map) {
Peter Zijlstraeb7a74e62011-04-07 14:10:00 +02006734 struct sched_domain_topology_level *tl;
6735
Peter Zijlstra3bd65a82011-04-07 14:09:54 +02006736 sd = NULL;
Viresh Kumar27723a62013-06-10 16:27:20 +05306737 for_each_sd_topology(tl) {
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306738 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
Viresh Kumar22da9562013-06-04 15:41:15 +05306739 if (tl == sched_domain_topology)
6740 *per_cpu_ptr(d.sd, i) = sd;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006741 if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
6742 sd->flags |= SD_OVERLAP;
Peter Zijlstrad1102352011-07-20 18:42:57 +02006743 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
6744 break;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006745 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006746 }
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006747
Peter Zijlstradce840a2011-04-07 14:09:50 +02006748 /* Build the groups for the domains */
6749 for_each_cpu(i, cpu_map) {
6750 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6751 sd->span_weight = cpumask_weight(sched_domain_span(sd));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006752 if (sd->flags & SD_OVERLAP) {
6753 if (build_overlap_sched_groups(sd, i))
6754 goto error;
6755 } else {
6756 if (build_sched_groups(sd, i))
6757 goto error;
6758 }
Peter Zijlstra1cf519022011-04-07 14:09:47 +02006759 }
Peter Zijlstraa06dadb2011-04-07 14:09:44 +02006760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761
Nicolas Pitreced549f2014-05-26 18:19:38 -04006762 /* Calculate CPU capacity for physical packages and nodes */
Peter Zijlstraa9c9a9b2011-04-07 14:09:49 +02006763 for (i = nr_cpumask_bits-1; i >= 0; i--) {
6764 if (!cpumask_test_cpu(i, cpu_map))
6765 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766
Peter Zijlstradce840a2011-04-07 14:09:50 +02006767 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6768 claim_allocations(i, sd);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006769 init_sched_groups_capacity(i, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006770 }
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006771 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006772
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 /* Attach the domains */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006774 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10306775 for_each_cpu(i, cpu_map) {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006776 sd = *per_cpu_ptr(d.sd, i);
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006777 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006779 rcu_read_unlock();
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006780
Peter Zijlstra822ff792011-04-07 14:09:51 +02006781 ret = 0;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006782error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02006783 __free_domain_allocs(&d, alloc_state, cpu_map);
Peter Zijlstra822ff792011-04-07 14:09:51 +02006784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785}
Paul Jackson029190c2007-10-18 23:40:20 -07006786
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306787static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07006788static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02006789static struct sched_domain_attr *dattr_cur;
6790 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07006791
6792/*
6793 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10306794 * cpumask) fails, then fallback to a single sched domain,
6795 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07006796 */
Rusty Russell42128232008-11-25 02:35:12 +10306797static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07006798
Heiko Carstensee79d1b2008-12-09 18:49:50 +01006799/*
6800 * arch_update_cpu_topology lets virtualized architectures update the
6801 * cpu core maps. It is supposed to return 1 if the topology changed
6802 * or 0 if it stayed the same.
6803 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07006804int __weak arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01006805{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01006806 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01006807}
6808
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306809cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
6810{
6811 int i;
6812 cpumask_var_t *doms;
6813
6814 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
6815 if (!doms)
6816 return NULL;
6817 for (i = 0; i < ndoms; i++) {
6818 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
6819 free_sched_domains(doms, i);
6820 return NULL;
6821 }
6822 }
6823 return doms;
6824}
6825
6826void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
6827{
6828 unsigned int i;
6829 for (i = 0; i < ndoms; i++)
6830 free_cpumask_var(doms[i]);
6831 kfree(doms);
6832}
6833
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006834/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006835 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006836 * For now this just excludes isolated cpus, but could be used to
6837 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006838 */
Peter Zijlstrac4a88492011-04-07 14:09:42 +02006839static int init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006840{
Milton Miller73785472007-10-24 18:23:48 +02006841 int err;
6842
Heiko Carstens22e52b02008-03-12 18:31:59 +01006843 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07006844 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306845 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07006846 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306847 doms_cur = &fallback_doms;
6848 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006849 err = build_sched_domains(doms_cur[0], NULL);
Milton Miller6382bc92007-10-15 17:00:19 +02006850 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006851
6852 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006853}
6854
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006855/*
6856 * Detach sched domains from a group of cpus specified in cpu_map
6857 * These cpus will now be attached to the NULL domain
6858 */
Rusty Russell96f874e22008-11-25 02:35:14 +10306859static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006860{
6861 int i;
6862
Peter Zijlstradce840a2011-04-07 14:09:50 +02006863 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10306864 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006865 cpu_attach_domain(NULL, &def_root_domain, i);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006866 rcu_read_unlock();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006867}
6868
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006869/* handle null as "default" */
6870static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
6871 struct sched_domain_attr *new, int idx_new)
6872{
6873 struct sched_domain_attr tmp;
6874
6875 /* fast path */
6876 if (!new && !cur)
6877 return 1;
6878
6879 tmp = SD_ATTR_INIT;
6880 return !memcmp(cur ? (cur + idx_cur) : &tmp,
6881 new ? (new + idx_new) : &tmp,
6882 sizeof(struct sched_domain_attr));
6883}
6884
Paul Jackson029190c2007-10-18 23:40:20 -07006885/*
6886 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006887 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006888 * doms_new[] to the current sched domain partitioning, doms_cur[].
6889 * It destroys each deleted domain and builds each new domain.
6890 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306891 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006892 * The masks don't intersect (don't overlap.) We should setup one
6893 * sched domain for each mask. CPUs not in any of the cpumasks will
6894 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07006895 * current 'doms_cur' domains and in the new 'doms_new', we can leave
6896 * it as it is.
6897 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306898 * The passed in 'doms_new' should be allocated using
6899 * alloc_sched_domains. This routine takes ownership of it and will
6900 * free_sched_domains it when done with it. If the caller failed the
6901 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
6902 * and partition_sched_domains() will fallback to the single partition
6903 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07006904 *
Rusty Russell96f874e22008-11-25 02:35:14 +10306905 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08006906 * ndoms_new == 0 is a special case for destroying existing domains,
6907 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006908 *
Paul Jackson029190c2007-10-18 23:40:20 -07006909 * Call with hotplug lock held
6910 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306911void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006912 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07006913{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006914 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006915 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07006916
Heiko Carstens712555e2008-04-28 11:33:07 +02006917 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006918
Milton Miller73785472007-10-24 18:23:48 +02006919 /* always unregister in case we don't destroy any domains */
6920 unregister_sched_domain_sysctl();
6921
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006922 /* Let architecture update cpu core mappings. */
6923 new_topology = arch_update_cpu_topology();
6924
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006925 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07006926
6927 /* Destroy deleted domains */
6928 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006929 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306930 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006931 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07006932 goto match1;
6933 }
6934 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306935 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07006936match1:
6937 ;
6938 }
6939
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08006940 n = ndoms_cur;
Max Krasnyanskye761b772008-07-15 04:43:49 -07006941 if (doms_new == NULL) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08006942 n = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306943 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01006944 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08006945 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07006946 }
6947
Paul Jackson029190c2007-10-18 23:40:20 -07006948 /* Build new domains */
6949 for (i = 0; i < ndoms_new; i++) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08006950 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306951 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006952 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07006953 goto match2;
6954 }
6955 /* no match - add a new doms_new */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006956 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07006957match2:
6958 ;
6959 }
6960
6961 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306962 if (doms_cur != &fallback_doms)
6963 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006964 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07006965 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006966 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07006967 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02006968
6969 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006970
Heiko Carstens712555e2008-04-28 11:33:07 +02006971 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07006972}
6973
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306974static int num_cpus_frozen; /* used to mark begin/end of suspend/resume */
6975
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976/*
Tejun Heo3a101d02010-06-08 21:40:36 +02006977 * Update cpusets according to cpu_active mask. If cpusets are
6978 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
6979 * around partition_sched_domains().
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306980 *
6981 * If we come here as part of a suspend/resume, don't touch cpusets because we
6982 * want to restore it back to its original state upon resume anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 */
Thomas Gleixner40190a72016-03-10 12:54:13 +01006984static void cpuset_cpu_active(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985{
Thomas Gleixner40190a72016-03-10 12:54:13 +01006986 if (cpuhp_tasks_frozen) {
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306987 /*
6988 * num_cpus_frozen tracks how many CPUs are involved in suspend
6989 * resume sequence. As long as this is not the last online
6990 * operation in the resume sequence, just build a single sched
6991 * domain, ignoring cpusets.
6992 */
6993 num_cpus_frozen--;
6994 if (likely(num_cpus_frozen)) {
6995 partition_sched_domains(1, NULL, NULL);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006996 return;
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306997 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306998 /*
6999 * This is the last CPU online operation. So fall through and
7000 * restore the original sched domains by considering the
7001 * cpuset configurations.
7002 */
Max Krasnyanskye761b772008-07-15 04:43:49 -07007003 }
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007004 cpuset_update_active_cpus(true);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007005}
Tejun Heo3a101d02010-06-08 21:40:36 +02007006
Thomas Gleixner40190a72016-03-10 12:54:13 +01007007static int cpuset_cpu_inactive(unsigned int cpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02007008{
Juri Lelli3c18d442015-03-31 09:53:37 +01007009 unsigned long flags;
Juri Lelli3c18d442015-03-31 09:53:37 +01007010 struct dl_bw *dl_b;
Omar Sandoval533445c2015-05-04 03:09:36 -07007011 bool overflow;
7012 int cpus;
Juri Lelli3c18d442015-03-31 09:53:37 +01007013
Thomas Gleixner40190a72016-03-10 12:54:13 +01007014 if (!cpuhp_tasks_frozen) {
Omar Sandoval533445c2015-05-04 03:09:36 -07007015 rcu_read_lock_sched();
7016 dl_b = dl_bw_of(cpu);
Juri Lelli3c18d442015-03-31 09:53:37 +01007017
Omar Sandoval533445c2015-05-04 03:09:36 -07007018 raw_spin_lock_irqsave(&dl_b->lock, flags);
7019 cpus = dl_bw_cpus(cpu);
7020 overflow = __dl_overflow(dl_b, cpus, 0, 0);
7021 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli3c18d442015-03-31 09:53:37 +01007022
Omar Sandoval533445c2015-05-04 03:09:36 -07007023 rcu_read_unlock_sched();
Juri Lelli3c18d442015-03-31 09:53:37 +01007024
Omar Sandoval533445c2015-05-04 03:09:36 -07007025 if (overflow)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007026 return -EBUSY;
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307027 cpuset_update_active_cpus(false);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007028 } else {
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307029 num_cpus_frozen++;
7030 partition_sched_domains(1, NULL, NULL);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007031 }
7032 return 0;
7033}
7034
Thomas Gleixner40190a72016-03-10 12:54:13 +01007035int sched_cpu_activate(unsigned int cpu)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007036{
Thomas Gleixner7d976692016-03-10 12:54:17 +01007037 struct rq *rq = cpu_rq(cpu);
7038 unsigned long flags;
7039
Thomas Gleixner40190a72016-03-10 12:54:13 +01007040 set_cpu_active(cpu, true);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007041
Thomas Gleixner40190a72016-03-10 12:54:13 +01007042 if (sched_smp_initialized) {
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007043 sched_domains_numa_masks_set(cpu);
Thomas Gleixner40190a72016-03-10 12:54:13 +01007044 cpuset_cpu_active();
Tejun Heo3a101d02010-06-08 21:40:36 +02007045 }
Thomas Gleixner7d976692016-03-10 12:54:17 +01007046
7047 /*
7048 * Put the rq online, if not already. This happens:
7049 *
7050 * 1) In the early boot process, because we build the real domains
7051 * after all cpus have been brought up.
7052 *
7053 * 2) At runtime, if cpuset_cpu_active() fails to rebuild the
7054 * domains.
7055 */
7056 raw_spin_lock_irqsave(&rq->lock, flags);
7057 if (rq->rd) {
7058 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
7059 set_rq_online(rq);
7060 }
7061 raw_spin_unlock_irqrestore(&rq->lock, flags);
7062
7063 update_max_interval();
7064
Thomas Gleixner40190a72016-03-10 12:54:13 +01007065 return 0;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007066}
7067
Thomas Gleixner40190a72016-03-10 12:54:13 +01007068int sched_cpu_deactivate(unsigned int cpu)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007069{
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007070 int ret;
7071
Thomas Gleixner40190a72016-03-10 12:54:13 +01007072 set_cpu_active(cpu, false);
Peter Zijlstrab2454ca2016-03-10 12:54:14 +01007073 /*
7074 * We've cleared cpu_active_mask, wait for all preempt-disabled and RCU
7075 * users of this state to go away such that all new such users will
7076 * observe it.
7077 *
7078 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
7079 * not imply sync_sched(), so wait for both.
7080 *
7081 * Do sync before park smpboot threads to take care the rcu boost case.
7082 */
7083 if (IS_ENABLED(CONFIG_PREEMPT))
7084 synchronize_rcu_mult(call_rcu, call_rcu_sched);
7085 else
7086 synchronize_rcu();
Thomas Gleixner40190a72016-03-10 12:54:13 +01007087
7088 if (!sched_smp_initialized)
7089 return 0;
7090
7091 ret = cpuset_cpu_inactive(cpu);
7092 if (ret) {
7093 set_cpu_active(cpu, true);
7094 return ret;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007095 }
Thomas Gleixner40190a72016-03-10 12:54:13 +01007096 sched_domains_numa_masks_clear(cpu);
7097 return 0;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007098}
7099
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01007100static void sched_rq_cpu_starting(unsigned int cpu)
7101{
7102 struct rq *rq = cpu_rq(cpu);
7103
7104 rq->calc_load_update = calc_load_update;
7105 account_reset_rq(rq);
7106 update_max_interval();
7107}
7108
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007109int sched_cpu_starting(unsigned int cpu)
7110{
7111 set_cpu_rq_start_time(cpu);
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01007112 sched_rq_cpu_starting(cpu);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01007113 return 0;
Tejun Heo3a101d02010-06-08 21:40:36 +02007114}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007115
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01007116#ifdef CONFIG_HOTPLUG_CPU
7117int sched_cpu_dying(unsigned int cpu)
7118{
7119 struct rq *rq = cpu_rq(cpu);
7120 unsigned long flags;
7121
7122 /* Handle pending wakeups and then migrate everything off */
7123 sched_ttwu_pending();
7124 raw_spin_lock_irqsave(&rq->lock, flags);
7125 if (rq->rd) {
7126 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
7127 set_rq_offline(rq);
7128 }
7129 migrate_tasks(rq);
7130 BUG_ON(rq->nr_running != 1);
7131 raw_spin_unlock_irqrestore(&rq->lock, flags);
7132 calc_load_migrate(rq);
7133 update_max_interval();
7134 return 0;
7135}
7136#endif
7137
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138void __init sched_init_smp(void)
7139{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307140 cpumask_var_t non_isolated_cpus;
7141
7142 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007143 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007144
Peter Zijlstracb83b622012-04-17 15:49:36 +02007145 sched_init_numa();
7146
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02007147 /*
7148 * There's no userspace yet to cause hotplug operations; hence all the
7149 * cpu masks are stable and all blatant races in the below code cannot
7150 * happen.
7151 */
Heiko Carstens712555e2008-04-28 11:33:07 +02007152 mutex_lock(&sched_domains_mutex);
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007153 init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307154 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7155 if (cpumask_empty(non_isolated_cpus))
7156 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007157 mutex_unlock(&sched_domains_mutex);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007158
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007159 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007160
7161 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307162 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007163 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007164 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307165 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307166
Rusty Russell0e3900e2008-11-25 02:35:13 +10307167 init_sched_rt_class();
Juri Lelli1baca4c2013-11-07 14:43:38 +01007168 init_sched_dl_class();
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01007169 sched_smp_initialized = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170}
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01007171
7172static int __init migration_init(void)
7173{
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01007174 sched_rq_cpu_starting(smp_processor_id());
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01007175 return 0;
7176}
7177early_initcall(migration_init);
7178
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179#else
7180void __init sched_init_smp(void)
7181{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007182 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183}
7184#endif /* CONFIG_SMP */
7185
7186int in_sched_functions(unsigned long addr)
7187{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 return in_lock_functions(addr) ||
7189 (addr >= (unsigned long)__sched_text_start
7190 && addr < (unsigned long)__sched_text_end);
7191}
7192
Peter Zijlstra029632f2011-10-25 10:00:11 +02007193#ifdef CONFIG_CGROUP_SCHED
Li Zefan27b4b932013-03-05 16:07:52 +08007194/*
7195 * Default task group.
7196 * Every task in system belongs to this group at bootup.
7197 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007198struct task_group root_task_group;
Mike Galbraith35cf4e52012-08-07 05:00:13 +02007199LIST_HEAD(task_groups);
Waiman Longb0367622015-12-02 13:41:49 -05007200
7201/* Cacheline aligned slab cache for task_group */
7202static struct kmem_cache *task_group_cache __read_mostly;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007203#endif
7204
Joonsoo Kime6252c32013-04-23 17:27:41 +09007205DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007206
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207void __init sched_init(void)
7208{
Ingo Molnardd41f592007-07-09 18:51:59 +02007209 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007210 unsigned long alloc_size = 0, ptr;
7211
7212#ifdef CONFIG_FAIR_GROUP_SCHED
7213 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7214#endif
7215#ifdef CONFIG_RT_GROUP_SCHED
7216 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7217#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007218 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007219 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007220
7221#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007222 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007223 ptr += nr_cpu_ids * sizeof(void **);
7224
Yong Zhang07e06b02011-01-07 15:17:36 +08007225 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007226 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007227
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007228#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007229#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007230 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007231 ptr += nr_cpu_ids * sizeof(void **);
7232
Yong Zhang07e06b02011-01-07 15:17:36 +08007233 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007234 ptr += nr_cpu_ids * sizeof(void **);
7235
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007236#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007237 }
Alex Thorltonb74e6272014-12-18 12:44:30 -06007238#ifdef CONFIG_CPUMASK_OFFSTACK
7239 for_each_possible_cpu(i) {
7240 per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
7241 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
7242 }
7243#endif /* CONFIG_CPUMASK_OFFSTACK */
Ingo Molnardd41f592007-07-09 18:51:59 +02007244
Dario Faggioli332ac172013-11-07 14:43:45 +01007245 init_rt_bandwidth(&def_rt_bandwidth,
7246 global_rt_period(), global_rt_runtime());
7247 init_dl_bandwidth(&def_dl_bandwidth,
Peter Zijlstra17248132013-12-17 12:44:49 +01007248 global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +01007249
Gregory Haskins57d885f2008-01-25 21:08:18 +01007250#ifdef CONFIG_SMP
7251 init_defrootdomain();
7252#endif
7253
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007254#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007255 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007256 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007257#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007258
Dhaval Giani7c941432010-01-20 13:26:18 +01007259#ifdef CONFIG_CGROUP_SCHED
Waiman Longb0367622015-12-02 13:41:49 -05007260 task_group_cache = KMEM_CACHE(task_group, 0);
7261
Yong Zhang07e06b02011-01-07 15:17:36 +08007262 list_add(&root_task_group.list, &task_groups);
7263 INIT_LIST_HEAD(&root_task_group.children);
Glauber Costaf4d6f6c2011-11-01 19:19:07 -02007264 INIT_LIST_HEAD(&root_task_group.siblings);
Mike Galbraith5091faa2010-11-30 14:18:03 +01007265 autogroup_init(&init_task);
Dhaval Giani7c941432010-01-20 13:26:18 +01007266#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007267
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007268 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007269 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270
7271 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007272 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07007273 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007274 rq->calc_load_active = 0;
7275 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007276 init_cfs_rq(&rq->cfs);
Abel Vesa07c54f72015-03-03 13:50:27 +02007277 init_rt_rq(&rq->rt);
7278 init_dl_rq(&rq->dl);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007279#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007280 root_task_group.shares = ROOT_TASK_GROUP_LOAD;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007281 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007282 /*
Yong Zhang07e06b02011-01-07 15:17:36 +08007283 * How much cpu bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02007284 *
7285 * In case of task-groups formed thr' the cgroup filesystem, it
7286 * gets 100% of the cpu resources in the system. This overall
7287 * system cpu resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08007288 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02007289 * based on each entity's (task or task-group's) weight
7290 * (se->load.weight).
7291 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007292 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02007293 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7294 * then A0's share of the cpu resource is:
7295 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007296 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007297 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007298 * We achieve this by letting root_task_group's tasks sit
7299 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02007300 */
Paul Turnerab84d312011-07-21 09:43:28 -07007301 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Yong Zhang07e06b02011-01-07 15:17:36 +08007302 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007303#endif /* CONFIG_FAIR_GROUP_SCHED */
7304
7305 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007306#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007307 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309
Ingo Molnardd41f592007-07-09 18:51:59 +02007310 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7311 rq->cpu_load[j] = 0;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07007312
7313 rq->last_load_update_tick = jiffies;
7314
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007316 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007317 rq->rd = NULL;
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007318 rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02007319 rq->balance_callback = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007321 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007323 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04007324 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007325 rq->idle_stamp = 0;
7326 rq->avg_idle = 2*sysctl_sched_migration_cost;
Jason Low9bd721c2013-09-13 11:26:52 -07007327 rq->max_idle_balance_cost = sysctl_sched_migration_cost;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007328
7329 INIT_LIST_HEAD(&rq->cfs_tasks);
7330
Gregory Haskinsdc938522008-01-25 21:08:26 +01007331 rq_attach_root(rq, &def_root_domain);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007332#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08007333 rq->nohz_flags = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007334#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02007335#ifdef CONFIG_NO_HZ_FULL
7336 rq->last_sched_tick = 0;
7337#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007339 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 }
7342
Peter Williams2dd73a42006-06-27 02:54:34 -07007343 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007344
Avi Kivitye107be32007-07-26 13:40:43 +02007345#ifdef CONFIG_PREEMPT_NOTIFIERS
7346 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7347#endif
7348
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 /*
7350 * The boot idle thread does lazy MMU switching as well:
7351 */
7352 atomic_inc(&init_mm.mm_count);
7353 enter_lazy_tlb(&init_mm, current);
7354
7355 /*
Yao Dongdong1b537c72014-12-29 14:41:43 +08007356 * During early bootup we pretend to be a normal task:
7357 */
7358 current->sched_class = &fair_sched_class;
7359
7360 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 * Make us the idle thread. Technically, schedule() should not be
7362 * called from this thread, however somewhere below it might be,
7363 * but because we are the idle thread, we just pick up running again
7364 * when this runqueue becomes "idle".
7365 */
7366 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007367
7368 calc_load_update = jiffies + LOAD_FREQ;
7369
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307370#ifdef CONFIG_SMP
Peter Zijlstra4cb98832011-04-07 14:09:58 +02007371 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
Rusty Russellbdddd292009-12-02 14:09:16 +10307372 /* May be allocated at isolcpus cmdline parse time */
7373 if (cpu_isolated_map == NULL)
7374 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Thomas Gleixner29d5e042012-04-20 13:05:45 +00007375 idle_thread_set_boot_cpu();
Thomas Gleixner9cf72432016-03-10 12:54:09 +01007376 set_cpu_rq_start_time(smp_processor_id());
Peter Zijlstra029632f2011-10-25 10:00:11 +02007377#endif
7378 init_sched_fair_class();
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307379
Ingo Molnar6892b752008-02-13 14:02:36 +01007380 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381}
7382
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02007383#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007384static inline int preempt_count_equals(int preempt_offset)
7385{
Peter Zijlstrada7142e2015-09-28 18:11:45 +02007386 int nested = preempt_count() + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007387
Arnd Bergmann4ba82162011-01-25 22:52:22 +01007388 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007389}
7390
Simon Kagstromd8948372009-12-23 11:08:18 +01007391void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392{
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007393 /*
7394 * Blocking primitives will set (and therefore destroy) current->state,
7395 * since we will exit with TASK_RUNNING make sure we enter with it,
7396 * otherwise we will destroy state.
7397 */
Linus Torvalds00845eb2015-02-01 12:23:32 -08007398 WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007399 "do not call blocking ops when !TASK_RUNNING; "
7400 "state=%lx set at [<%p>] %pS\n",
7401 current->state,
7402 (void *)current->task_state_change,
Linus Torvalds00845eb2015-02-01 12:23:32 -08007403 (void *)current->task_state_change);
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007404
Peter Zijlstra34274452014-09-24 10:18:56 +02007405 ___might_sleep(file, line, preempt_offset);
7406}
7407EXPORT_SYMBOL(__might_sleep);
7408
7409void ___might_sleep(const char *file, int line, int preempt_offset)
7410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 static unsigned long prev_jiffy; /* ratelimiting */
7412
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07007413 rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
Thomas Gleixnerdb273be2014-02-07 20:58:38 +01007414 if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
7415 !is_idle_task(current)) ||
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007416 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007417 return;
7418 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7419 return;
7420 prev_jiffy = jiffies;
7421
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007422 printk(KERN_ERR
7423 "BUG: sleeping function called from invalid context at %s:%d\n",
7424 file, line);
7425 printk(KERN_ERR
7426 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7427 in_atomic(), irqs_disabled(),
7428 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007429
Eric Sandeena8b686b2014-12-16 16:25:28 -06007430 if (task_stack_end_corrupted(current))
7431 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
7432
Ingo Molnaraef745f2008-08-28 11:34:43 +02007433 debug_show_held_locks(current);
7434 if (irqs_disabled())
7435 print_irqtrace_events(current);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007436#ifdef CONFIG_DEBUG_PREEMPT
7437 if (!preempt_count_equals(preempt_offset)) {
7438 pr_err("Preemption disabled at:");
7439 print_ip_sym(current->preempt_disable_ip);
7440 pr_cont("\n");
7441 }
7442#endif
Ingo Molnaraef745f2008-08-28 11:34:43 +02007443 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444}
Peter Zijlstra34274452014-09-24 10:18:56 +02007445EXPORT_SYMBOL(___might_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446#endif
7447
7448#ifdef CONFIG_MAGIC_SYSRQ
7449void normalize_rt_tasks(void)
7450{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007451 struct task_struct *g, *p;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007452 struct sched_attr attr = {
7453 .sched_policy = SCHED_NORMAL,
7454 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007456 read_lock(&tasklist_lock);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007457 for_each_process_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007458 /*
7459 * Only normalize user tasks:
7460 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007461 if (p->flags & PF_KTHREAD)
Ingo Molnar178be792007-10-15 17:00:18 +02007462 continue;
7463
Ingo Molnardd41f592007-07-09 18:51:59 +02007464 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007465#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03007466 p->se.statistics.wait_start = 0;
7467 p->se.statistics.sleep_start = 0;
7468 p->se.statistics.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007469#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007470
Dario Faggioliaab03e02013-11-28 11:14:43 +01007471 if (!dl_task(p) && !rt_task(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007472 /*
7473 * Renice negative nice level userspace
7474 * tasks back to 0:
7475 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007476 if (task_nice(p) < 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02007477 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007481 __sched_setscheduler(p, &attr, false, false);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007482 }
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007483 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484}
7485
7486#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007487
Jason Wessel67fc4e02010-05-20 21:04:21 -05007488#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007489/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007490 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007491 *
7492 * They can only be called when the whole system has been
7493 * stopped - every CPU needs to be quiescent, and no scheduling
7494 * activity can take place. Using them for anything else would
7495 * be a serious bug, and as a result, they aren't even visible
7496 * under any other configuration.
7497 */
7498
7499/**
7500 * curr_task - return the current task for a given cpu.
7501 * @cpu: the processor in question.
7502 *
7503 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
Yacine Belkadie69f6182013-07-12 20:45:47 +02007504 *
7505 * Return: The current task for @cpu.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007506 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007507struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007508{
7509 return cpu_curr(cpu);
7510}
7511
Jason Wessel67fc4e02010-05-20 21:04:21 -05007512#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7513
7514#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07007515/**
7516 * set_curr_task - set the current task for a given cpu.
7517 * @cpu: the processor in question.
7518 * @p: the task pointer to set.
7519 *
7520 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007521 * are serviced on a separate stack. It allows the architecture to switch the
7522 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007523 * must be called with all CPU's synchronized, and interrupts disabled, the
7524 * and caller must save the original value of the current task (see
7525 * curr_task() above) and restore that value before reenabling interrupts and
7526 * re-starting the system.
7527 *
7528 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7529 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007530void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007531{
7532 cpu_curr(cpu) = p;
7533}
7534
7535#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007536
Dhaval Giani7c941432010-01-20 13:26:18 +01007537#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007538/* task_group_lock serializes the addition/removal of task groups */
7539static DEFINE_SPINLOCK(task_group_lock);
7540
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007541static void sched_free_group(struct task_group *tg)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007542{
7543 free_fair_sched_group(tg);
7544 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01007545 autogroup_free(tg);
Waiman Longb0367622015-12-02 13:41:49 -05007546 kmem_cache_free(task_group_cache, tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007547}
7548
7549/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007550struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007551{
7552 struct task_group *tg;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007553
Waiman Longb0367622015-12-02 13:41:49 -05007554 tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007555 if (!tg)
7556 return ERR_PTR(-ENOMEM);
7557
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007558 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007559 goto err;
7560
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007561 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007562 goto err;
7563
Li Zefanace783b2013-01-24 14:30:48 +08007564 return tg;
7565
7566err:
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007567 sched_free_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08007568 return ERR_PTR(-ENOMEM);
7569}
7570
7571void sched_online_group(struct task_group *tg, struct task_group *parent)
7572{
7573 unsigned long flags;
7574
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007575 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007576 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007577
7578 WARN_ON(!parent); /* root should already exist */
7579
7580 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007581 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08007582 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007583 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007584}
7585
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007586/* rcu callback to free various structures associated with a task group */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007587static void sched_free_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007588{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007589 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007590 sched_free_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007591}
7592
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007593void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007594{
Li Zefanace783b2013-01-24 14:30:48 +08007595 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007596 call_rcu(&tg->rcu, sched_free_group_rcu);
Li Zefanace783b2013-01-24 14:30:48 +08007597}
7598
7599void sched_offline_group(struct task_group *tg)
7600{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007601 unsigned long flags;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007602
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007603 /* end participation in shares distribution */
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01007604 unregister_fair_sched_group(tg);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007605
7606 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007607 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007608 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007609 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007610}
7611
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007612/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007613 * The caller of this function should have put the task in its new group
7614 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7615 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007616 */
7617void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007618{
Peter Zijlstra8323f262012-06-22 13:36:05 +02007619 struct task_group *tg;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007620 int queued, running;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007621 unsigned long flags;
7622 struct rq *rq;
7623
7624 rq = task_rq_lock(tsk, &flags);
7625
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007626 running = task_current(rq, tsk);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007627 queued = task_on_rq_queued(tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007628
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007629 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01007630 dequeue_task(rq, tsk, DEQUEUE_SAVE | DEQUEUE_MOVE);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007631 if (unlikely(running))
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04007632 put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007633
Kirill Tkhaif7b8a472014-10-28 08:24:34 +03007634 /*
7635 * All callers are synchronized by task_rq_lock(); we do not use RCU
7636 * which is pointless here. Thus, we pass "true" to task_css_check()
7637 * to prevent lockdep warnings.
7638 */
7639 tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
Peter Zijlstra8323f262012-06-22 13:36:05 +02007640 struct task_group, css);
7641 tg = autogroup_task_group(tsk, tg);
7642 tsk->sched_task_group = tg;
7643
Peter Zijlstra810b3812008-02-29 15:21:01 -05007644#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007645 if (tsk->sched_class->task_move_group)
Peter Zijlstrabc54da22015-08-31 17:13:55 +02007646 tsk->sched_class->task_move_group(tsk);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007647 else
Peter Zijlstra810b3812008-02-29 15:21:01 -05007648#endif
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007649 set_task_rq(tsk, task_cpu(tsk));
Peter Zijlstra810b3812008-02-29 15:21:01 -05007650
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007651 if (unlikely(running))
7652 tsk->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007653 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01007654 enqueue_task(rq, tsk, ENQUEUE_RESTORE | ENQUEUE_MOVE);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007655
Peter Zijlstra0122ec52011-04-05 17:23:51 +02007656 task_rq_unlock(rq, tsk, &flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007657}
Dhaval Giani7c941432010-01-20 13:26:18 +01007658#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007659
Paul Turnera790de92011-07-21 09:43:29 -07007660#ifdef CONFIG_RT_GROUP_SCHED
7661/*
7662 * Ensure that the real time constraints are schedulable.
7663 */
7664static DEFINE_MUTEX(rt_constraints_mutex);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007665
Dhaval Giani521f1a242008-02-28 15:21:56 +05307666/* Must be called with tasklist_lock held */
7667static inline int tg_has_rt_tasks(struct task_group *tg)
7668{
7669 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007670
Peter Zijlstra1fe89e12015-02-09 11:53:18 +01007671 /*
7672 * Autogroups do not have RT tasks; see autogroup_create().
7673 */
7674 if (task_group_is_autogroup(tg))
7675 return 0;
7676
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007677 for_each_process_thread(g, p) {
Oleg Nesterov8651c652014-09-21 21:33:36 +02007678 if (rt_task(p) && task_group(p) == tg)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307679 return 1;
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007680 }
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007681
Dhaval Giani521f1a242008-02-28 15:21:56 +05307682 return 0;
7683}
7684
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007685struct rt_schedulable_data {
7686 struct task_group *tg;
7687 u64 rt_period;
7688 u64 rt_runtime;
7689};
7690
Paul Turnera790de92011-07-21 09:43:29 -07007691static int tg_rt_schedulable(struct task_group *tg, void *data)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007692{
7693 struct rt_schedulable_data *d = data;
7694 struct task_group *child;
7695 unsigned long total, sum = 0;
7696 u64 period, runtime;
7697
7698 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7699 runtime = tg->rt_bandwidth.rt_runtime;
7700
7701 if (tg == d->tg) {
7702 period = d->rt_period;
7703 runtime = d->rt_runtime;
7704 }
7705
Peter Zijlstra4653f802008-09-23 15:33:44 +02007706 /*
7707 * Cannot have more runtime than the period.
7708 */
7709 if (runtime > period && runtime != RUNTIME_INF)
7710 return -EINVAL;
7711
7712 /*
7713 * Ensure we don't starve existing RT tasks.
7714 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007715 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
7716 return -EBUSY;
7717
7718 total = to_ratio(period, runtime);
7719
Peter Zijlstra4653f802008-09-23 15:33:44 +02007720 /*
7721 * Nobody can have more than the global setting allows.
7722 */
7723 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
7724 return -EINVAL;
7725
7726 /*
7727 * The sum of our children's runtime should not exceed our own.
7728 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007729 list_for_each_entry_rcu(child, &tg->children, siblings) {
7730 period = ktime_to_ns(child->rt_bandwidth.rt_period);
7731 runtime = child->rt_bandwidth.rt_runtime;
7732
7733 if (child == d->tg) {
7734 period = d->rt_period;
7735 runtime = d->rt_runtime;
7736 }
7737
7738 sum += to_ratio(period, runtime);
7739 }
7740
7741 if (sum > total)
7742 return -EINVAL;
7743
7744 return 0;
7745}
7746
7747static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
7748{
Paul Turner82774342011-07-21 09:43:35 -07007749 int ret;
7750
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007751 struct rt_schedulable_data data = {
7752 .tg = tg,
7753 .rt_period = period,
7754 .rt_runtime = runtime,
7755 };
7756
Paul Turner82774342011-07-21 09:43:35 -07007757 rcu_read_lock();
7758 ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
7759 rcu_read_unlock();
7760
7761 return ret;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007762}
7763
Paul Turnerab84d312011-07-21 09:43:28 -07007764static int tg_set_rt_bandwidth(struct task_group *tg,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007765 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007766{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007767 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007768
Peter Zijlstra2636ed52015-02-09 12:23:20 +01007769 /*
7770 * Disallowing the root group RT runtime is BAD, it would disallow the
7771 * kernel creating (and or operating) RT threads.
7772 */
7773 if (tg == &root_task_group && rt_runtime == 0)
7774 return -EINVAL;
7775
7776 /* No period doesn't make any sense. */
7777 if (rt_period == 0)
7778 return -EINVAL;
7779
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007780 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307781 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007782 err = __rt_schedulable(tg, rt_period, rt_runtime);
7783 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307784 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007785
Thomas Gleixner0986b112009-11-17 15:32:06 +01007786 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007787 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
7788 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007789
7790 for_each_possible_cpu(i) {
7791 struct rt_rq *rt_rq = tg->rt_rq[i];
7792
Thomas Gleixner0986b112009-11-17 15:32:06 +01007793 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007794 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01007795 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007796 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007797 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra49246272010-10-17 21:46:10 +02007798unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307799 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007800 mutex_unlock(&rt_constraints_mutex);
7801
7802 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007803}
7804
Li Zefan25cc7da2013-03-05 16:07:33 +08007805static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007806{
7807 u64 rt_runtime, rt_period;
7808
7809 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7810 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7811 if (rt_runtime_us < 0)
7812 rt_runtime = RUNTIME_INF;
7813
Paul Turnerab84d312011-07-21 09:43:28 -07007814 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007815}
7816
Li Zefan25cc7da2013-03-05 16:07:33 +08007817static long sched_group_rt_runtime(struct task_group *tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007818{
7819 u64 rt_runtime_us;
7820
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007821 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007822 return -1;
7823
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007824 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007825 do_div(rt_runtime_us, NSEC_PER_USEC);
7826 return rt_runtime_us;
7827}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007828
Nicholas Mc Guirece2f5fe2015-05-03 10:51:56 +02007829static int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007830{
7831 u64 rt_runtime, rt_period;
7832
Nicholas Mc Guirece2f5fe2015-05-03 10:51:56 +02007833 rt_period = rt_period_us * NSEC_PER_USEC;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007834 rt_runtime = tg->rt_bandwidth.rt_runtime;
7835
Paul Turnerab84d312011-07-21 09:43:28 -07007836 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007837}
7838
Li Zefan25cc7da2013-03-05 16:07:33 +08007839static long sched_group_rt_period(struct task_group *tg)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007840{
7841 u64 rt_period_us;
7842
7843 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
7844 do_div(rt_period_us, NSEC_PER_USEC);
7845 return rt_period_us;
7846}
Dario Faggioli332ac172013-11-07 14:43:45 +01007847#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007848
Dario Faggioli332ac172013-11-07 14:43:45 +01007849#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007850static int sched_rt_global_constraints(void)
7851{
7852 int ret = 0;
7853
7854 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007855 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02007856 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007857 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007858 mutex_unlock(&rt_constraints_mutex);
7859
7860 return ret;
7861}
Dhaval Giani54e99122009-02-27 15:13:54 +05307862
Li Zefan25cc7da2013-03-05 16:07:33 +08007863static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
Dhaval Giani54e99122009-02-27 15:13:54 +05307864{
7865 /* Don't accept realtime tasks when there is no way for them to run */
7866 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
7867 return 0;
7868
7869 return 1;
7870}
7871
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007872#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007873static int sched_rt_global_constraints(void)
7874{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007875 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01007876 int i, ret = 0;
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07007877
Thomas Gleixner0986b112009-11-17 15:32:06 +01007878 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007879 for_each_possible_cpu(i) {
7880 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
7881
Thomas Gleixner0986b112009-11-17 15:32:06 +01007882 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007883 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01007884 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007885 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007886 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007887
Dario Faggioli332ac172013-11-07 14:43:45 +01007888 return ret;
7889}
7890#endif /* CONFIG_RT_GROUP_SCHED */
7891
Wanpeng Lia1963b82015-03-17 19:15:31 +08007892static int sched_dl_global_validate(void)
Dario Faggioli332ac172013-11-07 14:43:45 +01007893{
Peter Zijlstra17248132013-12-17 12:44:49 +01007894 u64 runtime = global_rt_runtime();
7895 u64 period = global_rt_period();
Dario Faggioli332ac172013-11-07 14:43:45 +01007896 u64 new_bw = to_ratio(period, runtime);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007897 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01007898 int cpu, ret = 0;
Juri Lelli49516342014-02-11 09:24:27 +01007899 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01007900
7901 /*
7902 * Here we want to check the bandwidth not being set to some
7903 * value smaller than the currently allocated bandwidth in
7904 * any of the root_domains.
7905 *
7906 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
7907 * cycling on root_domains... Discussion on different/better
7908 * solutions is welcome!
7909 */
Peter Zijlstra17248132013-12-17 12:44:49 +01007910 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007911 rcu_read_lock_sched();
7912 dl_b = dl_bw_of(cpu);
Dario Faggioli332ac172013-11-07 14:43:45 +01007913
Juri Lelli49516342014-02-11 09:24:27 +01007914 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007915 if (new_bw < dl_b->total_bw)
7916 ret = -EBUSY;
Juri Lelli49516342014-02-11 09:24:27 +01007917 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007918
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007919 rcu_read_unlock_sched();
7920
Peter Zijlstra17248132013-12-17 12:44:49 +01007921 if (ret)
7922 break;
Dario Faggioli332ac172013-11-07 14:43:45 +01007923 }
7924
Peter Zijlstra17248132013-12-17 12:44:49 +01007925 return ret;
7926}
7927
7928static void sched_dl_do_global(void)
7929{
7930 u64 new_bw = -1;
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007931 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01007932 int cpu;
Juri Lelli49516342014-02-11 09:24:27 +01007933 unsigned long flags;
Peter Zijlstra17248132013-12-17 12:44:49 +01007934
7935 def_dl_bandwidth.dl_period = global_rt_period();
7936 def_dl_bandwidth.dl_runtime = global_rt_runtime();
7937
7938 if (global_rt_runtime() != RUNTIME_INF)
7939 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
7940
7941 /*
7942 * FIXME: As above...
7943 */
7944 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007945 rcu_read_lock_sched();
7946 dl_b = dl_bw_of(cpu);
Peter Zijlstra17248132013-12-17 12:44:49 +01007947
Juri Lelli49516342014-02-11 09:24:27 +01007948 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007949 dl_b->bw = new_bw;
Juri Lelli49516342014-02-11 09:24:27 +01007950 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007951
7952 rcu_read_unlock_sched();
Peter Zijlstra17248132013-12-17 12:44:49 +01007953 }
7954}
7955
7956static int sched_rt_global_validate(void)
7957{
7958 if (sysctl_sched_rt_period <= 0)
7959 return -EINVAL;
7960
Juri Lellie9e7cb32014-02-11 09:24:26 +01007961 if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
7962 (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
Peter Zijlstra17248132013-12-17 12:44:49 +01007963 return -EINVAL;
7964
Dario Faggioli332ac172013-11-07 14:43:45 +01007965 return 0;
7966}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007967
Peter Zijlstra17248132013-12-17 12:44:49 +01007968static void sched_rt_do_global(void)
7969{
7970 def_rt_bandwidth.rt_runtime = global_rt_runtime();
7971 def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
7972}
7973
7974int sched_rt_handler(struct ctl_table *table, int write,
7975 void __user *buffer, size_t *lenp,
7976 loff_t *ppos)
7977{
7978 int old_period, old_runtime;
7979 static DEFINE_MUTEX(mutex);
7980 int ret;
7981
7982 mutex_lock(&mutex);
7983 old_period = sysctl_sched_rt_period;
7984 old_runtime = sysctl_sched_rt_runtime;
7985
7986 ret = proc_dointvec(table, write, buffer, lenp, ppos);
7987
7988 if (!ret && write) {
7989 ret = sched_rt_global_validate();
7990 if (ret)
7991 goto undo;
7992
Wanpeng Lia1963b82015-03-17 19:15:31 +08007993 ret = sched_dl_global_validate();
Peter Zijlstra17248132013-12-17 12:44:49 +01007994 if (ret)
7995 goto undo;
7996
Wanpeng Lia1963b82015-03-17 19:15:31 +08007997 ret = sched_rt_global_constraints();
Peter Zijlstra17248132013-12-17 12:44:49 +01007998 if (ret)
7999 goto undo;
8000
8001 sched_rt_do_global();
8002 sched_dl_do_global();
8003 }
8004 if (0) {
8005undo:
8006 sysctl_sched_rt_period = old_period;
8007 sysctl_sched_rt_runtime = old_runtime;
8008 }
8009 mutex_unlock(&mutex);
8010
8011 return ret;
8012}
8013
Clark Williamsce0dbbb2013-02-07 09:47:04 -06008014int sched_rr_handler(struct ctl_table *table, int write,
8015 void __user *buffer, size_t *lenp,
8016 loff_t *ppos)
8017{
8018 int ret;
8019 static DEFINE_MUTEX(mutex);
8020
8021 mutex_lock(&mutex);
8022 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8023 /* make sure that internally we keep jiffies */
8024 /* also, writing zero resets timeslice to default */
8025 if (!ret && write) {
8026 sched_rr_timeslice = sched_rr_timeslice <= 0 ?
8027 RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice);
8028 }
8029 mutex_unlock(&mutex);
8030 return ret;
8031}
8032
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008033#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008034
Tejun Heoa7c6d552013-08-08 20:11:23 -04008035static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008036{
Tejun Heoa7c6d552013-08-08 20:11:23 -04008037 return css ? container_of(css, struct task_group, css) : NULL;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008038}
8039
Tejun Heoeb954192013-08-08 20:11:23 -04008040static struct cgroup_subsys_state *
8041cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008042{
Tejun Heoeb954192013-08-08 20:11:23 -04008043 struct task_group *parent = css_tg(parent_css);
8044 struct task_group *tg;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008045
Tejun Heoeb954192013-08-08 20:11:23 -04008046 if (!parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008047 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08008048 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008049 }
8050
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008051 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008052 if (IS_ERR(tg))
8053 return ERR_PTR(-ENOMEM);
8054
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008055 sched_online_group(tg, parent);
8056
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008057 return &tg->css;
8058}
8059
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008060static void cpu_cgroup_css_released(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08008061{
Tejun Heoeb954192013-08-08 20:11:23 -04008062 struct task_group *tg = css_tg(css);
Li Zefanace783b2013-01-24 14:30:48 +08008063
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008064 sched_offline_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08008065}
8066
Tejun Heoeb954192013-08-08 20:11:23 -04008067static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008068{
Tejun Heoeb954192013-08-08 20:11:23 -04008069 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008070
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008071 /*
8072 * Relies on the RCU grace period between css_released() and this.
8073 */
8074 sched_free_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08008075}
8076
Oleg Nesterovb53202e2015-12-03 10:24:08 -05008077static void cpu_cgroup_fork(struct task_struct *task)
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008078{
8079 sched_move_task(task);
8080}
8081
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008082static int cpu_cgroup_can_attach(struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008083{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008084 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008085 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08008086
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008087 cgroup_taskset_for_each(task, css, tset) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008088#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heoeb954192013-08-08 20:11:23 -04008089 if (!sched_rt_can_attach(css_tg(css), task))
Tejun Heobb9d97b2011-12-12 18:12:21 -08008090 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008091#else
Tejun Heobb9d97b2011-12-12 18:12:21 -08008092 /* We don't support RT-tasks being in separate groups */
8093 if (task->sched_class != &fair_sched_class)
8094 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008095#endif
Tejun Heobb9d97b2011-12-12 18:12:21 -08008096 }
Ben Blumbe367d02009-09-23 15:56:31 -07008097 return 0;
8098}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008099
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008100static void cpu_cgroup_attach(struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008101{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008102 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008103 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08008104
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05008105 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008106 sched_move_task(task);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008107}
8108
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008109#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008110static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
8111 struct cftype *cftype, u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008112{
Tejun Heo182446d2013-08-08 20:11:24 -04008113 return sched_group_set_shares(css_tg(css), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008114}
8115
Tejun Heo182446d2013-08-08 20:11:24 -04008116static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
8117 struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008118{
Tejun Heo182446d2013-08-08 20:11:24 -04008119 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008120
Nikhil Raoc8b28112011-05-18 14:37:48 -07008121 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008122}
Paul Turnerab84d312011-07-21 09:43:28 -07008123
8124#ifdef CONFIG_CFS_BANDWIDTH
Paul Turnera790de92011-07-21 09:43:29 -07008125static DEFINE_MUTEX(cfs_constraints_mutex);
8126
Paul Turnerab84d312011-07-21 09:43:28 -07008127const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
8128const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
8129
Paul Turnera790de92011-07-21 09:43:29 -07008130static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
8131
Paul Turnerab84d312011-07-21 09:43:28 -07008132static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
8133{
Paul Turner56f570e2011-11-07 20:26:33 -08008134 int i, ret = 0, runtime_enabled, runtime_was_enabled;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008135 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnerab84d312011-07-21 09:43:28 -07008136
8137 if (tg == &root_task_group)
8138 return -EINVAL;
8139
8140 /*
8141 * Ensure we have at some amount of bandwidth every period. This is
8142 * to prevent reaching a state of large arrears when throttled via
8143 * entity_tick() resulting in prolonged exit starvation.
8144 */
8145 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
8146 return -EINVAL;
8147
8148 /*
8149 * Likewise, bound things on the otherside by preventing insane quota
8150 * periods. This also allows us to normalize in computing quota
8151 * feasibility.
8152 */
8153 if (period > max_cfs_quota_period)
8154 return -EINVAL;
8155
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008156 /*
8157 * Prevent race between setting of cfs_rq->runtime_enabled and
8158 * unthrottle_offline_cfs_rqs().
8159 */
8160 get_online_cpus();
Paul Turnera790de92011-07-21 09:43:29 -07008161 mutex_lock(&cfs_constraints_mutex);
8162 ret = __cfs_schedulable(tg, period, quota);
8163 if (ret)
8164 goto out_unlock;
8165
Paul Turner58088ad2011-07-21 09:43:31 -07008166 runtime_enabled = quota != RUNTIME_INF;
Paul Turner56f570e2011-11-07 20:26:33 -08008167 runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
Ben Segall1ee14e62013-10-16 11:16:12 -07008168 /*
8169 * If we need to toggle cfs_bandwidth_used, off->on must occur
8170 * before making related changes, and on->off must occur afterwards
8171 */
8172 if (runtime_enabled && !runtime_was_enabled)
8173 cfs_bandwidth_usage_inc();
Paul Turnerab84d312011-07-21 09:43:28 -07008174 raw_spin_lock_irq(&cfs_b->lock);
8175 cfs_b->period = ns_to_ktime(period);
8176 cfs_b->quota = quota;
Paul Turner58088ad2011-07-21 09:43:31 -07008177
Paul Turnera9cf55b2011-07-21 09:43:32 -07008178 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07008179 /* restart the period timer (if active) to handle new period expiry */
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02008180 if (runtime_enabled)
8181 start_cfs_bandwidth(cfs_b);
Paul Turnerab84d312011-07-21 09:43:28 -07008182 raw_spin_unlock_irq(&cfs_b->lock);
8183
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008184 for_each_online_cpu(i) {
Paul Turnerab84d312011-07-21 09:43:28 -07008185 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
Peter Zijlstra029632f2011-10-25 10:00:11 +02008186 struct rq *rq = cfs_rq->rq;
Paul Turnerab84d312011-07-21 09:43:28 -07008187
8188 raw_spin_lock_irq(&rq->lock);
Paul Turner58088ad2011-07-21 09:43:31 -07008189 cfs_rq->runtime_enabled = runtime_enabled;
Paul Turnerab84d312011-07-21 09:43:28 -07008190 cfs_rq->runtime_remaining = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07008191
Peter Zijlstra029632f2011-10-25 10:00:11 +02008192 if (cfs_rq->throttled)
Paul Turner671fd9d2011-07-21 09:43:34 -07008193 unthrottle_cfs_rq(cfs_rq);
Paul Turnerab84d312011-07-21 09:43:28 -07008194 raw_spin_unlock_irq(&rq->lock);
8195 }
Ben Segall1ee14e62013-10-16 11:16:12 -07008196 if (runtime_was_enabled && !runtime_enabled)
8197 cfs_bandwidth_usage_dec();
Paul Turnera790de92011-07-21 09:43:29 -07008198out_unlock:
8199 mutex_unlock(&cfs_constraints_mutex);
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008200 put_online_cpus();
Paul Turnerab84d312011-07-21 09:43:28 -07008201
Paul Turnera790de92011-07-21 09:43:29 -07008202 return ret;
Paul Turnerab84d312011-07-21 09:43:28 -07008203}
8204
8205int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
8206{
8207 u64 quota, period;
8208
Peter Zijlstra029632f2011-10-25 10:00:11 +02008209 period = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008210 if (cfs_quota_us < 0)
8211 quota = RUNTIME_INF;
8212 else
8213 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
8214
8215 return tg_set_cfs_bandwidth(tg, period, quota);
8216}
8217
8218long tg_get_cfs_quota(struct task_group *tg)
8219{
8220 u64 quota_us;
8221
Peter Zijlstra029632f2011-10-25 10:00:11 +02008222 if (tg->cfs_bandwidth.quota == RUNTIME_INF)
Paul Turnerab84d312011-07-21 09:43:28 -07008223 return -1;
8224
Peter Zijlstra029632f2011-10-25 10:00:11 +02008225 quota_us = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008226 do_div(quota_us, NSEC_PER_USEC);
8227
8228 return quota_us;
8229}
8230
8231int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
8232{
8233 u64 quota, period;
8234
8235 period = (u64)cfs_period_us * NSEC_PER_USEC;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008236 quota = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008237
Paul Turnerab84d312011-07-21 09:43:28 -07008238 return tg_set_cfs_bandwidth(tg, period, quota);
8239}
8240
8241long tg_get_cfs_period(struct task_group *tg)
8242{
8243 u64 cfs_period_us;
8244
Peter Zijlstra029632f2011-10-25 10:00:11 +02008245 cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008246 do_div(cfs_period_us, NSEC_PER_USEC);
8247
8248 return cfs_period_us;
8249}
8250
Tejun Heo182446d2013-08-08 20:11:24 -04008251static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
8252 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008253{
Tejun Heo182446d2013-08-08 20:11:24 -04008254 return tg_get_cfs_quota(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008255}
8256
Tejun Heo182446d2013-08-08 20:11:24 -04008257static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
8258 struct cftype *cftype, s64 cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008259{
Tejun Heo182446d2013-08-08 20:11:24 -04008260 return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008261}
8262
Tejun Heo182446d2013-08-08 20:11:24 -04008263static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
8264 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008265{
Tejun Heo182446d2013-08-08 20:11:24 -04008266 return tg_get_cfs_period(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008267}
8268
Tejun Heo182446d2013-08-08 20:11:24 -04008269static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
8270 struct cftype *cftype, u64 cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008271{
Tejun Heo182446d2013-08-08 20:11:24 -04008272 return tg_set_cfs_period(css_tg(css), cfs_period_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008273}
8274
Paul Turnera790de92011-07-21 09:43:29 -07008275struct cfs_schedulable_data {
8276 struct task_group *tg;
8277 u64 period, quota;
8278};
8279
8280/*
8281 * normalize group quota/period to be quota/max_period
8282 * note: units are usecs
8283 */
8284static u64 normalize_cfs_quota(struct task_group *tg,
8285 struct cfs_schedulable_data *d)
8286{
8287 u64 quota, period;
8288
8289 if (tg == d->tg) {
8290 period = d->period;
8291 quota = d->quota;
8292 } else {
8293 period = tg_get_cfs_period(tg);
8294 quota = tg_get_cfs_quota(tg);
8295 }
8296
8297 /* note: these should typically be equivalent */
8298 if (quota == RUNTIME_INF || quota == -1)
8299 return RUNTIME_INF;
8300
8301 return to_ratio(period, quota);
8302}
8303
8304static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
8305{
8306 struct cfs_schedulable_data *d = data;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008307 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008308 s64 quota = 0, parent_quota = -1;
8309
8310 if (!tg->parent) {
8311 quota = RUNTIME_INF;
8312 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02008313 struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008314
8315 quota = normalize_cfs_quota(tg, d);
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008316 parent_quota = parent_b->hierarchical_quota;
Paul Turnera790de92011-07-21 09:43:29 -07008317
8318 /*
8319 * ensure max(child_quota) <= parent_quota, inherit when no
8320 * limit is set
8321 */
8322 if (quota == RUNTIME_INF)
8323 quota = parent_quota;
8324 else if (parent_quota != RUNTIME_INF && quota > parent_quota)
8325 return -EINVAL;
8326 }
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008327 cfs_b->hierarchical_quota = quota;
Paul Turnera790de92011-07-21 09:43:29 -07008328
8329 return 0;
8330}
8331
8332static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
8333{
Paul Turner82774342011-07-21 09:43:35 -07008334 int ret;
Paul Turnera790de92011-07-21 09:43:29 -07008335 struct cfs_schedulable_data data = {
8336 .tg = tg,
8337 .period = period,
8338 .quota = quota,
8339 };
8340
8341 if (quota != RUNTIME_INF) {
8342 do_div(data.period, NSEC_PER_USEC);
8343 do_div(data.quota, NSEC_PER_USEC);
8344 }
8345
Paul Turner82774342011-07-21 09:43:35 -07008346 rcu_read_lock();
8347 ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
8348 rcu_read_unlock();
8349
8350 return ret;
Paul Turnera790de92011-07-21 09:43:29 -07008351}
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008352
Tejun Heo2da8ca82013-12-05 12:28:04 -05008353static int cpu_stats_show(struct seq_file *sf, void *v)
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008354{
Tejun Heo2da8ca82013-12-05 12:28:04 -05008355 struct task_group *tg = css_tg(seq_css(sf));
Peter Zijlstra029632f2011-10-25 10:00:11 +02008356 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008357
Tejun Heo44ffc752013-12-05 12:28:01 -05008358 seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
8359 seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
8360 seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008361
8362 return 0;
8363}
Paul Turnerab84d312011-07-21 09:43:28 -07008364#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008365#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008366
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008367#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008368static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
8369 struct cftype *cft, s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008370{
Tejun Heo182446d2013-08-08 20:11:24 -04008371 return sched_group_set_rt_runtime(css_tg(css), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008372}
8373
Tejun Heo182446d2013-08-08 20:11:24 -04008374static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
8375 struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008376{
Tejun Heo182446d2013-08-08 20:11:24 -04008377 return sched_group_rt_runtime(css_tg(css));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008378}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008379
Tejun Heo182446d2013-08-08 20:11:24 -04008380static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
8381 struct cftype *cftype, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008382{
Tejun Heo182446d2013-08-08 20:11:24 -04008383 return sched_group_set_rt_period(css_tg(css), rt_period_us);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008384}
8385
Tejun Heo182446d2013-08-08 20:11:24 -04008386static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
8387 struct cftype *cft)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008388{
Tejun Heo182446d2013-08-08 20:11:24 -04008389 return sched_group_rt_period(css_tg(css));
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008390}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008391#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008392
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008393static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008394#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008395 {
8396 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008397 .read_u64 = cpu_shares_read_u64,
8398 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008399 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008400#endif
Paul Turnerab84d312011-07-21 09:43:28 -07008401#ifdef CONFIG_CFS_BANDWIDTH
8402 {
8403 .name = "cfs_quota_us",
8404 .read_s64 = cpu_cfs_quota_read_s64,
8405 .write_s64 = cpu_cfs_quota_write_s64,
8406 },
8407 {
8408 .name = "cfs_period_us",
8409 .read_u64 = cpu_cfs_period_read_u64,
8410 .write_u64 = cpu_cfs_period_write_u64,
8411 },
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008412 {
8413 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05008414 .seq_show = cpu_stats_show,
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008415 },
Paul Turnerab84d312011-07-21 09:43:28 -07008416#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008417#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008418 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008419 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008420 .read_s64 = cpu_rt_runtime_read,
8421 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008422 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008423 {
8424 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008425 .read_u64 = cpu_rt_period_read_uint,
8426 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008427 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008428#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07008429 { } /* terminate */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008430};
8431
Tejun Heo073219e2014-02-08 10:36:58 -05008432struct cgroup_subsys cpu_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008433 .css_alloc = cpu_cgroup_css_alloc,
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008434 .css_released = cpu_cgroup_css_released,
Tejun Heo92fb9742012-11-19 08:13:38 -08008435 .css_free = cpu_cgroup_css_free,
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008436 .fork = cpu_cgroup_fork,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008437 .can_attach = cpu_cgroup_can_attach,
8438 .attach = cpu_cgroup_attach,
Tejun Heo55779642014-07-15 11:05:09 -04008439 .legacy_cftypes = cpu_files,
Tejun Heob38e42e2016-02-23 10:00:50 -05008440 .early_init = true,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008441};
8442
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008443#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008444
Paul E. McKenneyb637a322012-09-19 16:58:38 -07008445void dump_cpu_task(int cpu)
8446{
8447 pr_info("Task dump for CPU %d:\n", cpu);
8448 sched_show_task(cpu_curr(cpu));
8449}
Andi Kleened82b8a2015-11-29 20:59:43 -08008450
8451/*
8452 * Nice levels are multiplicative, with a gentle 10% change for every
8453 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
8454 * nice 1, it will get ~10% less CPU time than another CPU-bound task
8455 * that remained on nice 0.
8456 *
8457 * The "10% effect" is relative and cumulative: from _any_ nice level,
8458 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
8459 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
8460 * If a task goes up by ~10% and another task goes down by ~10% then
8461 * the relative distance between them is ~25%.)
8462 */
8463const int sched_prio_to_weight[40] = {
8464 /* -20 */ 88761, 71755, 56483, 46273, 36291,
8465 /* -15 */ 29154, 23254, 18705, 14949, 11916,
8466 /* -10 */ 9548, 7620, 6100, 4904, 3906,
8467 /* -5 */ 3121, 2501, 1991, 1586, 1277,
8468 /* 0 */ 1024, 820, 655, 526, 423,
8469 /* 5 */ 335, 272, 215, 172, 137,
8470 /* 10 */ 110, 87, 70, 56, 45,
8471 /* 15 */ 36, 29, 23, 18, 15,
8472};
8473
8474/*
8475 * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated.
8476 *
8477 * In cases where the weight does not change often, we can use the
8478 * precalculated inverse to speed up arithmetics by turning divisions
8479 * into multiplications:
8480 */
8481const u32 sched_prio_to_wmult[40] = {
8482 /* -20 */ 48388, 59856, 76040, 92818, 118348,
8483 /* -15 */ 147320, 184698, 229616, 287308, 360437,
8484 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
8485 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
8486 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
8487 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
8488 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
8489 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
8490};