rt.c 47 KB

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