sched_rt.c 42 KB

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