Skip to contentSkip to conversation

Problem with time-out ends

Xenomai-help Fabien MAHOT 4 participants 53 messages May 19, 2008 – Jun 30, 2008 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Hello,

I ve a problem with my software application. I use Linux kernel 2.6.23 patched Xenomai 2.4.2. My hardware is an ETX module with AMD Geode Processor.

I wrote a test program to explain my problem. This crashes my system and I must reboot it. I think there is a bug when a thread calls Linux write function and when in the same time, a signal is catched to indicate a time-out end.

My test program :
Two threads are created :
TimeOut thread : with the higher priority, creates dynamically TimeOut and waits theirs ends. This thread uses Display function (with Linux write function) to display debug traces.
Display thread : uses Display function in a loop.

/***********************************************************************************/

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>

#define STACKSIZE 350

// stack
static int Stack[STACKSIZE];
static unsigned short Write_ptr = 0;
static unsigned short Read_ptr = 0;

// Display
pthread_mutex_t lockDisplay;
unsigned char bufferDisplay[2048];

// Timer

struct stTimeOut {
       timer_t timer_h;
       struct sigaction    sa;
       struct sigevent     sig_spec;
       struct itimerspec   tmr_setting;
       int number;
}*timeOut0_ptr, *timeOut1_ptr, *timeOut2_ptr;

// Thread start
pthread_cond_t start_signal;
pthread_mutex_t main_start_lock;
pthread_cond_t EndTimeOut_signal;
pthread_mutex_t TimeOut_lock;

/************************ Stack functions *************************/

int StackCreation(void)

{
	Write_ptr = 0;
	Read_ptr  = 0;
	return 0;
}

void StackWrite(int number)

{
	if (Write_ptr >= STACKSIZE)
	{
		Write_ptr = 0;
	}
	Stack[Write_ptr++] = number;
}

int StackRead(void)

{
    	int number;
    	if (Read_ptr >= STACKSIZE)
    	{
		Read_ptr = 0;
	}
	number = Stack[Read_ptr++];
	return number;
}

unsigned short GetWritePtr(void)

{
     	return Write_ptr;
}

unsigned short GetReadPtr(void)

{
     	return Read_ptr;
}

/************************ Functions ******************************/ void display(char * chaine,...)

{
    	pthread_mutex_lock(&lockDisplay);
    	va_list ArgDisplay;
	va_start(ArgDisplay, chaine);
	vsprintf((char *)bufferDisplay,chaine,ArgDisplay);
	write(2, (char*)bufferDisplay, strlen((char *)bufferDisplay));
	pthread_mutex_unlock(&lockDisplay);
}

int func(volatile int* i)

{
	return (*i)++;
}

void DeleteTimer(timer_t timer)

{
     	if (timer!=NULL)
     	{
          	timer_delete(timer);
     	}
}

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
     	volatile int i, result = 0;
     	DeleteTimer(((struct stTimeOut*)(info->si_value.sival_ptr))->timer_h);

     	StackWrite(((struct stTimeOut*)(info->si_value.sival_ptr))->number);

     	pthread_mutex_lock(&TimeOut_lock);
     	pthread_cond_broadcast(&EndTimeOut_signal);
     	pthread_mutex_unlock(&TimeOut_lock);
}

void StartTimeOut (int nb_Sec, int nb_nSec, struct stTimeOut* timeOut)

{
    	(timeOut->sa).sa_flags = SA_SIGINFO;
    	(timeOut->sa).sa_sigaction = EndTimeOut;

    	if(sigaction(SIGRTMIN, &(timeOut->sa), NULL)==-1)
    	{
        	display("sigaction error - errno : %d  -> %s\n",errno,
strerror(errno));
    	}

    	(timeOut->sig_spec).sigev_notify = SIGEV_SIGNAL;
    	(timeOut->sig_spec).sigev_signo = SIGRTMIN;
    	(timeOut->sig_spec).sigev_value.sival_ptr = timeOut;

    	if (timer_create(CLOCK_REALTIME, &(timeOut->sig_spec),
&(timeOut->timer_h)) == -1)
    	{
        	display("timer_create error - errno : %d  -> %s\n",errno,
strerror(errno));
    	}

    	(timeOut->tmr_setting).it_value.tv_sec = nb_Sec;
    	(timeOut->tmr_setting).it_value.tv_nsec = nb_nSec;
    	(timeOut->tmr_setting).it_interval.tv_sec = 0;
    	(timeOut->tmr_setting).it_interval.tv_nsec = 0;

    	if (timer_settime((timeOut->timer_h), 0,
&(timeOut->tmr_setting),NULL) == -1)
    	{
    	   display("timer_settime error - errno : %d  -> %s\n",errno,
strerror(errno));
    	}

}

/************************** Threads ********************************/

void* threadTimeOut(void * arg) {
    	int i=0;
    	int j, k, NbTimeOut, numTimeOut;

    	timeOut0_ptr = timeOut1_ptr = timeOut2_ptr = NULL;

	display("TimeOut thread\n");
	pthread_mutex_lock(&main_start_lock);
    	pthread_cond_wait(&start_signal, &main_start_lock);
    	pthread_mutex_unlock(&main_start_lock);
    	display("TimeOut thread\n");

    	while (i < 100)
    	{
        	// Malloc and start of time out
          	for (j=0 ; j < 3; j++)
          	{
              		switch(j)
              		{
              		case 0 : if (timeOut0_ptr == NULL)
                       		 {
                          		timeOut0_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          		if (timeOut0_ptr == NULL) exit(1);
                          			timeOut0_ptr->number = i;
                          			i++;
                          			display("Start of time out %d - 5ms\n",
timeOut0_ptr->number);
                          			StartTimeOut(0,500000000,timeOut0_ptr);
                       		  }
                       		  break;
              		case 1 : if (timeOut1_ptr == NULL)
                       		 {
                          		timeOut1_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          		if (timeOut1_ptr == NULL) exit(1);
                          		timeOut1_ptr->number = i;
                          		i++;
                          		display("Start of time out %d - 5ms\n",
timeOut1_ptr->number);
                          		StartTimeOut(0,500000000,timeOut1_ptr);
                       		  }
                       		  break;
              		case 2 : if (timeOut2_ptr == NULL)
                       		 {
                          		timeOut2_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          		if (timeOut2_ptr == NULL) exit(1);
                          			timeOut2_ptr->number = i;
                          			i++;
                          			display("Start of time out %d - 5ms\n",
timeOut2_ptr->number);
                          			StartTimeOut(0,500000000,timeOut2_ptr);
                       		  }
                       		   break;
              		}
          }
          // Waiting of first time out end
          pthread_mutex_lock(&TimeOut_lock);
          pthread_cond_wait(&EndTimeOut_signal, &TimeOut_lock);
          pthread_mutex_unlock(&TimeOut_lock);

          // Reading of time out numbers and free space
          NbTimeOut = GetWritePtr()- GetReadPtr();
          display("Number of time out ends : %d\n", NbTimeOut);
          for (j=0; j < NbTimeOut; j++)
          {
               numTimeOut = StackRead();
               display("TimeOut%d ends\n", numTimeOut);
               for (k=0; k < 3; k++)
               {
                   switch(k)
                   {
                   case 0 : if (timeOut0_ptr != NULL)
                            {
                               if (timeOut0_ptr->number == numTimeOut)
                               {
                                    free(timeOut0_ptr);
                                    timeOut0_ptr = NULL;
                               }
                            }
                            break;
                   case 1 : if (timeOut1_ptr != NULL)
                            {
                               if (timeOut1_ptr->number == numTimeOut)
                               {
                                    free(timeOut1_ptr);
                                    timeOut1_ptr = NULL;
                               }
                            }
                            break;
                   case 2 : if (timeOut2_ptr != NULL)
                            {
                               if (timeOut2_ptr->number == numTimeOut)
                               {
                                    free(timeOut2_ptr);
                                    timeOut2_ptr = NULL;
                               }
                            }
                            break;
                   }
               }
          }
    }

    while(1)
    {
        sleep(10);
    }
	return NULL;
}
void* threadDisplay(void * arg) {
	volatile int i=0;
	volatile int result;
	display ("Display thread\n");
	pthread_mutex_lock(&main_start_lock);
    	pthread_cond_wait(&start_signal, &main_start_lock);
    	pthread_mutex_unlock(&main_start_lock);
	display ("Display thread\n");

    	while (i <= 300000) {
        	result = func(&i);
        	display("Display thread :%d \r",result);
    	}
    	display ("End of display thread\n");
    	return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
     	DeleteTimer(timeOut0_ptr->timer_h);
     	DeleteTimer(timeOut1_ptr->timer_h);
     	DeleteTimer(timeOut2_ptr->timer_h);
     	exit(0);
}
int main(int argc, char** argv) {
    	pthread_attr_t attr;
    	pthread_mutexattr_t attr_proto;
    	pthread_t p1;
    	pthread_t p2;
    	struct sched_param sch;

    	// Stack creation
    	StackCreation();

    	signal(SIGINT, cleanup_upon_sig);
    	signal(SIGTERM, cleanup_upon_sig);

    	mlockall(MCL_CURRENT|MCL_FUTURE);

// mutex initialisation

	pthread_cond_init(&start_signal, NULL);
    	pthread_mutex_init(&main_start_lock, NULL);
    	pthread_cond_init(&EndTimeOut_signal, NULL);
    	pthread_mutex_init(&TimeOut_lock, NULL);
    	pthread_mutexattr_init(&attr_proto);
    	pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT);
    	pthread_mutex_init(&lockDisplay, &attr_proto);

    	pthread_attr_init(&attr);
    	pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(&attr, SCHED_FIFO);

    	// TimeOut thread creation
    	sch.sched_priority = 80;
    	pthread_attr_setschedparam(&attr, &sch);
    	pthread_create(&p1, &attr, threadTimeOut, NULL);

	// Display thread creation
	sch.sched_priority = 70;
    	pthread_attr_setschedparam(&attr, &sch);
    	pthread_create(&p2, &attr, threadDisplay, NULL);

    	pthread_attr_destroy(&attr);

    	display ("Main condition broadcast\n");
    	// Start of all threads
    	pthread_mutex_lock(&main_start_lock);
    	pthread_cond_broadcast(&start_signal);
    	pthread_mutex_unlock(&main_start_lock);

    	while (1) {
        	sleep(5);
    	}

    	return 0;
}

/***********************************************************************************/

OK, so when I run this program, the system crashes :
- Result on screen :

TimeOut thread
Display thread
Main condition broadcast
TimeOut thread
Start of time out 0 - 5ms
Display thread
Start of time out 1 - 5ms
Start of time out 2 - 5ms
Number of time out ends : 1
TimeOut0 ends
Start of time out 3 - 5ms
Number of time out ends : 3
TimeOut1 ends
TimeOut2 ends
TimeOut3 ends
Start of time out 4 - 5ms
Start of time out 5 - 5ms
Start of time out 6 - 5ms
Number of time out ends : 1
Start of time out 7 - 5ms
Display thread :22635 <- Crash of the system and I must reboot it

If someone can run several times my program on his system and says me if there is the problem.
I think that EndTimeOut() processing is realised by the thread which creates time-out. If this threads is in Linux domain (call to Write()) and a signal arrived to indicate a time-out end, it must change of domain and realise EndTimeOut() processing. Maybe, there is a problem at this moment.

I would like to know why I ve got this problem with Xenomai. If you ve got an idea, please , give me your opinion.

Thanks a lot.

#1 lore raw
Quoted 71 lines · Fabien MAHOT

Hello,

I ve a problem with my software application. I use Linux kernel 2.6.23 patched Xenomai 2.4.2. My hardware is an ETX module with AMD Geode Processor.

I wrote a test program to explain my problem. This crashes my system and I must reboot it. I think there is a bug when a thread calls Linux write function and when in the same time, a signal is catched to indicate a time-out end.

My test program :
Two threads are created :
TimeOut thread : with the higher priority, creates dynamically TimeOut and waits theirs ends. This thread uses Display function (with Linux write function) to display debug traces.
Display thread : uses Display function in a loop.

/***********************************************************************************/

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>

#define STACKSIZE 350

// stack
static int Stack[STACKSIZE];
static unsigned short Write_ptr = 0;
static unsigned short Read_ptr = 0;

// Display
pthread_mutex_t lockDisplay;
unsigned char bufferDisplay[2048];

// Timer

struct stTimeOut {
      timer_t timer_h;
      struct sigaction    sa;
      struct sigevent     sig_spec;
      struct itimerspec   tmr_setting;
      int number;
}*timeOut0_ptr, *timeOut1_ptr, *timeOut2_ptr;

// Thread start
pthread_cond_t start_signal;
pthread_mutex_t main_start_lock;
pthread_cond_t EndTimeOut_signal;
pthread_mutex_t TimeOut_lock;

/************************ Stack functions *************************/

int StackCreation(void)

{
       Write_ptr = 0;
       Read_ptr  = 0;
       return 0;
}
Quoted 8 lines

void StackWrite(int number)

{
       if (Write_ptr >= STACKSIZE)
       {
               Write_ptr = 0;
       }
       Stack[Write_ptr++] = number;
}

Note that this function is not atomic. You should probably add a mutex.

Quoted 25 lines

int StackRead(void)

{
       int number;
       if (Read_ptr >= STACKSIZE)
       {
               Read_ptr = 0;
       }
       number = Stack[Read_ptr++];
       return number;
}

unsigned short GetWritePtr(void)

{
       return Write_ptr;
}

unsigned short GetReadPtr(void)

{
       return Read_ptr;
}

/************************ Functions ******************************/ void display(char * chaine,...)

Quoted 8 lines
{
       pthread_mutex_lock(&lockDisplay);
       va_list ArgDisplay;
       va_start(ArgDisplay, chaine);
       vsprintf((char *)bufferDisplay,chaine,ArgDisplay);
       write(2, (char*)bufferDisplay, strlen((char *)bufferDisplay));
       pthread_mutex_unlock(&lockDisplay);
}

There is no point in doing this. Really, using fprintf(stderr) makes no difference. If you are looking for a display function that does not make you leave the primary mode, you should use the rt_printf function declared in the rtdk.h

Quoted 14 lines

int func(volatile int* i)

{
       return (*i)++;
}

void DeleteTimer(timer_t timer)

{
       if (timer!=NULL)
       {
               timer_delete(timer);
       }
}
Quoted 6 lines

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
       volatile int i, result = 0;
       DeleteTimer(((struct stTimeOut*)(info->si_value.sival_ptr))->timer_h);

       StackWrite(((struct stTimeOut*)(info->si_value.sival_ptr))->number);

I am not sure info->si_value.sival_ptr is what you think it is. This is probably a bug in Xenomai.

Quoted 43 lines
       pthread_mutex_lock(&TimeOut_lock);
       pthread_cond_broadcast(&EndTimeOut_signal);
       pthread_mutex_unlock(&TimeOut_lock);
}

void StartTimeOut (int nb_Sec, int nb_nSec, struct stTimeOut* timeOut)

{
       (timeOut->sa).sa_flags = SA_SIGINFO;
       (timeOut->sa).sa_sigaction = EndTimeOut;

       if(sigaction(SIGRTMIN, &(timeOut->sa), NULL)==-1)
       {
               display("sigaction error - errno : %d  -> %s\n",errno,
strerror(errno));
       }

       (timeOut->sig_spec).sigev_notify = SIGEV_SIGNAL;
       (timeOut->sig_spec).sigev_signo = SIGRTMIN;
       (timeOut->sig_spec).sigev_value.sival_ptr = timeOut;

       if (timer_create(CLOCK_REALTIME, &(timeOut->sig_spec),
&(timeOut->timer_h)) == -1)
       {
               display("timer_create error - errno : %d  -> %s\n",errno,
strerror(errno));
       }

       (timeOut->tmr_setting).it_value.tv_sec = nb_Sec;
       (timeOut->tmr_setting).it_value.tv_nsec = nb_nSec;
       (timeOut->tmr_setting).it_interval.tv_sec = 0;
       (timeOut->tmr_setting).it_interval.tv_nsec = 0;

       if (timer_settime((timeOut->timer_h), 0,
&(timeOut->tmr_setting),NULL) == -1)
       {
          display("timer_settime error - errno : %d  -> %s\n",errno,
strerror(errno));
       }

}

/************************** Threads ********************************/

Quoted 10 lines
void* threadTimeOut(void * arg) {
       int i=0;
       int j, k, NbTimeOut, numTimeOut;

       timeOut0_ptr = timeOut1_ptr = timeOut2_ptr = NULL;

       display("TimeOut thread\n");
       pthread_mutex_lock(&main_start_lock);
       pthread_cond_wait(&start_signal, &main_start_lock);
       pthread_mutex_unlock(&main_start_lock);

This is bad, pthread_cond_wait may experience spurious wakeups (notably when receiving a signal). So, you MUST check a variable external to the condition variable.

Quoted 43 lines
       display("TimeOut thread\n");

       while (i < 100)
       {
               // Malloc and start of time out
               for (j=0 ; j < 3; j++)
               {
                       switch(j)
                       {
                       case 0 : if (timeOut0_ptr == NULL)
                                {
                                       timeOut0_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                                       if (timeOut0_ptr == NULL) exit(1);
                                               timeOut0_ptr->number = i;
                                               i++;
                                               display("Start of time out %d - 5ms\n",
timeOut0_ptr->number);
                                               StartTimeOut(0,500000000,timeOut0_ptr);
                                 }
                                 break;
                       case 1 : if (timeOut1_ptr == NULL)
                                {
                                       timeOut1_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                                       if (timeOut1_ptr == NULL) exit(1);
                                       timeOut1_ptr->number = i;
                                       i++;
                                       display("Start of time out %d - 5ms\n",
timeOut1_ptr->number);
                                       StartTimeOut(0,500000000,timeOut1_ptr);
                                 }
                                 break;
                       case 2 : if (timeOut2_ptr == NULL)
                                {
                                       timeOut2_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                                       if (timeOut2_ptr == NULL) exit(1);
                                               timeOut2_ptr->number = i;
                                               i++;
                                               display("Start of time out %d - 5ms\n",
timeOut2_ptr->number);
                                               StartTimeOut(0,500000000,timeOut2_ptr);
Quoted 8 lines
                                 }
                                  break;
                       }
         }
         // Waiting of first time out end
         pthread_mutex_lock(&TimeOut_lock);
         pthread_cond_wait(&EndTimeOut_signal, &TimeOut_lock);
         pthread_mutex_unlock(&TimeOut_lock);

Ditto.

Quoted 51 lines
         // Reading of time out numbers and free space
         NbTimeOut = GetWritePtr()- GetReadPtr();
         display("Number of time out ends : %d\n", NbTimeOut);
         for (j=0; j < NbTimeOut; j++)
         {
              numTimeOut = StackRead();
              display("TimeOut%d ends\n", numTimeOut);
              for (k=0; k < 3; k++)
              {
                  switch(k)
                  {
                  case 0 : if (timeOut0_ptr != NULL)
                           {
                              if (timeOut0_ptr->number == numTimeOut)
                              {
                                   free(timeOut0_ptr);
                                   timeOut0_ptr = NULL;
                              }
                           }
                           break;
                  case 1 : if (timeOut1_ptr != NULL)
                           {
                              if (timeOut1_ptr->number == numTimeOut)
                              {
                                   free(timeOut1_ptr);
                                   timeOut1_ptr = NULL;
                              }
                           }
                           break;
                  case 2 : if (timeOut2_ptr != NULL)
                           {
                              if (timeOut2_ptr->number == numTimeOut)
                              {
                                   free(timeOut2_ptr);
                                   timeOut2_ptr = NULL;
                              }
                           }
                           break;
                  }
              }
         }

}

   while(1)
   {
       sleep(10);
   }
       return NULL;
}
Quoted 7 lines
void* threadDisplay(void * arg) {
       volatile int i=0;
       volatile int result;
       display ("Display thread\n");
       pthread_mutex_lock(&main_start_lock);
       pthread_cond_wait(&start_signal, &main_start_lock);
       pthread_mutex_unlock(&main_start_lock);

Same comment.

Quoted 91 lines
       display ("Display thread\n");

       while (i <= 300000) {
               result = func(&i);
               display("Display thread :%d \r",result);
       }
       display ("End of display thread\n");
       return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
       DeleteTimer(timeOut0_ptr->timer_h);
       DeleteTimer(timeOut1_ptr->timer_h);
       DeleteTimer(timeOut2_ptr->timer_h);
       exit(0);
}
int main(int argc, char** argv) {
       pthread_attr_t attr;
       pthread_mutexattr_t attr_proto;
       pthread_t p1;
       pthread_t p2;
       struct sched_param sch;

       // Stack creation
       StackCreation();

       signal(SIGINT, cleanup_upon_sig);
       signal(SIGTERM, cleanup_upon_sig);

       mlockall(MCL_CURRENT|MCL_FUTURE);

       // mutex initialisation
       pthread_cond_init(&start_signal, NULL);
       pthread_mutex_init(&main_start_lock, NULL);
       pthread_cond_init(&EndTimeOut_signal, NULL);
       pthread_mutex_init(&TimeOut_lock, NULL);
       pthread_mutexattr_init(&attr_proto);
       pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT);
       pthread_mutex_init(&lockDisplay, &attr_proto);

       pthread_attr_init(&attr);
       pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
       pthread_attr_setschedpolicy(&attr, SCHED_FIFO);

       // TimeOut thread creation
       sch.sched_priority = 80;
       pthread_attr_setschedparam(&attr, &sch);
       pthread_create(&p1, &attr, threadTimeOut, NULL);

       // Display thread creation
       sch.sched_priority = 70;
       pthread_attr_setschedparam(&attr, &sch);
       pthread_create(&p2, &attr, threadDisplay, NULL);

       pthread_attr_destroy(&attr);

       display ("Main condition broadcast\n");
       // Start of all threads
       pthread_mutex_lock(&main_start_lock);
       pthread_cond_broadcast(&start_signal);
       pthread_mutex_unlock(&main_start_lock);

       while (1) {
               sleep(5);
       }

       return 0;
}

/***********************************************************************************/

OK, so when I run this program, the system crashes :
- Result on screen :

TimeOut thread
Display thread
Main condition broadcast
TimeOut thread
Start of time out 0 - 5ms
Display thread
Start of time out 1 - 5ms
Start of time out 2 - 5ms
Number of time out ends : 1
TimeOut0 ends
Start of time out 3 - 5ms
Number of time out ends : 3
TimeOut1 ends

Quoted 8 lines

TimeOut2 ends
TimeOut3 ends
Start of time out 4 - 5ms
Start of time out 5 - 5ms
Start of time out 6 - 5ms
Number of time out ends : 1
Start of time out 7 - 5ms
Display thread :22635 <- Crash of the system and I must reboot it

This is abnormal. You should get at least a segmentation fault. Are you running your program under X-window? If yes, could you try and run it in text mode, so that you will be able to see if you have a kernel oops?

Signature · 1 line
 Gilles
#2 lore raw
Quoted 4 lines · Gilles Chanteperdrix
Quoted 2 lines · Fabien MAHOT

Hello,

Quoted 8 lines
Quoted 8 lines

I ve a problem with my software application. I use Linux kernel 2.6.23 patched Xenomai 2.4.2. My hardware is an ETX module with AMD Geode Processor.

I wrote a test program to explain my problem. This crashes my system and I must reboot it. I think there is a bug when a thread calls Linux write function and when in the same time, a signal is catched to indicate a time-out end.

Also note that since real-time signals use Linux signals in user-space, they cause the target thread to switch to secondary mode, which means that it is no longer a real-time thread.

Signature · 1 line
 Gilles
#3 lore raw

Thanks a lot for your answer,

I add a mutex in my StackWrite function. What happens when the EndTimeOut routine is in process and a new time-out signal occurs? The EndTimeOut processing finished ?

I also add an external variable to the condition variable.

I try to use rt_printf() instead of calling my Display function. The compilation is OK, but when I execute my test program, there is no trace on the screen (I use rt_printf() like printf()). (I ve got rtdk.h and librtdk.so)
I would like to try with this function ...

In spite of these modifications, the system always crashes

I am not running my program under X-window. I ve got a screen on the VGA port of my target.

#4 lore raw
Quoted 5 lines · Fabien MAHOT

Thanks a lot for your answer,

I add a mutex in my StackWrite function. What happens when the EndTimeOut routine is in process and a new time-out signal occurs? The EndTimeOut processing finished ?

Actually locking a mutex in a signal handler is strictly forbiden. So, you should implement EndTimeOut so that it does not lock any mutex (implicitly or explicitly).

Quoted 8 lines

I also add an external variable to the condition variable.

I try to use rt_printf() instead of calling my Display function. The compilation is OK, but when I execute my test program, there is no trace on the screen (I use rt_printf() like printf()). (I ve got rtdk.h and librtdk.so)
I would like to try with this function ...

Maybe what you want is rather rt_fprintf(stderr) ?

Quoted 5 lines

In spite of these modifications, the system always crashes

I am not running my program under X-window. I ve got a screen on the VGA port of my target.

As I told you, I think the crash is due to the value of info->si_value.sival_ptr in EndTimeOut. Did you check this value ?

Signature · 1 line
 Gilles
#5 lore raw

[Xenomai-help] Patching order

Hi All,

I'm getting Olimex SAM9-L9260, Atmel AT91SAM9260 based board which initially ships with 2.6.23 patched kernel. I would like to upgrade to 2.6.24 kernel, so I can install latest Xenomai. In order to do that, I need to patch 2.6.24 kernel with 2.6.24-at91.patch.gz wich has support for the mentioned board. To install latest Xenomai, adeos-ipipe-2.6.24-arm-1.9-01 patch is needed. Looks logical to me that 2.6.24-at91.patch.gz should be applied prior to adeos-ipipe-2.6.24-arm-1.9-01. Could someone please confirm if this statement is correct or patching order is different (or order doesn't matter)?

Reards,

g.

#6 lore raw

Re: [Xenomai-help] Patching order

Quoted 14 lines · George Pitich

Hi All,

I'm getting Olimex SAM9-L9260, Atmel AT91SAM9260 based board which initially ships with 2.6.23 patched kernel. I would like to upgrade to 2.6.24 kernel, so I can install latest Xenomai. In order to do that, I need to patch 2.6.24 kernel with 2.6.24-at91.patch.gz wich has support for the mentioned board. To install latest Xenomai, adeos-ipipe-2.6.24-arm-1.9-01 patch is needed. Looks logical to me that 2.6.24-at91.patch.gz should be applied prior to adeos-ipipe-2.6.24-arm-1.9-01. Could someone please confirm if this statement is correct or patching order is different (or order doesn't matter)?

The patch order does not matter, and it is done on purpose.

Signature · 1 line
					    Gilles.
#7 lore raw
In reply to Gilles Chanteperdrix

Thank you,

I tried to use rt_fprintf(stderr) or rt_printf(), but there is no debug traces on my Screen. But when I used fprintf(stderr) or printf(), there is no problem, I ve got traces on my screen.
Compilation is good, I don't know why it doesn't work.

I also checked the info->si_value.sival_ptr value and it is good.

My StackWrite and StackRead functions don't need a mutex. There is no conflict between them.

Otherwise, I decided to cancel the lock use in EndTimeOut and I use a semaphore to indicate the time-out end to TimeOut thread. But there is a bug.
This test program explains it :
When TimeOut thread starts a time-out and waits the associated semaphore (sem_wait()). When signal of time-out end arrives, TimeOut thread wakes up whereas there is no sem_post(), and sem_wait returns no error.

/************************************************************************/

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>

// Timer
timer_t timer_h;

struct sigaction    sa;
struct sigevent     sig_spec;
struct itimerspec   tmr_setting;

// Thread start
pthread_mutex_t start_thread_lock;

// Time-out end
sem_t TimeOut_sem;

/************************ Functions ******************************/ void DeleteTimer(timer_t timer)

{
     if (timer!=NULL)
     {
          timer_delete(timer);
     }
}

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
     // no sem_post() !
}

void StartTimeOut (int nb_Sec, int nb_nSec)

