Skip to contentSkip to conversation

iommu/amd: Enable swiotlb if any device supports iommu v2 and uses identity mapping

PATCH In review Kai-Heng Feng 4 participants 9 messages Jul 8, 2021 – Sep 9, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/amd: Enable swiotlb if any device supports iommu v2 and uses identity mapping

We are seeing kernel panic on rtw88 probe routine because swiotlb isn't set:

[  252.036773] rtw_8821ce 0000:06:00.0: enabling device (0000 -> 0003)
[  252.037084] Kernel panic - not syncing: Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer
[  252.037146] CPU: 7 PID: 1174 Comm: modprobe Not tainted 5.13.0+ #39
[  252.037175] Hardware name: HP HP ProDesk 405 G6 Small Form Factor PC/8835, BIOS S05 Ver. 02.04.00 06/03/2021
[  252.037218] Call Trace:
[  252.037231]  dump_stack_lvl+0x4a/0x5f
[  252.037251]  dump_stack+0x10/0x12
[  252.037267]  panic+0x101/0x2e3
[  252.037284]  swiotlb_tbl_map_single.cold+0xc/0x73
[  252.037305]  ? __mod_lruvec_page_state+0x95/0xb0
[  252.037329]  ? kmalloc_large_node+0x8c/0xb0
[  252.037348]  ? __netdev_alloc_skb+0x44/0x160
[  252.037370]  swiotlb_map+0x61/0x240
[  252.037387]  ? __alloc_skb+0xed/0x1e0
[  252.037404]  dma_map_page_attrs+0x12c/0x1f0
[  252.037422]  ? __netdev_alloc_skb+0x44/0x160
[  252.037443]  rtw_pci_probe+0x30f/0x872 [rtw88_pci]
[  252.037467]  local_pci_probe+0x48/0x80
[  252.037487]  pci_device_probe+0x105/0x1c0
[  252.037506]  really_probe+0x1fe/0x3f0
[  252.037524]  __driver_probe_device+0x109/0x180
[  252.037545]  driver_probe_device+0x23/0x90
[  252.037564]  __driver_attach+0xac/0x1b0
[  252.037582]  ? __device_attach_driver+0xe0/0xe0
[  252.037602]  bus_for_each_dev+0x7e/0xc0
[  252.037620]  driver_attach+0x1e/0x20
[  252.037637]  bus_add_driver+0x135/0x1f0
[  252.037654]  driver_register+0x95/0xf0
[  252.037672]  ? 0xffffffffc0fa0000
[  252.037687]  __pci_register_driver+0x68/0x70
[  252.037707]  rtw_8821ce_driver_init+0x23/0x1000 [rtw88_8821ce]
[  252.037734]  do_one_initcall+0x48/0x1d0
[  252.037752]  ? __cond_resched+0x1a/0x50
[  252.037771]  ? kmem_cache_alloc_trace+0x29d/0x3c0
[  252.037792]  do_init_module+0x62/0x280
[  252.037810]  load_module+0x2577/0x27c0
[  252.037862]  __do_sys_finit_module+0xbf/0x120
[  252.037877]  __x64_sys_finit_module+0x1a/0x20
[  252.037893]  do_syscall_64+0x3b/0xc0
[  252.037907]  entry_SYSCALL_64_after_hwframe+0x44/0xae
[  252.037925] RIP: 0033:0x7ff5a2f9408d
[  252.037938] Code: 27 0d 00 0f 05 eb a9 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d ab dd 0c 00 f7 d8 64 89 01 48
[  252.037993] RSP: 002b:00007fffaa89dce8 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
[  252.038017] RAX: ffffffffffffffda RBX: 000055fd4f881080 RCX: 00007ff5a2f9408d
[  252.038039] RDX: 0000000000000000 RSI: 000055fd4f63ec02 RDI: 0000000000000009
[  252.038063] RBP: 0000000000040000 R08: 0000000000000000 R09: 000055fd4f8885b0
[  252.038085] R10: 0000000000000009 R11: 0000000000000246 R12: 000055fd4f63ec02
[  252.038107] R13: 000055fd4f881120 R14: 0000000000000000 R15: 000055fd4f88e350
[  252.038293] Kernel Offset: 0x30600000 from 0xffffffff81000000 (relocation range: 0xffffffff80000000-0xffffffffbfffffff)

Because the Realtek WiFi (PCI 06:00.0) is in the same IOMMU group as AMD graphics (PCI 01:00.0),

[    1.326166] pci 0000:01:00.0: Adding to iommu group 0
...
[    1.326268] pci 0000:06:00.0: Adding to iommu group 0

