powerpc/iommu: DMA address offset is incorrectly calculated with 2MB TCEs
PATCH v2 1/1 powerpc/iommu: DMA address offset is incorrectly calculated with 2MB TCEs
When DMA window is backed by 2MB TCEs, the DMA address for the mapped page should be the offset of the page relative to the 2MB TCE. The code was incorrectly setting the DMA address to the beginning of the TCE range.
Mellanox driver is reporting timeout trying to ENABLE_HCA for an SR-IOV ethernet port, when DMA window is backed by 2MB TCEs.
--- arch/powerpc/kernel/iommu.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
arch/powerpc/kernel/iommu.c +6 -3
@@ -517,7 +517,7 @@ int ppc_iommu_map_sg(struct device *dev, struct iommu_table *tbl,/* Convert entry to a dma_addr_t */ entry += tbl->it_offset; dma_addr = entry << tbl->it_page_shift; - dma_addr |= (s->offset & ~IOMMU_PAGE_MASK (tbl)); + dma_addr |= (vaddr & ~IOMMU_PAGE_MASK (tbl));DBG (" - %lu pages, entry: %lx, dma_addr: %lx\n" , npages, entry, dma_addr);
@@ -904,6 +904,7 @@ void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,unsigned int order;unsigned int nio_pages, io_order;struct page *page; +int tcesize = (1 << tbl->it_page_shift); size =PAGE_ALIGN (size); order =get_order (size);
@@ -930,7 +931,8 @@ void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,memset (ret,0 , size);/* Set up tces to cover the allocated range */ - nio_pages = size >> tbl->it_page_shift; + nio_pages =IOMMU_PAGE_ALIGN (size, tbl) >> tbl->it_page_shift; + io_order =get_iommu_order (size, tbl); mapping =iommu_alloc (dev, tbl, ret, nio_pages,DMA_BIDIRECTIONAL , mask >> tbl->it_page_shift, io_order,0 );
@@ -938,7 +940,8 @@ void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,free_pages ((unsigned long )ret, order);return NULL ; } - *dma_handle = mapping; + + *dma_handle = mapping | ((u64 )ret & (tcesize -1 ));return ret; }
Michael Ellerman
reviewerQuoted 7 lines · Gaurav Batra
When DMA window is backed by 2MB TCEs, the DMA address for the mapped page should be the offset of the page relative to the 2MB TCE. The code was incorrectly setting the DMA address to the beginning of the TCE range.
Mellanox driver is reporting timeout trying to
ENABLE_HCAfor an SR-IOV ethernet port, when DMA window is backed by 2MB TCEs.
I assume this is similar or related to the bug Srikar reported?
https://lore.kernel.org/linuxppc-dev/[email protected]/
In that thread Alexey suggested a patch, have you tried his patch? He suggested rounding up the allocation size, rather than adjusting the dma_handle.
Quoted 1 line
Fixes 3872731187141d5d0a5c4fb30007b8b9ec36a44d
That's not the right syntax, it's described in the documentation how to generate it.
It should be:
cheers
Quoted 42 lines
arch/powerpc/kernel/iommu.c +6
-3@@ -517,7 +517,7 @@ int ppc_iommu_map_sg(struct device *dev, struct iommu_table *tbl,/* Convert entry to a dma_addr_t */ entry += tbl->it_offset; dma_addr = entry << tbl->it_page_shift; - dma_addr |= (s->offset & ~IOMMU_PAGE_MASK (tbl)); + dma_addr |= (vaddr & ~IOMMU_PAGE_MASK (tbl));DBG (" - %lu pages, entry: %lx, dma_addr: %lx\n" , npages, entry, dma_addr);@@ -904,6 +904,7 @@ void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,unsigned int order;unsigned int nio_pages, io_order;struct page *page; +int tcesize = (1 << tbl->it_page_shift); size =PAGE_ALIGN (size); order =get_order (size);@@ -930,7 +931,8 @@ void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,memset (ret,0 , size);/* Set up tces to cover the allocated range */ - nio_pages = size >> tbl->it_page_shift; + nio_pages =IOMMU_PAGE_ALIGN (size, tbl) >> tbl->it_page_shift; + io_order =get_iommu_order (size, tbl); mapping =iommu_alloc (dev, tbl, ret, nio_pages,DMA_BIDIRECTIONAL , mask >> tbl->it_page_shift, io_order,0 );@@ -938,7 +940,8 @@ void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,free_pages ((unsigned long )ret, order);return NULL ; } - *dma_handle = mapping; + + *dma_handle = mapping | ((u64 )ret & (tcesize -1 ));return ret; }
Hello Michael,
I was looking into the Bug: 199106 (https://bugzilla.linux.ibm.com/show_bug.cgi?id=199106).
In the Bug, Mellanox driver was timing out when enabling SRIOV device.
I tested, Alexey's patch and it fixes the issue with Mellanox driver. The down side
to Alexey's fix is that even a small memory request by the driver will be aligned up
to 2MB. In my test, the Mellanox driver is issuing multiple requests of 64K size.
All these will get aligned up to 2MB, which is quite a waste of resources.
In any case, both the patches work. Let me know which approach you prefer. In case
we decide to go with my patch, I just realized that I need to fix nio_pages in
iommu_free_coherent() as well.
Thanks,
Gaurav
Quoted 68 lines · Michael Ellerman
Quoted 7 lines · Gaurav Batra
When DMA window is backed by 2MB TCEs, the DMA address for the mapped page should be the offset of the page relative to the 2MB TCE. The code was incorrectly setting the DMA address to the beginning of the TCE range.
Mellanox driver is reporting timeout trying to
ENABLE_HCAfor an SR-IOV ethernet port, when DMA window is backed by 2MB TCEs.I assume this is similar or related to the bug Srikar reported?
https://lore.kernel.org/linuxppc-dev/[email protected]/In that thread Alexey suggested a patch, have you tried his patch? He suggested rounding up the allocation size, rather than adjusting the
dma_handle.Quoted 1 line
Fixes 3872731187141d5d0a5c4fb30007b8b9ec36a44dThat's not the right syntax, it's described in the documentation how to generate it.
It should be:
Fixes 387273118714 ("powerps/pseries/dma: Add support for 2M IOMMU page size")cheers
Quoted 42 lines
arch/powerpc/kernel/iommu.c +6
-3@@ -517,7 +517,7 @@ int ppc_iommu_map_sg(struct device *dev, struct iommu_table *tbl,/* Convert entry to a dma_addr_t */ entry += tbl->it_offset; dma_addr = entry << tbl->it_page_shift; - dma_addr |= (s->offset & ~IOMMU_PAGE_MASK (tbl)); + dma_addr |= (vaddr & ~IOMMU_PAGE_MASK (tbl));DBG (" - %lu pages, entry: %lx, dma_addr: %lx\n" , npages, entry, dma_addr);@@ -904,6 +904,7 @@ void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,unsigned int order;unsigned int nio_pages, io_order;struct page *page; +int tcesize = (1 << tbl->it_page_shift); size =PAGE_ALIGN (size); order =get_order (size);@@ -930,7 +931,8 @@ void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,memset (ret,0 , size);/* Set up tces to cover the allocated range */ - nio_pages = size >> tbl->it_page_shift; + nio_pages =IOMMU_PAGE_ALIGN (size, tbl) >> tbl->it_page_shift; + io_order =get_iommu_order (size, tbl); mapping =iommu_alloc (dev, tbl, ret, nio_pages,DMA_BIDIRECTIONAL , mask >> tbl->it_page_shift, io_order,0 );@@ -938,7 +940,8 @@ void *iommu_alloc_coherent(struct device *dev, struct iommu_table *tbl,free_pages ((unsigned long )ret, order);return NULL ; } - *dma_handle = mapping; + + *dma_handle = mapping | ((u64 )ret & (tcesize -1 ));return ret; }
Hello Alexey,
I recently joined IOMMU team. There was a bug reported by test team where Mellanox driver was timing out during configuration. I proposed a fix for the same, which is below in the email.
You suggested a fix for Srikar's reported problem. Basically, both these fixes will resolve Srikar and Mellanox driver issues. The problem is with 2MB DDW.
Since you have extensive knowledge of IOMMU design and code, in your opinion, which patch should we adopt?
Thanks a lot
Gaurav
Quoted 95 lines · Gaurav Batra
Hello Michael,
I was looking into the Bug: 199106 (https://bugzilla.linux.ibm.com/show_bug.cgi?id=199106).
In the Bug, Mellanox driver was timing out when enabling SRIOV device.
I tested, Alexey's patch and it fixes the issue with Mellanox driver. The down side
to Alexey's fix is that even a small memory request by the driver will be aligned up
to 2MB. In my test, the Mellanox driver is issuing multiple requests of 64K size.
All these will get aligned up to 2MB, which is quite a waste of resources.
In any case, both the patches work. Let me know which approach you prefer. In case
we decide to go with my patch, I just realized that I need to fix
nio_pagesin
iommu_free_coherent()as well.Thanks,
Gaurav
Quoted 68 lines · Michael Ellerman
Quoted 7 lines · Gaurav Batra
When DMA window is backed by 2MB TCEs, the DMA address for the mapped page should be the offset of the page relative to the 2MB TCE. The code was incorrectly setting the DMA address to the beginning of the TCE range.
Mellanox driver is reporting timeout trying to
ENABLE_HCAfor an SR-IOV ethernet port, when DMA window is backed by 2MB TCEs.I assume this is similar or related to the bug Srikar reported?
https://lore.kernel.org/linuxppc-dev/[email protected]/
In that thread Alexey suggested a patch, have you tried his patch? He suggested rounding up the allocation size, rather than adjusting the
dma_handle.Quoted 1 line
Fixes 3872731187141d5d0a5c4fb30007b8b9ec36a44dThat's not the right syntax, it's described in the documentation how to generate it.
It should be:
Fixes 387273118714 ("powerps/pseries/dma: Add support for 2M IOMMU page size")cheers
Quoted 42 lines
arch/powerpc/kernel/iommu.c +6
-3@@ -517,7 +517,7 @@ int ppc_iommu_map_sg(struct device *dev, structiommu_table *tbl, /* Convert entry to a dma_addr_t */ entry += tbl->it_offset; dma_addr = entry << tbl->it_page_shift; - dma_addr |= (s->offset & ~IOMMU_PAGE_MASK(tbl)); + dma_addr |= (vaddr & ~IOMMU_PAGE_MASK(tbl)); DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n", npages, entry, dma_addr);@@ -904,6 +904,7 @@ void *iommu_alloc_coherent(struct device *dev,struct iommu_table *tbl, unsigned int order; unsigned int nio_pages, io_order; struct page *page; + int tcesize = (1 << tbl->it_page_shift); size = PAGE_ALIGN(size); order = get_order(size);@@ -930,7 +931,8 @@ void *iommu_alloc_coherent(struct device *dev,struct iommu_table *tbl, memset(ret, 0, size); /* Set up tces to cover the allocated range */ - nio_pages = size >> tbl->it_page_shift; + nio_pages = IOMMU_PAGE_ALIGN(size, tbl) >> tbl->it_page_shift; + io_order = get_iommu_order(size, tbl); mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL, mask >> tbl->it_page_shift, io_order, 0);@@ -938,7 +940,8 @@ void *iommu_alloc_coherent(struct device *dev,struct iommu_table *tbl, free_pages((unsigned long)ret, order); return NULL; } - *dma_handle = mapping; + + *dma_handle = mapping | ((u64)ret & (tcesize - 1)); return ret; }--
Hi Gaurav,
Sorry I missed this. Please share the link to the your fix, I do not see it in my mail. In general, the problem can probably be solved by using huge pages (anything more than 64K) only for 1:1 mapping.
Quoted 108 lines · Gaurav Batra
Hello Alexey,
I recently joined IOMMU team. There was a bug reported by test team where Mellanox driver was timing out during configuration. I proposed a fix for the same, which is below in the email.
You suggested a fix for Srikar's reported problem. Basically, both these fixes will resolve Srikar and Mellanox driver issues. The problem is with 2MB DDW.
Since you have extensive knowledge of IOMMU design and code, in your opinion, which patch should we adopt?
Thanks a lot
Gaurav
Quoted 95 lines · Gaurav Batra
Hello Michael,
I was looking into the Bug: 199106 (https://bugzilla.linux.ibm.com/show_bug.cgi?id=199106).
In the Bug, Mellanox driver was timing out when enabling SRIOV device.
I tested, Alexey's patch and it fixes the issue with Mellanox driver. The down side
to Alexey's fix is that even a small memory request by the driver will be aligned up
to 2MB. In my test, the Mellanox driver is issuing multiple requests of 64K size.
All these will get aligned up to 2MB, which is quite a waste of resources.
In any case, both the patches work. Let me know which approach you prefer. In case
we decide to go with my patch, I just realized that I need to fix
nio_pagesin
iommu_free_coherent()as well.Thanks,
Gaurav
Quoted 68 lines · Michael Ellerman
Quoted 7 lines · Gaurav Batra
When DMA window is backed by 2MB TCEs, the DMA address for the mapped page should be the offset of the page relative to the 2MB TCE. The code was incorrectly setting the DMA address to the beginning of the TCE range.
Mellanox driver is reporting timeout trying to
ENABLE_HCAfor an SR-IOV ethernet port, when DMA window is backed by 2MB TCEs.I assume this is similar or related to the bug Srikar reported?
https://lore.kernel.org/linuxppc-dev/[email protected]/
In that thread Alexey suggested a patch, have you tried his patch? He suggested rounding up the allocation size, rather than adjusting the
dma_handle.Quoted 1 line
Fixes 3872731187141d5d0a5c4fb30007b8b9ec36a44dThat's not the right syntax, it's described in the documentation how to generate it.
It should be:
Fixes 387273118714 ("powerps/pseries/dma: Add support for 2M IOMMU page size")cheers
Quoted 42 lines
arch/powerpc/kernel/iommu.c +6
-3@@ -517,7 +517,7 @@ int ppc_iommu_map_sg(struct device *dev, structiommu_table *tbl, /* Convert entry to a dma_addr_t */ entry += tbl->it_offset; dma_addr = entry << tbl->it_page_shift; - dma_addr |= (s->offset & ~IOMMU_PAGE_MASK(tbl)); + dma_addr |= (vaddr & ~IOMMU_PAGE_MASK(tbl)); DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n", npages, entry, dma_addr);@@ -904,6 +904,7 @@ void *iommu_alloc_coherent(struct device *dev,struct iommu_table *tbl, unsigned int order; unsigned int nio_pages, io_order; struct page *page; + int tcesize = (1 << tbl->it_page_shift); size = PAGE_ALIGN(size); order = get_order(size);@@ -930,7 +931,8 @@ void *iommu_alloc_coherent(struct device *dev,struct iommu_table *tbl, memset(ret, 0, size); /* Set up tces to cover the allocated range */ - nio_pages = size >> tbl->it_page_shift; + nio_pages = IOMMU_PAGE_ALIGN(size, tbl) >> tbl->it_page_shift; + io_order = get_iommu_order(size, tbl); mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL, mask >> tbl->it_page_shift, io_order, 0);@@ -938,7 +940,8 @@ void *iommu_alloc_coherent(struct device *dev,struct iommu_table *tbl, free_pages((unsigned long)ret, order); return NULL; } - *dma_handle = mapping; + + *dma_handle = mapping | ((u64)ret & (tcesize - 1)); return ret; }--
Signature · 1 line
Alexey
Hello Alexey,
No worries. I resolved the issue with Michael's help. The patch is merged upstream and it fixes the issue.
Here is the link
https://lore.kernel.org/all/[email protected]/
Thanks,
Gaurav
Quoted 116 lines · Alexey Kardashevskiy
Hi Gaurav,
Sorry I missed this. Please share the link to the your fix, I do not see it in my mail. In general, the problem can probably be solved by using huge pages (anything more than 64K) only for 1:1 mapping.
Quoted 109 lines · Gaurav Batra
Hello Alexey,
I recently joined IOMMU team. There was a bug reported by test team where Mellanox driver was timing out during configuration. I proposed a fix for the same, which is below in the email.
You suggested a fix for Srikar's reported problem. Basically, both these fixes will resolve Srikar and Mellanox driver issues. The problem is with 2MB DDW.
Since you have extensive knowledge of IOMMU design and code, in your opinion, which patch should we adopt?
Thanks a lot
Gaurav
Quoted 96 lines · Gaurav Batra
Hello Michael,
I was looking into the Bug: 199106 (https://bugzilla.linux.ibm.com/show_bug.cgi?id=199106).
In the Bug, Mellanox driver was timing out when enabling SRIOV device.
I tested, Alexey's patch and it fixes the issue with Mellanox driver. The down side
to Alexey's fix is that even a small memory request by the driver will be aligned up
to 2MB. In my test, the Mellanox driver is issuing multiple requests of 64K size.
All these will get aligned up to 2MB, which is quite a waste of resources.
In any case, both the patches work. Let me know which approach you prefer. In case
we decide to go with my patch, I just realized that I need to fix
nio_pagesin
iommu_free_coherent()as well.Thanks,
Gaurav
Quoted 69 lines · Michael Ellerman
Quoted 7 lines · Gaurav Batra
When DMA window is backed by 2MB TCEs, the DMA address for the mapped page should be the offset of the page relative to the 2MB TCE. The code was incorrectly setting the DMA address to the beginning of the TCE range.
Mellanox driver is reporting timeout trying to
ENABLE_HCAfor an SR-IOV ethernet port, when DMA window is backed by 2MB TCEs.I assume this is similar or related to the bug Srikar reported?
https://lore.kernel.org/linuxppc-dev/[email protected]/
In that thread Alexey suggested a patch, have you tried his patch? He suggested rounding up the allocation size, rather than adjusting the
dma_handle.Quoted 1 line
Fixes 3872731187141d5d0a5c4fb30007b8b9ec36a44dThat's not the right syntax, it's described in the documentation how to generate it.
It should be:
Fixes 387273118714 ("powerps/pseries/dma: Add support for 2M IOMMU page size")cheers
Quoted 42 lines
arch/powerpc/kernel/iommu.c +6
-3@@ -517,7 +517,7 @@ int ppc_iommu_map_sg(struct device *dev,struct iommu_table *tbl, /* Convert entry to a dma_addr_t */ entry += tbl->it_offset; dma_addr = entry << tbl->it_page_shift; - dma_addr |= (s->offset & ~IOMMU_PAGE_MASK(tbl)); + dma_addr |= (vaddr & ~IOMMU_PAGE_MASK(tbl)); DBG(" - %lu pages, entry: %lx, dma_addr: %lx\n", npages, entry, dma_addr);@@ -904,6 +904,7 @@ void *iommu_alloc_coherent(struct device *dev,struct iommu_table *tbl, unsigned int order; unsigned int nio_pages, io_order; struct page *page; + int tcesize = (1 << tbl->it_page_shift); size = PAGE_ALIGN(size); order = get_order(size);@@ -930,7 +931,8 @@ void *iommu_alloc_coherent(struct device *dev,struct iommu_table *tbl, memset(ret, 0, size); /* Set up tces to cover the allocated range */ - nio_pages = size >> tbl->it_page_shift; + nio_pages = IOMMU_PAGE_ALIGN(size, tbl) >> tbl->it_page_shift; + io_order = get_iommu_order(size, tbl); mapping = iommu_alloc(dev, tbl, ret, nio_pages, DMA_BIDIRECTIONAL, mask >> tbl->it_page_shift, io_order, 0);@@ -938,7 +940,8 @@ void *iommu_alloc_coherent(struct device *dev,struct iommu_table *tbl, free_pages((unsigned long)ret, order); return NULL; } - *dma_handle = mapping; + + *dma_handle = mapping | ((u64)ret & (tcesize - 1)); return ret; }--
Michael Ellerman
reviewerQuoted 10 lines · Gaurav Batra
Hello Michael,
I was looking into the Bug: 199106
(https://bugzilla.linux.ibm.com/show_bug.cgi?id=199106).In the Bug, Mellanox driver was timing out when enabling SRIOV device.
I tested, Alexey's patch and it fixes the issue with Mellanox driver. The down side
Quoted 7 lines
to Alexey's fix is that even a small memory request by the driver will be aligned up
to 2MB. In my test, the Mellanox driver is issuing multiple requests of 64K size.
All these will get aligned up to 2MB, which is quite a waste of resources.
OK. I guess we should use your patch then.
It's not ideal as it means the device can potentially read/write to memory it shouldn't, but 2MB is a lot to waste for a 64K alloc.
Quoted 7 lines
In any case, both the patches work. Let me know which approach you prefer. In case
we decide to go with my patch, I just realized that I need to fix
nio_pagesin
iommu_free_coherent()as well.
Can you send a v3 with that fixed please.
cheers
Hello Michael,
I agree with your concerns regarding a device been able to access memory that doesn't belong to it. That exposure we have today with 2MB TCEs. With 2MB TCEs, DMA window size will be big enough, for dedicated adapters, that whole memory is going to be mapped "direct". Which essentially means, that a "rogue" device/driver has the potential to corrupt LPAR wide memory.
I have sent you v3.
Thanks,
Gaurav
Quoted 33 lines · Michael Ellerman
Quoted 10 lines · Gaurav Batra
Hello Michael,
I was looking into the Bug: 199106
(https://bugzilla.linux.ibm.com/show_bug.cgi?id=199106).In the Bug, Mellanox driver was timing out when enabling SRIOV device.
I tested, Alexey's patch and it fixes the issue with Mellanox driver. The down side
Quoted 7 lines
to Alexey's fix is that even a small memory request by the driver will be aligned up
to 2MB. In my test, the Mellanox driver is issuing multiple requests of 64K size.
All these will get aligned up to 2MB, which is quite a waste of resources.
OK. I guess we should use your patch then.
It's not ideal as it means the device can potentially read/write to memory it shouldn't, but 2MB is a lot to waste for a 64K alloc.
Quoted 7 lines
In any case, both the patches work. Let me know which approach you prefer. In case
we decide to go with my patch, I just realized that I need to fix
nio_pagesin
iommu_free_coherent()as well.Can you send a v3 with that fixed please.
cheers
Michael Ellerman
reviewerQuoted 8 lines · Gaurav Batra
Hello Michael,
I agree with your concerns regarding a device been able to access memory that doesn't belong to it. That exposure we have today with 2MB TCEs. With 2MB TCEs, DMA window size will be big enough, for dedicated adapters, that whole memory is going to be mapped "direct". Which essentially means, that a "rogue" device/driver has the potential to corrupt LPAR wide memory.
Yes that's always been a trade-off between performance and robustness, and performance is generally the winner.
There have been various command line flags in the past to configure stricter behaviour, disable bypass etc. Some of those are now generic, iommu.strict/passthrough, it would be good to get them wired up to work on powerpc at some point.
Quoted 1 line
I have sent you v3.
Thanks.
cheers