sched_fair.c 34 KB

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