Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport v4.0-branch] lib: os: mpsc_pbuf: do not wait when spinlock is held #83577

Open
wants to merge 1 commit into
base: v4.0-branch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/os/mpsc_pbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ union mpsc_pbuf_generic *mpsc_pbuf_alloc(struct mpsc_pbuf_buffer *buffer,
add_skip_item(buffer, free_wlen);
cont = true;
} else if (IS_ENABLED(CONFIG_MULTITHREADING) && !K_TIMEOUT_EQ(timeout, K_NO_WAIT) &&
!k_is_in_isr()) {
!k_is_in_isr() && arch_irq_unlocked(key.key)) {
int err;

k_spin_unlock(&buffer->lock, key);
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/mpsc_pbuf/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1294,5 +1294,28 @@
zassert_true(packet == NULL);
}

/* Make sure that `mpsc_pbuf_alloc()` works in spinlock-held context when buf is not available */
ZTEST(log_buffer, test_alloc_in_spinlock)
{
struct mpsc_pbuf_buffer buffer;
struct test_data_var *packet;
struct k_spinlock l = {0};

init(&buffer, 32, false);

/* Allocate all available buffer */
packet = (struct test_data_var *)mpsc_pbuf_alloc(
&buffer, 32, K_MSEC(10));
zassert_not_null(packet);

K_SPINLOCK(&l) {
/* Try to allocate another buf */
packet = (struct test_data_var *)mpsc_pbuf_alloc(
&buffer, 32, K_MSEC(10));
/* No buf is available this time */

Check notice on line 1315 in tests/lib/mpsc_pbuf/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/lib/mpsc_pbuf/src/main.c:1315 - packet = (struct test_data_var *)mpsc_pbuf_alloc( - &buffer, 32, K_MSEC(10)); + packet = (struct test_data_var *)mpsc_pbuf_alloc(&buffer, 32, K_MSEC(10)); zassert_not_null(packet); K_SPINLOCK(&l) { /* Try to allocate another buf */ - packet = (struct test_data_var *)mpsc_pbuf_alloc( - &buffer, 32, K_MSEC(10)); + packet = (struct test_data_var *)mpsc_pbuf_alloc(&buffer, 32, K_MSEC(10));

Check notice on line 1315 in tests/lib/mpsc_pbuf/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/lib/mpsc_pbuf/src/main.c:1315 - packet = (struct test_data_var *)mpsc_pbuf_alloc( - &buffer, 32, K_MSEC(10)); + packet = (struct test_data_var *)mpsc_pbuf_alloc(&buffer, 32, K_MSEC(10)); zassert_not_null(packet); K_SPINLOCK(&l) { /* Try to allocate another buf */ - packet = (struct test_data_var *)mpsc_pbuf_alloc( - &buffer, 32, K_MSEC(10)); + packet = (struct test_data_var *)mpsc_pbuf_alloc(&buffer, 32, K_MSEC(10));
zassert_is_null(packet);
}
}

/*test case main entry*/
ZTEST_SUITE(log_buffer, NULL, NULL, NULL, NULL, NULL);
Loading