qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Auger <eric.auger@redhat.com>
To: Jason Wang <jasowang@redhat.com>
Cc: eric.auger.pro@gmail.com, qemu-devel@nongnu.org, mst@redhat.com,
	sgarzare@redhat.com, lvivier@redhat.com,
	zhenzhong.duan@intel.com
Subject: Re: [PATCH] hw/virtio/vhost: Disable IOTLB callbacks when IOMMU gets disabled
Date: Wed, 22 Jan 2025 08:55:24 +0100	[thread overview]
Message-ID: <678babb6-f64a-4db5-ad60-494214a4e673@redhat.com> (raw)
In-Reply-To: <CACGkMEv6ec3JLZg6ZedSHdNS5_McX7_xoV4d2MG05x_Y5t=uEA@mail.gmail.com>

Hi Jason,


On 1/22/25 8:17 AM, Jason Wang wrote:
> On Wed, Jan 22, 2025 at 12:25 AM Eric Auger <eric.auger@redhat.com> wrote:
>>
>> Hi Jason,
>>
>> On 1/21/25 4:27 AM, Jason Wang wrote:
>>> On Tue, Jan 21, 2025 at 1:33 AM Eric Auger <eric.auger@redhat.com> wrote:
>>>> When a guest exposed with a vhost device and protected by an
>>>> intel IOMMU gets rebooted, we sometimes observe a spurious warning:
>>>>
>>>> Fail to lookup the translated address ffffe000
>>>>
>>>> We observe that the IOMMU gets disabled through a write to the global
>>>> command register (CMAR_GCMD.TE) before the vhost device gets stopped.
>>>> When this warning happens it can be observed an inflight IOTLB
>>>> miss occurs after the IOMMU disable and before the vhost stop. In
>>>> that case a flat translation occurs and the check in
>>>> vhost_memory_region_lookup() fails.
>>>>
>>>> Let's disable the IOTLB callbacks when all IOMMU MRs have been
>>>> unregistered.
>>>>
>>>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>>>> ---
>>>>  hw/virtio/vhost.c | 4 ++++
>>>>  1 file changed, 4 insertions(+)
>>>>
>>>> diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
>>>> index 6aa72fd434..128c2ab094 100644
>>>> --- a/hw/virtio/vhost.c
>>>> +++ b/hw/virtio/vhost.c
>>>> @@ -931,6 +931,10 @@ static void vhost_iommu_region_del(MemoryListener *listener,
>>>>              break;
>>>>          }
>>>>      }
>>>> +    if (QLIST_EMPTY(&dev->iommu_list) &&
>>>> +        dev->vhost_ops->vhost_set_iotlb_callback) {
>>>> +        dev->vhost_ops->vhost_set_iotlb_callback(dev, false);
>>>> +    }
>>> So the current code assumes:
>>>
>>> 1) IOMMU is enabled before vhost starts
>>> 2) IOMMU is disabled after vhost stops
>>>
>>> This patch seems to fix 2) but not 1). Do we need to deal with the
>>> IOMMU enabled after vhost starts?
>> sorry I initially misunderstood the above comment. Indeed in the reboot
>> case assumption 2) happens to be wrong. However what I currently do is:
>> stop listening to iotlb miss requests from the kernel because my
>> understanding is those requests are just spurious ones, generate
>> warnings and we do not care since we are rebooting the system.
>>
>> However I do not claim this could handle the case where the IOMMU MR
>> would be turned off and then turned on. I think in that case we should
>> also flush the kernel IOTLB and this is not taken care of in this patch.
>> Is it a relevant use case?
> Not sure.
>
>> wrt removing assumption 1) and allow IOMMU enabled after vhost start. Is
>> that a valid use case as the virtio driver is using the dma api?
> It should not be but we can't assume the behaviour of the guest. It
> could be buggy or even malicious.

agreed
>
> Btw, we had the following codes while handling te:
>
> /* Handle Translation Enable/Disable */
> static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
> {
>     if (s->dmar_enabled == en) {
>         return;
>     }
>
>     trace_vtd_dmar_enable(en);
>
> ...
>
>     vtd_reset_caches(s);
>     vtd_address_space_refresh_all(s);
> }
>
> vtd_address_space_refresh_all() will basically disable the iommu
> memory region. It looks not sufficient to trigger the region_del
> callback, maybe we should delete the region or introduce listener
> callback?

This is exactly the code path which is entered in my use case.

vtd_address_space_refresh_all(s) induces the vhost_iommu_region_del. But given the current implement of this latter the IOTLB callback is not unset and the kernel IOTLB is not refreshed. Also as I pointed out the  hdev->mem->regions are not updated? shouldn't they. Can you explain what they correspond to?

Thanks

Eric

>
> Thanks
>
>> Eric
>>
>>
>>> Thanks
>>>
>>>>  }
>>>>
>>>>  void vhost_toggle_device_iotlb(VirtIODevice *vdev)
>>>> --
>>>> 2.47.1
>>>>



  reply	other threads:[~2025-01-22  7:56 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-20 17:33 [PATCH] hw/virtio/vhost: Disable IOTLB callbacks when IOMMU gets disabled Eric Auger
2025-01-21  3:27 ` Jason Wang
2025-01-21  7:15   ` Eric Auger
2025-01-21 16:25   ` Eric Auger
2025-01-22  7:17     ` Jason Wang
2025-01-22  7:55       ` Eric Auger [this message]
2025-01-23  1:34         ` Jason Wang
2025-01-23  8:31           ` Eric Auger
2025-01-24  1:48             ` Jason Wang
2025-01-24  2:44             ` Duan, Zhenzhong
2025-01-24  3:30               ` Jason Wang
2025-01-24  3:41                 ` Jason Wang
2025-01-24  4:00                   ` Duan, Zhenzhong
2025-01-24  9:20                     ` Jason Wang
2025-01-24  9:50                       ` Duan, Zhenzhong
2025-01-24 17:56                   ` Eric Auger
2025-01-24 15:18                 ` Peter Xu
2025-01-26  7:56                   ` Jason Wang
2025-01-27  0:44                     ` Jason Wang
2025-01-30 17:35                       ` Peter Xu
2025-01-24 17:47               ` Eric Auger
2025-01-26  7:09                 ` Duan, Zhenzhong
2025-01-31  9:55               ` Eric Auger
2025-02-20 15:25                 ` Michael S. Tsirkin
2025-02-20 15:57                   ` Eric Auger
2025-02-20 23:27                     ` Michael S. Tsirkin
2025-01-21  8:31 ` Laurent Vivier
2025-01-21  8:45   ` Stefano Garzarella
2025-01-21  8:49     ` Eric Auger
2025-01-21  8:48   ` Eric Auger
2025-01-21 16:32   ` Eric Auger
2025-01-21  9:18 ` Duan, Zhenzhong
2025-01-21 10:34   ` Eric Auger
2025-01-21 10:43     ` Duan, Zhenzhong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=678babb6-f64a-4db5-ad60-494214a4e673@redhat.com \
    --to=eric.auger@redhat.com \
    --cc=eric.auger.pro@gmail.com \
    --cc=jasowang@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=mst@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=sgarzare@redhat.com \
    --cc=zhenzhong.duan@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).