I've got a problem with queues that are implemented in chibiOS 2.0 (same in newest version), it seems they can over-write/over-read in some special scenarios, I'll make a short example here, using an output queue and
chOQPutTimeout
Code: Select all
msg_t chOQPutTimeout(OutputQueue *oqp, uint8_t b, systime_t time) {
msg_t msg;
chSysLock();
if ((msg = chSemWaitTimeoutS(&oqp->q_sem, time)) < RDY_OK) {
chSysUnlock();
return msg;
}
*oqp->q_wrptr++ = b;
if (oqp->q_wrptr >= oqp->q_top)
oqp->q_wrptr = oqp->q_buffer;
if (oqp->q_notify)
oqp->q_notify(oqp);
chSysUnlock();
return Q_OK;
}
Thread W tries to write to the queue, but it is full, so it will start sleeping in chSemWaitTimeoutS, after this Thread A, a high priority thread starts running and does some important and a bit lengthy operation.
While this is happening the interrupt that reads the output queue kicks in and reads one byte, and signals the semaphore, that there is room in the queue again, which will put the thread in ready state, but since Thread A has higher priority, the scheduler won't actually start the writer thread again. Again the interrupt kicks in, and reads a byte, and it actually kicks in so many times that it reads the whole queue empty, while the writer thread had no opportunity to finish the writing, and since the semaphore was already decreased when the waiting started, the interrupt reads the byte that was supposed to be written by Thread W.
This happened with an STM32 while trying to upload a GSM firmware (2.5 MB) through a serial port.
I have a suggested fix for this with putting additional logic in to the queues, a counter which only gets modified in the lock zone, and additional checks after we come out of the sleep:
new field in the GenericQueue struct: cnt_t q_counter, acts the same way as the semaphore, but won't go into negative values, and only get's modified in lock zone, where no sleeping occurs
chQSpace(q) uses this new counter, so: #define chQSpace(q) (q)->q_counter
The additional if after the semaphore wait, is required to detect whether there was a queue reset, after the semaphore was signaled.
Actual case that happened:
With input queue, a thread was trying to read from it, but it was empty, so it went into the semaphore wait. A high priority thread started running, and an interrupt put a byte into the queue, and signaled the semaphore, which again did not resume running because of the high priority thread. The interrupt got called again, but something bad happened and it decided that the queue has to be reseted, so it resets it. The high priority thread stops running, and the reader thread can resume, and now would read from a reseted queue. (Code was slightly different then, and it caused an infinite loop in syslock, and thus a hardware watchdog reset)
Code: Select all
msg_t chOQPutTimeout(OutputQueue *oqp, uint8_t b, systime_t time) {
msg_t msg;
chSysLock();
if ((msg = chSemWaitTimeoutS(&oqp->q_sem, time)) < RDY_OK) {
chSysUnlock();
return msg;
}
if (oqp->q_sem.s_cnt >= oqp->q_counter) {
chSysUnlock();
return RDY_RESET;
}
*oqp->q_wrptr++ = b;
--oqp->q_counter;
if (oqp->q_wrptr >= oqp->q_top)
oqp->q_wrptr = oqp->q_buffer;
if (oqp->q_notify)
oqp->q_notify();
chSysUnlock();
return Q_OK;
}
If you'd like, I can upload the modified files, just tell me, where should I do that.