Skip to contentSkip to conversation

signalfd: make it group-wide, fix posix-timers scheduling

PATCH In review Oleg Nesterov 1 participant 3 messages Aug 12, 2007 – Aug 14, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 4/4 signalfd: make it group-wide, fix posix-timers scheduling

Untested, user visible change.

With this patch any thread can dequeue its own private signals via signalfd, even if it was created by another sub-thread.

To do so, we pass "current" to dequeue_signal() if the caller is from the same thread group. This also fixes the scheduling of posix timers broken by the previous patch.

If the caller doesn't belong to this thread group, we can't handle __SI_TIMER case properly anyway. Perhaps we should forbid the cross-process signalfd usage and convert ctx->tsk to ctx->sighand.

fs/signalfd.c renamed from fs/signalfd.c~4_GROUP +12 -3
@@ -209,6 +209,15 @@ static int signalfd_copyinfo(struct sign
 	return err ? -EFAULT: sizeof(*uinfo);
 }
 
+static int
+__signalfd_dequeue(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
+{
+	if (tsk->tgid == current->tgid)
+		tsk = current;
+
+	return dequeue_signal(tsk, mask, info);
+}
+
 static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
 				int nonblock)
 {
@@ -219,7 +228,7 @@ static ssize_t signalfd_dequeue(struct s
 	if (!signalfd_lock(ctx, &lk))
 		return 0;
 
-	ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
+	ret = __signalfd_dequeue(lk.tsk, &ctx->sigmask, info);
 	switch (ret) {
 	case 0:
 		if (!nonblock)
@@ -233,7 +242,7 @@ static ssize_t signalfd_dequeue(struct s
 	add_wait_queue(&ctx->wqh, &wait);
 	for (;;) {
 		set_current_state(TASK_INTERRUPTIBLE);
-		ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
+		ret = __signalfd_dequeue(lk.tsk, &ctx->sigmask, info);
 		signalfd_unlock(&lk);
 		if (ret != 0)
 			break;
@@ -331,7 +340,7 @@ asmlinkage long sys_signalfd(int ufd, si
 
 		init_waitqueue_head(&ctx->wqh);
 		ctx->sigmask = sigmask;
-		ctx->tsk = current;
+		ctx->tsk = current->group_leader;
 
 		sighand = current->sighand;
 		/*
#1 lore raw
Quoted 6 lines · Oleg Nesterov

Untested, user visible change.

With this patch any thread can dequeue its own private signals via signalfd, even if it was created by another sub-thread.

Quoted 7 lines

To do so, we pass "current" to dequeue_signal() if the caller is from the same thread group. This also fixes the scheduling of posix timers broken by the previous patch.

If the caller doesn't belong to this thread group, we can't handle __SI_TIMER case properly anyway. Perhaps we should forbid the cross-process signalfd usage and convert ctx->tsk to ctx->sighand.

Sorry, the patch is not complete. I forgot about signalfd_poll() and somehow I forgot to actually tweak de_thread(). I'll send a new one tomorrow.

Something should be done for 2.6.23, imho.

Oleg.

#2 lore raw
Quoted 5 lines · Oleg Nesterov

Sorry, the patch is not complete. I forgot about signalfd_poll() and somehow I forgot to actually tweak de_thread(). I'll send a new one tomorrow.

Something should be done for 2.6.23, imho.

Slightly tested. This all needs furher cleanups (for example, __exit_signal()->signalfd_detach_locked() is not needed unless we are group_leader), but this code be done later.

[PATCH 4/4] signalfd: make it group-wide, fix posix-timers scheduling

With this patch any thread can dequeue its own private signals via signalfd, even if it was created by another sub-thread.

To do so, we use "current" instead of ctx->tsk if the caller is from the same thread group. This also fixes the scheduling of posix timers broken by the previous patch.

If the caller doesn't belong to this thread group, we can't handle __SI_TIMER case properly anyway. Perhaps we should forbid the cross-process signalfd usage and convert ctx->tsk to ctx->sighand.

fs/signalfd.c renamed from fs/signalfd.c~4_GROUP +10 -4
@@ -56,12 +56,18 @@ static int signalfd_lock(struct signalfd
 		sighand = lock_task_sighand(lk->tsk, &lk->flags);
 	rcu_read_unlock();
 
-	if (sighand && !ctx->tsk) {
+	if (!sighand)
+		return 0;
+
+	if (!ctx->tsk) {
 		unlock_task_sighand(lk->tsk, &lk->flags);
-		sighand = NULL;
+		return 0;
 	}
 
-	return sighand != NULL;
+	if (lk->tsk->tgid == current->tgid)
+		lk->tsk = current;
+
+	return 1;
 }
 
 static void signalfd_unlock(struct signalfd_lockctx *lk)
@@ -331,7 +337,7 @@ asmlinkage long sys_signalfd(int ufd, si
 
 		init_waitqueue_head(&ctx->wqh);
 		ctx->sigmask = sigmask;
-		ctx->tsk = current;
+		ctx->tsk = current->group_leader;
 
 		sighand = current->sighand;
 		/*
fs/exec.c renamed from fs/exec.c~4_GROUP +2 -7
@@ -780,18 +780,12 @@ static int de_thread(struct task_struct 
 	int count;
 
 	/*
-	 * Tell all the sighand listeners that this sighand has
-	 * been detached. The signalfd_detach() function grabs the
-	 * sighand lock, if signal listeners are present on the sighand.
-	 */
-	signalfd_detach(tsk);
-
-	/*
 	 * If we don't share sighandlers, then we aren't sharing anything
 	 * and we can just re-use it all.
 	 */
 	if (atomic_read(&oldsighand->count) <= 1) {
 		BUG_ON(atomic_read(&sig->count) != 1);
+		signalfd_detach(tsk);
 		exit_itimers(sig);
 		return 0;
 	}
@@ -930,6 +924,7 @@ static int de_thread(struct task_struct 
 	sig->flags = 0;
 
 no_thread_group:
+	signalfd_detach(tsk);
 	exit_itimers(sig);
 	if (leader)
 		release_task(leader);