rt.c 48 KB

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