rt.c 47 KB

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