sched_fair.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 * 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: 1 msec * ilog(ncpus), units: nanoseconds)
  38. */
  39. unsigned int sysctl_sched_min_granularity = 1000000ULL;
  40. /*
  41. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  42. */
  43. unsigned int sched_nr_latency = 20;
  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 * 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 * 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. }
  295. static inline void
  296. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  297. {
  298. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  299. }
  300. /*
  301. * Task is being enqueued - update stats:
  302. */
  303. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  304. {
  305. /*
  306. * Are we enqueueing a waiting task? (for current tasks
  307. * a dequeue/enqueue event is a NOP)
  308. */
  309. if (se != cfs_rq->curr)
  310. update_stats_wait_start(cfs_rq, se);
  311. }
  312. static void
  313. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  314. {
  315. schedstat_set(se->wait_max, max(se->wait_max,
  316. rq_of(cfs_rq)->clock - se->wait_start));
  317. schedstat_set(se->wait_start, 0);
  318. }
  319. static inline void
  320. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  321. {
  322. /*
  323. * Mark the end of the wait period if dequeueing a
  324. * waiting task:
  325. */
  326. if (se != cfs_rq->curr)
  327. update_stats_wait_end(cfs_rq, se);
  328. }
  329. /*
  330. * We are picking a new current task - update its stats:
  331. */
  332. static inline void
  333. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  334. {
  335. /*
  336. * We are starting a new run period:
  337. */
  338. se->exec_start = rq_of(cfs_rq)->clock;
  339. }
  340. /**************************************************
  341. * Scheduling class queueing methods:
  342. */
  343. static void
  344. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  345. {
  346. update_load_add(&cfs_rq->load, se->load.weight);
  347. cfs_rq->nr_running++;
  348. se->on_rq = 1;
  349. }
  350. static void
  351. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  352. {
  353. update_load_sub(&cfs_rq->load, se->load.weight);
  354. cfs_rq->nr_running--;
  355. se->on_rq = 0;
  356. }
  357. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  358. {
  359. #ifdef CONFIG_SCHEDSTATS
  360. if (se->sleep_start) {
  361. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  362. if ((s64)delta < 0)
  363. delta = 0;
  364. if (unlikely(delta > se->sleep_max))
  365. se->sleep_max = delta;
  366. se->sleep_start = 0;
  367. se->sum_sleep_runtime += delta;
  368. }
  369. if (se->block_start) {
  370. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  371. if ((s64)delta < 0)
  372. delta = 0;
  373. if (unlikely(delta > se->block_max))
  374. se->block_max = delta;
  375. se->block_start = 0;
  376. se->sum_sleep_runtime += delta;
  377. /*
  378. * Blocking time is in units of nanosecs, so shift by 20 to
  379. * get a milliseconds-range estimation of the amount of
  380. * time that the task spent sleeping:
  381. */
  382. if (unlikely(prof_on == SLEEP_PROFILING)) {
  383. struct task_struct *tsk = task_of(se);
  384. profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
  385. delta >> 20);
  386. }
  387. }
  388. #endif
  389. }
  390. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  391. {
  392. #ifdef CONFIG_SCHED_DEBUG
  393. s64 d = se->vruntime - cfs_rq->min_vruntime;
  394. if (d < 0)
  395. d = -d;
  396. if (d > 3*sysctl_sched_latency)
  397. schedstat_inc(cfs_rq, nr_spread_over);
  398. #endif
  399. }
  400. static void
  401. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  402. {
  403. u64 vruntime;
  404. vruntime = cfs_rq->min_vruntime;
  405. if (sched_feat(TREE_AVG)) {
  406. struct sched_entity *last = __pick_last_entity(cfs_rq);
  407. if (last) {
  408. vruntime += last->vruntime;
  409. vruntime >>= 1;
  410. }
  411. } else if (sched_feat(APPROX_AVG) && cfs_rq->nr_running)
  412. vruntime += sched_vslice(cfs_rq)/2;
  413. /*
  414. * The 'current' period is already promised to the current tasks,
  415. * however the extra weight of the new task will slow them down a
  416. * little, place the new task so that it fits in the slot that
  417. * stays open at the end.
  418. */
  419. if (initial && sched_feat(START_DEBIT))
  420. vruntime += sched_vslice_add(cfs_rq, se);
  421. if (!initial) {
  422. /* sleeps upto a single latency don't count. */
  423. if (sched_feat(NEW_FAIR_SLEEPERS) && entity_is_task(se) &&
  424. task_of(se)->policy != SCHED_BATCH)
  425. vruntime -= sysctl_sched_latency;
  426. /* ensure we never gain time by being placed backwards. */
  427. vruntime = max_vruntime(se->vruntime, vruntime);
  428. }
  429. se->vruntime = vruntime;
  430. }
  431. static void
  432. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  433. {
  434. /*
  435. * Update run-time statistics of the 'current'.
  436. */
  437. update_curr(cfs_rq);
  438. if (wakeup) {
  439. place_entity(cfs_rq, se, 0);
  440. enqueue_sleeper(cfs_rq, se);
  441. }
  442. update_stats_enqueue(cfs_rq, se);
  443. check_spread(cfs_rq, se);
  444. if (se != cfs_rq->curr)
  445. __enqueue_entity(cfs_rq, se);
  446. account_entity_enqueue(cfs_rq, se);
  447. }
  448. static void
  449. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  450. {
  451. /*
  452. * Update run-time statistics of the 'current'.
  453. */
  454. update_curr(cfs_rq);
  455. update_stats_dequeue(cfs_rq, se);
  456. if (sleep) {
  457. #ifdef CONFIG_SCHEDSTATS
  458. if (entity_is_task(se)) {
  459. struct task_struct *tsk = task_of(se);
  460. if (tsk->state & TASK_INTERRUPTIBLE)
  461. se->sleep_start = rq_of(cfs_rq)->clock;
  462. if (tsk->state & TASK_UNINTERRUPTIBLE)
  463. se->block_start = rq_of(cfs_rq)->clock;
  464. }
  465. #endif
  466. }
  467. if (se != cfs_rq->curr)
  468. __dequeue_entity(cfs_rq, se);
  469. account_entity_dequeue(cfs_rq, se);
  470. }
  471. /*
  472. * Preempt the current task with a newly woken task if needed:
  473. */
  474. static void
  475. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  476. {
  477. unsigned long ideal_runtime, delta_exec;
  478. ideal_runtime = sched_slice(cfs_rq, curr);
  479. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  480. if (delta_exec > ideal_runtime)
  481. resched_task(rq_of(cfs_rq)->curr);
  482. }
  483. static void
  484. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  485. {
  486. /* 'current' is not kept within the tree. */
  487. if (se->on_rq) {
  488. /*
  489. * Any task has to be enqueued before it get to execute on
  490. * a CPU. So account for the time it spent waiting on the
  491. * runqueue.
  492. */
  493. update_stats_wait_end(cfs_rq, se);
  494. __dequeue_entity(cfs_rq, se);
  495. }
  496. update_stats_curr_start(cfs_rq, se);
  497. cfs_rq->curr = se;
  498. #ifdef CONFIG_SCHEDSTATS
  499. /*
  500. * Track our maximum slice length, if the CPU's load is at
  501. * least twice that of our own weight (i.e. dont track it
  502. * when there are only lesser-weight tasks around):
  503. */
  504. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  505. se->slice_max = max(se->slice_max,
  506. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  507. }
  508. #endif
  509. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  510. }
  511. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  512. {
  513. struct sched_entity *se = NULL;
  514. if (first_fair(cfs_rq)) {
  515. se = __pick_next_entity(cfs_rq);
  516. set_next_entity(cfs_rq, se);
  517. }
  518. return se;
  519. }
  520. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  521. {
  522. /*
  523. * If still on the runqueue then deactivate_task()
  524. * was not called and update_curr() has to be done:
  525. */
  526. if (prev->on_rq)
  527. update_curr(cfs_rq);
  528. check_spread(cfs_rq, prev);
  529. if (prev->on_rq) {
  530. update_stats_wait_start(cfs_rq, prev);
  531. /* Put 'current' back into the tree. */
  532. __enqueue_entity(cfs_rq, prev);
  533. }
  534. cfs_rq->curr = NULL;
  535. }
  536. static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  537. {
  538. /*
  539. * Update run-time statistics of the 'current'.
  540. */
  541. update_curr(cfs_rq);
  542. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  543. check_preempt_tick(cfs_rq, curr);
  544. }
  545. /**************************************************
  546. * CFS operations on tasks:
  547. */
  548. #ifdef CONFIG_FAIR_GROUP_SCHED
  549. /* Walk up scheduling entities hierarchy */
  550. #define for_each_sched_entity(se) \
  551. for (; se; se = se->parent)
  552. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  553. {
  554. return p->se.cfs_rq;
  555. }
  556. /* runqueue on which this entity is (to be) queued */
  557. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  558. {
  559. return se->cfs_rq;
  560. }
  561. /* runqueue "owned" by this group */
  562. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  563. {
  564. return grp->my_q;
  565. }
  566. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  567. * another cpu ('this_cpu')
  568. */
  569. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  570. {
  571. return cfs_rq->tg->cfs_rq[this_cpu];
  572. }
  573. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  574. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  575. list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  576. /* Do the two (enqueued) entities belong to the same group ? */
  577. static inline int
  578. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  579. {
  580. if (se->cfs_rq == pse->cfs_rq)
  581. return 1;
  582. return 0;
  583. }
  584. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  585. {
  586. return se->parent;
  587. }
  588. #else /* CONFIG_FAIR_GROUP_SCHED */
  589. #define for_each_sched_entity(se) \
  590. for (; se; se = NULL)
  591. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  592. {
  593. return &task_rq(p)->cfs;
  594. }
  595. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  596. {
  597. struct task_struct *p = task_of(se);
  598. struct rq *rq = task_rq(p);
  599. return &rq->cfs;
  600. }
  601. /* runqueue "owned" by this group */
  602. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  603. {
  604. return NULL;
  605. }
  606. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  607. {
  608. return &cpu_rq(this_cpu)->cfs;
  609. }
  610. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  611. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  612. static inline int
  613. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  614. {
  615. return 1;
  616. }
  617. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  618. {
  619. return NULL;
  620. }
  621. #endif /* CONFIG_FAIR_GROUP_SCHED */
  622. /*
  623. * The enqueue_task method is called before nr_running is
  624. * increased. Here we update the fair scheduling stats and
  625. * then put the task into the rbtree:
  626. */
  627. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  628. {
  629. struct cfs_rq *cfs_rq;
  630. struct sched_entity *se = &p->se;
  631. for_each_sched_entity(se) {
  632. if (se->on_rq)
  633. break;
  634. cfs_rq = cfs_rq_of(se);
  635. enqueue_entity(cfs_rq, se, wakeup);
  636. wakeup = 1;
  637. }
  638. }
  639. /*
  640. * The dequeue_task method is called before nr_running is
  641. * decreased. We remove the task from the rbtree and
  642. * update the fair scheduling stats:
  643. */
  644. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  645. {
  646. struct cfs_rq *cfs_rq;
  647. struct sched_entity *se = &p->se;
  648. for_each_sched_entity(se) {
  649. cfs_rq = cfs_rq_of(se);
  650. dequeue_entity(cfs_rq, se, sleep);
  651. /* Don't dequeue parent if it has other entities besides us */
  652. if (cfs_rq->load.weight)
  653. break;
  654. sleep = 1;
  655. }
  656. }
  657. /*
  658. * sched_yield() support is very simple - we dequeue and enqueue.
  659. *
  660. * If compat_yield is turned on then we requeue to the end of the tree.
  661. */
  662. static void yield_task_fair(struct rq *rq)
  663. {
  664. struct cfs_rq *cfs_rq = task_cfs_rq(rq->curr);
  665. struct sched_entity *rightmost, *se = &rq->curr->se;
  666. /*
  667. * Are we the only task in the tree?
  668. */
  669. if (unlikely(cfs_rq->nr_running == 1))
  670. return;
  671. if (likely(!sysctl_sched_compat_yield)) {
  672. __update_rq_clock(rq);
  673. /*
  674. * Update run-time statistics of the 'current'.
  675. */
  676. update_curr(cfs_rq);
  677. return;
  678. }
  679. /*
  680. * Find the rightmost entry in the rbtree:
  681. */
  682. rightmost = __pick_last_entity(cfs_rq);
  683. /*
  684. * Already in the rightmost position?
  685. */
  686. if (unlikely(rightmost->vruntime < se->vruntime))
  687. return;
  688. /*
  689. * Minimally necessary key value to be last in the tree:
  690. * Upon rescheduling, sched_class::put_prev_task() will place
  691. * 'current' within the tree based on its new key value.
  692. */
  693. se->vruntime = rightmost->vruntime + 1;
  694. }
  695. /*
  696. * Preempt the current task with a newly woken task if needed:
  697. */
  698. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
  699. {
  700. struct task_struct *curr = rq->curr;
  701. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  702. struct sched_entity *se = &curr->se, *pse = &p->se;
  703. unsigned long gran;
  704. if (unlikely(rt_prio(p->prio))) {
  705. update_rq_clock(rq);
  706. update_curr(cfs_rq);
  707. resched_task(curr);
  708. return;
  709. }
  710. /*
  711. * Batch tasks do not preempt (their preemption is driven by
  712. * the tick):
  713. */
  714. if (unlikely(p->policy == SCHED_BATCH))
  715. return;
  716. if (!sched_feat(WAKEUP_PREEMPT))
  717. return;
  718. while (!is_same_group(se, pse)) {
  719. se = parent_entity(se);
  720. pse = parent_entity(pse);
  721. }
  722. gran = sysctl_sched_wakeup_granularity;
  723. if (unlikely(se->load.weight != NICE_0_LOAD))
  724. gran = calc_delta_fair(gran, &se->load);
  725. if (pse->vruntime + gran < se->vruntime)
  726. resched_task(curr);
  727. }
  728. static struct task_struct *pick_next_task_fair(struct rq *rq)
  729. {
  730. struct cfs_rq *cfs_rq = &rq->cfs;
  731. struct sched_entity *se;
  732. if (unlikely(!cfs_rq->nr_running))
  733. return NULL;
  734. do {
  735. se = pick_next_entity(cfs_rq);
  736. cfs_rq = group_cfs_rq(se);
  737. } while (cfs_rq);
  738. return task_of(se);
  739. }
  740. /*
  741. * Account for a descheduled task:
  742. */
  743. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  744. {
  745. struct sched_entity *se = &prev->se;
  746. struct cfs_rq *cfs_rq;
  747. for_each_sched_entity(se) {
  748. cfs_rq = cfs_rq_of(se);
  749. put_prev_entity(cfs_rq, se);
  750. }
  751. }
  752. #ifdef CONFIG_SMP
  753. /**************************************************
  754. * Fair scheduling class load-balancing methods:
  755. */
  756. /*
  757. * Load-balancing iterator. Note: while the runqueue stays locked
  758. * during the whole iteration, the current task might be
  759. * dequeued so the iterator has to be dequeue-safe. Here we
  760. * achieve that by always pre-iterating before returning
  761. * the current task:
  762. */
  763. static struct task_struct *
  764. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  765. {
  766. struct task_struct *p;
  767. if (!curr)
  768. return NULL;
  769. p = rb_entry(curr, struct task_struct, se.run_node);
  770. cfs_rq->rb_load_balance_curr = rb_next(curr);
  771. return p;
  772. }
  773. static struct task_struct *load_balance_start_fair(void *arg)
  774. {
  775. struct cfs_rq *cfs_rq = arg;
  776. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  777. }
  778. static struct task_struct *load_balance_next_fair(void *arg)
  779. {
  780. struct cfs_rq *cfs_rq = arg;
  781. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  782. }
  783. #ifdef CONFIG_FAIR_GROUP_SCHED
  784. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  785. {
  786. struct sched_entity *curr;
  787. struct task_struct *p;
  788. if (!cfs_rq->nr_running)
  789. return MAX_PRIO;
  790. curr = cfs_rq->curr;
  791. if (!curr)
  792. curr = __pick_next_entity(cfs_rq);
  793. p = task_of(curr);
  794. return p->prio;
  795. }
  796. #endif
  797. static unsigned long
  798. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  799. unsigned long max_load_move,
  800. struct sched_domain *sd, enum cpu_idle_type idle,
  801. int *all_pinned, int *this_best_prio)
  802. {
  803. struct cfs_rq *busy_cfs_rq;
  804. long rem_load_move = max_load_move;
  805. struct rq_iterator cfs_rq_iterator;
  806. cfs_rq_iterator.start = load_balance_start_fair;
  807. cfs_rq_iterator.next = load_balance_next_fair;
  808. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  809. #ifdef CONFIG_FAIR_GROUP_SCHED
  810. struct cfs_rq *this_cfs_rq;
  811. long imbalance;
  812. unsigned long maxload;
  813. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  814. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  815. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  816. if (imbalance <= 0)
  817. continue;
  818. /* Don't pull more than imbalance/2 */
  819. imbalance /= 2;
  820. maxload = min(rem_load_move, imbalance);
  821. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  822. #else
  823. # define maxload rem_load_move
  824. #endif
  825. /*
  826. * pass busy_cfs_rq argument into
  827. * load_balance_[start|next]_fair iterators
  828. */
  829. cfs_rq_iterator.arg = busy_cfs_rq;
  830. rem_load_move -= balance_tasks(this_rq, this_cpu, busiest,
  831. maxload, sd, idle, all_pinned,
  832. this_best_prio,
  833. &cfs_rq_iterator);
  834. if (rem_load_move <= 0)
  835. break;
  836. }
  837. return max_load_move - rem_load_move;
  838. }
  839. static int
  840. move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  841. struct sched_domain *sd, enum cpu_idle_type idle)
  842. {
  843. struct cfs_rq *busy_cfs_rq;
  844. struct rq_iterator cfs_rq_iterator;
  845. cfs_rq_iterator.start = load_balance_start_fair;
  846. cfs_rq_iterator.next = load_balance_next_fair;
  847. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  848. /*
  849. * pass busy_cfs_rq argument into
  850. * load_balance_[start|next]_fair iterators
  851. */
  852. cfs_rq_iterator.arg = busy_cfs_rq;
  853. if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  854. &cfs_rq_iterator))
  855. return 1;
  856. }
  857. return 0;
  858. }
  859. #endif
  860. /*
  861. * scheduler tick hitting a task of our scheduling class:
  862. */
  863. static void task_tick_fair(struct rq *rq, struct task_struct *curr)
  864. {
  865. struct cfs_rq *cfs_rq;
  866. struct sched_entity *se = &curr->se;
  867. for_each_sched_entity(se) {
  868. cfs_rq = cfs_rq_of(se);
  869. entity_tick(cfs_rq, se);
  870. }
  871. }
  872. #define swap(a, b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
  873. /*
  874. * Share the fairness runtime between parent and child, thus the
  875. * total amount of pressure for CPU stays equal - new tasks
  876. * get a chance to run but frequent forkers are not allowed to
  877. * monopolize the CPU. Note: the parent runqueue is locked,
  878. * the child is not running yet.
  879. */
  880. static void task_new_fair(struct rq *rq, struct task_struct *p)
  881. {
  882. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  883. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  884. int this_cpu = smp_processor_id();
  885. sched_info_queued(p);
  886. update_curr(cfs_rq);
  887. place_entity(cfs_rq, se, 1);
  888. /* 'curr' will be NULL if the child belongs to a different group */
  889. if (sysctl_sched_child_runs_first && this_cpu == task_cpu(p) &&
  890. curr && curr->vruntime < se->vruntime) {
  891. /*
  892. * Upon rescheduling, sched_class::put_prev_task() will place
  893. * 'current' within the tree based on its new key value.
  894. */
  895. swap(curr->vruntime, se->vruntime);
  896. }
  897. enqueue_task_fair(rq, p, 0);
  898. resched_task(rq->curr);
  899. }
  900. /* Account for a task changing its policy or group.
  901. *
  902. * This routine is mostly called to set cfs_rq->curr field when a task
  903. * migrates between groups/classes.
  904. */
  905. static void set_curr_task_fair(struct rq *rq)
  906. {
  907. struct sched_entity *se = &rq->curr->se;
  908. for_each_sched_entity(se)
  909. set_next_entity(cfs_rq_of(se), se);
  910. }
  911. /*
  912. * All the scheduling class methods:
  913. */
  914. static const struct sched_class fair_sched_class = {
  915. .next = &idle_sched_class,
  916. .enqueue_task = enqueue_task_fair,
  917. .dequeue_task = dequeue_task_fair,
  918. .yield_task = yield_task_fair,
  919. .check_preempt_curr = check_preempt_wakeup,
  920. .pick_next_task = pick_next_task_fair,
  921. .put_prev_task = put_prev_task_fair,
  922. #ifdef CONFIG_SMP
  923. .load_balance = load_balance_fair,
  924. .move_one_task = move_one_task_fair,
  925. #endif
  926. .set_curr_task = set_curr_task_fair,
  927. .task_tick = task_tick_fair,
  928. .task_new = task_new_fair,
  929. };
  930. #ifdef CONFIG_SCHED_DEBUG
  931. static void print_cfs_stats(struct seq_file *m, int cpu)
  932. {
  933. struct cfs_rq *cfs_rq;
  934. #ifdef CONFIG_FAIR_GROUP_SCHED
  935. print_cfs_rq(m, cpu, &cpu_rq(cpu)->cfs);
  936. #endif
  937. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  938. print_cfs_rq(m, cpu, cfs_rq);
  939. }
  940. #endif