qemu-timer: Avoid overflows when converting timeout to struct timespec
In qemu_poll_ns(), when we convert an int64_t nanosecond timeout into a struct timespec, we may accidentally run into overflow problems if the timeout is very long. This happens because the tv_sec field is a time_t, which is signed, so we might end up setting it to a negative value by mistake. This will result in what was intended to be a near-infinite timeout turning into an instantaneous timeout, and we'll busy loop. Cap the maximum timeout at INT32_MAX seconds (about 68 years) to avoid this problem. This specifically manifested on ARM hosts as an extreme slowdown on guest shutdown (when the guest reprogrammed the PL031 RTC to not generate alarms using a very long timeout) but could happen on other hosts and guests too. Reported-by: Christoffer Dall <christoffer.dall@linaro.org> Cc: qemu-stable@nongnu.org Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Fam Zheng <famz@redhat.com> Message-id: 1416939705-1272-1-git-send-email-peter.maydell@linaro.org
This commit is contained in:
parent
3ef4ebcc5c
commit
490309fcfb
@ -314,7 +314,14 @@ int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
|
|||||||
return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
|
return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
|
||||||
} else {
|
} else {
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
ts.tv_sec = timeout / 1000000000LL;
|
int64_t tvsec = timeout / 1000000000LL;
|
||||||
|
/* Avoid possibly overflowing and specifying a negative number of
|
||||||
|
* seconds, which would turn a very long timeout into a busy-wait.
|
||||||
|
*/
|
||||||
|
if (tvsec > (int64_t)INT32_MAX) {
|
||||||
|
tvsec = INT32_MAX;
|
||||||
|
}
|
||||||
|
ts.tv_sec = tvsec;
|
||||||
ts.tv_nsec = timeout % 1000000000LL;
|
ts.tv_nsec = timeout % 1000000000LL;
|
||||||
return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
|
return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user