sched_fair.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /*
  2. * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Interactivity improvements by Mike Galbraith
  7. * (C) 2007 Mike Galbraith <efault@gmx.de>
  8. *
  9. * Various enhancements by Dmitry Adamushko.
  10. * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
  11. *
  12. * Group scheduling enhancements by Srivatsa Vaddagiri
  13. * Copyright IBM Corporation, 2007
  14. * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
  15. *
  16. * Scaled math optimizations by Thomas Gleixner
  17. * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
  18. *
  19. * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
  20. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  21. */
  22. /*
  23. * Targeted preemption latency for CPU-bound tasks:
  24. * (default: 20ms, units: nanoseconds)
  25. *
  26. * NOTE: this latency value is not the same as the concept of
  27. * 'timeslice length' - timeslices in CFS are of variable length.
  28. * (to see the precise effective timeslice length of your workload,
  29. * run vmstat and monitor the context-switches field)
  30. *
  31. * On SMP systems the value of this is multiplied by the log2 of the
  32. * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
  33. * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
  34. * Targeted preemption latency for CPU-bound tasks:
  35. */
  36. const_debug unsigned int sysctl_sched_latency = 20000000ULL;
  37. /*
  38. * After fork, child runs first. (default) If set to 0 then
  39. * parent will (try to) run first.
  40. */
  41. const_debug unsigned int sysctl_sched_child_runs_first = 1;
  42. /*
  43. * Minimal preemption granularity for CPU-bound tasks:
  44. * (default: 2 msec, units: nanoseconds)
  45. */
  46. unsigned int sysctl_sched_min_granularity __read_mostly = 2000000ULL;
  47. /*
  48. * sys_sched_yield() compat mode
  49. *
  50. * This option switches the agressive yield implementation of the
  51. * old scheduler back on.
  52. */
  53. unsigned int __read_mostly sysctl_sched_compat_yield;
  54. /*
  55. * SCHED_BATCH wake-up granularity.
  56. * (default: 25 msec, units: nanoseconds)
  57. *
  58. * This option delays the preemption effects of decoupled workloads
  59. * and reduces their over-scheduling. Synchronous workloads will still
  60. * have immediate wakeup/sleep latencies.
  61. */
  62. const_debug unsigned int sysctl_sched_batch_wakeup_granularity = 25000000UL;
  63. /*
  64. * SCHED_OTHER wake-up granularity.
  65. * (default: 1 msec, units: nanoseconds)
  66. *
  67. * This option delays the preemption effects of decoupled workloads
  68. * and reduces their over-scheduling. Synchronous workloads will still
  69. * have immediate wakeup/sleep latencies.
  70. */
  71. const_debug unsigned int sysctl_sched_wakeup_granularity = 2000000UL;
  72. unsigned int sysctl_sched_runtime_limit __read_mostly;
  73. extern struct sched_class fair_sched_class;
  74. /**************************************************************
  75. * CFS operations on generic schedulable entities:
  76. */
  77. #ifdef CONFIG_FAIR_GROUP_SCHED
  78. /* cpu runqueue to which this cfs_rq is attached */
  79. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  80. {
  81. return cfs_rq->rq;
  82. }
  83. /* An entity is a task if it doesn't "own" a runqueue */
  84. #define entity_is_task(se) (!se->my_q)
  85. #else /* CONFIG_FAIR_GROUP_SCHED */
  86. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  87. {
  88. return container_of(cfs_rq, struct rq, cfs);
  89. }
  90. #define entity_is_task(se) 1
  91. #endif /* CONFIG_FAIR_GROUP_SCHED */
  92. static inline struct task_struct *task_of(struct sched_entity *se)
  93. {
  94. return container_of(se, struct task_struct, se);
  95. }
  96. /**************************************************************
  97. * Scheduling class tree data structure manipulation methods:
  98. */
  99. static inline u64
  100. max_vruntime(u64 min_vruntime, u64 vruntime)
  101. {
  102. if ((vruntime > min_vruntime) ||
  103. (min_vruntime > (1ULL << 61) && vruntime < (1ULL << 50)))
  104. min_vruntime = vruntime;
  105. return min_vruntime;
  106. }
  107. static inline void
  108. set_leftmost(struct cfs_rq *cfs_rq, struct rb_node *leftmost)
  109. {
  110. struct sched_entity *se;
  111. cfs_rq->rb_leftmost = leftmost;
  112. if (leftmost)
  113. se = rb_entry(leftmost, struct sched_entity, run_node);
  114. }
  115. static inline s64
  116. entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  117. {
  118. return se->vruntime - cfs_rq->min_vruntime;
  119. }
  120. /*
  121. * Enqueue an entity into the rb-tree:
  122. */
  123. static void
  124. __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  125. {
  126. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  127. struct rb_node *parent = NULL;
  128. struct sched_entity *entry;
  129. s64 key = entity_key(cfs_rq, se);
  130. int leftmost = 1;
  131. /*
  132. * Find the right place in the rbtree:
  133. */
  134. while (*link) {
  135. parent = *link;
  136. entry = rb_entry(parent, struct sched_entity, run_node);
  137. /*
  138. * We dont care about collisions. Nodes with
  139. * the same key stay together.
  140. */
  141. if (key < entity_key(cfs_rq, entry)) {
  142. link = &parent->rb_left;
  143. } else {
  144. link = &parent->rb_right;
  145. leftmost = 0;
  146. }
  147. }
  148. /*
  149. * Maintain a cache of leftmost tree entries (it is frequently
  150. * used):
  151. */
  152. if (leftmost)
  153. set_leftmost(cfs_rq, &se->run_node);
  154. rb_link_node(&se->run_node, parent, link);
  155. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  156. }
  157. static void
  158. __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  159. {
  160. if (cfs_rq->rb_leftmost == &se->run_node)
  161. set_leftmost(cfs_rq, rb_next(&se->run_node));
  162. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  163. }
  164. static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
  165. {
  166. return cfs_rq->rb_leftmost;
  167. }
  168. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  169. {
  170. return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
  171. }
  172. static inline struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  173. {
  174. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  175. struct sched_entity *se = NULL;
  176. struct rb_node *parent;
  177. while (*link) {
  178. parent = *link;
  179. se = rb_entry(parent, struct sched_entity, run_node);
  180. link = &parent->rb_right;
  181. }
  182. return se;
  183. }
  184. /**************************************************************
  185. * Scheduling class statistics methods:
  186. */
  187. static u64 __sched_period(unsigned long nr_running)
  188. {
  189. u64 period = sysctl_sched_latency;
  190. unsigned long nr_latency =
  191. sysctl_sched_latency / sysctl_sched_min_granularity;
  192. if (unlikely(nr_running > nr_latency)) {
  193. period *= nr_running;
  194. do_div(period, nr_latency);
  195. }
  196. return period;
  197. }
  198. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  199. {
  200. u64 period = __sched_period(cfs_rq->nr_running);
  201. period *= se->load.weight;
  202. do_div(period, cfs_rq->load.weight);
  203. return period;
  204. }
  205. /*
  206. * Update the current task's runtime statistics. Skip current tasks that
  207. * are not in our scheduling class.
  208. */
  209. static inline void
  210. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  211. unsigned long delta_exec)
  212. {
  213. unsigned long delta_exec_weighted;
  214. u64 next_vruntime, min_vruntime;
  215. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  216. curr->sum_exec_runtime += delta_exec;
  217. schedstat_add(cfs_rq, exec_clock, delta_exec);
  218. delta_exec_weighted = delta_exec;
  219. if (unlikely(curr->load.weight != NICE_0_LOAD)) {
  220. delta_exec_weighted = calc_delta_fair(delta_exec_weighted,
  221. &curr->load);
  222. }
  223. curr->vruntime += delta_exec_weighted;
  224. /*
  225. * maintain cfs_rq->min_vruntime to be a monotonic increasing
  226. * value tracking the leftmost vruntime in the tree.
  227. */
  228. if (first_fair(cfs_rq)) {
  229. next_vruntime = __pick_next_entity(cfs_rq)->vruntime;
  230. /* min_vruntime() := !max_vruntime() */
  231. min_vruntime = max_vruntime(curr->vruntime, next_vruntime);
  232. if (min_vruntime == next_vruntime)
  233. min_vruntime = curr->vruntime;
  234. else
  235. min_vruntime = next_vruntime;
  236. } else
  237. min_vruntime = curr->vruntime;
  238. cfs_rq->min_vruntime =
  239. max_vruntime(cfs_rq->min_vruntime, min_vruntime);
  240. }
  241. static void update_curr(struct cfs_rq *cfs_rq)
  242. {
  243. struct sched_entity *curr = cfs_rq->curr;
  244. u64 now = rq_of(cfs_rq)->clock;
  245. unsigned long delta_exec;
  246. if (unlikely(!curr))
  247. return;
  248. /*
  249. * Get the amount of time the current task was running
  250. * since the last time we changed load (this cannot
  251. * overflow on 32 bits):
  252. */
  253. delta_exec = (unsigned long)(now - curr->exec_start);
  254. __update_curr(cfs_rq, curr, delta_exec);
  255. curr->exec_start = now;
  256. }
  257. static inline void
  258. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  259. {
  260. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  261. }
  262. static inline unsigned long
  263. calc_weighted(unsigned long delta, struct sched_entity *se)
  264. {
  265. unsigned long weight = se->load.weight;
  266. if (unlikely(weight != NICE_0_LOAD))
  267. return (u64)delta * se->load.weight >> NICE_0_SHIFT;
  268. else
  269. return delta;
  270. }
  271. /*
  272. * Task is being enqueued - update stats:
  273. */
  274. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  275. {
  276. /*
  277. * Are we enqueueing a waiting task? (for current tasks
  278. * a dequeue/enqueue event is a NOP)
  279. */
  280. if (se != cfs_rq->curr)
  281. update_stats_wait_start(cfs_rq, se);
  282. }
  283. static void
  284. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  285. {
  286. schedstat_set(se->wait_max, max(se->wait_max,
  287. rq_of(cfs_rq)->clock - se->wait_start));
  288. schedstat_set(se->wait_start, 0);
  289. }
  290. static inline void
  291. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  292. {
  293. update_curr(cfs_rq);
  294. /*
  295. * Mark the end of the wait period if dequeueing a
  296. * waiting task:
  297. */
  298. if (se != cfs_rq->curr)
  299. update_stats_wait_end(cfs_rq, se);
  300. }
  301. /*
  302. * We are picking a new current task - update its stats:
  303. */
  304. static inline void
  305. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  306. {
  307. /*
  308. * We are starting a new run period:
  309. */
  310. se->exec_start = rq_of(cfs_rq)->clock;
  311. }
  312. /*
  313. * We are descheduling a task - update its stats:
  314. */
  315. static inline void
  316. update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  317. {
  318. se->exec_start = 0;
  319. }
  320. /**************************************************
  321. * Scheduling class queueing methods:
  322. */
  323. static void
  324. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  325. {
  326. update_load_add(&cfs_rq->load, se->load.weight);
  327. cfs_rq->nr_running++;
  328. se->on_rq = 1;
  329. }
  330. static void
  331. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  332. {
  333. update_load_sub(&cfs_rq->load, se->load.weight);
  334. cfs_rq->nr_running--;
  335. se->on_rq = 0;
  336. }
  337. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  338. {
  339. #ifdef CONFIG_SCHEDSTATS
  340. if (se->sleep_start) {
  341. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  342. if ((s64)delta < 0)
  343. delta = 0;
  344. if (unlikely(delta > se->sleep_max))
  345. se->sleep_max = delta;
  346. se->sleep_start = 0;
  347. se->sum_sleep_runtime += delta;
  348. }
  349. if (se->block_start) {
  350. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  351. if ((s64)delta < 0)
  352. delta = 0;
  353. if (unlikely(delta > se->block_max))
  354. se->block_max = delta;
  355. se->block_start = 0;
  356. se->sum_sleep_runtime += delta;
  357. /*
  358. * Blocking time is in units of nanosecs, so shift by 20 to
  359. * get a milliseconds-range estimation of the amount of
  360. * time that the task spent sleeping:
  361. */
  362. if (unlikely(prof_on == SLEEP_PROFILING)) {
  363. struct task_struct *tsk = task_of(se);
  364. profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
  365. delta >> 20);
  366. }
  367. }
  368. #endif
  369. }
  370. static void
  371. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  372. {
  373. u64 min_runtime, latency;
  374. min_runtime = cfs_rq->min_vruntime;
  375. if (sched_feat(USE_TREE_AVG)) {
  376. struct sched_entity *last = __pick_last_entity(cfs_rq);
  377. if (last) {
  378. min_runtime = __pick_next_entity(cfs_rq)->vruntime;
  379. min_runtime += last->vruntime;
  380. min_runtime >>= 1;
  381. }
  382. } else if (sched_feat(APPROX_AVG))
  383. min_runtime += sysctl_sched_latency/2;
  384. if (initial && sched_feat(START_DEBIT))
  385. min_runtime += sched_slice(cfs_rq, se);
  386. if (!initial && sched_feat(NEW_FAIR_SLEEPERS)) {
  387. latency = sysctl_sched_latency;
  388. if (min_runtime > latency)
  389. min_runtime -= latency;
  390. else
  391. min_runtime = 0;
  392. }
  393. se->vruntime = max(se->vruntime, min_runtime);
  394. }
  395. static void
  396. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  397. {
  398. /*
  399. * Update the fair clock.
  400. */
  401. update_curr(cfs_rq);
  402. if (wakeup) {
  403. place_entity(cfs_rq, se, 0);
  404. enqueue_sleeper(cfs_rq, se);
  405. }
  406. update_stats_enqueue(cfs_rq, se);
  407. if (se != cfs_rq->curr)
  408. __enqueue_entity(cfs_rq, se);
  409. account_entity_enqueue(cfs_rq, se);
  410. }
  411. static void
  412. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  413. {
  414. update_stats_dequeue(cfs_rq, se);
  415. #ifdef CONFIG_SCHEDSTATS
  416. if (sleep) {
  417. if (entity_is_task(se)) {
  418. struct task_struct *tsk = task_of(se);
  419. if (tsk->state & TASK_INTERRUPTIBLE)
  420. se->sleep_start = rq_of(cfs_rq)->clock;
  421. if (tsk->state & TASK_UNINTERRUPTIBLE)
  422. se->block_start = rq_of(cfs_rq)->clock;
  423. }
  424. }
  425. #endif
  426. if (se != cfs_rq->curr)
  427. __dequeue_entity(cfs_rq, se);
  428. account_entity_dequeue(cfs_rq, se);
  429. }
  430. /*
  431. * Preempt the current task with a newly woken task if needed:
  432. */
  433. static void
  434. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  435. {
  436. unsigned long ideal_runtime, delta_exec;
  437. ideal_runtime = sched_slice(cfs_rq, curr);
  438. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  439. if (delta_exec > ideal_runtime)
  440. resched_task(rq_of(cfs_rq)->curr);
  441. }
  442. static inline void
  443. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  444. {
  445. /*
  446. * Any task has to be enqueued before it get to execute on
  447. * a CPU. So account for the time it spent waiting on the
  448. * runqueue.
  449. */
  450. update_stats_wait_end(cfs_rq, se);
  451. update_stats_curr_start(cfs_rq, se);
  452. cfs_rq->curr = se;
  453. #ifdef CONFIG_SCHEDSTATS
  454. /*
  455. * Track our maximum slice length, if the CPU's load is at
  456. * least twice that of our own weight (i.e. dont track it
  457. * when there are only lesser-weight tasks around):
  458. */
  459. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  460. se->slice_max = max(se->slice_max,
  461. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  462. }
  463. #endif
  464. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  465. }
  466. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  467. {
  468. struct sched_entity *se = __pick_next_entity(cfs_rq);
  469. /* 'current' is not kept within the tree. */
  470. if (se)
  471. __dequeue_entity(cfs_rq, se);
  472. set_next_entity(cfs_rq, se);
  473. return se;
  474. }
  475. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  476. {
  477. /*
  478. * If still on the runqueue then deactivate_task()
  479. * was not called and update_curr() has to be done:
  480. */
  481. if (prev->on_rq)
  482. update_curr(cfs_rq);
  483. update_stats_curr_end(cfs_rq, prev);
  484. if (prev->on_rq) {
  485. update_stats_wait_start(cfs_rq, prev);
  486. /* Put 'current' back into the tree. */
  487. __enqueue_entity(cfs_rq, prev);
  488. }
  489. cfs_rq->curr = NULL;
  490. }
  491. static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  492. {
  493. /*
  494. * Update run-time statistics of the 'current'.
  495. */
  496. update_curr(cfs_rq);
  497. if (cfs_rq->nr_running > 1)
  498. check_preempt_tick(cfs_rq, curr);
  499. }
  500. /**************************************************
  501. * CFS operations on tasks:
  502. */
  503. #ifdef CONFIG_FAIR_GROUP_SCHED
  504. /* Walk up scheduling entities hierarchy */
  505. #define for_each_sched_entity(se) \
  506. for (; se; se = se->parent)
  507. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  508. {
  509. return p->se.cfs_rq;
  510. }
  511. /* runqueue on which this entity is (to be) queued */
  512. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  513. {
  514. return se->cfs_rq;
  515. }
  516. /* runqueue "owned" by this group */
  517. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  518. {
  519. return grp->my_q;
  520. }
  521. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  522. * another cpu ('this_cpu')
  523. */
  524. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  525. {
  526. return cfs_rq->tg->cfs_rq[this_cpu];
  527. }
  528. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  529. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  530. list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  531. /* Do the two (enqueued) tasks belong to the same group ? */
  532. static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
  533. {
  534. if (curr->se.cfs_rq == p->se.cfs_rq)
  535. return 1;
  536. return 0;
  537. }
  538. #else /* CONFIG_FAIR_GROUP_SCHED */
  539. #define for_each_sched_entity(se) \
  540. for (; se; se = NULL)
  541. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  542. {
  543. return &task_rq(p)->cfs;
  544. }
  545. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  546. {
  547. struct task_struct *p = task_of(se);
  548. struct rq *rq = task_rq(p);
  549. return &rq->cfs;
  550. }
  551. /* runqueue "owned" by this group */
  552. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  553. {
  554. return NULL;
  555. }
  556. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  557. {
  558. return &cpu_rq(this_cpu)->cfs;
  559. }
  560. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  561. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  562. static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
  563. {
  564. return 1;
  565. }
  566. #endif /* CONFIG_FAIR_GROUP_SCHED */
  567. /*
  568. * The enqueue_task method is called before nr_running is
  569. * increased. Here we update the fair scheduling stats and
  570. * then put the task into the rbtree:
  571. */
  572. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  573. {
  574. struct cfs_rq *cfs_rq;
  575. struct sched_entity *se = &p->se;
  576. for_each_sched_entity(se) {
  577. if (se->on_rq)
  578. break;
  579. cfs_rq = cfs_rq_of(se);
  580. enqueue_entity(cfs_rq, se, wakeup);
  581. }
  582. }
  583. /*
  584. * The dequeue_task method is called before nr_running is
  585. * decreased. We remove the task from the rbtree and
  586. * update the fair scheduling stats:
  587. */
  588. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  589. {
  590. struct cfs_rq *cfs_rq;
  591. struct sched_entity *se = &p->se;
  592. for_each_sched_entity(se) {
  593. cfs_rq = cfs_rq_of(se);
  594. dequeue_entity(cfs_rq, se, sleep);
  595. /* Don't dequeue parent if it has other entities besides us */
  596. if (cfs_rq->load.weight)
  597. break;
  598. }
  599. }
  600. /*
  601. * sched_yield() support is very simple - we dequeue and enqueue.
  602. *
  603. * If compat_yield is turned on then we requeue to the end of the tree.
  604. */
  605. static void yield_task_fair(struct rq *rq, struct task_struct *p)
  606. {
  607. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  608. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  609. struct sched_entity *rightmost, *se = &p->se;
  610. struct rb_node *parent;
  611. /*
  612. * Are we the only task in the tree?
  613. */
  614. if (unlikely(cfs_rq->nr_running == 1))
  615. return;
  616. if (likely(!sysctl_sched_compat_yield)) {
  617. __update_rq_clock(rq);
  618. /*
  619. * Dequeue and enqueue the task to update its
  620. * position within the tree:
  621. */
  622. dequeue_entity(cfs_rq, &p->se, 0);
  623. enqueue_entity(cfs_rq, &p->se, 0);
  624. return;
  625. }
  626. /*
  627. * Find the rightmost entry in the rbtree:
  628. */
  629. do {
  630. parent = *link;
  631. link = &parent->rb_right;
  632. } while (*link);
  633. rightmost = rb_entry(parent, struct sched_entity, run_node);
  634. /*
  635. * Already in the rightmost position?
  636. */
  637. if (unlikely(rightmost == se))
  638. return;
  639. /*
  640. * Minimally necessary key value to be last in the tree:
  641. */
  642. se->vruntime = rightmost->vruntime + 1;
  643. if (cfs_rq->rb_leftmost == &se->run_node)
  644. cfs_rq->rb_leftmost = rb_next(&se->run_node);
  645. /*
  646. * Relink the task to the rightmost position:
  647. */
  648. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  649. rb_link_node(&se->run_node, parent, link);
  650. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  651. }
  652. /*
  653. * Preempt the current task with a newly woken task if needed:
  654. */
  655. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
  656. {
  657. struct task_struct *curr = rq->curr;
  658. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  659. if (unlikely(rt_prio(p->prio))) {
  660. update_rq_clock(rq);
  661. update_curr(cfs_rq);
  662. resched_task(curr);
  663. return;
  664. }
  665. if (is_same_group(curr, p)) {
  666. s64 delta = curr->se.vruntime - p->se.vruntime;
  667. if (delta > (s64)sysctl_sched_wakeup_granularity)
  668. resched_task(curr);
  669. }
  670. }
  671. static struct task_struct *pick_next_task_fair(struct rq *rq)
  672. {
  673. struct cfs_rq *cfs_rq = &rq->cfs;
  674. struct sched_entity *se;
  675. if (unlikely(!cfs_rq->nr_running))
  676. return NULL;
  677. do {
  678. se = pick_next_entity(cfs_rq);
  679. cfs_rq = group_cfs_rq(se);
  680. } while (cfs_rq);
  681. return task_of(se);
  682. }
  683. /*
  684. * Account for a descheduled task:
  685. */
  686. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  687. {
  688. struct sched_entity *se = &prev->se;
  689. struct cfs_rq *cfs_rq;
  690. for_each_sched_entity(se) {
  691. cfs_rq = cfs_rq_of(se);
  692. put_prev_entity(cfs_rq, se);
  693. }
  694. }
  695. /**************************************************
  696. * Fair scheduling class load-balancing methods:
  697. */
  698. /*
  699. * Load-balancing iterator. Note: while the runqueue stays locked
  700. * during the whole iteration, the current task might be
  701. * dequeued so the iterator has to be dequeue-safe. Here we
  702. * achieve that by always pre-iterating before returning
  703. * the current task:
  704. */
  705. static inline struct task_struct *
  706. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  707. {
  708. struct task_struct *p;
  709. if (!curr)
  710. return NULL;
  711. p = rb_entry(curr, struct task_struct, se.run_node);
  712. cfs_rq->rb_load_balance_curr = rb_next(curr);
  713. return p;
  714. }
  715. static struct task_struct *load_balance_start_fair(void *arg)
  716. {
  717. struct cfs_rq *cfs_rq = arg;
  718. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  719. }
  720. static struct task_struct *load_balance_next_fair(void *arg)
  721. {
  722. struct cfs_rq *cfs_rq = arg;
  723. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  724. }
  725. #ifdef CONFIG_FAIR_GROUP_SCHED
  726. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  727. {
  728. struct sched_entity *curr;
  729. struct task_struct *p;
  730. if (!cfs_rq->nr_running)
  731. return MAX_PRIO;
  732. curr = __pick_next_entity(cfs_rq);
  733. p = task_of(curr);
  734. return p->prio;
  735. }
  736. #endif
  737. static unsigned long
  738. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  739. unsigned long max_nr_move, unsigned long max_load_move,
  740. struct sched_domain *sd, enum cpu_idle_type idle,
  741. int *all_pinned, int *this_best_prio)
  742. {
  743. struct cfs_rq *busy_cfs_rq;
  744. unsigned long load_moved, total_nr_moved = 0, nr_moved;
  745. long rem_load_move = max_load_move;
  746. struct rq_iterator cfs_rq_iterator;
  747. cfs_rq_iterator.start = load_balance_start_fair;
  748. cfs_rq_iterator.next = load_balance_next_fair;
  749. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  750. #ifdef CONFIG_FAIR_GROUP_SCHED
  751. struct cfs_rq *this_cfs_rq;
  752. long imbalance;
  753. unsigned long maxload;
  754. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  755. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  756. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  757. if (imbalance <= 0)
  758. continue;
  759. /* Don't pull more than imbalance/2 */
  760. imbalance /= 2;
  761. maxload = min(rem_load_move, imbalance);
  762. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  763. #else
  764. # define maxload rem_load_move
  765. #endif
  766. /* pass busy_cfs_rq argument into
  767. * load_balance_[start|next]_fair iterators
  768. */
  769. cfs_rq_iterator.arg = busy_cfs_rq;
  770. nr_moved = balance_tasks(this_rq, this_cpu, busiest,
  771. max_nr_move, maxload, sd, idle, all_pinned,
  772. &load_moved, this_best_prio, &cfs_rq_iterator);
  773. total_nr_moved += nr_moved;
  774. max_nr_move -= nr_moved;
  775. rem_load_move -= load_moved;
  776. if (max_nr_move <= 0 || rem_load_move <= 0)
  777. break;
  778. }
  779. return max_load_move - rem_load_move;
  780. }
  781. /*
  782. * scheduler tick hitting a task of our scheduling class:
  783. */
  784. static void task_tick_fair(struct rq *rq, struct task_struct *curr)
  785. {
  786. struct cfs_rq *cfs_rq;
  787. struct sched_entity *se = &curr->se;
  788. for_each_sched_entity(se) {
  789. cfs_rq = cfs_rq_of(se);
  790. entity_tick(cfs_rq, se);
  791. }
  792. }
  793. #define swap(a,b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
  794. /*
  795. * Share the fairness runtime between parent and child, thus the
  796. * total amount of pressure for CPU stays equal - new tasks
  797. * get a chance to run but frequent forkers are not allowed to
  798. * monopolize the CPU. Note: the parent runqueue is locked,
  799. * the child is not running yet.
  800. */
  801. static void task_new_fair(struct rq *rq, struct task_struct *p)
  802. {
  803. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  804. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  805. sched_info_queued(p);
  806. update_curr(cfs_rq);
  807. place_entity(cfs_rq, se, 1);
  808. if (sysctl_sched_child_runs_first &&
  809. curr->vruntime < se->vruntime) {
  810. /*
  811. * Upon rescheduling, sched_class::put_prev_task() will place
  812. * 'current' within the tree based on its new key value.
  813. */
  814. swap(curr->vruntime, se->vruntime);
  815. }
  816. update_stats_enqueue(cfs_rq, se);
  817. __enqueue_entity(cfs_rq, se);
  818. account_entity_enqueue(cfs_rq, se);
  819. resched_task(rq->curr);
  820. }
  821. #ifdef CONFIG_FAIR_GROUP_SCHED
  822. /* Account for a task changing its policy or group.
  823. *
  824. * This routine is mostly called to set cfs_rq->curr field when a task
  825. * migrates between groups/classes.
  826. */
  827. static void set_curr_task_fair(struct rq *rq)
  828. {
  829. struct sched_entity *se = &rq->curr->se;
  830. for_each_sched_entity(se)
  831. set_next_entity(cfs_rq_of(se), se);
  832. }
  833. #else
  834. static void set_curr_task_fair(struct rq *rq)
  835. {
  836. struct sched_entity *se = &rq->curr->se;
  837. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  838. cfs_rq->curr = se;
  839. }
  840. #endif
  841. /*
  842. * All the scheduling class methods:
  843. */
  844. struct sched_class fair_sched_class __read_mostly = {
  845. .enqueue_task = enqueue_task_fair,
  846. .dequeue_task = dequeue_task_fair,
  847. .yield_task = yield_task_fair,
  848. .check_preempt_curr = check_preempt_wakeup,
  849. .pick_next_task = pick_next_task_fair,
  850. .put_prev_task = put_prev_task_fair,
  851. .load_balance = load_balance_fair,
  852. .set_curr_task = set_curr_task_fair,
  853. .task_tick = task_tick_fair,
  854. .task_new = task_new_fair,
  855. };
  856. #ifdef CONFIG_SCHED_DEBUG
  857. static void print_cfs_stats(struct seq_file *m, int cpu)
  858. {
  859. struct cfs_rq *cfs_rq;
  860. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  861. print_cfs_rq(m, cpu, cfs_rq);
  862. }
  863. #endif