sched_fair.c 34 KB

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