Skip to content

Commit 23c953f

Browse files
Joonwoo ParkExactExampl
authored andcommitted
sched: prevent out of bound access in sched_group_energy()
group_idle_state() can return INT_MAX + 1 which is undefined behaviour when there is no CPUs in sched_group. Prevent such by error correctly. Change-Id: If9796c829c091e461231569dc38c5e5456f58037 Signed-off-by: Joonwoo Park <joonwoop@codeaurora.org> Signed-off-by: Satya Durga Srinivasu Prabhala <satyap@codeaurora.org> [clingutla@codeaurora.org: Fixed trivial merge conflicts and squashed msm-4.14 change] Signed-off-by: Lingutla Chandrasekhar <clingutla@codeaurora.org> (cherry picked from commit bb5b0e61527011e4ebfc4058713a9068da9e7492)
1 parent afee777 commit 23c953f

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

kernel/sched/fair.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5792,6 +5792,9 @@ static int group_idle_state(struct energy_env *eenv, int cpu_idx)
57925792
for_each_cpu(i, sched_group_cpus(sg))
57935793
state = min(state, idle_get_state_idx(cpu_rq(i)));
57945794

5795+
if (unlikely(state == INT_MAX))
5796+
return -EINVAL;
5797+
57955798
/* Take non-cpuidle idling into account (active idle/arch_cpu_idle()) */
57965799
state++;
57975800

@@ -5858,7 +5861,7 @@ static int group_idle_state(struct energy_env *eenv, int cpu_idx)
58585861
* The required scaling will be performed just one time, by the calling
58595862
* functions, once we accumulated the contributons for all the SGs.
58605863
*/
5861-
static void calc_sg_energy(struct energy_env *eenv)
5864+
static int calc_sg_energy(struct energy_env *eenv)
58625865
{
58635866
struct sched_group *sg = eenv->sg;
58645867
int busy_energy, idle_energy;
@@ -5887,6 +5890,8 @@ static void calc_sg_energy(struct energy_env *eenv)
58875890

58885891
/* Compute IDLE energy */
58895892
idle_idx = group_idle_state(eenv, cpu_idx);
5893+
if (unlikely(idle_idx < 0))
5894+
return idle_idx;
58905895
idle_power = sg->sge->idle_states[idle_idx].power;
58915896

58925897
idle_energy = SCHED_CAPACITY_SCALE - sg_util;
@@ -5895,6 +5900,7 @@ static void calc_sg_energy(struct energy_env *eenv)
58955900
total_energy = busy_energy + idle_energy;
58965901
eenv->cpu[cpu_idx].energy += total_energy;
58975902
}
5903+
return 0;
58985904
}
58995905

59005906
/*
@@ -5956,7 +5962,8 @@ static int compute_energy(struct energy_env *eenv)
59565962
* CPUs in the current visited SG.
59575963
*/
59585964
eenv->sg = sg;
5959-
calc_sg_energy(eenv);
5965+
if (calc_sg_energy(eenv))
5966+
return -EINVAL;
59605967

59615968
/* remove CPUs we have just visited */
59625969
if (!sd->child) {

0 commit comments

Comments
 (0)