Skip to content

Commit 81e921f

Browse files
LuBaolujoergroedel
authored andcommitted
iommu/vt-d: Fix NULL domain on device release
In the kdump kernel, the IOMMU operates in deferred_attach mode. In this mode, info->domain may not yet be assigned by the time the release_device function is called. It leads to the following crash in the crash kernel: BUG: kernel NULL pointer dereference, address: 000000000000003c ... RIP: 0010:do_raw_spin_lock+0xa/0xa0 ... _raw_spin_lock_irqsave+0x1b/0x30 intel_iommu_release_device+0x96/0x170 iommu_deinit_device+0x39/0xf0 __iommu_group_remove_device+0xa0/0xd0 iommu_bus_notifier+0x55/0xb0 notifier_call_chain+0x5a/0xd0 blocking_notifier_call_chain+0x41/0x60 bus_notify+0x34/0x50 device_del+0x269/0x3d0 pci_remove_bus_device+0x77/0x100 p2sb_bar+0xae/0x1d0 ... i801_probe+0x423/0x740 Use the release_domain mechanism to fix it. The scalable mode context entry which is not part of release domain should be cleared in release_device(). Fixes: 586081d ("iommu/vt-d: Remove DEFER_DEVICE_DOMAIN_INFO") Reported-by: Eric Badger <ebadger@purestorage.com> Closes: https://lore.kernel.org/r/20240113181713.1817855-1-ebadger@purestorage.com Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Link: https://lore.kernel.org/r/20240305013305.204605-3-baolu.lu@linux.intel.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 0061ffe commit 81e921f

3 files changed

Lines changed: 71 additions & 25 deletions

File tree

drivers/iommu/intel/iommu.c

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,30 +3818,6 @@ static void domain_context_clear(struct device_domain_info *info)
38183818
&domain_context_clear_one_cb, info);
38193819
}
38203820

3821-
static void dmar_remove_one_dev_info(struct device *dev)
3822-
{
3823-
struct device_domain_info *info = dev_iommu_priv_get(dev);
3824-
struct dmar_domain *domain = info->domain;
3825-
struct intel_iommu *iommu = info->iommu;
3826-
unsigned long flags;
3827-
3828-
if (!dev_is_real_dma_subdevice(info->dev)) {
3829-
if (dev_is_pci(info->dev) && sm_supported(iommu))
3830-
intel_pasid_tear_down_entry(iommu, info->dev,
3831-
IOMMU_NO_PASID, false);
3832-
3833-
iommu_disable_pci_caps(info);
3834-
domain_context_clear(info);
3835-
}
3836-
3837-
spin_lock_irqsave(&domain->lock, flags);
3838-
list_del(&info->link);
3839-
spin_unlock_irqrestore(&domain->lock, flags);
3840-
3841-
domain_detach_iommu(domain, iommu);
3842-
info->domain = NULL;
3843-
}
3844-
38453821
/*
38463822
* Clear the page table pointer in context or pasid table entries so that
38473823
* all DMA requests without PASID from the device are blocked. If the page
@@ -4367,7 +4343,11 @@ static void intel_iommu_release_device(struct device *dev)
43674343
mutex_lock(&iommu->iopf_lock);
43684344
device_rbtree_remove(info);
43694345
mutex_unlock(&iommu->iopf_lock);
4370-
dmar_remove_one_dev_info(dev);
4346+
4347+
if (sm_supported(iommu) && !dev_is_real_dma_subdevice(dev) &&
4348+
!context_copied(iommu, info->bus, info->devfn))
4349+
intel_pasid_teardown_sm_context(dev);
4350+
43714351
intel_pasid_free_table(dev);
43724352
intel_iommu_debugfs_remove_dev(info);
43734353
kfree(info);
@@ -4826,6 +4806,7 @@ static const struct iommu_dirty_ops intel_dirty_ops = {
48264806

48274807
const struct iommu_ops intel_iommu_ops = {
48284808
.blocked_domain = &blocking_domain,
4809+
.release_domain = &blocking_domain,
48294810
.capable = intel_iommu_capable,
48304811
.hw_info = intel_iommu_hw_info,
48314812
.domain_alloc = intel_iommu_domain_alloc,

drivers/iommu/intel/pasid.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,3 +669,67 @@ int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev,
669669

670670
return 0;
671671
}
672+
673+
/*
674+
* Interfaces to setup or teardown a pasid table to the scalable-mode
675+
* context table entry:
676+
*/
677+
678+
static void device_pasid_table_teardown(struct device *dev, u8 bus, u8 devfn)
679+
{
680+
struct device_domain_info *info = dev_iommu_priv_get(dev);
681+
struct intel_iommu *iommu = info->iommu;
682+
struct context_entry *context;
683+
684+
spin_lock(&iommu->lock);
685+
context = iommu_context_addr(iommu, bus, devfn, false);
686+
if (!context) {
687+
spin_unlock(&iommu->lock);
688+
return;
689+
}
690+
691+
context_clear_entry(context);
692+
__iommu_flush_cache(iommu, context, sizeof(*context));
693+
spin_unlock(&iommu->lock);
694+
695+
/*
696+
* Cache invalidation for changes to a scalable-mode context table
697+
* entry.
698+
*
699+
* Section 6.5.3.3 of the VT-d spec:
700+
* - Device-selective context-cache invalidation;
701+
* - Domain-selective PASID-cache invalidation to affected domains
702+
* (can be skipped if all PASID entries were not-present);
703+
* - Domain-selective IOTLB invalidation to affected domains;
704+
* - Global Device-TLB invalidation to affected functions.
705+
*
706+
* The iommu has been parked in the blocking state. All domains have
707+
* been detached from the device or PASID. The PASID and IOTLB caches
708+
* have been invalidated during the domain detach path.
709+
*/
710+
iommu->flush.flush_context(iommu, 0, PCI_DEVID(bus, devfn),
711+
DMA_CCMD_MASK_NOBIT, DMA_CCMD_DEVICE_INVL);
712+
devtlb_invalidation_with_pasid(iommu, dev, IOMMU_NO_PASID);
713+
}
714+
715+
static int pci_pasid_table_teardown(struct pci_dev *pdev, u16 alias, void *data)
716+
{
717+
struct device *dev = data;
718+
719+
if (dev == &pdev->dev)
720+
device_pasid_table_teardown(dev, PCI_BUS_NUM(alias), alias & 0xff);
721+
722+
return 0;
723+
}
724+
725+
void intel_pasid_teardown_sm_context(struct device *dev)
726+
{
727+
struct device_domain_info *info = dev_iommu_priv_get(dev);
728+
729+
if (!dev_is_pci(dev)) {
730+
device_pasid_table_teardown(dev, info->bus, info->devfn);
731+
return;
732+
}
733+
734+
pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_teardown, dev);
735+
}

drivers/iommu/intel/pasid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,5 @@ void intel_pasid_tear_down_entry(struct intel_iommu *iommu,
319319
bool fault_ignore);
320320
void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu,
321321
struct device *dev, u32 pasid);
322+
void intel_pasid_teardown_sm_context(struct device *dev);
322323
#endif /* __INTEL_PASID_H */

0 commit comments

Comments
 (0)