sched: Optimize reader-writer semaphore and achieve MISRA HIS goto compliance#18210
Conversation
This change optimizes the reader-writer semaphore release process by consolidating redundant context switch operations and improving code efficiency, reducing unnecessary wake-ups during concurrent access scenarios. Signed-off-by: Yongrong Wang <wangyongrong@xiaomi.com>
This change replaces goto-based control flow with structured if-else blocks in the up_read() function to comply with MISRA HIS coding standards while maintaining identical functional behavior. Signed-off-by: hujun5 <hujun5@xiaomi.com>
linguini1
left a comment
There was a problem hiding this comment.
Please provide test logs for these cases:
Verified on QEMU ARMv7-A simulator with multimedia profile Reader-writer semaphore operations verified: Single reader access patterns Single writer access patterns Mixed reader-writer contention scenarios Waiter notification behavior verified No unnecessary context switches on optimized paths Static analysis shows improved compliance metricsTesting
Reader-writer semaphore scenarios:
Multiple readers with no writers
Multiple writers (exclusive access)
Mixed reader-writer access patterns
Waiter wake-up correctness
Lock holder tracking
Context switch reduction verification
Performance: Reduced context switch overhead in high-contention scenarios
How did you verify this performance improvement? Can you please share the scenario and the results?
Thank you for the review. This optimization has been validated in Vela OS (Xiaomi's embedded operating system based on NuttX) and is running in production across multiple product lines including wearable devices, IoT devices, and automotive systems. The optimization addresses two inefficiencies: up_read(): Original code checks waiter > 0 instead of reader > 0, potentially calling up_wait() while other readers still hold the lock. Write waiters wake up, find the lock unavailable, and go back to sleep. The fix ensures up_wait() is only called when the lock is actually available for acquisition - when writer reaches 0 or when the last reader releases. This has been running in Vela OS production for several months on ARM Cortex-A SMP platforms. We observed: Reduced scheduler overhead in recursive locking scenarios (VFS layer, graphics subsystem) Correctness |
Do you have any profiling information you can share that shows the improvement from a before/after comparison?
That's great, but if you're going to make these claims you're going to have to prove them with logs/measurements in the PR. Otherwise they don't mean anything. You say that the optimization "passes all existing tests". What are those, did you test all of them, and can you share the results? At least 10 of the PRs submitted in the last few days from Xiaomi have had completely AI generated descriptions with false information, so I do not trust any claims without proof. |
Ok, I have conducted comprehensive performance tests on QEMU ARMV8-A to verify this optimization scheme. The detailed results are as follows:
The same logic applies to |
|
@wyr-7 can you share the test cases you used? |
Yes, test cases are available at apache/nuttx-apps#3397 |
Summary
This PR contains two related improvements to the reader-writer semaphore
implementation:
up_read()andup_write()APIs to reduce unnecessary context switchesup_read()to eliminate goto statements and achieveMISRA HIS compliance
Motivation and Problem
Part 1: Performance Optimization
The original implementation had redundant context switch operations in the
reader-writer semaphore release paths. The
up_wait()function was being calledmultiple times in different code paths, creating unnecessary wake-ups and context
switches even when no waiters were present, impacting system performance.
Part 2: MISRA HIS Compliance
The
up_read()function used goto-based control flow which violates MISRA HIScoding standards for safety-critical systems. Structured if-else blocks improve
code clarity, verifiability, and compliance with automotive safety standards.
TEST