{
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = EndTimeOut;

    if(sigaction(SIGRTMIN, &(sa), NULL)==-1)
    {
        printf("sigaction error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    sig_spec.sigev_notify = SIGEV_SIGNAL;
    sig_spec.sigev_signo = SIGRTMIN;
    sig_spec.sigev_value.sival_ptr = NULL;

    if (timer_create(CLOCK_REALTIME, &(sig_spec), &(timer_h)) == -1)
    {
        printf("timer_create error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    tmr_setting.it_value.tv_sec = nb_Sec;
    tmr_setting.it_value.tv_nsec = nb_nSec;
    tmr_setting.it_interval.tv_sec = 0;
    tmr_setting.it_interval.tv_nsec = 0;

    if (timer_settime(timer_h, 0, &(tmr_setting),NULL) == -1)
    {
       printf("timer_settime error - errno : %d  -> %s\n",errno,
strerror(errno));
    }
}

/************************** Threads ********************************/ void* threadTimeOut(void * arg) {

    pthread_mutex_lock(&start_thread_lock);

    printf("Start of timeOut\n");
    StartTimeOut(1,500000000);

    printf("Wait of timeOut end\n");
    if ((sem_wait(&TimeOut_sem)) < 0)
    {
        printf("sem_wait error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    printf("End of timeOut thread\n");

    pthread_mutex_unlock(&start_thread_lock);

    while(1)
    {
        sleep(10);
    }
	return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
     DeleteTimer(timer_h);
     exit(0);
}
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    pthread_t p1;
    struct sched_param sch;

    signal(SIGINT, cleanup_upon_sig);
    signal(SIGTERM, cleanup_upon_sig);

    mlockall(MCL_CURRENT|MCL_FUTURE);
   	// mutex and sem initialisation
    pthread_mutex_init(&start_thread_lock, NULL);
    sem_init (&TimeOut_sem, 0,0);
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(&attr, SCHED_FIFO);

    // TimeOut thread creation
    sch.sched_priority = 80;
    pthread_attr_setschedparam(&attr, &sch);

    pthread_mutex_lock(&start_thread_lock);
    pthread_create(&p1, &attr, threadTimeOut, NULL);
    pthread_mutex_unlock(&start_thread_lock);

    while (1) {
        sleep(5);
    }

    return 0;
}

/************************************************************************************/

Result on the screen :

Start of timeOut
Wait of timeOut end
End of timeOut thread

The use of a semphore doesn't change my problem, I always have system crash.

#8 lore raw
Quoted 6 lines · Fabien MAHOT

Thank you,

I tried to use rt_fprintf(stderr) or rt_printf(), but there is no debug traces on my Screen. But when I used fprintf(stderr) or printf(), there is no problem, I ve got traces on my screen.
Compilation is good, I don't know why it doesn't work.

Neither do I. I am not very familiar with rt_printf.

Quoted 6 lines

I also checked the info->si_value.sival_ptr value and it is good.

My StackWrite and StackRead functions don't need a mutex. There is no conflict between them.

Quoted 7 lines

Otherwise, I decided to cancel the lock use in EndTimeOut and I use a semaphore to indicate the time-out end to TimeOut thread. But there is a bug.
This test program explains it :
When TimeOut thread starts a time-out and waits the associated semaphore (sem_wait()). When signal of time-out end arrives, TimeOut thread wakes up whereas there is no sem_post(), and sem_wait returns no error.

Could you check sem_wait return value (not just test if is negative) as well as errno ?

Quoted 9 lines

Result on the screen :

Start of timeOut
Wait of timeOut end
End of timeOut thread

The use of a semphore doesn't change my problem, I always have system crash.

Ok. I will run your test to see if I can reproduce the crash you observe.

Signature · 1 line
 Gilles
#9 lore raw
Quoted 10 lines · Gilles Chanteperdrix
Quoted 6 lines · Fabien MAHOT

Thank you,

I tried to use rt_fprintf(stderr) or rt_printf(), but there is no debug traces on my Screen. But when I used fprintf(stderr) or printf(), there is no problem, I ve got traces on my screen.
Compilation is good, I don't know why it doesn't work.

Neither do I. I am not very familiar with rt_printf.

Did you call 'rt_print_auto_init(1)'

/Anders

Signature · 5 lines
Anders Blomdell                  Email: [email protected]
Department of Automatic Control
Lund University                  Phone:    +46 46 222 4625
P.O. Box 118                     Fax:      +46 46 138118
SE-221 00 Lund, Sweden
#10 lore raw
In reply to Fabien MAHOT
Quoted 12 lines · Fabien MAHOT

Thank you,

I tried to use rt_fprintf(stderr) or rt_printf(), but there is no debug traces on my Screen. But when I used fprintf(stderr) or printf(), there is no problem, I ve got traces on my screen.
Compilation is good, I don't know why it doesn't work.

I also checked the info->si_value.sival_ptr value and it is good.

My StackWrite and StackRead functions don't need a mutex. There is no conflict between them.

Quoted 7 lines

Otherwise, I decided to cancel the lock use in EndTimeOut and I use a semaphore to indicate the time-out end to TimeOut thread. But there is a bug.
This test program explains it :
When TimeOut thread starts a time-out and waits the associated semaphore (sem_wait()). When signal of time-out end arrives, TimeOut thread wakes up whereas there is no sem_post(), and sem_wait returns no error.

The attached patch should fix the issue with sem_wait (or the interruption of any system call by a posix skin signal). However, I am unable to reproduce the crash.

Signature · 1 line
					    Gilles.
skins/posix/signal.c +6 -0
Index: ksrc/skins/posix/signal.c
===================================================================
@@ -1048,7 +1048,13 @@ static void pse51_dispatch_shadow_signal
 {
 	/* Migrate to secondary mode in order to get the signals delivered by
 	   Linux. */
+	xnthread_t *cur = xnpod_current_thread();
+	int interrupted;
+
+	interrupted = xnthread_test_info(cur, XNBREAK);
 	xnshadow_relax(1);
+	if (interrupted)
+		xnthread_set_info(cur, XNBREAK | XNKICKED);
 }
 
 void pse51_signal_handle_request(pthread_t thread)
#11 lore raw
In reply to Fabien MAHOT
Quoted 9 lines · Fabien MAHOT

Result on the screen :

Start of timeOut
Wait of timeOut end
End of timeOut thread

The use of a semphore doesn't change my problem, I always have system crash.

Do you observe any crash with Xenomai testsuite ?

Signature · 1 line
					    Gilles.
#12 lore raw

Hello,

I tried your patch. Now, sem_wait() returns an Interrupt system call error when a time-out end signal arrives. That's good.

But, with the same test program, the one which tests sem_wait(), my system crashes a while after the same_wait() call. (System freeze --> reboot) After sem_wait() call, there are a mutex unlock and a sleep calls, but I can't say if they are a problem with them :
- With mutex unlock call and sleep call: the system crashes between these calls. (printf() calls)
- If I cancel mutex unlock, the threads sleeps and the system is good.
- If I add a sem_post() in the EndTimeOut handler, sem_wait wakes up the thread but the system crashes before or over mutex unlock processing.

before your patch, with this program, i had sem_wait problem but no crash of the system.

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>

// Timer
timer_t timer_h;

struct sigaction    sa;
struct sigevent     sig_spec;
struct itimerspec   tmr_setting;

// Thread start
pthread_mutex_t start_thread_lock;

// Time-out end
sem_t TimeOut_sem;

/************************ Functions ******************************/ void DeleteTimer(timer_t timer)

{
     if (timer!=NULL)
     {
          timer_delete(timer);
     }
}

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
     // no sem_post() !

     //if ((sem_post(&TimeOut_sem)) < 0)
     //{
     //   printf("sem_post error - errno : %d  -> %s\n",errno,
strerror(errno));
     //}
}

void StartTimeOut (int nb_Sec, int nb_nSec)

{
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = EndTimeOut;

    if(sigaction(SIGRTMIN, &(sa), NULL)==-1)
    {
        printf("sigaction error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    sig_spec.sigev_notify = SIGEV_SIGNAL;
    sig_spec.sigev_signo = SIGRTMIN;
    sig_spec.sigev_value.sival_ptr = NULL;

    if (timer_create(CLOCK_REALTIME, &(sig_spec), &(timer_h)) == -1)
    {
        printf("timer_create error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    tmr_setting.it_value.tv_sec = nb_Sec;
    tmr_setting.it_value.tv_nsec = nb_nSec;
    tmr_setting.it_interval.tv_sec = 0;
    tmr_setting.it_interval.tv_nsec = 0;

    if (timer_settime(timer_h, 0, &(tmr_setting),NULL) == -1)
    {
       printf("timer_settime error - errno : %d  -> %s\n",errno,
strerror(errno));
    }
}

/************************** Threads ********************************/ void* threadTimeOut(void * arg) {

    if ((pthread_mutex_lock(&start_thread_lock)) < 0)
    {
        printf("pthread_mutex_lock error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    printf("Start of timeOut\n");
    StartTimeOut(1,500000000);

    printf("Wait of timeOut end\n");
    while ((sem_wait(&TimeOut_sem)) < 0)
    {
        printf("sem_wait error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    printf("End of timeOut thread\n");

    int err = pthread_mutex_unlock(&start_thread_lock);
    /*if ((pthread_mutex_unlock(&start_thread_lock)) < 0)
    {*/
        printf("pthread_mutex_unlock error - err : %d - errno : %d  ->
%s\n", err, errno, strerror(errno));
    //}

    while(1)
    {
        printf("avant sleep\n");
        sleep(10);
        printf("apres sleep\n");
    }
	return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
     DeleteTimer(timer_h);
     exit(0);
}
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    pthread_t p1;
    struct sched_param sch;

    signal(SIGINT, cleanup_upon_sig);
    signal(SIGTERM, cleanup_upon_sig);

    mlockall(MCL_CURRENT|MCL_FUTURE);
   	// mutex and sem initialisation
    pthread_mutex_init(&start_thread_lock, NULL);
    sem_init (&TimeOut_sem, 0,0);
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(&attr, SCHED_FIFO);

    // TimeOut thread creation
    sch.sched_priority = 80;
    pthread_attr_setschedparam(&attr, &sch);

    pthread_mutex_lock(&start_thread_lock);
    pthread_create(&p1, &attr, threadTimeOut, NULL);
    pthread_attr_destroy(&attr);
    pthread_mutex_unlock(&start_thread_lock);

    while (1) {
        sleep(5);
    }

    return 0;
}
#13 lore raw
Quoted 15 lines · Fabien MAHOT

Hello,

I tried your patch. Now, sem_wait() returns an Interrupt system call error when a time-out end signal arrives. That's good.

But, with the same test program, the one which tests sem_wait(), my system crashes a while after the same_wait() call. (System freeze --> reboot) After sem_wait() call, there are a mutex unlock and a sleep calls, but I can't say if they are a problem with them :
- With mutex unlock call and sleep call: the system crashes between these calls. (printf() calls)
- If I cancel mutex unlock, the threads sleeps and the system is good.
- If I add a sem_post() in the EndTimeOut handler, sem_wait wakes up the thread but the system crashes before or over mutex unlock processing.

Quoted 2 lines

before your patch, with this program, i had sem_wait problem but no crash of the system.

I have debugged the sem_wait issue by running your program, unloading and reloading xenomai posix skin module many times, without any crash or any reboot.

It shows one thing: there is an issue crashing your platform. Have you run the Xenomai testsuite to know if this is a general problem ?

Signature · 1 line
 Gilles
#14 lore raw
Quoted 11 lines

I have debugged the sem_wait issue by running your program, unloading and reloading xenomai posix skin module many times, without any crash or any reboot.

It shows one thing: there is an issue crashing your platform. Have you run the Xenomai testsuite to know if this is a general problem ?

--
Gilles

I ran some Xenomai tests on my target because I haven't installed native skin.

I ran cyclictest, and when I would to quit the execution with CTRL+C, I had a kernel panic - not syncing: Aiee, killing interrupt handler.

what does it mean ? There is a probleme with SIGINT ?

#15 lore raw
Quoted 12 lines · Fabien MAHOT
Quoted 11 lines

I have debugged the sem_wait issue by running your program, unloading and reloading xenomai posix skin module many times, without any crash or any reboot.

It shows one thing: there is an issue crashing your platform. Have you run the Xenomai testsuite to know if this is a general problem ?

--
Gilles

Quoted 7 lines

I ran some Xenomai tests on my target because I haven't installed native skin.

I ran cyclictest, and when I would to quit the execution with CTRL+C, I had a kernel panic - not syncing: Aiee, killing interrupt handler.

what does it mean ? There is a probleme with SIGINT ?

Aha, a kernel panic is much better than a silent crash: at least we can know what the kernel did not like.
Any chance to see the messages before the kernel panic ? If you have two computers with serial ports and a serial cross-cable, you can use a serial console. Otherwise, some users already sent us photographs of the panic.

Signature · 1 line
					    Gilles.
#16 lore raw

Hello,

Thanks to a serial console, I obtained the kernel traces before the crash when I executed Cyclictest.
However, I could not have the same error : kernel panic - not syncing:
Aiee, killing interrupt handler. The system crashed like before, without error message.

kernel traces :

################################################################################################

I-pipe: Detected illicit call from domain 'Xenomai'
into a service reserved for domain 'Linux' and below.

 [<c0102f2e>] show_trace_log_lvl+0x1f/0x34
 [<c0103b32>] show_trace+0x17/0x19
 [<c0103b87>] dump_stack+0x1a/0x1c
 [<c01323ab>] ipipe_check_context+0x70/0x7b
 [<c010f5a7>] sub_preempt_count+0x18/0x9f
 [<c036d91c>] _spin_unlock_irq+0x1c/0x31
 [<c010fbd5>] ipipe_reenter_root+0x3c/0xb6
 [<c0140667>] xnshadow_relax+0xe8/0x118
 [<c0140bf9>] hisyscall_event+0x1d9/0x226
 [<c0133806>] __ipipe_dispatch_event+0xde/0x1c2
 [<c01095f4>] __ipipe_syscall_root+0x74/0x105
 [<c01027b9>] system_call+0x29/0x4a
 =======================

I-pipe tracer log (100 points):
+ func 0 ipipe_trace_panic_freeze+0x9
(ipipe_check_context+0x39)
+ func -1 ipipe_check_context+0xc
(sub_preempt_count+0x18)

   +  func                   -1 sub_preempt_count+0xc (_spin_unlock_irq+0x1c)
 | +  end     0x80000000     -2 __ipipe_unstall_root+0x44

(_spin_unlock_irq+0x12)
| + func -3 __ipipe_sync_stage+0xe
(__ipipe_unstall_root+0x33)
| + begin 0x80000000 -4 __ipipe_unstall_root+0x1a
(_spin_unlock_irq+0x12)
+ func -5 __ipipe_unstall_root+0x8
(_spin_unlock_irq+0x12)

   +  func                   -5 debug_locks_off+0x8 (spin_bug+0x17)
   +  func                   -6 spin_bug+0xe (_raw_spin_unlock+0x58)
   +  func                   -7 debug_locks_off+0x8 (spin_bug+0x17)
   +  func                   -8 spin_bug+0xe (_raw_spin_unlock+0x30)
   +  func                   -9 _raw_spin_unlock+0x9 (_spin_unlock_irq+0xd)
   +  func                  -10 _spin_unlock_irq+0x8
(ipipe_reenter_root+0x3c)
   +  func                  -10 ipipe_reenter_root+0xe (xnshadow_relax+0xe8)
 | +  end     0x80000000    -11 __ipipe_restore_pipeline_head+0xa7

(xnshadow_relax+0xc7)
| # func -12 __ipipe_restore_pipeline_head+0xd
(xnshadow_relax+0xc7)
| # func -13 xnpod_suspend_thread+0xe
(xnshadow_relax+0xa3)

 | #  [  173] gatekeep 0    -16 xnpod_schedule_runnable+0x47 (rpi_push+0x192)
 | #  func                  -17 xnpod_schedule_runnable+0xe (rpi_push+0x192)
 | #  func                  -19 rpi_push+0xe (xnshadow_relax+0x6b)
 | #  func                  -21 __ipipe_set_irq_pending+0xb

(__ipipe_schedule_irq+0x6e)
| # func -22 __ipipe_schedule_irq+0xe
(rthal_apc_schedule+0x64)
| # func -23 rthal_apc_schedule+0xa
(schedule_linux_call+0x99)

 | #  func                  -23 schedule_linux_call+0xe (xnshadow_relax+0x61)
 | +  begin   0x80000000    -24 xnshadow_relax+0x36 (hisyscall_event+0x1d9)
   +  func                  -25 xnshadow_relax+0xe (hisyscall_event+0x1d9)
   +  func                  -26 hisyscall_event+0xe

(__ipipe_dispatch_event+0xde)
| + end 0x80000001 -27 __ipipe_dispatch_event+0xcf
(__ipipe_syscall_root+0x74)
| + begin 0x80000001 -28 __ipipe_dispatch_event+0x2c
(__ipipe_syscall_root+0x74)
+ func -29 __ipipe_dispatch_event+0xe
(__ipipe_syscall_root+0x74)

   +  func                  -30 __ipipe_syscall_root+0xa (system_call+0x29)
 | +  end     0x80000001    -86 __ipipe_dispatch_event+0x1ad

(__ipipe_syscall_root+0x74)
| + begin 0x80000001 -87 __ipipe_dispatch_event+0x101
(__ipipe_syscall_root+0x74)
| + end 0x80000000 -89 __ipipe_restore_pipeline_head+0xa7
(pthread_setschedparam+0x16b)
| # func -90 __ipipe_restore_pipeline_head+0xd
(pthread_setschedparam+0x16b)
| # [ 2981] lt-cycli 0 -91 xnpod_schedule+0x92
(pthread_setschedparam+0x145)
| # func -92 xnpod_schedule+0xe
(pthread_setschedparam+0x145)
| # func -93 xnpod_set_thread_mode+0xe
(pthread_setschedparam+0x140)
| # [ 2981] lt-cycli 0 -95 xnpod_resume_thread+0x66
(xnpod_renice_thread_inner+0x92)
| # func -96 xnpod_resume_thread+0xe
(xnpod_renice_thread_inner+0x92)
| # func -97 xnpod_renice_thread_inner+0xe
(xnpod_renice_thread+0x12)
| # func -98 xnpod_renice_thread+0x8
(pthread_setschedparam+0x133)
| + begin 0x80000000 -100 pthread_setschedparam+0x27
(__pthread_setschedparam+0x1cd)
+ func -101 pthread_setschedparam+0xe
(__pthread_setschedparam+0x1cd)
| + end 0x80000000 -102 __ipipe_restore_pipeline_head+0xa7
(__pthread_find+0xf9)
| # func -103 __ipipe_restore_pipeline_head+0xd
(__pthread_find+0xf9)
| + begin 0x80000000 -104 __pthread_find+0x89
(__pthread_setschedparam+0x59)
+ func -105 __pthread_find+0xe
(__pthread_setschedparam+0x59)
+ func -106 __pthread_setschedparam+0xe
(losyscall_event+0x9e)
| + end 0x80000000 -107 ipipe_unstall_pipeline_head+0x6a
(xnshadow_harden+0x158)
| # begin 0x80000000 -108 ipipe_unstall_pipeline_head+0x1a
(xnshadow_harden+0x158)
# func -108 ipipe_unstall_pipeline_head+0x8
(xnshadow_harden+0x158)

   #  func                 -111 xnpod_switch_fpu+0xb (xnshadow_harden+0xc9)
 | #  end     0x80000000   -112 __sched_text_start+0x2dd
(xnshadow_harden+0x9f)
 | #  func                 -113 debug_smp_processor_id+0xb (__switch_to+0x26)
 | #  func                 -114 __switch_to+0xe (__sched_text_start+0x2b9)
 | #  func                 -115 debug_smp_processor_id+0xb

(xnpod_schedule+0x6d3)
| # [ 2967] lt-cycl -1 -116 xnpod_schedule+0x92
(xnpod_schedule_handler+0x15)
| # func -117 xnpod_schedule+0xe
(xnpod_schedule_handler+0x15)

 | #  func                 -118 xnpod_schedule_handler+0x8 (__virq_end+0x2a)
 | +  func                 -119 __ipipe_sync_stage+0xe

(ipipe_suspend_domain+0x80)
| + func -120 ipipe_suspend_domain+0xe
(__ipipe_walk_pipeline+0x57)
| +func -121 __ipipe_walk_pipeline+0xa
(__ipipe_restore_pipeline_head+0x96)
| * +func -122 __ipipe_restore_pipeline_head+0xd
(gatekeeper_thread+0x145)
| * +func -123 __ipipe_set_irq_pending+0xb
(__ipipe_dispatch_wired+0x5f)
| * +func -124 __ipipe_dispatch_wired+0xb
(__ipipe_handle_irq+0x7c)
| * +func -125 __ipipe_handle_irq+0xe
(ipipe_trigger_irq+0x92)

 | * +func                 -126 memcpy+0xe (ipipe_trigger_irq+0x8d)
 | * +func                 -127 ipipe_trigger_irq+0xe (xnpod_schedule+0x28)
 | * +func                 -128 xnpod_schedule+0xe (gatekeeper_thread+0x11d)
 | * +func                 -129 disengage_irq_shield+0xb

(gatekeeper_thread+0x118)
| * +[ 2981] lt-cycl 99 -131 xnpod_resume_thread+0x66
(gatekeeper_thread+0x113)
| * +func -132 xnpod_resume_thread+0xe
(gatekeeper_thread+0x113)

 |   +begin   0x80000000   -133 gatekeeper_thread+0xeb (kthread+0x3e)
 |   +end     0x80000000   -134 __ipipe_restore_pipeline_head+0xa7
(rpi_pop+0x1c1)
 | * +func                 -135 __ipipe_restore_pipeline_head+0xd
(rpi_pop+0x1c1)
 |   +begin   0x80000000   -136 rpi_pop+0x22 (gatekeeper_thread+0xd9)
     +func                 -137 rpi_pop+0xe (gatekeeper_thread+0xd9)
     +func                 -138 kthread_should_stop+0x8
(gatekeeper_thread+0xbc)
     +func                 -139 ipipe_check_context+0xc
(sub_preempt_count+0x18)
     +func                 -140 sub_preempt_count+0xc
(__sched_text_start+0x367)
     +func                 -141 ipipe_check_context+0xc
(sub_preempt_count+0x18)
     +func                 -142 sub_preempt_count+0xc (_spin_unlock_irq+0x1c)

| +end 0x80000000 -142 __ipipe_unstall_root+0x44
(_spin_unlock_irq+0x12)
| #begin 0x80000000 -143 __ipipe_unstall_root+0x1a
(_spin_unlock_irq+0x12)

     #func                 -144 __ipipe_unstall_root+0x8
(_spin_unlock_irq+0x12)
     #func                 -145 _raw_spin_unlock+0x9 (_spin_unlock_irq+0xd)
     #func                 -146 _spin_unlock_irq+0x8
(__sched_text_start+0x313)
 |   #end     0x80000000   -147 __sched_text_start+0x2dd
(gatekeeper_thread+0xb7)
 |   #func                 -149 debug_smp_processor_id+0xb (__switch_to+0x26)
 |   #func                 -150 __switch_to+0xe (__sched_text_start+0x2b9)
 |   #begin   0x80000000   -151 __sched_text_start+0x1b8

(xnshadow_harden+0x9f)
| #end 0x80000001 -152 __ipipe_dispatch_event+0x1ad
(__sched_text_start+0x1a6)
| #begin 0x80000001 -153 __ipipe_dispatch_event+0x101
(__sched_text_start+0x1a6)
| #end 0x80000000 -154 __ipipe_restore_pipeline_head+0xa7
(schedule_event+0x3a7)
| * #func -155 __ipipe_restore_pipeline_head+0xd
(schedule_event+0x3a7)
| #begin 0x80000000 -156 schedule_event+0x22e
(__ipipe_dispatch_event+0xde)

     #func                 -157 schedule_event+0xe

(__ipipe_dispatch_event+0xde)
| #end 0x80000001 -158 __ipipe_dispatch_event+0xcf
(__sched_text_start+0x1a6)
| #begin 0x80000001 -159 __ipipe_dispatch_event+0x2c
(__sched_text_start+0x1a6)

######################################################################################################

I also tried without your patch, and when I stopped Cyclictest, there was no problem !

Morever, I tried one of my test program about time-out end. (without your patch)
In order to know if my system is really crash, I ran the timeout test program and also an another test program in which the highest priority thread is created. this last one calls printf function and sleeps. So, when the execution of time-out test pogram crashed, I hoped that the highest priority thread still ran.

Sometimes, the execution of time-out test pogram crashes but the system is OK, and the Highest priority thread runs.
But, I ve got also this case : the execution of time-out test pogram crashes and the system is crashed. In this case, I ve got bug traces (scheduling while atomic). I must reboot my system.

HighestPriorityThread.C

#########################################################################################

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <errno.h>

// Thread start
pthread_mutex_t start_thread_lock;

void* threadHighPriority(void * arg) {

    if ((pthread_mutex_lock(&start_thread_lock)) < 0)
    {
        printf("pthread_mutex_lock error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    if ((pthread_mutex_unlock(&start_thread_lock)) < 0)
    {
        printf("pthread_mutex_unlock error - errno : %d  -> %s\n", errno,
strerror(errno));
    }

    while(1)
    {
        printf("ThreadHighPriority : sleep 30s\n");
        sleep(30);
    }
	return NULL;
}

/***********************************************************************/

int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_t p1;
    struct sched_param sch;

    mlockall(MCL_CURRENT|MCL_FUTURE);
   	// mutex and sem initialisation
    pthread_mutex_init(&start_thread_lock, NULL);
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(&attr, SCHED_FIFO);

    // TimeOut thread creation
    sch.sched_priority = 99;
    pthread_attr_setschedparam(&attr, &sch);

    pthread_mutex_lock(&start_thread_lock);
    pthread_create(&p1, &attr, threadHighPriority, NULL);
    pthread_attr_destroy(&attr);
    pthread_mutex_unlock(&start_thread_lock);

    while (1) {
        sleep(5);
    }

    return 0;
}

#######################################################################################

testTimer10.C (this program uses sem_wait without your patch but there is a management)

#######################################################################################

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>
#include <rtdk.h> // rt_printf()

#define NB_PTR_TEMPO 3 // 5 time-out max
#define STACKSIZE 350

// stack
static int Stack[STACKSIZE];
static unsigned short Write_ptr = 0;
static unsigned short Read_ptr = 0;

// Display
pthread_mutex_t lockDisplay;
unsigned char bufferDisplay[2048];

// Timer

struct stTimeOut {
       timer_t timer_h;
       struct sigaction    sa;
       struct sigevent     sig_spec;
       struct itimerspec   tmr_setting;
       int number;
}*timeOut0_ptr, *timeOut1_ptr, *timeOut2_ptr, *timeOut3_ptr, *timeOut4_ptr;

// Thread start
pthread_cond_t start_signal;
pthread_mutex_t main_start_lock;
bool bMainStart = false;
// Time-out end
sem_t TimeOut_sem;

// Stack mutex
pthread_mutex_t Stack_lock;

// Display file
FILE *file = NULL;

/************************ Stack functions *************************/

int StackCreation(void)

{
	Write_ptr = 0;
	Read_ptr  = 0;
	return 0;
}

void StackWrite(int number)

{
    if (Write_ptr >= STACKSIZE)
	{
		Write_ptr = 0;
	}
	Stack[Write_ptr++] = number;
}

int StackRead(void)

{
    int number;
    if (Read_ptr >= STACKSIZE)
    {
		Read_ptr = 0;
	}
	number = Stack[Read_ptr];
	return number;
}

int StackReadValid (void)

{
    Read_ptr++;
}

unsigned short GetWritePtr(void)

{
     return Write_ptr;
}

unsigned short GetReadPtr(void)

{
     return Read_ptr;
}

/************************ Functions ******************************/ void display(char * chaine,...)

{
    pthread_mutex_lock(&lockDisplay);
    va_list ArgDisplay;
	va_start(ArgDisplay, chaine);
	vsprintf((char *)bufferDisplay,chaine,ArgDisplay);
	write(2, (char*)bufferDisplay, strlen((char *)bufferDisplay));
	//printf(bufferDisplay);
    pthread_mutex_unlock(&lockDisplay);
}

int func(volatile int* i)

{
	return (*i)++;
}

void DeleteTimer(timer_t timer)

{
     if (timer!=NULL)
     {
          timer_delete(timer);
     }
}

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
     volatile int i, result = 0;

     DeleteTimer(((struct stTimeOut*)(info->si_value.sival_ptr))->timer_h);

     StackWrite(((struct stTimeOut*)(info->si_value.sival_ptr))->number);

     if ((sem_post(&TimeOut_sem)) == -1)
     {
        printf("sem_post error - errno : %d  -> %s\n",errno,
strerror(errno));
     }
}

void StartTimeOut (int nb_Sec, int nb_nSec, struct stTimeOut* timeOut)

{
    (timeOut->sa).sa_flags = SA_SIGINFO;
    (timeOut->sa).sa_sigaction = EndTimeOut;

    while(sigaction(SIGRTMIN, &(timeOut->sa), NULL) < 0)
    {
        printf("sigaction error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    (timeOut->sig_spec).sigev_notify = SIGEV_SIGNAL;
    (timeOut->sig_spec).sigev_signo = SIGRTMIN;
    (timeOut->sig_spec).sigev_value.sival_ptr = timeOut;

    while (timer_create(CLOCK_REALTIME, &(timeOut->sig_spec),
&(timeOut->timer_h)) < 0)
    {
        printf("timer_create error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    (timeOut->tmr_setting).it_value.tv_sec = nb_Sec;
    (timeOut->tmr_setting).it_value.tv_nsec = nb_nSec;
    (timeOut->tmr_setting).it_interval.tv_sec = 0;
    (timeOut->tmr_setting).it_interval.tv_nsec = 0;

    while (timer_settime((timeOut->timer_h), 0,
&(timeOut->tmr_setting),NULL) < 0)
    {
       printf("timer_settime error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

}

/************************** Threads ********************************/

void* threadTimeOut(void * arg) {
    int i=1;
    int j, k, NbTimeOut, numTimeOut, err;
    int NbSem = 0;
    bool bFirstTimeOut = false;
    bool bEndTimeOutExec = false;

    timeOut0_ptr = timeOut1_ptr = timeOut2_ptr = timeOut3_ptr =

timeOut4_ptr = NULL;

	display("TimeOut thread\n");
	while (!bMainStart)
	{
       pthread_mutex_lock(&main_start_lock);
       pthread_cond_wait(&start_signal, &main_start_lock);
       pthread_mutex_unlock(&main_start_lock);
    }
    display("TimeOut thread\n");

    while (i < 100)
    {
          // Malloc and start of time out
          for (j=0 ; j < NB_PTR_TEMPO; j++)
          {
              switch(j)
              {
              case 0 : if (timeOut0_ptr == NULL)
                       {
                          timeOut0_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          if (timeOut0_ptr == NULL) exit(1);
                          timeOut0_ptr->number = i;
                          i++;
                          display("Start of time out %d - 5ms\n",
timeOut0_ptr->number);
                          StartTimeOut(0,500000000,timeOut0_ptr);
                       }
                       break;
              case 1 : if (timeOut1_ptr == NULL)
                       {
                          timeOut1_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          if (timeOut1_ptr == NULL) exit(1);
                          timeOut1_ptr->number = i;
                          i++;
                          display("Start of time out %d - 5ms\n",
timeOut1_ptr->number);
                          StartTimeOut(0,500000000,timeOut1_ptr);
                       }
                       break;
              case 2 : if (timeOut2_ptr == NULL)
                       {
                          timeOut2_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          if (timeOut2_ptr == NULL) exit(1);
                          timeOut2_ptr->number = i;
                          i++;
                          display("Start of time out %d - 5ms\n",
timeOut2_ptr->number);
                          StartTimeOut(0,500000000,timeOut2_ptr);
                       }
                       break;
              case 3 : if (timeOut3_ptr == NULL)
                       {
                          timeOut3_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          if (timeOut3_ptr == NULL) exit(1);
                          timeOut3_ptr->number = i;
                          i++;
                          display("Start of time out %d - 5ms\n",
timeOut3_ptr->number);
                          StartTimeOut(0,500000000,timeOut3_ptr);
                       }
                       break;
              case 4 : if (timeOut4_ptr == NULL)
                       {
                          timeOut4_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          if (timeOut4_ptr == NULL) exit(1);
                          timeOut4_ptr->number = i;
                          i++;
                          display("Start of time out %d - 5ms\n",
timeOut4_ptr->number);
                          StartTimeOut(0,500000000,timeOut4_ptr);
                       }
                       break;
              }
          }

          bFirstTimeOut = true;
          while (bFirstTimeOut == true || NbSem > 0)
          {
             bFirstTimeOut = false;

             while((sem_wait(&TimeOut_sem)) < 0)
             {
                 printf("sem_wait error - errno : %d  -> %s\n",errno,
strerror(errno));
             }

             numTimeOut = StackRead();
             if (numTimeOut)
             {
                StackReadValid();
                printf("TimeOut%d ends\n", numTimeOut);
                for (k=0; k < NB_PTR_TEMPO; k++)
                {
                    switch(k)
                    {
                    case 0 : if (timeOut0_ptr != NULL)
                             {
                             if (timeOut0_ptr->number == numTimeOut)
                                {
                                 free(timeOut0_ptr);
                                 timeOut0_ptr = NULL;
                                }
                             }
                             break;
                    case 1 : if (timeOut1_ptr != NULL)
                             {
                                if (timeOut1_ptr->number == numTimeOut)
                                {
                                   free(timeOut1_ptr);
                                   timeOut1_ptr = NULL;
                                }
                             }
                             break;
                    case 2 : if (timeOut2_ptr != NULL)
                             {
                                if (timeOut2_ptr->number == numTimeOut)
                                {
                                     free(timeOut2_ptr);
                                     timeOut2_ptr = NULL;
                                }
                             }
                             break;
                    case 3 : if (timeOut3_ptr != NULL)
                             {
                                if (timeOut3_ptr->number == numTimeOut)
                                {
                                     free(timeOut3_ptr);
                                     timeOut3_ptr = NULL;
                                }
                             }
                             break;
                    case 4 : if (timeOut4_ptr != NULL)
                             {
                                if (timeOut4_ptr->number == numTimeOut)
                                {
                                     free(timeOut4_ptr);
                                     timeOut4_ptr = NULL;
                                }
                             }
                             break;
                    }
                }
                if ((sem_getvalue(&TimeOut_sem,&NbSem)) == -1)
                {
                   printf("sem_getvalue error - errno : %d  ->
%s\n",errno, strerror(errno));
                }
             }
             else  NbSem = 0;
          }
    }
    //printf("End of timeOut thread\n");
    while(1)
    {
        sleep(10);
    }
	return NULL;
}
void* threadDisplay(void * arg) {
    volatile int i=0;
	volatile int result;
	display("Display thread\n");
	while (!bMainStart)
	{
         pthread_mutex_lock(&main_start_lock);
         pthread_cond_wait(&start_signal, &main_start_lock);
         pthread_mutex_unlock(&main_start_lock);
    }
	display("Display thread\n");

    while (i <= 20000) {
        result = func(&i);
        display("Display thread :%d \r",result);
    }
    display("End of display thread\n");
    return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
     //fclose(file);
     DeleteTimer(timeOut0_ptr->timer_h);
     DeleteTimer(timeOut1_ptr->timer_h);
     DeleteTimer(timeOut2_ptr->timer_h);
     exit(0);
}
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    pthread_t p1;
    pthread_t p2;
    struct sched_param sch;

    // rt_printf initialisation
    rt_print_auto_init(1);

    // Stack creation
    StackCreation();

    signal(SIGINT, cleanup_upon_sig);
    signal(SIGTERM, cleanup_upon_sig);

    mlockall(MCL_CURRENT|MCL_FUTURE);

// mutex and sem initialisation

	pthread_cond_init(&start_signal, NULL);
    pthread_mutex_init(&main_start_lock, NULL);
    sem_init (&TimeOut_sem, 0,0);
    pthread_mutexattr_init(&attr_proto);
    pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT);
    pthread_mutex_init(&lockDisplay, &attr_proto);

    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(&attr, SCHED_FIFO);

    // TimeOut thread creation
    sch.sched_priority = 80;
    pthread_attr_setschedparam(&attr, &sch);
    pthread_create(&p1, &attr, threadTimeOut, NULL);

	// Display thread creation
	sch.sched_priority = 70;
    pthread_attr_setschedparam(&attr, &sch);
    pthread_create(&p2, &attr, threadDisplay, NULL);

    pthread_attr_destroy(&attr);

    display("Main condition broadcast\n");
    // Start of all threads
    pthread_mutex_lock(&main_start_lock);
    bMainStart = true;
    pthread_cond_broadcast(&start_signal);
    pthread_mutex_unlock(&main_start_lock);

    while (1) {
        sleep(5);
    }

    return 0;
}

#################################################################################

kernel traces :

#################################################################################

BUG: scheduling while atomic: testTimer10/0x00000002/2854
 [<c0102f2e>] show_trace_log_lvl+0x1f/0x34
 [<c0103b32>] show_trace+0x17/0x19
 [<c0103b87>] dump_stack+0x1a/0x1c
 [<c010f9dd>] __schedule_bug+0x48/0x4a
 [<c036b536>] __sched_text_start+0x8e/0x381
 [<c0140816>] xnshadow_harden+0x9f/0x167
 [<c0140957>] losyscall_event+0x79/0x142
 [<c0133806>] __ipipe_dispatch_event+0xde/0x1c2
 [<c01095f4>] __ipipe_syscall_root+0x74/0x105
 [<c01027b9>] system_call+0x29/0x4a
 =======================
BUG: scheduling while atomic: testTimer10/0x00000002/2854
 [<c0102f2e>] show_trace_log_lvl+0x1f/0x34
 [<c0103b32>] show_trace+0x17/0x19
 [<c0103b87>] dump_stack+0x1a/0x1c
 [<c010f9dd>] __schedule_bug+0x48/0x4a
 [<c036b536>] __sched_text_start+0x8e/0x381
 [<c0140816>] xnshadow_harden+0x9f/0x167
 [<c0140957>] losyscall_event+0x79/0x142
 [<c0133806>] __ipipe_dispatch_event+0xde/0x1c2
 [<c01095f4>] __ipipe_syscall_root+0x74/0x105
 [<c01027b9>] system_call+0x29/0x4a
 =======================
BUG: scheduling while atomic: testTimer10/0x00000002/2854
 [<c0102f2e>] show_trace_log_lvl+0x1f/0x34
 [<c0103b32>] show_trace+0x17/0x19
 [<c0103b87>] dump_stack+0x1a/0x1c
 [<c010f9dd>] __schedule_bug+0x48/0x4a
 [<c036b536>] __sched_text_start+0x8e/0x381
 [<c0140816>] xnshadow_harden+0x9f/0x167
 [<c0140957>] losyscall_event+0x79/0x142
 [<c0133806>] __ipipe_dispatch_event+0xde/0x1c2
 [<c01095f4>] __ipipe_syscall_root+0x74/0x105
 [<c01027b9>] system_call+0x29/0x4a
 =======================
BUG: scheduling while atomic: testTimer10/0x00000002/2854
 [<c0102f2e>] show_trace_log_lvl+0x1f/0x34
 [<c0103b32>] show_trace+0x17/0x19
 [<c0103b87>] dump_stack+0x1a/0x1c
 [<c010f9dd>] __schedule_bug+0x48/0x4a
 [<c036b536>] __sched_text_start+0x8e/0x381
 [<c0140816>] xnshadow_harden+0x9f/0x167
 [<c0140957>] losyscall_event+0x79/0x142
 [<c0133806>] __ipipe_dispatch_event+0xde/0x1c2
 [<c01095f4>] __ipipe_syscall_root+0x74/0x105
 [<c01027b9>] system_call+0x29/0x4a
 =======================
BUG: scheduling while atomic: testTimer10/0x00000002/2854
 [<c0102f2e>] show_trace_log_lvl+0x1f/0x34
 [<c0103b32>] show_trace+0x17/0x19
 [<c0103b87>] dump_stack+0x1a/0x1c
 [<c010f9dd>] __schedule_bug+0x48/0x4a
 [<c036b536>] __sched_text_start+0x8e/0x381
 [<c0140816>] xnshadow_harden+0x9f/0x167
 [<c0140957>] losyscall_event+0x79/0x142
 [<c0133806>] __ipipe_dispatch_event+0xde/0x1c2
 [<c01095f4>] __ipipe_syscall_root+0x74/0x105
 [<c01027b9>] system_call+0x29/0x4a
 =======================

################################################################################

I don't succeed to understand these bug traces. Could you help me, please? I think that a thread must be preempted or must be in hold whereas it should be in execution.

thanks.

#17 lore raw
Quoted 7 lines · Fabien MAHOT

Hello,

Thanks to a serial console, I obtained the kernel traces before the crash when I executed Cyclictest.
However, I could not have the same error : kernel panic - not syncing:
Aiee, killing interrupt handler. The system crashed like before, without error message.

I do not understand what you mean. You tell us that you get no traces, but then shows some traces.

Anyway, could you enable the Xenomai nucleus debugging ? In particular, I would be interested to know if you get the fatal "Error relaxing a kicked thread".

Signature · 1 line
 Gilles
#18 lore raw
In reply to Fabien MAHOT
Quoted 2 lines · Fabien MAHOT

I also tried without your patch, and when I stopped Cyclictest, there was no problem !

Ah! You put interesting information in the middle of I-pipe tracer traces, how do you want me to discover them ? So, this is a bug caused by my patch. In my patch, do you get better results if you do not set the XNKICKED bit ?

Signature · 1 line
 Gilles
#19 lore raw
Quoted 8 lines · Fabien MAHOT
Quoted 7 lines

Hello,

Thanks to a serial console, I obtained the kernel traces before the crash when I executed Cyclictest.
However, I could not have the same error : kernel panic - not syncing:
Aiee, killing interrupt handler. The system crashed like before, without error message.

Quoted 6 lines

I do not understand what you mean. You tell us that you get no traces, but then shows some traces.

Anyway, could you enable the Xenomai nucleus debugging ? In particular, I would be interested to know if you get the fatal "Error relaxing a kicked thread".

sorry, my english is bad. I would like to say that I had not the explicit error message kernel panic - not syncing: Aiee, killing interrupt handler

Quoted 9 lines
Quoted 3 lines · Fabien MAHOT

I also tried without your patch, and when I stopped Cyclictest, there was
no problem !

Ah! You put interesting information in the middle of I-pipe tracer traces, how do you want me to discover them ? So, this is a bug caused by my patch. In my patch, do you get better results if you do not set

the XNKICKED bit ?

Quoted 4 lines

--
Gilles

yes, I cancelled XNKICKED in xnthread_set_info call. And, it's better, there is no problem when I stop Cyclictest.
With my sem_wait test program, sem_wait returns an error when a signal of time-out end unlocks it, so that's good.
And , I no longer have system crash after the sem_wait call.

However, When I executed my big application, the system crashed when a thread seemed to call pthread_cond_wait. I m not sure, in this application, there are a lot of threads. The kernel trace when the system crashed is : Xenomai: fatal: Relaxing a kicked thread(thread=Dialogue serie 1, mask=200)?!
That is what you planned. What does this message mean ?
I tried to reproduce this issue with a small test program but I didn't succeed.

I ve got a test program with this issue, but it's the one in which I ve got a lock in the time-out end routine. (pthread_cond_broadcast) I posted it in the mailing list and you corrected it.
I replaced the mutex use by a semaphore.

Otherwise, I ve got an other test program in which there are 3 real-time threads. Time-outs are created dynamically and have the same duration. (5ms)
I ve a display function, to display traces on the console. (use write function)
The threads :
threadTimeOut : creates and deletes time-outs threadTimeOutEnd : waits the end of a time-out (sem_wait) and indicates
it to threadTimeOut
threadDisplay : calls display function in a loop.

When I execute this program, there is a system crash. the kernel trace is : Xenomai: fatal: inserting element twice, holder=c7761c78, qslot=c7761160 at include/xenomai/nucleus/queue.h:321
I don't execute it without your patch.

this is the test program :

###########################################################################################

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>

#define NB_PTR_TEMPO 3 // 5 tempos maximum
#define STACKSIZE 350

// stack
static int Stack[STACKSIZE];
static unsigned short Write_ptr = 0;
static unsigned short Read_ptr = 0;

// Display
pthread_mutex_t lockDisplay;
unsigned char bufferDisplay[2048];

// Timer

struct stTimeOut {
       timer_t timer_h;
       struct sigaction    sa;
       struct sigevent     sig_spec;
       struct itimerspec   tmr_setting;
       int number;
}*timeOut0_ptr, *timeOut1_ptr, *timeOut2_ptr, *timeOut3_ptr, *timeOut4_ptr;

// TimeOut_ptr
struct stTimeOut* tabTimeOut_ptr[STACKSIZE];
static unsigned short Write_to_ptr = 0;
static unsigned short Read_to_ptr = 0;

// Thread start
pthread_cond_t start_signal;
pthread_mutex_t main_start_lock;
bool bMainStart = false;
// Time-out end
pthread_cond_t TimeOutEnd_signal;
pthread_mutex_t timeOutEnd_lock;
bool bTimeOutEnd = false;
sem_t TimeOutWait_sem;

// Stack mutex
pthread_mutex_t Stack_lock;

/************************ Stack functions *************************/

int StackCreation(void)

{
	Write_ptr = 0;
	Read_ptr  = 0;
	return 0;
}

void StackWrite(int number)

{
    if (Write_ptr >= STACKSIZE)
	{
		Write_ptr = 0;
	}
	Stack[Write_ptr++] = number;
}

int StackRead(void)

{
    int number;
    if (Read_ptr >= STACKSIZE)
    {
		Read_ptr = 0;
	}
	number = Stack[Read_ptr++];
	return number;
}

unsigned short GetWritePtr(void)

{
     return Write_ptr;
}

unsigned short GetReadPtr(void)

{
     return Read_ptr;
}

/************************ Functions ******************************/ void display(char * chaine,...)

{
    pthread_mutex_lock(&lockDisplay);
    va_list ArgDisplay;
	va_start(ArgDisplay, chaine);
	vsprintf((char *)bufferDisplay,chaine,ArgDisplay);
	write(2, (char*)bufferDisplay, strlen((char *)bufferDisplay));
	//printf(bufferDisplay);
    pthread_mutex_unlock(&lockDisplay);
}

int func(volatile int* i)

{
	return (*i)++;
}

void DeleteTimer(timer_t timer)

{
     if (timer!=NULL)
     {
          timer_delete(timer);
     }
}

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
     volatile int i, result = 0;

     DeleteTimer(((struct stTimeOut*)(info->si_value.sival_ptr))->timer_h);

     StackWrite(((struct stTimeOut*)(info->si_value.sival_ptr))->number);

     if ((sem_post(&TimeOutWait_sem)) == -1)
     {
        printf("sem_post error - errno : %d  -> %s\n",errno,
strerror(errno));
     }
}

void StartTimeOut (int nb_Sec, int nb_nSec, struct stTimeOut* timeOut)

{
    (timeOut->sa).sa_flags = SA_SIGINFO;
    (timeOut->sa).sa_sigaction = EndTimeOut;

    while(sigaction(SIGRTMIN, &(timeOut->sa), NULL) < 0)
    {
        display("sigaction error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    (timeOut->sig_spec).sigev_notify = SIGEV_SIGNAL;
    (timeOut->sig_spec).sigev_signo = SIGRTMIN;
    (timeOut->sig_spec).sigev_value.sival_ptr = timeOut;

    while (timer_create(CLOCK_REALTIME, &(timeOut->sig_spec),
&(timeOut->timer_h)) < 0)
    {
        display("timer_create error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

    (timeOut->tmr_setting).it_value.tv_sec = nb_Sec;
    (timeOut->tmr_setting).it_value.tv_nsec = nb_nSec;
    (timeOut->tmr_setting).it_interval.tv_sec = 0;
    (timeOut->tmr_setting).it_interval.tv_nsec = 0;

    while (timer_settime((timeOut->timer_h), 0,
&(timeOut->tmr_setting),NULL) < 0)
    {
       display("timer_settime error - errno : %d  -> %s\n",errno,
strerror(errno));
    }

}

/************************** Threads ********************************/ void* threadTimeOutEnd(void * arg) {

    int NbSem = 0;
    bool bSemWaitError;

    display("TimeOutCreation thread\n");
	pthread_mutex_lock(&main_start_lock);
	while (!bMainStart)
	{
       pthread_cond_wait(&start_signal, &main_start_lock);
    }
    pthread_mutex_unlock(&main_start_lock);
    display("TimeOutCreation thread\n");

    while (1)
    {
          do
          {
             bSemWaitError = false;
             if ((sem_wait(&TimeOutWait_sem)) < 0)
             {
                 display("sem_wait error - errno : %d  -> %s\n",errno,
strerror(errno));
                 if (errno == EINTR) // la tache appelant sem_wait a été
débloquée de son attente par un signal d'interruption
			     {
                      bSemWaitError = true;
                 }
             }
          }while (bSemWaitError);

          pthread_mutex_lock(&timeOutEnd_lock);
          if((pthread_cond_broadcast(&TimeOutEnd_signal)) < 0)
          {
              display("pthread_cond_broadcast error - errno : %d  ->
%s\n",errno, strerror(errno));
              exit(0);
          }
          bTimeOutEnd = true;
          pthread_mutex_unlock(&timeOutEnd_lock);
    }
}
void* threadTimeOut(void * arg) {
    int i=1;
    int j, k, NbTimeOut, numTimeOut;

    timeOut0_ptr = timeOut1_ptr = timeOut2_ptr = timeOut3_ptr =

timeOut4_ptr = NULL;

	display("TimeOut thread\n");
	pthread_mutex_lock(&main_start_lock);
	while (!bMainStart)
	{
       pthread_cond_wait(&start_signal, &main_start_lock);
    }
    pthread_mutex_unlock(&main_start_lock);
    display("TimeOut thread\n");

    while (i < 100)
    {
          // Malloc and start of time out
          for (j=0 ; j < NB_PTR_TEMPO; j++)
          {
              switch(j)
              {
              case 0 : if (timeOut0_ptr == NULL)
                       {
                          timeOut0_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          if (timeOut0_ptr == NULL) exit(1);
                          timeOut0_ptr->number = i;
                          i++;
                          display("Start of time out %d - 5ms\n",
timeOut0_ptr->number);
                          StartTimeOut(0,500000000,timeOut0_ptr);
                       }
                       break;
              case 1 : if (timeOut1_ptr == NULL)
                       {
                          timeOut1_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          if (timeOut1_ptr == NULL) exit(1);
                          timeOut1_ptr->number = i;
                          i++;
                          display("Start of time out %d - 5ms\n",
timeOut1_ptr->number);
                          StartTimeOut(0,500000000,timeOut1_ptr);
                       }
                       break;
              case 2 : if (timeOut2_ptr == NULL)
                       {
                          timeOut2_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          if (timeOut2_ptr == NULL) exit(1);
                          timeOut2_ptr->number = i;
                          i++;
                          display("Start of time out %d - 5ms\n",
timeOut2_ptr->number);
                          StartTimeOut(0,500000000,timeOut2_ptr);
                       }
                       break;
              case 3 : if (timeOut3_ptr == NULL)
                       {
                          timeOut3_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          if (timeOut3_ptr == NULL) exit(1);
                          timeOut3_ptr->number = i;
                          i++;
                          display("Start of time out %d - 5ms\n",
timeOut3_ptr->number);
                          StartTimeOut(0,500000000,timeOut3_ptr);
                       }
                       break;
              case 4 : if (timeOut4_ptr == NULL)
                       {
                          timeOut4_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                          if (timeOut4_ptr == NULL) exit(1);
                          timeOut4_ptr->number = i;
                          i++;
                          display("Start of time out %d - 5ms\n",
timeOut4_ptr->number);
                          StartTimeOut(0,500000000,timeOut4_ptr);
                       }
                       break;
              }
          }

          pthread_mutex_lock(&timeOutEnd_lock);
          while (!bTimeOutEnd)
          {
                if
((pthread_cond_wait(&TimeOutEnd_signal,&timeOutEnd_lock))
< 0)
                {
                   display("pthread_cond_wait error - errno : %d  ->
%s\n",errno, strerror(errno));
                   exit(0);
                }
          }
          bTimeOutEnd = false;
          pthread_mutex_unlock(&timeOutEnd_lock);

          NbTimeOut = GetWritePtr() - GetReadPtr();
          display("Number of time-outs ends : %d\n", NbTimeOut);
          for (j=0; j < NbTimeOut; j++)
          {
             numTimeOut = StackRead();
             display("TimeOut%d ends\n", numTimeOut);
             for (k=0; k < NB_PTR_TEMPO; k++)
             {
                  switch(k)
                  {
                  case 0 : if (timeOut0_ptr != NULL)
                           {
                           if (timeOut0_ptr->number == numTimeOut)
                              {
                               free(timeOut0_ptr);
                               timeOut0_ptr = NULL;
                              }
                           }
                           break;
                  case 1 : if (timeOut1_ptr != NULL)
                           {
                              if (timeOut1_ptr->number == numTimeOut)
                              {
                                 free(timeOut1_ptr);
                                 timeOut1_ptr = NULL;
                              }
                           }
                           break;
                  case 2 : if (timeOut2_ptr != NULL)
                           {
                              if (timeOut2_ptr->number == numTimeOut)
                              {
                                   free(timeOut2_ptr);
                                   timeOut2_ptr = NULL;
                              }
                           }
                           break;
                  case 3 : if (timeOut3_ptr != NULL)
                           {
                              if (timeOut3_ptr->number == numTimeOut)
                              {
                                   free(timeOut3_ptr);
                                   timeOut3_ptr = NULL;
                              }
                           }
                           break;
                  case 4 : if (timeOut4_ptr != NULL)
                           {
                              if (timeOut4_ptr->number == numTimeOut)
                              {
                                   free(timeOut4_ptr);
                                   timeOut4_ptr = NULL;
                              }
                           }
                           break;
                  }
              }
          }
    }
    while(1)
    {
        sleep(10);
    }
	return NULL;
}
void* threadDisplay(void * arg) {
    volatile int i=0;
	volatile int result;
	display("Display thread\n");
	pthread_mutex_lock(&main_start_lock);
	while (!bMainStart)
	{
       pthread_cond_wait(&start_signal, &main_start_lock);
    }
    pthread_mutex_unlock(&main_start_lock);
	display("Display thread\n");

    while (i <= 200000) {
        result = func(&i);
        display("Display thread :%d \r",result);
    }
    display("End of display thread\n");
    return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
     DeleteTimer(timeOut0_ptr->timer_h);
     DeleteTimer(timeOut1_ptr->timer_h);
     DeleteTimer(timeOut2_ptr->timer_h);
     exit(0);
}
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    pthread_t p0;
    pthread_t p1;
    pthread_t p2;
    struct sched_param sch;

    // Stack & TabTImeOutptr creation
    StackCreation();

    signal(SIGINT, cleanup_upon_sig);
    signal(SIGTERM, cleanup_upon_sig);

    mlockall(MCL_CURRENT|MCL_FUTURE);

// mutex and sem initialisation

	pthread_cond_init(&start_signal, NULL);
    pthread_mutex_init(&main_start_lock, NULL);
    pthread_cond_init(&TimeOutEnd_signal, NULL);
    pthread_mutex_init(&timeOutEnd_lock, NULL);
    sem_init (&TimeOutWait_sem, 0,0);
    pthread_mutexattr_init(&attr_proto);
    pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT);
    pthread_mutex_init(&lockDisplay, &attr_proto);

    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(&attr, SCHED_FIFO);

    // TimeOutCreation thread creation
    sch.sched_priority = 85;
    pthread_attr_setschedparam(&attr, &sch);
    pthread_create(&p0, &attr, threadTimeOutEnd, NULL);

    // TimeOut thread creation
    sch.sched_priority = 80;
    pthread_attr_setschedparam(&attr, &sch);
    pthread_create(&p1, &attr, threadTimeOut, NULL);

	// Display thread creation
	sch.sched_priority = 70;
    pthread_attr_setschedparam(&attr, &sch);
    pthread_create(&p2, &attr, threadDisplay, NULL);

    pthread_attr_destroy(&attr);

    display("Main condition broadcast\n");
    // Start of all threads
    pthread_mutex_lock(&main_start_lock);
    bMainStart = true;
    pthread_cond_broadcast(&start_signal);
    pthread_mutex_unlock(&main_start_lock);

    while (1) {
        sleep(5);
    }

    return 0;
}

##################################################################################

Results on the console :

TimeOutCreation thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutCreation thread
TimeOut thread
Start of time out 1 - 5ms
Display thread
Start of time out 2 - 5ms
Start of time out 3 - 5ms
Display thread :8791 <---- the system crashed

I hope that you can help me. I will try to execute this program without your patch to see what kernel traces I will have if the kernel crashes. I wonder why my system is so unsteady...

Thanks a lot for your help.

#20 lore raw
Quoted 43 lines · Fabien MAHOT
Quoted 8 lines · Fabien MAHOT
Quoted 7 lines

Hello,

Thanks to a serial console, I obtained the kernel traces before the crash when I executed Cyclictest.
However, I could not have the same error : kernel panic - not syncing:
Aiee, killing interrupt handler. The system crashed like before, without error message.

Quoted 6 lines

I do not understand what you mean. You tell us that you get no traces, but then shows some traces.

Anyway, could you enable the Xenomai nucleus debugging ? In particular, I would be interested to know if you get the fatal "Error relaxing a kicked thread".

sorry, my english is bad. I would like to say that I had not the explicit error message kernel panic - not syncing: Aiee, killing interrupt handler

Quoted 9 lines
Quoted 3 lines · Fabien MAHOT

I also tried without your patch, and when I stopped Cyclictest, there was
no problem !

Ah! You put interesting information in the middle of I-pipe tracer traces, how do you want me to discover them ? So, this is a bug caused by my patch. In my patch, do you get better results if you do not set

the XNKICKED bit ?

Quoted 4 lines

--
Gilles

yes, I cancelled XNKICKED in xnthread_set_info call. And, it's better, there is no problem when I stop Cyclictest.
With my sem_wait test program, sem_wait returns an error when a signal of time-out end unlocks it, so that's good.
And , I no longer have system crash after the sem_wait call.

Quoted 6 lines

However, When I executed my big application, the system crashed when a thread seemed to call pthread_cond_wait. I m not sure, in this application, there are a lot of threads. The kernel trace when the system crashed is : Xenomai: fatal: Relaxing a kicked thread(thread=Dialogue serie 1, mask=200)?!
That is what you planned. What does this message mean ?

It means that the way the Posix skin passes real-time signals to secondary mode is currently broken. Because they cause the target threads to switch to secondary mode, I guess nobody use them, so I advise you to stop using them as well until they get fixed (but even if we fix them, they will always cause the target thread to switch to secondary mode). Note that most posix services have "timed" variants which allow setting a timeout, such as pthread_mutex_timedlock, sem_timedwait, pthread_cond_timedwait, etc... so, you really do not need timers to set up a timeout.

Quoted 7 lines

I tried to reproduce this issue with a small test program but I didn't succeed.

I ve got a test program with this issue, but it's the one in which I ve got a lock in the time-out end routine. (pthread_cond_broadcast) I posted it in the mailing list and you corrected it.
I replaced the mutex use by a semaphore.

You mean you replaced a condvar by a semaphore ?

Quoted 12 lines

Otherwise, I ve got an other test program in which there are 3 real-time threads. Time-outs are created dynamically and have the same duration. (5ms)
I ve a display function, to display traces on the console. (use write function)
The threads :
threadTimeOut : creates and deletes time-outs threadTimeOutEnd : waits the end of a time-out (sem_wait) and indicates
it to threadTimeOut
threadDisplay : calls display function in a loop.

Quoted 4 lines

When I execute this program, there is a system crash. the kernel trace is : Xenomai: fatal: inserting element twice, holder=c7761c78, qslot=c7761160 at include/xenomai/nucleus/queue.h:321
I don't execute it without your patch.

Ok. To know exactly what happens we should put a call to show_stack(NULL, NULL) in the queue debugging code. But I will do this myself, I will try your test program.

Quoted 6 lines
   display("TimeOutCreation thread\n");
       pthread_mutex_lock(&main_start_lock);
       while (!bMainStart)
       {
      pthread_cond_wait(&start_signal, &main_start_lock);
   }

This is bad, if pthread_cond_wait fails for one reason or another, you end up with a system lock up. Better test its return value, and call exit if it fails for any reason.

Quoted 37 lines
   pthread_mutex_unlock(&main_start_lock);
   display("TimeOutCreation thread\n");
   while (1)
   {
         do
         {
            bSemWaitError = false;
            if ((sem_wait(&TimeOutWait_sem)) < 0)
            {
                display("sem_wait error - errno : %d  -> %s\n",errno,
strerror(errno));
                if (errno == EINTR) // la tache appelant sem_wait a été
débloquée de son attente par un signal d'interruption
                            {
                     bSemWaitError = true;
                }
            }
         }while (bSemWaitError);

         pthread_mutex_lock(&timeOutEnd_lock);
         if((pthread_cond_broadcast(&TimeOutEnd_signal)) < 0)
         {
             display("pthread_cond_broadcast error - errno : %d  ->
%s\n",errno, strerror(errno));
             exit(0);
         }
         bTimeOutEnd = true;
         pthread_mutex_unlock(&timeOutEnd_lock);

}
}

void* threadTimeOut(void * arg) {
   int i=1;
   int j, k, NbTimeOut, numTimeOut;

   timeOut0_ptr = timeOut1_ptr = timeOut2_ptr = timeOut3_ptr =
Quoted 8 lines

timeOut4_ptr = NULL;

       display("TimeOut thread\n");
       pthread_mutex_lock(&main_start_lock);
       while (!bMainStart)
       {
      pthread_cond_wait(&start_signal, &main_start_lock);

}

Ditto.

Quoted 75 lines
   pthread_mutex_unlock(&main_start_lock);
   display("TimeOut thread\n");
   while (i < 100)
   {
         // Malloc and start of time out
         for (j=0 ; j < NB_PTR_TEMPO; j++)
         {
             switch(j)
             {
             case 0 : if (timeOut0_ptr == NULL)
                      {
                         timeOut0_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                         if (timeOut0_ptr == NULL) exit(1);
                         timeOut0_ptr->number = i;
                         i++;
                         display("Start of time out %d - 5ms\n",
timeOut0_ptr->number);
                         StartTimeOut(0,500000000,timeOut0_ptr);
                      }
                      break;
             case 1 : if (timeOut1_ptr == NULL)
                      {
                         timeOut1_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                         if (timeOut1_ptr == NULL) exit(1);
                         timeOut1_ptr->number = i;
                         i++;
                         display("Start of time out %d - 5ms\n",
timeOut1_ptr->number);
                         StartTimeOut(0,500000000,timeOut1_ptr);
                      }
                      break;
             case 2 : if (timeOut2_ptr == NULL)
                      {
                         timeOut2_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                         if (timeOut2_ptr == NULL) exit(1);
                         timeOut2_ptr->number = i;
                         i++;
                         display("Start of time out %d - 5ms\n",
timeOut2_ptr->number);
                         StartTimeOut(0,500000000,timeOut2_ptr);
                      }
                      break;
             case 3 : if (timeOut3_ptr == NULL)
                      {
                         timeOut3_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                         if (timeOut3_ptr == NULL) exit(1);
                         timeOut3_ptr->number = i;
                         i++;
                         display("Start of time out %d - 5ms\n",
timeOut3_ptr->number);
                         StartTimeOut(0,500000000,timeOut3_ptr);
                      }
                      break;
             case 4 : if (timeOut4_ptr == NULL)
                      {
                         timeOut4_ptr=(struct
stTimeOut*)malloc(sizeof(struct stTimeOut));
                         if (timeOut4_ptr == NULL) exit(1);
                         timeOut4_ptr->number = i;
                         i++;
                         display("Start of time out %d - 5ms\n",
timeOut4_ptr->number);
                         StartTimeOut(0,500000000,timeOut4_ptr);
                      }
                      break;
             }
         }

         pthread_mutex_lock(&timeOutEnd_lock);
         while (!bTimeOutEnd)
Quoted 8 lines
         {
               if
((pthread_cond_wait(&TimeOutEnd_signal,&timeOutEnd_lock))
< 0)
               {
                  display("pthread_cond_wait error - errno : %d  ->
%s\n",errno, strerror(errno));
                  exit(0);

pthread_cond_wait never returns a negative error and does not set errno, it returns the error directly.

Signature · 1 line
 Gilles
#21 lore raw
Quoted 8 lines
Quoted 7 lines

However, When I executed my big application, the system crashed when a thread seemed to call pthread_cond_wait. I m not sure, in this application, there are a lot of threads. The kernel trace when the system
crashed is : Xenomai: fatal: Relaxing a kicked thread(thread=Dialogue serie 1, mask=200)?!
That is what you planned. What does this message mean ?

Quoted 10 lines

It means that the way the Posix skin passes real-time signals to secondary mode is currently broken. Because they cause the target threads to switch to secondary mode, I guess nobody use them, so I advise you to stop using them as well until they get fixed (but even if we fix them, they will always cause the target thread to switch to secondary mode). Note that most posix services have "timed" variants which allow setting a timeout, such as pthread_mutex_timedlock, sem_timedwait, pthread_cond_timedwait, etc... so, you really do not need timers to set up a timeout.

Yes, but currently, in my big application, when a thread sets up a timeout, it happens that the thread continues to execute code. In fact, when the timeout end signal arrives, the handler is executed and a semaphore is posted to a timeout thread. This timeout thread analyzes which timeouts are finished and indicates it to pertained threads thanks to a condvar or a flag variable.
So if I use pthread_mutex_timedlock, sem_timedwait or pthread_cond_timedwait, I can't have this behaviour. A thread is inevitably in waiting if it calls these functions.

Quoted 11 lines
Quoted 8 lines

I tried to reproduce this issue with a small test program but I didn't succeed.

I ve got a test program with this issue, but it's the one in which I ve got a lock in the time-out end routine. (pthread_cond_broadcast) I posted
it in the mailing list and you corrected it.
I replaced the mutex use by a semaphore.

You mean you replaced a condvar by a semaphore ?

Yes

Quoted 20 lines
Quoted 13 lines

Otherwise, I ve got an other test program in which there are 3 real-time threads. Time-outs are created dynamically and have the same duration. (5ms)
I ve a display function, to display traces on the console. (use write function)
The threads :
threadTimeOut : creates and deletes time-outs threadTimeOutEnd : waits the end of a time-out (sem_wait) and
indicates
it to threadTimeOut
threadDisplay : calls display function in a loop.

Quoted 6 lines

When I execute this program, there is a system crash. the kernel trace is
: Xenomai: fatal: inserting element twice, holder=c7761c78, qslot=c7761160
at include/xenomai/nucleus/queue.h:321
I don't execute it without your patch.

Quoted 4 lines

Ok. To know exactly what happens we should put a call to show_stack(NULL, NULL) in the queue debugging code. But I will do this myself, I will try your test program.

Ok , I corrected all your remarks. Do you have results with my test program? do you have a system crash with the same error message ?

Thanks a lot for your help.

#22 lore raw
Quoted 63 lines · Fabien MAHOT
Quoted 8 lines
Quoted 7 lines

However, When I executed my big application, the system crashed when a thread seemed to call pthread_cond_wait. I m not sure, in this application, there are a lot of threads. The kernel trace when the system
crashed is : Xenomai: fatal: Relaxing a kicked thread(thread=Dialogue serie 1, mask=200)?!
That is what you planned. What does this message mean ?

Quoted 10 lines

It means that the way the Posix skin passes real-time signals to secondary mode is currently broken. Because they cause the target threads to switch to secondary mode, I guess nobody use them, so I advise you to stop using them as well until they get fixed (but even if we fix them, they will always cause the target thread to switch to secondary mode). Note that most posix services have "timed" variants which allow setting a timeout, such as pthread_mutex_timedlock, sem_timedwait, pthread_cond_timedwait, etc... so, you really do not need timers to set up a timeout.

Yes, but currently, in my big application, when a thread sets up a timeout, it happens that the thread continues to execute code. In fact, when the timeout end signal arrives, the handler is executed and a semaphore is posted to a timeout thread. This timeout thread analyzes which timeouts are finished and indicates it to pertained threads thanks to a condvar or a flag variable.
So if I use pthread_mutex_timedlock, sem_timedwait or pthread_cond_timedwait, I can't have this behaviour. A thread is inevitably in waiting if it calls these functions.

Quoted 11 lines
Quoted 8 lines

I tried to reproduce this issue with a small test program but I didn't succeed.

I ve got a test program with this issue, but it's the one in which I ve got a lock in the time-out end routine. (pthread_cond_broadcast) I posted
it in the mailing list and you corrected it.
I replaced the mutex use by a semaphore.

You mean you replaced a condvar by a semaphore ?

Yes

Quoted 20 lines
Quoted 19 lines

Otherwise, I ve got an other test program in which there are 3 real-time threads. Time-outs are created dynamically and have the same duration. (5ms)
I ve a display function, to display traces on the console. (use write function)
The threads :
threadTimeOut : creates and deletes time-outs threadTimeOutEnd : waits the end of a time-out (sem_wait) and
indicates
it to threadTimeOut
threadDisplay : calls display function in a loop.

When I execute this program, there is a system crash. the kernel trace is
: Xenomai: fatal: inserting element twice, holder=c7761c78, qslot=c7761160
at include/xenomai/nucleus/queue.h:321
I don't execute it without your patch.

Quoted 7 lines
Quoted 4 lines

Ok. To know exactly what happens we should put a call to show_stack(NULL, NULL) in the queue debugging code. But I will do this myself, I will try your test program.

Ok , I corrected all your remarks. Do you have results with my test program? do you have a system crash with the same error message ?

No, I had not time to do it. I will probably test it tonight or tomorrow night.

Signature · 1 line
 Gilles
#23 lore raw
In reply to Fabien MAHOT
Quoted 2 lines · Fabien MAHOT

Ok , I corrected all your remarks. Do you have results with my test program? do you have a system crash with the same error message ?

Here comes a second attempt of fixing the posix shadow signals issue.

Signature · 1 line
					    Gilles.
skins/posix/signal.c +13 -2
Index: ksrc/skins/posix/signal.c
===================================================================
@@ -301,7 +301,7 @@ int sigismember(const sigset_t * set, in
 	return ismember(pse51_set, sig);
 }
 
-/* Must be called with nklock lock, irqs off, may reschedule. */
+/* Must be called with nklock lock, irqs off. */
 int pse51_sigqueue_inner(pthread_t thread, pse51_siginfo_t * si)
 {
 	unsigned prio;
@@ -327,8 +327,19 @@ int pse51_sigqueue_inner(pthread_t threa
 	}
 
 #ifdef CONFIG_XENO_OPT_PERVASIVE
-	if (testbits(thread->threadbase.state, XNSHADOW))
+	if (xnthread_test_state(&thread->threadbase, XNSHADOW)) {
 		pse51_schedule_lostage(PSE51_LO_SIGNAL_REQ, thread, 0);
+		if (xnthread_test_state(&thread->threadbase,
+					XNDELAY|XNPEND|XNSUSP|XNRELAX)) {
+			/* Thread is suspended in a syscall, or already relaxed,
+			   we do not need to run the signal dispatcher, the
+			   signal APC will cause the sigwake_event to be
+			   generated, which will kick the thread. */
+			thread->threadbase.signals = 0;
+			return 0;
+		} else
+			return thread == pse51_current_thread();
+	}
 #endif /* CONFIG_XENO_OPT_PERVASIVE */
 
 	return thread == pse51_current_thread()
#24 lore raw

Problem with time-out ends

Hello,

thanks a lot for time spent and your patch.
Sorry for the carriage returns of my mailer.

I tried your patch with test program version that you sent me. (I installed xenomai 2.4.3)

And, the system always crashes. But, now, there is no longer error kernel trace on my serial console.
I don t obtain result with your check function.

When you got the crash, what is your error kernel trace? the same as mine ? Xenomai: fatal: inserting element twice, holder=c7761c78, qslot=c7761160

With my big application, there is an improvement. The system doesn't crash. There is just my application which crashes but it still run and there is no error message. (I must stop it with a signal sending (Ctrl+c))

cordially

#25 lore raw
Quoted 10 lines · Fabien MAHOT

Problem with time-out ends

Hello,

thanks a lot for time spent and your patch.
Sorry for the carriage returns of my mailer.

I tried your patch with test program version that you sent me. (I installed xenomai 2.4.3)

Quoted 7 lines

And, the system always crashes. But, now, there is no longer error kernel trace on my serial console.
I don t obtain result with your check function.

When you got the crash, what is your error kernel trace? the same as mine ? Xenomai: fatal: inserting element twice, holder=c7761c78, qslot=c7761160

No, I do not get this error. Do you get a stack trace with this error ? I had an "error relaxing kicked thread".

Signature · 1 line
 Gilles
#26 lore raw
Quoted 21 lines
Quoted 10 lines · Fabien MAHOT

Problem with time-out ends

Hello,

thanks a lot for time spent and your patch.
Sorry for the carriage returns of my mailer.

I tried your patch with test program version that you sent me. (I installed xenomai 2.4.3)

Quoted 8 lines

And, the system always crashes. But, now, there is no longer error kernel
trace on my serial console.
I don t obtain result with your check function.

When you got the crash, what is your error kernel trace? the same as mine ? Xenomai: fatal: inserting element twice, holder=c7761c78, qslot=c7761160

Quoted 6 lines

No, I do not get this error. Do you get a stack trace with this error ? I had an "error relaxing kicked thread".

--
Gilles

OK. No, I didn't get stack traces with this error.

#27 lore raw
In reply to Gilles Chanteperdrix

I succeed to reduce my test program.
Now, I ve got two xenomai threads.
threadDisplay (prio : 70) : locks a mutex (conflict_lock mutex) and passes between Xenomai and Linux domain. (call to sleep()) threadTimeOut (prio : 80) : Starts one timeout of 5ms and tries to lock the mutex taken by threadDisplay. (Priority Inheritance)

threadTimeOut waits the conflict_lock unlock of threadDisplay. With priority inheritance, threadTimeOut gives its priority to threadDisplay. But, threadTimeOut must also do the EndTimeOut() processing. When the timeout end signal arrives, if threadDisplay is into Linux Domain, the system crashes.

this my test program :

############################################################################

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <errno.h>

// Timer
timer_t timer_h;

struct sigaction    sa;
struct sigevent     sig_spec;
struct itimerspec   tmr_setting;

// Thread start
pthread_cond_t start_signal;
pthread_mutex_t main_start_lock;
bool bMainStart = false;
// Conflict mutex
pthread_mutex_t conflict_lock;

/************************ Check Functions ******************************/ void check(const char *file, int line, const char *service,
int status, int err)

{
    if (status >= 0)
        return;

    fprintf(stderr, "%s:%d: %s: %s\n", file, line, service, strerror(err));
    exit(EXIT_FAILURE);
}
#define check_pthread(expr)                \
    ({                                                \
        int _status = (expr);                 \
        check(__FILE__, __LINE__, #expr, -_status, _status);        \
    })
#define check_unix(expr) \
    check(__FILE__, __LINE__, #expr, (expr), errno)

/************************ Functions ******************************/ void DeleteTimer(timer_t timer)

{
    if (timer!=NULL)
        check_unix(timer_delete(timer));
}

void EndTimeOut (int signo,siginfo_t *info,void*context) {
}

void StartTimeOut (int nb_Sec, int nb_nSec)

{
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = EndTimeOut;

    check_unix(sigaction(SIGRTMIN, &sa, NULL));

    sig_spec.sigev_notify = SIGEV_SIGNAL;
    sig_spec.sigev_signo = SIGRTMIN;
    sig_spec.sigev_value.sival_ptr = NULL;

    check_unix(timer_create(CLOCK_REALTIME, &sig_spec,
                            &timer_h));

    tmr_setting.it_value.tv_sec = nb_Sec;
    tmr_setting.it_value.tv_nsec = nb_nSec;
    tmr_setting.it_interval.tv_sec = 0;
    tmr_setting.it_interval.tv_nsec = 0;

    check_unix(timer_settime(timer_h, 0,
                             &tmr_setting,NULL));
}

/************************** Threads ********************************/ void* threadTimeOut(void * arg)

{
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));

    printf("threadTimeOut : Start of a timeout\n");
    StartTimeOut(0,500000000);

    // threadTimeOut waits the conflict_lock unlock of threadDisplay
    // With priority inheritance, threadTimeOut gives its priority to
    // threadDisplay. But, threadTimeOut must also do the EndTimeOut()
    // processing. When the timeout end signal arrives, the system crashes.
    check_pthread(pthread_mutex_lock(&conflict_lock));
    printf("threadTimeOut : Hold conflict_lock\n");

    while (1)
    {
        sleep(10);
    }
    return NULL;
}
void* threadDisplay(void * arg) {
    int i=0;

    // threadDisplay locks conflict_lock. Like this, when threadTimeOut
    // wants conflict_lock, it must give its priority to threadDisplay.
    check_pthread(pthread_mutex_lock(&conflict_lock));
    printf("threadDisplay : Hold conflict_lock\n");

    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    printf("threadDisplay : start\n");

    check_unix(sleep(10));  // Pass into Linux Domain

    printf("threadDisplay : end\n");

    check_pthread(pthread_mutex_unlock(&conflict_lock));
    while (1)
    {
          sleep(10);
    }
    return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
    DeleteTimer(timer_h);
    exit(0);
}
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    pthread_t p1;
    pthread_t p2;
    struct sched_param sch;

    check_unix(signal(SIGINT, cleanup_upon_sig));
    check_unix(signal(SIGTERM, cleanup_upon_sig));

    check_unix(mlockall(MCL_CURRENT|MCL_FUTURE));

    // mutex and sem initialisation
    check_pthread(pthread_cond_init(&start_signal, NULL));
    check_pthread(pthread_mutex_init(&main_start_lock, NULL));
    check_pthread(pthread_mutexattr_init(&attr_proto));
    check_pthread(pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT));
    check_pthread(pthread_mutex_init(&conflict_lock, &attr_proto));

    check_pthread(pthread_attr_init(&attr));
    check_pthread(pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED));
    check_pthread(pthread_attr_setschedpolicy(&attr, SCHED_FIFO));

    // TimeOut thread creation
    sch.sched_priority = 80;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p1, &attr, threadTimeOut, NULL));

    // Display thread creation
    sch.sched_priority = 70;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p2, &attr, threadDisplay, NULL));

    check_pthread(pthread_attr_destroy(&attr));

    printf("Main condition broadcast\n");
    // Start of all threads
    check_pthread(pthread_mutex_lock(&main_start_lock));
    bMainStart = true;
    check_pthread(pthread_cond_broadcast(&start_signal));
    check_pthread(pthread_mutex_unlock(&main_start_lock));

    while (1)
    {
        sleep(5);
    }

    return 0;
}

#############################################################################

result on the console :

threadDisplay : Hold conflict_lock
Main condition broadcast
threadTimeOut : Start of a timeout
threadDisplay : start
<---- the system crashes

#############################################################################

Error kernel traces :

Xenomai: fatal: removing non-linked element, holder=c7741c84, qslot=c04fbfc0 at include/xenomai/nucleus/queue.h:728

 CPU  PID    PRI      TIMEOUT  STAT      NAME
>  0  0       -1      0        00500080  ROOT
   0  2830     0      0        00300380  testTimer13_bis
   0  2832    80      0        00300180  testTimer13_bis
   0  2833    70      0        00300380  testTimer13_bis

Master time base: clock=59161137551
c043dd60 00000000 00000000 c7741c84 000000b1 c01037d1 c03bcfed c04fbfc0 c04fbfc0 c013f6e1 c03b8f16 c773c000 c04fbfc0 c03b9014 000002d8 00000086 c7741a20 ffffffff 00000002 00000001 c7741220 c04f5884 c0135c49 c043de38

Call Trace:
 [<c01037d1>] show_stack+0x27/0x2b
 [<c013f6e1>] rpi_update+0x166/0x22e
 [<c0135c49>] xnpod_resume_thread+0x119/0x527
 [<c0136160>] xnpod_unblock_thread+0x48/0x79
 [<c01401af>] sigwake_event+0xa1/0x104
 [<c012f475>] __ipipe_dispatch_event+0xb1/0x174
 [<c014010e>] sigwake_event+0x0/0x104
 [<c0118981>] signal_wake_up+0x3f/0x53
 [<c01192f9>] specific_send_sig_info+0x83/0x8e
 [<c011945e>] send_sig_info+0x3f/0x85
 [<c0149b73>] pse51_signal_handle_request+0x6d/0xd9
 [<c012fb9a>] rthal_apc_handler+0x0/0x2d
 [<c014f9bd>] pse51_lostage_handle_request+0x2b/0x53
 [<c012fbbd>] rthal_apc_handler+0x23/0x2d
 [<c012ebc4>] __xirq_end+0x2a/0x37
 [<c012fb9a>] rthal_apc_handler+0x0/0x2d
 [<c012ebd1>] __virq_end+0x0/0xa7
 [<c0130060>] hal_read_proc+0x55/0x6c
 [<c01087ef>] __ipipe_handle_irq+0x11c/0x13c
 [<c010125b>] default_idle+0x27/0x39
 [<c0101234>] default_idle+0x0/0x39
 [<c0102841>] common_interrupt+0x21/0x40
 [<c0101234>] default_idle+0x0/0x39
 [<c010125b>] default_idle+0x27/0x39
 [<c0100b7c>] cpu_idle+0x42/0x6b
 [<c043e9dc>] start_kernel+0x23b/0x240
 [<c043e323>] unknown_bootoption+0x0/0x195
 =======================

I hope that my test program is easy to understand and that you also have the crash.

Thanks

#28 lore raw
Quoted 7 lines · Fabien MAHOT

I succeed to reduce my test program.
Now, I ve got two xenomai threads.
threadDisplay (prio : 70) : locks a mutex (conflict_lock mutex) and passes between Xenomai and Linux domain. (call to sleep()) threadTimeOut (prio : 80) : Starts one timeout of 5ms and tries to lock the mutex taken by threadDisplay. (Priority Inheritance)

Quoted 5 lines

threadTimeOut waits the conflict_lock unlock of threadDisplay. With priority inheritance, threadTimeOut gives its priority to threadDisplay. But, threadTimeOut must also do the EndTimeOut() processing. When the timeout end signal arrives, if threadDisplay is into Linux Domain, the system crashes.

Do you have the same crash if you use vanilla Linux timer services (__real_timer_create, __real_timer_settime, etc...).

Signature · 1 line
 Gilles
#29 lore raw
Quoted 16 lines
Quoted 8 lines · Fabien MAHOT

I succeed to reduce my test program.
Now, I ve got two xenomai threads.
threadDisplay (prio : 70) : locks a mutex (conflict_lock mutex) and passes
between Xenomai and Linux domain. (call to sleep()) threadTimeOut (prio : 80) : Starts one timeout of 5ms and tries to lock the mutex taken by threadDisplay. (Priority Inheritance)

Quoted 5 lines

threadTimeOut waits the conflict_lock unlock of threadDisplay. With priority inheritance, threadTimeOut gives its priority to threadDisplay. But, threadTimeOut must also do the EndTimeOut() processing. When the timeout end signal arrives, if threadDisplay is into Linux Domain, the system crashes.

Quoted 5 lines

Do you have the same crash if you use vanilla Linux timer services (__real_timer_create, __real_timer_settime, etc...).

--
Gilles

Hello,

I try with vanilla linux timer. If threadTimeOut sets up one or two timeouts, now, it's ok. But if it sets up more than two timeouts, the system crashes.
I ve got the same error kernel traces as Posix timer use.

(By the way, In which file vanilla timer services are written ? __real_timer_create ... are declared in time.h in xenomai files. I look for in linux and glibc sources but I don t find them.)

I send you a test program in which severals timeouts can be created.

##############################################################################################

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>

// Timer

struct stTimeOut {
    timer_t timer_h;
    struct sigaction    sa;
    struct sigevent     sig_spec;
    struct itimerspec   tmr_setting;
    int number;
}timeOut0, timeOut1, timeOut2, timeOut3, timeOut4, timeOut5;

// Thread start
pthread_cond_t start_signal;
pthread_mutex_t main_start_lock;
bool bMainStart = false;
// Conflict mutex
pthread_mutex_t conflict_lock;

/************************ Check Functions ******************************/ void check(const char *file, int line, const char *service,
int status, int err)

{
    if (status >= 0)
        return;

    fprintf(stderr, "%s:%d: %s: %s\n", file, line, service, strerror(err));
    exit(EXIT_FAILURE);
}
#define check_pthread(expr)                \
    ({                                                \
        int _status = (expr);                 \
        check(__FILE__, __LINE__, #expr, -_status, _status);        \
    })
#define check_unix(expr) \
    check(__FILE__, __LINE__, #expr, (expr), errno)

/************************ Functions ******************************/ void DeleteTimer(timer_t timer)

{
    if (timer!=NULL)
        check_unix(__real_timer_delete(timer));
}

void EndTimeOut (int signo,siginfo_t *info,void*context) {
}

void StartTimeOut (int nb_Sec, int nb_nSec, struct stTimeOut timeOut)

{
    (timeOut.sa).sa_flags = SA_SIGINFO;
    (timeOut.sa).sa_sigaction = EndTimeOut;

    check_unix(sigaction(SIGRTMIN, &(timeOut.sa), NULL));

    (timeOut.sig_spec).sigev_notify = SIGEV_SIGNAL;
    (timeOut.sig_spec).sigev_signo = SIGRTMIN;
    (timeOut.sig_spec).sigev_value.sival_ptr = NULL;

    check_unix(__real_timer_create(CLOCK_REALTIME, &(timeOut.sig_spec),
                            &(timeOut.timer_h)));

    (timeOut.tmr_setting).it_value.tv_sec = nb_Sec;
    (timeOut.tmr_setting).it_value.tv_nsec = nb_nSec;
    (timeOut.tmr_setting).it_interval.tv_sec = 0;
    (timeOut.tmr_setting).it_interval.tv_nsec = 0;

    check_unix(__real_timer_settime((timeOut.timer_h), 0,
                             &(timeOut.tmr_setting),NULL));
}

/************************** Threads ********************************/ void* threadTimeOut(void * arg)

{
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));

    printf("threadTimeOut : Start of timeouts\n");
    StartTimeOut(0,500000000,timeOut0);
    StartTimeOut(0,500000000,timeOut1);
    StartTimeOut(0,500000000,timeOut2);
    //StartTimeOut(0,500000000,timeOut3);
    //StartTimeOut(0,500000000,timeOut4);
    //StartTimeOut(0,500000000,timeOut5);

    // threadTimeOut waits the conflict_lock unlock of threadDisplay
    // With priority inheritance, threadTimeOut gives its priority to
    // threadDisplay. But, threadTimeOut must also do the EndTimeOut()
    // processing. When the timeout end signal arrives, the system crashes.
    check_pthread(pthread_mutex_lock(&conflict_lock));
    printf("threadTimeOut : Hold conflict_lock\n");

    while (1)
    {
        sleep(10);
    }
    return NULL;
}

void* threadDisplay(void * arg)

{
    // threadDisplay locks conflict_lock. Like this, when threadTimeOut
    // wants conflict_lock, it must give its priority to threadDisplay.
    check_pthread(pthread_mutex_lock(&conflict_lock));
    printf("threadDisplay : Hold conflict_lock\n");

    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    printf("Display thread\n");

    sleep(10);

    printf("End of display thread\n");

    check_pthread(pthread_mutex_unlock(&conflict_lock));
    while (1)
    {
          sleep(10);
    }
    return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
    DeleteTimer(timeOut0.timer_h);
    DeleteTimer(timeOut1.timer_h);
    DeleteTimer(timeOut2.timer_h);
    DeleteTimer(timeOut3.timer_h);
    DeleteTimer(timeOut4.timer_h);
    DeleteTimer(timeOut5.timer_h);
    exit(0);
}
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    pthread_t p1;
    pthread_t p2;
    struct sched_param sch;

    check_unix(signal(SIGINT, cleanup_upon_sig));
    check_unix(signal(SIGTERM, cleanup_upon_sig));

    check_unix(mlockall(MCL_CURRENT|MCL_FUTURE));

    // mutex and sem initialisation
    check_pthread(pthread_cond_init(&start_signal, NULL));
    check_pthread(pthread_mutex_init(&main_start_lock, NULL));
    check_pthread(pthread_mutexattr_init(&attr_proto));
    check_pthread(pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT));
    check_pthread(pthread_mutexattr_setpshared(&attr_proto,PTHREAD_PROCESS_SHARED));
    check_pthread(pthread_mutex_init(&conflict_lock, &attr_proto));

    check_pthread(pthread_attr_init(&attr));
    check_pthread(pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED));
    check_pthread(pthread_attr_setschedpolicy(&attr, SCHED_FIFO));

    // TimeOut thread creation
    sch.sched_priority = 80;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p1, &attr, threadTimeOut, NULL));

    // Display thread creation
    sch.sched_priority = 70;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p2, &attr, threadDisplay, NULL));

    check_pthread(pthread_attr_destroy(&attr));

    printf("Main condition broadcast\n");
    // Start of all threads
    check_pthread(pthread_mutex_lock(&main_start_lock));
    bMainStart = true;
    check_pthread(pthread_cond_broadcast(&start_signal));
    check_pthread(pthread_mutex_unlock(&main_start_lock));

    while (1)
    {
        sleep(5);
    }

    return 0;
}

##############################################################################################

Error kernel traces :

Xenomai: fatal: removing non-linked element, holder=c7741484, qslot=c04fbfc0 at include/xenomai/nucleus/queue.h:728

 CPU  PID    PRI      TIMEOUT  STAT      NAME
>  0  0       -1      0        00500080  ROOT
   0  2948     0      0        00300380  testTimer13
   0  2950    80      0        00300180  testTimer13
   0  2951    70      0        00300380  testTimer13

Master time base: clock=203530244921
c043dd40 00000000 00000000 c7741484 000000b1 c01037d1 c03bcfed c04fbfc0 c04fbfc0 c013f6e1 c03b8f16 c773c000 c04fbfc0 c03b9014 000002d8 00000086 c7741220 ffffffff 00000002 00000001 c7740820 c04f5884 c0135c49 00000000

Call Trace:
 [<c01037d1>] show_stack+0x27/0x2b
 [<c013f6e1>] rpi_update+0x166/0x22e
 [<c0135c49>] xnpod_resume_thread+0x119/0x527
 [<c0136160>] xnpod_unblock_thread+0x48/0x79
 [<c01401af>] sigwake_event+0xa1/0x104
 [<c012f475>] __ipipe_dispatch_event+0xb1/0x174
 [<c014010e>] sigwake_event+0x0/0x104
 [<c0118981>] signal_wake_up+0x3f/0x53
 [<c011a91e>] send_group_sigqueue+0xcc/0x11b
 [<c011f617>] posix_timer_fn+0x52/0xcb
 [<c011f5c5>] posix_timer_fn+0x0/0xcb
 [<c0122856>] hrtimer_run_queues+0x104/0x172
 [<c0117676>] run_timer_softirq+0x12/0x18f
 [<c0125e34>] tick_handle_periodic+0xf/0x5f
 [<c0114a4e>] __do_softirq+0x39/0x80
 [<c0114ac9>] do_softirq+0x34/0x4a
 [<c0114bfa>] irq_exit+0x25/0x30
 [<c01040b7>] do_IRQ+0x51/0x63
 [<c012eb5d>] __ipipe_sync_stage+0xb4/0xf1
 [<c0104066>] do_IRQ+0x0/0x63
 [<c012eb95>] __ipipe_sync_stage+0xec/0xf1
 [<c0104066>] do_IRQ+0x0/0x63
 [<c012eb9a>] __xirq_end+0x0/0x37
 [<c012ecd3>] ipipe_suspend_domain+0x5b/0x78
 [<c012ed3f>] __ipipe_walk_pipeline+0x4f/0x8d
 [<c01087ef>] __ipipe_handle_irq+0x11c/0x13c
 [<c010125b>] default_idle+0x27/0x39
 [<c0101234>] default_idle+0x0/0x39
 [<c0102841>] common_interrupt+0x21/0x40
 [<c0101234>] default_idle+0x0/0x39
 [<c010125b>] default_idle+0x27/0x39
 [<c0100b7c>] cpu_idle+0x42/0x6b
 [<c043e9dc>] start_kernel+0x23b/0x240
 [<c043e323>] unknown_bootoption+0x0/0x195
 =======================

##############################################################################################

Result on the console :

threadDisplay : Hold conflict_lock
Main condition broadcast
Start of timeouts
Display thread
<----- the system is crashed

##############################################################################################

thanks

#30 lore raw
Quoted 24 lines · Fabien MAHOT
Quoted 16 lines
Quoted 8 lines · Fabien MAHOT

I succeed to reduce my test program.
Now, I ve got two xenomai threads.
threadDisplay (prio : 70) : locks a mutex (conflict_lock mutex) and passes
between Xenomai and Linux domain. (call to sleep()) threadTimeOut (prio : 80) : Starts one timeout of 5ms and tries to lock the mutex taken by threadDisplay. (Priority Inheritance)

Quoted 5 lines

threadTimeOut waits the conflict_lock unlock of threadDisplay. With priority inheritance, threadTimeOut gives its priority to threadDisplay. But, threadTimeOut must also do the EndTimeOut() processing. When the timeout end signal arrives, if threadDisplay is into Linux Domain, the system crashes.

Quoted 5 lines

Do you have the same crash if you use vanilla Linux timer services (__real_timer_create, __real_timer_settime, etc...).

--
Gilles

Hello,

Quoted 8 lines

I try with vanilla linux timer. If threadTimeOut sets up one or two timeouts, now, it's ok. But if it sets up more than two timeouts, the system crashes.
I ve got the same error kernel traces as Posix timer use.

(By the way, In which file vanilla timer services are written ? __real_timer_create ... are declared in time.h in xenomai files. I look for in linux and glibc sources but I don t find them.)

Read explanation of the --wrap option in ld documentation.

Signature · 1 line
 Gilles
#31 lore raw
In reply to Fabien MAHOT
Quoted 29 lines · Fabien MAHOT
Quoted 16 lines
Quoted 8 lines · Fabien MAHOT

I succeed to reduce my test program.
Now, I ve got two xenomai threads.
threadDisplay (prio : 70) : locks a mutex (conflict_lock mutex) and passes
between Xenomai and Linux domain. (call to sleep()) threadTimeOut (prio : 80) : Starts one timeout of 5ms and tries to lock the mutex taken by threadDisplay. (Priority Inheritance)

Quoted 5 lines

threadTimeOut waits the conflict_lock unlock of threadDisplay. With priority inheritance, threadTimeOut gives its priority to threadDisplay. But, threadTimeOut must also do the EndTimeOut() processing. When the timeout end signal arrives, if threadDisplay is into Linux Domain, the system crashes.

Quoted 5 lines

Do you have the same crash if you use vanilla Linux timer services (__real_timer_create, __real_timer_settime, etc...).

--
Gilles

Hello,

I try with vanilla linux timer. If threadTimeOut sets up one or two timeouts, now, it's ok. But if it sets up more than two timeouts, the system crashes.
I ve got the same error kernel traces as Posix timer use.

Quoted 5 lines

(By the way, In which file vanilla timer services are written ? __real_timer_create ... are declared in time.h in xenomai files. I look for in linux and glibc sources but I don t find them.)

I send you a test program in which severals timeouts can be created.

Ok. Got it now. The error you have is fixed in the v2.4.x branch. So, here is a patch that you can apply to the v2.4.3 release.

Signature · 1 line
					    Gilles.
nucleus/shadow.c +5 -3
Index: ksrc/nucleus/shadow.c
===================================================================
@@ -276,9 +276,11 @@ static void rpi_update(xnthread_t *threa
 
 	xnlock_get_irqsave(&rpislot->lock, s);
 
-	sched_removepq(&rpislot->threadq, &thread->xlink);
-	rpi_none(thread);
-	rpi_push(thread, cpu);
+	if (rpi_p(thread)) {
+		sched_removepq(&rpislot->threadq, &thread->xlink);
+		rpi_none(thread);
+		rpi_push(thread, cpu);
+	}
 
 	xnlock_put_irqrestore(&rpislot->lock, s);
 }
#32 lore raw

Hello,

I tried your new patch and, now there is no longer issue of priority inheritance with a mutex. Thanks a lot for that.

However, there are still problems with my big application. It still crashes.

Quoted 1 line

From the test program that you corrected (with check functions), I

succeeded to reproduce them.

Write function returns "Interrrupt system call" error (EINTR). this is normal.

but I ve got the same error message with pthread_mutex_unlock. In the specification of this function, there is a note about that : "These functions shall not return an error code of [EINTR]." (these functions are pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_trylock)

So I ve got 3 error types :
pthread_mutex_unlock returns "Interrrupt system call" and "Unknown error 512". When there is "Unknown error 512" message, there are "Bug :
scheduling while atomic" kernel traces.
In the last error type, My test program is stopped without display of a function error return code and there are also "Bug : scheduling while atomic" kernel traces.

In the test program, there are three Xenomai thread :
threadTimeOutEnd (prio : 85): This thread waits the timeout end thanks to a Semaphore (In the timeout end handler, the Semaphore is posted), and warns threadTimeOut with a condvar broadcast. This thread calls display function to debug the program.
threadTimeOut (prio : 80): Malloc memory space to create timeout. Set-up timeouts of 5ms. Free memory space of achieved timeouts (This thread is notified of timeout end with a condvar broadcasted by threadTimeOutEnd). This thread must do the timeout end handler processing.This thread calls display function to debug the program.
threadDisplay (prio : 70): Call display function in a loop.

This is the program : (sorry, it is a bit long. I can t reduce it)

##############################################################

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>

#define NB_PTR_TEMPO 5 // 5 timeouts maximum
#define STACKSIZE 350

// stack
static int Stack[STACKSIZE];
static unsigned short Write_ptr = 0;
static unsigned short Read_ptr = 0;

// Display
pthread_mutex_t lockDisplay;
unsigned char bufferDisplay[2048];

// Timer

struct stTimeOut {
    timer_t timer_h;
    struct sigaction    sa;
    struct sigevent     sig_spec;
    struct itimerspec   tmr_setting;
    int number;
}*timeOut0_ptr, *timeOut1_ptr, *timeOut2_ptr, *timeOut3_ptr, *timeOut4_ptr;

// Thread start
pthread_cond_t start_signal;
pthread_mutex_t main_start_lock;
bool bMainStart = false;
// Time-out end
pthread_cond_t TimeOutEnd_signal;
pthread_mutex_t timeOutEnd_lock;
bool bTimeOutEnd = false;
sem_t TimeOutWait_sem;

// Stack mutex
pthread_mutex_t Stack_lock;

void check(const char *file, int line, const char *service,
int status, int err)

{
    if (status >= 0)
        return;

    fprintf(stderr, "%s:%d: %s: %s\n", file, line, service, strerror(err));
    exit(EXIT_FAILURE);
}
#define check_pthread(expr)                \
    ({                                                \
        int _status = (expr);                 \
        check(__FILE__, __LINE__, #expr, -_status, _status);        \
    })
#define check_unix(expr) \
    ({                                                \
        int _status = (expr);                 \
        check(__FILE__, __LINE__, #expr, _status, errno);  \
    })

/************************ Stack functions *************************/

int StackCreation(void)

{
    Write_ptr = 0;
    Read_ptr  = 0;
    return 0;
}

void StackWrite(int number)

{
    if (Write_ptr >= STACKSIZE)
        Write_ptr = 0;
    Stack[Write_ptr++] = number;
}

int StackRead(void)

{
    int number;
    if (Read_ptr >= STACKSIZE)
        Read_ptr = 0;
    number = Stack[Read_ptr++];
    return number;
}

unsigned short GetWritePtr(void)

{
    return Write_ptr;
}

unsigned short GetReadPtr(void)

{
    return Read_ptr;
}

/************************ Functions ******************************/ void display(char * chaine,...)

{
    check_pthread(pthread_mutex_lock(&lockDisplay));
    va_list ArgDisplay;
    va_start(ArgDisplay, chaine);
    check_unix(vsprintf((char *)bufferDisplay,chaine,ArgDisplay));
    bool bErrWrite = false;
    do
	{
      bErrWrite = false;
      if (write(2, (char*)bufferDisplay, strlen((char *)bufferDisplay)) < 0)
      {
         int err = errno;
         printf("display : erreur write : %d, %s\n",err,strerror(err));
         if (err == EINTR)
         {
              bErrWrite = true;
         }
         else
         {
             exit(0);
         }
      }
    }while (bErrWrite);
    check_pthread(pthread_mutex_unlock(&lockDisplay));
}

int func(volatile int* i)

{
    return (*i)++;
}

void DeleteTimer(timer_t timer)

{
    if (timer!=NULL)
        check_unix(__real_timer_delete(timer));
}

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
    volatile int i, result = 0;

    DeleteTimer(((struct stTimeOut*)(info->si_value.sival_ptr))->timer_h);

    StackWrite(((struct stTimeOut*)(info->si_value.sival_ptr))->number);

    check_unix(sem_post(&TimeOutWait_sem));
}

void StartTimeOut (int nb_Sec, int nb_nSec, struct stTimeOut* timeOut)

{
    (timeOut->sa).sa_flags = SA_SIGINFO;
    (timeOut->sa).sa_sigaction = EndTimeOut;

    check_unix(sigaction(SIGRTMIN, &(timeOut->sa), NULL));

    (timeOut->sig_spec).sigev_notify = SIGEV_SIGNAL;
    (timeOut->sig_spec).sigev_signo = SIGRTMIN;
    (timeOut->sig_spec).sigev_value.sival_ptr = timeOut;

    check_unix(__real_timer_create(CLOCK_REALTIME, &(timeOut->sig_spec),
                            &(timeOut->timer_h)));

    (timeOut->tmr_setting).it_value.tv_sec = nb_Sec;
    (timeOut->tmr_setting).it_value.tv_nsec = nb_nSec;
    (timeOut->tmr_setting).it_interval.tv_sec = 0;
    (timeOut->tmr_setting).it_interval.tv_nsec = 0;

    check_unix(__real_timer_settime((timeOut->timer_h), 0,
                             &(timeOut->tmr_setting),NULL));
}

/************************** Threads ********************************/ void* threadTimeOutEnd(void * arg) {

    int NbSem = 0;
    bool bSemWaitError;

    display("TimeOutEnd thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart) {
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    }
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("TimeOutEnd thread\n");

    while (1) {
    do
    {
      bSemWaitError = false;
      if (sem_wait (&TimeOutWait_sem)<0)
      {
	int err = errno;
        display("Semaphore::Get - erreur sem_wait : errno :%d -> %s\n"
                                                 ,err,strerror(err));
        if (err == EINTR)
	{
	  bSemWaitError = true;
        }
      }
    }while (bSemWaitError);

    check_pthread(pthread_mutex_lock(&timeOutEnd_lock));
    check_pthread(pthread_cond_broadcast(&TimeOutEnd_signal));
    bTimeOutEnd = true;
    check_pthread(pthread_mutex_unlock(&timeOutEnd_lock));
    }
}

void* threadTimeOut(void * arg) {
int i=1;
int j, k, NbTimeOut, numTimeOut;

timeOut0_ptr = timeOut1_ptr = timeOut2_ptr = timeOut3_ptr =
timeOut4_ptr = NULL;

   display("TimeOut thread\n");
   check_pthread(pthread_mutex_lock(&main_start_lock));
   while (!bMainStart)
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
   check_pthread(pthread_mutex_unlock(&main_start_lock));
   display("TimeOut thread\n");
    while (i < 100) {
        // Malloc and start of time out
        for (j=0 ; j < NB_PTR_TEMPO; j++) {
          switch(j) {
          case 0 : if (timeOut0_ptr == NULL) {
                  timeOut0_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                  if (timeOut0_ptr == NULL)
                  {
                       display("0 : Malloc error\n");
                       exit(1);
                  }
                  timeOut0_ptr->number = i;
                  i++;
                  display("0 : Start of time out %d - 5ms\n",
                           timeOut0_ptr->number);
                  StartTimeOut(0,500000000,timeOut0_ptr);
                }
                break;
           case 1 : if (timeOut1_ptr == NULL) {
                  timeOut1_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                  if (timeOut1_ptr == NULL)
                  {
                       display("1 : Malloc error\n");
                       exit(1);
                  }
                  timeOut1_ptr->number = i;
                  i++;
                  display("1 : Start of time out %d - 5ms\n",
                          timeOut1_ptr->number);
                  StartTimeOut(0,500000000,timeOut1_ptr);
              }
              break;
          case 2 : if (timeOut2_ptr == NULL) {
                  timeOut2_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                  if (timeOut2_ptr == NULL)
                  {
                       display("2 : Malloc error\n");
                       exit(1);
                  }
                  timeOut2_ptr->number = i;
                  i++;
                  display("2 : Start of time out %d - 5ms\n",
                          timeOut2_ptr->number);
                  StartTimeOut(0,500000000,timeOut2_ptr);
              }
              break;
          case 3 : if (timeOut3_ptr == NULL) {
                  timeOut3_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                  if (timeOut3_ptr == NULL)
                  {
                       display("3 : Malloc error\n");
                       exit(1);
                  }
                  timeOut3_ptr->number = i;
                  i++;
                  display("3 : Start of time out %d - 5ms\n",
                            timeOut3_ptr->number);
                  StartTimeOut(0,500000000,timeOut3_ptr);
              }
              break;
          case 4 : if (timeOut4_ptr == NULL) {
                  timeOut4_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                  if (timeOut4_ptr == NULL)
                  {
                       display("4 : Malloc error\n");
                       exit(1);
                  }
                  timeOut4_ptr->number = i;
                  i++;
                  display("4 : Start of time out %d - 5ms\n",
                         timeOut4_ptr->number);
                  StartTimeOut(0,500000000,timeOut4_ptr);
              }
              break;
          }
      }
      check_pthread(pthread_mutex_lock(&timeOutEnd_lock));
      while (!bTimeOutEnd){
          check_pthread(pthread_cond_wait(&TimeOutEnd_signal,
                                          &timeOutEnd_lock));
          }
      bTimeOutEnd = false;
      check_pthread(pthread_mutex_unlock(&timeOutEnd_lock));
      NbTimeOut = GetWritePtr() - GetReadPtr();
      display("Number of time-outs ends : %d\n", NbTimeOut);
      for (j=0; j < NbTimeOut; j++) {
            numTimeOut = StackRead();
            display("TimeOut%d ends\n", numTimeOut);
            for (k=0; k < NB_PTR_TEMPO; k++) {
                switch(k) {
                case 0 : if (timeOut0_ptr != NULL) {
                        if (timeOut0_ptr->number == numTimeOut) {
                            free(timeOut0_ptr);
                            timeOut0_ptr = NULL;
                        }
                    }
                    break;
                case 1 : if (timeOut1_ptr != NULL) {
                        if (timeOut1_ptr->number == numTimeOut) {
                            free(timeOut1_ptr);
                            timeOut1_ptr = NULL;
                        }
                    }
                    break;
                case 2 : if (timeOut2_ptr != NULL) {
                        if (timeOut2_ptr->number == numTimeOut) {
                            free(timeOut2_ptr);
                            timeOut2_ptr = NULL;
                        }
                    }
                    break;
                case 3 : if (timeOut3_ptr != NULL) {
                        if (timeOut3_ptr->number == numTimeOut) {
                            free(timeOut3_ptr);
                            timeOut3_ptr = NULL;
                        }
                    }
                    break;
                case 4 : if (timeOut4_ptr != NULL) {
                        if (timeOut4_ptr->number == numTimeOut) {
                            free(timeOut4_ptr);
                            timeOut4_ptr = NULL;
                        }
                    }
                    break;
                }
            }
        }
    }
    while(1) {
        sleep(10);
    }
    return NULL;
}
void* threadDisplay(void * arg) {
    volatile int i=0;
    volatile int result;
    display("Display thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("Display thread\n");

    while (i <= 100000) {
        result = func(&i);
        display("Display thread :%d \r",result);
    }
    display("End of display thread\n");
    return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
    if (timeOut0_ptr)
        DeleteTimer(timeOut0_ptr->timer_h);
    if (timeOut1_ptr)
        DeleteTimer(timeOut1_ptr->timer_h);
    if (timeOut2_ptr)
        DeleteTimer(timeOut2_ptr->timer_h);
    if (timeOut3_ptr)
        DeleteTimer(timeOut3_ptr->timer_h);
    if (timeOut4_ptr)
        DeleteTimer(timeOut4_ptr->timer_h);
    exit(0);
}
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    pthread_t p0;
    pthread_t p1;
    pthread_t p2;
    struct sched_param sch;

    // Stack creation
    StackCreation();

    check_unix(signal(SIGINT, cleanup_upon_sig));
    check_unix(signal(SIGTERM, cleanup_upon_sig));

    check_unix(mlockall(MCL_CURRENT|MCL_FUTURE));

    // mutex and sem initialisation
    check_pthread(pthread_cond_init(&start_signal, NULL));
    check_pthread(pthread_mutex_init(&main_start_lock, NULL));
    check_pthread(pthread_cond_init(&TimeOutEnd_signal, NULL));
    check_pthread(pthread_mutex_init(&timeOutEnd_lock, NULL));
    check_unix(sem_init (&TimeOutWait_sem, 0,0));
    check_pthread(pthread_mutexattr_init(&attr_proto));
    check_pthread(pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT));
    check_pthread(pthread_mutex_init(&lockDisplay, &attr_proto));

    check_pthread(pthread_attr_init(&attr));
    check_pthread(pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED));
    check_pthread(pthread_attr_setschedpolicy(&attr, SCHED_FIFO));

    // TimeOutEnd thread creation
    sch.sched_priority = 85;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p0, &attr, threadTimeOutEnd, NULL));

    // TimeOut thread creation
    sch.sched_priority = 80;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p1, &attr, threadTimeOut, NULL));

    // Display thread creation
    sch.sched_priority = 70;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p2, &attr, threadDisplay, NULL));

    check_pthread(pthread_attr_destroy(&attr));

    display("Main condition broadcast\n");
    // Start of all threads
    check_pthread(pthread_mutex_lock(&main_start_lock));
    bMainStart = true;
    check_pthread(pthread_cond_broadcast(&start_signal));
    check_pthread(pthread_mutex_unlock(&main_start_lock));

    while (1) {
        sleep(5);
    }

    return 0;
}

##################################################################################

-->> First type of error (pthread_mutex_unlock returns Interrupted system call)

-> Result on the console :

TimeOutCreation thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutCreation thread
TimeOut thread
0 : Start of time out 1 - 5ms
Display thread
1 : Start of time out 2 - 5ms
2 : Start of time out 3 - 5ms
3 : Start of time out 4 - 5ms
4 : Start of time out 5 - 5ms
Semaphore::Get - erreur sem_wait : errno :4 -> Interrupted system call Number of time-outs ends : 5
TimeOut1 ends
TimeOut2 ends
TimeOut3 ends
TimeOut4 ends
TimeOut5 ends
...
0 : Start of time out 26 - 5ms
1 : Start of time out 27 - 5ms
Semaphore::Get - erreur sem_wait : errno :4 -> Interrupted system call Number of time-outs ends : 3
TimeOut23 ends
TimeOut24 ends
TimeOut25 ends
2 : Start of time out 28 - 5ms
3 : Start of time out 29 - 5ms
4 : Start of time out 30 - 5ms
display : erreur write : 4, Interrupted system call
Number of time-outs ends : 1
TimeOut26 ends
0 : Start of time out 31 - 5ms
Number of time-outs ends : 1
TimeOut27 ends
1 : Start of time out 32 - 5ms
testTimer12_corrected.c:169: pthread_mutex_unlock(&lockDisplay):
Interrupted system call <--- Stop of the program (exit(EXIT_FAILURE) of
check function)

-> Kernel traces (Normal) :

Xenomai: Posix: destroying semaphore c7760910.
Xenomai: Posix: destroying mutex c7760850.
Xenomai: Posix: destroying mutex c77608d0.
Xenomai: Posix: destroying mutex c7760950.
Xenomai: Posix: destroying condition variable c7760810.
Xenomai: Posix: destroying condition variable c7760890.

##################################################################################

-->> Second type of error (pthread_mutex_unlock returns Unknown error 512)

-> Result on the console :

TimeOutCreation thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutCreation thread
TimeOut thread
0 : Start of time out 1 - 5ms
Display thread
1 : Start of time out 2 - 5ms
2 : Start of time out 3 - 5ms
3 : Start of time out 4 - 5ms
4 : Start of time out 5 - 5ms
testTimer12_corrected.c:169: pthread_mutex_unlock(&lockDisplay): Unknown
error 512 <--- Stop of the program (exit(EXIT_FAILURE) of check
function)

-> Kernel traces :

BUG: scheduling while atomic: testTimer12_cor/0x00000002/3083
 [<c0362553>] schedule+0x7d/0x354
 [<c010ddc6>] __wake_up_sync+0x58/0x79
 [<c0140b7f>] xnshadow_harden+0x8a/0x1c0
 [<c02347e0>] tty_ldisc_deref+0x7e/0x9b
 [<c0140d28>] losyscall_event+0x73/0x13a
 [<c012f475>] __ipipe_dispatch_event+0xb1/0x174
 [<c0140cb5>] losyscall_event+0x0/0x13a
 [<c0108a4c>] __ipipe_syscall_root+0x6b/0xd1
 [<c0102589>] system_call+0x29/0x4a
 =======================

note: testTimer12_cor[3083] exited with preempt_count 1
Xenomai: Posix: destroying semaphore c7760910.
Xenomai: Posix: destroying mutex c7760850.
Xenomai: Posix: destroying mutex c77608d0.
Xenomai: Posix: destroying mutex c7760950.
Xenomai: Posix: destroying condition variable c7760810.
Xenomai: Posix: destroying condition variable c7760890.

##################################################################################

-->> Third type of error

-> Result on the console :

TimeOutCreation thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutCreation thread
TimeOut thread
0 : Start of time out 1 - 5ms
Display thread
1 : Start of time out 2 - 5ms
2 : Start of time out 3 - 5ms
3 : Start of time out 4 - 5ms
4 : Start of time out 5 - 5ms
Semaphore::Get - erreur sem_wait : errno :4 -> Interrupted system call Number of time-outs ends : 5
TimeOut1 ends
TimeOut2 ends
TimeOut3 ends
TimeOut4 ends
TimeOut5 ends
...
0 : Start of time out 16 - 5ms
1 : Start of time out 17 - 5ms
2 : Start of time out 18 - 5ms
3 : Start of time out 19 - 5ms
4 : Start of time out 20 - 5ms
Display tSemaphore::Get - erreur sem_wait : errno :4 -> Interrupted system call
Number of time-outs ends : 1
TimeOut16 ends
0 : Start of time out 21 - 5ms
Number of time-outs ends : 4
TimeOut17 ends
TimeOut18 ends
TimeOut19 ends
TimeOut20 ends
1 : Start of time out 22 - 5ms
2 : Start of time out 23 - 5ms
3 : Start of time out 24 - 5ms
4 : Start of time out 25 - 5ms <--- Stop of the program

-> Kernel traces :

BUG: scheduling while atomic: testTimer12_cor/0x00000002/3108
 [<c0362553>] schedule+0x7d/0x354
 [<c010ddc6>] __wake_up_sync+0x58/0x79
 [<c0140b7f>] xnshadow_harden+0x8a/0x1c0
 [<c02347e0>] tty_ldisc_deref+0x7e/0x9b
 [<c0140d28>] losyscall_event+0x73/0x13a
 [<c012f475>] __ipipe_dispatch_event+0xb1/0x174
 [<c0140cb5>] losyscall_event+0x0/0x13a
 [<c0108a4c>] __ipipe_syscall_root+0x6b/0xd1
 [<c0102589>] system_call+0x29/0x4a
 =======================

note: testTimer12_cor[3108] exited with preempt_count 1
Xenomai: Posix: destroying semaphore c7760910.
Xenomai: Posix: destroying mutex c7760850.
Xenomai: Posix: destroying mutex c77608d0.
Xenomai: Posix: destroying mutex c7760950.
Xenomai: Posix: destroying condition variable c7760810.
Xenomai: Posix: destroying condition variable c7760890.

##################################################################################

I think that second and third error types are the same problem. Sometimes, the program is also successfully executed.

What do you think about these traces ?

Thank you

#33 lore raw
Quoted 10 lines · Fabien MAHOT

Hello,

I tried your new patch and, now there is no longer issue of priority inheritance with a mutex. Thanks a lot for that.

However, there are still problems with my big application. It still crashes.

From the test program that you corrected (with check functions), I succeeded to reproduce them.

Quoted 7 lines

Write function returns "Interrrupt system call" error (EINTR). this is normal.

but I ve got the same error message with pthread_mutex_unlock. In the specification of this function, there is a note about that : "These functions shall not return an error code of [EINTR]." (these functions are pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_trylock)

Actually I do not know how this can happen, since the EINTR error is trapped inside pthread_mutex_lock. Will try your example.

Quoted 5 lines

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
   volatile int i, result = 0;

   DeleteTimer(((struct stTimeOut*)(info->si_value.sival_ptr))->timer_h);

This is incorrect, timer_delete is not async signal safe.

Quoted 5 lines
   StackWrite(((struct stTimeOut*)(info->si_value.sival_ptr))->number);
   check_unix(sem_post(&TimeOutWait_sem));
}
Signature · 1 line
 Gilles
#34 lore raw
Quoted 15 lines · Gilles Chanteperdrix
Quoted 13 lines · Fabien MAHOT

Hello,

I tried your new patch and, now there is no longer issue of priority inheritance with a mutex. Thanks a lot for that.

However, there are still problems with my big application. It still crashes.

From the test program that you corrected (with check functions), I succeeded to reproduce them.

Write function returns "Interrrupt system call" error (EINTR). this is normal.

Quoted 7 lines
Quoted 4 lines

but I ve got the same error message with pthread_mutex_unlock. In the specification of this function, there is a note about that : "These functions shall not return an error code of [EINTR]." (these functions are pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_trylock)

Actually I do not know how this can happen, since the EINTR error is trapped inside pthread_mutex_lock. Will try your example.

Ah, I see, it is pthread_mutex_unlock which returns EINTR, not pthread_mutex_lock. I will change this. In the meantime, you can simply ignore the EINTR error: the mutex unlock succeeeded even if it returns EINTR.

Signature · 1 line
 Gilles
#35 lore raw
Quoted 27 lines
Quoted 16 lines · Gilles Chanteperdrix
Quoted 14 lines · Fabien MAHOT

Hello,

I tried your new patch and, now there is no longer issue of priority inheritance with a mutex. Thanks a lot for that.

However, there are still problems with my big application. It still crashes.

From the test program that you corrected (with check functions), I succeeded to reproduce them.

Write function returns "Interrrupt system call" error (EINTR). this is normal.

Quoted 8 lines
Quoted 5 lines

but I ve got the same error message with pthread_mutex_unlock. In the specification of this function, there is a note about that : "These functions shall not return an error code of [EINTR]." (these functions are
pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_trylock)

Actually I do not know how this can happen, since the EINTR error is trapped inside pthread_mutex_lock. Will try your example.

Quoted 8 lines

Ah, I see, it is pthread_mutex_unlock which returns EINTR, not pthread_mutex_lock. I will change this. In the meantime, you can simply ignore the EINTR error: the mutex unlock succeeeded even if it returns EINTR.

--
Gilles

OK, thank you.

And do you have an idea about the bug traces ?

#36 lore raw
Quoted 32 lines · Fabien MAHOT
Quoted 32 lines
Quoted 16 lines · Gilles Chanteperdrix
Quoted 14 lines · Fabien MAHOT

Hello,

I tried your new patch and, now there is no longer issue of priority inheritance with a mutex. Thanks a lot for that.

However, there are still problems with my big application. It still crashes.

From the test program that you corrected (with check functions), I succeeded to reproduce them.

Write function returns "Interrrupt system call" error (EINTR). this is normal.

Quoted 8 lines
Quoted 5 lines

but I ve got the same error message with pthread_mutex_unlock. In the specification of this function, there is a note about that : "These functions shall not return an error code of [EINTR]." (these functions are
pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_trylock)

Actually I do not know how this can happen, since the EINTR error is trapped inside pthread_mutex_lock. Will try your example.

Ah, I see, it is pthread_mutex_unlock which returns EINTR, not pthread_mutex_lock. I will change this. In the meantime, you can simply ignore the EINTR error: the mutex unlock succeeeded even if it returns EINTR.

Quoted 7 lines
Quoted 3 lines

--
Gilles

OK, thank you.

And do you have an idea about the bug traces ?

No, I have no idea where this 512 come from. Do you still get it if you do not call timer_delete in the signal handler ?

Signature · 1 line
 Gilles
#37 lore raw
Quoted 42 lines
Quoted 40 lines · Fabien MAHOT
Quoted 29 lines
Quoted 24 lines · Gilles Chanteperdrix
Quoted 15 lines · Fabien MAHOT

Hello,

I tried your new patch and, now there is no longer issue of priority inheritance with a mutex. Thanks a lot for that.

However, there are still problems with my big application. It still crashes.

From the test program that you corrected (with check functions), I succeeded to reproduce them.

Write function returns "Interrrupt system call" error (EINTR). this is
normal.

Quoted 6 lines

but I ve got the same error message with pthread_mutex_unlock. In the specification of this function, there is a note about that : "These functions shall not return an error code of [EINTR]." (these functions
are
pthread_mutex_lock, pthread_mutex_unlock, pthread_mutex_trylock)

Quoted 2 lines

Actually I do not know how this can happen, since the EINTR error is trapped inside pthread_mutex_lock. Will try your example.

Quoted 8 lines

Ah, I see, it is pthread_mutex_unlock which returns EINTR, not pthread_mutex_lock. I will change this. In the meantime, you can simply ignore the EINTR error: the mutex unlock succeeeded even if it returns EINTR.

--
Gilles

OK, thank you.

Quoted 8 lines
Quoted 1 line

And do you have an idea about the bug traces ?

No, I have no idea where this 512 come from. Do you still get it if you do not call timer_delete in the signal handler ?

--
Gilles

yes, I still get it without timer_delete call in the signal handler

#38 lore raw
In reply to Gilles Chanteperdrix

Hello,

I succeeded to use printk in shadow.c :

So in xnshadow_harden function, there are calls to printk before ERESTARTSYS returns :

if (signal_pending(this_task) || down_interruptible(&gk->sync)) {
/* Grab the request token. */
printk(KERN_DEBUG "xnshadow_harden : return ERESTARTSYS 1\n"); return -ERESTARTSYS;
}
...
if (rthal_current_domain == rthal_root_domain) {
if (XENO_DEBUG(NUCLEUS) && (!signal_pending(this_task)

	    || this_task->state != TASK_RUNNING))
	xnpod_fatal
	    ("xnshadow_harden() failed for thread %s[%d]",
	     thread->name, xnthread_user_pid(thread));
  printk(KERN_DEBUG "xnshadow_harden : return ERESTARTSYS 2 - thread %s
[%d]\n"
                                   ,thread->name, nthread_user_pid(thread));

return -ERESTARTSYS;
}

Now , when I execute my test program, for each crash, I ve got one of my two messages in kernel trace.

In the test program, there are three Xenomai thread :
threadTimeOutEnd (prio : 85): This thread waits the timeout end thanks to a Semaphore (In the timeout end handler, the Semaphore is posted), and warns threadTimeOut with a condvar broadcast. This thread calls display function to debug the program.
threadTimeOut (prio : 80): Malloc memory space to create timeout. Set-up timeouts of 5ms. Free memory space of achieved timeouts (This thread is notified of timeout end with a condvar broadcasted by threadTimeOutEnd). This thread must do the timeout end handler processing.This thread calls display function to debug the program.
threadDisplay (prio : 70): Call display function in a loop.

This is the program :

###################################################################

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>

#define NB_PTR_TEMPO 5 // 5 timeouts maximum
#define STACKSIZE 350

// stack
static int Stack[STACKSIZE];
static unsigned short Write_ptr = 0;
static unsigned short Read_ptr = 0;

// Display
pthread_mutex_t lockDisplay;
unsigned char bufferDisplay[2048];

// Timer

struct stTimeOut {
    timer_t timer_h;
    struct sigaction    sa;
    struct sigevent     sig_spec;
    struct itimerspec   tmr_setting;
    int number;
}*timeOut0_ptr, *timeOut1_ptr, *timeOut2_ptr, *timeOut3_ptr, *timeOut4_ptr;

// Thread start
pthread_cond_t start_signal;
pthread_mutex_t main_start_lock;
bool bMainStart = false;
// Time-out end
pthread_cond_t TimeOutEnd_signal;
pthread_mutex_t timeOutEnd_lock;
bool bTimeOutEnd = false;
sem_t TimeOutWait_sem;

// Stack mutex
pthread_mutex_t Stack_lock;

void check(const char *file, int line, const char *service,
int status, int err)

{
    if (status >= 0)
        return;

    fprintf(stderr, "%s:%d: %s: %s\n", file, line, service, strerror(err));
    exit(EXIT_FAILURE);
}
#define check_pthread(expr)                \
    ({                                                \
        int _status = (expr);                 \
        check(__FILE__, __LINE__, #expr, -_status, _status);        \
    })
#define check_unix(expr) \
    ({                                                \
        int _status = (expr);                 \
        check(__FILE__, __LINE__, #expr, _status, errno);  \
    })

/************************ Stack functions *************************/

int StackCreation(void)

{
    Write_ptr = 0;
    Read_ptr  = 0;
    return 0;
}

void StackWrite(int number)

{
    if (Write_ptr >= STACKSIZE)
        Write_ptr = 0;
    Stack[Write_ptr++] = number;
}

int StackRead(void)

{
    int number;
    if (Read_ptr >= STACKSIZE)
        Read_ptr = 0;
    number = Stack[Read_ptr++];
    return number;
}

unsigned short GetWritePtr(void)

{
    return Write_ptr;
}

unsigned short GetReadPtr(void)

{
    return Read_ptr;
}

/************************ Functions ******************************/ void display(char * chaine,...)

{
    int err;
    check_pthread(pthread_mutex_lock(&lockDisplay));
    va_list ArgDisplay;
    va_start(ArgDisplay, chaine);
    check_unix(vsprintf((char *)bufferDisplay,chaine,ArgDisplay));
    bool bErrWrite = false;
    do
	{
      bErrWrite = false;
      if (write(2, (char*)bufferDisplay, strlen((char *)bufferDisplay)) < 0)
      {
         err = errno;
         printf("display : erreur write : %d, %s\n",err,strerror(err));
         if (err == EINTR)
         {
              bErrWrite = true;
         }
         else
         {
             exit(0);
         }
      }
    }while (bErrWrite);
    if((err = pthread_mutex_unlock(&lockDisplay)) != 0)
    {
        if(err != EINTR)
        {
           printf("display : erreur pthread_mutex_unlock : %d,
%s\n",err,strerror(err));
           exit(0);
        }
    }

}

int func(volatile int* i)

{
    return (*i)++;
}

void DeleteTimer(timer_t timer)

{
    if (timer!=NULL)
        check_unix(__real_timer_delete(timer));
}

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
    volatile int i, result = 0;

    StackWrite(((struct stTimeOut*)(info->si_value.sival_ptr))->number);

    check_unix(sem_post(&TimeOutWait_sem));
}

void StartTimeOut (int nb_Sec, int nb_nSec, struct stTimeOut* timeOut)

{
    (timeOut->sa).sa_flags = SA_SIGINFO;
    (timeOut->sa).sa_sigaction = EndTimeOut;

    check_unix(sigaction(SIGRTMIN, &(timeOut->sa), NULL));

    (timeOut->sig_spec).sigev_notify = SIGEV_SIGNAL;
    (timeOut->sig_spec).sigev_signo = SIGRTMIN;
    (timeOut->sig_spec).sigev_value.sival_ptr = timeOut;

    check_unix(__real_timer_create(CLOCK_REALTIME, &(timeOut->sig_spec),
                            &(timeOut->timer_h)));

    (timeOut->tmr_setting).it_value.tv_sec = nb_Sec;
    (timeOut->tmr_setting).it_value.tv_nsec = nb_nSec;
    (timeOut->tmr_setting).it_interval.tv_sec = 0;
    (timeOut->tmr_setting).it_interval.tv_nsec = 0;

    check_unix(__real_timer_settime((timeOut->timer_h), 0,
                             &(timeOut->tmr_setting),NULL));
}

/************************** Threads ********************************/ void* threadTimeOutEnd(void * arg) {

    int NbSem = 0;
    bool bSemWaitError;

    display("TimeOutEnd thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart) {
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    }
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("TimeOutEnd thread\n");

    while (1) {
    do
	{
	  bSemWaitError = false;
	  if (sem_wait (&TimeOutWait_sem)<0)
	  {
	   int err = errno;
       display("Semaphore::Get - erreur sem_wait : errno :%d -> %s\n"
                                                 ,err,strerror(err));
       if (err == EINTR)
	   {
	   	bSemWaitError = true;
       }
	  }
    }while (bSemWaitError);

    check_pthread(pthread_mutex_lock(&timeOutEnd_lock));
    check_pthread(pthread_cond_broadcast(&TimeOutEnd_signal));
    bTimeOutEnd = true;
    check_pthread(pthread_mutex_unlock(&timeOutEnd_lock));
    }
}
void* threadTimeOut(void * arg) {
    int i=1;
    int j, k, NbTimeOut, numTimeOut;
    char strComm[1024];

    timeOut0_ptr = timeOut1_ptr = timeOut2_ptr = timeOut3_ptr =
        timeOut4_ptr = NULL;

    display("TimeOut thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("TimeOut thread\n");

    while (i < 100) {
        // Malloc and start of time out
        for (j=0 ; j < NB_PTR_TEMPO; j++) {
            switch(j) {
            case 0 : if (timeOut0_ptr == NULL) {
                    timeOut0_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                    if (timeOut0_ptr == NULL)
                    {
                         display("0 : Malloc error\n");
                         exit(1);
                    }
                    timeOut0_ptr->number = i;
                    i++;
                    display("0 : Start of time out %d - 5ms\n",
                            timeOut0_ptr->number);
                    StartTimeOut(0,500000000,timeOut0_ptr);
                }
                break;
            case 1 : if (timeOut1_ptr == NULL) {
                    timeOut1_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                    if (timeOut1_ptr == NULL)
                    {
                         display("1 : Malloc error\n");
                         exit(1);
                    }
                    timeOut1_ptr->number = i;
                    i++;
                    display("1 : Start of time out %d - 5ms\n",
                            timeOut1_ptr->number);
                    StartTimeOut(0,500000000,timeOut1_ptr);
                }
                break;
            case 2 : if (timeOut2_ptr == NULL) {
                    timeOut2_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                    if (timeOut2_ptr == NULL)
                    {
                         display("2 : Malloc error\n");
                         exit(1);
                    }
                    timeOut2_ptr->number = i;
                    i++;
                    display("2 : Start of time out %d - 5ms\n",
                            timeOut2_ptr->number);
                    StartTimeOut(0,500000000,timeOut2_ptr);
                }
                break;
            case 3 : if (timeOut3_ptr == NULL) {
                    timeOut3_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                    if (timeOut3_ptr == NULL)
                    {
                         display("3 : Malloc error\n");
                         exit(1);
                    }
                    timeOut3_ptr->number = i;
                    i++;
                    display("3 : Start of time out %d - 5ms\n",
                            timeOut3_ptr->number);
                    StartTimeOut(0,500000000,timeOut3_ptr);
                }
                break;
            case 4 : if (timeOut4_ptr == NULL) {
                    timeOut4_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                    if (timeOut4_ptr == NULL)
                    {
                         display("4 : Malloc error\n");
                         exit(1);
                    }
                    timeOut4_ptr->number = i;
                    i++;
                    display("4 : Start of time out %d - 5ms\n",
                            timeOut4_ptr->number);
                    StartTimeOut(0,500000000,timeOut4_ptr);
                }
                break;
            }
        }
        check_pthread(pthread_mutex_lock(&timeOutEnd_lock));
        while (!bTimeOutEnd){
            check_pthread(pthread_cond_wait(&TimeOutEnd_signal,
                                            &timeOutEnd_lock));
            }
        bTimeOutEnd = false;
        check_pthread(pthread_mutex_unlock(&timeOutEnd_lock));

        NbTimeOut = GetWritePtr() - GetReadPtr();
        display("Number of time-outs ends : %d\n", NbTimeOut);
        for (j=0; j < NbTimeOut; j++) {
            numTimeOut = StackRead();
            display("TimeOut%d ends\n", numTimeOut);
            for (k=0; k < NB_PTR_TEMPO; k++) {
                switch(k) {
                case 0 : if (timeOut0_ptr != NULL) {
                        if (timeOut0_ptr->number == numTimeOut) {
                            free(timeOut0_ptr);
                            timeOut0_ptr = NULL;
                        }
                    }
                    break;
                case 1 : if (timeOut1_ptr != NULL) {
                        if (timeOut1_ptr->number == numTimeOut) {
                            free(timeOut1_ptr);
                            timeOut1_ptr = NULL;
                        }
                    }
                    break;
                case 2 : if (timeOut2_ptr != NULL) {
                        if (timeOut2_ptr->number == numTimeOut) {
                            free(timeOut2_ptr);
                            timeOut2_ptr = NULL;
                        }
                    }
                    break;
                case 3 : if (timeOut3_ptr != NULL) {
                        if (timeOut3_ptr->number == numTimeOut) {
                            free(timeOut3_ptr);
                            timeOut3_ptr = NULL;
                        }
                    }
                    break;
                case 4 : if (timeOut4_ptr != NULL) {
                        if (timeOut4_ptr->number == numTimeOut) {
                            free(timeOut4_ptr);
                            timeOut4_ptr = NULL;
                        }
                    }
                    break;
                }
            }
        }
    }
    while(1) {
        sleep(10);
    }
    return NULL;
}
void* threadDisplay(void * arg) {
    volatile int i=0;
    volatile int result;
    display("Display thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("Display thread\n");

    while (i <= 100000) {
        result = func(&i);
        display("Display thread :%d \r",result);
    }
    display("End of display thread\n");
    return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
    if (timeOut0_ptr)
        DeleteTimer(timeOut0_ptr->timer_h);
    if (timeOut1_ptr)
        DeleteTimer(timeOut1_ptr->timer_h);
    if (timeOut2_ptr)
        DeleteTimer(timeOut2_ptr->timer_h);
    if (timeOut3_ptr)
        DeleteTimer(timeOut3_ptr->timer_h);
    if (timeOut4_ptr)
        DeleteTimer(timeOut4_ptr->timer_h);
    exit(0);
}
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    pthread_t p0;
    pthread_t p1;
    pthread_t p2;
    struct sched_param sch;

    // Stack creation
    StackCreation();

    check_unix(signal(SIGINT, cleanup_upon_sig));
    check_unix(signal(SIGTERM, cleanup_upon_sig));

    check_unix(mlockall(MCL_CURRENT|MCL_FUTURE));

    // mutex and sem initialisation
    check_pthread(pthread_cond_init(&start_signal, NULL));
    check_pthread(pthread_mutex_init(&main_start_lock, NULL));
    check_pthread(pthread_cond_init(&TimeOutEnd_signal, NULL));
    check_pthread(pthread_mutex_init(&timeOutEnd_lock, NULL));
    check_unix(sem_init (&TimeOutWait_sem, 0,0));
    check_pthread(pthread_mutexattr_init(&attr_proto));
    check_pthread(pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT));
    check_pthread(pthread_mutex_init(&lockDisplay, &attr_proto));

    check_pthread(pthread_attr_init(&attr));
    check_pthread(pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED));
    check_pthread(pthread_attr_setschedpolicy(&attr, SCHED_FIFO));

    // TimeOutEnd thread creation
    sch.sched_priority = 85;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p0, &attr, threadTimeOutEnd, NULL));
    check_pthread(pthread_set_name_np( p0,"threadTimeOutEnd"));

    // TimeOut thread creation
    sch.sched_priority = 80;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p1, &attr, threadTimeOut, NULL));
    check_pthread(pthread_set_name_np( p1,"threadTimeOut"));

    // Display thread creation
    sch.sched_priority = 70;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p2, &attr, threadDisplay, NULL));
    check_pthread(pthread_set_name_np( p2,"threadDisplay"));

    check_pthread(pthread_attr_destroy(&attr));

    display("Main condition broadcast\n");
    // Start of all threads
    check_pthread(pthread_mutex_lock(&main_start_lock));
    bMainStart = true;
    check_pthread(pthread_cond_broadcast(&start_signal));
    check_pthread(pthread_mutex_unlock(&main_start_lock));

    while (1) {
        sleep(5);
    }

    return 0;
}

