Skip to content

testing/ostest: spinlock test fixes - #3936

Open
Fix-Point wants to merge 6 commits into
apache:masterfrom
Fix-Point:apps-ostest-spinlock
Open

Fix-Point wants to merge 6 commits into
apache:masterfrom
Fix-Point:apps-ostest-spinlock

Conversation

@Fix-Point

@Fix-Point Fix-Point commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR included:

  1. Fix the affinity side-affect — the test pinned threads to a CPU but did not restore the previous affinity, leaking side effects into other tests;
  2. Update the comments to describe MAX_THREAD_NUM and the loop count;
  3. Add CONFIG_TESTING_OSTEST_SPINLOCK_PRIORITY — spinlock test threads were created at SCHED_PRIORITY_MAX (255), which starves watchdog-feeding tasks on systems with watchdog monitoring (e.g. BMP + WdgM), eventually triggering a WarmReset; the new option (default 255) lets platforms lower the priority, and is used in both spinlock_test() and run_test_thread();
  4. Fix the spinlock loop count to match the Kconfig description — TEST_LOOP_SCALE is documented as "N x 10,000" but the code used * 100000; aligned to * 10000;
  5. Lower the loop scale under MM_KASAN — avoid excessively slow runs (and watchdog implications) on KASAN builds;
  6. Fix a Tasking compiler warning (W597) alignment issue.

Impact

  • User impact: test-only; no public API changes.
  • Build: no build-system changes; testing/ostest builds as before.
  • Hardware: none (validated on qemu-armv7a, see Testing).
  • Documentation: none.
  • Security: none.
  • Compatibility: the spinlock test now restores thread affinity, uses the configurable thread priority, and matches the documented loop counts.

Testing

Host: x86-64 Linux, arm-none-eabi-gcc + QEMU (qemu-system-arm).

Config: qemu-armv7a:smp (-smp 4)

Result: spinlock_test completes on 1/2/3/4 threads with correct final counters (N x 1000000) and 0 errors.

Log:

qemu-system-arm -cpu cortex-a7 -nographic -smp 4 \
-machine virt,virtualization=off,gic-version=2 \
-net none -chardev stdio,id=con,mux=on -serial chardev:con \
-mon chardev=con,mode=readline -kernel ./nuttx
nsh> hello
Hello, World!!
spinlock_test: start......
Thread num: 1, Loop times: 1000000

Test type: spinlock
spinlock: Test Results:
spinlock: Final counter: 1000000
spinlock: Average throughput : 7881812 op/s
spinlock: Total execution time: 127776336 ns

Test type: rspinlock
rspinlock: Test Results:
rspinlock: Final counter: 1000000
rspinlock: Average throughput : 4003758 op/s
spinlock: Total execution time: 250123904 ns

Test type: seqcount
seqcount: Test Results:
seqcount: Final counter: 1000000
seqcount: Average throughput : 7803117 op/s
seqcount: Total execution time: 128437824 ns

Thread num: 2, Loop times: 1000000

Test type: spinlock
spinlock: Test Results:
spinlock: Final counter: 2000000
spinlock: Average throughput : 3639822 op/s
spinlock: Total execution time: 275360688 ns

Test type: rspinlock
rspinlock: Test Results:
rspinlock: Final counter: 2000000
rspinlock: Average throughput : 1967137 op/s
rspinlock: Total execution time: 509021424 ns

Test type: seqcount
seqcount: Test Results:
seqcount: Final counter: 2000000
seqcount: Average throughput : 2913931 op/s
seqcount: Total execution time: 343893376 ns

Thread num: 3, Loop times: 1000000

Test type: spinlock
spinlock: Test Results:
spinlock: Final counter: 3000000
spinlock: Average throughput : 2436294 op/s
spinlock: Total execution time: 426570192 ns

Test type: rspinlock
rspinlock: Test Results:
rspinlock: Final counter: 3000000
rspinlock: Average throughput : 1141461 op/s
rspinlock: Total execution time: 879273104 ns

Test type: seqcount
seqcount: Test Results:
seqcount: Final counter: 3000000
seqcount: Average throughput : 2063639 op/s
seqcount: Total execution time: 528608448 ns

Thread num: 4, Loop times: 1000000

Test type: spinlock
spinlock: Test Results:
spinlock: Final counter: 4000000
spinlock: Average throughput : 1554178 op/s
spinlock: Total execution time: 651331712 ns

Test type: rspinlock
rspinlock: Test Results:
rspinlock: Final counter: 4000000
rspinlock: Average throughput : 679824 op/s
rspinlock: Total execution time: 1506000272 ns

Test type: seqcount
seqcount: Test Results:
seqcount: Final counter: 4000000
seqcount: Average throughput : 1066522 op/s
seqcount: Total execution time: 949742336 ns

spinlock_test: end......
nsh>

Fix-Point and others added 6 commits October 29, 2026 11:26
This commit created a new pthread to fix the side-affect introduced by sched_setaffinity.

Assisted-By: mimo-v2.6-pro
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit updated the comments.

