** Description changed: - [Impact] - - In progress - - [Test Plan] - - In progress + [ Impact ] + + On Noble, a VM whose virtio-scsi controller is bound to an IOThread can deadlock + QEMU when the block graph is changed while the guest is doing I/O. The guest + becomes unresponsive, and the only way to recover is to kill QEMU (virsh + destroy). Until then, other libvirt commands on the domain time out with "cannot + acquire state change lock". + + The block graph changes whenever a block node is added, removed or replaced. + Common triggers are external disk-only snapshots (virsh snapshot-create-as + --disk-only), the start, pivot and abort of block jobs (virsh blockcommit, + blockcopy, blockpull), and disk hotplug/unplug. The operation does not have to + target the disk attached to the IOThread. + + An IOThread is opt-in: QEMU runs virtio-scsi in the main loop unless the + device's iothread property is set. However, I/O-intensive guests such as + databases often require virtio-scsi with an IOThread to meet their throughput + and latency SLAs. Disabling the IOThread is therefore not an acceptable + workaround for such deployments. + + The cause is a lock-order inversion between the AioContext lock and the block + graph lock: + + - The main loop holds the BQL and acquires the graph write lock in + bdrv_graph_wrlock(). While still holding the graph write lock, it calls + bdrv_drain_all_end(), which walks the block nodes and acquires each node's + AioContext lock in turn. + - The IOThread holds its AioContext lock while handling a virtio-scsi request. + Its call to blk_is_available() from virtio_scsi_ctx_check() waits for a + coroutine that needs the graph read lock. + + The deadlock occurs when these waits overlap: the main loop cannot acquire the + IOThread's AioContext lock, and the IOThread cannot complete the request check + until the main loop releases the graph write lock, which it only does after + acquiring that AioContext lock. The vCPU threads then block on the BQL, which + the main loop never releases, so the guest freezes. Because this is a race, it + does not happen on every graph change. The IOThread must start checking a + request after the main loop has taken the graph write lock but before it has + acquired the IOThread's AioContext lock. + + Upstream QEMU 9.0 removed the AioContext lock. The drain path no longer acquires + that lock, breaking this lock cycle. This SRU backports the upstream + lock-removal changes [1][2], their prerequisites, and two fixes for regressions + caused by those changes. + + [ Test Plan ] + + The test is run on a Noble VM with nested KVM. The attached script + lp2176154-repro.sh boots a Noble guest with a virtio-scsi controller on an + IOThread. The guest writes continuously to a data disk, and the script + repeatedly takes an external snapshot of that disk and merges it back with an + active blockcommit and pivot. If an operation gets stuck, the script checks a + heartbeat that the guest prints to its serial console. This tells a deadlock + apart from a slow commit. + + ** Setup Test Environment ** + + 1. Install virt-manager on the host: + $ sudo apt install virt-manager virt-viewer qemu-system-x86 \ + libvirt-daemon-system libvirt-clients + + 2. Create a Noble VM with nested KVM: + $ virt-install --name qemu-test --virt-type kvm --memory 4096 \ + --vcpus 4 --cpu host-passthrough --disk size=25 \ + --cdrom /path/to/ubuntu-24.04-live-server-amd64.iso \ + --network network:default --osinfo detect=on,require=off \ + --noautoconsole --graphics spice + Complete the installation from virt-manager. Run the remaining steps inside + this VM. + + 3. Install the packages: + $ sudo apt install -y qemu-system-x86 qemu-utils libvirt-daemon-system \ + cloud-image-utils wget gdb cpu-checker + + 4. Check that KVM is available: + $ kvm-ok + + Expected result: + INFO: /dev/kvm exists + KVM acceleration can be used + + 5. (Temporary) Enable noble-proposed and install qemu and libvirt from it: + $ cat << EOF | sudo tee /etc/apt/sources.list.d/proposed.sources + Types: deb + URIs: http://archive.ubuntu.com/ubuntu + Suites: $(. /etc/os-release && echo $VERSION_CODENAME)-proposed + Components: main restricted universe multiverse + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg + EOF + $ sudo apt update + $ sudo apt install -t noble-proposed qemu-system-x86 qemu-utils \ + libvirt-daemon-system libvirt-daemon libvirt-daemon-driver-qemu \ + libvirt-clients libvirt0 + + The fixed package is based on 1:8.2.2+ds-0ubuntu1.20 (currently in + -proposed), whose qemu-system-common has "Breaks: libvirt-daemon-driver-qemu + (<< 10.0.0-2ubuntu8.20)". Until those packages are in noble-updates, they + have to come from noble-proposed. + + 6. Download the reproducer script attached to this bug and make it executable: + $ wget https://launchpadlibrarian.net/886523519/lp2176154-repro.sh + $ chmod +x lp2176154-repro.sh + + ** Verify the Bug ** + + 7. Run the script with the current qemu package: + $ sudo ./lp2176154-repro.sh + + The script makes 20 attempts by default. The deadlock should reproduce + within those 20 attempts. However, since it is a race, there is a chance + it may not. In that case, run the script again or pass a higher number of + attempts (e.g. sudo ./lp2176154-repro.sh 30). + + Observed result with 1:8.2.2+ds-0ubuntu1.20 and libvirt 10.0.0-2ubuntu8.20 + (both from noble-proposed): + 11:53:15 qemu-system-x86 1:8.2.2+ds-0ubuntu1.20, libvirt-daemon-system 10.0.0-2ubuntu8.20 + 11:53:17 Waiting for the guest to start writing to sdb + 11:53:53 Guest is writing (217655 writes in 10s), starting 20 attempts + 11:53:59 attempt 1: pivoted + 11:56:15 FAIL: attempt 2: blockcommit did not finish and the guest heartbeat stopped (deadlock) + 11:56:16 QEMU thread backtraces: /var/lib/libvirt/images/lp2176154/threads.txt + 11:56:16 Backtraces match LP: #2176154 (main loop blocked on a mutex, IOThread waiting in blk_is_available()) + 11:56:16 The VM is left running for inspection. + + Observed result with 1:8.2.2+ds-0ubuntu1.18 and libvirt 10.0.0-2ubuntu8.19 + (both from noble-updates): the same deadlock on the first attempt. + + Clean up the hung VM using the commands the script prints. + + ** Verify the Fix ** + + 8. Install the fixed qemu package and run the script again with 100 attempts: + $ sudo apt upgrade qemu-system-x86 qemu-utils + $ sudo ./lp2176154-repro.sh 100 + + Observed result with 1:8.2.2+ds-0ubuntu1.20+test455807v20261127b1 and + libvirt 10.0.0-2ubuntu8.20: + 09:07:07 qemu-system-x86 1:8.2.2+ds-0ubuntu1.20+test455807v20261127b1, libvirt-daemon-system 10.0.0-2ubuntu8.20 + 09:07:09 Waiting for the guest to start writing to sdb + 09:07:45 Guest is writing (203896 writes in 10s), starting 100 attempts + 09:07:49 attempt 1: pivoted + 09:07:57 attempt 2: pivoted + ... + 09:13:51 attempt 99: pivoted + 09:13:58 attempt 100: pivoted + 09:14:08 PASS: no deadlock in 100 attempts (100 pivoted, 0 slow commits aborted); guest still writing (248979 writes in 10s) + + ** Regression Testing ** + + 9. Run the upstream iotests (in a virtual machine) to exercise the block layer + and coroutines. Not all of these tests run during the package build. In the + source tree of the fixed package: + $ gbp pq switch + $ sudo apt build-dep ./ + $ sudo apt install seabios ipxe-qemu + $ make -f debian/rules configure-qemu + # Binaries needed for iotests + $ ninja -C b/qemu modules + $ make -f debian/rules build-x86-optionrom + $ cp -a b/optionrom/*.bin /usr/share/seabios/* /usr/lib/ipxe/qemu/* \ + b/qemu/qemu-bundle/usr/share/qemu/ + $ make -C b/qemu/ check-block + $ cd b/qemu/ + $ ./tests/qemu-iotests/check -qcow2 + + Observed result: + <IN PROGRESS> + + 10. Run the Ubuntu Server team's qemu-migration-test [3]. [ Where problems could occur ] - In progress - - [Other Info] - - In progress + This is a large change to core QEMU code: 28 commits that touch 72 files. It + changes the block layer, the block graph lock, block jobs (mirror, commit, + stream, backup), the SCSI and virtio-scsi emulation, virtio-blk, the NBD server, + file-posix, the generated coroutine wrappers, and more. These are shared code + paths. They run for every VM, not only for VMs with IOThreads. + + The AioContext lock used to serialize some state between the main loop and + IOThreads. The series replaces that protection with dedicated locks (for example + NBDClient->lock, the virtio-blk rq lock and the virtio-scsi tmf_bh_lock) or + moves the accesses to a single thread. If a place that still relied on the + AioContext lock was missed, the result would be a race between the main loop and + an IOThread, for example an assertion failure or a hang. + + Regressions of this kind were found after the series was merged upstream. Their + fixes are included in this backport: + + - ad89367202 "block-backend: Allow concurrent context changes" removes an + assertion in blk_get_aio_context() that could fail now that a BlockBackend's + AioContext can change at any time. + - 1604c04931 "scsi: Await request purging" makes SCSI device unrealize (for + example hot-unplug) wait until all requests are purged. + + The areas most worth watching are virtio-scsi and virtio-blk with IOThreads, + SCSI disk hotplug/unplug, block jobs, and NBD exports. + + What reduces the risk: + + - In every patch, the added and removed lines are identical to upstream. + - The same code has shipped upstream since QEMU 9.0.0. In Ubuntu, it was first + included in Oracular (1:9.0.2+ds) and is in all subsequent releases. + - Red Hat shipped the same series, with the same two regression fixes, on QEMU + 8.2.0 in RHEL 9.4. + + [ Other Info ] + + - The lock cycle was confirmed on a hung QEMU process (1:8.2.2+ds-0ubuntu1.18 + with qemu-system-x86-dbgsym installed). gdb shows the main thread in + bdrv_graph_wrlock() -> bdrv_drain_all_end() -> aio_context_acquire() on the + IOThread's AioContext, here during blockdev-add for the snapshot overlay. + The owner of that AioContext lock is the "IO iothread1" thread, which is in + virtio_scsi_ctx_check() -> blk_is_available(), polling for the graph read + lock. The full backtrace is attached to this bug. + + - The same lock cycle was reported upstream in October 2023 during backup and + mirror operations [4]. The thread mentions a first targeted fix attempt that + was "not enough" and hit a further deadlock in bdrv_close() [5]. Kevin Wolf + suggested two workarounds [5][6] but wrote: "if we can't find an easy fix in + the meantime, waiting for [the AioContext lock removal] could be a realistic + option" [5]. Neither workaround was merged or posted as a patch. Upstream + fixed the bug with the lock removal, which was merged on 2023-12-21. A + targeted fix for Noble would therefore be risky, as it would be an Ubuntu-only + locking change that is hard to get right and that has not been reviewed + upstream. + + - The lock removal was not backported to upstream stable-8.2. + + - Commits, in upstream order: + Prerequisites: + efade66d58 nbd/server: avoid per-NBDRequest nbd_client_get/put() + f816310d0c nbd/server: only traverse NBDExport->clients from main + loop thread + 7075d23511 nbd/server: introduce NBDClient->lock to protect fields + cd0c0db0aa block/file-posix: set up Linux AIO and io_uring in the + current thread + 9c67f33fca virtio-blk: add lock to protect s->rq + c1135913c6 virtio-blk: don't lock AioContext in the completion code + path + 6b5aa3a0fd virtio-blk: don't lock AioContext in the submission code + path + "scsi: eliminate AioContext lock" [1]: + eaad0fe260 scsi: only access SCSIDevice->requests from one thread + 765ca51606 virtio-scsi: don't lock AioContext around + virtio_queue_aio_attach_host_notifier() + 1404226804 scsi: don't lock AioContext in I/O code path + e661a24703 dma-helpers: don't lock AioContext in dma_blk_cb() + "aio: remove AioContext lock" [2]: + ed18b1ed4f virtio-scsi: replace AioContext lock with tmf_bh_lock + 10bcb0d996 scsi: assert that callbacks run in the correct AioContext + b3496d129b tests: remove aio_context_acquire() tests + b5f4fda4fb aio: make aio_context_acquire()/aio_context_release() a + no-op + 6bc30f1949 graph-lock: remove AioContext locking + b49f4755c7 block: remove AioContext locking + c43d5bc858 block: remove bdrv_co_lock() + 4f36b13847 scsi: remove AioContext locking + 95bbddf9ad aio-wait: draw equivalence between AIO_WAIT_WHILE() and + AIO_WAIT_WHILE_UNLOCKED() + 9f8d2fdcce aio: remove aio_context_acquire()/aio_context_release() + API + e0444c276a docs: remove AioContext lock from IOThread docs + e7fc3c4a8c scsi: remove outdated AioContext lock comment + e91083cd3f job: remove outdated AioContext locking comments + 23c983c8f6 block: remove outdated AioContext locking comments + Additional upstream commit: + c12887e1b0 block-coroutine-wrapper: use qemu_get_current_aio_context() + (last patch of "block-backend: process I/O in the current + AioContext" [7]; patches 1-4 are already in 8.2.0) + Regression fixes: + ad89367202 block-backend: Allow concurrent context changes + 1604c04931 scsi: Await request purging + + - 25 of the 28 commits cherry-pick cleanly onto 1:8.2.2+ds-0ubuntu1.20. All 28 + packaged patches preserve the added and removed lines of their corresponding + upstream commits. The patches for 7075d23511, 6bc30f1949, and b49f4755c7 have + context adjustments for changes already present in Noble. + + - Red Hat backported the same series to qemu-kvm 8.2.0 for RHEL 9.4 in CentOS + Stream MR 214 [8] as part of RHEL-15965 [9], 26 commits: the 7 prerequisites, + the 18 commits of the two series, and c12887e1b0. Each of the 26 is a straight + cherry-pick, and its diff is identical to upstream. Red Hat added the two + regression fixes, ad89367202 and 1604c04931, in qemu-kvm-8.2.0-5.el9 [10]. This + SRU carries the same 28 commits. + + - This upload is based on 1:8.2.2+ds-0ubuntu1.20, which is currently in + noble-proposed. 1.20 adds "Breaks: libvirt-daemon-driver-qemu + (<< 10.0.0-2ubuntu8.20)" to qemu-system-common, and libvirt 10.0.0-2ubuntu8.20 + is also only in noble-proposed. This fix can therefore only be released after + both of them. + + - A test package was built successfully on all architectures and is available in + the following PPA: + https://launchpad.net/~munirsid/+archive/ubuntu/lp2176154-test + + + [1] https://lore.kernel.org/qemu-devel/[email protected] + [2] https://lore.kernel.org/qemu-devel/[email protected] + [3] https://code.launchpad.net/~ubuntu-server/ubuntu/+source/qemu-migration-test/+git/qemu-migration-test + [4] https://lore.kernel.org/qemu-devel/[email protected] + [5] https://lore.kernel.org/qemu-devel/[email protected] + [6] https://lore.kernel.org/qemu-devel/[email protected] + [7] https://lore.kernel.org/qemu-devel/[email protected] + [8] https://gitlab.com/redhat/centos-stream/src/qemu-kvm/-/merge_requests/214 + [9] https://issues.redhat.com/browse/RHEL-15965 + [10] https://gitlab.com/redhat/centos-stream/src/qemu-kvm/-/blob/qemu-kvm-8.2.0-11.el9_4/.distro/qemu-kvm.spec#L1747-1748
-- You received this bug notification because you are a member of Ubuntu Bugs, which is subscribed to Ubuntu. https://bugs.launchpad.net/bugs/2176154 Title: Deadlock between AioContext lock and block graph lock with virtio-scsi IOThread To manage notifications about this bug go to: https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/2176154/+subscriptions -- ubuntu-bugs mailing list [email protected] https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs
