From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server 1/2] hotplug pending: only check if USB devices are left when one was unplugged
Date: Mon, 30 Mar 2026 13:09:43 +0200 [thread overview]
Message-ID: <20260330110954.82174-2-f.ebner@proxmox.com> (raw)
In-Reply-To: <20260330110954.82174-1-f.ebner@proxmox.com>
The call to unplug the 'xhci' controller would happen each time as
part of the vmconfig_hotplug_pending() call as long as the VM did not
have any USB devices and as long as it had a new enough version to
support USB hotplug, even if the actual changes have nothing to do
with USB.
Only check if there are any USB devices left if one was unplugged. In
particular, this avoid issuing superfluous QMP commands (via
vm_devices_list()) to check for the presence of the 'xhci' controller
for unrelated changes.
Previously, if an 'xhci' was added as part of a failing USB hotplug in
qemu_usb_hotplug(), it would be removed again implicitly by the check
at the end of vmconfig_hotplug_pending() that is now guarded. Add
explicitly error handling to remove the added 'xhci' controller again
in the error handling case in qemu_usb_hotplug() to avoid a left-over.
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
src/PVE/QemuServer.pm | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 5acd1ac8..26560122 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -4245,15 +4245,26 @@ sub qemu_usb_hotplug {
vm_deviceunplug($vmid, $conf, $deviceid);
# check if xhci controller is necessary and available
+ my $added_xhci;
my $devicelist = vm_devices_list($vmid);
if (!$devicelist->{xhci}) {
my $pciaddr = print_pci_addr("xhci", undef, $arch);
qemu_deviceadd($vmid, PVE::QemuServer::USB::print_qemu_xhci_controller($pciaddr));
+ $added_xhci = 1;
}
# add the new one
- vm_deviceplug($storecfg, $conf, $vmid, $deviceid, $device, $arch, $machine_type);
+ eval { vm_deviceplug($storecfg, $conf, $vmid, $deviceid, $device, $arch, $machine_type); };
+ if (my $err = $@) {
+ if ($added_xhci) {
+ eval { vm_deviceunplug($vmid, $conf, 'xhci'); };
+ warn "failed to unplug xhci controller - $@" if $@;
+ }
+ die $err;
+ }
+
+ return;
}
sub qemu_cpu_hotplug {
@@ -4650,6 +4661,7 @@ sub vmconfig_hotplug_pending {
my $version = extract_version($machine_type, get_running_qemu_version($vmid));
my $hotplug_features =
parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1');
+ my $usb_was_unplugged = 0;
my $usb_hotplug =
$hotplug_features->{usb}
&& min_version($version, 7, 1)
@@ -4680,6 +4692,7 @@ sub vmconfig_hotplug_pending {
die "skip\n" if !$usb_hotplug;
vm_deviceunplug($vmid, $conf, "usbredirdev$index"); # if it's a spice port
vm_deviceunplug($vmid, $conf, $opt);
+ $usb_was_unplugged = 1;
} elsif ($opt eq 'vcpus') {
die "skip\n" if !$hotplug_features->{cpu};
qemu_cpu_hotplug($vmid, $conf, undef);
@@ -4852,7 +4865,7 @@ sub vmconfig_hotplug_pending {
}
# unplug xhci controller if no usb device is left
- if ($usb_hotplug) {
+ if ($usb_was_unplugged) {
my $has_usb = 0;
for (my $i = 0; $i < $PVE::QemuServer::USB::MAX_USB_DEVICES; $i++) {
next if !defined($conf->{"usb$i"});
--
2.47.3
next prev parent reply other threads:[~2026-03-30 11:09 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-30 11:09 [PATCH-SERIES qemu-server 0/2] hotplug pending: only issue QMP commands related to USB hotplug if actually needed Fiona Ebner
2026-03-30 11:09 ` Fiona Ebner [this message]
2026-03-30 11:09 ` [PATCH qemu-server 2/2] hotplug pending: only check for USB hoptplug support when " Fiona Ebner
2026-03-31 14:29 ` [PATCH-SERIES qemu-server 0/2] hotplug pending: only issue QMP commands related to USB hotplug if " Manuel Federanko
2026-03-31 18:40 ` applied: " Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260330110954.82174-2-f.ebner@proxmox.com \
--to=f.ebner@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox