sched_rt.c 33 KB

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