8000 A UML guest caches the store twice and never gives the memory back · Issue #505 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

A UML guest caches the store twice and never gives the memory back #505

Open

Description

@Lillecarl

The UML half of #376, with the measurement that issue asks for.

Guest RAM is a file. UML's "physical" memory is an unlinked file it mmaps MAP_SHARED. It is sparse, so a large mem= costs nothing until the guest touches it -- and it only ever grows, because nothing punches holes in it.

Measured, one guest at mem=1024M reading 563 MiB of store paths over hostfs:

guest Cached 79 MB -> 658 MB
host blocks allocated to that guest's RAM file 717 MiB
host page cache of the same three files, by mincore 563 of 563 MiB

The host pays about 1126 MiB to read 563 MiB once: its own page cache, and the guest's copy inside the guest's RAM file. Neither half comes back while the guest runs.

The second copy buys less here than on KVM. A guest page-cache miss on hostfs is a host read() that hits the host's page cache -- a memcpy, not disk I/O. Same guest, same file, host cache hot:

  • guest cache miss: 1.3-1.5 GB/s
  • guest cache hit: 5.1-6.5 GB/s

So a smaller mem= costs a 4x slower re-read at memory speed, not a disk seek. That is a much cheaper trade than #376's QEMU case, and it makes right-sizing mem= a real lever on its own.

Nothing removes the copy. hostfs is generic_file_read_iter plus read_folio: no DAX, no O_DIRECT, no mapping of the host's own pages. ubd's flags are r s d c t, where s is O_SYNC; there is no O_DIRECT there either. Removing the copy means teaching hostfs to map the host's pages, which is upstream work.

Giving the memory back is possible today, in one of two shapes.

  1. mconsole's mem device. config mem=-256M allocates guest pages and MADV_REMOVEs them from the RAM file -- UML's own balloon. Needs CONFIG_MCONSOLE (allnoconfig leaves it off), uml_dir= and umid=, and a client, which is struct.pack on a datagram socket. Two limits: mem_config allocates GFP_ATOMIC and stops at the first failure, so it only takes pages that are already free -- drop the guest's caches first -- and mem=+N cannot go above the boot mem=.
  2. A page_reporting consumer. CONFIG_PAGE_REPORTING is generic mm. A UML driver that registers a report callback calling os_drop_memory shrinks continuously with no host orchestration, and growing back is free because the file is sparse. A kernel patch carried in pkgs/uml-kernel.

virtio-balloon does not apply: virtio_uml is a vhost-user transport, and there is no balloon backend to speak to.

Fixed separately while measuring this: every sandboxed guest's RAM file landed on the builder's disk, because Nix sets TMPDIR=/build and UML takes it with only a warning.


Assisted-By: Opus 5

