Skip to content

Commit b37b609

Browse files
djbwbjorn-helgaas
authored andcommitted
PCI: vmd: Switch to pci_bus_find_emul_domain_nr()
The new common domain number allocator can replace the custom allocator in VMD. Beyond some code reuse benefits it does close a potential race whereby vmd_find_free_domain() collides with new PCI buses coming online with a conflicting domain number. Such a race has not been observed in practice, hence not tagging this change as a fix. As VMD uses pci_create_root_bus() rather than pci_alloc_host_bridge() + pci_scan_root_bus_bridge() it has no chance to set ->domain_nr in the bridge so needs to manage freeing the domain number on its own. Signed-off-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Cc: Szymon Durawa <szymon.durawa@linux.intel.com> Cc: Nirmal Patel <nirmal.patel@linux.intel.com> Link: https://patch.msgid.link/20251024224622.1470555-3-dan.j.williams@intel.com
1 parent bcce8c7 commit b37b609

1 file changed

Lines changed: 17 additions & 23 deletions

File tree

  • drivers/pci/controller

drivers/pci/controller/vmd.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -565,22 +565,6 @@ static void vmd_detach_resources(struct vmd_dev *vmd)
565565
vmd->dev->resource[VMD_MEMBAR2].child = NULL;
566566
}
567567

568-
/*
569-
* VMD domains start at 0x10000 to not clash with ACPI _SEG domains.
570-
* Per ACPI r6.0, sec 6.5.6, _SEG returns an integer, of which the lower
571-
* 16 bits are the PCI Segment Group (domain) number. Other bits are
572-
* currently reserved.
573-
*/
574-
static int vmd_find_free_domain(void)
575-
{
576-
int domain = 0xffff;
577-
struct pci_bus *bus = NULL;
578-
579-
while ((bus = pci_find_next_bus(bus)) != NULL)
580-
domain = max_t(int, domain, pci_domain_nr(bus));
581-
return domain + 1;
582-
}
583-
584568
static int vmd_get_phys_offsets(struct vmd_dev *vmd, bool native_hint,
585569
resource_size_t *offset1,
586570
resource_size_t *offset2)
@@ -865,13 +849,6 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
865849
.parent = res,
866850
};
867851

868-
sd->vmd_dev = vmd->dev;
869-
sd->domain = vmd_find_free_domain();
870-
if (sd->domain < 0)
871-
return sd->domain;
872-
873-
sd->node = pcibus_to_node(vmd->dev->bus);
874-
875852
/*
876853
* Currently MSI remapping must be enabled in guest passthrough mode
877854
* due to some missing interrupt remapping plumbing. This is probably
@@ -897,9 +874,24 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
897874
pci_add_resource_offset(&resources, &vmd->resources[1], offset[0]);
898875
pci_add_resource_offset(&resources, &vmd->resources[2], offset[1]);
899876

877+
sd->vmd_dev = vmd->dev;
878+
879+
/*
880+
* Emulated domains start at 0x10000 to not clash with ACPI _SEG
881+
* domains. Per ACPI r6.0, sec 6.5.6, _SEG returns an integer, of
882+
* which the lower 16 bits are the PCI Segment Group (domain) number.
883+
* Other bits are currently reserved.
884+
*/
885+
sd->domain = pci_bus_find_emul_domain_nr(0, 0x10000, INT_MAX);
886+
if (sd->domain < 0)
887+
return sd->domain;
888+
889+
sd->node = pcibus_to_node(vmd->dev->bus);
890+
900891
vmd->bus = pci_create_root_bus(&vmd->dev->dev, vmd->busn_start,
901892
&vmd_ops, sd, &resources);
902893
if (!vmd->bus) {
894+
pci_bus_release_emul_domain_nr(sd->domain);
903895
pci_free_resource_list(&resources);
904896
vmd_remove_irq_domain(vmd);
905897
return -ENODEV;
@@ -992,6 +984,7 @@ static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
992984
return -ENOMEM;
993985

994986
vmd->dev = dev;
987+
vmd->sysdata.domain = PCI_DOMAIN_NR_NOT_SET;
995988
vmd->instance = ida_alloc(&vmd_instance_ida, GFP_KERNEL);
996989
if (vmd->instance < 0)
997990
return vmd->instance;
@@ -1057,6 +1050,7 @@ static void vmd_remove(struct pci_dev *dev)
10571050
vmd_detach_resources(vmd);
10581051
vmd_remove_irq_domain(vmd);
10591052
ida_free(&vmd_instance_ida, vmd->instance);
1053+
pci_bus_release_emul_domain_nr(vmd->sysdata.domain);
10601054
}
10611055

10621056
static void vmd_shutdown(struct pci_dev *dev)

0 commit comments

Comments
 (0)