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

um: Support time travel mode

Sometimes it can be useful to run with "time travel" inside the
UML instance, for example for testing. For example, some tests
for the wireless subsystem and userspace are based on hwsim, a
virtual wireless adapter. Some tests can take a long time to
run because they e.g. wait for 120 seconds to elapse for some
regulatory checks. This obviously goes faster if it need not
actually wait that long, but time inside the test environment
just "bumps up" when there's nothing to do.

Add CONFIG_UML_TIME_TRAVEL_SUPPORT to enable code to support
such modes at runtime, selected on the command line:
* just "time-travel", in which time inside the UML instance
can move faster than real time, if there's nothing to do
* "time-travel=inf-cpu" in which time also moves slower and
any CPU processing takes no time at all, which allows to
implement consistent behaviour regardless of host CPU load
(or speed) or debug overhead.

An additional "time-travel-start=<seconds>" parameter is also
supported in this case to start the wall clock at this time
(in unix epoch).

With this enabled, the test mentioned above goes from a runtime
of about 140 seconds (with startup overhead and all) to being
CPU bound and finishing in 15 seconds (on my slow laptop).

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: Richard Weinberger <richard@nod.at>

authored by

Johannes Berg and committed by
Richard Weinberger
06503870 c7c6f3b9

+233 -7
+12
arch/um/Kconfig
··· 184 184 185 185 If unsure, say Y. 186 186 187 + config UML_TIME_TRAVEL_SUPPORT 188 + bool 189 + prompt "Support time-travel mode (e.g. for test execution)" 190 + help 191 + Enable this option to support time travel inside the UML instance. 192 + 193 + After enabling this option, two modes are accessible at runtime 194 + (selected by the kernel command line), see the kernel's command- 195 + line help for more details. 196 + 197 + It is safe to say Y, but you probably don't need this. 198 + 187 199 endmenu 188 200 189 201 source "arch/um/drivers/Kconfig"
+46
arch/um/include/shared/timer-internal.h
··· 10 10 #define TIMER_MULTIPLIER 256 11 11 #define TIMER_MIN_DELTA 500 12 12 13 + enum time_travel_mode { 14 + TT_MODE_OFF, 15 + TT_MODE_BASIC, 16 + TT_MODE_INFCPU, 17 + }; 18 + 19 + enum time_travel_timer_mode { 20 + TT_TMR_DISABLED, 21 + TT_TMR_ONESHOT, 22 + TT_TMR_PERIODIC, 23 + }; 24 + 25 + #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT 26 + extern enum time_travel_mode time_travel_mode; 27 + extern unsigned long long time_travel_time; 28 + extern enum time_travel_timer_mode time_travel_timer_mode; 29 + extern unsigned long long time_travel_timer_expiry; 30 + extern unsigned long long time_travel_timer_interval; 31 + 32 + static inline void time_travel_set_time(unsigned long long ns) 33 + { 34 + time_travel_time = ns; 35 + } 36 + 37 + static inline void time_travel_set_timer(enum time_travel_timer_mode mode, 38 + unsigned long long expiry) 39 + { 40 + time_travel_timer_mode = mode; 41 + time_travel_timer_expiry = expiry; 42 + } 43 + #else 44 + #define time_travel_mode TT_MODE_OFF 45 + #define time_travel_time 0 46 + #define time_travel_timer_expiry 0 47 + #define time_travel_timer_interval 0 48 + 49 + static inline void time_travel_set_time(unsigned long long ns) 50 + { 51 + } 52 + 53 + static inline void time_travel_set_timer(enum time_travel_timer_mode mode, 54 + unsigned long long expiry) 55 + { 56 + } 57 + #endif 58 + 13 59 #endif
+41 -1
arch/um/kernel/process.c
··· 203 203 kmalloc_ok = save_kmalloc_ok; 204 204 } 205 205 206 + static void time_travel_sleep(unsigned long long duration) 207 + { 208 + unsigned long long next = time_travel_time + duration; 209 + 210 + if (time_travel_mode != TT_MODE_INFCPU) 211 + os_timer_disable(); 212 + 213 + if (time_travel_timer_mode != TT_TMR_DISABLED || 214 + time_travel_timer_expiry < next) { 215 + if (time_travel_timer_mode == TT_TMR_ONESHOT) 216 + time_travel_timer_mode = TT_TMR_DISABLED; 217 + /* 218 + * time_travel_time will be adjusted in the timer 219 + * IRQ handler so it works even when the signal 220 + * comes from the OS timer 221 + */ 222 + deliver_alarm(); 223 + } else { 224 + time_travel_set_time(next); 225 + } 226 + 227 + if (time_travel_mode != TT_MODE_INFCPU) { 228 + if (time_travel_timer_mode == TT_TMR_PERIODIC) 229 + os_timer_set_interval(time_travel_timer_interval); 230 + else if (time_travel_timer_mode == TT_TMR_ONESHOT) 231 + os_timer_one_shot(time_travel_timer_expiry - next); 232 + } 233 + } 234 + 235 + static void um_idle_sleep(void) 236 + { 237 + unsigned long long duration = UM_NSEC_PER_SEC; 238 + 239 + if (time_travel_mode != TT_MODE_OFF) { 240 + time_travel_sleep(duration); 241 + } else { 242 + os_idle_sleep(duration); 243 + } 244 + } 245 + 206 246 void arch_cpu_idle(void) 207 247 { 208 248 cpu_tasks[current_thread_info()->cpu].pid = os_getpid(); 209 - os_idle_sleep(UM_NSEC_PER_SEC); 249 + um_idle_sleep(); 210 250 local_irq_enable(); 211 251 } 212 252
+11
arch/um/kernel/skas/syscall.c
··· 10 10 #include <sysdep/ptrace.h> 11 11 #include <sysdep/ptrace_user.h> 12 12 #include <sysdep/syscalls.h> 13 + #include <shared/timer-internal.h> 13 14 14 15 void handle_syscall(struct uml_pt_regs *r) 15 16 { 16 17 struct pt_regs *regs = container_of(r, struct pt_regs, regs); 17 18 int syscall; 19 + 20 + /* 21 + * If we have infinite CPU resources, then make every syscall also a 22 + * preemption point, since we don't have any other preemption in this 23 + * case, and kernel threads would basically never run until userspace 24 + * went to sleep, even if said userspace interacts with the kernel in 25 + * various ways. 26 + */ 27 + if (time_travel_mode == TT_MODE_INFCPU) 28 + schedule(); 18 29 19 30 /* Initialize the syscall number and default return value. */ 20 31 UPT_SYSCALL_NR(r) = PT_SYSCALL_NR(r->gp);
+123 -6
arch/um/kernel/time.c
··· 19 19 #include <kern_util.h> 20 20 #include <os.h> 21 21 #include <timer-internal.h> 22 + #include <shared/init.h> 23 + 24 + #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT 25 + enum time_travel_mode time_travel_mode; 26 + unsigned long long time_travel_time; 27 + enum time_travel_timer_mode time_travel_timer_mode; 28 + unsigned long long time_travel_timer_expiry; 29 + unsigned long long time_travel_timer_interval; 30 + 31 + static bool time_travel_start_set; 32 + static unsigned long long time_travel_start; 33 + #else 34 + #define time_travel_start_set 0 35 + #define time_travel_start 0 36 + #endif 22 37 23 38 void timer_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs) 24 39 { 25 40 unsigned long flags; 41 + 42 + if (time_travel_mode != TT_MODE_OFF) 43 + time_travel_set_time(time_travel_timer_expiry); 26 44 27 45 local_irq_save(flags); 28 46 do_IRQ(TIMER_IRQ, regs); ··· 49 31 50 32 static int itimer_shutdown(struct clock_event_device *evt) 51 33 { 52 - os_timer_disable(); 34 + if (time_travel_mode != TT_MODE_OFF) 35 + time_travel_set_timer(TT_TMR_DISABLED, 0); 36 + 37 + if (time_travel_mode != TT_MODE_INFCPU) 38 + os_timer_disable(); 39 + 53 40 return 0; 54 41 } 55 42 56 43 static int itimer_set_periodic(struct clock_event_device *evt) 57 44 { 58 - os_timer_set_interval(NSEC_PER_SEC / HZ); 45 + unsigned long long interval = NSEC_PER_SEC / HZ; 46 + 47 + if (time_travel_mode != TT_MODE_OFF) 48 + time_travel_set_timer(TT_TMR_PERIODIC, 49 + time_travel_time + interval); 50 + 51 + if (time_travel_mode != TT_MODE_INFCPU) 52 + os_timer_set_interval(interval); 53 + 59 54 return 0; 60 55 } 61 56 62 57 static int itimer_next_event(unsigned long delta, 63 58 struct clock_event_device *evt) 64 59 { 65 - return os_timer_one_shot(delta + 1); 60 + delta += 1; 61 + 62 + if (time_travel_mode != TT_MODE_OFF) 63 + time_travel_set_timer(TT_TMR_ONESHOT, 64 + time_travel_time + delta); 65 + 66 + if (time_travel_mode != TT_MODE_INFCPU) 67 + return os_timer_one_shot(delta); 68 + 69 + return 0; 66 70 } 67 71 68 72 static int itimer_one_shot(struct clock_event_device *evt) 69 73 { 70 - os_timer_one_shot(1); 71 - return 0; 74 + return itimer_next_event(0, evt); 72 75 } 73 76 74 77 static struct clock_event_device timer_clockevent = { ··· 126 87 127 88 static u64 timer_read(struct clocksource *cs) 128 89 { 90 + if (time_travel_mode != TT_MODE_OFF) { 91 + /* 92 + * We make reading the timer cost a bit so that we don't get 93 + * stuck in loops that expect time to move more than the 94 + * exact requested sleep amount, e.g. python's socket server, 95 + * see https://bugs.python.org/issue37026. 96 + */ 97 + time_travel_set_time(time_travel_time + TIMER_MULTIPLIER); 98 + return time_travel_time / TIMER_MULTIPLIER; 99 + } 100 + 129 101 return os_nsecs() / TIMER_MULTIPLIER; 130 102 } 131 103 ··· 173 123 174 124 void read_persistent_clock64(struct timespec64 *ts) 175 125 { 176 - long long nsecs = os_persistent_clock_emulation(); 126 + long long nsecs; 127 + 128 + if (time_travel_start_set) 129 + nsecs = time_travel_start + time_travel_time; 130 + else 131 + nsecs = os_persistent_clock_emulation(); 177 132 178 133 set_normalized_timespec64(ts, nsecs / NSEC_PER_SEC, 179 134 nsecs % NSEC_PER_SEC); ··· 189 134 timer_set_signal_handler(); 190 135 late_time_init = um_timer_setup; 191 136 } 137 + 138 + #ifdef CONFIG_UML_TIME_TRAVEL_SUPPORT 139 + unsigned long calibrate_delay_is_known(void) 140 + { 141 + if (time_travel_mode == TT_MODE_INFCPU) 142 + return 1; 143 + return 0; 144 + } 145 + 146 + int setup_time_travel(char *str) 147 + { 148 + if (strcmp(str, "=inf-cpu") == 0) { 149 + time_travel_mode = TT_MODE_INFCPU; 150 + timer_clockevent.name = "time-travel-timer-infcpu"; 151 + timer_clocksource.name = "time-travel-clock"; 152 + return 1; 153 + } 154 + 155 + if (!*str) { 156 + time_travel_mode = TT_MODE_BASIC; 157 + timer_clockevent.name = "time-travel-timer"; 158 + timer_clocksource.name = "time-travel-clock"; 159 + return 1; 160 + } 161 + 162 + return -EINVAL; 163 + } 164 + 165 + __setup("time-travel", setup_time_travel); 166 + __uml_help(setup_time_travel, 167 + "time-travel\n" 168 + "This option just enables basic time travel mode, in which the clock/timers\n" 169 + "inside the UML instance skip forward when there's nothing to do, rather than\n" 170 + "waiting for real time to elapse. However, instance CPU speed is limited by\n" 171 + "the real CPU speed, so e.g. a 10ms timer will always fire after ~10ms wall\n" 172 + "clock (but quicker when there's nothing to do).\n" 173 + "\n" 174 + "time-travel=inf-cpu\n" 175 + "This enables time travel mode with infinite processing power, in which there\n" 176 + "are no wall clock timers, and any CPU processing happens - as seen from the\n" 177 + "guest - instantly. This can be useful for accurate simulation regardless of\n" 178 + "debug overhead, physical CPU speed, etc. but is somewhat dangerous as it can\n" 179 + "easily lead to getting stuck (e.g. if anything in the system busy loops).\n"); 180 + 181 + int setup_time_travel_start(char *str) 182 + { 183 + int err; 184 + 185 + err = kstrtoull(str, 0, &time_travel_start); 186 + if (err) 187 + return err; 188 + 189 + time_travel_start_set = 1; 190 + return 1; 191 + } 192 + 193 + __setup("time-travel-start", setup_time_travel_start); 194 + __uml_help(setup_time_travel_start, 195 + "time-travel-start=<seconds>\n" 196 + "Configure the UML instance's wall clock to start at this value rather than\n" 197 + "the host's wall clock at the time of UML boot.\n"); 198 + #endif