sched_fair.c 35 KB

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