um: time-travel: fix time going backwards
PATCH 1/1 um: time-travel: fix time going backwards
In basic time travel mode, I sometimes see "time goes backwards" panics like the one below:
Kernel panic: time-travel: time goes backwards 161689340000492 -> 161689339869814 Call Trace: panic+0x1a1/0x3d7 time_travel_update_time.cold+0xe9/0x133 timer_read+0xc1/0x100 ktime_get+0x10c/0x200 copy_process+0x1899/0x2230 kernel_clone+0x57/0x7a0 kernel_thread+0x4a/0x50 kthreadd+0x116/0x190
The problem is a race between time_travel_handle_real_alarm() and timer_read(). time_travel_handle_real_alarm() changes the time after time_read() reads the current time but before time_travel_update_time() has had a chance to add the end event.
Fix this by doing the time read and event add atomically with respect to time_travel_handle_real_alarm().
--- arch/um/kernel/time.c | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-)
arch/um/kernel/time.c +34 -14
@@ -392,17 +392,11 @@ bool time_travel_del_event(struct time_travel_event *e)return true; } -static void time_travel_update_time (unsigned long long next,bool idle) +static void time_travel_update_to_event (struct time_travel_event *ne,bool idle) { -struct time_travel_event ne = { - .onstack = true, - };struct time_travel_event *e;bool finished = idle; -/* add it without a handler - we deal with that specifically below */ -__time_travel_add_event (&ne, next); - do { e = time_travel_first_event();
@@ -414,7 +408,7 @@ static void time_travel_update_time(unsigned long long next, bool idle)BUG_ON (!time_travel_del_event (e));BUG_ON (time_travel_time != e->time); -if (e == &ne) { +if (e == ne) { finished = true; }else {if (e->onstack)
@@ -427,14 +421,38 @@ static void time_travel_update_time(unsigned long long next, bool idle) e =time_travel_first_event ();if (e)time_travel_ext_update_request (e->time); - }while (ne.pending && !finished); + }while (ne->pending && !finished); + +time_travel_del_event (ne); +} -time_travel_del_event (&ne); +static void time_travel_update_time (unsigned long long next,bool idle) +{ +struct time_travel_event ne = { + .onstack = true, + }; + +__time_travel_add_event (&ne, next); +time_travel_update_to_event (&ne, idle); +} + +static void time_travel_forward_time (unsigned long nsec,bool idle) +{ +struct time_travel_event ne = { + .onstack = true, + }; +unsigned long flags; + +local_irq_save (flags); +__time_travel_add_event (&ne, time_travel_time + nsec); +local_irq_restore (flags); + +time_travel_update_to_event (&ne, idle); }void time_travel_ndelay (unsigned long nsec) { -time_travel_update_time (time_travel_time + nsec, false); +time_travel_forward_time (nsec, false); }EXPORT_SYMBOL (time_travel_ndelay);
@@ -572,6 +590,10 @@ static inline void time_travel_update_time(unsigned long long ns, bool retearly) { } +static void time_travel_forward_time (unsigned long nsec,bool idle) +{ +} +static inline void time_travel_handle_real_alarm (void ) { }
@@ -720,9 +742,7 @@ static u64 timer_read(struct clocksource *cs)*/ if (!irqs_disabled () && !in_interrupt () && !in_softirq () && !time_travel_ext_waiting) -time_travel_update_time (time_travel_time + -TIMER_MULTIPLIER , - false); +time_travel_forward_time (TIMER_MULTIPLIER , false);return time_travel_time /TIMER_MULTIPLIER ; }
---
base-commit: 58720809f52779dc0f08e53e54b014209d13eebb
change-id: 20231020-uml-time-backwards-552c81aedd23
Best regards,
Signature · 1 line
Vincent Whitchurch <[email protected]>
Quoted 14 lines · Vincent Whitchurch
In basic time travel mode, I sometimes see "time goes backwards" panics like the one below:
Kernel panic: time-travel: time goes backwards 161689340000492 -> 161689339869814 Call Trace: panic+0x1a1/0x3d7 time_travel_update_time.cold+0xe9/0x133 timer_read+0xc1/0x100 ktime_get+0x10c/0x200 copy_process+0x1899/0x2230 kernel_clone+0x57/0x7a0 kernel_thread+0x4a/0x50 kthreadd+0x116/0x190
Quoted 7 lines
The problem is a race between
time_travel_handle_real_alarm()andtimer_read().time_travel_handle_real_alarm()changes the time aftertime_read()reads the current time but beforetime_travel_update_time()has had a chance to add the end event.Fix this by doing the time read and event add atomically with respect to
time_travel_handle_real_alarm().
Further testing resulted in hitting the BUG_ON(time_travel_time != e->time) so looks like this needs some more work.
BUG: failure at arch/um/kernel/time.c:409/time_travel_update_to_event()! Kernel panic - not syncing: BUG! CPU: 0 PID: 1 Comm: swapper Not tainted 6.5.0+ #13 Stack: 624db908 60a06c60 41b58ab3 60ae2398 6003fb90 10000c1e577c 41b58ab3 60b419ae 607f0dc0 00000000 00000000 6005b270 Call Trace: [<607fad5d>] show_stack.cold+0xc0/0x149 [<60841364>] dump_stack_lvl+0x75/0x94 [<6084139d>] dump_stack+0x1a/0x1c [<607fca1d>] panic+0x27e/0x490 [<607fb093>] time_travel_update_to_event.cold+0x89/0x18b [<60041317>] time_travel_forward_time.constprop.0+0xf7/0x160 [<600414f1>] timer_read+0x171/0x1b0 [<60140652>] random_get_entropy_fallback+0x42/0x60 [<60545943>] add_device_randomness+0x73/0xf0 [<6003a685>] do_one_initcall+0x145/0x3b0 [<60002598>] 0x60002598 [<60843ac5>] kernel_init+0x4a/0x195 [<6003d111>] new_thread_handler+0x141/0x1a0
Johannes Berg
maintainerQuoted 5 lines · Vincent Whitchurch
Quoted 4 lines · Vincent Whitchurch
In basic time travel mode, I sometimes see "time goes backwards" panics like the one below:
Kernel panic: time-travel: time goes backwards 161689340000492 -> 161689339869814
Ouch.
Quoted 15 lines
Quoted 15 lines
Call Trace: panic+0x1a1/0x3d7 time_travel_update_time.cold+0xe9/0x133 timer_read+0xc1/0x100 ktime_get+0x10c/0x200 copy_process+0x1899/0x2230 kernel_clone+0x57/0x7a0 kernel_thread+0x4a/0x50 kthreadd+0x116/0x190The problem is a race between
time_travel_handle_real_alarm()andtimer_read().time_travel_handle_real_alarm()changes the time aftertime_read()reads the current time but beforetime_travel_update_time()has had a chance to add the end event.
Quoted 6 lines
Quoted 2 lines
Fix this by doing the time read and event add atomically with respect to
time_travel_handle_real_alarm().Further testing resulted in hitting the
BUG_ON(time_travel_time != e->time)so looks like this needs some more work.
Yeah this is a tricky area, I fought with it for quite a while too, seems we're not done yet ;-)
We mostly use time-travel=ext mode these days, so our system may not be as susceptible to it? But not sure, in some cases it runs with just a single instance, and that should be pretty much the same due to the free-until information.
Do you have a specific workload that tends to reproduce this?
johannes
Quoted 1 line · Johannes Berg
Do you have a specific workload that tends to reproduce this?
I've been seeing it when running roadtest, but it's easily reproducible without that by using the attached config and the following program as init.
cp repro.config .config
make ARCH=um olddefconfig all
gcc -Wall -static -o repro repro.c
./linux time-travel init=$PWD/repro rootfstype=hostfs
With the above commands I usually see the panic in a few seconds. This is on an unmodified v6.6-rc7.
----8<-------
#include <time.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(void)
{
if (fork() == 0) {
while (1) {
nanosleep(&(struct timespec){.tv_nsec = 330 * 1000}, NULL);
}
return 0;
}
while (1) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
nanosleep(&(struct timespec){.tv_nsec = 22 * 1000}, NULL);
}
return 0;
} # CONFIG_LOCALVERSION_AUTO is not setCONFIG_NO_HZ=yCONFIG_HIGH_RES_TIMERS=yCONFIG_LOG_BUF_SHIFT=18CONFIG_EXPERT=yCONFIG_HOSTFS=yCONFIG_MAGIC_SYSRQ=yCONFIG_UML_TIME_TRAVEL_SUPPORT=yCONFIG_SSL=yCONFIG_NULL_CHAN=yCONFIG_PORT_CHAN=yCONFIG_PTY_CHAN=yCONFIG_TTY_CHAN=yCONFIG_XTERM_CHAN=yCONFIG_CON_CHAN="pts"CONFIG_SSL_CHAN="pts"CONFIG_VIRTIO_UML=yCONFIG_UML_PCI_OVER_VIRTIO=yCONFIG_UML_PCI_OVER_VIRTIO_DEVICE_ID=1234CONFIG_GCOV_KERNEL=yCONFIG_MODULES=yCONFIG_MODULE_UNLOAD=yCONFIG_BINFMT_MISC=m
# CONFIG_COMPACTION is not setCONFIG_NET=yCONFIG_UNIX=yCONFIG_DEVTMPFS=yCONFIG_DEVTMPFS_MOUNT=yCONFIG_OF=y
# CONFIG_INPUT is not setCONFIG_LEGACY_PTY_COUNT=32CONFIG_SERIAL_SC16IS7XX=mCONFIG_GOLDFISH_TTY=mCONFIG_SERIAL_DEV_BUS=yCONFIG_UML_RANDOM=y
# CONFIG_I2C_COMPAT is not setCONFIG_I2C_CHARDEV=yCONFIG_I2C_VIRTIO=yCONFIG_I2C_STUB=mCONFIG_SPI=yCONFIG_SPI_SC18IS602=yCONFIG_SPI_MUX=yCONFIG_PINCTRL=yCONFIG_GPIOLIB=yCONFIG_GPIO_SYSFS=yCONFIG_GPIO_GENERIC_PLATFORM=mCONFIG_GPIO_MOCKUP=yCONFIG_GPIO_VIRTIO=yCONFIG_POWER_SUPPLY=yCONFIG_CHARGER_BQ256XX=mCONFIG_PMBUS=yCONFIG_SENSORS_LTC2978=mCONFIG_SENSORS_LTC2978_REGULATOR=yCONFIG_SENSORS_PWM_FAN=mCONFIG_SENSORS_TMP401=mCONFIG_REGULATOR=yCONFIG_REGULATOR_DEBUG=yCONFIG_REGULATOR_FIXED_VOLTAGE=yCONFIG_REGULATOR_VIRTUAL_CONSUMER=mCONFIG_REGULATOR_GPIO=mCONFIG_REGULATOR_TPS6286X=mCONFIG_REGULATOR_TPS6287X=mCONFIG_DRM=yCONFIG_TINYDRM_ILI9341=mCONFIG_NEW_LEDS=yCONFIG_LEDS_CLASS=yCONFIG_LEDS_GPIO=yCONFIG_LEDS_TRIGGERS=yCONFIG_LEDS_TRIGGER_HEARTBEAT=yCONFIG_RTC_CLASS=y
# CONFIG_RTC_HCTOSYS is not set
# CONFIG_RTC_SYSTOHC is not setCONFIG_RTC_DEBUG=yCONFIG_RTC_DRV_PCF8523=mCONFIG_RTC_DRV_PCF8563=mCONFIG_GOLDFISH=yCONFIG_COMMON_CLK=yCONFIG_COMMON_CLK_SI5351=mCONFIG_IIO=yCONFIG_IIO_BUFFER_CB=mCONFIG_IIO_SW_TRIGGER=yCONFIG_MCP320X=mCONFIG_TI_ADC081C=mCONFIG_TI_ADC084S021=mCONFIG_TI_ADS7924=mCONFIG_PMS7003=mCONFIG_OPT3001=mCONFIG_OPT4001=mCONFIG_VCNL4000=mCONFIG_IIO_HRTIMER_TRIGGER=yCONFIG_IIO_SYSFS_TRIGGER=yCONFIG_IRSD200=mCONFIG_PWM=yCONFIG_PWM_PCA9685=mCONFIG_MUX_GPIO=yCONFIG_QUOTA=yCONFIG_PROC_KCORE=yCONFIG_TMPFS=yCONFIG_NLS=yCONFIG_CRYPTO=yCONFIG_CRYPTO_CRC32C=yCONFIG_CRYPTO_JITTERENTROPY=yCONFIG_CRC16=yCONFIG_PRINTK_TIME=yCONFIG_PRINTK_CALLER=yCONFIG_DYNAMIC_DEBUG=yCONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=yCONFIG_GDB_SCRIPTS=yCONFIG_FRAME_WARN=1024CONFIG_READABLE_ASM=yCONFIG_DEBUG_FS=yCONFIG_UBSAN=yCONFIG_PAGE_EXTENSION=yCONFIG_DEBUG_OBJECTS=yCONFIG_DEBUG_OBJECTS_FREE=yCONFIG_DEBUG_OBJECTS_TIMERS=yCONFIG_DEBUG_OBJECTS_WORK=yCONFIG_KASAN=yCONFIG_PROVE_LOCKING=yCONFIG_ENABLE_DEFAULT_TRACERS=yCONFIG_FAULT_INJECTION=yCONFIG_FAILSLAB=yCONFIG_FAULT_INJECTION_DEBUG_FS=yCONFIG_FAULT_INJECTION_STACKTRACE_FILTER=y
Johannes Berg
maintainerQuoted 7 lines · Vincent Whitchurch
Quoted 1 line · Johannes Berg
Do you have a specific workload that tends to reproduce this?
I've been seeing it when running roadtest, but it's easily reproducible without that by using the attached config and the following program as init.
Quoted 8 lines
cp repro.config .config
make ARCH=um olddefconfig all
gcc -Wall -static -o reprorepro.c
./linux time-travel init=$PWD/repro rootfstype=hostfsWith the above commands I usually see the panic in a few seconds. This is on an unmodified v6.6-rc7.
Yes, I can reproduce it with your test, thanks. I'm on 6.4-rc still for $reasons (6.5 we skipped during vacations, and 6.6 EEVDF scheduler broke everything for hwsim tests in hostap), but this code didn't really change anyway.
I'll poke at this.
(And I'm still amazed that anyone other than me even uses this stuff :P)
johannes
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
Johannes Berg
maintainerQuoted 4 lines · Johannes Berg
Quoted 3 lines · Vincent Whitchurch
Quoted 1 line · Johannes Berg
Do you have a specific workload that tends to reproduce this?
Quoted 8 lines
Quoted 8 lines
I've been seeing it when running roadtest, but it's easily reproducible without that by using the attached config and the following program as init.
cp repro.config .config
make ARCH=um olddefconfig all
gcc -Wall -static -o reprorepro.c
./linux time-travel init=$PWD/repro rootfstype=hostfs
Ohhh.
Pure "time-travel" is actually something I hardly think about these days, we mostly use time-travel=inf-cpu (or =ext).
That makes sense, here you actually *can* get interrupted. I'll need to dig into what happens though.
johannes
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
Johannes Berg
maintainerQuoted 14 lines · Johannes Berg
Quoted 4 lines · Johannes Berg
Quoted 3 lines · Vincent Whitchurch
Quoted 1 line · Johannes Berg
Do you have a specific workload that tends to reproduce this?
Quoted 8 lines
Quoted 8 lines
I've been seeing it when running roadtest, but it's easily reproducible without that by using the attached config and the following program as init.
cp repro.config .config
make ARCH=um olddefconfig all
gcc -Wall -static -o reprorepro.c
./linux time-travel init=$PWD/repro rootfstype=hostfs
Quoted 7 lines
Ohhh.
Pure "time-travel" is actually something I hardly think about these days, we mostly use time-travel=inf-cpu (or =ext).
That makes sense, here you actually *can* get interrupted. I'll need to dig into what happens though.
I just sent a patch, please take a look. It does seem to fix it - at least I got bored of waiting after running your test program for a few minutes :)
johannes
_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um