Skip to contentSkip to conversation

issues of region cache and iommu reset

Qemu-devel Jason Wang 5 participants 16 messages Mar 29, 2017 – Mar 30, 2017

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

Hi:

I meet an issue when doing reboot for a guest with two virtio-net-pci cards when iommu is enabled. What happens is:

1) vtd was reset first

2) during the reset of virtio-net-pci #1, deletion of msix subregion will cause a commit of all memory listeners

3) virito-net-pci #2's region cache will be update, but since vtd has already been reset, it can't get a valid mappings here

Any ideas on how to fix this? Need region cache be aware of IOMMU/IOTLB state in this case? Or can we simply reset IOMMU as the last one?

Thanks

#1 lore raw
Quoted 4 lines · Jason Wang

1) vtd was reset first

Quoted 8 lines

2) during the reset of virtio-net-pci #1, deletion of msix subregion will cause a commit of all memory listeners

3) virito-net-pci #2's region cache will be update, but since vtd has already been reset, it can't get a valid mappings here

Any ideas on how to fix this? Need region cache be aware of IOMMU/IOTLB state in this case? Or can we simply reset IOMMU as the last one?

Something like this?

hw/virtio/virtio.c +4 -0
index 03592c5..73e69ac 100644
@@ -176,6 +176,10 @@ err_used:
     address_space_cache_destroy(&new->desc);
 err_desc:
     g_free(new);
+    atomic_rcu_set(&vq->vring.caches, NULL);
+    if (old) {
+        call_rcu(old, virtio_free_region_cache, rcu);
+    }
 }
 
 /* virt queue functions */

Paolo

author
#2 lore raw
Quoted 31 lines · Paolo Bonzini
Quoted 11 lines · Jason Wang

1) vtd was reset first

2) during the reset of virtio-net-pci #1, deletion of msix subregion will cause a commit of all memory listeners

3) virito-net-pci #2's region cache will be update, but since vtd has already been reset, it can't get a valid mappings here

Any ideas on how to fix this? Need region cache be aware of IOMMU/IOTLB state in this case? Or can we simply reset IOMMU as the last one?

Something like this?

hw/virtio/virtio.c +4 -0
index 03592c5..73e69ac 100644
@@ -176,6 +176,10 @@ err_used:
      address_space_cache_destroy(&new->desc);
  err_desc:
      g_free(new);
+    atomic_rcu_set(&vq->vring.caches, NULL);
+    if (old) {
+        call_rcu(old, virtio_free_region_cache, rcu);
+    }
  }
 
  /* virt queue functions */
Quoted 1 line

Paolo

This looks a good fix but may not solve this issue completely. Depends on the iova that guest uses, address_space_cache_init() may succeed even in this case since vtd does a passthrough translation in this case.

Thanks

#3 lore raw
Quoted 15 lines · Jason Wang
Quoted 15 lines
hw/virtio/virtio.c +4 -0
index 03592c5..73e69ac 100644
@@ -176,6 +176,10 @@ err_used:
      address_space_cache_destroy(&new->desc);
  err_desc:
      g_free(new);
+    atomic_rcu_set(&vq->vring.caches, NULL);
+    if (old) {
+        call_rcu(old, virtio_free_region_cache, rcu);
+    }
  }
 
Quoted 7 lines
Quoted 3 lines
hw/virtio/virtio.c +0 -0
  /* virt queue functions */

Paolo

This looks a good fix but may not solve this issue completely. Depends on the iova that guest uses, address_space_cache_init() may succeed even in this case since vtd does a passthrough translation in this case.

Can you explain this more?

Paolo

author
#4 lore raw
Quoted 18 lines · Paolo Bonzini
Quoted 16 lines · Jason Wang
Quoted 16 lines
hw/virtio/virtio.c +4 -0
index 03592c5..73e69ac 100644
@@ -176,6 +176,10 @@ err_used:
       address_space_cache_destroy(&new->desc);
   err_desc:
       g_free(new);
