The aux-domain attach/detach are not tracked, some data structures might be used after free. This causes general protection faults when multiple subdevices are created and assigned to a same guest machine: | general protection fault, probably for non-canonical address 0xdead000000000100: 0000 [#1] SMP NOPTI | RIP: 0010:intel_iommu_aux_detach_device+0x12a/0x1f0 | [...] | Call Trace: | iommu_aux_detach_device+0x24/0x70 | vfio_mdev_detach_domain+0x3b/0x60 | ? vfio_mdev_set_domain+0x50/0x50 | iommu_group_for_each_dev+0x4f/0x80 | vfio_iommu_detach_group.isra.0+0x22/0x30 | vfio_iommu_type1_detach_group.cold+0x71/0x211 | ? find_exported_symbol_in_section+0x4a/0xd0 | ? each_symbol_section+0x28/0x50 | __vfio_group_unset_container+0x4d/0x150 | vfio_group_try_dissolve_container+0x25/0x30 | vfio_group_put_external_user+0x13/0x20 | kvm_vfio_group_put_external_user+0x27/0x40 [kvm] | kvm_vfio_destroy+0x45/0xb0 [kvm] | kvm_put_kvm+0x1bb/0x2e0 [kvm] | kvm_vm_release+0x22/0x30 [kvm] | __fput+0xcc/0x260 | ____fput+0xe/0x10 | task_work_run+0x8f/0xb0 | do_exit+0x358/0xaf0 | ? wake_up_state+0x10/0x20 | ? signal_wake_up_state+0x1a/0x30 | do_group_exit+0x47/0xb0 | __x64_sys_exit_group+0x18/0x20 | do_syscall_64+0x57/0x1d0 | entry_SYSCALL_64_after_hwframe+0x44/0xa9 Fix the crash by tracking the subdevices when attaching and detaching aux-domains.
Unified · Split
| @@ -1877,6 +1877,7 @@ static struct dmar_domain *alloc_domain(int flags) · 1876 unmodified lines | |||
| 1877 | domain->flags |= DOMAIN_FLAG_USE_FIRST_LEVEL; | 1877 | domain->flags |= DOMAIN_FLAG_USE_FIRST_LEVEL; |
| 1878 | domain->has_iotlb_device = false; | 1878 | domain->has_iotlb_device = false; |
| 1879 | INIT_LIST_HEAD(&domain->devices); | 1879 | INIT_LIST_HEAD(&domain->devices); |
| 1880 | INIT_LIST_HEAD(&domain->subdevices); | ||
| 1880 | 1881 | ||
| 1881 | return domain; | 1882 | return domain; |
| 1882 | } | 1883 | } |
| @@ -2547,7 +2548,7 @@ static struct dmar_domain *dmar_insert_one_dev_info(struct intel_iommu *iommu, · 664 unmodified lines | |||
| 2547 | info->iommu = iommu; | 2548 | info->iommu = iommu; |
| 2548 | info->pasid_table = NULL; | 2549 | info->pasid_table = NULL; |
| 2549 | info->auxd_enabled = 0; | 2550 | info->auxd_enabled = 0; |
| 2550 | INIT_LIST_HEAD(&info->auxiliary_domains); | 2551 | INIT_LIST_HEAD(&info->subdevices); |
| 2551 | 2552 | ||
| 2552 | if (dev && dev_is_pci(dev)) { | 2553 | if (dev && dev_is_pci(dev)) { |
| 2553 | struct pci_dev *pdev = to_pci_dev(info->dev); | 2554 | struct pci_dev *pdev = to_pci_dev(info->dev); |
| @@ -4475,33 +4476,61 @@ is_aux_domain(struct device *dev, struct iommu_domain *domain) · 1921 unmodified lines | |||
| 4475 | domain->type == IOMMU_DOMAIN_UNMANAGED; | 4476 | domain->type == IOMMU_DOMAIN_UNMANAGED; |
| 4476 | } | 4477 | } |
| 4477 | 4478 | ||
| 4478 | static void auxiliary_link_device(struct dmar_domain *domain, | 4479 | static inline struct subdev_domain_info * |
| 4479 | struct device *dev) | 4480 | lookup_subdev_info(struct dmar_domain *domain, struct device *dev) |
| 4481 | { | ||
| 4482 | struct subdev_domain_info *sinfo; | ||
| 4483 | |||
| 4484 | if (!list_empty(&domain->subdevices)) { | ||
| 4485 | list_for_each_entry(sinfo, &domain->subdevices, link_domain) { | ||
| 4486 | if (sinfo->pdev == dev) | ||
| 4487 | return sinfo; | ||
| 4488 | } | ||
| 4489 | } | ||
| 4490 | |||
| 4491 | return NULL; | ||
| 4492 | } | ||
| 4493 | |||
| 4494 | static int auxiliary_link_device(struct dmar_domain *domain, | ||
| 4495 | struct device *dev) | ||
| 4480 | { | 4496 | { |
| 4481 | struct device_domain_info *info = get_domain_info(dev); | 4497 | struct device_domain_info *info = get_domain_info(dev); |
| 4498 | struct subdev_domain_info *sinfo = lookup_subdev_info(domain, dev); | ||
| 4482 | 4499 | ||
| 4483 | assert_spin_locked(&device_domain_lock); | 4500 | assert_spin_locked(&device_domain_lock); |
| 4484 | if (WARN_ON(!info)) | 4501 | if (WARN_ON(!info)) |
| 4485 | return; | 4502 | return -EINVAL; |
| 4503 | |||
| 4504 | if (!sinfo) { | ||
| 4505 | sinfo = kzalloc(sizeof(*sinfo), GFP_ATOMIC); | ||
| 4506 | sinfo->domain = domain; | ||
| 4507 | sinfo->pdev = dev; | ||
| 4508 | list_add(&sinfo->link_phys, &info->subdevices); | ||
| 4509 | list_add(&sinfo->link_domain, &domain->subdevices); | ||
| 4510 | } | ||
| 4486 | 4511 | ||
| 4487 | domain->auxd_refcnt++; | 4512 | return ++sinfo->users; |
| 4488 | list_add(&domain->auxd, &info->auxiliary_domains); | ||
| 4489 | } | 4513 | } |
| 4490 | 4514 | ||
| 4491 | static void auxiliary_unlink_device(struct dmar_domain *domain, | 4515 | static int auxiliary_unlink_device(struct dmar_domain *domain, |
| 4492 | struct device *dev) | 4516 | struct device *dev) |
| 4493 | { | 4517 | { |
| 4494 | struct device_domain_info *info = get_domain_info(dev); | 4518 | struct device_domain_info *info = get_domain_info(dev); |
| 4519 | struct subdev_domain_info *sinfo = lookup_subdev_info(domain, dev); | ||
| 4520 | int ret; | ||
| 4495 | 4521 | ||
| 4496 | assert_spin_locked(&device_domain_lock); | 4522 | assert_spin_locked(&device_domain_lock); |
| 4497 | if (WARN_ON(!info)) | 4523 | if (WARN_ON(!info || !sinfo || sinfo->users <= 0)) |
| 4498 | return; | 4524 | return -EINVAL; |
| 4499 | 4525 | ||
| 4500 | list_del(&domain->auxd); | 4526 | ret = --sinfo->users; |
| 4501 | domain->auxd_refcnt--; | 4527 | if (!ret) { |
| 4528 | list_del(&sinfo->link_phys); | ||
| 4529 | list_del(&sinfo->link_domain); | ||
| 4530 | kfree(sinfo); | ||
| 4531 | } | ||
| 4502 | 4532 | ||
| 4503 | if (!domain->auxd_refcnt && domain->default_pasid > 0) | 4533 | return ret; |
| 4504 | ioasid_put(domain->default_pasid); | ||
| 4505 | } | 4534 | } |
| 4506 | 4535 | ||
| 4507 | static int aux_domain_add_dev(struct dmar_domain *domain, | 4536 | static int aux_domain_add_dev(struct dmar_domain *domain, |
| @@ -4530,6 +4559,19 @@ static int aux_domain_add_dev(struct dmar_domain *domain, · 22 unmodified lines | |||
| 4530 | } | 4559 | } |
| 4531 | 4560 | ||
| 4532 | spin_lock_irqsave(&device_domain_lock, flags); | 4561 | spin_lock_irqsave(&device_domain_lock, flags); |
| 4562 | ret = auxiliary_link_device(domain, dev); | ||
| 4563 | if (ret <= 0) | ||
| 4564 | goto link_failed; | ||
| 4565 | |||
| 4566 | /* | ||
| 4567 | * Subdevices from the same physical device can be attached to the | ||
| 4568 | * same domain. For such cases, only the first subdevice attachment | ||
| 4569 | * needs to go through the full steps in this function. So if ret > | ||
| 4570 | * 1, just goto out. | ||
| 4571 | */ | ||
| 4572 | if (ret > 1) | ||
| 4573 | goto out; | ||
| 4574 | |||
| 4533 | /* | 4575 | /* |
| 4534 | * iommu->lock must be held to attach domain to iommu and setup the | 4576 | * iommu->lock must be held to attach domain to iommu and setup the |
| 4535 | * pasid entry for second level translation. | 4577 | * pasid entry for second level translation. |
| @@ -4548,10 +4590,9 @@ static int aux_domain_add_dev(struct dmar_domain *domain, · 12 unmodified lines | |||
| 4548 | domain->default_pasid); | 4590 | domain->default_pasid); |
| 4549 | if (ret) | 4591 | if (ret) |
| 4550 | goto table_failed; | 4592 | goto table_failed; |
| 4551 | spin_unlock(&iommu->lock); | ||
| 4552 | |||
| 4553 | auxiliary_link_device(domain, dev); | ||
| 4554 | 4593 | ||
| 4594 | spin_unlock(&iommu->lock); | ||
| 4595 | out: | ||
| 4555 | spin_unlock_irqrestore(&device_domain_lock, flags); | 4596 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 4556 | 4597 | ||
| 4557 | return 0; | 4598 | return 0; |
| @@ -4560,8 +4601,10 @@ table_failed: · 2 unmodified lines | |||
| 4560 | domain_detach_iommu(domain, iommu); | 4601 | domain_detach_iommu(domain, iommu); |
| 4561 | attach_failed: | 4602 | attach_failed: |
| 4562 | spin_unlock(&iommu->lock); | 4603 | spin_unlock(&iommu->lock); |
| 4604 | auxiliary_unlink_device(domain, dev); | ||
| 4605 | link_failed: | ||
| 4563 | spin_unlock_irqrestore(&device_domain_lock, flags); | 4606 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 4564 | if (!domain->auxd_refcnt && domain->default_pasid > 0) | 4607 | if (list_empty(&domain->subdevices) && domain->default_pasid > 0) |
| 4565 | ioasid_put(domain->default_pasid); | 4608 | ioasid_put(domain->default_pasid); |
| 4566 | 4609 | ||
| 4567 | return ret; | 4610 | return ret; |
| @@ -4581,14 +4624,18 @@ static void aux_domain_remove_dev(struct dmar_domain *domain, · 13 unmodified lines | |||
| 4581 | info = get_domain_info(dev); | 4624 | info = get_domain_info(dev); |
| 4582 | iommu = info->iommu; | 4625 | iommu = info->iommu; |
| 4583 | 4626 | ||
| 4584 | auxiliary_unlink_device(domain, dev); | 4627 | if (!auxiliary_unlink_device(domain, dev)) { |
| 4585 | 4628 | spin_lock(&iommu->lock); | |
| 4586 | spin_lock(&iommu->lock); | 4629 | intel_pasid_tear_down_entry(iommu, dev, |
| 4587 | intel_pasid_tear_down_entry(iommu, dev, domain->default_pasid, false); | 4630 | domain->default_pasid, false); |
| 4588 | domain_detach_iommu(domain, iommu); | 4631 | domain_detach_iommu(domain, iommu); |
| 4589 | spin_unlock(&iommu->lock); | 4632 | spin_unlock(&iommu->lock); |
| 4633 | } | ||
| 4590 | 4634 | ||
| 4591 | spin_unlock_irqrestore(&device_domain_lock, flags); | 4635 | spin_unlock_irqrestore(&device_domain_lock, flags); |
| 4636 | |||
| 4637 | if (list_empty(&domain->subdevices) && domain->default_pasid > 0) | ||
| 4638 | ioasid_put(domain->default_pasid); | ||
| 4592 | } | 4639 | } |
| 4593 | 4640 | ||
| 4594 | static int prepare_domain_attach_device(struct iommu_domain *domain, | 4641 | static int prepare_domain_attach_device(struct iommu_domain *domain, |
| @@ -533,11 +533,10 @@ struct dmar_domain { · 532 unmodified lines | |||
| 533 | /* Domain ids per IOMMU. Use u16 since | 533 | /* Domain ids per IOMMU. Use u16 since |
| 534 | * domain ids are 16 bit wide according | 534 | * domain ids are 16 bit wide according |
| 535 | * to VT-d spec, section 9.3 */ | 535 | * to VT-d spec, section 9.3 */ |
| 536 | unsigned int auxd_refcnt; /* Refcount of auxiliary attaching */ | ||
| 537 | 536 | ||
| 538 | bool has_iotlb_device; | 537 | bool has_iotlb_device; |
| 539 | struct list_head devices; /* all devices' list */ | 538 | struct list_head devices; /* all devices' list */ |
| 540 | struct list_head auxd; /* link to device's auxiliary list */ | 539 | struct list_head subdevices; /* all subdevices' list */ |
| 541 | struct iova_domain iovad; /* iova's that belong to this domain */ | 540 | struct iova_domain iovad; /* iova's that belong to this domain */ |
| 542 | 541 | ||
| 543 | struct dma_pte *pgd; /* virtual address */ | 542 | struct dma_pte *pgd; /* virtual address */ |
| @@ -610,14 +609,21 @@ struct intel_iommu { · 66 unmodified lines | |||
| 610 | struct dmar_drhd_unit *drhd; | 609 | struct dmar_drhd_unit *drhd; |
| 611 | }; | 610 | }; |
| 612 | 611 | ||
| 612 | /* Per subdevice private data */ | ||
| 613 | struct subdev_domain_info { | ||
| 614 | struct list_head link_phys; /* link to phys device siblings */ | ||
| 615 | struct list_head link_domain; /* link to domain siblings */ | ||
| 616 | struct device *pdev; /* physical device derived from */ | ||
| 617 | struct dmar_domain *domain; /* aux-domain */ | ||
| 618 | int users; /* user count */ | ||
| 619 | }; | ||
| 620 | |||
| 613 | /* PCI domain-device relationship */ | 621 | /* PCI domain-device relationship */ |
| 614 | struct device_domain_info { | 622 | struct device_domain_info { |
| 615 | struct list_head link; /* link to domain siblings */ | 623 | struct list_head link; /* link to domain siblings */ |
| 616 | struct list_head global; /* link to global list */ | 624 | struct list_head global; /* link to global list */ |
| 617 | struct list_head table; /* link to pasid table */ | 625 | struct list_head table; /* link to pasid table */ |
| 618 | struct list_head auxiliary_domains; /* auxiliary domains | 626 | struct list_head subdevices; /* subdevices sibling */ |
| 619 | * attached to this device | ||
| 620 | */ | ||
| 621 | u32 segment; /* PCI segment number */ | 627 | u32 segment; /* PCI segment number */ |
| 622 | u8 bus; /* PCI bus number */ | 628 | u8 bus; /* PCI bus number */ |
| 623 | u8 devfn; /* PCI devfn number */ | 629 | u8 devfn; /* PCI devfn number */ |