Skip to contentSkip to conversation

iommu/amd-vi: remove zeroing of MMIO region

PATCH Superseded Roger Pau Monne 3 participants 15 messages May 6

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover iommu/amd-vi: remove zeroing of MMIO region

Hello,

Unconditionally zeroing the whole IOMMU MMIO region is dangerous, at least on an upcoming platform this puts the IOMMU in a broken state that's not recovered by the init procedure in Xen.

Get rid of the zeroing, and instead attempt to disable the IOMMU ahead of enabling it.

Thanks, Roger.

Roger Pau Monne (2):
iommu/amd-vi: allow disable_iommu() against non-initialized IOMMUs iommu/amd-vi: do not zero IOMMU MMIO region

 xen/drivers/passthrough/amd/iommu_init.c | 34 +++++++++++-------------
 1 file changed, 15 insertions(+), 19 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 iommu/amd-vi: allow disable_iommu() against non-initialized IOMMUs

Introduce a force option to disable_iommu() that allows it to disable the IOMMU, even when ->enabled is not set. While there remove the unlikely(), this is not a hot path anyway.

No functional change, as there are no current callers that pass force == true.

---
 xen/drivers/passthrough/amd/iommu_init.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/drivers/passthrough/amd/iommu_init.c +5 -5
index d77dd8511288..76ae78e5ea53 100644
@@ -929,13 +929,13 @@ static void enable_iommu(struct amd_iommu *iommu)
         amd_iommu_flush_all_caches(iommu);
 }
 
-static void disable_iommu(struct amd_iommu *iommu)
+static void disable_iommu(struct amd_iommu *iommu, bool force)
 {
     unsigned long flags;
 
     spin_lock_irqsave(&iommu->lock, flags);
 
-    if ( unlikely(!iommu->enabled) )
+    if ( !iommu->enabled && !force )
         goto out;
 
     if ( !iommu->ctrl.int_cap_xt_en )
@@ -1139,7 +1139,7 @@ static void __init amd_iommu_init_cleanup(void)
         iommu->ctrl.int_cap_xt_en = 0;
 
         if ( iommu->enabled )
-            disable_iommu(iommu);
+            disable_iommu(iommu, false);
         else if ( iommu->mmio_base )
             writeq(iommu->ctrl.raw,
                    iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
@@ -1584,7 +1584,7 @@ void cf_check amd_iommu_crash_shutdown(void)
     struct amd_iommu *iommu;
 
     for_each_amd_iommu ( iommu )
-        disable_iommu(iommu);
+        disable_iommu(iommu, false);
 }
 
 void cf_check amd_iommu_resume(void)
@@ -1598,7 +1598,7 @@ void cf_check amd_iommu_resume(void)
         * To make sure that iommus have not been touched 
         * before re-enablement
         */
-        disable_iommu(iommu);
+        disable_iommu(iommu, false);
         enable_iommu(iommu);
         if ( !iommu->features.flds.ia_sup )
             invalidate_all = false;
Signature · 1 line
2.53.0
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] iommu/amd-vi: allow disable_iommu() against non-initialized IOMMUs

Quoted 8 lines · Roger Pau Monne

Introduce a force option to disable_iommu() that allows it to disable the IOMMU, even when ->enabled is not set. While there remove the unlikely(), this is not a hot path anyway.

No functional change, as there are no current callers that pass force == true.

#3 lore raw

PATCH 2/2 iommu/amd-vi: do not zero IOMMU MMIO region

In reply to Roger Pau Monne

Attempting to memset the whole IOMMU MMIO region to zero is dangerous to say the least. We don't know what registers might be there, neither what values might be safe for those registers. On a forthcoming platform doing the zeroing of the MMIO region can put the IOMMU in a broken state, which is not recovered by the IOMMU initialization procedure in Xen.

Instead attempt to forcefully disable the IOMMU ahead of enabling it. Fold map_iommu_mmio_region() into it's only caller, as the function body is just an ioremap() call after the removal of the memset().

Fixes 0700c962ac2d ("Add AMD IOMMU support into hypervisor")
---
 xen/drivers/passthrough/amd/iommu_init.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
