signals: don't copy siginfo_t on dequeue
RFC PATCH 1/1 signals: don't copy siginfo_t on dequeue
Quoted 1 line
From
60fc9a464377159ab807aec63277d4970019d631Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Thu, 26 Feb 2009 19:17:58 +0100
Subject: [PATCH] signals: don't copy siginfo_t on dequeue
Instead of copying the siginfo_t whenever a signal is dequeued, just get the pointer to the struct sigqueue, which can be freed by the caller when the signal has been delivered.
We can save kernel text (x86, 32-bit):
$ scripts/bloat-o-meter vmlinux-unpatched vmlinux add/remove: 0/0 grow/shrink: 3/7 up/down: 81/-538 (-457) function old new delta get_signal_to_deliver 871 922 +51 release_console_sem 459 481 +22 generate_resume_trace 611 619 +8 send_sigqueue 257 253 -4 vma_adjust 1101 1093 -8 sys_rt_sigtimedwait 548 531 -17 dequeue_signal 415 372 -43 __dequeue_signal 388 259 -129 signalfd_read 1290 1139 -151 do_notify_resume 2216 2030 -186
And we reduce stack pressure; In handle_signal() (in x86 code), we replace a siginfo_t (128 bytes) with a pointer (8 bytes on x86_64), and the same in signalfd_read().
There is a slight slowdown (2.02% relative increase in CPU time):
unpatched patched
----------------------------------------
mean: 3.078500 3.140800
stddev: 0.074624 0.168989
(Numbers are: CPU time in seconds, for two processes to ping-pong in total 655360 SIGUSR1/SIGUSR2 signals between each other. This was repeated 100 times for each kernel.)
This patch will also enable us to send signals from the kernel without copying any siginfo_t at all (and save a similar amount of stack for some senders) with a future patch.
This is just experimental for now, it may happen that I altered signal handling completely (although my system seems to work :-)). But I will be happy for feedback (on the patch, on the results, on how to improve the results, or anything).
--- arch/x86/kernel/signal.c | 10 ++-- fs/signalfd.c | 38 ++++++------ include/linux/sched.h | 12 ++-- include/linux/signal.h | 3 +- include/linux/tracehook.h | 6 +- kernel/signal.c | 155 +++++++++++++++++++++------------------------ 6 files changed, 108 insertions(+), 116 deletions(-)
arch/x86/kernel/signal.c +5 -5
@@ -800,8 +800,7 @@ handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,static void do_signal (struct pt_regs *regs) {struct k_sigaction ka; -siginfo_t info; -int signr; +struct sigqueue *info;sigset_t *oldset;/*
@@ -819,8 +818,8 @@ static void do_signal(struct pt_regs *regs)else oldset = ¤t->blocked; - signr =get_signal_to_deliver (&info, &ka, regs,NULL ); -if (signr >0 ) { + info =get_signal_to_deliver (&ka, regs,NULL ); +if (info && info->info.si_signo >0 ) {/* * Re-enable any watchpoints before delivering the * signal to user space. The processor register will
@@ -831,7 +830,7 @@ static void do_signal(struct pt_regs *regs)set_debugreg (current->thread.debugreg7,7 );/* Whee! Actually deliver the signal. */ -if (handle_signal (signr, &info, &ka, oldset, regs) ==0 ) { +if (handle_signal (info->info.si_signo, &info->info, &ka, oldset, regs) ==0 ) {/* * A signal was successfully delivered; the saved * sigmask will have been stored in the signal frame,
@@ -840,6 +839,7 @@ static void do_signal(struct pt_regs *regs)*/ current_thread_info ()->status &= ~TS_RESTORE_SIGMASK ; } +__sigqueue_free (info);return ; }
fs/signalfd.c +19 -19
@@ -125,32 +125,29 @@ static int signalfd_copyinfo(struct signalfd_siginfo __user *uinfo,return err ? -EFAULT :sizeof (*uinfo); } -static ssize_t signalfd_dequeue (struct signalfd_ctx *ctx,siginfo_t *info, -int nonblock) +static struct sigqueue *signalfd_dequeue (struct signalfd_ctx *ctx,int nonblock) { -ssize_t ret; +struct sigqueue *signal;DECLARE_WAITQUEUE (wait, current);spin_lock_irq (¤t->sighand->siglock); - ret =dequeue_signal (current, &ctx->sigmask, info); -switch (ret) { -case 0 : -if (!nonblock) -break ; - ret = -EAGAIN ; -default : + signal =dequeue_signal (current, &ctx->sigmask); +if (signal) {spin_unlock_irq (¤t->sighand->siglock); -return ret; +return signal; + }else if (nonblock) { +spin_unlock_irq (¤t->sighand->siglock); +return ERR_PTR (-EAGAIN ); }add_wait_queue (¤t->sighand->signalfd_wqh, &wait);for (;;) {set_current_state (TASK_INTERRUPTIBLE ); - ret =dequeue_signal (current, &ctx->sigmask, info); -if (ret !=0 ) + signal =dequeue_signal (current, &ctx->sigmask); +if (signal)break ;if (signal_pending (current)) { - ret = -ERESTARTSYS ; + signal =ERR_PTR (-ERESTARTSYS );break ; }spin_unlock_irq (¤t->sighand->siglock);
@@ -162,7 +159,7 @@ static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,remove_wait_queue (¤t->sighand->signalfd_wqh, &wait);__set_current_state (TASK_RUNNING ); -return ret; +return signal; }/*
@@ -177,7 +174,7 @@ static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count,struct signalfd_siginfo __user *siginfo;int nonblock = file->f_flags &O_NONBLOCK ;ssize_t ret, total =0 ; -siginfo_t info; +struct sigqueue *info; count /=sizeof (struct signalfd_siginfo );if (!count)
@@ -185,10 +182,13 @@ static ssize_t signalfd_read(struct file *file, char __user *buf, size_t count, siginfo = (struct signalfd_siginfo __user *) buf; do { - ret = signalfd_dequeue(ctx, &info, nonblock); - if (unlikely(ret <= 0)) + info = signalfd_dequeue(ctx, nonblock); + if (unlikely(IS_ERR(info))) { + ret = PTR_ERR(info); break; - ret = signalfd_copyinfo(siginfo, &info); + } + ret = signalfd_copyinfo(siginfo, &info->info); + __sigqueue_free(info); if (ret < 0) break; siginfo++;
include/linux/sched.h +7 -5
@@ -1858,18 +1858,19 @@ extern void proc_caches_init(void);extern void flush_signals (struct task_struct *);extern void ignore_signals (struct task_struct *);extern void flush_signal_handlers (struct task_struct *,int force_default); -extern int dequeue_signal (struct task_struct *tsk,sigset_t *mask,siginfo_t *info); +extern struct sigqueue *dequeue_signal (struct task_struct *tsk,sigset_t *mask); -static inline int dequeue_signal_lock (struct task_struct *tsk,sigset_t *mask,siginfo_t *info) +static inline struct sigqueue *dequeue_signal_lock (struct task_struct *tsk, +sigset_t *mask) {unsigned long flags; -int ret; +struct sigqueue *info;spin_lock_irqsave (&tsk->sighand->siglock, flags); - ret =dequeue_signal (tsk, mask, info); + info =dequeue_signal (tsk, mask);spin_unlock_irqrestore (&tsk->sighand->siglock, flags); -return ret; +return info; } extern void block_all_signals(int (*notifier)(void *priv), void *priv,
@@ -1891,6 +1892,7 @@ extern void force_sig_specific(int, struct task_struct *);extern int send_sig (int ,struct task_struct *,int );extern void zap_other_threads (struct task_struct *p);extern struct sigqueue *sigqueue_alloc (void ); +extern void __sigqueue_free (struct sigqueue *);extern void sigqueue_free (struct sigqueue *);extern int send_sigqueue (struct sigqueue *,struct task_struct *,int group);extern int do_sigaction (int ,struct k_sigaction *,struct k_sigaction *);
include/linux/signal.h +2 -1
@@ -240,7 +240,8 @@ extern int sigprocmask(int, sigset_t *, sigset_t *);extern int show_unhandled_signals;struct pt_regs ; -extern int get_signal_to_deliver (siginfo_t *info,struct k_sigaction *return_ka,struct pt_regs *regs,void *cookie); +extern struct sigqueue *get_signal_to_deliver (struct k_sigaction *return_ka, +struct pt_regs *regs,void *cookie);extern void exit_signals (struct task_struct *tsk);extern struct kmem_cache *sighand_cachep;
include/linux/tracehook.h +2 -4
@@ -459,10 +459,8 @@ static inline int tracehook_force_sigpending(void)* * Called with @task->sighand->siglock held, before dequeuing pending signals. */ -static inline int tracehook_get_signal (struct task_struct *task, -struct pt_regs *regs, -siginfo_t *info, -struct k_sigaction *return_ka) +static inline struct sigqueue *tracehook_get_signal (struct task_struct *task, +struct pt_regs *regs,struct k_sigaction *return_ka) {return 0 ; }
kernel/signal.c +73 -82
352 lines not on this page · raw message
Signature · 1 line
1.6.0.6
Quoted 31 lines · Vegard Nossum
Quoted 1 line
From
60fc9a464377159ab807aec63277d4970019d631Mon Sep 17 00:00:00 2001From: Vegard Nossum <[email protected]>
Date: Thu, 26 Feb 2009 19:17:58 +0100
Subject: [PATCH] signals: don't copysiginfo_ton dequeueInstead of copying the
siginfo_twhenever a signal is dequeued, just get the pointer to the struct sigqueue, which can be freed by the caller when the signal has been delivered.We can save kernel text (x86, 32-bit):
$ scripts/bloat-o-meter vmlinux-unpatched vmlinux add/remove: 0/0 grow/shrink: 3/7 up/down: 81/-538 (-457) function old new delta get_signal_to_deliver 871 922 +51 release_console_sem 459 481 +22 generate_resume_trace 611 619 +8 send_sigqueue 257 253 -4 vma_adjust 1101 1093 -8 sys_rt_sigtimedwait 548 531 -17 dequeue_signal 415 372 -43 __dequeue_signal 388 259 -129 signalfd_read 1290 1139 -151 do_notify_resume 2216 2030 -186And we reduce stack pressure; In
handle_signal()(in x86 code), we replace asiginfo_t(128 bytes) with a pointer (8 bytes onx86_64), and the same insignalfd_read().There is a slight slowdown (2.02% relative increase in CPU time):
Quoted 8 lines
unpatched patched
----------------------------------------
mean: 3.078500 3.140800
stddev: 0.074624 0.168989(Numbers are: CPU time in seconds, for two processes to ping-pong in total 655360 SIGUSR1/SIGUSR2 signals between each other. This was repeated 100 times for each kernel.)
hm, does this SIGUSR1/SIGUSR2 test actually make use siginfo?
I.e. shouldnt we have seen a speedup, due to not having to copy the siginfo structure?
Ingo
2009/2/26 Ingo Molnar <[email protected]>:
Quoted 39 lines · Vegard Nossum
Quoted 37 lines
Quoted 2 lines
From
60fc9a464377159ab807aec63277d4970019d631Mon Sep 17 00:00:00 2001From: Vegard Nossum <[email protected]>
Date: Thu, 26 Feb 2009 19:17:58 +0100
Subject: [PATCH] signals: don't copysiginfo_ton dequeueInstead of copying the
siginfo_twhenever a signal is dequeued, just get the pointer to the struct sigqueue, which can be freed by the caller when the signal has been delivered.We can save kernel text (x86, 32-bit):
$ scripts/bloat-o-meter vmlinux-unpatched vmlinux add/remove: 0/0 grow/shrink: 3/7 up/down: 81/-538 (-457) function old new delta get_signal_to_deliver 871 922 +51 release_console_sem 459 481 +22 generate_resume_trace 611 619 +8 send_sigqueue 257 253 -4 vma_adjust 1101 1093 -8 sys_rt_sigtimedwait 548 531 -17 dequeue_signal 415 372 -43 __dequeue_signal 388 259 -129 signalfd_read 1290 1139 -151 do_notify_resume 2216 2030 -186And we reduce stack pressure; In
handle_signal()(in x86 code), we replace asiginfo_t(128 bytes) with a pointer (8 bytes onx86_64), and the same insignalfd_read().There is a slight slowdown (2.02% relative increase in CPU time):
unpatched patched
----------------------------------------
mean: 3.078500 3.140800
stddev: 0.074624 0.168989
Quoted 5 lines
Quoted 3 lines
(Numbers are: CPU time in seconds, for two processes to ping-pong in total 655360 SIGUSR1/SIGUSR2 signals between each other. This was repeated 100 times for each kernel.)
hm, does this SIGUSR1/SIGUSR2 test actually make use siginfo?
The delivery of a signal requires one copy_siginfo(). Likewise for e.g. sys_kill(), which also requires one copy_siginfo(). So with this patch, we have saved only the copy_siginfo() of the delivery path.
Quoted 2 lines
I.e. shouldnt we have seen a speedup, due to not having to copy the siginfo structure?
Actually, no. Because copy_siginfo() does not copy the whole (128 byte) structure if the signal was generated by the user, just probably the first 24 bytes or so. So I would expect the "kernel generated signal" path to be faster, but that is not tested by my SIGUSR test.
Besides, I think there is a small overhead in now having an extra level of indirection for getting our hands on the signal number.
But we might able to speed up the user->user case a little bit by fixing sys_kill() not to use copy_siginfo() too.
Vegard
Signature · 4 lines
"The animistic metaphor of the bug that maliciously sneaked in while the programmer was not looking is intellectually dishonest as it disguises that the error is the programmer's own creation." -- E. W. Dijkstra, EWD1036
Quoted 5 lines · Vegard Nossum
2009/2/26 Ingo Molnar <[email protected]>:
Quoted 3 lines
I.e. shouldnt we have seen a speedup, due to not having to copy the siginfo structure?
I am puzzled by these numbers too. I can't understand how this patch can make any difference.
Quoted 7 lines
Actually, no. Because
copy_siginfo()does not copy the whole (128 byte) structure if the signal was generated by the user, just probably the first 24 bytes or so. So I would expect the "kernel generated signal" path to be faster, but that is not tested by my SIGUSR test.Besides, I think there is a small overhead in now having an extra level of indirection for getting our hands on the signal number.
But this all is nothing compared to the "real work" we have to do in order to handle the signal? setup_rt_frame, sys_rt_sigreturn...
Quoted 2 lines
But we might able to speed up the
user->usercase a little bit by fixingsys_kill()not to usecopy_siginfo()too.
Yes. It would really, really nice to preallocate siginfo in sys_kill/etc and avoid the GFP_ATOMIC allocation in send_signal().
Unfortunaltely, this is not easy too. Because we can send the signal to pgrp or to all tasks.
Oleg.
Quoted 9 lines · Oleg Nesterov
Quoted 5 lines · Vegard Nossum
2009/2/26 Ingo Molnar <[email protected]>:
Quoted 3 lines
I.e. shouldnt we have seen a speedup, due to not having to copy the siginfo structure?
I am puzzled by these numbers too. I can't understand how this patch can make any difference.
Measurement noise perhaps. Especially such microbenchmarks can be very sensitive to cache layout, etc.
Ingo
Quoted 4 lines · Vegard Nossum
Instead of copying the
siginfo_twhenever a signal is dequeued, just get the pointer to the struct sigqueue, which can be freed by the caller when the signal has been delivered.
Yes, it would bi nice. But it is not that simple,
Quoted 46 lines
kernel/signal.c +11
-22-static void collect_signal (int sig,struct sigpending *list,siginfo_t *info) +static struct sigqueue *collect_signal (int sig,struct sigpending *list) {struct sigqueue *q, *first =NULL ;@@ -377,40 +377,29 @@ static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)if (first) { still_pending:list_del_init (&first->list); -copy_siginfo (info, &first->info); -__sigqueue_free (first); - }else { -/* Ok, it wasn't in the queue. This must be -a fast-pathed signal or we must have been -out of queue space. So zero out the info. -*/ - info->si_signo = sig; - info->si_errno =0 ; - info->si_code =0 ; - info->si_pid =0 ; - info->si_uid =0 ; } + +return first; } -static int __dequeue_signal (struct sigpending *pending,sigset_t *mask, -siginfo_t *info) +static struct sigqueue *__dequeue_signal (struct sigpending *pending, +sigset_t *mask) {int sig =next_signal (pending, mask); -if (sig) { -if (current->notifier) { -if (sigismember (current->notifier_mask, sig)) { -if (!(current->notifier)(current->notifier_data)) { -clear_thread_flag (TIF_SIGPENDING ); -return 0 ; - } +if (!sig) +return NULL ; + +if (current->notifier) { +if (sigismember (current->notifier_mask, sig)) { +if (!(current->notifier)(current->notifier_data)) {
kernel/signal.c static void collect_signal(int sig, struct sigpending *list, siginfo_t *info) · line 402
Quoted 10 lines
kernel/signal.c +3
-3+clear_thread_flag (TIF_SIGPENDING ); +return 0 ; } } - -collect_signal (sig, pending, info); } -return sig; +return collect_signal (sig, pending);
So. dequeue_signal() returns NULL if there is no siginfo queued. In that case we assume that the signal is not pending.
But this is not right. Think about SEND_SIG_FORCED, or __sigqueue_alloc() failure when the signal is sent. Or look at zap_other_threads() for example, it just sets the bit in ->pending but doesn't queue siginfo.
Oleg.
2009/2/26 Oleg Nesterov <[email protected]>:
Quoted 6 lines
So.
dequeue_signal()returns NULL if there is no siginfo queued. In that case we assume that the signal is not pending.But this is not right. Think about
SEND_SIG_FORCED, or__sigqueue_alloc()failure when the signal is sent. Or look atzap_other_threads()for example, it just sets the bit in ->pending but doesn't queue siginfo.
I will investigate. Thanks for looking, and thanks for the pointers :-)
Vegard
Signature · 4 lines
"The animistic metaphor of the bug that maliciously sneaked in while the programmer was not looking is intellectually dishonest as it disguises that the error is the programmer's own creation." -- E. W. Dijkstra, EWD1036
Quoted 10 lines · Vegard Nossum
2009/2/26 Oleg Nesterov <[email protected]>:
Quoted 6 lines
So.
dequeue_signal()returns NULL if there is no siginfo queued. In that case we assume that the signal is not pending.But this is not right. Think about
SEND_SIG_FORCED, or__sigqueue_alloc()failure when the signal is sent. Or look atzap_other_threads()for example, it just sets the bit in ->pending but doesn't queue siginfo.I will investigate.
Cough. Well, I must admit I am a bit skeptical about this patch ;) Because I suspect it will add more complications to the code. And _I think_ avoiding copy_siginfo() does not buy too much. I will be happy if I am wrong, though.
But. If you are going to do another version, then please note there is another problem with this patch, SIGQUEUE_PREALLOC.
If collect_signal() returns SIGQUEUE_PREALLOC info, we can not drop ->siglock. I mean, once we drop ->siglock, this info can be freed, so for example
spin_unlock(&tsk->sighand->siglock);
- do_schedule_next_timer(info);
+ do_schedule_next_timer(&signal->info);
even this part is not safe.
Also. The patch uses __sigqueue_free() to free the delivered siginfo, but this is not safe without ->siglock, we can race with sigqueue_free().
Oleg.