sched_rt.c 31 KB

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