sched_fair.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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: 5ms * (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 = 5000000ULL;
  36. unsigned int normalized_sysctl_sched_latency = 5000000ULL;
  37. /*
  38. * Minimal preemption granularity for CPU-bound tasks:
  39. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  40. */
  41. unsigned int sysctl_sched_min_granularity = 1000000ULL;
  42. unsigned int normalized_sysctl_sched_min_granularity = 1000000ULL;
  43. /*
  44. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  45. */
  46. static unsigned int sched_nr_latency = 5;
  47. /*
  48. * After fork, child runs first. If set to 0 (default) then
  49. * parent will (try to) run first.
  50. */
  51. unsigned int sysctl_sched_child_runs_first __read_mostly;
  52. /*
  53. * sys_sched_yield() compat mode
  54. *
  55. * This option switches the agressive yield implementation of the
  56. * old scheduler back on.
  57. */
  58. unsigned int __read_mostly sysctl_sched_compat_yield;
  59. /*
  60. * SCHED_OTHER wake-up granularity.
  61. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  62. *
  63. * This option delays the preemption effects of decoupled workloads
  64. * and reduces their over-scheduling. Synchronous workloads will still
  65. * have immediate wakeup/sleep latencies.
  66. */
  67. unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
  68. unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
  69. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  70. static const struct sched_class fair_sched_class;
  71. /**************************************************************
  72. * CFS operations on generic schedulable entities:
  73. */
  74. #ifdef CONFIG_FAIR_GROUP_SCHED
  75. /* cpu runqueue to which this cfs_rq is attached */
  76. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  77. {
  78. return cfs_rq->rq;
  79. }
  80. /* An entity is a task if it doesn't "own" a runqueue */
  81. #define entity_is_task(se) (!se->my_q)
  82. static inline struct task_struct *task_of(struct sched_entity *se)
  83. {
  84. #ifdef CONFIG_SCHED_DEBUG
  85. WARN_ON_ONCE(!entity_is_task(se));
  86. #endif
  87. return container_of(se, struct task_struct, se);
  88. }
  89. /* Walk up scheduling entities hierarchy */
  90. #define for_each_sched_entity(se) \
  91. for (; se; se = se->parent)
  92. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  93. {
  94. return p->se.cfs_rq;
  95. }
  96. /* runqueue on which this entity is (to be) queued */
  97. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  98. {
  99. return se->cfs_rq;
  100. }
  101. /* runqueue "owned" by this group */
  102. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  103. {
  104. return grp->my_q;
  105. }
  106. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  107. * another cpu ('this_cpu')
  108. */
  109. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  110. {
  111. return cfs_rq->tg->cfs_rq[this_cpu];
  112. }
  113. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  114. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  115. list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  116. /* Do the two (enqueued) entities belong to the same group ? */
  117. static inline int
  118. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  119. {
  120. if (se->cfs_rq == pse->cfs_rq)
  121. return 1;
  122. return 0;
  123. }
  124. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  125. {
  126. return se->parent;
  127. }
  128. /* return depth at which a sched entity is present in the hierarchy */
  129. static inline int depth_se(struct sched_entity *se)
  130. {
  131. int depth = 0;
  132. for_each_sched_entity(se)
  133. depth++;
  134. return depth;
  135. }
  136. static void
  137. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  138. {
  139. int se_depth, pse_depth;
  140. /*
  141. * preemption test can be made between sibling entities who are in the
  142. * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
  143. * both tasks until we find their ancestors who are siblings of common
  144. * parent.
  145. */
  146. /* First walk up until both entities are at same depth */
  147. se_depth = depth_se(*se);
  148. pse_depth = depth_se(*pse);
  149. while (se_depth > pse_depth) {
  150. se_depth--;
  151. *se = parent_entity(*se);
  152. }
  153. while (pse_depth > se_depth) {
  154. pse_depth--;
  155. *pse = parent_entity(*pse);
  156. }
  157. while (!is_same_group(*se, *pse)) {
  158. *se = parent_entity(*se);
  159. *pse = parent_entity(*pse);
  160. }
  161. }
  162. #else /* !CONFIG_FAIR_GROUP_SCHED */
  163. static inline struct task_struct *task_of(struct sched_entity *se)
  164. {
  165. return container_of(se, struct task_struct, se);
  166. }
  167. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  168. {
  169. return container_of(cfs_rq, struct rq, cfs);
  170. }
  171. #define entity_is_task(se) 1
  172. #define for_each_sched_entity(se) \
  173. for (; se; se = NULL)
  174. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  175. {
  176. return &task_rq(p)->cfs;
  177. }
  178. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  179. {
  180. struct task_struct *p = task_of(se);
  181. struct rq *rq = task_rq(p);
  182. return &rq->cfs;
  183. }
  184. /* runqueue "owned" by this group */
  185. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  186. {
  187. return NULL;
  188. }
  189. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  190. {
  191. return &cpu_rq(this_cpu)->cfs;
  192. }
  193. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  194. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  195. static inline int
  196. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  197. {
  198. return 1;
  199. }
  200. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  201. {
  202. return NULL;
  203. }
  204. static inline void
  205. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  206. {
  207. }
  208. #endif /* CONFIG_FAIR_GROUP_SCHED */
  209. /**************************************************************
  210. * Scheduling class tree data structure manipulation methods:
  211. */
  212. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  213. {
  214. s64 delta = (s64)(vruntime - min_vruntime);
  215. if (delta > 0)
  216. min_vruntime = vruntime;
  217. return min_vruntime;
  218. }
  219. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  220. {
  221. s64 delta = (s64)(vruntime - min_vruntime);
  222. if (delta < 0)
  223. min_vruntime = vruntime;
  224. return min_vruntime;
  225. }
  226. static inline int entity_before(struct sched_entity *a,
  227. struct sched_entity *b)
  228. {
  229. return (s64)(a->vruntime - b->vruntime) < 0;
  230. }
  231. static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  232. {
  233. return se->vruntime - cfs_rq->min_vruntime;
  234. }
  235. static void update_min_vruntime(struct cfs_rq *cfs_rq)
  236. {
  237. u64 vruntime = cfs_rq->min_vruntime;
  238. if (cfs_rq->curr)
  239. vruntime = cfs_rq->curr->vruntime;
  240. if (cfs_rq->rb_leftmost) {
  241. struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
  242. struct sched_entity,
  243. run_node);
  244. if (!cfs_rq->curr)
  245. vruntime = se->vruntime;
  246. else
  247. vruntime = min_vruntime(vruntime, se->vruntime);
  248. }
  249. cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
  250. }
  251. /*
  252. * Enqueue an entity into the rb-tree:
  253. */
  254. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  255. {
  256. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  257. struct rb_node *parent = NULL;
  258. struct sched_entity *entry;
  259. s64 key = entity_key(cfs_rq, se);
  260. int leftmost = 1;
  261. /*
  262. * Find the right place in the rbtree:
  263. */
  264. while (*link) {
  265. parent = *link;
  266. entry = rb_entry(parent, struct sched_entity, run_node);
  267. /*
  268. * We dont care about collisions. Nodes with
  269. * the same key stay together.
  270. */
  271. if (key < entity_key(cfs_rq, entry)) {
  272. link = &parent->rb_left;
  273. } else {
  274. link = &parent->rb_right;
  275. leftmost = 0;
  276. }
  277. }
  278. /*
  279. * Maintain a cache of leftmost tree entries (it is frequently
  280. * used):
  281. */
  282. if (leftmost)
  283. cfs_rq->rb_leftmost = &se->run_node;
  284. rb_link_node(&se->run_node, parent, link);
  285. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  286. }
  287. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  288. {
  289. if (cfs_rq->rb_leftmost == &se->run_node) {
  290. struct rb_node *next_node;
  291. next_node = rb_next(&se->run_node);
  292. cfs_rq->rb_leftmost = next_node;
  293. }
  294. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  295. }
  296. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  297. {
  298. struct rb_node *left = cfs_rq->rb_leftmost;
  299. if (!left)
  300. return NULL;
  301. return rb_entry(left, struct sched_entity, run_node);
  302. }
  303. static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  304. {
  305. struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
  306. if (!last)
  307. return NULL;
  308. return rb_entry(last, struct sched_entity, run_node);
  309. }
  310. /**************************************************************
  311. * Scheduling class statistics methods:
  312. */
  313. #ifdef CONFIG_SCHED_DEBUG
  314. int sched_nr_latency_handler(struct ctl_table *table, int write,
  315. void __user *buffer, size_t *lenp,
  316. loff_t *ppos)
  317. {
  318. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  319. if (ret || !write)
  320. return ret;
  321. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  322. sysctl_sched_min_granularity);
  323. return 0;
  324. }
  325. #endif
  326. /*
  327. * delta /= w
  328. */
  329. static inline unsigned long
  330. calc_delta_fair(unsigned long delta, struct sched_entity *se)
  331. {
  332. if (unlikely(se->load.weight != NICE_0_LOAD))
  333. delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
  334. return delta;
  335. }
  336. /*
  337. * The idea is to set a period in which each task runs once.
  338. *
  339. * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
  340. * this period because otherwise the slices get too small.
  341. *
  342. * p = (nr <= nl) ? l : l*nr/nl
  343. */
  344. static u64 __sched_period(unsigned long nr_running)
  345. {
  346. u64 period = sysctl_sched_latency;
  347. unsigned long nr_latency = sched_nr_latency;
  348. if (unlikely(nr_running > nr_latency)) {
  349. period = sysctl_sched_min_granularity;
  350. period *= nr_running;
  351. }
  352. return period;
  353. }
  354. /*
  355. * We calculate the wall-time slice from the period by taking a part
  356. * proportional to the weight.
  357. *
  358. * s = p*P[w/rw]
  359. */
  360. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  361. {
  362. u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
  363. for_each_sched_entity(se) {
  364. struct load_weight *load;
  365. struct load_weight lw;
  366. cfs_rq = cfs_rq_of(se);
  367. load = &cfs_rq->load;
  368. if (unlikely(!se->on_rq)) {
  369. lw = cfs_rq->load;
  370. update_load_add(&lw, se->load.weight);
  371. load = &lw;
  372. }
  373. slice = calc_delta_mine(slice, se->load.weight, load);
  374. }
  375. return slice;
  376. }
  377. /*
  378. * We calculate the vruntime slice of a to be inserted task
  379. *
  380. * vs = s/w
  381. */
  382. static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  383. {
  384. return calc_delta_fair(sched_slice(cfs_rq, se), se);
  385. }
  386. /*
  387. * Update the current task's runtime statistics. Skip current tasks that
  388. * are not in our scheduling class.
  389. */
  390. static inline void
  391. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  392. unsigned long delta_exec)
  393. {
  394. unsigned long delta_exec_weighted;
  395. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  396. curr->sum_exec_runtime += delta_exec;
  397. schedstat_add(cfs_rq, exec_clock, delta_exec);
  398. delta_exec_weighted = calc_delta_fair(delta_exec, curr);
  399. curr->vruntime += delta_exec_weighted;
  400. update_min_vruntime(cfs_rq);
  401. }
  402. static void update_curr(struct cfs_rq *cfs_rq)
  403. {
  404. struct sched_entity *curr = cfs_rq->curr;
  405. u64 now = rq_of(cfs_rq)->clock;
  406. unsigned long delta_exec;
  407. if (unlikely(!curr))
  408. return;
  409. /*
  410. * Get the amount of time the current task was running
  411. * since the last time we changed load (this cannot
  412. * overflow on 32 bits):
  413. */
  414. delta_exec = (unsigned long)(now - curr->exec_start);
  415. if (!delta_exec)
  416. return;
  417. __update_curr(cfs_rq, curr, delta_exec);
  418. curr->exec_start = now;
  419. if (entity_is_task(curr)) {
  420. struct task_struct *curtask = task_of(curr);
  421. trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
  422. cpuacct_charge(curtask, delta_exec);
  423. account_group_exec_runtime(curtask, delta_exec);
  424. }
  425. }
  426. static inline void
  427. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  428. {
  429. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  430. }
  431. /*
  432. * Task is being enqueued - update stats:
  433. */
  434. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  435. {
  436. /*
  437. * Are we enqueueing a waiting task? (for current tasks
  438. * a dequeue/enqueue event is a NOP)
  439. */
  440. if (se != cfs_rq->curr)
  441. update_stats_wait_start(cfs_rq, se);
  442. }
  443. static void
  444. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  445. {
  446. schedstat_set(se->wait_max, max(se->wait_max,
  447. rq_of(cfs_rq)->clock - se->wait_start));
  448. schedstat_set(se->wait_count, se->wait_count + 1);
  449. schedstat_set(se->wait_sum, se->wait_sum +
  450. rq_of(cfs_rq)->clock - se->wait_start);
  451. #ifdef CONFIG_SCHEDSTATS
  452. if (entity_is_task(se)) {
  453. trace_sched_stat_wait(task_of(se),
  454. rq_of(cfs_rq)->clock - se->wait_start);
  455. }
  456. #endif
  457. schedstat_set(se->wait_start, 0);
  458. }
  459. static inline void
  460. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  461. {
  462. /*
  463. * Mark the end of the wait period if dequeueing a
  464. * waiting task:
  465. */
  466. if (se != cfs_rq->curr)
  467. update_stats_wait_end(cfs_rq, se);
  468. }
  469. /*
  470. * We are picking a new current task - update its stats:
  471. */
  472. static inline void
  473. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  474. {
  475. /*
  476. * We are starting a new run period:
  477. */
  478. se->exec_start = rq_of(cfs_rq)->clock;
  479. }
  480. /**************************************************
  481. * Scheduling class queueing methods:
  482. */
  483. #if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
  484. static void
  485. add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
  486. {
  487. cfs_rq->task_weight += weight;
  488. }
  489. #else
  490. static inline void
  491. add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
  492. {
  493. }
  494. #endif
  495. static void
  496. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  497. {
  498. update_load_add(&cfs_rq->load, se->load.weight);
  499. if (!parent_entity(se))
  500. inc_cpu_load(rq_of(cfs_rq), se->load.weight);
  501. if (entity_is_task(se)) {
  502. add_cfs_task_weight(cfs_rq, se->load.weight);
  503. list_add(&se->group_node, &cfs_rq->tasks);
  504. }
  505. cfs_rq->nr_running++;
  506. se->on_rq = 1;
  507. }
  508. static void
  509. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  510. {
  511. update_load_sub(&cfs_rq->load, se->load.weight);
  512. if (!parent_entity(se))
  513. dec_cpu_load(rq_of(cfs_rq), se->load.weight);
  514. if (entity_is_task(se)) {
  515. add_cfs_task_weight(cfs_rq, -se->load.weight);
  516. list_del_init(&se->group_node);
  517. }
  518. cfs_rq->nr_running--;
  519. se->on_rq = 0;
  520. }
  521. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  522. {
  523. #ifdef CONFIG_SCHEDSTATS
  524. struct task_struct *tsk = NULL;
  525. if (entity_is_task(se))
  526. tsk = task_of(se);
  527. if (se->sleep_start) {
  528. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  529. if ((s64)delta < 0)
  530. delta = 0;
  531. if (unlikely(delta > se->sleep_max))
  532. se->sleep_max = delta;
  533. se->sleep_start = 0;
  534. se->sum_sleep_runtime += delta;
  535. if (tsk) {
  536. account_scheduler_latency(tsk, delta >> 10, 1);
  537. trace_sched_stat_sleep(tsk, delta);
  538. }
  539. }
  540. if (se->block_start) {
  541. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  542. if ((s64)delta < 0)
  543. delta = 0;
  544. if (unlikely(delta > se->block_max))
  545. se->block_max = delta;
  546. se->block_start = 0;
  547. se->sum_sleep_runtime += delta;
  548. if (tsk) {
  549. if (tsk->in_iowait) {
  550. se->iowait_sum += delta;
  551. se->iowait_count++;
  552. trace_sched_stat_iowait(tsk, delta);
  553. }
  554. /*
  555. * Blocking time is in units of nanosecs, so shift by
  556. * 20 to get a milliseconds-range estimation of the
  557. * amount of time that the task spent sleeping:
  558. */
  559. if (unlikely(prof_on == SLEEP_PROFILING)) {
  560. profile_hits(SLEEP_PROFILING,
  561. (void *)get_wchan(tsk),
  562. delta >> 20);
  563. }
  564. account_scheduler_latency(tsk, delta >> 10, 0);
  565. }
  566. }
  567. #endif
  568. }
  569. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  570. {
  571. #ifdef CONFIG_SCHED_DEBUG
  572. s64 d = se->vruntime - cfs_rq->min_vruntime;
  573. if (d < 0)
  574. d = -d;
  575. if (d > 3*sysctl_sched_latency)
  576. schedstat_inc(cfs_rq, nr_spread_over);
  577. #endif
  578. }
  579. static void
  580. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  581. {
  582. u64 vruntime = cfs_rq->min_vruntime;
  583. /*
  584. * The 'current' period is already promised to the current tasks,
  585. * however the extra weight of the new task will slow them down a
  586. * little, place the new task so that it fits in the slot that
  587. * stays open at the end.
  588. */
  589. if (initial && sched_feat(START_DEBIT))
  590. vruntime += sched_vslice(cfs_rq, se);
  591. /* sleeps up to a single latency don't count. */
  592. if (!initial && sched_feat(FAIR_SLEEPERS)) {
  593. unsigned long thresh = sysctl_sched_latency;
  594. /*
  595. * Convert the sleeper threshold into virtual time.
  596. * SCHED_IDLE is a special sub-class. We care about
  597. * fairness only relative to other SCHED_IDLE tasks,
  598. * all of which have the same weight.
  599. */
  600. if (sched_feat(NORMALIZED_SLEEPER) && (!entity_is_task(se) ||
  601. task_of(se)->policy != SCHED_IDLE))
  602. thresh = calc_delta_fair(thresh, se);
  603. /*
  604. * Halve their sleep time's effect, to allow
  605. * for a gentler effect of sleepers:
  606. */
  607. if (sched_feat(GENTLE_FAIR_SLEEPERS))
  608. thresh >>= 1;
  609. vruntime -= thresh;
  610. }
  611. /* ensure we never gain time by being placed backwards. */
  612. vruntime = max_vruntime(se->vruntime, vruntime);
  613. se->vruntime = vruntime;
  614. }
  615. static void
  616. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  617. {
  618. /*
  619. * Update run-time statistics of the 'current'.
  620. */
  621. update_curr(cfs_rq);
  622. account_entity_enqueue(cfs_rq, se);
  623. if (wakeup) {
  624. place_entity(cfs_rq, se, 0);
  625. enqueue_sleeper(cfs_rq, se);
  626. }
  627. update_stats_enqueue(cfs_rq, se);
  628. check_spread(cfs_rq, se);
  629. if (se != cfs_rq->curr)
  630. __enqueue_entity(cfs_rq, se);
  631. }
  632. static void __clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  633. {
  634. if (!se || cfs_rq->last == se)
  635. cfs_rq->last = NULL;
  636. if (!se || cfs_rq->next == se)
  637. cfs_rq->next = NULL;
  638. }
  639. static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  640. {
  641. for_each_sched_entity(se)
  642. __clear_buddies(cfs_rq_of(se), se);
  643. }
  644. static void
  645. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  646. {
  647. /*
  648. * Update run-time statistics of the 'current'.
  649. */
  650. update_curr(cfs_rq);
  651. update_stats_dequeue(cfs_rq, se);
  652. if (sleep) {
  653. #ifdef CONFIG_SCHEDSTATS
  654. if (entity_is_task(se)) {
  655. struct task_struct *tsk = task_of(se);
  656. if (tsk->state & TASK_INTERRUPTIBLE)
  657. se->sleep_start = rq_of(cfs_rq)->clock;
  658. if (tsk->state & TASK_UNINTERRUPTIBLE)
  659. se->block_start = rq_of(cfs_rq)->clock;
  660. }
  661. #endif
  662. }
  663. clear_buddies(cfs_rq, se);
  664. if (se != cfs_rq->curr)
  665. __dequeue_entity(cfs_rq, se);
  666. account_entity_dequeue(cfs_rq, se);
  667. update_min_vruntime(cfs_rq);
  668. }
  669. /*
  670. * Preempt the current task with a newly woken task if needed:
  671. */
  672. static void
  673. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  674. {
  675. unsigned long ideal_runtime, delta_exec;
  676. ideal_runtime = sched_slice(cfs_rq, curr);
  677. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  678. if (delta_exec > ideal_runtime) {
  679. resched_task(rq_of(cfs_rq)->curr);
  680. /*
  681. * The current task ran long enough, ensure it doesn't get
  682. * re-elected due to buddy favours.
  683. */
  684. clear_buddies(cfs_rq, curr);
  685. return;
  686. }
  687. /*
  688. * Ensure that a task that missed wakeup preemption by a
  689. * narrow margin doesn't have to wait for a full slice.
  690. * This also mitigates buddy induced latencies under load.
  691. */
  692. if (!sched_feat(WAKEUP_PREEMPT))
  693. return;
  694. if (delta_exec < sysctl_sched_min_granularity)
  695. return;
  696. if (cfs_rq->nr_running > 1) {
  697. struct sched_entity *se = __pick_next_entity(cfs_rq);
  698. s64 delta = curr->vruntime - se->vruntime;
  699. if (delta > ideal_runtime)
  700. resched_task(rq_of(cfs_rq)->curr);
  701. }
  702. }
  703. static void
  704. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  705. {
  706. /* 'current' is not kept within the tree. */
  707. if (se->on_rq) {
  708. /*
  709. * Any task has to be enqueued before it get to execute on
  710. * a CPU. So account for the time it spent waiting on the
  711. * runqueue.
  712. */
  713. update_stats_wait_end(cfs_rq, se);
  714. __dequeue_entity(cfs_rq, se);
  715. }
  716. update_stats_curr_start(cfs_rq, se);
  717. cfs_rq->curr = se;
  718. #ifdef CONFIG_SCHEDSTATS
  719. /*
  720. * Track our maximum slice length, if the CPU's load is at
  721. * least twice that of our own weight (i.e. dont track it
  722. * when there are only lesser-weight tasks around):
  723. */
  724. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  725. se->slice_max = max(se->slice_max,
  726. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  727. }
  728. #endif
  729. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  730. }
  731. static int
  732. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
  733. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  734. {
  735. struct sched_entity *se = __pick_next_entity(cfs_rq);
  736. struct sched_entity *left = se;
  737. if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
  738. se = cfs_rq->next;
  739. /*
  740. * Prefer last buddy, try to return the CPU to a preempted task.
  741. */
  742. if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
  743. se = cfs_rq->last;
  744. clear_buddies(cfs_rq, se);
  745. return se;
  746. }
  747. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  748. {
  749. /*
  750. * If still on the runqueue then deactivate_task()
  751. * was not called and update_curr() has to be done:
  752. */
  753. if (prev->on_rq)
  754. update_curr(cfs_rq);
  755. check_spread(cfs_rq, prev);
  756. if (prev->on_rq) {
  757. update_stats_wait_start(cfs_rq, prev);
  758. /* Put 'current' back into the tree. */
  759. __enqueue_entity(cfs_rq, prev);
  760. }
  761. cfs_rq->curr = NULL;
  762. }
  763. static void
  764. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  765. {
  766. /*
  767. * Update run-time statistics of the 'current'.
  768. */
  769. update_curr(cfs_rq);
  770. #ifdef CONFIG_SCHED_HRTICK
  771. /*
  772. * queued ticks are scheduled to match the slice, so don't bother
  773. * validating it and just reschedule.
  774. */
  775. if (queued) {
  776. resched_task(rq_of(cfs_rq)->curr);
  777. return;
  778. }
  779. /*
  780. * don't let the period tick interfere with the hrtick preemption
  781. */
  782. if (!sched_feat(DOUBLE_TICK) &&
  783. hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  784. return;
  785. #endif
  786. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  787. check_preempt_tick(cfs_rq, curr);
  788. }
  789. /**************************************************
  790. * CFS operations on tasks:
  791. */
  792. #ifdef CONFIG_SCHED_HRTICK
  793. static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  794. {
  795. struct sched_entity *se = &p->se;
  796. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  797. WARN_ON(task_rq(p) != rq);
  798. if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
  799. u64 slice = sched_slice(cfs_rq, se);
  800. u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
  801. s64 delta = slice - ran;
  802. if (delta < 0) {
  803. if (rq->curr == p)
  804. resched_task(p);
  805. return;
  806. }
  807. /*
  808. * Don't schedule slices shorter than 10000ns, that just
  809. * doesn't make sense. Rely on vruntime for fairness.
  810. */
  811. if (rq->curr != p)
  812. delta = max_t(s64, 10000LL, delta);
  813. hrtick_start(rq, delta);
  814. }
  815. }
  816. /*
  817. * called from enqueue/dequeue and updates the hrtick when the
  818. * current task is from our class and nr_running is low enough
  819. * to matter.
  820. */
  821. static void hrtick_update(struct rq *rq)
  822. {
  823. struct task_struct *curr = rq->curr;
  824. if (curr->sched_class != &fair_sched_class)
  825. return;
  826. if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
  827. hrtick_start_fair(rq, curr);
  828. }
  829. #else /* !CONFIG_SCHED_HRTICK */
  830. static inline void
  831. hrtick_start_fair(struct rq *rq, struct task_struct *p)
  832. {
  833. }
  834. static inline void hrtick_update(struct rq *rq)
  835. {
  836. }
  837. #endif
  838. /*
  839. * The enqueue_task method is called before nr_running is
  840. * increased. Here we update the fair scheduling stats and
  841. * then put the task into the rbtree:
  842. */
  843. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  844. {
  845. struct cfs_rq *cfs_rq;
  846. struct sched_entity *se = &p->se;
  847. for_each_sched_entity(se) {
  848. if (se->on_rq)
  849. break;
  850. cfs_rq = cfs_rq_of(se);
  851. enqueue_entity(cfs_rq, se, wakeup);
  852. wakeup = 1;
  853. }
  854. hrtick_update(rq);
  855. }
  856. /*
  857. * The dequeue_task method is called before nr_running is
  858. * decreased. We remove the task from the rbtree and
  859. * update the fair scheduling stats:
  860. */
  861. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  862. {
  863. struct cfs_rq *cfs_rq;
  864. struct sched_entity *se = &p->se;
  865. for_each_sched_entity(se) {
  866. cfs_rq = cfs_rq_of(se);
  867. dequeue_entity(cfs_rq, se, sleep);
  868. /* Don't dequeue parent if it has other entities besides us */
  869. if (cfs_rq->load.weight)
  870. break;
  871. sleep = 1;
  872. }
  873. hrtick_update(rq);
  874. }
  875. /*
  876. * sched_yield() support is very simple - we dequeue and enqueue.
  877. *
  878. * If compat_yield is turned on then we requeue to the end of the tree.
  879. */
  880. static void yield_task_fair(struct rq *rq)
  881. {
  882. struct task_struct *curr = rq->curr;
  883. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  884. struct sched_entity *rightmost, *se = &curr->se;
  885. /*
  886. * Are we the only task in the tree?
  887. */
  888. if (unlikely(cfs_rq->nr_running == 1))
  889. return;
  890. clear_buddies(cfs_rq, se);
  891. if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
  892. update_rq_clock(rq);
  893. /*
  894. * Update run-time statistics of the 'current'.
  895. */
  896. update_curr(cfs_rq);
  897. return;
  898. }
  899. /*
  900. * Find the rightmost entry in the rbtree:
  901. */
  902. rightmost = __pick_last_entity(cfs_rq);
  903. /*
  904. * Already in the rightmost position?
  905. */
  906. if (unlikely(!rightmost || entity_before(rightmost, se)))
  907. return;
  908. /*
  909. * Minimally necessary key value to be last in the tree:
  910. * Upon rescheduling, sched_class::put_prev_task() will place
  911. * 'current' within the tree based on its new key value.
  912. */
  913. se->vruntime = rightmost->vruntime + 1;
  914. }
  915. #ifdef CONFIG_SMP
  916. #ifdef CONFIG_FAIR_GROUP_SCHED
  917. /*
  918. * effective_load() calculates the load change as seen from the root_task_group
  919. *
  920. * Adding load to a group doesn't make a group heavier, but can cause movement
  921. * of group shares between cpus. Assuming the shares were perfectly aligned one
  922. * can calculate the shift in shares.
  923. *
  924. * The problem is that perfectly aligning the shares is rather expensive, hence
  925. * we try to avoid doing that too often - see update_shares(), which ratelimits
  926. * this change.
  927. *
  928. * We compensate this by not only taking the current delta into account, but
  929. * also considering the delta between when the shares were last adjusted and
  930. * now.
  931. *
  932. * We still saw a performance dip, some tracing learned us that between
  933. * cgroup:/ and cgroup:/foo balancing the number of affine wakeups increased
  934. * significantly. Therefore try to bias the error in direction of failing
  935. * the affine wakeup.
  936. *
  937. */
  938. static long effective_load(struct task_group *tg, int cpu,
  939. long wl, long wg)
  940. {
  941. struct sched_entity *se = tg->se[cpu];
  942. if (!tg->parent)
  943. return wl;
  944. /*
  945. * By not taking the decrease of shares on the other cpu into
  946. * account our error leans towards reducing the affine wakeups.
  947. */
  948. if (!wl && sched_feat(ASYM_EFF_LOAD))
  949. return wl;
  950. for_each_sched_entity(se) {
  951. long S, rw, s, a, b;
  952. long more_w;
  953. /*
  954. * Instead of using this increment, also add the difference
  955. * between when the shares were last updated and now.
  956. */
  957. more_w = se->my_q->load.weight - se->my_q->rq_weight;
  958. wl += more_w;
  959. wg += more_w;
  960. S = se->my_q->tg->shares;
  961. s = se->my_q->shares;
  962. rw = se->my_q->rq_weight;
  963. a = S*(rw + wl);
  964. b = S*rw + s*wg;
  965. wl = s*(a-b);
  966. if (likely(b))
  967. wl /= b;
  968. /*
  969. * Assume the group is already running and will
  970. * thus already be accounted for in the weight.
  971. *
  972. * That is, moving shares between CPUs, does not
  973. * alter the group weight.
  974. */
  975. wg = 0;
  976. }
  977. return wl;
  978. }
  979. #else
  980. static inline unsigned long effective_load(struct task_group *tg, int cpu,
  981. unsigned long wl, unsigned long wg)
  982. {
  983. return wl;
  984. }
  985. #endif
  986. static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
  987. {
  988. struct task_struct *curr = current;
  989. unsigned long this_load, load;
  990. int idx, this_cpu, prev_cpu;
  991. unsigned long tl_per_task;
  992. unsigned int imbalance;
  993. struct task_group *tg;
  994. unsigned long weight;
  995. int balanced;
  996. idx = sd->wake_idx;
  997. this_cpu = smp_processor_id();
  998. prev_cpu = task_cpu(p);
  999. load = source_load(prev_cpu, idx);
  1000. this_load = target_load(this_cpu, idx);
  1001. if (sync) {
  1002. if (sched_feat(SYNC_LESS) &&
  1003. (curr->se.avg_overlap > sysctl_sched_migration_cost ||
  1004. p->se.avg_overlap > sysctl_sched_migration_cost))
  1005. sync = 0;
  1006. } else {
  1007. if (sched_feat(SYNC_MORE) &&
  1008. (curr->se.avg_overlap < sysctl_sched_migration_cost &&
  1009. p->se.avg_overlap < sysctl_sched_migration_cost))
  1010. sync = 1;
  1011. }
  1012. /*
  1013. * If sync wakeup then subtract the (maximum possible)
  1014. * effect of the currently running task from the load
  1015. * of the current CPU:
  1016. */
  1017. if (sync) {
  1018. tg = task_group(current);
  1019. weight = current->se.load.weight;
  1020. this_load += effective_load(tg, this_cpu, -weight, -weight);
  1021. load += effective_load(tg, prev_cpu, 0, -weight);
  1022. }
  1023. tg = task_group(p);
  1024. weight = p->se.load.weight;
  1025. imbalance = 100 + (sd->imbalance_pct - 100) / 2;
  1026. /*
  1027. * In low-load situations, where prev_cpu is idle and this_cpu is idle
  1028. * due to the sync cause above having dropped this_load to 0, we'll
  1029. * always have an imbalance, but there's really nothing you can do
  1030. * about that, so that's good too.
  1031. *
  1032. * Otherwise check if either cpus are near enough in load to allow this
  1033. * task to be woken on this_cpu.
  1034. */
  1035. balanced = !this_load ||
  1036. 100*(this_load + effective_load(tg, this_cpu, weight, weight)) <=
  1037. imbalance*(load + effective_load(tg, prev_cpu, 0, weight));
  1038. /*
  1039. * If the currently running task will sleep within
  1040. * a reasonable amount of time then attract this newly
  1041. * woken task:
  1042. */
  1043. if (sync && balanced)
  1044. return 1;
  1045. schedstat_inc(p, se.nr_wakeups_affine_attempts);
  1046. tl_per_task = cpu_avg_load_per_task(this_cpu);
  1047. if (balanced ||
  1048. (this_load <= load &&
  1049. this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
  1050. /*
  1051. * This domain has SD_WAKE_AFFINE and
  1052. * p is cache cold in this domain, and
  1053. * there is no bad imbalance.
  1054. */
  1055. schedstat_inc(sd, ttwu_move_affine);
  1056. schedstat_inc(p, se.nr_wakeups_affine);
  1057. return 1;
  1058. }
  1059. return 0;
  1060. }
  1061. /*
  1062. * find_idlest_group finds and returns the least busy CPU group within the
  1063. * domain.
  1064. */
  1065. static struct sched_group *
  1066. find_idlest_group(struct sched_domain *sd, struct task_struct *p,
  1067. int this_cpu, int load_idx)
  1068. {
  1069. struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
  1070. unsigned long min_load = ULONG_MAX, this_load = 0;
  1071. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  1072. do {
  1073. unsigned long load, avg_load;
  1074. int local_group;
  1075. int i;
  1076. /* Skip over this group if it has no CPUs allowed */
  1077. if (!cpumask_intersects(sched_group_cpus(group),
  1078. &p->cpus_allowed))
  1079. continue;
  1080. local_group = cpumask_test_cpu(this_cpu,
  1081. sched_group_cpus(group));
  1082. /* Tally up the load of all CPUs in the group */
  1083. avg_load = 0;
  1084. for_each_cpu(i, sched_group_cpus(group)) {
  1085. /* Bias balancing toward cpus of our domain */
  1086. if (local_group)
  1087. load = source_load(i, load_idx);
  1088. else
  1089. load = target_load(i, load_idx);
  1090. avg_load += load;
  1091. }
  1092. /* Adjust by relative CPU power of the group */
  1093. avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
  1094. if (local_group) {
  1095. this_load = avg_load;
  1096. this = group;
  1097. } else if (avg_load < min_load) {
  1098. min_load = avg_load;
  1099. idlest = group;
  1100. }
  1101. } while (group = group->next, group != sd->groups);
  1102. if (!idlest || 100*this_load < imbalance*min_load)
  1103. return NULL;
  1104. return idlest;
  1105. }
  1106. /*
  1107. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  1108. */
  1109. static int
  1110. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  1111. {
  1112. unsigned long load, min_load = ULONG_MAX;
  1113. int idlest = -1;
  1114. int i;
  1115. /* Traverse only the allowed CPUs */
  1116. for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
  1117. load = weighted_cpuload(i);
  1118. if (load < min_load || (load == min_load && i == this_cpu)) {
  1119. min_load = load;
  1120. idlest = i;
  1121. }
  1122. }
  1123. return idlest;
  1124. }
  1125. /*
  1126. * Try and locate an idle CPU in the sched_domain.
  1127. */
  1128. static int
  1129. select_idle_sibling(struct task_struct *p, struct sched_domain *sd, int target)
  1130. {
  1131. int cpu = smp_processor_id();
  1132. int prev_cpu = task_cpu(p);
  1133. int i;
  1134. /*
  1135. * If this domain spans both cpu and prev_cpu (see the SD_WAKE_AFFINE
  1136. * test in select_task_rq_fair) and the prev_cpu is idle then that's
  1137. * always a better target than the current cpu.
  1138. */
  1139. if (target == cpu && !cpu_rq(prev_cpu)->cfs.nr_running)
  1140. return prev_cpu;
  1141. /*
  1142. * Otherwise, iterate the domain and find an elegible idle cpu.
  1143. */
  1144. for_each_cpu_and(i, sched_domain_span(sd), &p->cpus_allowed) {
  1145. if (!cpu_rq(i)->cfs.nr_running) {
  1146. target = i;
  1147. break;
  1148. }
  1149. }
  1150. return target;
  1151. }
  1152. /*
  1153. * sched_balance_self: balance the current task (running on cpu) in domains
  1154. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  1155. * SD_BALANCE_EXEC.
  1156. *
  1157. * Balance, ie. select the least loaded group.
  1158. *
  1159. * Returns the target CPU number, or the same CPU if no balancing is needed.
  1160. *
  1161. * preempt must be disabled.
  1162. */
  1163. static int select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
  1164. {
  1165. struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
  1166. int cpu = smp_processor_id();
  1167. int prev_cpu = task_cpu(p);
  1168. int new_cpu = cpu;
  1169. int want_affine = 0;
  1170. int want_sd = 1;
  1171. int sync = wake_flags & WF_SYNC;
  1172. if (sd_flag & SD_BALANCE_WAKE) {
  1173. if (sched_feat(AFFINE_WAKEUPS) &&
  1174. cpumask_test_cpu(cpu, &p->cpus_allowed))
  1175. want_affine = 1;
  1176. new_cpu = prev_cpu;
  1177. }
  1178. for_each_domain(cpu, tmp) {
  1179. /*
  1180. * If power savings logic is enabled for a domain, see if we
  1181. * are not overloaded, if so, don't balance wider.
  1182. */
  1183. if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
  1184. unsigned long power = 0;
  1185. unsigned long nr_running = 0;
  1186. unsigned long capacity;
  1187. int i;
  1188. for_each_cpu(i, sched_domain_span(tmp)) {
  1189. power += power_of(i);
  1190. nr_running += cpu_rq(i)->cfs.nr_running;
  1191. }
  1192. capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
  1193. if (tmp->flags & SD_POWERSAVINGS_BALANCE)
  1194. nr_running /= 2;
  1195. if (nr_running < capacity)
  1196. want_sd = 0;
  1197. }
  1198. /*
  1199. * While iterating the domains looking for a spanning
  1200. * WAKE_AFFINE domain, adjust the affine target to any idle cpu
  1201. * in cache sharing domains along the way.
  1202. */
  1203. if (want_affine) {
  1204. int target = -1;
  1205. /*
  1206. * If both cpu and prev_cpu are part of this domain,
  1207. * cpu is a valid SD_WAKE_AFFINE target.
  1208. */
  1209. if (cpumask_test_cpu(prev_cpu, sched_domain_span(tmp)))
  1210. target = cpu;
  1211. /*
  1212. * If there's an idle sibling in this domain, make that
  1213. * the wake_affine target instead of the current cpu.
  1214. */
  1215. if (tmp->flags & SD_PREFER_SIBLING)
  1216. target = select_idle_sibling(p, tmp, target);
  1217. if (target >= 0) {
  1218. if (tmp->flags & SD_WAKE_AFFINE) {
  1219. affine_sd = tmp;
  1220. want_affine = 0;
  1221. }
  1222. cpu = target;
  1223. }
  1224. }
  1225. if (!want_sd && !want_affine)
  1226. break;
  1227. if (!(tmp->flags & sd_flag))
  1228. continue;
  1229. if (want_sd)
  1230. sd = tmp;
  1231. }
  1232. if (sched_feat(LB_SHARES_UPDATE)) {
  1233. /*
  1234. * Pick the largest domain to update shares over
  1235. */
  1236. tmp = sd;
  1237. if (affine_sd && (!tmp ||
  1238. cpumask_weight(sched_domain_span(affine_sd)) >
  1239. cpumask_weight(sched_domain_span(sd))))
  1240. tmp = affine_sd;
  1241. if (tmp)
  1242. update_shares(tmp);
  1243. }
  1244. if (affine_sd && wake_affine(affine_sd, p, sync))
  1245. return cpu;
  1246. while (sd) {
  1247. int load_idx = sd->forkexec_idx;
  1248. struct sched_group *group;
  1249. int weight;
  1250. if (!(sd->flags & sd_flag)) {
  1251. sd = sd->child;
  1252. continue;
  1253. }
  1254. if (sd_flag & SD_BALANCE_WAKE)
  1255. load_idx = sd->wake_idx;
  1256. group = find_idlest_group(sd, p, cpu, load_idx);
  1257. if (!group) {
  1258. sd = sd->child;
  1259. continue;
  1260. }
  1261. new_cpu = find_idlest_cpu(group, p, cpu);
  1262. if (new_cpu == -1 || new_cpu == cpu) {
  1263. /* Now try balancing at a lower domain level of cpu */
  1264. sd = sd->child;
  1265. continue;
  1266. }
  1267. /* Now try balancing at a lower domain level of new_cpu */
  1268. cpu = new_cpu;
  1269. weight = cpumask_weight(sched_domain_span(sd));
  1270. sd = NULL;
  1271. for_each_domain(cpu, tmp) {
  1272. if (weight <= cpumask_weight(sched_domain_span(tmp)))
  1273. break;
  1274. if (tmp->flags & sd_flag)
  1275. sd = tmp;
  1276. }
  1277. /* while loop will break here if sd == NULL */
  1278. }
  1279. return new_cpu;
  1280. }
  1281. #endif /* CONFIG_SMP */
  1282. /*
  1283. * Adaptive granularity
  1284. *
  1285. * se->avg_wakeup gives the average time a task runs until it does a wakeup,
  1286. * with the limit of wakeup_gran -- when it never does a wakeup.
  1287. *
  1288. * So the smaller avg_wakeup is the faster we want this task to preempt,
  1289. * but we don't want to treat the preemptee unfairly and therefore allow it
  1290. * to run for at least the amount of time we'd like to run.
  1291. *
  1292. * NOTE: we use 2*avg_wakeup to increase the probability of actually doing one
  1293. *
  1294. * NOTE: we use *nr_running to scale with load, this nicely matches the
  1295. * degrading latency on load.
  1296. */
  1297. static unsigned long
  1298. adaptive_gran(struct sched_entity *curr, struct sched_entity *se)
  1299. {
  1300. u64 this_run = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  1301. u64 expected_wakeup = 2*se->avg_wakeup * cfs_rq_of(se)->nr_running;
  1302. u64 gran = 0;
  1303. if (this_run < expected_wakeup)
  1304. gran = expected_wakeup - this_run;
  1305. return min_t(s64, gran, sysctl_sched_wakeup_granularity);
  1306. }
  1307. static unsigned long
  1308. wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
  1309. {
  1310. unsigned long gran = sysctl_sched_wakeup_granularity;
  1311. if (cfs_rq_of(curr)->curr && sched_feat(ADAPTIVE_GRAN))
  1312. gran = adaptive_gran(curr, se);
  1313. /*
  1314. * Since its curr running now, convert the gran from real-time
  1315. * to virtual-time in his units.
  1316. */
  1317. if (sched_feat(ASYM_GRAN)) {
  1318. /*
  1319. * By using 'se' instead of 'curr' we penalize light tasks, so
  1320. * they get preempted easier. That is, if 'se' < 'curr' then
  1321. * the resulting gran will be larger, therefore penalizing the
  1322. * lighter, if otoh 'se' > 'curr' then the resulting gran will
  1323. * be smaller, again penalizing the lighter task.
  1324. *
  1325. * This is especially important for buddies when the leftmost
  1326. * task is higher priority than the buddy.
  1327. */
  1328. if (unlikely(se->load.weight != NICE_0_LOAD))
  1329. gran = calc_delta_fair(gran, se);
  1330. } else {
  1331. if (unlikely(curr->load.weight != NICE_0_LOAD))
  1332. gran = calc_delta_fair(gran, curr);
  1333. }
  1334. return gran;
  1335. }
  1336. /*
  1337. * Should 'se' preempt 'curr'.
  1338. *
  1339. * |s1
  1340. * |s2
  1341. * |s3
  1342. * g
  1343. * |<--->|c
  1344. *
  1345. * w(c, s1) = -1
  1346. * w(c, s2) = 0
  1347. * w(c, s3) = 1
  1348. *
  1349. */
  1350. static int
  1351. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
  1352. {
  1353. s64 gran, vdiff = curr->vruntime - se->vruntime;
  1354. if (vdiff <= 0)
  1355. return -1;
  1356. gran = wakeup_gran(curr, se);
  1357. if (vdiff > gran)
  1358. return 1;
  1359. return 0;
  1360. }
  1361. static void set_last_buddy(struct sched_entity *se)
  1362. {
  1363. if (likely(task_of(se)->policy != SCHED_IDLE)) {
  1364. for_each_sched_entity(se)
  1365. cfs_rq_of(se)->last = se;
  1366. }
  1367. }
  1368. static void set_next_buddy(struct sched_entity *se)
  1369. {
  1370. if (likely(task_of(se)->policy != SCHED_IDLE)) {
  1371. for_each_sched_entity(se)
  1372. cfs_rq_of(se)->next = se;
  1373. }
  1374. }
  1375. /*
  1376. * Preempt the current task with a newly woken task if needed:
  1377. */
  1378. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  1379. {
  1380. struct task_struct *curr = rq->curr;
  1381. struct sched_entity *se = &curr->se, *pse = &p->se;
  1382. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  1383. int sync = wake_flags & WF_SYNC;
  1384. int scale = cfs_rq->nr_running >= sched_nr_latency;
  1385. if (unlikely(rt_prio(p->prio)))
  1386. goto preempt;
  1387. if (unlikely(p->sched_class != &fair_sched_class))
  1388. return;
  1389. if (unlikely(se == pse))
  1390. return;
  1391. if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK))
  1392. set_next_buddy(pse);
  1393. /*
  1394. * We can come here with TIF_NEED_RESCHED already set from new task
  1395. * wake up path.
  1396. */
  1397. if (test_tsk_need_resched(curr))
  1398. return;
  1399. /*
  1400. * Batch and idle tasks do not preempt (their preemption is driven by
  1401. * the tick):
  1402. */
  1403. if (unlikely(p->policy != SCHED_NORMAL))
  1404. return;
  1405. /* Idle tasks are by definition preempted by everybody. */
  1406. if (unlikely(curr->policy == SCHED_IDLE))
  1407. goto preempt;
  1408. if (sched_feat(WAKEUP_SYNC) && sync)
  1409. goto preempt;
  1410. if (sched_feat(WAKEUP_OVERLAP) &&
  1411. se->avg_overlap < sysctl_sched_migration_cost &&
  1412. pse->avg_overlap < sysctl_sched_migration_cost)
  1413. goto preempt;
  1414. if (!sched_feat(WAKEUP_PREEMPT))
  1415. return;
  1416. update_curr(cfs_rq);
  1417. find_matching_se(&se, &pse);
  1418. BUG_ON(!pse);
  1419. if (wakeup_preempt_entity(se, pse) == 1)
  1420. goto preempt;
  1421. return;
  1422. preempt:
  1423. resched_task(curr);
  1424. /*
  1425. * Only set the backward buddy when the current task is still
  1426. * on the rq. This can happen when a wakeup gets interleaved
  1427. * with schedule on the ->pre_schedule() or idle_balance()
  1428. * point, either of which can * drop the rq lock.
  1429. *
  1430. * Also, during early boot the idle thread is in the fair class,
  1431. * for obvious reasons its a bad idea to schedule back to it.
  1432. */
  1433. if (unlikely(!se->on_rq || curr == rq->idle))
  1434. return;
  1435. if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
  1436. set_last_buddy(se);
  1437. }
  1438. static struct task_struct *pick_next_task_fair(struct rq *rq)
  1439. {
  1440. struct task_struct *p;
  1441. struct cfs_rq *cfs_rq = &rq->cfs;
  1442. struct sched_entity *se;
  1443. if (!cfs_rq->nr_running)
  1444. return NULL;
  1445. do {
  1446. se = pick_next_entity(cfs_rq);
  1447. set_next_entity(cfs_rq, se);
  1448. cfs_rq = group_cfs_rq(se);
  1449. } while (cfs_rq);
  1450. p = task_of(se);
  1451. hrtick_start_fair(rq, p);
  1452. return p;
  1453. }
  1454. /*
  1455. * Account for a descheduled task:
  1456. */
  1457. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  1458. {
  1459. struct sched_entity *se = &prev->se;
  1460. struct cfs_rq *cfs_rq;
  1461. for_each_sched_entity(se) {
  1462. cfs_rq = cfs_rq_of(se);
  1463. put_prev_entity(cfs_rq, se);
  1464. }
  1465. }
  1466. #ifdef CONFIG_SMP
  1467. /**************************************************
  1468. * Fair scheduling class load-balancing methods:
  1469. */
  1470. /*
  1471. * Load-balancing iterator. Note: while the runqueue stays locked
  1472. * during the whole iteration, the current task might be
  1473. * dequeued so the iterator has to be dequeue-safe. Here we
  1474. * achieve that by always pre-iterating before returning
  1475. * the current task:
  1476. */
  1477. static struct task_struct *
  1478. __load_balance_iterator(struct cfs_rq *cfs_rq, struct list_head *next)
  1479. {
  1480. struct task_struct *p = NULL;
  1481. struct sched_entity *se;
  1482. if (next == &cfs_rq->tasks)
  1483. return NULL;
  1484. se = list_entry(next, struct sched_entity, group_node);
  1485. p = task_of(se);
  1486. cfs_rq->balance_iterator = next->next;
  1487. return p;
  1488. }
  1489. static struct task_struct *load_balance_start_fair(void *arg)
  1490. {
  1491. struct cfs_rq *cfs_rq = arg;
  1492. return __load_balance_iterator(cfs_rq, cfs_rq->tasks.next);
  1493. }
  1494. static struct task_struct *load_balance_next_fair(void *arg)
  1495. {
  1496. struct cfs_rq *cfs_rq = arg;
  1497. return __load_balance_iterator(cfs_rq, cfs_rq->balance_iterator);
  1498. }
  1499. static unsigned long
  1500. __load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1501. unsigned long max_load_move, struct sched_domain *sd,
  1502. enum cpu_idle_type idle, int *all_pinned, int *this_best_prio,
  1503. struct cfs_rq *cfs_rq)
  1504. {
  1505. struct rq_iterator cfs_rq_iterator;
  1506. cfs_rq_iterator.start = load_balance_start_fair;
  1507. cfs_rq_iterator.next = load_balance_next_fair;
  1508. cfs_rq_iterator.arg = cfs_rq;
  1509. return balance_tasks(this_rq, this_cpu, busiest,
  1510. max_load_move, sd, idle, all_pinned,
  1511. this_best_prio, &cfs_rq_iterator);
  1512. }
  1513. #ifdef CONFIG_FAIR_GROUP_SCHED
  1514. static unsigned long
  1515. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1516. unsigned long max_load_move,
  1517. struct sched_domain *sd, enum cpu_idle_type idle,
  1518. int *all_pinned, int *this_best_prio)
  1519. {
  1520. long rem_load_move = max_load_move;
  1521. int busiest_cpu = cpu_of(busiest);
  1522. struct task_group *tg;
  1523. rcu_read_lock();
  1524. update_h_load(busiest_cpu);
  1525. list_for_each_entry_rcu(tg, &task_groups, list) {
  1526. struct cfs_rq *busiest_cfs_rq = tg->cfs_rq[busiest_cpu];
  1527. unsigned long busiest_h_load = busiest_cfs_rq->h_load;
  1528. unsigned long busiest_weight = busiest_cfs_rq->load.weight;
  1529. u64 rem_load, moved_load;
  1530. /*
  1531. * empty group
  1532. */
  1533. if (!busiest_cfs_rq->task_weight)
  1534. continue;
  1535. rem_load = (u64)rem_load_move * busiest_weight;
  1536. rem_load = div_u64(rem_load, busiest_h_load + 1);
  1537. moved_load = __load_balance_fair(this_rq, this_cpu, busiest,
  1538. rem_load, sd, idle, all_pinned, this_best_prio,
  1539. tg->cfs_rq[busiest_cpu]);
  1540. if (!moved_load)
  1541. continue;
  1542. moved_load *= busiest_h_load;
  1543. moved_load = div_u64(moved_load, busiest_weight + 1);
  1544. rem_load_move -= moved_load;
  1545. if (rem_load_move < 0)
  1546. break;
  1547. }
  1548. rcu_read_unlock();
  1549. return max_load_move - rem_load_move;
  1550. }
  1551. #else
  1552. static unsigned long
  1553. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1554. unsigned long max_load_move,
  1555. struct sched_domain *sd, enum cpu_idle_type idle,
  1556. int *all_pinned, int *this_best_prio)
  1557. {
  1558. return __load_balance_fair(this_rq, this_cpu, busiest,
  1559. max_load_move, sd, idle, all_pinned,
  1560. this_best_prio, &busiest->cfs);
  1561. }
  1562. #endif
  1563. static int
  1564. move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1565. struct sched_domain *sd, enum cpu_idle_type idle)
  1566. {
  1567. struct cfs_rq *busy_cfs_rq;
  1568. struct rq_iterator cfs_rq_iterator;
  1569. cfs_rq_iterator.start = load_balance_start_fair;
  1570. cfs_rq_iterator.next = load_balance_next_fair;
  1571. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  1572. /*
  1573. * pass busy_cfs_rq argument into
  1574. * load_balance_[start|next]_fair iterators
  1575. */
  1576. cfs_rq_iterator.arg = busy_cfs_rq;
  1577. if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  1578. &cfs_rq_iterator))
  1579. return 1;
  1580. }
  1581. return 0;
  1582. }
  1583. static void rq_online_fair(struct rq *rq)
  1584. {
  1585. update_sysctl();
  1586. }
  1587. static void rq_offline_fair(struct rq *rq)
  1588. {
  1589. update_sysctl();
  1590. }
  1591. #endif /* CONFIG_SMP */
  1592. /*
  1593. * scheduler tick hitting a task of our scheduling class:
  1594. */
  1595. static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  1596. {
  1597. struct cfs_rq *cfs_rq;
  1598. struct sched_entity *se = &curr->se;
  1599. for_each_sched_entity(se) {
  1600. cfs_rq = cfs_rq_of(se);
  1601. entity_tick(cfs_rq, se, queued);
  1602. }
  1603. }
  1604. /*
  1605. * called on fork with the child task as argument from the parent's context
  1606. * - child not yet on the tasklist
  1607. * - preemption disabled
  1608. */
  1609. static void task_fork_fair(struct task_struct *p)
  1610. {
  1611. struct cfs_rq *cfs_rq = task_cfs_rq(current);
  1612. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  1613. int this_cpu = smp_processor_id();
  1614. struct rq *rq = this_rq();
  1615. unsigned long flags;
  1616. spin_lock_irqsave(&rq->lock, flags);
  1617. if (unlikely(task_cpu(p) != this_cpu))
  1618. __set_task_cpu(p, this_cpu);
  1619. update_curr(cfs_rq);
  1620. if (curr)
  1621. se->vruntime = curr->vruntime;
  1622. place_entity(cfs_rq, se, 1);
  1623. if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
  1624. /*
  1625. * Upon rescheduling, sched_class::put_prev_task() will place
  1626. * 'current' within the tree based on its new key value.
  1627. */
  1628. swap(curr->vruntime, se->vruntime);
  1629. resched_task(rq->curr);
  1630. }
  1631. spin_unlock_irqrestore(&rq->lock, flags);
  1632. }
  1633. /*
  1634. * Priority of the task has changed. Check to see if we preempt
  1635. * the current task.
  1636. */
  1637. static void prio_changed_fair(struct rq *rq, struct task_struct *p,
  1638. int oldprio, int running)
  1639. {
  1640. /*
  1641. * Reschedule if we are currently running on this runqueue and
  1642. * our priority decreased, or if we are not currently running on
  1643. * this runqueue and our priority is higher than the current's
  1644. */
  1645. if (running) {
  1646. if (p->prio > oldprio)
  1647. resched_task(rq->curr);
  1648. } else
  1649. check_preempt_curr(rq, p, 0);
  1650. }
  1651. /*
  1652. * We switched to the sched_fair class.
  1653. */
  1654. static void switched_to_fair(struct rq *rq, struct task_struct *p,
  1655. int running)
  1656. {
  1657. /*
  1658. * We were most likely switched from sched_rt, so
  1659. * kick off the schedule if running, otherwise just see
  1660. * if we can still preempt the current task.
  1661. */
  1662. if (running)
  1663. resched_task(rq->curr);
  1664. else
  1665. check_preempt_curr(rq, p, 0);
  1666. }
  1667. /* Account for a task changing its policy or group.
  1668. *
  1669. * This routine is mostly called to set cfs_rq->curr field when a task
  1670. * migrates between groups/classes.
  1671. */
  1672. static void set_curr_task_fair(struct rq *rq)
  1673. {
  1674. struct sched_entity *se = &rq->curr->se;
  1675. for_each_sched_entity(se)
  1676. set_next_entity(cfs_rq_of(se), se);
  1677. }
  1678. #ifdef CONFIG_FAIR_GROUP_SCHED
  1679. static void moved_group_fair(struct task_struct *p)
  1680. {
  1681. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  1682. update_curr(cfs_rq);
  1683. place_entity(cfs_rq, &p->se, 1);
  1684. }
  1685. #endif
  1686. unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
  1687. {
  1688. struct sched_entity *se = &task->se;
  1689. unsigned int rr_interval = 0;
  1690. /*
  1691. * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
  1692. * idle runqueue:
  1693. */
  1694. if (rq->cfs.load.weight)
  1695. rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
  1696. return rr_interval;
  1697. }
  1698. /*
  1699. * All the scheduling class methods:
  1700. */
  1701. static const struct sched_class fair_sched_class = {
  1702. .next = &idle_sched_class,
  1703. .enqueue_task = enqueue_task_fair,
  1704. .dequeue_task = dequeue_task_fair,
  1705. .yield_task = yield_task_fair,
  1706. .check_preempt_curr = check_preempt_wakeup,
  1707. .pick_next_task = pick_next_task_fair,
  1708. .put_prev_task = put_prev_task_fair,
  1709. #ifdef CONFIG_SMP
  1710. .select_task_rq = select_task_rq_fair,
  1711. .load_balance = load_balance_fair,
  1712. .move_one_task = move_one_task_fair,
  1713. .rq_online = rq_online_fair,
  1714. .rq_offline = rq_offline_fair,
  1715. #endif
  1716. .set_curr_task = set_curr_task_fair,
  1717. .task_tick = task_tick_fair,
  1718. .task_fork = task_fork_fair,
  1719. .prio_changed = prio_changed_fair,
  1720. .switched_to = switched_to_fair,
  1721. .get_rr_interval = get_rr_interval_fair,
  1722. #ifdef CONFIG_FAIR_GROUP_SCHED
  1723. .moved_group = moved_group_fair,
  1724. #endif
  1725. };
  1726. #ifdef CONFIG_SCHED_DEBUG
  1727. static void print_cfs_stats(struct seq_file *m, int cpu)
  1728. {
  1729. struct cfs_rq *cfs_rq;
  1730. rcu_read_lock();
  1731. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  1732. print_cfs_rq(m, cpu, cfs_rq);
  1733. rcu_read_unlock();
  1734. }
  1735. #endif