From nobody Tue Dec 16 02:14:54 2025 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=reject dis=none) header.from=oracle.com ARC-Seal: i=1; a=rsa-sha256; t=1747415665; cv=none; d=zohomail.com; s=zohoarc; b=ODqGz8W9WPSYfXB2VH1+QovHvBs/2Mb9M0jjIGOkzFKYonQeKEDdlEfA77QxNMK+K/dL3orAS/6i49FweBPFGH3wXprn0YhzuSDz3nGElOMfiFDLpe0NwO6nxYheBkD+jisNu/U5/3N+HOtYUfusm5/a/ri6j6NZ3FACWZwWUX0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747415665; h=Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=F9EWuz2eOXfCnH8Etx2r5WfzfC+3jodB9QW6zseGQ/I=; b=XaQFlIwAOh5TuGoCpbl8GRkMGpBAJSOB9fQyddBJgbJRQh1aK2K8ykqlIVpaYLcuWjOL/0QgEwcE0dqUc4bjjw5w1loFTq1Ad4LLMKYs5QV5UJn78lRhWDFGXX9iwFegwHEOfaqdByQYhPczb60tPyVcD2XzE7U7QXT0PRF90wo= 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=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747415664815230.22436690918505; Fri, 16 May 2025 10:14:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uFyco-0002BT-3q; Fri, 16 May 2025 13:13:34 -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 1uFycj-0002Ay-B2 for qemu-devel@nongnu.org; Fri, 16 May 2025 13:13:30 -0400 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFyce-0003cn-LB for qemu-devel@nongnu.org; Fri, 16 May 2025 13:13:29 -0400 Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 54GGfu1n021982; Fri, 16 May 2025 17:13:21 GMT Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 46nrbj1ud1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 May 2025 17:13:21 +0000 (GMT) Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 54GGECT1004251; Fri, 16 May 2025 17:13:20 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 46mrmfkqbj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 May 2025 17:13:20 +0000 Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 54GHAvO7022758; Fri, 16 May 2025 17:13:20 GMT Received: from ca-dev63.us.oracle.com (ca-dev63.us.oracle.com [10.211.8.221]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 46mrmfkqb7-1; Fri, 16 May 2025 17:13:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :date:from:message-id:subject:to; s=corp-2025-04-25; bh=F9EWuz2e OXfCnH8Etx2r5WfzfC+3jodB9QW6zseGQ/I=; b=cpPs1wL0QcB57Igu2TS9ewT0 DFfNaA9w4Uy5ybu0Shj57dROrjs1C8EMkJb1LmtbeYl8aetqzpcuFhYFixPJ2OFj Ze60NUnJblEvlNGi2cmaQRFWDCBcXj1rHuZyNgrWr1v8JpPoAq4Iki7PATb+UeCc c/6rl5Dhyj6PsgxvqJa7eGjyU0aXQ37MCS6jw0d2DKeFGptNmATlMaBCNd46vdqK DD7dHP4j+429L9AVkvTRGgHIJ6+mzVxCHPxFXEimKK9exPCH8LSs+w8968d5Davk vNyEBqYIHRVZ1VSaWWu2+zLGNabU/nMQ/AEviog4KMK1bPXFMMiXapglRIc0MQ== From: Steve Sistare To: qemu-devel@nongnu.org Cc: Marc-Andre Lureau , Thanos Makatos , "Daniel P. Berrange" , Paolo Bonzini , Peter Xu , David Hildenbrand , Cedric Le Goater , Stefano Garzarella , "Michael S. Tsirkin" , Alex Williamson , Philippe Mathieu-Daude , John Levon , Steve Sistare Subject: [PATCH V4] vfio: return mr from vfio_get_xlat_addr Date: Fri, 16 May 2025 10:13:19 -0700 Message-Id: <1747415599-131553-1-git-send-email-steven.sistare@oracle.com> X-Mailer: git-send-email 1.8.3.1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1099,Hydra:6.0.736,FMLib:17.12.80.40 definitions=2025-05-16_05,2025-05-16_03,2025-03-28_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 spamscore=0 suspectscore=0 mlxscore=0 phishscore=0 mlxlogscore=999 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2505070000 definitions=main-2505160168 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUwNTE2MDE2OCBTYWx0ZWRfX22MfLSaR5OzS jlb2g2pQOmwh32nvea0A8+TOknRJfmr/ar3E68YxAvPzILigXAP9G+/tCIRZyxkiDgSm/zGt6Zy IxWHCYXCp3GtbHh8vNEw1aoVpwI7R8pB0YYOJMCO8VYY3I7UyhfYWd3ZOtf45yzAiiRypnbk+aA EbOcf87rKQBeGRcoi2OpXW3NMEIOTebOTjJf8tFWDCA2YRort1DbsWFDTqLZwSLnJr5RX2IMUTE ewS2A3p3yFHyoxx4VBjYYXjCqU+PF7GwIIvls7VZJ9xWt+hFrUz1dDLuQui1+C7ZA04X8Ckv7f9 YZBS1xWZKEPlQnLUIp0Mt6dincF/a87KYdcDF5nUipoQay9QAWHmScaKO2XMhlbXULA/3lMvHKQ 477Q6qS0VnW937zV33qCHwkAW4MdbKc+3YWvU/zmcFaQ9gHWYEgTeDQcOlFDECVONiVD0Los X-Proofpoint-GUID: cwh2aia3AiWMofcSvkK31meE-gfuujZT X-Proofpoint-ORIG-GUID: cwh2aia3AiWMofcSvkK31meE-gfuujZT X-Authority-Analysis: v=2.4 cv=YqwPR5YX c=1 sm=1 tr=0 ts=68277231 cx=c_pps a=OOZaFjgC48PWsiFpTAqLcw==:117 a=OOZaFjgC48PWsiFpTAqLcw==:17 a=dt9VzEwgFbYA:10 a=yPCof4ZbAAAA:8 a=QNV_3Zso053vk46vt-IA:9 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=205.220.165.32; envelope-from=steven.sistare@oracle.com; helo=mx0a-00069f02.pphosted.com X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.499, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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 @oracle.com) X-ZM-MESSAGEID: 1747415667003116600 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Modify memory_get_xlat_addr and vfio_get_xlat_addr to return the memory region that the translated address is found in. This will be needed by CPR in a subsequent patch to map blocks using IOMMU_IOAS_MAP_FILE. Also return the xlat offset, so we can simplify the interface by removing the out parameters that can be trivially derived from mr and xlat. Lastly, rename the functions to to memory_translate_iotlb() and vfio_translate_iotlb(). Signed-off-by: Steve Sistare Acked-by: David Hildenbrand --- hw/vfio/listener.c | 33 ++++++++++++++++++++++----------- hw/virtio/vhost-vdpa.c | 9 +++++++-- include/system/memory.h | 19 +++++++++---------- system/memory.c | 32 +++++++------------------------- 4 files changed, 45 insertions(+), 48 deletions(-) diff --git a/hw/vfio/listener.c b/hw/vfio/listener.c index bfacb3d..a4931f1 100644 --- a/hw/vfio/listener.c +++ b/hw/vfio/listener.c @@ -90,16 +90,17 @@ static bool vfio_listener_skipped_section(MemoryRegionS= ection *section) section->offset_within_address_space & (1ULL << 63); } =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, - Error **errp) +/* + * Called with rcu_read_lock held. + * The returned MemoryRegion must not be accessed after calling rcu_read_u= nlock. + */ +static MemoryRegion *vfio_translate_iotlb(IOMMUTLBEntry *iotlb, hwaddr *xl= at_p, + Error **errp) { - bool ret, mr_has_discard_manager; + MemoryRegion *mr; =20 - ret =3D memory_get_xlat_addr(iotlb, vaddr, ram_addr, read_only, - &mr_has_discard_manager, errp); - if (ret && mr_has_discard_manager) { + mr =3D memory_translate_iotlb(iotlb, xlat_p, errp); + if (!mr && memory_region_has_ram_discard_manager(mr)) { /* * Malicious VMs might trigger discarding of IOMMU-mapped memory. = The * pages will remain pinned inside vfio until unmapped, resulting = in a @@ -118,7 +119,7 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, vo= id **vaddr, " intended via an IOMMU. It's possible to mitigat= e " " by setting/adjusting RLIMIT_MEMLOCK."); } - return ret; + return mr; } =20 static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) @@ -126,6 +127,8 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOM= MUTLBEntry *iotlb) VFIOGuestIOMMU *giommu =3D container_of(n, VFIOGuestIOMMU, n); VFIOContainerBase *bcontainer =3D giommu->bcontainer; hwaddr iova =3D iotlb->iova + giommu->iommu_offset; + MemoryRegion *mr; + hwaddr xlat; void *vaddr; int ret; Error *local_err =3D NULL; @@ -150,10 +153,14 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, I= OMMUTLBEntry *iotlb) if ((iotlb->perm & IOMMU_RW) !=3D IOMMU_NONE) { bool read_only; =20 - if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, &local_er= r)) { + mr =3D vfio_translate_iotlb(iotlb, &xlat, &local_err); + if (!mr) { error_report_err(local_err); goto out; } + vaddr =3D memory_region_get_ram_ptr(mr) + xlat; + read_only =3D !(iotlb->perm & IOMMU_WO) || mr->readonly; + /* * vaddr is only valid until rcu_read_unlock(). But after * vfio_dma_map has set up the mapping the pages will be @@ -1010,6 +1017,8 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier= *n, IOMMUTLBEntry *iotlb) ram_addr_t translated_addr; Error *local_err =3D NULL; int ret =3D -EINVAL; + MemoryRegion *mr; + ram_addr_t xlat; =20 trace_vfio_iommu_map_dirty_notify(iova, iova + iotlb->addr_mask); =20 @@ -1021,9 +1030,11 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifie= r *n, IOMMUTLBEntry *iotlb) } =20 rcu_read_lock(); - if (!vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL, &local_er= r)) { + mr =3D vfio_translate_iotlb(iotlb, &xlat, &local_err); + if (!mr) { goto out_unlock; } + translated_addr =3D memory_region_get_ram_addr(mr) + xlat; =20 ret =3D vfio_container_query_dirty_bitmap(bcontainer, iova, iotlb->add= r_mask + 1, translated_addr, &local_err); diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 1ab2c11..a1dd9e1 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -209,6 +209,8 @@ static void vhost_vdpa_iommu_map_notify(IOMMUNotifier *= n, IOMMUTLBEntry *iotlb) int ret; Int128 llend; Error *local_err =3D NULL; + MemoryRegion *mr; + hwaddr xlat; =20 if (iotlb->target_as !=3D &address_space_memory) { error_report("Wrong target AS \"%s\", only system memory is allowe= d", @@ -228,11 +230,14 @@ 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, - &local_err)) { + mr =3D memory_translate_iotlb(iotlb, &xlat, &local_err); + if (!mr) { error_report_err(local_err); return; } + vaddr =3D memory_region_get_ram_ptr(mr) + xlat; + read_only =3D !(iotlb->perm & IOMMU_WO) || mr->readonly; + ret =3D vhost_vdpa_dma_map(s, VHOST_VDPA_GUEST_PA_ASID, iova, iotlb->addr_mask + 1, vaddr, read_only); if (ret) { diff --git a/include/system/memory.h b/include/system/memory.h index fbbf4cf..13416d7 100644 --- a/include/system/memory.h +++ b/include/system/memory.h @@ -738,21 +738,20 @@ void ram_discard_manager_unregister_listener(RamDisca= rdManager *rdm, RamDiscardListener *rdl); =20 /** - * memory_get_xlat_addr: Extract addresses from a TLB entry + * memory_translate_iotlb: Extract addresses from a TLB entry. + * Called with rcu_read_lock held. * * @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 + * @xlat_p: return the offset of the entry from the start of the returned + * MemoryRegion. * @errp: pointer to Error*, to store an error if it happens. * - * Return: true on success, else false setting @errp with error. + * Return: On success, return the MemoryRegion containing the @iotlb trans= lated + * addr. The MemoryRegion must not be accessed after rcu_read_unl= ock. + * On failure, return NULL, 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, Error **errp); +MemoryRegion *memory_translate_iotlb(IOMMUTLBEntry *iotlb, hwaddr *xlat_p, + Error **errp); =20 typedef struct CoalescedMemoryRange CoalescedMemoryRange; typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; diff --git a/system/memory.c b/system/memory.c index 63b983e..306e9ff 100644 --- a/system/memory.c +++ b/system/memory.c @@ -2174,18 +2174,14 @@ void ram_discard_manager_unregister_listener(RamDis= cardManager *rdm, } =20 /* 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, Error **errp) +MemoryRegion *memory_translate_iotlb(IOMMUTLBEntry *iotlb, hwaddr *xlat_p, + Error **errp) { MemoryRegion *mr; hwaddr xlat; hwaddr len =3D iotlb->addr_mask + 1; bool writable =3D iotlb->perm & IOMMU_WO; =20 - if (mr_has_discard_manager) { - *mr_has_discard_manager =3D false; - } /* * The IOMMU TLB entry we have just covers translation through * this IOMMU to its immediate target. We need to translate @@ -2195,7 +2191,7 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void = **vaddr, &xlat, &len, writable, MEMTXATTRS_UNSPECI= FIED); if (!memory_region_is_ram(mr)) { error_setg(errp, "iommu map to non memory area %" HWADDR_PRIx "", = xlat); - return false; + return NULL; } else if (memory_region_has_ram_discard_manager(mr)) { RamDiscardManager *rdm =3D memory_region_get_ram_discard_manager(m= r); MemoryRegionSection tmp =3D { @@ -2203,9 +2199,6 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void = **vaddr, .offset_within_region =3D xlat, .size =3D int128_make64(len), }; - if (mr_has_discard_manager) { - *mr_has_discard_manager =3D true; - } /* * Malicious VMs can map memory into the IOMMU, which is expected * to remain discarded. vfio will pin all pages, populating memory. @@ -2216,7 +2209,7 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void = **vaddr, error_setg(errp, "iommu map to discarded memory (e.g., unplugg= ed" " via virtio-mem): %" HWADDR_PRIx "", iotlb->translated_addr); - return false; + return NULL; } } =20 @@ -2226,22 +2219,11 @@ bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, voi= d **vaddr, */ if (len & iotlb->addr_mask) { error_setg(errp, "iommu has granularity incompatible with target A= S"); - return false; + return NULL; } =20 - if (vaddr) { - *vaddr =3D memory_region_get_ram_ptr(mr) + xlat; - } - - if (ram_addr) { - *ram_addr =3D memory_region_get_ram_addr(mr) + xlat; - } - - if (read_only) { - *read_only =3D !writable || mr->readonly; - } - - return true; + *xlat_p =3D xlat; + return mr; } =20 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client) --=20 1.8.3.1