Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * kernel/sched.c |
| 3 | * |
| 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 |
| 19 | */ |
| 20 | |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/nmi.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <asm/uaccess.h> |
| 26 | #include <linux/highmem.h> |
| 27 | #include <linux/smp_lock.h> |
| 28 | #include <asm/mmu_context.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/completion.h> |
| 31 | #include <linux/kernel_stat.h> |
| 32 | #include <linux/security.h> |
| 33 | #include <linux/notifier.h> |
| 34 | #include <linux/profile.h> |
| 35 | #include <linux/suspend.h> |
| 36 | #include <linux/blkdev.h> |
| 37 | #include <linux/delay.h> |
| 38 | #include <linux/smp.h> |
| 39 | #include <linux/threads.h> |
| 40 | #include <linux/timer.h> |
| 41 | #include <linux/rcupdate.h> |
| 42 | #include <linux/cpu.h> |
| 43 | #include <linux/cpuset.h> |
| 44 | #include <linux/percpu.h> |
| 45 | #include <linux/kthread.h> |
| 46 | #include <linux/seq_file.h> |
| 47 | #include <linux/syscalls.h> |
| 48 | #include <linux/times.h> |
| 49 | #include <linux/acct.h> |
| 50 | #include <asm/tlb.h> |
| 51 | |
| 52 | #include <asm/unistd.h> |
| 53 | |
| 54 | /* |
| 55 | * Convert user-nice values [ -20 ... 0 ... 19 ] |
| 56 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], |
| 57 | * and back. |
| 58 | */ |
| 59 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) |
| 60 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) |
| 61 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) |
| 62 | |
| 63 | /* |
| 64 | * 'User priority' is the nice value converted to something we |
| 65 | * can work with better when scaling various scheduler parameters, |
| 66 | * it's a [ 0 ... 39 ] range. |
| 67 | */ |
| 68 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) |
| 69 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) |
| 70 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) |
| 71 | |
| 72 | /* |
| 73 | * Some helpers for converting nanosecond timing to jiffy resolution |
| 74 | */ |
| 75 | #define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ)) |
| 76 | #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ)) |
| 77 | |
| 78 | /* |
| 79 | * These are the 'tuning knobs' of the scheduler: |
| 80 | * |
| 81 | * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger), |
| 82 | * default timeslice is 100 msecs, maximum timeslice is 800 msecs. |
| 83 | * Timeslices get refilled after they expire. |
| 84 | */ |
| 85 | #define MIN_TIMESLICE max(5 * HZ / 1000, 1) |
| 86 | #define DEF_TIMESLICE (100 * HZ / 1000) |
| 87 | #define ON_RUNQUEUE_WEIGHT 30 |
| 88 | #define CHILD_PENALTY 95 |
| 89 | #define PARENT_PENALTY 100 |
| 90 | #define EXIT_WEIGHT 3 |
| 91 | #define PRIO_BONUS_RATIO 25 |
| 92 | #define MAX_BONUS (MAX_USER_PRIO * PRIO_BONUS_RATIO / 100) |
| 93 | #define INTERACTIVE_DELTA 2 |
| 94 | #define MAX_SLEEP_AVG (DEF_TIMESLICE * MAX_BONUS) |
| 95 | #define STARVATION_LIMIT (MAX_SLEEP_AVG) |
| 96 | #define NS_MAX_SLEEP_AVG (JIFFIES_TO_NS(MAX_SLEEP_AVG)) |
| 97 | |
| 98 | /* |
| 99 | * If a task is 'interactive' then we reinsert it in the active |
| 100 | * array after it has expired its current timeslice. (it will not |
| 101 | * continue to run immediately, it will still roundrobin with |
| 102 | * other interactive tasks.) |
| 103 | * |
| 104 | * This part scales the interactivity limit depending on niceness. |
| 105 | * |
| 106 | * We scale it linearly, offset by the INTERACTIVE_DELTA delta. |
| 107 | * Here are a few examples of different nice levels: |
| 108 | * |
| 109 | * TASK_INTERACTIVE(-20): [1,1,1,1,1,1,1,1,1,0,0] |
| 110 | * TASK_INTERACTIVE(-10): [1,1,1,1,1,1,1,0,0,0,0] |
| 111 | * TASK_INTERACTIVE( 0): [1,1,1,1,0,0,0,0,0,0,0] |
| 112 | * TASK_INTERACTIVE( 10): [1,1,0,0,0,0,0,0,0,0,0] |
| 113 | * TASK_INTERACTIVE( 19): [0,0,0,0,0,0,0,0,0,0,0] |
| 114 | * |
| 115 | * (the X axis represents the possible -5 ... 0 ... +5 dynamic |
| 116 | * priority range a task can explore, a value of '1' means the |
| 117 | * task is rated interactive.) |
| 118 | * |
| 119 | * Ie. nice +19 tasks can never get 'interactive' enough to be |
| 120 | * reinserted into the active array. And only heavily CPU-hog nice -20 |
| 121 | * tasks will be expired. Default nice 0 tasks are somewhere between, |
| 122 | * it takes some effort for them to get interactive, but it's not |
| 123 | * too hard. |
| 124 | */ |
| 125 | |
| 126 | #define CURRENT_BONUS(p) \ |
| 127 | (NS_TO_JIFFIES((p)->sleep_avg) * MAX_BONUS / \ |
| 128 | MAX_SLEEP_AVG) |
| 129 | |
| 130 | #define GRANULARITY (10 * HZ / 1000 ? : 1) |
| 131 | |
| 132 | #ifdef CONFIG_SMP |
| 133 | #define TIMESLICE_GRANULARITY(p) (GRANULARITY * \ |
| 134 | (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)) * \ |
| 135 | num_online_cpus()) |
| 136 | #else |
| 137 | #define TIMESLICE_GRANULARITY(p) (GRANULARITY * \ |
| 138 | (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1))) |
| 139 | #endif |
| 140 | |
| 141 | #define SCALE(v1,v1_max,v2_max) \ |
| 142 | (v1) * (v2_max) / (v1_max) |
| 143 | |
| 144 | #define DELTA(p) \ |
| 145 | (SCALE(TASK_NICE(p), 40, MAX_BONUS) + INTERACTIVE_DELTA) |
| 146 | |
| 147 | #define TASK_INTERACTIVE(p) \ |
| 148 | ((p)->prio <= (p)->static_prio - DELTA(p)) |
| 149 | |
| 150 | #define INTERACTIVE_SLEEP(p) \ |
| 151 | (JIFFIES_TO_NS(MAX_SLEEP_AVG * \ |
| 152 | (MAX_BONUS / 2 + DELTA((p)) + 1) / MAX_BONUS - 1)) |
| 153 | |
| 154 | #define TASK_PREEMPTS_CURR(p, rq) \ |
| 155 | ((p)->prio < (rq)->curr->prio) |
| 156 | |
| 157 | /* |
| 158 | * task_timeslice() scales user-nice values [ -20 ... 0 ... 19 ] |
| 159 | * to time slice values: [800ms ... 100ms ... 5ms] |
| 160 | * |
| 161 | * The higher a thread's priority, the bigger timeslices |
| 162 | * it gets during one round of execution. But even the lowest |
| 163 | * priority thread gets MIN_TIMESLICE worth of execution time. |
| 164 | */ |
| 165 | |
| 166 | #define SCALE_PRIO(x, prio) \ |
| 167 | max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO/2), MIN_TIMESLICE) |
| 168 | |
| 169 | static inline unsigned int task_timeslice(task_t *p) |
| 170 | { |
| 171 | if (p->static_prio < NICE_TO_PRIO(0)) |
| 172 | return SCALE_PRIO(DEF_TIMESLICE*4, p->static_prio); |
| 173 | else |
| 174 | return SCALE_PRIO(DEF_TIMESLICE, p->static_prio); |
| 175 | } |
| 176 | #define task_hot(p, now, sd) ((long long) ((now) - (p)->last_ran) \ |
| 177 | < (long long) (sd)->cache_hot_time) |
| 178 | |
| 179 | /* |
| 180 | * These are the runqueue data structures: |
| 181 | */ |
| 182 | |
| 183 | #define BITMAP_SIZE ((((MAX_PRIO+1+7)/8)+sizeof(long)-1)/sizeof(long)) |
| 184 | |
| 185 | typedef struct runqueue runqueue_t; |
| 186 | |
| 187 | struct prio_array { |
| 188 | unsigned int nr_active; |
| 189 | unsigned long bitmap[BITMAP_SIZE]; |
| 190 | struct list_head queue[MAX_PRIO]; |
| 191 | }; |
| 192 | |
| 193 | /* |
| 194 | * This is the main, per-CPU runqueue data structure. |
| 195 | * |
| 196 | * Locking rule: those places that want to lock multiple runqueues |
| 197 | * (such as the load balancing or the thread migration code), lock |
| 198 | * acquire operations must be ordered by ascending &runqueue. |
| 199 | */ |
| 200 | struct runqueue { |
| 201 | spinlock_t lock; |
| 202 | |
| 203 | /* |
| 204 | * nr_running and cpu_load should be in the same cacheline because |
| 205 | * remote CPUs use both these fields when doing load calculation. |
| 206 | */ |
| 207 | unsigned long nr_running; |
| 208 | #ifdef CONFIG_SMP |
| 209 | unsigned long cpu_load; |
| 210 | #endif |
| 211 | unsigned long long nr_switches; |
| 212 | |
| 213 | /* |
| 214 | * This is part of a global counter where only the total sum |
| 215 | * over all CPUs matters. A task can increase this counter on |
| 216 | * one CPU and if it got migrated afterwards it may decrease |
| 217 | * it on another CPU. Always updated under the runqueue lock: |
| 218 | */ |
| 219 | unsigned long nr_uninterruptible; |
| 220 | |
| 221 | unsigned long expired_timestamp; |
| 222 | unsigned long long timestamp_last_tick; |
| 223 | task_t *curr, *idle; |
| 224 | struct mm_struct *prev_mm; |
| 225 | prio_array_t *active, *expired, arrays[2]; |
| 226 | int best_expired_prio; |
| 227 | atomic_t nr_iowait; |
| 228 | |
| 229 | #ifdef CONFIG_SMP |
| 230 | struct sched_domain *sd; |
| 231 | |
| 232 | /* For active balancing */ |
| 233 | int active_balance; |
| 234 | int push_cpu; |
| 235 | |
| 236 | task_t *migration_thread; |
| 237 | struct list_head migration_queue; |
| 238 | #endif |
| 239 | |
| 240 | #ifdef CONFIG_SCHEDSTATS |
| 241 | /* latency stats */ |
| 242 | struct sched_info rq_sched_info; |
| 243 | |
| 244 | /* sys_sched_yield() stats */ |
| 245 | unsigned long yld_exp_empty; |
| 246 | unsigned long yld_act_empty; |
| 247 | unsigned long yld_both_empty; |
| 248 | unsigned long yld_cnt; |
| 249 | |
| 250 | /* schedule() stats */ |
| 251 | unsigned long sched_switch; |
| 252 | unsigned long sched_cnt; |
| 253 | unsigned long sched_goidle; |
| 254 | |
| 255 | /* try_to_wake_up() stats */ |
| 256 | unsigned long ttwu_cnt; |
| 257 | unsigned long ttwu_local; |
| 258 | #endif |
| 259 | }; |
| 260 | |
| 261 | static DEFINE_PER_CPU(struct runqueue, runqueues); |
| 262 | |
| 263 | #define for_each_domain(cpu, domain) \ |
| 264 | for (domain = cpu_rq(cpu)->sd; domain; domain = domain->parent) |
| 265 | |
| 266 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) |
| 267 | #define this_rq() (&__get_cpu_var(runqueues)) |
| 268 | #define task_rq(p) cpu_rq(task_cpu(p)) |
| 269 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) |
| 270 | |
| 271 | /* |
| 272 | * Default context-switch locking: |
| 273 | */ |
| 274 | #ifndef prepare_arch_switch |
| 275 | # define prepare_arch_switch(rq, next) do { } while (0) |
| 276 | # define finish_arch_switch(rq, next) spin_unlock_irq(&(rq)->lock) |
| 277 | # define task_running(rq, p) ((rq)->curr == (p)) |
| 278 | #endif |
| 279 | |
| 280 | /* |
| 281 | * task_rq_lock - lock the runqueue a given task resides on and disable |
| 282 | * interrupts. Note the ordering: we can safely lookup the task_rq without |
| 283 | * explicitly disabling preemption. |
| 284 | */ |
| 285 | static inline runqueue_t *task_rq_lock(task_t *p, unsigned long *flags) |
| 286 | __acquires(rq->lock) |
| 287 | { |
| 288 | struct runqueue *rq; |
| 289 | |
| 290 | repeat_lock_task: |
| 291 | local_irq_save(*flags); |
| 292 | rq = task_rq(p); |
| 293 | spin_lock(&rq->lock); |
| 294 | if (unlikely(rq != task_rq(p))) { |
| 295 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 296 | goto repeat_lock_task; |
| 297 | } |
| 298 | return rq; |
| 299 | } |
| 300 | |
| 301 | static inline void task_rq_unlock(runqueue_t *rq, unsigned long *flags) |
| 302 | __releases(rq->lock) |
| 303 | { |
| 304 | spin_unlock_irqrestore(&rq->lock, *flags); |
| 305 | } |
| 306 | |
| 307 | #ifdef CONFIG_SCHEDSTATS |
| 308 | /* |
| 309 | * bump this up when changing the output format or the meaning of an existing |
| 310 | * format, so that tools can adapt (or abort) |
| 311 | */ |
| 312 | #define SCHEDSTAT_VERSION 11 |
| 313 | |
| 314 | static int show_schedstat(struct seq_file *seq, void *v) |
| 315 | { |
| 316 | int cpu; |
| 317 | |
| 318 | seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION); |
| 319 | seq_printf(seq, "timestamp %lu\n", jiffies); |
| 320 | for_each_online_cpu(cpu) { |
| 321 | runqueue_t *rq = cpu_rq(cpu); |
| 322 | #ifdef CONFIG_SMP |
| 323 | struct sched_domain *sd; |
| 324 | int dcnt = 0; |
| 325 | #endif |
| 326 | |
| 327 | /* runqueue-specific stats */ |
| 328 | seq_printf(seq, |
| 329 | "cpu%d %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", |
| 330 | cpu, rq->yld_both_empty, |
| 331 | rq->yld_act_empty, rq->yld_exp_empty, rq->yld_cnt, |
| 332 | rq->sched_switch, rq->sched_cnt, rq->sched_goidle, |
| 333 | rq->ttwu_cnt, rq->ttwu_local, |
| 334 | rq->rq_sched_info.cpu_time, |
| 335 | rq->rq_sched_info.run_delay, rq->rq_sched_info.pcnt); |
| 336 | |
| 337 | seq_printf(seq, "\n"); |
| 338 | |
| 339 | #ifdef CONFIG_SMP |
| 340 | /* domain-specific stats */ |
| 341 | for_each_domain(cpu, sd) { |
| 342 | enum idle_type itype; |
| 343 | char mask_str[NR_CPUS]; |
| 344 | |
| 345 | cpumask_scnprintf(mask_str, NR_CPUS, sd->span); |
| 346 | seq_printf(seq, "domain%d %s", dcnt++, mask_str); |
| 347 | for (itype = SCHED_IDLE; itype < MAX_IDLE_TYPES; |
| 348 | itype++) { |
| 349 | seq_printf(seq, " %lu %lu %lu %lu %lu %lu %lu %lu", |
| 350 | sd->lb_cnt[itype], |
| 351 | sd->lb_balanced[itype], |
| 352 | sd->lb_failed[itype], |
| 353 | sd->lb_imbalance[itype], |
| 354 | sd->lb_gained[itype], |
| 355 | sd->lb_hot_gained[itype], |
| 356 | sd->lb_nobusyq[itype], |
| 357 | sd->lb_nobusyg[itype]); |
| 358 | } |
| 359 | seq_printf(seq, " %lu %lu %lu %lu %lu %lu %lu %lu\n", |
| 360 | sd->alb_cnt, sd->alb_failed, sd->alb_pushed, |
| 361 | sd->sbe_pushed, sd->sbe_attempts, |
| 362 | sd->ttwu_wake_remote, sd->ttwu_move_affine, sd->ttwu_move_balance); |
| 363 | } |
| 364 | #endif |
| 365 | } |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int schedstat_open(struct inode *inode, struct file *file) |
| 370 | { |
| 371 | unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32); |
| 372 | char *buf = kmalloc(size, GFP_KERNEL); |
| 373 | struct seq_file *m; |
| 374 | int res; |
| 375 | |
| 376 | if (!buf) |
| 377 | return -ENOMEM; |
| 378 | res = single_open(file, show_schedstat, NULL); |
| 379 | if (!res) { |
| 380 | m = file->private_data; |
| 381 | m->buf = buf; |
| 382 | m->size = size; |
| 383 | } else |
| 384 | kfree(buf); |
| 385 | return res; |
| 386 | } |
| 387 | |
| 388 | struct file_operations proc_schedstat_operations = { |
| 389 | .open = schedstat_open, |
| 390 | .read = seq_read, |
| 391 | .llseek = seq_lseek, |
| 392 | .release = single_release, |
| 393 | }; |
| 394 | |
| 395 | # define schedstat_inc(rq, field) do { (rq)->field++; } while (0) |
| 396 | # define schedstat_add(rq, field, amt) do { (rq)->field += (amt); } while (0) |
| 397 | #else /* !CONFIG_SCHEDSTATS */ |
| 398 | # define schedstat_inc(rq, field) do { } while (0) |
| 399 | # define schedstat_add(rq, field, amt) do { } while (0) |
| 400 | #endif |
| 401 | |
| 402 | /* |
| 403 | * rq_lock - lock a given runqueue and disable interrupts. |
| 404 | */ |
| 405 | static inline runqueue_t *this_rq_lock(void) |
| 406 | __acquires(rq->lock) |
| 407 | { |
| 408 | runqueue_t *rq; |
| 409 | |
| 410 | local_irq_disable(); |
| 411 | rq = this_rq(); |
| 412 | spin_lock(&rq->lock); |
| 413 | |
| 414 | return rq; |
| 415 | } |
| 416 | |
| 417 | #ifdef CONFIG_SCHED_SMT |
| 418 | static int cpu_and_siblings_are_idle(int cpu) |
| 419 | { |
| 420 | int sib; |
| 421 | for_each_cpu_mask(sib, cpu_sibling_map[cpu]) { |
| 422 | if (idle_cpu(sib)) |
| 423 | continue; |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | return 1; |
| 428 | } |
| 429 | #else |
| 430 | #define cpu_and_siblings_are_idle(A) idle_cpu(A) |
| 431 | #endif |
| 432 | |
| 433 | #ifdef CONFIG_SCHEDSTATS |
| 434 | /* |
| 435 | * Called when a process is dequeued from the active array and given |
| 436 | * the cpu. We should note that with the exception of interactive |
| 437 | * tasks, the expired queue will become the active queue after the active |
| 438 | * queue is empty, without explicitly dequeuing and requeuing tasks in the |
| 439 | * expired queue. (Interactive tasks may be requeued directly to the |
| 440 | * active queue, thus delaying tasks in the expired queue from running; |
| 441 | * see scheduler_tick()). |
| 442 | * |
| 443 | * This function is only called from sched_info_arrive(), rather than |
| 444 | * dequeue_task(). Even though a task may be queued and dequeued multiple |
| 445 | * times as it is shuffled about, we're really interested in knowing how |
| 446 | * long it was from the *first* time it was queued to the time that it |
| 447 | * finally hit a cpu. |
| 448 | */ |
| 449 | static inline void sched_info_dequeued(task_t *t) |
| 450 | { |
| 451 | t->sched_info.last_queued = 0; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Called when a task finally hits the cpu. We can now calculate how |
| 456 | * long it was waiting to run. We also note when it began so that we |
| 457 | * can keep stats on how long its timeslice is. |
| 458 | */ |
| 459 | static inline void sched_info_arrive(task_t *t) |
| 460 | { |
| 461 | unsigned long now = jiffies, diff = 0; |
| 462 | struct runqueue *rq = task_rq(t); |
| 463 | |
| 464 | if (t->sched_info.last_queued) |
| 465 | diff = now - t->sched_info.last_queued; |
| 466 | sched_info_dequeued(t); |
| 467 | t->sched_info.run_delay += diff; |
| 468 | t->sched_info.last_arrival = now; |
| 469 | t->sched_info.pcnt++; |
| 470 | |
| 471 | if (!rq) |
| 472 | return; |
| 473 | |
| 474 | rq->rq_sched_info.run_delay += diff; |
| 475 | rq->rq_sched_info.pcnt++; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Called when a process is queued into either the active or expired |
| 480 | * array. The time is noted and later used to determine how long we |
| 481 | * had to wait for us to reach the cpu. Since the expired queue will |
| 482 | * become the active queue after active queue is empty, without dequeuing |
| 483 | * and requeuing any tasks, we are interested in queuing to either. It |
| 484 | * is unusual but not impossible for tasks to be dequeued and immediately |
| 485 | * requeued in the same or another array: this can happen in sched_yield(), |
| 486 | * set_user_nice(), and even load_balance() as it moves tasks from runqueue |
| 487 | * to runqueue. |
| 488 | * |
| 489 | * This function is only called from enqueue_task(), but also only updates |
| 490 | * the timestamp if it is already not set. It's assumed that |
| 491 | * sched_info_dequeued() will clear that stamp when appropriate. |
| 492 | */ |
| 493 | static inline void sched_info_queued(task_t *t) |
| 494 | { |
| 495 | if (!t->sched_info.last_queued) |
| 496 | t->sched_info.last_queued = jiffies; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Called when a process ceases being the active-running process, either |
| 501 | * voluntarily or involuntarily. Now we can calculate how long we ran. |
| 502 | */ |
| 503 | static inline void sched_info_depart(task_t *t) |
| 504 | { |
| 505 | struct runqueue *rq = task_rq(t); |
| 506 | unsigned long diff = jiffies - t->sched_info.last_arrival; |
| 507 | |
| 508 | t->sched_info.cpu_time += diff; |
| 509 | |
| 510 | if (rq) |
| 511 | rq->rq_sched_info.cpu_time += diff; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Called when tasks are switched involuntarily due, typically, to expiring |
| 516 | * their time slice. (This may also be called when switching to or from |
| 517 | * the idle task.) We are only called when prev != next. |
| 518 | */ |
| 519 | static inline void sched_info_switch(task_t *prev, task_t *next) |
| 520 | { |
| 521 | struct runqueue *rq = task_rq(prev); |
| 522 | |
| 523 | /* |
| 524 | * prev now departs the cpu. It's not interesting to record |
| 525 | * stats about how efficient we were at scheduling the idle |
| 526 | * process, however. |
| 527 | */ |
| 528 | if (prev != rq->idle) |
| 529 | sched_info_depart(prev); |
| 530 | |
| 531 | if (next != rq->idle) |
| 532 | sched_info_arrive(next); |
| 533 | } |
| 534 | #else |
| 535 | #define sched_info_queued(t) do { } while (0) |
| 536 | #define sched_info_switch(t, next) do { } while (0) |
| 537 | #endif /* CONFIG_SCHEDSTATS */ |
| 538 | |
| 539 | /* |
| 540 | * Adding/removing a task to/from a priority array: |
| 541 | */ |
| 542 | static void dequeue_task(struct task_struct *p, prio_array_t *array) |
| 543 | { |
| 544 | array->nr_active--; |
| 545 | list_del(&p->run_list); |
| 546 | if (list_empty(array->queue + p->prio)) |
| 547 | __clear_bit(p->prio, array->bitmap); |
| 548 | } |
| 549 | |
| 550 | static void enqueue_task(struct task_struct *p, prio_array_t *array) |
| 551 | { |
| 552 | sched_info_queued(p); |
| 553 | list_add_tail(&p->run_list, array->queue + p->prio); |
| 554 | __set_bit(p->prio, array->bitmap); |
| 555 | array->nr_active++; |
| 556 | p->array = array; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Put task to the end of the run list without the overhead of dequeue |
| 561 | * followed by enqueue. |
| 562 | */ |
| 563 | static void requeue_task(struct task_struct *p, prio_array_t *array) |
| 564 | { |
| 565 | list_move_tail(&p->run_list, array->queue + p->prio); |
| 566 | } |
| 567 | |
| 568 | static inline void enqueue_task_head(struct task_struct *p, prio_array_t *array) |
| 569 | { |
| 570 | list_add(&p->run_list, array->queue + p->prio); |
| 571 | __set_bit(p->prio, array->bitmap); |
| 572 | array->nr_active++; |
| 573 | p->array = array; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * effective_prio - return the priority that is based on the static |
| 578 | * priority but is modified by bonuses/penalties. |
| 579 | * |
| 580 | * We scale the actual sleep average [0 .... MAX_SLEEP_AVG] |
| 581 | * into the -5 ... 0 ... +5 bonus/penalty range. |
| 582 | * |
| 583 | * We use 25% of the full 0...39 priority range so that: |
| 584 | * |
| 585 | * 1) nice +19 interactive tasks do not preempt nice 0 CPU hogs. |
| 586 | * 2) nice -20 CPU hogs do not get preempted by nice 0 tasks. |
| 587 | * |
| 588 | * Both properties are important to certain workloads. |
| 589 | */ |
| 590 | static int effective_prio(task_t *p) |
| 591 | { |
| 592 | int bonus, prio; |
| 593 | |
| 594 | if (rt_task(p)) |
| 595 | return p->prio; |
| 596 | |
| 597 | bonus = CURRENT_BONUS(p) - MAX_BONUS / 2; |
| 598 | |
| 599 | prio = p->static_prio - bonus; |
| 600 | if (prio < MAX_RT_PRIO) |
| 601 | prio = MAX_RT_PRIO; |
| 602 | if (prio > MAX_PRIO-1) |
| 603 | prio = MAX_PRIO-1; |
| 604 | return prio; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * __activate_task - move a task to the runqueue. |
| 609 | */ |
| 610 | static inline void __activate_task(task_t *p, runqueue_t *rq) |
| 611 | { |
| 612 | enqueue_task(p, rq->active); |
| 613 | rq->nr_running++; |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * __activate_idle_task - move idle task to the _front_ of runqueue. |
| 618 | */ |
| 619 | static inline void __activate_idle_task(task_t *p, runqueue_t *rq) |
| 620 | { |
| 621 | enqueue_task_head(p, rq->active); |
| 622 | rq->nr_running++; |
| 623 | } |
| 624 | |
| 625 | static void recalc_task_prio(task_t *p, unsigned long long now) |
| 626 | { |
| 627 | /* Caller must always ensure 'now >= p->timestamp' */ |
| 628 | unsigned long long __sleep_time = now - p->timestamp; |
| 629 | unsigned long sleep_time; |
| 630 | |
| 631 | if (__sleep_time > NS_MAX_SLEEP_AVG) |
| 632 | sleep_time = NS_MAX_SLEEP_AVG; |
| 633 | else |
| 634 | sleep_time = (unsigned long)__sleep_time; |
| 635 | |
| 636 | if (likely(sleep_time > 0)) { |
| 637 | /* |
| 638 | * User tasks that sleep a long time are categorised as |
| 639 | * idle and will get just interactive status to stay active & |
| 640 | * prevent them suddenly becoming cpu hogs and starving |
| 641 | * other processes. |
| 642 | */ |
| 643 | if (p->mm && p->activated != -1 && |
| 644 | sleep_time > INTERACTIVE_SLEEP(p)) { |
| 645 | p->sleep_avg = JIFFIES_TO_NS(MAX_SLEEP_AVG - |
| 646 | DEF_TIMESLICE); |
| 647 | } else { |
| 648 | /* |
| 649 | * The lower the sleep avg a task has the more |
| 650 | * rapidly it will rise with sleep time. |
| 651 | */ |
| 652 | sleep_time *= (MAX_BONUS - CURRENT_BONUS(p)) ? : 1; |
| 653 | |
| 654 | /* |
| 655 | * Tasks waking from uninterruptible sleep are |
| 656 | * limited in their sleep_avg rise as they |
| 657 | * are likely to be waiting on I/O |
| 658 | */ |
| 659 | if (p->activated == -1 && p->mm) { |
| 660 | if (p->sleep_avg >= INTERACTIVE_SLEEP(p)) |
| 661 | sleep_time = 0; |
| 662 | else if (p->sleep_avg + sleep_time >= |
| 663 | INTERACTIVE_SLEEP(p)) { |
| 664 | p->sleep_avg = INTERACTIVE_SLEEP(p); |
| 665 | sleep_time = 0; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * This code gives a bonus to interactive tasks. |
| 671 | * |
| 672 | * The boost works by updating the 'average sleep time' |
| 673 | * value here, based on ->timestamp. The more time a |
| 674 | * task spends sleeping, the higher the average gets - |
| 675 | * and the higher the priority boost gets as well. |
| 676 | */ |
| 677 | p->sleep_avg += sleep_time; |
| 678 | |
| 679 | if (p->sleep_avg > NS_MAX_SLEEP_AVG) |
| 680 | p->sleep_avg = NS_MAX_SLEEP_AVG; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | p->prio = effective_prio(p); |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * activate_task - move a task to the runqueue and do priority recalculation |
| 689 | * |
| 690 | * Update all the scheduling statistics stuff. (sleep average |
| 691 | * calculation, priority modifiers, etc.) |
| 692 | */ |
| 693 | static void activate_task(task_t *p, runqueue_t *rq, int local) |
| 694 | { |
| 695 | unsigned long long now; |
| 696 | |
| 697 | now = sched_clock(); |
| 698 | #ifdef CONFIG_SMP |
| 699 | if (!local) { |
| 700 | /* Compensate for drifting sched_clock */ |
| 701 | runqueue_t *this_rq = this_rq(); |
| 702 | now = (now - this_rq->timestamp_last_tick) |
| 703 | + rq->timestamp_last_tick; |
| 704 | } |
| 705 | #endif |
| 706 | |
| 707 | recalc_task_prio(p, now); |
| 708 | |
| 709 | /* |
| 710 | * This checks to make sure it's not an uninterruptible task |
| 711 | * that is now waking up. |
| 712 | */ |
| 713 | if (!p->activated) { |
| 714 | /* |
| 715 | * Tasks which were woken up by interrupts (ie. hw events) |
| 716 | * are most likely of interactive nature. So we give them |
| 717 | * the credit of extending their sleep time to the period |
| 718 | * of time they spend on the runqueue, waiting for execution |
| 719 | * on a CPU, first time around: |
| 720 | */ |
| 721 | if (in_interrupt()) |
| 722 | p->activated = 2; |
| 723 | else { |
| 724 | /* |
| 725 | * Normal first-time wakeups get a credit too for |
| 726 | * on-runqueue time, but it will be weighted down: |
| 727 | */ |
| 728 | p->activated = 1; |
| 729 | } |
| 730 | } |
| 731 | p->timestamp = now; |
| 732 | |
| 733 | __activate_task(p, rq); |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * deactivate_task - remove a task from the runqueue. |
| 738 | */ |
| 739 | static void deactivate_task(struct task_struct *p, runqueue_t *rq) |
| 740 | { |
| 741 | rq->nr_running--; |
| 742 | dequeue_task(p, p->array); |
| 743 | p->array = NULL; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * resched_task - mark a task 'to be rescheduled now'. |
| 748 | * |
| 749 | * On UP this means the setting of the need_resched flag, on SMP it |
| 750 | * might also involve a cross-CPU call to trigger the scheduler on |
| 751 | * the target CPU. |
| 752 | */ |
| 753 | #ifdef CONFIG_SMP |
| 754 | static void resched_task(task_t *p) |
| 755 | { |
| 756 | int need_resched, nrpolling; |
| 757 | |
| 758 | assert_spin_locked(&task_rq(p)->lock); |
| 759 | |
| 760 | /* minimise the chance of sending an interrupt to poll_idle() */ |
| 761 | nrpolling = test_tsk_thread_flag(p,TIF_POLLING_NRFLAG); |
| 762 | need_resched = test_and_set_tsk_thread_flag(p,TIF_NEED_RESCHED); |
| 763 | nrpolling |= test_tsk_thread_flag(p,TIF_POLLING_NRFLAG); |
| 764 | |
| 765 | if (!need_resched && !nrpolling && (task_cpu(p) != smp_processor_id())) |
| 766 | smp_send_reschedule(task_cpu(p)); |
| 767 | } |
| 768 | #else |
| 769 | static inline void resched_task(task_t *p) |
| 770 | { |
| 771 | set_tsk_need_resched(p); |
| 772 | } |
| 773 | #endif |
| 774 | |
| 775 | /** |
| 776 | * task_curr - is this task currently executing on a CPU? |
| 777 | * @p: the task in question. |
| 778 | */ |
| 779 | inline int task_curr(const task_t *p) |
| 780 | { |
| 781 | return cpu_curr(task_cpu(p)) == p; |
| 782 | } |
| 783 | |
| 784 | #ifdef CONFIG_SMP |
| 785 | enum request_type { |
| 786 | REQ_MOVE_TASK, |
| 787 | REQ_SET_DOMAIN, |
| 788 | }; |
| 789 | |
| 790 | typedef struct { |
| 791 | struct list_head list; |
| 792 | enum request_type type; |
| 793 | |
| 794 | /* For REQ_MOVE_TASK */ |
| 795 | task_t *task; |
| 796 | int dest_cpu; |
| 797 | |
| 798 | /* For REQ_SET_DOMAIN */ |
| 799 | struct sched_domain *sd; |
| 800 | |
| 801 | struct completion done; |
| 802 | } migration_req_t; |
| 803 | |
| 804 | /* |
| 805 | * The task's runqueue lock must be held. |
| 806 | * Returns true if you have to wait for migration thread. |
| 807 | */ |
| 808 | static int migrate_task(task_t *p, int dest_cpu, migration_req_t *req) |
| 809 | { |
| 810 | runqueue_t *rq = task_rq(p); |
| 811 | |
| 812 | /* |
| 813 | * If the task is not on a runqueue (and not running), then |
| 814 | * it is sufficient to simply update the task's cpu field. |
| 815 | */ |
| 816 | if (!p->array && !task_running(rq, p)) { |
| 817 | set_task_cpu(p, dest_cpu); |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | init_completion(&req->done); |
| 822 | req->type = REQ_MOVE_TASK; |
| 823 | req->task = p; |
| 824 | req->dest_cpu = dest_cpu; |
| 825 | list_add(&req->list, &rq->migration_queue); |
| 826 | return 1; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * wait_task_inactive - wait for a thread to unschedule. |
| 831 | * |
| 832 | * The caller must ensure that the task *will* unschedule sometime soon, |
| 833 | * else this function might spin for a *long* time. This function can't |
| 834 | * be called with interrupts off, or it may introduce deadlock with |
| 835 | * smp_call_function() if an IPI is sent by the same process we are |
| 836 | * waiting to become inactive. |
| 837 | */ |
| 838 | void wait_task_inactive(task_t * p) |
| 839 | { |
| 840 | unsigned long flags; |
| 841 | runqueue_t *rq; |
| 842 | int preempted; |
| 843 | |
| 844 | repeat: |
| 845 | rq = task_rq_lock(p, &flags); |
| 846 | /* Must be off runqueue entirely, not preempted. */ |
| 847 | if (unlikely(p->array || task_running(rq, p))) { |
| 848 | /* If it's preempted, we yield. It could be a while. */ |
| 849 | preempted = !task_running(rq, p); |
| 850 | task_rq_unlock(rq, &flags); |
| 851 | cpu_relax(); |
| 852 | if (preempted) |
| 853 | yield(); |
| 854 | goto repeat; |
| 855 | } |
| 856 | task_rq_unlock(rq, &flags); |
| 857 | } |
| 858 | |
| 859 | /*** |
| 860 | * kick_process - kick a running thread to enter/exit the kernel |
| 861 | * @p: the to-be-kicked thread |
| 862 | * |
| 863 | * Cause a process which is running on another CPU to enter |
| 864 | * kernel-mode, without any delay. (to get signals handled.) |
| 865 | * |
| 866 | * NOTE: this function doesnt have to take the runqueue lock, |
| 867 | * because all it wants to ensure is that the remote task enters |
| 868 | * the kernel. If the IPI races and the task has been migrated |
| 869 | * to another CPU then no harm is done and the purpose has been |
| 870 | * achieved as well. |
| 871 | */ |
| 872 | void kick_process(task_t *p) |
| 873 | { |
| 874 | int cpu; |
| 875 | |
| 876 | preempt_disable(); |
| 877 | cpu = task_cpu(p); |
| 878 | if ((cpu != smp_processor_id()) && task_curr(p)) |
| 879 | smp_send_reschedule(cpu); |
| 880 | preempt_enable(); |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * Return a low guess at the load of a migration-source cpu. |
| 885 | * |
| 886 | * We want to under-estimate the load of migration sources, to |
| 887 | * balance conservatively. |
| 888 | */ |
| 889 | static inline unsigned long source_load(int cpu) |
| 890 | { |
| 891 | runqueue_t *rq = cpu_rq(cpu); |
| 892 | unsigned long load_now = rq->nr_running * SCHED_LOAD_SCALE; |
| 893 | |
| 894 | return min(rq->cpu_load, load_now); |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * Return a high guess at the load of a migration-target cpu |
| 899 | */ |
| 900 | static inline unsigned long target_load(int cpu) |
| 901 | { |
| 902 | runqueue_t *rq = cpu_rq(cpu); |
| 903 | unsigned long load_now = rq->nr_running * SCHED_LOAD_SCALE; |
| 904 | |
| 905 | return max(rq->cpu_load, load_now); |
| 906 | } |
| 907 | |
| 908 | #endif |
| 909 | |
| 910 | /* |
| 911 | * wake_idle() will wake a task on an idle cpu if task->cpu is |
| 912 | * not idle and an idle cpu is available. The span of cpus to |
| 913 | * search starts with cpus closest then further out as needed, |
| 914 | * so we always favor a closer, idle cpu. |
| 915 | * |
| 916 | * Returns the CPU we should wake onto. |
| 917 | */ |
| 918 | #if defined(ARCH_HAS_SCHED_WAKE_IDLE) |
| 919 | static int wake_idle(int cpu, task_t *p) |
| 920 | { |
| 921 | cpumask_t tmp; |
| 922 | struct sched_domain *sd; |
| 923 | int i; |
| 924 | |
| 925 | if (idle_cpu(cpu)) |
| 926 | return cpu; |
| 927 | |
| 928 | for_each_domain(cpu, sd) { |
| 929 | if (sd->flags & SD_WAKE_IDLE) { |
Nick Piggin | e0f364f | 2005-06-25 14:57:06 -0700 | [diff] [blame] | 930 | cpus_and(tmp, sd->span, p->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | for_each_cpu_mask(i, tmp) { |
| 932 | if (idle_cpu(i)) |
| 933 | return i; |
| 934 | } |
| 935 | } |
Nick Piggin | e0f364f | 2005-06-25 14:57:06 -0700 | [diff] [blame] | 936 | else |
| 937 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } |
| 939 | return cpu; |
| 940 | } |
| 941 | #else |
| 942 | static inline int wake_idle(int cpu, task_t *p) |
| 943 | { |
| 944 | return cpu; |
| 945 | } |
| 946 | #endif |
| 947 | |
| 948 | /*** |
| 949 | * try_to_wake_up - wake up a thread |
| 950 | * @p: the to-be-woken-up thread |
| 951 | * @state: the mask of task states that can be woken |
| 952 | * @sync: do a synchronous wakeup? |
| 953 | * |
| 954 | * Put it on the run-queue if it's not already there. The "current" |
| 955 | * thread is always on the run-queue (except when the actual |
| 956 | * re-schedule is in progress), and as such you're allowed to do |
| 957 | * the simpler "current->state = TASK_RUNNING" to mark yourself |
| 958 | * runnable without the overhead of this. |
| 959 | * |
| 960 | * returns failure only if the task is already active. |
| 961 | */ |
| 962 | static int try_to_wake_up(task_t * p, unsigned int state, int sync) |
| 963 | { |
| 964 | int cpu, this_cpu, success = 0; |
| 965 | unsigned long flags; |
| 966 | long old_state; |
| 967 | runqueue_t *rq; |
| 968 | #ifdef CONFIG_SMP |
| 969 | unsigned long load, this_load; |
| 970 | struct sched_domain *sd; |
| 971 | int new_cpu; |
| 972 | #endif |
| 973 | |
| 974 | rq = task_rq_lock(p, &flags); |
| 975 | old_state = p->state; |
| 976 | if (!(old_state & state)) |
| 977 | goto out; |
| 978 | |
| 979 | if (p->array) |
| 980 | goto out_running; |
| 981 | |
| 982 | cpu = task_cpu(p); |
| 983 | this_cpu = smp_processor_id(); |
| 984 | |
| 985 | #ifdef CONFIG_SMP |
| 986 | if (unlikely(task_running(rq, p))) |
| 987 | goto out_activate; |
| 988 | |
| 989 | #ifdef CONFIG_SCHEDSTATS |
| 990 | schedstat_inc(rq, ttwu_cnt); |
| 991 | if (cpu == this_cpu) { |
| 992 | schedstat_inc(rq, ttwu_local); |
| 993 | } else { |
| 994 | for_each_domain(this_cpu, sd) { |
| 995 | if (cpu_isset(cpu, sd->span)) { |
| 996 | schedstat_inc(sd, ttwu_wake_remote); |
| 997 | break; |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | #endif |
| 1002 | |
| 1003 | new_cpu = cpu; |
| 1004 | if (cpu == this_cpu || unlikely(!cpu_isset(this_cpu, p->cpus_allowed))) |
| 1005 | goto out_set_cpu; |
| 1006 | |
| 1007 | load = source_load(cpu); |
| 1008 | this_load = target_load(this_cpu); |
| 1009 | |
| 1010 | /* |
| 1011 | * If sync wakeup then subtract the (maximum possible) effect of |
| 1012 | * the currently running task from the load of the current CPU: |
| 1013 | */ |
| 1014 | if (sync) |
| 1015 | this_load -= SCHED_LOAD_SCALE; |
| 1016 | |
| 1017 | /* Don't pull the task off an idle CPU to a busy one */ |
| 1018 | if (load < SCHED_LOAD_SCALE/2 && this_load > SCHED_LOAD_SCALE/2) |
| 1019 | goto out_set_cpu; |
| 1020 | |
| 1021 | new_cpu = this_cpu; /* Wake to this CPU if we can */ |
| 1022 | |
| 1023 | /* |
| 1024 | * Scan domains for affine wakeup and passive balancing |
| 1025 | * possibilities. |
| 1026 | */ |
| 1027 | for_each_domain(this_cpu, sd) { |
| 1028 | unsigned int imbalance; |
| 1029 | /* |
| 1030 | * Start passive balancing when half the imbalance_pct |
| 1031 | * limit is reached. |
| 1032 | */ |
| 1033 | imbalance = sd->imbalance_pct + (sd->imbalance_pct - 100) / 2; |
| 1034 | |
| 1035 | if ((sd->flags & SD_WAKE_AFFINE) && |
| 1036 | !task_hot(p, rq->timestamp_last_tick, sd)) { |
| 1037 | /* |
| 1038 | * This domain has SD_WAKE_AFFINE and p is cache cold |
| 1039 | * in this domain. |
| 1040 | */ |
| 1041 | if (cpu_isset(cpu, sd->span)) { |
| 1042 | schedstat_inc(sd, ttwu_move_affine); |
| 1043 | goto out_set_cpu; |
| 1044 | } |
| 1045 | } else if ((sd->flags & SD_WAKE_BALANCE) && |
| 1046 | imbalance*this_load <= 100*load) { |
| 1047 | /* |
| 1048 | * This domain has SD_WAKE_BALANCE and there is |
| 1049 | * an imbalance. |
| 1050 | */ |
| 1051 | if (cpu_isset(cpu, sd->span)) { |
| 1052 | schedstat_inc(sd, ttwu_move_balance); |
| 1053 | goto out_set_cpu; |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */ |
| 1059 | out_set_cpu: |
| 1060 | new_cpu = wake_idle(new_cpu, p); |
| 1061 | if (new_cpu != cpu) { |
| 1062 | set_task_cpu(p, new_cpu); |
| 1063 | task_rq_unlock(rq, &flags); |
| 1064 | /* might preempt at this point */ |
| 1065 | rq = task_rq_lock(p, &flags); |
| 1066 | old_state = p->state; |
| 1067 | if (!(old_state & state)) |
| 1068 | goto out; |
| 1069 | if (p->array) |
| 1070 | goto out_running; |
| 1071 | |
| 1072 | this_cpu = smp_processor_id(); |
| 1073 | cpu = task_cpu(p); |
| 1074 | } |
| 1075 | |
| 1076 | out_activate: |
| 1077 | #endif /* CONFIG_SMP */ |
| 1078 | if (old_state == TASK_UNINTERRUPTIBLE) { |
| 1079 | rq->nr_uninterruptible--; |
| 1080 | /* |
| 1081 | * Tasks on involuntary sleep don't earn |
| 1082 | * sleep_avg beyond just interactive state. |
| 1083 | */ |
| 1084 | p->activated = -1; |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * Sync wakeups (i.e. those types of wakeups where the waker |
| 1089 | * has indicated that it will leave the CPU in short order) |
| 1090 | * don't trigger a preemption, if the woken up task will run on |
| 1091 | * this cpu. (in this case the 'I will reschedule' promise of |
| 1092 | * the waker guarantees that the freshly woken up task is going |
| 1093 | * to be considered on this CPU.) |
| 1094 | */ |
| 1095 | activate_task(p, rq, cpu == this_cpu); |
| 1096 | if (!sync || cpu != this_cpu) { |
| 1097 | if (TASK_PREEMPTS_CURR(p, rq)) |
| 1098 | resched_task(rq->curr); |
| 1099 | } |
| 1100 | success = 1; |
| 1101 | |
| 1102 | out_running: |
| 1103 | p->state = TASK_RUNNING; |
| 1104 | out: |
| 1105 | task_rq_unlock(rq, &flags); |
| 1106 | |
| 1107 | return success; |
| 1108 | } |
| 1109 | |
| 1110 | int fastcall wake_up_process(task_t * p) |
| 1111 | { |
| 1112 | return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED | |
| 1113 | TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0); |
| 1114 | } |
| 1115 | |
| 1116 | EXPORT_SYMBOL(wake_up_process); |
| 1117 | |
| 1118 | int fastcall wake_up_state(task_t *p, unsigned int state) |
| 1119 | { |
| 1120 | return try_to_wake_up(p, state, 0); |
| 1121 | } |
| 1122 | |
| 1123 | #ifdef CONFIG_SMP |
| 1124 | static int find_idlest_cpu(struct task_struct *p, int this_cpu, |
| 1125 | struct sched_domain *sd); |
| 1126 | #endif |
| 1127 | |
| 1128 | /* |
| 1129 | * Perform scheduler related setup for a newly forked process p. |
| 1130 | * p is forked by current. |
| 1131 | */ |
| 1132 | void fastcall sched_fork(task_t *p) |
| 1133 | { |
| 1134 | /* |
| 1135 | * We mark the process as running here, but have not actually |
| 1136 | * inserted it onto the runqueue yet. This guarantees that |
| 1137 | * nobody will actually run it, and a signal or other external |
| 1138 | * event cannot wake it up and insert it on the runqueue either. |
| 1139 | */ |
| 1140 | p->state = TASK_RUNNING; |
| 1141 | INIT_LIST_HEAD(&p->run_list); |
| 1142 | p->array = NULL; |
| 1143 | spin_lock_init(&p->switch_lock); |
| 1144 | #ifdef CONFIG_SCHEDSTATS |
| 1145 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
| 1146 | #endif |
| 1147 | #ifdef CONFIG_PREEMPT |
| 1148 | /* |
| 1149 | * During context-switch we hold precisely one spinlock, which |
| 1150 | * schedule_tail drops. (in the common case it's this_rq()->lock, |
| 1151 | * but it also can be p->switch_lock.) So we compensate with a count |
| 1152 | * of 1. Also, we want to start with kernel preemption disabled. |
| 1153 | */ |
| 1154 | p->thread_info->preempt_count = 1; |
| 1155 | #endif |
| 1156 | /* |
| 1157 | * Share the timeslice between parent and child, thus the |
| 1158 | * total amount of pending timeslices in the system doesn't change, |
| 1159 | * resulting in more scheduling fairness. |
| 1160 | */ |
| 1161 | local_irq_disable(); |
| 1162 | p->time_slice = (current->time_slice + 1) >> 1; |
| 1163 | /* |
| 1164 | * The remainder of the first timeslice might be recovered by |
| 1165 | * the parent if the child exits early enough. |
| 1166 | */ |
| 1167 | p->first_time_slice = 1; |
| 1168 | current->time_slice >>= 1; |
| 1169 | p->timestamp = sched_clock(); |
| 1170 | if (unlikely(!current->time_slice)) { |
| 1171 | /* |
| 1172 | * This case is rare, it happens when the parent has only |
| 1173 | * a single jiffy left from its timeslice. Taking the |
| 1174 | * runqueue lock is not a problem. |
| 1175 | */ |
| 1176 | current->time_slice = 1; |
| 1177 | preempt_disable(); |
| 1178 | scheduler_tick(); |
| 1179 | local_irq_enable(); |
| 1180 | preempt_enable(); |
| 1181 | } else |
| 1182 | local_irq_enable(); |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | * wake_up_new_task - wake up a newly created task for the first time. |
| 1187 | * |
| 1188 | * This function will do some initial scheduler statistics housekeeping |
| 1189 | * that must be done for every newly created context, then puts the task |
| 1190 | * on the runqueue and wakes it. |
| 1191 | */ |
| 1192 | void fastcall wake_up_new_task(task_t * p, unsigned long clone_flags) |
| 1193 | { |
| 1194 | unsigned long flags; |
| 1195 | int this_cpu, cpu; |
| 1196 | runqueue_t *rq, *this_rq; |
| 1197 | |
| 1198 | rq = task_rq_lock(p, &flags); |
| 1199 | cpu = task_cpu(p); |
| 1200 | this_cpu = smp_processor_id(); |
| 1201 | |
| 1202 | BUG_ON(p->state != TASK_RUNNING); |
| 1203 | |
| 1204 | /* |
| 1205 | * We decrease the sleep average of forking parents |
| 1206 | * and children as well, to keep max-interactive tasks |
| 1207 | * from forking tasks that are max-interactive. The parent |
| 1208 | * (current) is done further down, under its lock. |
| 1209 | */ |
| 1210 | p->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(p) * |
| 1211 | CHILD_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS); |
| 1212 | |
| 1213 | p->prio = effective_prio(p); |
| 1214 | |
| 1215 | if (likely(cpu == this_cpu)) { |
| 1216 | if (!(clone_flags & CLONE_VM)) { |
| 1217 | /* |
| 1218 | * The VM isn't cloned, so we're in a good position to |
| 1219 | * do child-runs-first in anticipation of an exec. This |
| 1220 | * usually avoids a lot of COW overhead. |
| 1221 | */ |
| 1222 | if (unlikely(!current->array)) |
| 1223 | __activate_task(p, rq); |
| 1224 | else { |
| 1225 | p->prio = current->prio; |
| 1226 | list_add_tail(&p->run_list, ¤t->run_list); |
| 1227 | p->array = current->array; |
| 1228 | p->array->nr_active++; |
| 1229 | rq->nr_running++; |
| 1230 | } |
| 1231 | set_need_resched(); |
| 1232 | } else |
| 1233 | /* Run child last */ |
| 1234 | __activate_task(p, rq); |
| 1235 | /* |
| 1236 | * We skip the following code due to cpu == this_cpu |
| 1237 | * |
| 1238 | * task_rq_unlock(rq, &flags); |
| 1239 | * this_rq = task_rq_lock(current, &flags); |
| 1240 | */ |
| 1241 | this_rq = rq; |
| 1242 | } else { |
| 1243 | this_rq = cpu_rq(this_cpu); |
| 1244 | |
| 1245 | /* |
| 1246 | * Not the local CPU - must adjust timestamp. This should |
| 1247 | * get optimised away in the !CONFIG_SMP case. |
| 1248 | */ |
| 1249 | p->timestamp = (p->timestamp - this_rq->timestamp_last_tick) |
| 1250 | + rq->timestamp_last_tick; |
| 1251 | __activate_task(p, rq); |
| 1252 | if (TASK_PREEMPTS_CURR(p, rq)) |
| 1253 | resched_task(rq->curr); |
| 1254 | |
| 1255 | /* |
| 1256 | * Parent and child are on different CPUs, now get the |
| 1257 | * parent runqueue to update the parent's ->sleep_avg: |
| 1258 | */ |
| 1259 | task_rq_unlock(rq, &flags); |
| 1260 | this_rq = task_rq_lock(current, &flags); |
| 1261 | } |
| 1262 | current->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(current) * |
| 1263 | PARENT_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS); |
| 1264 | task_rq_unlock(this_rq, &flags); |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Potentially available exiting-child timeslices are |
| 1269 | * retrieved here - this way the parent does not get |
| 1270 | * penalized for creating too many threads. |
| 1271 | * |
| 1272 | * (this cannot be used to 'generate' timeslices |
| 1273 | * artificially, because any timeslice recovered here |
| 1274 | * was given away by the parent in the first place.) |
| 1275 | */ |
| 1276 | void fastcall sched_exit(task_t * p) |
| 1277 | { |
| 1278 | unsigned long flags; |
| 1279 | runqueue_t *rq; |
| 1280 | |
| 1281 | /* |
| 1282 | * If the child was a (relative-) CPU hog then decrease |
| 1283 | * the sleep_avg of the parent as well. |
| 1284 | */ |
| 1285 | rq = task_rq_lock(p->parent, &flags); |
| 1286 | if (p->first_time_slice) { |
| 1287 | p->parent->time_slice += p->time_slice; |
| 1288 | if (unlikely(p->parent->time_slice > task_timeslice(p))) |
| 1289 | p->parent->time_slice = task_timeslice(p); |
| 1290 | } |
| 1291 | if (p->sleep_avg < p->parent->sleep_avg) |
| 1292 | p->parent->sleep_avg = p->parent->sleep_avg / |
| 1293 | (EXIT_WEIGHT + 1) * EXIT_WEIGHT + p->sleep_avg / |
| 1294 | (EXIT_WEIGHT + 1); |
| 1295 | task_rq_unlock(rq, &flags); |
| 1296 | } |
| 1297 | |
| 1298 | /** |
| 1299 | * finish_task_switch - clean up after a task-switch |
| 1300 | * @prev: the thread we just switched away from. |
| 1301 | * |
| 1302 | * We enter this with the runqueue still locked, and finish_arch_switch() |
| 1303 | * will unlock it along with doing any other architecture-specific cleanup |
| 1304 | * actions. |
| 1305 | * |
| 1306 | * Note that we may have delayed dropping an mm in context_switch(). If |
| 1307 | * so, we finish that here outside of the runqueue lock. (Doing it |
| 1308 | * with the lock held can cause deadlocks; see schedule() for |
| 1309 | * details.) |
| 1310 | */ |
| 1311 | static inline void finish_task_switch(task_t *prev) |
| 1312 | __releases(rq->lock) |
| 1313 | { |
| 1314 | runqueue_t *rq = this_rq(); |
| 1315 | struct mm_struct *mm = rq->prev_mm; |
| 1316 | unsigned long prev_task_flags; |
| 1317 | |
| 1318 | rq->prev_mm = NULL; |
| 1319 | |
| 1320 | /* |
| 1321 | * A task struct has one reference for the use as "current". |
| 1322 | * If a task dies, then it sets EXIT_ZOMBIE in tsk->exit_state and |
| 1323 | * calls schedule one last time. The schedule call will never return, |
| 1324 | * and the scheduled task must drop that reference. |
| 1325 | * The test for EXIT_ZOMBIE must occur while the runqueue locks are |
| 1326 | * still held, otherwise prev could be scheduled on another cpu, die |
| 1327 | * there before we look at prev->state, and then the reference would |
| 1328 | * be dropped twice. |
| 1329 | * Manfred Spraul <manfred@colorfullife.com> |
| 1330 | */ |
| 1331 | prev_task_flags = prev->flags; |
| 1332 | finish_arch_switch(rq, prev); |
| 1333 | if (mm) |
| 1334 | mmdrop(mm); |
| 1335 | if (unlikely(prev_task_flags & PF_DEAD)) |
| 1336 | put_task_struct(prev); |
| 1337 | } |
| 1338 | |
| 1339 | /** |
| 1340 | * schedule_tail - first thing a freshly forked thread must call. |
| 1341 | * @prev: the thread we just switched away from. |
| 1342 | */ |
| 1343 | asmlinkage void schedule_tail(task_t *prev) |
| 1344 | __releases(rq->lock) |
| 1345 | { |
| 1346 | finish_task_switch(prev); |
| 1347 | |
| 1348 | if (current->set_child_tid) |
| 1349 | put_user(current->pid, current->set_child_tid); |
| 1350 | } |
| 1351 | |
| 1352 | /* |
| 1353 | * context_switch - switch to the new MM and the new |
| 1354 | * thread's register state. |
| 1355 | */ |
| 1356 | static inline |
| 1357 | task_t * context_switch(runqueue_t *rq, task_t *prev, task_t *next) |
| 1358 | { |
| 1359 | struct mm_struct *mm = next->mm; |
| 1360 | struct mm_struct *oldmm = prev->active_mm; |
| 1361 | |
| 1362 | if (unlikely(!mm)) { |
| 1363 | next->active_mm = oldmm; |
| 1364 | atomic_inc(&oldmm->mm_count); |
| 1365 | enter_lazy_tlb(oldmm, next); |
| 1366 | } else |
| 1367 | switch_mm(oldmm, mm, next); |
| 1368 | |
| 1369 | if (unlikely(!prev->mm)) { |
| 1370 | prev->active_mm = NULL; |
| 1371 | WARN_ON(rq->prev_mm); |
| 1372 | rq->prev_mm = oldmm; |
| 1373 | } |
| 1374 | |
| 1375 | /* Here we just switch the register state and the stack. */ |
| 1376 | switch_to(prev, next, prev); |
| 1377 | |
| 1378 | return prev; |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * nr_running, nr_uninterruptible and nr_context_switches: |
| 1383 | * |
| 1384 | * externally visible scheduler statistics: current number of runnable |
| 1385 | * threads, current number of uninterruptible-sleeping threads, total |
| 1386 | * number of context switches performed since bootup. |
| 1387 | */ |
| 1388 | unsigned long nr_running(void) |
| 1389 | { |
| 1390 | unsigned long i, sum = 0; |
| 1391 | |
| 1392 | for_each_online_cpu(i) |
| 1393 | sum += cpu_rq(i)->nr_running; |
| 1394 | |
| 1395 | return sum; |
| 1396 | } |
| 1397 | |
| 1398 | unsigned long nr_uninterruptible(void) |
| 1399 | { |
| 1400 | unsigned long i, sum = 0; |
| 1401 | |
| 1402 | for_each_cpu(i) |
| 1403 | sum += cpu_rq(i)->nr_uninterruptible; |
| 1404 | |
| 1405 | /* |
| 1406 | * Since we read the counters lockless, it might be slightly |
| 1407 | * inaccurate. Do not allow it to go below zero though: |
| 1408 | */ |
| 1409 | if (unlikely((long)sum < 0)) |
| 1410 | sum = 0; |
| 1411 | |
| 1412 | return sum; |
| 1413 | } |
| 1414 | |
| 1415 | unsigned long long nr_context_switches(void) |
| 1416 | { |
| 1417 | unsigned long long i, sum = 0; |
| 1418 | |
| 1419 | for_each_cpu(i) |
| 1420 | sum += cpu_rq(i)->nr_switches; |
| 1421 | |
| 1422 | return sum; |
| 1423 | } |
| 1424 | |
| 1425 | unsigned long nr_iowait(void) |
| 1426 | { |
| 1427 | unsigned long i, sum = 0; |
| 1428 | |
| 1429 | for_each_cpu(i) |
| 1430 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
| 1431 | |
| 1432 | return sum; |
| 1433 | } |
| 1434 | |
| 1435 | #ifdef CONFIG_SMP |
| 1436 | |
| 1437 | /* |
| 1438 | * double_rq_lock - safely lock two runqueues |
| 1439 | * |
| 1440 | * Note this does not disable interrupts like task_rq_lock, |
| 1441 | * you need to do so manually before calling. |
| 1442 | */ |
| 1443 | static void double_rq_lock(runqueue_t *rq1, runqueue_t *rq2) |
| 1444 | __acquires(rq1->lock) |
| 1445 | __acquires(rq2->lock) |
| 1446 | { |
| 1447 | if (rq1 == rq2) { |
| 1448 | spin_lock(&rq1->lock); |
| 1449 | __acquire(rq2->lock); /* Fake it out ;) */ |
| 1450 | } else { |
| 1451 | if (rq1 < rq2) { |
| 1452 | spin_lock(&rq1->lock); |
| 1453 | spin_lock(&rq2->lock); |
| 1454 | } else { |
| 1455 | spin_lock(&rq2->lock); |
| 1456 | spin_lock(&rq1->lock); |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | /* |
| 1462 | * double_rq_unlock - safely unlock two runqueues |
| 1463 | * |
| 1464 | * Note this does not restore interrupts like task_rq_unlock, |
| 1465 | * you need to do so manually after calling. |
| 1466 | */ |
| 1467 | static void double_rq_unlock(runqueue_t *rq1, runqueue_t *rq2) |
| 1468 | __releases(rq1->lock) |
| 1469 | __releases(rq2->lock) |
| 1470 | { |
| 1471 | spin_unlock(&rq1->lock); |
| 1472 | if (rq1 != rq2) |
| 1473 | spin_unlock(&rq2->lock); |
| 1474 | else |
| 1475 | __release(rq2->lock); |
| 1476 | } |
| 1477 | |
| 1478 | /* |
| 1479 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. |
| 1480 | */ |
| 1481 | static void double_lock_balance(runqueue_t *this_rq, runqueue_t *busiest) |
| 1482 | __releases(this_rq->lock) |
| 1483 | __acquires(busiest->lock) |
| 1484 | __acquires(this_rq->lock) |
| 1485 | { |
| 1486 | if (unlikely(!spin_trylock(&busiest->lock))) { |
| 1487 | if (busiest < this_rq) { |
| 1488 | spin_unlock(&this_rq->lock); |
| 1489 | spin_lock(&busiest->lock); |
| 1490 | spin_lock(&this_rq->lock); |
| 1491 | } else |
| 1492 | spin_lock(&busiest->lock); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | /* |
| 1497 | * find_idlest_cpu - find the least busy runqueue. |
| 1498 | */ |
| 1499 | static int find_idlest_cpu(struct task_struct *p, int this_cpu, |
| 1500 | struct sched_domain *sd) |
| 1501 | { |
| 1502 | unsigned long load, min_load, this_load; |
| 1503 | int i, min_cpu; |
| 1504 | cpumask_t mask; |
| 1505 | |
| 1506 | min_cpu = UINT_MAX; |
| 1507 | min_load = ULONG_MAX; |
| 1508 | |
| 1509 | cpus_and(mask, sd->span, p->cpus_allowed); |
| 1510 | |
| 1511 | for_each_cpu_mask(i, mask) { |
| 1512 | load = target_load(i); |
| 1513 | |
| 1514 | if (load < min_load) { |
| 1515 | min_cpu = i; |
| 1516 | min_load = load; |
| 1517 | |
| 1518 | /* break out early on an idle CPU: */ |
| 1519 | if (!min_load) |
| 1520 | break; |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | /* add +1 to account for the new task */ |
| 1525 | this_load = source_load(this_cpu) + SCHED_LOAD_SCALE; |
| 1526 | |
| 1527 | /* |
| 1528 | * Would with the addition of the new task to the |
| 1529 | * current CPU there be an imbalance between this |
| 1530 | * CPU and the idlest CPU? |
| 1531 | * |
| 1532 | * Use half of the balancing threshold - new-context is |
| 1533 | * a good opportunity to balance. |
| 1534 | */ |
| 1535 | if (min_load*(100 + (sd->imbalance_pct-100)/2) < this_load*100) |
| 1536 | return min_cpu; |
| 1537 | |
| 1538 | return this_cpu; |
| 1539 | } |
| 1540 | |
| 1541 | /* |
| 1542 | * If dest_cpu is allowed for this process, migrate the task to it. |
| 1543 | * This is accomplished by forcing the cpu_allowed mask to only |
| 1544 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then |
| 1545 | * the cpu_allowed mask is restored. |
| 1546 | */ |
| 1547 | static void sched_migrate_task(task_t *p, int dest_cpu) |
| 1548 | { |
| 1549 | migration_req_t req; |
| 1550 | runqueue_t *rq; |
| 1551 | unsigned long flags; |
| 1552 | |
| 1553 | rq = task_rq_lock(p, &flags); |
| 1554 | if (!cpu_isset(dest_cpu, p->cpus_allowed) |
| 1555 | || unlikely(cpu_is_offline(dest_cpu))) |
| 1556 | goto out; |
| 1557 | |
| 1558 | /* force the process onto the specified CPU */ |
| 1559 | if (migrate_task(p, dest_cpu, &req)) { |
| 1560 | /* Need to wait for migration thread (might exit: take ref). */ |
| 1561 | struct task_struct *mt = rq->migration_thread; |
| 1562 | get_task_struct(mt); |
| 1563 | task_rq_unlock(rq, &flags); |
| 1564 | wake_up_process(mt); |
| 1565 | put_task_struct(mt); |
| 1566 | wait_for_completion(&req.done); |
| 1567 | return; |
| 1568 | } |
| 1569 | out: |
| 1570 | task_rq_unlock(rq, &flags); |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * sched_exec(): find the highest-level, exec-balance-capable |
| 1575 | * domain and try to migrate the task to the least loaded CPU. |
| 1576 | * |
| 1577 | * execve() is a valuable balancing opportunity, because at this point |
| 1578 | * the task has the smallest effective memory and cache footprint. |
| 1579 | */ |
| 1580 | void sched_exec(void) |
| 1581 | { |
| 1582 | struct sched_domain *tmp, *sd = NULL; |
| 1583 | int new_cpu, this_cpu = get_cpu(); |
| 1584 | |
| 1585 | /* Prefer the current CPU if there's only this task running */ |
| 1586 | if (this_rq()->nr_running <= 1) |
| 1587 | goto out; |
| 1588 | |
| 1589 | for_each_domain(this_cpu, tmp) |
| 1590 | if (tmp->flags & SD_BALANCE_EXEC) |
| 1591 | sd = tmp; |
| 1592 | |
| 1593 | if (sd) { |
| 1594 | schedstat_inc(sd, sbe_attempts); |
| 1595 | new_cpu = find_idlest_cpu(current, this_cpu, sd); |
| 1596 | if (new_cpu != this_cpu) { |
| 1597 | schedstat_inc(sd, sbe_pushed); |
| 1598 | put_cpu(); |
| 1599 | sched_migrate_task(current, new_cpu); |
| 1600 | return; |
| 1601 | } |
| 1602 | } |
| 1603 | out: |
| 1604 | put_cpu(); |
| 1605 | } |
| 1606 | |
| 1607 | /* |
| 1608 | * pull_task - move a task from a remote runqueue to the local runqueue. |
| 1609 | * Both runqueues must be locked. |
| 1610 | */ |
| 1611 | static inline |
| 1612 | void pull_task(runqueue_t *src_rq, prio_array_t *src_array, task_t *p, |
| 1613 | runqueue_t *this_rq, prio_array_t *this_array, int this_cpu) |
| 1614 | { |
| 1615 | dequeue_task(p, src_array); |
| 1616 | src_rq->nr_running--; |
| 1617 | set_task_cpu(p, this_cpu); |
| 1618 | this_rq->nr_running++; |
| 1619 | enqueue_task(p, this_array); |
| 1620 | p->timestamp = (p->timestamp - src_rq->timestamp_last_tick) |
| 1621 | + this_rq->timestamp_last_tick; |
| 1622 | /* |
| 1623 | * Note that idle threads have a prio of MAX_PRIO, for this test |
| 1624 | * to be always true for them. |
| 1625 | */ |
| 1626 | if (TASK_PREEMPTS_CURR(p, this_rq)) |
| 1627 | resched_task(this_rq->curr); |
| 1628 | } |
| 1629 | |
| 1630 | /* |
| 1631 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 1632 | */ |
| 1633 | static inline |
| 1634 | int can_migrate_task(task_t *p, runqueue_t *rq, int this_cpu, |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1635 | struct sched_domain *sd, enum idle_type idle, int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | { |
| 1637 | /* |
| 1638 | * We do not migrate tasks that are: |
| 1639 | * 1) running (obviously), or |
| 1640 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 1641 | * 3) are cache-hot on their current CPU. |
| 1642 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | if (!cpu_isset(this_cpu, p->cpus_allowed)) |
| 1644 | return 0; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1645 | *all_pinned = 0; |
| 1646 | |
| 1647 | if (task_running(rq, p)) |
| 1648 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | |
| 1650 | /* |
| 1651 | * Aggressive migration if: |
| 1652 | * 1) the [whole] cpu is idle, or |
| 1653 | * 2) too many balance attempts have failed. |
| 1654 | */ |
| 1655 | |
| 1656 | if (cpu_and_siblings_are_idle(this_cpu) || \ |
| 1657 | sd->nr_balance_failed > sd->cache_nice_tries) |
| 1658 | return 1; |
| 1659 | |
| 1660 | if (task_hot(p, rq->timestamp_last_tick, sd)) |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1661 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | return 1; |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * move_tasks tries to move up to max_nr_move tasks from busiest to this_rq, |
| 1667 | * as part of a balancing operation within "domain". Returns the number of |
| 1668 | * tasks moved. |
| 1669 | * |
| 1670 | * Called with both runqueues locked. |
| 1671 | */ |
| 1672 | static int move_tasks(runqueue_t *this_rq, int this_cpu, runqueue_t *busiest, |
| 1673 | unsigned long max_nr_move, struct sched_domain *sd, |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1674 | enum idle_type idle, int *all_pinned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | { |
| 1676 | prio_array_t *array, *dst_array; |
| 1677 | struct list_head *head, *curr; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1678 | int idx, pulled = 0, pinned = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | task_t *tmp; |
| 1680 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1681 | if (max_nr_move == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | goto out; |
| 1683 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1684 | pinned = 1; |
| 1685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | /* |
| 1687 | * We first consider expired tasks. Those will likely not be |
| 1688 | * executed in the near future, and they are most likely to |
| 1689 | * be cache-cold, thus switching CPUs has the least effect |
| 1690 | * on them. |
| 1691 | */ |
| 1692 | if (busiest->expired->nr_active) { |
| 1693 | array = busiest->expired; |
| 1694 | dst_array = this_rq->expired; |
| 1695 | } else { |
| 1696 | array = busiest->active; |
| 1697 | dst_array = this_rq->active; |
| 1698 | } |
| 1699 | |
| 1700 | new_array: |
| 1701 | /* Start searching at priority 0: */ |
| 1702 | idx = 0; |
| 1703 | skip_bitmap: |
| 1704 | if (!idx) |
| 1705 | idx = sched_find_first_bit(array->bitmap); |
| 1706 | else |
| 1707 | idx = find_next_bit(array->bitmap, MAX_PRIO, idx); |
| 1708 | if (idx >= MAX_PRIO) { |
| 1709 | if (array == busiest->expired && busiest->active->nr_active) { |
| 1710 | array = busiest->active; |
| 1711 | dst_array = this_rq->active; |
| 1712 | goto new_array; |
| 1713 | } |
| 1714 | goto out; |
| 1715 | } |
| 1716 | |
| 1717 | head = array->queue + idx; |
| 1718 | curr = head->prev; |
| 1719 | skip_queue: |
| 1720 | tmp = list_entry(curr, task_t, run_list); |
| 1721 | |
| 1722 | curr = curr->prev; |
| 1723 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1724 | if (!can_migrate_task(tmp, busiest, this_cpu, sd, idle, &pinned)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | if (curr != head) |
| 1726 | goto skip_queue; |
| 1727 | idx++; |
| 1728 | goto skip_bitmap; |
| 1729 | } |
| 1730 | |
| 1731 | #ifdef CONFIG_SCHEDSTATS |
| 1732 | if (task_hot(tmp, busiest->timestamp_last_tick, sd)) |
| 1733 | schedstat_inc(sd, lb_hot_gained[idle]); |
| 1734 | #endif |
| 1735 | |
| 1736 | pull_task(busiest, array, tmp, this_rq, dst_array, this_cpu); |
| 1737 | pulled++; |
| 1738 | |
| 1739 | /* We only want to steal up to the prescribed number of tasks. */ |
| 1740 | if (pulled < max_nr_move) { |
| 1741 | if (curr != head) |
| 1742 | goto skip_queue; |
| 1743 | idx++; |
| 1744 | goto skip_bitmap; |
| 1745 | } |
| 1746 | out: |
| 1747 | /* |
| 1748 | * Right now, this is the only place pull_task() is called, |
| 1749 | * so we can safely collect pull_task() stats here rather than |
| 1750 | * inside pull_task(). |
| 1751 | */ |
| 1752 | schedstat_add(sd, lb_gained[idle], pulled); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1753 | |
| 1754 | if (all_pinned) |
| 1755 | *all_pinned = pinned; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | return pulled; |
| 1757 | } |
| 1758 | |
| 1759 | /* |
| 1760 | * find_busiest_group finds and returns the busiest CPU group within the |
| 1761 | * domain. It calculates and returns the number of tasks which should be |
| 1762 | * moved to restore balance via the imbalance parameter. |
| 1763 | */ |
| 1764 | static struct sched_group * |
| 1765 | find_busiest_group(struct sched_domain *sd, int this_cpu, |
| 1766 | unsigned long *imbalance, enum idle_type idle) |
| 1767 | { |
| 1768 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; |
| 1769 | unsigned long max_load, avg_load, total_load, this_load, total_pwr; |
| 1770 | |
| 1771 | max_load = this_load = total_load = total_pwr = 0; |
| 1772 | |
| 1773 | do { |
| 1774 | unsigned long load; |
| 1775 | int local_group; |
| 1776 | int i; |
| 1777 | |
| 1778 | local_group = cpu_isset(this_cpu, group->cpumask); |
| 1779 | |
| 1780 | /* Tally up the load of all CPUs in the group */ |
| 1781 | avg_load = 0; |
| 1782 | |
| 1783 | for_each_cpu_mask(i, group->cpumask) { |
| 1784 | /* Bias balancing toward cpus of our domain */ |
| 1785 | if (local_group) |
| 1786 | load = target_load(i); |
| 1787 | else |
| 1788 | load = source_load(i); |
| 1789 | |
| 1790 | avg_load += load; |
| 1791 | } |
| 1792 | |
| 1793 | total_load += avg_load; |
| 1794 | total_pwr += group->cpu_power; |
| 1795 | |
| 1796 | /* Adjust by relative CPU power of the group */ |
| 1797 | avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power; |
| 1798 | |
| 1799 | if (local_group) { |
| 1800 | this_load = avg_load; |
| 1801 | this = group; |
| 1802 | goto nextgroup; |
| 1803 | } else if (avg_load > max_load) { |
| 1804 | max_load = avg_load; |
| 1805 | busiest = group; |
| 1806 | } |
| 1807 | nextgroup: |
| 1808 | group = group->next; |
| 1809 | } while (group != sd->groups); |
| 1810 | |
| 1811 | if (!busiest || this_load >= max_load) |
| 1812 | goto out_balanced; |
| 1813 | |
| 1814 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; |
| 1815 | |
| 1816 | if (this_load >= avg_load || |
| 1817 | 100*max_load <= sd->imbalance_pct*this_load) |
| 1818 | goto out_balanced; |
| 1819 | |
| 1820 | /* |
| 1821 | * We're trying to get all the cpus to the average_load, so we don't |
| 1822 | * want to push ourselves above the average load, nor do we wish to |
| 1823 | * reduce the max loaded cpu below the average load, as either of these |
| 1824 | * actions would just result in more rebalancing later, and ping-pong |
| 1825 | * tasks around. Thus we look for the minimum possible imbalance. |
| 1826 | * Negative imbalances (*we* are more loaded than anyone else) will |
| 1827 | * be counted as no imbalance for these purposes -- we can't fix that |
| 1828 | * by pulling tasks to us. Be careful of negative numbers as they'll |
| 1829 | * appear as very large values with unsigned longs. |
| 1830 | */ |
| 1831 | /* How much load to actually move to equalise the imbalance */ |
| 1832 | *imbalance = min((max_load - avg_load) * busiest->cpu_power, |
| 1833 | (avg_load - this_load) * this->cpu_power) |
| 1834 | / SCHED_LOAD_SCALE; |
| 1835 | |
| 1836 | if (*imbalance < SCHED_LOAD_SCALE) { |
| 1837 | unsigned long pwr_now = 0, pwr_move = 0; |
| 1838 | unsigned long tmp; |
| 1839 | |
| 1840 | if (max_load - this_load >= SCHED_LOAD_SCALE*2) { |
| 1841 | *imbalance = 1; |
| 1842 | return busiest; |
| 1843 | } |
| 1844 | |
| 1845 | /* |
| 1846 | * OK, we don't have enough imbalance to justify moving tasks, |
| 1847 | * however we may be able to increase total CPU power used by |
| 1848 | * moving them. |
| 1849 | */ |
| 1850 | |
| 1851 | pwr_now += busiest->cpu_power*min(SCHED_LOAD_SCALE, max_load); |
| 1852 | pwr_now += this->cpu_power*min(SCHED_LOAD_SCALE, this_load); |
| 1853 | pwr_now /= SCHED_LOAD_SCALE; |
| 1854 | |
| 1855 | /* Amount of load we'd subtract */ |
| 1856 | tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/busiest->cpu_power; |
| 1857 | if (max_load > tmp) |
| 1858 | pwr_move += busiest->cpu_power*min(SCHED_LOAD_SCALE, |
| 1859 | max_load - tmp); |
| 1860 | |
| 1861 | /* Amount of load we'd add */ |
| 1862 | if (max_load*busiest->cpu_power < |
| 1863 | SCHED_LOAD_SCALE*SCHED_LOAD_SCALE) |
| 1864 | tmp = max_load*busiest->cpu_power/this->cpu_power; |
| 1865 | else |
| 1866 | tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/this->cpu_power; |
| 1867 | pwr_move += this->cpu_power*min(SCHED_LOAD_SCALE, this_load + tmp); |
| 1868 | pwr_move /= SCHED_LOAD_SCALE; |
| 1869 | |
| 1870 | /* Move if we gain throughput */ |
| 1871 | if (pwr_move <= pwr_now) |
| 1872 | goto out_balanced; |
| 1873 | |
| 1874 | *imbalance = 1; |
| 1875 | return busiest; |
| 1876 | } |
| 1877 | |
| 1878 | /* Get rid of the scaling factor, rounding down as we divide */ |
| 1879 | *imbalance = *imbalance / SCHED_LOAD_SCALE; |
| 1880 | |
| 1881 | return busiest; |
| 1882 | |
| 1883 | out_balanced: |
| 1884 | if (busiest && (idle == NEWLY_IDLE || |
| 1885 | (idle == SCHED_IDLE && max_load > SCHED_LOAD_SCALE)) ) { |
| 1886 | *imbalance = 1; |
| 1887 | return busiest; |
| 1888 | } |
| 1889 | |
| 1890 | *imbalance = 0; |
| 1891 | return NULL; |
| 1892 | } |
| 1893 | |
| 1894 | /* |
| 1895 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 1896 | */ |
| 1897 | static runqueue_t *find_busiest_queue(struct sched_group *group) |
| 1898 | { |
| 1899 | unsigned long load, max_load = 0; |
| 1900 | runqueue_t *busiest = NULL; |
| 1901 | int i; |
| 1902 | |
| 1903 | for_each_cpu_mask(i, group->cpumask) { |
| 1904 | load = source_load(i); |
| 1905 | |
| 1906 | if (load > max_load) { |
| 1907 | max_load = load; |
| 1908 | busiest = cpu_rq(i); |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | return busiest; |
| 1913 | } |
| 1914 | |
| 1915 | /* |
| 1916 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 1917 | * tasks if there is an imbalance. |
| 1918 | * |
| 1919 | * Called with this_rq unlocked. |
| 1920 | */ |
| 1921 | static int load_balance(int this_cpu, runqueue_t *this_rq, |
| 1922 | struct sched_domain *sd, enum idle_type idle) |
| 1923 | { |
| 1924 | struct sched_group *group; |
| 1925 | runqueue_t *busiest; |
| 1926 | unsigned long imbalance; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1927 | int nr_moved, all_pinned; |
| 1928 | int active_balance = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | |
| 1930 | spin_lock(&this_rq->lock); |
| 1931 | schedstat_inc(sd, lb_cnt[idle]); |
| 1932 | |
| 1933 | group = find_busiest_group(sd, this_cpu, &imbalance, idle); |
| 1934 | if (!group) { |
| 1935 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 1936 | goto out_balanced; |
| 1937 | } |
| 1938 | |
| 1939 | busiest = find_busiest_queue(group); |
| 1940 | if (!busiest) { |
| 1941 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 1942 | goto out_balanced; |
| 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * This should be "impossible", but since load |
| 1947 | * balancing is inherently racy and statistical, |
| 1948 | * it could happen in theory. |
| 1949 | */ |
| 1950 | if (unlikely(busiest == this_rq)) { |
| 1951 | WARN_ON(1); |
| 1952 | goto out_balanced; |
| 1953 | } |
| 1954 | |
| 1955 | schedstat_add(sd, lb_imbalance[idle], imbalance); |
| 1956 | |
| 1957 | nr_moved = 0; |
| 1958 | if (busiest->nr_running > 1) { |
| 1959 | /* |
| 1960 | * Attempt to move tasks. If find_busiest_group has found |
| 1961 | * an imbalance but busiest->nr_running <= 1, the group is |
| 1962 | * still unbalanced. nr_moved simply stays zero, so it is |
| 1963 | * correctly treated as an imbalance. |
| 1964 | */ |
| 1965 | double_lock_balance(this_rq, busiest); |
| 1966 | nr_moved = move_tasks(this_rq, this_cpu, busiest, |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1967 | imbalance, sd, idle, |
| 1968 | &all_pinned); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | spin_unlock(&busiest->lock); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1970 | |
| 1971 | /* All tasks on this runqueue were pinned by CPU affinity */ |
| 1972 | if (unlikely(all_pinned)) |
| 1973 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | spin_unlock(&this_rq->lock); |
| 1977 | |
| 1978 | if (!nr_moved) { |
| 1979 | schedstat_inc(sd, lb_failed[idle]); |
| 1980 | sd->nr_balance_failed++; |
| 1981 | |
| 1982 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | |
| 1984 | spin_lock(&busiest->lock); |
| 1985 | if (!busiest->active_balance) { |
| 1986 | busiest->active_balance = 1; |
| 1987 | busiest->push_cpu = this_cpu; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1988 | active_balance = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | } |
| 1990 | spin_unlock(&busiest->lock); |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 1991 | if (active_balance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | wake_up_process(busiest->migration_thread); |
| 1993 | |
| 1994 | /* |
| 1995 | * We've kicked active balancing, reset the failure |
| 1996 | * counter. |
| 1997 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame^] | 1998 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | } |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2000 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | sd->nr_balance_failed = 0; |
| 2002 | |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2003 | if (likely(!active_balance)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | /* We were unbalanced, so reset the balancing interval */ |
| 2005 | sd->balance_interval = sd->min_interval; |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2006 | } else { |
| 2007 | /* |
| 2008 | * If we've begun active balancing, start to back off. This |
| 2009 | * case may not be covered by the all_pinned logic if there |
| 2010 | * is only 1 task on the busy runqueue (because we don't call |
| 2011 | * move_tasks). |
| 2012 | */ |
| 2013 | if (sd->balance_interval < sd->max_interval) |
| 2014 | sd->balance_interval *= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | return nr_moved; |
| 2018 | |
| 2019 | out_balanced: |
| 2020 | spin_unlock(&this_rq->lock); |
| 2021 | |
| 2022 | schedstat_inc(sd, lb_balanced[idle]); |
| 2023 | |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2024 | sd->nr_balance_failed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | /* tune up the balancing interval */ |
| 2026 | if (sd->balance_interval < sd->max_interval) |
| 2027 | sd->balance_interval *= 2; |
| 2028 | |
| 2029 | return 0; |
| 2030 | } |
| 2031 | |
| 2032 | /* |
| 2033 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 2034 | * tasks if there is an imbalance. |
| 2035 | * |
| 2036 | * Called from schedule when this_rq is about to become idle (NEWLY_IDLE). |
| 2037 | * this_rq is locked. |
| 2038 | */ |
| 2039 | static int load_balance_newidle(int this_cpu, runqueue_t *this_rq, |
| 2040 | struct sched_domain *sd) |
| 2041 | { |
| 2042 | struct sched_group *group; |
| 2043 | runqueue_t *busiest = NULL; |
| 2044 | unsigned long imbalance; |
| 2045 | int nr_moved = 0; |
| 2046 | |
| 2047 | schedstat_inc(sd, lb_cnt[NEWLY_IDLE]); |
| 2048 | group = find_busiest_group(sd, this_cpu, &imbalance, NEWLY_IDLE); |
| 2049 | if (!group) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | schedstat_inc(sd, lb_nobusyg[NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2051 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | busiest = find_busiest_queue(group); |
| 2055 | if (!busiest || busiest == this_rq) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | schedstat_inc(sd, lb_nobusyq[NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2057 | goto out_balanced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | /* Attempt to move tasks */ |
| 2061 | double_lock_balance(this_rq, busiest); |
| 2062 | |
| 2063 | schedstat_add(sd, lb_imbalance[NEWLY_IDLE], imbalance); |
| 2064 | nr_moved = move_tasks(this_rq, this_cpu, busiest, |
Nick Piggin | 8102679 | 2005-06-25 14:57:07 -0700 | [diff] [blame] | 2065 | imbalance, sd, NEWLY_IDLE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | if (!nr_moved) |
| 2067 | schedstat_inc(sd, lb_failed[NEWLY_IDLE]); |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2068 | else |
| 2069 | sd->nr_balance_failed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
| 2071 | spin_unlock(&busiest->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | return nr_moved; |
Nick Piggin | 16cfb1c | 2005-06-25 14:57:08 -0700 | [diff] [blame] | 2073 | |
| 2074 | out_balanced: |
| 2075 | schedstat_inc(sd, lb_balanced[NEWLY_IDLE]); |
| 2076 | sd->nr_balance_failed = 0; |
| 2077 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | /* |
| 2081 | * idle_balance is called by schedule() if this_cpu is about to become |
| 2082 | * idle. Attempts to pull tasks from other CPUs. |
| 2083 | */ |
| 2084 | static inline void idle_balance(int this_cpu, runqueue_t *this_rq) |
| 2085 | { |
| 2086 | struct sched_domain *sd; |
| 2087 | |
| 2088 | for_each_domain(this_cpu, sd) { |
| 2089 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
| 2090 | if (load_balance_newidle(this_cpu, this_rq, sd)) { |
| 2091 | /* We've pulled tasks over so stop searching */ |
| 2092 | break; |
| 2093 | } |
| 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | /* |
| 2099 | * active_load_balance is run by migration threads. It pushes running tasks |
| 2100 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be |
| 2101 | * running on each physical CPU where possible, and avoids physical / |
| 2102 | * logical imbalances. |
| 2103 | * |
| 2104 | * Called with busiest_rq locked. |
| 2105 | */ |
| 2106 | static void active_load_balance(runqueue_t *busiest_rq, int busiest_cpu) |
| 2107 | { |
| 2108 | struct sched_domain *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | runqueue_t *target_rq; |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame^] | 2110 | int target_cpu = busiest_rq->push_cpu; |
| 2111 | |
| 2112 | if (busiest_rq->nr_running <= 1) |
| 2113 | /* no task to move */ |
| 2114 | return; |
| 2115 | |
| 2116 | target_rq = cpu_rq(target_cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | |
| 2118 | /* |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame^] | 2119 | * This condition is "impossible", if it occurs |
| 2120 | * we need to fix it. Originally reported by |
| 2121 | * Bjorn Helgaas on a 128-cpu setup. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | */ |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame^] | 2123 | BUG_ON(busiest_rq == target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame^] | 2125 | /* move a task from busiest_rq to target_rq */ |
| 2126 | double_lock_balance(busiest_rq, target_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame^] | 2128 | /* Search for an sd spanning us and the target CPU. */ |
| 2129 | for_each_domain(target_cpu, sd) |
| 2130 | if ((sd->flags & SD_LOAD_BALANCE) && |
| 2131 | cpu_isset(busiest_cpu, sd->span)) |
| 2132 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame^] | 2134 | if (unlikely(sd == NULL)) |
| 2135 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | |
Nick Piggin | 3950745 | 2005-06-25 14:57:09 -0700 | [diff] [blame^] | 2137 | schedstat_inc(sd, alb_cnt); |
| 2138 | |
| 2139 | if (move_tasks(target_rq, target_cpu, busiest_rq, 1, sd, SCHED_IDLE, NULL)) |
| 2140 | schedstat_inc(sd, alb_pushed); |
| 2141 | else |
| 2142 | schedstat_inc(sd, alb_failed); |
| 2143 | out: |
| 2144 | spin_unlock(&target_rq->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | /* |
| 2148 | * rebalance_tick will get called every timer tick, on every CPU. |
| 2149 | * |
| 2150 | * It checks each scheduling domain to see if it is due to be balanced, |
| 2151 | * and initiates a balancing operation if so. |
| 2152 | * |
| 2153 | * Balancing parameters are set up in arch_init_sched_domains. |
| 2154 | */ |
| 2155 | |
| 2156 | /* Don't have all balancing operations going off at once */ |
| 2157 | #define CPU_OFFSET(cpu) (HZ * cpu / NR_CPUS) |
| 2158 | |
| 2159 | static void rebalance_tick(int this_cpu, runqueue_t *this_rq, |
| 2160 | enum idle_type idle) |
| 2161 | { |
| 2162 | unsigned long old_load, this_load; |
| 2163 | unsigned long j = jiffies + CPU_OFFSET(this_cpu); |
| 2164 | struct sched_domain *sd; |
| 2165 | |
| 2166 | /* Update our load */ |
| 2167 | old_load = this_rq->cpu_load; |
| 2168 | this_load = this_rq->nr_running * SCHED_LOAD_SCALE; |
| 2169 | /* |
| 2170 | * Round up the averaging division if load is increasing. This |
| 2171 | * prevents us from getting stuck on 9 if the load is 10, for |
| 2172 | * example. |
| 2173 | */ |
| 2174 | if (this_load > old_load) |
| 2175 | old_load++; |
| 2176 | this_rq->cpu_load = (old_load + this_load) / 2; |
| 2177 | |
| 2178 | for_each_domain(this_cpu, sd) { |
| 2179 | unsigned long interval; |
| 2180 | |
| 2181 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 2182 | continue; |
| 2183 | |
| 2184 | interval = sd->balance_interval; |
| 2185 | if (idle != SCHED_IDLE) |
| 2186 | interval *= sd->busy_factor; |
| 2187 | |
| 2188 | /* scale ms to jiffies */ |
| 2189 | interval = msecs_to_jiffies(interval); |
| 2190 | if (unlikely(!interval)) |
| 2191 | interval = 1; |
| 2192 | |
| 2193 | if (j - sd->last_balance >= interval) { |
| 2194 | if (load_balance(this_cpu, this_rq, sd, idle)) { |
| 2195 | /* We've pulled tasks over so no longer idle */ |
| 2196 | idle = NOT_IDLE; |
| 2197 | } |
| 2198 | sd->last_balance += interval; |
| 2199 | } |
| 2200 | } |
| 2201 | } |
| 2202 | #else |
| 2203 | /* |
| 2204 | * on UP we do not need to balance between CPUs: |
| 2205 | */ |
| 2206 | static inline void rebalance_tick(int cpu, runqueue_t *rq, enum idle_type idle) |
| 2207 | { |
| 2208 | } |
| 2209 | static inline void idle_balance(int cpu, runqueue_t *rq) |
| 2210 | { |
| 2211 | } |
| 2212 | #endif |
| 2213 | |
| 2214 | static inline int wake_priority_sleeper(runqueue_t *rq) |
| 2215 | { |
| 2216 | int ret = 0; |
| 2217 | #ifdef CONFIG_SCHED_SMT |
| 2218 | spin_lock(&rq->lock); |
| 2219 | /* |
| 2220 | * If an SMT sibling task has been put to sleep for priority |
| 2221 | * reasons reschedule the idle task to see if it can now run. |
| 2222 | */ |
| 2223 | if (rq->nr_running) { |
| 2224 | resched_task(rq->idle); |
| 2225 | ret = 1; |
| 2226 | } |
| 2227 | spin_unlock(&rq->lock); |
| 2228 | #endif |
| 2229 | return ret; |
| 2230 | } |
| 2231 | |
| 2232 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
| 2233 | |
| 2234 | EXPORT_PER_CPU_SYMBOL(kstat); |
| 2235 | |
| 2236 | /* |
| 2237 | * This is called on clock ticks and on context switches. |
| 2238 | * Bank in p->sched_time the ns elapsed since the last tick or switch. |
| 2239 | */ |
| 2240 | static inline void update_cpu_clock(task_t *p, runqueue_t *rq, |
| 2241 | unsigned long long now) |
| 2242 | { |
| 2243 | unsigned long long last = max(p->timestamp, rq->timestamp_last_tick); |
| 2244 | p->sched_time += now - last; |
| 2245 | } |
| 2246 | |
| 2247 | /* |
| 2248 | * Return current->sched_time plus any more ns on the sched_clock |
| 2249 | * that have not yet been banked. |
| 2250 | */ |
| 2251 | unsigned long long current_sched_time(const task_t *tsk) |
| 2252 | { |
| 2253 | unsigned long long ns; |
| 2254 | unsigned long flags; |
| 2255 | local_irq_save(flags); |
| 2256 | ns = max(tsk->timestamp, task_rq(tsk)->timestamp_last_tick); |
| 2257 | ns = tsk->sched_time + (sched_clock() - ns); |
| 2258 | local_irq_restore(flags); |
| 2259 | return ns; |
| 2260 | } |
| 2261 | |
| 2262 | /* |
| 2263 | * We place interactive tasks back into the active array, if possible. |
| 2264 | * |
| 2265 | * To guarantee that this does not starve expired tasks we ignore the |
| 2266 | * interactivity of a task if the first expired task had to wait more |
| 2267 | * than a 'reasonable' amount of time. This deadline timeout is |
| 2268 | * load-dependent, as the frequency of array switched decreases with |
| 2269 | * increasing number of running tasks. We also ignore the interactivity |
| 2270 | * if a better static_prio task has expired: |
| 2271 | */ |
| 2272 | #define EXPIRED_STARVING(rq) \ |
| 2273 | ((STARVATION_LIMIT && ((rq)->expired_timestamp && \ |
| 2274 | (jiffies - (rq)->expired_timestamp >= \ |
| 2275 | STARVATION_LIMIT * ((rq)->nr_running) + 1))) || \ |
| 2276 | ((rq)->curr->static_prio > (rq)->best_expired_prio)) |
| 2277 | |
| 2278 | /* |
| 2279 | * Account user cpu time to a process. |
| 2280 | * @p: the process that the cpu time gets accounted to |
| 2281 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 2282 | * @cputime: the cpu time spent in user space since the last update |
| 2283 | */ |
| 2284 | void account_user_time(struct task_struct *p, cputime_t cputime) |
| 2285 | { |
| 2286 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 2287 | cputime64_t tmp; |
| 2288 | |
| 2289 | p->utime = cputime_add(p->utime, cputime); |
| 2290 | |
| 2291 | /* Add user time to cpustat. */ |
| 2292 | tmp = cputime_to_cputime64(cputime); |
| 2293 | if (TASK_NICE(p) > 0) |
| 2294 | cpustat->nice = cputime64_add(cpustat->nice, tmp); |
| 2295 | else |
| 2296 | cpustat->user = cputime64_add(cpustat->user, tmp); |
| 2297 | } |
| 2298 | |
| 2299 | /* |
| 2300 | * Account system cpu time to a process. |
| 2301 | * @p: the process that the cpu time gets accounted to |
| 2302 | * @hardirq_offset: the offset to subtract from hardirq_count() |
| 2303 | * @cputime: the cpu time spent in kernel space since the last update |
| 2304 | */ |
| 2305 | void account_system_time(struct task_struct *p, int hardirq_offset, |
| 2306 | cputime_t cputime) |
| 2307 | { |
| 2308 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 2309 | runqueue_t *rq = this_rq(); |
| 2310 | cputime64_t tmp; |
| 2311 | |
| 2312 | p->stime = cputime_add(p->stime, cputime); |
| 2313 | |
| 2314 | /* Add system time to cpustat. */ |
| 2315 | tmp = cputime_to_cputime64(cputime); |
| 2316 | if (hardirq_count() - hardirq_offset) |
| 2317 | cpustat->irq = cputime64_add(cpustat->irq, tmp); |
| 2318 | else if (softirq_count()) |
| 2319 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); |
| 2320 | else if (p != rq->idle) |
| 2321 | cpustat->system = cputime64_add(cpustat->system, tmp); |
| 2322 | else if (atomic_read(&rq->nr_iowait) > 0) |
| 2323 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 2324 | else |
| 2325 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 2326 | /* Account for system time used */ |
| 2327 | acct_update_integrals(p); |
| 2328 | /* Update rss highwater mark */ |
| 2329 | update_mem_hiwater(p); |
| 2330 | } |
| 2331 | |
| 2332 | /* |
| 2333 | * Account for involuntary wait time. |
| 2334 | * @p: the process from which the cpu time has been stolen |
| 2335 | * @steal: the cpu time spent in involuntary wait |
| 2336 | */ |
| 2337 | void account_steal_time(struct task_struct *p, cputime_t steal) |
| 2338 | { |
| 2339 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; |
| 2340 | cputime64_t tmp = cputime_to_cputime64(steal); |
| 2341 | runqueue_t *rq = this_rq(); |
| 2342 | |
| 2343 | if (p == rq->idle) { |
| 2344 | p->stime = cputime_add(p->stime, steal); |
| 2345 | if (atomic_read(&rq->nr_iowait) > 0) |
| 2346 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
| 2347 | else |
| 2348 | cpustat->idle = cputime64_add(cpustat->idle, tmp); |
| 2349 | } else |
| 2350 | cpustat->steal = cputime64_add(cpustat->steal, tmp); |
| 2351 | } |
| 2352 | |
| 2353 | /* |
| 2354 | * This function gets called by the timer code, with HZ frequency. |
| 2355 | * We call it with interrupts disabled. |
| 2356 | * |
| 2357 | * It also gets called by the fork code, when changing the parent's |
| 2358 | * timeslices. |
| 2359 | */ |
| 2360 | void scheduler_tick(void) |
| 2361 | { |
| 2362 | int cpu = smp_processor_id(); |
| 2363 | runqueue_t *rq = this_rq(); |
| 2364 | task_t *p = current; |
| 2365 | unsigned long long now = sched_clock(); |
| 2366 | |
| 2367 | update_cpu_clock(p, rq, now); |
| 2368 | |
| 2369 | rq->timestamp_last_tick = now; |
| 2370 | |
| 2371 | if (p == rq->idle) { |
| 2372 | if (wake_priority_sleeper(rq)) |
| 2373 | goto out; |
| 2374 | rebalance_tick(cpu, rq, SCHED_IDLE); |
| 2375 | return; |
| 2376 | } |
| 2377 | |
| 2378 | /* Task might have expired already, but not scheduled off yet */ |
| 2379 | if (p->array != rq->active) { |
| 2380 | set_tsk_need_resched(p); |
| 2381 | goto out; |
| 2382 | } |
| 2383 | spin_lock(&rq->lock); |
| 2384 | /* |
| 2385 | * The task was running during this tick - update the |
| 2386 | * time slice counter. Note: we do not update a thread's |
| 2387 | * priority until it either goes to sleep or uses up its |
| 2388 | * timeslice. This makes it possible for interactive tasks |
| 2389 | * to use up their timeslices at their highest priority levels. |
| 2390 | */ |
| 2391 | if (rt_task(p)) { |
| 2392 | /* |
| 2393 | * RR tasks need a special form of timeslice management. |
| 2394 | * FIFO tasks have no timeslices. |
| 2395 | */ |
| 2396 | if ((p->policy == SCHED_RR) && !--p->time_slice) { |
| 2397 | p->time_slice = task_timeslice(p); |
| 2398 | p->first_time_slice = 0; |
| 2399 | set_tsk_need_resched(p); |
| 2400 | |
| 2401 | /* put it at the end of the queue: */ |
| 2402 | requeue_task(p, rq->active); |
| 2403 | } |
| 2404 | goto out_unlock; |
| 2405 | } |
| 2406 | if (!--p->time_slice) { |
| 2407 | dequeue_task(p, rq->active); |
| 2408 | set_tsk_need_resched(p); |
| 2409 | p->prio = effective_prio(p); |
| 2410 | p->time_slice = task_timeslice(p); |
| 2411 | p->first_time_slice = 0; |
| 2412 | |
| 2413 | if (!rq->expired_timestamp) |
| 2414 | rq->expired_timestamp = jiffies; |
| 2415 | if (!TASK_INTERACTIVE(p) || EXPIRED_STARVING(rq)) { |
| 2416 | enqueue_task(p, rq->expired); |
| 2417 | if (p->static_prio < rq->best_expired_prio) |
| 2418 | rq->best_expired_prio = p->static_prio; |
| 2419 | } else |
| 2420 | enqueue_task(p, rq->active); |
| 2421 | } else { |
| 2422 | /* |
| 2423 | * Prevent a too long timeslice allowing a task to monopolize |
| 2424 | * the CPU. We do this by splitting up the timeslice into |
| 2425 | * smaller pieces. |
| 2426 | * |
| 2427 | * Note: this does not mean the task's timeslices expire or |
| 2428 | * get lost in any way, they just might be preempted by |
| 2429 | * another task of equal priority. (one with higher |
| 2430 | * priority would have preempted this task already.) We |
| 2431 | * requeue this task to the end of the list on this priority |
| 2432 | * level, which is in essence a round-robin of tasks with |
| 2433 | * equal priority. |
| 2434 | * |
| 2435 | * This only applies to tasks in the interactive |
| 2436 | * delta range with at least TIMESLICE_GRANULARITY to requeue. |
| 2437 | */ |
| 2438 | if (TASK_INTERACTIVE(p) && !((task_timeslice(p) - |
| 2439 | p->time_slice) % TIMESLICE_GRANULARITY(p)) && |
| 2440 | (p->time_slice >= TIMESLICE_GRANULARITY(p)) && |
| 2441 | (p->array == rq->active)) { |
| 2442 | |
| 2443 | requeue_task(p, rq->active); |
| 2444 | set_tsk_need_resched(p); |
| 2445 | } |
| 2446 | } |
| 2447 | out_unlock: |
| 2448 | spin_unlock(&rq->lock); |
| 2449 | out: |
| 2450 | rebalance_tick(cpu, rq, NOT_IDLE); |
| 2451 | } |
| 2452 | |
| 2453 | #ifdef CONFIG_SCHED_SMT |
| 2454 | static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq) |
| 2455 | { |
| 2456 | struct sched_domain *sd = this_rq->sd; |
| 2457 | cpumask_t sibling_map; |
| 2458 | int i; |
| 2459 | |
| 2460 | if (!(sd->flags & SD_SHARE_CPUPOWER)) |
| 2461 | return; |
| 2462 | |
| 2463 | /* |
| 2464 | * Unlock the current runqueue because we have to lock in |
| 2465 | * CPU order to avoid deadlocks. Caller knows that we might |
| 2466 | * unlock. We keep IRQs disabled. |
| 2467 | */ |
| 2468 | spin_unlock(&this_rq->lock); |
| 2469 | |
| 2470 | sibling_map = sd->span; |
| 2471 | |
| 2472 | for_each_cpu_mask(i, sibling_map) |
| 2473 | spin_lock(&cpu_rq(i)->lock); |
| 2474 | /* |
| 2475 | * We clear this CPU from the mask. This both simplifies the |
| 2476 | * inner loop and keps this_rq locked when we exit: |
| 2477 | */ |
| 2478 | cpu_clear(this_cpu, sibling_map); |
| 2479 | |
| 2480 | for_each_cpu_mask(i, sibling_map) { |
| 2481 | runqueue_t *smt_rq = cpu_rq(i); |
| 2482 | |
| 2483 | /* |
| 2484 | * If an SMT sibling task is sleeping due to priority |
| 2485 | * reasons wake it up now. |
| 2486 | */ |
| 2487 | if (smt_rq->curr == smt_rq->idle && smt_rq->nr_running) |
| 2488 | resched_task(smt_rq->idle); |
| 2489 | } |
| 2490 | |
| 2491 | for_each_cpu_mask(i, sibling_map) |
| 2492 | spin_unlock(&cpu_rq(i)->lock); |
| 2493 | /* |
| 2494 | * We exit with this_cpu's rq still held and IRQs |
| 2495 | * still disabled: |
| 2496 | */ |
| 2497 | } |
| 2498 | |
| 2499 | static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq) |
| 2500 | { |
| 2501 | struct sched_domain *sd = this_rq->sd; |
| 2502 | cpumask_t sibling_map; |
| 2503 | prio_array_t *array; |
| 2504 | int ret = 0, i; |
| 2505 | task_t *p; |
| 2506 | |
| 2507 | if (!(sd->flags & SD_SHARE_CPUPOWER)) |
| 2508 | return 0; |
| 2509 | |
| 2510 | /* |
| 2511 | * The same locking rules and details apply as for |
| 2512 | * wake_sleeping_dependent(): |
| 2513 | */ |
| 2514 | spin_unlock(&this_rq->lock); |
| 2515 | sibling_map = sd->span; |
| 2516 | for_each_cpu_mask(i, sibling_map) |
| 2517 | spin_lock(&cpu_rq(i)->lock); |
| 2518 | cpu_clear(this_cpu, sibling_map); |
| 2519 | |
| 2520 | /* |
| 2521 | * Establish next task to be run - it might have gone away because |
| 2522 | * we released the runqueue lock above: |
| 2523 | */ |
| 2524 | if (!this_rq->nr_running) |
| 2525 | goto out_unlock; |
| 2526 | array = this_rq->active; |
| 2527 | if (!array->nr_active) |
| 2528 | array = this_rq->expired; |
| 2529 | BUG_ON(!array->nr_active); |
| 2530 | |
| 2531 | p = list_entry(array->queue[sched_find_first_bit(array->bitmap)].next, |
| 2532 | task_t, run_list); |
| 2533 | |
| 2534 | for_each_cpu_mask(i, sibling_map) { |
| 2535 | runqueue_t *smt_rq = cpu_rq(i); |
| 2536 | task_t *smt_curr = smt_rq->curr; |
| 2537 | |
| 2538 | /* |
| 2539 | * If a user task with lower static priority than the |
| 2540 | * running task on the SMT sibling is trying to schedule, |
| 2541 | * delay it till there is proportionately less timeslice |
| 2542 | * left of the sibling task to prevent a lower priority |
| 2543 | * task from using an unfair proportion of the |
| 2544 | * physical cpu's resources. -ck |
| 2545 | */ |
| 2546 | if (((smt_curr->time_slice * (100 - sd->per_cpu_gain) / 100) > |
| 2547 | task_timeslice(p) || rt_task(smt_curr)) && |
| 2548 | p->mm && smt_curr->mm && !rt_task(p)) |
| 2549 | ret = 1; |
| 2550 | |
| 2551 | /* |
| 2552 | * Reschedule a lower priority task on the SMT sibling, |
| 2553 | * or wake it up if it has been put to sleep for priority |
| 2554 | * reasons. |
| 2555 | */ |
| 2556 | if ((((p->time_slice * (100 - sd->per_cpu_gain) / 100) > |
| 2557 | task_timeslice(smt_curr) || rt_task(p)) && |
| 2558 | smt_curr->mm && p->mm && !rt_task(smt_curr)) || |
| 2559 | (smt_curr == smt_rq->idle && smt_rq->nr_running)) |
| 2560 | resched_task(smt_curr); |
| 2561 | } |
| 2562 | out_unlock: |
| 2563 | for_each_cpu_mask(i, sibling_map) |
| 2564 | spin_unlock(&cpu_rq(i)->lock); |
| 2565 | return ret; |
| 2566 | } |
| 2567 | #else |
| 2568 | static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq) |
| 2569 | { |
| 2570 | } |
| 2571 | |
| 2572 | static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq) |
| 2573 | { |
| 2574 | return 0; |
| 2575 | } |
| 2576 | #endif |
| 2577 | |
| 2578 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) |
| 2579 | |
| 2580 | void fastcall add_preempt_count(int val) |
| 2581 | { |
| 2582 | /* |
| 2583 | * Underflow? |
| 2584 | */ |
Jesper Juhl | be5b4fb | 2005-06-23 00:09:09 -0700 | [diff] [blame] | 2585 | BUG_ON((preempt_count() < 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | preempt_count() += val; |
| 2587 | /* |
| 2588 | * Spinlock count overflowing soon? |
| 2589 | */ |
| 2590 | BUG_ON((preempt_count() & PREEMPT_MASK) >= PREEMPT_MASK-10); |
| 2591 | } |
| 2592 | EXPORT_SYMBOL(add_preempt_count); |
| 2593 | |
| 2594 | void fastcall sub_preempt_count(int val) |
| 2595 | { |
| 2596 | /* |
| 2597 | * Underflow? |
| 2598 | */ |
| 2599 | BUG_ON(val > preempt_count()); |
| 2600 | /* |
| 2601 | * Is the spinlock portion underflowing? |
| 2602 | */ |
| 2603 | BUG_ON((val < PREEMPT_MASK) && !(preempt_count() & PREEMPT_MASK)); |
| 2604 | preempt_count() -= val; |
| 2605 | } |
| 2606 | EXPORT_SYMBOL(sub_preempt_count); |
| 2607 | |
| 2608 | #endif |
| 2609 | |
| 2610 | /* |
| 2611 | * schedule() is the main scheduler function. |
| 2612 | */ |
| 2613 | asmlinkage void __sched schedule(void) |
| 2614 | { |
| 2615 | long *switch_count; |
| 2616 | task_t *prev, *next; |
| 2617 | runqueue_t *rq; |
| 2618 | prio_array_t *array; |
| 2619 | struct list_head *queue; |
| 2620 | unsigned long long now; |
| 2621 | unsigned long run_time; |
| 2622 | int cpu, idx; |
| 2623 | |
| 2624 | /* |
| 2625 | * Test if we are atomic. Since do_exit() needs to call into |
| 2626 | * schedule() atomically, we ignore that path for now. |
| 2627 | * Otherwise, whine if we are scheduling when we should not be. |
| 2628 | */ |
| 2629 | if (likely(!current->exit_state)) { |
| 2630 | if (unlikely(in_atomic())) { |
| 2631 | printk(KERN_ERR "scheduling while atomic: " |
| 2632 | "%s/0x%08x/%d\n", |
| 2633 | current->comm, preempt_count(), current->pid); |
| 2634 | dump_stack(); |
| 2635 | } |
| 2636 | } |
| 2637 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
| 2638 | |
| 2639 | need_resched: |
| 2640 | preempt_disable(); |
| 2641 | prev = current; |
| 2642 | release_kernel_lock(prev); |
| 2643 | need_resched_nonpreemptible: |
| 2644 | rq = this_rq(); |
| 2645 | |
| 2646 | /* |
| 2647 | * The idle thread is not allowed to schedule! |
| 2648 | * Remove this check after it has been exercised a bit. |
| 2649 | */ |
| 2650 | if (unlikely(prev == rq->idle) && prev->state != TASK_RUNNING) { |
| 2651 | printk(KERN_ERR "bad: scheduling from the idle thread!\n"); |
| 2652 | dump_stack(); |
| 2653 | } |
| 2654 | |
| 2655 | schedstat_inc(rq, sched_cnt); |
| 2656 | now = sched_clock(); |
Ingo Molnar | 238628e | 2005-04-18 10:58:36 -0700 | [diff] [blame] | 2657 | if (likely((long long)(now - prev->timestamp) < NS_MAX_SLEEP_AVG)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | run_time = now - prev->timestamp; |
Ingo Molnar | 238628e | 2005-04-18 10:58:36 -0700 | [diff] [blame] | 2659 | if (unlikely((long long)(now - prev->timestamp) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | run_time = 0; |
| 2661 | } else |
| 2662 | run_time = NS_MAX_SLEEP_AVG; |
| 2663 | |
| 2664 | /* |
| 2665 | * Tasks charged proportionately less run_time at high sleep_avg to |
| 2666 | * delay them losing their interactive status |
| 2667 | */ |
| 2668 | run_time /= (CURRENT_BONUS(prev) ? : 1); |
| 2669 | |
| 2670 | spin_lock_irq(&rq->lock); |
| 2671 | |
| 2672 | if (unlikely(prev->flags & PF_DEAD)) |
| 2673 | prev->state = EXIT_DEAD; |
| 2674 | |
| 2675 | switch_count = &prev->nivcsw; |
| 2676 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
| 2677 | switch_count = &prev->nvcsw; |
| 2678 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && |
| 2679 | unlikely(signal_pending(prev)))) |
| 2680 | prev->state = TASK_RUNNING; |
| 2681 | else { |
| 2682 | if (prev->state == TASK_UNINTERRUPTIBLE) |
| 2683 | rq->nr_uninterruptible++; |
| 2684 | deactivate_task(prev, rq); |
| 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | cpu = smp_processor_id(); |
| 2689 | if (unlikely(!rq->nr_running)) { |
| 2690 | go_idle: |
| 2691 | idle_balance(cpu, rq); |
| 2692 | if (!rq->nr_running) { |
| 2693 | next = rq->idle; |
| 2694 | rq->expired_timestamp = 0; |
| 2695 | wake_sleeping_dependent(cpu, rq); |
| 2696 | /* |
| 2697 | * wake_sleeping_dependent() might have released |
| 2698 | * the runqueue, so break out if we got new |
| 2699 | * tasks meanwhile: |
| 2700 | */ |
| 2701 | if (!rq->nr_running) |
| 2702 | goto switch_tasks; |
| 2703 | } |
| 2704 | } else { |
| 2705 | if (dependent_sleeper(cpu, rq)) { |
| 2706 | next = rq->idle; |
| 2707 | goto switch_tasks; |
| 2708 | } |
| 2709 | /* |
| 2710 | * dependent_sleeper() releases and reacquires the runqueue |
| 2711 | * lock, hence go into the idle loop if the rq went |
| 2712 | * empty meanwhile: |
| 2713 | */ |
| 2714 | if (unlikely(!rq->nr_running)) |
| 2715 | goto go_idle; |
| 2716 | } |
| 2717 | |
| 2718 | array = rq->active; |
| 2719 | if (unlikely(!array->nr_active)) { |
| 2720 | /* |
| 2721 | * Switch the active and expired arrays. |
| 2722 | */ |
| 2723 | schedstat_inc(rq, sched_switch); |
| 2724 | rq->active = rq->expired; |
| 2725 | rq->expired = array; |
| 2726 | array = rq->active; |
| 2727 | rq->expired_timestamp = 0; |
| 2728 | rq->best_expired_prio = MAX_PRIO; |
| 2729 | } |
| 2730 | |
| 2731 | idx = sched_find_first_bit(array->bitmap); |
| 2732 | queue = array->queue + idx; |
| 2733 | next = list_entry(queue->next, task_t, run_list); |
| 2734 | |
| 2735 | if (!rt_task(next) && next->activated > 0) { |
| 2736 | unsigned long long delta = now - next->timestamp; |
Ingo Molnar | 238628e | 2005-04-18 10:58:36 -0700 | [diff] [blame] | 2737 | if (unlikely((long long)(now - next->timestamp) < 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | delta = 0; |
| 2739 | |
| 2740 | if (next->activated == 1) |
| 2741 | delta = delta * (ON_RUNQUEUE_WEIGHT * 128 / 100) / 128; |
| 2742 | |
| 2743 | array = next->array; |
| 2744 | dequeue_task(next, array); |
| 2745 | recalc_task_prio(next, next->timestamp + delta); |
| 2746 | enqueue_task(next, array); |
| 2747 | } |
| 2748 | next->activated = 0; |
| 2749 | switch_tasks: |
| 2750 | if (next == rq->idle) |
| 2751 | schedstat_inc(rq, sched_goidle); |
| 2752 | prefetch(next); |
| 2753 | clear_tsk_need_resched(prev); |
| 2754 | rcu_qsctr_inc(task_cpu(prev)); |
| 2755 | |
| 2756 | update_cpu_clock(prev, rq, now); |
| 2757 | |
| 2758 | prev->sleep_avg -= run_time; |
| 2759 | if ((long)prev->sleep_avg <= 0) |
| 2760 | prev->sleep_avg = 0; |
| 2761 | prev->timestamp = prev->last_ran = now; |
| 2762 | |
| 2763 | sched_info_switch(prev, next); |
| 2764 | if (likely(prev != next)) { |
| 2765 | next->timestamp = now; |
| 2766 | rq->nr_switches++; |
| 2767 | rq->curr = next; |
| 2768 | ++*switch_count; |
| 2769 | |
| 2770 | prepare_arch_switch(rq, next); |
| 2771 | prev = context_switch(rq, prev, next); |
| 2772 | barrier(); |
| 2773 | |
| 2774 | finish_task_switch(prev); |
| 2775 | } else |
| 2776 | spin_unlock_irq(&rq->lock); |
| 2777 | |
| 2778 | prev = current; |
| 2779 | if (unlikely(reacquire_kernel_lock(prev) < 0)) |
| 2780 | goto need_resched_nonpreemptible; |
| 2781 | preempt_enable_no_resched(); |
| 2782 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 2783 | goto need_resched; |
| 2784 | } |
| 2785 | |
| 2786 | EXPORT_SYMBOL(schedule); |
| 2787 | |
| 2788 | #ifdef CONFIG_PREEMPT |
| 2789 | /* |
| 2790 | * this is is the entry point to schedule() from in-kernel preemption |
| 2791 | * off of preempt_enable. Kernel preemptions off return from interrupt |
| 2792 | * occur there and call schedule directly. |
| 2793 | */ |
| 2794 | asmlinkage void __sched preempt_schedule(void) |
| 2795 | { |
| 2796 | struct thread_info *ti = current_thread_info(); |
| 2797 | #ifdef CONFIG_PREEMPT_BKL |
| 2798 | struct task_struct *task = current; |
| 2799 | int saved_lock_depth; |
| 2800 | #endif |
| 2801 | /* |
| 2802 | * If there is a non-zero preempt_count or interrupts are disabled, |
| 2803 | * we do not want to preempt the current task. Just return.. |
| 2804 | */ |
| 2805 | if (unlikely(ti->preempt_count || irqs_disabled())) |
| 2806 | return; |
| 2807 | |
| 2808 | need_resched: |
| 2809 | add_preempt_count(PREEMPT_ACTIVE); |
| 2810 | /* |
| 2811 | * We keep the big kernel semaphore locked, but we |
| 2812 | * clear ->lock_depth so that schedule() doesnt |
| 2813 | * auto-release the semaphore: |
| 2814 | */ |
| 2815 | #ifdef CONFIG_PREEMPT_BKL |
| 2816 | saved_lock_depth = task->lock_depth; |
| 2817 | task->lock_depth = -1; |
| 2818 | #endif |
| 2819 | schedule(); |
| 2820 | #ifdef CONFIG_PREEMPT_BKL |
| 2821 | task->lock_depth = saved_lock_depth; |
| 2822 | #endif |
| 2823 | sub_preempt_count(PREEMPT_ACTIVE); |
| 2824 | |
| 2825 | /* we could miss a preemption opportunity between schedule and now */ |
| 2826 | barrier(); |
| 2827 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 2828 | goto need_resched; |
| 2829 | } |
| 2830 | |
| 2831 | EXPORT_SYMBOL(preempt_schedule); |
| 2832 | |
| 2833 | /* |
| 2834 | * this is is the entry point to schedule() from kernel preemption |
| 2835 | * off of irq context. |
| 2836 | * Note, that this is called and return with irqs disabled. This will |
| 2837 | * protect us against recursive calling from irq. |
| 2838 | */ |
| 2839 | asmlinkage void __sched preempt_schedule_irq(void) |
| 2840 | { |
| 2841 | struct thread_info *ti = current_thread_info(); |
| 2842 | #ifdef CONFIG_PREEMPT_BKL |
| 2843 | struct task_struct *task = current; |
| 2844 | int saved_lock_depth; |
| 2845 | #endif |
| 2846 | /* Catch callers which need to be fixed*/ |
| 2847 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
| 2848 | |
| 2849 | need_resched: |
| 2850 | add_preempt_count(PREEMPT_ACTIVE); |
| 2851 | /* |
| 2852 | * We keep the big kernel semaphore locked, but we |
| 2853 | * clear ->lock_depth so that schedule() doesnt |
| 2854 | * auto-release the semaphore: |
| 2855 | */ |
| 2856 | #ifdef CONFIG_PREEMPT_BKL |
| 2857 | saved_lock_depth = task->lock_depth; |
| 2858 | task->lock_depth = -1; |
| 2859 | #endif |
| 2860 | local_irq_enable(); |
| 2861 | schedule(); |
| 2862 | local_irq_disable(); |
| 2863 | #ifdef CONFIG_PREEMPT_BKL |
| 2864 | task->lock_depth = saved_lock_depth; |
| 2865 | #endif |
| 2866 | sub_preempt_count(PREEMPT_ACTIVE); |
| 2867 | |
| 2868 | /* we could miss a preemption opportunity between schedule and now */ |
| 2869 | barrier(); |
| 2870 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) |
| 2871 | goto need_resched; |
| 2872 | } |
| 2873 | |
| 2874 | #endif /* CONFIG_PREEMPT */ |
| 2875 | |
| 2876 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, void *key) |
| 2877 | { |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 2878 | task_t *p = curr->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | return try_to_wake_up(p, mode, sync); |
| 2880 | } |
| 2881 | |
| 2882 | EXPORT_SYMBOL(default_wake_function); |
| 2883 | |
| 2884 | /* |
| 2885 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just |
| 2886 | * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve |
| 2887 | * number) then we wake all the non-exclusive tasks and one exclusive task. |
| 2888 | * |
| 2889 | * There are circumstances in which we can try to wake a task which has already |
| 2890 | * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns |
| 2891 | * zero in this (rare) case, and we handle it by continuing to scan the queue. |
| 2892 | */ |
| 2893 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, |
| 2894 | int nr_exclusive, int sync, void *key) |
| 2895 | { |
| 2896 | struct list_head *tmp, *next; |
| 2897 | |
| 2898 | list_for_each_safe(tmp, next, &q->task_list) { |
| 2899 | wait_queue_t *curr; |
| 2900 | unsigned flags; |
| 2901 | curr = list_entry(tmp, wait_queue_t, task_list); |
| 2902 | flags = curr->flags; |
| 2903 | if (curr->func(curr, mode, sync, key) && |
| 2904 | (flags & WQ_FLAG_EXCLUSIVE) && |
| 2905 | !--nr_exclusive) |
| 2906 | break; |
| 2907 | } |
| 2908 | } |
| 2909 | |
| 2910 | /** |
| 2911 | * __wake_up - wake up threads blocked on a waitqueue. |
| 2912 | * @q: the waitqueue |
| 2913 | * @mode: which threads |
| 2914 | * @nr_exclusive: how many wake-one or wake-many threads to wake up |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 2915 | * @key: is directly passed to the wakeup function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | */ |
| 2917 | void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode, |
| 2918 | int nr_exclusive, void *key) |
| 2919 | { |
| 2920 | unsigned long flags; |
| 2921 | |
| 2922 | spin_lock_irqsave(&q->lock, flags); |
| 2923 | __wake_up_common(q, mode, nr_exclusive, 0, key); |
| 2924 | spin_unlock_irqrestore(&q->lock, flags); |
| 2925 | } |
| 2926 | |
| 2927 | EXPORT_SYMBOL(__wake_up); |
| 2928 | |
| 2929 | /* |
| 2930 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. |
| 2931 | */ |
| 2932 | void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode) |
| 2933 | { |
| 2934 | __wake_up_common(q, mode, 1, 0, NULL); |
| 2935 | } |
| 2936 | |
| 2937 | /** |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 2938 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | * @q: the waitqueue |
| 2940 | * @mode: which threads |
| 2941 | * @nr_exclusive: how many wake-one or wake-many threads to wake up |
| 2942 | * |
| 2943 | * The sync wakeup differs that the waker knows that it will schedule |
| 2944 | * away soon, so while the target thread will be woken up, it will not |
| 2945 | * be migrated to another CPU - ie. the two threads are 'synchronized' |
| 2946 | * with each other. This can prevent needless bouncing between CPUs. |
| 2947 | * |
| 2948 | * On UP it can prevent extra preemption. |
| 2949 | */ |
| 2950 | void fastcall __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive) |
| 2951 | { |
| 2952 | unsigned long flags; |
| 2953 | int sync = 1; |
| 2954 | |
| 2955 | if (unlikely(!q)) |
| 2956 | return; |
| 2957 | |
| 2958 | if (unlikely(!nr_exclusive)) |
| 2959 | sync = 0; |
| 2960 | |
| 2961 | spin_lock_irqsave(&q->lock, flags); |
| 2962 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); |
| 2963 | spin_unlock_irqrestore(&q->lock, flags); |
| 2964 | } |
| 2965 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ |
| 2966 | |
| 2967 | void fastcall complete(struct completion *x) |
| 2968 | { |
| 2969 | unsigned long flags; |
| 2970 | |
| 2971 | spin_lock_irqsave(&x->wait.lock, flags); |
| 2972 | x->done++; |
| 2973 | __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, |
| 2974 | 1, 0, NULL); |
| 2975 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 2976 | } |
| 2977 | EXPORT_SYMBOL(complete); |
| 2978 | |
| 2979 | void fastcall complete_all(struct completion *x) |
| 2980 | { |
| 2981 | unsigned long flags; |
| 2982 | |
| 2983 | spin_lock_irqsave(&x->wait.lock, flags); |
| 2984 | x->done += UINT_MAX/2; |
| 2985 | __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, |
| 2986 | 0, 0, NULL); |
| 2987 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 2988 | } |
| 2989 | EXPORT_SYMBOL(complete_all); |
| 2990 | |
| 2991 | void fastcall __sched wait_for_completion(struct completion *x) |
| 2992 | { |
| 2993 | might_sleep(); |
| 2994 | spin_lock_irq(&x->wait.lock); |
| 2995 | if (!x->done) { |
| 2996 | DECLARE_WAITQUEUE(wait, current); |
| 2997 | |
| 2998 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 2999 | __add_wait_queue_tail(&x->wait, &wait); |
| 3000 | do { |
| 3001 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 3002 | spin_unlock_irq(&x->wait.lock); |
| 3003 | schedule(); |
| 3004 | spin_lock_irq(&x->wait.lock); |
| 3005 | } while (!x->done); |
| 3006 | __remove_wait_queue(&x->wait, &wait); |
| 3007 | } |
| 3008 | x->done--; |
| 3009 | spin_unlock_irq(&x->wait.lock); |
| 3010 | } |
| 3011 | EXPORT_SYMBOL(wait_for_completion); |
| 3012 | |
| 3013 | unsigned long fastcall __sched |
| 3014 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) |
| 3015 | { |
| 3016 | might_sleep(); |
| 3017 | |
| 3018 | spin_lock_irq(&x->wait.lock); |
| 3019 | if (!x->done) { |
| 3020 | DECLARE_WAITQUEUE(wait, current); |
| 3021 | |
| 3022 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3023 | __add_wait_queue_tail(&x->wait, &wait); |
| 3024 | do { |
| 3025 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 3026 | spin_unlock_irq(&x->wait.lock); |
| 3027 | timeout = schedule_timeout(timeout); |
| 3028 | spin_lock_irq(&x->wait.lock); |
| 3029 | if (!timeout) { |
| 3030 | __remove_wait_queue(&x->wait, &wait); |
| 3031 | goto out; |
| 3032 | } |
| 3033 | } while (!x->done); |
| 3034 | __remove_wait_queue(&x->wait, &wait); |
| 3035 | } |
| 3036 | x->done--; |
| 3037 | out: |
| 3038 | spin_unlock_irq(&x->wait.lock); |
| 3039 | return timeout; |
| 3040 | } |
| 3041 | EXPORT_SYMBOL(wait_for_completion_timeout); |
| 3042 | |
| 3043 | int fastcall __sched wait_for_completion_interruptible(struct completion *x) |
| 3044 | { |
| 3045 | int ret = 0; |
| 3046 | |
| 3047 | might_sleep(); |
| 3048 | |
| 3049 | spin_lock_irq(&x->wait.lock); |
| 3050 | if (!x->done) { |
| 3051 | DECLARE_WAITQUEUE(wait, current); |
| 3052 | |
| 3053 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3054 | __add_wait_queue_tail(&x->wait, &wait); |
| 3055 | do { |
| 3056 | if (signal_pending(current)) { |
| 3057 | ret = -ERESTARTSYS; |
| 3058 | __remove_wait_queue(&x->wait, &wait); |
| 3059 | goto out; |
| 3060 | } |
| 3061 | __set_current_state(TASK_INTERRUPTIBLE); |
| 3062 | spin_unlock_irq(&x->wait.lock); |
| 3063 | schedule(); |
| 3064 | spin_lock_irq(&x->wait.lock); |
| 3065 | } while (!x->done); |
| 3066 | __remove_wait_queue(&x->wait, &wait); |
| 3067 | } |
| 3068 | x->done--; |
| 3069 | out: |
| 3070 | spin_unlock_irq(&x->wait.lock); |
| 3071 | |
| 3072 | return ret; |
| 3073 | } |
| 3074 | EXPORT_SYMBOL(wait_for_completion_interruptible); |
| 3075 | |
| 3076 | unsigned long fastcall __sched |
| 3077 | wait_for_completion_interruptible_timeout(struct completion *x, |
| 3078 | unsigned long timeout) |
| 3079 | { |
| 3080 | might_sleep(); |
| 3081 | |
| 3082 | spin_lock_irq(&x->wait.lock); |
| 3083 | if (!x->done) { |
| 3084 | DECLARE_WAITQUEUE(wait, current); |
| 3085 | |
| 3086 | wait.flags |= WQ_FLAG_EXCLUSIVE; |
| 3087 | __add_wait_queue_tail(&x->wait, &wait); |
| 3088 | do { |
| 3089 | if (signal_pending(current)) { |
| 3090 | timeout = -ERESTARTSYS; |
| 3091 | __remove_wait_queue(&x->wait, &wait); |
| 3092 | goto out; |
| 3093 | } |
| 3094 | __set_current_state(TASK_INTERRUPTIBLE); |
| 3095 | spin_unlock_irq(&x->wait.lock); |
| 3096 | timeout = schedule_timeout(timeout); |
| 3097 | spin_lock_irq(&x->wait.lock); |
| 3098 | if (!timeout) { |
| 3099 | __remove_wait_queue(&x->wait, &wait); |
| 3100 | goto out; |
| 3101 | } |
| 3102 | } while (!x->done); |
| 3103 | __remove_wait_queue(&x->wait, &wait); |
| 3104 | } |
| 3105 | x->done--; |
| 3106 | out: |
| 3107 | spin_unlock_irq(&x->wait.lock); |
| 3108 | return timeout; |
| 3109 | } |
| 3110 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); |
| 3111 | |
| 3112 | |
| 3113 | #define SLEEP_ON_VAR \ |
| 3114 | unsigned long flags; \ |
| 3115 | wait_queue_t wait; \ |
| 3116 | init_waitqueue_entry(&wait, current); |
| 3117 | |
| 3118 | #define SLEEP_ON_HEAD \ |
| 3119 | spin_lock_irqsave(&q->lock,flags); \ |
| 3120 | __add_wait_queue(q, &wait); \ |
| 3121 | spin_unlock(&q->lock); |
| 3122 | |
| 3123 | #define SLEEP_ON_TAIL \ |
| 3124 | spin_lock_irq(&q->lock); \ |
| 3125 | __remove_wait_queue(q, &wait); \ |
| 3126 | spin_unlock_irqrestore(&q->lock, flags); |
| 3127 | |
| 3128 | void fastcall __sched interruptible_sleep_on(wait_queue_head_t *q) |
| 3129 | { |
| 3130 | SLEEP_ON_VAR |
| 3131 | |
| 3132 | current->state = TASK_INTERRUPTIBLE; |
| 3133 | |
| 3134 | SLEEP_ON_HEAD |
| 3135 | schedule(); |
| 3136 | SLEEP_ON_TAIL |
| 3137 | } |
| 3138 | |
| 3139 | EXPORT_SYMBOL(interruptible_sleep_on); |
| 3140 | |
| 3141 | long fastcall __sched interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
| 3142 | { |
| 3143 | SLEEP_ON_VAR |
| 3144 | |
| 3145 | current->state = TASK_INTERRUPTIBLE; |
| 3146 | |
| 3147 | SLEEP_ON_HEAD |
| 3148 | timeout = schedule_timeout(timeout); |
| 3149 | SLEEP_ON_TAIL |
| 3150 | |
| 3151 | return timeout; |
| 3152 | } |
| 3153 | |
| 3154 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
| 3155 | |
| 3156 | void fastcall __sched sleep_on(wait_queue_head_t *q) |
| 3157 | { |
| 3158 | SLEEP_ON_VAR |
| 3159 | |
| 3160 | current->state = TASK_UNINTERRUPTIBLE; |
| 3161 | |
| 3162 | SLEEP_ON_HEAD |
| 3163 | schedule(); |
| 3164 | SLEEP_ON_TAIL |
| 3165 | } |
| 3166 | |
| 3167 | EXPORT_SYMBOL(sleep_on); |
| 3168 | |
| 3169 | long fastcall __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
| 3170 | { |
| 3171 | SLEEP_ON_VAR |
| 3172 | |
| 3173 | current->state = TASK_UNINTERRUPTIBLE; |
| 3174 | |
| 3175 | SLEEP_ON_HEAD |
| 3176 | timeout = schedule_timeout(timeout); |
| 3177 | SLEEP_ON_TAIL |
| 3178 | |
| 3179 | return timeout; |
| 3180 | } |
| 3181 | |
| 3182 | EXPORT_SYMBOL(sleep_on_timeout); |
| 3183 | |
| 3184 | void set_user_nice(task_t *p, long nice) |
| 3185 | { |
| 3186 | unsigned long flags; |
| 3187 | prio_array_t *array; |
| 3188 | runqueue_t *rq; |
| 3189 | int old_prio, new_prio, delta; |
| 3190 | |
| 3191 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) |
| 3192 | return; |
| 3193 | /* |
| 3194 | * We have to be careful, if called from sys_setpriority(), |
| 3195 | * the task might be in the middle of scheduling on another CPU. |
| 3196 | */ |
| 3197 | rq = task_rq_lock(p, &flags); |
| 3198 | /* |
| 3199 | * The RT priorities are set via sched_setscheduler(), but we still |
| 3200 | * allow the 'normal' nice value to be set - but as expected |
| 3201 | * it wont have any effect on scheduling until the task is |
| 3202 | * not SCHED_NORMAL: |
| 3203 | */ |
| 3204 | if (rt_task(p)) { |
| 3205 | p->static_prio = NICE_TO_PRIO(nice); |
| 3206 | goto out_unlock; |
| 3207 | } |
| 3208 | array = p->array; |
| 3209 | if (array) |
| 3210 | dequeue_task(p, array); |
| 3211 | |
| 3212 | old_prio = p->prio; |
| 3213 | new_prio = NICE_TO_PRIO(nice); |
| 3214 | delta = new_prio - old_prio; |
| 3215 | p->static_prio = NICE_TO_PRIO(nice); |
| 3216 | p->prio += delta; |
| 3217 | |
| 3218 | if (array) { |
| 3219 | enqueue_task(p, array); |
| 3220 | /* |
| 3221 | * If the task increased its priority or is running and |
| 3222 | * lowered its priority, then reschedule its CPU: |
| 3223 | */ |
| 3224 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
| 3225 | resched_task(rq->curr); |
| 3226 | } |
| 3227 | out_unlock: |
| 3228 | task_rq_unlock(rq, &flags); |
| 3229 | } |
| 3230 | |
| 3231 | EXPORT_SYMBOL(set_user_nice); |
| 3232 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3233 | /* |
| 3234 | * can_nice - check if a task can reduce its nice value |
| 3235 | * @p: task |
| 3236 | * @nice: nice value |
| 3237 | */ |
| 3238 | int can_nice(const task_t *p, const int nice) |
| 3239 | { |
| 3240 | /* convert nice value [19,-20] to rlimit style value [0,39] */ |
| 3241 | int nice_rlim = 19 - nice; |
| 3242 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
| 3243 | capable(CAP_SYS_NICE)); |
| 3244 | } |
| 3245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | #ifdef __ARCH_WANT_SYS_NICE |
| 3247 | |
| 3248 | /* |
| 3249 | * sys_nice - change the priority of the current process. |
| 3250 | * @increment: priority increment |
| 3251 | * |
| 3252 | * sys_setpriority is a more generic, but much slower function that |
| 3253 | * does similar things. |
| 3254 | */ |
| 3255 | asmlinkage long sys_nice(int increment) |
| 3256 | { |
| 3257 | int retval; |
| 3258 | long nice; |
| 3259 | |
| 3260 | /* |
| 3261 | * Setpriority might change our priority at the same moment. |
| 3262 | * We don't have to worry. Conceptually one call occurs first |
| 3263 | * and we have a single winner. |
| 3264 | */ |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3265 | if (increment < -40) |
| 3266 | increment = -40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | if (increment > 40) |
| 3268 | increment = 40; |
| 3269 | |
| 3270 | nice = PRIO_TO_NICE(current->static_prio) + increment; |
| 3271 | if (nice < -20) |
| 3272 | nice = -20; |
| 3273 | if (nice > 19) |
| 3274 | nice = 19; |
| 3275 | |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3276 | if (increment < 0 && !can_nice(current, nice)) |
| 3277 | return -EPERM; |
| 3278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | retval = security_task_setnice(current, nice); |
| 3280 | if (retval) |
| 3281 | return retval; |
| 3282 | |
| 3283 | set_user_nice(current, nice); |
| 3284 | return 0; |
| 3285 | } |
| 3286 | |
| 3287 | #endif |
| 3288 | |
| 3289 | /** |
| 3290 | * task_prio - return the priority value of a given task. |
| 3291 | * @p: the task in question. |
| 3292 | * |
| 3293 | * This is the priority value as seen by users in /proc. |
| 3294 | * RT tasks are offset by -200. Normal tasks are centered |
| 3295 | * around 0, value goes from -16 to +15. |
| 3296 | */ |
| 3297 | int task_prio(const task_t *p) |
| 3298 | { |
| 3299 | return p->prio - MAX_RT_PRIO; |
| 3300 | } |
| 3301 | |
| 3302 | /** |
| 3303 | * task_nice - return the nice value of a given task. |
| 3304 | * @p: the task in question. |
| 3305 | */ |
| 3306 | int task_nice(const task_t *p) |
| 3307 | { |
| 3308 | return TASK_NICE(p); |
| 3309 | } |
| 3310 | |
| 3311 | /* |
| 3312 | * The only users of task_nice are binfmt_elf and binfmt_elf32. |
| 3313 | * binfmt_elf is no longer modular, but binfmt_elf32 still is. |
| 3314 | * Therefore, task_nice is needed if there is a compat_mode. |
| 3315 | */ |
| 3316 | #ifdef CONFIG_COMPAT |
| 3317 | EXPORT_SYMBOL_GPL(task_nice); |
| 3318 | #endif |
| 3319 | |
| 3320 | /** |
| 3321 | * idle_cpu - is a given cpu idle currently? |
| 3322 | * @cpu: the processor in question. |
| 3323 | */ |
| 3324 | int idle_cpu(int cpu) |
| 3325 | { |
| 3326 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; |
| 3327 | } |
| 3328 | |
| 3329 | EXPORT_SYMBOL_GPL(idle_cpu); |
| 3330 | |
| 3331 | /** |
| 3332 | * idle_task - return the idle task for a given cpu. |
| 3333 | * @cpu: the processor in question. |
| 3334 | */ |
| 3335 | task_t *idle_task(int cpu) |
| 3336 | { |
| 3337 | return cpu_rq(cpu)->idle; |
| 3338 | } |
| 3339 | |
| 3340 | /** |
| 3341 | * find_process_by_pid - find a process with a matching PID value. |
| 3342 | * @pid: the pid in question. |
| 3343 | */ |
| 3344 | static inline task_t *find_process_by_pid(pid_t pid) |
| 3345 | { |
| 3346 | return pid ? find_task_by_pid(pid) : current; |
| 3347 | } |
| 3348 | |
| 3349 | /* Actually do priority change: must hold rq lock. */ |
| 3350 | static void __setscheduler(struct task_struct *p, int policy, int prio) |
| 3351 | { |
| 3352 | BUG_ON(p->array); |
| 3353 | p->policy = policy; |
| 3354 | p->rt_priority = prio; |
| 3355 | if (policy != SCHED_NORMAL) |
| 3356 | p->prio = MAX_USER_RT_PRIO-1 - p->rt_priority; |
| 3357 | else |
| 3358 | p->prio = p->static_prio; |
| 3359 | } |
| 3360 | |
| 3361 | /** |
| 3362 | * sched_setscheduler - change the scheduling policy and/or RT priority of |
| 3363 | * a thread. |
| 3364 | * @p: the task in question. |
| 3365 | * @policy: new policy. |
| 3366 | * @param: structure containing the new RT priority. |
| 3367 | */ |
| 3368 | int sched_setscheduler(struct task_struct *p, int policy, struct sched_param *param) |
| 3369 | { |
| 3370 | int retval; |
| 3371 | int oldprio, oldpolicy = -1; |
| 3372 | prio_array_t *array; |
| 3373 | unsigned long flags; |
| 3374 | runqueue_t *rq; |
| 3375 | |
| 3376 | recheck: |
| 3377 | /* double check policy once rq lock held */ |
| 3378 | if (policy < 0) |
| 3379 | policy = oldpolicy = p->policy; |
| 3380 | else if (policy != SCHED_FIFO && policy != SCHED_RR && |
| 3381 | policy != SCHED_NORMAL) |
| 3382 | return -EINVAL; |
| 3383 | /* |
| 3384 | * Valid priorities for SCHED_FIFO and SCHED_RR are |
| 3385 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL is 0. |
| 3386 | */ |
| 3387 | if (param->sched_priority < 0 || |
| 3388 | param->sched_priority > MAX_USER_RT_PRIO-1) |
| 3389 | return -EINVAL; |
| 3390 | if ((policy == SCHED_NORMAL) != (param->sched_priority == 0)) |
| 3391 | return -EINVAL; |
| 3392 | |
| 3393 | if ((policy == SCHED_FIFO || policy == SCHED_RR) && |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 3394 | param->sched_priority > p->signal->rlim[RLIMIT_RTPRIO].rlim_cur && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | !capable(CAP_SYS_NICE)) |
| 3396 | return -EPERM; |
| 3397 | if ((current->euid != p->euid) && (current->euid != p->uid) && |
| 3398 | !capable(CAP_SYS_NICE)) |
| 3399 | return -EPERM; |
| 3400 | |
| 3401 | retval = security_task_setscheduler(p, policy, param); |
| 3402 | if (retval) |
| 3403 | return retval; |
| 3404 | /* |
| 3405 | * To be able to change p->policy safely, the apropriate |
| 3406 | * runqueue lock must be held. |
| 3407 | */ |
| 3408 | rq = task_rq_lock(p, &flags); |
| 3409 | /* recheck policy now with rq lock held */ |
| 3410 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
| 3411 | policy = oldpolicy = -1; |
| 3412 | task_rq_unlock(rq, &flags); |
| 3413 | goto recheck; |
| 3414 | } |
| 3415 | array = p->array; |
| 3416 | if (array) |
| 3417 | deactivate_task(p, rq); |
| 3418 | oldprio = p->prio; |
| 3419 | __setscheduler(p, policy, param->sched_priority); |
| 3420 | if (array) { |
| 3421 | __activate_task(p, rq); |
| 3422 | /* |
| 3423 | * Reschedule if we are currently running on this runqueue and |
| 3424 | * our priority decreased, or if we are not currently running on |
| 3425 | * this runqueue and our priority is higher than the current's |
| 3426 | */ |
| 3427 | if (task_running(rq, p)) { |
| 3428 | if (p->prio > oldprio) |
| 3429 | resched_task(rq->curr); |
| 3430 | } else if (TASK_PREEMPTS_CURR(p, rq)) |
| 3431 | resched_task(rq->curr); |
| 3432 | } |
| 3433 | task_rq_unlock(rq, &flags); |
| 3434 | return 0; |
| 3435 | } |
| 3436 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
| 3437 | |
| 3438 | static int do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) |
| 3439 | { |
| 3440 | int retval; |
| 3441 | struct sched_param lparam; |
| 3442 | struct task_struct *p; |
| 3443 | |
| 3444 | if (!param || pid < 0) |
| 3445 | return -EINVAL; |
| 3446 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) |
| 3447 | return -EFAULT; |
| 3448 | read_lock_irq(&tasklist_lock); |
| 3449 | p = find_process_by_pid(pid); |
| 3450 | if (!p) { |
| 3451 | read_unlock_irq(&tasklist_lock); |
| 3452 | return -ESRCH; |
| 3453 | } |
| 3454 | retval = sched_setscheduler(p, policy, &lparam); |
| 3455 | read_unlock_irq(&tasklist_lock); |
| 3456 | return retval; |
| 3457 | } |
| 3458 | |
| 3459 | /** |
| 3460 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority |
| 3461 | * @pid: the pid in question. |
| 3462 | * @policy: new policy. |
| 3463 | * @param: structure containing the new RT priority. |
| 3464 | */ |
| 3465 | asmlinkage long sys_sched_setscheduler(pid_t pid, int policy, |
| 3466 | struct sched_param __user *param) |
| 3467 | { |
| 3468 | return do_sched_setscheduler(pid, policy, param); |
| 3469 | } |
| 3470 | |
| 3471 | /** |
| 3472 | * sys_sched_setparam - set/change the RT priority of a thread |
| 3473 | * @pid: the pid in question. |
| 3474 | * @param: structure containing the new RT priority. |
| 3475 | */ |
| 3476 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) |
| 3477 | { |
| 3478 | return do_sched_setscheduler(pid, -1, param); |
| 3479 | } |
| 3480 | |
| 3481 | /** |
| 3482 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread |
| 3483 | * @pid: the pid in question. |
| 3484 | */ |
| 3485 | asmlinkage long sys_sched_getscheduler(pid_t pid) |
| 3486 | { |
| 3487 | int retval = -EINVAL; |
| 3488 | task_t *p; |
| 3489 | |
| 3490 | if (pid < 0) |
| 3491 | goto out_nounlock; |
| 3492 | |
| 3493 | retval = -ESRCH; |
| 3494 | read_lock(&tasklist_lock); |
| 3495 | p = find_process_by_pid(pid); |
| 3496 | if (p) { |
| 3497 | retval = security_task_getscheduler(p); |
| 3498 | if (!retval) |
| 3499 | retval = p->policy; |
| 3500 | } |
| 3501 | read_unlock(&tasklist_lock); |
| 3502 | |
| 3503 | out_nounlock: |
| 3504 | return retval; |
| 3505 | } |
| 3506 | |
| 3507 | /** |
| 3508 | * sys_sched_getscheduler - get the RT priority of a thread |
| 3509 | * @pid: the pid in question. |
| 3510 | * @param: structure containing the RT priority. |
| 3511 | */ |
| 3512 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) |
| 3513 | { |
| 3514 | struct sched_param lp; |
| 3515 | int retval = -EINVAL; |
| 3516 | task_t *p; |
| 3517 | |
| 3518 | if (!param || pid < 0) |
| 3519 | goto out_nounlock; |
| 3520 | |
| 3521 | read_lock(&tasklist_lock); |
| 3522 | p = find_process_by_pid(pid); |
| 3523 | retval = -ESRCH; |
| 3524 | if (!p) |
| 3525 | goto out_unlock; |
| 3526 | |
| 3527 | retval = security_task_getscheduler(p); |
| 3528 | if (retval) |
| 3529 | goto out_unlock; |
| 3530 | |
| 3531 | lp.sched_priority = p->rt_priority; |
| 3532 | read_unlock(&tasklist_lock); |
| 3533 | |
| 3534 | /* |
| 3535 | * This one might sleep, we cannot do it with a spinlock held ... |
| 3536 | */ |
| 3537 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; |
| 3538 | |
| 3539 | out_nounlock: |
| 3540 | return retval; |
| 3541 | |
| 3542 | out_unlock: |
| 3543 | read_unlock(&tasklist_lock); |
| 3544 | return retval; |
| 3545 | } |
| 3546 | |
| 3547 | long sched_setaffinity(pid_t pid, cpumask_t new_mask) |
| 3548 | { |
| 3549 | task_t *p; |
| 3550 | int retval; |
| 3551 | cpumask_t cpus_allowed; |
| 3552 | |
| 3553 | lock_cpu_hotplug(); |
| 3554 | read_lock(&tasklist_lock); |
| 3555 | |
| 3556 | p = find_process_by_pid(pid); |
| 3557 | if (!p) { |
| 3558 | read_unlock(&tasklist_lock); |
| 3559 | unlock_cpu_hotplug(); |
| 3560 | return -ESRCH; |
| 3561 | } |
| 3562 | |
| 3563 | /* |
| 3564 | * It is not safe to call set_cpus_allowed with the |
| 3565 | * tasklist_lock held. We will bump the task_struct's |
| 3566 | * usage count and then drop tasklist_lock. |
| 3567 | */ |
| 3568 | get_task_struct(p); |
| 3569 | read_unlock(&tasklist_lock); |
| 3570 | |
| 3571 | retval = -EPERM; |
| 3572 | if ((current->euid != p->euid) && (current->euid != p->uid) && |
| 3573 | !capable(CAP_SYS_NICE)) |
| 3574 | goto out_unlock; |
| 3575 | |
| 3576 | cpus_allowed = cpuset_cpus_allowed(p); |
| 3577 | cpus_and(new_mask, new_mask, cpus_allowed); |
| 3578 | retval = set_cpus_allowed(p, new_mask); |
| 3579 | |
| 3580 | out_unlock: |
| 3581 | put_task_struct(p); |
| 3582 | unlock_cpu_hotplug(); |
| 3583 | return retval; |
| 3584 | } |
| 3585 | |
| 3586 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, |
| 3587 | cpumask_t *new_mask) |
| 3588 | { |
| 3589 | if (len < sizeof(cpumask_t)) { |
| 3590 | memset(new_mask, 0, sizeof(cpumask_t)); |
| 3591 | } else if (len > sizeof(cpumask_t)) { |
| 3592 | len = sizeof(cpumask_t); |
| 3593 | } |
| 3594 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
| 3595 | } |
| 3596 | |
| 3597 | /** |
| 3598 | * sys_sched_setaffinity - set the cpu affinity of a process |
| 3599 | * @pid: pid of the process |
| 3600 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 3601 | * @user_mask_ptr: user-space pointer to the new cpu mask |
| 3602 | */ |
| 3603 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, |
| 3604 | unsigned long __user *user_mask_ptr) |
| 3605 | { |
| 3606 | cpumask_t new_mask; |
| 3607 | int retval; |
| 3608 | |
| 3609 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); |
| 3610 | if (retval) |
| 3611 | return retval; |
| 3612 | |
| 3613 | return sched_setaffinity(pid, new_mask); |
| 3614 | } |
| 3615 | |
| 3616 | /* |
| 3617 | * Represents all cpu's present in the system |
| 3618 | * In systems capable of hotplug, this map could dynamically grow |
| 3619 | * as new cpu's are detected in the system via any platform specific |
| 3620 | * method, such as ACPI for e.g. |
| 3621 | */ |
| 3622 | |
| 3623 | cpumask_t cpu_present_map; |
| 3624 | EXPORT_SYMBOL(cpu_present_map); |
| 3625 | |
| 3626 | #ifndef CONFIG_SMP |
| 3627 | cpumask_t cpu_online_map = CPU_MASK_ALL; |
| 3628 | cpumask_t cpu_possible_map = CPU_MASK_ALL; |
| 3629 | #endif |
| 3630 | |
| 3631 | long sched_getaffinity(pid_t pid, cpumask_t *mask) |
| 3632 | { |
| 3633 | int retval; |
| 3634 | task_t *p; |
| 3635 | |
| 3636 | lock_cpu_hotplug(); |
| 3637 | read_lock(&tasklist_lock); |
| 3638 | |
| 3639 | retval = -ESRCH; |
| 3640 | p = find_process_by_pid(pid); |
| 3641 | if (!p) |
| 3642 | goto out_unlock; |
| 3643 | |
| 3644 | retval = 0; |
| 3645 | cpus_and(*mask, p->cpus_allowed, cpu_possible_map); |
| 3646 | |
| 3647 | out_unlock: |
| 3648 | read_unlock(&tasklist_lock); |
| 3649 | unlock_cpu_hotplug(); |
| 3650 | if (retval) |
| 3651 | return retval; |
| 3652 | |
| 3653 | return 0; |
| 3654 | } |
| 3655 | |
| 3656 | /** |
| 3657 | * sys_sched_getaffinity - get the cpu affinity of a process |
| 3658 | * @pid: pid of the process |
| 3659 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr |
| 3660 | * @user_mask_ptr: user-space pointer to hold the current cpu mask |
| 3661 | */ |
| 3662 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, |
| 3663 | unsigned long __user *user_mask_ptr) |
| 3664 | { |
| 3665 | int ret; |
| 3666 | cpumask_t mask; |
| 3667 | |
| 3668 | if (len < sizeof(cpumask_t)) |
| 3669 | return -EINVAL; |
| 3670 | |
| 3671 | ret = sched_getaffinity(pid, &mask); |
| 3672 | if (ret < 0) |
| 3673 | return ret; |
| 3674 | |
| 3675 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) |
| 3676 | return -EFAULT; |
| 3677 | |
| 3678 | return sizeof(cpumask_t); |
| 3679 | } |
| 3680 | |
| 3681 | /** |
| 3682 | * sys_sched_yield - yield the current processor to other threads. |
| 3683 | * |
| 3684 | * this function yields the current CPU by moving the calling thread |
| 3685 | * to the expired array. If there are no other threads running on this |
| 3686 | * CPU then this function will return. |
| 3687 | */ |
| 3688 | asmlinkage long sys_sched_yield(void) |
| 3689 | { |
| 3690 | runqueue_t *rq = this_rq_lock(); |
| 3691 | prio_array_t *array = current->array; |
| 3692 | prio_array_t *target = rq->expired; |
| 3693 | |
| 3694 | schedstat_inc(rq, yld_cnt); |
| 3695 | /* |
| 3696 | * We implement yielding by moving the task into the expired |
| 3697 | * queue. |
| 3698 | * |
| 3699 | * (special rule: RT tasks will just roundrobin in the active |
| 3700 | * array.) |
| 3701 | */ |
| 3702 | if (rt_task(current)) |
| 3703 | target = rq->active; |
| 3704 | |
| 3705 | if (current->array->nr_active == 1) { |
| 3706 | schedstat_inc(rq, yld_act_empty); |
| 3707 | if (!rq->expired->nr_active) |
| 3708 | schedstat_inc(rq, yld_both_empty); |
| 3709 | } else if (!rq->expired->nr_active) |
| 3710 | schedstat_inc(rq, yld_exp_empty); |
| 3711 | |
| 3712 | if (array != target) { |
| 3713 | dequeue_task(current, array); |
| 3714 | enqueue_task(current, target); |
| 3715 | } else |
| 3716 | /* |
| 3717 | * requeue_task is cheaper so perform that if possible. |
| 3718 | */ |
| 3719 | requeue_task(current, array); |
| 3720 | |
| 3721 | /* |
| 3722 | * Since we are going to call schedule() anyway, there's |
| 3723 | * no need to preempt or enable interrupts: |
| 3724 | */ |
| 3725 | __release(rq->lock); |
| 3726 | _raw_spin_unlock(&rq->lock); |
| 3727 | preempt_enable_no_resched(); |
| 3728 | |
| 3729 | schedule(); |
| 3730 | |
| 3731 | return 0; |
| 3732 | } |
| 3733 | |
| 3734 | static inline void __cond_resched(void) |
| 3735 | { |
| 3736 | do { |
| 3737 | add_preempt_count(PREEMPT_ACTIVE); |
| 3738 | schedule(); |
| 3739 | sub_preempt_count(PREEMPT_ACTIVE); |
| 3740 | } while (need_resched()); |
| 3741 | } |
| 3742 | |
| 3743 | int __sched cond_resched(void) |
| 3744 | { |
| 3745 | if (need_resched()) { |
| 3746 | __cond_resched(); |
| 3747 | return 1; |
| 3748 | } |
| 3749 | return 0; |
| 3750 | } |
| 3751 | |
| 3752 | EXPORT_SYMBOL(cond_resched); |
| 3753 | |
| 3754 | /* |
| 3755 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, |
| 3756 | * call schedule, and on return reacquire the lock. |
| 3757 | * |
| 3758 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level |
| 3759 | * operations here to prevent schedule() from being called twice (once via |
| 3760 | * spin_unlock(), once by hand). |
| 3761 | */ |
| 3762 | int cond_resched_lock(spinlock_t * lock) |
| 3763 | { |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 3764 | int ret = 0; |
| 3765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3766 | if (need_lockbreak(lock)) { |
| 3767 | spin_unlock(lock); |
| 3768 | cpu_relax(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 3769 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | spin_lock(lock); |
| 3771 | } |
| 3772 | if (need_resched()) { |
| 3773 | _raw_spin_unlock(lock); |
| 3774 | preempt_enable_no_resched(); |
| 3775 | __cond_resched(); |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 3776 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3777 | spin_lock(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | } |
Jan Kara | 6df3cec | 2005-06-13 15:52:32 -0700 | [diff] [blame] | 3779 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | EXPORT_SYMBOL(cond_resched_lock); |
| 3783 | |
| 3784 | int __sched cond_resched_softirq(void) |
| 3785 | { |
| 3786 | BUG_ON(!in_softirq()); |
| 3787 | |
| 3788 | if (need_resched()) { |
| 3789 | __local_bh_enable(); |
| 3790 | __cond_resched(); |
| 3791 | local_bh_disable(); |
| 3792 | return 1; |
| 3793 | } |
| 3794 | return 0; |
| 3795 | } |
| 3796 | |
| 3797 | EXPORT_SYMBOL(cond_resched_softirq); |
| 3798 | |
| 3799 | |
| 3800 | /** |
| 3801 | * yield - yield the current processor to other threads. |
| 3802 | * |
| 3803 | * this is a shortcut for kernel-space yielding - it marks the |
| 3804 | * thread runnable and calls sys_sched_yield(). |
| 3805 | */ |
| 3806 | void __sched yield(void) |
| 3807 | { |
| 3808 | set_current_state(TASK_RUNNING); |
| 3809 | sys_sched_yield(); |
| 3810 | } |
| 3811 | |
| 3812 | EXPORT_SYMBOL(yield); |
| 3813 | |
| 3814 | /* |
| 3815 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
| 3816 | * that process accounting knows that this is a task in IO wait state. |
| 3817 | * |
| 3818 | * But don't do that if it is a deliberate, throttling IO wait (this task |
| 3819 | * has set its backing_dev_info: the queue against which it should throttle) |
| 3820 | */ |
| 3821 | void __sched io_schedule(void) |
| 3822 | { |
Ingo Molnar | 39c715b | 2005-06-21 17:14:34 -0700 | [diff] [blame] | 3823 | struct runqueue *rq = &per_cpu(runqueues, raw_smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | |
| 3825 | atomic_inc(&rq->nr_iowait); |
| 3826 | schedule(); |
| 3827 | atomic_dec(&rq->nr_iowait); |
| 3828 | } |
| 3829 | |
| 3830 | EXPORT_SYMBOL(io_schedule); |
| 3831 | |
| 3832 | long __sched io_schedule_timeout(long timeout) |
| 3833 | { |
Ingo Molnar | 39c715b | 2005-06-21 17:14:34 -0700 | [diff] [blame] | 3834 | struct runqueue *rq = &per_cpu(runqueues, raw_smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3835 | long ret; |
| 3836 | |
| 3837 | atomic_inc(&rq->nr_iowait); |
| 3838 | ret = schedule_timeout(timeout); |
| 3839 | atomic_dec(&rq->nr_iowait); |
| 3840 | return ret; |
| 3841 | } |
| 3842 | |
| 3843 | /** |
| 3844 | * sys_sched_get_priority_max - return maximum RT priority. |
| 3845 | * @policy: scheduling class. |
| 3846 | * |
| 3847 | * this syscall returns the maximum rt_priority that can be used |
| 3848 | * by a given scheduling class. |
| 3849 | */ |
| 3850 | asmlinkage long sys_sched_get_priority_max(int policy) |
| 3851 | { |
| 3852 | int ret = -EINVAL; |
| 3853 | |
| 3854 | switch (policy) { |
| 3855 | case SCHED_FIFO: |
| 3856 | case SCHED_RR: |
| 3857 | ret = MAX_USER_RT_PRIO-1; |
| 3858 | break; |
| 3859 | case SCHED_NORMAL: |
| 3860 | ret = 0; |
| 3861 | break; |
| 3862 | } |
| 3863 | return ret; |
| 3864 | } |
| 3865 | |
| 3866 | /** |
| 3867 | * sys_sched_get_priority_min - return minimum RT priority. |
| 3868 | * @policy: scheduling class. |
| 3869 | * |
| 3870 | * this syscall returns the minimum rt_priority that can be used |
| 3871 | * by a given scheduling class. |
| 3872 | */ |
| 3873 | asmlinkage long sys_sched_get_priority_min(int policy) |
| 3874 | { |
| 3875 | int ret = -EINVAL; |
| 3876 | |
| 3877 | switch (policy) { |
| 3878 | case SCHED_FIFO: |
| 3879 | case SCHED_RR: |
| 3880 | ret = 1; |
| 3881 | break; |
| 3882 | case SCHED_NORMAL: |
| 3883 | ret = 0; |
| 3884 | } |
| 3885 | return ret; |
| 3886 | } |
| 3887 | |
| 3888 | /** |
| 3889 | * sys_sched_rr_get_interval - return the default timeslice of a process. |
| 3890 | * @pid: pid of the process. |
| 3891 | * @interval: userspace pointer to the timeslice value. |
| 3892 | * |
| 3893 | * this syscall writes the default timeslice value of a given process |
| 3894 | * into the user-space timespec buffer. A value of '0' means infinity. |
| 3895 | */ |
| 3896 | asmlinkage |
| 3897 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) |
| 3898 | { |
| 3899 | int retval = -EINVAL; |
| 3900 | struct timespec t; |
| 3901 | task_t *p; |
| 3902 | |
| 3903 | if (pid < 0) |
| 3904 | goto out_nounlock; |
| 3905 | |
| 3906 | retval = -ESRCH; |
| 3907 | read_lock(&tasklist_lock); |
| 3908 | p = find_process_by_pid(pid); |
| 3909 | if (!p) |
| 3910 | goto out_unlock; |
| 3911 | |
| 3912 | retval = security_task_getscheduler(p); |
| 3913 | if (retval) |
| 3914 | goto out_unlock; |
| 3915 | |
| 3916 | jiffies_to_timespec(p->policy & SCHED_FIFO ? |
| 3917 | 0 : task_timeslice(p), &t); |
| 3918 | read_unlock(&tasklist_lock); |
| 3919 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
| 3920 | out_nounlock: |
| 3921 | return retval; |
| 3922 | out_unlock: |
| 3923 | read_unlock(&tasklist_lock); |
| 3924 | return retval; |
| 3925 | } |
| 3926 | |
| 3927 | static inline struct task_struct *eldest_child(struct task_struct *p) |
| 3928 | { |
| 3929 | if (list_empty(&p->children)) return NULL; |
| 3930 | return list_entry(p->children.next,struct task_struct,sibling); |
| 3931 | } |
| 3932 | |
| 3933 | static inline struct task_struct *older_sibling(struct task_struct *p) |
| 3934 | { |
| 3935 | if (p->sibling.prev==&p->parent->children) return NULL; |
| 3936 | return list_entry(p->sibling.prev,struct task_struct,sibling); |
| 3937 | } |
| 3938 | |
| 3939 | static inline struct task_struct *younger_sibling(struct task_struct *p) |
| 3940 | { |
| 3941 | if (p->sibling.next==&p->parent->children) return NULL; |
| 3942 | return list_entry(p->sibling.next,struct task_struct,sibling); |
| 3943 | } |
| 3944 | |
| 3945 | static void show_task(task_t * p) |
| 3946 | { |
| 3947 | task_t *relative; |
| 3948 | unsigned state; |
| 3949 | unsigned long free = 0; |
| 3950 | static const char *stat_nam[] = { "R", "S", "D", "T", "t", "Z", "X" }; |
| 3951 | |
| 3952 | printk("%-13.13s ", p->comm); |
| 3953 | state = p->state ? __ffs(p->state) + 1 : 0; |
| 3954 | if (state < ARRAY_SIZE(stat_nam)) |
| 3955 | printk(stat_nam[state]); |
| 3956 | else |
| 3957 | printk("?"); |
| 3958 | #if (BITS_PER_LONG == 32) |
| 3959 | if (state == TASK_RUNNING) |
| 3960 | printk(" running "); |
| 3961 | else |
| 3962 | printk(" %08lX ", thread_saved_pc(p)); |
| 3963 | #else |
| 3964 | if (state == TASK_RUNNING) |
| 3965 | printk(" running task "); |
| 3966 | else |
| 3967 | printk(" %016lx ", thread_saved_pc(p)); |
| 3968 | #endif |
| 3969 | #ifdef CONFIG_DEBUG_STACK_USAGE |
| 3970 | { |
| 3971 | unsigned long * n = (unsigned long *) (p->thread_info+1); |
| 3972 | while (!*n) |
| 3973 | n++; |
| 3974 | free = (unsigned long) n - (unsigned long)(p->thread_info+1); |
| 3975 | } |
| 3976 | #endif |
| 3977 | printk("%5lu %5d %6d ", free, p->pid, p->parent->pid); |
| 3978 | if ((relative = eldest_child(p))) |
| 3979 | printk("%5d ", relative->pid); |
| 3980 | else |
| 3981 | printk(" "); |
| 3982 | if ((relative = younger_sibling(p))) |
| 3983 | printk("%7d", relative->pid); |
| 3984 | else |
| 3985 | printk(" "); |
| 3986 | if ((relative = older_sibling(p))) |
| 3987 | printk(" %5d", relative->pid); |
| 3988 | else |
| 3989 | printk(" "); |
| 3990 | if (!p->mm) |
| 3991 | printk(" (L-TLB)\n"); |
| 3992 | else |
| 3993 | printk(" (NOTLB)\n"); |
| 3994 | |
| 3995 | if (state != TASK_RUNNING) |
| 3996 | show_stack(p, NULL); |
| 3997 | } |
| 3998 | |
| 3999 | void show_state(void) |
| 4000 | { |
| 4001 | task_t *g, *p; |
| 4002 | |
| 4003 | #if (BITS_PER_LONG == 32) |
| 4004 | printk("\n" |
| 4005 | " sibling\n"); |
| 4006 | printk(" task PC pid father child younger older\n"); |
| 4007 | #else |
| 4008 | printk("\n" |
| 4009 | " sibling\n"); |
| 4010 | printk(" task PC pid father child younger older\n"); |
| 4011 | #endif |
| 4012 | read_lock(&tasklist_lock); |
| 4013 | do_each_thread(g, p) { |
| 4014 | /* |
| 4015 | * reset the NMI-timeout, listing all files on a slow |
| 4016 | * console might take alot of time: |
| 4017 | */ |
| 4018 | touch_nmi_watchdog(); |
| 4019 | show_task(p); |
| 4020 | } while_each_thread(g, p); |
| 4021 | |
| 4022 | read_unlock(&tasklist_lock); |
| 4023 | } |
| 4024 | |
| 4025 | void __devinit init_idle(task_t *idle, int cpu) |
| 4026 | { |
| 4027 | runqueue_t *rq = cpu_rq(cpu); |
| 4028 | unsigned long flags; |
| 4029 | |
| 4030 | idle->sleep_avg = 0; |
| 4031 | idle->array = NULL; |
| 4032 | idle->prio = MAX_PRIO; |
| 4033 | idle->state = TASK_RUNNING; |
| 4034 | idle->cpus_allowed = cpumask_of_cpu(cpu); |
| 4035 | set_task_cpu(idle, cpu); |
| 4036 | |
| 4037 | spin_lock_irqsave(&rq->lock, flags); |
| 4038 | rq->curr = rq->idle = idle; |
| 4039 | set_tsk_need_resched(idle); |
| 4040 | spin_unlock_irqrestore(&rq->lock, flags); |
| 4041 | |
| 4042 | /* Set the preempt count _outside_ the spinlocks! */ |
| 4043 | #if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL) |
| 4044 | idle->thread_info->preempt_count = (idle->lock_depth >= 0); |
| 4045 | #else |
| 4046 | idle->thread_info->preempt_count = 0; |
| 4047 | #endif |
| 4048 | } |
| 4049 | |
| 4050 | /* |
| 4051 | * In a system that switches off the HZ timer nohz_cpu_mask |
| 4052 | * indicates which cpus entered this state. This is used |
| 4053 | * in the rcu update to wait only for active cpus. For system |
| 4054 | * which do not switch off the HZ timer nohz_cpu_mask should |
| 4055 | * always be CPU_MASK_NONE. |
| 4056 | */ |
| 4057 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; |
| 4058 | |
| 4059 | #ifdef CONFIG_SMP |
| 4060 | /* |
| 4061 | * This is how migration works: |
| 4062 | * |
| 4063 | * 1) we queue a migration_req_t structure in the source CPU's |
| 4064 | * runqueue and wake up that CPU's migration thread. |
| 4065 | * 2) we down() the locked semaphore => thread blocks. |
| 4066 | * 3) migration thread wakes up (implicitly it forces the migrated |
| 4067 | * thread off the CPU) |
| 4068 | * 4) it gets the migration request and checks whether the migrated |
| 4069 | * task is still in the wrong runqueue. |
| 4070 | * 5) if it's in the wrong runqueue then the migration thread removes |
| 4071 | * it and puts it into the right queue. |
| 4072 | * 6) migration thread up()s the semaphore. |
| 4073 | * 7) we wake up and the migration is done. |
| 4074 | */ |
| 4075 | |
| 4076 | /* |
| 4077 | * Change a given task's CPU affinity. Migrate the thread to a |
| 4078 | * proper CPU and schedule it away if the CPU it's executing on |
| 4079 | * is removed from the allowed bitmask. |
| 4080 | * |
| 4081 | * NOTE: the caller must have a valid reference to the task, the |
| 4082 | * task must not exit() & deallocate itself prematurely. The |
| 4083 | * call is not atomic; no spinlocks may be held. |
| 4084 | */ |
| 4085 | int set_cpus_allowed(task_t *p, cpumask_t new_mask) |
| 4086 | { |
| 4087 | unsigned long flags; |
| 4088 | int ret = 0; |
| 4089 | migration_req_t req; |
| 4090 | runqueue_t *rq; |
| 4091 | |
| 4092 | rq = task_rq_lock(p, &flags); |
| 4093 | if (!cpus_intersects(new_mask, cpu_online_map)) { |
| 4094 | ret = -EINVAL; |
| 4095 | goto out; |
| 4096 | } |
| 4097 | |
| 4098 | p->cpus_allowed = new_mask; |
| 4099 | /* Can the task run on the task's current CPU? If so, we're done */ |
| 4100 | if (cpu_isset(task_cpu(p), new_mask)) |
| 4101 | goto out; |
| 4102 | |
| 4103 | if (migrate_task(p, any_online_cpu(new_mask), &req)) { |
| 4104 | /* Need help from migration thread: drop lock and wait. */ |
| 4105 | task_rq_unlock(rq, &flags); |
| 4106 | wake_up_process(rq->migration_thread); |
| 4107 | wait_for_completion(&req.done); |
| 4108 | tlb_migrate_finish(p->mm); |
| 4109 | return 0; |
| 4110 | } |
| 4111 | out: |
| 4112 | task_rq_unlock(rq, &flags); |
| 4113 | return ret; |
| 4114 | } |
| 4115 | |
| 4116 | EXPORT_SYMBOL_GPL(set_cpus_allowed); |
| 4117 | |
| 4118 | /* |
| 4119 | * Move (not current) task off this cpu, onto dest cpu. We're doing |
| 4120 | * this because either it can't run here any more (set_cpus_allowed() |
| 4121 | * away from this CPU, or CPU going down), or because we're |
| 4122 | * attempting to rebalance this task on exec (sched_exec). |
| 4123 | * |
| 4124 | * So we race with normal scheduler movements, but that's OK, as long |
| 4125 | * as the task is no longer on this CPU. |
| 4126 | */ |
| 4127 | static void __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) |
| 4128 | { |
| 4129 | runqueue_t *rq_dest, *rq_src; |
| 4130 | |
| 4131 | if (unlikely(cpu_is_offline(dest_cpu))) |
| 4132 | return; |
| 4133 | |
| 4134 | rq_src = cpu_rq(src_cpu); |
| 4135 | rq_dest = cpu_rq(dest_cpu); |
| 4136 | |
| 4137 | double_rq_lock(rq_src, rq_dest); |
| 4138 | /* Already moved. */ |
| 4139 | if (task_cpu(p) != src_cpu) |
| 4140 | goto out; |
| 4141 | /* Affinity changed (again). */ |
| 4142 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) |
| 4143 | goto out; |
| 4144 | |
| 4145 | set_task_cpu(p, dest_cpu); |
| 4146 | if (p->array) { |
| 4147 | /* |
| 4148 | * Sync timestamp with rq_dest's before activating. |
| 4149 | * The same thing could be achieved by doing this step |
| 4150 | * afterwards, and pretending it was a local activate. |
| 4151 | * This way is cleaner and logically correct. |
| 4152 | */ |
| 4153 | p->timestamp = p->timestamp - rq_src->timestamp_last_tick |
| 4154 | + rq_dest->timestamp_last_tick; |
| 4155 | deactivate_task(p, rq_src); |
| 4156 | activate_task(p, rq_dest, 0); |
| 4157 | if (TASK_PREEMPTS_CURR(p, rq_dest)) |
| 4158 | resched_task(rq_dest->curr); |
| 4159 | } |
| 4160 | |
| 4161 | out: |
| 4162 | double_rq_unlock(rq_src, rq_dest); |
| 4163 | } |
| 4164 | |
| 4165 | /* |
| 4166 | * migration_thread - this is a highprio system thread that performs |
| 4167 | * thread migration by bumping thread off CPU then 'pushing' onto |
| 4168 | * another runqueue. |
| 4169 | */ |
| 4170 | static int migration_thread(void * data) |
| 4171 | { |
| 4172 | runqueue_t *rq; |
| 4173 | int cpu = (long)data; |
| 4174 | |
| 4175 | rq = cpu_rq(cpu); |
| 4176 | BUG_ON(rq->migration_thread != current); |
| 4177 | |
| 4178 | set_current_state(TASK_INTERRUPTIBLE); |
| 4179 | while (!kthread_should_stop()) { |
| 4180 | struct list_head *head; |
| 4181 | migration_req_t *req; |
| 4182 | |
| 4183 | if (current->flags & PF_FREEZE) |
| 4184 | refrigerator(PF_FREEZE); |
| 4185 | |
| 4186 | spin_lock_irq(&rq->lock); |
| 4187 | |
| 4188 | if (cpu_is_offline(cpu)) { |
| 4189 | spin_unlock_irq(&rq->lock); |
| 4190 | goto wait_to_die; |
| 4191 | } |
| 4192 | |
| 4193 | if (rq->active_balance) { |
| 4194 | active_load_balance(rq, cpu); |
| 4195 | rq->active_balance = 0; |
| 4196 | } |
| 4197 | |
| 4198 | head = &rq->migration_queue; |
| 4199 | |
| 4200 | if (list_empty(head)) { |
| 4201 | spin_unlock_irq(&rq->lock); |
| 4202 | schedule(); |
| 4203 | set_current_state(TASK_INTERRUPTIBLE); |
| 4204 | continue; |
| 4205 | } |
| 4206 | req = list_entry(head->next, migration_req_t, list); |
| 4207 | list_del_init(head->next); |
| 4208 | |
| 4209 | if (req->type == REQ_MOVE_TASK) { |
| 4210 | spin_unlock(&rq->lock); |
| 4211 | __migrate_task(req->task, cpu, req->dest_cpu); |
| 4212 | local_irq_enable(); |
| 4213 | } else if (req->type == REQ_SET_DOMAIN) { |
| 4214 | rq->sd = req->sd; |
| 4215 | spin_unlock_irq(&rq->lock); |
| 4216 | } else { |
| 4217 | spin_unlock_irq(&rq->lock); |
| 4218 | WARN_ON(1); |
| 4219 | } |
| 4220 | |
| 4221 | complete(&req->done); |
| 4222 | } |
| 4223 | __set_current_state(TASK_RUNNING); |
| 4224 | return 0; |
| 4225 | |
| 4226 | wait_to_die: |
| 4227 | /* Wait for kthread_stop */ |
| 4228 | set_current_state(TASK_INTERRUPTIBLE); |
| 4229 | while (!kthread_should_stop()) { |
| 4230 | schedule(); |
| 4231 | set_current_state(TASK_INTERRUPTIBLE); |
| 4232 | } |
| 4233 | __set_current_state(TASK_RUNNING); |
| 4234 | return 0; |
| 4235 | } |
| 4236 | |
| 4237 | #ifdef CONFIG_HOTPLUG_CPU |
| 4238 | /* Figure out where task on dead CPU should go, use force if neccessary. */ |
| 4239 | static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *tsk) |
| 4240 | { |
| 4241 | int dest_cpu; |
| 4242 | cpumask_t mask; |
| 4243 | |
| 4244 | /* On same node? */ |
| 4245 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); |
| 4246 | cpus_and(mask, mask, tsk->cpus_allowed); |
| 4247 | dest_cpu = any_online_cpu(mask); |
| 4248 | |
| 4249 | /* On any allowed CPU? */ |
| 4250 | if (dest_cpu == NR_CPUS) |
| 4251 | dest_cpu = any_online_cpu(tsk->cpus_allowed); |
| 4252 | |
| 4253 | /* No more Mr. Nice Guy. */ |
| 4254 | if (dest_cpu == NR_CPUS) { |
Paul Jackson | b39c4fa | 2005-05-20 13:59:15 -0700 | [diff] [blame] | 4255 | cpus_setall(tsk->cpus_allowed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | dest_cpu = any_online_cpu(tsk->cpus_allowed); |
| 4257 | |
| 4258 | /* |
| 4259 | * Don't tell them about moving exiting tasks or |
| 4260 | * kernel threads (both mm NULL), since they never |
| 4261 | * leave kernel. |
| 4262 | */ |
| 4263 | if (tsk->mm && printk_ratelimit()) |
| 4264 | printk(KERN_INFO "process %d (%s) no " |
| 4265 | "longer affine to cpu%d\n", |
| 4266 | tsk->pid, tsk->comm, dead_cpu); |
| 4267 | } |
| 4268 | __migrate_task(tsk, dead_cpu, dest_cpu); |
| 4269 | } |
| 4270 | |
| 4271 | /* |
| 4272 | * While a dead CPU has no uninterruptible tasks queued at this point, |
| 4273 | * it might still have a nonzero ->nr_uninterruptible counter, because |
| 4274 | * for performance reasons the counter is not stricly tracking tasks to |
| 4275 | * their home CPUs. So we just add the counter to another CPU's counter, |
| 4276 | * to keep the global sum constant after CPU-down: |
| 4277 | */ |
| 4278 | static void migrate_nr_uninterruptible(runqueue_t *rq_src) |
| 4279 | { |
| 4280 | runqueue_t *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL)); |
| 4281 | unsigned long flags; |
| 4282 | |
| 4283 | local_irq_save(flags); |
| 4284 | double_rq_lock(rq_src, rq_dest); |
| 4285 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; |
| 4286 | rq_src->nr_uninterruptible = 0; |
| 4287 | double_rq_unlock(rq_src, rq_dest); |
| 4288 | local_irq_restore(flags); |
| 4289 | } |
| 4290 | |
| 4291 | /* Run through task list and migrate tasks from the dead cpu. */ |
| 4292 | static void migrate_live_tasks(int src_cpu) |
| 4293 | { |
| 4294 | struct task_struct *tsk, *t; |
| 4295 | |
| 4296 | write_lock_irq(&tasklist_lock); |
| 4297 | |
| 4298 | do_each_thread(t, tsk) { |
| 4299 | if (tsk == current) |
| 4300 | continue; |
| 4301 | |
| 4302 | if (task_cpu(tsk) == src_cpu) |
| 4303 | move_task_off_dead_cpu(src_cpu, tsk); |
| 4304 | } while_each_thread(t, tsk); |
| 4305 | |
| 4306 | write_unlock_irq(&tasklist_lock); |
| 4307 | } |
| 4308 | |
| 4309 | /* Schedules idle task to be the next runnable task on current CPU. |
| 4310 | * It does so by boosting its priority to highest possible and adding it to |
| 4311 | * the _front_ of runqueue. Used by CPU offline code. |
| 4312 | */ |
| 4313 | void sched_idle_next(void) |
| 4314 | { |
| 4315 | int cpu = smp_processor_id(); |
| 4316 | runqueue_t *rq = this_rq(); |
| 4317 | struct task_struct *p = rq->idle; |
| 4318 | unsigned long flags; |
| 4319 | |
| 4320 | /* cpu has to be offline */ |
| 4321 | BUG_ON(cpu_online(cpu)); |
| 4322 | |
| 4323 | /* Strictly not necessary since rest of the CPUs are stopped by now |
| 4324 | * and interrupts disabled on current cpu. |
| 4325 | */ |
| 4326 | spin_lock_irqsave(&rq->lock, flags); |
| 4327 | |
| 4328 | __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1); |
| 4329 | /* Add idle task to _front_ of it's priority queue */ |
| 4330 | __activate_idle_task(p, rq); |
| 4331 | |
| 4332 | spin_unlock_irqrestore(&rq->lock, flags); |
| 4333 | } |
| 4334 | |
| 4335 | /* Ensures that the idle task is using init_mm right before its cpu goes |
| 4336 | * offline. |
| 4337 | */ |
| 4338 | void idle_task_exit(void) |
| 4339 | { |
| 4340 | struct mm_struct *mm = current->active_mm; |
| 4341 | |
| 4342 | BUG_ON(cpu_online(smp_processor_id())); |
| 4343 | |
| 4344 | if (mm != &init_mm) |
| 4345 | switch_mm(mm, &init_mm, current); |
| 4346 | mmdrop(mm); |
| 4347 | } |
| 4348 | |
| 4349 | static void migrate_dead(unsigned int dead_cpu, task_t *tsk) |
| 4350 | { |
| 4351 | struct runqueue *rq = cpu_rq(dead_cpu); |
| 4352 | |
| 4353 | /* Must be exiting, otherwise would be on tasklist. */ |
| 4354 | BUG_ON(tsk->exit_state != EXIT_ZOMBIE && tsk->exit_state != EXIT_DEAD); |
| 4355 | |
| 4356 | /* Cannot have done final schedule yet: would have vanished. */ |
| 4357 | BUG_ON(tsk->flags & PF_DEAD); |
| 4358 | |
| 4359 | get_task_struct(tsk); |
| 4360 | |
| 4361 | /* |
| 4362 | * Drop lock around migration; if someone else moves it, |
| 4363 | * that's OK. No task can be added to this CPU, so iteration is |
| 4364 | * fine. |
| 4365 | */ |
| 4366 | spin_unlock_irq(&rq->lock); |
| 4367 | move_task_off_dead_cpu(dead_cpu, tsk); |
| 4368 | spin_lock_irq(&rq->lock); |
| 4369 | |
| 4370 | put_task_struct(tsk); |
| 4371 | } |
| 4372 | |
| 4373 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ |
| 4374 | static void migrate_dead_tasks(unsigned int dead_cpu) |
| 4375 | { |
| 4376 | unsigned arr, i; |
| 4377 | struct runqueue *rq = cpu_rq(dead_cpu); |
| 4378 | |
| 4379 | for (arr = 0; arr < 2; arr++) { |
| 4380 | for (i = 0; i < MAX_PRIO; i++) { |
| 4381 | struct list_head *list = &rq->arrays[arr].queue[i]; |
| 4382 | while (!list_empty(list)) |
| 4383 | migrate_dead(dead_cpu, |
| 4384 | list_entry(list->next, task_t, |
| 4385 | run_list)); |
| 4386 | } |
| 4387 | } |
| 4388 | } |
| 4389 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 4390 | |
| 4391 | /* |
| 4392 | * migration_call - callback that gets triggered when a CPU is added. |
| 4393 | * Here we can start up the necessary migration thread for the new CPU. |
| 4394 | */ |
| 4395 | static int migration_call(struct notifier_block *nfb, unsigned long action, |
| 4396 | void *hcpu) |
| 4397 | { |
| 4398 | int cpu = (long)hcpu; |
| 4399 | struct task_struct *p; |
| 4400 | struct runqueue *rq; |
| 4401 | unsigned long flags; |
| 4402 | |
| 4403 | switch (action) { |
| 4404 | case CPU_UP_PREPARE: |
| 4405 | p = kthread_create(migration_thread, hcpu, "migration/%d",cpu); |
| 4406 | if (IS_ERR(p)) |
| 4407 | return NOTIFY_BAD; |
| 4408 | p->flags |= PF_NOFREEZE; |
| 4409 | kthread_bind(p, cpu); |
| 4410 | /* Must be high prio: stop_machine expects to yield to it. */ |
| 4411 | rq = task_rq_lock(p, &flags); |
| 4412 | __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1); |
| 4413 | task_rq_unlock(rq, &flags); |
| 4414 | cpu_rq(cpu)->migration_thread = p; |
| 4415 | break; |
| 4416 | case CPU_ONLINE: |
| 4417 | /* Strictly unneccessary, as first user will wake it. */ |
| 4418 | wake_up_process(cpu_rq(cpu)->migration_thread); |
| 4419 | break; |
| 4420 | #ifdef CONFIG_HOTPLUG_CPU |
| 4421 | case CPU_UP_CANCELED: |
| 4422 | /* Unbind it from offline cpu so it can run. Fall thru. */ |
| 4423 | kthread_bind(cpu_rq(cpu)->migration_thread,smp_processor_id()); |
| 4424 | kthread_stop(cpu_rq(cpu)->migration_thread); |
| 4425 | cpu_rq(cpu)->migration_thread = NULL; |
| 4426 | break; |
| 4427 | case CPU_DEAD: |
| 4428 | migrate_live_tasks(cpu); |
| 4429 | rq = cpu_rq(cpu); |
| 4430 | kthread_stop(rq->migration_thread); |
| 4431 | rq->migration_thread = NULL; |
| 4432 | /* Idle task back to normal (off runqueue, low prio) */ |
| 4433 | rq = task_rq_lock(rq->idle, &flags); |
| 4434 | deactivate_task(rq->idle, rq); |
| 4435 | rq->idle->static_prio = MAX_PRIO; |
| 4436 | __setscheduler(rq->idle, SCHED_NORMAL, 0); |
| 4437 | migrate_dead_tasks(cpu); |
| 4438 | task_rq_unlock(rq, &flags); |
| 4439 | migrate_nr_uninterruptible(rq); |
| 4440 | BUG_ON(rq->nr_running != 0); |
| 4441 | |
| 4442 | /* No need to migrate the tasks: it was best-effort if |
| 4443 | * they didn't do lock_cpu_hotplug(). Just wake up |
| 4444 | * the requestors. */ |
| 4445 | spin_lock_irq(&rq->lock); |
| 4446 | while (!list_empty(&rq->migration_queue)) { |
| 4447 | migration_req_t *req; |
| 4448 | req = list_entry(rq->migration_queue.next, |
| 4449 | migration_req_t, list); |
| 4450 | BUG_ON(req->type != REQ_MOVE_TASK); |
| 4451 | list_del_init(&req->list); |
| 4452 | complete(&req->done); |
| 4453 | } |
| 4454 | spin_unlock_irq(&rq->lock); |
| 4455 | break; |
| 4456 | #endif |
| 4457 | } |
| 4458 | return NOTIFY_OK; |
| 4459 | } |
| 4460 | |
| 4461 | /* Register at highest priority so that task migration (migrate_all_tasks) |
| 4462 | * happens before everything else. |
| 4463 | */ |
| 4464 | static struct notifier_block __devinitdata migration_notifier = { |
| 4465 | .notifier_call = migration_call, |
| 4466 | .priority = 10 |
| 4467 | }; |
| 4468 | |
| 4469 | int __init migration_init(void) |
| 4470 | { |
| 4471 | void *cpu = (void *)(long)smp_processor_id(); |
| 4472 | /* Start one for boot CPU. */ |
| 4473 | migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
| 4474 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
| 4475 | register_cpu_notifier(&migration_notifier); |
| 4476 | return 0; |
| 4477 | } |
| 4478 | #endif |
| 4479 | |
| 4480 | #ifdef CONFIG_SMP |
| 4481 | #define SCHED_DOMAIN_DEBUG |
| 4482 | #ifdef SCHED_DOMAIN_DEBUG |
| 4483 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
| 4484 | { |
| 4485 | int level = 0; |
| 4486 | |
| 4487 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
| 4488 | |
| 4489 | do { |
| 4490 | int i; |
| 4491 | char str[NR_CPUS]; |
| 4492 | struct sched_group *group = sd->groups; |
| 4493 | cpumask_t groupmask; |
| 4494 | |
| 4495 | cpumask_scnprintf(str, NR_CPUS, sd->span); |
| 4496 | cpus_clear(groupmask); |
| 4497 | |
| 4498 | printk(KERN_DEBUG); |
| 4499 | for (i = 0; i < level + 1; i++) |
| 4500 | printk(" "); |
| 4501 | printk("domain %d: ", level); |
| 4502 | |
| 4503 | if (!(sd->flags & SD_LOAD_BALANCE)) { |
| 4504 | printk("does not load-balance\n"); |
| 4505 | if (sd->parent) |
| 4506 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain has parent"); |
| 4507 | break; |
| 4508 | } |
| 4509 | |
| 4510 | printk("span %s\n", str); |
| 4511 | |
| 4512 | if (!cpu_isset(cpu, sd->span)) |
| 4513 | printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu); |
| 4514 | if (!cpu_isset(cpu, group->cpumask)) |
| 4515 | printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu); |
| 4516 | |
| 4517 | printk(KERN_DEBUG); |
| 4518 | for (i = 0; i < level + 2; i++) |
| 4519 | printk(" "); |
| 4520 | printk("groups:"); |
| 4521 | do { |
| 4522 | if (!group) { |
| 4523 | printk("\n"); |
| 4524 | printk(KERN_ERR "ERROR: group is NULL\n"); |
| 4525 | break; |
| 4526 | } |
| 4527 | |
| 4528 | if (!group->cpu_power) { |
| 4529 | printk("\n"); |
| 4530 | printk(KERN_ERR "ERROR: domain->cpu_power not set\n"); |
| 4531 | } |
| 4532 | |
| 4533 | if (!cpus_weight(group->cpumask)) { |
| 4534 | printk("\n"); |
| 4535 | printk(KERN_ERR "ERROR: empty group\n"); |
| 4536 | } |
| 4537 | |
| 4538 | if (cpus_intersects(groupmask, group->cpumask)) { |
| 4539 | printk("\n"); |
| 4540 | printk(KERN_ERR "ERROR: repeated CPUs\n"); |
| 4541 | } |
| 4542 | |
| 4543 | cpus_or(groupmask, groupmask, group->cpumask); |
| 4544 | |
| 4545 | cpumask_scnprintf(str, NR_CPUS, group->cpumask); |
| 4546 | printk(" %s", str); |
| 4547 | |
| 4548 | group = group->next; |
| 4549 | } while (group != sd->groups); |
| 4550 | printk("\n"); |
| 4551 | |
| 4552 | if (!cpus_equal(sd->span, groupmask)) |
| 4553 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
| 4554 | |
| 4555 | level++; |
| 4556 | sd = sd->parent; |
| 4557 | |
| 4558 | if (sd) { |
| 4559 | if (!cpus_subset(groupmask, sd->span)) |
| 4560 | printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n"); |
| 4561 | } |
| 4562 | |
| 4563 | } while (sd); |
| 4564 | } |
| 4565 | #else |
| 4566 | #define sched_domain_debug(sd, cpu) {} |
| 4567 | #endif |
| 4568 | |
| 4569 | /* |
| 4570 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
| 4571 | * hold the hotplug lock. |
| 4572 | */ |
| 4573 | void __devinit cpu_attach_domain(struct sched_domain *sd, int cpu) |
| 4574 | { |
| 4575 | migration_req_t req; |
| 4576 | unsigned long flags; |
| 4577 | runqueue_t *rq = cpu_rq(cpu); |
| 4578 | int local = 1; |
| 4579 | |
| 4580 | sched_domain_debug(sd, cpu); |
| 4581 | |
| 4582 | spin_lock_irqsave(&rq->lock, flags); |
| 4583 | |
| 4584 | if (cpu == smp_processor_id() || !cpu_online(cpu)) { |
| 4585 | rq->sd = sd; |
| 4586 | } else { |
| 4587 | init_completion(&req.done); |
| 4588 | req.type = REQ_SET_DOMAIN; |
| 4589 | req.sd = sd; |
| 4590 | list_add(&req.list, &rq->migration_queue); |
| 4591 | local = 0; |
| 4592 | } |
| 4593 | |
| 4594 | spin_unlock_irqrestore(&rq->lock, flags); |
| 4595 | |
| 4596 | if (!local) { |
| 4597 | wake_up_process(rq->migration_thread); |
| 4598 | wait_for_completion(&req.done); |
| 4599 | } |
| 4600 | } |
| 4601 | |
| 4602 | /* cpus with isolated domains */ |
| 4603 | cpumask_t __devinitdata cpu_isolated_map = CPU_MASK_NONE; |
| 4604 | |
| 4605 | /* Setup the mask of cpus configured for isolated domains */ |
| 4606 | static int __init isolated_cpu_setup(char *str) |
| 4607 | { |
| 4608 | int ints[NR_CPUS], i; |
| 4609 | |
| 4610 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 4611 | cpus_clear(cpu_isolated_map); |
| 4612 | for (i = 1; i <= ints[0]; i++) |
| 4613 | if (ints[i] < NR_CPUS) |
| 4614 | cpu_set(ints[i], cpu_isolated_map); |
| 4615 | return 1; |
| 4616 | } |
| 4617 | |
| 4618 | __setup ("isolcpus=", isolated_cpu_setup); |
| 4619 | |
| 4620 | /* |
| 4621 | * init_sched_build_groups takes an array of groups, the cpumask we wish |
| 4622 | * to span, and a pointer to a function which identifies what group a CPU |
| 4623 | * belongs to. The return value of group_fn must be a valid index into the |
| 4624 | * groups[] array, and must be >= 0 and < NR_CPUS (due to the fact that we |
| 4625 | * keep track of groups covered with a cpumask_t). |
| 4626 | * |
| 4627 | * init_sched_build_groups will build a circular linked list of the groups |
| 4628 | * covered by the given span, and will set each group's ->cpumask correctly, |
| 4629 | * and ->cpu_power to 0. |
| 4630 | */ |
| 4631 | void __devinit init_sched_build_groups(struct sched_group groups[], |
| 4632 | cpumask_t span, int (*group_fn)(int cpu)) |
| 4633 | { |
| 4634 | struct sched_group *first = NULL, *last = NULL; |
| 4635 | cpumask_t covered = CPU_MASK_NONE; |
| 4636 | int i; |
| 4637 | |
| 4638 | for_each_cpu_mask(i, span) { |
| 4639 | int group = group_fn(i); |
| 4640 | struct sched_group *sg = &groups[group]; |
| 4641 | int j; |
| 4642 | |
| 4643 | if (cpu_isset(i, covered)) |
| 4644 | continue; |
| 4645 | |
| 4646 | sg->cpumask = CPU_MASK_NONE; |
| 4647 | sg->cpu_power = 0; |
| 4648 | |
| 4649 | for_each_cpu_mask(j, span) { |
| 4650 | if (group_fn(j) != group) |
| 4651 | continue; |
| 4652 | |
| 4653 | cpu_set(j, covered); |
| 4654 | cpu_set(j, sg->cpumask); |
| 4655 | } |
| 4656 | if (!first) |
| 4657 | first = sg; |
| 4658 | if (last) |
| 4659 | last->next = sg; |
| 4660 | last = sg; |
| 4661 | } |
| 4662 | last->next = first; |
| 4663 | } |
| 4664 | |
| 4665 | |
| 4666 | #ifdef ARCH_HAS_SCHED_DOMAIN |
| 4667 | extern void __devinit arch_init_sched_domains(void); |
| 4668 | extern void __devinit arch_destroy_sched_domains(void); |
| 4669 | #else |
| 4670 | #ifdef CONFIG_SCHED_SMT |
| 4671 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); |
| 4672 | static struct sched_group sched_group_cpus[NR_CPUS]; |
| 4673 | static int __devinit cpu_to_cpu_group(int cpu) |
| 4674 | { |
| 4675 | return cpu; |
| 4676 | } |
| 4677 | #endif |
| 4678 | |
| 4679 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); |
| 4680 | static struct sched_group sched_group_phys[NR_CPUS]; |
| 4681 | static int __devinit cpu_to_phys_group(int cpu) |
| 4682 | { |
| 4683 | #ifdef CONFIG_SCHED_SMT |
| 4684 | return first_cpu(cpu_sibling_map[cpu]); |
| 4685 | #else |
| 4686 | return cpu; |
| 4687 | #endif |
| 4688 | } |
| 4689 | |
| 4690 | #ifdef CONFIG_NUMA |
| 4691 | |
| 4692 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
| 4693 | static struct sched_group sched_group_nodes[MAX_NUMNODES]; |
| 4694 | static int __devinit cpu_to_node_group(int cpu) |
| 4695 | { |
| 4696 | return cpu_to_node(cpu); |
| 4697 | } |
| 4698 | #endif |
| 4699 | |
| 4700 | #if defined(CONFIG_SCHED_SMT) && defined(CONFIG_NUMA) |
| 4701 | /* |
| 4702 | * The domains setup code relies on siblings not spanning |
| 4703 | * multiple nodes. Make sure the architecture has a proper |
| 4704 | * siblings map: |
| 4705 | */ |
| 4706 | static void check_sibling_maps(void) |
| 4707 | { |
| 4708 | int i, j; |
| 4709 | |
| 4710 | for_each_online_cpu(i) { |
| 4711 | for_each_cpu_mask(j, cpu_sibling_map[i]) { |
| 4712 | if (cpu_to_node(i) != cpu_to_node(j)) { |
| 4713 | printk(KERN_INFO "warning: CPU %d siblings map " |
| 4714 | "to different node - isolating " |
| 4715 | "them.\n", i); |
| 4716 | cpu_sibling_map[i] = cpumask_of_cpu(i); |
| 4717 | break; |
| 4718 | } |
| 4719 | } |
| 4720 | } |
| 4721 | } |
| 4722 | #endif |
| 4723 | |
| 4724 | /* |
| 4725 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
| 4726 | */ |
| 4727 | static void __devinit arch_init_sched_domains(void) |
| 4728 | { |
| 4729 | int i; |
| 4730 | cpumask_t cpu_default_map; |
| 4731 | |
| 4732 | #if defined(CONFIG_SCHED_SMT) && defined(CONFIG_NUMA) |
| 4733 | check_sibling_maps(); |
| 4734 | #endif |
| 4735 | /* |
| 4736 | * Setup mask for cpus without special case scheduling requirements. |
| 4737 | * For now this just excludes isolated cpus, but could be used to |
| 4738 | * exclude other special cases in the future. |
| 4739 | */ |
| 4740 | cpus_complement(cpu_default_map, cpu_isolated_map); |
| 4741 | cpus_and(cpu_default_map, cpu_default_map, cpu_online_map); |
| 4742 | |
| 4743 | /* |
| 4744 | * Set up domains. Isolated domains just stay on the dummy domain. |
| 4745 | */ |
| 4746 | for_each_cpu_mask(i, cpu_default_map) { |
| 4747 | int group; |
| 4748 | struct sched_domain *sd = NULL, *p; |
| 4749 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(i)); |
| 4750 | |
| 4751 | cpus_and(nodemask, nodemask, cpu_default_map); |
| 4752 | |
| 4753 | #ifdef CONFIG_NUMA |
| 4754 | sd = &per_cpu(node_domains, i); |
| 4755 | group = cpu_to_node_group(i); |
| 4756 | *sd = SD_NODE_INIT; |
| 4757 | sd->span = cpu_default_map; |
| 4758 | sd->groups = &sched_group_nodes[group]; |
| 4759 | #endif |
| 4760 | |
| 4761 | p = sd; |
| 4762 | sd = &per_cpu(phys_domains, i); |
| 4763 | group = cpu_to_phys_group(i); |
| 4764 | *sd = SD_CPU_INIT; |
| 4765 | sd->span = nodemask; |
| 4766 | sd->parent = p; |
| 4767 | sd->groups = &sched_group_phys[group]; |
| 4768 | |
| 4769 | #ifdef CONFIG_SCHED_SMT |
| 4770 | p = sd; |
| 4771 | sd = &per_cpu(cpu_domains, i); |
| 4772 | group = cpu_to_cpu_group(i); |
| 4773 | *sd = SD_SIBLING_INIT; |
| 4774 | sd->span = cpu_sibling_map[i]; |
| 4775 | cpus_and(sd->span, sd->span, cpu_default_map); |
| 4776 | sd->parent = p; |
| 4777 | sd->groups = &sched_group_cpus[group]; |
| 4778 | #endif |
| 4779 | } |
| 4780 | |
| 4781 | #ifdef CONFIG_SCHED_SMT |
| 4782 | /* Set up CPU (sibling) groups */ |
| 4783 | for_each_online_cpu(i) { |
| 4784 | cpumask_t this_sibling_map = cpu_sibling_map[i]; |
| 4785 | cpus_and(this_sibling_map, this_sibling_map, cpu_default_map); |
| 4786 | if (i != first_cpu(this_sibling_map)) |
| 4787 | continue; |
| 4788 | |
| 4789 | init_sched_build_groups(sched_group_cpus, this_sibling_map, |
| 4790 | &cpu_to_cpu_group); |
| 4791 | } |
| 4792 | #endif |
| 4793 | |
| 4794 | /* Set up physical groups */ |
| 4795 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 4796 | cpumask_t nodemask = node_to_cpumask(i); |
| 4797 | |
| 4798 | cpus_and(nodemask, nodemask, cpu_default_map); |
| 4799 | if (cpus_empty(nodemask)) |
| 4800 | continue; |
| 4801 | |
| 4802 | init_sched_build_groups(sched_group_phys, nodemask, |
| 4803 | &cpu_to_phys_group); |
| 4804 | } |
| 4805 | |
| 4806 | #ifdef CONFIG_NUMA |
| 4807 | /* Set up node groups */ |
| 4808 | init_sched_build_groups(sched_group_nodes, cpu_default_map, |
| 4809 | &cpu_to_node_group); |
| 4810 | #endif |
| 4811 | |
| 4812 | /* Calculate CPU power for physical packages and nodes */ |
| 4813 | for_each_cpu_mask(i, cpu_default_map) { |
| 4814 | int power; |
| 4815 | struct sched_domain *sd; |
| 4816 | #ifdef CONFIG_SCHED_SMT |
| 4817 | sd = &per_cpu(cpu_domains, i); |
| 4818 | power = SCHED_LOAD_SCALE; |
| 4819 | sd->groups->cpu_power = power; |
| 4820 | #endif |
| 4821 | |
| 4822 | sd = &per_cpu(phys_domains, i); |
| 4823 | power = SCHED_LOAD_SCALE + SCHED_LOAD_SCALE * |
| 4824 | (cpus_weight(sd->groups->cpumask)-1) / 10; |
| 4825 | sd->groups->cpu_power = power; |
| 4826 | |
| 4827 | #ifdef CONFIG_NUMA |
| 4828 | if (i == first_cpu(sd->groups->cpumask)) { |
| 4829 | /* Only add "power" once for each physical package. */ |
| 4830 | sd = &per_cpu(node_domains, i); |
| 4831 | sd->groups->cpu_power += power; |
| 4832 | } |
| 4833 | #endif |
| 4834 | } |
| 4835 | |
| 4836 | /* Attach the domains */ |
| 4837 | for_each_online_cpu(i) { |
| 4838 | struct sched_domain *sd; |
| 4839 | #ifdef CONFIG_SCHED_SMT |
| 4840 | sd = &per_cpu(cpu_domains, i); |
| 4841 | #else |
| 4842 | sd = &per_cpu(phys_domains, i); |
| 4843 | #endif |
| 4844 | cpu_attach_domain(sd, i); |
| 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | #ifdef CONFIG_HOTPLUG_CPU |
| 4849 | static void __devinit arch_destroy_sched_domains(void) |
| 4850 | { |
| 4851 | /* Do nothing: everything is statically allocated. */ |
| 4852 | } |
| 4853 | #endif |
| 4854 | |
| 4855 | #endif /* ARCH_HAS_SCHED_DOMAIN */ |
| 4856 | |
| 4857 | /* |
| 4858 | * Initial dummy domain for early boot and for hotplug cpu. Being static, |
| 4859 | * it is initialized to zero, so all balancing flags are cleared which is |
| 4860 | * what we want. |
| 4861 | */ |
| 4862 | static struct sched_domain sched_domain_dummy; |
| 4863 | |
| 4864 | #ifdef CONFIG_HOTPLUG_CPU |
| 4865 | /* |
| 4866 | * Force a reinitialization of the sched domains hierarchy. The domains |
| 4867 | * and groups cannot be updated in place without racing with the balancing |
| 4868 | * code, so we temporarily attach all running cpus to a "dummy" domain |
| 4869 | * which will prevent rebalancing while the sched domains are recalculated. |
| 4870 | */ |
| 4871 | static int update_sched_domains(struct notifier_block *nfb, |
| 4872 | unsigned long action, void *hcpu) |
| 4873 | { |
| 4874 | int i; |
| 4875 | |
| 4876 | switch (action) { |
| 4877 | case CPU_UP_PREPARE: |
| 4878 | case CPU_DOWN_PREPARE: |
| 4879 | for_each_online_cpu(i) |
| 4880 | cpu_attach_domain(&sched_domain_dummy, i); |
| 4881 | arch_destroy_sched_domains(); |
| 4882 | return NOTIFY_OK; |
| 4883 | |
| 4884 | case CPU_UP_CANCELED: |
| 4885 | case CPU_DOWN_FAILED: |
| 4886 | case CPU_ONLINE: |
| 4887 | case CPU_DEAD: |
| 4888 | /* |
| 4889 | * Fall through and re-initialise the domains. |
| 4890 | */ |
| 4891 | break; |
| 4892 | default: |
| 4893 | return NOTIFY_DONE; |
| 4894 | } |
| 4895 | |
| 4896 | /* The hotplug lock is already held by cpu_up/cpu_down */ |
| 4897 | arch_init_sched_domains(); |
| 4898 | |
| 4899 | return NOTIFY_OK; |
| 4900 | } |
| 4901 | #endif |
| 4902 | |
| 4903 | void __init sched_init_smp(void) |
| 4904 | { |
| 4905 | lock_cpu_hotplug(); |
| 4906 | arch_init_sched_domains(); |
| 4907 | unlock_cpu_hotplug(); |
| 4908 | /* XXX: Theoretical race here - CPU may be hotplugged now */ |
| 4909 | hotcpu_notifier(update_sched_domains, 0); |
| 4910 | } |
| 4911 | #else |
| 4912 | void __init sched_init_smp(void) |
| 4913 | { |
| 4914 | } |
| 4915 | #endif /* CONFIG_SMP */ |
| 4916 | |
| 4917 | int in_sched_functions(unsigned long addr) |
| 4918 | { |
| 4919 | /* Linker adds these: start and end of __sched functions */ |
| 4920 | extern char __sched_text_start[], __sched_text_end[]; |
| 4921 | return in_lock_functions(addr) || |
| 4922 | (addr >= (unsigned long)__sched_text_start |
| 4923 | && addr < (unsigned long)__sched_text_end); |
| 4924 | } |
| 4925 | |
| 4926 | void __init sched_init(void) |
| 4927 | { |
| 4928 | runqueue_t *rq; |
| 4929 | int i, j, k; |
| 4930 | |
| 4931 | for (i = 0; i < NR_CPUS; i++) { |
| 4932 | prio_array_t *array; |
| 4933 | |
| 4934 | rq = cpu_rq(i); |
| 4935 | spin_lock_init(&rq->lock); |
| 4936 | rq->active = rq->arrays; |
| 4937 | rq->expired = rq->arrays + 1; |
| 4938 | rq->best_expired_prio = MAX_PRIO; |
| 4939 | |
| 4940 | #ifdef CONFIG_SMP |
| 4941 | rq->sd = &sched_domain_dummy; |
| 4942 | rq->cpu_load = 0; |
| 4943 | rq->active_balance = 0; |
| 4944 | rq->push_cpu = 0; |
| 4945 | rq->migration_thread = NULL; |
| 4946 | INIT_LIST_HEAD(&rq->migration_queue); |
| 4947 | #endif |
| 4948 | atomic_set(&rq->nr_iowait, 0); |
| 4949 | |
| 4950 | for (j = 0; j < 2; j++) { |
| 4951 | array = rq->arrays + j; |
| 4952 | for (k = 0; k < MAX_PRIO; k++) { |
| 4953 | INIT_LIST_HEAD(array->queue + k); |
| 4954 | __clear_bit(k, array->bitmap); |
| 4955 | } |
| 4956 | // delimiter for bitsearch |
| 4957 | __set_bit(MAX_PRIO, array->bitmap); |
| 4958 | } |
| 4959 | } |
| 4960 | |
| 4961 | /* |
| 4962 | * The boot idle thread does lazy MMU switching as well: |
| 4963 | */ |
| 4964 | atomic_inc(&init_mm.mm_count); |
| 4965 | enter_lazy_tlb(&init_mm, current); |
| 4966 | |
| 4967 | /* |
| 4968 | * Make us the idle thread. Technically, schedule() should not be |
| 4969 | * called from this thread, however somewhere below it might be, |
| 4970 | * but because we are the idle thread, we just pick up running again |
| 4971 | * when this runqueue becomes "idle". |
| 4972 | */ |
| 4973 | init_idle(current, smp_processor_id()); |
| 4974 | } |
| 4975 | |
| 4976 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
| 4977 | void __might_sleep(char *file, int line) |
| 4978 | { |
| 4979 | #if defined(in_atomic) |
| 4980 | static unsigned long prev_jiffy; /* ratelimiting */ |
| 4981 | |
| 4982 | if ((in_atomic() || irqs_disabled()) && |
| 4983 | system_state == SYSTEM_RUNNING && !oops_in_progress) { |
| 4984 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
| 4985 | return; |
| 4986 | prev_jiffy = jiffies; |
| 4987 | printk(KERN_ERR "Debug: sleeping function called from invalid" |
| 4988 | " context at %s:%d\n", file, line); |
| 4989 | printk("in_atomic():%d, irqs_disabled():%d\n", |
| 4990 | in_atomic(), irqs_disabled()); |
| 4991 | dump_stack(); |
| 4992 | } |
| 4993 | #endif |
| 4994 | } |
| 4995 | EXPORT_SYMBOL(__might_sleep); |
| 4996 | #endif |
| 4997 | |
| 4998 | #ifdef CONFIG_MAGIC_SYSRQ |
| 4999 | void normalize_rt_tasks(void) |
| 5000 | { |
| 5001 | struct task_struct *p; |
| 5002 | prio_array_t *array; |
| 5003 | unsigned long flags; |
| 5004 | runqueue_t *rq; |
| 5005 | |
| 5006 | read_lock_irq(&tasklist_lock); |
| 5007 | for_each_process (p) { |
| 5008 | if (!rt_task(p)) |
| 5009 | continue; |
| 5010 | |
| 5011 | rq = task_rq_lock(p, &flags); |
| 5012 | |
| 5013 | array = p->array; |
| 5014 | if (array) |
| 5015 | deactivate_task(p, task_rq(p)); |
| 5016 | __setscheduler(p, SCHED_NORMAL, 0); |
| 5017 | if (array) { |
| 5018 | __activate_task(p, task_rq(p)); |
| 5019 | resched_task(rq->curr); |
| 5020 | } |
| 5021 | |
| 5022 | task_rq_unlock(rq, &flags); |
| 5023 | } |
| 5024 | read_unlock_irq(&tasklist_lock); |
| 5025 | } |
| 5026 | |
| 5027 | #endif /* CONFIG_MAGIC_SYSRQ */ |