sched_rt.c 42 KB

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