Public bug reported:

### Bug Description

On Ubuntu 26.04, a newly defined KVM guest with UEFI firmware and an
emulated TPM 2.0 often stalls in firmware before its boot manager. The
serial console shows nothing, one vCPU spins, and the guest stays that
way until something makes qemu's main loop run; one was still stalled
after 300 s. Any query to qemu ends it: after `virsh domstats --block
GUEST` the firmware goes on, and GRUB shows about five seconds later. A
guest without a TPM device does not stall.

Versions, on two hosts, both Ubuntu 26.04 on Intel CPUs:

- qemu-system-x86 1:10.2.1+ds-1ubuntu3.3 (also seen with ubuntu3.2)
- ovmf 2025.11-3ubuntu7.3
- swtpm 0.10.1+ds-1ubuntu3
- libvirt 12.0.0-1ubuntu5.6
- kernel 7.0.0-34 on host A, 7.0.0-43 on host B

How often, on host A, each boot a newly defined guest:

| Guest | Stalled |
| --- | --- |
| TPM, Ubuntu's Secure Boot OVMF, left alone | 8 of 8 |
| TPM, Ubuntu's OVMF without Secure Boot, left alone | 6 of 6 |
| TPM, another OVMF build (edk2-ovmf 20241117 from Rocky 9), left alone | 7 of 
7 |
| No TPM, left alone | 0 of 6 |
| TPM, Secure Boot OVMF, `virsh domstats --block` every 5 s | 0 of 8, GRUB at 
15 to 24 s |
| TPM, Secure Boot OVMF, `virsh domstats --block` every 2 s | 0 of 8, GRUB at 9 
to 12 s |

Each boot that was asked took two to four queries before GRUB. One guest
booted eight times in a row stalled on boots 1 and 2 and reached GRUB in
6 to 7 s on boots 3 to 8, with its TPM state kept or fresh.

Host B stalls less often, and its rate varies. In October, with qemu
ubuntu3.2, first boots stalled 7 of 10 with the CD-ROM on SATA, 7 of 10
on virtio-scsi, 6 of 8 with tpm-tis in place of tpm-crb, 3 of 6 without
a NIC, and 0 of 6 without a TPM. On one evening in November 1 of 10
stalled.

So the stall needs neither Secure Boot, a particular firmware build, a
TPM model, a NIC nor a CD-ROM bus. It needs the TPM.

### Steps to reproduce

1. Save the definition below as `tpmtest.xml`, with a UEFI-bootable ISO at the 
path it names. I used the Rocky Linux 9.9 boot ISO; the stall comes before the 
firmware's boot manager.
2. Run `virsh define tpmtest.xml && virsh start tpmtest && virsh console 
tpmtest`.
3. Expected: the firmware's `BdsDxe: loading Boot...` lines, then GRUB, within 
about ten seconds. On a stall the console shows nothing.
4. In another terminal run `virsh domstats --block tpmtest`. The firmware goes 
on within about five seconds. It can take two to four queries.
5. For another first boot run `virsh destroy tpmtest; virsh undefine tpmtest 
--nvram --tpm` and define it again.
6. Remove the `<tpm>` element and the guest boots every time.

```xml
<domain type='kvm'>
  <name>tpmtest</name>
  <memory unit='MiB'>2048</memory>
  <vcpu>2</vcpu>
  <os firmware='efi'>
    <type arch='x86_64' machine='q35'>hvm</type>
    <firmware>
      <feature enabled='yes' name='secure-boot'/>
      <feature enabled='yes' name='enrolled-keys'/>
    </firmware>
    <boot dev='cdrom'/>
  </os>
  <features><acpi/><smm state='on'/></features>
  <cpu mode='host-passthrough'/>
  <devices>
    <controller type='scsi' model='virtio-scsi'/>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/boot.iso'/>
      <target dev='sda' bus='scsi'/>
      <readonly/>
    </disk>
    <interface type='network'>
      <source network='default'/>
      <model type='virtio'/>
    </interface>
    <tpm model='tpm-crb'>
      <backend type='emulator' version='2.0'/>
    </tpm>
    <serial type='pty'><target port='0'/></serial>
    <console type='pty'><target type='serial' port='0'/></console>
  </devices>
</domain>
```

### What a stalled guest is doing

The TPM command is waiting for qemu's main loop, and the main loop is
asleep. On host A a stalled guest's first vCPU runs at 100 %, about
three quarters of it outside the guest, in qemu and the kernel: a
polling loop of I/O exits. Its second vCPU has done nothing. swtpm has
used 0.1 s of CPU in a minute and a half and sits idle. The qemu process
has no `worker` thread.

qemu's TPM backend hands each command to its thread pool
(`backends/tpm/tpm_backend.c`, `thread_pool_submit_aio`). The pool has
its first worker created by a bottom half in the main loop
(`util/thread-pool.c`, `spawn_thread`: "ask the main thread to create
one"). So the firmware polls for an answer that cannot come until the
main loop runs. Reading the guest's registers through the monitor 44 s,
10 s and 45 s into three stalled boots brought GRUB at 49 s, 14 s and 51
s.

### Upstream fixes missing from 26.04

Two upstream fixes for missed wake-ups of the main loop are in v10.2.2
and v11.0.0 and not in v10.2.1. From 10.2.0 qemu's main loop watches its
file descriptors through io\u005c_uring ("aio-posix: integrate fdmon
into glib event loop", ded29e64c6; not in 10.1.0). The fixes went into
master on 2026-02-23:

- [aio-posix: notify main loop when SQEs are 
queued](https://github.com/qemu/qemu/commit/2ae361ef1d7d526b07ff88d854552e2d009bfb1b),
 on stable-10.2 as f9b1537744
- [fdmon-io\u005c_uring: check CQ ring directly in 
gsource\u005c_check](https://github.com/qemu/qemu/commit/961fcc0f22768e7c3432fc645b93dc7cd4932fae),
 on stable-10.2 as a40b3f664c

The first says the fault is "a bit hard to detect, as it also relies on
the ppoll loop not waking up for other activity". A guest in its
firmware's first seconds has no other activity.

In Ubuntu, `qemu` 1:10.2.1+ds-1ubuntu3.3 and `qemu-hwe`
1:10.2.1+ds-1ubuntu4.4 in resolute-updates carry neither change, in the
source or in debian/patches. The uploads in resolute-proposed
(ubuntu3.4, ubuntu4.5) do not name them. stonking's 1:11.0.3+ds-2ubuntu2
has both.

Not tested: I have not run a qemu with these two commits on the affected
hosts. That they cure this stall is a reading of the code and of the
symptom, not a measurement.

### Workaround and request

Asking qemu anything while the firmware starts avoids the stall. `virsh
domstats --block GUEST` every two seconds until the boot manager shows
took new guests on host A from 8 of 8 stalled to 0 of 8. Defining the
guest without a TPM avoids it too.

Please backport the two commits, or move to 10.2.2 or later, in
resolute's `qemu` and `qemu-hwe`. I can test a build with the steps
above.

** Affects: qemu (Ubuntu)
     Importance: Undecided
         Status: New

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2175639

Title:
  qemu 10.2.1 (26.04): new UEFI guests with an emulated TPM stall in
  firmware until qemu's main loop is woken; fixed upstream in 10.2.2

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/2175639/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to