sched_rt.c 39 KB

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