Skip to content

Commit

Permalink
blk-iocost: fix divide-by-zero in transfer_surpluses()
Browse files Browse the repository at this point in the history
Conceptually, root_iocg->hweight_donating must be less than WEIGHT_ONE but
all hweight calculations round up and thus it may end up >= WEIGHT_ONE
triggering divide-by-zero and other issues. Bound the value to avoid
surprises.

Fixes: e08d02a ("blk-iocost: implement Andy's method for donation weight updates")
Signed-off-by: Tejun Heo <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
htejun authored and axboe committed Sep 11, 2020
1 parent 0806e60 commit 769b628
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions block/blk-iocost.c
Original file line number Diff line number Diff line change
Expand Up @@ -1881,15 +1881,21 @@ static void transfer_surpluses(struct list_head *surpluses, struct ioc_now *now)

/*
* Calculate the global donation rate (gamma) - the rate to adjust
* non-donating budgets by. No need to use 64bit multiplication here as
* the first operand is guaranteed to be smaller than WEIGHT_ONE
* (1<<16).
* non-donating budgets by.
*
* No need to use 64bit multiplication here as the first operand is
* guaranteed to be smaller than WEIGHT_ONE (1<<16).
*
* We know that there are beneficiary nodes and the sum of the donating
* hweights can't be whole; however, due to the round-ups during hweight
* calculations, root_iocg->hweight_donating might still end up equal to
* or greater than whole. Limit the range when calculating the divider.
*
* gamma = (1 - t_r') / (1 - t_r)
*/
gamma = DIV_ROUND_UP(
(WEIGHT_ONE - root_iocg->hweight_after_donation) * WEIGHT_ONE,
WEIGHT_ONE - root_iocg->hweight_donating);
WEIGHT_ONE - min_t(u32, root_iocg->hweight_donating, WEIGHT_ONE - 1));

/*
* Calculate adjusted hwi, child_adjusted_sum and inuse for the inner
Expand Down

0 comments on commit 769b628

Please sign in to comment.