2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
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>
12 #include <linux/slab.h>
14 int sched_rr_timeslice
= RR_TIMESLICE
;
16 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
);
18 struct rt_bandwidth def_rt_bandwidth
;
20 static enum hrtimer_restart
sched_rt_period_timer(struct hrtimer
*timer
)
22 struct rt_bandwidth
*rt_b
=
23 container_of(timer
, struct rt_bandwidth
, rt_period_timer
);
29 now
= hrtimer_cb_get_time(timer
);
30 overrun
= hrtimer_forward(timer
, now
, rt_b
->rt_period
);
35 idle
= do_sched_rt_period_timer(rt_b
, overrun
);
38 return idle
? HRTIMER_NORESTART
: HRTIMER_RESTART
;
41 void init_rt_bandwidth(struct rt_bandwidth
*rt_b
, u64 period
, u64 runtime
)
43 rt_b
->rt_period
= ns_to_ktime(period
);
44 rt_b
->rt_runtime
= runtime
;
46 raw_spin_lock_init(&rt_b
->rt_runtime_lock
);
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
;
53 static void start_rt_bandwidth(struct rt_bandwidth
*rt_b
)
55 if (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
)
58 if (hrtimer_active(&rt_b
->rt_period_timer
))
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
);
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
);
72 void init_rt_rq(struct rt_rq
*rt_rq
, struct rq
*rq
)
74 struct rt_prio_array
*array
;
76 #ifdef CONFIG_PROVE_LOCKING
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
);
85 /* delimiter for bitsearch: */
86 __set_bit(MAX_RT_PRIO
, array
->bitmap
);
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
);
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
);
104 idx
= per_cpu(spin_key_idx
, cpu
);
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
);
115 #ifdef CONFIG_RT_GROUP_SCHED
116 static void destroy_rt_bandwidth(struct rt_bandwidth
*rt_b
)
118 hrtimer_cancel(&rt_b
->rt_period_timer
);
121 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
123 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
125 #ifdef CONFIG_SCHED_DEBUG
126 WARN_ON_ONCE(!rt_entity_is_task(rt_se
));
128 return container_of(rt_se
, struct task_struct
, rt
);
131 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
136 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
141 void free_rt_sched_group(struct task_group
*tg
)
146 destroy_rt_bandwidth(&tg
->rt_bandwidth
);
148 for_each_possible_cpu(i
) {
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
)
163 struct rq
*rq
= cpu_rq(cpu
);
165 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
166 rt_rq
->rt_nr_boosted
= 0;
170 tg
->rt_rq
[cpu
] = rt_rq
;
171 tg
->rt_se
[cpu
] = rt_se
;
177 rt_se
->rt_rq
= &rq
->rt
;
179 rt_se
->rt_rq
= parent
->my_q
;
182 rt_se
->parent
= parent
;
183 INIT_LIST_HEAD(&rt_se
->run_list
);
186 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
189 struct sched_rt_entity
*rt_se
;
192 tg
->rt_rq
= kzalloc(sizeof(rt_rq
) * nr_cpu_ids
, GFP_KERNEL
);
195 tg
->rt_se
= kzalloc(sizeof(rt_se
) * nr_cpu_ids
, GFP_KERNEL
);
199 init_rt_bandwidth(&tg
->rt_bandwidth
,
200 ktime_to_ns(def_rt_bandwidth
.rt_period
), 0);
202 for_each_possible_cpu(i
) {
203 rt_rq
= kzalloc_node(sizeof(struct rt_rq
),
204 GFP_KERNEL
, cpu_to_node(i
));
208 rt_se
= kzalloc_node(sizeof(struct sched_rt_entity
),
209 GFP_KERNEL
, cpu_to_node(i
));
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
]);
226 #else /* CONFIG_RT_GROUP_SCHED */
228 #define rt_entity_is_task(rt_se) (1)
230 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
232 return container_of(rt_se
, struct task_struct
, rt
);
235 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
237 return container_of(rt_rq
, struct rq
, rt
);
240 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
242 struct task_struct
*p
= rt_task_of(rt_se
);
243 struct rq
*rq
= task_rq(p
);
248 void free_rt_sched_group(struct task_group
*tg
) { }
250 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
254 #endif /* CONFIG_RT_GROUP_SCHED */
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
;
263 static inline int rt_overloaded(struct rq
*rq
)
265 return atomic_read(&rq
->rd
->rto_count
);
268 static inline void rt_set_overload(struct rq
*rq
)
273 cpumask_set_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
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
282 atomic_inc(&rq
->rd
->rto_count
);
285 static inline void rt_clear_overload(struct rq
*rq
)
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
);
295 #ifdef CONFIG_MT_RT_SCHED
297 static inline int rt_overloaded_in_big(struct rq
*rq
)
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]);
306 if (cpumask_empty(new_mask
))
312 static inline int has_rt_task_in_little(void)
317 for_each_cpu(cpu
, &hmp_slow_cpu_mask
){
318 if (!cpu_online(cpu
))
322 if(rq
->rt
.rt_nr_running
>= 1)
330 static void update_rt_migration(struct rt_rq
*rt_rq
)
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;
337 } else if (rt_rq
->overloaded
) {
338 rt_clear_overload(rq_of_rt_rq(rt_rq
));
339 rt_rq
->overloaded
= 0;
343 static void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
345 struct task_struct
*p
;
347 if (!rt_entity_is_task(rt_se
))
350 p
= rt_task_of(rt_se
);
351 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
353 rt_rq
->rt_nr_total
++;
354 if (p
->nr_cpus_allowed
> 1)
355 rt_rq
->rt_nr_migratory
++;
357 update_rt_migration(rt_rq
);
360 static void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
362 struct task_struct
*p
;
364 if (!rt_entity_is_task(rt_se
))
367 p
= rt_task_of(rt_se
);
368 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
370 rt_rq
->rt_nr_total
--;
371 if (p
->nr_cpus_allowed
> 1)
372 rt_rq
->rt_nr_migratory
--;
374 update_rt_migration(rt_rq
);
377 static inline int has_pushable_tasks(struct rq
*rq
)
379 return !plist_head_empty(&rq
->rt
.pushable_tasks
);
382 static void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
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
);
388 /* Update the highest prio pushable task */
389 if (p
->prio
< rq
->rt
.highest_prio
.next
)
390 rq
->rt
.highest_prio
.next
= p
->prio
;
393 static void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
395 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
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
;
403 rq
->rt
.highest_prio
.next
= MAX_RT_PRIO
;
408 static inline void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
412 static inline void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
417 void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
422 void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
426 #endif /* CONFIG_SMP */
428 static inline int on_rt_rq(struct sched_rt_entity
*rt_se
)
430 return !list_empty(&rt_se
->run_list
);
433 #ifdef CONFIG_RT_GROUP_SCHED
435 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
440 return rt_rq
->rt_runtime
;
443 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
445 return ktime_to_ns(rt_rq
->tg
->rt_bandwidth
.rt_period
);
448 typedef struct task_group
*rt_rq_iter_t
;
450 static inline struct task_group
*next_task_group(struct task_group
*tg
)
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
));
457 if (&tg
->list
== &task_groups
)
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)]);)
468 static inline void list_add_leaf_rt_rq(struct rt_rq
*rt_rq
)
470 list_add_rcu(&rt_rq
->leaf_rt_rq_list
,
471 &rq_of_rt_rq(rt_rq
)->leaf_rt_rq_list
);
474 static inline void list_del_leaf_rt_rq(struct rt_rq
*rt_rq
)
476 list_del_rcu(&rt_rq
->leaf_rt_rq_list
);
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)
482 #define for_each_sched_rt_entity(rt_se) \
483 for (; rt_se; rt_se = rt_se->parent)
485 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
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
);
493 static void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
495 struct task_struct
*curr
= rq_of_rt_rq(rt_rq
)->curr
;
496 struct sched_rt_entity
*rt_se
;
498 int cpu
= cpu_of(rq_of_rt_rq(rt_rq
));
500 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
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
)
510 static void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
512 struct sched_rt_entity
*rt_se
;
513 int cpu
= cpu_of(rq_of_rt_rq(rt_rq
));
515 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
517 if (rt_se
&& on_rt_rq(rt_se
))
518 dequeue_rt_entity(rt_se
);
521 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
523 return rt_rq
->rt_throttled
&& !rt_rq
->rt_nr_boosted
;
526 static int rt_se_boosted(struct sched_rt_entity
*rt_se
)
528 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
529 struct task_struct
*p
;
532 return !!rt_rq
->rt_nr_boosted
;
534 p
= rt_task_of(rt_se
);
535 return p
->prio
!= p
->normal_prio
;
539 static inline const struct cpumask
*sched_rt_period_mask(void)
541 return cpu_rq(smp_processor_id())->rd
->span
;
544 static inline const struct cpumask
*sched_rt_period_mask(void)
546 return cpu_online_mask
;
551 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
553 return container_of(rt_b
, struct task_group
, rt_bandwidth
)->rt_rq
[cpu
];
556 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
558 return &rt_rq
->tg
->rt_bandwidth
;
561 void unthrottle_offline_rt_rqs(struct rq
*rq
) {
564 for_each_leaf_rt_rq(rt_rq
, rq
) {
566 * clock_task is not advancing so we just need to make sure
567 * there's some valid quota amount
569 if (rt_rq_throttled(rt_rq
)){
570 rt_rq
->rt_throttled
= 0;
571 printk(KERN_ERR
"sched: RT throttling inactivated\n");
576 #else /* !CONFIG_RT_GROUP_SCHED */
578 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
580 return rt_rq
->rt_runtime
;
583 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
585 return ktime_to_ns(def_rt_bandwidth
.rt_period
);
588 typedef struct rt_rq
*rt_rq_iter_t
;
590 #define for_each_rt_rq(rt_rq, iter, rq) \
591 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
593 static inline void list_add_leaf_rt_rq(struct rt_rq
*rt_rq
)
597 static inline void list_del_leaf_rt_rq(struct rt_rq
*rt_rq
)
601 #define for_each_leaf_rt_rq(rt_rq, rq) \
602 for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
604 #define for_each_sched_rt_entity(rt_se) \
605 for (; rt_se; rt_se = NULL)
607 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
612 static inline void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
614 if (rt_rq
->rt_nr_running
)
615 resched_task(rq_of_rt_rq(rt_rq
)->curr
);
618 static inline void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
622 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
624 return rt_rq
->rt_throttled
;
627 static inline const struct cpumask
*sched_rt_period_mask(void)
629 return cpu_online_mask
;
633 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
635 return &cpu_rq(cpu
)->rt
;
638 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
640 return &def_rt_bandwidth
;
643 void unthrottle_offline_rt_rqs(struct rq
*rq
) { }
645 #endif /* CONFIG_RT_GROUP_SCHED */
649 * We ran out of runtime, see if we can borrow some from our neighbours.
651 //#define MTK_DEBUG_CGROUP
652 static int do_balance_runtime(struct rt_rq
*rt_rq
)
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;
659 weight
= cpumask_weight(rd
->span
);
661 raw_spin_lock(&rt_b
->rt_runtime_lock
);
662 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
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
);
669 rt_period
= ktime_to_ns(rt_b
->rt_period
);
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]);
675 for_each_cpu(i
, rd
->span
) {
676 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
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
);
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
);
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.
698 if (iter
->rt_disable_borrow
==1)
700 if (iter
->rt_runtime
== RUNTIME_INF
)
704 * From runqueues with spare time, take 1/n part of their
705 * spare time, but no more than our period.
707 diff
= iter
->rt_runtime
- iter
->rt_time
;
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]);
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
;
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]);
726 if (rt_rq
->rt_runtime
== rt_period
) {
727 raw_spin_unlock(&iter
->rt_runtime_lock
);
732 raw_spin_unlock(&iter
->rt_runtime_lock
);
734 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
735 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
741 * Ensure this RQ takes back all the runtime it lend to its neighbours.
743 static void __disable_runtime(struct rq
*rq
)
745 struct root_domain
*rd
= rq
->rd
;
749 if (unlikely(!scheduler_running
))
752 for_each_rt_rq(rt_rq
, iter
, rq
) {
753 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
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;
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.
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
);
771 if (rt_rq
->rt_runtime
== RUNTIME_INF
||
772 rt_rq
->rt_runtime
== rt_b
->rt_runtime
)
774 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
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.
781 want
= rt_b
->rt_runtime
- rt_rq
->rt_runtime
;
784 * Greedy reclaim, take back as much as we can.
786 for_each_cpu(i
, rd
->span
) {
787 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
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]);
796 * Can't reclaim from ourselves or disabled runqueues.
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
);
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
);
812 diff
= min_t(s64
, iter
->rt_runtime
, want
);
813 iter
->rt_runtime
-= 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
);
820 iter
->rt_runtime
-= want
;
822 #ifdef MTK_DEBUG_CGROUP
823 printk(KERN_EMERG
"3. disable_runtime, rt_runtime=%llu, want=%lld\n", iter
->rt_runtime
, want
);
826 raw_spin_unlock(&iter
->rt_runtime_lock
);
832 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
834 * We cannot be left wanting - that would mean some runtime
835 * leaked out of the system.
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
);
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
);
857 * Disable all the borrow logic by pretending we have inf
858 * runtime - in which case borrowing doesn't make sense.
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
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
);
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
);
884 #ifdef CONFIG_MT_RT_SCHED_CRIT
885 trace_sched_rt_crit(rq
->cpu
, rq
->rt
.rt_throttled
);
889 static void disable_runtime(struct rq
*rq
)
893 raw_spin_lock_irqsave(&rq
->lock
, flags
);
894 __disable_runtime(rq
);
895 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
898 static void __enable_runtime(struct rq
*rq
)
903 if (unlikely(!scheduler_running
))
907 * Reset each runqueue's bandwidth settings
909 for_each_rt_rq(rt_rq
, iter
, rq
) {
910 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
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
);
918 rt_rq
->rt_runtime
= rt_b
->rt_runtime
;
920 rt_rq
->rt_throttled
= 0;
921 rt_rq
->rt_disable_borrow
= 0;
923 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
924 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
927 #ifdef CONFIG_MT_RT_SCHED_CRIT
928 trace_sched_rt_crit(rq
->cpu
, rq
->rt
.rt_throttled
);
932 static void enable_runtime(struct rq
*rq
)
936 raw_spin_lock_irqsave(&rq
->lock
, flags
);
937 __enable_runtime(rq
);
938 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
941 int update_runtime(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
943 int cpu
= (int)(long)hcpu
;
946 case CPU_DOWN_PREPARE
:
947 case CPU_DOWN_PREPARE_FROZEN
:
948 disable_runtime(cpu_rq(cpu
));
951 case CPU_DOWN_FAILED
:
952 case CPU_DOWN_FAILED_FROZEN
:
954 case CPU_ONLINE_FROZEN
:
955 enable_runtime(cpu_rq(cpu
));
963 static int balance_runtime(struct rt_rq
*rt_rq
)
967 if (!sched_feat(RT_RUNTIME_SHARE
))
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
);
978 #else /* !CONFIG_SMP */
979 static inline int balance_runtime(struct rt_rq
*rt_rq
)
983 #endif /* CONFIG_SMP */
985 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
)
987 int i
, idle
= 1, throttled
= 0;
988 const struct cpumask
*span
;
990 span
= sched_rt_period_mask();
991 #ifdef CONFIG_RT_GROUP_SCHED
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.
1001 if (rt_b
== &root_task_group
.rt_bandwidth
)
1002 span
= cpu_online_mask
;
1005 #ifdef MTK_DEBUG_CGROUP
1006 printk(KERN_EMERG
" do_sched_rt_period_timer curr_cpu=%d \n", smp_processor_id());
1008 for_each_cpu(i
, span
) {
1010 struct rt_rq
*rt_rq
= sched_rt_period_rt_rq(rt_b
, i
);
1011 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1013 raw_spin_lock(&rq
->lock
);
1014 if (rt_rq
->rt_time
) {
1016 u64 runtime_pre
, rt_time_pre
;
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
;
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
);
1033 if (rt_rq
->rt_throttled
&& rt_rq
->rt_time
< runtime
) {
1034 printk_deferred("sched: RT throttling inactivated"
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
);
1044 * Force a clock update if the CPU was idle,
1045 * lest wakeup -> unthrottle time accumulate.
1047 if (rt_rq
->rt_nr_running
&& rq
->curr
== rq
->idle
)
1048 rq
->skip_clock_update
= -1;
1050 if (rt_rq
->rt_time
|| rt_rq
->rt_nr_running
)
1052 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
1053 } else if (rt_rq
->rt_nr_running
) {
1055 if (!rt_rq_throttled(rt_rq
))
1058 if (rt_rq
->rt_throttled
)
1062 sched_rt_rq_enqueue(rt_rq
);
1063 raw_spin_unlock(&rq
->lock
);
1066 if (!throttled
&& (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
))
1072 static inline int rt_se_prio(struct sched_rt_entity
*rt_se
)
1074 #ifdef CONFIG_RT_GROUP_SCHED
1075 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
1078 return rt_rq
->highest_prio
.curr
;
1081 return rt_task_of(rt_se
)->prio
;
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
)
1088 u64 runtime
= sched_rt_runtime(rt_rq
);
1091 if (rt_rq
->rt_throttled
)
1092 return rt_rq_throttled(rt_rq
);
1094 if (runtime
>= sched_rt_period(rt_rq
))
1097 runtime_pre
= runtime
;
1098 balance_runtime(rt_rq
);
1099 runtime
= sched_rt_runtime(rt_rq
);
1100 if (runtime
== RUNTIME_INF
)
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
);
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
));
1115 * Don't actually throttle groups that have no runtime assigned
1116 * but accrue some time due to boosting.
1118 /* MTK patch: print rt throttle everytime*/
1119 if (likely(rt_b
->rt_runtime
)) {
1120 // static bool once = false;
1122 rt_rq
->rt_throttled
= 1;
1126 printk_deferred("sched: RT throttling activated cpu=%d\n",
1129 #ifdef CONFIG_MT_RT_SCHED_CRIT
1130 trace_sched_rt_crit(cpu
, rt_rq
->rt_throttled
);
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.
1142 if (rt_rq_throttled(rt_rq
)) {
1143 sched_rt_rq_dequeue(rt_rq
);
1152 * Update the current task's runtime statistics. Skip current tasks that
1153 * are not in our scheduling class.
1155 static void update_curr_rt(struct rq
*rq
)
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
);
1161 int cpu
= rq_cpu(rq
);
1163 if (curr
->sched_class
!= &rt_sched_class
)
1166 delta_exec
= rq
->clock_task
- curr
->se
.exec_start
;
1167 if (unlikely((s64
)delta_exec
<= 0))
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
);
1178 curr
->se
.exec_start
= rq
->clock_task
;
1179 cpuacct_charge(curr
, delta_exec
);
1181 sched_rt_avg_update(rq
, delta_exec
);
1183 if (!rt_bandwidth_enabled())
1186 for_each_sched_rt_entity(rt_se
) {
1187 rt_rq
= rt_rq_of_se(rt_se
);
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
))
1194 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
1199 #if defined CONFIG_SMP
1202 inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
1204 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1206 #ifdef CONFIG_RT_GROUP_SCHED
1208 * Change rq's cpupri only if rt_rq is the top queue.
1210 if (&rq
->rt
!= rt_rq
)
1213 if (rq
->online
&& prio
< prev_prio
)
1214 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, prio
);
1218 dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
1220 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1222 #ifdef CONFIG_RT_GROUP_SCHED
1224 * Change rq's cpupri only if rt_rq is the top queue.
1226 if (&rq
->rt
!= rt_rq
)
1229 if (rq
->online
&& rt_rq
->highest_prio
.curr
!= prev_prio
)
1230 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rt_rq
->highest_prio
.curr
);
1233 #else /* CONFIG_SMP */
1236 void inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
1238 void dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
1240 #endif /* CONFIG_SMP */
1242 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1244 inc_rt_prio(struct rt_rq
*rt_rq
, int prio
)
1246 int prev_prio
= rt_rq
->highest_prio
.curr
;
1248 if (prio
< prev_prio
)
1249 rt_rq
->highest_prio
.curr
= prio
;
1251 inc_rt_prio_smp(rt_rq
, prio
, prev_prio
);
1255 dec_rt_prio(struct rt_rq
*rt_rq
, int prio
)
1257 int prev_prio
= rt_rq
->highest_prio
.curr
;
1259 if (rt_rq
->rt_nr_running
) {
1261 WARN_ON(prio
< prev_prio
);
1264 * This may have been our highest task, and therefore
1265 * we may have some recomputation to do
1267 if (prio
== prev_prio
) {
1268 struct rt_prio_array
*array
= &rt_rq
->active
;
1270 rt_rq
->highest_prio
.curr
=
1271 sched_find_first_bit(array
->bitmap
);
1275 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
1277 dec_rt_prio_smp(rt_rq
, prio
, prev_prio
);
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
) {}
1285 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1287 #ifdef CONFIG_RT_GROUP_SCHED
1290 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1292 if (rt_se_boosted(rt_se
))
1293 rt_rq
->rt_nr_boosted
++;
1296 start_rt_bandwidth(&rt_rq
->tg
->rt_bandwidth
);
1300 dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1302 if (rt_se_boosted(rt_se
))
1303 rt_rq
->rt_nr_boosted
--;
1305 WARN_ON(!rt_rq
->rt_nr_running
&& rt_rq
->rt_nr_boosted
);
1308 #else /* CONFIG_RT_GROUP_SCHED */
1311 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1313 start_rt_bandwidth(&def_rt_bandwidth
);
1317 void dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
) {}
1319 #endif /* CONFIG_RT_GROUP_SCHED */
1322 void inc_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1324 int prio
= rt_se_prio(rt_se
);
1326 WARN_ON(!rt_prio(prio
));
1327 rt_rq
->rt_nr_running
++;
1329 inc_rt_prio(rt_rq
, prio
);
1330 inc_rt_migration(rt_se
, rt_rq
);
1331 inc_rt_group(rt_se
, rt_rq
);
1335 void dec_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1337 WARN_ON(!rt_prio(rt_se_prio(rt_se
)));
1338 WARN_ON(!rt_rq
->rt_nr_running
);
1339 rt_rq
->rt_nr_running
--;
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
);
1346 static void __enqueue_rt_entity(struct sched_rt_entity
*rt_se
, bool head
)
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
);
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
1359 // if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
1360 if (group_rq
&& ( !group_rq
->rt_nr_running
))
1363 if (!rt_rq
->rt_nr_running
)
1364 list_add_leaf_rt_rq(rt_rq
);
1367 list_add(&rt_se
->run_list
, queue
);
1369 list_add_tail(&rt_se
->run_list
, queue
);
1370 __set_bit(rt_se_prio(rt_se
), array
->bitmap
);
1372 inc_rt_tasks(rt_se
, rt_rq
);
1375 static void __dequeue_rt_entity(struct sched_rt_entity
*rt_se
)
1377 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1378 struct rt_prio_array
*array
= &rt_rq
->active
;
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
);
1384 dec_rt_tasks(rt_se
, rt_rq
);
1385 if (!rt_rq
->rt_nr_running
)
1386 list_del_leaf_rt_rq(rt_rq
);
1390 * Because the prio of an upper entry depends on the lower
1391 * entries, we must remove entries top - down.
1393 static void dequeue_rt_stack(struct sched_rt_entity
*rt_se
)
1395 struct sched_rt_entity
*back
= NULL
;
1397 for_each_sched_rt_entity(rt_se
) {
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
);
1408 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, bool head
)
1410 dequeue_rt_stack(rt_se
);
1411 for_each_sched_rt_entity(rt_se
)
1412 __enqueue_rt_entity(rt_se
, head
);
1415 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
)
1417 dequeue_rt_stack(rt_se
);
1419 for_each_sched_rt_entity(rt_se
) {
1420 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
1422 if (rt_rq
&& rt_rq
->rt_nr_running
)
1423 __enqueue_rt_entity(rt_se
, false);
1428 * Adding/removing a task to/from a priority array:
1431 enqueue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1433 struct sched_rt_entity
*rt_se
= &p
->rt
;
1435 if (flags
& ENQUEUE_WAKEUP
)
1438 enqueue_rt_entity(rt_se
, flags
& ENQUEUE_HEAD
);
1440 if (!task_current(rq
, p
) && p
->nr_cpus_allowed
> 1)
1441 enqueue_pushable_task(rq
, p
);
1446 static void dequeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1448 struct sched_rt_entity
*rt_se
= &p
->rt
;
1451 dequeue_rt_entity(rt_se
);
1453 dequeue_pushable_task(rq
, p
);
1459 * Put task to the head or the end of the run list without the overhead of
1460 * dequeue followed by enqueue.
1463 requeue_rt_entity(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
, int head
)
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
);
1470 list_move(&rt_se
->run_list
, queue
);
1472 list_move_tail(&rt_se
->run_list
, queue
);
1476 static void requeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int head
)
1478 struct sched_rt_entity
*rt_se
= &p
->rt
;
1479 struct rt_rq
*rt_rq
;
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
);
1487 static void yield_task_rt(struct rq
*rq
)
1489 requeue_task_rt(rq
, rq
->curr
, 0);
1493 static int find_lowest_rq(struct task_struct
*task
);
1496 select_task_rq_rt(struct task_struct
*p
, int sd_flag
, int flags
)
1498 struct task_struct
*curr
;
1504 if (p
->nr_cpus_allowed
== 1)
1507 /* For anything but wake ups, just return the task_cpu */
1508 if (sd_flag
!= SD_BALANCE_WAKE
&& sd_flag
!= SD_BALANCE_FORK
)
1514 curr
= ACCESS_ONCE(rq
->curr
); /* unlocked access */
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.
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
1530 * For equal prio tasks, we just let the scheduler sort it out.
1532 * Otherwise, just let it ride on the affined RQ and the
1533 * post-schedule router will push the preempted task away
1535 * This test is optimistic, if we get it wrong the load-balancer
1536 * will have to sort it out.
1538 #ifdef CONFIG_MT_RT_SCHED_INFO
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
);
1544 mt_rt_printf("0. select_task_rq_rt cpu=%d curr=%d:%s:%d",
1545 cpu
, p
->pid
, p
->comm
, p
->prio
);
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) ){
1554 unlikely(rt_task(curr
)) &&
1555 (curr
->nr_cpus_allowed
< 2 || curr
->prio
<= p
->prio
)
1556 && (p
->nr_cpus_allowed
> 1)) {
1558 int target
= find_lowest_rq(p
);
1563 #ifdef CONFIG_MT_RT_SCHED_INFO
1564 mt_rt_printf("1. select_task_rq_rt %d:%d:%s", cpu
, p
->pid
, p
->comm
);
1573 static void check_preempt_equal_prio(struct rq
*rq
, struct task_struct
*p
)
1575 if (rq
->curr
->nr_cpus_allowed
== 1)
1578 if (p
->nr_cpus_allowed
!= 1
1579 && cpupri_find(&rq
->rd
->cpupri
, p
, NULL
))
1582 if (!cpupri_find(&rq
->rd
->cpupri
, rq
->curr
, NULL
))
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:
1590 requeue_task_rt(rq
, p
, 1);
1591 resched_task(rq
->curr
);
1594 #endif /* CONFIG_SMP */
1597 * Preempt the current task with a newly woken task if needed:
1599 static void check_preempt_curr_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
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
);
1604 if (p
->prio
< rq
->curr
->prio
) {
1605 resched_task(rq
->curr
);
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
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
1622 if (p
->prio
== rq
->curr
->prio
&& !test_tsk_need_resched(rq
->curr
))
1623 check_preempt_equal_prio(rq
, p
);
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
)
1632 struct rt_prio_array
*array
= &rt_rq
->active
;
1633 struct sched_rt_entity
*next
= NULL
;
1634 struct sched_rt_entity
*rt_se
;
1637 idx
= sched_find_first_bit(array
->bitmap
);
1638 BUG_ON(idx
>= MAX_RT_PRIO
);
1641 list_for_each_entry(rt_se
, array
->queue
+ idx
, run_list
) {
1642 struct task_struct
*p
;
1644 if (!rt_entity_is_task(rt_se
)){
1649 p
= rt_task_of(rt_se
);
1650 if ( (!cpu_online(rq
->cpu
)) || (!test_tsk_need_released(p
))) {
1654 #ifdef CONFIG_MT_RT_SCHED_INFO
1655 mt_rt_printf("1. pick_next_rt_entity bypass %d %s", p
->pid
, p
->comm
);
1660 idx
= find_next_bit(array
->bitmap
, MAX_RT_PRIO
, idx
+1);
1661 if (idx
< MAX_RT_PRIO
)
1669 static struct sched_rt_entity
*pick_next_rt_entity(struct rq
*rq
,
1670 struct rt_rq
*rt_rq
)
1672 struct rt_prio_array
*array
= &rt_rq
->active
;
1673 struct sched_rt_entity
*next
= NULL
;
1674 struct list_head
*queue
;
1677 idx
= sched_find_first_bit(array
->bitmap
);
1678 BUG_ON(idx
>= MAX_RT_PRIO
);
1680 queue
= array
->queue
+ idx
;
1681 next
= list_entry(queue
->next
, struct sched_rt_entity
, run_list
);
1687 static struct task_struct
*_pick_next_task_rt(struct rq
*rq
)
1689 struct sched_rt_entity
*rt_se
;
1690 struct task_struct
*p
;
1691 struct rt_rq
*rt_rq
;
1695 if (!rt_rq
->rt_nr_running
)
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
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
);
1717 rt_se
= pick_next_rt_entity(rq
, rt_rq
);
1718 #ifdef CONFIG_MT_RT_SCHED
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
);
1729 rt_rq
= group_rt_rq(rt_se
);
1732 p
= rt_task_of(rt_se
);
1733 p
->se
.exec_start
= rq
->clock_task
;
1738 static struct task_struct
*pick_next_task_rt(struct rq
*rq
)
1740 struct task_struct
*p
= _pick_next_task_rt(rq
);
1742 /* The running task is never eligible for pushing */
1744 dequeue_pushable_task(rq
, p
);
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
1751 rq
->post_schedule
= has_pushable_tasks(rq
);
1757 static void put_prev_task_rt(struct rq
*rq
, struct task_struct
*p
)
1762 * The previous task needs to be made eligible for pushing
1763 * if it is still active
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
);
1769 if (on_rt_rq(&p
->rt
) && p
->nr_cpus_allowed
> 1)
1770 enqueue_pushable_task(rq
, p
);
1776 /* Only try algorithms three times */
1777 #define RT_MAX_TRIES 3
1779 static int pick_rt_task(struct rq
*rq
, struct task_struct
*p
, int cpu
)
1781 if (!task_running(rq
, p
) &&
1782 cpumask_test_cpu(cpu
, tsk_cpus_allowed(p
)))
1787 /* Return the second highest RT task, NULL otherwise */
1788 static struct task_struct
*pick_next_highest_task_rt(struct rq
*rq
, int cpu
)
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
;
1796 for_each_leaf_rt_rq(rt_rq
, rq
) {
1797 array
= &rt_rq
->active
;
1798 idx
= sched_find_first_bit(array
->bitmap
);
1800 if (idx
>= MAX_RT_PRIO
)
1802 if (next
&& next
->prio
<= idx
)
1804 list_for_each_entry(rt_se
, array
->queue
+ idx
, run_list
) {
1805 struct task_struct
*p
;
1807 if (!rt_entity_is_task(rt_se
))
1810 p
= rt_task_of(rt_se
);
1811 if (pick_rt_task(rq
, p
, cpu
)) {
1817 idx
= find_next_bit(array
->bitmap
, MAX_RT_PRIO
, idx
+1);
1825 static DEFINE_PER_CPU(cpumask_var_t
, local_cpu_mask
);
1827 #ifdef CONFIG_MT_RT_SCHED
1828 static int test_has_highest_prio(int this_cpu
)
1830 int cpu
, highest_prio
;
1831 struct rq
*this_rq
= cpu_rq(this_cpu
), *rq
;
1832 int prio
= this_rq
->curr
->prio
;
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]);
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
);
1846 for_each_cpu(cpu
, &hmp_fast_cpu_mask
) {
1847 if(!cpu_online(cpu
))
1850 if (!cpumask_test_cpu(cpu
, tsk_cpus_allowed(this_rq
->curr
)))
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",
1863 highest_prio
= rq
->rt
.highest_prio
.curr
;
1865 #ifdef CONFIG_MT_RT_SCHED_INFO
1866 mt_rt_printf( "1. test_has_highest_prio %d:%d %d",
1867 cpu
, highest_prio
, prio
);
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
);
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
);
1887 static void release_task_ipi(void *data
)
1889 #ifdef CONFIG_MT_RT_SCHED_NOTICE
1890 int target_cpu
= (int)(long) data
;
1892 int cpu
= smp_processor_id();
1893 struct rq
*rq
= cpu_rq(cpu
);
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
);
1900 /* check if current process is LITTLE */
1901 if (!cpumask_test_cpu(cpu
, &hmp_slow_cpu_mask
))
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");
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
);
1915 set_tsk_need_released(rq
->curr
);
1916 set_tsk_need_resched(rq
->curr
);
1919 static DEFINE_PER_CPU(int, mt_need_released
);
1920 static int find_highest_prio_in_LITTLE(struct rq
*this_rq
, int pull
)
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
);
1926 highest_prio
= MAX_RT_PRIO
;
1927 cpumask_clear(lowest_mask
);
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
,
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
));
1939 if (!cpu_online(cpu
))
1943 if(rq
->rt
.rt_nr_running
== 0)
1946 prio
= rq
->rt
.highest_prio
.curr
;
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]);
1953 /* If the highest priority of LITTLE CPU is smaller and equal than current,
1956 if (prio
>= this_rq
->rt
.highest_prio
.curr
)
1959 /* If the prority of LITTLE CPU is smaller and than highest_prio of LITTLE CPUs */
1960 if (prio
> highest_prio
)
1963 /* check the affinity */
1964 if (!cpumask_test_cpu(this_rq
->cpu
, tsk_cpus_allowed(rq
->curr
)))
1967 if (prio
< highest_prio
){
1969 #ifdef CONFIG_MT_RT_SCHED_INFO
1970 mt_rt_printf("3. find_highest_prio_in_LITTLE find");
1975 highest_prio
= prio
;
1976 cpumask_clear(lowest_mask
);
1979 cpumask_set_cpu(cpu
, lowest_mask
);
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]);
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");
1994 raw_spin_unlock_irq(&this_rq
->lock
);
1995 per_cpu(mt_need_released
, this_cpu
) = 1;
1996 for_each_cpu (cpu
, lowest_mask
) {
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
);
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
);
2008 /* the target CPU will execute release_task_ipi */
2009 smp_call_function_single(cpu
, release_task_ipi
, (void *)this_cpu
, 0);
2014 raw_spin_lock_irq(&this_rq
->lock
);
2018 static int find_lowest_rq_in_big(struct task_struct
*task
, struct cpumask
*lowest_mask
)
2020 int i
, lowest_prio
= 0;
2021 struct rq
*rq
= NULL
;
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
);
2030 for_each_cpu(i
, &hmp_fast_cpu_mask
){
2037 prio
= rq
->rt
.highest_prio
.curr
;
2039 #ifdef CONFIG_MT_RT_SCHED_INFO
2040 mt_rt_printf("1. find_lowest_rq_in_big %d:%d %d:%lu",
2042 lowest_prio
, (unsigned long)lowest_mask
->bits
[0]);
2045 /* If the highest priority of CPU is higher than lowest_prio
2046 * or higher than the task, then bypass
2048 if ((prio
< lowest_prio
) || (prio
<= task
->prio
))
2051 if (!cpumask_test_cpu(i
, tsk_cpus_allowed(task
)))
2054 /* If the priority lower than lowest_prio */
2055 if (prio
> lowest_prio
){
2057 cpumask_clear(lowest_mask
);
2060 cpumask_set_cpu(i
, lowest_mask
);
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");
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]);
2078 static int find_lowest_rq_in_LITTLE(struct task_struct
*task
, struct cpumask
*lowest_mask
)
2080 int i
, lowest_prio
= 0;
2081 struct rq
*rq
= NULL
;
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
);
2090 for_each_cpu(i
, &hmp_slow_cpu_mask
){
2097 prio
= rq
->rt
.highest_prio
.curr
;
2099 #ifdef CONFIG_MT_RT_SCHED_INFO
2100 mt_rt_printf("1. find_lowest_rq_in_LITTLE %d:%d %d:%lu",
2102 lowest_prio
, (unsigned long)lowest_mask
->bits
[0]);
2105 /* If the highest priority of CPU is higher than lowest_prio
2106 * or higher than the task, then bypass
2108 if ((prio
< lowest_prio
) || (prio
<= task
->prio
))
2111 if (!cpumask_test_cpu(i
, tsk_cpus_allowed(task
)))
2114 /* If the priority lower than lowest_prio */
2115 if (prio
> lowest_prio
){
2117 cpumask_clear(lowest_mask
);
2120 cpumask_set_cpu(i
, lowest_mask
);
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");
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]);
2141 static int find_lowest_rq(struct task_struct
*task
)
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
);
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
);
2152 /* Make sure the mask is initialized first */
2153 if (unlikely(!lowest_mask
))
2156 if (task
->nr_cpus_allowed
== 1)
2157 return -1; /* No other targets possible */
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 */
2166 if (!cpupri_find(&task_rq(task
)->rd
->cpupri
, task
, lowest_mask
))
2167 return -1; /* No targets found */
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]);
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.
2181 * We prioritize the last cpu that the task executed on since
2182 * it is most likely cache-hot in that location.
2184 if (cpumask_test_cpu(cpu
, lowest_mask
))
2188 * Otherwise, we consult the sched_domains span maps to figure
2189 * out which cpu is logically closest to our hot cache data.
2191 if (!cpumask_test_cpu(this_cpu
, lowest_mask
))
2192 this_cpu
= -1; /* Skip this_cpu opt if not among lowest */
2195 for_each_domain(cpu
, sd
) {
2196 if (sd
->flags
& SD_WAKE_AFFINE
) {
2200 * "this_cpu" is cheaper to preempt than a
2203 if (this_cpu
!= -1 &&
2204 cpumask_test_cpu(this_cpu
, sched_domain_span(sd
))) {
2209 best_cpu
= cpumask_first_and(lowest_mask
,
2210 sched_domain_span(sd
));
2211 if (best_cpu
< nr_cpu_ids
) {
2220 * And finally, if there were no matches within the domains
2221 * just give the caller *something* to work with from the compatible
2227 cpu
= cpumask_any(lowest_mask
);
2228 if (cpu
< nr_cpu_ids
)
2233 /* Will lock the rq it finds */
2234 static struct rq
*find_lock_lowest_rq(struct task_struct
*task
, struct rq
*rq
)
2236 struct rq
*lowest_rq
= NULL
;
2240 for (tries
= 0; tries
< RT_MAX_TRIES
; tries
++) {
2241 cpu
= find_lowest_rq(task
);
2243 if ((cpu
== -1) || (cpu
== rq
->cpu
))
2246 lowest_rq
= cpu_rq(cpu
);
2248 /* if the prio of this runqueue changed, try again */
2249 if (double_lock_balance(rq
, lowest_rq
)) {
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.
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
);
2261 if (unlikely(task_rq(task
) != rq
||
2262 !cpumask_test_cpu(lowest_rq
->cpu
,
2263 tsk_cpus_allowed(task
)) ||
2264 task_running(rq
, task
) ||
2267 double_unlock_balance(rq
, lowest_rq
);
2273 /* If this rq is still suitable use it. */
2274 if (lowest_rq
->rt
.highest_prio
.curr
> task
->prio
)
2278 double_unlock_balance(rq
, lowest_rq
);
2285 static struct task_struct
*pick_next_pushable_task(struct rq
*rq
)
2287 struct task_struct
*p
;
2289 if (!has_pushable_tasks(rq
))
2292 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
2293 struct task_struct
, pushable_tasks
);
2295 BUG_ON(rq
->cpu
!= task_cpu(p
));
2296 BUG_ON(task_current(rq
, p
));
2297 BUG_ON(p
->nr_cpus_allowed
<= 1);
2300 BUG_ON(!rt_task(p
));
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
)
2310 struct rq
*lowest_rq
= NULL
;
2313 cpu
= find_lowest_rq(task
);
2315 if ((cpu
== -1) || (cpu
== rq
->cpu
))
2318 lowest_rq
= cpu_rq(cpu
);
2320 /* if the prio of this runqueue changed, try again */
2321 if (double_lock_balance(rq
, lowest_rq
)) {
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.
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
);
2332 if (unlikely(task_rq(task
) != rq
||
2333 !cpumask_test_cpu(lowest_rq
->cpu
,
2334 tsk_cpus_allowed(task
)) ||
2335 task_running(rq
, task
) ||
2337 double_unlock_balance(rq
, lowest_rq
);
2342 /* If this rq is still suitable use it. */
2343 if (lowest_rq
->rt
.highest_prio
.curr
> task
->prio
){
2347 double_unlock_balance(rq
, lowest_rq
);
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
)
2357 struct rq
*lowest_rq
;
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
);
2368 if (unlikely(p
== rq
->curr
)) {
2373 /* We might release rq lock */
2376 /* find_lock_lowest_rq locks the rq if found */
2377 lowest_rq
= find_lock_lowest_rq_mtk(p
, 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
);
2385 if (likely(p
->prio
< rq
->curr
->prio
)) {
2386 resched_task(rq
->curr
);
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
);
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
);
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
);
2406 deactivate_task(rq
, p
, 0);
2407 set_task_cpu(p
, lowest_rq
->cpu
);
2408 activate_task(lowest_rq
, p
, 0);
2411 resched_task(lowest_rq
->curr
);
2413 double_unlock_balance(rq
, lowest_rq
);
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.
2426 static int push_rt_task(struct rq
*rq
)
2428 struct task_struct
*next_task
;
2429 struct rq
*lowest_rq
;
2432 if (!rq
->rt
.overloaded
)
2435 next_task
= pick_next_pushable_task(rq
);
2440 if (unlikely(next_task
== rq
->curr
)) {
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.
2450 if (unlikely(next_task
->prio
< rq
->curr
->prio
)) {
2451 resched_task(rq
->curr
);
2455 /* We might release rq lock */
2456 get_task_struct(next_task
);
2458 /* find_lock_lowest_rq locks the rq if found */
2459 lowest_rq
= find_lock_lowest_rq(next_task
, rq
);
2461 struct task_struct
*task
;
2463 * find_lock_lowest_rq releases rq->lock
2464 * so it is possible that next_task has migrated.
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
2470 task
= pick_next_pushable_task(rq
);
2471 if (task_cpu(next_task
) == rq
->cpu
&& task
== next_task
) {
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.
2482 /* No more tasks, just exit */
2486 * Something has shifted, try again.
2488 put_task_struct(next_task
);
2493 deactivate_task(rq
, next_task
, 0);
2494 set_task_cpu(next_task
, lowest_rq
->cpu
);
2495 activate_task(lowest_rq
, next_task
, 0);
2498 resched_task(lowest_rq
->curr
);
2500 double_unlock_balance(rq
, lowest_rq
);
2503 put_task_struct(next_task
);
2508 static void push_rt_tasks(struct rq
*rq
)
2510 /* push_rt_task will return true if it moved an RT */
2511 while (push_rt_task(rq
))
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
;
2522 for_each_cpu(cpu
, this_rq
->rd
->rto_mask
) {
2523 if (this_cpu
== cpu
)
2526 src_rq
= cpu_rq(cpu
);
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
2535 if (src_rq
->rt
.highest_prio
.next
>=
2536 this_rq
->rt
.highest_prio
.curr
)
2540 * We can potentially drop this_rq's lock in
2541 * double_lock_balance, and another CPU could
2544 double_lock_balance(this_rq
, src_rq
);
2547 * Are there still pullable RT tasks?
2549 if (src_rq
->rt
.rt_nr_running
<= 1)
2552 p
= pick_next_highest_task_rt(src_rq
, this_cpu
);
2555 * Do we have an RT task that preempts
2556 * the to-be-scheduled task?
2558 if (p
&& (p
->prio
< this_rq
->rt
.highest_prio
.curr
)) {
2559 WARN_ON(p
== src_rq
->curr
);
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
2570 if (p
->prio
< src_rq
->curr
->prio
)
2575 deactivate_task(src_rq
, p
, 0);
2576 set_task_cpu(p
, this_cpu
);
2577 activate_task(this_rq
, p
, 0);
2579 * We continue with the search, just in
2580 * case there's an even higher prio task
2581 * in another runqueue. (low likelihood
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
,
2588 p
->pid
, p
->comm
, p
->prio
);
2592 double_unlock_balance(this_rq
, src_rq
);
2598 void mt_check_rt_policy(struct rq
*this_rq
)
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
) )
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
);
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
);
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
);
2624 int mt_post_schedule(struct rq
*rq
)
2626 int this_cpu
= rq
->cpu
, ret
= 0;
2627 unsigned long flags
;
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);
2634 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
2640 #ifdef CONFIG_MT_RT_SCHED
2641 int pull_rt_task(struct rq
*this_rq
)
2643 static int pull_rt_task(struct rq
*this_rq
)
2646 #if defined(CONFIG_MT_RT_SCHED_INFO) || !defined(CONFIG_MT_RT_SCHED)
2647 int this_cpu
= this_rq
->cpu
;
2650 #ifndef CONFIG_MT_RT_SCHED
2652 struct task_struct
*p
;
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]);
2661 #ifdef CONFIG_MT_RT_SCHED
2662 if (likely(!rt_overloaded(this_rq
)))
2664 ret
= pick_next_highest_task(this_rq
);
2666 if (likely(!rt_overloaded(this_rq
)))
2669 #ifdef CONFIG_MT_RT_SCHED_INFO
2670 mt_rt_printf("1. pull_rt_task %lu ",
2671 this_rq
->rd
->rto_mask
->bits
[0]);
2673 for_each_cpu(cpu
, this_rq
->rd
->rto_mask
) {
2674 if (this_cpu
== cpu
)
2677 src_rq
= cpu_rq(cpu
);
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
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
);
2690 if (src_rq
->rt
.highest_prio
.next
>=
2691 this_rq
->rt
.highest_prio
.curr
)
2695 * We can potentially drop this_rq's lock in
2696 * double_lock_balance, and another CPU could
2699 double_lock_balance(this_rq
, src_rq
);
2702 * Are there still pullable RT tasks?
2704 if (src_rq
->rt
.rt_nr_running
<= 1)
2707 p
= pick_next_highest_task_rt(src_rq
, this_cpu
);
2710 * Do we have an RT task that preempts
2711 * the to-be-scheduled task?
2713 if (p
&& (p
->prio
< this_rq
->rt
.highest_prio
.curr
)) {
2714 WARN_ON(p
== src_rq
->curr
);
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
2725 if (p
->prio
< src_rq
->curr
->prio
)
2730 deactivate_task(src_rq
, p
, 0);
2731 set_task_cpu(p
, this_cpu
);
2732 activate_task(this_rq
, p
, 0);
2734 * We continue with the search, just in
2735 * case there's an even higher prio task
2736 * in another runqueue. (low likelihood
2741 double_unlock_balance(this_rq
, src_rq
);
2748 static void pre_schedule_rt(struct rq
*rq
, struct task_struct
*prev
)
2750 /* Try to pull RT tasks here if we lower this rq's prio */
2751 if (rq
->rt
.highest_prio
.curr
> prev
->prio
)
2755 static void post_schedule_rt(struct rq
*rq
)
2761 * If we are not running and we are not going to reschedule soon, we should
2762 * try to push tasks away now
2764 static void task_woken_rt(struct rq
*rq
, struct task_struct
*p
)
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
))
2776 static void set_cpus_allowed_rt(struct task_struct
*p
,
2777 const struct cpumask
*new_mask
)
2782 BUG_ON(!rt_task(p
));
2787 weight
= cpumask_weight(new_mask
);
2790 * Only update if the process changes its state from whether it
2791 * can migrate or not.
2793 if ((p
->nr_cpus_allowed
> 1) == (weight
> 1))
2799 * The process used to be able to migrate OR it can now migrate
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
--;
2807 if (!task_current(rq
, p
))
2808 enqueue_pushable_task(rq
, p
);
2809 rq
->rt
.rt_nr_migratory
++;
2812 update_rt_migration(&rq
->rt
);
2815 /* Assumes rq->lock is held */
2816 static void rq_online_rt(struct rq
*rq
)
2818 if (rq
->rt
.overloaded
)
2819 rt_set_overload(rq
);
2821 __enable_runtime(rq
);
2823 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rq
->rt
.highest_prio
.curr
);
2826 /* Assumes rq->lock is held */
2827 static void rq_offline_rt(struct rq
*rq
)
2829 if (rq
->rt
.overloaded
)
2830 rt_clear_overload(rq
);
2832 __disable_runtime(rq
);
2834 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, CPUPRI_INVALID
);
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.
2841 static void switched_from_rt(struct rq
*rq
, struct task_struct
*p
)
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
2850 if (!p
->on_rq
|| rq
->rt
.rt_nr_running
)
2853 if (pull_rt_task(rq
))
2854 resched_task(rq
->curr
);
2857 void init_sched_rt_class(void)
2861 for_each_possible_cpu(i
) {
2862 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask
, i
),
2863 GFP_KERNEL
, cpu_to_node(i
));
2866 #endif /* CONFIG_SMP */
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
2873 static void switched_to_rt(struct rq
*rq
, struct task_struct
*p
)
2875 int check_resched
= 1;
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.
2884 if (p
->on_rq
&& rq
->curr
!= p
) {
2886 if (rq
->rt
.overloaded
&& push_rt_task(rq
) &&
2887 /* Don't resched if we changed runqueues */
2890 #endif /* CONFIG_SMP */
2891 if (check_resched
&& p
->prio
< rq
->curr
->prio
)
2892 resched_task(rq
->curr
);
2897 * Priority of the task has changed. This may cause
2898 * us to initiate a push or pull.
2901 prio_changed_rt(struct rq
*rq
, struct task_struct
*p
, int oldprio
)
2906 if (rq
->curr
== p
) {
2909 * If our priority decreases while running, we
2910 * may need to pull tasks to this runqueue.
2912 if (oldprio
< p
->prio
)
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.
2920 if (p
->prio
> rq
->rt
.highest_prio
.curr
&& rq
->curr
== p
)
2923 /* For UP simply resched on drop of prio */
2924 if (oldprio
< p
->prio
)
2926 #endif /* CONFIG_SMP */
2929 * This task is not running, but if it is
2930 * greater than the current running task
2933 if (p
->prio
< rq
->curr
->prio
)
2934 resched_task(rq
->curr
);
2938 static void watchdog(struct rq
*rq
, struct task_struct
*p
)
2940 unsigned long soft
, hard
;
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
);
2946 if (soft
!= RLIM_INFINITY
) {
2949 if (p
->rt
.watchdog_stamp
!= jiffies
) {
2951 p
->rt
.watchdog_stamp
= jiffies
;
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
;
2960 static void task_tick_rt(struct rq
*rq
, struct task_struct
*p
, int queued
)
2962 struct sched_rt_entity
*rt_se
= &p
->rt
;
2969 * RR tasks need a special form of timeslice management.
2970 * FIFO tasks have no timeslices.
2972 if (p
->policy
!= SCHED_RR
)
2975 if (--p
->rt
.time_slice
)
2978 p
->rt
.time_slice
= sched_rr_timeslice
;
2981 * Requeue to the end of queue if we (and all of our ancestors) are the
2982 * only element on the queue
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
);
2993 static void set_curr_task_rt(struct rq
*rq
)
2995 struct task_struct
*p
= rq
->curr
;
2997 p
->se
.exec_start
= rq
->clock_task
;
2999 /* The running task is never eligible for pushing */
3000 dequeue_pushable_task(rq
, p
);
3003 static unsigned int get_rr_interval_rt(struct rq
*rq
, struct task_struct
*task
)
3006 * Time slice is 0 for SCHED_FIFO tasks
3008 if (task
->policy
== SCHED_RR
)
3009 return sched_rr_timeslice
;
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
,
3020 .check_preempt_curr
= check_preempt_curr_rt
,
3022 .pick_next_task
= pick_next_task_rt
,
3023 .put_prev_task
= put_prev_task_rt
,
3026 .select_task_rq
= select_task_rq_rt
,
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
,
3037 .set_curr_task
= set_curr_task_rt
,
3038 .task_tick
= task_tick_rt
,
3040 .get_rr_interval
= get_rr_interval_rt
,
3042 .prio_changed
= prio_changed_rt
,
3043 .switched_to
= switched_to_rt
,
3046 #ifdef CONFIG_SCHED_DEBUG
3047 extern void print_rt_rq(struct seq_file
*m
, int cpu
, struct rt_rq
*rt_rq
);
3049 void print_rt_stats(struct seq_file
*m
, int cpu
)
3052 struct rt_rq
*rt_rq
;
3055 for_each_rt_rq(rt_rq
, iter
, cpu_rq(cpu
))
3056 print_rt_rq(m
, cpu
, rt_rq
);
3059 #endif /* CONFIG_SCHED_DEBUG */