sched_fair.c 35 KB

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