Skip to content

testing/ostest: Queue and cancel work in the critical section. - #4111

Draft
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:ostest-wq-csection
Draft

casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:ostest-wq-csection

Conversation

@casaroli

@casaroli casaroli commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

depends-on: apache/nuttx/pull/22164

Summary

A new case of the ostest work queue test, for apache/nuttx#22164: 2 * CONFIG_SMP_NCPUS threads (2 threads without SMP) queue delayed and immediate work on a custom queue and cancel the delayed work, 2000 times each. The threads are not pinned to a CPU. Half of the threads do it in the critical section.

The work queue starts and stops its timer and wakes its workers with the queue locked, and these calls enter the critical section. On SMP NuttX master, a thread in the critical section waits for the queue lock while another CPU holds the queue lock and waits for the critical section, and the test stops.

Impact

Only testing/ostest/wqueue.c, in the flat build (the other builds have no enter_critical_section() in user space). No new Kconfig option. Without SMP the case runs with two threads.

Testing

qemu-armv8a:nsh under HVF with 4 CPUs: on NuttX master, ostest stops in wqueue_test: critical section; with apache/nuttx#22164, ostest ends with status 0. Built for qemu-armv7a:smp and qemu-armv7a:nsh.

tools/checkpatch.sh -c -u -m -g passes.

@cederom

cederom commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Thanks, this test does reproduce the inversion fixed by apache/nuttx#22164:

  • Threads with incsection set hold the critical section and then take the queue lock in work_queue_wq()/work_cancel_wq().
  • The other threads and the queue's workers take the queue lock and then enter the critical section in wd_start(), wd_cancel() and nxsem_post().

The guards look right. The code is under !CONFIG_DISABLE_PTHREAD from the enclosing block, and CONFIG_BUILD_FLAT covers enter_critical_section() and the kernel work_queue_wq(). In protected and kernel builds, the custom queues are the libc user implementation, which has no such inversion.

Comments:

  1. Merge order. On current master, this test makes SMP ostest hang rather than fail. It needs to land together with or after sched/wqueue: Lock the work queues with the critical section. nuttx#22164, otherwise the nuttx-apps CI for SMP sim/qemu configs will time out. Please cross-link the two PRs and say so in the description.

  2. CPU placement isn't enforced. The commit message says "two threads per CPU" and the comment says "the threads on some CPUs ... the threads on the other CPUs". But the threads are created without affinity, so nothing guarantees that a thread inside the critical section and one outside it run on different CPUs at the same time. On SMP, pinning would make the test deterministic about what it claims:

    #ifdef CONFIG_SMP
      cpu_set_t cpuset;
      CPU_ZERO(&cpuset);
      CPU_SET((i / 2) % CONFIG_SMP_NCPUS, &cpuset);
      pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset);
    #endif
    

    Alternatively, change the wording to "half of the threads".

  3. Failure mode. If it regresses, the result is a silent hang, not an ASSERT. That's acceptable for ostest, but a one-line comment above csection_test() saying "a deadlock here hangs ostest" would save the next person some time.

  4. Nit: struct csection_s and CSECTION_LOOPS/CSECTION_THREADS are only used under CONFIG_BUILD_FLAT. They could move inside that guard, next to csection_thread().

The rest looks good: the final work_cancel_sync_wq() plus the work_available() checks, the cleanup with work_queue_free(), and the loop count. nxstyle is clean.

@cederom

cederom commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Thanks for the update. I compared it with the previous version (e3077e8..40066f8):

  • Failure mode (comment 3): done. The comment above the test now says that a deadlock hangs ostest and no ASSERT reports it.
  • Guards (nit 4): done. CSECTION_LOOPS, CSECTION_THREADS and struct csection_s are now under CONFIG_BUILD_FLAT, next to the code that uses them.
  • CPU placement (comment 2): the code comment now says "Half of the threads ... the other half", which matches what the test does. But the commit message still says "two threads per CPU", and the threads are still created without affinity. Please change the commit message to match, e.g. "2 * CONFIG_SMP_NCPUS threads, half of them in the critical section". Pinning the threads is still optional.
  • Merge order (comment 1): this one is about the PR, not the code. On master without sched/wqueue: Lock the work queues with the critical section. nuttx#22164, this test makes SMP ostest hang. Please cross-link the two PRs and merge this one only together with or after the kernel fix, otherwise the SMP CI jobs here will time out.

The code changes look good. LGTM once the commit message is fixed and the kernel PR is merged first.

Add a work queue test: 2 * CONFIG_SMP_NCPUS threads (2 threads without
SMP) queue delayed and immediate work and cancel the delayed work in a
loop.  Half of the threads do it in the critical section.  The threads
are not pinned to a CPU.  The work queue enters the critical section to
start its timer and to wake a worker, so this checks that the two orders
do not stop the CPUs on SMP.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli

Copy link
Copy Markdown
Contributor Author

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants