Skip to contentSkip to conversation

hw/nvme: sr-iov hotunplug fixes (assertion failure, memory leak)

PATCH In review Daniel Paziyski 2 participants 4 messages Sep 5 – Sep 7

lore
Share & tools
lore
#0 lore raw

cover hw/nvme: sr-iov hotunplug fixes (assertion failure, memory leak)

As the subject suggests, in this series an assertion failure and a memory leak are fixed when hotunplugging a nvme controller with virtual functions.

The patches are exactly the same as
https://lore.kernel.org/qemu-devel/[email protected]/
("[PATCH 1/3] hw/nvme: fix assertion failure on sr-iov capable nvme controller removal")
https://lore.kernel.org/qemu-devel/[email protected]/
("[PATCH 2/3] hw/nvme: fix memory leak on sr-iov capable nvme controller removal")

posted in the series at
https://lore.kernel.org/qemu-devel/[email protected]/
("[PATCH 0/3] NVMe hotunplug and PCI SR-IOV fixes")

I split the patches here, as a separate series, since the other patch is unrelated to these ones and requires a fix, and it was a mistake on my part to put patches for different subsystems in the same series.

Daniel Paziyski (2):
hw/nvme: fix assertion failure on sr-iov capable nvme controller
removal
hw/nvme: fix memory leak on sr-iov capable nvme controller removal

 hw/nvme/ctrl.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 hw/nvme: fix assertion failure on sr-iov capable nvme controller removal

In a nvme subsystem, the ctrls array maps controller IDs to nvme controllers. The value of the array elements can either be NULL (no controller present for this ID), SUBSYS_SLOT_RSVD, or any other value, representing a pointer to the nvme controller structure.

The SUBSYS_SLOT_RSVD value is special: when a nvme controller physical function is being created and is reserving the controller IDs for its virtual functions, it indicates that the slot is soon going to be filled by its corresponding virtual function when it is realized, and on virtual function removal, it means that its controller has been removed.

When the physical function is being removed, it goes through its list of secondary controllers (virtual functions), ensures that their slots have the SUBSYS_SLOT_RSVD values, and then frees up the controller IDs by setting the NULL value. This traversal occurs before the virtual functions are destroyed, causing an assertion failure because the slots contain as values the pointers to the secondary controllers.

Destroy the virtual functions (and therefore, the secondary controllers) after they are offlined in the nvme_ctrl_reset call of the physical function, but before releasing the controller IDs of the secondary controllers in nvme_subsys_unregister_ctrl.

QEMU command line (boot with a hotunplug-aware OS, such as Linux):

    qemu-system-x86_64 -M q35 -device pcie-root-port,id=rp -monitor stdio \
    -device nvme-subsys,id=subsys0 \
    -device nvme,subsys=subsys0,serial=ctrl0,sriov_max_vfs=1,\

sriov_vq_flexible=2,sriov_vi_flexible=1,max_ioqpairs=4,msix_qsize=2,bus=rp,id=ctrl0

In the QEMU monitor:

device_del ctrl0

Message in stderr:

