sched_rt.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  1. /*
  2. * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
  3. * policies)
  4. */
  5. static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
  6. {
  7. return container_of(rt_se, struct task_struct, rt);
  8. }
  9. #ifdef CONFIG_RT_GROUP_SCHED
  10. #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
  11. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  12. {
  13. return rt_rq->rq;
  14. }
  15. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  16. {
  17. return rt_se->rt_rq;
  18. }
  19. #else /* CONFIG_RT_GROUP_SCHED */
  20. #define rt_entity_is_task(rt_se) (1)
  21. static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
  22. {
  23. return container_of(rt_rq, struct rq, rt);
  24. }
  25. static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
  26. {
  27. struct task_struct *p = rt_task_of(rt_se);
  28. struct rq *rq = task_rq(p);
  29. return &rq->rt;
  30. }
  31. #endif /* CONFIG_RT_GROUP_SCHED */
  32. #ifdef CONFIG_SMP
  33. static inline int rt_overloaded(struct rq *rq)
  34. {
  35. return atomic_read(&rq->rd->rto_count);
  36. }
  37. static inline void rt_set_overload(struct rq *rq)
  38. {
  39. if (!rq->online)
  40. return;
  41. cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
  42. /*
  43. * Make sure the mask is visible before we set
  44. * the overload count. That is checked to determine
  45. * if we should look at the mask. It would be a shame
  46. * if we looked at the mask, but the mask was not
  47. * updated yet.
  48. */
  49. wmb();
  50. atomic_inc(&rq->rd->rto_count);
  51. }
  52. static inline void rt_clear_overload(struct rq *rq)
  53. {
  54. if (!rq->online)
  55. return;
  56. /* the order here really doesn't matter */
  57. atomic_dec(&rq->rd->rto_count);
  58. cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
  59. }
  60. static void update_rt_migration(struct rt_rq *rt_rq)
  61. {
  62. if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
  63. if (!rt_rq->overloaded) {
  64. rt_set_overload(rq_of_rt_rq(rt_rq));
  65. rt_rq->overloaded = 1;
  66. }
  67. } else if (rt_rq->overloaded) {
  68. rt_clear_overload(rq_of_rt_rq(rt_rq));
  69. rt_rq->overloaded = 0;
  70. }
  71. }
  72. static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  73. {
  74. if (!rt_entity_is_task(rt_se))
  75. return;
  76. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  77. rt_rq->rt_nr_total++;
  78. if (rt_se->nr_cpus_allowed > 1)
  79. rt_rq->rt_nr_migratory++;
  80. update_rt_migration(rt_rq);
  81. }
  82. static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  83. {
  84. if (!rt_entity_is_task(rt_se))
  85. return;
  86. rt_rq = &rq_of_rt_rq(rt_rq)->rt;
  87. rt_rq->rt_nr_total--;
  88. if (rt_se->nr_cpus_allowed > 1)
  89. rt_rq->rt_nr_migratory--;
  90. update_rt_migration(rt_rq);
  91. }
  92. static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
  93. {
  94. plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
  95. plist_node_init(&p->pushable_tasks, p->prio);
  96. plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
  97. }
  98. static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
  99. {
  100. plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
  101. }
  102. #else
  103. static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
  104. {
  105. }
  106. static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
  107. {
  108. }
  109. static inline
  110. void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  111. {
  112. }
  113. static inline
  114. void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  115. {
  116. }
  117. #endif /* CONFIG_SMP */
  118. static inline int on_rt_rq(struct sched_rt_entity *rt_se)
  119. {
  120. return !list_empty(&rt_se->run_list);
  121. }
  122. #ifdef CONFIG_RT_GROUP_SCHED
  123. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  124. {
  125. if (!rt_rq->tg)
  126. return RUNTIME_INF;
  127. return rt_rq->rt_runtime;
  128. }
  129. static inline u64 sched_rt_period(struct rt_rq *rt_rq)
  130. {
  131. return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
  132. }
  133. #define for_each_leaf_rt_rq(rt_rq, rq) \
  134. list_for_each_entry_rcu(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
  135. #define for_each_sched_rt_entity(rt_se) \
  136. for (; rt_se; rt_se = rt_se->parent)
  137. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  138. {
  139. return rt_se->my_q;
  140. }
  141. static void enqueue_rt_entity(struct sched_rt_entity *rt_se);
  142. static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
  143. static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  144. {
  145. struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
  146. struct sched_rt_entity *rt_se = rt_rq->rt_se;
  147. if (rt_rq->rt_nr_running) {
  148. if (rt_se && !on_rt_rq(rt_se))
  149. enqueue_rt_entity(rt_se);
  150. if (rt_rq->highest_prio.curr < curr->prio)
  151. resched_task(curr);
  152. }
  153. }
  154. static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  155. {
  156. struct sched_rt_entity *rt_se = rt_rq->rt_se;
  157. if (rt_se && on_rt_rq(rt_se))
  158. dequeue_rt_entity(rt_se);
  159. }
  160. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  161. {
  162. return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
  163. }
  164. static int rt_se_boosted(struct sched_rt_entity *rt_se)
  165. {
  166. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  167. struct task_struct *p;
  168. if (rt_rq)
  169. return !!rt_rq->rt_nr_boosted;
  170. p = rt_task_of(rt_se);
  171. return p->prio != p->normal_prio;
  172. }
  173. #ifdef CONFIG_SMP
  174. static inline const struct cpumask *sched_rt_period_mask(void)
  175. {
  176. return cpu_rq(smp_processor_id())->rd->span;
  177. }
  178. #else
  179. static inline const struct cpumask *sched_rt_period_mask(void)
  180. {
  181. return cpu_online_mask;
  182. }
  183. #endif
  184. static inline
  185. struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
  186. {
  187. return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
  188. }
  189. static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
  190. {
  191. return &rt_rq->tg->rt_bandwidth;
  192. }
  193. #else /* !CONFIG_RT_GROUP_SCHED */
  194. static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
  195. {
  196. return rt_rq->rt_runtime;
  197. }
  198. static inline u64 sched_rt_period(struct rt_rq *rt_rq)
  199. {
  200. return ktime_to_ns(def_rt_bandwidth.rt_period);
  201. }
  202. #define for_each_leaf_rt_rq(rt_rq, rq) \
  203. for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
  204. #define for_each_sched_rt_entity(rt_se) \
  205. for (; rt_se; rt_se = NULL)
  206. static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
  207. {
  208. return NULL;
  209. }
  210. static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
  211. {
  212. if (rt_rq->rt_nr_running)
  213. resched_task(rq_of_rt_rq(rt_rq)->curr);
  214. }
  215. static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
  216. {
  217. }
  218. static inline int rt_rq_throttled(struct rt_rq *rt_rq)
  219. {
  220. return rt_rq->rt_throttled;
  221. }
  222. static inline const struct cpumask *sched_rt_period_mask(void)
  223. {
  224. return cpu_online_mask;
  225. }
  226. static inline
  227. struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
  228. {
  229. return &cpu_rq(cpu)->rt;
  230. }
  231. static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
  232. {
  233. return &def_rt_bandwidth;
  234. }
  235. #endif /* CONFIG_RT_GROUP_SCHED */
  236. #ifdef CONFIG_SMP
  237. /*
  238. * We ran out of runtime, see if we can borrow some from our neighbours.
  239. */
  240. static int do_balance_runtime(struct rt_rq *rt_rq)
  241. {
  242. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  243. struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
  244. int i, weight, more = 0;
  245. u64 rt_period;
  246. weight = cpumask_weight(rd->span);
  247. spin_lock(&rt_b->rt_runtime_lock);
  248. rt_period = ktime_to_ns(rt_b->rt_period);
  249. for_each_cpu(i, rd->span) {
  250. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  251. s64 diff;
  252. if (iter == rt_rq)
  253. continue;
  254. spin_lock(&iter->rt_runtime_lock);
  255. /*
  256. * Either all rqs have inf runtime and there's nothing to steal
  257. * or __disable_runtime() below sets a specific rq to inf to
  258. * indicate its been disabled and disalow stealing.
  259. */
  260. if (iter->rt_runtime == RUNTIME_INF)
  261. goto next;
  262. /*
  263. * From runqueues with spare time, take 1/n part of their
  264. * spare time, but no more than our period.
  265. */
  266. diff = iter->rt_runtime - iter->rt_time;
  267. if (diff > 0) {
  268. diff = div_u64((u64)diff, weight);
  269. if (rt_rq->rt_runtime + diff > rt_period)
  270. diff = rt_period - rt_rq->rt_runtime;
  271. iter->rt_runtime -= diff;
  272. rt_rq->rt_runtime += diff;
  273. more = 1;
  274. if (rt_rq->rt_runtime == rt_period) {
  275. spin_unlock(&iter->rt_runtime_lock);
  276. break;
  277. }
  278. }
  279. next:
  280. spin_unlock(&iter->rt_runtime_lock);
  281. }
  282. spin_unlock(&rt_b->rt_runtime_lock);
  283. return more;
  284. }
  285. /*
  286. * Ensure this RQ takes back all the runtime it lend to its neighbours.
  287. */
  288. static void __disable_runtime(struct rq *rq)
  289. {
  290. struct root_domain *rd = rq->rd;
  291. struct rt_rq *rt_rq;
  292. if (unlikely(!scheduler_running))
  293. return;
  294. for_each_leaf_rt_rq(rt_rq, rq) {
  295. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  296. s64 want;
  297. int i;
  298. spin_lock(&rt_b->rt_runtime_lock);
  299. spin_lock(&rt_rq->rt_runtime_lock);
  300. /*
  301. * Either we're all inf and nobody needs to borrow, or we're
  302. * already disabled and thus have nothing to do, or we have
  303. * exactly the right amount of runtime to take out.
  304. */
  305. if (rt_rq->rt_runtime == RUNTIME_INF ||
  306. rt_rq->rt_runtime == rt_b->rt_runtime)
  307. goto balanced;
  308. spin_unlock(&rt_rq->rt_runtime_lock);
  309. /*
  310. * Calculate the difference between what we started out with
  311. * and what we current have, that's the amount of runtime
  312. * we lend and now have to reclaim.
  313. */
  314. want = rt_b->rt_runtime - rt_rq->rt_runtime;
  315. /*
  316. * Greedy reclaim, take back as much as we can.
  317. */
  318. for_each_cpu(i, rd->span) {
  319. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  320. s64 diff;
  321. /*
  322. * Can't reclaim from ourselves or disabled runqueues.
  323. */
  324. if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
  325. continue;
  326. spin_lock(&iter->rt_runtime_lock);
  327. if (want > 0) {
  328. diff = min_t(s64, iter->rt_runtime, want);
  329. iter->rt_runtime -= diff;
  330. want -= diff;
  331. } else {
  332. iter->rt_runtime -= want;
  333. want -= want;
  334. }
  335. spin_unlock(&iter->rt_runtime_lock);
  336. if (!want)
  337. break;
  338. }
  339. spin_lock(&rt_rq->rt_runtime_lock);
  340. /*
  341. * We cannot be left wanting - that would mean some runtime
  342. * leaked out of the system.
  343. */
  344. BUG_ON(want);
  345. balanced:
  346. /*
  347. * Disable all the borrow logic by pretending we have inf
  348. * runtime - in which case borrowing doesn't make sense.
  349. */
  350. rt_rq->rt_runtime = RUNTIME_INF;
  351. spin_unlock(&rt_rq->rt_runtime_lock);
  352. spin_unlock(&rt_b->rt_runtime_lock);
  353. }
  354. }
  355. static void disable_runtime(struct rq *rq)
  356. {
  357. unsigned long flags;
  358. spin_lock_irqsave(&rq->lock, flags);
  359. __disable_runtime(rq);
  360. spin_unlock_irqrestore(&rq->lock, flags);
  361. }
  362. static void __enable_runtime(struct rq *rq)
  363. {
  364. struct rt_rq *rt_rq;
  365. if (unlikely(!scheduler_running))
  366. return;
  367. /*
  368. * Reset each runqueue's bandwidth settings
  369. */
  370. for_each_leaf_rt_rq(rt_rq, rq) {
  371. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  372. spin_lock(&rt_b->rt_runtime_lock);
  373. spin_lock(&rt_rq->rt_runtime_lock);
  374. rt_rq->rt_runtime = rt_b->rt_runtime;
  375. rt_rq->rt_time = 0;
  376. rt_rq->rt_throttled = 0;
  377. spin_unlock(&rt_rq->rt_runtime_lock);
  378. spin_unlock(&rt_b->rt_runtime_lock);
  379. }
  380. }
  381. static void enable_runtime(struct rq *rq)
  382. {
  383. unsigned long flags;
  384. spin_lock_irqsave(&rq->lock, flags);
  385. __enable_runtime(rq);
  386. spin_unlock_irqrestore(&rq->lock, flags);
  387. }
  388. static int balance_runtime(struct rt_rq *rt_rq)
  389. {
  390. int more = 0;
  391. if (rt_rq->rt_time > rt_rq->rt_runtime) {
  392. spin_unlock(&rt_rq->rt_runtime_lock);
  393. more = do_balance_runtime(rt_rq);
  394. spin_lock(&rt_rq->rt_runtime_lock);
  395. }
  396. return more;
  397. }
  398. #else /* !CONFIG_SMP */
  399. static inline int balance_runtime(struct rt_rq *rt_rq)
  400. {
  401. return 0;
  402. }
  403. #endif /* CONFIG_SMP */
  404. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
  405. {
  406. int i, idle = 1;
  407. const struct cpumask *span;
  408. if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
  409. return 1;
  410. span = sched_rt_period_mask();
  411. for_each_cpu(i, span) {
  412. int enqueue = 0;
  413. struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
  414. struct rq *rq = rq_of_rt_rq(rt_rq);
  415. spin_lock(&rq->lock);
  416. if (rt_rq->rt_time) {
  417. u64 runtime;
  418. spin_lock(&rt_rq->rt_runtime_lock);
  419. if (rt_rq->rt_throttled)
  420. balance_runtime(rt_rq);
  421. runtime = rt_rq->rt_runtime;
  422. rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
  423. if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
  424. rt_rq->rt_throttled = 0;
  425. enqueue = 1;
  426. }
  427. if (rt_rq->rt_time || rt_rq->rt_nr_running)
  428. idle = 0;
  429. spin_unlock(&rt_rq->rt_runtime_lock);
  430. } else if (rt_rq->rt_nr_running)
  431. idle = 0;
  432. if (enqueue)
  433. sched_rt_rq_enqueue(rt_rq);
  434. spin_unlock(&rq->lock);
  435. }
  436. return idle;
  437. }
  438. static inline int rt_se_prio(struct sched_rt_entity *rt_se)
  439. {
  440. #ifdef CONFIG_RT_GROUP_SCHED
  441. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  442. if (rt_rq)
  443. return rt_rq->highest_prio.curr;
  444. #endif
  445. return rt_task_of(rt_se)->prio;
  446. }
  447. static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
  448. {
  449. u64 runtime = sched_rt_runtime(rt_rq);
  450. if (rt_rq->rt_throttled)
  451. return rt_rq_throttled(rt_rq);
  452. if (sched_rt_runtime(rt_rq) >= sched_rt_period(rt_rq))
  453. return 0;
  454. balance_runtime(rt_rq);
  455. runtime = sched_rt_runtime(rt_rq);
  456. if (runtime == RUNTIME_INF)
  457. return 0;
  458. if (rt_rq->rt_time > runtime) {
  459. rt_rq->rt_throttled = 1;
  460. if (rt_rq_throttled(rt_rq)) {
  461. sched_rt_rq_dequeue(rt_rq);
  462. return 1;
  463. }
  464. }
  465. return 0;
  466. }
  467. /*
  468. * Update the current task's runtime statistics. Skip current tasks that
  469. * are not in our scheduling class.
  470. */
  471. static void update_curr_rt(struct rq *rq)
  472. {
  473. struct task_struct *curr = rq->curr;
  474. struct sched_rt_entity *rt_se = &curr->rt;
  475. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  476. u64 delta_exec;
  477. if (!task_has_rt_policy(curr))
  478. return;
  479. delta_exec = rq->clock - curr->se.exec_start;
  480. if (unlikely((s64)delta_exec < 0))
  481. delta_exec = 0;
  482. schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
  483. curr->se.sum_exec_runtime += delta_exec;
  484. account_group_exec_runtime(curr, delta_exec);
  485. curr->se.exec_start = rq->clock;
  486. cpuacct_charge(curr, delta_exec);
  487. if (!rt_bandwidth_enabled())
  488. return;
  489. for_each_sched_rt_entity(rt_se) {
  490. rt_rq = rt_rq_of_se(rt_se);
  491. if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
  492. spin_lock(&rt_rq->rt_runtime_lock);
  493. rt_rq->rt_time += delta_exec;
  494. if (sched_rt_runtime_exceeded(rt_rq))
  495. resched_task(curr);
  496. spin_unlock(&rt_rq->rt_runtime_lock);
  497. }
  498. }
  499. }
  500. #if defined CONFIG_SMP
  501. static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu);
  502. static inline int next_prio(struct rq *rq)
  503. {
  504. struct task_struct *next = pick_next_highest_task_rt(rq, rq->cpu);
  505. if (next && rt_prio(next->prio))
  506. return next->prio;
  507. else
  508. return MAX_RT_PRIO;
  509. }
  510. static void
  511. inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  512. {
  513. struct rq *rq = rq_of_rt_rq(rt_rq);
  514. if (prio < prev_prio) {
  515. /*
  516. * If the new task is higher in priority than anything on the
  517. * run-queue, we know that the previous high becomes our
  518. * next-highest.
  519. */
  520. rt_rq->highest_prio.next = prev_prio;
  521. if (rq->online)
  522. cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
  523. } else if (prio == rt_rq->highest_prio.curr)
  524. /*
  525. * If the next task is equal in priority to the highest on
  526. * the run-queue, then we implicitly know that the next highest
  527. * task cannot be any lower than current
  528. */
  529. rt_rq->highest_prio.next = prio;
  530. else if (prio < rt_rq->highest_prio.next)
  531. /*
  532. * Otherwise, we need to recompute next-highest
  533. */
  534. rt_rq->highest_prio.next = next_prio(rq);
  535. }
  536. static void
  537. dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  538. {
  539. struct rq *rq = rq_of_rt_rq(rt_rq);
  540. if (rt_rq->rt_nr_running && (prio <= rt_rq->highest_prio.next))
  541. rt_rq->highest_prio.next = next_prio(rq);
  542. if (rq->online && rt_rq->highest_prio.curr != prev_prio)
  543. cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
  544. }
  545. #else /* CONFIG_SMP */
  546. static inline
  547. void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  548. static inline
  549. void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  550. #endif /* CONFIG_SMP */
  551. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  552. static void
  553. inc_rt_prio(struct rt_rq *rt_rq, int prio)
  554. {
  555. int prev_prio = rt_rq->highest_prio.curr;
  556. if (prio < prev_prio)
  557. rt_rq->highest_prio.curr = prio;
  558. inc_rt_prio_smp(rt_rq, prio, prev_prio);
  559. }
  560. static void
  561. dec_rt_prio(struct rt_rq *rt_rq, int prio)
  562. {
  563. int prev_prio = rt_rq->highest_prio.curr;
  564. if (rt_rq->rt_nr_running) {
  565. WARN_ON(prio < prev_prio);
  566. /*
  567. * This may have been our highest task, and therefore
  568. * we may have some recomputation to do
  569. */
  570. if (prio == prev_prio) {
  571. struct rt_prio_array *array = &rt_rq->active;
  572. rt_rq->highest_prio.curr =
  573. sched_find_first_bit(array->bitmap);
  574. }
  575. } else
  576. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  577. dec_rt_prio_smp(rt_rq, prio, prev_prio);
  578. }
  579. #else
  580. static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
  581. static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
  582. #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
  583. #ifdef CONFIG_RT_GROUP_SCHED
  584. static void
  585. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  586. {
  587. if (rt_se_boosted(rt_se))
  588. rt_rq->rt_nr_boosted++;
  589. if (rt_rq->tg)
  590. start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
  591. }
  592. static void
  593. dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  594. {
  595. if (rt_se_boosted(rt_se))
  596. rt_rq->rt_nr_boosted--;
  597. WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
  598. }
  599. #else /* CONFIG_RT_GROUP_SCHED */
  600. static void
  601. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  602. {
  603. start_rt_bandwidth(&def_rt_bandwidth);
  604. }
  605. static inline
  606. void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
  607. #endif /* CONFIG_RT_GROUP_SCHED */
  608. static inline
  609. void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  610. {
  611. int prio = rt_se_prio(rt_se);
  612. WARN_ON(!rt_prio(prio));
  613. rt_rq->rt_nr_running++;
  614. inc_rt_prio(rt_rq, prio);
  615. inc_rt_migration(rt_se, rt_rq);
  616. inc_rt_group(rt_se, rt_rq);
  617. }
  618. static inline
  619. void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  620. {
  621. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  622. WARN_ON(!rt_rq->rt_nr_running);
  623. rt_rq->rt_nr_running--;
  624. dec_rt_prio(rt_rq, rt_se_prio(rt_se));
  625. dec_rt_migration(rt_se, rt_rq);
  626. dec_rt_group(rt_se, rt_rq);
  627. }
  628. static void __enqueue_rt_entity(struct sched_rt_entity *rt_se)
  629. {
  630. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  631. struct rt_prio_array *array = &rt_rq->active;
  632. struct rt_rq *group_rq = group_rt_rq(rt_se);
  633. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  634. /*
  635. * Don't enqueue the group if its throttled, or when empty.
  636. * The latter is a consequence of the former when a child group
  637. * get throttled and the current group doesn't have any other
  638. * active members.
  639. */
  640. if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
  641. return;
  642. list_add_tail(&rt_se->run_list, queue);
  643. __set_bit(rt_se_prio(rt_se), array->bitmap);
  644. inc_rt_tasks(rt_se, rt_rq);
  645. }
  646. static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
  647. {
  648. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  649. struct rt_prio_array *array = &rt_rq->active;
  650. list_del_init(&rt_se->run_list);
  651. if (list_empty(array->queue + rt_se_prio(rt_se)))
  652. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  653. dec_rt_tasks(rt_se, rt_rq);
  654. }
  655. /*
  656. * Because the prio of an upper entry depends on the lower
  657. * entries, we must remove entries top - down.
  658. */
  659. static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
  660. {
  661. struct sched_rt_entity *back = NULL;
  662. for_each_sched_rt_entity(rt_se) {
  663. rt_se->back = back;
  664. back = rt_se;
  665. }
  666. for (rt_se = back; rt_se; rt_se = rt_se->back) {
  667. if (on_rt_rq(rt_se))
  668. __dequeue_rt_entity(rt_se);
  669. }
  670. }
  671. static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
  672. {
  673. dequeue_rt_stack(rt_se);
  674. for_each_sched_rt_entity(rt_se)
  675. __enqueue_rt_entity(rt_se);
  676. }
  677. static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
  678. {
  679. dequeue_rt_stack(rt_se);
  680. for_each_sched_rt_entity(rt_se) {
  681. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  682. if (rt_rq && rt_rq->rt_nr_running)
  683. __enqueue_rt_entity(rt_se);
  684. }
  685. }
  686. /*
  687. * Adding/removing a task to/from a priority array:
  688. */
  689. static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
  690. {
  691. struct sched_rt_entity *rt_se = &p->rt;
  692. if (wakeup)
  693. rt_se->timeout = 0;
  694. enqueue_rt_entity(rt_se);
  695. if (!task_current(rq, p) && p->rt.nr_cpus_allowed > 1)
  696. enqueue_pushable_task(rq, p);
  697. inc_cpu_load(rq, p->se.load.weight);
  698. }
  699. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
  700. {
  701. struct sched_rt_entity *rt_se = &p->rt;
  702. update_curr_rt(rq);
  703. dequeue_rt_entity(rt_se);
  704. dequeue_pushable_task(rq, p);
  705. dec_cpu_load(rq, p->se.load.weight);
  706. }
  707. /*
  708. * Put task to the end of the run list without the overhead of dequeue
  709. * followed by enqueue.
  710. */
  711. static void
  712. requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
  713. {
  714. if (on_rt_rq(rt_se)) {
  715. struct rt_prio_array *array = &rt_rq->active;
  716. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  717. if (head)
  718. list_move(&rt_se->run_list, queue);
  719. else
  720. list_move_tail(&rt_se->run_list, queue);
  721. }
  722. }
  723. static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
  724. {
  725. struct sched_rt_entity *rt_se = &p->rt;
  726. struct rt_rq *rt_rq;
  727. for_each_sched_rt_entity(rt_se) {
  728. rt_rq = rt_rq_of_se(rt_se);
  729. requeue_rt_entity(rt_rq, rt_se, head);
  730. }
  731. }
  732. static void yield_task_rt(struct rq *rq)
  733. {
  734. requeue_task_rt(rq, rq->curr, 0);
  735. }
  736. #ifdef CONFIG_SMP
  737. static int find_lowest_rq(struct task_struct *task);
  738. static int select_task_rq_rt(struct task_struct *p, int sync)
  739. {
  740. struct rq *rq = task_rq(p);
  741. /*
  742. * If the current task is an RT task, then
  743. * try to see if we can wake this RT task up on another
  744. * runqueue. Otherwise simply start this RT task
  745. * on its current runqueue.
  746. *
  747. * We want to avoid overloading runqueues. Even if
  748. * the RT task is of higher priority than the current RT task.
  749. * RT tasks behave differently than other tasks. If
  750. * one gets preempted, we try to push it off to another queue.
  751. * So trying to keep a preempting RT task on the same
  752. * cache hot CPU will force the running RT task to
  753. * a cold CPU. So we waste all the cache for the lower
  754. * RT task in hopes of saving some of a RT task
  755. * that is just being woken and probably will have
  756. * cold cache anyway.
  757. */
  758. if (unlikely(rt_task(rq->curr)) &&
  759. (p->rt.nr_cpus_allowed > 1)) {
  760. int cpu = find_lowest_rq(p);
  761. return (cpu == -1) ? task_cpu(p) : cpu;
  762. }
  763. /*
  764. * Otherwise, just let it ride on the affined RQ and the
  765. * post-schedule router will push the preempted task away
  766. */
  767. return task_cpu(p);
  768. }
  769. static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  770. {
  771. if (rq->curr->rt.nr_cpus_allowed == 1)
  772. return;
  773. if (p->rt.nr_cpus_allowed != 1
  774. && cpupri_find(&rq->rd->cpupri, p, NULL))
  775. return;
  776. if (!cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  777. return;
  778. /*
  779. * There appears to be other cpus that can accept
  780. * current and none to run 'p', so lets reschedule
  781. * to try and push current away:
  782. */
  783. requeue_task_rt(rq, p, 1);
  784. resched_task(rq->curr);
  785. }
  786. #endif /* CONFIG_SMP */
  787. /*
  788. * Preempt the current task with a newly woken task if needed:
  789. */
  790. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int sync)
  791. {
  792. if (p->prio < rq->curr->prio) {
  793. resched_task(rq->curr);
  794. return;
  795. }
  796. #ifdef CONFIG_SMP
  797. /*
  798. * If:
  799. *
  800. * - the newly woken task is of equal priority to the current task
  801. * - the newly woken task is non-migratable while current is migratable
  802. * - current will be preempted on the next reschedule
  803. *
  804. * we should check to see if current can readily move to a different
  805. * cpu. If so, we will reschedule to allow the push logic to try
  806. * to move current somewhere else, making room for our non-migratable
  807. * task.
  808. */
  809. if (p->prio == rq->curr->prio && !need_resched())
  810. check_preempt_equal_prio(rq, p);
  811. #endif
  812. }
  813. static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
  814. struct rt_rq *rt_rq)
  815. {
  816. struct rt_prio_array *array = &rt_rq->active;
  817. struct sched_rt_entity *next = NULL;
  818. struct list_head *queue;
  819. int idx;
  820. idx = sched_find_first_bit(array->bitmap);
  821. BUG_ON(idx >= MAX_RT_PRIO);
  822. queue = array->queue + idx;
  823. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  824. return next;
  825. }
  826. static struct task_struct *_pick_next_task_rt(struct rq *rq)
  827. {
  828. struct sched_rt_entity *rt_se;
  829. struct task_struct *p;
  830. struct rt_rq *rt_rq;
  831. rt_rq = &rq->rt;
  832. if (unlikely(!rt_rq->rt_nr_running))
  833. return NULL;
  834. if (rt_rq_throttled(rt_rq))
  835. return NULL;
  836. do {
  837. rt_se = pick_next_rt_entity(rq, rt_rq);
  838. BUG_ON(!rt_se);
  839. rt_rq = group_rt_rq(rt_se);
  840. } while (rt_rq);
  841. p = rt_task_of(rt_se);
  842. p->se.exec_start = rq->clock;
  843. return p;
  844. }
  845. static struct task_struct *pick_next_task_rt(struct rq *rq)
  846. {
  847. struct task_struct *p = _pick_next_task_rt(rq);
  848. /* The running task is never eligible for pushing */
  849. if (p)
  850. dequeue_pushable_task(rq, p);
  851. return p;
  852. }
  853. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  854. {
  855. update_curr_rt(rq);
  856. p->se.exec_start = 0;
  857. /*
  858. * The previous task needs to be made eligible for pushing
  859. * if it is still active
  860. */
  861. if (p->se.on_rq && p->rt.nr_cpus_allowed > 1)
  862. enqueue_pushable_task(rq, p);
  863. }
  864. #ifdef CONFIG_SMP
  865. /* Only try algorithms three times */
  866. #define RT_MAX_TRIES 3
  867. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
  868. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  869. {
  870. if (!task_running(rq, p) &&
  871. (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) &&
  872. (p->rt.nr_cpus_allowed > 1))
  873. return 1;
  874. return 0;
  875. }
  876. /* Return the second highest RT task, NULL otherwise */
  877. static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
  878. {
  879. struct task_struct *next = NULL;
  880. struct sched_rt_entity *rt_se;
  881. struct rt_prio_array *array;
  882. struct rt_rq *rt_rq;
  883. int idx;
  884. for_each_leaf_rt_rq(rt_rq, rq) {
  885. array = &rt_rq->active;
  886. idx = sched_find_first_bit(array->bitmap);
  887. next_idx:
  888. if (idx >= MAX_RT_PRIO)
  889. continue;
  890. if (next && next->prio < idx)
  891. continue;
  892. list_for_each_entry(rt_se, array->queue + idx, run_list) {
  893. struct task_struct *p = rt_task_of(rt_se);
  894. if (pick_rt_task(rq, p, cpu)) {
  895. next = p;
  896. break;
  897. }
  898. }
  899. if (!next) {
  900. idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  901. goto next_idx;
  902. }
  903. }
  904. return next;
  905. }
  906. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
  907. static inline int pick_optimal_cpu(int this_cpu,
  908. const struct cpumask *mask)
  909. {
  910. int first;
  911. /* "this_cpu" is cheaper to preempt than a remote processor */
  912. if ((this_cpu != -1) && cpumask_test_cpu(this_cpu, mask))
  913. return this_cpu;
  914. first = cpumask_first(mask);
  915. if (first < nr_cpu_ids)
  916. return first;
  917. return -1;
  918. }
  919. static int find_lowest_rq(struct task_struct *task)
  920. {
  921. struct sched_domain *sd;
  922. struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask);
  923. int this_cpu = smp_processor_id();
  924. int cpu = task_cpu(task);
  925. cpumask_var_t domain_mask;
  926. if (task->rt.nr_cpus_allowed == 1)
  927. return -1; /* No other targets possible */
  928. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  929. return -1; /* No targets found */
  930. /*
  931. * Only consider CPUs that are usable for migration.
  932. * I guess we might want to change cpupri_find() to ignore those
  933. * in the first place.
  934. */
  935. cpumask_and(lowest_mask, lowest_mask, cpu_active_mask);
  936. /*
  937. * At this point we have built a mask of cpus representing the
  938. * lowest priority tasks in the system. Now we want to elect
  939. * the best one based on our affinity and topology.
  940. *
  941. * We prioritize the last cpu that the task executed on since
  942. * it is most likely cache-hot in that location.
  943. */
  944. if (cpumask_test_cpu(cpu, lowest_mask))
  945. return cpu;
  946. /*
  947. * Otherwise, we consult the sched_domains span maps to figure
  948. * out which cpu is logically closest to our hot cache data.
  949. */
  950. if (this_cpu == cpu)
  951. this_cpu = -1; /* Skip this_cpu opt if the same */
  952. if (alloc_cpumask_var(&domain_mask, GFP_ATOMIC)) {
  953. for_each_domain(cpu, sd) {
  954. if (sd->flags & SD_WAKE_AFFINE) {
  955. int best_cpu;
  956. cpumask_and(domain_mask,
  957. sched_domain_span(sd),
  958. lowest_mask);
  959. best_cpu = pick_optimal_cpu(this_cpu,
  960. domain_mask);
  961. if (best_cpu != -1) {
  962. free_cpumask_var(domain_mask);
  963. return best_cpu;
  964. }
  965. }
  966. }
  967. free_cpumask_var(domain_mask);
  968. }
  969. /*
  970. * And finally, if there were no matches within the domains
  971. * just give the caller *something* to work with from the compatible
  972. * locations.
  973. */
  974. return pick_optimal_cpu(this_cpu, lowest_mask);
  975. }
  976. /* Will lock the rq it finds */
  977. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  978. {
  979. struct rq *lowest_rq = NULL;
  980. int tries;
  981. int cpu;
  982. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  983. cpu = find_lowest_rq(task);
  984. if ((cpu == -1) || (cpu == rq->cpu))
  985. break;
  986. lowest_rq = cpu_rq(cpu);
  987. /* if the prio of this runqueue changed, try again */
  988. if (double_lock_balance(rq, lowest_rq)) {
  989. /*
  990. * We had to unlock the run queue. In
  991. * the mean time, task could have
  992. * migrated already or had its affinity changed.
  993. * Also make sure that it wasn't scheduled on its rq.
  994. */
  995. if (unlikely(task_rq(task) != rq ||
  996. !cpumask_test_cpu(lowest_rq->cpu,
  997. &task->cpus_allowed) ||
  998. task_running(rq, task) ||
  999. !task->se.on_rq)) {
  1000. spin_unlock(&lowest_rq->lock);
  1001. lowest_rq = NULL;
  1002. break;
  1003. }
  1004. }
  1005. /* If this rq is still suitable use it. */
  1006. if (lowest_rq->rt.highest_prio.curr > task->prio)
  1007. break;
  1008. /* try again */
  1009. double_unlock_balance(rq, lowest_rq);
  1010. lowest_rq = NULL;
  1011. }
  1012. return lowest_rq;
  1013. }
  1014. static inline int has_pushable_tasks(struct rq *rq)
  1015. {
  1016. return !plist_head_empty(&rq->rt.pushable_tasks);
  1017. }
  1018. static struct task_struct *pick_next_pushable_task(struct rq *rq)
  1019. {
  1020. struct task_struct *p;
  1021. if (!has_pushable_tasks(rq))
  1022. return NULL;
  1023. p = plist_first_entry(&rq->rt.pushable_tasks,
  1024. struct task_struct, pushable_tasks);
  1025. BUG_ON(rq->cpu != task_cpu(p));
  1026. BUG_ON(task_current(rq, p));
  1027. BUG_ON(p->rt.nr_cpus_allowed <= 1);
  1028. BUG_ON(!p->se.on_rq);
  1029. BUG_ON(!rt_task(p));
  1030. return p;
  1031. }
  1032. /*
  1033. * If the current CPU has more than one RT task, see if the non
  1034. * running task can migrate over to a CPU that is running a task
  1035. * of lesser priority.
  1036. */
  1037. static int push_rt_task(struct rq *rq)
  1038. {
  1039. struct task_struct *next_task;
  1040. struct rq *lowest_rq;
  1041. if (!rq->rt.overloaded)
  1042. return 0;
  1043. next_task = pick_next_pushable_task(rq);
  1044. if (!next_task)
  1045. return 0;
  1046. retry:
  1047. if (unlikely(next_task == rq->curr)) {
  1048. WARN_ON(1);
  1049. return 0;
  1050. }
  1051. /*
  1052. * It's possible that the next_task slipped in of
  1053. * higher priority than current. If that's the case
  1054. * just reschedule current.
  1055. */
  1056. if (unlikely(next_task->prio < rq->curr->prio)) {
  1057. resched_task(rq->curr);
  1058. return 0;
  1059. }
  1060. /* We might release rq lock */
  1061. get_task_struct(next_task);
  1062. /* find_lock_lowest_rq locks the rq if found */
  1063. lowest_rq = find_lock_lowest_rq(next_task, rq);
  1064. if (!lowest_rq) {
  1065. struct task_struct *task;
  1066. /*
  1067. * find lock_lowest_rq releases rq->lock
  1068. * so it is possible that next_task has migrated.
  1069. *
  1070. * We need to make sure that the task is still on the same
  1071. * run-queue and is also still the next task eligible for
  1072. * pushing.
  1073. */
  1074. task = pick_next_pushable_task(rq);
  1075. if (task_cpu(next_task) == rq->cpu && task == next_task) {
  1076. /*
  1077. * If we get here, the task hasnt moved at all, but
  1078. * it has failed to push. We will not try again,
  1079. * since the other cpus will pull from us when they
  1080. * are ready.
  1081. */
  1082. dequeue_pushable_task(rq, next_task);
  1083. goto out;
  1084. }
  1085. if (!task)
  1086. /* No more tasks, just exit */
  1087. goto out;
  1088. /*
  1089. * Something has shifted, try again.
  1090. */
  1091. put_task_struct(next_task);
  1092. next_task = task;
  1093. goto retry;
  1094. }
  1095. deactivate_task(rq, next_task, 0);
  1096. set_task_cpu(next_task, lowest_rq->cpu);
  1097. activate_task(lowest_rq, next_task, 0);
  1098. resched_task(lowest_rq->curr);
  1099. double_unlock_balance(rq, lowest_rq);
  1100. out:
  1101. put_task_struct(next_task);
  1102. return 1;
  1103. }
  1104. static void push_rt_tasks(struct rq *rq)
  1105. {
  1106. /* push_rt_task will return true if it moved an RT */
  1107. while (push_rt_task(rq))
  1108. ;
  1109. }
  1110. static int pull_rt_task(struct rq *this_rq)
  1111. {
  1112. int this_cpu = this_rq->cpu, ret = 0, cpu;
  1113. struct task_struct *p;
  1114. struct rq *src_rq;
  1115. if (likely(!rt_overloaded(this_rq)))
  1116. return 0;
  1117. for_each_cpu(cpu, this_rq->rd->rto_mask) {
  1118. if (this_cpu == cpu)
  1119. continue;
  1120. src_rq = cpu_rq(cpu);
  1121. /*
  1122. * Don't bother taking the src_rq->lock if the next highest
  1123. * task is known to be lower-priority than our current task.
  1124. * This may look racy, but if this value is about to go
  1125. * logically higher, the src_rq will push this task away.
  1126. * And if its going logically lower, we do not care
  1127. */
  1128. if (src_rq->rt.highest_prio.next >=
  1129. this_rq->rt.highest_prio.curr)
  1130. continue;
  1131. /*
  1132. * We can potentially drop this_rq's lock in
  1133. * double_lock_balance, and another CPU could
  1134. * alter this_rq
  1135. */
  1136. double_lock_balance(this_rq, src_rq);
  1137. /*
  1138. * Are there still pullable RT tasks?
  1139. */
  1140. if (src_rq->rt.rt_nr_running <= 1)
  1141. goto skip;
  1142. p = pick_next_highest_task_rt(src_rq, this_cpu);
  1143. /*
  1144. * Do we have an RT task that preempts
  1145. * the to-be-scheduled task?
  1146. */
  1147. if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
  1148. WARN_ON(p == src_rq->curr);
  1149. WARN_ON(!p->se.on_rq);
  1150. /*
  1151. * There's a chance that p is higher in priority
  1152. * than what's currently running on its cpu.
  1153. * This is just that p is wakeing up and hasn't
  1154. * had a chance to schedule. We only pull
  1155. * p if it is lower in priority than the
  1156. * current task on the run queue
  1157. */
  1158. if (p->prio < src_rq->curr->prio)
  1159. goto skip;
  1160. ret = 1;
  1161. deactivate_task(src_rq, p, 0);
  1162. set_task_cpu(p, this_cpu);
  1163. activate_task(this_rq, p, 0);
  1164. /*
  1165. * We continue with the search, just in
  1166. * case there's an even higher prio task
  1167. * in another runqueue. (low likelyhood
  1168. * but possible)
  1169. */
  1170. }
  1171. skip:
  1172. double_unlock_balance(this_rq, src_rq);
  1173. }
  1174. return ret;
  1175. }
  1176. static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
  1177. {
  1178. /* Try to pull RT tasks here if we lower this rq's prio */
  1179. if (unlikely(rt_task(prev)) && rq->rt.highest_prio.curr > prev->prio)
  1180. pull_rt_task(rq);
  1181. }
  1182. /*
  1183. * assumes rq->lock is held
  1184. */
  1185. static int needs_post_schedule_rt(struct rq *rq)
  1186. {
  1187. return has_pushable_tasks(rq);
  1188. }
  1189. static void post_schedule_rt(struct rq *rq)
  1190. {
  1191. /*
  1192. * This is only called if needs_post_schedule_rt() indicates that
  1193. * we need to push tasks away
  1194. */
  1195. spin_lock_irq(&rq->lock);
  1196. push_rt_tasks(rq);
  1197. spin_unlock_irq(&rq->lock);
  1198. }
  1199. /*
  1200. * If we are not running and we are not going to reschedule soon, we should
  1201. * try to push tasks away now
  1202. */
  1203. static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
  1204. {
  1205. if (!task_running(rq, p) &&
  1206. !test_tsk_need_resched(rq->curr) &&
  1207. has_pushable_tasks(rq) &&
  1208. p->rt.nr_cpus_allowed > 1)
  1209. push_rt_tasks(rq);
  1210. }
  1211. static unsigned long
  1212. load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1213. unsigned long max_load_move,
  1214. struct sched_domain *sd, enum cpu_idle_type idle,
  1215. int *all_pinned, int *this_best_prio)
  1216. {
  1217. /* don't touch RT tasks */
  1218. return 0;
  1219. }
  1220. static int
  1221. move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1222. struct sched_domain *sd, enum cpu_idle_type idle)
  1223. {
  1224. /* don't touch RT tasks */
  1225. return 0;
  1226. }
  1227. static void set_cpus_allowed_rt(struct task_struct *p,
  1228. const struct cpumask *new_mask)
  1229. {
  1230. int weight = cpumask_weight(new_mask);
  1231. BUG_ON(!rt_task(p));
  1232. /*
  1233. * Update the migration status of the RQ if we have an RT task
  1234. * which is running AND changing its weight value.
  1235. */
  1236. if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
  1237. struct rq *rq = task_rq(p);
  1238. if (!task_current(rq, p)) {
  1239. /*
  1240. * Make sure we dequeue this task from the pushable list
  1241. * before going further. It will either remain off of
  1242. * the list because we are no longer pushable, or it
  1243. * will be requeued.
  1244. */
  1245. if (p->rt.nr_cpus_allowed > 1)
  1246. dequeue_pushable_task(rq, p);
  1247. /*
  1248. * Requeue if our weight is changing and still > 1
  1249. */
  1250. if (weight > 1)
  1251. enqueue_pushable_task(rq, p);
  1252. }
  1253. if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
  1254. rq->rt.rt_nr_migratory++;
  1255. } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
  1256. BUG_ON(!rq->rt.rt_nr_migratory);
  1257. rq->rt.rt_nr_migratory--;
  1258. }
  1259. update_rt_migration(&rq->rt);
  1260. }
  1261. cpumask_copy(&p->cpus_allowed, new_mask);
  1262. p->rt.nr_cpus_allowed = weight;
  1263. }
  1264. /* Assumes rq->lock is held */
  1265. static void rq_online_rt(struct rq *rq)
  1266. {
  1267. if (rq->rt.overloaded)
  1268. rt_set_overload(rq);
  1269. __enable_runtime(rq);
  1270. cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
  1271. }
  1272. /* Assumes rq->lock is held */
  1273. static void rq_offline_rt(struct rq *rq)
  1274. {
  1275. if (rq->rt.overloaded)
  1276. rt_clear_overload(rq);
  1277. __disable_runtime(rq);
  1278. cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
  1279. }
  1280. /*
  1281. * When switch from the rt queue, we bring ourselves to a position
  1282. * that we might want to pull RT tasks from other runqueues.
  1283. */
  1284. static void switched_from_rt(struct rq *rq, struct task_struct *p,
  1285. int running)
  1286. {
  1287. /*
  1288. * If there are other RT tasks then we will reschedule
  1289. * and the scheduling of the other RT tasks will handle
  1290. * the balancing. But if we are the last RT task
  1291. * we may need to handle the pulling of RT tasks
  1292. * now.
  1293. */
  1294. if (!rq->rt.rt_nr_running)
  1295. pull_rt_task(rq);
  1296. }
  1297. static inline void init_sched_rt_class(void)
  1298. {
  1299. unsigned int i;
  1300. for_each_possible_cpu(i)
  1301. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
  1302. GFP_KERNEL, cpu_to_node(i));
  1303. }
  1304. #endif /* CONFIG_SMP */
  1305. /*
  1306. * When switching a task to RT, we may overload the runqueue
  1307. * with RT tasks. In this case we try to push them off to
  1308. * other runqueues.
  1309. */
  1310. static void switched_to_rt(struct rq *rq, struct task_struct *p,
  1311. int running)
  1312. {
  1313. int check_resched = 1;
  1314. /*
  1315. * If we are already running, then there's nothing
  1316. * that needs to be done. But if we are not running
  1317. * we may need to preempt the current running task.
  1318. * If that current running task is also an RT task
  1319. * then see if we can move to another run queue.
  1320. */
  1321. if (!running) {
  1322. #ifdef CONFIG_SMP
  1323. if (rq->rt.overloaded && push_rt_task(rq) &&
  1324. /* Don't resched if we changed runqueues */
  1325. rq != task_rq(p))
  1326. check_resched = 0;
  1327. #endif /* CONFIG_SMP */
  1328. if (check_resched && p->prio < rq->curr->prio)
  1329. resched_task(rq->curr);
  1330. }
  1331. }
  1332. /*
  1333. * Priority of the task has changed. This may cause
  1334. * us to initiate a push or pull.
  1335. */
  1336. static void prio_changed_rt(struct rq *rq, struct task_struct *p,
  1337. int oldprio, int running)
  1338. {
  1339. if (running) {
  1340. #ifdef CONFIG_SMP
  1341. /*
  1342. * If our priority decreases while running, we
  1343. * may need to pull tasks to this runqueue.
  1344. */
  1345. if (oldprio < p->prio)
  1346. pull_rt_task(rq);
  1347. /*
  1348. * If there's a higher priority task waiting to run
  1349. * then reschedule. Note, the above pull_rt_task
  1350. * can release the rq lock and p could migrate.
  1351. * Only reschedule if p is still on the same runqueue.
  1352. */
  1353. if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
  1354. resched_task(p);
  1355. #else
  1356. /* For UP simply resched on drop of prio */
  1357. if (oldprio < p->prio)
  1358. resched_task(p);
  1359. #endif /* CONFIG_SMP */
  1360. } else {
  1361. /*
  1362. * This task is not running, but if it is
  1363. * greater than the current running task
  1364. * then reschedule.
  1365. */
  1366. if (p->prio < rq->curr->prio)
  1367. resched_task(rq->curr);
  1368. }
  1369. }
  1370. static void watchdog(struct rq *rq, struct task_struct *p)
  1371. {
  1372. unsigned long soft, hard;
  1373. if (!p->signal)
  1374. return;
  1375. soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
  1376. hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
  1377. if (soft != RLIM_INFINITY) {
  1378. unsigned long next;
  1379. p->rt.timeout++;
  1380. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  1381. if (p->rt.timeout > next)
  1382. p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
  1383. }
  1384. }
  1385. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  1386. {
  1387. update_curr_rt(rq);
  1388. watchdog(rq, p);
  1389. /*
  1390. * RR tasks need a special form of timeslice management.
  1391. * FIFO tasks have no timeslices.
  1392. */
  1393. if (p->policy != SCHED_RR)
  1394. return;
  1395. if (--p->rt.time_slice)
  1396. return;
  1397. p->rt.time_slice = DEF_TIMESLICE;
  1398. /*
  1399. * Requeue to the end of queue if we are not the only element
  1400. * on the queue:
  1401. */
  1402. if (p->rt.run_list.prev != p->rt.run_list.next) {
  1403. requeue_task_rt(rq, p, 0);
  1404. set_tsk_need_resched(p);
  1405. }
  1406. }
  1407. static void set_curr_task_rt(struct rq *rq)
  1408. {
  1409. struct task_struct *p = rq->curr;
  1410. p->se.exec_start = rq->clock;
  1411. /* The running task is never eligible for pushing */
  1412. dequeue_pushable_task(rq, p);
  1413. }
  1414. static const struct sched_class rt_sched_class = {
  1415. .next = &fair_sched_class,
  1416. .enqueue_task = enqueue_task_rt,
  1417. .dequeue_task = dequeue_task_rt,
  1418. .yield_task = yield_task_rt,
  1419. .check_preempt_curr = check_preempt_curr_rt,
  1420. .pick_next_task = pick_next_task_rt,
  1421. .put_prev_task = put_prev_task_rt,
  1422. #ifdef CONFIG_SMP
  1423. .select_task_rq = select_task_rq_rt,
  1424. .load_balance = load_balance_rt,
  1425. .move_one_task = move_one_task_rt,
  1426. .set_cpus_allowed = set_cpus_allowed_rt,
  1427. .rq_online = rq_online_rt,
  1428. .rq_offline = rq_offline_rt,
  1429. .pre_schedule = pre_schedule_rt,
  1430. .needs_post_schedule = needs_post_schedule_rt,
  1431. .post_schedule = post_schedule_rt,
  1432. .task_wake_up = task_wake_up_rt,
  1433. .switched_from = switched_from_rt,
  1434. #endif
  1435. .set_curr_task = set_curr_task_rt,
  1436. .task_tick = task_tick_rt,
  1437. .prio_changed = prio_changed_rt,
  1438. .switched_to = switched_to_rt,
  1439. };
  1440. #ifdef CONFIG_SCHED_DEBUG
  1441. extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
  1442. static void print_rt_stats(struct seq_file *m, int cpu)
  1443. {
  1444. struct rt_rq *rt_rq;
  1445. rcu_read_lock();
  1446. for_each_leaf_rt_rq(rt_rq, cpu_rq(cpu))
  1447. print_rt_rq(m, cpu, rt_rq);
  1448. rcu_read_unlock();
  1449. }
  1450. #endif /* CONFIG_SCHED_DEBUG */