thinktime: Avoid calculating a negative time left to wait
authorMichael Kelley <mikelley@microsoft.com>
Fri, 14 Jul 2023 17:06:01 +0000 (17:06 +0000)
committerVincent Fu <vincent.fu@samsung.com>
Fri, 14 Jul 2023 18:03:34 +0000 (14:03 -0400)
commit14adf6e31487aa2bc8e47cd037428036089a3834
tree32110c2007d15e2fb8b924d4d88110bc8b3c3524
parentbb08a260413e54465f370523ae26a1bebb42308c
thinktime: Avoid calculating a negative time left to wait

When the thinktime_spin option specifies a value that is within
a few milliseconds of the thinktime value, in handle_thinktime()
it's possible in a VM environment for the duration of usec_spin()
to exceed the thinktime value. While doing usec_spin(), the vCPU
could get de-scheduled or the hypervisor could steal CPU time
from the vCPU. When the guest vCPU runs after being scheduled
again, it may read the clock and find that more time has elapsed
than intended. In such a case, the time left to wait could be
calculated as a negative value. Subsequent calculations then go
awry because the time left is cast as unsigned.

Fix this by detecting when the time left would go negative and
just set it to zero.

Fixes: 1a9bf8146 ("Add option to ignore thinktime for rated IO")
Fixes: https://github.com/axboe/fio/issues/1588
Link: https://lore.kernel.org/fio/1689354334-131024-1-git-send-email-mikelley@microsoft.com/T/#u
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Signed-off-by: Vincent Fu <vincent.fu@samsung.com>
backend.c