mica: ublk zero copy and final performance

Progress report. Updated 2026-10-31. Final tests: a cloud VM (2 vCPUs, 8 GiB, ext4, Linux 7.0). Zero-copy A/B: one laptop (16 CPUs, NVMe, btrfs). The commits are on main.

Final performance

All tests on the server passed. 36 crash runs and two 16-disk soak tests found 0 bad blocks. These tests found and fixed two launch blockers: S3 overload with many disks, and a page that a GC could delete before a power cut.
Rewrite inside the cache budget, 4 KiB, no sync
21.4k writes/s
p50 40 µs
Same, with fdatasync
2.1k writes/s
p50 450 µs
Sync latency, 16 busy disks
p50 1.7–4.3 ms
p99 11–86 ms
Daemon memory, 16 busy disks
40–72 MiB
under 1 MiB per idle disk

Test setup

The server is one VM in a Hetzner location. There are two S3 stores: RustFS in Docker on the same VM, and Hetzner Object Storage in the same location. A round trip to Hetzner Object Storage is 0.7 ms. The final binary ran the last 12 crash runs. The 24 crash runs and the soak tests before them used a build that differs only in its restart takeover, and that takeover went back to the code the earlier crash tests covered.

Writes to saved chunks

A saved chunk is a chunk that S3 has. Each saved chunk used to leave the local data files after each checkpoint, so its next write had to copy it back or download it. Now saved chunks stay local up to the disk's cache budget. The rewrite rows are 4 KiB random writes for 15 s, or 4 s for the rows inside the budget, with RustFS on the VM.

TestBeforeAfter
Rewrite 2 GiB, budget 1 GiB, no sync160–260 writes/s, p50 2.5–3.5 ms286–362 writes/s, p50 55–61 µs
Rewrite 2 GiB, budget 1 GiB, fdatasync138–149 writes/s, p50 4.8 ms290–361 writes/s, p50 1.1–1.6 ms
Rewrite inside the budget, no syncnot measured21,441 writes/s, p50 40 µs
Rewrite inside the budget, fdatasyncnot measured2,091 writes/s, p50 450 µs
Checkpoint of 2 GiB109–113 MiB/s132–143 MiB/s
Detach on ext4, 2 GiB written2–25 s1.0 s

Cost: a disk can keep its cache budget twice, in the data files and in the cache. The "before" detach times include builds between the two fixes.

S3 stores

RequestHetzner, same locationRustFS on the VM
GET 4 KiB, one at a time, p50127 ms1.5 ms
GET 256 KiB, one at a time, p50103 ms1.9 ms
PUT 1 MiB, 16 at a time44 MiB/s149 MiB/s
GET 1 MiB, 16 at a time54 MiB/s468 MiB/s
Slowest GET in the test150 sunder 1 s

Local reads and writes do not wait for S3. A cold read does: about 100 ms or more for each miss on Hetzner.

First checkpoint of 512 MiBUpload rate by the number of uploads at a time. More is better. Hetzner is slower at 8 and 16, so the limit stays 64.
HetznerRustFS on the VM

Many disks

Each disk could upload 64 chunks at a time, with no limit for the node. With 32 busy disks, about 2000 PUTs overloaded RustFS on the laptop. Now the node has at most 64 chunk uploads and 128 chunk downloads, of which 32 for read-ahead and the fill.

32 busy disks on the laptopNo node limitNode limit
Small S3 requestsover 20 s, failingno failures in 6 rounds
GCfailedpassed
Detachnot finished after minutes33 disks in 26 s
Daemon memory while writing558 MiB94–122 MiB

Sync latency in that laptop test was high (p99 about 2–4 s). Plain files on the same btrfs show the same under 32 sync writers, without mica: p99 2.2 s. So the filesystem causes it, not mica. The server does not show it.

Soak tests on the server

16 disks with a writer on each, 4 rounds of 90 s. Each round runs a GC during the writes, detaches and attaches half the disks, and checks every block. One round restarts the daemon during the writes.

ResultRustFS on the VMHetzner
Disks with 0 bad blocks, each round16 of 1616 of 16
Errors in the daemon log00
Probe, 4 KiB write + fdatasync, p501.7–4.3 ms1.8–4.0 ms
Same, p9913–86 ms11–19 ms
GC48–80 s31–44 s
Daemon restart command during writes30 s22 s

With writers at full speed on 16 disks, a separate test measured the longest wait of each write in a restart: at most 3.7 s. The restart command took 60 s there: the last checkpoints are slow and reach their limit. Guests go on during that time, and data not yet uploaded stays on the SSD.

Crash safety on the server

