From nobody Mon Feb 9 01:16:13 2026 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 ARC-Seal: i=1; a=rsa-sha256; t=1600965599; cv=none; d=zohomail.com; s=zohoarc; b=ER0o3KtdqMpUyXEJnCwEhSQ2xZDT4R3Er+xDCTUEhn4muF36xo+hpydqUlddc9IQDfBthBHbSVxjpcIRJU5gatTw8IBoJpMOvvHSq0yVMCoqQgBwJd4UeI8v+BOlUNBssriDGUfQboPYXf1CU/vpJVeti9RFGgn7Ou5Wv6DGQlE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600965599; 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=T/ZKOQNJcI2maVD2pHtRKj9D8ra+xSOLD0oKJHjr4E4=; b=MWQMZRZ3ZTbES65K5MQOrggi1W+QaAOsEfw4dlKDQAV9QdhJ7I92QYglJn8G+q5XOy1pKeGSh3I7kiNY5o0pEmv6MUN+ahuOFc6qHkGhCAIYW82C4Ke3aTVNE1IXML7JLwmz00p5Yh2pSSyBY6GO/VcoE/ahoM+ZCDUOQ9hMD6E= ARC-Authentication-Results: i=1; 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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1600965599699318.6726688107466; Thu, 24 Sep 2020 09:39:59 -0700 (PDT) Received: from localhost ([::1]:50804 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kLUHm-00074L-9F for importer@patchew.org; Thu, 24 Sep 2020 12:39:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57424) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kLTkN-0002Ds-MF for qemu-devel@nongnu.org; Thu, 24 Sep 2020 12:05:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:31094) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kLTkF-0003bq-LH for qemu-devel@nongnu.org; Thu, 24 Sep 2020 12:05:27 -0400 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-240-t5RGKrAvOdmNOHXtgOY4HA-1; Thu, 24 Sep 2020 12:05:12 -0400 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 D29AA18BA284; Thu, 24 Sep 2020 16:05:11 +0000 (UTC) Received: from t480s.redhat.com (ovpn-114-4.ams2.redhat.com [10.36.114.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id A058F19C66; Thu, 24 Sep 2020 16:05:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600963514; 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=T/ZKOQNJcI2maVD2pHtRKj9D8ra+xSOLD0oKJHjr4E4=; b=OSigXw2CqvQFQh1XQHIBzCPab6fEzhDUMR9C8WdC8thlGVdbO8XV5e/Pr7gwolXq2UbPXX X2CL3Up5SJHdB5WkZl3yOcTjGUyQIZKP3FHfS1WlFp/Sh7zDq5LeJKTrIepON72IGH9bBE h6CWd1JUux+a5MwdCuhUtUXTq3feNqs= X-MC-Unique: t5RGKrAvOdmNOHXtgOY4HA-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH PROTOTYPE 2/6] virtio-mem: Impelement SparseRAMHandler interface Date: Thu, 24 Sep 2020 18:04:19 +0200 Message-Id: <20200924160423.106747-3-david@redhat.com> In-Reply-To: <20200924160423.106747-1-david@redhat.com> References: <20200924160423.106747-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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=63.128.21.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/24 01:10:00 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -32 X-Spam_score: -3.3 X-Spam_bar: --- X-Spam_report: (-3.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.199, 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 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pankaj Gupta , David Hildenbrand , "Michael S. Tsirkin" , "Dr . David Alan Gilbert" , Peter Xu , Luiz Capitulino , Auger Eric , Alex Williamson , Wei Yang , Igor Mammedov , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Let's properly notify when (un)plugging blocks. Handle errors from notifiers gracefully when mapping, rolling back the change and telling the guest that the VM is busy. Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Wei Yang Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Signed-off-by: David Hildenbrand --- hw/virtio/virtio-mem.c | 158 ++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-mem.h | 3 + 2 files changed, 160 insertions(+), 1 deletion(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 8fbec77ccc..e23969eaed 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -72,6 +72,64 @@ static bool virtio_mem_is_busy(void) return migration_in_incoming_postcopy() || !migration_is_idle(); } =20 +static void virtio_mem_srh_notify_unmap(VirtIOMEM *vmem, uint64_t offset, + uint64_t size) +{ + SparseRAMNotifier *notifier; + + QLIST_FOREACH(notifier, &vmem->sram_notify, next) { + notifier->notify_unmap(notifier, &vmem->memdev->mr, offset, size); + } +} + +static int virtio_mem_srh_notify_map(VirtIOMEM *vmem, uint64_t offset, + uint64_t size) +{ + SparseRAMNotifier *notifier, *notifier2; + int ret =3D 0; + + QLIST_FOREACH(notifier, &vmem->sram_notify, next) { + ret =3D notifier->notify_map(notifier, &vmem->memdev->mr, offset, = size); + if (ret) { + break; + } + } + + /* In case any notifier failed, undo the whole operation. */ + if (ret) { + QLIST_FOREACH(notifier2, &vmem->sram_notify, next) { + if (notifier2 =3D=3D notifier) { + break; + } + notifier2->notify_unmap(notifier2, &vmem->memdev->mr, offset, = size); + } + } + return ret; +} + +/* + * TODO: Maybe we could notify directly that everything is unmapped/discar= ded. + * at least vfio should be able to deal with that. + */ +static void virtio_mem_srh_notify_unplug_all(VirtIOMEM *vmem) +{ + unsigned long first_zero_bit, last_zero_bit; + uint64_t offset, length; + + /* Find consecutive unplugged blocks and notify */ + first_zero_bit =3D find_first_zero_bit(vmem->bitmap, vmem->bitmap_size= ); + while (first_zero_bit < vmem->bitmap_size) { + offset =3D first_zero_bit * vmem->block_size; + last_zero_bit =3D find_next_bit(vmem->bitmap, vmem->bitmap_size, + first_zero_bit + 1) - 1; + length =3D (last_zero_bit - first_zero_bit + 1) * vmem->block_size; + + virtio_mem_srh_notify_unmap(vmem, offset, length); + first_zero_bit =3D find_next_zero_bit(vmem->bitmap, vmem->bitmap_s= ize, + last_zero_bit + 2); + } +} + static bool virtio_mem_test_bitmap(VirtIOMEM *vmem, uint64_t start_gpa, uint64_t size, bool plugged) { @@ -146,7 +204,7 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem, = uint64_t start_gpa, uint64_t size, bool plug) { const uint64_t offset =3D start_gpa - vmem->addr; - int ret; + int ret, ret2; =20 if (virtio_mem_is_busy()) { return -EBUSY; @@ -159,6 +217,23 @@ static int virtio_mem_set_block_state(VirtIOMEM *vmem,= uint64_t start_gpa, strerror(-ret)); return -EBUSY; } + /* + * We'll notify *after* discarding succeeded, because we might not= be + * able to map again ... + */ + virtio_mem_srh_notify_unmap(vmem, offset, size); + } else if (virtio_mem_srh_notify_map(vmem, offset, size)) { + /* + * Could be a mapping attempt already already resulted in memory + * getting populated. + */ + ret2 =3D ram_block_discard_range(vmem->memdev->mr.ram_block, offse= t, + size); + if (ret2) { + error_report("Unexpected error discarding RAM: %s", + strerror(-ret2)); + } + return -EBUSY; } virtio_mem_set_bitmap(vmem, start_gpa, size, plug); return 0; @@ -253,6 +328,8 @@ static int virtio_mem_unplug_all(VirtIOMEM *vmem) error_report("Unexpected error discarding RAM: %s", strerror(-ret)= ); return -EBUSY; } + virtio_mem_srh_notify_unplug_all(vmem); + bitmap_clear(vmem->bitmap, 0, vmem->bitmap_size); if (vmem->size) { vmem->size =3D 0; @@ -480,6 +557,13 @@ static void virtio_mem_device_realize(DeviceState *dev= , Error **errp) vmstate_register_ram(&vmem->memdev->mr, DEVICE(vmem)); qemu_register_reset(virtio_mem_system_reset, vmem); precopy_add_notifier(&vmem->precopy_notifier); + + /* + * Set it to sparse, so everybody is aware of it before the plug handl= er + * exposes the region to the system. + */ + memory_region_set_sparse_ram_handler(&vmem->memdev->mr, + SPARSE_RAM_HANDLER(vmem)); } =20 static void virtio_mem_device_unrealize(DeviceState *dev) @@ -487,6 +571,7 @@ static void virtio_mem_device_unrealize(DeviceState *de= v) VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); VirtIOMEM *vmem =3D VIRTIO_MEM(dev); =20 + memory_region_set_sparse_ram_handler(&vmem->memdev->mr, NULL); precopy_remove_notifier(&vmem->precopy_notifier); qemu_unregister_reset(virtio_mem_system_reset, vmem); vmstate_unregister_ram(&vmem->memdev->mr, DEVICE(vmem)); @@ -813,6 +898,7 @@ static void virtio_mem_instance_init(Object *obj) vmem->block_size =3D VIRTIO_MEM_MIN_BLOCK_SIZE; notifier_list_init(&vmem->size_change_notifiers); vmem->precopy_notifier.notify =3D virtio_mem_precopy_notify; + QLIST_INIT(&vmem->sram_notify); =20 object_property_add(obj, VIRTIO_MEM_SIZE_PROP, "size", virtio_mem_get_= size, NULL, NULL, NULL); @@ -832,11 +918,72 @@ static Property virtio_mem_properties[] =3D { DEFINE_PROP_END_OF_LIST(), }; =20 +static uint64_t virtio_mem_srh_get_granularity(const SparseRAMHandler *srh, + const MemoryRegion *mr) +{ + const VirtIOMEM *vmem =3D VIRTIO_MEM(srh); + + g_assert(mr =3D=3D &vmem->memdev->mr); + return vmem->block_size; +} + +static void virtio_mem_srh_register_listener(SparseRAMHandler *srh, + const MemoryRegion *mr, + SparseRAMNotifier *notifier) +{ + VirtIOMEM *vmem =3D VIRTIO_MEM(srh); + + g_assert(mr =3D=3D &vmem->memdev->mr); + QLIST_INSERT_HEAD(&vmem->sram_notify, notifier, next); +} + +static void virtio_mem_srh_unregister_listener(SparseRAMHandler *srh, + const MemoryRegion *mr, + SparseRAMNotifier *notifier) +{ + VirtIOMEM *vmem =3D VIRTIO_MEM(srh); + + g_assert(mr =3D=3D &vmem->memdev->mr); + QLIST_REMOVE(notifier, next); +} + +static int virtio_mem_srh_replay_mapped(SparseRAMHandler *srh, + const MemoryRegion *mr, + SparseRAMNotifier *notifier) +{ + VirtIOMEM *vmem =3D VIRTIO_MEM(srh); + unsigned long first_bit, last_bit; + uint64_t offset, length; + int ret =3D 0; + + g_assert(mr =3D=3D &vmem->memdev->mr); + + /* Find consecutive plugged blocks and notify */ + first_bit =3D find_first_bit(vmem->bitmap, vmem->bitmap_size); + while (first_bit < vmem->bitmap_size) { + offset =3D first_bit * vmem->block_size; + last_bit =3D find_next_zero_bit(vmem->bitmap, vmem->bitmap_size, + first_bit + 1) - 1; + length =3D (last_bit - first_bit + 1) * vmem->block_size; + + ret =3D notifier->notify_map(notifier, mr, offset, length); + if (ret) { + break; + } + first_bit =3D find_next_bit(vmem->bitmap, vmem->bitmap_size, + last_bit + 2); + } + + /* TODO: cleanup on error if necessary. */ + return ret; +} + static void virtio_mem_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); VirtioDeviceClass *vdc =3D VIRTIO_DEVICE_CLASS(klass); VirtIOMEMClass *vmc =3D VIRTIO_MEM_CLASS(klass); + SparseRAMHandlerClass *srhc =3D SPARSE_RAM_HANDLER_CLASS(klass); =20 device_class_set_props(dc, virtio_mem_properties); dc->vmsd =3D &vmstate_virtio_mem; @@ -852,6 +999,11 @@ static void virtio_mem_class_init(ObjectClass *klass, = void *data) vmc->get_memory_region =3D virtio_mem_get_memory_region; 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; + + srhc->get_granularity =3D virtio_mem_srh_get_granularity; + srhc->register_listener =3D virtio_mem_srh_register_listener; + srhc->unregister_listener =3D virtio_mem_srh_unregister_listener; + srhc->replay_mapped =3D virtio_mem_srh_replay_mapped; } =20 static const TypeInfo virtio_mem_info =3D { @@ -861,6 +1013,10 @@ static const TypeInfo virtio_mem_info =3D { .instance_init =3D virtio_mem_instance_init, .class_init =3D virtio_mem_class_init, .class_size =3D sizeof(VirtIOMEMClass), + .interfaces =3D (InterfaceInfo[]) { + { TYPE_SPARSE_RAM_HANDLER }, + { } + }, }; =20 static void virtio_register_types(void) diff --git a/include/hw/virtio/virtio-mem.h b/include/hw/virtio/virtio-mem.h index 4eeb82d5dd..91d9b48ba0 100644 --- a/include/hw/virtio/virtio-mem.h +++ b/include/hw/virtio/virtio-mem.h @@ -67,6 +67,9 @@ struct VirtIOMEM { =20 /* don't migrate unplugged memory */ NotifierWithReturn precopy_notifier; + + /* SparseRAMNotifier list to be notified on plug/unplug events. */ + QLIST_HEAD(, SparseRAMNotifier) sram_notify; }; =20 struct VirtIOMEMClass { --=20 2.26.2