iommu/amd: page-specific invalidations for more than one page
Subsystems (2)
PATCH 1/1 iommu/amd: page-specific invalidations for more than one page
From: Nadav Amit <[email protected]>
Currently, IOMMU invalidations and device-IOTLB invalidations using AMD IOMMU fall back to full address-space invalidation if more than a single page need to be flushed.
Full flushes are especially inefficient when the IOMMU is virtualized by a hypervisor, since it requires the hypervisor to synchronize the entire address-space.
AMD IOMMUs allow to provide a mask to perform page-specific invalidations for multiple pages that match the address. The mask is encoded as part of the address, and the first zero bit in the address (in bits [51:12]) indicates the mask size.
Use this hardware feature to perform selective IOMMU and IOTLB flushes. Combine the logic between both for better code reuse.
The IOMMU invalidations passed a smoke-test. The device IOTLB invalidations are untested.
--- drivers/iommu/amd/iommu.c | 76 +++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 34 deletions(-)
drivers/iommu/amd/iommu.c +42 -34
@@ -927,33 +927,58 @@ static void build_inv_dte(struct iommu_cmd *cmd, u16 devid)CMD_SET_TYPE (cmd,CMD_INV_DEV_ENTRY ); } -static void build_inv_iommu_pages (struct iommu_cmd *cmd,u64 address, -size_t size,u16 domid,int pde) +/* +* Builds an invalidation address which is suitable for one page or multiple +* pages. Sets the size bit (S) as needed is more than one page is flushed. +*/ +static inline u64 build_inv_address (u64 address,size_t size) { -u64 pages; -bool s; +u64 pages, end, msb_diff; pages =iommu_num_pages (address, size,PAGE_SIZE ); - s = false; -if (pages >1 ) { +if (pages ==1 ) +return address &PAGE_MASK ; + + end = address + size -1 ; + +/* +* msb_diff would hold the index of the most significant bit that +* flipped between the start and end. +*/ + msb_diff =fls64 (end ^ address) -1 ; + +/* +* Bits 63:52 are sign extended. If for some reason bit 51 is different +* between the start and the end, invalidate everything. +*/ +if (unlikely (msb_diff >51 )) { + address =CMD_INV_IOMMU_ALL_PAGES_ADDRESS ; + }else {/* -* If we have to flush more than one page, flush all -* TLB entries for this domain +* The msb-bit must be clear on the address. Just set all the +* lower bits. */ - address =CMD_INV_IOMMU_ALL_PAGES_ADDRESS ; - s = true; + address |=1ull << (msb_diff -1 ); } +/* Clear bits 11:0 */ address &=PAGE_MASK ; +/* Set the size bit - we flush more than one 4kb page */ +return address |CMD_INV_IOMMU_PAGES_SIZE_MASK ; +} + +static void build_inv_iommu_pages (struct iommu_cmd *cmd,u64 address, +size_t size,u16 domid,int pde) +{ +u64 inv_address =build_inv_address (address, size); +memset (cmd,0 ,sizeof (*cmd)); cmd->data[1 ] |= domid; - cmd->data[2 ] =lower_32_bits (address); - cmd->data[3 ] =upper_32_bits (address); + cmd->data[2 ] =lower_32_bits (inv_address); + cmd->data[3 ] =upper_32_bits (inv_address);CMD_SET_TYPE (cmd,CMD_INV_IOMMU_PAGES ); -if (s)/* size bit - we flush more than one 4kb page */ - cmd->data[2 ] |=CMD_INV_IOMMU_PAGES_SIZE_MASK ;if (pde)/* PDE bit - we want to flush everything, not only the PTEs */ cmd->data[2 ] |=CMD_INV_IOMMU_PAGES_PDE_MASK ; }
@@ -961,32 +986,15 @@ static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address,static void build_inv_iotlb_pages (struct iommu_cmd *cmd,u16 devid,int qdep,u64 address,size_t size) { -u64 pages; -bool s; - - pages =iommu_num_pages (address, size,PAGE_SIZE ); - s = false; - -if (pages >1 ) { -/* -* If we have to flush more than one page, flush all -* TLB entries for this domain -*/ - address =CMD_INV_IOMMU_ALL_PAGES_ADDRESS ; - s = true; - } - - address &=PAGE_MASK ; +u64 inv_address =build_inv_address (address, size);memset (cmd,0 ,sizeof (*cmd)); cmd->data[0 ] = devid; cmd->data[0 ] |= (qdep &0xff ) <<24 ; cmd->data[1 ] = devid; - cmd->data[2 ] =lower_32_bits (address); - cmd->data[3 ] =upper_32_bits (address); + cmd->data[2 ] =lower_32_bits (inv_address); + cmd->data[3 ] =upper_32_bits (inv_address);CMD_SET_TYPE (cmd,CMD_INV_IOTLB_PAGES ); -if (s) - cmd->data[2 ] |=CMD_INV_IOMMU_PAGES_SIZE_MASK ; } static void build_inv_iommu_pasid(struct iommu_cmd *cmd, u16 domid, u32 pasid,
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerQuoted 15 lines · Nadav Amit
From: Nadav Amit <[email protected]>
Currently, IOMMU invalidations and device-IOTLB invalidations using AMD IOMMU fall back to full address-space invalidation if more than a single page need to be flushed.
Full flushes are especially inefficient when the IOMMU is virtualized by a hypervisor, since it requires the hypervisor to synchronize the entire address-space.
AMD IOMMUs allow to provide a mask to perform page-specific invalidations for multiple pages that match the address. The mask is encoded as part of the address, and the first zero bit in the address (in bits [51:12]) indicates the mask size.
Quoted 5 lines
Use this hardware feature to perform selective IOMMU and IOTLB flushes. Combine the logic between both for better code reuse.
The IOMMU invalidations passed a smoke-test. The device IOTLB invalidations are untested.
Have you thoroughly tested this on real hardware? I had a patch-set doing the same many years ago and it lead to data corruption under load. Back then it could have been a bug in my code of course, but it made me cautious about using targeted invalidations.
Regards,
Joerg
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Quoted 23 lines · Joerg Roedel
Quoted 18 lines · Nadav Amit
From: Nadav Amit <[email protected]>
Currently, IOMMU invalidations and device-IOTLB invalidations using AMD IOMMU fall back to full address-space invalidation if more than a single page need to be flushed.
Full flushes are especially inefficient when the IOMMU is virtualized by a hypervisor, since it requires the hypervisor to synchronize the entire address-space.
AMD IOMMUs allow to provide a mask to perform page-specific invalidations for multiple pages that match the address. The mask is encoded as part of the address, and the first zero bit in the address (in bits [51:12]) indicates the mask size.
Use this hardware feature to perform selective IOMMU and IOTLB flushes. Combine the logic between both for better code reuse.
Quoted 7 lines
Quoted 2 lines
The IOMMU invalidations passed a smoke-test. The device IOTLB invalidations are untested.
Have you thoroughly tested this on real hardware? I had a patch-set doing the same many years ago and it lead to data corruption under load. Back then it could have been a bug in my code of course, but it made me cautious about using targeted invalidations.
I tested it on real bare-metal hardware. I ran some basic I/O workloads with the IOMMU enabled, checkers enabled/disabled, and so on.
However, I only tested the IOMMU-flushes and I did not test that the device-IOTLB flush work, since I did not have the hardware for that.
If you can refer me to the old patches, I will have a look and see whether I can see a difference in the logic or test them. If you want me to run different tests - let me know. If you want me to remove the device-IOTLB invalidations logic - that is also fine with me.
Joerg Roedel
maintainerHi Nadav,
Quoted 10 lines · Nadav Amit
I tested it on real bare-metal hardware. I ran some basic I/O workloads with the IOMMU enabled, checkers enabled/disabled, and so on.
However, I only tested the IOMMU-flushes and I did not test that the device-IOTLB flush work, since I did not have the hardware for that.
If you can refer me to the old patches, I will have a look and see whether I can see a difference in the logic or test them. If you want me to run different tests - let me know. If you want me to remove the device-IOTLB invalidations logic - that is also fine with me.
Here is the patch-set, it is from 2010 and against a very old version of the AMD IOMMU driver:
https://lore.kernel.org/lkml/[email protected]/
Regards,
Joerg
Quoted 12 lines · Joerg Roedel
Hi Nadav,
Quoted 6 lines · Nadav Amit
I tested it on real bare-metal hardware. I ran some basic I/O workloads with the IOMMU enabled, checkers enabled/disabled, and so on.
However, I only tested the IOMMU-flushes and I did not test that the device-IOTLB flush work, since I did not have the hardware for that.
Quoted 7 lines
Quoted 4 lines
If you can refer me to the old patches, I will have a look and see whether I can see a difference in the logic or test them. If you want me to run different tests - let me know. If you want me to remove the device-IOTLB invalidations logic - that is also fine with me.
Here is the patch-set, it is from 2010 and against a very old version of the AMD IOMMU driver:
Thanks. I looked at your code and I see a difference between the implementations.
As far as I understand, pages are always assumed to be aligned to their own sizes. I therefore assume that flushes should regard the lower bits as a “mask” and not just as encoding of the size.
In the version that you referred me to, iommu_update_domain_tlb() only regards the size of the region to be flushed and disregards the alignment:
+ order = get_order(domain->flush.end - domain->flush.start);
+ mask = (0x1000ULL << order) - 1;
+ address = ((domain->flush.start & ~mask) | (mask >> 1)) & ~0xfffULL;
If you need to flush for instance the region between 0x1000-0x5000, this version would use the address|mask of 0x1000 (16KB page). The version I sent regards the alignment, and since the range is not aligned would use address|mask of 0x3000 (32KB page).
IIUC, IOVA allocations today are aligned in such way, but at least in the past (looking on 3.19 for the matter), it was not like always like that, which can explain the problems.
Thoughts?
Joerg Roedel
maintainerQuoted 9 lines · Nadav Amit
In the version that you referred me to,
iommu_update_domain_tlb()only regards the size of the region to be flushed and disregards the alignment:(quoted diff) +3
-0+ order = get_order(domain->flush.end - domain->flush.start); + mask = (0x1000ULL << order) - 1; + address = ((domain->flush.start & ~mask) | (mask >> 1)) & ~0xfffULL;
Quoted 8 lines
If you need to flush for instance the region between 0x1000-0x5000, this version would use the address|mask of
0x1000(16KB page). The version I sent regards the alignment, and since the range is not aligned would use address|mask of0x3000(32KB page).IIUC, IOVA allocations today are aligned in such way, but at least in the past (looking on 3.19 for the matter), it was not like always like that, which can explain the problems.
Yeah, that make sense and explains the data corruption problems. I will give your patch a try on one of my test machines and consider it for v5.13 if all goes well.
Thanks,
Joerg
Joerg Roedel
maintainerQuoted 2 lines · Nadav Amit
drivers/iommu/amd/iommu.c | 76 +++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 34 deletions(-)
Load-testing looks good here too, so this patch is queued now for v5.13, thanks Nadav.
Regards,
Joerg