Skip to content

Commit 7a232e0

Browse files
Lai JiangshanIngo Molnar
authored andcommitted
sched: 64-bit: fix arithmetics overflow
(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]>
1 parent 2e08478 commit 7a232e0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

kernel/sched.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,13 @@ calc_delta_mine(unsigned long delta_exec, unsigned long weight,
13401340
{
13411341
u64 tmp;
13421342

1343-
if (!lw->inv_weight)
1344-
lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)/(lw->weight+1);
1343+
if (!lw->inv_weight) {
1344+
if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1345+
lw->inv_weight = 1;
1346+
else
1347+
lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1348+
/ (lw->weight+1);
1349+
}
13451350

13461351
tmp = (u64)delta_exec * weight;
13471352
/*

0 commit comments

Comments
 (0)