HLE: keep KernelWrite leases across pending overlapped and scatter reads #3231
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3231
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3231"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@kilvynsilveira-tech on GitHub · 2026-11-26 13:53 UTC · original
kilvynsilveira-tech:overlapped-scatter-kernel-reads→main· +1608 −15 · 2 files · 1 commits · openRefs #723
Coordination task:
NEW-345(#1633)What's wrong
Tracked title GPU memory and physical-page views are marked
PAGE_READONLY(watch::Guards) so CPU writes fault into the vectored handler. Kernel writes into read-only pages do not trigger user-mode exception handlers—they fail withERROR_NOACCESS(998) unless opened ahead of time withvictory_gpu::kernel_will_write.On
main(63bcca4), three gaps remain on the Win32 file-read path incrates/victory-hle/src/modules/files.rs:ReadFileScatterandReadFileExare not wrapped at all: title imports fromapi-ms-win-core-file-*/kernel32.dll/kernelbase.dllgo straight to the host, so scatter/gather pack reads (FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING) or APC reads into guarded 64 KiB physical-page views fail withERROR_NOACCESS.ReadFile(ERROR_IO_PENDING) drops itsKernelWritelease on return (noted on #723): as soon asReadFilereturns0withERROR_IO_PENDING,_writingdrops before the driver's read lands. Any concurrent GPU refresh (Tracked::take) can re-guard the destination pages (PAGE_READONLY) mid-flight—failing the async read—or copy zeros before completion without seeing a dirty mark once the bytes arrive.LastErrorclobbering onKernelWritedrop:Writing::dropcallsVirtualProtectafter the hostReadFile/freadcall returns, which can overwriteGetLastError()(such asERROR_IO_PENDINGorERROR_HANDLE_EOF) before returning to title code.What this change does
Adds
crates/victory-hle/src/modules/files/reads.rsand wires the file, I/O, handle, and wait API-set exports infiles::module():ReadFile,ReadFileEx, andReadFileScatter:ReadFile(lpOverlapped == null) andfreadskip zero-length/null ranges and preserve threadLastErroracrossKernelWritedrop.ReadFileScatterwalks theFILE_SEGMENT_ELEMENTarray (sized byGetSystemInfo().dwPageSize, stopping early on the terminating null element) and coalesces adjacent/overlapping 64 KiB tracked page spans so multi-segment reads acquire minimalKernelWriteleases.ReadFile/ReadFileScatter(ERROR_IO_PENDING) and queuedReadFileExkeep theirKernelWriteleases inPENDING(inserted with a unique monotonic ID before the host call so a fast IOCP worker waking on another core before the submitting thread returns still harvests cleanly).AtomicUsizefast path when no overlappedKernelWriteis active; raw volatile reads ofOVERLAPPED::Internalavoid forming&Overlappedwhile the kernel owns the IRP):GetOverlappedResultandGetOverlappedResultEx: release leases onceOVERLAPPED::Internal != STATUS_PENDING(covering bothTRUEcompletions and terminal errors likeERROR_HANDLE_EOF/ERROR_OPERATION_ABORTED, while keepingERROR_IO_INCOMPLETE/WAIT_TIMEOUTleases open).GetQueuedCompletionStatusandGetQueuedCompletionStatusEx: release leases for dequeuedOVERLAPPEDpackets (including failed packets dequeued withok == 0and*lpOverlapped != null).ReadFileExcompletion routine trampoline: releases theKernelWritelease before invoking the title'sLPOVERLAPPED_COMPLETION_ROUTINE(and leavesOVERLAPPED::hEventuntouched for caller context, per Win32ReadFileExrules).WaitForSingleObject(Ex) andWaitForMultipleObjects(Ex): when a wait succeeds on the completion event (with low-order IOCP-skip bit(uintptr_t)hEvent & ~1stripped) or file handle, harvest completed entries (Internal != STATUS_PENDING) so callers that wait on the event without callingGetOverlappedResultnever leak busy pages.CancelIoandCancelIoEx: release only entries whose IRP has already leftSTATUS_PENDING, keeping leases alive while the storage driver finishes aborting in-flight DMA.OVERLAPPEDreuse andCloseHandle: reusing anOVERLAPPEDafterHasOverlappedIoCompletedpolling releases the prior lease after acquiring the new one;CloseHandleclears matchingwait_handleslots (preventing recycled-handle collisions) and releases remaining leases on closed file handles.Tests
Synthetic Windows tests in
modules::files::reads::tests(no game files used), driving realwatch::GuardsPAGE_READONLYpage protection through a placed shader-visible descriptor heap (GuardRig):io_handle_and_wait_api_sets_resolve_to_hle— verifiesapi-ms-win-core-file-*,api-ms-win-core-io-*,api-ms-win-core-handle-*, andapi-ms-win-core-synch-*bind to HLE.pending_overlapped_read_keeps_guarded_view_open_across_refresh— pending overlappedReadFile(ERROR_IO_PENDING), mid-flight GPU refresh (PAGE_READWRITEretained),GetOverlappedResult(..., 0)(ERROR_IO_INCOMPLETE),GetOverlappedResultExharvest, post-harvest refresh re-guarding toPAGE_READONLY, plus negative control showing raw hostReadFilefails withERROR_NOACCESS(998).read_file_scatter_opens_guarded_segments_and_harvests_on_event_wait— unbuffered overlappedReadFileScatter(FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING) across scattered 4 KiB segments in two guarded views with a low-bit-tagged event (hEvent | 1) harvested viaWaitForSingleObject, plus negative control showing raw hostReadFileScatterfails withERROR_NOACCESS(998).iocp_read_file_ex_cancellation_and_eof_preserve_semantics—GetQueuedCompletionStatus,GetQueuedCompletionStatusEx,ReadFileExalertable APC with non-handle user context inOVERLAPPED::hEvent,CancelIoEx+GetOverlappedResult, andERROR_HANDLE_EOFpreservation.multi_wait_overlapped_reuse_and_close_handle_leave_no_stale_leases—WaitForMultipleObjects,HasOverlappedIoCompletedpolling +OVERLAPPEDreuse on a guarded view, andCloseHandlecleanup.Verified on Windows 10 x64 (19045), Ryzen 7 5700X, Radeon RX 580 2048SP:
cargo test -p victory-hle --locked(single-threaded and parallel): 159 passed, 0 failedcargo test -p victory-gpu --locked watch::: 15 passed, 0 failedcargo clippy --workspace --all-targets --locked -- -D warnings: cleancargo clippy --workspace --all-targets --all-features --locked -- -D warnings: cleancargo run -p xtask --locked -- policy: 192 files okNot tested: a retail title run. AI-assisted (Gemini).
@Dukestar69 on GitHub · 2026-11-26 14:11 UTC · original · approved these changes
Verified against
origin/main(63bcca4):cargo test -p victory-hle --lib modules::files:: -- --test-threads=1: 11/11 passed (all 5 new overlapped/scatterKernelWritelease tests pass).cargo clippy -p victory-hle --all-targets -- -D warnings: clean.cargo xtask policy: 192 files ok.Cleanly keeps
KernelWriteleases alive across pending overlapped and scatter reads and harvests them on completion/cancellation/close.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.