Skip to content

Commit 1d7173b

Browse files
cfd-36Linus Torvalds
authored andcommitted
[PATCH] uml: implement soft interrupts
This patch implements soft interrupts. Interrupt enabling and disabling no longer map to sigprocmask. Rather, a flag is set indicating whether interrupts may be handled. If a signal comes in and interrupts are marked as OK, then it is handled normally. If interrupts are marked as off, then the signal handler simply returns after noting that a signal needs handling. When interrupts are enabled later on, this pending signals flag is checked, and the IRQ handlers are called at that point. The point of this is to reduce the cost of local_irq_save et al, since they are very much more common than the signals that they are enabling and disabling. Soft interrupts produce a speed-up of ~25% on a kernel build. Subtleties - UML uses sigsetjmp/siglongjmp to switch contexts. sigsetjmp has been wrapped in a save_flags-like macro which remembers the interrupt state at setjmp time, and restores it when it is longjmp-ed back to. The enable_signals function has to loop because the IRQ handler disables interrupts before returning. enable_signals has to return with signals enabled, and signals may come in between the disabling and the return to enable_signals. So, it loops for as long as there are pending signals, ensuring that signals are enabled when it finally returns, and that there are no pending signals that need to be dealt with. Signed-off-by: Jeff Dike <jdike@addtoit.com> Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 09ee011 commit 1d7173b

8 files changed

Lines changed: 183 additions & 118 deletions

File tree