+    atomic_rcu_set(&vq->vring.caches, NULL);
+    if (old) {
+        call_rcu(old, virtio_free_region_cache, rcu);
+    }
   }
 
   /* virt queue functions */
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Paolo

This looks a good fix but may not solve this issue completely. Depends on the iova that guest uses, address_space_cache_init() may succeed even in this case since vtd does a passthrough translation in this case.

Can you explain this more?

Paolo

Yes. In this case, virtio-net-pci is not reset, but vtd has been reset. So virtio-net-pci will still try to use the iova of ring to setup the cache but now vtd treat iova as gpa since dmar has been disabled during reset. (E.g In my test with Linux driver, desc map succeed but used fail.)

Thanks

#5 lore raw
Quoted 10 lines · Jason Wang
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

This looks a good fix but may not solve this issue completely. Depends on the iova that guest uses, address_space_cache_init() may succeed even in this case since vtd does a passthrough translation in this case.

Can you explain this more?

Yes. In this case, virtio-net-pci is not reset, but vtd has been reset. So virtio-net-pci will still try to use the iova of ring to setup the cache but now vtd treat iova as gpa since dmar has been disabled during reset. (E.g In my test with Linux driver, desc map succeed but used fail.)

That would be a Linux bug, the devices below the IOMMU have to be reset first.

Paolo

author
#6 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 4 lines · Jason Wang
Quoted 4 lines
Quoted 3 lines

This looks a good fix but may not solve this issue completely. Depends on the iova that guest uses, address_space_cache_init() may succeed even in this case since vtd does a passthrough translation in this case.

Can you explain this more?

Quoted 8 lines
Quoted 4 lines

Yes. In this case, virtio-net-pci is not reset, but vtd has been reset. So virtio-net-pci will still try to use the iova of ring to setup the cache but now vtd treat iova as gpa since dmar has been disabled during reset. (E.g In my test with Linux driver, desc map succeed but used fail.)

That would be a Linux bug, the devices below the IOMMU have to be reset first.

Paolo

Maybe, but I meet the same issue if I do a "system_reset". I think we should guarantee IOMMU was reset after any pci devices in this case too?

Thanks

#7 lore raw
Quoted 16 lines · Jason Wang
Quoted 13 lines · Paolo Bonzini
Quoted 8 lines · Jason Wang
Quoted 4 lines
Quoted 3 lines

This looks a good fix but may not solve this issue completely. Depends on the iova that guest uses, address_space_cache_init() may succeed even in this case since vtd does a passthrough translation in this case.

Can you explain this more?

Yes. In this case, virtio-net-pci is not reset, but vtd has been reset. So virtio-net-pci will still try to use the iova of ring to setup the cache but now vtd treat iova as gpa since dmar has been disabled during reset. (E.g In my test with Linux driver, desc map succeed but used fail.)

That would be a Linux bug, the devices below the IOMMU have to be reset first.

Quoted 7 lines
Quoted 1 line

Paolo

Maybe, but I meet the same issue if I do a "system_reset". I think we should guarantee IOMMU was reset after any pci devices in this case too?

Thanks

system reset would indeed appear to be a big issue.

Signature · 1 line
MST
#8 lore raw
In reply to Paolo Bonzini
Quoted 21 lines · On Wed, 29 Mar 2017 10:09:10 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 4 lines · Jason Wang

1) vtd was reset first

Quoted 8 lines

2) during the reset of virtio-net-pci #1, deletion of msix subregion will cause a commit of all memory listeners

3) virito-net-pci #2's region cache will be update, but since vtd has already been reset, it can't get a valid mappings here

Any ideas on how to fix this? Need region cache be aware of IOMMU/IOTLB state in this case? Or can we simply reset IOMMU as the last one?

Something like this?

hw/virtio/virtio.c +0 -0
index 03592c5..73e69ac 100644

hw/virtio/virtio.c err_used: · line 182

Quoted 7 lines
hw/virtio/virtio.c +4 -0
@@ -176,6 +176,10 @@ err_used:
     address_space_cache_destroy(&new->desc);
 err_desc:
     g_free(new);
