Issue suspending threads on mercury
Hi all,
I am experiencing a problem with xenomai-forge (recent "next" checkout) on Mercury (on top of a 3.12.9+CONFIG_PREEMPT_RT on x86) when suspending tasks/threads.
As far as I understand, the mercury implementation sends a signal to the thread to be suspended, with that threads signal handler setting the suspended state, i.e. the setting of __THREAD_S_SUSPENDED happens in notifier_callback, which seems to be different from the cobalt implementation.
The problem I am experiencing is that sometimes the signal handler never seems to get called in a thread that should be suspended, i.e. that thread suspension sometimes simply does not work.
The following example code (for the vxworks skin) reproduces the issue (it gets stuck in the loop waiting for the WIND_SUSPEND flag):
http://www.pastebin.ca/2667435
Any hints on how I can continue investigating this issue?
Thanks in advance for the help
regards,
Matthias
Quoted 17 lines · Matthias Schneider
Hi all,
I am experiencing a problem with xenomai-forge (recent "next" checkout) on Mercury (on top of a 3.12.9+CONFIG_PREEMPT_RT on x86) when suspending tasks/threads.
As far as I understand, the mercury implementation sends a signal to the thread to be suspended, with that threads signal handler setting the suspended state, i.e. the setting of
__THREAD_S_SUSPENDEDhappens innotifier_callback, which seems to be different from the cobalt implementation.The problem I am experiencing is that sometimes the signal handler never seems to get called in a thread that should be suspended, i.e. that thread suspension sometimes simply does not work.
The following example code (for the vxworks skin) reproduces the issue (it gets stuck in the loop waiting for theWIND_SUSPENDflag):
Quoted 3 lines
http://www.pastebin.ca/2667435
Any hints on how I can continue investigating this issue?
Have you tried checking taskSpawn and taskSuspend return values? To see if the calls are successful?
Signature · 1 line
Gilles.
Quoted 21 lines · Gilles Chanteperdrix
Quoted 6 lines · Matthias Schneider
Hi all,
I am experiencing a problem with xenomai-forge (recent "next"
checkout) on Mercury (on top of a 3.12.9+CONFIG_PREEMPT_RT on x86) when suspending
Quoted 13 lines
tasks/threads.
As far as I understand, the mercury implementation sends a signal to the thread to be suspended, with that threads signal handler setting the suspended state, i.e. the setting of
__THREAD_S_SUSPENDEDhappens innotifier_callback, which seems to be different from the cobalt implementation.The problem I am experiencing is that sometimes the signal handler never seems to get called in a thread that should be suspended, i.e. that thread suspension sometimes simply does not work.
The following example code (for the vxworks skin) reproduces the issue (it gets stuck in the loop waiting for theWIND_SUSPENDflag):
Quoted 11 lines
Quoted 3 lines
http://www.pastebin.ca/2667435
Any hints on how I can continue investigating this issue?
Have you tried checking taskSpawn and taskSuspend return values? To see if the calls are successful?
Signature · 1 line
Gilles.
Hi Gilles,
yes, the calls seem to return with "OK". I have put updated sample code at
http://www.pastebin.ca/2667459
The problem seems to happen quite often when attached with gdb (mostly during the first execution of the do .. while loop.
The thread not suspended stack looks like this:
#0 0xffffe424 in __kernel_vsyscall ()
#1 0xb7e8e8ee in clock_nanosleep (clock_id=-1208270244, flags=1, req=0xb69ff1f0, rem=0x0) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:47 #2 0xb7fa02cb in threadobj_sleep (ts=0xb69ff1f0) at threadobj.c:1096
#3 0xb7fcdf00 in taskDelay (ticks=1000) at taskLib.c:851
#4 0x0804878f in windTask (...) at task-1.c:15
[...]
while all the other suspended stacks look like
#0 0xffffe424 in __kernel_vsyscall ()
#1 0xb7f5272b in read () at ../sysdeps/unix/syscall-template.S:81
#2 0xb7fa679f in notifier_wait (nf=0x804a418) at notifier.c:279
#3 0xb7f9cf36 in notifier_callback (nf=0x804a418) at threadobj.c:430
#4 0xb7fa524d in notifier_sighandler (sig=42, siginfo=0xb7d89e0c, uc=0xb7d89e8c) at notifier.c:91 #5 <signal handler called>
#6 0xffffe424 in __kernel_vsyscall ()
#7 0xb7e8e8ee in clock_nanosleep (...) at clock_nanosleep.c:47
#8 0xb7fa02cb in threadobj_sleep (ts=0xb7d8a1f0) at threadobj.c:1096
#9 0xb7fcdf00 in taskDelay (ticks=1000) at taskLib.c:851
#10 0x0804878f in windTask (...) at task-1.c:15
, which is probably what I would suspect...
Matthias
2014-03-16 22:07 GMT+01:00 Matthias Schneider <[email protected]>:
Quoted 35 lines
Quoted 23 lines · Gilles Chanteperdrix
Quoted 6 lines · Matthias Schneider
Hi all,
I am experiencing a problem with xenomai-forge (recent "next"
checkout) on Mercury (on top of a 3.12.9+CONFIG_PREEMPT_RT on x86) when suspending
Quoted 15 lines
tasks/threads.
As far as I understand, the mercury implementation sends a signal to the thread to be suspended, with that threads signal handler setting the suspended state, i.e. the setting of
__THREAD_S_SUSPENDEDhappens innotifier_callback, which seems to be different from the cobalt implementation.The problem I am experiencing is that sometimes the signal handler never seems to get called in a thread that should be suspended, i.e. that thread suspension sometimes simply does not work.
The following example code (for the vxworks skin) reproduces the issue (it gets stuck in the loop waiting for theWIND_SUSPENDflag):Quoted 9 lines
Quoted 1 line
Any hints on how I can continue investigating this issue?
Have you tried checking taskSpawn and taskSuspend return values? To see if the calls are successful?
--
Gilles.Hi Gilles,
Quoted 7 lines
yes, the calls seem to return with "OK". I have put updated sample code at
http://www.pastebin.ca/2667459
The problem seems to happen quite often when attached with gdb (mostly during the first execution of the do .. while loop.
Not sure if this is anyhow related but I have seen problems with the signal handler when gdb is attached before. I noticed this when debugging this issue:
http://www.xenomai.org/pipermail/xenomai/2014-February/030117.html
It looked like the notifier_sighandler() often ran on the wrong thread when gdb was attached, causing it to fail at owner check and thus never doing the notifier callback. I only noticed this with gdb attached though.
Quoted 31 lines
The thread not suspended stack looks like this:
#0 0xffffe424 in __kernel_vsyscall () #1 0xb7e8e8ee in clock_nanosleep (clock_id=-1208270244, flags=1, req=0xb69ff1f0, rem=0x0) at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:47 #2 0xb7fa02cb in threadobj_sleep (ts=0xb69ff1f0) at threadobj.c:1096 #3 0xb7fcdf00 in taskDelay (ticks=1000) at taskLib.c:851 #4 0x0804878f in windTask (...) at task-1.c:15[...]
while all the other suspended stacks look like
#0 0xffffe424 in __kernel_vsyscall () #1 0xb7f5272b in read () at ../sysdeps/unix/syscall-template.S:81 #2 0xb7fa679f in notifier_wait (nf=0x804a418) at notifier.c:279 #3 0xb7f9cf36 in notifier_callback (nf=0x804a418) at threadobj.c:430 #4 0xb7fa524d in notifier_sighandler (sig=42, siginfo=0xb7d89e0c, uc=0xb7d89e8c) at notifier.c:91 #5 <signal handler called> #6 0xffffe424 in __kernel_vsyscall () #7 0xb7e8e8ee in clock_nanosleep (...) at clock_nanosleep.c:47 #8 0xb7fa02cb in threadobj_sleep (ts=0xb7d8a1f0) at threadobj.c:1096 #9 0xb7fcdf00 in taskDelay (ticks=1000) at taskLib.c:851#10
0x0804878fin windTask (...) at task-1.c:15, which is probably what I would suspect...
Matthias
_______________________________________________
Xenomai mailing list
[email protected]
http://www.xenomai.org/mailman/listinfo/xenomai
Quoted 11 lines · Matthias Schneider
Hi all,
I am experiencing a problem with xenomai-forge (recent "next" checkout) on Mercury (on top of a 3.12.9+CONFIG_PREEMPT_RT on x86) when suspending tasks/threads.
As far as I understand, the mercury implementation sends a signal to the thread to be suspended, with that threads signal handler setting the suspended state, i.e. the setting of
__THREAD_S_SUSPENDEDhappens innotifier_callback, which seems to be different from the cobalt implementation.
Quoted 7 lines
The problem I am experiencing is that sometimes the signal handler never seems to get called in a thread that should be suspended, i.e. that thread suspension sometimes simply does not work.
The following example code (for the vxworks skin) reproduces the issue (it gets stuck in the loop waiting for theWIND_SUSPENDflag):
Could you check this URL? I can't seem to find anything pasted there.
Quoted 3 lines
Any hints on how I can continue investigating this issue?
Running the test suite in lib/vxworks/testsuite may give us more information. sem-1, sem-2, task-2 and wd-1 are all using the suspend/resume mechanism.
Signature · 1 line
Philippe.
Quoted 26 lines · Philippe Gerum
Quoted 13 lines · Matthias Schneider
Hi all,
I am experiencing a problem with xenomai-forge (recent "next" checkout) on Mercury (on top of a 3.12.9+CONFIG_PREEMPT_RT on x86) when suspending tasks/threads.
As far as I understand, the mercury implementation sends a signal to the thread to be suspended, with that threads signal handler setting the suspended state, i.e. the setting of
__THREAD_S_SUSPENDEDhappens innotifier_callback, which seems to be different from the cobalt implementation.Quoted 7 lines
The problem I am experiencing is that sometimes the signal handler never seems to get called in a thread that should be suspended, i.e. that thread suspension sometimes simply does not work.
The following example code (for the vxworks skin) reproduces the issue (it gets stuck in the loop waiting for theWIND_SUSPENDflag):Could you check this URL? I can't seem to find anything pasted there.
Quoted 1 line
Quoted 10 lines
Quoted 2 lines
Any hints on how I can continue investigating this issue?
Running the test suite in
lib/vxworks/testsuitemay give us more information. sem-1, sem-2, task-2 and wd-1 are all using the suspend/resume mechanism.Signature · 1 line
Philippe.
Hi all,
please find another copy of the example code at:
The scenario described by Kim when using gdb may coincide with ocurrence of the issue all the time when using gdb on my system. However, also without gdb it happens from time to time (several 10000s cycles of the code above).
The enclosed test code from vxworks/testsuite seems to work when executing it once, but note that the issue only happens from time to time (thats why I added the sample code).
Matthias
Quoted 52 lines · Matthias Schneider
Quoted 2 lines
Quoted 2 lines
On Monday, March 17, 2014 9:12 AM, Philippe Gerum <[email protected]>
Quoted 27 lines · wrote:
Quoted 12 lines · Matthias Schneider
Hi all,
I am experiencing a problem with xenomai-forge (recent "next" checkout) on Mercury (on top of a 3.12.9+CONFIG_PREEMPT_RT on x86) when suspending tasks/threads.
As far as I understand, the mercury implementation sends a signal to the thread to be suspended, with that threads signal handler setting the suspended state, i.e. the setting of
__THREAD_S_SUSPENDEDhappens innotifier_callback, which seems to be different from the cobalt implementation.Quoted 7 lines
The problem I am experiencing is that sometimes the signal handler never seems to get called in a thread that should be suspended, i.e. that thread suspension sometimes simply does not work.
The following example code (for the vxworks skin) reproduces the issue (it gets stuck in the loop waiting for theWIND_SUSPENDflag):Could you check this URL? I can't seem to find anything pasted there.
Quoted 3 lines
Any hints on how I can continue investigating this issue?
Quoted 7 lines
Running the test suite in
lib/vxworks/testsuitemay give us more information. sem-1, sem-2, task-2 and wd-1 are all using the suspend/resume mechanism.--
Philippe.Hi all,
please find another copy of the example code at:
The scenario described by Kim when using gdb may coincide with ocurrence of the issue all the time when using gdb on my system. However, also without gdb it happens from time to time (several 10000s cycles of the code above).
Quoted 7 lines
The enclosed test code from vxworks/testsuite seems to work when executing it once,
but note that the issue only happens from time to time (thats why I added the sample
code).Matthias
Investigating the issue further, it seems that the signal that should trigger the "suspend" notifier call seems to be routed to the main thread instead of the thread to be suspended. The main thread then executes notifier_sighandler(), finds the signaled fd, sets "matched" to 1, but does not enter the read/callback loop since it is not the owner (which is correct since the owner is the thread that was supposed to be suspended). After finishing the pvlist_for_each_entry loop, notifier_sighandler() then runs through the following code
/*
* We may have received a valid notification on the wrong
* thread: bail out silently, assuming the recipient will find
* out eventually.
*/
if (matched)
return;
actually doing nothing. The thread to be suspended however never gets its signal handler executed.
Investigating the registration of the signal handler in notifier_init(), it seems the following code is responsible:
fd = nf->psfd[0];fcntl(fd, F_SETSIG, NOTIFYSIG);fcntl(fd, F_SETOWN, nf->owner);fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_ASYNC | O_NONBLOCK); fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
with nf->owner being the thread id of the owning thread, i.e. the one to be suspended in my case.
Studying the manpage of fcntl() reveals the following information:
"Set the process ID or process group ID that will receive SIGIO and SIGURG signals for events on file descriptor fd to the ID given in arg. A process ID is specified as a positive value; a process group ID is specified as a negative value. Most commonly, the calling process specifies itself as the owner (that is, arg is specified as getpid(2)).
[...]
The following was true in 2.6.x kernels up to and including kernel 2.6.11:
If a nonzero value is given to F_SETSIG in a multithreaded process running with a threading library that supports thread groups (e.g., NPTL), then a positive value given to F_SETOWN has a different
meaning: instead of being a process ID identifying a whole process, it is a thread ID identifying a specific thread within a process. Consequently, it may be necessary to pass F_SETOWN the result of gettid(2) instead of getpid(2) to get sensible results when F_SETSIG is used. (In current Linux threading implementations, a main thread's thread ID is the same as its process ID. This means that a single-threaded program can equally use gettid(2) or getpid(2) in this scenario.) Note, however, that the statements in this paragraph do not apply to the SIGURG signal generated for out-of-band data on a socket: this signal is always sent to either a process or a process group, depending on the value given to F_SETOWN.
The above behavior was accidentally dropped in Linux 2.6.12, and won't be restored. From Linux 2.6.32 onward, use F_SETOWN_EX to target SIGIO and SIGURG signals at a particular thread."
If I understand correctly, the desired behavior, i.e. sending a signal to a specific thread, would just be achieved up to Linux 2.6.12. Note that I also stumbled over some manpages that did not include the paragraph above...
Using F_SETOWN_EX like in the attached patch solves my problem (at least the described one) with the misdirected signals. However I still do not understand why the signal seems to have been delivered sometimes to the correct thread and sometimes not, but the answer can probably be found studying the kernel sources..
Regards,
Matthias
-------------- next part --------------
A non-text attachment was scrubbed...
Name: notif_owner.patch
Type: text/x-patch
Size: 542 bytes
Desc: not available
URL: <http://www.xenomai.org/pipermail/xenomai/attachments/20140323/e36827d6/attachment.bin>
Quoted 4 lines · Matthias Schneider
If I understand correctly, the desired behavior, i.e. sending a signal to a specific thread, would just be achieved up to Linux 2.6.12. Note that I also stumbled over some manpages that did not include the paragraph above...
This makes sense, as the notifier logic was written many moons ago.
Quoted 5 lines
Using
F_SETOWN_EXlike in the attached patch solves my problem (at least the described one) with the misdirected signals. However I still do not understand why the signal seems to have been delivered sometimes to the correct thread and sometimes not, but the answer can probably be found studying the kernel sources..
wants_signal() probably diverts the signal to another thread, because the original target is either traced (gdb) or already received a signal which is still pending delivery.
Signature · 1 line
Philippe.
2014-03-23 17:29 GMT+01:00 Philippe Gerum <[email protected]>:
Quoted 22 lines · Matthias Schneider
Quoted 7 lines
If I understand correctly, the desired behavior, i.e. sending a signal to a
specific thread, would just be achieved up to Linux 2.6.12. Note that I also
stumbled over some manpages that did not include the paragraph above...This makes sense, as the notifier logic was written many moons ago.
Quoted 9 lines
Using
F_SETOWN_EXlike in the attached patch solves my problem (at least the
described one) with the misdirected signals. However I still do not understand
why the signal seems to have been delivered sometimes to the correct thread and
sometimes not, but the answer can probably be found studying the kernel sources..
Quoted 4 lines
wants_signal()probably diverts the signal to another thread, because the original target is either traced (gdb) or already received a signal which is still pending delivery.
That would explain what I noticed when using gdb.
I have tested the patch on my setup and the problem is indeed solved. The signal is now always diverted to correct thread, also when debugging. Thanks for figuring this out!