Skip to content

kernel: optimize C906 memmove and unaligned usercopy - #484

Merged
dormancygrace merged 6 commits into
mainfrom
codex/c906-scalar-copy
Oct 26, 2026
Merged

dormancygrace merged 6 commits into
mainfrom
codex/c906-scalar-copy

Conversation

@dormancygrace

@dormancygrace dormancygrace commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

Optimize kernel memmove and misaligned usercopy for NanoKVM's SG2002/C906.

memmove uses scalar 64-byte blocks, word copies and bounded short head/tail accesses. Each overlapping block is loaded before storing; the destination return value, early-boot PI aliases and aligned short entry are preserved.

Usercopy replaces shifted-word reconstruction with native misaligned scalar loads. Its normal short and aligned paths retain their exact instructions and addresses. Faults retry the accessible prefix bytewise and return an exact residual while preserving SUM ownership. No CPU dispatch or vector/FP state is needed for this target.

Validation:

  • Full production kernel Image/modules builds and platform, CPU-profile and kernel-configuration checks pass. Zero-fuzz patch application reproduces the tested source.
  • memmove: 68,208 live-kernel cases across two seeds, including overlap, alignment, return values and unmapped page guards; negative control rejected. Documented function timings use 28–55% fewer cycles.
  • Usercopy: 172,352 live-kernel checks, including 86,240 fault cases, all pass; captured-result corruption is detected. Only 40 bytes differ in the complete 7,415,382-byte kernel .text, all inside the original 44-byte unaligned-copy region.
  • Pipe/socket integration: three repetitions per cell in each of three separate processes, compared by medians, with 1 GHz CPU and Ethernet video active. At lengths through 128 bytes, the largest positive CPU-time changes are +0.72% (pipe) and +0.50% (socket). Misaligned pipes use 18.4% less CPU at 4 KiB and 15–16% less at 16–32 KiB; misaligned sockets also improve.

All main measurements are retained, including aligned socket outliers of +9.78% and +8.86% at 2920/3776 bytes. A subsequent baseline/candidate control with eight aligned buffer placements and three processes did not reproduce those slowdowns. Larger socket results vary between runs; no guaranteed aligned-socket, whole-system, browser-ISO or FPS gain is claimed.

Includes live-kernel verifiers, transfer benchmarks and complete process medians: memmove results, usercopy results.

Review clarification: both patches explicitly require native SG2002/C906 misaligned accesses; the 44-byte usercopy region is a qualified placement contract. Production assembly is unchanged by review corrections. The extended guard verifier passes 92,880 checks with the actual RISC-V memmove under a QEMU mmap/guard harness, and detects an injected overread. This does not replace the original live-kernel qualification recorded above.

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Build outputs

Compared with the build of main at 14fde0e.

13 changed, 0 added, 0 removed of 570 outputs.

directory files
changed . Image
changed boot boot/alpha.sha256
boot/beta.sha256
boot/detect.sha256
boot/fit-template.sd
boot/fit.layout
boot/lite.sha256
boot/pcie.sha256
changed boot-fit boot-fit/alpha.sd
boot-fit/beta.sd
boot-fit/detect.sd
boot-fit/lite.sd
boot-fit/pcie.sd
Hashes: outputs-sha256 artifact of this run.

@dormancygrace dormancygrace changed the title kernel: optimize scalar memmove for C906 kernel: optimize C906 memmove and unaligned usercopy Oct 25, 2026
@dormancygrace dormancygrace changed the title kernel: optimize C906 memmove and unaligned usercopy perf: optimize C906 kernel copies and Go Poly1305 Oct 25, 2026
@dormancygrace dormancygrace changed the title perf: optimize C906 kernel copies and Go Poly1305 kernel: optimize C906 memmove and unaligned usercopy Oct 25, 2026

@dormancygrace dormancygrace left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I found no correctness defect in either assembly patch.

What I checked:

  • Both patches apply with zero fuzz to the 7.3 memmove.S and uaccess.S, and both files assemble.
  • I read memmove by hand for every overlap direction and the 0, 1, 2–7, word and 64-byte paths. Each block is loaded before it is stored, and no access leaves [src,src+n) or [dst,dst+n).
  • In usercopy, every faulting access leaves a0/a1 un-advanced, so the bytewise retry returns an exact residual. .Lbulk_fault restores t0, and t5/t6 (SUM and ra) are untouched.
  • The exception-table entry count stays at 23. The new block fits the 44-byte region.

My comments are about the contract with 0045, test coverage and documentation.

Three open PR chains currently claim the same kernel patch numbers: this one (0052/0053), #492 (0052/0053) and #501/#503 (0052/0053). Whichever merges second has to renumber.

Comment thread platform/kernel/0053-riscv-native-scalar-usercopy.patch
Comment thread tests/manual/c906-memory/driver.c Outdated
Comment thread tests/manual/c906-memory/README.md Outdated
Comment thread platform/kernel/0053-riscv-native-scalar-usercopy.patch

@dormancygrace dormancygrace left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Independent review found no runtime defect in the final scalar implementations. Clarified the C906 hardware and usercopy placement contracts, corrected stale usercopy documentation and expanded memmove guard tests in fe2c0da. Production assembly is unchanged apart from comments. Zero-fuzz Linux 7.3 patch application, 92,880 RISC-V QEMU guard/overlap checks, an overread negative control and integrated platform/config/CPU-profile tests pass. Original live-kernel evidence remains attributed to its measured revision. All 15 final-head CI checks, including firmware, passed; head and main were rechecked immediately before merging.

@dormancygrace
dormancygrace merged commit 2571baa into main Oct 26, 2026
15 checks passed
@dormancygrace
dormancygrace deleted the codex/c906-scalar-copy branch October 26, 2026 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant