From nobody Sat Sep 21 02:52:38 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=1697450891; cv=none; d=zohomail.com; s=zohoarc; b=ReHMTKTdkeRyYBBM9bebpBY9+cyJOlKIw/25SKUxKAOhJGeFRyUFtvQShAiGA+Tjr6DHiu5FYhEnY+5Ou9QBvYSOmvHvYn91tp8c1Tbq4/zbrXpt2bhSnxNCZOubFKi/oarDAo9HfAtnVfzRPoLT01AysDsuJ96WbrUl5nIIOdQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1697450891; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=KuYyQQwWfQ7obuS2fy7ZldJSg7RA9HYmz86cPjRoGGk=; b=CQi3K76kM/hUjtBDBK8bFj4FkT6NsLuq6qoj4OuHONtgE6d0K40p3YQCCcKJVJ5cGZzvNKCIiZXWAVKF7bjPuyzS3bn8I6HnWW4F9IcdxdkeffwYJtPPvJk0OV73ElDIkni0XM7gftWvfYMdysR6IvBwZA+zEV/QZ5Y/WGQE9VE= 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 1697450891262977.5644760598944; Mon, 16 Oct 2023 03:08:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qsKVX-0001Nv-GN; Mon, 16 Oct 2023 06:07:31 -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 1qsKVM-0001JJ-8C for qemu-devel@nongnu.org; Mon, 16 Oct 2023 06:07:21 -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 1qsKVK-0007Nl-0d for qemu-devel@nongnu.org; Mon, 16 Oct 2023 06:07:19 -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-610-FqF2wy3YM_y5SWmfvCZT4Q-1; Mon, 16 Oct 2023 06:07:15 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 6CB5D3C1F120; Mon, 16 Oct 2023 10:07:14 +0000 (UTC) Received: from secure.mitica (unknown [10.39.194.127]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7115F63F45; Mon, 16 Oct 2023 10:07:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697450837; 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=KuYyQQwWfQ7obuS2fy7ZldJSg7RA9HYmz86cPjRoGGk=; b=ckcq9Ck2JKINyKz2mw75+xE1O2yGcs+ltEE6HqCu0Y3fH/cBZEYG5Vlp7VYy9X9JTONIxS wfvqFik0IpZSrrANtJZJWiMOJmDkHUcDGHP/A4EgVKeCIUEWOlRv9I0VNhGQhjGFdGQqPe iH6BuC+EGi73qpsQp/oeshaoRkCentA= X-MC-Unique: FqF2wy3YM_y5SWmfvCZT4Q-1 From: Juan Quintela To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Paolo Bonzini , Vladimir Sementsov-Ogievskiy , Fam Zheng , Cleber Rosa , Eric Blake , Li Zhijian , Peter Xu , Markus Armbruster , John Snow , Stefan Hajnoczi , Juan Quintela , Leonardo Bras , Laurent Vivier , Fabiano Rosas , Thomas Huth , Wei Wang , Isaku Yamahata Subject: [PULL 01/38] migration: refactor migration_completion Date: Mon, 16 Oct 2023 12:06:29 +0200 Message-ID: <20231016100706.2551-2-quintela@redhat.com> In-Reply-To: <20231016100706.2551-1-quintela@redhat.com> References: <20231016100706.2551-1-quintela@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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=quintela@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: 1697450893261100003 Content-Type: text/plain; charset="utf-8" From: Wei Wang Current migration_completion function is a bit long. Refactor the long implementation into different subfunctions: - migration_completion_precopy: completion code related to precopy - migration_completion_postcopy: completion code related to postcopy Rename await_return_path_close_on_source to close_return_path_on_source: It is renamed to match with open_return_path_on_source. This improves readability and is easier for future updates (e.g. add new subfunctions when completion code related to new features are needed). No functional changes intended. Signed-off-by: Wei Wang Reviewed-by: Peter Xu Reviewed-by: Isaku Yamahata Reviewed-by: Juan Quintela Message-ID: <20230804093053.5037-1-wei.w.wang@intel.com> Signed-off-by: Juan Quintela --- migration/migration.c | 167 ++++++++++++++++++++++++------------------ 1 file changed, 94 insertions(+), 73 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 1c6c81ad49..0e1002d017 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -99,7 +99,7 @@ static int migration_maybe_pause(MigrationState *s, int *current_active_state, int new_state); static void migrate_fd_cancel(MigrationState *s); -static int await_return_path_close_on_source(MigrationState *s); +static int close_return_path_on_source(MigrationState *s); =20 static bool migration_needs_multiple_sockets(void) { @@ -1191,7 +1191,7 @@ static void migrate_fd_cleanup(MigrationState *s) * We already cleaned up to_dst_file, so errors from the return * path might be due to that, ignore them. */ - await_return_path_close_on_source(s); + close_return_path_on_source(s); =20 assert(!migration_is_active(s)); =20 @@ -2049,8 +2049,7 @@ static int open_return_path_on_source(MigrationState = *ms) return 0; } =20 -/* Returns 0 if the RP was ok, otherwise there was an error on the RP */ -static int await_return_path_close_on_source(MigrationState *ms) +static int close_return_path_on_source(MigrationState *ms) { int ret; =20 @@ -2317,6 +2316,87 @@ static int migration_maybe_pause(MigrationState *s, return s->state =3D=3D new_state ? 0 : -EINVAL; } =20 +static int migration_completion_precopy(MigrationState *s, + int *current_active_state) +{ + int ret; + + qemu_mutex_lock_iothread(); + s->downtime_start =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); + qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); + + s->vm_old_state =3D runstate_get(); + global_state_store(); + + ret =3D vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); + trace_migration_completion_vm_stop(ret); + if (ret < 0) { + goto out_unlock; + } + + ret =3D migration_maybe_pause(s, current_active_state, + MIGRATION_STATUS_DEVICE); + if (ret < 0) { + goto out_unlock; + } + + /* + * Inactivate disks except in COLO, and track that we have done so in = order + * to remember to reactivate them if migration fails or is cancelled. + */ + s->block_inactive =3D !migrate_colo(); + migration_rate_set(RATE_LIMIT_DISABLED); + ret =3D qemu_savevm_state_complete_precopy(s->to_dst_file, false, + s->block_inactive); +out_unlock: + qemu_mutex_unlock_iothread(); + return ret; +} + +static void migration_completion_postcopy(MigrationState *s) +{ + trace_migration_completion_postcopy_end(); + + qemu_mutex_lock_iothread(); + qemu_savevm_state_complete_postcopy(s->to_dst_file); + qemu_mutex_unlock_iothread(); + + /* + * Shutdown the postcopy fast path thread. This is only needed when d= est + * QEMU binary is old (7.1/7.2). QEMU 8.0+ doesn't need this. + */ + if (migrate_postcopy_preempt() && s->preempt_pre_7_2) { + postcopy_preempt_shutdown_file(s); + } + + trace_migration_completion_postcopy_end_after_complete(); +} + +static void migration_completion_failed(MigrationState *s, + int current_active_state) +{ + if (s->block_inactive && (s->state =3D=3D MIGRATION_STATUS_ACTIVE || + s->state =3D=3D MIGRATION_STATUS_DEVICE)) { + /* + * If not doing postcopy, vm_start() will be called: let's + * regain control on images. + */ + Error *local_err =3D NULL; + + qemu_mutex_lock_iothread(); + bdrv_activate_all(&local_err); + if (local_err) { + error_report_err(local_err); + } else { + s->block_inactive =3D false; + } + qemu_mutex_unlock_iothread(); + } + + migrate_set_state(&s->state, current_active_state, + MIGRATION_STATUS_FAILED); +} + /** * migration_completion: Used by migration_thread when there's not much le= ft. * The caller 'breaks' the loop when this returns. @@ -2325,62 +2405,22 @@ static int migration_maybe_pause(MigrationState *s, */ static void migration_completion(MigrationState *s) { - int ret; + int ret =3D 0; int current_active_state =3D s->state; =20 if (s->state =3D=3D MIGRATION_STATUS_ACTIVE) { - qemu_mutex_lock_iothread(); - s->downtime_start =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); - qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); - - s->vm_old_state =3D runstate_get(); - global_state_store(); - - ret =3D vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); - trace_migration_completion_vm_stop(ret); - if (ret >=3D 0) { - ret =3D migration_maybe_pause(s, ¤t_active_state, - MIGRATION_STATUS_DEVICE); - } - if (ret >=3D 0) { - /* - * Inactivate disks except in COLO, and track that we - * have done so in order to remember to reactivate - * them if migration fails or is cancelled. - */ - s->block_inactive =3D !migrate_colo(); - migration_rate_set(RATE_LIMIT_DISABLED); - ret =3D qemu_savevm_state_complete_precopy(s->to_dst_file, fal= se, - s->block_inactive); - } - - qemu_mutex_unlock_iothread(); - - if (ret < 0) { - goto fail; - } + ret =3D migration_completion_precopy(s, ¤t_active_state); } else if (s->state =3D=3D MIGRATION_STATUS_POSTCOPY_ACTIVE) { - trace_migration_completion_postcopy_end(); - - qemu_mutex_lock_iothread(); - qemu_savevm_state_complete_postcopy(s->to_dst_file); - qemu_mutex_unlock_iothread(); - - /* - * Shutdown the postcopy fast path thread. This is only needed - * when dest QEMU binary is old (7.1/7.2). QEMU 8.0+ doesn't need - * this. - */ - if (migrate_postcopy_preempt() && s->preempt_pre_7_2) { - postcopy_preempt_shutdown_file(s); - } - - trace_migration_completion_postcopy_end_after_complete(); + migration_completion_postcopy(s); } else { + ret =3D -1; + } + + if (ret < 0) { goto fail; } =20 - if (await_return_path_close_on_source(s)) { + if (close_return_path_on_source(s)) { goto fail; } =20 @@ -2401,26 +2441,7 @@ static void migration_completion(MigrationState *s) return; =20 fail: - if (s->block_inactive && (s->state =3D=3D MIGRATION_STATUS_ACTIVE || - s->state =3D=3D MIGRATION_STATUS_DEVICE)) { - /* - * If not doing postcopy, vm_start() will be called: let's - * regain control on images. - */ - Error *local_err =3D NULL; - - qemu_mutex_lock_iothread(); - bdrv_activate_all(&local_err); - if (local_err) { - error_report_err(local_err); - } else { - s->block_inactive =3D false; - } - qemu_mutex_unlock_iothread(); - } - - migrate_set_state(&s->state, current_active_state, - MIGRATION_STATUS_FAILED); + migration_completion_failed(s, current_active_state); } =20 /** @@ -2563,7 +2584,7 @@ static MigThrError postcopy_pause(MigrationState *s) * path and just wait for the thread to finish. It will be * re-created when we resume. */ - await_return_path_close_on_source(s); + close_return_path_on_source(s); =20 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_POSTCOPY_PAUSED); --=20 2.41.0