+    atomic_rcu_set(&vq->vring.caches, NULL);
+    if (old) {
+        call_rcu(old, virtio_free_region_cache, rcu);
+    }

Maybe I'm just confused here, but isn't ->broken enough to prevent further accesses? And a reset will unset both ->broken and the caches anyway? What am I missing?

Quoted 6 lines
hw/virtio/virtio.c +0 -0
 }
 
 /* virt queue functions */

Paolo

author
#9 lore raw
Quoted 17 lines · Cornelia Huck
Quoted 14 lines · On Wed, 29 Mar 2017 10:09:10 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 11 lines · Jason Wang

1) vtd was reset first

2) during the reset of virtio-net-pci #1, deletion of msix subregion will cause a commit of all memory listeners

3) virito-net-pci #2's region cache will be update, but since vtd has already been reset, it can't get a valid mappings here

Any ideas on how to fix this? Need region cache be aware of IOMMU/IOTLB state in this case? Or can we simply reset IOMMU as the last one?

Something like this?

Quoted 14 lines

hw/virtio/virtio.c err_used: · line 182

Quoted 12 lines
hw/virtio/virtio.c +4 -0
index 03592c5..73e69ac 100644
@@ -176,6 +176,10 @@ err_used:
      address_space_cache_destroy(&new->desc);
  err_desc:
      g_free(new);
+    atomic_rcu_set(&vq->vring.caches, NULL);
+    if (old) {
+        call_rcu(old, virtio_free_region_cache, rcu);
+    }

Maybe I'm just confused here, but isn't ->broken enough to prevent further accesses?

Looks not (e.g virtio_queue_update_used_idx() that was called by vhost). We only have pfn check for all helpers now.

Quoted 2 lines

And a reset will unset both ->broken and the caches anyway? What am I missing?

Yes, but is it good to store invalid or even wrong mappings in the cache?

Thanks

Quoted 7 lines
Quoted 7 lines
hw/virtio/virtio.c +0 -0
  }
 
  /* virt queue functions */

Paolo

#10 lore raw
Quoted 33 lines · On Wed, 29 Mar 2017 17:18:00 +0800 Jason Wang <[email protected]> wrote:
Quoted 17 lines · Cornelia Huck
Quoted 14 lines · On Wed, 29 Mar 2017 10:09:10 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 11 lines · Jason Wang

1) vtd was reset first

2) during the reset of virtio-net-pci #1, deletion of msix subregion will cause a commit of all memory listeners

3) virito-net-pci #2's region cache will be update, but since vtd has already been reset, it can't get a valid mappings here

Any ideas on how to fix this? Need region cache be aware of IOMMU/IOTLB state in this case? Or can we simply reset IOMMU as the last one?

Something like this?

Quoted 14 lines

hw/virtio/virtio.c err_used: · line 182

Quoted 12 lines
hw/virtio/virtio.c +4 -0
index 03592c5..73e69ac 100644
@@ -176,6 +176,10 @@ err_used:
      address_space_cache_destroy(&new->desc);
  err_desc:
      g_free(new);
+    atomic_rcu_set(&vq->vring.caches, NULL);
+    if (old) {
+        call_rcu(old, virtio_free_region_cache, rcu);
+    }

Maybe I'm just confused here, but isn't ->broken enough to prevent further accesses?

Quoted 1 line

Looks not (e.g virtio_queue_update_used_idx() that was called by vhost).

We probably should take care that we don't do things like that for broken devices. But that ties into the virtio_error() discussion, and we probably want to revisit that in that context.

Quoted 1 line

We only have pfn check for all helpers now.

So we'll assert() after that change?

Quoted 5 lines
Quoted 3 lines

And a reset will unset both ->broken and the caches anyway? What am I missing?

Yes, but is it good to store invalid or even wrong mappings in the cache?

Not really; but I'd like to see ->broken as a kind of master indicator "this device does not work, do not trust/use anything until it has been reset".

