MT76x2U crashes XHCI driver on AMD Ryzen system
Subsystems (3)
I have an ALFA AWUS036ACM and whenever I plug it in, the XHCI driver dies. This is on an ASRock X370 motherboard. Kernel 4.20 Arch Linux.
After plugging in the adapter, the integrated wireless chip (Intel 3168) also dies. Disabling the motherboard wireless does not help. Here are two fairly similar logs. First with Intel 3168 and the second with it disabled in UEFI.
kernel: xhci_hcd 0000:03:00.0: xHCI host not responding to stop endpoint command.
kernel: xhci_hcd 0000:03:00.0: xHCI host controller not responding, assume dead
kernel: xhci_hcd 0000:03:00.0: HC died; cleaning up
kernel: usb 1-9: USB disconnect, device number 2
kernel: usb 1-14: USB disconnect, device number 3
bluetoothd[506]: Endpoint unregistered: sender=:1.151 path=/MediaEndpoint/A2DPSource
bluetoothd[506]: Endpoint unregistered: sender=:1.151 path=/MediaEndpoint/A2DPSink
systemd[1]: Starting Load/Save RF Kill Switch Status... NetworkManager[512]: <info> [1547157527.0255] bluez5: NAP: removed interface B0:35:9F:2D:73:1D
systemd[1]: Stopped target Bluetooth.
systemd[1]: Started Load/Save RF Kill Switch Status.
audit[1]: SERVICE_START pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-rfkill comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
kkuid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-rfkill comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-rfkill comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
kernel: audit: type=1131 audit(1547157532.035:57): pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-rfkill comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
kernel: iwlwifi 0000:27:00.0: iwl_pcie_cmdq_reclaim: Read index for DMA queue txq id (0), index 0 is out of range [0-256] 61 60. iwlwifi 0000:27:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000ff442000 flags=0x0020]
kernel: iwlwifi 0000:27:00.0: HCMD_ACTIVE already clear for command REPLY_SF_CFG_CMD
iwlwifi 0000:27:00.0: iwl_pcie_cmdq_reclaim: Read index for DMA queue txq id (0), index 0 is out of range [0-256] 61 60.
Folllowed by more iwlwifi errors as well as kernel crashdumps.
Without iwlwifi:
kernel: usb 1-14: new high-speed USB device number 3 using xhci_hcd
kernel: usb 1-14: New USB device found, idVendor=0e8d, idProduct=7612, bcdDevice= 1.00
kernel: usb 1-14: New USB device strings: Mfr=2, Product=3, SerialNumber=4 usb 1-14: Product: Wireless
kernel: usb 1-14: Manufacturer: MediaTek Inc.
kernel: usb 1-14: SerialNumber: 000000000
mtp-probe[1400]: checking bus 1, device 3:
"/sys/devices/pci0000:00/0000:00:01.3/0000:03:00.0/usb1/1-14" mtp-probe[1400]: bus: 1, device: 3 was not an MTP device
kernel: usb 1-14: reset high-speed USB device number 3 using xhci_hcd
kernel: mt76x2u 1-14:1.0: ASIC revision: 76120044
kernel: mt76x2u 1-14:1.0: ROM patch build: 20140408060640a
kernel: xhci_hcd 0000:03:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000f6d5c400 flags=0x0000]
kernel: mt76x2u 1-14:1.0: firmware upload timed out
kernel: xhci_hcd 0000:03:00.0: xHCI host not responding to stop endpoint command.
kernel: xhci_hcd 0000:03:00.0: xHCI host controller not responding, assume dead
kernel: xhci_hcd 0000:03:00.0: HC died; cleaning up
kernel: usb 1-9: USB disconnect, device number 2
bluetoothd[489]: Endpoint unregistered: sender=:1.150 path=/MediaEndpoint/A2DPSource
kernel: usb 1-14: USB disconnect, device number 3 NetworkManager[496]: <info> [1547157974.3360] bluez5: NAP: removed interface B0:35:9F:2D:73:1D
bluetoothd[489]: Endpoint unregistered: sender=:1.150 path=/MediaEndpoint/A2DPSink
systemd[1]: Starting Load/Save RF Kill Switch Status... systemd[1]: Stopped target Bluetooth.
systemd[1]: Started Load/Save RF Kill Switch Status.
audit[1]: SERVICE_START pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-rfkill comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
kernel: audit: type=1130 audit(1547157974.336:52): pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-rfkill comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-localed comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-hostnamed comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
kernel: audit: type=1131 audit(1547157977.826:53): pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-localed comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
audit: type=1131 audit(1547157977.826:54): pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-hostnamed comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-rfkill comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
kernel: audit: type=1131 audit(1547157979.346:55): pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=systemd-rfkill comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
kernel: mt76x2u 1-14:1.0: MAC RX failed to stop
kernel: mt76x2u: probe of 1-14:1.0 failed with error -5
kernel: usbcore: registered new interface driver mt76x2u geoclue[785]: Service not used for 60 seconds. Shutting down.. audit[1]: SERVICE_STOP pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=geoclue comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
kernel: audit: type=1131 audit(1547157990.969:56): pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=geoclue comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=success'
Quoted 9 lines
I have an ALFA AWUS036ACM and whenever I plug it in, the XHCI driver dies. This is on an ASRock X370 motherboard. Kernel 4.20 Arch Linux.
After plugging in the adapter, the integrated wireless chip (Intel 3168) also dies. Disabling the motherboard wireless does not help. Here are two fairly similar logs. First with Intel 3168 and the second with it disabled in UEFI.
[...]
Quoted 16 lines
Without iwlwifi:
kernel: usb 1-14: new high-speed USB device number 3 using
xhci_hcd
kernel: usb 1-14: New USB device found, idVendor=0e8d, idProduct=7612, bcdDevice= 1.00
kernel: usb 1-14: New USB device strings: Mfr=2, Product=3, SerialNumber=4 usb 1-14: Product: Wireless
kernel: usb 1-14: Manufacturer: MediaTek Inc.
kernel: usb 1-14: SerialNumber: 000000000
mtp-probe[1400]: checking bus 1, device 3:
"/sys/devices/pci0000:00/0000:00:01.3/0000:03:00.0/usb1/1-14" mtp-probe[1400]: bus: 1, device: 3 was not an MTP device
kernel: usb 1-14: reset high-speed USB device number 3 usingxhci_hcd
kernel: mt76x2u 1-14:1.0: ASIC revision: 76120044
kernel: mt76x2u 1-14:1.0: ROM patch build:20140408060640a
kernel:xhci_hcd0000:03:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT
Are you using the driver in a VM or directly in the host? This is an error reported by AMD iommu driver
Regards,
Lorenzo
Quoted 29 lines · Lorenzo Bianconi
Quoted 9 lines
I have an ALFA AWUS036ACM and whenever I plug it in, the XHCI driver dies. This is on an ASRock X370 motherboard. Kernel 4.20 Arch Linux.
After plugging in the adapter, the integrated wireless chip (Intel 3168) also dies. Disabling the motherboard wireless does not help. Here are two fairly similar logs. First with Intel 3168 and the second with it disabled in UEFI.
[...]
Quoted 16 lines
Without iwlwifi:
kernel: usb 1-14: new high-speed USB device number 3 using
xhci_hcd
kernel: usb 1-14: New USB device found, idVendor=0e8d, idProduct=7612, bcdDevice= 1.00
kernel: usb 1-14: New USB device strings: Mfr=2, Product=3, SerialNumber=4 usb 1-14: Product: Wireless
kernel: usb 1-14: Manufacturer: MediaTek Inc.
kernel: usb 1-14: SerialNumber: 000000000
mtp-probe[1400]: checking bus 1, device 3:
"/sys/devices/pci0000:00/0000:00:01.3/0000:03:00.0/usb1/1-14" mtp-probe[1400]: bus: 1, device: 3 was not an MTP device
kernel: usb 1-14: reset high-speed USB device number 3 usingxhci_hcd
kernel: mt76x2u 1-14:1.0: ASIC revision: 76120044
kernel: mt76x2u 1-14:1.0: ROM patch build:20140408060640a
kernel:xhci_hcd0000:03:00.0: AMD-Vi: Event logged [IO_PAGE_FAULT
Quoted 2 lines
Are you using the driver in a VM or directly in the host? This is an error reported by AMD iommu driver
Direct. No VM used. This is the only peripheral causing this issue.
Quoted 3 lines
Regards,
Lorenzo
Quoted 2 lines
Direct. No VM used. This is the only peripheral causing this issue.
Is the device connected to a usb3.0 port? If so, could you please try to connect the dongle to a 2.0 one?
Could you please double check if IOMMU is enabled?
Regards,
Lorenzo
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 10 lines · Lorenzo Bianconi
Direct. No VM used. This is the only peripheral causing this issue.
Is the device connected to a usb3.0 port? If so, could you please try to connect the dongle to a 2.0 one?
I tried through a USB 2.0 port. Shouldn't make a difference as they both use the xhci driver.
mt76x2u supports scatter-gather on usb 3.0 (not on 2.0)
Quoted 2 lines
Could you please double check if IOMMU is enabled?
Have you tried to disable it? Does it make any difference?
Regards,
Lorenzo
Quoted 8 lines
It is.
Note that this does not happen on kernel 4.15 (Ubuntu's kernel) as mt76 lacks support.
Regards, Lorenzo
Quoted 5 lines · Lorenzo Bianconi
Quoted 5 lines · Lorenzo Bianconi
Quoted 8 lines
Quoted 6 lines
Direct. No VM used. This is the only peripheral causing this issue.
Is the device connected to a usb3.0 port? If so, could you please try to connect the dongle to a 2.0 one?
I tried through a USB 2.0 port. Shouldn't make a difference as they both use the xhci driver.
mt76x2u supports scatter-gather on usb 3.0 (not on 2.0)
Tried a USB 3 port. Same result.
Quoted 5 lines
Quoted 3 lines
Could you please double check if IOMMU is enabled?
Have you tried to disable it? Does it make any difference?
No idea how. UEFI doesn't seem to show anything similar.
Similar bug report: https://bugzilla.kernel.org/show_bug.cgi?id=202241
Quoted 12 lines
Regards,
LorenzoQuoted 8 lines
It is.
Note that this does not happen on kernel 4.15 (Ubuntu's kernel) as mt76 lacks support.
Regards, Lorenzo
Quoted 9 lines · Rosen Penev
Quoted 4 lines · Lorenzo Bianconi
Quoted 2 lines
Could you please double check if IOMMU is enabled?
Have you tried to disable it? Does it make any difference?
No idea how. UEFI doesn't seem to show anything similar.
Similar bug report: https://bugzilla.kernel.org/show_bug.cgi?id=202241
As mentioned in that bug report, adding "iommu=off" to the kernel command line will disable it. I think there's also an "iommu=soft" option, but I'm not sure what that does.
Quoted 17 lines
Quoted 5 lines · Lorenzo Bianconi
Quoted 5 lines · Lorenzo Bianconi
Quoted 8 lines
Quoted 6 lines
Direct. No VM used. This is the only peripheral causing this issue.
Is the device connected to a usb3.0 port? If so, could you please try to connect the dongle to a 2.0 one?
I tried through a USB 2.0 port. Shouldn't make a difference as they both use the xhci driver.
mt76x2u supports scatter-gather on usb 3.0 (not on 2.0)
Tried a USB 3 port. Same result.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Could you please double check if IOMMU is enabled?
Have you tried to disable it? Does it make any difference?
No idea how. UEFI doesn't seem to show anything similar.
Similar bug report: https://bugzilla.kernel.org/show_bug.cgi?id=202241
You should be able to disable iommu using GRUB_CMDLINE_LINUX in /etc/default/grub (I guess setting iommu=off and reinstalling grub)
https://wiki.gentoo.org/wiki/IOMMU_SWIOTLB
Regards,
Lorenzo
Quoted 26 lines · Lorenzo Bianconi
Quoted 26 lines
Quoted 8 lines · Lorenzo Bianconi
Quoted 8 lines
On Sun, Jan 13, 2019 at 5:33 AM, Lorenzo Bianconi
<[email protected]> wrote:Direct. No VM used. This is the only peripheral causing this issue.
Quoted 8 lines
Quoted 6 lines
Is the device connected to a usb3.0 port? If so, could you please try to connect the dongle to a 2.0 one?
I tried through a USB 2.0 port. Shouldn't make a difference as they both use the xhci driver.
mt76x2u supports scatter-gather on usb 3.0 (not on 2.0)
Tried a USB 3 port. Same result.
Quoted 5 lines
Quoted 3 lines
Could you please double check if IOMMU is enabled?
Have you tried to disable it? Does it make any difference?
No idea how. UEFI doesn't seem to show anything similar.
Quoted 9 lines
Quoted 1 line
Similar bug report: https://bugzilla.kernel.org/show_bug.cgi?id=202241
You should be able to disable iommu using
GRUB_CMDLINE_LINUXin/etc/default/grub(I guess setting iommu=off and reinstalling grub)
https://wiki.gentoo.org/wiki/IOMMU_SWIOTLBRegards,
Lorenzo
updating grub, not reinstalling
Quoted 21 lines · Lorenzo Bianconi
Quoted 21 lines
Quoted 5 lines · Lorenzo Bianconi
Quoted 5 lines · Lorenzo Bianconi
Quoted 8 lines
Quoted 6 lines
Direct. No VM used. This is the only peripheral causing this issue.
Is the device connected to a usb3.0 port? If so, could you please try to connect the dongle to a 2.0 one?
I tried through a USB 2.0 port. Shouldn't make a difference as they both use the xhci driver.
mt76x2u supports scatter-gather on usb 3.0 (not on 2.0)
Tried a USB 3 port. Same result.
Quoted 4 lines
Quoted 3 lines
Could you please double check if IOMMU is enabled?
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Have you tried to disable it? Does it make any difference?
No idea how. UEFI doesn't seem to show anything similar.
Similar bug report: https://bugzilla.kernel.org/show_bug.cgi?id=202241
You should be able to disable iommu using
GRUB_CMDLINE_LINUXin/etc/default/grub(I guess setting iommu=off and reinstalling grub)
https://wiki.gentoo.org/wiki/IOMMU_SWIOTLB
Yep. Working great now. I wonder what mt76 is doing to cause the crash though...
Quoted 3 lines
Regards,
Lorenzo
Quoted 24 lines
Quoted 22 lines · Lorenzo Bianconi
Quoted 22 lines
Quoted 5 lines · Lorenzo Bianconi
Quoted 5 lines · Lorenzo Bianconi
Quoted 8 lines
Quoted 6 lines
Direct. No VM used. This is the only peripheral causing this issue.
Is the device connected to a usb3.0 port? If so, could you please try to connect the dongle to a 2.0 one?
I tried through a USB 2.0 port. Shouldn't make a difference as they both use the xhci driver.
mt76x2u supports scatter-gather on usb 3.0 (not on 2.0)
Tried a USB 3 port. Same result.
Quoted 5 lines
Quoted 3 lines
Could you please double check if IOMMU is enabled?
Have you tried to disable it? Does it make any difference?
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
No idea how. UEFI doesn't seem to show anything similar.
Similar bug report: https://bugzilla.kernel.org/show_bug.cgi?id=202241
You should be able to disable iommu using
GRUB_CMDLINE_LINUXin/etc/default/grub(I guess setting iommu=off and reinstalling grub)
https://wiki.gentoo.org/wiki/IOMMU_SWIOTLBYep. Working great now. I wonder what mt76 is doing to cause the crash though...
Thanks for bisecting the issue. I think amd iommu does not support well usb scatter-gather (used by default in mt76u). I am working on a series in order to add the possibility to disable it.
Regards,
Lorenzo
Quoted 3 lines
Quoted 3 lines
Regards,
Lorenzo
(cc: IOMMU & page_frag_alloc maintainers)
Quoted 20 lines · Lorenzo Bianconi
Quoted 20 lines
Quoted 18 lines · Lorenzo Bianconi
Quoted 18 lines
Quoted 5 lines · Lorenzo Bianconi
Quoted 5 lines · Lorenzo Bianconi
Quoted 8 lines
Quoted 6 lines
Direct. No VM used. This is the only peripheral causing this issue.
Is the device connected to a usb3.0 port? If so, could you please try to connect the dongle to a 2.0 one?
I tried through a USB 2.0 port. Shouldn't make a difference as they both use the xhci driver.
mt76x2u supports scatter-gather on usb 3.0 (not on 2.0)
Tried a USB 3 port. Same result.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Could you please double check if IOMMU is enabled?
Have you tried to disable it? Does it make any difference?
No idea how. UEFI doesn't seem to show anything similar.
Similar bug report: https://bugzilla.kernel.org/show_bug.cgi?id=202241
FWIW: I provided some patches in the bugzilla, which were reported to solve the problem. But I looking for confirmation if both are needed:
0001-mt76x02u-use-usb_bulk_msg-to-upload-firmware.patch 0002-mt76usb-do-not-use-compound-head-page-for-SG-I-O.patch
Or problem can be solved by just one of it (either first or second).
Additionally I'm not 100% sure if
0002-mt76usb-do-not-use-compound-head-page-for-SG-I-O.patch
is correct. So perhaps some IOMMU maintainer could look at it.
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
You should be able to disable iommu using
GRUB_CMDLINE_LINUXin/etc/default/grub(I guess setting iommu=off and reinstalling grub)
https://wiki.gentoo.org/wiki/IOMMU_SWIOTLBYep. Working great now. I wonder what mt76 is doing to cause the crash though...
Thanks for bisecting the issue.
Lorenzo, what you mean by 'bisecting' here ? Someone did 'git bisect' on this issue?
Quoted 3 lines
I think amd iommu does not support well usb scatter-gather
(used by default in mt76u). I am working on a series in order to add the possibility to disable it.
Even if that true that AMD IOMMU does not support 'well' SG (what I think is not true) disabling SG in mt76 driver is not right solution. Right solution would be propagate the issue to AMD IOMMU maintainers (already CCed).
One problem in mt76 is page_frag_alloc() usage with different sizes. page_frag_alloc() unlike like other allocators do not assure alignment and relay on callers to provide buffers sizes that are aligned. Unaligned buffer might then not be appropriate for DMA.
Another issue is that dma_map_sg() & dma_map_page() may require some constraints. I'm not sure about that and I want to clarify that with CCed mm maintainers. I think DMA drivers may expect sg->offset < PAGE_SIZE for both dma_map_sg() and dma_map_page(). Additionally dma_map_page() maight expect that offset & length specify buffer within one page.
Stanislaw
Quoted 48 lines
(cc: IOMMU &
page_frag_allocmaintainers)Quoted 20 lines · Lorenzo Bianconi
Quoted 20 lines
Quoted 18 lines · Lorenzo Bianconi
Quoted 18 lines
Quoted 5 lines · Lorenzo Bianconi
Quoted 5 lines · Lorenzo Bianconi
Quoted 8 lines
Quoted 6 lines
Direct. No VM used. This is the only peripheral causing this issue.
Is the device connected to a usb3.0 port? If so, could you please try to connect the dongle to a 2.0 one?
I tried through a USB 2.0 port. Shouldn't make a difference as they both use the xhci driver.
mt76x2u supports scatter-gather on usb 3.0 (not on 2.0)
Tried a USB 3 port. Same result.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Could you please double check if IOMMU is enabled?
Have you tried to disable it? Does it make any difference?
No idea how. UEFI doesn't seem to show anything similar.
Similar bug report: https://bugzilla.kernel.org/show_bug.cgi?id=202241
FWIW: I provided some patches in the bugzilla, which were reported to solve the problem. But I looking for confirmation if both are needed:
0001-mt76x02u-use-usb_bulk_msg-to-upload-firmware.patch 0002-mt76usb-do-not-use-compound-head-page-for-SG-I-O.patch
Or problem can be solved by just one of it (either first or second).
Additionally I'm not 100% sure if
0002-mt76usb-do-not-use-compound-head-page-for-SG-I-O.patch
is correct. So perhaps some IOMMU maintainer could look at it.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
You should be able to disable iommu using
GRUB_CMDLINE_LINUXin/etc/default/grub(I guess setting iommu=off and reinstalling grub)
https://wiki.gentoo.org/wiki/IOMMU_SWIOTLB
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Yep. Working great now. I wonder what mt76 is doing to cause the crash though...
Thanks for bisecting the issue.
Lorenzo, what you mean by 'bisecting' here ? Someone did 'git bisect' on this issue?
Hi Stanislaw,
I was meaning 'help bisecting' the issue
Quoted 8 lines
Quoted 3 lines
I think amd iommu does not support well usb scatter-gather
(used by default in mt76u). I am working on a series in order to add the possibility to disable it.Even if that true that AMD IOMMU does not support 'well' SG (what I think is not true) disabling SG in mt76 driver is not right solution. Right solution would be propagate the issue to AMD IOMMU maintainers (already CCed).
I meant that AMD iommu seems to have different constraints respect to Intel one.
Regards,
Lorenzo
Quoted 13 lines
One problem in mt76 is
page_frag_alloc()usage with different sizes.page_frag_alloc()unlike like other allocators do not assure alignment and relay on callers to provide buffers sizes that are aligned. Unaligned buffer might then not be appropriate for DMA.Another issue is that
dma_map_sg()&dma_map_page()may require some constraints. I'm not sure about that and I want to clarify that with CCed mm maintainers. I think DMA drivers may expectsg->offset<PAGE_SIZEfor bothdma_map_sg()anddma_map_page(). Additionallydma_map_page()maight expect that offset & length specify buffer within one page.Stanislaw
Robin Murphy
reviewerOn 18/02/2019 14:37, Stanislaw Gruszka wrote:
[...]
Quoted 5 lines
Another issue is that
dma_map_sg()&dma_map_page()may require some constraints. I'm not sure about that and I want to clarify that with CCed mm maintainers. I think DMA drivers may expectsg->offset<PAGE_SIZEfor bothdma_map_sg()anddma_map_page(). Additionallydma_map_page()maight expect that offset & length specify buffer within one page.
Luckily, this came up a while back[1] and we seemed to reach a consensus that sg->offset >= PAGE_SIZE for dma_map_sg() was weird but valid. IIRC it was only the Intel IOMMU code which failed to handle that case appropriately (and which I fixed) - the AMD IOMMU code always looked like it should be OK, but I'm not sure I've ever seen definitive test results (and I don't have hardware to do so myself).
For dma_map_page(), length >= PAGE_SIZE should be perfectly valid and handled correctly. The offset >= PAGE_SIZE case is a bit harder to justify, but at the same time has less scope for the DMA API backend to get it wrong, so either way is likely to be OK in practice (in particular the AMD IOMMU code looks like it won't have a problem, since its map_page() implementation converts page and offset to a plain physical address before doing anything else).
Robin.
[1] https://lists.linuxfoundation.org/pipermail/iommu/2017-September/024148.html
Quoted 8 lines · Robin Murphy
On 18/02/2019 14:37, Stanislaw Gruszka wrote:
[...]Quoted 5 lines
Another issue is that
dma_map_sg()&dma_map_page()may require some constraints. I'm not sure about that and I want to clarify that with CCed mm maintainers. I think DMA drivers may expectsg->offset<PAGE_SIZEfor bothdma_map_sg()anddma_map_page(). Additionallydma_map_page()maight expect that offset & length specify buffer within one page.
Quoted 7 lines
Luckily, this came up a while back[1] and we seemed to reach a consensus that
sg->offset>=PAGE_SIZEfordma_map_sg()was weird but valid. IIRC it was only the Intel IOMMU code which failed to handle that case appropriately (and which I fixed) - the AMD IOMMU code always looked like it should be OK, but I'm not sure I've ever seen definitive test results (and I don't have hardware to do so myself).
Funny that we have problems on AMD IOMMU and not with Intel IOMMU.
Quoted 7 lines
For
dma_map_page(), length >=PAGE_SIZEshould be perfectly valid and handled correctly. The offset >=PAGE_SIZEcase is a bit harder to justify, but at the same time has less scope for the DMA API backend to get it wrong, so either way is likely to be OK in practice (in particular the AMD IOMMU code looks like it won't have a problem, since itsmap_page()implementation converts page and offset to a plain physical address before doing anything else).
Thanks for clarify this. So my patch which do:
- page = virt_to_head_page(data);
+ page = virt_to_page(data);
offset = data - page_address(page); sg_set_page(&urb->sg[i], page, sglen, offset);
should not be necessary as IOMMU driver do exactly the same internally.
Are there any alignment requirement for offset for dma_map_{page,sg} ? It will work with let say sg->offset=113 or we have make sure it is aligned to some boundary. If so, what boundary ?
Stanislaw
Joerg Roedel
maintainerQuoted 10 lines · Stanislaw Gruszka
0001-mt76x02u-use-usb_bulk_msg-to-upload-firmware.patch 0002-mt76usb-do-not-use-compound-head-page-for-SG-I-O.patch
Or problem can be solved by just one of it (either first or second).
Additionally I'm not 100% sure if
0002-mt76usb-do-not-use-compound-head-page-for-SG-I-O.patch
is correct. So perhaps some IOMMU maintainer could look at it.
The patch looks good, but I don't understand why it is needed. The AMD IOMMU driver should handle sg->offset > PAGE_SIZE just fine. Can you verify that this is the problem? I will look into that again if it turns out there is bug in the IOMMU driver.
Regards,
Joerg
Quoted 8 lines · Joerg Roedel
Quoted 7 lines · Stanislaw Gruszka
0001-mt76x02u-use-usb_bulk_msg-to-upload-firmware.patch 0002-mt76usb-do-not-use-compound-head-page-for-SG-I-O.patch
Or problem can be solved by just one of it (either first or second).
Additionally I'm not 100% sure if
Quoted 8 lines
Quoted 3 lines
0002-mt76usb-do-not-use-compound-head-page-for-SG-I-O.patch
is correct. So perhaps some IOMMU maintainer could look at it.
The patch looks good, but I don't understand why it is needed. The AMD IOMMU driver should handle
sg->offset>PAGE_SIZEjust fine. Can you verify that this is the problem? I will look into that again if it turns out there is bug in the IOMMU driver.
I'm try to get that information from bug reporter, but I can't get it so far.
If sg->offset > PAGE_SIZE is fine then most likely we have problem with alignment. We use page_frag_alloc() in mt76usb for buffer allocation in scheme like this
page_frag_alloc(max_payload); // something like 14434page_frag_alloc(1024);page_frag_alloc(2048)page_frag_alloc(2048)page_frag_alloc(2048)
...
page_frag_alloc works smart and fast way internally by allocating fragments just but changing internal offset:
offset = nc->offset - fragsz;
if (unlikely(offset < 0)) {
page = virt_to_page(nc->va);
.
.
.
}
nc->offset = offset;
return nc->va + offset;but unlike other allocators like kmalloc that make effort to provide ARCH_DMA_MINALIGN buffers, it does not care about alignment. Above scheme of allocation in mt76usb breaks it.
Note hat issue is with dma_map_sg(), switching to dma_map_single() by using urb->transfer_buffer instead of urb->sg make things work on AMD IOMMU.
Stanislaw
Joerg Roedel
maintainerQuoted 3 lines · Stanislaw Gruszka
On Tue, Feb 26, 2019 at 11:05:36AM +0100, Joerg Roedel wrote:
Ifsg->offset>PAGE_SIZEis fine then most likely we have problem with alignment.
The map_sg implementation in the AMD IOMMU driver uses sg_phys() which handles the sg->page + sg->offset calculation fine.
Quoted 3 lines
Note hat issue is with
dma_map_sg(), switching todma_map_single()by usingurb->transfer_bufferinstead ofurb->sgmake things work on AMD IOMMU.
On the other hand this points to a bug in the driver, I'll look further if I can spot something there.
Regards,
Joerg
Quoted 8 lines · Joerg Roedel
Quoted 3 lines · Stanislaw Gruszka
On Tue, Feb 26, 2019 at 11:05:36AM +0100, Joerg Roedel wrote:
Ifsg->offset>PAGE_SIZEis fine then most likely we have problem with alignment.The
map_sgimplementation in the AMD IOMMU driver usessg_phys()which handles thesg->page+sg->offsetcalculation fine.
Quoted 6 lines
Quoted 3 lines
Note hat issue is with
dma_map_sg(), switching todma_map_single()by usingurb->transfer_bufferinstead ofurb->sgmake things work on AMD IOMMU.On the other hand this points to a bug in the driver, I'll look further if I can spot something there.
I think so too. And I have done some changes that avoid strange allocation scheme and use usb synchronous messages instead of allocating buffers with unaligned sizes. However things work ok on Intel IOMMU and there is no documentation what are dma_map_sg() requirement versus dma_map_single() which works. I think there are some unwritten requirements and things can work on some platforms and fails on others (different IOMMUs, no-IOMMU on some ARCHes)
Stanislaw
Quoted 16 lines · Stanislaw Gruszka
Quoted 8 lines · Joerg Roedel
Quoted 3 lines · Stanislaw Gruszka
On Tue, Feb 26, 2019 at 11:05:36AM +0100, Joerg Roedel wrote:
Ifsg->offset>PAGE_SIZEis fine then most likely we have problem with alignment.The
map_sgimplementation in the AMD IOMMU driver usessg_phys()which handles thesg->page+sg->offsetcalculation fine.Quoted 6 lines
Quoted 3 lines
Note hat issue is with
dma_map_sg(), switching todma_map_single()by usingurb->transfer_bufferinstead ofurb->sgmake things work on AMD IOMMU.On the other hand this points to a bug in the driver, I'll look further if I can spot something there.
Quoted 7 lines
I think so too. And I have done some changes that avoid strange allocation scheme and use usb synchronous messages instead of allocating buffers with unaligned sizes. However things work ok on Intel IOMMU and there is no documentation what are
dma_map_sg()requirement versusdma_map_single()which works. I think there are some unwritten requirements and things can work on some platforms and fails on others (different IOMMUs, no-IOMMU on some ARCHes)
For the record: we have another bug report with this issue:
https://bugzilla.kernel.org/show_bug.cgi?id=202673
I provided there patch that change alignment for page_frag_alloc() and it did not fixed the problem. So this is not alignment issue. Now I think it could be page->refcount issue ...
Stanislaw
Quoted 25 lines · Stanislaw Gruszka
Quoted 16 lines · Stanislaw Gruszka
Quoted 8 lines · Joerg Roedel
Quoted 3 lines · Stanislaw Gruszka
On Tue, Feb 26, 2019 at 11:05:36AM +0100, Joerg Roedel wrote:
Ifsg->offset>PAGE_SIZEis fine then most likely we have problem with alignment.The
map_sgimplementation in the AMD IOMMU driver usessg_phys()which handles thesg->page+sg->offsetcalculation fine.Quoted 6 lines
Quoted 3 lines
Note hat issue is with
dma_map_sg(), switching todma_map_single()by usingurb->transfer_bufferinstead ofurb->sgmake things work on AMD IOMMU.On the other hand this points to a bug in the driver, I'll look further if I can spot something there.
Quoted 7 lines
I think so too. And I have done some changes that avoid strange allocation scheme and use usb synchronous messages instead of allocating buffers with unaligned sizes. However things work ok on Intel IOMMU and there is no documentation what are
dma_map_sg()requirement versusdma_map_single()which works. I think there are some unwritten requirements and things can work on some platforms and fails on others (different IOMMUs, no-IOMMU on some ARCHes)
Quoted 6 lines
For the record: we have another bug report with this issue:
https://bugzilla.kernel.org/show_bug.cgi?id=202673I provided there patch that change alignment for
page_frag_alloc()and it did not fixed the problem. So this is not alignment issue. Now I think it could bepage->refcountissue ...
I looked at the map_sg() in amd_iommu.c code and one line looks suspicious to me, seems we can use not correctly initialized s->dma_address (should be 0, but I think can be non-zero if SG was reused). The code also seems do not do correct thing if there is more than one SG with multiple pages on individual segments. Something like in below patch seems to be more appropriate to me (not tested nor compiled).
Stanislaw
drivers/iommu/amd_iommu.c +4 -1
@@ -2517,6 +2517,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist, prot =dir2prot (direction);/* Map all sg entries */ + npages =0 ;for_each_sg (sglist, s, nelems, i) {int j, pages =iommu_num_pages (sg_phys (s), s->length,PAGE_SIZE );
@@ -2524,7 +2525,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,unsigned long bus_addr, phys_addr;int ret; - bus_addr = address + s->dma_address + (j <<PAGE_SHIFT ); + bus_addr = address + ((npages + j) <<PAGE_SHIFT ); phys_addr = (sg_phys (s) &PAGE_MASK ) + (j <<PAGE_SHIFT ); ret =iommu_map_page (domain, bus_addr, phys_addr,PAGE_SIZE , prot,GFP_ATOMIC );if (ret)
@@ -2532,6 +2533,8 @@ static int map_sg(struct device *dev, struct scatterlist *sglist, mapped_pages +=1 ; } + + npages += mapped_pages; }/* Everything is mapped - write the right values into s->dma_address */
Quoted 33 lines · Stanislaw Gruszka
Quoted 25 lines · Stanislaw Gruszka
Quoted 16 lines · Stanislaw Gruszka
Quoted 8 lines · Joerg Roedel
Quoted 3 lines · Stanislaw Gruszka
On Tue, Feb 26, 2019 at 11:05:36AM +0100, Joerg Roedel wrote:
Ifsg->offset>PAGE_SIZEis fine then most likely we have problem with alignment.The
map_sgimplementation in the AMD IOMMU driver usessg_phys()which handles thesg->page+sg->offsetcalculation fine.Quoted 6 lines
Quoted 3 lines
Note hat issue is with
dma_map_sg(), switching todma_map_single()by usingurb->transfer_bufferinstead ofurb->sgmake things work on AMD IOMMU.On the other hand this points to a bug in the driver, I'll look further if I can spot something there.
Quoted 7 lines
I think so too. And I have done some changes that avoid strange allocation scheme and use usb synchronous messages instead of allocating buffers with unaligned sizes. However things work ok on Intel IOMMU and there is no documentation what are
dma_map_sg()requirement versusdma_map_single()which works. I think there are some unwritten requirements and things can work on some platforms and fails on others (different IOMMUs, no-IOMMU on some ARCHes)Quoted 6 lines
For the record: we have another bug report with this issue:
https://bugzilla.kernel.org/show_bug.cgi?id=202673I provided there patch that change alignment for
page_frag_alloc()and it did not fixed the problem. So this is not alignment issue. Now I think it could bepage->refcountissue ...
Quoted 6 lines
I looked at the
map_sg()inamd_iommu.ccode and one line looks suspicious to me, seems we can use not correctly initializeds->dma_address(should be 0, but I think can be non-zero if SG was reused). The code also seems do not do correct thing if there is more than one SG with multiple pages on individual segments. Something like in below patch seems to be more appropriate to me (not tested nor compiled).
Nevermind, the patch is wrong, s->dma_address is initalized in sg_num_pages().
Stanislaw
Joerg Roedel
maintainerQuoted 1 line · Stanislaw Gruszka
Nevermind, the patch is wrong,
s->dma_addressis initalized insg_num_pages().
Yes, it is. In sg_num_pages() the offset into the IOMMU mapping is stored in s->dma_address, taking also the segment boundary mask into account. map_sg() later only adds the base-address to that.
Regards,
Joerg
Quoted 6 lines · Joerg Roedel
Quoted 1 line · Stanislaw Gruszka
Nevermind, the patch is wrong,
s->dma_addressis initalized insg_num_pages().Yes, it is. In
sg_num_pages()the offset into the IOMMU mapping is stored ins->dma_address, taking also the segment boundary mask into account.map_sg()later only adds the base-address to that.
I have some more info about the issues in
https://bugzilla.kernel.org/show_bug.cgi?id=202673
We have some bugs in mt76. Apparently we should not use page_frag_alloc() with size bigger than PAGE_SIZE as page_frag_alloc() can fallback to single page allocation. And also we should not make sizes unaligned as pointed in commit:3bed3cc4156e ("net: Do not allocate page fragments that are not skb aligned"
However after fixing that mt76usb still did not work. To make things work we had to change rx frag size from 2048 to PAGE_SIZE and change virt_to_head_page() to virt_to_page() when setting SG's.
I think I understand why first change was needed. If we do 2 separate dma maps of 2 different buffers in single page i.e (PAGE + off=0 and PAGE + off=2048) it causes problem. So either map_sg() return error which mt76usb does not handle correctly or there is issue in AMD IOMMU because two dma maps use the same page.
But I don't understand why the second change was needed. Without it we have issue with incorrect page->_refcount . It is somehow related with AMD IOMMU, because on different platforms we do not have such problems.
Joerg, could you look at this ? Thanks.
Stanislaw
Quoted 9 lines · Stanislaw Gruszka
Quoted 6 lines · Joerg Roedel
Quoted 1 line · Stanislaw Gruszka
Nevermind, the patch is wrong,
s->dma_addressis initalized insg_num_pages().Yes, it is. In
sg_num_pages()the offset into the IOMMU mapping is stored ins->dma_address, taking also the segment boundary mask into account.map_sg()later only adds the base-address to that.
Quoted 8 lines
I have some more info about the issues in
https://bugzilla.kernel.org/show_bug.cgi?id=202673We have some bugs in mt76. Apparently we should not use
page_frag_alloc()with size bigger thanPAGE_SIZEaspage_frag_alloc()can fallback to single page allocation. And also we should not make sizes unaligned as pointed in commit:3bed3cc4156e("net: Do not allocate page fragments that are not skb aligned"
As a small and totally unrelated note, page_frag_alloc is only used in mt76 and the nvme driver ;)
Quoted 19 lines
However after fixing that mt76usb still did not work. To make things work we had to change rx frag size from 2048 to
PAGE_SIZEand changevirt_to_head_page()tovirt_to_page()when setting SG's.I think I understand why first change was needed. If we do 2 separate dma maps of 2 different buffers in single page i.e (PAGE + off=0 and PAGE + off=2048) it causes problem. So either
map_sg()return error which mt76usb does not handle correctly or there is issue in AMD IOMMU because two dma maps use the same page.But I don't understand why the second change was needed. Without it we have issue with incorrect
page->_refcount. It is somehow related with AMD IOMMU, because on different platforms we do not have such problems.Joerg, could you look at this ? Thanks.
Stanislaw
Quoted 13 lines · Rosen Penev
Quoted 12 lines · Stanislaw Gruszka
Quoted 6 lines · Joerg Roedel
Quoted 1 line · Stanislaw Gruszka
Nevermind, the patch is wrong,
s->dma_addressis initalized insg_num_pages().Yes, it is. In
sg_num_pages()the offset into the IOMMU mapping is stored ins->dma_address, taking also the segment boundary mask into account.map_sg()later only adds the base-address to that.I have some more info about the issues in
https://bugzilla.kernel.org/show_bug.cgi?id=202673
Quoted 7 lines
Quoted 5 lines
We have some bugs in mt76. Apparently we should not use
page_frag_alloc()with size bigger thanPAGE_SIZEaspage_frag_alloc()can fallback to single page allocation. And also we should not make sizes unaligned as pointed in commit:3bed3cc4156e("net: Do not allocate page fragments that are not skb aligned"As a small and totally unrelated note,
page_frag_allocis only used in mt76 and the nvme driver ;)
And there is nvme problem on AMD IOMMU:
https://bugzilla.kernel.org/show_bug.cgi?id=202665
While page_frag_alloc() should be used with cautious, at least care of size alignment with ARCH_DMA_MINALIGN (not for IOMMU, but standard arch dma), at this point I think some of those issues are AMD IOMMU problems.
Quoted 9 lines
Quoted 9 lines
However after fixing that mt76usb still did not work. To make things work we had to change rx frag size from 2048 to
PAGE_SIZEand changevirt_to_head_page()tovirt_to_page()when setting SG's.I think I understand why first change was needed. If we do 2 separate dma maps of 2 different buffers in single page i.e (PAGE + off=0 and PAGE + off=2048) it causes problem. So either
map_sg()return error which mt76usb does not handle correctly or there is issue in AMD IOMMU because two dma maps use the same page.
Any comment on that? Is fine or not to do 2 or more dma mappings within the same single page on AMD IOMMU? If not, is there any mechanism for drivers to find out about this limitation to prevent to prepare wrong SG buffers?
Quoted 4 lines
Quoted 4 lines
But I don't understand why the second change was needed. Without it we have issue with incorrect
page->_refcount. It is somehow related with AMD IOMMU, because on different platforms we do not have such problems.
I think I found a bug in amd iommu code when setting sg->dma_address with sg->offset > PAGE_SIZE. Will post fix shortly.
Stanislaw
PATCH 1/1 iommu/amd: fix sg->dma_address for sg->offset bigger than PAGE_SIZE
Take into account that sg->offset can be bigger than PAGE_SIZE when setting segment sg->dma_address. Otherwise sg->dma_address will point at diffrent page, what makes DMA not possible with erros like this:
xhci_hcd 0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000fdaa70c0 flags=0x0020] xhci_hcd 0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000fdaa7040 flags=0x0020] xhci_hcd 0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000fdaa7080 flags=0x0020] xhci_hcd 0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000fdaa7100 flags=0x0020] xhci_hcd 0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000fdaa7000 flags=0x0020]
Additinally with wrong sg->dma_address unmap_sg will free wrong pages, what what can cause crashes like this:
Feb 28 19:27:45 kernel: BUG: Bad page state in process cinnamon pfn:39e8b1 Feb 28 19:27:45 kernel: Disabling lock debugging due to kernel taint Feb 28 19:27:45 kernel: flags: 0x2ffff0000000000() Feb 28 19:27:45 kernel: raw: 02ffff0000000000 0000000000000000 ffffffff00000301 0000000000000000 Feb 28 19:27:45 kernel: raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000 Feb 28 19:27:45 kernel: page dumped because: nonzero _refcount Feb 28 19:27:45 kernel: Modules linked in: ccm fuse arc4 nct6775 hwmon_vid amdgpu nls_iso8859_1 nls_cp437 edac_mce_amd vfat fat kvm_amd ccp rng_core kvm mt76x0u mt76x0_common mt76x02_usb irqbypass mt76_usb mt76x02_lib mt76 crct10dif_pclmul crc32_pclmul chash mac80211 amd_iommu_v2 ghash_clmulni_intel gpu_sched i2c_algo_bit ttm wmi_bmof snd_hda_codec_realtek snd_hda_codec_generic drm_kms_helper snd_hda_codec_hdmi snd_hda_intel drm snd_hda_codec aesni_intel snd_hda_core snd_hwdep aes_x86_64 crypto_simd snd_pcm cfg80211 cryptd mousedev snd_timer glue_helper pcspkr r8169 input_leds realtek agpgart libphy rfkill snd syscopyarea sysfillrect sysimgblt fb_sys_fops soundcore sp5100_tco k10temp i2c_piix4 wmi evdev gpio_amdpt pinctrl_amd mac_hid pcc_cpufreq acpi_cpufreq sg ip_tables x_tables ext4(E) c rc32c_generic(E) crc16(E) mbcache(E) jbd2(E) fscrypto(E) sd_mod(E) hid_generic(E) usbhid(E) hid(E) dm_mod(E) serio_raw(E) atkbd(E) libps2(E) crc32c_intel(E) ahci(E) libahci(E) libata(E) xhci_pci(E) xhci_hcd(E) Feb 28 19:27:45 kernel: scsi_mod(E) i8042(E) serio(E) bcache(E) crc64(E) Feb 28 19:27:45 kernel: CPU: 2 PID: 896 Comm: cinnamon Tainted: G B W E 4.20.12-arch1-1-custom #1 Feb 28 19:27:45 kernel: Hardware name: To Be Filled By O.E.M. To Be Filled By O.E.M./B450M Pro4, BIOS P1.20 06/26/2018 Feb 28 19:27:45 kernel: Call Trace: Feb 28 19:27:45 kernel: dump_stack+0x5c/0x80 Feb 28 19:27:45 kernel: bad_page.cold.29+0x7f/0xb2 Feb 28 19:27:45 kernel: __free_pages_ok+0x2c0/0x2d0 Feb 28 19:27:45 kernel: skb_release_data+0x96/0x180 Feb 28 19:27:45 kernel: __kfree_skb+0xe/0x20 Feb 28 19:27:45 kernel: tcp_recvmsg+0x894/0xc60 Feb 28 19:27:45 kernel: ? reuse_swap_page+0x120/0x340 Feb 28 19:27:45 kernel: ? ptep_set_access_flags+0x23/0x30 Feb 28 19:27:45 kernel: inet_recvmsg+0x5b/0x100 Feb 28 19:27:45 kernel: __sys_recvfrom+0xc3/0x180 Feb 28 19:27:45 kernel: ? handle_mm_fault+0x10a/0x250 Feb 28 19:27:45 kernel: ? syscall_trace_enter+0x1d3/0x2d0 Feb 28 19:27:45 kernel: ? __audit_syscall_exit+0x22a/0x290 Feb 28 19:27:45 kernel: __x64_sys_recvfrom+0x24/0x30 Feb 28 19:27:45 kernel: do_syscall_64+0x5b/0x170 Feb 28 19:27:45 kernel: entry_SYSCALL_64_after_hwframe+0x44/0xa9
--- drivers/iommu/amd_iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/amd_iommu.c +1 -1
@@ -2604,7 +2604,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,/* Everything is mapped - write the right values into s->dma_address */ for_each_sg (sglist, s, nelems, i) { - s->dma_address += address + s->offset; + s->dma_address += address + (s->offset & ~PAGE_MASK ); s->dma_length = s->length; }
Signature · 1 line
2.7.5
Re: [PATCH] iommu/amd: fix sg->dma_address for sg->offset bigger than PAGE_SIZE
Quoted 13 lines · Stanislaw Gruszka
Take into account that
sg->offsetcan be bigger thanPAGE_SIZEwhen setting segmentsg->dma_address. Otherwisesg->dma_addresswill point at diffrent page, what makes DMA not possible with erros like this:
xhci_hcd0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULTdomain=0x0000 address=0x00000000fdaa70c0 flags=0x0020]xhci_hcd0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULTdomain=0x0000 address=0x00000000fdaa7040 flags=0x0020]xhci_hcd0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULTdomain=0x0000 address=0x00000000fdaa7080 flags=0x0020]xhci_hcd0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULTdomain=0x0000 address=0x00000000fdaa7100 flags=0x0020]xhci_hcd0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULTdomain=0x0000 address=0x00000000fdaa7000 flags=0x0020]Additinally with wrong
sg->dma_addressunmap_sgwill free wrong pages, what what can cause crashes like this:
Quoted 7 lines
Feb 28 19:27:45 kernel: BUG: Bad page state in process cinnamon pfn:39e8b1 Feb 28 19:27:45 kernel: Disabling lock debugging due to kernel taint Feb 28 19:27:45 kernel: flags: 0x2ffff0000000000() Feb 28 19:27:45 kernel: raw: 02ffff0000000000 0000000000000000 ffffffff00000301 0000000000000000 Feb 28 19:27:45 kernel: raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000 Feb 28 19:27:45 kernel: page dumped because: nonzero _refcount Feb 28 19:27:45 kernel: Modules linked in: ccm fuse arc4 nct6775 hwmon_vid amdgpu nls_iso8859_1 nls_cp437 edac_mce_amd vfat fat kvm_amd ccp rng_core kvm mt76x0u mt76x0_common mt76x02_usb irqbypass mt76_usb mt76x02_lib mt76 crct10dif_pclmul crc32_pclmul chash mac80211 amd_iommu_v2 ghash_clmulni_intel gpu_sched i2c_algo_bit ttm wmi_bmof snd_hda_codec_realtek snd_hda_codec_generic drm_kms_helper snd_hda_codec_hdmi snd_hda_intel drm snd_hda_codec aesni_intel snd_hda_core snd_hwdep aes_x86_64 crypto_simd snd_pcm cfg80211 cryptd mousedev snd_timer glue_helper pcspkr r8169 input_leds realtek agpgart libphy rfkill snd syscopyarea sysfillrect sysimgblt fb_sys_fops soundcore sp5100_tco k10temp i2c_piix4 wmi evdev gpio_amdpt pinctrl_amd mac_hid pcc_cpufreq acpi_cpufreq sg ip_tables x_tables ext4(E)
crc32c_generic(E) crc16(E) mbcache(E) jbd2(E) fscrypto(E) sd_mod(E) hid_generic(E) usbhid(E) hid(E) dm_mod(E) serio_raw(E) atkbd(E) libps2(E) crc32c_intel(E) ahci(E) libahci(E) libata(E) xhci_pci(E) xhci_hcd(E)
Quoted 33 lines
Feb 28 19:27:45 kernel: scsi_mod(E) i8042(E) serio(E) bcache(E) crc64(E) Feb 28 19:27:45 kernel: CPU: 2 PID: 896 Comm: cinnamon Tainted: G B W E 4.20.12-arch1-1-custom #1 Feb 28 19:27:45 kernel: Hardware name: To Be Filled By O.E.M. To Be Filled By O.E.M./B450M Pro4, BIOS P1.20 06/26/2018 Feb 28 19:27:45 kernel: Call Trace: Feb 28 19:27:45 kernel: dump_stack+0x5c/0x80 Feb 28 19:27:45 kernel: bad_page.cold.29+0x7f/0xb2 Feb 28 19:27:45 kernel: __free_pages_ok+0x2c0/0x2d0 Feb 28 19:27:45 kernel: skb_release_data+0x96/0x180 Feb 28 19:27:45 kernel: __kfree_skb+0xe/0x20 Feb 28 19:27:45 kernel: tcp_recvmsg+0x894/0xc60 Feb 28 19:27:45 kernel: ? reuse_swap_page+0x120/0x340 Feb 28 19:27:45 kernel: ? ptep_set_access_flags+0x23/0x30 Feb 28 19:27:45 kernel: inet_recvmsg+0x5b/0x100 Feb 28 19:27:45 kernel: __sys_recvfrom+0xc3/0x180 Feb 28 19:27:45 kernel: ? handle_mm_fault+0x10a/0x250 Feb 28 19:27:45 kernel: ? syscall_trace_enter+0x1d3/0x2d0 Feb 28 19:27:45 kernel: ? __audit_syscall_exit+0x22a/0x290 Feb 28 19:27:45 kernel: __x64_sys_recvfrom+0x24/0x30 Feb 28 19:27:45 kernel: do_syscall_64+0x5b/0x170 Feb 28 19:27:45 kernel: entry_SYSCALL_64_after_hwframe+0x44/0xa9Reported-and-tested-by [email protected]Signed-off-by Stanislaw Gruszka <[email protected]>--- drivers/iommu/amd_iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iommu/amd_iommu.c +0
-0
drivers/iommu/amd_iommu.c static int map_sg(struct device *dev, struct scatterlist *sglist, · line 2610
Quoted 8 lines
drivers/iommu/amd_iommu.c +1
-1@@ -2604,7 +2604,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,/* Everything is mapped - write the right values into s->dma_address */ for_each_sg (sglist, s, nelems, i) { - s->dma_address += address + s->offset; + s->dma_address += address + (s->offset & ~PAGE_MASK ); s->dma_length = s->length; }
You should add a comment calling out that this is needed because the sg_phys(s) call above this is masked with PAGE_MASK. Then this makes much more sense. Otherwise I would have assumed you needed either the full offset or none.
Other than that, from that I can tell the code itself looks to be correct, but just difficult to read.
Re: [PATCH] iommu/amd: fix sg->dma_address for sg->offset bigger than PAGE_SIZE
Quoted 9 lines · Alexander Duyck
Quoted 9 lines
drivers/iommu/amd_iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iommu/amd_iommu.c +0
-0@@ -2604,7 +2604,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,
Quoted 12 lines
drivers/iommu/amd_iommu.c static int map_sg(struct device *dev, struct scatterlist *sglist, · line 2610
Quoted 7 lines
drivers/iommu/amd_iommu.c +1
-1/* Everything is mapped - write the right values into s->dma_address */ for_each_sg (sglist, s, nelems, i) { - s->dma_address += address + s->offset; + s->dma_address += address + (s->offset & ~PAGE_MASK ); s->dma_length = s->length; }You should add a comment calling out that this is needed because the
sg_phys(s)call above this is masked withPAGE_MASK. Then this makes much more sense. Otherwise I would have assumed you needed either the full offset or none.
Would something like this
/*
* Everything is mapped - write the right values into s->dma_address.
* Take into account s->offset can be bigger than page size and sg_phys(s)
* address has to be aligned to page granularity.
*/
be appropriate ?
Stanislaw
Re: [PATCH] iommu/amd: fix sg->dma_address for sg->offset bigger than PAGE_SIZE
Quoted 31 lines · Stanislaw Gruszka
Quoted 9 lines · Alexander Duyck
Quoted 9 lines
drivers/iommu/amd_iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/iommu/amd_iommu.c +0
-0@@ -2604,7 +2604,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,Quoted 12 lines
drivers/iommu/amd_iommu.c static int map_sg(struct device *dev, struct scatterlist *sglist, · line 2610
Quoted 7 lines
drivers/iommu/amd_iommu.c +1
-1/* Everything is mapped - write the right values into s->dma_address */ for_each_sg (sglist, s, nelems, i) { - s->dma_address += address + s->offset; + s->dma_address += address + (s->offset & ~PAGE_MASK ); s->dma_length = s->length; }You should add a comment calling out that this is needed because the
sg_phys(s)call above this is masked withPAGE_MASK. Then this makes much more sense. Otherwise I would have assumed you needed either the full offset or none.Would something like this
/*
* Everything is mapped - write the right values intos->dma_address.
* Take into accounts->offsetcan be bigger than page size andsg_phys(s)
* address has to be aligned to page granularity.
*/
Quoted 4 lines
be appropriate ?
Stanislaw
No, that isn't a good description. If you take a look at the code a few lines up you find:
phys_addr = (sg_phys(s) & PAGE_MASK) + (j << PAGE_SHIFT);
Now if I am not mistaken the whole reason why you are having to make the change here is because the application of PAGE_MASK in this line. Basically what sg_phys() will do is take the address of the page, convert it to a physical address and add the offset. However what the mask is doing is limiting how much of that offset can be added. As a result you have to add the remainder that was masked out. So maybe a better comment would be something like:
/*
* Add in the remaining piece of the scatter-gather offset that was
* masked out when we were determining the physical address via
* (sg_phys(s) & PAGE_MASK) earlier.
*/
PATCH v2 1/1 iommu/amd: fix sg->dma_address for sg->offset bigger than PAGE_SIZE
Take into account that sg->offset can be bigger than PAGE_SIZE when setting segment sg->dma_address. Otherwise sg->dma_address will point at diffrent page, what makes DMA not possible with erros like this:
xhci_hcd 0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000fdaa70c0 flags=0x0020] xhci_hcd 0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000fdaa7040 flags=0x0020] xhci_hcd 0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000fdaa7080 flags=0x0020] xhci_hcd 0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000fdaa7100 flags=0x0020] xhci_hcd 0000:38:00.3: AMD-Vi: Event logged [IO_PAGE_FAULT domain=0x0000 address=0x00000000fdaa7000 flags=0x0020]
Additinally with wrong sg->dma_address unmap_sg will free wrong pages, what what can cause crashes like this:
Feb 28 19:27:45 kernel: BUG: Bad page state in process cinnamon pfn:39e8b1 Feb 28 19:27:45 kernel: Disabling lock debugging due to kernel taint Feb 28 19:27:45 kernel: flags: 0x2ffff0000000000() Feb 28 19:27:45 kernel: raw: 02ffff0000000000 0000000000000000 ffffffff00000301 0000000000000000 Feb 28 19:27:45 kernel: raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000 Feb 28 19:27:45 kernel: page dumped because: nonzero _refcount Feb 28 19:27:45 kernel: Modules linked in: ccm fuse arc4 nct6775 hwmon_vid amdgpu nls_iso8859_1 nls_cp437 edac_mce_amd vfat fat kvm_amd ccp rng_core kvm mt76x0u mt76x0_common mt76x02_usb irqbypass mt76_usb mt76x02_lib mt76 crct10dif_pclmul crc32_pclmul chash mac80211 amd_iommu_v2 ghash_clmulni_intel gpu_sched i2c_algo_bit ttm wmi_bmof snd_hda_codec_realtek snd_hda_codec_generic drm_kms_helper snd_hda_codec_hdmi snd_hda_intel drm snd_hda_codec aesni_intel snd_hda_core snd_hwdep aes_x86_64 crypto_simd snd_pcm cfg80211 cryptd mousedev snd_timer glue_helper pcspkr r8169 input_leds realtek agpgart libphy rfkill snd syscopyarea sysfillrect sysimgblt fb_sys_fops soundcore sp5100_tco k10temp i2c_piix4 wmi evdev gpio_amdpt pinctrl_amd mac_hid pcc_cpufreq acpi_cpufreq sg ip_tables x_tables ext4(E) c rc32c_generic(E) crc16(E) mbcache(E) jbd2(E) fscrypto(E) sd_mod(E) hid_generic(E) usbhid(E) hid(E) dm_mod(E) serio_raw(E) atkbd(E) libps2(E) crc32c_intel(E) ahci(E) libahci(E) libata(E) xhci_pci(E) xhci_hcd(E) Feb 28 19:27:45 kernel: scsi_mod(E) i8042(E) serio(E) bcache(E) crc64(E) Feb 28 19:27:45 kernel: CPU: 2 PID: 896 Comm: cinnamon Tainted: G B W E 4.20.12-arch1-1-custom #1 Feb 28 19:27:45 kernel: Hardware name: To Be Filled By O.E.M. To Be Filled By O.E.M./B450M Pro4, BIOS P1.20 06/26/2018 Feb 28 19:27:45 kernel: Call Trace: Feb 28 19:27:45 kernel: dump_stack+0x5c/0x80 Feb 28 19:27:45 kernel: bad_page.cold.29+0x7f/0xb2 Feb 28 19:27:45 kernel: __free_pages_ok+0x2c0/0x2d0 Feb 28 19:27:45 kernel: skb_release_data+0x96/0x180 Feb 28 19:27:45 kernel: __kfree_skb+0xe/0x20 Feb 28 19:27:45 kernel: tcp_recvmsg+0x894/0xc60 Feb 28 19:27:45 kernel: ? reuse_swap_page+0x120/0x340 Feb 28 19:27:45 kernel: ? ptep_set_access_flags+0x23/0x30 Feb 28 19:27:45 kernel: inet_recvmsg+0x5b/0x100 Feb 28 19:27:45 kernel: __sys_recvfrom+0xc3/0x180 Feb 28 19:27:45 kernel: ? handle_mm_fault+0x10a/0x250 Feb 28 19:27:45 kernel: ? syscall_trace_enter+0x1d3/0x2d0 Feb 28 19:27:45 kernel: ? __audit_syscall_exit+0x22a/0x290 Feb 28 19:27:45 kernel: __x64_sys_recvfrom+0x24/0x30 Feb 28 19:27:45 kernel: do_syscall_64+0x5b/0x170 Feb 28 19:27:45 kernel: entry_SYSCALL_64_after_hwframe+0x44/0xa9
---
v2: add comment
drivers/iommu/amd_iommu.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/iommu/amd_iommu.c +5 -1
@@ -2604,7 +2604,11 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,/* Everything is mapped - write the right values into s->dma_address */ for_each_sg (sglist, s, nelems, i) { - s->dma_address += address + s->offset; +/* Add in the remaining piece of the scatter-gather offset that +* was masked out when we were determining the physical address +* via (sg_phys(s) & PAGE_MASK) earlier. +*/ + s->dma_address += address + (s->offset & ~PAGE_MASK ); s->dma_length = s->length; }
Signature · 1 line
2.7.5
Joerg Roedel
maintainerRe: [PATCH v2] iommu/amd: fix sg->dma_address for sg->offset bigger than PAGE_SIZE
Hi Stanislaw,
thanks a lot for looking into this and tracking it down!
drivers/iommu/amd_iommu.c static int map_sg(struct device *dev, struct scatterlist *sglist, · line 2612
Quoted 7 lines · Stanislaw Gruszka
drivers/iommu/amd_iommu.c +5
-1- s->dma_address += address + s->offset; +/* Add in the remaining piece of the scatter-gather offset that +* was masked out when we were determining the physical address +* via (sg_phys(s) & PAGE_MASK) earlier. +*/ + s->dma_address += address + (s->offset & ~PAGE_MASK ); s->dma_length = s->length;
Applied the patch for v5.1 (with an added Fixes-tag and a minor coding style change) and will send it upstream soon.
Thanks again,
Joerg
A message that is not in the archive (sent off-list or not yet received): [email protected]
Re: [PATCH v2] iommu/amd: fix sg->dma_address for sg->offset bigger than PAGE_SIZE
Quoted 14 lines · Sasha Levin
Hi,
[This is an automated email]
This commit has been processed because it contains a -stable tag. The stable tag indicates that it's relevant for the following trees: all
The bot has tested the following trees: v5.0.3, v4.19.30, v4.14.107, v4.9.164, v4.4.176, v3.18.136.
v5.0.3: Build OK!
v4.19.30: Build OK!
v4.14.107: Build OK!
v4.9.164: Build OK!
v4.4.176: Failed to apply! Possible dependencies:
<snip>
Quoted 1 line
v3.18.136: Failed to apply! Possible dependencies:
<snip>
Quoted 1 line
How should we proceed with this patch?
I've haven't looked into 4.4 and 3.18 amd_iommu code, but not counting comment, patch is one-liner, so should be trivial to backport it manually.
Stanislaw
Quoted 10 lines · Stanislaw Gruszka
Quoted 9 lines
Quoted 9 lines
However after fixing that mt76usb still did not work. To make things work we had to change rx frag size from 2048 to
PAGE_SIZEand changevirt_to_head_page()tovirt_to_page()when setting SG's.I think I understand why first change was needed. If we do 2 separate dma maps of 2 different buffers in single page i.e (PAGE + off=0 and PAGE + off=2048) it causes problem. So either
map_sg()return error which mt76usb does not handle correctly or there is issue in AMD IOMMU because two dma maps use the same page.
Quoted 4 lines
Any comment on that? Is fine or not to do 2 or more dma mappings within the same single page on AMD IOMMU? If not, is there any mechanism for drivers to find out about this limitation to prevent to prepare wrong SG buffers?
FTR: it was confirmed by Jan (bug reporter) the 2 or more dma mappings within single page works with AMD IOMMU. Most likely it was needed previously to workaround this sg->offset problem until proper fix to AMD IOMMU was applied.
Stanislaw