Skip to content

Commit

Permalink
kernel: queue: k_queue_poll: Fix slist access race condition
Browse files Browse the repository at this point in the history
All sys_slist_*() functions aren't threadsafe and calls to them
must be protected with irq_lock. This is usually done in a wider
caller context, but k_queue_poll() is called with irq_lock already
relinquished, and is thus subject to hard to detect and explain
race conditions, as e.g. was tracked in zephyrproject-rtos#4022.

Signed-off-by: Paul Sokolovsky <[email protected]>
  • Loading branch information
pfalcon committed Oct 16, 2017
1 parent de6fafb commit a8cbbbb
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion kernel/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ static void *k_queue_poll(struct k_queue *queue, s32_t timeout)
{
struct k_poll_event event;
int err;
unsigned int key;
void *val;

k_poll_event_init(&event, K_POLL_TYPE_FIFO_DATA_AVAILABLE,
K_POLL_MODE_NOTIFY_ONLY, queue);
Expand All @@ -212,7 +214,13 @@ static void *k_queue_poll(struct k_queue *queue, s32_t timeout)

__ASSERT_NO_MSG(event.state == K_POLL_STATE_FIFO_DATA_AVAILABLE);

return sys_slist_get(&queue->data_q);
/* sys_slist_* aren't threadsafe, so must be always protected by
* irq_lock.
*/
key = irq_lock();
val = sys_slist_get(&queue->data_q);
irq_unlock(key);
return val;
}
#endif /* CONFIG_POLL */

Expand Down

0 comments on commit a8cbbbb

Please sign in to comment.