qemu-system-x86_64: ../hw/nvme/subsys.c:49: nvme_subsys_unreserve_cntlids: Assertion `subsys->ctrls[cntlid] == SUBSYS_SLOT_RSVD' failed.

Fixes 44c2c09488db ("hw/nvme: Add support for SR-IOV")
---
 hw/nvme/ctrl.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/nvme/ctrl.c +4 -4
index 4893cf7e74..3040cb1760 100644
@@ -9702,6 +9702,10 @@ static void nvme_exit(PCIDevice *pci_dev)
         }
     }
 
+    if (!pci_is_vf(pci_dev) && n->params.sriov_max_vfs) {
+        pcie_sriov_pf_exit(pci_dev);
+    }
+
     nvme_subsys_unregister_ctrl(n->subsys, n);
 
     g_free(n->cq);
@@ -9726,10 +9730,6 @@ static void nvme_exit(PCIDevice *pci_dev)
         host_memory_backend_set_mapped(n->pmr.dev, false);
     }
 
-    if (!pci_is_vf(pci_dev) && n->params.sriov_max_vfs) {
-        pcie_sriov_pf_exit(pci_dev);
-    }
-
     if (n->params.msix_exclusive_bar && !pci_is_vf(pci_dev)) {
         msix_uninit_exclusive_bar(pci_dev);
     } else {
Signature · 1 line
2.55.0
#2 lore raw

PATCH 2/2 hw/nvme: fix memory leak on sr-iov capable nvme controller removal

In reply to Daniel Paziyski

If a nvme controller is SR-IOV capable, its list of secondary controllers (virtual functions) is stored in the sec_ctrl_list dynamically allocated array, located in the NvmeCtrl struct.

Free the secondary controller list after destroying the virtual functions and freeing their controller IDs.

QEMU command line (boot with a hotunplug-aware OS, such as Linux):

    qemu-system-x86_64 -M q35 -device pcie-root-port,id=rp -monitor stdio \
    -device nvme-subsys,id=subsys0 \
    -device nvme,subsys=subsys0,serial=ctrl0,sriov_max_vfs=1,\

sriov_vq_flexible=2,sriov_vi_flexible=1,max_ioqpairs=4,msix_qsize=2,bus=rp,id=ctrl0

In the QEMU monitor:

device_del ctrl0
quit

ASAN splat:

==78982==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:

    #0 0x7fcbab32bea9 in calloc (/usr/lib/libasan.so.8+0x12bea9) (BuildId: 7f2845989b820f536270e19ec47df085ae89a675)
    #1 0x7fcbaa2a34b2 in g_malloc0 (/usr/lib/libglib-2.0.so.0+0x694b2) (BuildId: cb17d184459352a7985a010f1cd3acef4a4f90d8)
    #2 0x559c531fff4b in nvme_subsys_register_ctrl ../hw/nvme/subsys.c:65
    #3 0x559c531d715e in nvme_init_subsys ../hw/nvme/ctrl.c:9582
    #4 0x559c531d7a7d in nvme_realize ../hw/nvme/ctrl.c:9637
    #5 0x559c5323c0da in pci_qdev_realize ../hw/pci/pci.c:2316
    #6 0x559c54001e88 in device_set_realized ../hw/core/qdev.c:514
    #7 0x559c5402462e in property_set_bool ../qom/object.c:2484
    #8 0x559c5401dbd2 in object_property_set ../qom/object.c:1548
    #9 0x559c5402b76c in object_property_set_qobject ../qom/qom-qobject.c:28
    #10 0x559c5401e24c in object_property_set_bool ../qom/object.c:1618
    #11 0x559c53fffd77 in qdev_realize ../hw/core/qdev.c:277
    #12 0x559c53934166 in qdev_device_add_from_qdict ../system/qdev-monitor.c:740
    #13 0x559c53934272 in qdev_device_add ../system/qdev-monitor.c:758
    #14 0x559c538867c8 in device_init_func ../system/vl.c:1217
    #15 0x559c5487236a in qemu_opts_foreach ../util/qemu-option.c:1148
    #16 0x559c53891205 in qemu_create_cli_devices ../system/vl.c:2762
    #17 0x559c53891968 in qmp_x_exit_preconfig ../system/vl.c:2822
    #18 0x559c53898108 in qemu_init ../system/vl.c:3862
    #19 0x559c545efabf in main ../system/main.c:71
    #20 0x7fcba7627780  (/usr/lib/libc.so.6+0x27780) (BuildId: 1fa174a830cef40a5b2388add4318ee2795f573e)
    #21 0x7fcba76278b8 in __libc_start_main (/usr/lib/libc.so.6+0x278b8) (BuildId: 1fa174a830cef40a5b2388add4318ee2795f573e)
    #22 0x559c524df1f4 in _start (BuildId: 35402cb4fc46114b7a4102258726bbdec82cd9bc)
Fixes c6159d0e384f ("hw/nvme: Allocate sec-ctrl-list as a dynamic array")
---
 hw/nvme/ctrl.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/nvme/ctrl.c +4 -0
index 3040cb1760..76ff92c9eb 100644
@@ -9708,6 +9708,10 @@ static void nvme_exit(PCIDevice *pci_dev)
 
     nvme_subsys_unregister_ctrl(n->subsys, n);
 
+    if (!pci_is_vf(pci_dev) && n->params.sriov_max_vfs) {
+        g_free(n->sec_ctrl_list);
+    }
+
     g_free(n->cq);
     g_free(n->sq);
     g_free(n->aer_reqs);
Signature · 1 line
2.55.0
Reviewed-by on the v1 series
#3 lore raw
In reply to Daniel Paziyski
Quoted 20 lines · Daniel Paziyski

As the subject suggests, in this series an assertion failure and a memory leak are fixed when hotunplugging a nvme controller with virtual functions.

The patches are exactly the same as
https://lore.kernel.org/qemu-devel/[email protected]/
("[PATCH 1/3] hw/nvme: fix assertion failure on sr-iov capable nvme controller removal")
https://lore.kernel.org/qemu-devel/[email protected]/
("[PATCH 2/3] hw/nvme: fix memory leak on sr-iov capable nvme controller removal")

posted in the series at
https://lore.kernel.org/qemu-devel/[email protected]/
("[PATCH 0/3] NVMe hotunplug and PCI SR-IOV fixes")

I split the patches here, as a separate series, since the other patch is unrelated to these ones and requires a fix, and it was a mistake on my part to put patches for different subsystems in the same series.

Quoted 12 lines

Daniel Paziyski (2):
hw/nvme: fix assertion failure on sr-iov capable nvme controller
removal
hw/nvme: fix memory leak on sr-iov capable nvme controller removal

 hw/nvme/ctrl.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
Signature · 1 line
2.55.0

Thanks,