author
#11 lore raw
Quoted 34 lines · Cornelia Huck
Quoted 31 lines · On Wed, 29 Mar 2017 17:18:00 +0800 Jason Wang <[email protected]> wrote:
Quoted 30 lines · Cornelia Huck
Quoted 18 lines · On Wed, 29 Mar 2017 10:09:10 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 10 lines · Jason Wang

1) vtd was reset first

2) during the reset of virtio-net-pci #1, deletion of msix subregion will cause a commit of all memory listeners

3) virito-net-pci #2's region cache will be update, but since vtd has already been reset, it can't get a valid mappings here

Any ideas on how to fix this? Need region cache be aware of IOMMU/IOTLB state in this case? Or can we simply reset IOMMU as the last one?

Something like this?

hw/virtio/virtio.c +0 -0
index 03592c5..73e69ac 100644

hw/virtio/virtio.c err_used: · line 182

Quoted 7 lines
hw/virtio/virtio.c +4 -0
@@ -176,6 +176,10 @@ err_used:
       address_space_cache_destroy(&new->desc);
   err_desc:
       g_free(new);
+    atomic_rcu_set(&vq->vring.caches, NULL);
+    if (old) {
+        call_rcu(old, virtio_free_region_cache, rcu);
+    }

Maybe I'm just confused here, but isn't ->broken enough to prevent further accesses?

Quoted 4 lines
Quoted 1 line

Looks not (e.g virtio_queue_update_used_idx() that was called by vhost).

We probably should take care that we don't do things like that for broken devices. But that ties into the virtio_error() discussion, and we probably want to revisit that in that context.

Yes, but the issue here is, the device should not be treated as broken since it does nothing wrong.

Quoted 3 lines
Quoted 2 lines

We only have pfn check for all helpers now.

So we'll assert() after that change?

I think we don't want assert too, since it was a indicator of bug somewhere.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

And a reset will unset both ->broken and the caches anyway? What am I missing?

Yes, but is it good to store invalid or even wrong mappings in the cache?

Not really; but I'd like to see ->broken as a kind of master indicator "this device does not work, do not trust/use anything until it has been reset".

Then I guess we probably check broken for all exported helpers like what we did for gfn.

Thanks

#12 lore raw
In reply to Paolo Bonzini
Quoted 23 lines · Paolo Bonzini
Quoted 4 lines · Jason Wang

1) vtd was reset first

Quoted 8 lines

2) during the reset of virtio-net-pci #1, deletion of msix subregion will cause a commit of all memory listeners

3) virito-net-pci #2's region cache will be update, but since vtd has already been reset, it can't get a valid mappings here

Any ideas on how to fix this? Need region cache be aware of IOMMU/IOTLB state in this case? Or can we simply reset IOMMU as the last one?

Something like this?

hw/virtio/virtio.c +0 -0
index 03592c5..73e69ac 100644

hw/virtio/virtio.c err_used: · line 185

Quoted 10 lines
hw/virtio/virtio.c +4 -0
@@ -176,6 +176,10 @@ err_used:
     address_space_cache_destroy(&new->desc);
 err_desc:
     g_free(new);
+    atomic_rcu_set(&vq->vring.caches, NULL);
+    if (old) {
+        call_rcu(old, virtio_free_region_cache, rcu);
+    }
 }
 
 /* virt queue functions */

I would be worried about call_rcu here - this means something can hang on to and use the old cache, and reset really must act as a sync/flush point.

Quoted 1 line

Paolo

#13 lore raw
Quoted 16 lines · Michael S. Tsirkin
Quoted 16 lines

Something like this?

hw/virtio/virtio.c +4 -0
index 03592c5..73e69ac 100644
@@ -176,6 +176,10 @@ err_used:
     address_space_cache_destroy(&new->desc);
 err_desc:
     g_free(new);
+    atomic_rcu_set(&vq->vring.caches, NULL);
+    if (old) {
+        call_rcu(old, virtio_free_region_cache, rcu);
+    }
 }
 
