Skip to content

virtio-blk: add direct option to bypass the host page cache - #126

Open
QichangZheng wants to merge 1 commit into
libkrun:mainfrom
QichangZheng:virtio-blk-direct
Open

QichangZheng wants to merge 1 commit into
libkrun:mainfrom
QichangZheng:virtio-blk-direct

Conversation

@QichangZheng

Copy link
Copy Markdown

krunkit always opens disk images with direct_io disabled, so on macOS every block the guest
reads or writes also stays in the host's unified buffer cache, on top of the guest's own page
cache. On a memory-constrained host running a large VM, that second copy competes with the VM
for RAM (it is reclaimable, but it pushes the host into compression / eviction earlier).

This adds direct=true|false to virtio-blk, mapped to krun_add_disk3's direct_io
(libkrun passes it to imago, which sets fcntl(F_NOCACHE) on macOS). It follows the shape of
#117 (sync=):

  • Default false. With neither sync nor direct krunkit still calls krun_add_disk2, so
    existing command lines behave exactly as before.
  • direct and sync are independent. krun_add_disk3 needs a sync mode, so direct=true
    alone passes KRUN_SYNC_RELAXED, which is what krun_add_disk2 uses on macOS; only caching
    changes.
  • Parsed with the existing parse_boolean, so on/off work like for offloading.

Testing

On a MacBook Pro (M1 Pro, 16 GB, macOS 27.0.1), krunkit built from main + this patch,
linked against libkrun 1.19 (note: a locally patched build, not stock Homebrew), Ubuntu 24.04
guest with 1 GiB RAM and a 2 GiB raw data disk. Host cache residency of the image was measured
with mmap + mincore on the image file; the guest read/wrote the whole disk with dd
iflag=direct / oflag=direct,conv=fsync.

no direct direct=true sync=full,direct=true
image in host cache after guest cold read 2048 MiB 0 MiB 0 MiB
... after write + 20 s 2048 MiB 0 MiB 0 MiB
cold sequential read 1575 MB/s 453 MB/s 441 MB/s
second read 3368 MB/s (host cache) 472 MB/s 378 MB/s
write + fsync 1257 MB/s 933 MB/s 1043 MB/s

512-byte unaligned direct writes/reads on the direct disk read back correctly; no I/O errors in
the guest. cargo fmt --check, cargo clippy -- -D warnings, and the unit tests (38, one new)
pass.

An equivalent backport on 1.3.2 was also exercised with ext4 + fstrim (image shrinks),
guest swap on a direct disk (checksums match), kill -9 of krunkit followed by journal
recovery and a clean e2fsck, and sparse images (holes read back as zeros).

In production, on an M4 Mac mini (16 GB) running a 10 GiB guest with direct=true on its swap and
data disks (krunkit 1.3.2 with an equivalent backport of this patch; libkrun 1.19 with local
patches, including an equivalent of libkrun/libkrun#1034), the host file cache held by those two images went from 1.33–1.59 GiB
(three measurements before the switch) to 0.0 MiB about 10 hours after it. The host's total
file-backed memory did not drop measurably (2.33–2.45 GiB before, 2.25–2.40 GiB after), presumably
because other files took up the room; the system disk, which stays cached, went from 0.07 to
0.54 GiB over those hours. So the gain is that the guest's disk data is no longer cached twice,
not a smaller host file cache.

Risk: guest memory corruption without a libkrun fix

With direct_io on macOS, guest pages can read back as zeros under host memory pressure
(libkrun/libkrun#1033). We reproduced this once on an unmodified libkrun stable-1.19.x build with
this krunkit patch (1 of 4 20-minute runs on an M1), and more often on our own patched libkrun. The
root cause is unknown. With libkrun copying direct I/O through its own buffer (libkrun/libkrun#1034) it
has not happened again on our build; a run on unmodified libkrun with it is in progress. Until it
or an equivalent lands in the libkrun that krunkit links, it is probably better to keep this option
undocumented or marked experimental; happy to hold this PR until then.

Trade-offs / limitations

  • Large sequential reads are several times slower with direct=true and cost more host CPU,
    since nothing is served from the host cache any more. It is meant for hosts where memory
    matters more than disk throughput, hence off by default.
  • Only tested with raw images. For qcow2, libkrun passes the same option to imago, and
    implicitly opened backing / data files inherit the image's open options, but that has not
    been tested.
  • When krunkit moves to the libkrun 2.0 block-device API, this maps to
    krun_block_device_set_direct_io().

This PR was drafted with AI assistance (Claude Code).

krunkit always opens disk images with direct_io disabled, so on macOS
everything the guest reads or writes also lands in the host's unified
buffer cache, on top of the guest's own page cache. When host memory is
tight this second copy competes with the VM itself.

Expose krun_add_disk3's direct_io as direct=true|false (parsed like
other krunkit booleans, so on/off are accepted too). libkrun hands it
to imago, which sets fcntl(F_NOCACHE) on the image file on macOS.

direct and sync are independent. krun_add_disk3 needs a sync mode, so
when only direct=true is given krunkit passes KRUN_SYNC_RELAXED, the
mode krun_add_disk2 uses on macOS; flush behavior is unchanged. With
neither option krunkit keeps calling krun_add_disk2, so the default is
unchanged.

Assisted-by: Claude Code / claude-opus-5.5
Signed-off-by: Qichang Zheng <qichang.zheng2000@gmail.com>
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