From nobody Mon Nov 25 09:27:42 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=1716372086; cv=none; d=zohomail.com; s=zohoarc; b=RvipRv1eIV5jNhUyX5My/xqfOdQCDmmNYmjFW3Y+d+uX3Z55GuinwQhEWeZiucT5jbbz1UMx4KcI3MYy6Od1YNSFul742P/Ckf/xOXjbrujPJNpG/aLgA4F95IkxpPlXhXt1QG/tttvzbb6ygN3ToED6cyC1F8zCfNwOIKW+uT0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716372086; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xEgW3e/XEuQ2cctgyXjEEqmbcF5A+1Z7ssJBUhFQbnc=; b=oAo5NBKP5OYlH05XZM4TBr/dDMorVgwCllKc2RLmAiYfrYQoPCtuGjtYLq1B0c4MCHCsGeiRbcGnjuYTVu9kYEXLY+2znDsj1EkTwXU/X5n93+ZMQlQUr5+Vhhd1iXyhfxK5amWD0v7Tbzyoms/PIzfbQOUoq7xquc3qQbq+//M= 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 171637208682820.22662853653003; Wed, 22 May 2024 03:01:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s9igh-0003P4-OM; Wed, 22 May 2024 05:55:11 -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 1s9igb-0003LQ-2y for qemu-devel@nongnu.org; Wed, 22 May 2024 05:55:05 -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 1s9igZ-0001Mg-5v for qemu-devel@nongnu.org; Wed, 22 May 2024 05:55:04 -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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-277-Rz1qrY-fO9y4d9ylXtfhSw-1; Wed, 22 May 2024 05:54:59 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3A5E01C29EA7; Wed, 22 May 2024 09:54:59 +0000 (UTC) Received: from corto.redhat.com (unknown [10.39.192.133]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8EE6251BF; Wed, 22 May 2024 09:54:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716371702; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xEgW3e/XEuQ2cctgyXjEEqmbcF5A+1Z7ssJBUhFQbnc=; b=ULYADHlkQfK15puymEiimqHGezMez/OWS690KL2WDMJyqLu0GEmgnUNSImo8oAAEE+BP2a YNMwQOMr5HXtg3mneFqvGT4fqWsvuEdGzHObIlwUrJnWLG8fsAtpjoVjYLlmcLIlgkORbz foYP2m3V1ozR4vFR8UBrcHj5w9bPcQg= X-MC-Unique: Rz1qrY-fO9y4d9ylXtfhSw-1 From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: qemu-devel@nongnu.org Cc: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , "Michael S . Tsirkin" , Paolo Bonzini , David Hildenbrand , Peter Xu , Eric Auger , Avihai Horon Subject: [PULL 07/47] memory: Add Error** argument to memory_get_xlat_addr() Date: Wed, 22 May 2024 11:54:02 +0200 Message-ID: <20240522095442.195243-8-clg@redhat.com> In-Reply-To: <20240522095442.195243-1-clg@redhat.com> References: <20240522095442.195243-1-clg@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 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=clg@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: 1716372104506100007 Let the callers do the reporting. This will be useful in vfio_iommu_map_dirty_notify(). Cc: Michael S. Tsirkin Cc: Paolo Bonzini Cc: David Hildenbrand Reviewed-by: Peter Xu Reviewed-by: Eric Auger Reviewed-by: Avihai Horon Signed-off-by: C=C3=A9dric Le Goater --- include/exec/memory.h | 15 ++++++++++++++- hw/vfio/common.c | 13 +++++++++---- hw/virtio/vhost-vdpa.c | 5 ++++- system/memory.c | 10 +++++----- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index d417d7f363dbbca6553c449582a93d5da73cca40..9cdd64e9c69b63f9d27cebc2e8c= b366e22ed7577 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -774,9 +774,22 @@ void ram_discard_manager_register_listener(RamDiscardM= anager *rdm, void ram_discard_manager_unregister_listener(RamDiscardManager *rdm, RamDiscardListener *rdl); =20 +/** + * memory_get_xlat_addr: Extract addresses from a TLB entry + * + * @iotlb: pointer to an #IOMMUTLBEntry + * @vaddr: virtual address + * @ram_addr: RAM address + * @read_only: indicates if writes are allowed + * @mr_has_discard_manager: indicates memory is controlled by a + * RamDiscardManager + * @errp: pointer to Error*, to store an error if it happens. + * + * Return: true on success, else false setting @errp with error. + */ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, ram_addr_t *ram_addr, bool *read_only, - bool *mr_has_discard_manager); + bool *mr_has_discard_manager, Error **errp); =20 typedef struct CoalescedMemoryRange CoalescedMemoryRange; typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; diff --git a/hw/vfio/common.c b/hw/vfio/common.c index c7f274fb5c851e4c44498552891265018d2c5313..7313043f1d161ed0326b5ba3fa1= 085608eaf6740 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -253,12 +253,13 @@ static bool vfio_listener_skipped_section(MemoryRegio= nSection *section) =20 /* Called with rcu_read_lock held. */ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, - ram_addr_t *ram_addr, bool *read_only) + ram_addr_t *ram_addr, bool *read_only, + Error **errp) { bool ret, mr_has_discard_manager; =20 ret =3D memory_get_xlat_addr(iotlb, vaddr, ram_addr, read_only, - &mr_has_discard_manager); + &mr_has_discard_manager, errp); if (ret && mr_has_discard_manager) { /* * Malicious VMs might trigger discarding of IOMMU-mapped memory. = The @@ -288,6 +289,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOM= MUTLBEntry *iotlb) hwaddr iova =3D iotlb->iova + giommu->iommu_offset; void *vaddr; int ret; + Error *local_err =3D NULL; =20 trace_vfio_iommu_map_notify(iotlb->perm =3D=3D IOMMU_NONE ? "UNMAP" : = "MAP", iova, iova + iotlb->addr_mask); @@ -304,7 +306,8 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOM= MUTLBEntry *iotlb) if ((iotlb->perm & IOMMU_RW) !=3D IOMMU_NONE) { bool read_only; =20 - if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only)) { + if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, &local_er= r)) { + error_report_err(local_err); goto out; } /* @@ -1213,6 +1216,7 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier= *n, IOMMUTLBEntry *iotlb) VFIOContainerBase *bcontainer =3D giommu->bcontainer; hwaddr iova =3D iotlb->iova + giommu->iommu_offset; ram_addr_t translated_addr; + Error *local_err =3D NULL; int ret =3D -EINVAL; =20 trace_vfio_iommu_map_dirty_notify(iova, iova + iotlb->addr_mask); @@ -1224,7 +1228,8 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier= *n, IOMMUTLBEntry *iotlb) } =20 rcu_read_lock(); - if (!vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL)) { + if (!vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL, &local_er= r)) { + error_report_err(local_err); goto out_unlock; } =20 diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index e827b9175fc61f1ef419e48d90a440b00449312a..ed99ab87457d8f31b98ace96071= 3f48d47b27102 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -208,6 +208,7 @@ static void vhost_vdpa_iommu_map_notify(IOMMUNotifier *= n, IOMMUTLBEntry *iotlb) void *vaddr; int ret; Int128 llend; + Error *local_err =3D NULL; =20 if (iotlb->target_as !=3D &address_space_memory) { error_report("Wrong target AS \"%s\", only system memory is allowe= d", @@ -227,7 +228,9 @@ static void vhost_vdpa_iommu_map_notify(IOMMUNotifier *= n, IOMMUTLBEntry *iotlb) if ((iotlb->perm & IOMMU_RW) !=3D IOMMU_NONE) { bool read_only; =20 - if (!memory_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, NULL)) { + if (!memory_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, NULL, + &local_err)) { + error_report_err(local_err); return; } ret =3D vhost_vdpa_dma_map(s, VHOST_VDPA_GUEST_PA_ASID, iova, diff --git a/system/memory.c b/system/memory.c index 642a449f8c867d38c62a748a4dfd5c055637c205..9540caa8a1f4da8501bf5ae9d7e= edde8b775e1dc 100644 --- a/system/memory.c +++ b/system/memory.c @@ -2179,7 +2179,7 @@ void ram_discard_manager_unregister_listener(RamDisca= rdManager *rdm, /* Called with rcu_read_lock held. */ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, ram_addr_t *ram_addr, bool *read_only, - bool *mr_has_discard_manager) + bool *mr_has_discard_manager, Error **errp) { MemoryRegion *mr; hwaddr xlat; @@ -2197,7 +2197,7 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void = **vaddr, mr =3D address_space_translate(&address_space_memory, iotlb->translate= d_addr, &xlat, &len, writable, MEMTXATTRS_UNSPECI= FIED); if (!memory_region_is_ram(mr)) { - error_report("iommu map to non memory area %" HWADDR_PRIx "", xlat= ); + error_setg(errp, "iommu map to non memory area %" HWADDR_PRIx "", = xlat); return false; } else if (memory_region_has_ram_discard_manager(mr)) { RamDiscardManager *rdm =3D memory_region_get_ram_discard_manager(m= r); @@ -2216,8 +2216,8 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void = **vaddr, * were already restored before IOMMUs are restored. */ if (!ram_discard_manager_is_populated(rdm, &tmp)) { - error_report("iommu map to discarded memory (e.g., unplugged v= ia" - " virtio-mem): %" HWADDR_PRIx "", + error_setg(errp, "iommu map to discarded memory (e.g., unplugg= ed" + " via virtio-mem): %" HWADDR_PRIx "", iotlb->translated_addr); return false; } @@ -2228,7 +2228,7 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void = **vaddr, * check that it did not truncate too much. */ if (len & iotlb->addr_mask) { - error_report("iommu has granularity incompatible with target AS"); + error_setg(errp, "iommu has granularity incompatible with target A= S"); return false; } =20 --=20 2.45.1