rt.c 53 KB

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