From nobody Sat May 18 22:54:10 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1612467439; cv=none; d=zohomail.com; s=zohoarc; b=Nyh/1+4L3tIkQdz5MmqKhFE1ANkCT6J2NdZR4RqlyjYdXro7xkrkDzZ/9wQvZenvtRZMOG6jZTiI73iyEOEHEIZlZspX9gyqJnYpd7dTk4wYj2iFPenTyf4KhJiBKB5/Z+0rlOjr5gM9U7Kn+cemi4kvBWnJ3IHVvfUN/GtnXHA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612467439; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=wq1oNNb6es5putCafMaMjx/H+TYOyFtHTNVfvZIqlXQ=; b=cyH+wDB9Sy/ywAjXzZSCgKMlCtizIC/FRbJevG3+V5aJMLyMJL7MNBrtFSVYYlSbXgvQnzCVPT7C5eRlMOdK4iVe94qt17F/m9dE088F6Re1fEf8D1+mCwUL/i3WwEeujz3ASmcglilPgL/C3PVB7Jf3Ewej7Zn8U4/brWwAwoc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1612467439408453.80181407180544; Thu, 4 Feb 2021 11:37:19 -0800 (PST) Received: from localhost ([::1]:43982 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l7kRK-0007Tt-Bw for importer@patchew.org; Thu, 04 Feb 2021 14:37:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49030) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l7k3a-0006oG-JO for qemu-devel@nongnu.org; Thu, 04 Feb 2021 14:12:46 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:49019) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l7k3P-0003IZ-6b for qemu-devel@nongnu.org; Thu, 04 Feb 2021 14:12:46 -0500 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-407-mc9JYkYRPjqYmF6iwxl69g-1; Thu, 04 Feb 2021 14:12:31 -0500 Received: by mail-qt1-f198.google.com with SMTP id v13so3365448qtq.18 for ; Thu, 04 Feb 2021 11:12:31 -0800 (PST) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-20-174-93-89-182.dsl.bell.ca. [174.93.89.182]) by smtp.gmail.com with ESMTPSA id k8sm6220122qkk.79.2021.02.04.11.12.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Feb 2021 11:12:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612465953; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=wq1oNNb6es5putCafMaMjx/H+TYOyFtHTNVfvZIqlXQ=; b=GpAyHUu39Zz9oxv7gpzUZnmZJje4j+F3n8QzmhAT0gEtg3r6RrzmtqHl3ksXzvlxKOY7Cg xfiy0ih7szvGd3KARzt/3Bx2iWQDZawf34WqUz9MU+jvla+X0/KdCo2g2ac/zOfmEXcFzW WkbqhzKwurpcX++ETWpNnu53a8BURt8= X-MC-Unique: mc9JYkYRPjqYmF6iwxl69g-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=wq1oNNb6es5putCafMaMjx/H+TYOyFtHTNVfvZIqlXQ=; b=OS4JYJYYofx6D86Nr7N/x9HmAUgtinQO3xbo1CJIcRbjwjWkCrpUKrZM4fMxJFw9kz +4qEiJKyWlzzWwQPodRX9iqdi0FpwtU+JfudUVlpTLaNB/wtUrBQzFwAB7z1VNG/xlX3 WGWJJ+4csx+9OJnq1XTNcPgEHWCLjTN2Mxe8UBx7bAZZAn1qnuOTWTRUoREa3MLYSn4b RUr3r+2LhJ/Us7OO7KGyZs1yq1xlazDOBTtCFO/VoF8m2otBOBPozUPOuGJFxQQKsse5 Sa9rZPSFBk6eTYINPc753BWSydOSkNCCVUfgX9Gyquff4cmA+RSjcNFSRWr6SIswewvf q/6A== X-Gm-Message-State: AOAM533BsXe1FTRfzMsJnv0qxAafOnMpuOrMOM2Rbpp9kPuEvyc2ggLI 2xCjsJU36xI9zEHVkKHErFjH6jrviChjDai7NsbPfnNCSA0y1OohCenrQmyxj7hiA/VVQVmHcf7 zmiUCTAytk1zdZdTvQaNFrrvWB8TCdprwhHyDV3kMmgSXgAhcDpGZ5TbnO17TYF2D X-Received: by 2002:ac8:5189:: with SMTP id c9mr1016147qtn.241.1612465950922; Thu, 04 Feb 2021 11:12:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJwwIptZK7bql5eMTeTTrmSHEQHwypTIdWETmGMAkytwTmSMLM3O7P6oEHLbdK7ADTJyPpHlvA== X-Received: by 2002:ac8:5189:: with SMTP id c9mr1016117qtn.241.1612465950629; Thu, 04 Feb 2021 11:12:30 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Subject: [PATCH] vhost: Unbreak SMMU and virtio-iommu on dev-iotlb support Date: Thu, 4 Feb 2021 14:12:28 -0500 Message-Id: <20210204191228.187550-1-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.351, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eric Auger , Jason Wang , "Michael S . Tsirkin" , peterx@redhat.com, Eugenio Perez Martin Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Previous work on dev-iotlb message broke vhost on either SMMU or virtio-iom= mu since dev-iotlb (or PCIe ATS) is not yet supported for those archs. An initial idea is that we can let IOMMU to export this information to vhos= t so that vhost would know whether the vIOMMU would support dev-iotlb, then vhost can conditionally register to dev-iotlb or the old iotlb way. We can work based on some previous patch to introduce PCIIOMMUOps as Yi Liu proposed [1= ]. However it's not as easy as I thought since vhost_iommu_region_add() does n= ot have a PCIDevice context at all since it's completely a backend. It seems non-trivial to pass over a PCI device to the backend during init. E.g. when the IOMMU notifier registered hdev->vdev is still NULL. To make the fix smaller and easier, this patch goes the other way to levera= ge the flag_changed() hook of vIOMMUs so that SMMU and virtio-iommu can trap t= he dev-iotlb registration and fail it. Then vhost could try the fallback solu= tion as using UNMAP invalidation for it's translations. [1] https://lore.kernel.org/qemu-devel/1599735398-6829-4-git-send-email-yi.= l.liu@intel.com/ Reported-by: Eric Auger Fixes: b68ba1ca57677acf870d5ab10579e6105c1f5338 Reviewed-by: Eric Auger Tested-by: Eric Auger Signed-off-by: Peter Xu --- hw/arm/smmuv3.c | 5 +++++ hw/virtio/vhost.c | 13 +++++++++++-- hw/virtio/virtio-iommu.c | 5 +++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/hw/arm/smmuv3.c b/hw/arm/smmuv3.c index 98b99d4fe8e..bd1f97000d9 100644 --- a/hw/arm/smmuv3.c +++ b/hw/arm/smmuv3.c @@ -1497,6 +1497,11 @@ static int smmuv3_notify_flag_changed(IOMMUMemoryReg= ion *iommu, SMMUv3State *s3 =3D sdev->smmu; SMMUState *s =3D &(s3->smmu_state); =20 + if (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP) { + error_setg(errp, "SMMUv3 does not support dev-iotlb yet"); + return -EINVAL; + } + if (new & IOMMU_NOTIFIER_MAP) { error_setg(errp, "device %02x.%02x.%x requires iommu MAP notifier which = is " diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 28c7d781721..6e17d631f77 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -704,6 +704,7 @@ static void vhost_iommu_region_add(MemoryListener *list= ener, Int128 end; int iommu_idx; IOMMUMemoryRegion *iommu_mr; + int ret; =20 if (!memory_region_is_iommu(section->mr)) { return; @@ -726,8 +727,16 @@ static void vhost_iommu_region_add(MemoryListener *lis= tener, iommu->iommu_offset =3D section->offset_within_address_space - section->offset_within_region; iommu->hdev =3D dev; - memory_region_register_iommu_notifier(section->mr, &iommu->n, - &error_fatal); + ret =3D 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 t= he + * UNMAP legacy message + */ + iommu->n.notifier_flags =3D IOMMU_NOTIFIER_UNMAP; + 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? */ } diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 6b9ef7f6b2b..c2883a2f6c8 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -893,6 +893,11 @@ static int virtio_iommu_notify_flag_changed(IOMMUMemor= yRegion *iommu_mr, IOMMUNotifierFlag new, Error **errp) { + if (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP) { + error_setg(errp, "Virtio-iommu does not support dev-iotlb yet"); + return -EINVAL; + } + if (old =3D=3D IOMMU_NOTIFIER_NONE) { trace_virtio_iommu_notify_flag_add(iommu_mr->parent_obj.name); } else if (new =3D=3D IOMMU_NOTIFIER_NONE) { --=20 2.26.2