Skip to content

Commit

Permalink
sched: fair-group: fix a Div0 error of the fair group scheduler
Browse files Browse the repository at this point in the history
When I echoed 0 into the "cpu.shares" file, a Div0 error occured.

We found it is caused by the following calling.

   sched_group_set_shares(tg, shares)
       set_se_shares(tg->se[i], shares/nr_cpu_ids)
           __set_se_shares(se, shares)
               div64_64((1ULL<<32), shares)

When the echoed value was less than the number of processores, the result of the
sentence "shares/nr_cpu_ids" was 0, and then the system called div64() to divide
the result, the Div0 error occured.

It is unnecessary that the shares value is divided by nr_cpu_ids, I think.
Because in the function  __update_group_shares_cpu() and init_tg_cfs_entry(),
the shares value isn't divided by nr_cpu_ids when setting shares of the sched
entity.

This patch fixes this bug. And echoing ULONG_MAX value into cpu.shares also
causes Div0 error, so we set a macro MAX_SHARES to limit the max value of
shares.

Signed-off-by: Miao Xie <[email protected]>
Acked-by: Peter Zijlstra <[email protected]>
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
Miao Xie authored and Ingo Molnar committed May 5, 2008
1 parent 712555e commit cb4ad1f
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions kernel/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,13 @@ static DEFINE_SPINLOCK(task_group_lock);
# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
#endif

/*
* A weight of 0, 1 or ULONG_MAX can cause arithmetics problems.
* (The default weight is 1024 - so there's no practical
* limitation from this.)
*/
#define MIN_SHARES 2
#define MAX_SHARES (ULONG_MAX - 1)

static int init_task_group_load = INIT_TASK_GROUP_LOAD;
#endif
Expand Down Expand Up @@ -1804,6 +1810,8 @@ __update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd,

if (shares < MIN_SHARES)
shares = MIN_SHARES;
else if (shares > MAX_SHARES)
shares = MAX_SHARES;

__set_se_shares(tg->se[tcpu], shares);
}
Expand Down Expand Up @@ -8785,13 +8793,10 @@ int sched_group_set_shares(struct task_group *tg, unsigned long shares)
if (!tg->se[0])
return -EINVAL;

/*
* A weight of 0 or 1 can cause arithmetics problems.
* (The default weight is 1024 - so there's no practical
* limitation from this.)
*/
if (shares < MIN_SHARES)
shares = MIN_SHARES;
else if (shares > MAX_SHARES)
shares = MAX_SHARES;

mutex_lock(&shares_mutex);
if (tg->shares == shares)
Expand All @@ -8816,7 +8821,7 @@ int sched_group_set_shares(struct task_group *tg, unsigned long shares)
* force a rebalance
*/
cfs_rq_set_shares(tg->cfs_rq[i], 0);
set_se_shares(tg->se[i], shares/nr_cpu_ids);
set_se_shares(tg->se[i], shares);
}

/*
Expand Down

0 comments on commit cb4ad1f

Please sign in to comment.