sched_rt.c 42 KB

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