sched_fair.c 26 KB

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