sched_fair.c 35 KB

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