sched_fair.c 27 KB

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