hujun260 opened a new pull request, #17986:
URL: https://github.com/apache/nuttx/pull/17986

   ## Summary
   
   Fix a critical issue where addrenv_switch() may change the current running 
task (this_task), leading to exceptions or context corruption. After switching 
address environments, deferred work execution or context changes can cause the 
active TCB to change. This patch updates all relevant code paths to re-fetch 
tcb = this_task() after addrenv_switch(), ensuring subsequent scheduler and 
context operations use the correct TCB.
   
   ## Changes
   
   - **All architectures (38 files)**: After addrenv_switch(tcb), immediately 
re-fetch tcb = this_task() to ensure TCB pointer reflects current running task
   - **Affected paths**: doirq, syscall, svcall, exit, switchcontext handlers 
across ARM, ARM64, AVR, HC, MIPS, MISOC, OR1K, Renesas, RISC-V, SPARC, TriCore, 
x86, x86_64, Xtensa, Z80
   - **Code reordering**: Move scheduler parameter updates (g_running_tasks[]), 
context restores, and time slice adjustments to execute after TCB refresh
   - **Cleanup**: Remove now-incorrect assumptions that TCB remains unchanged 
after addrenv_switch
   
   ## Benefits & Technical Details
   
   - **Correctness**: Ensures all context and scheduler operations use the 
actual current TCB after address environment changes
   - **Stability**: Prevents rare but critical exceptions and context 
corruption when addrenv_switch triggers deferred work or task switches
   - **Portability**: Applies the fix consistently across all supported 
architectures and context transition paths
   - **SMP safety**: Critical for SMP systems where addrenv changes may 
interact with other CPUs' scheduling
   - **Robustness**: Makes code resilient to future addrenv_switch or deferred 
work handling improvements
   
   ## Testing
   
   - Verified context switching and interrupt handling on all supported 
architectures
   - Confirmed no exceptions or context corruption after address environment 
changes
   - Tested task exit, fork, vfork, and signal delivery paths with correct TCB 
usage
   - Validated SMP and uniprocessor builds
   - Confirmed no regressions in scheduler, context switch, or time slice logic
   
   ## Impact
   
   - **Correctness**: Fixes a subtle but critical bug affecting all 
architectures with address environment support
   - **Compatibility**: No API changes, fully backward compatible
   - **Scope**: Affects all context switch, interrupt, and syscall paths using 
addrenv_switch
   - **Stability**: Greatly improves system robustness in complex memory and 
SMP scenarios


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to