All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@redhat.com>
To: Linus Torvalds <torvalds@linux-foundation.org>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Tejun Heo <tj@kernel.org>,
	"Nikita V. Youshchenko" <nyoushchenko@mvista.com>,
	Matt Fleming <matt@console-pimps.org>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 2/6] signal: introduce do_sigtimedwait() to factor out compat/native code
Date: Tue, 26 Apr 2011 21:49:04 +0200	[thread overview]
Message-ID: <20110426194904.GC8520@redhat.com> (raw)
In-Reply-To: <20110426194822.GA8520@redhat.com>

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>
---

 include/linux/signal.h |    2 
 kernel/signal.c        |  104 +++++++++++++++++++++++++++----------------------
 kernel/compat.c        |   39 ++----------------
 3 files changed, 67 insertions(+), 78 deletions(-)

--- sigprocmask/include/linux/signal.h~2_do_sigtimedwait	2011-04-26 19:52:30.000000000 +0200
+++ sigprocmask/include/linux/signal.h	2011-04-26 19:53:42.000000000 +0200
@@ -242,6 +242,8 @@ extern int __group_send_sig_info(int, st
 extern long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig,
 				 siginfo_t *info);
 extern long do_sigpending(void __user *, unsigned long);
+extern int do_sigtimedwait(const sigset_t *, siginfo_t *,
+				const struct timespec *);
 extern int sigprocmask(int, sigset_t *, sigset_t *);
 extern void set_current_blocked(const sigset_t *);
 extern int show_unhandled_signals;
--- sigprocmask/kernel/signal.c~2_do_sigtimedwait	2011-04-26 19:53:19.000000000 +0200
+++ sigprocmask/kernel/signal.c	2011-04-26 19:53:42.000000000 +0200
@@ -2311,6 +2311,60 @@ int copy_siginfo_to_user(siginfo_t __use
 
 #endif
 
+int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
+			const struct timespec *ts)
+{
+	struct task_struct *tsk = current;
+	long timeout = MAX_SCHEDULE_TIMEOUT;
+	sigset_t mask = *which;
+	int sig;
+
+	if (ts) {
+		if (!timespec_valid(ts))
+			return -EINVAL;
+		timeout = timespec_to_jiffies(ts);
+		/*
+		 * We can be close to the next tick, add another one
+		 * to ensure we will wait at least the time asked for.
+		 */
+		if (ts->tv_sec || ts->tv_nsec)
+			timeout++;
+	}
+
+	/*
+	 * Invert the set of allowed signals to get those we want to block.
+	 */
+	sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
+	signotset(&mask);
+
+	spin_lock_irq(&tsk->sighand->siglock);
+	sig = dequeue_signal(tsk, &mask, info);
+	if (!sig && timeout) {
+		/*
+		 * None ready, temporarily unblock those we're interested
+		 * while we are sleeping in so that we'll be awakened when
+		 * they arrive.
+		 */
+		tsk->real_blocked = tsk->blocked;
+		sigandsets(&tsk->blocked, &tsk->blocked, &mask);
+		recalc_sigpending();
+		spin_unlock_irq(&tsk->sighand->siglock);
+
+		timeout = schedule_timeout_interruptible(timeout);
+
+		spin_lock_irq(&tsk->sighand->siglock);
+		sig = dequeue_signal(tsk, &mask, info);
+		tsk->blocked = tsk->real_blocked;
+		siginitset(&tsk->real_blocked, 0);
+		recalc_sigpending();
+	}
+	spin_unlock_irq(&tsk->sighand->siglock);
+
+	if (sig)
+		return sig;
+	return timeout ? -EINTR : -EAGAIN;
+}
+
 /**
  *  sys_rt_sigtimedwait - synchronously wait for queued signals specified
  *			in @uthese
@@ -2323,11 +2377,10 @@ SYSCALL_DEFINE4(rt_sigtimedwait, const s
 		siginfo_t __user *, uinfo, const struct timespec __user *, uts,
 		size_t, sigsetsize)
 {
-	int ret, sig;
 	sigset_t these;
 	struct timespec ts;
 	siginfo_t info;
-	long timeout;
+	int ret;
 
 	/* XXX: Don't preclude handling different sized sigset_t's.  */
 	if (sigsetsize != sizeof(sigset_t))
