From nobody Sat Nov 23 22:17:48 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=reject dis=none) header.from=oracle.com ARC-Seal: i=1; a=rsa-sha256; t=1728045941; cv=none; d=zohomail.com; s=zohoarc; b=IMzFZfsm6fII8KD6zp4UvobUBTGuLECH6w4U8lNJ730cX+aukxvCRQeTkDV/MZ7u92pDJse3i2mAtIgey8OixFLC8yEXsciPI42Q6wWMQc5WyCFtR35xvVWKvhZjA8bpRsU2V31U1VRZfmXTuNtdZUUrSJDX5gBnR34Lhon747g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1728045941; h=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=xtR5OwVYJBoZ58v61LyRSYl31KA/FDiR+Fq3tzLMaJQ=; b=CrcDJsOxkiesQ2ysmv3lU2PVm+OabGbU2/uKtxE/edoxzZtuOAGJC7vvycX1MGoPgaYx533kz55v5WXk2D3uDaKJS0+8LC1lJvcCPHJwnCYR7YmIjE/CsiHtnsmMQv9c/S5i13t1QL/09CIv9KggazOsraUQBo93rZr4/j+/Q4g= 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 1728045941223391.73836435635974; Fri, 4 Oct 2024 05:45:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1swhg2-0004Wd-0e; Fri, 04 Oct 2024 08:44:58 -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 1swhg0-0004Vl-3T for qemu-devel@nongnu.org; Fri, 04 Oct 2024 08:44:56 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1swhfy-0005EU-9k for qemu-devel@nongnu.org; Fri, 04 Oct 2024 08:44:55 -0400 Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 494CMZbO000873; Fri, 4 Oct 2024 12:44:51 GMT Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42204b1hnk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 04 Oct 2024 12:44:51 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 494BDCwF005916; Fri, 4 Oct 2024 12:44:50 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 422056fcfh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 04 Oct 2024 12:44:50 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 494Cimo4020535; Fri, 4 Oct 2024 12:44:49 GMT Received: from jonah-ol8.us.oracle.com (dhcp-10-65-167-128.vpn.oracle.com [10.65.167.128]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 422056fce8-2; Fri, 04 Oct 2024 12:44:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=x tR5OwVYJBoZ58v61LyRSYl31KA/FDiR+Fq3tzLMaJQ=; b=PMFf/lE0kQXkO8IxU LE2qjUT87PgfV50EUBug7cRpE/19FSRVqaGWjQtNzOhKGv1Ry9B2CshOjV1nPysn vBa4g38a0uB80y81WN4DTcFlxyR7G1nYEtF+LaSZKsQLQ8g0NpAn4fZfLBw30xum owm8sdUgwi8mfpSZuUK8Qa56cBK9vA3GX2R+WaEKke41aGxH0rRkRWb2w5VfitOW GODghon/qaNA7mYilM/xE9W7us90g8u3tjQZtj5B0bHrBI9Hwb18n1GUgiD9yF87 8c/qpdYSBH7ocfb98q03E9IQdMVmQea7wAGRBOAHV5SWGhGxRHIx/ZHPmKp6GwFu X3HUA== From: Jonah Palmer To: qemu-devel@nongnu.org Cc: eperezma@redhat.com, mst@redhat.com, leiyang@redhat.com, peterx@redhat.com, dtatulea@nvidia.com, jasowang@redhat.com, si-wei.liu@oracle.com, boris.ostrovsky@oracle.com, jonah.palmer@oracle.com Subject: [RFC v2 1/2] vhost-vdpa: Implement IOVA->GPA tree Date: Fri, 4 Oct 2024 08:44:41 -0400 Message-ID: <20241004124445.3802090-2-jonah.palmer@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241004124445.3802090-1-jonah.palmer@oracle.com> References: <20241004124445.3802090-1-jonah.palmer@oracle.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-04_10,2024-10-03_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 phishscore=0 suspectscore=0 adultscore=0 bulkscore=0 mlxscore=0 mlxlogscore=999 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410040093 X-Proofpoint-ORIG-GUID: L0fH6H537sOoxBFpvsDQyAMpYkqJuyaZ X-Proofpoint-GUID: L0fH6H537sOoxBFpvsDQyAMpYkqJuyaZ 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.177.32; envelope-from=jonah.palmer@oracle.com; helo=mx0b-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, 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_H4=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: 1728045941926116600 Content-Type: text/plain; charset="utf-8" Implements the IOVA->GPA tree for handling mapping, unmapping, and translations for guest memory regions. When the guest has overlapping memory regions, an HVA to IOVA translation may return an incorrect IOVA when searching the IOVA->HVA tree. This is due to one HVA range being contained (overlapping) in another HVA range in the IOVA->HVA tree. By creating an IOVA->GPA tree, we can use GPAs to translate and find the correct IOVA for guest memory regions. Signed-off-by: Jonah Palmer --- hw/virtio/vhost-iova-tree.c | 78 +++++++++++++++++++++++++++++++++++-- hw/virtio/vhost-iova-tree.h | 5 +++ hw/virtio/vhost-vdpa.c | 20 ++++++---- 3 files changed, 92 insertions(+), 11 deletions(-) diff --git a/hw/virtio/vhost-iova-tree.c b/hw/virtio/vhost-iova-tree.c index 3d03395a77..e33fd56225 100644 --- a/hw/virtio/vhost-iova-tree.c +++ b/hw/virtio/vhost-iova-tree.c @@ -28,12 +28,15 @@ struct VhostIOVATree { =20 /* IOVA address to qemu memory maps. */ IOVATree *iova_taddr_map; + + /* IOVA address to guest memory maps. */ + IOVATree *iova_gpa_map; }; =20 /** - * Create a new IOVA tree + * Create a new VhostIOVATree * - * Returns the new IOVA tree + * Returns the new VhostIOVATree */ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last) { @@ -44,6 +47,7 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwa= ddr iova_last) tree->iova_last =3D iova_last; =20 tree->iova_taddr_map =3D iova_tree_new(); + tree->iova_gpa_map =3D iova_tree_new(); return tree; } =20 @@ -53,6 +57,7 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwa= ddr iova_last) void vhost_iova_tree_delete(VhostIOVATree *iova_tree) { iova_tree_destroy(iova_tree->iova_taddr_map); + iova_tree_destroy(iova_tree->iova_gpa_map); g_free(iova_tree); } =20 @@ -71,7 +76,7 @@ const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATr= ee *tree, } =20 /** - * Allocate a new mapping + * Allocate a new mapping in the IOVA->HVA tree * * @tree: The iova tree * @map: The iova map @@ -108,3 +113,70 @@ void vhost_iova_tree_remove(VhostIOVATree *iova_tree, = DMAMap map) { iova_tree_remove(iova_tree->iova_taddr_map, map); } + +/** + * Find the IOVA address stored from a guest memory address + * + * @tree: The VhostIOVATree + * @map: The map with the guest memory address + * + * Return the stored mapping, or NULL if not found. + */ +const DMAMap *vhost_iova_gpa_tree_find_iova(const VhostIOVATree *tree, + const DMAMap *map) +{ + return iova_tree_find_iova(tree->iova_gpa_map, map); +} + +/** + * Allocate new mappings in the IOVA->HVA & IOVA->GPA trees + * + * @tree: The VhostIOVATree + * @map: The iova map + * @gpa: The guest physical address (GPA) + * + * Returns: + * - IOVA_OK if the map fits both containers + * - IOVA_ERR_INVALID if the map does not make sense (like size overflow) + * - IOVA_ERR_NOMEM if the IOVA->HVA tree cannot allocate more space + * + * It returns an assigned iova in map->iova if return value is IOVA_OK. + */ +int vhost_iova_tree_map_alloc_gpa(VhostIOVATree *tree, DMAMap *map, hwaddr= gpa) +{ + int ret; + + /* Some vhost devices don't like addr 0. Skip first page */ + hwaddr iova_first =3D tree->iova_first ?: qemu_real_host_page_size(); + + if (map->translated_addr + map->size < map->translated_addr || + map->perm =3D=3D IOMMU_NONE) { + return IOVA_ERR_INVALID; + } + + /* Allocate a node in the IOVA->HVA tree */ + ret =3D iova_tree_alloc_map(tree->iova_taddr_map, map, iova_first, + tree->iova_last); + if (unlikely(ret !=3D IOVA_OK)) { + return ret; + } + + /* Insert a node in the IOVA->GPA tree */ + map->translated_addr =3D gpa; + return iova_tree_insert(tree->iova_gpa_map, map); +} + +/** + * Remove existing mappings from the IOVA->HVA & IOVA->GPA trees + * + * @iova_tree: The VhostIOVATree + * @map: The map to remove + */ +void vhost_iova_tree_remove_gpa(VhostIOVATree *iova_tree, DMAMap map) +{ + /* Remove the existing mapping from the IOVA->GPA tree */ + iova_tree_remove(iova_tree->iova_gpa_map, map); + + /* Remove the corresponding mapping from the IOVA->HVA tree */ + iova_tree_remove(iova_tree->iova_taddr_map, map); +} diff --git a/hw/virtio/vhost-iova-tree.h b/hw/virtio/vhost-iova-tree.h index 4adfd79ff0..511c6d18ae 100644 --- a/hw/virtio/vhost-iova-tree.h +++ b/hw/virtio/vhost-iova-tree.h @@ -23,5 +23,10 @@ const DMAMap *vhost_iova_tree_find_iova(const VhostIOVAT= ree *iova_tree, const DMAMap *map); int vhost_iova_tree_map_alloc(VhostIOVATree *iova_tree, DMAMap *map); void vhost_iova_tree_remove(VhostIOVATree *iova_tree, DMAMap map); +const DMAMap *vhost_iova_gpa_tree_find_iova(const VhostIOVATree *iova_tree, + const DMAMap *map); +int vhost_iova_tree_map_alloc_gpa(VhostIOVATree *iova_tree, DMAMap *map, + hwaddr gpa); +void vhost_iova_tree_remove_gpa(VhostIOVATree *iova_tree, DMAMap map); =20 #endif diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 3cdaa12ed5..591ff426e7 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -365,9 +365,16 @@ static void vhost_vdpa_listener_region_add(MemoryListe= ner *listener, mem_region.size =3D int128_get64(llsize) - 1, mem_region.perm =3D IOMMU_ACCESS_FLAG(true, section->readonly), =20 - r =3D vhost_iova_tree_map_alloc(s->iova_tree, &mem_region); + r =3D vhost_iova_tree_map_alloc_gpa(s->iova_tree, &mem_region, + section->offset_within_address_s= pace); if (unlikely(r !=3D IOVA_OK)) { error_report("Can't allocate a mapping (%d)", r); + + /* Insertion to IOVA->GPA tree failed */ + if (mem_region.translated_addr =3D=3D + section->offset_within_address_space) { + goto fail_map; + } goto fail; } =20 @@ -386,7 +393,7 @@ static void vhost_vdpa_listener_region_add(MemoryListen= er *listener, =20 fail_map: if (s->shadow_data) { - vhost_iova_tree_remove(s->iova_tree, mem_region); + vhost_iova_tree_remove_gpa(s->iova_tree, mem_region); } =20 fail: @@ -440,21 +447,18 @@ static void vhost_vdpa_listener_region_del(MemoryList= ener *listener, =20 if (s->shadow_data) { const DMAMap *result; - const void *vaddr =3D memory_region_get_ram_ptr(section->mr) + - section->offset_within_region + - (iova - section->offset_within_address_space); DMAMap mem_region =3D { - .translated_addr =3D (hwaddr)(uintptr_t)vaddr, + .translated_addr =3D section->offset_within_address_space, .size =3D int128_get64(llsize) - 1, }; =20 - result =3D vhost_iova_tree_find_iova(s->iova_tree, &mem_region); + result =3D vhost_iova_gpa_tree_find_iova(s->iova_tree, &mem_region= ); if (!result) { /* The memory listener map wasn't mapped */ return; } iova =3D result->iova; - vhost_iova_tree_remove(s->iova_tree, *result); + vhost_iova_tree_remove_gpa(s->iova_tree, *result); } vhost_vdpa_iotlb_batch_begin_once(s); /* --=20 2.43.5 From nobody Sat Nov 23 22:17:48 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=reject dis=none) header.from=oracle.com ARC-Seal: i=1; a=rsa-sha256; t=1728045915; cv=none; d=zohomail.com; s=zohoarc; b=VCyRmk/UmFbTPSVcB4iVbe52zJ1hWUMj29CNvPGOhn6ulNOxDqrRwCsmm+JMyzCcx/9tr1ZU3Pq/5KvUhEzfRhK5IPtLZzrkwoGHw8PAiYvrbizgqnT105OhaMm5YZTWCPC2QXgT807e4WmeVdoFyaiiCOONWO3HYpkxvSRdoy0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1728045915; h=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=QAJuSOyaE4Dz4mMOJfpiJfDTVKFK/928JyeyEQvN5c8=; b=RhEGx4B8dqb3E4iLSC0u1mDW4vmKXnHC3rdH8Furb8uYP1EYmPVAXd7793xseIugXfH/6vj6+bQpvlTY5h48l6dD6EyQc4idzHMMZR8tFPV7CIesppP82PPxORbxDHdAhBzHZIK3Zdr9V2beDi3cBfCHfv+SpU6nliWBoEQG2GE= 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 17280459151061.6713727286852418; Fri, 4 Oct 2024 05:45:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1swhg3-0004Wu-IG; Fri, 04 Oct 2024 08:44:59 -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 1swhg1-0004WC-4L for qemu-devel@nongnu.org; Fri, 04 Oct 2024 08:44:57 -0400 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1swhfy-0005EY-FY for qemu-devel@nongnu.org; Fri, 04 Oct 2024 08:44:56 -0400 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 494CMYR0026749; Fri, 4 Oct 2024 12:44:52 GMT Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 4220489htt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 04 Oct 2024 12:44:52 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 494Bct3m005903; Fri, 4 Oct 2024 12:44:51 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 422056fcfx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 04 Oct 2024 12:44:51 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 494Cimo6020535; Fri, 4 Oct 2024 12:44:50 GMT Received: from jonah-ol8.us.oracle.com (dhcp-10-65-167-128.vpn.oracle.com [10.65.167.128]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 422056fce8-3; Fri, 04 Oct 2024 12:44:50 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=Q AJuSOyaE4Dz4mMOJfpiJfDTVKFK/928JyeyEQvN5c8=; b=SRmyqSuOeNEBtyVlZ Nv7TLzBq3Q4wSquZ3GY/36yAt/6zROr0M0B0jtzET/zReZ35hzbxjaNtqJ1fm+FO vPLSj2OadC3PlBACEHi7fosle7HgRIGx4QZFImNC5ZGW9Z3qE44JernBE1X5UlJt IGYz/XXw+BDL26AoAQErM5PsRVhMyrXUsnAwxkUhZKcW9nWuJkvm7cnXYaeTbwkk GXIMONomCs4D41EA8iofuWmaeDgW8jUCVPzSRTQS8lAyVYM6FhdqWxir2teVukR9 1UBG7iEXZKPP8QSYuHggjQNU8HTtcrIJViWjYDck04Y0v/aS9biTITdcDeK5AT7K vJ76w== From: Jonah Palmer To: qemu-devel@nongnu.org Cc: eperezma@redhat.com, mst@redhat.com, leiyang@redhat.com, peterx@redhat.com, dtatulea@nvidia.com, jasowang@redhat.com, si-wei.liu@oracle.com, boris.ostrovsky@oracle.com, jonah.palmer@oracle.com Subject: [RFC v2 2/2] vhost-svq: Translate guest-backed memory with IOVA->GPA tree Date: Fri, 4 Oct 2024 08:44:42 -0400 Message-ID: <20241004124445.3802090-3-jonah.palmer@oracle.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241004124445.3802090-1-jonah.palmer@oracle.com> References: <20241004124445.3802090-1-jonah.palmer@oracle.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-04_10,2024-10-03_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 phishscore=0 suspectscore=0 adultscore=0 bulkscore=0 mlxscore=0 mlxlogscore=999 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410040093 X-Proofpoint-ORIG-GUID: k9ss4RUbAR95It_w3A179Ws9CrbK7vjV X-Proofpoint-GUID: k9ss4RUbAR95It_w3A179Ws9CrbK7vjV 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.177.32; envelope-from=jonah.palmer@oracle.com; helo=mx0b-00069f02.pphosted.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, 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_H4=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: 1728045916130116600 Content-Type: text/plain; charset="utf-8" Implements searching the IOVA->GPA tree when translating guest-backed memory (and searching the IOVA->HVA tree when translating host-only memory). By using the IOVA->GPA tree to find IOVA translations, we avoid the issue where, if the guest has overlapping memory regions, HVAs backed by guest memory can lead to multiple different GPAs. In other words, we may translate to an incorrect IOVA if we search the IOVA->HVA tree using an HVA that's backed by guest memory. Signed-off-by: Jonah Palmer --- hw/virtio/vhost-shadow-virtqueue.c | 61 ++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-vi= rtqueue.c index fc5f408f77..a72093c00b 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -16,6 +16,7 @@ #include "qemu/log.h" #include "qemu/memalign.h" #include "linux-headers/linux/vhost.h" +#include "exec/ramblock.h" =20 /** * Validate the transport device features that both guests can use with th= e SVQ @@ -78,24 +79,55 @@ uint16_t vhost_svq_available_slots(const VhostShadowVir= tqueue *svq) * @vaddr: Translated IOVA addresses * @iovec: Source qemu's VA addresses * @num: Length of iovec and minimum length of vaddr + * @is_guest_memory: True if iovec is backed by guest memory */ static bool vhost_svq_translate_addr(const VhostShadowVirtqueue *svq, hwaddr *addrs, const struct iovec *io= vec, - size_t num) + size_t num, bool is_guest_memory) { if (num =3D=3D 0) { return true; } =20 for (size_t i =3D 0; i < num; ++i) { - DMAMap needle =3D { - .translated_addr =3D (hwaddr)(uintptr_t)iovec[i].iov_base, - .size =3D iovec[i].iov_len, - }; Int128 needle_last, map_last; size_t off; + const DMAMap *map; + DMAMap needle; + + /* + * If the HVA is backed by guest memory, find its GPA and search t= he + * IOVA->GPA tree for the translated IOVA + */ + if (is_guest_memory) { + RAMBlock *rb; + hwaddr gpa; + ram_addr_t offset; + + rb =3D qemu_ram_block_from_host(iovec[i].iov_base, false, &off= set); + if (unlikely(!rb)) { + qemu_log_mask(LOG_GUEST_ERROR, + "No expected RAMBlock found at HVA 0x%"HWADD= R_PRIx"", + (hwaddr)(uintptr_t)iovec[i].iov_base); + return false; + } + gpa =3D rb->offset + offset; + + /* Search IOVA->GPA tree */ + needle =3D (DMAMap) { + .translated_addr =3D gpa, + .size =3D iovec[i].iov_len, + }; + map =3D vhost_iova_gpa_tree_find_iova(svq->iova_tree, &needle); + } else { + /* Search IOVA->HVA tree */ + needle =3D (DMAMap) { + .translated_addr =3D (hwaddr)(uintptr_t)iovec[i].iov_base, + .size =3D iovec[i].iov_len, + }; + map =3D vhost_iova_tree_find_iova(svq->iova_tree, &needle); + } =20 - const DMAMap *map =3D vhost_iova_tree_find_iova(svq->iova_tree, &n= eedle); /* * Map cannot be NULL since iova map contains all guest space and * qemu already has a physical address mapped @@ -132,12 +164,14 @@ static bool vhost_svq_translate_addr(const VhostShado= wVirtqueue *svq, * @num: iovec length * @more_descs: True if more descriptors come in the chain * @write: True if they are writeable descriptors + * @is_guest_memory: True if iovec is backed by guest memory * * Return true if success, false otherwise and print error. */ static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr = *sg, const struct iovec *iovec, size_t = num, - bool more_descs, bool write) + bool more_descs, bool write, + bool is_guest_memory) { uint16_t i =3D svq->free_head, last =3D svq->free_head; unsigned n; @@ -149,7 +183,7 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirt= queue *svq, hwaddr *sg, return true; } =20 - ok =3D vhost_svq_translate_addr(svq, sg, iovec, num); + ok =3D vhost_svq_translate_addr(svq, sg, iovec, num, is_guest_memory); if (unlikely(!ok)) { return false; } @@ -175,7 +209,7 @@ static bool vhost_svq_vring_write_descs(VhostShadowVirt= queue *svq, hwaddr *sg, static bool vhost_svq_add_split(VhostShadowVirtqueue *svq, const struct iovec *out_sg, size_t out_num, const struct iovec *in_sg, size_t in_num, - unsigned *head) + unsigned *head, bool is_guest_memory) { unsigned avail_idx; vring_avail_t *avail =3D svq->vring.avail; @@ -192,12 +226,13 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue = *svq, } =20 ok =3D vhost_svq_vring_write_descs(svq, sgs, out_sg, out_num, in_num >= 0, - false); + false, is_guest_memory); if (unlikely(!ok)) { return false; } =20 - ok =3D vhost_svq_vring_write_descs(svq, sgs, in_sg, in_num, false, tru= e); + ok =3D vhost_svq_vring_write_descs(svq, sgs, in_sg, in_num, false, tru= e, + is_guest_memory); if (unlikely(!ok)) { return false; } @@ -253,12 +288,14 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const st= ruct iovec *out_sg, unsigned qemu_head; unsigned ndescs =3D in_num + out_num; bool ok; + bool is_guest_memory =3D (elem !=3D NULL) ? true : false; =20 if (unlikely(ndescs > vhost_svq_available_slots(svq))) { return -ENOSPC; } =20 - ok =3D vhost_svq_add_split(svq, out_sg, out_num, in_sg, in_num, &qemu_= head); + ok =3D vhost_svq_add_split(svq, out_sg, out_num, in_sg, in_num, &qemu_= head, + is_guest_memory); if (unlikely(!ok)) { return -EINVAL; } --=20 2.43.5