####################################################################

I always have severals types of crash :

####################################################################

-->> first : pthread_mutex_unlock returns Unknown error 512

-> console :

TimeOutEnd thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutEnd thread
TimeOut thread
0 : Start of time out 1 - 5ms
Display thread
1 : Start of time out 2 - 5ms
2 : Start of time out 3 - 5ms
3 : Start of time out 4 - 5ms
4 : Start of time out 5 - 5ms
display : erreur pthread_mutex_unlock : 512, Unknown error 512 <--- stop
of test program execution : exit call in display function

-> kernel traces :

BUG: scheduling while atomic: testTimer12_cor/0x00000002/4501
 [<c03631db>] schedule+0x7d/0x354
 [<c010ddc6>] __wake_up_sync+0x58/0x79
 [<c0140b88>] xnshadow_harden+0x93/0x203
 [<c0234860>] tty_ldisc_deref+0x7e/0x9b
 [<c0140d6b>] losyscall_event+0x73/0x13a
 [<c012f475>] __ipipe_dispatch_event+0xb1/0x174
 [<c0140cf8>] losyscall_event+0x0/0x13a
 [<c0108a4c>] __ipipe_syscall_root+0x6b/0xd1
 [<c0102589>] system_call+0x29/0x4a
 =======================

xnshadow_harden : return ERESTARTSYS 2 - thread threadDisplay [4501]
note: testTimer12_cor[4501] exited with preempt_count 1
Xenomai: Posix: destroying semaphore c7768290.
Xenomai: Posix: destroying mutex c7765c50.
Xenomai: Posix: destroying mutex c7761690.
Xenomai: Posix: destroying mutex c7766b10.
Xenomai: Posix: destroying condition variable c7765d50.
Xenomai: Posix: destroying condition variable c7766d50.