Activity

  1. Lillecarl commented on Sep 21, 2026

    @Lillecarl
    ContributorAuthor

    The mconsole half is landed: user-mode-nixos 04d7d96b, locked by nixidae 61e4641a.

    CONFIG_MCONSOLE=y, uml_dir=/umid= on the kernel line, a struct.pack client in uml_runner, and Machine.drop_caches / shrink / grow / meminfo / host_memory_kib. tests/memory.py is the check, and it is in checks.

    Measured by that test, one guest at mem=1024M reading its own 362 MiB closure:

    guest Cached host pays
    after the read 441M 553M
    after drop_caches 40M 553M
    after shrink("256M") 40M 300M

    Still open here: the page_reporting driver, which is what makes this automatic instead of a knob a test has to call. Filed separately.


    Assisted-By: Opus 5

  2. Lillecarl commented on Sep 21, 2026

    @Lillecarl
    ContributorAuthor

    Both runners now, and both mechanisms. user-mode-nixos 48bef443, locked by nixidae 0125c92c.

    memory.py runs on both backends and cannot tell which it is on. One guest at memory = "1024M" reading its own 362 MiB closure and dropping its cache:

    UML QEMU
    at boot 149M 349M
    after the read 553M 782M
    seconds later, unasked 148M 404M

    Answering the original question directly:

    • The double copy is real and cannot be removed today. 563 MiB read over hostfs cost the host 1126 MiB — its own page cache plus the guest's, measured with mincore on one side and the guest's RAM file on the other. hostfs is generic_file_read_iter + read_folio: no DAX, no O_DIRECT, no mapping of the host's pages. Removing it means teaching hostfs to map them, which is upstream work nobody has started.
    • It matters less here than on KVM. A guest cache miss on hostfs is a host read() that hits the host's own cache — 1.3-1.5 GB/s against 5.1-6.5 GB/s for a hit. A memcpy, not a seek. So a smaller memory is a cheap lever under UML in a way it is not under QEMU.
    • Reclaiming it is now automatic. Free page reporting on both backends, at order 5 on both: madvise(MADV_REMOVE) under UML (pynixd: detect the builder-rpc-v0 feature so a remote store can present it #13, a patch in pkgs/uml-kernel), virtio-balloon-pci,free-page-reporting=on under QEMU (Move to one repository with josh exports, the way pyterm did #4).
    • Growing and shrinking on demand works too, through UML's management console and QEMU's monitor, behind vm.shrink() / vm.grow().

    Nothing left open here. #506 and #376 carry the two halves.


    Assisted-By: Opus 5

  3. added
    source: vivarium
    on Oct 19, 2026
  4. transferred this issue fromnixidae/vivariumon Oct 19, 2026
  5. added theissue type on Oct 19, 2026
  6. Lillecarl commented on Nov 3, 2026

    @Lillecarl
    ContributorAuthor

    Why the krun answer, a DAX window, does not reach UML today. Read in linux-7.2.9's Kconfig against the umlKernel config (2026-11-03):

    • FUSE_DAX (virtio-fs DAX, what krun uses) depends on VIRTIO_FS, FS_DAX and DAX (fs/fuse/Kconfig:43).
    • FS_DAX depends on ZONE_DEVICE (fs/Kconfig:58), and ZONE_DEVICE depends on MEMORY_HOTPLUG, MEMORY_HOTREMOVE and SPARSEMEM_VMEMMAP (mm/Kconfig:1236).
    • arch/um/Kconfig selects neither ARCH_SPARSEMEM_ENABLE nor ARCH_ENABLE_MEMORY_HOTPLUG. So UML is FLATMEM, and none of those options appear in umlKernel's config at all.
    • The transport exists: VIRTIO_UML drives virtio devices over vhost-user (arch/um/drivers/Kconfig:142), so virtio-fs with virtiofsd would load. It would not get DAX.

    So DAX on UML first needs SPARSEMEM_VMEMMAP and memory hotplug in arch/um. That is arch work, not a config change. The other route is the one this issue names: hostfs that maps the host's page-cache pages on a fault. UML's MMU is built on host mmap, so that may cost less than porting ZONE_DEVICE. Neither is measured.

    For reference, the krun numbers this would aim at: 3-node k8s on dynhetz, peak host PSS of the guest processes 2250 MiB on krun against 3347 MiB on QEMU.


    Assisted-By: Claude Opus 5.5

  7. Lillecarl commented on Nov 17, 2026

    @Lillecarl
    ContributorAuthor

    User wants this for UML when the time is right (2026-11-17): something that works like krun's virtio-fs DAX window, so a UML guest reads the store from the host's page cache and keeps no copy of its own.

    The two routes in the comment above still stand:

    1. A hostfs that maps the host's page-cache pages on a fault. UML's MMU already maps host files, so this may be the smaller change.
    2. SPARSEMEM_VMEMMAP and memory hotplug in arch/um, then FUSE_DAX over virtio_uml with virtiofsd.

    Measure route 1 first.


    Assisted-By: Claude Opus 5.5

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

    source: vivarium

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0