iommu/vt-d: Fix lockdep splat in intel_iommu_init()
Subsystems (10)
Lu Baolu
maintainercover iommu/vt-d: Fix lockdep splat in intel_iommu_init()
Hi folks,
As commit c919739ce472 ("iommu/vt-d: Handle race between registration and device probe") highlights, a lockdep splat issue happens after moving iommu probing device process into iommu_device_register().
This is due to a conflict that get_resv_regions wants hold the dmar_global_lock, but it's also possible to be called from within a section where intel_iommu_init() already holds the lock.
Historically, before commit 5f64ce5411b46 ("iommu/vt-d: Duplicate iommu_resv_region objects per device list"), the rcu_lock is used in get_resv_regions. This commit converted it to dmar_global_lock in order to allowing sleeping in iommu_alloc_resv_region().
This aims to fix the lockdep issue by making iommu_alloc_resv_region() available in critical section and rolling dmar_global_lock back to rcu lock in get_resv_regions of the Intel IOMMU driver.
Best regards,
baolu
Lu Baolu (2):
iommu: Add gfp parameter to iommu_alloc_resv_region iommu/vt-d: Use rcu_lock in get_resv_regions
include/linux/iommu.h | 2 +- drivers/acpi/arm64/iort.c | 3 ++- drivers/iommu/amd/iommu.c | 7 ++++--- drivers/iommu/apple-dart.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +- drivers/iommu/arm/arm-smmu/arm-smmu.c | 2 +- drivers/iommu/intel/iommu.c | 12 +++++++----- drivers/iommu/iommu.c | 7 ++++--- drivers/iommu/mtk_iommu.c | 3 ++- drivers/iommu/virtio-iommu.c | 9 ++++++--- 10 files changed, 29 insertions(+), 20 deletions(-)
Signature · 1 line
2.34.1
Lu Baolu
maintainerPATCH 1/2 iommu: Add gfp parameter to iommu_alloc_resv_region
Add gfp parameter to iommu_alloc_resv_region() for the callers to specify the memory allocation behavior. Thus iommu_alloc_resv_region() could also be available in critical contexts.
--- include/linux/iommu.h | 2 +- drivers/acpi/arm64/iort.c | 3 ++- drivers/iommu/amd/iommu.c | 7 ++++--- drivers/iommu/apple-dart.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +- drivers/iommu/arm/arm-smmu/arm-smmu.c | 2 +- drivers/iommu/intel/iommu.c | 8 +++++--- drivers/iommu/iommu.c | 7 ++++--- drivers/iommu/mtk_iommu.c | 3 ++- drivers/iommu/virtio-iommu.c | 9 ++++++--- 10 files changed, 27 insertions(+), 18 deletions(-)
include/linux/iommu.h +1 -1
@@ -455,7 +455,7 @@ extern void iommu_set_default_translated(bool cmd_line);extern bool iommu_default_passthrough (void );extern struct iommu_resv_region *iommu_alloc_resv_region (phys_addr_t start,size_t length,int prot, -enum iommu_resv_type type); +enum iommu_resv_type type,gfp_t gfp);extern int iommu_get_group_resv_regions (struct iommu_group *group,struct list_head *head);
drivers/acpi/arm64/iort.c +2 -1
@@ -1142,7 +1142,8 @@ static void iort_iommu_msi_get_resv_regions(struct device *dev,struct iommu_resv_region *region; region =iommu_alloc_resv_region (base +SZ_64K ,SZ_64K , - prot,IOMMU_RESV_MSI ); + prot,IOMMU_RESV_MSI , +GFP_KERNEL );if (region)list_add_tail (®ion->list, head); }
drivers/iommu/amd/iommu.c +4 -3
@@ -2330,7 +2330,8 @@ static void amd_iommu_get_resv_regions(struct device *dev, type =IOMMU_RESV_RESERVED ; region =iommu_alloc_resv_region (entry->address_start, - length, prot, type); + length, prot, type, +GFP_KERNEL );if (!region) {dev_err (dev,"Out of memory allocating dm-regions\n" );return ;
@@ -2340,14 +2341,14 @@ static void amd_iommu_get_resv_regions(struct device *dev, region =iommu_alloc_resv_region (MSI_RANGE_START ,MSI_RANGE_END -MSI_RANGE_START +1 , -0 ,IOMMU_RESV_MSI ); +0 ,IOMMU_RESV_MSI ,GFP_KERNEL );if (!region)return ;list_add_tail (®ion->list, head); region =iommu_alloc_resv_region (HT_RANGE_START ,HT_RANGE_END -HT_RANGE_START +1 , -0 ,IOMMU_RESV_RESERVED ); +0 ,IOMMU_RESV_RESERVED ,GFP_KERNEL );if (!region)return ;list_add_tail (®ion->list, head);
drivers/iommu/apple-dart.c +1 -1
@@ -758,7 +758,7 @@ static void apple_dart_get_resv_regions(struct device *dev, region =iommu_alloc_resv_region (DOORBELL_ADDR ,PAGE_SIZE , prot, -IOMMU_RESV_MSI ); +IOMMU_RESV_MSI ,GFP_KERNEL );if (!region)return ;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1 -1
@@ -2757,7 +2757,7 @@ static void arm_smmu_get_resv_regions(struct device *dev,int prot =IOMMU_WRITE |IOMMU_NOEXEC |IOMMU_MMIO ; region =iommu_alloc_resv_region (MSI_IOVA_BASE ,MSI_IOVA_LENGTH , - prot,IOMMU_RESV_SW_MSI ); + prot,IOMMU_RESV_SW_MSI ,GFP_KERNEL );if (!region)return ;
drivers/iommu/arm/arm-smmu/arm-smmu.c +1 -1
@@ -1534,7 +1534,7 @@ static void arm_smmu_get_resv_regions(struct device *dev,int prot =IOMMU_WRITE |IOMMU_NOEXEC |IOMMU_MMIO ; region =iommu_alloc_resv_region (MSI_IOVA_BASE ,MSI_IOVA_LENGTH , - prot,IOMMU_RESV_SW_MSI ); + prot,IOMMU_RESV_SW_MSI ,GFP_KERNEL );if (!region)return ;
drivers/iommu/intel/iommu.c +5 -3
@@ -4552,7 +4552,8 @@ static void intel_iommu_get_resv_regions(struct device *device, IOMMU_RESV_DIRECT_RELAXABLE :IOMMU_RESV_DIRECT ; resv =iommu_alloc_resv_region (rmrr->base_address, - length, prot, type); + length, prot, type, +GFP_KERNEL );if (!resv)break ;
@@ -4567,7 +4568,8 @@ static void intel_iommu_get_resv_regions(struct device *device,if ((pdev->class >>8 ) ==PCI_CLASS_BRIDGE_ISA ) { reg =iommu_alloc_resv_region (0 ,1UL <<24 , prot, -IOMMU_RESV_DIRECT_RELAXABLE ); +IOMMU_RESV_DIRECT_RELAXABLE , +GFP_KERNEL );if (reg)list_add_tail (®->list, head); }
@@ -4576,7 +4578,7 @@ static void intel_iommu_get_resv_regions(struct device *device, reg =iommu_alloc_resv_region (IOAPIC_RANGE_START ,IOAPIC_RANGE_END -IOAPIC_RANGE_START +1 , -0 ,IOMMU_RESV_MSI ); +0 ,IOMMU_RESV_MSI ,GFP_KERNEL );if (!reg)return ;list_add_tail (®->list, head);
drivers/iommu/iommu.c +4 -3
@@ -504,7 +504,7 @@ static int iommu_insert_resv_region(struct iommu_resv_region *new,LIST_HEAD (stack); nr =iommu_alloc_resv_region (new->start, new->length, - new->prot, new->type); + new->prot, new->type,GFP_KERNEL );if (!nr)return -ENOMEM ;
@@ -2579,11 +2579,12 @@ EXPORT_SYMBOL(iommu_put_resv_regions);struct iommu_resv_region *iommu_alloc_resv_region (phys_addr_t start,size_t length,int prot, -enum iommu_resv_type type) +enum iommu_resv_type type, +gfp_t gfp) {struct iommu_resv_region *region; - region =kzalloc (sizeof (*region),GFP_KERNEL ); + region =kzalloc (sizeof (*region), gfp);if (!region)return NULL ;
drivers/iommu/mtk_iommu.c +2 -1
@@ -917,7 +917,8 @@ static void mtk_iommu_get_resv_regions(struct device *dev,continue ; region =iommu_alloc_resv_region (resv->iova_base, resv->size, - prot,IOMMU_RESV_RESERVED ); + prot,IOMMU_RESV_RESERVED , +GFP_KERNEL );if (!region)return ;
drivers/iommu/virtio-iommu.c +6 -3
@@ -490,11 +490,13 @@ static int viommu_add_resv_mem(struct viommu_endpoint *vdev, fallthrough;case VIRTIO_IOMMU_RESV_MEM_T_RESERVED : region =iommu_alloc_resv_region (start, size,0 , -IOMMU_RESV_RESERVED ); +IOMMU_RESV_RESERVED , +GFP_KERNEL );break ;case VIRTIO_IOMMU_RESV_MEM_T_MSI : region =iommu_alloc_resv_region (start, size, prot, -IOMMU_RESV_MSI ); +IOMMU_RESV_MSI , +GFP_KERNEL );break ; }if (!region)
@@ -909,7 +911,8 @@ static void viommu_get_resv_regions(struct device *dev, struct list_head *head)*/ if (!msi) { msi =iommu_alloc_resv_region (MSI_IOVA_BASE ,MSI_IOVA_LENGTH , - prot,IOMMU_RESV_SW_MSI ); + prot,IOMMU_RESV_SW_MSI , +GFP_KERNEL );if (!msi)return ;
Signature · 1 line
2.34.1
Lu Baolu
maintainerPATCH 2/2 iommu/vt-d: Use rcu_lock in get_resv_regions
In reply to Lu BaoluCommit 5f64ce5411b46 ("iommu/vt-d: Duplicate iommu_resv_region objects per device list") converted rcu_lock in get_resv_regions to dmar_global_lock to allow sleeping in iommu_alloc_resv_region(). This introduced possible recursive locking if get_resv_regions is called from within a section where intel_iommu_init() already holds dmar_global_lock.
Especially after commit c919739ce4721 ("iommu/vt-d: Handle race between registration and device probe"), below lockdep splats could always be seen.
============================================
WARNING: possible recursive locking detected
6.0.0-rc4+ #325 Tainted: G I
--------------------------------------------
swapper/0/1 is trying to acquire lock:
ffffffffa8a18c90 (dmar_global_lock){++++}-{3:3}, at:
intel_iommu_get_resv_regions+0x25/0x270
but task is already holding lock:
ffffffffa8a18c90 (dmar_global_lock){++++}-{3:3}, at:
intel_iommu_init+0x36d/0x6ea
...
Call Trace: <TASK> dump_stack_lvl+0x48/0x5f __lock_acquire.cold.73+0xad/0x2bb lock_acquire+0xc2/0x2e0 ? intel_iommu_get_resv_regions+0x25/0x270 ? lock_is_held_type+0x9d/0x110 down_read+0x42/0x150 ? intel_iommu_get_resv_regions+0x25/0x270 intel_iommu_get_resv_regions+0x25/0x270 iommu_create_device_direct_mappings.isra.28+0x8d/0x1c0 ? iommu_get_dma_cookie+0x6d/0x90 bus_iommu_probe+0x19f/0x2e0 iommu_device_register+0xd4/0x130 intel_iommu_init+0x3e1/0x6ea ? iommu_setup+0x289/0x289 ? rdinit_setup+0x34/0x34 pci_iommu_init+0x12/0x3a do_one_initcall+0x65/0x320 ? rdinit_setup+0x34/0x34 ? rcu_read_lock_sched_held+0x5a/0x80 kernel_init_freeable+0x28a/0x2f3 ? rest_init+0x1b0/0x1b0 kernel_init+0x1a/0x130 ret_from_fork+0x1f/0x30 </TASK>
This rolls back dmar_global_lock to rcu_lock to avoid the lockdep splat.
--- drivers/iommu/intel/iommu.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/intel/iommu.c +3 -3
@@ -4534,7 +4534,7 @@ static void intel_iommu_get_resv_regions(struct device *device,struct device *i_dev;int i; -down_read (&dmar_global_lock); +rcu_read_lock ();for_each_rmrr_units (rmrr) {for_each_active_dev_scope (rmrr->devices, rmrr->devices_cnt, i, i_dev) {
@@ -4553,14 +4553,14 @@ static void intel_iommu_get_resv_regions(struct device *device, resv =iommu_alloc_resv_region (rmrr->base_address, length, prot, type, -GFP_KERNEL ); +GFP_ATOMIC );if (!resv)break ;list_add_tail (&resv->list, head); } } -up_read (&dmar_global_lock); +rcu_read_unlock ();#ifdef CONFIG_INTEL_IOMMU_FLOPPY_WA if (dev_is_pci (device)) {
Signature · 1 line
2.34.1
Baolu Lu
maintainerQuoted 10 lines · Lu Baolu
Hi folks,
As commit
c919739ce472("iommu/vt-d: Handle race between registration and device probe") highlights, a lockdep splat issue happens after moving iommu probing device process intoiommu_device_register().This is due to a conflict that
get_resv_regionswants hold thedmar_global_lock, but it's also possible to be called from within a section whereintel_iommu_init()already holds the lock.
Quoted 8 lines
Historically, before commit
5f64ce5411b46("iommu/vt-d: Duplicateiommu_resv_regionobjects per device list"), thercu_lockis used inget_resv_regions. This commit converted it todmar_global_lockin order to allowing sleeping iniommu_alloc_resv_region().This aims to fix the lockdep issue by making
iommu_alloc_resv_region()available in critical section and rollingdmar_global_lockback to rcu lock inget_resv_regionsof the Intel IOMMU driver.
A gentle ping. Any comments to this fix? It's better to work out a feasible solution before the users keep complaining. :-)
Best regards,
baolu
Quoted 26 lines · On Tue, 27 Sep 2022 13:31:07 +0800 Lu Baolu <[email protected]> wrote:
Hi folks,
As commit
c919739ce472("iommu/vt-d: Handle race between registration and device probe") highlights, a lockdep splat issue happens after moving iommu probing device process intoiommu_device_register().This is due to a conflict that
get_resv_regionswants hold thedmar_global_lock, but it's also possible to be called from within a section whereintel_iommu_init()already holds the lock.Historically, before commit
5f64ce5411b46("iommu/vt-d: Duplicateiommu_resv_regionobjects per device list"), thercu_lockis used inget_resv_regions. This commit converted it todmar_global_lockin order to allowing sleeping iniommu_alloc_resv_region().This aims to fix the lockdep issue by making
iommu_alloc_resv_region()available in critical section and rollingdmar_global_lockback to rcu lock inget_resv_regionsof the Intel IOMMU driver.Best regards,
baoluLu Baolu (2):
iommu: Add gfp parameter toiommu_alloc_resv_regioniommu/vt-d: Usercu_lockinget_resv_regions
Quoted 11 lines
include/linux/iommu.h | 2 +- drivers/acpi/arm64/iort.c | 3 ++- drivers/iommu/amd/iommu.c | 7 ++++--- drivers/iommu/apple-dart.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +- drivers/iommu/arm/arm-smmu/arm-smmu.c | 2 +- drivers/iommu/intel/iommu.c | 12 +++++++----- drivers/iommu/iommu.c | 7 ++++--- drivers/iommu/mtk_iommu.c | 3 ++- drivers/iommu/virtio-iommu.c | 9 ++++++--- 10 files changed, 29 insertions(+), 20 deletions(-)
Resolves the regression for me. Hopefully Joerg will queue this for 6.1-rc. Thanks!
Baolu Lu
maintainerQuoted 19 lines · Lu Baolu
Hi folks,
As commit
c919739ce472("iommu/vt-d: Handle race between registration and device probe") highlights, a lockdep splat issue happens after moving iommu probing device process intoiommu_device_register().This is due to a conflict that
get_resv_regionswants hold thedmar_global_lock, but it's also possible to be called from within a section whereintel_iommu_init()already holds the lock.Historically, before commit
5f64ce5411b46("iommu/vt-d: Duplicateiommu_resv_regionobjects per device list"), thercu_lockis used inget_resv_regions. This commit converted it todmar_global_lockin order to allowing sleeping iniommu_alloc_resv_region().This aims to fix the lockdep issue by making
iommu_alloc_resv_region()available in critical section and rollingdmar_global_lockback to rcu lock inget_resv_regionsof the Intel IOMMU driver.
Quoted 6 lines
Best regards,
baoluLu Baolu (2):
iommu: Add gfp parameter toiommu_alloc_resv_regioniommu/vt-d: Usercu_lockinget_resv_regions
Patches has been queued for v6.1.
https://lore.kernel.org/linux-iommu/[email protected]/
Best regards,
baolu