@@ -2336,55 +2389,16 @@ SYSCALL_DEFINE4(rt_sigtimedwait, const s
 	if (copy_from_user(&these, uthese, sizeof(these)))
 		return -EFAULT;
 
-	/*
-	 * Invert the set of allowed signals to get those we
-	 * want to block.
-	 */
-	sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
-	signotset(&these);
-
-	timeout = MAX_SCHEDULE_TIMEOUT;
 	if (uts) {
 		if (copy_from_user(&ts, uts, sizeof(ts)))
 			return -EFAULT;
-		if (!timespec_valid(&ts))
-			return -EINVAL;
-		timeout = timespec_to_jiffies(&ts) + (ts.tv_sec || ts.tv_nsec);
 	}
 
-	spin_lock_irq(&current->sighand->siglock);
-	sig = dequeue_signal(current, &these, &info);
-	if (!sig && timeout) {
-		/*
-		 * None ready -- temporarily unblock those we're
-		 * interested while we are sleeping in so that we'll
-		 * be awakened when they arrive.
-		 */
-		current->real_blocked = current->blocked;
-		sigandsets(&current->blocked, &current->blocked, &these);
-		recalc_sigpending();
-		spin_unlock_irq(&current->sighand->siglock);
-
-		timeout = schedule_timeout_interruptible(timeout);
-
-		spin_lock_irq(&current->sighand->siglock);
-		sig = dequeue_signal(current, &these, &info);
-		current->blocked = current->real_blocked;
-		siginitset(&current->real_blocked, 0);
-		recalc_sigpending();
-	}
-	spin_unlock_irq(&current->sighand->siglock);
+	ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
 
-	if (sig) {
-		ret = sig;
-		if (uinfo) {
-			if (copy_siginfo_to_user(uinfo, &info))
-				ret = -EFAULT;
-		}
-	} else {
-		ret = -EAGAIN;
-		if (timeout)
-			ret = -EINTR;
+	if (ret > 0 && uinfo) {
+		if (copy_siginfo_to_user(uinfo, &info))
+			ret = -EFAULT;
 	}
 
 	return ret;
--- sigprocmask/kernel/compat.c~2_do_sigtimedwait	2011-04-26 19:53:19.000000000 +0200
+++ sigprocmask/kernel/compat.c	2011-04-26 19:53:42.000000000 +0200
@@ -890,10 +890,9 @@ compat_sys_rt_sigtimedwait (compat_sigse
 {
 	compat_sigset_t s32;
 	sigset_t s;
-	int sig;
 	struct timespec t;
 	siginfo_t info;
-	long ret, timeout;
+	long ret;
 
 	if (sigsetsize != sizeof(sigset_t))
 		return -EINVAL;
@@ -901,45 +900,19 @@ compat_sys_rt_sigtimedwait (compat_sigse
 	if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
 		return -EFAULT;
 	sigset_from_compat(&s, &s32);
-	sigdelsetmask(&s,sigmask(SIGKILL)|sigmask(SIGSTOP));
-	signotset(&s);
 
-	timeout = MAX_SCHEDULE_TIMEOUT;
 	if (uts) {
-		if (get_compat_timespec (&t, uts))
+		if (get_compat_timespec(&t, uts))
 			return -EFAULT;
-		if (!timespec_valid(&t))
-			return -EINVAL;
-		timeout = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
 	}
 
-	spin_lock_irq(&current->sighand->siglock);
-	sig = dequeue_signal(current, &s, &info);
-	if (!sig && timeout) {
-		current->real_blocked = current->blocked;
-		sigandsets(&current->blocked, &current->blocked, &s);
-		recalc_sigpending();
-		spin_unlock_irq(&current->sighand->siglock);
-
-		timeout = schedule_timeout_interruptible(timeout);
+	ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
 
-		spin_lock_irq(&current->sighand->siglock);
-		sig = dequeue_signal(current, &s, &info);
-		current->blocked = current->real_blocked;
-		siginitset(&current->real_blocked, 0);
-		recalc_sigpending();
+	if (ret > 0 && uinfo) {
+		if (copy_siginfo_to_user32(uinfo, &info))
+			ret = -EFAULT;
 	}
-	spin_unlock_irq(&current->sighand->siglock);
 
-	if (sig) {
-		ret = sig;
-		if (uinfo) {
-			if (copy_siginfo_to_user32(uinfo, &info))
-				ret = -EFAULT;
-		}
-	} else {
-		ret = timeout?-EINTR:-EAGAIN;
-	}
 	return ret;
 
 }


  parent reply	other threads:[~2011-04-26 19:50 UTC|newest]

Thread overview: 88+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-18 13:44 [RFC PATCH v2 0/7] signal: sigprocmask fixes Oleg Nesterov
2011-04-18 13:44 ` [PATCH v2 1/7] signal: introduce retarget_shared_pending() Oleg Nesterov
2011-04-22 12:04   ` Matt Fleming
2011-04-25 10:49   ` Tejun Heo
2011-04-18 13:45 ` [PATCH v2 2/7] signal: retarget_shared_pending: consider shared/unblocked signals only Oleg Nesterov
2011-04-22 12:22   ` Matt Fleming
2011-04-25 10:52   ` Tejun Heo
2011-04-25 15:20     ` Oleg Nesterov
2011-04-25 16:19       ` Tejun Heo
2011-04-25 17:02         ` Oleg Nesterov
2011-04-25 17:11           ` Tejun Heo
2011-04-26 19:45             ` Oleg Nesterov
2011-04-28 15:26               ` [PATCHSET] signals-review branch Oleg Nesterov
2011-04-30 12:51                 ` Tejun Heo
2011-04-18 13:45 ` [PATCH v2 3/7] signal: retarget_shared_pending: optimize while_each_thread() loop Oleg Nesterov
2011-04-22 12:26   ` Matt Fleming
2011-04-25 11:03   ` Tejun Heo
2011-04-18 13:45 ` [PATCH v2 4/7] signal: sigprocmask: narrow the scope of ->siglock Oleg Nesterov
2011-04-22 12:31   ` Matt Fleming
2011-04-25 11:05   ` Tejun Heo
2011-04-18 13:45 ` [PATCH v2 5/7] signal: sigprocmask() should do retarget_shared_pending() Oleg Nesterov
2011-04-22 12:46   ` Matt Fleming
2011-04-25 11:14   ` Tejun Heo
2011-04-18 13:46 ` [PATCH v2 6/7] x86: signal: handle_signal() should use set_current_blocked() Oleg Nesterov
2011-04-22 13:45   ` Matt Fleming
2011-04-25 11:19   ` Tejun Heo
2011-04-18 13:46 ` [PATCH v2 7/7] x86: signal: sys_rt_sigreturn() " Oleg Nesterov
2011-04-22 14:14   ` Matt Fleming
2011-04-23 18:12     ` Oleg Nesterov
2011-04-25 11:21   ` Tejun Heo
2011-04-18 13:47 ` [PATCH v2 8/7] signal: cleanup sys_rt_sigprocmask() Oleg Nesterov
2011-04-22 14:30   ` Matt Fleming
2011-04-23 18:20     ` Oleg Nesterov
2011-04-23 18:47       ` Matt Fleming
2011-04-25 11:26   ` Tejun Heo
2011-04-18 17:16 ` [RFC PATCH v2 0/7] signal: sigprocmask fixes Linus Torvalds
2011-04-18 17:32   ` Oleg Nesterov
2011-04-18 17:40     ` Linus Torvalds
2011-04-23 17:59       ` [PATCH 0/3] do_sigtimedwait() needs retarget_shared_pending() Oleg Nesterov
2011-04-23 17:59         ` [PATCH 1/3] signal: sys_rt_sigtimedwait: simplify the timeout logic Oleg Nesterov
2011-04-25 11:37           ` Tejun Heo
2011-04-25 17:26             ` Oleg Nesterov
2011-04-25 17:34               ` Linus Torvalds
2011-04-25 17:56                 ` Oleg Nesterov
2011-04-25 19:38                   ` Linus Torvalds
2011-04-26 10:18           ` Matt Fleming
2011-04-23 17:59         ` [PATCH 2/3] signal: introduce do_sigtimedwait() to factor out compat/native code Oleg Nesterov
2011-04-25 11:39           ` Tejun Heo
2011-04-25 11:49           ` Tejun Heo
2011-04-25 15:33             ` Oleg Nesterov
2011-04-25 16:25               ` Tejun Heo
2011-04-26 10:28           ` Matt Fleming
2011-04-23 18:00         ` [PATCH 3/3] signal: do_sigtimedwait() needs retarget_shared_pending() Oleg Nesterov
2011-04-25 11:52           ` Tejun Heo
2011-04-25 16:01             ` Oleg Nesterov
2011-04-25 16:27               ` Tejun Heo
2011-04-25 17:07                 ` Oleg Nesterov
2011-04-25 17:12                   ` Tejun Heo
2011-04-26 10:40                   ` Matt Fleming
2011-04-26 10:42           ` Matt Fleming
2011-04-26 19:48         ` [PATCH v2 0/6] sigtimedwait/sigprocmask need retarget_shared_pending() Oleg Nesterov
2011-04-26 19:48           ` [PATCH v2 1/6] signal: sys_rt_sigtimedwait: simplify the timeout logic Oleg Nesterov
2011-04-26 19:49           ` Oleg Nesterov [this message]
2011-04-27 10:09             ` [PATCH v2 2/6] signal: introduce do_sigtimedwait() to factor out compat/native code Tejun Heo
2011-04-27 21:24             ` Matt Fleming
2011-05-11 16:21             ` Mike Frysinger
2011-05-12 18:54               ` Oleg Nesterov
2011-05-13 16:44               ` [PATCH] signal: trivial, fix the "timespec declared inside parameter list" warning Oleg Nesterov
2011-05-13 18:09                 ` Mike Frysinger
2011-05-16 12:57                   ` Oleg Nesterov
2011-05-16 12:57                     ` [PATCH v2] " Oleg Nesterov
2011-05-16 17:39                       ` Mike Frysinger
2011-05-18 23:37                       ` Andrew Morton
2011-05-19 18:19                         ` Oleg Nesterov
2011-05-19 19:21                           ` Mike Frysinger
2011-04-26 19:49           ` [PATCH v2 3/6] signal: do_sigtimedwait() needs retarget_shared_pending() Oleg Nesterov
2011-04-26 19:49           ` [PATCH v2 4/6] signal: cleanup sys_sigprocmask() Oleg Nesterov
2011-04-27 10:12             ` Tejun Heo
2011-04-27 21:31             ` Matt Fleming
2011-04-26 19:50           ` [PATCH v2 5/6] signal: sys_sigprocmask() needs retarget_shared_pending() Oleg Nesterov
2011-04-26 21:43             ` Linus Torvalds
2011-04-27 12:57               ` Oleg Nesterov
2011-04-27 13:04                 ` Tejun Heo
2011-05-01 20:07               ` [PATCH v2 0/1] " Oleg Nesterov
2011-05-01 20:08                 ` [PATCH v2 1/1] " Oleg Nesterov
2011-04-26 19:50           ` [PATCH v2 6/6] signal: rename signandsets() to sigandnsets() Oleg Nesterov
2011-04-27 10:11             ` Tejun Heo
2011-04-27 21:43             ` Matt Fleming

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110426194904.GC8520@redhat.com \
    --to=oleg@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matt@console-pimps.org \
    --cc=nyoushchenko@mvista.com \
    --cc=tj@kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.