On Mon, 18 Sep 2023 05:45:05 -0400 Jing Liu <jing2....@intel.com> wrote:
> The vector_use callback is used to enable vector that is unmasked in > guest. The kernel used to only support static MSI-X allocation. When > allocating a new interrupt using "static MSI-X allocation" kernels, > QEMU first disables all previously allocated vectors and then > re-allocates all including the new one. The nr_vectors of VFIOPCIDevice > indicates that all vectors from 0 to nr_vectors are allocated (and may > be enabled), which is used to to loop all the possibly used vectors ^^ ^^ s/to to/to/ > When, e.g., disabling MSI-X interrupts. > > Extend the vector_use function to support dynamic MSI-X allocation when > host supports the capability. QEMU therefore can individually allocate > and enable a new interrupt without affecting others or causing interrupts > lost during runtime. > > Utilize nr_vectors to calculate the upper bound of enabled vectors in > dynamic MSI-X allocation mode since looping all msix_entries_nr is not > efficient and unnecessary. > > Signed-off-by: Jing Liu <jing2....@intel.com> > Tested-by: Reinette Chatre <reinette.cha...@intel.com> > --- > Changes since v1: > - Revise Qemu to QEMU. > > Changes since RFC v1: > - Test vdev->msix->noresize to identify the allocation mode. (Alex) > - Move defer_kvm_irq_routing test out and update nr_vectors in a > common place before vfio_enable_vectors(). (Alex) > - Revise the comments. (Alex) > --- > hw/vfio/pci.c | 44 +++++++++++++++++++++++++++----------------- > 1 file changed, 27 insertions(+), 17 deletions(-) > > diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c > index 60654ca28ab8..84987e46fd7a 100644 > --- a/hw/vfio/pci.c > +++ b/hw/vfio/pci.c > @@ -470,6 +470,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, > unsigned int nr, > VFIOPCIDevice *vdev = VFIO_PCI(pdev); > VFIOMSIVector *vector; > int ret; > + int old_nr_vecs = vdev->nr_vectors; Minor suggestion, it reads slightly better below if this were something like: bool resizing = !!(vdev->nr_vectors < nr + 1); Then use the bool in place of the nr+1 tests below. Thanks, Alex > > trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr); > > @@ -512,33 +513,42 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, > unsigned int nr, > } > > /* > - * We don't want to have the host allocate all possible MSI vectors > - * for a device if they're not in use, so we shutdown and incrementally > - * increase them as needed. > + * When dynamic allocation is not supported, we don't want to have the > + * host allocate all possible MSI vectors for a device if they're not > + * in use, so we shutdown and incrementally increase them as needed. > + * nr_vectors represents the total number of vectors allocated. > + * > + * When dynamic allocation is supported, let the host only allocate > + * and enable a vector when it is in use in guest. nr_vectors represents > + * the upper bound of vectors being enabled (but not all of the ranges > + * is allocated or enabled). > */ > if (vdev->nr_vectors < nr + 1) { > vdev->nr_vectors = nr + 1; > - if (!vdev->defer_kvm_irq_routing) { > + } > + > + if (!vdev->defer_kvm_irq_routing) { > + if (vdev->msix->noresize && (old_nr_vecs < nr + 1)) { > vfio_disable_irqindex(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX); > ret = vfio_enable_vectors(vdev, true); > if (ret) { > error_report("vfio: failed to enable vectors, %d", ret); > } > - } > - } else { > - Error *err = NULL; > - int32_t fd; > - > - if (vector->virq >= 0) { > - fd = event_notifier_get_fd(&vector->kvm_interrupt); > } else { > - fd = event_notifier_get_fd(&vector->interrupt); > - } > + Error *err = NULL; > + int32_t fd; > > - if (vfio_set_irq_signaling(&vdev->vbasedev, > - VFIO_PCI_MSIX_IRQ_INDEX, nr, > - VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) > { > - error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); > + if (vector->virq >= 0) { > + fd = event_notifier_get_fd(&vector->kvm_interrupt); > + } else { > + fd = event_notifier_get_fd(&vector->interrupt); > + } > + > + if (vfio_set_irq_signaling(&vdev->vbasedev, > + VFIO_PCI_MSIX_IRQ_INDEX, nr, > + VFIO_IRQ_SET_ACTION_TRIGGER, fd, > &err)) { > + error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name); > + } > } > } >