Skip to content

Commit

Permalink
sched: 64-bit: fix arithmetics overflow
Browse files Browse the repository at this point in the history
(overflow means weight >= 2^32 here, because inv_weigh = 2^32/weight)

A weight of a cfs_rq is the sum of weights of which entities
are queued on this cfs_rq, so it will overflow when there are
too many entities.

Although, overflow occurs very rarely, but it break fairness when
it occurs. 64-bits systems have more memory than 32-bit systems
and 64-bit systems can create more process usually, so overflow may
occur more frequently.

This patch guarantees fairness when overflow happens on 64-bit systems.
Thanks to the optimization of compiler, it changes nothing on 32-bit.

Signed-off-by: Lai Jiangshan <[email protected]>
Acked-by: Peter Zijlstra <[email protected]>
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
Lai Jiangshan authored and Ingo Molnar committed Jun 12, 2008
1 parent 2e08478 commit 7a232e0
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions kernel/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -1340,8 +1340,13 @@ calc_delta_mine(unsigned long delta_exec, unsigned long weight,
{
u64 tmp;

if (!lw->inv_weight)
lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1);
if (!lw->inv_weight) {
if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
lw->inv_weight = 1;
else
lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
/ (lw->weight+1);
}

tmp = (u64)delta_exec * weight;
/*
Expand Down

0 comments on commit 7a232e0

Please sign in to comment.