And the AMD graphics supports iommu v2, so the group uses intentity mapping based on the query from amd_iommu_def_domain_type().

However, the Realtek WiFi doesn't support 64bit DMA, so we need to enable swiotlb, which was disabled by amd_iommu_init_dma_ops(), to make remapping work.

---
 drivers/iommu/amd/iommu.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/iommu/amd/iommu.c +3 -0
index 811a49a95d04..7c5111ed5c97 100644
@@ -344,6 +344,9 @@ static int iommu_init_device(struct device *dev)
 
 		iommu = amd_iommu_rlookup_table[dev_data->devid];
 		dev_data->iommu_v2 = iommu->is_iommu_v2;
+
+		if (dev_data->iommu_v2)
+			swiotlb = 1;
 	}
 
 	dev_iommu_priv_set(dev, dev_data);
Signature · 1 line
2.31.1
maintainer
#1 lore raw

drivers/iommu/amd/iommu.c static int iommu_init_device(struct device *dev) · line 349

Quoted 7 lines · Kai-Heng Feng
drivers/iommu/amd/iommu.c +3 -0
@@ -344,6 +344,9 @@ static int iommu_init_device(struct device *dev)
 
 		iommu = amd_iommu_rlookup_table[dev_data->devid];
 		dev_data->iommu_v2 = iommu->is_iommu_v2;
+
+		if (dev_data->iommu_v2)
+			swiotlb = 1;

This looks like the big hammer, as it will affect all other systems where the AMD GPUs are in their own group.

What is needed here is an explicit check whether a non-iommu-v2 device is direct-mapped because it shares a group with the GPU, and only enable swiotlb in this case.

Thanks,

Joerg

reviewer
#2 lore raw
Quoted 12 lines · Joerg Roedel

drivers/iommu/amd/iommu.c static int iommu_init_device(struct device *dev) · line 349

Quoted 7 lines · Kai-Heng Feng
drivers/iommu/amd/iommu.c +3 -0
@@ -344,6 +344,9 @@ static int iommu_init_device(struct device *dev)
 
  		iommu = amd_iommu_rlookup_table[dev_data->devid];
  		dev_data->iommu_v2 = iommu->is_iommu_v2;
+
+		if (dev_data->iommu_v2)
+			swiotlb = 1;

This looks like the big hammer, as it will affect all other systems where the AMD GPUs are in their own group.

Quoted 3 lines

What is needed here is an explicit check whether a non-iommu-v2 device is direct-mapped because it shares a group with the GPU, and only enable swiotlb in this case.

Right, it's basically about whether any DMA-limited device might at any time end up in an IOMMU_DOMAIN_IDENTITY domain. And given the possibility of device hotplug and the user being silly with the sysfs interface, I don't think we can categorically determine that at boot time.

Also note that Intel systems are likely to be similarly affected (in fact intel-iommu doesn't even have the iommu_default_passthough() check so it's probably even easier to blow up).

Robin.

#3 lore raw
Quoted 18 lines · Robin Murphy
Quoted 12 lines · Joerg Roedel

drivers/iommu/amd/iommu.c static int iommu_init_device(struct device *dev) · line 349

Quoted 7 lines · Kai-Heng Feng
drivers/iommu/amd/iommu.c +3 -0
@@ -344,6 +344,9 @@ static int iommu_init_device(struct device *dev)
 
             iommu = amd_iommu_rlookup_table[dev_data->devid];
             dev_data->iommu_v2 = iommu->is_iommu_v2;
+
+            if (dev_data->iommu_v2)
+                    swiotlb = 1;

This looks like the big hammer, as it will affect all other systems where the AMD GPUs are in their own group.

Quoted 3 lines

What is needed here is an explicit check whether a non-iommu-v2 device is direct-mapped because it shares a group with the GPU, and only enable swiotlb in this case.

Quoted 8 lines

Right, it's basically about whether any DMA-limited device might at any time end up in an IOMMU_DOMAIN_IDENTITY domain. And given the possibility of device hotplug and the user being silly with the sysfs interface, I don't think we can categorically determine that at boot time.

Also note that Intel systems are likely to be similarly affected (in fact intel-iommu doesn't even have the iommu_default_passthough() check so it's probably even easier to blow up).

swiotlb is enabled by pci_swiotlb_detect_4gb() and intel-iommu doesn't disable it.

I wonder if we can take the same approach in amd-iommu?

Kai-Heng

Quoted 2 lines

Robin.

reviewer
#4 lore raw
Quoted 24 lines · Kai-Heng Feng
Quoted 23 lines · Robin Murphy
Quoted 12 lines · Joerg Roedel

