rt.c 47 KB

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