drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / sched / rt.c
1 /*
2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
3 * policies)
4 */
5
6 #include "sched.h"
7 #if defined(CONFIG_HMP_TRACER) || \
8 defined(CONFIG_MT_RT_SCHED_CRIT) || defined(CONFIG_MT_RT_SCHED_NOTICE)
9 #include <trace/events/sched.h>
10 #endif
11
12 #include <linux/slab.h>
13
14 int sched_rr_timeslice = RR_TIMESLICE;
15
16 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
17
18 struct rt_bandwidth def_rt_bandwidth;
19
20 static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
21 {
22 struct rt_bandwidth *rt_b =
23 container_of(timer, struct rt_bandwidth, rt_period_timer);
24 ktime_t now;
25 int overrun;
26 int idle = 0;
27
28 for (;;) {
29 now = hrtimer_cb_get_time(timer);
30 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
31
32 if (!overrun)
33 break;
34
35 idle = do_sched_rt_period_timer(rt_b, overrun);
36 }
37
38 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
39 }
40
41 void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
42 {
43 rt_b->rt_period = ns_to_ktime(period);
44 rt_b->rt_runtime = runtime;
45
46 raw_spin_lock_init(&rt_b->rt_runtime_lock);
47
48 hrtimer_init(&rt_b->rt_period_timer,
49 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
50 rt_b->rt_period_timer.function = sched_rt_period_timer;
51 }
52
53 static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
54 {
55 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
56 return;
57
58 if (hrtimer_active(&rt_b->rt_period_timer))
59 return;
60
61 raw_spin_lock(&rt_b->rt_runtime_lock);
62 start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period);
63 raw_spin_unlock(&rt_b->rt_runtime_lock);
64 }
65
66 #ifdef CONFIG_PROVE_LOCKING
67 DEFINE_RAW_SPINLOCK(rt_rq_runtime_spinlock);
68 #define MAX_SPIN_KEY 10
69 DEFINE_PER_CPU(struct lock_class_key, spin_key[MAX_SPIN_KEY]);
70 DEFINE_PER_CPU(int, spin_key_idx);
71 #endif
72 void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
73 {
74 struct rt_prio_array *array;
75 int i;
76 #ifdef CONFIG_PROVE_LOCKING
77 int cpu, idx;
78 #endif
79
80 array = &rt_rq->active;
81 for (i = 0; i < MAX_RT_PRIO; i++) {
82 INIT_LIST_HEAD(array->queue + i);
83 __clear_bit(i, array->bitmap);
84 }
85 /* delimiter for bitsearch: */
86 __set_bit(MAX_RT_PRIO, array->bitmap);
87
88 #if defined CONFIG_SMP
89 rt_rq->highest_prio.curr = MAX_RT_PRIO;
90 rt_rq->highest_prio.next = MAX_RT_PRIO;
91 rt_rq->rt_nr_migratory = 0;
92 rt_rq->overloaded = 0;
93 plist_head_init(&rt_rq->pushable_tasks);
94 #endif
95
96 rt_rq->rt_time = 0;
97 rt_rq->rt_throttled = 0;
98 rt_rq->rt_runtime = 0;
99 /* MTK patch: prevent to continue borrow RT runtime after restore the default value*/
100 rt_rq->rt_disable_borrow = 0;
101 #ifdef CONFIG_PROVE_LOCKING
102 raw_spin_lock(&rt_rq_runtime_spinlock);
103 cpu = rq->cpu;
104 idx = per_cpu(spin_key_idx, cpu);
105 #endif
106 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
107 #ifdef CONFIG_PROVE_LOCKING
108 lockdep_set_class(&rt_rq->rt_runtime_lock, &per_cpu(spin_key[idx], cpu));
109 per_cpu(spin_key_idx, cpu)++;
110 BUG_ON(per_cpu(spin_key_idx, cpu) >= MAX_SPIN_KEY);
111 raw_spin_unlock(&rt_rq_runtime_spinlock);
112 #endif
113 }
114
115 #ifdef CONFIG_RT_GROUP_SCHED
116 static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
117 {
118 hrtimer_cancel(&rt_b->rt_period_timer);
119 }
120
121 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
122
123 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
124 {
125 #ifdef CONFIG_SCHED_DEBUG
126 WARN_ON_ONCE(!rt_entity_is_task(rt_se));
127 #endif
128 return container_of(rt_se, struct task_struct, rt);
129 }
130
131 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
132 {
133 return rt_rq->rq;
134 }
135
136 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
137 {
138 return rt_se->rt_rq;
139 }
140
141 void free_rt_sched_group(struct task_group *tg)
142 {
143 int i;
144
145 if (tg->rt_se)
146 destroy_rt_bandwidth(&tg->rt_bandwidth);
147
148 for_each_possible_cpu(i) {
149 if (tg->rt_rq)
150 kfree(tg->rt_rq[i]);
151 if (tg->rt_se)
152 kfree(tg->rt_se[i]);
153 }
154
155 kfree(tg->rt_rq);
156 kfree(tg->rt_se);
157 }
158
159 void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
160 struct sched_rt_entity *rt_se, int cpu,
161 struct sched_rt_entity *parent)
162 {
163 struct rq *rq = cpu_rq(cpu);
164
165 rt_rq->highest_prio.curr = MAX_RT_PRIO;
166 rt_rq->rt_nr_boosted = 0;
167 rt_rq->rq = rq;
168 rt_rq->tg = tg;
169
170 tg->rt_rq[cpu] = rt_rq;
171 tg->rt_se[cpu] = rt_se;
172
173 if (!rt_se)
174 return;
175
176 if (!parent)
177 rt_se->rt_rq = &rq->rt;
178 else
179 rt_se->rt_rq = parent->my_q;
180
181 rt_se->my_q = rt_rq;
182 rt_se->parent = parent;
183 INIT_LIST_HEAD(&rt_se->run_list);
184 }
185
186 int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
187 {
188 struct rt_rq *rt_rq;
189 struct sched_rt_entity *rt_se;
190 int i;
191
192 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
193 if (!tg->rt_rq)
194 goto err;
195 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
196 if (!tg->rt_se)
197 goto err;
198
199 init_rt_bandwidth(&tg->rt_bandwidth,
200 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
201
202 for_each_possible_cpu(i) {
203 rt_rq = kzalloc_node(sizeof(struct rt_rq),
204 GFP_KERNEL, cpu_to_node(i));
205 if (!rt_rq)
206 goto err;
207
208 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
209 GFP_KERNEL, cpu_to_node(i));
210 if (!rt_se)
211 goto err_free_rq;
212
213 init_rt_rq(rt_rq, cpu_rq(i));
214 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
215 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
216 }
217
218 return 1;
219
220 err_free_rq:
221 kfree(rt_rq);
222 err:
223 return 0;
224 }
225
226 #else /* CONFIG_RT_GROUP_SCHED */
227
228 #define rt_entity_is_task(rt_se) (1)
229
230 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
231 {
232 return container_of(rt_se, struct task_struct, rt);
233 }
234
235 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
236 {
237 return container_of(rt_rq, struct rq, rt);
238 }
239
240 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
241 {
242 struct task_struct *p = rt_task_of(rt_se);
243 struct rq *rq = task_rq(p);
244
245 return &rq->rt;
246 }
247
248 void free_rt_sched_group(struct task_group *tg) { }
249
250 int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
251 {
252 return 1;
253 }
254 #endif /* CONFIG_RT_GROUP_SCHED */
255
256 #if defined(CONFIG_MT_RT_SCHED) || defined(CONFIG_MT_RT_SCHED_LOG)
257 extern struct cpumask hmp_fast_cpu_mask;
258 extern struct cpumask hmp_slow_cpu_mask;
259 #endif
260
261 #ifdef CONFIG_SMP
262
263 static inline int rt_overloaded(struct rq *rq)
264 {
265 return atomic_read(&rq->rd->rto_count);
266 }
267
268 static inline void rt_set_overload(struct rq *rq)
269 {
270 if (!rq->online)
271 return;
272
273 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
274 /*
275 * Make sure the mask is visible before we set
276 * the overload count. That is checked to determine
277 * if we should look at the mask. It would be a shame
278 * if we looked at the mask, but the mask was not
279 * updated yet.
280 */
281 wmb();
282 atomic_inc(&rq->rd->rto_count);
283 }
284
285 static inline void rt_clear_overload(struct rq *rq)
286 {
287 if (!rq->online)
288 return;
289
290 /* the order here really doesn't matter */
291 atomic_dec(&rq->rd->rto_count);
292 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
293 }
294
295 #ifdef CONFIG_MT_RT_SCHED
296 #if 0
297 static inline int rt_overloaded_in_big(struct rq *rq)
298 {
299 cpumask_var_t new_mask;
300 cpumask_and(new_mask, rq->rd->rto_mask, &hmp_fast_cpu_mask);
301 #ifdef CONFIG_MT_RT_SCHED_INFO
302 mt_rt_printf("rt_overloaded_in_big %lu:%lu:%lu",
303 new_mask->bits[0], rq->rd->rto_mask->bits[0], hmp_fast_cpu_mask.bits[0]);
304
305 #endif
306 if (cpumask_empty(new_mask))
307 return 0;
308
309 return 1;
310 }
311 #endif
312 static inline int has_rt_task_in_little(void)
313 {
314 int cpu;
315 struct rq *rq;
316
317 for_each_cpu(cpu, &hmp_slow_cpu_mask){
318 if (!cpu_online(cpu))
319 continue;
320
321 rq = cpu_rq(cpu);
322 if(rq->rt.rt_nr_running >= 1)
323 return 1;
324 }
325
326 return 0;
327 }
328 #endif
329
330 static void update_rt_migration(struct rt_rq *rt_rq)
331 {
332 if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
333 if (!rt_rq->overloaded) {
334 rt_set_overload(rq_of_rt_rq(rt_rq));
335 rt_rq->overloaded = 1;
336 }
337 } else if (rt_rq->overloaded) {
338 rt_clear_overload(rq_of_rt_rq(rt_rq));
339 rt_rq->overloaded = 0;
340 }
341 }
342
343 static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
344 {
345 struct task_struct *p;
346
347 if (!rt_entity_is_task(rt_se))
348 return;
349
350 p = rt_task_of(rt_se);
351 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
352
353 rt_rq->rt_nr_total++;
354 if (p->nr_cpus_allowed > 1)
355 rt_rq->rt_nr_migratory++;
356
357 update_rt_migration(rt_rq);
358 }
359
360 static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
361 {
362 struct task_struct *p;
363
364 if (!rt_entity_is_task(rt_se))
365 return;
366
367 p = rt_task_of(rt_se);
368 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
369
370 rt_rq->rt_nr_total--;
371 if (p->nr_cpus_allowed > 1)
372 rt_rq->rt_nr_migratory--;
373
374 update_rt_migration(rt_rq);
375 }
376
377 static inline int has_pushable_tasks(struct rq *rq)
378 {
379 return !plist_head_empty(&rq->rt.pushable_tasks);
380 }
381
382 static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
383 {
384 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
385 plist_node_init(&p->pushable_tasks, p->prio);
386 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
387
388 /* Update the highest prio pushable task */
389 if (p->prio < rq->rt.highest_prio.next)
390 rq->rt.highest_prio.next = p->prio;
391 }
392
393 static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
394 {
395 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
396
397 /* Update the new highest prio pushable task */
398 if (has_pushable_tasks(rq)) {
399 p = plist_first_entry(&rq->rt.pushable_tasks,
400 struct task_struct, pushable_tasks);
401 rq->rt.highest_prio.next = p->prio;
402 } else
403 rq->rt.highest_prio.next = MAX_RT_PRIO;
404 }
405
406 #else
407
408 static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
409 {
410 }
411
412 static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
413 {
414 }
415
416 static inline
417 void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
418 {
419 }
420
421 static inline
422 void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
423 {
424 }
425
426 #endif /* CONFIG_SMP */
427
428 static inline int on_rt_rq(struct sched_rt_entity *rt_se)
429 {
430 return !list_empty(&rt_se->run_list);
431 }
432
433 #ifdef CONFIG_RT_GROUP_SCHED
434
435 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
436 {
437 if (!rt_rq->tg)
438 return RUNTIME_INF;
439
440 return rt_rq->rt_runtime;
441 }
442
443 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
444 {
445 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
446 }
447
448 typedef struct task_group *rt_rq_iter_t;
449
450 static inline struct task_group *next_task_group(struct task_group *tg)
451 {
452 do {
453 tg = list_entry_rcu(tg->list.next,
454 typeof(struct task_group), list);
455 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
456
457 if (&tg->list == &task_groups)
458 tg = NULL;
459
460 return tg;
461 }
462
463 #define for_each_rt_rq(rt_rq, iter, rq) \
464 for (iter = container_of(&task_groups, typeof(*iter), list); \
465 (iter = next_task_group(iter)) && \
466 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
467
468 static inline void list_add_leaf_rt_rq(struct rt_rq *rt_rq)
469 {
470 list_add_rcu(&rt_rq->leaf_rt_rq_list,
471 &rq_of_rt_rq(rt_rq)->leaf_rt_rq_list);
472 }
473
474 static inline void list_del_leaf_rt_rq(struct rt_rq *rt_rq)
475 {
476 list_del_rcu(&rt_rq->leaf_rt_rq_list);
477 }
478
479 #define for_each_leaf_rt_rq(rt_rq, rq) \
480 list_for_each_entry_rcu(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
481
482 #define for_each_sched_rt_entity(rt_se) \
483 for (; rt_se; rt_se = rt_se->parent)
484
485 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
486 {
487 return rt_se->my_q;
488 }
489
490 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head);
491 static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
492
493 static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
494 {
495 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
496 struct sched_rt_entity *rt_se;
497
498 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
499
500 rt_se = rt_rq->tg->rt_se[cpu];
501
502 if (rt_rq->rt_nr_running) {
503 if (rt_se && !on_rt_rq(rt_se))
504 enqueue_rt_entity(rt_se, false);
505 if (rt_rq->highest_prio.curr < curr->prio)
506 resched_task(curr);
507 }
508 }
509
510 static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
511 {
512 struct sched_rt_entity *rt_se;
513 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
514
515 rt_se = rt_rq->tg->rt_se[cpu];
516
517 if (rt_se && on_rt_rq(rt_se))
518 dequeue_rt_entity(rt_se);
519 }
520
521 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
522 {
523 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
524 }
525
526 static int rt_se_boosted(struct sched_rt_entity *rt_se)
527 {
528 struct rt_rq *rt_rq = group_rt_rq(rt_se);
529 struct task_struct *p;
530
531 if (rt_rq)
532 return !!rt_rq->rt_nr_boosted;
533
534 p = rt_task_of(rt_se);
535 return p->prio != p->normal_prio;
536 }
537
538 #ifdef CONFIG_SMP
539 static inline const struct cpumask *sched_rt_period_mask(void)
540 {
541 return cpu_rq(smp_processor_id())->rd->span;
542 }
543 #else
544 static inline const struct cpumask *sched_rt_period_mask(void)
545 {
546 return cpu_online_mask;
547 }
548 #endif
549
550 static inline
551 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
552 {
553 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
554 }
555
556 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
557 {
558 return &rt_rq->tg->rt_bandwidth;
559 }
560
561 void unthrottle_offline_rt_rqs(struct rq *rq) {
562 struct rt_rq *rt_rq;
563
564 for_each_leaf_rt_rq(rt_rq, rq) {
565 /*
566 * clock_task is not advancing so we just need to make sure
567 * there's some valid quota amount
568 */
569 if (rt_rq_throttled(rt_rq)){
570 rt_rq->rt_throttled = 0;
571 printk(KERN_ERR "sched: RT throttling inactivated\n");
572 }
573 }
574 }
575
576 #else /* !CONFIG_RT_GROUP_SCHED */
577
578 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
579 {
580 return rt_rq->rt_runtime;
581 }
582
583 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
584 {
585 return ktime_to_ns(def_rt_bandwidth.rt_period);
586 }
587
588 typedef struct rt_rq *rt_rq_iter_t;
589
590 #define for_each_rt_rq(rt_rq, iter, rq) \
591 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
592
593 static inline void list_add_leaf_rt_rq(struct rt_rq *rt_rq)
594 {
595 }
596
597 static inline void list_del_leaf_rt_rq(struct rt_rq *rt_rq)
598 {
599 }
600
601 #define for_each_leaf_rt_rq(rt_rq, rq) \
602 for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
603
604 #define for_each_sched_rt_entity(rt_se) \
605 for (; rt_se; rt_se = NULL)
606
607 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
608 {
609 return NULL;
610 }
611
612 static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
613 {
614 if (rt_rq->rt_nr_running)
615 resched_task(rq_of_rt_rq(rt_rq)->curr);
616 }
617
618 static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
619 {
620 }
621
622 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
623 {
624 return rt_rq->rt_throttled;
625 }
626
627 static inline const struct cpumask *sched_rt_period_mask(void)
628 {
629 return cpu_online_mask;
630 }
631
632 static inline
633 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
634 {
635 return &cpu_rq(cpu)->rt;
636 }
637
638 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
639 {
640 return &def_rt_bandwidth;
641 }
642
643 void unthrottle_offline_rt_rqs(struct rq *rq) { }
644
645 #endif /* CONFIG_RT_GROUP_SCHED */
646
647 #ifdef CONFIG_SMP
648 /*
649 * We ran out of runtime, see if we can borrow some from our neighbours.
650 */
651 //#define MTK_DEBUG_CGROUP
652 static int do_balance_runtime(struct rt_rq *rt_rq)
653 {
654 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
655 struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
656 int i, weight, more = 0;
657 u64 rt_period;
658
659 weight = cpumask_weight(rd->span);
660
661 raw_spin_lock(&rt_b->rt_runtime_lock);
662 raw_spin_lock(&rt_rq->rt_runtime_lock);
663
664 if (rt_rq->rt_disable_borrow ==1){
665 raw_spin_unlock(&rt_rq->rt_runtime_lock);
666 raw_spin_unlock(&rt_b->rt_runtime_lock);
667 return 0;
668 }
669 rt_period = ktime_to_ns(rt_b->rt_period);
670
671 #ifdef MTK_DEBUG_CGROUP
672 printk(KERN_EMERG " do_balance_runtime curr_cpu=%d, dst_cpu=%d, span=%lu\n",
673 smp_processor_id(), rt_rq->rq->cpu, rd->span->bits[0]);
674 #endif
675 for_each_cpu(i, rd->span) {
676 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
677 s64 diff;
678
679 if (iter == rt_rq)
680 continue;
681
682 /* MTK Patch: use try lock to prevent deadlock */
683 // raw_spin_lock(&iter->rt_runtime_lock);
684 #ifdef MTK_DEBUG_CGROUP
685 printk(KERN_EMERG " do_balance_runtime get lock cpu=%d\n", i);
686 #endif
687 if(!raw_spin_trylock(&iter->rt_runtime_lock)){
688 #ifdef MTK_DEBUG_CGROUP
689 printk(KERN_EMERG " do_balance_runtime try lock fail cpu=%d\n", i);
690 #endif
691 continue;
692 }
693 /*
694 * Either all rqs have inf runtime and there's nothing to steal
695 * or __disable_runtime() below sets a specific rq to inf to
696 * indicate its been disabled and disalow stealing.
697 */
698 if (iter->rt_disable_borrow ==1)
699 goto next;
700 if (iter->rt_runtime == RUNTIME_INF)
701 goto next;
702
703 /*
704 * From runqueues with spare time, take 1/n part of their
705 * spare time, but no more than our period.
706 */
707 diff = iter->rt_runtime - iter->rt_time;
708
709 #ifdef MTK_DEBUG_CGROUP
710 printk(KERN_EMERG "borrow, dst_cpu=%d, src_cpu=%d, src_cpu2=%d, src_addr=%x, dst_addr=%x,dst->rt_runtime=%llu, src->rt_runtime=%llu, diff=%lld, span=%lu\n",
711 rt_rq->rq->cpu, i, iter->rq->cpu, iter,
712 rt_rq, rt_rq->rt_runtime, iter->rt_runtime, diff, rd->span->bits[0]);
713 #endif
714 if (diff > 0) {
715 diff = div_u64((u64)diff, weight);
716 if (rt_rq->rt_runtime + diff > rt_period)
717 diff = rt_period - rt_rq->rt_runtime;
718 iter->rt_runtime -= diff;
719 rt_rq->rt_runtime += diff;
720 more = 1;
721 #ifdef MTK_DEBUG_CGROUP
722 printk(KERN_EMERG "borrow successfully, dst_cpu=%d, src_cpu=%d, src_cpu2=%d, src_addr=%x, dst_addr=%x,dst->rt_runtime=%llu, src->rt_runtime=%llu, diff=%lld, span=%lu\n",
723 rt_rq->rq->cpu, i, iter->rq->cpu, iter,
724 rt_rq, rt_rq->rt_runtime, iter->rt_runtime, diff, rd->span->bits[0]);
725 #endif
726 if (rt_rq->rt_runtime == rt_period) {
727 raw_spin_unlock(&iter->rt_runtime_lock);
728 break;
729 }
730 }
731 next:
732 raw_spin_unlock(&iter->rt_runtime_lock);
733 }
734 raw_spin_unlock(&rt_rq->rt_runtime_lock);
735 raw_spin_unlock(&rt_b->rt_runtime_lock);
736
737 return more;
738 }
739
740 /*
741 * Ensure this RQ takes back all the runtime it lend to its neighbours.
742 */
743 static void __disable_runtime(struct rq *rq)
744 {
745 struct root_domain *rd = rq->rd;
746 rt_rq_iter_t iter;
747 struct rt_rq *rt_rq;
748
749 if (unlikely(!scheduler_running))
750 return;
751
752 for_each_rt_rq(rt_rq, iter, rq) {
753 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
754 s64 want;
755 int i;
756
757 raw_spin_lock(&rt_b->rt_runtime_lock);
758 raw_spin_lock(&rt_rq->rt_runtime_lock);
759 /* MTK Patch: prevent race condition */
760 rt_rq->rt_disable_borrow = 1;
761 /*
762 * Either we're all inf and nobody needs to borrow, or we're
763 * already disabled and thus have nothing to do, or we have
764 * exactly the right amount of runtime to take out.
765 */
766 #ifdef MTK_DEBUG_CGROUP
767 printk(KERN_EMERG "0. disable_runtime, cpu=%d, rd->span=%lu, rt_rq_addr=%x, rt_rq->rt_runtime=%llu, rt_b->rt_runtime=%llu\n",
768 rt_rq->rq->cpu, rd->span->bits[0],
769 rt_rq, rt_rq->rt_runtime, rt_b->rt_runtime);
770 #endif
771 if (rt_rq->rt_runtime == RUNTIME_INF ||
772 rt_rq->rt_runtime == rt_b->rt_runtime)
773 goto balanced;
774 raw_spin_unlock(&rt_rq->rt_runtime_lock);
775
776 /*
777 * Calculate the difference between what we started out with
778 * and what we current have, that's the amount of runtime
779 * we lend and now have to reclaim.
780 */
781 want = rt_b->rt_runtime - rt_rq->rt_runtime;
782
783 /*
784 * Greedy reclaim, take back as much as we can.
785 */
786 for_each_cpu(i, rd->span) {
787 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
788 s64 diff;
789
790 #ifdef MTK_DEBUG_CGROUP
791 printk(KERN_EMERG "0. disable_runtime, cpu=%d,rt_b->rt_runtime=%llu, rt_rq->rt_runtime=%llu, want=%lld, rd->span=%lu\n",
792 rt_rq->rq->cpu, rt_b->rt_runtime, rt_rq->rt_runtime, want, rd->span->bits[0]);
793 #endif
794
795 /*
796 * Can't reclaim from ourselves or disabled runqueues.
797 */
798 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF || iter->rt_disable_borrow){
799 #ifdef MTK_DEBUG_CGROUP
800 printk(KERN_EMERG "1. disable_runtime, cpu=%d, %llu\n",
801 i, iter->rt_runtime);
802 #endif
803 continue;
804 }
805
806 raw_spin_lock(&iter->rt_runtime_lock);
807 #ifdef MTK_DEBUG_CGROUP
808 printk(KERN_EMERG "2-1. disable_runtime cpu=%d, want=%lld, iter->rt_runtime=%llu\n",
809 i, want, iter->rt_runtime);
810 #endif
811 if (want > 0) {
812 diff = min_t(s64, iter->rt_runtime, want);
813 iter->rt_runtime -= diff;
814 want -= diff;
815 #ifdef MTK_DEBUG_CGROUP
816 printk(KERN_EMERG "2. disable_runtime, rt_runtime=%llu, diff=%lld, want=%lld\n",
817 iter->rt_runtime, diff, want);
818 #endif
819 } else {
820 iter->rt_runtime -= want;
821 want -= want;
822 #ifdef MTK_DEBUG_CGROUP
823 printk(KERN_EMERG "3. disable_runtime, rt_runtime=%llu, want=%lld\n", iter->rt_runtime, want);
824 #endif
825 }
826 raw_spin_unlock(&iter->rt_runtime_lock);
827
828 if (!want)
829 break;
830 }
831
832 raw_spin_lock(&rt_rq->rt_runtime_lock);
833 /*
834 * We cannot be left wanting - that would mean some runtime
835 * leaked out of the system.
836 */
837 if(want){
838 #ifdef MTK_DEBUG_CGROUP
839 printk(KERN_EMERG "4. disable_runtime, want=%lld, rt_rq->rt_runtime=%llu\n",
840 want, rt_rq->rt_runtime);
841 {
842 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, 0);
843 printk(KERN_EMERG "4-0. disable_runtime %llu\n", iter->rt_runtime);
844 iter = sched_rt_period_rt_rq(rt_b, 1);
845 printk(KERN_EMERG "4-1. disable_runtime %llu\n", iter->rt_runtime);
846 iter = sched_rt_period_rt_rq(rt_b, 2);
847 printk(KERN_EMERG "4-2. disable_runtime %llu\n", iter->rt_runtime);
848 iter = sched_rt_period_rt_rq(rt_b, 3);
849 printk(KERN_EMERG "4-3. disable_runtime %llu\n", iter->rt_runtime);
850 }
851 #endif
852
853 BUG_ON(want);
854 }
855 balanced:
856 /*
857 * Disable all the borrow logic by pretending we have inf
858 * runtime - in which case borrowing doesn't make sense.
859 */
860 // MTK patch: prevent normal task could run anymore, use rt_disable_borrow
861 //rt_rq->rt_runtime = RUNTIME_INF;
862 rt_rq->rt_runtime = rt_b->rt_runtime;
863 rt_rq->rt_throttled = 0;
864 #ifdef MTK_DEBUG_CGROUP
865 {
866 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, 0);
867 printk(KERN_EMERG "5-0. disable_runtime %llu\n", iter->rt_runtime);
868 iter = sched_rt_period_rt_rq(rt_b, 1);
869 printk(KERN_EMERG "5-1. disable_runtime %llu\n", iter->rt_runtime);
870 iter = sched_rt_period_rt_rq(rt_b, 2);
871 printk(KERN_EMERG "5-2. disable_runtime %llu\n", iter->rt_runtime);
872 iter = sched_rt_period_rt_rq(rt_b, 3);
873 printk(KERN_EMERG "5-3. disable_runtime %llu\n", iter->rt_runtime);
874 }
875 #endif
876 raw_spin_unlock(&rt_rq->rt_runtime_lock);
877 raw_spin_unlock(&rt_b->rt_runtime_lock);
878 #ifdef MTK_DEBUG_CGROUP
879 printk(KERN_ERR "disable_runtime after: rt_rq->rt_runtime=%llu rq_rt->rt_throttled=%d\n",
880 rt_rq->rt_runtime, rt_rq->rt_throttled);
881 #endif
882 }
883
884 #ifdef CONFIG_MT_RT_SCHED_CRIT
885 trace_sched_rt_crit(rq->cpu, rq->rt.rt_throttled);
886 #endif
887 }
888
889 static void disable_runtime(struct rq *rq)
890 {
891 unsigned long flags;
892
893 raw_spin_lock_irqsave(&rq->lock, flags);
894 __disable_runtime(rq);
895 raw_spin_unlock_irqrestore(&rq->lock, flags);
896 }
897
898 static void __enable_runtime(struct rq *rq)
899 {
900 rt_rq_iter_t iter;
901 struct rt_rq *rt_rq;
902
903 if (unlikely(!scheduler_running))
904 return;
905
906 /*
907 * Reset each runqueue's bandwidth settings
908 */
909 for_each_rt_rq(rt_rq, iter, rq) {
910 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
911
912 raw_spin_lock(&rt_b->rt_runtime_lock);
913 raw_spin_lock(&rt_rq->rt_runtime_lock);
914 if (rt_rq->rt_disable_borrow ){
915 #ifdef MTK_DEBUG_CGROUP
916 printk(KERN_EMERG "enable_runtime %d \n", rq->cpu);
917 #endif
918 rt_rq->rt_runtime = rt_b->rt_runtime;
919 rt_rq->rt_time = 0;
920 rt_rq->rt_throttled = 0;
921 rt_rq->rt_disable_borrow = 0;
922 }
923 raw_spin_unlock(&rt_rq->rt_runtime_lock);
924 raw_spin_unlock(&rt_b->rt_runtime_lock);
925 }
926
927 #ifdef CONFIG_MT_RT_SCHED_CRIT
928 trace_sched_rt_crit(rq->cpu, rq->rt.rt_throttled);
929 #endif
930 }
931
932 static void enable_runtime(struct rq *rq)
933 {
934 unsigned long flags;
935
936 raw_spin_lock_irqsave(&rq->lock, flags);
937 __enable_runtime(rq);
938 raw_spin_unlock_irqrestore(&rq->lock, flags);
939 }
940
941 int update_runtime(struct notifier_block *nfb, unsigned long action, void *hcpu)
942 {
943 int cpu = (int)(long)hcpu;
944
945 switch (action) {
946 case CPU_DOWN_PREPARE:
947 case CPU_DOWN_PREPARE_FROZEN:
948 disable_runtime(cpu_rq(cpu));
949 return NOTIFY_OK;
950
951 case CPU_DOWN_FAILED:
952 case CPU_DOWN_FAILED_FROZEN:
953 case CPU_ONLINE:
954 case CPU_ONLINE_FROZEN:
955 enable_runtime(cpu_rq(cpu));
956 return NOTIFY_OK;
957
958 default:
959 return NOTIFY_DONE;
960 }
961 }
962
963 static int balance_runtime(struct rt_rq *rt_rq)
964 {
965 int more = 0;
966
967 if (!sched_feat(RT_RUNTIME_SHARE))
968 return more;
969
970 if (rt_rq->rt_time > rt_rq->rt_runtime) {
971 raw_spin_unlock(&rt_rq->rt_runtime_lock);
972 more = do_balance_runtime(rt_rq);
973 raw_spin_lock(&rt_rq->rt_runtime_lock);
974 }
975
976 return more;
977 }
978 #else /* !CONFIG_SMP */
979 static inline int balance_runtime(struct rt_rq *rt_rq)
980 {
981 return 0;
982 }
983 #endif /* CONFIG_SMP */
984
985 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
986 {
987 int i, idle = 1, throttled = 0;
988 const struct cpumask *span;
989
990 span = sched_rt_period_mask();
991 #ifdef CONFIG_RT_GROUP_SCHED
992 /*
993 * FIXME: isolated CPUs should really leave the root task group,
994 * whether they are isolcpus or were isolated via cpusets, lest
995 * the timer run on a CPU which does not service all runqueues,
996 * potentially leaving other CPUs indefinitely throttled. If
997 * isolation is really required, the user will turn the throttle
998 * off to kill the perturbations it causes anyway. Meanwhile,
999 * this maintains functionality for boot and/or troubleshooting.
1000 */
1001 if (rt_b == &root_task_group.rt_bandwidth)
1002 span = cpu_online_mask;
1003 #endif
1004
1005 #ifdef MTK_DEBUG_CGROUP
1006 printk(KERN_EMERG " do_sched_rt_period_timer curr_cpu=%d \n", smp_processor_id());
1007 #endif
1008 for_each_cpu(i, span) {
1009 int enqueue = 0;
1010 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
1011 struct rq *rq = rq_of_rt_rq(rt_rq);
1012
1013 raw_spin_lock(&rq->lock);
1014 if (rt_rq->rt_time) {
1015 u64 runtime;
1016 u64 runtime_pre, rt_time_pre;
1017
1018 raw_spin_lock(&rt_rq->rt_runtime_lock);
1019 if (rt_rq->rt_throttled) {
1020 runtime_pre = rt_rq->rt_runtime;
1021 balance_runtime(rt_rq);
1022 rt_time_pre = rt_rq->rt_time;
1023 }
1024 runtime = rt_rq->rt_runtime;
1025 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
1026 if (rt_rq->rt_throttled) {
1027 printk_deferred("sched: cpu=%d, [%llu -> %llu]"
1028 " -= min(%llu, %d*[%llu -> %llu])"
1029 "\n", i, rt_time_pre,
1030 rt_rq->rt_time, rt_time_pre,
1031 overrun, runtime_pre, runtime);
1032 }
1033 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
1034 printk_deferred("sched: RT throttling inactivated"
1035 " cpu=%d\n", i);
1036 rt_rq->rt_throttled = 0;
1037 #ifdef CONFIG_MT_RT_SCHED_CRIT
1038 trace_sched_rt_crit(rq_cpu(rq), rq->rt.rt_throttled);
1039 #endif
1040
1041 enqueue = 1;
1042
1043 /*
1044 * Force a clock update if the CPU was idle,
1045 * lest wakeup -> unthrottle time accumulate.
1046 */
1047 if (rt_rq->rt_nr_running && rq->curr == rq->idle)
1048 rq->skip_clock_update = -1;
1049 }
1050 if (rt_rq->rt_time || rt_rq->rt_nr_running)
1051 idle = 0;
1052 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1053 } else if (rt_rq->rt_nr_running) {
1054 idle = 0;
1055 if (!rt_rq_throttled(rt_rq))
1056 enqueue = 1;
1057 }
1058 if (rt_rq->rt_throttled)
1059 throttled = 1;
1060
1061 if (enqueue)
1062 sched_rt_rq_enqueue(rt_rq);
1063 raw_spin_unlock(&rq->lock);
1064 }
1065
1066 if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
1067 return 1;
1068
1069 return idle;
1070 }
1071
1072 static inline int rt_se_prio(struct sched_rt_entity *rt_se)
1073 {
1074 #ifdef CONFIG_RT_GROUP_SCHED
1075 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1076
1077 if (rt_rq)
1078 return rt_rq->highest_prio.curr;
1079 #endif
1080
1081 return rt_task_of(rt_se)->prio;
1082 }
1083 DEFINE_PER_CPU(u64, exec_delta_time);
1084 DEFINE_PER_CPU(u64, clock_task);
1085 DEFINE_PER_CPU(u64, exec_start);
1086 static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
1087 {
1088 u64 runtime = sched_rt_runtime(rt_rq);
1089 u64 runtime_pre;
1090
1091 if (rt_rq->rt_throttled)
1092 return rt_rq_throttled(rt_rq);
1093
1094 if (runtime >= sched_rt_period(rt_rq))
1095 return 0;
1096
1097 runtime_pre = runtime;
1098 balance_runtime(rt_rq);
1099 runtime = sched_rt_runtime(rt_rq);
1100 if (runtime == RUNTIME_INF)
1101 return 0;
1102
1103 if (rt_rq->rt_time > runtime) {
1104 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
1105 int cpu = rq_cpu(rt_rq->rq);
1106
1107 printk_deferred("sched: cpu=%d rt_time %llu <-> runtime"
1108 " [%llu -> %llu], exec_delta_time[%llu]"
1109 ", clock_task[%llu], exec_start[%llu]\n",
1110 cpu, rt_rq->rt_time, runtime_pre, runtime,
1111 per_cpu(exec_delta_time, cpu),
1112 per_cpu(clock_task, cpu),
1113 per_cpu(exec_start, cpu));
1114 /*
1115 * Don't actually throttle groups that have no runtime assigned
1116 * but accrue some time due to boosting.
1117 */
1118 /* MTK patch: print rt throttle everytime*/
1119 if (likely(rt_b->rt_runtime)) {
1120 // static bool once = false;
1121
1122 rt_rq->rt_throttled = 1;
1123
1124 // if (!once) {
1125 // once = true;
1126 printk_deferred("sched: RT throttling activated cpu=%d\n",
1127 cpu);
1128 // }
1129 #ifdef CONFIG_MT_RT_SCHED_CRIT
1130 trace_sched_rt_crit(cpu, rt_rq->rt_throttled);
1131 #endif
1132
1133 } else {
1134 /*
1135 * In case we did anyway, make it go away,
1136 * replenishment is a joke, since it will replenish us
1137 * with exactly 0 ns.
1138 */
1139 rt_rq->rt_time = 0;
1140 }
1141
1142 if (rt_rq_throttled(rt_rq)) {
1143 sched_rt_rq_dequeue(rt_rq);
1144 return 1;
1145 }
1146 }
1147
1148 return 0;
1149 }
1150
1151 /*
1152 * Update the current task's runtime statistics. Skip current tasks that
1153 * are not in our scheduling class.
1154 */
1155 static void update_curr_rt(struct rq *rq)
1156 {
1157 struct task_struct *curr = rq->curr;
1158 struct sched_rt_entity *rt_se = &curr->rt;
1159 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1160 u64 delta_exec;
1161 int cpu = rq_cpu(rq);
1162
1163 if (curr->sched_class != &rt_sched_class)
1164 return;
1165
1166 delta_exec = rq->clock_task - curr->se.exec_start;
1167 if (unlikely((s64)delta_exec <= 0))
1168 return;
1169
1170 schedstat_set(curr->se.statistics.exec_max,
1171 max(curr->se.statistics.exec_max, delta_exec));
1172 per_cpu(exec_delta_time, cpu) = delta_exec;
1173 per_cpu(clock_task, cpu) = rq->clock_task;
1174 per_cpu(exec_start, cpu) = curr->se.exec_start;
1175 curr->se.sum_exec_runtime += delta_exec;
1176 account_group_exec_runtime(curr, delta_exec);
1177
1178 curr->se.exec_start = rq->clock_task;
1179 cpuacct_charge(curr, delta_exec);
1180
1181 sched_rt_avg_update(rq, delta_exec);
1182
1183 if (!rt_bandwidth_enabled())
1184 return;
1185
1186 for_each_sched_rt_entity(rt_se) {
1187 rt_rq = rt_rq_of_se(rt_se);
1188
1189 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
1190 raw_spin_lock(&rt_rq->rt_runtime_lock);
1191 rt_rq->rt_time += delta_exec;
1192 if (sched_rt_runtime_exceeded(rt_rq))
1193 resched_task(curr);
1194 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1195 }
1196 }
1197 }
1198
1199 #if defined CONFIG_SMP
1200
1201 static void
1202 inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1203 {
1204 struct rq *rq = rq_of_rt_rq(rt_rq);
1205
1206 #ifdef CONFIG_RT_GROUP_SCHED
1207 /*
1208 * Change rq's cpupri only if rt_rq is the top queue.
1209 */
1210 if (&rq->rt != rt_rq)
1211 return;
1212 #endif
1213 if (rq->online && prio < prev_prio)
1214 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
1215 }
1216
1217 static void
1218 dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1219 {
1220 struct rq *rq = rq_of_rt_rq(rt_rq);
1221
1222 #ifdef CONFIG_RT_GROUP_SCHED
1223 /*
1224 * Change rq's cpupri only if rt_rq is the top queue.
1225 */
1226 if (&rq->rt != rt_rq)
1227 return;
1228 #endif
1229 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1230 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1231 }
1232
1233 #else /* CONFIG_SMP */
1234
1235 static inline
1236 void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1237 static inline
1238 void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1239
1240 #endif /* CONFIG_SMP */
1241
1242 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1243 static void
1244 inc_rt_prio(struct rt_rq *rt_rq, int prio)
1245 {
1246 int prev_prio = rt_rq->highest_prio.curr;
1247
1248 if (prio < prev_prio)
1249 rt_rq->highest_prio.curr = prio;
1250
1251 inc_rt_prio_smp(rt_rq, prio, prev_prio);
1252 }
1253
1254 static void
1255 dec_rt_prio(struct rt_rq *rt_rq, int prio)
1256 {
1257 int prev_prio = rt_rq->highest_prio.curr;
1258
1259 if (rt_rq->rt_nr_running) {
1260
1261 WARN_ON(prio < prev_prio);
1262
1263 /*
1264 * This may have been our highest task, and therefore
1265 * we may have some recomputation to do
1266 */
1267 if (prio == prev_prio) {
1268 struct rt_prio_array *array = &rt_rq->active;
1269
1270 rt_rq->highest_prio.curr =
1271 sched_find_first_bit(array->bitmap);
1272 }
1273
1274 } else
1275 rt_rq->highest_prio.curr = MAX_RT_PRIO;
1276
1277 dec_rt_prio_smp(rt_rq, prio, prev_prio);
1278 }
1279
1280 #else
1281
1282 static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1283 static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1284
1285 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1286
1287 #ifdef CONFIG_RT_GROUP_SCHED
1288
1289 static void
1290 inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1291 {
1292 if (rt_se_boosted(rt_se))
1293 rt_rq->rt_nr_boosted++;
1294
1295 if (rt_rq->tg)
1296 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
1297 }
1298
1299 static void
1300 dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1301 {
1302 if (rt_se_boosted(rt_se))
1303 rt_rq->rt_nr_boosted--;
1304
1305 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
1306 }
1307
1308 #else /* CONFIG_RT_GROUP_SCHED */
1309
1310 static void
1311 inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1312 {
1313 start_rt_bandwidth(&def_rt_bandwidth);
1314 }
1315
1316 static inline
1317 void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1318
1319 #endif /* CONFIG_RT_GROUP_SCHED */
1320
1321 static inline
1322 void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1323 {
1324 int prio = rt_se_prio(rt_se);
1325
1326 WARN_ON(!rt_prio(prio));
1327 rt_rq->rt_nr_running++;
1328
1329 inc_rt_prio(rt_rq, prio);
1330 inc_rt_migration(rt_se, rt_rq);
1331 inc_rt_group(rt_se, rt_rq);
1332 }
1333
1334 static inline
1335 void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1336 {
1337 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
1338 WARN_ON(!rt_rq->rt_nr_running);
1339 rt_rq->rt_nr_running--;
1340
1341 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1342 dec_rt_migration(rt_se, rt_rq);
1343 dec_rt_group(rt_se, rt_rq);
1344 }
1345
1346 static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
1347 {
1348 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1349 struct rt_prio_array *array = &rt_rq->active;
1350 struct rt_rq *group_rq = group_rt_rq(rt_se);
1351 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1352
1353 /*
1354 * Don't enqueue the group if its throttled, or when empty.
1355 * The latter is a consequence of the former when a child group
1356 * get throttled and the current group doesn't have any other
1357 * active members.
1358 */
1359 // if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
1360 if (group_rq && ( !group_rq->rt_nr_running))
1361 return;
1362
1363 if (!rt_rq->rt_nr_running)
1364 list_add_leaf_rt_rq(rt_rq);
1365
1366 if (head)
1367 list_add(&rt_se->run_list, queue);
1368 else
1369 list_add_tail(&rt_se->run_list, queue);
1370 __set_bit(rt_se_prio(rt_se), array->bitmap);
1371
1372 inc_rt_tasks(rt_se, rt_rq);
1373 }
1374
1375 static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
1376 {
1377 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1378 struct rt_prio_array *array = &rt_rq->active;
1379
1380 list_del_init(&rt_se->run_list);
1381 if (list_empty(array->queue + rt_se_prio(rt_se)))
1382 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1383
1384 dec_rt_tasks(rt_se, rt_rq);
1385 if (!rt_rq->rt_nr_running)
1386 list_del_leaf_rt_rq(rt_rq);
1387 }
1388
1389 /*
1390 * Because the prio of an upper entry depends on the lower
1391 * entries, we must remove entries top - down.
1392 */
1393 static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
1394 {
1395 struct sched_rt_entity *back = NULL;
1396
1397 for_each_sched_rt_entity(rt_se) {
1398 rt_se->back = back;
1399 back = rt_se;
1400 }
1401
1402 for (rt_se = back; rt_se; rt_se = rt_se->back) {
1403 if (on_rt_rq(rt_se))
1404 __dequeue_rt_entity(rt_se);
1405 }
1406 }
1407
1408 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
1409 {
1410 dequeue_rt_stack(rt_se);
1411 for_each_sched_rt_entity(rt_se)
1412 __enqueue_rt_entity(rt_se, head);
1413 }
1414
1415 static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
1416 {
1417 dequeue_rt_stack(rt_se);
1418
1419 for_each_sched_rt_entity(rt_se) {
1420 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1421
1422 if (rt_rq && rt_rq->rt_nr_running)
1423 __enqueue_rt_entity(rt_se, false);
1424 }
1425 }
1426
1427 /*
1428 * Adding/removing a task to/from a priority array:
1429 */
1430 static void
1431 enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1432 {
1433 struct sched_rt_entity *rt_se = &p->rt;
1434
1435 if (flags & ENQUEUE_WAKEUP)
1436 rt_se->timeout = 0;
1437
1438 enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
1439
1440 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
1441 enqueue_pushable_task(rq, p);
1442
1443 inc_nr_running(rq);
1444 }
1445
1446 static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1447 {
1448 struct sched_rt_entity *rt_se = &p->rt;
1449
1450 update_curr_rt(rq);
1451 dequeue_rt_entity(rt_se);
1452
1453 dequeue_pushable_task(rq, p);
1454
1455 dec_nr_running(rq);
1456 }
1457
1458 /*
1459 * Put task to the head or the end of the run list without the overhead of
1460 * dequeue followed by enqueue.
1461 */
1462 static void
1463 requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
1464 {
1465 if (on_rt_rq(rt_se)) {
1466 struct rt_prio_array *array = &rt_rq->active;
1467 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1468
1469 if (head)
1470 list_move(&rt_se->run_list, queue);
1471 else
1472 list_move_tail(&rt_se->run_list, queue);
1473 }
1474 }
1475
1476 static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
1477 {
1478 struct sched_rt_entity *rt_se = &p->rt;
1479 struct rt_rq *rt_rq;
1480
1481 for_each_sched_rt_entity(rt_se) {
1482 rt_rq = rt_rq_of_se(rt_se);
1483 requeue_rt_entity(rt_rq, rt_se, head);
1484 }
1485 }
1486
1487 static void yield_task_rt(struct rq *rq)
1488 {
1489 requeue_task_rt(rq, rq->curr, 0);
1490 }
1491
1492 #ifdef CONFIG_SMP
1493 static int find_lowest_rq(struct task_struct *task);
1494
1495 static int
1496 select_task_rq_rt(struct task_struct *p, int sd_flag, int flags)
1497 {
1498 struct task_struct *curr;
1499 struct rq *rq;
1500 int cpu;
1501
1502 cpu = task_cpu(p);
1503
1504 if (p->nr_cpus_allowed == 1)
1505 goto out;
1506
1507 /* For anything but wake ups, just return the task_cpu */
1508 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1509 goto out;
1510
1511 rq = cpu_rq(cpu);
1512
1513 rcu_read_lock();
1514 curr = ACCESS_ONCE(rq->curr); /* unlocked access */
1515
1516 /*
1517 * If the current task on @p's runqueue is an RT task, then
1518 * try to see if we can wake this RT task up on another
1519 * runqueue. Otherwise simply start this RT task
1520 * on its current runqueue.
1521 *
1522 * We want to avoid overloading runqueues. If the woken
1523 * task is a higher priority, then it will stay on this CPU
1524 * and the lower prio task should be moved to another CPU.
1525 * Even though this will probably make the lower prio task
1526 * lose its cache, we do not want to bounce a higher task
1527 * around just because it gave up its CPU, perhaps for a
1528 * lock?
1529 *
1530 * For equal prio tasks, we just let the scheduler sort it out.
1531 *
1532 * Otherwise, just let it ride on the affined RQ and the
1533 * post-schedule router will push the preempted task away
1534 *
1535 * This test is optimistic, if we get it wrong the load-balancer
1536 * will have to sort it out.
1537 */
1538 #ifdef CONFIG_MT_RT_SCHED_INFO
1539 if(curr){
1540 mt_rt_printf("0. select_task_rq_rt cpu=%d p=%d:%s:%d:%d curr=%d:%s:%d:%d",
1541 cpu, p->pid, p->comm, p->prio, p->nr_cpus_allowed,
1542 curr->pid, curr->comm, curr->prio, curr->nr_cpus_allowed);
1543 }else{
1544 mt_rt_printf("0. select_task_rq_rt cpu=%d curr=%d:%s:%d",
1545 cpu, p->pid, p->comm, p->prio);
1546 }
1547 #endif
1548
1549 #ifdef CONFIG_MT_RT_SCHED
1550 /* if the task is allowed to put more than one CPU. */
1551 if ( (p->nr_cpus_allowed > 1) ){
1552 #else
1553 if (curr &&
1554 unlikely(rt_task(curr)) &&
1555 (curr->nr_cpus_allowed < 2 || curr->prio <= p->prio)
1556 && (p->nr_cpus_allowed > 1)) {
1557 #endif
1558 int target = find_lowest_rq(p);
1559
1560 if (target != -1)
1561 cpu = target;
1562
1563 #ifdef CONFIG_MT_RT_SCHED_INFO
1564 mt_rt_printf("1. select_task_rq_rt %d:%d:%s", cpu, p->pid, p->comm);
1565 #endif
1566 }
1567 rcu_read_unlock();
1568
1569 out:
1570 return cpu;
1571 }
1572
1573 static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1574 {
1575 if (rq->curr->nr_cpus_allowed == 1)
1576 return;
1577
1578 if (p->nr_cpus_allowed != 1
1579 && cpupri_find(&rq->rd->cpupri, p, NULL))
1580 return;
1581
1582 if (!cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
1583 return;
1584
1585 /*
1586 * There appears to be other cpus that can accept
1587 * current and none to run 'p', so lets reschedule
1588 * to try and push current away:
1589 */
1590 requeue_task_rt(rq, p, 1);
1591 resched_task(rq->curr);
1592 }
1593
1594 #endif /* CONFIG_SMP */
1595
1596 /*
1597 * Preempt the current task with a newly woken task if needed:
1598 */
1599 static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
1600 {
1601 #ifdef CONFIG_MT_RT_SCHED_INFO
1602 mt_rt_printf("check_preempt_curr_rt %d:%d:%s", p->prio, rq->curr->prio, p->comm);
1603 #endif
1604 if (p->prio < rq->curr->prio) {
1605 resched_task(rq->curr);
1606 return;
1607 }
1608
1609 #ifdef CONFIG_SMP
1610 /*
1611 * If:
1612 *
1613 * - the newly woken task is of equal priority to the current task
1614 * - the newly woken task is non-migratable while current is migratable
1615 * - current will be preempted on the next reschedule
1616 *
1617 * we should check to see if current can readily move to a different
1618 * cpu. If so, we will reschedule to allow the push logic to try
1619 * to move current somewhere else, making room for our non-migratable
1620 * task.
1621 */
1622 if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
1623 check_preempt_equal_prio(rq, p);
1624 #endif
1625 }
1626
1627 #ifdef CONFIG_MT_RT_SCHED
1628 /* Return the second highest RT task, NULL otherwise */
1629 static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1630 struct rt_rq *rt_rq)
1631 {
1632 struct rt_prio_array *array = &rt_rq->active;
1633 struct sched_rt_entity *next = NULL;
1634 struct sched_rt_entity *rt_se;
1635 int idx;
1636
1637 idx = sched_find_first_bit(array->bitmap);
1638 BUG_ON(idx >= MAX_RT_PRIO);
1639
1640 next_idx:
1641 list_for_each_entry(rt_se, array->queue + idx, run_list) {
1642 struct task_struct *p;
1643
1644 if (!rt_entity_is_task(rt_se)){
1645 next = rt_se;
1646 break;
1647 }
1648
1649 p = rt_task_of(rt_se);
1650 if ( (!cpu_online(rq->cpu)) || (!test_tsk_need_released(p))) {
1651 next = rt_se;
1652 break;
1653 }else{
1654 #ifdef CONFIG_MT_RT_SCHED_INFO
1655 mt_rt_printf("1. pick_next_rt_entity bypass %d %s", p->pid, p->comm);
1656 #endif
1657 }
1658 }
1659 if (!next) {
1660 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
1661 if (idx < MAX_RT_PRIO)
1662 goto next_idx;
1663 }
1664
1665 return next;
1666 }
1667
1668 #else
1669 static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1670 struct rt_rq *rt_rq)
1671 {
1672 struct rt_prio_array *array = &rt_rq->active;
1673 struct sched_rt_entity *next = NULL;
1674 struct list_head *queue;
1675 int idx;
1676
1677 idx = sched_find_first_bit(array->bitmap);
1678 BUG_ON(idx >= MAX_RT_PRIO);
1679
1680 queue = array->queue + idx;
1681 next = list_entry(queue->next, struct sched_rt_entity, run_list);
1682
1683 return next;
1684 }
1685 #endif
1686
1687 static struct task_struct *_pick_next_task_rt(struct rq *rq)
1688 {
1689 struct sched_rt_entity *rt_se;
1690 struct task_struct *p;
1691 struct rt_rq *rt_rq;
1692
1693 rt_rq = &rq->rt;
1694
1695 if (!rt_rq->rt_nr_running)
1696 return NULL;
1697
1698 if (rt_rq_throttled(rt_rq)){
1699 /* prevent wdt from RT throttle */
1700 struct rt_prio_array *array = &rt_rq->active;
1701 int idx = 0, prio = MAX_RT_PRIO- 1 - idx; //WDT priority
1702
1703 if( test_bit(idx, array->bitmap)){
1704 list_for_each_entry(rt_se, array->queue + idx, run_list){
1705 p = rt_task_of(rt_se);
1706 if( (p->rt_priority == prio) && (0 == strncmp(p->comm, "wdtk", 4)) ){
1707 p->se.exec_start = rq->clock_task;
1708 printk(KERN_WARNING "sched: unthrottle %s\n", p->comm);
1709 return p;
1710 }
1711 }
1712 }
1713 return NULL;
1714 }
1715
1716 do {
1717 rt_se = pick_next_rt_entity(rq, rt_rq);
1718 #ifdef CONFIG_MT_RT_SCHED
1719 if(!rt_se){
1720 #ifdef CONFIG_MT_RT_SCHED_INFO
1721 mt_rt_printf("_pick_next_task_rt %d:%s:%d:%d:%d",
1722 rq->curr->pid, rq->curr->comm, rq->curr->prio,
1723 test_tsk_need_released(rq->curr), rt_rq->rt_nr_running);
1724 #endif
1725 return NULL;
1726 }
1727 #endif
1728 BUG_ON(!rt_se);
1729 rt_rq = group_rt_rq(rt_se);
1730 } while (rt_rq);
1731
1732 p = rt_task_of(rt_se);
1733 p->se.exec_start = rq->clock_task;
1734
1735 return p;
1736 }
1737
1738 static struct task_struct *pick_next_task_rt(struct rq *rq)
1739 {
1740 struct task_struct *p = _pick_next_task_rt(rq);
1741
1742 /* The running task is never eligible for pushing */
1743 if (p)
1744 dequeue_pushable_task(rq, p);
1745
1746 #ifdef CONFIG_SMP
1747 /*
1748 * We detect this state here so that we can avoid taking the RQ
1749 * lock again later if there is no need to push
1750 */
1751 rq->post_schedule = has_pushable_tasks(rq);
1752 #endif
1753
1754 return p;
1755 }
1756
1757 static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
1758 {
1759 update_curr_rt(rq);
1760
1761 /*
1762 * The previous task needs to be made eligible for pushing
1763 * if it is still active
1764 */
1765 #ifdef CONFIG_MT_RT_SCHED
1766 if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1 && !test_tsk_need_released(p))
1767 enqueue_pushable_task(rq, p);
1768 #else
1769 if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
1770 enqueue_pushable_task(rq, p);
1771 #endif
1772 }
1773
1774 #ifdef CONFIG_SMP
1775
1776 /* Only try algorithms three times */
1777 #define RT_MAX_TRIES 3
1778
1779 static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1780 {
1781 if (!task_running(rq, p) &&
1782 cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
1783 return 1;
1784 return 0;
1785 }
1786
1787 /* Return the second highest RT task, NULL otherwise */
1788 static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
1789 {
1790 struct task_struct *next = NULL;
1791 struct sched_rt_entity *rt_se;
1792 struct rt_prio_array *array;
1793 struct rt_rq *rt_rq;
1794 int idx;
1795
1796 for_each_leaf_rt_rq(rt_rq, rq) {
1797 array = &rt_rq->active;
1798 idx = sched_find_first_bit(array->bitmap);
1799 next_idx:
1800 if (idx >= MAX_RT_PRIO)
1801 continue;
1802 if (next && next->prio <= idx)
1803 continue;
1804 list_for_each_entry(rt_se, array->queue + idx, run_list) {
1805 struct task_struct *p;
1806
1807 if (!rt_entity_is_task(rt_se))
1808 continue;
1809
1810 p = rt_task_of(rt_se);
1811 if (pick_rt_task(rq, p, cpu)) {
1812 next = p;
1813 break;
1814 }
1815 }
1816 if (!next) {
1817 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
1818 goto next_idx;
1819 }
1820 }
1821
1822 return next;
1823 }
1824
1825 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
1826
1827 #ifdef CONFIG_MT_RT_SCHED
1828 static int test_has_highest_prio(int this_cpu)
1829 {
1830 int cpu, highest_prio;
1831 struct rq *this_rq = cpu_rq(this_cpu), *rq;
1832 int prio = this_rq->curr->prio;
1833
1834 #ifdef CONFIG_MT_RT_SCHED_INFO
1835 mt_rt_printf("0. test_has_highest_prio %d:%d:%s:%d %lu",
1836 this_cpu, this_rq->curr->pid, this_rq->curr->comm, prio, tsk_cpus_allowed(this_rq->curr)->bits[0]);
1837 #endif
1838 if (prio >= MAX_RT_PRIO){
1839 #ifdef CONFIG_MT_RT_SCHED_INFO
1840 mt_rt_printf("test_has_highest_prio false %d:%d:%s:%d",
1841 this_cpu, this_rq->curr->pid, this_rq->curr->comm, prio);
1842 #endif
1843 return 0;
1844 }
1845
1846 for_each_cpu(cpu, &hmp_fast_cpu_mask) {
1847 if(!cpu_online(cpu))
1848 continue;
1849
1850 if (!cpumask_test_cpu(cpu, tsk_cpus_allowed(this_rq->curr)))
1851 continue;
1852
1853 rq = cpu_rq(cpu);
1854
1855 if(rq->rt.rt_nr_running == 0){
1856 #ifdef CONFIG_MT_RT_SCHED_NOTICE
1857 mt_rt_printf( "test_has_highest_prio true %d",
1858 cpu);
1859 #endif
1860 return 1;
1861 }
1862
1863 highest_prio = rq->rt.highest_prio.curr;
1864
1865 #ifdef CONFIG_MT_RT_SCHED_INFO
1866 mt_rt_printf( "1. test_has_highest_prio %d:%d %d",
1867 cpu, highest_prio, prio);
1868 #endif
1869 /* if currenet task's priority is higher than process in big CPU */
1870 if(prio < highest_prio){
1871 #ifdef CONFIG_MT_RT_SCHED_NOTICE
1872 mt_rt_printf("test_has_highest_prio true %d:%d:%d",
1873 cpu, highest_prio, prio);
1874 #endif
1875 return 1;
1876 }
1877 }
1878
1879 #ifdef CONFIG_MT_RT_SCHED_NOTICE
1880 mt_rt_printf("test_has_highest_prio false %d:%d:%s:%d",
1881 this_cpu, this_rq->curr->pid, this_rq->curr->comm, prio);
1882 #endif
1883
1884 return 0;
1885 }
1886
1887 static void release_task_ipi(void *data)
1888 {
1889 #ifdef CONFIG_MT_RT_SCHED_NOTICE
1890 int target_cpu = (int)(long) data;
1891 #endif
1892 int cpu = smp_processor_id();
1893 struct rq *rq = cpu_rq(cpu);
1894
1895 #ifdef CONFIG_MT_RT_SCHED_NOTICE
1896 mt_rt_printf("1. release_task_ipi %d %lu %d",
1897 cpu, hmp_slow_cpu_mask.bits[0], target_cpu);
1898 #endif
1899
1900 /* check if current process is LITTLE */
1901 if (!cpumask_test_cpu(cpu, &hmp_slow_cpu_mask))
1902 return;
1903
1904 /* check if current task is highest_n_tasks? */
1905 if ( !test_has_highest_prio(cpu)){
1906 #ifdef CONFIG_MT_RT_SCHED_INFO
1907 mt_rt_printf("3. release_task_ipi false");
1908 #endif
1909 return;
1910 }
1911
1912 #ifdef CONFIG_MT_RT_SCHED_INFO
1913 mt_rt_printf("set_tsk_need_release %d:%s:%d", rq->curr->pid, rq->curr->comm, rq->curr->prio);
1914 #endif
1915 set_tsk_need_released(rq->curr);
1916 set_tsk_need_resched(rq->curr);
1917 }
1918
1919 static DEFINE_PER_CPU(int, mt_need_released);
1920 static int find_highest_prio_in_LITTLE(struct rq *this_rq, int pull)
1921 {
1922 int cpu, prio, this_cpu = this_rq->cpu, highest_prio;
1923 struct rq *rq = NULL;
1924 struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask);
1925
1926 highest_prio = MAX_RT_PRIO;
1927 cpumask_clear(lowest_mask);
1928
1929 #ifdef CONFIG_MT_RT_SCHED_INFO
1930 mt_rt_printf("0. find_highest_prio_in_LITTLE %lu %d:%d %d",
1931 hmp_slow_cpu_mask.bits[0],
1932 this_rq->cpu, this_rq->rt.highest_prio.curr,
1933 pull);
1934 #endif
1935 for_each_cpu(cpu, &hmp_slow_cpu_mask){
1936 #ifdef CONFIG_MT_RT_SCHED_INFO
1937 mt_rt_printf("1. find_highest_prio_in_LITTLE %d %d", cpu, cpu_online(cpu));
1938 #endif
1939 if (!cpu_online(cpu))
1940 continue;
1941
1942 rq = cpu_rq(cpu);
1943 if(rq->rt.rt_nr_running == 0)
1944 continue;
1945
1946 prio = rq->rt.highest_prio.curr;
1947
1948 #ifdef CONFIG_MT_RT_SCHED_INFO
1949 mt_rt_printf("2. find_highest_prio_in_LITTLE %d %d %d %lu",
1950 cpu, prio, highest_prio, tsk_cpus_allowed(rq->curr)->bits[0]);
1951 #endif
1952
1953 /* If the highest priority of LITTLE CPU is smaller and equal than current,
1954 * then bypass
1955 */
1956 if (prio >= this_rq->rt.highest_prio.curr )
1957 continue;
1958
1959 /* If the prority of LITTLE CPU is smaller and than highest_prio of LITTLE CPUs */
1960 if (prio > highest_prio)
1961 continue;
1962
1963 /* check the affinity */
1964 if (!cpumask_test_cpu(this_rq->cpu, tsk_cpus_allowed(rq->curr)))
1965 continue;
1966
1967 if (prio < highest_prio){
1968 if ( 0 == pull ){
1969 #ifdef CONFIG_MT_RT_SCHED_INFO
1970 mt_rt_printf("3. find_highest_prio_in_LITTLE find");
1971 #endif
1972 return 1;
1973 }
1974
1975 highest_prio = prio;
1976 cpumask_clear(lowest_mask);
1977 }
1978
1979 cpumask_set_cpu(cpu, lowest_mask);
1980
1981 #ifdef CONFIG_MT_RT_SCHED_INFO
1982 mt_rt_printf("2. find_highest_prio_in_LITTLE %d:%d %d %lu",
1983 cpu, prio, highest_prio, lowest_mask->bits[0]);
1984 #endif
1985 }
1986
1987 if (cpumask_empty(lowest_mask)){
1988 #ifdef CONFIG_MT_RT_SCHED_INFO
1989 mt_rt_printf("3. find_highest_prio_in_LITTLE not find");
1990 #endif
1991 return 0;
1992 }
1993
1994 raw_spin_unlock_irq(&this_rq->lock);
1995 per_cpu(mt_need_released, this_cpu) = 1;
1996 for_each_cpu (cpu, lowest_mask) {
1997 rq = cpu_rq(cpu);
1998 #ifdef CONFIG_MT_RT_SCHED_INFO
1999 mt_rt_printf("4. find_highest_prio_in_LITTLE %d %d",
2000 cpu, rq->rt.highest_prio.curr);
2001 #endif
2002 if (highest_prio == rq->rt.highest_prio.curr) {
2003 /* send IPI release */
2004 #if defined (CONFIG_MT_RT_SCHED_NOTICE)
2005 mt_rt_printf("send ipi release to cpu=%d prio=%d",
2006 cpu, rq->rt.highest_prio.curr);
2007 #endif
2008 /* the target CPU will execute release_task_ipi */
2009 smp_call_function_single(cpu, release_task_ipi, (void *)this_cpu, 0);
2010 break;
2011 }
2012 }
2013
2014 raw_spin_lock_irq(&this_rq->lock);
2015 return 1;
2016 }
2017
2018 static int find_lowest_rq_in_big(struct task_struct *task, struct cpumask *lowest_mask)
2019 {
2020 int i, lowest_prio = 0;
2021 struct rq *rq = NULL;
2022
2023 cpumask_clear(lowest_mask);
2024 #ifdef CONFIG_MT_RT_SCHED_INFO
2025 mt_rt_printf("0. find_lowest_rq_in_big %lu %d:%s:%d",
2026 (unsigned long)hmp_fast_cpu_mask.bits[0],
2027 task->pid, task->comm, task->prio);
2028 #endif
2029
2030 for_each_cpu(i, &hmp_fast_cpu_mask){
2031 int prio;
2032
2033 if (!cpu_online(i))
2034 continue;
2035
2036 rq = cpu_rq(i);
2037 prio = rq->rt.highest_prio.curr;
2038
2039 #ifdef CONFIG_MT_RT_SCHED_INFO
2040 mt_rt_printf("1. find_lowest_rq_in_big %d:%d %d:%lu",
2041 i, prio,
2042 lowest_prio, (unsigned long)lowest_mask->bits[0]);
2043 #endif
2044
2045 /* If the highest priority of CPU is higher than lowest_prio
2046 * or higher than the task, then bypass
2047 */
2048 if ((prio < lowest_prio) || (prio <= task->prio))
2049 continue;
2050
2051 if (!cpumask_test_cpu(i, tsk_cpus_allowed(task)))
2052 continue;
2053
2054 /* If the priority lower than lowest_prio */
2055 if (prio > lowest_prio){
2056 lowest_prio = prio;
2057 cpumask_clear(lowest_mask);
2058 }
2059
2060 cpumask_set_cpu(i, lowest_mask);
2061 }
2062
2063 if (cpumask_empty(lowest_mask)){
2064 #ifdef CONFIG_MT_RT_SCHED_INFO
2065 mt_rt_printf("2. find_lowest_rq_in_big not find");
2066 #endif
2067 return 0;
2068 }
2069
2070 #ifdef CONFIG_MT_RT_SCHED_INFO
2071 mt_rt_printf("3. find_lowest_rq_in_big find %d:%s:%d %d:%lu",
2072 task->pid, task->comm, task->prio,
2073 lowest_prio, (unsigned long)lowest_mask->bits[0]);
2074 #endif
2075 return 1;
2076 }
2077
2078 static int find_lowest_rq_in_LITTLE(struct task_struct *task, struct cpumask *lowest_mask)
2079 {
2080 int i, lowest_prio = 0;
2081 struct rq *rq = NULL;
2082
2083 cpumask_clear(lowest_mask);
2084 #ifdef CONFIG_MT_RT_SCHED_INFO
2085 mt_rt_printf("0. find_lowest_rq_in_LITTLE %lu %d:%s:%d",
2086 (unsigned long)hmp_slow_cpu_mask.bits[0],
2087 task->pid, task->comm, task->prio);
2088 #endif
2089
2090 for_each_cpu(i, &hmp_slow_cpu_mask){
2091 int prio;
2092
2093 if (!cpu_online(i))
2094 continue;
2095
2096 rq = cpu_rq(i);
2097 prio = rq->rt.highest_prio.curr;
2098
2099 #ifdef CONFIG_MT_RT_SCHED_INFO
2100 mt_rt_printf("1. find_lowest_rq_in_LITTLE %d:%d %d:%lu",
2101 i, prio,
2102 lowest_prio, (unsigned long)lowest_mask->bits[0]);
2103 #endif
2104
2105 /* If the highest priority of CPU is higher than lowest_prio
2106 * or higher than the task, then bypass
2107 */
2108 if ((prio < lowest_prio) || (prio <= task->prio))
2109 continue;
2110
2111 if (!cpumask_test_cpu(i, tsk_cpus_allowed(task)))
2112 continue;
2113
2114 /* If the priority lower than lowest_prio */
2115 if (prio > lowest_prio){
2116 lowest_prio = prio;
2117 cpumask_clear(lowest_mask);
2118 }
2119
2120 cpumask_set_cpu(i, lowest_mask);
2121 }
2122
2123 if (cpumask_empty(lowest_mask)){
2124 #ifdef CONFIG_MT_RT_SCHED_INFO
2125 mt_rt_printf("2. find_lowest_rq_in_LITTLE not find");
2126 #endif
2127 return 0;
2128 }
2129
2130 #ifdef CONFIG_MT_RT_SCHED_INFO
2131 mt_rt_printf("3. find_lowest_rq_in_LITTLE find %d:%s:%d %d:%lu",
2132 task->pid, task->comm, task->prio,
2133 lowest_prio, (unsigned long)lowest_mask->bits[0]);
2134 #endif
2135
2136 return 1;
2137 }
2138
2139 #endif
2140
2141 static int find_lowest_rq(struct task_struct *task)
2142 {
2143 struct sched_domain *sd;
2144 struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask);
2145 int this_cpu = smp_processor_id();
2146 int cpu = task_cpu(task);
2147
2148 #ifdef CONFIG_MT_RT_SCHED_INFO
2149 mt_rt_printf("0. find_lowest_rq lowest_mask=%lu, task->nr_cpus_allowed=%d",
2150 lowest_mask->bits[0], task->nr_cpus_allowed );
2151 #endif
2152 /* Make sure the mask is initialized first */
2153 if (unlikely(!lowest_mask))
2154 return -1;
2155
2156 if (task->nr_cpus_allowed == 1)
2157 return -1; /* No other targets possible */
2158
2159 #ifdef CONFIG_MT_RT_SCHED
2160 if (!find_lowest_rq_in_big(task, lowest_mask)){
2161 if (!find_lowest_rq_in_LITTLE(task, lowest_mask)){
2162 return -1; /* No targets found */
2163 }
2164 }
2165 #else
2166 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
2167 return -1; /* No targets found */
2168 #endif
2169
2170 #ifdef CONFIG_MT_RT_SCHED_NOTICE
2171 mt_rt_printf("find_lowest_rq %d:%s:%d %lu",
2172 task->pid, task->comm, task->prio,
2173 lowest_mask->bits[0]);
2174 #endif
2175
2176 /*
2177 * At this point we have built a mask of cpus representing the
2178 * lowest priority tasks in the system. Now we want to elect
2179 * the best one based on our affinity and topology.
2180 *
2181 * We prioritize the last cpu that the task executed on since
2182 * it is most likely cache-hot in that location.
2183 */
2184 if (cpumask_test_cpu(cpu, lowest_mask))
2185 return cpu;
2186
2187 /*
2188 * Otherwise, we consult the sched_domains span maps to figure
2189 * out which cpu is logically closest to our hot cache data.
2190 */
2191 if (!cpumask_test_cpu(this_cpu, lowest_mask))
2192 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
2193
2194 rcu_read_lock();
2195 for_each_domain(cpu, sd) {
2196 if (sd->flags & SD_WAKE_AFFINE) {
2197 int best_cpu;
2198
2199 /*
2200 * "this_cpu" is cheaper to preempt than a
2201 * remote processor.
2202 */
2203 if (this_cpu != -1 &&
2204 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
2205 rcu_read_unlock();
2206 return this_cpu;
2207 }
2208
2209 best_cpu = cpumask_first_and(lowest_mask,
2210 sched_domain_span(sd));
2211 if (best_cpu < nr_cpu_ids) {
2212 rcu_read_unlock();
2213 return best_cpu;
2214 }
2215 }
2216 }
2217 rcu_read_unlock();
2218
2219 /*
2220 * And finally, if there were no matches within the domains
2221 * just give the caller *something* to work with from the compatible
2222 * locations.
2223 */
2224 if (this_cpu != -1)
2225 return this_cpu;
2226
2227 cpu = cpumask_any(lowest_mask);
2228 if (cpu < nr_cpu_ids)
2229 return cpu;
2230 return -1;
2231 }
2232
2233 /* Will lock the rq it finds */
2234 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
2235 {
2236 struct rq *lowest_rq = NULL;
2237 int tries;
2238 int cpu;
2239
2240 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
2241 cpu = find_lowest_rq(task);
2242
2243 if ((cpu == -1) || (cpu == rq->cpu))
2244 break;
2245
2246 lowest_rq = cpu_rq(cpu);
2247
2248 /* if the prio of this runqueue changed, try again */
2249 if (double_lock_balance(rq, lowest_rq)) {
2250 /*
2251 * We had to unlock the run queue. In
2252 * the mean time, task could have
2253 * migrated already or had its affinity changed.
2254 * Also make sure that it wasn't scheduled on its rq.
2255 */
2256
2257 #ifdef CONFIG_MT_RT_SCHED_INFO
2258 mt_rt_printf("1. find_lock_lowest_rq %d %d %d %s",
2259 lowest_rq->cpu, rq->cpu, task->pid, task->comm);
2260 #endif
2261 if (unlikely(task_rq(task) != rq ||
2262 !cpumask_test_cpu(lowest_rq->cpu,
2263 tsk_cpus_allowed(task)) ||
2264 task_running(rq, task) ||
2265 !task->on_rq)) {
2266
2267 double_unlock_balance(rq, lowest_rq);
2268 lowest_rq = NULL;
2269 break;
2270 }
2271 }
2272
2273 /* If this rq is still suitable use it. */
2274 if (lowest_rq->rt.highest_prio.curr > task->prio)
2275 break;
2276
2277 /* try again */
2278 double_unlock_balance(rq, lowest_rq);
2279 lowest_rq = NULL;
2280 }
2281
2282 return lowest_rq;
2283 }
2284
2285 static struct task_struct *pick_next_pushable_task(struct rq *rq)
2286 {
2287 struct task_struct *p;
2288
2289 if (!has_pushable_tasks(rq))
2290 return NULL;
2291
2292 p = plist_first_entry(&rq->rt.pushable_tasks,
2293 struct task_struct, pushable_tasks);
2294
2295 BUG_ON(rq->cpu != task_cpu(p));
2296 BUG_ON(task_current(rq, p));
2297 BUG_ON(p->nr_cpus_allowed <= 1);
2298
2299 BUG_ON(!p->on_rq);
2300 BUG_ON(!rt_task(p));
2301
2302 return p;
2303 }
2304
2305 #ifdef CONFIG_MT_RT_SCHED
2306 /* Will lock the rq it finds */
2307 /* refer find_lock_lowest_rq() */
2308 static struct rq *find_lock_lowest_rq_mtk(struct task_struct *task, struct rq *rq)
2309 {
2310 struct rq *lowest_rq = NULL;
2311 int cpu;
2312
2313 cpu = find_lowest_rq(task);
2314
2315 if ((cpu == -1) || (cpu == rq->cpu))
2316 return NULL;
2317
2318 lowest_rq = cpu_rq(cpu);
2319
2320 /* if the prio of this runqueue changed, try again */
2321 if (double_lock_balance(rq, lowest_rq)) {
2322 /*
2323 * We had to unlock the run queue. In
2324 * the mean time, task could have
2325 * migrated already or had its affinity changed.
2326 * Also make sure that it wasn't scheduled on its rq.
2327 */
2328 #ifdef CONFIG_MT_RT_SCHED_INFO
2329 mt_rt_printf("1. find_lock_lowest_rq_mtk %d %d %d %s",
2330 lowest_rq->cpu, rq->cpu, task->pid, task->comm);
2331 #endif
2332 if (unlikely(task_rq(task) != rq ||
2333 !cpumask_test_cpu(lowest_rq->cpu,
2334 tsk_cpus_allowed(task)) ||
2335 task_running(rq, task) ||
2336 !task->on_rq)) {
2337 double_unlock_balance(rq, lowest_rq);
2338 return NULL;
2339 }
2340 }
2341
2342 /* If this rq is still suitable use it. */
2343 if (lowest_rq->rt.highest_prio.curr > task->prio){
2344 return lowest_rq;
2345 }
2346
2347 double_unlock_balance(rq, lowest_rq);
2348 return NULL;
2349 }
2350 #endif
2351
2352
2353 #ifdef CONFIG_MT_RT_SCHED
2354 /* refer push_rt_task() */
2355 int push_need_released_rt_task(struct rq *rq, struct task_struct *p)
2356 {
2357 struct rq *lowest_rq;
2358 int ret = 0;
2359
2360 if (!p)
2361 return 0;
2362
2363 #ifdef CONFIG_MT_RT_SCHED_INFO
2364 mt_rt_printf("0. push_need_released_task %d:%s %d:%s",
2365 p->pid, p->comm, rq->curr->pid, rq->curr->comm);
2366 #endif
2367
2368 if (unlikely(p == rq->curr)) {
2369 WARN_ON(1);
2370 return 0;
2371 }
2372
2373 /* We might release rq lock */
2374 get_task_struct(p);
2375
2376 /* find_lock_lowest_rq locks the rq if found */
2377 lowest_rq = find_lock_lowest_rq_mtk(p, rq);
2378 if (!lowest_rq) {
2379 #ifdef CONFIG_MT_RT_SCHED_NOTICE
2380 mt_rt_printf("1. push_need_released_task fail %d:%s:%d %d",
2381 p->pid, p->comm, p->prio, rq->curr->prio);
2382 #endif
2383 put_task_struct(p);
2384
2385 if (likely(p->prio < rq->curr->prio)) {
2386 resched_task(rq->curr);
2387 }else{
2388 #ifdef CONFIG_MT_RT_SCHED_NOTICE
2389 mt_rt_printf("1. push_need_released_task fail %d:%s:%d %d",
2390 p->pid, p->comm, p->prio, rq->curr->prio);
2391 #if 0
2392 printk(KERN_ALERT "[sched] push_need_released_task fail %d:%s:%d %d\n",
2393 p->pid, p->comm, p->prio, rq->curr->prio);
2394 WARN_ON(1);
2395 #endif
2396 #endif
2397 }
2398 return 0;
2399 }
2400
2401 #ifdef CONFIG_MT_RT_SCHED_NOTICE
2402 mt_rt_printf("push_need_released_task task=%d:%s cpu=%d",
2403 p->pid, p->comm, lowest_rq->cpu);
2404 #endif
2405
2406 deactivate_task(rq, p, 0);
2407 set_task_cpu(p, lowest_rq->cpu);
2408 activate_task(lowest_rq, p, 0);
2409 ret = 1;
2410
2411 resched_task(lowest_rq->curr);
2412
2413 double_unlock_balance(rq, lowest_rq);
2414
2415 put_task_struct(p);
2416
2417 return ret;
2418 }
2419 #endif
2420
2421 /*
2422 * If the current CPU has more than one RT task, see if the non
2423 * running task can migrate over to a CPU that is running a task
2424 * of lesser priority.
2425 */
2426 static int push_rt_task(struct rq *rq)
2427 {
2428 struct task_struct *next_task;
2429 struct rq *lowest_rq;
2430 int ret = 0;
2431
2432 if (!rq->rt.overloaded)
2433 return 0;
2434
2435 next_task = pick_next_pushable_task(rq);
2436 if (!next_task)
2437 return 0;
2438
2439 retry:
2440 if (unlikely(next_task == rq->curr)) {
2441 WARN_ON(1);
2442 return 0;
2443 }
2444
2445 /*
2446 * It's possible that the next_task slipped in of
2447 * higher priority than current. If that's the case
2448 * just reschedule current.
2449 */
2450 if (unlikely(next_task->prio < rq->curr->prio)) {
2451 resched_task(rq->curr);
2452 return 0;
2453 }
2454
2455 /* We might release rq lock */
2456 get_task_struct(next_task);
2457
2458 /* find_lock_lowest_rq locks the rq if found */
2459 lowest_rq = find_lock_lowest_rq(next_task, rq);
2460 if (!lowest_rq) {
2461 struct task_struct *task;
2462 /*
2463 * find_lock_lowest_rq releases rq->lock
2464 * so it is possible that next_task has migrated.
2465 *
2466 * We need to make sure that the task is still on the same
2467 * run-queue and is also still the next task eligible for
2468 * pushing.
2469 */
2470 task = pick_next_pushable_task(rq);
2471 if (task_cpu(next_task) == rq->cpu && task == next_task) {
2472 /*
2473 * The task hasn't migrated, and is still the next
2474 * eligible task, but we failed to find a run-queue
2475 * to push it to. Do not retry in this case, since
2476 * other cpus will pull from us when ready.
2477 */
2478 goto out;
2479 }
2480
2481 if (!task)
2482 /* No more tasks, just exit */
2483 goto out;
2484
2485 /*
2486 * Something has shifted, try again.
2487 */
2488 put_task_struct(next_task);
2489 next_task = task;
2490 goto retry;
2491 }
2492
2493 deactivate_task(rq, next_task, 0);
2494 set_task_cpu(next_task, lowest_rq->cpu);
2495 activate_task(lowest_rq, next_task, 0);
2496 ret = 1;
2497
2498 resched_task(lowest_rq->curr);
2499
2500 double_unlock_balance(rq, lowest_rq);
2501
2502 out:
2503 put_task_struct(next_task);
2504
2505 return ret;
2506 }
2507
2508 static void push_rt_tasks(struct rq *rq)
2509 {
2510 /* push_rt_task will return true if it moved an RT */
2511 while (push_rt_task(rq))
2512 ;
2513 }
2514
2515 #ifdef CONFIG_MT_RT_SCHED
2516 /* refer pull_rt_task() */
2517 static int pick_next_highest_task(struct rq *this_rq){
2518 int this_cpu = this_rq->cpu, ret = 0, cpu;
2519 struct task_struct *p;
2520 struct rq *src_rq;
2521
2522 for_each_cpu(cpu, this_rq->rd->rto_mask) {
2523 if (this_cpu == cpu)
2524 continue;
2525
2526 src_rq = cpu_rq(cpu);
2527
2528 /*
2529 * Don't bother taking the src_rq->lock if the next highest
2530 * task is known to be lower-priority than our current task.
2531 * This may look racy, but if this value is about to go
2532 * logically higher, the src_rq will push this task away.
2533 * And if its going logically lower, we do not care
2534 */
2535 if (src_rq->rt.highest_prio.next >=
2536 this_rq->rt.highest_prio.curr)
2537 continue;
2538
2539 /*
2540 * We can potentially drop this_rq's lock in
2541 * double_lock_balance, and another CPU could
2542 * alter this_rq
2543 */
2544 double_lock_balance(this_rq, src_rq);
2545
2546 /*
2547 * Are there still pullable RT tasks?
2548 */
2549 if (src_rq->rt.rt_nr_running <= 1)
2550 goto skip;
2551
2552 p = pick_next_highest_task_rt(src_rq, this_cpu);
2553
2554 /*
2555 * Do we have an RT task that preempts
2556 * the to-be-scheduled task?
2557 */
2558 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
2559 WARN_ON(p == src_rq->curr);
2560 WARN_ON(!p->on_rq);
2561
2562 /*
2563 * There's a chance that p is higher in priority
2564 * than what's currently running on its cpu.
2565 * This is just that p is wakeing up and hasn't
2566 * had a chance to schedule. We only pull
2567 * p if it is lower in priority than the
2568 * current task on the run queue
2569 */
2570 if (p->prio < src_rq->curr->prio)
2571 goto skip;
2572
2573 ret = 1;
2574
2575 deactivate_task(src_rq, p, 0);
2576 set_task_cpu(p, this_cpu);
2577 activate_task(this_rq, p, 0);
2578 /*
2579 * We continue with the search, just in
2580 * case there's an even higher prio task
2581 * in another runqueue. (low likelihood
2582 * but possible)
2583 */
2584 #ifdef CONFIG_MT_RT_SCHED_INFO
2585 mt_rt_printf("pick_next_highest_task %d:%d %d %d:%s:%d\n",
2586 this_rq->cpu, this_rq->rt.highest_prio.curr,
2587 src_rq->cpu,
2588 p->pid, p->comm, p->prio);
2589 #endif
2590 }
2591 skip:
2592 double_unlock_balance(this_rq, src_rq);
2593 }
2594
2595 return ret;
2596 }
2597
2598 void mt_check_rt_policy(struct rq *this_rq)
2599 {
2600 int this_cpu = this_rq->cpu;
2601 if ( cpumask_test_cpu(this_cpu, &hmp_fast_cpu_mask) ){
2602 if ( !per_cpu(mt_need_released, this_cpu) )
2603 return;
2604 #ifdef CONFIG_MT_RT_SCHED_INFO
2605 mt_rt_printf("0. mt_check_rt_policy %d %d %s",
2606 this_cpu, this_rq->curr->pid, this_rq->curr->comm );
2607 #endif
2608
2609 if ( find_highest_prio_in_LITTLE(this_rq, 0) ){
2610 set_tsk_need_resched(this_rq->curr);
2611 #ifdef CONFIG_MT_RT_SCHED_INFO
2612 mt_rt_printf("1. mt_check_rt_policy %d %d %s",
2613 this_cpu, this_rq->curr->pid, this_rq->curr->comm );
2614 #endif
2615 }else{
2616 per_cpu(mt_need_released, this_cpu)=0;
2617 #ifdef CONFIG_MT_RT_SCHED_INFO
2618 mt_rt_printf("2. mt_check_rt_policy %d", this_cpu);
2619 #endif
2620 }
2621 }
2622 }
2623
2624 int mt_post_schedule(struct rq *rq)
2625 {
2626 int this_cpu = rq->cpu, ret = 0;
2627 unsigned long flags;
2628
2629 raw_spin_lock_irqsave(&rq->lock, flags);
2630 if ( cpumask_test_cpu(this_cpu, &hmp_fast_cpu_mask) ) {
2631 if ( has_rt_task_in_little() )
2632 ret = find_highest_prio_in_LITTLE(rq, 1);
2633 }
2634 raw_spin_unlock_irqrestore(&rq->lock, flags);
2635
2636 return ret;
2637 }
2638 #endif
2639
2640 #ifdef CONFIG_MT_RT_SCHED
2641 int pull_rt_task(struct rq *this_rq)
2642 #else
2643 static int pull_rt_task(struct rq *this_rq)
2644 #endif
2645 {
2646 #if defined(CONFIG_MT_RT_SCHED_INFO) || !defined(CONFIG_MT_RT_SCHED)
2647 int this_cpu = this_rq->cpu;
2648 #endif
2649 int ret = 0;
2650 #ifndef CONFIG_MT_RT_SCHED
2651 int cpu;
2652 struct task_struct *p;
2653 struct rq *src_rq;
2654 #endif
2655
2656 #ifdef CONFIG_MT_RT_SCHED_INFO
2657 mt_rt_printf("0. pull_rt_task %d %d %lu",
2658 rt_overloaded(this_rq), this_cpu, hmp_fast_cpu_mask.bits[0]);
2659 #endif
2660
2661 #ifdef CONFIG_MT_RT_SCHED
2662 if (likely(!rt_overloaded(this_rq)))
2663 return 0;
2664 ret = pick_next_highest_task(this_rq);
2665 #else
2666 if (likely(!rt_overloaded(this_rq)))
2667 return 0;
2668
2669 #ifdef CONFIG_MT_RT_SCHED_INFO
2670 mt_rt_printf("1. pull_rt_task %lu ",
2671 this_rq->rd->rto_mask->bits[0]);
2672 #endif
2673 for_each_cpu(cpu, this_rq->rd->rto_mask) {
2674 if (this_cpu == cpu)
2675 continue;
2676
2677 src_rq = cpu_rq(cpu);
2678
2679 /*
2680 * Don't bother taking the src_rq->lock if the next highest
2681 * task is known to be lower-priority than our current task.
2682 * This may look racy, but if this value is about to go
2683 * logically higher, the src_rq will push this task away.
2684 * And if its going logically lower, we do not care
2685 */
2686 #ifdef CONFIG_MT_RT_SCHED_INFO
2687 mt_rt_printf("2. pull_rt_task %d %d ",
2688 src_rq->rt.highest_prio.next, this_rq->rt.highest_prio.curr);
2689 #endif
2690 if (src_rq->rt.highest_prio.next >=
2691 this_rq->rt.highest_prio.curr)
2692 continue;
2693
2694 /*
2695 * We can potentially drop this_rq's lock in
2696 * double_lock_balance, and another CPU could
2697 * alter this_rq
2698 */
2699 double_lock_balance(this_rq, src_rq);
2700
2701 /*
2702 * Are there still pullable RT tasks?
2703 */
2704 if (src_rq->rt.rt_nr_running <= 1)
2705 goto skip;
2706
2707 p = pick_next_highest_task_rt(src_rq, this_cpu);
2708
2709 /*
2710 * Do we have an RT task that preempts
2711 * the to-be-scheduled task?
2712 */
2713 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
2714 WARN_ON(p == src_rq->curr);
2715 WARN_ON(!p->on_rq);
2716
2717 /*
2718 * There's a chance that p is higher in priority
2719 * than what's currently running on its cpu.
2720 * This is just that p is wakeing up and hasn't
2721 * had a chance to schedule. We only pull
2722 * p if it is lower in priority than the
2723 * current task on the run queue
2724 */
2725 if (p->prio < src_rq->curr->prio)
2726 goto skip;
2727
2728 ret = 1;
2729
2730 deactivate_task(src_rq, p, 0);
2731 set_task_cpu(p, this_cpu);
2732 activate_task(this_rq, p, 0);
2733 /*
2734 * We continue with the search, just in
2735 * case there's an even higher prio task
2736 * in another runqueue. (low likelihood
2737 * but possible)
2738 */
2739 }
2740 skip:
2741 double_unlock_balance(this_rq, src_rq);
2742 }
2743 #endif
2744
2745 return ret;
2746 }
2747
2748 static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
2749 {
2750 /* Try to pull RT tasks here if we lower this rq's prio */
2751 if (rq->rt.highest_prio.curr > prev->prio)
2752 pull_rt_task(rq);
2753 }
2754
2755 static void post_schedule_rt(struct rq *rq)
2756 {
2757 push_rt_tasks(rq);
2758 }
2759
2760 /*
2761 * If we are not running and we are not going to reschedule soon, we should
2762 * try to push tasks away now
2763 */
2764 static void task_woken_rt(struct rq *rq, struct task_struct *p)
2765 {
2766 if (!task_running(rq, p) &&
2767 !test_tsk_need_resched(rq->curr) &&
2768 has_pushable_tasks(rq) &&
2769 p->nr_cpus_allowed > 1 &&
2770 rt_task(rq->curr) &&
2771 (rq->curr->nr_cpus_allowed < 2 ||
2772 rq->curr->prio <= p->prio))
2773 push_rt_tasks(rq);
2774 }
2775
2776 static void set_cpus_allowed_rt(struct task_struct *p,
2777 const struct cpumask *new_mask)
2778 {
2779 struct rq *rq;
2780 int weight;
2781
2782 BUG_ON(!rt_task(p));
2783
2784 if (!p->on_rq)
2785 return;
2786
2787 weight = cpumask_weight(new_mask);
2788
2789 /*
2790 * Only update if the process changes its state from whether it
2791 * can migrate or not.
2792 */
2793 if ((p->nr_cpus_allowed > 1) == (weight > 1))
2794 return;
2795
2796 rq = task_rq(p);
2797
2798 /*
2799 * The process used to be able to migrate OR it can now migrate
2800 */
2801 if (weight <= 1) {
2802 if (!task_current(rq, p))
2803 dequeue_pushable_task(rq, p);
2804 BUG_ON(!rq->rt.rt_nr_migratory);
2805 rq->rt.rt_nr_migratory--;
2806 } else {
2807 if (!task_current(rq, p))
2808 enqueue_pushable_task(rq, p);
2809 rq->rt.rt_nr_migratory++;
2810 }
2811
2812 update_rt_migration(&rq->rt);
2813 }
2814
2815 /* Assumes rq->lock is held */
2816 static void rq_online_rt(struct rq *rq)
2817 {
2818 if (rq->rt.overloaded)
2819 rt_set_overload(rq);
2820
2821 __enable_runtime(rq);
2822
2823 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
2824 }
2825
2826 /* Assumes rq->lock is held */
2827 static void rq_offline_rt(struct rq *rq)
2828 {
2829 if (rq->rt.overloaded)
2830 rt_clear_overload(rq);
2831
2832 __disable_runtime(rq);
2833
2834 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
2835 }
2836
2837 /*
2838 * When switch from the rt queue, we bring ourselves to a position
2839 * that we might want to pull RT tasks from other runqueues.
2840 */
2841 static void switched_from_rt(struct rq *rq, struct task_struct *p)
2842 {
2843 /*
2844 * If there are other RT tasks then we will reschedule
2845 * and the scheduling of the other RT tasks will handle
2846 * the balancing. But if we are the last RT task
2847 * we may need to handle the pulling of RT tasks
2848 * now.
2849 */
2850 if (!p->on_rq || rq->rt.rt_nr_running)
2851 return;
2852
2853 if (pull_rt_task(rq))
2854 resched_task(rq->curr);
2855 }
2856
2857 void init_sched_rt_class(void)
2858 {
2859 unsigned int i;
2860
2861 for_each_possible_cpu(i) {
2862 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
2863 GFP_KERNEL, cpu_to_node(i));
2864 }
2865 }
2866 #endif /* CONFIG_SMP */
2867
2868 /*
2869 * When switching a task to RT, we may overload the runqueue
2870 * with RT tasks. In this case we try to push them off to
2871 * other runqueues.
2872 */
2873 static void switched_to_rt(struct rq *rq, struct task_struct *p)
2874 {
2875 int check_resched = 1;
2876
2877 /*
2878 * If we are already running, then there's nothing
2879 * that needs to be done. But if we are not running
2880 * we may need to preempt the current running task.
2881 * If that current running task is also an RT task
2882 * then see if we can move to another run queue.
2883 */
2884 if (p->on_rq && rq->curr != p) {
2885 #ifdef CONFIG_SMP
2886 if (rq->rt.overloaded && push_rt_task(rq) &&
2887 /* Don't resched if we changed runqueues */
2888 rq != task_rq(p))
2889 check_resched = 0;
2890 #endif /* CONFIG_SMP */
2891 if (check_resched && p->prio < rq->curr->prio)
2892 resched_task(rq->curr);
2893 }
2894 }
2895
2896 /*
2897 * Priority of the task has changed. This may cause
2898 * us to initiate a push or pull.
2899 */
2900 static void
2901 prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
2902 {
2903 if (!p->on_rq)
2904 return;
2905
2906 if (rq->curr == p) {
2907 #ifdef CONFIG_SMP
2908 /*
2909 * If our priority decreases while running, we
2910 * may need to pull tasks to this runqueue.
2911 */
2912 if (oldprio < p->prio)
2913 pull_rt_task(rq);
2914 /*
2915 * If there's a higher priority task waiting to run
2916 * then reschedule. Note, the above pull_rt_task
2917 * can release the rq lock and p could migrate.
2918 * Only reschedule if p is still on the same runqueue.
2919 */
2920 if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
2921 resched_task(p);
2922 #else
2923 /* For UP simply resched on drop of prio */
2924 if (oldprio < p->prio)
2925 resched_task(p);
2926 #endif /* CONFIG_SMP */
2927 } else {
2928 /*
2929 * This task is not running, but if it is
2930 * greater than the current running task
2931 * then reschedule.
2932 */
2933 if (p->prio < rq->curr->prio)
2934 resched_task(rq->curr);
2935 }
2936 }
2937
2938 static void watchdog(struct rq *rq, struct task_struct *p)
2939 {
2940 unsigned long soft, hard;
2941
2942 /* max may change after cur was read, this will be fixed next tick */
2943 soft = task_rlimit(p, RLIMIT_RTTIME);
2944 hard = task_rlimit_max(p, RLIMIT_RTTIME);
2945
2946 if (soft != RLIM_INFINITY) {
2947 unsigned long next;
2948
2949 if (p->rt.watchdog_stamp != jiffies) {
2950 p->rt.timeout++;
2951 p->rt.watchdog_stamp = jiffies;
2952 }
2953
2954 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
2955 if (p->rt.timeout > next)
2956 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
2957 }
2958 }
2959
2960 static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
2961 {
2962 struct sched_rt_entity *rt_se = &p->rt;
2963
2964 update_curr_rt(rq);
2965
2966 watchdog(rq, p);
2967
2968 /*
2969 * RR tasks need a special form of timeslice management.
2970 * FIFO tasks have no timeslices.
2971 */
2972 if (p->policy != SCHED_RR)
2973 return;
2974
2975 if (--p->rt.time_slice)
2976 return;
2977
2978 p->rt.time_slice = sched_rr_timeslice;
2979
2980 /*
2981 * Requeue to the end of queue if we (and all of our ancestors) are the
2982 * only element on the queue
2983 */
2984 for_each_sched_rt_entity(rt_se) {
2985 if (rt_se->run_list.prev != rt_se->run_list.next) {
2986 requeue_task_rt(rq, p, 0);
2987 set_tsk_need_resched(p);
2988 return;
2989 }
2990 }
2991 }
2992
2993 static void set_curr_task_rt(struct rq *rq)
2994 {
2995 struct task_struct *p = rq->curr;
2996
2997 p->se.exec_start = rq->clock_task;
2998
2999 /* The running task is never eligible for pushing */
3000 dequeue_pushable_task(rq, p);
3001 }
3002
3003 static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
3004 {
3005 /*
3006 * Time slice is 0 for SCHED_FIFO tasks
3007 */
3008 if (task->policy == SCHED_RR)
3009 return sched_rr_timeslice;
3010 else
3011 return 0;
3012 }
3013
3014 const struct sched_class rt_sched_class = {
3015 .next = &fair_sched_class,
3016 .enqueue_task = enqueue_task_rt,
3017 .dequeue_task = dequeue_task_rt,
3018 .yield_task = yield_task_rt,
3019
3020 .check_preempt_curr = check_preempt_curr_rt,
3021
3022 .pick_next_task = pick_next_task_rt,
3023 .put_prev_task = put_prev_task_rt,
3024
3025 #ifdef CONFIG_SMP
3026 .select_task_rq = select_task_rq_rt,
3027
3028 .set_cpus_allowed = set_cpus_allowed_rt,
3029 .rq_online = rq_online_rt,
3030 .rq_offline = rq_offline_rt,
3031 .pre_schedule = pre_schedule_rt,
3032 .post_schedule = post_schedule_rt,
3033 .task_woken = task_woken_rt,
3034 .switched_from = switched_from_rt,
3035 #endif
3036
3037 .set_curr_task = set_curr_task_rt,
3038 .task_tick = task_tick_rt,
3039
3040 .get_rr_interval = get_rr_interval_rt,
3041
3042 .prio_changed = prio_changed_rt,
3043 .switched_to = switched_to_rt,
3044 };
3045
3046 #ifdef CONFIG_SCHED_DEBUG
3047 extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
3048
3049 void print_rt_stats(struct seq_file *m, int cpu)
3050 {
3051 rt_rq_iter_t iter;
3052 struct rt_rq *rt_rq;
3053
3054 rcu_read_lock();
3055 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
3056 print_rt_rq(m, cpu, rt_rq);
3057 rcu_read_unlock();
3058 }
3059 #endif /* CONFIG_SCHED_DEBUG */