####################################################################

-->> Second : no error code returned by a test program function

-> console :

TimeOutEnd thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutEnd thread
TimeOut thread
0 : Start of time out 1 - 5ms
Display thread
1 : Start of time out 2 - 5ms
2 : Start of time out 3 - 5ms
3 : Start of time out 4 - 5ms
4 : Start of time out 5 - 5ms
Semaphore::Get - erreur sem_wait : errno :4 -> Interrupted system call Number of time-outs ends : 5
TimeOut1 ends
TimeOut2 ends
TimeOut3 ends
TimeOut4 ends
TimeOut5 ends
0 : Start of time out 6 - 5ms
1 : Start of time out 7 - 5ms
2 : Start of time out 8 - 5ms
3 : Start of time out 9 - 5ms
4 : Start of time out 10 - 5ms
display : erreur write : 4, Interrupted system call
Number of time-outs ends : 1
TimeOut6 ends
0 : Start of time out 11 - 5ms
Number of time-outs ends : 4
TimeOut7 ends
TimeOut8 ends
TimeOut9 ends
TimeOut10 ends
1 : Start of time out 12 - 5ms
2 : Start of time out 13 - 5ms
3 : Start of time out 14 - 5ms
4 : Start of time out 15 - 5ms <--- stop of test program execution

