Skip to content

Commit 2eb5f31

Browse files
Anton Ivanovrichardweinberger
authored andcommitted
um: Switch clocksource to hrtimers
UML is using an obsolete itimer call for all timers and "polls" for kernel space timer firing in its userspace portion resulting in a long list of bugs and incorrect behaviour(s). It also uses ITIMER_VIRTUAL for its timer which results in the timer being dependent on it running and the cpu load. This patch fixes this by moving to posix high resolution timers firing off CLOCK_MONOTONIC and relaying the timer correctly to the UML userspace. Fixes: - crashes when hosts suspends/resumes - broken userspace timers - effecive ~40Hz instead of what they should be. Note - this modifies skas behavior by no longer setting an itimer per clone(). Timer events are relayed instead. - kernel network packet scheduling disciplines - tcp behaviour especially under load - various timer related corner cases Finally, overall responsiveness of userspace is better. Signed-off-by: Thomas Meyer <thomas@m3y3r.de> Signed-off-by: Anton Ivanov <aivanov@brocade.com> [rw: massaged commit message] Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent e17c6d7 commit 2eb5f31

14 files changed

Lines changed: 255 additions & 224 deletions

File tree

‎arch/um/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export LDS_ELF_FORMAT := $(ELF_FORMAT)
131131
# The wrappers will select whether using "malloc" or the kernel allocator.
132132
LINK_WRAPS = -Wl,--wrap,malloc -Wl,--wrap,free -Wl,--wrap,calloc
133133

134-
LD_FLAGS_CMDLINE = $(foreach opt,$(LDFLAGS),-Wl,$(opt))
134+
LD_FLAGS_CMDLINE = $(foreach opt,$(LDFLAGS),-Wl,$(opt)) -lrt
135135

136136
# Used by link-vmlinux.sh which has special support for um link
137137
export CFLAGS_vmlinux := $(LINK-y) $(LINK_WRAPS) $(LD_FLAGS_CMDLINE)

‎arch/um/include/shared/os.h‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/*
2+
* Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
3+
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
24
* Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
35
* Licensed under the GPL
46
*/
@@ -183,6 +185,7 @@ extern int create_mem_file(unsigned long long len);
183185
/* process.c */
184186
extern unsigned long os_process_pc(int pid);
185187
extern int os_process_parent(int pid);
188+
extern void os_alarm_process(int pid);
186189
extern void os_stop_process(int pid);
187190
extern void os_kill_process(int pid, int reap_child);
188191
extern void os_kill_ptraced_process(int pid, int reap_child);
@@ -217,7 +220,7 @@ extern int set_umid(char *name);
217220
extern char *get_umid(void);
218221

219222
/* signal.c */
220-
extern void timer_init(void);
223+
extern void timer_set_signal_handler(void);
221224
extern void set_sigstack(void *sig_stack, int size);
222225
extern void remove_sigstack(void);
223226
extern void set_handler(int sig);
@@ -227,6 +230,7 @@ extern void unblock_signals(void);
227230
extern int get_signals(void);
228231
extern int set_signals(int enable);
229232
extern int os_is_signal_stack(void);
233+
extern void deliver_alarm(void);
230234

231235
/* util.c */
232236
extern void stack_protections(unsigned long address);
@@ -238,12 +242,16 @@ extern void um_early_printk(const char *s, unsigned int n);
238242
extern void os_fix_helper_signals(void);
239243

240244
/* time.c */
241-
extern void idle_sleep(unsigned long long nsecs);
242-
extern int set_interval(void);
243-
extern int timer_one_shot(int ticks);
244-
extern long long disable_timer(void);
245+
extern void os_idle_sleep(unsigned long long nsecs);
246+
extern int os_timer_create(void* timer);
247+
extern int os_timer_set_interval(void* timer, void* its);
248+
extern int os_timer_one_shot(int ticks);
249+
extern long long os_timer_disable(void);
250+
extern long os_timer_remain(void* timer);
245251
extern void uml_idle_timer(void);
252+
extern long long os_persistent_clock_emulation(void);
246253
extern long long os_nsecs(void);
254+
extern long long os_vnsecs(void);
247255

