Skip to content

Commit ccda459

Browse files
binbinwu1wenlingz
authored andcommitted
dm: passthru: add error handling if msix table init failed
Release the resource reqeusted during msix table init if error occurs. Change the type of the second arg of deinit_msix_table from pci_vdev to ptdev, to align with init_msix_table. Tracked-On: #1782 Signed-off-by: Binbin Wu <binbin.wu@intel.com> Reviewed-by: Shuo A Liu <shuo.a.liu@intel.com>
1 parent 3363779 commit ccda459

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

devicemodel/hw/pci/passthrough.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct passthru_dev {
102102
int table_size; /* page aligned size */
103103
void *table_pages;
104104
int table_offset; /* page aligned */
105+
bool ptirq_allocated;
105106
} msix;
106107
bool pcie_cap;
107108
struct pcisel sel;
@@ -465,7 +466,13 @@ init_msix_table(struct vmctx *ctx, struct passthru_dev *ptdev, uint64_t base)
465466
ptirq.msix.vector_cnt = dev->msix.table_count;
466467
ptirq.msix.table_paddr = 0;
467468
ptirq.msix.table_size = 0;
468-
vm_set_ptdev_msix_info(ctx, &ptirq);
469+
error = vm_set_ptdev_msix_info(ctx, &ptirq);
470+
if (error) {
471+
warnx("Failed to alloc ptirq entry on %x/%x/%x", b,s,f);
472+
return error;
473+
}
474+
ptdev->msix.ptirq_allocated = true;
475+
469476

470477
/* Skip the MSI-X table */
471478
base += table_size;
@@ -492,15 +499,18 @@ init_msix_table(struct vmctx *ctx, struct passthru_dev *ptdev, uint64_t base)
492499
}
493500

494501
static void
495-
deinit_msix_table(struct vmctx *ctx, struct pci_vdev *dev)
502+
deinit_msix_table(struct vmctx *ctx, struct passthru_dev *ptdev)
496503
{
497-
struct passthru_dev *ptdev = (struct passthru_dev *) dev->arg;;
504+
struct pci_vdev *dev = ptdev->dev;
498505
uint16_t virt_bdf = PCI_BDF(dev->bus, dev->slot, dev->func);
499506
int vector_cnt = dev->msix.table_count;
500507

501-
printf("ptdev reset msix: 0x%x-%x, vector_cnt=%d.\n",
502-
virt_bdf, ptdev->phys_bdf, vector_cnt);
503-
vm_reset_ptdev_msix_info(ctx, virt_bdf, vector_cnt);
508+
if (ptdev->msix.ptirq_allocated) {
509+
printf("ptdev reset msix: 0x%x-%x, vector_cnt=%d.\n",
510+
virt_bdf, ptdev->phys_bdf, vector_cnt);
511+
vm_reset_ptdev_msix_info(ctx, virt_bdf, vector_cnt);
512+
ptdev->msix.ptirq_allocated = false;
513+
}
504514

505515
if (ptdev->msix.table_pages) {
506516
pci_device_unmap_range(ptdev->phys_dev, ptdev->msix.table_pages, ptdev->msix.table_size);
@@ -589,8 +599,10 @@ cfginitbar(struct vmctx *ctx, struct passthru_dev *ptdev)
589599
/* The MSI-X table needs special handling */
590600
if (i == ptdev_msix_table_bar(ptdev)) {
591601
error = init_msix_table(ctx, ptdev, base);
592-
if (error)
602+
if (error) {
603+
deinit_msix_table(ctx, ptdev);
593604
return -1;
605+
}
594606
} else if (bartype != PCIBAR_IO) {
595607
/* Map the physical BAR in the guest MMIO space */
596608
error = vm_map_ptdev_mmio(ctx, ptdev->sel.bus,
@@ -890,7 +902,7 @@ passthru_deinit(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
890902
func = ptdev->phys_bdf & 0x7;
891903

892904
if (ptdev->msix.capoff != 0)
893-
deinit_msix_table(ctx, dev);
905+
deinit_msix_table(ctx, ptdev);
894906
else if(ptdev->msi.capoff != 0) {
895907
/* Currently only support 1 vector */
896908
printf("ptdev reset msi: 0x%x-%x\n", virt_bdf, ptdev->phys_bdf);

0 commit comments

Comments
 (0)