sched_rt.c 33 KB

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