Skip to contentSkip to conversation

sched: 64bit: fix overflow(was: fix divide by zero)

PATCH Applied Lai Jiangshan 2 participants 2 messages Jun 12, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 sched: 64bit: fix overflow(was: fix divide by zero)

(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 be overflow when entities are too many.

Although, overflow is very hardly occurred, but it break fairness when it is occurred. And 64bits system have more memory than 32bits system and 64bits system can create more process usually, so overflow many be occurred more.

This patch guarantee fairness when overflow in 64bits system. Thanks to the optimization of compiler, it change nothing in 32bits system.

---

kernel/sched.c +7 -2
index bfb8ad8..e277533 100644
@@ -1337,8 +1337,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;
 	/*
Acked-by on v1 2/2
#1 lore raw
Quoted 6 lines · Lai Jiangshan

(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 be overflow when entities are too many.

Quoted 7 lines

Although, overflow is very hardly occurred, but it break fairness when it is occurred. And 64bits system have more memory than 32bits system and 64bits system can create more process usually, so overflow many be occurred more.

This patch guarantee fairness when overflow in 64bits system. Thanks to the optimization of compiler, it change nothing in 32bits system.

Yeah,. bit sad for that one branch extra on 64-bit, but I get the unlikely is as good as we can get it.

Quoted 26 lines

---

kernel/sched.c +7 -2
index bfb8ad8..e277533 100644
@@ -1337,8 +1337,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;
 	/*