sched_rt.c 36 KB

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