Skip to content

libkernel: MAP_FIXED over flexible memory keeps its flexible charge, and anonymous mmap is not charged as flexible #7120

Open

Description

@oneandonlydean

Two gaps in flexible-memory accounting, found while reviewing #7112. Main (c9e6c45) behaves the same way, so neither is a regression from that PR, and neither is in the paths it changes.

1. MAP_FIXED over flexible pages keeps their charge. sceKernelMmap is a thin wrapper over mmap_nid_postfix. A MAP_PRIVATE|MAP_ANON|MAP_FIXED map inside the arena without MAP_EXCL (the DoMapAnon / GuestMapFixed placement), or a file map with MAP_FIXED, can replace pages that were mapped as flexible. Neither path touches g_flexibleRanges (DirectMemory/Export.cpp), so the replaced pages stay charged until munmap, and the used figure reads higher than what is actually mapped as flexible. It's the same overstated-used class that #7112 fixes for direct and reserved memory.

2. Anonymous mmap is never charged as flexible. Plain anonymous mmap doesn't add to g_flexibleRanges on main. If anonymous maps draw from the flexible budget on real hardware, the used figure reads lower than a title would see. I haven't confirmed the hardware behaviour, so treat this point as a question.

sceKernelJitMapSharedMemory isn't affected by point 1: DoMapShared calls MapPlaced(*addr, len, PROT_NONE, 0, PS5_PAGE_SIZE) in DirectMemory/DirectMemory.cpp, so the address is only a hint, never MAP_FIXED, and it can't land on a live flexible range.

Why it matters: titles read the flexible figure at boot, so counting too much or too little can change what a title decides to allocate. I don't know of a title that hits either gap yet; this comes from reading the code.

Questions for whoever owns the kernel memory code: should MAP_FIXED over a flexible range release the replaced pages' charge? And should anonymous mmap be recorded in g_flexibleRanges the way the flexible map call is?

Related: #5260 (munmap release of flexible memory), #7112 (release scoping).

Activity

  1. MarcZX10 commented on Oct 17, 2026

    @MarcZX10
    Contributor

    I checked the current PR inventory and source paths. The MAP_FIXED replacement accounting is already implemented by open #6491, and the current overlap report for #6491 shows shared edits with #7112 in the same flexible-range accounting helpers. I would avoid a competing patch there; #6491 needs to be coordinated/rebased with #7112.

    The anonymous mmap point remains a separate question, but the issue says its flexible-budget behavior on retail hardware is unconfirmed. Before changing accounting, could you provide a console/API observation or a title repro showing that anonymous mappings consume the flexible-memory budget? Without that evidence, charging every anonymous map risks changing the budget semantics based only on host mapping behavior.

  2. MarcZX10 commented on Oct 17, 2026

    @MarcZX10
    Contributor

    Correction to my earlier overlap note: after inspecting the full #6491 diff, it fixes fixed replacement through sceKernelMapFlexibleMemory itself; it does not touch mmap_nid_postfix or the successful MAP_FIXED anonymous/file-backed paths described in point 1 here. So #6491 is related interval-accounting work, but it is not the same POSIX mmap fix. My current draft addresses only those successful fixed mmap paths (the unconfirmed anonymous-mmap charging question remains out of scope). It still shares the internal accounting helper/file with open #7112, so I have asked that PR author about sequencing before publishing.

  3. MarcZX10 commented on Oct 17, 2026

    @MarcZX10
    Contributor

    Opened PR #7242 for the confirmed successful MAP_FIXED anonymous/file-backed mmap replacement gap. It releases the replaced flexible-accounting interval only after a successful mapping and adds accounting/content regressions for middle-page replacement. The separate question about charging ordinary anonymous mmap remains unchanged because its retail behavior is unconfirmed.

  4. MarcZX10 commented on Oct 18, 2026

    @MarcZX10
    Contributor

    Update: #7242 was merged at 5a8677066, resolving the confirmed MAP_FIXED replacement-accounting gap in point 1. The separate question in point 2—whether ordinary anonymous mappings consume the flexible-memory budget on retail hardware—still lacks evidence, so I am leaving that behavior unclaimed.

  5. oneandonlydean commented on Oct 20, 2026

    @oneandonlydean
    CollaboratorAuthor

    Gap 1 (MAP_FIXED keeping the old flexible charge) was fixed by #7242 (5a86770). Gap 2, anonymous mmap not being charged as flexible, is still open as far as I can tell. Is that right, or did #7242 or another change cover it? If it's still open I'll keep this issue for gap 2 only.

  6. MarcZX10 commented on Oct 21, 2026

    @MarcZX10
    Contributor

    I checked the current mmap_nid_postfix path on main after #7242. Successful fixed anonymous and file-backed mappings call ReleaseFlexibleMappingRange, so the confirmed replacement-accounting gap is resolved. Ordinary MAP_PRIVATE|MAP_ANON still returns from DoMapAnon without being added to the flexible-range tracker. So gap 2 remains open as a code observation, but its console budget semantics are still unverified; I would keep it as a question and not change accounting until we have a hardware/API measurement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions