All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: [BUG] signal: multithread program returns with wrong errno on receiving SIGSTOP
@ 2007-05-28  7:07 Oleg Nesterov
  2007-05-28  9:58 ` Satoru Takeuchi
  2007-05-29  7:33 ` Roland McGrath
  0 siblings, 2 replies; 10+ messages in thread
From: Oleg Nesterov @ 2007-05-28  7:07 UTC (permalink / raw)
  To: Satoru Takeuchi
  Cc: Alan Cox, Andrew Morton, Eric W. Biederman, Ingo Molnar,
	Roland McGrath, linux-kernel

Satoru Takeuchi wrote:
>
> I found a bug on signal subsystem. If there is some multithread program
> and one of the thread is blocking on the system call, it returns with
> wrong errno on receiving SIGSTOP and following SIGCONT.

It doesn't matter in this particular case, but in general it is good to
report the kernel version as well.

> 1. issue the test program (attached on this mail) from term A.
>
>    $ ./mt-wrong-errno
>
> 2. send SIGSTOP to test program from term B.
>
>    $ ps a
>    ...
>     9685 pts/3    Sl+    0:00 ./mt-wrong-errno
>     9688 pts/4    R+     0:00 ps a
>    $ kill -STOP 9685
>
> 3. send SIGCONT to test program from term B.
>
>    $ kill -CONT 9685
>
> Expected Result
> ===============
>
> `./mt-wrong-errno' restarts read syscall (and stop again by receiving
> SIGTTIN).
>
> Actual Result
> =============
>
> `./mt-wrong-errno' returns with wrong errno 512 means ERESTARTSYS.
> The comment on include/linux/errno.h says "this errno should never be
> seen by user program." So it's definitely a bug.

Wild guess,

	drivers/char/n_tty.c:job_control()

		kill_pgrp(task_pgrp(current), SIGTTIN, 1);
		return -ERESTARTSYS;

This is wrong. kill_pgrp()->__group_send_sig_info() sends SIGTTIN
to the thread group, but can choose any sub-thread as a target for
signal_wake_up().

This means we return ERESTARTSYS, but signal_pending() != true, bug.

Satoru, could you re-test with the untested patch below?

Oleg.

--- t/drivers/char/n_tty.c~	2007-04-05 12:18:26.000000000 +0400
+++ t/drivers/char/n_tty.c	2007-05-28 10:57:58.000000000 +0400
@@ -1191,6 +1191,7 @@ static int job_control(struct tty_struct
 			    is_current_pgrp_orphaned())
 				return -EIO;
 			kill_pgrp(task_pgrp(current), SIGTTIN, 1);
+			set_thread_flag(TIF_SIGPENDING);
 			return -ERESTARTSYS;
 		}
 	}


^ permalink raw reply	[flat|nested] 10+ messages in thread
* [BUG] signal: multithread program returns with wrong errno on receiving SIGSTOP
@ 2007-05-28  2:32 Satoru Takeuchi
  2007-05-29  8:03 ` Andrew Morton
  0 siblings, 1 reply; 10+ messages in thread
From: Satoru Takeuchi @ 2007-05-28  2:32 UTC (permalink / raw)
  To: linux-kernel, Ingo Molnar

Hi,

I found a bug on signal subsystem. If there is some multithread program
and one of the thread is blocking on the system call, it returns with
wrong errno on receiving SIGSTOP and following SIGCONT.

Arch dependency
===============

succeed to reproduce: i386, ia64
Unknown:              any other arch

# I suspect that this problem occur on any arch...

How to reproduce
================

This process needs 2 terminals, term A and term B.

1. issue the test program (attached on this mail) from term A.

   $ ./mt-wrong-errno

2. send SIGSTOP to test program from term B.

   $ ps a
   ...
    9685 pts/3    Sl+    0:00 ./mt-wrong-errno
    9688 pts/4    R+     0:00 ps a
   $ kill -STOP 9685

3. send SIGCONT to test program from term B.

   $ kill -CONT 9685

Expected Result
===============

`./mt-wrong-errno' restarts read syscall (and stop again by receiving
SIGTTIN).

Actual Result
=============

`./mt-wrong-errno' returns with wrong errno 512 means ERESTARTSYS.
The comment on include/linux/errno.h says "this errno should never be
seen by user program." So it's definitely a bug.

  output of term A
  ----------------

    $ ./mt-wrong-errno

    [1]+  Stopped                 ./mt-wrong-errno
    $ errno = 512

  output of term B
  ----------------

    $ ps a
    ...
     9685 pts/3    Sl+    0:00 ./mt-wrong-errno
     9688 pts/4    R+     0:00 ps a
    $ kill -STOP 9685
    $ kill -CONT 9685


This problem can always reproduce, so it's no timing issue. I suspect
that group_stop_count counting mechanism has something wrong and
signal handling is canceled erroneously. 

Any hints or patch itself are welcome.

Thanks,

Satoru

-------------------------------------------------------------------------------
/*
 * mt-wrong-errno.c
 * 
 * Copyright 2007 Satoru Takeuchi <takeuchi_satoru@jp.futjisu.com>
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 * 
 */

#include <sys/types.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

void *thread_fn(void *arg)
{
	char c;
	int ret;

	ret = read(STDIN_FILENO, &c, 1);
	if (ret < 0)
		printf("errno = %d\n", errno);

	return NULL;
}

int main(int argc, char *argv[])
{
	pthread_t t;
	
	if (pthread_create(&t, NULL, thread_fn, NULL))
		err(EXIT_FAILURE, "pthread_create() failed\n");
	
	if (pthread_join(t, NULL))
		warn("pthread_join() failed\n");
}

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2007-05-29 19:28 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-28  7:07 [BUG] signal: multithread program returns with wrong errno on receiving SIGSTOP Oleg Nesterov
2007-05-28  9:58 ` Satoru Takeuchi
2007-05-28 10:13   ` Oleg Nesterov
2007-05-29  7:33 ` Roland McGrath
2007-05-29 19:22   ` Oleg Nesterov
2007-05-29 19:28     ` Roland McGrath
  -- strict thread matches above, loose matches on Subject: below --
2007-05-28  2:32 Satoru Takeuchi
2007-05-29  8:03 ` Andrew Morton
2007-05-29  8:04   ` Roland McGrath
2007-05-29  8:09   ` Satoru Takeuchi

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.