From nobody Tue Nov 26 11:22:52 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=1707923975; cv=none; d=zohomail.com; s=zohoarc; b=YPlcEOzgj9AWrhqQy3KzldPdAARantw6cAP8Re6t4TWQnhaNa1ZLP4UFETaeK5SY2bRaab+B+9JT6TVc2F75LLXM56HllMe2HquURco0H4XBUEilWO/5pBV85ovFsdyox8j+GecYoE17+Qmbasn5DakkoGDz0vIAORIa1kOKzTY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923975; 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=h3vrvrrsqxQRRX1DdJMjzsxwaD3Jnd+isNz0iU98YIk=; b=Moh3FAz4XncMvjKEpPdEIEJdWg7AS00+doYfDlrhXIzKZ6K4MeqhWEs6DCiXEECVGqiicYnJzjn9RFgDybc0Yu8H40eD3oY2p0KMBWIjhsCLJCNXiVa4Lbgy3R7KS6Lus8rf2b4LmbMEMaQtC4BFhHWKSwhmJZKaYZVLXZAMv5k= 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 1707923975266215.31813952457367; Wed, 14 Feb 2024 07:19:35 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH0h-0007gn-I6; Wed, 14 Feb 2024 10:17:19 -0500 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 1raH0d-0007gb-SO for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:15 -0500 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 1raH0c-0000Kn-C4 for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:15 -0500 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-317-GgdMN97APlyHHNzWgKodqA-1; Wed, 14 Feb 2024 10:17:09 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 E67691C05AE3; Wed, 14 Feb 2024 15:17:08 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id D04DA1C02ECC; Wed, 14 Feb 2024 15:17:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923832; 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=h3vrvrrsqxQRRX1DdJMjzsxwaD3Jnd+isNz0iU98YIk=; b=izEa6RMoY7yr6qr99wqGDgzXxsJVi4Zj2lZgr8BwZ2FhYuyEbQ4aXg/9BbqUn+mKl45Pah XbmkfShuuzA4KyIWpNFtt4Zq8pY13tYV3sKb1yPmCKKPWGnFpNrSnbzA5lIX3lE+d40mA2 JKTYt10WqzVzofg6Su/EqPRgJhayiNo= X-MC-Unique: GgdMN97APlyHHNzWgKodqA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 01/14] libvhost-user: Dynamically allocate memory for memory slots Date: Wed, 14 Feb 2024 16:16:48 +0100 Message-ID: <20240214151701.29906-2-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923977490100006 Content-Type: text/plain; charset="utf-8" Let's prepare for increasing VHOST_USER_MAX_RAM_SLOTS by dynamically allocating dev->regions. We don't have any ABI guarantees (not dynamically linked), so we can simply change the layout of VuDev. Let's zero out the memory, just as we used to do. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 11 +++++++++++ subprojects/libvhost-user/libvhost-user.h | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index a3b158c671..360c5366d6 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -2171,6 +2171,8 @@ vu_deinit(VuDev *dev) =20 free(dev->vq); dev->vq =3D NULL; + free(dev->regions); + dev->regions =3D NULL; } =20 bool @@ -2205,9 +2207,18 @@ vu_init(VuDev *dev, dev->backend_fd =3D -1; dev->max_queues =3D max_queues; =20 + dev->regions =3D malloc(VHOST_USER_MAX_RAM_SLOTS * sizeof(dev->regions= [0])); + if (!dev->regions) { + DPRINT("%s: failed to malloc mem regions\n", __func__); + return false; + } + memset(dev->regions, 0, VHOST_USER_MAX_RAM_SLOTS * sizeof(dev->regions= [0])); + dev->vq =3D malloc(max_queues * sizeof(dev->vq[0])); if (!dev->vq) { DPRINT("%s: failed to malloc virtqueues\n", __func__); + free(dev->regions); + dev->regions =3D NULL; return false; } =20 diff --git a/subprojects/libvhost-user/libvhost-user.h b/subprojects/libvho= st-user/libvhost-user.h index c2352904f0..c882b4e3a2 100644 --- a/subprojects/libvhost-user/libvhost-user.h +++ b/subprojects/libvhost-user/libvhost-user.h @@ -398,7 +398,7 @@ typedef struct VuDevInflightInfo { struct VuDev { int sock; uint32_t nregions; - VuDevRegion regions[VHOST_USER_MAX_RAM_SLOTS]; + VuDevRegion *regions; VuVirtq *vq; VuDevInflightInfo inflight_info; int log_call_fd; --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923885; cv=none; d=zohomail.com; s=zohoarc; b=KVD3COF+TZvLXfpJISxc/dV98cMh4qb2fFNtqmSiPUGF5haSps5KP9MZasJDmdgGPdFdc7wHyv+5E+LihB742IUpXo8YLGqUIdg8EVwCeLMkLGWRdv1yyZMKP1utRk061GsTQS0sdxbFUySF0BRVFXo43XS+bpwpy2VAPITCqqc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923885; 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=D5aOVJSEpKei/7ex6EeASiVcKRUD1LDRJrwBUNnZEXU=; b=ZLMRe9exDZEJeujBYU2DcOpInfNij7ZkUnoTGK4ephA13DbbXUoVbiWJP0zqXfLm6magPHZJpJf/8iqfhadp56/YQ1EfVKf2Y5kMaxmMlNNCkU/vvU7CGVAPv8V8PVoBRpe1RpzNrTpKPrBwkTYXtj1IJ/9Dk8a9HTJqHTs15us= 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 1707923885341505.49663851349374; Wed, 14 Feb 2024 07:18:05 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH0j-0007hR-RF; Wed, 14 Feb 2024 10:17:21 -0500 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 1raH0h-0007gt-9J for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:19 -0500 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 1raH0f-0000LD-9W for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:18 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-19-lvvM4uYSMBSsSnzfrGQzvg-1; Wed, 14 Feb 2024 10:17:12 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 3626483F265; Wed, 14 Feb 2024 15:17:12 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 525771C02ECA; Wed, 14 Feb 2024 15:17:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923836; 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=D5aOVJSEpKei/7ex6EeASiVcKRUD1LDRJrwBUNnZEXU=; b=hIgFVXJ5OecC0ZD3+1e6y9Q2ooUxNrE8zCUUp6XF0Lcs4W9B0gocYWQNSzOU8xsOpGW7d2 Ggn+DZCdKS6ec4N4VoFZr+kHsRDq+c5blKTVOnmtYULwNhS6FPzFjYoi77y58Y1Efdqrca YEQFHS8wfd4ETwltkCOXu6k1kFR7Pcg= X-MC-Unique: lvvM4uYSMBSsSnzfrGQzvg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 02/14] libvhost-user: Bump up VHOST_USER_MAX_RAM_SLOTS to 509 Date: Wed, 14 Feb 2024 16:16:49 +0100 Message-ID: <20240214151701.29906-3-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923887133100007 Content-Type: text/plain; charset="utf-8" Let's support up to 509 mem slots, just like vhost in the kernel usually does and the rust vhost-user implementation recently [1] started doing. This is required to properly support memory hotplug, either using multiple DIMMs (ACPI supports up to 256) or using virtio-mem. The 509 used to be the KVM limit, it supported 512, but 3 were used for internal purposes. Currently, KVM supports more than 512, but it usually doesn't make use of more than ~260 (i.e., 256 DIMMs + boot memory), except when other memory devices like PCI devices with BARs are used. So, 509 seems to work well for vhost in the kernel. Details can be found in the QEMU change that made virtio-mem consume up to 256 mem slots across all virtio-mem devices. [2] 509 mem slots implies 509 VMAs/mappings in the worst case (even though, in practice with virtio-mem we won't be seeing more than ~260 in most setups). With max_map_count under Linux defaulting to 64k, 509 mem slots still correspond to less than 1% of the maximum number of mappings. There are plenty left for the application to consume. [1] https://github.com/rust-vmm/vhost/pull/224 [2] https://lore.kernel.org/all/20230926185738.277351-1-david@redhat.com/ Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.h b/subprojects/libvho= st-user/libvhost-user.h index c882b4e3a2..deb40e77b3 100644 --- a/subprojects/libvhost-user/libvhost-user.h +++ b/subprojects/libvhost-user/libvhost-user.h @@ -31,10 +31,12 @@ #define VHOST_MEMORY_BASELINE_NREGIONS 8 =20 /* - * Set a reasonable maximum number of ram slots, which will be supported by - * any architecture. + * vhost in the kernel usually supports 509 mem slots. 509 used to be the + * KVM limit, it supported 512, but 3 were used for internal purposes. This + * limit is sufficient to support many DIMMs and virtio-mem in + * "dynamic-memslots" mode. */ -#define VHOST_USER_MAX_RAM_SLOTS 32 +#define VHOST_USER_MAX_RAM_SLOTS 509 =20 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64) =20 --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923885; cv=none; d=zohomail.com; s=zohoarc; b=XdLIom/iQXwmd2DmNoUOa2qic9f80gRlAO3ht7IbN9Z/et/T82zTUWybA9XueYiquSq812esEooUtXSWTldY2R9jjkxY5cU8QgIXb2Y2MUEWPqDc2VbtTcmV8z1n8Rn4mA86yll81cfBCJ6w/oa8sPWfAZ0Gxb1weyRkSNYvw2Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923885; 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=B+Z3ENfw4oRiH2TCXYEchq2/igD7y2lzOO/MtdRoAj4=; b=iGkggyCKVdlY8CmqBMShDURfoG0E+Re5Z7wKOee4aujJpTwFV2Kmwuc7YKQyU/X5YYq2nRsAtItmxqykeF4VZ2g5QnBStmwfkYgzxqjEFSpYr4/9jYBwTQFg5D9XwxvmJcfcNhr45dASp+2ZI+rYI4c+yAfQ1aeDAQ+R2oWwabs= 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 1707923885344887.5290336411323; Wed, 14 Feb 2024 07:18:05 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH0k-0007i1-By; Wed, 14 Feb 2024 10:17:22 -0500 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 1raH0i-0007hI-1K for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:20 -0500 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 1raH0g-0000LU-ES for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:19 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-637-R_6XcIiJPdKDBUxHjPvW9g-1; Wed, 14 Feb 2024 10:17:15 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 4661512501E1; Wed, 14 Feb 2024 15:17:15 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 715261C02ECC; Wed, 14 Feb 2024 15:17:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923837; 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=B+Z3ENfw4oRiH2TCXYEchq2/igD7y2lzOO/MtdRoAj4=; b=Zrz9u45t9KHkWkjNnmvr1NOZMn1l7RiwnGngPF7UXob4L8RJ3LxKRirSTDe/KodyAOinvd ytVIEwPZIK1ggVd83Qbp+AJPNPf+jTu95BQef0XOsWYvQjYpHk3nw5fvz91gLFksp+wjfh kBZDpqICCy/fUZH944UVYK4ko6y/doo= X-MC-Unique: R_6XcIiJPdKDBUxHjPvW9g-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 03/14] libvhost-user: Factor out removing all mem regions Date: Wed, 14 Feb 2024 16:16:50 +0100 Message-ID: <20240214151701.29906-4-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923887170100008 Content-Type: text/plain; charset="utf-8" Let's factor it out. Note that the check for MAP_FAILED was wrong as we never set mmap_addr if mmap() failed. We'll remove the NULL check separately. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 34 ++++++++++++----------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index 360c5366d6..e4907dfc26 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -240,6 +240,22 @@ qva_to_va(VuDev *dev, uint64_t qemu_addr) return NULL; } =20 +static void +vu_remove_all_mem_regs(VuDev *dev) +{ + unsigned int i; + + for (i =3D 0; i < dev->nregions; i++) { + VuDevRegion *r =3D &dev->regions[i]; + void *ma =3D (void *)(uintptr_t)r->mmap_addr; + + if (ma) { + munmap(ma, r->size + r->mmap_offset); + } + } + dev->nregions =3D 0; +} + static void vmsg_close_fds(VhostUserMsg *vmsg) { @@ -1003,14 +1019,7 @@ vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg) unsigned int i; VhostUserMemory m =3D vmsg->payload.memory, *memory =3D &m; =20 - for (i =3D 0; i < dev->nregions; i++) { - VuDevRegion *r =3D &dev->regions[i]; - void *ma =3D (void *) (uintptr_t) r->mmap_addr; - - if (ma) { - munmap(ma, r->size + r->mmap_offset); - } - } + vu_remove_all_mem_regs(dev); dev->nregions =3D memory->nregions; =20 if (dev->postcopy_listening) { @@ -2112,14 +2121,7 @@ vu_deinit(VuDev *dev) { unsigned int i; =20 - for (i =3D 0; i < dev->nregions; i++) { - VuDevRegion *r =3D &dev->regions[i]; - void *m =3D (void *) (uintptr_t) r->mmap_addr; - if (m !=3D MAP_FAILED) { - munmap(m, r->size + r->mmap_offset); - } - } - dev->nregions =3D 0; + vu_remove_all_mem_regs(dev); =20 for (i =3D 0; i < dev->max_queues; i++) { VuVirtq *vq =3D &dev->vq[i]; --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923972; cv=none; d=zohomail.com; s=zohoarc; b=Po8DgSe79kB2LsqTsXWTz2u7oTSZkbN1TSgHYfxJNXbLv7BEruMrGip8HfbQ+GylG8X8ouuQQSEQHBqzgmmtEXCIdT7Ts4OfDFkCzifhJP6+3JheImllufi4oPqrY52ED7PatalN1s31Qj+9WFO67N7nd+iilAZTMBzMCkCjrOE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923972; 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=nnZdB938cYCDit76BNbGQDIjKNGJFkUbtEJRpThuoMg=; b=YAVCWqKw6n/L42GDyKCen6atVBeSibr++YZdLD9eLQIgjYEbKidOiHab8+8mU2A/V2qWCFFXppjE2RuR8RJPfPYFn5GP/Bwik9DbNPUdBf+pjnJqYwOqiLo/y8HaIhhysdfrkIY+HtNCq4OlD2RrcYD5GJXdW7X67IZcfjaE0p8= 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 1707923972441126.04347632740178; Wed, 14 Feb 2024 07:19:32 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH0o-0007ma-7O; Wed, 14 Feb 2024 10:17:26 -0500 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 1raH0n-0007lG-1O for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:25 -0500 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 1raH0l-0000MD-AF for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:24 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-80-CXlwDM50Nne6VR-M7gsMIA-1; Wed, 14 Feb 2024 10:17:19 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 A506B83F266; Wed, 14 Feb 2024 15:17:18 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8DE9A1C066A9; Wed, 14 Feb 2024 15:17:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923842; 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=nnZdB938cYCDit76BNbGQDIjKNGJFkUbtEJRpThuoMg=; b=dXnbwiSxRdPUtfEAIehc3bF/MgZdtyBBl3fgooUaNRa5bdU2hCMMdrndxQArMu8vT8DxSb cKp/bxdrOnpHxhHqw8bSD35512liPxRleXNfx6EGlP6DAdxQYk82b9BdxMH7l40VFKiWee VzS5Vili+y2rv9BrFLTdObD8oqtOJ2c= X-MC-Unique: CXlwDM50Nne6VR-M7gsMIA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 04/14] libvhost-user: Merge vu_set_mem_table_exec_postcopy() into vu_set_mem_table_exec() Date: Wed, 14 Feb 2024 16:16:51 +0100 Message-ID: <20240214151701.29906-5-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923973491100007 Content-Type: text/plain; charset="utf-8" Let's reduce some code duplication and prepare for further changes. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 119 +++++++--------------- 1 file changed, 39 insertions(+), 80 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index e4907dfc26..a7bd7de3cd 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -937,95 +937,23 @@ vu_get_shared_object(VuDev *dev, VhostUserMsg *vmsg) } =20 static bool -vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg) +vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg) { - unsigned int i; VhostUserMemory m =3D vmsg->payload.memory, *memory =3D &m; - dev->nregions =3D memory->nregions; - - DPRINT("Nregions: %u\n", memory->nregions); - for (i =3D 0; i < dev->nregions; i++) { - void *mmap_addr; - VhostUserMemoryRegion *msg_region =3D &memory->regions[i]; - VuDevRegion *dev_region =3D &dev->regions[i]; - - DPRINT("Region %d\n", i); - DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n", - msg_region->guest_phys_addr); - DPRINT(" memory_size: 0x%016"PRIx64"\n", - msg_region->memory_size); - DPRINT(" userspace_addr 0x%016"PRIx64"\n", - msg_region->userspace_addr); - DPRINT(" mmap_offset 0x%016"PRIx64"\n", - msg_region->mmap_offset); - - dev_region->gpa =3D msg_region->guest_phys_addr; - dev_region->size =3D msg_region->memory_size; - dev_region->qva =3D msg_region->userspace_addr; - dev_region->mmap_offset =3D msg_region->mmap_offset; + int prot =3D PROT_READ | PROT_WRITE; + unsigned int i; =20 - /* We don't use offset argument of mmap() since the - * mapped address has to be page aligned, and we use huge - * pages. + if (dev->postcopy_listening) { + /* * In postcopy we're using PROT_NONE here to catch anyone * accessing it before we userfault */ - mmap_addr =3D mmap(0, dev_region->size + dev_region->mmap_offset, - PROT_NONE, MAP_SHARED | MAP_NORESERVE, - vmsg->fds[i], 0); - - if (mmap_addr =3D=3D MAP_FAILED) { - vu_panic(dev, "region mmap error: %s", strerror(errno)); - } else { - dev_region->mmap_addr =3D (uint64_t)(uintptr_t)mmap_addr; - DPRINT(" mmap_addr: 0x%016"PRIx64"\n", - dev_region->mmap_addr); - } - - /* Return the address to QEMU so that it can translate the ufd - * fault addresses back. - */ - msg_region->userspace_addr =3D (uintptr_t)(mmap_addr + - dev_region->mmap_offset); - close(vmsg->fds[i]); - } - - /* Send the message back to qemu with the addresses filled in */ - vmsg->fd_num =3D 0; - if (!vu_send_reply(dev, dev->sock, vmsg)) { - vu_panic(dev, "failed to respond to set-mem-table for postcopy"); - return false; - } - - /* Wait for QEMU to confirm that it's registered the handler for the - * faults. - */ - if (!dev->read_msg(dev, dev->sock, vmsg) || - vmsg->size !=3D sizeof(vmsg->payload.u64) || - vmsg->payload.u64 !=3D 0) { - vu_panic(dev, "failed to receive valid ack for postcopy set-mem-ta= ble"); - return false; + prot =3D PROT_NONE; } =20 - /* OK, now we can go and register the memory and generate faults */ - (void)generate_faults(dev); - - return false; -} - -static bool -vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg) -{ - unsigned int i; - VhostUserMemory m =3D vmsg->payload.memory, *memory =3D &m; - vu_remove_all_mem_regs(dev); dev->nregions =3D memory->nregions; =20 - if (dev->postcopy_listening) { - return vu_set_mem_table_exec_postcopy(dev, vmsg); - } - DPRINT("Nregions: %u\n", memory->nregions); for (i =3D 0; i < dev->nregions; i++) { void *mmap_addr; @@ -1051,8 +979,7 @@ vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg) * mapped address has to be page aligned, and we use huge * pages. */ mmap_addr =3D mmap(0, dev_region->size + dev_region->mmap_offset, - PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERV= E, - vmsg->fds[i], 0); + prot, MAP_SHARED | MAP_NORESERVE, vmsg->fds[i], 0= ); =20 if (mmap_addr =3D=3D MAP_FAILED) { vu_panic(dev, "region mmap error: %s", strerror(errno)); @@ -1062,9 +989,41 @@ vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg) dev_region->mmap_addr); } =20 + if (dev->postcopy_listening) { + /* + * Return the address to QEMU so that it can translate the ufd + * fault addresses back. + */ + msg_region->userspace_addr =3D (uintptr_t)(mmap_addr + + dev_region->mmap_offs= et); + } close(vmsg->fds[i]); } =20 + if (dev->postcopy_listening) { + /* Send the message back to qemu with the addresses filled in */ + vmsg->fd_num =3D 0; + if (!vu_send_reply(dev, dev->sock, vmsg)) { + vu_panic(dev, "failed to respond to set-mem-table for postcopy= "); + return false; + } + + /* + * Wait for QEMU to confirm that it's registered the handler for t= he + * faults. + */ + if (!dev->read_msg(dev, dev->sock, vmsg) || + vmsg->size !=3D sizeof(vmsg->payload.u64) || + vmsg->payload.u64 !=3D 0) { + vu_panic(dev, "failed to receive valid ack for postcopy set-me= m-table"); + return false; + } + + /* OK, now we can go and register the memory and generate faults */ + (void)generate_faults(dev); + return false; + } + for (i =3D 0; i < dev->max_queues; i++) { if (dev->vq[i].vring.desc) { if (map_ring(dev, &dev->vq[i])) { --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923885; cv=none; d=zohomail.com; s=zohoarc; b=C5omd78lL7wxueiR3I7PQYc6eQ7NBEOOrEmq6KO3M/hRGNehJ0ZbYAyH4POk8ZWLrrqmx0UitJIDFzCpeF7bFrq9J2AI0pNcsGuVto5UMzXU6g1aQP/GazCW/yz2Jf56B0M273Nkx1lphzG3ZFE6w7zRSVKhW7cak61KMgfrrTc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923885; 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=bYuVYjbipeO6IX5HsCSIPRM1Jfer1+FbLhArUi42e5c=; b=TY8mRFmeEVnnUS02dfHqowWwj7a5r5oLnDzaW0WoD1gdEDP9kxxZ3A3OMwsi/zZQ3fNyTVBKfQJxRPnUfXox/VKQqdJJXtYvEfGE+kF8sSXaXtCxUFmlARzUKx3lW1+WoJQY/4agyBQYYNnc0KIQeleqIWUOXAkiLSe/jx0uXgs= 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 1707923885340484.2518459856626; Wed, 14 Feb 2024 07:18:05 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH0p-0007nL-Tm; Wed, 14 Feb 2024 10:17:27 -0500 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 1raH0o-0007mi-Ik for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:26 -0500 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 1raH0m-0000MM-HH for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:26 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-421-0C7l77vCOlGvU0xLUzvg9g-1; Wed, 14 Feb 2024 10:17:22 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 C465F83F279; Wed, 14 Feb 2024 15:17:21 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id F13A51C02ECA; Wed, 14 Feb 2024 15:17:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923843; 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=bYuVYjbipeO6IX5HsCSIPRM1Jfer1+FbLhArUi42e5c=; b=HZ2hHTz0apfzxj7wn3gkIMjv4PnTPmbmvRw/hanV3+z/zY8fX+Gaph+SXowvY5d1Ba9H0u ISPZ8jdN6ck8Hs54SSbX7XZS1AT3Nylmk9jw3U36aXchIUvEfBDU9mU4fM9ibSIrKIkxS8 csDfuQXZ/tPiaCBHYMlKctIIwJsItEE= X-MC-Unique: 0C7l77vCOlGvU0xLUzvg9g-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 05/14] libvhost-user: Factor out adding a memory region Date: Wed, 14 Feb 2024 16:16:52 +0100 Message-ID: <20240214151701.29906-6-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923887209100011 Content-Type: text/plain; charset="utf-8" Let's factor it out, reducing quite some code duplication and perparing for further changes. If we fail to mmap a region and panic, we now simply don't add that (broken) region. Note that we now increment dev->nregions as we are successfully adding memory regions, and don't increment dev->nregions if anything went wrong. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 168 ++++++++-------------- 1 file changed, 60 insertions(+), 108 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index a7bd7de3cd..f43b5096d0 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -256,6 +256,61 @@ vu_remove_all_mem_regs(VuDev *dev) dev->nregions =3D 0; } =20 +static void +_vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion *msg_region, int fd) +{ + int prot =3D PROT_READ | PROT_WRITE; + VuDevRegion *r; + void *mmap_addr; + + DPRINT("Adding region %d\n", dev->nregions); + DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n", + msg_region->guest_phys_addr); + DPRINT(" memory_size: 0x%016"PRIx64"\n", + msg_region->memory_size); + DPRINT(" userspace_addr: 0x%016"PRIx64"\n", + msg_region->userspace_addr); + DPRINT(" mmap_offset: 0x%016"PRIx64"\n", + msg_region->mmap_offset); + + if (dev->postcopy_listening) { + /* + * In postcopy we're using PROT_NONE here to catch anyone + * accessing it before we userfault + */ + prot =3D PROT_NONE; + } + + /* + * We don't use offset argument of mmap() since the mapped address has + * to be page aligned, and we use huge pages. + */ + mmap_addr =3D mmap(0, msg_region->memory_size + msg_region->mmap_offse= t, + prot, MAP_SHARED | MAP_NORESERVE, fd, 0); + if (mmap_addr =3D=3D MAP_FAILED) { + vu_panic(dev, "region mmap error: %s", strerror(errno)); + return; + } + DPRINT(" mmap_addr: 0x%016"PRIx64"\n", + (uint64_t)(uintptr_t)mmap_addr); + + r =3D &dev->regions[dev->nregions]; + r->gpa =3D msg_region->guest_phys_addr; + r->size =3D msg_region->memory_size; + r->qva =3D msg_region->userspace_addr; + r->mmap_addr =3D (uint64_t)(uintptr_t)mmap_addr; + r->mmap_offset =3D msg_region->mmap_offset; + dev->nregions++; + + if (dev->postcopy_listening) { + /* + * Return the address to QEMU so that it can translate the ufd + * fault addresses back. + */ + msg_region->userspace_addr =3D r->mmap_addr + r->mmap_offset; + } +} + static void vmsg_close_fds(VhostUserMsg *vmsg) { @@ -727,10 +782,7 @@ generate_faults(VuDev *dev) { static bool vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { int i; - bool track_ramblocks =3D dev->postcopy_listening; VhostUserMemoryRegion m =3D vmsg->payload.memreg.region, *msg_region = =3D &m; - VuDevRegion *dev_region =3D &dev->regions[dev->nregions]; - void *mmap_addr; =20 if (vmsg->fd_num !=3D 1) { vmsg_close_fds(vmsg); @@ -760,69 +812,20 @@ vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { * we know all the postcopy client bases have been received, and we * should start generating faults. */ - if (track_ramblocks && + if (dev->postcopy_listening && vmsg->size =3D=3D sizeof(vmsg->payload.u64) && vmsg->payload.u64 =3D=3D 0) { (void)generate_faults(dev); return false; } =20 - DPRINT("Adding region: %u\n", dev->nregions); - DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n", - msg_region->guest_phys_addr); - DPRINT(" memory_size: 0x%016"PRIx64"\n", - msg_region->memory_size); - DPRINT(" userspace_addr 0x%016"PRIx64"\n", - msg_region->userspace_addr); - DPRINT(" mmap_offset 0x%016"PRIx64"\n", - msg_region->mmap_offset); - - dev_region->gpa =3D msg_region->guest_phys_addr; - dev_region->size =3D msg_region->memory_size; - dev_region->qva =3D msg_region->userspace_addr; - dev_region->mmap_offset =3D msg_region->mmap_offset; - - /* - * We don't use offset argument of mmap() since the - * mapped address has to be page aligned, and we use huge - * pages. - */ - if (track_ramblocks) { - /* - * In postcopy we're using PROT_NONE here to catch anyone - * accessing it before we userfault. - */ - mmap_addr =3D mmap(0, dev_region->size + dev_region->mmap_offset, - PROT_NONE, MAP_SHARED | MAP_NORESERVE, - vmsg->fds[0], 0); - } else { - mmap_addr =3D mmap(0, dev_region->size + dev_region->mmap_offset, - PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERV= E, - vmsg->fds[0], 0); - } - - if (mmap_addr =3D=3D MAP_FAILED) { - vu_panic(dev, "region mmap error: %s", strerror(errno)); - } else { - dev_region->mmap_addr =3D (uint64_t)(uintptr_t)mmap_addr; - DPRINT(" mmap_addr: 0x%016"PRIx64"\n", - dev_region->mmap_addr); - } - + _vu_add_mem_reg(dev, msg_region, vmsg->fds[0]); close(vmsg->fds[0]); =20 - if (track_ramblocks) { - /* - * Return the address to QEMU so that it can translate the ufd - * fault addresses back. - */ - msg_region->userspace_addr =3D (uintptr_t)(mmap_addr + - dev_region->mmap_offset); - + if (dev->postcopy_listening) { /* Send the message back to qemu with the addresses filled in. */ vmsg->fd_num =3D 0; DPRINT("Successfully added new region in postcopy\n"); - dev->nregions++; return true; } else { for (i =3D 0; i < dev->max_queues; i++) { @@ -835,7 +838,6 @@ vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { } =20 DPRINT("Successfully added new region\n"); - dev->nregions++; return false; } } @@ -940,63 +942,13 @@ static bool vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg) { VhostUserMemory m =3D vmsg->payload.memory, *memory =3D &m; - int prot =3D PROT_READ | PROT_WRITE; unsigned int i; =20 - if (dev->postcopy_listening) { - /* - * In postcopy we're using PROT_NONE here to catch anyone - * accessing it before we userfault - */ - prot =3D PROT_NONE; - } - vu_remove_all_mem_regs(dev); - dev->nregions =3D memory->nregions; =20 DPRINT("Nregions: %u\n", memory->nregions); - for (i =3D 0; i < dev->nregions; i++) { - void *mmap_addr; - VhostUserMemoryRegion *msg_region =3D &memory->regions[i]; - VuDevRegion *dev_region =3D &dev->regions[i]; - - DPRINT("Region %d\n", i); - DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n", - msg_region->guest_phys_addr); - DPRINT(" memory_size: 0x%016"PRIx64"\n", - msg_region->memory_size); - DPRINT(" userspace_addr 0x%016"PRIx64"\n", - msg_region->userspace_addr); - DPRINT(" mmap_offset 0x%016"PRIx64"\n", - msg_region->mmap_offset); - - dev_region->gpa =3D msg_region->guest_phys_addr; - dev_region->size =3D msg_region->memory_size; - dev_region->qva =3D msg_region->userspace_addr; - dev_region->mmap_offset =3D msg_region->mmap_offset; - - /* We don't use offset argument of mmap() since the - * mapped address has to be page aligned, and we use huge - * pages. */ - mmap_addr =3D mmap(0, dev_region->size + dev_region->mmap_offset, - prot, MAP_SHARED | MAP_NORESERVE, vmsg->fds[i], 0= ); - - if (mmap_addr =3D=3D MAP_FAILED) { - vu_panic(dev, "region mmap error: %s", strerror(errno)); - } else { - dev_region->mmap_addr =3D (uint64_t)(uintptr_t)mmap_addr; - DPRINT(" mmap_addr: 0x%016"PRIx64"\n", - dev_region->mmap_addr); - } - - if (dev->postcopy_listening) { - /* - * Return the address to QEMU so that it can translate the ufd - * fault addresses back. - */ - msg_region->userspace_addr =3D (uintptr_t)(mmap_addr + - dev_region->mmap_offs= et); - } + for (i =3D 0; i < memory->nregions; i++) { + _vu_add_mem_reg(dev, &memory->regions[i], vmsg->fds[i]); close(vmsg->fds[i]); } =20 --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707924012; cv=none; d=zohomail.com; s=zohoarc; b=QctMKulTB6Lomv9O2W6f6F51dv5nUUvzGvidANLE6OyqLC0S6bncuucw1si7Pv85fQgwB+rc3Wl6ziSYQ+juzgVCVqvka5luizD+iROYceq4ktPJiuvorEoKeB1NPDfn7ByZUR1Y3JllNRFNfVckgKtvoZ7sFCoDX+7bibwCSCY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707924012; 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=VPHqjxQpRiRtRDIIggbdguev1zt3QIU4HlnJgsRz948=; b=ie9hLVXMgXaiK2+h3a5E+174nnSbRXi4pj3T7uhLljIdtLzV4B6OZzVy2QTr4MXiq5VTtHOK+PlX7wBMxALJFPzHk1LJhIGqaC0v9dm3EwlqSyylXpdn+Tg3ar8dGSvsokDH1FRet75ZNg2lwYthzgjDUA0pY76Ni//Nc6nQgOE= 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 17079240122271019.531448798136; Wed, 14 Feb 2024 07:20:12 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH0s-0007o1-PI; Wed, 14 Feb 2024 10:17:30 -0500 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 1raH0q-0007nh-U8 for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:28 -0500 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 1raH0p-0000NU-K9 for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:28 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-693--8MLhHEdOGallgqmNQH38g-1; Wed, 14 Feb 2024 10:17:25 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 AD57985A588; Wed, 14 Feb 2024 15:17:24 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A1111C060B1; Wed, 14 Feb 2024 15:17:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923846; 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=VPHqjxQpRiRtRDIIggbdguev1zt3QIU4HlnJgsRz948=; b=ApdS0P+S8AzNPNIIWxx9JHjE/mAfzViHwg9pSzaVuD7dlTMwm7X5R/cgPLgvAxtEHjrCbO 3WFQSZKLeFVG7RKpi6jdWIRUDExRJvnYLbZQsAcrwF5Bj2s8uLlE+j+M/O+LBS+k6B7wdz V+lae3UGUfvdV3Re+K6/RmsFMoXlhbA= X-MC-Unique: -8MLhHEdOGallgqmNQH38g-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 06/14] libvhost-user: No need to check for NULL when unmapping Date: Wed, 14 Feb 2024 16:16:53 +0100 Message-ID: <20240214151701.29906-7-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707924013650100003 Content-Type: text/plain; charset="utf-8" We never add a memory region if mmap() failed. Therefore, no need to check for NULL. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index f43b5096d0..225283f764 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -247,11 +247,8 @@ vu_remove_all_mem_regs(VuDev *dev) =20 for (i =3D 0; i < dev->nregions; i++) { VuDevRegion *r =3D &dev->regions[i]; - void *ma =3D (void *)(uintptr_t)r->mmap_addr; =20 - if (ma) { - munmap(ma, r->size + r->mmap_offset); - } + munmap((void *)(uintptr_t)r->mmap_addr, r->size + r->mmap_offset); } dev->nregions =3D 0; } @@ -888,11 +885,8 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { for (i =3D 0; i < dev->nregions; i++) { if (reg_equal(&dev->regions[i], msg_region)) { VuDevRegion *r =3D &dev->regions[i]; - void *ma =3D (void *) (uintptr_t) r->mmap_addr; =20 - if (ma) { - munmap(ma, r->size + r->mmap_offset); - } + munmap((void *)(uintptr_t)r->mmap_addr, r->size + r->mmap_offs= et); =20 /* * Shift all affected entries by 1 to close the hole at index = i and --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923925; cv=none; d=zohomail.com; s=zohoarc; b=kZymz8AwUlgkYXAZqLOboTIaDnZ8MYiJqVi1naxOrY7W2+fc2ryoC7Ae4Bo5cJ/W+zNKgyniqV3YSeIOXBs5t9avY34ELdcyc9kDP/vNflPku2Hpht/aQzVYUWcPKcGkptKqc5GwA28gvsy1FAuKU5+kGF7jyDkvA4Gj7on/bc0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923925; 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=One4X5o0fH7+3UMZ+ay4SRtGvmXlCFeNr2ddU55pqks=; b=TdCiTKXtBC9j9phrxQ3mG+/1hEbfd5eL09dTaqDvM1e97GQj49zldl2ji43H1NPYT3buiFGqDVdGrCBAm/EQOLiDHHvbZdsdKQwkx4rrbTYwV6+VNe5gBN4wcMPFk3nw4U+nhcYqQ9J8B+P1fVgmBKu+DFaAmHsIAky5+ycD06E= 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 1707923925113486.73817193406046; Wed, 14 Feb 2024 07:18:45 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH0y-0007pZ-7U; Wed, 14 Feb 2024 10:17:36 -0500 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 1raH0v-0007od-Op for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:34 -0500 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 1raH0u-0000OA-DH for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:33 -0500 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-288-SdXVBWUDPMCn2hpTJ9VRlA-1; Wed, 14 Feb 2024 10:17:28 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 F0DAD1C06915; Wed, 14 Feb 2024 15:17:27 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 19A531C02ECA; Wed, 14 Feb 2024 15:17:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923851; 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=One4X5o0fH7+3UMZ+ay4SRtGvmXlCFeNr2ddU55pqks=; b=Lbh369pFgThH7GSOaTDgpVaXVOpZ7j5ttmppd/veG5BUXzrNC9urKlO53wQ6zrcBA5PsmB /id98hVP1gCDIvtgSri7Vxh1y69KpLJJf9yw1tpZGvSeKetooZxH6RcFCz0G0S0VC80R3D dkSAFKm+aFFSu6esYKUM/IfgeOwMt4U= X-MC-Unique: SdXVBWUDPMCn2hpTJ9VRlA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 07/14] libvhost-user: Don't zero out memory for memory regions Date: Wed, 14 Feb 2024 16:16:54 +0100 Message-ID: <20240214151701.29906-8-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923927390100003 Content-Type: text/plain; charset="utf-8" dev->nregions always covers only valid entries. Stop zeroing out other array elements that are unused. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index 225283f764..2e8b611385 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -888,13 +888,9 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { =20 munmap((void *)(uintptr_t)r->mmap_addr, r->size + r->mmap_offs= et); =20 - /* - * Shift all affected entries by 1 to close the hole at index = i and - * zero out the last entry. - */ + /* Shift all affected entries by 1 to close the hole at index.= */ memmove(dev->regions + i, dev->regions + i + 1, sizeof(VuDevRegion) * (dev->nregions - i - 1)); - memset(dev->regions + dev->nregions - 1, 0, sizeof(VuDevRegion= )); DPRINT("Successfully removed a region\n"); dev->nregions--; i--; @@ -2119,7 +2115,6 @@ vu_init(VuDev *dev, DPRINT("%s: failed to malloc mem regions\n", __func__); return false; } - memset(dev->regions, 0, VHOST_USER_MAX_RAM_SLOTS * sizeof(dev->regions= [0])); =20 dev->vq =3D malloc(max_queues * sizeof(dev->vq[0])); if (!dev->vq) { --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923970; cv=none; d=zohomail.com; s=zohoarc; b=dIdRmQtulXd0zo1X6fxqka4XZUk1pqoRt209ezybCt9ExOrDn+zAKJItPFRkHQptZ8ZcyUKKpIZM8zSLrIZByl6Fx9QfkSkBkptsrLO2nW1Fu9/4xBsTEtmu84K2drL1TjUjd6umRhqyR8yGNa+QtA/43wCroRca/hfA8d02L3c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923970; 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=dxj28ap7rEkCWOXXUytFMAylfWHpiWTY6Vrv6REnUiw=; b=XQ7YSx59LFuwGPLF7vPGnDX9PvE12T4Sl1dA58bT4zxr4gqkFufva704BRRJy4oJ8dDCbCyD4xDA1Bhj0UvrBfbFIC6ZNpn2MaeqUQ4gndaFzD0uSW51iGIQVQXJKLOCPytmfqKA/AgRuKgKD7ZMK5HAEyynHLRpjJcqFZxgKyU= 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 1707923970886688.4451954592959; Wed, 14 Feb 2024 07:19:30 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH10-0007qU-GB; Wed, 14 Feb 2024 10:17:38 -0500 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 1raH0y-0007pv-TB for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:36 -0500 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 1raH0x-0000Oo-E1 for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:36 -0500 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-435-t7ewWYpTMD6Z92Mvm0eQYQ-1; Wed, 14 Feb 2024 10:17:31 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 0581A29AA2CF; Wed, 14 Feb 2024 15:17:31 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5D2D71C060B1; Wed, 14 Feb 2024 15:17:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923854; 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=dxj28ap7rEkCWOXXUytFMAylfWHpiWTY6Vrv6REnUiw=; b=I902RSeKWmw8vMAAEgsu7G9z4b9gbycHqrmEP4JGjbSzH3DLM0BExsb8hGPsXZdMyqIASL wIjx+Qkb1wpqCuHHezCZfotowOwokwgAGINhx8TQSLhTf21Ca0NHJdPznNyqnFwQxQRNDV P/afeOtarhIBq/cWj7N8dJbO/bTx0YQ= X-MC-Unique: t7ewWYpTMD6Z92Mvm0eQYQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 08/14] libvhost-user: Don't search for duplicates when removing memory regions Date: Wed, 14 Feb 2024 16:16:55 +0100 Message-ID: <20240214151701.29906-9-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923971448100001 Content-Type: text/plain; charset="utf-8" We cannot have duplicate memory regions, something would be deeply flawed elsewhere. Let's just stop the search once we found an entry. We'll add more sanity checks when adding memory regions later. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index 2e8b611385..7f29e01c30 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -896,8 +896,7 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { i--; =20 found =3D true; - - /* Continue the search for eventual duplicates. */ + break; } } =20 --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923982; cv=none; d=zohomail.com; s=zohoarc; b=PIxbFjDbPFoXuo+a9h/VcSA6cLhUx/8TqsrfzulSaVexbH9aabk6vxepDUcwejnyePshB8ZF3+eAmKULNRjVW24KhTZul+XXMIvoqCAcG9I7UoKMU27Dc0ideYtxT0k2QZ3Qjmue0gbpWavYC2mrwc2at8uzAjk/nD83iMRDDCg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923982; 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=4XvHRrSZGz0C59Zu95hBu5n9kentS+2TwgEY3I45/xk=; b=f0hLYxCVGRt52NbUWG5PpjcB+hSSL9hT1FG/NwAbDTG7ln6z6oOGsEUVt2e9ndal47YPRB9AnTgG/0ndKJ4ZeeHZ8CpQBmAVlREAeRL9Ba1S25PxJP6nFu6Ak5RuvqP6uq3Co0doUcVstOqwoscHmS13nmFnS4V8dKYLeMk4tGc= 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 170792398239868.97248656343402; Wed, 14 Feb 2024 07:19:42 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH16-0007rR-Mx; Wed, 14 Feb 2024 10:17:44 -0500 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 1raH12-0007qz-Kf for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:40 -0500 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 1raH10-0000PQ-SD for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:40 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-527-_3p6Hmf6OI-LMlamvCbY4g-1; Wed, 14 Feb 2024 10:17:34 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 20724185A783; Wed, 14 Feb 2024 15:17:34 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 55A7E1C066AA; Wed, 14 Feb 2024 15:17:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923858; 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=4XvHRrSZGz0C59Zu95hBu5n9kentS+2TwgEY3I45/xk=; b=AD5FKGKahUv1pmlXHSf08zkkLhL7DYZvhIIz3mr1QNjOvVNgDFx6cj/XRcpvFZV081z+fK ZL+zoWPTSesFuEaM5u5G8ts7U/q/2KemKUYHRlPs48SV6/sSzhuBsv0h3xyPjxJspljQm3 zXrPIrV3O8N2A1rf+gCthD/cXsnGe4U= X-MC-Unique: _3p6Hmf6OI-LMlamvCbY4g-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 09/14] libvhost-user: Factor out search for memory region by GPA and simplify Date: Wed, 14 Feb 2024 16:16:56 +0100 Message-ID: <20240214151701.29906-10-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923983512100003 Content-Type: text/plain; charset="utf-8" Memory regions cannot overlap, and if we ever hit that case something would be really flawed. For example, when vhost code in QEMU decides to increase the size of memory regions to cover full huge pages, it makes sure to never create overlaps, and if there would be overlaps, it would bail out. QEMU commits 48d7c9757749 ("vhost: Merge sections added to temporary list"), c1ece84e7c93 ("vhost: Huge page align and merge") and e7b94a84b6cb ("vhost: Allow adjoining regions") added and clarified that handling and how overlaps are impossible. Consequently, each GPA can belong to at most one memory region, and everything else doesn't make sense. Let's factor out our search to prepare for further changes. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 79 +++++++++++++---------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index 7f29e01c30..d72f25396d 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -195,30 +195,47 @@ vu_panic(VuDev *dev, const char *msg, ...) */ } =20 +/* Search for a memory region that covers this guest physical address. */ +static VuDevRegion * +vu_gpa_to_mem_region(VuDev *dev, uint64_t guest_addr) +{ + unsigned int i; + + /* + * Memory regions cannot overlap in guest physical address space. Each + * GPA belongs to exactly one memory region, so there can only be one + * match. + */ + for (i =3D 0; i < dev->nregions; i++) { + VuDevRegion *cur =3D &dev->regions[i]; + + if (guest_addr >=3D cur->gpa && guest_addr < cur->gpa + cur->size)= { + return cur; + } + } + return NULL; +} + /* Translate guest physical address to our virtual address. */ void * vu_gpa_to_va(VuDev *dev, uint64_t *plen, uint64_t guest_addr) { - unsigned int i; + VuDevRegion *r; =20 if (*plen =3D=3D 0) { return NULL; } =20 - /* Find matching memory region. */ - for (i =3D 0; i < dev->nregions; i++) { - VuDevRegion *r =3D &dev->regions[i]; - - if ((guest_addr >=3D r->gpa) && (guest_addr < (r->gpa + r->size)))= { - if ((guest_addr + *plen) > (r->gpa + r->size)) { - *plen =3D r->gpa + r->size - guest_addr; - } - return (void *)(uintptr_t) - guest_addr - r->gpa + r->mmap_addr + r->mmap_offset; - } + r =3D vu_gpa_to_mem_region(dev, guest_addr); + if (!r) { + return NULL; } =20 - return NULL; + if ((guest_addr + *plen) > (r->gpa + r->size)) { + *plen =3D r->gpa + r->size - guest_addr; + } + return (void *)(uintptr_t)guest_addr - r->gpa + r->mmap_addr + + r->mmap_offset; } =20 /* Translate qemu virtual address to our virtual address. */ @@ -854,8 +871,8 @@ static inline bool reg_equal(VuDevRegion *vudev_reg, static bool vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { VhostUserMemoryRegion m =3D vmsg->payload.memreg.region, *msg_region = =3D &m; - unsigned int i; - bool found =3D false; + unsigned int idx; + VuDevRegion *r; =20 if (vmsg->fd_num > 1) { vmsg_close_fds(vmsg); @@ -882,28 +899,22 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { DPRINT(" mmap_offset 0x%016"PRIx64"\n", msg_region->mmap_offset); =20 - for (i =3D 0; i < dev->nregions; i++) { - if (reg_equal(&dev->regions[i], msg_region)) { - VuDevRegion *r =3D &dev->regions[i]; - - munmap((void *)(uintptr_t)r->mmap_addr, r->size + r->mmap_offs= et); - - /* Shift all affected entries by 1 to close the hole at index.= */ - memmove(dev->regions + i, dev->regions + i + 1, - sizeof(VuDevRegion) * (dev->nregions - i - 1)); - DPRINT("Successfully removed a region\n"); - dev->nregions--; - i--; - - found =3D true; - break; - } - } - - if (!found) { + r =3D vu_gpa_to_mem_region(dev, msg_region->guest_phys_addr); + if (!r || !reg_equal(r, msg_region)) { + vmsg_close_fds(vmsg); vu_panic(dev, "Specified region not found\n"); + return false; } =20 + munmap((void *)(uintptr_t)r->mmap_addr, r->size + r->mmap_offset); + + idx =3D r - dev->regions; + assert(idx < dev->nregions); + /* Shift all affected entries by 1 to close the hole. */ + memmove(r, r + 1, sizeof(VuDevRegion) * (dev->nregions - idx - 1)); + DPRINT("Successfully removed a region\n"); + dev->nregions--; + vmsg_close_fds(vmsg); =20 return false; --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923998; cv=none; d=zohomail.com; s=zohoarc; b=UjfKMVZWzGDwue2bSZ9Ox4tdX/VtQZTjLL5xPGa7D6+z3w3pWENuPbk9DcaM7c/UoJ7WXo2eLh2HsReaMapJ47g+BHrTCYqbMvUUgRpqLLBd3PntUVJMWyOvNusFtSpmjjeu+n8yfdk6JJEiQ3fht8nih9ga6ovtr0ynmezsEA8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923998; 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=my+wGO0YbSzMmtMrev16Z2y1/ALKJa0gbZGKAXKRFuU=; b=eYpGha85hZgQbkUPkxKZuERA2xNwH2iOJoerIzT0dXuIC0z/b5aMswo3H+V+OXoGwkZDh9vODSaTedDjGjRXktt/6ItANLL4SO+qy1ysb7nq57imPimTacfQUMPwnJPUWJ+4KTVTZN6E37AAs+zGa38Ai2f3qkuNqbAqIbt+kqo= 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 1707923998008123.31728238277833; Wed, 14 Feb 2024 07:19:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH18-0007tV-RP; Wed, 14 Feb 2024 10:17:46 -0500 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 1raH14-0007rQ-8R for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:44 -0500 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 1raH12-0000Pi-Ks for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:41 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-601-1gBfL7e-NsauB3GKoh2iyA-1; Wed, 14 Feb 2024 10:17:37 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 23A9D85CDF2; Wed, 14 Feb 2024 15:17:37 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6E9831C03428; Wed, 14 Feb 2024 15:17:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923859; 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=my+wGO0YbSzMmtMrev16Z2y1/ALKJa0gbZGKAXKRFuU=; b=RNemFPjlITP8ed2QZA+qYeJ7sEEthJRJnmHYkPF1Yb5SsO4ft9aVoik8Avb9joKrezaq2x SYDIDmQKTJ9EtO1kJsoCfJJCu9OOPQa8Jh96hPrSg6ajKcq35gH/CrGJshM3hJk98tfST1 Pu/0ED9zSCtRpOrXbYbhhEiQBzekUeQ= X-MC-Unique: 1gBfL7e-NsauB3GKoh2iyA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 10/14] libvhost-user: Speedup gpa_to_mem_region() and vu_gpa_to_va() Date: Wed, 14 Feb 2024 16:16:57 +0100 Message-ID: <20240214151701.29906-11-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923999586100003 Content-Type: text/plain; charset="utf-8" Let's speed up GPA to memory region / virtual address lookup. Store the memory regions ordered by guest physical addresses, and use binary search for address translation, as well as when adding/removing memory regions. Most importantly, this will speed up GPA->VA address translation when we have many memslots. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 49 +++++++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index d72f25396d..ef6353d847 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -199,19 +199,30 @@ vu_panic(VuDev *dev, const char *msg, ...) static VuDevRegion * vu_gpa_to_mem_region(VuDev *dev, uint64_t guest_addr) { - unsigned int i; + int low =3D 0; + int high =3D dev->nregions - 1; =20 /* * Memory regions cannot overlap in guest physical address space. Each * GPA belongs to exactly one memory region, so there can only be one * match. + * + * We store our memory regions ordered by GPA and can simply perform a + * binary search. */ - for (i =3D 0; i < dev->nregions; i++) { - VuDevRegion *cur =3D &dev->regions[i]; + while (low <=3D high) { + unsigned int mid =3D low + (high - low) / 2; + VuDevRegion *cur =3D &dev->regions[mid]; =20 if (guest_addr >=3D cur->gpa && guest_addr < cur->gpa + cur->size)= { return cur; } + if (guest_addr >=3D cur->gpa + cur->size) { + low =3D mid + 1; + } + if (guest_addr < cur->gpa) { + high =3D mid - 1; + } } return NULL; } @@ -273,9 +284,14 @@ vu_remove_all_mem_regs(VuDev *dev) static void _vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion *msg_region, int fd) { + const uint64_t start_gpa =3D msg_region->guest_phys_addr; + const uint64_t end_gpa =3D start_gpa + msg_region->memory_size; int prot =3D PROT_READ | PROT_WRITE; VuDevRegion *r; void *mmap_addr; + int low =3D 0; + int high =3D dev->nregions - 1; + unsigned int idx; =20 DPRINT("Adding region %d\n", dev->nregions); DPRINT(" guest_phys_addr: 0x%016"PRIx64"\n", @@ -295,6 +311,29 @@ _vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion *msg= _region, int fd) prot =3D PROT_NONE; } =20 + /* + * We will add memory regions into the array sorted by GPA. Perform a + * binary search to locate the insertion point: it will be at the low + * index. + */ + while (low <=3D high) { + unsigned int mid =3D low + (high - low) / 2; + VuDevRegion *cur =3D &dev->regions[mid]; + + /* Overlap of GPA addresses. */ + if (start_gpa < cur->gpa + cur->size && cur->gpa < end_gpa) { + vu_panic(dev, "regions with overlapping guest physical address= es"); + return; + } + if (start_gpa >=3D cur->gpa + cur->size) { + low =3D mid + 1; + } + if (start_gpa < cur->gpa) { + high =3D mid - 1; + } + } + idx =3D low; + /* * We don't use offset argument of mmap() since the mapped address has * to be page aligned, and we use huge pages. @@ -308,7 +347,9 @@ _vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion *msg_= region, int fd) DPRINT(" mmap_addr: 0x%016"PRIx64"\n", (uint64_t)(uintptr_t)mmap_addr); =20 - r =3D &dev->regions[dev->nregions]; + /* Shift all affected entries by 1 to open a hole at idx. */ + r =3D &dev->regions[idx]; + memmove(r + 1, r, sizeof(VuDevRegion) * (dev->nregions - idx)); r->gpa =3D msg_region->guest_phys_addr; r->size =3D msg_region->memory_size; r->qva =3D msg_region->userspace_addr; --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923906; cv=none; d=zohomail.com; s=zohoarc; b=Xt6ZPS0sxbiT86d+7AdXmam4iVxaln7dDCVgeX3CLGj5URJ6Lbcx9cyfkY19lP93p7IxO8tVnRIYF1JIXWLxTrcc/EZmtOZAS3O2BUSTLLagOktE8dYz6nK93KTWyHnP/K8jUTbf1S037tC75guaK00k6m6oNFqZKGW2jOPt/n8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923906; 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=rxV7qmtkwuVczLTxmNjIfKVqthnX4qcJ3g6F4Fre2ks=; b=njXtZANl/WJ5Xmn4aFHUdPsxRnyO65NeQZnmVz7MYHFATwNPn/KgOpRP2A0evM+8e84tj93iGi0nVDlaROT682aU7rWZ9UpcB8gPnIKut4r7g3/Anb8RAziPCZZtkRCaI9EHE+E06+0wKkIV9fcKOPYPnKnF0PMPauLsk3rlipE= 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 170792390605931.131519234248344; Wed, 14 Feb 2024 07:18:26 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH1B-00082o-Fa; Wed, 14 Feb 2024 10:17:49 -0500 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 1raH19-0007zL-OD for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:47 -0500 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 1raH17-0000QD-3N for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:47 -0500 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-147-Q7LVip_nP9qwIh6bjHgeBQ-1; Wed, 14 Feb 2024 10:17:40 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 7294C3812582; Wed, 14 Feb 2024 15:17:40 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id 67A841C066A9; Wed, 14 Feb 2024 15:17:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923864; 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=rxV7qmtkwuVczLTxmNjIfKVqthnX4qcJ3g6F4Fre2ks=; b=fMRTX2kKrMOZZX8/j/6pOeUBGYDSIa0vnaQ3o4wDg+UE5yBFc+Wc1zEpqMSeupn87HSprv NSjMWShXr9FQVVr7kUlL80mHs47YvqDLl3C9peW4G9DOVh91rk9liUE3+LdgvHwheCMKpu 8nDlS4VhGHIcxn7zGraMm/hBejAsgsc= X-MC-Unique: Q7LVip_nP9qwIh6bjHgeBQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 11/14] libvhost-user: Use most of mmap_offset as fd_offset Date: Wed, 14 Feb 2024 16:16:58 +0100 Message-ID: <20240214151701.29906-12-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923907195100003 Content-Type: text/plain; charset="utf-8" In the past, QEMU would create memory regions that could partially cover hugetlb pages, making mmap() fail if we would use the mmap_offset as an fd_offset. For that reason, we never used the mmap_offset as an offset into the fd and instead always mapped the fd from the very start. However, that can easily result in us mmap'ing a lot of unnecessary parts of an fd, possibly repeatedly. QEMU nowadays does not create memory regions that partially cover huge pages -- it never really worked with postcopy. QEMU handles merging of regions that partially cover huge pages (due to holes in boot memory) since 2018 in c1ece84e7c93 ("vhost: Huge page align and merge"). Let's be a bit careful and not unconditionally convert the mmap_offset into an fd_offset. Instead, let's simply detect the hugetlb size and pass as much as we can as fd_offset, making sure that we call mmap() with a properly aligned offset. With QEMU and a virtio-mem device that is fully plugged (50GiB using 50 memslots) the qemu-storage daemon process consumes in the VA space 1281GiB before this change and 58GiB after this change. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Vhost user message =3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Request: VHOST_USER_ADD_MEM_REG (37) Flags: 0x9 Size: 40 Fds: 59 Adding region 4 guest_phys_addr: 0x0000000200000000 memory_size: 0x0000000040000000 userspace_addr: 0x00007fb73bffe000 old mmap_offset: 0x0000000080000000 fd_offset: 0x0000000080000000 new mmap_offset: 0x0000000000000000 mmap_addr: 0x00007f02f1bdc000 Successfully added new region =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Vhost user message =3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Request: VHOST_USER_ADD_MEM_REG (37) Flags: 0x9 Size: 40 Fds: 59 Adding region 5 guest_phys_addr: 0x0000000240000000 memory_size: 0x0000000040000000 userspace_addr: 0x00007fb77bffe000 old mmap_offset: 0x00000000c0000000 fd_offset: 0x00000000c0000000 new mmap_offset: 0x0000000000000000 mmap_addr: 0x00007f0284000000 Successfully added new region Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 54 ++++++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index ef6353d847..55aef5fcc6 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -43,6 +43,8 @@ #include #include #include +#include +#include =20 #ifdef __NR_userfaultfd #include @@ -281,12 +283,32 @@ vu_remove_all_mem_regs(VuDev *dev) dev->nregions =3D 0; } =20 +static size_t +get_fd_hugepagesize(int fd) +{ +#if defined(__linux__) + struct statfs fs; + int ret; + + do { + ret =3D fstatfs(fd, &fs); + } while (ret !=3D 0 && errno =3D=3D EINTR); + + if (!ret && (unsigned int)fs.f_type =3D=3D HUGETLBFS_MAGIC) { + return fs.f_bsize; + } +#endif + return 0; +} + static void _vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion *msg_region, int fd) { const uint64_t start_gpa =3D msg_region->guest_phys_addr; const uint64_t end_gpa =3D start_gpa + msg_region->memory_size; int prot =3D PROT_READ | PROT_WRITE; + uint64_t mmap_offset, fd_offset; + size_t hugepagesize; VuDevRegion *r; void *mmap_addr; int low =3D 0; @@ -300,7 +322,7 @@ _vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion *msg_= region, int fd) msg_region->memory_size); DPRINT(" userspace_addr: 0x%016"PRIx64"\n", msg_region->userspace_addr); - DPRINT(" mmap_offset: 0x%016"PRIx64"\n", + DPRINT(" old mmap_offset: 0x%016"PRIx64"\n", msg_region->mmap_offset); =20 if (dev->postcopy_listening) { @@ -335,11 +357,31 @@ _vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion *ms= g_region, int fd) idx =3D low; =20 /* - * We don't use offset argument of mmap() since the mapped address has - * to be page aligned, and we use huge pages. + * Convert most of msg_region->mmap_offset to fd_offset. In almost all + * cases, this will leave us with mmap_offset =3D=3D 0, mmap()'ing only + * what we really need. Only if a memory region would partially cover + * hugetlb pages, we'd get mmap_offset !=3D 0, which usually doesn't h= appen + * anymore (i.e., modern QEMU). + * + * Note that mmap() with hugetlb would fail if the offset into the file + * is not aligned to the huge page size. */ - mmap_addr =3D mmap(0, msg_region->memory_size + msg_region->mmap_offse= t, - prot, MAP_SHARED | MAP_NORESERVE, fd, 0); + hugepagesize =3D get_fd_hugepagesize(fd); + if (hugepagesize) { + fd_offset =3D ALIGN_DOWN(msg_region->mmap_offset, hugepagesize); + mmap_offset =3D msg_region->mmap_offset - fd_offset; + } else { + fd_offset =3D msg_region->mmap_offset; + mmap_offset =3D 0; + } + + DPRINT(" fd_offset: 0x%016"PRIx64"\n", + fd_offset); + DPRINT(" new mmap_offset: 0x%016"PRIx64"\n", + mmap_offset); + + mmap_addr =3D mmap(0, msg_region->memory_size + mmap_offset, + prot, MAP_SHARED | MAP_NORESERVE, fd, fd_offset); if (mmap_addr =3D=3D MAP_FAILED) { vu_panic(dev, "region mmap error: %s", strerror(errno)); return; @@ -354,7 +396,7 @@ _vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion *msg_= region, int fd) r->size =3D msg_region->memory_size; r->qva =3D msg_region->userspace_addr; r->mmap_addr =3D (uint64_t)(uintptr_t)mmap_addr; - r->mmap_offset =3D msg_region->mmap_offset; + r->mmap_offset =3D mmap_offset; dev->nregions++; =20 if (dev->postcopy_listening) { --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923935; cv=none; d=zohomail.com; s=zohoarc; b=aQJs78Iteh0ALH1BAeFhCueAXlb7DFq6B2VpTRe1LVAqHCLsYIzCsqJqze5kTfU93ndKcFzvMEiPrcOQDfV3CwJ3LftMKLsd4Ovy+95EXD00lq8U13FYwp6V0h6HGM2JRlCuidihV+2H7ACYjlUND3fUpchTmP3iyxtfuROIVf4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923935; 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=JxDdDY1G8dEOkRqb31Z0V6a22urxma+UOY5TsNZaGr0=; b=WqnAWNX26zbsS30KEJKZsyglyCEuvy/DJYsr7Ai79xxB1OD1ottmNRUz98rb90jxCM3hxc4r9k8Znm0MJruUpSar7prCWHFdytEOt54cKpt5+5eoezv7iNla0+rsowkCtVAJ8KE3YmPYOYtAV3Myn6vKxdNpYCL2XvqOEAvnsxs= 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 1707923935071830.3752292431831; Wed, 14 Feb 2024 07:18:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH1C-00083E-Cp; Wed, 14 Feb 2024 10:17:50 -0500 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 1raH1A-00080t-Dr for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:48 -0500 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 1raH19-0000QS-1y for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:48 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-670-RV_dkP0hMdGn6gc1RBBJGg-1; Wed, 14 Feb 2024 10:17:43 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 6400A88D01C; Wed, 14 Feb 2024 15:17:43 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id BCADB1C03428; Wed, 14 Feb 2024 15:17:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923866; 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=JxDdDY1G8dEOkRqb31Z0V6a22urxma+UOY5TsNZaGr0=; b=UYbyId00R4T5x+M/HkJufRmsyqwJceAyHOFSE/p9WsE9wDOzgu9SgwggJwItku0Qxm4U2k P+w3yQrLCk7MUZt6PuPQssHeqebdWLlVfUQOV/McEqHeRxlW/8mm0q7ZOYwq/fg7hpREIG MkcflBrY1uWN8IjcQI6bzb2wa3mgbj4= X-MC-Unique: RV_dkP0hMdGn6gc1RBBJGg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 12/14] libvhost-user: Factor out vq usability check Date: Wed, 14 Feb 2024 16:16:59 +0100 Message-ID: <20240214151701.29906-13-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923935288100001 Content-Type: text/plain; charset="utf-8" Let's factor it out to prepare for further changes. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index 55aef5fcc6..ed0a978d4f 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -283,6 +283,12 @@ vu_remove_all_mem_regs(VuDev *dev) dev->nregions =3D 0; } =20 +static bool +vu_is_vq_usable(VuDev *dev, VuVirtq *vq) +{ + return likely(!dev->broken) && likely(vq->vring.avail); +} + static size_t get_fd_hugepagesize(int fd) { @@ -2380,8 +2386,7 @@ vu_queue_get_avail_bytes(VuDev *dev, VuVirtq *vq, uns= igned int *in_bytes, idx =3D vq->last_avail_idx; =20 total_bufs =3D in_total =3D out_total =3D 0; - if (unlikely(dev->broken) || - unlikely(!vq->vring.avail)) { + if (!vu_is_vq_usable(dev, vq)) { goto done; } =20 @@ -2496,8 +2501,7 @@ vu_queue_avail_bytes(VuDev *dev, VuVirtq *vq, unsigne= d int in_bytes, bool vu_queue_empty(VuDev *dev, VuVirtq *vq) { - if (unlikely(dev->broken) || - unlikely(!vq->vring.avail)) { + if (!vu_is_vq_usable(dev, vq)) { return true; } =20 @@ -2536,8 +2540,7 @@ vring_notify(VuDev *dev, VuVirtq *vq) =20 static void _vu_queue_notify(VuDev *dev, VuVirtq *vq, bool sync) { - if (unlikely(dev->broken) || - unlikely(!vq->vring.avail)) { + if (!vu_is_vq_usable(dev, vq)) { return; } =20 @@ -2862,8 +2865,7 @@ vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz) unsigned int head; VuVirtqElement *elem; =20 - if (unlikely(dev->broken) || - unlikely(!vq->vring.avail)) { + if (!vu_is_vq_usable(dev, vq)) { return NULL; } =20 @@ -3020,8 +3022,7 @@ vu_queue_fill(VuDev *dev, VuVirtq *vq, { struct vring_used_elem uelem; =20 - if (unlikely(dev->broken) || - unlikely(!vq->vring.avail)) { + if (!vu_is_vq_usable(dev, vq)) { return; } =20 @@ -3050,8 +3051,7 @@ vu_queue_flush(VuDev *dev, VuVirtq *vq, unsigned int = count) { uint16_t old, new; =20 - if (unlikely(dev->broken) || - unlikely(!vq->vring.avail)) { + if (!vu_is_vq_usable(dev, vq)) { return; } =20 --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923938; cv=none; d=zohomail.com; s=zohoarc; b=CE8irOD2jfgOS7/abpDoW4JdqZiRORfqoQ80htg2dZXfIgWThZj6Jvbaj94Lqlt0nD5QTh9N2uiIF3ngTyjtunbsq7jNVrHimi+j2KE7dBUti8yFUZo5BEARo2MN9Vo8vKr/oBs0qdxz2AIbtUkfrCuDOas9haDcNde5EEq5rZk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923938; 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=sJvmuYQcr3NZtZIqa/c6lgotLyTeaxZmmLs3MLAIWDU=; b=kK7h/SBA9M/FohNe7AuFQxkYVo6bP2MFK1Ou2gSkxJreizNekwpamPPtlj4Nnpq6aOugZfbVemZZ1ci6/RvOO/gvc1WzJ2lRdHyvOh+oq9TRh5YZftmWnQD9KOw4Gi3OE7UXb+Qv+b7BIqdlMxfYyOQfpKd55sbwZZWnHV/iqJM= 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 1707923938511956.0995502978668; Wed, 14 Feb 2024 07:18:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH1F-00083T-7S; Wed, 14 Feb 2024 10:17:53 -0500 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 1raH1C-00083H-JP for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:50 -0500 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 1raH1A-0000Qr-RA for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:50 -0500 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-330-oZ_ZYVCpNfqOuYaT3ckWcw-1; Wed, 14 Feb 2024 10:17:46 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 66B981C06916; Wed, 14 Feb 2024 15:17:46 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id A05421C066A9; Wed, 14 Feb 2024 15:17:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923868; 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=sJvmuYQcr3NZtZIqa/c6lgotLyTeaxZmmLs3MLAIWDU=; b=Afm0c2eGj7JymFxCdlWmGKAFfUQLNHdyJR1BaxO6AfH46IGpncVsYxmNDS8IeUO8xKmsif TOLs6pxTKUgR1gNXlDkV57yEGCf6ABgAGZReHwkcPZTyp7NFz4Ctq5gTh5ox9OPPqTsGRG WK0mHAlvJ/g9rDIArua+yGac125t+Z4= X-MC-Unique: oZ_ZYVCpNfqOuYaT3ckWcw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 13/14] libvhost-user: Dynamically remap rings after (temporarily?) removing memory regions Date: Wed, 14 Feb 2024 16:17:00 +0100 Message-ID: <20240214151701.29906-14-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923939375100007 Content-Type: text/plain; charset="utf-8" Currently, we try to remap all rings whenever we add a single new memory region. That doesn't quite make sense, because we already map rings when setting the ring address, and panic if that goes wrong. Likely, that handling was simply copied from set_mem_table code, where we actually have to remap all rings. Remapping all rings might require us to walk quite a lot of memory regions to perform the address translations. Ideally, we'd simply remove that remapping. However, let's be a bit careful. There might be some weird corner cases where we might temporarily remove a single memory region (e.g., resize it), that would have worked for now. Further, a ring might be located on hotplugged memory, and as the VM reboots, we might unplug that memory, to hotplug memory before resetting the ring addresses. So let's unmap affected rings as we remove a memory region, and try dynamically mapping the ring again when required. Acked-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 107 ++++++++++++++++------ 1 file changed, 78 insertions(+), 29 deletions(-) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index ed0a978d4f..61fb3050b3 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -283,10 +283,75 @@ vu_remove_all_mem_regs(VuDev *dev) dev->nregions =3D 0; } =20 +static bool +map_ring(VuDev *dev, VuVirtq *vq) +{ + vq->vring.desc =3D qva_to_va(dev, vq->vra.desc_user_addr); + vq->vring.used =3D qva_to_va(dev, vq->vra.used_user_addr); + vq->vring.avail =3D qva_to_va(dev, vq->vra.avail_user_addr); + + DPRINT("Setting virtq addresses:\n"); + DPRINT(" vring_desc at %p\n", vq->vring.desc); + DPRINT(" vring_used at %p\n", vq->vring.used); + DPRINT(" vring_avail at %p\n", vq->vring.avail); + + return !(vq->vring.desc && vq->vring.used && vq->vring.avail); +} + static bool vu_is_vq_usable(VuDev *dev, VuVirtq *vq) { - return likely(!dev->broken) && likely(vq->vring.avail); + if (unlikely(dev->broken)) { + return false; + } + + if (likely(vq->vring.avail)) { + return true; + } + + /* + * In corner cases, we might temporarily remove a memory region that + * mapped a ring. When removing a memory region we make sure to + * unmap any rings that would be impacted. Let's try to remap if we + * already succeeded mapping this ring once. + */ + if (!vq->vra.desc_user_addr || !vq->vra.used_user_addr || + !vq->vra.avail_user_addr) { + return false; + } + if (map_ring(dev, vq)) { + vu_panic(dev, "remapping queue on access"); + return false; + } + return true; +} + +static void +unmap_rings(VuDev *dev, VuDevRegion *r) +{ + int i; + + for (i =3D 0; i < dev->max_queues; i++) { + VuVirtq *vq =3D &dev->vq[i]; + const uintptr_t desc =3D (uintptr_t)vq->vring.desc; + const uintptr_t used =3D (uintptr_t)vq->vring.used; + const uintptr_t avail =3D (uintptr_t)vq->vring.avail; + + if (desc < r->mmap_addr || desc >=3D r->mmap_addr + r->size) { + continue; + } + if (used < r->mmap_addr || used >=3D r->mmap_addr + r->size) { + continue; + } + if (avail < r->mmap_addr || avail >=3D r->mmap_addr + r->size) { + continue; + } + + DPRINT("Unmapping rings of queue %d\n", i); + vq->vring.desc =3D NULL; + vq->vring.used =3D NULL; + vq->vring.avail =3D NULL; + } } =20 static size_t @@ -786,21 +851,6 @@ vu_reset_device_exec(VuDev *dev, VhostUserMsg *vmsg) return false; } =20 -static bool -map_ring(VuDev *dev, VuVirtq *vq) -{ - vq->vring.desc =3D qva_to_va(dev, vq->vra.desc_user_addr); - vq->vring.used =3D qva_to_va(dev, vq->vra.used_user_addr); - vq->vring.avail =3D qva_to_va(dev, vq->vra.avail_user_addr); - - DPRINT("Setting virtq addresses:\n"); - DPRINT(" vring_desc at %p\n", vq->vring.desc); - DPRINT(" vring_used at %p\n", vq->vring.used); - DPRINT(" vring_avail at %p\n", vq->vring.avail); - - return !(vq->vring.desc && vq->vring.used && vq->vring.avail); -} - static bool generate_faults(VuDev *dev) { unsigned int i; @@ -884,7 +934,6 @@ generate_faults(VuDev *dev) { =20 static bool vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { - int i; VhostUserMemoryRegion m =3D vmsg->payload.memreg.region, *msg_region = =3D &m; =20 if (vmsg->fd_num !=3D 1) { @@ -930,19 +979,9 @@ vu_add_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { vmsg->fd_num =3D 0; DPRINT("Successfully added new region in postcopy\n"); return true; - } else { - for (i =3D 0; i < dev->max_queues; i++) { - if (dev->vq[i].vring.desc) { - if (map_ring(dev, &dev->vq[i])) { - vu_panic(dev, "remapping queue %d for new memory regio= n", - i); - } - } - } - - DPRINT("Successfully added new region\n"); - return false; } + DPRINT("Successfully added new region\n"); + return false; } =20 static inline bool reg_equal(VuDevRegion *vudev_reg, @@ -995,6 +1034,16 @@ vu_rem_mem_reg(VuDev *dev, VhostUserMsg *vmsg) { return false; } =20 + /* + * There might be valid cases where we temporarily remove memory regio= ns + * to readd them again, or remove memory regions and don't use the rin= gs + * anymore before we set the ring addresses and restart the device. + * + * Unmap all affected rings, remapping them on demand later. This shou= ld + * be a corner case. + */ + unmap_rings(dev, r); + munmap((void *)(uintptr_t)r->mmap_addr, r->size + r->mmap_offset); =20 idx =3D r - dev->regions; --=20 2.43.0 From nobody Tue Nov 26 11:22:52 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=1707923975; cv=none; d=zohomail.com; s=zohoarc; b=aBbXrPpKweUFrczgroAzfm081ATKWq7Q6LR5XUQ7kNHcn3CTTpm8toz/3WiFmg3fE2BGej/PCvEqZZ2DN+tQpjE5W2/PZh5f7FTvYC3BNkx1FpF1KtVceEQQLjF0oUv+CV3FLbQooRjd+NL7Lp5mH9uYkjJFRTZ69yQubEs++oc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1707923975; 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=TLnNO5ulewL8SdL9pqVb1gTlxm0dqDjONy8Cj8nYHpE=; b=eFXlEFxrAwfFj98TzfehG5hw3BQvyLVNbIk2LDXMz8Br2EvMwkZ77fBPtXje1exqDRgL99apNrGoxnUSu2NDN/EXapnW4hCYAHgEQ4/djoqQ0jK4l24xmiW49ZfdiMtRcfVIa2XzyH3YXNQyGRr4YPgZGRZdRN4Ru3zvDXIXL9o= 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 1707923975264635.987513823412; Wed, 14 Feb 2024 07:19:35 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1raH1L-00084A-3h; Wed, 14 Feb 2024 10:17:59 -0500 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 1raH1J-000842-Q6 for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:57 -0500 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 1raH1G-0000Re-R5 for qemu-devel@nongnu.org; Wed, 14 Feb 2024 10:17:56 -0500 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-257-s5xpLaBYO2yYhnQ0fXVuEQ-1; Wed, 14 Feb 2024 10:17:49 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 473C43812582; Wed, 14 Feb 2024 15:17:49 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.174]) by smtp.corp.redhat.com (Postfix) with ESMTP id ADD2F1C060B1; Wed, 14 Feb 2024 15:17:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707923873; 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=TLnNO5ulewL8SdL9pqVb1gTlxm0dqDjONy8Cj8nYHpE=; b=fEVqVVUvE8GYQ3O7p5sn8TtEAhxnQ0Q1xvldfYOoOExASYkq4GYpqc1mZ1C7MkP7HE6J3h JCNC6xd/E/PJJ9KzmADEl/SiIG7bdtqxmLhA5mYxXXHyb3vdEfww2oTZcfFfvmZRjsTlJG JFBz7wrjNKopzJIlqTkzPBwMBbxbcFk= X-MC-Unique: s5xpLaBYO2yYhnQ0fXVuEQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , "Michael S . Tsirkin" , Jason Wang , Stefan Hajnoczi , Stefano Garzarella , Germano Veit Michel , Raphael Norwitz , Raphael Norwitz Subject: [PATCH v2 14/14] libvhost-user: Mark mmap'ed region memory as MADV_DONTDUMP Date: Wed, 14 Feb 2024 16:17:01 +0100 Message-ID: <20240214151701.29906-15-david@redhat.com> In-Reply-To: <20240214151701.29906-1-david@redhat.com> References: <20240214151701.29906-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.531, 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_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: 1707923977488100005 Content-Type: text/plain; charset="utf-8" We already use MADV_NORESERVE to deal with sparse memory regions. Let's also set madvise(MADV_DONTDUMP), otherwise a crash of the process can result in us allocating all memory in the mmap'ed region for dumping purposes. This change implies that the mmap'ed rings won't be included in a coredump. If ever required for debugging purposes, we could mark only the mapped rings MADV_DODUMP. Ignore errors during madvise() for now. Reviewed-by: Raphael Norwitz Acked-by: Stefano Garzarella Signed-off-by: David Hildenbrand Tested-by: Mario Casquero --- subprojects/libvhost-user/libvhost-user.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index 61fb3050b3..a879149fef 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -460,6 +460,12 @@ _vu_add_mem_reg(VuDev *dev, VhostUserMemoryRegion *msg= _region, int fd) DPRINT(" mmap_addr: 0x%016"PRIx64"\n", (uint64_t)(uintptr_t)mmap_addr); =20 +#if defined(__linux__) + /* Don't include all guest memory in a coredump. */ + madvise(mmap_addr, msg_region->memory_size + mmap_offset, + MADV_DONTDUMP); +#endif + /* Shift all affected entries by 1 to open a hole at idx. */ r =3D &dev->regions[idx]; memmove(r + 1, r, sizeof(VuDevRegion) * (dev->nregions - idx)); --=20 2.43.0