From patchwork Thu May 25 12:57:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Prutyanov X-Patchwork-Id: 13255196 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E78A5C77B7E for ; Thu, 25 May 2023 12:58:37 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q2AXZ-0003db-Mc; Thu, 25 May 2023 08:58:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q2AXX-0003ct-Fk for qemu-devel@nongnu.org; Thu, 25 May 2023 08:57:59 -0400 Received: from mail-lf1-x12e.google.com ([2a00:1450:4864:20::12e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q2AXV-0004oC-Fg for qemu-devel@nongnu.org; Thu, 25 May 2023 08:57:59 -0400 Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-4f1411e8111so2492829e87.1 for ; Thu, 25 May 2023 05:57:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1685019476; x=1687611476; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kr8tbHJTibD9N/kYZQtUA+ePJsrH6dqFoUFs/8Ufdic=; b=A4pSjeJiSsyiFIvQrGCA3v74x9dg+cNFwdtpR6IvkdNtykQthvQSdLi8H/QPt3asTV Hz/C0i/k9LV5UsHMovIdKpohfYcBn9TriI/9eDAZ9/AMDlA4FOiJfJPo7qJkrzdIBbIF 5TPqYDcTI++HQhsiWLwsTY0MAX/bjcMkd2KYDLLJ4CsD0x2E1VKYRX2fnixOtJgQXaRG nsSwm8tBKUjMy3ppiBcGk4krFMJLuXybTU4DoALE8Sh1rQGoo1dPhAg6QmVcq7WkBq7M zUKWF9SAVKzOfHh4FFrTtkCDLbJdaTJy8rT+DYkUJmxovRbbdyhGXENHpTjFE90GVKTX sZaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685019476; x=1687611476; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kr8tbHJTibD9N/kYZQtUA+ePJsrH6dqFoUFs/8Ufdic=; b=iGwDat6bRrAKhv2sW8LDKERoZxeQqC6ehWoaFvz2EjVcHEos5NTuJho/cdCjaCwa2+ /cL9hQbE6KgWp/oDLg0hDHZRIG/JnoH6nlXXAy3CwVW6kYWepLvGMNuBTNyABx2DnW2h zQBHfYAAZjr5LZVY8VFVyjR/1v9kr2K7qZUzYbiN1CYggND+CDDhZo8yTg2z7IXIx41O zgAve0ZXJYT7O27mSfHWTwdpfqhKFJrtM1YMoflg20wjEvoVHMaCAjUntg3mfn+j2Sex hq69BflNR0nAYevTSQzHZnt+vGQHc63x3ENrjhV+h8bax24eBVvnotDQxBXanWcR7j40 4Hjg== X-Gm-Message-State: AC+VfDxmQXsnFOyjdFm1LwgWkxVkNMGWMmVNK7urbvBz7aWEBsVyl4/+ V8cKtVA04phbV7NK9N8Z+NsZEw== X-Google-Smtp-Source: ACHHUZ7GGuzIkXnAwYko4KbOBi9+RWFfI9jIz5PzX4l8k8TQssoKQhaxSdxh2vsnKF7iCpQEM5l6og== X-Received: by 2002:a19:c517:0:b0:4f1:dcd2:ab6d with SMTP id w23-20020a19c517000000b004f1dcd2ab6dmr6097368lfe.28.1685019475852; Thu, 25 May 2023 05:57:55 -0700 (PDT) Received: from localhost.Dlink ([94.188.60.65]) by smtp.gmail.com with ESMTPSA id r9-20020ac25a49000000b004f1430ee142sm209509lfn.17.2023.05.25.05.57.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 May 2023 05:57:55 -0700 (PDT) From: Viktor Prutyanov To: mst@redhat.com, jasowang@redhat.com Cc: qemu-devel@nongnu.org, yan@daynix.com, yuri.benditovich@daynix.com, Viktor Prutyanov Subject: [PATCH v4 1/2] vhost: register and change IOMMU flag depending on Device-TLB state Date: Thu, 25 May 2023 15:57:41 +0300 Message-Id: <20230525125742.4190-2-viktor@daynix.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230525125742.4190-1-viktor@daynix.com> References: <20230525125742.4190-1-viktor@daynix.com> MIME-Version: 1.0 Received-SPF: none client-ip=2a00:1450:4864:20::12e; envelope-from=viktor@daynix.com; helo=mail-lf1-x12e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org The guest can disable or never enable Device-TLB. In these cases, it can't be used even if enabled in QEMU. So, check Device-TLB state before registering IOMMU notifier and select unmap flag depending on that. Also, implement a way to change IOMMU notifier flag if Device-TLB state is changed. Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2001312 Signed-off-by: Viktor Prutyanov Acked-by: Jason Wang --- hw/virtio/vhost.c | 38 ++++++++++++++++++++++++++------------ include/hw/virtio/vhost.h | 1 + 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 23da579ce2..e48507b5a1 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -781,7 +781,6 @@ static void vhost_iommu_region_add(MemoryListener *listener, Int128 end; int iommu_idx; IOMMUMemoryRegion *iommu_mr; - int ret; if (!memory_region_is_iommu(section->mr)) { return; @@ -796,7 +795,9 @@ static void vhost_iommu_region_add(MemoryListener *listener, iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr, MEMTXATTRS_UNSPECIFIED); iommu_notifier_init(&iommu->n, vhost_iommu_unmap_notify, - IOMMU_NOTIFIER_DEVIOTLB_UNMAP, + dev->vdev->device_iotlb_enabled ? + IOMMU_NOTIFIER_DEVIOTLB_UNMAP : + IOMMU_NOTIFIER_UNMAP, section->offset_within_region, int128_get64(end), iommu_idx); @@ -804,16 +805,8 @@ static void vhost_iommu_region_add(MemoryListener *listener, iommu->iommu_offset = section->offset_within_address_space - section->offset_within_region; iommu->hdev = dev; - ret = memory_region_register_iommu_notifier(section->mr, &iommu->n, NULL); - if (ret) { - /* - * Some vIOMMUs do not support dev-iotlb yet. If so, try to use the - * UNMAP legacy message - */ - iommu->n.notifier_flags = IOMMU_NOTIFIER_UNMAP; - memory_region_register_iommu_notifier(section->mr, &iommu->n, - &error_fatal); - } + memory_region_register_iommu_notifier(section->mr, &iommu->n, + &error_fatal); QLIST_INSERT_HEAD(&dev->iommu_list, iommu, iommu_next); /* TODO: can replay help performance here? */ } @@ -841,6 +834,27 @@ static void vhost_iommu_region_del(MemoryListener *listener, } } +void vhost_toggle_device_iotlb(VirtIODevice *vdev) +{ + VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); + struct vhost_dev *dev; + struct vhost_iommu *iommu; + + if (vdev->vhost_started) { + dev = vdc->get_vhost(vdev); + } else { + return; + } + + QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) { + memory_region_unregister_iommu_notifier(iommu->mr, &iommu->n); + iommu->n.notifier_flags = vdev->device_iotlb_enabled ? + IOMMU_NOTIFIER_DEVIOTLB_UNMAP : IOMMU_NOTIFIER_UNMAP; + memory_region_register_iommu_notifier(iommu->mr, &iommu->n, + &error_fatal); + } +} + static int vhost_virtqueue_set_addr(struct vhost_dev *dev, struct vhost_virtqueue *vq, unsigned idx, bool enable_log) diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index f7f10c8fb7..6a173cb9fa 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -320,6 +320,7 @@ bool vhost_has_free_slot(void); int vhost_net_set_backend(struct vhost_dev *hdev, struct vhost_vring_file *file); +void vhost_toggle_device_iotlb(VirtIODevice *vdev); int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write); int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev, From patchwork Thu May 25 12:57:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viktor Prutyanov X-Patchwork-Id: 13255197 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DE7B7C77B7A for ; Thu, 25 May 2023 12:58:45 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q2AXZ-0003dc-NW; Thu, 25 May 2023 08:58:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q2AXY-0003d9-8B for qemu-devel@nongnu.org; Thu, 25 May 2023 08:58:00 -0400 Received: from mail-lf1-x12d.google.com ([2a00:1450:4864:20::12d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q2AXW-0004oO-O2 for qemu-devel@nongnu.org; Thu, 25 May 2023 08:58:00 -0400 Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-4f4bd608cf4so2511299e87.1 for ; Thu, 25 May 2023 05:57:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20221208.gappssmtp.com; s=20221208; t=1685019476; x=1687611476; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BYrWUYvAm6rLDqyDbruYNdZgW+c/CCYF4lxevzeCCew=; b=oeUmBEo2wQxh7AFs4XkLvtc1IDm+WOrlD2rYTvNv2Mwpmz1IRSFlPZSAlYZzelSAbm 85zQdpX1zOH27jv+D4XjttQ3s+F8GMLCV3g+Skc7bXdTUPUvoIDr+2AMLA66N6R1RXFo 2k+oVDiAGD8v1zajCsJvrC2SyP7W2U9lrRAq58gBKySKfoXFXfrH328xOBELLv6PrTVv ApzlIxRDlLTMOKlDQlf1VhLOLm1G3HYPM5cok1spUNm2z0R9sR1KXytToI0+jWmcBeCl xKcnrmV+sXu/0DlgzNZ+k2xJ4jrCvQEKolHW3X+agyyZ7ykDINXShtr6hU9kdmJmybLU /aNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685019476; x=1687611476; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BYrWUYvAm6rLDqyDbruYNdZgW+c/CCYF4lxevzeCCew=; b=kaki3LzaUlpiLhOBrV4Uu9rEqAvEvmmz9yMO7KHa96s6+hHfV4GKD4T4UTmAxh0ZlI u4RrsR6gZyozWmyIssuGye5iP3Lu4LsL/8oNb1SLCkMEgVsM74xKFc17Y9UJm3zXVeOZ dG2pflqvWSU2LO6IHKFgpQpqpccu628KcLWuHSExJhDlSozVrBFeLq5hJntHTEvM+ZRy QiDV2q+0juPqk6tHLHs8aDrs/u0roT7WlQVAo/yz7uctF1x0AaY7i+6GZZKhx2hYNeeD BvBKpCWBCwPKhOv2s7JNcm9nUldxlLBbn+BzB7yaVcy7bj3T3WkvDjZqHuH6xY1VN7wj SWfg== X-Gm-Message-State: AC+VfDxm8xINc6hzugvMUP0Ufy9CTX7QUhZ8ctQbqZVrVQhEsw+D/tuH VBzZT6VbVzTnrzUixkWxfiZSrA== X-Google-Smtp-Source: ACHHUZ51YQDpolpalGY87jp+y58PUKTz6duUyYfZwdWYpqa2F+sdPIND6G1zj4j6FDYjEvMdCbYnEQ== X-Received: by 2002:a05:6512:2186:b0:4ea:f526:5bef with SMTP id b6-20020a056512218600b004eaf5265befmr7375960lft.28.1685019476777; Thu, 25 May 2023 05:57:56 -0700 (PDT) Received: from localhost.Dlink ([94.188.60.65]) by smtp.gmail.com with ESMTPSA id r9-20020ac25a49000000b004f1430ee142sm209509lfn.17.2023.05.25.05.57.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 May 2023 05:57:56 -0700 (PDT) From: Viktor Prutyanov To: mst@redhat.com, jasowang@redhat.com Cc: qemu-devel@nongnu.org, yan@daynix.com, yuri.benditovich@daynix.com, Viktor Prutyanov Subject: [PATCH v4 2/2] virtio-net: pass Device-TLB enable/disable events to vhost Date: Thu, 25 May 2023 15:57:42 +0300 Message-Id: <20230525125742.4190-3-viktor@daynix.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230525125742.4190-1-viktor@daynix.com> References: <20230525125742.4190-1-viktor@daynix.com> MIME-Version: 1.0 Received-SPF: none client-ip=2a00:1450:4864:20::12d; envelope-from=viktor@daynix.com; helo=mail-lf1-x12d.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org If vhost is enabled for virtio-net, Device-TLB enable/disable events must be passed to vhost for proper IOMMU unmap flag selection. Signed-off-by: Viktor Prutyanov Acked-by: Jason Wang --- hw/net/virtio-net.c | 1 + 1 file changed, 1 insertion(+) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 6df6b7329d..30497c00b5 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -3951,6 +3951,7 @@ static void virtio_net_class_init(ObjectClass *klass, void *data) vdc->vmsd = &vmstate_virtio_net_device; vdc->primary_unplug_pending = primary_unplug_pending; vdc->get_vhost = virtio_net_get_vhost; + vdc->toggle_device_iotlb = vhost_toggle_device_iotlb; } static const TypeInfo virtio_net_info = {