sched_fair.c 26 KB

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