From nobody Wed May 15 09:35:24 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=1695754752; cv=none; d=zohomail.com; s=zohoarc; b=mzEBBoOOC8lGTZGM8R60UbdN+zW4R4wzL+Zdg9xgbk0MNuKEtTsSOnAfGHOgCUKmOw2DA3JczU3NozOfbnrqfyBOUYMy9jkVBKjeRBbSzcvJ0I9fPaY7/6BSHO06vtB2FxEZ7KQwBsjl3X9ZFjupXU0xtDZevyVxxi6AadBEDyk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754752; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=H2lWTPsgDvCfwCJwS9h9vAACqbsIOrrr2brotmfmJqM=; b=I+IIOUH3jllMSLC1mtUZBgaDdMC1biVUtbRr+FIzk6ak7vhQKzq0P1KlJxBG6yELd5cswo5c15/kdwCzbUhvwD29G2H5isospOlVvBI92/vQ+3/09ViqDOAFiBlElK8j95DEPOj/5Ic2Z9EQJkLxLwHKMmMlMCP9Xv4jOepxiXI= 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 1695754752592769.7597620202596; Tue, 26 Sep 2023 11:59:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDGE-0006kv-Qx; Tue, 26 Sep 2023 14:58:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGD-0006jU-Au for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:58:17 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGB-0002ua-1P for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:58:16 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-340-s2AyaTeMM_urvlGdoEpzYA-1; Tue, 26 Sep 2023 14:58:05 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2C0DB85A5BF; Tue, 26 Sep 2023 18:58:05 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8FE2E2026D68; Tue, 26 Sep 2023 18:57:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754694; 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=H2lWTPsgDvCfwCJwS9h9vAACqbsIOrrr2brotmfmJqM=; b=EhxLkiLIJvM5J63auahb8vu9DYd3TO7/S8XtGL4PIL0G34bbU5x959ypRHn84Xef96cJq5 Ld7QaUbA8SfAK8BJoITOz3JMAlSoN3IfNbtIBXpK0RB5jvc/RkijusDDmezkJ4RCDQCXjD 8XV7WLfYj17HRFcseB1ylanIuHBKo+8= X-MC-Unique: s2AyaTeMM_urvlGdoEpzYA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, Tiwei Bie Subject: [PATCH v4 01/18] vhost: Rework memslot filtering and fix "used_memslot" tracking Date: Tue, 26 Sep 2023 20:57:21 +0200 Message-ID: <20230926185738.277351-2-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754754753100013 Content-Type: text/plain; charset="utf-8" Having multiple vhost devices, some filtering out fd-less memslots and some not, can mess up the "used_memslot" accounting. Consequently our "free memslot" checks become unreliable and we might run out of free memslots at runtime later. An example sequence which can trigger a potential issue that involves different vhost backends (vhost-kernel and vhost-user) and hotplugged memory devices can be found at [1]. Let's make the filtering mechanism less generic and distinguish between backends that support private memslots (without a fd) and ones that only support shared memslots (with a fd). Track the used_memslots for both cases separately and use the corresponding value when required. Note: Most probably we should filter out MAP_PRIVATE fd-based RAM regions (for example, via memory-backend-memfd,...,shared=3Doff or as default with memory-backend-file) as well. When not using MAP_SHARED, it might not work as expected. Add a TODO for now. [1] https://lkml.kernel.org/r/fad9136f-08d3-3fd9-71a1-502069c000cf@redhat.c= om Fixes: 988a27754bbb ("vhost: allow backends to filter memory sections") Cc: Tiwei Bie Acked-by: Igor Mammedov Reviewed-by: Peter Xu Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/virtio/vhost-user.c | 7 ++-- hw/virtio/vhost.c | 56 ++++++++++++++++++++++++++----- include/hw/virtio/vhost-backend.h | 5 ++- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 8dcf049d42..1e7553352a 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -2500,10 +2500,9 @@ vhost_user_crypto_close_session(struct vhost_dev *de= v, uint64_t session_id) return 0; } =20 -static bool vhost_user_mem_section_filter(struct vhost_dev *dev, - MemoryRegionSection *section) +static bool vhost_user_no_private_memslots(struct vhost_dev *dev) { - return memory_region_get_fd(section->mr) >=3D 0; + return true; } =20 static int vhost_user_get_inflight_fd(struct vhost_dev *dev, @@ -2746,6 +2745,7 @@ const VhostOps user_ops =3D { .vhost_backend_init =3D vhost_user_backend_init, .vhost_backend_cleanup =3D vhost_user_backend_cleanup, .vhost_backend_memslots_limit =3D vhost_user_memslots_limit, + .vhost_backend_no_private_memslots =3D vhost_user_no_private_memsl= ots, .vhost_set_log_base =3D vhost_user_set_log_base, .vhost_set_mem_table =3D vhost_user_set_mem_table, .vhost_set_vring_addr =3D vhost_user_set_vring_addr, @@ -2772,7 +2772,6 @@ const VhostOps user_ops =3D { .vhost_set_config =3D vhost_user_set_config, .vhost_crypto_create_session =3D vhost_user_crypto_create_session, .vhost_crypto_close_session =3D vhost_user_crypto_close_session, - .vhost_backend_mem_section_filter =3D vhost_user_mem_section_filte= r, .vhost_get_inflight_fd =3D vhost_user_get_inflight_fd, .vhost_set_inflight_fd =3D vhost_user_set_inflight_fd, .vhost_dev_start =3D vhost_user_dev_start, diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index e2f6ffb446..c1e6148833 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -45,20 +45,33 @@ static struct vhost_log *vhost_log; static struct vhost_log *vhost_log_shm; =20 +/* Memslots used by backends that support private memslots (without an fd)= . */ static unsigned int used_memslots; + +/* Memslots used by backends that only support shared memslots (with an fd= ). */ +static unsigned int used_shared_memslots; + static QLIST_HEAD(, vhost_dev) vhost_devices =3D QLIST_HEAD_INITIALIZER(vhost_devices); =20 bool vhost_has_free_slot(void) { - unsigned int slots_limit =3D ~0U; + unsigned int free =3D UINT_MAX; struct vhost_dev *hdev; =20 QLIST_FOREACH(hdev, &vhost_devices, entry) { unsigned int r =3D hdev->vhost_ops->vhost_backend_memslots_limit(h= dev); - slots_limit =3D MIN(slots_limit, r); + unsigned int cur_free; + + if (hdev->vhost_ops->vhost_backend_no_private_memslots && + hdev->vhost_ops->vhost_backend_no_private_memslots(hdev)) { + cur_free =3D r - used_shared_memslots; + } else { + cur_free =3D r - used_memslots; + } + free =3D MIN(free, cur_free); } - return slots_limit > used_memslots; + return free > 0; } =20 static void vhost_dev_sync_region(struct vhost_dev *dev, @@ -474,8 +487,7 @@ static int vhost_verify_ring_mappings(struct vhost_dev = *dev, * vhost_section: identify sections needed for vhost access * * We only care about RAM sections here (where virtqueue and guest - * internals accessed by virtio might live). If we find one we still - * allow the backend to potentially filter it out of our list. + * internals accessed by virtio might live). */ static bool vhost_section(struct vhost_dev *dev, MemoryRegionSection *sect= ion) { @@ -502,8 +514,16 @@ static bool vhost_section(struct vhost_dev *dev, Memor= yRegionSection *section) return false; } =20 - if (dev->vhost_ops->vhost_backend_mem_section_filter && - !dev->vhost_ops->vhost_backend_mem_section_filter(dev, section= )) { + /* + * Some backends (like vhost-user) can only handle memory regions + * that have an fd (can be mapped into a different process). Filter + * the ones without an fd out, if requested. + * + * TODO: we might have to limit to MAP_SHARED as well. + */ + if (memory_region_get_fd(section->mr) < 0 && + dev->vhost_ops->vhost_backend_no_private_memslots && + dev->vhost_ops->vhost_backend_no_private_memslots(dev)) { trace_vhost_reject_section(mr->name, 2); return false; } @@ -568,7 +588,14 @@ static void vhost_commit(MemoryListener *listener) dev->n_mem_sections * sizeof dev->mem->regions[0]; dev->mem =3D g_realloc(dev->mem, regions_size); dev->mem->nregions =3D dev->n_mem_sections; - used_memslots =3D dev->mem->nregions; + + if (dev->vhost_ops->vhost_backend_no_private_memslots && + dev->vhost_ops->vhost_backend_no_private_memslots(dev)) { + used_shared_memslots =3D dev->mem->nregions; + } else { + used_memslots =3D dev->mem->nregions; + } + for (i =3D 0; i < dev->n_mem_sections; i++) { struct vhost_memory_region *cur_vmr =3D dev->mem->regions + i; struct MemoryRegionSection *mrs =3D dev->mem_sections + i; @@ -1400,6 +1427,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaq= ue, VhostBackendType backend_type, uint32_t busyloop_timeou= t, Error **errp) { + unsigned int used; uint64_t features; int i, r, n_initialized_vqs =3D 0; =20 @@ -1495,7 +1523,17 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opa= que, memory_listener_register(&hdev->memory_listener, &address_space_memory= ); QLIST_INSERT_HEAD(&vhost_devices, hdev, entry); =20 - if (used_memslots > hdev->vhost_ops->vhost_backend_memslots_limit(hdev= )) { + /* + * The listener we registered properly updated the corresponding count= er. + * So we can trust that these values are accurate. + */ + if (hdev->vhost_ops->vhost_backend_no_private_memslots && + hdev->vhost_ops->vhost_backend_no_private_memslots(hdev)) { + used =3D used_shared_memslots; + } else { + used =3D used_memslots; + } + if (used > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) { error_setg(errp, "vhost backend memory slots limit is less" " than current number of present memory slots"); r =3D -EINVAL; diff --git a/include/hw/virtio/vhost-backend.h b/include/hw/virtio/vhost-ba= ckend.h index 31a251a9f5..df2821ddae 100644 --- a/include/hw/virtio/vhost-backend.h +++ b/include/hw/virtio/vhost-backend.h @@ -108,8 +108,7 @@ typedef int (*vhost_crypto_create_session_op)(struct vh= ost_dev *dev, typedef int (*vhost_crypto_close_session_op)(struct vhost_dev *dev, uint64_t session_id); =20 -typedef bool (*vhost_backend_mem_section_filter_op)(struct vhost_dev *dev, - MemoryRegionSection *secti= on); +typedef bool (*vhost_backend_no_private_memslots_op)(struct vhost_dev *dev= ); =20 typedef int (*vhost_get_inflight_fd_op)(struct vhost_dev *dev, uint16_t queue_size, @@ -138,6 +137,7 @@ typedef struct VhostOps { vhost_backend_init vhost_backend_init; vhost_backend_cleanup vhost_backend_cleanup; vhost_backend_memslots_limit vhost_backend_memslots_limit; + vhost_backend_no_private_memslots_op vhost_backend_no_private_memslots; vhost_net_set_backend_op vhost_net_set_backend; vhost_net_set_mtu_op vhost_net_set_mtu; vhost_scsi_set_endpoint_op vhost_scsi_set_endpoint; @@ -172,7 +172,6 @@ typedef struct VhostOps { vhost_set_config_op vhost_set_config; vhost_crypto_create_session_op vhost_crypto_create_session; vhost_crypto_close_session_op vhost_crypto_close_session; - vhost_backend_mem_section_filter_op vhost_backend_mem_section_filter; vhost_get_inflight_fd_op vhost_get_inflight_fd; vhost_set_inflight_fd_op vhost_set_inflight_fd; vhost_dev_start_op vhost_dev_start; --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754758; cv=none; d=zohomail.com; s=zohoarc; b=fLbrlWqH4wmC0zet6UfJe+UPE7vvxFnP1mG4BDWE9Hv9fILJtL4xdsObOVpGzx8Na6AqkHzvo/ZAJIOrR1rA57K7rf5oi3rGyoha8PYyGw7Ij3mXfFA4E5br0gvBDL/+zq08pq308ihOHJ+JBd+OKmIVbnWrVhfavV/WTtKHwEA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754758; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5hGg5Jd2AwoQp/M7rl8YRge8IzBVWAeYBCSo0mtTyyc=; b=KPfo8JEqzY7WmpFsyX0ExOubjDCmi9dGZs0Pnb81zDoxEQisW/eJApQIN99rcNq5p2LHswrJXgAIs6wuw7wU+v0R3vlewhird7UjTVpUaDI4gLf7bYiVbmxwMSTguVKmYBu9EO4D3lC+ZHipj6A9i/3mwQoJvCjI1bAAci+oiMM= 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 1695754758551893.2132685357972; Tue, 26 Sep 2023 11:59:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDGM-0006p9-CB; Tue, 26 Sep 2023 14:58:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGJ-0006o0-Df for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:58:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGH-0002vi-Qc for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:58:23 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-140-btqKUQzdPK6FB-vU-IaNHw-1; Tue, 26 Sep 2023 14:58:19 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E5A90800B35; Tue, 26 Sep 2023 18:58:18 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id E9B1B2026D4B; Tue, 26 Sep 2023 18:58:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754701; 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=5hGg5Jd2AwoQp/M7rl8YRge8IzBVWAeYBCSo0mtTyyc=; b=RakYpAvu/Dp3A/8dTwf9SVW+G/NgGktMeKtgVsLRSuFkH9puWG/Ib391HbodYt3gAUmsqL vpw9vkAO1pQpJkZ8hZl1Ulamv4uKYAEfs63q1u8tOzeKQCuf2/DOLDYGQJg+A5k4sa31YR vmuqpJgvhxGnJCTVQjfiP4QgxMVzEPg= X-MC-Unique: btqKUQzdPK6FB-vU-IaNHw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org Subject: [PATCH v4 02/18] vhost: Remove vhost_backend_can_merge() callback Date: Tue, 26 Sep 2023 20:57:22 +0200 Message-ID: <20230926185738.277351-3-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754759481100001 Content-Type: text/plain; charset="utf-8" Checking whether the memory regions are equal is sufficient: if they are equal, then most certainly the contained fd is equal. The whole vhost-user memslot handling is suboptimal and overly complicated. We shouldn't have to lookup a RAM memory regions we got notified about in vhost_user_get_mr_data() using a host pointer. But that requires a bigger rework -- especially an alternative vhost_set_mem_table() backend call that simply consumes MemoryRegionSections. For now, let's just drop vhost_backend_can_merge(). Acked-by: Stefan Hajnoczi Reviewed-by: Igor Mammedov Acked-by: Igor Mammedov Reviewed-by: Peter Xu Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/virtio/vhost-user.c | 14 -------------- hw/virtio/vhost-vdpa.c | 1 - hw/virtio/vhost.c | 6 +----- include/hw/virtio/vhost-backend.h | 4 ---- 4 files changed, 1 insertion(+), 24 deletions(-) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 1e7553352a..e6de930872 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -2205,19 +2205,6 @@ static int vhost_user_migration_done(struct vhost_de= v *dev, char* mac_addr) return -ENOTSUP; } =20 -static bool vhost_user_can_merge(struct vhost_dev *dev, - uint64_t start1, uint64_t size1, - uint64_t start2, uint64_t size2) -{ - ram_addr_t offset; - int mfd, rfd; - - (void)vhost_user_get_mr_data(start1, &offset, &mfd); - (void)vhost_user_get_mr_data(start2, &offset, &rfd); - - return mfd =3D=3D rfd; -} - static int vhost_user_net_set_mtu(struct vhost_dev *dev, uint16_t mtu) { VhostUserMsg msg; @@ -2764,7 +2751,6 @@ const VhostOps user_ops =3D { .vhost_set_vring_enable =3D vhost_user_set_vring_enable, .vhost_requires_shm_log =3D vhost_user_requires_shm_log, .vhost_migration_done =3D vhost_user_migration_done, - .vhost_backend_can_merge =3D vhost_user_can_merge, .vhost_net_set_mtu =3D vhost_user_net_set_mtu, .vhost_set_iotlb_callback =3D vhost_user_set_iotlb_callback, .vhost_send_device_iotlb_msg =3D vhost_user_send_device_iotlb_msg, diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 42f2a4bae9..8f07bee041 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -1508,7 +1508,6 @@ const VhostOps vdpa_ops =3D { .vhost_set_config =3D vhost_vdpa_set_config, .vhost_requires_shm_log =3D NULL, .vhost_migration_done =3D NULL, - .vhost_backend_can_merge =3D NULL, .vhost_net_set_mtu =3D NULL, .vhost_set_iotlb_callback =3D NULL, .vhost_send_device_iotlb_msg =3D NULL, diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index c1e6148833..c16ad14535 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -728,11 +728,7 @@ static void vhost_region_add_section(struct vhost_dev = *dev, size_t offset =3D mrs_gpa - prev_gpa_start; =20 if (prev_host_start + offset =3D=3D mrs_host && - section->mr =3D=3D prev_sec->mr && - (!dev->vhost_ops->vhost_backend_can_merge || - dev->vhost_ops->vhost_backend_can_merge(dev, - mrs_host, mrs_size, - prev_host_start, prev_size))) { + section->mr =3D=3D prev_sec->mr) { uint64_t max_end =3D MAX(prev_host_end, mrs_host + mrs_siz= e); need_add =3D false; prev_sec->offset_within_address_space =3D diff --git a/include/hw/virtio/vhost-backend.h b/include/hw/virtio/vhost-ba= ckend.h index df2821ddae..12d578824b 100644 --- a/include/hw/virtio/vhost-backend.h +++ b/include/hw/virtio/vhost-backend.h @@ -86,9 +86,6 @@ typedef int (*vhost_set_vring_enable_op)(struct vhost_dev= *dev, typedef bool (*vhost_requires_shm_log_op)(struct vhost_dev *dev); typedef int (*vhost_migration_done_op)(struct vhost_dev *dev, char *mac_addr); -typedef bool (*vhost_backend_can_merge_op)(struct vhost_dev *dev, - uint64_t start1, uint64_t size1, - uint64_t start2, uint64_t size2= ); typedef int (*vhost_vsock_set_guest_cid_op)(struct vhost_dev *dev, uint64_t guest_cid); typedef int (*vhost_vsock_set_running_op)(struct vhost_dev *dev, int start= ); @@ -163,7 +160,6 @@ typedef struct VhostOps { vhost_set_vring_enable_op vhost_set_vring_enable; vhost_requires_shm_log_op vhost_requires_shm_log; vhost_migration_done_op vhost_migration_done; - vhost_backend_can_merge_op vhost_backend_can_merge; vhost_vsock_set_guest_cid_op vhost_vsock_set_guest_cid; vhost_vsock_set_running_op vhost_vsock_set_running; vhost_set_iotlb_callback_op vhost_set_iotlb_callback; --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754752; cv=none; d=zohomail.com; s=zohoarc; b=GFFUiWniqIsVfPQD6wTT4g5WNg84Qdxj7+THntYR50JTdrGxZAjzkShQsvyYEyPgtCZZc9umqW7Uz0kqXQltenHha1gYz6EbXsiYcXhlmjswLih6pWCF0X98HF+hIdnEq5W+/ip5IDn2hSm+5YIjbcTq5fLQv6aMI3r7TGtKljc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754752; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Q/KeM+cRvn3GHTg7YjBM909At/Q8zVrNV7vc1/nVhPQ=; b=aCq8dnKxNgqPfKKmDcXSTU9o2kXVdbMfeXOOVo2Z0UYVCzc8A1LRzkRRFU0NdAxC+UqKaaJUbMd6Ds+v+X5732KsB4ObLRN1b/Fe1RqZIw1ktzLI/UUzSob+jOxAA4AohbVuEdqL8Cdd27SpYHVQ/ZeDPegG+JtDWAkGrcySB/o= 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 16957547523035.130769965883701; Tue, 26 Sep 2023 11:59:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDGZ-0006rC-W5; Tue, 26 Sep 2023 14:58:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGX-0006qo-RQ for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:58:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGW-0002xu-AB for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:58:37 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-144-fT9mbBPmMxGc39bmmIY2Vw-1; Tue, 26 Sep 2023 14:58:30 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C1200800962; Tue, 26 Sep 2023 18:58:27 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id B27692026D4B; Tue, 26 Sep 2023 18:58:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754714; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q/KeM+cRvn3GHTg7YjBM909At/Q8zVrNV7vc1/nVhPQ=; b=RnHz7iZ3ft+SxD4b8PrYQkM36znkg9QSboSQpiI/IlVstwKlc3zdboWlsWYPmKRXpLkmKo 7+T1q+zMWIgbjjvOoHiLXTm2//toD4sws+Nar7yqBu/FvGCbsydr3sGE1IOEEcYiaAavOm v71ykGiQIa8GNoP5oGjADK59F266Df4= X-MC-Unique: fT9mbBPmMxGc39bmmIY2Vw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org Subject: [PATCH v4 03/18] softmmu/physmem: Fixup qemu_ram_block_from_host() documentation Date: Tue, 26 Sep 2023 20:57:23 +0200 Message-ID: <20230926185738.277351-4-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754753342100001 Let's fixup the documentation (e.g., removing traces of the ram_addr parameter that no longer exists) and move it to the header file while at it. Suggested-by: Igor Mammedov Acked-by: Igor Mammedov Reviewed-by: Peter Xu Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- include/exec/cpu-common.h | 15 +++++++++++++++ softmmu/physmem.c | 17 ----------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/include/exec/cpu-common.h b/include/exec/cpu-common.h index 41788c0bdd..e29b13dc70 100644 --- a/include/exec/cpu-common.h +++ b/include/exec/cpu-common.h @@ -76,6 +76,21 @@ void qemu_ram_remap(ram_addr_t addr, ram_addr_t length); ram_addr_t qemu_ram_addr_from_host(void *ptr); ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr); RAMBlock *qemu_ram_block_by_name(const char *name); + +/* + * Translates a host ptr back to a RAMBlock and an offset in that RAMBlock. + * + * @ptr: The host pointer to translate. + * @round_offset: Whether to round the result offset down to a target page + * @offset: Will be set to the offset within the returned RAMBlock. + * + * Returns: RAMBlock (or NULL if not found) + * + * By the time this function returns, the returned pointer is not protected + * by RCU anymore. If the caller is not within an RCU critical section and + * does not hold the iothread lock, it must have other means of protecting= the + * pointer, such as a reference to the memory region that owns the RAMBloc= k. + */ RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset, ram_addr_t *offset); ram_addr_t qemu_ram_block_host_offset(RAMBlock *rb, void *host); diff --git a/softmmu/physmem.c b/softmmu/physmem.c index 4f6ca653b3..40105d558d 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -2221,23 +2221,6 @@ ram_addr_t qemu_ram_block_host_offset(RAMBlock *rb, = void *host) return res; } =20 -/* - * Translates a host ptr back to a RAMBlock, a ram_addr and an offset - * in that RAMBlock. - * - * ptr: Host pointer to look up - * round_offset: If true round the result offset down to a page boundary - * *ram_addr: set to result ram_addr - * *offset: set to result offset within the RAMBlock - * - * Returns: RAMBlock (or NULL if not found) - * - * By the time this function returns, the returned pointer is not protected - * by RCU anymore. If the caller is not within an RCU critical section and - * does not hold the iothread lock, it must have other means of protecting= the - * pointer, such as a reference to the region that includes the incoming - * ram_addr_t. - */ RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset, ram_addr_t *offset) { --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754752; cv=none; d=zohomail.com; s=zohoarc; b=jEScqW68uI1NRQp8qnM/zQaBoapJ3VYrzOGEgVH6VwQkF2xkTNallWw0jkQg6F2hRpczbH/RWoBoJpKMxkOqjgs1tts+8rM58uHSxWP7Db0VYIPropn6CuidfnX5SWAS88YYTZKl3JmOUhr8iUKnH4s695AMBaHCw9Za+ip+fJY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754752; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iQv88OFvb8fJICC0aQ4EZvXR0U7eWR19gDzG5bZkaE4=; b=IS8wcPu12e2FfomM28KlNgPPSmyRG1nX752dorNA+ehc2WcEdLNnJsmZon60FanaqlBN+/KC1P6Yv5Au5+u6ywSZu9ox6DFbwCV/bg1bUSs4KpYL4Oz+5ZFynzCEjN3KIfwRiR/Yf6zab1kTtpQAoC/T+jXdedBYoQgpHbge0zg= 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 1695754752885865.799591215444; Tue, 26 Sep 2023 11:59:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDGg-0006rl-5Y; Tue, 26 Sep 2023 14:58:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGe-0006rV-Kt for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:58:44 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGc-0002zW-Ew for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:58:44 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-92-PLCiT9W-MzSNlkc2dFf4TQ-1; Tue, 26 Sep 2023 14:58:38 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AF30F3813F2E; Tue, 26 Sep 2023 18:58:37 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7DB292026D76; Tue, 26 Sep 2023 18:58:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754721; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iQv88OFvb8fJICC0aQ4EZvXR0U7eWR19gDzG5bZkaE4=; b=HZ/IJYu80tHGyka37IzS8HWGdjENTL7/RsI1UIWUIgZ3/tDLBsc6NXFxyuYbNhuhMxM/SO 8rBYQJ0vmuyGDRJcLtHVcFXNjnd78L1/Ls2ARgpquYDB+teiQ6bsZ8ZIBF1RwloRJ0QTEA HaypMFwNu+M3HD/UsqaW8c8TZRK3fac= X-MC-Unique: PLCiT9W-MzSNlkc2dFf4TQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, "Maciej S . Szmigiero" Subject: [PATCH v4 04/18] kvm: Return number of free memslots Date: Tue, 26 Sep 2023 20:57:24 +0200 Message-ID: <20230926185738.277351-5-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: 1695754753495100005 Let's return the number of free slots instead of only checking if there is a free slot. While at it, check all address spaces, which will also consider SMM under x86 correctly. This is a preparation for memory devices that consume multiple memslots. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Maciej S. Szmigiero Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- accel/kvm/kvm-all.c | 33 ++++++++++++++++++++------------- accel/stubs/kvm-stub.c | 4 ++-- hw/mem/memory-device.c | 2 +- include/sysemu/kvm.h | 2 +- include/sysemu/kvm_int.h | 1 + 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index ff1578bb32..9d4c5a4c51 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -181,6 +181,24 @@ int kvm_get_max_memslots(void) return s->nr_slots; } =20 +unsigned int kvm_get_free_memslots(void) +{ + unsigned int used_slots =3D 0; + KVMState *s =3D kvm_state; + int i; + + kvm_slots_lock(); + for (i =3D 0; i < s->nr_as; i++) { + if (!s->as[i].ml) { + continue; + } + used_slots =3D MAX(used_slots, s->as[i].ml->nr_used_slots); + } + kvm_slots_unlock(); + + return s->nr_slots - used_slots; +} + /* Called with KVMMemoryListener.slots_lock held */ static KVMSlot *kvm_get_free_slot(KVMMemoryListener *kml) { @@ -196,19 +214,6 @@ static KVMSlot *kvm_get_free_slot(KVMMemoryListener *k= ml) return NULL; } =20 -bool kvm_has_free_slot(MachineState *ms) -{ - KVMState *s =3D KVM_STATE(ms->accelerator); - bool result; - KVMMemoryListener *kml =3D &s->memory_listener; - - kvm_slots_lock(); - result =3D !!kvm_get_free_slot(kml); - kvm_slots_unlock(); - - return result; -} - /* Called with KVMMemoryListener.slots_lock held */ static KVMSlot *kvm_alloc_slot(KVMMemoryListener *kml) { @@ -1387,6 +1392,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, } start_addr +=3D slot_size; size -=3D slot_size; + kml->nr_used_slots--; } while (size); return; } @@ -1412,6 +1418,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, ram_start_offset +=3D slot_size; ram +=3D slot_size; size -=3D slot_size; + kml->nr_used_slots++; } while (size); } =20 diff --git a/accel/stubs/kvm-stub.c b/accel/stubs/kvm-stub.c index 235dc661bc..a5d4442d8f 100644 --- a/accel/stubs/kvm-stub.c +++ b/accel/stubs/kvm-stub.c @@ -109,9 +109,9 @@ int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s, = EventNotifier *n, return -ENOSYS; } =20 -bool kvm_has_free_slot(MachineState *ms) +unsigned int kvm_get_free_memslots(void) { - return false; + return 0; } =20 void kvm_init_cpu_signals(CPUState *cpu) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 667d56bd29..98e355c960 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -59,7 +59,7 @@ static void memory_device_check_addable(MachineState *ms,= MemoryRegion *mr, const uint64_t size =3D memory_region_size(mr); =20 /* we will need a new memory slot for kvm and vhost */ - if (kvm_enabled() && !kvm_has_free_slot(ms)) { + if (kvm_enabled() && !kvm_get_free_memslots()) { error_setg(errp, "hypervisor has no free memory slots left"); return; } diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index ee9025f8e9..c3d831baef 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -215,7 +215,7 @@ typedef struct KVMRouteChange { =20 /* external API */ =20 -bool kvm_has_free_slot(MachineState *ms); +unsigned int kvm_get_free_memslots(void); bool kvm_has_sync_mmu(void); int kvm_has_vcpu_events(void); int kvm_has_robust_singlestep(void); diff --git a/include/sysemu/kvm_int.h b/include/sysemu/kvm_int.h index a5b9122cb8..075939a3c4 100644 --- a/include/sysemu/kvm_int.h +++ b/include/sysemu/kvm_int.h @@ -40,6 +40,7 @@ typedef struct KVMMemoryUpdate { typedef struct KVMMemoryListener { MemoryListener listener; KVMSlot *slots; + unsigned int nr_used_slots; int as_id; QSIMPLEQ_HEAD(, KVMMemoryUpdate) transaction_add; QSIMPLEQ_HEAD(, KVMMemoryUpdate) transaction_del; --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754765; cv=none; d=zohomail.com; s=zohoarc; b=eGDfcznbbiPQBuwDsQnwodBXXyGht/PSQz56sIyfrNkVeWNSHf6w++4lm/g8C9qN3HTu8uRd7qAvodQJMOlItlb7wFpCfZ8udP9j9yaatqUw+mfTQh6IXH6Ky/2OzNGcJG/0QBSwP8WEv9dxA6PPa1vkVFJbh2I1h5jAWaglb/A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754765; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Rwy8XG8etvevYx4+uyycYPxkPB6mvZoERehXXpRCdCg=; b=Wzw5oVX3ETGT/GTdWpQTSnE8vVGwpSGgs4ii2Q3KIA7Hr9NLTIhkDmODnz2ramWvNzvBSTsanBRtIRvrOd5PZuE0Vw5fVhyJD/KyTqdt6NifAk7g7GKk/T7+WhQIWbzpizwgbajiA8FWTcR4OlyqYAq9lAVIU4WqLsTSM3jK7IM= 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 1695754765101410.67788777436795; Tue, 26 Sep 2023 11:59:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDGo-00071I-Pu; Tue, 26 Sep 2023 14:58:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGm-00070l-CC for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:58:53 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGk-000328-UR for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:58:52 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-257-mwRYvnN1NQ-DqiTJUuZQ_A-1; Tue, 26 Sep 2023 14:58:47 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7DD852815E28; Tue, 26 Sep 2023 18:58:46 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 71D102026D4B; Tue, 26 Sep 2023 18:58:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754730; 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=Rwy8XG8etvevYx4+uyycYPxkPB6mvZoERehXXpRCdCg=; b=cWDt9JaPNux2BT6Zn8QiM/0RWlFiDR5WrhFYkCRFGQQ0ylR79j6PWVxbg6CMDIuAXogxLh sGk4K3tlvKqAjhwVxgRMppKwXGW2oI2ICfG4cbkdalUb1pYm2LLfVDSB/z2l5MC3cnwL9/ dB2S4f1t4cJZEhYsSZrSpAfVzC7s3OM= X-MC-Unique: mwRYvnN1NQ-DqiTJUuZQ_A-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, "Maciej S . Szmigiero" Subject: [PATCH v4 05/18] vhost: Return number of free memslots Date: Tue, 26 Sep 2023 20:57:25 +0200 Message-ID: <20230926185738.277351-6-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: 1695754766058100001 Content-Type: text/plain; charset="utf-8" Let's return the number of free slots instead of only checking if there is a free slot. Required to support memory devices that consume multiple memslots. This is a preparation for memory devices that consume multiple memslots. Reviewed-by: Maciej S. Szmigiero Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/mem/memory-device.c | 2 +- hw/virtio/vhost-stub.c | 4 ++-- hw/virtio/vhost.c | 4 ++-- include/hw/virtio/vhost.h | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 98e355c960..e09960744d 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -63,7 +63,7 @@ static void memory_device_check_addable(MachineState *ms,= MemoryRegion *mr, error_setg(errp, "hypervisor has no free memory slots left"); return; } - if (!vhost_has_free_slot()) { + if (!vhost_get_free_memslots()) { error_setg(errp, "a used vhost backend has no free memory slots le= ft"); return; } diff --git a/hw/virtio/vhost-stub.c b/hw/virtio/vhost-stub.c index aa858ef3fb..d53dd9d288 100644 --- a/hw/virtio/vhost-stub.c +++ b/hw/virtio/vhost-stub.c @@ -2,9 +2,9 @@ #include "hw/virtio/vhost.h" #include "hw/virtio/vhost-user.h" =20 -bool vhost_has_free_slot(void) +unsigned int vhost_get_free_memslots(void) { - return true; + return UINT_MAX; } =20 bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index c16ad14535..8e84dca246 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -54,7 +54,7 @@ static unsigned int used_shared_memslots; static QLIST_HEAD(, vhost_dev) vhost_devices =3D QLIST_HEAD_INITIALIZER(vhost_devices); =20 -bool vhost_has_free_slot(void) +unsigned int vhost_get_free_memslots(void) { unsigned int free =3D UINT_MAX; struct vhost_dev *hdev; @@ -71,7 +71,7 @@ bool vhost_has_free_slot(void) } free =3D MIN(free, cur_free); } - return free > 0; + return free; } =20 static void vhost_dev_sync_region(struct vhost_dev *dev, diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index 6a173cb9fa..603bf834be 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -315,7 +315,7 @@ uint64_t vhost_get_features(struct vhost_dev *hdev, con= st int *feature_bits, */ void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, uint64_t features); -bool vhost_has_free_slot(void); +unsigned int vhost_get_free_memslots(void); =20 int vhost_net_set_backend(struct vhost_dev *hdev, struct vhost_vring_file *file); --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754894; cv=none; d=zohomail.com; s=zohoarc; b=e2PofaVcu92yKTIMmEzdTTeAMTqvCG69xkRGCxSewgpQKnqsNjv6mq3GJ8eGvVdWkw23TIcxVUmScNu0suaq14xa4ND7KhkYmBHmhgGdr1K53HOFVUsu+NGthFZI1TJEtK1FPTOgQYoCjZ0szwRfI5FCH8dG+437mEV/qRhphTg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754894; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=X6FoTWZmAFi9ZDQpW8tsN1nzgSbk2tEh+9iM6CbpDp4=; b=J71vrd6ZHe3XZY1FrgzzT3zwg5tlqA/7O5PgoI48E0AbfHPGsR1zyFIynMQfwSXYRcthuIQT2/m9gl35998fC2KLaFKRwN7v7DGPGu/mY+iuaX28rLj3/FKFCony/MvKS8H8NuQAoW3NNAdLuc6nzi9uAmSWoB+4Wv3bmNIYxMA= 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 1695754894789950.6727698851931; Tue, 26 Sep 2023 12:01:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDH0-000755-71; Tue, 26 Sep 2023 14:59:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGw-00074Q-H9 for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:02 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDGt-00033d-JE for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:01 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-48-XkJhC17kOYS5HH0CKa08Yg-1; Tue, 26 Sep 2023 14:58:55 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D88EB101A529; Tue, 26 Sep 2023 18:58:54 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3264C2026D4B; Tue, 26 Sep 2023 18:58:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754739; 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=X6FoTWZmAFi9ZDQpW8tsN1nzgSbk2tEh+9iM6CbpDp4=; b=Av5T+1Y67dV43pG50FztodfKl8dQB4TT/dOrvCpLTsWdyHE+OWaTE7Bv+MO20QLX/Dbyvt w+FB+WrrnBY7yxEQyJVOXDBSEuPtYv6Pbpul8A9fnTfqD9I5Oqif42jMqpB2F2cSAulvwb K3JGAfsJVq+PU1N5YiSWWkJTNygvRjI= X-MC-Unique: XkJhC17kOYS5HH0CKa08Yg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, "Maciej S . Szmigiero" Subject: [PATCH v4 06/18] memory-device: Support memory devices with multiple memslots Date: Tue, 26 Sep 2023 20:57:26 +0200 Message-ID: <20230926185738.277351-7-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754896065100001 Content-Type: text/plain; charset="utf-8" We want to support memory devices that have a memory region container as device memory region that maps multiple RAM memory regions. Let's start by supporting memory devices that statically map multiple RAM memory regions and, thereby, consume multiple memslots. We already have one device that uses a container as device memory region: NVDIMMs. However, a NVDIMM always ends up consuming exactly one memslot. Let's add support for that by asking the memory device via a new callback how many memslots it requires. Reviewed-by: Maciej S. Szmigiero Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/mem/memory-device.c | 27 +++++++++++++++++++-------- include/hw/mem/memory-device.h | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index e09960744d..0eec0872a9 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -52,19 +52,30 @@ static int memory_device_build_list(Object *obj, void *= opaque) return 0; } =20 -static void memory_device_check_addable(MachineState *ms, MemoryRegion *mr, - Error **errp) +static unsigned int memory_device_get_memslots(MemoryDeviceState *md) +{ + const MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(md); + + if (mdc->get_memslots) { + return mdc->get_memslots(md); + } + return 1; +} + +static void memory_device_check_addable(MachineState *ms, MemoryDeviceStat= e *md, + MemoryRegion *mr, Error **errp) { const uint64_t used_region_size =3D ms->device_memory->used_region_siz= e; const uint64_t size =3D memory_region_size(mr); + const unsigned int required_memslots =3D memory_device_get_memslots(md= ); =20 - /* we will need a new memory slot for kvm and vhost */ - if (kvm_enabled() && !kvm_get_free_memslots()) { - error_setg(errp, "hypervisor has no free memory slots left"); + /* we will need memory slots for kvm and vhost */ + if (kvm_enabled() && kvm_get_free_memslots() < required_memslots) { + error_setg(errp, "hypervisor has not enough free memory slots left= "); return; } - if (!vhost_get_free_memslots()) { - error_setg(errp, "a used vhost backend has no free memory slots le= ft"); + if (vhost_get_free_memslots() < required_memslots) { + error_setg(errp, "a used vhost backend has not enough free memory = slots left"); return; } =20 @@ -233,7 +244,7 @@ void memory_device_pre_plug(MemoryDeviceState *md, Mach= ineState *ms, goto out; } =20 - memory_device_check_addable(ms, mr, &local_err); + memory_device_check_addable(ms, md, mr, &local_err); if (local_err) { goto out; } diff --git a/include/hw/mem/memory-device.h b/include/hw/mem/memory-device.h index 48d2611fc5..b51a579fb9 100644 --- a/include/hw/mem/memory-device.h +++ b/include/hw/mem/memory-device.h @@ -41,6 +41,11 @@ typedef struct MemoryDeviceState MemoryDeviceState; * successive memory regions are used, a covering memory region has to * be provided. Scattered memory regions are not supported for single * devices. + * + * The device memory region returned via @get_memory_region may either be a + * single RAM memory region or a memory region container with subregions + * that are RAM memory regions or aliases to RAM memory regions. Other + * memory regions or subregions are not supported. */ struct MemoryDeviceClass { /* private */ @@ -88,6 +93,19 @@ struct MemoryDeviceClass { */ MemoryRegion *(*get_memory_region)(MemoryDeviceState *md, Error **errp= ); =20 + /* + * Optional for memory devices that require only a single memslot, + * required for all other memory devices: Return the number of memslots + * (distinct RAM memory regions in the device memory region) that are + * required by the device. + * + * If this function is not implemented, the assumption is "1". + * + * Called when (un)plugging the memory device, to check if the require= ments + * can be satisfied, and to do proper accounting. + */ + unsigned int (*get_memslots)(MemoryDeviceState *md); + /* * Optional: Return the desired minimum alignment of the device in gue= st * physical address space. The final alignment is computed based on th= is --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754802; cv=none; d=zohomail.com; s=zohoarc; b=f0c0YoV2FnBkf+nMSICF3mS3O8vLGBlS7GGqh4uf1Jzk9pcAfdIVp6NrgWnaiLFQsR7g1SUO32CrCDiOAX/pznFIUmvhhmv6j4K0OOB30jbwRNM+MqC6W7R9lOcH//R1Xs5Geymz3DRsnsxZuj72yuLdq/oM5y5M0S8B0IwBYbE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754802; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=h27AAOIFaWEN2eZjYKOHkf0H+CWsHEPdCLVcRFclZv0=; b=N1mNpLuask+a1mst+/FnlWzW1RJ5DuaYQNaQ1S24Xl2x+8M2WtpTzYD4U58iy+sgJPfLKu8twzUNE4tLTUoLxoKmr9T7rVCt1EmuZVeqA6lE+fB37ejLbO3e1VnIJDEGI7kjgWj/udQbK+t84tYhHaCKVGXpPzxBvLSnxlMUL4M= 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 1695754802262634.0744743142579; Tue, 26 Sep 2023 12:00:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDH9-0007tF-DR; Tue, 26 Sep 2023 14:59:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDH6-0007W8-RN for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDH4-00036c-4s for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:12 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-240-0GLu2QzMNJKHAUDp7SwDWQ-1; Tue, 26 Sep 2023 14:59:03 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8261B801779; Tue, 26 Sep 2023 18:59:02 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9AD432026D4B; Tue, 26 Sep 2023 18:58:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754749; 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=h27AAOIFaWEN2eZjYKOHkf0H+CWsHEPdCLVcRFclZv0=; b=dk9q74kZRiAP1qRZ4KFdc9d4rtV0WTVFbf676hvMKKCpnRQw9f04+/vmHRv0VoHtbzEKvr TDKb3W6xQM6PxbdZhgBUQamhP/k83wHUZKwtNO8YbzCWOvoWWnQ1vRwZdvurS+WmkFGzUU U82zlZXVhiQok1TmD9nd0SzPBdakJfI= X-MC-Unique: 0GLu2QzMNJKHAUDp7SwDWQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, "Maciej S . Szmigiero" Subject: [PATCH v4 07/18] stubs: Rename qmp_memory_device.c to memory_device.c Date: Tue, 26 Sep 2023 20:57:27 +0200 Message-ID: <20230926185738.277351-8-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754804235100002 Content-Type: text/plain; charset="utf-8" We want to place non-qmp stubs in there, so let's rename it. While at it, put it into the MAINTAINERS file under "Memory devices". Reviewed-by: Maciej S. Szmigiero Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- MAINTAINERS | 1 + stubs/{qmp_memory_device.c =3D> memory_device.c} | 0 stubs/meson.build | 2 +- 3 files changed, 2 insertions(+), 1 deletion(-) rename stubs/{qmp_memory_device.c =3D> memory_device.c} (100%) diff --git a/MAINTAINERS b/MAINTAINERS index 355b1960ce..21e5f9426f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2863,6 +2863,7 @@ F: hw/mem/pc-dimm.c F: include/hw/mem/memory-device.h F: include/hw/mem/nvdimm.h F: include/hw/mem/pc-dimm.h +F: stubs/memory_device.c F: docs/nvdimm.txt =20 SPICE diff --git a/stubs/qmp_memory_device.c b/stubs/memory_device.c similarity index 100% rename from stubs/qmp_memory_device.c rename to stubs/memory_device.c diff --git a/stubs/meson.build b/stubs/meson.build index ef6e39a64d..cde44972bf 100644 --- a/stubs/meson.build +++ b/stubs/meson.build @@ -32,7 +32,7 @@ stub_ss.add(files('monitor.c')) stub_ss.add(files('monitor-core.c')) stub_ss.add(files('physmem.c')) stub_ss.add(files('qemu-timer-notify-cb.c')) -stub_ss.add(files('qmp_memory_device.c')) +stub_ss.add(files('memory_device.c')) stub_ss.add(files('qmp-command-available.c')) stub_ss.add(files('qmp-quit.c')) stub_ss.add(files('qtest.c')) --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754770; cv=none; d=zohomail.com; s=zohoarc; b=JUFUv0pj1jnbaY5xi+3Cl+4Msi3ilCxlB1fv+ZbGLbWCaX63H+ctoaSKHJPwMAOPoUmnSNwqD+ocnijo0sJ1S4OY34ZsctfITSikE7DUvB3FwC9ry5rExrl03g81XcYfpiM4x6R9mx1yXYJatai8U3QJ9DZlGFWh30Q3Yse4ViY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754770; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5slP1Mq6ReelzapelY7FkkpRO49e4JIwaD4dFcmfqHc=; b=HGPFWDLwul4+gB1nqPAxpmtBttExTTjM23ePpmyDLYzGae6E8kEkiOrVRIkTjLk9Qvxi/8Xo3mf6gSmXoDDEajHlGzjTTLKDYpeKpSlYUZTVED7kHIrcRx6aCyxqJh7764VrzIor1ryyzGAjEQG0uVG/EGIALYoNSmPPYDpxn+4= 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 1695754770706113.34281568805477; Tue, 26 Sep 2023 11:59:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDHC-0008EN-Tk; Tue, 26 Sep 2023 14:59:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHA-000847-Es for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:16 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDH8-00037a-Gm for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:16 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-179-feTK4n4PNCexBE0aSqjznw-1; Tue, 26 Sep 2023 14:59:10 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 828FA8039C1; Tue, 26 Sep 2023 18:59:09 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1F2D42026D4B; Tue, 26 Sep 2023 18:59:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754753; 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=5slP1Mq6ReelzapelY7FkkpRO49e4JIwaD4dFcmfqHc=; b=JVgwhgvB4CHKFbrMKsapCop2+K7q1Ybqjw9Hske76XYe3QJpOixAnzgJMhDUVoRz5fIuQO HUhuTaWXPJZZvWEQP3HELuD6iOgRtya4MdS1fsWWa8Is43RHmLFGqjh/NkqyTE0DKyxl5N YJgSDhxoEtR8tHpg4MJjadoF79eU4ag= X-MC-Unique: feTK4n4PNCexBE0aSqjznw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, "Maciej S . Szmigiero" Subject: [PATCH v4 08/18] memory-device: Track required and actually used memslots in DeviceMemoryState Date: Tue, 26 Sep 2023 20:57:28 +0200 Message-ID: <20230926185738.277351-9-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754771778100003 Content-Type: text/plain; charset="utf-8" Let's track how many memslots are required by plugged memory devices and how many are currently actually getting used by plugged memory devices. "required - used" is the number of reserved memslots. For now, the number of used and required memslots is always equal, and there are no reservations. This is a preparation for memory devices that want to dynamically consume memslots after initially specifying how many they require -- where we'll end up with reserved memslots. To track the number of used memslots, create a new address space for our device memory and register a memory listener (add/remove) for that address space. Reviewed-by: Maciej S. Szmigiero Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/mem/memory-device.c | 54 ++++++++++++++++++++++++++++++++++++++++++ include/hw/boards.h | 10 +++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 0eec0872a9..d37cfbd65d 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -286,6 +286,7 @@ void memory_device_plug(MemoryDeviceState *md, MachineS= tate *ms) g_assert(ms->device_memory); =20 ms->device_memory->used_region_size +=3D memory_region_size(mr); + ms->device_memory->required_memslots +=3D memory_device_get_memslots(m= d); memory_region_add_subregion(&ms->device_memory->mr, addr - ms->device_memory->base, mr); trace_memory_device_plug(DEVICE(md)->id ? DEVICE(md)->id : "", addr); @@ -305,6 +306,7 @@ void memory_device_unplug(MemoryDeviceState *md, Machin= eState *ms) =20 memory_region_del_subregion(&ms->device_memory->mr, mr); ms->device_memory->used_region_size -=3D memory_region_size(mr); + ms->device_memory->required_memslots -=3D memory_device_get_memslots(m= d); trace_memory_device_unplug(DEVICE(md)->id ? DEVICE(md)->id : "", mdc->get_addr(md)); } @@ -324,6 +326,50 @@ uint64_t memory_device_get_region_size(const MemoryDev= iceState *md, return memory_region_size(mr); } =20 +static void memory_devices_region_mod(MemoryListener *listener, + MemoryRegionSection *mrs, bool add) +{ + DeviceMemoryState *dms =3D container_of(listener, DeviceMemoryState, + listener); + + if (!memory_region_is_ram(mrs->mr)) { + warn_report("Unexpected memory region mapped into device memory re= gion."); + return; + } + + /* + * The expectation is that each distinct RAM memory region section in + * our region for memory devices consumes exactly one memslot in KVM + * and in vhost. For vhost, this is true, except: + * * ROM memory regions don't consume a memslot. These get used very + * rarely for memory devices (R/O NVDIMMs). + * * Memslots without a fd (memory-backend-ram) don't necessarily + * consume a memslot. Such setups are quite rare and possibly bogus: + * the memory would be inaccessible by such vhost devices. + * + * So for vhost, in corner cases we might over-estimate the number of + * memslots that are currently used or that might still be reserved + * (required - used). + */ + dms->used_memslots +=3D add ? 1 : -1; + + if (dms->used_memslots > dms->required_memslots) { + warn_report("Memory devices use more memory slots than indicated a= s required."); + } +} + +static void memory_devices_region_add(MemoryListener *listener, + MemoryRegionSection *mrs) +{ + return memory_devices_region_mod(listener, mrs, true); +} + +static void memory_devices_region_del(MemoryListener *listener, + MemoryRegionSection *mrs) +{ + return memory_devices_region_mod(listener, mrs, false); +} + void machine_memory_devices_init(MachineState *ms, hwaddr base, uint64_t s= ize) { g_assert(size); @@ -333,8 +379,16 @@ void machine_memory_devices_init(MachineState *ms, hwa= ddr base, uint64_t size) =20 memory_region_init(&ms->device_memory->mr, OBJECT(ms), "device-memory", size); + address_space_init(&ms->device_memory->as, &ms->device_memory->mr, + "device-memory"); memory_region_add_subregion(get_system_memory(), ms->device_memory->ba= se, &ms->device_memory->mr); + + /* Track the number of memslots used by memory devices. */ + ms->device_memory->listener.region_add =3D memory_devices_region_add; + ms->device_memory->listener.region_del =3D memory_devices_region_del; + memory_listener_register(&ms->device_memory->listener, + &ms->device_memory->as); } =20 static const TypeInfo memory_device_info =3D { diff --git a/include/hw/boards.h b/include/hw/boards.h index 6c67af196a..5a9d72289c 100644 --- a/include/hw/boards.h +++ b/include/hw/boards.h @@ -296,15 +296,23 @@ struct MachineClass { * DeviceMemoryState: * @base: address in guest physical address space where the memory * address space for memory devices starts - * @mr: address space container for memory devices + * @mr: memory region container for memory devices + * @as: address space for memory devices + * @listener: memory listener used to track used memslots in the address s= pace * @dimm_size: the sum of plugged DIMMs' sizes * @used_region_size: the part of @mr already used by memory devices + * @required_memslots: the number of memslots required by memory devices + * @used_memslots: the number of memslots currently used by memory devices */ typedef struct DeviceMemoryState { hwaddr base; MemoryRegion mr; + AddressSpace as; + MemoryListener listener; uint64_t dimm_size; uint64_t used_region_size; + unsigned int required_memslots; + unsigned int used_memslots; } DeviceMemoryState; =20 /** --=20 2.41.0 From nobody Wed May 15 09:35:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1695754891625256.3517665337007; Tue, 26 Sep 2023 12:01:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDHI-0000Ia-UY; Tue, 26 Sep 2023 14:59:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHG-0000Dm-TS for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHE-00039D-TI for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:22 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-642-kL5N-bImMhus8JdUVGCtYg-1; Tue, 26 Sep 2023 14:59:16 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0B08C805B29; Tue, 26 Sep 2023 18:59:16 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C8382026D4B; Tue, 26 Sep 2023 18:59:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754760; 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=kZcRdwyNP50tSHDs9hyBXUz08rwwVIv8MNAI/C060fQ=; b=DO81J8Y/fFpz3S8w1U/STlM9PwCZer9olhgLT1tOwdF5JGQggpgCocHO0iG61IJ8m8mcwT Nce/S6fPWnL0AsUtOZrPiE+nWm9mGRnelAv1e6jShNYvgfoMbETnukFlWfaeGw59wMIC0w QsFbn9Q+4PEdo2rYrfvde7g4as8Z9uQ= X-MC-Unique: kL5N-bImMhus8JdUVGCtYg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, "Maciej S . Szmigiero" Subject: [PATCH v4 09/18] memory-device, vhost: Support memory devices that dynamically consume memslots Date: Tue, 26 Sep 2023 20:57:29 +0200 Message-ID: <20230926185738.277351-10-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1695754891882100001 Content-Type: text/plain; charset="utf-8" We want to support memory devices that have a dynamically managed memory region container as device memory region. This device memory region maps multiple RAM memory subregions (e.g., aliases to the same RAM memory region), whereby these subregions can be (un)mapped on demand. Each RAM subregion will consume a memslot in KVM and vhost, resulting in such a new device consuming memslots dynamically, and initially usually 0. We already track the number of used vs. required memslots for all memslots. From that, we can derive the number of reserved memslots that must not be used otherwise. The target use case is virtio-mem and the hyper-v balloon, which will dynamically map aliases to RAM memory region into their device memory region container. Properly document what's supported and what's not and extend the vhost memslot check accordingly. Reviewed-by: Maciej S. Szmigiero Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/mem/memory-device.c | 29 +++++++++++++++++++++++++++-- hw/virtio/vhost.c | 18 ++++++++++++++---- include/hw/mem/memory-device.h | 7 +++++++ stubs/memory_device.c | 5 +++++ 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index d37cfbd65d..1b14ba5661 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -62,19 +62,44 @@ static unsigned int memory_device_get_memslots(MemoryDe= viceState *md) return 1; } =20 +/* + * Memslots that are reserved by memory devices (required but still report= ed + * as free from KVM / vhost). + */ +static unsigned int get_reserved_memslots(MachineState *ms) +{ + if (ms->device_memory->used_memslots > + ms->device_memory->required_memslots) { + /* This is unexpected, and we warned already in the memory notifie= r. */ + return 0; + } + return ms->device_memory->required_memslots - + ms->device_memory->used_memslots; +} + +unsigned int memory_devices_get_reserved_memslots(void) +{ + if (!current_machine->device_memory) { + return 0; + } + return get_reserved_memslots(current_machine); +} + static void memory_device_check_addable(MachineState *ms, MemoryDeviceStat= e *md, MemoryRegion *mr, Error **errp) { const uint64_t used_region_size =3D ms->device_memory->used_region_siz= e; const uint64_t size =3D memory_region_size(mr); const unsigned int required_memslots =3D memory_device_get_memslots(md= ); + const unsigned int reserved_memslots =3D get_reserved_memslots(ms); =20 /* we will need memory slots for kvm and vhost */ - if (kvm_enabled() && kvm_get_free_memslots() < required_memslots) { + if (kvm_enabled() && + kvm_get_free_memslots() < required_memslots + reserved_memslots) { error_setg(errp, "hypervisor has not enough free memory slots left= "); return; } - if (vhost_get_free_memslots() < required_memslots) { + if (vhost_get_free_memslots() < required_memslots + reserved_memslots)= { error_setg(errp, "a used vhost backend has not enough free memory = slots left"); return; } diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 8e84dca246..f7e1ac12a8 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -23,6 +23,7 @@ #include "qemu/log.h" #include "standard-headers/linux/vhost_types.h" #include "hw/virtio/virtio-bus.h" +#include "hw/mem/memory-device.h" #include "migration/blocker.h" #include "migration/qemu-file-types.h" #include "sysemu/dma.h" @@ -1423,7 +1424,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaq= ue, VhostBackendType backend_type, uint32_t busyloop_timeou= t, Error **errp) { - unsigned int used; + unsigned int used, reserved, limit; uint64_t features; int i, r, n_initialized_vqs =3D 0; =20 @@ -1529,9 +1530,18 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opa= que, } else { used =3D used_memslots; } - if (used > hdev->vhost_ops->vhost_backend_memslots_limit(hdev)) { - error_setg(errp, "vhost backend memory slots limit is less" - " than current number of present memory slots"); + /* + * We assume that all reserved memslots actually require a real memslot + * in our vhost backend. This might not be true, for example, if the + * memslot would be ROM. If ever relevant, we can optimize for that -- + * but we'll need additional information about the reservations. + */ + reserved =3D memory_devices_get_reserved_memslots(); + limit =3D hdev->vhost_ops->vhost_backend_memslots_limit(hdev); + if (used + reserved > limit) { + error_setg(errp, "vhost backend memory slots limit (%d) is less" + " than current number of used (%d) and reserved (%d)" + " memory slots for memory devices.", limit, used, reser= ved); r =3D -EINVAL; goto fail_busyloop; } diff --git a/include/hw/mem/memory-device.h b/include/hw/mem/memory-device.h index b51a579fb9..c7b624da6a 100644 --- a/include/hw/mem/memory-device.h +++ b/include/hw/mem/memory-device.h @@ -46,6 +46,12 @@ typedef struct MemoryDeviceState MemoryDeviceState; * single RAM memory region or a memory region container with subregions * that are RAM memory regions or aliases to RAM memory regions. Other * memory regions or subregions are not supported. + * + * If the device memory region returned via @get_memory_region is a + * memory region container, it's supported to dynamically (un)map subregio= ns + * as long as the number of memslots returned by @get_memslots() won't + * be exceeded and as long as all memory regions are of the same kind (e.g= ., + * all RAM or all ROM). */ struct MemoryDeviceClass { /* private */ @@ -125,6 +131,7 @@ struct MemoryDeviceClass { =20 MemoryDeviceInfoList *qmp_memory_device_list(void); uint64_t get_plugged_memory_size(void); +unsigned int memory_devices_get_reserved_memslots(void); void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms, const uint64_t *legacy_align, Error **errp); void memory_device_plug(MemoryDeviceState *md, MachineState *ms); diff --git a/stubs/memory_device.c b/stubs/memory_device.c index e75cac62dc..318a5d4187 100644 --- a/stubs/memory_device.c +++ b/stubs/memory_device.c @@ -10,3 +10,8 @@ uint64_t get_plugged_memory_size(void) { return (uint64_t)-1; } + +unsigned int memory_devices_get_reserved_memslots(void) +{ + return 0; +} --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754943; cv=none; d=zohomail.com; s=zohoarc; b=ccJxzs66naiYbPfln+rGVghwjZ4vIG7XG5qhHMY9WqlCJnmFbRvOyIAkcnuBVcTB+nqy6PEgNdv6YWixws2l3PwQGbyMO7Fy6WSonn5BDvczSAHoaPg6M2haBM/aLmA88NWlrJ4Bw2e24doug1dQA53e9/A/CZRAgQSXEs93JHo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754943; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=aTbdvO7UZM5OCNU5gIZTIqcO3Wj+jsd902L61o+vwPE=; b=IivBzlHFxJZ/pQ+LwKrZKbIL4a1yP83X4YISW/nE7ud7AYrpgFP4IVcVIITBSvuCpRG10bwaqdOFhVb8zRJdqzFdGsdEehDiJmtZetZUI/7rWd9xRT8Sf7qhdcgxCkpAFwB0wquQHvqMnhQPj4K9CYr7g6uULyuqJ323CYYlRec= 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 1695754943186163.57933160108212; Tue, 26 Sep 2023 12:02:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDHW-00016W-PT; Tue, 26 Sep 2023 14:59:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHO-0000cd-PG for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHN-0003AK-7d for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:30 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-620-VocOJCasP5W_kOLm8aZTjw-1; Tue, 26 Sep 2023 14:59:23 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7CA99185A79B; Tue, 26 Sep 2023 18:59:22 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 99AEC2026D4B; Tue, 26 Sep 2023 18:59:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754768; 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=aTbdvO7UZM5OCNU5gIZTIqcO3Wj+jsd902L61o+vwPE=; b=L21/3qa1Ll6cFYvepr2P9oqsHV7yWGtcI7ae+F2u2tkj5MFJDdBxvjJ7GRDdzoh/f0N5QI kMtaHP/NzOsgf8vTx/CJhEuv/yj72atW7OL117tkrRTVs+FOB+/9MEnmCx2mJ4TU+n8Fbv G88rmY7A8SfzOTe4/bxXqkC3QVRI/3I= X-MC-Unique: VocOJCasP5W_kOLm8aZTjw-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, "Maciej S . Szmigiero" Subject: [PATCH v4 10/18] kvm: Add stub for kvm_get_max_memslots() Date: Tue, 26 Sep 2023 20:57:30 +0200 Message-ID: <20230926185738.277351-11-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754944095100003 Content-Type: text/plain; charset="utf-8" We'll need the stub soon from memory device context. While at it, use "unsigned int" as return value and place the declaration next to kvm_get_free_memslots(). Reviewed-by: Maciej S. Szmigiero Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- accel/kvm/kvm-all.c | 2 +- accel/stubs/kvm-stub.c | 5 +++++ include/sysemu/kvm.h | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 9d4c5a4c51..628076bdd8 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -174,7 +174,7 @@ void kvm_resample_fd_notify(int gsi) } } =20 -int kvm_get_max_memslots(void) +unsigned int kvm_get_max_memslots(void) { KVMState *s =3D KVM_STATE(current_accel()); =20 diff --git a/accel/stubs/kvm-stub.c b/accel/stubs/kvm-stub.c index a5d4442d8f..51f522e52e 100644 --- a/accel/stubs/kvm-stub.c +++ b/accel/stubs/kvm-stub.c @@ -109,6 +109,11 @@ int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s,= EventNotifier *n, return -ENOSYS; } =20 +unsigned int kvm_get_max_memslots(void) +{ + return 0; +} + unsigned int kvm_get_free_memslots(void) { return 0; diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index c3d831baef..97a8a4f201 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -215,6 +215,7 @@ typedef struct KVMRouteChange { =20 /* external API */ =20 +unsigned int kvm_get_max_memslots(void); unsigned int kvm_get_free_memslots(void); bool kvm_has_sync_mmu(void); int kvm_has_vcpu_events(void); @@ -552,7 +553,6 @@ int kvm_set_one_reg(CPUState *cs, uint64_t id, void *so= urce); */ int kvm_get_one_reg(CPUState *cs, uint64_t id, void *target); struct ppc_radix_page_info *kvm_get_radix_page_info(void); -int kvm_get_max_memslots(void); =20 /* Notify resamplefd for EOI of specific interrupts. */ void kvm_resample_fd_notify(int gsi); --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754829; cv=none; d=zohomail.com; s=zohoarc; b=bKjCx/Yzh4oPGr0zofPOD74DkGH1AHtZJb9DfFpIOgLh1QVaUajZCYkQVeR1yYllbl+ondltTcfhIP6C5kDnAerM4peMd0jn0AiZCQE7YBsn6XxwqB843gJrNOZ2jFTRja4gDcBjBqiSfNg4I9INM7uroHk5A70totswM1PQkIA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754829; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=lIfRMUCd48IhDe7JraPd1zdi82ZXxhpWgPA7X+aEIzE=; b=CC0U05SQwjQrkY7fTjlyYlqHrV816WgnZ+dkmkURUpyJHf/Nae6xDBMdq8/95UsyQOLIm6vc65G+lTHw3dmHdHRibH8BYHU4+NBfUD7HA2ijyY/Yr9yq6gl5bvcZBKl2WAUBmXgEWv84SknZaGj7WLqHcodmnWB+ahwtIlt4/Cg= 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 1695754829351883.4367935990779; Tue, 26 Sep 2023 12:00:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDHl-0001Jz-7J; Tue, 26 Sep 2023 14:59:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHU-00014Y-7P for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHS-0003BZ-Jf for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:35 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-326-akunlqBOMquWrlqJztU1Ug-1; Tue, 26 Sep 2023 14:59:29 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C3557800962; Tue, 26 Sep 2023 18:59:28 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id F33B92026D4B; Tue, 26 Sep 2023 18:59:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754773; 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=lIfRMUCd48IhDe7JraPd1zdi82ZXxhpWgPA7X+aEIzE=; b=f5Vqy41ojbpvr5OOl3FJD4gOi+oqOvP8PWpNcIbpfPRkB1uamgnjbnhDgEg2KhXJAdBogE BvGv1xus66oyOMt/80sqCAVMHZCOyQkPvluNtazRwmdcThSrmWvHkxVgLC2HR7GMzPIArg yB1uzzIJ7qjH6FfgPRcKtgD3Hqjf1v0= X-MC-Unique: akunlqBOMquWrlqJztU1Ug-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, "Maciej S . Szmigiero" Subject: [PATCH v4 11/18] vhost: Add vhost_get_max_memslots() Date: Tue, 26 Sep 2023 20:57:31 +0200 Message-ID: <20230926185738.277351-12-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754830733100001 Content-Type: text/plain; charset="utf-8" Let's add vhost_get_max_memslots(). Reviewed-by: Maciej S. Szmigiero Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/virtio/vhost-stub.c | 5 +++++ hw/virtio/vhost.c | 11 +++++++++++ include/hw/virtio/vhost.h | 1 + 3 files changed, 17 insertions(+) diff --git a/hw/virtio/vhost-stub.c b/hw/virtio/vhost-stub.c index d53dd9d288..52d42adab2 100644 --- a/hw/virtio/vhost-stub.c +++ b/hw/virtio/vhost-stub.c @@ -2,6 +2,11 @@ #include "hw/virtio/vhost.h" #include "hw/virtio/vhost-user.h" =20 +unsigned int vhost_get_max_memslots(void) +{ + return UINT_MAX; +} + unsigned int vhost_get_free_memslots(void) { return UINT_MAX; diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index f7e1ac12a8..ee193b07c7 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -55,6 +55,17 @@ static unsigned int used_shared_memslots; static QLIST_HEAD(, vhost_dev) vhost_devices =3D QLIST_HEAD_INITIALIZER(vhost_devices); =20 +unsigned int vhost_get_max_memslots(void) +{ + unsigned int max =3D UINT_MAX; + struct vhost_dev *hdev; + + QLIST_FOREACH(hdev, &vhost_devices, entry) { + max =3D MIN(max, hdev->vhost_ops->vhost_backend_memslots_limit(hde= v)); + } + return max; +} + unsigned int vhost_get_free_memslots(void) { unsigned int free =3D UINT_MAX; diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index 603bf834be..c7e5467693 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -315,6 +315,7 @@ uint64_t vhost_get_features(struct vhost_dev *hdev, con= st int *feature_bits, */ void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, uint64_t features); +unsigned int vhost_get_max_memslots(void); unsigned int vhost_get_free_memslots(void); =20 int vhost_net_set_backend(struct vhost_dev *hdev, --=20 2.41.0 From nobody Wed May 15 09:35:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1695754965283759.2478866913046; Tue, 26 Sep 2023 12:02:45 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDID-000259-Ea; Tue, 26 Sep 2023 15:00:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHl-0001O9-9A for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:54 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHY-0003CD-Sm for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:52 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-258-59ub39sdPvyFoZr250euSA-1; Tue, 26 Sep 2023 14:59:35 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C083F101AA70; Tue, 26 Sep 2023 18:59:34 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4E0732026D4B; Tue, 26 Sep 2023 18:59:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754779; 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=9HTQ1GzBM+CWG+dSAEeSN+0jirsBI1Al/8Rh4D+SeCk=; b=Y9Vg3FMozuS2EwKqayRGUAgkOVQFirb5lDjLNC081PuCJnX3jaBqAPHTG4b833pW3zLY72 P9PO5yKRDWehP5FQHFLnNz758TkKpUKHgCIKL8BjX5c1hh+1nVa5yO7pehzsQ+DLTdnmo+ Xq/zvIrVJJIgreg4IbmPwiTuivDdvc4= X-MC-Unique: 59ub39sdPvyFoZr250euSA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, "Maciej S . Szmigiero" Subject: [PATCH v4 12/18] memory-device, vhost: Support automatic decision on the number of memslots Date: Tue, 26 Sep 2023 20:57:32 +0200 Message-ID: <20230926185738.277351-13-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1695754967761100003 Content-Type: text/plain; charset="utf-8" We want to support memory devices that can automatically decide how many memslots they will use. In the worst case, they have to use a single memslot. The target use cases are virtio-mem and the hyper-v balloon. Let's calculate a reasonable limit such a memory device may use, and instruct the device to make a decision based on that limit. Use a simple heuristic that considers: * A memslot soft-limit for all memory devices of 256; also, to not consume too many memslots -- which could harm performance. * Actually still free and unreserved memslots * The percentage of the remaining device memory region that memory device will occupy. Further, while we properly check before plugging a memory device whether there still is are free memslots, we have other memslot consumers (such as boot memory, PCI BARs) that don't perform any checks and might dynamically consume memslots without any prior reservation. So we might succeed in plugging a memory device, but once we dynamically map a PCI BAR we would be in trouble. Doing accounting / reservation / checks for all such users is problematic (e.g., sometimes we might temporarily split boot memory into two memslots, triggered by the BIOS). We use the historic magic memslot number of 509 as orientation to when supporting 256 memory devices -> memslots (leaving 253 for boot memory and other devices) has been proven to work reliable. We'll fallback to suggesting a single memslot if we don't have at least 509 total memslots. Plugging vhost devices with less than 509 memslots available while we have memory devices plugged that consume multiple memslots due to automatic decisions can be problematic. Most configurations might just fail due to "limit < used + reserved", however, it can also happen that these memory devices would suddenly consume memslots that would actually be required by other memslot consumers (boot, PCI BARs) later. Note that this has always been sketchy with vhost devices that support only a small number of memslots; but we don't want to make it any worse.So let's keep it simple and simply reject plugging such vhost devices in such a configuration. Eventually, all vhost devices that want to be fully compatible with such memory devices should support a decent number of memslots (>=3D 509). Reviewed-by: Maciej S. Szmigiero Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/mem/memory-device.c | 96 ++++++++++++++++++++++++++++++++-- hw/virtio/vhost.c | 14 ++++- include/hw/boards.h | 4 ++ include/hw/mem/memory-device.h | 32 ++++++++++++ stubs/memory_device.c | 5 ++ 5 files changed, 147 insertions(+), 4 deletions(-) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 1b14ba5661..ae38f48f16 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -85,13 +85,93 @@ unsigned int memory_devices_get_reserved_memslots(void) return get_reserved_memslots(current_machine); } =20 +bool memory_devices_memslot_auto_decision_active(void) +{ + if (!current_machine->device_memory) { + return false; + } + + return current_machine->device_memory->memslot_auto_decision_active; +} + +static unsigned int memory_device_memslot_decision_limit(MachineState *ms, + MemoryRegion *mr) +{ + const unsigned int reserved =3D get_reserved_memslots(ms); + const uint64_t size =3D memory_region_size(mr); + unsigned int max =3D vhost_get_max_memslots(); + unsigned int free =3D vhost_get_free_memslots(); + uint64_t available_space; + unsigned int memslots; + + if (kvm_enabled()) { + max =3D MIN(max, kvm_get_max_memslots()); + free =3D MIN(free, kvm_get_free_memslots()); + } + + /* + * If we only have less overall memslots than what we consider reasona= ble, + * just keep it to a minimum. + */ + if (max < MEMORY_DEVICES_SAFE_MAX_MEMSLOTS) { + return 1; + } + + /* + * Consider our soft-limit across all memory devices. We don't really + * expect to exceed this limit in reasonable configurations. + */ + if (MEMORY_DEVICES_SOFT_MEMSLOT_LIMIT <=3D + ms->device_memory->required_memslots) { + return 1; + } + memslots =3D MEMORY_DEVICES_SOFT_MEMSLOT_LIMIT - + ms->device_memory->required_memslots; + + /* + * Consider the actually still free memslots. This is only relevant if + * other memslot consumers would consume *significantly* more memslots= than + * what we prepared for (> 253). Unlikely, but let's just handle it + * cleanly. + */ + memslots =3D MIN(memslots, free - reserved); + if (memslots < 1 || unlikely(free < reserved)) { + return 1; + } + + /* We cannot have any other memory devices? So give all to this device= . */ + if (size =3D=3D ms->maxram_size - ms->ram_size) { + return memslots; + } + + /* + * Simple heuristic: equally distribute the memslots over the space + * still available for memory devices. + */ + available_space =3D ms->maxram_size - ms->ram_size - + ms->device_memory->used_region_size; + memslots =3D (double)memslots * size / available_space; + return memslots < 1 ? 1 : memslots; +} + static void memory_device_check_addable(MachineState *ms, MemoryDeviceStat= e *md, MemoryRegion *mr, Error **errp) { + const MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(md); const uint64_t used_region_size =3D ms->device_memory->used_region_siz= e; const uint64_t size =3D memory_region_size(mr); - const unsigned int required_memslots =3D memory_device_get_memslots(md= ); const unsigned int reserved_memslots =3D get_reserved_memslots(ms); + unsigned int required_memslots, memslot_limit; + + /* + * Instruct the device to decide how many memslots to use, if applicab= le, + * before we query the number of required memslots the first time. + */ + if (mdc->decide_memslots) { + memslot_limit =3D memory_device_memslot_decision_limit(ms, mr); + mdc->decide_memslots(md, memslot_limit); + } + required_memslots =3D memory_device_get_memslots(md); =20 /* we will need memory slots for kvm and vhost */ if (kvm_enabled() && @@ -300,6 +380,7 @@ out: void memory_device_plug(MemoryDeviceState *md, MachineState *ms) { const MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(md); + const unsigned int memslots =3D memory_device_get_memslots(md); const uint64_t addr =3D mdc->get_addr(md); MemoryRegion *mr; =20 @@ -311,7 +392,11 @@ void memory_device_plug(MemoryDeviceState *md, Machine= State *ms) g_assert(ms->device_memory); =20 ms->device_memory->used_region_size +=3D memory_region_size(mr); - ms->device_memory->required_memslots +=3D memory_device_get_memslots(m= d); + ms->device_memory->required_memslots +=3D memslots; + if (mdc->decide_memslots && memslots > 1) { + ms->device_memory->memslot_auto_decision_active++; + } + memory_region_add_subregion(&ms->device_memory->mr, addr - ms->device_memory->base, mr); trace_memory_device_plug(DEVICE(md)->id ? DEVICE(md)->id : "", addr); @@ -320,6 +405,7 @@ void memory_device_plug(MemoryDeviceState *md, MachineS= tate *ms) void memory_device_unplug(MemoryDeviceState *md, MachineState *ms) { const MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(md); + const unsigned int memslots =3D memory_device_get_memslots(md); MemoryRegion *mr; =20 /* @@ -330,8 +416,12 @@ void memory_device_unplug(MemoryDeviceState *md, Machi= neState *ms) g_assert(ms->device_memory); =20 memory_region_del_subregion(&ms->device_memory->mr, mr); + + if (mdc->decide_memslots && memslots > 1) { + ms->device_memory->memslot_auto_decision_active--; + } ms->device_memory->used_region_size -=3D memory_region_size(mr); - ms->device_memory->required_memslots -=3D memory_device_get_memslots(m= d); + ms->device_memory->required_memslots -=3D memslots; trace_memory_device_unplug(DEVICE(md)->id ? DEVICE(md)->id : "", mdc->get_addr(md)); } diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index ee193b07c7..24013b39d6 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -1462,6 +1462,19 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opa= que, goto fail; } =20 + limit =3D hdev->vhost_ops->vhost_backend_memslots_limit(hdev); + if (limit < MEMORY_DEVICES_SAFE_MAX_MEMSLOTS && + memory_devices_memslot_auto_decision_active()) { + error_setg(errp, "some memory device (like virtio-mem)" + " decided how many memory slots to use based on the overall" + " number of memory slots; this vhost backend would further" + " restricts the overall number of memory slots"); + error_append_hint(errp, "Try plugging this vhost backend before" + " plugging such memory devices.\n"); + r =3D -EINVAL; + goto fail; + } + for (i =3D 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) { r =3D vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i= ); if (r < 0) { @@ -1548,7 +1561,6 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaq= ue, * but we'll need additional information about the reservations. */ reserved =3D memory_devices_get_reserved_memslots(); - limit =3D hdev->vhost_ops->vhost_backend_memslots_limit(hdev); if (used + reserved > limit) { error_setg(errp, "vhost backend memory slots limit (%d) is less" " than current number of used (%d) and reserved (%d)" diff --git a/include/hw/boards.h b/include/hw/boards.h index 5a9d72289c..96985c6bfc 100644 --- a/include/hw/boards.h +++ b/include/hw/boards.h @@ -303,6 +303,9 @@ struct MachineClass { * @used_region_size: the part of @mr already used by memory devices * @required_memslots: the number of memslots required by memory devices * @used_memslots: the number of memslots currently used by memory devices + * @memslot_auto_decision_active: whether any plugged memory device + * automatically decided to use more than + * one memslot */ typedef struct DeviceMemoryState { hwaddr base; @@ -313,6 +316,7 @@ typedef struct DeviceMemoryState { uint64_t used_region_size; unsigned int required_memslots; unsigned int used_memslots; + unsigned int memslot_auto_decision_active; } DeviceMemoryState; =20 /** diff --git a/include/hw/mem/memory-device.h b/include/hw/mem/memory-device.h index c7b624da6a..3354d6c166 100644 --- a/include/hw/mem/memory-device.h +++ b/include/hw/mem/memory-device.h @@ -14,6 +14,7 @@ #define MEMORY_DEVICE_H =20 #include "hw/qdev-core.h" +#include "qemu/typedefs.h" #include "qapi/qapi-types-machine.h" #include "qom/object.h" =20 @@ -99,6 +100,15 @@ struct MemoryDeviceClass { */ MemoryRegion *(*get_memory_region)(MemoryDeviceState *md, Error **errp= ); =20 + /* + * Optional: Instruct the memory device to decide how many memory slots + * it requires, not exceeding the given limit. + * + * Called exactly once when pre-plugging the memory device, before + * querying the number of memslots using @get_memslots the first time. + */ + void (*decide_memslots)(MemoryDeviceState *md, unsigned int limit); + /* * Optional for memory devices that require only a single memslot, * required for all other memory devices: Return the number of memslots @@ -129,9 +139,31 @@ struct MemoryDeviceClass { MemoryDeviceInfo *info); }; =20 +/* + * Traditionally, KVM/vhost in many setups supported 509 memslots, whereby + * 253 memslots were "reserved" for boot memory and other devices (such + * as PCI BARs, which can get mapped dynamically) and 256 memslots were + * dedicated for DIMMs. These magic numbers worked reliably in the past. + * + * Further, using many memslots can negatively affect performance, so sett= ing + * the soft-limit of memslots used by memory devices to the traditional + * DIMM limit of 256 sounds reasonable. + * + * If we have less than 509 memslots, we will instruct memory devices that + * support automatically deciding how many memslots to use to only use a s= ingle + * one. + * + * Hotplugging vhost devices with at least 509 memslots is not expected to + * cause problems, not even when devices automatically decided how many me= mslots + * to use. + */ +#define MEMORY_DEVICES_SOFT_MEMSLOT_LIMIT 256 +#define MEMORY_DEVICES_SAFE_MAX_MEMSLOTS 509 + MemoryDeviceInfoList *qmp_memory_device_list(void); uint64_t get_plugged_memory_size(void); unsigned int memory_devices_get_reserved_memslots(void); +bool memory_devices_memslot_auto_decision_active(void); void memory_device_pre_plug(MemoryDeviceState *md, MachineState *ms, const uint64_t *legacy_align, Error **errp); void memory_device_plug(MemoryDeviceState *md, MachineState *ms); diff --git a/stubs/memory_device.c b/stubs/memory_device.c index 318a5d4187..15fd93ff67 100644 --- a/stubs/memory_device.c +++ b/stubs/memory_device.c @@ -15,3 +15,8 @@ unsigned int memory_devices_get_reserved_memslots(void) { return 0; } + +bool memory_devices_memslot_auto_decision_active(void) +{ + return false; +} --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754864; cv=none; d=zohomail.com; s=zohoarc; b=cOrJccCdbUEgIbWslEdLW8fT5dGZA9DZqAbtzyVX8HXfA6/8mAWp9EA06N9CVuexOENeh30/zoY1Di8a4NKsRN2grJ+dQ4y0SdDYGKQtTTlrGZLT86yoRu3PxtddbiSGMt4XQElLvRHbfmc1PiYVysxTwPhF0H4c4N6dxLaFm9o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754864; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kAjWTObzNlBTuzJ6Kx6X0lhS4kVtRiZ5bCIIKZx09sc=; b=AR42v3DhqBKB9+LZgIRZohnZvGN2ineasP/tAkZCcIdMzoURFP12Ru7HdRW6xIFMFX0qfr6bIJcZlYfIH8rm9/48Da0M9XQTKRKyIazHr9umrkMP/emB7/cDTkmEyVVR/sAnXaUtvzAg9NvaH50IhU1O+NSNgEyDfrSfj2+AqNM= 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 1695754864767920.2144719146447; Tue, 26 Sep 2023 12:01:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDI9-0001q2-13; Tue, 26 Sep 2023 15:00:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHj-0001LQ-LU for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:52 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHf-0003Dl-NE for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:51 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-558-5lnrPmILMWqhoyXc1N9-GA-1; Tue, 26 Sep 2023 14:59:41 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 13CCA8007A4; Tue, 26 Sep 2023 18:59:41 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 474612026D4B; Tue, 26 Sep 2023 18:59:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754786; 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=kAjWTObzNlBTuzJ6Kx6X0lhS4kVtRiZ5bCIIKZx09sc=; b=BbLieDgbq/mAb4zx9xeJV8g/D+IgFwHedh+FV/o1DPlRwlvdhYqrxG5SJMKqNZEu5Qd4s8 ypOVtlVWj/gbiAFGTgpZqqQw1i1eHjIYAKDnTChvcttzAWus4LJ9/BlL1K11zaDD0xzGqL Z80GeWxB2TZpT6sNPcrxl6Or3YV5dMA= X-MC-Unique: 5lnrPmILMWqhoyXc1N9-GA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org, "Maciej S . Szmigiero" Subject: [PATCH v4 13/18] memory: Clarify mapping requirements for RamDiscardManager Date: Tue, 26 Sep 2023 20:57:33 +0200 Message-ID: <20230926185738.277351-14-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754865436100001 Content-Type: text/plain; charset="utf-8" We really only care about the RAM memory region not being mapped into an address space yet as long as we're still setting up the RamDiscardManager. Once mapped into an address space, memory notifiers would get notified about such a region and any attempts to modify the RamDiscardManager would be wrong. While "mapped into an address space" is easy to check for RAM regions that are mapped directly (following the ->container links), it's harder to check when such regions are mapped indirectly via aliases. For now, we can only detect that a region is mapped through an alias (->mapped_via_alias), but we don't have a handle on these aliases to follow all their ->container links to test if they are eventually mapped into an address space. So relax the assertion in memory_region_set_ram_discard_manager(), remove the check in memory_region_get_ram_discard_manager() and clarify the doc. Reviewed-by: Maciej S. Szmigiero Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- include/exec/memory.h | 5 +++-- softmmu/memory.c | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/exec/memory.h b/include/exec/memory.h index ef23d65afc..73062edf49 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -599,8 +599,9 @@ typedef void (*ReplayRamDiscard)(MemoryRegionSection *s= ection, void *opaque); * populated (consuming memory), to be used/accessed by the VM. * * A #RamDiscardManager can only be set for a RAM #MemoryRegion while the - * #MemoryRegion isn't mapped yet; it cannot change while the #MemoryRegio= n is - * mapped. + * #MemoryRegion isn't mapped into an address space yet (either directly + * or via an alias); it cannot change while the #MemoryRegion is + * mapped into an address space. * * The #RamDiscardManager is intended to be used by technologies that are * incompatible with discarding of RAM (e.g., VFIO, which may pin all diff --git a/softmmu/memory.c b/softmmu/memory.c index c0383a163d..9d491d488b 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -2085,7 +2085,7 @@ int memory_region_iommu_num_indexes(IOMMUMemoryRegion= *iommu_mr) =20 RamDiscardManager *memory_region_get_ram_discard_manager(MemoryRegion *mr) { - if (!memory_region_is_mapped(mr) || !memory_region_is_ram(mr)) { + if (!memory_region_is_ram(mr)) { return NULL; } return mr->rdm; @@ -2094,7 +2094,7 @@ RamDiscardManager *memory_region_get_ram_discard_mana= ger(MemoryRegion *mr) void memory_region_set_ram_discard_manager(MemoryRegion *mr, RamDiscardManager *rdm) { - g_assert(memory_region_is_ram(mr) && !memory_region_is_mapped(mr)); + g_assert(memory_region_is_ram(mr)); g_assert(!rdm || !mr->rdm); mr->rdm =3D rdm; } --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754937; cv=none; d=zohomail.com; s=zohoarc; b=NmIk7hIc4bBR1KzXwxl06iCG+sHe0K7t3IPKcy9D+DIUZbXkvLEIVjngP+sxBSehtHckGwQMihZHccsY8SdhvUj49IsIwUm0WDrS9jGFikN4ySfUevuc90CyH3OkZ30gZgx2jTSTyDNPg4clRQwe/Bnsg40IKAF/R3LJeLUvkGY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754937; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=adaLIN+oCidzf9Yl1V7wtFZZqNM0t8YdJyA6CFGysIY=; b=KU5BwGWP41lCQiuqdgT+EyMku0ITI15Xp0dUhWiwlebRTFpqyZrCl57OeXMtcJRqTCwMI6vfU7zAwugzEpWOkQDiDb93e66SPJv4o6rRCgWsuMlpPGsZS2bdDssdDe1BaR0qXx+VkCYTFr3hrdxowk0MCxogsjjEF0i8c+7aYeI= 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 1695754937208509.3856063672562; Tue, 26 Sep 2023 12:02:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDIA-0001v0-7g; Tue, 26 Sep 2023 15:00:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHl-0001O5-97 for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:53 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHj-0003EA-E0 for qemu-devel@nongnu.org; Tue, 26 Sep 2023 14:59:52 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-515-x1igqqiVPtOTFGoR0hcyVg-1; Tue, 26 Sep 2023 14:59:47 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 140603C0BE3C; Tue, 26 Sep 2023 18:59:47 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9FBBE2026D4B; Tue, 26 Sep 2023 18:59:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754790; 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=adaLIN+oCidzf9Yl1V7wtFZZqNM0t8YdJyA6CFGysIY=; b=M2MP9cjadlp5ycdvKQVxLLyeYFRoIdINtm+WBucT3RUQj1v1ve/WLf8FFRskS38NQAVG34 c7QMP2/ejWIrwKELLLi9mO08wjVoDqIFt7s9cWq1VJDMMSvzzsDJVfbXlpUNaLP2VeJQkK 9wfDAWOEYuo2PtsnAVNIvVSj5JEaP5M= X-MC-Unique: x1igqqiVPtOTFGoR0hcyVg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org Subject: [PATCH v4 14/18] virtio-mem: Pass non-const VirtIOMEM via virtio_mem_range_cb Date: Tue, 26 Sep 2023 20:57:34 +0200 Message-ID: <20230926185738.277351-15-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: 1695754938903100001 Content-Type: text/plain; charset="utf-8" Let's prepare for a user that has to modify the VirtIOMEM device state. Signed-off-by: David Hildenbrand Reviewed-by: Maciej S. Szmigiero Reviewed-by: Michael S. Tsirkin --- hw/virtio/virtio-mem.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index da5b09cefc..0b0e6c5090 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -177,10 +177,10 @@ static bool virtio_mem_is_busy(void) return migration_in_incoming_postcopy() || !migration_is_idle(); } =20 -typedef int (*virtio_mem_range_cb)(const VirtIOMEM *vmem, void *arg, +typedef int (*virtio_mem_range_cb)(VirtIOMEM *vmem, void *arg, uint64_t offset, uint64_t size); =20 -static int virtio_mem_for_each_unplugged_range(const VirtIOMEM *vmem, void= *arg, +static int virtio_mem_for_each_unplugged_range(VirtIOMEM *vmem, void *arg, virtio_mem_range_cb cb) { unsigned long first_zero_bit, last_zero_bit; @@ -204,7 +204,7 @@ static int virtio_mem_for_each_unplugged_range(const Vi= rtIOMEM *vmem, void *arg, return ret; } =20 -static int virtio_mem_for_each_plugged_range(const VirtIOMEM *vmem, void *= arg, +static int virtio_mem_for_each_plugged_range(VirtIOMEM *vmem, void *arg, virtio_mem_range_cb cb) { unsigned long first_bit, last_bit; @@ -969,7 +969,7 @@ static void virtio_mem_device_unrealize(DeviceState *de= v) ram_block_coordinated_discard_require(false); } =20 -static int virtio_mem_discard_range_cb(const VirtIOMEM *vmem, void *arg, +static int virtio_mem_discard_range_cb(VirtIOMEM *vmem, void *arg, uint64_t offset, uint64_t size) { RAMBlock *rb =3D vmem->memdev->mr.ram_block; @@ -1021,7 +1021,7 @@ static int virtio_mem_post_load(void *opaque, int ver= sion_id) return virtio_mem_restore_unplugged(vmem); } =20 -static int virtio_mem_prealloc_range_cb(const VirtIOMEM *vmem, void *arg, +static int virtio_mem_prealloc_range_cb(VirtIOMEM *vmem, void *arg, uint64_t offset, uint64_t size) { void *area =3D memory_region_get_ram_ptr(&vmem->memdev->mr) + offset; --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754931; cv=none; d=zohomail.com; s=zohoarc; b=Z3Zj5l5p5IVs8MdVl3n/e+YFmicoi+j7bA8KJqlkd0SO1O+DFtHZxbPr/QrCD148GdDIC2DbjSv3K83ZnzdiOHmvrFuuKNQ6Cl7Y2v5kQXd/FkYsPn33g67bOjsI8tGnW5P6vpUBrzUXeLzVZUaAgpyOC/r/DEN8BbrQn158m44= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754931; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ntTc+iP8L3yWrsrYKBBm9BSj/8eg/GV85E1SzdVW1Ig=; b=ILtt5B1U9silrTZhd3epdohwq2Yl6+MoFJUy1q7gYEQogOjEpT6ovTVGI4kyiTifdnvF+LRdtBUHsFLiiWK66UeVmpOjCig+wbXseOUB8rRGHAuNwiF1A6uzrea82NKdd3rqcHKmF821OgY8/oj3VCbC8Pzs2Q/xrOleNk2jdhc= 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 1695754931841632.5590056506852; Tue, 26 Sep 2023 12:02:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDIA-0001yy-NZ; Tue, 26 Sep 2023 15:00:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHt-0001la-Uj for qemu-devel@nongnu.org; Tue, 26 Sep 2023 15:00:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHq-0003Fo-UW for qemu-devel@nongnu.org; Tue, 26 Sep 2023 15:00:00 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-86-yclTkQ33N6O_cZeOs2BOmA-1; Tue, 26 Sep 2023 14:59:52 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 46C3A3814581; Tue, 26 Sep 2023 18:59:52 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 75CBD2026D4B; Tue, 26 Sep 2023 18:59:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754798; 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=ntTc+iP8L3yWrsrYKBBm9BSj/8eg/GV85E1SzdVW1Ig=; b=SGR2B77oj0OHifHwCHGehZuV+PCjjYnjmGcVOjFL0cK6fN6cH6nZS8E28e61nTj8bqHj74 gbjzDVbcgn7VooFoL8jw3d6EQ+95NhULT8InzYDu8UEiY0gOKmHkZTMBO2sp/bCQJNbq1J 0TxrZMbu6Yg48ITHvDVJMUZpDtWZQ70= X-MC-Unique: yclTkQ33N6O_cZeOs2BOmA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org Subject: [PATCH v4 15/18] virtio-mem: Update state to match bitmap as soon as it's been migrated Date: Tue, 26 Sep 2023 20:57:35 +0200 Message-ID: <20230926185738.277351-16-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: 1695754932628100002 Content-Type: text/plain; charset="utf-8" It's cleaner and future-proof to just have other state that depends on the bitmap state to be updated as soon as possible when restoring the bitmap. So factor out informing RamDiscardListener into a functon and call it in case of early migration right after we restored the bitmap. Signed-off-by: David Hildenbrand Reviewed-by: Maciej S. Szmigiero Reviewed-by: Michael S. Tsirkin --- hw/virtio/virtio-mem.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 0b0e6c5090..0cf47df9cf 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -984,9 +984,8 @@ static int virtio_mem_restore_unplugged(VirtIOMEM *vmem) virtio_mem_discard_range_cb= ); } =20 -static int virtio_mem_post_load(void *opaque, int version_id) +static int virtio_mem_post_load_bitmap(VirtIOMEM *vmem) { - VirtIOMEM *vmem =3D VIRTIO_MEM(opaque); RamDiscardListener *rdl; int ret; =20 @@ -1001,6 +1000,20 @@ static int virtio_mem_post_load(void *opaque, int ve= rsion_id) return ret; } } + return 0; +} + +static int virtio_mem_post_load(void *opaque, int version_id) +{ + VirtIOMEM *vmem =3D VIRTIO_MEM(opaque); + int ret; + + if (!vmem->early_migration) { + ret =3D virtio_mem_post_load_bitmap(vmem); + if (ret) { + return ret; + } + } =20 /* * If shared RAM is migrated using the file content and not using QEMU, @@ -1043,7 +1056,7 @@ static int virtio_mem_post_load_early(void *opaque, i= nt version_id) int ret; =20 if (!vmem->prealloc) { - return 0; + goto post_load_bitmap; } =20 /* @@ -1051,7 +1064,7 @@ static int virtio_mem_post_load_early(void *opaque, i= nt version_id) * don't mess with preallocation and postcopy. */ if (migrate_ram_is_ignored(rb)) { - return 0; + goto post_load_bitmap; } =20 /* @@ -1084,7 +1097,10 @@ static int virtio_mem_post_load_early(void *opaque, = int version_id) return -EBUSY; } } - return 0; + +post_load_bitmap: + /* Finally, update any other state to be consistent with the new bitma= p. */ + return virtio_mem_post_load_bitmap(vmem); } =20 typedef struct VirtIOMEMMigSanityChecks { --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754922; cv=none; d=zohomail.com; s=zohoarc; b=D3wTEhZt4fyf3g6rEEgNcl4LNNK/dRmx4xUX0T7E+LOC4ziFIFPmYaiZYbgAoA974fnUikUgMFt3+U4tqlS4+Y3ZcXoLN6QdEFYh2l6ZRrwIHdpNBxpRh+8MAS0IZrFchXpgTy7bjzLV2yQPUW7O2fQvNd1XYnzeEaUfci7B9Yw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754922; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=vV4PQc9MX5cpMSAkjQdv/L1WcUiVtYlZmV5E59fKZWI=; b=cEGBvmb2qrJrffu6V95r9dYghN6KY6ymaqcoMdtPwqtKai2ROyl2axkoWYShYoHEzxTeAbYoZuwfX9ZEATNM8umdzhjo7CeBgcSvDxTCcCrkONOEGPruVp8P04I4zzJrjoGSGtbn5zcNIN5XWwYKo6/qCRga1uE7m3s1VuvSrEs= 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 1695754922368601.1150830914517; Tue, 26 Sep 2023 12:02:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDIF-0002Dr-NZ; Tue, 26 Sep 2023 15:00:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHv-0001oC-PN for qemu-devel@nongnu.org; Tue, 26 Sep 2023 15:00:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHs-0003Fw-Fq for qemu-devel@nongnu.org; Tue, 26 Sep 2023 15:00:03 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-80-9MIqkCUJMXW4b70zUgM2KQ-1; Tue, 26 Sep 2023 14:59:58 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 929DA811E7D; Tue, 26 Sep 2023 18:59:57 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id B62362026D4B; Tue, 26 Sep 2023 18:59:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754799; 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=vV4PQc9MX5cpMSAkjQdv/L1WcUiVtYlZmV5E59fKZWI=; b=YSNCI/kkJxhnJY5oxKMf+OU5YIHd5nd9zjju1sIUio1qaFcHJZxpEmYkM4gvt2K/EF2wsH FVhIIR2i7cmPaT0KNLbK3uENXlaMgc9xrTdCA2ujqnTUbeB3OVdMwakY5LP3cpRGr+tjZu uIcvPqCcTQ2JP1nk0PKrrqZC28/3oKA= X-MC-Unique: 9MIqkCUJMXW4b70zUgM2KQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org Subject: [PATCH v4 16/18] virtio-mem: Expose device memory dynamically via multiple memslots if enabled Date: Tue, 26 Sep 2023 20:57:36 +0200 Message-ID: <20230926185738.277351-17-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754924036100001 Content-Type: text/plain; charset="utf-8" Having large virtio-mem devices that only expose little memory to a VM is currently a problem: we map the whole sparse memory region into the guest using a single memslot, resulting in one gigantic memslot in KVM. KVM allocates metadata for the whole memslot, which can result in quite some memory waste. Assuming we have a 1 TiB virtio-mem device and only expose little (e.g., 1 GiB) memory, we would create a single 1 TiB memslot and KVM has to allocate metadata for that 1 TiB memslot: on x86, this implies allocating a significant amount of memory for metadata: (1) RMAP: 8 bytes per 4 KiB, 8 bytes per 2 MiB, 8 bytes per 1 GiB -> For 1 TiB: 2147483648 + 4194304 + 8192 =3D ~ 2 GiB (0.2 %) With the TDP MMU (cat /sys/module/kvm/parameters/tdp_mmu) this gets allocated lazily when required for nested VMs (2) gfn_track: 2 bytes per 4 KiB -> For 1 TiB: 536870912 =3D ~512 MiB (0.05 %) (3) lpage_info: 4 bytes per 2 MiB, 4 bytes per 1 GiB -> For 1 TiB: 2097152 + 4096 =3D ~2 MiB (0.0002 %) (4) 2x dirty bitmaps for tracking: 2x 1 bit per 4 KiB page -> For 1 TiB: 536870912 =3D 64 MiB (0.006 %) So we primarily care about (1) and (2). The bad thing is, that the memory consumption *doubles* once SMM is enabled, because we create the memslot once for !SMM and once for SMM. Having a 1 TiB memslot without the TDP MMU consumes around: * With SMM: 5 GiB * Without SMM: 2.5 GiB Having a 1 TiB memslot with the TDP MMU consumes around: * With SMM: 1 GiB * Without SMM: 512 MiB ... and that's really something we want to optimize, to be able to just start a VM with small boot memory (e.g., 4 GiB) and a virtio-mem device that can grow very large (e.g., 1 TiB). Consequently, using multiple memslots and only mapping the memslots we really need can significantly reduce memory waste and speed up memslot-related operations. Let's expose the sparse RAM memory region using multiple memslots, mapping only the memslots we currently need into our device memory region container. The feature can be enabled using "dynamic-memslots=3Don" and requires "unplugged-inaccessible=3Don", which is nowadays the default. Once enabled, we'll auto-detect the number of memslots to use based on the memslot limit provided by the core. We'll use at most 1 memslot per gigabyte. Note that our global limit of memslots accross all memory devices is currently set to 256: even with multiple large virtio-mem devices, we'd still have a sane limit on the number of memslots used. The default is to not dynamically map memslot for now ("dynamic-memslots=3Doff"). The optimization must be enabled manually, because some vhost setups (e.g., hotplug of vhost-user devices) might be problematic until we support more memslots especially in vhost-user backend= s. Note that "dynamic-memslots=3Don" is just a hint that multiple memslots *may* be used for internal optimizations, not that multiple memslots *must* be used. The actual number of memslots that are used is an internal detail: for example, once memslot metadata is no longer an issue, we could simply stop optimizing for that. Migration source and destination can differ on the setting of "dynamic-memslots". Signed-off-by: David Hildenbrand Reviewed-by: Maciej S. Szmigiero Reviewed-by: Michael S. Tsirkin --- hw/virtio/virtio-mem-pci.c | 21 +++ hw/virtio/virtio-mem.c | 288 +++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-mem.h | 32 +++- 3 files changed, 340 insertions(+), 1 deletion(-) diff --git a/hw/virtio/virtio-mem-pci.c b/hw/virtio/virtio-mem-pci.c index c4597e029e..1b4e9a3284 100644 --- a/hw/virtio/virtio-mem-pci.c +++ b/hw/virtio/virtio-mem-pci.c @@ -48,6 +48,25 @@ static MemoryRegion *virtio_mem_pci_get_memory_region(Me= moryDeviceState *md, return vmc->get_memory_region(vmem, errp); } =20 +static void virtio_mem_pci_decide_memslots(MemoryDeviceState *md, + unsigned int limit) +{ + VirtIOMEMPCI *pci_mem =3D VIRTIO_MEM_PCI(md); + VirtIOMEM *vmem =3D VIRTIO_MEM(&pci_mem->vdev); + VirtIOMEMClass *vmc =3D VIRTIO_MEM_GET_CLASS(vmem); + + vmc->decide_memslots(vmem, limit); +} + +static unsigned int virtio_mem_pci_get_memslots(MemoryDeviceState *md) +{ + VirtIOMEMPCI *pci_mem =3D VIRTIO_MEM_PCI(md); + VirtIOMEM *vmem =3D VIRTIO_MEM(&pci_mem->vdev); + VirtIOMEMClass *vmc =3D VIRTIO_MEM_GET_CLASS(vmem); + + return vmc->get_memslots(vmem); +} + static uint64_t virtio_mem_pci_get_plugged_size(const MemoryDeviceState *m= d, Error **errp) { @@ -150,6 +169,8 @@ static void virtio_mem_pci_class_init(ObjectClass *klas= s, void *data) mdc->set_addr =3D virtio_mem_pci_set_addr; mdc->get_plugged_size =3D virtio_mem_pci_get_plugged_size; mdc->get_memory_region =3D virtio_mem_pci_get_memory_region; + mdc->decide_memslots =3D virtio_mem_pci_decide_memslots; + mdc->get_memslots =3D virtio_mem_pci_get_memslots; mdc->fill_device_info =3D virtio_mem_pci_fill_device_info; mdc->get_min_alignment =3D virtio_mem_pci_get_min_alignment; =20 diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 0cf47df9cf..e1e4250e69 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -66,6 +66,13 @@ static uint32_t virtio_mem_default_thp_size(void) return default_thp_size; } =20 +/* + * The minimum memslot size depends on this setting ("sane default"), the + * device block size, and the memory backend page size. The last (or singl= e) + * memslot might be smaller than this constant. + */ +#define VIRTIO_MEM_MIN_MEMSLOT_SIZE (1 * GiB) + /* * We want to have a reasonable default block size such that * 1. We avoid splitting THPs when unplugging memory, which degrades @@ -483,6 +490,96 @@ static bool virtio_mem_valid_range(const VirtIOMEM *vm= em, uint64_t gpa, return true; } =20 +static void virtio_mem_activate_memslot(VirtIOMEM *vmem, unsigned int idx) +{ + const uint64_t memslot_offset =3D idx * vmem->memslot_size; + + assert(vmem->memslots); + + /* + * Instead of enabling/disabling memslots, we add/remove them. This sh= ould + * make address space updates faster, because we don't have to loop ov= er + * many disabled subregions. + */ + if (memory_region_is_mapped(&vmem->memslots[idx])) { + return; + } + memory_region_add_subregion(vmem->mr, memslot_offset, &vmem->memslots[= idx]); +} + +static void virtio_mem_deactivate_memslot(VirtIOMEM *vmem, unsigned int id= x) +{ + assert(vmem->memslots); + + if (!memory_region_is_mapped(&vmem->memslots[idx])) { + return; + } + memory_region_del_subregion(vmem->mr, &vmem->memslots[idx]); +} + +static void virtio_mem_activate_memslots_to_plug(VirtIOMEM *vmem, + uint64_t offset, uint64_t= size) +{ + const unsigned int start_idx =3D offset / vmem->memslot_size; + const unsigned int end_idx =3D (offset + size + vmem->memslot_size - 1= ) / + vmem->memslot_size; + unsigned int idx; + + if (!vmem->dynamic_memslots) { + return; + } + + /* Activate all involved memslots in a single transaction. */ + memory_region_transaction_begin(); + for (idx =3D start_idx; idx < end_idx; idx++) { + virtio_mem_activate_memslot(vmem, idx); + } + memory_region_transaction_commit(); +} + +static void virtio_mem_deactivate_unplugged_memslots(VirtIOMEM *vmem, + uint64_t offset, + uint64_t size) +{ + const uint64_t region_size =3D memory_region_size(&vmem->memdev->mr); + const unsigned int start_idx =3D offset / vmem->memslot_size; + const unsigned int end_idx =3D (offset + size + vmem->memslot_size - 1= ) / + vmem->memslot_size; + unsigned int idx; + + if (!vmem->dynamic_memslots) { + return; + } + + /* Deactivate all memslots with unplugged blocks in a single transacti= on. */ + memory_region_transaction_begin(); + for (idx =3D start_idx; idx < end_idx; idx++) { + const uint64_t memslot_offset =3D idx * vmem->memslot_size; + uint64_t memslot_size =3D vmem->memslot_size; + + /* The size of the last memslot might be smaller. */ + if (idx =3D=3D vmem->nb_memslots - 1) { + memslot_size =3D region_size - memslot_offset; + } + + /* + * Partially covered memslots might still have some blocks plugged= and + * have to remain active if that's the case. + */ + if (offset > memslot_offset || + offset + size < memslot_offset + memslot_size) { + const uint64_t gpa =3D vmem->addr + memslot_offset; + + if (!virtio_mem_is_range_unplugged(vmem, gpa, memslot_size)) { + continue; + } + } + + virtio_mem_deactivate_memslot(vmem, idx); + } + memory_region_transaction_commit(); +} + static int virtio_mem_set_block_state(VirtIOMEM *vmem, uint64_t start_gpa, uint64_t size, bool plug) { @@ -500,6 +597,8 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem, = uint64_t start_gpa, } virtio_mem_notify_unplug(vmem, offset, size); virtio_mem_set_range_unplugged(vmem, start_gpa, size); + /* Deactivate completely unplugged memslots after updating the sta= te. */ + virtio_mem_deactivate_unplugged_memslots(vmem, offset, size); return 0; } =20 @@ -527,7 +626,20 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem,= uint64_t start_gpa, } =20 if (!ret) { + /* + * Activate before notifying and rollback in case of any errors. + * + * When activating a yet inactive memslot, memory notifiers will g= et + * notified about the added memory region and can register with the + * RamDiscardManager; this will traverse all plugged blocks and sk= ip the + * blocks we are plugging here. The following notification will in= form + * registered listeners about the blocks we're plugging. + */ + virtio_mem_activate_memslots_to_plug(vmem, offset, size); ret =3D virtio_mem_notify_plug(vmem, offset, size); + if (ret) { + virtio_mem_deactivate_unplugged_memslots(vmem, offset, size); + } } if (ret) { /* Could be preallocation or a notifier populated memory. */ @@ -620,6 +732,7 @@ static void virtio_mem_resize_usable_region(VirtIOMEM *= vmem, =20 static int virtio_mem_unplug_all(VirtIOMEM *vmem) { + const uint64_t region_size =3D memory_region_size(&vmem->memdev->mr); RAMBlock *rb =3D vmem->memdev->mr.ram_block; =20 if (vmem->size) { @@ -634,6 +747,9 @@ static int virtio_mem_unplug_all(VirtIOMEM *vmem) bitmap_clear(vmem->bitmap, 0, vmem->bitmap_size); vmem->size =3D 0; notifier_list_notify(&vmem->size_change_notifiers, &vmem->size); + + /* Deactivate all memslots after updating the state. */ + virtio_mem_deactivate_unplugged_memslots(vmem, 0, region_size); } =20 trace_virtio_mem_unplugged_all(); @@ -790,6 +906,43 @@ static void virtio_mem_system_reset(void *opaque) virtio_mem_unplug_all(vmem); } =20 +static void virtio_mem_prepare_mr(VirtIOMEM *vmem) +{ + const uint64_t region_size =3D memory_region_size(&vmem->memdev->mr); + + assert(!vmem->mr && vmem->dynamic_memslots); + vmem->mr =3D g_new0(MemoryRegion, 1); + memory_region_init(vmem->mr, OBJECT(vmem), "virtio-mem", + region_size); + vmem->mr->align =3D memory_region_get_alignment(&vmem->memdev->mr); +} + +static void virtio_mem_prepare_memslots(VirtIOMEM *vmem) +{ + const uint64_t region_size =3D memory_region_size(&vmem->memdev->mr); + unsigned int idx; + + g_assert(!vmem->memslots && vmem->nb_memslots && vmem->dynamic_memslot= s); + vmem->memslots =3D g_new0(MemoryRegion, vmem->nb_memslots); + + /* Initialize our memslots, but don't map them yet. */ + for (idx =3D 0; idx < vmem->nb_memslots; idx++) { + const uint64_t memslot_offset =3D idx * vmem->memslot_size; + uint64_t memslot_size =3D vmem->memslot_size; + char name[20]; + + /* The size of the last memslot might be smaller. */ + if (idx =3D=3D vmem->nb_memslots - 1) { + memslot_size =3D region_size - memslot_offset; + } + + snprintf(name, sizeof(name), "memslot-%u", idx); + memory_region_init_alias(&vmem->memslots[idx], OBJECT(vmem), name, + &vmem->memdev->mr, memslot_offset, + memslot_size); + } +} + static void virtio_mem_device_realize(DeviceState *dev, Error **errp) { MachineState *ms =3D MACHINE(qdev_get_machine()); @@ -861,6 +1014,14 @@ static void virtio_mem_device_realize(DeviceState *de= v, Error **errp) vmem->unplugged_inaccessible =3D ON_OFF_AUTO_ON; #endif /* VIRTIO_MEM_HAS_LEGACY_GUESTS */ =20 + if (vmem->dynamic_memslots && + vmem->unplugged_inaccessible !=3D ON_OFF_AUTO_ON) { + error_setg(errp, "'%s' property set to 'on' requires '%s' to be 'o= n'", + VIRTIO_MEM_DYNAMIC_MEMSLOTS_PROP, + VIRTIO_MEM_UNPLUGGED_INACCESSIBLE_PROP); + return; + } + /* * If the block size wasn't configured by the user, use a sane default= . This * allows using hugetlbfs backends of any page size without manual @@ -930,6 +1091,25 @@ static void virtio_mem_device_realize(DeviceState *de= v, Error **errp) virtio_init(vdev, VIRTIO_ID_MEM, sizeof(struct virtio_mem_config)); vmem->vq =3D virtio_add_queue(vdev, 128, virtio_mem_handle_request); =20 + /* + * With "dynamic-memslots=3Doff" (old behavior) we always map the whole + * RAM memory region directly. + */ + if (vmem->dynamic_memslots) { + if (!vmem->mr) { + virtio_mem_prepare_mr(vmem); + } + if (vmem->nb_memslots <=3D 1) { + vmem->nb_memslots =3D 1; + vmem->memslot_size =3D memory_region_size(&vmem->memdev->mr); + } + if (!vmem->memslots) { + virtio_mem_prepare_memslots(vmem); + } + } else { + assert(!vmem->mr && !vmem->nb_memslots && !vmem->memslots); + } + host_memory_backend_set_mapped(vmem->memdev, true); vmstate_register_ram(&vmem->memdev->mr, DEVICE(vmem)); if (vmem->early_migration) { @@ -984,11 +1164,31 @@ static int virtio_mem_restore_unplugged(VirtIOMEM *v= mem) virtio_mem_discard_range_cb= ); } =20 +static int virtio_mem_activate_memslot_range_cb(VirtIOMEM *vmem, void *arg, + uint64_t offset, uint64_t = size) +{ + virtio_mem_activate_memslots_to_plug(vmem, offset, size); + return 0; +} + static int virtio_mem_post_load_bitmap(VirtIOMEM *vmem) { RamDiscardListener *rdl; int ret; =20 + /* + * We restored the bitmap and updated the requested size; activate all + * memslots (so listeners register) before notifying about plugged blo= cks. + */ + if (vmem->dynamic_memslots) { + /* + * We don't expect any active memslots at this point to deactivate= : no + * memory was plugged on the migration destination. + */ + virtio_mem_for_each_plugged_range(vmem, NULL, + virtio_mem_activate_memslot_rang= e_cb); + } + /* * We started out with all memory discarded and our memory region is m= apped * into an address space. Replay, now that we updated the bitmap. @@ -1251,11 +1451,79 @@ static MemoryRegion *virtio_mem_get_memory_region(V= irtIOMEM *vmem, Error **errp) if (!vmem->memdev) { error_setg(errp, "'%s' property must be set", VIRTIO_MEM_MEMDEV_PR= OP); return NULL; + } else if (vmem->dynamic_memslots) { + if (!vmem->mr) { + virtio_mem_prepare_mr(vmem); + } + return vmem->mr; } =20 return &vmem->memdev->mr; } =20 +static void virtio_mem_decide_memslots(VirtIOMEM *vmem, unsigned int limit) +{ + uint64_t region_size, memslot_size, min_memslot_size; + unsigned int memslots; + RAMBlock *rb; + + if (!vmem->dynamic_memslots) { + return; + } + + /* We're called exactly once, before realizing the device. */ + assert(!vmem->nb_memslots); + + /* If realizing the device will fail, just assume a single memslot. */ + if (limit <=3D 1 || !vmem->memdev || !vmem->memdev->mr.ram_block) { + vmem->nb_memslots =3D 1; + return; + } + + rb =3D vmem->memdev->mr.ram_block; + region_size =3D memory_region_size(&vmem->memdev->mr); + + /* + * Determine the default block size now, to determine the minimum mems= lot + * size. We want the minimum slot size to be at least the device block= size. + */ + if (!vmem->block_size) { + vmem->block_size =3D virtio_mem_default_block_size(rb); + } + /* If realizing the device will fail, just assume a single memslot. */ + if (vmem->block_size < qemu_ram_pagesize(rb) || + !QEMU_IS_ALIGNED(region_size, vmem->block_size)) { + vmem->nb_memslots =3D 1; + return; + } + + /* + * All memslots except the last one have a reasonable minimum size, and + * and all memslot sizes are aligned to the device block size. + */ + memslot_size =3D QEMU_ALIGN_UP(region_size / limit, vmem->block_size); + min_memslot_size =3D MAX(vmem->block_size, VIRTIO_MEM_MIN_MEMSLOT_SIZE= ); + memslot_size =3D MAX(memslot_size, min_memslot_size); + + memslots =3D QEMU_ALIGN_UP(region_size, memslot_size) / memslot_size; + if (memslots !=3D 1) { + vmem->memslot_size =3D memslot_size; + } + vmem->nb_memslots =3D memslots; +} + +static unsigned int virtio_mem_get_memslots(VirtIOMEM *vmem) +{ + if (!vmem->dynamic_memslots) { + /* Exactly one static RAM memory region. */ + return 1; + } + + /* We're called after instructed to make a decision. */ + g_assert(vmem->nb_memslots); + return vmem->nb_memslots; +} + static void virtio_mem_add_size_change_notifier(VirtIOMEM *vmem, Notifier *notifier) { @@ -1393,6 +1661,21 @@ static void virtio_mem_instance_init(Object *obj) NULL, NULL); } =20 +static void virtio_mem_instance_finalize(Object *obj) +{ + VirtIOMEM *vmem =3D VIRTIO_MEM(obj); + + /* + * Note: the core already dropped the references on all memory regions + * (it's passed as the owner to memory_region_init_*()) and finalized + * these objects. We can simply free the memory. + */ + g_free(vmem->memslots); + vmem->memslots =3D NULL; + g_free(vmem->mr); + vmem->mr =3D NULL; +} + static Property virtio_mem_properties[] =3D { DEFINE_PROP_UINT64(VIRTIO_MEM_ADDR_PROP, VirtIOMEM, addr, 0), DEFINE_PROP_UINT32(VIRTIO_MEM_NODE_PROP, VirtIOMEM, node, 0), @@ -1405,6 +1688,8 @@ static Property virtio_mem_properties[] =3D { #endif DEFINE_PROP_BOOL(VIRTIO_MEM_EARLY_MIGRATION_PROP, VirtIOMEM, early_migration, true), + DEFINE_PROP_BOOL(VIRTIO_MEM_DYNAMIC_MEMSLOTS_PROP, VirtIOMEM, + dynamic_memslots, false), DEFINE_PROP_END_OF_LIST(), }; =20 @@ -1572,6 +1857,8 @@ static void virtio_mem_class_init(ObjectClass *klass,= void *data) =20 vmc->fill_device_info =3D virtio_mem_fill_device_info; vmc->get_memory_region =3D virtio_mem_get_memory_region; + vmc->decide_memslots =3D virtio_mem_decide_memslots; + vmc->get_memslots =3D virtio_mem_get_memslots; vmc->add_size_change_notifier =3D virtio_mem_add_size_change_notifier; vmc->remove_size_change_notifier =3D virtio_mem_remove_size_change_not= ifier; vmc->unplug_request_check =3D virtio_mem_unplug_request_check; @@ -1589,6 +1876,7 @@ static const TypeInfo virtio_mem_info =3D { .parent =3D TYPE_VIRTIO_DEVICE, .instance_size =3D sizeof(VirtIOMEM), .instance_init =3D virtio_mem_instance_init, + .instance_finalize =3D virtio_mem_instance_finalize, .class_init =3D virtio_mem_class_init, .class_size =3D sizeof(VirtIOMEMClass), .interfaces =3D (InterfaceInfo[]) { diff --git a/include/hw/virtio/virtio-mem.h b/include/hw/virtio/virtio-mem.h index ab0fe2b4f2..5f5b02b8f9 100644 --- a/include/hw/virtio/virtio-mem.h +++ b/include/hw/virtio/virtio-mem.h @@ -33,6 +33,7 @@ OBJECT_DECLARE_TYPE(VirtIOMEM, VirtIOMEMClass, #define VIRTIO_MEM_UNPLUGGED_INACCESSIBLE_PROP "unplugged-inaccessible" #define VIRTIO_MEM_EARLY_MIGRATION_PROP "x-early-migration" #define VIRTIO_MEM_PREALLOC_PROP "prealloc" +#define VIRTIO_MEM_DYNAMIC_MEMSLOTS_PROP "dynamic-memslots" =20 struct VirtIOMEM { VirtIODevice parent_obj; @@ -44,7 +45,28 @@ struct VirtIOMEM { int32_t bitmap_size; unsigned long *bitmap; =20 - /* assigned memory backend and memory region */ + /* + * With "dynamic-memslots=3Don": Device memory region in which we dyna= mically + * map the memslots. + */ + MemoryRegion *mr; + + /* + * With "dynamic-memslots=3Don": The individual memslots (aliases into= the + * memory backend). + */ + MemoryRegion *memslots; + + /* With "dynamic-memslots=3Don": The total number of memslots. */ + uint16_t nb_memslots; + + /* + * With "dynamic-memslots=3Don": Size of one memslot (the size of the + * last one can differ). + */ + uint64_t memslot_size; + + /* Assigned memory backend with the RAM memory region. */ HostMemoryBackend *memdev; =20 /* NUMA node */ @@ -82,6 +104,12 @@ struct VirtIOMEM { */ bool early_migration; =20 + /* + * Whether we dynamically map (multiple, if possible) memslots instead= of + * statically mapping the whole RAM memory region. + */ + bool dynamic_memslots; + /* notifiers to notify when "size" changes */ NotifierList size_change_notifiers; =20 @@ -96,6 +124,8 @@ struct VirtIOMEMClass { /* public */ void (*fill_device_info)(const VirtIOMEM *vmen, VirtioMEMDeviceInfo *v= i); MemoryRegion *(*get_memory_region)(VirtIOMEM *vmem, Error **errp); + void (*decide_memslots)(VirtIOMEM *vmem, unsigned int limit); + unsigned int (*get_memslots)(VirtIOMEM *vmem); void (*add_size_change_notifier)(VirtIOMEM *vmem, Notifier *notifier); void (*remove_size_change_notifier)(VirtIOMEM *vmem, Notifier *notifie= r); void (*unplug_request_check)(VirtIOMEM *vmem, Error **errp); --=20 2.41.0 From nobody Wed May 15 09:35:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1695754962097155.25244807617446; Tue, 26 Sep 2023 12:02:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDIC-00026t-W7; Tue, 26 Sep 2023 15:00:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDI1-0001q0-8Q for qemu-devel@nongnu.org; Tue, 26 Sep 2023 15:00:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDHz-0003TH-3X for qemu-devel@nongnu.org; Tue, 26 Sep 2023 15:00:08 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-460-iRB-Y2SJOiueZ_L_n-LBqQ-1; Tue, 26 Sep 2023 15:00:02 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4D4FC8007A4; Tue, 26 Sep 2023 19:00:02 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id F16802026D4B; Tue, 26 Sep 2023 18:59:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754806; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gj1qc7LgFRuhKwjikQ31GZzbWw6ASlcmaBs3JdeJeGk=; b=J3Lb2uiqjs9fjh0xlfIPMt0RwwlWutme9ECUFPm6NH+eg/9yFDLChRktMjCJwh9FlOg74n oiKBYsAvB+mH9V05vA+7J8IhJrrqBry6uyN6Ndw+riNnunTi4polq+SgaSxq5KNFK+pPjR LF+t8mqkF1ScpIodThPlbotspwsK9Ak= X-MC-Unique: iRB-Y2SJOiueZ_L_n-LBqQ-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org Subject: [PATCH v4 17/18] memory, vhost: Allow for marking memory device memory regions unmergeable Date: Tue, 26 Sep 2023 20:57:37 +0200 Message-ID: <20230926185738.277351-18-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1695754963130100001 Let's allow for marking memory regions unmergeable, to teach flatview code and vhost to not merge adjacent aliases to the same memory region into a larger memory section; instead, we want separate aliases to stay separate such that we can atomically map/unmap aliases without affecting other aliases. This is desired for virtio-mem mapping device memory located on a RAM memory region via multiple aliases into a memory region container, resulting in separate memslots that can get (un)mapped atomically. As an example with virtio-mem, the layout would look something like this: [...] 0000000240000000-00000020bfffffff (prio 0, i/o): device-memory 0000000240000000-000000043fffffff (prio 0, i/o): virtio-mem 0000000240000000-000000027fffffff (prio 0, ram): alias memslot-0 @mem= 2 0000000000000000-000000003fffffff 0000000280000000-00000002bfffffff (prio 0, ram): alias memslot-1 @mem= 2 0000000040000000-000000007fffffff 00000002c0000000-00000002ffffffff (prio 0, ram): alias memslot-2 @mem= 2 0000000080000000-00000000bfffffff [...] Without unmergable memory regions, all three memslots would get merged into a single memory section. For example, when mapping another alias (e.g., virtio-mem-memslot-3) or when unmapping any of the mapped aliases, memory listeners will first get notified about the removal of the big memory section to then get notified about re-adding of the new (differently merged) memory section(s). In an ideal world, memory listeners would be able to deal with that atomically, like KVM nowadays does. However, (a) supporting this for other memory listeners (vhost-user, vfio) is fairly hard: temporary removal can result in all kinds of issues on concurrent access to guest memory; and (b) this handling is undesired, because temporarily removing+readding can consume quite some time on bigger memslots and is not efficient (e.g., vfio unpinning and repinning pages ...). Let's allow for marking a memory region unmergeable, such that we can atomically (un)map aliases to the same memory region, similar to (un)mapping individual DIMMs. Similarly, teach vhost code to not redo what flatview core stopped doing: don't merge such sections. Merging in vhost code is really only relevant for handling random holes in boot memory where; without this merging, the vhost-user backend wouldn't be able to mmap() some boot memory backed on hugetlb. We'll use this for virtio-mem next. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/virtio/vhost.c | 4 ++-- include/exec/memory.h | 22 ++++++++++++++++++++++ softmmu/memory.c | 31 +++++++++++++++++++++++++------ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 24013b39d6..503a160c96 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -707,7 +707,7 @@ static void vhost_region_add_section(struct vhost_dev *= dev, mrs_size, mrs_host); } =20 - if (dev->n_tmp_sections) { + if (dev->n_tmp_sections && !section->unmergeable) { /* Since we already have at least one section, lets see if * this extends it; since we're scanning in order, we only * have to look at the last one, and the FlatView that calls @@ -740,7 +740,7 @@ static void vhost_region_add_section(struct vhost_dev *= dev, size_t offset =3D mrs_gpa - prev_gpa_start; =20 if (prev_host_start + offset =3D=3D mrs_host && - section->mr =3D=3D prev_sec->mr) { + section->mr =3D=3D prev_sec->mr && !prev_sec->unmergeable)= { uint64_t max_end =3D MAX(prev_host_end, mrs_host + mrs_siz= e); need_add =3D false; prev_sec->offset_within_address_space =3D diff --git a/include/exec/memory.h b/include/exec/memory.h index 73062edf49..38b22b4fe0 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -95,6 +95,7 @@ struct ReservedRegion { * relative to the region's address space * @readonly: writes to this section are ignored * @nonvolatile: this section is non-volatile + * @unmergeable: this section should not get merged with adjacent sections */ struct MemoryRegionSection { Int128 size; @@ -104,6 +105,7 @@ struct MemoryRegionSection { hwaddr offset_within_address_space; bool readonly; bool nonvolatile; + bool unmergeable; }; =20 typedef struct IOMMUTLBEntry IOMMUTLBEntry; @@ -773,6 +775,7 @@ struct MemoryRegion { bool nonvolatile; bool rom_device; bool flush_coalesced_mmio; + bool unmergeable; uint8_t dirty_log_mask; bool is_iommu; RAMBlock *ram_block; @@ -2351,6 +2354,25 @@ void memory_region_set_size(MemoryRegion *mr, uint64= _t size); void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset); =20 +/* + * memory_region_set_unmergeable: Set a memory region unmergeable + * + * Mark a memory region unmergeable, resulting in the memory region (or + * everything contained in a memory region container) not getting merged w= hen + * simplifying the address space and notifying memory listeners. Consequen= tly, + * memory listeners will never get notified about ranges that are larger t= han + * the original memory regions. + * + * This is primarily useful when multiple aliases to a RAM memory region a= re + * mapped into a memory region container, and updates (e.g., enable/disabl= e or + * map/unmap) of individual memory region aliases are not supposed to affe= ct + * other memory regions in the same container. + * + * @mr: the #MemoryRegion to be updated + * @unmergeable: whether to mark the #MemoryRegion unmergeable + */ +void memory_region_set_unmergeable(MemoryRegion *mr, bool unmergeable); + /** * memory_region_present: checks if an address relative to a @container * translates into #MemoryRegion within @container diff --git a/softmmu/memory.c b/softmmu/memory.c index 9d491d488b..8125eac927 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -224,6 +224,7 @@ struct FlatRange { bool romd_mode; bool readonly; bool nonvolatile; + bool unmergeable; }; =20 #define FOR_EACH_FLAT_RANGE(var, view) \ @@ -240,6 +241,7 @@ section_from_flat_range(FlatRange *fr, FlatView *fv) .offset_within_address_space =3D int128_get64(fr->addr.start), .readonly =3D fr->readonly, .nonvolatile =3D fr->nonvolatile, + .unmergeable =3D fr->unmergeable, }; } =20 @@ -250,7 +252,8 @@ static bool flatrange_equal(FlatRange *a, FlatRange *b) && a->offset_in_region =3D=3D b->offset_in_region && a->romd_mode =3D=3D b->romd_mode && a->readonly =3D=3D b->readonly - && a->nonvolatile =3D=3D b->nonvolatile; + && a->nonvolatile =3D=3D b->nonvolatile + && a->unmergeable =3D=3D b->unmergeable; } =20 static FlatView *flatview_new(MemoryRegion *mr_root) @@ -323,7 +326,8 @@ static bool can_merge(FlatRange *r1, FlatRange *r2) && r1->dirty_log_mask =3D=3D r2->dirty_log_mask && r1->romd_mode =3D=3D r2->romd_mode && r1->readonly =3D=3D r2->readonly - && r1->nonvolatile =3D=3D r2->nonvolatile; + && r1->nonvolatile =3D=3D r2->nonvolatile + && !r1->unmergeable && !r2->unmergeable; } =20 /* Attempt to simplify a view by merging adjacent ranges */ @@ -599,7 +603,8 @@ static void render_memory_region(FlatView *view, Int128 base, AddrRange clip, bool readonly, - bool nonvolatile) + bool nonvolatile, + bool unmergeable) { MemoryRegion *subregion; unsigned i; @@ -616,6 +621,7 @@ static void render_memory_region(FlatView *view, int128_addto(&base, int128_make64(mr->addr)); readonly |=3D mr->readonly; nonvolatile |=3D mr->nonvolatile; + unmergeable |=3D mr->unmergeable; =20 tmp =3D addrrange_make(base, mr->size); =20 @@ -629,14 +635,14 @@ static void render_memory_region(FlatView *view, int128_subfrom(&base, int128_make64(mr->alias->addr)); int128_subfrom(&base, int128_make64(mr->alias_offset)); render_memory_region(view, mr->alias, base, clip, - readonly, nonvolatile); + readonly, nonvolatile, unmergeable); return; } =20 /* Render subregions in priority order. */ QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) { render_memory_region(view, subregion, base, clip, - readonly, nonvolatile); + readonly, nonvolatile, unmergeable); } =20 if (!mr->terminates) { @@ -652,6 +658,7 @@ static void render_memory_region(FlatView *view, fr.romd_mode =3D mr->romd_mode; fr.readonly =3D readonly; fr.nonvolatile =3D nonvolatile; + fr.unmergeable =3D unmergeable; =20 /* Render the region itself into any gaps left by the current view. */ for (i =3D 0; i < view->nr && int128_nz(remain); ++i) { @@ -753,7 +760,7 @@ static FlatView *generate_memory_topology(MemoryRegion = *mr) if (mr) { render_memory_region(view, mr, int128_zero(), addrrange_make(int128_zero(), int128_2_64()), - false, false); + false, false, false); } flatview_simplify(view); =20 @@ -2755,6 +2762,18 @@ void memory_region_set_alias_offset(MemoryRegion *mr= , hwaddr offset) memory_region_transaction_commit(); } =20 +void memory_region_set_unmergeable(MemoryRegion *mr, bool unmergeable) +{ + if (unmergeable =3D=3D mr->unmergeable) { + return; + } + + memory_region_transaction_begin(); + mr->unmergeable =3D unmergeable; + memory_region_update_pending |=3D mr->enabled; + memory_region_transaction_commit(); +} + uint64_t memory_region_get_alignment(const MemoryRegion *mr) { return mr->align; --=20 2.41.0 From nobody Wed May 15 09:35:24 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=1695754887; cv=none; d=zohomail.com; s=zohoarc; b=mTtPbh/MS6Z2g6aSK5DbdjTUSAPEJnyP0WEJg59y+kq/7Bd4y0ctr1PWkqcFpN/APWeYoaaduBNSygyIvFuRpBbt917KEgEUPXhbPZDLsw0T6mJCTMJOUWehgYj+G1E3qHK8l8v0Ts1P4qbhVBA/SDqZDY9/kaOPh4FoehO2ZF8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1695754887; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=jAvglJj91/o8R3ulPUvMP06jx5ezTDNURc+rL2BIGo4=; b=DecIIl+FIP2D7emgm7NgYp7QeacvqA7KShleCZanYWEpsXJHaLBoleQOaIJFlWJ1zZNmmfI/i0fOwJxsgBNUp7/ghi7ELG00UwwqPvvWMzOd8jIORvgbU/B/z6F88qF0YVmM5EQM/GzRXxY89O1rmYBpLUwWdjAr7vy+A4hQ9Ok= 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 1695754887485968.2991461329835; Tue, 26 Sep 2023 12:01:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlDIJ-0002M2-UY; Tue, 26 Sep 2023 15:00:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDI8-0001to-8e for qemu-devel@nongnu.org; Tue, 26 Sep 2023 15:00:16 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qlDI4-0003Uq-Ix for qemu-devel@nongnu.org; Tue, 26 Sep 2023 15:00:15 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-281-t_nnSmVvNL-nvOoa5tOcxg-1; Tue, 26 Sep 2023 15:00:07 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1DE3E811E8E; Tue, 26 Sep 2023 19:00:07 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id B9B372026D4B; Tue, 26 Sep 2023 19:00:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695754811; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jAvglJj91/o8R3ulPUvMP06jx5ezTDNURc+rL2BIGo4=; b=hR12PzKXXvgb//rP8/f7FQccH0FZyKQbaawQnhzy2V+TVcxtJRRVw2KKXx+SYqNZOyT8Ro 5EE4TTfACEKDkiFVFP0UbCGnrIPUetn+m6flyA9GOQMdfJ+qEczZxyWzHo4ju6C7Z+Qs/v KcrmJg5mJ+eLK0qbyMEAxfuXirZxXhg= X-MC-Unique: t_nnSmVvNL-nvOoa5tOcxg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Igor Mammedov , Xiao Guangrong , "Michael S. Tsirkin" , Peter Xu , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Michal Privoznik , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Gavin Shan , Alex Williamson , Stefan Hajnoczi , "Maciej S . Szmigiero" , kvm@vger.kernel.org Subject: [PATCH v4 18/18] virtio-mem: Mark memslot alias memory regions unmergeable Date: Tue, 26 Sep 2023 20:57:38 +0200 Message-ID: <20230926185738.277351-19-david@redhat.com> In-Reply-To: <20230926185738.277351-1-david@redhat.com> References: <20230926185738.277351-1-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1695754888836100003 Let's mark the memslot alias memory regions as unmergable, such that flatview and vhost won't merge adjacent memory region aliases and we can atomically map/unmap individual aliases without affecting adjacent alias memory regions. This handles vhost and vfio in multiple-memslot mode correctly (which do not support atomic memslot updates) and avoids the temporary removal of large memslots, which can be an expensive operation. For example, vfio might have to unpin + repin a lot of memory, which is undesired. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: David Hildenbrand Reviewed-by: Michael S. Tsirkin --- hw/virtio/virtio-mem.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index e1e4250e69..9dc3c61b5a 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -940,6 +940,12 @@ static void virtio_mem_prepare_memslots(VirtIOMEM *vme= m) memory_region_init_alias(&vmem->memslots[idx], OBJECT(vmem), name, &vmem->memdev->mr, memslot_offset, memslot_size); + /* + * We want to be able to atomically and efficiently activate/deact= ivate + * individual memslots without affecting adjacent memslots in memo= ry + * notifiers. + */ + memory_region_set_unmergeable(&vmem->memslots[idx], true); } } =20 --=20 2.41.0