TestRunsLost data
Power cut: ext4, XFS, btrfs180
Daemon kill during writes, ext460
Final binary: power cut, ext460
Final binary: daemon kill, ext460

A checkpoint now repairs objects that a GC deleted before it writes the head. Before, a power cut between the head and the repair could leave a head that named a deleted page. A unit test covers this order. No crash run forced this exact timing.

Tried and removed

Parallel takeover of devices at restart showed no gain: serial takeover also takes under 1 s for 16 disks. It went back to the simpler code.

Not tested yet

Zero copy summary

Done. Zero copy is committed on main: memory −96 %, 1 MiB reads 2.3×, 1 MiB writes −12 % CPU. Cold reads, FLUSH and queue-depth-1 latency did not change. 32 of 32 crash runs without data loss. The iowait fix is merged on main too. A cleanup refactor is on the branch refactor, not merged.
Daemon memory, 8 disks, while reading
732 → 32 MiB
−96 %
1 MiB local reads, 8 disks
7.9 → 17.9 GiB/s
2.3× at the same daemon CPU
Daemon CPU per 1 MiB read
1440 → 656 µs
−54 %

Context

mica serves a disk to Linux through ublk. ublk is a kernel driver that sends each block request (read, write, flush) to a program in user space. Here, that program is the mica daemon.

Before this change, each request tag had its own 512 KiB buffer in the daemon. A tag is one slot for a request in flight. One disk has 4 queues of 64 tags, so a disk can use up to 128 MiB of buffers. The kernel copies each request's data into this buffer or out of it. With many disks on one host, this memory is the main cost. The copy also costs CPU.

The change

The device now uses two ublk features:

PathBeforeNow
Read of a local chunkfile → daemon buffer → requestfile → request (ReadFixed)
Read of a part never writtenmemset in the buffer, then copy/dev/zero → request (ReadFixed)
Write into a local chunkrequest → daemon buffer → filerequest → file (WriteFixed)
Read through S3 or the cacheVec → buffer → request (2 copies)Vec → request through /dev/ublkcN (1 copy)
FUA write, or chunk not localrequest → buffer → Vec (2 copies)request → Vec through /dev/ublkcN (1 copy)

If the kernel cannot register the pages (for example, when memory is low), it still sends the request with the flag UBLK_IO_F_NEED_REG_BUF. mica then serves the request through the copy path, and logs a count.

Kernel probe

A small test program made a device with both flags. It checked five operations: ReadFixed from a file (two parts at different buffer offsets), ReadFixed from /dev/zero, WriteFixed to a file, and copies in both directions through /dev/ublkcN. All five returned the correct data on the lab kernel (Fedora, Linux 7.0.12) and on a second server (Ubuntu, Linux 7.0.0). Kernels before 6.16 do not have UBLK_F_AUTO_BUF_REG, so they cannot run this build.

Correctness

A wrong buffer offset would corrupt data without an error. A new lab tool, datacheck.py, checks this. It sends random reads and writes of 4 KiB to 1 MiB at any 4 KiB offset, so requests cross chunk borders and touch parts that were never written. It also sends FUA writes, FLUSH, discard and write-zeroes. It compares every read with a model of the disk content. The disk has a 32 MiB cache and a 64 MiB pending-write budget, so many requests take the slow paths. After a detach and a reattach, a full read compares the whole disk again, and those reads come from S3.

Crash tests

The crash test writes self-describing 4 KiB blocks to a 1 GiB disk, with FLUSH between some writes, and then breaks the node at a random time (2 to 20 s):

TestFilesystem of the data folderRunsRuns that lost data
Power cutbtrfs60
Power cutXFS60
Power cutext460
Daemon killbtrfs60

A/B results