drivers/iommu/amd/iommu.c static int iommu_init_device(struct device *dev) · line 349

Quoted 7 lines · Kai-Heng Feng
drivers/iommu/amd/iommu.c +3 -0
@@ -344,6 +344,9 @@ static int iommu_init_device(struct device *dev)
 
              iommu = amd_iommu_rlookup_table[dev_data->devid];
              dev_data->iommu_v2 = iommu->is_iommu_v2;
+
+            if (dev_data->iommu_v2)
+                    swiotlb = 1;

This looks like the big hammer, as it will affect all other systems where the AMD GPUs are in their own group.

Quoted 3 lines

What is needed here is an explicit check whether a non-iommu-v2 device is direct-mapped because it shares a group with the GPU, and only enable swiotlb in this case.

Right, it's basically about whether any DMA-limited device might at any time end up in an IOMMU_DOMAIN_IDENTITY domain. And given the possibility of device hotplug and the user being silly with the sysfs interface, I don't think we can categorically determine that at boot time.

Quoted 6 lines
Quoted 3 lines

Also note that Intel systems are likely to be similarly affected (in fact intel-iommu doesn't even have the iommu_default_passthough() check so it's probably even easier to blow up).

swiotlb is enabled by pci_swiotlb_detect_4gb() and intel-iommu doesn't disable it.

Oh, right... I did say I found this dance hard to follow. Clearly I shouldn't have trusted what I thought I remembered from looking at it yesterday :)

Also not helped by the fact that it sets iommu_detected which *does* disable SWIOTLB, but only on IA-64.

Quoted 1 line

I wonder if we can take the same approach in amd-iommu?

Certainly if there's a precedent for leaving SWIOTLB enabled even if it *might* be redundant, that seems like the easiest option (it's what we do on arm64 too, but then we have system topologies where some devices may not be behind IOMMUs even when others are). More fun would be to try to bring it up at the first sign of IOMMU_DOMAIN_IDENTITY if it was disabled previously, but I don't have the highest hope of that being practical.

Robin.

#5 lore raw
Quoted 39 lines · Robin Murphy
Quoted 24 lines · Kai-Heng Feng
Quoted 23 lines · Robin Murphy
Quoted 12 lines · Joerg Roedel

drivers/iommu/amd/iommu.c static int iommu_init_device(struct device *dev) · line 349

Quoted 7 lines · Kai-Heng Feng
drivers/iommu/amd/iommu.c +3 -0
@@ -344,6 +344,9 @@ static int iommu_init_device(struct device *dev)
 
              iommu = amd_iommu_rlookup_table[dev_data->devid];
              dev_data->iommu_v2 = iommu->is_iommu_v2;
+
+            if (dev_data->iommu_v2)
+                    swiotlb = 1;

This looks like the big hammer, as it will affect all other systems where the AMD GPUs are in their own group.

Quoted 3 lines

What is needed here is an explicit check whether a non-iommu-v2 device is direct-mapped because it shares a group with the GPU, and only enable swiotlb in this case.

Right, it's basically about whether any DMA-limited device might at any time end up in an IOMMU_DOMAIN_IDENTITY domain. And given the possibility of device hotplug and the user being silly with the sysfs interface, I don't think we can categorically determine that at boot time.

Quoted 6 lines
Quoted 3 lines

Also note that Intel systems are likely to be similarly affected (in fact intel-iommu doesn't even have the iommu_default_passthough() check so it's probably even easier to blow up).

swiotlb is enabled by pci_swiotlb_detect_4gb() and intel-iommu doesn't disable it.

Oh, right... I did say I found this dance hard to follow. Clearly I shouldn't have trusted what I thought I remembered from looking at it yesterday :)

Also not helped by the fact that it sets iommu_detected which *does* disable SWIOTLB, but only on IA-64.

Quoted 8 lines
Quoted 1 line

I wonder if we can take the same approach in amd-iommu?

Certainly if there's a precedent for leaving SWIOTLB enabled even if it *might* be redundant, that seems like the easiest option (it's what we do on arm64 too, but then we have system topologies where some devices may not be behind IOMMUs even when others are). More fun would be to try to bring it up at the first sign of IOMMU_DOMAIN_IDENTITY if it was disabled previously, but I don't have the highest hope of that being practical.

<scratches his head>
It is kind of silly to enable SWIOTLB which will just eat 64MB of memory "just in case".

