Skip to content

feat: add host-driven balloon controls#59

Draft
enricoschaaf wants to merge 1 commit into
superradcompany:krunfrom
enricoschaaf:enrico/host-balloon-control
Draft

feat: add host-driven balloon controls#59
enricoschaaf wants to merge 1 commit into
superradcompany:krunfrom
enricoschaaf:enrico/host-balloon-control

Conversation

@enricoschaaf

Copy link
Copy Markdown

Summary

Adds host-driven virtio-balloon control to libkrun and exposes it through both the native Rust API and the C API.

This lets callers configure a VM memory ceiling, boot the guest with a lower guest-visible memory target, and adjust that target at runtime through a host-side Unix socket.

Technical implementation

  • Extends the virtio-balloon device with a cloneable BalloonControl handle backed by an atomic target page count and an eventfd registered in the VMM event loop.
  • Changes Balloon::new() to accept initial and maximum balloon targets in virtio-balloon page units. The initial target seeds num_pages, so the guest sees the desired target during driver initialization.
  • Handles the inflate and deflate virtqueues instead of logging them as unsupported:
    • inflate descriptors are parsed as 4 KiB PFNs and each page is released back to the host with madvise (MADV_FREE_REUSABLE on macOS, MADV_DONTNEED elsewhere);
    • deflate descriptors are acknowledged so the guest can reuse those pages;
    • free-page reporting continues to reclaim descriptor ranges with the same platform-specific advice.
  • Accepts guest writes to the actual config field and signals config-change interrupts when the host target changes, which is the virtio-balloon mechanism the guest driver uses to observe a new num_pages target.
  • Adds VmResources::balloon as a small BalloonResize struct containing initial and maximum reclaimed page counts. build_microvm() passes this into balloon device attachment and stores the BalloonControl on Vmm when runtime control is enabled.
  • Adds VmBuilder::balloon(...) for the Rust API. It validates initial_mib <= memory_mib, min_mib <= memory_mib, and min_mib <= initial_mib, then converts guest-visible MiB values into reclaimed 4 KiB pages relative to the configured memory ceiling.
  • Adds a per-VM Unix stream socket listener. Each accepted connection reads a decimal MiB target, converts it to reclaimed pages relative to the ceiling, updates the BalloonControl, and writes an OK <pages> or ERR ... response.
  • Adds the C API entry point krun_set_balloon(ctx_id, initial_mib, min_mib, control_socket_path) with the same guest-visible MiB semantics and a TEE stub returning EOPNOTSUPP.
  • Propagates blk and net feature flags through src/libkrun/Cargo.toml, matching the existing Rust crate feature propagation, so the C library crate builds with the expected optional device stacks.
  • Bumps the msb_krun* crate versions to 0.1.15 so downstream code can depend on this API distinctly from the currently published crate.

Validation

  • cargo check -p msb_krun --features 'blk net'
  • cargo test -p msb_krun_devices balloon --lib
  • cargo check -p libkrun --features 'blk net'
  • cargo check -p msb_krun_vmm

I did not run a VM smoke test for this PR; this repo does not include a lightweight local smoke harness for exercising a full balloon resize path without a booted guest.

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