Assisted-By: mimo-v2.6-pro
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
The spinlock test threads were created with SCHED_PRIORITY_MAX (255),
which starves the watchdog feeding task (BSW_TASK_100MS, priority 191)
on BMP systems with WdgM enabled. The WdgM satellite alive counter
expires on secondary cores, triggering a WarmReset via SafetyErrMgr
and powering off the system through semihosting exit.
Add CONFIG_TESTING_OSTEST_SPINLOCK_PRIORITY Kconfig option (default 255)
to allow platforms with watchdog monitoring to configure a lower priority.
Use CONFIG_TESTING_OSTEST_SPINLOCK_PRIORITY in both spinlock_test() and
run_test_thread() instead of hardcoded priority values.

Assisted-By: mimo-v2.6-pro
Signed-off-by: tengshuangshuang <tengshuangshuang@xiaomi.com>
…tion

The LOOP_TIMES macro used CONFIG_TEST_LOOP_SCALE * 100000, but the
Kconfig description states "N x 10,000". With the default scale of 100,
this resulted in 10,000,000 iterations per lock type instead of the
intended 1,000,000, causing ostest to exceed its 800-second timeout on
QEMU targets with KASAN instrumentation enabled.
Align the multiplier with the Kconfig description by changing 100000
to 10000.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: vela-autotest03 <vela-autotest03@xiaomi.com>
The spinlock/rspinlock/seqcount performance test executes
LOOP_TIMES = CONFIG_TEST_LOOP_SCALE * 100000 lock/unlock pairs per
thread per lock type. With the default scale of 100 that is 10M
operations per lock type, or 30M in total.
When CONFIG_MM_KASAN is enabled every spin_lock_irqsave goes through
kasan_check_report, which itself takes the global kasan spinlock
(g_lock). On single-core QEMU/emulator builds (e.g. goldfish-miwear
with CONFIG_SMP_NCPUS=1 + CONFIG_MM_KASAN_GENERIC=y) this serializes
each op through one extra spin + atomic RMW. A live GDB snapshot on
goldfish-miwear measured ~10K ops/sec, putting the full spinlock_test
well past any sensible test-runner timeout and causing ostest to
miss the "Exiting with status" pattern.
Add a MM_KASAN-conditional default of 5 (= 500K ops, ~150s total for
the three lock types on the same board) so instrumented builds
finish in time, while non-KASAN builds keep the original 100 scale
and retain meaningful throughput numbers. Users can still override
via menuconfig/defconfig when needed.
Root cause: KASAN per-op overhead made the default LOOP_TIMES
infeasible on instrumented QEMU/emulator builds.
Fix: add `default 5 if MM_KASAN` to CONFIG_TEST_LOOP_SCALE and
document the rationale in ---help---.

Assisted-By: mimo-v2.6-pro
Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: vela-autotest04 <vela-autotest04@xiaomi.com>
Remove aligned_data(64) from stack-local union since spinlock_t/
rspinlock_t/seqcount_t are all <= 8 bytes (power-of-2), naturally
fitting within a single cache line without explicit alignment.
Warning: ctc W597: alignment of automatic object limited to 4
File: apps/testing/ostest/spinlock.c
Line: 365

Assisted-By: mimo-v2.6-pro
Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>
@Fix-Point Fix-Point changed the title testing/ostest: Sync the spinlock test with vela fixes testing/ostest: spinlock test fixes Oct 29, 2026

@cederom cederom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Fix-Point :-)

@casaroli casaroli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. testing/ostest/spinlock.c:253: The test needs no priority option: the final counters are right at any priority, and run_test_thread() already reads the priority of the caller with sched_getparam() (line 245) and then overwrites it. ostest runs at SCHED_PRIORITY_DEFAULT, so threads at that priority stay below a watchdog task.

Fix: keep the priority that sched_getparam() returns (here and at line 440), and remove CONFIG_TESTING_OSTEST_SPINLOCK_PRIORITY.


  1. testing/ostest/spinlock.c:226: getpid() gives the process ID, which is the main ostest thread, so sched_setaffinity() still pins the main thread, also when spinlock_test() runs the test in a new pthread. The affinity side effect stays.

Fix: use sched_setaffinity(0, ...), which changes the calling thread.


  1. testing/ostest/Kconfig:162: The help says "scale * 100000", but LOOP_TIMES is now CONFIG_TEST_LOOP_SCALE * 10000 (and the prompt says N x 10,000).

Fix: write "scale * 10000".


  1. testing/ostest/spinlock.c:343: rspinlock_t is the recursive spinlock, not a read-write spinlock. The comment at line 437 says "default - 1", but the code sets the Kconfig value.

Fix: correct both comments.

@casaroli

Copy link
Copy Markdown
Contributor

Commits 15513a3 ("fix spinlock test loop count to match Kconfig description") and deb5eff ("lower spinlock loop scale under MM_KASAN") are signed off by vela-autotest03 <vela-autotest03@xiaomi.com> and vela-autotest04 <vela-autotest04@xiaomi.com>. These look like automation accounts, and CONTRIBUTING.md 1.5 says: "AI agents MUST NOT add Signed-off-by tags. Only humans can legally certify the commit."

Fix: have the human author of each commit sign it off with their own name and address.

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.

5 participants