rt.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  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 void 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;
  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. if (rt_rq->rt_runtime == rt_period) {
  532. raw_spin_unlock(&iter->rt_runtime_lock);
  533. break;
  534. }
  535. }
  536. next:
  537. raw_spin_unlock(&iter->rt_runtime_lock);
  538. }
  539. raw_spin_unlock(&rt_b->rt_runtime_lock);
  540. }
  541. /*
  542. * Ensure this RQ takes back all the runtime it lend to its neighbours.
  543. */
  544. static void __disable_runtime(struct rq *rq)
  545. {
  546. struct root_domain *rd = rq->rd;
  547. rt_rq_iter_t iter;
  548. struct rt_rq *rt_rq;
  549. if (unlikely(!scheduler_running))
  550. return;
  551. for_each_rt_rq(rt_rq, iter, rq) {
  552. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  553. s64 want;
  554. int i;
  555. raw_spin_lock(&rt_b->rt_runtime_lock);
  556. raw_spin_lock(&rt_rq->rt_runtime_lock);
  557. /*
  558. * Either we're all inf and nobody needs to borrow, or we're
  559. * already disabled and thus have nothing to do, or we have
  560. * exactly the right amount of runtime to take out.
  561. */
  562. if (rt_rq->rt_runtime == RUNTIME_INF ||
  563. rt_rq->rt_runtime == rt_b->rt_runtime)
  564. goto balanced;
  565. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  566. /*
  567. * Calculate the difference between what we started out with
  568. * and what we current have, that's the amount of runtime
  569. * we lend and now have to reclaim.
  570. */
  571. want = rt_b->rt_runtime - rt_rq->rt_runtime;
  572. /*
  573. * Greedy reclaim, take back as much as we can.
  574. */
  575. for_each_cpu(i, rd->span) {
  576. struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
  577. s64 diff;
  578. /*
  579. * Can't reclaim from ourselves or disabled runqueues.
  580. */
  581. if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
  582. continue;
  583. raw_spin_lock(&iter->rt_runtime_lock);
  584. if (want > 0) {
  585. diff = min_t(s64, iter->rt_runtime, want);
  586. iter->rt_runtime -= diff;
  587. want -= diff;
  588. } else {
  589. iter->rt_runtime -= want;
  590. want -= want;
  591. }
  592. raw_spin_unlock(&iter->rt_runtime_lock);
  593. if (!want)
  594. break;
  595. }
  596. raw_spin_lock(&rt_rq->rt_runtime_lock);
  597. /*
  598. * We cannot be left wanting - that would mean some runtime
  599. * leaked out of the system.
  600. */
  601. BUG_ON(want);
  602. balanced:
  603. /*
  604. * Disable all the borrow logic by pretending we have inf
  605. * runtime - in which case borrowing doesn't make sense.
  606. */
  607. rt_rq->rt_runtime = RUNTIME_INF;
  608. rt_rq->rt_throttled = 0;
  609. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  610. raw_spin_unlock(&rt_b->rt_runtime_lock);
  611. /* Make rt_rq available for pick_next_task() */
  612. sched_rt_rq_enqueue(rt_rq);
  613. }
  614. }
  615. static void __enable_runtime(struct rq *rq)
  616. {
  617. rt_rq_iter_t iter;
  618. struct rt_rq *rt_rq;
  619. if (unlikely(!scheduler_running))
  620. return;
  621. /*
  622. * Reset each runqueue's bandwidth settings
  623. */
  624. for_each_rt_rq(rt_rq, iter, rq) {
  625. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  626. raw_spin_lock(&rt_b->rt_runtime_lock);
  627. raw_spin_lock(&rt_rq->rt_runtime_lock);
  628. rt_rq->rt_runtime = rt_b->rt_runtime;
  629. rt_rq->rt_time = 0;
  630. rt_rq->rt_throttled = 0;
  631. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  632. raw_spin_unlock(&rt_b->rt_runtime_lock);
  633. }
  634. }
  635. static void balance_runtime(struct rt_rq *rt_rq)
  636. {
  637. if (!sched_feat(RT_RUNTIME_SHARE))
  638. return;
  639. if (rt_rq->rt_time > rt_rq->rt_runtime) {
  640. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  641. do_balance_runtime(rt_rq);
  642. raw_spin_lock(&rt_rq->rt_runtime_lock);
  643. }
  644. }
  645. #else /* !CONFIG_SMP */
  646. static inline void balance_runtime(struct rt_rq *rt_rq) {}
  647. #endif /* CONFIG_SMP */
  648. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
  649. {
  650. int i, idle = 1, throttled = 0;
  651. const struct cpumask *span;
  652. span = sched_rt_period_mask();
  653. #ifdef CONFIG_RT_GROUP_SCHED
  654. /*
  655. * FIXME: isolated CPUs should really leave the root task group,
  656. * whether they are isolcpus or were isolated via cpusets, lest
  657. * the timer run on a CPU which does not service all runqueues,
  658. * potentially leaving other CPUs indefinitely throttled. If
  659. * isolation is really required, the user will turn the throttle
  660. * off to kill the perturbations it causes anyway. Meanwhile,
  661. * this maintains functionality for boot and/or troubleshooting.
  662. */
  663. if (rt_b == &root_task_group.rt_bandwidth)
  664. span = cpu_online_mask;
  665. #endif
  666. for_each_cpu(i, span) {
  667. int enqueue = 0;
  668. struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
  669. struct rq *rq = rq_of_rt_rq(rt_rq);
  670. raw_spin_lock(&rq->lock);
  671. if (rt_rq->rt_time) {
  672. u64 runtime;
  673. raw_spin_lock(&rt_rq->rt_runtime_lock);
  674. if (rt_rq->rt_throttled)
  675. balance_runtime(rt_rq);
  676. runtime = rt_rq->rt_runtime;
  677. rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
  678. if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
  679. rt_rq->rt_throttled = 0;
  680. enqueue = 1;
  681. /*
  682. * When we're idle and a woken (rt) task is
  683. * throttled check_preempt_curr() will set
  684. * skip_update and the time between the wakeup
  685. * and this unthrottle will get accounted as
  686. * 'runtime'.
  687. */
  688. if (rt_rq->rt_nr_running && rq->curr == rq->idle)
  689. rq_clock_skip_update(rq, false);
  690. }
  691. if (rt_rq->rt_time || rt_rq->rt_nr_running)
  692. idle = 0;
  693. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  694. } else if (rt_rq->rt_nr_running) {
  695. idle = 0;
  696. if (!rt_rq_throttled(rt_rq))
  697. enqueue = 1;
  698. }
  699. if (rt_rq->rt_throttled)
  700. throttled = 1;
  701. if (enqueue)
  702. sched_rt_rq_enqueue(rt_rq);
  703. raw_spin_unlock(&rq->lock);
  704. }
  705. if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
  706. return 1;
  707. return idle;
  708. }
  709. static inline int rt_se_prio(struct sched_rt_entity *rt_se)
  710. {
  711. #ifdef CONFIG_RT_GROUP_SCHED
  712. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  713. if (rt_rq)
  714. return rt_rq->highest_prio.curr;
  715. #endif
  716. return rt_task_of(rt_se)->prio;
  717. }
  718. static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
  719. {
  720. u64 runtime = sched_rt_runtime(rt_rq);
  721. if (rt_rq->rt_throttled)
  722. return rt_rq_throttled(rt_rq);
  723. if (runtime >= sched_rt_period(rt_rq))
  724. return 0;
  725. balance_runtime(rt_rq);
  726. runtime = sched_rt_runtime(rt_rq);
  727. if (runtime == RUNTIME_INF)
  728. return 0;
  729. if (rt_rq->rt_time > runtime) {
  730. struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
  731. /*
  732. * Don't actually throttle groups that have no runtime assigned
  733. * but accrue some time due to boosting.
  734. */
  735. if (likely(rt_b->rt_runtime)) {
  736. rt_rq->rt_throttled = 1;
  737. printk_deferred_once("sched: RT throttling activated\n");
  738. } else {
  739. /*
  740. * In case we did anyway, make it go away,
  741. * replenishment is a joke, since it will replenish us
  742. * with exactly 0 ns.
  743. */
  744. rt_rq->rt_time = 0;
  745. }
  746. if (rt_rq_throttled(rt_rq)) {
  747. sched_rt_rq_dequeue(rt_rq);
  748. return 1;
  749. }
  750. }
  751. return 0;
  752. }
  753. /*
  754. * Update the current task's runtime statistics. Skip current tasks that
  755. * are not in our scheduling class.
  756. */
  757. static void update_curr_rt(struct rq *rq)
  758. {
  759. struct task_struct *curr = rq->curr;
  760. struct sched_rt_entity *rt_se = &curr->rt;
  761. u64 delta_exec;
  762. if (curr->sched_class != &rt_sched_class)
  763. return;
  764. delta_exec = rq_clock_task(rq) - curr->se.exec_start;
  765. if (unlikely((s64)delta_exec <= 0))
  766. return;
  767. schedstat_set(curr->se.statistics.exec_max,
  768. max(curr->se.statistics.exec_max, delta_exec));
  769. curr->se.sum_exec_runtime += delta_exec;
  770. account_group_exec_runtime(curr, delta_exec);
  771. curr->se.exec_start = rq_clock_task(rq);
  772. cpuacct_charge(curr, delta_exec);
  773. sched_rt_avg_update(rq, delta_exec);
  774. if (!rt_bandwidth_enabled())
  775. return;
  776. for_each_sched_rt_entity(rt_se) {
  777. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  778. if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
  779. raw_spin_lock(&rt_rq->rt_runtime_lock);
  780. rt_rq->rt_time += delta_exec;
  781. if (sched_rt_runtime_exceeded(rt_rq))
  782. resched_curr(rq);
  783. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  784. }
  785. }
  786. }
  787. static void
  788. dequeue_top_rt_rq(struct rt_rq *rt_rq)
  789. {
  790. struct rq *rq = rq_of_rt_rq(rt_rq);
  791. BUG_ON(&rq->rt != rt_rq);
  792. if (!rt_rq->rt_queued)
  793. return;
  794. BUG_ON(!rq->nr_running);
  795. sub_nr_running(rq, rt_rq->rt_nr_running);
  796. rt_rq->rt_queued = 0;
  797. }
  798. static void
  799. enqueue_top_rt_rq(struct rt_rq *rt_rq)
  800. {
  801. struct rq *rq = rq_of_rt_rq(rt_rq);
  802. BUG_ON(&rq->rt != rt_rq);
  803. if (rt_rq->rt_queued)
  804. return;
  805. if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
  806. return;
  807. add_nr_running(rq, rt_rq->rt_nr_running);
  808. rt_rq->rt_queued = 1;
  809. }
  810. #if defined CONFIG_SMP
  811. static void
  812. inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  813. {
  814. struct rq *rq = rq_of_rt_rq(rt_rq);
  815. #ifdef CONFIG_RT_GROUP_SCHED
  816. /*
  817. * Change rq's cpupri only if rt_rq is the top queue.
  818. */
  819. if (&rq->rt != rt_rq)
  820. return;
  821. #endif
  822. if (rq->online && prio < prev_prio)
  823. cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
  824. }
  825. static void
  826. dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
  827. {
  828. struct rq *rq = rq_of_rt_rq(rt_rq);
  829. #ifdef CONFIG_RT_GROUP_SCHED
  830. /*
  831. * Change rq's cpupri only if rt_rq is the top queue.
  832. */
  833. if (&rq->rt != rt_rq)
  834. return;
  835. #endif
  836. if (rq->online && rt_rq->highest_prio.curr != prev_prio)
  837. cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
  838. }
  839. #else /* CONFIG_SMP */
  840. static inline
  841. void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  842. static inline
  843. void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
  844. #endif /* CONFIG_SMP */
  845. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  846. static void
  847. inc_rt_prio(struct rt_rq *rt_rq, int prio)
  848. {
  849. int prev_prio = rt_rq->highest_prio.curr;
  850. if (prio < prev_prio)
  851. rt_rq->highest_prio.curr = prio;
  852. inc_rt_prio_smp(rt_rq, prio, prev_prio);
  853. }
  854. static void
  855. dec_rt_prio(struct rt_rq *rt_rq, int prio)
  856. {
  857. int prev_prio = rt_rq->highest_prio.curr;
  858. if (rt_rq->rt_nr_running) {
  859. WARN_ON(prio < prev_prio);
  860. /*
  861. * This may have been our highest task, and therefore
  862. * we may have some recomputation to do
  863. */
  864. if (prio == prev_prio) {
  865. struct rt_prio_array *array = &rt_rq->active;
  866. rt_rq->highest_prio.curr =
  867. sched_find_first_bit(array->bitmap);
  868. }
  869. } else
  870. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  871. dec_rt_prio_smp(rt_rq, prio, prev_prio);
  872. }
  873. #else
  874. static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
  875. static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
  876. #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
  877. #ifdef CONFIG_RT_GROUP_SCHED
  878. static void
  879. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  880. {
  881. if (rt_se_boosted(rt_se))
  882. rt_rq->rt_nr_boosted++;
  883. if (rt_rq->tg)
  884. start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
  885. }
  886. static void
  887. dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  888. {
  889. if (rt_se_boosted(rt_se))
  890. rt_rq->rt_nr_boosted--;
  891. WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
  892. }
  893. #else /* CONFIG_RT_GROUP_SCHED */
  894. static void
  895. inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  896. {
  897. start_rt_bandwidth(&def_rt_bandwidth);
  898. }
  899. static inline
  900. void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
  901. #endif /* CONFIG_RT_GROUP_SCHED */
  902. static inline
  903. unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
  904. {
  905. struct rt_rq *group_rq = group_rt_rq(rt_se);
  906. if (group_rq)
  907. return group_rq->rt_nr_running;
  908. else
  909. return 1;
  910. }
  911. static inline
  912. void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  913. {
  914. int prio = rt_se_prio(rt_se);
  915. WARN_ON(!rt_prio(prio));
  916. rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
  917. inc_rt_prio(rt_rq, prio);
  918. inc_rt_migration(rt_se, rt_rq);
  919. inc_rt_group(rt_se, rt_rq);
  920. }
  921. static inline
  922. void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
  923. {
  924. WARN_ON(!rt_prio(rt_se_prio(rt_se)));
  925. WARN_ON(!rt_rq->rt_nr_running);
  926. rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
  927. dec_rt_prio(rt_rq, rt_se_prio(rt_se));
  928. dec_rt_migration(rt_se, rt_rq);
  929. dec_rt_group(rt_se, rt_rq);
  930. }
  931. static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
  932. {
  933. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  934. struct rt_prio_array *array = &rt_rq->active;
  935. struct rt_rq *group_rq = group_rt_rq(rt_se);
  936. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  937. /*
  938. * Don't enqueue the group if its throttled, or when empty.
  939. * The latter is a consequence of the former when a child group
  940. * get throttled and the current group doesn't have any other
  941. * active members.
  942. */
  943. if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
  944. return;
  945. if (head)
  946. list_add(&rt_se->run_list, queue);
  947. else
  948. list_add_tail(&rt_se->run_list, queue);
  949. __set_bit(rt_se_prio(rt_se), array->bitmap);
  950. inc_rt_tasks(rt_se, rt_rq);
  951. }
  952. static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
  953. {
  954. struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
  955. struct rt_prio_array *array = &rt_rq->active;
  956. list_del_init(&rt_se->run_list);
  957. if (list_empty(array->queue + rt_se_prio(rt_se)))
  958. __clear_bit(rt_se_prio(rt_se), array->bitmap);
  959. dec_rt_tasks(rt_se, rt_rq);
  960. }
  961. /*
  962. * Because the prio of an upper entry depends on the lower
  963. * entries, we must remove entries top - down.
  964. */
  965. static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
  966. {
  967. struct sched_rt_entity *back = NULL;
  968. for_each_sched_rt_entity(rt_se) {
  969. rt_se->back = back;
  970. back = rt_se;
  971. }
  972. dequeue_top_rt_rq(rt_rq_of_se(back));
  973. for (rt_se = back; rt_se; rt_se = rt_se->back) {
  974. if (on_rt_rq(rt_se))
  975. __dequeue_rt_entity(rt_se);
  976. }
  977. }
  978. static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
  979. {
  980. struct rq *rq = rq_of_rt_se(rt_se);
  981. dequeue_rt_stack(rt_se);
  982. for_each_sched_rt_entity(rt_se)
  983. __enqueue_rt_entity(rt_se, head);
  984. enqueue_top_rt_rq(&rq->rt);
  985. }
  986. static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
  987. {
  988. struct rq *rq = rq_of_rt_se(rt_se);
  989. dequeue_rt_stack(rt_se);
  990. for_each_sched_rt_entity(rt_se) {
  991. struct rt_rq *rt_rq = group_rt_rq(rt_se);
  992. if (rt_rq && rt_rq->rt_nr_running)
  993. __enqueue_rt_entity(rt_se, false);
  994. }
  995. enqueue_top_rt_rq(&rq->rt);
  996. }
  997. /*
  998. * Adding/removing a task to/from a priority array:
  999. */
  1000. static void
  1001. enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  1002. {
  1003. struct sched_rt_entity *rt_se = &p->rt;
  1004. if (flags & ENQUEUE_WAKEUP)
  1005. rt_se->timeout = 0;
  1006. enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
  1007. if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  1008. enqueue_pushable_task(rq, p);
  1009. }
  1010. static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
  1011. {
  1012. struct sched_rt_entity *rt_se = &p->rt;
  1013. update_curr_rt(rq);
  1014. dequeue_rt_entity(rt_se);
  1015. dequeue_pushable_task(rq, p);
  1016. }
  1017. /*
  1018. * Put task to the head or the end of the run list without the overhead of
  1019. * dequeue followed by enqueue.
  1020. */
  1021. static void
  1022. requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
  1023. {
  1024. if (on_rt_rq(rt_se)) {
  1025. struct rt_prio_array *array = &rt_rq->active;
  1026. struct list_head *queue = array->queue + rt_se_prio(rt_se);
  1027. if (head)
  1028. list_move(&rt_se->run_list, queue);
  1029. else
  1030. list_move_tail(&rt_se->run_list, queue);
  1031. }
  1032. }
  1033. static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
  1034. {
  1035. struct sched_rt_entity *rt_se = &p->rt;
  1036. struct rt_rq *rt_rq;
  1037. for_each_sched_rt_entity(rt_se) {
  1038. rt_rq = rt_rq_of_se(rt_se);
  1039. requeue_rt_entity(rt_rq, rt_se, head);
  1040. }
  1041. }
  1042. static void yield_task_rt(struct rq *rq)
  1043. {
  1044. requeue_task_rt(rq, rq->curr, 0);
  1045. }
  1046. #ifdef CONFIG_SMP
  1047. static int find_lowest_rq(struct task_struct *task);
  1048. static int
  1049. select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
  1050. {
  1051. struct task_struct *curr;
  1052. struct rq *rq;
  1053. /* For anything but wake ups, just return the task_cpu */
  1054. if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
  1055. goto out;
  1056. rq = cpu_rq(cpu);
  1057. rcu_read_lock();
  1058. curr = READ_ONCE(rq->curr); /* unlocked access */
  1059. /*
  1060. * If the current task on @p's runqueue is an RT task, then
  1061. * try to see if we can wake this RT task up on another
  1062. * runqueue. Otherwise simply start this RT task
  1063. * on its current runqueue.
  1064. *
  1065. * We want to avoid overloading runqueues. If the woken
  1066. * task is a higher priority, then it will stay on this CPU
  1067. * and the lower prio task should be moved to another CPU.
  1068. * Even though this will probably make the lower prio task
  1069. * lose its cache, we do not want to bounce a higher task
  1070. * around just because it gave up its CPU, perhaps for a
  1071. * lock?
  1072. *
  1073. * For equal prio tasks, we just let the scheduler sort it out.
  1074. *
  1075. * Otherwise, just let it ride on the affined RQ and the
  1076. * post-schedule router will push the preempted task away
  1077. *
  1078. * This test is optimistic, if we get it wrong the load-balancer
  1079. * will have to sort it out.
  1080. */
  1081. if (curr && unlikely(rt_task(curr)) &&
  1082. (curr->nr_cpus_allowed < 2 ||
  1083. curr->prio <= p->prio)) {
  1084. int target = find_lowest_rq(p);
  1085. /*
  1086. * Don't bother moving it if the destination CPU is
  1087. * not running a lower priority task.
  1088. */
  1089. if (target != -1 &&
  1090. p->prio < cpu_rq(target)->rt.highest_prio.curr)
  1091. cpu = target;
  1092. }
  1093. rcu_read_unlock();
  1094. out:
  1095. return cpu;
  1096. }
  1097. static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
  1098. {
  1099. /*
  1100. * Current can't be migrated, useless to reschedule,
  1101. * let's hope p can move out.
  1102. */
  1103. if (rq->curr->nr_cpus_allowed == 1 ||
  1104. !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
  1105. return;
  1106. /*
  1107. * p is migratable, so let's not schedule it and
  1108. * see if it is pushed or pulled somewhere else.
  1109. */
  1110. if (p->nr_cpus_allowed != 1
  1111. && cpupri_find(&rq->rd->cpupri, p, NULL))
  1112. return;
  1113. /*
  1114. * There appears to be other cpus that can accept
  1115. * current and none to run 'p', so lets reschedule
  1116. * to try and push current away:
  1117. */
  1118. requeue_task_rt(rq, p, 1);
  1119. resched_curr(rq);
  1120. }
  1121. #endif /* CONFIG_SMP */
  1122. /*
  1123. * Preempt the current task with a newly woken task if needed:
  1124. */
  1125. static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
  1126. {
  1127. if (p->prio < rq->curr->prio) {
  1128. resched_curr(rq);
  1129. return;
  1130. }
  1131. #ifdef CONFIG_SMP
  1132. /*
  1133. * If:
  1134. *
  1135. * - the newly woken task is of equal priority to the current task
  1136. * - the newly woken task is non-migratable while current is migratable
  1137. * - current will be preempted on the next reschedule
  1138. *
  1139. * we should check to see if current can readily move to a different
  1140. * cpu. If so, we will reschedule to allow the push logic to try
  1141. * to move current somewhere else, making room for our non-migratable
  1142. * task.
  1143. */
  1144. if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
  1145. check_preempt_equal_prio(rq, p);
  1146. #endif
  1147. }
  1148. static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
  1149. struct rt_rq *rt_rq)
  1150. {
  1151. struct rt_prio_array *array = &rt_rq->active;
  1152. struct sched_rt_entity *next = NULL;
  1153. struct list_head *queue;
  1154. int idx;
  1155. idx = sched_find_first_bit(array->bitmap);
  1156. BUG_ON(idx >= MAX_RT_PRIO);
  1157. queue = array->queue + idx;
  1158. next = list_entry(queue->next, struct sched_rt_entity, run_list);
  1159. return next;
  1160. }
  1161. static struct task_struct *_pick_next_task_rt(struct rq *rq)
  1162. {
  1163. struct sched_rt_entity *rt_se;
  1164. struct task_struct *p;
  1165. struct rt_rq *rt_rq = &rq->rt;
  1166. do {
  1167. rt_se = pick_next_rt_entity(rq, rt_rq);
  1168. BUG_ON(!rt_se);
  1169. rt_rq = group_rt_rq(rt_se);
  1170. } while (rt_rq);
  1171. p = rt_task_of(rt_se);
  1172. p->se.exec_start = rq_clock_task(rq);
  1173. return p;
  1174. }
  1175. static struct task_struct *
  1176. pick_next_task_rt(struct rq *rq, struct task_struct *prev)
  1177. {
  1178. struct task_struct *p;
  1179. struct rt_rq *rt_rq = &rq->rt;
  1180. if (need_pull_rt_task(rq, prev)) {
  1181. /*
  1182. * This is OK, because current is on_cpu, which avoids it being
  1183. * picked for load-balance and preemption/IRQs are still
  1184. * disabled avoiding further scheduler activity on it and we're
  1185. * being very careful to re-start the picking loop.
  1186. */
  1187. lockdep_unpin_lock(&rq->lock);
  1188. pull_rt_task(rq);
  1189. lockdep_pin_lock(&rq->lock);
  1190. /*
  1191. * pull_rt_task() can drop (and re-acquire) rq->lock; this
  1192. * means a dl or stop task can slip in, in which case we need
  1193. * to re-start task selection.
  1194. */
  1195. if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) ||
  1196. rq->dl.dl_nr_running))
  1197. return RETRY_TASK;
  1198. }
  1199. /*
  1200. * We may dequeue prev's rt_rq in put_prev_task().
  1201. * So, we update time before rt_nr_running check.
  1202. */
  1203. if (prev->sched_class == &rt_sched_class)
  1204. update_curr_rt(rq);
  1205. if (!rt_rq->rt_queued)
  1206. return NULL;
  1207. put_prev_task(rq, prev);
  1208. p = _pick_next_task_rt(rq);
  1209. /* The running task is never eligible for pushing */
  1210. dequeue_pushable_task(rq, p);
  1211. queue_push_tasks(rq);
  1212. return p;
  1213. }
  1214. static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
  1215. {
  1216. update_curr_rt(rq);
  1217. /*
  1218. * The previous task needs to be made eligible for pushing
  1219. * if it is still active
  1220. */
  1221. if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
  1222. enqueue_pushable_task(rq, p);
  1223. }
  1224. #ifdef CONFIG_SMP
  1225. /* Only try algorithms three times */
  1226. #define RT_MAX_TRIES 3
  1227. static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
  1228. {
  1229. if (!task_running(rq, p) &&
  1230. cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
  1231. return 1;
  1232. return 0;
  1233. }
  1234. /*
  1235. * Return the highest pushable rq's task, which is suitable to be executed
  1236. * on the cpu, NULL otherwise
  1237. */
  1238. static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
  1239. {
  1240. struct plist_head *head = &rq->rt.pushable_tasks;
  1241. struct task_struct *p;
  1242. if (!has_pushable_tasks(rq))
  1243. return NULL;
  1244. plist_for_each_entry(p, head, pushable_tasks) {
  1245. if (pick_rt_task(rq, p, cpu))
  1246. return p;
  1247. }
  1248. return NULL;
  1249. }
  1250. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
  1251. static int find_lowest_rq(struct task_struct *task)
  1252. {
  1253. struct sched_domain *sd;
  1254. struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
  1255. int this_cpu = smp_processor_id();
  1256. int cpu = task_cpu(task);
  1257. /* Make sure the mask is initialized first */
  1258. if (unlikely(!lowest_mask))
  1259. return -1;
  1260. if (task->nr_cpus_allowed == 1)
  1261. return -1; /* No other targets possible */
  1262. if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
  1263. return -1; /* No targets found */
  1264. /*
  1265. * At this point we have built a mask of cpus representing the
  1266. * lowest priority tasks in the system. Now we want to elect
  1267. * the best one based on our affinity and topology.
  1268. *
  1269. * We prioritize the last cpu that the task executed on since
  1270. * it is most likely cache-hot in that location.
  1271. */
  1272. if (cpumask_test_cpu(cpu, lowest_mask))
  1273. return cpu;
  1274. /*
  1275. * Otherwise, we consult the sched_domains span maps to figure
  1276. * out which cpu is logically closest to our hot cache data.
  1277. */
  1278. if (!cpumask_test_cpu(this_cpu, lowest_mask))
  1279. this_cpu = -1; /* Skip this_cpu opt if not among lowest */
  1280. rcu_read_lock();
  1281. for_each_domain(cpu, sd) {
  1282. if (sd->flags & SD_WAKE_AFFINE) {
  1283. int best_cpu;
  1284. /*
  1285. * "this_cpu" is cheaper to preempt than a
  1286. * remote processor.
  1287. */
  1288. if (this_cpu != -1 &&
  1289. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  1290. rcu_read_unlock();
  1291. return this_cpu;
  1292. }
  1293. best_cpu = cpumask_first_and(lowest_mask,
  1294. sched_domain_span(sd));
  1295. if (best_cpu < nr_cpu_ids) {
  1296. rcu_read_unlock();
  1297. return best_cpu;
  1298. }
  1299. }
  1300. }
  1301. rcu_read_unlock();
  1302. /*
  1303. * And finally, if there were no matches within the domains
  1304. * just give the caller *something* to work with from the compatible
  1305. * locations.
  1306. */
  1307. if (this_cpu != -1)
  1308. return this_cpu;
  1309. cpu = cpumask_any(lowest_mask);
  1310. if (cpu < nr_cpu_ids)
  1311. return cpu;
  1312. return -1;
  1313. }
  1314. /* Will lock the rq it finds */
  1315. static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
  1316. {
  1317. struct rq *lowest_rq = NULL;
  1318. int tries;
  1319. int cpu;
  1320. for (tries = 0; tries < RT_MAX_TRIES; tries++) {
  1321. cpu = find_lowest_rq(task);
  1322. if ((cpu == -1) || (cpu == rq->cpu))
  1323. break;
  1324. lowest_rq = cpu_rq(cpu);
  1325. if (lowest_rq->rt.highest_prio.curr <= task->prio) {
  1326. /*
  1327. * Target rq has tasks of equal or higher priority,
  1328. * retrying does not release any lock and is unlikely
  1329. * to yield a different result.
  1330. */
  1331. lowest_rq = NULL;
  1332. break;
  1333. }
  1334. /* if the prio of this runqueue changed, try again */
  1335. if (double_lock_balance(rq, lowest_rq)) {
  1336. /*
  1337. * We had to unlock the run queue. In
  1338. * the mean time, task could have
  1339. * migrated already or had its affinity changed.
  1340. * Also make sure that it wasn't scheduled on its rq.
  1341. */
  1342. if (unlikely(task_rq(task) != rq ||
  1343. !cpumask_test_cpu(lowest_rq->cpu,
  1344. tsk_cpus_allowed(task)) ||
  1345. task_running(rq, task) ||
  1346. !task_on_rq_queued(task))) {
  1347. double_unlock_balance(rq, lowest_rq);
  1348. lowest_rq = NULL;
  1349. break;
  1350. }
  1351. }
  1352. /* If this rq is still suitable use it. */
  1353. if (lowest_rq->rt.highest_prio.curr > task->prio)
  1354. break;
  1355. /* try again */
  1356. double_unlock_balance(rq, lowest_rq);
  1357. lowest_rq = NULL;
  1358. }
  1359. return lowest_rq;
  1360. }
  1361. static struct task_struct *pick_next_pushable_task(struct rq *rq)
  1362. {
  1363. struct task_struct *p;
  1364. if (!has_pushable_tasks(rq))
  1365. return NULL;
  1366. p = plist_first_entry(&rq->rt.pushable_tasks,
  1367. struct task_struct, pushable_tasks);
  1368. BUG_ON(rq->cpu != task_cpu(p));
  1369. BUG_ON(task_current(rq, p));
  1370. BUG_ON(p->nr_cpus_allowed <= 1);
  1371. BUG_ON(!task_on_rq_queued(p));
  1372. BUG_ON(!rt_task(p));
  1373. return p;
  1374. }
  1375. /*
  1376. * If the current CPU has more than one RT task, see if the non
  1377. * running task can migrate over to a CPU that is running a task
  1378. * of lesser priority.
  1379. */
  1380. static int push_rt_task(struct rq *rq)
  1381. {
  1382. struct task_struct *next_task;
  1383. struct rq *lowest_rq;
  1384. int ret = 0;
  1385. if (!rq->rt.overloaded)
  1386. return 0;
  1387. next_task = pick_next_pushable_task(rq);
  1388. if (!next_task)
  1389. return 0;
  1390. retry:
  1391. if (unlikely(next_task == rq->curr)) {
  1392. WARN_ON(1);
  1393. return 0;
  1394. }
  1395. /*
  1396. * It's possible that the next_task slipped in of
  1397. * higher priority than current. If that's the case
  1398. * just reschedule current.
  1399. */
  1400. if (unlikely(next_task->prio < rq->curr->prio)) {
  1401. resched_curr(rq);
  1402. return 0;
  1403. }
  1404. /* We might release rq lock */
  1405. get_task_struct(next_task);
  1406. /* find_lock_lowest_rq locks the rq if found */
  1407. lowest_rq = find_lock_lowest_rq(next_task, rq);
  1408. if (!lowest_rq) {
  1409. struct task_struct *task;
  1410. /*
  1411. * find_lock_lowest_rq releases rq->lock
  1412. * so it is possible that next_task has migrated.
  1413. *
  1414. * We need to make sure that the task is still on the same
  1415. * run-queue and is also still the next task eligible for
  1416. * pushing.
  1417. */
  1418. task = pick_next_pushable_task(rq);
  1419. if (task_cpu(next_task) == rq->cpu && task == next_task) {
  1420. /*
  1421. * The task hasn't migrated, and is still the next
  1422. * eligible task, but we failed to find a run-queue
  1423. * to push it to. Do not retry in this case, since
  1424. * other cpus will pull from us when ready.
  1425. */
  1426. goto out;
  1427. }
  1428. if (!task)
  1429. /* No more tasks, just exit */
  1430. goto out;
  1431. /*
  1432. * Something has shifted, try again.
  1433. */
  1434. put_task_struct(next_task);
  1435. next_task = task;
  1436. goto retry;
  1437. }
  1438. deactivate_task(rq, next_task, 0);
  1439. set_task_cpu(next_task, lowest_rq->cpu);
  1440. activate_task(lowest_rq, next_task, 0);
  1441. ret = 1;
  1442. resched_curr(lowest_rq);
  1443. double_unlock_balance(rq, lowest_rq);
  1444. out:
  1445. put_task_struct(next_task);
  1446. return ret;
  1447. }
  1448. static void push_rt_tasks(struct rq *rq)
  1449. {
  1450. /* push_rt_task will return true if it moved an RT */
  1451. while (push_rt_task(rq))
  1452. ;
  1453. }
  1454. #ifdef HAVE_RT_PUSH_IPI
  1455. /*
  1456. * The search for the next cpu always starts at rq->cpu and ends
  1457. * when we reach rq->cpu again. It will never return rq->cpu.
  1458. * This returns the next cpu to check, or nr_cpu_ids if the loop
  1459. * is complete.
  1460. *
  1461. * rq->rt.push_cpu holds the last cpu returned by this function,
  1462. * or if this is the first instance, it must hold rq->cpu.
  1463. */
  1464. static int rto_next_cpu(struct rq *rq)
  1465. {
  1466. int prev_cpu = rq->rt.push_cpu;
  1467. int cpu;
  1468. cpu = cpumask_next(prev_cpu, rq->rd->rto_mask);
  1469. /*
  1470. * If the previous cpu is less than the rq's CPU, then it already
  1471. * passed the end of the mask, and has started from the beginning.
  1472. * We end if the next CPU is greater or equal to rq's CPU.
  1473. */
  1474. if (prev_cpu < rq->cpu) {
  1475. if (cpu >= rq->cpu)
  1476. return nr_cpu_ids;
  1477. } else if (cpu >= nr_cpu_ids) {
  1478. /*
  1479. * We passed the end of the mask, start at the beginning.
  1480. * If the result is greater or equal to the rq's CPU, then
  1481. * the loop is finished.
  1482. */
  1483. cpu = cpumask_first(rq->rd->rto_mask);
  1484. if (cpu >= rq->cpu)
  1485. return nr_cpu_ids;
  1486. }
  1487. rq->rt.push_cpu = cpu;
  1488. /* Return cpu to let the caller know if the loop is finished or not */
  1489. return cpu;
  1490. }
  1491. static int find_next_push_cpu(struct rq *rq)
  1492. {
  1493. struct rq *next_rq;
  1494. int cpu;
  1495. while (1) {
  1496. cpu = rto_next_cpu(rq);
  1497. if (cpu >= nr_cpu_ids)
  1498. break;
  1499. next_rq = cpu_rq(cpu);
  1500. /* Make sure the next rq can push to this rq */
  1501. if (next_rq->rt.highest_prio.next < rq->rt.highest_prio.curr)
  1502. break;
  1503. }
  1504. return cpu;
  1505. }
  1506. #define RT_PUSH_IPI_EXECUTING 1
  1507. #define RT_PUSH_IPI_RESTART 2
  1508. static void tell_cpu_to_push(struct rq *rq)
  1509. {
  1510. int cpu;
  1511. if (rq->rt.push_flags & RT_PUSH_IPI_EXECUTING) {
  1512. raw_spin_lock(&rq->rt.push_lock);
  1513. /* Make sure it's still executing */
  1514. if (rq->rt.push_flags & RT_PUSH_IPI_EXECUTING) {
  1515. /*
  1516. * Tell the IPI to restart the loop as things have
  1517. * changed since it started.
  1518. */
  1519. rq->rt.push_flags |= RT_PUSH_IPI_RESTART;
  1520. raw_spin_unlock(&rq->rt.push_lock);
  1521. return;
  1522. }
  1523. raw_spin_unlock(&rq->rt.push_lock);
  1524. }
  1525. /* When here, there's no IPI going around */
  1526. rq->rt.push_cpu = rq->cpu;
  1527. cpu = find_next_push_cpu(rq);
  1528. if (cpu >= nr_cpu_ids)
  1529. return;
  1530. rq->rt.push_flags = RT_PUSH_IPI_EXECUTING;
  1531. irq_work_queue_on(&rq->rt.push_work, cpu);
  1532. }
  1533. /* Called from hardirq context */
  1534. static void try_to_push_tasks(void *arg)
  1535. {
  1536. struct rt_rq *rt_rq = arg;
  1537. struct rq *rq, *src_rq;
  1538. int this_cpu;
  1539. int cpu;
  1540. this_cpu = rt_rq->push_cpu;
  1541. /* Paranoid check */
  1542. BUG_ON(this_cpu != smp_processor_id());
  1543. rq = cpu_rq(this_cpu);
  1544. src_rq = rq_of_rt_rq(rt_rq);
  1545. again:
  1546. if (has_pushable_tasks(rq)) {
  1547. raw_spin_lock(&rq->lock);
  1548. push_rt_task(rq);
  1549. raw_spin_unlock(&rq->lock);
  1550. }
  1551. /* Pass the IPI to the next rt overloaded queue */
  1552. raw_spin_lock(&rt_rq->push_lock);
  1553. /*
  1554. * If the source queue changed since the IPI went out,
  1555. * we need to restart the search from that CPU again.
  1556. */
  1557. if (rt_rq->push_flags & RT_PUSH_IPI_RESTART) {
  1558. rt_rq->push_flags &= ~RT_PUSH_IPI_RESTART;
  1559. rt_rq->push_cpu = src_rq->cpu;
  1560. }
  1561. cpu = find_next_push_cpu(src_rq);
  1562. if (cpu >= nr_cpu_ids)
  1563. rt_rq->push_flags &= ~RT_PUSH_IPI_EXECUTING;
  1564. raw_spin_unlock(&rt_rq->push_lock);
  1565. if (cpu >= nr_cpu_ids)
  1566. return;
  1567. /*
  1568. * It is possible that a restart caused this CPU to be
  1569. * chosen again. Don't bother with an IPI, just see if we
  1570. * have more to push.
  1571. */
  1572. if (unlikely(cpu == rq->cpu))
  1573. goto again;
  1574. /* Try the next RT overloaded CPU */
  1575. irq_work_queue_on(&rt_rq->push_work, cpu);
  1576. }
  1577. static void push_irq_work_func(struct irq_work *work)
  1578. {
  1579. struct rt_rq *rt_rq = container_of(work, struct rt_rq, push_work);
  1580. try_to_push_tasks(rt_rq);
  1581. }
  1582. #endif /* HAVE_RT_PUSH_IPI */
  1583. static void pull_rt_task(struct rq *this_rq)
  1584. {
  1585. int this_cpu = this_rq->cpu, cpu;
  1586. bool resched = false;
  1587. struct task_struct *p;
  1588. struct rq *src_rq;
  1589. if (likely(!rt_overloaded(this_rq)))
  1590. return;
  1591. /*
  1592. * Match the barrier from rt_set_overloaded; this guarantees that if we
  1593. * see overloaded we must also see the rto_mask bit.
  1594. */
  1595. smp_rmb();
  1596. #ifdef HAVE_RT_PUSH_IPI
  1597. if (sched_feat(RT_PUSH_IPI)) {
  1598. tell_cpu_to_push(this_rq);
  1599. return;
  1600. }
  1601. #endif
  1602. for_each_cpu(cpu, this_rq->rd->rto_mask) {
  1603. if (this_cpu == cpu)
  1604. continue;
  1605. src_rq = cpu_rq(cpu);
  1606. /*
  1607. * Don't bother taking the src_rq->lock if the next highest
  1608. * task is known to be lower-priority than our current task.
  1609. * This may look racy, but if this value is about to go
  1610. * logically higher, the src_rq will push this task away.
  1611. * And if its going logically lower, we do not care
  1612. */
  1613. if (src_rq->rt.highest_prio.next >=
  1614. this_rq->rt.highest_prio.curr)
  1615. continue;
  1616. /*
  1617. * We can potentially drop this_rq's lock in
  1618. * double_lock_balance, and another CPU could
  1619. * alter this_rq
  1620. */
  1621. double_lock_balance(this_rq, src_rq);
  1622. /*
  1623. * We can pull only a task, which is pushable
  1624. * on its rq, and no others.
  1625. */
  1626. p = pick_highest_pushable_task(src_rq, this_cpu);
  1627. /*
  1628. * Do we have an RT task that preempts
  1629. * the to-be-scheduled task?
  1630. */
  1631. if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
  1632. WARN_ON(p == src_rq->curr);
  1633. WARN_ON(!task_on_rq_queued(p));
  1634. /*
  1635. * There's a chance that p is higher in priority
  1636. * than what's currently running on its cpu.
  1637. * This is just that p is wakeing up and hasn't
  1638. * had a chance to schedule. We only pull
  1639. * p if it is lower in priority than the
  1640. * current task on the run queue
  1641. */
  1642. if (p->prio < src_rq->curr->prio)
  1643. goto skip;
  1644. resched = true;
  1645. deactivate_task(src_rq, p, 0);
  1646. set_task_cpu(p, this_cpu);
  1647. activate_task(this_rq, p, 0);
  1648. /*
  1649. * We continue with the search, just in
  1650. * case there's an even higher prio task
  1651. * in another runqueue. (low likelihood
  1652. * but possible)
  1653. */
  1654. }
  1655. skip:
  1656. double_unlock_balance(this_rq, src_rq);
  1657. }
  1658. if (resched)
  1659. resched_curr(this_rq);
  1660. }
  1661. /*
  1662. * If we are not running and we are not going to reschedule soon, we should
  1663. * try to push tasks away now
  1664. */
  1665. static void task_woken_rt(struct rq *rq, struct task_struct *p)
  1666. {
  1667. if (!task_running(rq, p) &&
  1668. !test_tsk_need_resched(rq->curr) &&
  1669. p->nr_cpus_allowed > 1 &&
  1670. (dl_task(rq->curr) || rt_task(rq->curr)) &&
  1671. (rq->curr->nr_cpus_allowed < 2 ||
  1672. rq->curr->prio <= p->prio))
  1673. push_rt_tasks(rq);
  1674. }
  1675. /* Assumes rq->lock is held */
  1676. static void rq_online_rt(struct rq *rq)
  1677. {
  1678. if (rq->rt.overloaded)
  1679. rt_set_overload(rq);
  1680. __enable_runtime(rq);
  1681. cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
  1682. }
  1683. /* Assumes rq->lock is held */
  1684. static void rq_offline_rt(struct rq *rq)
  1685. {
  1686. if (rq->rt.overloaded)
  1687. rt_clear_overload(rq);
  1688. __disable_runtime(rq);
  1689. cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
  1690. }
  1691. /*
  1692. * When switch from the rt queue, we bring ourselves to a position
  1693. * that we might want to pull RT tasks from other runqueues.
  1694. */
  1695. static void switched_from_rt(struct rq *rq, struct task_struct *p)
  1696. {
  1697. /*
  1698. * If there are other RT tasks then we will reschedule
  1699. * and the scheduling of the other RT tasks will handle
  1700. * the balancing. But if we are the last RT task
  1701. * we may need to handle the pulling of RT tasks
  1702. * now.
  1703. */
  1704. if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
  1705. return;
  1706. queue_pull_task(rq);
  1707. }
  1708. void __init init_sched_rt_class(void)
  1709. {
  1710. unsigned int i;
  1711. for_each_possible_cpu(i) {
  1712. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
  1713. GFP_KERNEL, cpu_to_node(i));
  1714. }
  1715. }
  1716. #endif /* CONFIG_SMP */
  1717. /*
  1718. * When switching a task to RT, we may overload the runqueue
  1719. * with RT tasks. In this case we try to push them off to
  1720. * other runqueues.
  1721. */
  1722. static void switched_to_rt(struct rq *rq, struct task_struct *p)
  1723. {
  1724. /*
  1725. * If we are already running, then there's nothing
  1726. * that needs to be done. But if we are not running
  1727. * we may need to preempt the current running task.
  1728. * If that current running task is also an RT task
  1729. * then see if we can move to another run queue.
  1730. */
  1731. if (task_on_rq_queued(p) && rq->curr != p) {
  1732. #ifdef CONFIG_SMP
  1733. if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
  1734. queue_push_tasks(rq);
  1735. #else
  1736. if (p->prio < rq->curr->prio)
  1737. resched_curr(rq);
  1738. #endif /* CONFIG_SMP */
  1739. }
  1740. }
  1741. /*
  1742. * Priority of the task has changed. This may cause
  1743. * us to initiate a push or pull.
  1744. */
  1745. static void
  1746. prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
  1747. {
  1748. if (!task_on_rq_queued(p))
  1749. return;
  1750. if (rq->curr == p) {
  1751. #ifdef CONFIG_SMP
  1752. /*
  1753. * If our priority decreases while running, we
  1754. * may need to pull tasks to this runqueue.
  1755. */
  1756. if (oldprio < p->prio)
  1757. queue_pull_task(rq);
  1758. /*
  1759. * If there's a higher priority task waiting to run
  1760. * then reschedule.
  1761. */
  1762. if (p->prio > rq->rt.highest_prio.curr)
  1763. resched_curr(rq);
  1764. #else
  1765. /* For UP simply resched on drop of prio */
  1766. if (oldprio < p->prio)
  1767. resched_curr(rq);
  1768. #endif /* CONFIG_SMP */
  1769. } else {
  1770. /*
  1771. * This task is not running, but if it is
  1772. * greater than the current running task
  1773. * then reschedule.
  1774. */
  1775. if (p->prio < rq->curr->prio)
  1776. resched_curr(rq);
  1777. }
  1778. }
  1779. static void watchdog(struct rq *rq, struct task_struct *p)
  1780. {
  1781. unsigned long soft, hard;
  1782. /* max may change after cur was read, this will be fixed next tick */
  1783. soft = task_rlimit(p, RLIMIT_RTTIME);
  1784. hard = task_rlimit_max(p, RLIMIT_RTTIME);
  1785. if (soft != RLIM_INFINITY) {
  1786. unsigned long next;
  1787. if (p->rt.watchdog_stamp != jiffies) {
  1788. p->rt.timeout++;
  1789. p->rt.watchdog_stamp = jiffies;
  1790. }
  1791. next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
  1792. if (p->rt.timeout > next)
  1793. p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
  1794. }
  1795. }
  1796. static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
  1797. {
  1798. struct sched_rt_entity *rt_se = &p->rt;
  1799. update_curr_rt(rq);
  1800. watchdog(rq, p);
  1801. /*
  1802. * RR tasks need a special form of timeslice management.
  1803. * FIFO tasks have no timeslices.
  1804. */
  1805. if (p->policy != SCHED_RR)
  1806. return;
  1807. if (--p->rt.time_slice)
  1808. return;
  1809. p->rt.time_slice = sched_rr_timeslice;
  1810. /*
  1811. * Requeue to the end of queue if we (and all of our ancestors) are not
  1812. * the only element on the queue
  1813. */
  1814. for_each_sched_rt_entity(rt_se) {
  1815. if (rt_se->run_list.prev != rt_se->run_list.next) {
  1816. requeue_task_rt(rq, p, 0);
  1817. resched_curr(rq);
  1818. return;
  1819. }
  1820. }
  1821. }
  1822. static void set_curr_task_rt(struct rq *rq)
  1823. {
  1824. struct task_struct *p = rq->curr;
  1825. p->se.exec_start = rq_clock_task(rq);
  1826. /* The running task is never eligible for pushing */
  1827. dequeue_pushable_task(rq, p);
  1828. }
  1829. static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
  1830. {
  1831. /*
  1832. * Time slice is 0 for SCHED_FIFO tasks
  1833. */
  1834. if (task->policy == SCHED_RR)
  1835. return sched_rr_timeslice;
  1836. else
  1837. return 0;
  1838. }
  1839. const struct sched_class rt_sched_class = {
  1840. .next = &fair_sched_class,
  1841. .enqueue_task = enqueue_task_rt,
  1842. .dequeue_task = dequeue_task_rt,
  1843. .yield_task = yield_task_rt,
  1844. .check_preempt_curr = check_preempt_curr_rt,
  1845. .pick_next_task = pick_next_task_rt,
  1846. .put_prev_task = put_prev_task_rt,
  1847. #ifdef CONFIG_SMP
  1848. .select_task_rq = select_task_rq_rt,
  1849. .set_cpus_allowed = set_cpus_allowed_common,
  1850. .rq_online = rq_online_rt,
  1851. .rq_offline = rq_offline_rt,
  1852. .task_woken = task_woken_rt,
  1853. .switched_from = switched_from_rt,
  1854. #endif
  1855. .set_curr_task = set_curr_task_rt,
  1856. .task_tick = task_tick_rt,
  1857. .get_rr_interval = get_rr_interval_rt,
  1858. .prio_changed = prio_changed_rt,
  1859. .switched_to = switched_to_rt,
  1860. .update_curr = update_curr_rt,
  1861. };
  1862. #ifdef CONFIG_SCHED_DEBUG
  1863. extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
  1864. void print_rt_stats(struct seq_file *m, int cpu)
  1865. {
  1866. rt_rq_iter_t iter;
  1867. struct rt_rq *rt_rq;
  1868. rcu_read_lock();
  1869. for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
  1870. print_rt_rq(m, cpu, rt_rq);
  1871. rcu_read_unlock();
  1872. }
  1873. #endif /* CONFIG_SCHED_DEBUG */