-> kernel traces :

BUG: scheduling while atomic: testTimer12_cor/0x00000002/4506
 [<c03631db>] schedule+0x7d/0x354
 [<c010ddc6>] __wake_up_sync+0x58/0x79
 [<c0140b88>] xnshadow_harden+0x93/0x203
 [<c0234860>] tty_ldisc_deref+0x7e/0x9b
 [<c0140d6b>] losyscall_event+0x73/0x13a
 [<c012f475>] __ipipe_dispatch_event+0xb1/0x174
 [<c0140cf8>] losyscall_event+0x0/0x13a
 [<c0108a4c>] __ipipe_syscall_root+0x6b/0xd1
 [<c0102589>] system_call+0x29/0x4a
 =======================

xnshadow_harden : return ERESTARTSYS 2 - thread threadDisplay [4506]
note: testTimer12_cor[4506] exited with preempt_count 1
Xenomai: POSIX: destroyed thread c7760810
Xenomai: Posix: destroying semaphore c7765c50.
Xenomai: Posix: destroying mutex c7765d50.
Xenomai: Posix: destroying mutex c7761690.
Xenomai: Posix: destroying mutex c7768290.
Xenomai: Posix: destroying condition variable c7766d50.
Xenomai: Posix: destroying condition variable c7766b10.

