From patchwork Thu Oct 24 21:30:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849761 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8FA00D10380 for ; Thu, 24 Oct 2024 21:31:57 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t45QJ-0005et-Nz; Thu, 24 Oct 2024 17:31:16 -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 1t45QD-0005dK-7v for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:09 -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 1t45Q8-0005X4-Ab for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729805463; 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=kQuiqCDFP7O6dGnJw0uzIfXFNnqGc/QZrj0NrKoRZoA=; b=dapSrsSveM0iMTkcX1KDAJQut2OaUhp5eJXyNMjgLIALxraleysE03Rk1rNdV6rMzWiV/1 +jhEUj+wLBVC+3X99gXGDAvsBLxvQHPdIMTY3SSMY6DaSrUMNIXEnNxafhWu2Ci/DH3sV4 f4UoIjEvGQHycrNLR1TPonpnbC+8jQ4= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-115-i2T0jw7OPSqRnFb0-FQumA-1; Thu, 24 Oct 2024 17:31:01 -0400 X-MC-Unique: i2T0jw7OPSqRnFb0-FQumA-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-460afc4747bso26777841cf.0 for ; Thu, 24 Oct 2024 14:31:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729805461; x=1730410261; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kQuiqCDFP7O6dGnJw0uzIfXFNnqGc/QZrj0NrKoRZoA=; b=KsCCkVVM8Q5HwBAwVS3EHPiKf9i6oeFOxTc2/flXkHwdfnBFE14pIN4FbB5oOFfyiY DyEmvKksMOfZkKHoalsQahFGSqFtVGnVCgTLCVNsCPLQ17agG8Rg/MNNL5NJDd/QyzE/ wKrjnw8JH7DVD2LBDVyLh6hgxhMr2lVUf3ZafXuc/zE/gtsggGmnkFbCNemLNgwQG4nP 7UWoDtLmIhygVTlmlaC8dUt1DzzITiV3a+VkRg48QeWo4yP4/X7YpTcGF60lRDL8qPvy sjJcpznZz4BwPi6EIg5bJCoIzKTrlSS5nOo+aGZbUqgjVCsy5x0Y73qLPWtGQuEk3LMs 5/kA== X-Gm-Message-State: AOJu0Yw2MU/Oc3bUeLJq+sXvwUqeHCPPhVWKYblGTSshJaMuo7u9WQii BpqP4m64OXrPHfTYAFQFWKXa1KV+31LFFuKaWWN/00aVpHKIRyd4Mip3YLZyTuRrH2T3F0AXzIz ehhhOC6og+8QWOFzcZJ0zStYYts/O4PZBQfOQrQ5TAAvva9dX6A+NeayoI/JCeGe+KIE+dk1G7j 4wB7hy4NFc98yFQ0qBWVMVpeQ9ghllSGDXMA== X-Received: by 2002:a05:622a:1992:b0:460:a82a:39c9 with SMTP id d75a77b69052e-4612525f1d1mr52367221cf.10.1729805460731; Thu, 24 Oct 2024 14:31:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHs0Kkco9NPHLR6DyTeQgaXjiAae4BYQ7N0k1m99KfGVb8p8HZ/bVQIkjtE0mJV4IjUFEz3vQ== X-Received: by 2002:a05:622a:1992:b0:460:a82a:39c9 with SMTP id d75a77b69052e-4612525f1d1mr52366921cf.10.1729805460308; Thu, 24 Oct 2024 14:31:00 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com. [99.254.114.190]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-460d3c3a434sm55465091cf.19.2024.10.24.14.30.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 14:30:59 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Cc: Avihai Horon , =?utf-8?q?C=C3=A9dric_Le_Goater?= , peterx@redhat.com, Fabiano Rosas , Alex Williamson Subject: [PATCH v3 1/8] migration: Take migration object refcount earlier for threads Date: Thu, 24 Oct 2024 17:30:49 -0400 Message-ID: <20241024213056.1395400-2-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241024213056.1395400-1-peterx@redhat.com> References: <20241024213056.1395400-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.263, 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_H2=-1.697, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Both migration thread or background snapshot thread will take a refcount of the migration object at the entrace of the thread function. That makes sense, because it protects the object from being freed by the main thread in migration_shutdown() later, but it might still race with it if the thread is scheduled too late. Consider the case right after pthread_create() happened, VM shuts down with the object released, but right after that the migration thread finally got created, referencing MigrationState* in the opaque pointer which is already freed. The only 100% safe way to make sure it won't get freed is taking the refcount right before the thread is created, meanwhile when BQL is held. Signed-off-by: Peter Xu Reviewed-by: Cédric Le Goater Reviewed-by: Fabiano Rosas --- migration/migration.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 74812ca785..e82ffa8cf3 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -3491,7 +3491,6 @@ static void *migration_thread(void *opaque) rcu_register_thread(); - object_ref(OBJECT(s)); update_iteration_initial_status(s); if (!multifd_send_setup()) { @@ -3629,7 +3628,6 @@ static void *bg_migration_thread(void *opaque) int ret; rcu_register_thread(); - object_ref(OBJECT(s)); migration_rate_set(RATE_LIMIT_DISABLED); @@ -3841,6 +3839,14 @@ void migrate_fd_connect(MigrationState *s, Error *error_in) } } + /* + * Take a refcount to make sure the migration object won't get freed by + * the main thread already in migration_shutdown(). + * + * The refcount will be released at the end of the thread function. + */ + object_ref(OBJECT(s)); + if (migrate_background_snapshot()) { qemu_thread_create(&s->thread, MIGRATION_THREAD_SNAPSHOT, bg_migration_thread, s, QEMU_THREAD_JOINABLE); From patchwork Thu Oct 24 21:30:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849763 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7A1B5D10380 for ; Thu, 24 Oct 2024 21:32:28 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t45QM-0005gR-7Y; Thu, 24 Oct 2024 17:31:18 -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 1t45QF-0005dY-Ob for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:13 -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 1t45QB-0005XZ-UF for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729805465; 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=aQi0jNBi64+93LR6lEvSe205PyBWXB4O7JAmokml2GE=; b=g9FugSj754VdO0tswSuKFzQkHiQ9Z3LRern8Xx42cEF67rDHwoqo074Ue6UN0xdan9Q8J7 c1d3gw/dqq/c+u+e3L08KEUNTohvxM6B4KPPh1Oqy7aX9yn7npjCqBJIAO82I9BZQEHKt4 pzV5hEL5UF6AH591Me4VScjBfpl9UTM= Received: from mail-vs1-f70.google.com (mail-vs1-f70.google.com [209.85.217.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-449-wAFJxnQiMJimptAh-OgsPw-1; Thu, 24 Oct 2024 17:31:04 -0400 X-MC-Unique: wAFJxnQiMJimptAh-OgsPw-1 Received: by mail-vs1-f70.google.com with SMTP id ada2fe7eead31-4a470b7ff49so444714137.2 for ; Thu, 24 Oct 2024 14:31:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729805463; x=1730410263; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aQi0jNBi64+93LR6lEvSe205PyBWXB4O7JAmokml2GE=; b=dxfVcMUWk+a2pFY2IVTg8JFuDC9hXFrNlMKTQqZdzg/6qfCCmpEULNOK3JwW4U2MGy pni/m+xocAyYQBRQuzR7ho7PJBD9+/c4fev+Wd41dug0OGI8RM7w2MDFPKC+Bz5I1dZS UMgxcppSvyVJ2ojusfaJ/wt/P9wGoSoTihicche1d9bI1ojGUIkGAXmshLD1kHMEYItO LLzNMJlhrZq0UU/tYipYMP5a4OykTEnOIhJIQmHSOoNMRyafy9prFtfVXaCRvM9R+ORo Z7x8mAN4sxFsfitTQJ+cIRkQISGCXQ4UcuA0Tq4bfXCH2ZWw7UzJ/L9/jAyEieH36t0b XULQ== X-Gm-Message-State: AOJu0YwJEgj0uCECyR8DpJrqcZ6ET2djphLdK4pKO2A+9uBDJf58HlIh CdA1AS4GtGD6lfa+OGE/fAIoxz83cb2qS9xX9YNMCxhfliUUCMAkp9u7ein8RstQD8t3cElVc/x DVHBX2W0i4f0P53bfKk8t3JgUVcruuz5ObfzF8WTmyufT93VTNnaZepzbMOfEoUUeNun1DmqmDS dzTHs0/51Aw5QgH52sSTQnf/9IM05rEfScsA== X-Received: by 2002:a05:6102:955:b0:4a7:4900:4b83 with SMTP id ada2fe7eead31-4a751c0b89cmr9617324137.15.1729805462797; Thu, 24 Oct 2024 14:31:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEB6u934XPonhdzpQjPiG5vkxK3ZjOKaQEkToJjnesu6BmSqva8uW+Wru+VQEuSP6n9jWapow== X-Received: by 2002:a05:6102:955:b0:4a7:4900:4b83 with SMTP id ada2fe7eead31-4a751c0b89cmr9617289137.15.1729805462382; Thu, 24 Oct 2024 14:31:02 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com. [99.254.114.190]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-460d3c3a434sm55465091cf.19.2024.10.24.14.31.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 14:31:01 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Cc: Avihai Horon , =?utf-8?q?C=C3=A9dric_Le_Goater?= , peterx@redhat.com, Fabiano Rosas , Alex Williamson Subject: [PATCH v3 2/8] migration: Unexport dirty_bitmap_mig_init() Date: Thu, 24 Oct 2024 17:30:50 -0400 Message-ID: <20241024213056.1395400-3-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241024213056.1395400-1-peterx@redhat.com> References: <20241024213056.1395400-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.263, 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_H2=-1.697, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org It's only used within migration/, so it shouldn't be exported. Reviewed-by: Cédric Le Goater Signed-off-by: Peter Xu Reviewed-by: Fabiano Rosas --- include/migration/misc.h | 3 --- migration/migration.h | 4 ++++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/migration/misc.h b/include/migration/misc.h index bfadc5613b..df57be6b5e 100644 --- a/include/migration/misc.h +++ b/include/migration/misc.h @@ -108,7 +108,4 @@ bool migration_incoming_postcopy_advised(void); /* True if background snapshot is active */ bool migration_in_bg_snapshot(void); -/* migration/block-dirty-bitmap.c */ -void dirty_bitmap_mig_init(void); - #endif diff --git a/migration/migration.h b/migration/migration.h index 7dc59c5e8d..0956e9274b 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -552,4 +552,8 @@ int migration_rp_wait(MigrationState *s); void migration_rp_kick(MigrationState *s); void migration_bitmap_sync_precopy(bool last_stage); + +/* migration/block-dirty-bitmap.c */ +void dirty_bitmap_mig_init(void); + #endif From patchwork Thu Oct 24 21:30:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849770 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1C87CD10380 for ; Thu, 24 Oct 2024 21:33:03 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t45QL-0005fE-8R; Thu, 24 Oct 2024 17:31:17 -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 1t45QF-0005dX-OP for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:13 -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 1t45QC-0005YA-Vu for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729805467; 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=h5qDUbT0oI/ii3wep25YhtJ0lpIyWNP4aqcokwUdLXo=; b=V3XPPWa/RwDQXoZZ1GmjkzSlKSBoLMJIywQJpkcOw1GDB2Od5ClJBaELjO3jm/caqScHCb WIpcBmNmXMfYTmFwGN0M+MldlyLDsLTBfXSMOWUany21KAM2RWS5GTgfwV10hDtny7B2Wa bJRsXoL3yKlud/1tdtSXhgtEOc9/6ZA= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-441-rHNI_zGZN3OM1ViNBm0c7g-1; Thu, 24 Oct 2024 17:31:06 -0400 X-MC-Unique: rHNI_zGZN3OM1ViNBm0c7g-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-460dd31b4c1so20491691cf.1 for ; Thu, 24 Oct 2024 14:31:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729805465; x=1730410265; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=h5qDUbT0oI/ii3wep25YhtJ0lpIyWNP4aqcokwUdLXo=; b=RbmDk25tLd52eTskvKqWIHWKT64MnvANl6PwqYxjlhXw5MeocRJYwL9/dN0Dw3yokC 0F488vRr7BadMAF9XhZNSd93Jy0/WhlFoTLk0EUXPniEv0348EhnPVjwbAvGajWAPjoc xmcPxKkgwhBK77AH8aiQoVa+1AcroV09/ScDfHvU5K3Wi/rNDJD5L854Kk5yXIDzCKFb qH/r0eON95r+RXH3ylGRGYnn58LFZ7GBc70wE0acGXpzkFEB39vrHJzH0M8ilFPooVBR nMiv0RFMImoFlmrNGoTt9UtbGXUHXkDBZDbQLNQuRSBtgHlCBAcP2TFEuv26JPl39AEe Y8eg== X-Gm-Message-State: AOJu0YzuL+fwXoRpuSygfeAZZtGSHs1iEmdStxRrMfloL6KlQPut3vGd ooQEARPVUi+X68T+l/DhHg3w0QI/vWvG9zYhuQ8W1mz/+HQR86wCC7o5dQdy4Ianyz3q3tlfrgN RwgnnEMQaNIJJd1RgQnaQzEpd2RgcGCZ+i5LNpkT667ZpiJYT48CR92MIMK70aUjH8VJP+r3UPQ vXKhuPi8WBT22+axOB5vXY5zinkfatNZH/FA== X-Received: by 2002:a05:622a:14cc:b0:45e:ff67:e058 with SMTP id d75a77b69052e-46114715486mr89951351cf.44.1729805464947; Thu, 24 Oct 2024 14:31:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE9Ya/zv4qjiQLCLXoG1QByfJGhOsaXZRQq+Kw5POUEhex2J1GYkXh31NxoWNv1VOfKsrT/Sw== X-Received: by 2002:a05:622a:14cc:b0:45e:ff67:e058 with SMTP id d75a77b69052e-46114715486mr89951091cf.44.1729805464543; Thu, 24 Oct 2024 14:31:04 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com. [99.254.114.190]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-460d3c3a434sm55465091cf.19.2024.10.24.14.31.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 14:31:03 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Cc: Avihai Horon , =?utf-8?q?C=C3=A9dric_Le_Goater?= , peterx@redhat.com, Fabiano Rosas , Alex Williamson Subject: [PATCH v3 3/8] migration: Unexport ram_mig_init() Date: Thu, 24 Oct 2024 17:30:51 -0400 Message-ID: <20241024213056.1395400-4-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241024213056.1395400-1-peterx@redhat.com> References: <20241024213056.1395400-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.263, 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_H2=-1.697, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org It's only used within migration/. Signed-off-by: Peter Xu Reviewed-by: Cédric Le Goater Reviewed-by: Fabiano Rosas --- include/migration/misc.h | 1 - migration/ram.h | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/include/migration/misc.h b/include/migration/misc.h index df57be6b5e..e8490e3af5 100644 --- a/include/migration/misc.h +++ b/include/migration/misc.h @@ -39,7 +39,6 @@ void precopy_add_notifier(NotifierWithReturn *n); void precopy_remove_notifier(NotifierWithReturn *n); int precopy_notify(PrecopyNotifyReason reason, Error **errp); -void ram_mig_init(void); void qemu_guest_free_page_hint(void *addr, size_t len); bool migrate_ram_is_ignored(RAMBlock *block); diff --git a/migration/ram.h b/migration/ram.h index bc0318b834..0d1981f888 100644 --- a/migration/ram.h +++ b/migration/ram.h @@ -44,6 +44,7 @@ extern XBZRLECacheStats xbzrle_counters; INTERNAL_RAMBLOCK_FOREACH(block) \ if (!qemu_ram_is_migratable(block)) {} else +void ram_mig_init(void); int xbzrle_cache_resize(uint64_t new_size, Error **errp); uint64_t ram_bytes_remaining(void); uint64_t ram_bytes_total(void); From patchwork Thu Oct 24 21:30:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849762 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2DB0BD10388 for ; Thu, 24 Oct 2024 21:31:57 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t45QK-0005fC-UT; Thu, 24 Oct 2024 17:31:16 -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 1t45QH-0005eC-F7 for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:13 -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 1t45QE-0005Yn-2w for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729805469; 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=3olLJmt75jk+FJwxKOAC+XSuUICRdqFmF+p2v48vhJw=; b=SCpGLYdXndl5HlztTUyUbQy1cdcSz7SS1N+15C6o/UOTw/GnamUCoAsV+RLQHBuE7YynrI epmMmym9OBDNfdykG3lwxrsFT29gQJpCpg1fRXJirK8SvpfnAENUwSs2AF/K1pyVOmj9iy cL6EK3w+OwNh8pbSfymbms22t2C3aZo= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-511-DohlQ0LmOmKiiaBxkhuYYw-1; Thu, 24 Oct 2024 17:31:08 -0400 X-MC-Unique: DohlQ0LmOmKiiaBxkhuYYw-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-460b638b668so20330341cf.0 for ; Thu, 24 Oct 2024 14:31:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729805467; x=1730410267; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3olLJmt75jk+FJwxKOAC+XSuUICRdqFmF+p2v48vhJw=; b=Bup7Rtu7fkFzWaHUHJ9PyA2aFX1H3oAWDh5wkX5evOI1sPz5+Bi/sQceGYTQgGCR5U y6JmHWfPThS/y3pM9luwBB9pny4lxSYCL5Rora3LkGCr6BW2ILAAKAps2OxgX9L/TU+4 FLhp4nMV1gqf0y5ycCf0LCY+VCIEJJ1RYcDo+l2F47yjuNrgieNPfNLSJ1lr0xQXLutn ptUcQP81BYlkHImMOVVvmqHW8kkggnaFONZV8JTJOEO15rQCdpgbWIU8FT6ou7sanSJz 4TrhP8P2PI02sOFcBJuN7G1EX57xyacos/mft4PfCbC1S3BXm0cbgqG87El9j0gNS8yS qEIA== X-Gm-Message-State: AOJu0YyJacr1vMhd7r0+e8UpPj+adF2dCTVr6pyDdZ9i/sjN19wkrvJ/ kuefvTnLKJOguuUaNEZ0d01rstraGepTAfJDW5T0vbXq+pgni+9WDMSEbeAUEsXoKGwRenmtIPt 5+g9KbvOGvcdbzHfxw7fZNohxb+lbjvZI3RapTzgTIYhPazbtZuMYlJ2G+CZYVO3l7DOROtXCO7 sTAVplX/THRymhXJH6aSfC6Rm2I0RuPnBzQw== X-Received: by 2002:a05:622a:1109:b0:461:1474:2059 with SMTP id d75a77b69052e-461258e3428mr42551471cf.13.1729805466804; Thu, 24 Oct 2024 14:31:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEM4IdpNMXK7juRo68WjGvXa/gEzd365qeeAJM4Omi4FyNvz10hSvFKOb2LsgeFi0qVUeGcmQ== X-Received: by 2002:a05:622a:1109:b0:461:1474:2059 with SMTP id d75a77b69052e-461258e3428mr42551191cf.13.1729805466487; Thu, 24 Oct 2024 14:31:06 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com. [99.254.114.190]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-460d3c3a434sm55465091cf.19.2024.10.24.14.31.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 14:31:05 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Cc: Avihai Horon , =?utf-8?q?C=C3=A9dric_Le_Goater?= , peterx@redhat.com, Fabiano Rosas , Alex Williamson Subject: [PATCH v3 4/8] migration: Drop migration_is_setup_or_active() Date: Thu, 24 Oct 2024 17:30:52 -0400 Message-ID: <20241024213056.1395400-5-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241024213056.1395400-1-peterx@redhat.com> References: <20241024213056.1395400-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.263, 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_H2=-1.697, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org This helper is mostly the same as migration_is_running(), except that one has COLO reported as true, the other has CANCELLING reported as true. Per my past years experience on the state changes, none of them should matter. To make it slightly safer, report both COLO || CANCELLING to be true in migration_is_running(), then drop the other one. We kept the 1st only because the name is simpler, and clear enough. Signed-off-by: Peter Xu Reviewed-by: Cédric Le Goater Reviewed-by: Fabiano Rosas --- include/migration/misc.h | 4 ++-- hw/vfio/common.c | 2 +- migration/migration.c | 35 +++-------------------------------- migration/ram.c | 5 ++--- net/vhost-vdpa.c | 3 +-- 5 files changed, 9 insertions(+), 40 deletions(-) diff --git a/include/migration/misc.h b/include/migration/misc.h index e8490e3af5..86ef160f19 100644 --- a/include/migration/misc.h +++ b/include/migration/misc.h @@ -52,11 +52,12 @@ void dump_vmstate_json_to_file(FILE *out_fp); /* migration/migration.c */ void migration_object_init(void); void migration_shutdown(void); + bool migration_is_idle(void); bool migration_is_active(void); bool migration_is_device(void); +bool migration_is_running(void); bool migration_thread_is_self(void); -bool migration_is_setup_or_active(void); typedef enum MigrationEventType { MIG_EVENT_PRECOPY_SETUP, @@ -95,7 +96,6 @@ void migration_add_notifier_mode(NotifierWithReturn *notify, MigrationNotifyFunc func, MigMode mode); void migration_remove_notifier(NotifierWithReturn *notify); -bool migration_is_running(void); void migration_file_set_error(int ret, Error *err); /* True if incoming migration entered POSTCOPY_INCOMING_DISCARD */ diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 36d0cf6585..dcef44fe55 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -149,7 +149,7 @@ bool vfio_viommu_preset(VFIODevice *vbasedev) static void vfio_set_migration_error(int ret) { - if (migration_is_setup_or_active()) { + if (migration_is_running()) { migration_file_set_error(ret, NULL); } } diff --git a/migration/migration.c b/migration/migration.c index e82ffa8cf3..3365195def 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -1116,33 +1116,6 @@ void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value) migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf); } -/* - * Return true if we're already in the middle of a migration - * (i.e. any of the active or setup states) - */ -bool migration_is_setup_or_active(void) -{ - MigrationState *s = current_migration; - - switch (s->state) { - case MIGRATION_STATUS_ACTIVE: - case MIGRATION_STATUS_POSTCOPY_ACTIVE: - case MIGRATION_STATUS_POSTCOPY_PAUSED: - case MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP: - case MIGRATION_STATUS_POSTCOPY_RECOVER: - case MIGRATION_STATUS_SETUP: - case MIGRATION_STATUS_PRE_SWITCHOVER: - case MIGRATION_STATUS_DEVICE: - case MIGRATION_STATUS_WAIT_UNPLUG: - case MIGRATION_STATUS_COLO: - return true; - - default: - return false; - - } -} - bool migration_is_running(void) { MigrationState *s = current_migration; @@ -1158,11 +1131,10 @@ bool migration_is_running(void) case MIGRATION_STATUS_DEVICE: case MIGRATION_STATUS_WAIT_UNPLUG: case MIGRATION_STATUS_CANCELLING: + case MIGRATION_STATUS_COLO: return true; - default: return false; - } } @@ -1661,8 +1633,7 @@ bool migration_incoming_postcopy_advised(void) bool migration_in_bg_snapshot(void) { - return migrate_background_snapshot() && - migration_is_setup_or_active(); + return migrate_background_snapshot() && migration_is_running(); } bool migration_is_idle(void) @@ -2335,7 +2306,7 @@ static void *source_return_path_thread(void *opaque) trace_source_return_path_thread_entry(); rcu_register_thread(); - while (migration_is_setup_or_active()) { + while (migration_is_running()) { trace_source_return_path_thread_loop_top(); header_type = qemu_get_be16(rp); diff --git a/migration/ram.c b/migration/ram.c index d284f63854..5646a0b882 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -2860,7 +2860,7 @@ void qemu_guest_free_page_hint(void *addr, size_t len) size_t used_len, start, npages; /* This function is currently expected to be used during live migration */ - if (!migration_is_setup_or_active()) { + if (!migration_is_running()) { return; } @@ -3208,8 +3208,7 @@ static int ram_save_iterate(QEMUFile *f, void *opaque) } out: - if (ret >= 0 - && migration_is_setup_or_active()) { + if (ret >= 0 && migration_is_running()) { if (migrate_multifd() && migrate_multifd_flush_after_each_section() && !migrate_mapped_ram()) { ret = multifd_ram_flush_and_sync(); diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c index 46b02c50be..231b45246c 100644 --- a/net/vhost-vdpa.c +++ b/net/vhost-vdpa.c @@ -375,8 +375,7 @@ static int vhost_vdpa_net_data_start(NetClientState *nc) assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); - if (s->always_svq || - migration_is_setup_or_active()) { + if (s->always_svq || migration_is_running()) { v->shadow_vqs_enabled = true; } else { v->shadow_vqs_enabled = false; From patchwork Thu Oct 24 21:30:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849768 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 24F10D10380 for ; Thu, 24 Oct 2024 21:32:56 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t45QL-0005fM-Cp; Thu, 24 Oct 2024 17:31:17 -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 1t45QJ-0005ev-OS for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:16 -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 1t45QH-0005Z8-7O for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729805472; 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=/6k9K1F4sWItx0KAORYMPzbNGOJ+dvY3NLwf36xd2l4=; b=YykItHm2JTlA62fZcwT4rSeS20FbAnVkwhA1vOxUAzu5s0ssM6SV9SNuTgZBKiL5t0d9TM Ca0fbJIhZFr48jOmCJS6H9gZx1W8bys32A/0JuI/JwYSPmPtrsXyrSys2dWCvf7VBEL5ou QZNdYW1tPhIPfP+0rtK7ASEYfIJKlTI= Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-414-lCTRQEExPbCnVSwUuNEi6w-1; Thu, 24 Oct 2024 17:31:10 -0400 X-MC-Unique: lCTRQEExPbCnVSwUuNEi6w-1 Received: by mail-ot1-f71.google.com with SMTP id 46e09a7af769-7181ca4eca5so1553388a34.1 for ; Thu, 24 Oct 2024 14:31:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729805469; x=1730410269; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/6k9K1F4sWItx0KAORYMPzbNGOJ+dvY3NLwf36xd2l4=; b=p5pG+18ShGulXr5pWdxFwLdIF3cJX502hA/aYMMHq0PbFTOMozcUtwD9fgeVX+Y8Oc EdsGnkk9cNYkl32DeGc4R32NGYN65zRJTAnwF/0CvYDTESN++EvdeLlUVeT35X7jKuje LuElrANuIYyGQuFOdy6AUHFGrUy3cXBM55xM4QO3tuAUU1K/fcv/8wRERSzqf307GI+F yOFoYdjZVEwynLsTBheyhM1JQcczteZmIhmN2+DqmuU9ArRBTe8MpogiYicKROSR2pae yQR8vOI+wsNE2ryAU8gKGzUw2XBXQDnN/POoj1TSi7yNombmpDFiCM0hWeyNv6MGFxE5 Djxw== X-Gm-Message-State: AOJu0Yzv0s6N7NGei40HRQIjWXjxJnHTABdZc0OA0sAiJWydEL53Rwyg QGt7mRAIUEs1Oe4s9lD61svKpsUr9AWC/MphiEcnYEnHm02Rqt+L6CK75Sz1YNbVdMDgawXn2El U9OqDHHQcithz0MgWNrkERVoXarKhtwvKWmh+ks1UG+A35J+VghEwdmPrBCr2EzcWZLZW+B0EhT JVJlPZIfqTm1hpXLjzJ6VE40nyzpfMfCNe2A== X-Received: by 2002:a05:6358:7211:b0:197:df0e:f23c with SMTP id e5c5f4694b2df-1c3e4d1fd0emr265778355d.11.1729805468913; Thu, 24 Oct 2024 14:31:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHgNXyX+9BUC5E8HTotTZ7ubI6ph201MDCgEpTCL2YHdpRoHyl6p9kTRr3NCUYtR5o+uY242w== X-Received: by 2002:a05:6358:7211:b0:197:df0e:f23c with SMTP id e5c5f4694b2df-1c3e4d1fd0emr265776055d.11.1729805468481; Thu, 24 Oct 2024 14:31:08 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com. [99.254.114.190]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-460d3c3a434sm55465091cf.19.2024.10.24.14.31.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 14:31:07 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Cc: Avihai Horon , =?utf-8?q?C=C3=A9dric_Le_Goater?= , peterx@redhat.com, Fabiano Rosas , Alex Williamson Subject: [PATCH v3 5/8] migration: Drop migration_is_idle() Date: Thu, 24 Oct 2024 17:30:53 -0400 Message-ID: <20241024213056.1395400-6-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241024213056.1395400-1-peterx@redhat.com> References: <20241024213056.1395400-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.263, 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, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Now with the current migration_is_running(), it will report exactly the opposite of what will be reported by migration_is_idle(). Drop migration_is_idle(), instead use "!migration_is_running()" which should be identical on functionality. In reality, most of the idle check is inverted, so it's even easier to write with "migrate_is_running()" check. Signed-off-by: Peter Xu Reviewed-by: Cédric Le Goater Reviewed-by: Fabiano Rosas --- include/migration/misc.h | 1 - hw/virtio/virtio-mem.c | 2 +- migration/migration.c | 21 +-------------------- migration/ram.c | 2 +- system/qdev-monitor.c | 4 ++-- 5 files changed, 5 insertions(+), 25 deletions(-) diff --git a/include/migration/misc.h b/include/migration/misc.h index 86ef160f19..804eb23c06 100644 --- a/include/migration/misc.h +++ b/include/migration/misc.h @@ -53,7 +53,6 @@ void dump_vmstate_json_to_file(FILE *out_fp); void migration_object_init(void); void migration_shutdown(void); -bool migration_is_idle(void); bool migration_is_active(void); bool migration_is_device(void); bool migration_is_running(void); diff --git a/hw/virtio/virtio-mem.c b/hw/virtio/virtio-mem.c index ae1e81d7ba..80ada89551 100644 --- a/hw/virtio/virtio-mem.c +++ b/hw/virtio/virtio-mem.c @@ -188,7 +188,7 @@ static bool virtio_mem_is_busy(void) * after plugging them) until we're running on the destination (as we didn't * migrate these blocks when they were unplugged). */ - return migration_in_incoming_postcopy() || !migration_is_idle(); + return migration_in_incoming_postcopy() || migration_is_running(); } typedef int (*virtio_mem_range_cb)(VirtIOMEM *vmem, void *arg, diff --git a/migration/migration.c b/migration/migration.c index 3365195def..f86c709699 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -1636,25 +1636,6 @@ bool migration_in_bg_snapshot(void) return migrate_background_snapshot() && migration_is_running(); } -bool migration_is_idle(void) -{ - MigrationState *s = current_migration; - - if (!s) { - return true; - } - - switch (s->state) { - case MIGRATION_STATUS_NONE: - case MIGRATION_STATUS_CANCELLED: - case MIGRATION_STATUS_COMPLETED: - case MIGRATION_STATUS_FAILED: - return true; - default: - return false; - } -} - bool migration_is_active(void) { MigrationState *s = current_migration; @@ -1733,7 +1714,7 @@ static bool is_busy(Error **reasonp, Error **errp) ERRP_GUARD(); /* Snapshots are similar to migrations, so check RUN_STATE_SAVE_VM too. */ - if (runstate_check(RUN_STATE_SAVE_VM) || !migration_is_idle()) { + if (runstate_check(RUN_STATE_SAVE_VM) || migration_is_running()) { error_propagate_prepend(errp, *reasonp, "disallowing migration blocker " "(migration/snapshot in progress) for: "); diff --git a/migration/ram.c b/migration/ram.c index 5646a0b882..504b48d584 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -4498,7 +4498,7 @@ static void ram_mig_ram_block_resized(RAMBlockNotifier *n, void *host, return; } - if (!migration_is_idle()) { + if (migration_is_running()) { /* * Precopy code on the source cannot deal with the size of RAM blocks * changing at random points in time - especially after sending the diff --git a/system/qdev-monitor.c b/system/qdev-monitor.c index 1310f35c9f..83fa684475 100644 --- a/system/qdev-monitor.c +++ b/system/qdev-monitor.c @@ -686,7 +686,7 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts, return NULL; } - if (!migration_is_idle()) { + if (migration_is_running()) { error_setg(errp, "device_add not allowed while migrating"); return NULL; } @@ -935,7 +935,7 @@ void qdev_unplug(DeviceState *dev, Error **errp) return; } - if (!migration_is_idle() && !dev->allow_unplug_during_migration) { + if (migration_is_running() && !dev->allow_unplug_during_migration) { error_setg(errp, "device_del not allowed while migrating"); return; } From patchwork Thu Oct 24 21:30:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849766 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 61129D10388 for ; Thu, 24 Oct 2024 21:32:39 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t45QO-0005ht-Qy; Thu, 24 Oct 2024 17:31:20 -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 1t45QK-0005fD-T8 for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:16 -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 1t45QJ-0005ZT-1z for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729805473; 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=/6xFER4Y5so64URVhMy+JgrIVvXPCbxhauceydNM1kE=; b=aEUkbczaE/9ZbE1sFFayZ2r0Hsg/rJ6zYpd4rh4oDDsSytHG7DCLpfWulUJxqg/CIojFkP 8BjgO7Hwa/of/3sh2XLPX1Cu4rHUt6vidwA1DfZVg/XJ2/aIibqniWrpJ10UK4AVWyZ0FL qmPQT+jlxh+p905l35PPj0YhpOs4A7U= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-224-ZPrxl4qHM9yPLOi-VXapIw-1; Thu, 24 Oct 2024 17:31:12 -0400 X-MC-Unique: ZPrxl4qHM9yPLOi-VXapIw-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-460dd31b4c1so20493181cf.1 for ; Thu, 24 Oct 2024 14:31:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729805471; x=1730410271; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/6xFER4Y5so64URVhMy+JgrIVvXPCbxhauceydNM1kE=; b=mBQr6GrKkYfAF85STJSNzERqIC39harh9+tCMjccrYsBxRKBW5cs5EiDheNEDWzXgo 7iu7kx3UK12GE6LDfVkptZduy1rhtRbyxt3tA39xgt24IRw4fMwrsS1HSHYbbm8GryE4 StWACVE2HSFU520u/S/NTIh+L4Oeh7+9u1tUDVD4D0GKvrJXdvf65oK8IM2p4++6JXIc mtzq55VLdm0VOM0dO0Q3AKY6sLjikx04cbuAN6uwaviLY6cXHLrxyU4mMbwvfr9CZz14 Hw3vQyQ5wTmq/I9wBVaT//0gh6FyP8omxLQ7SQUYjHqB7S54o97KUxkrGJDomRacbozp OdGA== X-Gm-Message-State: AOJu0Yx4G0N1tN5fLC/vrksG9VX7F7F1QYlRjGKiEWU1Tmac/TZ7uE2C GwvAQqkxYsjpCg3vGQNETaeJScVH1aNx+z/g7sFy/62fXZiWfBK8wLH5qyTAuCa96hqrtNrUyRy 0AhgJSPXAWunpWgE7dqWqTPSGPeiwj19K87Crev31V8VcP/UEblkXqZSw8INJaEBZatQT7Smniv bq5K8nFsMQPxUTzp+gKBB1vxuSv4AUbCSu8g== X-Received: by 2002:ac8:58d4:0:b0:460:42f7:fa3f with SMTP id d75a77b69052e-461145b938fmr109750001cf.19.1729805470827; Thu, 24 Oct 2024 14:31:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF3m0nKmt3RafhxwjJOPF+IsczbS53HQlvyevm7dIpOkCCIx4ascOxYgpbvG4H7ywL2aAz2vA== X-Received: by 2002:ac8:58d4:0:b0:460:42f7:fa3f with SMTP id d75a77b69052e-461145b938fmr109749621cf.19.1729805470431; Thu, 24 Oct 2024 14:31:10 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com. [99.254.114.190]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-460d3c3a434sm55465091cf.19.2024.10.24.14.31.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 14:31:09 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Cc: Avihai Horon , =?utf-8?q?C=C3=A9dric_Le_Goater?= , peterx@redhat.com, Fabiano Rosas , Alex Williamson Subject: [PATCH v3 6/8] migration: Drop migration_is_device() Date: Thu, 24 Oct 2024 17:30:54 -0400 Message-ID: <20241024213056.1395400-7-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241024213056.1395400-1-peterx@redhat.com> References: <20241024213056.1395400-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.263, 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, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org This only checks the DEVICE stage, which is a very special stage, and only checked together with ACTIVE+POSTCOPY_ACTIVE. It's debatable why this needs a separate helper just to be exported. Theoretically speaking, DEVICE stage is also part of ACTIVE stage, where we're reaching the switchover phase but waiting for some external operation, during which VM stopped but migration is definitely active. Report DEVICE as active too, then we can drop migration_is_device(). Signed-off-by: Peter Xu --- include/migration/misc.h | 1 - hw/vfio/common.c | 2 +- migration/migration.c | 19 +++++++++---------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/include/migration/misc.h b/include/migration/misc.h index 804eb23c06..ad1e25826a 100644 --- a/include/migration/misc.h +++ b/include/migration/misc.h @@ -54,7 +54,6 @@ void migration_object_init(void); void migration_shutdown(void); bool migration_is_active(void); -bool migration_is_device(void); bool migration_is_running(void); bool migration_thread_is_self(void); diff --git a/hw/vfio/common.c b/hw/vfio/common.c index dcef44fe55..cc72282c71 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -174,7 +174,7 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainerBase *bcontainer) { VFIODevice *vbasedev; - if (!migration_is_active() && !migration_is_device()) { + if (!migration_is_active()) { return false; } diff --git a/migration/migration.c b/migration/migration.c index f86c709699..127b01734d 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -1638,17 +1638,16 @@ bool migration_in_bg_snapshot(void) bool migration_is_active(void) { - MigrationState *s = current_migration; - - return (s->state == MIGRATION_STATUS_ACTIVE || - s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); -} + MigrationStatus state = current_migration->state; -bool migration_is_device(void) -{ - MigrationState *s = current_migration; - - return s->state == MIGRATION_STATUS_DEVICE; + switch (state) { + case MIGRATION_STATUS_ACTIVE: + case MIGRATION_STATUS_POSTCOPY_ACTIVE: + case MIGRATION_STATUS_DEVICE: + return true; + default: + return false; + } } bool migration_thread_is_self(void) From patchwork Thu Oct 24 21:30:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849767 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6ABE5D10380 for ; Thu, 24 Oct 2024 21:32:43 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t45QO-0005hN-4K; Thu, 24 Oct 2024 17:31:20 -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 1t45QL-0005fL-5D for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:17 -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 1t45QJ-0005Zg-Lx for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729805475; 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=vte9HPiXE32VhwFKsE2SGPglzqTAs5Z4orbp/UZZ2ec=; b=FvaLUDRBiXJRl8ITv5yw7iRjHH2dfWuUPK1qVqALa8fPwiTQi7y+iKKiFolc49LWEIDKoz fNQVdQ5iVpEEchlwkwMdyL2rZCYu0olnvmMDb97OQn95JhSkvo90r1e4f7k68wt4pBTbyN Rc0/hSjNCwXHFtoft6YnO+q5qCAhDK0= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-401-U5Q76G8nP8-8Tjuft_Q-9g-1; Thu, 24 Oct 2024 17:31:13 -0400 X-MC-Unique: U5Q76G8nP8-8Tjuft_Q-9g-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7ac9b08cb77so207102185a.1 for ; Thu, 24 Oct 2024 14:31:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729805473; x=1730410273; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vte9HPiXE32VhwFKsE2SGPglzqTAs5Z4orbp/UZZ2ec=; b=PZclegvjxGBR1k+u4FSBBP9f0Y14oP32LrT7722FGpcYcxHwRa7Ri+2CFApMyorxJS DaWy2IGbCqEwZfbJNMK9iRae7HZffktA5Ac3PxiTBPXrpRcTgqwqZqxsON5DDjh/4xQ6 pZdaaOnr9zC9uHY3tEKNauAGnrvA6uwwjxGKBWoaBspC0HhIY7bUcPdWlByg84Hn36tK zgpRyBykPBZYqsv0QbCmYlFp3J46U2YsYJ59ONe2wri3sVGUGV1LLjgYtDaM+mHoDryz Fz5sxEki1L7/gaF3gcH4TfcZFMZrXklu2cY5h8t9aw/lggPLRsCyf2IRxPYSxuAotq6A RGtQ== X-Gm-Message-State: AOJu0YzadpGvYyUBxccNtQDDDh/mES3YMTorcPlaO7hObSpPYjgJDbm/ N9t+0h9BLze627YvXEuewJGq4HECtKb3yBmJ7+402pXN2TydDi9+0R9GD+8YbOOH2/kBIxnBjdj aM8LZF0ikjVJuLxAXCLps3jp7wSThVItHg8wlsZkB9Rem7FA6y80buw6aJz0pIoE0gop4VGWAtP mIyoRHqLI7vqD2t9bpQPPGFuwB5YNHSV8Vsw== X-Received: by 2002:ac8:5a52:0:b0:457:cab4:6e4a with SMTP id d75a77b69052e-4611471f04dmr108164791cf.37.1729805472866; Thu, 24 Oct 2024 14:31:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFQOcNK9ZtKvtLWG7+ZNpte+LVc/3X0MTpv+8D7V2VzBaT08OCRfXpMgl0wZJdfENNshehb4A== X-Received: by 2002:ac8:5a52:0:b0:457:cab4:6e4a with SMTP id d75a77b69052e-4611471f04dmr108164411cf.37.1729805472410; Thu, 24 Oct 2024 14:31:12 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com. [99.254.114.190]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-460d3c3a434sm55465091cf.19.2024.10.24.14.31.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 14:31:11 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Cc: Avihai Horon , =?utf-8?q?C=C3=A9dric_Le_Goater?= , peterx@redhat.com, Fabiano Rosas , Alex Williamson Subject: [PATCH v3 7/8] migration: Unexport migration_is_active() Date: Thu, 24 Oct 2024 17:30:55 -0400 Message-ID: <20241024213056.1395400-8-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241024213056.1395400-1-peterx@redhat.com> References: <20241024213056.1395400-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.263, 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_H2=-1.697, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org We have two outside users of this API, so it's exported. Is it really necessary? Does it matter whether it must be ACTIVE/POSTCOPY_ACTIVE/DEVICE? I guess no. The external user is trying to detect whether migration is running or not, as simple as that. To make the migration_is*() APIs even shorter, let's use migration_is_running() for outside worlds. Internally there're actually three places that literally needs migration_is_active() rather than running(). Keep that an internal helper. After this patch, we finally only export one helper that allows external world to try detect migration status, which is migration_is_running(). Signed-off-by: Peter Xu --- include/migration/misc.h | 1 - migration/migration.h | 1 + hw/vfio/common.c | 4 ++-- system/dirtylimit.c | 3 +-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/include/migration/misc.h b/include/migration/misc.h index ad1e25826a..c0e23fdac9 100644 --- a/include/migration/misc.h +++ b/include/migration/misc.h @@ -53,7 +53,6 @@ void dump_vmstate_json_to_file(FILE *out_fp); void migration_object_init(void); void migration_shutdown(void); -bool migration_is_active(void); bool migration_is_running(void); bool migration_thread_is_self(void); diff --git a/migration/migration.h b/migration/migration.h index 0956e9274b..9fa26ab06a 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -492,6 +492,7 @@ int migration_call_notifiers(MigrationState *s, MigrationEventType type, int migrate_init(MigrationState *s, Error **errp); bool migration_is_blocked(Error **errp); +bool migration_is_active(void); /* True if outgoing migration has entered postcopy phase */ bool migration_in_postcopy(void); bool migration_postcopy_is_alive(MigrationStatus state); diff --git a/hw/vfio/common.c b/hw/vfio/common.c index cc72282c71..7eb99ebd4d 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -174,7 +174,7 @@ static bool vfio_devices_all_dirty_tracking(VFIOContainerBase *bcontainer) { VFIODevice *vbasedev; - if (!migration_is_active()) { + if (!migration_is_running()) { return false; } @@ -219,7 +219,7 @@ vfio_devices_all_running_and_mig_active(const VFIOContainerBase *bcontainer) { VFIODevice *vbasedev; - if (!migration_is_active()) { + if (!migration_is_running()) { return false; } diff --git a/system/dirtylimit.c b/system/dirtylimit.c index ab20da34bb..d7a855c603 100644 --- a/system/dirtylimit.c +++ b/system/dirtylimit.c @@ -80,8 +80,7 @@ static void vcpu_dirty_rate_stat_collect(void) int i = 0; int64_t period = DIRTYLIMIT_CALC_TIME_MS; - if (migrate_dirty_limit() && - migration_is_active()) { + if (migrate_dirty_limit() && migration_is_running()) { period = migrate_vcpu_dirty_limit_period(); } From patchwork Thu Oct 24 21:30:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13849769 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B0160D10388 for ; Thu, 24 Oct 2024 21:32:58 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t45QQ-0005iX-Ek; Thu, 24 Oct 2024 17:31:22 -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 1t45QN-0005hR-Kt for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:20 -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 1t45QL-0005aF-Tz for qemu-devel@nongnu.org; Thu, 24 Oct 2024 17:31:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729805477; 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=bJXamzqrq7AiFagiu9llJXewSi82uD7Ymbaq7adW03I=; b=MlqG7o+lyGN0PsuB1WNl4cAgQmJJgVcLXHcBZx7jlIGj84MZLadn0xuawu/1Boj6tCIMy5 tnoMXg5PwvCKOtivMhM3YwFiToA81vMw1FYQwjXsybYh+sq4zBu7Xudml96x98J7KPAHH+ /9tS0ApUtMztd77uUStjkp/o0hZFzgo= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-541-o7G2DmmTMo-ADXYSKrWmPw-1; Thu, 24 Oct 2024 17:31:15 -0400 X-MC-Unique: o7G2DmmTMo-ADXYSKrWmPw-1 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-460b07774a7so21584551cf.2 for ; Thu, 24 Oct 2024 14:31:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729805474; x=1730410274; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bJXamzqrq7AiFagiu9llJXewSi82uD7Ymbaq7adW03I=; b=Kdq4VnNmjgZkXc8WsrLQZEbiKl+UMHYnjN8rXW+XlyuiOu35rPBnbEHvms8SMsWunm nwzXm2JpXcjfqW/zvt+bJ5l6gPjjLQQP96INzDi6wCwcMnE03wEAkkxkcJdxY2SJNGwb UZ0qq88puM1Jtgwy6NlVwH0gEFwuoNmPpo5KYI5t+xwbH+/qovMPDlIQhj2kJHWQQh6Z Z2Onyv2UqQht1gTtsF4D4qfBgQ6gEHQkHvGfo9KN+Y7PdVDuWhbwQacOgdCjeRFly9t1 eZIi7rM1rnfB3TgJ9JJSocE/nP04weKE3qzWcO8KQA/GyG3mIF+OjbRq9PUwwy2XhHCY yt1Q== X-Gm-Message-State: AOJu0Yy6a6EKnPYDg/r5X+lVdve3ZI7dM1BjqGxBtTOKNradCctoIRGE xXU9ST/Lh7vjX83YELZ4QXmxDyj7QTUlw7r4rso6I4yxA/f/kJyCz5yuS4I3+052MpZGRpbqb0k AonPZ/J5D7748lnFIAvOYB12MTE047JnSZu5ixDb17IRt/1tZtOaPtnfXd+bCZcdF65oPg9DBrb Jtmv+NSd3DqvOC9UFg0qpqBttoxlzpoxU4OA== X-Received: by 2002:a05:622a:30b:b0:460:9a7c:8dd0 with SMTP id d75a77b69052e-46114745ea3mr102531841cf.55.1729805474459; Thu, 24 Oct 2024 14:31:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGWRcFG/xreUGKLjkXNCc+1IMxAXwaEE4kuCf0QBvjEHQYqdQt1NC5dwVPkayPhJzgdunfPJg== X-Received: by 2002:a05:622a:30b:b0:460:9a7c:8dd0 with SMTP id d75a77b69052e-46114745ea3mr102531551cf.55.1729805474084; Thu, 24 Oct 2024 14:31:14 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com. [99.254.114.190]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-460d3c3a434sm55465091cf.19.2024.10.24.14.31.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 14:31:13 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Cc: Avihai Horon , =?utf-8?q?C=C3=A9dric_Le_Goater?= , peterx@redhat.com, Fabiano Rosas , Alex Williamson Subject: [PATCH v3 8/8] migration: Protect updates to current_migration with a mutex Date: Thu, 24 Oct 2024 17:30:56 -0400 Message-ID: <20241024213056.1395400-9-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241024213056.1395400-1-peterx@redhat.com> References: <20241024213056.1395400-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -40 X-Spam_score: -4.1 X-Spam_bar: ---- X-Spam_report: (-4.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.263, 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_H2=-1.697, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Introduce migration_mutex, protecting concurrent updates to current_migration. In reality, most of the exported migration functions are safe to access migration objects on capabilities, etc., e.g. many of the code is invoked within migration thread via different hooks (e.g., precopy notifier, vmstate handler hooks, etc.). So literally the mutex so far only makes sure below two APIs that are prone to accessing freed current_migration: migration_is_running() migration_file_set_error() Then we'll need to take the mutex too when init/free the migration object. Signed-off-by: Peter Xu --- migration/migration.h | 3 +++ migration/migration.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/migration/migration.h b/migration/migration.h index 9fa26ab06a..05edcf0c49 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -473,6 +473,9 @@ struct MigrationState { bool rdma_migration; }; +extern QemuMutex migration_mutex; +#define QEMU_MIGRATION_LOCK_GUARD() QEMU_LOCK_GUARD(&migration_mutex) + void migrate_set_state(MigrationStatus *state, MigrationStatus old_state, MigrationStatus new_state); diff --git a/migration/migration.c b/migration/migration.c index 127b01734d..ef044968df 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -97,6 +97,14 @@ enum mig_rp_message_type { migrations at once. For now we don't need to add dynamic creation of migration */ +/* + * Protects current_migration below. Must be hold when using migration + * exported functions unless the caller knows it won't get freed. For + * example, when in the context of migration_thread() it's safe to access + * current_migration without the mutex, because the thread holds one extra + * refcount of the object, so it literally pins the object in-memory. + */ +QemuMutex migration_mutex; static MigrationState *current_migration; static MigrationIncomingState *current_incoming; @@ -110,6 +118,17 @@ static void migrate_fd_cancel(MigrationState *s); static bool close_return_path_on_source(MigrationState *s); static void migration_completion_end(MigrationState *s); +/* + * This is explicitly done without migration_object_init(), because it may + * start to use this lock already when instance_init() of the object. The + * mutex is alive for the whole lifecycle of QEMU, so it's always usable + * and never destroyed. + */ +static void __attribute__((constructor)) migration_mutex_init(void) +{ + qemu_mutex_init(&migration_mutex); +} + static void migration_downtime_start(MigrationState *s) { trace_vmstate_downtime_checkpoint("src-downtime-start"); @@ -336,6 +355,14 @@ void migration_shutdown(void) * stop the migration using this structure */ migration_cancel(NULL); + /* + * Release the refcount from the main thread. It means it can be freed + * here if migration thread is not running. + * + * NOTE: we don't need QEMU_MIGRATION_LOCK_GUARD() on this access + * because we're sure there's one refcount. The lock will be taken in + * finalize() if it triggers, so we can't take it here anyway. + */ object_unref(OBJECT(current_migration)); /* @@ -1118,8 +1145,14 @@ void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value) bool migration_is_running(void) { + QEMU_MIGRATION_LOCK_GUARD(); + MigrationState *s = current_migration; + if (!s) { + return false; + } + switch (s->state) { case MIGRATION_STATUS_ACTIVE: case MIGRATION_STATUS_POSTCOPY_ACTIVE: @@ -3029,8 +3062,14 @@ static MigThrError postcopy_pause(MigrationState *s) void migration_file_set_error(int ret, Error *err) { + QEMU_MIGRATION_LOCK_GUARD(); + MigrationState *s = current_migration; + if (!s) { + return; + } + WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) { if (s->to_dst_file) { qemu_file_set_error_obj(s->to_dst_file, ret, err); @@ -3835,6 +3874,8 @@ static void migration_instance_finalize(Object *obj) { MigrationState *ms = MIGRATION_OBJ(obj); + QEMU_MIGRATION_LOCK_GUARD(); + qemu_mutex_destroy(&ms->error_mutex); qemu_mutex_destroy(&ms->qemu_file_lock); qemu_sem_destroy(&ms->wait_unplug_sem); @@ -3858,6 +3899,8 @@ static void migration_instance_init(Object *obj) { MigrationState *ms = MIGRATION_OBJ(obj); + QEMU_MIGRATION_LOCK_GUARD(); + /* * There can only be one migration object globally. Keep a record of * the pointer in current_migration, which will be reset after the