Skip to content

Commit 943df14

Browse files
committed
signal: introduce do_sigtimedwait() to factor out compat/native code
Factor out the common code in sys_rt_sigtimedwait/compat_sys_rt_sigtimedwait to the new helper, do_sigtimedwait(). Add the comment to document the extra tick we add to timespec_to_jiffies(ts), thanks to Linus who explained this to me. Perhaps it would be better to move compat_sys_rt_sigtimedwait() into signal.c under CONFIG_COMPAT, then we can make do_sigtimedwait() static. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Acked-by: Tejun Heo <tj@kernel.org> Reviewed-by: Matt Fleming <matt.fleming@linux.intel.com>
1 parent fe0faa0 commit 943df14

3 files changed

Lines changed: 74 additions & 79 deletions

File tree

‎include/linux/signal.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ extern int __group_send_sig_info(int, struct siginfo *, struct task_struct *);
242242
extern long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig,
243243
siginfo_t *info);
244244
extern long do_sigpending(void __user *, unsigned long);
245+
extern int do_sigtimedwait(const sigset_t *, siginfo_t *,
246+
const struct timespec *);
245247
extern int sigprocmask(int, sigset_t *, sigset_t *);
246248
extern void set_current_blocked(const sigset_t *);
247249
extern int show_unhandled_signals;

‎kernel/compat.c‎

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -890,56 +890,29 @@ compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
890890
{
891891
compat_sigset_t s32;
892892
sigset_t s;
893-
int sig;
894893
struct timespec t;
895894
siginfo_t info;
896-
long ret, timeout;
895+
long ret;
897896

898897
if (sigsetsize != sizeof(sigset_t))
899898
return -EINVAL;
900899

901900
if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
902901
return -EFAULT;
903902
sigset_from_compat(&s, &s32);
904-
sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
905-
signotset(&s);
906903

907-
timeout = MAX_SCHEDULE_TIMEOUT;
908904
if (uts) {
909-
if (get_compat_timespec (&t, uts))
905+
if (get_compat_timespec(&t, uts))
910906
return -EFAULT;
911-
if (!timespec_valid(&t))
912-
return -EINVAL;
913-
timeout = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
914907
}
915908

916-
spin_lock_irq(&current->sighand->siglock);
917-
sig = dequeue_signal(current, &s, &info);
918-
if (!sig && timeout) {
919-
current->real_blocked = current->blocked;
920-
sigandsets(&current->blocked, &current->blocked, &s);
921-
recalc_sigpending();
922-
spin_unlock_irq(&current->sighand->siglock);
923-
924-
timeout = schedule_timeout_interruptible(timeout);
925-
926-
spin_lock_irq(&current->sighand->siglock);
927-
sig = dequeue_signal(current, &s, &info);
928-
current->blocked = current->real_blocked;
929-
siginitset(&current->real_blocked, 0);
930-
recalc_sigpending();
931-
}
932-
spin_unlock_irq(&current->sighand->siglock);
909+
ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
933910

934-
if (sig) {
935-
ret = sig;
936-
if (uinfo) {
937-
if (copy_siginfo_to_user32(uinfo, &info))
938-
ret = -EFAULT;
939-
}
940-
} else {
941-
ret = timeout?-EINTR:-EAGAIN;
911+
if (ret > 0 && uinfo) {
912+
if (copy_siginfo_to_user32(uinfo, &info))
913+
ret = -EFAULT;
942914
}
915+
943916
return ret;
944917

945918
}

‎kernel/signal.c‎

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,66 @@ int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
25032503

25042504
#endif
25052505

2506+
/**
2507+
* do_sigtimedwait - wait for queued signals specified in @which
2508+
* @which: queued signals to wait for
2509+
* @info: if non-null, the signal's siginfo is returned here
2510+
* @ts: upper bound on process time suspension
2511+
*/
2512+
int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2513+
const struct timespec *ts)
2514+
{
2515+
struct task_struct *tsk = current;
2516+
long timeout = MAX_SCHEDULE_TIMEOUT;
2517+
sigset_t mask = *which;
2518+
int sig;
2519+
2520+
if (ts) {
2521+
if (!timespec_valid(ts))
2522+
return -EINVAL;
2523+
timeout = timespec_to_jiffies(ts);
2524+
/*
2525+
* We can be close to the next tick, add another one
2526+
* to ensure we will wait at least the time asked for.
2527+
*/
2528+
if (ts->tv_sec || ts->tv_nsec)
2529+
timeout++;
2530+
}
2531+
2532+
/*
2533+
* Invert the set of allowed signals to get those we want to block.
2534+
*/
2535+
sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2536+
signotset(&mask);
2537+
2538+
spin_lock_irq(&tsk->sighand->siglock);
2539+
sig = dequeue_signal(tsk, &mask, info);
2540+
if (!sig && timeout) {
2541+
/*
2542+
* None ready, temporarily unblock those we're interested
2543+
* while we are sleeping in so that we'll be awakened when
2544+
* they arrive.
2545+
*/
2546+
tsk->real_blocked = tsk->blocked;
2547+
sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2548+
recalc_sigpending();
2549+
spin_unlock_irq(&tsk->sighand->siglock);
2550+
2551+
timeout = schedule_timeout_interruptible(timeout);
2552+
2553+
spin_lock_irq(&tsk->sighand->siglock);
2554+
sig = dequeue_signal(tsk, &mask, info);
2555+
tsk->blocked = tsk->real_blocked;
2556+
siginitset(&tsk->real_blocked, 0);
2557+
recalc_sigpending();
2558+
}
2559+
spin_unlock_irq(&tsk->sighand->siglock);
2560+
2561+
if (sig)
2562+
return sig;
2563+
return timeout ? -EINTR : -EAGAIN;
2564+
}
2565+
25062566
/**
25072567
* sys_rt_sigtimedwait - synchronously wait for queued signals specified
25082568
* in @uthese
@@ -2515,11 +2575,10 @@ SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
25152575
siginfo_t __user *, uinfo, const struct timespec __user *, uts,
25162576
size_t, sigsetsize)
25172577
{
2518-
int ret, sig;
25192578
sigset_t these;
25202579
struct timespec ts;
25212580
siginfo_t info;
2522-
long timeout;
2581+
int ret;
25232582

25242583
/* XXX: Don't preclude handling different sized sigset_t's. */
25252584
if (sigsetsize != sizeof(sigset_t))
@@ -2528,55 +2587,16 @@ SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
25282587
if (copy_from_user(&these, uthese, sizeof(these)))
25292588
return -EFAULT;
25302589