or

BUG: scheduling while atomic: testTimer12_cor/0x00000002/2940
 [<c03631db>] schedule+0x7d/0x354
 [<c010ddc6>] __wake_up_sync+0x58/0x79
 [<c0140b88>] xnshadow_harden+0x93/0x203
 [<c01229fe>] hrtimer_start+0xa1/0xc3
 [<c0140d6b>] losyscall_event+0x73/0x13a
 [<c012f475>] __ipipe_dispatch_event+0xb1/0x174
 [<c0140cf8>] losyscall_event+0x0/0x13a
 [<c0108a4c>] __ipipe_syscall_root+0x6b/0xd1
 [<c0102589>] system_call+0x29/0x4a
 =======================

xnshadow_harden : return ERESTARTSYS 2 - thread threadTimeOut [2940]
note: testTimer12_cor[2940] exited with preempt_count 1
Xenomai: Posix: destroying semaphore c7760910.
Xenomai: Posix: destroying mutex c7760850.
Xenomai: Posix: destroying mutex c77608d0.
Xenomai: Posix: destroying mutex c7760950.
Xenomai: Posix: destroying condition variable c7760810.
Xenomai: Posix: destroying condition variable c7760890.

####################################################################

-->> Third : no error code returned by a test program function

-> console :

TimeOutEnd thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutEnd thread
TimeOut thread
0 : Start of time out 1 - 5ms
Display thread
1 : Start of time out 2 - 5ms
2 : Start of time out 3 - 5ms
3 : Start of time out 4 - 5ms
4 : Start of time out 5 - 5ms
Semaphore::Get - erreur sem_wait : errno :4 -> Interrupted system call Number of time-outs ends : 4
TimeOut1 ends
TimeOut2 ends
TimeOut3 ends
TimeOut4 ends
...
0 : Start of time out 16 - 5ms
1 : Start of time out 17 - 5ms
2 : Start of time out 18 - 5ms
3 : Start of time out 19 - 5ms
Number of time-outs ends : 1
TimeOut15 ends
4 : Start of time out 20 - 5ms
Number of time-outs ends : 2
TimeOut16 ends
TimeOut17 ends
0 : Start of time out 21 - 5ms
1 : Start of time out 22 - 5ms
Number of time-outs ends : 2
TimeOut18 ends
TimeOut19 ends
2 : Start of time out 23 - 5ms
3 : Start of time out 24 - 5ms
Number of time-outs ends : 1
TimeOut20 ends
4 : Start of time out 25 - 5ms
Display thread :43617 <--- test program crash (ctrl+c to stop it)

