aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched/fair.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/sched/fair.c')
-rw-r--r--kernel/sched/fair.c543
1 files changed, 142 insertions, 401 deletions
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 0d97ebdc58f0..c099cc6eebe3 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -784,7 +784,7 @@ account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
784 update_load_add(&rq_of(cfs_rq)->load, se->load.weight); 784 update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
785#ifdef CONFIG_SMP 785#ifdef CONFIG_SMP
786 if (entity_is_task(se)) 786 if (entity_is_task(se))
787 list_add_tail(&se->group_node, &rq_of(cfs_rq)->cfs_tasks); 787 list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
788#endif 788#endif
789 cfs_rq->nr_running++; 789 cfs_rq->nr_running++;
790} 790}
@@ -2703,7 +2703,7 @@ select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
2703 int want_sd = 1; 2703 int want_sd = 1;
2704 int sync = wake_flags & WF_SYNC; 2704 int sync = wake_flags & WF_SYNC;
2705 2705
2706 if (p->rt.nr_cpus_allowed == 1) 2706 if (p->nr_cpus_allowed == 1)
2707 return prev_cpu; 2707 return prev_cpu;
2708 2708
2709 if (sd_flag & SD_BALANCE_WAKE) { 2709 if (sd_flag & SD_BALANCE_WAKE) {
@@ -2721,7 +2721,7 @@ select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
2721 * If power savings logic is enabled for a domain, see if we 2721 * If power savings logic is enabled for a domain, see if we
2722 * are not overloaded, if so, don't balance wider. 2722 * are not overloaded, if so, don't balance wider.
2723 */ 2723 */
2724 if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) { 2724 if (tmp->flags & (SD_PREFER_LOCAL)) {
2725 unsigned long power = 0; 2725 unsigned long power = 0;
2726 unsigned long nr_running = 0; 2726 unsigned long nr_running = 0;
2727 unsigned long capacity; 2727 unsigned long capacity;
@@ -2734,9 +2734,6 @@ select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
2734 2734
2735 capacity = DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE); 2735 capacity = DIV_ROUND_CLOSEST(power, SCHED_POWER_SCALE);
2736 2736
2737 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2738 nr_running /= 2;
2739
2740 if (nr_running < capacity) 2737 if (nr_running < capacity)
2741 want_sd = 0; 2738 want_sd = 0;
2742 } 2739 }
@@ -3082,7 +3079,7 @@ struct lb_env {
3082 struct rq *dst_rq; 3079 struct rq *dst_rq;
3083 3080
3084 enum cpu_idle_type idle; 3081 enum cpu_idle_type idle;
3085 long load_move; 3082 long imbalance;
3086 unsigned int flags; 3083 unsigned int flags;
3087 3084
3088 unsigned int loop; 3085 unsigned int loop;
@@ -3215,8 +3212,10 @@ static int move_one_task(struct lb_env *env)
3215 3212
3216static unsigned long task_h_load(struct task_struct *p); 3213static unsigned long task_h_load(struct task_struct *p);
3217 3214
3215static const unsigned int sched_nr_migrate_break = 32;
3216
3218/* 3217/*
3219 * move_tasks tries to move up to load_move weighted load from busiest to 3218 * move_tasks tries to move up to imbalance weighted load from busiest to
3220 * this_rq, as part of a balancing operation within domain "sd". 3219 * this_rq, as part of a balancing operation within domain "sd".
3221 * Returns 1 if successful and 0 otherwise. 3220 * Returns 1 if successful and 0 otherwise.
3222 * 3221 *
@@ -3229,7 +3228,7 @@ static int move_tasks(struct lb_env *env)
3229 unsigned long load; 3228 unsigned long load;
3230 int pulled = 0; 3229 int pulled = 0;
3231 3230
3232 if (env->load_move <= 0) 3231 if (env->imbalance <= 0)
3233 return 0; 3232 return 0;
3234 3233
3235 while (!list_empty(tasks)) { 3234 while (!list_empty(tasks)) {
@@ -3242,7 +3241,7 @@ static int move_tasks(struct lb_env *env)
3242 3241
3243 /* take a breather every nr_migrate tasks */ 3242 /* take a breather every nr_migrate tasks */
3244 if (env->loop > env->loop_break) { 3243 if (env->loop > env->loop_break) {
3245 env->loop_break += sysctl_sched_nr_migrate; 3244 env->loop_break += sched_nr_migrate_break;
3246 env->flags |= LBF_NEED_BREAK; 3245 env->flags |= LBF_NEED_BREAK;
3247 break; 3246 break;
3248 } 3247 }
@@ -3252,10 +3251,10 @@ static int move_tasks(struct lb_env *env)
3252 3251
3253 load = task_h_load(p); 3252 load = task_h_load(p);
3254 3253
3255 if (load < 16 && !env->sd->nr_balance_failed) 3254 if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
3256 goto next; 3255 goto next;
3257 3256
3258 if ((load / 2) > env->load_move) 3257 if ((load / 2) > env->imbalance)
3259 goto next; 3258 goto next;
3260 3259
3261 if (!can_migrate_task(p, env)) 3260 if (!can_migrate_task(p, env))
@@ -3263,7 +3262,7 @@ static int move_tasks(struct lb_env *env)
3263 3262
3264 move_task(p, env); 3263 move_task(p, env);
3265 pulled++; 3264 pulled++;
3266 env->load_move -= load; 3265 env->imbalance -= load;
3267 3266
3268#ifdef CONFIG_PREEMPT 3267#ifdef CONFIG_PREEMPT
3269 /* 3268 /*
@@ -3279,7 +3278,7 @@ static int move_tasks(struct lb_env *env)
3279 * We only want to steal up to the prescribed amount of 3278 * We only want to steal up to the prescribed amount of
3280 * weighted load. 3279 * weighted load.
3281 */ 3280 */
3282 if (env->load_move <= 0) 3281 if (env->imbalance <= 0)
3283 break; 3282 break;
3284 3283
3285 continue; 3284 continue;
@@ -3433,14 +3432,6 @@ struct sd_lb_stats {
3433 unsigned int busiest_group_weight; 3432 unsigned int busiest_group_weight;
3434 3433
3435 int group_imb; /* Is there imbalance in this sd */ 3434 int group_imb; /* Is there imbalance in this sd */
3436#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3437 int power_savings_balance; /* Is powersave balance needed for this sd */
3438 struct sched_group *group_min; /* Least loaded group in sd */
3439 struct sched_group *group_leader; /* Group which relieves group_min */
3440 unsigned long min_load_per_task; /* load_per_task in group_min */
3441 unsigned long leader_nr_running; /* Nr running of group_leader */
3442 unsigned long min_nr_running; /* Nr running of group_min */
3443#endif
3444}; 3435};
3445 3436
3446/* 3437/*
@@ -3484,148 +3475,6 @@ static inline int get_sd_load_idx(struct sched_domain *sd,
3484 return load_idx; 3475 return load_idx;
3485} 3476}
3486 3477
3487
3488#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3489/**
3490 * init_sd_power_savings_stats - Initialize power savings statistics for
3491 * the given sched_domain, during load balancing.
3492 *
3493 * @sd: Sched domain whose power-savings statistics are to be initialized.
3494 * @sds: Variable containing the statistics for sd.
3495 * @idle: Idle status of the CPU at which we're performing load-balancing.
3496 */
3497static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3498 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3499{
3500 /*
3501 * Busy processors will not participate in power savings
3502 * balance.
3503 */
3504 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
3505 sds->power_savings_balance = 0;
3506 else {
3507 sds->power_savings_balance = 1;
3508 sds->min_nr_running = ULONG_MAX;
3509 sds->leader_nr_running = 0;
3510 }
3511}
3512
3513/**
3514 * update_sd_power_savings_stats - Update the power saving stats for a
3515 * sched_domain while performing load balancing.
3516 *
3517 * @group: sched_group belonging to the sched_domain under consideration.
3518 * @sds: Variable containing the statistics of the sched_domain
3519 * @local_group: Does group contain the CPU for which we're performing
3520 * load balancing ?
3521 * @sgs: Variable containing the statistics of the group.
3522 */
3523static inline void update_sd_power_savings_stats(struct sched_group *group,
3524 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3525{
3526
3527 if (!sds->power_savings_balance)
3528 return;
3529
3530 /*
3531 * If the local group is idle or completely loaded
3532 * no need to do power savings balance at this domain
3533 */
3534 if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
3535 !sds->this_nr_running))
3536 sds->power_savings_balance = 0;
3537
3538 /*
3539 * If a group is already running at full capacity or idle,
3540 * don't include that group in power savings calculations
3541 */
3542 if (!sds->power_savings_balance ||
3543 sgs->sum_nr_running >= sgs->group_capacity ||
3544 !sgs->sum_nr_running)
3545 return;
3546
3547 /*
3548 * Calculate the group which has the least non-idle load.
3549 * This is the group from where we need to pick up the load
3550 * for saving power
3551 */
3552 if ((sgs->sum_nr_running < sds->min_nr_running) ||
3553 (sgs->sum_nr_running == sds->min_nr_running &&
3554 group_first_cpu(group) > group_first_cpu(sds->group_min))) {
3555 sds->group_min = group;
3556 sds->min_nr_running = sgs->sum_nr_running;
3557 sds->min_load_per_task = sgs->sum_weighted_load /
3558 sgs->sum_nr_running;
3559 }
3560
3561 /*
3562 * Calculate the group which is almost near its
3563 * capacity but still has some space to pick up some load
3564 * from other group and save more power
3565 */
3566 if (sgs->sum_nr_running + 1 > sgs->group_capacity)
3567 return;
3568
3569 if (sgs->sum_nr_running > sds->leader_nr_running ||
3570 (sgs->sum_nr_running == sds->leader_nr_running &&
3571 group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
3572 sds->group_leader = group;
3573 sds->leader_nr_running = sgs->sum_nr_running;
3574 }
3575}
3576
3577/**
3578 * check_power_save_busiest_group - see if there is potential for some power-savings balance
3579 * @sds: Variable containing the statistics of the sched_domain
3580 * under consideration.
3581 * @this_cpu: Cpu at which we're currently performing load-balancing.
3582 * @imbalance: Variable to store the imbalance.
3583 *
3584 * Description:
3585 * Check if we have potential to perform some power-savings balance.
3586 * If yes, set the busiest group to be the least loaded group in the
3587 * sched_domain, so that it's CPUs can be put to idle.
3588 *
3589 * Returns 1 if there is potential to perform power-savings balance.
3590 * Else returns 0.
3591 */
3592static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3593 int this_cpu, unsigned long *imbalance)
3594{
3595 if (!sds->power_savings_balance)
3596 return 0;
3597
3598 if (sds->this != sds->group_leader ||
3599 sds->group_leader == sds->group_min)
3600 return 0;
3601
3602 *imbalance = sds->min_load_per_task;
3603 sds->busiest = sds->group_min;
3604
3605 return 1;
3606
3607}
3608#else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3609static inline void init_sd_power_savings_stats(struct sched_domain *sd,
3610 struct sd_lb_stats *sds, enum cpu_idle_type idle)
3611{
3612 return;
3613}
3614
3615static inline void update_sd_power_savings_stats(struct sched_group *group,
3616 struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
3617{
3618 return;
3619}
3620
3621static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
3622 int this_cpu, unsigned long *imbalance)
3623{
3624 return 0;
3625}
3626#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
3627
3628
3629unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu) 3478unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
3630{ 3479{
3631 return SCHED_POWER_SCALE; 3480 return SCHED_POWER_SCALE;
@@ -3654,15 +3503,22 @@ unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
3654unsigned long scale_rt_power(int cpu) 3503unsigned long scale_rt_power(int cpu)
3655{ 3504{
3656 struct rq *rq = cpu_rq(cpu); 3505 struct rq *rq = cpu_rq(cpu);
3657 u64 total, available; 3506 u64 total, available, age_stamp, avg;
3507
3508 /*
3509 * Since we're reading these variables without serialization make sure
3510 * we read them once before doing sanity checks on them.
3511 */
3512 age_stamp = ACCESS_ONCE(rq->age_stamp);
3513 avg = ACCESS_ONCE(rq->rt_avg);
3658 3514
3659 total = sched_avg_period() + (rq->clock - rq->age_stamp); 3515 total = sched_avg_period() + (rq->clock - age_stamp);
3660 3516
3661 if (unlikely(total < rq->rt_avg)) { 3517 if (unlikely(total < avg)) {
3662 /* Ensures that power won't end up being negative */ 3518 /* Ensures that power won't end up being negative */
3663 available = 0; 3519 available = 0;
3664 } else { 3520 } else {
3665 available = total - rq->rt_avg; 3521 available = total - avg;
3666 } 3522 }
3667 3523
3668 if (unlikely((s64)total < SCHED_POWER_SCALE)) 3524 if (unlikely((s64)total < SCHED_POWER_SCALE))
@@ -3725,13 +3581,28 @@ void update_group_power(struct sched_domain *sd, int cpu)
3725 3581
3726 power = 0; 3582 power = 0;
3727 3583
3728 group = child->groups; 3584 if (child->flags & SD_OVERLAP) {
3729 do { 3585 /*
3730 power += group->sgp->power; 3586 * SD_OVERLAP domains cannot assume that child groups
3731 group = group->next; 3587 * span the current group.
3732 } while (group != child->groups); 3588 */
3733 3589
3734 sdg->sgp->power = power; 3590 for_each_cpu(cpu, sched_group_cpus(sdg))
3591 power += power_of(cpu);
3592 } else {
3593 /*
3594 * !SD_OVERLAP domains can assume that child groups
3595 * span the current group.
3596 */
3597
3598 group = child->groups;
3599 do {
3600 power += group->sgp->power;
3601 group = group->next;
3602 } while (group != child->groups);
3603 }
3604
3605 sdg->sgp->power_orig = sdg->sgp->power = power;
3735} 3606}
3736 3607
3737/* 3608/*
@@ -3761,41 +3632,43 @@ fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
3761 3632
3762/** 3633/**
3763 * update_sg_lb_stats - Update sched_group's statistics for load balancing. 3634 * update_sg_lb_stats - Update sched_group's statistics for load balancing.
3764 * @sd: The sched_domain whose statistics are to be updated. 3635 * @env: The load balancing environment.
3765 * @group: sched_group whose statistics are to be updated. 3636 * @group: sched_group whose statistics are to be updated.
3766 * @this_cpu: Cpu for which load balance is currently performed.
3767 * @idle: Idle status of this_cpu
3768 * @load_idx: Load index of sched_domain of this_cpu for load calc. 3637 * @load_idx: Load index of sched_domain of this_cpu for load calc.
3769 * @local_group: Does group contain this_cpu. 3638 * @local_group: Does group contain this_cpu.
3770 * @cpus: Set of cpus considered for load balancing. 3639 * @cpus: Set of cpus considered for load balancing.
3771 * @balance: Should we balance. 3640 * @balance: Should we balance.
3772 * @sgs: variable to hold the statistics for this group. 3641 * @sgs: variable to hold the statistics for this group.
3773 */ 3642 */
3774static inline void update_sg_lb_stats(struct sched_domain *sd, 3643static inline void update_sg_lb_stats(struct lb_env *env,
3775 struct sched_group *group, int this_cpu, 3644 struct sched_group *group, int load_idx,
3776 enum cpu_idle_type idle, int load_idx,
3777 int local_group, const struct cpumask *cpus, 3645 int local_group, const struct cpumask *cpus,
3778 int *balance, struct sg_lb_stats *sgs) 3646 int *balance, struct sg_lb_stats *sgs)
3779{ 3647{
3780 unsigned long load, max_cpu_load, min_cpu_load, max_nr_running; 3648 unsigned long nr_running, max_nr_running, min_nr_running;
3781 int i; 3649 unsigned long load, max_cpu_load, min_cpu_load;
3782 unsigned int balance_cpu = -1, first_idle_cpu = 0; 3650 unsigned int balance_cpu = -1, first_idle_cpu = 0;
3783 unsigned long avg_load_per_task = 0; 3651 unsigned long avg_load_per_task = 0;
3652 int i;
3784 3653
3785 if (local_group) 3654 if (local_group)
3786 balance_cpu = group_first_cpu(group); 3655 balance_cpu = group_balance_cpu(group);
3787 3656
3788 /* Tally up the load of all CPUs in the group */ 3657 /* Tally up the load of all CPUs in the group */
3789 max_cpu_load = 0; 3658 max_cpu_load = 0;
3790 min_cpu_load = ~0UL; 3659 min_cpu_load = ~0UL;
3791 max_nr_running = 0; 3660 max_nr_running = 0;
3661 min_nr_running = ~0UL;
3792 3662
3793 for_each_cpu_and(i, sched_group_cpus(group), cpus) { 3663 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3794 struct rq *rq = cpu_rq(i); 3664 struct rq *rq = cpu_rq(i);
3795 3665
3666 nr_running = rq->nr_running;
3667
3796 /* Bias balancing toward cpus of our domain */ 3668 /* Bias balancing toward cpus of our domain */
3797 if (local_group) { 3669 if (local_group) {
3798 if (idle_cpu(i) && !first_idle_cpu) { 3670 if (idle_cpu(i) && !first_idle_cpu &&
3671 cpumask_test_cpu(i, sched_group_mask(group))) {
3799 first_idle_cpu = 1; 3672 first_idle_cpu = 1;
3800 balance_cpu = i; 3673 balance_cpu = i;
3801 } 3674 }
@@ -3803,16 +3676,19 @@ static inline void update_sg_lb_stats(struct sched_domain *sd,
3803 load = target_load(i, load_idx); 3676 load = target_load(i, load_idx);
3804 } else { 3677 } else {
3805 load = source_load(i, load_idx); 3678 load = source_load(i, load_idx);
3806 if (load > max_cpu_load) { 3679 if (load > max_cpu_load)
3807 max_cpu_load = load; 3680 max_cpu_load = load;
3808 max_nr_running = rq->nr_running;
3809 }
3810 if (min_cpu_load > load) 3681 if (min_cpu_load > load)
3811 min_cpu_load = load; 3682 min_cpu_load = load;
3683
3684 if (nr_running > max_nr_running)
3685 max_nr_running = nr_running;
3686 if (min_nr_running > nr_running)
3687 min_nr_running = nr_running;
3812 } 3688 }
3813 3689
3814 sgs->group_load += load; 3690 sgs->group_load += load;
3815 sgs->sum_nr_running += rq->nr_running; 3691 sgs->sum_nr_running += nr_running;
3816 sgs->sum_weighted_load += weighted_cpuload(i); 3692 sgs->sum_weighted_load += weighted_cpuload(i);
3817 if (idle_cpu(i)) 3693 if (idle_cpu(i))
3818 sgs->idle_cpus++; 3694 sgs->idle_cpus++;
@@ -3825,14 +3701,14 @@ static inline void update_sg_lb_stats(struct sched_domain *sd,
3825 * to do the newly idle load balance. 3701 * to do the newly idle load balance.
3826 */ 3702 */
3827 if (local_group) { 3703 if (local_group) {
3828 if (idle != CPU_NEWLY_IDLE) { 3704 if (env->idle != CPU_NEWLY_IDLE) {
3829 if (balance_cpu != this_cpu) { 3705 if (balance_cpu != env->dst_cpu) {
3830 *balance = 0; 3706 *balance = 0;
3831 return; 3707 return;
3832 } 3708 }
3833 update_group_power(sd, this_cpu); 3709 update_group_power(env->sd, env->dst_cpu);
3834 } else if (time_after_eq(jiffies, group->sgp->next_update)) 3710 } else if (time_after_eq(jiffies, group->sgp->next_update))
3835 update_group_power(sd, this_cpu); 3711 update_group_power(env->sd, env->dst_cpu);
3836 } 3712 }
3837 3713
3838 /* Adjust by relative CPU power of the group */ 3714 /* Adjust by relative CPU power of the group */
@@ -3850,13 +3726,14 @@ static inline void update_sg_lb_stats(struct sched_domain *sd,
3850 if (sgs->sum_nr_running) 3726 if (sgs->sum_nr_running)
3851 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; 3727 avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
3852 3728
3853 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task && max_nr_running > 1) 3729 if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
3730 (max_nr_running - min_nr_running) > 1)
3854 sgs->group_imb = 1; 3731 sgs->group_imb = 1;
3855 3732
3856 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power, 3733 sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
3857 SCHED_POWER_SCALE); 3734 SCHED_POWER_SCALE);
3858 if (!sgs->group_capacity) 3735 if (!sgs->group_capacity)
3859 sgs->group_capacity = fix_small_capacity(sd, group); 3736 sgs->group_capacity = fix_small_capacity(env->sd, group);
3860 sgs->group_weight = group->group_weight; 3737 sgs->group_weight = group->group_weight;
3861 3738
3862 if (sgs->group_capacity > sgs->sum_nr_running) 3739 if (sgs->group_capacity > sgs->sum_nr_running)
@@ -3865,20 +3742,18 @@ static inline void update_sg_lb_stats(struct sched_domain *sd,
3865 3742
3866/** 3743/**
3867 * update_sd_pick_busiest - return 1 on busiest group 3744 * update_sd_pick_busiest - return 1 on busiest group
3868 * @sd: sched_domain whose statistics are to be checked 3745 * @env: The load balancing environment.
3869 * @sds: sched_domain statistics 3746 * @sds: sched_domain statistics
3870 * @sg: sched_group candidate to be checked for being the busiest 3747 * @sg: sched_group candidate to be checked for being the busiest
3871 * @sgs: sched_group statistics 3748 * @sgs: sched_group statistics
3872 * @this_cpu: the current cpu
3873 * 3749 *
3874 * Determine if @sg is a busier group than the previously selected 3750 * Determine if @sg is a busier group than the previously selected
3875 * busiest group. 3751 * busiest group.
3876 */ 3752 */
3877static bool update_sd_pick_busiest(struct sched_domain *sd, 3753static bool update_sd_pick_busiest(struct lb_env *env,
3878 struct sd_lb_stats *sds, 3754 struct sd_lb_stats *sds,
3879 struct sched_group *sg, 3755 struct sched_group *sg,
3880 struct sg_lb_stats *sgs, 3756 struct sg_lb_stats *sgs)
3881 int this_cpu)
3882{ 3757{
3883 if (sgs->avg_load <= sds->max_load) 3758 if (sgs->avg_load <= sds->max_load)
3884 return false; 3759 return false;
@@ -3894,8 +3769,8 @@ static bool update_sd_pick_busiest(struct sched_domain *sd,
3894 * numbered CPUs in the group, therefore mark all groups 3769 * numbered CPUs in the group, therefore mark all groups
3895 * higher than ourself as busy. 3770 * higher than ourself as busy.
3896 */ 3771 */
3897 if ((sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running && 3772 if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
3898 this_cpu < group_first_cpu(sg)) { 3773 env->dst_cpu < group_first_cpu(sg)) {
3899 if (!sds->busiest) 3774 if (!sds->busiest)
3900 return true; 3775 return true;
3901 3776
@@ -3908,35 +3783,32 @@ static bool update_sd_pick_busiest(struct sched_domain *sd,
3908 3783
3909/** 3784/**
3910 * update_sd_lb_stats - Update sched_domain's statistics for load balancing. 3785 * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
3911 * @sd: sched_domain whose statistics are to be updated. 3786 * @env: The load balancing environment.
3912 * @this_cpu: Cpu for which load balance is currently performed.
3913 * @idle: Idle status of this_cpu
3914 * @cpus: Set of cpus considered for load balancing. 3787 * @cpus: Set of cpus considered for load balancing.
3915 * @balance: Should we balance. 3788 * @balance: Should we balance.
3916 * @sds: variable to hold the statistics for this sched_domain. 3789 * @sds: variable to hold the statistics for this sched_domain.
3917 */ 3790 */
3918static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu, 3791static inline void update_sd_lb_stats(struct lb_env *env,
3919 enum cpu_idle_type idle, const struct cpumask *cpus, 3792 const struct cpumask *cpus,
3920 int *balance, struct sd_lb_stats *sds) 3793 int *balance, struct sd_lb_stats *sds)
3921{ 3794{
3922 struct sched_domain *child = sd->child; 3795 struct sched_domain *child = env->sd->child;
3923 struct sched_group *sg = sd->groups; 3796 struct sched_group *sg = env->sd->groups;
3924 struct sg_lb_stats sgs; 3797 struct sg_lb_stats sgs;
3925 int load_idx, prefer_sibling = 0; 3798 int load_idx, prefer_sibling = 0;
3926 3799
3927 if (child && child->flags & SD_PREFER_SIBLING) 3800 if (child && child->flags & SD_PREFER_SIBLING)
3928 prefer_sibling = 1; 3801 prefer_sibling = 1;
3929 3802
3930 init_sd_power_savings_stats(sd, sds, idle); 3803 load_idx = get_sd_load_idx(env->sd, env->idle);
3931 load_idx = get_sd_load_idx(sd, idle);
3932 3804
3933 do { 3805 do {
3934 int local_group; 3806 int local_group;
3935 3807
3936 local_group = cpumask_test_cpu(this_cpu, sched_group_cpus(sg)); 3808 local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
3937 memset(&sgs, 0, sizeof(sgs)); 3809 memset(&sgs, 0, sizeof(sgs));
3938 update_sg_lb_stats(sd, sg, this_cpu, idle, load_idx, 3810 update_sg_lb_stats(env, sg, load_idx, local_group,
3939 local_group, cpus, balance, &sgs); 3811 cpus, balance, &sgs);
3940 3812
3941 if (local_group && !(*balance)) 3813 if (local_group && !(*balance))
3942 return; 3814 return;
@@ -3964,7 +3836,7 @@ static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
3964 sds->this_load_per_task = sgs.sum_weighted_load; 3836 sds->this_load_per_task = sgs.sum_weighted_load;
3965 sds->this_has_capacity = sgs.group_has_capacity; 3837 sds->this_has_capacity = sgs.group_has_capacity;
3966 sds->this_idle_cpus = sgs.idle_cpus; 3838 sds->this_idle_cpus = sgs.idle_cpus;
3967 } else if (update_sd_pick_busiest(sd, sds, sg, &sgs, this_cpu)) { 3839 } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
3968 sds->max_load = sgs.avg_load; 3840 sds->max_load = sgs.avg_load;
3969 sds->busiest = sg; 3841 sds->busiest = sg;
3970 sds->busiest_nr_running = sgs.sum_nr_running; 3842 sds->busiest_nr_running = sgs.sum_nr_running;
@@ -3976,9 +3848,8 @@ static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
3976 sds->group_imb = sgs.group_imb; 3848 sds->group_imb = sgs.group_imb;
3977 } 3849 }
3978 3850
3979 update_sd_power_savings_stats(sg, sds, local_group, &sgs);
3980 sg = sg->next; 3851 sg = sg->next;
3981 } while (sg != sd->groups); 3852 } while (sg != env->sd->groups);
3982} 3853}
3983 3854
3984/** 3855/**
@@ -4001,29 +3872,26 @@ static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
4001 * Returns 1 when packing is required and a task should be moved to 3872 * Returns 1 when packing is required and a task should be moved to
4002 * this CPU. The amount of the imbalance is returned in *imbalance. 3873 * this CPU. The amount of the imbalance is returned in *imbalance.
4003 * 3874 *
4004 * @sd: The sched_domain whose packing is to be checked. 3875 * @env: The load balancing environment.
4005 * @sds: Statistics of the sched_domain which is to be packed 3876 * @sds: Statistics of the sched_domain which is to be packed
4006 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4007 * @imbalance: returns amount of imbalanced due to packing.
4008 */ 3877 */
4009static int check_asym_packing(struct sched_domain *sd, 3878static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
4010 struct sd_lb_stats *sds,
4011 int this_cpu, unsigned long *imbalance)
4012{ 3879{
4013 int busiest_cpu; 3880 int busiest_cpu;
4014 3881
4015 if (!(sd->flags & SD_ASYM_PACKING)) 3882 if (!(env->sd->flags & SD_ASYM_PACKING))
4016 return 0; 3883 return 0;
4017 3884
4018 if (!sds->busiest) 3885 if (!sds->busiest)
4019 return 0; 3886 return 0;
4020 3887
4021 busiest_cpu = group_first_cpu(sds->busiest); 3888 busiest_cpu = group_first_cpu(sds->busiest);
4022 if (this_cpu > busiest_cpu) 3889 if (env->dst_cpu > busiest_cpu)
4023 return 0; 3890 return 0;
4024 3891
4025 *imbalance = DIV_ROUND_CLOSEST(sds->max_load * sds->busiest->sgp->power, 3892 env->imbalance = DIV_ROUND_CLOSEST(
4026 SCHED_POWER_SCALE); 3893 sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
3894
4027 return 1; 3895 return 1;
4028} 3896}
4029 3897
@@ -4031,12 +3899,11 @@ static int check_asym_packing(struct sched_domain *sd,
4031 * fix_small_imbalance - Calculate the minor imbalance that exists 3899 * fix_small_imbalance - Calculate the minor imbalance that exists
4032 * amongst the groups of a sched_domain, during 3900 * amongst the groups of a sched_domain, during
4033 * load balancing. 3901 * load balancing.
3902 * @env: The load balancing environment.
4034 * @sds: Statistics of the sched_domain whose imbalance is to be calculated. 3903 * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
4035 * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
4036 * @imbalance: Variable to store the imbalance.
4037 */ 3904 */
4038static inline void fix_small_imbalance(struct sd_lb_stats *sds, 3905static inline
4039 int this_cpu, unsigned long *imbalance) 3906void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
4040{ 3907{
4041 unsigned long tmp, pwr_now = 0, pwr_move = 0; 3908 unsigned long tmp, pwr_now = 0, pwr_move = 0;
4042 unsigned int imbn = 2; 3909 unsigned int imbn = 2;
@@ -4047,9 +3914,10 @@ static inline void fix_small_imbalance(struct sd_lb_stats *sds,
4047 if (sds->busiest_load_per_task > 3914 if (sds->busiest_load_per_task >
4048 sds->this_load_per_task) 3915 sds->this_load_per_task)
4049 imbn = 1; 3916 imbn = 1;
4050 } else 3917 } else {
4051 sds->this_load_per_task = 3918 sds->this_load_per_task =
4052 cpu_avg_load_per_task(this_cpu); 3919 cpu_avg_load_per_task(env->dst_cpu);
3920 }
4053 3921
4054 scaled_busy_load_per_task = sds->busiest_load_per_task 3922 scaled_busy_load_per_task = sds->busiest_load_per_task
4055 * SCHED_POWER_SCALE; 3923 * SCHED_POWER_SCALE;
@@ -4057,7 +3925,7 @@ static inline void fix_small_imbalance(struct sd_lb_stats *sds,
4057 3925
4058 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >= 3926 if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
4059 (scaled_busy_load_per_task * imbn)) { 3927 (scaled_busy_load_per_task * imbn)) {
4060 *imbalance = sds->busiest_load_per_task; 3928 env->imbalance = sds->busiest_load_per_task;
4061 return; 3929 return;
4062 } 3930 }
4063 3931
@@ -4094,18 +3962,16 @@ static inline void fix_small_imbalance(struct sd_lb_stats *sds,
4094 3962
4095 /* Move if we gain throughput */ 3963 /* Move if we gain throughput */
4096 if (pwr_move > pwr_now) 3964 if (pwr_move > pwr_now)
4097 *imbalance = sds->busiest_load_per_task; 3965 env->imbalance = sds->busiest_load_per_task;
4098} 3966}
4099 3967
4100/** 3968/**
4101 * calculate_imbalance - Calculate the amount of imbalance present within the 3969 * calculate_imbalance - Calculate the amount of imbalance present within the
4102 * groups of a given sched_domain during load balance. 3970 * groups of a given sched_domain during load balance.
3971 * @env: load balance environment
4103 * @sds: statistics of the sched_domain whose imbalance is to be calculated. 3972 * @sds: statistics of the sched_domain whose imbalance is to be calculated.
4104 * @this_cpu: Cpu for which currently load balance is being performed.
4105 * @imbalance: The variable to store the imbalance.
4106 */ 3973 */
4107static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu, 3974static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
4108 unsigned long *imbalance)
4109{ 3975{
4110 unsigned long max_pull, load_above_capacity = ~0UL; 3976 unsigned long max_pull, load_above_capacity = ~0UL;
4111 3977
@@ -4121,8 +3987,8 @@ static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
4121 * its cpu_power, while calculating max_load..) 3987 * its cpu_power, while calculating max_load..)
4122 */ 3988 */
4123 if (sds->max_load < sds->avg_load) { 3989 if (sds->max_load < sds->avg_load) {
4124 *imbalance = 0; 3990 env->imbalance = 0;
4125 return fix_small_imbalance(sds, this_cpu, imbalance); 3991 return fix_small_imbalance(env, sds);
4126 } 3992 }
4127 3993
4128 if (!sds->group_imb) { 3994 if (!sds->group_imb) {
@@ -4150,7 +4016,7 @@ static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
4150 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity); 4016 max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
4151 4017
4152 /* How much load to actually move to equalise the imbalance */ 4018 /* How much load to actually move to equalise the imbalance */
4153 *imbalance = min(max_pull * sds->busiest->sgp->power, 4019 env->imbalance = min(max_pull * sds->busiest->sgp->power,
4154 (sds->avg_load - sds->this_load) * sds->this->sgp->power) 4020 (sds->avg_load - sds->this_load) * sds->this->sgp->power)
4155 / SCHED_POWER_SCALE; 4021 / SCHED_POWER_SCALE;
4156 4022
@@ -4160,8 +4026,8 @@ static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
4160 * a think about bumping its value to force at least one task to be 4026 * a think about bumping its value to force at least one task to be
4161 * moved 4027 * moved
4162 */ 4028 */
4163 if (*imbalance < sds->busiest_load_per_task) 4029 if (env->imbalance < sds->busiest_load_per_task)
4164 return fix_small_imbalance(sds, this_cpu, imbalance); 4030 return fix_small_imbalance(env, sds);
4165 4031
4166} 4032}
4167 4033
@@ -4177,11 +4043,7 @@ static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
4177 * Also calculates the amount of weighted load which should be moved 4043 * Also calculates the amount of weighted load which should be moved
4178 * to restore balance. 4044 * to restore balance.
4179 * 4045 *
4180 * @sd: The sched_domain whose busiest group is to be returned. 4046 * @env: The load balancing environment.
4181 * @this_cpu: The cpu for which load balancing is currently being performed.
4182 * @imbalance: Variable which stores amount of weighted load which should
4183 * be moved to restore balance/put a group to idle.
4184 * @idle: The idle status of this_cpu.
4185 * @cpus: The set of CPUs under consideration for load-balancing. 4047 * @cpus: The set of CPUs under consideration for load-balancing.
4186 * @balance: Pointer to a variable indicating if this_cpu 4048 * @balance: Pointer to a variable indicating if this_cpu
4187 * is the appropriate cpu to perform load balancing at this_level. 4049 * is the appropriate cpu to perform load balancing at this_level.
@@ -4192,9 +4054,7 @@ static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
4192 * put to idle by rebalancing its tasks onto our group. 4054 * put to idle by rebalancing its tasks onto our group.
4193 */ 4055 */
4194static struct sched_group * 4056static struct sched_group *
4195find_busiest_group(struct sched_domain *sd, int this_cpu, 4057find_busiest_group(struct lb_env *env, const struct cpumask *cpus, int *balance)
4196 unsigned long *imbalance, enum cpu_idle_type idle,
4197 const struct cpumask *cpus, int *balance)
4198{ 4058{
4199 struct sd_lb_stats sds; 4059 struct sd_lb_stats sds;
4200 4060
@@ -4204,7 +4064,7 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
4204 * Compute the various statistics relavent for load balancing at 4064 * Compute the various statistics relavent for load balancing at
4205 * this level. 4065 * this level.
4206 */ 4066 */
4207 update_sd_lb_stats(sd, this_cpu, idle, cpus, balance, &sds); 4067 update_sd_lb_stats(env, cpus, balance, &sds);
4208 4068
4209 /* 4069 /*
4210 * this_cpu is not the appropriate cpu to perform load balancing at 4070 * this_cpu is not the appropriate cpu to perform load balancing at
@@ -4213,8 +4073,8 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
4213 if (!(*balance)) 4073 if (!(*balance))
4214 goto ret; 4074 goto ret;
4215 4075
4216 if ((idle == CPU_IDLE || idle == CPU_NEWLY_IDLE) && 4076 if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
4217 check_asym_packing(sd, &sds, this_cpu, imbalance)) 4077 check_asym_packing(env, &sds))
4218 return sds.busiest; 4078 return sds.busiest;
4219 4079
4220 /* There is no busy sibling group to pull tasks from */ 4080 /* There is no busy sibling group to pull tasks from */
@@ -4232,7 +4092,7 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
4232 goto force_balance; 4092 goto force_balance;
4233 4093
4234 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */ 4094 /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
4235 if (idle == CPU_NEWLY_IDLE && sds.this_has_capacity && 4095 if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
4236 !sds.busiest_has_capacity) 4096 !sds.busiest_has_capacity)
4237 goto force_balance; 4097 goto force_balance;
4238 4098
@@ -4250,7 +4110,7 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
4250 if (sds.this_load >= sds.avg_load) 4110 if (sds.this_load >= sds.avg_load)
4251 goto out_balanced; 4111 goto out_balanced;
4252 4112
4253 if (idle == CPU_IDLE) { 4113 if (env->idle == CPU_IDLE) {
4254 /* 4114 /*
4255 * This cpu is idle. If the busiest group load doesn't 4115 * This cpu is idle. If the busiest group load doesn't
4256 * have more tasks than the number of available cpu's and 4116 * have more tasks than the number of available cpu's and
@@ -4265,34 +4125,27 @@ find_busiest_group(struct sched_domain *sd, int this_cpu,
4265 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use 4125 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
4266 * imbalance_pct to be conservative. 4126 * imbalance_pct to be conservative.
4267 */ 4127 */
4268 if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load) 4128 if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
4269 goto out_balanced; 4129 goto out_balanced;
4270 } 4130 }
4271 4131
4272force_balance: 4132force_balance:
4273 /* Looks like there is an imbalance. Compute it */ 4133 /* Looks like there is an imbalance. Compute it */
4274 calculate_imbalance(&sds, this_cpu, imbalance); 4134 calculate_imbalance(env, &sds);
4275 return sds.busiest; 4135 return sds.busiest;
4276 4136
4277out_balanced: 4137out_balanced:
4278 /*
4279 * There is no obvious imbalance. But check if we can do some balancing
4280 * to save power.
4281 */
4282 if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
4283 return sds.busiest;
4284ret: 4138ret:
4285 *imbalance = 0; 4139 env->imbalance = 0;
4286 return NULL; 4140 return NULL;
4287} 4141}
4288 4142
4289/* 4143/*
4290 * find_busiest_queue - find the busiest runqueue among the cpus in group. 4144 * find_busiest_queue - find the busiest runqueue among the cpus in group.
4291 */ 4145 */
4292static struct rq * 4146static struct rq *find_busiest_queue(struct lb_env *env,
4293find_busiest_queue(struct sched_domain *sd, struct sched_group *group, 4147 struct sched_group *group,
4294 enum cpu_idle_type idle, unsigned long imbalance, 4148 const struct cpumask *cpus)
4295 const struct cpumask *cpus)
4296{ 4149{
4297 struct rq *busiest = NULL, *rq; 4150 struct rq *busiest = NULL, *rq;
4298 unsigned long max_load = 0; 4151 unsigned long max_load = 0;
@@ -4305,7 +4158,7 @@ find_busiest_queue(struct sched_domain *sd, struct sched_group *group,
4305 unsigned long wl; 4158 unsigned long wl;
4306 4159
4307 if (!capacity) 4160 if (!capacity)
4308 capacity = fix_small_capacity(sd, group); 4161 capacity = fix_small_capacity(env->sd, group);
4309 4162
4310 if (!cpumask_test_cpu(i, cpus)) 4163 if (!cpumask_test_cpu(i, cpus))
4311 continue; 4164 continue;
@@ -4317,7 +4170,7 @@ find_busiest_queue(struct sched_domain *sd, struct sched_group *group,
4317 * When comparing with imbalance, use weighted_cpuload() 4170 * When comparing with imbalance, use weighted_cpuload()
4318 * which is not scaled with the cpu power. 4171 * which is not scaled with the cpu power.
4319 */ 4172 */
4320 if (capacity && rq->nr_running == 1 && wl > imbalance) 4173 if (capacity && rq->nr_running == 1 && wl > env->imbalance)
4321 continue; 4174 continue;
4322 4175
4323 /* 4176 /*
@@ -4346,40 +4199,19 @@ find_busiest_queue(struct sched_domain *sd, struct sched_group *group,
4346/* Working cpumask for load_balance and load_balance_newidle. */ 4199/* Working cpumask for load_balance and load_balance_newidle. */
4347DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask); 4200DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
4348 4201
4349static int need_active_balance(struct sched_domain *sd, int idle, 4202static int need_active_balance(struct lb_env *env)
4350 int busiest_cpu, int this_cpu)
4351{ 4203{
4352 if (idle == CPU_NEWLY_IDLE) { 4204 struct sched_domain *sd = env->sd;
4205
4206 if (env->idle == CPU_NEWLY_IDLE) {
4353 4207
4354 /* 4208 /*
4355 * ASYM_PACKING needs to force migrate tasks from busy but 4209 * ASYM_PACKING needs to force migrate tasks from busy but
4356 * higher numbered CPUs in order to pack all tasks in the 4210 * higher numbered CPUs in order to pack all tasks in the
4357 * lowest numbered CPUs. 4211 * lowest numbered CPUs.
4358 */ 4212 */
4359 if ((sd->flags & SD_ASYM_PACKING) && busiest_cpu > this_cpu) 4213 if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
4360 return 1; 4214 return 1;
4361
4362 /*
4363 * The only task running in a non-idle cpu can be moved to this
4364 * cpu in an attempt to completely freeup the other CPU
4365 * package.
4366 *
4367 * The package power saving logic comes from
4368 * find_busiest_group(). If there are no imbalance, then
4369 * f_b_g() will return NULL. However when sched_mc={1,2} then
4370 * f_b_g() will select a group from which a running task may be
4371 * pulled to this cpu in order to make the other package idle.
4372 * If there is no opportunity to make a package idle and if
4373 * there are no imbalance, then f_b_g() will return NULL and no
4374 * action will be taken in load_balance_newidle().
4375 *
4376 * Under normal task pull operation due to imbalance, there
4377 * will be more than one task in the source run queue and
4378 * move_tasks() will succeed. ld_moved will be true and this
4379 * active balance code will not be triggered.
4380 */
4381 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
4382 return 0;
4383 } 4215 }
4384 4216
4385 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); 4217 return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
@@ -4397,7 +4229,6 @@ static int load_balance(int this_cpu, struct rq *this_rq,
4397{ 4229{
4398 int ld_moved, active_balance = 0; 4230 int ld_moved, active_balance = 0;
4399 struct sched_group *group; 4231 struct sched_group *group;
4400 unsigned long imbalance;
4401 struct rq *busiest; 4232 struct rq *busiest;
4402 unsigned long flags; 4233 unsigned long flags;
4403 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask); 4234 struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
@@ -4407,7 +4238,7 @@ static int load_balance(int this_cpu, struct rq *this_rq,
4407 .dst_cpu = this_cpu, 4238 .dst_cpu = this_cpu,
4408 .dst_rq = this_rq, 4239 .dst_rq = this_rq,
4409 .idle = idle, 4240 .idle = idle,
4410 .loop_break = sysctl_sched_nr_migrate, 4241 .loop_break = sched_nr_migrate_break,
4411 }; 4242 };
4412 4243
4413 cpumask_copy(cpus, cpu_active_mask); 4244 cpumask_copy(cpus, cpu_active_mask);
@@ -4415,8 +4246,7 @@ static int load_balance(int this_cpu, struct rq *this_rq,
4415 schedstat_inc(sd, lb_count[idle]); 4246 schedstat_inc(sd, lb_count[idle]);
4416 4247
4417redo: 4248redo:
4418 group = find_busiest_group(sd, this_cpu, &imbalance, idle, 4249 group = find_busiest_group(&env, cpus, balance);
4419 cpus, balance);
4420 4250
4421 if (*balance == 0) 4251 if (*balance == 0)
4422 goto out_balanced; 4252 goto out_balanced;
@@ -4426,7 +4256,7 @@ redo:
4426 goto out_balanced; 4256 goto out_balanced;
4427 } 4257 }
4428 4258
4429 busiest = find_busiest_queue(sd, group, idle, imbalance, cpus); 4259 busiest = find_busiest_queue(&env, group, cpus);
4430 if (!busiest) { 4260 if (!busiest) {
4431 schedstat_inc(sd, lb_nobusyq[idle]); 4261 schedstat_inc(sd, lb_nobusyq[idle]);
4432 goto out_balanced; 4262 goto out_balanced;
@@ -4434,7 +4264,7 @@ redo:
4434 4264
4435 BUG_ON(busiest == this_rq); 4265 BUG_ON(busiest == this_rq);
4436 4266
4437 schedstat_add(sd, lb_imbalance[idle], imbalance); 4267 schedstat_add(sd, lb_imbalance[idle], env.imbalance);
4438 4268
4439 ld_moved = 0; 4269 ld_moved = 0;
4440 if (busiest->nr_running > 1) { 4270 if (busiest->nr_running > 1) {
@@ -4445,10 +4275,9 @@ redo:
4445 * correctly treated as an imbalance. 4275 * correctly treated as an imbalance.
4446 */ 4276 */
4447 env.flags |= LBF_ALL_PINNED; 4277 env.flags |= LBF_ALL_PINNED;
4448 env.load_move = imbalance; 4278 env.src_cpu = busiest->cpu;
4449 env.src_cpu = busiest->cpu; 4279 env.src_rq = busiest;
4450 env.src_rq = busiest; 4280 env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
4451 env.loop_max = busiest->nr_running;
4452 4281
4453more_balance: 4282more_balance:
4454 local_irq_save(flags); 4283 local_irq_save(flags);
@@ -4490,7 +4319,7 @@ more_balance:
4490 if (idle != CPU_NEWLY_IDLE) 4319 if (idle != CPU_NEWLY_IDLE)
4491 sd->nr_balance_failed++; 4320 sd->nr_balance_failed++;
4492 4321
4493 if (need_active_balance(sd, idle, cpu_of(busiest), this_cpu)) { 4322 if (need_active_balance(&env)) {
4494 raw_spin_lock_irqsave(&busiest->lock, flags); 4323 raw_spin_lock_irqsave(&busiest->lock, flags);
4495 4324
4496 /* don't kick the active_load_balance_cpu_stop, 4325 /* don't kick the active_load_balance_cpu_stop,
@@ -4517,10 +4346,11 @@ more_balance:
4517 } 4346 }
4518 raw_spin_unlock_irqrestore(&busiest->lock, flags); 4347 raw_spin_unlock_irqrestore(&busiest->lock, flags);
4519 4348
4520 if (active_balance) 4349 if (active_balance) {
4521 stop_one_cpu_nowait(cpu_of(busiest), 4350 stop_one_cpu_nowait(cpu_of(busiest),
4522 active_load_balance_cpu_stop, busiest, 4351 active_load_balance_cpu_stop, busiest,
4523 &busiest->active_balance_work); 4352 &busiest->active_balance_work);
4353 }
4524 4354
4525 /* 4355 /*
4526 * We've kicked active balancing, reset the failure 4356 * We've kicked active balancing, reset the failure
@@ -4701,104 +4531,15 @@ static struct {
4701 unsigned long next_balance; /* in jiffy units */ 4531 unsigned long next_balance; /* in jiffy units */
4702} nohz ____cacheline_aligned; 4532} nohz ____cacheline_aligned;
4703 4533
4704#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) 4534static inline int find_new_ilb(int call_cpu)
4705/**
4706 * lowest_flag_domain - Return lowest sched_domain containing flag.
4707 * @cpu: The cpu whose lowest level of sched domain is to
4708 * be returned.
4709 * @flag: The flag to check for the lowest sched_domain
4710 * for the given cpu.
4711 *
4712 * Returns the lowest sched_domain of a cpu which contains the given flag.
4713 */
4714static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
4715{
4716 struct sched_domain *sd;
4717
4718 for_each_domain(cpu, sd)
4719 if (sd->flags & flag)
4720 break;
4721
4722 return sd;
4723}
4724
4725/**
4726 * for_each_flag_domain - Iterates over sched_domains containing the flag.
4727 * @cpu: The cpu whose domains we're iterating over.
4728 * @sd: variable holding the value of the power_savings_sd
4729 * for cpu.
4730 * @flag: The flag to filter the sched_domains to be iterated.
4731 *
4732 * Iterates over all the scheduler domains for a given cpu that has the 'flag'
4733 * set, starting from the lowest sched_domain to the highest.
4734 */
4735#define for_each_flag_domain(cpu, sd, flag) \
4736 for (sd = lowest_flag_domain(cpu, flag); \
4737 (sd && (sd->flags & flag)); sd = sd->parent)
4738
4739/**
4740 * find_new_ilb - Finds the optimum idle load balancer for nomination.
4741 * @cpu: The cpu which is nominating a new idle_load_balancer.
4742 *
4743 * Returns: Returns the id of the idle load balancer if it exists,
4744 * Else, returns >= nr_cpu_ids.
4745 *
4746 * This algorithm picks the idle load balancer such that it belongs to a
4747 * semi-idle powersavings sched_domain. The idea is to try and avoid
4748 * completely idle packages/cores just for the purpose of idle load balancing
4749 * when there are other idle cpu's which are better suited for that job.
4750 */
4751static int find_new_ilb(int cpu)
4752{ 4535{
4753 int ilb = cpumask_first(nohz.idle_cpus_mask); 4536 int ilb = cpumask_first(nohz.idle_cpus_mask);
4754 struct sched_group *ilbg;
4755 struct sched_domain *sd;
4756
4757 /*
4758 * Have idle load balancer selection from semi-idle packages only
4759 * when power-aware load balancing is enabled
4760 */
4761 if (!(sched_smt_power_savings || sched_mc_power_savings))
4762 goto out_done;
4763
4764 /*
4765 * Optimize for the case when we have no idle CPUs or only one
4766 * idle CPU. Don't walk the sched_domain hierarchy in such cases
4767 */
4768 if (cpumask_weight(nohz.idle_cpus_mask) < 2)
4769 goto out_done;
4770 4537
4771 rcu_read_lock();
4772 for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
4773 ilbg = sd->groups;
4774
4775 do {
4776 if (ilbg->group_weight !=
4777 atomic_read(&ilbg->sgp->nr_busy_cpus)) {
4778 ilb = cpumask_first_and(nohz.idle_cpus_mask,
4779 sched_group_cpus(ilbg));
4780 goto unlock;
4781 }
4782
4783 ilbg = ilbg->next;
4784
4785 } while (ilbg != sd->groups);
4786 }
4787unlock:
4788 rcu_read_unlock();
4789
4790out_done:
4791 if (ilb < nr_cpu_ids && idle_cpu(ilb)) 4538 if (ilb < nr_cpu_ids && idle_cpu(ilb))
4792 return ilb; 4539 return ilb;
4793 4540
4794 return nr_cpu_ids; 4541 return nr_cpu_ids;
4795} 4542}
4796#else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
4797static inline int find_new_ilb(int call_cpu)
4798{
4799 return nr_cpu_ids;
4800}
4801#endif
4802 4543
4803/* 4544/*
4804 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the 4545 * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
@@ -5021,7 +4762,7 @@ static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
5021 4762
5022 raw_spin_lock_irq(&this_rq->lock); 4763 raw_spin_lock_irq(&this_rq->lock);
5023 update_rq_clock(this_rq); 4764 update_rq_clock(this_rq);
5024 update_cpu_load(this_rq); 4765 update_idle_cpu_load(this_rq);
5025 raw_spin_unlock_irq(&this_rq->lock); 4766 raw_spin_unlock_irq(&this_rq->lock);
5026 4767
5027 rebalance_domains(balance_cpu, CPU_IDLE); 4768 rebalance_domains(balance_cpu, CPU_IDLE);