[Bug 300949] bhyve NVMe and guest fsync

From: <bugzilla-noreply_at_freebsd.org>
Date: Fri, 20 Nov 2026 19:17:31 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=300949

            Bug ID: 300949
           Summary: bhyve NVMe and guest fsync
           Product: Base System
           Version: Unspecified
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Many People
          Priority: ---
         Component: bhyve
          Assignee: virtualization@FreeBSD.org
          Reporter: pprocacci@gmail.com

bhyve's NVMe controller (`usr.sbin/bhyve/pci_nvme.c`) tells guests that it has
**no volatile write cache

pci_nvme_init_ctrldata()` sets:

cd->vwc = NVMEF(NVME_CTRLR_DATA_VWC_ALL, NVME_CTRLR_DATA_VWC_ALL_NO);

This leaves bit 0 of VWC ("a volatile write cache is present") clear. By the
NVMe specification, a controller without a volatile write cache completes a
write only once it is on non-volatile media, so a guest has no reason to send
Flush, and correct guests don't.

But by default, bhyve's block backend opens the disk **without `O_SYNC`**
(`block_if.c`: `O_SYNC` only with `sync=true` or `direct=true`). It completes
each write as soon as `pwritev()` returns, which is when the data is in the
host's cache: ZFS's ARC for a zvol, the buffer cache for a file. The data
reaches stable storage later, at the host's leisure.

So with the default options:

1. A Linux guest sees `write_cache = write through` on its NVMe disk and sends
no Flush.
2. The guest's `fsync()` / `fdatasync()` / `O_DSYNC` writes return once bhyve's
`pwritev()` returns.
3. A host crash or power loss then loses writes the guest was told were
durable: committed database transactions, journal writes, and so on.

bhyve does implement Flush (`NVME_OPC_FLUSH` → `blockif_flush()` → `DIOCGFLUSH`
or `fsync()`), but because of the VWC bit no guest ever sends it.

bhyve's other disk emulations get this right, so only NVMe is affected.

----

How to reproduce:

Host: FreeBSD 15.1-RELEASE-p5, amd64. The disk is a zvol (`volmode=dev`,
`sync=standard`) on a two-disk HDD mirror with no separate log device, so one
real flush costs about 10 ms. Guest: Debian 13 (Linux 6.12).

bhyve disk argument (no `sync` or `direct`):

-s 2,nvme,/dev/zvol/<pool>/<vol>,ioslots=32

1. In the guest, the controller reports no volatile write cache, and Linux
treats the disk as write-through:

   $ cat /sys/block/nvme0n1/queue/write_cache
   write through
   $ sudo nvme id-ctrl /dev/nvme0 | grep '^vwc'
   vwc       : 0x4

2. In the guest, 200 synchronous 4 KiB writes:

   $ dd if=/dev/zero of=/var/tmp/sync.test bs=4k count=200 oflag=dsync
   819200 bytes (819 kB, 800 KiB) copied, 0.0327507 s, 25.0 MB/s

   That is about 0.16 ms per synchronous write, against about 10 ms for a real
flush on this pool.

3. On the host, at the same time, counting the bhyve process's system calls:

   dtrace -n 'syscall::fsync:entry,syscall::fdatasync:entry /pid == $BHYVE/ {
@[probefunc] = count(); }
              syscall::ioctl:entry /pid == $BHYVE && arg1 == 0x20006487/ {
@["ioctl DIOCGFLUSH"] = count(); }
              syscall::pwrite:entry,syscall::pwritev:entry /pid == $BHYVE/ {
@[probefunc] = count(); }'

   The result: `pwritev 783`, and no `fsync`, `fdatasync` or `DIOCGFLUSH`.


---
# Expected

A guest's `fsync()` returns only once its data is on stable storage, as on AHCI
and virtio-blk. Either:

- the controller reports a volatile write cache, so the guest sends Flush, and
bhyve's existing Flush handling makes the data durable; or
- with no volatile write cache reported, bhyve completes writes only once they
are durable (as with `sync=true`).

---

# Suggested fix

Report a volatile write cache unless the backend is opened `O_SYNC`:

        cd->vwc = NVMEF(NVME_CTRLR_DATA_VWC_ALL, NVME_CTRLR_DATA_VWC_ALL_NO);
        /*
         * Writes complete once they are in the host's cache, unless the
         * backing store is opened O_SYNC: say so, so that guests send Flush
         * (blockif_flush) when they need data on stable storage.
         */
        if (!backend_is_sync)   /* sync=true or direct=true */
                cd->vwc |= NVMEF(NVME_CTRLR_DATA_VWC_PRESENT, 1);

---
# Workaround
Until then, the workaround is to pass `sync=true` (or `direct=true`) on NVMe
disks. That makes every write synchronous, so it costs much more than flushing
only when the guest asks.

---
## Impact

Any guest on a bhyve NVMe disk with the default options can lose acknowledged
writes on a host crash or power loss. That includes databases with synchronous
commit, journaling filesystems, and anything else that relies on `fsync()`.


Now,I'm fully aware about using 'sync=true' but the device's claim is false
under bhyve's own defaults.  Reporting no volatile cache promises that a
completed write is durable, and the default backend doesn't keep that promise.

-- 
You are receiving this mail because:
You are the assignee for the bug.