1
linux/arch/um/os-Linux/time.c

116 lines
2.5 KiB
C
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
* Copyright (C) 2012-2014 Cisco Systems
* Copyright (C) 2000 - 2007 Jeff Dike (jdike{addtoit,linux.intel}.com)
*/
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <kern_util.h>
#include <os.h>
#include <string.h>
static timer_t event_high_res_timer = 0;
static inline long long timespec_to_ns(const struct timespec *ts)
{
return ((long long) ts->tv_sec * UM_NSEC_PER_SEC) + ts->tv_nsec;
}
long long os_persistent_clock_emulation(void)
{
struct timespec realtime_tp;
clock_gettime(CLOCK_REALTIME, &realtime_tp);
return timespec_to_ns(&realtime_tp);
}
/**
* os_timer_create() - create an new posix (interval) timer
*/
int os_timer_create(void)
{
timer_t *t = &event_high_res_timer;
if (timer_create(CLOCK_MONOTONIC, NULL, t) == -1)
return -1;
return 0;
}
int os_timer_set_interval(unsigned long long nsecs)
{
struct itimerspec its;
its.it_value.tv_sec = nsecs / UM_NSEC_PER_SEC;
its.it_value.tv_nsec = nsecs % UM_NSEC_PER_SEC;
its.it_interval.tv_sec = nsecs / UM_NSEC_PER_SEC;
its.it_interval.tv_nsec = nsecs % UM_NSEC_PER_SEC;
if (timer_settime(event_high_res_timer, 0, &its, NULL) == -1)
return -errno;
return 0;
}
int os_timer_one_shot(unsigned long long nsecs)
{
struct itimerspec its = {
.it_value.tv_sec = nsecs / UM_NSEC_PER_SEC,
.it_value.tv_nsec = nsecs % UM_NSEC_PER_SEC,
.it_interval.tv_sec = 0,
.it_interval.tv_nsec = 0, // we cheat here
};
timer_settime(event_high_res_timer, 0, &its, NULL);
return 0;
}
/**
* os_timer_disable() - disable the posix (interval) timer
*/
void os_timer_disable(void)
{
struct itimerspec its;
memset(&its, 0, sizeof(struct itimerspec));
timer_settime(event_high_res_timer, 0, &its, NULL);
}
long long os_nsecs(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC,&ts);
return timespec_to_ns(&ts);
}
/**
* os_idle_sleep() - sleep until interrupted
*/
void os_idle_sleep(void)
{
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: 49da38a3ef33 ("um: Simplify os_idle_sleep() and sleep longer") Signed-off-by: Johannes Berg <johannes.berg@intel.com> Acked-By: Anton Ivanov <anton.ivanov@cambridgegreys.com> Signed-off-by: Richard Weinberger <richard@nod.at>
2021-01-22 13:40:23 -07:00
struct itimerspec its;
sigset_t set, old;
/* block SIGALRM while we analyze the timer state */
sigemptyset(&set);
sigaddset(&set, SIGALRM);
sigprocmask(SIG_BLOCK, &set, &old);
/* check the timer, and if it'll fire then wait for it */
timer_gettime(event_high_res_timer, &its);
if (its.it_value.tv_sec || its.it_value.tv_nsec)
sigsuspend(&old);
/* either way, restore the signal mask */
sigprocmask(SIG_UNBLOCK, &set, NULL);
}