vmm: vm.remove-device can deadlock with a vCPU that moves the device's BAR

Đang mở
#9,463 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
infrastructure

Hướng nghiên cứu

Start with DeviceManager::remove_device in vmm/src/device_manager.rs, where device_tree is held while the virtio PCI device lock is taken, and compare it with PciConfigIo/PciConfigMmio::config_space_write in pci/src/bus.rs, which lock the device before AddressManager::move_bar locks device_tree. Run the repro.sh move mode to confirm the hang. Done means remove-device returns across the 100-call loop while the guest rewrites BAR 0, and the other Migratable methods on DeviceManager are checked for the same lock order.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

AI use: Claude found the issue and I iterated with it to find a minimal repro. I don't know the code myself so I won't propose a PR—just relaying an issue in the hopes that it is useful.

Describe the bug

vm.remove-device and a guest BAR write take the same two locks in opposite orders, so they can block each other for good. The API then answers nothing and the guest hangs.

  • DeviceManager::remove_device (vmm/src/device_manager.rs) locks device_tree, then locks the virtio PCI device to read its type.
  • PciConfigIo::config_space_write and PciConfigMmio::config_space_write (pci/src/bus.rs) lock the PCI device, then AddressManager::move_bar calls update_device_tree, which locks device_tree.

remove_device is not the only site with that order. Every Migratable method on DeviceManager holds the device_tree guard across migratable.lock(): pause, resume, snapshot, start_dirty_log, stop_dirty_log, dirty_log, notify_started_migration, notify_failed_migration and notify_completed_migration. For a virtio-pci device, node.migratable is an Arc::clone of the same VirtioPciDevice the PCI bus locks, so a vCPU moving its BAR can block those too. I reproduced only remove_device.

So the rule is: do not hold device_tree across a device lock. Releasing the tree first fixes the site I reproduced:

         let pci_device_handle = pci_device_node
             .pci_device_handle
-            .as_ref()
+            .clone()
             .ok_or(DeviceManagerError::MissingPciDevice)?;
-        if let PciDeviceHandle::Virtio(virtio_pci_device) = pci_device_handle {
+        drop(device_tree);
+        if let PciDeviceHandle::Virtio(virtio_pci_device) = &pci_device_handle {

To Reproduce

./repro.sh boots the v55.0 release binary with a kernel and a busybox initramfs, hot-adds a disk, and the guest then rewrites that device's 64-bit BAR 0 in a loop through sysfs config. The host calls remove-device in a loop. It needs curl, cpio and busybox-static, and runs unprivileged given /dev/kvm and a readable kernel: KERNEL=/path/to/vmlinux ./repro.sh. It defaults to the host's own kernel.

aarch64, KVM, 3 runs each:

  • Guest rewriting the BAR: remove-device did not return within 20 s, in 3 of 3 runs (on call 1, 1 and 2).
  • Guest idle (./repro.sh idle): 100 of 100 calls returned.
repro.sh
#!/bin/bash
# vm.remove-device deadlocks while the guest moves the device's BAR.
# Usage: ./repro.sh              (needs curl, cpio, busybox-static)
#        ./repro.sh idle         (control: the guest leaves the BAR alone)
# It boots the host's kernel; KERNEL=/path/to/vmlinux overrides that. Root is
# needed only to read a kernel image that is mode 0600 (Debian, Ubuntu) or to
# open /dev/kvm without being in the kvm group.
set -euo pipefail
mode=${1:-move}
kernel=${KERNEL:-/boot/vmlinuz-$(uname -r)}
[ -r "$kernel" ] || { echo "cannot read $kernel; set KERNEL= or run as root" >&2; exit 2; }
[ -w /dev/kvm ] || { echo "cannot open /dev/kvm; join the kvm group or run as root" >&2; exit 2; }
url=https://github.com/cloud-hypervisor/cloud-hypervisor/releases/download/v55.0
case $(uname -m) in aarch64) sfx=-static-aarch64 con=ttyAMA0 ;; *) sfx=-static con=ttyS0 ;; esac
cd "$(mktemp -d)"
curl -fsSL -o ch "$url/cloud-hypervisor$sfx" -o ch-remote "$url/ch-remote$sfx"
chmod +x ch ch-remote
truncate -s 1M disk
zcat "$kernel" >kernel 2>/dev/null || cp "$kernel" kernel

# Guest: wait for the hot-plugged device (slot 2), then rewrite its 64-bit BAR 0.
mkdir -p root/bin root/proc root/sys
cp "$(command -v busybox)" root/bin/busybox
cat >root/init <<'GUEST'
#!/bin/busybox sh
export PATH=/bin
busybox mount -t proc proc /proc   # busybox --install reads /proc/self/exe
busybox --install -s /bin
mount -t sysfs sys /sys
cfg=/sys/bus/pci/devices/0000:00:02.0/config
until [ -e $cfg ]; do echo 1 >/sys/bus/pci/rescan; sleep 0.2; done
echo READY
grep -q idle /proc/cmdline && exec sleep infinity
# Each pass rewrites BAR 0's high dword (config offset 0x14), 1 then 2, which
# is what makes the VMM move a 64-bit BAR.
while :; do
  printf '\001\000\000\000' | dd of=$cfg bs=4 seek=5 conv=notrunc 2>/dev/null
  printf '\002\000\000\000' | dd of=$cfg bs=4 seek=5 conv=notrunc 2>/dev/null
done
GUEST
chmod +x root/init
(cd root && find . | cpio -o -H newc 2>/dev/null) >initramfs

./ch --api-socket api.sock --kernel kernel --initramfs initramfs --memory size=512M \
  --cmdline "console=$con rdinit=/init $mode" --serial file=serial.log --console off &
trap 'kill -9 $!' EXIT
until [ -S api.sock ]; do sleep 0.2; done
./ch-remote --api-socket api.sock add-disk path=disk,id=hot0,image_type=raw
until grep -q READY serial.log; do sleep 0.2; done

for i in $(seq 100); do
  timeout 20 ./ch-remote --api-socket api.sock remove-device hot0 ||
    { echo "HUNG: remove-device call $i did not return in 20 s"; exit 1; }
done
echo "OK: 100 remove-device calls returned"

Related

Issue #8028 names a neighboring hazard, DeviceManager::pause() against MMIO to DeviceManager (its own BusDevice impl, the ACPI PCI hotplug registers), and asks for a controller split. This report is a different lock pair: device_tree against a PCI device's own mutex, reached through PciConfigIo/PciConfigMmio and AddressManager::move_bar, with no ACPI register involved. I did not work out whether the split proposed there would also remove this inversion.

Version

The v55.0 release binary, cloud-hypervisor-static-aarch64. The same code is in main at c0eda57.

VM configuration

The script's: --memory size=512M, one vCPU by default, plus one hot-added virtio-blk device.

Ngôn ngữ chính
Rust
Star
6.3k
Fork
786
Merge trung bình
1 ngày 3 giờ
Pull request đã merge (30 ngày)
195

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của cloud-hypervisor/cloud-hypervisor

Tất cả issue của cloud-hypervisor/cloud-hypervisor

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.