rt.c 56 KB

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