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

   depends-on: apache/nuttx/pull/21778
   
   ## Summary
   
   `addrenv_switch()` keeps a reference to the address environment of the last 
process that ran on each CPU (`g_addrenv[cpu]`). It drops the reference only 
when the CPU switches to another process: for a kernel thread or the idle task 
it returns at once. So after the end of a process, a CPU that then runs only 
kernel threads or idles keeps the address environment and all the memory of the 
process. With SMP, each CPU that ran a thread of the process last can keep it, 
and a later process runs out of memory.
   
   - arch: Add `up_addrenv_select_kernel()` (new `ARCH_HAVE_ADDRENV_KERNEL`). 
It selects the kernel page tables alone on the calling CPU and invalidates its 
TLB. arm64 with the MMU and RISC-V write `g_kernel_pgt_pbase` to `TTBR0_EL1` or 
`satp`. x86_64 writes the boot PML4 to `CR3`; its label in `intel64_head.S` 
becomes global as `g_pml4_low`, like `g_pdpt_low`.
   - sched/addrenv: When a kernel thread or the idle task runs next, 
`addrenv_switch()` counts the CPUs that have the address environment of this 
CPU. When these CPUs hold all its references, no task owns it: the CPU makes 
the caches coherent, selects the kernel page tables and drops its reference. 
The last drop frees the address environment in the low priority work queue, as 
before. With SMP, `addrenv_drop()` of an address environment that only CPUs 
still have sends an async SMP call to these CPUs, so an idle CPU leaves it at 
once and does not wait for its next context switch.
   
   ## Impact
   
   - Kernel builds on arm64 (MMU), RISC-V and x86_64, with one CPU or more: the 
memory of an ended process comes back when the process ends, also when the CPUs 
that ran it then idle.
   - A switch to a kernel thread or the idle task takes `g_addrenv_lock` and 
reads the entry of each CPU. The end of a process can send one SMP call to the 
idle CPUs that still have its address environment.
   - Other architectures with `ARCH_ADDRENV` (arm, z80, xtensa, arm64 with the 
MPU) do not select `ARCH_HAVE_ADDRENV_KERNEL`: no change. Flat and protected 
builds have no `ARCH_ADDRENV`: no change.
   - No change to the user API or ABI. `Documentation/os/memory/addrenv.rst` 
describes the new function, and the last commit adds the page of the `exitmem` 
test.
   
   ## Testing
   
   Test: `exitmem` (testing/sched/exitmem), in a nuttx-apps PR that follows. 
The parent stays on CPU 0 and forks 8 children, one after the other. Each child 
runs on another CPU, runs a thread on a third CPU, takes 40 % of the free 
memory, writes every page and ends. After each `waitpid()` the free memory 
(`sysconf(_SC_AVPHYS_PAGES)`) must come back within 3 s.
   
   | | master 7f4629ca01a | this PR |
   |---|---|---|
   | exitmem, qemu-armv8a:knsh with SMP, 4 CPUs (HVF) | FAIL: free 9060 KiB 
before the first child, 5340 KiB after it; the next children get no memory 
(`fork()`: `ENOMEM`) | PASS, 8 rounds |
   | exitmem, rv-virt:ksmp64, 4 CPUs (TCG) | FAIL: free 4924 KiB before the 
first child, 2404 KiB after it; the next `fork()` calls fail with `ENOMEM` | 
PASS, 8 rounds |
   | exitmem, qemu-armv8a:knsh, 1 CPU | PASS | PASS |
   | ostest, qemu-armv8a:knsh with 1 and 4 CPUs, rv-virt:ksmp64 | 1 CPU: status 
0; 4 CPUs: cannot start after exitmem (no memory) | status 0 |
   | ostest, qemu-armv8a:nsh (flat) with 1 and 4 CPUs | status 0 | status 0 |
   
   x86_64 is built, not run.
   
   Builds with `EXTRAFLAGS="-Wno-cpp -Werror"`, master and this PR:
   
   | config | build | result |
   |---|---|---|
   | qemu-armv8a:knsh | kernel, arm64 | OK, no warning; text +192 bytes |
   | rv-virt:knsh64, rv-virt:ksmp64 | kernel, riscv64 | OK, no warning; text 
+138 and +432 bytes (ksmp64: data +56 bytes, the SMP call data) |
   | qemu-intel64:knsh_romfs | kernel, x86_64 | OK; text +352 bytes; `-Werror` 
fails on master and with this PR on the same GCC 16 warning in 
`libm/lib_gamma.c` |
   | qemu-armv7a:knsh | kernel, arm (no `ARCH_HAVE_ADDRENV_KERNEL`) | OK, same 
size |
   | qemu-armv8a:nsh, qemu-armv8a:nsh_smp | flat | OK, same size |
   | rv-virt:pnsh64 | protected | OK, same size |
   
   Compilers: aarch64-none-elf GCC 14.2, riscv-none-elf GCC 14.3, x86_64-elf 
GCC 16.2, arm-none-eabi GCC 15.3. Clang 22 compiles `sched/addrenv/addrenv.c` 
(arm64, riscv64, x86_64), `arm64_addrenv_mmu.c` and `riscv_addrenv.c` with 
`-Werror`. `x86_64_addrenv.c` does not compile with clang on master either 
(`get_cr3()` in `arch/x86_64/include/intel64/irq.h`).
   
   `tools/checkpatch.sh -c -u -m -g` passes.
   


-- 
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