rt.c 53 KB

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