248256
/* skas/mem.c */
249257
extern long run_syscall_stub(struct mm_id * mm_idp,

‎arch/um/include/shared/skas/stub-data.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/*
2+
3+
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
24
* Copyright (C) 2005 Jeff Dike (jdike@karaya.com)
35
* Licensed under the GPL
46
*/
57

68
#ifndef __STUB_DATA_H
79
#define __STUB_DATA_H
810

9-
#include <sys/time.h>
11+
#include <time.h>
1012

1113
struct stub_data {
12-
long offset;
14+
unsigned long offset;
1315
int fd;
14-
struct itimerval timer;
1516
long err;
1617
};
1718

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (C) 2012 - 2014 Cisco Systems
3+
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4+
* Licensed under the GPL
5+
*/
6+
7+
#ifndef __TIMER_INTERNAL_H__
8+
#define __TIMER_INTERNAL_H__
9+
10+
#define TIMER_MULTIPLIER 256
11+
#define TIMER_MIN_DELTA 500
12+
13+
#endif

‎arch/um/kernel/process.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/*
2+
* Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
3+
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
24
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
35
* Copyright 2003 PathScale, Inc.
46
* Licensed under the GPL
@@ -27,6 +29,7 @@
2729
#include <kern_util.h>
2830
#include <os.h>
2931
#include <skas.h>
32+
#include <timer-internal.h>
3033

3134
/*
3235
* This is a per-cpu array. A processor only modifies its entry and it only
@@ -203,11 +206,8 @@ void initial_thread_cb(void (*proc)(void *), void *arg)
203206

204207
void arch_cpu_idle(void)
205208
{
206-
unsigned long long nsecs;
207-
208209
cpu_tasks[current_thread_info()->cpu].pid = os_getpid();
209-
nsecs = disable_timer();
210-
idle_sleep(nsecs);
210+
os_idle_sleep(UM_NSEC_PER_SEC);
211211
local_irq_enable();
212212
}
213213

‎arch/um/kernel/skas/clone.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
23
* Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
34
* Licensed under the GPL
45
*/
@@ -35,11 +36,6 @@ stub_clone_handler(void)
3536
if (err)
3637
goto out;
3738

38-
err = stub_syscall3(__NR_setitimer, ITIMER_VIRTUAL,
39-
(long) &data->timer, 0);
40-
if (err)
41-
goto out;
42-
4339
remap_stack(data->fd, data->offset);
4440
goto done;
4541

‎arch/um/kernel/skas/mmu.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
23
* Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
34
* Licensed under the GPL
45
*/
@@ -61,10 +62,12 @@ int init_new_context(struct task_struct *task, struct mm_struct *mm)
6162
if (current->mm != NULL && current->mm != &init_mm)
6263
from_mm = &current->mm->context;
6364

65+
block_signals();
6466
if (from_mm)
6567
to_mm->id.u.pid = copy_context_skas0(stack,
6668
from_mm->id.u.pid);
6769
else to_mm->id.u.pid = start_userspace(stack);
70+
unblock_signals();
6871

6972
if (to_mm->id.u.pid < 0) {
7073
ret = to_mm->id.u.pid;

‎arch/um/kernel/time.c‎

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/*
2+
* Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
3+
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4+
* Copyright (C) 2012-2014 Cisco Systems
25
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
36
* Licensed under the GPL
47
*/
@@ -7,11 +10,15 @@
710
#include <linux/init.h>
811
#include <linux/interrupt.h>
912
#include <linux/jiffies.h>
13+
#include <linux/mm.h>
14+
#include <linux/sched.h>
15+
#include <linux/spinlock.h>
1016
#include <linux/threads.h>
1117
#include <asm/irq.h>
1218
#include <asm/param.h>
1319
#include <kern_util.h>
1420
#include <os.h>
21+
#include <timer-internal.h>
1522

1623
void timer_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
1724
{
@@ -24,88 +31,104 @@ void timer_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
2431

2532
static int itimer_shutdown(struct clock_event_device *evt)
2633
{
27-
disable_timer();
34+
os_timer_disable();
2835
return 0;
2936
}
3037

3138
static int itimer_set_periodic(struct clock_event_device *evt)
3239
{
33-
set_interval();
40+
os_timer_set_interval(NULL, NULL);
3441
return 0;
3542
}
3643

3744
static int itimer_next_event(unsigned long delta,
3845
struct clock_event_device *evt)
3946
{
40-
return timer_one_shot(delta + 1);
47+
return os_timer_one_shot(delta);
4148
}
4249

43-
static struct clock_event_device itimer_clockevent = {
44-
.name = "itimer",
50+
static int itimer_one_shot(struct clock_event_device *evt)
51+
{
52+
os_timer_one_shot(1);
53+
return 0;
54+
}
55+
56+
static struct clock_event_device timer_clockevent = {
57+
.name = "posix-timer",
4558
.rating = 250,
4659
.cpumask = cpu_all_mask,
4760
.features = CLOCK_EVT_FEAT_PERIODIC |
4861
CLOCK_EVT_FEAT_ONESHOT,
4962
.set_state_shutdown = itimer_shutdown,
5063
.set_state_periodic = itimer_set_periodic,
51-
.set_state_oneshot = itimer_shutdown,
64+
.set_state_oneshot = itimer_one_shot,
5265
.set_next_event = itimer_next_event,
53-
.shift = 32,
66+
.shift = 0,
67+
.max_delta_ns = 0xffffffff,
68+
.min_delta_ns = TIMER_MIN_DELTA, //microsecond resolution should be enough for anyone, same as 640K RAM
5469
.irq = 0,
70+
.mult = 1,
5571
};
5672

5773
static irqreturn_t um_timer(int irq, void *dev)
5874
{
59-
(*itimer_clockevent.event_handler)(&itimer_clockevent);
75+
if (get_current()->mm != NULL)
76+
{
77+
/* userspace - relay signal, results in correct userspace timers */
78+
os_alarm_process(get_current()->mm->context.id.u.pid);
79+
}
80+
81+
(*timer_clockevent.event_handler)(&timer_clockevent);
6082

6183
return IRQ_HANDLED;
6284
}
6385

64-
static cycle_t itimer_read(struct clocksource *cs)
86+
static cycle_t timer_read(struct clocksource *cs)
6587
{
66-
return os_nsecs() / 1000;
88+
return os_nsecs() / TIMER_MULTIPLIER;
6789
}
6890

69-
static struct clocksource itimer_clocksource = {
70-
.name = "itimer",
91+
static struct clocksource timer_clocksource = {
92+
.name = "timer",
7193
.rating = 300,
72-
.read = itimer_read,
94+
.read = timer_read,
7395
.mask = CLOCKSOURCE_MASK(64),
7496
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
7597
};
7698

77-
static void __init setup_itimer(void)
99+
static void __init timer_setup(void)
78100
{
79101
int err;
80102

81-
err = request_irq(TIMER_IRQ, um_timer, 0, "timer", NULL);
103+
err = request_irq(TIMER_IRQ, um_timer, IRQF_TIMER, "hr timer", NULL);
82104
if (err != 0)
83105
printk(KERN_ERR "register_timer : request_irq failed - "
84106
"errno = %d\n", -err);
85107

86-
itimer_clockevent.mult = div_sc(HZ, NSEC_PER_SEC, 32);
87-
itimer_clockevent.max_delta_ns =
88-
clockevent_delta2ns(60 * HZ, &itimer_clockevent);
89-
itimer_clockevent.min_delta_ns =
90-
clockevent_delta2ns(1, &itimer_clockevent);
91-
err = clocksource_register_hz(&itimer_clocksource, USEC_PER_SEC);
108+
err = os_timer_create(NULL);
109+
if (err != 0) {
110+
printk(KERN_ERR "creation of timer failed - errno = %d\n", -err);
111+
return;
112+
}
113+
114+
err = clocksource_register_hz(&timer_clocksource, NSEC_PER_SEC/TIMER_MULTIPLIER);
92115
if (err) {
93116
printk(KERN_ERR "clocksource_register_hz returned %d\n", err);
94117
return;
95118
}
96-
clockevents_register_device(&itimer_clockevent);
119+
clockevents_register_device(&timer_clockevent);
97120
}
98121

99122
void read_persistent_clock(struct timespec *ts)
100123
{
101-
long long nsecs = os_nsecs();
124+
long long nsecs = os_persistent_clock_emulation();
102125

103126
set_normalized_timespec(ts, nsecs / NSEC_PER_SEC,
104127
nsecs % NSEC_PER_SEC);
105128
}
106129

107130
void __init time_init(void)
108131
{
109-
timer_init();
110-
late_time_init = setup_itimer;
132+
timer_set_signal_handler();
133+
late_time_init = timer_setup;
111134
}

‎arch/um/os-Linux/internal.h‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎arch/um/os-Linux/main.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
23
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
34
* Licensed under the GPL
45
*/
@@ -163,13 +164,13 @@ int __init main(int argc, char **argv, char **envp)
163164

164165
/*
165166
* This signal stuff used to be in the reboot case. However,
166-
* sometimes a SIGVTALRM can come in when we're halting (reproducably
167+
* sometimes a timer signal can come in when we're halting (reproducably
167168
* when writing out gcov information, presumably because that takes
168169
* some time) and cause a segfault.
169170
*/
170171

171-
/* stop timers and set SIGVTALRM to be ignored */
172-
disable_timer();
172+
/* stop timers and set timer signal to be ignored */
173+
os_timer_disable();
173174

174175
/* disable SIGIO for the fds and set SIGIO to be ignored */
175176
err = deactivate_all_fds();

0 commit comments

Comments
 (0)