2531-
/*
2532-
* Invert the set of allowed signals to get those we
2533-
* want to block.
2534-
*/
2535-
sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2536-
signotset(&these);
2537-
2538-
timeout = MAX_SCHEDULE_TIMEOUT;
25392590
if (uts) {
25402591
if (copy_from_user(&ts, uts, sizeof(ts)))
25412592
return -EFAULT;
2542-
if (!timespec_valid(&ts))
2543-
return -EINVAL;
2544-
timeout = timespec_to_jiffies(&ts) + (ts.tv_sec || ts.tv_nsec);
25452593
}
25462594

2547-
spin_lock_irq(&current->sighand->siglock);
2548-
sig = dequeue_signal(current, &these, &info);
2549-
if (!sig && timeout) {
2550-
/*
2551-
* None ready -- temporarily unblock those we're
2552-
* interested while we are sleeping in so that we'll
2553-
* be awakened when they arrive.
2554-
*/
2555-
current->real_blocked = current->blocked;
2556-
sigandsets(&current->blocked, &current->blocked, &these);
2557-
recalc_sigpending();
2558-
spin_unlock_irq(&current->sighand->siglock);
2559-
2560-
timeout = schedule_timeout_interruptible(timeout);
2561-
2562-
spin_lock_irq(&current->sighand->siglock);
2563-
sig = dequeue_signal(current, &these, &info);
2564-
current->blocked = current->real_blocked;
2565-
siginitset(&current->real_blocked, 0);
2566-
recalc_sigpending();
2567-
}
2568-
spin_unlock_irq(&current->sighand->siglock);
2595+
ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
25692596

2570-
if (sig) {
2571-
ret = sig;
2572-
if (uinfo) {
2573-
if (copy_siginfo_to_user(uinfo, &info))
2574-
ret = -EFAULT;
2575-
}
2576-
} else {
2577-
ret = -EAGAIN;
2578-
if (timeout)
2579-
ret = -EINTR;
2597+
if (ret > 0 && uinfo) {
2598+
if (copy_siginfo_to_user(uinfo, &info))
2599+
ret = -EFAULT;
25802600
}
25812601

25822602
return ret;

0 commit comments

Comments
 (0)