Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

uml: track and make up lost ticks

Alarm delivery could be noticably late in the !CONFIG_NOHZ case because lost
ticks weren't being taken into account. This is now treated more carefully,
with the time between ticks being calculated and the appropriate number of
ticks delivered to the timekeeping system.

Cc: Nix <nix@esperi.org.uk>
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Jeff Dike and committed by
Linus Torvalds
fe2cc53e 3d88958e

+53 -6
+2 -2
arch/um/include/process.h
··· 11 11 /* Copied from linux/compiler-gcc.h since we can't include it directly */ 12 12 #define barrier() __asm__ __volatile__("": : :"memory") 13 13 14 - extern void sig_handler(int sig, struct sigcontext sc); 15 - extern void alarm_handler(int sig, struct sigcontext sc); 14 + extern void sig_handler(int sig, struct sigcontext *sc); 15 + extern void alarm_handler(int sig, struct sigcontext *sc); 16 16 17 17 #endif
+1
arch/um/os-Linux/signal.c
··· 12 12 #include "as-layout.h" 13 13 #include "kern_util.h" 14 14 #include "os.h" 15 + #include "process.h" 15 16 #include "sysdep/barrier.h" 16 17 #include "sysdep/sigcontext.h" 17 18 #include "user.h"
+50 -4
arch/um/os-Linux/time.c
··· 9 9 #include <time.h> 10 10 #include <sys/time.h> 11 11 #include "kern_constants.h" 12 + #include "kern_util.h" 12 13 #include "os.h" 14 + #include "process.h" 13 15 #include "user.h" 14 16 15 17 int set_interval(void) ··· 60 58 long long disable_timer(void) 61 59 { 62 60 struct itimerval time = ((struct itimerval) { { 0, 0 }, { 0, 0 } }); 61 + int remain, max = UM_NSEC_PER_SEC / UM_HZ; 63 62 64 63 if (setitimer(ITIMER_VIRTUAL, &time, &time) < 0) 65 64 printk(UM_KERN_ERR "disable_timer - setitimer failed, " 66 65 "errno = %d\n", errno); 67 66 68 - return timeval_to_ns(&time.it_value); 67 + remain = timeval_to_ns(&time.it_value); 68 + if (remain > max) 69 + remain = max; 70 + 71 + return remain; 69 72 } 70 73 71 74 long long os_nsecs(void) ··· 86 79 { 87 80 return 0; 88 81 } 82 + 83 + static void deliver_alarm(void) 84 + { 85 + alarm_handler(SIGVTALRM, NULL); 86 + } 87 + 88 + static unsigned long long sleep_time(unsigned long long nsecs) 89 + { 90 + return nsecs; 91 + } 92 + 89 93 #else 94 + unsigned long long last_tick; 95 + unsigned long long skew; 96 + 97 + static void deliver_alarm(void) 98 + { 99 + unsigned long long this_tick = os_nsecs(); 100 + int one_tick = UM_NSEC_PER_SEC / UM_HZ; 101 + 102 + if (last_tick == 0) 103 + last_tick = this_tick - one_tick; 104 + 105 + skew += this_tick - last_tick; 106 + 107 + while (skew >= one_tick) { 108 + alarm_handler(SIGVTALRM, NULL); 109 + skew -= one_tick; 110 + } 111 + 112 + last_tick = this_tick; 113 + } 114 + 115 + static unsigned long long sleep_time(unsigned long long nsecs) 116 + { 117 + return nsecs > skew ? nsecs - skew : 0; 118 + } 119 + 90 120 static inline long long timespec_to_us(const struct timespec *ts) 91 121 { 92 122 return ((long long) ts->tv_sec * UM_USEC_PER_SEC) + ··· 146 102 */ 147 103 if (start_usecs > usec) 148 104 start_usecs = usec; 105 + 106 + start_usecs -= skew / UM_NSEC_PER_USEC; 149 107 tv = ((struct timeval) { .tv_sec = start_usecs / UM_USEC_PER_SEC, 150 108 .tv_usec = start_usecs % UM_USEC_PER_SEC }); 151 109 interval = ((struct itimerval) { { 0, usec }, tv }); ··· 158 112 return 0; 159 113 } 160 114 #endif 161 - 162 - extern void alarm_handler(int sig, struct sigcontext *sc); 163 115 164 116 void idle_sleep(unsigned long long nsecs) 165 117 { ··· 170 126 */ 171 127 if (nsecs == 0) 172 128 nsecs = UM_NSEC_PER_SEC / UM_HZ; 129 + 130 + nsecs = sleep_time(nsecs); 173 131 ts = ((struct timespec) { .tv_sec = nsecs / UM_NSEC_PER_SEC, 174 132 .tv_nsec = nsecs % UM_NSEC_PER_SEC }); 175 133 176 134 if (nanosleep(&ts, &ts) == 0) 177 - alarm_handler(SIGVTALRM, NULL); 135 + deliver_alarm(); 178 136 after_sleep_interval(&ts); 179 137 }