sched_fair.c 35 KB

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