Skip to content

Commit 7380241

Browse files
Damian-Nordickartben
authored andcommitted
lib: os: mpsc_pbuf: take spinlock in utilization getters
Two public functions do not take the spinlock even though they access mutable, non-atomic members of the buffer: - mpsc_pbuf_get_utilization() - mpsc_pbuf_get_max_utilization() Take the spinlock to avoid possible data races. Signed-off-by: Damian Krolik <[email protected]>
1 parent ece3f3b commit 7380241

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/os/mpsc_pbuf.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,18 +627,28 @@ bool mpsc_pbuf_is_pending(struct mpsc_pbuf_buffer *buffer)
627627
void mpsc_pbuf_get_utilization(struct mpsc_pbuf_buffer *buffer,
628628
uint32_t *size, uint32_t *now)
629629
{
630+
k_spinlock_key_t key = k_spin_lock(&buffer->lock);
631+
630632
/* One byte is left for full/empty distinction. */
631633
*size = (buffer->size - 1) * sizeof(int);
632634
*now = get_usage(buffer) * sizeof(int);
635+
636+
k_spin_unlock(&buffer->lock, key);
633637
}
634638

635639
int mpsc_pbuf_get_max_utilization(struct mpsc_pbuf_buffer *buffer, uint32_t *max)
636640
{
641+
int rc;
642+
k_spinlock_key_t key = k_spin_lock(&buffer->lock);
637643

638-
if (!(buffer->flags & MPSC_PBUF_MAX_UTILIZATION)) {
639-
return -ENOTSUP;
644+
if (buffer->flags & MPSC_PBUF_MAX_UTILIZATION) {
645+
*max = buffer->max_usage * sizeof(int);
646+
rc = 0;
647+
} else {
648+
rc = -ENOTSUP;
640649
}
641650

642-
*max = buffer->max_usage * sizeof(int);
643-
return 0;
651+
k_spin_unlock(&buffer->lock, key);
652+
653+
return rc;
644654
}

0 commit comments

Comments
 (0)