From d8782e5d0a4b53ad441a793fcd8e2ddf448d9d80 Mon Sep 17 00:00:00 2001 From: Nicolas Pitre Date: Wed, 8 Oct 2025 17:06:05 -0400 Subject: [PATCH] tests: lib: lockfree: Fix MPSC test consumer node return logic The MPSC test consumer was incorrectly handling node returns to producer free queues. It called spsc_acquire() without checking the return value, then unconditionally called spsc_produce(). When the SPSC free queue was full, spsc_acquire() would return NULL and not increment the acquire counter, causing spsc_produce() to silently do nothing (it only produces if acquire > 0). This resulted in lost nodes. At least on QEMU, a 4-CPU configuration is needed for this bug to manifest. Producers put all nodes in flight simultaneously with the single consumer unable to keep up, causing frequent SPSC queue full conditions. The Fix: Loop on spsc_acquire() until it succeeds before calling spsc_produce(). This ensures nodes are always successfully returned to producer queues. Added board overlay for qemu_cortex_a53/smp to enable 4-CPU testing, which reproduces the issue and validates the fix. Signed-off-by: Nicolas Pitre --- .../qemu_cortex_a53_qemu_cortex_a53_smp.conf | 2 ++ ...emu_cortex_a53_qemu_cortex_a53_smp.overlay | 22 +++++++++++++++++++ tests/lib/lockfree/src/test_mpsc.c | 5 ++++- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/lib/lockfree/boards/qemu_cortex_a53_qemu_cortex_a53_smp.conf create mode 100644 tests/lib/lockfree/boards/qemu_cortex_a53_qemu_cortex_a53_smp.overlay diff --git a/tests/lib/lockfree/boards/qemu_cortex_a53_qemu_cortex_a53_smp.conf b/tests/lib/lockfree/boards/qemu_cortex_a53_qemu_cortex_a53_smp.conf new file mode 100644 index 0000000000000..9eb7d2bea94db --- /dev/null +++ b/tests/lib/lockfree/boards/qemu_cortex_a53_qemu_cortex_a53_smp.conf @@ -0,0 +1,2 @@ +# Configuration overlay for 4-CPU testing on QEMU Cortex-A53 +CONFIG_MP_MAX_NUM_CPUS=4 diff --git a/tests/lib/lockfree/boards/qemu_cortex_a53_qemu_cortex_a53_smp.overlay b/tests/lib/lockfree/boards/qemu_cortex_a53_qemu_cortex_a53_smp.overlay new file mode 100644 index 0000000000000..aa91adf0a64bb --- /dev/null +++ b/tests/lib/lockfree/boards/qemu_cortex_a53_qemu_cortex_a53_smp.overlay @@ -0,0 +1,22 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Device tree overlay to add 2 more CPUs to QEMU Cortex-A53 SMP + * for testing 4-CPU configurations + */ + +/ { + cpus { + cpu@2 { + device_type = "cpu"; + compatible = "arm,cortex-a53"; + reg = <2>; + }; + + cpu@3 { + device_type = "cpu"; + compatible = "arm,cortex-a53"; + reg = <3>; + }; + }; +}; diff --git a/tests/lib/lockfree/src/test_mpsc.c b/tests/lib/lockfree/src/test_mpsc.c index 2085d26b32d6f..35c278bc0f67e 100644 --- a/tests/lib/lockfree/src/test_mpsc.c +++ b/tests/lib/lockfree/src/test_mpsc.c @@ -127,7 +127,10 @@ static void mpsc_consumer(void *p1, void *p2, void *p3) nn = CONTAINER_OF(n, struct test_mpsc_node, n); - spsc_acquire(node_q[nn->id]); + /* Return node to producer's free queue - must retry if queue is full */ + while (spsc_acquire(node_q[nn->id]) == NULL) { + k_yield(); + } spsc_produce(node_q[nn->id]); } }