sched_rt.c 40 KB

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