sched_fair.c 26 KB

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