Skip to content

Commit 9868c20

Browse files
jmberg-intelrichardweinberger
authored andcommitted
um: fix os_idle_sleep() to not hang
Changing os_idle_sleep() to use pause() (I accidentally described it as an empty select() in the commit log because I had changed it from that to pause() in a later revision) exposed a race condition in the idle code. The following can happen: timer_settime(0, 0, {it_interval={tv_sec=0, tv_nsec=0}, it_value={tv_sec=0, tv_nsec=624017}}, NULL) = 0 ... <SIGALRM is delivered but we're already on the way to idle> pause() and we now hang forever. This was previously possible as well, but it could never cause UML to hang for more than a second since we could only sleep for that much, so at most you'd notice a "hiccup" in the UML. Obviously, any sort of external interrupt also "saves" it and interrupts pause(). Fix this by properly handling the race, rather than papering over it again: - first, block SIGALRM, and obtain the old signal set - check the timer - suspend, waiting for any signal out of the old set, if, and only if, the timer will fire in the future - restore the old signal mask This ensures race-free operation: as it's blocked, the signal won't be delivered while we're looking at the timer even if it were to be triggered right _after_ we've returned from timer_gettime() with a non-zero value (telling us the timer will trigger). Thus, despite getting to sigsuspend() because timer_gettime() told us we're still waiting, we'll not hang because sigsuspend() will return immediately due to the pending signal. Fixes: 49da38a ("um: Simplify os_idle_sleep() and sleep longer") Signed-off-by: Johannes Berg <[email protected]> Acked-By: Anton Ivanov <[email protected]> Signed-off-by: Richard Weinberger <[email protected]>
1 parent a31e9c4 commit 9868c20

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

arch/um/os-Linux/time.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,18 @@ long long os_nsecs(void)
104104
*/
105105
void os_idle_sleep(void)
106106
{
107-
pause();
107+
struct itimerspec its;
108+
sigset_t set, old;
109+
110+
/* block SIGALRM while we analyze the timer state */
111+
sigemptyset(&set);
112+
sigaddset(&set, SIGALRM);
113+
sigprocmask(SIG_BLOCK, &set, &old);
114+
115+
/* check the timer, and if it'll fire then wait for it */
116+
timer_gettime(event_high_res_timer, &its);
117+
if (its.it_value.tv_sec || its.it_value.tv_nsec)
118+
sigsuspend(&old);
119+
/* either way, restore the signal mask */
120+
sigprocmask(SIG_UNBLOCK, &set, NULL);
108121
}

0 commit comments

Comments
 (0)