Repository navigation
virtio-blk: add direct option to bypass the host page cache - #126
Open
QichangZheng wants to merge 1 commit into
Open
QichangZheng wants to merge 1 commit into
QichangZheng wants to merge 1 commit into
Conversation
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>
QichangZheng
requested review from
jakecorrenti,
slp and
tylerfanelli
as code owners
November 26, 2026 09:11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
krunkit always opens disk images with
direct_iodisabled, so on macOS every block the guestreads 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|falsetovirtio-blk, mapped tokrun_add_disk3'sdirect_io(libkrun passes it to imago, which sets
fcntl(F_NOCACHE)on macOS). It follows the shape of#117 (
sync=):false. With neithersyncnordirectkrunkit still callskrun_add_disk2, soexisting command lines behave exactly as before.
directandsyncare independent.krun_add_disk3needs a sync mode, sodirect=truealone passes
KRUN_SYNC_RELAXED, which is whatkrun_add_disk2uses on macOS; only cachingchanges.
parse_boolean, soon/offwork like foroffloading.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+mincoreon the image file; the guest read/wrote the whole disk withddiflag=direct/oflag=direct,conv=fsync.directdirect=truesync=full,direct=true512-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 -9of krunkit followed by journalrecovery 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=trueon its swap anddata 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_ioon 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.xbuild withthis 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
direct=trueand 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.
implicitly opened backing / data files inherit the image's open options, but that has not
been tested.
krun_block_device_set_direct_io().This PR was drafted with AI assistance (Claude Code).