Setup. 8 disks at the same time. A Python tool reads and writes each /dev/mica/* device with O_DIRECT: 4 threads per disk for 1 MiB I/O, 8 threads per disk for 4 KiB I/O, 20 s per phase, over the first 256 MiB of each disk. All chunks are local, so this measures the queue thread, not S3. The disk limit of the lab is off. CPU is the daemon's user and system time. The two builds ran in turns, two rounds each, and each run started only when the host's I/O pressure was below 2 %. Numbers are the mean of the two rounds; the table below has each round.

Daemon memory (RSS), 8 disksMiB. Lower is better. "Peak" includes checkpoint staging after the write phases.
v0.2c (copy)zero copy

The buffers were the memory. With the old build, RSS reaches about 700 MiB as soon as the first writes touch the buffers, and stays there. With zero copy, RSS stays near 30 MiB until the write phases start. After those phases, both builds grow by a similar amount: a checkpoint keeps chunk data in memory while it uploads. That part does not come from I/O buffers.

1 MiB reads of local chunks, 8 disksGiB/s. Higher is better.
v0.2c (copy)zero copy
Daemon CPU per operation, zero copy as a percentage of v0.2c100 % = same CPU as the old build. Lower is better.

Large reads gain the most: each 1 MiB read was two copies of 1 MiB, and now it is one (page cache to request). The daemon used the same CPU in both builds, about 11.6 cores, but moved 2.3 times more data. At 4 KiB, the copy is small, so the CPU per request stays the same. 1 MiB writes use 5 % less CPU.

The fill phase uses about 12 % more CPU per write. These writes go to chunks that are not local yet, so they take the slow path: the queue thread copies the request out through /dev/ublkcN, then tokio writes it. The phase is short (2048 writes, about 1 s), so the figure is not precise. This path waits for the engine anyway; I will look at it again if a profile shows the copy.

Local write throughput moved in both directions in this test: 1 MiB writes were 1007 vs 791 MiB/s in round 1 and 633 vs 824 MiB/s in round 2 (old vs new). The SSD and the checkpoint uploads (RustFS writes to the same SSD) set these numbers. So a second test measured writes alone.

Writes alone

Same 8 disks, but checkpoints held off, so nothing uploads during the test. Each build ran 3 rounds, in turns, with two 15 s passes of each load per round: 6 samples for each bar.

1 MiB local writes, 8 disks: daemon CPU per writeµs. Lower is better. Mean of 6 samples. 4 KiB writes are in the table: the same in both builds.
v0.2c (copy)zero copy
Loadv0.2czero copy
1 MiB writes: throughput704 MiB/s (sd 187)699 MiB/s (sd 109)
1 MiB writes: daemon CPU per write1841 µs (sd 109)1626 µs (sd 53), −12 %
4 KiB random writes: throughput46.6k IOPS (sd 7.5k)51.6k IOPS (sd 9.8k)
4 KiB random writes: daemon CPU per write38.4 µs (sd 2.0)38.4 µs (sd 2.9)

No write regression. The SSD sets the write throughput, and both builds reach the same speed. A 1 MiB write costs 12 % less daemon CPU, because the copy into the daemon's buffer is gone. At 4 KiB the copy is small, so the cost is the same.

Latency: cold reads, FLUSH, single requests

These tests wait for S3, the SSD or one request at a time, so the copy is a small part of their time. The plan expected no change. A third build is in the test: zero copy plus the iowait fix described below. All builds ran in turns, and each run started on a quiet host.

Cold sequential reads from S3, 20 ms round tripMiB/s, 1 MiB reads at queue depth 1, 128 MiB. Mean of 12 samples (3 cycles × 4 rounds). Higher is better.
v0.2c (copy)zero copyzero copy, no iowait
Testv0.2czero copyno iowait
Cold sequential, MiB/s (12 samples, mean ± sd)79.2 ± 5.177.9 ± 3.076.6 ± 5.6
Cold random 1 MiB reads, median ms (12 samples)333433.5
Idle 4 KiB write + fdatasync, writes/s (5 runs)9221045833
Idle 4 KiB write + fdatasync, p50720–746 µs721–748 µs718–724 µs
Local 4 KiB random reads, queue depth 1 (round 2)89.8k/s, p99 18 µs91.7k/s, p99 16 µs93.9k/s, p99 18 µs
Local 4 KiB random writes, queue depth 1, no sync (2 rounds)31.9k*, 41.3k/s41.4k, 37.2k/s46.2k, 46.1k/s

* Round 1 of v0.2c ran while a Clippy build used the CPU. The first round of cold reads (4 samples per build) showed zero copy about 9 % slower; 12 samples show no difference.

Zero copy: no change in cold reads, FLUSH or single requests, as the plan expected. The FLUSH throughput moves with rare slow syncs of btrfs (p99 up to 9 ms, max up to 3 s), not with the build: the median is the same.

A lab finding: idle disks look like an I/O stall

The first try of the write test waited forever for a quiet host. With 8 disks attached and no load, /proc/pressure/io showed about 95 %, and vmstat showed 32 blocked tasks, but the SSD did almost no I/O. 32 is 8 disks × 4 queue threads. Each queue thread waits in io_uring_enter while requests are in flight (the ublk fetch commands), and the kernel counts that wait as iowait. Both builds do this. On a production host, each attached disk adds up to 4 blocked tasks, and I/O pressure and iowait look high. The load average does not change, because io_uring waits are interruptible. An earlier version of this page said that it did. io_uring has a flag to stop this (IORING_ENTER_NO_IOWAIT, Linux 6.15), but libublk makes the wait call, and the kernel uses iowait to raise the CPU clock for low-depth I/O. So this needs its own A/B.

Fix, merged (38f770d): the queue thread runs libublk's event loop with its own wait, the same in every way but with IORING_ENTER_NO_IOWAIT. With 8 idle disks: 0 blocked tasks and 0 % iowait (before: 32 and about 95 %). The data check passes. In the A/B above, single requests are the same or faster, and FLUSH p50 is the same. The mean of idle write + fdatasync was lower (833/s against 922/s, lower in 4 of 5 paired runs), from a heavier tail, but the spread between runs is wide. The fix improves host metrics, not speed, and adds a small unsafe wait loop. It is merged because host monitoring must not see a stall that does not exist. The write + fdatasync tail stays on the watch list.

All numbers, per round
Phasev0.2c r1v0.2c r2zero copy r1zero copy r2
RSS after attach (MiB)35352223
Fill, 1 MiB writes to new chunks (MiB/s; µs CPU/op)2595; 2676978; 25392279; 29202197; 2940
1 MiB reads, local (MiB/s; µs CPU/op)7822; 14758411; 140618716; 61717848; 695
4 KiB random reads, local (IOPS; µs CPU/op)961k; 7.2887k; 7.7990k; 7.0864k; 7.7
1 MiB writes, local (MiB/s; µs CPU/op)1007; 1979633; 2014791; 1849824; 1939
4 KiB random writes, local (IOPS; µs CPU/op)57.9k; 38.233.6k; 40.832.6k; 41.954.4k; 37.8
RSS after reads (MiB)7207453232
RSS peak (MiB)8561037327144

Log

  1. Saved the old build as mica-v0.2c for the A/B and for rollback.
  2. Kernel probe: pass on two kernels.
  3. Implemented zero copy in src/ublk_queue.rs (new) and src/queue_io.rs. src/disk.rs and src/local_data.rs did not change.
  4. Unit tests (36) and strict Clippy: pass.
  5. Data check: found and fixed short reads from /dev/zero.
  6. Code review (codex, gpt-6.1-sol): no problems in the Rust change. Four problems in the new bench scripts (errors not reported, a disk that already existed could be deleted). Fixed.
  7. Clean A/B, 2 rounds: memory −96 %, 1 MiB reads 2.3×. Writes need a focused test.
  8. Focused write A/B, 3 rounds: same throughput, 1 MiB writes −12 % CPU. Found that idle queue threads count as iowait.
  9. Crash tests: 24 runs, 0 lost data. Committed zero copy on main (c89ffb7).
  10. Latency A/B (cold reads, FLUSH, queue depth 1) with three builds: no change from zero copy. The iowait fix is not clearly free, so it stays on its branch.
  11. Merged the iowait fix on main (38f770d, docs in 29634e3). Correction: the load average never changed; blocked tasks, I/O pressure and iowait did.
  12. Refactor on the branch refactor: rustfmt for the whole tree; disk.rs (900 lines), local_data.rs (800) and chunk_cache.rs (580) split by job into child modules; a CI workflow (format, strict Clippy, tests, lab script parse). The code does the same thing; codex found two small differences (an error text, a lint), both fixed.

Open

  1. Merge the branch refactor (or not), after a look at the diff.
  2. The rest of the to-do list for ublk: more threads per queue, FUA writes into local chunks on the queue thread. UBLK_F_BATCH_IO is out: libublk rejects it with zero copy.
  3. A Firecracker guest soak on the server.

Code changes

BranchCommitWhat
mainaa97e7dThe zero-copy plan; the lab bucket prefix.
mainc89ffb7Zero copy: src/ublk_queue.rs (new), src/queue_io.rs, src/ublk_device.rs; lab tools load.py, zerocopy.sh, datacheck.py, datacheck.sh; docs.
refactor6 commits, 0ace2be..f595b95rustfmt, three file splits, CI, review fixes. Lab check: data check pass; A/B against zero copy within noise (1 MiB reads 659 vs 688 µs CPU per read, 4 KiB reads 8.4 vs 8.6 µs); crash 8 runs (4 power cut, 4 daemon kill), 0 lost data. Not merged.
main4fcf02d, 1f6f976Cold-read benchmark lab/bench/coldread.sh; the iowait finding in todo.md.
main38f770d, 29634e3Idle queue threads do not count as iowait (see above); docs.
main338ae20Removes compatibility with older builds and data.
main0eac372, d629b84Saved chunks stay local up to the cache budget; a detach copies only what fits; the fill starts again on a new budget.
main9ee5f0aRepairs objects that a GC deleted before the head names them.
maina60dc6b, 4e9d945Chunk transfer limits for the whole node; the soak test.

Sources: ublk uapi header · libublk-rs · LWN: ublk auto buffer registration