Quoted 4 lines

hw/virtio/virtio.c err_used: · line 185

Quoted 1 line
hw/virtio/virtio.c +0 -0
 /* virt queue functions */

I would be worried about call_rcu here - this means something can hang on to and use the old cache, and reset really must act as a sync/flush point.

The flush is done later in virtio_reset. Here it's just reacting asynchronously to the IOMMU reset.

I'm thinking of adding a global generation count for IOMMU mappings, and forcing an update when the IOMMU mappings have changed.

Paolo

#14 lore raw
In reply to Jason Wang
Quoted 7 lines · Jason Wang

Hi:

I meet an issue when doing reboot for a guest with two virtio-net-pci cards when iommu is enabled. What happens is:

1) vtd was reset first

I'll add an extra question:

Currently with Q35 and VT-d, our system qtree looks like (simplified version):

bus: system bus
dev: intel-iommu
dev: q35-pcihost
bus: pcie.0
dev: pci device 1
dev: pci device 2
...

Not sure whether it'll be clearer to switch to:

bus: system bus
dev: q35-pcihost
bus: iommu-scope 0
dev: intel-iommu
bus: pcie.0
dev: pci device 1
dev: pci device 2
...

since logically the IOMMU device should be part of q35 pci-host?

Further, if we'll have more intel-iommus in the future in a single guest, we'll be able to have devices dangle under the specific iommu that it belongs. That'll be nice imho.

(Btw, I see that we are using qbus-qdev-qbus-... iterations to
describe the system tree. Why cannot we have qdev under another qdev? Sorry if the question is stupid... Any pointers on explanations to these qdev logics would be appreciated as well.)

Quoted 4 lines

2) during the reset of virtio-net-pci #1, deletion of msix subregion will cause a commit of all memory listeners

Quoted 9 lines

3) virito-net-pci #2's region cache will be update, but since vtd has already been reset, it can't get a valid mappings here

Any ideas on how to fix this? Need region cache be aware of IOMMU/IOTLB state in this case? Or can we simply reset IOMMU as the last one?

Thanks

-- peterx

#15 lore raw
Quoted 35 lines · Peter Xu
Quoted 7 lines · Jason Wang

Hi:

I meet an issue when doing reboot for a guest with two virtio-net-pci cards when iommu is enabled. What happens is:

1) vtd was reset first

I'll add an extra question:

Currently with Q35 and VT-d, our system qtree looks like (simplified version):

bus: system bus
dev: intel-iommu
dev: q35-pcihost
bus: pcie.0
dev: pci device 1
dev: pci device 2
...

Not sure whether it'll be clearer to switch to:

bus: system bus
dev: q35-pcihost
bus: iommu-scope 0
dev: intel-iommu
bus: pcie.0
dev: pci device 1
dev: pci device 2
...

since logically the IOMMU device should be part of q35 pci-host?

Quoted 8 lines

Further, if we'll have more intel-iommus in the future in a single guest, we'll be able to have devices dangle under the specific iommu that it belongs. That'll be nice imho.

(Btw, I see that we are using qbus-qdev-qbus-... iterations to
describe the system tree. Why cannot we have qdev under another qdev? Sorry if the question is stupid... Any pointers on explanations to these qdev logics would be appreciated as well.)

Especially when you have many iommus, it's quite common to have iommus be guest programmable with the bus range that they handle.

They are integrated devices, yes, but they simply aren't buses in the real world. I am guessing what happens on bare metal is that host bridge resets the bus first and then resets itself including the IOMMU.

Quoted 15 lines
Quoted 4 lines

2) during the reset of virtio-net-pci #1, deletion of msix subregion will cause a commit of all memory listeners

Quoted 9 lines

3) virito-net-pci #2's region cache will be update, but since vtd has already been reset, it can't get a valid mappings here

Any ideas on how to fix this? Need region cache be aware of IOMMU/IOTLB state in this case? Or can we simply reset IOMMU as the last one?

Thanks

-- peterx