-> kernel traces :

xnshadow_harden : return ERESTARTSYS 1
Xenomai: POSIX: destroyed thread c7760810
Xenomai: Posix: destroying semaphore c7761110.
Xenomai: Posix: destroying mutex c7761050.
Xenomai: Posix: destroying mutex c77610d0.
Xenomai: Posix: destroying mutex c7761150.
Xenomai: Posix: destroying condition variable c7761010.
Xenomai: Posix: destroying condition variable c7761090.

or

xnshadow_harden : return ERESTARTSYS 2 - thread threadDisplay [3165]
Xenomai: POSIX: destroyed thread c7760810
Xenomai: Posix: destroying semaphore c7761110.
Xenomai: Posix: destroying mutex c7761050.
Xenomai: Posix: destroying mutex c77610d0.
Xenomai: Posix: destroying mutex c7761150.
Xenomai: Posix: destroying condition variable c7761010.
Xenomai: Posix: destroying condition variable c7761090.

####################################################################

-->> Fourth : test program is executed without crash but there are xnshadow_harden traces

-> console :
...

-> kernel traces (For example) :
xnshadow_harden : return ERESTARTSYS 1

or

xnshadow_harden : return ERESTARTSYS 2 - thread threadDisplay [2889]

or

xnshadow_harden : return ERESTARTSYS 2 - thread threadTimeOut [2950]

####################################################################

test program is also executed without errors.

It's difficult to me to understand what exactly happens in xnshadow_harden function. At a moment, there is a problem when display thread or timeout thread have to return in the xenomai domain, but why ?

thank you for your help.

#39 lore raw
In reply to Gilles Chanteperdrix

Hello,

thank you very much for your work.

I tried your Xenomai 2.4.4.

I began to patch a linux kernel 2.6.20.21 with the i386 adeos patch. I would to know why you cancelled the i386 adeos patch for linux kernel 2.6.23 ?

So, with the 2.6.20.21, when I execute my test program, I hadn't got a big improvement. There were always bugs.

Then, I patched a linux kernel 2.6.25 with the x86 adeos patch. This time, there was a big improvement when I executed my test program.But, occasionally, there was a program crash, without bug messages in the kernel traces.
I added debug traces (calls to write function) in my program to understand why there was this crash. With theses new traces, I had a big crash of my program execution, and the system is also crashed. (I must reboot my system)
In this case, I had a Xenomai fatal message in the kernel traces.

To sum up my test program :

In the test program, there are three Xenomai thread :
threadTimeOutEnd (prio : 85): This thread waits the timeout end thanks to a Semaphore (In the timeout end handler, the Semaphore is posted), and warns threadTimeOut with a condvar broadcast. This thread calls display function to debug the program.
threadTimeOut (prio : 80): Malloc memory space to create timeout. Set-up timeouts of 500ms. Free memory space of achieved timeouts (This thread is notified of timeout end with a condvar broadcasted by threadTimeOutEnd). This thread must do the timeout end handler processing.This thread calls display function to debug the program.
threadDisplay (prio : 70): Call display function in a loop. Normally, this thread ends the test program and displays the stack of timeout end numbers. (if necesserray, adjust the loop counter)

Sorry, my test program is a bit long
################################################################################################

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>

#define NB_PTR_TEMPO 5 // 5 timeouts maximum
#define STACKSIZE 350

// stack
static int Stack[STACKSIZE];
static unsigned short Write_ptr = 0;
static unsigned short Read_ptr = 0;

// Display
pthread_mutex_t lockDisplay;
unsigned char bufferDisplay[2048];

// Timer

struct stTimeOut {
    timer_t timer_h;
    struct sigaction    sa;
    struct sigevent     sig_spec;
    struct itimerspec   tmr_setting;
    int number;
}*timeOut0_ptr, *timeOut1_ptr, *timeOut2_ptr, *timeOut3_ptr, *timeOut4_ptr;

// Thread start
pthread_cond_t start_signal;
pthread_mutex_t main_start_lock;
bool bMainStart = false;
// Time-out end
pthread_cond_t TimeOutEnd_signal;
pthread_mutex_t timeOutEnd_lock;
bool bTimeOutEnd = false;
sem_t TimeOutWait_sem;

// Stack mutex
pthread_mutex_t Stack_lock;

void check(const char *file, int line, const char *service,
int status, int err)

{
    if (status >= 0)
        return;

    fprintf(stderr, "%s:%d: %s: %s\n", file, line, service, strerror(err));
    exit(EXIT_FAILURE);
}
#define check_pthread(expr)                \
    ({                                                \
        int _status = (expr);                 \
        check(__FILE__, __LINE__, #expr, -_status, _status);        \
    })
#define check_unix(expr) \
    ({                                                \
        int _status = (expr);                 \
        check(__FILE__, __LINE__, #expr, _status, errno);  \
    })

/************************ Stack functions *************************/

int StackCreation(void)

{
    Write_ptr = 0;
    Read_ptr  = 0;
    return 0;
}

void StackWrite(int number)

{
    if (Write_ptr >= STACKSIZE)
        Write_ptr = 0;
    Stack[Write_ptr++] = number;
}

int StackRead(void)

{
    int number;
    if (Read_ptr >= STACKSIZE)
        Read_ptr = 0;
    number = Stack[Read_ptr++];
    return number;
}

int StackReadOneElement(int ReadOne_ptr)

{
    int number;
    if ((ReadOne_ptr < 0) || (ReadOne_ptr >= STACKSIZE))
    {
        return 0;
    }
    number = Stack[ReadOne_ptr];
    return number;
}

unsigned short GetWritePtr(void)

{
    return Write_ptr;
}

unsigned short GetReadPtr(void)

{
    return Read_ptr;
}

/************************ Functions ******************************/ void display(char * chaine,...)

{
    int err;
    check_pthread(pthread_mutex_lock(&lockDisplay));
    va_list ArgDisplay;
    va_start(ArgDisplay, chaine);
    check_unix(vsprintf((char *)bufferDisplay,chaine,ArgDisplay));
    bool bErrWrite = false;
    do
	{
      bErrWrite = false;
      if (write(2, (char*)bufferDisplay, strlen((char *)bufferDisplay)) < 0)
      {
         err = errno;
         printf("display : erreur write : %d, %s\n",err,strerror(err));
         if (err == EINTR)
         {
              bErrWrite = true;
         }
         else
         {
             exit(EXIT_FAILURE);
         }
      }
    }while (bErrWrite);
    if((err = pthread_mutex_unlock(&lockDisplay)) != 0)
    {
        if(err != EINTR)
        {
           printf("display : erreur pthread_mutex_unlock : %d,
%s\n",err,strerror(err));
           exit(EXIT_FAILURE);
        }
    }
}

int func(volatile int* i)

{
    return (*i)++;
}

void DeleteTimer(timer_t timer)

{
    if (timer!=NULL)
        check_unix(__real_timer_delete(timer));
}

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
    volatile int i, result = 0;

    StackWrite(((struct stTimeOut*)(info->si_value.sival_ptr))->number);

    check_unix(sem_post(&TimeOutWait_sem));
}

void StartTimeOut (int nb_Sec, int nb_nSec, struct stTimeOut* timeOut)

{
     int err;
     bool bErrSettime = false;

    (timeOut->sa).sa_flags = SA_SIGINFO;
    (timeOut->sa).sa_sigaction = EndTimeOut;

    check_unix(sigaction(SIGRTMIN, &(timeOut->sa), NULL));

    (timeOut->sig_spec).sigev_notify = SIGEV_SIGNAL;
    (timeOut->sig_spec).sigev_signo = SIGRTMIN;
    (timeOut->sig_spec).sigev_value.sival_ptr = timeOut;

    check_unix(__real_timer_create(CLOCK_REALTIME, &(timeOut->sig_spec),
                            &(timeOut->timer_h)));

    (timeOut->tmr_setting).it_value.tv_sec = nb_Sec;
    (timeOut->tmr_setting).it_value.tv_nsec = nb_nSec;
    (timeOut->tmr_setting).it_interval.tv_sec = 0;
    (timeOut->tmr_setting).it_interval.tv_nsec = 0;

    do
	{
      bErrSettime = false;
      if ((__real_timer_settime((timeOut->timer_h), 0,
                               &(timeOut->tmr_setting),NULL)) < 0)
      {
         err = errno;
         printf("StartTimeOut : erreur timer_settime : %d,
%s\n",err,strerror(err));
         if (err == EINTR)
         {
              bErrSettime = true;
         }
      }
    }while (bErrSettime);
}

/************************** Threads ********************************/ void* threadTimeOutEnd(void * arg) {

    int NbSem = 0;
    bool bSemWaitError;

    display("TimeOutEnd thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart) {
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    }
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("TimeOutEnd thread\n");

    unsigned char Message[200];

    while (1) {
    /*>>>>>********************************************/
    strcpy(Message,"threadTimeOutEnd thread : before sem_wait\n");
    write(2,(char *)Message,strlen((char *)Message));
    /********************************************<<<<<*/

    do
	{
	  bSemWaitError = false;
	  if (sem_wait (&TimeOutWait_sem)<0)
	  {
	   int err = errno;
       display("Semaphore::Get - erreur sem_wait : errno :%d -> %s\n"
                                                 ,err,strerror(err));
       if (err == EINTR)
	   {
	   	bSemWaitError = true;
       }
       else exit(EXIT_FAILURE);
	  }
    }while (bSemWaitError);

    /*>>>>>********************************************/
    strcpy(Message,"threadTimeOutEnd thread : after sem_wait\n");
    write(2,(char *)Message,strlen((char *)Message));
    /********************************************<<<<<*/

    check_pthread(pthread_mutex_lock(&timeOutEnd_lock));
    check_pthread(pthread_cond_broadcast(&TimeOutEnd_signal));
    bTimeOutEnd = true;
    check_pthread(pthread_mutex_unlock(&timeOutEnd_lock));
    }
}
void* threadTimeOut(void * arg) {
    int i=1;
    int j, k, NbTimeOut, numTimeOut;
    char strComm[1024];

    timeOut0_ptr = timeOut1_ptr = timeOut2_ptr = timeOut3_ptr =
        timeOut4_ptr = NULL;

    display("TimeOut thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("TimeOut thread\n");

    while (i < 100) {
        // Malloc and start of time out
        for (j=0 ; j < NB_PTR_TEMPO; j++) {
            switch(j) {
            case 0 : if (timeOut0_ptr == NULL) {
                    timeOut0_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                    if (timeOut0_ptr == NULL)
                    {
                         display("0 : Malloc error\n");
                         exit(EXIT_FAILURE);
                    }
                    timeOut0_ptr->number = i;
                    i++;
                    display("0 : Start of time out %d - 500ms\n",
                            timeOut0_ptr->number);
                    StartTimeOut(0,500000000,timeOut0_ptr);
                }
                break;
            case 1 : if (timeOut1_ptr == NULL) {
                    timeOut1_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                    if (timeOut1_ptr == NULL)
                    {
                         display("1 : Malloc error\n");
                         exit(EXIT_FAILURE);
                    }
                    timeOut1_ptr->number = i;
                    i++;
                    display("1 : Start of time out %d - 500ms\n",
                            timeOut1_ptr->number);
                    StartTimeOut(0,500000000,timeOut1_ptr);
                }
                break;
            case 2 : if (timeOut2_ptr == NULL) {
                    timeOut2_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                    if (timeOut2_ptr == NULL)
                    {
                         display("2 : Malloc error\n");
                         exit(EXIT_FAILURE);
                    }
                    timeOut2_ptr->number = i;
                    i++;
                    display("2 : Start of time out %d - 500ms\n",
                            timeOut2_ptr->number);
                    StartTimeOut(0,500000000,timeOut2_ptr);
                }
                break;
            case 3 : if (timeOut3_ptr == NULL) {
                    timeOut3_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                    if (timeOut3_ptr == NULL)
                    {
                         display("3 : Malloc error\n");
                         exit(EXIT_FAILURE);
                    }
                    timeOut3_ptr->number = i;
                    i++;
                    display("3 : Start of time out %d - 500ms\n",
                            timeOut3_ptr->number);
                    StartTimeOut(0,500000000,timeOut3_ptr);
                }
                break;
            case 4 : if (timeOut4_ptr == NULL) {
                    timeOut4_ptr=(struct stTimeOut*)malloc(sizeof(struct
stTimeOut));
                    if (timeOut4_ptr == NULL)
                    {
                         display("4 : Malloc error\n");
                         exit(EXIT_FAILURE);
                    }
                    timeOut4_ptr->number = i;
                    i++;
                    display("4 : Start of time out %d - 500ms\n",
                            timeOut4_ptr->number);
                    StartTimeOut(0,500000000,timeOut4_ptr);
                }
                break;
            }
        }
        check_pthread(pthread_mutex_lock(&timeOutEnd_lock));
        while (!bTimeOutEnd){
            check_pthread(pthread_cond_wait(&TimeOutEnd_signal,
                                            &timeOutEnd_lock));
            display("TimeOut thread : bTimeOutEnd : %d\n", bTimeOutEnd);
            }
        bTimeOutEnd = false;
        check_pthread(pthread_mutex_unlock(&timeOutEnd_lock));

        NbTimeOut = GetWritePtr() - GetReadPtr();
        display("Number of time-outs ends : %d\n", NbTimeOut);
        for (j=0; j < NbTimeOut; j++) {
            numTimeOut = StackRead();
            display("TimeOut%d ends\n", numTimeOut);
            for (k=0; k < NB_PTR_TEMPO; k++) {
                switch(k) {
                case 0 : if (timeOut0_ptr != NULL) {
                        if (timeOut0_ptr->number == numTimeOut) {
                            free(timeOut0_ptr);
                            timeOut0_ptr = NULL;
                        }
                    }
                    break;
                case 1 : if (timeOut1_ptr != NULL) {
                        if (timeOut1_ptr->number == numTimeOut) {
                            free(timeOut1_ptr);
                            timeOut1_ptr = NULL;
                        }
                    }
                    break;
                case 2 : if (timeOut2_ptr != NULL) {
                        if (timeOut2_ptr->number == numTimeOut) {
                            free(timeOut2_ptr);
                            timeOut2_ptr = NULL;
                        }
                    }
                    break;
                case 3 : if (timeOut3_ptr != NULL) {
                        if (timeOut3_ptr->number == numTimeOut) {
                            free(timeOut3_ptr);
                            timeOut3_ptr = NULL;
                        }
                    }
                    break;
                case 4 : if (timeOut4_ptr != NULL) {
                        if (timeOut4_ptr->number == numTimeOut) {
                            free(timeOut4_ptr);
                            timeOut4_ptr = NULL;
                        }
                    }
                    break;
                }
            }
        }
    }
    /*while(1) {
        sleep(10);
    }*/
    return NULL;
}
void* threadDisplay(void * arg) {
    volatile int i=0;
    volatile int result;
    int numTimeOut = 1;
    display("Display thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
        check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("Display thread\n");

    while (i <= 120000) {
        result = func(&i);
        display("Display thread :%d \r",result);
    }
    display("----> End of display thread <----\n");

    display("Display thread: Reading number of achieved timeouts :\n");
    i=0;
    while(numTimeOut && (i<STACKSIZE))
    {
        numTimeOut=StackReadOneElement(i);
        if(numTimeOut)
        {
           display("[%d]:%d ",i ,numTimeOut);
        }
        i++;
        if(i==10 || i==20 || i==30 || i==40 || i==50
                    || i==60 || i==70 || i==80 || i==90)
        {
            display("\n");
        }
    }
    display("\n");
    exit(0);
    /*while(1)
    {
        sleep(5);
    }*/
    return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
    if (timeOut0_ptr)
        DeleteTimer(timeOut0_ptr->timer_h);
    if (timeOut1_ptr)
        DeleteTimer(timeOut1_ptr->timer_h);
    if (timeOut2_ptr)
        DeleteTimer(timeOut2_ptr->timer_h);
    if (timeOut3_ptr)
        DeleteTimer(timeOut3_ptr->timer_h);
    if (timeOut4_ptr)
        DeleteTimer(timeOut4_ptr->timer_h);
    exit(EXIT_FAILURE);
}
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    pthread_t p0;
    pthread_t p1;
    pthread_t p2;
    struct sched_param sch;

    // Stack creation
    StackCreation();

    check_unix(signal(SIGINT, cleanup_upon_sig));
    check_unix(signal(SIGTERM, cleanup_upon_sig));

    check_unix(mlockall(MCL_CURRENT|MCL_FUTURE));

    // mutex and sem initialisation
    check_pthread(pthread_cond_init(&start_signal, NULL));
    check_pthread(pthread_mutex_init(&main_start_lock, NULL));
    check_pthread(pthread_cond_init(&TimeOutEnd_signal, NULL));
    check_pthread(pthread_mutex_init(&timeOutEnd_lock, NULL));
    check_unix(sem_init (&TimeOutWait_sem, 0,0));
    check_pthread(pthread_mutexattr_init(&attr_proto));
    check_pthread(pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT));
    check_pthread(pthread_mutex_init(&lockDisplay, &attr_proto));

    check_pthread(pthread_attr_init(&attr));
    check_pthread(pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED));
    check_pthread(pthread_attr_setschedpolicy(&attr, SCHED_FIFO));

    // TimeOutEnd thread creation
    sch.sched_priority = 85;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p0, &attr, threadTimeOutEnd, NULL));
    check_pthread(pthread_set_name_np( p0,"threadTimeOutEnd"));

    // TimeOut thread creation
    sch.sched_priority = 80;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p1, &attr, threadTimeOut, NULL));
    check_pthread(pthread_set_name_np( p1,"threadTimeOut"));

    // Display thread creation
    sch.sched_priority = 70;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p2, &attr, threadDisplay, NULL));
    check_pthread(pthread_set_name_np( p2,"threadDisplay"));

    check_pthread(pthread_attr_destroy(&attr));

    display("Main condition broadcast\n");
    // Start of all threads
    check_pthread(pthread_mutex_lock(&main_start_lock));
    bMainStart = true;
    check_pthread(pthread_cond_broadcast(&start_signal));
    check_pthread(pthread_mutex_unlock(&main_start_lock));

    while (1) {
        sleep(5);
    }

    return 0;
}

###############################################################################

Here, this is a script, if you want to launch in a loop my test program (my program executable name is testTimer12_corrected):

#!/bin/bash
Err=0
while [ "$Err" -eq 0 ];
do
{
date
./testTimer12_corrected
Err=$?
}
done

##################################################################################

Here this is the result of the debug traces :

TimeOutEnd thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutEnd thread
threadTimeOutEnd thread : before sem_wait
TimeOut thread
0 : Start of time out 1 - 500ms
Display thread
1 : Start of time out 2 - 500ms
2 : Start of time out 3 - 500ms
3 : Start of time out 4 - 500ms
4 : Start of time out 5 - 500ms

...

0 : Start of time out 41 - 500ms
1 : Start of time out 42 - 500ms
2 : Start of time out 43 - 500ms
3 : Start of time out 44 - 500ms
Number of time-outs ends : 0
threadTimeOutEnd thread : after sem_wait
threadTimeOutEnd thread : before sem_wait
TimeOut thread : bTimeOutEnd : 1
Number of time-outs ends : 1
TimeOut40 ends
4 : Start of time out 45 - 500ms
Semaphore::Get - erreur sem_wait : errno :4 -> Interrupted system call threadTimeOutEnd thread : after sem_wait
threadTimeOutEnd thread : before sem_wait
threadTimeOutEnd thread : after sem_wait
TimeOut thread : bTimeOutEnd : 1
threadTimeOutEnd thread : before sem_wait
Number of time-outs ends : 2
TimeOut41 ends
Semaphore::Get - erreur sem_wait : errno :4 -> Interrupted system call threadTimeOutEnd thread : after sem_wait
threadTimeOutEnd thread : before sem_wait
threadTimeOutEnd thread : after sem_wait
threadTimeOutEnd thread : before sem_wait
TimeOut42 ends
0 : Start of time out 46 - 500ms
1 : Start of time out 47 - 500ms
Number of time-outs ends : 2
TimeOut43 ends
TimeOut44 ends
2 : Start of time out 48 - 500ms
3 : Start of time out 49 - 500ms
threadTimeOutEnd thread : after sem_wait
threadTimeOutEnd thread : before sem_wait
TimeOut thread : bTimeOutEnd : 1
Number of time-outs ends : 1
TimeOut45 ends
4 : Start of time out 50 - 500ms
Display thread :92353 <--- the system crashes (I must reboot it)

##################################################################################

Here this is the kernel traces :

       c70750f0 00000086 00300182 c70e1a20 ffffffff 00000001 c72ca800
c0274fec
       c7080650 c70e1090 00300182 c70e1a20 ffffffff 00000001 c014d9ca
00000200
       c7080650 00000000 c70750f0 c7080650 c71e77e0 00000033 00000000
c70e1150
Call Trace:
 [<c0274fec>] n_tty_receive_buf+0x51c/0x1270
 [<c014d9ca>] rpi_push+0x6a/0x420
 [<c01445a4>] xnpod_suspend_thread+0x424/0x4a0
 [<c014df7c>] rpi_update+0x1fc/0x230
 [<c014a1d7>] xnsynch_sleep_on+0x8e7/0xd10
 [<c014d159>] ppd_lookup+0x19/0x30
 [<c0153258>] pse51_mutex_timedlock_break+0x1f8/0x220
 [<c0161b9e>] __pthread_mutex_lock+0x4e/0x60
 [<c014f6d4>] losyscall_event+0xb4/0x170
 [<c013c5b2>] __ipipe_dispatch_event+0xa2/0x180
 [<c014f620>] losyscall_event+0x0/0x170
 [<c010cac0>] __ipipe_syscall_root+0x40/0xf0
 [<c0102eb9>] system_call+0x29/0x4a
 =======================
Xenomai: fatal: Hardened thread threadTimeOut[4698] running in Linux
domain?! (status=0x300182, sig=1, prev=gatekeeper/0[148])
 CPU  PID    PRI      TIMEOUT  STAT      NAME
>  0  0       80      0        00500080  ROOT
   0  4695     0      0        00300380  testTimer12_cor
   0  4697    85      0        00300182  threadTimeOutEnd
   0  4698    80      0        00300182  threadTimeOut
   0  4699    80      0        00300b80  threadDisplay

Master time base: clock=823018273704

       c70c3ecc 00000000 00000000 00000001 c7080650 c0104256 00000000
c0442211
       0000125a c70e1dec c014e3fd c043c6a0 c70c4000 0000125a 00300182
00000001
       c70752cc 00000094 c0584980 c013ce20 00000021 c013ce43 c013bb78
00000021
Call Trace:
 [<c0104256>] show_stack+0x36/0x50
 [<c014e3fd>] schedule_event+0x39d/0x850
 [<c013ce20>] rthal_apc_handler+0x0/0x30
 [<c013ce43>] rthal_apc_handler+0x23/0x30
 [<c013bb78>] __xirq_end+0x39/0x46
 [<c0102f28>] restore_nocheck_notrace+0x0/0xe
 [<c013ce20>] rthal_apc_handler+0x0/0x30
 [<c0106fd1>] native_sched_clock+0x61/0xb0
 [<c013c5b2>] __ipipe_dispatch_event+0xa2/0x180
 [<c014e060>] schedule_event+0x0/0x850
 [<c03dbeb6>] schedule+0x226/0x3b0
 [<c014fe6d>] gatekeeper_thread+0xad/0x140
 [<c03dbe48>] schedule+0x1b8/0x3b0
 [<c0111a40>] default_wake_function+0x0/0x10
 [<c014fdc0>] gatekeeper_thread+0x0/0x140
 [<c0127f42>] kthread+0x42/0x70
 [<c0127f00>] kthread+0x0/0x70
 [<c01032d7>] kernel_thread_helper+0x7/0x10
 =======================

###############################################################################

There is a problem with threadTimeOut. Maybe, this thread runs in linux domain and must come back in xenomai domain, but there is a

problem.

thank you.

#40 lore raw
Quoted 52 lines · Fabien MAHOT

Hello,
Here this is the kernel traces :

      c70750f0 00000086 00300182 c70e1a20 ffffffff 00000001 c72ca800
c0274fec
      c7080650 c70e1090 00300182 c70e1a20 ffffffff 00000001 c014d9ca
00000200
      c7080650 00000000 c70750f0 c7080650 c71e77e0 00000033 00000000
c70e1150
Call Trace:
 [<c0274fec>] n_tty_receive_buf+0x51c/0x1270
 [<c014d9ca>] rpi_push+0x6a/0x420
 [<c01445a4>] xnpod_suspend_thread+0x424/0x4a0
 [<c014df7c>] rpi_update+0x1fc/0x230
 [<c014a1d7>] xnsynch_sleep_on+0x8e7/0xd10
 [<c014d159>] ppd_lookup+0x19/0x30
 [<c0153258>] pse51_mutex_timedlock_break+0x1f8/0x220
 [<c0161b9e>] __pthread_mutex_lock+0x4e/0x60
 [<c014f6d4>] losyscall_event+0xb4/0x170
 [<c013c5b2>] __ipipe_dispatch_event+0xa2/0x180
 [<c014f620>] losyscall_event+0x0/0x170
 [<c010cac0>] __ipipe_syscall_root+0x40/0xf0
 [<c0102eb9>] system_call+0x29/0x4a
 =======================
Xenomai: fatal: Hardened thread threadTimeOut[4698] running in Linux
domain?! (status=0x300182, sig=1, prev=gatekeeper/0[148])
 CPU  PID    PRI      TIMEOUT  STAT      NAME
>  0  0       80      0        00500080  ROOT
  0  4695     0      0        00300380  testTimer12_cor
  0  4697    85      0        00300182  threadTimeOutEnd
  0  4698    80      0        00300182  threadTimeOut
  0  4699    80      0        00300b80  threadDisplay

Master time base: clock=823018273704

      c70c3ecc 00000000 00000000 00000001 c7080650 c0104256 00000000
c0442211
      0000125a c70e1dec c014e3fd c043c6a0 c70c4000 0000125a 00300182
