blob: d811652fe6f598633ca9a955b7aace4fe5b923c5 [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{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100167 if (static_key_enabled(&sched_feat_keys[i]))
168 static_key_slow_dec(&sched_feat_keys[i]);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200169}
170
171static void sched_feat_enable(int i)
172{
Ingo Molnarc5905af2012-02-24 08:31:31 +0100173 if (!static_key_enabled(&sched_feat_keys[i]))
174 static_key_slow_inc(&sched_feat_keys[i]);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200175}
176#else
177static void sched_feat_disable(int i) { };
178static void sched_feat_enable(int i) { };
179#endif /* HAVE_JUMP_LABEL */
180
Mel Gorman1a687c22012-11-22 11:16:36 +0000181static int sched_feat_set(char *cmp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200182{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200183 int i;
Mel Gorman1a687c22012-11-22 11:16:36 +0000184 int neg = 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200185
Hillf Danton524429c2011-01-06 20:58:12 +0800186 if (strncmp(cmp, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200187 neg = 1;
188 cmp += 3;
189 }
190
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200191 for (i = 0; i < __SCHED_FEAT_NR; i++) {
Mathieu Desnoyers77401912010-09-13 17:47:00 -0400192 if (strcmp(cmp, sched_feat_names[i]) == 0) {
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200193 if (neg) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200194 sysctl_sched_features &= ~(1UL << i);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200195 sched_feat_disable(i);
196 } else {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200197 sysctl_sched_features |= (1UL << i);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200198 sched_feat_enable(i);
199 }
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200200 break;
201 }
202 }
203
Mel Gorman1a687c22012-11-22 11:16:36 +0000204 return i;
205}
206
207static ssize_t
208sched_feat_write(struct file *filp, const char __user *ubuf,
209 size_t cnt, loff_t *ppos)
210{
211 char buf[64];
212 char *cmp;
213 int i;
Jason Baron5cd08fb2014-07-02 15:52:44 +0000214 struct inode *inode;
Mel Gorman1a687c22012-11-22 11:16:36 +0000215
216 if (cnt > 63)
217 cnt = 63;
218
219 if (copy_from_user(&buf, ubuf, cnt))
220 return -EFAULT;
221
222 buf[cnt] = 0;
223 cmp = strstrip(buf);
224
Jason Baron5cd08fb2014-07-02 15:52:44 +0000225 /* Ensure the static_key remains in a consistent state */
226 inode = file_inode(filp);
227 mutex_lock(&inode->i_mutex);
Mel Gorman1a687c22012-11-22 11:16:36 +0000228 i = sched_feat_set(cmp);
Jason Baron5cd08fb2014-07-02 15:52:44 +0000229 mutex_unlock(&inode->i_mutex);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200230 if (i == __SCHED_FEAT_NR)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200231 return -EINVAL;
232
Jan Blunck42994722009-11-20 17:40:37 +0100233 *ppos += cnt;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200234
235 return cnt;
236}
237
Li Zefan34f3a812008-10-30 15:23:32 +0800238static int sched_feat_open(struct inode *inode, struct file *filp)
239{
240 return single_open(filp, sched_feat_show, NULL);
241}
242
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700243static const struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800244 .open = sched_feat_open,
245 .write = sched_feat_write,
246 .read = seq_read,
247 .llseek = seq_lseek,
248 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200249};
250
251static __init int sched_init_debug(void)
252{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200253 debugfs_create_file("sched_features", 0644, NULL, NULL,
254 &sched_feat_fops);
255
256 return 0;
257}
258late_initcall(sched_init_debug);
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +0200259#endif /* CONFIG_SCHED_DEBUG */
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200260
261/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100262 * Number of tasks to iterate in a single balance run.
263 * Limited because this is done with IRQs disabled.
264 */
265const_debug unsigned int sysctl_sched_nr_migrate = 32;
266
267/*
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200268 * period over which we average the RT time consumption, measured
269 * in ms.
270 *
271 * default: 1s
272 */
273const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
274
275/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100276 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100277 * default: 1s
278 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100279unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100280
Peter Zijlstra029632f2011-10-25 10:00:11 +0200281__read_mostly int scheduler_running;
Ingo Molnar6892b752008-02-13 14:02:36 +0100282
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100283/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100284 * part of the period that we allow rt tasks to run in us.
285 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100286 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100287int sysctl_sched_rt_runtime = 950000;
288
Rik van Riel3fa08182015-03-09 12:12:07 -0400289/* cpus with isolated domains */
290cpumask_var_t cpu_isolated_map;
291
Dario Faggioli332ac172013-11-07 14:43:45 +0100292/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800293 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200295static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 __acquires(rq->lock)
297{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700298 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 local_irq_disable();
301 rq = this_rq();
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100302 raw_spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return rq;
305}
306
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100307#ifdef CONFIG_SCHED_HRTICK
308/*
309 * Use HR-timers to deliver accurate preemption points.
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100310 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100311
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100312static void hrtick_clear(struct rq *rq)
313{
314 if (hrtimer_active(&rq->hrtick_timer))
315 hrtimer_cancel(&rq->hrtick_timer);
316}
317
318/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100319 * High-resolution timer tick.
320 * Runs from hardirq context with interrupts disabled.
321 */
322static enum hrtimer_restart hrtick(struct hrtimer *timer)
323{
324 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
325
326 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
327
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100328 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200329 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100330 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100331 raw_spin_unlock(&rq->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100332
333 return HRTIMER_NORESTART;
334}
335
Rabin Vincent95e904c2008-05-11 05:55:33 +0530336#ifdef CONFIG_SMP
Peter Zijlstra971ee282013-06-28 11:18:53 +0200337
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000338static void __hrtick_restart(struct rq *rq)
Peter Zijlstra971ee282013-06-28 11:18:53 +0200339{
340 struct hrtimer *timer = &rq->hrtick_timer;
Peter Zijlstra971ee282013-06-28 11:18:53 +0200341
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000342 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200343}
344
Peter Zijlstra31656512008-07-18 18:01:23 +0200345/*
346 * called from hardirq (IPI) context
347 */
348static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200349{
Peter Zijlstra31656512008-07-18 18:01:23 +0200350 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200351
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100352 raw_spin_lock(&rq->lock);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200353 __hrtick_restart(rq);
Peter Zijlstra31656512008-07-18 18:01:23 +0200354 rq->hrtick_csd_pending = 0;
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100355 raw_spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200356}
357
Peter Zijlstra31656512008-07-18 18:01:23 +0200358/*
359 * Called to set the hrtick timer state.
360 *
361 * called with rq->lock held and irqs disabled
362 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200363void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200364{
Peter Zijlstra31656512008-07-18 18:01:23 +0200365 struct hrtimer *timer = &rq->hrtick_timer;
xiaofeng.yan177ef2a2014-08-26 03:15:41 +0000366 ktime_t time;
367 s64 delta;
368
369 /*
370 * Don't schedule slices shorter than 10000ns, that just
371 * doesn't make sense and can cause timer DoS.
372 */
373 delta = max_t(s64, delay, 10000LL);
374 time = ktime_add_ns(timer->base->get_time(), delta);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200375
Arjan van de Vencc584b22008-09-01 15:02:30 -0700376 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +0200377
378 if (rq == this_rq()) {
Peter Zijlstra971ee282013-06-28 11:18:53 +0200379 __hrtick_restart(rq);
Peter Zijlstra31656512008-07-18 18:01:23 +0200380 } else if (!rq->hrtick_csd_pending) {
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100381 smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
Peter Zijlstra31656512008-07-18 18:01:23 +0200382 rq->hrtick_csd_pending = 1;
383 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200384}
385
386static int
387hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
388{
389 int cpu = (int)(long)hcpu;
390
391 switch (action) {
392 case CPU_UP_CANCELED:
393 case CPU_UP_CANCELED_FROZEN:
394 case CPU_DOWN_PREPARE:
395 case CPU_DOWN_PREPARE_FROZEN:
396 case CPU_DEAD:
397 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +0200398 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200399 return NOTIFY_OK;
400 }
401
402 return NOTIFY_DONE;
403}
404
Rakib Mullickfa748202008-09-22 14:55:45 -0700405static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200406{
407 hotcpu_notifier(hotplug_hrtick, 0);
408}
Peter Zijlstra31656512008-07-18 18:01:23 +0200409#else
410/*
411 * Called to set the hrtick timer state.
412 *
413 * called with rq->lock held and irqs disabled
414 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200415void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstra31656512008-07-18 18:01:23 +0200416{
Wanpeng Li86893332014-11-26 08:44:06 +0800417 /*
418 * Don't schedule slices shorter than 10000ns, that just
419 * doesn't make sense. Rely on vruntime for fairness.
420 */
421 delay = max_t(u64, delay, 10000LL);
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000422 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
423 HRTIMER_MODE_REL_PINNED);
Peter Zijlstra31656512008-07-18 18:01:23 +0200424}
425
Andrew Morton006c75f2008-09-22 14:55:46 -0700426static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +0200427{
428}
Rabin Vincent95e904c2008-05-11 05:55:33 +0530429#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200430
431static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100432{
Peter Zijlstra31656512008-07-18 18:01:23 +0200433#ifdef CONFIG_SMP
434 rq->hrtick_csd_pending = 0;
435
436 rq->hrtick_csd.flags = 0;
437 rq->hrtick_csd.func = __hrtick_start;
438 rq->hrtick_csd.info = rq;
439#endif
440
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100441 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
442 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100443}
Andrew Morton006c75f2008-09-22 14:55:46 -0700444#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100445static inline void hrtick_clear(struct rq *rq)
446{
447}
448
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100449static inline void init_rq_hrtick(struct rq *rq)
450{
451}
452
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200453static inline void init_hrtick(void)
454{
455}
Andrew Morton006c75f2008-09-22 14:55:46 -0700456#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100457
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200458/*
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200459 * cmpxchg based fetch_or, macro so it works for different integer types
460 */
461#define fetch_or(ptr, val) \
462({ typeof(*(ptr)) __old, __val = *(ptr); \
463 for (;;) { \
464 __old = cmpxchg((ptr), __val, __val | (val)); \
465 if (__old == __val) \
466 break; \
467 __val = __old; \
468 } \
469 __old; \
470})
471
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700472#if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200473/*
474 * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
475 * this avoids any races wrt polling state changes and thereby avoids
476 * spurious IPIs.
477 */
478static bool set_nr_and_not_polling(struct task_struct *p)
479{
480 struct thread_info *ti = task_thread_info(p);
481 return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
482}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700483
484/*
485 * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
486 *
487 * If this returns true, then the idle task promises to call
488 * sched_ttwu_pending() and reschedule soon.
489 */
490static bool set_nr_if_polling(struct task_struct *p)
491{
492 struct thread_info *ti = task_thread_info(p);
Jason Low316c1608d2015-04-28 13:00:20 -0700493 typeof(ti->flags) old, val = READ_ONCE(ti->flags);
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700494
495 for (;;) {
496 if (!(val & _TIF_POLLING_NRFLAG))
497 return false;
498 if (val & _TIF_NEED_RESCHED)
499 return true;
500 old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED);
501 if (old == val)
502 break;
503 val = old;
504 }
505 return true;
506}
507
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200508#else
509static bool set_nr_and_not_polling(struct task_struct *p)
510{
511 set_tsk_need_resched(p);
512 return true;
513}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700514
515#ifdef CONFIG_SMP
516static bool set_nr_if_polling(struct task_struct *p)
517{
518 return false;
519}
520#endif
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200521#endif
522
Peter Zijlstra76751042015-05-01 08:27:50 -0700523void wake_q_add(struct wake_q_head *head, struct task_struct *task)
524{
525 struct wake_q_node *node = &task->wake_q;
526
527 /*
528 * Atomically grab the task, if ->wake_q is !nil already it means
529 * its already queued (either by us or someone else) and will get the
530 * wakeup due to that.
531 *
532 * This cmpxchg() implies a full barrier, which pairs with the write
533 * barrier implied by the wakeup in wake_up_list().
534 */
535 if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL))
536 return;
537
538 get_task_struct(task);
539
540 /*
541 * The head is context local, there can be no concurrency.
542 */
543 *head->lastp = node;
544 head->lastp = &node->next;
545}
546
547void wake_up_q(struct wake_q_head *head)
548{
549 struct wake_q_node *node = head->first;
550
551 while (node != WAKE_Q_TAIL) {
552 struct task_struct *task;
553
554 task = container_of(node, struct task_struct, wake_q);
555 BUG_ON(!task);
556 /* task can safely be re-inserted now */
557 node = node->next;
558 task->wake_q.next = NULL;
559
560 /*
561 * wake_up_process() implies a wmb() to pair with the queueing
562 * in wake_q_add() so as not to miss wakeups.
563 */
564 wake_up_process(task);
565 put_task_struct(task);
566 }
567}
568
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200569/*
Kirill Tkhai88751252014-06-29 00:03:57 +0400570 * resched_curr - mark rq's current task 'to be rescheduled now'.
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200571 *
572 * On UP this means the setting of the need_resched flag, on SMP it
573 * might also involve a cross-CPU call to trigger the scheduler on
574 * the target CPU.
575 */
Kirill Tkhai88751252014-06-29 00:03:57 +0400576void resched_curr(struct rq *rq)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200577{
Kirill Tkhai88751252014-06-29 00:03:57 +0400578 struct task_struct *curr = rq->curr;
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200579 int cpu;
580
Kirill Tkhai88751252014-06-29 00:03:57 +0400581 lockdep_assert_held(&rq->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200582
Kirill Tkhai88751252014-06-29 00:03:57 +0400583 if (test_tsk_need_resched(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200584 return;
585
Kirill Tkhai88751252014-06-29 00:03:57 +0400586 cpu = cpu_of(rq);
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200587
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200588 if (cpu == smp_processor_id()) {
Kirill Tkhai88751252014-06-29 00:03:57 +0400589 set_tsk_need_resched(curr);
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200590 set_preempt_need_resched();
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200591 return;
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200592 }
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200593
Kirill Tkhai88751252014-06-29 00:03:57 +0400594 if (set_nr_and_not_polling(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200595 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700596 else
597 trace_sched_wake_idle_without_ipi(cpu);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200598}
599
Peter Zijlstra029632f2011-10-25 10:00:11 +0200600void resched_cpu(int cpu)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200601{
602 struct rq *rq = cpu_rq(cpu);
603 unsigned long flags;
604
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100605 if (!raw_spin_trylock_irqsave(&rq->lock, flags))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200606 return;
Kirill Tkhai88751252014-06-29 00:03:57 +0400607 resched_curr(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100608 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200609}
Thomas Gleixner06d83082008-03-22 09:20:24 +0100610
Peter Zijlstrab021fe32013-09-17 09:30:55 +0200611#ifdef CONFIG_SMP
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200612#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixner06d83082008-03-22 09:20:24 +0100613/*
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700614 * In the semi idle case, use the nearest busy cpu for migrating timers
615 * from an idle cpu. This is good for power-savings.
616 *
617 * We don't do similar optimization for completely idle system, as
618 * selecting an idle cpu will add more delays to the timers than intended
619 * (as that cpu's timer base may not be uptodate wrt jiffies etc).
620 */
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000621int get_nohz_timer_target(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700622{
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000623 int i, cpu = smp_processor_id();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700624 struct sched_domain *sd;
625
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000626 if (!idle_cpu(cpu))
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530627 return cpu;
628
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200629 rcu_read_lock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700630 for_each_domain(cpu, sd) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200631 for_each_cpu(i, sched_domain_span(sd)) {
632 if (!idle_cpu(i)) {
633 cpu = i;
634 goto unlock;
635 }
636 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700637 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200638unlock:
639 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700640 return cpu;
641}
642/*
Thomas Gleixner06d83082008-03-22 09:20:24 +0100643 * When add_timer_on() enqueues a timer into the timer wheel of an
644 * idle CPU then this timer might expire before the next timer event
645 * which is scheduled to wake up that CPU. In case of a completely
646 * idle system the next event might even be infinite time into the
647 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
648 * leaves the inner idle loop so the newly added timer is taken into
649 * account when the CPU goes back to idle and evaluates the timer
650 * wheel for the next timer event.
651 */
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200652static void wake_up_idle_cpu(int cpu)
Thomas Gleixner06d83082008-03-22 09:20:24 +0100653{
654 struct rq *rq = cpu_rq(cpu);
655
656 if (cpu == smp_processor_id())
657 return;
658
Andy Lutomirski67b9ca72014-06-04 10:31:17 -0700659 if (set_nr_and_not_polling(rq->idle))
Thomas Gleixner06d83082008-03-22 09:20:24 +0100660 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700661 else
662 trace_sched_wake_idle_without_ipi(cpu);
Thomas Gleixner06d83082008-03-22 09:20:24 +0100663}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100664
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200665static bool wake_up_full_nohz_cpu(int cpu)
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200666{
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200667 /*
668 * We just need the target to call irq_exit() and re-evaluate
669 * the next tick. The nohz full kick at least implies that.
670 * If needed we can still optimize that later with an
671 * empty IRQ.
672 */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200673 if (tick_nohz_full_cpu(cpu)) {
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200674 if (cpu != smp_processor_id() ||
675 tick_nohz_tick_stopped())
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200676 tick_nohz_full_kick_cpu(cpu);
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200677 return true;
678 }
679
680 return false;
681}
682
683void wake_up_nohz_cpu(int cpu)
684{
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200685 if (!wake_up_full_nohz_cpu(cpu))
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200686 wake_up_idle_cpu(cpu);
687}
688
Suresh Siddhaca380622011-10-03 15:09:00 -0700689static inline bool got_nohz_idle_kick(void)
690{
Suresh Siddha1c792db2011-12-01 17:07:32 -0800691 int cpu = smp_processor_id();
Vincent Guittot873b4c62013-06-05 10:13:11 +0200692
693 if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)))
694 return false;
695
696 if (idle_cpu(cpu) && !need_resched())
697 return true;
698
699 /*
700 * We can't run Idle Load Balance on this CPU for this time so we
701 * cancel it and clear NOHZ_BALANCE_KICK
702 */
703 clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu));
704 return false;
Suresh Siddhaca380622011-10-03 15:09:00 -0700705}
706
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200707#else /* CONFIG_NO_HZ_COMMON */
Suresh Siddhaca380622011-10-03 15:09:00 -0700708
709static inline bool got_nohz_idle_kick(void)
710{
711 return false;
712}
713
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200714#endif /* CONFIG_NO_HZ_COMMON */
Thomas Gleixner06d83082008-03-22 09:20:24 +0100715
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200716#ifdef CONFIG_NO_HZ_FULL
717bool sched_can_stop_tick(void)
718{
Frederic Weisbecker3882ec62014-03-18 22:54:04 +0100719 /*
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500720 * FIFO realtime policy runs the highest priority task. Other runnable
721 * tasks are of a lower priority. The scheduler tick does nothing.
722 */
723 if (current->policy == SCHED_FIFO)
724 return true;
725
726 /*
727 * Round-robin realtime tasks time slice with other tasks at the same
728 * realtime priority. Is this task the only one at this priority?
729 */
730 if (current->policy == SCHED_RR) {
731 struct sched_rt_entity *rt_se = &current->rt;
732
733 return rt_se->run_list.prev == rt_se->run_list.next;
734 }
735
736 /*
Frederic Weisbecker3882ec62014-03-18 22:54:04 +0100737 * More than one running task need preemption.
738 * nr_running update is assumed to be visible
739 * after IPI is sent from wakers.
740 */
Viresh Kumar541b8262014-06-24 14:04:12 +0530741 if (this_rq()->nr_running > 1)
742 return false;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200743
Viresh Kumar541b8262014-06-24 14:04:12 +0530744 return true;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200745}
746#endif /* CONFIG_NO_HZ_FULL */
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200747
Peter Zijlstra029632f2011-10-25 10:00:11 +0200748void sched_avg_update(struct rq *rq)
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200749{
750 s64 period = sched_avg_period();
751
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200752 while ((s64)(rq_clock(rq) - rq->age_stamp) > period) {
Will Deacon0d98bb22010-05-24 12:11:43 -0700753 /*
754 * Inline assembly required to prevent the compiler
755 * optimising this loop into a divmod call.
756 * See __iter_div_u64_rem() for another example of this.
757 */
758 asm("" : "+rm" (rq->age_stamp));
Peter Zijlstrae9e92502009-09-01 10:34:37 +0200759 rq->age_stamp += period;
760 rq->rt_avg /= 2;
761 }
762}
763
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200764#endif /* CONFIG_SMP */
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200765
Paul Turnera790de92011-07-21 09:43:29 -0700766#if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
767 (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
Peter Zijlstraeb755802008-08-19 12:33:05 +0200768/*
Paul Turner82774342011-07-21 09:43:35 -0700769 * Iterate task_group tree rooted at *from, calling @down when first entering a
770 * node and @up when leaving it for the final time.
771 *
772 * Caller must hold rcu_lock or sufficient equivalent.
Peter Zijlstraeb755802008-08-19 12:33:05 +0200773 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200774int walk_tg_tree_from(struct task_group *from,
Paul Turner82774342011-07-21 09:43:35 -0700775 tg_visitor down, tg_visitor up, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200776{
777 struct task_group *parent, *child;
778 int ret;
779
Paul Turner82774342011-07-21 09:43:35 -0700780 parent = from;
781
Peter Zijlstraeb755802008-08-19 12:33:05 +0200782down:
783 ret = (*down)(parent, data);
784 if (ret)
Paul Turner82774342011-07-21 09:43:35 -0700785 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200786 list_for_each_entry_rcu(child, &parent->children, siblings) {
787 parent = child;
788 goto down;
789
790up:
791 continue;
792 }
793 ret = (*up)(parent, data);
Paul Turner82774342011-07-21 09:43:35 -0700794 if (ret || parent == from)
795 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200796
797 child = parent;
798 parent = parent->parent;
799 if (parent)
800 goto up;
Paul Turner82774342011-07-21 09:43:35 -0700801out:
Peter Zijlstraeb755802008-08-19 12:33:05 +0200802 return ret;
803}
804
Peter Zijlstra029632f2011-10-25 10:00:11 +0200805int tg_nop(struct task_group *tg, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200806{
807 return 0;
808}
809#endif
810
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200811static void set_load_weight(struct task_struct *p)
812{
Nikhil Raof05998d2011-05-18 10:09:38 -0700813 int prio = p->static_prio - MAX_RT_PRIO;
814 struct load_weight *load = &p->se.load;
815
Ingo Molnardd41f592007-07-09 18:51:59 +0200816 /*
817 * SCHED_IDLE tasks get minimal weight:
818 */
819 if (p->policy == SCHED_IDLE) {
Nikhil Raoc8b28112011-05-18 14:37:48 -0700820 load->weight = scale_load(WEIGHT_IDLEPRIO);
Nikhil Raof05998d2011-05-18 10:09:38 -0700821 load->inv_weight = WMULT_IDLEPRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +0200822 return;
823 }
824
Nikhil Raoc8b28112011-05-18 14:37:48 -0700825 load->weight = scale_load(prio_to_weight[prio]);
Nikhil Raof05998d2011-05-18 10:09:38 -0700826 load->inv_weight = prio_to_wmult[prio];
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200827}
828
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100829static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -0600830{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100831 update_rq_clock(rq);
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300832 sched_info_queued(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100833 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +0200834}
835
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100836static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +0200837{
Mike Galbraitha64692a2010-03-11 17:16:20 +0100838 update_rq_clock(rq);
Michael S. Tsirkin43148952013-09-22 17:20:54 +0300839 sched_info_dequeued(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100840 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200841}
842
Peter Zijlstra029632f2011-10-25 10:00:11 +0200843void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100844{
845 if (task_contributes_to_load(p))
846 rq->nr_uninterruptible--;
847
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100848 enqueue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100849}
850
Peter Zijlstra029632f2011-10-25 10:00:11 +0200851void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100852{
853 if (task_contributes_to_load(p))
854 rq->nr_uninterruptible++;
855
Peter Zijlstra371fd7e2010-03-24 16:38:48 +0100856 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100857}
858
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100859static void update_rq_clock_task(struct rq *rq, s64 delta)
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700860{
Glauber Costa095c0aa2011-07-11 15:28:18 -0400861/*
862 * In theory, the compile should just see 0 here, and optimize out the call
863 * to sched_rt_avg_update. But I don't trust it...
864 */
865#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
866 s64 steal = 0, irq_delta = 0;
867#endif
868#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Peter Zijlstra8e92c202010-12-09 14:15:34 +0100869 irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100870
871 /*
872 * Since irq_time is only updated on {soft,}irq_exit, we might run into
873 * this case when a previous update_rq_clock() happened inside a
874 * {soft,}irq region.
875 *
876 * When this happens, we stop ->clock_task and only update the
877 * prev_irq_time stamp to account for the part that fit, so that a next
878 * update will consume the rest. This ensures ->clock_task is
879 * monotonic.
880 *
881 * It does however cause some slight miss-attribution of {soft,}irq
882 * time, a more accurate solution would be to update the irq_time using
883 * the current rq->clock timestamp, except that would require using
884 * atomic ops.
885 */
886 if (irq_delta > delta)
887 irq_delta = delta;
888
889 rq->prev_irq_time += irq_delta;
890 delta -= irq_delta;
Glauber Costa095c0aa2011-07-11 15:28:18 -0400891#endif
892#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
Ingo Molnarc5905af2012-02-24 08:31:31 +0100893 if (static_key_false((&paravirt_steal_rq_enabled))) {
Glauber Costa095c0aa2011-07-11 15:28:18 -0400894 steal = paravirt_steal_clock(cpu_of(rq));
895 steal -= rq->prev_steal_time_rq;
896
897 if (unlikely(steal > delta))
898 steal = delta;
899
Glauber Costa095c0aa2011-07-11 15:28:18 -0400900 rq->prev_steal_time_rq += steal;
Glauber Costa095c0aa2011-07-11 15:28:18 -0400901 delta -= steal;
902 }
903#endif
904
Peter Zijlstrafe44d622010-12-09 14:15:34 +0100905 rq->clock_task += delta;
906
Glauber Costa095c0aa2011-07-11 15:28:18 -0400907#if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -0400908 if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
Glauber Costa095c0aa2011-07-11 15:28:18 -0400909 sched_rt_avg_update(rq, irq_delta + steal);
910#endif
Venkatesh Pallipadiaa483802010-10-04 17:03:22 -0700911}
912
Peter Zijlstra34f971f2010-09-22 13:53:15 +0200913void sched_set_stop_task(int cpu, struct task_struct *stop)
914{
915 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
916 struct task_struct *old_stop = cpu_rq(cpu)->stop;
917
918 if (stop) {
919 /*
920 * Make it appear like a SCHED_FIFO task, its something
921 * userspace knows about and won't get confused about.
922 *
923 * Also, it will make PI more or less work without too
924 * much confusion -- but then, stop work should not
925 * rely on PI working anyway.
926 */
927 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
928
929 stop->sched_class = &stop_sched_class;
930 }
931
932 cpu_rq(cpu)->stop = stop;
933
934 if (old_stop) {
935 /*
936 * Reset it back to a normal scheduling class so that
937 * it can die in pieces.
938 */
939 old_stop->sched_class = &rt_sched_class;
940 }
941}
942
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +0100943/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200944 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200945 */
Ingo Molnar14531182007-07-09 18:51:59 +0200946static inline int __normal_prio(struct task_struct *p)
947{
Ingo Molnardd41f592007-07-09 18:51:59 +0200948 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +0200949}
950
951/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700952 * Calculate the expected normal priority: i.e. priority
953 * without taking RT-inheritance into account. Might be
954 * boosted by interactivity modifiers. Changes upon fork,
955 * setprio syscalls, and whenever the interactivity
956 * estimator recalculates.
957 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700958static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700959{
960 int prio;
961
Dario Faggioliaab03e02013-11-28 11:14:43 +0100962 if (task_has_dl_policy(p))
963 prio = MAX_DL_PRIO-1;
964 else if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -0700965 prio = MAX_RT_PRIO-1 - p->rt_priority;
966 else
967 prio = __normal_prio(p);
968 return prio;
969}
970
971/*
972 * Calculate the current priority, i.e. the priority
973 * taken into account by the scheduler. This value might
974 * be boosted by RT tasks, or might be boosted by
975 * interactivity modifiers. Will be RT if the task got
976 * RT-boosted. If not then it returns p->normal_prio.
977 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700978static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700979{
980 p->normal_prio = normal_prio(p);
981 /*
982 * If we are RT tasks or we were boosted to RT priority,
983 * keep the priority unchanged. Otherwise, update priority
984 * to the normal priority:
985 */
986 if (!rt_prio(p->prio))
987 return p->normal_prio;
988 return p->prio;
989}
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991/**
992 * task_curr - is this task currently executing on a CPU?
993 * @p: the task in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +0200994 *
995 * Return: 1 if the task is currently executing. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700997inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 return cpu_curr(task_cpu(p)) == p;
1000}
1001
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001002/*
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001003 * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock,
1004 * use the balance_callback list if you want balancing.
1005 *
1006 * this means any call to check_class_changed() must be followed by a call to
1007 * balance_callback().
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001008 */
Steven Rostedtcb469842008-01-25 21:08:22 +01001009static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1010 const struct sched_class *prev_class,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001011 int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01001012{
1013 if (prev_class != p->sched_class) {
1014 if (prev_class->switched_from)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001015 prev_class->switched_from(rq, p);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001016
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001017 p->sched_class->switched_to(rq, p);
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001018 } else if (oldprio != p->prio || dl_task(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001019 p->sched_class->prio_changed(rq, p, oldprio);
Steven Rostedtcb469842008-01-25 21:08:22 +01001020}
1021
Peter Zijlstra029632f2011-10-25 10:00:11 +02001022void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001023{
1024 const struct sched_class *class;
1025
1026 if (p->sched_class == rq->curr->sched_class) {
1027 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
1028 } else {
1029 for_each_class(class) {
1030 if (class == rq->curr->sched_class)
1031 break;
1032 if (class == p->sched_class) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001033 resched_curr(rq);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001034 break;
1035 }
1036 }
1037 }
1038
1039 /*
1040 * A queue event has occurred, and we're going to schedule. In
1041 * this case, we can save a useless back to back clock update.
1042 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001043 if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01001044 rq_clock_skip_update(rq, true);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001045}
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047#ifdef CONFIG_SMP
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001048/*
1049 * This is how migration works:
1050 *
1051 * 1) we invoke migration_cpu_stop() on the target CPU using
1052 * stop_one_cpu().
1053 * 2) stopper starts to run (implicitly forcing the migrated thread
1054 * off the CPU)
1055 * 3) it checks whether the migrated task is still in the wrong runqueue.
1056 * 4) if it's in the wrong runqueue then the migration thread removes
1057 * it and puts it into the right queue.
1058 * 5) stopper completes and stop_one_cpu() returns and the migration
1059 * is done.
1060 */
1061
1062/*
1063 * move_queued_task - move a queued task to new rq.
1064 *
1065 * Returns (locked) new rq. Old rq's lock is released.
1066 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001067static struct rq *move_queued_task(struct rq *rq, struct task_struct *p, int new_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001068{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001069 lockdep_assert_held(&rq->lock);
1070
1071 dequeue_task(rq, p, 0);
1072 p->on_rq = TASK_ON_RQ_MIGRATING;
1073 set_task_cpu(p, new_cpu);
1074 raw_spin_unlock(&rq->lock);
1075
1076 rq = cpu_rq(new_cpu);
1077
1078 raw_spin_lock(&rq->lock);
1079 BUG_ON(task_cpu(p) != new_cpu);
1080 p->on_rq = TASK_ON_RQ_QUEUED;
1081 enqueue_task(rq, p, 0);
1082 check_preempt_curr(rq, p, 0);
1083
1084 return rq;
1085}
1086
1087struct migration_arg {
1088 struct task_struct *task;
1089 int dest_cpu;
1090};
1091
1092/*
1093 * Move (not current) task off this cpu, onto dest cpu. We're doing
1094 * this because either it can't run here any more (set_cpus_allowed()
1095 * away from this CPU, or CPU going down), or because we're
1096 * attempting to rebalance this task on exec (sched_exec).
1097 *
1098 * So we race with normal scheduler movements, but that's OK, as long
1099 * as the task is no longer on this CPU.
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001100 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001101static struct rq *__migrate_task(struct rq *rq, struct task_struct *p, int dest_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001102{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001103 if (unlikely(!cpu_active(dest_cpu)))
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001104 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001105
1106 /* Affinity changed (again). */
1107 if (!cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001108 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001109
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001110 rq = move_queued_task(rq, p, dest_cpu);
1111
1112 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001113}
1114
1115/*
1116 * migration_cpu_stop - this will be executed by a highprio stopper thread
1117 * and performs thread migration by bumping thread off CPU then
1118 * 'pushing' onto another runqueue.
1119 */
1120static int migration_cpu_stop(void *data)
1121{
1122 struct migration_arg *arg = data;
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001123 struct task_struct *p = arg->task;
1124 struct rq *rq = this_rq();
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001125
1126 /*
1127 * The original target cpu might have gone down and we might
1128 * be on another cpu but it doesn't matter.
1129 */
1130 local_irq_disable();
1131 /*
1132 * We need to explicitly wake pending tasks before running
1133 * __migrate_task() such that we will not miss enforcing cpus_allowed
1134 * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
1135 */
1136 sched_ttwu_pending();
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001137
1138 raw_spin_lock(&p->pi_lock);
1139 raw_spin_lock(&rq->lock);
1140 /*
1141 * If task_rq(p) != rq, it cannot be migrated here, because we're
1142 * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
1143 * we're holding p->pi_lock.
1144 */
1145 if (task_rq(p) == rq && task_on_rq_queued(p))
1146 rq = __migrate_task(rq, p, arg->dest_cpu);
1147 raw_spin_unlock(&rq->lock);
1148 raw_spin_unlock(&p->pi_lock);
1149
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001150 local_irq_enable();
1151 return 0;
1152}
1153
1154void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
1155{
1156 if (p->sched_class->set_cpus_allowed)
1157 p->sched_class->set_cpus_allowed(p, new_mask);
1158
1159 cpumask_copy(&p->cpus_allowed, new_mask);
1160 p->nr_cpus_allowed = cpumask_weight(new_mask);
1161}
1162
1163/*
1164 * Change a given task's CPU affinity. Migrate the thread to a
1165 * proper CPU and schedule it away if the CPU it's executing on
1166 * is removed from the allowed bitmask.
1167 *
1168 * NOTE: the caller must have a valid reference to the task, the
1169 * task must not exit() & deallocate itself prematurely. The
1170 * call is not atomic; no spinlocks may be held.
1171 */
1172int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1173{
1174 unsigned long flags;
1175 struct rq *rq;
1176 unsigned int dest_cpu;
1177 int ret = 0;
1178
1179 rq = task_rq_lock(p, &flags);
1180
1181 if (cpumask_equal(&p->cpus_allowed, new_mask))
1182 goto out;
1183
1184 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
1185 ret = -EINVAL;
1186 goto out;
1187 }
1188
1189 do_set_cpus_allowed(p, new_mask);
1190
1191 /* Can the task run on the task's current CPU? If so, we're done */
1192 if (cpumask_test_cpu(task_cpu(p), new_mask))
1193 goto out;
1194
1195 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
1196 if (task_running(rq, p) || p->state == TASK_WAKING) {
1197 struct migration_arg arg = { p, dest_cpu };
1198 /* Need help from migration thread: drop lock and wait. */
1199 task_rq_unlock(rq, p, &flags);
1200 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
1201 tlb_migrate_finish(p->mm);
1202 return 0;
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001203 } else if (task_on_rq_queued(p)) {
1204 /*
1205 * OK, since we're going to drop the lock immediately
1206 * afterwards anyway.
1207 */
1208 lockdep_unpin_lock(&rq->lock);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001209 rq = move_queued_task(rq, p, dest_cpu);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001210 lockdep_pin_lock(&rq->lock);
1211 }
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001212out:
1213 task_rq_unlock(rq, p, &flags);
1214
1215 return ret;
1216}
1217EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
1218
Ingo Molnardd41f592007-07-09 18:51:59 +02001219void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001220{
Peter Zijlstrae2912002009-12-16 18:04:36 +01001221#ifdef CONFIG_SCHED_DEBUG
1222 /*
1223 * We should never call set_task_cpu() on a blocked task,
1224 * ttwu() will sort out the placement.
1225 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01001226 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
Oleg Nesterove2336f62014-10-08 20:33:48 +02001227 !p->on_rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001228
1229#ifdef CONFIG_LOCKDEP
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001230 /*
1231 * The caller should hold either p->pi_lock or rq->lock, when changing
1232 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
1233 *
1234 * sched_move_task() holds both and thus holding either pins the cgroup,
Peter Zijlstra8323f262012-06-22 13:36:05 +02001235 * see task_group().
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001236 *
1237 * Furthermore, all task_rq users should acquire both locks, see
1238 * task_rq_lock().
1239 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001240 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
1241 lockdep_is_held(&task_rq(p)->lock)));
1242#endif
Peter Zijlstrae2912002009-12-16 18:04:36 +01001243#endif
1244
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08001245 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001246
Peter Zijlstra0c697742009-12-22 15:43:19 +01001247 if (task_cpu(p) != new_cpu) {
Paul Turner0a74bef2012-10-04 13:18:30 +02001248 if (p->sched_class->migrate_task_rq)
1249 p->sched_class->migrate_task_rq(p, new_cpu);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001250 p->se.nr_migrations++;
Peter Zijlstraff303e62015-04-17 20:05:30 +02001251 perf_event_task_migrate(p);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001252 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001253
1254 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001255}
1256
Peter Zijlstraac66f542013-10-07 11:29:16 +01001257static void __migrate_swap_task(struct task_struct *p, int cpu)
1258{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001259 if (task_on_rq_queued(p)) {
Peter Zijlstraac66f542013-10-07 11:29:16 +01001260 struct rq *src_rq, *dst_rq;
1261
1262 src_rq = task_rq(p);
1263 dst_rq = cpu_rq(cpu);
1264
1265 deactivate_task(src_rq, p, 0);
1266 set_task_cpu(p, cpu);
1267 activate_task(dst_rq, p, 0);
1268 check_preempt_curr(dst_rq, p, 0);
1269 } else {
1270 /*
1271 * Task isn't running anymore; make it appear like we migrated
1272 * it before it went to sleep. This means on wakeup we make the
1273 * previous cpu our targer instead of where it really is.
1274 */
1275 p->wake_cpu = cpu;
1276 }
1277}
1278
1279struct migration_swap_arg {
1280 struct task_struct *src_task, *dst_task;
1281 int src_cpu, dst_cpu;
1282};
1283
1284static int migrate_swap_stop(void *data)
1285{
1286 struct migration_swap_arg *arg = data;
1287 struct rq *src_rq, *dst_rq;
1288 int ret = -EAGAIN;
1289
1290 src_rq = cpu_rq(arg->src_cpu);
1291 dst_rq = cpu_rq(arg->dst_cpu);
1292
Peter Zijlstra74602312013-10-10 20:17:22 +02001293 double_raw_lock(&arg->src_task->pi_lock,
1294 &arg->dst_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001295 double_rq_lock(src_rq, dst_rq);
1296 if (task_cpu(arg->dst_task) != arg->dst_cpu)
1297 goto unlock;
1298
1299 if (task_cpu(arg->src_task) != arg->src_cpu)
1300 goto unlock;
1301
1302 if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task)))
1303 goto unlock;
1304
1305 if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task)))
1306 goto unlock;
1307
1308 __migrate_swap_task(arg->src_task, arg->dst_cpu);
1309 __migrate_swap_task(arg->dst_task, arg->src_cpu);
1310
1311 ret = 0;
1312
1313unlock:
1314 double_rq_unlock(src_rq, dst_rq);
Peter Zijlstra74602312013-10-10 20:17:22 +02001315 raw_spin_unlock(&arg->dst_task->pi_lock);
1316 raw_spin_unlock(&arg->src_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001317
1318 return ret;
1319}
1320
1321/*
1322 * Cross migrate two tasks
1323 */
1324int migrate_swap(struct task_struct *cur, struct task_struct *p)
1325{
1326 struct migration_swap_arg arg;
1327 int ret = -EINVAL;
1328
Peter Zijlstraac66f542013-10-07 11:29:16 +01001329 arg = (struct migration_swap_arg){
1330 .src_task = cur,
1331 .src_cpu = task_cpu(cur),
1332 .dst_task = p,
1333 .dst_cpu = task_cpu(p),
1334 };
1335
1336 if (arg.src_cpu == arg.dst_cpu)
1337 goto out;
1338
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02001339 /*
1340 * These three tests are all lockless; this is OK since all of them
1341 * will be re-checked with proper locks held further down the line.
1342 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01001343 if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
1344 goto out;
1345
1346 if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task)))
1347 goto out;
1348
1349 if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task)))
1350 goto out;
1351
Mel Gorman286549d2014-01-21 15:51:03 -08001352 trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001353 ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
1354
1355out:
Peter Zijlstraac66f542013-10-07 11:29:16 +01001356 return ret;
1357}
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * wait_task_inactive - wait for a thread to unschedule.
1361 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001362 * If @match_state is nonzero, it's the @p->state value just checked and
1363 * not expected to change. If it changes, i.e. @p might have woken up,
1364 * then return zero. When we succeed in waiting for @p to be off its CPU,
1365 * we return a positive number (its total switch count). If a second call
1366 * a short while later returns the same number, the caller can be sure that
1367 * @p has remained unscheduled the whole time.
1368 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * The caller must ensure that the task *will* unschedule sometime soon,
1370 * else this function might spin for a *long* time. This function can't
1371 * be called with interrupts off, or it may introduce deadlock with
1372 * smp_call_function() if an IPI is sent by the same process we are
1373 * waiting to become inactive.
1374 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001375unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001378 int running, queued;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001379 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001380 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Andi Kleen3a5c3592007-10-15 17:00:14 +02001382 for (;;) {
1383 /*
1384 * We do the initial early heuristics without holding
1385 * any task-queue locks at all. We'll only try to get
1386 * the runqueue lock when things look like they will
1387 * work out!
1388 */
1389 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001390
Andi Kleen3a5c3592007-10-15 17:00:14 +02001391 /*
1392 * If the task is actively running on another CPU
1393 * still, just relax and busy-wait without holding
1394 * any locks.
1395 *
1396 * NOTE! Since we don't hold any locks, it's not
1397 * even sure that "rq" stays as the right runqueue!
1398 * But we don't care, since "task_running()" will
1399 * return false if the runqueue has changed and p
1400 * is actually now running somewhere else!
1401 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001402 while (task_running(rq, p)) {
1403 if (match_state && unlikely(p->state != match_state))
1404 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02001405 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07001406 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001407
Andi Kleen3a5c3592007-10-15 17:00:14 +02001408 /*
1409 * Ok, time to look more closely! We need the rq
1410 * lock now, to be *sure*. If we're wrong, we'll
1411 * just go back and repeat.
1412 */
1413 rq = task_rq_lock(p, &flags);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02001414 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001415 running = task_running(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001416 queued = task_on_rq_queued(p);
Roland McGrath85ba2d82008-07-25 19:45:58 -07001417 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07001418 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07001419 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001420 task_rq_unlock(rq, p, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001421
Andi Kleen3a5c3592007-10-15 17:00:14 +02001422 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07001423 * If it changed from the expected state, bail out now.
1424 */
1425 if (unlikely(!ncsw))
1426 break;
1427
1428 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02001429 * Was it really running after all now that we
1430 * checked with the proper locks actually held?
1431 *
1432 * Oops. Go back and try again..
1433 */
1434 if (unlikely(running)) {
1435 cpu_relax();
1436 continue;
1437 }
1438
1439 /*
1440 * It's not enough that it's not actively running,
1441 * it must be off the runqueue _entirely_, and not
1442 * preempted!
1443 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00001444 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02001445 * running right now), it's preempted, and we should
1446 * yield - it could be a while.
1447 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001448 if (unlikely(queued)) {
Thomas Gleixner8eb90c32011-02-23 23:52:21 +00001449 ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
1450
1451 set_current_state(TASK_UNINTERRUPTIBLE);
1452 schedule_hrtimeout(&to, HRTIMER_MODE_REL);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001453 continue;
1454 }
1455
1456 /*
1457 * Ahh, all good. It wasn't running, and it wasn't
1458 * runnable, which means that it will never become
1459 * running in the future either. We're all done!
1460 */
1461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07001463
1464 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467/***
1468 * kick_process - kick a running thread to enter/exit the kernel
1469 * @p: the to-be-kicked thread
1470 *
1471 * Cause a process which is running on another CPU to enter
1472 * kernel-mode, without any delay. (to get signals handled.)
1473 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001474 * NOTE: this function doesn't have to take the runqueue lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * because all it wants to ensure is that the remote task enters
1476 * the kernel. If the IPI races and the task has been migrated
1477 * to another CPU then no harm is done and the purpose has been
1478 * achieved as well.
1479 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001480void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 int cpu;
1483
1484 preempt_disable();
1485 cpu = task_cpu(p);
1486 if ((cpu != smp_processor_id()) && task_curr(p))
1487 smp_send_reschedule(cpu);
1488 preempt_enable();
1489}
Rusty Russellb43e3522009-06-12 22:27:00 -06001490EXPORT_SYMBOL_GPL(kick_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Oleg Nesterov30da6882010-03-15 10:10:19 +01001492/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001493 * ->cpus_allowed is protected by both rq->lock and p->pi_lock
Oleg Nesterov30da6882010-03-15 10:10:19 +01001494 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001495static int select_fallback_rq(int cpu, struct task_struct *p)
1496{
Tang Chenaa00d892013-02-22 16:33:33 -08001497 int nid = cpu_to_node(cpu);
1498 const struct cpumask *nodemask = NULL;
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001499 enum { cpuset, possible, fail } state = cpuset;
1500 int dest_cpu;
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001501
Tang Chenaa00d892013-02-22 16:33:33 -08001502 /*
1503 * If the node that the cpu is on has been offlined, cpu_to_node()
1504 * will return -1. There is no cpu on the node, and we should
1505 * select the cpu on the other node.
1506 */
1507 if (nid != -1) {
1508 nodemask = cpumask_of_node(nid);
1509
1510 /* Look for allowed, online CPU in same node. */
1511 for_each_cpu(dest_cpu, nodemask) {
1512 if (!cpu_online(dest_cpu))
1513 continue;
1514 if (!cpu_active(dest_cpu))
1515 continue;
1516 if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
1517 return dest_cpu;
1518 }
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001519 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001520
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001521 for (;;) {
1522 /* Any allowed, online CPU? */
Srivatsa S. Bhate3831ed2012-03-30 19:40:28 +05301523 for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) {
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001524 if (!cpu_online(dest_cpu))
1525 continue;
1526 if (!cpu_active(dest_cpu))
1527 continue;
1528 goto out;
1529 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001530
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001531 switch (state) {
1532 case cpuset:
1533 /* No more Mr. Nice Guy. */
1534 cpuset_cpus_allowed_fallback(p);
1535 state = possible;
1536 break;
1537
1538 case possible:
1539 do_set_cpus_allowed(p, cpu_possible_mask);
1540 state = fail;
1541 break;
1542
1543 case fail:
1544 BUG();
1545 break;
1546 }
1547 }
1548
1549out:
1550 if (state != cpuset) {
1551 /*
1552 * Don't tell them about moving exiting tasks or
1553 * kernel threads (both mm NULL), since they never
1554 * leave kernel.
1555 */
1556 if (p->mm && printk_ratelimit()) {
John Stultzaac74dc2014-06-04 16:11:40 -07001557 printk_deferred("process %d (%s) no longer affine to cpu%d\n",
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001558 task_pid_nr(p), p->comm, cpu);
1559 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001560 }
1561
1562 return dest_cpu;
1563}
1564
Peter Zijlstrae2912002009-12-16 18:04:36 +01001565/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001566 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01001567 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001568static inline
Peter Zijlstraac66f542013-10-07 11:29:16 +01001569int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001570{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001571 lockdep_assert_held(&p->pi_lock);
1572
Wanpeng Li6c1d9412014-11-05 09:14:37 +08001573 if (p->nr_cpus_allowed > 1)
1574 cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01001575
1576 /*
1577 * In order not to call set_task_cpu() on a blocking task we need
1578 * to rely on ttwu() to place the task on a valid ->cpus_allowed
1579 * cpu.
1580 *
1581 * Since this is common to all placement strategies, this lives here.
1582 *
1583 * [ this allows ->select_task() to simply return task_cpu(p) and
1584 * not worry about this generic constraint ]
1585 */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001586 if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01001587 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001588 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01001589
1590 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001591}
Mike Galbraith09a40af2010-04-15 07:29:59 +02001592
1593static void update_avg(u64 *avg, u64 sample)
1594{
1595 s64 diff = sample - *avg;
1596 *avg += diff >> 3;
1597}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001598#endif /* CONFIG_SMP */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001599
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001600static void
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001601ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001602{
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001603#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001604 struct rq *rq = this_rq();
Tejun Heo9ed38112009-12-03 15:08:03 +09001605
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001606#ifdef CONFIG_SMP
1607 int this_cpu = smp_processor_id();
Tejun Heo9ed38112009-12-03 15:08:03 +09001608
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001609 if (cpu == this_cpu) {
1610 schedstat_inc(rq, ttwu_local);
1611 schedstat_inc(p, se.statistics.nr_wakeups_local);
1612 } else {
1613 struct sched_domain *sd;
1614
1615 schedstat_inc(p, se.statistics.nr_wakeups_remote);
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001616 rcu_read_lock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001617 for_each_domain(this_cpu, sd) {
1618 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
1619 schedstat_inc(sd, ttwu_wake_remote);
1620 break;
1621 }
1622 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001623 rcu_read_unlock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001624 }
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001625
1626 if (wake_flags & WF_MIGRATED)
1627 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
1628
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001629#endif /* CONFIG_SMP */
1630
1631 schedstat_inc(rq, ttwu_count);
1632 schedstat_inc(p, se.statistics.nr_wakeups);
1633
1634 if (wake_flags & WF_SYNC)
1635 schedstat_inc(p, se.statistics.nr_wakeups_sync);
1636
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001637#endif /* CONFIG_SCHEDSTATS */
Tejun Heo9ed38112009-12-03 15:08:03 +09001638}
1639
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001640static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001641{
Tejun Heo9ed38112009-12-03 15:08:03 +09001642 activate_task(rq, p, en_flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001643 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrac2f71152011-04-13 13:28:56 +02001644
1645 /* if a worker is waking up, notify workqueue */
1646 if (p->flags & PF_WQ_WORKER)
1647 wq_worker_waking_up(p, cpu_of(rq));
Tejun Heo9ed38112009-12-03 15:08:03 +09001648}
1649
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001650/*
1651 * Mark the task runnable and perform wakeup-preemption.
1652 */
Peter Zijlstra89363382011-04-05 17:23:42 +02001653static void
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001654ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001655{
Tejun Heo9ed38112009-12-03 15:08:03 +09001656 check_preempt_curr(rq, p, wake_flags);
Peter Zijlstraa8d7ad52013-03-14 10:48:39 +01001657 trace_sched_wakeup(p, true);
Tejun Heo9ed38112009-12-03 15:08:03 +09001658
1659 p->state = TASK_RUNNING;
1660#ifdef CONFIG_SMP
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001661 if (p->sched_class->task_woken) {
1662 /*
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001663 * Our task @p is fully woken up and running; so its safe to
1664 * drop the rq->lock, hereafter rq is only used for statistics.
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001665 */
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001666 lockdep_unpin_lock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09001667 p->sched_class->task_woken(rq, p);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001668 lockdep_pin_lock(&rq->lock);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001669 }
Tejun Heo9ed38112009-12-03 15:08:03 +09001670
Steven Rostedte69c6342010-12-06 17:10:31 -05001671 if (rq->idle_stamp) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001672 u64 delta = rq_clock(rq) - rq->idle_stamp;
Jason Low9bd721c2013-09-13 11:26:52 -07001673 u64 max = 2*rq->max_idle_balance_cost;
Tejun Heo9ed38112009-12-03 15:08:03 +09001674
Jason Lowabfafa52013-09-13 11:26:51 -07001675 update_avg(&rq->avg_idle, delta);
1676
1677 if (rq->avg_idle > max)
Tejun Heo9ed38112009-12-03 15:08:03 +09001678 rq->avg_idle = max;
Jason Lowabfafa52013-09-13 11:26:51 -07001679
Tejun Heo9ed38112009-12-03 15:08:03 +09001680 rq->idle_stamp = 0;
1681 }
1682#endif
1683}
1684
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001685static void
1686ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)
1687{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001688 lockdep_assert_held(&rq->lock);
1689
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001690#ifdef CONFIG_SMP
1691 if (p->sched_contributes_to_load)
1692 rq->nr_uninterruptible--;
1693#endif
1694
1695 ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);
1696 ttwu_do_wakeup(rq, p, wake_flags);
1697}
1698
1699/*
1700 * Called in case the task @p isn't fully descheduled from its runqueue,
1701 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
1702 * since all we need to do is flip p->state to TASK_RUNNING, since
1703 * the task is still ->on_rq.
1704 */
1705static int ttwu_remote(struct task_struct *p, int wake_flags)
1706{
1707 struct rq *rq;
1708 int ret = 0;
1709
1710 rq = __task_rq_lock(p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001711 if (task_on_rq_queued(p)) {
Frederic Weisbecker1ad4ec02013-04-12 01:51:00 +02001712 /* check_preempt_curr() may use rq clock */
1713 update_rq_clock(rq);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001714 ttwu_do_wakeup(rq, p, wake_flags);
1715 ret = 1;
1716 }
1717 __task_rq_unlock(rq);
1718
1719 return ret;
1720}
1721
Peter Zijlstra317f3942011-04-05 17:23:58 +02001722#ifdef CONFIG_SMP
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001723void sched_ttwu_pending(void)
Peter Zijlstra317f3942011-04-05 17:23:58 +02001724{
1725 struct rq *rq = this_rq();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001726 struct llist_node *llist = llist_del_all(&rq->wake_list);
1727 struct task_struct *p;
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001728 unsigned long flags;
Peter Zijlstra317f3942011-04-05 17:23:58 +02001729
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001730 if (!llist)
1731 return;
1732
1733 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001734 lockdep_pin_lock(&rq->lock);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001735
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001736 while (llist) {
1737 p = llist_entry(llist, struct task_struct, wake_entry);
1738 llist = llist_next(llist);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001739 ttwu_do_activate(rq, p, 0);
1740 }
1741
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001742 lockdep_unpin_lock(&rq->lock);
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001743 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001744}
1745
1746void scheduler_ipi(void)
1747{
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001748 /*
1749 * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
1750 * TIF_NEED_RESCHED remotely (for the first time) will also send
1751 * this IPI.
1752 */
Peter Zijlstra8cb75e02013-11-20 12:22:37 +01001753 preempt_fold_need_resched();
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001754
Frederic Weisbeckerfd2ac4f2014-03-18 21:12:53 +01001755 if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001756 return;
1757
1758 /*
1759 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
1760 * traditionally all their work was done from the interrupt return
1761 * path. Now that we actually do some work, we need to make sure
1762 * we do call them.
1763 *
1764 * Some archs already do call them, luckily irq_enter/exit nest
1765 * properly.
1766 *
1767 * Arguably we should visit all archs and update all handlers,
1768 * however a fair share of IPIs are still resched only so this would
1769 * somewhat pessimize the simple resched case.
1770 */
1771 irq_enter();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001772 sched_ttwu_pending();
Suresh Siddhaca380622011-10-03 15:09:00 -07001773
1774 /*
1775 * Check if someone kicked us for doing the nohz idle load balance.
1776 */
Vincent Guittot873b4c62013-06-05 10:13:11 +02001777 if (unlikely(got_nohz_idle_kick())) {
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001778 this_rq()->idle_balance = 1;
Suresh Siddhaca380622011-10-03 15:09:00 -07001779 raise_softirq_irqoff(SCHED_SOFTIRQ);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001780 }
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001781 irq_exit();
Peter Zijlstra317f3942011-04-05 17:23:58 +02001782}
1783
1784static void ttwu_queue_remote(struct task_struct *p, int cpu)
1785{
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001786 struct rq *rq = cpu_rq(cpu);
1787
1788 if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) {
1789 if (!set_nr_if_polling(rq->idle))
1790 smp_send_reschedule(cpu);
1791 else
1792 trace_sched_wake_idle_without_ipi(cpu);
1793 }
Peter Zijlstra317f3942011-04-05 17:23:58 +02001794}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001795
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001796void wake_up_if_idle(int cpu)
1797{
1798 struct rq *rq = cpu_rq(cpu);
1799 unsigned long flags;
1800
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001801 rcu_read_lock();
1802
1803 if (!is_idle_task(rcu_dereference(rq->curr)))
1804 goto out;
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001805
1806 if (set_nr_if_polling(rq->idle)) {
1807 trace_sched_wake_idle_without_ipi(cpu);
1808 } else {
1809 raw_spin_lock_irqsave(&rq->lock, flags);
1810 if (is_idle_task(rq->curr))
1811 smp_send_reschedule(cpu);
1812 /* Else cpu is not in idle, do nothing here */
1813 raw_spin_unlock_irqrestore(&rq->lock, flags);
1814 }
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001815
1816out:
1817 rcu_read_unlock();
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001818}
1819
Peter Zijlstra39be3502012-01-26 12:44:34 +01001820bool cpus_share_cache(int this_cpu, int that_cpu)
Peter Zijlstra518cd622011-12-07 15:07:31 +01001821{
1822 return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
1823}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001824#endif /* CONFIG_SMP */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001825
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001826static void ttwu_queue(struct task_struct *p, int cpu)
1827{
1828 struct rq *rq = cpu_rq(cpu);
1829
Daniel Hellstrom17d9f312011-05-20 04:01:10 +00001830#if defined(CONFIG_SMP)
Peter Zijlstra39be3502012-01-26 12:44:34 +01001831 if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
Peter Zijlstraf01114c2011-05-31 12:26:55 +02001832 sched_clock_cpu(cpu); /* sync clocks x-cpu */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001833 ttwu_queue_remote(p, cpu);
1834 return;
1835 }
1836#endif
1837
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001838 raw_spin_lock(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001839 lockdep_pin_lock(&rq->lock);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001840 ttwu_do_activate(rq, p, 0);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001841 lockdep_unpin_lock(&rq->lock);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001842 raw_spin_unlock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09001843}
1844
1845/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09001847 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09001849 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 *
1851 * Put it on the run-queue if it's not already there. The "current"
1852 * thread is always on the run-queue (except when the actual
1853 * re-schedule is in progress), and as such you're allowed to do
1854 * the simpler "current->state = TASK_RUNNING" to mark yourself
1855 * runnable without the overhead of this.
1856 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02001857 * Return: %true if @p was woken up, %false if it was already running.
Tejun Heo9ed38112009-12-03 15:08:03 +09001858 * or @state didn't match @p's state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001860static int
1861try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 unsigned long flags;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001864 int cpu, success = 0;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001865
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02001866 /*
1867 * If we are going to wake up a thread waiting for CONDITION we
1868 * need to ensure that CONDITION=1 done by the caller can not be
1869 * reordered with p->state check below. This pairs with mb() in
1870 * set_current_state() the waiting thread does.
1871 */
1872 smp_mb__before_spinlock();
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001873 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001874 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 goto out;
1876
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001877 success = 1; /* we're going to change ->state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 cpu = task_cpu(p);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001879
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001880 if (p->on_rq && ttwu_remote(p, wake_flags))
1881 goto stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883#ifdef CONFIG_SMP
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001884 /*
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001885 * If the owning (remote) cpu is still in the middle of schedule() with
1886 * this task as prev, wait until its done referencing the task.
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001887 */
Peter Zijlstraf3e94782012-09-12 11:22:00 +02001888 while (p->on_cpu)
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001889 cpu_relax();
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001890 /*
1891 * Pairs with the smp_wmb() in finish_lock_switch().
1892 */
1893 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Peter Zijlstraa8e4f2e2011-04-05 17:23:49 +02001895 p->sched_contributes_to_load = !!task_contributes_to_load(p);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001896 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01001897
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001898 if (p->sched_class->task_waking)
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02001899 p->sched_class->task_waking(p);
Peter Zijlstraab19cb22009-11-27 15:44:43 +01001900
Peter Zijlstraac66f542013-10-07 11:29:16 +01001901 cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001902 if (task_cpu(p) != cpu) {
1903 wake_flags |= WF_MIGRATED;
Mike Galbraithf5dc3752009-10-09 08:35:03 +02001904 set_task_cpu(p, cpu);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001908 ttwu_queue(p, cpu);
1909stat:
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001910 ttwu_stat(p, cpu, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911out:
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001912 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 return success;
1915}
1916
David Howells50fa6102009-04-28 15:01:38 +01001917/**
Tejun Heo21aa9af2010-06-08 21:40:37 +02001918 * try_to_wake_up_local - try to wake up a local task with rq lock held
1919 * @p: the thread to be awakened
1920 *
Peter Zijlstra2acca552011-04-05 17:23:50 +02001921 * Put @p on the run-queue if it's not already there. The caller must
Tejun Heo21aa9af2010-06-08 21:40:37 +02001922 * ensure that this_rq() is locked, @p is bound to this_rq() and not
Peter Zijlstra2acca552011-04-05 17:23:50 +02001923 * the current task.
Tejun Heo21aa9af2010-06-08 21:40:37 +02001924 */
1925static void try_to_wake_up_local(struct task_struct *p)
1926{
1927 struct rq *rq = task_rq(p);
Tejun Heo21aa9af2010-06-08 21:40:37 +02001928
Tejun Heo383efcd2013-03-18 12:22:34 -07001929 if (WARN_ON_ONCE(rq != this_rq()) ||
1930 WARN_ON_ONCE(p == current))
1931 return;
1932
Tejun Heo21aa9af2010-06-08 21:40:37 +02001933 lockdep_assert_held(&rq->lock);
1934
Peter Zijlstra2acca552011-04-05 17:23:50 +02001935 if (!raw_spin_trylock(&p->pi_lock)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001936 /*
1937 * This is OK, because current is on_cpu, which avoids it being
1938 * picked for load-balance and preemption/IRQs are still
1939 * disabled avoiding further scheduler activity on it and we've
1940 * not yet picked a replacement task.
1941 */
1942 lockdep_unpin_lock(&rq->lock);
Peter Zijlstra2acca552011-04-05 17:23:50 +02001943 raw_spin_unlock(&rq->lock);
1944 raw_spin_lock(&p->pi_lock);
1945 raw_spin_lock(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001946 lockdep_pin_lock(&rq->lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02001947 }
Peter Zijlstra2acca552011-04-05 17:23:50 +02001948
Tejun Heo21aa9af2010-06-08 21:40:37 +02001949 if (!(p->state & TASK_NORMAL))
Peter Zijlstra2acca552011-04-05 17:23:50 +02001950 goto out;
Tejun Heo21aa9af2010-06-08 21:40:37 +02001951
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001952 if (!task_on_rq_queued(p))
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001953 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
1954
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001955 ttwu_do_wakeup(rq, p, 0);
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001956 ttwu_stat(p, smp_processor_id(), 0);
Peter Zijlstra2acca552011-04-05 17:23:50 +02001957out:
1958 raw_spin_unlock(&p->pi_lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02001959}
1960
1961/**
David Howells50fa6102009-04-28 15:01:38 +01001962 * wake_up_process - Wake up a specific process
1963 * @p: The process to be woken up.
1964 *
1965 * Attempt to wake up the nominated process and move it to the set of runnable
Yacine Belkadie69f6182013-07-12 20:45:47 +02001966 * processes.
1967 *
1968 * Return: 1 if the process was woken up, 0 if it was already running.
David Howells50fa6102009-04-28 15:01:38 +01001969 *
1970 * It may be assumed that this function implies a write memory barrier before
1971 * changing the task state if and only if any tasks are woken up.
1972 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001973int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Oleg Nesterov9067ac82013-01-21 20:48:17 +01001975 WARN_ON(task_is_stopped_or_traced(p));
1976 return try_to_wake_up(p, TASK_NORMAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978EXPORT_SYMBOL(wake_up_process);
1979
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001980int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 return try_to_wake_up(p, state, 0);
1983}
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985/*
Juri Lellia5e7be32014-09-19 10:22:39 +01001986 * This function clears the sched_dl_entity static params.
1987 */
1988void __dl_clear_params(struct task_struct *p)
1989{
1990 struct sched_dl_entity *dl_se = &p->dl;
1991
1992 dl_se->dl_runtime = 0;
1993 dl_se->dl_deadline = 0;
1994 dl_se->dl_period = 0;
1995 dl_se->flags = 0;
1996 dl_se->dl_bw = 0;
Peter Zijlstra40767b02015-01-28 15:08:03 +01001997
1998 dl_se->dl_throttled = 0;
1999 dl_se->dl_new = 1;
2000 dl_se->dl_yielded = 0;
Juri Lellia5e7be32014-09-19 10:22:39 +01002001}
2002
2003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 * Perform scheduler related setup for a newly forked process p.
2005 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002006 *
2007 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 */
Rik van Riel5e1576e2013-10-07 11:29:26 +01002009static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002011 p->on_rq = 0;
2012
2013 p->se.on_rq = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002014 p->se.exec_start = 0;
2015 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002016 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002017 p->se.nr_migrations = 0;
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002018 p->se.vruntime = 0;
Kirill Tkhaibb041592014-12-15 14:56:58 +03002019#ifdef CONFIG_SMP
2020 p->se.avg.decay_count = 0;
2021#endif
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002022 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002023
2024#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03002025 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002026#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002027
Dario Faggioliaab03e02013-11-28 11:14:43 +01002028 RB_CLEAR_NODE(&p->dl.rb_node);
Peter Zijlstra40767b02015-01-28 15:08:03 +01002029 init_dl_task_timer(&p->dl);
Juri Lellia5e7be32014-09-19 10:22:39 +01002030 __dl_clear_params(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002031
Peter Zijlstrafa717062008-01-25 21:08:27 +01002032 INIT_LIST_HEAD(&p->rt.run_list);
Nick Piggin476d1392005-06-25 14:57:29 -07002033
Avi Kivitye107be32007-07-26 13:40:43 +02002034#ifdef CONFIG_PREEMPT_NOTIFIERS
2035 INIT_HLIST_HEAD(&p->preempt_notifiers);
2036#endif
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002037
2038#ifdef CONFIG_NUMA_BALANCING
2039 if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002040 p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002041 p->mm->numa_scan_seq = 0;
2042 }
2043
Rik van Riel5e1576e2013-10-07 11:29:26 +01002044 if (clone_flags & CLONE_VM)
2045 p->numa_preferred_nid = current->numa_preferred_nid;
2046 else
2047 p->numa_preferred_nid = -1;
2048
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002049 p->node_stamp = 0ULL;
2050 p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
Peter Zijlstra4b96a292012-10-25 14:16:47 +02002051 p->numa_scan_period = sysctl_numa_balancing_scan_delay;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002052 p->numa_work.next = &p->numa_work;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002053 p->numa_faults = NULL;
Rik van Riel7e2703e2014-01-27 17:03:45 -05002054 p->last_task_numa_placement = 0;
2055 p->last_sum_exec_runtime = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002056
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002057 p->numa_group = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002058#endif /* CONFIG_NUMA_BALANCING */
Ingo Molnardd41f592007-07-09 18:51:59 +02002059}
2060
Mel Gorman1a687c22012-11-22 11:16:36 +00002061#ifdef CONFIG_NUMA_BALANCING
Mel Gorman3105b862012-11-23 11:23:49 +00002062#ifdef CONFIG_SCHED_DEBUG
Mel Gorman1a687c22012-11-22 11:16:36 +00002063void set_numabalancing_state(bool enabled)
2064{
2065 if (enabled)
2066 sched_feat_set("NUMA");
2067 else
2068 sched_feat_set("NO_NUMA");
2069}
Mel Gorman3105b862012-11-23 11:23:49 +00002070#else
2071__read_mostly bool numabalancing_enabled;
2072
2073void set_numabalancing_state(bool enabled)
2074{
2075 numabalancing_enabled = enabled;
2076}
2077#endif /* CONFIG_SCHED_DEBUG */
Andi Kleen54a43d52014-01-23 15:53:13 -08002078
2079#ifdef CONFIG_PROC_SYSCTL
2080int sysctl_numa_balancing(struct ctl_table *table, int write,
2081 void __user *buffer, size_t *lenp, loff_t *ppos)
2082{
2083 struct ctl_table t;
2084 int err;
2085 int state = numabalancing_enabled;
2086
2087 if (write && !capable(CAP_SYS_ADMIN))
2088 return -EPERM;
2089
2090 t = *table;
2091 t.data = &state;
2092 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2093 if (err < 0)
2094 return err;
2095 if (write)
2096 set_numabalancing_state(state);
2097 return err;
2098}
2099#endif
2100#endif
Mel Gorman1a687c22012-11-22 11:16:36 +00002101
Ingo Molnardd41f592007-07-09 18:51:59 +02002102/*
2103 * fork()/clone()-time setup:
2104 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002105int sched_fork(unsigned long clone_flags, struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02002106{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002107 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002108 int cpu = get_cpu();
2109
Rik van Riel5e1576e2013-10-07 11:29:26 +01002110 __sched_fork(clone_flags, p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002111 /*
Peter Zijlstra0017d732010-03-24 18:34:10 +01002112 * We mark the process as running here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002113 * nobody will actually run it, and a signal or other external
2114 * event cannot wake it up and insert it on the runqueue either.
2115 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002116 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002117
Ingo Molnarb29739f2006-06-27 02:54:51 -07002118 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02002119 * Make sure we do not leak PI boosting priority to the child.
2120 */
2121 p->prio = current->normal_prio;
2122
2123 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002124 * Revert to default priority/policy on fork if requested.
2125 */
2126 if (unlikely(p->sched_reset_on_fork)) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01002127 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002128 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002129 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02002130 p->rt_priority = 0;
2131 } else if (PRIO_TO_NICE(p->static_prio) < 0)
2132 p->static_prio = NICE_TO_PRIO(0);
2133
2134 p->prio = p->normal_prio = __normal_prio(p);
2135 set_load_weight(p);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002136
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002137 /*
2138 * We don't need the reset flag anymore after the fork. It has
2139 * fulfilled its duty:
2140 */
2141 p->sched_reset_on_fork = 0;
2142 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02002143
Dario Faggioliaab03e02013-11-28 11:14:43 +01002144 if (dl_prio(p->prio)) {
2145 put_cpu();
2146 return -EAGAIN;
2147 } else if (rt_prio(p->prio)) {
2148 p->sched_class = &rt_sched_class;
2149 } else {
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002150 p->sched_class = &fair_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01002151 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07002152
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002153 if (p->sched_class->task_fork)
2154 p->sched_class->task_fork(p);
2155
Peter Zijlstra86951592010-06-22 11:44:53 +02002156 /*
2157 * The child is not yet in the pid-hash so no cgroup attach races,
2158 * and the cgroup is pinned to this child due to cgroup_fork()
2159 * is ran before sched_fork().
2160 *
2161 * Silence PROVE_RCU.
2162 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002163 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002164 set_task_cpu(p, cpu);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002165 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002166
Naveen N. Raof6db8342015-06-25 23:53:37 +05302167#ifdef CONFIG_SCHED_INFO
Ingo Molnardd41f592007-07-09 18:51:59 +02002168 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002169 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02002171#if defined(CONFIG_SMP)
2172 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07002173#endif
Peter Zijlstra01028742013-08-14 14:55:46 +02002174 init_task_preempt_count(p);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002175#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05002176 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002177 RB_CLEAR_NODE(&p->pushable_dl_tasks);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002178#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002179
Nick Piggin476d1392005-06-25 14:57:29 -07002180 put_cpu();
Dario Faggioliaab03e02013-11-28 11:14:43 +01002181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Dario Faggioli332ac172013-11-07 14:43:45 +01002184unsigned long to_ratio(u64 period, u64 runtime)
2185{
2186 if (runtime == RUNTIME_INF)
2187 return 1ULL << 20;
2188
2189 /*
2190 * Doing this here saves a lot of checks in all
2191 * the calling paths, and returning zero seems
2192 * safe for them anyway.
2193 */
2194 if (period == 0)
2195 return 0;
2196
2197 return div64_u64(runtime << 20, period);
2198}
2199
2200#ifdef CONFIG_SMP
2201inline struct dl_bw *dl_bw_of(int i)
2202{
Kirill Tkhai66339c32014-09-22 22:36:24 +04002203 rcu_lockdep_assert(rcu_read_lock_sched_held(),
2204 "sched RCU must be held");
Dario Faggioli332ac172013-11-07 14:43:45 +01002205 return &cpu_rq(i)->rd->dl_bw;
2206}
2207
Peter Zijlstrade212f12013-12-19 11:54:45 +01002208static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002209{
Peter Zijlstrade212f12013-12-19 11:54:45 +01002210 struct root_domain *rd = cpu_rq(i)->rd;
2211 int cpus = 0;
2212
Kirill Tkhai66339c32014-09-22 22:36:24 +04002213 rcu_lockdep_assert(rcu_read_lock_sched_held(),
2214 "sched RCU must be held");
Peter Zijlstrade212f12013-12-19 11:54:45 +01002215 for_each_cpu_and(i, rd->span, cpu_active_mask)
2216 cpus++;
2217
2218 return cpus;
Dario Faggioli332ac172013-11-07 14:43:45 +01002219}
2220#else
2221inline struct dl_bw *dl_bw_of(int i)
2222{
2223 return &cpu_rq(i)->dl.dl_bw;
2224}
2225
Peter Zijlstrade212f12013-12-19 11:54:45 +01002226static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002227{
2228 return 1;
2229}
2230#endif
2231
Dario Faggioli332ac172013-11-07 14:43:45 +01002232/*
2233 * We must be sure that accepting a new task (or allowing changing the
2234 * parameters of an existing one) is consistent with the bandwidth
2235 * constraints. If yes, this function also accordingly updates the currently
2236 * allocated bandwidth to reflect the new situation.
2237 *
2238 * This function is called while holding p's rq->lock.
Peter Zijlstra40767b02015-01-28 15:08:03 +01002239 *
2240 * XXX we should delay bw change until the task's 0-lag point, see
2241 * __setparam_dl().
Dario Faggioli332ac172013-11-07 14:43:45 +01002242 */
2243static int dl_overflow(struct task_struct *p, int policy,
2244 const struct sched_attr *attr)
2245{
2246
2247 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
Steven Rostedt4df16382014-02-19 13:53:35 -05002248 u64 period = attr->sched_period ?: attr->sched_deadline;
Dario Faggioli332ac172013-11-07 14:43:45 +01002249 u64 runtime = attr->sched_runtime;
2250 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
Peter Zijlstrade212f12013-12-19 11:54:45 +01002251 int cpus, err = -1;
Dario Faggioli332ac172013-11-07 14:43:45 +01002252
2253 if (new_bw == p->dl.dl_bw)
2254 return 0;
2255
2256 /*
2257 * Either if a task, enters, leave, or stays -deadline but changes
2258 * its parameters, we may need to update accordingly the total
2259 * allocated bandwidth of the container.
2260 */
2261 raw_spin_lock(&dl_b->lock);
Peter Zijlstrade212f12013-12-19 11:54:45 +01002262 cpus = dl_bw_cpus(task_cpu(p));
Dario Faggioli332ac172013-11-07 14:43:45 +01002263 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2264 !__dl_overflow(dl_b, cpus, 0, new_bw)) {
2265 __dl_add(dl_b, new_bw);
2266 err = 0;
2267 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2268 !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
2269 __dl_clear(dl_b, p->dl.dl_bw);
2270 __dl_add(dl_b, new_bw);
2271 err = 0;
2272 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2273 __dl_clear(dl_b, p->dl.dl_bw);
2274 err = 0;
2275 }
2276 raw_spin_unlock(&dl_b->lock);
2277
2278 return err;
2279}
2280
2281extern void init_dl_bw(struct dl_bw *dl_b);
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283/*
2284 * wake_up_new_task - wake up a newly created task for the first time.
2285 *
2286 * This function will do some initial scheduler statistics housekeeping
2287 * that must be done for every newly created context, then puts the task
2288 * on the runqueue and wakes it.
2289 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002290void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002293 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002294
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002295 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002296#ifdef CONFIG_SMP
2297 /*
2298 * Fork balancing, do it here and not earlier because:
2299 * - cpus_allowed can change in the fork path
2300 * - any previously selected cpu might disappear through hotplug
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002301 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01002302 set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002303#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Alex Shia75cdaa2013-06-20 10:18:47 +08002305 /* Initialize new task's runnable average */
2306 init_task_runnable_average(p);
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002307 rq = __task_rq_lock(p);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002308 activate_task(rq, p, 0);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002309 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra89363382011-04-05 17:23:42 +02002310 trace_sched_wakeup_new(p, true);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002311 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002312#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002313 if (p->sched_class->task_woken)
2314 p->sched_class->task_woken(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002315#endif
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002316 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
Avi Kivitye107be32007-07-26 13:40:43 +02002319#ifdef CONFIG_PREEMPT_NOTIFIERS
2320
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002321static struct static_key preempt_notifier_key = STATIC_KEY_INIT_FALSE;
2322
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02002323void preempt_notifier_inc(void)
2324{
2325 static_key_slow_inc(&preempt_notifier_key);
2326}
2327EXPORT_SYMBOL_GPL(preempt_notifier_inc);
2328
2329void preempt_notifier_dec(void)
2330{
2331 static_key_slow_dec(&preempt_notifier_key);
2332}
2333EXPORT_SYMBOL_GPL(preempt_notifier_dec);
2334
Avi Kivitye107be32007-07-26 13:40:43 +02002335/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002336 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002337 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002338 */
2339void preempt_notifier_register(struct preempt_notifier *notifier)
2340{
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02002341 if (!static_key_false(&preempt_notifier_key))
2342 WARN(1, "registering preempt_notifier while notifiers disabled\n");
2343
Avi Kivitye107be32007-07-26 13:40:43 +02002344 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2345}
2346EXPORT_SYMBOL_GPL(preempt_notifier_register);
2347
2348/**
2349 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002350 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002351 *
Mathieu Desnoyersd84525a2015-05-17 12:53:10 -04002352 * This is *not* safe to call from within a preemption notifier.
Avi Kivitye107be32007-07-26 13:40:43 +02002353 */
2354void preempt_notifier_unregister(struct preempt_notifier *notifier)
2355{
2356 hlist_del(&notifier->link);
2357}
2358EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2359
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002360static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02002361{
2362 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002363
Sasha Levinb67bfe02013-02-27 17:06:00 -08002364 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002365 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2366}
2367
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002368static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2369{
2370 if (static_key_false(&preempt_notifier_key))
2371 __fire_sched_in_preempt_notifiers(curr);
2372}
2373
Avi Kivitye107be32007-07-26 13:40:43 +02002374static void
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002375__fire_sched_out_preempt_notifiers(struct task_struct *curr,
2376 struct task_struct *next)
Avi Kivitye107be32007-07-26 13:40:43 +02002377{
2378 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002379
Sasha Levinb67bfe02013-02-27 17:06:00 -08002380 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002381 notifier->ops->sched_out(notifier, next);
2382}
2383
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002384static __always_inline void
2385fire_sched_out_preempt_notifiers(struct task_struct *curr,
2386 struct task_struct *next)
2387{
2388 if (static_key_false(&preempt_notifier_key))
2389 __fire_sched_out_preempt_notifiers(curr, next);
2390}
2391
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002392#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002393
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002394static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02002395{
2396}
2397
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002398static inline void
Avi Kivitye107be32007-07-26 13:40:43 +02002399fire_sched_out_preempt_notifiers(struct task_struct *curr,
2400 struct task_struct *next)
2401{
2402}
2403
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002404#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002407 * prepare_task_switch - prepare to switch tasks
2408 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002409 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002410 * @next: the task we are going to switch to.
2411 *
2412 * This is called with the rq lock held and interrupts off. It must
2413 * be paired with a subsequent finish_task_switch after the context
2414 * switch.
2415 *
2416 * prepare_task_switch sets up locking and calls architecture specific
2417 * hooks.
2418 */
Avi Kivitye107be32007-07-26 13:40:43 +02002419static inline void
2420prepare_task_switch(struct rq *rq, struct task_struct *prev,
2421 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002422{
Andrew Vagin895dd922012-07-12 14:14:29 +04002423 trace_sched_switch(prev, next);
Michael S. Tsirkin43148952013-09-22 17:20:54 +03002424 sched_info_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002425 perf_event_task_sched_out(prev, next);
Avi Kivitye107be32007-07-26 13:40:43 +02002426 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002427 prepare_lock_switch(rq, next);
2428 prepare_arch_switch(next);
2429}
2430
2431/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 * finish_task_switch - clean up after a task-switch
2433 * @prev: the thread we just switched away from.
2434 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002435 * finish_task_switch must be called after the context switch, paired
2436 * with a prepare_task_switch call before the context switch.
2437 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2438 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 *
2440 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002441 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 * with the lock held can cause deadlocks; see schedule() for
2443 * details.)
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002444 *
2445 * The context switch have flipped the stack from under us and restored the
2446 * local variables which were saved when this task called schedule() in the
2447 * past. prev == current is still correct but we need to recalculate this_rq
2448 * because prev may have moved to another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002450static struct rq *finish_task_switch(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 __releases(rq->lock)
2452{
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002453 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002455 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 rq->prev_mm = NULL;
2458
2459 /*
2460 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002461 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002462 * schedule one last time. The schedule call will never return, and
2463 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002464 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 * still held, otherwise prev could be scheduled on another cpu, die
2466 * there before we look at prev->state, and then the reference would
2467 * be dropped twice.
2468 * Manfred Spraul <manfred@colorfullife.com>
2469 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002470 prev_state = prev->state;
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +02002471 vtime_task_switch(prev);
Nick Piggin4866cde2005-06-25 14:57:23 -07002472 finish_arch_switch(prev);
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002473 perf_event_task_sched_in(prev, current);
Nick Piggin4866cde2005-06-25 14:57:23 -07002474 finish_lock_switch(rq, prev);
Catalin Marinas01f23e12011-11-27 21:43:10 +00002475 finish_arch_post_lock_switch();
Steven Rostedte8fa1362008-01-25 21:08:05 +01002476
Avi Kivitye107be32007-07-26 13:40:43 +02002477 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (mm)
2479 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002480 if (unlikely(prev_state == TASK_DEAD)) {
Dario Faggiolie6c390f2013-11-07 14:43:35 +01002481 if (prev->sched_class->task_dead)
2482 prev->sched_class->task_dead(prev);
2483
bibo maoc6fd91f2006-03-26 01:38:20 -08002484 /*
2485 * Remove function-return probe instances associated with this
2486 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002487 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002488 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002490 }
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +02002491
2492 tick_nohz_task_switch(current);
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002493 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Gregory Haskins3f029d32009-07-29 11:08:47 -04002496#ifdef CONFIG_SMP
2497
Gregory Haskins3f029d32009-07-29 11:08:47 -04002498/* rq->lock is NOT held, but preemption is disabled */
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002499static void __balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04002500{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002501 struct callback_head *head, *next;
2502 void (*func)(struct rq *rq);
2503 unsigned long flags;
Gregory Haskins3f029d32009-07-29 11:08:47 -04002504
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002505 raw_spin_lock_irqsave(&rq->lock, flags);
2506 head = rq->balance_callback;
2507 rq->balance_callback = NULL;
2508 while (head) {
2509 func = (void (*)(struct rq *))head->func;
2510 next = head->next;
2511 head->next = NULL;
2512 head = next;
Gregory Haskins3f029d32009-07-29 11:08:47 -04002513
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002514 func(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002515 }
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002516 raw_spin_unlock_irqrestore(&rq->lock, flags);
2517}
2518
2519static inline void balance_callback(struct rq *rq)
2520{
2521 if (unlikely(rq->balance_callback))
2522 __balance_callback(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002523}
2524
2525#else
2526
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002527static inline void balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04002528{
2529}
2530
2531#endif
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533/**
2534 * schedule_tail - first thing a freshly forked thread must call.
2535 * @prev: the thread we just switched away from.
2536 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002537asmlinkage __visible void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 __releases(rq->lock)
2539{
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002540 struct rq *rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002541
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002542 /* finish_task_switch() drops rq->lock and enables preemtion */
2543 preempt_disable();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002544 rq = finish_task_switch(prev);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002545 balance_callback(rq);
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002546 preempt_enable();
Steven Rostedtda19ab52009-07-29 00:21:22 -04002547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002549 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
2552/*
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002553 * context_switch - switch to the new MM and the new thread's register state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002555static inline struct rq *
Ingo Molnar70b97a72006-07-03 00:25:42 -07002556context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002557 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Ingo Molnardd41f592007-07-09 18:51:59 +02002559 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Avi Kivitye107be32007-07-26 13:40:43 +02002561 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002562
Ingo Molnardd41f592007-07-09 18:51:59 +02002563 mm = next->mm;
2564 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002565 /*
2566 * For paravirt, this is coupled with an exit in switch_to to
2567 * combine the page table reload and the switch backend into
2568 * one hypercall.
2569 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002570 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002571
Heiko Carstens31915ab2010-09-16 14:42:25 +02002572 if (!mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 next->active_mm = oldmm;
2574 atomic_inc(&oldmm->mm_count);
2575 enter_lazy_tlb(oldmm, next);
2576 } else
2577 switch_mm(oldmm, mm, next);
2578
Heiko Carstens31915ab2010-09-16 14:42:25 +02002579 if (!prev->mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 rq->prev_mm = oldmm;
2582 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002583 /*
2584 * Since the runqueue lock will be released by the next
2585 * task (which is an invalid locking op but in the case
2586 * of the scheduler it's an obvious special-case), so we
2587 * do an early lockdep release here:
2588 */
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002589 lockdep_unpin_lock(&rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002590 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 /* Here we just switch the register state and the stack. */
2593 switch_to(prev, next, prev);
Ingo Molnardd41f592007-07-09 18:51:59 +02002594 barrier();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002595
2596 return finish_task_switch(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
2599/*
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002600 * nr_running and nr_context_switches:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 *
2602 * externally visible scheduler statistics: current number of runnable
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002603 * threads, total number of context switches performed since bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 */
2605unsigned long nr_running(void)
2606{
2607 unsigned long i, sum = 0;
2608
2609 for_each_online_cpu(i)
2610 sum += cpu_rq(i)->nr_running;
2611
2612 return sum;
2613}
2614
Tim Chen2ee507c2014-07-31 10:29:48 -07002615/*
2616 * Check if only the current task is running on the cpu.
2617 */
2618bool single_task_running(void)
2619{
2620 if (cpu_rq(smp_processor_id())->nr_running == 1)
2621 return true;
2622 else
2623 return false;
2624}
2625EXPORT_SYMBOL(single_task_running);
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627unsigned long long nr_context_switches(void)
2628{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002629 int i;
2630 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002632 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 sum += cpu_rq(i)->nr_switches;
2634
2635 return sum;
2636}
2637
2638unsigned long nr_iowait(void)
2639{
2640 unsigned long i, sum = 0;
2641
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002642 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2644
2645 return sum;
2646}
2647
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002648unsigned long nr_iowait_cpu(int cpu)
Arjan van de Ven69d25872009-09-21 17:04:08 -07002649{
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002650 struct rq *this = cpu_rq(cpu);
Arjan van de Ven69d25872009-09-21 17:04:08 -07002651 return atomic_read(&this->nr_iowait);
2652}
2653
Mel Gorman372ba8c2014-08-06 14:19:21 +01002654void get_iowait_load(unsigned long *nr_waiters, unsigned long *load)
2655{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02002656 struct rq *rq = this_rq();
2657 *nr_waiters = atomic_read(&rq->nr_iowait);
2658 *load = rq->load.weight;
Mel Gorman372ba8c2014-08-06 14:19:21 +01002659}
2660
Ingo Molnardd41f592007-07-09 18:51:59 +02002661#ifdef CONFIG_SMP
2662
Ingo Molnar48f24c42006-07-03 00:25:40 -07002663/*
Peter Zijlstra38022902009-12-16 18:04:37 +01002664 * sched_exec - execve() is a valuable balancing opportunity, because at
2665 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 */
Peter Zijlstra38022902009-12-16 18:04:37 +01002667void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Peter Zijlstra38022902009-12-16 18:04:37 +01002669 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01002671 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01002672
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002673 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstraac66f542013-10-07 11:29:16 +01002674 dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002675 if (dest_cpu == smp_processor_id())
2676 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01002677
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002678 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02002679 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07002680
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002681 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
2682 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return;
2684 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01002685unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002686 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689#endif
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691DEFINE_PER_CPU(struct kernel_stat, kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002692DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694EXPORT_PER_CPU_SYMBOL(kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002695EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002698 * Return accounted runtime for the task.
2699 * In case the task is currently running, return the runtime plus current's
2700 * pending runtime that have not been accounted yet.
2701 */
2702unsigned long long task_sched_runtime(struct task_struct *p)
2703{
2704 unsigned long flags;
2705 struct rq *rq;
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002706 u64 ns;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002707
Peter Zijlstra911b2892013-11-11 18:21:56 +01002708#if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
2709 /*
2710 * 64-bit doesn't need locks to atomically read a 64bit value.
2711 * So we have a optimization chance when the task's delta_exec is 0.
2712 * Reading ->on_cpu is racy, but this is ok.
2713 *
2714 * If we race with it leaving cpu, we'll take a lock. So we're correct.
2715 * If we race with it entering cpu, unaccounted time is 0. This is
2716 * indistinguishable from the read occurring a few cycles earlier.
Mike Galbraith4036ac12014-06-24 07:49:40 +02002717 * If we see ->on_cpu without ->on_rq, the task is leaving, and has
2718 * been accounted, so we're correct here as well.
Peter Zijlstra911b2892013-11-11 18:21:56 +01002719 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002720 if (!p->on_cpu || !task_on_rq_queued(p))
Peter Zijlstra911b2892013-11-11 18:21:56 +01002721 return p->se.sum_exec_runtime;
2722#endif
2723
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002724 rq = task_rq_lock(p, &flags);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002725 /*
2726 * Must be ->curr _and_ ->on_rq. If dequeued, we would
2727 * project cycles that may never be accounted to this
2728 * thread, breaking clock_gettime().
2729 */
2730 if (task_current(rq, p) && task_on_rq_queued(p)) {
2731 update_rq_clock(rq);
2732 p->sched_class->update_curr(rq);
2733 }
2734 ns = p->se.sum_exec_runtime;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002735 task_rq_unlock(rq, p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002736
2737 return ns;
2738}
2739
Balbir Singh49048622008-09-05 18:12:23 +02002740/*
Christoph Lameter7835b982006-12-10 02:20:22 -08002741 * This function gets called by the timer code, with HZ frequency.
2742 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08002743 */
2744void scheduler_tick(void)
2745{
Christoph Lameter7835b982006-12-10 02:20:22 -08002746 int cpu = smp_processor_id();
2747 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002748 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002749
2750 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08002751
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002752 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002753 update_rq_clock(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01002754 curr->sched_class->task_tick(rq, curr, 0);
Alex Shi83dfd522013-06-20 10:18:49 +08002755 update_cpu_load_active(rq);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02002756 calc_global_load_tick(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002757 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02002758
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002759 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002760
Christoph Lametere418e1c2006-12-10 02:20:23 -08002761#ifdef CONFIG_SMP
Suresh Siddha6eb57e02011-10-03 15:09:01 -07002762 rq->idle_balance = idle_cpu(cpu);
Daniel Lezcano7caff662014-01-06 12:34:38 +01002763 trigger_load_balance(rq);
Christoph Lametere418e1c2006-12-10 02:20:23 -08002764#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002765 rq_last_tick_reset(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
2767
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002768#ifdef CONFIG_NO_HZ_FULL
2769/**
2770 * scheduler_tick_max_deferment
2771 *
2772 * Keep at least one tick per second when a single
2773 * active task is running because the scheduler doesn't
2774 * yet completely support full dynticks environment.
2775 *
2776 * This makes sure that uptime, CFS vruntime, load
2777 * balancing, etc... continue to move forward, even
2778 * with a very low granularity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02002779 *
2780 * Return: Maximum deferment in nanoseconds.
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002781 */
2782u64 scheduler_tick_max_deferment(void)
2783{
2784 struct rq *rq = this_rq();
Jason Low316c1608d2015-04-28 13:00:20 -07002785 unsigned long next, now = READ_ONCE(jiffies);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002786
2787 next = rq->last_sched_tick + HZ;
2788
2789 if (time_before_eq(next, now))
2790 return 0;
2791
Kevin Hilman8fe8ff02014-01-15 14:51:38 +01002792 return jiffies_to_nsecs(next - now);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002793}
2794#endif
2795
Lai Jiangshan132380a2009-04-02 14:18:25 +08002796notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002797{
2798 if (in_lock_functions(addr)) {
2799 addr = CALLER_ADDR2;
2800 if (in_lock_functions(addr))
2801 addr = CALLER_ADDR3;
2802 }
2803 return addr;
2804}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05002806#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
2807 defined(CONFIG_PREEMPT_TRACER))
2808
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002809void preempt_count_add(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002811#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 /*
2813 * Underflow?
2814 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002815 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
2816 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002817#endif
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002818 __preempt_count_add(val);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002819#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 /*
2821 * Spinlock count overflowing soon?
2822 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002823 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
2824 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002825#endif
Thomas Gleixner8f47b182014-02-07 20:58:39 +01002826 if (preempt_count() == val) {
2827 unsigned long ip = get_parent_ip(CALLER_ADDR1);
2828#ifdef CONFIG_DEBUG_PREEMPT
2829 current->preempt_disable_ip = ip;
2830#endif
2831 trace_preempt_off(CALLER_ADDR0, ip);
2832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002834EXPORT_SYMBOL(preempt_count_add);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002835NOKPROBE_SYMBOL(preempt_count_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002837void preempt_count_sub(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002839#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 /*
2841 * Underflow?
2842 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01002843 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002844 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 /*
2846 * Is the spinlock portion underflowing?
2847 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002848 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
2849 !(preempt_count() & PREEMPT_MASK)))
2850 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002851#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002852
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002853 if (preempt_count() == val)
2854 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002855 __preempt_count_sub(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002857EXPORT_SYMBOL(preempt_count_sub);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002858NOKPROBE_SYMBOL(preempt_count_sub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860#endif
2861
2862/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002863 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002865static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
Dave Jones664dfa62011-12-22 16:39:30 -05002867 if (oops_in_progress)
2868 return;
2869
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01002870 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
2871 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02002872
Ingo Molnardd41f592007-07-09 18:51:59 +02002873 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07002874 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02002875 if (irqs_disabled())
2876 print_irqtrace_events(prev);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01002877#ifdef CONFIG_DEBUG_PREEMPT
2878 if (in_atomic_preempt_off()) {
2879 pr_err("Preemption disabled at:");
2880 print_ip_sym(current->preempt_disable_ip);
2881 pr_cont("\n");
2882 }
2883#endif
Stephen Boyd6135fc12012-03-28 17:10:47 -07002884 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +10302885 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Ingo Molnardd41f592007-07-09 18:51:59 +02002886}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Ingo Molnardd41f592007-07-09 18:51:59 +02002888/*
2889 * Various schedule()-time debugging checks and statistics:
2890 */
2891static inline void schedule_debug(struct task_struct *prev)
2892{
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01002893#ifdef CONFIG_SCHED_STACK_END_CHECK
2894 BUG_ON(unlikely(task_stack_end_corrupted(prev)));
2895#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002897 * Test if we are atomic. Since do_exit() needs to call into
Oleg Nesterov192301e2013-11-13 16:45:38 +01002898 * schedule() atomically, we ignore that path. Otherwise whine
2899 * if we are scheduling when we should not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 */
Oleg Nesterov192301e2013-11-13 16:45:38 +01002901 if (unlikely(in_atomic_preempt_off() && prev->state != TASK_DEAD))
Ingo Molnardd41f592007-07-09 18:51:59 +02002902 __schedule_bug(prev);
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002903 rcu_sleep_check();
Ingo Molnardd41f592007-07-09 18:51:59 +02002904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
2906
Ingo Molnar2d723762007-10-15 17:00:12 +02002907 schedstat_inc(this_rq(), sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02002908}
2909
2910/*
2911 * Pick up the highest-prio task:
2912 */
2913static inline struct task_struct *
Peter Zijlstra606dba22012-02-11 06:05:00 +01002914pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02002915{
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002916 const struct sched_class *class = &fair_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02002917 struct task_struct *p;
2918
2919 /*
2920 * Optimization: we know that if all tasks are in
2921 * the fair class we can call that function directly:
2922 */
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002923 if (likely(prev->sched_class == class &&
Peter Zijlstra38033c32014-01-23 20:32:21 +01002924 rq->nr_running == rq->cfs.h_nr_running)) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01002925 p = fair_sched_class.pick_next_task(rq, prev);
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02002926 if (unlikely(p == RETRY_TASK))
2927 goto again;
2928
2929 /* assumes fair_sched_class->next == idle_sched_class */
2930 if (unlikely(!p))
2931 p = idle_sched_class.pick_next_task(rq, prev);
2932
2933 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02002934 }
2935
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002936again:
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002937 for_each_class(class) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01002938 p = class->pick_next_task(rq, prev);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002939 if (p) {
2940 if (unlikely(p == RETRY_TASK))
2941 goto again;
Ingo Molnardd41f592007-07-09 18:51:59 +02002942 return p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002943 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002944 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002945
2946 BUG(); /* the idle class will always have a runnable task */
Ingo Molnardd41f592007-07-09 18:51:59 +02002947}
2948
2949/*
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002950 * __schedule() is the main scheduler function.
Pekka Enbergedde96e2012-08-04 11:49:47 +03002951 *
2952 * The main means of driving the scheduler and thus entering this function are:
2953 *
2954 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
2955 *
2956 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
2957 * paths. For example, see arch/x86/entry_64.S.
2958 *
2959 * To drive preemption between tasks, the scheduler sets the flag in timer
2960 * interrupt handler scheduler_tick().
2961 *
2962 * 3. Wakeups don't really cause entry into schedule(). They add a
2963 * task to the run-queue and that's it.
2964 *
2965 * Now, if the new task added to the run-queue preempts the current
2966 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
2967 * called on the nearest possible occasion:
2968 *
2969 * - If the kernel is preemptible (CONFIG_PREEMPT=y):
2970 *
2971 * - in syscall or exception context, at the next outmost
2972 * preempt_enable(). (this might be as soon as the wake_up()'s
2973 * spin_unlock()!)
2974 *
2975 * - in IRQ context, return from interrupt-handler to
2976 * preemptible context
2977 *
2978 * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
2979 * then at the next:
2980 *
2981 * - cond_resched() call
2982 * - explicit schedule() call
2983 * - return from syscall or exception to user-space
2984 * - return from interrupt-handler to user-space
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01002985 *
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02002986 * WARNING: must be called with preemption disabled!
Ingo Molnardd41f592007-07-09 18:51:59 +02002987 */
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002988static void __sched __schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02002989{
2990 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08002991 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02002992 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02002993 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02002994
Ingo Molnardd41f592007-07-09 18:51:59 +02002995 cpu = smp_processor_id();
2996 rq = cpu_rq(cpu);
Paul E. McKenney38200cf2014-10-21 12:50:04 -07002997 rcu_note_context_switch();
Ingo Molnardd41f592007-07-09 18:51:59 +02002998 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02002999
Ingo Molnardd41f592007-07-09 18:51:59 +02003000 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Peter Zijlstra31656512008-07-18 18:01:23 +02003002 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02003003 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003004
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02003005 /*
3006 * Make sure that signal_pending_state()->signal_pending() below
3007 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
3008 * done by the caller to avoid the race with signal_wake_up().
3009 */
3010 smp_mb__before_spinlock();
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003011 raw_spin_lock_irq(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003012 lockdep_pin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003014 rq->clock_skip_update <<= 1; /* promote REQ to ACT */
3015
Oleg Nesterov246d86b2010-05-19 14:57:11 +02003016 switch_count = &prev->nivcsw;
Ingo Molnardd41f592007-07-09 18:51:59 +02003017 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Tejun Heo21aa9af2010-06-08 21:40:37 +02003018 if (unlikely(signal_pending_state(prev->state, prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003019 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02003020 } else {
Peter Zijlstra2acca552011-04-05 17:23:50 +02003021 deactivate_task(rq, prev, DEQUEUE_SLEEP);
3022 prev->on_rq = 0;
3023
Tejun Heo21aa9af2010-06-08 21:40:37 +02003024 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02003025 * If a worker went to sleep, notify and ask workqueue
3026 * whether it wants to wake up a task to maintain
3027 * concurrency.
Tejun Heo21aa9af2010-06-08 21:40:37 +02003028 */
3029 if (prev->flags & PF_WQ_WORKER) {
3030 struct task_struct *to_wakeup;
3031
3032 to_wakeup = wq_worker_sleeping(prev, cpu);
3033 if (to_wakeup)
3034 try_to_wake_up_local(to_wakeup);
3035 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02003036 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003037 switch_count = &prev->nvcsw;
3038 }
3039
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003040 if (task_on_rq_queued(prev))
Peter Zijlstra606dba22012-02-11 06:05:00 +01003041 update_rq_clock(rq);
3042
3043 next = pick_next_task(rq, prev);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01003044 clear_tsk_need_resched(prev);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02003045 clear_preempt_need_resched();
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003046 rq->clock_skip_update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 rq->nr_switches++;
3050 rq->curr = next;
3051 ++*switch_count;
3052
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003053 rq = context_switch(rq, prev, next); /* unlocks the rq */
3054 cpu = cpu_of(rq);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003055 } else {
3056 lockdep_unpin_lock(&rq->lock);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003057 raw_spin_unlock_irq(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003060 balance_callback(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003062
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003063static inline void sched_submit_work(struct task_struct *tsk)
3064{
Thomas Gleixner3c7d5182011-07-17 20:46:52 +02003065 if (!tsk->state || tsk_is_pi_blocked(tsk))
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003066 return;
3067 /*
3068 * If we are going to sleep and we have plugged IO queued,
3069 * make sure to submit it to avoid deadlocks.
3070 */
3071 if (blk_needs_flush_plug(tsk))
3072 blk_schedule_flush_plug(tsk);
3073}
3074
Andi Kleen722a9f92014-05-02 00:44:38 +02003075asmlinkage __visible void __sched schedule(void)
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003076{
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003077 struct task_struct *tsk = current;
3078
3079 sched_submit_work(tsk);
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003080 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003081 preempt_disable();
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003082 __schedule();
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003083 sched_preempt_enable_no_resched();
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003084 } while (need_resched());
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086EXPORT_SYMBOL(schedule);
3087
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01003088#ifdef CONFIG_CONTEXT_TRACKING
Andi Kleen722a9f92014-05-02 00:44:38 +02003089asmlinkage __visible void __sched schedule_user(void)
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003090{
3091 /*
3092 * If we come here after a random call to set_need_resched(),
3093 * or we have been woken up remotely but the IPI has not yet arrived,
3094 * we haven't yet exited the RCU idle mode. Do it here manually until
3095 * we find a better solution.
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003096 *
3097 * NB: There are buggy callers of this function. Ideally we
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +01003098 * should warn if prev_state != CONTEXT_USER, but that will trigger
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003099 * too frequently to make sense yet.
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003100 */
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003101 enum ctx_state prev_state = exception_enter();
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003102 schedule();
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003103 exception_exit(prev_state);
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003104}
3105#endif
3106
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01003107/**
3108 * schedule_preempt_disabled - called with preemption disabled
3109 *
3110 * Returns with preemption disabled. Note: preempt_count must be 1
3111 */
3112void __sched schedule_preempt_disabled(void)
3113{
Thomas Gleixnerba74c142011-03-21 13:32:17 +01003114 sched_preempt_enable_no_resched();
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01003115 schedule();
3116 preempt_disable();
3117}
3118
Frederic Weisbecker06b1f802015-02-16 19:20:07 +01003119static void __sched notrace preempt_schedule_common(void)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003120{
3121 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003122 preempt_active_enter();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003123 __schedule();
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003124 preempt_active_exit();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003125
3126 /*
3127 * Check again in case we missed a preemption opportunity
3128 * between schedule and now.
3129 */
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003130 } while (need_resched());
3131}
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133#ifdef CONFIG_PREEMPT
3134/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003135 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003136 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 * occur there and call schedule directly.
3138 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003139asmlinkage __visible void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 /*
3142 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003143 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 */
Frederic Weisbeckerfbb00b52013-06-19 23:56:22 +02003145 if (likely(!preemptible()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return;
3147
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003148 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09003150NOKPROBE_SYMBOL(preempt_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151EXPORT_SYMBOL(preempt_schedule);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003152
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003153/**
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003154 * preempt_schedule_notrace - preempt_schedule called by tracing
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003155 *
3156 * The tracing infrastructure uses preempt_enable_notrace to prevent
3157 * recursion and tracing preempt enabling caused by the tracing
3158 * infrastructure itself. But as tracing can happen in areas coming
3159 * from userspace or just about to enter userspace, a preempt enable
3160 * can occur before user_exit() is called. This will cause the scheduler
3161 * to be called when the system is still in usermode.
3162 *
3163 * To prevent this, the preempt_enable_notrace will use this function
3164 * instead of preempt_schedule() to exit user context if needed before
3165 * calling the scheduler.
3166 */
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003167asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003168{
3169 enum ctx_state prev_ctx;
3170
3171 if (likely(!preemptible()))
3172 return;
3173
3174 do {
Frederic Weisbeckerbe690032015-06-04 17:39:07 +02003175 /*
3176 * Use raw __prempt_count() ops that don't call function.
3177 * We can't call functions before disabling preemption which
3178 * disarm preemption tracing recursions.
3179 */
3180 __preempt_count_add(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET);
3181 barrier();
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003182 /*
3183 * Needs preempt disabled in case user_exit() is traced
3184 * and the tracer calls preempt_enable_notrace() causing
3185 * an infinite recursion.
3186 */
3187 prev_ctx = exception_enter();
3188 __schedule();
3189 exception_exit(prev_ctx);
3190
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003191 barrier();
Frederic Weisbeckerbe690032015-06-04 17:39:07 +02003192 __preempt_count_sub(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003193 } while (need_resched());
3194}
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003195EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003196
Thomas Gleixner32e475d2013-11-21 12:41:44 +01003197#endif /* CONFIG_PREEMPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003200 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 * off of irq context.
3202 * Note, that this is called and return with irqs disabled. This will
3203 * protect us against recursive calling from irq.
3204 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003205asmlinkage __visible void __sched preempt_schedule_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003207 enum ctx_state prev_state;
Ingo Molnar6478d882008-01-25 21:08:33 +01003208
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003209 /* Catch callers which need to be fixed */
Peter Zijlstraf27dde82013-08-14 14:55:31 +02003210 BUG_ON(preempt_count() || !irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003212 prev_state = exception_enter();
3213
Andi Kleen3a5c3592007-10-15 17:00:14 +02003214 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003215 preempt_active_enter();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003216 local_irq_enable();
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003217 __schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003218 local_irq_disable();
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003219 preempt_active_exit();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003220 } while (need_resched());
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003221
3222 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223}
3224
Peter Zijlstra63859d42009-09-15 19:14:42 +02003225int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003226 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
Peter Zijlstra63859d42009-09-15 19:14:42 +02003228 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230EXPORT_SYMBOL(default_wake_function);
3231
Ingo Molnarb29739f2006-06-27 02:54:51 -07003232#ifdef CONFIG_RT_MUTEXES
3233
3234/*
3235 * rt_mutex_setprio - set the current priority of a task
3236 * @p: task
3237 * @prio: prio value (kernel-internal form)
3238 *
3239 * This function changes the 'effective' priority of a task. It does
3240 * not touch ->normal_prio like __setscheduler().
3241 *
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003242 * Used by the rt_mutex code to implement priority inheritance
3243 * logic. Call site only calls if the priority of the task changed.
Ingo Molnarb29739f2006-06-27 02:54:51 -07003244 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003245void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003246{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003247 int oldprio, queued, running, enqueue_flag = 0;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003248 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003249 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003250
Dario Faggioliaab03e02013-11-28 11:14:43 +01003251 BUG_ON(prio > MAX_PRIO);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003252
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003253 rq = __task_rq_lock(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003254
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003255 /*
3256 * Idle task boosting is a nono in general. There is one
3257 * exception, when PREEMPT_RT and NOHZ is active:
3258 *
3259 * The idle task calls get_next_timer_interrupt() and holds
3260 * the timer wheel base->lock on the CPU and another CPU wants
3261 * to access the timer (probably to cancel it). We can safely
3262 * ignore the boosting request, as the idle CPU runs this code
3263 * with interrupts disabled and will complete the lock
3264 * protected section without being interrupted. So there is no
3265 * real need to boost.
3266 */
3267 if (unlikely(p == rq->idle)) {
3268 WARN_ON(p != rq->curr);
3269 WARN_ON(p->pi_blocked_on);
3270 goto out_unlock;
3271 }
3272
Steven Rostedta8027072010-09-20 15:13:34 -04003273 trace_sched_pi_setprio(p, prio);
Andrew Mortond5f9f942007-05-08 20:27:06 -07003274 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003275 prev_class = p->sched_class;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003276 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003277 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003278 if (queued)
Ingo Molnar69be72c2007-08-09 11:16:49 +02003279 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003280 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003281 put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02003282
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003283 /*
3284 * Boosting condition are:
3285 * 1. -rt task is running and holds mutex A
3286 * --> -dl task blocks on mutex A
3287 *
3288 * 2. -dl task is running and holds mutex A
3289 * --> -dl task blocks on mutex A and could preempt the
3290 * running task
3291 */
3292 if (dl_prio(prio)) {
Oleg Nesterov466af292014-06-06 18:52:06 +02003293 struct task_struct *pi_task = rt_mutex_get_top_task(p);
3294 if (!dl_prio(p->normal_prio) ||
3295 (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003296 p->dl.dl_boosted = 1;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003297 enqueue_flag = ENQUEUE_REPLENISH;
3298 } else
3299 p->dl.dl_boosted = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003300 p->sched_class = &dl_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003301 } else if (rt_prio(prio)) {
3302 if (dl_prio(oldprio))
3303 p->dl.dl_boosted = 0;
3304 if (oldprio < prio)
3305 enqueue_flag = ENQUEUE_HEAD;
Ingo Molnardd41f592007-07-09 18:51:59 +02003306 p->sched_class = &rt_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003307 } else {
3308 if (dl_prio(oldprio))
3309 p->dl.dl_boosted = 0;
Brian Silverman746db942015-02-18 16:23:56 -08003310 if (rt_prio(oldprio))
3311 p->rt.timeout = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003312 p->sched_class = &fair_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003313 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003314
Ingo Molnarb29739f2006-06-27 02:54:51 -07003315 p->prio = prio;
3316
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003317 if (running)
3318 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003319 if (queued)
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003320 enqueue_task(rq, p, enqueue_flag);
Steven Rostedtcb469842008-01-25 21:08:22 +01003321
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003322 check_class_changed(rq, p, prev_class, oldprio);
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003323out_unlock:
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003324 preempt_disable(); /* avoid rq from going away on us */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003325 __task_rq_unlock(rq);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003326
3327 balance_callback(rq);
3328 preempt_enable();
Ingo Molnarb29739f2006-06-27 02:54:51 -07003329}
Ingo Molnarb29739f2006-06-27 02:54:51 -07003330#endif
Dario Faggiolid50dde52013-11-07 14:43:36 +01003331
Ingo Molnar36c8b582006-07-03 00:25:41 -07003332void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003334 int old_prio, delta, queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003336 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Dongsheng Yang75e45d52014-02-11 15:34:50 +08003338 if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 return;
3340 /*
3341 * We have to be careful, if called from sys_setpriority(),
3342 * the task might be in the middle of scheduling on another CPU.
3343 */
3344 rq = task_rq_lock(p, &flags);
3345 /*
3346 * The RT priorities are set via sched_setscheduler(), but we still
3347 * allow the 'normal' nice value to be set - but as expected
3348 * it wont have any effect on scheduling until the task is
Dario Faggioliaab03e02013-11-28 11:14:43 +01003349 * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003351 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 p->static_prio = NICE_TO_PRIO(nice);
3353 goto out_unlock;
3354 }
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003355 queued = task_on_rq_queued(p);
3356 if (queued)
Ingo Molnar69be72c2007-08-09 11:16:49 +02003357 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003360 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003361 old_prio = p->prio;
3362 p->prio = effective_prio(p);
3363 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003365 if (queued) {
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003366 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07003368 * If the task increased its priority or is running and
3369 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003371 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Kirill Tkhai88751252014-06-29 00:03:57 +04003372 resched_curr(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
3374out_unlock:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003375 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377EXPORT_SYMBOL(set_user_nice);
3378
Matt Mackalle43379f2005-05-01 08:59:00 -07003379/*
3380 * can_nice - check if a task can reduce its nice value
3381 * @p: task
3382 * @nice: nice value
3383 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003384int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07003385{
Matt Mackall024f4742005-08-18 11:24:19 -07003386 /* convert nice value [19,-20] to rlimit style value [1,40] */
Dongsheng Yang7aa2c012014-05-08 18:33:49 +09003387 int nice_rlim = nice_to_rlimit(nice);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003388
Jiri Slaby78d7d402010-03-05 13:42:54 -08003389 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07003390 capable(CAP_SYS_NICE));
3391}
3392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393#ifdef __ARCH_WANT_SYS_NICE
3394
3395/*
3396 * sys_nice - change the priority of the current process.
3397 * @increment: priority increment
3398 *
3399 * sys_setpriority is a more generic, but much slower function that
3400 * does similar things.
3401 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003402SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003404 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
3406 /*
3407 * Setpriority might change our priority at the same moment.
3408 * We don't have to worry. Conceptually one call occurs first
3409 * and we have a single winner.
3410 */
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003411 increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003412 nice = task_nice(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003414 nice = clamp_val(nice, MIN_NICE, MAX_NICE);
Matt Mackalle43379f2005-05-01 08:59:00 -07003415 if (increment < 0 && !can_nice(current, nice))
3416 return -EPERM;
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 retval = security_task_setnice(current, nice);
3419 if (retval)
3420 return retval;
3421
3422 set_user_nice(current, nice);
3423 return 0;
3424}
3425
3426#endif
3427
3428/**
3429 * task_prio - return the priority value of a given task.
3430 * @p: the task in question.
3431 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003432 * Return: The priority value as seen by users in /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 * RT tasks are offset by -200. Normal tasks are centered
3434 * around 0, value goes from -16 to +15.
3435 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003436int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437{
3438 return p->prio - MAX_RT_PRIO;
3439}
3440
3441/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 * idle_cpu - is a given cpu idle currently?
3443 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003444 *
3445 * Return: 1 if the CPU is currently idle. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 */
3447int idle_cpu(int cpu)
3448{
Thomas Gleixner908a3282011-09-15 15:32:06 +02003449 struct rq *rq = cpu_rq(cpu);
3450
3451 if (rq->curr != rq->idle)
3452 return 0;
3453
3454 if (rq->nr_running)
3455 return 0;
3456
3457#ifdef CONFIG_SMP
3458 if (!llist_empty(&rq->wake_list))
3459 return 0;
3460#endif
3461
3462 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463}
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465/**
3466 * idle_task - return the idle task for a given cpu.
3467 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003468 *
3469 * Return: The idle task for the cpu @cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003471struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
3473 return cpu_rq(cpu)->idle;
3474}
3475
3476/**
3477 * find_process_by_pid - find a process with a matching PID value.
3478 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003479 *
3480 * The task of @pid, if found. %NULL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003482static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003484 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
Dario Faggioliaab03e02013-11-28 11:14:43 +01003487/*
3488 * This function initializes the sched_dl_entity of a newly becoming
3489 * SCHED_DEADLINE task.
3490 *
3491 * Only the static values are considered here, the actual runtime and the
3492 * absolute deadline will be properly calculated when the task is enqueued
3493 * for the first time with its new policy.
3494 */
3495static void
3496__setparam_dl(struct task_struct *p, const struct sched_attr *attr)
3497{
3498 struct sched_dl_entity *dl_se = &p->dl;
3499
Dario Faggioliaab03e02013-11-28 11:14:43 +01003500 dl_se->dl_runtime = attr->sched_runtime;
3501 dl_se->dl_deadline = attr->sched_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003502 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003503 dl_se->flags = attr->sched_flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01003504 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
Peter Zijlstra40767b02015-01-28 15:08:03 +01003505
3506 /*
3507 * Changing the parameters of a task is 'tricky' and we're not doing
3508 * the correct thing -- also see task_dead_dl() and switched_from_dl().
3509 *
3510 * What we SHOULD do is delay the bandwidth release until the 0-lag
3511 * point. This would include retaining the task_struct until that time
3512 * and change dl_overflow() to not immediately decrement the current
3513 * amount.
3514 *
3515 * Instead we retain the current runtime/deadline and let the new
3516 * parameters take effect after the current reservation period lapses.
3517 * This is safe (albeit pessimistic) because the 0-lag point is always
3518 * before the current scheduling deadline.
3519 *
3520 * We can still have temporary overloads because we do not delay the
3521 * change in bandwidth until that time; so admission control is
3522 * not on the safe side. It does however guarantee tasks will never
3523 * consume more than promised.
3524 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003525}
3526
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003527/*
3528 * sched_setparam() passes in -1 for its policy, to let the functions
3529 * it calls know not to change it.
3530 */
3531#define SETPARAM_POLICY -1
3532
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003533static void __setscheduler_params(struct task_struct *p,
3534 const struct sched_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
Dario Faggiolid50dde52013-11-07 14:43:36 +01003536 int policy = attr->sched_policy;
3537
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003538 if (policy == SETPARAM_POLICY)
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003539 policy = p->policy;
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 p->policy = policy;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003542
Dario Faggioliaab03e02013-11-28 11:14:43 +01003543 if (dl_policy(policy))
3544 __setparam_dl(p, attr);
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003545 else if (fair_policy(policy))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003546 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
3547
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003548 /*
3549 * __sched_setscheduler() ensures attr->sched_priority == 0 when
3550 * !rt_policy. Always setting this ensures that things like
3551 * getparam()/getattr() don't report silly values for !rt tasks.
3552 */
3553 p->rt_priority = attr->sched_priority;
Steven Rostedt383afd02014-03-11 19:24:20 -04003554 p->normal_prio = normal_prio(p);
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003555 set_load_weight(p);
3556}
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003557
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003558/* Actually do priority change: must hold pi & rq lock. */
3559static void __setscheduler(struct rq *rq, struct task_struct *p,
Thomas Gleixner0782e632015-05-05 19:49:49 +02003560 const struct sched_attr *attr, bool keep_boost)
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003561{
3562 __setscheduler_params(p, attr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003563
Steven Rostedt383afd02014-03-11 19:24:20 -04003564 /*
Thomas Gleixner0782e632015-05-05 19:49:49 +02003565 * Keep a potential priority boosting if called from
3566 * sched_setscheduler().
Steven Rostedt383afd02014-03-11 19:24:20 -04003567 */
Thomas Gleixner0782e632015-05-05 19:49:49 +02003568 if (keep_boost)
3569 p->prio = rt_mutex_get_effective_prio(p, normal_prio(p));
3570 else
3571 p->prio = normal_prio(p);
Steven Rostedt383afd02014-03-11 19:24:20 -04003572
Dario Faggioliaab03e02013-11-28 11:14:43 +01003573 if (dl_prio(p->prio))
3574 p->sched_class = &dl_sched_class;
3575 else if (rt_prio(p->prio))
Peter Zijlstraffd44db2009-11-10 20:12:01 +01003576 p->sched_class = &rt_sched_class;
3577 else
3578 p->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
Dario Faggioliaab03e02013-11-28 11:14:43 +01003580
3581static void
3582__getparam_dl(struct task_struct *p, struct sched_attr *attr)
3583{
3584 struct sched_dl_entity *dl_se = &p->dl;
3585
3586 attr->sched_priority = p->rt_priority;
3587 attr->sched_runtime = dl_se->dl_runtime;
3588 attr->sched_deadline = dl_se->dl_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003589 attr->sched_period = dl_se->dl_period;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003590 attr->sched_flags = dl_se->flags;
3591}
3592
3593/*
3594 * This function validates the new parameters of a -deadline task.
3595 * We ask for the deadline not being zero, and greater or equal
Harald Gustafsson755378a2013-11-07 14:43:40 +01003596 * than the runtime, as well as the period of being zero or
Dario Faggioli332ac172013-11-07 14:43:45 +01003597 * greater than deadline. Furthermore, we have to be sure that
Juri Lellib08278192014-05-13 14:11:31 +02003598 * user parameters are above the internal resolution of 1us (we
3599 * check sched_runtime only since it is always the smaller one) and
3600 * below 2^63 ns (we have to check both sched_deadline and
3601 * sched_period, as the latter can be zero).
Dario Faggioliaab03e02013-11-28 11:14:43 +01003602 */
3603static bool
3604__checkparam_dl(const struct sched_attr *attr)
3605{
Juri Lellib08278192014-05-13 14:11:31 +02003606 /* deadline != 0 */
3607 if (attr->sched_deadline == 0)
3608 return false;
3609
3610 /*
3611 * Since we truncate DL_SCALE bits, make sure we're at least
3612 * that big.
3613 */
3614 if (attr->sched_runtime < (1ULL << DL_SCALE))
3615 return false;
3616
3617 /*
3618 * Since we use the MSB for wrap-around and sign issues, make
3619 * sure it's not set (mind that period can be equal to zero).
3620 */
3621 if (attr->sched_deadline & (1ULL << 63) ||
3622 attr->sched_period & (1ULL << 63))
3623 return false;
3624
3625 /* runtime <= deadline <= period (if period != 0) */
3626 if ((attr->sched_period != 0 &&
3627 attr->sched_period < attr->sched_deadline) ||
3628 attr->sched_deadline < attr->sched_runtime)
3629 return false;
3630
3631 return true;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003632}
3633
David Howellsc69e8d92008-11-14 10:39:19 +11003634/*
3635 * check the target process has a UID that matches the current process's
3636 */
3637static bool check_same_owner(struct task_struct *p)
3638{
3639 const struct cred *cred = current_cred(), *pcred;
3640 bool match;
3641
3642 rcu_read_lock();
3643 pcred = __task_cred(p);
Eric W. Biederman9c806aa2012-02-02 18:54:02 -08003644 match = (uid_eq(cred->euid, pcred->euid) ||
3645 uid_eq(cred->euid, pcred->uid));
David Howellsc69e8d92008-11-14 10:39:19 +11003646 rcu_read_unlock();
3647 return match;
3648}
3649
Wanpeng Li75381602014-11-26 08:44:04 +08003650static bool dl_param_changed(struct task_struct *p,
3651 const struct sched_attr *attr)
3652{
3653 struct sched_dl_entity *dl_se = &p->dl;
3654
3655 if (dl_se->dl_runtime != attr->sched_runtime ||
3656 dl_se->dl_deadline != attr->sched_deadline ||
3657 dl_se->dl_period != attr->sched_period ||
3658 dl_se->flags != attr->sched_flags)
3659 return true;
3660
3661 return false;
3662}
3663
Dario Faggiolid50dde52013-11-07 14:43:36 +01003664static int __sched_setscheduler(struct task_struct *p,
3665 const struct sched_attr *attr,
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003666 bool user, bool pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
Steven Rostedt383afd02014-03-11 19:24:20 -04003668 int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
3669 MAX_RT_PRIO - 1 - attr->sched_priority;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003670 int retval, oldprio, oldpolicy = -1, queued, running;
Thomas Gleixner0782e632015-05-05 19:49:49 +02003671 int new_effective_prio, policy = attr->sched_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003673 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003674 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003675 int reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Steven Rostedt66e53932006-06-27 02:54:44 -07003677 /* may grab non-irq protected spin_locks */
3678 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679recheck:
3680 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02003681 if (policy < 0) {
3682 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003684 } else {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003685 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
Lennart Poetteringca94c442009-06-15 17:17:47 +02003686
Dario Faggioliaab03e02013-11-28 11:14:43 +01003687 if (policy != SCHED_DEADLINE &&
3688 policy != SCHED_FIFO && policy != SCHED_RR &&
Lennart Poetteringca94c442009-06-15 17:17:47 +02003689 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
3690 policy != SCHED_IDLE)
3691 return -EINVAL;
3692 }
3693
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003694 if (attr->sched_flags & ~(SCHED_FLAG_RESET_ON_FORK))
3695 return -EINVAL;
3696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 /*
3698 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02003699 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
3700 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 */
Peter Zijlstra0bb040a2014-01-15 17:15:13 +01003702 if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
Dario Faggiolid50dde52013-11-07 14:43:36 +01003703 (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return -EINVAL;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003705 if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
3706 (rt_policy(policy) != (attr->sched_priority != 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 return -EINVAL;
3708
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003709 /*
3710 * Allow unprivileged RT tasks to decrease priority:
3711 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10003712 if (user && !capable(CAP_SYS_NICE)) {
Dario Faggiolid50dde52013-11-07 14:43:36 +01003713 if (fair_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003714 if (attr->sched_nice < task_nice(p) &&
Peter Zijlstraeaad4512014-01-16 17:54:25 +01003715 !can_nice(p, attr->sched_nice))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003716 return -EPERM;
3717 }
3718
Ingo Molnare05606d2007-07-09 18:51:59 +02003719 if (rt_policy(policy)) {
Oleg Nesterova44702e82010-06-11 01:09:44 +02003720 unsigned long rlim_rtprio =
3721 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003722
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003723 /* can't set/change the rt policy */
3724 if (policy != p->policy && !rlim_rtprio)
3725 return -EPERM;
3726
3727 /* can't increase priority */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003728 if (attr->sched_priority > p->rt_priority &&
3729 attr->sched_priority > rlim_rtprio)
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003730 return -EPERM;
3731 }
Darren Hartc02aa732011-02-17 15:37:07 -08003732
Juri Lellid44753b2014-03-03 12:09:21 +01003733 /*
3734 * Can't set/change SCHED_DEADLINE policy at all for now
3735 * (safest behavior); in the future we would like to allow
3736 * unprivileged DL tasks to increase their relative deadline
3737 * or reduce their runtime (both ways reducing utilization)
3738 */
3739 if (dl_policy(policy))
3740 return -EPERM;
3741
Ingo Molnardd41f592007-07-09 18:51:59 +02003742 /*
Darren Hartc02aa732011-02-17 15:37:07 -08003743 * Treat SCHED_IDLE as nice 20. Only allow a switch to
3744 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02003745 */
Darren Hartc02aa732011-02-17 15:37:07 -08003746 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003747 if (!can_nice(p, task_nice(p)))
Darren Hartc02aa732011-02-17 15:37:07 -08003748 return -EPERM;
3749 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003750
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003751 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11003752 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003753 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003754
3755 /* Normal users shall not reset the sched_reset_on_fork flag */
3756 if (p->sched_reset_on_fork && !reset_on_fork)
3757 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003760 if (user) {
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003761 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003762 if (retval)
3763 return retval;
3764 }
3765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07003767 * make sure no PI-waiters arrive (or leave) while we are
3768 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003769 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003770 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 * runqueue lock must be held.
3772 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003773 rq = task_rq_lock(p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003774
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003775 /*
3776 * Changing the policy of the stop threads its a very bad idea
3777 */
3778 if (p == rq->stop) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003779 task_rq_unlock(rq, p, &flags);
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003780 return -EINVAL;
3781 }
3782
Dario Faggiolia51e9192011-03-24 14:00:18 +01003783 /*
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003784 * If not changing anything there's no need to proceed further,
3785 * but store a possible modification of reset_on_fork.
Dario Faggiolia51e9192011-03-24 14:00:18 +01003786 */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003787 if (unlikely(policy == p->policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003788 if (fair_policy(policy) && attr->sched_nice != task_nice(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003789 goto change;
3790 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
3791 goto change;
Wanpeng Li75381602014-11-26 08:44:04 +08003792 if (dl_policy(policy) && dl_param_changed(p, attr))
Dario Faggioliaab03e02013-11-28 11:14:43 +01003793 goto change;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003794
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003795 p->sched_reset_on_fork = reset_on_fork;
Namhyung Kim45afb172012-07-07 16:49:02 +09003796 task_rq_unlock(rq, p, &flags);
Dario Faggiolia51e9192011-03-24 14:00:18 +01003797 return 0;
3798 }
Dario Faggiolid50dde52013-11-07 14:43:36 +01003799change:
Dario Faggiolia51e9192011-03-24 14:00:18 +01003800
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003801 if (user) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003802#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003803 /*
3804 * Do not allow realtime tasks into groups that have no runtime
3805 * assigned.
3806 */
3807 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01003808 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
3809 !task_group_is_autogroup(task_group(p))) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003810 task_rq_unlock(rq, p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003811 return -EPERM;
3812 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003813#endif
Dario Faggioli332ac172013-11-07 14:43:45 +01003814#ifdef CONFIG_SMP
3815 if (dl_bandwidth_enabled() && dl_policy(policy)) {
3816 cpumask_t *span = rq->rd->span;
Dario Faggioli332ac172013-11-07 14:43:45 +01003817
3818 /*
3819 * Don't allow tasks with an affinity mask smaller than
3820 * the entire root_domain to become SCHED_DEADLINE. We
3821 * will also fail if there's no bandwidth available.
3822 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01003823 if (!cpumask_subset(span, &p->cpus_allowed) ||
3824 rq->rd->dl_bw.bw == 0) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003825 task_rq_unlock(rq, p, &flags);
3826 return -EPERM;
3827 }
3828 }
3829#endif
3830 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 /* recheck policy now with rq lock held */
3833 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
3834 policy = oldpolicy = -1;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003835 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 goto recheck;
3837 }
Dario Faggioli332ac172013-11-07 14:43:45 +01003838
3839 /*
3840 * If setscheduling to SCHED_DEADLINE (or changing the parameters
3841 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
3842 * is available.
3843 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01003844 if ((dl_policy(policy) || dl_task(p)) && dl_overflow(p, policy, attr)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003845 task_rq_unlock(rq, p, &flags);
3846 return -EBUSY;
3847 }
3848
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003849 p->sched_reset_on_fork = reset_on_fork;
3850 oldprio = p->prio;
3851
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003852 if (pi) {
3853 /*
3854 * Take priority boosted tasks into account. If the new
3855 * effective priority is unchanged, we just store the new
3856 * normal parameters and do not touch the scheduler class and
3857 * the runqueue. This will be done when the task deboost
3858 * itself.
3859 */
3860 new_effective_prio = rt_mutex_get_effective_prio(p, newprio);
3861 if (new_effective_prio == oldprio) {
3862 __setscheduler_params(p, attr);
3863 task_rq_unlock(rq, p, &flags);
3864 return 0;
3865 }
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003866 }
3867
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003868 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003869 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003870 if (queued)
Peter Zijlstra4ca9b722012-01-25 11:50:51 +01003871 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003872 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003873 put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02003874
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003875 prev_class = p->sched_class;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003876 __setscheduler(rq, p, attr, pi);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02003877
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003878 if (running)
3879 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003880 if (queued) {
Thomas Gleixner81a44c52014-02-07 20:58:41 +01003881 /*
3882 * We enqueue to tail when the priority of a task is
3883 * increased (user space view).
3884 */
3885 enqueue_task(rq, p, oldprio <= p->prio ? ENQUEUE_HEAD : 0);
3886 }
Steven Rostedtcb469842008-01-25 21:08:22 +01003887
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003888 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003889 preempt_disable(); /* avoid rq from going away on us */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003890 task_rq_unlock(rq, p, &flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003891
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003892 if (pi)
3893 rt_mutex_adjust_pi(p);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07003894
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003895 /*
3896 * Run balance callbacks after we've adjusted the PI chain.
3897 */
3898 balance_callback(rq);
3899 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
3901 return 0;
3902}
Rusty Russell961ccdd2008-06-23 13:55:38 +10003903
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003904static int _sched_setscheduler(struct task_struct *p, int policy,
3905 const struct sched_param *param, bool check)
3906{
3907 struct sched_attr attr = {
3908 .sched_policy = policy,
3909 .sched_priority = param->sched_priority,
3910 .sched_nice = PRIO_TO_NICE(p->static_prio),
3911 };
3912
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003913 /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
3914 if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003915 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
3916 policy &= ~SCHED_RESET_ON_FORK;
3917 attr.sched_policy = policy;
3918 }
3919
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003920 return __sched_setscheduler(p, &attr, check, true);
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003921}
Rusty Russell961ccdd2008-06-23 13:55:38 +10003922/**
3923 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
3924 * @p: the task in question.
3925 * @policy: new policy.
3926 * @param: structure containing the new RT priority.
3927 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003928 * Return: 0 on success. An error code otherwise.
3929 *
Rusty Russell961ccdd2008-06-23 13:55:38 +10003930 * NOTE that the task may be already dead.
3931 */
3932int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07003933 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10003934{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003935 return _sched_setscheduler(p, policy, param, true);
Rusty Russell961ccdd2008-06-23 13:55:38 +10003936}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937EXPORT_SYMBOL_GPL(sched_setscheduler);
3938
Dario Faggiolid50dde52013-11-07 14:43:36 +01003939int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
3940{
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003941 return __sched_setscheduler(p, attr, true, true);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003942}
3943EXPORT_SYMBOL_GPL(sched_setattr);
3944
Rusty Russell961ccdd2008-06-23 13:55:38 +10003945/**
3946 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
3947 * @p: the task in question.
3948 * @policy: new policy.
3949 * @param: structure containing the new RT priority.
3950 *
3951 * Just like sched_setscheduler, only don't bother checking if the
3952 * current context has permission. For example, this is needed in
3953 * stop_machine(): we create temporary high priority worker threads,
3954 * but our caller might not have that capability.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003955 *
3956 * Return: 0 on success. An error code otherwise.
Rusty Russell961ccdd2008-06-23 13:55:38 +10003957 */
3958int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07003959 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10003960{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003961 return _sched_setscheduler(p, policy, param, false);
Rusty Russell961ccdd2008-06-23 13:55:38 +10003962}
3963
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003964static int
3965do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 struct sched_param lparam;
3968 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003969 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 if (!param || pid < 0)
3972 return -EINVAL;
3973 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
3974 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003975
3976 rcu_read_lock();
3977 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003979 if (p != NULL)
3980 retval = sched_setscheduler(p, policy, &lparam);
3981 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07003982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return retval;
3984}
3985
Dario Faggiolid50dde52013-11-07 14:43:36 +01003986/*
3987 * Mimics kernel/events/core.c perf_copy_attr().
3988 */
3989static int sched_copy_attr(struct sched_attr __user *uattr,
3990 struct sched_attr *attr)
3991{
3992 u32 size;
3993 int ret;
3994
3995 if (!access_ok(VERIFY_WRITE, uattr, SCHED_ATTR_SIZE_VER0))
3996 return -EFAULT;
3997
3998 /*
3999 * zero the full structure, so that a short copy will be nice.
4000 */
4001 memset(attr, 0, sizeof(*attr));
4002
4003 ret = get_user(size, &uattr->size);
4004 if (ret)
4005 return ret;
4006
4007 if (size > PAGE_SIZE) /* silly large */
4008 goto err_size;
4009
4010 if (!size) /* abi compat */
4011 size = SCHED_ATTR_SIZE_VER0;
4012
4013 if (size < SCHED_ATTR_SIZE_VER0)
4014 goto err_size;
4015
4016 /*
4017 * If we're handed a bigger struct than we know of,
4018 * ensure all the unknown bits are 0 - i.e. new
4019 * user-space does not rely on any kernel feature
4020 * extensions we dont know about yet.
4021 */
4022 if (size > sizeof(*attr)) {
4023 unsigned char __user *addr;
4024 unsigned char __user *end;
4025 unsigned char val;
4026
4027 addr = (void __user *)uattr + sizeof(*attr);
4028 end = (void __user *)uattr + size;
4029
4030 for (; addr < end; addr++) {
4031 ret = get_user(val, addr);
4032 if (ret)
4033 return ret;
4034 if (val)
4035 goto err_size;
4036 }
4037 size = sizeof(*attr);
4038 }
4039
4040 ret = copy_from_user(attr, uattr, size);
4041 if (ret)
4042 return -EFAULT;
4043
4044 /*
4045 * XXX: do we want to be lenient like existing syscalls; or do we want
4046 * to be strict and return an error on out-of-bounds values?
4047 */
Dongsheng Yang75e45d52014-02-11 15:34:50 +08004048 attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004049
Michael Kerriske78c7bc2014-05-09 16:54:28 +02004050 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004051
4052err_size:
4053 put_user(sizeof(*attr), &uattr->size);
Michael Kerriske78c7bc2014-05-09 16:54:28 +02004054 return -E2BIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004055}
4056
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057/**
4058 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4059 * @pid: the pid in question.
4060 * @policy: new policy.
4061 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004062 *
4063 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004065SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
4066 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
Jason Baronc21761f2006-01-18 17:43:03 -08004068 /* negative values for policy are not valid */
4069 if (policy < 0)
4070 return -EINVAL;
4071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return do_sched_setscheduler(pid, policy, param);
4073}
4074
4075/**
4076 * sys_sched_setparam - set/change the RT priority of a thread
4077 * @pid: the pid in question.
4078 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004079 *
4080 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004082SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
Steven Rostedtc13db6b2014-07-23 11:28:26 -04004084 return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085}
4086
4087/**
Dario Faggiolid50dde52013-11-07 14:43:36 +01004088 * sys_sched_setattr - same as above, but with extended sched_attr
4089 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01004090 * @uattr: structure containing the extended parameters.
Masanari Iidadb66d752014-04-18 01:59:15 +09004091 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004092 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004093SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
4094 unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004095{
4096 struct sched_attr attr;
4097 struct task_struct *p;
4098 int retval;
4099
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004100 if (!uattr || pid < 0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004101 return -EINVAL;
4102
Michael Kerrisk143cf232014-05-09 16:54:15 +02004103 retval = sched_copy_attr(uattr, &attr);
4104 if (retval)
4105 return retval;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004106
Richard Weinbergerb14ed2c2014-06-02 22:38:34 +02004107 if ((int)attr.sched_policy < 0)
Peter Zijlstradbdb2272014-05-09 10:49:03 +02004108 return -EINVAL;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004109
4110 rcu_read_lock();
4111 retval = -ESRCH;
4112 p = find_process_by_pid(pid);
4113 if (p != NULL)
4114 retval = sched_setattr(p, &attr);
4115 rcu_read_unlock();
4116
4117 return retval;
4118}
4119
4120/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4122 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004123 *
4124 * Return: On success, the policy of the thread. Otherwise, a negative error
4125 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004127SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004129 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004130 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
4132 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004133 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
4135 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004136 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 p = find_process_by_pid(pid);
4138 if (p) {
4139 retval = security_task_getscheduler(p);
4140 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02004141 retval = p->policy
4142 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004144 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 return retval;
4146}
4147
4148/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02004149 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 * @pid: the pid in question.
4151 * @param: structure containing the RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004152 *
4153 * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
4154 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004156SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02004158 struct sched_param lp = { .sched_priority = 0 };
Ingo Molnar36c8b582006-07-03 00:25:41 -07004159 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004160 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
4162 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004163 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004165 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 p = find_process_by_pid(pid);
4167 retval = -ESRCH;
4168 if (!p)
4169 goto out_unlock;
4170
4171 retval = security_task_getscheduler(p);
4172 if (retval)
4173 goto out_unlock;
4174
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02004175 if (task_has_rt_policy(p))
4176 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004177 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
4179 /*
4180 * This one might sleep, we cannot do it with a spinlock held ...
4181 */
4182 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4183
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 return retval;
4185
4186out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004187 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 return retval;
4189}
4190
Dario Faggiolid50dde52013-11-07 14:43:36 +01004191static int sched_read_attr(struct sched_attr __user *uattr,
4192 struct sched_attr *attr,
4193 unsigned int usize)
4194{
4195 int ret;
4196
4197 if (!access_ok(VERIFY_WRITE, uattr, usize))
4198 return -EFAULT;
4199
4200 /*
4201 * If we're handed a smaller struct than we know of,
4202 * ensure all the unknown bits are 0 - i.e. old
4203 * user-space does not get uncomplete information.
4204 */
4205 if (usize < sizeof(*attr)) {
4206 unsigned char *addr;
4207 unsigned char *end;
4208
4209 addr = (void *)attr + usize;
4210 end = (void *)attr + sizeof(*attr);
4211
4212 for (; addr < end; addr++) {
4213 if (*addr)
Michael Kerrisk22400672014-05-09 16:54:33 +02004214 return -EFBIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004215 }
4216
4217 attr->size = usize;
4218 }
4219
Vegard Nossum4efbc452014-02-16 22:24:17 +01004220 ret = copy_to_user(uattr, attr, attr->size);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004221 if (ret)
4222 return -EFAULT;
4223
Michael Kerrisk22400672014-05-09 16:54:33 +02004224 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004225}
4226
4227/**
Dario Faggioliaab03e02013-11-28 11:14:43 +01004228 * sys_sched_getattr - similar to sched_getparam, but with sched_attr
Dario Faggiolid50dde52013-11-07 14:43:36 +01004229 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01004230 * @uattr: structure containing the extended parameters.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004231 * @size: sizeof(attr) for fwd/bwd comp.
Masanari Iidadb66d752014-04-18 01:59:15 +09004232 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004233 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004234SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
4235 unsigned int, size, unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004236{
4237 struct sched_attr attr = {
4238 .size = sizeof(struct sched_attr),
4239 };
4240 struct task_struct *p;
4241 int retval;
4242
4243 if (!uattr || pid < 0 || size > PAGE_SIZE ||
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004244 size < SCHED_ATTR_SIZE_VER0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004245 return -EINVAL;
4246
4247 rcu_read_lock();
4248 p = find_process_by_pid(pid);
4249 retval = -ESRCH;
4250 if (!p)
4251 goto out_unlock;
4252
4253 retval = security_task_getscheduler(p);
4254 if (retval)
4255 goto out_unlock;
4256
4257 attr.sched_policy = p->policy;
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004258 if (p->sched_reset_on_fork)
4259 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004260 if (task_has_dl_policy(p))
4261 __getparam_dl(p, &attr);
4262 else if (task_has_rt_policy(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01004263 attr.sched_priority = p->rt_priority;
4264 else
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05004265 attr.sched_nice = task_nice(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004266
4267 rcu_read_unlock();
4268
4269 retval = sched_read_attr(uattr, &attr, size);
4270 return retval;
4271
4272out_unlock:
4273 rcu_read_unlock();
4274 return retval;
4275}
4276
Rusty Russell96f874e22008-11-25 02:35:14 +10304277long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304279 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004280 struct task_struct *p;
4281 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004283 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 p = find_process_by_pid(pid);
4286 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004287 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 return -ESRCH;
4289 }
4290
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004291 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004293 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Tejun Heo14a40ff2013-03-19 13:45:20 -07004295 if (p->flags & PF_NO_SETAFFINITY) {
4296 retval = -EINVAL;
4297 goto out_put_task;
4298 }
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304299 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
4300 retval = -ENOMEM;
4301 goto out_put_task;
4302 }
4303 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
4304 retval = -ENOMEM;
4305 goto out_free_cpus_allowed;
4306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 retval = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004308 if (!check_same_owner(p)) {
4309 rcu_read_lock();
4310 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
4311 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004312 goto out_free_new_mask;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004313 }
4314 rcu_read_unlock();
4315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004317 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07004318 if (retval)
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004319 goto out_free_new_mask;
David Quigleye7834f82006-06-23 02:03:59 -07004320
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004321
4322 cpuset_cpus_allowed(p, cpus_allowed);
4323 cpumask_and(new_mask, in_mask, cpus_allowed);
4324
Dario Faggioli332ac172013-11-07 14:43:45 +01004325 /*
4326 * Since bandwidth control happens on root_domain basis,
4327 * if admission test is enabled, we only admit -deadline
4328 * tasks allowed to run on all the CPUs in the task's
4329 * root_domain.
4330 */
4331#ifdef CONFIG_SMP
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004332 if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
4333 rcu_read_lock();
4334 if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01004335 retval = -EBUSY;
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004336 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004337 goto out_free_new_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01004338 }
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004339 rcu_read_unlock();
Dario Faggioli332ac172013-11-07 14:43:45 +01004340 }
4341#endif
Peter Zijlstra49246272010-10-17 21:46:10 +02004342again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304343 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Paul Menage8707d8b2007-10-18 23:40:22 -07004345 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304346 cpuset_cpus_allowed(p, cpus_allowed);
4347 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07004348 /*
4349 * We must have raced with a concurrent cpuset
4350 * update. Just reset the cpus_allowed to the
4351 * cpuset's cpus_allowed
4352 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304353 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004354 goto again;
4355 }
4356 }
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004357out_free_new_mask:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304358 free_cpumask_var(new_mask);
4359out_free_cpus_allowed:
4360 free_cpumask_var(cpus_allowed);
4361out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 return retval;
4364}
4365
4366static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e22008-11-25 02:35:14 +10304367 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
Rusty Russell96f874e22008-11-25 02:35:14 +10304369 if (len < cpumask_size())
4370 cpumask_clear(new_mask);
4371 else if (len > cpumask_size())
4372 len = cpumask_size();
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4375}
4376
4377/**
4378 * sys_sched_setaffinity - set the cpu affinity of a process
4379 * @pid: pid of the process
4380 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4381 * @user_mask_ptr: user-space pointer to the new cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004382 *
4383 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004385SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
4386 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304388 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 int retval;
4390
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304391 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4392 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304394 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
4395 if (retval == 0)
4396 retval = sched_setaffinity(pid, new_mask);
4397 free_cpumask_var(new_mask);
4398 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399}
4400
Rusty Russell96f874e22008-11-25 02:35:14 +10304401long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004403 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00004404 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004407 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
4409 retval = -ESRCH;
4410 p = find_process_by_pid(pid);
4411 if (!p)
4412 goto out_unlock;
4413
David Quigleye7834f82006-06-23 02:03:59 -07004414 retval = security_task_getscheduler(p);
4415 if (retval)
4416 goto out_unlock;
4417
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004418 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02004419 cpumask_and(mask, &p->cpus_allowed, cpu_active_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004420 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
4422out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004423 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Ulrich Drepper9531b622007-08-09 11:16:46 +02004425 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
4427
4428/**
4429 * sys_sched_getaffinity - get the cpu affinity of a process
4430 * @pid: pid of the process
4431 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4432 * @user_mask_ptr: user-space pointer to hold the current cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004433 *
4434 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004436SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
4437 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
4439 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10304440 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Anton Blanchard84fba5e2010-04-06 17:02:19 +10004442 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004443 return -EINVAL;
4444 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 return -EINVAL;
4446
Rusty Russellf17c8602008-11-25 02:35:11 +10304447 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
4448 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Rusty Russellf17c8602008-11-25 02:35:11 +10304450 ret = sched_getaffinity(pid, mask);
4451 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09004452 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004453
4454 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10304455 ret = -EFAULT;
4456 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004457 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10304458 }
4459 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Rusty Russellf17c8602008-11-25 02:35:11 +10304461 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
4464/**
4465 * sys_sched_yield - yield the current processor to other threads.
4466 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004467 * This function yields the current CPU to other tasks. If there are no
4468 * other threads running on this CPU then this function will return.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004469 *
4470 * Return: 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004472SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004474 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Ingo Molnar2d723762007-10-15 17:00:12 +02004476 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004477 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 /*
4480 * Since we are going to call schedule() anyway, there's
4481 * no need to preempt or enable interrupts:
4482 */
4483 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004484 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01004485 do_raw_spin_unlock(&rq->lock);
Thomas Gleixnerba74c142011-03-21 13:32:17 +01004486 sched_preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
4488 schedule();
4489
4490 return 0;
4491}
4492
Herbert Xu02b67cc32008-01-25 21:08:28 +01004493int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004495 if (should_resched()) {
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004496 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 return 1;
4498 }
4499 return 0;
4500}
Herbert Xu02b67cc32008-01-25 21:08:28 +01004501EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
4503/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004504 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 * call schedule, and on return reacquire the lock.
4506 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004507 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 * operations here to prevent schedule() from being called twice (once via
4509 * spin_unlock(), once by hand).
4510 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004511int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004513 int resched = should_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07004514 int ret = 0;
4515
Peter Zijlstraf607c662009-07-20 19:16:29 +02004516 lockdep_assert_held(lock);
4517
Paul E. McKenney4a81e832014-06-20 16:49:01 -07004518 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004520 if (resched)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004521 preempt_schedule_common();
Nick Piggin95c354f2008-01-30 13:31:20 +01004522 else
4523 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004524 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004529EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004531int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532{
4533 BUG_ON(!in_softirq());
4534
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004535 if (should_resched()) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004536 local_bh_enable();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004537 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 local_bh_disable();
4539 return 1;
4540 }
4541 return 0;
4542}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004543EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545/**
4546 * yield - yield the current processor to other threads.
4547 *
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01004548 * Do not ever use this function, there's a 99% chance you're doing it wrong.
4549 *
4550 * The scheduler is at all times free to pick the calling task as the most
4551 * eligible task to run, if removing the yield() call from your code breaks
4552 * it, its already broken.
4553 *
4554 * Typical broken usage is:
4555 *
4556 * while (!event)
4557 * yield();
4558 *
4559 * where one assumes that yield() will let 'the other' process run that will
4560 * make event true. If the current task is a SCHED_FIFO task that will never
4561 * happen. Never use yield() as a progress guarantee!!
4562 *
4563 * If you want to use yield() to wait for something, use wait_event().
4564 * If you want to use yield() to be 'nice' for others, use cond_resched().
4565 * If you still want to use yield(), do not!
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 */
4567void __sched yield(void)
4568{
4569 set_current_state(TASK_RUNNING);
4570 sys_sched_yield();
4571}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572EXPORT_SYMBOL(yield);
4573
Mike Galbraithd95f4122011-02-01 09:50:51 -05004574/**
4575 * yield_to - yield the current processor to another thread in
4576 * your thread group, or accelerate that thread toward the
4577 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07004578 * @p: target task
4579 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05004580 *
4581 * It's the caller's job to ensure that the target task struct
4582 * can't go away on us before we can do any checks.
4583 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004584 * Return:
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304585 * true (>0) if we indeed boosted the target task.
4586 * false (0) if we failed to boost the target.
4587 * -ESRCH if there's no task to yield to.
Mike Galbraithd95f4122011-02-01 09:50:51 -05004588 */
Dan Carpenterfa933842014-05-23 13:20:42 +03004589int __sched yield_to(struct task_struct *p, bool preempt)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004590{
4591 struct task_struct *curr = current;
4592 struct rq *rq, *p_rq;
4593 unsigned long flags;
Dan Carpenterc3c18642013-02-05 14:37:51 +03004594 int yielded = 0;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004595
4596 local_irq_save(flags);
4597 rq = this_rq();
4598
4599again:
4600 p_rq = task_rq(p);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304601 /*
4602 * If we're the only runnable task on the rq and target rq also
4603 * has only one task, there's absolutely no point in yielding.
4604 */
4605 if (rq->nr_running == 1 && p_rq->nr_running == 1) {
4606 yielded = -ESRCH;
4607 goto out_irq;
4608 }
4609
Mike Galbraithd95f4122011-02-01 09:50:51 -05004610 double_rq_lock(rq, p_rq);
Shigeru Yoshida39e24d8f2013-11-23 18:38:01 +09004611 if (task_rq(p) != p_rq) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004612 double_rq_unlock(rq, p_rq);
4613 goto again;
4614 }
4615
4616 if (!curr->sched_class->yield_to_task)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304617 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004618
4619 if (curr->sched_class != p->sched_class)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304620 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004621
4622 if (task_running(p_rq, p) || p->state)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304623 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004624
4625 yielded = curr->sched_class->yield_to_task(rq, p, preempt);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004626 if (yielded) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004627 schedstat_inc(rq, yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004628 /*
4629 * Make p's CPU reschedule; pick_next_entity takes care of
4630 * fairness.
4631 */
4632 if (preempt && rq != p_rq)
Kirill Tkhai88751252014-06-29 00:03:57 +04004633 resched_curr(p_rq);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004634 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05004635
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304636out_unlock:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004637 double_rq_unlock(rq, p_rq);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304638out_irq:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004639 local_irq_restore(flags);
4640
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304641 if (yielded > 0)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004642 schedule();
4643
4644 return yielded;
4645}
4646EXPORT_SYMBOL_GPL(yield_to);
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004649 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652long __sched io_schedule_timeout(long timeout)
4653{
NeilBrown9cff8ad2015-02-13 15:49:17 +11004654 int old_iowait = current->in_iowait;
4655 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 long ret;
4657
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004658 current->in_iowait = 1;
Shaohua Li10d784e2015-05-08 10:51:29 -07004659 blk_schedule_flush_plug(current);
NeilBrown9cff8ad2015-02-13 15:49:17 +11004660
4661 delayacct_blkio_start();
4662 rq = raw_rq();
4663 atomic_inc(&rq->nr_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 ret = schedule_timeout(timeout);
NeilBrown9cff8ad2015-02-13 15:49:17 +11004665 current->in_iowait = old_iowait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004667 delayacct_blkio_end();
NeilBrown9cff8ad2015-02-13 15:49:17 +11004668
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return ret;
4670}
NeilBrown9cff8ad2015-02-13 15:49:17 +11004671EXPORT_SYMBOL(io_schedule_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673/**
4674 * sys_sched_get_priority_max - return maximum RT priority.
4675 * @policy: scheduling class.
4676 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004677 * Return: On success, this syscall returns the maximum
4678 * rt_priority that can be used by a given scheduling class.
4679 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004681SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682{
4683 int ret = -EINVAL;
4684
4685 switch (policy) {
4686 case SCHED_FIFO:
4687 case SCHED_RR:
4688 ret = MAX_USER_RT_PRIO-1;
4689 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004690 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004692 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004693 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 ret = 0;
4695 break;
4696 }
4697 return ret;
4698}
4699
4700/**
4701 * sys_sched_get_priority_min - return minimum RT priority.
4702 * @policy: scheduling class.
4703 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004704 * Return: On success, this syscall returns the minimum
4705 * rt_priority that can be used by a given scheduling class.
4706 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004708SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
4710 int ret = -EINVAL;
4711
4712 switch (policy) {
4713 case SCHED_FIFO:
4714 case SCHED_RR:
4715 ret = 1;
4716 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004717 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004719 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004720 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 ret = 0;
4722 }
4723 return ret;
4724}
4725
4726/**
4727 * sys_sched_rr_get_interval - return the default timeslice of a process.
4728 * @pid: pid of the process.
4729 * @interval: userspace pointer to the timeslice value.
4730 *
4731 * this syscall writes the default timeslice value of a given process
4732 * into the user-space timespec buffer. A value of '0' means infinity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004733 *
4734 * Return: On success, 0 and the timeslice is in @interval. Otherwise,
4735 * an error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01004737SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01004738 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004740 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004741 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004742 unsigned long flags;
4743 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004744 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
4747 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004748 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
4750 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004751 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 p = find_process_by_pid(pid);
4753 if (!p)
4754 goto out_unlock;
4755
4756 retval = security_task_getscheduler(p);
4757 if (retval)
4758 goto out_unlock;
4759
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004760 rq = task_rq_lock(p, &flags);
Peter Zijlstraa57beec2014-01-27 11:54:13 +01004761 time_slice = 0;
4762 if (p->sched_class->get_rr_interval)
4763 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004764 task_rq_unlock(rq, p, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004765
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004766 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004767 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004772 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 return retval;
4774}
4775
Steven Rostedt7c731e02008-05-12 21:20:41 +02004776static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004777
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004778void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 unsigned long free = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004781 int ppid;
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09004782 unsigned long state = p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09004784 if (state)
4785 state = __ffs(state) + 1;
Erik Gilling28d06862010-11-19 18:08:51 -08004786 printk(KERN_INFO "%-15.15s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004787 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02004788#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004790 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004792 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793#else
4794 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004795 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004797 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798#endif
4799#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05004800 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801#endif
Oleg Nesterova90e9842014-12-10 15:45:21 -08004802 ppid = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004803 rcu_read_lock();
Oleg Nesterova90e9842014-12-10 15:45:21 -08004804 if (pid_alive(p))
4805 ppid = task_pid_nr(rcu_dereference(p->real_parent));
Paul E. McKenney4e797522012-11-07 13:35:32 -08004806 rcu_read_unlock();
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004807 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
Paul E. McKenney4e797522012-11-07 13:35:32 -08004808 task_pid_nr(p), ppid,
David Rientjesaa47b7e2009-05-04 01:38:05 -07004809 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Tejun Heo3d1cb202013-04-30 15:27:22 -07004811 print_worker_info(KERN_INFO, p);
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01004812 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004815void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004817 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Ingo Molnar4bd77322007-07-11 21:21:47 +02004819#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004820 printk(KERN_INFO
4821 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004823 printk(KERN_INFO
4824 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02004826 rcu_read_lock();
Oleg Nesterov5d07f422014-08-13 21:19:53 +02004827 for_each_process_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 /*
4829 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004830 * console might take a lot of time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 */
4832 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07004833 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004834 sched_show_task(p);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02004835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07004837 touch_all_softlockup_watchdogs();
4838
Ingo Molnardd41f592007-07-09 18:51:59 +02004839#ifdef CONFIG_SCHED_DEBUG
4840 sysrq_sched_debug_show();
4841#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02004842 rcu_read_unlock();
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004843 /*
4844 * Only show locks if all tasks are dumped:
4845 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02004846 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004847 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
4849
Paul Gortmaker0db06282013-06-19 14:53:51 -04004850void init_idle_bootup_task(struct task_struct *idle)
Ingo Molnar1df21052007-07-09 18:51:58 +02004851{
Ingo Molnardd41f592007-07-09 18:51:59 +02004852 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02004853}
4854
Ingo Molnarf340c0d2005-06-28 16:40:42 +02004855/**
4856 * init_idle - set up an idle thread for a given CPU
4857 * @idle: task in question
4858 * @cpu: cpu the idle task belongs to
4859 *
4860 * NOTE: this function does not set the idle thread's NEED_RESCHED
4861 * flag, to make booting more robust.
4862 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004863void init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004865 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 unsigned long flags;
4867
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004868 raw_spin_lock_irqsave(&rq->lock, flags);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01004869
Rik van Riel5e1576e2013-10-07 11:29:26 +01004870 __sched_fork(0, idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01004871 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02004872 idle->se.exec_start = sched_clock();
4873
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004874 do_set_cpus_allowed(idle, cpumask_of(cpu));
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02004875 /*
4876 * We're having a chicken and egg problem, even though we are
4877 * holding rq->lock, the cpu isn't yet set to this cpu so the
4878 * lockdep check in task_group() will fail.
4879 *
4880 * Similar case to sched_fork(). / Alternatively we could
4881 * use task_rq_lock() here and obtain the other rq->lock.
4882 *
4883 * Silence PROVE_RCU
4884 */
4885 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02004886 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02004887 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 rq->curr = rq->idle = idle;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004890 idle->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02004891#if defined(CONFIG_SMP)
4892 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07004893#endif
Thomas Gleixner05fa7852009-11-17 14:28:38 +01004894 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
4896 /* Set the preempt count _outside_ the spinlocks! */
Peter Zijlstra01028742013-08-14 14:55:46 +02004897 init_idle_preempt_count(idle, cpu);
Jonathan Corbet625f2a32011-04-22 11:19:10 -06004898
Ingo Molnardd41f592007-07-09 18:51:59 +02004899 /*
4900 * The idle tasks have their own, simple scheduling class:
4901 */
4902 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05004903 ftrace_graph_init_idle_task(idle, cpu);
Frederic Weisbecker45eacc62013-05-15 22:16:32 +02004904 vtime_init_idle(idle, cpu);
Carsten Emdef1c6f1a2011-10-26 23:14:16 +02004905#if defined(CONFIG_SMP)
4906 sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
4907#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908}
4909
Juri Lellif82f8042014-10-07 09:52:11 +01004910int cpuset_cpumask_can_shrink(const struct cpumask *cur,
4911 const struct cpumask *trial)
4912{
4913 int ret = 1, trial_cpus;
4914 struct dl_bw *cur_dl_b;
4915 unsigned long flags;
4916
Mike Galbraithbb2bc552015-01-28 04:53:55 +01004917 if (!cpumask_weight(cur))
4918 return ret;
4919
Juri Lelli75e23e492014-10-28 11:54:46 +00004920 rcu_read_lock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01004921 cur_dl_b = dl_bw_of(cpumask_any(cur));
4922 trial_cpus = cpumask_weight(trial);
4923
4924 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
4925 if (cur_dl_b->bw != -1 &&
4926 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
4927 ret = 0;
4928 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00004929 rcu_read_unlock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01004930
4931 return ret;
4932}
4933
Juri Lelli7f514122014-09-19 10:22:40 +01004934int task_can_attach(struct task_struct *p,
4935 const struct cpumask *cs_cpus_allowed)
4936{
4937 int ret = 0;
4938
4939 /*
4940 * Kthreads which disallow setaffinity shouldn't be moved
4941 * to a new cpuset; we don't want to change their cpu
4942 * affinity and isolating such threads by their set of
4943 * allowed nodes is unnecessary. Thus, cpusets are not
4944 * applicable for such threads. This prevents checking for
4945 * success of set_cpus_allowed_ptr() on all attached tasks
4946 * before cpus_allowed may be changed.
4947 */
4948 if (p->flags & PF_NO_SETAFFINITY) {
4949 ret = -EINVAL;
4950 goto out;
4951 }
4952
4953#ifdef CONFIG_SMP
4954 if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
4955 cs_cpus_allowed)) {
4956 unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
4957 cs_cpus_allowed);
Juri Lelli75e23e492014-10-28 11:54:46 +00004958 struct dl_bw *dl_b;
Juri Lelli7f514122014-09-19 10:22:40 +01004959 bool overflow;
4960 int cpus;
4961 unsigned long flags;
4962
Juri Lelli75e23e492014-10-28 11:54:46 +00004963 rcu_read_lock_sched();
4964 dl_b = dl_bw_of(dest_cpu);
Juri Lelli7f514122014-09-19 10:22:40 +01004965 raw_spin_lock_irqsave(&dl_b->lock, flags);
4966 cpus = dl_bw_cpus(dest_cpu);
4967 overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
4968 if (overflow)
4969 ret = -EBUSY;
4970 else {
4971 /*
4972 * We reserve space for this task in the destination
4973 * root_domain, as we can't fail after this point.
4974 * We will free resources in the source root_domain
4975 * later on (see set_cpus_allowed_dl()).
4976 */
4977 __dl_add(dl_b, p->dl.dl_bw);
4978 }
4979 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00004980 rcu_read_unlock_sched();
Juri Lelli7f514122014-09-19 10:22:40 +01004981
4982 }
4983#endif
4984out:
4985 return ret;
4986}
4987
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Mel Gormane6628d52013-10-07 11:29:02 +01004990#ifdef CONFIG_NUMA_BALANCING
4991/* Migrate current task p to target_cpu */
4992int migrate_task_to(struct task_struct *p, int target_cpu)
4993{
4994 struct migration_arg arg = { p, target_cpu };
4995 int curr_cpu = task_cpu(p);
4996
4997 if (curr_cpu == target_cpu)
4998 return 0;
4999
5000 if (!cpumask_test_cpu(target_cpu, tsk_cpus_allowed(p)))
5001 return -EINVAL;
5002
5003 /* TODO: This is not properly updating schedstats */
5004
Mel Gorman286549d2014-01-21 15:51:03 -08005005 trace_sched_move_numa(p, curr_cpu, target_cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01005006 return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
5007}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005008
5009/*
5010 * Requeue a task on a given node and accurately track the number of NUMA
5011 * tasks on the runqueues
5012 */
5013void sched_setnuma(struct task_struct *p, int nid)
5014{
5015 struct rq *rq;
5016 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005017 bool queued, running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005018
5019 rq = task_rq_lock(p, &flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005020 queued = task_on_rq_queued(p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005021 running = task_current(rq, p);
5022
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005023 if (queued)
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005024 dequeue_task(rq, p, 0);
5025 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04005026 put_prev_task(rq, p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005027
5028 p->numa_preferred_nid = nid;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005029
5030 if (running)
5031 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005032 if (queued)
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005033 enqueue_task(rq, p, 0);
5034 task_rq_unlock(rq, p, &flags);
5035}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02005036#endif /* CONFIG_NUMA_BALANCING */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
5038#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar48f24c42006-07-03 00:25:40 -07005039/*
5040 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 * offline.
5042 */
5043void idle_task_exit(void)
5044{
5045 struct mm_struct *mm = current->active_mm;
5046
5047 BUG_ON(cpu_online(smp_processor_id()));
5048
Martin Schwidefskya53efe52012-10-26 17:17:44 +02005049 if (mm != &init_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 switch_mm(mm, &init_mm, current);
Martin Schwidefskya53efe52012-10-26 17:17:44 +02005051 finish_arch_post_lock_switch();
5052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 mmdrop(mm);
5054}
5055
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005056/*
Peter Zijlstra5d180232012-08-20 11:26:57 +02005057 * Since this CPU is going 'away' for a while, fold any nr_active delta
5058 * we might have. Assumes we're called after migrate_tasks() so that the
5059 * nr_active count is stable.
5060 *
5061 * Also see the comment "Global load-average calculations".
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005062 */
Peter Zijlstra5d180232012-08-20 11:26:57 +02005063static void calc_load_migrate(struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064{
Peter Zijlstra5d180232012-08-20 11:26:57 +02005065 long delta = calc_load_fold_active(rq);
5066 if (delta)
5067 atomic_long_add(delta, &calc_load_tasks);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005068}
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005069
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005070static void put_prev_task_fake(struct rq *rq, struct task_struct *prev)
5071{
5072}
5073
5074static const struct sched_class fake_sched_class = {
5075 .put_prev_task = put_prev_task_fake,
5076};
5077
5078static struct task_struct fake_task = {
5079 /*
5080 * Avoid pull_{rt,dl}_task()
5081 */
5082 .prio = MAX_PRIO + 1,
5083 .sched_class = &fake_sched_class,
5084};
5085
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005086/*
5087 * Migrate all tasks from the rq, sleeping tasks will be migrated by
5088 * try_to_wake_up()->select_task_rq().
5089 *
5090 * Called with rq->lock held even though we'er in stop_machine() and
5091 * there's no concurrency possible, we hold the required locks anyway
5092 * because of lock validation efforts.
5093 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005094static void migrate_tasks(struct rq *dead_rq)
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005095{
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005096 struct rq *rq = dead_rq;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005097 struct task_struct *next, *stop = rq->stop;
5098 int dest_cpu;
5099
5100 /*
5101 * Fudge the rq selection such that the below task selection loop
5102 * doesn't get stuck on the currently eligible stop task.
5103 *
5104 * We're currently inside stop_machine() and the rq is either stuck
5105 * in the stop_machine_cpu_stop() loop, or we're executing this code,
5106 * either way we should never end up calling schedule() until we're
5107 * done here.
5108 */
5109 rq->stop = NULL;
5110
Frederic Weisbecker77bd3972013-04-12 01:50:58 +02005111 /*
5112 * put_prev_task() and pick_next_task() sched
5113 * class method both need to have an up-to-date
5114 * value of rq->clock[_task]
5115 */
5116 update_rq_clock(rq);
5117
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005118 for (;;) {
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005119 /*
5120 * There's this thread running, bail when that's the only
5121 * remaining thread.
5122 */
5123 if (rq->nr_running == 1)
5124 break;
5125
Peter Zijlstracbce1a62015-06-11 14:46:54 +02005126 /*
5127 * Ensure rq->lock covers the entire task selection
5128 * until the migration.
5129 */
5130 lockdep_pin_lock(&rq->lock);
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005131 next = pick_next_task(rq, &fake_task);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005132 BUG_ON(!next);
5133 next->sched_class->put_prev_task(rq, next);
5134
5135 /* Find suitable destination for @next, with force if needed. */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005136 dest_cpu = select_fallback_rq(dead_rq->cpu, next);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005137
Peter Zijlstracbce1a62015-06-11 14:46:54 +02005138 lockdep_unpin_lock(&rq->lock);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005139 rq = __migrate_task(rq, next, dest_cpu);
5140 if (rq != dead_rq) {
5141 raw_spin_unlock(&rq->lock);
5142 rq = dead_rq;
5143 raw_spin_lock(&rq->lock);
5144 }
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005145 }
5146
5147 rq->stop = stop;
5148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149#endif /* CONFIG_HOTPLUG_CPU */
5150
Nick Piggine692ab52007-07-26 13:40:43 +02005151#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5152
5153static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005154 {
5155 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005156 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005157 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005158 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005159};
5160
5161static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005162 {
5163 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005164 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005165 .child = sd_ctl_dir,
5166 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005167 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005168};
5169
5170static struct ctl_table *sd_alloc_ctl_entry(int n)
5171{
5172 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005173 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005174
Nick Piggine692ab52007-07-26 13:40:43 +02005175 return entry;
5176}
5177
Milton Miller6382bc92007-10-15 17:00:19 +02005178static void sd_free_ctl_entry(struct ctl_table **tablep)
5179{
Milton Millercd7900762007-10-17 16:55:11 +02005180 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005181
Milton Millercd7900762007-10-17 16:55:11 +02005182 /*
5183 * In the intermediate directories, both the child directory and
5184 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005185 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005186 * static strings and all have proc handlers.
5187 */
5188 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005189 if (entry->child)
5190 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005191 if (entry->proc_handler == NULL)
5192 kfree(entry->procname);
5193 }
Milton Miller6382bc92007-10-15 17:00:19 +02005194
5195 kfree(*tablep);
5196 *tablep = NULL;
5197}
5198
Namhyung Kim201c3732012-08-16 17:03:24 +09005199static int min_load_idx = 0;
libinfd9b86d2013-04-08 14:39:12 +08005200static int max_load_idx = CPU_LOAD_IDX_MAX-1;
Namhyung Kim201c3732012-08-16 17:03:24 +09005201
Nick Piggine692ab52007-07-26 13:40:43 +02005202static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005203set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005204 const char *procname, void *data, int maxlen,
Namhyung Kim201c3732012-08-16 17:03:24 +09005205 umode_t mode, proc_handler *proc_handler,
5206 bool load_idx)
Nick Piggine692ab52007-07-26 13:40:43 +02005207{
Nick Piggine692ab52007-07-26 13:40:43 +02005208 entry->procname = procname;
5209 entry->data = data;
5210 entry->maxlen = maxlen;
5211 entry->mode = mode;
5212 entry->proc_handler = proc_handler;
Namhyung Kim201c3732012-08-16 17:03:24 +09005213
5214 if (load_idx) {
5215 entry->extra1 = &min_load_idx;
5216 entry->extra2 = &max_load_idx;
5217 }
Nick Piggine692ab52007-07-26 13:40:43 +02005218}
5219
5220static struct ctl_table *
5221sd_alloc_ctl_domain_table(struct sched_domain *sd)
5222{
Alex Shi37e6bae2014-01-23 18:39:54 +08005223 struct ctl_table *table = sd_alloc_ctl_entry(14);
Nick Piggine692ab52007-07-26 13:40:43 +02005224
Milton Millerad1cdc12007-10-15 17:00:19 +02005225 if (table == NULL)
5226 return NULL;
5227
Alexey Dobriyane0361852007-08-09 11:16:46 +02005228 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Namhyung Kim201c3732012-08-16 17:03:24 +09005229 sizeof(long), 0644, proc_doulongvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005230 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Namhyung Kim201c3732012-08-16 17:03:24 +09005231 sizeof(long), 0644, proc_doulongvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005232 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005233 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005234 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005235 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005236 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005237 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005238 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005239 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005240 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005241 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005242 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Namhyung Kim201c3732012-08-16 17:03:24 +09005243 sizeof(int), 0644, proc_dointvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005244 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Namhyung Kim201c3732012-08-16 17:03:24 +09005245 sizeof(int), 0644, proc_dointvec_minmax, false);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005246 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005247 &sd->cache_nice_tries,
Namhyung Kim201c3732012-08-16 17:03:24 +09005248 sizeof(int), 0644, proc_dointvec_minmax, false);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005249 set_table_entry(&table[10], "flags", &sd->flags,
Namhyung Kim201c3732012-08-16 17:03:24 +09005250 sizeof(int), 0644, proc_dointvec_minmax, false);
Alex Shi37e6bae2014-01-23 18:39:54 +08005251 set_table_entry(&table[11], "max_newidle_lb_cost",
5252 &sd->max_newidle_lb_cost,
5253 sizeof(long), 0644, proc_doulongvec_minmax, false);
5254 set_table_entry(&table[12], "name", sd->name,
Namhyung Kim201c3732012-08-16 17:03:24 +09005255 CORENAME_MAX_SIZE, 0444, proc_dostring, false);
Alex Shi37e6bae2014-01-23 18:39:54 +08005256 /* &table[13] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005257
5258 return table;
5259}
5260
Joe Perchesbe7002e2013-06-12 11:55:36 -07005261static struct ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005262{
5263 struct ctl_table *entry, *table;
5264 struct sched_domain *sd;
5265 int domain_num = 0, i;
5266 char buf[32];
5267
5268 for_each_domain(cpu, sd)
5269 domain_num++;
5270 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005271 if (table == NULL)
5272 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005273
5274 i = 0;
5275 for_each_domain(cpu, sd) {
5276 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005277 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005278 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005279 entry->child = sd_alloc_ctl_domain_table(sd);
5280 entry++;
5281 i++;
5282 }
5283 return table;
5284}
5285
5286static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005287static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005288{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005289 int i, cpu_num = num_possible_cpus();
Nick Piggine692ab52007-07-26 13:40:43 +02005290 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5291 char buf[32];
5292
Milton Miller73785472007-10-24 18:23:48 +02005293 WARN_ON(sd_ctl_dir[0].child);
5294 sd_ctl_dir[0].child = entry;
5295
Milton Millerad1cdc12007-10-15 17:00:19 +02005296 if (entry == NULL)
5297 return;
5298
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005299 for_each_possible_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005300 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005301 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005302 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005303 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005304 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005305 }
Milton Miller73785472007-10-24 18:23:48 +02005306
5307 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005308 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5309}
Milton Miller6382bc92007-10-15 17:00:19 +02005310
Milton Miller73785472007-10-24 18:23:48 +02005311/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005312static void unregister_sched_domain_sysctl(void)
5313{
Milton Miller73785472007-10-24 18:23:48 +02005314 if (sd_sysctl_header)
5315 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005316 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005317 if (sd_ctl_dir[0].child)
5318 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005319}
Nick Piggine692ab52007-07-26 13:40:43 +02005320#else
Milton Miller6382bc92007-10-15 17:00:19 +02005321static void register_sched_domain_sysctl(void)
5322{
5323}
5324static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005325{
5326}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02005327#endif /* CONFIG_SCHED_DEBUG && CONFIG_SYSCTL */
Nick Piggine692ab52007-07-26 13:40:43 +02005328
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005329static void set_rq_online(struct rq *rq)
5330{
5331 if (!rq->online) {
5332 const struct sched_class *class;
5333
Rusty Russellc6c49272008-11-25 02:35:05 +10305334 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005335 rq->online = 1;
5336
5337 for_each_class(class) {
5338 if (class->rq_online)
5339 class->rq_online(rq);
5340 }
5341 }
5342}
5343
5344static void set_rq_offline(struct rq *rq)
5345{
5346 if (rq->online) {
5347 const struct sched_class *class;
5348
5349 for_each_class(class) {
5350 if (class->rq_offline)
5351 class->rq_offline(rq);
5352 }
5353
Rusty Russellc6c49272008-11-25 02:35:05 +10305354 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005355 rq->online = 0;
5356 }
5357}
5358
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359/*
5360 * migration_call - callback that gets triggered when a CPU is added.
5361 * Here we can start up the necessary migration thread for the new CPU.
5362 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005363static int
Ingo Molnar48f24c42006-07-03 00:25:40 -07005364migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005366 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 unsigned long flags;
Tejun Heo969c7922010-05-06 18:49:21 +02005368 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005370 switch (action & ~CPU_TASKS_FROZEN) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005371
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 case CPU_UP_PREPARE:
Thomas Gleixnera468d382009-07-17 14:15:46 +02005373 rq->calc_load_update = calc_load_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005375
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 case CPU_ONLINE:
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005377 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005378 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005379 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305380 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005381
5382 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005383 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005384 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005386
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387#ifdef CONFIG_HOTPLUG_CPU
Gregory Haskins08f503b2008-03-10 17:59:11 -04005388 case CPU_DYING:
Peter Zijlstra317f3942011-04-05 17:23:58 +02005389 sched_ttwu_pending();
Gregory Haskins57d885f2008-01-25 21:08:18 +01005390 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005391 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005392 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305393 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005394 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005395 }
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005396 migrate_tasks(rq);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005397 BUG_ON(rq->nr_running != 1); /* the migration thread */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005398 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra5d180232012-08-20 11:26:57 +02005399 break;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005400
Peter Zijlstra5d180232012-08-20 11:26:57 +02005401 case CPU_DEAD:
Peter Zijlstraf319da02012-08-20 11:26:57 +02005402 calc_load_migrate(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404#endif
5405 }
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005406
5407 update_max_interval();
5408
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 return NOTIFY_OK;
5410}
5411
Paul Mackerrasf38b0822009-06-02 21:05:16 +10005412/*
5413 * Register at high priority so that task migration (migrate_all_tasks)
5414 * happens before everything else. This has to be lower priority than
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005415 * the notifier in the perf_event subsystem, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005417static struct notifier_block migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 .notifier_call = migration_call,
Tejun Heo50a323b2010-06-08 21:40:36 +02005419 .priority = CPU_PRI_MIGRATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420};
5421
Paul Gortmaker6a82b602015-04-27 18:47:50 -04005422static void set_cpu_rq_start_time(void)
Corey Minyarda803f022014-05-08 13:47:39 -05005423{
5424 int cpu = smp_processor_id();
5425 struct rq *rq = cpu_rq(cpu);
5426 rq->age_stamp = sched_clock_cpu(cpu);
5427}
5428
Paul Gortmaker0db06282013-06-19 14:53:51 -04005429static int sched_cpu_active(struct notifier_block *nfb,
Tejun Heo3a101d02010-06-08 21:40:36 +02005430 unsigned long action, void *hcpu)
5431{
5432 switch (action & ~CPU_TASKS_FROZEN) {
Corey Minyarda803f022014-05-08 13:47:39 -05005433 case CPU_STARTING:
5434 set_cpu_rq_start_time();
5435 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02005436 case CPU_DOWN_FAILED:
5437 set_cpu_active((long)hcpu, true);
5438 return NOTIFY_OK;
5439 default:
5440 return NOTIFY_DONE;
5441 }
5442}
5443
Paul Gortmaker0db06282013-06-19 14:53:51 -04005444static int sched_cpu_inactive(struct notifier_block *nfb,
Tejun Heo3a101d02010-06-08 21:40:36 +02005445 unsigned long action, void *hcpu)
5446{
5447 switch (action & ~CPU_TASKS_FROZEN) {
5448 case CPU_DOWN_PREPARE:
Juri Lelli3c18d442015-03-31 09:53:37 +01005449 set_cpu_active((long)hcpu, false);
Tejun Heo3a101d02010-06-08 21:40:36 +02005450 return NOTIFY_OK;
Juri Lelli3c18d442015-03-31 09:53:37 +01005451 default:
5452 return NOTIFY_DONE;
Tejun Heo3a101d02010-06-08 21:40:36 +02005453 }
5454}
5455
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005456static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457{
5458 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005459 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005460
Tejun Heo3a101d02010-06-08 21:40:36 +02005461 /* Initialize migration for the boot CPU */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005462 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5463 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5465 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005466
Tejun Heo3a101d02010-06-08 21:40:36 +02005467 /* Register cpu active notifiers */
5468 cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
5469 cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
5470
Thomas Gleixnera004cd42009-07-21 09:54:05 +02005471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005473early_initcall(migration_init);
Christoph Lameter476f3532007-05-06 14:48:58 -07005474
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005475static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
5476
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005477#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005478
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005479static __read_mostly int sched_debug_enabled;
Mike Travisf6630112009-11-17 18:22:15 -06005480
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005481static int __init sched_debug_setup(char *str)
Mike Travisf6630112009-11-17 18:22:15 -06005482{
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005483 sched_debug_enabled = 1;
Mike Travisf6630112009-11-17 18:22:15 -06005484
5485 return 0;
5486}
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005487early_param("sched_debug", sched_debug_setup);
5488
5489static inline bool sched_debug(void)
5490{
5491 return sched_debug_enabled;
5492}
Mike Travisf6630112009-11-17 18:22:15 -06005493
Mike Travis7c16ec52008-04-04 18:11:11 -07005494static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e22008-11-25 02:35:14 +10305495 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005496{
5497 struct sched_group *group = sd->groups;
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005498
Rusty Russell96f874e22008-11-25 02:35:14 +10305499 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005500
5501 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5502
5503 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005504 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005505 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005506 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5507 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005508 return -1;
5509 }
5510
Tejun Heo333470e2015-02-13 14:37:28 -08005511 printk(KERN_CONT "span %*pbl level %s\n",
5512 cpumask_pr_args(sched_domain_span(sd)), sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005513
Rusty Russell758b2cd2008-11-25 02:35:04 +10305514 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005515 printk(KERN_ERR "ERROR: domain->span does not contain "
5516 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005517 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10305518 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005519 printk(KERN_ERR "ERROR: domain->groups does not contain"
5520 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005521 }
5522
5523 printk(KERN_DEBUG "%*s groups:", level + 1, "");
5524 do {
5525 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005526 printk("\n");
5527 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005528 break;
5529 }
5530
Rusty Russell758b2cd2008-11-25 02:35:04 +10305531 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005532 printk(KERN_CONT "\n");
5533 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005534 break;
5535 }
5536
Peter Zijlstracb83b622012-04-17 15:49:36 +02005537 if (!(sd->flags & SD_OVERLAP) &&
5538 cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005539 printk(KERN_CONT "\n");
5540 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005541 break;
5542 }
5543
Rusty Russell758b2cd2008-11-25 02:35:04 +10305544 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005545
Tejun Heo333470e2015-02-13 14:37:28 -08005546 printk(KERN_CONT " %*pbl",
5547 cpumask_pr_args(sched_group_cpus(group)));
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005548 if (group->sgc->capacity != SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005549 printk(KERN_CONT " (cpu_capacity = %d)",
5550 group->sgc->capacity);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305551 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005552
5553 group = group->next;
5554 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005555 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005556
Rusty Russell758b2cd2008-11-25 02:35:04 +10305557 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005558 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005559
Rusty Russell758b2cd2008-11-25 02:35:04 +10305560 if (sd->parent &&
5561 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005562 printk(KERN_ERR "ERROR: parent span is not a superset "
5563 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005564 return 0;
5565}
5566
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567static void sched_domain_debug(struct sched_domain *sd, int cpu)
5568{
5569 int level = 0;
5570
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005571 if (!sched_debug_enabled)
Mike Travisf6630112009-11-17 18:22:15 -06005572 return;
5573
Nick Piggin41c7ce92005-06-25 14:57:24 -07005574 if (!sd) {
5575 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5576 return;
5577 }
5578
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5580
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005581 for (;;) {
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005582 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 level++;
5585 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005586 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005587 break;
5588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005590#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005591# define sched_domain_debug(sd, cpu) do { } while (0)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005592static inline bool sched_debug(void)
5593{
5594 return false;
5595}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005596#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005598static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005599{
Rusty Russell758b2cd2008-11-25 02:35:04 +10305600 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005601 return 1;
5602
5603 /* Following flags need at least 2 groups */
5604 if (sd->flags & (SD_LOAD_BALANCE |
5605 SD_BALANCE_NEWIDLE |
5606 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005607 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005608 SD_SHARE_CPUCAPACITY |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005609 SD_SHARE_PKG_RESOURCES |
5610 SD_SHARE_POWERDOMAIN)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005611 if (sd->groups != sd->groups->next)
5612 return 0;
5613 }
5614
5615 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005616 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005617 return 0;
5618
5619 return 1;
5620}
5621
Ingo Molnar48f24c42006-07-03 00:25:40 -07005622static int
5623sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005624{
5625 unsigned long cflags = sd->flags, pflags = parent->flags;
5626
5627 if (sd_degenerate(parent))
5628 return 1;
5629
Rusty Russell758b2cd2008-11-25 02:35:04 +10305630 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005631 return 0;
5632
Suresh Siddha245af2c2005-06-25 14:57:25 -07005633 /* Flags needing groups don't count if only 1 group in parent */
5634 if (parent->groups == parent->groups->next) {
5635 pflags &= ~(SD_LOAD_BALANCE |
5636 SD_BALANCE_NEWIDLE |
5637 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005638 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005639 SD_SHARE_CPUCAPACITY |
Peter Zijlstra10866e622013-08-19 16:57:04 +02005640 SD_SHARE_PKG_RESOURCES |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005641 SD_PREFER_SIBLING |
5642 SD_SHARE_POWERDOMAIN);
Ken Chen54364992008-12-07 18:47:37 -08005643 if (nr_node_ids == 1)
5644 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005645 }
5646 if (~cflags & pflags)
5647 return 0;
5648
5649 return 1;
5650}
5651
Peter Zijlstradce840a2011-04-07 14:09:50 +02005652static void free_rootdomain(struct rcu_head *rcu)
Rusty Russellc6c49272008-11-25 02:35:05 +10305653{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005654 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
Peter Zijlstra047106a2009-11-16 10:28:09 +01005655
Rusty Russell68e74562008-11-25 02:35:13 +10305656 cpupri_cleanup(&rd->cpupri);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005657 cpudl_cleanup(&rd->cpudl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005658 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305659 free_cpumask_var(rd->rto_mask);
5660 free_cpumask_var(rd->online);
5661 free_cpumask_var(rd->span);
5662 kfree(rd);
5663}
5664
Gregory Haskins57d885f2008-01-25 21:08:18 +01005665static void rq_attach_root(struct rq *rq, struct root_domain *rd)
5666{
Ingo Molnara0490fa2009-02-12 11:35:40 +01005667 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005668 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005669
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005670 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005671
5672 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01005673 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005674
Rusty Russellc6c49272008-11-25 02:35:05 +10305675 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005676 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005677
Rusty Russellc6c49272008-11-25 02:35:05 +10305678 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01005679
Ingo Molnara0490fa2009-02-12 11:35:40 +01005680 /*
Shigeru Yoshida05159732013-11-17 12:12:36 +09005681 * If we dont want to free the old_rd yet then
Ingo Molnara0490fa2009-02-12 11:35:40 +01005682 * set old_rd to NULL to skip the freeing later
5683 * in this function:
5684 */
5685 if (!atomic_dec_and_test(&old_rd->refcount))
5686 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005687 }
5688
5689 atomic_inc(&rd->refcount);
5690 rq->rd = rd;
5691
Rusty Russellc6c49272008-11-25 02:35:05 +10305692 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04005693 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005694 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005695
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005696 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01005697
5698 if (old_rd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02005699 call_rcu_sched(&old_rd->rcu, free_rootdomain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005700}
5701
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005702static int init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005703{
5704 memset(rd, 0, sizeof(*rd));
5705
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005706 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08005707 goto out;
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005708 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305709 goto free_span;
Juri Lelli1baca4c2013-11-07 14:43:38 +01005710 if (!alloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305711 goto free_online;
Juri Lelli1baca4c2013-11-07 14:43:38 +01005712 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
5713 goto free_dlo_mask;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02005714
Dario Faggioli332ac172013-11-07 14:43:45 +01005715 init_dl_bw(&rd->dl_bw);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005716 if (cpudl_init(&rd->cpudl) != 0)
5717 goto free_dlo_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01005718
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005719 if (cpupri_init(&rd->cpupri) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10305720 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10305721 return 0;
5722
Rusty Russell68e74562008-11-25 02:35:13 +10305723free_rto_mask:
5724 free_cpumask_var(rd->rto_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005725free_dlo_mask:
5726 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305727free_online:
5728 free_cpumask_var(rd->online);
5729free_span:
5730 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08005731out:
Rusty Russellc6c49272008-11-25 02:35:05 +10305732 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005733}
5734
Peter Zijlstra029632f2011-10-25 10:00:11 +02005735/*
5736 * By default the system creates a single root-domain with all cpus as
5737 * members (mimicking the global state we have today).
5738 */
5739struct root_domain def_root_domain;
5740
Gregory Haskins57d885f2008-01-25 21:08:18 +01005741static void init_defrootdomain(void)
5742{
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005743 init_rootdomain(&def_root_domain);
Rusty Russellc6c49272008-11-25 02:35:05 +10305744
Gregory Haskins57d885f2008-01-25 21:08:18 +01005745 atomic_set(&def_root_domain.refcount, 1);
5746}
5747
Gregory Haskinsdc938522008-01-25 21:08:26 +01005748static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005749{
5750 struct root_domain *rd;
5751
5752 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
5753 if (!rd)
5754 return NULL;
5755
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005756 if (init_rootdomain(rd) != 0) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305757 kfree(rd);
5758 return NULL;
5759 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01005760
5761 return rd;
5762}
5763
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005764static void free_sched_groups(struct sched_group *sg, int free_sgc)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005765{
5766 struct sched_group *tmp, *first;
5767
5768 if (!sg)
5769 return;
5770
5771 first = sg;
5772 do {
5773 tmp = sg->next;
5774
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005775 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
5776 kfree(sg->sgc);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005777
5778 kfree(sg);
5779 sg = tmp;
5780 } while (sg != first);
5781}
5782
Peter Zijlstradce840a2011-04-07 14:09:50 +02005783static void free_sched_domain(struct rcu_head *rcu)
5784{
5785 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005786
5787 /*
5788 * If its an overlapping domain it has private groups, iterate and
5789 * nuke them all.
5790 */
5791 if (sd->flags & SD_OVERLAP) {
5792 free_sched_groups(sd->groups, 1);
5793 } else if (atomic_dec_and_test(&sd->groups->ref)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005794 kfree(sd->groups->sgc);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005795 kfree(sd->groups);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005796 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005797 kfree(sd);
5798}
5799
5800static void destroy_sched_domain(struct sched_domain *sd, int cpu)
5801{
5802 call_rcu(&sd->rcu, free_sched_domain);
5803}
5804
5805static void destroy_sched_domains(struct sched_domain *sd, int cpu)
5806{
5807 for (; sd; sd = sd->parent)
5808 destroy_sched_domain(sd, cpu);
5809}
5810
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811/*
Peter Zijlstra518cd622011-12-07 15:07:31 +01005812 * Keep a special pointer to the highest sched_domain that has
5813 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
5814 * allows us to avoid some pointer chasing select_idle_sibling().
5815 *
5816 * Also keep a unique ID per domain (we use the first cpu number in
5817 * the cpumask of the domain), this allows us to quickly tell if
Peter Zijlstra39be3502012-01-26 12:44:34 +01005818 * two cpus are in the same cache domain, see cpus_share_cache().
Peter Zijlstra518cd622011-12-07 15:07:31 +01005819 */
5820DEFINE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005821DEFINE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005822DEFINE_PER_CPU(int, sd_llc_id);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005823DEFINE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305824DEFINE_PER_CPU(struct sched_domain *, sd_busy);
5825DEFINE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005826
5827static void update_top_cache_domain(int cpu)
5828{
5829 struct sched_domain *sd;
Mel Gorman5d4cf992013-12-17 09:21:25 +00005830 struct sched_domain *busy_sd = NULL;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005831 int id = cpu;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005832 int size = 1;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005833
5834 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005835 if (sd) {
Peter Zijlstra518cd622011-12-07 15:07:31 +01005836 id = cpumask_first(sched_domain_span(sd));
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005837 size = cpumask_weight(sched_domain_span(sd));
Mel Gorman5d4cf992013-12-17 09:21:25 +00005838 busy_sd = sd->parent; /* sd_busy */
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005839 }
Mel Gorman5d4cf992013-12-17 09:21:25 +00005840 rcu_assign_pointer(per_cpu(sd_busy, cpu), busy_sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005841
5842 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005843 per_cpu(sd_llc_size, cpu) = size;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005844 per_cpu(sd_llc_id, cpu) = id;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005845
5846 sd = lowest_flag_domain(cpu, SD_NUMA);
5847 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305848
5849 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
5850 rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005851}
5852
5853/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01005854 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 * hold the hotplug lock.
5856 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01005857static void
5858cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005860 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005861 struct sched_domain *tmp;
5862
5863 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08005864 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005865 struct sched_domain *parent = tmp->parent;
5866 if (!parent)
5867 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08005868
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005869 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005870 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005871 if (parent->parent)
5872 parent->parent->child = tmp;
Peter Zijlstra10866e622013-08-19 16:57:04 +02005873 /*
5874 * Transfer SD_PREFER_SIBLING down in case of a
5875 * degenerate parent; the spans match for this
5876 * so the property transfers.
5877 */
5878 if (parent->flags & SD_PREFER_SIBLING)
5879 tmp->flags |= SD_PREFER_SIBLING;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005880 destroy_sched_domain(parent, cpu);
Li Zefanf29c9b12008-11-06 09:45:16 +08005881 } else
5882 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005883 }
5884
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005885 if (sd && sd_degenerate(sd)) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02005886 tmp = sd;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005887 sd = sd->parent;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005888 destroy_sched_domain(tmp, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005889 if (sd)
5890 sd->child = NULL;
5891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005893 sched_domain_debug(sd, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Gregory Haskins57d885f2008-01-25 21:08:18 +01005895 rq_attach_root(rq, rd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005896 tmp = rq->sd;
Nick Piggin674311d2005-06-25 14:57:27 -07005897 rcu_assign_pointer(rq->sd, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005898 destroy_sched_domains(tmp, cpu);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005899
5900 update_top_cache_domain(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901}
5902
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903/* Setup the mask of cpus configured for isolated domains */
5904static int __init isolated_cpu_setup(char *str)
5905{
Rusty Russellbdddd292009-12-02 14:09:16 +10305906 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russell968ea6d2008-12-13 21:55:51 +10305907 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 return 1;
5909}
5910
Ingo Molnar8927f492007-10-15 17:00:13 +02005911__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
Andreas Herrmann49a02c52009-08-18 12:51:52 +02005913struct s_data {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02005914 struct sched_domain ** __percpu sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02005915 struct root_domain *rd;
5916};
5917
Andreas Herrmann2109b992009-08-18 12:53:00 +02005918enum s_alloc {
Andreas Herrmann2109b992009-08-18 12:53:00 +02005919 sa_rootdomain,
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02005920 sa_sd,
Peter Zijlstradce840a2011-04-07 14:09:50 +02005921 sa_sd_storage,
Andreas Herrmann2109b992009-08-18 12:53:00 +02005922 sa_none,
5923};
5924
Peter Zijlstrac1174872012-05-31 14:47:33 +02005925/*
5926 * Build an iteration mask that can exclude certain CPUs from the upwards
5927 * domain traversal.
5928 *
5929 * Asymmetric node setups can result in situations where the domain tree is of
5930 * unequal depth, make sure to skip domains that already cover the entire
5931 * range.
5932 *
5933 * In that case build_sched_domains() will have terminated the iteration early
5934 * and our sibling sd spans will be empty. Domains should always include the
5935 * cpu they're built on, so check that.
5936 *
5937 */
5938static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
5939{
5940 const struct cpumask *span = sched_domain_span(sd);
5941 struct sd_data *sdd = sd->private;
5942 struct sched_domain *sibling;
5943 int i;
5944
5945 for_each_cpu(i, span) {
5946 sibling = *per_cpu_ptr(sdd->sd, i);
5947 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
5948 continue;
5949
5950 cpumask_set_cpu(i, sched_group_mask(sg));
5951 }
5952}
5953
5954/*
5955 * Return the canonical balance cpu for this group, this is the first cpu
5956 * of this group that's also in the iteration mask.
5957 */
5958int group_balance_cpu(struct sched_group *sg)
5959{
5960 return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
5961}
5962
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005963static int
5964build_overlap_sched_groups(struct sched_domain *sd, int cpu)
5965{
5966 struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
5967 const struct cpumask *span = sched_domain_span(sd);
5968 struct cpumask *covered = sched_domains_tmpmask;
5969 struct sd_data *sdd = sd->private;
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005970 struct sched_domain *sibling;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005971 int i;
5972
5973 cpumask_clear(covered);
5974
5975 for_each_cpu(i, span) {
5976 struct cpumask *sg_span;
5977
5978 if (cpumask_test_cpu(i, covered))
5979 continue;
5980
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005981 sibling = *per_cpu_ptr(sdd->sd, i);
Peter Zijlstrac1174872012-05-31 14:47:33 +02005982
5983 /* See the comment near build_group_mask(). */
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005984 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
Peter Zijlstrac1174872012-05-31 14:47:33 +02005985 continue;
5986
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005987 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
Suresh Siddha4d78a222011-11-18 15:03:29 -08005988 GFP_KERNEL, cpu_to_node(cpu));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005989
5990 if (!sg)
5991 goto fail;
5992
5993 sg_span = sched_group_cpus(sg);
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005994 if (sibling->child)
5995 cpumask_copy(sg_span, sched_domain_span(sibling->child));
5996 else
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005997 cpumask_set_cpu(i, sg_span);
5998
5999 cpumask_or(covered, covered, sg_span);
6000
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006001 sg->sgc = *per_cpu_ptr(sdd->sgc, i);
6002 if (atomic_inc_return(&sg->sgc->ref) == 1)
Peter Zijlstrac1174872012-05-31 14:47:33 +02006003 build_group_mask(sd, sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006004
Peter Zijlstrac3decf02012-05-31 12:05:32 +02006005 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006006 * Initialize sgc->capacity such that even if we mess up the
Peter Zijlstrac3decf02012-05-31 12:05:32 +02006007 * domains and no possible iteration will get us here, we won't
6008 * die on a /0 trap.
6009 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006010 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006011
6012 /*
6013 * Make sure the first group of this domain contains the
6014 * canonical balance cpu. Otherwise the sched_domain iteration
6015 * breaks. See update_sg_lb_stats().
6016 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02006017 if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
Peter Zijlstrac1174872012-05-31 14:47:33 +02006018 group_balance_cpu(sg) == cpu)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006019 groups = sg;
6020
6021 if (!first)
6022 first = sg;
6023 if (last)
6024 last->next = sg;
6025 last = sg;
6026 last->next = first;
6027 }
6028 sd->groups = groups;
6029
6030 return 0;
6031
6032fail:
6033 free_sched_groups(first, 0);
6034
6035 return -ENOMEM;
6036}
6037
Peter Zijlstradce840a2011-04-07 14:09:50 +02006038static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006040 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
6041 struct sched_domain *child = sd->child;
6042
6043 if (child)
6044 cpu = cpumask_first(sched_domain_span(child));
6045
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006046 if (sg) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02006047 *sg = *per_cpu_ptr(sdd->sg, cpu);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006048 (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
6049 atomic_set(&(*sg)->sgc->ref, 1); /* for claim_allocations */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006050 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006051
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 return cpu;
6053}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
Ingo Molnar48f24c42006-07-03 00:25:40 -07006055/*
Peter Zijlstradce840a2011-04-07 14:09:50 +02006056 * build_sched_groups will build a circular linked list of the groups
6057 * covered by the given span, and will set each group's ->cpumask correctly,
Nicolas Pitreced549f2014-05-26 18:19:38 -04006058 * and ->cpu_capacity to 0.
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006059 *
6060 * Assumes the sched_domain tree is fully constructed
Ingo Molnar48f24c42006-07-03 00:25:40 -07006061 */
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006062static int
6063build_sched_groups(struct sched_domain *sd, int cpu)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006064{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006065 struct sched_group *first = NULL, *last = NULL;
6066 struct sd_data *sdd = sd->private;
6067 const struct cpumask *span = sched_domain_span(sd);
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006068 struct cpumask *covered;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006069 int i;
6070
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006071 get_group(cpu, sdd, &sd->groups);
6072 atomic_inc(&sd->groups->ref);
6073
Viresh Kumar09366292013-06-11 16:32:43 +05306074 if (cpu != cpumask_first(span))
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006075 return 0;
6076
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006077 lockdep_assert_held(&sched_domains_mutex);
6078 covered = sched_domains_tmpmask;
6079
Peter Zijlstradce840a2011-04-07 14:09:50 +02006080 cpumask_clear(covered);
6081
6082 for_each_cpu(i, span) {
6083 struct sched_group *sg;
Viresh Kumarcd08e922013-06-11 16:32:44 +05306084 int group, j;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006085
6086 if (cpumask_test_cpu(i, covered))
6087 continue;
6088
Viresh Kumarcd08e922013-06-11 16:32:44 +05306089 group = get_group(i, sdd, &sg);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006090 cpumask_setall(sched_group_mask(sg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02006091
6092 for_each_cpu(j, span) {
6093 if (get_group(j, sdd, NULL) != group)
6094 continue;
6095
6096 cpumask_set_cpu(j, covered);
6097 cpumask_set_cpu(j, sched_group_cpus(sg));
6098 }
6099
6100 if (!first)
6101 first = sg;
6102 if (last)
6103 last->next = sg;
6104 last = sg;
6105 }
6106 last->next = first;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006107
6108 return 0;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006109}
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006110
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111/*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006112 * Initialize sched groups cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006113 *
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006114 * cpu_capacity indicates the capacity of sched group, which is used while
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006115 * distributing the load between different sched groups in a sched domain.
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006116 * Typically cpu_capacity for all the groups in a sched domain will be same
6117 * unless there are asymmetries in the topology. If there are asymmetries,
6118 * group having more cpu_capacity will pickup more load compared to the
6119 * group having less cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006120 */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006121static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006122{
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006123 struct sched_group *sg = sd->groups;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006124
Viresh Kumar94c95ba2013-06-11 16:32:45 +05306125 WARN_ON(!sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006126
6127 do {
6128 sg->group_weight = cpumask_weight(sched_group_cpus(sg));
6129 sg = sg->next;
6130 } while (sg != sd->groups);
6131
Peter Zijlstrac1174872012-05-31 14:47:33 +02006132 if (cpu != group_balance_cpu(sg))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006133 return;
6134
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006135 update_group_capacity(sd, cpu);
6136 atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006137}
6138
6139/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006140 * Initializers for schedule domains
6141 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6142 */
6143
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006144static int default_relax_domain_level = -1;
Peter Zijlstra60495e72011-04-07 14:10:04 +02006145int sched_domain_level_max;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006146
6147static int __init setup_relax_domain_level(char *str)
6148{
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006149 if (kstrtoint(str, 0, &default_relax_domain_level))
6150 pr_warn("Unable to set relax_domain_level\n");
Li Zefan30e0e172008-05-13 10:27:17 +08006151
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006152 return 1;
6153}
6154__setup("relax_domain_level=", setup_relax_domain_level);
6155
6156static void set_domain_attribute(struct sched_domain *sd,
6157 struct sched_domain_attr *attr)
6158{
6159 int request;
6160
6161 if (!attr || attr->relax_domain_level < 0) {
6162 if (default_relax_domain_level < 0)
6163 return;
6164 else
6165 request = default_relax_domain_level;
6166 } else
6167 request = attr->relax_domain_level;
6168 if (request < sd->level) {
6169 /* turn off idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006170 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006171 } else {
6172 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006173 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006174 }
6175}
6176
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006177static void __sdt_free(const struct cpumask *cpu_map);
6178static int __sdt_alloc(const struct cpumask *cpu_map);
6179
Andreas Herrmann2109b992009-08-18 12:53:00 +02006180static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
6181 const struct cpumask *cpu_map)
6182{
6183 switch (what) {
Andreas Herrmann2109b992009-08-18 12:53:00 +02006184 case sa_rootdomain:
Peter Zijlstra822ff792011-04-07 14:09:51 +02006185 if (!atomic_read(&d->rd->refcount))
6186 free_rootdomain(&d->rd->rcu); /* fall through */
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006187 case sa_sd:
6188 free_percpu(d->sd); /* fall through */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006189 case sa_sd_storage:
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006190 __sdt_free(cpu_map); /* fall through */
Andreas Herrmann2109b992009-08-18 12:53:00 +02006191 case sa_none:
6192 break;
6193 }
6194}
6195
6196static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
6197 const struct cpumask *cpu_map)
6198{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006199 memset(d, 0, sizeof(*d));
6200
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006201 if (__sdt_alloc(cpu_map))
6202 return sa_sd_storage;
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006203 d->sd = alloc_percpu(struct sched_domain *);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006204 if (!d->sd)
6205 return sa_sd_storage;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006206 d->rd = alloc_rootdomain();
Peter Zijlstradce840a2011-04-07 14:09:50 +02006207 if (!d->rd)
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006208 return sa_sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006209 return sa_rootdomain;
6210}
6211
Peter Zijlstradce840a2011-04-07 14:09:50 +02006212/*
6213 * NULL the sd_data elements we've used to build the sched_domain and
6214 * sched_group structure so that the subsequent __free_domain_allocs()
6215 * will not free the data we're using.
6216 */
6217static void claim_allocations(int cpu, struct sched_domain *sd)
6218{
6219 struct sd_data *sdd = sd->private;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006220
6221 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
6222 *per_cpu_ptr(sdd->sd, cpu) = NULL;
6223
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006224 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
Peter Zijlstradce840a2011-04-07 14:09:50 +02006225 *per_cpu_ptr(sdd->sg, cpu) = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006226
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006227 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
6228 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006229}
6230
Peter Zijlstracb83b622012-04-17 15:49:36 +02006231#ifdef CONFIG_NUMA
Peter Zijlstracb83b622012-04-17 15:49:36 +02006232static int sched_domains_numa_levels;
Rik van Riele3fe70b2014-10-17 03:29:50 -04006233enum numa_topology_type sched_numa_topology_type;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006234static int *sched_domains_numa_distance;
Rik van Riel9942f792014-10-17 03:29:49 -04006235int sched_max_numa_distance;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006236static struct cpumask ***sched_domains_numa_masks;
6237static int sched_domains_curr_level;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006238#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006239
Vincent Guittot143e1e22014-04-11 11:44:37 +02006240/*
6241 * SD_flags allowed in topology descriptions.
6242 *
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006243 * SD_SHARE_CPUCAPACITY - describes SMT topologies
Vincent Guittot143e1e22014-04-11 11:44:37 +02006244 * SD_SHARE_PKG_RESOURCES - describes shared caches
6245 * SD_NUMA - describes NUMA topologies
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006246 * SD_SHARE_POWERDOMAIN - describes shared power domain
Vincent Guittot143e1e22014-04-11 11:44:37 +02006247 *
6248 * Odd one out:
6249 * SD_ASYM_PACKING - describes SMT quirks
6250 */
6251#define TOPOLOGY_SD_FLAGS \
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006252 (SD_SHARE_CPUCAPACITY | \
Vincent Guittot143e1e22014-04-11 11:44:37 +02006253 SD_SHARE_PKG_RESOURCES | \
6254 SD_NUMA | \
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006255 SD_ASYM_PACKING | \
6256 SD_SHARE_POWERDOMAIN)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006257
6258static struct sched_domain *
Vincent Guittot143e1e22014-04-11 11:44:37 +02006259sd_init(struct sched_domain_topology_level *tl, int cpu)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006260{
6261 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
Vincent Guittot143e1e22014-04-11 11:44:37 +02006262 int sd_weight, sd_flags = 0;
6263
6264#ifdef CONFIG_NUMA
6265 /*
6266 * Ugly hack to pass state to sd_numa_mask()...
6267 */
6268 sched_domains_curr_level = tl->numa_level;
6269#endif
6270
6271 sd_weight = cpumask_weight(tl->mask(cpu));
6272
6273 if (tl->sd_flags)
6274 sd_flags = (*tl->sd_flags)();
6275 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
6276 "wrong sd_flags in topology description\n"))
6277 sd_flags &= ~TOPOLOGY_SD_FLAGS;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006278
6279 *sd = (struct sched_domain){
6280 .min_interval = sd_weight,
6281 .max_interval = 2*sd_weight,
6282 .busy_factor = 32,
Peter Zijlstra870a0bb2012-05-11 00:26:27 +02006283 .imbalance_pct = 125,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006284
6285 .cache_nice_tries = 0,
6286 .busy_idx = 0,
6287 .idle_idx = 0,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006288 .newidle_idx = 0,
6289 .wake_idx = 0,
6290 .forkexec_idx = 0,
6291
6292 .flags = 1*SD_LOAD_BALANCE
6293 | 1*SD_BALANCE_NEWIDLE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006294 | 1*SD_BALANCE_EXEC
6295 | 1*SD_BALANCE_FORK
Peter Zijlstracb83b622012-04-17 15:49:36 +02006296 | 0*SD_BALANCE_WAKE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006297 | 1*SD_WAKE_AFFINE
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006298 | 0*SD_SHARE_CPUCAPACITY
Peter Zijlstracb83b622012-04-17 15:49:36 +02006299 | 0*SD_SHARE_PKG_RESOURCES
Vincent Guittot143e1e22014-04-11 11:44:37 +02006300 | 0*SD_SERIALIZE
Peter Zijlstracb83b622012-04-17 15:49:36 +02006301 | 0*SD_PREFER_SIBLING
Vincent Guittot143e1e22014-04-11 11:44:37 +02006302 | 0*SD_NUMA
6303 | sd_flags
Peter Zijlstracb83b622012-04-17 15:49:36 +02006304 ,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006305
Peter Zijlstracb83b622012-04-17 15:49:36 +02006306 .last_balance = jiffies,
6307 .balance_interval = sd_weight,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006308 .smt_gain = 0,
Jason Low2b4cfe62014-04-23 18:30:34 -07006309 .max_newidle_lb_cost = 0,
6310 .next_decay_max_lb_cost = jiffies,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006311#ifdef CONFIG_SCHED_DEBUG
6312 .name = tl->name,
6313#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006314 };
Peter Zijlstracb83b622012-04-17 15:49:36 +02006315
6316 /*
Vincent Guittot143e1e22014-04-11 11:44:37 +02006317 * Convert topological properties into behaviour.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006318 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006319
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006320 if (sd->flags & SD_SHARE_CPUCAPACITY) {
Vincent Guittotcaff37e2015-02-27 16:54:13 +01006321 sd->flags |= SD_PREFER_SIBLING;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006322 sd->imbalance_pct = 110;
6323 sd->smt_gain = 1178; /* ~15% */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006324
6325 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
6326 sd->imbalance_pct = 117;
6327 sd->cache_nice_tries = 1;
6328 sd->busy_idx = 2;
6329
6330#ifdef CONFIG_NUMA
6331 } else if (sd->flags & SD_NUMA) {
6332 sd->cache_nice_tries = 2;
6333 sd->busy_idx = 3;
6334 sd->idle_idx = 2;
6335
6336 sd->flags |= SD_SERIALIZE;
6337 if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
6338 sd->flags &= ~(SD_BALANCE_EXEC |
6339 SD_BALANCE_FORK |
6340 SD_WAKE_AFFINE);
6341 }
6342
6343#endif
6344 } else {
6345 sd->flags |= SD_PREFER_SIBLING;
6346 sd->cache_nice_tries = 1;
6347 sd->busy_idx = 2;
6348 sd->idle_idx = 1;
6349 }
6350
6351 sd->private = &tl->data;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006352
6353 return sd;
6354}
6355
Vincent Guittot143e1e22014-04-11 11:44:37 +02006356/*
6357 * Topology list, bottom-up.
6358 */
6359static struct sched_domain_topology_level default_topology[] = {
6360#ifdef CONFIG_SCHED_SMT
6361 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
6362#endif
6363#ifdef CONFIG_SCHED_MC
6364 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
6365#endif
Vincent Guittot143e1e22014-04-11 11:44:37 +02006366 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
6367 { NULL, },
6368};
6369
6370struct sched_domain_topology_level *sched_domain_topology = default_topology;
6371
6372#define for_each_sd_topology(tl) \
6373 for (tl = sched_domain_topology; tl->mask; tl++)
6374
6375void set_sched_topology(struct sched_domain_topology_level *tl)
6376{
6377 sched_domain_topology = tl;
6378}
6379
6380#ifdef CONFIG_NUMA
6381
Peter Zijlstracb83b622012-04-17 15:49:36 +02006382static const struct cpumask *sd_numa_mask(int cpu)
6383{
6384 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
6385}
6386
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006387static void sched_numa_warn(const char *str)
6388{
6389 static int done = false;
6390 int i,j;
6391
6392 if (done)
6393 return;
6394
6395 done = true;
6396
6397 printk(KERN_WARNING "ERROR: %s\n\n", str);
6398
6399 for (i = 0; i < nr_node_ids; i++) {
6400 printk(KERN_WARNING " ");
6401 for (j = 0; j < nr_node_ids; j++)
6402 printk(KERN_CONT "%02d ", node_distance(i,j));
6403 printk(KERN_CONT "\n");
6404 }
6405 printk(KERN_WARNING "\n");
6406}
6407
Rik van Riel9942f792014-10-17 03:29:49 -04006408bool find_numa_distance(int distance)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006409{
6410 int i;
6411
6412 if (distance == node_distance(0, 0))
6413 return true;
6414
6415 for (i = 0; i < sched_domains_numa_levels; i++) {
6416 if (sched_domains_numa_distance[i] == distance)
6417 return true;
6418 }
6419
6420 return false;
6421}
6422
Rik van Riele3fe70b2014-10-17 03:29:50 -04006423/*
6424 * A system can have three types of NUMA topology:
6425 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
6426 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
6427 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
6428 *
6429 * The difference between a glueless mesh topology and a backplane
6430 * topology lies in whether communication between not directly
6431 * connected nodes goes through intermediary nodes (where programs
6432 * could run), or through backplane controllers. This affects
6433 * placement of programs.
6434 *
6435 * The type of topology can be discerned with the following tests:
6436 * - If the maximum distance between any nodes is 1 hop, the system
6437 * is directly connected.
6438 * - If for two nodes A and B, located N > 1 hops away from each other,
6439 * there is an intermediary node C, which is < N hops away from both
6440 * nodes A and B, the system is a glueless mesh.
6441 */
6442static void init_numa_topology_type(void)
6443{
6444 int a, b, c, n;
6445
6446 n = sched_max_numa_distance;
6447
6448 if (n <= 1)
6449 sched_numa_topology_type = NUMA_DIRECT;
6450
6451 for_each_online_node(a) {
6452 for_each_online_node(b) {
6453 /* Find two nodes furthest removed from each other. */
6454 if (node_distance(a, b) < n)
6455 continue;
6456
6457 /* Is there an intermediary node between a and b? */
6458 for_each_online_node(c) {
6459 if (node_distance(a, c) < n &&
6460 node_distance(b, c) < n) {
6461 sched_numa_topology_type =
6462 NUMA_GLUELESS_MESH;
6463 return;
6464 }
6465 }
6466
6467 sched_numa_topology_type = NUMA_BACKPLANE;
6468 return;
6469 }
6470 }
6471}
6472
Peter Zijlstracb83b622012-04-17 15:49:36 +02006473static void sched_init_numa(void)
6474{
6475 int next_distance, curr_distance = node_distance(0, 0);
6476 struct sched_domain_topology_level *tl;
6477 int level = 0;
6478 int i, j, k;
6479
Peter Zijlstracb83b622012-04-17 15:49:36 +02006480 sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
6481 if (!sched_domains_numa_distance)
6482 return;
6483
6484 /*
6485 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
6486 * unique distances in the node_distance() table.
6487 *
6488 * Assumes node_distance(0,j) includes all distances in
6489 * node_distance(i,j) in order to avoid cubic time.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006490 */
6491 next_distance = curr_distance;
6492 for (i = 0; i < nr_node_ids; i++) {
6493 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006494 for (k = 0; k < nr_node_ids; k++) {
6495 int distance = node_distance(i, k);
6496
6497 if (distance > curr_distance &&
6498 (distance < next_distance ||
6499 next_distance == curr_distance))
6500 next_distance = distance;
6501
6502 /*
6503 * While not a strong assumption it would be nice to know
6504 * about cases where if node A is connected to B, B is not
6505 * equally connected to A.
6506 */
6507 if (sched_debug() && node_distance(k, i) != distance)
6508 sched_numa_warn("Node-distance not symmetric");
6509
6510 if (sched_debug() && i && !find_numa_distance(distance))
6511 sched_numa_warn("Node-0 not representative");
6512 }
6513 if (next_distance != curr_distance) {
6514 sched_domains_numa_distance[level++] = next_distance;
6515 sched_domains_numa_levels = level;
6516 curr_distance = next_distance;
6517 } else break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006518 }
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006519
6520 /*
6521 * In case of sched_debug() we verify the above assumption.
6522 */
6523 if (!sched_debug())
6524 break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006525 }
Andrey Ryabininc1235882014-11-07 17:53:40 +03006526
6527 if (!level)
6528 return;
6529
Peter Zijlstracb83b622012-04-17 15:49:36 +02006530 /*
6531 * 'level' contains the number of unique distances, excluding the
6532 * identity distance node_distance(i,i).
6533 *
Viresh Kumar28b4a522013-04-05 16:26:46 +05306534 * The sched_domains_numa_distance[] array includes the actual distance
Peter Zijlstracb83b622012-04-17 15:49:36 +02006535 * numbers.
6536 */
6537
Tang Chen5f7865f2012-09-25 21:12:30 +08006538 /*
6539 * Here, we should temporarily reset sched_domains_numa_levels to 0.
6540 * If it fails to allocate memory for array sched_domains_numa_masks[][],
6541 * the array will contain less then 'level' members. This could be
6542 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
6543 * in other functions.
6544 *
6545 * We reset it to 'level' at the end of this function.
6546 */
6547 sched_domains_numa_levels = 0;
6548
Peter Zijlstracb83b622012-04-17 15:49:36 +02006549 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
6550 if (!sched_domains_numa_masks)
6551 return;
6552
6553 /*
6554 * Now for each level, construct a mask per node which contains all
6555 * cpus of nodes that are that many hops away from us.
6556 */
6557 for (i = 0; i < level; i++) {
6558 sched_domains_numa_masks[i] =
6559 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
6560 if (!sched_domains_numa_masks[i])
6561 return;
6562
6563 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstra2ea45802012-05-25 09:26:43 +02006564 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
Peter Zijlstracb83b622012-04-17 15:49:36 +02006565 if (!mask)
6566 return;
6567
6568 sched_domains_numa_masks[i][j] = mask;
6569
6570 for (k = 0; k < nr_node_ids; k++) {
Peter Zijlstradd7d8632012-05-11 00:56:20 +02006571 if (node_distance(j, k) > sched_domains_numa_distance[i])
Peter Zijlstracb83b622012-04-17 15:49:36 +02006572 continue;
6573
6574 cpumask_or(mask, mask, cpumask_of_node(k));
6575 }
6576 }
6577 }
6578
Vincent Guittot143e1e22014-04-11 11:44:37 +02006579 /* Compute default topology size */
6580 for (i = 0; sched_domain_topology[i].mask; i++);
6581
Vincent Guittotc515db82014-05-13 11:11:01 +02006582 tl = kzalloc((i + level + 1) *
Peter Zijlstracb83b622012-04-17 15:49:36 +02006583 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
6584 if (!tl)
6585 return;
6586
6587 /*
6588 * Copy the default topology bits..
6589 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006590 for (i = 0; sched_domain_topology[i].mask; i++)
6591 tl[i] = sched_domain_topology[i];
Peter Zijlstracb83b622012-04-17 15:49:36 +02006592
6593 /*
6594 * .. and append 'j' levels of NUMA goodness.
6595 */
6596 for (j = 0; j < level; i++, j++) {
6597 tl[i] = (struct sched_domain_topology_level){
Peter Zijlstracb83b622012-04-17 15:49:36 +02006598 .mask = sd_numa_mask,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006599 .sd_flags = cpu_numa_flags,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006600 .flags = SDTL_OVERLAP,
6601 .numa_level = j,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006602 SD_INIT_NAME(NUMA)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006603 };
6604 }
6605
6606 sched_domain_topology = tl;
Tang Chen5f7865f2012-09-25 21:12:30 +08006607
6608 sched_domains_numa_levels = level;
Rik van Riel9942f792014-10-17 03:29:49 -04006609 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
Rik van Riele3fe70b2014-10-17 03:29:50 -04006610
6611 init_numa_topology_type();
Peter Zijlstracb83b622012-04-17 15:49:36 +02006612}
Tang Chen301a5cb2012-09-25 21:12:31 +08006613
6614static void sched_domains_numa_masks_set(int cpu)
6615{
6616 int i, j;
6617 int node = cpu_to_node(cpu);
6618
6619 for (i = 0; i < sched_domains_numa_levels; i++) {
6620 for (j = 0; j < nr_node_ids; j++) {
6621 if (node_distance(j, node) <= sched_domains_numa_distance[i])
6622 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
6623 }
6624 }
6625}
6626
6627static void sched_domains_numa_masks_clear(int cpu)
6628{
6629 int i, j;
6630 for (i = 0; i < sched_domains_numa_levels; i++) {
6631 for (j = 0; j < nr_node_ids; j++)
6632 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
6633 }
6634}
6635
6636/*
6637 * Update sched_domains_numa_masks[level][node] array when new cpus
6638 * are onlined.
6639 */
6640static int sched_domains_numa_masks_update(struct notifier_block *nfb,
6641 unsigned long action,
6642 void *hcpu)
6643{
6644 int cpu = (long)hcpu;
6645
6646 switch (action & ~CPU_TASKS_FROZEN) {
6647 case CPU_ONLINE:
6648 sched_domains_numa_masks_set(cpu);
6649 break;
6650
6651 case CPU_DEAD:
6652 sched_domains_numa_masks_clear(cpu);
6653 break;
6654
6655 default:
6656 return NOTIFY_DONE;
6657 }
6658
6659 return NOTIFY_OK;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006660}
6661#else
6662static inline void sched_init_numa(void)
6663{
6664}
Tang Chen301a5cb2012-09-25 21:12:31 +08006665
6666static int sched_domains_numa_masks_update(struct notifier_block *nfb,
6667 unsigned long action,
6668 void *hcpu)
6669{
6670 return 0;
6671}
Peter Zijlstracb83b622012-04-17 15:49:36 +02006672#endif /* CONFIG_NUMA */
6673
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006674static int __sdt_alloc(const struct cpumask *cpu_map)
6675{
6676 struct sched_domain_topology_level *tl;
6677 int j;
6678
Viresh Kumar27723a62013-06-10 16:27:20 +05306679 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006680 struct sd_data *sdd = &tl->data;
6681
6682 sdd->sd = alloc_percpu(struct sched_domain *);
6683 if (!sdd->sd)
6684 return -ENOMEM;
6685
6686 sdd->sg = alloc_percpu(struct sched_group *);
6687 if (!sdd->sg)
6688 return -ENOMEM;
6689
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006690 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
6691 if (!sdd->sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006692 return -ENOMEM;
6693
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006694 for_each_cpu(j, cpu_map) {
6695 struct sched_domain *sd;
6696 struct sched_group *sg;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006697 struct sched_group_capacity *sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006698
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02006699 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006700 GFP_KERNEL, cpu_to_node(j));
6701 if (!sd)
6702 return -ENOMEM;
6703
6704 *per_cpu_ptr(sdd->sd, j) = sd;
6705
6706 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
6707 GFP_KERNEL, cpu_to_node(j));
6708 if (!sg)
6709 return -ENOMEM;
6710
Igor Mammedov30b4e9e2012-05-09 12:38:28 +02006711 sg->next = sg;
6712
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006713 *per_cpu_ptr(sdd->sg, j) = sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006714
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006715 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006716 GFP_KERNEL, cpu_to_node(j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006717 if (!sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006718 return -ENOMEM;
6719
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006720 *per_cpu_ptr(sdd->sgc, j) = sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006721 }
6722 }
6723
6724 return 0;
6725}
6726
6727static void __sdt_free(const struct cpumask *cpu_map)
6728{
6729 struct sched_domain_topology_level *tl;
6730 int j;
6731
Viresh Kumar27723a62013-06-10 16:27:20 +05306732 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006733 struct sd_data *sdd = &tl->data;
6734
6735 for_each_cpu(j, cpu_map) {
he, bofb2cf2c2012-04-25 19:59:21 +08006736 struct sched_domain *sd;
6737
6738 if (sdd->sd) {
6739 sd = *per_cpu_ptr(sdd->sd, j);
6740 if (sd && (sd->flags & SD_OVERLAP))
6741 free_sched_groups(sd->groups, 0);
6742 kfree(*per_cpu_ptr(sdd->sd, j));
6743 }
6744
6745 if (sdd->sg)
6746 kfree(*per_cpu_ptr(sdd->sg, j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006747 if (sdd->sgc)
6748 kfree(*per_cpu_ptr(sdd->sgc, j));
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006749 }
6750 free_percpu(sdd->sd);
he, bofb2cf2c2012-04-25 19:59:21 +08006751 sdd->sd = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006752 free_percpu(sdd->sg);
he, bofb2cf2c2012-04-25 19:59:21 +08006753 sdd->sg = NULL;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006754 free_percpu(sdd->sgc);
6755 sdd->sgc = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006756 }
6757}
6758
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006759struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306760 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6761 struct sched_domain *child, int cpu)
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006762{
Vincent Guittot143e1e22014-04-11 11:44:37 +02006763 struct sched_domain *sd = sd_init(tl, cpu);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006764 if (!sd)
Peter Zijlstrad069b912011-04-07 14:10:02 +02006765 return child;
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006766
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006767 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
Peter Zijlstra60495e72011-04-07 14:10:04 +02006768 if (child) {
6769 sd->level = child->level + 1;
6770 sched_domain_level_max = max(sched_domain_level_max, sd->level);
Peter Zijlstrad069b912011-04-07 14:10:02 +02006771 child->parent = sd;
Viresh Kumarc75e0122013-06-10 16:27:19 +05306772 sd->child = child;
Peter Zijlstra6ae72df2014-07-22 11:47:40 +02006773
6774 if (!cpumask_subset(sched_domain_span(child),
6775 sched_domain_span(sd))) {
6776 pr_err("BUG: arch topology borken\n");
6777#ifdef CONFIG_SCHED_DEBUG
6778 pr_err(" the %s domain not a subset of the %s domain\n",
6779 child->name, sd->name);
6780#endif
6781 /* Fixup, ensure @sd has at least @child cpus. */
6782 cpumask_or(sched_domain_span(sd),
6783 sched_domain_span(sd),
6784 sched_domain_span(child));
6785 }
6786
Peter Zijlstra60495e72011-04-07 14:10:04 +02006787 }
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006788 set_domain_attribute(sd, attr);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006789
6790 return sd;
6791}
6792
Mike Travis7c16ec52008-04-04 18:11:11 -07006793/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006794 * Build sched domains for a given set of cpus and attach the sched domains
6795 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006797static int build_sched_domains(const struct cpumask *cpu_map,
6798 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799{
Viresh Kumar1c632162013-06-10 16:27:18 +05306800 enum s_alloc alloc_state;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006801 struct sched_domain *sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006802 struct s_data d;
Peter Zijlstra822ff792011-04-07 14:09:51 +02006803 int i, ret = -ENOMEM;
Rusty Russell3404c8d2008-11-25 02:35:03 +10306804
Andreas Herrmann2109b992009-08-18 12:53:00 +02006805 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
6806 if (alloc_state != sa_rootdomain)
6807 goto error;
Mike Travis7c16ec52008-04-04 18:11:11 -07006808
Peter Zijlstradce840a2011-04-07 14:09:50 +02006809 /* Set up domains for cpus specified by the cpu_map. */
Rusty Russellabcd0832008-11-25 02:35:02 +10306810 for_each_cpu(i, cpu_map) {
Peter Zijlstraeb7a74e62011-04-07 14:10:00 +02006811 struct sched_domain_topology_level *tl;
6812
Peter Zijlstra3bd65a82011-04-07 14:09:54 +02006813 sd = NULL;
Viresh Kumar27723a62013-06-10 16:27:20 +05306814 for_each_sd_topology(tl) {
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306815 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
Viresh Kumar22da9562013-06-04 15:41:15 +05306816 if (tl == sched_domain_topology)
6817 *per_cpu_ptr(d.sd, i) = sd;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006818 if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
6819 sd->flags |= SD_OVERLAP;
Peter Zijlstrad1102352011-07-20 18:42:57 +02006820 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
6821 break;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006822 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006823 }
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006824
Peter Zijlstradce840a2011-04-07 14:09:50 +02006825 /* Build the groups for the domains */
6826 for_each_cpu(i, cpu_map) {
6827 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6828 sd->span_weight = cpumask_weight(sched_domain_span(sd));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006829 if (sd->flags & SD_OVERLAP) {
6830 if (build_overlap_sched_groups(sd, i))
6831 goto error;
6832 } else {
6833 if (build_sched_groups(sd, i))
6834 goto error;
6835 }
Peter Zijlstra1cf519022011-04-07 14:09:47 +02006836 }
Peter Zijlstraa06dadb2011-04-07 14:09:44 +02006837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
Nicolas Pitreced549f2014-05-26 18:19:38 -04006839 /* Calculate CPU capacity for physical packages and nodes */
Peter Zijlstraa9c9a9b2011-04-07 14:09:49 +02006840 for (i = nr_cpumask_bits-1; i >= 0; i--) {
6841 if (!cpumask_test_cpu(i, cpu_map))
6842 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
Peter Zijlstradce840a2011-04-07 14:09:50 +02006844 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6845 claim_allocations(i, sd);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006846 init_sched_groups_capacity(i, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006847 }
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006848 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006849
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 /* Attach the domains */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006851 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10306852 for_each_cpu(i, cpu_map) {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006853 sd = *per_cpu_ptr(d.sd, i);
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006854 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006856 rcu_read_unlock();
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006857
Peter Zijlstra822ff792011-04-07 14:09:51 +02006858 ret = 0;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006859error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02006860 __free_domain_allocs(&d, alloc_state, cpu_map);
Peter Zijlstra822ff792011-04-07 14:09:51 +02006861 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862}
Paul Jackson029190c2007-10-18 23:40:20 -07006863
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306864static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07006865static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02006866static struct sched_domain_attr *dattr_cur;
6867 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07006868
6869/*
6870 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10306871 * cpumask) fails, then fallback to a single sched domain,
6872 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07006873 */
Rusty Russell42128232008-11-25 02:35:12 +10306874static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07006875
Heiko Carstensee79d1b2008-12-09 18:49:50 +01006876/*
6877 * arch_update_cpu_topology lets virtualized architectures update the
6878 * cpu core maps. It is supposed to return 1 if the topology changed
6879 * or 0 if it stayed the same.
6880 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07006881int __weak arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01006882{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01006883 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01006884}
6885
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306886cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
6887{
6888 int i;
6889 cpumask_var_t *doms;
6890
6891 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
6892 if (!doms)
6893 return NULL;
6894 for (i = 0; i < ndoms; i++) {
6895 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
6896 free_sched_domains(doms, i);
6897 return NULL;
6898 }
6899 }
6900 return doms;
6901}
6902
6903void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
6904{
6905 unsigned int i;
6906 for (i = 0; i < ndoms; i++)
6907 free_cpumask_var(doms[i]);
6908 kfree(doms);
6909}
6910
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006911/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006912 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006913 * For now this just excludes isolated cpus, but could be used to
6914 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006915 */
Peter Zijlstrac4a88492011-04-07 14:09:42 +02006916static int init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006917{
Milton Miller73785472007-10-24 18:23:48 +02006918 int err;
6919
Heiko Carstens22e52b02008-03-12 18:31:59 +01006920 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07006921 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306922 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07006923 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306924 doms_cur = &fallback_doms;
6925 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006926 err = build_sched_domains(doms_cur[0], NULL);
Milton Miller6382bc92007-10-15 17:00:19 +02006927 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006928
6929 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006930}
6931
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006932/*
6933 * Detach sched domains from a group of cpus specified in cpu_map
6934 * These cpus will now be attached to the NULL domain
6935 */
Rusty Russell96f874e22008-11-25 02:35:14 +10306936static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006937{
6938 int i;
6939
Peter Zijlstradce840a2011-04-07 14:09:50 +02006940 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10306941 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006942 cpu_attach_domain(NULL, &def_root_domain, i);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006943 rcu_read_unlock();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006944}
6945
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006946/* handle null as "default" */
6947static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
6948 struct sched_domain_attr *new, int idx_new)
6949{
6950 struct sched_domain_attr tmp;
6951
6952 /* fast path */
6953 if (!new && !cur)
6954 return 1;
6955
6956 tmp = SD_ATTR_INIT;
6957 return !memcmp(cur ? (cur + idx_cur) : &tmp,
6958 new ? (new + idx_new) : &tmp,
6959 sizeof(struct sched_domain_attr));
6960}
6961
Paul Jackson029190c2007-10-18 23:40:20 -07006962/*
6963 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006964 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006965 * doms_new[] to the current sched domain partitioning, doms_cur[].
6966 * It destroys each deleted domain and builds each new domain.
6967 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306968 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006969 * The masks don't intersect (don't overlap.) We should setup one
6970 * sched domain for each mask. CPUs not in any of the cpumasks will
6971 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07006972 * current 'doms_cur' domains and in the new 'doms_new', we can leave
6973 * it as it is.
6974 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306975 * The passed in 'doms_new' should be allocated using
6976 * alloc_sched_domains. This routine takes ownership of it and will
6977 * free_sched_domains it when done with it. If the caller failed the
6978 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
6979 * and partition_sched_domains() will fallback to the single partition
6980 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07006981 *
Rusty Russell96f874e22008-11-25 02:35:14 +10306982 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08006983 * ndoms_new == 0 is a special case for destroying existing domains,
6984 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006985 *
Paul Jackson029190c2007-10-18 23:40:20 -07006986 * Call with hotplug lock held
6987 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306988void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006989 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07006990{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07006991 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006992 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07006993
Heiko Carstens712555e2008-04-28 11:33:07 +02006994 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006995
Milton Miller73785472007-10-24 18:23:48 +02006996 /* always unregister in case we don't destroy any domains */
6997 unregister_sched_domain_sysctl();
6998
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01006999 /* Let architecture update cpu core mappings. */
7000 new_topology = arch_update_cpu_topology();
7001
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007002 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007003
7004 /* Destroy deleted domains */
7005 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007006 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307007 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007008 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007009 goto match1;
7010 }
7011 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307012 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07007013match1:
7014 ;
7015 }
7016
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007017 n = ndoms_cur;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007018 if (doms_new == NULL) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007019 n = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307020 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007021 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08007022 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007023 }
7024
Paul Jackson029190c2007-10-18 23:40:20 -07007025 /* Build new domains */
7026 for (i = 0; i < ndoms_new; i++) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007027 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307028 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007029 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007030 goto match2;
7031 }
7032 /* no match - add a new doms_new */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007033 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007034match2:
7035 ;
7036 }
7037
7038 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307039 if (doms_cur != &fallback_doms)
7040 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007041 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007042 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007043 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007044 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007045
7046 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007047
Heiko Carstens712555e2008-04-28 11:33:07 +02007048 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007049}
7050
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307051static int num_cpus_frozen; /* used to mark begin/end of suspend/resume */
7052
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053/*
Tejun Heo3a101d02010-06-08 21:40:36 +02007054 * Update cpusets according to cpu_active mask. If cpusets are
7055 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
7056 * around partition_sched_domains().
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307057 *
7058 * If we come here as part of a suspend/resume, don't touch cpusets because we
7059 * want to restore it back to its original state upon resume anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 */
Tejun Heo0b2e9182010-06-21 23:53:31 +02007061static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
7062 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063{
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307064 switch (action) {
7065 case CPU_ONLINE_FROZEN:
7066 case CPU_DOWN_FAILED_FROZEN:
7067
7068 /*
7069 * num_cpus_frozen tracks how many CPUs are involved in suspend
7070 * resume sequence. As long as this is not the last online
7071 * operation in the resume sequence, just build a single sched
7072 * domain, ignoring cpusets.
7073 */
7074 num_cpus_frozen--;
7075 if (likely(num_cpus_frozen)) {
7076 partition_sched_domains(1, NULL, NULL);
7077 break;
7078 }
7079
7080 /*
7081 * This is the last CPU online operation. So fall through and
7082 * restore the original sched domains by considering the
7083 * cpuset configurations.
7084 */
7085
Max Krasnyanskye761b772008-07-15 04:43:49 -07007086 case CPU_ONLINE:
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307087 cpuset_update_active_cpus(true);
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307088 break;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007089 default:
7090 return NOTIFY_DONE;
7091 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307092 return NOTIFY_OK;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007093}
Tejun Heo3a101d02010-06-08 21:40:36 +02007094
Tejun Heo0b2e9182010-06-21 23:53:31 +02007095static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
7096 void *hcpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02007097{
Juri Lelli3c18d442015-03-31 09:53:37 +01007098 unsigned long flags;
7099 long cpu = (long)hcpu;
7100 struct dl_bw *dl_b;
Omar Sandoval533445c2015-05-04 03:09:36 -07007101 bool overflow;
7102 int cpus;
Juri Lelli3c18d442015-03-31 09:53:37 +01007103
Omar Sandoval533445c2015-05-04 03:09:36 -07007104 switch (action) {
Tejun Heo3a101d02010-06-08 21:40:36 +02007105 case CPU_DOWN_PREPARE:
Omar Sandoval533445c2015-05-04 03:09:36 -07007106 rcu_read_lock_sched();
7107 dl_b = dl_bw_of(cpu);
Juri Lelli3c18d442015-03-31 09:53:37 +01007108
Omar Sandoval533445c2015-05-04 03:09:36 -07007109 raw_spin_lock_irqsave(&dl_b->lock, flags);
7110 cpus = dl_bw_cpus(cpu);
7111 overflow = __dl_overflow(dl_b, cpus, 0, 0);
7112 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli3c18d442015-03-31 09:53:37 +01007113
Omar Sandoval533445c2015-05-04 03:09:36 -07007114 rcu_read_unlock_sched();
Juri Lelli3c18d442015-03-31 09:53:37 +01007115
Omar Sandoval533445c2015-05-04 03:09:36 -07007116 if (overflow)
7117 return notifier_from_errno(-EBUSY);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307118 cpuset_update_active_cpus(false);
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307119 break;
7120 case CPU_DOWN_PREPARE_FROZEN:
7121 num_cpus_frozen++;
7122 partition_sched_domains(1, NULL, NULL);
7123 break;
Tejun Heo3a101d02010-06-08 21:40:36 +02007124 default:
7125 return NOTIFY_DONE;
7126 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307127 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02007128}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007129
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130void __init sched_init_smp(void)
7131{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307132 cpumask_var_t non_isolated_cpus;
7133
7134 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007135 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007136
Chris Metcalf8cb97642015-05-06 18:04:26 +02007137 /* nohz_full won't take effect without isolating the cpus. */
7138 tick_nohz_full_add_cpus_to(cpu_isolated_map);
7139
Peter Zijlstracb83b622012-04-17 15:49:36 +02007140 sched_init_numa();
7141
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02007142 /*
7143 * There's no userspace yet to cause hotplug operations; hence all the
7144 * cpu masks are stable and all blatant races in the below code cannot
7145 * happen.
7146 */
Heiko Carstens712555e2008-04-28 11:33:07 +02007147 mutex_lock(&sched_domains_mutex);
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007148 init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307149 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7150 if (cpumask_empty(non_isolated_cpus))
7151 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007152 mutex_unlock(&sched_domains_mutex);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007153
Tang Chen301a5cb2012-09-25 21:12:31 +08007154 hotcpu_notifier(sched_domains_numa_masks_update, CPU_PRI_SCHED_ACTIVE);
Tejun Heo3a101d02010-06-08 21:40:36 +02007155 hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
7156 hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007157
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007158 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007159
7160 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307161 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007162 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007163 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307164 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307165
Rusty Russell0e3900e2008-11-25 02:35:13 +10307166 init_sched_rt_class();
Juri Lelli1baca4c2013-11-07 14:43:38 +01007167 init_sched_dl_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168}
7169#else
7170void __init sched_init_smp(void)
7171{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007172 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173}
7174#endif /* CONFIG_SMP */
7175
7176int in_sched_functions(unsigned long addr)
7177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 return in_lock_functions(addr) ||
7179 (addr >= (unsigned long)__sched_text_start
7180 && addr < (unsigned long)__sched_text_end);
7181}
7182
Peter Zijlstra029632f2011-10-25 10:00:11 +02007183#ifdef CONFIG_CGROUP_SCHED
Li Zefan27b4b932013-03-05 16:07:52 +08007184/*
7185 * Default task group.
7186 * Every task in system belongs to this group at bootup.
7187 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007188struct task_group root_task_group;
Mike Galbraith35cf4e52012-08-07 05:00:13 +02007189LIST_HEAD(task_groups);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007190#endif
7191
Joonsoo Kime6252c32013-04-23 17:27:41 +09007192DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007193
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194void __init sched_init(void)
7195{
Ingo Molnardd41f592007-07-09 18:51:59 +02007196 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007197 unsigned long alloc_size = 0, ptr;
7198
7199#ifdef CONFIG_FAIR_GROUP_SCHED
7200 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7201#endif
7202#ifdef CONFIG_RT_GROUP_SCHED
7203 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7204#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007205 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007206 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007207
7208#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007209 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007210 ptr += nr_cpu_ids * sizeof(void **);
7211
Yong Zhang07e06b02011-01-07 15:17:36 +08007212 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007213 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007214
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007215#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007216#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007217 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007218 ptr += nr_cpu_ids * sizeof(void **);
7219
Yong Zhang07e06b02011-01-07 15:17:36 +08007220 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007221 ptr += nr_cpu_ids * sizeof(void **);
7222
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007223#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007224 }
Alex Thorltonb74e6272014-12-18 12:44:30 -06007225#ifdef CONFIG_CPUMASK_OFFSTACK
7226 for_each_possible_cpu(i) {
7227 per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
7228 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
7229 }
7230#endif /* CONFIG_CPUMASK_OFFSTACK */
Ingo Molnardd41f592007-07-09 18:51:59 +02007231
Dario Faggioli332ac172013-11-07 14:43:45 +01007232 init_rt_bandwidth(&def_rt_bandwidth,
7233 global_rt_period(), global_rt_runtime());
7234 init_dl_bandwidth(&def_dl_bandwidth,
Peter Zijlstra17248132013-12-17 12:44:49 +01007235 global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +01007236
Gregory Haskins57d885f2008-01-25 21:08:18 +01007237#ifdef CONFIG_SMP
7238 init_defrootdomain();
7239#endif
7240
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007241#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007242 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007243 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007244#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007245
Dhaval Giani7c941432010-01-20 13:26:18 +01007246#ifdef CONFIG_CGROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007247 list_add(&root_task_group.list, &task_groups);
7248 INIT_LIST_HEAD(&root_task_group.children);
Glauber Costaf4d6f6c2011-11-01 19:19:07 -02007249 INIT_LIST_HEAD(&root_task_group.siblings);
Mike Galbraith5091faa2010-11-30 14:18:03 +01007250 autogroup_init(&init_task);
Glauber Costa54c707e2011-11-28 14:45:19 -02007251
Dhaval Giani7c941432010-01-20 13:26:18 +01007252#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007253
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007254 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007255 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256
7257 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007258 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07007259 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007260 rq->calc_load_active = 0;
7261 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007262 init_cfs_rq(&rq->cfs);
Abel Vesa07c54f72015-03-03 13:50:27 +02007263 init_rt_rq(&rq->rt);
7264 init_dl_rq(&rq->dl);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007265#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007266 root_task_group.shares = ROOT_TASK_GROUP_LOAD;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007267 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007268 /*
Yong Zhang07e06b02011-01-07 15:17:36 +08007269 * How much cpu bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02007270 *
7271 * In case of task-groups formed thr' the cgroup filesystem, it
7272 * gets 100% of the cpu resources in the system. This overall
7273 * system cpu resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08007274 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02007275 * based on each entity's (task or task-group's) weight
7276 * (se->load.weight).
7277 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007278 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02007279 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7280 * then A0's share of the cpu resource is:
7281 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007282 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007283 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007284 * We achieve this by letting root_task_group's tasks sit
7285 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02007286 */
Paul Turnerab84d312011-07-21 09:43:28 -07007287 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Yong Zhang07e06b02011-01-07 15:17:36 +08007288 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007289#endif /* CONFIG_FAIR_GROUP_SCHED */
7290
7291 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007292#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007293 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007294#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295
Ingo Molnardd41f592007-07-09 18:51:59 +02007296 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7297 rq->cpu_load[j] = 0;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07007298
7299 rq->last_load_update_tick = jiffies;
7300
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007302 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007303 rq->rd = NULL;
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007304 rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02007305 rq->balance_callback = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007307 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007309 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04007310 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007311 rq->idle_stamp = 0;
7312 rq->avg_idle = 2*sysctl_sched_migration_cost;
Jason Low9bd721c2013-09-13 11:26:52 -07007313 rq->max_idle_balance_cost = sysctl_sched_migration_cost;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007314
7315 INIT_LIST_HEAD(&rq->cfs_tasks);
7316
Gregory Haskinsdc938522008-01-25 21:08:26 +01007317 rq_attach_root(rq, &def_root_domain);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007318#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08007319 rq->nohz_flags = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007320#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02007321#ifdef CONFIG_NO_HZ_FULL
7322 rq->last_sched_tick = 0;
7323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007325 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 }
7328
Peter Williams2dd73a42006-06-27 02:54:34 -07007329 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007330
Avi Kivitye107be32007-07-26 13:40:43 +02007331#ifdef CONFIG_PREEMPT_NOTIFIERS
7332 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7333#endif
7334
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 /*
7336 * The boot idle thread does lazy MMU switching as well:
7337 */
7338 atomic_inc(&init_mm.mm_count);
7339 enter_lazy_tlb(&init_mm, current);
7340
7341 /*
Yao Dongdong1b537c72014-12-29 14:41:43 +08007342 * During early bootup we pretend to be a normal task:
7343 */
7344 current->sched_class = &fair_sched_class;
7345
7346 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 * Make us the idle thread. Technically, schedule() should not be
7348 * called from this thread, however somewhere below it might be,
7349 * but because we are the idle thread, we just pick up running again
7350 * when this runqueue becomes "idle".
7351 */
7352 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007353
7354 calc_load_update = jiffies + LOAD_FREQ;
7355
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307356#ifdef CONFIG_SMP
Peter Zijlstra4cb98832011-04-07 14:09:58 +02007357 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
Rusty Russellbdddd292009-12-02 14:09:16 +10307358 /* May be allocated at isolcpus cmdline parse time */
7359 if (cpu_isolated_map == NULL)
7360 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Thomas Gleixner29d5e042012-04-20 13:05:45 +00007361 idle_thread_set_boot_cpu();
Corey Minyarda803f022014-05-08 13:47:39 -05007362 set_cpu_rq_start_time();
Peter Zijlstra029632f2011-10-25 10:00:11 +02007363#endif
7364 init_sched_fair_class();
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307365
Ingo Molnar6892b752008-02-13 14:02:36 +01007366 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367}
7368
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02007369#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007370static inline int preempt_count_equals(int preempt_offset)
7371{
Frederic Weisbecker234da7b2009-12-16 20:21:05 +01007372 int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007373
Arnd Bergmann4ba82162011-01-25 22:52:22 +01007374 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007375}
7376
Simon Kagstromd8948372009-12-23 11:08:18 +01007377void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378{
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007379 /*
7380 * Blocking primitives will set (and therefore destroy) current->state,
7381 * since we will exit with TASK_RUNNING make sure we enter with it,
7382 * otherwise we will destroy state.
7383 */
Linus Torvalds00845eb2015-02-01 12:23:32 -08007384 WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007385 "do not call blocking ops when !TASK_RUNNING; "
7386 "state=%lx set at [<%p>] %pS\n",
7387 current->state,
7388 (void *)current->task_state_change,
Linus Torvalds00845eb2015-02-01 12:23:32 -08007389 (void *)current->task_state_change);
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007390
Peter Zijlstra34274452014-09-24 10:18:56 +02007391 ___might_sleep(file, line, preempt_offset);
7392}
7393EXPORT_SYMBOL(__might_sleep);
7394
7395void ___might_sleep(const char *file, int line, int preempt_offset)
7396{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 static unsigned long prev_jiffy; /* ratelimiting */
7398
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07007399 rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
Thomas Gleixnerdb273be2014-02-07 20:58:38 +01007400 if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
7401 !is_idle_task(current)) ||
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007402 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007403 return;
7404 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7405 return;
7406 prev_jiffy = jiffies;
7407
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007408 printk(KERN_ERR
7409 "BUG: sleeping function called from invalid context at %s:%d\n",
7410 file, line);
7411 printk(KERN_ERR
7412 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7413 in_atomic(), irqs_disabled(),
7414 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007415
Eric Sandeena8b686b2014-12-16 16:25:28 -06007416 if (task_stack_end_corrupted(current))
7417 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
7418
Ingo Molnaraef745f2008-08-28 11:34:43 +02007419 debug_show_held_locks(current);
7420 if (irqs_disabled())
7421 print_irqtrace_events(current);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007422#ifdef CONFIG_DEBUG_PREEMPT
7423 if (!preempt_count_equals(preempt_offset)) {
7424 pr_err("Preemption disabled at:");
7425 print_ip_sym(current->preempt_disable_ip);
7426 pr_cont("\n");
7427 }
7428#endif
Ingo Molnaraef745f2008-08-28 11:34:43 +02007429 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430}
Peter Zijlstra34274452014-09-24 10:18:56 +02007431EXPORT_SYMBOL(___might_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432#endif
7433
7434#ifdef CONFIG_MAGIC_SYSRQ
7435void normalize_rt_tasks(void)
7436{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007437 struct task_struct *g, *p;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007438 struct sched_attr attr = {
7439 .sched_policy = SCHED_NORMAL,
7440 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007442 read_lock(&tasklist_lock);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007443 for_each_process_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007444 /*
7445 * Only normalize user tasks:
7446 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007447 if (p->flags & PF_KTHREAD)
Ingo Molnar178be792007-10-15 17:00:18 +02007448 continue;
7449
Ingo Molnardd41f592007-07-09 18:51:59 +02007450 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007451#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03007452 p->se.statistics.wait_start = 0;
7453 p->se.statistics.sleep_start = 0;
7454 p->se.statistics.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007455#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007456
Dario Faggioliaab03e02013-11-28 11:14:43 +01007457 if (!dl_task(p) && !rt_task(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007458 /*
7459 * Renice negative nice level userspace
7460 * tasks back to 0:
7461 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007462 if (task_nice(p) < 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02007463 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007467 __sched_setscheduler(p, &attr, false, false);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007468 }
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007469 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470}
7471
7472#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007473
Jason Wessel67fc4e02010-05-20 21:04:21 -05007474#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007475/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007476 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007477 *
7478 * They can only be called when the whole system has been
7479 * stopped - every CPU needs to be quiescent, and no scheduling
7480 * activity can take place. Using them for anything else would
7481 * be a serious bug, and as a result, they aren't even visible
7482 * under any other configuration.
7483 */
7484
7485/**
7486 * curr_task - return the current task for a given cpu.
7487 * @cpu: the processor in question.
7488 *
7489 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
Yacine Belkadie69f6182013-07-12 20:45:47 +02007490 *
7491 * Return: The current task for @cpu.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007492 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007493struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007494{
7495 return cpu_curr(cpu);
7496}
7497
Jason Wessel67fc4e02010-05-20 21:04:21 -05007498#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7499
7500#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07007501/**
7502 * set_curr_task - set the current task for a given cpu.
7503 * @cpu: the processor in question.
7504 * @p: the task pointer to set.
7505 *
7506 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007507 * are serviced on a separate stack. It allows the architecture to switch the
7508 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007509 * must be called with all CPU's synchronized, and interrupts disabled, the
7510 * and caller must save the original value of the current task (see
7511 * curr_task() above) and restore that value before reenabling interrupts and
7512 * re-starting the system.
7513 *
7514 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7515 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007516void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007517{
7518 cpu_curr(cpu) = p;
7519}
7520
7521#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007522
Dhaval Giani7c941432010-01-20 13:26:18 +01007523#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007524/* task_group_lock serializes the addition/removal of task groups */
7525static DEFINE_SPINLOCK(task_group_lock);
7526
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007527static void free_sched_group(struct task_group *tg)
7528{
7529 free_fair_sched_group(tg);
7530 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01007531 autogroup_free(tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007532 kfree(tg);
7533}
7534
7535/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007536struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007537{
7538 struct task_group *tg;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007539
7540 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7541 if (!tg)
7542 return ERR_PTR(-ENOMEM);
7543
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007544 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007545 goto err;
7546
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007547 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007548 goto err;
7549
Li Zefanace783b2013-01-24 14:30:48 +08007550 return tg;
7551
7552err:
7553 free_sched_group(tg);
7554 return ERR_PTR(-ENOMEM);
7555}
7556
7557void sched_online_group(struct task_group *tg, struct task_group *parent)
7558{
7559 unsigned long flags;
7560
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007561 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007562 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007563
7564 WARN_ON(!parent); /* root should already exist */
7565
7566 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007567 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08007568 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007569 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007570}
7571
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007572/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007573static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007574{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007575 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007576 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007577}
7578
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007579/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007580void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007581{
Li Zefanace783b2013-01-24 14:30:48 +08007582 /* wait for possible concurrent references to cfs_rqs complete */
7583 call_rcu(&tg->rcu, free_sched_group_rcu);
7584}
7585
7586void sched_offline_group(struct task_group *tg)
7587{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007588 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007589 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007590
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007591 /* end participation in shares distribution */
7592 for_each_possible_cpu(i)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007593 unregister_fair_sched_group(tg, i);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007594
7595 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007596 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007597 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007598 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007599}
7600
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007601/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007602 * The caller of this function should have put the task in its new group
7603 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7604 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007605 */
7606void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007607{
Peter Zijlstra8323f262012-06-22 13:36:05 +02007608 struct task_group *tg;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007609 int queued, running;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007610 unsigned long flags;
7611 struct rq *rq;
7612
7613 rq = task_rq_lock(tsk, &flags);
7614
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007615 running = task_current(rq, tsk);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007616 queued = task_on_rq_queued(tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007617
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007618 if (queued)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007619 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007620 if (unlikely(running))
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04007621 put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007622
Kirill Tkhaif7b8a472014-10-28 08:24:34 +03007623 /*
7624 * All callers are synchronized by task_rq_lock(); we do not use RCU
7625 * which is pointless here. Thus, we pass "true" to task_css_check()
7626 * to prevent lockdep warnings.
7627 */
7628 tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
Peter Zijlstra8323f262012-06-22 13:36:05 +02007629 struct task_group, css);
7630 tg = autogroup_task_group(tsk, tg);
7631 tsk->sched_task_group = tg;
7632
Peter Zijlstra810b3812008-02-29 15:21:01 -05007633#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007634 if (tsk->sched_class->task_move_group)
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007635 tsk->sched_class->task_move_group(tsk, queued);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007636 else
Peter Zijlstra810b3812008-02-29 15:21:01 -05007637#endif
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007638 set_task_rq(tsk, task_cpu(tsk));
Peter Zijlstra810b3812008-02-29 15:21:01 -05007639
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007640 if (unlikely(running))
7641 tsk->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007642 if (queued)
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01007643 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007644
Peter Zijlstra0122ec52011-04-05 17:23:51 +02007645 task_rq_unlock(rq, tsk, &flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007646}
Dhaval Giani7c941432010-01-20 13:26:18 +01007647#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007648
Paul Turnera790de92011-07-21 09:43:29 -07007649#ifdef CONFIG_RT_GROUP_SCHED
7650/*
7651 * Ensure that the real time constraints are schedulable.
7652 */
7653static DEFINE_MUTEX(rt_constraints_mutex);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007654
Dhaval Giani521f1a242008-02-28 15:21:56 +05307655/* Must be called with tasklist_lock held */
7656static inline int tg_has_rt_tasks(struct task_group *tg)
7657{
7658 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007659
Peter Zijlstra1fe89e12015-02-09 11:53:18 +01007660 /*
7661 * Autogroups do not have RT tasks; see autogroup_create().
7662 */
7663 if (task_group_is_autogroup(tg))
7664 return 0;
7665
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007666 for_each_process_thread(g, p) {
Oleg Nesterov8651c652014-09-21 21:33:36 +02007667 if (rt_task(p) && task_group(p) == tg)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307668 return 1;
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007669 }
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007670
Dhaval Giani521f1a242008-02-28 15:21:56 +05307671 return 0;
7672}
7673
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007674struct rt_schedulable_data {
7675 struct task_group *tg;
7676 u64 rt_period;
7677 u64 rt_runtime;
7678};
7679
Paul Turnera790de92011-07-21 09:43:29 -07007680static int tg_rt_schedulable(struct task_group *tg, void *data)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007681{
7682 struct rt_schedulable_data *d = data;
7683 struct task_group *child;
7684 unsigned long total, sum = 0;
7685 u64 period, runtime;
7686
7687 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7688 runtime = tg->rt_bandwidth.rt_runtime;
7689
7690 if (tg == d->tg) {
7691 period = d->rt_period;
7692 runtime = d->rt_runtime;
7693 }
7694
Peter Zijlstra4653f802008-09-23 15:33:44 +02007695 /*
7696 * Cannot have more runtime than the period.
7697 */
7698 if (runtime > period && runtime != RUNTIME_INF)
7699 return -EINVAL;
7700
7701 /*
7702 * Ensure we don't starve existing RT tasks.
7703 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007704 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
7705 return -EBUSY;
7706
7707 total = to_ratio(period, runtime);
7708
Peter Zijlstra4653f802008-09-23 15:33:44 +02007709 /*
7710 * Nobody can have more than the global setting allows.
7711 */
7712 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
7713 return -EINVAL;
7714
7715 /*
7716 * The sum of our children's runtime should not exceed our own.
7717 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007718 list_for_each_entry_rcu(child, &tg->children, siblings) {
7719 period = ktime_to_ns(child->rt_bandwidth.rt_period);
7720 runtime = child->rt_bandwidth.rt_runtime;
7721
7722 if (child == d->tg) {
7723 period = d->rt_period;
7724 runtime = d->rt_runtime;
7725 }
7726
7727 sum += to_ratio(period, runtime);
7728 }
7729
7730 if (sum > total)
7731 return -EINVAL;
7732
7733 return 0;
7734}
7735
7736static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
7737{
Paul Turner82774342011-07-21 09:43:35 -07007738 int ret;
7739
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007740 struct rt_schedulable_data data = {
7741 .tg = tg,
7742 .rt_period = period,
7743 .rt_runtime = runtime,
7744 };
7745
Paul Turner82774342011-07-21 09:43:35 -07007746 rcu_read_lock();
7747 ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
7748 rcu_read_unlock();
7749
7750 return ret;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007751}
7752
Paul Turnerab84d312011-07-21 09:43:28 -07007753static int tg_set_rt_bandwidth(struct task_group *tg,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007754 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007755{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007756 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007757
Peter Zijlstra2636ed52015-02-09 12:23:20 +01007758 /*
7759 * Disallowing the root group RT runtime is BAD, it would disallow the
7760 * kernel creating (and or operating) RT threads.
7761 */
7762 if (tg == &root_task_group && rt_runtime == 0)
7763 return -EINVAL;
7764
7765 /* No period doesn't make any sense. */
7766 if (rt_period == 0)
7767 return -EINVAL;
7768
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007769 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307770 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007771 err = __rt_schedulable(tg, rt_period, rt_runtime);
7772 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307773 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007774
Thomas Gleixner0986b112009-11-17 15:32:06 +01007775 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007776 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
7777 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007778
7779 for_each_possible_cpu(i) {
7780 struct rt_rq *rt_rq = tg->rt_rq[i];
7781
Thomas Gleixner0986b112009-11-17 15:32:06 +01007782 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007783 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01007784 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007785 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007786 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra49246272010-10-17 21:46:10 +02007787unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307788 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007789 mutex_unlock(&rt_constraints_mutex);
7790
7791 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007792}
7793
Li Zefan25cc7da2013-03-05 16:07:33 +08007794static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007795{
7796 u64 rt_runtime, rt_period;
7797
7798 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7799 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7800 if (rt_runtime_us < 0)
7801 rt_runtime = RUNTIME_INF;
7802
Paul Turnerab84d312011-07-21 09:43:28 -07007803 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007804}
7805
Li Zefan25cc7da2013-03-05 16:07:33 +08007806static long sched_group_rt_runtime(struct task_group *tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007807{
7808 u64 rt_runtime_us;
7809
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007810 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007811 return -1;
7812
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007813 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007814 do_div(rt_runtime_us, NSEC_PER_USEC);
7815 return rt_runtime_us;
7816}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007817
Nicholas Mc Guirece2f5fe2015-05-03 10:51:56 +02007818static int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007819{
7820 u64 rt_runtime, rt_period;
7821
Nicholas Mc Guirece2f5fe2015-05-03 10:51:56 +02007822 rt_period = rt_period_us * NSEC_PER_USEC;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007823 rt_runtime = tg->rt_bandwidth.rt_runtime;
7824
Paul Turnerab84d312011-07-21 09:43:28 -07007825 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007826}
7827
Li Zefan25cc7da2013-03-05 16:07:33 +08007828static long sched_group_rt_period(struct task_group *tg)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007829{
7830 u64 rt_period_us;
7831
7832 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
7833 do_div(rt_period_us, NSEC_PER_USEC);
7834 return rt_period_us;
7835}
Dario Faggioli332ac172013-11-07 14:43:45 +01007836#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007837
Dario Faggioli332ac172013-11-07 14:43:45 +01007838#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007839static int sched_rt_global_constraints(void)
7840{
7841 int ret = 0;
7842
7843 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007844 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02007845 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007846 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007847 mutex_unlock(&rt_constraints_mutex);
7848
7849 return ret;
7850}
Dhaval Giani54e99122009-02-27 15:13:54 +05307851
Li Zefan25cc7da2013-03-05 16:07:33 +08007852static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
Dhaval Giani54e99122009-02-27 15:13:54 +05307853{
7854 /* Don't accept realtime tasks when there is no way for them to run */
7855 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
7856 return 0;
7857
7858 return 1;
7859}
7860
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007861#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007862static int sched_rt_global_constraints(void)
7863{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007864 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01007865 int i, ret = 0;
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07007866
Thomas Gleixner0986b112009-11-17 15:32:06 +01007867 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007868 for_each_possible_cpu(i) {
7869 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
7870
Thomas Gleixner0986b112009-11-17 15:32:06 +01007871 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007872 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01007873 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007874 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007875 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007876
Dario Faggioli332ac172013-11-07 14:43:45 +01007877 return ret;
7878}
7879#endif /* CONFIG_RT_GROUP_SCHED */
7880
Wanpeng Lia1963b82015-03-17 19:15:31 +08007881static int sched_dl_global_validate(void)
Dario Faggioli332ac172013-11-07 14:43:45 +01007882{
Peter Zijlstra17248132013-12-17 12:44:49 +01007883 u64 runtime = global_rt_runtime();
7884 u64 period = global_rt_period();
Dario Faggioli332ac172013-11-07 14:43:45 +01007885 u64 new_bw = to_ratio(period, runtime);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007886 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01007887 int cpu, ret = 0;
Juri Lelli49516342014-02-11 09:24:27 +01007888 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01007889
7890 /*
7891 * Here we want to check the bandwidth not being set to some
7892 * value smaller than the currently allocated bandwidth in
7893 * any of the root_domains.
7894 *
7895 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
7896 * cycling on root_domains... Discussion on different/better
7897 * solutions is welcome!
7898 */
Peter Zijlstra17248132013-12-17 12:44:49 +01007899 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007900 rcu_read_lock_sched();
7901 dl_b = dl_bw_of(cpu);
Dario Faggioli332ac172013-11-07 14:43:45 +01007902
Juri Lelli49516342014-02-11 09:24:27 +01007903 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007904 if (new_bw < dl_b->total_bw)
7905 ret = -EBUSY;
Juri Lelli49516342014-02-11 09:24:27 +01007906 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007907
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007908 rcu_read_unlock_sched();
7909
Peter Zijlstra17248132013-12-17 12:44:49 +01007910 if (ret)
7911 break;
Dario Faggioli332ac172013-11-07 14:43:45 +01007912 }
7913
Peter Zijlstra17248132013-12-17 12:44:49 +01007914 return ret;
7915}
7916
7917static void sched_dl_do_global(void)
7918{
7919 u64 new_bw = -1;
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007920 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01007921 int cpu;
Juri Lelli49516342014-02-11 09:24:27 +01007922 unsigned long flags;
Peter Zijlstra17248132013-12-17 12:44:49 +01007923
7924 def_dl_bandwidth.dl_period = global_rt_period();
7925 def_dl_bandwidth.dl_runtime = global_rt_runtime();
7926
7927 if (global_rt_runtime() != RUNTIME_INF)
7928 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
7929
7930 /*
7931 * FIXME: As above...
7932 */
7933 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007934 rcu_read_lock_sched();
7935 dl_b = dl_bw_of(cpu);
Peter Zijlstra17248132013-12-17 12:44:49 +01007936
Juri Lelli49516342014-02-11 09:24:27 +01007937 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007938 dl_b->bw = new_bw;
Juri Lelli49516342014-02-11 09:24:27 +01007939 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007940
7941 rcu_read_unlock_sched();
Peter Zijlstra17248132013-12-17 12:44:49 +01007942 }
7943}
7944
7945static int sched_rt_global_validate(void)
7946{
7947 if (sysctl_sched_rt_period <= 0)
7948 return -EINVAL;
7949
Juri Lellie9e7cb32014-02-11 09:24:26 +01007950 if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
7951 (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
Peter Zijlstra17248132013-12-17 12:44:49 +01007952 return -EINVAL;
7953
Dario Faggioli332ac172013-11-07 14:43:45 +01007954 return 0;
7955}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007956
Peter Zijlstra17248132013-12-17 12:44:49 +01007957static void sched_rt_do_global(void)
7958{
7959 def_rt_bandwidth.rt_runtime = global_rt_runtime();
7960 def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
7961}
7962
7963int sched_rt_handler(struct ctl_table *table, int write,
7964 void __user *buffer, size_t *lenp,
7965 loff_t *ppos)
7966{
7967 int old_period, old_runtime;
7968 static DEFINE_MUTEX(mutex);
7969 int ret;
7970
7971 mutex_lock(&mutex);
7972 old_period = sysctl_sched_rt_period;
7973 old_runtime = sysctl_sched_rt_runtime;
7974
7975 ret = proc_dointvec(table, write, buffer, lenp, ppos);
7976
7977 if (!ret && write) {
7978 ret = sched_rt_global_validate();
7979 if (ret)
7980 goto undo;
7981
Wanpeng Lia1963b82015-03-17 19:15:31 +08007982 ret = sched_dl_global_validate();
Peter Zijlstra17248132013-12-17 12:44:49 +01007983 if (ret)
7984 goto undo;
7985
Wanpeng Lia1963b82015-03-17 19:15:31 +08007986 ret = sched_rt_global_constraints();
Peter Zijlstra17248132013-12-17 12:44:49 +01007987 if (ret)
7988 goto undo;
7989
7990 sched_rt_do_global();
7991 sched_dl_do_global();
7992 }
7993 if (0) {
7994undo:
7995 sysctl_sched_rt_period = old_period;
7996 sysctl_sched_rt_runtime = old_runtime;
7997 }
7998 mutex_unlock(&mutex);
7999
8000 return ret;
8001}
8002
Clark Williamsce0dbbb2013-02-07 09:47:04 -06008003int sched_rr_handler(struct ctl_table *table, int write,
8004 void __user *buffer, size_t *lenp,
8005 loff_t *ppos)
8006{
8007 int ret;
8008 static DEFINE_MUTEX(mutex);
8009
8010 mutex_lock(&mutex);
8011 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8012 /* make sure that internally we keep jiffies */
8013 /* also, writing zero resets timeslice to default */
8014 if (!ret && write) {
8015 sched_rr_timeslice = sched_rr_timeslice <= 0 ?
8016 RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice);
8017 }
8018 mutex_unlock(&mutex);
8019 return ret;
8020}
8021
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008022#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008023
Tejun Heoa7c6d552013-08-08 20:11:23 -04008024static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008025{
Tejun Heoa7c6d552013-08-08 20:11:23 -04008026 return css ? container_of(css, struct task_group, css) : NULL;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008027}
8028
Tejun Heoeb954192013-08-08 20:11:23 -04008029static struct cgroup_subsys_state *
8030cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008031{
Tejun Heoeb954192013-08-08 20:11:23 -04008032 struct task_group *parent = css_tg(parent_css);
8033 struct task_group *tg;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008034
Tejun Heoeb954192013-08-08 20:11:23 -04008035 if (!parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008036 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08008037 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008038 }
8039
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008040 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008041 if (IS_ERR(tg))
8042 return ERR_PTR(-ENOMEM);
8043
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008044 return &tg->css;
8045}
8046
Tejun Heoeb954192013-08-08 20:11:23 -04008047static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08008048{
Tejun Heoeb954192013-08-08 20:11:23 -04008049 struct task_group *tg = css_tg(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04008050 struct task_group *parent = css_tg(css->parent);
Li Zefanace783b2013-01-24 14:30:48 +08008051
Tejun Heo63876982013-08-08 20:11:23 -04008052 if (parent)
8053 sched_online_group(tg, parent);
Li Zefanace783b2013-01-24 14:30:48 +08008054 return 0;
8055}
8056
Tejun Heoeb954192013-08-08 20:11:23 -04008057static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008058{
Tejun Heoeb954192013-08-08 20:11:23 -04008059 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008060
8061 sched_destroy_group(tg);
8062}
8063
Tejun Heoeb954192013-08-08 20:11:23 -04008064static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08008065{
Tejun Heoeb954192013-08-08 20:11:23 -04008066 struct task_group *tg = css_tg(css);
Li Zefanace783b2013-01-24 14:30:48 +08008067
8068 sched_offline_group(tg);
8069}
8070
Aleksa Sarai7e476822015-06-09 21:32:09 +10008071static void cpu_cgroup_fork(struct task_struct *task, void *private)
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008072{
8073 sched_move_task(task);
8074}
8075
Tejun Heoeb954192013-08-08 20:11:23 -04008076static int cpu_cgroup_can_attach(struct cgroup_subsys_state *css,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008077 struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008078{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008079 struct task_struct *task;
8080
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008081 cgroup_taskset_for_each(task, tset) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008082#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heoeb954192013-08-08 20:11:23 -04008083 if (!sched_rt_can_attach(css_tg(css), task))
Tejun Heobb9d97b2011-12-12 18:12:21 -08008084 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008085#else
Tejun Heobb9d97b2011-12-12 18:12:21 -08008086 /* We don't support RT-tasks being in separate groups */
8087 if (task->sched_class != &fair_sched_class)
8088 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008089#endif
Tejun Heobb9d97b2011-12-12 18:12:21 -08008090 }
Ben Blumbe367d02009-09-23 15:56:31 -07008091 return 0;
8092}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008093
Tejun Heoeb954192013-08-08 20:11:23 -04008094static void cpu_cgroup_attach(struct cgroup_subsys_state *css,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008095 struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008096{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008097 struct task_struct *task;
8098
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008099 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008100 sched_move_task(task);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008101}
8102
Tejun Heoeb954192013-08-08 20:11:23 -04008103static void cpu_cgroup_exit(struct cgroup_subsys_state *css,
8104 struct cgroup_subsys_state *old_css,
8105 struct task_struct *task)
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01008106{
8107 /*
8108 * cgroup_exit() is called in the copy_process() failure path.
8109 * Ignore this case since the task hasn't ran yet, this avoids
8110 * trying to poke a half freed task state from generic code.
8111 */
8112 if (!(task->flags & PF_EXITING))
8113 return;
8114
8115 sched_move_task(task);
8116}
8117
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008118#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008119static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
8120 struct cftype *cftype, u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008121{
Tejun Heo182446d2013-08-08 20:11:24 -04008122 return sched_group_set_shares(css_tg(css), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008123}
8124
Tejun Heo182446d2013-08-08 20:11:24 -04008125static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
8126 struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008127{
Tejun Heo182446d2013-08-08 20:11:24 -04008128 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008129
Nikhil Raoc8b28112011-05-18 14:37:48 -07008130 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008131}
Paul Turnerab84d312011-07-21 09:43:28 -07008132
8133#ifdef CONFIG_CFS_BANDWIDTH
Paul Turnera790de92011-07-21 09:43:29 -07008134static DEFINE_MUTEX(cfs_constraints_mutex);
8135
Paul Turnerab84d312011-07-21 09:43:28 -07008136const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
8137const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
8138
Paul Turnera790de92011-07-21 09:43:29 -07008139static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
8140
Paul Turnerab84d312011-07-21 09:43:28 -07008141static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
8142{
Paul Turner56f570e2011-11-07 20:26:33 -08008143 int i, ret = 0, runtime_enabled, runtime_was_enabled;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008144 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnerab84d312011-07-21 09:43:28 -07008145
8146 if (tg == &root_task_group)
8147 return -EINVAL;
8148
8149 /*
8150 * Ensure we have at some amount of bandwidth every period. This is
8151 * to prevent reaching a state of large arrears when throttled via
8152 * entity_tick() resulting in prolonged exit starvation.
8153 */
8154 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
8155 return -EINVAL;
8156
8157 /*
8158 * Likewise, bound things on the otherside by preventing insane quota
8159 * periods. This also allows us to normalize in computing quota
8160 * feasibility.
8161 */
8162 if (period > max_cfs_quota_period)
8163 return -EINVAL;
8164
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008165 /*
8166 * Prevent race between setting of cfs_rq->runtime_enabled and
8167 * unthrottle_offline_cfs_rqs().
8168 */
8169 get_online_cpus();
Paul Turnera790de92011-07-21 09:43:29 -07008170 mutex_lock(&cfs_constraints_mutex);
8171 ret = __cfs_schedulable(tg, period, quota);
8172 if (ret)
8173 goto out_unlock;
8174
Paul Turner58088ad2011-07-21 09:43:31 -07008175 runtime_enabled = quota != RUNTIME_INF;
Paul Turner56f570e2011-11-07 20:26:33 -08008176 runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
Ben Segall1ee14e62013-10-16 11:16:12 -07008177 /*
8178 * If we need to toggle cfs_bandwidth_used, off->on must occur
8179 * before making related changes, and on->off must occur afterwards
8180 */
8181 if (runtime_enabled && !runtime_was_enabled)
8182 cfs_bandwidth_usage_inc();
Paul Turnerab84d312011-07-21 09:43:28 -07008183 raw_spin_lock_irq(&cfs_b->lock);
8184 cfs_b->period = ns_to_ktime(period);
8185 cfs_b->quota = quota;
Paul Turner58088ad2011-07-21 09:43:31 -07008186
Paul Turnera9cf55b2011-07-21 09:43:32 -07008187 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07008188 /* restart the period timer (if active) to handle new period expiry */
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02008189 if (runtime_enabled)
8190 start_cfs_bandwidth(cfs_b);
Paul Turnerab84d312011-07-21 09:43:28 -07008191 raw_spin_unlock_irq(&cfs_b->lock);
8192
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008193 for_each_online_cpu(i) {
Paul Turnerab84d312011-07-21 09:43:28 -07008194 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
Peter Zijlstra029632f2011-10-25 10:00:11 +02008195 struct rq *rq = cfs_rq->rq;
Paul Turnerab84d312011-07-21 09:43:28 -07008196
8197 raw_spin_lock_irq(&rq->lock);
Paul Turner58088ad2011-07-21 09:43:31 -07008198 cfs_rq->runtime_enabled = runtime_enabled;
Paul Turnerab84d312011-07-21 09:43:28 -07008199 cfs_rq->runtime_remaining = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07008200
Peter Zijlstra029632f2011-10-25 10:00:11 +02008201 if (cfs_rq->throttled)
Paul Turner671fd9d2011-07-21 09:43:34 -07008202 unthrottle_cfs_rq(cfs_rq);
Paul Turnerab84d312011-07-21 09:43:28 -07008203 raw_spin_unlock_irq(&rq->lock);
8204 }
Ben Segall1ee14e62013-10-16 11:16:12 -07008205 if (runtime_was_enabled && !runtime_enabled)
8206 cfs_bandwidth_usage_dec();
Paul Turnera790de92011-07-21 09:43:29 -07008207out_unlock:
8208 mutex_unlock(&cfs_constraints_mutex);
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008209 put_online_cpus();
Paul Turnerab84d312011-07-21 09:43:28 -07008210
Paul Turnera790de92011-07-21 09:43:29 -07008211 return ret;
Paul Turnerab84d312011-07-21 09:43:28 -07008212}
8213
8214int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
8215{
8216 u64 quota, period;
8217
Peter Zijlstra029632f2011-10-25 10:00:11 +02008218 period = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008219 if (cfs_quota_us < 0)
8220 quota = RUNTIME_INF;
8221 else
8222 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
8223
8224 return tg_set_cfs_bandwidth(tg, period, quota);
8225}
8226
8227long tg_get_cfs_quota(struct task_group *tg)
8228{
8229 u64 quota_us;
8230
Peter Zijlstra029632f2011-10-25 10:00:11 +02008231 if (tg->cfs_bandwidth.quota == RUNTIME_INF)
Paul Turnerab84d312011-07-21 09:43:28 -07008232 return -1;
8233
Peter Zijlstra029632f2011-10-25 10:00:11 +02008234 quota_us = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008235 do_div(quota_us, NSEC_PER_USEC);
8236
8237 return quota_us;
8238}
8239
8240int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
8241{
8242 u64 quota, period;
8243
8244 period = (u64)cfs_period_us * NSEC_PER_USEC;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008245 quota = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008246
Paul Turnerab84d312011-07-21 09:43:28 -07008247 return tg_set_cfs_bandwidth(tg, period, quota);
8248}
8249
8250long tg_get_cfs_period(struct task_group *tg)
8251{
8252 u64 cfs_period_us;
8253
Peter Zijlstra029632f2011-10-25 10:00:11 +02008254 cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008255 do_div(cfs_period_us, NSEC_PER_USEC);
8256
8257 return cfs_period_us;
8258}
8259
Tejun Heo182446d2013-08-08 20:11:24 -04008260static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
8261 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008262{
Tejun Heo182446d2013-08-08 20:11:24 -04008263 return tg_get_cfs_quota(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008264}
8265
Tejun Heo182446d2013-08-08 20:11:24 -04008266static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
8267 struct cftype *cftype, s64 cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008268{
Tejun Heo182446d2013-08-08 20:11:24 -04008269 return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008270}
8271
Tejun Heo182446d2013-08-08 20:11:24 -04008272static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
8273 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008274{
Tejun Heo182446d2013-08-08 20:11:24 -04008275 return tg_get_cfs_period(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008276}
8277
Tejun Heo182446d2013-08-08 20:11:24 -04008278static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
8279 struct cftype *cftype, u64 cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008280{
Tejun Heo182446d2013-08-08 20:11:24 -04008281 return tg_set_cfs_period(css_tg(css), cfs_period_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008282}
8283
Paul Turnera790de92011-07-21 09:43:29 -07008284struct cfs_schedulable_data {
8285 struct task_group *tg;
8286 u64 period, quota;
8287};
8288
8289/*
8290 * normalize group quota/period to be quota/max_period
8291 * note: units are usecs
8292 */
8293static u64 normalize_cfs_quota(struct task_group *tg,
8294 struct cfs_schedulable_data *d)
8295{
8296 u64 quota, period;
8297
8298 if (tg == d->tg) {
8299 period = d->period;
8300 quota = d->quota;
8301 } else {
8302 period = tg_get_cfs_period(tg);
8303 quota = tg_get_cfs_quota(tg);
8304 }
8305
8306 /* note: these should typically be equivalent */
8307 if (quota == RUNTIME_INF || quota == -1)
8308 return RUNTIME_INF;
8309
8310 return to_ratio(period, quota);
8311}
8312
8313static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
8314{
8315 struct cfs_schedulable_data *d = data;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008316 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008317 s64 quota = 0, parent_quota = -1;
8318
8319 if (!tg->parent) {
8320 quota = RUNTIME_INF;
8321 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02008322 struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008323
8324 quota = normalize_cfs_quota(tg, d);
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008325 parent_quota = parent_b->hierarchical_quota;
Paul Turnera790de92011-07-21 09:43:29 -07008326
8327 /*
8328 * ensure max(child_quota) <= parent_quota, inherit when no
8329 * limit is set
8330 */
8331 if (quota == RUNTIME_INF)
8332 quota = parent_quota;
8333 else if (parent_quota != RUNTIME_INF && quota > parent_quota)
8334 return -EINVAL;
8335 }
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008336 cfs_b->hierarchical_quota = quota;
Paul Turnera790de92011-07-21 09:43:29 -07008337
8338 return 0;
8339}
8340
8341static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
8342{
Paul Turner82774342011-07-21 09:43:35 -07008343 int ret;
Paul Turnera790de92011-07-21 09:43:29 -07008344 struct cfs_schedulable_data data = {
8345 .tg = tg,
8346 .period = period,
8347 .quota = quota,
8348 };
8349
8350 if (quota != RUNTIME_INF) {
8351 do_div(data.period, NSEC_PER_USEC);
8352 do_div(data.quota, NSEC_PER_USEC);
8353 }
8354
Paul Turner82774342011-07-21 09:43:35 -07008355 rcu_read_lock();
8356 ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
8357 rcu_read_unlock();
8358
8359 return ret;
Paul Turnera790de92011-07-21 09:43:29 -07008360}
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008361
Tejun Heo2da8ca82013-12-05 12:28:04 -05008362static int cpu_stats_show(struct seq_file *sf, void *v)
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008363{
Tejun Heo2da8ca82013-12-05 12:28:04 -05008364 struct task_group *tg = css_tg(seq_css(sf));
Peter Zijlstra029632f2011-10-25 10:00:11 +02008365 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008366
Tejun Heo44ffc752013-12-05 12:28:01 -05008367 seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
8368 seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
8369 seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008370
8371 return 0;
8372}
Paul Turnerab84d312011-07-21 09:43:28 -07008373#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008374#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008375
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008376#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008377static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
8378 struct cftype *cft, s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008379{
Tejun Heo182446d2013-08-08 20:11:24 -04008380 return sched_group_set_rt_runtime(css_tg(css), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008381}
8382
Tejun Heo182446d2013-08-08 20:11:24 -04008383static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
8384 struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008385{
Tejun Heo182446d2013-08-08 20:11:24 -04008386 return sched_group_rt_runtime(css_tg(css));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008387}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008388
Tejun Heo182446d2013-08-08 20:11:24 -04008389static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
8390 struct cftype *cftype, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008391{
Tejun Heo182446d2013-08-08 20:11:24 -04008392 return sched_group_set_rt_period(css_tg(css), rt_period_us);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008393}
8394
Tejun Heo182446d2013-08-08 20:11:24 -04008395static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
8396 struct cftype *cft)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008397{
Tejun Heo182446d2013-08-08 20:11:24 -04008398 return sched_group_rt_period(css_tg(css));
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008399}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008400#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008401
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008402static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008403#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008404 {
8405 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008406 .read_u64 = cpu_shares_read_u64,
8407 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008408 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008409#endif
Paul Turnerab84d312011-07-21 09:43:28 -07008410#ifdef CONFIG_CFS_BANDWIDTH
8411 {
8412 .name = "cfs_quota_us",
8413 .read_s64 = cpu_cfs_quota_read_s64,
8414 .write_s64 = cpu_cfs_quota_write_s64,
8415 },
8416 {
8417 .name = "cfs_period_us",
8418 .read_u64 = cpu_cfs_period_read_u64,
8419 .write_u64 = cpu_cfs_period_write_u64,
8420 },
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008421 {
8422 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05008423 .seq_show = cpu_stats_show,
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008424 },
Paul Turnerab84d312011-07-21 09:43:28 -07008425#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008426#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008427 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008428 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008429 .read_s64 = cpu_rt_runtime_read,
8430 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008431 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008432 {
8433 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008434 .read_u64 = cpu_rt_period_read_uint,
8435 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008436 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008437#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07008438 { } /* terminate */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008439};
8440
Tejun Heo073219e2014-02-08 10:36:58 -05008441struct cgroup_subsys cpu_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008442 .css_alloc = cpu_cgroup_css_alloc,
8443 .css_free = cpu_cgroup_css_free,
Li Zefanace783b2013-01-24 14:30:48 +08008444 .css_online = cpu_cgroup_css_online,
8445 .css_offline = cpu_cgroup_css_offline,
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008446 .fork = cpu_cgroup_fork,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008447 .can_attach = cpu_cgroup_can_attach,
8448 .attach = cpu_cgroup_attach,
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01008449 .exit = cpu_cgroup_exit,
Tejun Heo55779642014-07-15 11:05:09 -04008450 .legacy_cftypes = cpu_files,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008451 .early_init = 1,
8452};
8453
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008454#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008455
Paul E. McKenneyb637a322012-09-19 16:58:38 -07008456void dump_cpu_task(int cpu)
8457{
8458 pr_info("Task dump for CPU %d:\n", cpu);
8459 sched_show_task(cpu_curr(cpu));
8460}