From nobody Tue Feb 10 15:01:32 2026 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=1687523021; cv=none; d=zohomail.com; s=zohoarc; b=Dko2g/g1WDpI6jfaQ0XFBdJhxI5hKuiAYVUEGCE3tZypgKUMiPnxIyW0QSHXwwpa645mBnS90JyVrKNQnZlC9xoZ3/arI5XIN0hTVLmpG12zat/w0mk5P+oQn0Ywhg8YSok8L/PnqNjRTlF+Lk4+1zwiGHi2fWGW1+wKoYBt994= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1687523021; 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=xvAOyyso/d96OeLCp/LopB3F79UeURE4NzHYAGLndjw=; b=legedIo4Fu8D2x6OAvUbhE8ev2Edsm1mqoZqOEpe9nOyx90877LZDD5OAeLnsxDloU5jYsE3NxJeuSdJicTr634OJ9Jfzpv+0OCIVWHlKVhYxSAER3kEQrz6StX1sFachsaZA+iOY4eevE1jbxRf8Smfykcy15OCOsgzq8RlyTc= 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 1687523021789946.2415820189212; Fri, 23 Jun 2023 05:23:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qCfnk-00048F-1C; Fri, 23 Jun 2023 08:22:08 -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 1qCfn7-0003rc-GN for qemu-devel@nongnu.org; Fri, 23 Jun 2023 08:21:33 -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 1qCfn3-0003pa-BN for qemu-devel@nongnu.org; Fri, 23 Jun 2023 08:21:27 -0400 Received: from mimecast-mx02.redhat.com (mx3-rdu2.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-33-YuGVt_8WMzahFY1LUjcuLg-1; Fri, 23 Jun 2023 08:21:17 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CAAE13C23100; Fri, 23 Jun 2023 12:21:16 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.22.32.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id C58C340C2070; Fri, 23 Jun 2023 12:21:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687522879; 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=xvAOyyso/d96OeLCp/LopB3F79UeURE4NzHYAGLndjw=; b=P64X+uUqlBJF10h2FwzzFce+n2qyUuk7gLyF8tfRKalF+ACrkvyVRwHx0WI3JZIINbV/82 5+DgrXt2o5NK2QflpcdFWxekJNCtpyUOo8U5rvooL+2S9xj1SV2AQCACdVDDwuWMwojDd1 kUCh8jleL5hkaMU5QXW9xdwMAOmYwb0= X-MC-Unique: YuGVt_8WMzahFY1LUjcuLg-1 From: David Hildenbrand To: qemu-devel@nongnu.org Cc: David Hildenbrand , Peter Maydell , Paolo Bonzini , Richard Henderson , Eduardo Habkost , "Michael S. Tsirkin" , Marcel Apfelbaum , Igor Mammedov , qemu-arm@nongnu.org, Gavin Shan Subject: [PATCH v2 7/7] virtio-mem-pci: Device unplug support Date: Fri, 23 Jun 2023 14:20:56 +0200 Message-Id: <20230623122056.374312-8-david@redhat.com> In-Reply-To: <20230623122056.374312-1-david@redhat.com> References: <20230623122056.374312-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1687523022823100009 Content-Type: text/plain; charset="utf-8" Let's support device unplug by forwarding the unplug_request_check() callback to the virtio-mem device. Further, disallow changing the requested-size once an unplug request is pending. Disallowing requested-size changes handles corner cases such as (1) pausing the VM (2) requesting device unplug and (3) adjusting the requested size. If the VM would plug memory (due to the requested size change) before processing the unplug request, we would be in trouble. Signed-off-by: David Hildenbrand --- hw/virtio/virtio-mem-pci.c | 49 +++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/hw/virtio/virtio-mem-pci.c b/hw/virtio/virtio-mem-pci.c index 2ef0f07630..c4597e029e 100644 --- a/hw/virtio/virtio-mem-pci.c +++ b/hw/virtio/virtio-mem-pci.c @@ -93,12 +93,53 @@ static void virtio_mem_pci_size_change_notify(Notifier = *notifier, void *data) g_free(qom_path); } =20 +static void virtio_mem_pci_unplug_request_check(VirtIOMDPCI *vmd, Error **= errp) +{ + VirtIOMEMPCI *pci_mem =3D VIRTIO_MEM_PCI(vmd); + VirtIOMEM *vmem =3D &pci_mem->vdev; + VirtIOMEMClass *vpc =3D VIRTIO_MEM_GET_CLASS(vmem); + + vpc->unplug_request_check(vmem, errp); +} + +static void virtio_mem_pci_get_requested_size(Object *obj, Visitor *v, + const char *name, void *opaq= ue, + Error **errp) +{ + VirtIOMEMPCI *pci_mem =3D VIRTIO_MEM_PCI(obj); + + object_property_get(OBJECT(&pci_mem->vdev), name, v, errp); +} + +static void virtio_mem_pci_set_requested_size(Object *obj, Visitor *v, + const char *name, void *opaq= ue, + Error **errp) +{ + VirtIOMEMPCI *pci_mem =3D VIRTIO_MEM_PCI(obj); + DeviceState *dev =3D DEVICE(obj); + + /* + * If we passed virtio_mem_pci_unplug_request_check(), making sure that + * the requested size is 0, don't allow modifying the requested size + * anymore, otherwise the VM might end up hotplugging memory before + * handling the unplug request. + */ + if (dev->pending_deleted_event) { + error_setg(errp, "'%s' cannot be changed if the device is in the" + " process of unplug", name); + return; + } + + object_property_set(OBJECT(&pci_mem->vdev), name, v, errp); +} + static void virtio_mem_pci_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); VirtioPCIClass *k =3D VIRTIO_PCI_CLASS(klass); PCIDeviceClass *pcidev_k =3D PCI_DEVICE_CLASS(klass); MemoryDeviceClass *mdc =3D MEMORY_DEVICE_CLASS(klass); + VirtIOMDPCIClass *vmdc =3D VIRTIO_MD_PCI_CLASS(klass); =20 k->realize =3D virtio_mem_pci_realize; set_bit(DEVICE_CATEGORY_MISC, dc->categories); @@ -111,6 +152,8 @@ static void virtio_mem_pci_class_init(ObjectClass *klas= s, void *data) mdc->get_memory_region =3D virtio_mem_pci_get_memory_region; mdc->fill_device_info =3D virtio_mem_pci_fill_device_info; mdc->get_min_alignment =3D virtio_mem_pci_get_min_alignment; + + vmdc->unplug_request_check =3D virtio_mem_pci_unplug_request_check; } =20 static void virtio_mem_pci_instance_init(Object *obj) @@ -135,9 +178,9 @@ static void virtio_mem_pci_instance_init(Object *obj) OBJECT(&dev->vdev), VIRTIO_MEM_BLOCK_SIZE_PR= OP); object_property_add_alias(obj, VIRTIO_MEM_SIZE_PROP, OBJECT(&dev->vdev= ), VIRTIO_MEM_SIZE_PROP); - object_property_add_alias(obj, VIRTIO_MEM_REQUESTED_SIZE_PROP, - OBJECT(&dev->vdev), - VIRTIO_MEM_REQUESTED_SIZE_PROP); + object_property_add(obj, VIRTIO_MEM_REQUESTED_SIZE_PROP, "size", + virtio_mem_pci_get_requested_size, + virtio_mem_pci_set_requested_size, NULL, NULL); } =20 static const VirtioPCIDeviceTypeInfo virtio_mem_pci_info =3D { --=20 2.40.1