public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server v2 24/32] qm: showcmd: never reserve PCI devices
Date: Wed, 18 Jun 2025 15:02:01 +0200	[thread overview]
Message-ID: <20250618130209.90649-25-f.ebner@proxmox.com> (raw)
In-Reply-To: <20250618130209.90649-1-f.ebner@proxmox.com>

Never reserve PCI devices when config_to_command() is called for
'showcmd'. Previously, choose_hostpci_devices() only skipped reserving
PCI devices when the VM was already running as a heuristic. Make it
explicit via a new parameter. Adapt the config-to-command test to
never expect actual reservation. There is a dedicated test for this
since commit "test: add tests for PCI reservations".

Suggested-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

New in v2.

 src/PVE/QemuServer.pm                         | 19 +++++--------------
 src/PVE/QemuServer/PCI.pm                     | 15 ++++++---------
 .../q35-linux-hostpci-mapping.conf.cmd        |  2 +-
 src/test/run_config2command_tests.pl          | 14 ++------------
 4 files changed, 14 insertions(+), 36 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 245e1a26..843db76a 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -3528,8 +3528,8 @@ my sub get_vga_properties {
 sub config_to_command {
     my ($storecfg, $vmid, $conf, $defaults, $options) = @_;
 
-    my ($forcemachine, $forcecpu, $live_restore_backing) =
-        $options->@{qw(force-machine force-cpu live-restore-backing)};
+    my ($forcemachine, $forcecpu, $live_restore_backing, $dry_run) =
+        $options->@{qw(force-machine force-cpu live-restore-backing dry-run)};
 
     # minimize config for templates, they can only start for backup,
     # so most options besides the disks are irrelevant
@@ -3738,7 +3738,7 @@ sub config_to_command {
     # host pci device passthrough
     my ($kvm_off, $gpu_passthrough, $legacy_igd, $pci_devices) =
         PVE::QemuServer::PCI::print_hostpci_devices(
-            $vmid, $conf, $devices, $vga, $winversion, $bridges, $arch, $bootorder,
+            $vmid, $conf, $devices, $vga, $winversion, $bridges, $arch, $bootorder, $dry_run,
         );
 
     # usb devices
@@ -6325,7 +6325,7 @@ sub vm_commandline {
 
     my $conf = PVE::QemuConfig->load_config($vmid);
 
-    my $options = {};
+    my $options = { 'dry-run' => 1 };
     if ($snapname) {
         my $snapshot = $conf->{snapshots}->{$snapname};
         die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
@@ -6350,18 +6350,9 @@ sub vm_commandline {
         PVE::Storage::activate_volumes($storecfg, $volumes);
     }
 
-    my $cmd;
-    eval { $cmd = config_to_command($storecfg, $vmid, $conf, $defaults, $options); };
-    my $err = $@;
-
-    # If the vm is not running, need to clean up the reserved/created devices.
     # There might be concurrent operations on the volumes, so do not deactivate.
-    if (!$running) {
-        eval { cleanup_pci_devices($vmid, $conf) };
-        warn $@ if $@;
-    }
 
-    die $err if $err;
+    my $cmd = config_to_command($storecfg, $vmid, $conf, $defaults, $options);
 
     return PVE::Tools::cmd2string($cmd);
 }
diff --git a/src/PVE/QemuServer/PCI.pm b/src/PVE/QemuServer/PCI.pm
index 2dbc87a7..ddf2f028 100644
--- a/src/PVE/QemuServer/PCI.pm
+++ b/src/PVE/QemuServer/PCI.pm
@@ -577,10 +577,7 @@ my sub create_nvidia_device {
 # mdev devices must be chosen later when we actually allocate it, but we
 # flatten the inner list since there can only be one device per alternative anyway
 sub choose_hostpci_devices {
-    my ($devices, $vmid) = @_;
-
-    # if the vm is running, we must be in 'showcmd', so don't actually reserve or create anything
-    my $is_running = PVE::QemuServer::Helpers::vm_running_locally($vmid) ? 1 : 0;
+    my ($devices, $vmid, $dry_run) = @_;
 
     my $used = {};
 
@@ -606,7 +603,7 @@ sub choose_hostpci_devices {
             # we only have one alternative, use that
             $device->{ids} = $device->{ids}->[0];
             $add_used_device->($device->{ids});
-            if ($device->{nvidia} && !$is_running) {
+            if ($device->{nvidia} && !$dry_run) {
                 reserve_pci_usage($device->{ids}->[0]->{id}, $vmid, 10, undef);
                 create_nvidia_device($device->{ids}->[0]->{id}, $device->{nvidia});
             }
@@ -618,12 +615,12 @@ sub choose_hostpci_devices {
             my $ids = [map { $_->{id} } @$alternative];
 
             next if grep { defined($used->{$_}) } @$ids; # already used
-            if (!$is_running) {
+            if (!$dry_run) {
                 eval { reserve_pci_usage($ids, $vmid, 10, undef) };
                 next if $@;
             }
 
-            if ($device->{nvidia} && !$is_running) {
+            if ($device->{nvidia} && !$dry_run) {
                 eval { create_nvidia_device($ids->[0], $device->{nvidia}) };
                 if (my $err = $@) {
                     warn $err;
@@ -645,14 +642,14 @@ sub choose_hostpci_devices {
 }
 
 sub print_hostpci_devices {
-    my ($vmid, $conf, $devices, $vga, $winversion, $bridges, $arch, $bootorder) = @_;
+    my ($vmid, $conf, $devices, $vga, $winversion, $bridges, $arch, $bootorder, $dry_run) = @_;
 
     my $kvm_off = 0;
     my $gpu_passthrough = 0;
     my $legacy_igd = 0;
 
     my $pciaddr;
-    my $pci_devices = choose_hostpci_devices(parse_hostpci_devices($conf), $vmid);
+    my $pci_devices = choose_hostpci_devices(parse_hostpci_devices($conf), $vmid, $dry_run);
 
     for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
         my $id = "hostpci$i";
diff --git a/src/test/cfg2cmd/q35-linux-hostpci-mapping.conf.cmd b/src/test/cfg2cmd/q35-linux-hostpci-mapping.conf.cmd
index 533e1983..6df8b5f2 100644
--- a/src/test/cfg2cmd/q35-linux-hostpci-mapping.conf.cmd
+++ b/src/test/cfg2cmd/q35-linux-hostpci-mapping.conf.cmd
@@ -29,7 +29,7 @@
   -device 'usb-tablet,id=tablet,bus=ehci.0,port=1' \
   -device 'vfio-pci,host=0000:07:10.0,id=hostpci0,bus=pci.0,addr=0x10' \
   -device 'vfio-pci,sysfsdev=/sys/bus/mdev/devices/00000001-0000-0000-0000-000000008006,id=hostpci1,bus=pci.0,addr=0x11' \
-  -device 'vfio-pci,host=0000:07:10.4,id=hostpci2,bus=pci.0,addr=0x1b' \
+  -device 'vfio-pci,host=0000:07:10.1,id=hostpci2,bus=pci.0,addr=0x1b' \
   -device 'VGA,id=vga,bus=pcie.0,addr=0x1' \
   -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3,free-page-reporting=on' \
   -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \
diff --git a/src/test/run_config2command_tests.pl b/src/test/run_config2command_tests.pl
index cbd8cd8b..9f4ecabf 100755
--- a/src/test/run_config2command_tests.pl
+++ b/src/test/run_config2command_tests.pl
@@ -474,20 +474,10 @@ $mapping_pci_module->mock(
 my $pci_module = Test::MockModule->new("PVE::QemuServer::PCI");
 $pci_module->mock(
     reserve_pci_usage => sub {
-        my ($ids, $vmid, $timeout, $pid, $dryrun) = @_;
-
-        $ids = [$ids] if !ref($ids);
-
-        for my $id (@$ids) {
-            if ($id eq "0000:07:10.1") {
-                die "reserved";
-            }
-        }
-
-        return undef;
+        die "reserve_pci_usage should not be called for 'qm showcmd'\n";
     },
     create_nvidia_device => sub {
-        return 1;
+        die "create_nvidia_device should not be called for 'qm showcmd'\n";
     },
 );
 
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

  parent reply	other threads:[~2025-06-18 13:04 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-18 13:01 [pve-devel] [PATCH-SERIES common/qemu-server v2 00/32] preparation for switch to blockdev Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH common v2 01/32] schema: parse property string: support skipping keys Fiona Ebner
2025-06-20 11:00   ` Fabian Grünbichler
2025-06-18 13:01 ` [pve-devel] [PATCH common v2 02/32] json schema: add helper to convert to JSON boolean Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 03/32] buildsys: order Perl source files in QemuServer/Makefile Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 04/32] drive: code cleanup: drop unused $vmid parameter from get_path_and_format() Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 05/32] cfg2cmd: require at least QEMU binary version 6.0 Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 06/32] drive: parse: use hash argument for optional parameters Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 07/32] drive: parse: properly handle dropped properties for restore Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 08/32] drive: remove geometry options gone since QEMU 3.1 Fiona Ebner
2025-06-20 11:03   ` Fabian Grünbichler
2025-06-20 11:20     ` Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 09/32] clone disk: io uring check: fix call to determine cache direct Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 10/32] drive: move storage_allows_io_uring_default() and drive_uses_cache_direct() helpers to drive module Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 11/32] drive: introduce aio_cmdline_option() helper Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 12/32] drive: introduce detect_zeroes_cmdline_option() helper Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 13/32] vm start: assert that migration type is set for 'tcp' migration Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 14/32] introduce StateFile module for state file related helpers Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 15/32] vm start: move state file handling to dedicated module Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 16/32] vm start: move config_to_command() call further down Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 17/32] vm start/commandline: also clean up pci reservation when config_to_command() fails Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 18/32] vm start/commandline: activate volumes before config_to_command() Fiona Ebner
2025-06-20 11:33   ` Fabian Grünbichler
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 19/32] pci: add missing includes Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 20/32] test: add tests for PCI reservations Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 21/32] cfg2cmd: print vga: fix call to print_pcie_addr() Fiona Ebner
2025-06-18 13:01 ` [pve-devel] [PATCH qemu-server v2 22/32] pci: code cleanup: remove superfluous machine type paramater from print_pci_addr Fiona Ebner
2025-06-18 15:19   ` Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 23/32] cfg2cmd: collect optional parameters as a hash Fiona Ebner
2025-06-18 13:02 ` Fiona Ebner [this message]
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 25/32] vm devices list: prepare querying block device names for -blockdev Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 26/32] print drive device: explicitly set write-cache starting with machine version 10.0 Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 27/32] print drive device: set {r, w}error front-end properties " Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 28/32] print drive device: don't reference any drive for 'none' " Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 29/32] drive: create a throttle group for each drive " Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [PATCH qemu-server v2 30/32] blockdev: add helpers to generate blockdev commandline Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [RFC qemu-server v2 31/32] blockdev: add support for NBD paths Fiona Ebner
2025-06-18 13:02 ` [pve-devel] [RFC qemu-server v2 32/32] command line: switch to blockdev starting with machine version 10.0 Fiona Ebner
2025-06-23  9:12   ` DERUMIER, Alexandre via pve-devel
2025-06-23  9:31     ` Fiona Ebner
2025-06-23 13:06       ` DERUMIER, Alexandre via pve-devel
2025-06-20 13:03 ` [pve-devel] partially-applied: [PATCH-SERIES common/qemu-server v2 00/32] preparation for switch to blockdev Fabian Grünbichler

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=20250618130209.90649-25-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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal