rt.c 47 KB

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