Skip to contentSkip to conversation

signalfd v2 - signalfd core ...

PATCH Oleg Nesterov 3 participants 8 messages Mar 8, 2007 – Mar 9, 2007

lore
Share & tools
lore
#0 lore raw
Quoted 10 lines · Davide Libenzi
(quoted diff) +9 -0
+int signalfd_deliver(struct sighand_struct *sighand, int sig, struct siginfo *info)
+{
+	int nsig = 0;
+	struct list_head *pos;
+	struct signalfd_ctx *ctx;
+	struct signalfd_sq *sq;
+
+	list_for_each(pos, &sighand->sfdlist) {
+		ctx = list_entry(pos, struct signalfd_ctx, lnk);

Review

Quoted 10 lines
(quoted diff) +10 -0
+		/*
+		 * We use a negative signal value as a way to broadcast that the
+		 * sighand has been orphaned, so that we can notify all the
+		 * listeners about this.
+		 */
+		if (sig < 0)
+			__wake_up_locked(&ctx->wqh, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE);
+		else if (sigismember(&ctx->sigmask, sig) &&
+			 (sig >= SIGRTMIN || !sigismember(&ctx->pending, sig))) {
+			sigaddset(&ctx->pending, sig);

I don't understand the "(sig >= SIGRTMIN || !sigismember(&ctx->pending, sig))" check. This mimics the LEGACY_QUEUE() check, but seems strange. The signal may be pending in ctx->pending just because it was not signalfd_fetchsig()ed, yes?

Please also see below.

Quoted 10 lines
(quoted diff) +7 -0
+asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemask)
+{
 
[...snip...]
 
+	} else {
+		error = -EBADF;
+		file = fget(ufd);
+		if (!file)
+			goto err_exit;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		ctx = file->private_data;
+		error = -EINVAL;
+		if (file->f_op != &signalfd_fops) {
+			fput(file);
+			goto err_exit;
+		}
+		spin_lock_irq(&ctx->sighand->siglock);
+		ctx->sigmask = sigmask;
+		spin_unlock_irq(&ctx->sighand->siglock);
+		wake_up(&ctx->wqh);

Can't this race with sys_signalfd_dequeue() which use lockless __add_wait_queue()? Looks like we should do __wake_up_locked() under ctx->sighand->siglock.

Quoted 19 lines
kernel/signal.c +5 -0
 
[...snip...]
@@ -780,6 +785,11 @@
 	BUG_ON(!irqs_disabled());
 	assert_spin_locked(&t->sighand->siglock);
 
+	/*
+	 * Deliver the signal to listening signalfds ...
+	 */
+	signalfd_notify(t->sighand, sig, info);
+
 	/* Short-circuit ignored signals.  */
 	if (sig_ignored(t, sig))
 		goto out;
@@ -968,6 +978,11 @@
 	assert_spin_locked(&p->sighand->siglock);

kernel/signal.c line 988

Quoted 10 lines
kernel/signal.c +5 -0
 	handle_stop_signal(sig, p);
 
+	/*
+	 * Deliver the signal to listening signalfds ...
+	 */
+	signalfd_notify(p->sighand, sig, info);
+
 	/* Short-circuit ignored signals.  */
 	if (sig_ignored(p, sig))
 		return ret;

It is strange that we are doing signalfd_notify() even if the signal is ignored. Isn't it better to shift signalfd_notify() into send_signal() ? This way we do not need the special check in signalfd_deliver() above.

Also, this patch doesn't take send_sigqueue/send_group_sigqueue into account.

Oleg.

#1 lore raw
Quoted 17 lines · Oleg Nesterov
Quoted 16 lines · Davide Libenzi
(quoted diff) +15 -0
+int signalfd_deliver(struct sighand_struct *sighand, int sig, struct siginfo *info)
+{
+	int nsig = 0;
+	struct list_head *pos;
+	struct signalfd_ctx *ctx;
+	struct signalfd_sq *sq;
+
+	list_for_each(pos, &sighand->sfdlist) {
+		ctx = list_entry(pos, struct signalfd_ctx, lnk);
+		/*
+		 * We use a negative signal value as a way to broadcast that the
+		 * sighand has been orphaned, so that we can notify all the
+		 * listeners about this.
+		 */
+		if (sig < 0)
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+			__wake_up_locked(&ctx->wqh, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE);
+		else if (sigismember(&ctx->sigmask, sig) &&
+			 (sig >= SIGRTMIN || !sigismember(&ctx->pending, sig))) {
+			sigaddset(&ctx->pending, sig);

I don't understand the "(sig >= SIGRTMIN || !sigismember(&ctx->pending, sig))" check. This mimics the LEGACY_QUEUE() check, but seems strange. The signal may be pending in ctx->pending just because it was not signalfd_fetchsig()ed, yes?

Logic is, if it's not an RT signal, queue only one, otherwise multiple. The bit on the ->pending mask is clealer only when the queue slot becomes empty.

Quoted 17 lines

Please also see below.

Quoted 15 lines
(quoted diff) +12 -0
+asmlinkage long sys_signalfd(int ufd, sigset_t __user *user_mask, size_t sizemask)
+{
 
[...snip...]
 
+	} else {
+		error = -EBADF;
+		file = fget(ufd);
+		if (!file)
+			goto err_exit;
+		ctx = file->private_data;
+		error = -EINVAL;
+		if (file->f_op != &signalfd_fops) {
+			fput(file);
+			goto err_exit;
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+		}
+		spin_lock_irq(&ctx->sighand->siglock);
+		ctx->sigmask = sigmask;
+		spin_unlock_irq(&ctx->sighand->siglock);
+		wake_up(&ctx->wqh);

Can't this race with sys_signalfd_dequeue() which use lockless __add_wait_queue()? Looks like we should do __wake_up_locked() under ctx->sighand->siglock.

Yes, good catch. Fixed.

Quoted 21 lines
Quoted 21 lines
kernel/signal.c +5 -0
 
[...snip...]
@@ -780,6 +785,11 @@
 	BUG_ON(!irqs_disabled());
 	assert_spin_locked(&t->sighand->siglock);
 
+	/*
+	 * Deliver the signal to listening signalfds ...
+	 */
+	signalfd_notify(t->sighand, sig, info);
+
 	/* Short-circuit ignored signals.  */
 	if (sig_ignored(t, sig))
 		goto out;
@@ -968,6 +978,11 @@
 	assert_spin_locked(&p->sighand->siglock);
 	handle_stop_signal(sig, p);
 
Quoted 12 lines

kernel/signal.c line 988

Quoted 8 lines
kernel/signal.c +5 -0
+	/*
+	 * Deliver the signal to listening signalfds ...
+	 */
+	signalfd_notify(p->sighand, sig, info);
+
 	/* Short-circuit ignored signals.  */
 	if (sig_ignored(p, sig))
 		return ret;

It is strange that we are doing signalfd_notify() even if the signal is ignored. Isn't it better to shift signalfd_notify() into send_signal() ? This way we do not need the special check in signalfd_deliver() above.

The two trasports can rely on different masks. The signalfd_notify() does not even go in signalfd_deliver() if no signalfds are attached to the sighand.

Quoted 1 line

Also, this patch doesn't take send_sigqueue/send_group_sigqueue into account.

I added that too. but I noticed something strange, dunno if intentional or not. In send_sigqueue and send_group_sigqueue, the check for the timer-special and the ignored is inverted. This lead to two different behaviours. Is there a reason for that?

- Davide

#2 lore raw
Quoted 29 lines · Davide Libenzi
Quoted 17 lines · Oleg Nesterov
Quoted 16 lines · Davide Libenzi
(quoted diff) +15 -0
+int signalfd_deliver(struct sighand_struct *sighand, int sig, struct siginfo *info)
+{
+	int nsig = 0;
+	struct list_head *pos;
+	struct signalfd_ctx *ctx;
+	struct signalfd_sq *sq;
+
+	list_for_each(pos, &sighand->sfdlist) {
+		ctx = list_entry(pos, struct signalfd_ctx, lnk);
+		/*
+		 * We use a negative signal value as a way to broadcast that the
+		 * sighand has been orphaned, so that we can notify all the
+		 * listeners about this.
+		 */
+		if (sig < 0)
Quoted 8 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+			__wake_up_locked(&ctx->wqh, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE);
+		else if (sigismember(&ctx->sigmask, sig) &&
+			 (sig >= SIGRTMIN || !sigismember(&ctx->pending, sig))) {
+			sigaddset(&ctx->pending, sig);

I don't understand the "(sig >= SIGRTMIN || !sigismember(&ctx->pending, sig))" check. This mimics the LEGACY_QUEUE() check, but seems strange. The signal may be pending in ctx->pending just because it was not signalfd_fetchsig()ed, yes?

Quoted 2 lines

Logic is, if it's not an RT signal, queue only one, otherwise multiple. The bit on the ->pending mask is clealer only when the queue slot becomes empty.

Yes, I see what the code does, but I don't undestand why. For example, SIGCHLD was delivered to the process _and_ handled several times, then sys_signalfd_dequeue() comes and finds only one siginfo. Isn't this strange?

Quoted 29 lines
Quoted 16 lines
Quoted 16 lines
(quoted diff) +5 -0
@@ -780,6 +785,11 @@
 	BUG_ON(!irqs_disabled());
 	assert_spin_locked(&t->sighand->siglock);
 
+	/*
+	 * Deliver the signal to listening signalfds ...
+	 */
+	signalfd_notify(t->sighand, sig, info);
+
 	/* Short-circuit ignored signals.  */
 	if (sig_ignored(t, sig))
 		goto out;
@@ -968,6 +978,11 @@
 	assert_spin_locked(&p->sighand->siglock);
 	handle_stop_signal(sig, p);
 
Quoted 12 lines

Review line 988

Quoted 8 lines
(quoted diff) +5 -0
+	/*
+	 * Deliver the signal to listening signalfds ...
+	 */
+	signalfd_notify(p->sighand, sig, info);
+
 	/* Short-circuit ignored signals.  */
 	if (sig_ignored(p, sig))
 		return ret;

It is strange that we are doing signalfd_notify() even if the signal is ignored. Isn't it better to shift signalfd_notify() into send_signal() ? This way we do not need the special check in signalfd_deliver() above.

Quoted 3 lines

The two trasports can rely on different masks. The signalfd_notify() does not even go in signalfd_deliver() if no signalfds are attached to the sighand.

Sorry, I don't understand. The masks are different, yes, but ->sighand is the same? How this can make any difference for "if no signalfds are attached" ?

OK. What is the purpose of signalfd? Should it record the signals which were sent to the process, or only those which were delivered? The latter looks more natural for me. But inn any case, I don't see a reason to check ->pending mask in signalfd_deliver().

BTW, sys_signalfd_dequeue() re-queues signalfd_sq if copy_siginfo_to_user() fails. Why not -EFAULT?

Also. A malicious user can eat all memory, signalfd_deliver()->kmem_cache_alloc() doesn't check any limits.

Quoted 6 lines
Quoted 1 line

Also, this patch doesn't take send_sigqueue/send_group_sigqueue into account.

I added that too. but I noticed something strange, dunno if intentional or not. In send_sigqueue and send_group_sigqueue, the check for the timer-special and the ignored is inverted. This lead to two different behaviours. Is there a reason for that?

I was wondering about that too.

Oleg.

#3 lore raw
Quoted 3 lines · Oleg Nesterov

Also. A malicious user can eat all memory, signalfd_deliver()->kmem_cache_alloc() doesn't check any limits.

This, btw, is one reason I *really* think signalfd() should just use the same old signal queue, and not try to make its own.

Signal queueing and unqueuing simply isn't that simple.

Linus

#4 lore raw
Quoted 11 lines · Linus Torvalds
Quoted 3 lines · Oleg Nesterov

Also. A malicious user can eat all memory, signalfd_deliver()->kmem_cache_alloc() doesn't check any limits.

This, btw, is one reason I *really* think signalfd() should just use the same old signal queue, and not try to make its own.

Signal queueing and unqueuing simply isn't that simple.

Alrighty. Back to the dequeue_signal ...

- Davide

#5 lore raw
In reply to Oleg Nesterov
Quoted 6 lines · Oleg Nesterov
Quoted 2 lines

Logic is, if it's not an RT signal, queue only one, otherwise multiple. The bit on the ->pending mask is clealer only when the queue slot becomes empty.

Yes, I see what the code does, but I don't undestand why. For example, SIGCHLD was delivered to the process _and_ handled several times, then sys_signalfd_dequeue() comes and finds only one siginfo. Isn't this strange?

That's the same logic the kernel folows for non-RT signals.

Quoted 6 lines
Quoted 3 lines

The two trasports can rely on different masks. The signalfd_notify() does not even go in signalfd_deliver() if no signalfds are attached to the sighand.

Sorry, I don't understand. The masks are different, yes, but ->sighand is the same? How this can make any difference for "if no signalfds are attached" ?

The list_empty() che would not make you fall inside signalfd_deliver(), hence the fast path really lives up to its name ;)

Quoted 2 lines

Also. A malicious user can eat all memory, signalfd_deliver()->kmem_cache_alloc() doesn't check any limits.

I'll make that use the std dequeu_signal, so everything is handled in there.

- Davide

#6 lore raw
Quoted 11 lines · Davide Libenzi
Quoted 6 lines · Oleg Nesterov
Quoted 2 lines

Logic is, if it's not an RT signal, queue only one, otherwise multiple. The bit on the ->pending mask is clealer only when the queue slot becomes empty.

Yes, I see what the code does, but I don't undestand why. For example, SIGCHLD was delivered to the process _and_ handled several times, then sys_signalfd_dequeue() comes and finds only one siginfo. Isn't this strange?

That's the same logic the kernel folows for non-RT signals.

Yes, specific_send_sig_info/__group_send_sig_info "ignores" the non-RT signal

- if we already have a pending one (blocked, or not yet handled)

- the only reason is backward compatibility

signalfd:

- if the same signr was not fetched via sys_signalfd_dequeue()

- the reason is ????

Suppose we are doing a simple application which just logs all signals which were sent to this process. Now, we can miss the signal if the logging blocks.

Think about sub-threads. We can send the same non-RT signal to T1, T2, T3 via specific_send_sig_info(). All 3 signals will be delivered, but signalfd (which is process wide) will see only the first.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

The two trasports can rely on different masks. The signalfd_notify() does not even go in signalfd_deliver() if no signalfds are attached to the sighand.

Sorry, I don't understand. The masks are different, yes, but ->sighand is the same? How this can make any difference for "if no signalfds are attached" ?

The list_empty() che would not make you fall inside signalfd_deliver(), hence the fast path really lives up to its name ;)

Ugh. Still can't understand, probably I missed something or misread this patch.

If we shift signalfd_notify() from specific_send_sig_info/__group_send_sig_info to send_signal(), we have the same "list_empty()" fastpath if no signalfds are attached to the sighand. The difference is that we don't count sig_ignored() signals, which looks right to me.

Oleg.

#7 lore raw
Quoted 6 lines · Oleg Nesterov

Ugh. Still can't understand, probably I missed something or misread this patch.

If we shift signalfd_notify() from specific_send_sig_info/__group_send_sig_info to send_signal(), we have the same "list_empty()" fastpath if no signalfds are attached to the sighand. The difference is that we don't count sig_ignored() signals, which looks right to me.

Now I shifted the check into send_signal(), since signalfd now uses the standard dequeue_signal, and hence compete with standard signal delivery against the queue. This was initial Linus design.

- Davide