00000001
      c70752cc 00000094 c0584980 c013ce20 00000021 c013ce43 c013bb78
00000021
Call Trace:
 [<c0104256>] show_stack+0x36/0x50
 [<c014e3fd>] schedule_event+0x39d/0x850
 [<c013ce20>] rthal_apc_handler+0x0/0x30
 [<c013ce43>] rthal_apc_handler+0x23/0x30
 [<c013bb78>] __xirq_end+0x39/0x46
 [<c0102f28>] restore_nocheck_notrace+0x0/0xe
 [<c013ce20>] rthal_apc_handler+0x0/0x30
 [<c0106fd1>] native_sched_clock+0x61/0xb0
 [<c013c5b2>] __ipipe_dispatch_event+0xa2/0x180
 [<c014e060>] schedule_event+0x0/0x850
 [<c03dbeb6>] schedule+0x226/0x3b0
Quoted 8 lines
 [<c014fe6d>] gatekeeper_thread+0xad/0x140
 [<c03dbe48>] schedule+0x1b8/0x3b0
 [<c0111a40>] default_wake_function+0x0/0x10
 [<c014fdc0>] gatekeeper_thread+0x0/0x140
 [<c0127f42>] kthread+0x42/0x70
 [<c0127f00>] kthread+0x0/0x70
 [<c01032d7>] kernel_thread_helper+0x7/0x10
 =======================

Your stack traces are strange, to say the least. Could you enable frame pointers and get another trace ?

Signature · 1 line
 Gilles
#41 lore raw
In reply to Fabien MAHOT
Quoted 4 lines · Fabien MAHOT

There is a problem with threadTimeOut. Maybe, this thread runs in linux domain and must come back in xenomai domain, but there is a

problem.

Do you run your program on UP, or on SMP ?
By the way, testing that pthread_mutex_unlock returns EINTR should be useless now, this bug also has been fixed.

Signature · 1 line
 Gilles
#42 lore raw

Hello,

I enabled debug traces for rtc drivers.

cordially

#43 lore raw
Quoted 3 lines · Fabien MAHOT

Hello,

I enabled debug traces for rtc drivers.

This will not help. What you need to enable are the "frame pointers", in the kernel configuration, menu kernel hacking, enable "Kernel debugging", then enable frame pointers.

Signature · 1 line
 Gilles
#44 lore raw
Quoted 4 lines
Quoted 2 lines · Fabien MAHOT

Hello,

Quoted 9 lines
Quoted 1 line

I enabled debug traces for rtc drivers.

This will not help. What you need to enable are the "frame pointers", in the kernel configuration, menu kernel hacking, enable "Kernel debugging", then enable frame pointers.

--
Gilles

Sorry, I didn t understand...

I enable frame pointers:

#############################################################
Debug traces :

TimeOutEnd thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutEnd thread
threadTimeOutEnd thread : before sem_wait
TimeOut thread
0 : Start of time out 1 - 500ms
Display thread
1 : Start of time out 2 - 500ms
2 : Start of time out 3 - 500ms
3 : Start of time out 4 - 500ms
4 : Start of time out 5 - 500ms

...

0 : Start of time out 41 - 500ms
1 : Start of time out 42 - 500ms
2 : Start of time out 43 - 500ms
3 : Start of time out 44 - 500ms
Number of time-outs ends : 0
threadTimeOutEnd thread : after sem_wait
threadTimeOutEnd thread : before sem_wait
TimeOut thread : bTimeOutEnd : 1
Number of time-outs ends : 1
TimeOut40 ends
4 : Start of time out 45 - 500ms
Semaphore::Get - erreur sem_wait : errno :4 -> Interrupted system call threadTimeOutEnd thread : after sem_wait
threadTimeOutEnd thread : before sem_wait
threadTimeOutEnd thread : after sem_wait
TimeOut thread : bTimeOutEnd : 1
threadTimeOutEnd thread : before sem_wait
Number of time-outs ends : 2
TimeOut41 ends
Semaphore::Get - erreur sem_wait : errno :4 -> Interrupted system call threadTimeOutEnd thread : after sem_wait
threadTimeOutEnd thread : before sem_wait
threadTimeOutEnd thread : after sem_wait
threadTimeOutEnd thread : before sem_wait
TimeOut42 ends
0 : Start of time out 46 - 500ms
1 : Start of time out 47 - 500ms
Number of time-outs ends : 2
TimeOut43 ends
TimeOut44 ends
2 : Start of time out 48 - 500ms
3 : Start of time out 49 - 500ms
threadTimeOutEnd thread : after sem_wait
threadTimeOutEnd thread : before sem_wait
TimeOut thread : bTimeOutEnd : 1
Number of time-outs ends : 1
TimeOut45 ends
4 : Start of time out 50 - 500ms
Display thread :92353

#############################################################
kernel traces :

c7397e60 00000086 c70e1220 00300182 c70e1a20 ffffffff 00000001 c7397ee8 c71f9400 c7397ef0 00300182 c70e1a20 ffffffff 00000001 c70e1c78 c70e10a0 00000102 c71f9400 00000033 c7074b90 c70375d0 c7074b90 c71d01e0 c70bbf98

Call Trace:
 [<c0142985>] ? xnpod_schedule+0x3c5/0x10f0
 [<c0144233>] xnpod_suspend_thread+0x413/0x490
 [<c014de14>] ? schedule_event+0x184/0x880
 [<c0106ea4>] ? native_sched_clock+0x64/0xb0
 [<c0110132>] ? __update_rq_clock+0x22/0x130
 [<c0149df2>] xnsynch_sleep_on+0x8f2/0xd20
 [<c0152f2c>] pse51_mutex_timedlock_break+0x1ec/0x210
 [<c016190e>] __pthread_mutex_lock+0x4e/0x60
 [<c013bd02>] ? __ipipe_restore_root+0x22/0x30
 [<c014f337>] losyscall_event+0xb7/0x170
 [<c014f280>] ? losyscall_event+0x0/0x170
 [<c013c45c>] __ipipe_dispatch_event+0x9c/0x170
 [<c010c811>] __ipipe_syscall_root+0x41/0x100
 [<c0102ea9>] system_call+0x29/0x4a
 =======================
Xenomai: fatal: Hardened thread threadTimeOut[2934] running in Linux
domain?! (status=0x300182, sig=1, prev=gatekeeper/0[148])
 CPU  PID    PRI      TIMEOUT  STAT      NAME
>  0  0       80      0        00500080  ROOT
   0  2931     0      0        00300380  testTimer12_cor
   0  2933    85      0        00300182  threadTimeOutEnd
   0  2934    80      0        00300182  threadTimeOut
   0  2935    80      0        00300380  threadDisplay

Master time base: clock=187444262538

c70bbeb0 00000000 00000000 00000001 c70bbed8 c01041e7 00000000 c0440251 00000b76 c70e1dec c70bbf3c c014e03e c043a6a0 c70c0000 00000b76 00300182 00000001 c70377ac 00000094 000000b1 00000094 c7074b90 00000086 c70e1220

Call Trace:
 [<c01041e7>] ? show_stack+0x37/0x50
 [<c014e03e>] ? schedule_event+0x3ae/0x880
 [<c0142bba>] ? xnpod_schedule+0x5fa/0x10f0
 [<c0106ea4>] ? native_sched_clock+0x64/0xb0
 [<c014dc90>] ? schedule_event+0x0/0x880
 [<c013c45c>] ? __ipipe_dispatch_event+0x9c/0x170
 [<c03da5d6>] ? schedule+0x226/0x3b0
 [<c013bafa>] ? __ipipe_walk_pipeline+0x6a/0xa0
 [<c014faed>] ? gatekeeper_thread+0xad/0x140
 [<c01117c0>] ? default_wake_function+0x0/0x10
 [<c014fa40>] ? gatekeeper_thread+0x0/0x140
 [<c0127ca2>] ? kthread+0x42/0x70
 [<c0127c60>] ? kthread+0x0/0x70
 [<c01032c7>] ? kernel_thread_helper+0x7/0x10
 =======================

A message that is not in the archive (sent off-list or not yet received): [email protected]

#46 lore raw
Quoted 4 lines · Fabien MAHOT

Hello,

I would like to know if you find something about my problem? Do you have more clues with the "frame pointers" debug?

Hi Fabien,

I found something. Could you try the attached patch, to see if we are spotting the right bug ?

TIA,

Signature · 1 line
					    Gilles.
nucleus/shadow.c +4 -0
Index: ksrc/nucleus/shadow.c
===================================================================
@@ -1002,6 +1002,8 @@ static int gatekeeper_thread(void *data)
 	up(&gk->sync);		/* Sync with xnshadow_mount(). */
 
 	for (;;) {
+		unsigned long flags;
+
 		set_current_state(TASK_INTERRUPTIBLE);
 		up(&gk->sync);	/* Make the request token available. */
 		schedule();
@@ -1018,6 +1020,7 @@ static int gatekeeper_thread(void *data)
 		   by a signal before we have been able to process the
 		   pending request, just ignore the latter. */
 
+		local_irq_save(flags);
 		if (xnthread_user_task(thread)->state == TASK_INTERRUPTIBLE) {
 			rpi_pop(thread);
 			xnlock_get_irqsave(&nklock, s);
@@ -1034,6 +1037,7 @@ static int gatekeeper_thread(void *data)
 			xnpod_schedule();
 			xnlock_put_irqrestore(&nklock, s);
 		}
+		local_irq_restore(flags);
 	}
 
 	return 0;
#47 lore raw
Quoted 10 lines · Gilles Chanteperdrix
Quoted 4 lines · Fabien MAHOT

Hello,

I would like to know if you find something about my problem? Do you have more clues with the "frame pointers" debug?

Hi Fabien,

I found something. Could you try the attached patch, to see if we are spotting the right bug ?

Ok. Here comes a better patch. To be applied to the I-pipe patched kernel.

Signature · 1 line
                                                 Gilles.
kernel/sched.c +0 -0
index a4bf7d3..0865f23 100644
@@ -2488,6 +2488,8 @@ context_switch(struct rq *rq, struct task_struct

*prev,

     barrier();

+ prev->state &= ~TASK_ATOMICSWITCH;
+

     if (task_hijacked(prev))
         return 1;
     /*
@@ -4161,11 +4163,9 @@ need_resched:
     rcu_qsctr_inc(cpu);
     prev = rq->curr;
     switch_count = &prev->nivcsw;
-     if (unlikely(prev->state & TASK_ATOMICSWITCH)) {
-         prev->state &= ~TASK_ATOMICSWITCH;
+     if (unlikely(prev->state & TASK_ATOMICSWITCH))
         /* Pop one disable level -- one still remains. */
         preempt_enable();

- }

     release_kernel_lock(prev);
 need_resched_nonpreemptible:
@@ -4216,8 +4216,10 @@ need_resched_nonpreemptible:
          */
         cpu = smp_processor_id();
         rq = cpu_rq(cpu);
-    } else
+    } else {
+        prev->state &= ~TASK_ATOMICSWITCH;
         spin_unlock_irq(&rq->lock);

+ }

     hrtick_set(rq);
#48 lore raw
In reply to Fabien MAHOT
Quoted 39 lines · Fabien MAHOT

Hello,

thank you very much for your work.

I tried your Xenomai 2.4.4.

I began to patch a linux kernel 2.6.20.21 with the i386 adeos patch. I would to know why you cancelled the i386 adeos patch for linux kernel 2.6.23 ?

So, with the 2.6.20.21, when I execute my test program, I hadn't got a big improvement. There were always bugs.

Then, I patched a linux kernel 2.6.25 with the x86 adeos patch. This time, there was a big improvement when I executed my test program.But, occasionally, there was a program crash, without bug messages in the kernel traces.
I added debug traces (calls to write function) in my program to understand why there was this crash. With theses new traces, I had a big crash of my program execution, and the system is also crashed. (I must reboot my system)
In this case, I had a Xenomai fatal message in the kernel traces.

To sum up my test program :

In the test program, there are three Xenomai thread :
threadTimeOutEnd (prio : 85): This thread waits the timeout end thanks to a Semaphore (In the timeout end handler, the Semaphore is posted), and warns threadTimeOut with a condvar broadcast. This thread calls display function to debug the program.
threadTimeOut (prio : 80): Malloc memory space to create timeout. Set-up timeouts of 500ms. Free memory space of achieved timeouts (This thread is notified of timeout end with a condvar broadcasted by threadTimeOutEnd). This thread must do the timeout end handler processing.This thread calls display function to debug the program.
threadDisplay (prio : 70): Call display function in a loop. Normally, this thread ends the test program and displays the stack of timeout end numbers. (if necesserray, adjust the loop counter)

Quoted 1 line

Sorry, my test program is a bit long

This program does not compile due to carriage return added by your mailer. This is not the first time I tell you that. Please resend it as an attachment.

Signature · 1 line
					    Gilles.
#50 lore raw
Quoted 1 line · Fabien MAHOT

OK, thanks for your help.

Your program does not cause any oops on my test board. Do you get these errors on an SMP or UP system ?

Signature · 1 line
                                                 Gilles Chanteperdrix
#51 lore raw
Quoted 6 lines

Your program does not cause any oops on my test board. Do you get these errors on an SMP or UP system ?

--
Gilles Chanteperdrix

I get these errors on a UP system. (processor: AMD Geode LX800)

#52 lore raw
In reply to Fabien MAHOT
Quoted 15 lines · Fabien MAHOT

Results on the console :

TimeOutCreation thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutCreation thread
TimeOut thread
Start of time out 1 - 5ms
Display thread
Start of time out 2 - 5ms
Start of time out 3 - 5ms
Display thread :8791 <---- the system crashed

Quoted 2 lines

I hope that you can help me. I will try to execute this program without your patch to see what kernel traces I will have if the kernel crashes.

I tried your program, and first it was hard to compile because:
- you use UTF-8 accents in the comments;
- your mailer add carriage returns when lines are too long, which usually does not matter with C code, but...
- you use C++ style comments where a carriage return matters, so when your mailer adds carriage returns in the middle of C++ comments, the compiler ends up trying to interpret your comments (with their UTF-8 accents) as code.

Anyway, it does not crash, it simply stops after displaying:

TimeOutCreation thread
TimeOut thread
Display thread
Main condition broadcast
TimeOutCreation thread
TimeOut thread

However, when pressing ctrl-C, I had a segmentation fault because cleanup_upon_sig used the timeOut*_ptr pointers without testint whether they were NULL.
Your program is way to big for me to try and understand it. So, no sorry, unless you come up with a real small and understandable sample, with understandable comments, I can not help you.

Quoted 1 line

I wonder why my system is so unsteady...

If you ask me, I will tell you that it is because you use Xenomai posix skin timers in user-space, that nobody else uses, since they bring nothing when compared with Linux vanilla timers. But I think I already told you that.

Anyway, I think I will try and re-implement the timers with a server thread without resorting to signals. The thing I do not know yet is if I can get the signal handlers without a system call.

Quoted 3 lines

Thanks a lot for your help.

Signature · 1 line
					    Gilles.
#53 lore raw
In reply to Fabien MAHOT
Quoted 3 lines · Fabien MAHOT

I hope that you can help me. I will try to execute this program without your patch to see what kernel traces I will have if the kernel crashes. I wonder why my system is so unsteady...

Ok. I finally got the crash. Please find attached a version of your program rewritten in "return value paranoid" mode. It is a good idea to always check the return value of any function you call, especially in test code. And especially in production code too. So, all the time in fact.

Signature · 1 line
					    Gilles.

#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <time.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <errno.h>

#define NB_PTR_TEMPO 3 // 5 tempos maximum
#define STACKSIZE 350

// stack
static int Stack[STACKSIZE];
static unsigned short Write_ptr = 0;
static unsigned short Read_ptr = 0;

// Display
pthread_mutex_t lockDisplay;
unsigned char bufferDisplay[2048];

// Timer

struct stTimeOut {
    timer_t timer_h;
    struct sigaction    sa;
    struct sigevent     sig_spec;
    struct itimerspec   tmr_setting;
    int number;
}*timeOut0_ptr, *timeOut1_ptr, *timeOut2_ptr, *timeOut3_ptr, *timeOut4_ptr;

// TimeOut_ptr
struct stTimeOut* tabTimeOut_ptr[STACKSIZE];
static unsigned short Write_to_ptr = 0;
static unsigned short Read_to_ptr = 0;

// Thread start
pthread_cond_t start_signal;
pthread_mutex_t main_start_lock;
bool bMainStart = false;
// Time-out end
pthread_cond_t TimeOutEnd_signal;
pthread_mutex_t timeOutEnd_lock;
bool bTimeOutEnd = false;
sem_t TimeOutWait_sem;

// Stack mutex
pthread_mutex_t Stack_lock;

void check(const char *file, int line, const char *service, int status, int err)

{
    if (status >= 0)
	return;

    fprintf(stderr, "%s:%d: %s: %s\n", file, line, service, strerror(err));
    exit(EXIT_FAILURE);
}
#define check_pthread(expr)		\
    ({						\
	int _status = (expr);                 \
	check(__FILE__, __LINE__, #expr, -_status, _status);	\
    })
#define check_unix(expr) \
    check(__FILE__, __LINE__, #expr, (expr), errno)

/************************ Stack functions *************************/

int StackCreation(void)

{
    Write_ptr = 0;
    Read_ptr  = 0;
    return 0;
}

void StackWrite(int number)

{
    if (Write_ptr >= STACKSIZE)
	Write_ptr = 0;
    Stack[Write_ptr++] = number;
}

int StackRead(void)

{
    int number;
    if (Read_ptr >= STACKSIZE)
	Read_ptr = 0;
    number = Stack[Read_ptr++];
    return number;
}

unsigned short GetWritePtr(void)

{
    return Write_ptr;
}

unsigned short GetReadPtr(void)

{
    return Read_ptr;
}

/************************ Functions ******************************/ void display(char * chaine,...)

{
    check_pthread(pthread_mutex_lock(&lockDisplay));
    va_list ArgDisplay;
    va_start(ArgDisplay, chaine);
    check_unix(vsprintf((char *)bufferDisplay,chaine,ArgDisplay));
    check_unix(write(2,(char*)bufferDisplay, strlen((char *)bufferDisplay)));
    //printf(bufferDisplay);
    check_pthread(pthread_mutex_unlock(&lockDisplay));
}

int func(volatile int* i)

{
    return (*i)++;
}

void DeleteTimer(timer_t timer)

{
    if (timer!=NULL)
	check_unix(timer_delete(timer));
}

void EndTimeOut (int signo,siginfo_t *info,void*context)

{
    volatile int i, result = 0;

    DeleteTimer(((struct stTimeOut*)(info->si_value.sival_ptr))->timer_h);

    StackWrite(((struct stTimeOut*)(info->si_value.sival_ptr))->number);

    check_unix(sem_post(&TimeOutWait_sem));
}

void StartTimeOut (int nb_Sec, int nb_nSec, struct stTimeOut* timeOut)

{
    (timeOut->sa).sa_flags = SA_SIGINFO;
    (timeOut->sa).sa_sigaction = EndTimeOut;

    check_unix(sigaction(SIGRTMIN, &(timeOut->sa), NULL));

    (timeOut->sig_spec).sigev_notify = SIGEV_SIGNAL;
    (timeOut->sig_spec).sigev_signo = SIGRTMIN;
    (timeOut->sig_spec).sigev_value.sival_ptr = timeOut;

    check_unix(timer_create(CLOCK_REALTIME, &(timeOut->sig_spec),
			    &(timeOut->timer_h)));

    (timeOut->tmr_setting).it_value.tv_sec = nb_Sec;
    (timeOut->tmr_setting).it_value.tv_nsec = nb_nSec;
    (timeOut->tmr_setting).it_interval.tv_sec = 0;
    (timeOut->tmr_setting).it_interval.tv_nsec = 0;

    check_unix(timer_settime((timeOut->timer_h), 0,
			     &(timeOut->tmr_setting),NULL));
}

/************************** Threads ********************************/ void* threadTimeOutEnd(void * arg) {

    int NbSem = 0;
    bool bSemWaitError;

    display("TimeOutCreation thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart) {
	check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    }
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("TimeOutCreation thread\n");

    while (1) {
	while ((bSemWaitError = sem_wait(&TimeOutWait_sem)) < 0
	       && errno == EINTR)
	    ;
	if (bSemWaitError) {
	    fprintf(stderr, "sem_wait(&TimeOutWait_sem): %s\n", strerror(errno));
	    exit(EXIT_FAILURE);
	}

	check_pthread(pthread_mutex_lock(&timeOutEnd_lock));
	check_pthread(pthread_cond_broadcast(&TimeOutEnd_signal));
	bTimeOutEnd = true;
	check_pthread(pthread_mutex_unlock(&timeOutEnd_lock));
    }
}
void* threadTimeOut(void * arg) {
    int i=1;
    int j, k, NbTimeOut, numTimeOut;

    timeOut0_ptr = timeOut1_ptr = timeOut2_ptr = timeOut3_ptr =
	timeOut4_ptr = NULL;

    display("TimeOut thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
	check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("TimeOut thread\n");

    while (i < 100) {
	// Malloc and start of time out
	for (j=0 ; j < NB_PTR_TEMPO; j++) {
	    switch(j) {
	    case 0 : if (timeOut0_ptr == NULL) {
		    timeOut0_ptr=(struct stTimeOut*)malloc(sizeof(struct stTimeOut));
		    if (timeOut0_ptr == NULL) exit(1);
		    timeOut0_ptr->number = i;
		    i++;
		    display("Start of time out %d - 5ms\n",
			    timeOut0_ptr->number);
		    StartTimeOut(0,500000000,timeOut0_ptr);
		}
		break;
	    case 1 : if (timeOut1_ptr == NULL) {
		    timeOut1_ptr=(struct stTimeOut*)malloc(sizeof(struct stTimeOut));
		    if (timeOut1_ptr == NULL) exit(1);
		    timeOut1_ptr->number = i;
		    i++;
		    display("Start of time out %d - 5ms\n",
			    timeOut1_ptr->number);
		    StartTimeOut(0,500000000,timeOut1_ptr);
		}
		break;
	    case 2 : if (timeOut2_ptr == NULL) {
		    timeOut2_ptr=(struct stTimeOut*)malloc(sizeof(struct stTimeOut));
		    if (timeOut2_ptr == NULL) exit(1);
		    timeOut2_ptr->number = i;
		    i++;
		    display("Start of time out %d - 5ms\n",
			    timeOut2_ptr->number);
		    StartTimeOut(0,500000000,timeOut2_ptr);
		}
		break;
	    case 3 : if (timeOut3_ptr == NULL) {
		    timeOut3_ptr=(struct stTimeOut*)malloc(sizeof(struct stTimeOut));
		    if (timeOut3_ptr == NULL) exit(1);
		    timeOut3_ptr->number = i;
		    i++;
		    display("Start of time out %d - 5ms\n",
			    timeOut3_ptr->number);
		    StartTimeOut(0,500000000,timeOut3_ptr);
		}
		break;
	    case 4 : if (timeOut4_ptr == NULL) {
		    timeOut4_ptr=(struct stTimeOut*)malloc(sizeof(struct stTimeOut));
		    if (timeOut4_ptr == NULL) exit(1);
		    timeOut4_ptr->number = i;
		    i++;
		    display("Start of time out %d - 5ms\n",
			    timeOut4_ptr->number);
		    StartTimeOut(0,500000000,timeOut4_ptr);
		}
		break;
	    }
	}

	check_pthread(pthread_mutex_lock(&timeOutEnd_lock));
	while (!bTimeOutEnd)
	    check_pthread(pthread_cond_wait(&TimeOutEnd_signal,
					    &timeOutEnd_lock));
	bTimeOutEnd = false;

	check_pthread(pthread_mutex_unlock(&timeOutEnd_lock));

	NbTimeOut = GetWritePtr() - GetReadPtr();
	display("Number of time-outs ends : %d\n", NbTimeOut);
	for (j=0; j < NbTimeOut; j++) {
	    numTimeOut = StackRead();
	    display("TimeOut%d ends\n", numTimeOut);
	    for (k=0; k < NB_PTR_TEMPO; k++) {
		switch(k) {
		case 0 : if (timeOut0_ptr != NULL) {
			if (timeOut0_ptr->number == numTimeOut) {
			    free(timeOut0_ptr);
			    timeOut0_ptr = NULL;
			}
		    }
		    break;
		case 1 : if (timeOut1_ptr != NULL) {
			if (timeOut1_ptr->number == numTimeOut) {
			    free(timeOut1_ptr);
			    timeOut1_ptr = NULL;
			}
		    }
		    break;
		case 2 : if (timeOut2_ptr != NULL) {
			if (timeOut2_ptr->number == numTimeOut) {
			    free(timeOut2_ptr);
			    timeOut2_ptr = NULL;
			}
		    }
		    break;
		case 3 : if (timeOut3_ptr != NULL) {
			if (timeOut3_ptr->number == numTimeOut) {
			    free(timeOut3_ptr);
			    timeOut3_ptr = NULL;
			}
		    }
		    break;
		case 4 : if (timeOut4_ptr != NULL) {
			if (timeOut4_ptr->number == numTimeOut) {
			    free(timeOut4_ptr);
			    timeOut4_ptr = NULL;
			}
		    }
		    break;
		}
	    }
	}
    }
    while(1) {
	sleep(10);
    }
    return NULL;
}
void* threadDisplay(void * arg) {
    volatile int i=0;
    volatile int result;
    display("Display thread\n");
    check_pthread(pthread_mutex_lock(&main_start_lock));
    while (!bMainStart)
	check_pthread(pthread_cond_wait(&start_signal, &main_start_lock));
    check_pthread(pthread_mutex_unlock(&main_start_lock));
    display("Display thread\n");

    while (i <= 200000) {
	result = func(&i);
	display("Display thread :%d \r",result);
    }
    display("End of display thread\n");
    return NULL;
}

/***********************************************************************/ void cleanup_upon_sig(int sig __attribute__((unused)))

{
    if (timeOut0_ptr)
	DeleteTimer(timeOut0_ptr->timer_h);
    if (timeOut1_ptr)
	DeleteTimer(timeOut1_ptr->timer_h);
    if (timeOut2_ptr)
	DeleteTimer(timeOut2_ptr->timer_h);
    exit(0);
}
int main(int argc, char** argv) {
    pthread_attr_t attr;
    pthread_mutexattr_t attr_proto;
    pthread_t p0;
    pthread_t p1;
    pthread_t p2;
    struct sched_param sch;

    // Stack & TabTImeOutptr creation
    StackCreation();

    check_unix(signal(SIGINT, cleanup_upon_sig));
    check_unix(signal(SIGTERM, cleanup_upon_sig));
		   
    check_unix(mlockall(MCL_CURRENT|MCL_FUTURE));

    // mutex and sem initialisation
    check_pthread(pthread_cond_init(&start_signal, NULL));
    check_pthread(pthread_mutex_init(&main_start_lock, NULL));
    check_pthread(pthread_cond_init(&TimeOutEnd_signal, NULL));
    check_pthread(pthread_mutex_init(&timeOutEnd_lock, NULL));
    check_unix(sem_init (&TimeOutWait_sem, 0,0));
    check_pthread(pthread_mutexattr_init(&attr_proto));
    check_pthread(pthread_mutexattr_setprotocol(&attr_proto,PTHREAD_PRIO_INHERIT));
    check_pthread(pthread_mutex_init(&lockDisplay, &attr_proto));

    check_pthread(pthread_attr_init(&attr));
    check_pthread(pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED));
    check_pthread(pthread_attr_setschedpolicy(&attr, SCHED_FIFO));

    // TimeOutCreation thread creation
    sch.sched_priority = 85;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p0, &attr, threadTimeOutEnd, NULL));

    // TimeOut thread creation
    sch.sched_priority = 80;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p1, &attr, threadTimeOut, NULL));

    // Display thread creation
    sch.sched_priority = 70;
    check_pthread(pthread_attr_setschedparam(&attr, &sch));
    check_pthread(pthread_create(&p2, &attr, threadDisplay, NULL));

    check_pthread(pthread_attr_destroy(&attr));

    display("Main condition broadcast\n");
    // Start of all threads
    check_pthread(pthread_mutex_lock(&main_start_lock));
    bMainStart = true;
    check_pthread(pthread_cond_broadcast(&start_signal));
    check_pthread(pthread_mutex_unlock(&main_start_lock));

    while (1) {
	sleep(5);
    }

    return 0;
}