rt.c 47 KB

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