From nobody Tue Nov 18 16:48:48 2025 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=1611227408; cv=none; d=zohomail.com; s=zohoarc; b=B0SPBGF3978pLl5wBh0ZJn6R3iGtp6nMhZXrLM7edxNvtsxc5n4aPd9oe7kTqjXQ5z4e0N43/Boef7JpRcidx/pii9xmI1SBHAsa28MhZZ/dHtE81c4X/zwlFQeQKtAotT9Xn10oFWAM6nbluBaIRWbKzc/AiBUYuIjbz0gtUvU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611227408; 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=EHFdQA7aTBQT13RD0wAY2nXbum2oXJWh4vl0UvSD72U=; b=dsXXWnE5Cbwmuwhf+4rnZgSOoj12CF7osxrnbYHvAFF9hL3b3/O1uBuOfeMOdtY6Jrcx6DDwoL3++M0q6i9Xxg8S6oJRRFFWunQgjeLtebnUBcID0jg88jqsxn7vahEcoMmveDR2WXpaJRLA6pLsOCO7FbrG5SwA8gRcic6eE3U= 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 1611227408302466.13077535828916; Thu, 21 Jan 2021 03:10:08 -0800 (PST) Received: from localhost ([::1]:39460 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l2Xqn-00028X-4j for importer@patchew.org; Thu, 21 Jan 2021 06:10:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46926) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l2XnB-0006Ca-CD for qemu-devel@nongnu.org; Thu, 21 Jan 2021 06:06:21 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:44159) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l2Xn8-0006Tk-RL for qemu-devel@nongnu.org; Thu, 21 Jan 2021 06:06:21 -0500 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-232-tm8Y7tj_OCiIp20ple244w-1; Thu, 21 Jan 2021 06:06:16 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E5C63801817; Thu, 21 Jan 2021 11:06:14 +0000 (UTC) Received: from t480s.redhat.com (ovpn-115-70.ams2.redhat.com [10.36.115.70]) by smtp.corp.redhat.com (Postfix) with ESMTP id 600CE100AE3A; Thu, 21 Jan 2021 11:06:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611227178; 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=EHFdQA7aTBQT13RD0wAY2nXbum2oXJWh4vl0UvSD72U=; b=afmqt9IWT4qxOqNmvjKgQQ7+wmDodN1ZYrpuPuQLZaCQwTtLIGEKz+ZQ10w3c1AwNi2B2N 1jbjDHxFXITWTf9+O7mcbJ+WMT4TawqORldLER4sQTppxXL1jQRwlaCDFL6Frz7Of01N3E bZ8DOfTJ/TlZg9uPIs6dVaEHsGxlr7U= X-MC-Unique: tm8Y7tj_OCiIp20ple244w-1 From: David Hildenbrand To: qemu-devel@nongnu.org Subject: [PATCH v5 02/11] virtio-mem: Factor out traversing unplugged ranges Date: Thu, 21 Jan 2021 12:05:31 +0100 Message-Id: <20210121110540.33704-3-david@redhat.com> In-Reply-To: <20210121110540.33704-1-david@redhat.com> References: <20210121110540.33704-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Marek Kedzierski , Pankaj Gupta , Wei Yang , David Hildenbrand , "Michael S. Tsirkin" , Alex Williamson , Peter Xu , "Dr . David Alan Gilbert" , Auger Eric , Pankaj Gupta , teawater , Jonathan Cameron , Paolo Bonzini , Igor Mammedov 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 factor out the core logic, to be reused soon. Reviewed-by: Pankaj Gupta Cc: Paolo Bonzini Cc: "Michael S. Tsirkin" Cc: Alex Williamson Cc: Dr. David Alan Gilbert Cc: Igor Mammedov Cc: Pankaj Gupta Cc: Peter Xu Cc: Auger Eric Cc: Wei Yang Cc: teawater Cc: Marek Kedzierski Signed-off-by: David Hildenbrand --- hw/virtio/virtio-mem.c | 86 ++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index 655824ff81..471e464171 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -145,6 +145,33 @@ 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, + uint64_t offset, uint64_t size); + +static int virtio_mem_for_each_unplugged_range(const VirtIOMEM *vmem, void= *arg, + virtio_mem_range_cb cb) +{ + unsigned long first_zero_bit, last_zero_bit; + uint64_t offset, size; + int ret =3D 0; + + 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; + size =3D (last_zero_bit - first_zero_bit + 1) * vmem->block_size; + + ret =3D cb(vmem, arg, offset, size); + if (ret) { + break; + } + first_zero_bit =3D find_next_zero_bit(vmem->bitmap, vmem->bitmap_s= ize, + last_zero_bit + 2); + } + return ret; +} + static bool virtio_mem_test_bitmap(VirtIOMEM *vmem, uint64_t start_gpa, uint64_t size, bool plugged) { @@ -594,33 +621,27 @@ static void virtio_mem_device_unrealize(DeviceState *= dev) ram_block_discard_require(false); } =20 -static int virtio_mem_restore_unplugged(VirtIOMEM *vmem) +static int virtio_mem_discard_range_cb(const VirtIOMEM *vmem, void *arg, + uint64_t offset, uint64_t size) { RAMBlock *rb =3D vmem->memdev->mr.ram_block; - unsigned long first_zero_bit, last_zero_bit; - uint64_t offset, length; int ret; =20 - /* Find consecutive unplugged blocks and discard the consecutive range= . */ - 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; - - ret =3D ram_block_discard_range(rb, offset, length); - if (ret) { - error_report("Unexpected error discarding RAM: %s", - strerror(-ret)); - return -EINVAL; - } - first_zero_bit =3D find_next_zero_bit(vmem->bitmap, vmem->bitmap_s= ize, - last_zero_bit + 2); + ret =3D ram_block_discard_range(rb, offset, size); + if (ret) { + error_report("Unexpected error discarding RAM: %s", strerror(-ret)= ); + return -EINVAL; } return 0; } =20 +static int virtio_mem_restore_unplugged(VirtIOMEM *vmem) +{ + /* Make sure all memory is really discarded after migration. */ + return virtio_mem_for_each_unplugged_range(vmem, NULL, + virtio_mem_discard_range_cb= ); +} + static int virtio_mem_post_load(void *opaque, int version_id) { if (migration_in_incoming_postcopy()) { @@ -872,28 +893,19 @@ static void virtio_mem_set_block_size(Object *obj, Vi= sitor *v, const char *name, vmem->block_size =3D value; } =20 -static void virtio_mem_precopy_exclude_unplugged(VirtIOMEM *vmem) +static int virtio_mem_precopy_exclude_range_cb(const VirtIOMEM *vmem, void= *arg, + uint64_t offset, uint64_t s= ize) { void * const host =3D qemu_ram_get_host_addr(vmem->memdev->mr.ram_bloc= k); - unsigned long first_zero_bit, last_zero_bit; - uint64_t offset, length; =20 - /* - * Find consecutive unplugged blocks and exclude them from migration. - * - * Note: Blocks cannot get (un)plugged during precopy, no locking need= ed. - */ - 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; + qemu_guest_free_page_hint(host + offset, size); + return 0; +} =20 - qemu_guest_free_page_hint(host + offset, length); - first_zero_bit =3D find_next_zero_bit(vmem->bitmap, vmem->bitmap_s= ize, - last_zero_bit + 2); - } +static void virtio_mem_precopy_exclude_unplugged(VirtIOMEM *vmem) +{ + virtio_mem_for_each_unplugged_range(vmem, NULL, + virtio_mem_precopy_exclude_range_c= b); } =20 static int virtio_mem_precopy_notify(NotifierWithReturn *n, void *data) --=20 2.29.2