Skip to contentSkip to conversation

Don't split flush for amd_iommu_domain_flush_all()

PATCH Superseded Weinan Liu 6 participants 10 messages Apr 14 – May 28

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Weinan Liu

author
#0 lore raw

cover Don't split flush for amd_iommu_domain_flush_all()

This patch is a respin of Josef Bacik's original work[1] to fix the performance issues and soft lockups in the AMD IOMMU driver when running within a VM. I am taking over the respin as Josef currently has limited time for this

[1]:https://lore.kernel.org/linux-iommu/ad8652c5e9f8aeee05e2103f4987589cdd4a3fd0.1772659768.git.josef@toxicpanda.com

Weinan Liu (1):
iommu/amd: Don't split flush for amd_iommu_domain_flush_all()

 drivers/iommu/amd/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
author
#1 lore raw

PATCH 1/1 iommu/amd: Don't split flush for amd_iommu_domain_flush_all()

We have observed multiple full invalidations occurring during device detach when we are done using the vfio-device.

blocked_domain_attach_device()
-> detach_device()

    -> amd_iommu_domain_flush_all()
      -> amd_iommu_domain_flush_pages(..., CMD_INV_IOMMU_ALL_PAGES_ADDRESS)

      	while (size != 0) {

          -> __domain_flush_pages( flush_size /* power of 2 flush_size */)
            -> domain_flush_pages_v1()
              -> build_inv_iommu_pages()
                -> build_inv_address()

         }

build_inv_address() will trigger a full invalidation if the chunk size > (1 << 51). Consequently, the guest will issue multiple full invalidations for a single call to amd_iommu_domain_flush_all()

Without this patch, we will see 10 time instead of 1 time full invalidations for every amd_iommu_domain_flush_all().

Fixes a270be1b3fdf ("iommu/amd: Use only natural aligned flushes in a VM")
---
 drivers/iommu/amd/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +2 -1