‎arch/um/include/longjmp.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __UML_LONGJMP_H
2+
#define __UML_LONGJMP_H
3+
4+
#include <setjmp.h>
5+
#include "os.h"
6+
7+
#define UML_SIGLONGJMP(buf, val) do { \
8+
siglongjmp(*buf, val); \
9+
} while(0)
10+
11+
#define UML_SIGSETJMP(buf, enable) ({ \
12+
int n; \
13+
enable = get_signals(); \
14+
n = sigsetjmp(*buf, 1); \
15+
if(n != 0) \
16+
set_signals(enable); \
17+
n; })
18+
19+
#endif

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,8 @@ extern void scan_elf_aux( char **envp);
8181
int main(int argc, char **argv, char **envp)
8282
{
8383
char **new_argv;
84-
sigset_t mask;
8584
int ret, i, err;
8685

87-
/* Enable all signals except SIGIO - in some environments, we can
88-
* enter with some signals blocked
89-
*/
90-
91-
sigemptyset(&mask);
92-
sigaddset(&mask, SIGIO);
93-
if(sigprocmask(SIG_SETMASK, &mask, NULL) < 0){
94-
perror("sigprocmask");
95-
exit(1);
96-
}
97-
9886
#ifdef UML_CONFIG_CMDLINE_ON_HOST
9987
/* Allocate memory for thread command lines */
10088
if(argc < 2 || strlen(argv[1]) < THREAD_NAME_LEN - 1){

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "process.h"
1919
#include "irq_user.h"
2020
#include "kern_util.h"
21+
#include "longjmp.h"
2122

2223
#define ARBITRARY_ADDR -1
2324
#define FAILURE_PID -1
@@ -205,24 +206,13 @@ void init_new_thread_signals(int altstack)
205206

206207
int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
207208
{
208-
sigjmp_buf buf;
209-
int n;
210-
211-
*jmp_ptr = &buf;
212-
n = sigsetjmp(buf, 1);
213-
if(n != 0)
214-
return(n);
215-
(*fn)(arg);
216-
return(0);
209+
sigjmp_buf buf;
210+
int n, enable;
211+
212+
*jmp_ptr = &buf;
213+
n = UML_SIGSETJMP(&buf, enable);
214+
if(n != 0)
215+
return(n);
216+
(*fn)(arg);
217+
return(0);
217218
}
218-
219-
/*
220-
* Overrides for Emacs so that we follow Linus's tabbing style.
221-
* Emacs will notice this stuff at the end of the file and automatically
222-
* adjust the settings for this buffer only. This must remain at the end
223-
* of the file.
224-
* ---------------------------------------------------------------------------
225-
* Local variables:
226-
* c-file-style: "linux"
227-
* End:
228-
*/

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

Lines changed: 131 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,58 @@
2020
#include "mode.h"
2121
#include "os.h"
2222

23+
/* These are the asynchronous signals. SIGVTALRM and SIGARLM are handled
24+
* together under SIGVTALRM_BIT. SIGPROF is excluded because we want to
25+
* be able to profile all of UML, not just the non-critical sections. If
26+
* profiling is not thread-safe, then that is not my problem. We can disable
27+
* profiling when SMP is enabled in that case.
28+
*/
29+
#define SIGIO_BIT 0
30+
#define SIGIO_MASK (1 << SIGIO_BIT)
31+
32+
#define SIGVTALRM_BIT 1
33+
#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
34+
35+
#define SIGALRM_BIT 2
36+
#define SIGALRM_MASK (1 << SIGALRM_BIT)
37+
38+
static int signals_enabled = 1;
39+
static int pending = 0;
40+
2341
void sig_handler(ARCH_SIGHDLR_PARAM)
2442
{
2543
struct sigcontext *sc;
44+
int enabled;
45+
46+
/* Must be the first thing that this handler does - x86_64 stores
47+
* the sigcontext in %rdx, and we need to save it before it has a
48+
* chance to get trashed.
49+
*/
2650

2751
ARCH_GET_SIGCONTEXT(sc, sig);
52+
53+
enabled = signals_enabled;
54+
if(!enabled && (sig == SIGIO)){
55+
pending |= SIGIO_MASK;
56+
return;
57+
}
58+
59+
block_signals();
60+
2861
CHOOSE_MODE_PROC(sig_handler_common_tt, sig_handler_common_skas,
2962
sig, sc);
63+
64+
set_signals(enabled);
3065
}
3166

3267
extern int timer_irq_inited;
3368

34-
void alarm_handler(ARCH_SIGHDLR_PARAM)
69+
static void real_alarm_handler(int sig, struct sigcontext *sc)
3570
{
36-
struct sigcontext *sc;
37-
38-
ARCH_GET_SIGCONTEXT(sc, sig);
39-
if(!timer_irq_inited) return;
71+
if(!timer_irq_inited){
72+
signals_enabled = 1;
73+
return;
74+
}
4075

4176
if(sig == SIGALRM)
4277
switch_timers(0);
@@ -46,17 +81,52 @@ void alarm_handler(ARCH_SIGHDLR_PARAM)
4681

4782
if(sig == SIGALRM)
4883
switch_timers(1);
84+
85+
}
86+
87+
void alarm_handler(ARCH_SIGHDLR_PARAM)
88+
{
89+
struct sigcontext *sc;
90+
int enabled;
91+
92+
ARCH_GET_SIGCONTEXT(sc, sig);
93+
94+
enabled = signals_enabled;
95+
if(!signals_enabled){
96+
if(sig == SIGVTALRM)
97+
pending |= SIGVTALRM_MASK;
98+
else pending |= SIGALRM_MASK;
99+
100+
return;
101+
}
102+
103+
block_signals();
104+
105+
real_alarm_handler(sig, sc);
106+
set_signals(enabled);
49107
}
50108

51109
extern void do_boot_timer_handler(struct sigcontext * sc);
52110

53111
void boot_timer_handler(ARCH_SIGHDLR_PARAM)
54112
{
55113
struct sigcontext *sc;
114+
int enabled;
56115

57116
ARCH_GET_SIGCONTEXT(sc, sig);
58117

118+
enabled = signals_enabled;
119+
if(!enabled){
120+
if(sig == SIGVTALRM)
121+
pending |= SIGVTALRM_MASK;
122+
else pending |= SIGALRM_MASK;
123+
return;
124+
}
125+
126+
block_signals();
127+
59128
do_boot_timer_handler(sc);
129+
set_signals(enabled);
60130
}
61131

62132
void set_sigstack(void *sig_stack, int size)
@@ -83,6 +153,7 @@ void set_handler(int sig, void (*handler)(int), int flags, ...)
83153
{
84154
struct sigaction action;
85155
va_list ap;
156+
sigset_t sig_mask;
86157
int mask;
87158

88159
va_start(ap, flags);
@@ -95,7 +166,12 @@ void set_handler(int sig, void (*handler)(int), int flags, ...)
95166
action.sa_flags = flags;
96167
action.sa_restorer = NULL;
97168
if(sigaction(sig, &action, NULL) < 0)
98-
panic("sigaction failed");
169+
panic("sigaction failed - errno = %d\n", errno);
170+
171+
sigemptyset(&sig_mask);
172+
sigaddset(&sig_mask, sig);
173+
if(sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
174+
panic("sigprocmask failed - errno = %d\n", errno);
99175
}
100176

101177
int change_sig(int signal, int on)
@@ -108,91 +184,74 @@ int change_sig(int signal, int on)
108184
return(!sigismember(&old, signal));
109185
}
110186

111-
/* Both here and in set/get_signal we don't touch SIGPROF, because we must not
112-
* disable profiling; it's safe because the profiling code does not interact
113-
* with the kernel code at all.*/
114-
115-
static void change_signals(int type)
116-
{
117-
sigset_t mask;
118-
119-
sigemptyset(&mask);
120-
sigaddset(&mask, SIGVTALRM);
121-
sigaddset(&mask, SIGALRM);
122-
sigaddset(&mask, SIGIO);
123-
if(sigprocmask(type, &mask, NULL) < 0)
124-
panic("Failed to change signal mask - errno = %d", errno);
125-
}
126-
127187
void block_signals(void)
128188
{
129-
change_signals(SIG_BLOCK);
189+
signals_enabled = 0;
130190
}
131191

132192
void unblock_signals(void)
133193
{
134-
change_signals(SIG_UNBLOCK);
135-
}
194+
int save_pending;
136195

137-
/* These are the asynchronous signals. SIGVTALRM and SIGARLM are handled
138-
* together under SIGVTALRM_BIT. SIGPROF is excluded because we want to
139-
* be able to profile all of UML, not just the non-critical sections. If
140-
* profiling is not thread-safe, then that is not my problem. We can disable
141-
* profiling when SMP is enabled in that case.
142-
*/
143-
#define SIGIO_BIT 0
144-
#define SIGVTALRM_BIT 1
145-
146-
static int enable_mask(sigset_t *mask)
147-
{
148-
int sigs;
196+
if(signals_enabled == 1)
197+
return;
149198

150-
sigs = sigismember(mask, SIGIO) ? 0 : 1 << SIGIO_BIT;
151-
sigs |= sigismember(mask, SIGVTALRM) ? 0 : 1 << SIGVTALRM_BIT;
152-
sigs |= sigismember(mask, SIGALRM) ? 0 : 1 << SIGVTALRM_BIT;
153-
return(sigs);
199+
/* We loop because the IRQ handler returns with interrupts off. So,
200+
* interrupts may have arrived and we need to re-enable them and
201+
* recheck pending.
202+
*/
203+
while(1){
204+
/* Save and reset save_pending after enabling signals. This
205+
* way, pending won't be changed while we're reading it.
206+
*/
207+
signals_enabled = 1;
208+
209+
save_pending = pending;
210+
if(save_pending == 0)
211+
return;
212+
213+
pending = 0;
214+
215+
/* We have pending interrupts, so disable signals, as the
216+
* handlers expect them off when they are called. They will
217+
* be enabled again above.
218+
*/
219+
220+
signals_enabled = 0;
221+
222+
/* Deal with SIGIO first because the alarm handler might
223+
* schedule, leaving the pending SIGIO stranded until we come
224+
* back here.
225+
*/
226+
if(save_pending & SIGIO_MASK)
227+
CHOOSE_MODE_PROC(sig_handler_common_tt,
228+
sig_handler_common_skas, SIGIO, NULL);
229+
230+
if(save_pending & SIGALRM_MASK)
231+
real_alarm_handler(SIGALRM, NULL);
232+
233+
if(save_pending & SIGVTALRM_MASK)
234+
real_alarm_handler(SIGVTALRM, NULL);
235+
}
154236
}
155237

156238
int get_signals(void)
157239
{
158-
sigset_t mask;
159-
160-
if(sigprocmask(SIG_SETMASK, NULL, &mask) < 0)
161-
panic("Failed to get signal mask");
162-
return(enable_mask(&mask));
240+
return signals_enabled;
163241
}
164242

165243
int set_signals(int enable)
166244
{
167-
sigset_t mask;
168245
int ret;
246+
if(signals_enabled == enable)
247+
return enable;
169248

170-
sigemptyset(&mask);
171-
if(enable & (1 << SIGIO_BIT))
172-
sigaddset(&mask, SIGIO);
173-
if(enable & (1 << SIGVTALRM_BIT)){
174-
sigaddset(&mask, SIGVTALRM);
175-
sigaddset(&mask, SIGALRM);
176-
}
177-
178-
/* This is safe - sigprocmask is guaranteed to copy locally the
179-
* value of new_set, do his work and then, at the end, write to
180-
* old_set.
181-
*/
182-
if(sigprocmask(SIG_UNBLOCK, &mask, &mask) < 0)
183-
panic("Failed to enable signals");
184-
ret = enable_mask(&mask);
185-
sigemptyset(&mask);
186-
if((enable & (1 << SIGIO_BIT)) == 0)
187-
sigaddset(&mask, SIGIO);
188-
if((enable & (1 << SIGVTALRM_BIT)) == 0){
189-
sigaddset(&mask, SIGVTALRM);
190-
sigaddset(&mask, SIGALRM);
191-
}
192-
if(sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
193-
panic("Failed to block signals");
249+
ret = signals_enabled;
250+
if(enable)
251+
unblock_signals();
252+
else block_signals();
194253

195-
return(ret);
254+
return ret;
196255
}
197256

198257
void os_usr1_signal(int on)

0 commit comments

Comments
 (0)