At runtime, consume_config() clears PCI_SRIOV_CTRL_VFE from wmask when
NumVFs > TotalVFs, preventing the guest from enabling more VFs than the
total_vfs-sized dev->exp.sriov_pf.vf[] array holds.

On migration load, get_pci_config_device() validates the incoming
config space against the post-reset wmask where both VFE and NumVFs are
writable, and pcie_sriov_pf_post_load() -> consume_config() ->
register_vfs() then loops up to NumVFs without checking TotalVFs. Clamp
num_vfs to total_vfs in register_vfs() and write the clamped value back
to config space.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4777
Signed-off-by: lbyxiaolizi <[email protected]>
---
 hw/pci/pcie_sriov.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/hw/pci/pcie_sriov.c b/hw/pci/pcie_sriov.c
index 6a748c3..cb79430 100644
--- a/hw/pci/pcie_sriov.c
+++ b/hw/pci/pcie_sriov.c
@@ -35,11 +35,18 @@ static void unparent_vfs(PCIDevice *dev, uint16_t total_vfs)
 static void register_vfs(PCIDevice *dev)
 {
     uint16_t num_vfs;
+    uint16_t total_vfs;
     uint16_t i;
     uint16_t sriov_cap = dev->exp.sriov_cap;
 
     assert(sriov_cap > 0);
     num_vfs = pci_get_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF);
+    total_vfs = pci_get_word(dev->config + sriov_cap + PCI_SRIOV_TOTAL_VF);
+
+    if (num_vfs > total_vfs) {
+        num_vfs = total_vfs;
+        pci_set_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF, num_vfs);
+    }
 
     trace_sriov_register_vfs(dev->name, PCI_SLOT(dev->devfn),
                              PCI_FUNC(dev->devfn), num_vfs);

Reply via email to