Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/boot-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,40 @@ it, and a swtpm that never answers hangs the machine where the guest's driver us
bounded wait, the command completed in the write when swtpm answers within a millisecond and on
the thread pool otherwise, would keep the gain without the hold (#860).

**A bounded wait in the start write (#860).** *The budget entry, written before the run.* The
phase is the same: what a command costs the guest inside `tpm_transmit` beyond swtpm's work. On
main (0012, 0033), the unbounded start write took −30.4 [−37.3, −22.6] off the first login (lab
46275881507, 60 a side), so that is the ceiling. The START write hands the command to the thread
pool as today and then waits, BQL held, up to 1 ms for the worker to finish `handle_request`. If it
finished, the write completes the command; if not, the write returns and the thread pool's
completion runs on the main loop as before. The BQL is held at most 1 ms a command. A worker the
pool has to create is made by a bottom half on the main loop, which cannot run while the vCPU holds
the BQL, so the first command after 10 s idle waits out the bound and completes as before. It is
decided by the first login against main's QEMU, interleaved, 60 a side (the effect is ~30 ms,
under the ±7-9 ms of 20 a side), with the restored login no worse, and the ledger with the TPM
kprobes shows the short commands do not sleep and the RSA primaries still do. Prior art: Cloud
Hypervisor's CRB runs the command in the write with a 100 ms receive timeout, and a slower answer
fails the guest's TPM for good (cloud-hypervisor/cloud-hypervisor#9053, 2026-10-11, open; its fix
#9054, a worker as QEMU has, closed unmerged). QEMU's backend has used its thread pool since
c4fb8561bcec (2018), whose completion bottom half `tpm_backend_finish_sync` waits on. I found no
bounded or polled completion of a TPM command in qemu-devel, QEMU's GitLab or swtpm's issues
(searched 2026-12-03), and no measurement of either path in a guest besides lever 3's.

*Measured* (bench, spin's shape, the release's image, main's kernel and QEMU of parts run
47231873254 against the same with 0013, parts run 47231867269, shirudo 412dc8f3, 2026-12-03). The
first login −13.3 [−19.9, −6.7], p50/p95 915/952 to 901/928 (lab 47234042415, 60 a side); restored
+1.4 [−0.1, +4.3] evicted and −0.1 [−1.4, +2.8] cached, within noise (lab 47234045520, 20 a side).
The ledger with the TPM kprobes (lab 47234050891, 5 a side): a boot's 326 commands slept in
`tpm_transmit` 23 times instead of 326, and spent 87.5 ms there instead of 133.8, a command 0.20 ms
instead of 0.30 at the median. The ones that still slept were the boot's first, before the thread
pool had a worker, and those swtpm took longer than the bound to answer, 0.6-0.9 ms at the median
and up to 2.2. No command on this login's path took over 20 ms, so the BQL held a millisecond at
most where an RSA primary would have held it for its whole time. The gain is under the −30.4 the
unbounded write measured, which was with shirudo 88f9888 on a first login of 1236 ms at the median;
with 412dc8f3 it is 915, and the bounded write leaves 23 commands a boot sleeping that the
unbounded one did not (inferred: the two were not run side by side). **Carried** (qemu/patches/0013, "What
the machine carries").

## What the machine carries

This table lists every change the tree makes for boot time, with what it earns in spin's shape.
Expand Down
19 changes: 19 additions & 0 deletions docs/experiments/860-qemu-0013-tpm-crb-bounded-start-write.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"decision": "carried",
"order": 39,
"change": "QEMU 0013: tpm-crb's START write waits up to 1 ms, BQL held, for the thread pool's worker, so a command swtpm answers within it completes before the write returns and the guest's first status read finds it done; a slower one, or a TPM that does not answer, completes on the main loop as before (#860)",
"where": "qemu/patches",
"result": "the shirudo machine's first login −13.3 [−19.9, −6.7], p50/p95 915/952 to 901/928 (lab 47234042415, 60 a side); restored +1.4 [−0.1, +4.3] evicted and −0.1 [−1.4, +2.8] cached, within noise (lab 47234045520, 20 a side). The ledger with the TPM kprobes: a boot's 326 commands slept in `tpm_transmit` 23 times instead of 326, 133.8 to 87.5 ms there, a command 0.30 to 0.20 ms at the median; those that still slept were the boot's first, before the pool had a worker, and the ones longer than the bound (lab 47234050891, 5 a side)",
"evidence": "measured, 60 a side",
"source": "labs 47234042415, 47234045520, 47234050891; parts 47231867269, 47231873254",
"issues": [
860
],
"runs": [
47231867269,
47231873254,
47234042415,
47234045520,
47234050891
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
date: "2026-12-03"
order: 10
area: qemu
title: a TPM command completes inside the start write within 1 ms (0013, #860)
issues: [851, 860]
runs: [47231867269, 47231873254, 47234042415, 47234045520, 47234050891]
---
*Why:* running every TPM command inside tpm-crb's START write took −30.4 ms off the first login and was rejected for holding the BQL for swtpm's whole time (#851, lever 3). *Done:* qemu/patches/0013 hands the command to the thread pool as before and waits up to 1 ms, BQL held, for the worker; a command answered within it completes before the write returns, a slower one on the main loop as before (#860). The build holds both halves under qtest: short TPM2_GetRandom commands are done when the write returns, and with swtpm stopped the write returns and the command completes once it runs. *Measured* (bench, spin's shape, the release's image, main's kernel and QEMU of parts run 47231873254 against the same with 0013, parts run 47231867269, shirudo 412dc8f3, 2026-12-03): the first login −13.3 [−19.9, −6.7], p50/p95 915/952 to 901/928 (lab 47234042415, 60 a side); restored within noise, +1.4 [−0.1, +4.3] evicted and −0.1 [−1.4, +2.8] cached (lab 47234045520, 20 a side). The ledger with the TPM kprobes (lab 47234050891, 5 a side): a boot's 326 commands slept in `tpm_transmit` 23 times instead of 326, and spent 87.5 ms there instead of 133.8. The ones that still slept were the boot's first, before the pool had a worker, and those longer than the bound, up to 2.2 ms. No command on this login's path took over 20 ms, so no RSA primary was among them. *Status:* carried.
28 changes: 28 additions & 0 deletions qemu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,34 @@ RUN d=$(mktemp -d) && \
*) echo "ERROR: tpm-crb's command buffer is not a page of RAM at 0xFED41000 that the control area names: qemu/patches/0012 is lost"; echo "$out"; exit 1 ;; \
esac

# qemu/patches/0013 waits up to 1 ms in the guest's START write for the TPM's answer, so the guest's
# first read of START after the write finds a short command done, and no longer, so a TPM that does
# not answer leaves the machine running. qtest stands in for the guest. After a TPM2_Startup, which
# also gives the thread pool its worker, five TPM2_GetRandom are each started and START read at once:
# the write and the read arrive in one read of qtest's input, so without the patch the main loop
# cannot complete the command between them, and with it most are done (one is enough, for a busy
# builder). Then swtpm is stopped: the START write returns, START reads set, and once swtpm runs
# again the command completes on the main loop with its answer in the buffer (#860).
RUN d=$(mktemp -d) && \
{ /opt/swtpm/bin/swtpm socket --tpm2 --tpmstate dir="$d" --ctrl type=unixio,path="$d/ctrl" & } && sp=$! && \
for i in $(seq 500); do [ -S "$d/ctrl" ] && break; sleep 0.01; done; \
rand='write 0xfed41000 12 0x80010000000c0000017b0008\nwritel 0xfed4004c 0x1\nreadl 0xfed4004c\n'; \
out=$({ printf 'writel 0xfed40008 0x1\nwritel 0xfed40040 0x1\nwrite 0xfed41000 12 0x80010000000c000001440000\nwritel 0xfed4004c 0x1\n'; \
sleep 0.5; for i in 1 2 3 4 5; do printf "$rand"; sleep 0.2; done; \
kill -STOP $sp; printf "$rand"; sleep 0.5; kill -CONT $sp; sleep 0.5; \
printf 'readl 0xfed4004c\nread 0xfed41000 10\n'; sleep 0.2; } | \
timeout 10 /opt/qemu-tcg/bin/qemu-system-x86_64 -machine q35 -accel qtest -m 128M -nodefaults \
-display none -L /build/pc-bios -chardev socket,id=tpm,path="$d/ctrl" \
-tpmdev emulator,id=tpm,chardev=tpm -device tpm-crb,tpmdev=tpm -qtest stdio 2>/dev/null | tr '\n' ' '); \
kill -CONT $sp 2>/dev/null; kill $sp 2>/dev/null; wait $sp 2>/dev/null; rm -rf "$d"; \
done=$(echo "$out" | grep -o 'OK OK OK 0x0000000000000000' | wc -l); \
case "$out" in \
*"OK OK OK 0x0000000000000001 OK 0x0000000000000000 OK 0x80010000001400000000 ") ;; \
*) echo "ERROR: with swtpm stopped, the START write did not return, or the command did not complete once swtpm ran: qemu/patches/0013 is lost or holds the BQL until the TPM answers"; echo "$out"; exit 1 ;; \
esac; \
[ "$done" -ge 1 ] || { echo "ERROR: no TPM2_GetRandom of five was done when the write that started it returned: qemu/patches/0013 is lost"; echo "$out"; exit 1; }; \
echo "OK: $done of 5 short TPM commands were done when the write that started them returned, and a TPM that does not answer leaves the write returning"

# passt, the network of a NIC on passt (machine.NIC.Passt): one beside each QEMU whose machine has
# one, which machine.Start runs on a socket pair. Not QEMU's --enable-passt, which has QEMU spawn
# it, nor slirp, which puts a network stack inside the QEMU process every machine runs (both stay
Expand Down
1 change: 1 addition & 0 deletions qemu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ posted upstream.
| 0010 | `-userfaultfd PATH`: the device postcopy makes its userfaultfds from is opened through `qemu_open`, so `/dev/fdset/N` works, opened for each userfaultfd and closed once it is made. `machine.QEMU.Resume` hands a fast load the descriptor it opened with `add-fd` and takes it back with `remove-fd` before it returns, so QEMU holds the device only while that load starts, and a load in full is never handed it (#789). A QEMU run as a user of its own loads fast with `/dev/userfaultfd` root's 0600 and `vm.unprivileged_userfaultfd` 0, and no other process on the host is granted either (spin-stack/spin#1711). Upstream has no way to hand QEMU the device (qemu-devel's 2023 `/dev/userfaultfd` series opens it by path) | correctness: a fast load without granting userfaultfd to the host | the build: postcopy-ram on a held TCG restore handed /dev/null for the device is refused with ENOTTY, which neither the device by path nor the system call answers, and asked again once the set is removed finds none (ENOENT); `boot:fastload`: a QEMU run as nobody loads a 1.7 GiB state fast with the device open to root alone and the sysctl 0 |
| 0011 | A fast load's fault thread and eager thread place pages without allocating: each page is read with `qio_channel_pread`, where `qemu_get_buffer_at` allocated an iovec in `qio_channel_preadv_all_eof`, and a faulting page is placed without adding it to `page_requested`, a GTree, which is for pages asked of a source. musl's malloc has one lock for every thread. io_uring runs a read that missed the host's page cache again from task work on QEMU's main thread, the disks' submitter, at whatever instruction that thread had reached, malloc's included, and the read's copy into a guest page not placed yet waits for the fault thread. The fault thread waited for malloc's lock, the guest's vCPUs for pages nobody placed, and the monitors answered nothing (spin-stack/spin#1747). postcopy-blocktime no longer counts a fast load's faults, as its accounting allocates too | correctness: without it, each of three runs of `boot:fastload-reads` stopped at its first load (labs 46015140066, 46016828489, 46016833115) and spin's suspend and resume loop stopped at 8 of 608 fast loads (kvm.yml 46004238975, 46014308166, 46018501292); with it, 35 of 35 lane loads (labs 46015145265, 46016838150, 46019688290, compat 46019149732) and 299 of 299 loop loads (kvm.yml 46018501292) read to their end. restore-first within noise (lab 46014684857) | `boot:fastload-reads` (compat.yml, five loads): a guest reading its disk through fast loads made long and cold (QEMU's reads through io.max, the disk dropped from the host's cache every 100 ms, query-blockstats meanwhile) reads on, and its state is read to its end every time |
| 0012 | tpm-crb's command buffer is a page of its own at 0xFED41000, which KVM maps, named in the control area and in a second `_CRS` resource of the DSDT; its size stays 0xF80. At 0x80 in the registers' page it had no memory slot, and each 4-byte store of a command was a full exit, 14,002 a boot on the shirudo machine (#851). Its RAM block is renamed `tpm-crb-buffer`, so a migration across the two layouts is refused, and a machine with a TPM refuses a checkpoint from before (`ram tpm-crb-cmd`) | the shirudo machine's cold −350.8 [−377.0, −314.7], 1730 to 1379 ms (lab 46274812245), and −193.9 [−207.9, −181.9] with shirudo 88f9888 (lab 46276747177); its time in `tpm_transmit` 537 to 239 ms a boot (lab 46274837561), `memcpy_toio`'s exits 14,002 to 0 (lab 46274837821); restored within noise (labs 46274837838, 46276747157) | the build: a held TCG machine with the swtpm it built maps a page of RAM at 0xFED41000 and nothing of the buffer at 0x80, and its control area names 0xFED41000; `boot:record`: a machine with a TPM is refused by one without, by `ram tpm-crb-buffer` |
| 0013 | tpm-crb's START write hands the command to the thread pool as before, then waits up to 1 ms, BQL held, for the worker: a command swtpm answers within it completes before the write returns, so the guest's first status read finds it done and the guest does not sleep; a slower one, or a TPM that does not answer, completes on the main loop as before. The thread pool makes a missing worker from the main loop, so the first command after 10 s idle waits out the bound (#860) | the shirudo machine's cold −13.3 [−19.9, −6.7], p50 915 to 901 ms (lab 47234042415, 60 a side); restored within noise (lab 47234045520); a boot's 326 commands slept in `tpm_transmit` 23 times instead of 326, 133.8 to 87.5 ms there (ledger with the TPM kprobes, lab 47234050891) | the build: under qtest, with the swtpm it built, short TPM2_GetRandom commands are done when the write that started them returns, and with swtpm stopped the write returns and the command completes once it runs |

`docs/boot-time.md`, "What the machine carries", says what each earns among everything else the
machine carries. The qemu entry of `versions.yaml` says what a bump runs.
Expand Down
Loading