sched_fair.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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. #include <linux/latencytop.h>
  23. /*
  24. * Targeted preemption latency for CPU-bound tasks:
  25. * (default: 20ms * (1 + ilog(ncpus)), units: nanoseconds)
  26. *
  27. * NOTE: this latency value is not the same as the concept of
  28. * 'timeslice length' - timeslices in CFS are of variable length
  29. * and have no persistent notion like in traditional, time-slice
  30. * based scheduling concepts.
  31. *
  32. * (to see the precise effective timeslice length of your workload,
  33. * run vmstat and monitor the context-switches (cs) field)
  34. */
  35. unsigned int sysctl_sched_latency = 20000000ULL;
  36. /*
  37. * Minimal preemption granularity for CPU-bound tasks:
  38. * (default: 4 msec * (1 + ilog(ncpus)), units: nanoseconds)
  39. */
  40. unsigned int sysctl_sched_min_granularity = 4000000ULL;
  41. /*
  42. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  43. */
  44. static unsigned int sched_nr_latency = 5;
  45. /*
  46. * After fork, child runs first. (default) If set to 0 then
  47. * parent will (try to) run first.
  48. */
  49. const_debug unsigned int sysctl_sched_child_runs_first = 1;
  50. /*
  51. * sys_sched_yield() compat mode
  52. *
  53. * This option switches the agressive yield implementation of the
  54. * old scheduler back on.
  55. */
  56. unsigned int __read_mostly sysctl_sched_compat_yield;
  57. /*
  58. * SCHED_OTHER wake-up granularity.
  59. * (default: 10 msec * (1 + ilog(ncpus)), units: nanoseconds)
  60. *
  61. * This option delays the preemption effects of decoupled workloads
  62. * and reduces their over-scheduling. Synchronous workloads will still
  63. * have immediate wakeup/sleep latencies.
  64. */
  65. unsigned int sysctl_sched_wakeup_granularity = 10000000UL;
  66. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  67. /**************************************************************
  68. * CFS operations on generic schedulable entities:
  69. */
  70. #ifdef CONFIG_FAIR_GROUP_SCHED
  71. /* cpu runqueue to which this cfs_rq is attached */
  72. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  73. {
  74. return cfs_rq->rq;
  75. }
  76. /* An entity is a task if it doesn't "own" a runqueue */
  77. #define entity_is_task(se) (!se->my_q)
  78. #else /* CONFIG_FAIR_GROUP_SCHED */
  79. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  80. {
  81. return container_of(cfs_rq, struct rq, cfs);
  82. }
  83. #define entity_is_task(se) 1
  84. #endif /* CONFIG_FAIR_GROUP_SCHED */
  85. static inline struct task_struct *task_of(struct sched_entity *se)
  86. {
  87. return container_of(se, struct task_struct, se);
  88. }
  89. /**************************************************************
  90. * Scheduling class tree data structure manipulation methods:
  91. */
  92. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  93. {
  94. s64 delta = (s64)(vruntime - min_vruntime);
  95. if (delta > 0)
  96. min_vruntime = vruntime;
  97. return min_vruntime;
  98. }
  99. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  100. {
  101. s64 delta = (s64)(vruntime - min_vruntime);
  102. if (delta < 0)
  103. min_vruntime = vruntime;
  104. return min_vruntime;
  105. }
  106. static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  107. {
  108. return se->vruntime - cfs_rq->min_vruntime;
  109. }
  110. /*
  111. * Enqueue an entity into the rb-tree:
  112. */
  113. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  114. {
  115. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  116. struct rb_node *parent = NULL;
  117. struct sched_entity *entry;
  118. s64 key = entity_key(cfs_rq, se);
  119. int leftmost = 1;
  120. /*
  121. * Find the right place in the rbtree:
  122. */
  123. while (*link) {
  124. parent = *link;
  125. entry = rb_entry(parent, struct sched_entity, run_node);
  126. /*
  127. * We dont care about collisions. Nodes with
  128. * the same key stay together.
  129. */
  130. if (key < entity_key(cfs_rq, entry)) {
  131. link = &parent->rb_left;
  132. } else {
  133. link = &parent->rb_right;
  134. leftmost = 0;
  135. }
  136. }
  137. /*
  138. * Maintain a cache of leftmost tree entries (it is frequently
  139. * used):
  140. */
  141. if (leftmost) {
  142. cfs_rq->rb_leftmost = &se->run_node;
  143. /*
  144. * maintain cfs_rq->min_vruntime to be a monotonic increasing
  145. * value tracking the leftmost vruntime in the tree.
  146. */
  147. cfs_rq->min_vruntime =
  148. max_vruntime(cfs_rq->min_vruntime, se->vruntime);
  149. }
  150. rb_link_node(&se->run_node, parent, link);
  151. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  152. }
  153. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  154. {
  155. if (cfs_rq->rb_leftmost == &se->run_node) {
  156. struct rb_node *next_node;
  157. struct sched_entity *next;
  158. next_node = rb_next(&se->run_node);
  159. cfs_rq->rb_leftmost = next_node;
  160. if (next_node) {
  161. next = rb_entry(next_node,
  162. struct sched_entity, run_node);
  163. cfs_rq->min_vruntime =
  164. max_vruntime(cfs_rq->min_vruntime,
  165. next->vruntime);
  166. }
  167. }
  168. if (cfs_rq->next == se)
  169. cfs_rq->next = NULL;
  170. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  171. }
  172. static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
  173. {
  174. return cfs_rq->rb_leftmost;
  175. }
  176. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  177. {
  178. return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
  179. }
  180. static inline struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  181. {
  182. struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
  183. if (!last)
  184. return NULL;
  185. return rb_entry(last, struct sched_entity, run_node);
  186. }
  187. /**************************************************************
  188. * Scheduling class statistics methods:
  189. */
  190. #ifdef CONFIG_SCHED_DEBUG
  191. int sched_nr_latency_handler(struct ctl_table *table, int write,
  192. struct file *filp, void __user *buffer, size_t *lenp,
  193. loff_t *ppos)
  194. {
  195. int ret = proc_dointvec_minmax(table, write, filp, buffer, lenp, ppos);
  196. if (ret || !write)
  197. return ret;
  198. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  199. sysctl_sched_min_granularity);
  200. return 0;
  201. }
  202. #endif
  203. /*
  204. * The idea is to set a period in which each task runs once.
  205. *
  206. * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
  207. * this period because otherwise the slices get too small.
  208. *
  209. * p = (nr <= nl) ? l : l*nr/nl
  210. */
  211. static u64 __sched_period(unsigned long nr_running)
  212. {
  213. u64 period = sysctl_sched_latency;
  214. unsigned long nr_latency = sched_nr_latency;
  215. if (unlikely(nr_running > nr_latency)) {
  216. period = sysctl_sched_min_granularity;
  217. period *= nr_running;
  218. }
  219. return period;
  220. }
  221. /*
  222. * We calculate the wall-time slice from the period by taking a part
  223. * proportional to the weight.
  224. *
  225. * s = p*w/rw
  226. */
  227. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  228. {
  229. return calc_delta_mine(__sched_period(cfs_rq->nr_running),
  230. se->load.weight, &cfs_rq->load);
  231. }
  232. /*
  233. * We calculate the vruntime slice.
  234. *
  235. * vs = s/w = p/rw
  236. */
  237. static u64 __sched_vslice(unsigned long rq_weight, unsigned long nr_running)
  238. {
  239. u64 vslice = __sched_period(nr_running);
  240. vslice *= NICE_0_LOAD;
  241. do_div(vslice, rq_weight);
  242. return vslice;
  243. }
  244. static u64 sched_vslice_add(struct cfs_rq *cfs_rq, struct sched_entity *se)
  245. {
  246. return __sched_vslice(cfs_rq->load.weight + se->load.weight,
  247. cfs_rq->nr_running + 1);
  248. }
  249. /*
  250. * Update the current task's runtime statistics. Skip current tasks that
  251. * are not in our scheduling class.
  252. */
  253. static inline void
  254. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  255. unsigned long delta_exec)
  256. {
  257. unsigned long delta_exec_weighted;
  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. static void update_curr(struct cfs_rq *cfs_rq)
  269. {
  270. struct sched_entity *curr = cfs_rq->curr;
  271. u64 now = rq_of(cfs_rq)->clock;
  272. unsigned long delta_exec;
  273. if (unlikely(!curr))
  274. return;
  275. /*
  276. * Get the amount of time the current task was running
  277. * since the last time we changed load (this cannot
  278. * overflow on 32 bits):
  279. */
  280. delta_exec = (unsigned long)(now - curr->exec_start);
  281. __update_curr(cfs_rq, curr, delta_exec);
  282. curr->exec_start = now;
  283. if (entity_is_task(curr)) {
  284. struct task_struct *curtask = task_of(curr);
  285. cpuacct_charge(curtask, delta_exec);
  286. }
  287. }
  288. static inline void
  289. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  290. {
  291. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  292. }
  293. /*
  294. * Task is being enqueued - update stats:
  295. */
  296. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  297. {
  298. /*
  299. * Are we enqueueing a waiting task? (for current tasks
  300. * a dequeue/enqueue event is a NOP)
  301. */
  302. if (se != cfs_rq->curr)
  303. update_stats_wait_start(cfs_rq, se);
  304. }
  305. static void
  306. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  307. {
  308. schedstat_set(se->wait_max, max(se->wait_max,
  309. rq_of(cfs_rq)->clock - se->wait_start));
  310. schedstat_set(se->wait_count, se->wait_count + 1);
  311. schedstat_set(se->wait_sum, se->wait_sum +
  312. rq_of(cfs_rq)->clock - se->wait_start);
  313. schedstat_set(se->wait_start, 0);
  314. }
  315. static inline void
  316. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  317. {
  318. /*
  319. * Mark the end of the wait period if dequeueing a
  320. * waiting task:
  321. */
  322. if (se != cfs_rq->curr)
  323. update_stats_wait_end(cfs_rq, se);
  324. }
  325. /*
  326. * We are picking a new current task - update its stats:
  327. */
  328. static inline void
  329. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  330. {
  331. /*
  332. * We are starting a new run period:
  333. */
  334. se->exec_start = rq_of(cfs_rq)->clock;
  335. }
  336. /**************************************************
  337. * Scheduling class queueing methods:
  338. */
  339. static void
  340. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  341. {
  342. update_load_add(&cfs_rq->load, se->load.weight);
  343. cfs_rq->nr_running++;
  344. se->on_rq = 1;
  345. }
  346. static void
  347. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  348. {
  349. update_load_sub(&cfs_rq->load, se->load.weight);
  350. cfs_rq->nr_running--;
  351. se->on_rq = 0;
  352. }
  353. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  354. {
  355. #ifdef CONFIG_SCHEDSTATS
  356. if (se->sleep_start) {
  357. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  358. struct task_struct *tsk = task_of(se);
  359. if ((s64)delta < 0)
  360. delta = 0;
  361. if (unlikely(delta > se->sleep_max))
  362. se->sleep_max = delta;
  363. se->sleep_start = 0;
  364. se->sum_sleep_runtime += delta;
  365. account_scheduler_latency(tsk, delta >> 10, 1);
  366. }
  367. if (se->block_start) {
  368. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  369. struct task_struct *tsk = task_of(se);
  370. if ((s64)delta < 0)
  371. delta = 0;
  372. if (unlikely(delta > se->block_max))
  373. se->block_max = delta;
  374. se->block_start = 0;
  375. se->sum_sleep_runtime += delta;
  376. /*
  377. * Blocking time is in units of nanosecs, so shift by 20 to
  378. * get a milliseconds-range estimation of the amount of
  379. * time that the task spent sleeping:
  380. */
  381. if (unlikely(prof_on == SLEEP_PROFILING)) {
  382. profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
  383. delta >> 20);
  384. }
  385. account_scheduler_latency(tsk, delta >> 10, 0);
  386. }
  387. #endif
  388. }
  389. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  390. {
  391. #ifdef CONFIG_SCHED_DEBUG
  392. s64 d = se->vruntime - cfs_rq->min_vruntime;
  393. if (d < 0)
  394. d = -d;
  395. if (d > 3*sysctl_sched_latency)
  396. schedstat_inc(cfs_rq, nr_spread_over);
  397. #endif
  398. }
  399. static void
  400. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  401. {
  402. u64 vruntime;
  403. if (first_fair(cfs_rq)) {
  404. vruntime = min_vruntime(cfs_rq->min_vruntime,
  405. __pick_next_entity(cfs_rq)->vruntime);
  406. } else
  407. vruntime = cfs_rq->min_vruntime;
  408. /*
  409. * The 'current' period is already promised to the current tasks,
  410. * however the extra weight of the new task will slow them down a
  411. * little, place the new task so that it fits in the slot that
  412. * stays open at the end.
  413. */
  414. if (initial && sched_feat(START_DEBIT))
  415. vruntime += sched_vslice_add(cfs_rq, se);
  416. if (!initial) {
  417. /* sleeps upto a single latency don't count. */
  418. if (sched_feat(NEW_FAIR_SLEEPERS)) {
  419. vruntime -= calc_delta_fair(sysctl_sched_latency,
  420. &cfs_rq->load);
  421. }
  422. /* ensure we never gain time by being placed backwards. */
  423. vruntime = max_vruntime(se->vruntime, vruntime);
  424. }
  425. se->vruntime = vruntime;
  426. }
  427. static void
  428. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  429. {
  430. /*
  431. * Update run-time statistics of the 'current'.
  432. */
  433. update_curr(cfs_rq);
  434. if (wakeup) {
  435. place_entity(cfs_rq, se, 0);
  436. enqueue_sleeper(cfs_rq, se);
  437. }
  438. update_stats_enqueue(cfs_rq, se);
  439. check_spread(cfs_rq, se);
  440. if (se != cfs_rq->curr)
  441. __enqueue_entity(cfs_rq, se);
  442. account_entity_enqueue(cfs_rq, se);
  443. }
  444. static void update_avg(u64 *avg, u64 sample)
  445. {
  446. s64 diff = sample - *avg;
  447. *avg += diff >> 3;
  448. }
  449. static void update_avg_stats(struct cfs_rq *cfs_rq, struct sched_entity *se)
  450. {
  451. if (!se->last_wakeup)
  452. return;
  453. update_avg(&se->avg_overlap, se->sum_exec_runtime - se->last_wakeup);
  454. se->last_wakeup = 0;
  455. }
  456. static void
  457. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  458. {
  459. /*
  460. * Update run-time statistics of the 'current'.
  461. */
  462. update_curr(cfs_rq);
  463. update_stats_dequeue(cfs_rq, se);
  464. if (sleep) {
  465. update_avg_stats(cfs_rq, se);
  466. #ifdef CONFIG_SCHEDSTATS
  467. if (entity_is_task(se)) {
  468. struct task_struct *tsk = task_of(se);
  469. if (tsk->state & TASK_INTERRUPTIBLE)
  470. se->sleep_start = rq_of(cfs_rq)->clock;
  471. if (tsk->state & TASK_UNINTERRUPTIBLE)
  472. se->block_start = rq_of(cfs_rq)->clock;
  473. }
  474. #endif
  475. }
  476. if (se != cfs_rq->curr)
  477. __dequeue_entity(cfs_rq, se);
  478. account_entity_dequeue(cfs_rq, se);
  479. }
  480. /*
  481. * Preempt the current task with a newly woken task if needed:
  482. */
  483. static void
  484. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  485. {
  486. unsigned long ideal_runtime, delta_exec;
  487. ideal_runtime = sched_slice(cfs_rq, curr);
  488. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  489. if (delta_exec > ideal_runtime)
  490. resched_task(rq_of(cfs_rq)->curr);
  491. }
  492. static void
  493. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  494. {
  495. /* 'current' is not kept within the tree. */
  496. if (se->on_rq) {
  497. /*
  498. * Any task has to be enqueued before it get to execute on
  499. * a CPU. So account for the time it spent waiting on the
  500. * runqueue.
  501. */
  502. update_stats_wait_end(cfs_rq, se);
  503. __dequeue_entity(cfs_rq, se);
  504. }
  505. update_stats_curr_start(cfs_rq, se);
  506. cfs_rq->curr = se;
  507. #ifdef CONFIG_SCHEDSTATS
  508. /*
  509. * Track our maximum slice length, if the CPU's load is at
  510. * least twice that of our own weight (i.e. dont track it
  511. * when there are only lesser-weight tasks around):
  512. */
  513. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  514. se->slice_max = max(se->slice_max,
  515. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  516. }
  517. #endif
  518. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  519. }
  520. static int
  521. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
  522. static struct sched_entity *
  523. pick_next(struct cfs_rq *cfs_rq, struct sched_entity *se)
  524. {
  525. if (!cfs_rq->next)
  526. return se;
  527. if (wakeup_preempt_entity(cfs_rq->next, se) != 0)
  528. return se;
  529. return cfs_rq->next;
  530. }
  531. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  532. {
  533. struct sched_entity *se = NULL;
  534. if (first_fair(cfs_rq)) {
  535. se = __pick_next_entity(cfs_rq);
  536. se = pick_next(cfs_rq, se);
  537. set_next_entity(cfs_rq, se);
  538. }
  539. return se;
  540. }
  541. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  542. {
  543. /*
  544. * If still on the runqueue then deactivate_task()
  545. * was not called and update_curr() has to be done:
  546. */
  547. if (prev->on_rq)
  548. update_curr(cfs_rq);
  549. check_spread(cfs_rq, prev);
  550. if (prev->on_rq) {
  551. update_stats_wait_start(cfs_rq, prev);
  552. /* Put 'current' back into the tree. */
  553. __enqueue_entity(cfs_rq, prev);
  554. }
  555. cfs_rq->curr = NULL;
  556. }
  557. static void
  558. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  559. {
  560. /*
  561. * Update run-time statistics of the 'current'.
  562. */
  563. update_curr(cfs_rq);
  564. #ifdef CONFIG_SCHED_HRTICK
  565. /*
  566. * queued ticks are scheduled to match the slice, so don't bother
  567. * validating it and just reschedule.
  568. */
  569. if (queued)
  570. return resched_task(rq_of(cfs_rq)->curr);
  571. /*
  572. * don't let the period tick interfere with the hrtick preemption
  573. */
  574. if (!sched_feat(DOUBLE_TICK) &&
  575. hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  576. return;
  577. #endif
  578. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  579. check_preempt_tick(cfs_rq, curr);
  580. }
  581. /**************************************************
  582. * CFS operations on tasks:
  583. */
  584. #ifdef CONFIG_FAIR_GROUP_SCHED
  585. /* Walk up scheduling entities hierarchy */
  586. #define for_each_sched_entity(se) \
  587. for (; se; se = se->parent)
  588. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  589. {
  590. return p->se.cfs_rq;
  591. }
  592. /* runqueue on which this entity is (to be) queued */
  593. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  594. {
  595. return se->cfs_rq;
  596. }
  597. /* runqueue "owned" by this group */
  598. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  599. {
  600. return grp->my_q;
  601. }
  602. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  603. * another cpu ('this_cpu')
  604. */
  605. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  606. {
  607. return cfs_rq->tg->cfs_rq[this_cpu];
  608. }
  609. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  610. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  611. list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  612. /* Do the two (enqueued) entities belong to the same group ? */
  613. static inline int
  614. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  615. {
  616. if (se->cfs_rq == pse->cfs_rq)
  617. return 1;
  618. return 0;
  619. }
  620. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  621. {
  622. return se->parent;
  623. }
  624. #else /* CONFIG_FAIR_GROUP_SCHED */
  625. #define for_each_sched_entity(se) \
  626. for (; se; se = NULL)
  627. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  628. {
  629. return &task_rq(p)->cfs;
  630. }
  631. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  632. {
  633. struct task_struct *p = task_of(se);
  634. struct rq *rq = task_rq(p);
  635. return &rq->cfs;
  636. }
  637. /* runqueue "owned" by this group */
  638. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  639. {
  640. return NULL;
  641. }
  642. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  643. {
  644. return &cpu_rq(this_cpu)->cfs;
  645. }
  646. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  647. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  648. static inline int
  649. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  650. {
  651. return 1;
  652. }
  653. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  654. {
  655. return NULL;
  656. }
  657. #endif /* CONFIG_FAIR_GROUP_SCHED */
  658. #ifdef CONFIG_SCHED_HRTICK
  659. static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  660. {
  661. int requeue = rq->curr == p;
  662. struct sched_entity *se = &p->se;
  663. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  664. WARN_ON(task_rq(p) != rq);
  665. if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
  666. u64 slice = sched_slice(cfs_rq, se);
  667. u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
  668. s64 delta = slice - ran;
  669. if (delta < 0) {
  670. if (rq->curr == p)
  671. resched_task(p);
  672. return;
  673. }
  674. /*
  675. * Don't schedule slices shorter than 10000ns, that just
  676. * doesn't make sense. Rely on vruntime for fairness.
  677. */
  678. if (!requeue)
  679. delta = max(10000LL, delta);
  680. hrtick_start(rq, delta, requeue);
  681. }
  682. }
  683. #else
  684. static inline void
  685. hrtick_start_fair(struct rq *rq, struct task_struct *p)
  686. {
  687. }
  688. #endif
  689. /*
  690. * The enqueue_task method is called before nr_running is
  691. * increased. Here we update the fair scheduling stats and
  692. * then put the task into the rbtree:
  693. */
  694. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  695. {
  696. struct cfs_rq *cfs_rq;
  697. struct sched_entity *se = &p->se;
  698. for_each_sched_entity(se) {
  699. if (se->on_rq)
  700. break;
  701. cfs_rq = cfs_rq_of(se);
  702. enqueue_entity(cfs_rq, se, wakeup);
  703. wakeup = 1;
  704. }
  705. hrtick_start_fair(rq, rq->curr);
  706. }
  707. /*
  708. * The dequeue_task method is called before nr_running is
  709. * decreased. We remove the task from the rbtree and
  710. * update the fair scheduling stats:
  711. */
  712. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  713. {
  714. struct cfs_rq *cfs_rq;
  715. struct sched_entity *se = &p->se;
  716. for_each_sched_entity(se) {
  717. cfs_rq = cfs_rq_of(se);
  718. dequeue_entity(cfs_rq, se, sleep);
  719. /* Don't dequeue parent if it has other entities besides us */
  720. if (cfs_rq->load.weight)
  721. break;
  722. sleep = 1;
  723. }
  724. hrtick_start_fair(rq, rq->curr);
  725. }
  726. /*
  727. * sched_yield() support is very simple - we dequeue and enqueue.
  728. *
  729. * If compat_yield is turned on then we requeue to the end of the tree.
  730. */
  731. static void yield_task_fair(struct rq *rq)
  732. {
  733. struct task_struct *curr = rq->curr;
  734. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  735. struct sched_entity *rightmost, *se = &curr->se;
  736. /*
  737. * Are we the only task in the tree?
  738. */
  739. if (unlikely(cfs_rq->nr_running == 1))
  740. return;
  741. if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
  742. __update_rq_clock(rq);
  743. /*
  744. * Update run-time statistics of the 'current'.
  745. */
  746. update_curr(cfs_rq);
  747. return;
  748. }
  749. /*
  750. * Find the rightmost entry in the rbtree:
  751. */
  752. rightmost = __pick_last_entity(cfs_rq);
  753. /*
  754. * Already in the rightmost position?
  755. */
  756. if (unlikely(!rightmost || rightmost->vruntime < se->vruntime))
  757. return;
  758. /*
  759. * Minimally necessary key value to be last in the tree:
  760. * Upon rescheduling, sched_class::put_prev_task() will place
  761. * 'current' within the tree based on its new key value.
  762. */
  763. se->vruntime = rightmost->vruntime + 1;
  764. }
  765. /*
  766. * wake_idle() will wake a task on an idle cpu if task->cpu is
  767. * not idle and an idle cpu is available. The span of cpus to
  768. * search starts with cpus closest then further out as needed,
  769. * so we always favor a closer, idle cpu.
  770. *
  771. * Returns the CPU we should wake onto.
  772. */
  773. #if defined(ARCH_HAS_SCHED_WAKE_IDLE)
  774. static int wake_idle(int cpu, struct task_struct *p)
  775. {
  776. cpumask_t tmp;
  777. struct sched_domain *sd;
  778. int i;
  779. /*
  780. * If it is idle, then it is the best cpu to run this task.
  781. *
  782. * This cpu is also the best, if it has more than one task already.
  783. * Siblings must be also busy(in most cases) as they didn't already
  784. * pickup the extra load from this cpu and hence we need not check
  785. * sibling runqueue info. This will avoid the checks and cache miss
  786. * penalities associated with that.
  787. */
  788. if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1)
  789. return cpu;
  790. for_each_domain(cpu, sd) {
  791. if (sd->flags & SD_WAKE_IDLE) {
  792. cpus_and(tmp, sd->span, p->cpus_allowed);
  793. for_each_cpu_mask(i, tmp) {
  794. if (idle_cpu(i)) {
  795. if (i != task_cpu(p)) {
  796. schedstat_inc(p,
  797. se.nr_wakeups_idle);
  798. }
  799. return i;
  800. }
  801. }
  802. } else {
  803. break;
  804. }
  805. }
  806. return cpu;
  807. }
  808. #else
  809. static inline int wake_idle(int cpu, struct task_struct *p)
  810. {
  811. return cpu;
  812. }
  813. #endif
  814. #ifdef CONFIG_SMP
  815. static const struct sched_class fair_sched_class;
  816. static int
  817. wake_affine(struct rq *rq, struct sched_domain *this_sd, struct rq *this_rq,
  818. struct task_struct *p, int prev_cpu, int this_cpu, int sync,
  819. int idx, unsigned long load, unsigned long this_load,
  820. unsigned int imbalance)
  821. {
  822. struct task_struct *curr = this_rq->curr;
  823. unsigned long tl = this_load;
  824. unsigned long tl_per_task;
  825. if (!(this_sd->flags & SD_WAKE_AFFINE))
  826. return 0;
  827. /*
  828. * If the currently running task will sleep within
  829. * a reasonable amount of time then attract this newly
  830. * woken task:
  831. */
  832. if (sync && curr->sched_class == &fair_sched_class) {
  833. if (curr->se.avg_overlap < sysctl_sched_migration_cost &&
  834. p->se.avg_overlap < sysctl_sched_migration_cost)
  835. return 1;
  836. }
  837. schedstat_inc(p, se.nr_wakeups_affine_attempts);
  838. tl_per_task = cpu_avg_load_per_task(this_cpu);
  839. /*
  840. * If sync wakeup then subtract the (maximum possible)
  841. * effect of the currently running task from the load
  842. * of the current CPU:
  843. */
  844. if (sync)
  845. tl -= current->se.load.weight;
  846. if ((tl <= load && tl + target_load(prev_cpu, idx) <= tl_per_task) ||
  847. 100*(tl + p->se.load.weight) <= imbalance*load) {
  848. /*
  849. * This domain has SD_WAKE_AFFINE and
  850. * p is cache cold in this domain, and
  851. * there is no bad imbalance.
  852. */
  853. schedstat_inc(this_sd, ttwu_move_affine);
  854. schedstat_inc(p, se.nr_wakeups_affine);
  855. return 1;
  856. }
  857. return 0;
  858. }
  859. static int select_task_rq_fair(struct task_struct *p, int sync)
  860. {
  861. struct sched_domain *sd, *this_sd = NULL;
  862. int prev_cpu, this_cpu, new_cpu;
  863. unsigned long load, this_load;
  864. struct rq *rq, *this_rq;
  865. unsigned int imbalance;
  866. int idx;
  867. prev_cpu = task_cpu(p);
  868. rq = task_rq(p);
  869. this_cpu = smp_processor_id();
  870. this_rq = cpu_rq(this_cpu);
  871. new_cpu = prev_cpu;
  872. /*
  873. * 'this_sd' is the first domain that both
  874. * this_cpu and prev_cpu are present in:
  875. */
  876. for_each_domain(this_cpu, sd) {
  877. if (cpu_isset(prev_cpu, sd->span)) {
  878. this_sd = sd;
  879. break;
  880. }
  881. }
  882. if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
  883. goto out;
  884. /*
  885. * Check for affine wakeup and passive balancing possibilities.
  886. */
  887. if (!this_sd)
  888. goto out;
  889. idx = this_sd->wake_idx;
  890. imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;
  891. load = source_load(prev_cpu, idx);
  892. this_load = target_load(this_cpu, idx);
  893. if (wake_affine(rq, this_sd, this_rq, p, prev_cpu, this_cpu, sync, idx,
  894. load, this_load, imbalance))
  895. return this_cpu;
  896. if (prev_cpu == this_cpu)
  897. goto out;
  898. /*
  899. * Start passive balancing when half the imbalance_pct
  900. * limit is reached.
  901. */
  902. if (this_sd->flags & SD_WAKE_BALANCE) {
  903. if (imbalance*this_load <= 100*load) {
  904. schedstat_inc(this_sd, ttwu_move_balance);
  905. schedstat_inc(p, se.nr_wakeups_passive);
  906. return this_cpu;
  907. }
  908. }
  909. out:
  910. return wake_idle(new_cpu, p);
  911. }
  912. #endif /* CONFIG_SMP */
  913. static unsigned long wakeup_gran(struct sched_entity *se)
  914. {
  915. unsigned long gran = sysctl_sched_wakeup_granularity;
  916. /*
  917. * More easily preempt - nice tasks, while not making
  918. * it harder for + nice tasks.
  919. */
  920. if (unlikely(se->load.weight > NICE_0_LOAD))
  921. gran = calc_delta_fair(gran, &se->load);
  922. return gran;
  923. }
  924. /*
  925. * Should 'se' preempt 'curr'.
  926. *
  927. * |s1
  928. * |s2
  929. * |s3
  930. * g
  931. * |<--->|c
  932. *
  933. * w(c, s1) = -1
  934. * w(c, s2) = 0
  935. * w(c, s3) = 1
  936. *
  937. */
  938. static int
  939. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
  940. {
  941. s64 gran, vdiff = curr->vruntime - se->vruntime;
  942. if (vdiff < 0)
  943. return -1;
  944. gran = wakeup_gran(curr);
  945. if (vdiff > gran)
  946. return 1;
  947. return 0;
  948. }
  949. /*
  950. * Preempt the current task with a newly woken task if needed:
  951. */
  952. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
  953. {
  954. struct task_struct *curr = rq->curr;
  955. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  956. struct sched_entity *se = &curr->se, *pse = &p->se;
  957. if (unlikely(rt_prio(p->prio))) {
  958. update_rq_clock(rq);
  959. update_curr(cfs_rq);
  960. resched_task(curr);
  961. return;
  962. }
  963. se->last_wakeup = se->sum_exec_runtime;
  964. if (unlikely(se == pse))
  965. return;
  966. cfs_rq_of(pse)->next = pse;
  967. /*
  968. * Batch tasks do not preempt (their preemption is driven by
  969. * the tick):
  970. */
  971. if (unlikely(p->policy == SCHED_BATCH))
  972. return;
  973. if (!sched_feat(WAKEUP_PREEMPT))
  974. return;
  975. while (!is_same_group(se, pse)) {
  976. se = parent_entity(se);
  977. pse = parent_entity(pse);
  978. }
  979. if (wakeup_preempt_entity(se, pse) == 1)
  980. resched_task(curr);
  981. }
  982. static struct task_struct *pick_next_task_fair(struct rq *rq)
  983. {
  984. struct task_struct *p;
  985. struct cfs_rq *cfs_rq = &rq->cfs;
  986. struct sched_entity *se;
  987. if (unlikely(!cfs_rq->nr_running))
  988. return NULL;
  989. do {
  990. se = pick_next_entity(cfs_rq);
  991. cfs_rq = group_cfs_rq(se);
  992. } while (cfs_rq);
  993. p = task_of(se);
  994. hrtick_start_fair(rq, p);
  995. return p;
  996. }
  997. /*
  998. * Account for a descheduled task:
  999. */
  1000. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  1001. {
  1002. struct sched_entity *se = &prev->se;
  1003. struct cfs_rq *cfs_rq;
  1004. for_each_sched_entity(se) {
  1005. cfs_rq = cfs_rq_of(se);
  1006. put_prev_entity(cfs_rq, se);
  1007. }
  1008. }
  1009. #ifdef CONFIG_SMP
  1010. /**************************************************
  1011. * Fair scheduling class load-balancing methods:
  1012. */
  1013. /*
  1014. * Load-balancing iterator. Note: while the runqueue stays locked
  1015. * during the whole iteration, the current task might be
  1016. * dequeued so the iterator has to be dequeue-safe. Here we
  1017. * achieve that by always pre-iterating before returning
  1018. * the current task:
  1019. */
  1020. static struct task_struct *
  1021. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  1022. {
  1023. struct task_struct *p;
  1024. if (!curr)
  1025. return NULL;
  1026. p = rb_entry(curr, struct task_struct, se.run_node);
  1027. cfs_rq->rb_load_balance_curr = rb_next(curr);
  1028. return p;
  1029. }
  1030. static struct task_struct *load_balance_start_fair(void *arg)
  1031. {
  1032. struct cfs_rq *cfs_rq = arg;
  1033. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  1034. }
  1035. static struct task_struct *load_balance_next_fair(void *arg)
  1036. {
  1037. struct cfs_rq *cfs_rq = arg;
  1038. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  1039. }
  1040. #ifdef CONFIG_FAIR_GROUP_SCHED
  1041. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  1042. {
  1043. struct sched_entity *curr;
  1044. struct task_struct *p;
  1045. if (!cfs_rq->nr_running || !first_fair(cfs_rq))
  1046. return MAX_PRIO;
  1047. curr = cfs_rq->curr;
  1048. if (!curr)
  1049. curr = __pick_next_entity(cfs_rq);
  1050. p = task_of(curr);
  1051. return p->prio;
  1052. }
  1053. #endif
  1054. static unsigned long
  1055. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1056. unsigned long max_load_move,
  1057. struct sched_domain *sd, enum cpu_idle_type idle,
  1058. int *all_pinned, int *this_best_prio)
  1059. {
  1060. struct cfs_rq *busy_cfs_rq;
  1061. long rem_load_move = max_load_move;
  1062. struct rq_iterator cfs_rq_iterator;
  1063. cfs_rq_iterator.start = load_balance_start_fair;
  1064. cfs_rq_iterator.next = load_balance_next_fair;
  1065. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  1066. #ifdef CONFIG_FAIR_GROUP_SCHED
  1067. struct cfs_rq *this_cfs_rq;
  1068. long imbalance;
  1069. unsigned long maxload;
  1070. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  1071. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  1072. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  1073. if (imbalance <= 0)
  1074. continue;
  1075. /* Don't pull more than imbalance/2 */
  1076. imbalance /= 2;
  1077. maxload = min(rem_load_move, imbalance);
  1078. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  1079. #else
  1080. # define maxload rem_load_move
  1081. #endif
  1082. /*
  1083. * pass busy_cfs_rq argument into
  1084. * load_balance_[start|next]_fair iterators
  1085. */
  1086. cfs_rq_iterator.arg = busy_cfs_rq;
  1087. rem_load_move -= balance_tasks(this_rq, this_cpu, busiest,
  1088. maxload, sd, idle, all_pinned,
  1089. this_best_prio,
  1090. &cfs_rq_iterator);
  1091. if (rem_load_move <= 0)
  1092. break;
  1093. }
  1094. return max_load_move - rem_load_move;
  1095. }
  1096. static int
  1097. move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1098. struct sched_domain *sd, enum cpu_idle_type idle)
  1099. {
  1100. struct cfs_rq *busy_cfs_rq;
  1101. struct rq_iterator cfs_rq_iterator;
  1102. cfs_rq_iterator.start = load_balance_start_fair;
  1103. cfs_rq_iterator.next = load_balance_next_fair;
  1104. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  1105. /*
  1106. * pass busy_cfs_rq argument into
  1107. * load_balance_[start|next]_fair iterators
  1108. */
  1109. cfs_rq_iterator.arg = busy_cfs_rq;
  1110. if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  1111. &cfs_rq_iterator))
  1112. return 1;
  1113. }
  1114. return 0;
  1115. }
  1116. #endif
  1117. /*
  1118. * scheduler tick hitting a task of our scheduling class:
  1119. */
  1120. static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  1121. {
  1122. struct cfs_rq *cfs_rq;
  1123. struct sched_entity *se = &curr->se;
  1124. for_each_sched_entity(se) {
  1125. cfs_rq = cfs_rq_of(se);
  1126. entity_tick(cfs_rq, se, queued);
  1127. }
  1128. }
  1129. #define swap(a, b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
  1130. /*
  1131. * Share the fairness runtime between parent and child, thus the
  1132. * total amount of pressure for CPU stays equal - new tasks
  1133. * get a chance to run but frequent forkers are not allowed to
  1134. * monopolize the CPU. Note: the parent runqueue is locked,
  1135. * the child is not running yet.
  1136. */
  1137. static void task_new_fair(struct rq *rq, struct task_struct *p)
  1138. {
  1139. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  1140. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  1141. int this_cpu = smp_processor_id();
  1142. sched_info_queued(p);
  1143. update_curr(cfs_rq);
  1144. place_entity(cfs_rq, se, 1);
  1145. /* 'curr' will be NULL if the child belongs to a different group */
  1146. if (sysctl_sched_child_runs_first && this_cpu == task_cpu(p) &&
  1147. curr && curr->vruntime < se->vruntime) {
  1148. /*
  1149. * Upon rescheduling, sched_class::put_prev_task() will place
  1150. * 'current' within the tree based on its new key value.
  1151. */
  1152. swap(curr->vruntime, se->vruntime);
  1153. }
  1154. enqueue_task_fair(rq, p, 0);
  1155. resched_task(rq->curr);
  1156. }
  1157. /*
  1158. * Priority of the task has changed. Check to see if we preempt
  1159. * the current task.
  1160. */
  1161. static void prio_changed_fair(struct rq *rq, struct task_struct *p,
  1162. int oldprio, int running)
  1163. {
  1164. /*
  1165. * Reschedule if we are currently running on this runqueue and
  1166. * our priority decreased, or if we are not currently running on
  1167. * this runqueue and our priority is higher than the current's
  1168. */
  1169. if (running) {
  1170. if (p->prio > oldprio)
  1171. resched_task(rq->curr);
  1172. } else
  1173. check_preempt_curr(rq, p);
  1174. }
  1175. /*
  1176. * We switched to the sched_fair class.
  1177. */
  1178. static void switched_to_fair(struct rq *rq, struct task_struct *p,
  1179. int running)
  1180. {
  1181. /*
  1182. * We were most likely switched from sched_rt, so
  1183. * kick off the schedule if running, otherwise just see
  1184. * if we can still preempt the current task.
  1185. */
  1186. if (running)
  1187. resched_task(rq->curr);
  1188. else
  1189. check_preempt_curr(rq, p);
  1190. }
  1191. /* Account for a task changing its policy or group.
  1192. *
  1193. * This routine is mostly called to set cfs_rq->curr field when a task
  1194. * migrates between groups/classes.
  1195. */
  1196. static void set_curr_task_fair(struct rq *rq)
  1197. {
  1198. struct sched_entity *se = &rq->curr->se;
  1199. for_each_sched_entity(se)
  1200. set_next_entity(cfs_rq_of(se), se);
  1201. }
  1202. #ifdef CONFIG_FAIR_GROUP_SCHED
  1203. static void moved_group_fair(struct task_struct *p)
  1204. {
  1205. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  1206. update_curr(cfs_rq);
  1207. place_entity(cfs_rq, &p->se, 1);
  1208. }
  1209. #endif
  1210. /*
  1211. * All the scheduling class methods:
  1212. */
  1213. static const struct sched_class fair_sched_class = {
  1214. .next = &idle_sched_class,
  1215. .enqueue_task = enqueue_task_fair,
  1216. .dequeue_task = dequeue_task_fair,
  1217. .yield_task = yield_task_fair,
  1218. #ifdef CONFIG_SMP
  1219. .select_task_rq = select_task_rq_fair,
  1220. #endif /* CONFIG_SMP */
  1221. .check_preempt_curr = check_preempt_wakeup,
  1222. .pick_next_task = pick_next_task_fair,
  1223. .put_prev_task = put_prev_task_fair,
  1224. #ifdef CONFIG_SMP
  1225. .load_balance = load_balance_fair,
  1226. .move_one_task = move_one_task_fair,
  1227. #endif
  1228. .set_curr_task = set_curr_task_fair,
  1229. .task_tick = task_tick_fair,
  1230. .task_new = task_new_fair,
  1231. .prio_changed = prio_changed_fair,
  1232. .switched_to = switched_to_fair,
  1233. #ifdef CONFIG_FAIR_GROUP_SCHED
  1234. .moved_group = moved_group_fair,
  1235. #endif
  1236. };
  1237. #ifdef CONFIG_SCHED_DEBUG
  1238. static void print_cfs_stats(struct seq_file *m, int cpu)
  1239. {
  1240. struct cfs_rq *cfs_rq;
  1241. #ifdef CONFIG_FAIR_GROUP_SCHED
  1242. print_cfs_rq(m, cpu, &cpu_rq(cpu)->cfs);
  1243. #endif
  1244. rcu_read_lock();
  1245. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  1246. print_cfs_rq(m, cpu, cfs_rq);
  1247. rcu_read_unlock();
  1248. }
  1249. #endif