From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814816; cv=none; d=zohomail.com; s=zohoarc; b=dv90RotR7g+MVTIW1PSCs08KzQPA55od30fNC7FnfhY/7mTG58EZbSepP3xA2NDna0kztonyOUie+Hq+3JOjhi7yjA6221Dd8SlrnwoccmVLul8+lQc1n2yepSwTr6bNAOazFYIs0JR9RZjEqRh+0057ueuRIKnyFPobw256KuA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814816; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=BF3aZ5m8bDKQ/gf/crA7TPaevSIkblan+m/xnqK2smE=; b=TB3S11F8H5mDPxCrOFlY39YsCaHzMMammGwvx1PEZFj7pxyr6De1HswJj0mbvKD/968vlqpl9WtUt5lMyBSyIsVHyF23l9gHM9nZO6+ESGuw1jeWNypINpzVYYo2dc/8bFIJMU4DmvGCAEgA78sWPq/ubNgGNmkXXZN8S6FUGpw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 169381481620172.36383886797637; Mon, 4 Sep 2023 01:06:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4b8-0000uB-0b; Mon, 04 Sep 2023 04:06:16 -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 1qd4b5-0000sw-JK for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4b1-00066j-QH for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:11 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-634-1nJkZsFYNKaracu66GZSZQ-1; Mon, 04 Sep 2023 04:04:59 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5A46029AB3FD; Mon, 4 Sep 2023 08:04:59 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A81140ED412; Mon, 4 Sep 2023 08:04:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814766; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BF3aZ5m8bDKQ/gf/crA7TPaevSIkblan+m/xnqK2smE=; b=hWm1BHwPw2WIX3qZp3gB4jQeWmnhAQ4eOEzV2nwhOuPQvgS7ghBiSONgX9oFGVmBd32JcX Ev9nzIk6gVCLcJHWSACyLeeK7LWs905SWfHuVUMymZSFrrk1cVdWFxdM2jLOlmMjass+Pn 3DP4EmKhZfTFCxsf43wfhVo7kHCKDb4= X-MC-Unique: 1nJkZsFYNKaracu66GZSZQ-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 01/13] memory: Let ReservedRegion use Range Date: Mon, 4 Sep 2023 10:03:44 +0200 Message-ID: <20230904080451.424731-2-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.133.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814816567100001 Content-Type: text/plain; charset="utf-8" A reserved region is a range tagged with a type. Let's directly use the Range type in the prospect to reuse some of the library helpers shipped with the Range type. Signed-off-by: Eric Auger Reviewed-by: David Hildenbrand Reviewed-by: Peter Xu Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Yanghang Liu --- include/exec/memory.h | 4 ++-- hw/core/qdev-properties-system.c | 9 ++++++--- hw/virtio/virtio-iommu.c | 6 +++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index 68284428f8..184cb3a01b 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -24,6 +24,7 @@ #include "qemu/bswap.h" #include "qemu/queue.h" #include "qemu/int128.h" +#include "qemu/range.h" #include "qemu/notify.h" #include "qom/object.h" #include "qemu/rcu.h" @@ -79,8 +80,7 @@ extern unsigned int global_dirty_tracking; typedef struct MemoryRegionOps MemoryRegionOps; =20 struct ReservedRegion { - hwaddr low; - hwaddr high; + Range range; unsigned type; }; =20 diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-sys= tem.c index 6d5d43eda2..8d486eeefd 100644 --- a/hw/core/qdev-properties-system.c +++ b/hw/core/qdev-properties-system.c @@ -699,7 +699,7 @@ static void get_reserved_region(Object *obj, Visitor *v= , const char *name, int rc; =20 rc =3D snprintf(buffer, sizeof(buffer), "0x%"PRIx64":0x%"PRIx64":%u", - rr->low, rr->high, rr->type); + range_lob(&rr->range), range_upb(&rr->range), rr->type); assert(rc < sizeof(buffer)); =20 visit_type_str(v, name, &p, errp); @@ -711,6 +711,7 @@ static void set_reserved_region(Object *obj, Visitor *v= , const char *name, Property *prop =3D opaque; ReservedRegion *rr =3D object_field_prop_ptr(obj, prop); const char *endptr; + uint64_t lob, upb; char *str; int ret; =20 @@ -718,7 +719,7 @@ static void set_reserved_region(Object *obj, Visitor *v= , const char *name, return; } =20 - ret =3D qemu_strtou64(str, &endptr, 16, &rr->low); + ret =3D qemu_strtou64(str, &endptr, 16, &lob); if (ret) { error_setg(errp, "start address of '%s'" " must be a hexadecimal integer", name); @@ -728,7 +729,7 @@ static void set_reserved_region(Object *obj, Visitor *v= , const char *name, goto separator_error; } =20 - ret =3D qemu_strtou64(endptr + 1, &endptr, 16, &rr->high); + ret =3D qemu_strtou64(endptr + 1, &endptr, 16, &upb); if (ret) { error_setg(errp, "end address of '%s'" " must be a hexadecimal integer", name); @@ -738,6 +739,8 @@ static void set_reserved_region(Object *obj, Visitor *v= , const char *name, goto separator_error; } =20 + range_set_bounds(&rr->range, lob, upb); + ret =3D qemu_strtoui(endptr + 1, &endptr, 10, &rr->type); if (ret) { error_setg(errp, "type of '%s'" diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index be51635895..e5e46e1b55 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -645,8 +645,8 @@ static ssize_t virtio_iommu_fill_resv_mem_prop(VirtIOIO= MMU *s, uint32_t ep, prop.head.type =3D cpu_to_le16(VIRTIO_IOMMU_PROBE_T_RESV_MEM); prop.head.length =3D cpu_to_le16(length); prop.subtype =3D subtype; - prop.start =3D cpu_to_le64(s->reserved_regions[i].low); - prop.end =3D cpu_to_le64(s->reserved_regions[i].high); + prop.start =3D cpu_to_le64(range_lob(&s->reserved_regions[i].range= )); + prop.end =3D cpu_to_le64(range_upb(&s->reserved_regions[i].range)); =20 memcpy(buf, &prop, size); =20 @@ -897,7 +897,7 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemory= Region *mr, hwaddr addr, for (i =3D 0; i < s->nb_reserved_regions; i++) { ReservedRegion *reg =3D &s->reserved_regions[i]; =20 - if (addr >=3D reg->low && addr <=3D reg->high) { + if (range_contains(®->range, addr)) { switch (reg->type) { case VIRTIO_IOMMU_RESV_MEM_T_MSI: entry.perm =3D flag; --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814865; cv=none; d=zohomail.com; s=zohoarc; b=muacekEWbmzaXPpr1bejFfRu+wE/x1XksXiXeGJPmde6eMFNGENvpnk3Ru9gMAAzg4Eln1YSZunbiP+5WBGO8kCEJNMlU3mzBaT5oqyuDIOLblykEfBBoB6o5RNMpY/YJFh3XiMRHIGXkAJh/EGn1wScOws4hH1tPDmjfimIO/Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814865; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0fClSLFNTnj1PBBeNErEhvIOOlv5XDQmnbvHzzG3f0E=; b=Ww60s58sExJpTpZIKXljk147SH+ys8m5xRLHZZEPlcaqLeqRcEn7Meyax6aQYJbnQFunwMxklIJgWX5DJ2COOjGm5O9qd3seAyYB2XLe3bM1pvny4Q/xL5xv0BQSpJcMpMjOZKJI88moZM8Tle60SUjsAXzfDlJ+aVy2F75BT0k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814865263534.2043155803176; Mon, 4 Sep 2023 01:07:45 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4bE-0000xq-Al; Mon, 04 Sep 2023 04:06:20 -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 1qd4bC-0000vT-AU for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4b7-0006AB-Fi for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:16 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-295-CBDaXKdjPeGayDcBfJDI3A-1; Mon, 04 Sep 2023 04:05:02 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 01F0D8015AA; Mon, 4 Sep 2023 08:05:02 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1592400D277; Mon, 4 Sep 2023 08:04:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814772; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0fClSLFNTnj1PBBeNErEhvIOOlv5XDQmnbvHzzG3f0E=; b=UQgBpjXPFRF9zAIbIbINQ7HN5CLSYRCXLGvXGV62iw6M0jeUwiYe7sS9/GVpqJH0C6W8Xq zD8QPGeQYnXzZ0BeZUP6RlH41uXEgwDnMRXpR2wn/YWnJnvIDAFcvO+wRzQkbYyBNY8zf1 vbYl+VumzmjpAulGv5fbIu5OH3pTaXE= X-MC-Unique: CBDaXKdjPeGayDcBfJDI3A-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 02/13] memory: Introduce memory_region_iommu_set_iova_ranges Date: Mon, 4 Sep 2023 10:03:45 +0200 Message-ID: <20230904080451.424731-3-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.129.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814866553100003 Content-Type: text/plain; charset="utf-8" This helper will allow to convey information about valid IOVA ranges to virtual IOMMUS. Signed-off-by: Eric Auger Reviewed-by: David Hildenbrand Reviewed-by: Peter Xu Tested-by: Yanghang Liu --- include/exec/memory.h | 26 ++++++++++++++++++++++++++ softmmu/memory.c | 15 +++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/include/exec/memory.h b/include/exec/memory.h index 184cb3a01b..f6fb99dd3f 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -519,6 +519,27 @@ struct IOMMUMemoryRegionClass { int (*iommu_set_page_size_mask)(IOMMUMemoryRegion *iommu, uint64_t page_size_mask, Error **errp); + /** + * @iommu_set_iova_ranges: + * + * Propagate information about the usable IOVA ranges for a given IOMMU + * memory region. Used for example to propagate host physical device + * reserved memory region constraints to the virtual IOMMU. + * + * Optional method: if this method is not provided, then the default I= OVA + * aperture is used. + * + * @nr_ranges: number of IOVA ranges + * + * @iova_ranges: an array of @nr_ranges usable IOVA ranges + * + * Returns 0 on success, or a negative error. In case of failure, the = error + * object must be created. + */ + int (*iommu_set_iova_ranges)(IOMMUMemoryRegion *iommu, + uint32_t nr_ranges, + struct Range *iova_ranges, + Error **errp); }; =20 typedef struct RamDiscardListener RamDiscardListener; @@ -1845,6 +1866,11 @@ int memory_region_iommu_set_page_size_mask(IOMMUMemo= ryRegion *iommu_mr, uint64_t page_size_mask, Error **errp); =20 +int memory_region_iommu_set_iova_ranges(IOMMUMemoryRegion *iommu, + uint32_t nr_ranges, + struct Range *iova_ranges, + Error **errp); + /** * memory_region_name: get a memory region's name * diff --git a/softmmu/memory.c b/softmmu/memory.c index 7d9494ce70..07499457aa 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -1905,6 +1905,21 @@ int memory_region_iommu_set_page_size_mask(IOMMUMemo= ryRegion *iommu_mr, return ret; } =20 +int memory_region_iommu_set_iova_ranges(IOMMUMemoryRegion *iommu_mr, + uint32_t nr_ranges, + struct Range *iova_ranges, + Error **errp) +{ + IOMMUMemoryRegionClass *imrc =3D IOMMU_MEMORY_REGION_GET_CLASS(iommu_m= r); + int ret =3D 0; + + if (imrc->iommu_set_iova_ranges) { + ret =3D imrc->iommu_set_iova_ranges(iommu_mr, nr_ranges, + iova_ranges, errp); + } + return ret; +} + int memory_region_register_iommu_notifier(MemoryRegion *mr, IOMMUNotifier *n, Error **errp) { --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814823; cv=none; d=zohomail.com; s=zohoarc; b=VWa7ZmlAkgCw5DPhhRefynUINkcV8gdqr557v5NhhL2Jq+PucN4uocJ89ZUiIuXt13Z0LS0YwF6IiOEMWmLIZgGU5x6gu9QrX4T3xVP+EK7AGVbYgldBwVVjsP0hN37jZTK0m8g+UQOxx4MAE0K6NPkcUgyELimXjYZqmyt6Qj8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814823; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=deUtT+oz+1XIYevIMg+Drga7A61AlvVs6/MjQyLPDyM=; b=G2Fbfi+618dVMScAZu/6NF8pjk2iQqUxQmj3us/h1DKQqW+8IAqPd9mCdMTriHBoSXA/gVIdm/vwJ6obDsDUiNYblgReyETfhI0+7MnfwL6AhQPU3qkNnC4fUQ4yS2jPTzqOa3cuOMwcZ5gjyaF/5Ly36TjM6fZxkYPx+gQQ2J4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814822999412.64173928190905; Mon, 4 Sep 2023 01:07:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4bV-00013Y-SM; Mon, 04 Sep 2023 04:06:37 -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 1qd4bR-00011S-Ri for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bD-0006El-3w for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:21 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-655-0hZ-OyfcMQCV7dXXmd4C1A-1; Mon, 04 Sep 2023 04:05:05 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CDE8B8008A4; Mon, 4 Sep 2023 08:05:04 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4826840C1258; Mon, 4 Sep 2023 08:05:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814776; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=deUtT+oz+1XIYevIMg+Drga7A61AlvVs6/MjQyLPDyM=; b=Q6JPubzNh+cliw7S8BA2lPPFgACaO1pcAtxMXLHPozpNlTt7vFCtOhgCf1UBtF4Aq1ZhzA vfCNZaY2wRTYVjDfmpOeqEnUE8ZeNzyOQtUEvN8zDuTiA9FPvX28c3CA1W0ObBMfFVRd3E i2R8gNP6wCLLN0JamqW4HrpSbHvJUX8= X-MC-Unique: 0hZ-OyfcMQCV7dXXmd4C1A-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 03/13] vfio: Collect container iova range info Date: Mon, 4 Sep 2023 10:03:46 +0200 Message-ID: <20230904080451.424731-4-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.129.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814824117100003 Content-Type: text/plain; charset="utf-8" Collect iova range information if VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE capability is supported. This allows to propagate the information though the IOMMU MR set_iova_ranges() callback so that virtual IOMMUs get aware of those aperture constraints. Signed-off-by: Eric Auger Tested-by: Yanghang Liu --- include/hw/vfio/vfio-common.h | 2 ++ hw/vfio/common.c | 45 +++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index da43d27352..74b9b27270 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -98,6 +98,8 @@ typedef struct VFIOContainer { QLIST_HEAD(, VFIOGroup) group_list; QLIST_HEAD(, VFIORamDiscardListener) vrdl_list; QLIST_ENTRY(VFIOContainer) next; + unsigned nr_iovas; + struct vfio_iova_range *iova_ranges; } VFIOContainer; =20 typedef struct VFIOGuestIOMMU { diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 9aac21abb7..26da38de05 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1157,6 +1157,14 @@ static void vfio_listener_region_add(MemoryListener = *listener, goto fail; } =20 + ret =3D memory_region_iommu_set_iova_ranges(giommu->iommu_mr, + container->nr_iovas, (struct Range *)container->iova_range= s, + &err); + if (ret) { + g_free(giommu); + goto fail; + } + ret =3D memory_region_register_iommu_notifier(section->mr, &giommu= ->n, &err); if (ret) { @@ -1981,6 +1989,29 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1= _info *info, return true; } =20 +static void vfio_get_info_iova_range(struct vfio_iommu_type1_info *info, + unsigned int *nr_iovas, + struct vfio_iova_range **iova_ranges) +{ + struct vfio_info_cap_header *hdr; + struct vfio_iommu_type1_info_cap_iova_range *cap; + + hdr =3D vfio_get_iommu_type1_info_cap(info, + VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RAN= GE); + if (hdr =3D=3D NULL) { + return; + } + + cap =3D (void *)hdr; + *nr_iovas =3D cap->nr_iovas; + + if (*nr_iovas =3D=3D 0) { + return; + } + *iova_ranges =3D g_memdup2(cap->iova_ranges, + *nr_iovas * sizeof(struct vfio_iova_range)); +} + static int vfio_setup_region_sparse_mmaps(VFIORegion *region, struct vfio_region_info *info) { @@ -2433,6 +2464,12 @@ static void vfio_get_iommu_info_migration(VFIOContai= ner *container, } } =20 +static void vfio_free_container(VFIOContainer *container) +{ + g_free(container->iova_ranges); + g_free(container); +} + static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, Error **errp) { @@ -2550,6 +2587,10 @@ static int vfio_connect_container(VFIOGroup *group, = AddressSpace *as, if (!vfio_get_info_dma_avail(info, &container->dma_max_mappings)) { container->dma_max_mappings =3D 65535; } + + vfio_get_info_iova_range(info, &container->nr_iovas, + &container->iova_ranges); + vfio_get_iommu_info_migration(container, info); g_free(info); =20 @@ -2663,7 +2704,7 @@ enable_discards_exit: vfio_ram_block_discard_disable(container, false); =20 free_container_exit: - g_free(container); + vfio_free_container(container); =20 close_fd_exit: close(fd); @@ -2717,7 +2758,7 @@ static void vfio_disconnect_container(VFIOGroup *grou= p) =20 trace_vfio_disconnect_container(container->fd); close(container->fd); - g_free(container); + vfio_free_container(container); =20 vfio_put_address_space(space); } --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814920776676.7711949108574; Mon, 4 Sep 2023 01:08:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4bV-000134-Fr; Mon, 04 Sep 2023 04:06:37 -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 1qd4bC-0000vm-LN for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bA-0006DV-1y for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:18 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-615-nRy1yXKhNM2ko5Cu-8N5LQ-1; Mon, 04 Sep 2023 04:05:08 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F34BE3C19365; Mon, 4 Sep 2023 08:05:07 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3598340ED412; Mon, 4 Sep 2023 08:05:05 +0000 (UTC) X-MC-Unique: nRy1yXKhNM2ko5Cu-8N5LQ-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 04/13] virtio-iommu: Rename reserved_regions into prop_resv_regions Date: Mon, 4 Sep 2023 10:03:47 +0200 Message-ID: <20230904080451.424731-5-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.133.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.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, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1693814922731100003 Content-Type: text/plain; charset="utf-8" Rename VirtIOIOMMU (nb_)reserved_regions fields with the "prop_" prefix to highlight those fields are set through a property, at machine level. They are IOMMU wide. A subsequent patch will introduce per IOMMUDevice reserved regions that will include both those IOMMU wide property reserved regions plus, sometimes, host reserved regions, if the device is backed by a host device protected by a physical IOMMU. Also change nb_ prefix by nr_. Signed-off-by: Eric Auger Tested-by: Yanghang Liu --- include/hw/virtio/virtio-iommu.h | 4 ++-- hw/virtio/virtio-iommu-pci.c | 8 ++++---- hw/virtio/virtio-iommu.c | 15 ++++++++------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-io= mmu.h index a93fc5383e..eea4564782 100644 --- a/include/hw/virtio/virtio-iommu.h +++ b/include/hw/virtio/virtio-iommu.h @@ -55,8 +55,8 @@ struct VirtIOIOMMU { GHashTable *as_by_busptr; IOMMUPciBus *iommu_pcibus_by_bus_num[PCI_BUS_MAX]; PCIBus *primary_bus; - ReservedRegion *reserved_regions; - uint32_t nb_reserved_regions; + ReservedRegion *prop_resv_regions; + uint32_t nr_prop_resv_regions; GTree *domains; QemuRecMutex mutex; GTree *endpoints; diff --git a/hw/virtio/virtio-iommu-pci.c b/hw/virtio/virtio-iommu-pci.c index 7ef2f9dcdb..9459fbf6ed 100644 --- a/hw/virtio/virtio-iommu-pci.c +++ b/hw/virtio/virtio-iommu-pci.c @@ -37,7 +37,7 @@ struct VirtIOIOMMUPCI { static Property virtio_iommu_pci_properties[] =3D { DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), DEFINE_PROP_ARRAY("reserved-regions", VirtIOIOMMUPCI, - vdev.nb_reserved_regions, vdev.reserved_regions, + vdev.nr_prop_resv_regions, vdev.prop_resv_regions, qdev_prop_reserved_region, ReservedRegion), DEFINE_PROP_END_OF_LIST(), }; @@ -54,9 +54,9 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci= _dev, Error **errp) "for the virtio-iommu-pci device"); return; } - for (int i =3D 0; i < s->nb_reserved_regions; i++) { - if (s->reserved_regions[i].type !=3D VIRTIO_IOMMU_RESV_MEM_T_RESER= VED && - s->reserved_regions[i].type !=3D VIRTIO_IOMMU_RESV_MEM_T_MSI) { + for (int i =3D 0; i < s->nr_prop_resv_regions; i++) { + if (s->prop_resv_regions[i].type !=3D VIRTIO_IOMMU_RESV_MEM_T_RESE= RVED && + s->prop_resv_regions[i].type !=3D VIRTIO_IOMMU_RESV_MEM_T_MSI)= { error_setg(errp, "reserved region %d has an invalid type", i); error_append_hint(errp, "Valid values are 0 and 1\n"); return; diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index e5e46e1b55..979cdb5648 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -631,22 +631,23 @@ static ssize_t virtio_iommu_fill_resv_mem_prop(VirtIO= IOMMU *s, uint32_t ep, size_t size =3D sizeof(prop), length =3D size - sizeof(prop.head), tot= al; int i; =20 - total =3D size * s->nb_reserved_regions; + total =3D size * s->nr_prop_resv_regions; =20 if (total > free) { return -ENOSPC; } =20 - for (i =3D 0; i < s->nb_reserved_regions; i++) { - unsigned subtype =3D s->reserved_regions[i].type; + for (i =3D 0; i < s->nr_prop_resv_regions; i++) { + unsigned subtype =3D s->prop_resv_regions[i].type; + Range *range =3D &s->prop_resv_regions[i].range; =20 assert(subtype =3D=3D VIRTIO_IOMMU_RESV_MEM_T_RESERVED || subtype =3D=3D VIRTIO_IOMMU_RESV_MEM_T_MSI); prop.head.type =3D cpu_to_le16(VIRTIO_IOMMU_PROBE_T_RESV_MEM); prop.head.length =3D cpu_to_le16(length); prop.subtype =3D subtype; - prop.start =3D cpu_to_le64(range_lob(&s->reserved_regions[i].range= )); - prop.end =3D cpu_to_le64(range_upb(&s->reserved_regions[i].range)); + prop.start =3D cpu_to_le64(range_lob(range)); + prop.end =3D cpu_to_le64(range_upb(range)); =20 memcpy(buf, &prop, size); =20 @@ -894,8 +895,8 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemory= Region *mr, hwaddr addr, goto unlock; } =20 - for (i =3D 0; i < s->nb_reserved_regions; i++) { - ReservedRegion *reg =3D &s->reserved_regions[i]; + for (i =3D 0; i < s->nr_prop_resv_regions; i++) { + ReservedRegion *reg =3D &s->prop_resv_regions[i]; =20 if (range_contains(®->range, addr)) { switch (reg->type) { --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814980; cv=none; d=zohomail.com; s=zohoarc; b=g6MKo0/Lpt2EfSLqxaiMXjXovaTcqkOJ8JM957D5ZnExcdzO73qpEB6RkAda7JdGMZLGaCPlAbs2d0Ikq+XC2BTyl4rpazaLGDVjQl4g55kBN+yKKOhkSFRMkpMCA8crJQzLRdLumT2/z36CaTZ00OnNOOAlK3cUcr3xBM7E5vo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814980; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=IAZop55Ny6Gxc5AQo+TBRznDjzisZRxOxhW8y3kjP5o=; b=cZSJo0RJT3yZXlR8IOArhwVS7oHlsbE4wC13CBaWWkf+ioKgWPC7CaiuBYwwzo2emVKxyVI/lbOpdQblguerAEVQUPdzKc+0DV2w+0MizC5fOyTdzkUDB4A3B2A93VJqh0qWsVYEjU/fZlztoAG9LT+08oyiaSbmREY0PAgY49s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814980879991.7235825772461; Mon, 4 Sep 2023 01:09:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4bc-00017y-G7; Mon, 04 Sep 2023 04:06:44 -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 1qd4bR-00011Q-PJ for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bD-0006HN-Vq for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:22 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-696-s3V8MV8DNhqsH4pJDynJrA-1; Mon, 04 Sep 2023 04:05:11 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 99107382C967; Mon, 4 Sep 2023 08:05:10 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 47B0C40D2962; Mon, 4 Sep 2023 08:05:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814778; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IAZop55Ny6Gxc5AQo+TBRznDjzisZRxOxhW8y3kjP5o=; b=FH1iXBTbDzp746Mur2ieJBce3mKMgApmM7SVH4XSdjTiYqwSFclrp3e2bk0jFAGoHqGsMj a3ShWHEC4/L59q35hZk5eKT5hlzOC7hbqqN6AUub2G4167sLzdoLQGN1AASUeBWbHMXLi0 UtZuV9v2hs9f84lqoInRxPi/ep10Rfs= X-MC-Unique: s3V8MV8DNhqsH4pJDynJrA-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 05/13] virtio-iommu: Introduce per IOMMUDevice reserved regions Date: Mon, 4 Sep 2023 10:03:48 +0200 Message-ID: <20230904080451.424731-6-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.133.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814981696100001 Content-Type: text/plain; charset="utf-8" For the time being the per device reserved regions are just a duplicate of IOMMU wide reserved regions. Subsequent patches will combine those with host reserved regions, if any. Signed-off-by: Eric Auger Tested-by: Yanghang Liu --- include/hw/virtio/virtio-iommu.h | 1 + hw/virtio/virtio-iommu.c | 42 ++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-io= mmu.h index eea4564782..70b8ace34d 100644 --- a/include/hw/virtio/virtio-iommu.h +++ b/include/hw/virtio/virtio-iommu.h @@ -39,6 +39,7 @@ typedef struct IOMMUDevice { AddressSpace as; MemoryRegion root; /* The root container of the device */ MemoryRegion bypass_mr; /* The alias of shared memory MR */ + GList *resv_regions; } IOMMUDevice; =20 typedef struct IOMMUPciBus { diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 979cdb5648..ea359b586a 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -624,22 +624,48 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s, return ret; } =20 +static int consolidate_resv_regions(IOMMUDevice *sdev) +{ + VirtIOIOMMU *s =3D sdev->viommu; + int i; + + for (i =3D 0; i < s->nr_prop_resv_regions; i++) { + ReservedRegion *reg =3D g_new0(ReservedRegion, 1); + + *reg =3D s->prop_resv_regions[i]; + sdev->resv_regions =3D g_list_append(sdev->resv_regions, reg); + } + return 0; +} + static ssize_t virtio_iommu_fill_resv_mem_prop(VirtIOIOMMU *s, uint32_t ep, uint8_t *buf, size_t free) { struct virtio_iommu_probe_resv_mem prop =3D {}; size_t size =3D sizeof(prop), length =3D size - sizeof(prop.head), tot= al; - int i; + IOMMUDevice *sdev; + GList *l; + int ret; =20 - total =3D size * s->nr_prop_resv_regions; + sdev =3D container_of(virtio_iommu_mr(s, ep), IOMMUDevice, iommu_mr); + if (!sdev) { + return -EINVAL; + } =20 + ret =3D consolidate_resv_regions(sdev); + if (ret) { + return ret; + } + + total =3D size * g_list_length(sdev->resv_regions); if (total > free) { return -ENOSPC; } =20 - for (i =3D 0; i < s->nr_prop_resv_regions; i++) { - unsigned subtype =3D s->prop_resv_regions[i].type; - Range *range =3D &s->prop_resv_regions[i].range; + for (l =3D sdev->resv_regions; l; l =3D l->next) { + ReservedRegion *reg =3D l->data; + unsigned subtype =3D reg->type; + Range *range =3D ®->range; =20 assert(subtype =3D=3D VIRTIO_IOMMU_RESV_MEM_T_RESERVED || subtype =3D=3D VIRTIO_IOMMU_RESV_MEM_T_MSI); @@ -857,7 +883,7 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemory= Region *mr, hwaddr addr, bool bypass_allowed; int granule; bool found; - int i; + GList *l; =20 interval.low =3D addr; interval.high =3D addr + 1; @@ -895,8 +921,8 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemory= Region *mr, hwaddr addr, goto unlock; } =20 - for (i =3D 0; i < s->nr_prop_resv_regions; i++) { - ReservedRegion *reg =3D &s->prop_resv_regions[i]; + for (l =3D sdev->resv_regions; l; l =3D l->next) { + ReservedRegion *reg =3D l->data; =20 if (range_contains(®->range, addr)) { switch (reg->type) { --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814910; cv=none; d=zohomail.com; s=zohoarc; b=HAg0al9tHxt3CTBfwXYwviX9W7RXl2O+HXFktW6GPAya/o3kXY4o+vEUxWUn7OvTVhJZZlQR8GCzNSsUWBgQ0n4/3xQTLFR20MZgWskS6JK3oU6xtmYpEHOjRmy3CPDGLuR54nr3j4CvPfelNidF4XHGcjhNjRTZbPdyKyPy1fQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814910; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Um6REkjCwNMOmEMDVW18psduw0V2ZAPmFC4IdpStheA=; b=WwjQVmKeAD82/EhYb5MFI5uhXmwAyQDiHeG5R7VHW/7AEulyd28hltwPCTurWRCHgGdnjnFx2ezaeqjBp9nAVBYNkaJs96OieTpjDQle/LTP95FgnnPzxRBtV0N+dYTMgpb/dq0g+7gjwZjqi/R1jn5GUgy8CN7b7llcSonqGg8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814910522599.9178817268729; Mon, 4 Sep 2023 01:08:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4bc-00018S-Na; Mon, 04 Sep 2023 04:06:44 -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 1qd4bV-00013S-La for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:37 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bR-0006Js-Bt for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:37 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-146-RgijNkkRMfySp58HyT5o3Q-1; Mon, 04 Sep 2023 04:05:15 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EA5291E441D4; Mon, 4 Sep 2023 08:05:14 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id DEF1940ED412; Mon, 4 Sep 2023 08:05:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814781; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Um6REkjCwNMOmEMDVW18psduw0V2ZAPmFC4IdpStheA=; b=Iox/4fZAPpBsJLMT2tvO4yrbqqDJXGY1io0sUDSUEP/4Xn0qdN+faN4idcKNml9DWW1Bzc zKcHW66f7uE7M56dh87Jkvc18RW7IASB7GpZx1MaVaYLuhOj72xswcIBAjegoJHpBYs1E5 BBeaaaLHjommgPV3bIDLaHtYJgKVsPo= X-MC-Unique: RgijNkkRMfySp58HyT5o3Q-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 06/13] range: Introduce range_inverse_array() Date: Mon, 4 Sep 2023 10:03:49 +0200 Message-ID: <20230904080451.424731-7-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.133.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814912434100005 Content-Type: text/plain; charset="utf-8" This helper reverses an array of regions, turning original regions into holes and original holes into actual regions, covering the whole UINT64_MAX span. Signed-off-by: Eric Auger Tested-by: Yanghang Liu --- include/qemu/range.h | 3 +++ util/range.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/qemu/range.h b/include/qemu/range.h index 7e2b1cc447..fc1d3dabe6 100644 --- a/include/qemu/range.h +++ b/include/qemu/range.h @@ -219,4 +219,7 @@ static inline int ranges_overlap(uint64_t first1, uint6= 4_t len1, =20 GList *range_list_insert(GList *list, Range *data); =20 +void range_inverse_array(uint32_t nr_ranges, Range *ranges, + uint32_t *nr_inv_ranges, Range **inv_ranges); + #endif diff --git a/util/range.c b/util/range.c index 098d9d2dc0..11c4ff0b78 100644 --- a/util/range.c +++ b/util/range.c @@ -70,3 +70,38 @@ GList *range_list_insert(GList *list, Range *data) =20 return list; } + +/* + * Inverse an array of sorted ranges over the UINT64_MAX span, ie. + * original ranges becomes holes in the newly allocated inv_ranges + */ +void range_inverse_array(uint32_t nr_ranges, Range *ranges, + uint32_t *nr_inv_ranges, Range **inv_ranges) +{ + Range *resv; + int i =3D 0, j =3D 0; + + resv =3D g_malloc0_n(nr_ranges + 1, sizeof(Range)); + + /* first range lob is greater than 0, insert a first range */ + if (range_lob(&ranges[0]) > 0) { + range_set_bounds(&resv[i++], 0, + range_lob(&ranges[0]) - 1); + } + + /* insert a range inbetween each original range */ + for (; j < nr_ranges - 1; j++) { + if (range_compare(&ranges[j], &ranges[j + 1])) { + range_set_bounds(&resv[i++], range_upb(&ranges[j]) + 1, + range_lob(&ranges[j + 1]) - 1); + } + } + /* last range upb is less than UINT64_MAX, insert a last range */ + if (range_upb(&ranges[nr_ranges - 1]) < UINT64_MAX) { + range_set_bounds(&resv[i++], + range_upb(&ranges[nr_ranges - 1]) + 1, UINT64_MA= X); + } + *nr_inv_ranges =3D i; + resv =3D g_realloc(resv, i * sizeof(Range)); + *inv_ranges =3D resv; +} --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814993; cv=none; d=zohomail.com; s=zohoarc; b=BhBA9LS8IlxGvLWr8dc3/qd3ppqFKGHRmJL/KZ8yiXdbSK8uDYwfULuSGlE7nOgKvvAimw4eutfvTSPfOwWsCsqJpCtUwq/MyNNTfjYhC/2Y7IK7oVixMbPjj7taNhl2G6n6Xi3QgeKy5kQhYFbt9+cmDHH+zBBOJDCmsEt5lj4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814993; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mqcE63CfOAlQUpwWTYprlvV2vd+VNunVacYwB1rAneQ=; b=J38IPyOo2ORtgdI1/anUA1j5IEhhSwRE8r36T5VZnreSP2xiuR3Oxx0Tx9Ov7BxCSFiWtn+15zxedvs4HOVfyuR//PmH4Eed+CCzZQMTQL7/SlfqGcmOS2ff3bRNo3fb0XhLjlND9bvScAd0T8Cm22ufwkBWrMJRSndIFQqmQ1Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814993670101.83663688787135; Mon, 4 Sep 2023 01:09:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4bd-0001AS-Fz; Mon, 04 Sep 2023 04:06:45 -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 1qd4bc-000185-EK for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:44 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bR-0006MA-BZ for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:44 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-650-C9ssjOpUM-OtlYBHnwSxMw-1; Mon, 04 Sep 2023 04:05:18 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D1D62382C969; Mon, 4 Sep 2023 08:05:17 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3CFDB400D277; Mon, 4 Sep 2023 08:05:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814787; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mqcE63CfOAlQUpwWTYprlvV2vd+VNunVacYwB1rAneQ=; b=Z5LpflDA7fWGhd25elLsdyNh1QeLSg1uw4bTscRpwpK7WbkMmq057f7njYR5eh60ujFpNH BvIg1/Vb+rYb8ZiITcFTcQvT2kd0RhcWTrUo9zdoRxl1Cdcyu9toF5tsmLuhGXfjT7zx05 Qwsdv/E7FPD36eTnvXY47DeF5mQo2Lo= X-MC-Unique: C9ssjOpUM-OtlYBHnwSxMw-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 07/13] virtio-iommu: Implement set_iova_ranges() callback Date: Mon, 4 Sep 2023 10:03:50 +0200 Message-ID: <20230904080451.424731-8-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.133.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814994649100002 Content-Type: text/plain; charset="utf-8" The implementation populates the array of per IOMMUDevice host reserved regions. Signed-off-by: Eric Auger Tested-by: Yanghang Liu --- include/hw/virtio/virtio-iommu.h | 2 ++ hw/virtio/virtio-iommu.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-io= mmu.h index 70b8ace34d..31b69c8261 100644 --- a/include/hw/virtio/virtio-iommu.h +++ b/include/hw/virtio/virtio-iommu.h @@ -40,6 +40,8 @@ typedef struct IOMMUDevice { MemoryRegion root; /* The root container of the device */ MemoryRegion bypass_mr; /* The alias of shared memory MR */ GList *resv_regions; + Range *host_resv_regions; + uint32_t nr_host_resv_regions; } IOMMUDevice; =20 typedef struct IOMMUPciBus { diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index ea359b586a..02f1a59d57 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -20,6 +20,7 @@ #include "qemu/osdep.h" #include "qemu/log.h" #include "qemu/iov.h" +#include "qemu/range.h" #include "exec/target_page.h" #include "hw/qdev-properties.h" #include "hw/virtio/virtio.h" @@ -1158,6 +1159,21 @@ static int virtio_iommu_set_page_size_mask(IOMMUMemo= ryRegion *mr, return 0; } =20 +static int virtio_iommu_set_iova_ranges(IOMMUMemoryRegion *mr, + uint32_t nr_ranges, + struct Range *iova_ranges, + Error **errp) +{ + IOMMUDevice *sdev =3D container_of(mr, IOMMUDevice, iommu_mr); + assert(nr_ranges); + + + range_inverse_array(nr_ranges, iova_ranges, + &sdev->nr_host_resv_regions, &sdev->host_resv_regi= ons); + + return 0; +} + static void virtio_iommu_system_reset(void *opaque) { VirtIOIOMMU *s =3D opaque; @@ -1453,6 +1469,7 @@ static void virtio_iommu_memory_region_class_init(Obj= ectClass *klass, imrc->replay =3D virtio_iommu_replay; imrc->notify_flag_changed =3D virtio_iommu_notify_flag_changed; imrc->iommu_set_page_size_mask =3D virtio_iommu_set_page_size_mask; + imrc->iommu_set_iova_ranges =3D virtio_iommu_set_iova_ranges; } =20 static const TypeInfo virtio_iommu_info =3D { --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814993; cv=none; d=zohomail.com; s=zohoarc; b=B3gZt834SfLm2lwWfOzgHuoXkpRCaMe0a/TOEcbpT1N+MiMYT5Nwhc399hSWwlYRzOJChnKS1uSe4WB6iUhiJbIFFPztFz0+7mGr8NmUZR0OPbtKranBEmkxjXR0wRYUAK4/x//HxSkWeck45l8DM1tQhyz2efYLF6phBSN7pJ0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814993; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=k2oowtsVMHgV/gX0BOO0dY/1y0XnnvK0ZYYn6+GB3/M=; b=mTWO+5UCL2iW+JAm/C/Q6Fif3dXVde5bhblzJlULnvu6xTFMSNtqhowGRJAHzg4qLDBf7b81sQAsviPOAt/xRVNgXpZFK5eljg+L4pLhfnPsPJPU3AD/2r9WRgoYTiLvcPiM/fpZObbs+UnesmSJM8dwIAAIJKhkfV41wAp+vBA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814993665679.4059037710541; Mon, 4 Sep 2023 01:09:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4c0-0002Ap-ID; Mon, 04 Sep 2023 04:07:08 -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 1qd4by-00021x-5m for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:07:06 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bu-0006XH-He for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:07:05 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-353-_0rgm7GvMgSy4SMH_2zrvQ-1; Mon, 04 Sep 2023 04:05:21 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3F36A8EE7C5; Mon, 4 Sep 2023 08:05:21 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 28AD440C6CCC; Mon, 4 Sep 2023 08:05:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814821; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k2oowtsVMHgV/gX0BOO0dY/1y0XnnvK0ZYYn6+GB3/M=; b=G3ci9UHYBHdphjoI5VilKRQIIRNDsR/vaM5w+80TwiVLbK0RxPIYoRf37Pgxnj1pXn++0y 1ajtu76SmOUPJj5aP5uXodTINGeWQdSMqH9roZwD6y7hD2S3z7YYvzRH1QMC5ser/VkEBT RbdEJAh3eR4ygNvkDB/z+ky36J9kYyk= X-MC-Unique: _0rgm7GvMgSy4SMH_2zrvQ-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 08/13] range: Make range_compare() public Date: Mon, 4 Sep 2023 10:03:51 +0200 Message-ID: <20230904080451.424731-9-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.129.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814994635100001 Content-Type: text/plain; charset="utf-8" Let's expose range_compare() in the header so that it can be reused outside of util/range.c Signed-off-by: Eric Auger Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Yanghang Liu --- include/qemu/range.h | 6 ++++++ util/range.c | 6 +----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/qemu/range.h b/include/qemu/range.h index fc1d3dabe6..52641dfb79 100644 --- a/include/qemu/range.h +++ b/include/qemu/range.h @@ -217,6 +217,12 @@ static inline int ranges_overlap(uint64_t first1, uint= 64_t len1, return !(last2 < first1 || last1 < first2); } =20 +/* + * Return -1 if @a < @b, 1 @a > @b, and 0 if they touch or overlap. + * Both @a and @b must not be empty. + */ +int range_compare(Range *a, Range *b); + GList *range_list_insert(GList *list, Range *data); =20 void range_inverse_array(uint32_t nr_ranges, Range *ranges, diff --git a/util/range.c b/util/range.c index 11c4ff0b78..1fc399e88f 100644 --- a/util/range.c +++ b/util/range.c @@ -20,11 +20,7 @@ #include "qemu/osdep.h" #include "qemu/range.h" =20 -/* - * Return -1 if @a < @b, 1 @a > @b, and 0 if they touch or overlap. - * Both @a and @b must not be empty. - */ -static inline int range_compare(Range *a, Range *b) +int range_compare(Range *a, Range *b) { assert(!range_is_empty(a) && !range_is_empty(b)); =20 --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814860; cv=none; d=zohomail.com; s=zohoarc; b=M8mG13hWt1srhvo1nfvsMD+lQu0Jy0i0OfRe/bgAOM4dbwWCQLlyhzUnhBfitLz0rElj243N7bPkC68kJhd0q9ZCQiyqo7z0nrYDKcvNaD3A/rSIEfUuF+rRaurnPJ2mOO8tDVTwQpXz5heqvCignoGyTLltk4itM0a1wdZPW9s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814860; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Rxn5EDYXEwVoQRGNPIOql5tx6O0sQvV/pW4KSk35sJQ=; b=LonGAfc19hGA3O500kEp7SeLyYKtWUVxmLZ+NxHEd6givO3fCoxnl8X3HIf7rabQox4FuAE5k4lTMo3FMlg8WhrXaZeNKM53hsTkXW14tpGGdcvCMVWo4rpfENhINxrkDqn4UdXLi2HbSmeeLxtIdopgSuGknAbGOLbuQD/jV6c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814860745429.5130195275333; Mon, 4 Sep 2023 01:07:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4be-0001Fw-PT; Mon, 04 Sep 2023 04:06:46 -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 1qd4bW-00014E-J0 for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:39 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bR-0006Mh-CV for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:38 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-118-ebzfLZf2MpGK1cW64yG4oQ-1; Mon, 04 Sep 2023 04:05:24 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E51303C1936C; Mon, 4 Sep 2023 08:05:23 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 86F4B400D277; Mon, 4 Sep 2023 08:05:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814790; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Rxn5EDYXEwVoQRGNPIOql5tx6O0sQvV/pW4KSk35sJQ=; b=imRcH9jaN+GXUh5sUdRj/feV3OKY3vlo1xNfJTNq3Ope9D+190weONenIV+UE1SUnIOG2U 9YiBZwLuKHewIzmRZ3UrmPf2+WKMpAeUJBjcN9iINjvLBReUVhBUNtQEQiwhcAsGkT48v0 5rmQQ45jAAyqLgok3xLDSIrG3ILT1iQ= X-MC-Unique: ebzfLZf2MpGK1cW64yG4oQ-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 09/13] util/reserved-region: Add new ReservedRegion helpers Date: Mon, 4 Sep 2023 10:03:52 +0200 Message-ID: <20230904080451.424731-10-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.129.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814862962100003 Content-Type: text/plain; charset="utf-8" Introduce resv_region_list_insert() helper which inserts a new ReservedRegion into a sorted list of reserved region. In case of overlap, the new region has higher priority and hides the existing overlapped segments. If the overlap is partial, new regions are created for parts which are not overlapped. The new region has higher priority independently on the type of the regions. Signed-off-by: Eric Auger Tested-by: Yanghang Liu --- include/qemu/reserved-region.h | 32 ++++++++++++ util/reserved-region.c | 94 ++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 127 insertions(+) create mode 100644 include/qemu/reserved-region.h create mode 100644 util/reserved-region.c diff --git a/include/qemu/reserved-region.h b/include/qemu/reserved-region.h new file mode 100644 index 0000000000..8e6f0a97e2 --- /dev/null +++ b/include/qemu/reserved-region.h @@ -0,0 +1,32 @@ +/* + * QEMU ReservedRegion helpers + * + * Copyright (c) 2023 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#ifndef QEMU_RESERVED_REGION_H +#define QEMU_RESERVED_REGION_H + +#include "exec/memory.h" + +/* + * Insert a new region into a sorted list of reserved regions. In case + * there is overlap with existing regions, the new added region has + * higher priority and replaces the overlapped segment. + */ +GList *resv_region_list_insert(GList *list, ReservedRegion *reg); + +#endif diff --git a/util/reserved-region.c b/util/reserved-region.c new file mode 100644 index 0000000000..bb26a6bed3 --- /dev/null +++ b/util/reserved-region.c @@ -0,0 +1,94 @@ +/* + * QEMU ReservedRegion helpers + * + * Copyright (c) 2023 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "qemu/osdep.h" +#include "qemu/range.h" +#include "qemu/reserved-region.h" + +GList *resv_region_list_insert(GList *list, ReservedRegion *reg) +{ + ReservedRegion *resv_iter, *new_reg; + Range *r =3D ®->range; + Range *range_iter; + GList *l; + + for (l =3D list; l ; ) { + resv_iter =3D (ReservedRegion *)l->data; + range_iter =3D &resv_iter->range; + + /* Skip all list elements strictly less than range to add */ + if (range_compare(range_iter, r) < 0) { + l =3D l->next; + } else if (range_compare(range_iter, r) > 0) { + return g_list_insert_before(list, l, reg); + } else { /* there is an operlap */ + if (range_contains_range(r, range_iter)) { + /* new range contains current item, simply remove this lat= ter */ + GList *prev =3D l->prev; + g_free(l->data); + list =3D g_list_delete_link(list, l); + if (prev) { + l =3D prev; + if (l) { + l =3D l->next; + } + } else { + l =3D list; + } + } else if (range_contains_range(range_iter, r)) { + /* new region is included in the current region */ + if (range_lob(range_iter) =3D=3D range_lob(r)) { + /* adjacent on the left side, derives into 2 regions */ + range_set_bounds(range_iter, range_upb(r) + 1, + range_upb(range_iter)); + return g_list_insert_before(list, l, reg); + } else if (range_upb(range_iter) =3D=3D range_upb(r)) { + /* adjacent on the right side, derives into 2 regions = */ + range_set_bounds(range_iter, range_lob(range_iter), + range_lob(r) - 1); + l =3D l->next; + } else { + uint64_t lob =3D range_lob(range_iter); + /* + * the new range is in the middle of an existing one, + * split this latter into 3 regs instead + */ + range_set_bounds(range_iter, range_upb(r) + 1, + range_upb(range_iter)); + new_reg =3D g_new0(ReservedRegion, 1); + new_reg->type =3D resv_iter->type; + range_set_bounds(&new_reg->range, + lob, range_lob(r) - 1); + list =3D g_list_insert_before(list, l, new_reg); + return g_list_insert_before(list, l, reg); + } + } else if (range_lob(r) < range_lob(range_iter)) { + range_set_bounds(range_iter, range_upb(r) + 1, + range_upb(range_iter)); + return g_list_insert_before(list, l, reg); + } else { /* intersection on the upper range */ + range_set_bounds(range_iter, range_lob(range_iter), + range_lob(r) - 1); + l =3D l->next; + } + } /* overlap */ + } + return g_list_append(list, reg); +} + diff --git a/util/meson.build b/util/meson.build index a375160286..7e85889606 100644 --- a/util/meson.build +++ b/util/meson.build @@ -52,6 +52,7 @@ util_ss.add(files('qdist.c')) util_ss.add(files('qht.c')) util_ss.add(files('qsp.c')) util_ss.add(files('range.c')) +util_ss.add(files('reserved-region.c')) util_ss.add(files('stats64.c')) util_ss.add(files('systemd.c')) util_ss.add(files('transactions.c')) --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814960; cv=none; d=zohomail.com; s=zohoarc; b=mraN+Mm6capp2NlBnRDskMNQWPSb/b+95KLRVtqJ9ACtvA0FZx8Ky079cBdY9q6ZZp47tsCSBkV0PrxHcVkyD4pXOPxHEWDQwKeKtf4+zi10eYV4bcum2u+bXXqE+vvf4JpS+2NT0ZNxqrBTe0lS8wc6D+YibdFDBeWJqeB6SwY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814960; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+oWpFb2g/83VBCkX6ZRLRafCOFZTt914xW2uQS8bpf0=; b=gSE4xmNIfRhOxyMWmPd2uAfJ0mItTR0hgeZHAvwLIX1cXtEyhQ5vFgWiJZ0keGQrMf8zD4RoUCA3CgYG2Rn75SG3CdrXHVYInzM9bHzq+XNAEhOP7Uj3oUMr74F03MPRpwugzbnoJEZoeVgAhbRij0Wl8sIftlQr1WLXyp+ENqs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814960391176.3751703735312; Mon, 4 Sep 2023 01:09:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4c5-0002MR-La; Mon, 04 Sep 2023 04:07:13 -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 1qd4c0-0002Av-BK for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:07:08 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bu-0006XF-JI for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:07:07 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-153-lWseRNySNoaqT0zV_E8IzQ-1; Mon, 04 Sep 2023 04:05:27 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C542F925FD4; Mon, 4 Sep 2023 08:05:26 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3F2F740ED77D; Mon, 4 Sep 2023 08:05:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814821; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+oWpFb2g/83VBCkX6ZRLRafCOFZTt914xW2uQS8bpf0=; b=VMsHE4w8uesobl4JTVm5uzhqrXh/KbM6bLRCMsWx9TaT1VIukGwhuGHqS4lDd1LM03FDYt KJaFFdv0c2AQM7ApIJozS59hdVQhc53MZ1x3E0qUGw8aqLmb75oFzMCTNiwGScUXWFmv44 APHYzFcjIjJw9PlAiVqCs7C2iKcMF7w= X-MC-Unique: lWseRNySNoaqT0zV_E8IzQ-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 10/13] virtio-iommu: Consolidate host reserved regions and property set ones Date: Mon, 4 Sep 2023 10:03:53 +0200 Message-ID: <20230904080451.424731-11-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.129.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814961112100001 Content-Type: text/plain; charset="utf-8" Up to now we were exposing to the RESV_MEM probe requests the reserved memory regions set though the reserved-regions array property. Combine those with the host reserved memory regions if any. Those latter are tagged as RESERVED. We don't have more information about them besides then cannot be mapped. Reserved regions set by property have higher priority. Signed-off-by: Eric Auger Tested-by: Yanghang Liu --- hw/virtio/virtio-iommu.c | 17 ++++++++++++++++- hw/virtio/trace-events | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 02f1a59d57..d260235078 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -21,6 +21,7 @@ #include "qemu/log.h" #include "qemu/iov.h" #include "qemu/range.h" +#include "qemu/reserved-region.h" #include "exec/target_page.h" #include "hw/qdev-properties.h" #include "hw/virtio/virtio.h" @@ -630,11 +631,25 @@ static int consolidate_resv_regions(IOMMUDevice *sdev) VirtIOIOMMU *s =3D sdev->viommu; int i; =20 + /* First add host reserved regions if any, all tagged as RESERVED */ + for (i =3D 0; i < sdev->nr_host_resv_regions; i++) { + ReservedRegion *reg =3D g_new0(ReservedRegion, 1); + reg->range =3D sdev->host_resv_regions[i]; + reg->type =3D VIRTIO_IOMMU_RESV_MEM_T_RESERVED; + sdev->resv_regions =3D resv_region_list_insert(sdev->resv_regions,= reg); + trace_virtio_iommu_host_resv_regions(sdev->iommu_mr.parent_obj.nam= e, i, + range_lob(®->range), + range_upb(®->range)); + } + /* + * then add higher priority reserved regions set through properties by= the + * machine + */ for (i =3D 0; i < s->nr_prop_resv_regions; i++) { ReservedRegion *reg =3D g_new0(ReservedRegion, 1); =20 *reg =3D s->prop_resv_regions[i]; - sdev->resv_regions =3D g_list_append(sdev->resv_regions, reg); + sdev->resv_regions =3D resv_region_list_insert(sdev->resv_regions,= reg); } return 0; } diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 7109cf1a3b..796574b0f3 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -134,6 +134,7 @@ virtio_iommu_notify_flag_add(const char *name) "add not= ifier to mr %s" virtio_iommu_notify_flag_del(const char *name) "del notifier from mr %s" virtio_iommu_switch_address_space(uint8_t bus, uint8_t slot, uint8_t fn, b= ool on) "Device %02x:%02x.%x switching address space (iommu enabled=3D%d)" virtio_iommu_freeze_granule(uint64_t page_size_mask) "granule set to 0x%"P= RIx64 +virtio_iommu_host_resv_regions(const char *name, uint32_t index, uint64_t = lob, uint64_t upb) "mr=3D%s host-resv-reg[%d] =3D [0x%"PRIx64",0x%"PRIx64"]" =20 # virtio-mem.c virtio_mem_send_response(uint16_t type) "type=3D%" PRIu16 --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814869; cv=none; d=zohomail.com; s=zohoarc; b=WdDhgX2X4dz69Hz308GwVfG3+VBgXEUgc9rA4IyqQskSfhq10CLwWvuLnIE3WpIM6SyCtPz4JGul/KjMXmRLD3KDghHdiuCpOupFjlCzt0Rk1QG/KO+dUBMsQHk2qUMUCaeXCieVlX13DSNTelQ+aBSlt5897mW6M9Yoi3AHops= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814869; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Xmpz4K6SW2/WoItBTO6+xpT/XaONGuH9RQ+gRhGH858=; b=R+qiHKs/ieebeuM1gl8hfAWYmcMr2b2Yz23vF/1FvUI8qF/zfnywIi8prPEsBEZ43rx7mbOHAOAcbaIxaTDyvoo9iIFkbiR1BQvTrASIeGEey2gswVXmHC0c27CsoGO3M89tw941cRcbta97I03BwfDkW7WYQSdOzNiCaEluZaI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814869132419.6609191600595; Mon, 4 Sep 2023 01:07:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4bh-0001Np-80; Mon, 04 Sep 2023 04:06:49 -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 1qd4be-0001GB-PR for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:46 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bc-0006Q2-5a for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:06:46 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-125-QS9-w2I5N-muVdVfqjayLg-1; Mon, 04 Sep 2023 04:05:29 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 74A981E441D3; Mon, 4 Sep 2023 08:05:29 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 185D040C6CCC; Mon, 4 Sep 2023 08:05:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814799; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Xmpz4K6SW2/WoItBTO6+xpT/XaONGuH9RQ+gRhGH858=; b=glxTRLddf2yuU8ZWDQGBngI0BPXDww/06orVSMzXp7gc3DpHAD5/w5L9JrUFVwCcRTgnUJ 6bbG4V9cjL5E1J4Qug0KR2WGTbnrc5pE2qfNrjKYlVO5zO/z1xnAkKy6M6k74BmENM7gih C/1uQy+EOoJzNJwPxYL83NF27JsPKSY= X-MC-Unique: QS9-w2I5N-muVdVfqjayLg-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 11/13] test: Add some tests for range and resv-mem helpers Date: Mon, 4 Sep 2023 10:03:54 +0200 Message-ID: <20230904080451.424731-12-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.129.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814871259100003 Content-Type: text/plain; charset="utf-8" Add unit tests for both resv_region_list_insert() and range_inverse_array(). Signed-off-by: Eric Auger Tested-by: Yanghang Liu --- tests/unit/test-resv-mem.c | 198 +++++++++++++++++++++++++++++++++++++ tests/unit/meson.build | 1 + 2 files changed, 199 insertions(+) create mode 100644 tests/unit/test-resv-mem.c diff --git a/tests/unit/test-resv-mem.c b/tests/unit/test-resv-mem.c new file mode 100644 index 0000000000..35c9828375 --- /dev/null +++ b/tests/unit/test-resv-mem.c @@ -0,0 +1,198 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * reserved-region/range.c unit-tests. + * + * Copyright (C) 2023, Red Hat, Inc. + * + * Author: Eric Auger + */ + +#include "qemu/osdep.h" +#include "qemu/range.h" +#include "exec/memory.h" +#include "qemu/reserved-region.h" + +#define DEBUG 0 + +static void print_rev_array(const char *prefix, uint32_t nr_rev, + Range *rev, uint32_t expected_nr_rev) +{ + g_assert_cmpint(nr_rev, =3D=3D, expected_nr_rev); +#if DEBUG + printf("%s nr_rev=3D%d\n", prefix, nr_rev); + for (int i =3D 0; i < nr_rev; i++) { + printf("%s rev[%i] =3D [0x%"PRIx64",0x%"PRIx64"]\n", + prefix, i, range_lob(&rev[i]), range_upb(&rev[i])); + } +#endif +} + +static void check_range_reverse_array(void) +{ + Range r[10]; + Range *rev; + uint32_t nr_rev; + + range_set_bounds(&r[0], 0x10000, UINT64_MAX); + range_inverse_array(1, r, &nr_rev, &rev); + print_rev_array("test1", nr_rev, rev, 1); + g_free(rev); + + range_set_bounds(&r[0], 0x10000, 0xFFFFFFFFFFFF); + range_inverse_array(1, r, &nr_rev, &rev); + print_rev_array("test2", nr_rev, rev, 2); + g_free(rev); + + range_set_bounds(&r[0], 0x0, 0xFFFF); + range_set_bounds(&r[1], 0x10000, 0x2FFFF); + range_inverse_array(2, r, &nr_rev, &rev); + print_rev_array("test3", nr_rev, rev, 1); + g_free(rev); + + range_set_bounds(&r[0], 0x50000, 0x5FFFF); + range_set_bounds(&r[1], 0x60000, 0xFFFFFFFFFFFF); + range_inverse_array(2, r, &nr_rev, &rev); + print_rev_array("test4", nr_rev, rev, 2); + g_free(rev); + + range_set_bounds(&r[0], 0x0, UINT64_MAX); + range_inverse_array(1, r, &nr_rev, &rev); + print_rev_array("test5", nr_rev, rev, 0); + g_free(rev); +} + +static ReservedRegion *alloc_resv_mem(unsigned type, uint64_t lob, uint64_= t upb) +{ + ReservedRegion *r; + + r =3D g_new0(ReservedRegion, 1); + r->type =3D type; + range_set_bounds(&r->range, lob, upb); + return r; +} + +static void print_resv_region_list(const char *prefix, GList *list, + uint32_t expected_length) +{ + int i =3D g_list_length(list); + + g_assert_cmpint(i, =3D=3D, expected_length); +#if DEBUG + i =3D 0; + for (GList *l =3D list; l; l =3D l->next) { + ReservedRegion *r =3D (ReservedRegion *)l->data; + Range *range =3D &r->range; + + printf("%s item[%d]=3D[0x%x, 0x%"PRIx64", 0x%"PRIx64"]\n", + prefix, i++, r->type, range_lob(range), range_upb(range)); + } +#endif +} + +static void free_resv_region(gpointer data) +{ + ReservedRegion *reg =3D (ReservedRegion *)data; + + g_free(reg); +} + +static void check_resv_region_list_insert(void) +{ + ReservedRegion *r[10]; + GList *l =3D NULL; + + r[0] =3D alloc_resv_mem(0xA, 0, 0xFFFF); + r[1] =3D alloc_resv_mem(0xA, 0x20000, 0x2FFFF); + l =3D resv_region_list_insert(l, r[0]); + l =3D resv_region_list_insert(l, r[1]); + print_resv_region_list("test1", l, 2); + + /* adjacent on left */ + r[2] =3D alloc_resv_mem(0xB, 0x0, 0xFFF); + l =3D resv_region_list_insert(l, r[2]); + /* adjacent on right */ + r[3] =3D alloc_resv_mem(0xC, 0x21000, 0x2FFFF); + l =3D resv_region_list_insert(l, r[3]); + print_resv_region_list("test2", l, 4); + + /* exact overlap of D into C*/ + r[4] =3D alloc_resv_mem(0xD, 0x21000, 0x2FFFF); + l =3D resv_region_list_insert(l, r[4]); + print_resv_region_list("test3", l, 4); + + /* in the middle */ + r[5] =3D alloc_resv_mem(0xE, 0x22000, 0x23FFF); + l =3D resv_region_list_insert(l, r[5]); + print_resv_region_list("test4", l, 6); + + /* overwrites several existing ones */ + r[6] =3D alloc_resv_mem(0xF, 0x10000, 0x2FFFF); + l =3D resv_region_list_insert(l, r[6]); + print_resv_region_list("test5", l, 3); + + /* contiguous at the end */ + r[7] =3D alloc_resv_mem(0x0, 0x30000, 0x40000); + l =3D resv_region_list_insert(l, r[7]); + print_resv_region_list("test6", l, 4); + + g_list_free_full(l, free_resv_region); + l =3D NULL; + + r[0] =3D alloc_resv_mem(0x0, 0x10000, 0x1FFFF); + l =3D resv_region_list_insert(l, r[0]); + /* insertion before the 1st item */ + r[1] =3D alloc_resv_mem(0x1, 0x0, 0xFF); + l =3D resv_region_list_insert(l, r[1]); + print_resv_region_list("test8", l, 2); + + /* collision on the left side */ + r[2] =3D alloc_resv_mem(0xA, 0x1200, 0x11FFF); + l =3D resv_region_list_insert(l, r[2]); + print_resv_region_list("test9", l, 3); + + /* collision on the right side */ + r[3] =3D alloc_resv_mem(0xA, 0x1F000, 0x2FFFF); + l =3D resv_region_list_insert(l, r[3]); + print_resv_region_list("test10", l, 4); + + /* override everything */ + r[4] =3D alloc_resv_mem(0xF, 0x0, UINT64_MAX); + l =3D resv_region_list_insert(l, r[4]); + print_resv_region_list("test11", l, 1); + + g_list_free_full(l, free_resv_region); + l =3D NULL; + + r[0] =3D alloc_resv_mem(0xF, 0x1000000000000, UINT64_MAX); + l =3D resv_region_list_insert(l, r[0]); + print_resv_region_list("test12", l, 1); + + r[1] =3D alloc_resv_mem(0xA, 0x0, 0xFFFFFFF); + l =3D resv_region_list_insert(l, r[1]); + print_resv_region_list("test12", l, 2); + + r[2] =3D alloc_resv_mem(0xB, 0x100000000, 0x1FFFFFFFF); + l =3D resv_region_list_insert(l, r[2]); + print_resv_region_list("test12", l, 3); + + r[3] =3D alloc_resv_mem(0x0, 0x010000000, 0x2FFFFFFFF); + l =3D resv_region_list_insert(l, r[3]); + print_resv_region_list("test12", l, 3); + + g_list_free_full(l, free_resv_region); +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/resv-mem/range_reverse_array", + check_range_reverse_array); + g_test_add_func("/resv-mem/resv_region_list_insert", + check_resv_region_list_insert); + + g_test_run(); + + return 0; +} diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 93977cc32d..765b1a8dad 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -21,6 +21,7 @@ tests =3D { 'test-opts-visitor': [testqapi], 'test-visitor-serialization': [testqapi], 'test-bitmap': [], + 'test-resv-mem': [], # all code tested by test-x86-cpuid is inside topology.h 'test-x86-cpuid': [], 'test-cutils': [], --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814947; cv=none; d=zohomail.com; s=zohoarc; b=QZZXfS7LwCuxQrehmmdj1B2Wg8iIDV+9mlg7frjpKe7QLNNvTIAkY1OXf1Q5pY2a00LyZLSCZlpev+OlgPJADD58Z1tBuSYnc4UsnJegxo+Xb9Yyx81AcOdmZ8BBcwfKmIgruHQun63m79+R7IlEPvQ2y4bSFIvcLYpIMuWlFKI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814947; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9XjeJbiDNaXUaAllo9WuRHroYJyNuZaKG8WZqJbDNBw=; b=ctvr878HCY/Daf9oaw7cqGOxVBkL1ggP2vfYngdE5fdjigol2ee9TANC4y4pRKOZSPNr6p7nHntJIWNiDV3IG/pJ+xfr3MWFWLuo++mIhY+ma9cGP5iialkUcpyFVz3n9qWQSUdPR1gnFoaSAz3DNQyEx0u9V0gzKdJ41R8Ky18= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814947486289.38859460559945; Mon, 4 Sep 2023 01:09:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4c1-0002CP-1q; Mon, 04 Sep 2023 04:07:09 -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 1qd4by-00022w-7d for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:07:06 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bt-0006Wz-UP for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:07:05 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-501-744gbhUMPsG4Gi2DiD6G9w-1; Mon, 04 Sep 2023 04:05:33 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CE6D9925FC9; Mon, 4 Sep 2023 08:05:32 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id C3F6340D2962; Mon, 4 Sep 2023 08:05:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814821; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9XjeJbiDNaXUaAllo9WuRHroYJyNuZaKG8WZqJbDNBw=; b=D/LyQ6vT0QnOwh9zaHv9dSsaN+nx4f6TYr5ZuIvz/e/ccBEgNqw6FrkFtjSscCsSe/jhpp 2o5XQrKzCNxZkNMniyqc22goxLlSmt5rmY7BVJhL0uUxemIMxFdj3UqJzCwkTt6TV/6+LG PgT44p17pjPGnjitD04PEie5rNhSmjw= X-MC-Unique: 744gbhUMPsG4Gi2DiD6G9w-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 12/13] virtio-iommu: Resize memory region according to the max iova info Date: Mon, 4 Sep 2023 10:03:55 +0200 Message-ID: <20230904080451.424731-13-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.133.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814948837100003 Content-Type: text/plain; charset="utf-8" By default the virtio-iommu MR has a 64b span. As we intend to remove the VFIO assumption of 64b IOVA, let's make sure the MR is resized according to the actual GAW of the physical IOMMU. Otherwise we will get a failure on vfio vfio_find_hostwin(). Signed-off-by: Eric Auger Reviewed-by: Philippe Mathieu-Daud=C3=A9 Tested-by: Yanghang Liu --- hw/virtio/virtio-iommu.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index d260235078..d877119df1 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -1180,8 +1180,14 @@ static int virtio_iommu_set_iova_ranges(IOMMUMemoryR= egion *mr, Error **errp) { IOMMUDevice *sdev =3D container_of(mr, IOMMUDevice, iommu_mr); + uint64_t max_iova; + assert(nr_ranges); =20 + max_iova =3D range_upb(&iova_ranges[nr_ranges - 1]); + if (max_iova < UINT64_MAX) { + memory_region_set_size(&mr->parent_obj, max_iova + 1); + } =20 range_inverse_array(nr_ranges, iova_ranges, &sdev->nr_host_resv_regions, &sdev->host_resv_regi= ons); --=20 2.41.0 From nobody Thu Nov 28 06:48:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693814909; cv=none; d=zohomail.com; s=zohoarc; b=EYCmMwUqPfuozXoeo9U3/lOPDz6yeneZpVceWXYo84tw7gUDIBIFsAnnoGpxfhCZrk8pAcn0hvue7fVjBS4+oy+zE2xy4XxcfhYNlOQXSoDkHmHIJUYzFhJp1E9Fzcpm4Qw2EuBKzU1AKTHzvgCf5hkW70HN4FiBPn9ymMwRTjA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693814909; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cdJyzro/1PW1wpTBijWjAZMD3UPhZ9MGOFDP+xuEvXk=; b=PS9IOiFU7/wRqFm0CGRL1cDPOmmDeYnQZf4rn461Vssv7dd5khGx7NCBBqZ1jAtMa48KIhAAHXK/xZTovvuvWMLhgEVJSOqr2Hf0RPN6w3QVp+I9CyvVtUMBSHEbSFzBHyxtIgvKMSgOlh37UcQUUiwSPwMZxW92vBab5BXNAwA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693814909117572.9730864571392; Mon, 4 Sep 2023 01:08:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd4by-00023B-CB; Mon, 04 Sep 2023 04:07:06 -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 1qd4bw-0001zF-4f for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:07:04 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd4bt-0006Wq-Q5 for qemu-devel@nongnu.org; Mon, 04 Sep 2023 04:07:03 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-594-fRGHcY_WMvqyLpc1b289WA-1; Mon, 04 Sep 2023 04:05:36 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C953F3804519; Mon, 4 Sep 2023 08:05:35 +0000 (UTC) Received: from laptop.redhat.com (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 271A740C6CCC; Mon, 4 Sep 2023 08:05:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693814821; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cdJyzro/1PW1wpTBijWjAZMD3UPhZ9MGOFDP+xuEvXk=; b=Qbe4nkw+lvP6YcAG+qdRdQuC0JK2ietyHiN4sWV8BNjRNeIdlgi63ryFDfEQkk4clftK4b OlWpzt1WDKrNx0tVXQmepOaDuZnnuI6DXvzq8uU7jvMTZ4Z9gVvUle6ZW1WPEg8ae/+DRU qTyKdkQLAg2ocUpmdlVelgwJcv6OmOw= X-MC-Unique: fRGHcY_WMvqyLpc1b289WA-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, jean-philippe@linaro.org, mst@redhat.com, pbonzini@redhat.com Cc: peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: [PATCH 13/13] vfio: Remove 64-bit IOVA address space assumption Date: Mon, 4 Sep 2023 10:03:56 +0200 Message-ID: <20230904080451.424731-14-eric.auger@redhat.com> In-Reply-To: <20230904080451.424731-1-eric.auger@redhat.com> References: <20230904080451.424731-1-eric.auger@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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=170.10.129.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693814910570100002 Content-Type: text/plain; charset="utf-8" Now we retrieve the usable IOVA ranges from the host, let's remove the assumption of 64b IOVA space when calling vfio_host_win_add(). Make sure the vfio_find_hostwin() gets called after we get a chance to call set_iova_regions and resize the IOMMU MR according to the actual physical GAW. Signed-off-by: Eric Auger Tested-by: Yanghang Liu --- hw/vfio/common.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 26da38de05..40cac1ca91 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1112,13 +1112,6 @@ static void vfio_listener_region_add(MemoryListener = *listener, #endif } =20 - hostwin =3D vfio_find_hostwin(container, iova, end); - if (!hostwin) { - error_setg(&err, "Container %p can't map guest IOVA region" - " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx, container, iova, e= nd); - goto fail; - } - memory_region_ref(section->mr); =20 if (memory_region_is_iommu(section->mr)) { @@ -1177,6 +1170,14 @@ static void vfio_listener_region_add(MemoryListener = *listener, return; } =20 + hostwin =3D vfio_find_hostwin(container, iova, end); + if (!hostwin) { + error_setg(&err, "Container %p can't map guest IOVA region" + " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx, container, iova, e= nd); + goto fail; + } + + /* Here we assume that memory_region_is_ram(section->mr)=3D=3Dtrue */ =20 /* @@ -2594,12 +2595,10 @@ static int vfio_connect_container(VFIOGroup *group,= AddressSpace *as, vfio_get_iommu_info_migration(container, info); g_free(info); =20 - /* - * FIXME: We should parse VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE - * information to get the actual window extent rather than assume - * a 64-bit IOVA address space. - */ - vfio_host_win_add(container, 0, (hwaddr)-1, container->pgsizes); + g_assert(container->nr_iovas); + vfio_host_win_add(container, 0, + container->iova_ranges[container->nr_iovas - 1].= end, + container->pgsizes); =20 break; } --=20 2.41.0