From patchwork Wed Oct 27 12:45:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 12587243 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 109A5C433FE for ; Wed, 27 Oct 2021 12:46:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E16756109D for ; Wed, 27 Oct 2021 12:46:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242003AbhJ0Msr (ORCPT ); Wed, 27 Oct 2021 08:48:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:24020 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235839AbhJ0Msn (ORCPT ); Wed, 27 Oct 2021 08:48:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635338772; 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=ETMqw/34hByQcj56CeaZZMN6SPT1/xyTSjcuShEUBz4=; b=UXpdNuOMDtA9qohD43OcW3xFYH1P40P8Yjw/n8s9hiZTxE+g9D/FJCj2PKg5k2NYnYj8dX wD/m+4nY2oMDNPOw7lGts5rZgYzqWcR35wOi5grUx1ujFMY095tTANiKymqMy0mrvS67eL ZHzRYlmdKEBcgF/oJWQGGgTkj9T5USQ= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-192-vWcmnyppOC2TTOgvaTYddA-1; Wed, 27 Oct 2021 08:46:09 -0400 X-MC-Unique: vWcmnyppOC2TTOgvaTYddA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 31A8019200C1; Wed, 27 Oct 2021 12:46:08 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.193.176]) by smtp.corp.redhat.com (Postfix) with ESMTP id E2FDB1A7E9; Wed, 27 Oct 2021 12:46:04 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Paolo Bonzini , Eduardo Habkost , Marcel Apfelbaum , "Michael S. Tsirkin" , Igor Mammedov , Ani Sinha , Peter Xu , "Dr . David Alan Gilbert" , Stefan Hajnoczi , Richard Henderson , =?utf-8?q?Philippe_Mathie?= =?utf-8?q?u-Daud=C3=A9?= , Hui Zhu , Sebastien Boeuf , kvm@vger.kernel.org Subject: [PATCH v1 08/12] vhost: Respect reserved memslots for memory devices when realizing a vhost device Date: Wed, 27 Oct 2021 14:45:27 +0200 Message-Id: <20211027124531.57561-9-david@redhat.com> In-Reply-To: <20211027124531.57561-1-david@redhat.com> References: <20211027124531.57561-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Make sure that the current reservations can be fulfilled, otherwise we might run out of memslots later when memory devices start actually using the reserved memslots and crash. Signed-off-by: David Hildenbrand --- hw/virtio/vhost.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 49a1074097..a8f0d0bdf7 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -23,6 +23,7 @@ #include "standard-headers/linux/vhost_types.h" #include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-access.h" +#include "hw/mem/memory-device.h" #include "migration/blocker.h" #include "migration/qemu-file-types.h" #include "sysemu/dma.h" @@ -1319,7 +1320,7 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque, Error **errp) { uint64_t features; - int i, r, n_initialized_vqs = 0; + int i, r, reserved_memslots, backend_memslots, n_initialized_vqs = 0; hdev->vdev = NULL; hdev->migration_blocker = NULL; @@ -1415,9 +1416,13 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque, memory_listener_register(&hdev->memory_listener, &address_space_memory); QLIST_INSERT_HEAD(&vhost_devices, hdev, entry); - if (used_memslots > 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"); + reserved_memslots = memory_devices_get_reserved_memslots(); + backend_memslots = hdev->vhost_ops->vhost_backend_memslots_limit(hdev); + if (used_memslots + reserved_memslots > backend_memslots) { + error_setg(errp, "vhost backend memory slots limit (%d) is less" + " than current number of used (%d) and reserved (%d)" + " memory slots", backend_memslots, used_memslots, + reserved_memslots); r = -EINVAL; goto fail_busyloop; }