Skip to content

Commit bb8d558

Browse files
YuKuai-huaweiaxboe
authored andcommitted
blk-throttle: fix wrong comparation while 'carryover_ios/bytes' is negative
carryover_ios/bytes[] can be negative in the case that ios are dispatched in the slice in advance, and then configuration is updated. For example: 1) set iops limit to 1000, and slice start is 0, slice end is 100ms; 2) current time is 0, and 100 ios are dispatched, those ios will not be throttled, hence io_disp is 100; 3) still at current time 0, update iops limit to 100, then carryover_ios is (0 - 100) = -100; 4) then, dispatch a new io at time 0, the expected result is that this io will wait for 1s. The calculation in tg_within_iops_limit: io_disp = 0; io_allowed = calculate_io_allowed + carryover_ios = 10 + (-100) = -90; io won't be throttled if (io_disp + 1 < io_allowed) passed. Before this patch, in step 4) (io_disp + 1 < io_allowed) is passed, because -90 for unsigned value is very huge, and such io won't be throttled. Fix this problem by checking if 'io/bytes_allowed' is negative first. Signed-off-by: Yu Kuai <yukuai3@huawei.com> Acked-by: Tejun Heo <tj@kernel.org> Link: https://lore.kernel.org/r/20230816012708.1193747-3-yukuai1@huaweicloud.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent ef10039 commit bb8d558

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

block/blk-throttle.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ static unsigned long tg_within_iops_limit(struct throtl_grp *tg, struct bio *bio
825825
u32 iops_limit)
826826
{
827827
bool rw = bio_data_dir(bio);
828-
unsigned int io_allowed;
828+
int io_allowed;
829829
unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
830830

831831
if (iops_limit == UINT_MAX) {
@@ -838,9 +838,8 @@ static unsigned long tg_within_iops_limit(struct throtl_grp *tg, struct bio *bio
838838
jiffy_elapsed_rnd = roundup(jiffy_elapsed + 1, tg->td->throtl_slice);
839839
io_allowed = calculate_io_allowed(iops_limit, jiffy_elapsed_rnd) +
840840
tg->carryover_ios[rw];
841-
if (tg->io_disp[rw] + 1 <= io_allowed) {
841+
if (io_allowed > 0 && tg->io_disp[rw] + 1 <= io_allowed)
842842
return 0;
843-
}
844843

845844
/* Calc approx time to dispatch */
846845
jiffy_wait = jiffy_elapsed_rnd - jiffy_elapsed;
@@ -851,7 +850,8 @@ static unsigned long tg_within_bps_limit(struct throtl_grp *tg, struct bio *bio,
851850
u64 bps_limit)
852851
{
853852
bool rw = bio_data_dir(bio);
854-
u64 bytes_allowed, extra_bytes;
853+
long long bytes_allowed;
854+
u64 extra_bytes;
855855
unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
856856
unsigned int bio_size = throtl_bio_data_size(bio);
857857

@@ -869,9 +869,8 @@ static unsigned long tg_within_bps_limit(struct throtl_grp *tg, struct bio *bio,
869869
jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
870870
bytes_allowed = calculate_bytes_allowed(bps_limit, jiffy_elapsed_rnd) +
871871
tg->carryover_bytes[rw];
872-
if (tg->bytes_disp[rw] + bio_size <= bytes_allowed) {
872+
if (bytes_allowed > 0 && tg->bytes_disp[rw] + bio_size <= bytes_allowed)
873873
return 0;
874-
}
875874

876875
/* Calc approx time to dispatch */
877876
extra_bytes = tg->bytes_disp[rw] + bio_size - bytes_allowed;

0 commit comments

Comments
 (0)