rt.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  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. rt_rq->rt_throttled = 0;
  553. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  554. raw_spin_unlock(&rt_b->rt_runtime_lock);
  555. }
  556. }
  557. static void disable_runtime(struct rq *rq)
  558. {
  559. unsigned long flags;
  560. raw_spin_lock_irqsave(&rq->lock, flags);
  561. __disable_runtime(rq);
  562. raw_spin_unlock_irqrestore(&rq->lock, flags);
  563. }
  564. static void __enable_runtime(struct rq *rq)
  565. {
  566. rt_rq_iter_t iter;
  567. struct rt_rq *rt_rq;
  568. if (unlikely(!scheduler_running))
  569. return;
  570. /*
  571. * Reset each runqueue's bandwidth settings
  572. */
  573. for_each_rt_rq(rt_rq, iter, rq) {
  574. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  575. raw_spin_lock(&rt_b->rt_runtime_lock);
  576. raw_spin_lock(&rt_rq->rt_runtime_lock);
  577. rt_rq->rt_runtime = rt_b->rt_runtime;
  578. rt_rq->rt_time = 0;
  579. rt_rq->rt_throttled = 0;
  580. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  581. raw_spin_unlock(&rt_b->rt_runtime_lock);
  582. }
  583. }
  584. static void enable_runtime(struct rq *rq)
  585. {
  586. unsigned long flags;
  587. raw_spin_lock_irqsave(&rq->lock, flags);
  588. __enable_runtime(rq);
  589. raw_spin_unlock_irqrestore(&rq->lock, flags);
  590. }
  591. int update_runtime(struct notifier_block *nfb, unsigned long action, void *hcpu)
  592. {
  593. int cpu = (int)(long)hcpu;
  594. switch (action) {
  595. case CPU_DOWN_PREPARE:
  596. case CPU_DOWN_PREPARE_FROZEN:
  597. disable_runtime(cpu_rq(cpu));
  598. return NOTIFY_OK;
  599. case CPU_DOWN_FAILED:
  600. case CPU_DOWN_FAILED_FROZEN:
  601. case CPU_ONLINE:
  602. case CPU_ONLINE_FROZEN:
  603. enable_runtime(cpu_rq(cpu));
  604. return NOTIFY_OK;
  605. default:
  606. return NOTIFY_DONE;
  607. }
  608. }
  609. static int balance_runtime(struct rt_rq *rt_rq)
  610. {
  611. int more = 0;
  612. if (!sched_feat(RT_RUNTIME_SHARE))
  613. return more;
  614. if (rt_rq->rt_time > rt_rq->rt_runtime) {
  615. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  616. more = do_balance_runtime(rt_rq);
  617. raw_spin_lock(&rt_rq->rt_runtime_lock);
  618. }
  619. return more;
  620. }
  621. #else /* !CONFIG_SMP */
  622. static inline int balance_runtime(struct rt_rq *rt_rq)
  623. {
  624. return 0;
  625. }
  626. #endif /* CONFIG_SMP */
  627. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
  628. {
  629. int i, idle = 1, throttled = 0;
  630. const struct cpumask *span;
  631. span = sched_rt_period_mask();
  632. #ifdef CONFIG_RT_GROUP_SCHED
  633. /*
  634. * FIXME: isolated CPUs should really leave the root task group,
  635. * whether they are isolcpus or were isolated via cpusets, lest
  636. * the timer run on a CPU which does not service all runqueues,
  637. * potentially leaving other CPUs indefinitely throttled. If
  638. * isolation is really required, the user will turn the throttle
  639. * off to kill the perturbations it causes anyway. Meanwhile,
  640. * this maintains functionality for boot and/or troubleshooting.
  641. */
  642. if (rt_b == &root_task_group.rt_bandwidth)
  643. span = cpu_online_mask;
  644. #endif
  645. for_each_cpu(i, span) {
  646. int enqueue = 0;
  647. struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
  648. struct rq *rq = rq_of_rt_rq(rt_rq);
  649. raw_spin_lock(&rq->lock);
  650. if (rt_rq->rt_time) {
  651. u64 runtime;
  652. raw_spin_lock(&rt_rq->rt_runtime_lock);
  653. if (rt_rq->rt_throttled)
  654. balance_runtime(rt_rq);
  655. runtime = rt_rq->rt_runtime;
  656. rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
  657. if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
  658. rt_rq->rt_throttled = 0;
  659. enqueue = 1;
  660. /*
  661. * Force a clock update if the CPU was idle,
  662. * lest wakeup -> unthrottle time accumulate.
  663. */
  664. if (rt_rq->rt_nr_running && rq->curr == rq->idle)
  665. rq->skip_clock_update = -1;
  666. }
  667. if (rt_rq->rt_time || rt_rq->rt_nr_running)
  668. idle = 0;
  669. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  670. } else if (rt_rq->rt_nr_running) {
  671. idle = 0;
  672. if (!rt_rq_throttled(rt_rq))
  673. enqueue = 1;
  674. }
  675. if (rt_rq->rt_throttled)
  676. throttled = 1;
  677. if (enqueue)
  678. sched_rt_rq_enqueue(rt_rq);
  679. raw_spin_unlock(&rq->lock);
  680. }
  681. if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
  682. return 1;
  683. return idle;
  684. }
  685. static inline int rt_se_prio(struct sched_rt_entity *rt_se)
  686. {
  687. #ifdef CONFIG_RT_GROUP_SCHED
  688. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  689. if (rt_rq)
  690. return rt_rq->highest_prio.curr;
  691. #endif
  692. return rt_task_of(rt_se)->prio;
  693. }
  694. static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
  695. {
  696. u64 runtime = sched_rt_runtime(rt_rq);
  697. if (rt_rq->rt_throttled)
  698. return rt_rq_throttled(rt_rq);
  699. if (runtime >= sched_rt_period(rt_rq))
  700. return 0;
  701. balance_runtime(rt_rq);
  702. runtime = sched_rt_runtime(rt_rq);
  703. if (runtime == RUNTIME_INF)
  704. return 0;
  705. if (rt_rq->rt_time > runtime) {
  706. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  707. /*
  708. * Don't actually throttle groups that have no runtime assigned
  709. * but accrue some time due to boosting.
  710. */
  711. if (likely(rt_b->rt_runtime)) {
  712. static bool once = false;
  713. rt_rq->rt_throttled = 1;
  714. if (!once) {
  715. once = true;
  716. printk_sched("sched: RT throttling activated\n");
  717. }
  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. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  742. u64 delta_exec;
  743. if (curr->sched_class != &rt_sched_class)
  744. return;
  745. delta_exec = rq->clock_task - curr->se.exec_start;
  746. if (unlikely((s64)delta_exec <= 0))
  747. return;
  748. schedstat_set(curr->se.statistics.exec_max,
  749. max(curr->se.statistics.exec_max, delta_exec));
  750. curr->se.sum_exec_runtime += delta_exec;
  751. account_group_exec_runtime(curr, delta_exec);
  752. curr->se.exec_start = rq->clock_task;
  753. cpuacct_charge(curr, delta_exec);
  754. sched_rt_avg_update(rq, delta_exec);
  755. if (!rt_bandwidth_enabled())
  756. return;
  757. for_each_sched_rt_entity(rt_se) {
  758. rt_rq = rt_rq_of_se(rt_se);
  759. if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
  760. raw_spin_lock(&rt_rq->rt_runtime_lock);
  761. rt_rq->rt_time += delta_exec;
  762. if (sched_rt_runtime_exceeded(rt_rq))
  763. resched_task(curr);
  764. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  765. }
  766. }
  767. }
  768. #if defined CONFIG_SMP
  769. static void
  770. inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  771. {
  772. struct rq *rq = rq_of_rt_rq(rt_rq);
  773. if (rq->online && prio < prev_prio)
  774. cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
  775. }
  776. static void
  777. dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  778. {
  779. struct rq *rq = rq_of_rt_rq(rt_rq);
  780. if (rq->online && rt_rq->highest_prio.curr != prev_prio)
  781. cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
  782. }
  783. #else /* CONFIG_SMP */
  784. static inline
  785. void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  786. static inline
  787. void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  788. #endif /* CONFIG_SMP */
  789. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  790. static void
  791. inc_rt_prio(struct rt_rq *rt_rq, int prio)
  792. {
  793. int prev_prio = rt_rq->highest_prio.curr;
  794. if (prio < prev_prio)
  795. rt_rq->highest_prio.curr = prio;
  796. inc_rt_prio_smp(rt_rq, prio, prev_prio);
  797. }
  798. static void
  799. dec_rt_prio(struct rt_rq *rt_rq, int prio)
  800. {
  801. int prev_prio = rt_rq->highest_prio.curr;
  802. if (rt_rq->rt_nr_running) {
  803. WARN_ON(prio < prev_prio);
  804. /*
  805. * This may have been our highest task, and therefore
  806. * we may have some recomputation to do
  807. */
  808. if (prio == prev_prio) {
  809. struct rt_prio_array *array = &rt_rq->active;
  810. rt_rq->highest_prio.curr =
  811. sched_find_first_bit(array->bitmap);
  812. }
  813. } else
  814. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  815. dec_rt_prio_smp(rt_rq, prio, prev_prio);
  816. }
  817. #else
  818. static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
  819. static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
  820. #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
  821. #ifdef CONFIG_RT_GROUP_SCHED
  822. static void
  823. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  824. {
  825. if (rt_se_boosted(rt_se))
  826. rt_rq->rt_nr_boosted++;
  827. if (rt_rq->tg)
  828. start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
  829. }
  830. static void
  831. dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  832. {
  833. if (rt_se_boosted(rt_se))
  834. rt_rq->rt_nr_boosted--;
  835. WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
  836. }
  837. #else /* CONFIG_RT_GROUP_SCHED */
  838. static void
  839. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  840. {
  841. start_rt_bandwidth(&def_rt_bandwidth);
  842. }
  843. static inline
  844. void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
  845. #endif /* CONFIG_RT_GROUP_SCHED */
  846. static inline
  847. void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  848. {
  849. int prio = rt_se_prio(rt_se);
  850. WARN_ON(!rt_prio(prio));
  851. rt_rq->rt_nr_running++;
  852. inc_rt_prio(rt_rq, prio);
  853. inc_rt_migration(rt_se, rt_rq);
  854. inc_rt_group(rt_se, rt_rq);
  855. }
  856. static inline
  857. void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  858. {
  859. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  860. WARN_ON(!rt_rq->rt_nr_running);
  861. rt_rq->rt_nr_running--;
  862. dec_rt_prio(rt_rq, rt_se_prio(rt_se));
  863. dec_rt_migration(rt_se, rt_rq);
  864. dec_rt_group(rt_se, rt_rq);
  865. }
  866. static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
  867. {
  868. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  869. struct rt_prio_array *array = &rt_rq->active;
  870. struct rt_rq *group_rq = group_rt_rq(rt_se);
  871. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  872. /*
  873. * Don't enqueue the group if its throttled, or when empty.
  874. * The latter is a consequence of the former when a child group
  875. * get throttled and the current group doesn't have any other
  876. * active members.
  877. */
  878. if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
  879. return;
  880. if (!rt_rq->rt_nr_running)
  881. list_add_leaf_rt_rq(rt_rq);
  882. if (head)
  883. list_add(&rt_se->run_list, queue);
  884. else
  885. list_add_tail(&rt_se->run_list, queue);
  886. __set_bit(rt_se_prio(rt_se), array->bitmap);
  887. inc_rt_tasks(rt_se, rt_rq);
  888. }
  889. static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
  890. {
  891. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  892. struct rt_prio_array *array = &rt_rq->active;
  893. list_del_init(&rt_se->run_list);
  894. if (list_empty(array->queue + rt_se_prio(rt_se)))
  895. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  896. dec_rt_tasks(rt_se, rt_rq);
  897. if (!rt_rq->rt_nr_running)
  898. list_del_leaf_rt_rq(rt_rq);
  899. }
  900. /*
  901. * Because the prio of an upper entry depends on the lower
  902. * entries, we must remove entries top - down.
  903. */
  904. static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
  905. {
  906. struct sched_rt_entity *back = NULL;
  907. for_each_sched_rt_entity(rt_se) {
  908. rt_se->back = back;
  909. back = rt_se;
  910. }
  911. for (rt_se = back; rt_se; rt_se = rt_se->back) {
  912. if (on_rt_rq(rt_se))
  913. __dequeue_rt_entity(rt_se);
  914. }
  915. }
  916. static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
  917. {
  918. dequeue_rt_stack(rt_se);
  919. for_each_sched_rt_entity(rt_se)
  920. __enqueue_rt_entity(rt_se, head);
  921. }
  922. static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
  923. {
  924. dequeue_rt_stack(rt_se);
  925. for_each_sched_rt_entity(rt_se) {
  926. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  927. if (rt_rq && rt_rq->rt_nr_running)
  928. __enqueue_rt_entity(rt_se, false);
  929. }
  930. }
  931. /*
  932. * Adding/removing a task to/from a priority array:
  933. */
  934. static void
  935. enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  936. {
  937. struct sched_rt_entity *rt_se = &p->rt;
  938. if (flags & ENQUEUE_WAKEUP)
  939. rt_se->timeout = 0;
  940. enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
  941. if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  942. enqueue_pushable_task(rq, p);
  943. inc_nr_running(rq);
  944. }
  945. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  946. {
  947. struct sched_rt_entity *rt_se = &p->rt;
  948. update_curr_rt(rq);
  949. dequeue_rt_entity(rt_se);
  950. dequeue_pushable_task(rq, p);
  951. dec_nr_running(rq);
  952. }
  953. /*
  954. * Put task to the head or the end of the run list without the overhead of
  955. * dequeue followed by enqueue.
  956. */
  957. static void
  958. requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
  959. {
  960. if (on_rt_rq(rt_se)) {
  961. struct rt_prio_array *array = &rt_rq->active;
  962. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  963. if (head)
  964. list_move(&rt_se->run_list, queue);
  965. else
  966. list_move_tail(&rt_se->run_list, queue);
  967. }
  968. }
  969. static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
  970. {
  971. struct sched_rt_entity *rt_se = &p->rt;
  972. struct rt_rq *rt_rq;
  973. for_each_sched_rt_entity(rt_se) {
  974. rt_rq = rt_rq_of_se(rt_se);
  975. requeue_rt_entity(rt_rq, rt_se, head);
  976. }
  977. }
  978. static void yield_task_rt(struct rq *rq)
  979. {
  980. requeue_task_rt(rq, rq->curr, 0);
  981. }
  982. #ifdef CONFIG_SMP
  983. static int find_lowest_rq(struct task_struct *task);
  984. static int
  985. select_task_rq_rt(struct task_struct *p, int sd_flag, int flags)
  986. {
  987. struct task_struct *curr;
  988. struct rq *rq;
  989. int cpu;
  990. cpu = task_cpu(p);
  991. if (p->nr_cpus_allowed == 1)
  992. goto out;
  993. /* For anything but wake ups, just return the task_cpu */
  994. if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
  995. goto out;
  996. rq = cpu_rq(cpu);
  997. rcu_read_lock();
  998. curr = ACCESS_ONCE(rq->curr); /* unlocked access */
  999. /*
  1000. * If the current task on @p's runqueue is an RT task, then
  1001. * try to see if we can wake this RT task up on another
  1002. * runqueue. Otherwise simply start this RT task
  1003. * on its current runqueue.
  1004. *
  1005. * We want to avoid overloading runqueues. If the woken
  1006. * task is a higher priority, then it will stay on this CPU
  1007. * and the lower prio task should be moved to another CPU.
  1008. * Even though this will probably make the lower prio task
  1009. * lose its cache, we do not want to bounce a higher task
  1010. * around just because it gave up its CPU, perhaps for a
  1011. * lock?
  1012. *
  1013. * For equal prio tasks, we just let the scheduler sort it out.
  1014. *
  1015. * Otherwise, just let it ride on the affined RQ and the
  1016. * post-schedule router will push the preempted task away
  1017. *
  1018. * This test is optimistic, if we get it wrong the load-balancer
  1019. * will have to sort it out.
  1020. */
  1021. if (curr && unlikely(rt_task(curr)) &&
  1022. (curr->nr_cpus_allowed < 2 ||
  1023. curr->prio <= p->prio) &&
  1024. (p->nr_cpus_allowed > 1)) {
  1025. int target = find_lowest_rq(p);
  1026. if (target != -1)
  1027. cpu = target;
  1028. }
  1029. rcu_read_unlock();
  1030. out:
  1031. return cpu;
  1032. }
  1033. static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  1034. {
  1035. if (rq->curr->nr_cpus_allowed == 1)
  1036. return;
  1037. if (p->nr_cpus_allowed != 1
  1038. && cpupri_find(&rq->rd->cpupri, p, NULL))
  1039. return;
  1040. if (!cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  1041. return;
  1042. /*
  1043. * There appears to be other cpus that can accept
  1044. * current and none to run 'p', so lets reschedule
  1045. * to try and push current away:
  1046. */
  1047. requeue_task_rt(rq, p, 1);
  1048. resched_task(rq->curr);
  1049. }
  1050. #endif /* CONFIG_SMP */
  1051. /*
  1052. * Preempt the current task with a newly woken task if needed:
  1053. */
  1054. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
  1055. {
  1056. if (p->prio < rq->curr->prio) {
  1057. resched_task(rq->curr);
  1058. return;
  1059. }
  1060. #ifdef CONFIG_SMP
  1061. /*
  1062. * If:
  1063. *
  1064. * - the newly woken task is of equal priority to the current task
  1065. * - the newly woken task is non-migratable while current is migratable
  1066. * - current will be preempted on the next reschedule
  1067. *
  1068. * we should check to see if current can readily move to a different
  1069. * cpu. If so, we will reschedule to allow the push logic to try
  1070. * to move current somewhere else, making room for our non-migratable
  1071. * task.
  1072. */
  1073. if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
  1074. check_preempt_equal_prio(rq, p);
  1075. #endif
  1076. }
  1077. static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
  1078. struct rt_rq *rt_rq)
  1079. {
  1080. struct rt_prio_array *array = &rt_rq->active;
  1081. struct sched_rt_entity *next = NULL;
  1082. struct list_head *queue;
  1083. int idx;
  1084. idx = sched_find_first_bit(array->bitmap);
  1085. BUG_ON(idx >= MAX_RT_PRIO);
  1086. queue = array->queue + idx;
  1087. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  1088. return next;
  1089. }
  1090. static struct task_struct *_pick_next_task_rt(struct rq *rq)
  1091. {
  1092. struct sched_rt_entity *rt_se;
  1093. struct task_struct *p;
  1094. struct rt_rq *rt_rq;
  1095. rt_rq = &rq->rt;
  1096. if (!rt_rq->rt_nr_running)
  1097. return NULL;
  1098. if (rt_rq_throttled(rt_rq))
  1099. return NULL;
  1100. do {
  1101. rt_se = pick_next_rt_entity(rq, rt_rq);
  1102. BUG_ON(!rt_se);
  1103. rt_rq = group_rt_rq(rt_se);
  1104. } while (rt_rq);
  1105. p = rt_task_of(rt_se);
  1106. p->se.exec_start = rq->clock_task;
  1107. return p;
  1108. }
  1109. static struct task_struct *pick_next_task_rt(struct rq *rq)
  1110. {
  1111. struct task_struct *p = _pick_next_task_rt(rq);
  1112. /* The running task is never eligible for pushing */
  1113. if (p)
  1114. dequeue_pushable_task(rq, p);
  1115. #ifdef CONFIG_SMP
  1116. /*
  1117. * We detect this state here so that we can avoid taking the RQ
  1118. * lock again later if there is no need to push
  1119. */
  1120. rq->post_schedule = has_pushable_tasks(rq);
  1121. #endif
  1122. return p;
  1123. }
  1124. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  1125. {
  1126. update_curr_rt(rq);
  1127. /*
  1128. * The previous task needs to be made eligible for pushing
  1129. * if it is still active
  1130. */
  1131. if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
  1132. enqueue_pushable_task(rq, p);
  1133. }
  1134. #ifdef CONFIG_SMP
  1135. /* Only try algorithms three times */
  1136. #define RT_MAX_TRIES 3
  1137. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  1138. {
  1139. if (!task_running(rq, p) &&
  1140. (cpu < 0 || cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) &&
  1141. (p->nr_cpus_allowed > 1))
  1142. return 1;
  1143. return 0;
  1144. }
  1145. /* Return the second highest RT task, NULL otherwise */
  1146. static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
  1147. {
  1148. struct task_struct *next = NULL;
  1149. struct sched_rt_entity *rt_se;
  1150. struct rt_prio_array *array;
  1151. struct rt_rq *rt_rq;
  1152. int idx;
  1153. for_each_leaf_rt_rq(rt_rq, rq) {
  1154. array = &rt_rq->active;
  1155. idx = sched_find_first_bit(array->bitmap);
  1156. next_idx:
  1157. if (idx >= MAX_RT_PRIO)
  1158. continue;
  1159. if (next && next->prio <= idx)
  1160. continue;
  1161. list_for_each_entry(rt_se, array->queue + idx, run_list) {
  1162. struct task_struct *p;
  1163. if (!rt_entity_is_task(rt_se))
  1164. continue;
  1165. p = rt_task_of(rt_se);
  1166. if (pick_rt_task(rq, p, cpu)) {
  1167. next = p;
  1168. break;
  1169. }
  1170. }
  1171. if (!next) {
  1172. idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
  1173. goto next_idx;
  1174. }
  1175. }
  1176. return next;
  1177. }
  1178. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
  1179. static int find_lowest_rq(struct task_struct *task)
  1180. {
  1181. struct sched_domain *sd;
  1182. struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask);
  1183. int this_cpu = smp_processor_id();
  1184. int cpu = task_cpu(task);
  1185. /* Make sure the mask is initialized first */
  1186. if (unlikely(!lowest_mask))
  1187. return -1;
  1188. if (task->nr_cpus_allowed == 1)
  1189. return -1; /* No other targets possible */
  1190. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  1191. return -1; /* No targets found */
  1192. /*
  1193. * At this point we have built a mask of cpus representing the
  1194. * lowest priority tasks in the system. Now we want to elect
  1195. * the best one based on our affinity and topology.
  1196. *
  1197. * We prioritize the last cpu that the task executed on since
  1198. * it is most likely cache-hot in that location.
  1199. */
  1200. if (cpumask_test_cpu(cpu, lowest_mask))
  1201. return cpu;
  1202. /*
  1203. * Otherwise, we consult the sched_domains span maps to figure
  1204. * out which cpu is logically closest to our hot cache data.
  1205. */
  1206. if (!cpumask_test_cpu(this_cpu, lowest_mask))
  1207. this_cpu = -1; /* Skip this_cpu opt if not among lowest */
  1208. rcu_read_lock();
  1209. for_each_domain(cpu, sd) {
  1210. if (sd->flags & SD_WAKE_AFFINE) {
  1211. int best_cpu;
  1212. /*
  1213. * "this_cpu" is cheaper to preempt than a
  1214. * remote processor.
  1215. */
  1216. if (this_cpu != -1 &&
  1217. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  1218. rcu_read_unlock();
  1219. return this_cpu;
  1220. }
  1221. best_cpu = cpumask_first_and(lowest_mask,
  1222. sched_domain_span(sd));
  1223. if (best_cpu < nr_cpu_ids) {
  1224. rcu_read_unlock();
  1225. return best_cpu;
  1226. }
  1227. }
  1228. }
  1229. rcu_read_unlock();
  1230. /*
  1231. * And finally, if there were no matches within the domains
  1232. * just give the caller *something* to work with from the compatible
  1233. * locations.
  1234. */
  1235. if (this_cpu != -1)
  1236. return this_cpu;
  1237. cpu = cpumask_any(lowest_mask);
  1238. if (cpu < nr_cpu_ids)
  1239. return cpu;
  1240. return -1;
  1241. }
  1242. /* Will lock the rq it finds */
  1243. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  1244. {
  1245. struct rq *lowest_rq = NULL;
  1246. int tries;
  1247. int cpu;
  1248. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  1249. cpu = find_lowest_rq(task);
  1250. if ((cpu == -1) || (cpu == rq->cpu))
  1251. break;
  1252. lowest_rq = cpu_rq(cpu);
  1253. /* if the prio of this runqueue changed, try again */
  1254. if (double_lock_balance(rq, lowest_rq)) {
  1255. /*
  1256. * We had to unlock the run queue. In
  1257. * the mean time, task could have
  1258. * migrated already or had its affinity changed.
  1259. * Also make sure that it wasn't scheduled on its rq.
  1260. */
  1261. if (unlikely(task_rq(task) != rq ||
  1262. !cpumask_test_cpu(lowest_rq->cpu,
  1263. tsk_cpus_allowed(task)) ||
  1264. task_running(rq, task) ||
  1265. !task->on_rq)) {
  1266. double_unlock_balance(rq, lowest_rq);
  1267. lowest_rq = NULL;
  1268. break;
  1269. }
  1270. }
  1271. /* If this rq is still suitable use it. */
  1272. if (lowest_rq->rt.highest_prio.curr > task->prio)
  1273. break;
  1274. /* try again */
  1275. double_unlock_balance(rq, lowest_rq);
  1276. lowest_rq = NULL;
  1277. }
  1278. return lowest_rq;
  1279. }
  1280. static struct task_struct *pick_next_pushable_task(struct rq *rq)
  1281. {
  1282. struct task_struct *p;
  1283. if (!has_pushable_tasks(rq))
  1284. return NULL;
  1285. p = plist_first_entry(&rq->rt.pushable_tasks,
  1286. struct task_struct, pushable_tasks);
  1287. BUG_ON(rq->cpu != task_cpu(p));
  1288. BUG_ON(task_current(rq, p));
  1289. BUG_ON(p->nr_cpus_allowed <= 1);
  1290. BUG_ON(!p->on_rq);
  1291. BUG_ON(!rt_task(p));
  1292. return p;
  1293. }
  1294. /*
  1295. * If the current CPU has more than one RT task, see if the non
  1296. * running task can migrate over to a CPU that is running a task
  1297. * of lesser priority.
  1298. */
  1299. static int push_rt_task(struct rq *rq)
  1300. {
  1301. struct task_struct *next_task;
  1302. struct rq *lowest_rq;
  1303. int ret = 0;
  1304. if (!rq->rt.overloaded)
  1305. return 0;
  1306. next_task = pick_next_pushable_task(rq);
  1307. if (!next_task)
  1308. return 0;
  1309. retry:
  1310. if (unlikely(next_task == rq->curr)) {
  1311. WARN_ON(1);
  1312. return 0;
  1313. }
  1314. /*
  1315. * It's possible that the next_task slipped in of
  1316. * higher priority than current. If that's the case
  1317. * just reschedule current.
  1318. */
  1319. if (unlikely(next_task->prio < rq->curr->prio)) {
  1320. resched_task(rq->curr);
  1321. return 0;
  1322. }
  1323. /* We might release rq lock */
  1324. get_task_struct(next_task);
  1325. /* find_lock_lowest_rq locks the rq if found */
  1326. lowest_rq = find_lock_lowest_rq(next_task, rq);
  1327. if (!lowest_rq) {
  1328. struct task_struct *task;
  1329. /*
  1330. * find_lock_lowest_rq releases rq->lock
  1331. * so it is possible that next_task has migrated.
  1332. *
  1333. * We need to make sure that the task is still on the same
  1334. * run-queue and is also still the next task eligible for
  1335. * pushing.
  1336. */
  1337. task = pick_next_pushable_task(rq);
  1338. if (task_cpu(next_task) == rq->cpu && task == next_task) {
  1339. /*
  1340. * The task hasn't migrated, and is still the next
  1341. * eligible task, but we failed to find a run-queue
  1342. * to push it to. Do not retry in this case, since
  1343. * other cpus will pull from us when ready.
  1344. */
  1345. goto out;
  1346. }
  1347. if (!task)
  1348. /* No more tasks, just exit */
  1349. goto out;
  1350. /*
  1351. * Something has shifted, try again.
  1352. */
  1353. put_task_struct(next_task);
  1354. next_task = task;
  1355. goto retry;
  1356. }
  1357. deactivate_task(rq, next_task, 0);
  1358. set_task_cpu(next_task, lowest_rq->cpu);
  1359. activate_task(lowest_rq, next_task, 0);
  1360. ret = 1;
  1361. resched_task(lowest_rq->curr);
  1362. double_unlock_balance(rq, lowest_rq);
  1363. out:
  1364. put_task_struct(next_task);
  1365. return ret;
  1366. }
  1367. static void push_rt_tasks(struct rq *rq)
  1368. {
  1369. /* push_rt_task will return true if it moved an RT */
  1370. while (push_rt_task(rq))
  1371. ;
  1372. }
  1373. static int pull_rt_task(struct rq *this_rq)
  1374. {
  1375. int this_cpu = this_rq->cpu, ret = 0, cpu;
  1376. struct task_struct *p;
  1377. struct rq *src_rq;
  1378. if (likely(!rt_overloaded(this_rq)))
  1379. return 0;
  1380. for_each_cpu(cpu, this_rq->rd->rto_mask) {
  1381. if (this_cpu == cpu)
  1382. continue;
  1383. src_rq = cpu_rq(cpu);
  1384. /*
  1385. * Don't bother taking the src_rq->lock if the next highest
  1386. * task is known to be lower-priority than our current task.
  1387. * This may look racy, but if this value is about to go
  1388. * logically higher, the src_rq will push this task away.
  1389. * And if its going logically lower, we do not care
  1390. */
  1391. if (src_rq->rt.highest_prio.next >=
  1392. this_rq->rt.highest_prio.curr)
  1393. continue;
  1394. /*
  1395. * We can potentially drop this_rq's lock in
  1396. * double_lock_balance, and another CPU could
  1397. * alter this_rq
  1398. */
  1399. double_lock_balance(this_rq, src_rq);
  1400. /*
  1401. * Are there still pullable RT tasks?
  1402. */
  1403. if (src_rq->rt.rt_nr_running <= 1)
  1404. goto skip;
  1405. p = pick_next_highest_task_rt(src_rq, this_cpu);
  1406. /*
  1407. * Do we have an RT task that preempts
  1408. * the to-be-scheduled task?
  1409. */
  1410. if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
  1411. WARN_ON(p == src_rq->curr);
  1412. WARN_ON(!p->on_rq);
  1413. /*
  1414. * There's a chance that p is higher in priority
  1415. * than what's currently running on its cpu.
  1416. * This is just that p is wakeing up and hasn't
  1417. * had a chance to schedule. We only pull
  1418. * p if it is lower in priority than the
  1419. * current task on the run queue
  1420. */
  1421. if (p->prio < src_rq->curr->prio)
  1422. goto skip;
  1423. ret = 1;
  1424. deactivate_task(src_rq, p, 0);
  1425. set_task_cpu(p, this_cpu);
  1426. activate_task(this_rq, p, 0);
  1427. /*
  1428. * We continue with the search, just in
  1429. * case there's an even higher prio task
  1430. * in another runqueue. (low likelihood
  1431. * but possible)
  1432. */
  1433. }
  1434. skip:
  1435. double_unlock_balance(this_rq, src_rq);
  1436. }
  1437. return ret;
  1438. }
  1439. static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
  1440. {
  1441. /* Try to pull RT tasks here if we lower this rq's prio */
  1442. if (rq->rt.highest_prio.curr > prev->prio)
  1443. pull_rt_task(rq);
  1444. }
  1445. static void post_schedule_rt(struct rq *rq)
  1446. {
  1447. push_rt_tasks(rq);
  1448. }
  1449. /*
  1450. * If we are not running and we are not going to reschedule soon, we should
  1451. * try to push tasks away now
  1452. */
  1453. static void task_woken_rt(struct rq *rq, struct task_struct *p)
  1454. {
  1455. if (!task_running(rq, p) &&
  1456. !test_tsk_need_resched(rq->curr) &&
  1457. has_pushable_tasks(rq) &&
  1458. p->nr_cpus_allowed > 1 &&
  1459. rt_task(rq->curr) &&
  1460. (rq->curr->nr_cpus_allowed < 2 ||
  1461. rq->curr->prio <= p->prio))
  1462. push_rt_tasks(rq);
  1463. }
  1464. static void set_cpus_allowed_rt(struct task_struct *p,
  1465. const struct cpumask *new_mask)
  1466. {
  1467. struct rq *rq;
  1468. int weight;
  1469. BUG_ON(!rt_task(p));
  1470. if (!p->on_rq)
  1471. return;
  1472. weight = cpumask_weight(new_mask);
  1473. /*
  1474. * Only update if the process changes its state from whether it
  1475. * can migrate or not.
  1476. */
  1477. if ((p->nr_cpus_allowed > 1) == (weight > 1))
  1478. return;
  1479. rq = task_rq(p);
  1480. /*
  1481. * The process used to be able to migrate OR it can now migrate
  1482. */
  1483. if (weight <= 1) {
  1484. if (!task_current(rq, p))
  1485. dequeue_pushable_task(rq, p);
  1486. BUG_ON(!rq->rt.rt_nr_migratory);
  1487. rq->rt.rt_nr_migratory--;
  1488. } else {
  1489. if (!task_current(rq, p))
  1490. enqueue_pushable_task(rq, p);
  1491. rq->rt.rt_nr_migratory++;
  1492. }
  1493. update_rt_migration(&rq->rt);
  1494. }
  1495. /* Assumes rq->lock is held */
  1496. static void rq_online_rt(struct rq *rq)
  1497. {
  1498. if (rq->rt.overloaded)
  1499. rt_set_overload(rq);
  1500. __enable_runtime(rq);
  1501. cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
  1502. }
  1503. /* Assumes rq->lock is held */
  1504. static void rq_offline_rt(struct rq *rq)
  1505. {
  1506. if (rq->rt.overloaded)
  1507. rt_clear_overload(rq);
  1508. __disable_runtime(rq);
  1509. cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
  1510. }
  1511. /*
  1512. * When switch from the rt queue, we bring ourselves to a position
  1513. * that we might want to pull RT tasks from other runqueues.
  1514. */
  1515. static void switched_from_rt(struct rq *rq, struct task_struct *p)
  1516. {
  1517. /*
  1518. * If there are other RT tasks then we will reschedule
  1519. * and the scheduling of the other RT tasks will handle
  1520. * the balancing. But if we are the last RT task
  1521. * we may need to handle the pulling of RT tasks
  1522. * now.
  1523. */
  1524. if (!p->on_rq || rq->rt.rt_nr_running)
  1525. return;
  1526. if (pull_rt_task(rq))
  1527. resched_task(rq->curr);
  1528. }
  1529. void init_sched_rt_class(void)
  1530. {
  1531. unsigned int i;
  1532. for_each_possible_cpu(i) {
  1533. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
  1534. GFP_KERNEL, cpu_to_node(i));
  1535. }
  1536. }
  1537. #endif /* CONFIG_SMP */
  1538. /*
  1539. * When switching a task to RT, we may overload the runqueue
  1540. * with RT tasks. In this case we try to push them off to
  1541. * other runqueues.
  1542. */
  1543. static void switched_to_rt(struct rq *rq, struct task_struct *p)
  1544. {
  1545. int check_resched = 1;
  1546. /*
  1547. * If we are already running, then there's nothing
  1548. * that needs to be done. But if we are not running
  1549. * we may need to preempt the current running task.
  1550. * If that current running task is also an RT task
  1551. * then see if we can move to another run queue.
  1552. */
  1553. if (p->on_rq && rq->curr != p) {
  1554. #ifdef CONFIG_SMP
  1555. if (rq->rt.overloaded && push_rt_task(rq) &&
  1556. /* Don't resched if we changed runqueues */
  1557. rq != task_rq(p))
  1558. check_resched = 0;
  1559. #endif /* CONFIG_SMP */
  1560. if (check_resched && p->prio < rq->curr->prio)
  1561. resched_task(rq->curr);
  1562. }
  1563. }
  1564. /*
  1565. * Priority of the task has changed. This may cause
  1566. * us to initiate a push or pull.
  1567. */
  1568. static void
  1569. prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
  1570. {
  1571. if (!p->on_rq)
  1572. return;
  1573. if (rq->curr == p) {
  1574. #ifdef CONFIG_SMP
  1575. /*
  1576. * If our priority decreases while running, we
  1577. * may need to pull tasks to this runqueue.
  1578. */
  1579. if (oldprio < p->prio)
  1580. pull_rt_task(rq);
  1581. /*
  1582. * If there's a higher priority task waiting to run
  1583. * then reschedule. Note, the above pull_rt_task
  1584. * can release the rq lock and p could migrate.
  1585. * Only reschedule if p is still on the same runqueue.
  1586. */
  1587. if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
  1588. resched_task(p);
  1589. #else
  1590. /* For UP simply resched on drop of prio */
  1591. if (oldprio < p->prio)
  1592. resched_task(p);
  1593. #endif /* CONFIG_SMP */
  1594. } else {
  1595. /*
  1596. * This task is not running, but if it is
  1597. * greater than the current running task
  1598. * then reschedule.
  1599. */
  1600. if (p->prio < rq->curr->prio)
  1601. resched_task(rq->curr);
  1602. }
  1603. }
  1604. static void watchdog(struct rq *rq, struct task_struct *p)
  1605. {
  1606. unsigned long soft, hard;
  1607. /* max may change after cur was read, this will be fixed next tick */
  1608. soft = task_rlimit(p, RLIMIT_RTTIME);
  1609. hard = task_rlimit_max(p, RLIMIT_RTTIME);
  1610. if (soft != RLIM_INFINITY) {
  1611. unsigned long next;
  1612. if (p->rt.watchdog_stamp != jiffies) {
  1613. p->rt.timeout++;
  1614. p->rt.watchdog_stamp = jiffies;
  1615. }
  1616. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  1617. if (p->rt.timeout > next)
  1618. p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
  1619. }
  1620. }
  1621. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  1622. {
  1623. struct sched_rt_entity *rt_se = &p->rt;
  1624. update_curr_rt(rq);
  1625. watchdog(rq, p);
  1626. /*
  1627. * RR tasks need a special form of timeslice management.
  1628. * FIFO tasks have no timeslices.
  1629. */
  1630. if (p->policy != SCHED_RR)
  1631. return;
  1632. if (--p->rt.time_slice)
  1633. return;
  1634. p->rt.time_slice = RR_TIMESLICE;
  1635. /*
  1636. * Requeue to the end of queue if we (and all of our ancestors) are the
  1637. * only element on the queue
  1638. */
  1639. for_each_sched_rt_entity(rt_se) {
  1640. if (rt_se->run_list.prev != rt_se->run_list.next) {
  1641. requeue_task_rt(rq, p, 0);
  1642. set_tsk_need_resched(p);
  1643. return;
  1644. }
  1645. }
  1646. }
  1647. static void set_curr_task_rt(struct rq *rq)
  1648. {
  1649. struct task_struct *p = rq->curr;
  1650. p->se.exec_start = rq->clock_task;
  1651. /* The running task is never eligible for pushing */
  1652. dequeue_pushable_task(rq, p);
  1653. }
  1654. static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
  1655. {
  1656. /*
  1657. * Time slice is 0 for SCHED_FIFO tasks
  1658. */
  1659. if (task->policy == SCHED_RR)
  1660. return RR_TIMESLICE;
  1661. else
  1662. return 0;
  1663. }
  1664. const struct sched_class rt_sched_class = {
  1665. .next = &fair_sched_class,
  1666. .enqueue_task = enqueue_task_rt,
  1667. .dequeue_task = dequeue_task_rt,
  1668. .yield_task = yield_task_rt,
  1669. .check_preempt_curr = check_preempt_curr_rt,
  1670. .pick_next_task = pick_next_task_rt,
  1671. .put_prev_task = put_prev_task_rt,
  1672. #ifdef CONFIG_SMP
  1673. .select_task_rq = select_task_rq_rt,
  1674. .set_cpus_allowed = set_cpus_allowed_rt,
  1675. .rq_online = rq_online_rt,
  1676. .rq_offline = rq_offline_rt,
  1677. .pre_schedule = pre_schedule_rt,
  1678. .post_schedule = post_schedule_rt,
  1679. .task_woken = task_woken_rt,
  1680. .switched_from = switched_from_rt,
  1681. #endif
  1682. .set_curr_task = set_curr_task_rt,
  1683. .task_tick = task_tick_rt,
  1684. .get_rr_interval = get_rr_interval_rt,
  1685. .prio_changed = prio_changed_rt,
  1686. .switched_to = switched_to_rt,
  1687. };
  1688. #ifdef CONFIG_SCHED_DEBUG
  1689. extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
  1690. void print_rt_stats(struct seq_file *m, int cpu)
  1691. {
  1692. rt_rq_iter_t iter;
  1693. struct rt_rq *rt_rq;
  1694. rcu_read_lock();
  1695. for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
  1696. print_rt_rq(m, cpu, rt_rq);
  1697. rcu_read_unlock();
  1698. }
  1699. #endif /* CONFIG_SCHED_DEBUG */