Skip to contentSkip to conversation

+ signalfd-retrieve-multiple-signals-with-one-read-call.patch added to -mm tree

DISCUSSION Oleg Nesterov 3 participants 3 messages May 21, 2007 VFS

lore
Share & tools
lore
#0 lore raw

A couple of very minor nits,

Quoted 24 lines
(quoted diff) +24 -0
+static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
+				int nonblock)
+{
+	int locked;
+	ssize_t ret;
+	struct signalfd_lockctx lk;
+	DECLARE_WAITQUEUE(wait, current);
+
+	locked = signalfd_lock(ctx, &lk);
+	if (!locked)
+		return 0;
+
+	ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
+	switch (ret) {
+	case 0:
+		if (!nonblock)
+			break;
+		ret = -EAGAIN;
+	default:
+		signalfd_unlock(&lk);
+		return ret;
+	}
+
+	add_wait_queue(&ctx->wqh, &wait);

Review

Quoted 10 lines
(quoted diff) +10 -0
+	for (;;) {
+		set_current_state(TASK_INTERRUPTIBLE);
+		ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
+		if (ret != 0)
+			break;
+		if (signal_pending(current)) {
+			ret = -ERESTARTSYS;
+			break;
+		}
+		signalfd_unlock(&lk);

The locking looks a bit overcomplicated. We don't need signalfd_lock() to check ret != 0 or signal_pending(), we can drop it earlier. This way we always leave the loop in "unlocked" state.

Review

Quoted 10 lines
(quoted diff) +10 -0
+		schedule();
+		locked = signalfd_lock(ctx, &lk);
+		if (unlikely(!locked)) {
+			/*
+			 * Let the caller read zero byte, ala socket
+			 * recv() when the peer disconnect. This test
+			 * must be done before doing a dequeue_signal(),
+			 * because if the sighand has been orphaned,
+			 * the dequeue_signal() call is going to crash.
+			 */

Imho, the comment is a bit confusing. dequeue_signal() needs ->siglock even if signalfd_ctx is not orphaned.

Review

Quoted 13 lines
(quoted diff) +13 -0
+			 ret = 0;
+			 break;
+		}
+	}
+
+	remove_wait_queue(&ctx->wqh, &wait);
+	__set_current_state(TASK_RUNNING);
+
+	if (likely(locked))
+		signalfd_unlock(&lk);
+
+	return ret;
+}

IOW, how about this?

	static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
					int nonblock)
	{
		ssize_t ret;
		struct signalfd_lockctx lk;
		DECLARE_WAITQUEUE(wait, current);

		if (!signalfd_lock(ctx, &lk))
			return 0;

		ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
		switch (ret) {
		case 0:
			if (!nonblock)
				break;
			ret = -EAGAIN;
		default:
			signalfd_unlock(&lk);
			return ret;
		}

		add_wait_queue(&ctx->wqh, &wait);
		for (;;) {
			set_current_state(TASK_INTERRUPTIBLE);
			ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
			signalfd_unlock(&lk);

			if (ret != 0)
				break;

			ret = -ERESTARTSYS;
			if (signal_pending(current))
				break;

			schedule();

			ret = 0;
			if (!signalfd_lock(ctx, &lk))
				break;
		}

		remove_wait_queue(&ctx->wqh, &wait);
		__set_current_state(TASK_RUNNING);

		return ret;
	}

Oleg.

#1 lore raw
Quoted 5 lines · Oleg Nesterov
Quoted 5 lines
(quoted diff) +5 -0
+		schedule();
+		locked = signalfd_lock(ctx, &lk);
+		if (unlikely(!locked)) {
+			/*
+			 * Let the caller read zero byte, ala socket
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+			 * recv() when the peer disconnect. This test
+			 * must be done before doing a dequeue_signal(),
+			 * because if the sighand has been orphaned,
+			 * the dequeue_signal() call is going to crash.
+			 */

Imho, the comment is a bit confusing. dequeue_signal() needs ->siglock even if signalfd_ctx is not orphaned.

The comment looks clear to me. It states:

1) The policy of returning 0 when the sighand has been detached

2) That we _must_not_ call dequeue_signal() in case signalfd_lock() fails

#ACK on the code mod below.

- Davide

#2 lore raw
Quoted 17 lines · Davide Libenzi
Quoted 4 lines · Oleg Nesterov
Quoted 4 lines
(quoted diff) +4 -0
+		schedule();
+		locked = signalfd_lock(ctx, &lk);
+		if (unlikely(!locked)) {
+			/*
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+			 * Let the caller read zero byte, ala socket
+			 * recv() when the peer disconnect. This test
+			 * must be done before doing a dequeue_signal(),
+			 * because if the sighand has been orphaned,
+			 * the dequeue_signal() call is going to crash.
+			 */

Imho, the comment is a bit confusing. dequeue_signal() needs ->siglock even if signalfd_ctx is not orphaned.

The comment looks clear to me. It states:

Quoted 8 lines

1) The policy of returning 0 when the sighand has been detached

2) That we _must_not_ call dequeue_signal() in case signalfd_lock() fails

#ACK on the code mod below.

- Davide

Andrew, please apply on top.

Simplify signalfd locking following suggestions by Oleg Nesterov.

fs/signalfd.c +6 -11
Index: linux-2.6/fs/signalfd.c
===================================================================
@@ -211,13 +211,11 @@ static int signalfd_copyinfo(struct sign
 static ssize_t signalfd_dequeue(struct signalfd_ctx *ctx, siginfo_t *info,
 				int nonblock)
 {
-	int locked;
 	ssize_t ret;
 	struct signalfd_lockctx lk;
 	DECLARE_WAITQUEUE(wait, current);
 
-	locked = signalfd_lock(ctx, &lk);
-	if (!locked)
+	if (!signalfd_lock(ctx, &lk))
 		return 0;
 
 	ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
@@ -235,24 +233,24 @@ static ssize_t signalfd_dequeue(struct s
 	for (;;) {
 		set_current_state(TASK_INTERRUPTIBLE);
 		ret = dequeue_signal(lk.tsk, &ctx->sigmask, info);
+		signalfd_unlock(&lk);
 		if (ret != 0)
 			break;
 		if (signal_pending(current)) {
 			ret = -ERESTARTSYS;
 			break;
 		}
-		signalfd_unlock(&lk);
 		schedule();
-		locked = signalfd_lock(ctx, &lk);
-		if (unlikely(!locked)) {
+		ret = signalfd_lock(ctx, &lk);
+		if (unlikely(!ret)) {
 			/*
 			 * Let the caller read zero byte, ala socket
 			 * recv() when the peer disconnect. This test
 			 * must be done before doing a dequeue_signal(),
 			 * because if the sighand has been orphaned,
-			 * the dequeue_signal() call is going to crash.
+			 * the dequeue_signal() call is going to crash
+			 * because ->sighand will be long gone.
 			 */
-			 ret = 0;
 			 break;
 		}
 	}
@@ -260,9 +258,6 @@ static ssize_t signalfd_dequeue(struct s
 	remove_wait_queue(&ctx->wqh, &wait);
 	__set_current_state(TASK_RUNNING);
 
-	if (likely(locked))
-		signalfd_unlock(&lk);
-
 	return ret;
 }