rt.c 48 KB

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