rt.c 47 KB

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