index 760d5f4623b5..bcec8721d228 100644
@@ -1769,7 +1769,8 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 {
 	lockdep_assert_held(&domain->lock);
 
-	if (likely(!amd_iommu_np_cache)) {
+	if (likely(!amd_iommu_np_cache) ||
+		size == CMD_INV_IOMMU_ALL_PAGES_ADDRESS) {
 		__domain_flush_pages(domain, address, size);
 
 		/* Wait until IOMMU TLB and all device IOTLB flushes are complete */
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
Reviewed-by on v1 1/1
#2 lore raw

Re: [PATCH v1 1/1] iommu/amd: Don't split flush for amd_iommu_domain_flush_all()

drivers/iommu/amd/iommu.c void amd_iommu_domain_flush_pages(struct protection_domain *domain, · line 1773

Quoted 11 lines · Weinan Liu
drivers/iommu/amd/iommu.c +2 -1
index 760d5f4623b5..bcec8721d228 100644
@@ -1769,7 +1769,8 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 {
 	lockdep_assert_held(&domain->lock);
 
-	if (likely(!amd_iommu_np_cache)) {
+	if (likely(!amd_iommu_np_cache) ||
+		size == CMD_INV_IOMMU_ALL_PAGES_ADDRESS) {

This is better as size >= (1ULL<<52)

Which is the break point where it switches over to full invalidation anyhow, there is no sense then in doing the special np flow..

Otherwise

Jason

author
#3 lore raw

PATCH 1/1 iommu/amd: Don't split flush for amd_iommu_domain_flush_all()

We have observed multiple full invalidations occurring during device detach when we are done using the vfio-device.

blocked_domain_attach_device()
-> detach_device()

    -> amd_iommu_domain_flush_all()
      -> amd_iommu_domain_flush_pages(..., CMD_INV_IOMMU_ALL_PAGES_ADDRESS)

      	while (size != 0) {

          -> __domain_flush_pages( flush_size /* power of 2 flush_size */)
            -> domain_flush_pages_v1()
              -> build_inv_iommu_pages()
                -> build_inv_address()

         }

build_inv_address() will trigger a full invalidation if the chunk size > (1 << 51). Consequently, the guest will issue multiple full invalidations for a single call to amd_iommu_domain_flush_all()

Without this patch, we will see 10 time instead of 1 time full invalidations for every amd_iommu_domain_flush_all().

Fixes a270be1b3fdf ("iommu/amd: Use only natural aligned flushes in a VM")
---
 drivers/iommu/amd/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +2 -1
index 760d5f4623b5..59650a44ab50 100644
@@ -1769,7 +1769,8 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 {
 	lockdep_assert_held(&domain->lock);
 
-	if (likely(!amd_iommu_np_cache)) {
+	if (likely(!amd_iommu_np_cache) ||
+		size >= (1ULL<<52)) {
 		__domain_flush_pages(domain, address, size);
 
 		/* Wait until IOMMU TLB and all device IOTLB flushes are complete */
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
Reviewed-by on v1 1/1
#4 lore raw

Re: [PATCH v1 1/1] iommu/amd: Don't split flush for amd_iommu_domain_flush_all()

Quoted 24 lines · Weinan Liu

We have observed multiple full invalidations occurring during device detach when we are done using the vfio-device.

blocked_domain_attach_device()
-> detach_device()

    -> amd_iommu_domain_flush_all()
      -> amd_iommu_domain_flush_pages(..., CMD_INV_IOMMU_ALL_PAGES_ADDRESS)

      	while (size != 0) {

          -> __domain_flush_pages( flush_size /* power of 2 flush_size */)
            -> domain_flush_pages_v1()
              -> build_inv_iommu_pages()
                -> build_inv_address()

         }

build_inv_address() will trigger a full invalidation if the chunk size > (1 << 51). Consequently, the guest will issue multiple full invalidations for a single call to amd_iommu_domain_flush_all()

Without this patch, we will see 10 time instead of 1 time full invalidations for every amd_iommu_domain_flush_all().

Quoted 8 lines
Fixes a270be1b3fdf ("iommu/amd: Use only natural aligned flushes in a VM")
---
 drivers/iommu/amd/iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Jason

Reviewed-by on v1 1/1
#5 lore raw

Re: [PATCH v1 1/1] iommu/amd: Don't split flush for amd_iommu_domain_flush_all()

In reply to Weinan Liu
Quoted 38 lines · Weinan Liu

We have observed multiple full invalidations occurring during device detach when we are done using the vfio-device.

blocked_domain_attach_device()
-> detach_device()

     -> amd_iommu_domain_flush_all()
       -> amd_iommu_domain_flush_pages(..., CMD_INV_IOMMU_ALL_PAGES_ADDRESS)

       	while (size != 0) {

           -> __domain_flush_pages( flush_size /* power of 2 flush_size */)
             -> domain_flush_pages_v1()
               -> build_inv_iommu_pages()
                 -> build_inv_address()

          }

build_inv_address() will trigger a full invalidation if the chunk size > (1 << 51). Consequently, the guest will issue multiple full invalidations for a single call to amd_iommu_domain_flush_all()

Without this patch, we will see 10 time instead of 1 time full invalidations for every amd_iommu_domain_flush_all().

Fixes a270be1b3fdf ("iommu/amd: Use only natural aligned flushes in a VM")
---
  drivers/iommu/amd/iommu.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +0 -0
index 760d5f4623b5..59650a44ab50 100644

drivers/iommu/amd/iommu.c void amd_iommu_domain_flush_pages(struct protection_domain *domain, · line 1776

Quoted 9 lines
drivers/iommu/amd/iommu.c +2 -1
@@ -1769,7 +1769,8 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,
  {
  	lockdep_assert_held(&domain->lock);
 
-	if (likely(!amd_iommu_np_cache)) {
+	if (likely(!amd_iommu_np_cache) ||
+		size >= (1ULL<<52)) {
  		__domain_flush_pages(domain, address, size);
 
  		/* Wait until IOMMU TLB and all device IOTLB flushes are complete */
Reviewed-by on v1 1/1
#6 lore raw

Re: [PATCH v1 1/1] iommu/amd: Don't split flush for amd_iommu_domain_flush_all()

In reply to Weinan Liu
Quoted 51 lines · Weinan Liu

We have observed multiple full invalidations occurring during device detach when we are done using the vfio-device.

blocked_domain_attach_device()
-> detach_device()
-> amd_iommu_domain_flush_all()

     -> amd_iommu_domain_flush_pages(..., CMD_INV_IOMMU_ALL_PAGES_ADDRESS)

     	while (size != 0) {

         -> __domain_flush_pages( flush_size /* power of 2 flush_size */)
           -> domain_flush_pages_v1()
             -> build_inv_iommu_pages()
               -> build_inv_address()

        }

build_inv_address() will trigger a full invalidation if the chunk size > (1 << 51). Consequently, the guest will issue multiple full invalidations for a single call to amd_iommu_domain_flush_all()

Without this patch, we will see 10 time instead of 1 time full invalidations for every amd_iommu_domain_flush_all().

Fixes a270be1b3fdf ("iommu/amd: Use only natural aligned flushes in a VM")

---
drivers/iommu/amd/iommu.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +2 -1
index 760d5f4623b5..bcec8721d228 100644
@@ -1769,7 +1769,8 @@ void amd_iommu_domain_flush_pages(struct protection_domain *domain,
 {
 	lockdep_assert_held(&domain->lock);
 
-	if (likely(!amd_iommu_np_cache)) {
+	if (likely(!amd_iommu_np_cache) ||
+		size == CMD_INV_IOMMU_ALL_PAGES_ADDRESS) {
 		__domain_flush_pages(domain, address, size);
 
 		/* Wait until IOMMU TLB and all device IOTLB flushes are complete */
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
reviewer Reviewed-by
#7 lore raw

Re: [PATCH v1 1/1] iommu/amd: Don't split flush for amd_iommu_domain_flush_all()

Quoted 48 lines · Samiullah Khawaja
Quoted 47 lines · Weinan Liu

We have observed multiple full invalidations occurring during device detach when we are done using the vfio-device.

blocked_domain_attach_device()
 -> detach_device()
   -> amd_iommu_domain_flush_all()
     -> amd_iommu_domain_flush_pages(..., CMD_INV_IOMMU_ALL_PAGES_ADDRESS)

         while (size != 0) {

         -> __domain_flush_pages( flush_size /* power of 2 flush_size */) -> domain_flush_pages_v1()
             -> build_inv_iommu_pages()
               -> build_inv_address()

        }

build_inv_address() will trigger a full invalidation  if the chunk size > (1 << 51). Consequently, the guest will issue multiple full invalidations for a single call to  amd_iommu_domain_flush_all()

Without this patch, we will see 10 time instead of 1 time full invalidations for every amd_iommu_domain_flush_all().

Fixes a270be1b3fdf ("iommu/amd: Use only natural aligned flushes in a VM")

---
drivers/iommu/amd/iommu.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +2 -1
index 760d5f4623b5..bcec8721d228 100644
@@ -1769,7 +1769,8 @@ void amd_iommu_domain_flush_pages(struct 
 protection_domain *domain,
 {
     lockdep_assert_held(&domain->lock);
 
-    if (likely(!amd_iommu_np_cache)) {
+    if (likely(!amd_iommu_np_cache) ||
+        size == CMD_INV_IOMMU_ALL_PAGES_ADDRESS) {
         __domain_flush_pages(domain, address, size);
 
Quoted 7 lines
Quoted 5 lines

        /* Wait until IOMMU TLB and all device IOTLB flushes are complete */

Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog

Thanks,

maintainer
#8 lore raw
In reply to Weinan Liu

Hi,

There is an updated patch in this thread, can you please re-send with the correct diff as a new thread and all Acks/Reviewed-by tags added?

Thanks,

Joerg

author
#9 lore raw

Thanks for the feedback. I will update the patch, gather the necessary tags, and re-send it as a new thread shortly.

Weinan

Quoted 9 lines · Jörg Rödel

Hi,

There is an updated patch in this thread, can you please re-send with the correct diff as a new thread and all Acks/Reviewed-by tags added?

Thanks,

Joerg