blob: d591db1f2d6d85043e6a40f7c9098e63c161c2a1 [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
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/mmu_context.h>
36#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080037#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/completion.h>
39#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070040#include <linux/debug_locks.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020041#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +040057#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020059#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/syscalls.h>
61#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070062#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080063#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070064#include <linux/delayacct.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020065#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020066#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010067#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070068#include <linux/tick.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020069#include <linux/debugfs.h>
70#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>
Al Viro40401532012-02-13 03:58:52 +000074#include <linux/binfmts.h>
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010075#include <linux/context_tracking.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070076#include <linux/compiler.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
127#ifdef CONFIG_SCHED_DEBUG
128#define SCHED_FEAT(name, enabled) \
129 #name ,
130
Hiroshi Shimamoto12925312012-05-25 15:41:54 +0900131static const char * const sched_feat_names[] = {
Peter Zijlstra391e43d2011-11-15 17:14:39 +0100132#include "features.h"
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200133};
134
135#undef SCHED_FEAT
136
Li Zefan34f3a812008-10-30 15:23:32 +0800137static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200138{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200139 int i;
140
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200141 for (i = 0; i < __SCHED_FEAT_NR; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800142 if (!(sysctl_sched_features & (1UL << i)))
143 seq_puts(m, "NO_");
144 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200145 }
Li Zefan34f3a812008-10-30 15:23:32 +0800146 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200147
Li Zefan34f3a812008-10-30 15:23:32 +0800148 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200149}
150
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200151#ifdef HAVE_JUMP_LABEL
152
Ingo Molnarc5905af2012-02-24 08:31:31 +0100153#define jump_label_key__true STATIC_KEY_INIT_TRUE
154#define jump_label_key__false STATIC_KEY_INIT_FALSE
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200155
156#define SCHED_FEAT(name, enabled) \
157 jump_label_key__##enabled ,
158
Ingo Molnarc5905af2012-02-24 08:31:31 +0100159struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200160#include "features.h"
161};
162
163#undef SCHED_FEAT
164
165static void sched_feat_disable(int i)
166{
Peter Zijlstrae33886b2015-07-24 15:03:40 +0200167 static_key_disable(&sched_feat_keys[i]);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200168}
169
170static void sched_feat_enable(int i)
171{
Peter Zijlstrae33886b2015-07-24 15:03:40 +0200172 static_key_enable(&sched_feat_keys[i]);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200173}
174#else
175static void sched_feat_disable(int i) { };
176static void sched_feat_enable(int i) { };
177#endif /* HAVE_JUMP_LABEL */
178
Mel Gorman1a687c22012-11-22 11:16:36 +0000179static int sched_feat_set(char *cmp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200180{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200181 int i;
Mel Gorman1a687c22012-11-22 11:16:36 +0000182 int neg = 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200183
Hillf Danton524429c2011-01-06 20:58:12 +0800184 if (strncmp(cmp, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200185 neg = 1;
186 cmp += 3;
187 }
188
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200189 for (i = 0; i < __SCHED_FEAT_NR; i++) {
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400190 if (strcmp(cmp, sched_feat_names[i]) == 0) {
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200191 if (neg) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200192 sysctl_sched_features &= ~(1UL << i);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200193 sched_feat_disable(i);
194 } else {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200195 sysctl_sched_features |= (1UL << i);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200196 sched_feat_enable(i);
197 }
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200198 break;
199 }
200 }
201
Mel Gorman1a687c22012-11-22 11:16:36 +0000202 return i;
203}
204
205static ssize_t
206sched_feat_write(struct file *filp, const char __user *ubuf,
207 size_t cnt, loff_t *ppos)
208{
209 char buf[64];
210 char *cmp;
211 int i;
Jason Baron5cd08fb2014-07-02 15:52:44 +0000212 struct inode *inode;
Mel Gorman1a687c22012-11-22 11:16:36 +0000213
214 if (cnt > 63)
215 cnt = 63;
216
217 if (copy_from_user(&buf, ubuf, cnt))
218 return -EFAULT;
219
220 buf[cnt] = 0;
221 cmp = strstrip(buf);
222
Jason Baron5cd08fb2014-07-02 15:52:44 +0000223 /* Ensure the static_key remains in a consistent state */
224 inode = file_inode(filp);
225 mutex_lock(&inode->i_mutex);
Mel Gorman1a687c22012-11-22 11:16:36 +0000226 i = sched_feat_set(cmp);
Jason Baron5cd08fb2014-07-02 15:52:44 +0000227 mutex_unlock(&inode->i_mutex);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200228 if (i == __SCHED_FEAT_NR)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200229 return -EINVAL;
230
Jan Blunck42994722009-11-20 17:40:37 +0100231 *ppos += cnt;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200232
233 return cnt;
234}
235
Li Zefan34f3a812008-10-30 15:23:32 +0800236static int sched_feat_open(struct inode *inode, struct file *filp)
237{
238 return single_open(filp, sched_feat_show, NULL);
239}
240
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700241static const struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800242 .open = sched_feat_open,
243 .write = sched_feat_write,
244 .read = seq_read,
245 .llseek = seq_lseek,
246 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200247};
248
249static __init int sched_init_debug(void)
250{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200251 debugfs_create_file("sched_features", 0644, NULL, NULL,
252 &sched_feat_fops);
253
254 return 0;
255}
256late_initcall(sched_init_debug);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200257#endif /* CONFIG_SCHED_DEBUG */
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200258
259/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100260 * Number of tasks to iterate in a single balance run.
261 * Limited because this is done with IRQs disabled.
262 */
263const_debug unsigned int sysctl_sched_nr_migrate = 32;
264
265/*
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200266 * period over which we average the RT time consumption, measured
267 * in ms.
268 *
269 * default: 1s
270 */
271const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
272
273/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100274 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100275 * default: 1s
276 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100277unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100278
Peter Zijlstra029632f2011-10-25 10:00:11 +0200279__read_mostly int scheduler_running;
Ingo Molnar6892b752008-02-13 14:02:36 +0100280
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100281/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100282 * part of the period that we allow rt tasks to run in us.
283 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100284 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100285int sysctl_sched_rt_runtime = 950000;
286
Rik van Riel3fa08182015-03-09 12:12:07 -0400287/* cpus with isolated domains */
288cpumask_var_t cpu_isolated_map;
289
Dario Faggioli332ac172013-11-07 14:43:45 +0100290/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800291 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200293static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 __acquires(rq->lock)
295{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700296 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 local_irq_disable();
299 rq = this_rq();
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100300 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 return rq;
303}
304
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100305#ifdef CONFIG_SCHED_HRTICK
306/*
307 * Use HR-timers to deliver accurate preemption points.
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100308 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100309
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100310static void hrtick_clear(struct rq *rq)
311{
312 if (hrtimer_active(&rq->hrtick_timer))
313 hrtimer_cancel(&rq->hrtick_timer);
314}
315
316/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100317 * High-resolution timer tick.
318 * Runs from hardirq context with interrupts disabled.
319 */
320static enum hrtimer_restart hrtick(struct hrtimer *timer)
321{
322 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
323
324 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
325
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100326 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200327 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100328 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100329 raw_spin_unlock(&rq->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100330
331 return HRTIMER_NORESTART;
332}
333
Rabin Vincent95e904c2008-05-11 05:55:33 +0530334#ifdef CONFIG_SMP
Peter Zijlstra971ee282013-06-28 11:18:53 +0200335
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000336static void __hrtick_restart(struct rq *rq)
Peter Zijlstra971ee282013-06-28 11:18:53 +0200337{
338 struct hrtimer *timer = &rq->hrtick_timer;
Peter Zijlstra971ee282013-06-28 11:18:53 +0200339
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000340 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200341}
342
Peter Zijlstra31656512008-07-18 18:01:23 +0200343/*
344 * called from hardirq (IPI) context
345 */
346static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200347{
Peter Zijlstra31656512008-07-18 18:01:23 +0200348 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200349
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100350 raw_spin_lock(&rq->lock);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200351 __hrtick_restart(rq);
Peter Zijlstra31656512008-07-18 18:01:23 +0200352 rq->hrtick_csd_pending = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100353 raw_spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200354}
355
Peter Zijlstra31656512008-07-18 18:01:23 +0200356/*
357 * Called to set the hrtick timer state.
358 *
359 * called with rq->lock held and irqs disabled
360 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200361void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200362{
Peter Zijlstra31656512008-07-18 18:01:23 +0200363 struct hrtimer *timer = &rq->hrtick_timer;
xiaofeng.yan177ef2a2014-08-26 03:15:41 +0000364 ktime_t time;
365 s64 delta;
366
367 /*
368 * Don't schedule slices shorter than 10000ns, that just
369 * doesn't make sense and can cause timer DoS.
370 */
371 delta = max_t(s64, delay, 10000LL);
372 time = ktime_add_ns(timer->base->get_time(), delta);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200373
Arjan van de Vencc584b22008-09-01 15:02:30 -0700374 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +0200375
376 if (rq == this_rq()) {
Peter Zijlstra971ee282013-06-28 11:18:53 +0200377 __hrtick_restart(rq);
Peter Zijlstra31656512008-07-18 18:01:23 +0200378 } else if (!rq->hrtick_csd_pending) {
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100379 smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
Peter Zijlstra31656512008-07-18 18:01:23 +0200380 rq->hrtick_csd_pending = 1;
381 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200382}
383
384static int
385hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
386{
387 int cpu = (int)(long)hcpu;
388
389 switch (action) {
390 case CPU_UP_CANCELED:
391 case CPU_UP_CANCELED_FROZEN:
392 case CPU_DOWN_PREPARE:
393 case CPU_DOWN_PREPARE_FROZEN:
394 case CPU_DEAD:
395 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +0200396 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200397 return NOTIFY_OK;
398 }
399
400 return NOTIFY_DONE;
401}
402
Rakib Mullickfa748202008-09-22 14:55:45 -0700403static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200404{
405 hotcpu_notifier(hotplug_hrtick, 0);
406}
Peter Zijlstra31656512008-07-18 18:01:23 +0200407#else
408/*
409 * Called to set the hrtick timer state.
410 *
411 * called with rq->lock held and irqs disabled
412 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200413void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstra31656512008-07-18 18:01:23 +0200414{
Wanpeng Li86893332014-11-26 08:44:06 +0800415 /*
416 * Don't schedule slices shorter than 10000ns, that just
417 * doesn't make sense. Rely on vruntime for fairness.
418 */
419 delay = max_t(u64, delay, 10000LL);
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000420 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
421 HRTIMER_MODE_REL_PINNED);
Peter Zijlstra31656512008-07-18 18:01:23 +0200422}
423
Andrew Morton006c75f2008-09-22 14:55:46 -0700424static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +0200425{
426}
Rabin Vincent95e904c2008-05-11 05:55:33 +0530427#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200428
429static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100430{
Peter Zijlstra31656512008-07-18 18:01:23 +0200431#ifdef CONFIG_SMP
432 rq->hrtick_csd_pending = 0;
433
434 rq->hrtick_csd.flags = 0;
435 rq->hrtick_csd.func = __hrtick_start;
436 rq->hrtick_csd.info = rq;
437#endif
438
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100439 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
440 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100441}
Andrew Morton006c75f2008-09-22 14:55:46 -0700442#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100443static inline void hrtick_clear(struct rq *rq)
444{
445}
446
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100447static inline void init_rq_hrtick(struct rq *rq)
448{
449}
450
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200451static inline void init_hrtick(void)
452{
453}
Andrew Morton006c75f2008-09-22 14:55:46 -0700454#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100455
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200456/*
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200457 * cmpxchg based fetch_or, macro so it works for different integer types
458 */
459#define fetch_or(ptr, val) \
460({ typeof(*(ptr)) __old, __val = *(ptr); \
461 for (;;) { \
462 __old = cmpxchg((ptr), __val, __val | (val)); \
463 if (__old == __val) \
464 break; \
465 __val = __old; \
466 } \
467 __old; \
468})
469
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700470#if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200471/*
472 * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
473 * this avoids any races wrt polling state changes and thereby avoids
474 * spurious IPIs.
475 */
476static bool set_nr_and_not_polling(struct task_struct *p)
477{
478 struct thread_info *ti = task_thread_info(p);
479 return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
480}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700481
482/*
483 * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
484 *
485 * If this returns true, then the idle task promises to call
486 * sched_ttwu_pending() and reschedule soon.
487 */
488static bool set_nr_if_polling(struct task_struct *p)
489{
490 struct thread_info *ti = task_thread_info(p);
Jason Low316c1608d2015-04-28 13:00:20 -0700491 typeof(ti->flags) old, val = READ_ONCE(ti->flags);
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700492
493 for (;;) {
494 if (!(val & _TIF_POLLING_NRFLAG))
495 return false;
496 if (val & _TIF_NEED_RESCHED)
497 return true;
498 old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED);
499 if (old == val)
500 break;
501 val = old;
502 }
503 return true;
504}
505
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200506#else
507static bool set_nr_and_not_polling(struct task_struct *p)
508{
509 set_tsk_need_resched(p);
510 return true;
511}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700512
513#ifdef CONFIG_SMP
514static bool set_nr_if_polling(struct task_struct *p)
515{
516 return false;
517}
518#endif
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200519#endif
520
Peter Zijlstra76751042015-05-01 08:27:50 -0700521void wake_q_add(struct wake_q_head *head, struct task_struct *task)
522{
523 struct wake_q_node *node = &task->wake_q;
524
525 /*
526 * Atomically grab the task, if ->wake_q is !nil already it means
527 * its already queued (either by us or someone else) and will get the
528 * wakeup due to that.
529 *
530 * This cmpxchg() implies a full barrier, which pairs with the write
531 * barrier implied by the wakeup in wake_up_list().
532 */
533 if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL))
534 return;
535
536 get_task_struct(task);
537
538 /*
539 * The head is context local, there can be no concurrency.
540 */
541 *head->lastp = node;
542 head->lastp = &node->next;
543}
544
545void wake_up_q(struct wake_q_head *head)
546{
547 struct wake_q_node *node = head->first;
548
549 while (node != WAKE_Q_TAIL) {
550 struct task_struct *task;
551
552 task = container_of(node, struct task_struct, wake_q);
553 BUG_ON(!task);
554 /* task can safely be re-inserted now */
555 node = node->next;
556 task->wake_q.next = NULL;
557
558 /*
559 * wake_up_process() implies a wmb() to pair with the queueing
560 * in wake_q_add() so as not to miss wakeups.
561 */
562 wake_up_process(task);
563 put_task_struct(task);
564 }
565}
566
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200567/*
Kirill Tkhai88751252014-06-29 00:03:57 +0400568 * resched_curr - mark rq's current task 'to be rescheduled now'.
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200569 *
570 * On UP this means the setting of the need_resched flag, on SMP it
571 * might also involve a cross-CPU call to trigger the scheduler on
572 * the target CPU.
573 */
Kirill Tkhai88751252014-06-29 00:03:57 +0400574void resched_curr(struct rq *rq)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200575{
Kirill Tkhai88751252014-06-29 00:03:57 +0400576 struct task_struct *curr = rq->curr;
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200577 int cpu;
578
Kirill Tkhai88751252014-06-29 00:03:57 +0400579 lockdep_assert_held(&rq->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200580
Kirill Tkhai88751252014-06-29 00:03:57 +0400581 if (test_tsk_need_resched(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200582 return;
583
Kirill Tkhai88751252014-06-29 00:03:57 +0400584 cpu = cpu_of(rq);
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200585
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200586 if (cpu == smp_processor_id()) {
Kirill Tkhai88751252014-06-29 00:03:57 +0400587 set_tsk_need_resched(curr);
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200588 set_preempt_need_resched();
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200589 return;
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200590 }
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200591
Kirill Tkhai88751252014-06-29 00:03:57 +0400592 if (set_nr_and_not_polling(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200593 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700594 else
595 trace_sched_wake_idle_without_ipi(cpu);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200596}
597
Peter Zijlstra029632f2011-10-25 10:00:11 +0200598void resched_cpu(int cpu)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200599{
600 struct rq *rq = cpu_rq(cpu);
601 unsigned long flags;
602
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100603 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200604 return;
Kirill Tkhai88751252014-06-29 00:03:57 +0400605 resched_curr(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100606 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200607}
Thomas Gleixner06d83082008-03-22 09:20:24 +0100608
Peter Zijlstrab021fe32013-09-17 09:30:55 +0200609#ifdef CONFIG_SMP
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200610#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixner06d83082008-03-22 09:20:24 +0100611/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700612 * In the semi idle case, use the nearest busy cpu for migrating timers
613 * from an idle cpu. This is good for power-savings.
614 *
615 * We don't do similar optimization for completely idle system, as
616 * selecting an idle cpu will add more delays to the timers than intended
617 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
618 */
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000619int get_nohz_timer_target(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700620{
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000621 int i, cpu = smp_processor_id();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700622 struct sched_domain *sd;
623
Vatika Harlalka9642d182015-09-01 16:50:59 +0200624 if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530625 return cpu;
626
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200627 rcu_read_lock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700628 for_each_domain(cpu, sd) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200629 for_each_cpu(i, sched_domain_span(sd)) {
Vatika Harlalka9642d182015-09-01 16:50:59 +0200630 if (!idle_cpu(i) && is_housekeeping_cpu(cpu)) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200631 cpu = i;
632 goto unlock;
633 }
634 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700635 }
Vatika Harlalka9642d182015-09-01 16:50:59 +0200636
637 if (!is_housekeeping_cpu(cpu))
638 cpu = housekeeping_any_cpu();
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200639unlock:
640 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700641 return cpu;
642}
643/*
Thomas Gleixner06d83082008-03-22 09:20:24 +0100644 * When add_timer_on() enqueues a timer into the timer wheel of an
645 * idle CPU then this timer might expire before the next timer event
646 * which is scheduled to wake up that CPU. In case of a completely
647 * idle system the next event might even be infinite time into the
648 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
649 * leaves the inner idle loop so the newly added timer is taken into
650 * account when the CPU goes back to idle and evaluates the timer
651 * wheel for the next timer event.
652 */
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200653static void wake_up_idle_cpu(int cpu)
Thomas Gleixner06d83082008-03-22 09:20:24 +0100654{
655 struct rq *rq = cpu_rq(cpu);
656
657 if (cpu == smp_processor_id())
658 return;
659
Andy Lutomirski67b9ca72014-06-04 10:31:17 -0700660 if (set_nr_and_not_polling(rq->idle))
Thomas Gleixner06d83082008-03-22 09:20:24 +0100661 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700662 else
663 trace_sched_wake_idle_without_ipi(cpu);
Thomas Gleixner06d83082008-03-22 09:20:24 +0100664}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100665
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200666static bool wake_up_full_nohz_cpu(int cpu)
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200667{
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200668 /*
669 * We just need the target to call irq_exit() and re-evaluate
670 * the next tick. The nohz full kick at least implies that.
671 * If needed we can still optimize that later with an
672 * empty IRQ.
673 */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200674 if (tick_nohz_full_cpu(cpu)) {
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200675 if (cpu != smp_processor_id() ||
676 tick_nohz_tick_stopped())
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200677 tick_nohz_full_kick_cpu(cpu);
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200678 return true;
679 }
680
681 return false;
682}
683
684void wake_up_nohz_cpu(int cpu)
685{
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200686 if (!wake_up_full_nohz_cpu(cpu))
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200687 wake_up_idle_cpu(cpu);
688}
689
Suresh Siddhaca380622011-10-03 15:09:00 -0700690static inline bool got_nohz_idle_kick(void)
691{
Suresh Siddha1c792db2011-12-01 17:07:32 -0800692 int cpu = smp_processor_id();
Vincent Guittot873b4c62013-06-05 10:13:11 +0200693
694 if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)))
695 return false;
696
697 if (idle_cpu(cpu) && !need_resched())
698 return true;
699
700 /*
701 * We can't run Idle Load Balance on this CPU for this time so we
702 * cancel it and clear NOHZ_BALANCE_KICK
703 */
704 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu));
705 return false;
Suresh Siddhaca380622011-10-03 15:09:00 -0700706}
707
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200708#else /* CONFIG_NO_HZ_COMMON */
Suresh Siddhaca380622011-10-03 15:09:00 -0700709
710static inline bool got_nohz_idle_kick(void)
711{
712 return false;
713}
714
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200715#endif /* CONFIG_NO_HZ_COMMON */
Thomas Gleixner06d83082008-03-22 09:20:24 +0100716
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200717#ifdef CONFIG_NO_HZ_FULL
718bool sched_can_stop_tick(void)
719{
Frederic Weisbecker3882ec62014-03-18 22:54:04 +0100720 /*
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500721 * FIFO realtime policy runs the highest priority task. Other runnable
722 * tasks are of a lower priority. The scheduler tick does nothing.
723 */
724 if (current->policy == SCHED_FIFO)
725 return true;
726
727 /*
728 * Round-robin realtime tasks time slice with other tasks at the same
729 * realtime priority. Is this task the only one at this priority?
730 */
731 if (current->policy == SCHED_RR) {
732 struct sched_rt_entity *rt_se = &current->rt;
733
Geliang Tang01783e02015-11-15 18:18:40 +0800734 return list_is_singular(&rt_se->run_list);
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500735 }
736
737 /*
Frederic Weisbecker3882ec62014-03-18 22:54:04 +0100738 * More than one running task need preemption.
739 * nr_running update is assumed to be visible
740 * after IPI is sent from wakers.
741 */
Viresh Kumar541b8262014-06-24 14:04:12 +0530742 if (this_rq()->nr_running > 1)
743 return false;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200744
Viresh Kumar541b8262014-06-24 14:04:12 +0530745 return true;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200746}
747#endif /* CONFIG_NO_HZ_FULL */
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200748
Peter Zijlstra029632f2011-10-25 10:00:11 +0200749void sched_avg_update(struct rq *rq)
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200750{
751 s64 period = sched_avg_period();
752
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200753 while ((s64)(rq_clock(rq) - rq->age_stamp) > period) {
Will Deacon0d98bb22010-05-24 12:11:43 -0700754 /*
755 * Inline assembly required to prevent the compiler
756 * optimising this loop into a divmod call.
757 * See __iter_div_u64_rem() for another example of this.
758 */
759 asm("" : "+rm" (rq->age_stamp));
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200760 rq->age_stamp += period;
761 rq->rt_avg /= 2;
762 }
763}
764
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200765#endif /* CONFIG_SMP */
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200766
Paul Turnera790de92011-07-21 09:43:29 -0700767#if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
768 (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
Peter Zijlstraeb755802008-08-19 12:33:05 +0200769/*
Paul Turner82774342011-07-21 09:43:35 -0700770 * Iterate task_group tree rooted at *from, calling @down when first entering a
771 * node and @up when leaving it for the final time.
772 *
773 * Caller must hold rcu_lock or sufficient equivalent.
Peter Zijlstraeb755802008-08-19 12:33:05 +0200774 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200775int walk_tg_tree_from(struct task_group *from,
Paul Turner82774342011-07-21 09:43:35 -0700776 tg_visitor down, tg_visitor up, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200777{
778 struct task_group *parent, *child;
779 int ret;
780
Paul Turner82774342011-07-21 09:43:35 -0700781 parent = from;
782
Peter Zijlstraeb755802008-08-19 12:33:05 +0200783down:
784 ret = (*down)(parent, data);
785 if (ret)
Paul Turner82774342011-07-21 09:43:35 -0700786 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200787 list_for_each_entry_rcu(child, &parent->children, siblings) {
788 parent = child;
789 goto down;
790
791up:
792 continue;
793 }
794 ret = (*up)(parent, data);
Paul Turner82774342011-07-21 09:43:35 -0700795 if (ret || parent == from)
796 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200797
798 child = parent;
799 parent = parent->parent;
800 if (parent)
801 goto up;
Paul Turner82774342011-07-21 09:43:35 -0700802out:
Peter Zijlstraeb755802008-08-19 12:33:05 +0200803 return ret;
804}
805
Peter Zijlstra029632f2011-10-25 10:00:11 +0200806int tg_nop(struct task_group *tg, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200807{
808 return 0;
809}
810#endif
811
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200812static void set_load_weight(struct task_struct *p)
813{
Nikhil Raof05998d2011-05-18 10:09:38 -0700814 int prio = p->static_prio - MAX_RT_PRIO;
815 struct load_weight *load = &p->se.load;
816
Ingo Molnardd41f592007-07-09 18:51:59 +0200817 /*
818 * SCHED_IDLE tasks get minimal weight:
819 */
Henrik Austad20f9cd22015-09-09 17:00:41 +0200820 if (idle_policy(p->policy)) {
Nikhil Raoc8b28112011-05-18 14:37:48 -0700821 load->weight = scale_load(WEIGHT_IDLEPRIO);
Nikhil Raof05998d2011-05-18 10:09:38 -0700822 load->inv_weight = WMULT_IDLEPRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +0200823 return;
824 }
825
Andi Kleened82b8a2015-11-29 20:59:43 -0800826 load->weight = scale_load(sched_prio_to_weight[prio]);
827 load->inv_weight = sched_prio_to_wmult[prio];
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200828}
829
Peter Zijlstra1de64442015-09-30 17:44:13 +0200830static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -0600831{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100832 update_rq_clock(rq);
Peter Zijlstra1de64442015-09-30 17:44:13 +0200833 if (!(flags & ENQUEUE_RESTORE))
834 sched_info_queued(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100835 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +0200836}
837
Peter Zijlstra1de64442015-09-30 17:44:13 +0200838static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +0200839{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100840 update_rq_clock(rq);
Peter Zijlstra1de64442015-09-30 17:44:13 +0200841 if (!(flags & DEQUEUE_SAVE))
842 sched_info_dequeued(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100843 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200844}
845
Peter Zijlstra029632f2011-10-25 10:00:11 +0200846void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100847{
848 if (task_contributes_to_load(p))
849 rq->nr_uninterruptible--;
850
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100851 enqueue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100852}
853
Peter Zijlstra029632f2011-10-25 10:00:11 +0200854void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100855{
856 if (task_contributes_to_load(p))
857 rq->nr_uninterruptible++;
858
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100859 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100860}
861
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100862static void update_rq_clock_task(struct rq *rq, s64 delta)
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700863{
Glauber Costa095c0aa2011-07-11 15:28:18 -0400864/*
865 * In theory, the compile should just see 0 here, and optimize out the call
866 * to sched_rt_avg_update. But I don't trust it...
867 */
868#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
869 s64 steal = 0, irq_delta = 0;
870#endif
871#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Peter Zijlstra8e92c202010-12-09 14:15:34 +0100872 irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100873
874 /*
875 * Since irq_time is only updated on {soft,}irq_exit, we might run into
876 * this case when a previous update_rq_clock() happened inside a
877 * {soft,}irq region.
878 *
879 * When this happens, we stop ->clock_task and only update the
880 * prev_irq_time stamp to account for the part that fit, so that a next
881 * update will consume the rest. This ensures ->clock_task is
882 * monotonic.
883 *
884 * It does however cause some slight miss-attribution of {soft,}irq
885 * time, a more accurate solution would be to update the irq_time using
886 * the current rq->clock timestamp, except that would require using
887 * atomic ops.
888 */
889 if (irq_delta > delta)
890 irq_delta = delta;
891
892 rq->prev_irq_time += irq_delta;
893 delta -= irq_delta;
Glauber Costa095c0aa2011-07-11 15:28:18 -0400894#endif
895#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
Ingo Molnarc5905af2012-02-24 08:31:31 +0100896 if (static_key_false((&paravirt_steal_rq_enabled))) {
Glauber Costa095c0aa2011-07-11 15:28:18 -0400897 steal = paravirt_steal_clock(cpu_of(rq));
898 steal -= rq->prev_steal_time_rq;
899
900 if (unlikely(steal > delta))
901 steal = delta;
902
Glauber Costa095c0aa2011-07-11 15:28:18 -0400903 rq->prev_steal_time_rq += steal;
Glauber Costa095c0aa2011-07-11 15:28:18 -0400904 delta -= steal;
905 }
906#endif
907
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100908 rq->clock_task += delta;
909
Glauber Costa095c0aa2011-07-11 15:28:18 -0400910#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -0400911 if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
Glauber Costa095c0aa2011-07-11 15:28:18 -0400912 sched_rt_avg_update(rq, irq_delta + steal);
913#endif
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700914}
915
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200916void sched_set_stop_task(int cpu, struct task_struct *stop)
917{
918 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
919 struct task_struct *old_stop = cpu_rq(cpu)->stop;
920
921 if (stop) {
922 /*
923 * Make it appear like a SCHED_FIFO task, its something
924 * userspace knows about and won't get confused about.
925 *
926 * Also, it will make PI more or less work without too
927 * much confusion -- but then, stop work should not
928 * rely on PI working anyway.
929 */
930 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
931
932 stop->sched_class = &stop_sched_class;
933 }
934
935 cpu_rq(cpu)->stop = stop;
936
937 if (old_stop) {
938 /*
939 * Reset it back to a normal scheduling class so that
940 * it can die in pieces.
941 */
942 old_stop->sched_class = &rt_sched_class;
943 }
944}
945
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100946/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200947 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200948 */
Ingo Molnar14531182007-07-09 18:51:59 +0200949static inline int __normal_prio(struct task_struct *p)
950{
Ingo Molnardd41f592007-07-09 18:51:59 +0200951 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +0200952}
953
954/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700955 * Calculate the expected normal priority: i.e. priority
956 * without taking RT-inheritance into account. Might be
957 * boosted by interactivity modifiers. Changes upon fork,
958 * setprio syscalls, and whenever the interactivity
959 * estimator recalculates.
960 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700961static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700962{
963 int prio;
964
Dario Faggioliaab03e02013-11-28 11:14:43 +0100965 if (task_has_dl_policy(p))
966 prio = MAX_DL_PRIO-1;
967 else if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -0700968 prio = MAX_RT_PRIO-1 - p->rt_priority;
969 else
970 prio = __normal_prio(p);
971 return prio;
972}
973
974/*
975 * Calculate the current priority, i.e. the priority
976 * taken into account by the scheduler. This value might
977 * be boosted by RT tasks, or might be boosted by
978 * interactivity modifiers. Will be RT if the task got
979 * RT-boosted. If not then it returns p->normal_prio.
980 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700981static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700982{
983 p->normal_prio = normal_prio(p);
984 /*
985 * If we are RT tasks or we were boosted to RT priority,
986 * keep the priority unchanged. Otherwise, update priority
987 * to the normal priority:
988 */
989 if (!rt_prio(p->prio))
990 return p->normal_prio;
991 return p->prio;
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/**
995 * task_curr - is this task currently executing on a CPU?
996 * @p: the task in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +0200997 *
998 * Return: 1 if the task is currently executing. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001000inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 return cpu_curr(task_cpu(p)) == p;
1003}
1004
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001005/*
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001006 * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock,
1007 * use the balance_callback list if you want balancing.
1008 *
1009 * this means any call to check_class_changed() must be followed by a call to
1010 * balance_callback().
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001011 */
Steven Rostedtcb469842008-01-25 21:08:22 +01001012static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1013 const struct sched_class *prev_class,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001014 int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01001015{
1016 if (prev_class != p->sched_class) {
1017 if (prev_class->switched_from)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001018 prev_class->switched_from(rq, p);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001019
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001020 p->sched_class->switched_to(rq, p);
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001021 } else if (oldprio != p->prio || dl_task(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001022 p->sched_class->prio_changed(rq, p, oldprio);
Steven Rostedtcb469842008-01-25 21:08:22 +01001023}
1024
Peter Zijlstra029632f2011-10-25 10:00:11 +02001025void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001026{
1027 const struct sched_class *class;
1028
1029 if (p->sched_class == rq->curr->sched_class) {
1030 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
1031 } else {
1032 for_each_class(class) {
1033 if (class == rq->curr->sched_class)
1034 break;
1035 if (class == p->sched_class) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001036 resched_curr(rq);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001037 break;
1038 }
1039 }
1040 }
1041
1042 /*
1043 * A queue event has occurred, and we're going to schedule. In
1044 * this case, we can save a useless back to back clock update.
1045 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001046 if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01001047 rq_clock_skip_update(rq, true);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#ifdef CONFIG_SMP
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001051/*
1052 * This is how migration works:
1053 *
1054 * 1) we invoke migration_cpu_stop() on the target CPU using
1055 * stop_one_cpu().
1056 * 2) stopper starts to run (implicitly forcing the migrated thread
1057 * off the CPU)
1058 * 3) it checks whether the migrated task is still in the wrong runqueue.
1059 * 4) if it's in the wrong runqueue then the migration thread removes
1060 * it and puts it into the right queue.
1061 * 5) stopper completes and stop_one_cpu() returns and the migration
1062 * is done.
1063 */
1064
1065/*
1066 * move_queued_task - move a queued task to new rq.
1067 *
1068 * Returns (locked) new rq. Old rq's lock is released.
1069 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001070static struct rq *move_queued_task(struct rq *rq, struct task_struct *p, int new_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001071{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001072 lockdep_assert_held(&rq->lock);
1073
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001074 p->on_rq = TASK_ON_RQ_MIGRATING;
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001075 dequeue_task(rq, p, 0);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001076 set_task_cpu(p, new_cpu);
1077 raw_spin_unlock(&rq->lock);
1078
1079 rq = cpu_rq(new_cpu);
1080
1081 raw_spin_lock(&rq->lock);
1082 BUG_ON(task_cpu(p) != new_cpu);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001083 enqueue_task(rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001084 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001085 check_preempt_curr(rq, p, 0);
1086
1087 return rq;
1088}
1089
1090struct migration_arg {
1091 struct task_struct *task;
1092 int dest_cpu;
1093};
1094
1095/*
1096 * Move (not current) task off this cpu, onto dest cpu. We're doing
1097 * this because either it can't run here any more (set_cpus_allowed()
1098 * away from this CPU, or CPU going down), or because we're
1099 * attempting to rebalance this task on exec (sched_exec).
1100 *
1101 * So we race with normal scheduler movements, but that's OK, as long
1102 * as the task is no longer on this CPU.
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001103 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001104static struct rq *__migrate_task(struct rq *rq, struct task_struct *p, int dest_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001105{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001106 if (unlikely(!cpu_active(dest_cpu)))
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001107 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001108
1109 /* Affinity changed (again). */
1110 if (!cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001111 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001112
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001113 rq = move_queued_task(rq, p, dest_cpu);
1114
1115 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001116}
1117
1118/*
1119 * migration_cpu_stop - this will be executed by a highprio stopper thread
1120 * and performs thread migration by bumping thread off CPU then
1121 * 'pushing' onto another runqueue.
1122 */
1123static int migration_cpu_stop(void *data)
1124{
1125 struct migration_arg *arg = data;
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001126 struct task_struct *p = arg->task;
1127 struct rq *rq = this_rq();
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001128
1129 /*
1130 * The original target cpu might have gone down and we might
1131 * be on another cpu but it doesn't matter.
1132 */
1133 local_irq_disable();
1134 /*
1135 * We need to explicitly wake pending tasks before running
1136 * __migrate_task() such that we will not miss enforcing cpus_allowed
1137 * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
1138 */
1139 sched_ttwu_pending();
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001140
1141 raw_spin_lock(&p->pi_lock);
1142 raw_spin_lock(&rq->lock);
1143 /*
1144 * If task_rq(p) != rq, it cannot be migrated here, because we're
1145 * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
1146 * we're holding p->pi_lock.
1147 */
1148 if (task_rq(p) == rq && task_on_rq_queued(p))
1149 rq = __migrate_task(rq, p, arg->dest_cpu);
1150 raw_spin_unlock(&rq->lock);
1151 raw_spin_unlock(&p->pi_lock);
1152
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001153 local_irq_enable();
1154 return 0;
1155}
1156
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001157/*
1158 * sched_class::set_cpus_allowed must do the below, but is not required to
1159 * actually call this function.
1160 */
1161void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001162{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001163 cpumask_copy(&p->cpus_allowed, new_mask);
1164 p->nr_cpus_allowed = cpumask_weight(new_mask);
1165}
1166
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001167void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
1168{
Peter Zijlstra6c370672015-05-15 17:43:36 +02001169 struct rq *rq = task_rq(p);
1170 bool queued, running;
1171
Peter Zijlstra25834c72015-05-15 17:43:34 +02001172 lockdep_assert_held(&p->pi_lock);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001173
1174 queued = task_on_rq_queued(p);
1175 running = task_current(rq, p);
1176
1177 if (queued) {
1178 /*
1179 * Because __kthread_bind() calls this on blocked tasks without
1180 * holding rq->lock.
1181 */
1182 lockdep_assert_held(&rq->lock);
Peter Zijlstra1de64442015-09-30 17:44:13 +02001183 dequeue_task(rq, p, DEQUEUE_SAVE);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001184 }
1185 if (running)
1186 put_prev_task(rq, p);
1187
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001188 p->sched_class->set_cpus_allowed(p, new_mask);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001189
1190 if (running)
1191 p->sched_class->set_curr_task(rq);
1192 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02001193 enqueue_task(rq, p, ENQUEUE_RESTORE);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001194}
1195
1196/*
1197 * Change a given task's CPU affinity. Migrate the thread to a
1198 * proper CPU and schedule it away if the CPU it's executing on
1199 * is removed from the allowed bitmask.
1200 *
1201 * NOTE: the caller must have a valid reference to the task, the
1202 * task must not exit() & deallocate itself prematurely. The
1203 * call is not atomic; no spinlocks may be held.
1204 */
Peter Zijlstra25834c72015-05-15 17:43:34 +02001205static int __set_cpus_allowed_ptr(struct task_struct *p,
1206 const struct cpumask *new_mask, bool check)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001207{
1208 unsigned long flags;
1209 struct rq *rq;
1210 unsigned int dest_cpu;
1211 int ret = 0;
1212
1213 rq = task_rq_lock(p, &flags);
1214
Peter Zijlstra25834c72015-05-15 17:43:34 +02001215 /*
1216 * Must re-check here, to close a race against __kthread_bind(),
1217 * sched_setaffinity() is not guaranteed to observe the flag.
1218 */
1219 if (check && (p->flags & PF_NO_SETAFFINITY)) {
1220 ret = -EINVAL;
1221 goto out;
1222 }
1223
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001224 if (cpumask_equal(&p->cpus_allowed, new_mask))
1225 goto out;
1226
1227 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
1228 ret = -EINVAL;
1229 goto out;
1230 }
1231
1232 do_set_cpus_allowed(p, new_mask);
1233
1234 /* Can the task run on the task's current CPU? If so, we're done */
1235 if (cpumask_test_cpu(task_cpu(p), new_mask))
1236 goto out;
1237
1238 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
1239 if (task_running(rq, p) || p->state == TASK_WAKING) {
1240 struct migration_arg arg = { p, dest_cpu };
1241 /* Need help from migration thread: drop lock and wait. */
1242 task_rq_unlock(rq, p, &flags);
1243 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
1244 tlb_migrate_finish(p->mm);
1245 return 0;
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001246 } else if (task_on_rq_queued(p)) {
1247 /*
1248 * OK, since we're going to drop the lock immediately
1249 * afterwards anyway.
1250 */
1251 lockdep_unpin_lock(&rq->lock);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001252 rq = move_queued_task(rq, p, dest_cpu);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001253 lockdep_pin_lock(&rq->lock);
1254 }
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001255out:
1256 task_rq_unlock(rq, p, &flags);
1257
1258 return ret;
1259}
Peter Zijlstra25834c72015-05-15 17:43:34 +02001260
1261int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1262{
1263 return __set_cpus_allowed_ptr(p, new_mask, false);
1264}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001265EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
1266
Ingo Molnardd41f592007-07-09 18:51:59 +02001267void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001268{
Peter Zijlstrae2912002009-12-16 18:04:36 +01001269#ifdef CONFIG_SCHED_DEBUG
1270 /*
1271 * We should never call set_task_cpu() on a blocked task,
1272 * ttwu() will sort out the placement.
1273 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01001274 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
Oleg Nesterove2336f62014-10-08 20:33:48 +02001275 !p->on_rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001276
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001277 /*
1278 * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING,
1279 * because schedstat_wait_{start,end} rebase migrating task's wait_start
1280 * time relying on p->on_rq.
1281 */
1282 WARN_ON_ONCE(p->state == TASK_RUNNING &&
1283 p->sched_class == &fair_sched_class &&
1284 (p->on_rq && !task_on_rq_migrating(p)));
1285
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001286#ifdef CONFIG_LOCKDEP
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001287 /*
1288 * The caller should hold either p->pi_lock or rq->lock, when changing
1289 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
1290 *
1291 * sched_move_task() holds both and thus holding either pins the cgroup,
Peter Zijlstra8323f262012-06-22 13:36:05 +02001292 * see task_group().
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001293 *
1294 * Furthermore, all task_rq users should acquire both locks, see
1295 * task_rq_lock().
1296 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001297 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
1298 lockdep_is_held(&task_rq(p)->lock)));
1299#endif
Peter Zijlstrae2912002009-12-16 18:04:36 +01001300#endif
1301
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08001302 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001303
Peter Zijlstra0c697742009-12-22 15:43:19 +01001304 if (task_cpu(p) != new_cpu) {
Paul Turner0a74bef2012-10-04 13:18:30 +02001305 if (p->sched_class->migrate_task_rq)
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08001306 p->sched_class->migrate_task_rq(p);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001307 p->se.nr_migrations++;
Peter Zijlstraff303e62015-04-17 20:05:30 +02001308 perf_event_task_migrate(p);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001309 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001310
1311 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001312}
1313
Peter Zijlstraac66f542013-10-07 11:29:16 +01001314static void __migrate_swap_task(struct task_struct *p, int cpu)
1315{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001316 if (task_on_rq_queued(p)) {
Peter Zijlstraac66f542013-10-07 11:29:16 +01001317 struct rq *src_rq, *dst_rq;
1318
1319 src_rq = task_rq(p);
1320 dst_rq = cpu_rq(cpu);
1321
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001322 p->on_rq = TASK_ON_RQ_MIGRATING;
Peter Zijlstraac66f542013-10-07 11:29:16 +01001323 deactivate_task(src_rq, p, 0);
1324 set_task_cpu(p, cpu);
1325 activate_task(dst_rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001326 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstraac66f542013-10-07 11:29:16 +01001327 check_preempt_curr(dst_rq, p, 0);
1328 } else {
1329 /*
1330 * Task isn't running anymore; make it appear like we migrated
1331 * it before it went to sleep. This means on wakeup we make the
1332 * previous cpu our targer instead of where it really is.
1333 */
1334 p->wake_cpu = cpu;
1335 }
1336}
1337
1338struct migration_swap_arg {
1339 struct task_struct *src_task, *dst_task;
1340 int src_cpu, dst_cpu;
1341};
1342
1343static int migrate_swap_stop(void *data)
1344{
1345 struct migration_swap_arg *arg = data;
1346 struct rq *src_rq, *dst_rq;
1347 int ret = -EAGAIN;
1348
Peter Zijlstra62694cd2015-10-09 18:36:29 +02001349 if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu))
1350 return -EAGAIN;
1351
Peter Zijlstraac66f542013-10-07 11:29:16 +01001352 src_rq = cpu_rq(arg->src_cpu);
1353 dst_rq = cpu_rq(arg->dst_cpu);
1354
Peter Zijlstra74602312013-10-10 20:17:22 +02001355 double_raw_lock(&arg->src_task->pi_lock,
1356 &arg->dst_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001357 double_rq_lock(src_rq, dst_rq);
Peter Zijlstra62694cd2015-10-09 18:36:29 +02001358
Peter Zijlstraac66f542013-10-07 11:29:16 +01001359 if (task_cpu(arg->dst_task) != arg->dst_cpu)
1360 goto unlock;
1361
1362 if (task_cpu(arg->src_task) != arg->src_cpu)
1363 goto unlock;
1364
1365 if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task)))
1366 goto unlock;
1367
1368 if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task)))
1369 goto unlock;
1370
1371 __migrate_swap_task(arg->src_task, arg->dst_cpu);
1372 __migrate_swap_task(arg->dst_task, arg->src_cpu);
1373
1374 ret = 0;
1375
1376unlock:
1377 double_rq_unlock(src_rq, dst_rq);
Peter Zijlstra74602312013-10-10 20:17:22 +02001378 raw_spin_unlock(&arg->dst_task->pi_lock);
1379 raw_spin_unlock(&arg->src_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001380
1381 return ret;
1382}
1383
1384/*
1385 * Cross migrate two tasks
1386 */
1387int migrate_swap(struct task_struct *cur, struct task_struct *p)
1388{
1389 struct migration_swap_arg arg;
1390 int ret = -EINVAL;
1391
Peter Zijlstraac66f542013-10-07 11:29:16 +01001392 arg = (struct migration_swap_arg){
1393 .src_task = cur,
1394 .src_cpu = task_cpu(cur),
1395 .dst_task = p,
1396 .dst_cpu = task_cpu(p),
1397 };
1398
1399 if (arg.src_cpu == arg.dst_cpu)
1400 goto out;
1401
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02001402 /*
1403 * These three tests are all lockless; this is OK since all of them
1404 * will be re-checked with proper locks held further down the line.
1405 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01001406 if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
1407 goto out;
1408
1409 if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task)))
1410 goto out;
1411
1412 if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task)))
1413 goto out;
1414
Mel Gorman286549d2014-01-21 15:51:03 -08001415 trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001416 ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
1417
1418out:
Peter Zijlstraac66f542013-10-07 11:29:16 +01001419 return ret;
1420}
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 * wait_task_inactive - wait for a thread to unschedule.
1424 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001425 * If @match_state is nonzero, it's the @p->state value just checked and
1426 * not expected to change. If it changes, i.e. @p might have woken up,
1427 * then return zero. When we succeed in waiting for @p to be off its CPU,
1428 * we return a positive number (its total switch count). If a second call
1429 * a short while later returns the same number, the caller can be sure that
1430 * @p has remained unscheduled the whole time.
1431 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 * The caller must ensure that the task *will* unschedule sometime soon,
1433 * else this function might spin for a *long* time. This function can't
1434 * be called with interrupts off, or it may introduce deadlock with
1435 * smp_call_function() if an IPI is sent by the same process we are
1436 * waiting to become inactive.
1437 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001438unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001441 int running, queued;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001442 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001443 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Andi Kleen3a5c3592007-10-15 17:00:14 +02001445 for (;;) {
1446 /*
1447 * We do the initial early heuristics without holding
1448 * any task-queue locks at all. We'll only try to get
1449 * the runqueue lock when things look like they will
1450 * work out!
1451 */
1452 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001453
Andi Kleen3a5c3592007-10-15 17:00:14 +02001454 /*
1455 * If the task is actively running on another CPU
1456 * still, just relax and busy-wait without holding
1457 * any locks.
1458 *
1459 * NOTE! Since we don't hold any locks, it's not
1460 * even sure that "rq" stays as the right runqueue!
1461 * But we don't care, since "task_running()" will
1462 * return false if the runqueue has changed and p
1463 * is actually now running somewhere else!
1464 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001465 while (task_running(rq, p)) {
1466 if (match_state && unlikely(p->state != match_state))
1467 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02001468 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07001469 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001470
Andi Kleen3a5c3592007-10-15 17:00:14 +02001471 /*
1472 * Ok, time to look more closely! We need the rq
1473 * lock now, to be *sure*. If we're wrong, we'll
1474 * just go back and repeat.
1475 */
1476 rq = task_rq_lock(p, &flags);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02001477 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001478 running = task_running(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001479 queued = task_on_rq_queued(p);
Roland McGrath85ba2d82008-07-25 19:45:58 -07001480 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07001481 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07001482 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001483 task_rq_unlock(rq, p, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001484
Andi Kleen3a5c3592007-10-15 17:00:14 +02001485 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07001486 * If it changed from the expected state, bail out now.
1487 */
1488 if (unlikely(!ncsw))
1489 break;
1490
1491 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02001492 * Was it really running after all now that we
1493 * checked with the proper locks actually held?
1494 *
1495 * Oops. Go back and try again..
1496 */
1497 if (unlikely(running)) {
1498 cpu_relax();
1499 continue;
1500 }
1501
1502 /*
1503 * It's not enough that it's not actively running,
1504 * it must be off the runqueue _entirely_, and not
1505 * preempted!
1506 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00001507 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02001508 * running right now), it's preempted, and we should
1509 * yield - it could be a while.
1510 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001511 if (unlikely(queued)) {
Thomas Gleixner8eb90c32011-02-23 23:52:21 +00001512 ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
1513
1514 set_current_state(TASK_UNINTERRUPTIBLE);
1515 schedule_hrtimeout(&to, HRTIMER_MODE_REL);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001516 continue;
1517 }
1518
1519 /*
1520 * Ahh, all good. It wasn't running, and it wasn't
1521 * runnable, which means that it will never become
1522 * running in the future either. We're all done!
1523 */
1524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07001526
1527 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
1530/***
1531 * kick_process - kick a running thread to enter/exit the kernel
1532 * @p: the to-be-kicked thread
1533 *
1534 * Cause a process which is running on another CPU to enter
1535 * kernel-mode, without any delay. (to get signals handled.)
1536 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001537 * NOTE: this function doesn't have to take the runqueue lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 * because all it wants to ensure is that the remote task enters
1539 * the kernel. If the IPI races and the task has been migrated
1540 * to another CPU then no harm is done and the purpose has been
1541 * achieved as well.
1542 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001543void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 int cpu;
1546
1547 preempt_disable();
1548 cpu = task_cpu(p);
1549 if ((cpu != smp_processor_id()) && task_curr(p))
1550 smp_send_reschedule(cpu);
1551 preempt_enable();
1552}
Rusty Russellb43e3522009-06-12 22:27:00 -06001553EXPORT_SYMBOL_GPL(kick_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Oleg Nesterov30da6882010-03-15 10:10:19 +01001555/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001556 * ->cpus_allowed is protected by both rq->lock and p->pi_lock
Oleg Nesterov30da6882010-03-15 10:10:19 +01001557 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001558static int select_fallback_rq(int cpu, struct task_struct *p)
1559{
Tang Chenaa00d892013-02-22 16:33:33 -08001560 int nid = cpu_to_node(cpu);
1561 const struct cpumask *nodemask = NULL;
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001562 enum { cpuset, possible, fail } state = cpuset;
1563 int dest_cpu;
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001564
Tang Chenaa00d892013-02-22 16:33:33 -08001565 /*
1566 * If the node that the cpu is on has been offlined, cpu_to_node()
1567 * will return -1. There is no cpu on the node, and we should
1568 * select the cpu on the other node.
1569 */
1570 if (nid != -1) {
1571 nodemask = cpumask_of_node(nid);
1572
1573 /* Look for allowed, online CPU in same node. */
1574 for_each_cpu(dest_cpu, nodemask) {
1575 if (!cpu_online(dest_cpu))
1576 continue;
1577 if (!cpu_active(dest_cpu))
1578 continue;
1579 if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
1580 return dest_cpu;
1581 }
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001582 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001583
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001584 for (;;) {
1585 /* Any allowed, online CPU? */
Srivatsa S. Bhate3831ed2012-03-30 19:40:28 +05301586 for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) {
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001587 if (!cpu_online(dest_cpu))
1588 continue;
1589 if (!cpu_active(dest_cpu))
1590 continue;
1591 goto out;
1592 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001593
Oleg Nesterove73e85f2015-10-10 20:53:15 +02001594 /* No more Mr. Nice Guy. */
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001595 switch (state) {
1596 case cpuset:
Oleg Nesterove73e85f2015-10-10 20:53:15 +02001597 if (IS_ENABLED(CONFIG_CPUSETS)) {
1598 cpuset_cpus_allowed_fallback(p);
1599 state = possible;
1600 break;
1601 }
1602 /* fall-through */
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001603 case possible:
1604 do_set_cpus_allowed(p, cpu_possible_mask);
1605 state = fail;
1606 break;
1607
1608 case fail:
1609 BUG();
1610 break;
1611 }
1612 }
1613
1614out:
1615 if (state != cpuset) {
1616 /*
1617 * Don't tell them about moving exiting tasks or
1618 * kernel threads (both mm NULL), since they never
1619 * leave kernel.
1620 */
1621 if (p->mm && printk_ratelimit()) {
John Stultzaac74dc2014-06-04 16:11:40 -07001622 printk_deferred("process %d (%s) no longer affine to cpu%d\n",
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001623 task_pid_nr(p), p->comm, cpu);
1624 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001625 }
1626
1627 return dest_cpu;
1628}
1629
Peter Zijlstrae2912002009-12-16 18:04:36 +01001630/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001631 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01001632 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001633static inline
Peter Zijlstraac66f542013-10-07 11:29:16 +01001634int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001635{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001636 lockdep_assert_held(&p->pi_lock);
1637
Wanpeng Li6c1d9412014-11-05 09:14:37 +08001638 if (p->nr_cpus_allowed > 1)
1639 cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01001640
1641 /*
1642 * In order not to call set_task_cpu() on a blocking task we need
1643 * to rely on ttwu() to place the task on a valid ->cpus_allowed
1644 * cpu.
1645 *
1646 * Since this is common to all placement strategies, this lives here.
1647 *
1648 * [ this allows ->select_task() to simply return task_cpu(p) and
1649 * not worry about this generic constraint ]
1650 */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001651 if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01001652 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001653 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01001654
1655 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001656}
Mike Galbraith09a40af2010-04-15 07:29:59 +02001657
1658static void update_avg(u64 *avg, u64 sample)
1659{
1660 s64 diff = sample - *avg;
1661 *avg += diff >> 3;
1662}
Peter Zijlstra25834c72015-05-15 17:43:34 +02001663
1664#else
1665
1666static inline int __set_cpus_allowed_ptr(struct task_struct *p,
1667 const struct cpumask *new_mask, bool check)
1668{
1669 return set_cpus_allowed_ptr(p, new_mask);
1670}
1671
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001672#endif /* CONFIG_SMP */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001673
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001674static void
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001675ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001676{
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001677#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001678 struct rq *rq = this_rq();
Tejun Heo9ed38112009-12-03 15:08:03 +09001679
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001680#ifdef CONFIG_SMP
1681 int this_cpu = smp_processor_id();
Tejun Heo9ed38112009-12-03 15:08:03 +09001682
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001683 if (cpu == this_cpu) {
1684 schedstat_inc(rq, ttwu_local);
1685 schedstat_inc(p, se.statistics.nr_wakeups_local);
1686 } else {
1687 struct sched_domain *sd;
1688
1689 schedstat_inc(p, se.statistics.nr_wakeups_remote);
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001690 rcu_read_lock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001691 for_each_domain(this_cpu, sd) {
1692 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
1693 schedstat_inc(sd, ttwu_wake_remote);
1694 break;
1695 }
1696 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001697 rcu_read_unlock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001698 }
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001699
1700 if (wake_flags & WF_MIGRATED)
1701 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
1702
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001703#endif /* CONFIG_SMP */
1704
1705 schedstat_inc(rq, ttwu_count);
1706 schedstat_inc(p, se.statistics.nr_wakeups);
1707
1708 if (wake_flags & WF_SYNC)
1709 schedstat_inc(p, se.statistics.nr_wakeups_sync);
1710
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001711#endif /* CONFIG_SCHEDSTATS */
Tejun Heo9ed38112009-12-03 15:08:03 +09001712}
1713
Peter Zijlstra1de64442015-09-30 17:44:13 +02001714static inline void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001715{
Tejun Heo9ed38112009-12-03 15:08:03 +09001716 activate_task(rq, p, en_flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001717 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrac2f71152011-04-13 13:28:56 +02001718
1719 /* if a worker is waking up, notify workqueue */
1720 if (p->flags & PF_WQ_WORKER)
1721 wq_worker_waking_up(p, cpu_of(rq));
Tejun Heo9ed38112009-12-03 15:08:03 +09001722}
1723
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001724/*
1725 * Mark the task runnable and perform wakeup-preemption.
1726 */
Peter Zijlstra89363382011-04-05 17:23:42 +02001727static void
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001728ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001729{
Tejun Heo9ed38112009-12-03 15:08:03 +09001730 check_preempt_curr(rq, p, wake_flags);
Tejun Heo9ed38112009-12-03 15:08:03 +09001731 p->state = TASK_RUNNING;
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02001732 trace_sched_wakeup(p);
1733
Tejun Heo9ed38112009-12-03 15:08:03 +09001734#ifdef CONFIG_SMP
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001735 if (p->sched_class->task_woken) {
1736 /*
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001737 * Our task @p is fully woken up and running; so its safe to
1738 * drop the rq->lock, hereafter rq is only used for statistics.
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001739 */
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001740 lockdep_unpin_lock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09001741 p->sched_class->task_woken(rq, p);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001742 lockdep_pin_lock(&rq->lock);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001743 }
Tejun Heo9ed38112009-12-03 15:08:03 +09001744
Steven Rostedte69c6342010-12-06 17:10:31 -05001745 if (rq->idle_stamp) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001746 u64 delta = rq_clock(rq) - rq->idle_stamp;
Jason Low9bd721c2013-09-13 11:26:52 -07001747 u64 max = 2*rq->max_idle_balance_cost;
Tejun Heo9ed38112009-12-03 15:08:03 +09001748
Jason Lowabfafa52013-09-13 11:26:51 -07001749 update_avg(&rq->avg_idle, delta);
1750
1751 if (rq->avg_idle > max)
Tejun Heo9ed38112009-12-03 15:08:03 +09001752 rq->avg_idle = max;
Jason Lowabfafa52013-09-13 11:26:51 -07001753
Tejun Heo9ed38112009-12-03 15:08:03 +09001754 rq->idle_stamp = 0;
1755 }
1756#endif
1757}
1758
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001759static void
1760ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)
1761{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001762 lockdep_assert_held(&rq->lock);
1763
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001764#ifdef CONFIG_SMP
1765 if (p->sched_contributes_to_load)
1766 rq->nr_uninterruptible--;
1767#endif
1768
1769 ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);
1770 ttwu_do_wakeup(rq, p, wake_flags);
1771}
1772
1773/*
1774 * Called in case the task @p isn't fully descheduled from its runqueue,
1775 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
1776 * since all we need to do is flip p->state to TASK_RUNNING, since
1777 * the task is still ->on_rq.
1778 */
1779static int ttwu_remote(struct task_struct *p, int wake_flags)
1780{
1781 struct rq *rq;
1782 int ret = 0;
1783
1784 rq = __task_rq_lock(p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001785 if (task_on_rq_queued(p)) {
Frederic Weisbecker1ad4ec02013-04-12 01:51:00 +02001786 /* check_preempt_curr() may use rq clock */
1787 update_rq_clock(rq);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001788 ttwu_do_wakeup(rq, p, wake_flags);
1789 ret = 1;
1790 }
1791 __task_rq_unlock(rq);
1792
1793 return ret;
1794}
1795
Peter Zijlstra317f3942011-04-05 17:23:58 +02001796#ifdef CONFIG_SMP
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001797void sched_ttwu_pending(void)
Peter Zijlstra317f3942011-04-05 17:23:58 +02001798{
1799 struct rq *rq = this_rq();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001800 struct llist_node *llist = llist_del_all(&rq->wake_list);
1801 struct task_struct *p;
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001802 unsigned long flags;
Peter Zijlstra317f3942011-04-05 17:23:58 +02001803
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001804 if (!llist)
1805 return;
1806
1807 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001808 lockdep_pin_lock(&rq->lock);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001809
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001810 while (llist) {
1811 p = llist_entry(llist, struct task_struct, wake_entry);
1812 llist = llist_next(llist);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001813 ttwu_do_activate(rq, p, 0);
1814 }
1815
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001816 lockdep_unpin_lock(&rq->lock);
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001817 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001818}
1819
1820void scheduler_ipi(void)
1821{
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001822 /*
1823 * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
1824 * TIF_NEED_RESCHED remotely (for the first time) will also send
1825 * this IPI.
1826 */
Peter Zijlstra8cb75e02013-11-20 12:22:37 +01001827 preempt_fold_need_resched();
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001828
Frederic Weisbeckerfd2ac4f2014-03-18 21:12:53 +01001829 if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001830 return;
1831
1832 /*
1833 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
1834 * traditionally all their work was done from the interrupt return
1835 * path. Now that we actually do some work, we need to make sure
1836 * we do call them.
1837 *
1838 * Some archs already do call them, luckily irq_enter/exit nest
1839 * properly.
1840 *
1841 * Arguably we should visit all archs and update all handlers,
1842 * however a fair share of IPIs are still resched only so this would
1843 * somewhat pessimize the simple resched case.
1844 */
1845 irq_enter();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001846 sched_ttwu_pending();
Suresh Siddhaca380622011-10-03 15:09:00 -07001847
1848 /*
1849 * Check if someone kicked us for doing the nohz idle load balance.
1850 */
Vincent Guittot873b4c62013-06-05 10:13:11 +02001851 if (unlikely(got_nohz_idle_kick())) {
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001852 this_rq()->idle_balance = 1;
Suresh Siddhaca380622011-10-03 15:09:00 -07001853 raise_softirq_irqoff(SCHED_SOFTIRQ);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001854 }
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001855 irq_exit();
Peter Zijlstra317f3942011-04-05 17:23:58 +02001856}
1857
1858static void ttwu_queue_remote(struct task_struct *p, int cpu)
1859{
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001860 struct rq *rq = cpu_rq(cpu);
1861
1862 if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) {
1863 if (!set_nr_if_polling(rq->idle))
1864 smp_send_reschedule(cpu);
1865 else
1866 trace_sched_wake_idle_without_ipi(cpu);
1867 }
Peter Zijlstra317f3942011-04-05 17:23:58 +02001868}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001869
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001870void wake_up_if_idle(int cpu)
1871{
1872 struct rq *rq = cpu_rq(cpu);
1873 unsigned long flags;
1874
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001875 rcu_read_lock();
1876
1877 if (!is_idle_task(rcu_dereference(rq->curr)))
1878 goto out;
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001879
1880 if (set_nr_if_polling(rq->idle)) {
1881 trace_sched_wake_idle_without_ipi(cpu);
1882 } else {
1883 raw_spin_lock_irqsave(&rq->lock, flags);
1884 if (is_idle_task(rq->curr))
1885 smp_send_reschedule(cpu);
1886 /* Else cpu is not in idle, do nothing here */
1887 raw_spin_unlock_irqrestore(&rq->lock, flags);
1888 }
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001889
1890out:
1891 rcu_read_unlock();
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001892}
1893
Peter Zijlstra39be3502012-01-26 12:44:34 +01001894bool cpus_share_cache(int this_cpu, int that_cpu)
Peter Zijlstra518cd622011-12-07 15:07:31 +01001895{
1896 return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
1897}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001898#endif /* CONFIG_SMP */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001899
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001900static void ttwu_queue(struct task_struct *p, int cpu)
1901{
1902 struct rq *rq = cpu_rq(cpu);
1903
Daniel Hellstrom17d9f312011-05-20 04:01:10 +00001904#if defined(CONFIG_SMP)
Peter Zijlstra39be3502012-01-26 12:44:34 +01001905 if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
Peter Zijlstraf01114c2011-05-31 12:26:55 +02001906 sched_clock_cpu(cpu); /* sync clocks x-cpu */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001907 ttwu_queue_remote(p, cpu);
1908 return;
1909 }
1910#endif
1911
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001912 raw_spin_lock(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001913 lockdep_pin_lock(&rq->lock);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001914 ttwu_do_activate(rq, p, 0);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001915 lockdep_unpin_lock(&rq->lock);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001916 raw_spin_unlock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09001917}
1918
1919/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09001921 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09001923 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 *
1925 * Put it on the run-queue if it's not already there. The "current"
1926 * thread is always on the run-queue (except when the actual
1927 * re-schedule is in progress), and as such you're allowed to do
1928 * the simpler "current->state = TASK_RUNNING" to mark yourself
1929 * runnable without the overhead of this.
1930 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02001931 * Return: %true if @p was woken up, %false if it was already running.
Tejun Heo9ed38112009-12-03 15:08:03 +09001932 * or @state didn't match @p's state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001934static int
1935try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 unsigned long flags;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001938 int cpu, success = 0;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001939
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02001940 /*
1941 * If we are going to wake up a thread waiting for CONDITION we
1942 * need to ensure that CONDITION=1 done by the caller can not be
1943 * reordered with p->state check below. This pairs with mb() in
1944 * set_current_state() the waiting thread does.
1945 */
1946 smp_mb__before_spinlock();
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001947 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001948 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 goto out;
1950
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02001951 trace_sched_waking(p);
1952
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001953 success = 1; /* we're going to change ->state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 cpu = task_cpu(p);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001955
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001956 if (p->on_rq && ttwu_remote(p, wake_flags))
1957 goto stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959#ifdef CONFIG_SMP
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001960 /*
Peter Zijlstraecf7d012015-10-07 14:14:13 +02001961 * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
1962 * possible to, falsely, observe p->on_cpu == 0.
1963 *
1964 * One must be running (->on_cpu == 1) in order to remove oneself
1965 * from the runqueue.
1966 *
1967 * [S] ->on_cpu = 1; [L] ->on_rq
1968 * UNLOCK rq->lock
1969 * RMB
1970 * LOCK rq->lock
1971 * [S] ->on_rq = 0; [L] ->on_cpu
1972 *
1973 * Pairs with the full barrier implied in the UNLOCK+LOCK on rq->lock
1974 * from the consecutive calls to schedule(); the first switching to our
1975 * task, the second putting it to sleep.
1976 */
1977 smp_rmb();
1978
1979 /*
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001980 * If the owning (remote) cpu is still in the middle of schedule() with
1981 * this task as prev, wait until its done referencing the task.
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001982 */
Peter Zijlstraf3e94782012-09-12 11:22:00 +02001983 while (p->on_cpu)
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001984 cpu_relax();
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001985 /*
Peter Zijlstrab75a2252015-10-06 14:36:17 +02001986 * Combined with the control dependency above, we have an effective
1987 * smp_load_acquire() without the need for full barriers.
1988 *
1989 * Pairs with the smp_store_release() in finish_lock_switch().
1990 *
1991 * This ensures that tasks getting woken will be fully ordered against
1992 * their previous state and preserve Program Order.
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001993 */
1994 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Peter Zijlstraa8e4f2e2011-04-05 17:23:49 +02001996 p->sched_contributes_to_load = !!task_contributes_to_load(p);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001997 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01001998
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001999 if (p->sched_class->task_waking)
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02002000 p->sched_class->task_waking(p);
Peter Zijlstraab19cb22009-11-27 15:44:43 +01002001
Peter Zijlstraac66f542013-10-07 11:29:16 +01002002 cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002003 if (task_cpu(p) != cpu) {
2004 wake_flags |= WF_MIGRATED;
Mike Galbraithf5dc3752009-10-09 08:35:03 +02002005 set_task_cpu(p, cpu);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002009 ttwu_queue(p, cpu);
2010stat:
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002011 ttwu_stat(p, cpu, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012out:
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002013 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 return success;
2016}
2017
David Howells50fa6102009-04-28 15:01:38 +01002018/**
Tejun Heo21aa9af2010-06-08 21:40:37 +02002019 * try_to_wake_up_local - try to wake up a local task with rq lock held
2020 * @p: the thread to be awakened
2021 *
Peter Zijlstra2acca552011-04-05 17:23:50 +02002022 * Put @p on the run-queue if it's not already there. The caller must
Tejun Heo21aa9af2010-06-08 21:40:37 +02002023 * ensure that this_rq() is locked, @p is bound to this_rq() and not
Peter Zijlstra2acca552011-04-05 17:23:50 +02002024 * the current task.
Tejun Heo21aa9af2010-06-08 21:40:37 +02002025 */
2026static void try_to_wake_up_local(struct task_struct *p)
2027{
2028 struct rq *rq = task_rq(p);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002029
Tejun Heo383efcd2013-03-18 12:22:34 -07002030 if (WARN_ON_ONCE(rq != this_rq()) ||
2031 WARN_ON_ONCE(p == current))
2032 return;
2033
Tejun Heo21aa9af2010-06-08 21:40:37 +02002034 lockdep_assert_held(&rq->lock);
2035
Peter Zijlstra2acca552011-04-05 17:23:50 +02002036 if (!raw_spin_trylock(&p->pi_lock)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002037 /*
2038 * This is OK, because current is on_cpu, which avoids it being
2039 * picked for load-balance and preemption/IRQs are still
2040 * disabled avoiding further scheduler activity on it and we've
2041 * not yet picked a replacement task.
2042 */
2043 lockdep_unpin_lock(&rq->lock);
Peter Zijlstra2acca552011-04-05 17:23:50 +02002044 raw_spin_unlock(&rq->lock);
2045 raw_spin_lock(&p->pi_lock);
2046 raw_spin_lock(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002047 lockdep_pin_lock(&rq->lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002048 }
Peter Zijlstra2acca552011-04-05 17:23:50 +02002049
Tejun Heo21aa9af2010-06-08 21:40:37 +02002050 if (!(p->state & TASK_NORMAL))
Peter Zijlstra2acca552011-04-05 17:23:50 +02002051 goto out;
Tejun Heo21aa9af2010-06-08 21:40:37 +02002052
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002053 trace_sched_waking(p);
2054
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002055 if (!task_on_rq_queued(p))
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002056 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
2057
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02002058 ttwu_do_wakeup(rq, p, 0);
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002059 ttwu_stat(p, smp_processor_id(), 0);
Peter Zijlstra2acca552011-04-05 17:23:50 +02002060out:
2061 raw_spin_unlock(&p->pi_lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02002062}
2063
2064/**
David Howells50fa6102009-04-28 15:01:38 +01002065 * wake_up_process - Wake up a specific process
2066 * @p: The process to be woken up.
2067 *
2068 * Attempt to wake up the nominated process and move it to the set of runnable
Yacine Belkadie69f6182013-07-12 20:45:47 +02002069 * processes.
2070 *
2071 * Return: 1 if the process was woken up, 0 if it was already running.
David Howells50fa6102009-04-28 15:01:38 +01002072 *
2073 * It may be assumed that this function implies a write memory barrier before
2074 * changing the task state if and only if any tasks are woken up.
2075 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002076int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Oleg Nesterov9067ac82013-01-21 20:48:17 +01002078 return try_to_wake_up(p, TASK_NORMAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080EXPORT_SYMBOL(wake_up_process);
2081
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002082int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 return try_to_wake_up(p, state, 0);
2085}
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087/*
Juri Lellia5e7be32014-09-19 10:22:39 +01002088 * This function clears the sched_dl_entity static params.
2089 */
2090void __dl_clear_params(struct task_struct *p)
2091{
2092 struct sched_dl_entity *dl_se = &p->dl;
2093
2094 dl_se->dl_runtime = 0;
2095 dl_se->dl_deadline = 0;
2096 dl_se->dl_period = 0;
2097 dl_se->flags = 0;
2098 dl_se->dl_bw = 0;
Peter Zijlstra40767b02015-01-28 15:08:03 +01002099
2100 dl_se->dl_throttled = 0;
2101 dl_se->dl_new = 1;
2102 dl_se->dl_yielded = 0;
Juri Lellia5e7be32014-09-19 10:22:39 +01002103}
2104
2105/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * Perform scheduler related setup for a newly forked process p.
2107 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002108 *
2109 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 */
Rik van Riel5e1576e2013-10-07 11:29:26 +01002111static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002113 p->on_rq = 0;
2114
2115 p->se.on_rq = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002116 p->se.exec_start = 0;
2117 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002118 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002119 p->se.nr_migrations = 0;
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002120 p->se.vruntime = 0;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002121 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002122
Byungchul Parkad936d82015-10-24 01:16:19 +09002123#ifdef CONFIG_FAIR_GROUP_SCHED
2124 p->se.cfs_rq = NULL;
2125#endif
2126
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002127#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03002128 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002129#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002130
Dario Faggioliaab03e02013-11-28 11:14:43 +01002131 RB_CLEAR_NODE(&p->dl.rb_node);
Peter Zijlstra40767b02015-01-28 15:08:03 +01002132 init_dl_task_timer(&p->dl);
Juri Lellia5e7be32014-09-19 10:22:39 +01002133 __dl_clear_params(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002134
Peter Zijlstrafa717062008-01-25 21:08:27 +01002135 INIT_LIST_HEAD(&p->rt.run_list);
Nick Piggin476d1392005-06-25 14:57:29 -07002136
Avi Kivitye107be32007-07-26 13:40:43 +02002137#ifdef CONFIG_PREEMPT_NOTIFIERS
2138 INIT_HLIST_HEAD(&p->preempt_notifiers);
2139#endif
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002140
2141#ifdef CONFIG_NUMA_BALANCING
2142 if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002143 p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002144 p->mm->numa_scan_seq = 0;
2145 }
2146
Rik van Riel5e1576e2013-10-07 11:29:26 +01002147 if (clone_flags & CLONE_VM)
2148 p->numa_preferred_nid = current->numa_preferred_nid;
2149 else
2150 p->numa_preferred_nid = -1;
2151
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002152 p->node_stamp = 0ULL;
2153 p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
Peter Zijlstra4b96a292012-10-25 14:16:47 +02002154 p->numa_scan_period = sysctl_numa_balancing_scan_delay;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002155 p->numa_work.next = &p->numa_work;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002156 p->numa_faults = NULL;
Rik van Riel7e2703e2014-01-27 17:03:45 -05002157 p->last_task_numa_placement = 0;
2158 p->last_sum_exec_runtime = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002159
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002160 p->numa_group = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002161#endif /* CONFIG_NUMA_BALANCING */
Ingo Molnardd41f592007-07-09 18:51:59 +02002162}
2163
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302164DEFINE_STATIC_KEY_FALSE(sched_numa_balancing);
2165
Mel Gorman1a687c22012-11-22 11:16:36 +00002166#ifdef CONFIG_NUMA_BALANCING
Mel Gorman3105b862012-11-23 11:23:49 +00002167
2168void set_numabalancing_state(bool enabled)
2169{
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302170 if (enabled)
2171 static_branch_enable(&sched_numa_balancing);
2172 else
2173 static_branch_disable(&sched_numa_balancing);
Mel Gorman3105b862012-11-23 11:23:49 +00002174}
Andi Kleen54a43d52014-01-23 15:53:13 -08002175
2176#ifdef CONFIG_PROC_SYSCTL
2177int sysctl_numa_balancing(struct ctl_table *table, int write,
2178 void __user *buffer, size_t *lenp, loff_t *ppos)
2179{
2180 struct ctl_table t;
2181 int err;
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302182 int state = static_branch_likely(&sched_numa_balancing);
Andi Kleen54a43d52014-01-23 15:53:13 -08002183
2184 if (write && !capable(CAP_SYS_ADMIN))
2185 return -EPERM;
2186
2187 t = *table;
2188 t.data = &state;
2189 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2190 if (err < 0)
2191 return err;
2192 if (write)
2193 set_numabalancing_state(state);
2194 return err;
2195}
2196#endif
2197#endif
Mel Gorman1a687c22012-11-22 11:16:36 +00002198
Ingo Molnardd41f592007-07-09 18:51:59 +02002199/*
2200 * fork()/clone()-time setup:
2201 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002202int sched_fork(unsigned long clone_flags, struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02002203{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002204 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002205 int cpu = get_cpu();
2206
Rik van Riel5e1576e2013-10-07 11:29:26 +01002207 __sched_fork(clone_flags, p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002208 /*
Peter Zijlstra0017d732010-03-24 18:34:10 +01002209 * We mark the process as running here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002210 * nobody will actually run it, and a signal or other external
2211 * event cannot wake it up and insert it on the runqueue either.
2212 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002213 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002214
Ingo Molnarb29739f2006-06-27 02:54:51 -07002215 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02002216 * Make sure we do not leak PI boosting priority to the child.
2217 */
2218 p->prio = current->normal_prio;
2219
2220 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002221 * Revert to default priority/policy on fork if requested.
2222 */
2223 if (unlikely(p->sched_reset_on_fork)) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01002224 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002225 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002226 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02002227 p->rt_priority = 0;
2228 } else if (PRIO_TO_NICE(p->static_prio) < 0)
2229 p->static_prio = NICE_TO_PRIO(0);
2230
2231 p->prio = p->normal_prio = __normal_prio(p);
2232 set_load_weight(p);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002233
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002234 /*
2235 * We don't need the reset flag anymore after the fork. It has
2236 * fulfilled its duty:
2237 */
2238 p->sched_reset_on_fork = 0;
2239 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02002240
Dario Faggioliaab03e02013-11-28 11:14:43 +01002241 if (dl_prio(p->prio)) {
2242 put_cpu();
2243 return -EAGAIN;
2244 } else if (rt_prio(p->prio)) {
2245 p->sched_class = &rt_sched_class;
2246 } else {
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002247 p->sched_class = &fair_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01002248 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07002249
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002250 if (p->sched_class->task_fork)
2251 p->sched_class->task_fork(p);
2252
Peter Zijlstra86951592010-06-22 11:44:53 +02002253 /*
2254 * The child is not yet in the pid-hash so no cgroup attach races,
2255 * and the cgroup is pinned to this child due to cgroup_fork()
2256 * is ran before sched_fork().
2257 *
2258 * Silence PROVE_RCU.
2259 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002260 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002261 set_task_cpu(p, cpu);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002262 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002263
Naveen N. Raof6db8342015-06-25 23:53:37 +05302264#ifdef CONFIG_SCHED_INFO
Ingo Molnardd41f592007-07-09 18:51:59 +02002265 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002266 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02002268#if defined(CONFIG_SMP)
2269 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07002270#endif
Peter Zijlstra01028742013-08-14 14:55:46 +02002271 init_task_preempt_count(p);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002272#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05002273 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002274 RB_CLEAR_NODE(&p->pushable_dl_tasks);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002275#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002276
Nick Piggin476d1392005-06-25 14:57:29 -07002277 put_cpu();
Dario Faggioliaab03e02013-11-28 11:14:43 +01002278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Dario Faggioli332ac172013-11-07 14:43:45 +01002281unsigned long to_ratio(u64 period, u64 runtime)
2282{
2283 if (runtime == RUNTIME_INF)
2284 return 1ULL << 20;
2285
2286 /*
2287 * Doing this here saves a lot of checks in all
2288 * the calling paths, and returning zero seems
2289 * safe for them anyway.
2290 */
2291 if (period == 0)
2292 return 0;
2293
2294 return div64_u64(runtime << 20, period);
2295}
2296
2297#ifdef CONFIG_SMP
2298inline struct dl_bw *dl_bw_of(int i)
2299{
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -07002300 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2301 "sched RCU must be held");
Dario Faggioli332ac172013-11-07 14:43:45 +01002302 return &cpu_rq(i)->rd->dl_bw;
2303}
2304
Peter Zijlstrade212f12013-12-19 11:54:45 +01002305static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002306{
Peter Zijlstrade212f12013-12-19 11:54:45 +01002307 struct root_domain *rd = cpu_rq(i)->rd;
2308 int cpus = 0;
2309
Paul E. McKenneyf78f5b92015-06-18 15:50:02 -07002310 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2311 "sched RCU must be held");
Peter Zijlstrade212f12013-12-19 11:54:45 +01002312 for_each_cpu_and(i, rd->span, cpu_active_mask)
2313 cpus++;
2314
2315 return cpus;
Dario Faggioli332ac172013-11-07 14:43:45 +01002316}
2317#else
2318inline struct dl_bw *dl_bw_of(int i)
2319{
2320 return &cpu_rq(i)->dl.dl_bw;
2321}
2322
Peter Zijlstrade212f12013-12-19 11:54:45 +01002323static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002324{
2325 return 1;
2326}
2327#endif
2328
Dario Faggioli332ac172013-11-07 14:43:45 +01002329/*
2330 * We must be sure that accepting a new task (or allowing changing the
2331 * parameters of an existing one) is consistent with the bandwidth
2332 * constraints. If yes, this function also accordingly updates the currently
2333 * allocated bandwidth to reflect the new situation.
2334 *
2335 * This function is called while holding p's rq->lock.
Peter Zijlstra40767b02015-01-28 15:08:03 +01002336 *
2337 * XXX we should delay bw change until the task's 0-lag point, see
2338 * __setparam_dl().
Dario Faggioli332ac172013-11-07 14:43:45 +01002339 */
2340static int dl_overflow(struct task_struct *p, int policy,
2341 const struct sched_attr *attr)
2342{
2343
2344 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
Steven Rostedt4df16382014-02-19 13:53:35 -05002345 u64 period = attr->sched_period ?: attr->sched_deadline;
Dario Faggioli332ac172013-11-07 14:43:45 +01002346 u64 runtime = attr->sched_runtime;
2347 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
Peter Zijlstrade212f12013-12-19 11:54:45 +01002348 int cpus, err = -1;
Dario Faggioli332ac172013-11-07 14:43:45 +01002349
2350 if (new_bw == p->dl.dl_bw)
2351 return 0;
2352
2353 /*
2354 * Either if a task, enters, leave, or stays -deadline but changes
2355 * its parameters, we may need to update accordingly the total
2356 * allocated bandwidth of the container.
2357 */
2358 raw_spin_lock(&dl_b->lock);
Peter Zijlstrade212f12013-12-19 11:54:45 +01002359 cpus = dl_bw_cpus(task_cpu(p));
Dario Faggioli332ac172013-11-07 14:43:45 +01002360 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2361 !__dl_overflow(dl_b, cpus, 0, new_bw)) {
2362 __dl_add(dl_b, new_bw);
2363 err = 0;
2364 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2365 !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
2366 __dl_clear(dl_b, p->dl.dl_bw);
2367 __dl_add(dl_b, new_bw);
2368 err = 0;
2369 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2370 __dl_clear(dl_b, p->dl.dl_bw);
2371 err = 0;
2372 }
2373 raw_spin_unlock(&dl_b->lock);
2374
2375 return err;
2376}
2377
2378extern void init_dl_bw(struct dl_bw *dl_b);
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380/*
2381 * wake_up_new_task - wake up a newly created task for the first time.
2382 *
2383 * This function will do some initial scheduler statistics housekeeping
2384 * that must be done for every newly created context, then puts the task
2385 * on the runqueue and wakes it.
2386 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002387void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002390 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002391
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002392 raw_spin_lock_irqsave(&p->pi_lock, flags);
Morten Rasmussen98d8fd82015-08-14 17:23:14 +01002393 /* Initialize new task's runnable average */
2394 init_entity_runnable_average(&p->se);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002395#ifdef CONFIG_SMP
2396 /*
2397 * Fork balancing, do it here and not earlier because:
2398 * - cpus_allowed can change in the fork path
2399 * - any previously selected cpu might disappear through hotplug
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002400 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01002401 set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002402#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002404 rq = __task_rq_lock(p);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002405 activate_task(rq, p, 0);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002406 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002407 trace_sched_wakeup_new(p);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002408 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002409#ifdef CONFIG_SMP
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02002410 if (p->sched_class->task_woken) {
2411 /*
2412 * Nothing relies on rq->lock after this, so its fine to
2413 * drop it.
2414 */
2415 lockdep_unpin_lock(&rq->lock);
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002416 p->sched_class->task_woken(rq, p);
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02002417 lockdep_pin_lock(&rq->lock);
2418 }
Steven Rostedt9a897c52008-01-25 21:08:22 +01002419#endif
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002420 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
Avi Kivitye107be32007-07-26 13:40:43 +02002423#ifdef CONFIG_PREEMPT_NOTIFIERS
2424
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002425static struct static_key preempt_notifier_key = STATIC_KEY_INIT_FALSE;
2426
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02002427void preempt_notifier_inc(void)
2428{
2429 static_key_slow_inc(&preempt_notifier_key);
2430}
2431EXPORT_SYMBOL_GPL(preempt_notifier_inc);
2432
2433void preempt_notifier_dec(void)
2434{
2435 static_key_slow_dec(&preempt_notifier_key);
2436}
2437EXPORT_SYMBOL_GPL(preempt_notifier_dec);
2438
Avi Kivitye107be32007-07-26 13:40:43 +02002439/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002440 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002441 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002442 */
2443void preempt_notifier_register(struct preempt_notifier *notifier)
2444{
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02002445 if (!static_key_false(&preempt_notifier_key))
2446 WARN(1, "registering preempt_notifier while notifiers disabled\n");
2447
Avi Kivitye107be32007-07-26 13:40:43 +02002448 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2449}
2450EXPORT_SYMBOL_GPL(preempt_notifier_register);
2451
2452/**
2453 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002454 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002455 *
Mathieu Desnoyersd84525a2015-05-17 12:53:10 -04002456 * This is *not* safe to call from within a preemption notifier.
Avi Kivitye107be32007-07-26 13:40:43 +02002457 */
2458void preempt_notifier_unregister(struct preempt_notifier *notifier)
2459{
2460 hlist_del(&notifier->link);
2461}
2462EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2463
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002464static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02002465{
2466 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002467
Sasha Levinb67bfe02013-02-27 17:06:00 -08002468 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002469 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2470}
2471
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002472static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2473{
2474 if (static_key_false(&preempt_notifier_key))
2475 __fire_sched_in_preempt_notifiers(curr);
2476}
2477
Avi Kivitye107be32007-07-26 13:40:43 +02002478static void
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002479__fire_sched_out_preempt_notifiers(struct task_struct *curr,
2480 struct task_struct *next)
Avi Kivitye107be32007-07-26 13:40:43 +02002481{
2482 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002483
Sasha Levinb67bfe02013-02-27 17:06:00 -08002484 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002485 notifier->ops->sched_out(notifier, next);
2486}
2487
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002488static __always_inline void
2489fire_sched_out_preempt_notifiers(struct task_struct *curr,
2490 struct task_struct *next)
2491{
2492 if (static_key_false(&preempt_notifier_key))
2493 __fire_sched_out_preempt_notifiers(curr, next);
2494}
2495
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002496#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002497
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002498static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02002499{
2500}
2501
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002502static inline void
Avi Kivitye107be32007-07-26 13:40:43 +02002503fire_sched_out_preempt_notifiers(struct task_struct *curr,
2504 struct task_struct *next)
2505{
2506}
2507
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002508#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002511 * prepare_task_switch - prepare to switch tasks
2512 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002513 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002514 * @next: the task we are going to switch to.
2515 *
2516 * This is called with the rq lock held and interrupts off. It must
2517 * be paired with a subsequent finish_task_switch after the context
2518 * switch.
2519 *
2520 * prepare_task_switch sets up locking and calls architecture specific
2521 * hooks.
2522 */
Avi Kivitye107be32007-07-26 13:40:43 +02002523static inline void
2524prepare_task_switch(struct rq *rq, struct task_struct *prev,
2525 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002526{
Michael S. Tsirkin43148952013-09-22 17:20:54 +03002527 sched_info_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002528 perf_event_task_sched_out(prev, next);
Avi Kivitye107be32007-07-26 13:40:43 +02002529 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002530 prepare_lock_switch(rq, next);
2531 prepare_arch_switch(next);
2532}
2533
2534/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 * finish_task_switch - clean up after a task-switch
2536 * @prev: the thread we just switched away from.
2537 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002538 * finish_task_switch must be called after the context switch, paired
2539 * with a prepare_task_switch call before the context switch.
2540 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2541 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 *
2543 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002544 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 * with the lock held can cause deadlocks; see schedule() for
2546 * details.)
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002547 *
2548 * The context switch have flipped the stack from under us and restored the
2549 * local variables which were saved when this task called schedule() in the
2550 * past. prev == current is still correct but we need to recalculate this_rq
2551 * because prev may have moved to another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002553static struct rq *finish_task_switch(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 __releases(rq->lock)
2555{
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002556 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002558 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Peter Zijlstra609ca062015-09-28 17:52:18 +02002560 /*
2561 * The previous task will have left us with a preempt_count of 2
2562 * because it left us after:
2563 *
2564 * schedule()
2565 * preempt_disable(); // 1
2566 * __schedule()
2567 * raw_spin_lock_irq(&rq->lock) // 2
2568 *
2569 * Also, see FORK_PREEMPT_COUNT.
2570 */
Peter Zijlstrae2bf1c4b2015-09-29 12:18:46 +02002571 if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
2572 "corrupted preempt_count: %s/%d/0x%x\n",
2573 current->comm, current->pid, preempt_count()))
2574 preempt_count_set(FORK_PREEMPT_COUNT);
Peter Zijlstra609ca062015-09-28 17:52:18 +02002575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 rq->prev_mm = NULL;
2577
2578 /*
2579 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002580 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002581 * schedule one last time. The schedule call will never return, and
2582 * the scheduled task must drop that reference.
Peter Zijlstra95913d92015-09-29 14:45:09 +02002583 *
2584 * We must observe prev->state before clearing prev->on_cpu (in
2585 * finish_lock_switch), otherwise a concurrent wakeup can get prev
2586 * running on another CPU and we could rave with its RUNNING -> DEAD
2587 * transition, resulting in a double drop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002589 prev_state = prev->state;
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +02002590 vtime_task_switch(prev);
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002591 perf_event_task_sched_in(prev, current);
Nick Piggin4866cde2005-06-25 14:57:23 -07002592 finish_lock_switch(rq, prev);
Catalin Marinas01f23e12011-11-27 21:43:10 +00002593 finish_arch_post_lock_switch();
Steven Rostedte8fa1362008-01-25 21:08:05 +01002594
Avi Kivitye107be32007-07-26 13:40:43 +02002595 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (mm)
2597 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002598 if (unlikely(prev_state == TASK_DEAD)) {
Dario Faggiolie6c390f2013-11-07 14:43:35 +01002599 if (prev->sched_class->task_dead)
2600 prev->sched_class->task_dead(prev);
2601
bibo maoc6fd91f2006-03-26 01:38:20 -08002602 /*
2603 * Remove function-return probe instances associated with this
2604 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002605 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002606 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002608 }
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +02002609
Frederic Weisbeckerde734f82015-06-11 18:07:12 +02002610 tick_nohz_task_switch();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002611 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Gregory Haskins3f029d32009-07-29 11:08:47 -04002614#ifdef CONFIG_SMP
2615
Gregory Haskins3f029d32009-07-29 11:08:47 -04002616/* rq->lock is NOT held, but preemption is disabled */
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002617static void __balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04002618{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002619 struct callback_head *head, *next;
2620 void (*func)(struct rq *rq);
2621 unsigned long flags;
Gregory Haskins3f029d32009-07-29 11:08:47 -04002622
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002623 raw_spin_lock_irqsave(&rq->lock, flags);
2624 head = rq->balance_callback;
2625 rq->balance_callback = NULL;
2626 while (head) {
2627 func = (void (*)(struct rq *))head->func;
2628 next = head->next;
2629 head->next = NULL;
2630 head = next;
Gregory Haskins3f029d32009-07-29 11:08:47 -04002631
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002632 func(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002633 }
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002634 raw_spin_unlock_irqrestore(&rq->lock, flags);
2635}
2636
2637static inline void balance_callback(struct rq *rq)
2638{
2639 if (unlikely(rq->balance_callback))
2640 __balance_callback(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002641}
2642
2643#else
2644
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002645static inline void balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04002646{
2647}
2648
2649#endif
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651/**
2652 * schedule_tail - first thing a freshly forked thread must call.
2653 * @prev: the thread we just switched away from.
2654 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002655asmlinkage __visible void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 __releases(rq->lock)
2657{
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002658 struct rq *rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002659
Peter Zijlstra609ca062015-09-28 17:52:18 +02002660 /*
2661 * New tasks start with FORK_PREEMPT_COUNT, see there and
2662 * finish_task_switch() for details.
2663 *
2664 * finish_task_switch() will drop rq->lock() and lower preempt_count
2665 * and the preempt_enable() will end up enabling preemption (on
2666 * PREEMPT_COUNT kernels).
2667 */
2668
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002669 rq = finish_task_switch(prev);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002670 balance_callback(rq);
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002671 preempt_enable();
Steven Rostedtda19ab52009-07-29 00:21:22 -04002672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002674 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
2677/*
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002678 * context_switch - switch to the new MM and the new thread's register state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002680static inline struct rq *
Ingo Molnar70b97a72006-07-03 00:25:42 -07002681context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002682 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Ingo Molnardd41f592007-07-09 18:51:59 +02002684 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Avi Kivitye107be32007-07-26 13:40:43 +02002686 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002687
Ingo Molnardd41f592007-07-09 18:51:59 +02002688 mm = next->mm;
2689 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002690 /*
2691 * For paravirt, this is coupled with an exit in switch_to to
2692 * combine the page table reload and the switch backend into
2693 * one hypercall.
2694 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002695 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002696
Heiko Carstens31915ab2010-09-16 14:42:25 +02002697 if (!mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 next->active_mm = oldmm;
2699 atomic_inc(&oldmm->mm_count);
2700 enter_lazy_tlb(oldmm, next);
2701 } else
2702 switch_mm(oldmm, mm, next);
2703
Heiko Carstens31915ab2010-09-16 14:42:25 +02002704 if (!prev->mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 rq->prev_mm = oldmm;
2707 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002708 /*
2709 * Since the runqueue lock will be released by the next
2710 * task (which is an invalid locking op but in the case
2711 * of the scheduler it's an obvious special-case), so we
2712 * do an early lockdep release here:
2713 */
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002714 lockdep_unpin_lock(&rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002715 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 /* Here we just switch the register state and the stack. */
2718 switch_to(prev, next, prev);
Ingo Molnardd41f592007-07-09 18:51:59 +02002719 barrier();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002720
2721 return finish_task_switch(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
2724/*
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002725 * nr_running and nr_context_switches:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 *
2727 * externally visible scheduler statistics: current number of runnable
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002728 * threads, total number of context switches performed since bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 */
2730unsigned long nr_running(void)
2731{
2732 unsigned long i, sum = 0;
2733
2734 for_each_online_cpu(i)
2735 sum += cpu_rq(i)->nr_running;
2736
2737 return sum;
2738}
2739
Tim Chen2ee507c2014-07-31 10:29:48 -07002740/*
2741 * Check if only the current task is running on the cpu.
Dominik Dingel00cc16332015-09-18 11:27:45 +02002742 *
2743 * Caution: this function does not check that the caller has disabled
2744 * preemption, thus the result might have a time-of-check-to-time-of-use
2745 * race. The caller is responsible to use it correctly, for example:
2746 *
2747 * - from a non-preemptable section (of course)
2748 *
2749 * - from a thread that is bound to a single CPU
2750 *
2751 * - in a loop with very short iterations (e.g. a polling loop)
Tim Chen2ee507c2014-07-31 10:29:48 -07002752 */
2753bool single_task_running(void)
2754{
Dominik Dingel00cc16332015-09-18 11:27:45 +02002755 return raw_rq()->nr_running == 1;
Tim Chen2ee507c2014-07-31 10:29:48 -07002756}
2757EXPORT_SYMBOL(single_task_running);
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759unsigned long long nr_context_switches(void)
2760{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002761 int i;
2762 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002764 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 sum += cpu_rq(i)->nr_switches;
2766
2767 return sum;
2768}
2769
2770unsigned long nr_iowait(void)
2771{
2772 unsigned long i, sum = 0;
2773
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002774 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2776
2777 return sum;
2778}
2779
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002780unsigned long nr_iowait_cpu(int cpu)
Arjan van de Ven69d25872009-09-21 17:04:08 -07002781{
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002782 struct rq *this = cpu_rq(cpu);
Arjan van de Ven69d25872009-09-21 17:04:08 -07002783 return atomic_read(&this->nr_iowait);
2784}
2785
Mel Gorman372ba8c2014-08-06 14:19:21 +01002786void get_iowait_load(unsigned long *nr_waiters, unsigned long *load)
2787{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02002788 struct rq *rq = this_rq();
2789 *nr_waiters = atomic_read(&rq->nr_iowait);
2790 *load = rq->load.weight;
Mel Gorman372ba8c2014-08-06 14:19:21 +01002791}
2792
Ingo Molnardd41f592007-07-09 18:51:59 +02002793#ifdef CONFIG_SMP
2794
Ingo Molnar48f24c42006-07-03 00:25:40 -07002795/*
Peter Zijlstra38022902009-12-16 18:04:37 +01002796 * sched_exec - execve() is a valuable balancing opportunity, because at
2797 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 */
Peter Zijlstra38022902009-12-16 18:04:37 +01002799void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
Peter Zijlstra38022902009-12-16 18:04:37 +01002801 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01002803 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01002804
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002805 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstraac66f542013-10-07 11:29:16 +01002806 dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002807 if (dest_cpu == smp_processor_id())
2808 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01002809
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002810 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02002811 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07002812
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002813 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
2814 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return;
2816 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01002817unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002818 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821#endif
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823DEFINE_PER_CPU(struct kernel_stat, kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002824DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826EXPORT_PER_CPU_SYMBOL(kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002827EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002830 * Return accounted runtime for the task.
2831 * In case the task is currently running, return the runtime plus current's
2832 * pending runtime that have not been accounted yet.
2833 */
2834unsigned long long task_sched_runtime(struct task_struct *p)
2835{
2836 unsigned long flags;
2837 struct rq *rq;
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002838 u64 ns;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002839
Peter Zijlstra911b2892013-11-11 18:21:56 +01002840#if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
2841 /*
2842 * 64-bit doesn't need locks to atomically read a 64bit value.
2843 * So we have a optimization chance when the task's delta_exec is 0.
2844 * Reading ->on_cpu is racy, but this is ok.
2845 *
2846 * If we race with it leaving cpu, we'll take a lock. So we're correct.
2847 * If we race with it entering cpu, unaccounted time is 0. This is
2848 * indistinguishable from the read occurring a few cycles earlier.
Mike Galbraith4036ac12014-06-24 07:49:40 +02002849 * If we see ->on_cpu without ->on_rq, the task is leaving, and has
2850 * been accounted, so we're correct here as well.
Peter Zijlstra911b2892013-11-11 18:21:56 +01002851 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002852 if (!p->on_cpu || !task_on_rq_queued(p))
Peter Zijlstra911b2892013-11-11 18:21:56 +01002853 return p->se.sum_exec_runtime;
2854#endif
2855
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002856 rq = task_rq_lock(p, &flags);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002857 /*
2858 * Must be ->curr _and_ ->on_rq. If dequeued, we would
2859 * project cycles that may never be accounted to this
2860 * thread, breaking clock_gettime().
2861 */
2862 if (task_current(rq, p) && task_on_rq_queued(p)) {
2863 update_rq_clock(rq);
2864 p->sched_class->update_curr(rq);
2865 }
2866 ns = p->se.sum_exec_runtime;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002867 task_rq_unlock(rq, p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002868
2869 return ns;
2870}
2871
Balbir Singh49048622008-09-05 18:12:23 +02002872/*
Christoph Lameter7835b982006-12-10 02:20:22 -08002873 * This function gets called by the timer code, with HZ frequency.
2874 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08002875 */
2876void scheduler_tick(void)
2877{
Christoph Lameter7835b982006-12-10 02:20:22 -08002878 int cpu = smp_processor_id();
2879 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002880 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002881
2882 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08002883
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002884 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002885 update_rq_clock(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01002886 curr->sched_class->task_tick(rq, curr, 0);
Alex Shi83dfd522013-06-20 10:18:49 +08002887 update_cpu_load_active(rq);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02002888 calc_global_load_tick(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002889 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02002890
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002891 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002892
Christoph Lametere418e1c2006-12-10 02:20:23 -08002893#ifdef CONFIG_SMP
Suresh Siddha6eb57e02011-10-03 15:09:01 -07002894 rq->idle_balance = idle_cpu(cpu);
Daniel Lezcano7caff662014-01-06 12:34:38 +01002895 trigger_load_balance(rq);
Christoph Lametere418e1c2006-12-10 02:20:23 -08002896#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002897 rq_last_tick_reset(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002900#ifdef CONFIG_NO_HZ_FULL
2901/**
2902 * scheduler_tick_max_deferment
2903 *
2904 * Keep at least one tick per second when a single
2905 * active task is running because the scheduler doesn't
2906 * yet completely support full dynticks environment.
2907 *
2908 * This makes sure that uptime, CFS vruntime, load
2909 * balancing, etc... continue to move forward, even
2910 * with a very low granularity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02002911 *
2912 * Return: Maximum deferment in nanoseconds.
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002913 */
2914u64 scheduler_tick_max_deferment(void)
2915{
2916 struct rq *rq = this_rq();
Jason Low316c1608d2015-04-28 13:00:20 -07002917 unsigned long next, now = READ_ONCE(jiffies);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002918
2919 next = rq->last_sched_tick + HZ;
2920
2921 if (time_before_eq(next, now))
2922 return 0;
2923
Kevin Hilman8fe8ff02014-01-15 14:51:38 +01002924 return jiffies_to_nsecs(next - now);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002925}
2926#endif
2927
Lai Jiangshan132380a2009-04-02 14:18:25 +08002928notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002929{
2930 if (in_lock_functions(addr)) {
2931 addr = CALLER_ADDR2;
2932 if (in_lock_functions(addr))
2933 addr = CALLER_ADDR3;
2934 }
2935 return addr;
2936}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05002938#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
2939 defined(CONFIG_PREEMPT_TRACER))
2940
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002941void preempt_count_add(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002943#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 /*
2945 * Underflow?
2946 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002947 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
2948 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002949#endif
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002950 __preempt_count_add(val);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002951#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 /*
2953 * Spinlock count overflowing soon?
2954 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002955 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
2956 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002957#endif
Thomas Gleixner8f47b182014-02-07 20:58:39 +01002958 if (preempt_count() == val) {
2959 unsigned long ip = get_parent_ip(CALLER_ADDR1);
2960#ifdef CONFIG_DEBUG_PREEMPT
2961 current->preempt_disable_ip = ip;
2962#endif
2963 trace_preempt_off(CALLER_ADDR0, ip);
2964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002966EXPORT_SYMBOL(preempt_count_add);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002967NOKPROBE_SYMBOL(preempt_count_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002969void preempt_count_sub(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002971#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 /*
2973 * Underflow?
2974 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01002975 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002976 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 /*
2978 * Is the spinlock portion underflowing?
2979 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002980 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
2981 !(preempt_count() & PREEMPT_MASK)))
2982 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002983#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002984
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002985 if (preempt_count() == val)
2986 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002987 __preempt_count_sub(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002989EXPORT_SYMBOL(preempt_count_sub);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002990NOKPROBE_SYMBOL(preempt_count_sub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992#endif
2993
2994/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002995 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002997static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
Dave Jones664dfa62011-12-22 16:39:30 -05002999 if (oops_in_progress)
3000 return;
3001
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01003002 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3003 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02003004
Ingo Molnardd41f592007-07-09 18:51:59 +02003005 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07003006 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02003007 if (irqs_disabled())
3008 print_irqtrace_events(prev);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01003009#ifdef CONFIG_DEBUG_PREEMPT
3010 if (in_atomic_preempt_off()) {
3011 pr_err("Preemption disabled at:");
3012 print_ip_sym(current->preempt_disable_ip);
3013 pr_cont("\n");
3014 }
3015#endif
Stephen Boyd6135fc12012-03-28 17:10:47 -07003016 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +10303017 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Ingo Molnardd41f592007-07-09 18:51:59 +02003018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Ingo Molnardd41f592007-07-09 18:51:59 +02003020/*
3021 * Various schedule()-time debugging checks and statistics:
3022 */
3023static inline void schedule_debug(struct task_struct *prev)
3024{
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01003025#ifdef CONFIG_SCHED_STACK_END_CHECK
Geliang Tangce03e412015-10-05 21:26:05 +08003026 BUG_ON(task_stack_end_corrupted(prev));
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01003027#endif
Peter Zijlstrab99def82015-09-28 18:02:03 +02003028
Peter Zijlstra1dc0fff2015-09-28 17:57:39 +02003029 if (unlikely(in_atomic_preempt_off())) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003030 __schedule_bug(prev);
Peter Zijlstra1dc0fff2015-09-28 17:57:39 +02003031 preempt_count_set(PREEMPT_DISABLED);
3032 }
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003033 rcu_sleep_check();
Ingo Molnardd41f592007-07-09 18:51:59 +02003034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3036
Ingo Molnar2d723762007-10-15 17:00:12 +02003037 schedstat_inc(this_rq(), sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02003038}
3039
3040/*
3041 * Pick up the highest-prio task:
3042 */
3043static inline struct task_struct *
Peter Zijlstra606dba22012-02-11 06:05:00 +01003044pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02003045{
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003046 const struct sched_class *class = &fair_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003047 struct task_struct *p;
3048
3049 /*
3050 * Optimization: we know that if all tasks are in
3051 * the fair class we can call that function directly:
3052 */
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003053 if (likely(prev->sched_class == class &&
Peter Zijlstra38033c32014-01-23 20:32:21 +01003054 rq->nr_running == rq->cfs.h_nr_running)) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01003055 p = fair_sched_class.pick_next_task(rq, prev);
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02003056 if (unlikely(p == RETRY_TASK))
3057 goto again;
3058
3059 /* assumes fair_sched_class->next == idle_sched_class */
3060 if (unlikely(!p))
3061 p = idle_sched_class.pick_next_task(rq, prev);
3062
3063 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02003064 }
3065
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003066again:
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003067 for_each_class(class) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01003068 p = class->pick_next_task(rq, prev);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003069 if (p) {
3070 if (unlikely(p == RETRY_TASK))
3071 goto again;
Ingo Molnardd41f592007-07-09 18:51:59 +02003072 return p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01003073 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003074 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003075
3076 BUG(); /* the idle class will always have a runnable task */
Ingo Molnardd41f592007-07-09 18:51:59 +02003077}
3078
3079/*
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003080 * __schedule() is the main scheduler function.
Pekka Enbergedde96e2012-08-04 11:49:47 +03003081 *
3082 * The main means of driving the scheduler and thus entering this function are:
3083 *
3084 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
3085 *
3086 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
3087 * paths. For example, see arch/x86/entry_64.S.
3088 *
3089 * To drive preemption between tasks, the scheduler sets the flag in timer
3090 * interrupt handler scheduler_tick().
3091 *
3092 * 3. Wakeups don't really cause entry into schedule(). They add a
3093 * task to the run-queue and that's it.
3094 *
3095 * Now, if the new task added to the run-queue preempts the current
3096 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
3097 * called on the nearest possible occasion:
3098 *
3099 * - If the kernel is preemptible (CONFIG_PREEMPT=y):
3100 *
3101 * - in syscall or exception context, at the next outmost
3102 * preempt_enable(). (this might be as soon as the wake_up()'s
3103 * spin_unlock()!)
3104 *
3105 * - in IRQ context, return from interrupt-handler to
3106 * preemptible context
3107 *
3108 * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
3109 * then at the next:
3110 *
3111 * - cond_resched() call
3112 * - explicit schedule() call
3113 * - return from syscall or exception to user-space
3114 * - return from interrupt-handler to user-space
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003115 *
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003116 * WARNING: must be called with preemption disabled!
Ingo Molnardd41f592007-07-09 18:51:59 +02003117 */
Peter Zijlstra499d7952015-09-28 18:52:36 +02003118static void __sched notrace __schedule(bool preempt)
Ingo Molnardd41f592007-07-09 18:51:59 +02003119{
3120 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003121 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02003122 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02003123 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02003124
Ingo Molnardd41f592007-07-09 18:51:59 +02003125 cpu = smp_processor_id();
3126 rq = cpu_rq(cpu);
Paul E. McKenney38200cf2014-10-21 12:50:04 -07003127 rcu_note_context_switch();
Ingo Molnardd41f592007-07-09 18:51:59 +02003128 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02003129
Peter Zijlstrab99def82015-09-28 18:02:03 +02003130 /*
3131 * do_exit() calls schedule() with preemption disabled as an exception;
3132 * however we must fix that up, otherwise the next task will see an
3133 * inconsistent (higher) preempt count.
3134 *
3135 * It also avoids the below schedule_debug() test from complaining
3136 * about this.
3137 */
3138 if (unlikely(prev->state == TASK_DEAD))
3139 preempt_enable_no_resched_notrace();
3140
Ingo Molnardd41f592007-07-09 18:51:59 +02003141 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Peter Zijlstra31656512008-07-18 18:01:23 +02003143 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02003144 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003145
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02003146 /*
3147 * Make sure that signal_pending_state()->signal_pending() below
3148 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
3149 * done by the caller to avoid the race with signal_wake_up().
3150 */
3151 smp_mb__before_spinlock();
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003152 raw_spin_lock_irq(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003153 lockdep_pin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003155 rq->clock_skip_update <<= 1; /* promote REQ to ACT */
3156
Oleg Nesterov246d86b2010-05-19 14:57:11 +02003157 switch_count = &prev->nivcsw;
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003158 if (!preempt && prev->state) {
Tejun Heo21aa9af2010-06-08 21:40:37 +02003159 if (unlikely(signal_pending_state(prev->state, prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003160 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02003161 } else {
Peter Zijlstra2acca552011-04-05 17:23:50 +02003162 deactivate_task(rq, prev, DEQUEUE_SLEEP);
3163 prev->on_rq = 0;
3164
Tejun Heo21aa9af2010-06-08 21:40:37 +02003165 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02003166 * If a worker went to sleep, notify and ask workqueue
3167 * whether it wants to wake up a task to maintain
3168 * concurrency.
Tejun Heo21aa9af2010-06-08 21:40:37 +02003169 */
3170 if (prev->flags & PF_WQ_WORKER) {
3171 struct task_struct *to_wakeup;
3172
3173 to_wakeup = wq_worker_sleeping(prev, cpu);
3174 if (to_wakeup)
3175 try_to_wake_up_local(to_wakeup);
3176 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02003177 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003178 switch_count = &prev->nvcsw;
3179 }
3180
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003181 if (task_on_rq_queued(prev))
Peter Zijlstra606dba22012-02-11 06:05:00 +01003182 update_rq_clock(rq);
3183
3184 next = pick_next_task(rq, prev);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01003185 clear_tsk_need_resched(prev);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02003186 clear_preempt_need_resched();
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003187 rq->clock_skip_update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 rq->nr_switches++;
3191 rq->curr = next;
3192 ++*switch_count;
3193
Peter Zijlstrac73464b2015-09-28 18:06:56 +02003194 trace_sched_switch(preempt, prev, next);
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003195 rq = context_switch(rq, prev, next); /* unlocks the rq */
3196 cpu = cpu_of(rq);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003197 } else {
3198 lockdep_unpin_lock(&rq->lock);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003199 raw_spin_unlock_irq(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003202 balance_callback(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003204
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003205static inline void sched_submit_work(struct task_struct *tsk)
3206{
Thomas Gleixner3c7d5182011-07-17 20:46:52 +02003207 if (!tsk->state || tsk_is_pi_blocked(tsk))
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003208 return;
3209 /*
3210 * If we are going to sleep and we have plugged IO queued,
3211 * make sure to submit it to avoid deadlocks.
3212 */
3213 if (blk_needs_flush_plug(tsk))
3214 blk_schedule_flush_plug(tsk);
3215}
3216
Andi Kleen722a9f92014-05-02 00:44:38 +02003217asmlinkage __visible void __sched schedule(void)
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003218{
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003219 struct task_struct *tsk = current;
3220
3221 sched_submit_work(tsk);
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003222 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003223 preempt_disable();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003224 __schedule(false);
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003225 sched_preempt_enable_no_resched();
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003226 } while (need_resched());
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228EXPORT_SYMBOL(schedule);
3229
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01003230#ifdef CONFIG_CONTEXT_TRACKING
Andi Kleen722a9f92014-05-02 00:44:38 +02003231asmlinkage __visible void __sched schedule_user(void)
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003232{
3233 /*
3234 * If we come here after a random call to set_need_resched(),
3235 * or we have been woken up remotely but the IPI has not yet arrived,
3236 * we haven't yet exited the RCU idle mode. Do it here manually until
3237 * we find a better solution.
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003238 *
3239 * NB: There are buggy callers of this function. Ideally we
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +01003240 * should warn if prev_state != CONTEXT_USER, but that will trigger
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003241 * too frequently to make sense yet.
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003242 */
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003243 enum ctx_state prev_state = exception_enter();
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003244 schedule();
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003245 exception_exit(prev_state);
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003246}
3247#endif
3248
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01003249/**
3250 * schedule_preempt_disabled - called with preemption disabled
3251 *
3252 * Returns with preemption disabled. Note: preempt_count must be 1
3253 */
3254void __sched schedule_preempt_disabled(void)
3255{
Thomas Gleixnerba74c142011-03-21 13:32:17 +01003256 sched_preempt_enable_no_resched();
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01003257 schedule();
3258 preempt_disable();
3259}
3260
Frederic Weisbecker06b1f802015-02-16 19:20:07 +01003261static void __sched notrace preempt_schedule_common(void)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003262{
3263 do {
Peter Zijlstra499d7952015-09-28 18:52:36 +02003264 preempt_disable_notrace();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003265 __schedule(true);
Peter Zijlstra499d7952015-09-28 18:52:36 +02003266 preempt_enable_no_resched_notrace();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003267
3268 /*
3269 * Check again in case we missed a preemption opportunity
3270 * between schedule and now.
3271 */
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003272 } while (need_resched());
3273}
3274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275#ifdef CONFIG_PREEMPT
3276/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003277 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003278 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 * occur there and call schedule directly.
3280 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003281asmlinkage __visible void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /*
3284 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003285 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 */
Frederic Weisbeckerfbb00b52013-06-19 23:56:22 +02003287 if (likely(!preemptible()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return;
3289
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003290 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09003292NOKPROBE_SYMBOL(preempt_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293EXPORT_SYMBOL(preempt_schedule);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003294
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003295/**
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003296 * preempt_schedule_notrace - preempt_schedule called by tracing
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003297 *
3298 * The tracing infrastructure uses preempt_enable_notrace to prevent
3299 * recursion and tracing preempt enabling caused by the tracing
3300 * infrastructure itself. But as tracing can happen in areas coming
3301 * from userspace or just about to enter userspace, a preempt enable
3302 * can occur before user_exit() is called. This will cause the scheduler
3303 * to be called when the system is still in usermode.
3304 *
3305 * To prevent this, the preempt_enable_notrace will use this function
3306 * instead of preempt_schedule() to exit user context if needed before
3307 * calling the scheduler.
3308 */
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003309asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003310{
3311 enum ctx_state prev_ctx;
3312
3313 if (likely(!preemptible()))
3314 return;
3315
3316 do {
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003317 preempt_disable_notrace();
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003318 /*
3319 * Needs preempt disabled in case user_exit() is traced
3320 * and the tracer calls preempt_enable_notrace() causing
3321 * an infinite recursion.
3322 */
3323 prev_ctx = exception_enter();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003324 __schedule(true);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003325 exception_exit(prev_ctx);
3326
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003327 preempt_enable_no_resched_notrace();
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003328 } while (need_resched());
3329}
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003330EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003331
Thomas Gleixner32e475d2013-11-21 12:41:44 +01003332#endif /* CONFIG_PREEMPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
3334/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003335 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 * off of irq context.
3337 * Note, that this is called and return with irqs disabled. This will
3338 * protect us against recursive calling from irq.
3339 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003340asmlinkage __visible void __sched preempt_schedule_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003342 enum ctx_state prev_state;
Ingo Molnar6478d882008-01-25 21:08:33 +01003343
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003344 /* Catch callers which need to be fixed */
Peter Zijlstraf27dde82013-08-14 14:55:31 +02003345 BUG_ON(preempt_count() || !irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003347 prev_state = exception_enter();
3348
Andi Kleen3a5c3592007-10-15 17:00:14 +02003349 do {
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003350 preempt_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003351 local_irq_enable();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02003352 __schedule(true);
Andi Kleen3a5c3592007-10-15 17:00:14 +02003353 local_irq_disable();
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02003354 sched_preempt_enable_no_resched();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003355 } while (need_resched());
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003356
3357 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
Peter Zijlstra63859d42009-09-15 19:14:42 +02003360int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003361 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
Peter Zijlstra63859d42009-09-15 19:14:42 +02003363 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365EXPORT_SYMBOL(default_wake_function);
3366
Ingo Molnarb29739f2006-06-27 02:54:51 -07003367#ifdef CONFIG_RT_MUTEXES
3368
3369/*
3370 * rt_mutex_setprio - set the current priority of a task
3371 * @p: task
3372 * @prio: prio value (kernel-internal form)
3373 *
3374 * This function changes the 'effective' priority of a task. It does
3375 * not touch ->normal_prio like __setscheduler().
3376 *
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003377 * Used by the rt_mutex code to implement priority inheritance
3378 * logic. Call site only calls if the priority of the task changed.
Ingo Molnarb29739f2006-06-27 02:54:51 -07003379 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003380void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003381{
Peter Zijlstra1de64442015-09-30 17:44:13 +02003382 int oldprio, queued, running, enqueue_flag = ENQUEUE_RESTORE;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003383 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003384 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003385
Dario Faggioliaab03e02013-11-28 11:14:43 +01003386 BUG_ON(prio > MAX_PRIO);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003387
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003388 rq = __task_rq_lock(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003389
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003390 /*
3391 * Idle task boosting is a nono in general. There is one
3392 * exception, when PREEMPT_RT and NOHZ is active:
3393 *
3394 * The idle task calls get_next_timer_interrupt() and holds
3395 * the timer wheel base->lock on the CPU and another CPU wants
3396 * to access the timer (probably to cancel it). We can safely
3397 * ignore the boosting request, as the idle CPU runs this code
3398 * with interrupts disabled and will complete the lock
3399 * protected section without being interrupted. So there is no
3400 * real need to boost.
3401 */
3402 if (unlikely(p == rq->idle)) {
3403 WARN_ON(p != rq->curr);
3404 WARN_ON(p->pi_blocked_on);
3405 goto out_unlock;
3406 }
3407
Steven Rostedta8027072010-09-20 15:13:34 -04003408 trace_sched_pi_setprio(p, prio);
Andrew Mortond5f9f942007-05-08 20:27:06 -07003409 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003410 prev_class = p->sched_class;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003411 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003412 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003413 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02003414 dequeue_task(rq, p, DEQUEUE_SAVE);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003415 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003416 put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02003417
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003418 /*
3419 * Boosting condition are:
3420 * 1. -rt task is running and holds mutex A
3421 * --> -dl task blocks on mutex A
3422 *
3423 * 2. -dl task is running and holds mutex A
3424 * --> -dl task blocks on mutex A and could preempt the
3425 * running task
3426 */
3427 if (dl_prio(prio)) {
Oleg Nesterov466af292014-06-06 18:52:06 +02003428 struct task_struct *pi_task = rt_mutex_get_top_task(p);
3429 if (!dl_prio(p->normal_prio) ||
3430 (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003431 p->dl.dl_boosted = 1;
Peter Zijlstra1de64442015-09-30 17:44:13 +02003432 enqueue_flag |= ENQUEUE_REPLENISH;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003433 } else
3434 p->dl.dl_boosted = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003435 p->sched_class = &dl_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003436 } else if (rt_prio(prio)) {
3437 if (dl_prio(oldprio))
3438 p->dl.dl_boosted = 0;
3439 if (oldprio < prio)
Peter Zijlstra1de64442015-09-30 17:44:13 +02003440 enqueue_flag |= ENQUEUE_HEAD;
Ingo Molnardd41f592007-07-09 18:51:59 +02003441 p->sched_class = &rt_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003442 } else {
3443 if (dl_prio(oldprio))
3444 p->dl.dl_boosted = 0;
Brian Silverman746db942015-02-18 16:23:56 -08003445 if (rt_prio(oldprio))
3446 p->rt.timeout = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003447 p->sched_class = &fair_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003448 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003449
Ingo Molnarb29739f2006-06-27 02:54:51 -07003450 p->prio = prio;
3451
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003452 if (running)
3453 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003454 if (queued)
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003455 enqueue_task(rq, p, enqueue_flag);
Steven Rostedtcb469842008-01-25 21:08:22 +01003456
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003457 check_class_changed(rq, p, prev_class, oldprio);
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003458out_unlock:
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003459 preempt_disable(); /* avoid rq from going away on us */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003460 __task_rq_unlock(rq);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003461
3462 balance_callback(rq);
3463 preempt_enable();
Ingo Molnarb29739f2006-06-27 02:54:51 -07003464}
Ingo Molnarb29739f2006-06-27 02:54:51 -07003465#endif
Dario Faggiolid50dde52013-11-07 14:43:36 +01003466
Ingo Molnar36c8b582006-07-03 00:25:41 -07003467void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003469 int old_prio, delta, queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003471 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Dongsheng Yang75e45d52014-02-11 15:34:50 +08003473 if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 return;
3475 /*
3476 * We have to be careful, if called from sys_setpriority(),
3477 * the task might be in the middle of scheduling on another CPU.
3478 */
3479 rq = task_rq_lock(p, &flags);
3480 /*
3481 * The RT priorities are set via sched_setscheduler(), but we still
3482 * allow the 'normal' nice value to be set - but as expected
3483 * it wont have any effect on scheduling until the task is
Dario Faggioliaab03e02013-11-28 11:14:43 +01003484 * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003486 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 p->static_prio = NICE_TO_PRIO(nice);
3488 goto out_unlock;
3489 }
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003490 queued = task_on_rq_queued(p);
3491 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02003492 dequeue_task(rq, p, DEQUEUE_SAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003495 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003496 old_prio = p->prio;
3497 p->prio = effective_prio(p);
3498 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003500 if (queued) {
Peter Zijlstra1de64442015-09-30 17:44:13 +02003501 enqueue_task(rq, p, ENQUEUE_RESTORE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07003503 * If the task increased its priority or is running and
3504 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003506 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Kirill Tkhai88751252014-06-29 00:03:57 +04003507 resched_curr(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
3509out_unlock:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003510 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512EXPORT_SYMBOL(set_user_nice);
3513
Matt Mackalle43379f2005-05-01 08:59:00 -07003514/*
3515 * can_nice - check if a task can reduce its nice value
3516 * @p: task
3517 * @nice: nice value
3518 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003519int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07003520{
Matt Mackall024f4742005-08-18 11:24:19 -07003521 /* convert nice value [19,-20] to rlimit style value [1,40] */
Dongsheng Yang7aa2c012014-05-08 18:33:49 +09003522 int nice_rlim = nice_to_rlimit(nice);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003523
Jiri Slaby78d7d402010-03-05 13:42:54 -08003524 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07003525 capable(CAP_SYS_NICE));
3526}
3527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528#ifdef __ARCH_WANT_SYS_NICE
3529
3530/*
3531 * sys_nice - change the priority of the current process.
3532 * @increment: priority increment
3533 *
3534 * sys_setpriority is a more generic, but much slower function that
3535 * does similar things.
3536 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003537SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003539 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541 /*
3542 * Setpriority might change our priority at the same moment.
3543 * We don't have to worry. Conceptually one call occurs first
3544 * and we have a single winner.
3545 */
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003546 increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003547 nice = task_nice(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003549 nice = clamp_val(nice, MIN_NICE, MAX_NICE);
Matt Mackalle43379f2005-05-01 08:59:00 -07003550 if (increment < 0 && !can_nice(current, nice))
3551 return -EPERM;
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 retval = security_task_setnice(current, nice);
3554 if (retval)
3555 return retval;
3556
3557 set_user_nice(current, nice);
3558 return 0;
3559}
3560
3561#endif
3562
3563/**
3564 * task_prio - return the priority value of a given task.
3565 * @p: the task in question.
3566 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003567 * Return: The priority value as seen by users in /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 * RT tasks are offset by -200. Normal tasks are centered
3569 * around 0, value goes from -16 to +15.
3570 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003571int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
3573 return p->prio - MAX_RT_PRIO;
3574}
3575
3576/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 * idle_cpu - is a given cpu idle currently?
3578 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003579 *
3580 * Return: 1 if the CPU is currently idle. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 */
3582int idle_cpu(int cpu)
3583{
Thomas Gleixner908a3282011-09-15 15:32:06 +02003584 struct rq *rq = cpu_rq(cpu);
3585
3586 if (rq->curr != rq->idle)
3587 return 0;
3588
3589 if (rq->nr_running)
3590 return 0;
3591
3592#ifdef CONFIG_SMP
3593 if (!llist_empty(&rq->wake_list))
3594 return 0;
3595#endif
3596
3597 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600/**
3601 * idle_task - return the idle task for a given cpu.
3602 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003603 *
3604 * Return: The idle task for the cpu @cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003606struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
3608 return cpu_rq(cpu)->idle;
3609}
3610
3611/**
3612 * find_process_by_pid - find a process with a matching PID value.
3613 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003614 *
3615 * The task of @pid, if found. %NULL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003617static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003619 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
3621
Dario Faggioliaab03e02013-11-28 11:14:43 +01003622/*
3623 * This function initializes the sched_dl_entity of a newly becoming
3624 * SCHED_DEADLINE task.
3625 *
3626 * Only the static values are considered here, the actual runtime and the
3627 * absolute deadline will be properly calculated when the task is enqueued
3628 * for the first time with its new policy.
3629 */
3630static void
3631__setparam_dl(struct task_struct *p, const struct sched_attr *attr)
3632{
3633 struct sched_dl_entity *dl_se = &p->dl;
3634
Dario Faggioliaab03e02013-11-28 11:14:43 +01003635 dl_se->dl_runtime = attr->sched_runtime;
3636 dl_se->dl_deadline = attr->sched_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003637 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003638 dl_se->flags = attr->sched_flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01003639 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
Peter Zijlstra40767b02015-01-28 15:08:03 +01003640
3641 /*
3642 * Changing the parameters of a task is 'tricky' and we're not doing
3643 * the correct thing -- also see task_dead_dl() and switched_from_dl().
3644 *
3645 * What we SHOULD do is delay the bandwidth release until the 0-lag
3646 * point. This would include retaining the task_struct until that time
3647 * and change dl_overflow() to not immediately decrement the current
3648 * amount.
3649 *
3650 * Instead we retain the current runtime/deadline and let the new
3651 * parameters take effect after the current reservation period lapses.
3652 * This is safe (albeit pessimistic) because the 0-lag point is always
3653 * before the current scheduling deadline.
3654 *
3655 * We can still have temporary overloads because we do not delay the
3656 * change in bandwidth until that time; so admission control is
3657 * not on the safe side. It does however guarantee tasks will never
3658 * consume more than promised.
3659 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003660}
3661
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003662/*
3663 * sched_setparam() passes in -1 for its policy, to let the functions
3664 * it calls know not to change it.
3665 */
3666#define SETPARAM_POLICY -1
3667
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003668static void __setscheduler_params(struct task_struct *p,
3669 const struct sched_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670{
Dario Faggiolid50dde52013-11-07 14:43:36 +01003671 int policy = attr->sched_policy;
3672
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003673 if (policy == SETPARAM_POLICY)
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003674 policy = p->policy;
3675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 p->policy = policy;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003677
Dario Faggioliaab03e02013-11-28 11:14:43 +01003678 if (dl_policy(policy))
3679 __setparam_dl(p, attr);
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003680 else if (fair_policy(policy))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003681 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
3682
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003683 /*
3684 * __sched_setscheduler() ensures attr->sched_priority == 0 when
3685 * !rt_policy. Always setting this ensures that things like
3686 * getparam()/getattr() don't report silly values for !rt tasks.
3687 */
3688 p->rt_priority = attr->sched_priority;
Steven Rostedt383afd02014-03-11 19:24:20 -04003689 p->normal_prio = normal_prio(p);
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003690 set_load_weight(p);
3691}
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003692
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003693/* Actually do priority change: must hold pi & rq lock. */
3694static void __setscheduler(struct rq *rq, struct task_struct *p,
Thomas Gleixner0782e632015-05-05 19:49:49 +02003695 const struct sched_attr *attr, bool keep_boost)
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003696{
3697 __setscheduler_params(p, attr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003698
Steven Rostedt383afd02014-03-11 19:24:20 -04003699 /*
Thomas Gleixner0782e632015-05-05 19:49:49 +02003700 * Keep a potential priority boosting if called from
3701 * sched_setscheduler().
Steven Rostedt383afd02014-03-11 19:24:20 -04003702 */
Thomas Gleixner0782e632015-05-05 19:49:49 +02003703 if (keep_boost)
3704 p->prio = rt_mutex_get_effective_prio(p, normal_prio(p));
3705 else
3706 p->prio = normal_prio(p);
Steven Rostedt383afd02014-03-11 19:24:20 -04003707
Dario Faggioliaab03e02013-11-28 11:14:43 +01003708 if (dl_prio(p->prio))
3709 p->sched_class = &dl_sched_class;
3710 else if (rt_prio(p->prio))
Peter Zijlstraffd44db2009-11-10 20:12:01 +01003711 p->sched_class = &rt_sched_class;
3712 else
3713 p->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714}
Dario Faggioliaab03e02013-11-28 11:14:43 +01003715
3716static void
3717__getparam_dl(struct task_struct *p, struct sched_attr *attr)
3718{
3719 struct sched_dl_entity *dl_se = &p->dl;
3720
3721 attr->sched_priority = p->rt_priority;
3722 attr->sched_runtime = dl_se->dl_runtime;
3723 attr->sched_deadline = dl_se->dl_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003724 attr->sched_period = dl_se->dl_period;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003725 attr->sched_flags = dl_se->flags;
3726}
3727
3728/*
3729 * This function validates the new parameters of a -deadline task.
3730 * We ask for the deadline not being zero, and greater or equal
Harald Gustafsson755378a2013-11-07 14:43:40 +01003731 * than the runtime, as well as the period of being zero or
Dario Faggioli332ac172013-11-07 14:43:45 +01003732 * greater than deadline. Furthermore, we have to be sure that
Juri Lellib08278192014-05-13 14:11:31 +02003733 * user parameters are above the internal resolution of 1us (we
3734 * check sched_runtime only since it is always the smaller one) and
3735 * below 2^63 ns (we have to check both sched_deadline and
3736 * sched_period, as the latter can be zero).
Dario Faggioliaab03e02013-11-28 11:14:43 +01003737 */
3738static bool
3739__checkparam_dl(const struct sched_attr *attr)
3740{
Juri Lellib08278192014-05-13 14:11:31 +02003741 /* deadline != 0 */
3742 if (attr->sched_deadline == 0)
3743 return false;
3744
3745 /*
3746 * Since we truncate DL_SCALE bits, make sure we're at least
3747 * that big.
3748 */
3749 if (attr->sched_runtime < (1ULL << DL_SCALE))
3750 return false;
3751
3752 /*
3753 * Since we use the MSB for wrap-around and sign issues, make
3754 * sure it's not set (mind that period can be equal to zero).
3755 */
3756 if (attr->sched_deadline & (1ULL << 63) ||
3757 attr->sched_period & (1ULL << 63))
3758 return false;
3759
3760 /* runtime <= deadline <= period (if period != 0) */
3761 if ((attr->sched_period != 0 &&
3762 attr->sched_period < attr->sched_deadline) ||
3763 attr->sched_deadline < attr->sched_runtime)
3764 return false;
3765
3766 return true;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003767}
3768
David Howellsc69e8d92008-11-14 10:39:19 +11003769/*
3770 * check the target process has a UID that matches the current process's
3771 */
3772static bool check_same_owner(struct task_struct *p)
3773{
3774 const struct cred *cred = current_cred(), *pcred;
3775 bool match;
3776
3777 rcu_read_lock();
3778 pcred = __task_cred(p);
Eric W. Biederman9c806aa2012-02-02 18:54:02 -08003779 match = (uid_eq(cred->euid, pcred->euid) ||
3780 uid_eq(cred->euid, pcred->uid));
David Howellsc69e8d92008-11-14 10:39:19 +11003781 rcu_read_unlock();
3782 return match;
3783}
3784
Wanpeng Li75381602014-11-26 08:44:04 +08003785static bool dl_param_changed(struct task_struct *p,
3786 const struct sched_attr *attr)
3787{
3788 struct sched_dl_entity *dl_se = &p->dl;
3789
3790 if (dl_se->dl_runtime != attr->sched_runtime ||
3791 dl_se->dl_deadline != attr->sched_deadline ||
3792 dl_se->dl_period != attr->sched_period ||
3793 dl_se->flags != attr->sched_flags)
3794 return true;
3795
3796 return false;
3797}
3798
Dario Faggiolid50dde52013-11-07 14:43:36 +01003799static int __sched_setscheduler(struct task_struct *p,
3800 const struct sched_attr *attr,
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003801 bool user, bool pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
Steven Rostedt383afd02014-03-11 19:24:20 -04003803 int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
3804 MAX_RT_PRIO - 1 - attr->sched_priority;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003805 int retval, oldprio, oldpolicy = -1, queued, running;
Thomas Gleixner0782e632015-05-05 19:49:49 +02003806 int new_effective_prio, policy = attr->sched_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003808 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003809 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003810 int reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Steven Rostedt66e53932006-06-27 02:54:44 -07003812 /* may grab non-irq protected spin_locks */
3813 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814recheck:
3815 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02003816 if (policy < 0) {
3817 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003819 } else {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003820 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
Lennart Poetteringca94c442009-06-15 17:17:47 +02003821
Henrik Austad20f9cd22015-09-09 17:00:41 +02003822 if (!valid_policy(policy))
Lennart Poetteringca94c442009-06-15 17:17:47 +02003823 return -EINVAL;
3824 }
3825
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003826 if (attr->sched_flags & ~(SCHED_FLAG_RESET_ON_FORK))
3827 return -EINVAL;
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 /*
3830 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02003831 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
3832 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 */
Peter Zijlstra0bb040a2014-01-15 17:15:13 +01003834 if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
Dario Faggiolid50dde52013-11-07 14:43:36 +01003835 (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 return -EINVAL;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003837 if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
3838 (rt_policy(policy) != (attr->sched_priority != 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 return -EINVAL;
3840
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003841 /*
3842 * Allow unprivileged RT tasks to decrease priority:
3843 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10003844 if (user && !capable(CAP_SYS_NICE)) {
Dario Faggiolid50dde52013-11-07 14:43:36 +01003845 if (fair_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003846 if (attr->sched_nice < task_nice(p) &&
Peter Zijlstraeaad4512014-01-16 17:54:25 +01003847 !can_nice(p, attr->sched_nice))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003848 return -EPERM;
3849 }
3850
Ingo Molnare05606d2007-07-09 18:51:59 +02003851 if (rt_policy(policy)) {
Oleg Nesterova44702e82010-06-11 01:09:44 +02003852 unsigned long rlim_rtprio =
3853 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003854
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003855 /* can't set/change the rt policy */
3856 if (policy != p->policy && !rlim_rtprio)
3857 return -EPERM;
3858
3859 /* can't increase priority */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003860 if (attr->sched_priority > p->rt_priority &&
3861 attr->sched_priority > rlim_rtprio)
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003862 return -EPERM;
3863 }
Darren Hartc02aa732011-02-17 15:37:07 -08003864
Juri Lellid44753b2014-03-03 12:09:21 +01003865 /*
3866 * Can't set/change SCHED_DEADLINE policy at all for now
3867 * (safest behavior); in the future we would like to allow
3868 * unprivileged DL tasks to increase their relative deadline
3869 * or reduce their runtime (both ways reducing utilization)
3870 */
3871 if (dl_policy(policy))
3872 return -EPERM;
3873
Ingo Molnardd41f592007-07-09 18:51:59 +02003874 /*
Darren Hartc02aa732011-02-17 15:37:07 -08003875 * Treat SCHED_IDLE as nice 20. Only allow a switch to
3876 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02003877 */
Henrik Austad20f9cd22015-09-09 17:00:41 +02003878 if (idle_policy(p->policy) && !idle_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003879 if (!can_nice(p, task_nice(p)))
Darren Hartc02aa732011-02-17 15:37:07 -08003880 return -EPERM;
3881 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003882
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003883 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11003884 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003885 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003886
3887 /* Normal users shall not reset the sched_reset_on_fork flag */
3888 if (p->sched_reset_on_fork && !reset_on_fork)
3889 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003892 if (user) {
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003893 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003894 if (retval)
3895 return retval;
3896 }
3897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07003899 * make sure no PI-waiters arrive (or leave) while we are
3900 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003901 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003902 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 * runqueue lock must be held.
3904 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003905 rq = task_rq_lock(p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003906
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003907 /*
3908 * Changing the policy of the stop threads its a very bad idea
3909 */
3910 if (p == rq->stop) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003911 task_rq_unlock(rq, p, &flags);
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003912 return -EINVAL;
3913 }
3914
Dario Faggiolia51e9192011-03-24 14:00:18 +01003915 /*
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003916 * If not changing anything there's no need to proceed further,
3917 * but store a possible modification of reset_on_fork.
Dario Faggiolia51e9192011-03-24 14:00:18 +01003918 */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003919 if (unlikely(policy == p->policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003920 if (fair_policy(policy) && attr->sched_nice != task_nice(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003921 goto change;
3922 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
3923 goto change;
Wanpeng Li75381602014-11-26 08:44:04 +08003924 if (dl_policy(policy) && dl_param_changed(p, attr))
Dario Faggioliaab03e02013-11-28 11:14:43 +01003925 goto change;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003926
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003927 p->sched_reset_on_fork = reset_on_fork;
Namhyung Kim45afb172012-07-07 16:49:02 +09003928 task_rq_unlock(rq, p, &flags);
Dario Faggiolia51e9192011-03-24 14:00:18 +01003929 return 0;
3930 }
Dario Faggiolid50dde52013-11-07 14:43:36 +01003931change:
Dario Faggiolia51e9192011-03-24 14:00:18 +01003932
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003933 if (user) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003934#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003935 /*
3936 * Do not allow realtime tasks into groups that have no runtime
3937 * assigned.
3938 */
3939 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01003940 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
3941 !task_group_is_autogroup(task_group(p))) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003942 task_rq_unlock(rq, p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003943 return -EPERM;
3944 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003945#endif
Dario Faggioli332ac172013-11-07 14:43:45 +01003946#ifdef CONFIG_SMP
3947 if (dl_bandwidth_enabled() && dl_policy(policy)) {
3948 cpumask_t *span = rq->rd->span;
Dario Faggioli332ac172013-11-07 14:43:45 +01003949
3950 /*
3951 * Don't allow tasks with an affinity mask smaller than
3952 * the entire root_domain to become SCHED_DEADLINE. We
3953 * will also fail if there's no bandwidth available.
3954 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01003955 if (!cpumask_subset(span, &p->cpus_allowed) ||
3956 rq->rd->dl_bw.bw == 0) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003957 task_rq_unlock(rq, p, &flags);
3958 return -EPERM;
3959 }
3960 }
3961#endif
3962 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 /* recheck policy now with rq lock held */
3965 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
3966 policy = oldpolicy = -1;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003967 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 goto recheck;
3969 }
Dario Faggioli332ac172013-11-07 14:43:45 +01003970
3971 /*
3972 * If setscheduling to SCHED_DEADLINE (or changing the parameters
3973 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
3974 * is available.
3975 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01003976 if ((dl_policy(policy) || dl_task(p)) && dl_overflow(p, policy, attr)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003977 task_rq_unlock(rq, p, &flags);
3978 return -EBUSY;
3979 }
3980
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003981 p->sched_reset_on_fork = reset_on_fork;
3982 oldprio = p->prio;
3983
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003984 if (pi) {
3985 /*
3986 * Take priority boosted tasks into account. If the new
3987 * effective priority is unchanged, we just store the new
3988 * normal parameters and do not touch the scheduler class and
3989 * the runqueue. This will be done when the task deboost
3990 * itself.
3991 */
3992 new_effective_prio = rt_mutex_get_effective_prio(p, newprio);
3993 if (new_effective_prio == oldprio) {
3994 __setscheduler_params(p, attr);
3995 task_rq_unlock(rq, p, &flags);
3996 return 0;
3997 }
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003998 }
3999
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004000 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004001 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004002 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02004003 dequeue_task(rq, p, DEQUEUE_SAVE);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004004 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04004005 put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004006
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004007 prev_class = p->sched_class;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004008 __setscheduler(rq, p, attr, pi);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004009
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004010 if (running)
4011 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004012 if (queued) {
Peter Zijlstra1de64442015-09-30 17:44:13 +02004013 int enqueue_flags = ENQUEUE_RESTORE;
Thomas Gleixner81a44c52014-02-07 20:58:41 +01004014 /*
4015 * We enqueue to tail when the priority of a task is
4016 * increased (user space view).
4017 */
Peter Zijlstra1de64442015-09-30 17:44:13 +02004018 if (oldprio <= p->prio)
4019 enqueue_flags |= ENQUEUE_HEAD;
4020
4021 enqueue_task(rq, p, enqueue_flags);
Thomas Gleixner81a44c52014-02-07 20:58:41 +01004022 }
Steven Rostedtcb469842008-01-25 21:08:22 +01004023
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004024 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02004025 preempt_disable(); /* avoid rq from going away on us */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004026 task_rq_unlock(rq, p, &flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004027
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004028 if (pi)
4029 rt_mutex_adjust_pi(p);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004030
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02004031 /*
4032 * Run balance callbacks after we've adjusted the PI chain.
4033 */
4034 balance_callback(rq);
4035 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
4037 return 0;
4038}
Rusty Russell961ccdd2008-06-23 13:55:38 +10004039
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004040static int _sched_setscheduler(struct task_struct *p, int policy,
4041 const struct sched_param *param, bool check)
4042{
4043 struct sched_attr attr = {
4044 .sched_policy = policy,
4045 .sched_priority = param->sched_priority,
4046 .sched_nice = PRIO_TO_NICE(p->static_prio),
4047 };
4048
Steven Rostedtc13db6b2014-07-23 11:28:26 -04004049 /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
4050 if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004051 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
4052 policy &= ~SCHED_RESET_ON_FORK;
4053 attr.sched_policy = policy;
4054 }
4055
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004056 return __sched_setscheduler(p, &attr, check, true);
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004057}
Rusty Russell961ccdd2008-06-23 13:55:38 +10004058/**
4059 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
4060 * @p: the task in question.
4061 * @policy: new policy.
4062 * @param: structure containing the new RT priority.
4063 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004064 * Return: 0 on success. An error code otherwise.
4065 *
Rusty Russell961ccdd2008-06-23 13:55:38 +10004066 * NOTE that the task may be already dead.
4067 */
4068int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07004069 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10004070{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004071 return _sched_setscheduler(p, policy, param, true);
Rusty Russell961ccdd2008-06-23 13:55:38 +10004072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073EXPORT_SYMBOL_GPL(sched_setscheduler);
4074
Dario Faggiolid50dde52013-11-07 14:43:36 +01004075int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
4076{
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004077 return __sched_setscheduler(p, attr, true, true);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004078}
4079EXPORT_SYMBOL_GPL(sched_setattr);
4080
Rusty Russell961ccdd2008-06-23 13:55:38 +10004081/**
4082 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
4083 * @p: the task in question.
4084 * @policy: new policy.
4085 * @param: structure containing the new RT priority.
4086 *
4087 * Just like sched_setscheduler, only don't bother checking if the
4088 * current context has permission. For example, this is needed in
4089 * stop_machine(): we create temporary high priority worker threads,
4090 * but our caller might not have that capability.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004091 *
4092 * Return: 0 on success. An error code otherwise.
Rusty Russell961ccdd2008-06-23 13:55:38 +10004093 */
4094int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07004095 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10004096{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004097 return _sched_setscheduler(p, policy, param, false);
Rusty Russell961ccdd2008-06-23 13:55:38 +10004098}
Davidlohr Bueso84778472015-09-02 01:28:44 -07004099EXPORT_SYMBOL_GPL(sched_setscheduler_nocheck);
Rusty Russell961ccdd2008-06-23 13:55:38 +10004100
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004101static int
4102do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 struct sched_param lparam;
4105 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004106 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
4108 if (!param || pid < 0)
4109 return -EINVAL;
4110 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4111 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004112
4113 rcu_read_lock();
4114 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004116 if (p != NULL)
4117 retval = sched_setscheduler(p, policy, &lparam);
4118 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 return retval;
4121}
4122
Dario Faggiolid50dde52013-11-07 14:43:36 +01004123/*
4124 * Mimics kernel/events/core.c perf_copy_attr().
4125 */
4126static int sched_copy_attr(struct sched_attr __user *uattr,
4127 struct sched_attr *attr)
4128{
4129 u32 size;
4130 int ret;
4131
4132 if (!access_ok(VERIFY_WRITE, uattr, SCHED_ATTR_SIZE_VER0))
4133 return -EFAULT;
4134
4135 /*
4136 * zero the full structure, so that a short copy will be nice.
4137 */
4138 memset(attr, 0, sizeof(*attr));
4139
4140 ret = get_user(size, &uattr->size);
4141 if (ret)
4142 return ret;
4143
4144 if (size > PAGE_SIZE) /* silly large */
4145 goto err_size;
4146
4147 if (!size) /* abi compat */
4148 size = SCHED_ATTR_SIZE_VER0;
4149
4150 if (size < SCHED_ATTR_SIZE_VER0)
4151 goto err_size;
4152
4153 /*
4154 * If we're handed a bigger struct than we know of,
4155 * ensure all the unknown bits are 0 - i.e. new
4156 * user-space does not rely on any kernel feature
4157 * extensions we dont know about yet.
4158 */
4159 if (size > sizeof(*attr)) {
4160 unsigned char __user *addr;
4161 unsigned char __user *end;
4162 unsigned char val;
4163
4164 addr = (void __user *)uattr + sizeof(*attr);
4165 end = (void __user *)uattr + size;
4166
4167 for (; addr < end; addr++) {
4168 ret = get_user(val, addr);
4169 if (ret)
4170 return ret;
4171 if (val)
4172 goto err_size;
4173 }
4174 size = sizeof(*attr);
4175 }
4176
4177 ret = copy_from_user(attr, uattr, size);
4178 if (ret)
4179 return -EFAULT;
4180
4181 /*
4182 * XXX: do we want to be lenient like existing syscalls; or do we want
4183 * to be strict and return an error on out-of-bounds values?
4184 */
Dongsheng Yang75e45d52014-02-11 15:34:50 +08004185 attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004186
Michael Kerriske78c7bc2014-05-09 16:54:28 +02004187 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004188
4189err_size:
4190 put_user(sizeof(*attr), &uattr->size);
Michael Kerriske78c7bc2014-05-09 16:54:28 +02004191 return -E2BIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004192}
4193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194/**
4195 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4196 * @pid: the pid in question.
4197 * @policy: new policy.
4198 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004199 *
4200 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004202SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
4203 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
Jason Baronc21761f2006-01-18 17:43:03 -08004205 /* negative values for policy are not valid */
4206 if (policy < 0)
4207 return -EINVAL;
4208
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 return do_sched_setscheduler(pid, policy, param);
4210}
4211
4212/**
4213 * sys_sched_setparam - set/change the RT priority of a thread
4214 * @pid: the pid in question.
4215 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004216 *
4217 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004219SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
Steven Rostedtc13db6b2014-07-23 11:28:26 -04004221 return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222}
4223
4224/**
Dario Faggiolid50dde52013-11-07 14:43:36 +01004225 * sys_sched_setattr - same as above, but with extended sched_attr
4226 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01004227 * @uattr: structure containing the extended parameters.
Masanari Iidadb66d752014-04-18 01:59:15 +09004228 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004229 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004230SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
4231 unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004232{
4233 struct sched_attr attr;
4234 struct task_struct *p;
4235 int retval;
4236
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004237 if (!uattr || pid < 0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004238 return -EINVAL;
4239
Michael Kerrisk143cf232014-05-09 16:54:15 +02004240 retval = sched_copy_attr(uattr, &attr);
4241 if (retval)
4242 return retval;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004243
Richard Weinbergerb14ed2c2014-06-02 22:38:34 +02004244 if ((int)attr.sched_policy < 0)
Peter Zijlstradbdb2272014-05-09 10:49:03 +02004245 return -EINVAL;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004246
4247 rcu_read_lock();
4248 retval = -ESRCH;
4249 p = find_process_by_pid(pid);
4250 if (p != NULL)
4251 retval = sched_setattr(p, &attr);
4252 rcu_read_unlock();
4253
4254 return retval;
4255}
4256
4257/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4259 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004260 *
4261 * Return: On success, the policy of the thread. Otherwise, a negative error
4262 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004264SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004266 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004267 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
4269 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004270 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004273 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 p = find_process_by_pid(pid);
4275 if (p) {
4276 retval = security_task_getscheduler(p);
4277 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02004278 retval = p->policy
4279 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004281 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 return retval;
4283}
4284
4285/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02004286 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 * @pid: the pid in question.
4288 * @param: structure containing the RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004289 *
4290 * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
4291 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004293SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02004295 struct sched_param lp = { .sched_priority = 0 };
Ingo Molnar36c8b582006-07-03 00:25:41 -07004296 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004297 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
4299 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004300 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004302 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 p = find_process_by_pid(pid);
4304 retval = -ESRCH;
4305 if (!p)
4306 goto out_unlock;
4307
4308 retval = security_task_getscheduler(p);
4309 if (retval)
4310 goto out_unlock;
4311
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02004312 if (task_has_rt_policy(p))
4313 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004314 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
4316 /*
4317 * This one might sleep, we cannot do it with a spinlock held ...
4318 */
4319 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return retval;
4322
4323out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004324 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return retval;
4326}
4327
Dario Faggiolid50dde52013-11-07 14:43:36 +01004328static int sched_read_attr(struct sched_attr __user *uattr,
4329 struct sched_attr *attr,
4330 unsigned int usize)
4331{
4332 int ret;
4333
4334 if (!access_ok(VERIFY_WRITE, uattr, usize))
4335 return -EFAULT;
4336
4337 /*
4338 * If we're handed a smaller struct than we know of,
4339 * ensure all the unknown bits are 0 - i.e. old
4340 * user-space does not get uncomplete information.
4341 */
4342 if (usize < sizeof(*attr)) {
4343 unsigned char *addr;
4344 unsigned char *end;
4345
4346 addr = (void *)attr + usize;
4347 end = (void *)attr + sizeof(*attr);
4348
4349 for (; addr < end; addr++) {
4350 if (*addr)
Michael Kerrisk22400672014-05-09 16:54:33 +02004351 return -EFBIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004352 }
4353
4354 attr->size = usize;
4355 }
4356
Vegard Nossum4efbc452014-02-16 22:24:17 +01004357 ret = copy_to_user(uattr, attr, attr->size);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004358 if (ret)
4359 return -EFAULT;
4360
Michael Kerrisk22400672014-05-09 16:54:33 +02004361 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004362}
4363
4364/**
Dario Faggioliaab03e02013-11-28 11:14:43 +01004365 * sys_sched_getattr - similar to sched_getparam, but with sched_attr
Dario Faggiolid50dde52013-11-07 14:43:36 +01004366 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01004367 * @uattr: structure containing the extended parameters.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004368 * @size: sizeof(attr) for fwd/bwd comp.
Masanari Iidadb66d752014-04-18 01:59:15 +09004369 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004370 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004371SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
4372 unsigned int, size, unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004373{
4374 struct sched_attr attr = {
4375 .size = sizeof(struct sched_attr),
4376 };
4377 struct task_struct *p;
4378 int retval;
4379
4380 if (!uattr || pid < 0 || size > PAGE_SIZE ||
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004381 size < SCHED_ATTR_SIZE_VER0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004382 return -EINVAL;
4383
4384 rcu_read_lock();
4385 p = find_process_by_pid(pid);
4386 retval = -ESRCH;
4387 if (!p)
4388 goto out_unlock;
4389
4390 retval = security_task_getscheduler(p);
4391 if (retval)
4392 goto out_unlock;
4393
4394 attr.sched_policy = p->policy;
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004395 if (p->sched_reset_on_fork)
4396 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004397 if (task_has_dl_policy(p))
4398 __getparam_dl(p, &attr);
4399 else if (task_has_rt_policy(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01004400 attr.sched_priority = p->rt_priority;
4401 else
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05004402 attr.sched_nice = task_nice(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004403
4404 rcu_read_unlock();
4405
4406 retval = sched_read_attr(uattr, &attr, size);
4407 return retval;
4408
4409out_unlock:
4410 rcu_read_unlock();
4411 return retval;
4412}
4413
Rusty Russell96f874e22008-11-25 02:35:14 +10304414long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304416 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004417 struct task_struct *p;
4418 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004420 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
4422 p = find_process_by_pid(pid);
4423 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004424 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 return -ESRCH;
4426 }
4427
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004428 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004430 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
Tejun Heo14a40ff2013-03-19 13:45:20 -07004432 if (p->flags & PF_NO_SETAFFINITY) {
4433 retval = -EINVAL;
4434 goto out_put_task;
4435 }
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304436 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
4437 retval = -ENOMEM;
4438 goto out_put_task;
4439 }
4440 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
4441 retval = -ENOMEM;
4442 goto out_free_cpus_allowed;
4443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 retval = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004445 if (!check_same_owner(p)) {
4446 rcu_read_lock();
4447 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
4448 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004449 goto out_free_new_mask;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004450 }
4451 rcu_read_unlock();
4452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004454 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07004455 if (retval)
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004456 goto out_free_new_mask;
David Quigleye7834f82006-06-23 02:03:59 -07004457
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004458
4459 cpuset_cpus_allowed(p, cpus_allowed);
4460 cpumask_and(new_mask, in_mask, cpus_allowed);
4461
Dario Faggioli332ac172013-11-07 14:43:45 +01004462 /*
4463 * Since bandwidth control happens on root_domain basis,
4464 * if admission test is enabled, we only admit -deadline
4465 * tasks allowed to run on all the CPUs in the task's
4466 * root_domain.
4467 */
4468#ifdef CONFIG_SMP
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004469 if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
4470 rcu_read_lock();
4471 if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01004472 retval = -EBUSY;
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004473 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004474 goto out_free_new_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01004475 }
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004476 rcu_read_unlock();
Dario Faggioli332ac172013-11-07 14:43:45 +01004477 }
4478#endif
Peter Zijlstra49246272010-10-17 21:46:10 +02004479again:
Peter Zijlstra25834c72015-05-15 17:43:34 +02004480 retval = __set_cpus_allowed_ptr(p, new_mask, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Paul Menage8707d8b2007-10-18 23:40:22 -07004482 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304483 cpuset_cpus_allowed(p, cpus_allowed);
4484 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07004485 /*
4486 * We must have raced with a concurrent cpuset
4487 * update. Just reset the cpus_allowed to the
4488 * cpuset's cpus_allowed
4489 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304490 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004491 goto again;
4492 }
4493 }
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004494out_free_new_mask:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304495 free_cpumask_var(new_mask);
4496out_free_cpus_allowed:
4497 free_cpumask_var(cpus_allowed);
4498out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 return retval;
4501}
4502
4503static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e22008-11-25 02:35:14 +10304504 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
Rusty Russell96f874e22008-11-25 02:35:14 +10304506 if (len < cpumask_size())
4507 cpumask_clear(new_mask);
4508 else if (len > cpumask_size())
4509 len = cpumask_size();
4510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4512}
4513
4514/**
4515 * sys_sched_setaffinity - set the cpu affinity of a process
4516 * @pid: pid of the process
4517 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4518 * @user_mask_ptr: user-space pointer to the new cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004519 *
4520 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004522SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
4523 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304525 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 int retval;
4527
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304528 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4529 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304531 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
4532 if (retval == 0)
4533 retval = sched_setaffinity(pid, new_mask);
4534 free_cpumask_var(new_mask);
4535 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536}
4537
Rusty Russell96f874e22008-11-25 02:35:14 +10304538long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004540 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00004541 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004544 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
4546 retval = -ESRCH;
4547 p = find_process_by_pid(pid);
4548 if (!p)
4549 goto out_unlock;
4550
David Quigleye7834f82006-06-23 02:03:59 -07004551 retval = security_task_getscheduler(p);
4552 if (retval)
4553 goto out_unlock;
4554
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004555 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02004556 cpumask_and(mask, &p->cpus_allowed, cpu_active_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004557 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
4559out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004560 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
Ulrich Drepper9531b622007-08-09 11:16:46 +02004562 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563}
4564
4565/**
4566 * sys_sched_getaffinity - get the cpu affinity of a process
4567 * @pid: pid of the process
4568 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4569 * @user_mask_ptr: user-space pointer to hold the current cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004570 *
4571 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004573SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
4574 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575{
4576 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10304577 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Anton Blanchard84fba5e2010-04-06 17:02:19 +10004579 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004580 return -EINVAL;
4581 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 return -EINVAL;
4583
Rusty Russellf17c8602008-11-25 02:35:11 +10304584 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
4585 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Rusty Russellf17c8602008-11-25 02:35:11 +10304587 ret = sched_getaffinity(pid, mask);
4588 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09004589 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004590
4591 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10304592 ret = -EFAULT;
4593 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004594 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10304595 }
4596 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Rusty Russellf17c8602008-11-25 02:35:11 +10304598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599}
4600
4601/**
4602 * sys_sched_yield - yield the current processor to other threads.
4603 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004604 * This function yields the current CPU to other tasks. If there are no
4605 * other threads running on this CPU then this function will return.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004606 *
4607 * Return: 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004609SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004611 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Ingo Molnar2d723762007-10-15 17:00:12 +02004613 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004614 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
4616 /*
4617 * Since we are going to call schedule() anyway, there's
4618 * no need to preempt or enable interrupts:
4619 */
4620 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004621 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01004622 do_raw_spin_unlock(&rq->lock);
Thomas Gleixnerba74c142011-03-21 13:32:17 +01004623 sched_preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
4625 schedule();
4626
4627 return 0;
4628}
4629
Herbert Xu02b67cc32008-01-25 21:08:28 +01004630int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004632 if (should_resched(0)) {
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004633 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 return 1;
4635 }
4636 return 0;
4637}
Herbert Xu02b67cc32008-01-25 21:08:28 +01004638EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
4640/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004641 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 * call schedule, and on return reacquire the lock.
4643 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004644 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 * operations here to prevent schedule() from being called twice (once via
4646 * spin_unlock(), once by hand).
4647 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004648int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004650 int resched = should_resched(PREEMPT_LOCK_OFFSET);
Jan Kara6df3cec2005-06-13 15:52:32 -07004651 int ret = 0;
4652
Peter Zijlstraf607c662009-07-20 19:16:29 +02004653 lockdep_assert_held(lock);
4654
Paul E. McKenney4a81e832014-06-20 16:49:01 -07004655 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004657 if (resched)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004658 preempt_schedule_common();
Nick Piggin95c354f2008-01-30 13:31:20 +01004659 else
4660 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004661 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004664 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004666EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004668int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
4670 BUG_ON(!in_softirq());
4671
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004672 if (should_resched(SOFTIRQ_DISABLE_OFFSET)) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004673 local_bh_enable();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004674 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 local_bh_disable();
4676 return 1;
4677 }
4678 return 0;
4679}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004680EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682/**
4683 * yield - yield the current processor to other threads.
4684 *
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01004685 * Do not ever use this function, there's a 99% chance you're doing it wrong.
4686 *
4687 * The scheduler is at all times free to pick the calling task as the most
4688 * eligible task to run, if removing the yield() call from your code breaks
4689 * it, its already broken.
4690 *
4691 * Typical broken usage is:
4692 *
4693 * while (!event)
4694 * yield();
4695 *
4696 * where one assumes that yield() will let 'the other' process run that will
4697 * make event true. If the current task is a SCHED_FIFO task that will never
4698 * happen. Never use yield() as a progress guarantee!!
4699 *
4700 * If you want to use yield() to wait for something, use wait_event().
4701 * If you want to use yield() to be 'nice' for others, use cond_resched().
4702 * If you still want to use yield(), do not!
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 */
4704void __sched yield(void)
4705{
4706 set_current_state(TASK_RUNNING);
4707 sys_sched_yield();
4708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709EXPORT_SYMBOL(yield);
4710
Mike Galbraithd95f4122011-02-01 09:50:51 -05004711/**
4712 * yield_to - yield the current processor to another thread in
4713 * your thread group, or accelerate that thread toward the
4714 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07004715 * @p: target task
4716 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05004717 *
4718 * It's the caller's job to ensure that the target task struct
4719 * can't go away on us before we can do any checks.
4720 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004721 * Return:
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304722 * true (>0) if we indeed boosted the target task.
4723 * false (0) if we failed to boost the target.
4724 * -ESRCH if there's no task to yield to.
Mike Galbraithd95f4122011-02-01 09:50:51 -05004725 */
Dan Carpenterfa933842014-05-23 13:20:42 +03004726int __sched yield_to(struct task_struct *p, bool preempt)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004727{
4728 struct task_struct *curr = current;
4729 struct rq *rq, *p_rq;
4730 unsigned long flags;
Dan Carpenterc3c18642013-02-05 14:37:51 +03004731 int yielded = 0;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004732
4733 local_irq_save(flags);
4734 rq = this_rq();
4735
4736again:
4737 p_rq = task_rq(p);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304738 /*
4739 * If we're the only runnable task on the rq and target rq also
4740 * has only one task, there's absolutely no point in yielding.
4741 */
4742 if (rq->nr_running == 1 && p_rq->nr_running == 1) {
4743 yielded = -ESRCH;
4744 goto out_irq;
4745 }
4746
Mike Galbraithd95f4122011-02-01 09:50:51 -05004747 double_rq_lock(rq, p_rq);
Shigeru Yoshida39e24d8f2013-11-23 18:38:01 +09004748 if (task_rq(p) != p_rq) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004749 double_rq_unlock(rq, p_rq);
4750 goto again;
4751 }
4752
4753 if (!curr->sched_class->yield_to_task)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304754 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004755
4756 if (curr->sched_class != p->sched_class)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304757 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004758
4759 if (task_running(p_rq, p) || p->state)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304760 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004761
4762 yielded = curr->sched_class->yield_to_task(rq, p, preempt);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004763 if (yielded) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004764 schedstat_inc(rq, yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004765 /*
4766 * Make p's CPU reschedule; pick_next_entity takes care of
4767 * fairness.
4768 */
4769 if (preempt && rq != p_rq)
Kirill Tkhai88751252014-06-29 00:03:57 +04004770 resched_curr(p_rq);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004771 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05004772
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304773out_unlock:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004774 double_rq_unlock(rq, p_rq);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304775out_irq:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004776 local_irq_restore(flags);
4777
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304778 if (yielded > 0)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004779 schedule();
4780
4781 return yielded;
4782}
4783EXPORT_SYMBOL_GPL(yield_to);
4784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004786 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789long __sched io_schedule_timeout(long timeout)
4790{
NeilBrown9cff8ad2015-02-13 15:49:17 +11004791 int old_iowait = current->in_iowait;
4792 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 long ret;
4794
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004795 current->in_iowait = 1;
Shaohua Li10d784e2015-05-08 10:51:29 -07004796 blk_schedule_flush_plug(current);
NeilBrown9cff8ad2015-02-13 15:49:17 +11004797
4798 delayacct_blkio_start();
4799 rq = raw_rq();
4800 atomic_inc(&rq->nr_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 ret = schedule_timeout(timeout);
NeilBrown9cff8ad2015-02-13 15:49:17 +11004802 current->in_iowait = old_iowait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004804 delayacct_blkio_end();
NeilBrown9cff8ad2015-02-13 15:49:17 +11004805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 return ret;
4807}
NeilBrown9cff8ad2015-02-13 15:49:17 +11004808EXPORT_SYMBOL(io_schedule_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
4810/**
4811 * sys_sched_get_priority_max - return maximum RT priority.
4812 * @policy: scheduling class.
4813 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004814 * Return: On success, this syscall returns the maximum
4815 * rt_priority that can be used by a given scheduling class.
4816 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004818SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819{
4820 int ret = -EINVAL;
4821
4822 switch (policy) {
4823 case SCHED_FIFO:
4824 case SCHED_RR:
4825 ret = MAX_USER_RT_PRIO-1;
4826 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004827 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004829 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004830 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 ret = 0;
4832 break;
4833 }
4834 return ret;
4835}
4836
4837/**
4838 * sys_sched_get_priority_min - return minimum RT priority.
4839 * @policy: scheduling class.
4840 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004841 * Return: On success, this syscall returns the minimum
4842 * rt_priority that can be used by a given scheduling class.
4843 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004845SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
4847 int ret = -EINVAL;
4848
4849 switch (policy) {
4850 case SCHED_FIFO:
4851 case SCHED_RR:
4852 ret = 1;
4853 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004854 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004856 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004857 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 ret = 0;
4859 }
4860 return ret;
4861}
4862
4863/**
4864 * sys_sched_rr_get_interval - return the default timeslice of a process.
4865 * @pid: pid of the process.
4866 * @interval: userspace pointer to the timeslice value.
4867 *
4868 * this syscall writes the default timeslice value of a given process
4869 * into the user-space timespec buffer. A value of '0' means infinity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004870 *
4871 * Return: On success, 0 and the timeslice is in @interval. Otherwise,
4872 * an error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01004874SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01004875 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004877 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004878 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004879 unsigned long flags;
4880 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004881 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
4884 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004885 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
4887 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004888 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 p = find_process_by_pid(pid);
4890 if (!p)
4891 goto out_unlock;
4892
4893 retval = security_task_getscheduler(p);
4894 if (retval)
4895 goto out_unlock;
4896
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004897 rq = task_rq_lock(p, &flags);
Peter Zijlstraa57beec2014-01-27 11:54:13 +01004898 time_slice = 0;
4899 if (p->sched_class->get_rr_interval)
4900 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004901 task_rq_unlock(rq, p, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004902
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004903 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004904 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004907
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004909 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 return retval;
4911}
4912
Steven Rostedt7c731e02008-05-12 21:20:41 +02004913static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004914
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004915void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 unsigned long free = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004918 int ppid;
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09004919 unsigned long state = p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09004921 if (state)
4922 state = __ffs(state) + 1;
Erik Gilling28d06862010-11-19 18:08:51 -08004923 printk(KERN_INFO "%-15.15s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004924 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02004925#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004927 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004929 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930#else
4931 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004932 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004934 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935#endif
4936#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05004937 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938#endif
Oleg Nesterova90e9842014-12-10 15:45:21 -08004939 ppid = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004940 rcu_read_lock();
Oleg Nesterova90e9842014-12-10 15:45:21 -08004941 if (pid_alive(p))
4942 ppid = task_pid_nr(rcu_dereference(p->real_parent));
Paul E. McKenney4e797522012-11-07 13:35:32 -08004943 rcu_read_unlock();
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004944 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
Paul E. McKenney4e797522012-11-07 13:35:32 -08004945 task_pid_nr(p), ppid,
David Rientjesaa47b7e2009-05-04 01:38:05 -07004946 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
Tejun Heo3d1cb202013-04-30 15:27:22 -07004948 print_worker_info(KERN_INFO, p);
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01004949 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950}
4951
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004952void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004954 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Ingo Molnar4bd77322007-07-11 21:21:47 +02004956#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004957 printk(KERN_INFO
4958 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004960 printk(KERN_INFO
4961 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02004963 rcu_read_lock();
Oleg Nesterov5d07f422014-08-13 21:19:53 +02004964 for_each_process_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 /*
4966 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004967 * console might take a lot of time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 */
4969 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07004970 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004971 sched_show_task(p);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02004972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07004974 touch_all_softlockup_watchdogs();
4975
Ingo Molnardd41f592007-07-09 18:51:59 +02004976#ifdef CONFIG_SCHED_DEBUG
4977 sysrq_sched_debug_show();
4978#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02004979 rcu_read_unlock();
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004980 /*
4981 * Only show locks if all tasks are dumped:
4982 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02004983 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004984 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985}
4986
Paul Gortmaker0db06282013-06-19 14:53:51 -04004987void init_idle_bootup_task(struct task_struct *idle)
Ingo Molnar1df21052007-07-09 18:51:58 +02004988{
Ingo Molnardd41f592007-07-09 18:51:59 +02004989 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02004990}
4991
Ingo Molnarf340c0d2005-06-28 16:40:42 +02004992/**
4993 * init_idle - set up an idle thread for a given CPU
4994 * @idle: task in question
4995 * @cpu: cpu the idle task belongs to
4996 *
4997 * NOTE: this function does not set the idle thread's NEED_RESCHED
4998 * flag, to make booting more robust.
4999 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005000void init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005002 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 unsigned long flags;
5004
Peter Zijlstra25834c72015-05-15 17:43:34 +02005005 raw_spin_lock_irqsave(&idle->pi_lock, flags);
5006 raw_spin_lock(&rq->lock);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01005007
Rik van Riel5e1576e2013-10-07 11:29:26 +01005008 __sched_fork(0, idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01005009 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02005010 idle->se.exec_start = sched_clock();
5011
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02005012#ifdef CONFIG_SMP
5013 /*
5014 * Its possible that init_idle() gets called multiple times on a task,
5015 * in that case do_set_cpus_allowed() will not do the right thing.
5016 *
5017 * And since this is boot we can forgo the serialization.
5018 */
5019 set_cpus_allowed_common(idle, cpumask_of(cpu));
5020#endif
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005021 /*
5022 * We're having a chicken and egg problem, even though we are
5023 * holding rq->lock, the cpu isn't yet set to this cpu so the
5024 * lockdep check in task_group() will fail.
5025 *
5026 * Similar case to sched_fork(). / Alternatively we could
5027 * use task_rq_lock() here and obtain the other rq->lock.
5028 *
5029 * Silence PROVE_RCU
5030 */
5031 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02005032 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02005033 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 rq->curr = rq->idle = idle;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005036 idle->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02005037#ifdef CONFIG_SMP
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02005038 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07005039#endif
Peter Zijlstra25834c72015-05-15 17:43:34 +02005040 raw_spin_unlock(&rq->lock);
5041 raw_spin_unlock_irqrestore(&idle->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
5043 /* Set the preempt count _outside_ the spinlocks! */
Peter Zijlstra01028742013-08-14 14:55:46 +02005044 init_idle_preempt_count(idle, cpu);
Jonathan Corbet625f2a32011-04-22 11:19:10 -06005045
Ingo Molnardd41f592007-07-09 18:51:59 +02005046 /*
5047 * The idle tasks have their own, simple scheduling class:
5048 */
5049 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05005050 ftrace_graph_init_idle_task(idle, cpu);
Frederic Weisbecker45eacc62013-05-15 22:16:32 +02005051 vtime_init_idle(idle, cpu);
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02005052#ifdef CONFIG_SMP
Carsten Emdef1c6f1a2011-10-26 23:14:16 +02005053 sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
5054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055}
5056
Juri Lellif82f8042014-10-07 09:52:11 +01005057int cpuset_cpumask_can_shrink(const struct cpumask *cur,
5058 const struct cpumask *trial)
5059{
5060 int ret = 1, trial_cpus;
5061 struct dl_bw *cur_dl_b;
5062 unsigned long flags;
5063
Mike Galbraithbb2bc552015-01-28 04:53:55 +01005064 if (!cpumask_weight(cur))
5065 return ret;
5066
Juri Lelli75e23e492014-10-28 11:54:46 +00005067 rcu_read_lock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01005068 cur_dl_b = dl_bw_of(cpumask_any(cur));
5069 trial_cpus = cpumask_weight(trial);
5070
5071 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
5072 if (cur_dl_b->bw != -1 &&
5073 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
5074 ret = 0;
5075 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00005076 rcu_read_unlock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01005077
5078 return ret;
5079}
5080
Juri Lelli7f514122014-09-19 10:22:40 +01005081int task_can_attach(struct task_struct *p,
5082 const struct cpumask *cs_cpus_allowed)
5083{
5084 int ret = 0;
5085
5086 /*
5087 * Kthreads which disallow setaffinity shouldn't be moved
5088 * to a new cpuset; we don't want to change their cpu
5089 * affinity and isolating such threads by their set of
5090 * allowed nodes is unnecessary. Thus, cpusets are not
5091 * applicable for such threads. This prevents checking for
5092 * success of set_cpus_allowed_ptr() on all attached tasks
5093 * before cpus_allowed may be changed.
5094 */
5095 if (p->flags & PF_NO_SETAFFINITY) {
5096 ret = -EINVAL;
5097 goto out;
5098 }
5099
5100#ifdef CONFIG_SMP
5101 if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
5102 cs_cpus_allowed)) {
5103 unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
5104 cs_cpus_allowed);
Juri Lelli75e23e492014-10-28 11:54:46 +00005105 struct dl_bw *dl_b;
Juri Lelli7f514122014-09-19 10:22:40 +01005106 bool overflow;
5107 int cpus;
5108 unsigned long flags;
5109
Juri Lelli75e23e492014-10-28 11:54:46 +00005110 rcu_read_lock_sched();
5111 dl_b = dl_bw_of(dest_cpu);
Juri Lelli7f514122014-09-19 10:22:40 +01005112 raw_spin_lock_irqsave(&dl_b->lock, flags);
5113 cpus = dl_bw_cpus(dest_cpu);
5114 overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
5115 if (overflow)
5116 ret = -EBUSY;
5117 else {
5118 /*
5119 * We reserve space for this task in the destination
5120 * root_domain, as we can't fail after this point.
5121 * We will free resources in the source root_domain
5122 * later on (see set_cpus_allowed_dl()).
5123 */
5124 __dl_add(dl_b, p->dl.dl_bw);
5125 }
5126 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00005127 rcu_read_unlock_sched();
Juri Lelli7f514122014-09-19 10:22:40 +01005128
5129 }
5130#endif
5131out:
5132 return ret;
5133}
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
Mel Gormane6628d52013-10-07 11:29:02 +01005137#ifdef CONFIG_NUMA_BALANCING
5138/* Migrate current task p to target_cpu */
5139int migrate_task_to(struct task_struct *p, int target_cpu)
5140{
5141 struct migration_arg arg = { p, target_cpu };
5142 int curr_cpu = task_cpu(p);
5143
5144 if (curr_cpu == target_cpu)
5145 return 0;
5146
5147 if (!cpumask_test_cpu(target_cpu, tsk_cpus_allowed(p)))
5148 return -EINVAL;
5149
5150 /* TODO: This is not properly updating schedstats */
5151
Mel Gorman286549d2014-01-21 15:51:03 -08005152 trace_sched_move_numa(p, curr_cpu, target_cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01005153 return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
5154}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005155
5156/*
5157 * Requeue a task on a given node and accurately track the number of NUMA
5158 * tasks on the runqueues
5159 */
5160void sched_setnuma(struct task_struct *p, int nid)
5161{
5162 struct rq *rq;
5163 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005164 bool queued, running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005165
5166 rq = task_rq_lock(p, &flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005167 queued = task_on_rq_queued(p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005168 running = task_current(rq, p);
5169
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005170 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02005171 dequeue_task(rq, p, DEQUEUE_SAVE);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005172 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04005173 put_prev_task(rq, p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005174
5175 p->numa_preferred_nid = nid;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005176
5177 if (running)
5178 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005179 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02005180 enqueue_task(rq, p, ENQUEUE_RESTORE);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005181 task_rq_unlock(rq, p, &flags);
5182}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02005183#endif /* CONFIG_NUMA_BALANCING */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
5185#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar48f24c42006-07-03 00:25:40 -07005186/*
5187 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 * offline.
5189 */
5190void idle_task_exit(void)
5191{
5192 struct mm_struct *mm = current->active_mm;
5193
5194 BUG_ON(cpu_online(smp_processor_id()));
5195
Martin Schwidefskya53efe52012-10-26 17:17:44 +02005196 if (mm != &init_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 switch_mm(mm, &init_mm, current);
Martin Schwidefskya53efe52012-10-26 17:17:44 +02005198 finish_arch_post_lock_switch();
5199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 mmdrop(mm);
5201}
5202
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005203/*
Peter Zijlstra5d180232012-08-20 11:26:57 +02005204 * Since this CPU is going 'away' for a while, fold any nr_active delta
5205 * we might have. Assumes we're called after migrate_tasks() so that the
5206 * nr_active count is stable.
5207 *
5208 * Also see the comment "Global load-average calculations".
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005209 */
Peter Zijlstra5d180232012-08-20 11:26:57 +02005210static void calc_load_migrate(struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211{
Peter Zijlstra5d180232012-08-20 11:26:57 +02005212 long delta = calc_load_fold_active(rq);
5213 if (delta)
5214 atomic_long_add(delta, &calc_load_tasks);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005215}
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005216
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005217static void put_prev_task_fake(struct rq *rq, struct task_struct *prev)
5218{
5219}
5220
5221static const struct sched_class fake_sched_class = {
5222 .put_prev_task = put_prev_task_fake,
5223};
5224
5225static struct task_struct fake_task = {
5226 /*
5227 * Avoid pull_{rt,dl}_task()
5228 */
5229 .prio = MAX_PRIO + 1,
5230 .sched_class = &fake_sched_class,
5231};
5232
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005233/*
5234 * Migrate all tasks from the rq, sleeping tasks will be migrated by
5235 * try_to_wake_up()->select_task_rq().
5236 *
5237 * Called with rq->lock held even though we'er in stop_machine() and
5238 * there's no concurrency possible, we hold the required locks anyway
5239 * because of lock validation efforts.
5240 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005241static void migrate_tasks(struct rq *dead_rq)
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005242{
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005243 struct rq *rq = dead_rq;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005244 struct task_struct *next, *stop = rq->stop;
5245 int dest_cpu;
5246
5247 /*
5248 * Fudge the rq selection such that the below task selection loop
5249 * doesn't get stuck on the currently eligible stop task.
5250 *
5251 * We're currently inside stop_machine() and the rq is either stuck
5252 * in the stop_machine_cpu_stop() loop, or we're executing this code,
5253 * either way we should never end up calling schedule() until we're
5254 * done here.
5255 */
5256 rq->stop = NULL;
5257
Frederic Weisbecker77bd3972013-04-12 01:50:58 +02005258 /*
5259 * put_prev_task() and pick_next_task() sched
5260 * class method both need to have an up-to-date
5261 * value of rq->clock[_task]
5262 */
5263 update_rq_clock(rq);
5264
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005265 for (;;) {
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005266 /*
5267 * There's this thread running, bail when that's the only
5268 * remaining thread.
5269 */
5270 if (rq->nr_running == 1)
5271 break;
5272
Peter Zijlstracbce1a62015-06-11 14:46:54 +02005273 /*
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08005274 * pick_next_task assumes pinned rq->lock.
Peter Zijlstracbce1a62015-06-11 14:46:54 +02005275 */
5276 lockdep_pin_lock(&rq->lock);
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005277 next = pick_next_task(rq, &fake_task);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005278 BUG_ON(!next);
5279 next->sched_class->put_prev_task(rq, next);
5280
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08005281 /*
5282 * Rules for changing task_struct::cpus_allowed are holding
5283 * both pi_lock and rq->lock, such that holding either
5284 * stabilizes the mask.
5285 *
5286 * Drop rq->lock is not quite as disastrous as it usually is
5287 * because !cpu_active at this point, which means load-balance
5288 * will not interfere. Also, stop-machine.
5289 */
5290 lockdep_unpin_lock(&rq->lock);
5291 raw_spin_unlock(&rq->lock);
5292 raw_spin_lock(&next->pi_lock);
5293 raw_spin_lock(&rq->lock);
5294
5295 /*
5296 * Since we're inside stop-machine, _nothing_ should have
5297 * changed the task, WARN if weird stuff happened, because in
5298 * that case the above rq->lock drop is a fail too.
5299 */
5300 if (WARN_ON(task_rq(next) != rq || !task_on_rq_queued(next))) {
5301 raw_spin_unlock(&next->pi_lock);
5302 continue;
5303 }
5304
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005305 /* Find suitable destination for @next, with force if needed. */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005306 dest_cpu = select_fallback_rq(dead_rq->cpu, next);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005307
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005308 rq = __migrate_task(rq, next, dest_cpu);
5309 if (rq != dead_rq) {
5310 raw_spin_unlock(&rq->lock);
5311 rq = dead_rq;
5312 raw_spin_lock(&rq->lock);
5313 }
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08005314 raw_spin_unlock(&next->pi_lock);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005315 }
5316
5317 rq->stop = stop;
5318}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319#endif /* CONFIG_HOTPLUG_CPU */
5320
Nick Piggine692ab52007-07-26 13:40:43 +02005321#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5322
5323static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005324 {
5325 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005326 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005327 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005328 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005329};
5330
5331static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005332 {
5333 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005334 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005335 .child = sd_ctl_dir,
5336 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005337 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005338};
5339
5340static struct ctl_table *sd_alloc_ctl_entry(int n)
5341{
5342 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005343 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005344
Nick Piggine692ab52007-07-26 13:40:43 +02005345 return entry;
5346}
5347
Milton Miller6382bc92007-10-15 17:00:19 +02005348static void sd_free_ctl_entry(struct ctl_table **tablep)
5349{
Milton Millercd7900762007-10-17 16:55:11 +02005350 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005351
Milton Millercd7900762007-10-17 16:55:11 +02005352 /*
5353 * In the intermediate directories, both the child directory and
5354 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005355 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005356 * static strings and all have proc handlers.
5357 */
5358 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005359 if (entry->child)
5360 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005361 if (entry->proc_handler == NULL)
5362 kfree(entry->procname);
5363 }
Milton Miller6382bc92007-10-15 17:00:19 +02005364
5365 kfree(*tablep);
5366 *tablep = NULL;
5367}
5368
Namhyung Kim201c3732012-08-16 17:03:24 +09005369static int min_load_idx = 0;
libinfd9b86d2013-04-08 14:39:12 +08005370static int max_load_idx = CPU_LOAD_IDX_MAX-1;
Namhyung Kim201c3732012-08-16 17:03:24 +09005371
Nick Piggine692ab52007-07-26 13:40:43 +02005372static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005373set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005374 const char *procname, void *data, int maxlen,
Namhyung Kim201c3732012-08-16 17:03:24 +09005375 umode_t mode, proc_handler *proc_handler,
5376 bool load_idx)
Nick Piggine692ab52007-07-26 13:40:43 +02005377{
Nick Piggine692ab52007-07-26 13:40:43 +02005378 entry->procname = procname;
5379 entry->data = data;
5380 entry->maxlen = maxlen;
5381 entry->mode = mode;
5382 entry->proc_handler = proc_handler;
Namhyung Kim201c3732012-08-16 17:03:24 +09005383
5384 if (load_idx) {
5385 entry->extra1 = &min_load_idx;
5386 entry->extra2 = &max_load_idx;
5387 }
Nick Piggine692ab52007-07-26 13:40:43 +02005388}
5389
5390static struct ctl_table *
5391sd_alloc_ctl_domain_table(struct sched_domain *sd)
5392{
Alex Shi37e6bae2014-01-23 18:39:54 +08005393 struct ctl_table *table = sd_alloc_ctl_entry(14);
Nick Piggine692ab52007-07-26 13:40:43 +02005394
Milton Millerad1cdc12007-10-15 17:00:19 +02005395 if (table == NULL)
5396 return NULL;
5397
Alexey Dobriyane0361852007-08-09 11:16:46 +02005398 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Namhyung Kim201c3732012-08-16 17:03:24 +09005399 sizeof(long), 0644, proc_doulongvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005400 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Namhyung Kim201c3732012-08-16 17:03:24 +09005401 sizeof(long), 0644, proc_doulongvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005402 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005403 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005404 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005405 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005406 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005407 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005408 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005409 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005410 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005411 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005412 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Namhyung Kim201c3732012-08-16 17:03:24 +09005413 sizeof(int), 0644, proc_dointvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005414 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Namhyung Kim201c3732012-08-16 17:03:24 +09005415 sizeof(int), 0644, proc_dointvec_minmax, false);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005416 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005417 &sd->cache_nice_tries,
Namhyung Kim201c3732012-08-16 17:03:24 +09005418 sizeof(int), 0644, proc_dointvec_minmax, false);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005419 set_table_entry(&table[10], "flags", &sd->flags,
Namhyung Kim201c3732012-08-16 17:03:24 +09005420 sizeof(int), 0644, proc_dointvec_minmax, false);
Alex Shi37e6bae2014-01-23 18:39:54 +08005421 set_table_entry(&table[11], "max_newidle_lb_cost",
5422 &sd->max_newidle_lb_cost,
5423 sizeof(long), 0644, proc_doulongvec_minmax, false);
5424 set_table_entry(&table[12], "name", sd->name,
Namhyung Kim201c3732012-08-16 17:03:24 +09005425 CORENAME_MAX_SIZE, 0444, proc_dostring, false);
Alex Shi37e6bae2014-01-23 18:39:54 +08005426 /* &table[13] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005427
5428 return table;
5429}
5430
Joe Perchesbe7002e2013-06-12 11:55:36 -07005431static struct ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005432{
5433 struct ctl_table *entry, *table;
5434 struct sched_domain *sd;
5435 int domain_num = 0, i;
5436 char buf[32];
5437
5438 for_each_domain(cpu, sd)
5439 domain_num++;
5440 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005441 if (table == NULL)
5442 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005443
5444 i = 0;
5445 for_each_domain(cpu, sd) {
5446 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005447 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005448 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005449 entry->child = sd_alloc_ctl_domain_table(sd);
5450 entry++;
5451 i++;
5452 }
5453 return table;
5454}
5455
5456static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005457static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005458{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005459 int i, cpu_num = num_possible_cpus();
Nick Piggine692ab52007-07-26 13:40:43 +02005460 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5461 char buf[32];
5462
Milton Miller73785472007-10-24 18:23:48 +02005463 WARN_ON(sd_ctl_dir[0].child);
5464 sd_ctl_dir[0].child = entry;
5465
Milton Millerad1cdc12007-10-15 17:00:19 +02005466 if (entry == NULL)
5467 return;
5468
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005469 for_each_possible_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005470 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005471 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005472 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005473 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005474 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005475 }
Milton Miller73785472007-10-24 18:23:48 +02005476
5477 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005478 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5479}
Milton Miller6382bc92007-10-15 17:00:19 +02005480
Milton Miller73785472007-10-24 18:23:48 +02005481/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005482static void unregister_sched_domain_sysctl(void)
5483{
Markus Elfring781b0202015-07-04 09:06:32 +02005484 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005485 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005486 if (sd_ctl_dir[0].child)
5487 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005488}
Nick Piggine692ab52007-07-26 13:40:43 +02005489#else
Milton Miller6382bc92007-10-15 17:00:19 +02005490static void register_sched_domain_sysctl(void)
5491{
5492}
5493static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005494{
5495}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02005496#endif /* CONFIG_SCHED_DEBUG && CONFIG_SYSCTL */
Nick Piggine692ab52007-07-26 13:40:43 +02005497
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005498static void set_rq_online(struct rq *rq)
5499{
5500 if (!rq->online) {
5501 const struct sched_class *class;
5502
Rusty Russellc6c49272008-11-25 02:35:05 +10305503 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005504 rq->online = 1;
5505
5506 for_each_class(class) {
5507 if (class->rq_online)
5508 class->rq_online(rq);
5509 }
5510 }
5511}
5512
5513static void set_rq_offline(struct rq *rq)
5514{
5515 if (rq->online) {
5516 const struct sched_class *class;
5517
5518 for_each_class(class) {
5519 if (class->rq_offline)
5520 class->rq_offline(rq);
5521 }
5522
Rusty Russellc6c49272008-11-25 02:35:05 +10305523 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005524 rq->online = 0;
5525 }
5526}
5527
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528/*
5529 * migration_call - callback that gets triggered when a CPU is added.
5530 * Here we can start up the necessary migration thread for the new CPU.
5531 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005532static int
Ingo Molnar48f24c42006-07-03 00:25:40 -07005533migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005535 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 unsigned long flags;
Tejun Heo969c7922010-05-06 18:49:21 +02005537 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005539 switch (action & ~CPU_TASKS_FROZEN) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 case CPU_UP_PREPARE:
Thomas Gleixnera468d382009-07-17 14:15:46 +02005542 rq->calc_load_update = calc_load_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005544
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 case CPU_ONLINE:
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005546 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005547 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005548 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305549 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005550
5551 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005552 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005553 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005555
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556#ifdef CONFIG_HOTPLUG_CPU
Gregory Haskins08f503b2008-03-10 17:59:11 -04005557 case CPU_DYING:
Peter Zijlstra317f3942011-04-05 17:23:58 +02005558 sched_ttwu_pending();
Gregory Haskins57d885f2008-01-25 21:08:18 +01005559 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005560 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005561 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305562 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005563 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005564 }
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005565 migrate_tasks(rq);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005566 BUG_ON(rq->nr_running != 1); /* the migration thread */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005567 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra5d180232012-08-20 11:26:57 +02005568 break;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005569
Peter Zijlstra5d180232012-08-20 11:26:57 +02005570 case CPU_DEAD:
Peter Zijlstraf319da02012-08-20 11:26:57 +02005571 calc_load_migrate(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005572 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573#endif
5574 }
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005575
5576 update_max_interval();
5577
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 return NOTIFY_OK;
5579}
5580
Paul Mackerrasf38b0822009-06-02 21:05:16 +10005581/*
5582 * Register at high priority so that task migration (migrate_all_tasks)
5583 * happens before everything else. This has to be lower priority than
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584 * the notifier in the perf_event subsystem, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005586static struct notifier_block migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 .notifier_call = migration_call,
Tejun Heo50a323b2010-06-08 21:40:36 +02005588 .priority = CPU_PRI_MIGRATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589};
5590
Paul Gortmaker6a82b602015-04-27 18:47:50 -04005591static void set_cpu_rq_start_time(void)
Corey Minyarda803f022014-05-08 13:47:39 -05005592{
5593 int cpu = smp_processor_id();
5594 struct rq *rq = cpu_rq(cpu);
5595 rq->age_stamp = sched_clock_cpu(cpu);
5596}
5597
Paul Gortmaker0db06282013-06-19 14:53:51 -04005598static int sched_cpu_active(struct notifier_block *nfb,
Tejun Heo3a101d02010-06-08 21:40:36 +02005599 unsigned long action, void *hcpu)
5600{
Peter Zijlstra07f06cb2015-10-09 18:00:54 +02005601 int cpu = (long)hcpu;
5602
Tejun Heo3a101d02010-06-08 21:40:36 +02005603 switch (action & ~CPU_TASKS_FROZEN) {
Corey Minyarda803f022014-05-08 13:47:39 -05005604 case CPU_STARTING:
5605 set_cpu_rq_start_time();
5606 return NOTIFY_OK;
Peter Zijlstra07f06cb2015-10-09 18:00:54 +02005607
Jan H. Schönherrdd9d3842015-08-12 21:35:56 +02005608 case CPU_ONLINE:
5609 /*
5610 * At this point a starting CPU has marked itself as online via
5611 * set_cpu_online(). But it might not yet have marked itself
5612 * as active, which is essential from here on.
Jan H. Schönherrdd9d3842015-08-12 21:35:56 +02005613 */
Peter Zijlstra07f06cb2015-10-09 18:00:54 +02005614 set_cpu_active(cpu, true);
5615 stop_machine_unpark(cpu);
Tejun Heo3a101d02010-06-08 21:40:36 +02005616 return NOTIFY_OK;
Peter Zijlstra07f06cb2015-10-09 18:00:54 +02005617
5618 case CPU_DOWN_FAILED:
5619 set_cpu_active(cpu, true);
5620 return NOTIFY_OK;
5621
Tejun Heo3a101d02010-06-08 21:40:36 +02005622 default:
5623 return NOTIFY_DONE;
5624 }
5625}
5626
Paul Gortmaker0db06282013-06-19 14:53:51 -04005627static int sched_cpu_inactive(struct notifier_block *nfb,
Tejun Heo3a101d02010-06-08 21:40:36 +02005628 unsigned long action, void *hcpu)
5629{
5630 switch (action & ~CPU_TASKS_FROZEN) {
5631 case CPU_DOWN_PREPARE:
Juri Lelli3c18d442015-03-31 09:53:37 +01005632 set_cpu_active((long)hcpu, false);
Tejun Heo3a101d02010-06-08 21:40:36 +02005633 return NOTIFY_OK;
Juri Lelli3c18d442015-03-31 09:53:37 +01005634 default:
5635 return NOTIFY_DONE;
Tejun Heo3a101d02010-06-08 21:40:36 +02005636 }
5637}
5638
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005639static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640{
5641 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005642 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005643
Tejun Heo3a101d02010-06-08 21:40:36 +02005644 /* Initialize migration for the boot CPU */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005645 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5646 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5648 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005649
Tejun Heo3a101d02010-06-08 21:40:36 +02005650 /* Register cpu active notifiers */
5651 cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
5652 cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
5653
Thomas Gleixnera004cd42009-07-21 09:54:05 +02005654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005656early_initcall(migration_init);
Christoph Lameter476f3532007-05-06 14:48:58 -07005657
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005658static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
5659
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005660#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005661
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005662static __read_mostly int sched_debug_enabled;
Mike Travisf6630112009-11-17 18:22:15 -06005663
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005664static int __init sched_debug_setup(char *str)
Mike Travisf6630112009-11-17 18:22:15 -06005665{
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005666 sched_debug_enabled = 1;
Mike Travisf6630112009-11-17 18:22:15 -06005667
5668 return 0;
5669}
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005670early_param("sched_debug", sched_debug_setup);
5671
5672static inline bool sched_debug(void)
5673{
5674 return sched_debug_enabled;
5675}
Mike Travisf6630112009-11-17 18:22:15 -06005676
Mike Travis7c16ec52008-04-04 18:11:11 -07005677static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e22008-11-25 02:35:14 +10305678 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005679{
5680 struct sched_group *group = sd->groups;
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005681
Rusty Russell96f874e22008-11-25 02:35:14 +10305682 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005683
5684 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5685
5686 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005687 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005688 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005689 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5690 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005691 return -1;
5692 }
5693
Tejun Heo333470e2015-02-13 14:37:28 -08005694 printk(KERN_CONT "span %*pbl level %s\n",
5695 cpumask_pr_args(sched_domain_span(sd)), sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005696
Rusty Russell758b2cd2008-11-25 02:35:04 +10305697 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005698 printk(KERN_ERR "ERROR: domain->span does not contain "
5699 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005700 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10305701 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005702 printk(KERN_ERR "ERROR: domain->groups does not contain"
5703 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005704 }
5705
5706 printk(KERN_DEBUG "%*s groups:", level + 1, "");
5707 do {
5708 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005709 printk("\n");
5710 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005711 break;
5712 }
5713
Rusty Russell758b2cd2008-11-25 02:35:04 +10305714 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005715 printk(KERN_CONT "\n");
5716 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005717 break;
5718 }
5719
Peter Zijlstracb83b622012-04-17 15:49:36 +02005720 if (!(sd->flags & SD_OVERLAP) &&
5721 cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005722 printk(KERN_CONT "\n");
5723 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005724 break;
5725 }
5726
Rusty Russell758b2cd2008-11-25 02:35:04 +10305727 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005728
Tejun Heo333470e2015-02-13 14:37:28 -08005729 printk(KERN_CONT " %*pbl",
5730 cpumask_pr_args(sched_group_cpus(group)));
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005731 if (group->sgc->capacity != SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005732 printk(KERN_CONT " (cpu_capacity = %d)",
5733 group->sgc->capacity);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305734 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005735
5736 group = group->next;
5737 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005738 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005739
Rusty Russell758b2cd2008-11-25 02:35:04 +10305740 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005741 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005742
Rusty Russell758b2cd2008-11-25 02:35:04 +10305743 if (sd->parent &&
5744 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005745 printk(KERN_ERR "ERROR: parent span is not a superset "
5746 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005747 return 0;
5748}
5749
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750static void sched_domain_debug(struct sched_domain *sd, int cpu)
5751{
5752 int level = 0;
5753
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005754 if (!sched_debug_enabled)
Mike Travisf6630112009-11-17 18:22:15 -06005755 return;
5756
Nick Piggin41c7ce92005-06-25 14:57:24 -07005757 if (!sd) {
5758 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5759 return;
5760 }
5761
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5763
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005764 for (;;) {
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005765 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 level++;
5768 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005769 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005770 break;
5771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005773#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005774# define sched_domain_debug(sd, cpu) do { } while (0)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005775static inline bool sched_debug(void)
5776{
5777 return false;
5778}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005779#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005781static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005782{
Rusty Russell758b2cd2008-11-25 02:35:04 +10305783 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005784 return 1;
5785
5786 /* Following flags need at least 2 groups */
5787 if (sd->flags & (SD_LOAD_BALANCE |
5788 SD_BALANCE_NEWIDLE |
5789 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005790 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005791 SD_SHARE_CPUCAPACITY |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005792 SD_SHARE_PKG_RESOURCES |
5793 SD_SHARE_POWERDOMAIN)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005794 if (sd->groups != sd->groups->next)
5795 return 0;
5796 }
5797
5798 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005799 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005800 return 0;
5801
5802 return 1;
5803}
5804
Ingo Molnar48f24c42006-07-03 00:25:40 -07005805static int
5806sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005807{
5808 unsigned long cflags = sd->flags, pflags = parent->flags;
5809
5810 if (sd_degenerate(parent))
5811 return 1;
5812
Rusty Russell758b2cd2008-11-25 02:35:04 +10305813 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005814 return 0;
5815
Suresh Siddha245af2c2005-06-25 14:57:25 -07005816 /* Flags needing groups don't count if only 1 group in parent */
5817 if (parent->groups == parent->groups->next) {
5818 pflags &= ~(SD_LOAD_BALANCE |
5819 SD_BALANCE_NEWIDLE |
5820 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005821 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005822 SD_SHARE_CPUCAPACITY |
Peter Zijlstra10866e622013-08-19 16:57:04 +02005823 SD_SHARE_PKG_RESOURCES |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005824 SD_PREFER_SIBLING |
5825 SD_SHARE_POWERDOMAIN);
Ken Chen54364992008-12-07 18:47:37 -08005826 if (nr_node_ids == 1)
5827 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005828 }
5829 if (~cflags & pflags)
5830 return 0;
5831
5832 return 1;
5833}
5834
Peter Zijlstradce840a2011-04-07 14:09:50 +02005835static void free_rootdomain(struct rcu_head *rcu)
Rusty Russellc6c49272008-11-25 02:35:05 +10305836{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005837 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
Peter Zijlstra047106a2009-11-16 10:28:09 +01005838
Rusty Russell68e74562008-11-25 02:35:13 +10305839 cpupri_cleanup(&rd->cpupri);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005840 cpudl_cleanup(&rd->cpudl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005841 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305842 free_cpumask_var(rd->rto_mask);
5843 free_cpumask_var(rd->online);
5844 free_cpumask_var(rd->span);
5845 kfree(rd);
5846}
5847
Gregory Haskins57d885f2008-01-25 21:08:18 +01005848static void rq_attach_root(struct rq *rq, struct root_domain *rd)
5849{
Ingo Molnara0490fa2009-02-12 11:35:40 +01005850 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005851 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005852
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005853 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005854
5855 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01005856 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005857
Rusty Russellc6c49272008-11-25 02:35:05 +10305858 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005859 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005860
Rusty Russellc6c49272008-11-25 02:35:05 +10305861 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01005862
Ingo Molnara0490fa2009-02-12 11:35:40 +01005863 /*
Shigeru Yoshida05159732013-11-17 12:12:36 +09005864 * If we dont want to free the old_rd yet then
Ingo Molnara0490fa2009-02-12 11:35:40 +01005865 * set old_rd to NULL to skip the freeing later
5866 * in this function:
5867 */
5868 if (!atomic_dec_and_test(&old_rd->refcount))
5869 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005870 }
5871
5872 atomic_inc(&rd->refcount);
5873 rq->rd = rd;
5874
Rusty Russellc6c49272008-11-25 02:35:05 +10305875 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04005876 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005877 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005878
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005879 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01005880
5881 if (old_rd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02005882 call_rcu_sched(&old_rd->rcu, free_rootdomain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005883}
5884
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005885static int init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005886{
5887 memset(rd, 0, sizeof(*rd));
5888
Xunlei Pang8295c692015-12-02 19:52:59 +08005889 if (!zalloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08005890 goto out;
Xunlei Pang8295c692015-12-02 19:52:59 +08005891 if (!zalloc_cpumask_var(&rd->online, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305892 goto free_span;
Xunlei Pang8295c692015-12-02 19:52:59 +08005893 if (!zalloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305894 goto free_online;
Xunlei Pang8295c692015-12-02 19:52:59 +08005895 if (!zalloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
Juri Lelli1baca4c2013-11-07 14:43:38 +01005896 goto free_dlo_mask;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02005897
Dario Faggioli332ac172013-11-07 14:43:45 +01005898 init_dl_bw(&rd->dl_bw);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005899 if (cpudl_init(&rd->cpudl) != 0)
5900 goto free_dlo_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01005901
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005902 if (cpupri_init(&rd->cpupri) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10305903 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10305904 return 0;
5905
Rusty Russell68e74562008-11-25 02:35:13 +10305906free_rto_mask:
5907 free_cpumask_var(rd->rto_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005908free_dlo_mask:
5909 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305910free_online:
5911 free_cpumask_var(rd->online);
5912free_span:
5913 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08005914out:
Rusty Russellc6c49272008-11-25 02:35:05 +10305915 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005916}
5917
Peter Zijlstra029632f2011-10-25 10:00:11 +02005918/*
5919 * By default the system creates a single root-domain with all cpus as
5920 * members (mimicking the global state we have today).
5921 */
5922struct root_domain def_root_domain;
5923
Gregory Haskins57d885f2008-01-25 21:08:18 +01005924static void init_defrootdomain(void)
5925{
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005926 init_rootdomain(&def_root_domain);
Rusty Russellc6c49272008-11-25 02:35:05 +10305927
Gregory Haskins57d885f2008-01-25 21:08:18 +01005928 atomic_set(&def_root_domain.refcount, 1);
5929}
5930
Gregory Haskinsdc938522008-01-25 21:08:26 +01005931static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005932{
5933 struct root_domain *rd;
5934
5935 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
5936 if (!rd)
5937 return NULL;
5938
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005939 if (init_rootdomain(rd) != 0) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305940 kfree(rd);
5941 return NULL;
5942 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01005943
5944 return rd;
5945}
5946
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005947static void free_sched_groups(struct sched_group *sg, int free_sgc)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005948{
5949 struct sched_group *tmp, *first;
5950
5951 if (!sg)
5952 return;
5953
5954 first = sg;
5955 do {
5956 tmp = sg->next;
5957
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005958 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
5959 kfree(sg->sgc);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005960
5961 kfree(sg);
5962 sg = tmp;
5963 } while (sg != first);
5964}
5965
Peter Zijlstradce840a2011-04-07 14:09:50 +02005966static void free_sched_domain(struct rcu_head *rcu)
5967{
5968 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005969
5970 /*
5971 * If its an overlapping domain it has private groups, iterate and
5972 * nuke them all.
5973 */
5974 if (sd->flags & SD_OVERLAP) {
5975 free_sched_groups(sd->groups, 1);
5976 } else if (atomic_dec_and_test(&sd->groups->ref)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005977 kfree(sd->groups->sgc);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005978 kfree(sd->groups);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005979 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005980 kfree(sd);
5981}
5982
5983static void destroy_sched_domain(struct sched_domain *sd, int cpu)
5984{
5985 call_rcu(&sd->rcu, free_sched_domain);
5986}
5987
5988static void destroy_sched_domains(struct sched_domain *sd, int cpu)
5989{
5990 for (; sd; sd = sd->parent)
5991 destroy_sched_domain(sd, cpu);
5992}
5993
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994/*
Peter Zijlstra518cd622011-12-07 15:07:31 +01005995 * Keep a special pointer to the highest sched_domain that has
5996 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
5997 * allows us to avoid some pointer chasing select_idle_sibling().
5998 *
5999 * Also keep a unique ID per domain (we use the first cpu number in
6000 * the cpumask of the domain), this allows us to quickly tell if
Peter Zijlstra39be3502012-01-26 12:44:34 +01006001 * two cpus are in the same cache domain, see cpus_share_cache().
Peter Zijlstra518cd622011-12-07 15:07:31 +01006002 */
6003DEFINE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08006004DEFINE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01006005DEFINE_PER_CPU(int, sd_llc_id);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01006006DEFINE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306007DEFINE_PER_CPU(struct sched_domain *, sd_busy);
6008DEFINE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +01006009
6010static void update_top_cache_domain(int cpu)
6011{
6012 struct sched_domain *sd;
Mel Gorman5d4cf992013-12-17 09:21:25 +00006013 struct sched_domain *busy_sd = NULL;
Peter Zijlstra518cd622011-12-07 15:07:31 +01006014 int id = cpu;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08006015 int size = 1;
Peter Zijlstra518cd622011-12-07 15:07:31 +01006016
6017 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08006018 if (sd) {
Peter Zijlstra518cd622011-12-07 15:07:31 +01006019 id = cpumask_first(sched_domain_span(sd));
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08006020 size = cpumask_weight(sched_domain_span(sd));
Mel Gorman5d4cf992013-12-17 09:21:25 +00006021 busy_sd = sd->parent; /* sd_busy */
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08006022 }
Mel Gorman5d4cf992013-12-17 09:21:25 +00006023 rcu_assign_pointer(per_cpu(sd_busy, cpu), busy_sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01006024
6025 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08006026 per_cpu(sd_llc_size, cpu) = size;
Peter Zijlstra518cd622011-12-07 15:07:31 +01006027 per_cpu(sd_llc_id, cpu) = id;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01006028
6029 sd = lowest_flag_domain(cpu, SD_NUMA);
6030 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05306031
6032 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
6033 rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01006034}
6035
6036/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006037 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 * hold the hotplug lock.
6039 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006040static void
6041cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006043 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006044 struct sched_domain *tmp;
6045
6046 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08006047 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006048 struct sched_domain *parent = tmp->parent;
6049 if (!parent)
6050 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08006051
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006052 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006053 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006054 if (parent->parent)
6055 parent->parent->child = tmp;
Peter Zijlstra10866e622013-08-19 16:57:04 +02006056 /*
6057 * Transfer SD_PREFER_SIBLING down in case of a
6058 * degenerate parent; the spans match for this
6059 * so the property transfers.
6060 */
6061 if (parent->flags & SD_PREFER_SIBLING)
6062 tmp->flags |= SD_PREFER_SIBLING;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006063 destroy_sched_domain(parent, cpu);
Li Zefanf29c9b12008-11-06 09:45:16 +08006064 } else
6065 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006066 }
6067
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006068 if (sd && sd_degenerate(sd)) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02006069 tmp = sd;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006070 sd = sd->parent;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006071 destroy_sched_domain(tmp, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006072 if (sd)
6073 sd->child = NULL;
6074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
Peter Zijlstra4cb98832011-04-07 14:09:58 +02006076 sched_domain_debug(sd, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
Gregory Haskins57d885f2008-01-25 21:08:18 +01006078 rq_attach_root(rq, rd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006079 tmp = rq->sd;
Nick Piggin674311d2005-06-25 14:57:27 -07006080 rcu_assign_pointer(rq->sd, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006081 destroy_sched_domains(tmp, cpu);
Peter Zijlstra518cd622011-12-07 15:07:31 +01006082
6083 update_top_cache_domain(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084}
6085
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086/* Setup the mask of cpus configured for isolated domains */
6087static int __init isolated_cpu_setup(char *str)
6088{
Rusty Russellbdddd292009-12-02 14:09:16 +10306089 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russell968ea6d2008-12-13 21:55:51 +10306090 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 return 1;
6092}
6093
Ingo Molnar8927f492007-10-15 17:00:13 +02006094__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006096struct s_data {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006097 struct sched_domain ** __percpu sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006098 struct root_domain *rd;
6099};
6100
Andreas Herrmann2109b992009-08-18 12:53:00 +02006101enum s_alloc {
Andreas Herrmann2109b992009-08-18 12:53:00 +02006102 sa_rootdomain,
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006103 sa_sd,
Peter Zijlstradce840a2011-04-07 14:09:50 +02006104 sa_sd_storage,
Andreas Herrmann2109b992009-08-18 12:53:00 +02006105 sa_none,
6106};
6107
Peter Zijlstrac1174872012-05-31 14:47:33 +02006108/*
6109 * Build an iteration mask that can exclude certain CPUs from the upwards
6110 * domain traversal.
6111 *
6112 * Asymmetric node setups can result in situations where the domain tree is of
6113 * unequal depth, make sure to skip domains that already cover the entire
6114 * range.
6115 *
6116 * In that case build_sched_domains() will have terminated the iteration early
6117 * and our sibling sd spans will be empty. Domains should always include the
6118 * cpu they're built on, so check that.
6119 *
6120 */
6121static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
6122{
6123 const struct cpumask *span = sched_domain_span(sd);
6124 struct sd_data *sdd = sd->private;
6125 struct sched_domain *sibling;
6126 int i;
6127
6128 for_each_cpu(i, span) {
6129 sibling = *per_cpu_ptr(sdd->sd, i);
6130 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
6131 continue;
6132
6133 cpumask_set_cpu(i, sched_group_mask(sg));
6134 }
6135}
6136
6137/*
6138 * Return the canonical balance cpu for this group, this is the first cpu
6139 * of this group that's also in the iteration mask.
6140 */
6141int group_balance_cpu(struct sched_group *sg)
6142{
6143 return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
6144}
6145
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006146static int
6147build_overlap_sched_groups(struct sched_domain *sd, int cpu)
6148{
6149 struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
6150 const struct cpumask *span = sched_domain_span(sd);
6151 struct cpumask *covered = sched_domains_tmpmask;
6152 struct sd_data *sdd = sd->private;
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006153 struct sched_domain *sibling;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006154 int i;
6155
6156 cpumask_clear(covered);
6157
6158 for_each_cpu(i, span) {
6159 struct cpumask *sg_span;
6160
6161 if (cpumask_test_cpu(i, covered))
6162 continue;
6163
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006164 sibling = *per_cpu_ptr(sdd->sd, i);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006165
6166 /* See the comment near build_group_mask(). */
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006167 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
Peter Zijlstrac1174872012-05-31 14:47:33 +02006168 continue;
6169
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006170 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
Suresh Siddha4d78a222011-11-18 15:03:29 -08006171 GFP_KERNEL, cpu_to_node(cpu));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006172
6173 if (!sg)
6174 goto fail;
6175
6176 sg_span = sched_group_cpus(sg);
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006177 if (sibling->child)
6178 cpumask_copy(sg_span, sched_domain_span(sibling->child));
6179 else
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006180 cpumask_set_cpu(i, sg_span);
6181
6182 cpumask_or(covered, covered, sg_span);
6183
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006184 sg->sgc = *per_cpu_ptr(sdd->sgc, i);
6185 if (atomic_inc_return(&sg->sgc->ref) == 1)
Peter Zijlstrac1174872012-05-31 14:47:33 +02006186 build_group_mask(sd, sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006187
Peter Zijlstrac3decf02012-05-31 12:05:32 +02006188 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006189 * Initialize sgc->capacity such that even if we mess up the
Peter Zijlstrac3decf02012-05-31 12:05:32 +02006190 * domains and no possible iteration will get us here, we won't
6191 * die on a /0 trap.
6192 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006193 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006194
6195 /*
6196 * Make sure the first group of this domain contains the
6197 * canonical balance cpu. Otherwise the sched_domain iteration
6198 * breaks. See update_sg_lb_stats().
6199 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02006200 if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
Peter Zijlstrac1174872012-05-31 14:47:33 +02006201 group_balance_cpu(sg) == cpu)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006202 groups = sg;
6203
6204 if (!first)
6205 first = sg;
6206 if (last)
6207 last->next = sg;
6208 last = sg;
6209 last->next = first;
6210 }
6211 sd->groups = groups;
6212
6213 return 0;
6214
6215fail:
6216 free_sched_groups(first, 0);
6217
6218 return -ENOMEM;
6219}
6220
Peter Zijlstradce840a2011-04-07 14:09:50 +02006221static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006223 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
6224 struct sched_domain *child = sd->child;
6225
6226 if (child)
6227 cpu = cpumask_first(sched_domain_span(child));
6228
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006229 if (sg) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02006230 *sg = *per_cpu_ptr(sdd->sg, cpu);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006231 (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
6232 atomic_set(&(*sg)->sgc->ref, 1); /* for claim_allocations */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006233 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006234
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 return cpu;
6236}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
Ingo Molnar48f24c42006-07-03 00:25:40 -07006238/*
Peter Zijlstradce840a2011-04-07 14:09:50 +02006239 * build_sched_groups will build a circular linked list of the groups
6240 * covered by the given span, and will set each group's ->cpumask correctly,
Nicolas Pitreced549f2014-05-26 18:19:38 -04006241 * and ->cpu_capacity to 0.
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006242 *
6243 * Assumes the sched_domain tree is fully constructed
Ingo Molnar48f24c42006-07-03 00:25:40 -07006244 */
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006245static int
6246build_sched_groups(struct sched_domain *sd, int cpu)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006247{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006248 struct sched_group *first = NULL, *last = NULL;
6249 struct sd_data *sdd = sd->private;
6250 const struct cpumask *span = sched_domain_span(sd);
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006251 struct cpumask *covered;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006252 int i;
6253
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006254 get_group(cpu, sdd, &sd->groups);
6255 atomic_inc(&sd->groups->ref);
6256
Viresh Kumar09366292013-06-11 16:32:43 +05306257 if (cpu != cpumask_first(span))
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006258 return 0;
6259
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006260 lockdep_assert_held(&sched_domains_mutex);
6261 covered = sched_domains_tmpmask;
6262
Peter Zijlstradce840a2011-04-07 14:09:50 +02006263 cpumask_clear(covered);
6264
6265 for_each_cpu(i, span) {
6266 struct sched_group *sg;
Viresh Kumarcd08e922013-06-11 16:32:44 +05306267 int group, j;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006268
6269 if (cpumask_test_cpu(i, covered))
6270 continue;
6271
Viresh Kumarcd08e922013-06-11 16:32:44 +05306272 group = get_group(i, sdd, &sg);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006273 cpumask_setall(sched_group_mask(sg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02006274
6275 for_each_cpu(j, span) {
6276 if (get_group(j, sdd, NULL) != group)
6277 continue;
6278
6279 cpumask_set_cpu(j, covered);
6280 cpumask_set_cpu(j, sched_group_cpus(sg));
6281 }
6282
6283 if (!first)
6284 first = sg;
6285 if (last)
6286 last->next = sg;
6287 last = sg;
6288 }
6289 last->next = first;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006290
6291 return 0;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006292}
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006293
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294/*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006295 * Initialize sched groups cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006296 *
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006297 * cpu_capacity indicates the capacity of sched group, which is used while
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006298 * distributing the load between different sched groups in a sched domain.
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006299 * Typically cpu_capacity for all the groups in a sched domain will be same
6300 * unless there are asymmetries in the topology. If there are asymmetries,
6301 * group having more cpu_capacity will pickup more load compared to the
6302 * group having less cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006303 */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006304static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006305{
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006306 struct sched_group *sg = sd->groups;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006307
Viresh Kumar94c95ba2013-06-11 16:32:45 +05306308 WARN_ON(!sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006309
6310 do {
6311 sg->group_weight = cpumask_weight(sched_group_cpus(sg));
6312 sg = sg->next;
6313 } while (sg != sd->groups);
6314
Peter Zijlstrac1174872012-05-31 14:47:33 +02006315 if (cpu != group_balance_cpu(sg))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006316 return;
6317
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006318 update_group_capacity(sd, cpu);
6319 atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006320}
6321
6322/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006323 * Initializers for schedule domains
6324 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6325 */
6326
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006327static int default_relax_domain_level = -1;
Peter Zijlstra60495e72011-04-07 14:10:04 +02006328int sched_domain_level_max;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006329
6330static int __init setup_relax_domain_level(char *str)
6331{
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006332 if (kstrtoint(str, 0, &default_relax_domain_level))
6333 pr_warn("Unable to set relax_domain_level\n");
Li Zefan30e0e172008-05-13 10:27:17 +08006334
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006335 return 1;
6336}
6337__setup("relax_domain_level=", setup_relax_domain_level);
6338
6339static void set_domain_attribute(struct sched_domain *sd,
6340 struct sched_domain_attr *attr)
6341{
6342 int request;
6343
6344 if (!attr || attr->relax_domain_level < 0) {
6345 if (default_relax_domain_level < 0)
6346 return;
6347 else
6348 request = default_relax_domain_level;
6349 } else
6350 request = attr->relax_domain_level;
6351 if (request < sd->level) {
6352 /* turn off idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006353 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006354 } else {
6355 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006356 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006357 }
6358}
6359
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006360static void __sdt_free(const struct cpumask *cpu_map);
6361static int __sdt_alloc(const struct cpumask *cpu_map);
6362
Andreas Herrmann2109b992009-08-18 12:53:00 +02006363static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
6364 const struct cpumask *cpu_map)
6365{
6366 switch (what) {
Andreas Herrmann2109b992009-08-18 12:53:00 +02006367 case sa_rootdomain:
Peter Zijlstra822ff792011-04-07 14:09:51 +02006368 if (!atomic_read(&d->rd->refcount))
6369 free_rootdomain(&d->rd->rcu); /* fall through */
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006370 case sa_sd:
6371 free_percpu(d->sd); /* fall through */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006372 case sa_sd_storage:
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006373 __sdt_free(cpu_map); /* fall through */
Andreas Herrmann2109b992009-08-18 12:53:00 +02006374 case sa_none:
6375 break;
6376 }
6377}
6378
6379static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
6380 const struct cpumask *cpu_map)
6381{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006382 memset(d, 0, sizeof(*d));
6383
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006384 if (__sdt_alloc(cpu_map))
6385 return sa_sd_storage;
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006386 d->sd = alloc_percpu(struct sched_domain *);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006387 if (!d->sd)
6388 return sa_sd_storage;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006389 d->rd = alloc_rootdomain();
Peter Zijlstradce840a2011-04-07 14:09:50 +02006390 if (!d->rd)
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006391 return sa_sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006392 return sa_rootdomain;
6393}
6394
Peter Zijlstradce840a2011-04-07 14:09:50 +02006395/*
6396 * NULL the sd_data elements we've used to build the sched_domain and
6397 * sched_group structure so that the subsequent __free_domain_allocs()
6398 * will not free the data we're using.
6399 */
6400static void claim_allocations(int cpu, struct sched_domain *sd)
6401{
6402 struct sd_data *sdd = sd->private;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006403
6404 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
6405 *per_cpu_ptr(sdd->sd, cpu) = NULL;
6406
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006407 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
Peter Zijlstradce840a2011-04-07 14:09:50 +02006408 *per_cpu_ptr(sdd->sg, cpu) = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006409
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006410 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
6411 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006412}
6413
Peter Zijlstracb83b622012-04-17 15:49:36 +02006414#ifdef CONFIG_NUMA
Peter Zijlstracb83b622012-04-17 15:49:36 +02006415static int sched_domains_numa_levels;
Rik van Riele3fe70b2014-10-17 03:29:50 -04006416enum numa_topology_type sched_numa_topology_type;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006417static int *sched_domains_numa_distance;
Rik van Riel9942f792014-10-17 03:29:49 -04006418int sched_max_numa_distance;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006419static struct cpumask ***sched_domains_numa_masks;
6420static int sched_domains_curr_level;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006421#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006422
Vincent Guittot143e1e22014-04-11 11:44:37 +02006423/*
6424 * SD_flags allowed in topology descriptions.
6425 *
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006426 * SD_SHARE_CPUCAPACITY - describes SMT topologies
Vincent Guittot143e1e22014-04-11 11:44:37 +02006427 * SD_SHARE_PKG_RESOURCES - describes shared caches
6428 * SD_NUMA - describes NUMA topologies
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006429 * SD_SHARE_POWERDOMAIN - describes shared power domain
Vincent Guittot143e1e22014-04-11 11:44:37 +02006430 *
6431 * Odd one out:
6432 * SD_ASYM_PACKING - describes SMT quirks
6433 */
6434#define TOPOLOGY_SD_FLAGS \
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006435 (SD_SHARE_CPUCAPACITY | \
Vincent Guittot143e1e22014-04-11 11:44:37 +02006436 SD_SHARE_PKG_RESOURCES | \
6437 SD_NUMA | \
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006438 SD_ASYM_PACKING | \
6439 SD_SHARE_POWERDOMAIN)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006440
6441static struct sched_domain *
Vincent Guittot143e1e22014-04-11 11:44:37 +02006442sd_init(struct sched_domain_topology_level *tl, int cpu)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006443{
6444 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
Vincent Guittot143e1e22014-04-11 11:44:37 +02006445 int sd_weight, sd_flags = 0;
6446
6447#ifdef CONFIG_NUMA
6448 /*
6449 * Ugly hack to pass state to sd_numa_mask()...
6450 */
6451 sched_domains_curr_level = tl->numa_level;
6452#endif
6453
6454 sd_weight = cpumask_weight(tl->mask(cpu));
6455
6456 if (tl->sd_flags)
6457 sd_flags = (*tl->sd_flags)();
6458 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
6459 "wrong sd_flags in topology description\n"))
6460 sd_flags &= ~TOPOLOGY_SD_FLAGS;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006461
6462 *sd = (struct sched_domain){
6463 .min_interval = sd_weight,
6464 .max_interval = 2*sd_weight,
6465 .busy_factor = 32,
Peter Zijlstra870a0bb2012-05-11 00:26:27 +02006466 .imbalance_pct = 125,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006467
6468 .cache_nice_tries = 0,
6469 .busy_idx = 0,
6470 .idle_idx = 0,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006471 .newidle_idx = 0,
6472 .wake_idx = 0,
6473 .forkexec_idx = 0,
6474
6475 .flags = 1*SD_LOAD_BALANCE
6476 | 1*SD_BALANCE_NEWIDLE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006477 | 1*SD_BALANCE_EXEC
6478 | 1*SD_BALANCE_FORK
Peter Zijlstracb83b622012-04-17 15:49:36 +02006479 | 0*SD_BALANCE_WAKE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006480 | 1*SD_WAKE_AFFINE
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006481 | 0*SD_SHARE_CPUCAPACITY
Peter Zijlstracb83b622012-04-17 15:49:36 +02006482 | 0*SD_SHARE_PKG_RESOURCES
Vincent Guittot143e1e22014-04-11 11:44:37 +02006483 | 0*SD_SERIALIZE
Peter Zijlstracb83b622012-04-17 15:49:36 +02006484 | 0*SD_PREFER_SIBLING
Vincent Guittot143e1e22014-04-11 11:44:37 +02006485 | 0*SD_NUMA
6486 | sd_flags
Peter Zijlstracb83b622012-04-17 15:49:36 +02006487 ,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006488
Peter Zijlstracb83b622012-04-17 15:49:36 +02006489 .last_balance = jiffies,
6490 .balance_interval = sd_weight,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006491 .smt_gain = 0,
Jason Low2b4cfe62014-04-23 18:30:34 -07006492 .max_newidle_lb_cost = 0,
6493 .next_decay_max_lb_cost = jiffies,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006494#ifdef CONFIG_SCHED_DEBUG
6495 .name = tl->name,
6496#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006497 };
Peter Zijlstracb83b622012-04-17 15:49:36 +02006498
6499 /*
Vincent Guittot143e1e22014-04-11 11:44:37 +02006500 * Convert topological properties into behaviour.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006501 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006502
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006503 if (sd->flags & SD_SHARE_CPUCAPACITY) {
Vincent Guittotcaff37e2015-02-27 16:54:13 +01006504 sd->flags |= SD_PREFER_SIBLING;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006505 sd->imbalance_pct = 110;
6506 sd->smt_gain = 1178; /* ~15% */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006507
6508 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
6509 sd->imbalance_pct = 117;
6510 sd->cache_nice_tries = 1;
6511 sd->busy_idx = 2;
6512
6513#ifdef CONFIG_NUMA
6514 } else if (sd->flags & SD_NUMA) {
6515 sd->cache_nice_tries = 2;
6516 sd->busy_idx = 3;
6517 sd->idle_idx = 2;
6518
6519 sd->flags |= SD_SERIALIZE;
6520 if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
6521 sd->flags &= ~(SD_BALANCE_EXEC |
6522 SD_BALANCE_FORK |
6523 SD_WAKE_AFFINE);
6524 }
6525
6526#endif
6527 } else {
6528 sd->flags |= SD_PREFER_SIBLING;
6529 sd->cache_nice_tries = 1;
6530 sd->busy_idx = 2;
6531 sd->idle_idx = 1;
6532 }
6533
6534 sd->private = &tl->data;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006535
6536 return sd;
6537}
6538
Vincent Guittot143e1e22014-04-11 11:44:37 +02006539/*
6540 * Topology list, bottom-up.
6541 */
6542static struct sched_domain_topology_level default_topology[] = {
6543#ifdef CONFIG_SCHED_SMT
6544 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
6545#endif
6546#ifdef CONFIG_SCHED_MC
6547 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
6548#endif
Vincent Guittot143e1e22014-04-11 11:44:37 +02006549 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
6550 { NULL, },
6551};
6552
Juergen Grossc6e1e7b2015-09-22 12:48:59 +02006553static struct sched_domain_topology_level *sched_domain_topology =
6554 default_topology;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006555
6556#define for_each_sd_topology(tl) \
6557 for (tl = sched_domain_topology; tl->mask; tl++)
6558
6559void set_sched_topology(struct sched_domain_topology_level *tl)
6560{
6561 sched_domain_topology = tl;
6562}
6563
6564#ifdef CONFIG_NUMA
6565
Peter Zijlstracb83b622012-04-17 15:49:36 +02006566static const struct cpumask *sd_numa_mask(int cpu)
6567{
6568 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
6569}
6570
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006571static void sched_numa_warn(const char *str)
6572{
6573 static int done = false;
6574 int i,j;
6575
6576 if (done)
6577 return;
6578
6579 done = true;
6580
6581 printk(KERN_WARNING "ERROR: %s\n\n", str);
6582
6583 for (i = 0; i < nr_node_ids; i++) {
6584 printk(KERN_WARNING " ");
6585 for (j = 0; j < nr_node_ids; j++)
6586 printk(KERN_CONT "%02d ", node_distance(i,j));
6587 printk(KERN_CONT "\n");
6588 }
6589 printk(KERN_WARNING "\n");
6590}
6591
Rik van Riel9942f792014-10-17 03:29:49 -04006592bool find_numa_distance(int distance)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006593{
6594 int i;
6595
6596 if (distance == node_distance(0, 0))
6597 return true;
6598
6599 for (i = 0; i < sched_domains_numa_levels; i++) {
6600 if (sched_domains_numa_distance[i] == distance)
6601 return true;
6602 }
6603
6604 return false;
6605}
6606
Rik van Riele3fe70b2014-10-17 03:29:50 -04006607/*
6608 * A system can have three types of NUMA topology:
6609 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
6610 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
6611 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
6612 *
6613 * The difference between a glueless mesh topology and a backplane
6614 * topology lies in whether communication between not directly
6615 * connected nodes goes through intermediary nodes (where programs
6616 * could run), or through backplane controllers. This affects
6617 * placement of programs.
6618 *
6619 * The type of topology can be discerned with the following tests:
6620 * - If the maximum distance between any nodes is 1 hop, the system
6621 * is directly connected.
6622 * - If for two nodes A and B, located N > 1 hops away from each other,
6623 * there is an intermediary node C, which is < N hops away from both
6624 * nodes A and B, the system is a glueless mesh.
6625 */
6626static void init_numa_topology_type(void)
6627{
6628 int a, b, c, n;
6629
6630 n = sched_max_numa_distance;
6631
Aravind Gopalakrishnane2378822015-08-10 20:20:48 -05006632 if (sched_domains_numa_levels <= 1) {
Rik van Riele3fe70b2014-10-17 03:29:50 -04006633 sched_numa_topology_type = NUMA_DIRECT;
Aravind Gopalakrishnane2378822015-08-10 20:20:48 -05006634 return;
6635 }
Rik van Riele3fe70b2014-10-17 03:29:50 -04006636
6637 for_each_online_node(a) {
6638 for_each_online_node(b) {
6639 /* Find two nodes furthest removed from each other. */
6640 if (node_distance(a, b) < n)
6641 continue;
6642
6643 /* Is there an intermediary node between a and b? */
6644 for_each_online_node(c) {
6645 if (node_distance(a, c) < n &&
6646 node_distance(b, c) < n) {
6647 sched_numa_topology_type =
6648 NUMA_GLUELESS_MESH;
6649 return;
6650 }
6651 }
6652
6653 sched_numa_topology_type = NUMA_BACKPLANE;
6654 return;
6655 }
6656 }
6657}
6658
Peter Zijlstracb83b622012-04-17 15:49:36 +02006659static void sched_init_numa(void)
6660{
6661 int next_distance, curr_distance = node_distance(0, 0);
6662 struct sched_domain_topology_level *tl;
6663 int level = 0;
6664 int i, j, k;
6665
Peter Zijlstracb83b622012-04-17 15:49:36 +02006666 sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
6667 if (!sched_domains_numa_distance)
6668 return;
6669
6670 /*
6671 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
6672 * unique distances in the node_distance() table.
6673 *
6674 * Assumes node_distance(0,j) includes all distances in
6675 * node_distance(i,j) in order to avoid cubic time.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006676 */
6677 next_distance = curr_distance;
6678 for (i = 0; i < nr_node_ids; i++) {
6679 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006680 for (k = 0; k < nr_node_ids; k++) {
6681 int distance = node_distance(i, k);
6682
6683 if (distance > curr_distance &&
6684 (distance < next_distance ||
6685 next_distance == curr_distance))
6686 next_distance = distance;
6687
6688 /*
6689 * While not a strong assumption it would be nice to know
6690 * about cases where if node A is connected to B, B is not
6691 * equally connected to A.
6692 */
6693 if (sched_debug() && node_distance(k, i) != distance)
6694 sched_numa_warn("Node-distance not symmetric");
6695
6696 if (sched_debug() && i && !find_numa_distance(distance))
6697 sched_numa_warn("Node-0 not representative");
6698 }
6699 if (next_distance != curr_distance) {
6700 sched_domains_numa_distance[level++] = next_distance;
6701 sched_domains_numa_levels = level;
6702 curr_distance = next_distance;
6703 } else break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006704 }
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006705
6706 /*
6707 * In case of sched_debug() we verify the above assumption.
6708 */
6709 if (!sched_debug())
6710 break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006711 }
Andrey Ryabininc1235882014-11-07 17:53:40 +03006712
6713 if (!level)
6714 return;
6715
Peter Zijlstracb83b622012-04-17 15:49:36 +02006716 /*
6717 * 'level' contains the number of unique distances, excluding the
6718 * identity distance node_distance(i,i).
6719 *
Viresh Kumar28b4a522013-04-05 16:26:46 +05306720 * The sched_domains_numa_distance[] array includes the actual distance
Peter Zijlstracb83b622012-04-17 15:49:36 +02006721 * numbers.
6722 */
6723
Tang Chen5f7865f2012-09-25 21:12:30 +08006724 /*
6725 * Here, we should temporarily reset sched_domains_numa_levels to 0.
6726 * If it fails to allocate memory for array sched_domains_numa_masks[][],
6727 * the array will contain less then 'level' members. This could be
6728 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
6729 * in other functions.
6730 *
6731 * We reset it to 'level' at the end of this function.
6732 */
6733 sched_domains_numa_levels = 0;
6734
Peter Zijlstracb83b622012-04-17 15:49:36 +02006735 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
6736 if (!sched_domains_numa_masks)
6737 return;
6738
6739 /*
6740 * Now for each level, construct a mask per node which contains all
6741 * cpus of nodes that are that many hops away from us.
6742 */
6743 for (i = 0; i < level; i++) {
6744 sched_domains_numa_masks[i] =
6745 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
6746 if (!sched_domains_numa_masks[i])
6747 return;
6748
6749 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstra2ea45802012-05-25 09:26:43 +02006750 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
Peter Zijlstracb83b622012-04-17 15:49:36 +02006751 if (!mask)
6752 return;
6753
6754 sched_domains_numa_masks[i][j] = mask;
6755
6756 for (k = 0; k < nr_node_ids; k++) {
Peter Zijlstradd7d8632012-05-11 00:56:20 +02006757 if (node_distance(j, k) > sched_domains_numa_distance[i])
Peter Zijlstracb83b622012-04-17 15:49:36 +02006758 continue;
6759
6760 cpumask_or(mask, mask, cpumask_of_node(k));
6761 }
6762 }
6763 }
6764
Vincent Guittot143e1e22014-04-11 11:44:37 +02006765 /* Compute default topology size */
6766 for (i = 0; sched_domain_topology[i].mask; i++);
6767
Vincent Guittotc515db82014-05-13 11:11:01 +02006768 tl = kzalloc((i + level + 1) *
Peter Zijlstracb83b622012-04-17 15:49:36 +02006769 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
6770 if (!tl)
6771 return;
6772
6773 /*
6774 * Copy the default topology bits..
6775 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006776 for (i = 0; sched_domain_topology[i].mask; i++)
6777 tl[i] = sched_domain_topology[i];
Peter Zijlstracb83b622012-04-17 15:49:36 +02006778
6779 /*
6780 * .. and append 'j' levels of NUMA goodness.
6781 */
6782 for (j = 0; j < level; i++, j++) {
6783 tl[i] = (struct sched_domain_topology_level){
Peter Zijlstracb83b622012-04-17 15:49:36 +02006784 .mask = sd_numa_mask,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006785 .sd_flags = cpu_numa_flags,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006786 .flags = SDTL_OVERLAP,
6787 .numa_level = j,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006788 SD_INIT_NAME(NUMA)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006789 };
6790 }
6791
6792 sched_domain_topology = tl;
Tang Chen5f7865f2012-09-25 21:12:30 +08006793
6794 sched_domains_numa_levels = level;
Rik van Riel9942f792014-10-17 03:29:49 -04006795 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
Rik van Riele3fe70b2014-10-17 03:29:50 -04006796
6797 init_numa_topology_type();
Peter Zijlstracb83b622012-04-17 15:49:36 +02006798}
Tang Chen301a5cb2012-09-25 21:12:31 +08006799
6800static void sched_domains_numa_masks_set(int cpu)
6801{
6802 int i, j;
6803 int node = cpu_to_node(cpu);
6804
6805 for (i = 0; i < sched_domains_numa_levels; i++) {
6806 for (j = 0; j < nr_node_ids; j++) {
6807 if (node_distance(j, node) <= sched_domains_numa_distance[i])
6808 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
6809 }
6810 }
6811}
6812
6813static void sched_domains_numa_masks_clear(int cpu)
6814{
6815 int i, j;
6816 for (i = 0; i < sched_domains_numa_levels; i++) {
6817 for (j = 0; j < nr_node_ids; j++)
6818 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
6819 }
6820}
6821
6822/*
6823 * Update sched_domains_numa_masks[level][node] array when new cpus
6824 * are onlined.
6825 */
6826static int sched_domains_numa_masks_update(struct notifier_block *nfb,
6827 unsigned long action,
6828 void *hcpu)
6829{
6830 int cpu = (long)hcpu;
6831
6832 switch (action & ~CPU_TASKS_FROZEN) {
6833 case CPU_ONLINE:
6834 sched_domains_numa_masks_set(cpu);
6835 break;
6836
6837 case CPU_DEAD:
6838 sched_domains_numa_masks_clear(cpu);
6839 break;
6840
6841 default:
6842 return NOTIFY_DONE;
6843 }
6844
6845 return NOTIFY_OK;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006846}
6847#else
6848static inline void sched_init_numa(void)
6849{
6850}
Tang Chen301a5cb2012-09-25 21:12:31 +08006851
6852static int sched_domains_numa_masks_update(struct notifier_block *nfb,
6853 unsigned long action,
6854 void *hcpu)
6855{
6856 return 0;
6857}
Peter Zijlstracb83b622012-04-17 15:49:36 +02006858#endif /* CONFIG_NUMA */
6859
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006860static int __sdt_alloc(const struct cpumask *cpu_map)
6861{
6862 struct sched_domain_topology_level *tl;
6863 int j;
6864
Viresh Kumar27723a62013-06-10 16:27:20 +05306865 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006866 struct sd_data *sdd = &tl->data;
6867
6868 sdd->sd = alloc_percpu(struct sched_domain *);
6869 if (!sdd->sd)
6870 return -ENOMEM;
6871
6872 sdd->sg = alloc_percpu(struct sched_group *);
6873 if (!sdd->sg)
6874 return -ENOMEM;
6875
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006876 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
6877 if (!sdd->sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006878 return -ENOMEM;
6879
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006880 for_each_cpu(j, cpu_map) {
6881 struct sched_domain *sd;
6882 struct sched_group *sg;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006883 struct sched_group_capacity *sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006884
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02006885 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006886 GFP_KERNEL, cpu_to_node(j));
6887 if (!sd)
6888 return -ENOMEM;
6889
6890 *per_cpu_ptr(sdd->sd, j) = sd;
6891
6892 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
6893 GFP_KERNEL, cpu_to_node(j));
6894 if (!sg)
6895 return -ENOMEM;
6896
Igor Mammedov30b4e9e2012-05-09 12:38:28 +02006897 sg->next = sg;
6898
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006899 *per_cpu_ptr(sdd->sg, j) = sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006900
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006901 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006902 GFP_KERNEL, cpu_to_node(j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006903 if (!sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006904 return -ENOMEM;
6905
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006906 *per_cpu_ptr(sdd->sgc, j) = sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006907 }
6908 }
6909
6910 return 0;
6911}
6912
6913static void __sdt_free(const struct cpumask *cpu_map)
6914{
6915 struct sched_domain_topology_level *tl;
6916 int j;
6917
Viresh Kumar27723a62013-06-10 16:27:20 +05306918 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006919 struct sd_data *sdd = &tl->data;
6920
6921 for_each_cpu(j, cpu_map) {
he, bofb2cf2c2012-04-25 19:59:21 +08006922 struct sched_domain *sd;
6923
6924 if (sdd->sd) {
6925 sd = *per_cpu_ptr(sdd->sd, j);
6926 if (sd && (sd->flags & SD_OVERLAP))
6927 free_sched_groups(sd->groups, 0);
6928 kfree(*per_cpu_ptr(sdd->sd, j));
6929 }
6930
6931 if (sdd->sg)
6932 kfree(*per_cpu_ptr(sdd->sg, j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006933 if (sdd->sgc)
6934 kfree(*per_cpu_ptr(sdd->sgc, j));
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006935 }
6936 free_percpu(sdd->sd);
he, bofb2cf2c2012-04-25 19:59:21 +08006937 sdd->sd = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006938 free_percpu(sdd->sg);
he, bofb2cf2c2012-04-25 19:59:21 +08006939 sdd->sg = NULL;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006940 free_percpu(sdd->sgc);
6941 sdd->sgc = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006942 }
6943}
6944
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006945struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306946 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6947 struct sched_domain *child, int cpu)
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006948{
Vincent Guittot143e1e22014-04-11 11:44:37 +02006949 struct sched_domain *sd = sd_init(tl, cpu);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006950 if (!sd)
Peter Zijlstrad069b912011-04-07 14:10:02 +02006951 return child;
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006952
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006953 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
Peter Zijlstra60495e72011-04-07 14:10:04 +02006954 if (child) {
6955 sd->level = child->level + 1;
6956 sched_domain_level_max = max(sched_domain_level_max, sd->level);
Peter Zijlstrad069b912011-04-07 14:10:02 +02006957 child->parent = sd;
Viresh Kumarc75e0122013-06-10 16:27:19 +05306958 sd->child = child;
Peter Zijlstra6ae72df2014-07-22 11:47:40 +02006959
6960 if (!cpumask_subset(sched_domain_span(child),
6961 sched_domain_span(sd))) {
6962 pr_err("BUG: arch topology borken\n");
6963#ifdef CONFIG_SCHED_DEBUG
6964 pr_err(" the %s domain not a subset of the %s domain\n",
6965 child->name, sd->name);
6966#endif
6967 /* Fixup, ensure @sd has at least @child cpus. */
6968 cpumask_or(sched_domain_span(sd),
6969 sched_domain_span(sd),
6970 sched_domain_span(child));
6971 }
6972
Peter Zijlstra60495e72011-04-07 14:10:04 +02006973 }
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006974 set_domain_attribute(sd, attr);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006975
6976 return sd;
6977}
6978
Mike Travis7c16ec52008-04-04 18:11:11 -07006979/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006980 * Build sched domains for a given set of cpus and attach the sched domains
6981 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006983static int build_sched_domains(const struct cpumask *cpu_map,
6984 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985{
Viresh Kumar1c632162013-06-10 16:27:18 +05306986 enum s_alloc alloc_state;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006987 struct sched_domain *sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006988 struct s_data d;
Peter Zijlstra822ff792011-04-07 14:09:51 +02006989 int i, ret = -ENOMEM;
Rusty Russell3404c8d2008-11-25 02:35:03 +10306990
Andreas Herrmann2109b992009-08-18 12:53:00 +02006991 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
6992 if (alloc_state != sa_rootdomain)
6993 goto error;
Mike Travis7c16ec52008-04-04 18:11:11 -07006994
Peter Zijlstradce840a2011-04-07 14:09:50 +02006995 /* Set up domains for cpus specified by the cpu_map. */
Rusty Russellabcd0832008-11-25 02:35:02 +10306996 for_each_cpu(i, cpu_map) {
Peter Zijlstraeb7a74e62011-04-07 14:10:00 +02006997 struct sched_domain_topology_level *tl;
6998
Peter Zijlstra3bd65a82011-04-07 14:09:54 +02006999 sd = NULL;
Viresh Kumar27723a62013-06-10 16:27:20 +05307000 for_each_sd_topology(tl) {
Viresh Kumar4a850cb2013-06-04 16:12:43 +05307001 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
Viresh Kumar22da9562013-06-04 15:41:15 +05307002 if (tl == sched_domain_topology)
7003 *per_cpu_ptr(d.sd, i) = sd;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007004 if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
7005 sd->flags |= SD_OVERLAP;
Peter Zijlstrad1102352011-07-20 18:42:57 +02007006 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
7007 break;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007008 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02007009 }
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007010
Peter Zijlstradce840a2011-04-07 14:09:50 +02007011 /* Build the groups for the domains */
7012 for_each_cpu(i, cpu_map) {
7013 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
7014 sd->span_weight = cpumask_weight(sched_domain_span(sd));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02007015 if (sd->flags & SD_OVERLAP) {
7016 if (build_overlap_sched_groups(sd, i))
7017 goto error;
7018 } else {
7019 if (build_sched_groups(sd, i))
7020 goto error;
7021 }
Peter Zijlstra1cf519022011-04-07 14:09:47 +02007022 }
Peter Zijlstraa06dadb2011-04-07 14:09:44 +02007023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024
Nicolas Pitreced549f2014-05-26 18:19:38 -04007025 /* Calculate CPU capacity for physical packages and nodes */
Peter Zijlstraa9c9a9b2011-04-07 14:09:49 +02007026 for (i = nr_cpumask_bits-1; i >= 0; i--) {
7027 if (!cpumask_test_cpu(i, cpu_map))
7028 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029
Peter Zijlstradce840a2011-04-07 14:09:50 +02007030 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
7031 claim_allocations(i, sd);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04007032 init_sched_groups_capacity(i, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007033 }
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007034 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007035
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 /* Attach the domains */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007037 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10307038 for_each_cpu(i, cpu_map) {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02007039 sd = *per_cpu_ptr(d.sd, i);
Andreas Herrmann49a02c52009-08-18 12:51:52 +02007040 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02007042 rcu_read_unlock();
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007043
Peter Zijlstra822ff792011-04-07 14:09:51 +02007044 ret = 0;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007045error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02007046 __free_domain_allocs(&d, alloc_state, cpu_map);
Peter Zijlstra822ff792011-04-07 14:09:51 +02007047 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048}
Paul Jackson029190c2007-10-18 23:40:20 -07007049
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307050static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07007051static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007052static struct sched_domain_attr *dattr_cur;
7053 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007054
7055/*
7056 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10307057 * cpumask) fails, then fallback to a single sched domain,
7058 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07007059 */
Rusty Russell42128232008-11-25 02:35:12 +10307060static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07007061
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007062/*
7063 * arch_update_cpu_topology lets virtualized architectures update the
7064 * cpu core maps. It is supposed to return 1 if the topology changed
7065 * or 0 if it stayed the same.
7066 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07007067int __weak arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01007068{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007069 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01007070}
7071
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307072cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
7073{
7074 int i;
7075 cpumask_var_t *doms;
7076
7077 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
7078 if (!doms)
7079 return NULL;
7080 for (i = 0; i < ndoms; i++) {
7081 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
7082 free_sched_domains(doms, i);
7083 return NULL;
7084 }
7085 }
7086 return doms;
7087}
7088
7089void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
7090{
7091 unsigned int i;
7092 for (i = 0; i < ndoms; i++)
7093 free_cpumask_var(doms[i]);
7094 kfree(doms);
7095}
7096
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007097/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007098 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007099 * For now this just excludes isolated cpus, but could be used to
7100 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007101 */
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007102static int init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007103{
Milton Miller73785472007-10-24 18:23:48 +02007104 int err;
7105
Heiko Carstens22e52b02008-03-12 18:31:59 +01007106 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007107 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307108 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07007109 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307110 doms_cur = &fallback_doms;
7111 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007112 err = build_sched_domains(doms_cur[0], NULL);
Milton Miller6382bc92007-10-15 17:00:19 +02007113 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007114
7115 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007116}
7117
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007118/*
7119 * Detach sched domains from a group of cpus specified in cpu_map
7120 * These cpus will now be attached to the NULL domain
7121 */
Rusty Russell96f874e22008-11-25 02:35:14 +10307122static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007123{
7124 int i;
7125
Peter Zijlstradce840a2011-04-07 14:09:50 +02007126 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10307127 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007128 cpu_attach_domain(NULL, &def_root_domain, i);
Peter Zijlstradce840a2011-04-07 14:09:50 +02007129 rcu_read_unlock();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007130}
7131
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007132/* handle null as "default" */
7133static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7134 struct sched_domain_attr *new, int idx_new)
7135{
7136 struct sched_domain_attr tmp;
7137
7138 /* fast path */
7139 if (!new && !cur)
7140 return 1;
7141
7142 tmp = SD_ATTR_INIT;
7143 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7144 new ? (new + idx_new) : &tmp,
7145 sizeof(struct sched_domain_attr));
7146}
7147
Paul Jackson029190c2007-10-18 23:40:20 -07007148/*
7149 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007150 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007151 * doms_new[] to the current sched domain partitioning, doms_cur[].
7152 * It destroys each deleted domain and builds each new domain.
7153 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307154 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007155 * The masks don't intersect (don't overlap.) We should setup one
7156 * sched domain for each mask. CPUs not in any of the cpumasks will
7157 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007158 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7159 * it as it is.
7160 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307161 * The passed in 'doms_new' should be allocated using
7162 * alloc_sched_domains. This routine takes ownership of it and will
7163 * free_sched_domains it when done with it. If the caller failed the
7164 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
7165 * and partition_sched_domains() will fallback to the single partition
7166 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007167 *
Rusty Russell96f874e22008-11-25 02:35:14 +10307168 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08007169 * ndoms_new == 0 is a special case for destroying existing domains,
7170 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007171 *
Paul Jackson029190c2007-10-18 23:40:20 -07007172 * Call with hotplug lock held
7173 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307174void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007175 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007176{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007177 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007178 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07007179
Heiko Carstens712555e2008-04-28 11:33:07 +02007180 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007181
Milton Miller73785472007-10-24 18:23:48 +02007182 /* always unregister in case we don't destroy any domains */
7183 unregister_sched_domain_sysctl();
7184
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007185 /* Let architecture update cpu core mappings. */
7186 new_topology = arch_update_cpu_topology();
7187
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007188 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007189
7190 /* Destroy deleted domains */
7191 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007192 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307193 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007194 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007195 goto match1;
7196 }
7197 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307198 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07007199match1:
7200 ;
7201 }
7202
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007203 n = ndoms_cur;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007204 if (doms_new == NULL) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007205 n = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307206 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007207 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08007208 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007209 }
7210
Paul Jackson029190c2007-10-18 23:40:20 -07007211 /* Build new domains */
7212 for (i = 0; i < ndoms_new; i++) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007213 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307214 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007215 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007216 goto match2;
7217 }
7218 /* no match - add a new doms_new */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007219 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007220match2:
7221 ;
7222 }
7223
7224 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307225 if (doms_cur != &fallback_doms)
7226 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007227 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007228 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007229 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007230 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007231
7232 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007233
Heiko Carstens712555e2008-04-28 11:33:07 +02007234 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007235}
7236
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307237static int num_cpus_frozen; /* used to mark begin/end of suspend/resume */
7238
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239/*
Tejun Heo3a101d02010-06-08 21:40:36 +02007240 * Update cpusets according to cpu_active mask. If cpusets are
7241 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
7242 * around partition_sched_domains().
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307243 *
7244 * If we come here as part of a suspend/resume, don't touch cpusets because we
7245 * want to restore it back to its original state upon resume anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 */
Tejun Heo0b2e9182010-06-21 23:53:31 +02007247static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
7248 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249{
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307250 switch (action) {
7251 case CPU_ONLINE_FROZEN:
7252 case CPU_DOWN_FAILED_FROZEN:
7253
7254 /*
7255 * num_cpus_frozen tracks how many CPUs are involved in suspend
7256 * resume sequence. As long as this is not the last online
7257 * operation in the resume sequence, just build a single sched
7258 * domain, ignoring cpusets.
7259 */
7260 num_cpus_frozen--;
7261 if (likely(num_cpus_frozen)) {
7262 partition_sched_domains(1, NULL, NULL);
7263 break;
7264 }
7265
7266 /*
7267 * This is the last CPU online operation. So fall through and
7268 * restore the original sched domains by considering the
7269 * cpuset configurations.
7270 */
7271
Max Krasnyanskye761b772008-07-15 04:43:49 -07007272 case CPU_ONLINE:
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307273 cpuset_update_active_cpus(true);
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307274 break;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007275 default:
7276 return NOTIFY_DONE;
7277 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307278 return NOTIFY_OK;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007279}
Tejun Heo3a101d02010-06-08 21:40:36 +02007280
Tejun Heo0b2e9182010-06-21 23:53:31 +02007281static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
7282 void *hcpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02007283{
Juri Lelli3c18d442015-03-31 09:53:37 +01007284 unsigned long flags;
7285 long cpu = (long)hcpu;
7286 struct dl_bw *dl_b;
Omar Sandoval533445c2015-05-04 03:09:36 -07007287 bool overflow;
7288 int cpus;
Juri Lelli3c18d442015-03-31 09:53:37 +01007289
Omar Sandoval533445c2015-05-04 03:09:36 -07007290 switch (action) {
Tejun Heo3a101d02010-06-08 21:40:36 +02007291 case CPU_DOWN_PREPARE:
Omar Sandoval533445c2015-05-04 03:09:36 -07007292 rcu_read_lock_sched();
7293 dl_b = dl_bw_of(cpu);
Juri Lelli3c18d442015-03-31 09:53:37 +01007294
Omar Sandoval533445c2015-05-04 03:09:36 -07007295 raw_spin_lock_irqsave(&dl_b->lock, flags);
7296 cpus = dl_bw_cpus(cpu);
7297 overflow = __dl_overflow(dl_b, cpus, 0, 0);
7298 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli3c18d442015-03-31 09:53:37 +01007299
Omar Sandoval533445c2015-05-04 03:09:36 -07007300 rcu_read_unlock_sched();
Juri Lelli3c18d442015-03-31 09:53:37 +01007301
Omar Sandoval533445c2015-05-04 03:09:36 -07007302 if (overflow)
7303 return notifier_from_errno(-EBUSY);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307304 cpuset_update_active_cpus(false);
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307305 break;
7306 case CPU_DOWN_PREPARE_FROZEN:
7307 num_cpus_frozen++;
7308 partition_sched_domains(1, NULL, NULL);
7309 break;
Tejun Heo3a101d02010-06-08 21:40:36 +02007310 default:
7311 return NOTIFY_DONE;
7312 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307313 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02007314}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007315
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316void __init sched_init_smp(void)
7317{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307318 cpumask_var_t non_isolated_cpus;
7319
7320 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007321 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007322
Peter Zijlstracb83b622012-04-17 15:49:36 +02007323 sched_init_numa();
7324
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02007325 /*
7326 * There's no userspace yet to cause hotplug operations; hence all the
7327 * cpu masks are stable and all blatant races in the below code cannot
7328 * happen.
7329 */
Heiko Carstens712555e2008-04-28 11:33:07 +02007330 mutex_lock(&sched_domains_mutex);
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007331 init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307332 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7333 if (cpumask_empty(non_isolated_cpus))
7334 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007335 mutex_unlock(&sched_domains_mutex);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007336
Tang Chen301a5cb2012-09-25 21:12:31 +08007337 hotcpu_notifier(sched_domains_numa_masks_update, CPU_PRI_SCHED_ACTIVE);
Tejun Heo3a101d02010-06-08 21:40:36 +02007338 hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
7339 hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007340
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007341 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007342
7343 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307344 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007345 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007346 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307347 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307348
Rusty Russell0e3900e2008-11-25 02:35:13 +10307349 init_sched_rt_class();
Juri Lelli1baca4c2013-11-07 14:43:38 +01007350 init_sched_dl_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351}
7352#else
7353void __init sched_init_smp(void)
7354{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007355 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356}
7357#endif /* CONFIG_SMP */
7358
7359int in_sched_functions(unsigned long addr)
7360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 return in_lock_functions(addr) ||
7362 (addr >= (unsigned long)__sched_text_start
7363 && addr < (unsigned long)__sched_text_end);
7364}
7365
Peter Zijlstra029632f2011-10-25 10:00:11 +02007366#ifdef CONFIG_CGROUP_SCHED
Li Zefan27b4b932013-03-05 16:07:52 +08007367/*
7368 * Default task group.
7369 * Every task in system belongs to this group at bootup.
7370 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007371struct task_group root_task_group;
Mike Galbraith35cf4e52012-08-07 05:00:13 +02007372LIST_HEAD(task_groups);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007373#endif
7374
Joonsoo Kime6252c32013-04-23 17:27:41 +09007375DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007376
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377void __init sched_init(void)
7378{
Ingo Molnardd41f592007-07-09 18:51:59 +02007379 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007380 unsigned long alloc_size = 0, ptr;
7381
7382#ifdef CONFIG_FAIR_GROUP_SCHED
7383 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7384#endif
7385#ifdef CONFIG_RT_GROUP_SCHED
7386 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7387#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007388 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007389 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007390
7391#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007392 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007393 ptr += nr_cpu_ids * sizeof(void **);
7394
Yong Zhang07e06b02011-01-07 15:17:36 +08007395 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007396 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007397
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007398#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007399#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007400 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007401 ptr += nr_cpu_ids * sizeof(void **);
7402
Yong Zhang07e06b02011-01-07 15:17:36 +08007403 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007404 ptr += nr_cpu_ids * sizeof(void **);
7405
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007406#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007407 }
Alex Thorltonb74e6272014-12-18 12:44:30 -06007408#ifdef CONFIG_CPUMASK_OFFSTACK
7409 for_each_possible_cpu(i) {
7410 per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
7411 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
7412 }
7413#endif /* CONFIG_CPUMASK_OFFSTACK */
Ingo Molnardd41f592007-07-09 18:51:59 +02007414
Dario Faggioli332ac172013-11-07 14:43:45 +01007415 init_rt_bandwidth(&def_rt_bandwidth,
7416 global_rt_period(), global_rt_runtime());
7417 init_dl_bandwidth(&def_dl_bandwidth,
Peter Zijlstra17248132013-12-17 12:44:49 +01007418 global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +01007419
Gregory Haskins57d885f2008-01-25 21:08:18 +01007420#ifdef CONFIG_SMP
7421 init_defrootdomain();
7422#endif
7423
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007424#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007425 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007426 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007427#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007428
Dhaval Giani7c941432010-01-20 13:26:18 +01007429#ifdef CONFIG_CGROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007430 list_add(&root_task_group.list, &task_groups);
7431 INIT_LIST_HEAD(&root_task_group.children);
Glauber Costaf4d6f6c2011-11-01 19:19:07 -02007432 INIT_LIST_HEAD(&root_task_group.siblings);
Mike Galbraith5091faa2010-11-30 14:18:03 +01007433 autogroup_init(&init_task);
Glauber Costa54c707e2011-11-28 14:45:19 -02007434
Dhaval Giani7c941432010-01-20 13:26:18 +01007435#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007436
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007437 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007438 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439
7440 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007441 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07007442 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007443 rq->calc_load_active = 0;
7444 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007445 init_cfs_rq(&rq->cfs);
Abel Vesa07c54f72015-03-03 13:50:27 +02007446 init_rt_rq(&rq->rt);
7447 init_dl_rq(&rq->dl);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007448#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007449 root_task_group.shares = ROOT_TASK_GROUP_LOAD;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007450 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007451 /*
Yong Zhang07e06b02011-01-07 15:17:36 +08007452 * How much cpu bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02007453 *
7454 * In case of task-groups formed thr' the cgroup filesystem, it
7455 * gets 100% of the cpu resources in the system. This overall
7456 * system cpu resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08007457 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02007458 * based on each entity's (task or task-group's) weight
7459 * (se->load.weight).
7460 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007461 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02007462 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7463 * then A0's share of the cpu resource is:
7464 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007465 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007466 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007467 * We achieve this by letting root_task_group's tasks sit
7468 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02007469 */
Paul Turnerab84d312011-07-21 09:43:28 -07007470 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Yong Zhang07e06b02011-01-07 15:17:36 +08007471 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007472#endif /* CONFIG_FAIR_GROUP_SCHED */
7473
7474 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007475#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007476 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
Ingo Molnardd41f592007-07-09 18:51:59 +02007479 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7480 rq->cpu_load[j] = 0;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07007481
7482 rq->last_load_update_tick = jiffies;
7483
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007485 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007486 rq->rd = NULL;
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007487 rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02007488 rq->balance_callback = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007490 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007492 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04007493 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007494 rq->idle_stamp = 0;
7495 rq->avg_idle = 2*sysctl_sched_migration_cost;
Jason Low9bd721c2013-09-13 11:26:52 -07007496 rq->max_idle_balance_cost = sysctl_sched_migration_cost;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007497
7498 INIT_LIST_HEAD(&rq->cfs_tasks);
7499
Gregory Haskinsdc938522008-01-25 21:08:26 +01007500 rq_attach_root(rq, &def_root_domain);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007501#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08007502 rq->nohz_flags = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007503#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02007504#ifdef CONFIG_NO_HZ_FULL
7505 rq->last_sched_tick = 0;
7506#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007508 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 }
7511
Peter Williams2dd73a42006-06-27 02:54:34 -07007512 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007513
Avi Kivitye107be32007-07-26 13:40:43 +02007514#ifdef CONFIG_PREEMPT_NOTIFIERS
7515 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7516#endif
7517
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 /*
7519 * The boot idle thread does lazy MMU switching as well:
7520 */
7521 atomic_inc(&init_mm.mm_count);
7522 enter_lazy_tlb(&init_mm, current);
7523
7524 /*
Yao Dongdong1b537c72014-12-29 14:41:43 +08007525 * During early bootup we pretend to be a normal task:
7526 */
7527 current->sched_class = &fair_sched_class;
7528
7529 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 * Make us the idle thread. Technically, schedule() should not be
7531 * called from this thread, however somewhere below it might be,
7532 * but because we are the idle thread, we just pick up running again
7533 * when this runqueue becomes "idle".
7534 */
7535 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007536
7537 calc_load_update = jiffies + LOAD_FREQ;
7538
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307539#ifdef CONFIG_SMP
Peter Zijlstra4cb98832011-04-07 14:09:58 +02007540 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
Rusty Russellbdddd292009-12-02 14:09:16 +10307541 /* May be allocated at isolcpus cmdline parse time */
7542 if (cpu_isolated_map == NULL)
7543 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Thomas Gleixner29d5e042012-04-20 13:05:45 +00007544 idle_thread_set_boot_cpu();
Corey Minyarda803f022014-05-08 13:47:39 -05007545 set_cpu_rq_start_time();
Peter Zijlstra029632f2011-10-25 10:00:11 +02007546#endif
7547 init_sched_fair_class();
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307548
Ingo Molnar6892b752008-02-13 14:02:36 +01007549 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550}
7551
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02007552#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007553static inline int preempt_count_equals(int preempt_offset)
7554{
Peter Zijlstrada7142e2015-09-28 18:11:45 +02007555 int nested = preempt_count() + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007556
Arnd Bergmann4ba82162011-01-25 22:52:22 +01007557 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007558}
7559
Simon Kagstromd8948372009-12-23 11:08:18 +01007560void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561{
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007562 /*
7563 * Blocking primitives will set (and therefore destroy) current->state,
7564 * since we will exit with TASK_RUNNING make sure we enter with it,
7565 * otherwise we will destroy state.
7566 */
Linus Torvalds00845eb2015-02-01 12:23:32 -08007567 WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007568 "do not call blocking ops when !TASK_RUNNING; "
7569 "state=%lx set at [<%p>] %pS\n",
7570 current->state,
7571 (void *)current->task_state_change,
Linus Torvalds00845eb2015-02-01 12:23:32 -08007572 (void *)current->task_state_change);
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007573
Peter Zijlstra34274452014-09-24 10:18:56 +02007574 ___might_sleep(file, line, preempt_offset);
7575}
7576EXPORT_SYMBOL(__might_sleep);
7577
7578void ___might_sleep(const char *file, int line, int preempt_offset)
7579{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 static unsigned long prev_jiffy; /* ratelimiting */
7581
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07007582 rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
Thomas Gleixnerdb273be2014-02-07 20:58:38 +01007583 if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
7584 !is_idle_task(current)) ||
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007585 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007586 return;
7587 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7588 return;
7589 prev_jiffy = jiffies;
7590
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007591 printk(KERN_ERR
7592 "BUG: sleeping function called from invalid context at %s:%d\n",
7593 file, line);
7594 printk(KERN_ERR
7595 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7596 in_atomic(), irqs_disabled(),
7597 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007598
Eric Sandeena8b686b2014-12-16 16:25:28 -06007599 if (task_stack_end_corrupted(current))
7600 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
7601
Ingo Molnaraef745f2008-08-28 11:34:43 +02007602 debug_show_held_locks(current);
7603 if (irqs_disabled())
7604 print_irqtrace_events(current);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007605#ifdef CONFIG_DEBUG_PREEMPT
7606 if (!preempt_count_equals(preempt_offset)) {
7607 pr_err("Preemption disabled at:");
7608 print_ip_sym(current->preempt_disable_ip);
7609 pr_cont("\n");
7610 }
7611#endif
Ingo Molnaraef745f2008-08-28 11:34:43 +02007612 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613}
Peter Zijlstra34274452014-09-24 10:18:56 +02007614EXPORT_SYMBOL(___might_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615#endif
7616
7617#ifdef CONFIG_MAGIC_SYSRQ
7618void normalize_rt_tasks(void)
7619{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007620 struct task_struct *g, *p;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007621 struct sched_attr attr = {
7622 .sched_policy = SCHED_NORMAL,
7623 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007625 read_lock(&tasklist_lock);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007626 for_each_process_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007627 /*
7628 * Only normalize user tasks:
7629 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007630 if (p->flags & PF_KTHREAD)
Ingo Molnar178be792007-10-15 17:00:18 +02007631 continue;
7632
Ingo Molnardd41f592007-07-09 18:51:59 +02007633 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007634#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03007635 p->se.statistics.wait_start = 0;
7636 p->se.statistics.sleep_start = 0;
7637 p->se.statistics.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007638#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007639
Dario Faggioliaab03e02013-11-28 11:14:43 +01007640 if (!dl_task(p) && !rt_task(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007641 /*
7642 * Renice negative nice level userspace
7643 * tasks back to 0:
7644 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007645 if (task_nice(p) < 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02007646 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007650 __sched_setscheduler(p, &attr, false, false);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007651 }
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007652 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653}
7654
7655#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007656
Jason Wessel67fc4e02010-05-20 21:04:21 -05007657#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007658/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007659 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007660 *
7661 * They can only be called when the whole system has been
7662 * stopped - every CPU needs to be quiescent, and no scheduling
7663 * activity can take place. Using them for anything else would
7664 * be a serious bug, and as a result, they aren't even visible
7665 * under any other configuration.
7666 */
7667
7668/**
7669 * curr_task - return the current task for a given cpu.
7670 * @cpu: the processor in question.
7671 *
7672 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
Yacine Belkadie69f6182013-07-12 20:45:47 +02007673 *
7674 * Return: The current task for @cpu.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007675 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007676struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007677{
7678 return cpu_curr(cpu);
7679}
7680
Jason Wessel67fc4e02010-05-20 21:04:21 -05007681#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7682
7683#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07007684/**
7685 * set_curr_task - set the current task for a given cpu.
7686 * @cpu: the processor in question.
7687 * @p: the task pointer to set.
7688 *
7689 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007690 * are serviced on a separate stack. It allows the architecture to switch the
7691 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007692 * must be called with all CPU's synchronized, and interrupts disabled, the
7693 * and caller must save the original value of the current task (see
7694 * curr_task() above) and restore that value before reenabling interrupts and
7695 * re-starting the system.
7696 *
7697 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7698 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007699void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007700{
7701 cpu_curr(cpu) = p;
7702}
7703
7704#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007705
Dhaval Giani7c941432010-01-20 13:26:18 +01007706#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007707/* task_group_lock serializes the addition/removal of task groups */
7708static DEFINE_SPINLOCK(task_group_lock);
7709
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007710static void free_sched_group(struct task_group *tg)
7711{
7712 free_fair_sched_group(tg);
7713 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01007714 autogroup_free(tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007715 kfree(tg);
7716}
7717
7718/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007719struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007720{
7721 struct task_group *tg;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007722
7723 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7724 if (!tg)
7725 return ERR_PTR(-ENOMEM);
7726
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007727 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007728 goto err;
7729
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007730 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007731 goto err;
7732
Li Zefanace783b2013-01-24 14:30:48 +08007733 return tg;
7734
7735err:
7736 free_sched_group(tg);
7737 return ERR_PTR(-ENOMEM);
7738}
7739
7740void sched_online_group(struct task_group *tg, struct task_group *parent)
7741{
7742 unsigned long flags;
7743
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007744 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007745 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007746
7747 WARN_ON(!parent); /* root should already exist */
7748
7749 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007750 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08007751 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007752 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007753}
7754
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007755/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007756static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007757{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007758 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007759 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007760}
7761
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007762/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007763void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007764{
Li Zefanace783b2013-01-24 14:30:48 +08007765 /* wait for possible concurrent references to cfs_rqs complete */
7766 call_rcu(&tg->rcu, free_sched_group_rcu);
7767}
7768
7769void sched_offline_group(struct task_group *tg)
7770{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007771 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007772 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007773
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007774 /* end participation in shares distribution */
7775 for_each_possible_cpu(i)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007776 unregister_fair_sched_group(tg, i);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007777
7778 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007779 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007780 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007781 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007782}
7783
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007784/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007785 * The caller of this function should have put the task in its new group
7786 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7787 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007788 */
7789void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007790{
Peter Zijlstra8323f262012-06-22 13:36:05 +02007791 struct task_group *tg;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007792 int queued, running;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007793 unsigned long flags;
7794 struct rq *rq;
7795
7796 rq = task_rq_lock(tsk, &flags);
7797
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007798 running = task_current(rq, tsk);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007799 queued = task_on_rq_queued(tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007800
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007801 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02007802 dequeue_task(rq, tsk, DEQUEUE_SAVE);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007803 if (unlikely(running))
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04007804 put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007805
Kirill Tkhaif7b8a472014-10-28 08:24:34 +03007806 /*
7807 * All callers are synchronized by task_rq_lock(); we do not use RCU
7808 * which is pointless here. Thus, we pass "true" to task_css_check()
7809 * to prevent lockdep warnings.
7810 */
7811 tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
Peter Zijlstra8323f262012-06-22 13:36:05 +02007812 struct task_group, css);
7813 tg = autogroup_task_group(tsk, tg);
7814 tsk->sched_task_group = tg;
7815
Peter Zijlstra810b3812008-02-29 15:21:01 -05007816#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007817 if (tsk->sched_class->task_move_group)
Peter Zijlstrabc54da22015-08-31 17:13:55 +02007818 tsk->sched_class->task_move_group(tsk);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007819 else
Peter Zijlstra810b3812008-02-29 15:21:01 -05007820#endif
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007821 set_task_rq(tsk, task_cpu(tsk));
Peter Zijlstra810b3812008-02-29 15:21:01 -05007822
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007823 if (unlikely(running))
7824 tsk->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007825 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02007826 enqueue_task(rq, tsk, ENQUEUE_RESTORE);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007827
Peter Zijlstra0122ec52011-04-05 17:23:51 +02007828 task_rq_unlock(rq, tsk, &flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007829}
Dhaval Giani7c941432010-01-20 13:26:18 +01007830#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007831
Paul Turnera790de92011-07-21 09:43:29 -07007832#ifdef CONFIG_RT_GROUP_SCHED
7833/*
7834 * Ensure that the real time constraints are schedulable.
7835 */
7836static DEFINE_MUTEX(rt_constraints_mutex);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007837
Dhaval Giani521f1a242008-02-28 15:21:56 +05307838/* Must be called with tasklist_lock held */
7839static inline int tg_has_rt_tasks(struct task_group *tg)
7840{
7841 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007842
Peter Zijlstra1fe89e12015-02-09 11:53:18 +01007843 /*
7844 * Autogroups do not have RT tasks; see autogroup_create().
7845 */
7846 if (task_group_is_autogroup(tg))
7847 return 0;
7848
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007849 for_each_process_thread(g, p) {
Oleg Nesterov8651c652014-09-21 21:33:36 +02007850 if (rt_task(p) && task_group(p) == tg)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307851 return 1;
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007852 }
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007853
Dhaval Giani521f1a242008-02-28 15:21:56 +05307854 return 0;
7855}
7856
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007857struct rt_schedulable_data {
7858 struct task_group *tg;
7859 u64 rt_period;
7860 u64 rt_runtime;
7861};
7862
Paul Turnera790de92011-07-21 09:43:29 -07007863static int tg_rt_schedulable(struct task_group *tg, void *data)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007864{
7865 struct rt_schedulable_data *d = data;
7866 struct task_group *child;
7867 unsigned long total, sum = 0;
7868 u64 period, runtime;
7869
7870 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7871 runtime = tg->rt_bandwidth.rt_runtime;
7872
7873 if (tg == d->tg) {
7874 period = d->rt_period;
7875 runtime = d->rt_runtime;
7876 }
7877
Peter Zijlstra4653f802008-09-23 15:33:44 +02007878 /*
7879 * Cannot have more runtime than the period.
7880 */
7881 if (runtime > period && runtime != RUNTIME_INF)
7882 return -EINVAL;
7883
7884 /*
7885 * Ensure we don't starve existing RT tasks.
7886 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007887 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
7888 return -EBUSY;
7889
7890 total = to_ratio(period, runtime);
7891
Peter Zijlstra4653f802008-09-23 15:33:44 +02007892 /*
7893 * Nobody can have more than the global setting allows.
7894 */
7895 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
7896 return -EINVAL;
7897
7898 /*
7899 * The sum of our children's runtime should not exceed our own.
7900 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007901 list_for_each_entry_rcu(child, &tg->children, siblings) {
7902 period = ktime_to_ns(child->rt_bandwidth.rt_period);
7903 runtime = child->rt_bandwidth.rt_runtime;
7904
7905 if (child == d->tg) {
7906 period = d->rt_period;
7907 runtime = d->rt_runtime;
7908 }
7909
7910 sum += to_ratio(period, runtime);
7911 }
7912
7913 if (sum > total)
7914 return -EINVAL;
7915
7916 return 0;
7917}
7918
7919static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
7920{
Paul Turner82774342011-07-21 09:43:35 -07007921 int ret;
7922
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007923 struct rt_schedulable_data data = {
7924 .tg = tg,
7925 .rt_period = period,
7926 .rt_runtime = runtime,
7927 };
7928
Paul Turner82774342011-07-21 09:43:35 -07007929 rcu_read_lock();
7930 ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
7931 rcu_read_unlock();
7932
7933 return ret;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007934}
7935
Paul Turnerab84d312011-07-21 09:43:28 -07007936static int tg_set_rt_bandwidth(struct task_group *tg,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007937 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007938{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007939 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007940
Peter Zijlstra2636ed52015-02-09 12:23:20 +01007941 /*
7942 * Disallowing the root group RT runtime is BAD, it would disallow the
7943 * kernel creating (and or operating) RT threads.
7944 */
7945 if (tg == &root_task_group && rt_runtime == 0)
7946 return -EINVAL;
7947
7948 /* No period doesn't make any sense. */
7949 if (rt_period == 0)
7950 return -EINVAL;
7951
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007952 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307953 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007954 err = __rt_schedulable(tg, rt_period, rt_runtime);
7955 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307956 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007957
Thomas Gleixner0986b112009-11-17 15:32:06 +01007958 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007959 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
7960 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007961
7962 for_each_possible_cpu(i) {
7963 struct rt_rq *rt_rq = tg->rt_rq[i];
7964
Thomas Gleixner0986b112009-11-17 15:32:06 +01007965 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007966 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01007967 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007968 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007969 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra49246272010-10-17 21:46:10 +02007970unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307971 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007972 mutex_unlock(&rt_constraints_mutex);
7973
7974 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007975}
7976
Li Zefan25cc7da2013-03-05 16:07:33 +08007977static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007978{
7979 u64 rt_runtime, rt_period;
7980
7981 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7982 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7983 if (rt_runtime_us < 0)
7984 rt_runtime = RUNTIME_INF;
7985
Paul Turnerab84d312011-07-21 09:43:28 -07007986 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007987}
7988
Li Zefan25cc7da2013-03-05 16:07:33 +08007989static long sched_group_rt_runtime(struct task_group *tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007990{
7991 u64 rt_runtime_us;
7992
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007993 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007994 return -1;
7995
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007996 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007997 do_div(rt_runtime_us, NSEC_PER_USEC);
7998 return rt_runtime_us;
7999}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008000
Nicholas Mc Guirece2f5fe2015-05-03 10:51:56 +02008001static int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008002{
8003 u64 rt_runtime, rt_period;
8004
Nicholas Mc Guirece2f5fe2015-05-03 10:51:56 +02008005 rt_period = rt_period_us * NSEC_PER_USEC;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008006 rt_runtime = tg->rt_bandwidth.rt_runtime;
8007
Paul Turnerab84d312011-07-21 09:43:28 -07008008 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008009}
8010
Li Zefan25cc7da2013-03-05 16:07:33 +08008011static long sched_group_rt_period(struct task_group *tg)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008012{
8013 u64 rt_period_us;
8014
8015 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8016 do_div(rt_period_us, NSEC_PER_USEC);
8017 return rt_period_us;
8018}
Dario Faggioli332ac172013-11-07 14:43:45 +01008019#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008020
Dario Faggioli332ac172013-11-07 14:43:45 +01008021#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008022static int sched_rt_global_constraints(void)
8023{
8024 int ret = 0;
8025
8026 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008027 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02008028 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008029 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008030 mutex_unlock(&rt_constraints_mutex);
8031
8032 return ret;
8033}
Dhaval Giani54e99122009-02-27 15:13:54 +05308034
Li Zefan25cc7da2013-03-05 16:07:33 +08008035static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
Dhaval Giani54e99122009-02-27 15:13:54 +05308036{
8037 /* Don't accept realtime tasks when there is no way for them to run */
8038 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
8039 return 0;
8040
8041 return 1;
8042}
8043
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008044#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008045static int sched_rt_global_constraints(void)
8046{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008047 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01008048 int i, ret = 0;
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07008049
Thomas Gleixner0986b112009-11-17 15:32:06 +01008050 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008051 for_each_possible_cpu(i) {
8052 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8053
Thomas Gleixner0986b112009-11-17 15:32:06 +01008054 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008055 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01008056 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008057 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01008058 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008059
Dario Faggioli332ac172013-11-07 14:43:45 +01008060 return ret;
8061}
8062#endif /* CONFIG_RT_GROUP_SCHED */
8063
Wanpeng Lia1963b82015-03-17 19:15:31 +08008064static int sched_dl_global_validate(void)
Dario Faggioli332ac172013-11-07 14:43:45 +01008065{
Peter Zijlstra17248132013-12-17 12:44:49 +01008066 u64 runtime = global_rt_runtime();
8067 u64 period = global_rt_period();
Dario Faggioli332ac172013-11-07 14:43:45 +01008068 u64 new_bw = to_ratio(period, runtime);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008069 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01008070 int cpu, ret = 0;
Juri Lelli49516342014-02-11 09:24:27 +01008071 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01008072
8073 /*
8074 * Here we want to check the bandwidth not being set to some
8075 * value smaller than the currently allocated bandwidth in
8076 * any of the root_domains.
8077 *
8078 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
8079 * cycling on root_domains... Discussion on different/better
8080 * solutions is welcome!
8081 */
Peter Zijlstra17248132013-12-17 12:44:49 +01008082 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008083 rcu_read_lock_sched();
8084 dl_b = dl_bw_of(cpu);
Dario Faggioli332ac172013-11-07 14:43:45 +01008085
Juri Lelli49516342014-02-11 09:24:27 +01008086 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01008087 if (new_bw < dl_b->total_bw)
8088 ret = -EBUSY;
Juri Lelli49516342014-02-11 09:24:27 +01008089 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01008090
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008091 rcu_read_unlock_sched();
8092
Peter Zijlstra17248132013-12-17 12:44:49 +01008093 if (ret)
8094 break;
Dario Faggioli332ac172013-11-07 14:43:45 +01008095 }
8096
Peter Zijlstra17248132013-12-17 12:44:49 +01008097 return ret;
8098}
8099
8100static void sched_dl_do_global(void)
8101{
8102 u64 new_bw = -1;
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008103 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01008104 int cpu;
Juri Lelli49516342014-02-11 09:24:27 +01008105 unsigned long flags;
Peter Zijlstra17248132013-12-17 12:44:49 +01008106
8107 def_dl_bandwidth.dl_period = global_rt_period();
8108 def_dl_bandwidth.dl_runtime = global_rt_runtime();
8109
8110 if (global_rt_runtime() != RUNTIME_INF)
8111 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
8112
8113 /*
8114 * FIXME: As above...
8115 */
8116 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008117 rcu_read_lock_sched();
8118 dl_b = dl_bw_of(cpu);
Peter Zijlstra17248132013-12-17 12:44:49 +01008119
Juri Lelli49516342014-02-11 09:24:27 +01008120 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01008121 dl_b->bw = new_bw;
Juri Lelli49516342014-02-11 09:24:27 +01008122 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04008123
8124 rcu_read_unlock_sched();
Peter Zijlstra17248132013-12-17 12:44:49 +01008125 }
8126}
8127
8128static int sched_rt_global_validate(void)
8129{
8130 if (sysctl_sched_rt_period <= 0)
8131 return -EINVAL;
8132
Juri Lellie9e7cb32014-02-11 09:24:26 +01008133 if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
8134 (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
Peter Zijlstra17248132013-12-17 12:44:49 +01008135 return -EINVAL;
8136
Dario Faggioli332ac172013-11-07 14:43:45 +01008137 return 0;
8138}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008139
Peter Zijlstra17248132013-12-17 12:44:49 +01008140static void sched_rt_do_global(void)
8141{
8142 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8143 def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
8144}
8145
8146int sched_rt_handler(struct ctl_table *table, int write,
8147 void __user *buffer, size_t *lenp,
8148 loff_t *ppos)
8149{
8150 int old_period, old_runtime;
8151 static DEFINE_MUTEX(mutex);
8152 int ret;
8153
8154 mutex_lock(&mutex);
8155 old_period = sysctl_sched_rt_period;
8156 old_runtime = sysctl_sched_rt_runtime;
8157
8158 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8159
8160 if (!ret && write) {
8161 ret = sched_rt_global_validate();
8162 if (ret)
8163 goto undo;
8164
Wanpeng Lia1963b82015-03-17 19:15:31 +08008165 ret = sched_dl_global_validate();
Peter Zijlstra17248132013-12-17 12:44:49 +01008166 if (ret)
8167 goto undo;
8168
Wanpeng Lia1963b82015-03-17 19:15:31 +08008169 ret = sched_rt_global_constraints();
Peter Zijlstra17248132013-12-17 12:44:49 +01008170 if (ret)
8171 goto undo;
8172
8173 sched_rt_do_global();
8174 sched_dl_do_global();
8175 }
8176 if (0) {
8177undo:
8178 sysctl_sched_rt_period = old_period;
8179 sysctl_sched_rt_runtime = old_runtime;
8180 }
8181 mutex_unlock(&mutex);
8182
8183 return ret;
8184}
8185
Clark Williamsce0dbbb2013-02-07 09:47:04 -06008186int sched_rr_handler(struct ctl_table *table, int write,
8187 void __user *buffer, size_t *lenp,
8188 loff_t *ppos)
8189{
8190 int ret;
8191 static DEFINE_MUTEX(mutex);
8192
8193 mutex_lock(&mutex);
8194 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8195 /* make sure that internally we keep jiffies */
8196 /* also, writing zero resets timeslice to default */
8197 if (!ret && write) {
8198 sched_rr_timeslice = sched_rr_timeslice <= 0 ?
8199 RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice);
8200 }
8201 mutex_unlock(&mutex);
8202 return ret;
8203}
8204
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008205#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008206
Tejun Heoa7c6d552013-08-08 20:11:23 -04008207static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008208{
Tejun Heoa7c6d552013-08-08 20:11:23 -04008209 return css ? container_of(css, struct task_group, css) : NULL;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008210}
8211
Tejun Heoeb954192013-08-08 20:11:23 -04008212static struct cgroup_subsys_state *
8213cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008214{
Tejun Heoeb954192013-08-08 20:11:23 -04008215 struct task_group *parent = css_tg(parent_css);
8216 struct task_group *tg;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008217
Tejun Heoeb954192013-08-08 20:11:23 -04008218 if (!parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008219 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08008220 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008221 }
8222
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008223 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008224 if (IS_ERR(tg))
8225 return ERR_PTR(-ENOMEM);
8226
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008227 return &tg->css;
8228}
8229
Tejun Heoeb954192013-08-08 20:11:23 -04008230static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08008231{
Tejun Heoeb954192013-08-08 20:11:23 -04008232 struct task_group *tg = css_tg(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04008233 struct task_group *parent = css_tg(css->parent);
Li Zefanace783b2013-01-24 14:30:48 +08008234
Tejun Heo63876982013-08-08 20:11:23 -04008235 if (parent)
8236 sched_online_group(tg, parent);
Li Zefanace783b2013-01-24 14:30:48 +08008237 return 0;
8238}
8239
Tejun Heoeb954192013-08-08 20:11:23 -04008240static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008241{
Tejun Heoeb954192013-08-08 20:11:23 -04008242 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008243
8244 sched_destroy_group(tg);
8245}
8246
Tejun Heoeb954192013-08-08 20:11:23 -04008247static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08008248{
Tejun Heoeb954192013-08-08 20:11:23 -04008249 struct task_group *tg = css_tg(css);
Li Zefanace783b2013-01-24 14:30:48 +08008250
8251 sched_offline_group(tg);
8252}
8253
Aleksa Sarai7e476822015-06-09 21:32:09 +10008254static void cpu_cgroup_fork(struct task_struct *task, void *private)
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008255{
8256 sched_move_task(task);
8257}
8258
Tejun Heoeb954192013-08-08 20:11:23 -04008259static int cpu_cgroup_can_attach(struct cgroup_subsys_state *css,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008260 struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008261{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008262 struct task_struct *task;
8263
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008264 cgroup_taskset_for_each(task, tset) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008265#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heoeb954192013-08-08 20:11:23 -04008266 if (!sched_rt_can_attach(css_tg(css), task))
Tejun Heobb9d97b2011-12-12 18:12:21 -08008267 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008268#else
Tejun Heobb9d97b2011-12-12 18:12:21 -08008269 /* We don't support RT-tasks being in separate groups */
8270 if (task->sched_class != &fair_sched_class)
8271 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008272#endif
Tejun Heobb9d97b2011-12-12 18:12:21 -08008273 }
Ben Blumbe367d02009-09-23 15:56:31 -07008274 return 0;
8275}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008276
Tejun Heoeb954192013-08-08 20:11:23 -04008277static void cpu_cgroup_attach(struct cgroup_subsys_state *css,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008278 struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008279{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008280 struct task_struct *task;
8281
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008282 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008283 sched_move_task(task);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008284}
8285
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008286#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008287static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
8288 struct cftype *cftype, u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008289{
Tejun Heo182446d2013-08-08 20:11:24 -04008290 return sched_group_set_shares(css_tg(css), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008291}
8292
Tejun Heo182446d2013-08-08 20:11:24 -04008293static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
8294 struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008295{
Tejun Heo182446d2013-08-08 20:11:24 -04008296 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008297
Nikhil Raoc8b28112011-05-18 14:37:48 -07008298 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008299}
Paul Turnerab84d312011-07-21 09:43:28 -07008300
8301#ifdef CONFIG_CFS_BANDWIDTH
Paul Turnera790de92011-07-21 09:43:29 -07008302static DEFINE_MUTEX(cfs_constraints_mutex);
8303
Paul Turnerab84d312011-07-21 09:43:28 -07008304const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
8305const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
8306
Paul Turnera790de92011-07-21 09:43:29 -07008307static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
8308
Paul Turnerab84d312011-07-21 09:43:28 -07008309static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
8310{
Paul Turner56f570e2011-11-07 20:26:33 -08008311 int i, ret = 0, runtime_enabled, runtime_was_enabled;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008312 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnerab84d312011-07-21 09:43:28 -07008313
8314 if (tg == &root_task_group)
8315 return -EINVAL;
8316
8317 /*
8318 * Ensure we have at some amount of bandwidth every period. This is
8319 * to prevent reaching a state of large arrears when throttled via
8320 * entity_tick() resulting in prolonged exit starvation.
8321 */
8322 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
8323 return -EINVAL;
8324
8325 /*
8326 * Likewise, bound things on the otherside by preventing insane quota
8327 * periods. This also allows us to normalize in computing quota
8328 * feasibility.
8329 */
8330 if (period > max_cfs_quota_period)
8331 return -EINVAL;
8332
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008333 /*
8334 * Prevent race between setting of cfs_rq->runtime_enabled and
8335 * unthrottle_offline_cfs_rqs().
8336 */
8337 get_online_cpus();
Paul Turnera790de92011-07-21 09:43:29 -07008338 mutex_lock(&cfs_constraints_mutex);
8339 ret = __cfs_schedulable(tg, period, quota);
8340 if (ret)
8341 goto out_unlock;
8342
Paul Turner58088ad2011-07-21 09:43:31 -07008343 runtime_enabled = quota != RUNTIME_INF;
Paul Turner56f570e2011-11-07 20:26:33 -08008344 runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
Ben Segall1ee14e62013-10-16 11:16:12 -07008345 /*
8346 * If we need to toggle cfs_bandwidth_used, off->on must occur
8347 * before making related changes, and on->off must occur afterwards
8348 */
8349 if (runtime_enabled && !runtime_was_enabled)
8350 cfs_bandwidth_usage_inc();
Paul Turnerab84d312011-07-21 09:43:28 -07008351 raw_spin_lock_irq(&cfs_b->lock);
8352 cfs_b->period = ns_to_ktime(period);
8353 cfs_b->quota = quota;
Paul Turner58088ad2011-07-21 09:43:31 -07008354
Paul Turnera9cf55b2011-07-21 09:43:32 -07008355 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07008356 /* restart the period timer (if active) to handle new period expiry */
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02008357 if (runtime_enabled)
8358 start_cfs_bandwidth(cfs_b);
Paul Turnerab84d312011-07-21 09:43:28 -07008359 raw_spin_unlock_irq(&cfs_b->lock);
8360
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008361 for_each_online_cpu(i) {
Paul Turnerab84d312011-07-21 09:43:28 -07008362 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
Peter Zijlstra029632f2011-10-25 10:00:11 +02008363 struct rq *rq = cfs_rq->rq;
Paul Turnerab84d312011-07-21 09:43:28 -07008364
8365 raw_spin_lock_irq(&rq->lock);
Paul Turner58088ad2011-07-21 09:43:31 -07008366 cfs_rq->runtime_enabled = runtime_enabled;
Paul Turnerab84d312011-07-21 09:43:28 -07008367 cfs_rq->runtime_remaining = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07008368
Peter Zijlstra029632f2011-10-25 10:00:11 +02008369 if (cfs_rq->throttled)
Paul Turner671fd9d2011-07-21 09:43:34 -07008370 unthrottle_cfs_rq(cfs_rq);
Paul Turnerab84d312011-07-21 09:43:28 -07008371 raw_spin_unlock_irq(&rq->lock);
8372 }
Ben Segall1ee14e62013-10-16 11:16:12 -07008373 if (runtime_was_enabled && !runtime_enabled)
8374 cfs_bandwidth_usage_dec();
Paul Turnera790de92011-07-21 09:43:29 -07008375out_unlock:
8376 mutex_unlock(&cfs_constraints_mutex);
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008377 put_online_cpus();
Paul Turnerab84d312011-07-21 09:43:28 -07008378
Paul Turnera790de92011-07-21 09:43:29 -07008379 return ret;
Paul Turnerab84d312011-07-21 09:43:28 -07008380}
8381
8382int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
8383{
8384 u64 quota, period;
8385
Peter Zijlstra029632f2011-10-25 10:00:11 +02008386 period = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008387 if (cfs_quota_us < 0)
8388 quota = RUNTIME_INF;
8389 else
8390 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
8391
8392 return tg_set_cfs_bandwidth(tg, period, quota);
8393}
8394
8395long tg_get_cfs_quota(struct task_group *tg)
8396{
8397 u64 quota_us;
8398
Peter Zijlstra029632f2011-10-25 10:00:11 +02008399 if (tg->cfs_bandwidth.quota == RUNTIME_INF)
Paul Turnerab84d312011-07-21 09:43:28 -07008400 return -1;
8401
Peter Zijlstra029632f2011-10-25 10:00:11 +02008402 quota_us = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008403 do_div(quota_us, NSEC_PER_USEC);
8404
8405 return quota_us;
8406}
8407
8408int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
8409{
8410 u64 quota, period;
8411
8412 period = (u64)cfs_period_us * NSEC_PER_USEC;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008413 quota = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008414
Paul Turnerab84d312011-07-21 09:43:28 -07008415 return tg_set_cfs_bandwidth(tg, period, quota);
8416}
8417
8418long tg_get_cfs_period(struct task_group *tg)
8419{
8420 u64 cfs_period_us;
8421
Peter Zijlstra029632f2011-10-25 10:00:11 +02008422 cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008423 do_div(cfs_period_us, NSEC_PER_USEC);
8424
8425 return cfs_period_us;
8426}
8427
Tejun Heo182446d2013-08-08 20:11:24 -04008428static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
8429 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008430{
Tejun Heo182446d2013-08-08 20:11:24 -04008431 return tg_get_cfs_quota(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008432}
8433
Tejun Heo182446d2013-08-08 20:11:24 -04008434static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
8435 struct cftype *cftype, s64 cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008436{
Tejun Heo182446d2013-08-08 20:11:24 -04008437 return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008438}
8439
Tejun Heo182446d2013-08-08 20:11:24 -04008440static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
8441 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008442{
Tejun Heo182446d2013-08-08 20:11:24 -04008443 return tg_get_cfs_period(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008444}
8445
Tejun Heo182446d2013-08-08 20:11:24 -04008446static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
8447 struct cftype *cftype, u64 cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008448{
Tejun Heo182446d2013-08-08 20:11:24 -04008449 return tg_set_cfs_period(css_tg(css), cfs_period_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008450}
8451
Paul Turnera790de92011-07-21 09:43:29 -07008452struct cfs_schedulable_data {
8453 struct task_group *tg;
8454 u64 period, quota;
8455};
8456
8457/*
8458 * normalize group quota/period to be quota/max_period
8459 * note: units are usecs
8460 */
8461static u64 normalize_cfs_quota(struct task_group *tg,
8462 struct cfs_schedulable_data *d)
8463{
8464 u64 quota, period;
8465
8466 if (tg == d->tg) {
8467 period = d->period;
8468 quota = d->quota;
8469 } else {
8470 period = tg_get_cfs_period(tg);
8471 quota = tg_get_cfs_quota(tg);
8472 }
8473
8474 /* note: these should typically be equivalent */
8475 if (quota == RUNTIME_INF || quota == -1)
8476 return RUNTIME_INF;
8477
8478 return to_ratio(period, quota);
8479}
8480
8481static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
8482{
8483 struct cfs_schedulable_data *d = data;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008484 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008485 s64 quota = 0, parent_quota = -1;
8486
8487 if (!tg->parent) {
8488 quota = RUNTIME_INF;
8489 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02008490 struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008491
8492 quota = normalize_cfs_quota(tg, d);
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008493 parent_quota = parent_b->hierarchical_quota;
Paul Turnera790de92011-07-21 09:43:29 -07008494
8495 /*
8496 * ensure max(child_quota) <= parent_quota, inherit when no
8497 * limit is set
8498 */
8499 if (quota == RUNTIME_INF)
8500 quota = parent_quota;
8501 else if (parent_quota != RUNTIME_INF && quota > parent_quota)
8502 return -EINVAL;
8503 }
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008504 cfs_b->hierarchical_quota = quota;
Paul Turnera790de92011-07-21 09:43:29 -07008505
8506 return 0;
8507}
8508
8509static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
8510{
Paul Turner82774342011-07-21 09:43:35 -07008511 int ret;
Paul Turnera790de92011-07-21 09:43:29 -07008512 struct cfs_schedulable_data data = {
8513 .tg = tg,
8514 .period = period,
8515 .quota = quota,
8516 };
8517
8518 if (quota != RUNTIME_INF) {
8519 do_div(data.period, NSEC_PER_USEC);
8520 do_div(data.quota, NSEC_PER_USEC);
8521 }
8522
Paul Turner82774342011-07-21 09:43:35 -07008523 rcu_read_lock();
8524 ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
8525 rcu_read_unlock();
8526
8527 return ret;
Paul Turnera790de92011-07-21 09:43:29 -07008528}
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008529
Tejun Heo2da8ca82013-12-05 12:28:04 -05008530static int cpu_stats_show(struct seq_file *sf, void *v)
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008531{
Tejun Heo2da8ca82013-12-05 12:28:04 -05008532 struct task_group *tg = css_tg(seq_css(sf));
Peter Zijlstra029632f2011-10-25 10:00:11 +02008533 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008534
Tejun Heo44ffc752013-12-05 12:28:01 -05008535 seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
8536 seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
8537 seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008538
8539 return 0;
8540}
Paul Turnerab84d312011-07-21 09:43:28 -07008541#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008542#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008543
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008544#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008545static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
8546 struct cftype *cft, s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008547{
Tejun Heo182446d2013-08-08 20:11:24 -04008548 return sched_group_set_rt_runtime(css_tg(css), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008549}
8550
Tejun Heo182446d2013-08-08 20:11:24 -04008551static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
8552 struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008553{
Tejun Heo182446d2013-08-08 20:11:24 -04008554 return sched_group_rt_runtime(css_tg(css));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008555}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008556
Tejun Heo182446d2013-08-08 20:11:24 -04008557static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
8558 struct cftype *cftype, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008559{
Tejun Heo182446d2013-08-08 20:11:24 -04008560 return sched_group_set_rt_period(css_tg(css), rt_period_us);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008561}
8562
Tejun Heo182446d2013-08-08 20:11:24 -04008563static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
8564 struct cftype *cft)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008565{
Tejun Heo182446d2013-08-08 20:11:24 -04008566 return sched_group_rt_period(css_tg(css));
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008567}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008568#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008569
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008570static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008571#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008572 {
8573 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008574 .read_u64 = cpu_shares_read_u64,
8575 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008576 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008577#endif
Paul Turnerab84d312011-07-21 09:43:28 -07008578#ifdef CONFIG_CFS_BANDWIDTH
8579 {
8580 .name = "cfs_quota_us",
8581 .read_s64 = cpu_cfs_quota_read_s64,
8582 .write_s64 = cpu_cfs_quota_write_s64,
8583 },
8584 {
8585 .name = "cfs_period_us",
8586 .read_u64 = cpu_cfs_period_read_u64,
8587 .write_u64 = cpu_cfs_period_write_u64,
8588 },
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008589 {
8590 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05008591 .seq_show = cpu_stats_show,
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008592 },
Paul Turnerab84d312011-07-21 09:43:28 -07008593#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008594#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008595 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008596 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008597 .read_s64 = cpu_rt_runtime_read,
8598 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008599 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008600 {
8601 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008602 .read_u64 = cpu_rt_period_read_uint,
8603 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008604 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008605#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07008606 { } /* terminate */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008607};
8608
Tejun Heo073219e2014-02-08 10:36:58 -05008609struct cgroup_subsys cpu_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008610 .css_alloc = cpu_cgroup_css_alloc,
8611 .css_free = cpu_cgroup_css_free,
Li Zefanace783b2013-01-24 14:30:48 +08008612 .css_online = cpu_cgroup_css_online,
8613 .css_offline = cpu_cgroup_css_offline,
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008614 .fork = cpu_cgroup_fork,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008615 .can_attach = cpu_cgroup_can_attach,
8616 .attach = cpu_cgroup_attach,
Tejun Heo55779642014-07-15 11:05:09 -04008617 .legacy_cftypes = cpu_files,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008618 .early_init = 1,
8619};
8620
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008621#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008622
Paul E. McKenneyb637a322012-09-19 16:58:38 -07008623void dump_cpu_task(int cpu)
8624{
8625 pr_info("Task dump for CPU %d:\n", cpu);
8626 sched_show_task(cpu_curr(cpu));
8627}
Andi Kleened82b8a2015-11-29 20:59:43 -08008628
8629/*
8630 * Nice levels are multiplicative, with a gentle 10% change for every
8631 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
8632 * nice 1, it will get ~10% less CPU time than another CPU-bound task
8633 * that remained on nice 0.
8634 *
8635 * The "10% effect" is relative and cumulative: from _any_ nice level,
8636 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
8637 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
8638 * If a task goes up by ~10% and another task goes down by ~10% then
8639 * the relative distance between them is ~25%.)
8640 */
8641const int sched_prio_to_weight[40] = {
8642 /* -20 */ 88761, 71755, 56483, 46273, 36291,
8643 /* -15 */ 29154, 23254, 18705, 14949, 11916,
8644 /* -10 */ 9548, 7620, 6100, 4904, 3906,
8645 /* -5 */ 3121, 2501, 1991, 1586, 1277,
8646 /* 0 */ 1024, 820, 655, 526, 423,
8647 /* 5 */ 335, 272, 215, 172, 137,
8648 /* 10 */ 110, 87, 70, 56, 45,
8649 /* 15 */ 36, 29, 23, 18, 15,
8650};
8651
8652/*
8653 * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated.
8654 *
8655 * In cases where the weight does not change often, we can use the
8656 * precalculated inverse to speed up arithmetics by turning divisions
8657 * into multiplications:
8658 */
8659const u32 sched_prio_to_wmult[40] = {
8660 /* -20 */ 48388, 59856, 76040, 92818, 118348,
8661 /* -15 */ 147320, 184698, 229616, 287308, 360437,
8662 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
8663 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
8664 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
8665 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
8666 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
8667 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
8668};