blob: 2e3b983da836a70bc642489e6779b7e8fbf2433b [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{
Peter Zijlstra25834c72015-05-15 17:43:34 +02001156 lockdep_assert_held(&p->pi_lock);
1157
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001158 if (p->sched_class->set_cpus_allowed)
1159 p->sched_class->set_cpus_allowed(p, new_mask);
1160
1161 cpumask_copy(&p->cpus_allowed, new_mask);
1162 p->nr_cpus_allowed = cpumask_weight(new_mask);
1163}
1164
1165/*
1166 * Change a given task's CPU affinity. Migrate the thread to a
1167 * proper CPU and schedule it away if the CPU it's executing on
1168 * is removed from the allowed bitmask.
1169 *
1170 * NOTE: the caller must have a valid reference to the task, the
1171 * task must not exit() & deallocate itself prematurely. The
1172 * call is not atomic; no spinlocks may be held.
1173 */
Peter Zijlstra25834c72015-05-15 17:43:34 +02001174static int __set_cpus_allowed_ptr(struct task_struct *p,
1175 const struct cpumask *new_mask, bool check)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001176{
1177 unsigned long flags;
1178 struct rq *rq;
1179 unsigned int dest_cpu;
1180 int ret = 0;
1181
1182 rq = task_rq_lock(p, &flags);
1183
Peter Zijlstra25834c72015-05-15 17:43:34 +02001184 /*
1185 * Must re-check here, to close a race against __kthread_bind(),
1186 * sched_setaffinity() is not guaranteed to observe the flag.
1187 */
1188 if (check && (p->flags & PF_NO_SETAFFINITY)) {
1189 ret = -EINVAL;
1190 goto out;
1191 }
1192
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001193 if (cpumask_equal(&p->cpus_allowed, new_mask))
1194 goto out;
1195
1196 if (!cpumask_intersects(new_mask, cpu_active_mask)) {
1197 ret = -EINVAL;
1198 goto out;
1199 }
1200
1201 do_set_cpus_allowed(p, new_mask);
1202
1203 /* Can the task run on the task's current CPU? If so, we're done */
1204 if (cpumask_test_cpu(task_cpu(p), new_mask))
1205 goto out;
1206
1207 dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
1208 if (task_running(rq, p) || p->state == TASK_WAKING) {
1209 struct migration_arg arg = { p, dest_cpu };
1210 /* Need help from migration thread: drop lock and wait. */
1211 task_rq_unlock(rq, p, &flags);
1212 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
1213 tlb_migrate_finish(p->mm);
1214 return 0;
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001215 } else if (task_on_rq_queued(p)) {
1216 /*
1217 * OK, since we're going to drop the lock immediately
1218 * afterwards anyway.
1219 */
1220 lockdep_unpin_lock(&rq->lock);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001221 rq = move_queued_task(rq, p, dest_cpu);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001222 lockdep_pin_lock(&rq->lock);
1223 }
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001224out:
1225 task_rq_unlock(rq, p, &flags);
1226
1227 return ret;
1228}
Peter Zijlstra25834c72015-05-15 17:43:34 +02001229
1230int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1231{
1232 return __set_cpus_allowed_ptr(p, new_mask, false);
1233}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001234EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
1235
Ingo Molnardd41f592007-07-09 18:51:59 +02001236void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001237{
Peter Zijlstrae2912002009-12-16 18:04:36 +01001238#ifdef CONFIG_SCHED_DEBUG
1239 /*
1240 * We should never call set_task_cpu() on a blocked task,
1241 * ttwu() will sort out the placement.
1242 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01001243 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
Oleg Nesterove2336f62014-10-08 20:33:48 +02001244 !p->on_rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001245
1246#ifdef CONFIG_LOCKDEP
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001247 /*
1248 * The caller should hold either p->pi_lock or rq->lock, when changing
1249 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
1250 *
1251 * sched_move_task() holds both and thus holding either pins the cgroup,
Peter Zijlstra8323f262012-06-22 13:36:05 +02001252 * see task_group().
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001253 *
1254 * Furthermore, all task_rq users should acquire both locks, see
1255 * task_rq_lock().
1256 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001257 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
1258 lockdep_is_held(&task_rq(p)->lock)));
1259#endif
Peter Zijlstrae2912002009-12-16 18:04:36 +01001260#endif
1261
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08001262 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001263
Peter Zijlstra0c697742009-12-22 15:43:19 +01001264 if (task_cpu(p) != new_cpu) {
Paul Turner0a74bef2012-10-04 13:18:30 +02001265 if (p->sched_class->migrate_task_rq)
1266 p->sched_class->migrate_task_rq(p, new_cpu);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001267 p->se.nr_migrations++;
Peter Zijlstraff303e62015-04-17 20:05:30 +02001268 perf_event_task_migrate(p);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001269 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001270
1271 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001272}
1273
Peter Zijlstraac66f542013-10-07 11:29:16 +01001274static void __migrate_swap_task(struct task_struct *p, int cpu)
1275{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001276 if (task_on_rq_queued(p)) {
Peter Zijlstraac66f542013-10-07 11:29:16 +01001277 struct rq *src_rq, *dst_rq;
1278
1279 src_rq = task_rq(p);
1280 dst_rq = cpu_rq(cpu);
1281
1282 deactivate_task(src_rq, p, 0);
1283 set_task_cpu(p, cpu);
1284 activate_task(dst_rq, p, 0);
1285 check_preempt_curr(dst_rq, p, 0);
1286 } else {
1287 /*
1288 * Task isn't running anymore; make it appear like we migrated
1289 * it before it went to sleep. This means on wakeup we make the
1290 * previous cpu our targer instead of where it really is.
1291 */
1292 p->wake_cpu = cpu;
1293 }
1294}
1295
1296struct migration_swap_arg {
1297 struct task_struct *src_task, *dst_task;
1298 int src_cpu, dst_cpu;
1299};
1300
1301static int migrate_swap_stop(void *data)
1302{
1303 struct migration_swap_arg *arg = data;
1304 struct rq *src_rq, *dst_rq;
1305 int ret = -EAGAIN;
1306
1307 src_rq = cpu_rq(arg->src_cpu);
1308 dst_rq = cpu_rq(arg->dst_cpu);
1309
Peter Zijlstra74602312013-10-10 20:17:22 +02001310 double_raw_lock(&arg->src_task->pi_lock,
1311 &arg->dst_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001312 double_rq_lock(src_rq, dst_rq);
1313 if (task_cpu(arg->dst_task) != arg->dst_cpu)
1314 goto unlock;
1315
1316 if (task_cpu(arg->src_task) != arg->src_cpu)
1317 goto unlock;
1318
1319 if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task)))
1320 goto unlock;
1321
1322 if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task)))
1323 goto unlock;
1324
1325 __migrate_swap_task(arg->src_task, arg->dst_cpu);
1326 __migrate_swap_task(arg->dst_task, arg->src_cpu);
1327
1328 ret = 0;
1329
1330unlock:
1331 double_rq_unlock(src_rq, dst_rq);
Peter Zijlstra74602312013-10-10 20:17:22 +02001332 raw_spin_unlock(&arg->dst_task->pi_lock);
1333 raw_spin_unlock(&arg->src_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001334
1335 return ret;
1336}
1337
1338/*
1339 * Cross migrate two tasks
1340 */
1341int migrate_swap(struct task_struct *cur, struct task_struct *p)
1342{
1343 struct migration_swap_arg arg;
1344 int ret = -EINVAL;
1345
Peter Zijlstraac66f542013-10-07 11:29:16 +01001346 arg = (struct migration_swap_arg){
1347 .src_task = cur,
1348 .src_cpu = task_cpu(cur),
1349 .dst_task = p,
1350 .dst_cpu = task_cpu(p),
1351 };
1352
1353 if (arg.src_cpu == arg.dst_cpu)
1354 goto out;
1355
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02001356 /*
1357 * These three tests are all lockless; this is OK since all of them
1358 * will be re-checked with proper locks held further down the line.
1359 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01001360 if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
1361 goto out;
1362
1363 if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task)))
1364 goto out;
1365
1366 if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task)))
1367 goto out;
1368
Mel Gorman286549d2014-01-21 15:51:03 -08001369 trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001370 ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
1371
1372out:
Peter Zijlstraac66f542013-10-07 11:29:16 +01001373 return ret;
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 * wait_task_inactive - wait for a thread to unschedule.
1378 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001379 * If @match_state is nonzero, it's the @p->state value just checked and
1380 * not expected to change. If it changes, i.e. @p might have woken up,
1381 * then return zero. When we succeed in waiting for @p to be off its CPU,
1382 * we return a positive number (its total switch count). If a second call
1383 * a short while later returns the same number, the caller can be sure that
1384 * @p has remained unscheduled the whole time.
1385 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * The caller must ensure that the task *will* unschedule sometime soon,
1387 * else this function might spin for a *long* time. This function can't
1388 * be called with interrupts off, or it may introduce deadlock with
1389 * smp_call_function() if an IPI is sent by the same process we are
1390 * waiting to become inactive.
1391 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001392unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001395 int running, queued;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001396 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001397 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Andi Kleen3a5c3592007-10-15 17:00:14 +02001399 for (;;) {
1400 /*
1401 * We do the initial early heuristics without holding
1402 * any task-queue locks at all. We'll only try to get
1403 * the runqueue lock when things look like they will
1404 * work out!
1405 */
1406 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001407
Andi Kleen3a5c3592007-10-15 17:00:14 +02001408 /*
1409 * If the task is actively running on another CPU
1410 * still, just relax and busy-wait without holding
1411 * any locks.
1412 *
1413 * NOTE! Since we don't hold any locks, it's not
1414 * even sure that "rq" stays as the right runqueue!
1415 * But we don't care, since "task_running()" will
1416 * return false if the runqueue has changed and p
1417 * is actually now running somewhere else!
1418 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001419 while (task_running(rq, p)) {
1420 if (match_state && unlikely(p->state != match_state))
1421 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02001422 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07001423 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001424
Andi Kleen3a5c3592007-10-15 17:00:14 +02001425 /*
1426 * Ok, time to look more closely! We need the rq
1427 * lock now, to be *sure*. If we're wrong, we'll
1428 * just go back and repeat.
1429 */
1430 rq = task_rq_lock(p, &flags);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02001431 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001432 running = task_running(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001433 queued = task_on_rq_queued(p);
Roland McGrath85ba2d82008-07-25 19:45:58 -07001434 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07001435 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07001436 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001437 task_rq_unlock(rq, p, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001438
Andi Kleen3a5c3592007-10-15 17:00:14 +02001439 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07001440 * If it changed from the expected state, bail out now.
1441 */
1442 if (unlikely(!ncsw))
1443 break;
1444
1445 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02001446 * Was it really running after all now that we
1447 * checked with the proper locks actually held?
1448 *
1449 * Oops. Go back and try again..
1450 */
1451 if (unlikely(running)) {
1452 cpu_relax();
1453 continue;
1454 }
1455
1456 /*
1457 * It's not enough that it's not actively running,
1458 * it must be off the runqueue _entirely_, and not
1459 * preempted!
1460 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00001461 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02001462 * running right now), it's preempted, and we should
1463 * yield - it could be a while.
1464 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001465 if (unlikely(queued)) {
Thomas Gleixner8eb90c32011-02-23 23:52:21 +00001466 ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
1467
1468 set_current_state(TASK_UNINTERRUPTIBLE);
1469 schedule_hrtimeout(&to, HRTIMER_MODE_REL);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001470 continue;
1471 }
1472
1473 /*
1474 * Ahh, all good. It wasn't running, and it wasn't
1475 * runnable, which means that it will never become
1476 * running in the future either. We're all done!
1477 */
1478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07001480
1481 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484/***
1485 * kick_process - kick a running thread to enter/exit the kernel
1486 * @p: the to-be-kicked thread
1487 *
1488 * Cause a process which is running on another CPU to enter
1489 * kernel-mode, without any delay. (to get signals handled.)
1490 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001491 * NOTE: this function doesn't have to take the runqueue lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 * because all it wants to ensure is that the remote task enters
1493 * the kernel. If the IPI races and the task has been migrated
1494 * to another CPU then no harm is done and the purpose has been
1495 * achieved as well.
1496 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001497void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 int cpu;
1500
1501 preempt_disable();
1502 cpu = task_cpu(p);
1503 if ((cpu != smp_processor_id()) && task_curr(p))
1504 smp_send_reschedule(cpu);
1505 preempt_enable();
1506}
Rusty Russellb43e3522009-06-12 22:27:00 -06001507EXPORT_SYMBOL_GPL(kick_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Oleg Nesterov30da6882010-03-15 10:10:19 +01001509/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001510 * ->cpus_allowed is protected by both rq->lock and p->pi_lock
Oleg Nesterov30da6882010-03-15 10:10:19 +01001511 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001512static int select_fallback_rq(int cpu, struct task_struct *p)
1513{
Tang Chenaa00d892013-02-22 16:33:33 -08001514 int nid = cpu_to_node(cpu);
1515 const struct cpumask *nodemask = NULL;
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001516 enum { cpuset, possible, fail } state = cpuset;
1517 int dest_cpu;
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001518
Tang Chenaa00d892013-02-22 16:33:33 -08001519 /*
1520 * If the node that the cpu is on has been offlined, cpu_to_node()
1521 * will return -1. There is no cpu on the node, and we should
1522 * select the cpu on the other node.
1523 */
1524 if (nid != -1) {
1525 nodemask = cpumask_of_node(nid);
1526
1527 /* Look for allowed, online CPU in same node. */
1528 for_each_cpu(dest_cpu, nodemask) {
1529 if (!cpu_online(dest_cpu))
1530 continue;
1531 if (!cpu_active(dest_cpu))
1532 continue;
1533 if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
1534 return dest_cpu;
1535 }
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001536 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001537
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001538 for (;;) {
1539 /* Any allowed, online CPU? */
Srivatsa S. Bhate3831ed2012-03-30 19:40:28 +05301540 for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) {
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001541 if (!cpu_online(dest_cpu))
1542 continue;
1543 if (!cpu_active(dest_cpu))
1544 continue;
1545 goto out;
1546 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001547
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001548 switch (state) {
1549 case cpuset:
1550 /* No more Mr. Nice Guy. */
1551 cpuset_cpus_allowed_fallback(p);
1552 state = possible;
1553 break;
1554
1555 case possible:
1556 do_set_cpus_allowed(p, cpu_possible_mask);
1557 state = fail;
1558 break;
1559
1560 case fail:
1561 BUG();
1562 break;
1563 }
1564 }
1565
1566out:
1567 if (state != cpuset) {
1568 /*
1569 * Don't tell them about moving exiting tasks or
1570 * kernel threads (both mm NULL), since they never
1571 * leave kernel.
1572 */
1573 if (p->mm && printk_ratelimit()) {
John Stultzaac74dc2014-06-04 16:11:40 -07001574 printk_deferred("process %d (%s) no longer affine to cpu%d\n",
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01001575 task_pid_nr(p), p->comm, cpu);
1576 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001577 }
1578
1579 return dest_cpu;
1580}
1581
Peter Zijlstrae2912002009-12-16 18:04:36 +01001582/*
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001583 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01001584 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001585static inline
Peter Zijlstraac66f542013-10-07 11:29:16 +01001586int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001587{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001588 lockdep_assert_held(&p->pi_lock);
1589
Wanpeng Li6c1d9412014-11-05 09:14:37 +08001590 if (p->nr_cpus_allowed > 1)
1591 cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
Peter Zijlstrae2912002009-12-16 18:04:36 +01001592
1593 /*
1594 * In order not to call set_task_cpu() on a blocking task we need
1595 * to rely on ttwu() to place the task on a valid ->cpus_allowed
1596 * cpu.
1597 *
1598 * Since this is common to all placement strategies, this lives here.
1599 *
1600 * [ this allows ->select_task() to simply return task_cpu(p) and
1601 * not worry about this generic constraint ]
1602 */
Peter Zijlstrafa17b502011-06-16 12:23:22 +02001603 if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) ||
Peter Zijlstra70f11202009-12-20 17:36:27 +01001604 !cpu_online(cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01001605 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01001606
1607 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001608}
Mike Galbraith09a40af2010-04-15 07:29:59 +02001609
1610static void update_avg(u64 *avg, u64 sample)
1611{
1612 s64 diff = sample - *avg;
1613 *avg += diff >> 3;
1614}
Peter Zijlstra25834c72015-05-15 17:43:34 +02001615
1616#else
1617
1618static inline int __set_cpus_allowed_ptr(struct task_struct *p,
1619 const struct cpumask *new_mask, bool check)
1620{
1621 return set_cpus_allowed_ptr(p, new_mask);
1622}
1623
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001624#endif /* CONFIG_SMP */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01001625
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001626static void
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001627ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001628{
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001629#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001630 struct rq *rq = this_rq();
Tejun Heo9ed38112009-12-03 15:08:03 +09001631
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001632#ifdef CONFIG_SMP
1633 int this_cpu = smp_processor_id();
Tejun Heo9ed38112009-12-03 15:08:03 +09001634
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001635 if (cpu == this_cpu) {
1636 schedstat_inc(rq, ttwu_local);
1637 schedstat_inc(p, se.statistics.nr_wakeups_local);
1638 } else {
1639 struct sched_domain *sd;
1640
1641 schedstat_inc(p, se.statistics.nr_wakeups_remote);
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001642 rcu_read_lock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001643 for_each_domain(this_cpu, sd) {
1644 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
1645 schedstat_inc(sd, ttwu_wake_remote);
1646 break;
1647 }
1648 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02001649 rcu_read_unlock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001650 }
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001651
1652 if (wake_flags & WF_MIGRATED)
1653 schedstat_inc(p, se.statistics.nr_wakeups_migrate);
1654
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001655#endif /* CONFIG_SMP */
1656
1657 schedstat_inc(rq, ttwu_count);
1658 schedstat_inc(p, se.statistics.nr_wakeups);
1659
1660 if (wake_flags & WF_SYNC)
1661 schedstat_inc(p, se.statistics.nr_wakeups_sync);
1662
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001663#endif /* CONFIG_SCHEDSTATS */
Tejun Heo9ed38112009-12-03 15:08:03 +09001664}
1665
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001666static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001667{
Tejun Heo9ed38112009-12-03 15:08:03 +09001668 activate_task(rq, p, en_flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001669 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrac2f71152011-04-13 13:28:56 +02001670
1671 /* if a worker is waking up, notify workqueue */
1672 if (p->flags & PF_WQ_WORKER)
1673 wq_worker_waking_up(p, cpu_of(rq));
Tejun Heo9ed38112009-12-03 15:08:03 +09001674}
1675
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001676/*
1677 * Mark the task runnable and perform wakeup-preemption.
1678 */
Peter Zijlstra89363382011-04-05 17:23:42 +02001679static void
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001680ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09001681{
Tejun Heo9ed38112009-12-03 15:08:03 +09001682 check_preempt_curr(rq, p, wake_flags);
Tejun Heo9ed38112009-12-03 15:08:03 +09001683 p->state = TASK_RUNNING;
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02001684 trace_sched_wakeup(p);
1685
Tejun Heo9ed38112009-12-03 15:08:03 +09001686#ifdef CONFIG_SMP
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001687 if (p->sched_class->task_woken) {
1688 /*
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001689 * Our task @p is fully woken up and running; so its safe to
1690 * drop the rq->lock, hereafter rq is only used for statistics.
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001691 */
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001692 lockdep_unpin_lock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09001693 p->sched_class->task_woken(rq, p);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001694 lockdep_pin_lock(&rq->lock);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001695 }
Tejun Heo9ed38112009-12-03 15:08:03 +09001696
Steven Rostedte69c6342010-12-06 17:10:31 -05001697 if (rq->idle_stamp) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02001698 u64 delta = rq_clock(rq) - rq->idle_stamp;
Jason Low9bd721c2013-09-13 11:26:52 -07001699 u64 max = 2*rq->max_idle_balance_cost;
Tejun Heo9ed38112009-12-03 15:08:03 +09001700
Jason Lowabfafa52013-09-13 11:26:51 -07001701 update_avg(&rq->avg_idle, delta);
1702
1703 if (rq->avg_idle > max)
Tejun Heo9ed38112009-12-03 15:08:03 +09001704 rq->avg_idle = max;
Jason Lowabfafa52013-09-13 11:26:51 -07001705
Tejun Heo9ed38112009-12-03 15:08:03 +09001706 rq->idle_stamp = 0;
1707 }
1708#endif
1709}
1710
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001711static void
1712ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)
1713{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001714 lockdep_assert_held(&rq->lock);
1715
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001716#ifdef CONFIG_SMP
1717 if (p->sched_contributes_to_load)
1718 rq->nr_uninterruptible--;
1719#endif
1720
1721 ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);
1722 ttwu_do_wakeup(rq, p, wake_flags);
1723}
1724
1725/*
1726 * Called in case the task @p isn't fully descheduled from its runqueue,
1727 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
1728 * since all we need to do is flip p->state to TASK_RUNNING, since
1729 * the task is still ->on_rq.
1730 */
1731static int ttwu_remote(struct task_struct *p, int wake_flags)
1732{
1733 struct rq *rq;
1734 int ret = 0;
1735
1736 rq = __task_rq_lock(p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001737 if (task_on_rq_queued(p)) {
Frederic Weisbecker1ad4ec02013-04-12 01:51:00 +02001738 /* check_preempt_curr() may use rq clock */
1739 update_rq_clock(rq);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001740 ttwu_do_wakeup(rq, p, wake_flags);
1741 ret = 1;
1742 }
1743 __task_rq_unlock(rq);
1744
1745 return ret;
1746}
1747
Peter Zijlstra317f3942011-04-05 17:23:58 +02001748#ifdef CONFIG_SMP
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001749void sched_ttwu_pending(void)
Peter Zijlstra317f3942011-04-05 17:23:58 +02001750{
1751 struct rq *rq = this_rq();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001752 struct llist_node *llist = llist_del_all(&rq->wake_list);
1753 struct task_struct *p;
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001754 unsigned long flags;
Peter Zijlstra317f3942011-04-05 17:23:58 +02001755
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001756 if (!llist)
1757 return;
1758
1759 raw_spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001760 lockdep_pin_lock(&rq->lock);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001761
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001762 while (llist) {
1763 p = llist_entry(llist, struct task_struct, wake_entry);
1764 llist = llist_next(llist);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001765 ttwu_do_activate(rq, p, 0);
1766 }
1767
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001768 lockdep_unpin_lock(&rq->lock);
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001769 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra317f3942011-04-05 17:23:58 +02001770}
1771
1772void scheduler_ipi(void)
1773{
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001774 /*
1775 * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
1776 * TIF_NEED_RESCHED remotely (for the first time) will also send
1777 * this IPI.
1778 */
Peter Zijlstra8cb75e02013-11-20 12:22:37 +01001779 preempt_fold_need_resched();
Peter Zijlstraf27dde82013-08-14 14:55:31 +02001780
Frederic Weisbeckerfd2ac4f2014-03-18 21:12:53 +01001781 if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001782 return;
1783
1784 /*
1785 * Not all reschedule IPI handlers call irq_enter/irq_exit, since
1786 * traditionally all their work was done from the interrupt return
1787 * path. Now that we actually do some work, we need to make sure
1788 * we do call them.
1789 *
1790 * Some archs already do call them, luckily irq_enter/exit nest
1791 * properly.
1792 *
1793 * Arguably we should visit all archs and update all handlers,
1794 * however a fair share of IPIs are still resched only so this would
1795 * somewhat pessimize the simple resched case.
1796 */
1797 irq_enter();
Peter Zijlstrafa14ff42011-09-12 13:06:17 +02001798 sched_ttwu_pending();
Suresh Siddhaca380622011-10-03 15:09:00 -07001799
1800 /*
1801 * Check if someone kicked us for doing the nohz idle load balance.
1802 */
Vincent Guittot873b4c62013-06-05 10:13:11 +02001803 if (unlikely(got_nohz_idle_kick())) {
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001804 this_rq()->idle_balance = 1;
Suresh Siddhaca380622011-10-03 15:09:00 -07001805 raise_softirq_irqoff(SCHED_SOFTIRQ);
Suresh Siddha6eb57e02011-10-03 15:09:01 -07001806 }
Peter Zijlstrac5d753a2011-07-19 15:07:25 -07001807 irq_exit();
Peter Zijlstra317f3942011-04-05 17:23:58 +02001808}
1809
1810static void ttwu_queue_remote(struct task_struct *p, int cpu)
1811{
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001812 struct rq *rq = cpu_rq(cpu);
1813
1814 if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) {
1815 if (!set_nr_if_polling(rq->idle))
1816 smp_send_reschedule(cpu);
1817 else
1818 trace_sched_wake_idle_without_ipi(cpu);
1819 }
Peter Zijlstra317f3942011-04-05 17:23:58 +02001820}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001821
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001822void wake_up_if_idle(int cpu)
1823{
1824 struct rq *rq = cpu_rq(cpu);
1825 unsigned long flags;
1826
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001827 rcu_read_lock();
1828
1829 if (!is_idle_task(rcu_dereference(rq->curr)))
1830 goto out;
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001831
1832 if (set_nr_if_polling(rq->idle)) {
1833 trace_sched_wake_idle_without_ipi(cpu);
1834 } else {
1835 raw_spin_lock_irqsave(&rq->lock, flags);
1836 if (is_idle_task(rq->curr))
1837 smp_send_reschedule(cpu);
1838 /* Else cpu is not in idle, do nothing here */
1839 raw_spin_unlock_irqrestore(&rq->lock, flags);
1840 }
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08001841
1842out:
1843 rcu_read_unlock();
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08001844}
1845
Peter Zijlstra39be3502012-01-26 12:44:34 +01001846bool cpus_share_cache(int this_cpu, int that_cpu)
Peter Zijlstra518cd622011-12-07 15:07:31 +01001847{
1848 return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
1849}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02001850#endif /* CONFIG_SMP */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001851
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001852static void ttwu_queue(struct task_struct *p, int cpu)
1853{
1854 struct rq *rq = cpu_rq(cpu);
1855
Daniel Hellstrom17d9f312011-05-20 04:01:10 +00001856#if defined(CONFIG_SMP)
Peter Zijlstra39be3502012-01-26 12:44:34 +01001857 if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
Peter Zijlstraf01114c2011-05-31 12:26:55 +02001858 sched_clock_cpu(cpu); /* sync clocks x-cpu */
Peter Zijlstra317f3942011-04-05 17:23:58 +02001859 ttwu_queue_remote(p, cpu);
1860 return;
1861 }
1862#endif
1863
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001864 raw_spin_lock(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001865 lockdep_pin_lock(&rq->lock);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001866 ttwu_do_activate(rq, p, 0);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001867 lockdep_unpin_lock(&rq->lock);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001868 raw_spin_unlock(&rq->lock);
Tejun Heo9ed38112009-12-03 15:08:03 +09001869}
1870
1871/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09001873 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09001875 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 *
1877 * Put it on the run-queue if it's not already there. The "current"
1878 * thread is always on the run-queue (except when the actual
1879 * re-schedule is in progress), and as such you're allowed to do
1880 * the simpler "current->state = TASK_RUNNING" to mark yourself
1881 * runnable without the overhead of this.
1882 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02001883 * Return: %true if @p was woken up, %false if it was already running.
Tejun Heo9ed38112009-12-03 15:08:03 +09001884 * or @state didn't match @p's state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001886static int
1887try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 unsigned long flags;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001890 int cpu, success = 0;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001891
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02001892 /*
1893 * If we are going to wake up a thread waiting for CONDITION we
1894 * need to ensure that CONDITION=1 done by the caller can not be
1895 * reordered with p->state check below. This pairs with mb() in
1896 * set_current_state() the waiting thread does.
1897 */
1898 smp_mb__before_spinlock();
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001899 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001900 if (!(p->state & state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 goto out;
1902
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02001903 trace_sched_waking(p);
1904
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001905 success = 1; /* we're going to change ->state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 cpu = task_cpu(p);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001907
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001908 if (p->on_rq && ttwu_remote(p, wake_flags))
1909 goto stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911#ifdef CONFIG_SMP
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001912 /*
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001913 * If the owning (remote) cpu is still in the middle of schedule() with
1914 * this task as prev, wait until its done referencing the task.
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001915 */
Peter Zijlstraf3e94782012-09-12 11:22:00 +02001916 while (p->on_cpu)
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001917 cpu_relax();
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001918 /*
1919 * Pairs with the smp_wmb() in finish_lock_switch().
1920 */
1921 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Peter Zijlstraa8e4f2e2011-04-05 17:23:49 +02001923 p->sched_contributes_to_load = !!task_contributes_to_load(p);
Peter Zijlstrae9c84312009-09-15 14:43:03 +02001924 p->state = TASK_WAKING;
Peter Zijlstraefbbd052009-12-16 18:04:40 +01001925
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02001926 if (p->sched_class->task_waking)
Peter Zijlstra74f8e4b2011-04-05 17:23:47 +02001927 p->sched_class->task_waking(p);
Peter Zijlstraab19cb22009-11-27 15:44:43 +01001928
Peter Zijlstraac66f542013-10-07 11:29:16 +01001929 cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001930 if (task_cpu(p) != cpu) {
1931 wake_flags |= WF_MIGRATED;
Mike Galbraithf5dc3752009-10-09 08:35:03 +02001932 set_task_cpu(p, cpu);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02001933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02001936 ttwu_queue(p, cpu);
1937stat:
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001938 ttwu_stat(p, cpu, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939out:
Peter Zijlstra013fdb82011-04-05 17:23:45 +02001940 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 return success;
1943}
1944
David Howells50fa6102009-04-28 15:01:38 +01001945/**
Tejun Heo21aa9af2010-06-08 21:40:37 +02001946 * try_to_wake_up_local - try to wake up a local task with rq lock held
1947 * @p: the thread to be awakened
1948 *
Peter Zijlstra2acca552011-04-05 17:23:50 +02001949 * Put @p on the run-queue if it's not already there. The caller must
Tejun Heo21aa9af2010-06-08 21:40:37 +02001950 * ensure that this_rq() is locked, @p is bound to this_rq() and not
Peter Zijlstra2acca552011-04-05 17:23:50 +02001951 * the current task.
Tejun Heo21aa9af2010-06-08 21:40:37 +02001952 */
1953static void try_to_wake_up_local(struct task_struct *p)
1954{
1955 struct rq *rq = task_rq(p);
Tejun Heo21aa9af2010-06-08 21:40:37 +02001956
Tejun Heo383efcd2013-03-18 12:22:34 -07001957 if (WARN_ON_ONCE(rq != this_rq()) ||
1958 WARN_ON_ONCE(p == current))
1959 return;
1960
Tejun Heo21aa9af2010-06-08 21:40:37 +02001961 lockdep_assert_held(&rq->lock);
1962
Peter Zijlstra2acca552011-04-05 17:23:50 +02001963 if (!raw_spin_trylock(&p->pi_lock)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001964 /*
1965 * This is OK, because current is on_cpu, which avoids it being
1966 * picked for load-balance and preemption/IRQs are still
1967 * disabled avoiding further scheduler activity on it and we've
1968 * not yet picked a replacement task.
1969 */
1970 lockdep_unpin_lock(&rq->lock);
Peter Zijlstra2acca552011-04-05 17:23:50 +02001971 raw_spin_unlock(&rq->lock);
1972 raw_spin_lock(&p->pi_lock);
1973 raw_spin_lock(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001974 lockdep_pin_lock(&rq->lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02001975 }
Peter Zijlstra2acca552011-04-05 17:23:50 +02001976
Tejun Heo21aa9af2010-06-08 21:40:37 +02001977 if (!(p->state & TASK_NORMAL))
Peter Zijlstra2acca552011-04-05 17:23:50 +02001978 goto out;
Tejun Heo21aa9af2010-06-08 21:40:37 +02001979
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02001980 trace_sched_waking(p);
1981
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001982 if (!task_on_rq_queued(p))
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02001983 ttwu_activate(rq, p, ENQUEUE_WAKEUP);
1984
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02001985 ttwu_do_wakeup(rq, p, 0);
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02001986 ttwu_stat(p, smp_processor_id(), 0);
Peter Zijlstra2acca552011-04-05 17:23:50 +02001987out:
1988 raw_spin_unlock(&p->pi_lock);
Tejun Heo21aa9af2010-06-08 21:40:37 +02001989}
1990
1991/**
David Howells50fa6102009-04-28 15:01:38 +01001992 * wake_up_process - Wake up a specific process
1993 * @p: The process to be woken up.
1994 *
1995 * Attempt to wake up the nominated process and move it to the set of runnable
Yacine Belkadie69f6182013-07-12 20:45:47 +02001996 * processes.
1997 *
1998 * Return: 1 if the process was woken up, 0 if it was already running.
David Howells50fa6102009-04-28 15:01:38 +01001999 *
2000 * It may be assumed that this function implies a write memory barrier before
2001 * changing the task state if and only if any tasks are woken up.
2002 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002003int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Oleg Nesterov9067ac82013-01-21 20:48:17 +01002005 WARN_ON(task_is_stopped_or_traced(p));
2006 return try_to_wake_up(p, TASK_NORMAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008EXPORT_SYMBOL(wake_up_process);
2009
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002010int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 return try_to_wake_up(p, state, 0);
2013}
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015/*
Juri Lellia5e7be32014-09-19 10:22:39 +01002016 * This function clears the sched_dl_entity static params.
2017 */
2018void __dl_clear_params(struct task_struct *p)
2019{
2020 struct sched_dl_entity *dl_se = &p->dl;
2021
2022 dl_se->dl_runtime = 0;
2023 dl_se->dl_deadline = 0;
2024 dl_se->dl_period = 0;
2025 dl_se->flags = 0;
2026 dl_se->dl_bw = 0;
Peter Zijlstra40767b02015-01-28 15:08:03 +01002027
2028 dl_se->dl_throttled = 0;
2029 dl_se->dl_new = 1;
2030 dl_se->dl_yielded = 0;
Juri Lellia5e7be32014-09-19 10:22:39 +01002031}
2032
2033/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 * Perform scheduler related setup for a newly forked process p.
2035 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002036 *
2037 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 */
Rik van Riel5e1576e2013-10-07 11:29:26 +01002039static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002041 p->on_rq = 0;
2042
2043 p->se.on_rq = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002044 p->se.exec_start = 0;
2045 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002046 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002047 p->se.nr_migrations = 0;
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002048 p->se.vruntime = 0;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002049 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002050
2051#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03002052 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002053#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002054
Dario Faggioliaab03e02013-11-28 11:14:43 +01002055 RB_CLEAR_NODE(&p->dl.rb_node);
Peter Zijlstra40767b02015-01-28 15:08:03 +01002056 init_dl_task_timer(&p->dl);
Juri Lellia5e7be32014-09-19 10:22:39 +01002057 __dl_clear_params(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002058
Peter Zijlstrafa717062008-01-25 21:08:27 +01002059 INIT_LIST_HEAD(&p->rt.run_list);
Nick Piggin476d1392005-06-25 14:57:29 -07002060
Avi Kivitye107be32007-07-26 13:40:43 +02002061#ifdef CONFIG_PREEMPT_NOTIFIERS
2062 INIT_HLIST_HEAD(&p->preempt_notifiers);
2063#endif
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002064
2065#ifdef CONFIG_NUMA_BALANCING
2066 if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
Mel Gorman7e8d16b2013-10-07 11:28:54 +01002067 p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002068 p->mm->numa_scan_seq = 0;
2069 }
2070
Rik van Riel5e1576e2013-10-07 11:29:26 +01002071 if (clone_flags & CLONE_VM)
2072 p->numa_preferred_nid = current->numa_preferred_nid;
2073 else
2074 p->numa_preferred_nid = -1;
2075
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002076 p->node_stamp = 0ULL;
2077 p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
Peter Zijlstra4b96a292012-10-25 14:16:47 +02002078 p->numa_scan_period = sysctl_numa_balancing_scan_delay;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002079 p->numa_work.next = &p->numa_work;
Iulia Manda44dba3d2014-10-31 02:13:31 +02002080 p->numa_faults = NULL;
Rik van Riel7e2703e2014-01-27 17:03:45 -05002081 p->last_task_numa_placement = 0;
2082 p->last_sum_exec_runtime = 0;
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002083
Peter Zijlstra8c8a7432013-10-07 11:29:21 +01002084 p->numa_group = NULL;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002085#endif /* CONFIG_NUMA_BALANCING */
Ingo Molnardd41f592007-07-09 18:51:59 +02002086}
2087
Mel Gorman1a687c22012-11-22 11:16:36 +00002088#ifdef CONFIG_NUMA_BALANCING
Mel Gorman3105b862012-11-23 11:23:49 +00002089#ifdef CONFIG_SCHED_DEBUG
Mel Gorman1a687c22012-11-22 11:16:36 +00002090void set_numabalancing_state(bool enabled)
2091{
2092 if (enabled)
2093 sched_feat_set("NUMA");
2094 else
2095 sched_feat_set("NO_NUMA");
2096}
Mel Gorman3105b862012-11-23 11:23:49 +00002097#else
2098__read_mostly bool numabalancing_enabled;
2099
2100void set_numabalancing_state(bool enabled)
2101{
2102 numabalancing_enabled = enabled;
2103}
2104#endif /* CONFIG_SCHED_DEBUG */
Andi Kleen54a43d52014-01-23 15:53:13 -08002105
2106#ifdef CONFIG_PROC_SYSCTL
2107int sysctl_numa_balancing(struct ctl_table *table, int write,
2108 void __user *buffer, size_t *lenp, loff_t *ppos)
2109{
2110 struct ctl_table t;
2111 int err;
2112 int state = numabalancing_enabled;
2113
2114 if (write && !capable(CAP_SYS_ADMIN))
2115 return -EPERM;
2116
2117 t = *table;
2118 t.data = &state;
2119 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2120 if (err < 0)
2121 return err;
2122 if (write)
2123 set_numabalancing_state(state);
2124 return err;
2125}
2126#endif
2127#endif
Mel Gorman1a687c22012-11-22 11:16:36 +00002128
Ingo Molnardd41f592007-07-09 18:51:59 +02002129/*
2130 * fork()/clone()-time setup:
2131 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002132int sched_fork(unsigned long clone_flags, struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02002133{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002134 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002135 int cpu = get_cpu();
2136
Rik van Riel5e1576e2013-10-07 11:29:26 +01002137 __sched_fork(clone_flags, p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002138 /*
Peter Zijlstra0017d732010-03-24 18:34:10 +01002139 * We mark the process as running here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002140 * nobody will actually run it, and a signal or other external
2141 * event cannot wake it up and insert it on the runqueue either.
2142 */
Peter Zijlstra0017d732010-03-24 18:34:10 +01002143 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002144
Ingo Molnarb29739f2006-06-27 02:54:51 -07002145 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02002146 * Make sure we do not leak PI boosting priority to the child.
2147 */
2148 p->prio = current->normal_prio;
2149
2150 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002151 * Revert to default priority/policy on fork if requested.
2152 */
2153 if (unlikely(p->sched_reset_on_fork)) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01002154 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002155 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002156 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02002157 p->rt_priority = 0;
2158 } else if (PRIO_TO_NICE(p->static_prio) < 0)
2159 p->static_prio = NICE_TO_PRIO(0);
2160
2161 p->prio = p->normal_prio = __normal_prio(p);
2162 set_load_weight(p);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02002163
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02002164 /*
2165 * We don't need the reset flag anymore after the fork. It has
2166 * fulfilled its duty:
2167 */
2168 p->sched_reset_on_fork = 0;
2169 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02002170
Dario Faggioliaab03e02013-11-28 11:14:43 +01002171 if (dl_prio(p->prio)) {
2172 put_cpu();
2173 return -EAGAIN;
2174 } else if (rt_prio(p->prio)) {
2175 p->sched_class = &rt_sched_class;
2176 } else {
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002177 p->sched_class = &fair_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01002178 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07002179
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002180 if (p->sched_class->task_fork)
2181 p->sched_class->task_fork(p);
2182
Peter Zijlstra86951592010-06-22 11:44:53 +02002183 /*
2184 * The child is not yet in the pid-hash so no cgroup attach races,
2185 * and the cgroup is pinned to this child due to cgroup_fork()
2186 * is ran before sched_fork().
2187 *
2188 * Silence PROVE_RCU.
2189 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002190 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002191 set_task_cpu(p, cpu);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002192 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02002193
Naveen N. Raof6db8342015-06-25 23:53:37 +05302194#ifdef CONFIG_SCHED_INFO
Ingo Molnardd41f592007-07-09 18:51:59 +02002195 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002196 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02002198#if defined(CONFIG_SMP)
2199 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07002200#endif
Peter Zijlstra01028742013-08-14 14:55:46 +02002201 init_task_preempt_count(p);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002202#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05002203 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002204 RB_CLEAR_NODE(&p->pushable_dl_tasks);
Dario Faggioli806c09a2010-11-30 19:51:33 +01002205#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002206
Nick Piggin476d1392005-06-25 14:57:29 -07002207 put_cpu();
Dario Faggioliaab03e02013-11-28 11:14:43 +01002208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Dario Faggioli332ac172013-11-07 14:43:45 +01002211unsigned long to_ratio(u64 period, u64 runtime)
2212{
2213 if (runtime == RUNTIME_INF)
2214 return 1ULL << 20;
2215
2216 /*
2217 * Doing this here saves a lot of checks in all
2218 * the calling paths, and returning zero seems
2219 * safe for them anyway.
2220 */
2221 if (period == 0)
2222 return 0;
2223
2224 return div64_u64(runtime << 20, period);
2225}
2226
2227#ifdef CONFIG_SMP
2228inline struct dl_bw *dl_bw_of(int i)
2229{
Kirill Tkhai66339c32014-09-22 22:36:24 +04002230 rcu_lockdep_assert(rcu_read_lock_sched_held(),
2231 "sched RCU must be held");
Dario Faggioli332ac172013-11-07 14:43:45 +01002232 return &cpu_rq(i)->rd->dl_bw;
2233}
2234
Peter Zijlstrade212f12013-12-19 11:54:45 +01002235static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002236{
Peter Zijlstrade212f12013-12-19 11:54:45 +01002237 struct root_domain *rd = cpu_rq(i)->rd;
2238 int cpus = 0;
2239
Kirill Tkhai66339c32014-09-22 22:36:24 +04002240 rcu_lockdep_assert(rcu_read_lock_sched_held(),
2241 "sched RCU must be held");
Peter Zijlstrade212f12013-12-19 11:54:45 +01002242 for_each_cpu_and(i, rd->span, cpu_active_mask)
2243 cpus++;
2244
2245 return cpus;
Dario Faggioli332ac172013-11-07 14:43:45 +01002246}
2247#else
2248inline struct dl_bw *dl_bw_of(int i)
2249{
2250 return &cpu_rq(i)->dl.dl_bw;
2251}
2252
Peter Zijlstrade212f12013-12-19 11:54:45 +01002253static inline int dl_bw_cpus(int i)
Dario Faggioli332ac172013-11-07 14:43:45 +01002254{
2255 return 1;
2256}
2257#endif
2258
Dario Faggioli332ac172013-11-07 14:43:45 +01002259/*
2260 * We must be sure that accepting a new task (or allowing changing the
2261 * parameters of an existing one) is consistent with the bandwidth
2262 * constraints. If yes, this function also accordingly updates the currently
2263 * allocated bandwidth to reflect the new situation.
2264 *
2265 * This function is called while holding p's rq->lock.
Peter Zijlstra40767b02015-01-28 15:08:03 +01002266 *
2267 * XXX we should delay bw change until the task's 0-lag point, see
2268 * __setparam_dl().
Dario Faggioli332ac172013-11-07 14:43:45 +01002269 */
2270static int dl_overflow(struct task_struct *p, int policy,
2271 const struct sched_attr *attr)
2272{
2273
2274 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
Steven Rostedt4df16382014-02-19 13:53:35 -05002275 u64 period = attr->sched_period ?: attr->sched_deadline;
Dario Faggioli332ac172013-11-07 14:43:45 +01002276 u64 runtime = attr->sched_runtime;
2277 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
Peter Zijlstrade212f12013-12-19 11:54:45 +01002278 int cpus, err = -1;
Dario Faggioli332ac172013-11-07 14:43:45 +01002279
2280 if (new_bw == p->dl.dl_bw)
2281 return 0;
2282
2283 /*
2284 * Either if a task, enters, leave, or stays -deadline but changes
2285 * its parameters, we may need to update accordingly the total
2286 * allocated bandwidth of the container.
2287 */
2288 raw_spin_lock(&dl_b->lock);
Peter Zijlstrade212f12013-12-19 11:54:45 +01002289 cpus = dl_bw_cpus(task_cpu(p));
Dario Faggioli332ac172013-11-07 14:43:45 +01002290 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2291 !__dl_overflow(dl_b, cpus, 0, new_bw)) {
2292 __dl_add(dl_b, new_bw);
2293 err = 0;
2294 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2295 !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
2296 __dl_clear(dl_b, p->dl.dl_bw);
2297 __dl_add(dl_b, new_bw);
2298 err = 0;
2299 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2300 __dl_clear(dl_b, p->dl.dl_bw);
2301 err = 0;
2302 }
2303 raw_spin_unlock(&dl_b->lock);
2304
2305 return err;
2306}
2307
2308extern void init_dl_bw(struct dl_bw *dl_b);
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310/*
2311 * wake_up_new_task - wake up a newly created task for the first time.
2312 *
2313 * This function will do some initial scheduler statistics housekeeping
2314 * that must be done for every newly created context, then puts the task
2315 * on the runqueue and wakes it.
2316 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02002317void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
2319 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002320 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002321
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002322 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002323#ifdef CONFIG_SMP
2324 /*
2325 * Fork balancing, do it here and not earlier because:
2326 * - cpus_allowed can change in the fork path
2327 * - any previously selected cpu might disappear through hotplug
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002328 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01002329 set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01002330#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Alex Shia75cdaa2013-06-20 10:18:47 +08002332 /* Initialize new task's runnable average */
Yuyang Du540247f2015-07-15 08:04:39 +08002333 init_entity_runnable_average(&p->se);
Peter Zijlstraab2515c2011-04-05 17:23:52 +02002334 rq = __task_rq_lock(p);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002335 activate_task(rq, p, 0);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002336 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002337 trace_sched_wakeup_new(p);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02002338 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002339#ifdef CONFIG_SMP
Peter Zijlstraefbbd052009-12-16 18:04:40 +01002340 if (p->sched_class->task_woken)
2341 p->sched_class->task_woken(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002342#endif
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002343 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
Avi Kivitye107be32007-07-26 13:40:43 +02002346#ifdef CONFIG_PREEMPT_NOTIFIERS
2347
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002348static struct static_key preempt_notifier_key = STATIC_KEY_INIT_FALSE;
2349
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02002350void preempt_notifier_inc(void)
2351{
2352 static_key_slow_inc(&preempt_notifier_key);
2353}
2354EXPORT_SYMBOL_GPL(preempt_notifier_inc);
2355
2356void preempt_notifier_dec(void)
2357{
2358 static_key_slow_dec(&preempt_notifier_key);
2359}
2360EXPORT_SYMBOL_GPL(preempt_notifier_dec);
2361
Avi Kivitye107be32007-07-26 13:40:43 +02002362/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00002363 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07002364 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002365 */
2366void preempt_notifier_register(struct preempt_notifier *notifier)
2367{
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02002368 if (!static_key_false(&preempt_notifier_key))
2369 WARN(1, "registering preempt_notifier while notifiers disabled\n");
2370
Avi Kivitye107be32007-07-26 13:40:43 +02002371 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2372}
2373EXPORT_SYMBOL_GPL(preempt_notifier_register);
2374
2375/**
2376 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002377 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002378 *
Mathieu Desnoyersd84525a2015-05-17 12:53:10 -04002379 * This is *not* safe to call from within a preemption notifier.
Avi Kivitye107be32007-07-26 13:40:43 +02002380 */
2381void preempt_notifier_unregister(struct preempt_notifier *notifier)
2382{
2383 hlist_del(&notifier->link);
2384}
2385EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2386
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002387static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02002388{
2389 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002390
Sasha Levinb67bfe02013-02-27 17:06:00 -08002391 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002392 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2393}
2394
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002395static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2396{
2397 if (static_key_false(&preempt_notifier_key))
2398 __fire_sched_in_preempt_notifiers(curr);
2399}
2400
Avi Kivitye107be32007-07-26 13:40:43 +02002401static void
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002402__fire_sched_out_preempt_notifiers(struct task_struct *curr,
2403 struct task_struct *next)
Avi Kivitye107be32007-07-26 13:40:43 +02002404{
2405 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02002406
Sasha Levinb67bfe02013-02-27 17:06:00 -08002407 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02002408 notifier->ops->sched_out(notifier, next);
2409}
2410
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002411static __always_inline void
2412fire_sched_out_preempt_notifiers(struct task_struct *curr,
2413 struct task_struct *next)
2414{
2415 if (static_key_false(&preempt_notifier_key))
2416 __fire_sched_out_preempt_notifiers(curr, next);
2417}
2418
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002419#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002420
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002421static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02002422{
2423}
2424
Peter Zijlstra1cde2932015-06-08 16:00:30 +02002425static inline void
Avi Kivitye107be32007-07-26 13:40:43 +02002426fire_sched_out_preempt_notifiers(struct task_struct *curr,
2427 struct task_struct *next)
2428{
2429}
2430
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002431#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002434 * prepare_task_switch - prepare to switch tasks
2435 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002436 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002437 * @next: the task we are going to switch to.
2438 *
2439 * This is called with the rq lock held and interrupts off. It must
2440 * be paired with a subsequent finish_task_switch after the context
2441 * switch.
2442 *
2443 * prepare_task_switch sets up locking and calls architecture specific
2444 * hooks.
2445 */
Avi Kivitye107be32007-07-26 13:40:43 +02002446static inline void
2447prepare_task_switch(struct rq *rq, struct task_struct *prev,
2448 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002449{
Andrew Vagin895dd922012-07-12 14:14:29 +04002450 trace_sched_switch(prev, next);
Michael S. Tsirkin43148952013-09-22 17:20:54 +03002451 sched_info_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002452 perf_event_task_sched_out(prev, next);
Avi Kivitye107be32007-07-26 13:40:43 +02002453 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002454 prepare_lock_switch(rq, next);
2455 prepare_arch_switch(next);
2456}
2457
2458/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 * finish_task_switch - clean up after a task-switch
2460 * @prev: the thread we just switched away from.
2461 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002462 * finish_task_switch must be called after the context switch, paired
2463 * with a prepare_task_switch call before the context switch.
2464 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2465 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 *
2467 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002468 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 * with the lock held can cause deadlocks; see schedule() for
2470 * details.)
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002471 *
2472 * The context switch have flipped the stack from under us and restored the
2473 * local variables which were saved when this task called schedule() in the
2474 * past. prev == current is still correct but we need to recalculate this_rq
2475 * because prev may have moved to another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002477static struct rq *finish_task_switch(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 __releases(rq->lock)
2479{
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002480 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002482 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 rq->prev_mm = NULL;
2485
2486 /*
2487 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002488 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002489 * schedule one last time. The schedule call will never return, and
2490 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002491 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 * still held, otherwise prev could be scheduled on another cpu, die
2493 * there before we look at prev->state, and then the reference would
2494 * be dropped twice.
2495 * Manfred Spraul <manfred@colorfullife.com>
2496 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002497 prev_state = prev->state;
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +02002498 vtime_task_switch(prev);
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002499 perf_event_task_sched_in(prev, current);
Nick Piggin4866cde2005-06-25 14:57:23 -07002500 finish_lock_switch(rq, prev);
Catalin Marinas01f23e12011-11-27 21:43:10 +00002501 finish_arch_post_lock_switch();
Steven Rostedte8fa1362008-01-25 21:08:05 +01002502
Avi Kivitye107be32007-07-26 13:40:43 +02002503 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (mm)
2505 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002506 if (unlikely(prev_state == TASK_DEAD)) {
Dario Faggiolie6c390f2013-11-07 14:43:35 +01002507 if (prev->sched_class->task_dead)
2508 prev->sched_class->task_dead(prev);
2509
bibo maoc6fd91f2006-03-26 01:38:20 -08002510 /*
2511 * Remove function-return probe instances associated with this
2512 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002513 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002514 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002516 }
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +02002517
2518 tick_nohz_task_switch(current);
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002519 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Gregory Haskins3f029d32009-07-29 11:08:47 -04002522#ifdef CONFIG_SMP
2523
Gregory Haskins3f029d32009-07-29 11:08:47 -04002524/* rq->lock is NOT held, but preemption is disabled */
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002525static void __balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04002526{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002527 struct callback_head *head, *next;
2528 void (*func)(struct rq *rq);
2529 unsigned long flags;
Gregory Haskins3f029d32009-07-29 11:08:47 -04002530
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002531 raw_spin_lock_irqsave(&rq->lock, flags);
2532 head = rq->balance_callback;
2533 rq->balance_callback = NULL;
2534 while (head) {
2535 func = (void (*)(struct rq *))head->func;
2536 next = head->next;
2537 head->next = NULL;
2538 head = next;
Gregory Haskins3f029d32009-07-29 11:08:47 -04002539
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002540 func(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002541 }
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002542 raw_spin_unlock_irqrestore(&rq->lock, flags);
2543}
2544
2545static inline void balance_callback(struct rq *rq)
2546{
2547 if (unlikely(rq->balance_callback))
2548 __balance_callback(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04002549}
2550
2551#else
2552
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002553static inline void balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04002554{
2555}
2556
2557#endif
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559/**
2560 * schedule_tail - first thing a freshly forked thread must call.
2561 * @prev: the thread we just switched away from.
2562 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002563asmlinkage __visible void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 __releases(rq->lock)
2565{
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002566 struct rq *rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002567
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002568 /* finish_task_switch() drops rq->lock and enables preemtion */
2569 preempt_disable();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002570 rq = finish_task_switch(prev);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02002571 balance_callback(rq);
Oleg Nesterov1a43a142014-10-08 21:36:44 +02002572 preempt_enable();
Steven Rostedtda19ab52009-07-29 00:21:22 -04002573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002575 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
2578/*
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002579 * context_switch - switch to the new MM and the new thread's register state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002581static inline struct rq *
Ingo Molnar70b97a72006-07-03 00:25:42 -07002582context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002583 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
Ingo Molnardd41f592007-07-09 18:51:59 +02002585 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Avi Kivitye107be32007-07-26 13:40:43 +02002587 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002588
Ingo Molnardd41f592007-07-09 18:51:59 +02002589 mm = next->mm;
2590 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002591 /*
2592 * For paravirt, this is coupled with an exit in switch_to to
2593 * combine the page table reload and the switch backend into
2594 * one hypercall.
2595 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08002596 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01002597
Heiko Carstens31915ab2010-09-16 14:42:25 +02002598 if (!mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 next->active_mm = oldmm;
2600 atomic_inc(&oldmm->mm_count);
2601 enter_lazy_tlb(oldmm, next);
2602 } else
2603 switch_mm(oldmm, mm, next);
2604
Heiko Carstens31915ab2010-09-16 14:42:25 +02002605 if (!prev->mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 rq->prev_mm = oldmm;
2608 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002609 /*
2610 * Since the runqueue lock will be released by the next
2611 * task (which is an invalid locking op but in the case
2612 * of the scheduler it's an obvious special-case), so we
2613 * do an early lockdep release here:
2614 */
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002615 lockdep_unpin_lock(&rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002616 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 /* Here we just switch the register state and the stack. */
2619 switch_to(prev, next, prev);
Ingo Molnardd41f592007-07-09 18:51:59 +02002620 barrier();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02002621
2622 return finish_task_switch(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
2625/*
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002626 * nr_running and nr_context_switches:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 *
2628 * externally visible scheduler statistics: current number of runnable
Sha Zhengju1c3e8262013-02-20 17:14:38 +08002629 * threads, total number of context switches performed since bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 */
2631unsigned long nr_running(void)
2632{
2633 unsigned long i, sum = 0;
2634
2635 for_each_online_cpu(i)
2636 sum += cpu_rq(i)->nr_running;
2637
2638 return sum;
2639}
2640
Tim Chen2ee507c2014-07-31 10:29:48 -07002641/*
2642 * Check if only the current task is running on the cpu.
2643 */
2644bool single_task_running(void)
2645{
2646 if (cpu_rq(smp_processor_id())->nr_running == 1)
2647 return true;
2648 else
2649 return false;
2650}
2651EXPORT_SYMBOL(single_task_running);
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653unsigned long long nr_context_switches(void)
2654{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002655 int i;
2656 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002658 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 sum += cpu_rq(i)->nr_switches;
2660
2661 return sum;
2662}
2663
2664unsigned long nr_iowait(void)
2665{
2666 unsigned long i, sum = 0;
2667
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002668 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2670
2671 return sum;
2672}
2673
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002674unsigned long nr_iowait_cpu(int cpu)
Arjan van de Ven69d25872009-09-21 17:04:08 -07002675{
Peter Zijlstra8c215bd2010-07-01 09:07:17 +02002676 struct rq *this = cpu_rq(cpu);
Arjan van de Ven69d25872009-09-21 17:04:08 -07002677 return atomic_read(&this->nr_iowait);
2678}
2679
Mel Gorman372ba8c2014-08-06 14:19:21 +01002680void get_iowait_load(unsigned long *nr_waiters, unsigned long *load)
2681{
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02002682 struct rq *rq = this_rq();
2683 *nr_waiters = atomic_read(&rq->nr_iowait);
2684 *load = rq->load.weight;
Mel Gorman372ba8c2014-08-06 14:19:21 +01002685}
2686
Ingo Molnardd41f592007-07-09 18:51:59 +02002687#ifdef CONFIG_SMP
2688
Ingo Molnar48f24c42006-07-03 00:25:40 -07002689/*
Peter Zijlstra38022902009-12-16 18:04:37 +01002690 * sched_exec - execve() is a valuable balancing opportunity, because at
2691 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
Peter Zijlstra38022902009-12-16 18:04:37 +01002693void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Peter Zijlstra38022902009-12-16 18:04:37 +01002695 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01002697 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01002698
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002699 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstraac66f542013-10-07 11:29:16 +01002700 dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01002701 if (dest_cpu == smp_processor_id())
2702 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01002703
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002704 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02002705 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07002706
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002707 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
2708 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return;
2710 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01002711unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02002712 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715#endif
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717DEFINE_PER_CPU(struct kernel_stat, kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002718DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720EXPORT_PER_CPU_SYMBOL(kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02002721EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002724 * Return accounted runtime for the task.
2725 * In case the task is currently running, return the runtime plus current's
2726 * pending runtime that have not been accounted yet.
2727 */
2728unsigned long long task_sched_runtime(struct task_struct *p)
2729{
2730 unsigned long flags;
2731 struct rq *rq;
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002732 u64 ns;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002733
Peter Zijlstra911b2892013-11-11 18:21:56 +01002734#if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
2735 /*
2736 * 64-bit doesn't need locks to atomically read a 64bit value.
2737 * So we have a optimization chance when the task's delta_exec is 0.
2738 * Reading ->on_cpu is racy, but this is ok.
2739 *
2740 * If we race with it leaving cpu, we'll take a lock. So we're correct.
2741 * If we race with it entering cpu, unaccounted time is 0. This is
2742 * indistinguishable from the read occurring a few cycles earlier.
Mike Galbraith4036ac12014-06-24 07:49:40 +02002743 * If we see ->on_cpu without ->on_rq, the task is leaving, and has
2744 * been accounted, so we're correct here as well.
Peter Zijlstra911b2892013-11-11 18:21:56 +01002745 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002746 if (!p->on_cpu || !task_on_rq_queued(p))
Peter Zijlstra911b2892013-11-11 18:21:56 +01002747 return p->se.sum_exec_runtime;
2748#endif
2749
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002750 rq = task_rq_lock(p, &flags);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002751 /*
2752 * Must be ->curr _and_ ->on_rq. If dequeued, we would
2753 * project cycles that may never be accounted to this
2754 * thread, breaking clock_gettime().
2755 */
2756 if (task_current(rq, p) && task_on_rq_queued(p)) {
2757 update_rq_clock(rq);
2758 p->sched_class->update_curr(rq);
2759 }
2760 ns = p->se.sum_exec_runtime;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002761 task_rq_unlock(rq, p, &flags);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09002762
2763 return ns;
2764}
2765
Balbir Singh49048622008-09-05 18:12:23 +02002766/*
Christoph Lameter7835b982006-12-10 02:20:22 -08002767 * This function gets called by the timer code, with HZ frequency.
2768 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08002769 */
2770void scheduler_tick(void)
2771{
Christoph Lameter7835b982006-12-10 02:20:22 -08002772 int cpu = smp_processor_id();
2773 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002774 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002775
2776 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08002777
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002778 raw_spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02002779 update_rq_clock(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01002780 curr->sched_class->task_tick(rq, curr, 0);
Alex Shi83dfd522013-06-20 10:18:49 +08002781 update_cpu_load_active(rq);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02002782 calc_global_load_tick(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01002783 raw_spin_unlock(&rq->lock);
Ingo Molnardd41f592007-07-09 18:51:59 +02002784
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002785 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02002786
Christoph Lametere418e1c2006-12-10 02:20:23 -08002787#ifdef CONFIG_SMP
Suresh Siddha6eb57e02011-10-03 15:09:01 -07002788 rq->idle_balance = idle_cpu(cpu);
Daniel Lezcano7caff662014-01-06 12:34:38 +01002789 trigger_load_balance(rq);
Christoph Lametere418e1c2006-12-10 02:20:23 -08002790#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002791 rq_last_tick_reset(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002794#ifdef CONFIG_NO_HZ_FULL
2795/**
2796 * scheduler_tick_max_deferment
2797 *
2798 * Keep at least one tick per second when a single
2799 * active task is running because the scheduler doesn't
2800 * yet completely support full dynticks environment.
2801 *
2802 * This makes sure that uptime, CFS vruntime, load
2803 * balancing, etc... continue to move forward, even
2804 * with a very low granularity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02002805 *
2806 * Return: Maximum deferment in nanoseconds.
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002807 */
2808u64 scheduler_tick_max_deferment(void)
2809{
2810 struct rq *rq = this_rq();
Jason Low316c1608d2015-04-28 13:00:20 -07002811 unsigned long next, now = READ_ONCE(jiffies);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002812
2813 next = rq->last_sched_tick + HZ;
2814
2815 if (time_before_eq(next, now))
2816 return 0;
2817
Kevin Hilman8fe8ff02014-01-15 14:51:38 +01002818 return jiffies_to_nsecs(next - now);
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02002819}
2820#endif
2821
Lai Jiangshan132380a2009-04-02 14:18:25 +08002822notrace unsigned long get_parent_ip(unsigned long addr)
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002823{
2824 if (in_lock_functions(addr)) {
2825 addr = CALLER_ADDR2;
2826 if (in_lock_functions(addr))
2827 addr = CALLER_ADDR3;
2828 }
2829 return addr;
2830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05002832#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
2833 defined(CONFIG_PREEMPT_TRACER))
2834
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002835void preempt_count_add(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002837#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 /*
2839 * Underflow?
2840 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002841 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
2842 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002843#endif
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002844 __preempt_count_add(val);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002845#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 /*
2847 * Spinlock count overflowing soon?
2848 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002849 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
2850 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002851#endif
Thomas Gleixner8f47b182014-02-07 20:58:39 +01002852 if (preempt_count() == val) {
2853 unsigned long ip = get_parent_ip(CALLER_ADDR1);
2854#ifdef CONFIG_DEBUG_PREEMPT
2855 current->preempt_disable_ip = ip;
2856#endif
2857 trace_preempt_off(CALLER_ADDR0, ip);
2858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002860EXPORT_SYMBOL(preempt_count_add);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002861NOKPROBE_SYMBOL(preempt_count_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002863void preempt_count_sub(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002865#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 /*
2867 * Underflow?
2868 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01002869 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002870 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 /*
2872 * Is the spinlock portion underflowing?
2873 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002874 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
2875 !(preempt_count() & PREEMPT_MASK)))
2876 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002877#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07002878
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02002879 if (preempt_count() == val)
2880 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002881 __preempt_count_sub(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02002883EXPORT_SYMBOL(preempt_count_sub);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09002884NOKPROBE_SYMBOL(preempt_count_sub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
2886#endif
2887
2888/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002889 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002891static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
Dave Jones664dfa62011-12-22 16:39:30 -05002893 if (oops_in_progress)
2894 return;
2895
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01002896 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
2897 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02002898
Ingo Molnardd41f592007-07-09 18:51:59 +02002899 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07002900 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02002901 if (irqs_disabled())
2902 print_irqtrace_events(prev);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01002903#ifdef CONFIG_DEBUG_PREEMPT
2904 if (in_atomic_preempt_off()) {
2905 pr_err("Preemption disabled at:");
2906 print_ip_sym(current->preempt_disable_ip);
2907 pr_cont("\n");
2908 }
2909#endif
Stephen Boyd6135fc12012-03-28 17:10:47 -07002910 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +10302911 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Ingo Molnardd41f592007-07-09 18:51:59 +02002912}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Ingo Molnardd41f592007-07-09 18:51:59 +02002914/*
2915 * Various schedule()-time debugging checks and statistics:
2916 */
2917static inline void schedule_debug(struct task_struct *prev)
2918{
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01002919#ifdef CONFIG_SCHED_STACK_END_CHECK
2920 BUG_ON(unlikely(task_stack_end_corrupted(prev)));
2921#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002923 * Test if we are atomic. Since do_exit() needs to call into
Oleg Nesterov192301e2013-11-13 16:45:38 +01002924 * schedule() atomically, we ignore that path. Otherwise whine
2925 * if we are scheduling when we should not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 */
Oleg Nesterov192301e2013-11-13 16:45:38 +01002927 if (unlikely(in_atomic_preempt_off() && prev->state != TASK_DEAD))
Ingo Molnardd41f592007-07-09 18:51:59 +02002928 __schedule_bug(prev);
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002929 rcu_sleep_check();
Ingo Molnardd41f592007-07-09 18:51:59 +02002930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
2932
Ingo Molnar2d723762007-10-15 17:00:12 +02002933 schedstat_inc(this_rq(), sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02002934}
2935
2936/*
2937 * Pick up the highest-prio task:
2938 */
2939static inline struct task_struct *
Peter Zijlstra606dba22012-02-11 06:05:00 +01002940pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02002941{
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002942 const struct sched_class *class = &fair_sched_class;
Ingo Molnardd41f592007-07-09 18:51:59 +02002943 struct task_struct *p;
2944
2945 /*
2946 * Optimization: we know that if all tasks are in
2947 * the fair class we can call that function directly:
2948 */
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002949 if (likely(prev->sched_class == class &&
Peter Zijlstra38033c32014-01-23 20:32:21 +01002950 rq->nr_running == rq->cfs.h_nr_running)) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01002951 p = fair_sched_class.pick_next_task(rq, prev);
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02002952 if (unlikely(p == RETRY_TASK))
2953 goto again;
2954
2955 /* assumes fair_sched_class->next == idle_sched_class */
2956 if (unlikely(!p))
2957 p = idle_sched_class.pick_next_task(rq, prev);
2958
2959 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02002960 }
2961
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002962again:
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002963 for_each_class(class) {
Peter Zijlstra606dba22012-02-11 06:05:00 +01002964 p = class->pick_next_task(rq, prev);
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002965 if (p) {
2966 if (unlikely(p == RETRY_TASK))
2967 goto again;
Ingo Molnardd41f592007-07-09 18:51:59 +02002968 return p;
Peter Zijlstra37e117c2014-02-14 12:25:08 +01002969 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002970 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02002971
2972 BUG(); /* the idle class will always have a runnable task */
Ingo Molnardd41f592007-07-09 18:51:59 +02002973}
2974
2975/*
Thomas Gleixnerc259e012011-06-22 19:47:00 +02002976 * __schedule() is the main scheduler function.
Pekka Enbergedde96e2012-08-04 11:49:47 +03002977 *
2978 * The main means of driving the scheduler and thus entering this function are:
2979 *
2980 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
2981 *
2982 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
2983 * paths. For example, see arch/x86/entry_64.S.
2984 *
2985 * To drive preemption between tasks, the scheduler sets the flag in timer
2986 * interrupt handler scheduler_tick().
2987 *
2988 * 3. Wakeups don't really cause entry into schedule(). They add a
2989 * task to the run-queue and that's it.
2990 *
2991 * Now, if the new task added to the run-queue preempts the current
2992 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
2993 * called on the nearest possible occasion:
2994 *
2995 * - If the kernel is preemptible (CONFIG_PREEMPT=y):
2996 *
2997 * - in syscall or exception context, at the next outmost
2998 * preempt_enable(). (this might be as soon as the wake_up()'s
2999 * spin_unlock()!)
3000 *
3001 * - in IRQ context, return from interrupt-handler to
3002 * preemptible context
3003 *
3004 * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
3005 * then at the next:
3006 *
3007 * - cond_resched() call
3008 * - explicit schedule() call
3009 * - return from syscall or exception to user-space
3010 * - return from interrupt-handler to user-space
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003011 *
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003012 * WARNING: must be called with preemption disabled!
Ingo Molnardd41f592007-07-09 18:51:59 +02003013 */
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003014static void __sched __schedule(void)
Ingo Molnardd41f592007-07-09 18:51:59 +02003015{
3016 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003017 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02003018 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02003019 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02003020
Ingo Molnardd41f592007-07-09 18:51:59 +02003021 cpu = smp_processor_id();
3022 rq = cpu_rq(cpu);
Paul E. McKenney38200cf2014-10-21 12:50:04 -07003023 rcu_note_context_switch();
Ingo Molnardd41f592007-07-09 18:51:59 +02003024 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02003025
Ingo Molnardd41f592007-07-09 18:51:59 +02003026 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Peter Zijlstra31656512008-07-18 18:01:23 +02003028 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02003029 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003030
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02003031 /*
3032 * Make sure that signal_pending_state()->signal_pending() below
3033 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
3034 * done by the caller to avoid the race with signal_wake_up().
3035 */
3036 smp_mb__before_spinlock();
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003037 raw_spin_lock_irq(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003038 lockdep_pin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003040 rq->clock_skip_update <<= 1; /* promote REQ to ACT */
3041
Oleg Nesterov246d86b2010-05-19 14:57:11 +02003042 switch_count = &prev->nivcsw;
Ingo Molnardd41f592007-07-09 18:51:59 +02003043 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Tejun Heo21aa9af2010-06-08 21:40:37 +02003044 if (unlikely(signal_pending_state(prev->state, prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003045 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02003046 } else {
Peter Zijlstra2acca552011-04-05 17:23:50 +02003047 deactivate_task(rq, prev, DEQUEUE_SLEEP);
3048 prev->on_rq = 0;
3049
Tejun Heo21aa9af2010-06-08 21:40:37 +02003050 /*
Peter Zijlstra2acca552011-04-05 17:23:50 +02003051 * If a worker went to sleep, notify and ask workqueue
3052 * whether it wants to wake up a task to maintain
3053 * concurrency.
Tejun Heo21aa9af2010-06-08 21:40:37 +02003054 */
3055 if (prev->flags & PF_WQ_WORKER) {
3056 struct task_struct *to_wakeup;
3057
3058 to_wakeup = wq_worker_sleeping(prev, cpu);
3059 if (to_wakeup)
3060 try_to_wake_up_local(to_wakeup);
3061 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02003062 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003063 switch_count = &prev->nvcsw;
3064 }
3065
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003066 if (task_on_rq_queued(prev))
Peter Zijlstra606dba22012-02-11 06:05:00 +01003067 update_rq_clock(rq);
3068
3069 next = pick_next_task(rq, prev);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01003070 clear_tsk_need_resched(prev);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02003071 clear_preempt_need_resched();
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01003072 rq->clock_skip_update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 rq->nr_switches++;
3076 rq->curr = next;
3077 ++*switch_count;
3078
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003079 rq = context_switch(rq, prev, next); /* unlocks the rq */
3080 cpu = cpu_of(rq);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003081 } else {
3082 lockdep_unpin_lock(&rq->lock);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01003083 raw_spin_unlock_irq(&rq->lock);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02003084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003086 balance_callback(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003088
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003089static inline void sched_submit_work(struct task_struct *tsk)
3090{
Thomas Gleixner3c7d5182011-07-17 20:46:52 +02003091 if (!tsk->state || tsk_is_pi_blocked(tsk))
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003092 return;
3093 /*
3094 * If we are going to sleep and we have plugged IO queued,
3095 * make sure to submit it to avoid deadlocks.
3096 */
3097 if (blk_needs_flush_plug(tsk))
3098 blk_schedule_flush_plug(tsk);
3099}
3100
Andi Kleen722a9f92014-05-02 00:44:38 +02003101asmlinkage __visible void __sched schedule(void)
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003102{
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02003103 struct task_struct *tsk = current;
3104
3105 sched_submit_work(tsk);
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003106 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003107 preempt_disable();
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003108 __schedule();
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003109 sched_preempt_enable_no_resched();
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01003110 } while (need_resched());
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112EXPORT_SYMBOL(schedule);
3113
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01003114#ifdef CONFIG_CONTEXT_TRACKING
Andi Kleen722a9f92014-05-02 00:44:38 +02003115asmlinkage __visible void __sched schedule_user(void)
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003116{
3117 /*
3118 * If we come here after a random call to set_need_resched(),
3119 * or we have been woken up remotely but the IPI has not yet arrived,
3120 * we haven't yet exited the RCU idle mode. Do it here manually until
3121 * we find a better solution.
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003122 *
3123 * NB: There are buggy callers of this function. Ideally we
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +01003124 * should warn if prev_state != CONTEXT_USER, but that will trigger
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003125 * too frequently to make sense yet.
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003126 */
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003127 enum ctx_state prev_state = exception_enter();
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003128 schedule();
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08003129 exception_exit(prev_state);
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02003130}
3131#endif
3132
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01003133/**
3134 * schedule_preempt_disabled - called with preemption disabled
3135 *
3136 * Returns with preemption disabled. Note: preempt_count must be 1
3137 */
3138void __sched schedule_preempt_disabled(void)
3139{
Thomas Gleixnerba74c142011-03-21 13:32:17 +01003140 sched_preempt_enable_no_resched();
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01003141 schedule();
3142 preempt_disable();
3143}
3144
Frederic Weisbecker06b1f802015-02-16 19:20:07 +01003145static void __sched notrace preempt_schedule_common(void)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003146{
3147 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003148 preempt_active_enter();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003149 __schedule();
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003150 preempt_active_exit();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003151
3152 /*
3153 * Check again in case we missed a preemption opportunity
3154 * between schedule and now.
3155 */
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003156 } while (need_resched());
3157}
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159#ifdef CONFIG_PREEMPT
3160/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003161 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003162 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 * occur there and call schedule directly.
3164 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003165asmlinkage __visible void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 /*
3168 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003169 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 */
Frederic Weisbeckerfbb00b52013-06-19 23:56:22 +02003171 if (likely(!preemptible()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 return;
3173
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01003174 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09003176NOKPROBE_SYMBOL(preempt_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177EXPORT_SYMBOL(preempt_schedule);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003178
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003179/**
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003180 * preempt_schedule_notrace - preempt_schedule called by tracing
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003181 *
3182 * The tracing infrastructure uses preempt_enable_notrace to prevent
3183 * recursion and tracing preempt enabling caused by the tracing
3184 * infrastructure itself. But as tracing can happen in areas coming
3185 * from userspace or just about to enter userspace, a preempt enable
3186 * can occur before user_exit() is called. This will cause the scheduler
3187 * to be called when the system is still in usermode.
3188 *
3189 * To prevent this, the preempt_enable_notrace will use this function
3190 * instead of preempt_schedule() to exit user context if needed before
3191 * calling the scheduler.
3192 */
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003193asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003194{
3195 enum ctx_state prev_ctx;
3196
3197 if (likely(!preemptible()))
3198 return;
3199
3200 do {
Frederic Weisbeckerbe690032015-06-04 17:39:07 +02003201 /*
3202 * Use raw __prempt_count() ops that don't call function.
3203 * We can't call functions before disabling preemption which
3204 * disarm preemption tracing recursions.
3205 */
3206 __preempt_count_add(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET);
3207 barrier();
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003208 /*
3209 * Needs preempt disabled in case user_exit() is traced
3210 * and the tracer calls preempt_enable_notrace() causing
3211 * an infinite recursion.
3212 */
3213 prev_ctx = exception_enter();
3214 __schedule();
3215 exception_exit(prev_ctx);
3216
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003217 barrier();
Frederic Weisbeckerbe690032015-06-04 17:39:07 +02003218 __preempt_count_sub(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003219 } while (need_resched());
3220}
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02003221EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02003222
Thomas Gleixner32e475d2013-11-21 12:41:44 +01003223#endif /* CONFIG_PREEMPT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
3225/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003226 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 * off of irq context.
3228 * Note, that this is called and return with irqs disabled. This will
3229 * protect us against recursive calling from irq.
3230 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003231asmlinkage __visible void __sched preempt_schedule_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003233 enum ctx_state prev_state;
Ingo Molnar6478d882008-01-25 21:08:33 +01003234
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003235 /* Catch callers which need to be fixed */
Peter Zijlstraf27dde82013-08-14 14:55:31 +02003236 BUG_ON(preempt_count() || !irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003238 prev_state = exception_enter();
3239
Andi Kleen3a5c3592007-10-15 17:00:14 +02003240 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003241 preempt_active_enter();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003242 local_irq_enable();
Thomas Gleixnerc259e012011-06-22 19:47:00 +02003243 __schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003244 local_irq_disable();
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02003245 preempt_active_exit();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08003246 } while (need_resched());
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01003247
3248 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
Peter Zijlstra63859d42009-09-15 19:14:42 +02003251int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003252 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Peter Zijlstra63859d42009-09-15 19:14:42 +02003254 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256EXPORT_SYMBOL(default_wake_function);
3257
Ingo Molnarb29739f2006-06-27 02:54:51 -07003258#ifdef CONFIG_RT_MUTEXES
3259
3260/*
3261 * rt_mutex_setprio - set the current priority of a task
3262 * @p: task
3263 * @prio: prio value (kernel-internal form)
3264 *
3265 * This function changes the 'effective' priority of a task. It does
3266 * not touch ->normal_prio like __setscheduler().
3267 *
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003268 * Used by the rt_mutex code to implement priority inheritance
3269 * logic. Call site only calls if the priority of the task changed.
Ingo Molnarb29739f2006-06-27 02:54:51 -07003270 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003271void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003272{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003273 int oldprio, queued, running, enqueue_flag = 0;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003274 struct rq *rq;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003275 const struct sched_class *prev_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003276
Dario Faggioliaab03e02013-11-28 11:14:43 +01003277 BUG_ON(prio > MAX_PRIO);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003278
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003279 rq = __task_rq_lock(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003280
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003281 /*
3282 * Idle task boosting is a nono in general. There is one
3283 * exception, when PREEMPT_RT and NOHZ is active:
3284 *
3285 * The idle task calls get_next_timer_interrupt() and holds
3286 * the timer wheel base->lock on the CPU and another CPU wants
3287 * to access the timer (probably to cancel it). We can safely
3288 * ignore the boosting request, as the idle CPU runs this code
3289 * with interrupts disabled and will complete the lock
3290 * protected section without being interrupted. So there is no
3291 * real need to boost.
3292 */
3293 if (unlikely(p == rq->idle)) {
3294 WARN_ON(p != rq->curr);
3295 WARN_ON(p->pi_blocked_on);
3296 goto out_unlock;
3297 }
3298
Steven Rostedta8027072010-09-20 15:13:34 -04003299 trace_sched_pi_setprio(p, prio);
Andrew Mortond5f9f942007-05-08 20:27:06 -07003300 oldprio = p->prio;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003301 prev_class = p->sched_class;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003302 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003303 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003304 if (queued)
Ingo Molnar69be72c2007-08-09 11:16:49 +02003305 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003306 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003307 put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02003308
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003309 /*
3310 * Boosting condition are:
3311 * 1. -rt task is running and holds mutex A
3312 * --> -dl task blocks on mutex A
3313 *
3314 * 2. -dl task is running and holds mutex A
3315 * --> -dl task blocks on mutex A and could preempt the
3316 * running task
3317 */
3318 if (dl_prio(prio)) {
Oleg Nesterov466af292014-06-06 18:52:06 +02003319 struct task_struct *pi_task = rt_mutex_get_top_task(p);
3320 if (!dl_prio(p->normal_prio) ||
3321 (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003322 p->dl.dl_boosted = 1;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003323 enqueue_flag = ENQUEUE_REPLENISH;
3324 } else
3325 p->dl.dl_boosted = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003326 p->sched_class = &dl_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003327 } else if (rt_prio(prio)) {
3328 if (dl_prio(oldprio))
3329 p->dl.dl_boosted = 0;
3330 if (oldprio < prio)
3331 enqueue_flag = ENQUEUE_HEAD;
Ingo Molnardd41f592007-07-09 18:51:59 +02003332 p->sched_class = &rt_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003333 } else {
3334 if (dl_prio(oldprio))
3335 p->dl.dl_boosted = 0;
Brian Silverman746db942015-02-18 16:23:56 -08003336 if (rt_prio(oldprio))
3337 p->rt.timeout = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003338 p->sched_class = &fair_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003339 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003340
Ingo Molnarb29739f2006-06-27 02:54:51 -07003341 p->prio = prio;
3342
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003343 if (running)
3344 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003345 if (queued)
Dario Faggioli2d3d8912013-11-07 14:43:44 +01003346 enqueue_task(rq, p, enqueue_flag);
Steven Rostedtcb469842008-01-25 21:08:22 +01003347
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003348 check_class_changed(rq, p, prev_class, oldprio);
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02003349out_unlock:
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003350 preempt_disable(); /* avoid rq from going away on us */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003351 __task_rq_unlock(rq);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003352
3353 balance_callback(rq);
3354 preempt_enable();
Ingo Molnarb29739f2006-06-27 02:54:51 -07003355}
Ingo Molnarb29739f2006-06-27 02:54:51 -07003356#endif
Dario Faggiolid50dde52013-11-07 14:43:36 +01003357
Ingo Molnar36c8b582006-07-03 00:25:41 -07003358void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003360 int old_prio, delta, queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003362 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Dongsheng Yang75e45d52014-02-11 15:34:50 +08003364 if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 return;
3366 /*
3367 * We have to be careful, if called from sys_setpriority(),
3368 * the task might be in the middle of scheduling on another CPU.
3369 */
3370 rq = task_rq_lock(p, &flags);
3371 /*
3372 * The RT priorities are set via sched_setscheduler(), but we still
3373 * allow the 'normal' nice value to be set - but as expected
3374 * it wont have any effect on scheduling until the task is
Dario Faggioliaab03e02013-11-28 11:14:43 +01003375 * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003377 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 p->static_prio = NICE_TO_PRIO(nice);
3379 goto out_unlock;
3380 }
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003381 queued = task_on_rq_queued(p);
3382 if (queued)
Ingo Molnar69be72c2007-08-09 11:16:49 +02003383 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003386 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003387 old_prio = p->prio;
3388 p->prio = effective_prio(p);
3389 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003391 if (queued) {
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01003392 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07003394 * If the task increased its priority or is running and
3395 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003397 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Kirill Tkhai88751252014-06-29 00:03:57 +04003398 resched_curr(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 }
3400out_unlock:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003401 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403EXPORT_SYMBOL(set_user_nice);
3404
Matt Mackalle43379f2005-05-01 08:59:00 -07003405/*
3406 * can_nice - check if a task can reduce its nice value
3407 * @p: task
3408 * @nice: nice value
3409 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003410int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07003411{
Matt Mackall024f4742005-08-18 11:24:19 -07003412 /* convert nice value [19,-20] to rlimit style value [1,40] */
Dongsheng Yang7aa2c012014-05-08 18:33:49 +09003413 int nice_rlim = nice_to_rlimit(nice);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003414
Jiri Slaby78d7d402010-03-05 13:42:54 -08003415 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07003416 capable(CAP_SYS_NICE));
3417}
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419#ifdef __ARCH_WANT_SYS_NICE
3420
3421/*
3422 * sys_nice - change the priority of the current process.
3423 * @increment: priority increment
3424 *
3425 * sys_setpriority is a more generic, but much slower function that
3426 * does similar things.
3427 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01003428SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003430 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
3432 /*
3433 * Setpriority might change our priority at the same moment.
3434 * We don't have to worry. Conceptually one call occurs first
3435 * and we have a single winner.
3436 */
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003437 increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003438 nice = task_nice(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09003440 nice = clamp_val(nice, MIN_NICE, MAX_NICE);
Matt Mackalle43379f2005-05-01 08:59:00 -07003441 if (increment < 0 && !can_nice(current, nice))
3442 return -EPERM;
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 retval = security_task_setnice(current, nice);
3445 if (retval)
3446 return retval;
3447
3448 set_user_nice(current, nice);
3449 return 0;
3450}
3451
3452#endif
3453
3454/**
3455 * task_prio - return the priority value of a given task.
3456 * @p: the task in question.
3457 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003458 * Return: The priority value as seen by users in /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 * RT tasks are offset by -200. Normal tasks are centered
3460 * around 0, value goes from -16 to +15.
3461 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003462int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463{
3464 return p->prio - MAX_RT_PRIO;
3465}
3466
3467/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 * idle_cpu - is a given cpu idle currently?
3469 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003470 *
3471 * Return: 1 if the CPU is currently idle. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 */
3473int idle_cpu(int cpu)
3474{
Thomas Gleixner908a3282011-09-15 15:32:06 +02003475 struct rq *rq = cpu_rq(cpu);
3476
3477 if (rq->curr != rq->idle)
3478 return 0;
3479
3480 if (rq->nr_running)
3481 return 0;
3482
3483#ifdef CONFIG_SMP
3484 if (!llist_empty(&rq->wake_list))
3485 return 0;
3486#endif
3487
3488 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491/**
3492 * idle_task - return the idle task for a given cpu.
3493 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003494 *
3495 * Return: The idle task for the cpu @cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003497struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
3499 return cpu_rq(cpu)->idle;
3500}
3501
3502/**
3503 * find_process_by_pid - find a process with a matching PID value.
3504 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003505 *
3506 * The task of @pid, if found. %NULL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003508static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003510 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
Dario Faggioliaab03e02013-11-28 11:14:43 +01003513/*
3514 * This function initializes the sched_dl_entity of a newly becoming
3515 * SCHED_DEADLINE task.
3516 *
3517 * Only the static values are considered here, the actual runtime and the
3518 * absolute deadline will be properly calculated when the task is enqueued
3519 * for the first time with its new policy.
3520 */
3521static void
3522__setparam_dl(struct task_struct *p, const struct sched_attr *attr)
3523{
3524 struct sched_dl_entity *dl_se = &p->dl;
3525
Dario Faggioliaab03e02013-11-28 11:14:43 +01003526 dl_se->dl_runtime = attr->sched_runtime;
3527 dl_se->dl_deadline = attr->sched_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003528 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003529 dl_se->flags = attr->sched_flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01003530 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
Peter Zijlstra40767b02015-01-28 15:08:03 +01003531
3532 /*
3533 * Changing the parameters of a task is 'tricky' and we're not doing
3534 * the correct thing -- also see task_dead_dl() and switched_from_dl().
3535 *
3536 * What we SHOULD do is delay the bandwidth release until the 0-lag
3537 * point. This would include retaining the task_struct until that time
3538 * and change dl_overflow() to not immediately decrement the current
3539 * amount.
3540 *
3541 * Instead we retain the current runtime/deadline and let the new
3542 * parameters take effect after the current reservation period lapses.
3543 * This is safe (albeit pessimistic) because the 0-lag point is always
3544 * before the current scheduling deadline.
3545 *
3546 * We can still have temporary overloads because we do not delay the
3547 * change in bandwidth until that time; so admission control is
3548 * not on the safe side. It does however guarantee tasks will never
3549 * consume more than promised.
3550 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003551}
3552
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003553/*
3554 * sched_setparam() passes in -1 for its policy, to let the functions
3555 * it calls know not to change it.
3556 */
3557#define SETPARAM_POLICY -1
3558
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003559static void __setscheduler_params(struct task_struct *p,
3560 const struct sched_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561{
Dario Faggiolid50dde52013-11-07 14:43:36 +01003562 int policy = attr->sched_policy;
3563
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003564 if (policy == SETPARAM_POLICY)
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003565 policy = p->policy;
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 p->policy = policy;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003568
Dario Faggioliaab03e02013-11-28 11:14:43 +01003569 if (dl_policy(policy))
3570 __setparam_dl(p, attr);
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003571 else if (fair_policy(policy))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003572 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
3573
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003574 /*
3575 * __sched_setscheduler() ensures attr->sched_priority == 0 when
3576 * !rt_policy. Always setting this ensures that things like
3577 * getparam()/getattr() don't report silly values for !rt tasks.
3578 */
3579 p->rt_priority = attr->sched_priority;
Steven Rostedt383afd02014-03-11 19:24:20 -04003580 p->normal_prio = normal_prio(p);
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003581 set_load_weight(p);
3582}
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01003583
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003584/* Actually do priority change: must hold pi & rq lock. */
3585static void __setscheduler(struct rq *rq, struct task_struct *p,
Thomas Gleixner0782e632015-05-05 19:49:49 +02003586 const struct sched_attr *attr, bool keep_boost)
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003587{
3588 __setscheduler_params(p, attr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003589
Steven Rostedt383afd02014-03-11 19:24:20 -04003590 /*
Thomas Gleixner0782e632015-05-05 19:49:49 +02003591 * Keep a potential priority boosting if called from
3592 * sched_setscheduler().
Steven Rostedt383afd02014-03-11 19:24:20 -04003593 */
Thomas Gleixner0782e632015-05-05 19:49:49 +02003594 if (keep_boost)
3595 p->prio = rt_mutex_get_effective_prio(p, normal_prio(p));
3596 else
3597 p->prio = normal_prio(p);
Steven Rostedt383afd02014-03-11 19:24:20 -04003598
Dario Faggioliaab03e02013-11-28 11:14:43 +01003599 if (dl_prio(p->prio))
3600 p->sched_class = &dl_sched_class;
3601 else if (rt_prio(p->prio))
Peter Zijlstraffd44db2009-11-10 20:12:01 +01003602 p->sched_class = &rt_sched_class;
3603 else
3604 p->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
Dario Faggioliaab03e02013-11-28 11:14:43 +01003606
3607static void
3608__getparam_dl(struct task_struct *p, struct sched_attr *attr)
3609{
3610 struct sched_dl_entity *dl_se = &p->dl;
3611
3612 attr->sched_priority = p->rt_priority;
3613 attr->sched_runtime = dl_se->dl_runtime;
3614 attr->sched_deadline = dl_se->dl_deadline;
Harald Gustafsson755378a2013-11-07 14:43:40 +01003615 attr->sched_period = dl_se->dl_period;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003616 attr->sched_flags = dl_se->flags;
3617}
3618
3619/*
3620 * This function validates the new parameters of a -deadline task.
3621 * We ask for the deadline not being zero, and greater or equal
Harald Gustafsson755378a2013-11-07 14:43:40 +01003622 * than the runtime, as well as the period of being zero or
Dario Faggioli332ac172013-11-07 14:43:45 +01003623 * greater than deadline. Furthermore, we have to be sure that
Juri Lellib08278192014-05-13 14:11:31 +02003624 * user parameters are above the internal resolution of 1us (we
3625 * check sched_runtime only since it is always the smaller one) and
3626 * below 2^63 ns (we have to check both sched_deadline and
3627 * sched_period, as the latter can be zero).
Dario Faggioliaab03e02013-11-28 11:14:43 +01003628 */
3629static bool
3630__checkparam_dl(const struct sched_attr *attr)
3631{
Juri Lellib08278192014-05-13 14:11:31 +02003632 /* deadline != 0 */
3633 if (attr->sched_deadline == 0)
3634 return false;
3635
3636 /*
3637 * Since we truncate DL_SCALE bits, make sure we're at least
3638 * that big.
3639 */
3640 if (attr->sched_runtime < (1ULL << DL_SCALE))
3641 return false;
3642
3643 /*
3644 * Since we use the MSB for wrap-around and sign issues, make
3645 * sure it's not set (mind that period can be equal to zero).
3646 */
3647 if (attr->sched_deadline & (1ULL << 63) ||
3648 attr->sched_period & (1ULL << 63))
3649 return false;
3650
3651 /* runtime <= deadline <= period (if period != 0) */
3652 if ((attr->sched_period != 0 &&
3653 attr->sched_period < attr->sched_deadline) ||
3654 attr->sched_deadline < attr->sched_runtime)
3655 return false;
3656
3657 return true;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003658}
3659
David Howellsc69e8d92008-11-14 10:39:19 +11003660/*
3661 * check the target process has a UID that matches the current process's
3662 */
3663static bool check_same_owner(struct task_struct *p)
3664{
3665 const struct cred *cred = current_cred(), *pcred;
3666 bool match;
3667
3668 rcu_read_lock();
3669 pcred = __task_cred(p);
Eric W. Biederman9c806aa2012-02-02 18:54:02 -08003670 match = (uid_eq(cred->euid, pcred->euid) ||
3671 uid_eq(cred->euid, pcred->uid));
David Howellsc69e8d92008-11-14 10:39:19 +11003672 rcu_read_unlock();
3673 return match;
3674}
3675
Wanpeng Li75381602014-11-26 08:44:04 +08003676static bool dl_param_changed(struct task_struct *p,
3677 const struct sched_attr *attr)
3678{
3679 struct sched_dl_entity *dl_se = &p->dl;
3680
3681 if (dl_se->dl_runtime != attr->sched_runtime ||
3682 dl_se->dl_deadline != attr->sched_deadline ||
3683 dl_se->dl_period != attr->sched_period ||
3684 dl_se->flags != attr->sched_flags)
3685 return true;
3686
3687 return false;
3688}
3689
Dario Faggiolid50dde52013-11-07 14:43:36 +01003690static int __sched_setscheduler(struct task_struct *p,
3691 const struct sched_attr *attr,
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003692 bool user, bool pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
Steven Rostedt383afd02014-03-11 19:24:20 -04003694 int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
3695 MAX_RT_PRIO - 1 - attr->sched_priority;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003696 int retval, oldprio, oldpolicy = -1, queued, running;
Thomas Gleixner0782e632015-05-05 19:49:49 +02003697 int new_effective_prio, policy = attr->sched_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 unsigned long flags;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003699 const struct sched_class *prev_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003700 struct rq *rq;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003701 int reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Steven Rostedt66e53932006-06-27 02:54:44 -07003703 /* may grab non-irq protected spin_locks */
3704 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705recheck:
3706 /* double check policy once rq lock held */
Lennart Poetteringca94c442009-06-15 17:17:47 +02003707 if (policy < 0) {
3708 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003710 } else {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003711 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
Lennart Poetteringca94c442009-06-15 17:17:47 +02003712
Dario Faggioliaab03e02013-11-28 11:14:43 +01003713 if (policy != SCHED_DEADLINE &&
3714 policy != SCHED_FIFO && policy != SCHED_RR &&
Lennart Poetteringca94c442009-06-15 17:17:47 +02003715 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
3716 policy != SCHED_IDLE)
3717 return -EINVAL;
3718 }
3719
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003720 if (attr->sched_flags & ~(SCHED_FLAG_RESET_ON_FORK))
3721 return -EINVAL;
3722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 /*
3724 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02003725 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
3726 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 */
Peter Zijlstra0bb040a2014-01-15 17:15:13 +01003728 if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
Dario Faggiolid50dde52013-11-07 14:43:36 +01003729 (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 return -EINVAL;
Dario Faggioliaab03e02013-11-28 11:14:43 +01003731 if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
3732 (rt_policy(policy) != (attr->sched_priority != 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 return -EINVAL;
3734
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003735 /*
3736 * Allow unprivileged RT tasks to decrease priority:
3737 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10003738 if (user && !capable(CAP_SYS_NICE)) {
Dario Faggiolid50dde52013-11-07 14:43:36 +01003739 if (fair_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003740 if (attr->sched_nice < task_nice(p) &&
Peter Zijlstraeaad4512014-01-16 17:54:25 +01003741 !can_nice(p, attr->sched_nice))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003742 return -EPERM;
3743 }
3744
Ingo Molnare05606d2007-07-09 18:51:59 +02003745 if (rt_policy(policy)) {
Oleg Nesterova44702e82010-06-11 01:09:44 +02003746 unsigned long rlim_rtprio =
3747 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07003748
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003749 /* can't set/change the rt policy */
3750 if (policy != p->policy && !rlim_rtprio)
3751 return -EPERM;
3752
3753 /* can't increase priority */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003754 if (attr->sched_priority > p->rt_priority &&
3755 attr->sched_priority > rlim_rtprio)
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003756 return -EPERM;
3757 }
Darren Hartc02aa732011-02-17 15:37:07 -08003758
Juri Lellid44753b2014-03-03 12:09:21 +01003759 /*
3760 * Can't set/change SCHED_DEADLINE policy at all for now
3761 * (safest behavior); in the future we would like to allow
3762 * unprivileged DL tasks to increase their relative deadline
3763 * or reduce their runtime (both ways reducing utilization)
3764 */
3765 if (dl_policy(policy))
3766 return -EPERM;
3767
Ingo Molnardd41f592007-07-09 18:51:59 +02003768 /*
Darren Hartc02aa732011-02-17 15:37:07 -08003769 * Treat SCHED_IDLE as nice 20. Only allow a switch to
3770 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02003771 */
Darren Hartc02aa732011-02-17 15:37:07 -08003772 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003773 if (!can_nice(p, task_nice(p)))
Darren Hartc02aa732011-02-17 15:37:07 -08003774 return -EPERM;
3775 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07003776
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003777 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11003778 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003779 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02003780
3781 /* Normal users shall not reset the sched_reset_on_fork flag */
3782 if (p->sched_reset_on_fork && !reset_on_fork)
3783 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07003784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003786 if (user) {
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003787 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07003788 if (retval)
3789 return retval;
3790 }
3791
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07003793 * make sure no PI-waiters arrive (or leave) while we are
3794 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003795 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003796 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 * runqueue lock must be held.
3798 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003799 rq = task_rq_lock(p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003800
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003801 /*
3802 * Changing the policy of the stop threads its a very bad idea
3803 */
3804 if (p == rq->stop) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003805 task_rq_unlock(rq, p, &flags);
Peter Zijlstra34f971f2010-09-22 13:53:15 +02003806 return -EINVAL;
3807 }
3808
Dario Faggiolia51e9192011-03-24 14:00:18 +01003809 /*
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003810 * If not changing anything there's no need to proceed further,
3811 * but store a possible modification of reset_on_fork.
Dario Faggiolia51e9192011-03-24 14:00:18 +01003812 */
Dario Faggiolid50dde52013-11-07 14:43:36 +01003813 if (unlikely(policy == p->policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05003814 if (fair_policy(policy) && attr->sched_nice != task_nice(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01003815 goto change;
3816 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
3817 goto change;
Wanpeng Li75381602014-11-26 08:44:04 +08003818 if (dl_policy(policy) && dl_param_changed(p, attr))
Dario Faggioliaab03e02013-11-28 11:14:43 +01003819 goto change;
Dario Faggiolid50dde52013-11-07 14:43:36 +01003820
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01003821 p->sched_reset_on_fork = reset_on_fork;
Namhyung Kim45afb172012-07-07 16:49:02 +09003822 task_rq_unlock(rq, p, &flags);
Dario Faggiolia51e9192011-03-24 14:00:18 +01003823 return 0;
3824 }
Dario Faggiolid50dde52013-11-07 14:43:36 +01003825change:
Dario Faggiolia51e9192011-03-24 14:00:18 +01003826
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003827 if (user) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003828#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003829 /*
3830 * Do not allow realtime tasks into groups that have no runtime
3831 * assigned.
3832 */
3833 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01003834 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
3835 !task_group_is_autogroup(task_group(p))) {
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003836 task_rq_unlock(rq, p, &flags);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003837 return -EPERM;
3838 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003839#endif
Dario Faggioli332ac172013-11-07 14:43:45 +01003840#ifdef CONFIG_SMP
3841 if (dl_bandwidth_enabled() && dl_policy(policy)) {
3842 cpumask_t *span = rq->rd->span;
Dario Faggioli332ac172013-11-07 14:43:45 +01003843
3844 /*
3845 * Don't allow tasks with an affinity mask smaller than
3846 * the entire root_domain to become SCHED_DEADLINE. We
3847 * will also fail if there's no bandwidth available.
3848 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01003849 if (!cpumask_subset(span, &p->cpus_allowed) ||
3850 rq->rd->dl_bw.bw == 0) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003851 task_rq_unlock(rq, p, &flags);
3852 return -EPERM;
3853 }
3854 }
3855#endif
3856 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02003857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 /* recheck policy now with rq lock held */
3859 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
3860 policy = oldpolicy = -1;
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003861 task_rq_unlock(rq, p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 goto recheck;
3863 }
Dario Faggioli332ac172013-11-07 14:43:45 +01003864
3865 /*
3866 * If setscheduling to SCHED_DEADLINE (or changing the parameters
3867 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
3868 * is available.
3869 */
Peter Zijlstrae4099a52013-12-17 10:03:34 +01003870 if ((dl_policy(policy) || dl_task(p)) && dl_overflow(p, policy, attr)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01003871 task_rq_unlock(rq, p, &flags);
3872 return -EBUSY;
3873 }
3874
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003875 p->sched_reset_on_fork = reset_on_fork;
3876 oldprio = p->prio;
3877
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003878 if (pi) {
3879 /*
3880 * Take priority boosted tasks into account. If the new
3881 * effective priority is unchanged, we just store the new
3882 * normal parameters and do not touch the scheduler class and
3883 * the runqueue. This will be done when the task deboost
3884 * itself.
3885 */
3886 new_effective_prio = rt_mutex_get_effective_prio(p, newprio);
3887 if (new_effective_prio == oldprio) {
3888 __setscheduler_params(p, attr);
3889 task_rq_unlock(rq, p, &flags);
3890 return 0;
3891 }
Thomas Gleixnerc365c292014-02-07 20:58:42 +01003892 }
3893
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003894 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003895 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003896 if (queued)
Peter Zijlstra4ca9b722012-01-25 11:50:51 +01003897 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003898 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04003899 put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02003900
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01003901 prev_class = p->sched_class;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003902 __setscheduler(rq, p, attr, pi);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02003903
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07003904 if (running)
3905 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003906 if (queued) {
Thomas Gleixner81a44c52014-02-07 20:58:41 +01003907 /*
3908 * We enqueue to tail when the priority of a task is
3909 * increased (user space view).
3910 */
3911 enqueue_task(rq, p, oldprio <= p->prio ? ENQUEUE_HEAD : 0);
3912 }
Steven Rostedtcb469842008-01-25 21:08:22 +01003913
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003914 check_class_changed(rq, p, prev_class, oldprio);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003915 preempt_disable(); /* avoid rq from going away on us */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003916 task_rq_unlock(rq, p, &flags);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003917
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003918 if (pi)
3919 rt_mutex_adjust_pi(p);
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07003920
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02003921 /*
3922 * Run balance callbacks after we've adjusted the PI chain.
3923 */
3924 balance_callback(rq);
3925 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927 return 0;
3928}
Rusty Russell961ccdd2008-06-23 13:55:38 +10003929
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003930static int _sched_setscheduler(struct task_struct *p, int policy,
3931 const struct sched_param *param, bool check)
3932{
3933 struct sched_attr attr = {
3934 .sched_policy = policy,
3935 .sched_priority = param->sched_priority,
3936 .sched_nice = PRIO_TO_NICE(p->static_prio),
3937 };
3938
Steven Rostedtc13db6b2014-07-23 11:28:26 -04003939 /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
3940 if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003941 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
3942 policy &= ~SCHED_RESET_ON_FORK;
3943 attr.sched_policy = policy;
3944 }
3945
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003946 return __sched_setscheduler(p, &attr, check, true);
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003947}
Rusty Russell961ccdd2008-06-23 13:55:38 +10003948/**
3949 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
3950 * @p: the task in question.
3951 * @policy: new policy.
3952 * @param: structure containing the new RT priority.
3953 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02003954 * Return: 0 on success. An error code otherwise.
3955 *
Rusty Russell961ccdd2008-06-23 13:55:38 +10003956 * NOTE that the task may be already dead.
3957 */
3958int sched_setscheduler(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, true);
Rusty Russell961ccdd2008-06-23 13:55:38 +10003962}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963EXPORT_SYMBOL_GPL(sched_setscheduler);
3964
Dario Faggiolid50dde52013-11-07 14:43:36 +01003965int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
3966{
Peter Zijlstradbc7f062015-06-11 14:46:38 +02003967 return __sched_setscheduler(p, attr, true, true);
Dario Faggiolid50dde52013-11-07 14:43:36 +01003968}
3969EXPORT_SYMBOL_GPL(sched_setattr);
3970
Rusty Russell961ccdd2008-06-23 13:55:38 +10003971/**
3972 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
3973 * @p: the task in question.
3974 * @policy: new policy.
3975 * @param: structure containing the new RT priority.
3976 *
3977 * Just like sched_setscheduler, only don't bother checking if the
3978 * current context has permission. For example, this is needed in
3979 * stop_machine(): we create temporary high priority worker threads,
3980 * but our caller might not have that capability.
Yacine Belkadie69f6182013-07-12 20:45:47 +02003981 *
3982 * Return: 0 on success. An error code otherwise.
Rusty Russell961ccdd2008-06-23 13:55:38 +10003983 */
3984int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07003985 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10003986{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01003987 return _sched_setscheduler(p, policy, param, false);
Rusty Russell961ccdd2008-06-23 13:55:38 +10003988}
3989
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003990static int
3991do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 struct sched_param lparam;
3994 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07003995 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 if (!param || pid < 0)
3998 return -EINVAL;
3999 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4000 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004001
4002 rcu_read_lock();
4003 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004005 if (p != NULL)
4006 retval = sched_setscheduler(p, policy, &lparam);
4007 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004008
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 return retval;
4010}
4011
Dario Faggiolid50dde52013-11-07 14:43:36 +01004012/*
4013 * Mimics kernel/events/core.c perf_copy_attr().
4014 */
4015static int sched_copy_attr(struct sched_attr __user *uattr,
4016 struct sched_attr *attr)
4017{
4018 u32 size;
4019 int ret;
4020
4021 if (!access_ok(VERIFY_WRITE, uattr, SCHED_ATTR_SIZE_VER0))
4022 return -EFAULT;
4023
4024 /*
4025 * zero the full structure, so that a short copy will be nice.
4026 */
4027 memset(attr, 0, sizeof(*attr));
4028
4029 ret = get_user(size, &uattr->size);
4030 if (ret)
4031 return ret;
4032
4033 if (size > PAGE_SIZE) /* silly large */
4034 goto err_size;
4035
4036 if (!size) /* abi compat */
4037 size = SCHED_ATTR_SIZE_VER0;
4038
4039 if (size < SCHED_ATTR_SIZE_VER0)
4040 goto err_size;
4041
4042 /*
4043 * If we're handed a bigger struct than we know of,
4044 * ensure all the unknown bits are 0 - i.e. new
4045 * user-space does not rely on any kernel feature
4046 * extensions we dont know about yet.
4047 */
4048 if (size > sizeof(*attr)) {
4049 unsigned char __user *addr;
4050 unsigned char __user *end;
4051 unsigned char val;
4052
4053 addr = (void __user *)uattr + sizeof(*attr);
4054 end = (void __user *)uattr + size;
4055
4056 for (; addr < end; addr++) {
4057 ret = get_user(val, addr);
4058 if (ret)
4059 return ret;
4060 if (val)
4061 goto err_size;
4062 }
4063 size = sizeof(*attr);
4064 }
4065
4066 ret = copy_from_user(attr, uattr, size);
4067 if (ret)
4068 return -EFAULT;
4069
4070 /*
4071 * XXX: do we want to be lenient like existing syscalls; or do we want
4072 * to be strict and return an error on out-of-bounds values?
4073 */
Dongsheng Yang75e45d52014-02-11 15:34:50 +08004074 attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004075
Michael Kerriske78c7bc2014-05-09 16:54:28 +02004076 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004077
4078err_size:
4079 put_user(sizeof(*attr), &uattr->size);
Michael Kerriske78c7bc2014-05-09 16:54:28 +02004080 return -E2BIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004081}
4082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083/**
4084 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4085 * @pid: the pid in question.
4086 * @policy: new policy.
4087 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004088 *
4089 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004091SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
4092 struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
Jason Baronc21761f2006-01-18 17:43:03 -08004094 /* negative values for policy are not valid */
4095 if (policy < 0)
4096 return -EINVAL;
4097
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 return do_sched_setscheduler(pid, policy, param);
4099}
4100
4101/**
4102 * sys_sched_setparam - set/change the RT priority of a thread
4103 * @pid: the pid in question.
4104 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004105 *
4106 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004108SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
Steven Rostedtc13db6b2014-07-23 11:28:26 -04004110 return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111}
4112
4113/**
Dario Faggiolid50dde52013-11-07 14:43:36 +01004114 * sys_sched_setattr - same as above, but with extended sched_attr
4115 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01004116 * @uattr: structure containing the extended parameters.
Masanari Iidadb66d752014-04-18 01:59:15 +09004117 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004118 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004119SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
4120 unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004121{
4122 struct sched_attr attr;
4123 struct task_struct *p;
4124 int retval;
4125
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004126 if (!uattr || pid < 0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004127 return -EINVAL;
4128
Michael Kerrisk143cf232014-05-09 16:54:15 +02004129 retval = sched_copy_attr(uattr, &attr);
4130 if (retval)
4131 return retval;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004132
Richard Weinbergerb14ed2c2014-06-02 22:38:34 +02004133 if ((int)attr.sched_policy < 0)
Peter Zijlstradbdb2272014-05-09 10:49:03 +02004134 return -EINVAL;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004135
4136 rcu_read_lock();
4137 retval = -ESRCH;
4138 p = find_process_by_pid(pid);
4139 if (p != NULL)
4140 retval = sched_setattr(p, &attr);
4141 rcu_read_unlock();
4142
4143 return retval;
4144}
4145
4146/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4148 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004149 *
4150 * Return: On success, the policy of the thread. Otherwise, a negative error
4151 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004153SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004155 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004156 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
4158 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004159 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
4161 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004162 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 p = find_process_by_pid(pid);
4164 if (p) {
4165 retval = security_task_getscheduler(p);
4166 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02004167 retval = p->policy
4168 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004170 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 return retval;
4172}
4173
4174/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02004175 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 * @pid: the pid in question.
4177 * @param: structure containing the RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004178 *
4179 * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
4180 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004182SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02004184 struct sched_param lp = { .sched_priority = 0 };
Ingo Molnar36c8b582006-07-03 00:25:41 -07004185 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004186 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
4188 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004189 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004191 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 p = find_process_by_pid(pid);
4193 retval = -ESRCH;
4194 if (!p)
4195 goto out_unlock;
4196
4197 retval = security_task_getscheduler(p);
4198 if (retval)
4199 goto out_unlock;
4200
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02004201 if (task_has_rt_policy(p))
4202 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004203 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205 /*
4206 * This one might sleep, we cannot do it with a spinlock held ...
4207 */
4208 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 return retval;
4211
4212out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00004213 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 return retval;
4215}
4216
Dario Faggiolid50dde52013-11-07 14:43:36 +01004217static int sched_read_attr(struct sched_attr __user *uattr,
4218 struct sched_attr *attr,
4219 unsigned int usize)
4220{
4221 int ret;
4222
4223 if (!access_ok(VERIFY_WRITE, uattr, usize))
4224 return -EFAULT;
4225
4226 /*
4227 * If we're handed a smaller struct than we know of,
4228 * ensure all the unknown bits are 0 - i.e. old
4229 * user-space does not get uncomplete information.
4230 */
4231 if (usize < sizeof(*attr)) {
4232 unsigned char *addr;
4233 unsigned char *end;
4234
4235 addr = (void *)attr + usize;
4236 end = (void *)attr + sizeof(*attr);
4237
4238 for (; addr < end; addr++) {
4239 if (*addr)
Michael Kerrisk22400672014-05-09 16:54:33 +02004240 return -EFBIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004241 }
4242
4243 attr->size = usize;
4244 }
4245
Vegard Nossum4efbc452014-02-16 22:24:17 +01004246 ret = copy_to_user(uattr, attr, attr->size);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004247 if (ret)
4248 return -EFAULT;
4249
Michael Kerrisk22400672014-05-09 16:54:33 +02004250 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004251}
4252
4253/**
Dario Faggioliaab03e02013-11-28 11:14:43 +01004254 * sys_sched_getattr - similar to sched_getparam, but with sched_attr
Dario Faggiolid50dde52013-11-07 14:43:36 +01004255 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01004256 * @uattr: structure containing the extended parameters.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004257 * @size: sizeof(attr) for fwd/bwd comp.
Masanari Iidadb66d752014-04-18 01:59:15 +09004258 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01004259 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004260SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
4261 unsigned int, size, unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004262{
4263 struct sched_attr attr = {
4264 .size = sizeof(struct sched_attr),
4265 };
4266 struct task_struct *p;
4267 int retval;
4268
4269 if (!uattr || pid < 0 || size > PAGE_SIZE ||
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01004270 size < SCHED_ATTR_SIZE_VER0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01004271 return -EINVAL;
4272
4273 rcu_read_lock();
4274 p = find_process_by_pid(pid);
4275 retval = -ESRCH;
4276 if (!p)
4277 goto out_unlock;
4278
4279 retval = security_task_getscheduler(p);
4280 if (retval)
4281 goto out_unlock;
4282
4283 attr.sched_policy = p->policy;
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004284 if (p->sched_reset_on_fork)
4285 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004286 if (task_has_dl_policy(p))
4287 __getparam_dl(p, &attr);
4288 else if (task_has_rt_policy(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01004289 attr.sched_priority = p->rt_priority;
4290 else
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05004291 attr.sched_nice = task_nice(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004292
4293 rcu_read_unlock();
4294
4295 retval = sched_read_attr(uattr, &attr, size);
4296 return retval;
4297
4298out_unlock:
4299 rcu_read_unlock();
4300 return retval;
4301}
4302
Rusty Russell96f874e22008-11-25 02:35:14 +10304303long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304305 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004306 struct task_struct *p;
4307 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004309 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
4311 p = find_process_by_pid(pid);
4312 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004313 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return -ESRCH;
4315 }
4316
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004317 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004319 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
Tejun Heo14a40ff2013-03-19 13:45:20 -07004321 if (p->flags & PF_NO_SETAFFINITY) {
4322 retval = -EINVAL;
4323 goto out_put_task;
4324 }
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304325 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
4326 retval = -ENOMEM;
4327 goto out_put_task;
4328 }
4329 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
4330 retval = -ENOMEM;
4331 goto out_free_cpus_allowed;
4332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 retval = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004334 if (!check_same_owner(p)) {
4335 rcu_read_lock();
4336 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
4337 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004338 goto out_free_new_mask;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07004339 }
4340 rcu_read_unlock();
4341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004343 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07004344 if (retval)
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004345 goto out_free_new_mask;
David Quigleye7834f82006-06-23 02:03:59 -07004346
Peter Zijlstrae4099a52013-12-17 10:03:34 +01004347
4348 cpuset_cpus_allowed(p, cpus_allowed);
4349 cpumask_and(new_mask, in_mask, cpus_allowed);
4350
Dario Faggioli332ac172013-11-07 14:43:45 +01004351 /*
4352 * Since bandwidth control happens on root_domain basis,
4353 * if admission test is enabled, we only admit -deadline
4354 * tasks allowed to run on all the CPUs in the task's
4355 * root_domain.
4356 */
4357#ifdef CONFIG_SMP
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004358 if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
4359 rcu_read_lock();
4360 if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01004361 retval = -EBUSY;
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004362 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004363 goto out_free_new_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01004364 }
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04004365 rcu_read_unlock();
Dario Faggioli332ac172013-11-07 14:43:45 +01004366 }
4367#endif
Peter Zijlstra49246272010-10-17 21:46:10 +02004368again:
Peter Zijlstra25834c72015-05-15 17:43:34 +02004369 retval = __set_cpus_allowed_ptr(p, new_mask, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Paul Menage8707d8b2007-10-18 23:40:22 -07004371 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304372 cpuset_cpus_allowed(p, cpus_allowed);
4373 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07004374 /*
4375 * We must have raced with a concurrent cpuset
4376 * update. Just reset the cpus_allowed to the
4377 * cpuset's cpus_allowed
4378 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304379 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004380 goto again;
4381 }
4382 }
Kirill Tkhai16303ab2014-09-22 22:36:30 +04004383out_free_new_mask:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304384 free_cpumask_var(new_mask);
4385out_free_cpus_allowed:
4386 free_cpumask_var(cpus_allowed);
4387out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return retval;
4390}
4391
4392static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e22008-11-25 02:35:14 +10304393 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
Rusty Russell96f874e22008-11-25 02:35:14 +10304395 if (len < cpumask_size())
4396 cpumask_clear(new_mask);
4397 else if (len > cpumask_size())
4398 len = cpumask_size();
4399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4401}
4402
4403/**
4404 * sys_sched_setaffinity - set the cpu affinity of a process
4405 * @pid: pid of the process
4406 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4407 * @user_mask_ptr: user-space pointer to the new cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004408 *
4409 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004411SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
4412 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304414 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 int retval;
4416
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304417 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
4418 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Rusty Russell5a16f3d2008-11-25 02:35:11 +10304420 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
4421 if (retval == 0)
4422 retval = sched_setaffinity(pid, new_mask);
4423 free_cpumask_var(new_mask);
4424 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
Rusty Russell96f874e22008-11-25 02:35:14 +10304427long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004429 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00004430 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004433 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435 retval = -ESRCH;
4436 p = find_process_by_pid(pid);
4437 if (!p)
4438 goto out_unlock;
4439
David Quigleye7834f82006-06-23 02:03:59 -07004440 retval = security_task_getscheduler(p);
4441 if (retval)
4442 goto out_unlock;
4443
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004444 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02004445 cpumask_and(mask, &p->cpus_allowed, cpu_active_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02004446 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
4448out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00004449 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Ulrich Drepper9531b622007-08-09 11:16:46 +02004451 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452}
4453
4454/**
4455 * sys_sched_getaffinity - get the cpu affinity of a process
4456 * @pid: pid of the process
4457 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4458 * @user_mask_ptr: user-space pointer to hold the current cpu mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02004459 *
4460 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004462SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
4463 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464{
4465 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10304466 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Anton Blanchard84fba5e2010-04-06 17:02:19 +10004468 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004469 return -EINVAL;
4470 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 return -EINVAL;
4472
Rusty Russellf17c8602008-11-25 02:35:11 +10304473 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
4474 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Rusty Russellf17c8602008-11-25 02:35:11 +10304476 ret = sched_getaffinity(pid, mask);
4477 if (ret == 0) {
KOSAKI Motohiro8bc037f2010-03-17 09:36:58 +09004478 size_t retlen = min_t(size_t, len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004479
4480 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10304481 ret = -EFAULT;
4482 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09004483 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10304484 }
4485 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Rusty Russellf17c8602008-11-25 02:35:11 +10304487 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
4489
4490/**
4491 * sys_sched_yield - yield the current processor to other threads.
4492 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004493 * This function yields the current CPU to other tasks. If there are no
4494 * other threads running on this CPU then this function will return.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004495 *
4496 * Return: 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004498SYSCALL_DEFINE0(sched_yield)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004500 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Ingo Molnar2d723762007-10-15 17:00:12 +02004502 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004503 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
4505 /*
4506 * Since we are going to call schedule() anyway, there's
4507 * no need to preempt or enable interrupts:
4508 */
4509 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004510 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Thomas Gleixner9828ea92009-12-03 20:55:53 +01004511 do_raw_spin_unlock(&rq->lock);
Thomas Gleixnerba74c142011-03-21 13:32:17 +01004512 sched_preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
4514 schedule();
4515
4516 return 0;
4517}
4518
Herbert Xu02b67cc32008-01-25 21:08:28 +01004519int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004521 if (should_resched(0)) {
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004522 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 return 1;
4524 }
4525 return 0;
4526}
Herbert Xu02b67cc32008-01-25 21:08:28 +01004527EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
4529/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004530 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 * call schedule, and on return reacquire the lock.
4532 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004533 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 * operations here to prevent schedule() from being called twice (once via
4535 * spin_unlock(), once by hand).
4536 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004537int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004539 int resched = should_resched(PREEMPT_LOCK_OFFSET);
Jan Kara6df3cec2005-06-13 15:52:32 -07004540 int ret = 0;
4541
Peter Zijlstraf607c662009-07-20 19:16:29 +02004542 lockdep_assert_held(lock);
4543
Paul E. McKenney4a81e832014-06-20 16:49:01 -07004544 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02004546 if (resched)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004547 preempt_schedule_common();
Nick Piggin95c354f2008-01-30 13:31:20 +01004548 else
4549 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004550 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004553 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004555EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004557int __sched __cond_resched_softirq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558{
4559 BUG_ON(!in_softirq());
4560
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03004561 if (should_resched(SOFTIRQ_DISABLE_OFFSET)) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004562 local_bh_enable();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004563 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 local_bh_disable();
4565 return 1;
4566 }
4567 return 0;
4568}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02004569EXPORT_SYMBOL(__cond_resched_softirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571/**
4572 * yield - yield the current processor to other threads.
4573 *
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01004574 * Do not ever use this function, there's a 99% chance you're doing it wrong.
4575 *
4576 * The scheduler is at all times free to pick the calling task as the most
4577 * eligible task to run, if removing the yield() call from your code breaks
4578 * it, its already broken.
4579 *
4580 * Typical broken usage is:
4581 *
4582 * while (!event)
4583 * yield();
4584 *
4585 * where one assumes that yield() will let 'the other' process run that will
4586 * make event true. If the current task is a SCHED_FIFO task that will never
4587 * happen. Never use yield() as a progress guarantee!!
4588 *
4589 * If you want to use yield() to wait for something, use wait_event().
4590 * If you want to use yield() to be 'nice' for others, use cond_resched().
4591 * If you still want to use yield(), do not!
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 */
4593void __sched yield(void)
4594{
4595 set_current_state(TASK_RUNNING);
4596 sys_sched_yield();
4597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598EXPORT_SYMBOL(yield);
4599
Mike Galbraithd95f4122011-02-01 09:50:51 -05004600/**
4601 * yield_to - yield the current processor to another thread in
4602 * your thread group, or accelerate that thread toward the
4603 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07004604 * @p: target task
4605 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05004606 *
4607 * It's the caller's job to ensure that the target task struct
4608 * can't go away on us before we can do any checks.
4609 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004610 * Return:
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304611 * true (>0) if we indeed boosted the target task.
4612 * false (0) if we failed to boost the target.
4613 * -ESRCH if there's no task to yield to.
Mike Galbraithd95f4122011-02-01 09:50:51 -05004614 */
Dan Carpenterfa933842014-05-23 13:20:42 +03004615int __sched yield_to(struct task_struct *p, bool preempt)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004616{
4617 struct task_struct *curr = current;
4618 struct rq *rq, *p_rq;
4619 unsigned long flags;
Dan Carpenterc3c18642013-02-05 14:37:51 +03004620 int yielded = 0;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004621
4622 local_irq_save(flags);
4623 rq = this_rq();
4624
4625again:
4626 p_rq = task_rq(p);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304627 /*
4628 * If we're the only runnable task on the rq and target rq also
4629 * has only one task, there's absolutely no point in yielding.
4630 */
4631 if (rq->nr_running == 1 && p_rq->nr_running == 1) {
4632 yielded = -ESRCH;
4633 goto out_irq;
4634 }
4635
Mike Galbraithd95f4122011-02-01 09:50:51 -05004636 double_rq_lock(rq, p_rq);
Shigeru Yoshida39e24d8f2013-11-23 18:38:01 +09004637 if (task_rq(p) != p_rq) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004638 double_rq_unlock(rq, p_rq);
4639 goto again;
4640 }
4641
4642 if (!curr->sched_class->yield_to_task)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304643 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004644
4645 if (curr->sched_class != p->sched_class)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304646 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004647
4648 if (task_running(p_rq, p) || p->state)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304649 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05004650
4651 yielded = curr->sched_class->yield_to_task(rq, p, preempt);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004652 if (yielded) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05004653 schedstat_inc(rq, yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004654 /*
4655 * Make p's CPU reschedule; pick_next_entity takes care of
4656 * fairness.
4657 */
4658 if (preempt && rq != p_rq)
Kirill Tkhai88751252014-06-29 00:03:57 +04004659 resched_curr(p_rq);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08004660 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05004661
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304662out_unlock:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004663 double_rq_unlock(rq, p_rq);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304664out_irq:
Mike Galbraithd95f4122011-02-01 09:50:51 -05004665 local_irq_restore(flags);
4666
Peter Zijlstra7b270f62013-01-22 13:09:13 +05304667 if (yielded > 0)
Mike Galbraithd95f4122011-02-01 09:50:51 -05004668 schedule();
4669
4670 return yielded;
4671}
4672EXPORT_SYMBOL_GPL(yield_to);
4673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004675 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678long __sched io_schedule_timeout(long timeout)
4679{
NeilBrown9cff8ad2015-02-13 15:49:17 +11004680 int old_iowait = current->in_iowait;
4681 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 long ret;
4683
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -07004684 current->in_iowait = 1;
Shaohua Li10d784e2015-05-08 10:51:29 -07004685 blk_schedule_flush_plug(current);
NeilBrown9cff8ad2015-02-13 15:49:17 +11004686
4687 delayacct_blkio_start();
4688 rq = raw_rq();
4689 atomic_inc(&rq->nr_iowait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 ret = schedule_timeout(timeout);
NeilBrown9cff8ad2015-02-13 15:49:17 +11004691 current->in_iowait = old_iowait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004693 delayacct_blkio_end();
NeilBrown9cff8ad2015-02-13 15:49:17 +11004694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 return ret;
4696}
NeilBrown9cff8ad2015-02-13 15:49:17 +11004697EXPORT_SYMBOL(io_schedule_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
4699/**
4700 * sys_sched_get_priority_max - return maximum RT priority.
4701 * @policy: scheduling class.
4702 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004703 * Return: On success, this syscall returns the maximum
4704 * rt_priority that can be used by a given scheduling class.
4705 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004707SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
4709 int ret = -EINVAL;
4710
4711 switch (policy) {
4712 case SCHED_FIFO:
4713 case SCHED_RR:
4714 ret = MAX_USER_RT_PRIO-1;
4715 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004716 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004718 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004719 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 ret = 0;
4721 break;
4722 }
4723 return ret;
4724}
4725
4726/**
4727 * sys_sched_get_priority_min - return minimum RT priority.
4728 * @policy: scheduling class.
4729 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004730 * Return: On success, this syscall returns the minimum
4731 * rt_priority that can be used by a given scheduling class.
4732 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004734SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735{
4736 int ret = -EINVAL;
4737
4738 switch (policy) {
4739 case SCHED_FIFO:
4740 case SCHED_RR:
4741 ret = 1;
4742 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004743 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004745 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004746 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 ret = 0;
4748 }
4749 return ret;
4750}
4751
4752/**
4753 * sys_sched_rr_get_interval - return the default timeslice of a process.
4754 * @pid: pid of the process.
4755 * @interval: userspace pointer to the timeslice value.
4756 *
4757 * this syscall writes the default timeslice value of a given process
4758 * into the user-space timespec buffer. A value of '0' means infinity.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004759 *
4760 * Return: On success, 0 and the timeslice is in @interval. Otherwise,
4761 * an error code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01004763SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Heiko Carstens754fe8d2009-01-14 14:14:09 +01004764 struct timespec __user *, interval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004766 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004767 unsigned int time_slice;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004768 unsigned long flags;
4769 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004770 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
4773 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004774 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
4776 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004777 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 p = find_process_by_pid(pid);
4779 if (!p)
4780 goto out_unlock;
4781
4782 retval = security_task_getscheduler(p);
4783 if (retval)
4784 goto out_unlock;
4785
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01004786 rq = task_rq_lock(p, &flags);
Peter Zijlstraa57beec2014-01-27 11:54:13 +01004787 time_slice = 0;
4788 if (p->sched_class->get_rr_interval)
4789 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02004790 task_rq_unlock(rq, p, &flags);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004791
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004792 rcu_read_unlock();
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02004793 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004796
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00004798 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 return retval;
4800}
4801
Steven Rostedt7c731e02008-05-12 21:20:41 +02004802static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004803
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004804void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 unsigned long free = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004807 int ppid;
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09004808 unsigned long state = p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Tetsuo Handa1f8a7632014-12-05 21:22:22 +09004810 if (state)
4811 state = __ffs(state) + 1;
Erik Gilling28d06862010-11-19 18:08:51 -08004812 printk(KERN_INFO "%-15.15s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004813 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02004814#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004816 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004818 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819#else
4820 if (state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004821 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004823 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824#endif
4825#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05004826 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827#endif
Oleg Nesterova90e9842014-12-10 15:45:21 -08004828 ppid = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08004829 rcu_read_lock();
Oleg Nesterova90e9842014-12-10 15:45:21 -08004830 if (pid_alive(p))
4831 ppid = task_pid_nr(rcu_dereference(p->real_parent));
Paul E. McKenney4e797522012-11-07 13:35:32 -08004832 rcu_read_unlock();
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004833 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
Paul E. McKenney4e797522012-11-07 13:35:32 -08004834 task_pid_nr(p), ppid,
David Rientjesaa47b7e2009-05-04 01:38:05 -07004835 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Tejun Heo3d1cb202013-04-30 15:27:22 -07004837 print_worker_info(KERN_INFO, p);
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01004838 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839}
4840
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004841void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004843 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Ingo Molnar4bd77322007-07-11 21:21:47 +02004845#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004846 printk(KERN_INFO
4847 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004849 printk(KERN_INFO
4850 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02004852 rcu_read_lock();
Oleg Nesterov5d07f422014-08-13 21:19:53 +02004853 for_each_process_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 /*
4855 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004856 * console might take a lot of time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 */
4858 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07004859 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004860 sched_show_task(p);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02004861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07004863 touch_all_softlockup_watchdogs();
4864
Ingo Molnardd41f592007-07-09 18:51:59 +02004865#ifdef CONFIG_SCHED_DEBUG
4866 sysrq_sched_debug_show();
4867#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02004868 rcu_read_unlock();
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004869 /*
4870 * Only show locks if all tasks are dumped:
4871 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02004872 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004873 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874}
4875
Paul Gortmaker0db06282013-06-19 14:53:51 -04004876void init_idle_bootup_task(struct task_struct *idle)
Ingo Molnar1df21052007-07-09 18:51:58 +02004877{
Ingo Molnardd41f592007-07-09 18:51:59 +02004878 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02004879}
4880
Ingo Molnarf340c0d2005-06-28 16:40:42 +02004881/**
4882 * init_idle - set up an idle thread for a given CPU
4883 * @idle: task in question
4884 * @cpu: cpu the idle task belongs to
4885 *
4886 * NOTE: this function does not set the idle thread's NEED_RESCHED
4887 * flag, to make booting more robust.
4888 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04004889void init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004891 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 unsigned long flags;
4893
Peter Zijlstra25834c72015-05-15 17:43:34 +02004894 raw_spin_lock_irqsave(&idle->pi_lock, flags);
4895 raw_spin_lock(&rq->lock);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01004896
Rik van Riel5e1576e2013-10-07 11:29:26 +01004897 __sched_fork(0, idle);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01004898 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02004899 idle->se.exec_start = sched_clock();
4900
KOSAKI Motohiro1e1b6c52011-05-19 15:08:58 +09004901 do_set_cpus_allowed(idle, cpumask_of(cpu));
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02004902 /*
4903 * We're having a chicken and egg problem, even though we are
4904 * holding rq->lock, the cpu isn't yet set to this cpu so the
4905 * lockdep check in task_group() will fail.
4906 *
4907 * Similar case to sched_fork(). / Alternatively we could
4908 * use task_rq_lock() here and obtain the other rq->lock.
4909 *
4910 * Silence PROVE_RCU
4911 */
4912 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02004913 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02004914 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 rq->curr = rq->idle = idle;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004917 idle->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02004918#if defined(CONFIG_SMP)
4919 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07004920#endif
Peter Zijlstra25834c72015-05-15 17:43:34 +02004921 raw_spin_unlock(&rq->lock);
4922 raw_spin_unlock_irqrestore(&idle->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
4924 /* Set the preempt count _outside_ the spinlocks! */
Peter Zijlstra01028742013-08-14 14:55:46 +02004925 init_idle_preempt_count(idle, cpu);
Jonathan Corbet625f2a32011-04-22 11:19:10 -06004926
Ingo Molnardd41f592007-07-09 18:51:59 +02004927 /*
4928 * The idle tasks have their own, simple scheduling class:
4929 */
4930 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05004931 ftrace_graph_init_idle_task(idle, cpu);
Frederic Weisbecker45eacc62013-05-15 22:16:32 +02004932 vtime_init_idle(idle, cpu);
Carsten Emdef1c6f1a2011-10-26 23:14:16 +02004933#if defined(CONFIG_SMP)
4934 sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
4935#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936}
4937
Juri Lellif82f8042014-10-07 09:52:11 +01004938int cpuset_cpumask_can_shrink(const struct cpumask *cur,
4939 const struct cpumask *trial)
4940{
4941 int ret = 1, trial_cpus;
4942 struct dl_bw *cur_dl_b;
4943 unsigned long flags;
4944
Mike Galbraithbb2bc552015-01-28 04:53:55 +01004945 if (!cpumask_weight(cur))
4946 return ret;
4947
Juri Lelli75e23e492014-10-28 11:54:46 +00004948 rcu_read_lock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01004949 cur_dl_b = dl_bw_of(cpumask_any(cur));
4950 trial_cpus = cpumask_weight(trial);
4951
4952 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
4953 if (cur_dl_b->bw != -1 &&
4954 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
4955 ret = 0;
4956 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00004957 rcu_read_unlock_sched();
Juri Lellif82f8042014-10-07 09:52:11 +01004958
4959 return ret;
4960}
4961
Juri Lelli7f514122014-09-19 10:22:40 +01004962int task_can_attach(struct task_struct *p,
4963 const struct cpumask *cs_cpus_allowed)
4964{
4965 int ret = 0;
4966
4967 /*
4968 * Kthreads which disallow setaffinity shouldn't be moved
4969 * to a new cpuset; we don't want to change their cpu
4970 * affinity and isolating such threads by their set of
4971 * allowed nodes is unnecessary. Thus, cpusets are not
4972 * applicable for such threads. This prevents checking for
4973 * success of set_cpus_allowed_ptr() on all attached tasks
4974 * before cpus_allowed may be changed.
4975 */
4976 if (p->flags & PF_NO_SETAFFINITY) {
4977 ret = -EINVAL;
4978 goto out;
4979 }
4980
4981#ifdef CONFIG_SMP
4982 if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
4983 cs_cpus_allowed)) {
4984 unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
4985 cs_cpus_allowed);
Juri Lelli75e23e492014-10-28 11:54:46 +00004986 struct dl_bw *dl_b;
Juri Lelli7f514122014-09-19 10:22:40 +01004987 bool overflow;
4988 int cpus;
4989 unsigned long flags;
4990
Juri Lelli75e23e492014-10-28 11:54:46 +00004991 rcu_read_lock_sched();
4992 dl_b = dl_bw_of(dest_cpu);
Juri Lelli7f514122014-09-19 10:22:40 +01004993 raw_spin_lock_irqsave(&dl_b->lock, flags);
4994 cpus = dl_bw_cpus(dest_cpu);
4995 overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
4996 if (overflow)
4997 ret = -EBUSY;
4998 else {
4999 /*
5000 * We reserve space for this task in the destination
5001 * root_domain, as we can't fail after this point.
5002 * We will free resources in the source root_domain
5003 * later on (see set_cpus_allowed_dl()).
5004 */
5005 __dl_add(dl_b, p->dl.dl_bw);
5006 }
5007 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli75e23e492014-10-28 11:54:46 +00005008 rcu_read_unlock_sched();
Juri Lelli7f514122014-09-19 10:22:40 +01005009
5010 }
5011#endif
5012out:
5013 return ret;
5014}
5015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
Mel Gormane6628d52013-10-07 11:29:02 +01005018#ifdef CONFIG_NUMA_BALANCING
5019/* Migrate current task p to target_cpu */
5020int migrate_task_to(struct task_struct *p, int target_cpu)
5021{
5022 struct migration_arg arg = { p, target_cpu };
5023 int curr_cpu = task_cpu(p);
5024
5025 if (curr_cpu == target_cpu)
5026 return 0;
5027
5028 if (!cpumask_test_cpu(target_cpu, tsk_cpus_allowed(p)))
5029 return -EINVAL;
5030
5031 /* TODO: This is not properly updating schedstats */
5032
Mel Gorman286549d2014-01-21 15:51:03 -08005033 trace_sched_move_numa(p, curr_cpu, target_cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01005034 return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
5035}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005036
5037/*
5038 * Requeue a task on a given node and accurately track the number of NUMA
5039 * tasks on the runqueues
5040 */
5041void sched_setnuma(struct task_struct *p, int nid)
5042{
5043 struct rq *rq;
5044 unsigned long flags;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005045 bool queued, running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005046
5047 rq = task_rq_lock(p, &flags);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005048 queued = task_on_rq_queued(p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005049 running = task_current(rq, p);
5050
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005051 if (queued)
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005052 dequeue_task(rq, p, 0);
5053 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04005054 put_prev_task(rq, p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005055
5056 p->numa_preferred_nid = nid;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005057
5058 if (running)
5059 p->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005060 if (queued)
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01005061 enqueue_task(rq, p, 0);
5062 task_rq_unlock(rq, p, &flags);
5063}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02005064#endif /* CONFIG_NUMA_BALANCING */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
5066#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar48f24c42006-07-03 00:25:40 -07005067/*
5068 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 * offline.
5070 */
5071void idle_task_exit(void)
5072{
5073 struct mm_struct *mm = current->active_mm;
5074
5075 BUG_ON(cpu_online(smp_processor_id()));
5076
Martin Schwidefskya53efe52012-10-26 17:17:44 +02005077 if (mm != &init_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 switch_mm(mm, &init_mm, current);
Martin Schwidefskya53efe52012-10-26 17:17:44 +02005079 finish_arch_post_lock_switch();
5080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 mmdrop(mm);
5082}
5083
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005084/*
Peter Zijlstra5d180232012-08-20 11:26:57 +02005085 * Since this CPU is going 'away' for a while, fold any nr_active delta
5086 * we might have. Assumes we're called after migrate_tasks() so that the
5087 * nr_active count is stable.
5088 *
5089 * Also see the comment "Global load-average calculations".
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005090 */
Peter Zijlstra5d180232012-08-20 11:26:57 +02005091static void calc_load_migrate(struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092{
Peter Zijlstra5d180232012-08-20 11:26:57 +02005093 long delta = calc_load_fold_active(rq);
5094 if (delta)
5095 atomic_long_add(delta, &calc_load_tasks);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02005096}
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005097
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005098static void put_prev_task_fake(struct rq *rq, struct task_struct *prev)
5099{
5100}
5101
5102static const struct sched_class fake_sched_class = {
5103 .put_prev_task = put_prev_task_fake,
5104};
5105
5106static struct task_struct fake_task = {
5107 /*
5108 * Avoid pull_{rt,dl}_task()
5109 */
5110 .prio = MAX_PRIO + 1,
5111 .sched_class = &fake_sched_class,
5112};
5113
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005114/*
5115 * Migrate all tasks from the rq, sleeping tasks will be migrated by
5116 * try_to_wake_up()->select_task_rq().
5117 *
5118 * Called with rq->lock held even though we'er in stop_machine() and
5119 * there's no concurrency possible, we hold the required locks anyway
5120 * because of lock validation efforts.
5121 */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005122static void migrate_tasks(struct rq *dead_rq)
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005123{
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005124 struct rq *rq = dead_rq;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005125 struct task_struct *next, *stop = rq->stop;
5126 int dest_cpu;
5127
5128 /*
5129 * Fudge the rq selection such that the below task selection loop
5130 * doesn't get stuck on the currently eligible stop task.
5131 *
5132 * We're currently inside stop_machine() and the rq is either stuck
5133 * in the stop_machine_cpu_stop() loop, or we're executing this code,
5134 * either way we should never end up calling schedule() until we're
5135 * done here.
5136 */
5137 rq->stop = NULL;
5138
Frederic Weisbecker77bd3972013-04-12 01:50:58 +02005139 /*
5140 * put_prev_task() and pick_next_task() sched
5141 * class method both need to have an up-to-date
5142 * value of rq->clock[_task]
5143 */
5144 update_rq_clock(rq);
5145
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005146 for (;;) {
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005147 /*
5148 * There's this thread running, bail when that's the only
5149 * remaining thread.
5150 */
5151 if (rq->nr_running == 1)
5152 break;
5153
Peter Zijlstracbce1a62015-06-11 14:46:54 +02005154 /*
5155 * Ensure rq->lock covers the entire task selection
5156 * until the migration.
5157 */
5158 lockdep_pin_lock(&rq->lock);
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01005159 next = pick_next_task(rq, &fake_task);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005160 BUG_ON(!next);
5161 next->sched_class->put_prev_task(rq, next);
5162
5163 /* Find suitable destination for @next, with force if needed. */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005164 dest_cpu = select_fallback_rq(dead_rq->cpu, next);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005165
Peter Zijlstracbce1a62015-06-11 14:46:54 +02005166 lockdep_unpin_lock(&rq->lock);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005167 rq = __migrate_task(rq, next, dest_cpu);
5168 if (rq != dead_rq) {
5169 raw_spin_unlock(&rq->lock);
5170 rq = dead_rq;
5171 raw_spin_lock(&rq->lock);
5172 }
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005173 }
5174
5175 rq->stop = stop;
5176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177#endif /* CONFIG_HOTPLUG_CPU */
5178
Nick Piggine692ab52007-07-26 13:40:43 +02005179#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5180
5181static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005182 {
5183 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005184 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005185 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005186 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005187};
5188
5189static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005190 {
5191 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005192 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005193 .child = sd_ctl_dir,
5194 },
Eric W. Biederman56992302009-11-05 15:38:40 -08005195 {}
Nick Piggine692ab52007-07-26 13:40:43 +02005196};
5197
5198static struct ctl_table *sd_alloc_ctl_entry(int n)
5199{
5200 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005201 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005202
Nick Piggine692ab52007-07-26 13:40:43 +02005203 return entry;
5204}
5205
Milton Miller6382bc92007-10-15 17:00:19 +02005206static void sd_free_ctl_entry(struct ctl_table **tablep)
5207{
Milton Millercd7900762007-10-17 16:55:11 +02005208 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005209
Milton Millercd7900762007-10-17 16:55:11 +02005210 /*
5211 * In the intermediate directories, both the child directory and
5212 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005213 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005214 * static strings and all have proc handlers.
5215 */
5216 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005217 if (entry->child)
5218 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005219 if (entry->proc_handler == NULL)
5220 kfree(entry->procname);
5221 }
Milton Miller6382bc92007-10-15 17:00:19 +02005222
5223 kfree(*tablep);
5224 *tablep = NULL;
5225}
5226
Namhyung Kim201c3732012-08-16 17:03:24 +09005227static int min_load_idx = 0;
libinfd9b86d2013-04-08 14:39:12 +08005228static int max_load_idx = CPU_LOAD_IDX_MAX-1;
Namhyung Kim201c3732012-08-16 17:03:24 +09005229
Nick Piggine692ab52007-07-26 13:40:43 +02005230static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005231set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005232 const char *procname, void *data, int maxlen,
Namhyung Kim201c3732012-08-16 17:03:24 +09005233 umode_t mode, proc_handler *proc_handler,
5234 bool load_idx)
Nick Piggine692ab52007-07-26 13:40:43 +02005235{
Nick Piggine692ab52007-07-26 13:40:43 +02005236 entry->procname = procname;
5237 entry->data = data;
5238 entry->maxlen = maxlen;
5239 entry->mode = mode;
5240 entry->proc_handler = proc_handler;
Namhyung Kim201c3732012-08-16 17:03:24 +09005241
5242 if (load_idx) {
5243 entry->extra1 = &min_load_idx;
5244 entry->extra2 = &max_load_idx;
5245 }
Nick Piggine692ab52007-07-26 13:40:43 +02005246}
5247
5248static struct ctl_table *
5249sd_alloc_ctl_domain_table(struct sched_domain *sd)
5250{
Alex Shi37e6bae2014-01-23 18:39:54 +08005251 struct ctl_table *table = sd_alloc_ctl_entry(14);
Nick Piggine692ab52007-07-26 13:40:43 +02005252
Milton Millerad1cdc12007-10-15 17:00:19 +02005253 if (table == NULL)
5254 return NULL;
5255
Alexey Dobriyane0361852007-08-09 11:16:46 +02005256 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Namhyung Kim201c3732012-08-16 17:03:24 +09005257 sizeof(long), 0644, proc_doulongvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005258 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Namhyung Kim201c3732012-08-16 17:03:24 +09005259 sizeof(long), 0644, proc_doulongvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005260 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005261 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005262 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005263 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005264 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005265 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005266 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005267 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005268 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Namhyung Kim201c3732012-08-16 17:03:24 +09005269 sizeof(int), 0644, proc_dointvec_minmax, true);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005270 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Namhyung Kim201c3732012-08-16 17:03:24 +09005271 sizeof(int), 0644, proc_dointvec_minmax, false);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005272 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Namhyung Kim201c3732012-08-16 17:03:24 +09005273 sizeof(int), 0644, proc_dointvec_minmax, false);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005274 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005275 &sd->cache_nice_tries,
Namhyung Kim201c3732012-08-16 17:03:24 +09005276 sizeof(int), 0644, proc_dointvec_minmax, false);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005277 set_table_entry(&table[10], "flags", &sd->flags,
Namhyung Kim201c3732012-08-16 17:03:24 +09005278 sizeof(int), 0644, proc_dointvec_minmax, false);
Alex Shi37e6bae2014-01-23 18:39:54 +08005279 set_table_entry(&table[11], "max_newidle_lb_cost",
5280 &sd->max_newidle_lb_cost,
5281 sizeof(long), 0644, proc_doulongvec_minmax, false);
5282 set_table_entry(&table[12], "name", sd->name,
Namhyung Kim201c3732012-08-16 17:03:24 +09005283 CORENAME_MAX_SIZE, 0444, proc_dostring, false);
Alex Shi37e6bae2014-01-23 18:39:54 +08005284 /* &table[13] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005285
5286 return table;
5287}
5288
Joe Perchesbe7002e2013-06-12 11:55:36 -07005289static struct ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005290{
5291 struct ctl_table *entry, *table;
5292 struct sched_domain *sd;
5293 int domain_num = 0, i;
5294 char buf[32];
5295
5296 for_each_domain(cpu, sd)
5297 domain_num++;
5298 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005299 if (table == NULL)
5300 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005301
5302 i = 0;
5303 for_each_domain(cpu, sd) {
5304 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005305 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005306 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005307 entry->child = sd_alloc_ctl_domain_table(sd);
5308 entry++;
5309 i++;
5310 }
5311 return table;
5312}
5313
5314static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005315static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005316{
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005317 int i, cpu_num = num_possible_cpus();
Nick Piggine692ab52007-07-26 13:40:43 +02005318 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5319 char buf[32];
5320
Milton Miller73785472007-10-24 18:23:48 +02005321 WARN_ON(sd_ctl_dir[0].child);
5322 sd_ctl_dir[0].child = entry;
5323
Milton Millerad1cdc12007-10-15 17:00:19 +02005324 if (entry == NULL)
5325 return;
5326
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01005327 for_each_possible_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005328 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005329 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005330 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005331 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005332 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005333 }
Milton Miller73785472007-10-24 18:23:48 +02005334
5335 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005336 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5337}
Milton Miller6382bc92007-10-15 17:00:19 +02005338
Milton Miller73785472007-10-24 18:23:48 +02005339/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005340static void unregister_sched_domain_sysctl(void)
5341{
Markus Elfring781b0202015-07-04 09:06:32 +02005342 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005343 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005344 if (sd_ctl_dir[0].child)
5345 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005346}
Nick Piggine692ab52007-07-26 13:40:43 +02005347#else
Milton Miller6382bc92007-10-15 17:00:19 +02005348static void register_sched_domain_sysctl(void)
5349{
5350}
5351static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005352{
5353}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02005354#endif /* CONFIG_SCHED_DEBUG && CONFIG_SYSCTL */
Nick Piggine692ab52007-07-26 13:40:43 +02005355
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005356static void set_rq_online(struct rq *rq)
5357{
5358 if (!rq->online) {
5359 const struct sched_class *class;
5360
Rusty Russellc6c49272008-11-25 02:35:05 +10305361 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005362 rq->online = 1;
5363
5364 for_each_class(class) {
5365 if (class->rq_online)
5366 class->rq_online(rq);
5367 }
5368 }
5369}
5370
5371static void set_rq_offline(struct rq *rq)
5372{
5373 if (rq->online) {
5374 const struct sched_class *class;
5375
5376 for_each_class(class) {
5377 if (class->rq_offline)
5378 class->rq_offline(rq);
5379 }
5380
Rusty Russellc6c49272008-11-25 02:35:05 +10305381 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005382 rq->online = 0;
5383 }
5384}
5385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386/*
5387 * migration_call - callback that gets triggered when a CPU is added.
5388 * Here we can start up the necessary migration thread for the new CPU.
5389 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005390static int
Ingo Molnar48f24c42006-07-03 00:25:40 -07005391migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005393 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 unsigned long flags;
Tejun Heo969c7922010-05-06 18:49:21 +02005395 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005397 switch (action & ~CPU_TASKS_FROZEN) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 case CPU_UP_PREPARE:
Thomas Gleixnera468d382009-07-17 14:15:46 +02005400 rq->calc_load_update = calc_load_update;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 case CPU_ONLINE:
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005404 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005405 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005406 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305407 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005408
5409 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005410 }
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005411 raw_spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005413
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414#ifdef CONFIG_HOTPLUG_CPU
Gregory Haskins08f503b2008-03-10 17:59:11 -04005415 case CPU_DYING:
Peter Zijlstra317f3942011-04-05 17:23:58 +02005416 sched_ttwu_pending();
Gregory Haskins57d885f2008-01-25 21:08:18 +01005417 /* Update our root-domain */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005418 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005419 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305420 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005421 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005422 }
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02005423 migrate_tasks(rq);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005424 BUG_ON(rq->nr_running != 1); /* the migration thread */
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005425 raw_spin_unlock_irqrestore(&rq->lock, flags);
Peter Zijlstra5d180232012-08-20 11:26:57 +02005426 break;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01005427
Peter Zijlstra5d180232012-08-20 11:26:57 +02005428 case CPU_DEAD:
Peter Zijlstraf319da02012-08-20 11:26:57 +02005429 calc_load_migrate(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431#endif
5432 }
Peter Zijlstra49c022e2011-04-05 10:14:25 +02005433
5434 update_max_interval();
5435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 return NOTIFY_OK;
5437}
5438
Paul Mackerrasf38b0822009-06-02 21:05:16 +10005439/*
5440 * Register at high priority so that task migration (migrate_all_tasks)
5441 * happens before everything else. This has to be lower priority than
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005442 * the notifier in the perf_event subsystem, though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04005444static struct notifier_block migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 .notifier_call = migration_call,
Tejun Heo50a323b2010-06-08 21:40:36 +02005446 .priority = CPU_PRI_MIGRATION,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447};
5448
Paul Gortmaker6a82b602015-04-27 18:47:50 -04005449static void set_cpu_rq_start_time(void)
Corey Minyarda803f022014-05-08 13:47:39 -05005450{
5451 int cpu = smp_processor_id();
5452 struct rq *rq = cpu_rq(cpu);
5453 rq->age_stamp = sched_clock_cpu(cpu);
5454}
5455
Paul Gortmaker0db06282013-06-19 14:53:51 -04005456static int sched_cpu_active(struct notifier_block *nfb,
Tejun Heo3a101d02010-06-08 21:40:36 +02005457 unsigned long action, void *hcpu)
5458{
5459 switch (action & ~CPU_TASKS_FROZEN) {
Corey Minyarda803f022014-05-08 13:47:39 -05005460 case CPU_STARTING:
5461 set_cpu_rq_start_time();
5462 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02005463 case CPU_DOWN_FAILED:
5464 set_cpu_active((long)hcpu, true);
5465 return NOTIFY_OK;
5466 default:
5467 return NOTIFY_DONE;
5468 }
5469}
5470
Paul Gortmaker0db06282013-06-19 14:53:51 -04005471static int sched_cpu_inactive(struct notifier_block *nfb,
Tejun Heo3a101d02010-06-08 21:40:36 +02005472 unsigned long action, void *hcpu)
5473{
5474 switch (action & ~CPU_TASKS_FROZEN) {
5475 case CPU_DOWN_PREPARE:
Juri Lelli3c18d442015-03-31 09:53:37 +01005476 set_cpu_active((long)hcpu, false);
Tejun Heo3a101d02010-06-08 21:40:36 +02005477 return NOTIFY_OK;
Juri Lelli3c18d442015-03-31 09:53:37 +01005478 default:
5479 return NOTIFY_DONE;
Tejun Heo3a101d02010-06-08 21:40:36 +02005480 }
5481}
5482
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005483static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484{
5485 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005486 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005487
Tejun Heo3a101d02010-06-08 21:40:36 +02005488 /* Initialize migration for the boot CPU */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005489 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5490 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5492 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005493
Tejun Heo3a101d02010-06-08 21:40:36 +02005494 /* Register cpu active notifiers */
5495 cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
5496 cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
5497
Thomas Gleixnera004cd42009-07-21 09:54:05 +02005498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07005500early_initcall(migration_init);
Christoph Lameter476f3532007-05-06 14:48:58 -07005501
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005502static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
5503
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005504#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005505
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005506static __read_mostly int sched_debug_enabled;
Mike Travisf6630112009-11-17 18:22:15 -06005507
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005508static int __init sched_debug_setup(char *str)
Mike Travisf6630112009-11-17 18:22:15 -06005509{
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005510 sched_debug_enabled = 1;
Mike Travisf6630112009-11-17 18:22:15 -06005511
5512 return 0;
5513}
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005514early_param("sched_debug", sched_debug_setup);
5515
5516static inline bool sched_debug(void)
5517{
5518 return sched_debug_enabled;
5519}
Mike Travisf6630112009-11-17 18:22:15 -06005520
Mike Travis7c16ec52008-04-04 18:11:11 -07005521static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e22008-11-25 02:35:14 +10305522 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005523{
5524 struct sched_group *group = sd->groups;
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005525
Rusty Russell96f874e22008-11-25 02:35:14 +10305526 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005527
5528 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5529
5530 if (!(sd->flags & SD_LOAD_BALANCE)) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005531 printk("does not load-balance\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005532 if (sd->parent)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005533 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5534 " has parent");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005535 return -1;
5536 }
5537
Tejun Heo333470e2015-02-13 14:37:28 -08005538 printk(KERN_CONT "span %*pbl level %s\n",
5539 cpumask_pr_args(sched_domain_span(sd)), sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005540
Rusty Russell758b2cd2008-11-25 02:35:04 +10305541 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005542 printk(KERN_ERR "ERROR: domain->span does not contain "
5543 "CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005544 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10305545 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005546 printk(KERN_ERR "ERROR: domain->groups does not contain"
5547 " CPU%d\n", cpu);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005548 }
5549
5550 printk(KERN_DEBUG "%*s groups:", level + 1, "");
5551 do {
5552 if (!group) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005553 printk("\n");
5554 printk(KERN_ERR "ERROR: group is NULL\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005555 break;
5556 }
5557
Rusty Russell758b2cd2008-11-25 02:35:04 +10305558 if (!cpumask_weight(sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005559 printk(KERN_CONT "\n");
5560 printk(KERN_ERR "ERROR: empty group\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005561 break;
5562 }
5563
Peter Zijlstracb83b622012-04-17 15:49:36 +02005564 if (!(sd->flags & SD_OVERLAP) &&
5565 cpumask_intersects(groupmask, sched_group_cpus(group))) {
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005566 printk(KERN_CONT "\n");
5567 printk(KERN_ERR "ERROR: repeated CPUs\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005568 break;
5569 }
5570
Rusty Russell758b2cd2008-11-25 02:35:04 +10305571 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005572
Tejun Heo333470e2015-02-13 14:37:28 -08005573 printk(KERN_CONT " %*pbl",
5574 cpumask_pr_args(sched_group_cpus(group)));
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04005575 if (group->sgc->capacity != SCHED_CAPACITY_SCALE) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005576 printk(KERN_CONT " (cpu_capacity = %d)",
5577 group->sgc->capacity);
Gautham R Shenoy381512c2009-04-14 09:09:36 +05305578 }
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005579
5580 group = group->next;
5581 } while (group != sd->groups);
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005582 printk(KERN_CONT "\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005583
Rusty Russell758b2cd2008-11-25 02:35:04 +10305584 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005585 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005586
Rusty Russell758b2cd2008-11-25 02:35:04 +10305587 if (sd->parent &&
5588 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01005589 printk(KERN_ERR "ERROR: parent span is not a superset "
5590 "of domain->span\n");
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005591 return 0;
5592}
5593
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594static void sched_domain_debug(struct sched_domain *sd, int cpu)
5595{
5596 int level = 0;
5597
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005598 if (!sched_debug_enabled)
Mike Travisf6630112009-11-17 18:22:15 -06005599 return;
5600
Nick Piggin41c7ce92005-06-25 14:57:24 -07005601 if (!sd) {
5602 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5603 return;
5604 }
5605
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5607
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005608 for (;;) {
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005609 if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 level++;
5612 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005613 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005614 break;
5615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005617#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005618# define sched_domain_debug(sd, cpu) do { } while (0)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02005619static inline bool sched_debug(void)
5620{
5621 return false;
5622}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02005623#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005625static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005626{
Rusty Russell758b2cd2008-11-25 02:35:04 +10305627 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005628 return 1;
5629
5630 /* Following flags need at least 2 groups */
5631 if (sd->flags & (SD_LOAD_BALANCE |
5632 SD_BALANCE_NEWIDLE |
5633 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005634 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005635 SD_SHARE_CPUCAPACITY |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005636 SD_SHARE_PKG_RESOURCES |
5637 SD_SHARE_POWERDOMAIN)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005638 if (sd->groups != sd->groups->next)
5639 return 0;
5640 }
5641
5642 /* Following flags don't use groups */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02005643 if (sd->flags & (SD_WAKE_AFFINE))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005644 return 0;
5645
5646 return 1;
5647}
5648
Ingo Molnar48f24c42006-07-03 00:25:40 -07005649static int
5650sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005651{
5652 unsigned long cflags = sd->flags, pflags = parent->flags;
5653
5654 if (sd_degenerate(parent))
5655 return 1;
5656
Rusty Russell758b2cd2008-11-25 02:35:04 +10305657 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07005658 return 0;
5659
Suresh Siddha245af2c2005-06-25 14:57:25 -07005660 /* Flags needing groups don't count if only 1 group in parent */
5661 if (parent->groups == parent->groups->next) {
5662 pflags &= ~(SD_LOAD_BALANCE |
5663 SD_BALANCE_NEWIDLE |
5664 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005665 SD_BALANCE_EXEC |
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04005666 SD_SHARE_CPUCAPACITY |
Peter Zijlstra10866e622013-08-19 16:57:04 +02005667 SD_SHARE_PKG_RESOURCES |
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02005668 SD_PREFER_SIBLING |
5669 SD_SHARE_POWERDOMAIN);
Ken Chen54364992008-12-07 18:47:37 -08005670 if (nr_node_ids == 1)
5671 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005672 }
5673 if (~cflags & pflags)
5674 return 0;
5675
5676 return 1;
5677}
5678
Peter Zijlstradce840a2011-04-07 14:09:50 +02005679static void free_rootdomain(struct rcu_head *rcu)
Rusty Russellc6c49272008-11-25 02:35:05 +10305680{
Peter Zijlstradce840a2011-04-07 14:09:50 +02005681 struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
Peter Zijlstra047106a2009-11-16 10:28:09 +01005682
Rusty Russell68e74562008-11-25 02:35:13 +10305683 cpupri_cleanup(&rd->cpupri);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005684 cpudl_cleanup(&rd->cpudl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005685 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305686 free_cpumask_var(rd->rto_mask);
5687 free_cpumask_var(rd->online);
5688 free_cpumask_var(rd->span);
5689 kfree(rd);
5690}
5691
Gregory Haskins57d885f2008-01-25 21:08:18 +01005692static void rq_attach_root(struct rq *rq, struct root_domain *rd)
5693{
Ingo Molnara0490fa2009-02-12 11:35:40 +01005694 struct root_domain *old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005695 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005696
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005697 raw_spin_lock_irqsave(&rq->lock, flags);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005698
5699 if (rq->rd) {
Ingo Molnara0490fa2009-02-12 11:35:40 +01005700 old_rd = rq->rd;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005701
Rusty Russellc6c49272008-11-25 02:35:05 +10305702 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005703 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005704
Rusty Russellc6c49272008-11-25 02:35:05 +10305705 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01005706
Ingo Molnara0490fa2009-02-12 11:35:40 +01005707 /*
Shigeru Yoshida05159732013-11-17 12:12:36 +09005708 * If we dont want to free the old_rd yet then
Ingo Molnara0490fa2009-02-12 11:35:40 +01005709 * set old_rd to NULL to skip the freeing later
5710 * in this function:
5711 */
5712 if (!atomic_dec_and_test(&old_rd->refcount))
5713 old_rd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005714 }
5715
5716 atomic_inc(&rd->refcount);
5717 rq->rd = rd;
5718
Rusty Russellc6c49272008-11-25 02:35:05 +10305719 cpumask_set_cpu(rq->cpu, rd->span);
Gregory Haskins00aec932009-07-30 10:57:23 -04005720 if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04005721 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005722
Thomas Gleixner05fa7852009-11-17 14:28:38 +01005723 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnara0490fa2009-02-12 11:35:40 +01005724
5725 if (old_rd)
Peter Zijlstradce840a2011-04-07 14:09:50 +02005726 call_rcu_sched(&old_rd->rcu, free_rootdomain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005727}
5728
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005729static int init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005730{
5731 memset(rd, 0, sizeof(*rd));
5732
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005733 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08005734 goto out;
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005735 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305736 goto free_span;
Juri Lelli1baca4c2013-11-07 14:43:38 +01005737 if (!alloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
Rusty Russellc6c49272008-11-25 02:35:05 +10305738 goto free_online;
Juri Lelli1baca4c2013-11-07 14:43:38 +01005739 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
5740 goto free_dlo_mask;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02005741
Dario Faggioli332ac172013-11-07 14:43:45 +01005742 init_dl_bw(&rd->dl_bw);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01005743 if (cpudl_init(&rd->cpudl) != 0)
5744 goto free_dlo_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01005745
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005746 if (cpupri_init(&rd->cpupri) != 0)
Rusty Russell68e74562008-11-25 02:35:13 +10305747 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10305748 return 0;
5749
Rusty Russell68e74562008-11-25 02:35:13 +10305750free_rto_mask:
5751 free_cpumask_var(rd->rto_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01005752free_dlo_mask:
5753 free_cpumask_var(rd->dlo_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10305754free_online:
5755 free_cpumask_var(rd->online);
5756free_span:
5757 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08005758out:
Rusty Russellc6c49272008-11-25 02:35:05 +10305759 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005760}
5761
Peter Zijlstra029632f2011-10-25 10:00:11 +02005762/*
5763 * By default the system creates a single root-domain with all cpus as
5764 * members (mimicking the global state we have today).
5765 */
5766struct root_domain def_root_domain;
5767
Gregory Haskins57d885f2008-01-25 21:08:18 +01005768static void init_defrootdomain(void)
5769{
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005770 init_rootdomain(&def_root_domain);
Rusty Russellc6c49272008-11-25 02:35:05 +10305771
Gregory Haskins57d885f2008-01-25 21:08:18 +01005772 atomic_set(&def_root_domain.refcount, 1);
5773}
5774
Gregory Haskinsdc938522008-01-25 21:08:26 +01005775static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01005776{
5777 struct root_domain *rd;
5778
5779 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
5780 if (!rd)
5781 return NULL;
5782
Pekka Enberg68c38fc2010-07-15 23:18:22 +03005783 if (init_rootdomain(rd) != 0) {
Rusty Russellc6c49272008-11-25 02:35:05 +10305784 kfree(rd);
5785 return NULL;
5786 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01005787
5788 return rd;
5789}
5790
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005791static void free_sched_groups(struct sched_group *sg, int free_sgc)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005792{
5793 struct sched_group *tmp, *first;
5794
5795 if (!sg)
5796 return;
5797
5798 first = sg;
5799 do {
5800 tmp = sg->next;
5801
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005802 if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
5803 kfree(sg->sgc);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005804
5805 kfree(sg);
5806 sg = tmp;
5807 } while (sg != first);
5808}
5809
Peter Zijlstradce840a2011-04-07 14:09:50 +02005810static void free_sched_domain(struct rcu_head *rcu)
5811{
5812 struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005813
5814 /*
5815 * If its an overlapping domain it has private groups, iterate and
5816 * nuke them all.
5817 */
5818 if (sd->flags & SD_OVERLAP) {
5819 free_sched_groups(sd->groups, 1);
5820 } else if (atomic_dec_and_test(&sd->groups->ref)) {
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04005821 kfree(sd->groups->sgc);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005822 kfree(sd->groups);
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02005823 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02005824 kfree(sd);
5825}
5826
5827static void destroy_sched_domain(struct sched_domain *sd, int cpu)
5828{
5829 call_rcu(&sd->rcu, free_sched_domain);
5830}
5831
5832static void destroy_sched_domains(struct sched_domain *sd, int cpu)
5833{
5834 for (; sd; sd = sd->parent)
5835 destroy_sched_domain(sd, cpu);
5836}
5837
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838/*
Peter Zijlstra518cd622011-12-07 15:07:31 +01005839 * Keep a special pointer to the highest sched_domain that has
5840 * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
5841 * allows us to avoid some pointer chasing select_idle_sibling().
5842 *
5843 * Also keep a unique ID per domain (we use the first cpu number in
5844 * the cpumask of the domain), this allows us to quickly tell if
Peter Zijlstra39be3502012-01-26 12:44:34 +01005845 * two cpus are in the same cache domain, see cpus_share_cache().
Peter Zijlstra518cd622011-12-07 15:07:31 +01005846 */
5847DEFINE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005848DEFINE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005849DEFINE_PER_CPU(int, sd_llc_id);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005850DEFINE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305851DEFINE_PER_CPU(struct sched_domain *, sd_busy);
5852DEFINE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005853
5854static void update_top_cache_domain(int cpu)
5855{
5856 struct sched_domain *sd;
Mel Gorman5d4cf992013-12-17 09:21:25 +00005857 struct sched_domain *busy_sd = NULL;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005858 int id = cpu;
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005859 int size = 1;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005860
5861 sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005862 if (sd) {
Peter Zijlstra518cd622011-12-07 15:07:31 +01005863 id = cpumask_first(sched_domain_span(sd));
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005864 size = cpumask_weight(sched_domain_span(sd));
Mel Gorman5d4cf992013-12-17 09:21:25 +00005865 busy_sd = sd->parent; /* sd_busy */
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005866 }
Mel Gorman5d4cf992013-12-17 09:21:25 +00005867 rcu_assign_pointer(per_cpu(sd_busy, cpu), busy_sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005868
5869 rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08005870 per_cpu(sd_llc_size, cpu) = size;
Peter Zijlstra518cd622011-12-07 15:07:31 +01005871 per_cpu(sd_llc_id, cpu) = id;
Mel Gormanfb13c7e2013-10-07 11:29:17 +01005872
5873 sd = lowest_flag_domain(cpu, SD_NUMA);
5874 rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05305875
5876 sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
5877 rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005878}
5879
5880/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01005881 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 * hold the hotplug lock.
5883 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01005884static void
5885cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005887 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005888 struct sched_domain *tmp;
5889
5890 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08005891 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005892 struct sched_domain *parent = tmp->parent;
5893 if (!parent)
5894 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08005895
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005896 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005897 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005898 if (parent->parent)
5899 parent->parent->child = tmp;
Peter Zijlstra10866e622013-08-19 16:57:04 +02005900 /*
5901 * Transfer SD_PREFER_SIBLING down in case of a
5902 * degenerate parent; the spans match for this
5903 * so the property transfers.
5904 */
5905 if (parent->flags & SD_PREFER_SIBLING)
5906 tmp->flags |= SD_PREFER_SIBLING;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005907 destroy_sched_domain(parent, cpu);
Li Zefanf29c9b12008-11-06 09:45:16 +08005908 } else
5909 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005910 }
5911
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005912 if (sd && sd_degenerate(sd)) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02005913 tmp = sd;
Suresh Siddha245af2c2005-06-25 14:57:25 -07005914 sd = sd->parent;
Peter Zijlstradce840a2011-04-07 14:09:50 +02005915 destroy_sched_domain(tmp, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005916 if (sd)
5917 sd->child = NULL;
5918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
Peter Zijlstra4cb98832011-04-07 14:09:58 +02005920 sched_domain_debug(sd, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Gregory Haskins57d885f2008-01-25 21:08:18 +01005922 rq_attach_root(rq, rd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005923 tmp = rq->sd;
Nick Piggin674311d2005-06-25 14:57:27 -07005924 rcu_assign_pointer(rq->sd, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02005925 destroy_sched_domains(tmp, cpu);
Peter Zijlstra518cd622011-12-07 15:07:31 +01005926
5927 update_top_cache_domain(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928}
5929
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930/* Setup the mask of cpus configured for isolated domains */
5931static int __init isolated_cpu_setup(char *str)
5932{
Rusty Russellbdddd292009-12-02 14:09:16 +10305933 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russell968ea6d2008-12-13 21:55:51 +10305934 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 return 1;
5936}
5937
Ingo Molnar8927f492007-10-15 17:00:13 +02005938__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
Andreas Herrmann49a02c52009-08-18 12:51:52 +02005940struct s_data {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02005941 struct sched_domain ** __percpu sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02005942 struct root_domain *rd;
5943};
5944
Andreas Herrmann2109b992009-08-18 12:53:00 +02005945enum s_alloc {
Andreas Herrmann2109b992009-08-18 12:53:00 +02005946 sa_rootdomain,
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02005947 sa_sd,
Peter Zijlstradce840a2011-04-07 14:09:50 +02005948 sa_sd_storage,
Andreas Herrmann2109b992009-08-18 12:53:00 +02005949 sa_none,
5950};
5951
Peter Zijlstrac1174872012-05-31 14:47:33 +02005952/*
5953 * Build an iteration mask that can exclude certain CPUs from the upwards
5954 * domain traversal.
5955 *
5956 * Asymmetric node setups can result in situations where the domain tree is of
5957 * unequal depth, make sure to skip domains that already cover the entire
5958 * range.
5959 *
5960 * In that case build_sched_domains() will have terminated the iteration early
5961 * and our sibling sd spans will be empty. Domains should always include the
5962 * cpu they're built on, so check that.
5963 *
5964 */
5965static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
5966{
5967 const struct cpumask *span = sched_domain_span(sd);
5968 struct sd_data *sdd = sd->private;
5969 struct sched_domain *sibling;
5970 int i;
5971
5972 for_each_cpu(i, span) {
5973 sibling = *per_cpu_ptr(sdd->sd, i);
5974 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
5975 continue;
5976
5977 cpumask_set_cpu(i, sched_group_mask(sg));
5978 }
5979}
5980
5981/*
5982 * Return the canonical balance cpu for this group, this is the first cpu
5983 * of this group that's also in the iteration mask.
5984 */
5985int group_balance_cpu(struct sched_group *sg)
5986{
5987 return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
5988}
5989
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005990static int
5991build_overlap_sched_groups(struct sched_domain *sd, int cpu)
5992{
5993 struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
5994 const struct cpumask *span = sched_domain_span(sd);
5995 struct cpumask *covered = sched_domains_tmpmask;
5996 struct sd_data *sdd = sd->private;
Zhihui Zhangaaecac42014-08-01 21:18:03 -04005997 struct sched_domain *sibling;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02005998 int i;
5999
6000 cpumask_clear(covered);
6001
6002 for_each_cpu(i, span) {
6003 struct cpumask *sg_span;
6004
6005 if (cpumask_test_cpu(i, covered))
6006 continue;
6007
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006008 sibling = *per_cpu_ptr(sdd->sd, i);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006009
6010 /* See the comment near build_group_mask(). */
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006011 if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
Peter Zijlstrac1174872012-05-31 14:47:33 +02006012 continue;
6013
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006014 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
Suresh Siddha4d78a222011-11-18 15:03:29 -08006015 GFP_KERNEL, cpu_to_node(cpu));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006016
6017 if (!sg)
6018 goto fail;
6019
6020 sg_span = sched_group_cpus(sg);
Zhihui Zhangaaecac42014-08-01 21:18:03 -04006021 if (sibling->child)
6022 cpumask_copy(sg_span, sched_domain_span(sibling->child));
6023 else
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006024 cpumask_set_cpu(i, sg_span);
6025
6026 cpumask_or(covered, covered, sg_span);
6027
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006028 sg->sgc = *per_cpu_ptr(sdd->sgc, i);
6029 if (atomic_inc_return(&sg->sgc->ref) == 1)
Peter Zijlstrac1174872012-05-31 14:47:33 +02006030 build_group_mask(sd, sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006031
Peter Zijlstrac3decf02012-05-31 12:05:32 +02006032 /*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006033 * Initialize sgc->capacity such that even if we mess up the
Peter Zijlstrac3decf02012-05-31 12:05:32 +02006034 * domains and no possible iteration will get us here, we won't
6035 * die on a /0 trap.
6036 */
Nicolas Pitreca8ce3d2014-05-26 18:19:39 -04006037 sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006038
6039 /*
6040 * Make sure the first group of this domain contains the
6041 * canonical balance cpu. Otherwise the sched_domain iteration
6042 * breaks. See update_sg_lb_stats().
6043 */
Peter Zijlstra74a5ce22012-05-23 18:00:43 +02006044 if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
Peter Zijlstrac1174872012-05-31 14:47:33 +02006045 group_balance_cpu(sg) == cpu)
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006046 groups = sg;
6047
6048 if (!first)
6049 first = sg;
6050 if (last)
6051 last->next = sg;
6052 last = sg;
6053 last->next = first;
6054 }
6055 sd->groups = groups;
6056
6057 return 0;
6058
6059fail:
6060 free_sched_groups(first, 0);
6061
6062 return -ENOMEM;
6063}
6064
Peter Zijlstradce840a2011-04-07 14:09:50 +02006065static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006067 struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
6068 struct sched_domain *child = sd->child;
6069
6070 if (child)
6071 cpu = cpumask_first(sched_domain_span(child));
6072
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006073 if (sg) {
Peter Zijlstradce840a2011-04-07 14:09:50 +02006074 *sg = *per_cpu_ptr(sdd->sg, cpu);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006075 (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
6076 atomic_set(&(*sg)->sgc->ref, 1); /* for claim_allocations */
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006077 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006078
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 return cpu;
6080}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Ingo Molnar48f24c42006-07-03 00:25:40 -07006082/*
Peter Zijlstradce840a2011-04-07 14:09:50 +02006083 * build_sched_groups will build a circular linked list of the groups
6084 * covered by the given span, and will set each group's ->cpumask correctly,
Nicolas Pitreced549f2014-05-26 18:19:38 -04006085 * and ->cpu_capacity to 0.
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006086 *
6087 * Assumes the sched_domain tree is fully constructed
Ingo Molnar48f24c42006-07-03 00:25:40 -07006088 */
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006089static int
6090build_sched_groups(struct sched_domain *sd, int cpu)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006091{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006092 struct sched_group *first = NULL, *last = NULL;
6093 struct sd_data *sdd = sd->private;
6094 const struct cpumask *span = sched_domain_span(sd);
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006095 struct cpumask *covered;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006096 int i;
6097
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006098 get_group(cpu, sdd, &sd->groups);
6099 atomic_inc(&sd->groups->ref);
6100
Viresh Kumar09366292013-06-11 16:32:43 +05306101 if (cpu != cpumask_first(span))
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006102 return 0;
6103
Peter Zijlstraf96225f2011-04-07 14:09:57 +02006104 lockdep_assert_held(&sched_domains_mutex);
6105 covered = sched_domains_tmpmask;
6106
Peter Zijlstradce840a2011-04-07 14:09:50 +02006107 cpumask_clear(covered);
6108
6109 for_each_cpu(i, span) {
6110 struct sched_group *sg;
Viresh Kumarcd08e922013-06-11 16:32:44 +05306111 int group, j;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006112
6113 if (cpumask_test_cpu(i, covered))
6114 continue;
6115
Viresh Kumarcd08e922013-06-11 16:32:44 +05306116 group = get_group(i, sdd, &sg);
Peter Zijlstrac1174872012-05-31 14:47:33 +02006117 cpumask_setall(sched_group_mask(sg));
Peter Zijlstradce840a2011-04-07 14:09:50 +02006118
6119 for_each_cpu(j, span) {
6120 if (get_group(j, sdd, NULL) != group)
6121 continue;
6122
6123 cpumask_set_cpu(j, covered);
6124 cpumask_set_cpu(j, sched_group_cpus(sg));
6125 }
6126
6127 if (!first)
6128 first = sg;
6129 if (last)
6130 last->next = sg;
6131 last = sg;
6132 }
6133 last->next = first;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006134
6135 return 0;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006136}
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006137
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138/*
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006139 * Initialize sched groups cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006140 *
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006141 * cpu_capacity indicates the capacity of sched group, which is used while
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006142 * distributing the load between different sched groups in a sched domain.
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006143 * Typically cpu_capacity for all the groups in a sched domain will be same
6144 * unless there are asymmetries in the topology. If there are asymmetries,
6145 * group having more cpu_capacity will pickup more load compared to the
6146 * group having less cpu_capacity.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006147 */
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006148static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006149{
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006150 struct sched_group *sg = sd->groups;
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006151
Viresh Kumar94c95ba2013-06-11 16:32:45 +05306152 WARN_ON(!sg);
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006153
6154 do {
6155 sg->group_weight = cpumask_weight(sched_group_cpus(sg));
6156 sg = sg->next;
6157 } while (sg != sd->groups);
6158
Peter Zijlstrac1174872012-05-31 14:47:33 +02006159 if (cpu != group_balance_cpu(sg))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006160 return;
6161
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006162 update_group_capacity(sd, cpu);
6163 atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006164}
6165
6166/*
Mike Travis7c16ec52008-04-04 18:11:11 -07006167 * Initializers for schedule domains
6168 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
6169 */
6170
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006171static int default_relax_domain_level = -1;
Peter Zijlstra60495e72011-04-07 14:10:04 +02006172int sched_domain_level_max;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006173
6174static int __init setup_relax_domain_level(char *str)
6175{
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006176 if (kstrtoint(str, 0, &default_relax_domain_level))
6177 pr_warn("Unable to set relax_domain_level\n");
Li Zefan30e0e172008-05-13 10:27:17 +08006178
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006179 return 1;
6180}
6181__setup("relax_domain_level=", setup_relax_domain_level);
6182
6183static void set_domain_attribute(struct sched_domain *sd,
6184 struct sched_domain_attr *attr)
6185{
6186 int request;
6187
6188 if (!attr || attr->relax_domain_level < 0) {
6189 if (default_relax_domain_level < 0)
6190 return;
6191 else
6192 request = default_relax_domain_level;
6193 } else
6194 request = attr->relax_domain_level;
6195 if (request < sd->level) {
6196 /* turn off idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006197 sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006198 } else {
6199 /* turn on idle balance on this domain */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02006200 sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006201 }
6202}
6203
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006204static void __sdt_free(const struct cpumask *cpu_map);
6205static int __sdt_alloc(const struct cpumask *cpu_map);
6206
Andreas Herrmann2109b992009-08-18 12:53:00 +02006207static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
6208 const struct cpumask *cpu_map)
6209{
6210 switch (what) {
Andreas Herrmann2109b992009-08-18 12:53:00 +02006211 case sa_rootdomain:
Peter Zijlstra822ff792011-04-07 14:09:51 +02006212 if (!atomic_read(&d->rd->refcount))
6213 free_rootdomain(&d->rd->rcu); /* fall through */
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006214 case sa_sd:
6215 free_percpu(d->sd); /* fall through */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006216 case sa_sd_storage:
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006217 __sdt_free(cpu_map); /* fall through */
Andreas Herrmann2109b992009-08-18 12:53:00 +02006218 case sa_none:
6219 break;
6220 }
6221}
6222
6223static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
6224 const struct cpumask *cpu_map)
6225{
Peter Zijlstradce840a2011-04-07 14:09:50 +02006226 memset(d, 0, sizeof(*d));
6227
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006228 if (__sdt_alloc(cpu_map))
6229 return sa_sd_storage;
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006230 d->sd = alloc_percpu(struct sched_domain *);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006231 if (!d->sd)
6232 return sa_sd_storage;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006233 d->rd = alloc_rootdomain();
Peter Zijlstradce840a2011-04-07 14:09:50 +02006234 if (!d->rd)
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006235 return sa_sd;
Andreas Herrmann2109b992009-08-18 12:53:00 +02006236 return sa_rootdomain;
6237}
6238
Peter Zijlstradce840a2011-04-07 14:09:50 +02006239/*
6240 * NULL the sd_data elements we've used to build the sched_domain and
6241 * sched_group structure so that the subsequent __free_domain_allocs()
6242 * will not free the data we're using.
6243 */
6244static void claim_allocations(int cpu, struct sched_domain *sd)
6245{
6246 struct sd_data *sdd = sd->private;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006247
6248 WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
6249 *per_cpu_ptr(sdd->sd, cpu) = NULL;
6250
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006251 if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
Peter Zijlstradce840a2011-04-07 14:09:50 +02006252 *per_cpu_ptr(sdd->sg, cpu) = NULL;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006253
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006254 if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
6255 *per_cpu_ptr(sdd->sgc, cpu) = NULL;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006256}
6257
Peter Zijlstracb83b622012-04-17 15:49:36 +02006258#ifdef CONFIG_NUMA
Peter Zijlstracb83b622012-04-17 15:49:36 +02006259static int sched_domains_numa_levels;
Rik van Riele3fe70b2014-10-17 03:29:50 -04006260enum numa_topology_type sched_numa_topology_type;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006261static int *sched_domains_numa_distance;
Rik van Riel9942f792014-10-17 03:29:49 -04006262int sched_max_numa_distance;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006263static struct cpumask ***sched_domains_numa_masks;
6264static int sched_domains_curr_level;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006265#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006266
Vincent Guittot143e1e22014-04-11 11:44:37 +02006267/*
6268 * SD_flags allowed in topology descriptions.
6269 *
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006270 * SD_SHARE_CPUCAPACITY - describes SMT topologies
Vincent Guittot143e1e22014-04-11 11:44:37 +02006271 * SD_SHARE_PKG_RESOURCES - describes shared caches
6272 * SD_NUMA - describes NUMA topologies
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006273 * SD_SHARE_POWERDOMAIN - describes shared power domain
Vincent Guittot143e1e22014-04-11 11:44:37 +02006274 *
6275 * Odd one out:
6276 * SD_ASYM_PACKING - describes SMT quirks
6277 */
6278#define TOPOLOGY_SD_FLAGS \
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006279 (SD_SHARE_CPUCAPACITY | \
Vincent Guittot143e1e22014-04-11 11:44:37 +02006280 SD_SHARE_PKG_RESOURCES | \
6281 SD_NUMA | \
Vincent Guittotd77b3ed2014-04-11 11:44:40 +02006282 SD_ASYM_PACKING | \
6283 SD_SHARE_POWERDOMAIN)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006284
6285static struct sched_domain *
Vincent Guittot143e1e22014-04-11 11:44:37 +02006286sd_init(struct sched_domain_topology_level *tl, int cpu)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006287{
6288 struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
Vincent Guittot143e1e22014-04-11 11:44:37 +02006289 int sd_weight, sd_flags = 0;
6290
6291#ifdef CONFIG_NUMA
6292 /*
6293 * Ugly hack to pass state to sd_numa_mask()...
6294 */
6295 sched_domains_curr_level = tl->numa_level;
6296#endif
6297
6298 sd_weight = cpumask_weight(tl->mask(cpu));
6299
6300 if (tl->sd_flags)
6301 sd_flags = (*tl->sd_flags)();
6302 if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
6303 "wrong sd_flags in topology description\n"))
6304 sd_flags &= ~TOPOLOGY_SD_FLAGS;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006305
6306 *sd = (struct sched_domain){
6307 .min_interval = sd_weight,
6308 .max_interval = 2*sd_weight,
6309 .busy_factor = 32,
Peter Zijlstra870a0bb2012-05-11 00:26:27 +02006310 .imbalance_pct = 125,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006311
6312 .cache_nice_tries = 0,
6313 .busy_idx = 0,
6314 .idle_idx = 0,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006315 .newidle_idx = 0,
6316 .wake_idx = 0,
6317 .forkexec_idx = 0,
6318
6319 .flags = 1*SD_LOAD_BALANCE
6320 | 1*SD_BALANCE_NEWIDLE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006321 | 1*SD_BALANCE_EXEC
6322 | 1*SD_BALANCE_FORK
Peter Zijlstracb83b622012-04-17 15:49:36 +02006323 | 0*SD_BALANCE_WAKE
Vincent Guittot143e1e22014-04-11 11:44:37 +02006324 | 1*SD_WAKE_AFFINE
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006325 | 0*SD_SHARE_CPUCAPACITY
Peter Zijlstracb83b622012-04-17 15:49:36 +02006326 | 0*SD_SHARE_PKG_RESOURCES
Vincent Guittot143e1e22014-04-11 11:44:37 +02006327 | 0*SD_SERIALIZE
Peter Zijlstracb83b622012-04-17 15:49:36 +02006328 | 0*SD_PREFER_SIBLING
Vincent Guittot143e1e22014-04-11 11:44:37 +02006329 | 0*SD_NUMA
6330 | sd_flags
Peter Zijlstracb83b622012-04-17 15:49:36 +02006331 ,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006332
Peter Zijlstracb83b622012-04-17 15:49:36 +02006333 .last_balance = jiffies,
6334 .balance_interval = sd_weight,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006335 .smt_gain = 0,
Jason Low2b4cfe62014-04-23 18:30:34 -07006336 .max_newidle_lb_cost = 0,
6337 .next_decay_max_lb_cost = jiffies,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006338#ifdef CONFIG_SCHED_DEBUG
6339 .name = tl->name,
6340#endif
Peter Zijlstracb83b622012-04-17 15:49:36 +02006341 };
Peter Zijlstracb83b622012-04-17 15:49:36 +02006342
6343 /*
Vincent Guittot143e1e22014-04-11 11:44:37 +02006344 * Convert topological properties into behaviour.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006345 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006346
Nicolas Pitre5d4dfdd2014-05-27 13:50:41 -04006347 if (sd->flags & SD_SHARE_CPUCAPACITY) {
Vincent Guittotcaff37e2015-02-27 16:54:13 +01006348 sd->flags |= SD_PREFER_SIBLING;
Vincent Guittot143e1e22014-04-11 11:44:37 +02006349 sd->imbalance_pct = 110;
6350 sd->smt_gain = 1178; /* ~15% */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006351
6352 } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
6353 sd->imbalance_pct = 117;
6354 sd->cache_nice_tries = 1;
6355 sd->busy_idx = 2;
6356
6357#ifdef CONFIG_NUMA
6358 } else if (sd->flags & SD_NUMA) {
6359 sd->cache_nice_tries = 2;
6360 sd->busy_idx = 3;
6361 sd->idle_idx = 2;
6362
6363 sd->flags |= SD_SERIALIZE;
6364 if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
6365 sd->flags &= ~(SD_BALANCE_EXEC |
6366 SD_BALANCE_FORK |
6367 SD_WAKE_AFFINE);
6368 }
6369
6370#endif
6371 } else {
6372 sd->flags |= SD_PREFER_SIBLING;
6373 sd->cache_nice_tries = 1;
6374 sd->busy_idx = 2;
6375 sd->idle_idx = 1;
6376 }
6377
6378 sd->private = &tl->data;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006379
6380 return sd;
6381}
6382
Vincent Guittot143e1e22014-04-11 11:44:37 +02006383/*
6384 * Topology list, bottom-up.
6385 */
6386static struct sched_domain_topology_level default_topology[] = {
6387#ifdef CONFIG_SCHED_SMT
6388 { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
6389#endif
6390#ifdef CONFIG_SCHED_MC
6391 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
6392#endif
Vincent Guittot143e1e22014-04-11 11:44:37 +02006393 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
6394 { NULL, },
6395};
6396
6397struct sched_domain_topology_level *sched_domain_topology = default_topology;
6398
6399#define for_each_sd_topology(tl) \
6400 for (tl = sched_domain_topology; tl->mask; tl++)
6401
6402void set_sched_topology(struct sched_domain_topology_level *tl)
6403{
6404 sched_domain_topology = tl;
6405}
6406
6407#ifdef CONFIG_NUMA
6408
Peter Zijlstracb83b622012-04-17 15:49:36 +02006409static const struct cpumask *sd_numa_mask(int cpu)
6410{
6411 return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
6412}
6413
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006414static void sched_numa_warn(const char *str)
6415{
6416 static int done = false;
6417 int i,j;
6418
6419 if (done)
6420 return;
6421
6422 done = true;
6423
6424 printk(KERN_WARNING "ERROR: %s\n\n", str);
6425
6426 for (i = 0; i < nr_node_ids; i++) {
6427 printk(KERN_WARNING " ");
6428 for (j = 0; j < nr_node_ids; j++)
6429 printk(KERN_CONT "%02d ", node_distance(i,j));
6430 printk(KERN_CONT "\n");
6431 }
6432 printk(KERN_WARNING "\n");
6433}
6434
Rik van Riel9942f792014-10-17 03:29:49 -04006435bool find_numa_distance(int distance)
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006436{
6437 int i;
6438
6439 if (distance == node_distance(0, 0))
6440 return true;
6441
6442 for (i = 0; i < sched_domains_numa_levels; i++) {
6443 if (sched_domains_numa_distance[i] == distance)
6444 return true;
6445 }
6446
6447 return false;
6448}
6449
Rik van Riele3fe70b2014-10-17 03:29:50 -04006450/*
6451 * A system can have three types of NUMA topology:
6452 * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
6453 * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
6454 * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
6455 *
6456 * The difference between a glueless mesh topology and a backplane
6457 * topology lies in whether communication between not directly
6458 * connected nodes goes through intermediary nodes (where programs
6459 * could run), or through backplane controllers. This affects
6460 * placement of programs.
6461 *
6462 * The type of topology can be discerned with the following tests:
6463 * - If the maximum distance between any nodes is 1 hop, the system
6464 * is directly connected.
6465 * - If for two nodes A and B, located N > 1 hops away from each other,
6466 * there is an intermediary node C, which is < N hops away from both
6467 * nodes A and B, the system is a glueless mesh.
6468 */
6469static void init_numa_topology_type(void)
6470{
6471 int a, b, c, n;
6472
6473 n = sched_max_numa_distance;
6474
Aravind Gopalakrishnane2378822015-08-10 20:20:48 -05006475 if (sched_domains_numa_levels <= 1) {
Rik van Riele3fe70b2014-10-17 03:29:50 -04006476 sched_numa_topology_type = NUMA_DIRECT;
Aravind Gopalakrishnane2378822015-08-10 20:20:48 -05006477 return;
6478 }
Rik van Riele3fe70b2014-10-17 03:29:50 -04006479
6480 for_each_online_node(a) {
6481 for_each_online_node(b) {
6482 /* Find two nodes furthest removed from each other. */
6483 if (node_distance(a, b) < n)
6484 continue;
6485
6486 /* Is there an intermediary node between a and b? */
6487 for_each_online_node(c) {
6488 if (node_distance(a, c) < n &&
6489 node_distance(b, c) < n) {
6490 sched_numa_topology_type =
6491 NUMA_GLUELESS_MESH;
6492 return;
6493 }
6494 }
6495
6496 sched_numa_topology_type = NUMA_BACKPLANE;
6497 return;
6498 }
6499 }
6500}
6501
Peter Zijlstracb83b622012-04-17 15:49:36 +02006502static void sched_init_numa(void)
6503{
6504 int next_distance, curr_distance = node_distance(0, 0);
6505 struct sched_domain_topology_level *tl;
6506 int level = 0;
6507 int i, j, k;
6508
Peter Zijlstracb83b622012-04-17 15:49:36 +02006509 sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
6510 if (!sched_domains_numa_distance)
6511 return;
6512
6513 /*
6514 * O(nr_nodes^2) deduplicating selection sort -- in order to find the
6515 * unique distances in the node_distance() table.
6516 *
6517 * Assumes node_distance(0,j) includes all distances in
6518 * node_distance(i,j) in order to avoid cubic time.
Peter Zijlstracb83b622012-04-17 15:49:36 +02006519 */
6520 next_distance = curr_distance;
6521 for (i = 0; i < nr_node_ids; i++) {
6522 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006523 for (k = 0; k < nr_node_ids; k++) {
6524 int distance = node_distance(i, k);
6525
6526 if (distance > curr_distance &&
6527 (distance < next_distance ||
6528 next_distance == curr_distance))
6529 next_distance = distance;
6530
6531 /*
6532 * While not a strong assumption it would be nice to know
6533 * about cases where if node A is connected to B, B is not
6534 * equally connected to A.
6535 */
6536 if (sched_debug() && node_distance(k, i) != distance)
6537 sched_numa_warn("Node-distance not symmetric");
6538
6539 if (sched_debug() && i && !find_numa_distance(distance))
6540 sched_numa_warn("Node-0 not representative");
6541 }
6542 if (next_distance != curr_distance) {
6543 sched_domains_numa_distance[level++] = next_distance;
6544 sched_domains_numa_levels = level;
6545 curr_distance = next_distance;
6546 } else break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006547 }
Peter Zijlstrad039ac62012-05-31 21:20:16 +02006548
6549 /*
6550 * In case of sched_debug() we verify the above assumption.
6551 */
6552 if (!sched_debug())
6553 break;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006554 }
Andrey Ryabininc1235882014-11-07 17:53:40 +03006555
6556 if (!level)
6557 return;
6558
Peter Zijlstracb83b622012-04-17 15:49:36 +02006559 /*
6560 * 'level' contains the number of unique distances, excluding the
6561 * identity distance node_distance(i,i).
6562 *
Viresh Kumar28b4a522013-04-05 16:26:46 +05306563 * The sched_domains_numa_distance[] array includes the actual distance
Peter Zijlstracb83b622012-04-17 15:49:36 +02006564 * numbers.
6565 */
6566
Tang Chen5f7865f2012-09-25 21:12:30 +08006567 /*
6568 * Here, we should temporarily reset sched_domains_numa_levels to 0.
6569 * If it fails to allocate memory for array sched_domains_numa_masks[][],
6570 * the array will contain less then 'level' members. This could be
6571 * dangerous when we use it to iterate array sched_domains_numa_masks[][]
6572 * in other functions.
6573 *
6574 * We reset it to 'level' at the end of this function.
6575 */
6576 sched_domains_numa_levels = 0;
6577
Peter Zijlstracb83b622012-04-17 15:49:36 +02006578 sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
6579 if (!sched_domains_numa_masks)
6580 return;
6581
6582 /*
6583 * Now for each level, construct a mask per node which contains all
6584 * cpus of nodes that are that many hops away from us.
6585 */
6586 for (i = 0; i < level; i++) {
6587 sched_domains_numa_masks[i] =
6588 kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
6589 if (!sched_domains_numa_masks[i])
6590 return;
6591
6592 for (j = 0; j < nr_node_ids; j++) {
Peter Zijlstra2ea45802012-05-25 09:26:43 +02006593 struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
Peter Zijlstracb83b622012-04-17 15:49:36 +02006594 if (!mask)
6595 return;
6596
6597 sched_domains_numa_masks[i][j] = mask;
6598
6599 for (k = 0; k < nr_node_ids; k++) {
Peter Zijlstradd7d8632012-05-11 00:56:20 +02006600 if (node_distance(j, k) > sched_domains_numa_distance[i])
Peter Zijlstracb83b622012-04-17 15:49:36 +02006601 continue;
6602
6603 cpumask_or(mask, mask, cpumask_of_node(k));
6604 }
6605 }
6606 }
6607
Vincent Guittot143e1e22014-04-11 11:44:37 +02006608 /* Compute default topology size */
6609 for (i = 0; sched_domain_topology[i].mask; i++);
6610
Vincent Guittotc515db82014-05-13 11:11:01 +02006611 tl = kzalloc((i + level + 1) *
Peter Zijlstracb83b622012-04-17 15:49:36 +02006612 sizeof(struct sched_domain_topology_level), GFP_KERNEL);
6613 if (!tl)
6614 return;
6615
6616 /*
6617 * Copy the default topology bits..
6618 */
Vincent Guittot143e1e22014-04-11 11:44:37 +02006619 for (i = 0; sched_domain_topology[i].mask; i++)
6620 tl[i] = sched_domain_topology[i];
Peter Zijlstracb83b622012-04-17 15:49:36 +02006621
6622 /*
6623 * .. and append 'j' levels of NUMA goodness.
6624 */
6625 for (j = 0; j < level; i++, j++) {
6626 tl[i] = (struct sched_domain_topology_level){
Peter Zijlstracb83b622012-04-17 15:49:36 +02006627 .mask = sd_numa_mask,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006628 .sd_flags = cpu_numa_flags,
Peter Zijlstracb83b622012-04-17 15:49:36 +02006629 .flags = SDTL_OVERLAP,
6630 .numa_level = j,
Vincent Guittot143e1e22014-04-11 11:44:37 +02006631 SD_INIT_NAME(NUMA)
Peter Zijlstracb83b622012-04-17 15:49:36 +02006632 };
6633 }
6634
6635 sched_domain_topology = tl;
Tang Chen5f7865f2012-09-25 21:12:30 +08006636
6637 sched_domains_numa_levels = level;
Rik van Riel9942f792014-10-17 03:29:49 -04006638 sched_max_numa_distance = sched_domains_numa_distance[level - 1];
Rik van Riele3fe70b2014-10-17 03:29:50 -04006639
6640 init_numa_topology_type();
Peter Zijlstracb83b622012-04-17 15:49:36 +02006641}
Tang Chen301a5cb2012-09-25 21:12:31 +08006642
6643static void sched_domains_numa_masks_set(int cpu)
6644{
6645 int i, j;
6646 int node = cpu_to_node(cpu);
6647
6648 for (i = 0; i < sched_domains_numa_levels; i++) {
6649 for (j = 0; j < nr_node_ids; j++) {
6650 if (node_distance(j, node) <= sched_domains_numa_distance[i])
6651 cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
6652 }
6653 }
6654}
6655
6656static void sched_domains_numa_masks_clear(int cpu)
6657{
6658 int i, j;
6659 for (i = 0; i < sched_domains_numa_levels; i++) {
6660 for (j = 0; j < nr_node_ids; j++)
6661 cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
6662 }
6663}
6664
6665/*
6666 * Update sched_domains_numa_masks[level][node] array when new cpus
6667 * are onlined.
6668 */
6669static int sched_domains_numa_masks_update(struct notifier_block *nfb,
6670 unsigned long action,
6671 void *hcpu)
6672{
6673 int cpu = (long)hcpu;
6674
6675 switch (action & ~CPU_TASKS_FROZEN) {
6676 case CPU_ONLINE:
6677 sched_domains_numa_masks_set(cpu);
6678 break;
6679
6680 case CPU_DEAD:
6681 sched_domains_numa_masks_clear(cpu);
6682 break;
6683
6684 default:
6685 return NOTIFY_DONE;
6686 }
6687
6688 return NOTIFY_OK;
Peter Zijlstracb83b622012-04-17 15:49:36 +02006689}
6690#else
6691static inline void sched_init_numa(void)
6692{
6693}
Tang Chen301a5cb2012-09-25 21:12:31 +08006694
6695static int sched_domains_numa_masks_update(struct notifier_block *nfb,
6696 unsigned long action,
6697 void *hcpu)
6698{
6699 return 0;
6700}
Peter Zijlstracb83b622012-04-17 15:49:36 +02006701#endif /* CONFIG_NUMA */
6702
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006703static int __sdt_alloc(const struct cpumask *cpu_map)
6704{
6705 struct sched_domain_topology_level *tl;
6706 int j;
6707
Viresh Kumar27723a62013-06-10 16:27:20 +05306708 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006709 struct sd_data *sdd = &tl->data;
6710
6711 sdd->sd = alloc_percpu(struct sched_domain *);
6712 if (!sdd->sd)
6713 return -ENOMEM;
6714
6715 sdd->sg = alloc_percpu(struct sched_group *);
6716 if (!sdd->sg)
6717 return -ENOMEM;
6718
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006719 sdd->sgc = alloc_percpu(struct sched_group_capacity *);
6720 if (!sdd->sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006721 return -ENOMEM;
6722
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006723 for_each_cpu(j, cpu_map) {
6724 struct sched_domain *sd;
6725 struct sched_group *sg;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006726 struct sched_group_capacity *sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006727
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02006728 sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006729 GFP_KERNEL, cpu_to_node(j));
6730 if (!sd)
6731 return -ENOMEM;
6732
6733 *per_cpu_ptr(sdd->sd, j) = sd;
6734
6735 sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
6736 GFP_KERNEL, cpu_to_node(j));
6737 if (!sg)
6738 return -ENOMEM;
6739
Igor Mammedov30b4e9e2012-05-09 12:38:28 +02006740 sg->next = sg;
6741
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006742 *per_cpu_ptr(sdd->sg, j) = sg;
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006743
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006744 sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006745 GFP_KERNEL, cpu_to_node(j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006746 if (!sgc)
Peter Zijlstra9c3f75c2011-07-14 13:00:06 +02006747 return -ENOMEM;
6748
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006749 *per_cpu_ptr(sdd->sgc, j) = sgc;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006750 }
6751 }
6752
6753 return 0;
6754}
6755
6756static void __sdt_free(const struct cpumask *cpu_map)
6757{
6758 struct sched_domain_topology_level *tl;
6759 int j;
6760
Viresh Kumar27723a62013-06-10 16:27:20 +05306761 for_each_sd_topology(tl) {
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006762 struct sd_data *sdd = &tl->data;
6763
6764 for_each_cpu(j, cpu_map) {
he, bofb2cf2c2012-04-25 19:59:21 +08006765 struct sched_domain *sd;
6766
6767 if (sdd->sd) {
6768 sd = *per_cpu_ptr(sdd->sd, j);
6769 if (sd && (sd->flags & SD_OVERLAP))
6770 free_sched_groups(sd->groups, 0);
6771 kfree(*per_cpu_ptr(sdd->sd, j));
6772 }
6773
6774 if (sdd->sg)
6775 kfree(*per_cpu_ptr(sdd->sg, j));
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006776 if (sdd->sgc)
6777 kfree(*per_cpu_ptr(sdd->sgc, j));
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006778 }
6779 free_percpu(sdd->sd);
he, bofb2cf2c2012-04-25 19:59:21 +08006780 sdd->sd = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006781 free_percpu(sdd->sg);
he, bofb2cf2c2012-04-25 19:59:21 +08006782 sdd->sg = NULL;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006783 free_percpu(sdd->sgc);
6784 sdd->sgc = NULL;
Peter Zijlstra54ab4ff2011-04-07 14:10:03 +02006785 }
6786}
6787
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006788struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306789 const struct cpumask *cpu_map, struct sched_domain_attr *attr,
6790 struct sched_domain *child, int cpu)
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006791{
Vincent Guittot143e1e22014-04-11 11:44:37 +02006792 struct sched_domain *sd = sd_init(tl, cpu);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006793 if (!sd)
Peter Zijlstrad069b912011-04-07 14:10:02 +02006794 return child;
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006795
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006796 cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
Peter Zijlstra60495e72011-04-07 14:10:04 +02006797 if (child) {
6798 sd->level = child->level + 1;
6799 sched_domain_level_max = max(sched_domain_level_max, sd->level);
Peter Zijlstrad069b912011-04-07 14:10:02 +02006800 child->parent = sd;
Viresh Kumarc75e0122013-06-10 16:27:19 +05306801 sd->child = child;
Peter Zijlstra6ae72df2014-07-22 11:47:40 +02006802
6803 if (!cpumask_subset(sched_domain_span(child),
6804 sched_domain_span(sd))) {
6805 pr_err("BUG: arch topology borken\n");
6806#ifdef CONFIG_SCHED_DEBUG
6807 pr_err(" the %s domain not a subset of the %s domain\n",
6808 child->name, sd->name);
6809#endif
6810 /* Fixup, ensure @sd has at least @child cpus. */
6811 cpumask_or(sched_domain_span(sd),
6812 sched_domain_span(sd),
6813 sched_domain_span(child));
6814 }
6815
Peter Zijlstra60495e72011-04-07 14:10:04 +02006816 }
Dimitri Sivanicha841f8c2012-06-05 13:44:36 -05006817 set_domain_attribute(sd, attr);
Peter Zijlstra2c402dc2011-04-07 14:10:01 +02006818
6819 return sd;
6820}
6821
Mike Travis7c16ec52008-04-04 18:11:11 -07006822/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006823 * Build sched domains for a given set of cpus and attach the sched domains
6824 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006826static int build_sched_domains(const struct cpumask *cpu_map,
6827 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828{
Viresh Kumar1c632162013-06-10 16:27:18 +05306829 enum s_alloc alloc_state;
Peter Zijlstradce840a2011-04-07 14:09:50 +02006830 struct sched_domain *sd;
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006831 struct s_data d;
Peter Zijlstra822ff792011-04-07 14:09:51 +02006832 int i, ret = -ENOMEM;
Rusty Russell3404c8d2008-11-25 02:35:03 +10306833
Andreas Herrmann2109b992009-08-18 12:53:00 +02006834 alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
6835 if (alloc_state != sa_rootdomain)
6836 goto error;
Mike Travis7c16ec52008-04-04 18:11:11 -07006837
Peter Zijlstradce840a2011-04-07 14:09:50 +02006838 /* Set up domains for cpus specified by the cpu_map. */
Rusty Russellabcd0832008-11-25 02:35:02 +10306839 for_each_cpu(i, cpu_map) {
Peter Zijlstraeb7a74e62011-04-07 14:10:00 +02006840 struct sched_domain_topology_level *tl;
6841
Peter Zijlstra3bd65a82011-04-07 14:09:54 +02006842 sd = NULL;
Viresh Kumar27723a62013-06-10 16:27:20 +05306843 for_each_sd_topology(tl) {
Viresh Kumar4a850cb2013-06-04 16:12:43 +05306844 sd = build_sched_domain(tl, cpu_map, attr, sd, i);
Viresh Kumar22da9562013-06-04 15:41:15 +05306845 if (tl == sched_domain_topology)
6846 *per_cpu_ptr(d.sd, i) = sd;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006847 if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
6848 sd->flags |= SD_OVERLAP;
Peter Zijlstrad1102352011-07-20 18:42:57 +02006849 if (cpumask_equal(cpu_map, sched_domain_span(sd)))
6850 break;
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006851 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006852 }
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006853
Peter Zijlstradce840a2011-04-07 14:09:50 +02006854 /* Build the groups for the domains */
6855 for_each_cpu(i, cpu_map) {
6856 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6857 sd->span_weight = cpumask_weight(sched_domain_span(sd));
Peter Zijlstrae3589f62011-07-15 10:35:52 +02006858 if (sd->flags & SD_OVERLAP) {
6859 if (build_overlap_sched_groups(sd, i))
6860 goto error;
6861 } else {
6862 if (build_sched_groups(sd, i))
6863 goto error;
6864 }
Peter Zijlstra1cf519022011-04-07 14:09:47 +02006865 }
Peter Zijlstraa06dadb2011-04-07 14:09:44 +02006866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
Nicolas Pitreced549f2014-05-26 18:19:38 -04006868 /* Calculate CPU capacity for physical packages and nodes */
Peter Zijlstraa9c9a9b2011-04-07 14:09:49 +02006869 for (i = nr_cpumask_bits-1; i >= 0; i--) {
6870 if (!cpumask_test_cpu(i, cpu_map))
6871 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872
Peter Zijlstradce840a2011-04-07 14:09:50 +02006873 for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
6874 claim_allocations(i, sd);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04006875 init_sched_groups_capacity(i, sd);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006876 }
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006877 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006878
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 /* Attach the domains */
Peter Zijlstradce840a2011-04-07 14:09:50 +02006880 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10306881 for_each_cpu(i, cpu_map) {
Peter Zijlstra21d42cc2011-04-07 14:09:48 +02006882 sd = *per_cpu_ptr(d.sd, i);
Andreas Herrmann49a02c52009-08-18 12:51:52 +02006883 cpu_attach_domain(sd, d.rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 }
Peter Zijlstradce840a2011-04-07 14:09:50 +02006885 rcu_read_unlock();
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006886
Peter Zijlstra822ff792011-04-07 14:09:51 +02006887 ret = 0;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006888error:
Andreas Herrmann2109b992009-08-18 12:53:00 +02006889 __free_domain_allocs(&d, alloc_state, cpu_map);
Peter Zijlstra822ff792011-04-07 14:09:51 +02006890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891}
Paul Jackson029190c2007-10-18 23:40:20 -07006892
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306893static cpumask_var_t *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07006894static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02006895static struct sched_domain_attr *dattr_cur;
6896 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07006897
6898/*
6899 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10306900 * cpumask) fails, then fallback to a single sched domain,
6901 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07006902 */
Rusty Russell42128232008-11-25 02:35:12 +10306903static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07006904
Heiko Carstensee79d1b2008-12-09 18:49:50 +01006905/*
6906 * arch_update_cpu_topology lets virtualized architectures update the
6907 * cpu core maps. It is supposed to return 1 if the topology changed
6908 * or 0 if it stayed the same.
6909 */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07006910int __weak arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01006911{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01006912 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01006913}
6914
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306915cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
6916{
6917 int i;
6918 cpumask_var_t *doms;
6919
6920 doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
6921 if (!doms)
6922 return NULL;
6923 for (i = 0; i < ndoms; i++) {
6924 if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
6925 free_sched_domains(doms, i);
6926 return NULL;
6927 }
6928 }
6929 return doms;
6930}
6931
6932void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
6933{
6934 unsigned int i;
6935 for (i = 0; i < ndoms; i++)
6936 free_cpumask_var(doms[i]);
6937 kfree(doms);
6938}
6939
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006940/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006941 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006942 * For now this just excludes isolated cpus, but could be used to
6943 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006944 */
Peter Zijlstrac4a88492011-04-07 14:09:42 +02006945static int init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006946{
Milton Miller73785472007-10-24 18:23:48 +02006947 int err;
6948
Heiko Carstens22e52b02008-03-12 18:31:59 +01006949 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07006950 ndoms_cur = 1;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306951 doms_cur = alloc_sched_domains(ndoms_cur);
Paul Jackson029190c2007-10-18 23:40:20 -07006952 if (!doms_cur)
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306953 doms_cur = &fallback_doms;
6954 cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006955 err = build_sched_domains(doms_cur[0], NULL);
Milton Miller6382bc92007-10-15 17:00:19 +02006956 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006957
6958 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006959}
6960
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006961/*
6962 * Detach sched domains from a group of cpus specified in cpu_map
6963 * These cpus will now be attached to the NULL domain
6964 */
Rusty Russell96f874e22008-11-25 02:35:14 +10306965static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006966{
6967 int i;
6968
Peter Zijlstradce840a2011-04-07 14:09:50 +02006969 rcu_read_lock();
Rusty Russellabcd0832008-11-25 02:35:02 +10306970 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006971 cpu_attach_domain(NULL, &def_root_domain, i);
Peter Zijlstradce840a2011-04-07 14:09:50 +02006972 rcu_read_unlock();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006973}
6974
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09006975/* handle null as "default" */
6976static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
6977 struct sched_domain_attr *new, int idx_new)
6978{
6979 struct sched_domain_attr tmp;
6980
6981 /* fast path */
6982 if (!new && !cur)
6983 return 1;
6984
6985 tmp = SD_ATTR_INIT;
6986 return !memcmp(cur ? (cur + idx_cur) : &tmp,
6987 new ? (new + idx_new) : &tmp,
6988 sizeof(struct sched_domain_attr));
6989}
6990
Paul Jackson029190c2007-10-18 23:40:20 -07006991/*
6992 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006993 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006994 * doms_new[] to the current sched domain partitioning, doms_cur[].
6995 * It destroys each deleted domain and builds each new domain.
6996 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10306997 * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006998 * The masks don't intersect (don't overlap.) We should setup one
6999 * sched domain for each mask. CPUs not in any of the cpumasks will
7000 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007001 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7002 * it as it is.
7003 *
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307004 * The passed in 'doms_new' should be allocated using
7005 * alloc_sched_domains. This routine takes ownership of it and will
7006 * free_sched_domains it when done with it. If the caller failed the
7007 * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
7008 * and partition_sched_domains() will fallback to the single partition
7009 * 'fallback_doms', it also forces the domains to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007010 *
Rusty Russell96f874e22008-11-25 02:35:14 +10307011 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08007012 * ndoms_new == 0 is a special case for destroying existing domains,
7013 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007014 *
Paul Jackson029190c2007-10-18 23:40:20 -07007015 * Call with hotplug lock held
7016 */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307017void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007018 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007019{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007020 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007021 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07007022
Heiko Carstens712555e2008-04-28 11:33:07 +02007023 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007024
Milton Miller73785472007-10-24 18:23:48 +02007025 /* always unregister in case we don't destroy any domains */
7026 unregister_sched_domain_sysctl();
7027
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007028 /* Let architecture update cpu core mappings. */
7029 new_topology = arch_update_cpu_topology();
7030
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007031 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007032
7033 /* Destroy deleted domains */
7034 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007035 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307036 if (cpumask_equal(doms_cur[i], doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007037 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007038 goto match1;
7039 }
7040 /* no match - a current sched domain not in new doms_new[] */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307041 detach_destroy_domains(doms_cur[i]);
Paul Jackson029190c2007-10-18 23:40:20 -07007042match1:
7043 ;
7044 }
7045
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007046 n = ndoms_cur;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007047 if (doms_new == NULL) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007048 n = 0;
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307049 doms_new = &fallback_doms;
Peter Zijlstra6ad4c182009-11-25 13:31:39 +01007050 cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08007051 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007052 }
7053
Paul Jackson029190c2007-10-18 23:40:20 -07007054 /* Build new domains */
7055 for (i = 0; i < ndoms_new; i++) {
Xiaotian Fengc8d2d472013-08-06 20:06:42 +08007056 for (j = 0; j < n && !new_topology; j++) {
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307057 if (cpumask_equal(doms_new[i], doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007058 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007059 goto match2;
7060 }
7061 /* no match - add a new doms_new */
Peter Zijlstradce840a2011-04-07 14:09:50 +02007062 build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007063match2:
7064 ;
7065 }
7066
7067 /* Remember the new sched domains */
Rusty Russellacc3f5d2009-11-03 14:53:40 +10307068 if (doms_cur != &fallback_doms)
7069 free_sched_domains(doms_cur, ndoms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007070 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007071 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007072 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007073 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007074
7075 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007076
Heiko Carstens712555e2008-04-28 11:33:07 +02007077 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007078}
7079
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307080static int num_cpus_frozen; /* used to mark begin/end of suspend/resume */
7081
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082/*
Tejun Heo3a101d02010-06-08 21:40:36 +02007083 * Update cpusets according to cpu_active mask. If cpusets are
7084 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
7085 * around partition_sched_domains().
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307086 *
7087 * If we come here as part of a suspend/resume, don't touch cpusets because we
7088 * want to restore it back to its original state upon resume anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 */
Tejun Heo0b2e9182010-06-21 23:53:31 +02007090static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
7091 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092{
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307093 switch (action) {
7094 case CPU_ONLINE_FROZEN:
7095 case CPU_DOWN_FAILED_FROZEN:
7096
7097 /*
7098 * num_cpus_frozen tracks how many CPUs are involved in suspend
7099 * resume sequence. As long as this is not the last online
7100 * operation in the resume sequence, just build a single sched
7101 * domain, ignoring cpusets.
7102 */
7103 num_cpus_frozen--;
7104 if (likely(num_cpus_frozen)) {
7105 partition_sched_domains(1, NULL, NULL);
7106 break;
7107 }
7108
7109 /*
7110 * This is the last CPU online operation. So fall through and
7111 * restore the original sched domains by considering the
7112 * cpuset configurations.
7113 */
7114
Max Krasnyanskye761b772008-07-15 04:43:49 -07007115 case CPU_ONLINE:
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307116 cpuset_update_active_cpus(true);
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307117 break;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007118 default:
7119 return NOTIFY_DONE;
7120 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307121 return NOTIFY_OK;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007122}
Tejun Heo3a101d02010-06-08 21:40:36 +02007123
Tejun Heo0b2e9182010-06-21 23:53:31 +02007124static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
7125 void *hcpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02007126{
Juri Lelli3c18d442015-03-31 09:53:37 +01007127 unsigned long flags;
7128 long cpu = (long)hcpu;
7129 struct dl_bw *dl_b;
Omar Sandoval533445c2015-05-04 03:09:36 -07007130 bool overflow;
7131 int cpus;
Juri Lelli3c18d442015-03-31 09:53:37 +01007132
Omar Sandoval533445c2015-05-04 03:09:36 -07007133 switch (action) {
Tejun Heo3a101d02010-06-08 21:40:36 +02007134 case CPU_DOWN_PREPARE:
Omar Sandoval533445c2015-05-04 03:09:36 -07007135 rcu_read_lock_sched();
7136 dl_b = dl_bw_of(cpu);
Juri Lelli3c18d442015-03-31 09:53:37 +01007137
Omar Sandoval533445c2015-05-04 03:09:36 -07007138 raw_spin_lock_irqsave(&dl_b->lock, flags);
7139 cpus = dl_bw_cpus(cpu);
7140 overflow = __dl_overflow(dl_b, cpus, 0, 0);
7141 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Juri Lelli3c18d442015-03-31 09:53:37 +01007142
Omar Sandoval533445c2015-05-04 03:09:36 -07007143 rcu_read_unlock_sched();
Juri Lelli3c18d442015-03-31 09:53:37 +01007144
Omar Sandoval533445c2015-05-04 03:09:36 -07007145 if (overflow)
7146 return notifier_from_errno(-EBUSY);
Srivatsa S. Bhat7ddf96b2012-05-24 19:46:55 +05307147 cpuset_update_active_cpus(false);
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307148 break;
7149 case CPU_DOWN_PREPARE_FROZEN:
7150 num_cpus_frozen++;
7151 partition_sched_domains(1, NULL, NULL);
7152 break;
Tejun Heo3a101d02010-06-08 21:40:36 +02007153 default:
7154 return NOTIFY_DONE;
7155 }
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05307156 return NOTIFY_OK;
Tejun Heo3a101d02010-06-08 21:40:36 +02007157}
Max Krasnyanskye761b772008-07-15 04:43:49 -07007158
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159void __init sched_init_smp(void)
7160{
Rusty Russelldcc30a32008-11-25 02:35:12 +10307161 cpumask_var_t non_isolated_cpus;
7162
7163 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Yong Zhangcb5fd132009-09-14 20:20:16 +08007164 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007165
Chris Metcalf8cb97642015-05-06 18:04:26 +02007166 /* nohz_full won't take effect without isolating the cpus. */
7167 tick_nohz_full_add_cpus_to(cpu_isolated_map);
7168
Peter Zijlstracb83b622012-04-17 15:49:36 +02007169 sched_init_numa();
7170
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02007171 /*
7172 * There's no userspace yet to cause hotplug operations; hence all the
7173 * cpu masks are stable and all blatant races in the below code cannot
7174 * happen.
7175 */
Heiko Carstens712555e2008-04-28 11:33:07 +02007176 mutex_lock(&sched_domains_mutex);
Peter Zijlstrac4a88492011-04-07 14:09:42 +02007177 init_sched_domains(cpu_active_mask);
Rusty Russelldcc30a32008-11-25 02:35:12 +10307178 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
7179 if (cpumask_empty(non_isolated_cpus))
7180 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007181 mutex_unlock(&sched_domains_mutex);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007182
Tang Chen301a5cb2012-09-25 21:12:31 +08007183 hotcpu_notifier(sched_domains_numa_masks_update, CPU_PRI_SCHED_ACTIVE);
Tejun Heo3a101d02010-06-08 21:40:36 +02007184 hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
7185 hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007186
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007187 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007188
7189 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307190 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007191 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007192 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10307193 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10307194
Rusty Russell0e3900e2008-11-25 02:35:13 +10307195 init_sched_rt_class();
Juri Lelli1baca4c2013-11-07 14:43:38 +01007196 init_sched_dl_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197}
7198#else
7199void __init sched_init_smp(void)
7200{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007201 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202}
7203#endif /* CONFIG_SMP */
7204
7205int in_sched_functions(unsigned long addr)
7206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 return in_lock_functions(addr) ||
7208 (addr >= (unsigned long)__sched_text_start
7209 && addr < (unsigned long)__sched_text_end);
7210}
7211
Peter Zijlstra029632f2011-10-25 10:00:11 +02007212#ifdef CONFIG_CGROUP_SCHED
Li Zefan27b4b932013-03-05 16:07:52 +08007213/*
7214 * Default task group.
7215 * Every task in system belongs to this group at bootup.
7216 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007217struct task_group root_task_group;
Mike Galbraith35cf4e52012-08-07 05:00:13 +02007218LIST_HEAD(task_groups);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007219#endif
7220
Joonsoo Kime6252c32013-04-23 17:27:41 +09007221DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007222
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223void __init sched_init(void)
7224{
Ingo Molnardd41f592007-07-09 18:51:59 +02007225 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007226 unsigned long alloc_size = 0, ptr;
7227
7228#ifdef CONFIG_FAIR_GROUP_SCHED
7229 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7230#endif
7231#ifdef CONFIG_RT_GROUP_SCHED
7232 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7233#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007234 if (alloc_size) {
Pekka Enberg36b7b6d2009-06-10 23:42:36 +03007235 ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007236
7237#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007238 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007239 ptr += nr_cpu_ids * sizeof(void **);
7240
Yong Zhang07e06b02011-01-07 15:17:36 +08007241 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007242 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007243
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007244#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007245#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007246 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007247 ptr += nr_cpu_ids * sizeof(void **);
7248
Yong Zhang07e06b02011-01-07 15:17:36 +08007249 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007250 ptr += nr_cpu_ids * sizeof(void **);
7251
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007252#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007253 }
Alex Thorltonb74e6272014-12-18 12:44:30 -06007254#ifdef CONFIG_CPUMASK_OFFSTACK
7255 for_each_possible_cpu(i) {
7256 per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
7257 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
7258 }
7259#endif /* CONFIG_CPUMASK_OFFSTACK */
Ingo Molnardd41f592007-07-09 18:51:59 +02007260
Dario Faggioli332ac172013-11-07 14:43:45 +01007261 init_rt_bandwidth(&def_rt_bandwidth,
7262 global_rt_period(), global_rt_runtime());
7263 init_dl_bandwidth(&def_dl_bandwidth,
Peter Zijlstra17248132013-12-17 12:44:49 +01007264 global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +01007265
Gregory Haskins57d885f2008-01-25 21:08:18 +01007266#ifdef CONFIG_SMP
7267 init_defrootdomain();
7268#endif
7269
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007270#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007271 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007272 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007273#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007274
Dhaval Giani7c941432010-01-20 13:26:18 +01007275#ifdef CONFIG_CGROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007276 list_add(&root_task_group.list, &task_groups);
7277 INIT_LIST_HEAD(&root_task_group.children);
Glauber Costaf4d6f6c2011-11-01 19:19:07 -02007278 INIT_LIST_HEAD(&root_task_group.siblings);
Mike Galbraith5091faa2010-11-30 14:18:03 +01007279 autogroup_init(&init_task);
Glauber Costa54c707e2011-11-28 14:45:19 -02007280
Dhaval Giani7c941432010-01-20 13:26:18 +01007281#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007282
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007283 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007284 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285
7286 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007287 raw_spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07007288 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007289 rq->calc_load_active = 0;
7290 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007291 init_cfs_rq(&rq->cfs);
Abel Vesa07c54f72015-03-03 13:50:27 +02007292 init_rt_rq(&rq->rt);
7293 init_dl_rq(&rq->dl);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007294#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007295 root_task_group.shares = ROOT_TASK_GROUP_LOAD;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007296 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007297 /*
Yong Zhang07e06b02011-01-07 15:17:36 +08007298 * How much cpu bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02007299 *
7300 * In case of task-groups formed thr' the cgroup filesystem, it
7301 * gets 100% of the cpu resources in the system. This overall
7302 * system cpu resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08007303 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02007304 * based on each entity's (task or task-group's) weight
7305 * (se->load.weight).
7306 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007307 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02007308 * 1024) and two child groups A0 and A1 (of weight 1024 each),
7309 * then A0's share of the cpu resource is:
7310 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007311 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007312 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007313 * We achieve this by letting root_task_group's tasks sit
7314 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02007315 */
Paul Turnerab84d312011-07-21 09:43:28 -07007316 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Yong Zhang07e06b02011-01-07 15:17:36 +08007317 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007318#endif /* CONFIG_FAIR_GROUP_SCHED */
7319
7320 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007321#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007322 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324
Ingo Molnardd41f592007-07-09 18:51:59 +02007325 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7326 rq->cpu_load[j] = 0;
Venkatesh Pallipadifdf3e952010-05-17 18:14:43 -07007327
7328 rq->last_load_update_tick = jiffies;
7329
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007331 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007332 rq->rd = NULL;
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007333 rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02007334 rq->balance_callback = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007336 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007338 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04007339 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007340 rq->idle_stamp = 0;
7341 rq->avg_idle = 2*sysctl_sched_migration_cost;
Jason Low9bd721c2013-09-13 11:26:52 -07007342 rq->max_idle_balance_cost = sysctl_sched_migration_cost;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007343
7344 INIT_LIST_HEAD(&rq->cfs_tasks);
7345
Gregory Haskinsdc938522008-01-25 21:08:26 +01007346 rq_attach_root(rq, &def_root_domain);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007347#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08007348 rq->nohz_flags = 0;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007349#endif
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02007350#ifdef CONFIG_NO_HZ_FULL
7351 rq->last_sched_tick = 0;
7352#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007354 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 }
7357
Peter Williams2dd73a42006-06-27 02:54:34 -07007358 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007359
Avi Kivitye107be32007-07-26 13:40:43 +02007360#ifdef CONFIG_PREEMPT_NOTIFIERS
7361 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7362#endif
7363
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 /*
7365 * The boot idle thread does lazy MMU switching as well:
7366 */
7367 atomic_inc(&init_mm.mm_count);
7368 enter_lazy_tlb(&init_mm, current);
7369
7370 /*
Yao Dongdong1b537c72014-12-29 14:41:43 +08007371 * During early bootup we pretend to be a normal task:
7372 */
7373 current->sched_class = &fair_sched_class;
7374
7375 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 * Make us the idle thread. Technically, schedule() should not be
7377 * called from this thread, however somewhere below it might be,
7378 * but because we are the idle thread, we just pick up running again
7379 * when this runqueue becomes "idle".
7380 */
7381 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007382
7383 calc_load_update = jiffies + LOAD_FREQ;
7384
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307385#ifdef CONFIG_SMP
Peter Zijlstra4cb98832011-04-07 14:09:58 +02007386 zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
Rusty Russellbdddd292009-12-02 14:09:16 +10307387 /* May be allocated at isolcpus cmdline parse time */
7388 if (cpu_isolated_map == NULL)
7389 zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
Thomas Gleixner29d5e042012-04-20 13:05:45 +00007390 idle_thread_set_boot_cpu();
Corey Minyarda803f022014-05-08 13:47:39 -05007391 set_cpu_rq_start_time();
Peter Zijlstra029632f2011-10-25 10:00:11 +02007392#endif
7393 init_sched_fair_class();
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307394
Ingo Molnar6892b752008-02-13 14:02:36 +01007395 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396}
7397
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02007398#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007399static inline int preempt_count_equals(int preempt_offset)
7400{
Frederic Weisbecker234da7b2009-12-16 20:21:05 +01007401 int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007402
Arnd Bergmann4ba82162011-01-25 22:52:22 +01007403 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007404}
7405
Simon Kagstromd8948372009-12-23 11:08:18 +01007406void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407{
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007408 /*
7409 * Blocking primitives will set (and therefore destroy) current->state,
7410 * since we will exit with TASK_RUNNING make sure we enter with it,
7411 * otherwise we will destroy state.
7412 */
Linus Torvalds00845eb2015-02-01 12:23:32 -08007413 WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007414 "do not call blocking ops when !TASK_RUNNING; "
7415 "state=%lx set at [<%p>] %pS\n",
7416 current->state,
7417 (void *)current->task_state_change,
Linus Torvalds00845eb2015-02-01 12:23:32 -08007418 (void *)current->task_state_change);
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007419
Peter Zijlstra34274452014-09-24 10:18:56 +02007420 ___might_sleep(file, line, preempt_offset);
7421}
7422EXPORT_SYMBOL(__might_sleep);
7423
7424void ___might_sleep(const char *file, int line, int preempt_offset)
7425{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 static unsigned long prev_jiffy; /* ratelimiting */
7427
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07007428 rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
Thomas Gleixnerdb273be2014-02-07 20:58:38 +01007429 if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
7430 !is_idle_task(current)) ||
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007431 system_state != SYSTEM_RUNNING || oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007432 return;
7433 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7434 return;
7435 prev_jiffy = jiffies;
7436
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007437 printk(KERN_ERR
7438 "BUG: sleeping function called from invalid context at %s:%d\n",
7439 file, line);
7440 printk(KERN_ERR
7441 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7442 in_atomic(), irqs_disabled(),
7443 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007444
Eric Sandeena8b686b2014-12-16 16:25:28 -06007445 if (task_stack_end_corrupted(current))
7446 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
7447
Ingo Molnaraef745f2008-08-28 11:34:43 +02007448 debug_show_held_locks(current);
7449 if (irqs_disabled())
7450 print_irqtrace_events(current);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007451#ifdef CONFIG_DEBUG_PREEMPT
7452 if (!preempt_count_equals(preempt_offset)) {
7453 pr_err("Preemption disabled at:");
7454 print_ip_sym(current->preempt_disable_ip);
7455 pr_cont("\n");
7456 }
7457#endif
Ingo Molnaraef745f2008-08-28 11:34:43 +02007458 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459}
Peter Zijlstra34274452014-09-24 10:18:56 +02007460EXPORT_SYMBOL(___might_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461#endif
7462
7463#ifdef CONFIG_MAGIC_SYSRQ
7464void normalize_rt_tasks(void)
7465{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007466 struct task_struct *g, *p;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007467 struct sched_attr attr = {
7468 .sched_policy = SCHED_NORMAL,
7469 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007471 read_lock(&tasklist_lock);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007472 for_each_process_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007473 /*
7474 * Only normalize user tasks:
7475 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007476 if (p->flags & PF_KTHREAD)
Ingo Molnar178be792007-10-15 17:00:18 +02007477 continue;
7478
Ingo Molnardd41f592007-07-09 18:51:59 +02007479 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007480#ifdef CONFIG_SCHEDSTATS
Lucas De Marchi41acab82010-03-10 23:37:45 -03007481 p->se.statistics.wait_start = 0;
7482 p->se.statistics.sleep_start = 0;
7483 p->se.statistics.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007484#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007485
Dario Faggioliaab03e02013-11-28 11:14:43 +01007486 if (!dl_task(p) && !rt_task(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007487 /*
7488 * Renice negative nice level userspace
7489 * tasks back to 0:
7490 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007491 if (task_nice(p) < 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02007492 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007496 __sched_setscheduler(p, &attr, false, false);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007497 }
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007498 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499}
7500
7501#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007502
Jason Wessel67fc4e02010-05-20 21:04:21 -05007503#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007504/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007505 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007506 *
7507 * They can only be called when the whole system has been
7508 * stopped - every CPU needs to be quiescent, and no scheduling
7509 * activity can take place. Using them for anything else would
7510 * be a serious bug, and as a result, they aren't even visible
7511 * under any other configuration.
7512 */
7513
7514/**
7515 * curr_task - return the current task for a given cpu.
7516 * @cpu: the processor in question.
7517 *
7518 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
Yacine Belkadie69f6182013-07-12 20:45:47 +02007519 *
7520 * Return: The current task for @cpu.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007521 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007522struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007523{
7524 return cpu_curr(cpu);
7525}
7526
Jason Wessel67fc4e02010-05-20 21:04:21 -05007527#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7528
7529#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07007530/**
7531 * set_curr_task - set the current task for a given cpu.
7532 * @cpu: the processor in question.
7533 * @p: the task pointer to set.
7534 *
7535 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007536 * are serviced on a separate stack. It allows the architecture to switch the
7537 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007538 * must be called with all CPU's synchronized, and interrupts disabled, the
7539 * and caller must save the original value of the current task (see
7540 * curr_task() above) and restore that value before reenabling interrupts and
7541 * re-starting the system.
7542 *
7543 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7544 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007545void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007546{
7547 cpu_curr(cpu) = p;
7548}
7549
7550#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007551
Dhaval Giani7c941432010-01-20 13:26:18 +01007552#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007553/* task_group_lock serializes the addition/removal of task groups */
7554static DEFINE_SPINLOCK(task_group_lock);
7555
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007556static void free_sched_group(struct task_group *tg)
7557{
7558 free_fair_sched_group(tg);
7559 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01007560 autogroup_free(tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007561 kfree(tg);
7562}
7563
7564/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007565struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007566{
7567 struct task_group *tg;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007568
7569 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7570 if (!tg)
7571 return ERR_PTR(-ENOMEM);
7572
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007573 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007574 goto err;
7575
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007576 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007577 goto err;
7578
Li Zefanace783b2013-01-24 14:30:48 +08007579 return tg;
7580
7581err:
7582 free_sched_group(tg);
7583 return ERR_PTR(-ENOMEM);
7584}
7585
7586void sched_online_group(struct task_group *tg, struct task_group *parent)
7587{
7588 unsigned long flags;
7589
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007590 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007591 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007592
7593 WARN_ON(!parent); /* root should already exist */
7594
7595 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007596 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08007597 list_add_rcu(&tg->siblings, &parent->children);
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/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007602static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007603{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007604 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007605 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007606}
7607
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007608/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007609void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007610{
Li Zefanace783b2013-01-24 14:30:48 +08007611 /* wait for possible concurrent references to cfs_rqs complete */
7612 call_rcu(&tg->rcu, free_sched_group_rcu);
7613}
7614
7615void sched_offline_group(struct task_group *tg)
7616{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007617 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007618 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007619
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007620 /* end participation in shares distribution */
7621 for_each_possible_cpu(i)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007622 unregister_fair_sched_group(tg, i);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007623
7624 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007625 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007626 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007627 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007628}
7629
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007630/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007631 * The caller of this function should have put the task in its new group
7632 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7633 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007634 */
7635void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007636{
Peter Zijlstra8323f262012-06-22 13:36:05 +02007637 struct task_group *tg;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007638 int queued, running;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007639 unsigned long flags;
7640 struct rq *rq;
7641
7642 rq = task_rq_lock(tsk, &flags);
7643
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007644 running = task_current(rq, tsk);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007645 queued = task_on_rq_queued(tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007646
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007647 if (queued)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007648 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007649 if (unlikely(running))
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04007650 put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007651
Kirill Tkhaif7b8a472014-10-28 08:24:34 +03007652 /*
7653 * All callers are synchronized by task_rq_lock(); we do not use RCU
7654 * which is pointless here. Thus, we pass "true" to task_css_check()
7655 * to prevent lockdep warnings.
7656 */
7657 tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
Peter Zijlstra8323f262012-06-22 13:36:05 +02007658 struct task_group, css);
7659 tg = autogroup_task_group(tsk, tg);
7660 tsk->sched_task_group = tg;
7661
Peter Zijlstra810b3812008-02-29 15:21:01 -05007662#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007663 if (tsk->sched_class->task_move_group)
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007664 tsk->sched_class->task_move_group(tsk, queued);
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007665 else
Peter Zijlstra810b3812008-02-29 15:21:01 -05007666#endif
Peter Zijlstrab2b5ce02010-10-15 15:24:15 +02007667 set_task_rq(tsk, task_cpu(tsk));
Peter Zijlstra810b3812008-02-29 15:21:01 -05007668
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007669 if (unlikely(running))
7670 tsk->sched_class->set_curr_task(rq);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007671 if (queued)
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01007672 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007673
Peter Zijlstra0122ec52011-04-05 17:23:51 +02007674 task_rq_unlock(rq, tsk, &flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007675}
Dhaval Giani7c941432010-01-20 13:26:18 +01007676#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007677
Paul Turnera790de92011-07-21 09:43:29 -07007678#ifdef CONFIG_RT_GROUP_SCHED
7679/*
7680 * Ensure that the real time constraints are schedulable.
7681 */
7682static DEFINE_MUTEX(rt_constraints_mutex);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007683
Dhaval Giani521f1a242008-02-28 15:21:56 +05307684/* Must be called with tasklist_lock held */
7685static inline int tg_has_rt_tasks(struct task_group *tg)
7686{
7687 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007688
Peter Zijlstra1fe89e12015-02-09 11:53:18 +01007689 /*
7690 * Autogroups do not have RT tasks; see autogroup_create().
7691 */
7692 if (task_group_is_autogroup(tg))
7693 return 0;
7694
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007695 for_each_process_thread(g, p) {
Oleg Nesterov8651c652014-09-21 21:33:36 +02007696 if (rt_task(p) && task_group(p) == tg)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307697 return 1;
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007698 }
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007699
Dhaval Giani521f1a242008-02-28 15:21:56 +05307700 return 0;
7701}
7702
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007703struct rt_schedulable_data {
7704 struct task_group *tg;
7705 u64 rt_period;
7706 u64 rt_runtime;
7707};
7708
Paul Turnera790de92011-07-21 09:43:29 -07007709static int tg_rt_schedulable(struct task_group *tg, void *data)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007710{
7711 struct rt_schedulable_data *d = data;
7712 struct task_group *child;
7713 unsigned long total, sum = 0;
7714 u64 period, runtime;
7715
7716 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7717 runtime = tg->rt_bandwidth.rt_runtime;
7718
7719 if (tg == d->tg) {
7720 period = d->rt_period;
7721 runtime = d->rt_runtime;
7722 }
7723
Peter Zijlstra4653f802008-09-23 15:33:44 +02007724 /*
7725 * Cannot have more runtime than the period.
7726 */
7727 if (runtime > period && runtime != RUNTIME_INF)
7728 return -EINVAL;
7729
7730 /*
7731 * Ensure we don't starve existing RT tasks.
7732 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007733 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
7734 return -EBUSY;
7735
7736 total = to_ratio(period, runtime);
7737
Peter Zijlstra4653f802008-09-23 15:33:44 +02007738 /*
7739 * Nobody can have more than the global setting allows.
7740 */
7741 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
7742 return -EINVAL;
7743
7744 /*
7745 * The sum of our children's runtime should not exceed our own.
7746 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007747 list_for_each_entry_rcu(child, &tg->children, siblings) {
7748 period = ktime_to_ns(child->rt_bandwidth.rt_period);
7749 runtime = child->rt_bandwidth.rt_runtime;
7750
7751 if (child == d->tg) {
7752 period = d->rt_period;
7753 runtime = d->rt_runtime;
7754 }
7755
7756 sum += to_ratio(period, runtime);
7757 }
7758
7759 if (sum > total)
7760 return -EINVAL;
7761
7762 return 0;
7763}
7764
7765static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
7766{
Paul Turner82774342011-07-21 09:43:35 -07007767 int ret;
7768
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007769 struct rt_schedulable_data data = {
7770 .tg = tg,
7771 .rt_period = period,
7772 .rt_runtime = runtime,
7773 };
7774
Paul Turner82774342011-07-21 09:43:35 -07007775 rcu_read_lock();
7776 ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
7777 rcu_read_unlock();
7778
7779 return ret;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007780}
7781
Paul Turnerab84d312011-07-21 09:43:28 -07007782static int tg_set_rt_bandwidth(struct task_group *tg,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007783 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007784{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007785 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007786
Peter Zijlstra2636ed52015-02-09 12:23:20 +01007787 /*
7788 * Disallowing the root group RT runtime is BAD, it would disallow the
7789 * kernel creating (and or operating) RT threads.
7790 */
7791 if (tg == &root_task_group && rt_runtime == 0)
7792 return -EINVAL;
7793
7794 /* No period doesn't make any sense. */
7795 if (rt_period == 0)
7796 return -EINVAL;
7797
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007798 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307799 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007800 err = __rt_schedulable(tg, rt_period, rt_runtime);
7801 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05307802 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007803
Thomas Gleixner0986b112009-11-17 15:32:06 +01007804 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007805 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
7806 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007807
7808 for_each_possible_cpu(i) {
7809 struct rt_rq *rt_rq = tg->rt_rq[i];
7810
Thomas Gleixner0986b112009-11-17 15:32:06 +01007811 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007812 rt_rq->rt_runtime = rt_runtime;
Thomas Gleixner0986b112009-11-17 15:32:06 +01007813 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007814 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007815 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra49246272010-10-17 21:46:10 +02007816unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307817 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007818 mutex_unlock(&rt_constraints_mutex);
7819
7820 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007821}
7822
Li Zefan25cc7da2013-03-05 16:07:33 +08007823static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007824{
7825 u64 rt_runtime, rt_period;
7826
7827 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
7828 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7829 if (rt_runtime_us < 0)
7830 rt_runtime = RUNTIME_INF;
7831
Paul Turnerab84d312011-07-21 09:43:28 -07007832 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007833}
7834
Li Zefan25cc7da2013-03-05 16:07:33 +08007835static long sched_group_rt_runtime(struct task_group *tg)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007836{
7837 u64 rt_runtime_us;
7838
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007839 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007840 return -1;
7841
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007842 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007843 do_div(rt_runtime_us, NSEC_PER_USEC);
7844 return rt_runtime_us;
7845}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007846
Nicholas Mc Guirece2f5fe2015-05-03 10:51:56 +02007847static int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007848{
7849 u64 rt_runtime, rt_period;
7850
Nicholas Mc Guirece2f5fe2015-05-03 10:51:56 +02007851 rt_period = rt_period_us * NSEC_PER_USEC;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007852 rt_runtime = tg->rt_bandwidth.rt_runtime;
7853
Paul Turnerab84d312011-07-21 09:43:28 -07007854 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007855}
7856
Li Zefan25cc7da2013-03-05 16:07:33 +08007857static long sched_group_rt_period(struct task_group *tg)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007858{
7859 u64 rt_period_us;
7860
7861 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
7862 do_div(rt_period_us, NSEC_PER_USEC);
7863 return rt_period_us;
7864}
Dario Faggioli332ac172013-11-07 14:43:45 +01007865#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007866
Dario Faggioli332ac172013-11-07 14:43:45 +01007867#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007868static int sched_rt_global_constraints(void)
7869{
7870 int ret = 0;
7871
7872 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007873 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02007874 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02007875 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007876 mutex_unlock(&rt_constraints_mutex);
7877
7878 return ret;
7879}
Dhaval Giani54e99122009-02-27 15:13:54 +05307880
Li Zefan25cc7da2013-03-05 16:07:33 +08007881static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
Dhaval Giani54e99122009-02-27 15:13:54 +05307882{
7883 /* Don't accept realtime tasks when there is no way for them to run */
7884 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
7885 return 0;
7886
7887 return 1;
7888}
7889
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007890#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007891static int sched_rt_global_constraints(void)
7892{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007893 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01007894 int i, ret = 0;
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07007895
Thomas Gleixner0986b112009-11-17 15:32:06 +01007896 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007897 for_each_possible_cpu(i) {
7898 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
7899
Thomas Gleixner0986b112009-11-17 15:32:06 +01007900 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007901 rt_rq->rt_runtime = global_rt_runtime();
Thomas Gleixner0986b112009-11-17 15:32:06 +01007902 raw_spin_unlock(&rt_rq->rt_runtime_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007903 }
Thomas Gleixner0986b112009-11-17 15:32:06 +01007904 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007905
Dario Faggioli332ac172013-11-07 14:43:45 +01007906 return ret;
7907}
7908#endif /* CONFIG_RT_GROUP_SCHED */
7909
Wanpeng Lia1963b82015-03-17 19:15:31 +08007910static int sched_dl_global_validate(void)
Dario Faggioli332ac172013-11-07 14:43:45 +01007911{
Peter Zijlstra17248132013-12-17 12:44:49 +01007912 u64 runtime = global_rt_runtime();
7913 u64 period = global_rt_period();
Dario Faggioli332ac172013-11-07 14:43:45 +01007914 u64 new_bw = to_ratio(period, runtime);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007915 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01007916 int cpu, ret = 0;
Juri Lelli49516342014-02-11 09:24:27 +01007917 unsigned long flags;
Dario Faggioli332ac172013-11-07 14:43:45 +01007918
7919 /*
7920 * Here we want to check the bandwidth not being set to some
7921 * value smaller than the currently allocated bandwidth in
7922 * any of the root_domains.
7923 *
7924 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
7925 * cycling on root_domains... Discussion on different/better
7926 * solutions is welcome!
7927 */
Peter Zijlstra17248132013-12-17 12:44:49 +01007928 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007929 rcu_read_lock_sched();
7930 dl_b = dl_bw_of(cpu);
Dario Faggioli332ac172013-11-07 14:43:45 +01007931
Juri Lelli49516342014-02-11 09:24:27 +01007932 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007933 if (new_bw < dl_b->total_bw)
7934 ret = -EBUSY;
Juri Lelli49516342014-02-11 09:24:27 +01007935 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007936
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007937 rcu_read_unlock_sched();
7938
Peter Zijlstra17248132013-12-17 12:44:49 +01007939 if (ret)
7940 break;
Dario Faggioli332ac172013-11-07 14:43:45 +01007941 }
7942
Peter Zijlstra17248132013-12-17 12:44:49 +01007943 return ret;
7944}
7945
7946static void sched_dl_do_global(void)
7947{
7948 u64 new_bw = -1;
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007949 struct dl_bw *dl_b;
Peter Zijlstra17248132013-12-17 12:44:49 +01007950 int cpu;
Juri Lelli49516342014-02-11 09:24:27 +01007951 unsigned long flags;
Peter Zijlstra17248132013-12-17 12:44:49 +01007952
7953 def_dl_bandwidth.dl_period = global_rt_period();
7954 def_dl_bandwidth.dl_runtime = global_rt_runtime();
7955
7956 if (global_rt_runtime() != RUNTIME_INF)
7957 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
7958
7959 /*
7960 * FIXME: As above...
7961 */
7962 for_each_possible_cpu(cpu) {
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007963 rcu_read_lock_sched();
7964 dl_b = dl_bw_of(cpu);
Peter Zijlstra17248132013-12-17 12:44:49 +01007965
Juri Lelli49516342014-02-11 09:24:27 +01007966 raw_spin_lock_irqsave(&dl_b->lock, flags);
Peter Zijlstra17248132013-12-17 12:44:49 +01007967 dl_b->bw = new_bw;
Juri Lelli49516342014-02-11 09:24:27 +01007968 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
Kirill Tkhaif10e00f2014-09-30 12:23:37 +04007969
7970 rcu_read_unlock_sched();
Peter Zijlstra17248132013-12-17 12:44:49 +01007971 }
7972}
7973
7974static int sched_rt_global_validate(void)
7975{
7976 if (sysctl_sched_rt_period <= 0)
7977 return -EINVAL;
7978
Juri Lellie9e7cb32014-02-11 09:24:26 +01007979 if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
7980 (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
Peter Zijlstra17248132013-12-17 12:44:49 +01007981 return -EINVAL;
7982
Dario Faggioli332ac172013-11-07 14:43:45 +01007983 return 0;
7984}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007985
Peter Zijlstra17248132013-12-17 12:44:49 +01007986static void sched_rt_do_global(void)
7987{
7988 def_rt_bandwidth.rt_runtime = global_rt_runtime();
7989 def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
7990}
7991
7992int sched_rt_handler(struct ctl_table *table, int write,
7993 void __user *buffer, size_t *lenp,
7994 loff_t *ppos)
7995{
7996 int old_period, old_runtime;
7997 static DEFINE_MUTEX(mutex);
7998 int ret;
7999
8000 mutex_lock(&mutex);
8001 old_period = sysctl_sched_rt_period;
8002 old_runtime = sysctl_sched_rt_runtime;
8003
8004 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8005
8006 if (!ret && write) {
8007 ret = sched_rt_global_validate();
8008 if (ret)
8009 goto undo;
8010
Wanpeng Lia1963b82015-03-17 19:15:31 +08008011 ret = sched_dl_global_validate();
Peter Zijlstra17248132013-12-17 12:44:49 +01008012 if (ret)
8013 goto undo;
8014
Wanpeng Lia1963b82015-03-17 19:15:31 +08008015 ret = sched_rt_global_constraints();
Peter Zijlstra17248132013-12-17 12:44:49 +01008016 if (ret)
8017 goto undo;
8018
8019 sched_rt_do_global();
8020 sched_dl_do_global();
8021 }
8022 if (0) {
8023undo:
8024 sysctl_sched_rt_period = old_period;
8025 sysctl_sched_rt_runtime = old_runtime;
8026 }
8027 mutex_unlock(&mutex);
8028
8029 return ret;
8030}
8031
Clark Williamsce0dbbb2013-02-07 09:47:04 -06008032int sched_rr_handler(struct ctl_table *table, int write,
8033 void __user *buffer, size_t *lenp,
8034 loff_t *ppos)
8035{
8036 int ret;
8037 static DEFINE_MUTEX(mutex);
8038
8039 mutex_lock(&mutex);
8040 ret = proc_dointvec(table, write, buffer, lenp, ppos);
8041 /* make sure that internally we keep jiffies */
8042 /* also, writing zero resets timeslice to default */
8043 if (!ret && write) {
8044 sched_rr_timeslice = sched_rr_timeslice <= 0 ?
8045 RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice);
8046 }
8047 mutex_unlock(&mutex);
8048 return ret;
8049}
8050
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008051#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008052
Tejun Heoa7c6d552013-08-08 20:11:23 -04008053static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008054{
Tejun Heoa7c6d552013-08-08 20:11:23 -04008055 return css ? container_of(css, struct task_group, css) : NULL;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008056}
8057
Tejun Heoeb954192013-08-08 20:11:23 -04008058static struct cgroup_subsys_state *
8059cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008060{
Tejun Heoeb954192013-08-08 20:11:23 -04008061 struct task_group *parent = css_tg(parent_css);
8062 struct task_group *tg;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008063
Tejun Heoeb954192013-08-08 20:11:23 -04008064 if (!parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008065 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08008066 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008067 }
8068
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008069 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008070 if (IS_ERR(tg))
8071 return ERR_PTR(-ENOMEM);
8072
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008073 return &tg->css;
8074}
8075
Tejun Heoeb954192013-08-08 20:11:23 -04008076static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08008077{
Tejun Heoeb954192013-08-08 20:11:23 -04008078 struct task_group *tg = css_tg(css);
Tejun Heo5c9d5352014-05-16 13:22:48 -04008079 struct task_group *parent = css_tg(css->parent);
Li Zefanace783b2013-01-24 14:30:48 +08008080
Tejun Heo63876982013-08-08 20:11:23 -04008081 if (parent)
8082 sched_online_group(tg, parent);
Li Zefanace783b2013-01-24 14:30:48 +08008083 return 0;
8084}
8085
Tejun Heoeb954192013-08-08 20:11:23 -04008086static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008087{
Tejun Heoeb954192013-08-08 20:11:23 -04008088 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008089
8090 sched_destroy_group(tg);
8091}
8092
Tejun Heoeb954192013-08-08 20:11:23 -04008093static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08008094{
Tejun Heoeb954192013-08-08 20:11:23 -04008095 struct task_group *tg = css_tg(css);
Li Zefanace783b2013-01-24 14:30:48 +08008096
8097 sched_offline_group(tg);
8098}
8099
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008100static void cpu_cgroup_fork(struct task_struct *task)
8101{
8102 sched_move_task(task);
8103}
8104
Tejun Heoeb954192013-08-08 20:11:23 -04008105static int cpu_cgroup_can_attach(struct cgroup_subsys_state *css,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008106 struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008107{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008108 struct task_struct *task;
8109
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008110 cgroup_taskset_for_each(task, tset) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008111#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heoeb954192013-08-08 20:11:23 -04008112 if (!sched_rt_can_attach(css_tg(css), task))
Tejun Heobb9d97b2011-12-12 18:12:21 -08008113 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008114#else
Tejun Heobb9d97b2011-12-12 18:12:21 -08008115 /* We don't support RT-tasks being in separate groups */
8116 if (task->sched_class != &fair_sched_class)
8117 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008118#endif
Tejun Heobb9d97b2011-12-12 18:12:21 -08008119 }
Ben Blumbe367d02009-09-23 15:56:31 -07008120 return 0;
8121}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008122
Tejun Heoeb954192013-08-08 20:11:23 -04008123static void cpu_cgroup_attach(struct cgroup_subsys_state *css,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008124 struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008125{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008126 struct task_struct *task;
8127
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008128 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008129 sched_move_task(task);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008130}
8131
Tejun Heoeb954192013-08-08 20:11:23 -04008132static void cpu_cgroup_exit(struct cgroup_subsys_state *css,
8133 struct cgroup_subsys_state *old_css,
8134 struct task_struct *task)
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01008135{
8136 /*
8137 * cgroup_exit() is called in the copy_process() failure path.
8138 * Ignore this case since the task hasn't ran yet, this avoids
8139 * trying to poke a half freed task state from generic code.
8140 */
8141 if (!(task->flags & PF_EXITING))
8142 return;
8143
8144 sched_move_task(task);
8145}
8146
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008147#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008148static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
8149 struct cftype *cftype, u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008150{
Tejun Heo182446d2013-08-08 20:11:24 -04008151 return sched_group_set_shares(css_tg(css), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008152}
8153
Tejun Heo182446d2013-08-08 20:11:24 -04008154static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
8155 struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008156{
Tejun Heo182446d2013-08-08 20:11:24 -04008157 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008158
Nikhil Raoc8b28112011-05-18 14:37:48 -07008159 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008160}
Paul Turnerab84d312011-07-21 09:43:28 -07008161
8162#ifdef CONFIG_CFS_BANDWIDTH
Paul Turnera790de92011-07-21 09:43:29 -07008163static DEFINE_MUTEX(cfs_constraints_mutex);
8164
Paul Turnerab84d312011-07-21 09:43:28 -07008165const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
8166const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
8167
Paul Turnera790de92011-07-21 09:43:29 -07008168static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
8169
Paul Turnerab84d312011-07-21 09:43:28 -07008170static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
8171{
Paul Turner56f570e2011-11-07 20:26:33 -08008172 int i, ret = 0, runtime_enabled, runtime_was_enabled;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008173 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnerab84d312011-07-21 09:43:28 -07008174
8175 if (tg == &root_task_group)
8176 return -EINVAL;
8177
8178 /*
8179 * Ensure we have at some amount of bandwidth every period. This is
8180 * to prevent reaching a state of large arrears when throttled via
8181 * entity_tick() resulting in prolonged exit starvation.
8182 */
8183 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
8184 return -EINVAL;
8185
8186 /*
8187 * Likewise, bound things on the otherside by preventing insane quota
8188 * periods. This also allows us to normalize in computing quota
8189 * feasibility.
8190 */
8191 if (period > max_cfs_quota_period)
8192 return -EINVAL;
8193
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008194 /*
8195 * Prevent race between setting of cfs_rq->runtime_enabled and
8196 * unthrottle_offline_cfs_rqs().
8197 */
8198 get_online_cpus();
Paul Turnera790de92011-07-21 09:43:29 -07008199 mutex_lock(&cfs_constraints_mutex);
8200 ret = __cfs_schedulable(tg, period, quota);
8201 if (ret)
8202 goto out_unlock;
8203
Paul Turner58088ad2011-07-21 09:43:31 -07008204 runtime_enabled = quota != RUNTIME_INF;
Paul Turner56f570e2011-11-07 20:26:33 -08008205 runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
Ben Segall1ee14e62013-10-16 11:16:12 -07008206 /*
8207 * If we need to toggle cfs_bandwidth_used, off->on must occur
8208 * before making related changes, and on->off must occur afterwards
8209 */
8210 if (runtime_enabled && !runtime_was_enabled)
8211 cfs_bandwidth_usage_inc();
Paul Turnerab84d312011-07-21 09:43:28 -07008212 raw_spin_lock_irq(&cfs_b->lock);
8213 cfs_b->period = ns_to_ktime(period);
8214 cfs_b->quota = quota;
Paul Turner58088ad2011-07-21 09:43:31 -07008215
Paul Turnera9cf55b2011-07-21 09:43:32 -07008216 __refill_cfs_bandwidth_runtime(cfs_b);
Paul Turner58088ad2011-07-21 09:43:31 -07008217 /* restart the period timer (if active) to handle new period expiry */
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02008218 if (runtime_enabled)
8219 start_cfs_bandwidth(cfs_b);
Paul Turnerab84d312011-07-21 09:43:28 -07008220 raw_spin_unlock_irq(&cfs_b->lock);
8221
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008222 for_each_online_cpu(i) {
Paul Turnerab84d312011-07-21 09:43:28 -07008223 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
Peter Zijlstra029632f2011-10-25 10:00:11 +02008224 struct rq *rq = cfs_rq->rq;
Paul Turnerab84d312011-07-21 09:43:28 -07008225
8226 raw_spin_lock_irq(&rq->lock);
Paul Turner58088ad2011-07-21 09:43:31 -07008227 cfs_rq->runtime_enabled = runtime_enabled;
Paul Turnerab84d312011-07-21 09:43:28 -07008228 cfs_rq->runtime_remaining = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07008229
Peter Zijlstra029632f2011-10-25 10:00:11 +02008230 if (cfs_rq->throttled)
Paul Turner671fd9d2011-07-21 09:43:34 -07008231 unthrottle_cfs_rq(cfs_rq);
Paul Turnerab84d312011-07-21 09:43:28 -07008232 raw_spin_unlock_irq(&rq->lock);
8233 }
Ben Segall1ee14e62013-10-16 11:16:12 -07008234 if (runtime_was_enabled && !runtime_enabled)
8235 cfs_bandwidth_usage_dec();
Paul Turnera790de92011-07-21 09:43:29 -07008236out_unlock:
8237 mutex_unlock(&cfs_constraints_mutex);
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04008238 put_online_cpus();
Paul Turnerab84d312011-07-21 09:43:28 -07008239
Paul Turnera790de92011-07-21 09:43:29 -07008240 return ret;
Paul Turnerab84d312011-07-21 09:43:28 -07008241}
8242
8243int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
8244{
8245 u64 quota, period;
8246
Peter Zijlstra029632f2011-10-25 10:00:11 +02008247 period = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008248 if (cfs_quota_us < 0)
8249 quota = RUNTIME_INF;
8250 else
8251 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
8252
8253 return tg_set_cfs_bandwidth(tg, period, quota);
8254}
8255
8256long tg_get_cfs_quota(struct task_group *tg)
8257{
8258 u64 quota_us;
8259
Peter Zijlstra029632f2011-10-25 10:00:11 +02008260 if (tg->cfs_bandwidth.quota == RUNTIME_INF)
Paul Turnerab84d312011-07-21 09:43:28 -07008261 return -1;
8262
Peter Zijlstra029632f2011-10-25 10:00:11 +02008263 quota_us = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008264 do_div(quota_us, NSEC_PER_USEC);
8265
8266 return quota_us;
8267}
8268
8269int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
8270{
8271 u64 quota, period;
8272
8273 period = (u64)cfs_period_us * NSEC_PER_USEC;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008274 quota = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07008275
Paul Turnerab84d312011-07-21 09:43:28 -07008276 return tg_set_cfs_bandwidth(tg, period, quota);
8277}
8278
8279long tg_get_cfs_period(struct task_group *tg)
8280{
8281 u64 cfs_period_us;
8282
Peter Zijlstra029632f2011-10-25 10:00:11 +02008283 cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07008284 do_div(cfs_period_us, NSEC_PER_USEC);
8285
8286 return cfs_period_us;
8287}
8288
Tejun Heo182446d2013-08-08 20:11:24 -04008289static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
8290 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008291{
Tejun Heo182446d2013-08-08 20:11:24 -04008292 return tg_get_cfs_quota(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008293}
8294
Tejun Heo182446d2013-08-08 20:11:24 -04008295static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
8296 struct cftype *cftype, s64 cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008297{
Tejun Heo182446d2013-08-08 20:11:24 -04008298 return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008299}
8300
Tejun Heo182446d2013-08-08 20:11:24 -04008301static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
8302 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07008303{
Tejun Heo182446d2013-08-08 20:11:24 -04008304 return tg_get_cfs_period(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07008305}
8306
Tejun Heo182446d2013-08-08 20:11:24 -04008307static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
8308 struct cftype *cftype, u64 cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008309{
Tejun Heo182446d2013-08-08 20:11:24 -04008310 return tg_set_cfs_period(css_tg(css), cfs_period_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008311}
8312
Paul Turnera790de92011-07-21 09:43:29 -07008313struct cfs_schedulable_data {
8314 struct task_group *tg;
8315 u64 period, quota;
8316};
8317
8318/*
8319 * normalize group quota/period to be quota/max_period
8320 * note: units are usecs
8321 */
8322static u64 normalize_cfs_quota(struct task_group *tg,
8323 struct cfs_schedulable_data *d)
8324{
8325 u64 quota, period;
8326
8327 if (tg == d->tg) {
8328 period = d->period;
8329 quota = d->quota;
8330 } else {
8331 period = tg_get_cfs_period(tg);
8332 quota = tg_get_cfs_quota(tg);
8333 }
8334
8335 /* note: these should typically be equivalent */
8336 if (quota == RUNTIME_INF || quota == -1)
8337 return RUNTIME_INF;
8338
8339 return to_ratio(period, quota);
8340}
8341
8342static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
8343{
8344 struct cfs_schedulable_data *d = data;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008345 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008346 s64 quota = 0, parent_quota = -1;
8347
8348 if (!tg->parent) {
8349 quota = RUNTIME_INF;
8350 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02008351 struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008352
8353 quota = normalize_cfs_quota(tg, d);
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008354 parent_quota = parent_b->hierarchical_quota;
Paul Turnera790de92011-07-21 09:43:29 -07008355
8356 /*
8357 * ensure max(child_quota) <= parent_quota, inherit when no
8358 * limit is set
8359 */
8360 if (quota == RUNTIME_INF)
8361 quota = parent_quota;
8362 else if (parent_quota != RUNTIME_INF && quota > parent_quota)
8363 return -EINVAL;
8364 }
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008365 cfs_b->hierarchical_quota = quota;
Paul Turnera790de92011-07-21 09:43:29 -07008366
8367 return 0;
8368}
8369
8370static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
8371{
Paul Turner82774342011-07-21 09:43:35 -07008372 int ret;
Paul Turnera790de92011-07-21 09:43:29 -07008373 struct cfs_schedulable_data data = {
8374 .tg = tg,
8375 .period = period,
8376 .quota = quota,
8377 };
8378
8379 if (quota != RUNTIME_INF) {
8380 do_div(data.period, NSEC_PER_USEC);
8381 do_div(data.quota, NSEC_PER_USEC);
8382 }
8383
Paul Turner82774342011-07-21 09:43:35 -07008384 rcu_read_lock();
8385 ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
8386 rcu_read_unlock();
8387
8388 return ret;
Paul Turnera790de92011-07-21 09:43:29 -07008389}
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008390
Tejun Heo2da8ca82013-12-05 12:28:04 -05008391static int cpu_stats_show(struct seq_file *sf, void *v)
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008392{
Tejun Heo2da8ca82013-12-05 12:28:04 -05008393 struct task_group *tg = css_tg(seq_css(sf));
Peter Zijlstra029632f2011-10-25 10:00:11 +02008394 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008395
Tejun Heo44ffc752013-12-05 12:28:01 -05008396 seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
8397 seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
8398 seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008399
8400 return 0;
8401}
Paul Turnerab84d312011-07-21 09:43:28 -07008402#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008403#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008404
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008405#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008406static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
8407 struct cftype *cft, s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008408{
Tejun Heo182446d2013-08-08 20:11:24 -04008409 return sched_group_set_rt_runtime(css_tg(css), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008410}
8411
Tejun Heo182446d2013-08-08 20:11:24 -04008412static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
8413 struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008414{
Tejun Heo182446d2013-08-08 20:11:24 -04008415 return sched_group_rt_runtime(css_tg(css));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008416}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008417
Tejun Heo182446d2013-08-08 20:11:24 -04008418static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
8419 struct cftype *cftype, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008420{
Tejun Heo182446d2013-08-08 20:11:24 -04008421 return sched_group_set_rt_period(css_tg(css), rt_period_us);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008422}
8423
Tejun Heo182446d2013-08-08 20:11:24 -04008424static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
8425 struct cftype *cft)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008426{
Tejun Heo182446d2013-08-08 20:11:24 -04008427 return sched_group_rt_period(css_tg(css));
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008428}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008429#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008430
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008431static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008432#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008433 {
8434 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008435 .read_u64 = cpu_shares_read_u64,
8436 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008437 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008438#endif
Paul Turnerab84d312011-07-21 09:43:28 -07008439#ifdef CONFIG_CFS_BANDWIDTH
8440 {
8441 .name = "cfs_quota_us",
8442 .read_s64 = cpu_cfs_quota_read_s64,
8443 .write_s64 = cpu_cfs_quota_write_s64,
8444 },
8445 {
8446 .name = "cfs_period_us",
8447 .read_u64 = cpu_cfs_period_read_u64,
8448 .write_u64 = cpu_cfs_period_write_u64,
8449 },
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008450 {
8451 .name = "stat",
Tejun Heo2da8ca82013-12-05 12:28:04 -05008452 .seq_show = cpu_stats_show,
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008453 },
Paul Turnerab84d312011-07-21 09:43:28 -07008454#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008455#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008456 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008457 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008458 .read_s64 = cpu_rt_runtime_read,
8459 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008460 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008461 {
8462 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008463 .read_u64 = cpu_rt_period_read_uint,
8464 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008465 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008466#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -07008467 { } /* terminate */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008468};
8469
Tejun Heo073219e2014-02-08 10:36:58 -05008470struct cgroup_subsys cpu_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008471 .css_alloc = cpu_cgroup_css_alloc,
8472 .css_free = cpu_cgroup_css_free,
Li Zefanace783b2013-01-24 14:30:48 +08008473 .css_online = cpu_cgroup_css_online,
8474 .css_offline = cpu_cgroup_css_offline,
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008475 .fork = cpu_cgroup_fork,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008476 .can_attach = cpu_cgroup_can_attach,
8477 .attach = cpu_cgroup_attach,
Peter Zijlstra068c5cc2011-01-19 12:26:11 +01008478 .exit = cpu_cgroup_exit,
Tejun Heo55779642014-07-15 11:05:09 -04008479 .legacy_cftypes = cpu_files,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008480 .early_init = 1,
8481};
8482
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008483#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008484
Paul E. McKenneyb637a322012-09-19 16:58:38 -07008485void dump_cpu_task(int cpu)
8486{
8487 pr_info("Task dump for CPU %d:\n", cpu);
8488 sched_show_task(cpu_curr(cpu));
8489}