You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/02/04 15:58:26 UTC

[GitHub] [incubator-nuttx] szymonnnm opened a new issue #5412: Calling mq_send() from an interrupt triggers assertion

szymonnnm opened a new issue #5412:
URL: https://github.com/apache/incubator-nuttx/issues/5412


   Hello everyone,
   
   I have a problem with the message queue driver. I am currently porting our project of the current version of NuttX. So up until now, our project was using NuttX 8.2 and I am working on making it functional with NuttX 10.2. 
   
   Anyway, in one part of our project, we want to put some data on a message queue from an interrupt service routine. 
   ```c
   static int stm32_ubx_tp_irq_handler(int irq, FAR void *context, FAR void *arg)
   {
     struct data_to_send dummy;
     (....)
     mq_send(mq, (char*)&dummy, sizeof(struct data_to_send), 0);
     return 0;
   }
   ```
   
   And it worked flawlessly on NuttX 8.2, but after porting the project to NuttX 10.2 this operation triggers assertion. 
   [To be specific, this is the assertion that is being triggered](https://github.com/apache/incubator-nuttx/blob/master/sched/semaphore/sem_wait.c#L78):
   ```c
   int nxsem_wait(FAR sem_t *sem)
   {
     FAR struct tcb_s *rtcb = this_task();
     irqstate_t flags;
     int ret = -EINVAL;
   
     /* This API should not be called from interrupt handlers */
   
     DEBUGASSERT(sem != NULL && up_interrupt_context() == false);
   ```
   
   So, I have looked into the history of changes and it turns out that between 8.2 and 10.2 the function `nxmq_send()` has been modified.  In the older version, it was only executing the logic related to putting an item in the message queue, but now it has to obtain the file struct instance first, and obtaining it using regular mechanisms fails because of the fact that we are doing it from an ISR.
   The current call order is like this:
   `mq_send -> nxmq_send -> fs_getfilep -> _files_semtake -> nxsem_wait_uninterruptible -> nxsem_wait -> ASSERTION TRIGGERED`
   
   So my question is, what is the correct way on NuttX 10.2 to send an item using a message queue from ISR? [The documentation doesn't say anything about it not being possible](https://cwiki.apache.org/confluence/display/NUTTX/User+Guide#Message_Queue). I was also not able to find an alternative API (some RTOSes I have worked with in the past had two separate functions for that, like for example `msgq_send()` and `msgq_send_from_isr()`)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] szymonnnm commented on issue #5412: Calling mq_send() from an interrupt triggers assertion

Posted by GitBox <gi...@apache.org>.
szymonnnm commented on issue #5412:
URL: https://github.com/apache/incubator-nuttx/issues/5412#issuecomment-1031214115


   @xiaoxiang781216  @patacongo 
   Thanks a lot for your comments. I modified my code a little bit to change the function that is called from the ISR to `file_mq_send()` and in fact - it works now and doesn't trigger any assertions. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] patacongo commented on issue #5412: Calling mq_send() from an interrupt triggers assertion

Posted by GitBox <gi...@apache.org>.
patacongo commented on issue #5412:
URL: https://github.com/apache/incubator-nuttx/issues/5412#issuecomment-1030362138


   Yes, NuttX always supporting sending messages from interrupt handlers.  
   Sorry to hear that has been broken.
   
   The original IPC philosophy was that the providing side of all IPCs 
   should work from interrupt handlers:  messages, semaphores, signals, etc.
   
   On 2/4/2022 9:58 AM, szymonm wrote:
   >
   > Hello everyone,
   >
   > I have a problem with the message queue driver. I am currently porting 
   > our project of the current version of NuttX. So up until now, our 
   > project was using NuttX 8.2 and I am working on making it functional 
   > with NuttX 10.2.
   >
   > Anyway, in one part of our project, we want to put some data on a 
   > message queue from an interrupt service routine.
   >
   > static  int  stm32_ubx_tp_irq_handler(int  irq, FARvoid  *context, FARvoid  *arg)
   > {
   >    struct  data_to_send dummy;
   >    (....)
   >    mq_send(mq, (char*)&dummy,sizeof(struct  data_to_send),0);
   >    return  0;
   > }
   >
   > And it worked flawlessly on NuttX 8.2, but after porting the project 
   > to NuttX 10.2 this operation triggers assertion.
   > To be specific, this is the assertion that is being triggered 
   > <https://github.com/apache/incubator-nuttx/blob/master/sched/semaphore/sem_wait.c#L78>:
   >
   > int  nxsem_wait(FARsem_t  *sem)
   > {
   >    FARstruct  tcb_s *rtcb =this_task();
   >    irqstate_t  flags;
   >    int  ret = -EINVAL;
   >
   >    /* This API should not be called from interrupt handlers */
   >
   >    DEBUGASSERT(sem !=NULL  &&up_interrupt_context() ==false);
   >
   > So, I have looked into the history of changes and it turns out that 
   > between 8.2 and 10.2 the function |nxmq_send()| has been modified. In 
   > the older version, it was only executing the logic related to putting 
   > an item in the message queue, but now it has to obtain the file struct 
   > instance first, and obtaining it using regular mechanisms fails 
   > because of the fact that we are doing it from an ISR.
   > The current call order is like this:
   > |mq_send -> nxmq_send -> fs_getfilep -> _files_semtake -> 
   > nxsem_wait_uninterruptible -> nxsem_wait -> ASSERTION TRIGGERED|
   >
   > So my question is, what is the correct way on NuttX 10.2 to send an 
   > item using a message queue from ISR? The documentation doesn't say 
   > anything about it not being possible 
   > <https://cwiki.apache.org/confluence/display/NUTTX/User+Guide#Message_Queue>. 
   > I was also not able to find an alternative API (some RTOSes I have 
   > worked with in the past had two separate functions for that, like for 
   > example |msgq_send()| and |msgq_send_from_isr()|)
   >
   > —
   > Reply to this email directly, view it on GitHub 
   > <https://github.com/apache/incubator-nuttx/issues/5412>, or 
   > unsubscribe 
   > <https://github.com/notifications/unsubscribe-auth/ABFUG6TZ5G4QY5OUDEF5Z43UZPZSBANCNFSM5NSE5FTA>.
   > Triage notifications on the go with GitHub Mobile for iOS 
   > <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> 
   > or Android 
   > <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>. 
   >
   > You are receiving this because you are subscribed to this 
   > thread.Message ID: ***@***.***>
   >
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] szymonnnm closed issue #5412: Calling mq_send() from an interrupt triggers assertion

Posted by GitBox <gi...@apache.org>.
szymonnnm closed issue #5412:
URL: https://github.com/apache/incubator-nuttx/issues/5412


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on issue #5412: Calling mq_send() from an interrupt triggers assertion

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on issue #5412:
URL: https://github.com/apache/incubator-nuttx/issues/5412#issuecomment-1030190632


   > So, I have looked into the history of changes and it turns out that between 8.2 and 10.2 the function `nxmq_send()` has been modified. In the older version, it was only executing the logic related to putting an item in the message queue, but now it has to obtain the file struct instance first, and obtaining it using regular mechanisms fails because of the fact that we are doing it from an ISR. The current call order is like this: `mq_send -> nxmq_send -> fs_getfilep -> _files_semtake -> nxsem_wait_uninterruptible -> nxsem_wait -> ASSERTION TRIGGERED`
   > 
   
   The change of call chain is due to: https://github.com/apache/incubator-nuttx/pull/2631.
   The final goal is to support poll on mqueue: https://github.com/apache/incubator-nuttx/pull/2957.
   
   > So my question is, what is the correct way on NuttX 10.2 to send an item using a message queue from ISR? [The documentation doesn't say anything about it not being possible](https://cwiki.apache.org/confluence/display/NUTTX/User+Guide#Message_Queue). I was also not able to find an alternative API (some RTOSes I have worked with in the past had two separate functions for that, like for example `msgq_send()` and `msgq_send_from_isr()`)
   
   It isn't enough to just add msgq_send_from_isr because mqueue handler belong to a particular task, and it's impossible to use mqueue handler directly in the interrupt context.
   
   Instead, you need call file_mq_xxx in kernel side, but mq_xxx in user space.
   Please follow audio_null.c change in #2631 to adapter your driver code.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org