sched_rt.c 31 KB

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