Skip to content

Fix periodic game stalls by skipping unmappable address space - #622

Open
Vuqar05 wants to merge 1 commit into
willfaust:mainfrom
Vuqar05:fix/va-scan-unmappable-floor
Open

Vuqar05 wants to merge 1 commit into
willfaust:mainfrom
Vuqar05:fix/va-scan-unmappable-floor

Conversation

@Vuqar05

@Vuqar05 Vuqar05 commented Nov 13, 2026

Copy link
Copy Markdown

Summary

free-area scan
  ask TASK_VM_INFO for the address map's lower boundary once
  skip candidate addresses below that boundary
  continue the existing scan above it

Dock's periodic process snapshots exposed an allocator bottleneck: the kernel reports the forbidden range below the app image as an empty hole, so the existing occupied-region skip could not jump over it. Each small mapping could trigger over 66,000 refused mapping attempts while holding virtual_mutex, periodically blocking Terraria despite low GPU time.

Preserve fixed-address attempts, allocation results, diagnostic attempt counts and pressure-latch behavior. If the kernel cannot supply a usable boundary, retain the old scan. Add env.MADEIRA_VA_SCAN_FLOOR=0 as an opt-out and expose it in the settings catalog.

Only the main Madeira repository changes; no Wine or Dock submodule changes or pin updates are included. The separate OpenGL startup workaround used for device testing is not part of this PR.

Evidence

  • Before: The host regression harness reproduces 66,793 mapping attempts with the optimization disabled. Device logs show repeated long scans during Dock polling and frame times of hundreds of milliseconds beside approximately 1.3 ms of GPU time.
    After: The same harness uses 4 mapping attempts with identical allocation results. The rebuilt app was installed on an iPad Air (M2), and the user confirmed Terraria works with the fix.
  • python3 tests/host/check-va-scan-floor.py — PASS. Covers bottom-up and top-down searches, different alignments and windows, explicit fixed-address attempts, opt-out and unavailable kernel boundary.
  • git diff --check origin/main...HEAD — PASS.
  • iOS Debug build and code-signature verification succeeded during device testing; that build also included the separate, uncommitted OpenGL startup workaround noted above.

Merge Danger

Door: two-way

Revert the commit or set env.MADEIRA_VA_SCAN_FLOOR=0 to restore the previous search. No persistent data or format changes.

Blast Radius: allocator

Applies to iOS free-area searches, not only Terraria. The optimization trusts the cached kernel-reported lower address boundary; other games and OS versions have not been exhaustively tested. Dock polling remains enabled and unchanged.

…ery granule below it

iOS extends __PAGEZERO by the ASLR slide, so the address map starts at the
app image (address-map=[0x104e60000,0xfc0000000)) and every fixed mapping
below is refused. Mach reports that range as a free hole, so
ios_skip_occupied had nothing to jump over and a bottom-up scan whose window
starts at 0x10000 tried it one 64 KB granule at a time: 66,845 refused
mach_vm_map calls and as many mach_vm_region queries to place one 4 KB view,
all under virtual_mutex ("[va-scan] SLOW window=0x10000..0x400000000
size=0x1000 ... tries=66845 ... firstfail=0x10000 errno=12").

Madeira Dock takes a process snapshot once a second while a game runs
(sh_hold_poll), and Wine's toolhelp maps and unmaps such a view for every
Process32First/Next: 14 of these scans a second kept Dock's main thread at
~85% of a core while holding the lock every other thread needs for a read,
an ioctl or an allocation. In Terraria (iPad14,8, 2026-11-13) the game's
main thread ran only in the gaps: a few frames, then a stall, about once a
second, with frame times of hundreds of ms beside 1.3 ms of GPU time.
env.MADEIRA_DOCK_KEEP_ALIVE = 0 (no snapshots) made it smooth, which
confirmed the cause.

try_map_free_area now asks the kernel for the map's floor once
(TASK_VM_INFO.min_address) and, bottom-up, resumes at the floor; top-down,
the scan ends there. The granules not tried are still counted as tries and
reported as the first refusal, so the [va-scan] line and the pressure latch
decide exactly as before; the line gains below-floor=N. A fixed-address
attempt is left alone. env.MADEIRA_VA_SCAN_FLOOR = 0 restores the crawl.

tests/host/check-va-scan-floor.py runs the scan on the host against a fake
address map: same address, tries and first refusal with the switch on, off
and with no floor reported; the measured case drops from 66,793 mapping
attempts to 4.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Vuqar05 <vuqarakhmedov05@gmail.com>
@Vuqar05
Vuqar05 force-pushed the fix/va-scan-unmappable-floor branch from cc78825 to 3adceb5 Compare November 13, 2026 19:50
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