1391e43daSPeter Zijlstra /* 2391e43daSPeter Zijlstra * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR 3391e43daSPeter Zijlstra * policies) 4391e43daSPeter Zijlstra */ 5391e43daSPeter Zijlstra 6391e43daSPeter Zijlstra #include "sched.h" 7391e43daSPeter Zijlstra 8391e43daSPeter Zijlstra #include <linux/slab.h> 9391e43daSPeter Zijlstra 10ce0dbbbbSClark Williams int sched_rr_timeslice = RR_TIMESLICE; 11ce0dbbbbSClark Williams 12391e43daSPeter Zijlstra static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); 13391e43daSPeter Zijlstra 14391e43daSPeter Zijlstra struct rt_bandwidth def_rt_bandwidth; 15391e43daSPeter Zijlstra 16391e43daSPeter Zijlstra static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) 17391e43daSPeter Zijlstra { 18391e43daSPeter Zijlstra struct rt_bandwidth *rt_b = 19391e43daSPeter Zijlstra container_of(timer, struct rt_bandwidth, rt_period_timer); 20391e43daSPeter Zijlstra ktime_t now; 21391e43daSPeter Zijlstra int overrun; 22391e43daSPeter Zijlstra int idle = 0; 23391e43daSPeter Zijlstra 24391e43daSPeter Zijlstra for (;;) { 25391e43daSPeter Zijlstra now = hrtimer_cb_get_time(timer); 26391e43daSPeter Zijlstra overrun = hrtimer_forward(timer, now, rt_b->rt_period); 27391e43daSPeter Zijlstra 28391e43daSPeter Zijlstra if (!overrun) 29391e43daSPeter Zijlstra break; 30391e43daSPeter Zijlstra 31391e43daSPeter Zijlstra idle = do_sched_rt_period_timer(rt_b, overrun); 32391e43daSPeter Zijlstra } 33391e43daSPeter Zijlstra 34391e43daSPeter Zijlstra return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; 35391e43daSPeter Zijlstra } 36391e43daSPeter Zijlstra 37391e43daSPeter Zijlstra void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) 38391e43daSPeter Zijlstra { 39391e43daSPeter Zijlstra rt_b->rt_period = ns_to_ktime(period); 40391e43daSPeter Zijlstra rt_b->rt_runtime = runtime; 41391e43daSPeter Zijlstra 42391e43daSPeter Zijlstra raw_spin_lock_init(&rt_b->rt_runtime_lock); 43391e43daSPeter Zijlstra 44391e43daSPeter Zijlstra hrtimer_init(&rt_b->rt_period_timer, 45391e43daSPeter Zijlstra CLOCK_MONOTONIC, HRTIMER_MODE_REL); 46391e43daSPeter Zijlstra rt_b->rt_period_timer.function = sched_rt_period_timer; 47391e43daSPeter Zijlstra } 48391e43daSPeter Zijlstra 49391e43daSPeter Zijlstra static void start_rt_bandwidth(struct rt_bandwidth *rt_b) 50391e43daSPeter Zijlstra { 51391e43daSPeter Zijlstra if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) 52391e43daSPeter Zijlstra return; 53391e43daSPeter Zijlstra 54391e43daSPeter Zijlstra if (hrtimer_active(&rt_b->rt_period_timer)) 55391e43daSPeter Zijlstra return; 56391e43daSPeter Zijlstra 57391e43daSPeter Zijlstra raw_spin_lock(&rt_b->rt_runtime_lock); 58391e43daSPeter Zijlstra start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period); 59391e43daSPeter Zijlstra raw_spin_unlock(&rt_b->rt_runtime_lock); 60391e43daSPeter Zijlstra } 61391e43daSPeter Zijlstra 62391e43daSPeter Zijlstra void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) 63391e43daSPeter Zijlstra { 64391e43daSPeter Zijlstra struct rt_prio_array *array; 65391e43daSPeter Zijlstra int i; 66391e43daSPeter Zijlstra 67391e43daSPeter Zijlstra array = &rt_rq->active; 68391e43daSPeter Zijlstra for (i = 0; i < MAX_RT_PRIO; i++) { 69391e43daSPeter Zijlstra INIT_LIST_HEAD(array->queue + i); 70391e43daSPeter Zijlstra __clear_bit(i, array->bitmap); 71391e43daSPeter Zijlstra } 72391e43daSPeter Zijlstra /* delimiter for bitsearch: */ 73391e43daSPeter Zijlstra __set_bit(MAX_RT_PRIO, array->bitmap); 74391e43daSPeter Zijlstra 75391e43daSPeter Zijlstra #if defined CONFIG_SMP 76391e43daSPeter Zijlstra rt_rq->highest_prio.curr = MAX_RT_PRIO; 77391e43daSPeter Zijlstra rt_rq->highest_prio.next = MAX_RT_PRIO; 78391e43daSPeter Zijlstra rt_rq->rt_nr_migratory = 0; 79391e43daSPeter Zijlstra rt_rq->overloaded = 0; 80391e43daSPeter Zijlstra plist_head_init(&rt_rq->pushable_tasks); 81391e43daSPeter Zijlstra #endif 82391e43daSPeter Zijlstra 83391e43daSPeter Zijlstra rt_rq->rt_time = 0; 84391e43daSPeter Zijlstra rt_rq->rt_throttled = 0; 85391e43daSPeter Zijlstra rt_rq->rt_runtime = 0; 86391e43daSPeter Zijlstra raw_spin_lock_init(&rt_rq->rt_runtime_lock); 87391e43daSPeter Zijlstra } 88391e43daSPeter Zijlstra 89391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 90391e43daSPeter Zijlstra static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) 91391e43daSPeter Zijlstra { 92391e43daSPeter Zijlstra hrtimer_cancel(&rt_b->rt_period_timer); 93391e43daSPeter Zijlstra } 94391e43daSPeter Zijlstra 95391e43daSPeter Zijlstra #define rt_entity_is_task(rt_se) (!(rt_se)->my_q) 96391e43daSPeter Zijlstra 97391e43daSPeter Zijlstra static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) 98391e43daSPeter Zijlstra { 99391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 100391e43daSPeter Zijlstra WARN_ON_ONCE(!rt_entity_is_task(rt_se)); 101391e43daSPeter Zijlstra #endif 102391e43daSPeter Zijlstra return container_of(rt_se, struct task_struct, rt); 103391e43daSPeter Zijlstra } 104391e43daSPeter Zijlstra 105391e43daSPeter Zijlstra static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) 106391e43daSPeter Zijlstra { 107391e43daSPeter Zijlstra return rt_rq->rq; 108391e43daSPeter Zijlstra } 109391e43daSPeter Zijlstra 110391e43daSPeter Zijlstra static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) 111391e43daSPeter Zijlstra { 112391e43daSPeter Zijlstra return rt_se->rt_rq; 113391e43daSPeter Zijlstra } 114391e43daSPeter Zijlstra 115391e43daSPeter Zijlstra void free_rt_sched_group(struct task_group *tg) 116391e43daSPeter Zijlstra { 117391e43daSPeter Zijlstra int i; 118391e43daSPeter Zijlstra 119391e43daSPeter Zijlstra if (tg->rt_se) 120391e43daSPeter Zijlstra destroy_rt_bandwidth(&tg->rt_bandwidth); 121391e43daSPeter Zijlstra 122391e43daSPeter Zijlstra for_each_possible_cpu(i) { 123391e43daSPeter Zijlstra if (tg->rt_rq) 124391e43daSPeter Zijlstra kfree(tg->rt_rq[i]); 125391e43daSPeter Zijlstra if (tg->rt_se) 126391e43daSPeter Zijlstra kfree(tg->rt_se[i]); 127391e43daSPeter Zijlstra } 128391e43daSPeter Zijlstra 129391e43daSPeter Zijlstra kfree(tg->rt_rq); 130391e43daSPeter Zijlstra kfree(tg->rt_se); 131391e43daSPeter Zijlstra } 132391e43daSPeter Zijlstra 133391e43daSPeter Zijlstra void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, 134391e43daSPeter Zijlstra struct sched_rt_entity *rt_se, int cpu, 135391e43daSPeter Zijlstra struct sched_rt_entity *parent) 136391e43daSPeter Zijlstra { 137391e43daSPeter Zijlstra struct rq *rq = cpu_rq(cpu); 138391e43daSPeter Zijlstra 139391e43daSPeter Zijlstra rt_rq->highest_prio.curr = MAX_RT_PRIO; 140391e43daSPeter Zijlstra rt_rq->rt_nr_boosted = 0; 141391e43daSPeter Zijlstra rt_rq->rq = rq; 142391e43daSPeter Zijlstra rt_rq->tg = tg; 143391e43daSPeter Zijlstra 144391e43daSPeter Zijlstra tg->rt_rq[cpu] = rt_rq; 145391e43daSPeter Zijlstra tg->rt_se[cpu] = rt_se; 146391e43daSPeter Zijlstra 147391e43daSPeter Zijlstra if (!rt_se) 148391e43daSPeter Zijlstra return; 149391e43daSPeter Zijlstra 150391e43daSPeter Zijlstra if (!parent) 151391e43daSPeter Zijlstra rt_se->rt_rq = &rq->rt; 152391e43daSPeter Zijlstra else 153391e43daSPeter Zijlstra rt_se->rt_rq = parent->my_q; 154391e43daSPeter Zijlstra 155391e43daSPeter Zijlstra rt_se->my_q = rt_rq; 156391e43daSPeter Zijlstra rt_se->parent = parent; 157391e43daSPeter Zijlstra INIT_LIST_HEAD(&rt_se->run_list); 158391e43daSPeter Zijlstra } 159391e43daSPeter Zijlstra 160391e43daSPeter Zijlstra int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) 161391e43daSPeter Zijlstra { 162391e43daSPeter Zijlstra struct rt_rq *rt_rq; 163391e43daSPeter Zijlstra struct sched_rt_entity *rt_se; 164391e43daSPeter Zijlstra int i; 165391e43daSPeter Zijlstra 166391e43daSPeter Zijlstra tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); 167391e43daSPeter Zijlstra if (!tg->rt_rq) 168391e43daSPeter Zijlstra goto err; 169391e43daSPeter Zijlstra tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); 170391e43daSPeter Zijlstra if (!tg->rt_se) 171391e43daSPeter Zijlstra goto err; 172391e43daSPeter Zijlstra 173391e43daSPeter Zijlstra init_rt_bandwidth(&tg->rt_bandwidth, 174391e43daSPeter Zijlstra ktime_to_ns(def_rt_bandwidth.rt_period), 0); 175391e43daSPeter Zijlstra 176391e43daSPeter Zijlstra for_each_possible_cpu(i) { 177391e43daSPeter Zijlstra rt_rq = kzalloc_node(sizeof(struct rt_rq), 178391e43daSPeter Zijlstra GFP_KERNEL, cpu_to_node(i)); 179391e43daSPeter Zijlstra if (!rt_rq) 180391e43daSPeter Zijlstra goto err; 181391e43daSPeter Zijlstra 182391e43daSPeter Zijlstra rt_se = kzalloc_node(sizeof(struct sched_rt_entity), 183391e43daSPeter Zijlstra GFP_KERNEL, cpu_to_node(i)); 184391e43daSPeter Zijlstra if (!rt_se) 185391e43daSPeter Zijlstra goto err_free_rq; 186391e43daSPeter Zijlstra 187391e43daSPeter Zijlstra init_rt_rq(rt_rq, cpu_rq(i)); 188391e43daSPeter Zijlstra rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; 189391e43daSPeter Zijlstra init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]); 190391e43daSPeter Zijlstra } 191391e43daSPeter Zijlstra 192391e43daSPeter Zijlstra return 1; 193391e43daSPeter Zijlstra 194391e43daSPeter Zijlstra err_free_rq: 195391e43daSPeter Zijlstra kfree(rt_rq); 196391e43daSPeter Zijlstra err: 197391e43daSPeter Zijlstra return 0; 198391e43daSPeter Zijlstra } 199391e43daSPeter Zijlstra 200391e43daSPeter Zijlstra #else /* CONFIG_RT_GROUP_SCHED */ 201391e43daSPeter Zijlstra 202391e43daSPeter Zijlstra #define rt_entity_is_task(rt_se) (1) 203391e43daSPeter Zijlstra 204391e43daSPeter Zijlstra static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) 205391e43daSPeter Zijlstra { 206391e43daSPeter Zijlstra return container_of(rt_se, struct task_struct, rt); 207391e43daSPeter Zijlstra } 208391e43daSPeter Zijlstra 209391e43daSPeter Zijlstra static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) 210391e43daSPeter Zijlstra { 211391e43daSPeter Zijlstra return container_of(rt_rq, struct rq, rt); 212391e43daSPeter Zijlstra } 213391e43daSPeter Zijlstra 214391e43daSPeter Zijlstra static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) 215391e43daSPeter Zijlstra { 216391e43daSPeter Zijlstra struct task_struct *p = rt_task_of(rt_se); 217391e43daSPeter Zijlstra struct rq *rq = task_rq(p); 218391e43daSPeter Zijlstra 219391e43daSPeter Zijlstra return &rq->rt; 220391e43daSPeter Zijlstra } 221391e43daSPeter Zijlstra 222391e43daSPeter Zijlstra void free_rt_sched_group(struct task_group *tg) { } 223391e43daSPeter Zijlstra 224391e43daSPeter Zijlstra int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) 225391e43daSPeter Zijlstra { 226391e43daSPeter Zijlstra return 1; 227391e43daSPeter Zijlstra } 228391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */ 229391e43daSPeter Zijlstra 230391e43daSPeter Zijlstra #ifdef CONFIG_SMP 231391e43daSPeter Zijlstra 232391e43daSPeter Zijlstra static inline int rt_overloaded(struct rq *rq) 233391e43daSPeter Zijlstra { 234391e43daSPeter Zijlstra return atomic_read(&rq->rd->rto_count); 235391e43daSPeter Zijlstra } 236391e43daSPeter Zijlstra 237391e43daSPeter Zijlstra static inline void rt_set_overload(struct rq *rq) 238391e43daSPeter Zijlstra { 239391e43daSPeter Zijlstra if (!rq->online) 240391e43daSPeter Zijlstra return; 241391e43daSPeter Zijlstra 242391e43daSPeter Zijlstra cpumask_set_cpu(rq->cpu, rq->rd->rto_mask); 243391e43daSPeter Zijlstra /* 244391e43daSPeter Zijlstra * Make sure the mask is visible before we set 245391e43daSPeter Zijlstra * the overload count. That is checked to determine 246391e43daSPeter Zijlstra * if we should look at the mask. It would be a shame 247391e43daSPeter Zijlstra * if we looked at the mask, but the mask was not 248391e43daSPeter Zijlstra * updated yet. 2497c3f2ab7SPeter Zijlstra * 2507c3f2ab7SPeter Zijlstra * Matched by the barrier in pull_rt_task(). 251391e43daSPeter Zijlstra */ 2527c3f2ab7SPeter Zijlstra smp_wmb(); 253391e43daSPeter Zijlstra atomic_inc(&rq->rd->rto_count); 254391e43daSPeter Zijlstra } 255391e43daSPeter Zijlstra 256391e43daSPeter Zijlstra static inline void rt_clear_overload(struct rq *rq) 257391e43daSPeter Zijlstra { 258391e43daSPeter Zijlstra if (!rq->online) 259391e43daSPeter Zijlstra return; 260391e43daSPeter Zijlstra 261391e43daSPeter Zijlstra /* the order here really doesn't matter */ 262391e43daSPeter Zijlstra atomic_dec(&rq->rd->rto_count); 263391e43daSPeter Zijlstra cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask); 264391e43daSPeter Zijlstra } 265391e43daSPeter Zijlstra 266391e43daSPeter Zijlstra static void update_rt_migration(struct rt_rq *rt_rq) 267391e43daSPeter Zijlstra { 268391e43daSPeter Zijlstra if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) { 269391e43daSPeter Zijlstra if (!rt_rq->overloaded) { 270391e43daSPeter Zijlstra rt_set_overload(rq_of_rt_rq(rt_rq)); 271391e43daSPeter Zijlstra rt_rq->overloaded = 1; 272391e43daSPeter Zijlstra } 273391e43daSPeter Zijlstra } else if (rt_rq->overloaded) { 274391e43daSPeter Zijlstra rt_clear_overload(rq_of_rt_rq(rt_rq)); 275391e43daSPeter Zijlstra rt_rq->overloaded = 0; 276391e43daSPeter Zijlstra } 277391e43daSPeter Zijlstra } 278391e43daSPeter Zijlstra 279391e43daSPeter Zijlstra static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 280391e43daSPeter Zijlstra { 28129baa747SPeter Zijlstra struct task_struct *p; 28229baa747SPeter Zijlstra 283391e43daSPeter Zijlstra if (!rt_entity_is_task(rt_se)) 284391e43daSPeter Zijlstra return; 285391e43daSPeter Zijlstra 28629baa747SPeter Zijlstra p = rt_task_of(rt_se); 287391e43daSPeter Zijlstra rt_rq = &rq_of_rt_rq(rt_rq)->rt; 288391e43daSPeter Zijlstra 289391e43daSPeter Zijlstra rt_rq->rt_nr_total++; 29029baa747SPeter Zijlstra if (p->nr_cpus_allowed > 1) 291391e43daSPeter Zijlstra rt_rq->rt_nr_migratory++; 292391e43daSPeter Zijlstra 293391e43daSPeter Zijlstra update_rt_migration(rt_rq); 294391e43daSPeter Zijlstra } 295391e43daSPeter Zijlstra 296391e43daSPeter Zijlstra static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 297391e43daSPeter Zijlstra { 29829baa747SPeter Zijlstra struct task_struct *p; 29929baa747SPeter Zijlstra 300391e43daSPeter Zijlstra if (!rt_entity_is_task(rt_se)) 301391e43daSPeter Zijlstra return; 302391e43daSPeter Zijlstra 30329baa747SPeter Zijlstra p = rt_task_of(rt_se); 304391e43daSPeter Zijlstra rt_rq = &rq_of_rt_rq(rt_rq)->rt; 305391e43daSPeter Zijlstra 306391e43daSPeter Zijlstra rt_rq->rt_nr_total--; 30729baa747SPeter Zijlstra if (p->nr_cpus_allowed > 1) 308391e43daSPeter Zijlstra rt_rq->rt_nr_migratory--; 309391e43daSPeter Zijlstra 310391e43daSPeter Zijlstra update_rt_migration(rt_rq); 311391e43daSPeter Zijlstra } 312391e43daSPeter Zijlstra 313391e43daSPeter Zijlstra static inline int has_pushable_tasks(struct rq *rq) 314391e43daSPeter Zijlstra { 315391e43daSPeter Zijlstra return !plist_head_empty(&rq->rt.pushable_tasks); 316391e43daSPeter Zijlstra } 317391e43daSPeter Zijlstra 318391e43daSPeter Zijlstra static void enqueue_pushable_task(struct rq *rq, struct task_struct *p) 319391e43daSPeter Zijlstra { 320391e43daSPeter Zijlstra plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); 321391e43daSPeter Zijlstra plist_node_init(&p->pushable_tasks, p->prio); 322391e43daSPeter Zijlstra plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks); 323391e43daSPeter Zijlstra 324391e43daSPeter Zijlstra /* Update the highest prio pushable task */ 325391e43daSPeter Zijlstra if (p->prio < rq->rt.highest_prio.next) 326391e43daSPeter Zijlstra rq->rt.highest_prio.next = p->prio; 327391e43daSPeter Zijlstra } 328391e43daSPeter Zijlstra 329391e43daSPeter Zijlstra static void dequeue_pushable_task(struct rq *rq, struct task_struct *p) 330391e43daSPeter Zijlstra { 331391e43daSPeter Zijlstra plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); 332391e43daSPeter Zijlstra 333391e43daSPeter Zijlstra /* Update the new highest prio pushable task */ 334391e43daSPeter Zijlstra if (has_pushable_tasks(rq)) { 335391e43daSPeter Zijlstra p = plist_first_entry(&rq->rt.pushable_tasks, 336391e43daSPeter Zijlstra struct task_struct, pushable_tasks); 337391e43daSPeter Zijlstra rq->rt.highest_prio.next = p->prio; 338391e43daSPeter Zijlstra } else 339391e43daSPeter Zijlstra rq->rt.highest_prio.next = MAX_RT_PRIO; 340391e43daSPeter Zijlstra } 341391e43daSPeter Zijlstra 342391e43daSPeter Zijlstra #else 343391e43daSPeter Zijlstra 344391e43daSPeter Zijlstra static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p) 345391e43daSPeter Zijlstra { 346391e43daSPeter Zijlstra } 347391e43daSPeter Zijlstra 348391e43daSPeter Zijlstra static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p) 349391e43daSPeter Zijlstra { 350391e43daSPeter Zijlstra } 351391e43daSPeter Zijlstra 352391e43daSPeter Zijlstra static inline 353391e43daSPeter Zijlstra void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 354391e43daSPeter Zijlstra { 355391e43daSPeter Zijlstra } 356391e43daSPeter Zijlstra 357391e43daSPeter Zijlstra static inline 358391e43daSPeter Zijlstra void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 359391e43daSPeter Zijlstra { 360391e43daSPeter Zijlstra } 361391e43daSPeter Zijlstra 362391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 363391e43daSPeter Zijlstra 364391e43daSPeter Zijlstra static inline int on_rt_rq(struct sched_rt_entity *rt_se) 365391e43daSPeter Zijlstra { 366391e43daSPeter Zijlstra return !list_empty(&rt_se->run_list); 367391e43daSPeter Zijlstra } 368391e43daSPeter Zijlstra 369391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 370391e43daSPeter Zijlstra 371391e43daSPeter Zijlstra static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) 372391e43daSPeter Zijlstra { 373391e43daSPeter Zijlstra if (!rt_rq->tg) 374391e43daSPeter Zijlstra return RUNTIME_INF; 375391e43daSPeter Zijlstra 376391e43daSPeter Zijlstra return rt_rq->rt_runtime; 377391e43daSPeter Zijlstra } 378391e43daSPeter Zijlstra 379391e43daSPeter Zijlstra static inline u64 sched_rt_period(struct rt_rq *rt_rq) 380391e43daSPeter Zijlstra { 381391e43daSPeter Zijlstra return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period); 382391e43daSPeter Zijlstra } 383391e43daSPeter Zijlstra 384391e43daSPeter Zijlstra typedef struct task_group *rt_rq_iter_t; 385391e43daSPeter Zijlstra 386391e43daSPeter Zijlstra static inline struct task_group *next_task_group(struct task_group *tg) 387391e43daSPeter Zijlstra { 388391e43daSPeter Zijlstra do { 389391e43daSPeter Zijlstra tg = list_entry_rcu(tg->list.next, 390391e43daSPeter Zijlstra typeof(struct task_group), list); 391391e43daSPeter Zijlstra } while (&tg->list != &task_groups && task_group_is_autogroup(tg)); 392391e43daSPeter Zijlstra 393391e43daSPeter Zijlstra if (&tg->list == &task_groups) 394391e43daSPeter Zijlstra tg = NULL; 395391e43daSPeter Zijlstra 396391e43daSPeter Zijlstra return tg; 397391e43daSPeter Zijlstra } 398391e43daSPeter Zijlstra 399391e43daSPeter Zijlstra #define for_each_rt_rq(rt_rq, iter, rq) \ 400391e43daSPeter Zijlstra for (iter = container_of(&task_groups, typeof(*iter), list); \ 401391e43daSPeter Zijlstra (iter = next_task_group(iter)) && \ 402391e43daSPeter Zijlstra (rt_rq = iter->rt_rq[cpu_of(rq)]);) 403391e43daSPeter Zijlstra 404391e43daSPeter Zijlstra #define for_each_sched_rt_entity(rt_se) \ 405391e43daSPeter Zijlstra for (; rt_se; rt_se = rt_se->parent) 406391e43daSPeter Zijlstra 407391e43daSPeter Zijlstra static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) 408391e43daSPeter Zijlstra { 409391e43daSPeter Zijlstra return rt_se->my_q; 410391e43daSPeter Zijlstra } 411391e43daSPeter Zijlstra 412391e43daSPeter Zijlstra static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head); 413391e43daSPeter Zijlstra static void dequeue_rt_entity(struct sched_rt_entity *rt_se); 414391e43daSPeter Zijlstra 415391e43daSPeter Zijlstra static void sched_rt_rq_enqueue(struct rt_rq *rt_rq) 416391e43daSPeter Zijlstra { 417391e43daSPeter Zijlstra struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr; 418391e43daSPeter Zijlstra struct sched_rt_entity *rt_se; 419391e43daSPeter Zijlstra 420391e43daSPeter Zijlstra int cpu = cpu_of(rq_of_rt_rq(rt_rq)); 421391e43daSPeter Zijlstra 422391e43daSPeter Zijlstra rt_se = rt_rq->tg->rt_se[cpu]; 423391e43daSPeter Zijlstra 424391e43daSPeter Zijlstra if (rt_rq->rt_nr_running) { 425391e43daSPeter Zijlstra if (rt_se && !on_rt_rq(rt_se)) 426391e43daSPeter Zijlstra enqueue_rt_entity(rt_se, false); 427391e43daSPeter Zijlstra if (rt_rq->highest_prio.curr < curr->prio) 428391e43daSPeter Zijlstra resched_task(curr); 429391e43daSPeter Zijlstra } 430391e43daSPeter Zijlstra } 431391e43daSPeter Zijlstra 432391e43daSPeter Zijlstra static void sched_rt_rq_dequeue(struct rt_rq *rt_rq) 433391e43daSPeter Zijlstra { 434391e43daSPeter Zijlstra struct sched_rt_entity *rt_se; 435391e43daSPeter Zijlstra int cpu = cpu_of(rq_of_rt_rq(rt_rq)); 436391e43daSPeter Zijlstra 437391e43daSPeter Zijlstra rt_se = rt_rq->tg->rt_se[cpu]; 438391e43daSPeter Zijlstra 439391e43daSPeter Zijlstra if (rt_se && on_rt_rq(rt_se)) 440391e43daSPeter Zijlstra dequeue_rt_entity(rt_se); 441391e43daSPeter Zijlstra } 442391e43daSPeter Zijlstra 443391e43daSPeter Zijlstra static inline int rt_rq_throttled(struct rt_rq *rt_rq) 444391e43daSPeter Zijlstra { 445391e43daSPeter Zijlstra return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted; 446391e43daSPeter Zijlstra } 447391e43daSPeter Zijlstra 448391e43daSPeter Zijlstra static int rt_se_boosted(struct sched_rt_entity *rt_se) 449391e43daSPeter Zijlstra { 450391e43daSPeter Zijlstra struct rt_rq *rt_rq = group_rt_rq(rt_se); 451391e43daSPeter Zijlstra struct task_struct *p; 452391e43daSPeter Zijlstra 453391e43daSPeter Zijlstra if (rt_rq) 454391e43daSPeter Zijlstra return !!rt_rq->rt_nr_boosted; 455391e43daSPeter Zijlstra 456391e43daSPeter Zijlstra p = rt_task_of(rt_se); 457391e43daSPeter Zijlstra return p->prio != p->normal_prio; 458391e43daSPeter Zijlstra } 459391e43daSPeter Zijlstra 460391e43daSPeter Zijlstra #ifdef CONFIG_SMP 461391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void) 462391e43daSPeter Zijlstra { 463424c93feSNathan Zimmer return this_rq()->rd->span; 464391e43daSPeter Zijlstra } 465391e43daSPeter Zijlstra #else 466391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void) 467391e43daSPeter Zijlstra { 468391e43daSPeter Zijlstra return cpu_online_mask; 469391e43daSPeter Zijlstra } 470391e43daSPeter Zijlstra #endif 471391e43daSPeter Zijlstra 472391e43daSPeter Zijlstra static inline 473391e43daSPeter Zijlstra struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) 474391e43daSPeter Zijlstra { 475391e43daSPeter Zijlstra return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu]; 476391e43daSPeter Zijlstra } 477391e43daSPeter Zijlstra 478391e43daSPeter Zijlstra static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) 479391e43daSPeter Zijlstra { 480391e43daSPeter Zijlstra return &rt_rq->tg->rt_bandwidth; 481391e43daSPeter Zijlstra } 482391e43daSPeter Zijlstra 483391e43daSPeter Zijlstra #else /* !CONFIG_RT_GROUP_SCHED */ 484391e43daSPeter Zijlstra 485391e43daSPeter Zijlstra static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) 486391e43daSPeter Zijlstra { 487391e43daSPeter Zijlstra return rt_rq->rt_runtime; 488391e43daSPeter Zijlstra } 489391e43daSPeter Zijlstra 490391e43daSPeter Zijlstra static inline u64 sched_rt_period(struct rt_rq *rt_rq) 491391e43daSPeter Zijlstra { 492391e43daSPeter Zijlstra return ktime_to_ns(def_rt_bandwidth.rt_period); 493391e43daSPeter Zijlstra } 494391e43daSPeter Zijlstra 495391e43daSPeter Zijlstra typedef struct rt_rq *rt_rq_iter_t; 496391e43daSPeter Zijlstra 497391e43daSPeter Zijlstra #define for_each_rt_rq(rt_rq, iter, rq) \ 498391e43daSPeter Zijlstra for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL) 499391e43daSPeter Zijlstra 500391e43daSPeter Zijlstra #define for_each_sched_rt_entity(rt_se) \ 501391e43daSPeter Zijlstra for (; rt_se; rt_se = NULL) 502391e43daSPeter Zijlstra 503391e43daSPeter Zijlstra static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) 504391e43daSPeter Zijlstra { 505391e43daSPeter Zijlstra return NULL; 506391e43daSPeter Zijlstra } 507391e43daSPeter Zijlstra 508391e43daSPeter Zijlstra static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq) 509391e43daSPeter Zijlstra { 510391e43daSPeter Zijlstra if (rt_rq->rt_nr_running) 511391e43daSPeter Zijlstra resched_task(rq_of_rt_rq(rt_rq)->curr); 512391e43daSPeter Zijlstra } 513391e43daSPeter Zijlstra 514391e43daSPeter Zijlstra static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq) 515391e43daSPeter Zijlstra { 516391e43daSPeter Zijlstra } 517391e43daSPeter Zijlstra 518391e43daSPeter Zijlstra static inline int rt_rq_throttled(struct rt_rq *rt_rq) 519391e43daSPeter Zijlstra { 520391e43daSPeter Zijlstra return rt_rq->rt_throttled; 521391e43daSPeter Zijlstra } 522391e43daSPeter Zijlstra 523391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void) 524391e43daSPeter Zijlstra { 525391e43daSPeter Zijlstra return cpu_online_mask; 526391e43daSPeter Zijlstra } 527391e43daSPeter Zijlstra 528391e43daSPeter Zijlstra static inline 529391e43daSPeter Zijlstra struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) 530391e43daSPeter Zijlstra { 531391e43daSPeter Zijlstra return &cpu_rq(cpu)->rt; 532391e43daSPeter Zijlstra } 533391e43daSPeter Zijlstra 534391e43daSPeter Zijlstra static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) 535391e43daSPeter Zijlstra { 536391e43daSPeter Zijlstra return &def_rt_bandwidth; 537391e43daSPeter Zijlstra } 538391e43daSPeter Zijlstra 539391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */ 540391e43daSPeter Zijlstra 541391e43daSPeter Zijlstra #ifdef CONFIG_SMP 542391e43daSPeter Zijlstra /* 543391e43daSPeter Zijlstra * We ran out of runtime, see if we can borrow some from our neighbours. 544391e43daSPeter Zijlstra */ 545391e43daSPeter Zijlstra static int do_balance_runtime(struct rt_rq *rt_rq) 546391e43daSPeter Zijlstra { 547391e43daSPeter Zijlstra struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); 548aa7f6730SShawn Bohrer struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd; 549391e43daSPeter Zijlstra int i, weight, more = 0; 550391e43daSPeter Zijlstra u64 rt_period; 551391e43daSPeter Zijlstra 552391e43daSPeter Zijlstra weight = cpumask_weight(rd->span); 553391e43daSPeter Zijlstra 554391e43daSPeter Zijlstra raw_spin_lock(&rt_b->rt_runtime_lock); 555391e43daSPeter Zijlstra rt_period = ktime_to_ns(rt_b->rt_period); 556391e43daSPeter Zijlstra for_each_cpu(i, rd->span) { 557391e43daSPeter Zijlstra struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); 558391e43daSPeter Zijlstra s64 diff; 559391e43daSPeter Zijlstra 560391e43daSPeter Zijlstra if (iter == rt_rq) 561391e43daSPeter Zijlstra continue; 562391e43daSPeter Zijlstra 563391e43daSPeter Zijlstra raw_spin_lock(&iter->rt_runtime_lock); 564391e43daSPeter Zijlstra /* 565391e43daSPeter Zijlstra * Either all rqs have inf runtime and there's nothing to steal 566391e43daSPeter Zijlstra * or __disable_runtime() below sets a specific rq to inf to 567391e43daSPeter Zijlstra * indicate its been disabled and disalow stealing. 568391e43daSPeter Zijlstra */ 569391e43daSPeter Zijlstra if (iter->rt_runtime == RUNTIME_INF) 570391e43daSPeter Zijlstra goto next; 571391e43daSPeter Zijlstra 572391e43daSPeter Zijlstra /* 573391e43daSPeter Zijlstra * From runqueues with spare time, take 1/n part of their 574391e43daSPeter Zijlstra * spare time, but no more than our period. 575391e43daSPeter Zijlstra */ 576391e43daSPeter Zijlstra diff = iter->rt_runtime - iter->rt_time; 577391e43daSPeter Zijlstra if (diff > 0) { 578391e43daSPeter Zijlstra diff = div_u64((u64)diff, weight); 579391e43daSPeter Zijlstra if (rt_rq->rt_runtime + diff > rt_period) 580391e43daSPeter Zijlstra diff = rt_period - rt_rq->rt_runtime; 581391e43daSPeter Zijlstra iter->rt_runtime -= diff; 582391e43daSPeter Zijlstra rt_rq->rt_runtime += diff; 583391e43daSPeter Zijlstra more = 1; 584391e43daSPeter Zijlstra if (rt_rq->rt_runtime == rt_period) { 585391e43daSPeter Zijlstra raw_spin_unlock(&iter->rt_runtime_lock); 586391e43daSPeter Zijlstra break; 587391e43daSPeter Zijlstra } 588391e43daSPeter Zijlstra } 589391e43daSPeter Zijlstra next: 590391e43daSPeter Zijlstra raw_spin_unlock(&iter->rt_runtime_lock); 591391e43daSPeter Zijlstra } 592391e43daSPeter Zijlstra raw_spin_unlock(&rt_b->rt_runtime_lock); 593391e43daSPeter Zijlstra 594391e43daSPeter Zijlstra return more; 595391e43daSPeter Zijlstra } 596391e43daSPeter Zijlstra 597391e43daSPeter Zijlstra /* 598391e43daSPeter Zijlstra * Ensure this RQ takes back all the runtime it lend to its neighbours. 599391e43daSPeter Zijlstra */ 600391e43daSPeter Zijlstra static void __disable_runtime(struct rq *rq) 601391e43daSPeter Zijlstra { 602391e43daSPeter Zijlstra struct root_domain *rd = rq->rd; 603391e43daSPeter Zijlstra rt_rq_iter_t iter; 604391e43daSPeter Zijlstra struct rt_rq *rt_rq; 605391e43daSPeter Zijlstra 606391e43daSPeter Zijlstra if (unlikely(!scheduler_running)) 607391e43daSPeter Zijlstra return; 608391e43daSPeter Zijlstra 609391e43daSPeter Zijlstra for_each_rt_rq(rt_rq, iter, rq) { 610391e43daSPeter Zijlstra struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); 611391e43daSPeter Zijlstra s64 want; 612391e43daSPeter Zijlstra int i; 613391e43daSPeter Zijlstra 614391e43daSPeter Zijlstra raw_spin_lock(&rt_b->rt_runtime_lock); 615391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 616391e43daSPeter Zijlstra /* 617391e43daSPeter Zijlstra * Either we're all inf and nobody needs to borrow, or we're 618391e43daSPeter Zijlstra * already disabled and thus have nothing to do, or we have 619391e43daSPeter Zijlstra * exactly the right amount of runtime to take out. 620391e43daSPeter Zijlstra */ 621391e43daSPeter Zijlstra if (rt_rq->rt_runtime == RUNTIME_INF || 622391e43daSPeter Zijlstra rt_rq->rt_runtime == rt_b->rt_runtime) 623391e43daSPeter Zijlstra goto balanced; 624391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 625391e43daSPeter Zijlstra 626391e43daSPeter Zijlstra /* 627391e43daSPeter Zijlstra * Calculate the difference between what we started out with 628391e43daSPeter Zijlstra * and what we current have, that's the amount of runtime 629391e43daSPeter Zijlstra * we lend and now have to reclaim. 630391e43daSPeter Zijlstra */ 631391e43daSPeter Zijlstra want = rt_b->rt_runtime - rt_rq->rt_runtime; 632391e43daSPeter Zijlstra 633391e43daSPeter Zijlstra /* 634391e43daSPeter Zijlstra * Greedy reclaim, take back as much as we can. 635391e43daSPeter Zijlstra */ 636391e43daSPeter Zijlstra for_each_cpu(i, rd->span) { 637391e43daSPeter Zijlstra struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); 638391e43daSPeter Zijlstra s64 diff; 639391e43daSPeter Zijlstra 640391e43daSPeter Zijlstra /* 641391e43daSPeter Zijlstra * Can't reclaim from ourselves or disabled runqueues. 642391e43daSPeter Zijlstra */ 643391e43daSPeter Zijlstra if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF) 644391e43daSPeter Zijlstra continue; 645391e43daSPeter Zijlstra 646391e43daSPeter Zijlstra raw_spin_lock(&iter->rt_runtime_lock); 647391e43daSPeter Zijlstra if (want > 0) { 648391e43daSPeter Zijlstra diff = min_t(s64, iter->rt_runtime, want); 649391e43daSPeter Zijlstra iter->rt_runtime -= diff; 650391e43daSPeter Zijlstra want -= diff; 651391e43daSPeter Zijlstra } else { 652391e43daSPeter Zijlstra iter->rt_runtime -= want; 653391e43daSPeter Zijlstra want -= want; 654391e43daSPeter Zijlstra } 655391e43daSPeter Zijlstra raw_spin_unlock(&iter->rt_runtime_lock); 656391e43daSPeter Zijlstra 657391e43daSPeter Zijlstra if (!want) 658391e43daSPeter Zijlstra break; 659391e43daSPeter Zijlstra } 660391e43daSPeter Zijlstra 661391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 662391e43daSPeter Zijlstra /* 663391e43daSPeter Zijlstra * We cannot be left wanting - that would mean some runtime 664391e43daSPeter Zijlstra * leaked out of the system. 665391e43daSPeter Zijlstra */ 666391e43daSPeter Zijlstra BUG_ON(want); 667391e43daSPeter Zijlstra balanced: 668391e43daSPeter Zijlstra /* 669391e43daSPeter Zijlstra * Disable all the borrow logic by pretending we have inf 670391e43daSPeter Zijlstra * runtime - in which case borrowing doesn't make sense. 671391e43daSPeter Zijlstra */ 672391e43daSPeter Zijlstra rt_rq->rt_runtime = RUNTIME_INF; 673a4c96ae3SPeter Boonstoppel rt_rq->rt_throttled = 0; 674391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 675391e43daSPeter Zijlstra raw_spin_unlock(&rt_b->rt_runtime_lock); 676391e43daSPeter Zijlstra } 677391e43daSPeter Zijlstra } 678391e43daSPeter Zijlstra 679391e43daSPeter Zijlstra static void __enable_runtime(struct rq *rq) 680391e43daSPeter Zijlstra { 681391e43daSPeter Zijlstra rt_rq_iter_t iter; 682391e43daSPeter Zijlstra struct rt_rq *rt_rq; 683391e43daSPeter Zijlstra 684391e43daSPeter Zijlstra if (unlikely(!scheduler_running)) 685391e43daSPeter Zijlstra return; 686391e43daSPeter Zijlstra 687391e43daSPeter Zijlstra /* 688391e43daSPeter Zijlstra * Reset each runqueue's bandwidth settings 689391e43daSPeter Zijlstra */ 690391e43daSPeter Zijlstra for_each_rt_rq(rt_rq, iter, rq) { 691391e43daSPeter Zijlstra struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); 692391e43daSPeter Zijlstra 693391e43daSPeter Zijlstra raw_spin_lock(&rt_b->rt_runtime_lock); 694391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 695391e43daSPeter Zijlstra rt_rq->rt_runtime = rt_b->rt_runtime; 696391e43daSPeter Zijlstra rt_rq->rt_time = 0; 697391e43daSPeter Zijlstra rt_rq->rt_throttled = 0; 698391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 699391e43daSPeter Zijlstra raw_spin_unlock(&rt_b->rt_runtime_lock); 700391e43daSPeter Zijlstra } 701391e43daSPeter Zijlstra } 702391e43daSPeter Zijlstra 703391e43daSPeter Zijlstra static int balance_runtime(struct rt_rq *rt_rq) 704391e43daSPeter Zijlstra { 705391e43daSPeter Zijlstra int more = 0; 706391e43daSPeter Zijlstra 707391e43daSPeter Zijlstra if (!sched_feat(RT_RUNTIME_SHARE)) 708391e43daSPeter Zijlstra return more; 709391e43daSPeter Zijlstra 710391e43daSPeter Zijlstra if (rt_rq->rt_time > rt_rq->rt_runtime) { 711391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 712391e43daSPeter Zijlstra more = do_balance_runtime(rt_rq); 713391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 714391e43daSPeter Zijlstra } 715391e43daSPeter Zijlstra 716391e43daSPeter Zijlstra return more; 717391e43daSPeter Zijlstra } 718391e43daSPeter Zijlstra #else /* !CONFIG_SMP */ 719391e43daSPeter Zijlstra static inline int balance_runtime(struct rt_rq *rt_rq) 720391e43daSPeter Zijlstra { 721391e43daSPeter Zijlstra return 0; 722391e43daSPeter Zijlstra } 723391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 724391e43daSPeter Zijlstra 725391e43daSPeter Zijlstra static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun) 726391e43daSPeter Zijlstra { 72742c62a58SPeter Zijlstra int i, idle = 1, throttled = 0; 728391e43daSPeter Zijlstra const struct cpumask *span; 729391e43daSPeter Zijlstra 730391e43daSPeter Zijlstra span = sched_rt_period_mask(); 731e221d028SMike Galbraith #ifdef CONFIG_RT_GROUP_SCHED 732e221d028SMike Galbraith /* 733e221d028SMike Galbraith * FIXME: isolated CPUs should really leave the root task group, 734e221d028SMike Galbraith * whether they are isolcpus or were isolated via cpusets, lest 735e221d028SMike Galbraith * the timer run on a CPU which does not service all runqueues, 736e221d028SMike Galbraith * potentially leaving other CPUs indefinitely throttled. If 737e221d028SMike Galbraith * isolation is really required, the user will turn the throttle 738e221d028SMike Galbraith * off to kill the perturbations it causes anyway. Meanwhile, 739e221d028SMike Galbraith * this maintains functionality for boot and/or troubleshooting. 740e221d028SMike Galbraith */ 741e221d028SMike Galbraith if (rt_b == &root_task_group.rt_bandwidth) 742e221d028SMike Galbraith span = cpu_online_mask; 743e221d028SMike Galbraith #endif 744391e43daSPeter Zijlstra for_each_cpu(i, span) { 745391e43daSPeter Zijlstra int enqueue = 0; 746391e43daSPeter Zijlstra struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i); 747391e43daSPeter Zijlstra struct rq *rq = rq_of_rt_rq(rt_rq); 748391e43daSPeter Zijlstra 749391e43daSPeter Zijlstra raw_spin_lock(&rq->lock); 750391e43daSPeter Zijlstra if (rt_rq->rt_time) { 751391e43daSPeter Zijlstra u64 runtime; 752391e43daSPeter Zijlstra 753391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 754391e43daSPeter Zijlstra if (rt_rq->rt_throttled) 755391e43daSPeter Zijlstra balance_runtime(rt_rq); 756391e43daSPeter Zijlstra runtime = rt_rq->rt_runtime; 757391e43daSPeter Zijlstra rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime); 758391e43daSPeter Zijlstra if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) { 759391e43daSPeter Zijlstra rt_rq->rt_throttled = 0; 760391e43daSPeter Zijlstra enqueue = 1; 761391e43daSPeter Zijlstra 762391e43daSPeter Zijlstra /* 763391e43daSPeter Zijlstra * Force a clock update if the CPU was idle, 764391e43daSPeter Zijlstra * lest wakeup -> unthrottle time accumulate. 765391e43daSPeter Zijlstra */ 766391e43daSPeter Zijlstra if (rt_rq->rt_nr_running && rq->curr == rq->idle) 767391e43daSPeter Zijlstra rq->skip_clock_update = -1; 768391e43daSPeter Zijlstra } 769391e43daSPeter Zijlstra if (rt_rq->rt_time || rt_rq->rt_nr_running) 770391e43daSPeter Zijlstra idle = 0; 771391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 772391e43daSPeter Zijlstra } else if (rt_rq->rt_nr_running) { 773391e43daSPeter Zijlstra idle = 0; 774391e43daSPeter Zijlstra if (!rt_rq_throttled(rt_rq)) 775391e43daSPeter Zijlstra enqueue = 1; 776391e43daSPeter Zijlstra } 77742c62a58SPeter Zijlstra if (rt_rq->rt_throttled) 77842c62a58SPeter Zijlstra throttled = 1; 779391e43daSPeter Zijlstra 780391e43daSPeter Zijlstra if (enqueue) 781391e43daSPeter Zijlstra sched_rt_rq_enqueue(rt_rq); 782391e43daSPeter Zijlstra raw_spin_unlock(&rq->lock); 783391e43daSPeter Zijlstra } 784391e43daSPeter Zijlstra 78542c62a58SPeter Zijlstra if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)) 78642c62a58SPeter Zijlstra return 1; 78742c62a58SPeter Zijlstra 788391e43daSPeter Zijlstra return idle; 789391e43daSPeter Zijlstra } 790391e43daSPeter Zijlstra 791391e43daSPeter Zijlstra static inline int rt_se_prio(struct sched_rt_entity *rt_se) 792391e43daSPeter Zijlstra { 793391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 794391e43daSPeter Zijlstra struct rt_rq *rt_rq = group_rt_rq(rt_se); 795391e43daSPeter Zijlstra 796391e43daSPeter Zijlstra if (rt_rq) 797391e43daSPeter Zijlstra return rt_rq->highest_prio.curr; 798391e43daSPeter Zijlstra #endif 799391e43daSPeter Zijlstra 800391e43daSPeter Zijlstra return rt_task_of(rt_se)->prio; 801391e43daSPeter Zijlstra } 802391e43daSPeter Zijlstra 803391e43daSPeter Zijlstra static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq) 804391e43daSPeter Zijlstra { 805391e43daSPeter Zijlstra u64 runtime = sched_rt_runtime(rt_rq); 806391e43daSPeter Zijlstra 807391e43daSPeter Zijlstra if (rt_rq->rt_throttled) 808391e43daSPeter Zijlstra return rt_rq_throttled(rt_rq); 809391e43daSPeter Zijlstra 8105b680fd6SShan Hai if (runtime >= sched_rt_period(rt_rq)) 811391e43daSPeter Zijlstra return 0; 812391e43daSPeter Zijlstra 813391e43daSPeter Zijlstra balance_runtime(rt_rq); 814391e43daSPeter Zijlstra runtime = sched_rt_runtime(rt_rq); 815391e43daSPeter Zijlstra if (runtime == RUNTIME_INF) 816391e43daSPeter Zijlstra return 0; 817391e43daSPeter Zijlstra 818391e43daSPeter Zijlstra if (rt_rq->rt_time > runtime) { 8197abc63b1SPeter Zijlstra struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); 8207abc63b1SPeter Zijlstra 8217abc63b1SPeter Zijlstra /* 8227abc63b1SPeter Zijlstra * Don't actually throttle groups that have no runtime assigned 8237abc63b1SPeter Zijlstra * but accrue some time due to boosting. 8247abc63b1SPeter Zijlstra */ 8257abc63b1SPeter Zijlstra if (likely(rt_b->rt_runtime)) { 8263ccf3e83SPeter Zijlstra static bool once = false; 8273ccf3e83SPeter Zijlstra 828391e43daSPeter Zijlstra rt_rq->rt_throttled = 1; 8293ccf3e83SPeter Zijlstra 8303ccf3e83SPeter Zijlstra if (!once) { 8313ccf3e83SPeter Zijlstra once = true; 8323ccf3e83SPeter Zijlstra printk_sched("sched: RT throttling activated\n"); 8333ccf3e83SPeter Zijlstra } 8347abc63b1SPeter Zijlstra } else { 8357abc63b1SPeter Zijlstra /* 8367abc63b1SPeter Zijlstra * In case we did anyway, make it go away, 8377abc63b1SPeter Zijlstra * replenishment is a joke, since it will replenish us 8387abc63b1SPeter Zijlstra * with exactly 0 ns. 8397abc63b1SPeter Zijlstra */ 8407abc63b1SPeter Zijlstra rt_rq->rt_time = 0; 8417abc63b1SPeter Zijlstra } 8427abc63b1SPeter Zijlstra 843391e43daSPeter Zijlstra if (rt_rq_throttled(rt_rq)) { 844391e43daSPeter Zijlstra sched_rt_rq_dequeue(rt_rq); 845391e43daSPeter Zijlstra return 1; 846391e43daSPeter Zijlstra } 847391e43daSPeter Zijlstra } 848391e43daSPeter Zijlstra 849391e43daSPeter Zijlstra return 0; 850391e43daSPeter Zijlstra } 851391e43daSPeter Zijlstra 852391e43daSPeter Zijlstra /* 853391e43daSPeter Zijlstra * Update the current task's runtime statistics. Skip current tasks that 854391e43daSPeter Zijlstra * are not in our scheduling class. 855391e43daSPeter Zijlstra */ 856391e43daSPeter Zijlstra static void update_curr_rt(struct rq *rq) 857391e43daSPeter Zijlstra { 858391e43daSPeter Zijlstra struct task_struct *curr = rq->curr; 859391e43daSPeter Zijlstra struct sched_rt_entity *rt_se = &curr->rt; 860391e43daSPeter Zijlstra struct rt_rq *rt_rq = rt_rq_of_se(rt_se); 861391e43daSPeter Zijlstra u64 delta_exec; 862391e43daSPeter Zijlstra 863391e43daSPeter Zijlstra if (curr->sched_class != &rt_sched_class) 864391e43daSPeter Zijlstra return; 865391e43daSPeter Zijlstra 86678becc27SFrederic Weisbecker delta_exec = rq_clock_task(rq) - curr->se.exec_start; 867fc79e240SKirill Tkhai if (unlikely((s64)delta_exec <= 0)) 868fc79e240SKirill Tkhai return; 869391e43daSPeter Zijlstra 87042c62a58SPeter Zijlstra schedstat_set(curr->se.statistics.exec_max, 87142c62a58SPeter Zijlstra max(curr->se.statistics.exec_max, delta_exec)); 872391e43daSPeter Zijlstra 873391e43daSPeter Zijlstra curr->se.sum_exec_runtime += delta_exec; 874391e43daSPeter Zijlstra account_group_exec_runtime(curr, delta_exec); 875391e43daSPeter Zijlstra 87678becc27SFrederic Weisbecker curr->se.exec_start = rq_clock_task(rq); 877391e43daSPeter Zijlstra cpuacct_charge(curr, delta_exec); 878391e43daSPeter Zijlstra 879391e43daSPeter Zijlstra sched_rt_avg_update(rq, delta_exec); 880391e43daSPeter Zijlstra 881391e43daSPeter Zijlstra if (!rt_bandwidth_enabled()) 882391e43daSPeter Zijlstra return; 883391e43daSPeter Zijlstra 884391e43daSPeter Zijlstra for_each_sched_rt_entity(rt_se) { 885391e43daSPeter Zijlstra rt_rq = rt_rq_of_se(rt_se); 886391e43daSPeter Zijlstra 887391e43daSPeter Zijlstra if (sched_rt_runtime(rt_rq) != RUNTIME_INF) { 888391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 889391e43daSPeter Zijlstra rt_rq->rt_time += delta_exec; 890391e43daSPeter Zijlstra if (sched_rt_runtime_exceeded(rt_rq)) 891391e43daSPeter Zijlstra resched_task(curr); 892391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 893391e43daSPeter Zijlstra } 894391e43daSPeter Zijlstra } 895391e43daSPeter Zijlstra } 896391e43daSPeter Zijlstra 897391e43daSPeter Zijlstra #if defined CONFIG_SMP 898391e43daSPeter Zijlstra 899391e43daSPeter Zijlstra static void 900391e43daSPeter Zijlstra inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) 901391e43daSPeter Zijlstra { 902391e43daSPeter Zijlstra struct rq *rq = rq_of_rt_rq(rt_rq); 903391e43daSPeter Zijlstra 904*757dfcaaSKirill Tkhai #ifdef CONFIG_RT_GROUP_SCHED 905*757dfcaaSKirill Tkhai /* 906*757dfcaaSKirill Tkhai * Change rq's cpupri only if rt_rq is the top queue. 907*757dfcaaSKirill Tkhai */ 908*757dfcaaSKirill Tkhai if (&rq->rt != rt_rq) 909*757dfcaaSKirill Tkhai return; 910*757dfcaaSKirill Tkhai #endif 911391e43daSPeter Zijlstra if (rq->online && prio < prev_prio) 912391e43daSPeter Zijlstra cpupri_set(&rq->rd->cpupri, rq->cpu, prio); 913391e43daSPeter Zijlstra } 914391e43daSPeter Zijlstra 915391e43daSPeter Zijlstra static void 916391e43daSPeter Zijlstra dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) 917391e43daSPeter Zijlstra { 918391e43daSPeter Zijlstra struct rq *rq = rq_of_rt_rq(rt_rq); 919391e43daSPeter Zijlstra 920*757dfcaaSKirill Tkhai #ifdef CONFIG_RT_GROUP_SCHED 921*757dfcaaSKirill Tkhai /* 922*757dfcaaSKirill Tkhai * Change rq's cpupri only if rt_rq is the top queue. 923*757dfcaaSKirill Tkhai */ 924*757dfcaaSKirill Tkhai if (&rq->rt != rt_rq) 925*757dfcaaSKirill Tkhai return; 926*757dfcaaSKirill Tkhai #endif 927391e43daSPeter Zijlstra if (rq->online && rt_rq->highest_prio.curr != prev_prio) 928391e43daSPeter Zijlstra cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr); 929391e43daSPeter Zijlstra } 930391e43daSPeter Zijlstra 931391e43daSPeter Zijlstra #else /* CONFIG_SMP */ 932391e43daSPeter Zijlstra 933391e43daSPeter Zijlstra static inline 934391e43daSPeter Zijlstra void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} 935391e43daSPeter Zijlstra static inline 936391e43daSPeter Zijlstra void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} 937391e43daSPeter Zijlstra 938391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 939391e43daSPeter Zijlstra 940391e43daSPeter Zijlstra #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED 941391e43daSPeter Zijlstra static void 942391e43daSPeter Zijlstra inc_rt_prio(struct rt_rq *rt_rq, int prio) 943391e43daSPeter Zijlstra { 944391e43daSPeter Zijlstra int prev_prio = rt_rq->highest_prio.curr; 945391e43daSPeter Zijlstra 946391e43daSPeter Zijlstra if (prio < prev_prio) 947391e43daSPeter Zijlstra rt_rq->highest_prio.curr = prio; 948391e43daSPeter Zijlstra 949391e43daSPeter Zijlstra inc_rt_prio_smp(rt_rq, prio, prev_prio); 950391e43daSPeter Zijlstra } 951391e43daSPeter Zijlstra 952391e43daSPeter Zijlstra static void 953391e43daSPeter Zijlstra dec_rt_prio(struct rt_rq *rt_rq, int prio) 954391e43daSPeter Zijlstra { 955391e43daSPeter Zijlstra int prev_prio = rt_rq->highest_prio.curr; 956391e43daSPeter Zijlstra 957391e43daSPeter Zijlstra if (rt_rq->rt_nr_running) { 958391e43daSPeter Zijlstra 959391e43daSPeter Zijlstra WARN_ON(prio < prev_prio); 960391e43daSPeter Zijlstra 961391e43daSPeter Zijlstra /* 962391e43daSPeter Zijlstra * This may have been our highest task, and therefore 963391e43daSPeter Zijlstra * we may have some recomputation to do 964391e43daSPeter Zijlstra */ 965391e43daSPeter Zijlstra if (prio == prev_prio) { 966391e43daSPeter Zijlstra struct rt_prio_array *array = &rt_rq->active; 967391e43daSPeter Zijlstra 968391e43daSPeter Zijlstra rt_rq->highest_prio.curr = 969391e43daSPeter Zijlstra sched_find_first_bit(array->bitmap); 970391e43daSPeter Zijlstra } 971391e43daSPeter Zijlstra 972391e43daSPeter Zijlstra } else 973391e43daSPeter Zijlstra rt_rq->highest_prio.curr = MAX_RT_PRIO; 974391e43daSPeter Zijlstra 975391e43daSPeter Zijlstra dec_rt_prio_smp(rt_rq, prio, prev_prio); 976391e43daSPeter Zijlstra } 977391e43daSPeter Zijlstra 978391e43daSPeter Zijlstra #else 979391e43daSPeter Zijlstra 980391e43daSPeter Zijlstra static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {} 981391e43daSPeter Zijlstra static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {} 982391e43daSPeter Zijlstra 983391e43daSPeter Zijlstra #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */ 984391e43daSPeter Zijlstra 985391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 986391e43daSPeter Zijlstra 987391e43daSPeter Zijlstra static void 988391e43daSPeter Zijlstra inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 989391e43daSPeter Zijlstra { 990391e43daSPeter Zijlstra if (rt_se_boosted(rt_se)) 991391e43daSPeter Zijlstra rt_rq->rt_nr_boosted++; 992391e43daSPeter Zijlstra 993391e43daSPeter Zijlstra if (rt_rq->tg) 994391e43daSPeter Zijlstra start_rt_bandwidth(&rt_rq->tg->rt_bandwidth); 995391e43daSPeter Zijlstra } 996391e43daSPeter Zijlstra 997391e43daSPeter Zijlstra static void 998391e43daSPeter Zijlstra dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 999391e43daSPeter Zijlstra { 1000391e43daSPeter Zijlstra if (rt_se_boosted(rt_se)) 1001391e43daSPeter Zijlstra rt_rq->rt_nr_boosted--; 1002391e43daSPeter Zijlstra 1003391e43daSPeter Zijlstra WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted); 1004391e43daSPeter Zijlstra } 1005391e43daSPeter Zijlstra 1006391e43daSPeter Zijlstra #else /* CONFIG_RT_GROUP_SCHED */ 1007391e43daSPeter Zijlstra 1008391e43daSPeter Zijlstra static void 1009391e43daSPeter Zijlstra inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 1010391e43daSPeter Zijlstra { 1011391e43daSPeter Zijlstra start_rt_bandwidth(&def_rt_bandwidth); 1012391e43daSPeter Zijlstra } 1013391e43daSPeter Zijlstra 1014391e43daSPeter Zijlstra static inline 1015391e43daSPeter Zijlstra void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {} 1016391e43daSPeter Zijlstra 1017391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */ 1018391e43daSPeter Zijlstra 1019391e43daSPeter Zijlstra static inline 1020391e43daSPeter Zijlstra void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 1021391e43daSPeter Zijlstra { 1022391e43daSPeter Zijlstra int prio = rt_se_prio(rt_se); 1023391e43daSPeter Zijlstra 1024391e43daSPeter Zijlstra WARN_ON(!rt_prio(prio)); 1025391e43daSPeter Zijlstra rt_rq->rt_nr_running++; 1026391e43daSPeter Zijlstra 1027391e43daSPeter Zijlstra inc_rt_prio(rt_rq, prio); 1028391e43daSPeter Zijlstra inc_rt_migration(rt_se, rt_rq); 1029391e43daSPeter Zijlstra inc_rt_group(rt_se, rt_rq); 1030391e43daSPeter Zijlstra } 1031391e43daSPeter Zijlstra 1032391e43daSPeter Zijlstra static inline 1033391e43daSPeter Zijlstra void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 1034391e43daSPeter Zijlstra { 1035391e43daSPeter Zijlstra WARN_ON(!rt_prio(rt_se_prio(rt_se))); 1036391e43daSPeter Zijlstra WARN_ON(!rt_rq->rt_nr_running); 1037391e43daSPeter Zijlstra rt_rq->rt_nr_running--; 1038391e43daSPeter Zijlstra 1039391e43daSPeter Zijlstra dec_rt_prio(rt_rq, rt_se_prio(rt_se)); 1040391e43daSPeter Zijlstra dec_rt_migration(rt_se, rt_rq); 1041391e43daSPeter Zijlstra dec_rt_group(rt_se, rt_rq); 1042391e43daSPeter Zijlstra } 1043391e43daSPeter Zijlstra 1044391e43daSPeter Zijlstra static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head) 1045391e43daSPeter Zijlstra { 1046391e43daSPeter Zijlstra struct rt_rq *rt_rq = rt_rq_of_se(rt_se); 1047391e43daSPeter Zijlstra struct rt_prio_array *array = &rt_rq->active; 1048391e43daSPeter Zijlstra struct rt_rq *group_rq = group_rt_rq(rt_se); 1049391e43daSPeter Zijlstra struct list_head *queue = array->queue + rt_se_prio(rt_se); 1050391e43daSPeter Zijlstra 1051391e43daSPeter Zijlstra /* 1052391e43daSPeter Zijlstra * Don't enqueue the group if its throttled, or when empty. 1053391e43daSPeter Zijlstra * The latter is a consequence of the former when a child group 1054391e43daSPeter Zijlstra * get throttled and the current group doesn't have any other 1055391e43daSPeter Zijlstra * active members. 1056391e43daSPeter Zijlstra */ 1057391e43daSPeter Zijlstra if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) 1058391e43daSPeter Zijlstra return; 1059391e43daSPeter Zijlstra 1060391e43daSPeter Zijlstra if (head) 1061391e43daSPeter Zijlstra list_add(&rt_se->run_list, queue); 1062391e43daSPeter Zijlstra else 1063391e43daSPeter Zijlstra list_add_tail(&rt_se->run_list, queue); 1064391e43daSPeter Zijlstra __set_bit(rt_se_prio(rt_se), array->bitmap); 1065391e43daSPeter Zijlstra 1066391e43daSPeter Zijlstra inc_rt_tasks(rt_se, rt_rq); 1067391e43daSPeter Zijlstra } 1068391e43daSPeter Zijlstra 1069391e43daSPeter Zijlstra static void __dequeue_rt_entity(struct sched_rt_entity *rt_se) 1070391e43daSPeter Zijlstra { 1071391e43daSPeter Zijlstra struct rt_rq *rt_rq = rt_rq_of_se(rt_se); 1072391e43daSPeter Zijlstra struct rt_prio_array *array = &rt_rq->active; 1073391e43daSPeter Zijlstra 1074391e43daSPeter Zijlstra list_del_init(&rt_se->run_list); 1075391e43daSPeter Zijlstra if (list_empty(array->queue + rt_se_prio(rt_se))) 1076391e43daSPeter Zijlstra __clear_bit(rt_se_prio(rt_se), array->bitmap); 1077391e43daSPeter Zijlstra 1078391e43daSPeter Zijlstra dec_rt_tasks(rt_se, rt_rq); 1079391e43daSPeter Zijlstra } 1080391e43daSPeter Zijlstra 1081391e43daSPeter Zijlstra /* 1082391e43daSPeter Zijlstra * Because the prio of an upper entry depends on the lower 1083391e43daSPeter Zijlstra * entries, we must remove entries top - down. 1084391e43daSPeter Zijlstra */ 1085391e43daSPeter Zijlstra static void dequeue_rt_stack(struct sched_rt_entity *rt_se) 1086391e43daSPeter Zijlstra { 1087391e43daSPeter Zijlstra struct sched_rt_entity *back = NULL; 1088391e43daSPeter Zijlstra 1089391e43daSPeter Zijlstra for_each_sched_rt_entity(rt_se) { 1090391e43daSPeter Zijlstra rt_se->back = back; 1091391e43daSPeter Zijlstra back = rt_se; 1092391e43daSPeter Zijlstra } 1093391e43daSPeter Zijlstra 1094391e43daSPeter Zijlstra for (rt_se = back; rt_se; rt_se = rt_se->back) { 1095391e43daSPeter Zijlstra if (on_rt_rq(rt_se)) 1096391e43daSPeter Zijlstra __dequeue_rt_entity(rt_se); 1097391e43daSPeter Zijlstra } 1098391e43daSPeter Zijlstra } 1099391e43daSPeter Zijlstra 1100391e43daSPeter Zijlstra static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head) 1101391e43daSPeter Zijlstra { 1102391e43daSPeter Zijlstra dequeue_rt_stack(rt_se); 1103391e43daSPeter Zijlstra for_each_sched_rt_entity(rt_se) 1104391e43daSPeter Zijlstra __enqueue_rt_entity(rt_se, head); 1105391e43daSPeter Zijlstra } 1106391e43daSPeter Zijlstra 1107391e43daSPeter Zijlstra static void dequeue_rt_entity(struct sched_rt_entity *rt_se) 1108391e43daSPeter Zijlstra { 1109391e43daSPeter Zijlstra dequeue_rt_stack(rt_se); 1110391e43daSPeter Zijlstra 1111391e43daSPeter Zijlstra for_each_sched_rt_entity(rt_se) { 1112391e43daSPeter Zijlstra struct rt_rq *rt_rq = group_rt_rq(rt_se); 1113391e43daSPeter Zijlstra 1114391e43daSPeter Zijlstra if (rt_rq && rt_rq->rt_nr_running) 1115391e43daSPeter Zijlstra __enqueue_rt_entity(rt_se, false); 1116391e43daSPeter Zijlstra } 1117391e43daSPeter Zijlstra } 1118391e43daSPeter Zijlstra 1119391e43daSPeter Zijlstra /* 1120391e43daSPeter Zijlstra * Adding/removing a task to/from a priority array: 1121391e43daSPeter Zijlstra */ 1122391e43daSPeter Zijlstra static void 1123391e43daSPeter Zijlstra enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags) 1124391e43daSPeter Zijlstra { 1125391e43daSPeter Zijlstra struct sched_rt_entity *rt_se = &p->rt; 1126391e43daSPeter Zijlstra 1127391e43daSPeter Zijlstra if (flags & ENQUEUE_WAKEUP) 1128391e43daSPeter Zijlstra rt_se->timeout = 0; 1129391e43daSPeter Zijlstra 1130391e43daSPeter Zijlstra enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD); 1131391e43daSPeter Zijlstra 113229baa747SPeter Zijlstra if (!task_current(rq, p) && p->nr_cpus_allowed > 1) 1133391e43daSPeter Zijlstra enqueue_pushable_task(rq, p); 1134391e43daSPeter Zijlstra 1135391e43daSPeter Zijlstra inc_nr_running(rq); 1136391e43daSPeter Zijlstra } 1137391e43daSPeter Zijlstra 1138391e43daSPeter Zijlstra static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags) 1139391e43daSPeter Zijlstra { 1140391e43daSPeter Zijlstra struct sched_rt_entity *rt_se = &p->rt; 1141391e43daSPeter Zijlstra 1142391e43daSPeter Zijlstra update_curr_rt(rq); 1143391e43daSPeter Zijlstra dequeue_rt_entity(rt_se); 1144391e43daSPeter Zijlstra 1145391e43daSPeter Zijlstra dequeue_pushable_task(rq, p); 1146391e43daSPeter Zijlstra 1147391e43daSPeter Zijlstra dec_nr_running(rq); 1148391e43daSPeter Zijlstra } 1149391e43daSPeter Zijlstra 1150391e43daSPeter Zijlstra /* 1151391e43daSPeter Zijlstra * Put task to the head or the end of the run list without the overhead of 1152391e43daSPeter Zijlstra * dequeue followed by enqueue. 1153391e43daSPeter Zijlstra */ 1154391e43daSPeter Zijlstra static void 1155391e43daSPeter Zijlstra requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head) 1156391e43daSPeter Zijlstra { 1157391e43daSPeter Zijlstra if (on_rt_rq(rt_se)) { 1158391e43daSPeter Zijlstra struct rt_prio_array *array = &rt_rq->active; 1159391e43daSPeter Zijlstra struct list_head *queue = array->queue + rt_se_prio(rt_se); 1160391e43daSPeter Zijlstra 1161391e43daSPeter Zijlstra if (head) 1162391e43daSPeter Zijlstra list_move(&rt_se->run_list, queue); 1163391e43daSPeter Zijlstra else 1164391e43daSPeter Zijlstra list_move_tail(&rt_se->run_list, queue); 1165391e43daSPeter Zijlstra } 1166391e43daSPeter Zijlstra } 1167391e43daSPeter Zijlstra 1168391e43daSPeter Zijlstra static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head) 1169391e43daSPeter Zijlstra { 1170391e43daSPeter Zijlstra struct sched_rt_entity *rt_se = &p->rt; 1171391e43daSPeter Zijlstra struct rt_rq *rt_rq; 1172391e43daSPeter Zijlstra 1173391e43daSPeter Zijlstra for_each_sched_rt_entity(rt_se) { 1174391e43daSPeter Zijlstra rt_rq = rt_rq_of_se(rt_se); 1175391e43daSPeter Zijlstra requeue_rt_entity(rt_rq, rt_se, head); 1176391e43daSPeter Zijlstra } 1177391e43daSPeter Zijlstra } 1178391e43daSPeter Zijlstra 1179391e43daSPeter Zijlstra static void yield_task_rt(struct rq *rq) 1180391e43daSPeter Zijlstra { 1181391e43daSPeter Zijlstra requeue_task_rt(rq, rq->curr, 0); 1182391e43daSPeter Zijlstra } 1183391e43daSPeter Zijlstra 1184391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1185391e43daSPeter Zijlstra static int find_lowest_rq(struct task_struct *task); 1186391e43daSPeter Zijlstra 1187391e43daSPeter Zijlstra static int 1188ac66f547SPeter Zijlstra select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags) 1189391e43daSPeter Zijlstra { 1190391e43daSPeter Zijlstra struct task_struct *curr; 1191391e43daSPeter Zijlstra struct rq *rq; 1192391e43daSPeter Zijlstra 119329baa747SPeter Zijlstra if (p->nr_cpus_allowed == 1) 119476854c7eSMike Galbraith goto out; 119576854c7eSMike Galbraith 1196391e43daSPeter Zijlstra /* For anything but wake ups, just return the task_cpu */ 1197391e43daSPeter Zijlstra if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK) 1198391e43daSPeter Zijlstra goto out; 1199391e43daSPeter Zijlstra 1200391e43daSPeter Zijlstra rq = cpu_rq(cpu); 1201391e43daSPeter Zijlstra 1202391e43daSPeter Zijlstra rcu_read_lock(); 1203391e43daSPeter Zijlstra curr = ACCESS_ONCE(rq->curr); /* unlocked access */ 1204391e43daSPeter Zijlstra 1205391e43daSPeter Zijlstra /* 1206391e43daSPeter Zijlstra * If the current task on @p's runqueue is an RT task, then 1207391e43daSPeter Zijlstra * try to see if we can wake this RT task up on another 1208391e43daSPeter Zijlstra * runqueue. Otherwise simply start this RT task 1209391e43daSPeter Zijlstra * on its current runqueue. 1210391e43daSPeter Zijlstra * 1211391e43daSPeter Zijlstra * We want to avoid overloading runqueues. If the woken 1212391e43daSPeter Zijlstra * task is a higher priority, then it will stay on this CPU 1213391e43daSPeter Zijlstra * and the lower prio task should be moved to another CPU. 1214391e43daSPeter Zijlstra * Even though this will probably make the lower prio task 1215391e43daSPeter Zijlstra * lose its cache, we do not want to bounce a higher task 1216391e43daSPeter Zijlstra * around just because it gave up its CPU, perhaps for a 1217391e43daSPeter Zijlstra * lock? 1218391e43daSPeter Zijlstra * 1219391e43daSPeter Zijlstra * For equal prio tasks, we just let the scheduler sort it out. 1220391e43daSPeter Zijlstra * 1221391e43daSPeter Zijlstra * Otherwise, just let it ride on the affined RQ and the 1222391e43daSPeter Zijlstra * post-schedule router will push the preempted task away 1223391e43daSPeter Zijlstra * 1224391e43daSPeter Zijlstra * This test is optimistic, if we get it wrong the load-balancer 1225391e43daSPeter Zijlstra * will have to sort it out. 1226391e43daSPeter Zijlstra */ 1227391e43daSPeter Zijlstra if (curr && unlikely(rt_task(curr)) && 122829baa747SPeter Zijlstra (curr->nr_cpus_allowed < 2 || 12296bfa687cSShawn Bohrer curr->prio <= p->prio)) { 1230391e43daSPeter Zijlstra int target = find_lowest_rq(p); 1231391e43daSPeter Zijlstra 1232391e43daSPeter Zijlstra if (target != -1) 1233391e43daSPeter Zijlstra cpu = target; 1234391e43daSPeter Zijlstra } 1235391e43daSPeter Zijlstra rcu_read_unlock(); 1236391e43daSPeter Zijlstra 1237391e43daSPeter Zijlstra out: 1238391e43daSPeter Zijlstra return cpu; 1239391e43daSPeter Zijlstra } 1240391e43daSPeter Zijlstra 1241391e43daSPeter Zijlstra static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p) 1242391e43daSPeter Zijlstra { 124329baa747SPeter Zijlstra if (rq->curr->nr_cpus_allowed == 1) 1244391e43daSPeter Zijlstra return; 1245391e43daSPeter Zijlstra 124629baa747SPeter Zijlstra if (p->nr_cpus_allowed != 1 1247391e43daSPeter Zijlstra && cpupri_find(&rq->rd->cpupri, p, NULL)) 1248391e43daSPeter Zijlstra return; 1249391e43daSPeter Zijlstra 1250391e43daSPeter Zijlstra if (!cpupri_find(&rq->rd->cpupri, rq->curr, NULL)) 1251391e43daSPeter Zijlstra return; 1252391e43daSPeter Zijlstra 1253391e43daSPeter Zijlstra /* 1254391e43daSPeter Zijlstra * There appears to be other cpus that can accept 1255391e43daSPeter Zijlstra * current and none to run 'p', so lets reschedule 1256391e43daSPeter Zijlstra * to try and push current away: 1257391e43daSPeter Zijlstra */ 1258391e43daSPeter Zijlstra requeue_task_rt(rq, p, 1); 1259391e43daSPeter Zijlstra resched_task(rq->curr); 1260391e43daSPeter Zijlstra } 1261391e43daSPeter Zijlstra 1262391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 1263391e43daSPeter Zijlstra 1264391e43daSPeter Zijlstra /* 1265391e43daSPeter Zijlstra * Preempt the current task with a newly woken task if needed: 1266391e43daSPeter Zijlstra */ 1267391e43daSPeter Zijlstra static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags) 1268391e43daSPeter Zijlstra { 1269391e43daSPeter Zijlstra if (p->prio < rq->curr->prio) { 1270391e43daSPeter Zijlstra resched_task(rq->curr); 1271391e43daSPeter Zijlstra return; 1272391e43daSPeter Zijlstra } 1273391e43daSPeter Zijlstra 1274391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1275391e43daSPeter Zijlstra /* 1276391e43daSPeter Zijlstra * If: 1277391e43daSPeter Zijlstra * 1278391e43daSPeter Zijlstra * - the newly woken task is of equal priority to the current task 1279391e43daSPeter Zijlstra * - the newly woken task is non-migratable while current is migratable 1280391e43daSPeter Zijlstra * - current will be preempted on the next reschedule 1281391e43daSPeter Zijlstra * 1282391e43daSPeter Zijlstra * we should check to see if current can readily move to a different 1283391e43daSPeter Zijlstra * cpu. If so, we will reschedule to allow the push logic to try 1284391e43daSPeter Zijlstra * to move current somewhere else, making room for our non-migratable 1285391e43daSPeter Zijlstra * task. 1286391e43daSPeter Zijlstra */ 1287391e43daSPeter Zijlstra if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr)) 1288391e43daSPeter Zijlstra check_preempt_equal_prio(rq, p); 1289391e43daSPeter Zijlstra #endif 1290391e43daSPeter Zijlstra } 1291391e43daSPeter Zijlstra 1292391e43daSPeter Zijlstra static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq, 1293391e43daSPeter Zijlstra struct rt_rq *rt_rq) 1294391e43daSPeter Zijlstra { 1295391e43daSPeter Zijlstra struct rt_prio_array *array = &rt_rq->active; 1296391e43daSPeter Zijlstra struct sched_rt_entity *next = NULL; 1297391e43daSPeter Zijlstra struct list_head *queue; 1298391e43daSPeter Zijlstra int idx; 1299391e43daSPeter Zijlstra 1300391e43daSPeter Zijlstra idx = sched_find_first_bit(array->bitmap); 1301391e43daSPeter Zijlstra BUG_ON(idx >= MAX_RT_PRIO); 1302391e43daSPeter Zijlstra 1303391e43daSPeter Zijlstra queue = array->queue + idx; 1304391e43daSPeter Zijlstra next = list_entry(queue->next, struct sched_rt_entity, run_list); 1305391e43daSPeter Zijlstra 1306391e43daSPeter Zijlstra return next; 1307391e43daSPeter Zijlstra } 1308391e43daSPeter Zijlstra 1309391e43daSPeter Zijlstra static struct task_struct *_pick_next_task_rt(struct rq *rq) 1310391e43daSPeter Zijlstra { 1311391e43daSPeter Zijlstra struct sched_rt_entity *rt_se; 1312391e43daSPeter Zijlstra struct task_struct *p; 1313391e43daSPeter Zijlstra struct rt_rq *rt_rq; 1314391e43daSPeter Zijlstra 1315391e43daSPeter Zijlstra rt_rq = &rq->rt; 1316391e43daSPeter Zijlstra 1317391e43daSPeter Zijlstra if (!rt_rq->rt_nr_running) 1318391e43daSPeter Zijlstra return NULL; 1319391e43daSPeter Zijlstra 1320391e43daSPeter Zijlstra if (rt_rq_throttled(rt_rq)) 1321391e43daSPeter Zijlstra return NULL; 1322391e43daSPeter Zijlstra 1323391e43daSPeter Zijlstra do { 1324391e43daSPeter Zijlstra rt_se = pick_next_rt_entity(rq, rt_rq); 1325391e43daSPeter Zijlstra BUG_ON(!rt_se); 1326391e43daSPeter Zijlstra rt_rq = group_rt_rq(rt_se); 1327391e43daSPeter Zijlstra } while (rt_rq); 1328391e43daSPeter Zijlstra 1329391e43daSPeter Zijlstra p = rt_task_of(rt_se); 133078becc27SFrederic Weisbecker p->se.exec_start = rq_clock_task(rq); 1331391e43daSPeter Zijlstra 1332391e43daSPeter Zijlstra return p; 1333391e43daSPeter Zijlstra } 1334391e43daSPeter Zijlstra 1335391e43daSPeter Zijlstra static struct task_struct *pick_next_task_rt(struct rq *rq) 1336391e43daSPeter Zijlstra { 1337391e43daSPeter Zijlstra struct task_struct *p = _pick_next_task_rt(rq); 1338391e43daSPeter Zijlstra 1339391e43daSPeter Zijlstra /* The running task is never eligible for pushing */ 1340391e43daSPeter Zijlstra if (p) 1341391e43daSPeter Zijlstra dequeue_pushable_task(rq, p); 1342391e43daSPeter Zijlstra 1343391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1344391e43daSPeter Zijlstra /* 1345391e43daSPeter Zijlstra * We detect this state here so that we can avoid taking the RQ 1346391e43daSPeter Zijlstra * lock again later if there is no need to push 1347391e43daSPeter Zijlstra */ 1348391e43daSPeter Zijlstra rq->post_schedule = has_pushable_tasks(rq); 1349391e43daSPeter Zijlstra #endif 1350391e43daSPeter Zijlstra 1351391e43daSPeter Zijlstra return p; 1352391e43daSPeter Zijlstra } 1353391e43daSPeter Zijlstra 1354391e43daSPeter Zijlstra static void put_prev_task_rt(struct rq *rq, struct task_struct *p) 1355391e43daSPeter Zijlstra { 1356391e43daSPeter Zijlstra update_curr_rt(rq); 1357391e43daSPeter Zijlstra 1358391e43daSPeter Zijlstra /* 1359391e43daSPeter Zijlstra * The previous task needs to be made eligible for pushing 1360391e43daSPeter Zijlstra * if it is still active 1361391e43daSPeter Zijlstra */ 136229baa747SPeter Zijlstra if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1) 1363391e43daSPeter Zijlstra enqueue_pushable_task(rq, p); 1364391e43daSPeter Zijlstra } 1365391e43daSPeter Zijlstra 1366391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1367391e43daSPeter Zijlstra 1368391e43daSPeter Zijlstra /* Only try algorithms three times */ 1369391e43daSPeter Zijlstra #define RT_MAX_TRIES 3 1370391e43daSPeter Zijlstra 1371391e43daSPeter Zijlstra static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu) 1372391e43daSPeter Zijlstra { 1373391e43daSPeter Zijlstra if (!task_running(rq, p) && 137460334cafSKirill Tkhai cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) 1375391e43daSPeter Zijlstra return 1; 1376391e43daSPeter Zijlstra return 0; 1377391e43daSPeter Zijlstra } 1378391e43daSPeter Zijlstra 1379e23ee747SKirill Tkhai /* 1380e23ee747SKirill Tkhai * Return the highest pushable rq's task, which is suitable to be executed 1381e23ee747SKirill Tkhai * on the cpu, NULL otherwise 1382e23ee747SKirill Tkhai */ 1383e23ee747SKirill Tkhai static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu) 1384391e43daSPeter Zijlstra { 1385e23ee747SKirill Tkhai struct plist_head *head = &rq->rt.pushable_tasks; 1386391e43daSPeter Zijlstra struct task_struct *p; 1387391e43daSPeter Zijlstra 1388e23ee747SKirill Tkhai if (!has_pushable_tasks(rq)) 1389e23ee747SKirill Tkhai return NULL; 1390391e43daSPeter Zijlstra 1391e23ee747SKirill Tkhai plist_for_each_entry(p, head, pushable_tasks) { 1392e23ee747SKirill Tkhai if (pick_rt_task(rq, p, cpu)) 1393e23ee747SKirill Tkhai return p; 1394391e43daSPeter Zijlstra } 1395391e43daSPeter Zijlstra 1396e23ee747SKirill Tkhai return NULL; 1397391e43daSPeter Zijlstra } 1398391e43daSPeter Zijlstra 1399391e43daSPeter Zijlstra static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask); 1400391e43daSPeter Zijlstra 1401391e43daSPeter Zijlstra static int find_lowest_rq(struct task_struct *task) 1402391e43daSPeter Zijlstra { 1403391e43daSPeter Zijlstra struct sched_domain *sd; 1404391e43daSPeter Zijlstra struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask); 1405391e43daSPeter Zijlstra int this_cpu = smp_processor_id(); 1406391e43daSPeter Zijlstra int cpu = task_cpu(task); 1407391e43daSPeter Zijlstra 1408391e43daSPeter Zijlstra /* Make sure the mask is initialized first */ 1409391e43daSPeter Zijlstra if (unlikely(!lowest_mask)) 1410391e43daSPeter Zijlstra return -1; 1411391e43daSPeter Zijlstra 141229baa747SPeter Zijlstra if (task->nr_cpus_allowed == 1) 1413391e43daSPeter Zijlstra return -1; /* No other targets possible */ 1414391e43daSPeter Zijlstra 1415391e43daSPeter Zijlstra if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask)) 1416391e43daSPeter Zijlstra return -1; /* No targets found */ 1417391e43daSPeter Zijlstra 1418391e43daSPeter Zijlstra /* 1419391e43daSPeter Zijlstra * At this point we have built a mask of cpus representing the 1420391e43daSPeter Zijlstra * lowest priority tasks in the system. Now we want to elect 1421391e43daSPeter Zijlstra * the best one based on our affinity and topology. 1422391e43daSPeter Zijlstra * 1423391e43daSPeter Zijlstra * We prioritize the last cpu that the task executed on since 1424391e43daSPeter Zijlstra * it is most likely cache-hot in that location. 1425391e43daSPeter Zijlstra */ 1426391e43daSPeter Zijlstra if (cpumask_test_cpu(cpu, lowest_mask)) 1427391e43daSPeter Zijlstra return cpu; 1428391e43daSPeter Zijlstra 1429391e43daSPeter Zijlstra /* 1430391e43daSPeter Zijlstra * Otherwise, we consult the sched_domains span maps to figure 1431391e43daSPeter Zijlstra * out which cpu is logically closest to our hot cache data. 1432391e43daSPeter Zijlstra */ 1433391e43daSPeter Zijlstra if (!cpumask_test_cpu(this_cpu, lowest_mask)) 1434391e43daSPeter Zijlstra this_cpu = -1; /* Skip this_cpu opt if not among lowest */ 1435391e43daSPeter Zijlstra 1436391e43daSPeter Zijlstra rcu_read_lock(); 1437391e43daSPeter Zijlstra for_each_domain(cpu, sd) { 1438391e43daSPeter Zijlstra if (sd->flags & SD_WAKE_AFFINE) { 1439391e43daSPeter Zijlstra int best_cpu; 1440391e43daSPeter Zijlstra 1441391e43daSPeter Zijlstra /* 1442391e43daSPeter Zijlstra * "this_cpu" is cheaper to preempt than a 1443391e43daSPeter Zijlstra * remote processor. 1444391e43daSPeter Zijlstra */ 1445391e43daSPeter Zijlstra if (this_cpu != -1 && 1446391e43daSPeter Zijlstra cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { 1447391e43daSPeter Zijlstra rcu_read_unlock(); 1448391e43daSPeter Zijlstra return this_cpu; 1449391e43daSPeter Zijlstra } 1450391e43daSPeter Zijlstra 1451391e43daSPeter Zijlstra best_cpu = cpumask_first_and(lowest_mask, 1452391e43daSPeter Zijlstra sched_domain_span(sd)); 1453391e43daSPeter Zijlstra if (best_cpu < nr_cpu_ids) { 1454391e43daSPeter Zijlstra rcu_read_unlock(); 1455391e43daSPeter Zijlstra return best_cpu; 1456391e43daSPeter Zijlstra } 1457391e43daSPeter Zijlstra } 1458391e43daSPeter Zijlstra } 1459391e43daSPeter Zijlstra rcu_read_unlock(); 1460391e43daSPeter Zijlstra 1461391e43daSPeter Zijlstra /* 1462391e43daSPeter Zijlstra * And finally, if there were no matches within the domains 1463391e43daSPeter Zijlstra * just give the caller *something* to work with from the compatible 1464391e43daSPeter Zijlstra * locations. 1465391e43daSPeter Zijlstra */ 1466391e43daSPeter Zijlstra if (this_cpu != -1) 1467391e43daSPeter Zijlstra return this_cpu; 1468391e43daSPeter Zijlstra 1469391e43daSPeter Zijlstra cpu = cpumask_any(lowest_mask); 1470391e43daSPeter Zijlstra if (cpu < nr_cpu_ids) 1471391e43daSPeter Zijlstra return cpu; 1472391e43daSPeter Zijlstra return -1; 1473391e43daSPeter Zijlstra } 1474391e43daSPeter Zijlstra 1475391e43daSPeter Zijlstra /* Will lock the rq it finds */ 1476391e43daSPeter Zijlstra static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq) 1477391e43daSPeter Zijlstra { 1478391e43daSPeter Zijlstra struct rq *lowest_rq = NULL; 1479391e43daSPeter Zijlstra int tries; 1480391e43daSPeter Zijlstra int cpu; 1481391e43daSPeter Zijlstra 1482391e43daSPeter Zijlstra for (tries = 0; tries < RT_MAX_TRIES; tries++) { 1483391e43daSPeter Zijlstra cpu = find_lowest_rq(task); 1484391e43daSPeter Zijlstra 1485391e43daSPeter Zijlstra if ((cpu == -1) || (cpu == rq->cpu)) 1486391e43daSPeter Zijlstra break; 1487391e43daSPeter Zijlstra 1488391e43daSPeter Zijlstra lowest_rq = cpu_rq(cpu); 1489391e43daSPeter Zijlstra 1490391e43daSPeter Zijlstra /* if the prio of this runqueue changed, try again */ 1491391e43daSPeter Zijlstra if (double_lock_balance(rq, lowest_rq)) { 1492391e43daSPeter Zijlstra /* 1493391e43daSPeter Zijlstra * We had to unlock the run queue. In 1494391e43daSPeter Zijlstra * the mean time, task could have 1495391e43daSPeter Zijlstra * migrated already or had its affinity changed. 1496391e43daSPeter Zijlstra * Also make sure that it wasn't scheduled on its rq. 1497391e43daSPeter Zijlstra */ 1498391e43daSPeter Zijlstra if (unlikely(task_rq(task) != rq || 1499391e43daSPeter Zijlstra !cpumask_test_cpu(lowest_rq->cpu, 1500391e43daSPeter Zijlstra tsk_cpus_allowed(task)) || 1501391e43daSPeter Zijlstra task_running(rq, task) || 1502391e43daSPeter Zijlstra !task->on_rq)) { 1503391e43daSPeter Zijlstra 15047f1b4393SPeter Zijlstra double_unlock_balance(rq, lowest_rq); 1505391e43daSPeter Zijlstra lowest_rq = NULL; 1506391e43daSPeter Zijlstra break; 1507391e43daSPeter Zijlstra } 1508391e43daSPeter Zijlstra } 1509391e43daSPeter Zijlstra 1510391e43daSPeter Zijlstra /* If this rq is still suitable use it. */ 1511391e43daSPeter Zijlstra if (lowest_rq->rt.highest_prio.curr > task->prio) 1512391e43daSPeter Zijlstra break; 1513391e43daSPeter Zijlstra 1514391e43daSPeter Zijlstra /* try again */ 1515391e43daSPeter Zijlstra double_unlock_balance(rq, lowest_rq); 1516391e43daSPeter Zijlstra lowest_rq = NULL; 1517391e43daSPeter Zijlstra } 1518391e43daSPeter Zijlstra 1519391e43daSPeter Zijlstra return lowest_rq; 1520391e43daSPeter Zijlstra } 1521391e43daSPeter Zijlstra 1522391e43daSPeter Zijlstra static struct task_struct *pick_next_pushable_task(struct rq *rq) 1523391e43daSPeter Zijlstra { 1524391e43daSPeter Zijlstra struct task_struct *p; 1525391e43daSPeter Zijlstra 1526391e43daSPeter Zijlstra if (!has_pushable_tasks(rq)) 1527391e43daSPeter Zijlstra return NULL; 1528391e43daSPeter Zijlstra 1529391e43daSPeter Zijlstra p = plist_first_entry(&rq->rt.pushable_tasks, 1530391e43daSPeter Zijlstra struct task_struct, pushable_tasks); 1531391e43daSPeter Zijlstra 1532391e43daSPeter Zijlstra BUG_ON(rq->cpu != task_cpu(p)); 1533391e43daSPeter Zijlstra BUG_ON(task_current(rq, p)); 153429baa747SPeter Zijlstra BUG_ON(p->nr_cpus_allowed <= 1); 1535391e43daSPeter Zijlstra 1536391e43daSPeter Zijlstra BUG_ON(!p->on_rq); 1537391e43daSPeter Zijlstra BUG_ON(!rt_task(p)); 1538391e43daSPeter Zijlstra 1539391e43daSPeter Zijlstra return p; 1540391e43daSPeter Zijlstra } 1541391e43daSPeter Zijlstra 1542391e43daSPeter Zijlstra /* 1543391e43daSPeter Zijlstra * If the current CPU has more than one RT task, see if the non 1544391e43daSPeter Zijlstra * running task can migrate over to a CPU that is running a task 1545391e43daSPeter Zijlstra * of lesser priority. 1546391e43daSPeter Zijlstra */ 1547391e43daSPeter Zijlstra static int push_rt_task(struct rq *rq) 1548391e43daSPeter Zijlstra { 1549391e43daSPeter Zijlstra struct task_struct *next_task; 1550391e43daSPeter Zijlstra struct rq *lowest_rq; 1551391e43daSPeter Zijlstra int ret = 0; 1552391e43daSPeter Zijlstra 1553391e43daSPeter Zijlstra if (!rq->rt.overloaded) 1554391e43daSPeter Zijlstra return 0; 1555391e43daSPeter Zijlstra 1556391e43daSPeter Zijlstra next_task = pick_next_pushable_task(rq); 1557391e43daSPeter Zijlstra if (!next_task) 1558391e43daSPeter Zijlstra return 0; 1559391e43daSPeter Zijlstra 1560391e43daSPeter Zijlstra retry: 1561391e43daSPeter Zijlstra if (unlikely(next_task == rq->curr)) { 1562391e43daSPeter Zijlstra WARN_ON(1); 1563391e43daSPeter Zijlstra return 0; 1564391e43daSPeter Zijlstra } 1565391e43daSPeter Zijlstra 1566391e43daSPeter Zijlstra /* 1567391e43daSPeter Zijlstra * It's possible that the next_task slipped in of 1568391e43daSPeter Zijlstra * higher priority than current. If that's the case 1569391e43daSPeter Zijlstra * just reschedule current. 1570391e43daSPeter Zijlstra */ 1571391e43daSPeter Zijlstra if (unlikely(next_task->prio < rq->curr->prio)) { 1572391e43daSPeter Zijlstra resched_task(rq->curr); 1573391e43daSPeter Zijlstra return 0; 1574391e43daSPeter Zijlstra } 1575391e43daSPeter Zijlstra 1576391e43daSPeter Zijlstra /* We might release rq lock */ 1577391e43daSPeter Zijlstra get_task_struct(next_task); 1578391e43daSPeter Zijlstra 1579391e43daSPeter Zijlstra /* find_lock_lowest_rq locks the rq if found */ 1580391e43daSPeter Zijlstra lowest_rq = find_lock_lowest_rq(next_task, rq); 1581391e43daSPeter Zijlstra if (!lowest_rq) { 1582391e43daSPeter Zijlstra struct task_struct *task; 1583391e43daSPeter Zijlstra /* 1584391e43daSPeter Zijlstra * find_lock_lowest_rq releases rq->lock 1585391e43daSPeter Zijlstra * so it is possible that next_task has migrated. 1586391e43daSPeter Zijlstra * 1587391e43daSPeter Zijlstra * We need to make sure that the task is still on the same 1588391e43daSPeter Zijlstra * run-queue and is also still the next task eligible for 1589391e43daSPeter Zijlstra * pushing. 1590391e43daSPeter Zijlstra */ 1591391e43daSPeter Zijlstra task = pick_next_pushable_task(rq); 1592391e43daSPeter Zijlstra if (task_cpu(next_task) == rq->cpu && task == next_task) { 1593391e43daSPeter Zijlstra /* 1594391e43daSPeter Zijlstra * The task hasn't migrated, and is still the next 1595391e43daSPeter Zijlstra * eligible task, but we failed to find a run-queue 1596391e43daSPeter Zijlstra * to push it to. Do not retry in this case, since 1597391e43daSPeter Zijlstra * other cpus will pull from us when ready. 1598391e43daSPeter Zijlstra */ 1599391e43daSPeter Zijlstra goto out; 1600391e43daSPeter Zijlstra } 1601391e43daSPeter Zijlstra 1602391e43daSPeter Zijlstra if (!task) 1603391e43daSPeter Zijlstra /* No more tasks, just exit */ 1604391e43daSPeter Zijlstra goto out; 1605391e43daSPeter Zijlstra 1606391e43daSPeter Zijlstra /* 1607391e43daSPeter Zijlstra * Something has shifted, try again. 1608391e43daSPeter Zijlstra */ 1609391e43daSPeter Zijlstra put_task_struct(next_task); 1610391e43daSPeter Zijlstra next_task = task; 1611391e43daSPeter Zijlstra goto retry; 1612391e43daSPeter Zijlstra } 1613391e43daSPeter Zijlstra 1614391e43daSPeter Zijlstra deactivate_task(rq, next_task, 0); 1615391e43daSPeter Zijlstra set_task_cpu(next_task, lowest_rq->cpu); 1616391e43daSPeter Zijlstra activate_task(lowest_rq, next_task, 0); 1617391e43daSPeter Zijlstra ret = 1; 1618391e43daSPeter Zijlstra 1619391e43daSPeter Zijlstra resched_task(lowest_rq->curr); 1620391e43daSPeter Zijlstra 1621391e43daSPeter Zijlstra double_unlock_balance(rq, lowest_rq); 1622391e43daSPeter Zijlstra 1623391e43daSPeter Zijlstra out: 1624391e43daSPeter Zijlstra put_task_struct(next_task); 1625391e43daSPeter Zijlstra 1626391e43daSPeter Zijlstra return ret; 1627391e43daSPeter Zijlstra } 1628391e43daSPeter Zijlstra 1629391e43daSPeter Zijlstra static void push_rt_tasks(struct rq *rq) 1630391e43daSPeter Zijlstra { 1631391e43daSPeter Zijlstra /* push_rt_task will return true if it moved an RT */ 1632391e43daSPeter Zijlstra while (push_rt_task(rq)) 1633391e43daSPeter Zijlstra ; 1634391e43daSPeter Zijlstra } 1635391e43daSPeter Zijlstra 1636391e43daSPeter Zijlstra static int pull_rt_task(struct rq *this_rq) 1637391e43daSPeter Zijlstra { 1638391e43daSPeter Zijlstra int this_cpu = this_rq->cpu, ret = 0, cpu; 1639391e43daSPeter Zijlstra struct task_struct *p; 1640391e43daSPeter Zijlstra struct rq *src_rq; 1641391e43daSPeter Zijlstra 1642391e43daSPeter Zijlstra if (likely(!rt_overloaded(this_rq))) 1643391e43daSPeter Zijlstra return 0; 1644391e43daSPeter Zijlstra 16457c3f2ab7SPeter Zijlstra /* 16467c3f2ab7SPeter Zijlstra * Match the barrier from rt_set_overloaded; this guarantees that if we 16477c3f2ab7SPeter Zijlstra * see overloaded we must also see the rto_mask bit. 16487c3f2ab7SPeter Zijlstra */ 16497c3f2ab7SPeter Zijlstra smp_rmb(); 16507c3f2ab7SPeter Zijlstra 1651391e43daSPeter Zijlstra for_each_cpu(cpu, this_rq->rd->rto_mask) { 1652391e43daSPeter Zijlstra if (this_cpu == cpu) 1653391e43daSPeter Zijlstra continue; 1654391e43daSPeter Zijlstra 1655391e43daSPeter Zijlstra src_rq = cpu_rq(cpu); 1656391e43daSPeter Zijlstra 1657391e43daSPeter Zijlstra /* 1658391e43daSPeter Zijlstra * Don't bother taking the src_rq->lock if the next highest 1659391e43daSPeter Zijlstra * task is known to be lower-priority than our current task. 1660391e43daSPeter Zijlstra * This may look racy, but if this value is about to go 1661391e43daSPeter Zijlstra * logically higher, the src_rq will push this task away. 1662391e43daSPeter Zijlstra * And if its going logically lower, we do not care 1663391e43daSPeter Zijlstra */ 1664391e43daSPeter Zijlstra if (src_rq->rt.highest_prio.next >= 1665391e43daSPeter Zijlstra this_rq->rt.highest_prio.curr) 1666391e43daSPeter Zijlstra continue; 1667391e43daSPeter Zijlstra 1668391e43daSPeter Zijlstra /* 1669391e43daSPeter Zijlstra * We can potentially drop this_rq's lock in 1670391e43daSPeter Zijlstra * double_lock_balance, and another CPU could 1671391e43daSPeter Zijlstra * alter this_rq 1672391e43daSPeter Zijlstra */ 1673391e43daSPeter Zijlstra double_lock_balance(this_rq, src_rq); 1674391e43daSPeter Zijlstra 1675391e43daSPeter Zijlstra /* 1676e23ee747SKirill Tkhai * We can pull only a task, which is pushable 1677e23ee747SKirill Tkhai * on its rq, and no others. 1678391e43daSPeter Zijlstra */ 1679e23ee747SKirill Tkhai p = pick_highest_pushable_task(src_rq, this_cpu); 1680391e43daSPeter Zijlstra 1681391e43daSPeter Zijlstra /* 1682391e43daSPeter Zijlstra * Do we have an RT task that preempts 1683391e43daSPeter Zijlstra * the to-be-scheduled task? 1684391e43daSPeter Zijlstra */ 1685391e43daSPeter Zijlstra if (p && (p->prio < this_rq->rt.highest_prio.curr)) { 1686391e43daSPeter Zijlstra WARN_ON(p == src_rq->curr); 1687391e43daSPeter Zijlstra WARN_ON(!p->on_rq); 1688391e43daSPeter Zijlstra 1689391e43daSPeter Zijlstra /* 1690391e43daSPeter Zijlstra * There's a chance that p is higher in priority 1691391e43daSPeter Zijlstra * than what's currently running on its cpu. 1692391e43daSPeter Zijlstra * This is just that p is wakeing up and hasn't 1693391e43daSPeter Zijlstra * had a chance to schedule. We only pull 1694391e43daSPeter Zijlstra * p if it is lower in priority than the 1695391e43daSPeter Zijlstra * current task on the run queue 1696391e43daSPeter Zijlstra */ 1697391e43daSPeter Zijlstra if (p->prio < src_rq->curr->prio) 1698391e43daSPeter Zijlstra goto skip; 1699391e43daSPeter Zijlstra 1700391e43daSPeter Zijlstra ret = 1; 1701391e43daSPeter Zijlstra 1702391e43daSPeter Zijlstra deactivate_task(src_rq, p, 0); 1703391e43daSPeter Zijlstra set_task_cpu(p, this_cpu); 1704391e43daSPeter Zijlstra activate_task(this_rq, p, 0); 1705391e43daSPeter Zijlstra /* 1706391e43daSPeter Zijlstra * We continue with the search, just in 1707391e43daSPeter Zijlstra * case there's an even higher prio task 1708391e43daSPeter Zijlstra * in another runqueue. (low likelihood 1709391e43daSPeter Zijlstra * but possible) 1710391e43daSPeter Zijlstra */ 1711391e43daSPeter Zijlstra } 1712391e43daSPeter Zijlstra skip: 1713391e43daSPeter Zijlstra double_unlock_balance(this_rq, src_rq); 1714391e43daSPeter Zijlstra } 1715391e43daSPeter Zijlstra 1716391e43daSPeter Zijlstra return ret; 1717391e43daSPeter Zijlstra } 1718391e43daSPeter Zijlstra 1719391e43daSPeter Zijlstra static void pre_schedule_rt(struct rq *rq, struct task_struct *prev) 1720391e43daSPeter Zijlstra { 1721391e43daSPeter Zijlstra /* Try to pull RT tasks here if we lower this rq's prio */ 1722391e43daSPeter Zijlstra if (rq->rt.highest_prio.curr > prev->prio) 1723391e43daSPeter Zijlstra pull_rt_task(rq); 1724391e43daSPeter Zijlstra } 1725391e43daSPeter Zijlstra 1726391e43daSPeter Zijlstra static void post_schedule_rt(struct rq *rq) 1727391e43daSPeter Zijlstra { 1728391e43daSPeter Zijlstra push_rt_tasks(rq); 1729391e43daSPeter Zijlstra } 1730391e43daSPeter Zijlstra 1731391e43daSPeter Zijlstra /* 1732391e43daSPeter Zijlstra * If we are not running and we are not going to reschedule soon, we should 1733391e43daSPeter Zijlstra * try to push tasks away now 1734391e43daSPeter Zijlstra */ 1735391e43daSPeter Zijlstra static void task_woken_rt(struct rq *rq, struct task_struct *p) 1736391e43daSPeter Zijlstra { 1737391e43daSPeter Zijlstra if (!task_running(rq, p) && 1738391e43daSPeter Zijlstra !test_tsk_need_resched(rq->curr) && 1739391e43daSPeter Zijlstra has_pushable_tasks(rq) && 174029baa747SPeter Zijlstra p->nr_cpus_allowed > 1 && 1741391e43daSPeter Zijlstra rt_task(rq->curr) && 174229baa747SPeter Zijlstra (rq->curr->nr_cpus_allowed < 2 || 1743391e43daSPeter Zijlstra rq->curr->prio <= p->prio)) 1744391e43daSPeter Zijlstra push_rt_tasks(rq); 1745391e43daSPeter Zijlstra } 1746391e43daSPeter Zijlstra 1747391e43daSPeter Zijlstra static void set_cpus_allowed_rt(struct task_struct *p, 1748391e43daSPeter Zijlstra const struct cpumask *new_mask) 1749391e43daSPeter Zijlstra { 17508d3d5adaSKirill Tkhai struct rq *rq; 17518d3d5adaSKirill Tkhai int weight; 1752391e43daSPeter Zijlstra 1753391e43daSPeter Zijlstra BUG_ON(!rt_task(p)); 1754391e43daSPeter Zijlstra 17558d3d5adaSKirill Tkhai if (!p->on_rq) 17568d3d5adaSKirill Tkhai return; 1757391e43daSPeter Zijlstra 17588d3d5adaSKirill Tkhai weight = cpumask_weight(new_mask); 17598d3d5adaSKirill Tkhai 1760391e43daSPeter Zijlstra /* 17618d3d5adaSKirill Tkhai * Only update if the process changes its state from whether it 17628d3d5adaSKirill Tkhai * can migrate or not. 1763391e43daSPeter Zijlstra */ 176429baa747SPeter Zijlstra if ((p->nr_cpus_allowed > 1) == (weight > 1)) 17658d3d5adaSKirill Tkhai return; 17668d3d5adaSKirill Tkhai 17678d3d5adaSKirill Tkhai rq = task_rq(p); 17688d3d5adaSKirill Tkhai 17698d3d5adaSKirill Tkhai /* 17708d3d5adaSKirill Tkhai * The process used to be able to migrate OR it can now migrate 17718d3d5adaSKirill Tkhai */ 17728d3d5adaSKirill Tkhai if (weight <= 1) { 17738d3d5adaSKirill Tkhai if (!task_current(rq, p)) 1774391e43daSPeter Zijlstra dequeue_pushable_task(rq, p); 1775391e43daSPeter Zijlstra BUG_ON(!rq->rt.rt_nr_migratory); 1776391e43daSPeter Zijlstra rq->rt.rt_nr_migratory--; 17778d3d5adaSKirill Tkhai } else { 17788d3d5adaSKirill Tkhai if (!task_current(rq, p)) 17798d3d5adaSKirill Tkhai enqueue_pushable_task(rq, p); 17808d3d5adaSKirill Tkhai rq->rt.rt_nr_migratory++; 1781391e43daSPeter Zijlstra } 1782391e43daSPeter Zijlstra 1783391e43daSPeter Zijlstra update_rt_migration(&rq->rt); 1784391e43daSPeter Zijlstra } 1785391e43daSPeter Zijlstra 1786391e43daSPeter Zijlstra /* Assumes rq->lock is held */ 1787391e43daSPeter Zijlstra static void rq_online_rt(struct rq *rq) 1788391e43daSPeter Zijlstra { 1789391e43daSPeter Zijlstra if (rq->rt.overloaded) 1790391e43daSPeter Zijlstra rt_set_overload(rq); 1791391e43daSPeter Zijlstra 1792391e43daSPeter Zijlstra __enable_runtime(rq); 1793391e43daSPeter Zijlstra 1794391e43daSPeter Zijlstra cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr); 1795391e43daSPeter Zijlstra } 1796391e43daSPeter Zijlstra 1797391e43daSPeter Zijlstra /* Assumes rq->lock is held */ 1798391e43daSPeter Zijlstra static void rq_offline_rt(struct rq *rq) 1799391e43daSPeter Zijlstra { 1800391e43daSPeter Zijlstra if (rq->rt.overloaded) 1801391e43daSPeter Zijlstra rt_clear_overload(rq); 1802391e43daSPeter Zijlstra 1803391e43daSPeter Zijlstra __disable_runtime(rq); 1804391e43daSPeter Zijlstra 1805391e43daSPeter Zijlstra cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID); 1806391e43daSPeter Zijlstra } 1807391e43daSPeter Zijlstra 1808391e43daSPeter Zijlstra /* 1809391e43daSPeter Zijlstra * When switch from the rt queue, we bring ourselves to a position 1810391e43daSPeter Zijlstra * that we might want to pull RT tasks from other runqueues. 1811391e43daSPeter Zijlstra */ 1812391e43daSPeter Zijlstra static void switched_from_rt(struct rq *rq, struct task_struct *p) 1813391e43daSPeter Zijlstra { 1814391e43daSPeter Zijlstra /* 1815391e43daSPeter Zijlstra * If there are other RT tasks then we will reschedule 1816391e43daSPeter Zijlstra * and the scheduling of the other RT tasks will handle 1817391e43daSPeter Zijlstra * the balancing. But if we are the last RT task 1818391e43daSPeter Zijlstra * we may need to handle the pulling of RT tasks 1819391e43daSPeter Zijlstra * now. 1820391e43daSPeter Zijlstra */ 18211158ddb5SKirill Tkhai if (!p->on_rq || rq->rt.rt_nr_running) 18221158ddb5SKirill Tkhai return; 18231158ddb5SKirill Tkhai 18241158ddb5SKirill Tkhai if (pull_rt_task(rq)) 18251158ddb5SKirill Tkhai resched_task(rq->curr); 1826391e43daSPeter Zijlstra } 1827391e43daSPeter Zijlstra 1828391e43daSPeter Zijlstra void init_sched_rt_class(void) 1829391e43daSPeter Zijlstra { 1830391e43daSPeter Zijlstra unsigned int i; 1831391e43daSPeter Zijlstra 1832391e43daSPeter Zijlstra for_each_possible_cpu(i) { 1833391e43daSPeter Zijlstra zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i), 1834391e43daSPeter Zijlstra GFP_KERNEL, cpu_to_node(i)); 1835391e43daSPeter Zijlstra } 1836391e43daSPeter Zijlstra } 1837391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 1838391e43daSPeter Zijlstra 1839391e43daSPeter Zijlstra /* 1840391e43daSPeter Zijlstra * When switching a task to RT, we may overload the runqueue 1841391e43daSPeter Zijlstra * with RT tasks. In this case we try to push them off to 1842391e43daSPeter Zijlstra * other runqueues. 1843391e43daSPeter Zijlstra */ 1844391e43daSPeter Zijlstra static void switched_to_rt(struct rq *rq, struct task_struct *p) 1845391e43daSPeter Zijlstra { 1846391e43daSPeter Zijlstra int check_resched = 1; 1847391e43daSPeter Zijlstra 1848391e43daSPeter Zijlstra /* 1849391e43daSPeter Zijlstra * If we are already running, then there's nothing 1850391e43daSPeter Zijlstra * that needs to be done. But if we are not running 1851391e43daSPeter Zijlstra * we may need to preempt the current running task. 1852391e43daSPeter Zijlstra * If that current running task is also an RT task 1853391e43daSPeter Zijlstra * then see if we can move to another run queue. 1854391e43daSPeter Zijlstra */ 1855391e43daSPeter Zijlstra if (p->on_rq && rq->curr != p) { 1856391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1857391e43daSPeter Zijlstra if (rq->rt.overloaded && push_rt_task(rq) && 1858391e43daSPeter Zijlstra /* Don't resched if we changed runqueues */ 1859391e43daSPeter Zijlstra rq != task_rq(p)) 1860391e43daSPeter Zijlstra check_resched = 0; 1861391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 1862391e43daSPeter Zijlstra if (check_resched && p->prio < rq->curr->prio) 1863391e43daSPeter Zijlstra resched_task(rq->curr); 1864391e43daSPeter Zijlstra } 1865391e43daSPeter Zijlstra } 1866391e43daSPeter Zijlstra 1867391e43daSPeter Zijlstra /* 1868391e43daSPeter Zijlstra * Priority of the task has changed. This may cause 1869391e43daSPeter Zijlstra * us to initiate a push or pull. 1870391e43daSPeter Zijlstra */ 1871391e43daSPeter Zijlstra static void 1872391e43daSPeter Zijlstra prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio) 1873391e43daSPeter Zijlstra { 1874391e43daSPeter Zijlstra if (!p->on_rq) 1875391e43daSPeter Zijlstra return; 1876391e43daSPeter Zijlstra 1877391e43daSPeter Zijlstra if (rq->curr == p) { 1878391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1879391e43daSPeter Zijlstra /* 1880391e43daSPeter Zijlstra * If our priority decreases while running, we 1881391e43daSPeter Zijlstra * may need to pull tasks to this runqueue. 1882391e43daSPeter Zijlstra */ 1883391e43daSPeter Zijlstra if (oldprio < p->prio) 1884391e43daSPeter Zijlstra pull_rt_task(rq); 1885391e43daSPeter Zijlstra /* 1886391e43daSPeter Zijlstra * If there's a higher priority task waiting to run 1887391e43daSPeter Zijlstra * then reschedule. Note, the above pull_rt_task 1888391e43daSPeter Zijlstra * can release the rq lock and p could migrate. 1889391e43daSPeter Zijlstra * Only reschedule if p is still on the same runqueue. 1890391e43daSPeter Zijlstra */ 1891391e43daSPeter Zijlstra if (p->prio > rq->rt.highest_prio.curr && rq->curr == p) 1892391e43daSPeter Zijlstra resched_task(p); 1893391e43daSPeter Zijlstra #else 1894391e43daSPeter Zijlstra /* For UP simply resched on drop of prio */ 1895391e43daSPeter Zijlstra if (oldprio < p->prio) 1896391e43daSPeter Zijlstra resched_task(p); 1897391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 1898391e43daSPeter Zijlstra } else { 1899391e43daSPeter Zijlstra /* 1900391e43daSPeter Zijlstra * This task is not running, but if it is 1901391e43daSPeter Zijlstra * greater than the current running task 1902391e43daSPeter Zijlstra * then reschedule. 1903391e43daSPeter Zijlstra */ 1904391e43daSPeter Zijlstra if (p->prio < rq->curr->prio) 1905391e43daSPeter Zijlstra resched_task(rq->curr); 1906391e43daSPeter Zijlstra } 1907391e43daSPeter Zijlstra } 1908391e43daSPeter Zijlstra 1909391e43daSPeter Zijlstra static void watchdog(struct rq *rq, struct task_struct *p) 1910391e43daSPeter Zijlstra { 1911391e43daSPeter Zijlstra unsigned long soft, hard; 1912391e43daSPeter Zijlstra 1913391e43daSPeter Zijlstra /* max may change after cur was read, this will be fixed next tick */ 1914391e43daSPeter Zijlstra soft = task_rlimit(p, RLIMIT_RTTIME); 1915391e43daSPeter Zijlstra hard = task_rlimit_max(p, RLIMIT_RTTIME); 1916391e43daSPeter Zijlstra 1917391e43daSPeter Zijlstra if (soft != RLIM_INFINITY) { 1918391e43daSPeter Zijlstra unsigned long next; 1919391e43daSPeter Zijlstra 192057d2aa00SYing Xue if (p->rt.watchdog_stamp != jiffies) { 1921391e43daSPeter Zijlstra p->rt.timeout++; 192257d2aa00SYing Xue p->rt.watchdog_stamp = jiffies; 192357d2aa00SYing Xue } 192457d2aa00SYing Xue 1925391e43daSPeter Zijlstra next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ); 1926391e43daSPeter Zijlstra if (p->rt.timeout > next) 1927391e43daSPeter Zijlstra p->cputime_expires.sched_exp = p->se.sum_exec_runtime; 1928391e43daSPeter Zijlstra } 1929391e43daSPeter Zijlstra } 1930391e43daSPeter Zijlstra 1931391e43daSPeter Zijlstra static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued) 1932391e43daSPeter Zijlstra { 1933454c7999SColin Cross struct sched_rt_entity *rt_se = &p->rt; 1934454c7999SColin Cross 1935391e43daSPeter Zijlstra update_curr_rt(rq); 1936391e43daSPeter Zijlstra 1937391e43daSPeter Zijlstra watchdog(rq, p); 1938391e43daSPeter Zijlstra 1939391e43daSPeter Zijlstra /* 1940391e43daSPeter Zijlstra * RR tasks need a special form of timeslice management. 1941391e43daSPeter Zijlstra * FIFO tasks have no timeslices. 1942391e43daSPeter Zijlstra */ 1943391e43daSPeter Zijlstra if (p->policy != SCHED_RR) 1944391e43daSPeter Zijlstra return; 1945391e43daSPeter Zijlstra 1946391e43daSPeter Zijlstra if (--p->rt.time_slice) 1947391e43daSPeter Zijlstra return; 1948391e43daSPeter Zijlstra 1949ce0dbbbbSClark Williams p->rt.time_slice = sched_rr_timeslice; 1950391e43daSPeter Zijlstra 1951391e43daSPeter Zijlstra /* 1952e9aa39bbSLi Bin * Requeue to the end of queue if we (and all of our ancestors) are not 1953e9aa39bbSLi Bin * the only element on the queue 1954391e43daSPeter Zijlstra */ 1955454c7999SColin Cross for_each_sched_rt_entity(rt_se) { 1956454c7999SColin Cross if (rt_se->run_list.prev != rt_se->run_list.next) { 1957391e43daSPeter Zijlstra requeue_task_rt(rq, p, 0); 1958391e43daSPeter Zijlstra set_tsk_need_resched(p); 1959454c7999SColin Cross return; 1960454c7999SColin Cross } 1961391e43daSPeter Zijlstra } 1962391e43daSPeter Zijlstra } 1963391e43daSPeter Zijlstra 1964391e43daSPeter Zijlstra static void set_curr_task_rt(struct rq *rq) 1965391e43daSPeter Zijlstra { 1966391e43daSPeter Zijlstra struct task_struct *p = rq->curr; 1967391e43daSPeter Zijlstra 196878becc27SFrederic Weisbecker p->se.exec_start = rq_clock_task(rq); 1969391e43daSPeter Zijlstra 1970391e43daSPeter Zijlstra /* The running task is never eligible for pushing */ 1971391e43daSPeter Zijlstra dequeue_pushable_task(rq, p); 1972391e43daSPeter Zijlstra } 1973391e43daSPeter Zijlstra 1974391e43daSPeter Zijlstra static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task) 1975391e43daSPeter Zijlstra { 1976391e43daSPeter Zijlstra /* 1977391e43daSPeter Zijlstra * Time slice is 0 for SCHED_FIFO tasks 1978391e43daSPeter Zijlstra */ 1979391e43daSPeter Zijlstra if (task->policy == SCHED_RR) 1980ce0dbbbbSClark Williams return sched_rr_timeslice; 1981391e43daSPeter Zijlstra else 1982391e43daSPeter Zijlstra return 0; 1983391e43daSPeter Zijlstra } 1984391e43daSPeter Zijlstra 1985391e43daSPeter Zijlstra const struct sched_class rt_sched_class = { 1986391e43daSPeter Zijlstra .next = &fair_sched_class, 1987391e43daSPeter Zijlstra .enqueue_task = enqueue_task_rt, 1988391e43daSPeter Zijlstra .dequeue_task = dequeue_task_rt, 1989391e43daSPeter Zijlstra .yield_task = yield_task_rt, 1990391e43daSPeter Zijlstra 1991391e43daSPeter Zijlstra .check_preempt_curr = check_preempt_curr_rt, 1992391e43daSPeter Zijlstra 1993391e43daSPeter Zijlstra .pick_next_task = pick_next_task_rt, 1994391e43daSPeter Zijlstra .put_prev_task = put_prev_task_rt, 1995391e43daSPeter Zijlstra 1996391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1997391e43daSPeter Zijlstra .select_task_rq = select_task_rq_rt, 1998391e43daSPeter Zijlstra 1999391e43daSPeter Zijlstra .set_cpus_allowed = set_cpus_allowed_rt, 2000391e43daSPeter Zijlstra .rq_online = rq_online_rt, 2001391e43daSPeter Zijlstra .rq_offline = rq_offline_rt, 2002391e43daSPeter Zijlstra .pre_schedule = pre_schedule_rt, 2003391e43daSPeter Zijlstra .post_schedule = post_schedule_rt, 2004391e43daSPeter Zijlstra .task_woken = task_woken_rt, 2005391e43daSPeter Zijlstra .switched_from = switched_from_rt, 2006391e43daSPeter Zijlstra #endif 2007391e43daSPeter Zijlstra 2008391e43daSPeter Zijlstra .set_curr_task = set_curr_task_rt, 2009391e43daSPeter Zijlstra .task_tick = task_tick_rt, 2010391e43daSPeter Zijlstra 2011391e43daSPeter Zijlstra .get_rr_interval = get_rr_interval_rt, 2012391e43daSPeter Zijlstra 2013391e43daSPeter Zijlstra .prio_changed = prio_changed_rt, 2014391e43daSPeter Zijlstra .switched_to = switched_to_rt, 2015391e43daSPeter Zijlstra }; 2016391e43daSPeter Zijlstra 2017391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 2018391e43daSPeter Zijlstra extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq); 2019391e43daSPeter Zijlstra 2020391e43daSPeter Zijlstra void print_rt_stats(struct seq_file *m, int cpu) 2021391e43daSPeter Zijlstra { 2022391e43daSPeter Zijlstra rt_rq_iter_t iter; 2023391e43daSPeter Zijlstra struct rt_rq *rt_rq; 2024391e43daSPeter Zijlstra 2025391e43daSPeter Zijlstra rcu_read_lock(); 2026391e43daSPeter Zijlstra for_each_rt_rq(rt_rq, iter, cpu_rq(cpu)) 2027391e43daSPeter Zijlstra print_rt_rq(m, cpu, rt_rq); 2028391e43daSPeter Zijlstra rcu_read_unlock(); 2029391e43daSPeter Zijlstra } 2030391e43daSPeter Zijlstra #endif /* CONFIG_SCHED_DEBUG */ 2031