CMSIS-FreeRTOS icon indicating copy to clipboard operation
CMSIS-FreeRTOS copied to clipboard

Implementation of osKernelRestoreLock is dangerous and might leave the kernel locked

Open pakluba opened this issue 1 year ago • 1 comments

https://github.com/ARM-software/CMSIS-FreeRTOS/blob/ba82e7e1f62e72313992e47b88b442b40131bbdb/CMSIS/RTOS2/FreeRTOS/Source/cmsis_os2.c#L397

Unlike RTX, FreeRTOS does nesting in vTaskSuspendAll(). If the scheduling has already been suspended before osKernelLock() is called, osKernelRestoreLock() will increase the nesting level by 1 instead of removing 1 lock. So:

vTaskSuspendAll();           // the nesting level is at least 1
...
state = osKernelLock();      // does not increase nesting, returns 1 (was locked already)
...
osKernelRestoreLock(state);  // will call vTaskSuspendAll() to "restore" the lock -> boom...

pakluba avatar Jul 22 '24 08:07 pakluba

Hello, good find, thanks! I'll review the implementation and provide a fix.

VladimirUmek avatar Jul 22 '24 11:07 VladimirUmek

Thanks you for going forward and providing the PR! It has been merged hence I'm closing this issue.

VladimirUmek avatar Aug 19 '24 09:08 VladimirUmek