From 1fb1822ec94000e66cbea247d4cae9f77f867cc7 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Thu, 7 Oct 2021 15:45:31 +0200 Subject: [PATCH] fix #3258: block vm start when pci device is already in use on vm start, we reserve all pciids that we use, and remove the reservation again in vm_stop_cleanup first with only a time-based reservation but after the vm is started, we reserve again but with the pid. for this, we have to move the start_timeout calculation above the hostpci handling. also moved the pci initialization out of the conf parsing loop so that we can reserve all ids before we actually touch any of them while touching the lines, fix the indentation this way, when a vm starts with a pci device that is already configured for a different running vm, will not be started and the user gets the error that the device is already in use Signed-off-by: Dominik Csapak Signed-off-by: Thomas Lamprecht --- PVE/QemuServer.pm | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm index eb29fc2e..55603e0e 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -5381,13 +5381,30 @@ sub vm_start_nolock { push @$cmd, '-S'; } - # host pci devices + my $start_timeout = $params->{timeout} // config_aware_timeout($conf, $resume); + + my $pci_devices = {}; # host pci devices for (my $i = 0; $i < $PVE::QemuServer::PCI::MAX_HOSTPCI_DEVICES; $i++) { - my $d = parse_hostpci($conf->{"hostpci$i"}); - next if !$d; - for my $pcidevice ($d->{pciid}->@*) { - PVE::QemuServer::PCI::prepare_pci_device($vmid, $pcidevice->{id}, $i, $d->{mdev}); - } + my $dev = $conf->{"hostpci$i"} or next; + $pci_devices->{$i} = parse_hostpci($dev); + } + + my $pci_id_list = [ map { $_->{id} } map { $_->{pciid}->@* } values $pci_devices->%* ]; + # reserve all PCI IDs before actually doing anything with them + PVE::QemuServer::PCI::reserve_pci_usage($pci_id_list, $vmid, $start_timeout); + + eval { + for my $id (sort keys %$pci_devices) { + my $d = $pci_devices->{$id}; + for my $dev ($d->{pciid}->@*) { + PVE::QemuServer::PCI::prepare_pci_device($vmid, $dev->{id}, $id, $d->{mdev}); + } + } + }; + if (my $err = $@) { + eval { PVE::QemuServer::PCI::remove_pci_reservation($pci_id_list) }; + warn $@ if $@; + die $err; } PVE::Storage::activate_volumes($storecfg, $vollist); @@ -5402,7 +5419,6 @@ sub vm_start_nolock { my $cpuunits = get_cpuunits($conf); - my $start_timeout = $params->{timeout} // config_aware_timeout($conf, $resume); my %run_params = ( timeout => $statefile ? undef : $start_timeout, umask => 0077, @@ -5482,9 +5498,16 @@ sub vm_start_nolock { if (my $err = $@) { # deactivate volumes if start fails eval { PVE::Storage::deactivate_volumes($storecfg, $vollist); }; + eval { PVE::QemuServer::PCI::remove_pci_reservation($pci_id_list) }; + die "start failed: $err"; } + # re-reserve all PCI IDs now that we can know the actual VM PID + my $pid = PVE::QemuServer::Helpers::vm_running_locally($vmid); + eval { PVE::QemuServer::PCI::reserve_pci_usage($pci_id_list, $vmid, undef, $pid) }; + warn $@ if $@; + print "migration listens on $migrate_uri\n" if $migrate_uri; $res->{migrate_uri} = $migrate_uri; @@ -5673,6 +5696,7 @@ sub vm_stop_cleanup { unlink '/dev/shm/pve-shm-' . ($ivshmem->{name} // $vmid); } + my $ids = []; foreach my $key (keys %$conf) { next if $key !~ m/^hostpci(\d+)$/; my $hostpciindex = $1; @@ -5681,9 +5705,11 @@ sub vm_stop_cleanup { foreach my $pci (@{$d->{pciid}}) { my $pciid = $pci->{id}; + push @$ids, $pci->{id}; PVE::SysFSTools::pci_cleanup_mdev_device($pciid, $uuid); } } + PVE::QemuServer::PCI::remove_pci_reservation($ids); vmconfig_apply_pending($vmid, $conf, $storecfg) if $apply_pending_changes; };