The SWIOTLB does have support to do late initialization (xen-pcifront does that for example - so if you add devices that can't do 64-bit it will allocate something like 4MB).

Would that be a better choice going forward - that is allocate this under those conditions?

Quoted 2 lines

Robin.

#6 lore raw
Quoted 54 lines · Konrad Rzeszutek Wilk
Quoted 39 lines · Robin Murphy
Quoted 24 lines · Kai-Heng Feng
Quoted 23 lines · Robin Murphy
Quoted 12 lines · Joerg Roedel

drivers/iommu/amd/iommu.c static int iommu_init_device(struct device *dev) · line 349

Quoted 7 lines · Kai-Heng Feng
drivers/iommu/amd/iommu.c +3 -0
@@ -344,6 +344,9 @@ static int iommu_init_device(struct device *dev)
 
              iommu = amd_iommu_rlookup_table[dev_data->devid];
              dev_data->iommu_v2 = iommu->is_iommu_v2;
+
+            if (dev_data->iommu_v2)
+                    swiotlb = 1;

This looks like the big hammer, as it will affect all other systems where the AMD GPUs are in their own group.

Quoted 3 lines

What is needed here is an explicit check whether a non-iommu-v2 device is direct-mapped because it shares a group with the GPU, and only enable swiotlb in this case.

Right, it's basically about whether any DMA-limited device might at any time end up in an IOMMU_DOMAIN_IDENTITY domain. And given the possibility of device hotplug and the user being silly with the sysfs interface, I don't think we can categorically determine that at boot time.

Quoted 6 lines
Quoted 3 lines

Also note that Intel systems are likely to be similarly affected (in fact intel-iommu doesn't even have the iommu_default_passthough() check so it's probably even easier to blow up).

swiotlb is enabled by pci_swiotlb_detect_4gb() and intel-iommu doesn't disable it.

Oh, right... I did say I found this dance hard to follow. Clearly I shouldn't have trusted what I thought I remembered from looking at it yesterday :)

Also not helped by the fact that it sets iommu_detected which *does* disable SWIOTLB, but only on IA-64.

Quoted 8 lines
Quoted 1 line

I wonder if we can take the same approach in amd-iommu?

Certainly if there's a precedent for leaving SWIOTLB enabled even if it *might* be redundant, that seems like the easiest option (it's what we do on arm64 too, but then we have system topologies where some devices may not be behind IOMMUs even when others are). More fun would be to try to bring it up at the first sign of IOMMU_DOMAIN_IDENTITY if it was disabled previously, but I don't have the highest hope of that being practical.

<scratches his head>
It is kind of silly to enable SWIOTLB which will just eat 64MB of memory "just in case".

Quoted 6 lines

The SWIOTLB does have support to do late initialization (xen-pcifront does that for example - so if you add devices that can't do 64-bit it will allocate something like 4MB).

Would that be a better choice going forward - that is allocate this under those conditions?

But how to practically do swiotlb late init on 32-bit capable devices? On the first DMA map requested by the driver?

Kai-Heng

Quoted 2 lines
Quoted 2 lines

Robin.

maintainer
#7 lore raw
In reply to Konrad Rzeszutek Wilk
Quoted 3 lines · Konrad Rzeszutek Wilk

The SWIOTLB does have support to do late initialization (xen-pcifront does that for example - so if you add devices that can't do 64-bit it will allocate something like 4MB).

That sounds like a way to evaluate. I suggest to allocate the SWIOTLB memory at boot and when the IOMMUs are initialized we re-evaluate what we ended up with and free the SWIOTLB memory if its not needed.

If that turns out to be wrong during runtime (e.g. because a device is switched to a passthrough default domain at runtime), we allocate a small aperture for this device like the above mentioned 4MB.

(A boot option to always keep the aperture around might also be helpful
for some setups)

Regards,

Joerg
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#8 lore raw
Quoted 6 lines · Joerg Roedel
Quoted 3 lines · Konrad Rzeszutek Wilk

The SWIOTLB does have support to do late initialization (xen-pcifront does that for example - so if you add devices that can't do 64-bit it will allocate something like 4MB).

Quoted 7 lines

That sounds like a way to evaluate. I suggest to allocate the SWIOTLB memory at boot and when the IOMMUs are initialized we re-evaluate what we ended up with and free the SWIOTLB memory if its not needed.

If that turns out to be wrong during runtime (e.g. because a device is switched to a passthrough default domain at runtime), we allocate a small aperture for this device like the above mentioned 4MB.

I am currently working on this but I found that 4MB is not enough, 16MB is the minimal size to make the device work.
How do I know the right SWIOTLB size for each device?

Quoted 3 lines

(A boot option to always keep the aperture around might also be helpful
for some setups)

OK, will also implement this in next iteration.

Kai-Heng

Quoted 4 lines

Regards,

Joerg