rt.c 48 KB

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