xen/drivers/passthrough/amd/iommu_init.c +10 -14
index 76ae78e5ea53..8bf5ca4de18f 100644
@@ -42,18 +42,6 @@ static bool iommu_has_ht_flag(struct amd_iommu *iommu, u8 mask)
     return iommu->ht_flags & mask;
 }
 
-static int __init map_iommu_mmio_region(struct amd_iommu *iommu)
-{
-    iommu->mmio_base = ioremap(iommu->mmio_base_phys,
-                               IOMMU_MMIO_REGION_LENGTH);
-    if ( !iommu->mmio_base )
-        return -ENOMEM;
-
-    memset(iommu->mmio_base, 0, IOMMU_MMIO_REGION_LENGTH);
-
-    return 0;
-}
-
 static void __init unmap_iommu_mmio_region(struct amd_iommu *iommu)
 {
     if ( iommu->mmio_base )
@@ -1367,11 +1355,14 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
 {
     int rc = alloc_ivrs_mappings(iommu->sbdf.seg);
 
-    if ( !rc )
-        rc = map_iommu_mmio_region(iommu);
     if ( rc )
         return rc;
 
+    iommu->mmio_base = ioremap(iommu->mmio_base_phys,
+                               IOMMU_MMIO_REGION_LENGTH);
+    if ( !iommu->mmio_base )
+        return -ENOMEM;
+
     get_iommu_features(iommu);
 
     /*
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);
+    iommu->ctrl.raw = 0;
+
     return 0;
 }
 
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

Quoted 3 lines · Roger Pau Monne

Attempting to memset the whole IOMMU MMIO region to zero is dangerous to say the least. We don't know what registers might be there, neither what values might be safe for those registers.

Minor grammar.  "there, nor which values".

Quoted 2 lines

On a forthcoming platform doing
the zeroing of the MMIO region can put the IOMMU in a broken state,

"does put"

Quoted 1 line

which is not recovered by the IOMMU initialization procedure in Xen.

"recoverable".

Quoted 22 lines
xen/drivers/passthrough/amd/iommu_init.c +5 -2
index 76ae78e5ea53..8bf5ca4de18f 100644
@@ -1367,11 +1355,14 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
 {
     int rc = alloc_ivrs_mappings(iommu->sbdf.seg);
 
-    if ( !rc )
-        rc = map_iommu_mmio_region(iommu);
     if ( rc )
         return rc;
 
+    iommu->mmio_base = ioremap(iommu->mmio_base_phys,
+                               IOMMU_MMIO_REGION_LENGTH);
+    if ( !iommu->mmio_base )
+        return -ENOMEM;
+
     get_iommu_features(iommu);
 
     /*

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1381

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_init.c +5 -0
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);
+    iommu->ctrl.raw = 0;
+
     return 0;
 } 

These two things are unrelated at want splitting into separate patches at a minimum.  The removal of memset() critically needs backporting.

As for disabling the IOMMU, I'm not certain it's wise.

Linux can already "bring up" an already-live IOMMU and Xen needs to gain this ability in due course.  This is mainly for supporting PreBoot DMA Protection, but also for things like the kexec environment.

~Andrew

#5 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

Quoted 33 lines · Andrew Cooper
Quoted 18 lines · Roger Pau Monne
xen/drivers/passthrough/amd/iommu_init.c +5 -2
@@ -1367,11 +1355,14 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
 {
     int rc = alloc_ivrs_mappings(iommu->sbdf.seg);
 
-    if ( !rc )
-        rc = map_iommu_mmio_region(iommu);
     if ( rc )
         return rc;
 
+    iommu->mmio_base = ioremap(iommu->mmio_base_phys,
+                               IOMMU_MMIO_REGION_LENGTH);
+    if ( !iommu->mmio_base )
+        return -ENOMEM;
+
     get_iommu_features(iommu);
 
     /*

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1381

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_init.c +5 -0
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);
+    iommu->ctrl.raw = 0;
+
     return 0;
 } 

These two things are unrelated at want splitting into separate patches at a minimum.  The removal of memset() critically needs backporting.

Quoted 5 lines

As for disabling the IOMMU, I'm not certain it's wise.

Linux can already "bring up" an already-live IOMMU and Xen needs to gain this ability in due course.  This is mainly for supporting PreBoot DMA Protection, but also for things like the kexec environment.

While I agree we would better support this, as per my reply to Roger: How is that going to work if the IOMMU has features enabled we may not even be aware of? We'd still need to blindly clear everything we can't drive ourselves.

Jan

#6 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

Quoted 35 lines · Jan Beulich
Quoted 34 lines · Andrew Cooper
Quoted 18 lines · Roger Pau Monne
xen/drivers/passthrough/amd/iommu_init.c +5 -2
@@ -1367,11 +1355,14 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
 {
     int rc = alloc_ivrs_mappings(iommu->sbdf.seg);
 
-    if ( !rc )
-        rc = map_iommu_mmio_region(iommu);
     if ( rc )
         return rc;
 
+    iommu->mmio_base = ioremap(iommu->mmio_base_phys,
+                               IOMMU_MMIO_REGION_LENGTH);
+    if ( !iommu->mmio_base )
+        return -ENOMEM;
+
     get_iommu_features(iommu);
 
     /*

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1381

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_init.c +5 -0
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);
+    iommu->ctrl.raw = 0;
+
     return 0;
 } 

These two things are unrelated at want splitting into separate patches at a minimum.  The removal of memset() critically needs backporting.

As for disabling the IOMMU, I'm not certain it's wise.

Quoted 7 lines
Quoted 3 lines

Linux can already "bring up" an already-live IOMMU and Xen needs to gain this ability in due course.  This is mainly for supporting PreBoot DMA Protection, but also for things like the kexec environment.

While I agree we would better support this, as per my reply to Roger: How is that going to work if the IOMMU has features enabled we may not even be aware of? We'd still need to blindly clear everything we can't drive ourselves.

Zeroing 16k of unknown MMIO is completely unreasonable.  It is not RAM, and 0 is not a safe thing to write into an unknown register.

But to the AMD IOMMU specifically, the spec makes it clear that there are registers configured by firmware that we are expected to leave alone.

~Andrew

#7 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

Quoted 39 lines · Andrew Cooper
Quoted 38 lines · Jan Beulich
Quoted 37 lines · Andrew Cooper
Quoted 18 lines · Roger Pau Monne
xen/drivers/passthrough/amd/iommu_init.c +5 -2
@@ -1367,11 +1355,14 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
 {
     int rc = alloc_ivrs_mappings(iommu->sbdf.seg);
 
-    if ( !rc )
-        rc = map_iommu_mmio_region(iommu);
     if ( rc )
         return rc;
 
+    iommu->mmio_base = ioremap(iommu->mmio_base_phys,
+                               IOMMU_MMIO_REGION_LENGTH);
+    if ( !iommu->mmio_base )
+        return -ENOMEM;
+
     get_iommu_features(iommu);
 
     /*

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1381

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_init.c +5 -0
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);
+    iommu->ctrl.raw = 0;
+
     return 0;
 } 

These two things are unrelated at want splitting into separate patches at a minimum.  The removal of memset() critically needs backporting.

As for disabling the IOMMU, I'm not certain it's wise.

Linux can already "bring up" an already-live IOMMU and Xen needs to gain this ability in due course.  This is mainly for supporting PreBoot DMA Protection, but also for things like the kexec environment.

Quoted 7 lines
Quoted 4 lines

While I agree we would better support this, as per my reply to Roger: How is that going to work if the IOMMU has features enabled we may not even be aware of? We'd still need to blindly clear everything we can't drive ourselves.

Zeroing 16k of unknown MMIO is completely unreasonable.  It is not RAM, and 0 is not a safe thing to write into an unknown register.

From a very general perspective I agree. However, when adding new registers (or new bits in existing ones), having them default to 0 (and hence making 0 be a valid value) is common practice.

Quoted 2 lines

But to the AMD IOMMU specifically, the spec makes it clear that there are registers configured by firmware that we are expected to leave alone.

Well, okay. For firmware settings I think we can assume these would indeed be settings, not enables of any features which would typically require driving by an OS. Yet that still leaves the question (along the lines of what I had raised before) of how we'd deal with being invoked with unknown to us features enabled. We need to disable them, yet how do you suggest doing that without blindly clearing most (if not all) registers? The only clean way of doing that would look to be a "soft reset" command to the IOMMU (of course not to be issued via the command queue). I'm unaware of anything like this, though.

Jan

#8 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

In reply to Andrew Cooper
Quoted 41 lines · Andrew Cooper
Quoted 3 lines · Roger Pau Monne

Attempting to memset the whole IOMMU MMIO region to zero is dangerous to say the least. We don't know what registers might be there, neither what values might be safe for those registers.

Minor grammar.  "there, nor which values".

Quoted 2 lines

On a forthcoming platform doing
the zeroing of the MMIO region can put the IOMMU in a broken state,

"does put"

Quoted 1 line

which is not recovered by the IOMMU initialization procedure in Xen.

"recoverable".

Quoted 25 lines
xen/drivers/passthrough/amd/iommu_init.c +5 -2
index 76ae78e5ea53..8bf5ca4de18f 100644
@@ -1367,11 +1355,14 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
 {
     int rc = alloc_ivrs_mappings(iommu->sbdf.seg);
 
-    if ( !rc )
-        rc = map_iommu_mmio_region(iommu);
     if ( rc )
         return rc;
 
+    iommu->mmio_base = ioremap(iommu->mmio_base_phys,
+                               IOMMU_MMIO_REGION_LENGTH);
+    if ( !iommu->mmio_base )
+        return -ENOMEM;
+
     get_iommu_features(iommu);
 
     /*
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
Quoted 10 lines

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1381

Quoted 7 lines
xen/drivers/passthrough/amd/iommu_init.c +5 -0
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);
+    iommu->ctrl.raw = 0;
+
     return 0;
 } 

These two things are unrelated at want splitting into separate patches at a minimum.  The removal of memset() critically needs backporting.

But is it safe to backport the memset without also backporting the disabling side? We might then be dealing with an enabled IOMMU which could lead to all sorts of fun.

Quoted 5 lines

As for disabling the IOMMU, I'm not certain it's wise.

Linux can already "bring up" an already-live IOMMU and Xen needs to gain this ability in due course.  This is mainly for supporting PreBoot DMA Protection, but also for things like the kexec environment.

Note that Linux (when not booted from kdump) will do a similar sequence of what I'm attempting to do here for Xen and will call iommu_disable() ahead of attempting to enable the IOMMU.

I understand that when Xen supports kdump or preboot DMA protection we would need to be more careful there, but going from zeroing the whole MMIO area (which disables everything) to not doing any kind of disabling is IMO a dangerous approach, and I would rather backport a change that at least attempts to make sure the IOMMU is disabled.

Thanks, Roger.

#9 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

Quoted 48 lines · Roger Pau Monné
Quoted 38 lines · Andrew Cooper
Quoted 3 lines · Roger Pau Monne

Attempting to memset the whole IOMMU MMIO region to zero is dangerous to say the least. We don't know what registers might be there, neither what values might be safe for those registers.

Minor grammar.  "there, nor which values".

Quoted 2 lines

On a forthcoming platform doing
the zeroing of the MMIO region can put the IOMMU in a broken state,

"does put"

Quoted 1 line

which is not recovered by the IOMMU initialization procedure in Xen.

"recoverable".

Quoted 25 lines
xen/drivers/passthrough/amd/iommu_init.c +5 -2
index 76ae78e5ea53..8bf5ca4de18f 100644
@@ -1367,11 +1355,14 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
 {
     int rc = alloc_ivrs_mappings(iommu->sbdf.seg);
 
-    if ( !rc )
-        rc = map_iommu_mmio_region(iommu);
     if ( rc )
         return rc;
 
+    iommu->mmio_base = ioremap(iommu->mmio_base_phys,
+                               IOMMU_MMIO_REGION_LENGTH);
+    if ( !iommu->mmio_base )
+        return -ENOMEM;
+
     get_iommu_features(iommu);
 
     /*
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
Quoted 9 lines

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1381

Quoted 7 lines
xen/drivers/passthrough/amd/iommu_init.c +5 -0
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);
+    iommu->ctrl.raw = 0;
+
     return 0;
 } 

These two things are unrelated at want splitting into separate patches at a minimum.  The removal of memset() critically needs backporting.

Quoted 3 lines

But is it safe to backport the memset without also backporting the disabling side? We might then be dealing with an enabled IOMMU which could lead to all sorts of fun.

I would absolutely take working with a potentially active IOMMU over zeroing the registers which don't appear in the public documentation.

This is where BIOS norms save us.  The IOMMU can't be enabled without active OS negotiation, or DoS wouldn't be able to boot.  It is only in the past couple of years where this has not been the base compatibility case on all systems.

But I see your point.  Given that we were clearing the main enable bit, we ought to continue to do so for backport purposes.

It would be nice to have a printk_once() in there so we can spot when the IOMMU is enabled.  I expect it not to trigger but if it does trigger, we've got more investigation to do.

Quoted 8 lines
Quoted 5 lines

As for disabling the IOMMU, I'm not certain it's wise.

Linux can already "bring up" an already-live IOMMU and Xen needs to gain this ability in due course.  This is mainly for supporting PreBoot DMA Protection, but also for things like the kexec environment.

Note that Linux (when not booted from kdump) will do a similar sequence of what I'm attempting to do here for Xen and will call iommu_disable() ahead of attempting to enable the IOMMU.

Lovely...  That's broken.

The AMD IOMMU makes this especially easy to do.  Each of the ring buffers have enable bits separate to the general IOMMU enable, so you can temporary disable, move the buffer and clear the ring pointers, then re-enable.

What is critical for Preboot DMA protection is that DMA translation doesn't get turned off.

~Andrew

#10 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

In reply to Roger Pau Monne

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1377

Quoted 7 lines · Roger Pau Monne
xen/drivers/passthrough/amd/iommu_init.c +3 -0
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);

Don't you also need to pre-fill iommu->features? And with that field's use in disable_iommu(), won't we be at risk of leaving stuff enabled which we are entirely unaware of? Even if we fully cleared the control register (which would eliminate the need to fetch features), down the road a 2nd control register could appear. Has it become clear which register(s) or bit(s) it really is that are causing the observed issue? IOW is there truly something we may not clear? Or is it maybe that memset() really isn't suitable for use against MMIO, especially after having switched to use of REP MOVSB when ERMS is available?

Jan

#11 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

Quoted 10 lines · Jan Beulich

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1377

Quoted 7 lines · Roger Pau Monne
xen/drivers/passthrough/amd/iommu_init.c +3 -0
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);

Don't you also need to pre-fill iommu->features?

Indeed, that's done just ahead of this chunk, in the get_iommu_features() call.

Quoted 3 lines

And with that field's use in
disable_iommu(), won't we be at risk of leaving stuff enabled which we are entirely unaware of?

Possibly, yes, that's always a risk.

Quoted 3 lines

Even if we fully cleared the control register (which
would eliminate the need to fetch features), down the road a 2nd control register could appear.

We do clear the control register, it's indirectly done by us setting iommu->ctrl.raw = 0 after the disable_iommu() call.

I did wonder about just doing a write of 0 to the control register, but I think it's best if we try to gracefully disable the features (as done in disable_iommu()), and then reset the cached control state to
0. Future writes to the control register will clear any bits not directly set by Xen.

Quoted 3 lines

Has it become clear which register(s) or bit(s) it
really is that are causing the observed issue? IOW is there truly something we may not clear?

It's not so much as not clearing something, but rather the clearing itself putting the IOMMU in a broken state which we then can't recover from using the initialization procedure in Xen.

Thanks, Roger.

#12 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

Quoted 25 lines · Roger Pau Monné
Quoted 10 lines · Jan Beulich

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1377

Quoted 7 lines · Roger Pau Monne
xen/drivers/passthrough/amd/iommu_init.c +3 -0
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);

Don't you also need to pre-fill iommu->features?

Indeed, that's done just ahead of this chunk, in the get_iommu_features() call.

Quoted 3 lines

And with that field's use in
disable_iommu(), won't we be at risk of leaving stuff enabled which we are entirely unaware of?

Possibly, yes, that's always a risk.

Quoted 3 lines

Even if we fully cleared the control register (which
would eliminate the need to fetch features), down the road a 2nd control register could appear.

Quoted 8 lines

We do clear the control register, it's indirectly done by us setting iommu->ctrl.raw = 0 after the disable_iommu() call.

I did wonder about just doing a write of 0 to the control register, but I think it's best if we try to gracefully disable the features (as done in disable_iommu()), and then reset the cached control state to
0. Future writes to the control register will clear any bits not directly set by Xen.

Maybe better to explicitly write out that 0 right away, even if you want to keep using disable_iommu()?

Jan

#13 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

Quoted 29 lines · Jan Beulich
Quoted 28 lines · Roger Pau Monné
Quoted 10 lines · Jan Beulich

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1377

Quoted 7 lines · Roger Pau Monne
xen/drivers/passthrough/amd/iommu_init.c +3 -0
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);

Don't you also need to pre-fill iommu->features?

Indeed, that's done just ahead of this chunk, in the get_iommu_features() call.

Quoted 3 lines

And with that field's use in
disable_iommu(), won't we be at risk of leaving stuff enabled which we are entirely unaware of?

Possibly, yes, that's always a risk.

Quoted 3 lines

Even if we fully cleared the control register (which
would eliminate the need to fetch features), down the road a 2nd control register could appear.

We do clear the control register, it's indirectly done by us setting iommu->ctrl.raw = 0 after the disable_iommu() call.

Quoted 8 lines
Quoted 5 lines

I did wonder about just doing a write of 0 to the control register, but I think it's best if we try to gracefully disable the features (as done in disable_iommu()), and then reset the cached control state to
0. Future writes to the control register will clear any bits not directly set by Xen.

Maybe better to explicitly write out that 0 right away, even if you want to keep using disable_iommu()?

Yeah, I also considered that. So after disable_iommu() set the cached control state to 0 and also zero the control register right there. Can do in the next version, unless there are further objections.

Thanks, Roger.

#14 lore raw

Re: [PATCH 2/2] iommu/amd-vi: do not zero IOMMU MMIO region

Quoted 36 lines · Roger Pau Monné
Quoted 35 lines · Jan Beulich
Quoted 33 lines · Roger Pau Monné
Quoted 10 lines · Jan Beulich

xen/drivers/passthrough/amd/iommu_init.c static int __init amd_iommu_prepare_one(struct amd_iommu *iommu) · line 1377

Quoted 7 lines · Roger Pau Monne
xen/drivers/passthrough/amd/iommu_init.c +3 -0
@@ -1381,6 +1372,11 @@ static int __init amd_iommu_prepare_one(struct amd_iommu *iommu)
     if ( amd_iommu_max_paging_mode < amd_iommu_min_paging_mode )
         return -ERANGE;
 
+    /* Read current control register and forcefully disable the IOMMU. */
+    iommu->ctrl.raw = readq(iommu->mmio_base + IOMMU_CONTROL_MMIO_OFFSET);
+    disable_iommu(iommu, true);

Don't you also need to pre-fill iommu->features?

Indeed, that's done just ahead of this chunk, in the get_iommu_features() call.

Quoted 3 lines

And with that field's use in
disable_iommu(), won't we be at risk of leaving stuff enabled which we are entirely unaware of?

Possibly, yes, that's always a risk.

Quoted 3 lines

Even if we fully cleared the control register (which
would eliminate the need to fetch features), down the road a 2nd control register could appear.

We do clear the control register, it's indirectly done by us setting iommu->ctrl.raw = 0 after the disable_iommu() call.

I did wonder about just doing a write of 0 to the control register, but I think it's best if we try to gracefully disable the features (as done in disable_iommu()), and then reset the cached control state to
0. Future writes to the control register will clear any bits not directly set by Xen.

Quoted 6 lines
Quoted 2 lines

Maybe better to explicitly write out that 0 right away, even if you want to keep using disable_iommu()?

Yeah, I also considered that. So after disable_iommu() set the cached control state to 0 and also zero the control register right there. Can do in the next version, unless there are further objections.

Well, as said - I'm wary of fully dropping the memset(). It may help now, but it could easily cause issues later.

Jan