Skip to content

Commit fe0faa0

Browse files
committed
signal: sys_rt_sigtimedwait: simplify the timeout logic
No functional changes, cleanup compat_sys_rt_sigtimedwait() and sys_rt_sigtimedwait(). Calculate the timeout before we take ->siglock, this simplifies and lessens the code. Use timespec_valid() to check the timespec. 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 bb7efee commit fe0faa0

2 files changed

Lines changed: 39 additions & 51 deletions

File tree

‎kernel/compat.c‎

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
893893
int sig;
894894
struct timespec t;
895895
siginfo_t info;
896-
long ret, timeout = 0;
896+
long ret, timeout;
897897

898898
if (sigsetsize != sizeof(sigset_t))
899899
return -EINVAL;
@@ -904,36 +904,30 @@ compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
904904
sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
905905
signotset(&s);
906906

907+
timeout = MAX_SCHEDULE_TIMEOUT;
907908
if (uts) {
908909
if (get_compat_timespec (&t, uts))
909910
return -EFAULT;
910-
if (t.tv_nsec >= 1000000000L || t.tv_nsec < 0
911-
|| t.tv_sec < 0)
911+
if (!timespec_valid(&t))
912912
return -EINVAL;
913+
timeout = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
913914
}
914915

915916
spin_lock_irq(&current->sighand->siglock);
916917
sig = dequeue_signal(current, &s, &info);
917-
if (!sig) {
918-
timeout = MAX_SCHEDULE_TIMEOUT;
919-
if (uts)
920-
timeout = timespec_to_jiffies(&t)
921-
+(t.tv_sec || t.tv_nsec);
922-
if (timeout) {
923-
current->real_blocked = current->blocked;
924-
sigandsets(&current->blocked, &current->blocked, &s);
925-
926-
recalc_sigpending();
927-
spin_unlock_irq(&current->sighand->siglock);
928-
929-
timeout = schedule_timeout_interruptible(timeout);
930-
931-
spin_lock_irq(&current->sighand->siglock);
932-
sig = dequeue_signal(current, &s, &info);
933-
current->blocked = current->real_blocked;
934-
siginitset(&current->real_blocked, 0);
935-
recalc_sigpending();
936-
}
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();
937931
}
938932
spin_unlock_irq(&current->sighand->siglock);
939933

@@ -943,7 +937,7 @@ compat_sys_rt_sigtimedwait (compat_sigset_t __user *uthese,
943937
if (copy_siginfo_to_user32(uinfo, &info))
944938
ret = -EFAULT;
945939
}
946-
}else {
940+
} else {
947941
ret = timeout?-EINTR:-EAGAIN;
948942
}
949943
return ret;

‎kernel/signal.c‎

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,7 @@ SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
25192519
sigset_t these;
25202520
struct timespec ts;
25212521
siginfo_t info;
2522-
long timeout = 0;
2522+
long timeout;
25232523

25242524
/* XXX: Don't preclude handling different sized sigset_t's. */
25252525
if (sigsetsize != sizeof(sigset_t))
@@ -2535,41 +2535,35 @@ SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
25352535
sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
25362536
signotset(&these);
25372537

2538+
timeout = MAX_SCHEDULE_TIMEOUT;
25382539
if (uts) {
25392540
if (copy_from_user(&ts, uts, sizeof(ts)))
25402541
return -EFAULT;
2541-
if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2542-
|| ts.tv_sec < 0)
2542+
if (!timespec_valid(&ts))
25432543
return -EINVAL;
2544+
timeout = timespec_to_jiffies(&ts) + (ts.tv_sec || ts.tv_nsec);
25442545
}
25452546

25462547
spin_lock_irq(&current->sighand->siglock);
25472548
sig = dequeue_signal(current, &these, &info);
2548-
if (!sig) {
2549-
timeout = MAX_SCHEDULE_TIMEOUT;
2550-
if (uts)
2551-
timeout = (timespec_to_jiffies(&ts)
2552-
+ (ts.tv_sec || ts.tv_nsec));
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);
25532559

2554-
if (timeout) {
2555-
/*
2556-
* None ready -- temporarily unblock those we're
2557-
* interested while we are sleeping in so that we'll
2558-
* be awakened when they arrive.
2559-
*/
2560-
current->real_blocked = current->blocked;
2561-
sigandsets(&current->blocked, &current->blocked, &these);
2562-
recalc_sigpending();
2563-
spin_unlock_irq(&current->sighand->siglock);
2564-
2565-
timeout = schedule_timeout_interruptible(timeout);
2566-
2567-
spin_lock_irq(&current->sighand->siglock);
2568-
sig = dequeue_signal(current, &these, &info);
2569-
current->blocked = current->real_blocked;
2570-
siginitset(&current->real_blocked, 0);
2571-
recalc_sigpending();
2572-
}
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();
25732567
}
25742568
spin_unlock_irq(&current->sighand->siglock);
25752569

0 commit comments

Comments
 (0)