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> 9b6366f04SSteven Rostedt #include <linux/irq_work.h> 10391e43daSPeter Zijlstra 11ce0dbbbbSClark Williams int sched_rr_timeslice = RR_TIMESLICE; 12*975e155eSShile Zhang int sysctl_sched_rr_timeslice = (MSEC_PER_SEC / HZ) * RR_TIMESLICE; 13ce0dbbbbSClark Williams 14391e43daSPeter Zijlstra static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); 15391e43daSPeter Zijlstra 16391e43daSPeter Zijlstra struct rt_bandwidth def_rt_bandwidth; 17391e43daSPeter Zijlstra 18391e43daSPeter Zijlstra static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) 19391e43daSPeter Zijlstra { 20391e43daSPeter Zijlstra struct rt_bandwidth *rt_b = 21391e43daSPeter Zijlstra container_of(timer, struct rt_bandwidth, rt_period_timer); 22391e43daSPeter Zijlstra int idle = 0; 2377a4d1a1SPeter Zijlstra int overrun; 24391e43daSPeter Zijlstra 2577a4d1a1SPeter Zijlstra raw_spin_lock(&rt_b->rt_runtime_lock); 26391e43daSPeter Zijlstra for (;;) { 2777a4d1a1SPeter Zijlstra overrun = hrtimer_forward_now(timer, rt_b->rt_period); 28391e43daSPeter Zijlstra if (!overrun) 29391e43daSPeter Zijlstra break; 30391e43daSPeter Zijlstra 3177a4d1a1SPeter Zijlstra raw_spin_unlock(&rt_b->rt_runtime_lock); 32391e43daSPeter Zijlstra idle = do_sched_rt_period_timer(rt_b, overrun); 3377a4d1a1SPeter Zijlstra raw_spin_lock(&rt_b->rt_runtime_lock); 34391e43daSPeter Zijlstra } 354cfafd30SPeter Zijlstra if (idle) 364cfafd30SPeter Zijlstra rt_b->rt_period_active = 0; 3777a4d1a1SPeter Zijlstra raw_spin_unlock(&rt_b->rt_runtime_lock); 38391e43daSPeter Zijlstra 39391e43daSPeter Zijlstra return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; 40391e43daSPeter Zijlstra } 41391e43daSPeter Zijlstra 42391e43daSPeter Zijlstra void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) 43391e43daSPeter Zijlstra { 44391e43daSPeter Zijlstra rt_b->rt_period = ns_to_ktime(period); 45391e43daSPeter Zijlstra rt_b->rt_runtime = runtime; 46391e43daSPeter Zijlstra 47391e43daSPeter Zijlstra raw_spin_lock_init(&rt_b->rt_runtime_lock); 48391e43daSPeter Zijlstra 49391e43daSPeter Zijlstra hrtimer_init(&rt_b->rt_period_timer, 50391e43daSPeter Zijlstra CLOCK_MONOTONIC, HRTIMER_MODE_REL); 51391e43daSPeter Zijlstra rt_b->rt_period_timer.function = sched_rt_period_timer; 52391e43daSPeter Zijlstra } 53391e43daSPeter Zijlstra 54391e43daSPeter Zijlstra static void start_rt_bandwidth(struct rt_bandwidth *rt_b) 55391e43daSPeter Zijlstra { 56391e43daSPeter Zijlstra if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) 57391e43daSPeter Zijlstra return; 58391e43daSPeter Zijlstra 59391e43daSPeter Zijlstra raw_spin_lock(&rt_b->rt_runtime_lock); 604cfafd30SPeter Zijlstra if (!rt_b->rt_period_active) { 614cfafd30SPeter Zijlstra rt_b->rt_period_active = 1; 62c3a990dcSSteven Rostedt /* 63c3a990dcSSteven Rostedt * SCHED_DEADLINE updates the bandwidth, as a run away 64c3a990dcSSteven Rostedt * RT task with a DL task could hog a CPU. But DL does 65c3a990dcSSteven Rostedt * not reset the period. If a deadline task was running 66c3a990dcSSteven Rostedt * without an RT task running, it can cause RT tasks to 67c3a990dcSSteven Rostedt * throttle when they start up. Kick the timer right away 68c3a990dcSSteven Rostedt * to update the period. 69c3a990dcSSteven Rostedt */ 70c3a990dcSSteven Rostedt hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0)); 714cfafd30SPeter Zijlstra hrtimer_start_expires(&rt_b->rt_period_timer, HRTIMER_MODE_ABS_PINNED); 724cfafd30SPeter Zijlstra } 73391e43daSPeter Zijlstra raw_spin_unlock(&rt_b->rt_runtime_lock); 74391e43daSPeter Zijlstra } 75391e43daSPeter Zijlstra 7689b41108SArnd Bergmann #if defined(CONFIG_SMP) && defined(HAVE_RT_PUSH_IPI) 77b6366f04SSteven Rostedt static void push_irq_work_func(struct irq_work *work); 78b6366f04SSteven Rostedt #endif 79b6366f04SSteven Rostedt 8007c54f7aSAbel Vesa void init_rt_rq(struct rt_rq *rt_rq) 81391e43daSPeter Zijlstra { 82391e43daSPeter Zijlstra struct rt_prio_array *array; 83391e43daSPeter Zijlstra int i; 84391e43daSPeter Zijlstra 85391e43daSPeter Zijlstra array = &rt_rq->active; 86391e43daSPeter Zijlstra for (i = 0; i < MAX_RT_PRIO; i++) { 87391e43daSPeter Zijlstra INIT_LIST_HEAD(array->queue + i); 88391e43daSPeter Zijlstra __clear_bit(i, array->bitmap); 89391e43daSPeter Zijlstra } 90391e43daSPeter Zijlstra /* delimiter for bitsearch: */ 91391e43daSPeter Zijlstra __set_bit(MAX_RT_PRIO, array->bitmap); 92391e43daSPeter Zijlstra 93391e43daSPeter Zijlstra #if defined CONFIG_SMP 94391e43daSPeter Zijlstra rt_rq->highest_prio.curr = MAX_RT_PRIO; 95391e43daSPeter Zijlstra rt_rq->highest_prio.next = MAX_RT_PRIO; 96391e43daSPeter Zijlstra rt_rq->rt_nr_migratory = 0; 97391e43daSPeter Zijlstra rt_rq->overloaded = 0; 98391e43daSPeter Zijlstra plist_head_init(&rt_rq->pushable_tasks); 99b6366f04SSteven Rostedt 100b6366f04SSteven Rostedt #ifdef HAVE_RT_PUSH_IPI 101b6366f04SSteven Rostedt rt_rq->push_flags = 0; 102b6366f04SSteven Rostedt rt_rq->push_cpu = nr_cpu_ids; 103b6366f04SSteven Rostedt raw_spin_lock_init(&rt_rq->push_lock); 104b6366f04SSteven Rostedt init_irq_work(&rt_rq->push_work, push_irq_work_func); 105391e43daSPeter Zijlstra #endif 106b6366f04SSteven Rostedt #endif /* CONFIG_SMP */ 107f4ebcbc0SKirill Tkhai /* We start is dequeued state, because no RT tasks are queued */ 108f4ebcbc0SKirill Tkhai rt_rq->rt_queued = 0; 109391e43daSPeter Zijlstra 110391e43daSPeter Zijlstra rt_rq->rt_time = 0; 111391e43daSPeter Zijlstra rt_rq->rt_throttled = 0; 112391e43daSPeter Zijlstra rt_rq->rt_runtime = 0; 113391e43daSPeter Zijlstra raw_spin_lock_init(&rt_rq->rt_runtime_lock); 114391e43daSPeter Zijlstra } 115391e43daSPeter Zijlstra 116391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 117391e43daSPeter Zijlstra static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) 118391e43daSPeter Zijlstra { 119391e43daSPeter Zijlstra hrtimer_cancel(&rt_b->rt_period_timer); 120391e43daSPeter Zijlstra } 121391e43daSPeter Zijlstra 122391e43daSPeter Zijlstra #define rt_entity_is_task(rt_se) (!(rt_se)->my_q) 123391e43daSPeter Zijlstra 124391e43daSPeter Zijlstra static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) 125391e43daSPeter Zijlstra { 126391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 127391e43daSPeter Zijlstra WARN_ON_ONCE(!rt_entity_is_task(rt_se)); 128391e43daSPeter Zijlstra #endif 129391e43daSPeter Zijlstra return container_of(rt_se, struct task_struct, rt); 130391e43daSPeter Zijlstra } 131391e43daSPeter Zijlstra 132391e43daSPeter Zijlstra static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) 133391e43daSPeter Zijlstra { 134391e43daSPeter Zijlstra return rt_rq->rq; 135391e43daSPeter Zijlstra } 136391e43daSPeter Zijlstra 137391e43daSPeter Zijlstra static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) 138391e43daSPeter Zijlstra { 139391e43daSPeter Zijlstra return rt_se->rt_rq; 140391e43daSPeter Zijlstra } 141391e43daSPeter Zijlstra 142653d07a6SKirill Tkhai static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se) 143653d07a6SKirill Tkhai { 144653d07a6SKirill Tkhai struct rt_rq *rt_rq = rt_se->rt_rq; 145653d07a6SKirill Tkhai 146653d07a6SKirill Tkhai return rt_rq->rq; 147653d07a6SKirill Tkhai } 148653d07a6SKirill Tkhai 149391e43daSPeter Zijlstra void free_rt_sched_group(struct task_group *tg) 150391e43daSPeter Zijlstra { 151391e43daSPeter Zijlstra int i; 152391e43daSPeter Zijlstra 153391e43daSPeter Zijlstra if (tg->rt_se) 154391e43daSPeter Zijlstra destroy_rt_bandwidth(&tg->rt_bandwidth); 155391e43daSPeter Zijlstra 156391e43daSPeter Zijlstra for_each_possible_cpu(i) { 157391e43daSPeter Zijlstra if (tg->rt_rq) 158391e43daSPeter Zijlstra kfree(tg->rt_rq[i]); 159391e43daSPeter Zijlstra if (tg->rt_se) 160391e43daSPeter Zijlstra kfree(tg->rt_se[i]); 161391e43daSPeter Zijlstra } 162391e43daSPeter Zijlstra 163391e43daSPeter Zijlstra kfree(tg->rt_rq); 164391e43daSPeter Zijlstra kfree(tg->rt_se); 165391e43daSPeter Zijlstra } 166391e43daSPeter Zijlstra 167391e43daSPeter Zijlstra void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, 168391e43daSPeter Zijlstra struct sched_rt_entity *rt_se, int cpu, 169391e43daSPeter Zijlstra struct sched_rt_entity *parent) 170391e43daSPeter Zijlstra { 171391e43daSPeter Zijlstra struct rq *rq = cpu_rq(cpu); 172391e43daSPeter Zijlstra 173391e43daSPeter Zijlstra rt_rq->highest_prio.curr = MAX_RT_PRIO; 174391e43daSPeter Zijlstra rt_rq->rt_nr_boosted = 0; 175391e43daSPeter Zijlstra rt_rq->rq = rq; 176391e43daSPeter Zijlstra rt_rq->tg = tg; 177391e43daSPeter Zijlstra 178391e43daSPeter Zijlstra tg->rt_rq[cpu] = rt_rq; 179391e43daSPeter Zijlstra tg->rt_se[cpu] = rt_se; 180391e43daSPeter Zijlstra 181391e43daSPeter Zijlstra if (!rt_se) 182391e43daSPeter Zijlstra return; 183391e43daSPeter Zijlstra 184391e43daSPeter Zijlstra if (!parent) 185391e43daSPeter Zijlstra rt_se->rt_rq = &rq->rt; 186391e43daSPeter Zijlstra else 187391e43daSPeter Zijlstra rt_se->rt_rq = parent->my_q; 188391e43daSPeter Zijlstra 189391e43daSPeter Zijlstra rt_se->my_q = rt_rq; 190391e43daSPeter Zijlstra rt_se->parent = parent; 191391e43daSPeter Zijlstra INIT_LIST_HEAD(&rt_se->run_list); 192391e43daSPeter Zijlstra } 193391e43daSPeter Zijlstra 194391e43daSPeter Zijlstra int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) 195391e43daSPeter Zijlstra { 196391e43daSPeter Zijlstra struct rt_rq *rt_rq; 197391e43daSPeter Zijlstra struct sched_rt_entity *rt_se; 198391e43daSPeter Zijlstra int i; 199391e43daSPeter Zijlstra 200391e43daSPeter Zijlstra tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); 201391e43daSPeter Zijlstra if (!tg->rt_rq) 202391e43daSPeter Zijlstra goto err; 203391e43daSPeter Zijlstra tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); 204391e43daSPeter Zijlstra if (!tg->rt_se) 205391e43daSPeter Zijlstra goto err; 206391e43daSPeter Zijlstra 207391e43daSPeter Zijlstra init_rt_bandwidth(&tg->rt_bandwidth, 208391e43daSPeter Zijlstra ktime_to_ns(def_rt_bandwidth.rt_period), 0); 209391e43daSPeter Zijlstra 210391e43daSPeter Zijlstra for_each_possible_cpu(i) { 211391e43daSPeter Zijlstra rt_rq = kzalloc_node(sizeof(struct rt_rq), 212391e43daSPeter Zijlstra GFP_KERNEL, cpu_to_node(i)); 213391e43daSPeter Zijlstra if (!rt_rq) 214391e43daSPeter Zijlstra goto err; 215391e43daSPeter Zijlstra 216391e43daSPeter Zijlstra rt_se = kzalloc_node(sizeof(struct sched_rt_entity), 217391e43daSPeter Zijlstra GFP_KERNEL, cpu_to_node(i)); 218391e43daSPeter Zijlstra if (!rt_se) 219391e43daSPeter Zijlstra goto err_free_rq; 220391e43daSPeter Zijlstra 22107c54f7aSAbel Vesa init_rt_rq(rt_rq); 222391e43daSPeter Zijlstra rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; 223391e43daSPeter Zijlstra init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]); 224391e43daSPeter Zijlstra } 225391e43daSPeter Zijlstra 226391e43daSPeter Zijlstra return 1; 227391e43daSPeter Zijlstra 228391e43daSPeter Zijlstra err_free_rq: 229391e43daSPeter Zijlstra kfree(rt_rq); 230391e43daSPeter Zijlstra err: 231391e43daSPeter Zijlstra return 0; 232391e43daSPeter Zijlstra } 233391e43daSPeter Zijlstra 234391e43daSPeter Zijlstra #else /* CONFIG_RT_GROUP_SCHED */ 235391e43daSPeter Zijlstra 236391e43daSPeter Zijlstra #define rt_entity_is_task(rt_se) (1) 237391e43daSPeter Zijlstra 238391e43daSPeter Zijlstra static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) 239391e43daSPeter Zijlstra { 240391e43daSPeter Zijlstra return container_of(rt_se, struct task_struct, rt); 241391e43daSPeter Zijlstra } 242391e43daSPeter Zijlstra 243391e43daSPeter Zijlstra static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) 244391e43daSPeter Zijlstra { 245391e43daSPeter Zijlstra return container_of(rt_rq, struct rq, rt); 246391e43daSPeter Zijlstra } 247391e43daSPeter Zijlstra 248653d07a6SKirill Tkhai static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se) 249391e43daSPeter Zijlstra { 250391e43daSPeter Zijlstra struct task_struct *p = rt_task_of(rt_se); 251653d07a6SKirill Tkhai 252653d07a6SKirill Tkhai return task_rq(p); 253653d07a6SKirill Tkhai } 254653d07a6SKirill Tkhai 255653d07a6SKirill Tkhai static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) 256653d07a6SKirill Tkhai { 257653d07a6SKirill Tkhai struct rq *rq = rq_of_rt_se(rt_se); 258391e43daSPeter Zijlstra 259391e43daSPeter Zijlstra return &rq->rt; 260391e43daSPeter Zijlstra } 261391e43daSPeter Zijlstra 262391e43daSPeter Zijlstra void free_rt_sched_group(struct task_group *tg) { } 263391e43daSPeter Zijlstra 264391e43daSPeter Zijlstra int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) 265391e43daSPeter Zijlstra { 266391e43daSPeter Zijlstra return 1; 267391e43daSPeter Zijlstra } 268391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */ 269391e43daSPeter Zijlstra 270391e43daSPeter Zijlstra #ifdef CONFIG_SMP 271391e43daSPeter Zijlstra 2728046d680SPeter Zijlstra static void pull_rt_task(struct rq *this_rq); 27338033c37SPeter Zijlstra 274dc877341SPeter Zijlstra static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev) 275dc877341SPeter Zijlstra { 276dc877341SPeter Zijlstra /* Try to pull RT tasks here if we lower this rq's prio */ 277dc877341SPeter Zijlstra return rq->rt.highest_prio.curr > prev->prio; 278dc877341SPeter Zijlstra } 279dc877341SPeter Zijlstra 280391e43daSPeter Zijlstra static inline int rt_overloaded(struct rq *rq) 281391e43daSPeter Zijlstra { 282391e43daSPeter Zijlstra return atomic_read(&rq->rd->rto_count); 283391e43daSPeter Zijlstra } 284391e43daSPeter Zijlstra 285391e43daSPeter Zijlstra static inline void rt_set_overload(struct rq *rq) 286391e43daSPeter Zijlstra { 287391e43daSPeter Zijlstra if (!rq->online) 288391e43daSPeter Zijlstra return; 289391e43daSPeter Zijlstra 290391e43daSPeter Zijlstra cpumask_set_cpu(rq->cpu, rq->rd->rto_mask); 291391e43daSPeter Zijlstra /* 292391e43daSPeter Zijlstra * Make sure the mask is visible before we set 293391e43daSPeter Zijlstra * the overload count. That is checked to determine 294391e43daSPeter Zijlstra * if we should look at the mask. It would be a shame 295391e43daSPeter Zijlstra * if we looked at the mask, but the mask was not 296391e43daSPeter Zijlstra * updated yet. 2977c3f2ab7SPeter Zijlstra * 2987c3f2ab7SPeter Zijlstra * Matched by the barrier in pull_rt_task(). 299391e43daSPeter Zijlstra */ 3007c3f2ab7SPeter Zijlstra smp_wmb(); 301391e43daSPeter Zijlstra atomic_inc(&rq->rd->rto_count); 302391e43daSPeter Zijlstra } 303391e43daSPeter Zijlstra 304391e43daSPeter Zijlstra static inline void rt_clear_overload(struct rq *rq) 305391e43daSPeter Zijlstra { 306391e43daSPeter Zijlstra if (!rq->online) 307391e43daSPeter Zijlstra return; 308391e43daSPeter Zijlstra 309391e43daSPeter Zijlstra /* the order here really doesn't matter */ 310391e43daSPeter Zijlstra atomic_dec(&rq->rd->rto_count); 311391e43daSPeter Zijlstra cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask); 312391e43daSPeter Zijlstra } 313391e43daSPeter Zijlstra 314391e43daSPeter Zijlstra static void update_rt_migration(struct rt_rq *rt_rq) 315391e43daSPeter Zijlstra { 316391e43daSPeter Zijlstra if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) { 317391e43daSPeter Zijlstra if (!rt_rq->overloaded) { 318391e43daSPeter Zijlstra rt_set_overload(rq_of_rt_rq(rt_rq)); 319391e43daSPeter Zijlstra rt_rq->overloaded = 1; 320391e43daSPeter Zijlstra } 321391e43daSPeter Zijlstra } else if (rt_rq->overloaded) { 322391e43daSPeter Zijlstra rt_clear_overload(rq_of_rt_rq(rt_rq)); 323391e43daSPeter Zijlstra rt_rq->overloaded = 0; 324391e43daSPeter Zijlstra } 325391e43daSPeter Zijlstra } 326391e43daSPeter Zijlstra 327391e43daSPeter Zijlstra static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 328391e43daSPeter Zijlstra { 32929baa747SPeter Zijlstra struct task_struct *p; 33029baa747SPeter Zijlstra 331391e43daSPeter Zijlstra if (!rt_entity_is_task(rt_se)) 332391e43daSPeter Zijlstra return; 333391e43daSPeter Zijlstra 33429baa747SPeter Zijlstra p = rt_task_of(rt_se); 335391e43daSPeter Zijlstra rt_rq = &rq_of_rt_rq(rt_rq)->rt; 336391e43daSPeter Zijlstra 337391e43daSPeter Zijlstra rt_rq->rt_nr_total++; 33850605ffbSThomas Gleixner if (tsk_nr_cpus_allowed(p) > 1) 339391e43daSPeter Zijlstra rt_rq->rt_nr_migratory++; 340391e43daSPeter Zijlstra 341391e43daSPeter Zijlstra update_rt_migration(rt_rq); 342391e43daSPeter Zijlstra } 343391e43daSPeter Zijlstra 344391e43daSPeter Zijlstra static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 345391e43daSPeter Zijlstra { 34629baa747SPeter Zijlstra struct task_struct *p; 34729baa747SPeter Zijlstra 348391e43daSPeter Zijlstra if (!rt_entity_is_task(rt_se)) 349391e43daSPeter Zijlstra return; 350391e43daSPeter Zijlstra 35129baa747SPeter Zijlstra p = rt_task_of(rt_se); 352391e43daSPeter Zijlstra rt_rq = &rq_of_rt_rq(rt_rq)->rt; 353391e43daSPeter Zijlstra 354391e43daSPeter Zijlstra rt_rq->rt_nr_total--; 35550605ffbSThomas Gleixner if (tsk_nr_cpus_allowed(p) > 1) 356391e43daSPeter Zijlstra rt_rq->rt_nr_migratory--; 357391e43daSPeter Zijlstra 358391e43daSPeter Zijlstra update_rt_migration(rt_rq); 359391e43daSPeter Zijlstra } 360391e43daSPeter Zijlstra 361391e43daSPeter Zijlstra static inline int has_pushable_tasks(struct rq *rq) 362391e43daSPeter Zijlstra { 363391e43daSPeter Zijlstra return !plist_head_empty(&rq->rt.pushable_tasks); 364391e43daSPeter Zijlstra } 365391e43daSPeter Zijlstra 366fd7a4bedSPeter Zijlstra static DEFINE_PER_CPU(struct callback_head, rt_push_head); 367fd7a4bedSPeter Zijlstra static DEFINE_PER_CPU(struct callback_head, rt_pull_head); 368e3fca9e7SPeter Zijlstra 369e3fca9e7SPeter Zijlstra static void push_rt_tasks(struct rq *); 370fd7a4bedSPeter Zijlstra static void pull_rt_task(struct rq *); 371e3fca9e7SPeter Zijlstra 372e3fca9e7SPeter Zijlstra static inline void queue_push_tasks(struct rq *rq) 373dc877341SPeter Zijlstra { 374e3fca9e7SPeter Zijlstra if (!has_pushable_tasks(rq)) 375e3fca9e7SPeter Zijlstra return; 376e3fca9e7SPeter Zijlstra 377fd7a4bedSPeter Zijlstra queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks); 378fd7a4bedSPeter Zijlstra } 379fd7a4bedSPeter Zijlstra 380fd7a4bedSPeter Zijlstra static inline void queue_pull_task(struct rq *rq) 381fd7a4bedSPeter Zijlstra { 382fd7a4bedSPeter Zijlstra queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task); 383dc877341SPeter Zijlstra } 384dc877341SPeter Zijlstra 385391e43daSPeter Zijlstra static void enqueue_pushable_task(struct rq *rq, struct task_struct *p) 386391e43daSPeter Zijlstra { 387391e43daSPeter Zijlstra plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); 388391e43daSPeter Zijlstra plist_node_init(&p->pushable_tasks, p->prio); 389391e43daSPeter Zijlstra plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks); 390391e43daSPeter Zijlstra 391391e43daSPeter Zijlstra /* Update the highest prio pushable task */ 392391e43daSPeter Zijlstra if (p->prio < rq->rt.highest_prio.next) 393391e43daSPeter Zijlstra rq->rt.highest_prio.next = p->prio; 394391e43daSPeter Zijlstra } 395391e43daSPeter Zijlstra 396391e43daSPeter Zijlstra static void dequeue_pushable_task(struct rq *rq, struct task_struct *p) 397391e43daSPeter Zijlstra { 398391e43daSPeter Zijlstra plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); 399391e43daSPeter Zijlstra 400391e43daSPeter Zijlstra /* Update the new highest prio pushable task */ 401391e43daSPeter Zijlstra if (has_pushable_tasks(rq)) { 402391e43daSPeter Zijlstra p = plist_first_entry(&rq->rt.pushable_tasks, 403391e43daSPeter Zijlstra struct task_struct, pushable_tasks); 404391e43daSPeter Zijlstra rq->rt.highest_prio.next = p->prio; 405391e43daSPeter Zijlstra } else 406391e43daSPeter Zijlstra rq->rt.highest_prio.next = MAX_RT_PRIO; 407391e43daSPeter Zijlstra } 408391e43daSPeter Zijlstra 409391e43daSPeter Zijlstra #else 410391e43daSPeter Zijlstra 411391e43daSPeter Zijlstra static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p) 412391e43daSPeter Zijlstra { 413391e43daSPeter Zijlstra } 414391e43daSPeter Zijlstra 415391e43daSPeter Zijlstra static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p) 416391e43daSPeter Zijlstra { 417391e43daSPeter Zijlstra } 418391e43daSPeter Zijlstra 419391e43daSPeter Zijlstra static inline 420391e43daSPeter Zijlstra void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 421391e43daSPeter Zijlstra { 422391e43daSPeter Zijlstra } 423391e43daSPeter Zijlstra 424391e43daSPeter Zijlstra static inline 425391e43daSPeter Zijlstra void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 426391e43daSPeter Zijlstra { 427391e43daSPeter Zijlstra } 428391e43daSPeter Zijlstra 429dc877341SPeter Zijlstra static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev) 430dc877341SPeter Zijlstra { 431dc877341SPeter Zijlstra return false; 432dc877341SPeter Zijlstra } 433dc877341SPeter Zijlstra 4348046d680SPeter Zijlstra static inline void pull_rt_task(struct rq *this_rq) 435dc877341SPeter Zijlstra { 436dc877341SPeter Zijlstra } 437dc877341SPeter Zijlstra 438e3fca9e7SPeter Zijlstra static inline void queue_push_tasks(struct rq *rq) 439dc877341SPeter Zijlstra { 440dc877341SPeter Zijlstra } 441391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 442391e43daSPeter Zijlstra 443f4ebcbc0SKirill Tkhai static void enqueue_top_rt_rq(struct rt_rq *rt_rq); 444f4ebcbc0SKirill Tkhai static void dequeue_top_rt_rq(struct rt_rq *rt_rq); 445f4ebcbc0SKirill Tkhai 446391e43daSPeter Zijlstra static inline int on_rt_rq(struct sched_rt_entity *rt_se) 447391e43daSPeter Zijlstra { 448ff77e468SPeter Zijlstra return rt_se->on_rq; 449391e43daSPeter Zijlstra } 450391e43daSPeter Zijlstra 451391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 452391e43daSPeter Zijlstra 453391e43daSPeter Zijlstra static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) 454391e43daSPeter Zijlstra { 455391e43daSPeter Zijlstra if (!rt_rq->tg) 456391e43daSPeter Zijlstra return RUNTIME_INF; 457391e43daSPeter Zijlstra 458391e43daSPeter Zijlstra return rt_rq->rt_runtime; 459391e43daSPeter Zijlstra } 460391e43daSPeter Zijlstra 461391e43daSPeter Zijlstra static inline u64 sched_rt_period(struct rt_rq *rt_rq) 462391e43daSPeter Zijlstra { 463391e43daSPeter Zijlstra return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period); 464391e43daSPeter Zijlstra } 465391e43daSPeter Zijlstra 466391e43daSPeter Zijlstra typedef struct task_group *rt_rq_iter_t; 467391e43daSPeter Zijlstra 468391e43daSPeter Zijlstra static inline struct task_group *next_task_group(struct task_group *tg) 469391e43daSPeter Zijlstra { 470391e43daSPeter Zijlstra do { 471391e43daSPeter Zijlstra tg = list_entry_rcu(tg->list.next, 472391e43daSPeter Zijlstra typeof(struct task_group), list); 473391e43daSPeter Zijlstra } while (&tg->list != &task_groups && task_group_is_autogroup(tg)); 474391e43daSPeter Zijlstra 475391e43daSPeter Zijlstra if (&tg->list == &task_groups) 476391e43daSPeter Zijlstra tg = NULL; 477391e43daSPeter Zijlstra 478391e43daSPeter Zijlstra return tg; 479391e43daSPeter Zijlstra } 480391e43daSPeter Zijlstra 481391e43daSPeter Zijlstra #define for_each_rt_rq(rt_rq, iter, rq) \ 482391e43daSPeter Zijlstra for (iter = container_of(&task_groups, typeof(*iter), list); \ 483391e43daSPeter Zijlstra (iter = next_task_group(iter)) && \ 484391e43daSPeter Zijlstra (rt_rq = iter->rt_rq[cpu_of(rq)]);) 485391e43daSPeter Zijlstra 486391e43daSPeter Zijlstra #define for_each_sched_rt_entity(rt_se) \ 487391e43daSPeter Zijlstra for (; rt_se; rt_se = rt_se->parent) 488391e43daSPeter Zijlstra 489391e43daSPeter Zijlstra static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) 490391e43daSPeter Zijlstra { 491391e43daSPeter Zijlstra return rt_se->my_q; 492391e43daSPeter Zijlstra } 493391e43daSPeter Zijlstra 494ff77e468SPeter Zijlstra static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags); 495ff77e468SPeter Zijlstra static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags); 496391e43daSPeter Zijlstra 497391e43daSPeter Zijlstra static void sched_rt_rq_enqueue(struct rt_rq *rt_rq) 498391e43daSPeter Zijlstra { 499391e43daSPeter Zijlstra struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr; 5008875125eSKirill Tkhai struct rq *rq = rq_of_rt_rq(rt_rq); 501391e43daSPeter Zijlstra struct sched_rt_entity *rt_se; 502391e43daSPeter Zijlstra 5038875125eSKirill Tkhai int cpu = cpu_of(rq); 504391e43daSPeter Zijlstra 505391e43daSPeter Zijlstra rt_se = rt_rq->tg->rt_se[cpu]; 506391e43daSPeter Zijlstra 507391e43daSPeter Zijlstra if (rt_rq->rt_nr_running) { 508f4ebcbc0SKirill Tkhai if (!rt_se) 509f4ebcbc0SKirill Tkhai enqueue_top_rt_rq(rt_rq); 510f4ebcbc0SKirill Tkhai else if (!on_rt_rq(rt_se)) 511ff77e468SPeter Zijlstra enqueue_rt_entity(rt_se, 0); 512f4ebcbc0SKirill Tkhai 513391e43daSPeter Zijlstra if (rt_rq->highest_prio.curr < curr->prio) 5148875125eSKirill Tkhai resched_curr(rq); 515391e43daSPeter Zijlstra } 516391e43daSPeter Zijlstra } 517391e43daSPeter Zijlstra 518391e43daSPeter Zijlstra static void sched_rt_rq_dequeue(struct rt_rq *rt_rq) 519391e43daSPeter Zijlstra { 520391e43daSPeter Zijlstra struct sched_rt_entity *rt_se; 521391e43daSPeter Zijlstra int cpu = cpu_of(rq_of_rt_rq(rt_rq)); 522391e43daSPeter Zijlstra 523391e43daSPeter Zijlstra rt_se = rt_rq->tg->rt_se[cpu]; 524391e43daSPeter Zijlstra 525f4ebcbc0SKirill Tkhai if (!rt_se) 526f4ebcbc0SKirill Tkhai dequeue_top_rt_rq(rt_rq); 527f4ebcbc0SKirill Tkhai else if (on_rt_rq(rt_se)) 528ff77e468SPeter Zijlstra dequeue_rt_entity(rt_se, 0); 529391e43daSPeter Zijlstra } 530391e43daSPeter Zijlstra 53146383648SKirill Tkhai static inline int rt_rq_throttled(struct rt_rq *rt_rq) 53246383648SKirill Tkhai { 53346383648SKirill Tkhai return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted; 53446383648SKirill Tkhai } 53546383648SKirill Tkhai 536391e43daSPeter Zijlstra static int rt_se_boosted(struct sched_rt_entity *rt_se) 537391e43daSPeter Zijlstra { 538391e43daSPeter Zijlstra struct rt_rq *rt_rq = group_rt_rq(rt_se); 539391e43daSPeter Zijlstra struct task_struct *p; 540391e43daSPeter Zijlstra 541391e43daSPeter Zijlstra if (rt_rq) 542391e43daSPeter Zijlstra return !!rt_rq->rt_nr_boosted; 543391e43daSPeter Zijlstra 544391e43daSPeter Zijlstra p = rt_task_of(rt_se); 545391e43daSPeter Zijlstra return p->prio != p->normal_prio; 546391e43daSPeter Zijlstra } 547391e43daSPeter Zijlstra 548391e43daSPeter Zijlstra #ifdef CONFIG_SMP 549391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void) 550391e43daSPeter Zijlstra { 551424c93feSNathan Zimmer return this_rq()->rd->span; 552391e43daSPeter Zijlstra } 553391e43daSPeter Zijlstra #else 554391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void) 555391e43daSPeter Zijlstra { 556391e43daSPeter Zijlstra return cpu_online_mask; 557391e43daSPeter Zijlstra } 558391e43daSPeter Zijlstra #endif 559391e43daSPeter Zijlstra 560391e43daSPeter Zijlstra static inline 561391e43daSPeter Zijlstra struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) 562391e43daSPeter Zijlstra { 563391e43daSPeter Zijlstra return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu]; 564391e43daSPeter Zijlstra } 565391e43daSPeter Zijlstra 566391e43daSPeter Zijlstra static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) 567391e43daSPeter Zijlstra { 568391e43daSPeter Zijlstra return &rt_rq->tg->rt_bandwidth; 569391e43daSPeter Zijlstra } 570391e43daSPeter Zijlstra 571391e43daSPeter Zijlstra #else /* !CONFIG_RT_GROUP_SCHED */ 572391e43daSPeter Zijlstra 573391e43daSPeter Zijlstra static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) 574391e43daSPeter Zijlstra { 575391e43daSPeter Zijlstra return rt_rq->rt_runtime; 576391e43daSPeter Zijlstra } 577391e43daSPeter Zijlstra 578391e43daSPeter Zijlstra static inline u64 sched_rt_period(struct rt_rq *rt_rq) 579391e43daSPeter Zijlstra { 580391e43daSPeter Zijlstra return ktime_to_ns(def_rt_bandwidth.rt_period); 581391e43daSPeter Zijlstra } 582391e43daSPeter Zijlstra 583391e43daSPeter Zijlstra typedef struct rt_rq *rt_rq_iter_t; 584391e43daSPeter Zijlstra 585391e43daSPeter Zijlstra #define for_each_rt_rq(rt_rq, iter, rq) \ 586391e43daSPeter Zijlstra for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL) 587391e43daSPeter Zijlstra 588391e43daSPeter Zijlstra #define for_each_sched_rt_entity(rt_se) \ 589391e43daSPeter Zijlstra for (; rt_se; rt_se = NULL) 590391e43daSPeter Zijlstra 591391e43daSPeter Zijlstra static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) 592391e43daSPeter Zijlstra { 593391e43daSPeter Zijlstra return NULL; 594391e43daSPeter Zijlstra } 595391e43daSPeter Zijlstra 596391e43daSPeter Zijlstra static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq) 597391e43daSPeter Zijlstra { 598f4ebcbc0SKirill Tkhai struct rq *rq = rq_of_rt_rq(rt_rq); 599f4ebcbc0SKirill Tkhai 600f4ebcbc0SKirill Tkhai if (!rt_rq->rt_nr_running) 601f4ebcbc0SKirill Tkhai return; 602f4ebcbc0SKirill Tkhai 603f4ebcbc0SKirill Tkhai enqueue_top_rt_rq(rt_rq); 6048875125eSKirill Tkhai resched_curr(rq); 605391e43daSPeter Zijlstra } 606391e43daSPeter Zijlstra 607391e43daSPeter Zijlstra static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq) 608391e43daSPeter Zijlstra { 609f4ebcbc0SKirill Tkhai dequeue_top_rt_rq(rt_rq); 610391e43daSPeter Zijlstra } 611391e43daSPeter Zijlstra 61246383648SKirill Tkhai static inline int rt_rq_throttled(struct rt_rq *rt_rq) 61346383648SKirill Tkhai { 61446383648SKirill Tkhai return rt_rq->rt_throttled; 61546383648SKirill Tkhai } 61646383648SKirill Tkhai 617391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void) 618391e43daSPeter Zijlstra { 619391e43daSPeter Zijlstra return cpu_online_mask; 620391e43daSPeter Zijlstra } 621391e43daSPeter Zijlstra 622391e43daSPeter Zijlstra static inline 623391e43daSPeter Zijlstra struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) 624391e43daSPeter Zijlstra { 625391e43daSPeter Zijlstra return &cpu_rq(cpu)->rt; 626391e43daSPeter Zijlstra } 627391e43daSPeter Zijlstra 628391e43daSPeter Zijlstra static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) 629391e43daSPeter Zijlstra { 630391e43daSPeter Zijlstra return &def_rt_bandwidth; 631391e43daSPeter Zijlstra } 632391e43daSPeter Zijlstra 633391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */ 634391e43daSPeter Zijlstra 635faa59937SJuri Lelli bool sched_rt_bandwidth_account(struct rt_rq *rt_rq) 636faa59937SJuri Lelli { 637faa59937SJuri Lelli struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); 638faa59937SJuri Lelli 639faa59937SJuri Lelli return (hrtimer_active(&rt_b->rt_period_timer) || 640faa59937SJuri Lelli rt_rq->rt_time < rt_b->rt_runtime); 641faa59937SJuri Lelli } 642faa59937SJuri Lelli 643391e43daSPeter Zijlstra #ifdef CONFIG_SMP 644391e43daSPeter Zijlstra /* 645391e43daSPeter Zijlstra * We ran out of runtime, see if we can borrow some from our neighbours. 646391e43daSPeter Zijlstra */ 647269b26a5SJuri Lelli static void do_balance_runtime(struct rt_rq *rt_rq) 648391e43daSPeter Zijlstra { 649391e43daSPeter Zijlstra struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); 650aa7f6730SShawn Bohrer struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd; 651269b26a5SJuri Lelli int i, weight; 652391e43daSPeter Zijlstra u64 rt_period; 653391e43daSPeter Zijlstra 654391e43daSPeter Zijlstra weight = cpumask_weight(rd->span); 655391e43daSPeter Zijlstra 656391e43daSPeter Zijlstra raw_spin_lock(&rt_b->rt_runtime_lock); 657391e43daSPeter Zijlstra rt_period = ktime_to_ns(rt_b->rt_period); 658391e43daSPeter Zijlstra for_each_cpu(i, rd->span) { 659391e43daSPeter Zijlstra struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); 660391e43daSPeter Zijlstra s64 diff; 661391e43daSPeter Zijlstra 662391e43daSPeter Zijlstra if (iter == rt_rq) 663391e43daSPeter Zijlstra continue; 664391e43daSPeter Zijlstra 665391e43daSPeter Zijlstra raw_spin_lock(&iter->rt_runtime_lock); 666391e43daSPeter Zijlstra /* 667391e43daSPeter Zijlstra * Either all rqs have inf runtime and there's nothing to steal 668391e43daSPeter Zijlstra * or __disable_runtime() below sets a specific rq to inf to 669391e43daSPeter Zijlstra * indicate its been disabled and disalow stealing. 670391e43daSPeter Zijlstra */ 671391e43daSPeter Zijlstra if (iter->rt_runtime == RUNTIME_INF) 672391e43daSPeter Zijlstra goto next; 673391e43daSPeter Zijlstra 674391e43daSPeter Zijlstra /* 675391e43daSPeter Zijlstra * From runqueues with spare time, take 1/n part of their 676391e43daSPeter Zijlstra * spare time, but no more than our period. 677391e43daSPeter Zijlstra */ 678391e43daSPeter Zijlstra diff = iter->rt_runtime - iter->rt_time; 679391e43daSPeter Zijlstra if (diff > 0) { 680391e43daSPeter Zijlstra diff = div_u64((u64)diff, weight); 681391e43daSPeter Zijlstra if (rt_rq->rt_runtime + diff > rt_period) 682391e43daSPeter Zijlstra diff = rt_period - rt_rq->rt_runtime; 683391e43daSPeter Zijlstra iter->rt_runtime -= diff; 684391e43daSPeter Zijlstra rt_rq->rt_runtime += diff; 685391e43daSPeter Zijlstra if (rt_rq->rt_runtime == rt_period) { 686391e43daSPeter Zijlstra raw_spin_unlock(&iter->rt_runtime_lock); 687391e43daSPeter Zijlstra break; 688391e43daSPeter Zijlstra } 689391e43daSPeter Zijlstra } 690391e43daSPeter Zijlstra next: 691391e43daSPeter Zijlstra raw_spin_unlock(&iter->rt_runtime_lock); 692391e43daSPeter Zijlstra } 693391e43daSPeter Zijlstra raw_spin_unlock(&rt_b->rt_runtime_lock); 694391e43daSPeter Zijlstra } 695391e43daSPeter Zijlstra 696391e43daSPeter Zijlstra /* 697391e43daSPeter Zijlstra * Ensure this RQ takes back all the runtime it lend to its neighbours. 698391e43daSPeter Zijlstra */ 699391e43daSPeter Zijlstra static void __disable_runtime(struct rq *rq) 700391e43daSPeter Zijlstra { 701391e43daSPeter Zijlstra struct root_domain *rd = rq->rd; 702391e43daSPeter Zijlstra rt_rq_iter_t iter; 703391e43daSPeter Zijlstra struct rt_rq *rt_rq; 704391e43daSPeter Zijlstra 705391e43daSPeter Zijlstra if (unlikely(!scheduler_running)) 706391e43daSPeter Zijlstra return; 707391e43daSPeter Zijlstra 708391e43daSPeter Zijlstra for_each_rt_rq(rt_rq, iter, rq) { 709391e43daSPeter Zijlstra struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); 710391e43daSPeter Zijlstra s64 want; 711391e43daSPeter Zijlstra int i; 712391e43daSPeter Zijlstra 713391e43daSPeter Zijlstra raw_spin_lock(&rt_b->rt_runtime_lock); 714391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 715391e43daSPeter Zijlstra /* 716391e43daSPeter Zijlstra * Either we're all inf and nobody needs to borrow, or we're 717391e43daSPeter Zijlstra * already disabled and thus have nothing to do, or we have 718391e43daSPeter Zijlstra * exactly the right amount of runtime to take out. 719391e43daSPeter Zijlstra */ 720391e43daSPeter Zijlstra if (rt_rq->rt_runtime == RUNTIME_INF || 721391e43daSPeter Zijlstra rt_rq->rt_runtime == rt_b->rt_runtime) 722391e43daSPeter Zijlstra goto balanced; 723391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 724391e43daSPeter Zijlstra 725391e43daSPeter Zijlstra /* 726391e43daSPeter Zijlstra * Calculate the difference between what we started out with 727391e43daSPeter Zijlstra * and what we current have, that's the amount of runtime 728391e43daSPeter Zijlstra * we lend and now have to reclaim. 729391e43daSPeter Zijlstra */ 730391e43daSPeter Zijlstra want = rt_b->rt_runtime - rt_rq->rt_runtime; 731391e43daSPeter Zijlstra 732391e43daSPeter Zijlstra /* 733391e43daSPeter Zijlstra * Greedy reclaim, take back as much as we can. 734391e43daSPeter Zijlstra */ 735391e43daSPeter Zijlstra for_each_cpu(i, rd->span) { 736391e43daSPeter Zijlstra struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); 737391e43daSPeter Zijlstra s64 diff; 738391e43daSPeter Zijlstra 739391e43daSPeter Zijlstra /* 740391e43daSPeter Zijlstra * Can't reclaim from ourselves or disabled runqueues. 741391e43daSPeter Zijlstra */ 742391e43daSPeter Zijlstra if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF) 743391e43daSPeter Zijlstra continue; 744391e43daSPeter Zijlstra 745391e43daSPeter Zijlstra raw_spin_lock(&iter->rt_runtime_lock); 746391e43daSPeter Zijlstra if (want > 0) { 747391e43daSPeter Zijlstra diff = min_t(s64, iter->rt_runtime, want); 748391e43daSPeter Zijlstra iter->rt_runtime -= diff; 749391e43daSPeter Zijlstra want -= diff; 750391e43daSPeter Zijlstra } else { 751391e43daSPeter Zijlstra iter->rt_runtime -= want; 752391e43daSPeter Zijlstra want -= want; 753391e43daSPeter Zijlstra } 754391e43daSPeter Zijlstra raw_spin_unlock(&iter->rt_runtime_lock); 755391e43daSPeter Zijlstra 756391e43daSPeter Zijlstra if (!want) 757391e43daSPeter Zijlstra break; 758391e43daSPeter Zijlstra } 759391e43daSPeter Zijlstra 760391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 761391e43daSPeter Zijlstra /* 762391e43daSPeter Zijlstra * We cannot be left wanting - that would mean some runtime 763391e43daSPeter Zijlstra * leaked out of the system. 764391e43daSPeter Zijlstra */ 765391e43daSPeter Zijlstra BUG_ON(want); 766391e43daSPeter Zijlstra balanced: 767391e43daSPeter Zijlstra /* 768391e43daSPeter Zijlstra * Disable all the borrow logic by pretending we have inf 769391e43daSPeter Zijlstra * runtime - in which case borrowing doesn't make sense. 770391e43daSPeter Zijlstra */ 771391e43daSPeter Zijlstra rt_rq->rt_runtime = RUNTIME_INF; 772a4c96ae3SPeter Boonstoppel rt_rq->rt_throttled = 0; 773391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 774391e43daSPeter Zijlstra raw_spin_unlock(&rt_b->rt_runtime_lock); 77599b62567SKirill Tkhai 77699b62567SKirill Tkhai /* Make rt_rq available for pick_next_task() */ 77799b62567SKirill Tkhai sched_rt_rq_enqueue(rt_rq); 778391e43daSPeter Zijlstra } 779391e43daSPeter Zijlstra } 780391e43daSPeter Zijlstra 781391e43daSPeter Zijlstra static void __enable_runtime(struct rq *rq) 782391e43daSPeter Zijlstra { 783391e43daSPeter Zijlstra rt_rq_iter_t iter; 784391e43daSPeter Zijlstra struct rt_rq *rt_rq; 785391e43daSPeter Zijlstra 786391e43daSPeter Zijlstra if (unlikely(!scheduler_running)) 787391e43daSPeter Zijlstra return; 788391e43daSPeter Zijlstra 789391e43daSPeter Zijlstra /* 790391e43daSPeter Zijlstra * Reset each runqueue's bandwidth settings 791391e43daSPeter Zijlstra */ 792391e43daSPeter Zijlstra for_each_rt_rq(rt_rq, iter, rq) { 793391e43daSPeter Zijlstra struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); 794391e43daSPeter Zijlstra 795391e43daSPeter Zijlstra raw_spin_lock(&rt_b->rt_runtime_lock); 796391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 797391e43daSPeter Zijlstra rt_rq->rt_runtime = rt_b->rt_runtime; 798391e43daSPeter Zijlstra rt_rq->rt_time = 0; 799391e43daSPeter Zijlstra rt_rq->rt_throttled = 0; 800391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 801391e43daSPeter Zijlstra raw_spin_unlock(&rt_b->rt_runtime_lock); 802391e43daSPeter Zijlstra } 803391e43daSPeter Zijlstra } 804391e43daSPeter Zijlstra 805269b26a5SJuri Lelli static void balance_runtime(struct rt_rq *rt_rq) 806391e43daSPeter Zijlstra { 807391e43daSPeter Zijlstra if (!sched_feat(RT_RUNTIME_SHARE)) 808269b26a5SJuri Lelli return; 809391e43daSPeter Zijlstra 810391e43daSPeter Zijlstra if (rt_rq->rt_time > rt_rq->rt_runtime) { 811391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 812269b26a5SJuri Lelli do_balance_runtime(rt_rq); 813391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 814391e43daSPeter Zijlstra } 815391e43daSPeter Zijlstra } 816391e43daSPeter Zijlstra #else /* !CONFIG_SMP */ 817269b26a5SJuri Lelli static inline void balance_runtime(struct rt_rq *rt_rq) {} 818391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 819391e43daSPeter Zijlstra 820391e43daSPeter Zijlstra static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun) 821391e43daSPeter Zijlstra { 82242c62a58SPeter Zijlstra int i, idle = 1, throttled = 0; 823391e43daSPeter Zijlstra const struct cpumask *span; 824391e43daSPeter Zijlstra 825391e43daSPeter Zijlstra span = sched_rt_period_mask(); 826e221d028SMike Galbraith #ifdef CONFIG_RT_GROUP_SCHED 827e221d028SMike Galbraith /* 828e221d028SMike Galbraith * FIXME: isolated CPUs should really leave the root task group, 829e221d028SMike Galbraith * whether they are isolcpus or were isolated via cpusets, lest 830e221d028SMike Galbraith * the timer run on a CPU which does not service all runqueues, 831e221d028SMike Galbraith * potentially leaving other CPUs indefinitely throttled. If 832e221d028SMike Galbraith * isolation is really required, the user will turn the throttle 833e221d028SMike Galbraith * off to kill the perturbations it causes anyway. Meanwhile, 834e221d028SMike Galbraith * this maintains functionality for boot and/or troubleshooting. 835e221d028SMike Galbraith */ 836e221d028SMike Galbraith if (rt_b == &root_task_group.rt_bandwidth) 837e221d028SMike Galbraith span = cpu_online_mask; 838e221d028SMike Galbraith #endif 839391e43daSPeter Zijlstra for_each_cpu(i, span) { 840391e43daSPeter Zijlstra int enqueue = 0; 841391e43daSPeter Zijlstra struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i); 842391e43daSPeter Zijlstra struct rq *rq = rq_of_rt_rq(rt_rq); 843391e43daSPeter Zijlstra 844391e43daSPeter Zijlstra raw_spin_lock(&rq->lock); 845391e43daSPeter Zijlstra if (rt_rq->rt_time) { 846391e43daSPeter Zijlstra u64 runtime; 847391e43daSPeter Zijlstra 848391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 849391e43daSPeter Zijlstra if (rt_rq->rt_throttled) 850391e43daSPeter Zijlstra balance_runtime(rt_rq); 851391e43daSPeter Zijlstra runtime = rt_rq->rt_runtime; 852391e43daSPeter Zijlstra rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime); 853391e43daSPeter Zijlstra if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) { 854391e43daSPeter Zijlstra rt_rq->rt_throttled = 0; 855391e43daSPeter Zijlstra enqueue = 1; 856391e43daSPeter Zijlstra 857391e43daSPeter Zijlstra /* 8589edfbfedSPeter Zijlstra * When we're idle and a woken (rt) task is 8599edfbfedSPeter Zijlstra * throttled check_preempt_curr() will set 8609edfbfedSPeter Zijlstra * skip_update and the time between the wakeup 8619edfbfedSPeter Zijlstra * and this unthrottle will get accounted as 8629edfbfedSPeter Zijlstra * 'runtime'. 863391e43daSPeter Zijlstra */ 864391e43daSPeter Zijlstra if (rt_rq->rt_nr_running && rq->curr == rq->idle) 8659edfbfedSPeter Zijlstra rq_clock_skip_update(rq, false); 866391e43daSPeter Zijlstra } 867391e43daSPeter Zijlstra if (rt_rq->rt_time || rt_rq->rt_nr_running) 868391e43daSPeter Zijlstra idle = 0; 869391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 870391e43daSPeter Zijlstra } else if (rt_rq->rt_nr_running) { 871391e43daSPeter Zijlstra idle = 0; 872391e43daSPeter Zijlstra if (!rt_rq_throttled(rt_rq)) 873391e43daSPeter Zijlstra enqueue = 1; 874391e43daSPeter Zijlstra } 87542c62a58SPeter Zijlstra if (rt_rq->rt_throttled) 87642c62a58SPeter Zijlstra throttled = 1; 877391e43daSPeter Zijlstra 878391e43daSPeter Zijlstra if (enqueue) 879391e43daSPeter Zijlstra sched_rt_rq_enqueue(rt_rq); 880391e43daSPeter Zijlstra raw_spin_unlock(&rq->lock); 881391e43daSPeter Zijlstra } 882391e43daSPeter Zijlstra 88342c62a58SPeter Zijlstra if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)) 88442c62a58SPeter Zijlstra return 1; 88542c62a58SPeter Zijlstra 886391e43daSPeter Zijlstra return idle; 887391e43daSPeter Zijlstra } 888391e43daSPeter Zijlstra 889391e43daSPeter Zijlstra static inline int rt_se_prio(struct sched_rt_entity *rt_se) 890391e43daSPeter Zijlstra { 891391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 892391e43daSPeter Zijlstra struct rt_rq *rt_rq = group_rt_rq(rt_se); 893391e43daSPeter Zijlstra 894391e43daSPeter Zijlstra if (rt_rq) 895391e43daSPeter Zijlstra return rt_rq->highest_prio.curr; 896391e43daSPeter Zijlstra #endif 897391e43daSPeter Zijlstra 898391e43daSPeter Zijlstra return rt_task_of(rt_se)->prio; 899391e43daSPeter Zijlstra } 900391e43daSPeter Zijlstra 901391e43daSPeter Zijlstra static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq) 902391e43daSPeter Zijlstra { 903391e43daSPeter Zijlstra u64 runtime = sched_rt_runtime(rt_rq); 904391e43daSPeter Zijlstra 905391e43daSPeter Zijlstra if (rt_rq->rt_throttled) 906391e43daSPeter Zijlstra return rt_rq_throttled(rt_rq); 907391e43daSPeter Zijlstra 9085b680fd6SShan Hai if (runtime >= sched_rt_period(rt_rq)) 909391e43daSPeter Zijlstra return 0; 910391e43daSPeter Zijlstra 911391e43daSPeter Zijlstra balance_runtime(rt_rq); 912391e43daSPeter Zijlstra runtime = sched_rt_runtime(rt_rq); 913391e43daSPeter Zijlstra if (runtime == RUNTIME_INF) 914391e43daSPeter Zijlstra return 0; 915391e43daSPeter Zijlstra 916391e43daSPeter Zijlstra if (rt_rq->rt_time > runtime) { 9177abc63b1SPeter Zijlstra struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); 9187abc63b1SPeter Zijlstra 9197abc63b1SPeter Zijlstra /* 9207abc63b1SPeter Zijlstra * Don't actually throttle groups that have no runtime assigned 9217abc63b1SPeter Zijlstra * but accrue some time due to boosting. 9227abc63b1SPeter Zijlstra */ 9237abc63b1SPeter Zijlstra if (likely(rt_b->rt_runtime)) { 924391e43daSPeter Zijlstra rt_rq->rt_throttled = 1; 925c224815dSJohn Stultz printk_deferred_once("sched: RT throttling activated\n"); 9267abc63b1SPeter Zijlstra } else { 9277abc63b1SPeter Zijlstra /* 9287abc63b1SPeter Zijlstra * In case we did anyway, make it go away, 9297abc63b1SPeter Zijlstra * replenishment is a joke, since it will replenish us 9307abc63b1SPeter Zijlstra * with exactly 0 ns. 9317abc63b1SPeter Zijlstra */ 9327abc63b1SPeter Zijlstra rt_rq->rt_time = 0; 9337abc63b1SPeter Zijlstra } 9347abc63b1SPeter Zijlstra 935391e43daSPeter Zijlstra if (rt_rq_throttled(rt_rq)) { 936391e43daSPeter Zijlstra sched_rt_rq_dequeue(rt_rq); 937391e43daSPeter Zijlstra return 1; 938391e43daSPeter Zijlstra } 939391e43daSPeter Zijlstra } 940391e43daSPeter Zijlstra 941391e43daSPeter Zijlstra return 0; 942391e43daSPeter Zijlstra } 943391e43daSPeter Zijlstra 944391e43daSPeter Zijlstra /* 945391e43daSPeter Zijlstra * Update the current task's runtime statistics. Skip current tasks that 946391e43daSPeter Zijlstra * are not in our scheduling class. 947391e43daSPeter Zijlstra */ 948391e43daSPeter Zijlstra static void update_curr_rt(struct rq *rq) 949391e43daSPeter Zijlstra { 950391e43daSPeter Zijlstra struct task_struct *curr = rq->curr; 951391e43daSPeter Zijlstra struct sched_rt_entity *rt_se = &curr->rt; 952391e43daSPeter Zijlstra u64 delta_exec; 953391e43daSPeter Zijlstra 954391e43daSPeter Zijlstra if (curr->sched_class != &rt_sched_class) 955391e43daSPeter Zijlstra return; 956391e43daSPeter Zijlstra 95778becc27SFrederic Weisbecker delta_exec = rq_clock_task(rq) - curr->se.exec_start; 958fc79e240SKirill Tkhai if (unlikely((s64)delta_exec <= 0)) 959fc79e240SKirill Tkhai return; 960391e43daSPeter Zijlstra 96158919e83SRafael J. Wysocki /* Kick cpufreq (see the comment in kernel/sched/sched.h). */ 96212bde33dSRafael J. Wysocki cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_RT); 963594dd290SWanpeng Li 96442c62a58SPeter Zijlstra schedstat_set(curr->se.statistics.exec_max, 96542c62a58SPeter Zijlstra max(curr->se.statistics.exec_max, delta_exec)); 966391e43daSPeter Zijlstra 967391e43daSPeter Zijlstra curr->se.sum_exec_runtime += delta_exec; 968391e43daSPeter Zijlstra account_group_exec_runtime(curr, delta_exec); 969391e43daSPeter Zijlstra 97078becc27SFrederic Weisbecker curr->se.exec_start = rq_clock_task(rq); 971391e43daSPeter Zijlstra cpuacct_charge(curr, delta_exec); 972391e43daSPeter Zijlstra 973391e43daSPeter Zijlstra sched_rt_avg_update(rq, delta_exec); 974391e43daSPeter Zijlstra 975391e43daSPeter Zijlstra if (!rt_bandwidth_enabled()) 976391e43daSPeter Zijlstra return; 977391e43daSPeter Zijlstra 978391e43daSPeter Zijlstra for_each_sched_rt_entity(rt_se) { 9790b07939cSGiedrius Rekasius struct rt_rq *rt_rq = rt_rq_of_se(rt_se); 980391e43daSPeter Zijlstra 981391e43daSPeter Zijlstra if (sched_rt_runtime(rt_rq) != RUNTIME_INF) { 982391e43daSPeter Zijlstra raw_spin_lock(&rt_rq->rt_runtime_lock); 983391e43daSPeter Zijlstra rt_rq->rt_time += delta_exec; 984391e43daSPeter Zijlstra if (sched_rt_runtime_exceeded(rt_rq)) 9858875125eSKirill Tkhai resched_curr(rq); 986391e43daSPeter Zijlstra raw_spin_unlock(&rt_rq->rt_runtime_lock); 987391e43daSPeter Zijlstra } 988391e43daSPeter Zijlstra } 989391e43daSPeter Zijlstra } 990391e43daSPeter Zijlstra 991f4ebcbc0SKirill Tkhai static void 992f4ebcbc0SKirill Tkhai dequeue_top_rt_rq(struct rt_rq *rt_rq) 993f4ebcbc0SKirill Tkhai { 994f4ebcbc0SKirill Tkhai struct rq *rq = rq_of_rt_rq(rt_rq); 995f4ebcbc0SKirill Tkhai 996f4ebcbc0SKirill Tkhai BUG_ON(&rq->rt != rt_rq); 997f4ebcbc0SKirill Tkhai 998f4ebcbc0SKirill Tkhai if (!rt_rq->rt_queued) 999f4ebcbc0SKirill Tkhai return; 1000f4ebcbc0SKirill Tkhai 1001f4ebcbc0SKirill Tkhai BUG_ON(!rq->nr_running); 1002f4ebcbc0SKirill Tkhai 100372465447SKirill Tkhai sub_nr_running(rq, rt_rq->rt_nr_running); 1004f4ebcbc0SKirill Tkhai rt_rq->rt_queued = 0; 1005f4ebcbc0SKirill Tkhai } 1006f4ebcbc0SKirill Tkhai 1007f4ebcbc0SKirill Tkhai static void 1008f4ebcbc0SKirill Tkhai enqueue_top_rt_rq(struct rt_rq *rt_rq) 1009f4ebcbc0SKirill Tkhai { 1010f4ebcbc0SKirill Tkhai struct rq *rq = rq_of_rt_rq(rt_rq); 1011f4ebcbc0SKirill Tkhai 1012f4ebcbc0SKirill Tkhai BUG_ON(&rq->rt != rt_rq); 1013f4ebcbc0SKirill Tkhai 1014f4ebcbc0SKirill Tkhai if (rt_rq->rt_queued) 1015f4ebcbc0SKirill Tkhai return; 1016f4ebcbc0SKirill Tkhai if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running) 1017f4ebcbc0SKirill Tkhai return; 1018f4ebcbc0SKirill Tkhai 101972465447SKirill Tkhai add_nr_running(rq, rt_rq->rt_nr_running); 1020f4ebcbc0SKirill Tkhai rt_rq->rt_queued = 1; 1021f4ebcbc0SKirill Tkhai } 1022f4ebcbc0SKirill Tkhai 1023391e43daSPeter Zijlstra #if defined CONFIG_SMP 1024391e43daSPeter Zijlstra 1025391e43daSPeter Zijlstra static void 1026391e43daSPeter Zijlstra inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) 1027391e43daSPeter Zijlstra { 1028391e43daSPeter Zijlstra struct rq *rq = rq_of_rt_rq(rt_rq); 1029391e43daSPeter Zijlstra 1030757dfcaaSKirill Tkhai #ifdef CONFIG_RT_GROUP_SCHED 1031757dfcaaSKirill Tkhai /* 1032757dfcaaSKirill Tkhai * Change rq's cpupri only if rt_rq is the top queue. 1033757dfcaaSKirill Tkhai */ 1034757dfcaaSKirill Tkhai if (&rq->rt != rt_rq) 1035757dfcaaSKirill Tkhai return; 1036757dfcaaSKirill Tkhai #endif 1037391e43daSPeter Zijlstra if (rq->online && prio < prev_prio) 1038391e43daSPeter Zijlstra cpupri_set(&rq->rd->cpupri, rq->cpu, prio); 1039391e43daSPeter Zijlstra } 1040391e43daSPeter Zijlstra 1041391e43daSPeter Zijlstra static void 1042391e43daSPeter Zijlstra dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) 1043391e43daSPeter Zijlstra { 1044391e43daSPeter Zijlstra struct rq *rq = rq_of_rt_rq(rt_rq); 1045391e43daSPeter Zijlstra 1046757dfcaaSKirill Tkhai #ifdef CONFIG_RT_GROUP_SCHED 1047757dfcaaSKirill Tkhai /* 1048757dfcaaSKirill Tkhai * Change rq's cpupri only if rt_rq is the top queue. 1049757dfcaaSKirill Tkhai */ 1050757dfcaaSKirill Tkhai if (&rq->rt != rt_rq) 1051757dfcaaSKirill Tkhai return; 1052757dfcaaSKirill Tkhai #endif 1053391e43daSPeter Zijlstra if (rq->online && rt_rq->highest_prio.curr != prev_prio) 1054391e43daSPeter Zijlstra cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr); 1055391e43daSPeter Zijlstra } 1056391e43daSPeter Zijlstra 1057391e43daSPeter Zijlstra #else /* CONFIG_SMP */ 1058391e43daSPeter Zijlstra 1059391e43daSPeter Zijlstra static inline 1060391e43daSPeter Zijlstra void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} 1061391e43daSPeter Zijlstra static inline 1062391e43daSPeter Zijlstra void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} 1063391e43daSPeter Zijlstra 1064391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 1065391e43daSPeter Zijlstra 1066391e43daSPeter Zijlstra #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED 1067391e43daSPeter Zijlstra static void 1068391e43daSPeter Zijlstra inc_rt_prio(struct rt_rq *rt_rq, int prio) 1069391e43daSPeter Zijlstra { 1070391e43daSPeter Zijlstra int prev_prio = rt_rq->highest_prio.curr; 1071391e43daSPeter Zijlstra 1072391e43daSPeter Zijlstra if (prio < prev_prio) 1073391e43daSPeter Zijlstra rt_rq->highest_prio.curr = prio; 1074391e43daSPeter Zijlstra 1075391e43daSPeter Zijlstra inc_rt_prio_smp(rt_rq, prio, prev_prio); 1076391e43daSPeter Zijlstra } 1077391e43daSPeter Zijlstra 1078391e43daSPeter Zijlstra static void 1079391e43daSPeter Zijlstra dec_rt_prio(struct rt_rq *rt_rq, int prio) 1080391e43daSPeter Zijlstra { 1081391e43daSPeter Zijlstra int prev_prio = rt_rq->highest_prio.curr; 1082391e43daSPeter Zijlstra 1083391e43daSPeter Zijlstra if (rt_rq->rt_nr_running) { 1084391e43daSPeter Zijlstra 1085391e43daSPeter Zijlstra WARN_ON(prio < prev_prio); 1086391e43daSPeter Zijlstra 1087391e43daSPeter Zijlstra /* 1088391e43daSPeter Zijlstra * This may have been our highest task, and therefore 1089391e43daSPeter Zijlstra * we may have some recomputation to do 1090391e43daSPeter Zijlstra */ 1091391e43daSPeter Zijlstra if (prio == prev_prio) { 1092391e43daSPeter Zijlstra struct rt_prio_array *array = &rt_rq->active; 1093391e43daSPeter Zijlstra 1094391e43daSPeter Zijlstra rt_rq->highest_prio.curr = 1095391e43daSPeter Zijlstra sched_find_first_bit(array->bitmap); 1096391e43daSPeter Zijlstra } 1097391e43daSPeter Zijlstra 1098391e43daSPeter Zijlstra } else 1099391e43daSPeter Zijlstra rt_rq->highest_prio.curr = MAX_RT_PRIO; 1100391e43daSPeter Zijlstra 1101391e43daSPeter Zijlstra dec_rt_prio_smp(rt_rq, prio, prev_prio); 1102391e43daSPeter Zijlstra } 1103391e43daSPeter Zijlstra 1104391e43daSPeter Zijlstra #else 1105391e43daSPeter Zijlstra 1106391e43daSPeter Zijlstra static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {} 1107391e43daSPeter Zijlstra static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {} 1108391e43daSPeter Zijlstra 1109391e43daSPeter Zijlstra #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */ 1110391e43daSPeter Zijlstra 1111391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED 1112391e43daSPeter Zijlstra 1113391e43daSPeter Zijlstra static void 1114391e43daSPeter Zijlstra inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 1115391e43daSPeter Zijlstra { 1116391e43daSPeter Zijlstra if (rt_se_boosted(rt_se)) 1117391e43daSPeter Zijlstra rt_rq->rt_nr_boosted++; 1118391e43daSPeter Zijlstra 1119391e43daSPeter Zijlstra if (rt_rq->tg) 1120391e43daSPeter Zijlstra start_rt_bandwidth(&rt_rq->tg->rt_bandwidth); 1121391e43daSPeter Zijlstra } 1122391e43daSPeter Zijlstra 1123391e43daSPeter Zijlstra static void 1124391e43daSPeter Zijlstra dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 1125391e43daSPeter Zijlstra { 1126391e43daSPeter Zijlstra if (rt_se_boosted(rt_se)) 1127391e43daSPeter Zijlstra rt_rq->rt_nr_boosted--; 1128391e43daSPeter Zijlstra 1129391e43daSPeter Zijlstra WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted); 1130391e43daSPeter Zijlstra } 1131391e43daSPeter Zijlstra 1132391e43daSPeter Zijlstra #else /* CONFIG_RT_GROUP_SCHED */ 1133391e43daSPeter Zijlstra 1134391e43daSPeter Zijlstra static void 1135391e43daSPeter Zijlstra inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 1136391e43daSPeter Zijlstra { 1137391e43daSPeter Zijlstra start_rt_bandwidth(&def_rt_bandwidth); 1138391e43daSPeter Zijlstra } 1139391e43daSPeter Zijlstra 1140391e43daSPeter Zijlstra static inline 1141391e43daSPeter Zijlstra void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {} 1142391e43daSPeter Zijlstra 1143391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */ 1144391e43daSPeter Zijlstra 1145391e43daSPeter Zijlstra static inline 114622abdef3SKirill Tkhai unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se) 114722abdef3SKirill Tkhai { 114822abdef3SKirill Tkhai struct rt_rq *group_rq = group_rt_rq(rt_se); 114922abdef3SKirill Tkhai 115022abdef3SKirill Tkhai if (group_rq) 115122abdef3SKirill Tkhai return group_rq->rt_nr_running; 115222abdef3SKirill Tkhai else 115322abdef3SKirill Tkhai return 1; 115422abdef3SKirill Tkhai } 115522abdef3SKirill Tkhai 115622abdef3SKirill Tkhai static inline 115701d36d0aSFrederic Weisbecker unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se) 115801d36d0aSFrederic Weisbecker { 115901d36d0aSFrederic Weisbecker struct rt_rq *group_rq = group_rt_rq(rt_se); 116001d36d0aSFrederic Weisbecker struct task_struct *tsk; 116101d36d0aSFrederic Weisbecker 116201d36d0aSFrederic Weisbecker if (group_rq) 116301d36d0aSFrederic Weisbecker return group_rq->rr_nr_running; 116401d36d0aSFrederic Weisbecker 116501d36d0aSFrederic Weisbecker tsk = rt_task_of(rt_se); 116601d36d0aSFrederic Weisbecker 116701d36d0aSFrederic Weisbecker return (tsk->policy == SCHED_RR) ? 1 : 0; 116801d36d0aSFrederic Weisbecker } 116901d36d0aSFrederic Weisbecker 117001d36d0aSFrederic Weisbecker static inline 1171391e43daSPeter Zijlstra void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 1172391e43daSPeter Zijlstra { 1173391e43daSPeter Zijlstra int prio = rt_se_prio(rt_se); 1174391e43daSPeter Zijlstra 1175391e43daSPeter Zijlstra WARN_ON(!rt_prio(prio)); 117622abdef3SKirill Tkhai rt_rq->rt_nr_running += rt_se_nr_running(rt_se); 117701d36d0aSFrederic Weisbecker rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se); 1178391e43daSPeter Zijlstra 1179391e43daSPeter Zijlstra inc_rt_prio(rt_rq, prio); 1180391e43daSPeter Zijlstra inc_rt_migration(rt_se, rt_rq); 1181391e43daSPeter Zijlstra inc_rt_group(rt_se, rt_rq); 1182391e43daSPeter Zijlstra } 1183391e43daSPeter Zijlstra 1184391e43daSPeter Zijlstra static inline 1185391e43daSPeter Zijlstra void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) 1186391e43daSPeter Zijlstra { 1187391e43daSPeter Zijlstra WARN_ON(!rt_prio(rt_se_prio(rt_se))); 1188391e43daSPeter Zijlstra WARN_ON(!rt_rq->rt_nr_running); 118922abdef3SKirill Tkhai rt_rq->rt_nr_running -= rt_se_nr_running(rt_se); 119001d36d0aSFrederic Weisbecker rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se); 1191391e43daSPeter Zijlstra 1192391e43daSPeter Zijlstra dec_rt_prio(rt_rq, rt_se_prio(rt_se)); 1193391e43daSPeter Zijlstra dec_rt_migration(rt_se, rt_rq); 1194391e43daSPeter Zijlstra dec_rt_group(rt_se, rt_rq); 1195391e43daSPeter Zijlstra } 1196391e43daSPeter Zijlstra 1197ff77e468SPeter Zijlstra /* 1198ff77e468SPeter Zijlstra * Change rt_se->run_list location unless SAVE && !MOVE 1199ff77e468SPeter Zijlstra * 1200ff77e468SPeter Zijlstra * assumes ENQUEUE/DEQUEUE flags match 1201ff77e468SPeter Zijlstra */ 1202ff77e468SPeter Zijlstra static inline bool move_entity(unsigned int flags) 1203ff77e468SPeter Zijlstra { 1204ff77e468SPeter Zijlstra if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE) 1205ff77e468SPeter Zijlstra return false; 1206ff77e468SPeter Zijlstra 1207ff77e468SPeter Zijlstra return true; 1208ff77e468SPeter Zijlstra } 1209ff77e468SPeter Zijlstra 1210ff77e468SPeter Zijlstra static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array) 1211ff77e468SPeter Zijlstra { 1212ff77e468SPeter Zijlstra list_del_init(&rt_se->run_list); 1213ff77e468SPeter Zijlstra 1214ff77e468SPeter Zijlstra if (list_empty(array->queue + rt_se_prio(rt_se))) 1215ff77e468SPeter Zijlstra __clear_bit(rt_se_prio(rt_se), array->bitmap); 1216ff77e468SPeter Zijlstra 1217ff77e468SPeter Zijlstra rt_se->on_list = 0; 1218ff77e468SPeter Zijlstra } 1219ff77e468SPeter Zijlstra 1220ff77e468SPeter Zijlstra static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) 1221391e43daSPeter Zijlstra { 1222391e43daSPeter Zijlstra struct rt_rq *rt_rq = rt_rq_of_se(rt_se); 1223391e43daSPeter Zijlstra struct rt_prio_array *array = &rt_rq->active; 1224391e43daSPeter Zijlstra struct rt_rq *group_rq = group_rt_rq(rt_se); 1225391e43daSPeter Zijlstra struct list_head *queue = array->queue + rt_se_prio(rt_se); 1226391e43daSPeter Zijlstra 1227391e43daSPeter Zijlstra /* 1228391e43daSPeter Zijlstra * Don't enqueue the group if its throttled, or when empty. 1229391e43daSPeter Zijlstra * The latter is a consequence of the former when a child group 1230391e43daSPeter Zijlstra * get throttled and the current group doesn't have any other 1231391e43daSPeter Zijlstra * active members. 1232391e43daSPeter Zijlstra */ 1233ff77e468SPeter Zijlstra if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) { 1234ff77e468SPeter Zijlstra if (rt_se->on_list) 1235ff77e468SPeter Zijlstra __delist_rt_entity(rt_se, array); 1236391e43daSPeter Zijlstra return; 1237ff77e468SPeter Zijlstra } 1238391e43daSPeter Zijlstra 1239ff77e468SPeter Zijlstra if (move_entity(flags)) { 1240ff77e468SPeter Zijlstra WARN_ON_ONCE(rt_se->on_list); 1241ff77e468SPeter Zijlstra if (flags & ENQUEUE_HEAD) 1242391e43daSPeter Zijlstra list_add(&rt_se->run_list, queue); 1243391e43daSPeter Zijlstra else 1244391e43daSPeter Zijlstra list_add_tail(&rt_se->run_list, queue); 1245ff77e468SPeter Zijlstra 1246391e43daSPeter Zijlstra __set_bit(rt_se_prio(rt_se), array->bitmap); 1247ff77e468SPeter Zijlstra rt_se->on_list = 1; 1248ff77e468SPeter Zijlstra } 1249ff77e468SPeter Zijlstra rt_se->on_rq = 1; 1250391e43daSPeter Zijlstra 1251391e43daSPeter Zijlstra inc_rt_tasks(rt_se, rt_rq); 1252391e43daSPeter Zijlstra } 1253391e43daSPeter Zijlstra 1254ff77e468SPeter Zijlstra static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) 1255391e43daSPeter Zijlstra { 1256391e43daSPeter Zijlstra struct rt_rq *rt_rq = rt_rq_of_se(rt_se); 1257391e43daSPeter Zijlstra struct rt_prio_array *array = &rt_rq->active; 1258391e43daSPeter Zijlstra 1259ff77e468SPeter Zijlstra if (move_entity(flags)) { 1260ff77e468SPeter Zijlstra WARN_ON_ONCE(!rt_se->on_list); 1261ff77e468SPeter Zijlstra __delist_rt_entity(rt_se, array); 1262ff77e468SPeter Zijlstra } 1263ff77e468SPeter Zijlstra rt_se->on_rq = 0; 1264391e43daSPeter Zijlstra 1265391e43daSPeter Zijlstra dec_rt_tasks(rt_se, rt_rq); 1266391e43daSPeter Zijlstra } 1267391e43daSPeter Zijlstra 1268391e43daSPeter Zijlstra /* 1269391e43daSPeter Zijlstra * Because the prio of an upper entry depends on the lower 1270391e43daSPeter Zijlstra * entries, we must remove entries top - down. 1271391e43daSPeter Zijlstra */ 1272ff77e468SPeter Zijlstra static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags) 1273391e43daSPeter Zijlstra { 1274391e43daSPeter Zijlstra struct sched_rt_entity *back = NULL; 1275391e43daSPeter Zijlstra 1276391e43daSPeter Zijlstra for_each_sched_rt_entity(rt_se) { 1277391e43daSPeter Zijlstra rt_se->back = back; 1278391e43daSPeter Zijlstra back = rt_se; 1279391e43daSPeter Zijlstra } 1280391e43daSPeter Zijlstra 1281f4ebcbc0SKirill Tkhai dequeue_top_rt_rq(rt_rq_of_se(back)); 1282f4ebcbc0SKirill Tkhai 1283391e43daSPeter Zijlstra for (rt_se = back; rt_se; rt_se = rt_se->back) { 1284391e43daSPeter Zijlstra if (on_rt_rq(rt_se)) 1285ff77e468SPeter Zijlstra __dequeue_rt_entity(rt_se, flags); 1286391e43daSPeter Zijlstra } 1287391e43daSPeter Zijlstra } 1288391e43daSPeter Zijlstra 1289ff77e468SPeter Zijlstra static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) 1290391e43daSPeter Zijlstra { 1291f4ebcbc0SKirill Tkhai struct rq *rq = rq_of_rt_se(rt_se); 1292f4ebcbc0SKirill Tkhai 1293ff77e468SPeter Zijlstra dequeue_rt_stack(rt_se, flags); 1294391e43daSPeter Zijlstra for_each_sched_rt_entity(rt_se) 1295ff77e468SPeter Zijlstra __enqueue_rt_entity(rt_se, flags); 1296f4ebcbc0SKirill Tkhai enqueue_top_rt_rq(&rq->rt); 1297391e43daSPeter Zijlstra } 1298391e43daSPeter Zijlstra 1299ff77e468SPeter Zijlstra static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags) 1300391e43daSPeter Zijlstra { 1301f4ebcbc0SKirill Tkhai struct rq *rq = rq_of_rt_se(rt_se); 1302f4ebcbc0SKirill Tkhai 1303ff77e468SPeter Zijlstra dequeue_rt_stack(rt_se, flags); 1304391e43daSPeter Zijlstra 1305391e43daSPeter Zijlstra for_each_sched_rt_entity(rt_se) { 1306391e43daSPeter Zijlstra struct rt_rq *rt_rq = group_rt_rq(rt_se); 1307391e43daSPeter Zijlstra 1308391e43daSPeter Zijlstra if (rt_rq && rt_rq->rt_nr_running) 1309ff77e468SPeter Zijlstra __enqueue_rt_entity(rt_se, flags); 1310391e43daSPeter Zijlstra } 1311f4ebcbc0SKirill Tkhai enqueue_top_rt_rq(&rq->rt); 1312391e43daSPeter Zijlstra } 1313391e43daSPeter Zijlstra 1314391e43daSPeter Zijlstra /* 1315391e43daSPeter Zijlstra * Adding/removing a task to/from a priority array: 1316391e43daSPeter Zijlstra */ 1317391e43daSPeter Zijlstra static void 1318391e43daSPeter Zijlstra enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags) 1319391e43daSPeter Zijlstra { 1320391e43daSPeter Zijlstra struct sched_rt_entity *rt_se = &p->rt; 1321391e43daSPeter Zijlstra 1322391e43daSPeter Zijlstra if (flags & ENQUEUE_WAKEUP) 1323391e43daSPeter Zijlstra rt_se->timeout = 0; 1324391e43daSPeter Zijlstra 1325ff77e468SPeter Zijlstra enqueue_rt_entity(rt_se, flags); 1326391e43daSPeter Zijlstra 132750605ffbSThomas Gleixner if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1) 1328391e43daSPeter Zijlstra enqueue_pushable_task(rq, p); 1329391e43daSPeter Zijlstra } 1330391e43daSPeter Zijlstra 1331391e43daSPeter Zijlstra static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags) 1332391e43daSPeter Zijlstra { 1333391e43daSPeter Zijlstra struct sched_rt_entity *rt_se = &p->rt; 1334391e43daSPeter Zijlstra 1335391e43daSPeter Zijlstra update_curr_rt(rq); 1336ff77e468SPeter Zijlstra dequeue_rt_entity(rt_se, flags); 1337391e43daSPeter Zijlstra 1338391e43daSPeter Zijlstra dequeue_pushable_task(rq, p); 1339391e43daSPeter Zijlstra } 1340391e43daSPeter Zijlstra 1341391e43daSPeter Zijlstra /* 1342391e43daSPeter Zijlstra * Put task to the head or the end of the run list without the overhead of 1343391e43daSPeter Zijlstra * dequeue followed by enqueue. 1344391e43daSPeter Zijlstra */ 1345391e43daSPeter Zijlstra static void 1346391e43daSPeter Zijlstra requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head) 1347391e43daSPeter Zijlstra { 1348391e43daSPeter Zijlstra if (on_rt_rq(rt_se)) { 1349391e43daSPeter Zijlstra struct rt_prio_array *array = &rt_rq->active; 1350391e43daSPeter Zijlstra struct list_head *queue = array->queue + rt_se_prio(rt_se); 1351391e43daSPeter Zijlstra 1352391e43daSPeter Zijlstra if (head) 1353391e43daSPeter Zijlstra list_move(&rt_se->run_list, queue); 1354391e43daSPeter Zijlstra else 1355391e43daSPeter Zijlstra list_move_tail(&rt_se->run_list, queue); 1356391e43daSPeter Zijlstra } 1357391e43daSPeter Zijlstra } 1358391e43daSPeter Zijlstra 1359391e43daSPeter Zijlstra static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head) 1360391e43daSPeter Zijlstra { 1361391e43daSPeter Zijlstra struct sched_rt_entity *rt_se = &p->rt; 1362391e43daSPeter Zijlstra struct rt_rq *rt_rq; 1363391e43daSPeter Zijlstra 1364391e43daSPeter Zijlstra for_each_sched_rt_entity(rt_se) { 1365391e43daSPeter Zijlstra rt_rq = rt_rq_of_se(rt_se); 1366391e43daSPeter Zijlstra requeue_rt_entity(rt_rq, rt_se, head); 1367391e43daSPeter Zijlstra } 1368391e43daSPeter Zijlstra } 1369391e43daSPeter Zijlstra 1370391e43daSPeter Zijlstra static void yield_task_rt(struct rq *rq) 1371391e43daSPeter Zijlstra { 1372391e43daSPeter Zijlstra requeue_task_rt(rq, rq->curr, 0); 1373391e43daSPeter Zijlstra } 1374391e43daSPeter Zijlstra 1375391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1376391e43daSPeter Zijlstra static int find_lowest_rq(struct task_struct *task); 1377391e43daSPeter Zijlstra 1378391e43daSPeter Zijlstra static int 1379ac66f547SPeter Zijlstra select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags) 1380391e43daSPeter Zijlstra { 1381391e43daSPeter Zijlstra struct task_struct *curr; 1382391e43daSPeter Zijlstra struct rq *rq; 1383391e43daSPeter Zijlstra 1384391e43daSPeter Zijlstra /* For anything but wake ups, just return the task_cpu */ 1385391e43daSPeter Zijlstra if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK) 1386391e43daSPeter Zijlstra goto out; 1387391e43daSPeter Zijlstra 1388391e43daSPeter Zijlstra rq = cpu_rq(cpu); 1389391e43daSPeter Zijlstra 1390391e43daSPeter Zijlstra rcu_read_lock(); 1391316c1608SJason Low curr = READ_ONCE(rq->curr); /* unlocked access */ 1392391e43daSPeter Zijlstra 1393391e43daSPeter Zijlstra /* 1394391e43daSPeter Zijlstra * If the current task on @p's runqueue is an RT task, then 1395391e43daSPeter Zijlstra * try to see if we can wake this RT task up on another 1396391e43daSPeter Zijlstra * runqueue. Otherwise simply start this RT task 1397391e43daSPeter Zijlstra * on its current runqueue. 1398391e43daSPeter Zijlstra * 1399391e43daSPeter Zijlstra * We want to avoid overloading runqueues. If the woken 1400391e43daSPeter Zijlstra * task is a higher priority, then it will stay on this CPU 1401391e43daSPeter Zijlstra * and the lower prio task should be moved to another CPU. 1402391e43daSPeter Zijlstra * Even though this will probably make the lower prio task 1403391e43daSPeter Zijlstra * lose its cache, we do not want to bounce a higher task 1404391e43daSPeter Zijlstra * around just because it gave up its CPU, perhaps for a 1405391e43daSPeter Zijlstra * lock? 1406391e43daSPeter Zijlstra * 1407391e43daSPeter Zijlstra * For equal prio tasks, we just let the scheduler sort it out. 1408391e43daSPeter Zijlstra * 1409391e43daSPeter Zijlstra * Otherwise, just let it ride on the affined RQ and the 1410391e43daSPeter Zijlstra * post-schedule router will push the preempted task away 1411391e43daSPeter Zijlstra * 1412391e43daSPeter Zijlstra * This test is optimistic, if we get it wrong the load-balancer 1413391e43daSPeter Zijlstra * will have to sort it out. 1414391e43daSPeter Zijlstra */ 1415391e43daSPeter Zijlstra if (curr && unlikely(rt_task(curr)) && 141650605ffbSThomas Gleixner (tsk_nr_cpus_allowed(curr) < 2 || 14176bfa687cSShawn Bohrer curr->prio <= p->prio)) { 1418391e43daSPeter Zijlstra int target = find_lowest_rq(p); 1419391e43daSPeter Zijlstra 142080e3d87bSTim Chen /* 142180e3d87bSTim Chen * Don't bother moving it if the destination CPU is 142280e3d87bSTim Chen * not running a lower priority task. 142380e3d87bSTim Chen */ 142480e3d87bSTim Chen if (target != -1 && 142580e3d87bSTim Chen p->prio < cpu_rq(target)->rt.highest_prio.curr) 1426391e43daSPeter Zijlstra cpu = target; 1427391e43daSPeter Zijlstra } 1428391e43daSPeter Zijlstra rcu_read_unlock(); 1429391e43daSPeter Zijlstra 1430391e43daSPeter Zijlstra out: 1431391e43daSPeter Zijlstra return cpu; 1432391e43daSPeter Zijlstra } 1433391e43daSPeter Zijlstra 1434391e43daSPeter Zijlstra static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p) 1435391e43daSPeter Zijlstra { 1436308a623aSWanpeng Li /* 1437308a623aSWanpeng Li * Current can't be migrated, useless to reschedule, 1438308a623aSWanpeng Li * let's hope p can move out. 1439308a623aSWanpeng Li */ 144050605ffbSThomas Gleixner if (tsk_nr_cpus_allowed(rq->curr) == 1 || 1441308a623aSWanpeng Li !cpupri_find(&rq->rd->cpupri, rq->curr, NULL)) 1442391e43daSPeter Zijlstra return; 1443391e43daSPeter Zijlstra 1444308a623aSWanpeng Li /* 1445308a623aSWanpeng Li * p is migratable, so let's not schedule it and 1446308a623aSWanpeng Li * see if it is pushed or pulled somewhere else. 1447308a623aSWanpeng Li */ 144850605ffbSThomas Gleixner if (tsk_nr_cpus_allowed(p) != 1 1449391e43daSPeter Zijlstra && cpupri_find(&rq->rd->cpupri, p, NULL)) 1450391e43daSPeter Zijlstra return; 1451391e43daSPeter Zijlstra 1452391e43daSPeter Zijlstra /* 1453391e43daSPeter Zijlstra * There appears to be other cpus that can accept 1454391e43daSPeter Zijlstra * current and none to run 'p', so lets reschedule 1455391e43daSPeter Zijlstra * to try and push current away: 1456391e43daSPeter Zijlstra */ 1457391e43daSPeter Zijlstra requeue_task_rt(rq, p, 1); 14588875125eSKirill Tkhai resched_curr(rq); 1459391e43daSPeter Zijlstra } 1460391e43daSPeter Zijlstra 1461391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 1462391e43daSPeter Zijlstra 1463391e43daSPeter Zijlstra /* 1464391e43daSPeter Zijlstra * Preempt the current task with a newly woken task if needed: 1465391e43daSPeter Zijlstra */ 1466391e43daSPeter Zijlstra static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags) 1467391e43daSPeter Zijlstra { 1468391e43daSPeter Zijlstra if (p->prio < rq->curr->prio) { 14698875125eSKirill Tkhai resched_curr(rq); 1470391e43daSPeter Zijlstra return; 1471391e43daSPeter Zijlstra } 1472391e43daSPeter Zijlstra 1473391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1474391e43daSPeter Zijlstra /* 1475391e43daSPeter Zijlstra * If: 1476391e43daSPeter Zijlstra * 1477391e43daSPeter Zijlstra * - the newly woken task is of equal priority to the current task 1478391e43daSPeter Zijlstra * - the newly woken task is non-migratable while current is migratable 1479391e43daSPeter Zijlstra * - current will be preempted on the next reschedule 1480391e43daSPeter Zijlstra * 1481391e43daSPeter Zijlstra * we should check to see if current can readily move to a different 1482391e43daSPeter Zijlstra * cpu. If so, we will reschedule to allow the push logic to try 1483391e43daSPeter Zijlstra * to move current somewhere else, making room for our non-migratable 1484391e43daSPeter Zijlstra * task. 1485391e43daSPeter Zijlstra */ 1486391e43daSPeter Zijlstra if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr)) 1487391e43daSPeter Zijlstra check_preempt_equal_prio(rq, p); 1488391e43daSPeter Zijlstra #endif 1489391e43daSPeter Zijlstra } 1490391e43daSPeter Zijlstra 1491391e43daSPeter Zijlstra static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq, 1492391e43daSPeter Zijlstra struct rt_rq *rt_rq) 1493391e43daSPeter Zijlstra { 1494391e43daSPeter Zijlstra struct rt_prio_array *array = &rt_rq->active; 1495391e43daSPeter Zijlstra struct sched_rt_entity *next = NULL; 1496391e43daSPeter Zijlstra struct list_head *queue; 1497391e43daSPeter Zijlstra int idx; 1498391e43daSPeter Zijlstra 1499391e43daSPeter Zijlstra idx = sched_find_first_bit(array->bitmap); 1500391e43daSPeter Zijlstra BUG_ON(idx >= MAX_RT_PRIO); 1501391e43daSPeter Zijlstra 1502391e43daSPeter Zijlstra queue = array->queue + idx; 1503391e43daSPeter Zijlstra next = list_entry(queue->next, struct sched_rt_entity, run_list); 1504391e43daSPeter Zijlstra 1505391e43daSPeter Zijlstra return next; 1506391e43daSPeter Zijlstra } 1507391e43daSPeter Zijlstra 1508391e43daSPeter Zijlstra static struct task_struct *_pick_next_task_rt(struct rq *rq) 1509391e43daSPeter Zijlstra { 1510391e43daSPeter Zijlstra struct sched_rt_entity *rt_se; 1511391e43daSPeter Zijlstra struct task_struct *p; 1512606dba2eSPeter Zijlstra struct rt_rq *rt_rq = &rq->rt; 1513391e43daSPeter Zijlstra 1514391e43daSPeter Zijlstra do { 1515391e43daSPeter Zijlstra rt_se = pick_next_rt_entity(rq, rt_rq); 1516391e43daSPeter Zijlstra BUG_ON(!rt_se); 1517391e43daSPeter Zijlstra rt_rq = group_rt_rq(rt_se); 1518391e43daSPeter Zijlstra } while (rt_rq); 1519391e43daSPeter Zijlstra 1520391e43daSPeter Zijlstra p = rt_task_of(rt_se); 152178becc27SFrederic Weisbecker p->se.exec_start = rq_clock_task(rq); 1522391e43daSPeter Zijlstra 1523391e43daSPeter Zijlstra return p; 1524391e43daSPeter Zijlstra } 1525391e43daSPeter Zijlstra 1526606dba2eSPeter Zijlstra static struct task_struct * 1527d8ac8971SMatt Fleming pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) 1528391e43daSPeter Zijlstra { 1529606dba2eSPeter Zijlstra struct task_struct *p; 1530606dba2eSPeter Zijlstra struct rt_rq *rt_rq = &rq->rt; 1531606dba2eSPeter Zijlstra 153237e117c0SPeter Zijlstra if (need_pull_rt_task(rq, prev)) { 1533cbce1a68SPeter Zijlstra /* 1534cbce1a68SPeter Zijlstra * This is OK, because current is on_cpu, which avoids it being 1535cbce1a68SPeter Zijlstra * picked for load-balance and preemption/IRQs are still 1536cbce1a68SPeter Zijlstra * disabled avoiding further scheduler activity on it and we're 1537cbce1a68SPeter Zijlstra * being very careful to re-start the picking loop. 1538cbce1a68SPeter Zijlstra */ 1539d8ac8971SMatt Fleming rq_unpin_lock(rq, rf); 154038033c37SPeter Zijlstra pull_rt_task(rq); 1541d8ac8971SMatt Fleming rq_repin_lock(rq, rf); 154237e117c0SPeter Zijlstra /* 154337e117c0SPeter Zijlstra * pull_rt_task() can drop (and re-acquire) rq->lock; this 1544a1d9a323SKirill Tkhai * means a dl or stop task can slip in, in which case we need 1545a1d9a323SKirill Tkhai * to re-start task selection. 154637e117c0SPeter Zijlstra */ 1547da0c1e65SKirill Tkhai if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) || 1548a1d9a323SKirill Tkhai rq->dl.dl_nr_running)) 154937e117c0SPeter Zijlstra return RETRY_TASK; 155037e117c0SPeter Zijlstra } 155138033c37SPeter Zijlstra 1552734ff2a7SKirill Tkhai /* 1553734ff2a7SKirill Tkhai * We may dequeue prev's rt_rq in put_prev_task(). 1554734ff2a7SKirill Tkhai * So, we update time before rt_nr_running check. 1555734ff2a7SKirill Tkhai */ 1556734ff2a7SKirill Tkhai if (prev->sched_class == &rt_sched_class) 1557734ff2a7SKirill Tkhai update_curr_rt(rq); 1558734ff2a7SKirill Tkhai 1559f4ebcbc0SKirill Tkhai if (!rt_rq->rt_queued) 1560606dba2eSPeter Zijlstra return NULL; 1561606dba2eSPeter Zijlstra 15623f1d2a31SPeter Zijlstra put_prev_task(rq, prev); 1563606dba2eSPeter Zijlstra 1564606dba2eSPeter Zijlstra p = _pick_next_task_rt(rq); 1565391e43daSPeter Zijlstra 1566391e43daSPeter Zijlstra /* The running task is never eligible for pushing */ 1567391e43daSPeter Zijlstra dequeue_pushable_task(rq, p); 1568391e43daSPeter Zijlstra 1569e3fca9e7SPeter Zijlstra queue_push_tasks(rq); 1570391e43daSPeter Zijlstra 1571391e43daSPeter Zijlstra return p; 1572391e43daSPeter Zijlstra } 1573391e43daSPeter Zijlstra 1574391e43daSPeter Zijlstra static void put_prev_task_rt(struct rq *rq, struct task_struct *p) 1575391e43daSPeter Zijlstra { 1576391e43daSPeter Zijlstra update_curr_rt(rq); 1577391e43daSPeter Zijlstra 1578391e43daSPeter Zijlstra /* 1579391e43daSPeter Zijlstra * The previous task needs to be made eligible for pushing 1580391e43daSPeter Zijlstra * if it is still active 1581391e43daSPeter Zijlstra */ 158250605ffbSThomas Gleixner if (on_rt_rq(&p->rt) && tsk_nr_cpus_allowed(p) > 1) 1583391e43daSPeter Zijlstra enqueue_pushable_task(rq, p); 1584391e43daSPeter Zijlstra } 1585391e43daSPeter Zijlstra 1586391e43daSPeter Zijlstra #ifdef CONFIG_SMP 1587391e43daSPeter Zijlstra 1588391e43daSPeter Zijlstra /* Only try algorithms three times */ 1589391e43daSPeter Zijlstra #define RT_MAX_TRIES 3 1590391e43daSPeter Zijlstra 1591391e43daSPeter Zijlstra static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu) 1592391e43daSPeter Zijlstra { 1593391e43daSPeter Zijlstra if (!task_running(rq, p) && 159460334cafSKirill Tkhai cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) 1595391e43daSPeter Zijlstra return 1; 1596391e43daSPeter Zijlstra return 0; 1597391e43daSPeter Zijlstra } 1598391e43daSPeter Zijlstra 1599e23ee747SKirill Tkhai /* 1600e23ee747SKirill Tkhai * Return the highest pushable rq's task, which is suitable to be executed 1601e23ee747SKirill Tkhai * on the cpu, NULL otherwise 1602e23ee747SKirill Tkhai */ 1603e23ee747SKirill Tkhai static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu) 1604391e43daSPeter Zijlstra { 1605e23ee747SKirill Tkhai struct plist_head *head = &rq->rt.pushable_tasks; 1606391e43daSPeter Zijlstra struct task_struct *p; 1607391e43daSPeter Zijlstra 1608e23ee747SKirill Tkhai if (!has_pushable_tasks(rq)) 1609e23ee747SKirill Tkhai return NULL; 1610391e43daSPeter Zijlstra 1611e23ee747SKirill Tkhai plist_for_each_entry(p, head, pushable_tasks) { 1612e23ee747SKirill Tkhai if (pick_rt_task(rq, p, cpu)) 1613e23ee747SKirill Tkhai return p; 1614391e43daSPeter Zijlstra } 1615391e43daSPeter Zijlstra 1616e23ee747SKirill Tkhai return NULL; 1617391e43daSPeter Zijlstra } 1618391e43daSPeter Zijlstra 1619391e43daSPeter Zijlstra static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask); 1620391e43daSPeter Zijlstra 1621391e43daSPeter Zijlstra static int find_lowest_rq(struct task_struct *task) 1622391e43daSPeter Zijlstra { 1623391e43daSPeter Zijlstra struct sched_domain *sd; 16244ba29684SChristoph Lameter struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask); 1625391e43daSPeter Zijlstra int this_cpu = smp_processor_id(); 1626391e43daSPeter Zijlstra int cpu = task_cpu(task); 1627391e43daSPeter Zijlstra 1628391e43daSPeter Zijlstra /* Make sure the mask is initialized first */ 1629391e43daSPeter Zijlstra if (unlikely(!lowest_mask)) 1630391e43daSPeter Zijlstra return -1; 1631391e43daSPeter Zijlstra 163250605ffbSThomas Gleixner if (tsk_nr_cpus_allowed(task) == 1) 1633391e43daSPeter Zijlstra return -1; /* No other targets possible */ 1634391e43daSPeter Zijlstra 1635391e43daSPeter Zijlstra if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask)) 1636391e43daSPeter Zijlstra return -1; /* No targets found */ 1637391e43daSPeter Zijlstra 1638391e43daSPeter Zijlstra /* 1639391e43daSPeter Zijlstra * At this point we have built a mask of cpus representing the 1640391e43daSPeter Zijlstra * lowest priority tasks in the system. Now we want to elect 1641391e43daSPeter Zijlstra * the best one based on our affinity and topology. 1642391e43daSPeter Zijlstra * 1643391e43daSPeter Zijlstra * We prioritize the last cpu that the task executed on since 1644391e43daSPeter Zijlstra * it is most likely cache-hot in that location. 1645391e43daSPeter Zijlstra */ 1646391e43daSPeter Zijlstra if (cpumask_test_cpu(cpu, lowest_mask)) 1647391e43daSPeter Zijlstra return cpu; 1648391e43daSPeter Zijlstra 1649391e43daSPeter Zijlstra /* 1650391e43daSPeter Zijlstra * Otherwise, we consult the sched_domains span maps to figure 1651391e43daSPeter Zijlstra * out which cpu is logically closest to our hot cache data. 1652391e43daSPeter Zijlstra */ 1653391e43daSPeter Zijlstra if (!cpumask_test_cpu(this_cpu, lowest_mask)) 1654391e43daSPeter Zijlstra this_cpu = -1; /* Skip this_cpu opt if not among lowest */ 1655391e43daSPeter Zijlstra 1656391e43daSPeter Zijlstra rcu_read_lock(); 1657391e43daSPeter Zijlstra for_each_domain(cpu, sd) { 1658391e43daSPeter Zijlstra if (sd->flags & SD_WAKE_AFFINE) { 1659391e43daSPeter Zijlstra int best_cpu; 1660391e43daSPeter Zijlstra 1661391e43daSPeter Zijlstra /* 1662391e43daSPeter Zijlstra * "this_cpu" is cheaper to preempt than a 1663391e43daSPeter Zijlstra * remote processor. 1664391e43daSPeter Zijlstra */ 1665391e43daSPeter Zijlstra if (this_cpu != -1 && 1666391e43daSPeter Zijlstra cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { 1667391e43daSPeter Zijlstra rcu_read_unlock(); 1668391e43daSPeter Zijlstra return this_cpu; 1669391e43daSPeter Zijlstra } 1670391e43daSPeter Zijlstra 1671391e43daSPeter Zijlstra best_cpu = cpumask_first_and(lowest_mask, 1672391e43daSPeter Zijlstra sched_domain_span(sd)); 1673391e43daSPeter Zijlstra if (best_cpu < nr_cpu_ids) { 1674391e43daSPeter Zijlstra rcu_read_unlock(); 1675391e43daSPeter Zijlstra return best_cpu; 1676391e43daSPeter Zijlstra } 1677391e43daSPeter Zijlstra } 1678391e43daSPeter Zijlstra } 1679391e43daSPeter Zijlstra rcu_read_unlock(); 1680391e43daSPeter Zijlstra 1681391e43daSPeter Zijlstra /* 1682391e43daSPeter Zijlstra * And finally, if there were no matches within the domains 1683391e43daSPeter Zijlstra * just give the caller *something* to work with from the compatible 1684391e43daSPeter Zijlstra * locations. 1685391e43daSPeter Zijlstra */ 1686391e43daSPeter Zijlstra if (this_cpu != -1) 1687391e43daSPeter Zijlstra return this_cpu; 1688391e43daSPeter Zijlstra 1689391e43daSPeter Zijlstra cpu = cpumask_any(lowest_mask); 1690391e43daSPeter Zijlstra if (cpu < nr_cpu_ids) 1691391e43daSPeter Zijlstra return cpu; 1692391e43daSPeter Zijlstra return -1; 1693391e43daSPeter Zijlstra } 1694391e43daSPeter Zijlstra 1695391e43daSPeter Zijlstra /* Will lock the rq it finds */ 1696391e43daSPeter Zijlstra static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq) 1697391e43daSPeter Zijlstra { 1698391e43daSPeter Zijlstra struct rq *lowest_rq = NULL; 1699391e43daSPeter Zijlstra int tries; 1700391e43daSPeter Zijlstra int cpu; 1701391e43daSPeter Zijlstra 1702391e43daSPeter Zijlstra for (tries = 0; tries < RT_MAX_TRIES; tries++) { 1703391e43daSPeter Zijlstra cpu = find_lowest_rq(task); 1704391e43daSPeter Zijlstra 1705391e43daSPeter Zijlstra if ((cpu == -1) || (cpu == rq->cpu)) 1706391e43daSPeter Zijlstra break; 1707391e43daSPeter Zijlstra 1708391e43daSPeter Zijlstra lowest_rq = cpu_rq(cpu); 1709391e43daSPeter Zijlstra 171080e3d87bSTim Chen if (lowest_rq->rt.highest_prio.curr <= task->prio) { 171180e3d87bSTim Chen /* 171280e3d87bSTim Chen * Target rq has tasks of equal or higher priority, 171380e3d87bSTim Chen * retrying does not release any lock and is unlikely 171480e3d87bSTim Chen * to yield a different result. 171580e3d87bSTim Chen */ 171680e3d87bSTim Chen lowest_rq = NULL; 171780e3d87bSTim Chen break; 171880e3d87bSTim Chen } 171980e3d87bSTim Chen 1720391e43daSPeter Zijlstra /* if the prio of this runqueue changed, try again */ 1721391e43daSPeter Zijlstra if (double_lock_balance(rq, lowest_rq)) { 1722391e43daSPeter Zijlstra /* 1723391e43daSPeter Zijlstra * We had to unlock the run queue. In 1724391e43daSPeter Zijlstra * the mean time, task could have 1725391e43daSPeter Zijlstra * migrated already or had its affinity changed. 1726391e43daSPeter Zijlstra * Also make sure that it wasn't scheduled on its rq. 1727391e43daSPeter Zijlstra */ 1728391e43daSPeter Zijlstra if (unlikely(task_rq(task) != rq || 1729391e43daSPeter Zijlstra !cpumask_test_cpu(lowest_rq->cpu, 1730391e43daSPeter Zijlstra tsk_cpus_allowed(task)) || 1731391e43daSPeter Zijlstra task_running(rq, task) || 173213b5ab02SXunlei Pang !rt_task(task) || 1733da0c1e65SKirill Tkhai !task_on_rq_queued(task))) { 1734391e43daSPeter Zijlstra 17357f1b4393SPeter Zijlstra double_unlock_balance(rq, lowest_rq); 1736391e43daSPeter Zijlstra lowest_rq = NULL; 1737391e43daSPeter Zijlstra break; 1738391e43daSPeter Zijlstra } 1739391e43daSPeter Zijlstra } 1740391e43daSPeter Zijlstra 1741391e43daSPeter Zijlstra /* If this rq is still suitable use it. */ 1742391e43daSPeter Zijlstra if (lowest_rq->rt.highest_prio.curr > task->prio) 1743391e43daSPeter Zijlstra break; 1744391e43daSPeter Zijlstra 1745391e43daSPeter Zijlstra /* try again */ 1746391e43daSPeter Zijlstra double_unlock_balance(rq, lowest_rq); 1747391e43daSPeter Zijlstra lowest_rq = NULL; 1748391e43daSPeter Zijlstra } 1749391e43daSPeter Zijlstra 1750391e43daSPeter Zijlstra return lowest_rq; 1751391e43daSPeter Zijlstra } 1752391e43daSPeter Zijlstra 1753391e43daSPeter Zijlstra static struct task_struct *pick_next_pushable_task(struct rq *rq) 1754391e43daSPeter Zijlstra { 1755391e43daSPeter Zijlstra struct task_struct *p; 1756391e43daSPeter Zijlstra 1757391e43daSPeter Zijlstra if (!has_pushable_tasks(rq)) 1758391e43daSPeter Zijlstra return NULL; 1759391e43daSPeter Zijlstra 1760391e43daSPeter Zijlstra p = plist_first_entry(&rq->rt.pushable_tasks, 1761391e43daSPeter Zijlstra struct task_struct, pushable_tasks); 1762391e43daSPeter Zijlstra 1763391e43daSPeter Zijlstra BUG_ON(rq->cpu != task_cpu(p)); 1764391e43daSPeter Zijlstra BUG_ON(task_current(rq, p)); 176550605ffbSThomas Gleixner BUG_ON(tsk_nr_cpus_allowed(p) <= 1); 1766391e43daSPeter Zijlstra 1767da0c1e65SKirill Tkhai BUG_ON(!task_on_rq_queued(p)); 1768391e43daSPeter Zijlstra BUG_ON(!rt_task(p)); 1769391e43daSPeter Zijlstra 1770391e43daSPeter Zijlstra return p; 1771391e43daSPeter Zijlstra } 1772391e43daSPeter Zijlstra 1773391e43daSPeter Zijlstra /* 1774391e43daSPeter Zijlstra * If the current CPU has more than one RT task, see if the non 1775391e43daSPeter Zijlstra * running task can migrate over to a CPU that is running a task 1776391e43daSPeter Zijlstra * of lesser priority. 1777391e43daSPeter Zijlstra */ 1778391e43daSPeter Zijlstra static int push_rt_task(struct rq *rq) 1779391e43daSPeter Zijlstra { 1780391e43daSPeter Zijlstra struct task_struct *next_task; 1781391e43daSPeter Zijlstra struct rq *lowest_rq; 1782391e43daSPeter Zijlstra int ret = 0; 1783391e43daSPeter Zijlstra 1784391e43daSPeter Zijlstra if (!rq->rt.overloaded) 1785391e43daSPeter Zijlstra return 0; 1786391e43daSPeter Zijlstra 1787391e43daSPeter Zijlstra next_task = pick_next_pushable_task(rq); 1788391e43daSPeter Zijlstra if (!next_task) 1789391e43daSPeter Zijlstra return 0; 1790391e43daSPeter Zijlstra 1791391e43daSPeter Zijlstra retry: 1792391e43daSPeter Zijlstra if (unlikely(next_task == rq->curr)) { 1793391e43daSPeter Zijlstra WARN_ON(1); 1794391e43daSPeter Zijlstra return 0; 1795391e43daSPeter Zijlstra } 1796391e43daSPeter Zijlstra 1797391e43daSPeter Zijlstra /* 1798391e43daSPeter Zijlstra * It's possible that the next_task slipped in of 1799391e43daSPeter Zijlstra * higher priority than current. If that's the case 1800391e43daSPeter Zijlstra * just reschedule current. 1801391e43daSPeter Zijlstra */ 1802391e43daSPeter Zijlstra if (unlikely(next_task->prio < rq->curr->prio)) { 18038875125eSKirill Tkhai resched_curr(rq); 1804391e43daSPeter Zijlstra return 0; 1805391e43daSPeter Zijlstra } 1806391e43daSPeter Zijlstra 1807391e43daSPeter Zijlstra /* We might release rq lock */ 1808391e43daSPeter Zijlstra get_task_struct(next_task); 1809391e43daSPeter Zijlstra 1810391e43daSPeter Zijlstra /* find_lock_lowest_rq locks the rq if found */ 1811391e43daSPeter Zijlstra lowest_rq = find_lock_lowest_rq(next_task, rq); 1812391e43daSPeter Zijlstra if (!lowest_rq) { 1813391e43daSPeter Zijlstra struct task_struct *task; 1814391e43daSPeter Zijlstra /* 1815391e43daSPeter Zijlstra * find_lock_lowest_rq releases rq->lock 1816391e43daSPeter Zijlstra * so it is possible that next_task has migrated. 1817391e43daSPeter Zijlstra * 1818391e43daSPeter Zijlstra * We need to make sure that the task is still on the same 1819391e43daSPeter Zijlstra * run-queue and is also still the next task eligible for 1820391e43daSPeter Zijlstra * pushing. 1821391e43daSPeter Zijlstra */ 1822391e43daSPeter Zijlstra task = pick_next_pushable_task(rq); 1823391e43daSPeter Zijlstra if (task_cpu(next_task) == rq->cpu && task == next_task) { 1824391e43daSPeter Zijlstra /* 1825391e43daSPeter Zijlstra * The task hasn't migrated, and is still the next 1826391e43daSPeter Zijlstra * eligible task, but we failed to find a run-queue 1827391e43daSPeter Zijlstra * to push it to. Do not retry in this case, since 1828391e43daSPeter Zijlstra * other cpus will pull from us when ready. 1829391e43daSPeter Zijlstra */ 1830391e43daSPeter Zijlstra goto out; 1831391e43daSPeter Zijlstra } 1832391e43daSPeter Zijlstra 1833391e43daSPeter Zijlstra if (!task) 1834391e43daSPeter Zijlstra /* No more tasks, just exit */ 1835391e43daSPeter Zijlstra goto out; 1836391e43daSPeter Zijlstra 1837391e43daSPeter Zijlstra /* 1838391e43daSPeter Zijlstra * Something has shifted, try again. 1839391e43daSPeter Zijlstra */ 1840391e43daSPeter Zijlstra put_task_struct(next_task); 1841391e43daSPeter Zijlstra next_task = task; 1842391e43daSPeter Zijlstra goto retry; 1843391e43daSPeter Zijlstra } 1844391e43daSPeter Zijlstra 1845391e43daSPeter Zijlstra deactivate_task(rq, next_task, 0); 1846391e43daSPeter Zijlstra set_task_cpu(next_task, lowest_rq->cpu); 1847391e43daSPeter Zijlstra activate_task(lowest_rq, next_task, 0); 1848391e43daSPeter Zijlstra ret = 1; 1849391e43daSPeter Zijlstra 18508875125eSKirill Tkhai resched_curr(lowest_rq); 1851391e43daSPeter Zijlstra 1852391e43daSPeter Zijlstra double_unlock_balance(rq, lowest_rq); 1853391e43daSPeter Zijlstra 1854391e43daSPeter Zijlstra out: 1855391e43daSPeter Zijlstra put_task_struct(next_task); 1856391e43daSPeter Zijlstra 1857391e43daSPeter Zijlstra return ret; 1858391e43daSPeter Zijlstra } 1859391e43daSPeter Zijlstra 1860391e43daSPeter Zijlstra static void push_rt_tasks(struct rq *rq) 1861391e43daSPeter Zijlstra { 1862391e43daSPeter Zijlstra /* push_rt_task will return true if it moved an RT */ 1863391e43daSPeter Zijlstra while (push_rt_task(rq)) 1864391e43daSPeter Zijlstra ; 1865391e43daSPeter Zijlstra } 1866391e43daSPeter Zijlstra 1867b6366f04SSteven Rostedt #ifdef HAVE_RT_PUSH_IPI 1868b6366f04SSteven Rostedt /* 1869b6366f04SSteven Rostedt * The search for the next cpu always starts at rq->cpu and ends 1870b6366f04SSteven Rostedt * when we reach rq->cpu again. It will never return rq->cpu. 1871b6366f04SSteven Rostedt * This returns the next cpu to check, or nr_cpu_ids if the loop 1872b6366f04SSteven Rostedt * is complete. 1873b6366f04SSteven Rostedt * 1874b6366f04SSteven Rostedt * rq->rt.push_cpu holds the last cpu returned by this function, 1875b6366f04SSteven Rostedt * or if this is the first instance, it must hold rq->cpu. 1876b6366f04SSteven Rostedt */ 1877b6366f04SSteven Rostedt static int rto_next_cpu(struct rq *rq) 1878b6366f04SSteven Rostedt { 1879b6366f04SSteven Rostedt int prev_cpu = rq->rt.push_cpu; 1880b6366f04SSteven Rostedt int cpu; 1881b6366f04SSteven Rostedt 1882b6366f04SSteven Rostedt cpu = cpumask_next(prev_cpu, rq->rd->rto_mask); 1883b6366f04SSteven Rostedt 1884b6366f04SSteven Rostedt /* 1885b6366f04SSteven Rostedt * If the previous cpu is less than the rq's CPU, then it already 1886b6366f04SSteven Rostedt * passed the end of the mask, and has started from the beginning. 1887b6366f04SSteven Rostedt * We end if the next CPU is greater or equal to rq's CPU. 1888b6366f04SSteven Rostedt */ 1889b6366f04SSteven Rostedt if (prev_cpu < rq->cpu) { 1890b6366f04SSteven Rostedt if (cpu >= rq->cpu) 1891b6366f04SSteven Rostedt return nr_cpu_ids; 1892b6366f04SSteven Rostedt 1893b6366f04SSteven Rostedt } else if (cpu >= nr_cpu_ids) { 1894b6366f04SSteven Rostedt /* 1895b6366f04SSteven Rostedt * We passed the end of the mask, start at the beginning. 1896b6366f04SSteven Rostedt * If the result is greater or equal to the rq's CPU, then 1897b6366f04SSteven Rostedt * the loop is finished. 1898b6366f04SSteven Rostedt */ 1899b6366f04SSteven Rostedt cpu = cpumask_first(rq->rd->rto_mask); 1900b6366f04SSteven Rostedt if (cpu >= rq->cpu) 1901b6366f04SSteven Rostedt return nr_cpu_ids; 1902b6366f04SSteven Rostedt } 1903b6366f04SSteven Rostedt rq->rt.push_cpu = cpu; 1904b6366f04SSteven Rostedt 1905b6366f04SSteven Rostedt /* Return cpu to let the caller know if the loop is finished or not */ 1906b6366f04SSteven Rostedt return cpu; 1907b6366f04SSteven Rostedt } 1908b6366f04SSteven Rostedt 1909b6366f04SSteven Rostedt static int find_next_push_cpu(struct rq *rq) 1910b6366f04SSteven Rostedt { 1911b6366f04SSteven Rostedt struct rq *next_rq; 1912b6366f04SSteven Rostedt int cpu; 1913b6366f04SSteven Rostedt 1914b6366f04SSteven Rostedt while (1) { 1915b6366f04SSteven Rostedt cpu = rto_next_cpu(rq); 1916b6366f04SSteven Rostedt if (cpu >= nr_cpu_ids) 1917b6366f04SSteven Rostedt break; 1918b6366f04SSteven Rostedt next_rq = cpu_rq(cpu); 1919b6366f04SSteven Rostedt 1920b6366f04SSteven Rostedt /* Make sure the next rq can push to this rq */ 1921b6366f04SSteven Rostedt if (next_rq->rt.highest_prio.next < rq->rt.highest_prio.curr) 1922b6366f04SSteven Rostedt break; 1923b6366f04SSteven Rostedt } 1924b6366f04SSteven Rostedt 1925b6366f04SSteven Rostedt return cpu; 1926b6366f04SSteven Rostedt } 1927b6366f04SSteven Rostedt 1928b6366f04SSteven Rostedt #define RT_PUSH_IPI_EXECUTING 1 1929b6366f04SSteven Rostedt #define RT_PUSH_IPI_RESTART 2 1930b6366f04SSteven Rostedt 1931b6366f04SSteven Rostedt static void tell_cpu_to_push(struct rq *rq) 1932b6366f04SSteven Rostedt { 1933b6366f04SSteven Rostedt int cpu; 1934b6366f04SSteven Rostedt 1935b6366f04SSteven Rostedt if (rq->rt.push_flags & RT_PUSH_IPI_EXECUTING) { 1936b6366f04SSteven Rostedt raw_spin_lock(&rq->rt.push_lock); 1937b6366f04SSteven Rostedt /* Make sure it's still executing */ 1938b6366f04SSteven Rostedt if (rq->rt.push_flags & RT_PUSH_IPI_EXECUTING) { 1939b6366f04SSteven Rostedt /* 1940b6366f04SSteven Rostedt * Tell the IPI to restart the loop as things have 1941b6366f04SSteven Rostedt * changed since it started. 1942b6366f04SSteven Rostedt */ 1943b6366f04SSteven Rostedt rq->rt.push_flags |= RT_PUSH_IPI_RESTART; 1944b6366f04SSteven Rostedt raw_spin_unlock(&rq->rt.push_lock); 1945b6366f04SSteven Rostedt return; 1946b6366f04SSteven Rostedt } 1947b6366f04SSteven Rostedt raw_spin_unlock(&rq->rt.push_lock); 1948b6366f04SSteven Rostedt } 1949b6366f04SSteven Rostedt 1950b6366f04SSteven Rostedt /* When here, there's no IPI going around */ 1951b6366f04SSteven Rostedt 1952b6366f04SSteven Rostedt rq->rt.push_cpu = rq->cpu; 1953b6366f04SSteven Rostedt cpu = find_next_push_cpu(rq); 1954b6366f04SSteven Rostedt if (cpu >= nr_cpu_ids) 1955b6366f04SSteven Rostedt return; 1956b6366f04SSteven Rostedt 1957b6366f04SSteven Rostedt rq->rt.push_flags = RT_PUSH_IPI_EXECUTING; 1958b6366f04SSteven Rostedt 1959b6366f04SSteven Rostedt irq_work_queue_on(&rq->rt.push_work, cpu); 1960b6366f04SSteven Rostedt } 1961b6366f04SSteven Rostedt 1962b6366f04SSteven Rostedt /* Called from hardirq context */ 1963b6366f04SSteven Rostedt static void try_to_push_tasks(void *arg) 1964b6366f04SSteven Rostedt { 1965b6366f04SSteven Rostedt struct rt_rq *rt_rq = arg; 1966b6366f04SSteven Rostedt struct rq *rq, *src_rq; 1967b6366f04SSteven Rostedt int this_cpu; 1968b6366f04SSteven Rostedt int cpu; 1969b6366f04SSteven Rostedt 1970b6366f04SSteven Rostedt this_cpu = rt_rq->push_cpu; 1971b6366f04SSteven Rostedt 1972b6366f04SSteven Rostedt /* Paranoid check */ 1973b6366f04SSteven Rostedt BUG_ON(this_cpu != smp_processor_id()); 1974b6366f04SSteven Rostedt 1975b6366f04SSteven Rostedt rq = cpu_rq(this_cpu); 1976b6366f04SSteven Rostedt src_rq = rq_of_rt_rq(rt_rq); 1977b6366f04SSteven Rostedt 1978b6366f04SSteven Rostedt again: 1979b6366f04SSteven Rostedt if (has_pushable_tasks(rq)) { 1980b6366f04SSteven Rostedt raw_spin_lock(&rq->lock); 1981b6366f04SSteven Rostedt push_rt_task(rq); 1982b6366f04SSteven Rostedt raw_spin_unlock(&rq->lock); 1983b6366f04SSteven Rostedt } 1984b6366f04SSteven Rostedt 1985b6366f04SSteven Rostedt /* Pass the IPI to the next rt overloaded queue */ 1986b6366f04SSteven Rostedt raw_spin_lock(&rt_rq->push_lock); 1987b6366f04SSteven Rostedt /* 1988b6366f04SSteven Rostedt * If the source queue changed since the IPI went out, 1989b6366f04SSteven Rostedt * we need to restart the search from that CPU again. 1990b6366f04SSteven Rostedt */ 1991b6366f04SSteven Rostedt if (rt_rq->push_flags & RT_PUSH_IPI_RESTART) { 1992b6366f04SSteven Rostedt rt_rq->push_flags &= ~RT_PUSH_IPI_RESTART; 1993b6366f04SSteven Rostedt rt_rq->push_cpu = src_rq->cpu; 1994b6366f04SSteven Rostedt } 1995b6366f04SSteven Rostedt 1996b6366f04SSteven Rostedt cpu = find_next_push_cpu(src_rq); 1997b6366f04SSteven Rostedt 1998b6366f04SSteven Rostedt if (cpu >= nr_cpu_ids) 1999b6366f04SSteven Rostedt rt_rq->push_flags &= ~RT_PUSH_IPI_EXECUTING; 2000b6366f04SSteven Rostedt raw_spin_unlock(&rt_rq->push_lock); 2001b6366f04SSteven Rostedt 2002b6366f04SSteven Rostedt if (cpu >= nr_cpu_ids) 2003b6366f04SSteven Rostedt return; 2004b6366f04SSteven Rostedt 2005b6366f04SSteven Rostedt /* 2006b6366f04SSteven Rostedt * It is possible that a restart caused this CPU to be 2007b6366f04SSteven Rostedt * chosen again. Don't bother with an IPI, just see if we 2008b6366f04SSteven Rostedt * have more to push. 2009b6366f04SSteven Rostedt */ 2010b6366f04SSteven Rostedt if (unlikely(cpu == rq->cpu)) 2011b6366f04SSteven Rostedt goto again; 2012b6366f04SSteven Rostedt 2013b6366f04SSteven Rostedt /* Try the next RT overloaded CPU */ 2014b6366f04SSteven Rostedt irq_work_queue_on(&rt_rq->push_work, cpu); 2015b6366f04SSteven Rostedt } 2016b6366f04SSteven Rostedt 2017b6366f04SSteven Rostedt static void push_irq_work_func(struct irq_work *work) 2018b6366f04SSteven Rostedt { 2019b6366f04SSteven Rostedt struct rt_rq *rt_rq = container_of(work, struct rt_rq, push_work); 2020b6366f04SSteven Rostedt 2021b6366f04SSteven Rostedt try_to_push_tasks(rt_rq); 2022b6366f04SSteven Rostedt } 2023b6366f04SSteven Rostedt #endif /* HAVE_RT_PUSH_IPI */ 2024b6366f04SSteven Rostedt 20258046d680SPeter Zijlstra static void pull_rt_task(struct rq *this_rq) 2026391e43daSPeter Zijlstra { 20278046d680SPeter Zijlstra int this_cpu = this_rq->cpu, cpu; 20288046d680SPeter Zijlstra bool resched = false; 2029391e43daSPeter Zijlstra struct task_struct *p; 2030391e43daSPeter Zijlstra struct rq *src_rq; 2031391e43daSPeter Zijlstra 2032391e43daSPeter Zijlstra if (likely(!rt_overloaded(this_rq))) 20338046d680SPeter Zijlstra return; 2034391e43daSPeter Zijlstra 20357c3f2ab7SPeter Zijlstra /* 20367c3f2ab7SPeter Zijlstra * Match the barrier from rt_set_overloaded; this guarantees that if we 20377c3f2ab7SPeter Zijlstra * see overloaded we must also see the rto_mask bit. 20387c3f2ab7SPeter Zijlstra */ 20397c3f2ab7SPeter Zijlstra smp_rmb(); 20407c3f2ab7SPeter Zijlstra 2041b6366f04SSteven Rostedt #ifdef HAVE_RT_PUSH_IPI 2042b6366f04SSteven Rostedt if (sched_feat(RT_PUSH_IPI)) { 2043b6366f04SSteven Rostedt tell_cpu_to_push(this_rq); 20448046d680SPeter Zijlstra return; 2045b6366f04SSteven Rostedt } 2046b6366f04SSteven Rostedt #endif 2047b6366f04SSteven Rostedt 2048391e43daSPeter Zijlstra for_each_cpu(cpu, this_rq->rd->rto_mask) { 2049391e43daSPeter Zijlstra if (this_cpu == cpu) 2050391e43daSPeter Zijlstra continue; 2051391e43daSPeter Zijlstra 2052391e43daSPeter Zijlstra src_rq = cpu_rq(cpu); 2053391e43daSPeter Zijlstra 2054391e43daSPeter Zijlstra /* 2055391e43daSPeter Zijlstra * Don't bother taking the src_rq->lock if the next highest 2056391e43daSPeter Zijlstra * task is known to be lower-priority than our current task. 2057391e43daSPeter Zijlstra * This may look racy, but if this value is about to go 2058391e43daSPeter Zijlstra * logically higher, the src_rq will push this task away. 2059391e43daSPeter Zijlstra * And if its going logically lower, we do not care 2060391e43daSPeter Zijlstra */ 2061391e43daSPeter Zijlstra if (src_rq->rt.highest_prio.next >= 2062391e43daSPeter Zijlstra this_rq->rt.highest_prio.curr) 2063391e43daSPeter Zijlstra continue; 2064391e43daSPeter Zijlstra 2065391e43daSPeter Zijlstra /* 2066391e43daSPeter Zijlstra * We can potentially drop this_rq's lock in 2067391e43daSPeter Zijlstra * double_lock_balance, and another CPU could 2068391e43daSPeter Zijlstra * alter this_rq 2069391e43daSPeter Zijlstra */ 2070391e43daSPeter Zijlstra double_lock_balance(this_rq, src_rq); 2071391e43daSPeter Zijlstra 2072391e43daSPeter Zijlstra /* 2073e23ee747SKirill Tkhai * We can pull only a task, which is pushable 2074e23ee747SKirill Tkhai * on its rq, and no others. 2075391e43daSPeter Zijlstra */ 2076e23ee747SKirill Tkhai p = pick_highest_pushable_task(src_rq, this_cpu); 2077391e43daSPeter Zijlstra 2078391e43daSPeter Zijlstra /* 2079391e43daSPeter Zijlstra * Do we have an RT task that preempts 2080391e43daSPeter Zijlstra * the to-be-scheduled task? 2081391e43daSPeter Zijlstra */ 2082391e43daSPeter Zijlstra if (p && (p->prio < this_rq->rt.highest_prio.curr)) { 2083391e43daSPeter Zijlstra WARN_ON(p == src_rq->curr); 2084da0c1e65SKirill Tkhai WARN_ON(!task_on_rq_queued(p)); 2085391e43daSPeter Zijlstra 2086391e43daSPeter Zijlstra /* 2087391e43daSPeter Zijlstra * There's a chance that p is higher in priority 2088391e43daSPeter Zijlstra * than what's currently running on its cpu. 2089391e43daSPeter Zijlstra * This is just that p is wakeing up and hasn't 2090391e43daSPeter Zijlstra * had a chance to schedule. We only pull 2091391e43daSPeter Zijlstra * p if it is lower in priority than the 2092391e43daSPeter Zijlstra * current task on the run queue 2093391e43daSPeter Zijlstra */ 2094391e43daSPeter Zijlstra if (p->prio < src_rq->curr->prio) 2095391e43daSPeter Zijlstra goto skip; 2096391e43daSPeter Zijlstra 20978046d680SPeter Zijlstra resched = true; 2098391e43daSPeter Zijlstra 2099391e43daSPeter Zijlstra deactivate_task(src_rq, p, 0); 2100391e43daSPeter Zijlstra set_task_cpu(p, this_cpu); 2101391e43daSPeter Zijlstra activate_task(this_rq, p, 0); 2102391e43daSPeter Zijlstra /* 2103391e43daSPeter Zijlstra * We continue with the search, just in 2104391e43daSPeter Zijlstra * case there's an even higher prio task 2105391e43daSPeter Zijlstra * in another runqueue. (low likelihood 2106391e43daSPeter Zijlstra * but possible) 2107391e43daSPeter Zijlstra */ 2108391e43daSPeter Zijlstra } 2109391e43daSPeter Zijlstra skip: 2110391e43daSPeter Zijlstra double_unlock_balance(this_rq, src_rq); 2111391e43daSPeter Zijlstra } 2112391e43daSPeter Zijlstra 21138046d680SPeter Zijlstra if (resched) 21148046d680SPeter Zijlstra resched_curr(this_rq); 2115391e43daSPeter Zijlstra } 2116391e43daSPeter Zijlstra 2117391e43daSPeter Zijlstra /* 2118391e43daSPeter Zijlstra * If we are not running and we are not going to reschedule soon, we should 2119391e43daSPeter Zijlstra * try to push tasks away now 2120391e43daSPeter Zijlstra */ 2121391e43daSPeter Zijlstra static void task_woken_rt(struct rq *rq, struct task_struct *p) 2122391e43daSPeter Zijlstra { 2123391e43daSPeter Zijlstra if (!task_running(rq, p) && 2124391e43daSPeter Zijlstra !test_tsk_need_resched(rq->curr) && 212550605ffbSThomas Gleixner tsk_nr_cpus_allowed(p) > 1 && 21261baca4ceSJuri Lelli (dl_task(rq->curr) || rt_task(rq->curr)) && 212750605ffbSThomas Gleixner (tsk_nr_cpus_allowed(rq->curr) < 2 || 2128391e43daSPeter Zijlstra rq->curr->prio <= p->prio)) 2129391e43daSPeter Zijlstra push_rt_tasks(rq); 2130391e43daSPeter Zijlstra } 2131391e43daSPeter Zijlstra 2132391e43daSPeter Zijlstra /* Assumes rq->lock is held */ 2133391e43daSPeter Zijlstra static void rq_online_rt(struct rq *rq) 2134391e43daSPeter Zijlstra { 2135391e43daSPeter Zijlstra if (rq->rt.overloaded) 2136391e43daSPeter Zijlstra rt_set_overload(rq); 2137391e43daSPeter Zijlstra 2138391e43daSPeter Zijlstra __enable_runtime(rq); 2139391e43daSPeter Zijlstra 2140391e43daSPeter Zijlstra cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr); 2141391e43daSPeter Zijlstra } 2142391e43daSPeter Zijlstra 2143391e43daSPeter Zijlstra /* Assumes rq->lock is held */ 2144391e43daSPeter Zijlstra static void rq_offline_rt(struct rq *rq) 2145391e43daSPeter Zijlstra { 2146391e43daSPeter Zijlstra if (rq->rt.overloaded) 2147391e43daSPeter Zijlstra rt_clear_overload(rq); 2148391e43daSPeter Zijlstra 2149391e43daSPeter Zijlstra __disable_runtime(rq); 2150391e43daSPeter Zijlstra 2151391e43daSPeter Zijlstra cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID); 2152391e43daSPeter Zijlstra } 2153391e43daSPeter Zijlstra 2154391e43daSPeter Zijlstra /* 2155391e43daSPeter Zijlstra * When switch from the rt queue, we bring ourselves to a position 2156391e43daSPeter Zijlstra * that we might want to pull RT tasks from other runqueues. 2157391e43daSPeter Zijlstra */ 2158391e43daSPeter Zijlstra static void switched_from_rt(struct rq *rq, struct task_struct *p) 2159391e43daSPeter Zijlstra { 2160391e43daSPeter Zijlstra /* 2161391e43daSPeter Zijlstra * If there are other RT tasks then we will reschedule 2162391e43daSPeter Zijlstra * and the scheduling of the other RT tasks will handle 2163391e43daSPeter Zijlstra * the balancing. But if we are the last RT task 2164391e43daSPeter Zijlstra * we may need to handle the pulling of RT tasks 2165391e43daSPeter Zijlstra * now. 2166391e43daSPeter Zijlstra */ 2167da0c1e65SKirill Tkhai if (!task_on_rq_queued(p) || rq->rt.rt_nr_running) 21681158ddb5SKirill Tkhai return; 21691158ddb5SKirill Tkhai 2170fd7a4bedSPeter Zijlstra queue_pull_task(rq); 2171391e43daSPeter Zijlstra } 2172391e43daSPeter Zijlstra 217311c785b7SLi Zefan void __init init_sched_rt_class(void) 2174391e43daSPeter Zijlstra { 2175391e43daSPeter Zijlstra unsigned int i; 2176391e43daSPeter Zijlstra 2177391e43daSPeter Zijlstra for_each_possible_cpu(i) { 2178391e43daSPeter Zijlstra zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i), 2179391e43daSPeter Zijlstra GFP_KERNEL, cpu_to_node(i)); 2180391e43daSPeter Zijlstra } 2181391e43daSPeter Zijlstra } 2182391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 2183391e43daSPeter Zijlstra 2184391e43daSPeter Zijlstra /* 2185391e43daSPeter Zijlstra * When switching a task to RT, we may overload the runqueue 2186391e43daSPeter Zijlstra * with RT tasks. In this case we try to push them off to 2187391e43daSPeter Zijlstra * other runqueues. 2188391e43daSPeter Zijlstra */ 2189391e43daSPeter Zijlstra static void switched_to_rt(struct rq *rq, struct task_struct *p) 2190391e43daSPeter Zijlstra { 2191391e43daSPeter Zijlstra /* 2192391e43daSPeter Zijlstra * If we are already running, then there's nothing 2193391e43daSPeter Zijlstra * that needs to be done. But if we are not running 2194391e43daSPeter Zijlstra * we may need to preempt the current running task. 2195391e43daSPeter Zijlstra * If that current running task is also an RT task 2196391e43daSPeter Zijlstra * then see if we can move to another run queue. 2197391e43daSPeter Zijlstra */ 2198da0c1e65SKirill Tkhai if (task_on_rq_queued(p) && rq->curr != p) { 2199391e43daSPeter Zijlstra #ifdef CONFIG_SMP 220050605ffbSThomas Gleixner if (tsk_nr_cpus_allowed(p) > 1 && rq->rt.overloaded) 2201fd7a4bedSPeter Zijlstra queue_push_tasks(rq); 2202619bd4a7SSebastian Andrzej Siewior #endif /* CONFIG_SMP */ 2203fd7a4bedSPeter Zijlstra if (p->prio < rq->curr->prio) 22048875125eSKirill Tkhai resched_curr(rq); 2205391e43daSPeter Zijlstra } 2206391e43daSPeter Zijlstra } 2207391e43daSPeter Zijlstra 2208391e43daSPeter Zijlstra /* 2209391e43daSPeter Zijlstra * Priority of the task has changed. This may cause 2210391e43daSPeter Zijlstra * us to initiate a push or pull. 2211391e43daSPeter Zijlstra */ 2212391e43daSPeter Zijlstra static void 2213391e43daSPeter Zijlstra prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio) 2214391e43daSPeter Zijlstra { 2215da0c1e65SKirill Tkhai if (!task_on_rq_queued(p)) 2216391e43daSPeter Zijlstra return; 2217391e43daSPeter Zijlstra 2218391e43daSPeter Zijlstra if (rq->curr == p) { 2219391e43daSPeter Zijlstra #ifdef CONFIG_SMP 2220391e43daSPeter Zijlstra /* 2221391e43daSPeter Zijlstra * If our priority decreases while running, we 2222391e43daSPeter Zijlstra * may need to pull tasks to this runqueue. 2223391e43daSPeter Zijlstra */ 2224391e43daSPeter Zijlstra if (oldprio < p->prio) 2225fd7a4bedSPeter Zijlstra queue_pull_task(rq); 2226fd7a4bedSPeter Zijlstra 2227391e43daSPeter Zijlstra /* 2228391e43daSPeter Zijlstra * If there's a higher priority task waiting to run 2229fd7a4bedSPeter Zijlstra * then reschedule. 2230391e43daSPeter Zijlstra */ 2231fd7a4bedSPeter Zijlstra if (p->prio > rq->rt.highest_prio.curr) 22328875125eSKirill Tkhai resched_curr(rq); 2233391e43daSPeter Zijlstra #else 2234391e43daSPeter Zijlstra /* For UP simply resched on drop of prio */ 2235391e43daSPeter Zijlstra if (oldprio < p->prio) 22368875125eSKirill Tkhai resched_curr(rq); 2237391e43daSPeter Zijlstra #endif /* CONFIG_SMP */ 2238391e43daSPeter Zijlstra } else { 2239391e43daSPeter Zijlstra /* 2240391e43daSPeter Zijlstra * This task is not running, but if it is 2241391e43daSPeter Zijlstra * greater than the current running task 2242391e43daSPeter Zijlstra * then reschedule. 2243391e43daSPeter Zijlstra */ 2244391e43daSPeter Zijlstra if (p->prio < rq->curr->prio) 22458875125eSKirill Tkhai resched_curr(rq); 2246391e43daSPeter Zijlstra } 2247391e43daSPeter Zijlstra } 2248391e43daSPeter Zijlstra 2249391e43daSPeter Zijlstra static void watchdog(struct rq *rq, struct task_struct *p) 2250391e43daSPeter Zijlstra { 2251391e43daSPeter Zijlstra unsigned long soft, hard; 2252391e43daSPeter Zijlstra 2253391e43daSPeter Zijlstra /* max may change after cur was read, this will be fixed next tick */ 2254391e43daSPeter Zijlstra soft = task_rlimit(p, RLIMIT_RTTIME); 2255391e43daSPeter Zijlstra hard = task_rlimit_max(p, RLIMIT_RTTIME); 2256391e43daSPeter Zijlstra 2257391e43daSPeter Zijlstra if (soft != RLIM_INFINITY) { 2258391e43daSPeter Zijlstra unsigned long next; 2259391e43daSPeter Zijlstra 226057d2aa00SYing Xue if (p->rt.watchdog_stamp != jiffies) { 2261391e43daSPeter Zijlstra p->rt.timeout++; 226257d2aa00SYing Xue p->rt.watchdog_stamp = jiffies; 226357d2aa00SYing Xue } 226457d2aa00SYing Xue 2265391e43daSPeter Zijlstra next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ); 2266391e43daSPeter Zijlstra if (p->rt.timeout > next) 2267391e43daSPeter Zijlstra p->cputime_expires.sched_exp = p->se.sum_exec_runtime; 2268391e43daSPeter Zijlstra } 2269391e43daSPeter Zijlstra } 2270391e43daSPeter Zijlstra 2271391e43daSPeter Zijlstra static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued) 2272391e43daSPeter Zijlstra { 2273454c7999SColin Cross struct sched_rt_entity *rt_se = &p->rt; 2274454c7999SColin Cross 2275391e43daSPeter Zijlstra update_curr_rt(rq); 2276391e43daSPeter Zijlstra 2277391e43daSPeter Zijlstra watchdog(rq, p); 2278391e43daSPeter Zijlstra 2279391e43daSPeter Zijlstra /* 2280391e43daSPeter Zijlstra * RR tasks need a special form of timeslice management. 2281391e43daSPeter Zijlstra * FIFO tasks have no timeslices. 2282391e43daSPeter Zijlstra */ 2283391e43daSPeter Zijlstra if (p->policy != SCHED_RR) 2284391e43daSPeter Zijlstra return; 2285391e43daSPeter Zijlstra 2286391e43daSPeter Zijlstra if (--p->rt.time_slice) 2287391e43daSPeter Zijlstra return; 2288391e43daSPeter Zijlstra 2289ce0dbbbbSClark Williams p->rt.time_slice = sched_rr_timeslice; 2290391e43daSPeter Zijlstra 2291391e43daSPeter Zijlstra /* 2292e9aa39bbSLi Bin * Requeue to the end of queue if we (and all of our ancestors) are not 2293e9aa39bbSLi Bin * the only element on the queue 2294391e43daSPeter Zijlstra */ 2295454c7999SColin Cross for_each_sched_rt_entity(rt_se) { 2296454c7999SColin Cross if (rt_se->run_list.prev != rt_se->run_list.next) { 2297391e43daSPeter Zijlstra requeue_task_rt(rq, p, 0); 22988aa6f0ebSKirill Tkhai resched_curr(rq); 2299454c7999SColin Cross return; 2300454c7999SColin Cross } 2301391e43daSPeter Zijlstra } 2302391e43daSPeter Zijlstra } 2303391e43daSPeter Zijlstra 2304391e43daSPeter Zijlstra static void set_curr_task_rt(struct rq *rq) 2305391e43daSPeter Zijlstra { 2306391e43daSPeter Zijlstra struct task_struct *p = rq->curr; 2307391e43daSPeter Zijlstra 230878becc27SFrederic Weisbecker p->se.exec_start = rq_clock_task(rq); 2309391e43daSPeter Zijlstra 2310391e43daSPeter Zijlstra /* The running task is never eligible for pushing */ 2311391e43daSPeter Zijlstra dequeue_pushable_task(rq, p); 2312391e43daSPeter Zijlstra } 2313391e43daSPeter Zijlstra 2314391e43daSPeter Zijlstra static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task) 2315391e43daSPeter Zijlstra { 2316391e43daSPeter Zijlstra /* 2317391e43daSPeter Zijlstra * Time slice is 0 for SCHED_FIFO tasks 2318391e43daSPeter Zijlstra */ 2319391e43daSPeter Zijlstra if (task->policy == SCHED_RR) 2320ce0dbbbbSClark Williams return sched_rr_timeslice; 2321391e43daSPeter Zijlstra else 2322391e43daSPeter Zijlstra return 0; 2323391e43daSPeter Zijlstra } 2324391e43daSPeter Zijlstra 2325391e43daSPeter Zijlstra const struct sched_class rt_sched_class = { 2326391e43daSPeter Zijlstra .next = &fair_sched_class, 2327391e43daSPeter Zijlstra .enqueue_task = enqueue_task_rt, 2328391e43daSPeter Zijlstra .dequeue_task = dequeue_task_rt, 2329391e43daSPeter Zijlstra .yield_task = yield_task_rt, 2330391e43daSPeter Zijlstra 2331391e43daSPeter Zijlstra .check_preempt_curr = check_preempt_curr_rt, 2332391e43daSPeter Zijlstra 2333391e43daSPeter Zijlstra .pick_next_task = pick_next_task_rt, 2334391e43daSPeter Zijlstra .put_prev_task = put_prev_task_rt, 2335391e43daSPeter Zijlstra 2336391e43daSPeter Zijlstra #ifdef CONFIG_SMP 2337391e43daSPeter Zijlstra .select_task_rq = select_task_rq_rt, 2338391e43daSPeter Zijlstra 23396c37067eSPeter Zijlstra .set_cpus_allowed = set_cpus_allowed_common, 2340391e43daSPeter Zijlstra .rq_online = rq_online_rt, 2341391e43daSPeter Zijlstra .rq_offline = rq_offline_rt, 2342391e43daSPeter Zijlstra .task_woken = task_woken_rt, 2343391e43daSPeter Zijlstra .switched_from = switched_from_rt, 2344391e43daSPeter Zijlstra #endif 2345391e43daSPeter Zijlstra 2346391e43daSPeter Zijlstra .set_curr_task = set_curr_task_rt, 2347391e43daSPeter Zijlstra .task_tick = task_tick_rt, 2348391e43daSPeter Zijlstra 2349391e43daSPeter Zijlstra .get_rr_interval = get_rr_interval_rt, 2350391e43daSPeter Zijlstra 2351391e43daSPeter Zijlstra .prio_changed = prio_changed_rt, 2352391e43daSPeter Zijlstra .switched_to = switched_to_rt, 23536e998916SStanislaw Gruszka 23546e998916SStanislaw Gruszka .update_curr = update_curr_rt, 2355391e43daSPeter Zijlstra }; 2356391e43daSPeter Zijlstra 2357391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG 2358391e43daSPeter Zijlstra extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq); 2359391e43daSPeter Zijlstra 2360391e43daSPeter Zijlstra void print_rt_stats(struct seq_file *m, int cpu) 2361391e43daSPeter Zijlstra { 2362391e43daSPeter Zijlstra rt_rq_iter_t iter; 2363391e43daSPeter Zijlstra struct rt_rq *rt_rq; 2364391e43daSPeter Zijlstra 2365391e43daSPeter Zijlstra rcu_read_lock(); 2366391e43daSPeter Zijlstra for_each_rt_rq(rt_rq, iter, cpu_rq(cpu)) 2367391e43daSPeter Zijlstra print_rt_rq(m, cpu, rt_rq); 2368391e43daSPeter Zijlstra rcu_read_unlock(); 2369391e43daSPeter Zijlstra } 2370391e43daSPeter Zijlstra #endif /* CONFIG_SCHED_DEBUG */ 2371