From nobody Sat Nov 23 20:57:37 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=1730304128; cv=none; d=zohomail.com; s=zohoarc; b=SZYoyRgh4GBPnpksXp6nGNu/Zkxm0fPCDIxLpkwv6kDDtusYXDLCXZC9tOPATUEnwPkYBbmfPe6iZk4BtKvjyFH75SL8XuqZeSkZ4QhW9j631+uITJ9C0IlULhrpNAQQbI6mu2sqdZyl1E2QjNFJ0w11NgS0V5qfjgs1v+Pswlg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1730304128; 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=Fq34e+xBKmQ7ga4DrHPUzO43nYUW5RRJHXBVVJfk/Ig=; b=RI6MdtF8eOODSCoevjIqjbZAPsK5hotAHv4ah4kTnig29iq1TRL3OSQeGd2MaBX4doN+ASwsCL4f6ybdve2cT3hH2BRYMmtTpKPzrVtfDekeeUUg267OEKZiPNBHFNOgBqofjx6RcEwvI+NQp/cVYBnbplbb95iAEc/n7ZB3Axw= 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 1730304128504902.5168166775817; Wed, 30 Oct 2024 09:02:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t6B4s-0003Vz-Ek; Wed, 30 Oct 2024 11:57:46 -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 1t6B4q-0003VV-G6 for qemu-devel@nongnu.org; Wed, 30 Oct 2024 11:57:44 -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 1t6B4o-0007ke-F4 for qemu-devel@nongnu.org; Wed, 30 Oct 2024 11:57:44 -0400 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-674--EU3NgS7OGqrhYd2gIngfQ-1; Wed, 30 Oct 2024 11:57:39 -0400 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-6d19597cee0so49692256d6.3 for ; Wed, 30 Oct 2024 08:57:39 -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 6a1803df08f44-6d19c57c1ffsm22802276d6.89.2024.10.30.08.57.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Oct 2024 08:57:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730303861; 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=Fq34e+xBKmQ7ga4DrHPUzO43nYUW5RRJHXBVVJfk/Ig=; b=fdO2vxhwQuEK1YQAxcxg24y/FKHnyy4ZqaC14ZWKYyxhnN9N2R1Jwe1UtpWdKJUw9j3wJ/ qFSLMs5hG47unPPi9ZYuUMfcPPByiXzonsjJ3sBpgxsRcXVGBRAQ+uGaTp7I8gASD27s2d k63dxx8aGmnq+md4W8WxowZctoTYB8w= X-MC-Unique: -EU3NgS7OGqrhYd2gIngfQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730303858; x=1730908658; 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=Fq34e+xBKmQ7ga4DrHPUzO43nYUW5RRJHXBVVJfk/Ig=; b=kOclD0fG/CZ6/fpeofbN8MB2trUG4QAORzh48+aorX2RirFy1RPb6MJ9tt+PIvmxMV VCu1b4XOFiK955EutQDOf+6d9aEH4DHgaadZudupilOl/5SZa19nRoRr8Kh7ITBCYyCP rDyp4dHfql7JBuQ1bhaC/8HhCrnRK/dom2E2IDBn586A9QhqM0aHB6p5lZlo95bLWeyQ dG6QIukseu/7JTqIBIyoJZVLqIFRs/pM8LKMEQm2l29zI2gag15TMNTfLNUL4WI4TdBK DGoCKa1Lr992P6llnOx+wgYhp6k4q/EpKvyB8y/KXrkXFG9gjSKEhAeR7Wg7XrU/TtGe wCAQ== X-Gm-Message-State: AOJu0YypMJcKTIydF0ZWKWXc+6Rqb6GezyecbZqZL0RD5x16kLn8+92t IGMmYwYXc5Ilk1KhYAFh2DSFa5Q9vwK/EkD0fsRdmoXxg48naRbGFllMwTKIkl8Qjsz4W2TBy0+ TVeyCAbKTzkr5n0e58wBzGSi7uihDUWsydBYezCasMUCtm7L53zjdL1y6EdE0y7f978UIAMwAoS n8FzK6YB7N6tlyb6UE+H3mRjUynEdwpcoLog== X-Received: by 2002:a05:6214:568c:b0:6d1:939f:d861 with SMTP id 6a1803df08f44-6d346020777mr52962706d6.26.1730303858004; Wed, 30 Oct 2024 08:57:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEyEdN9R8MUVctyLByAlUSpYHSmqO/oODMoLGF/V9rG16auW8l9Mt3FR/CmTzRY86q2lq1TTg== X-Received: by 2002:a05:6214:568c:b0:6d1:939f:d861 with SMTP id 6a1803df08f44-6d346020777mr52962396d6.26.1730303857658; Wed, 30 Oct 2024 08:57:37 -0700 (PDT) From: Peter Xu To: qemu-devel@nongnu.org Cc: Fabiano Rosas , Peter Maydell , Peter Xu Subject: [PULL 01/18] migration: Cleanup migrate_fd_cleanup() on accessing to_dst_file Date: Wed, 30 Oct 2024 11:57:17 -0400 Message-ID: <20241030155734.2141398-2-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241030155734.2141398-1-peterx@redhat.com> References: <20241030155734.2141398-1-peterx@redhat.com> MIME-Version: 1.0 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=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.366, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1730304129565116600 Content-Type: text/plain; charset="utf-8" The cleanup function can in many cases needs cleanup on its own. The major thing we want to do here is not referencing to_dst_file when without the file mutex. When at it, touch things elsewhere too to make it look slightly better in general. One thing to mention is, migration_thread has its own "running" boolean, so it doesn't need to rely on to_dst_file being non-NULL. Multifd has a dependency so it needs to be skipped if to_dst_file is not yet set; add a richer comment for such reason. Resolves: Coverity CID 1527402 Reported-by: Peter Maydell Reviewed-by: Fabiano Rosas Link: https://lore.kernel.org/r/20240919163042.116767-1-peterx@redhat.com Signed-off-by: Peter Xu --- migration/migration.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 021faee2f3..f5f428e764 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -1405,6 +1405,9 @@ void migrate_set_state(MigrationStatus *state, Migrat= ionStatus old_state, static void migrate_fd_cleanup(MigrationState *s) { MigrationEventType type; + QEMUFile *tmp =3D NULL; + + trace_migrate_fd_cleanup(); =20 g_free(s->hostname); s->hostname =3D NULL; @@ -1415,26 +1418,29 @@ static void migrate_fd_cleanup(MigrationState *s) =20 close_return_path_on_source(s); =20 - if (s->to_dst_file) { - QEMUFile *tmp; - - trace_migrate_fd_cleanup(); + if (s->migration_thread_running) { bql_unlock(); - if (s->migration_thread_running) { - qemu_thread_join(&s->thread); - s->migration_thread_running =3D false; - } + qemu_thread_join(&s->thread); + s->migration_thread_running =3D false; bql_lock(); + } =20 - multifd_send_shutdown(); - qemu_mutex_lock(&s->qemu_file_lock); + WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) { + /* + * Close the file handle without the lock to make sure the critical + * section won't block for long. + */ tmp =3D s->to_dst_file; s->to_dst_file =3D NULL; - qemu_mutex_unlock(&s->qemu_file_lock); + } + + if (tmp) { /* - * Close the file handle without the lock to make sure the - * critical section won't block for long. + * We only need to shutdown multifd if tmp!=3DNULL, because if + * tmp=3D=3DNULL, it means the main channel isn't established, whi= le + * multifd is only setup after that (in migration_thread()). */ + multifd_send_shutdown(); migration_ioc_unregister_yank_from_file(tmp); qemu_fclose(tmp); } --=20 2.45.0