From nobody Thu May 16 10:35:22 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=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1693507263; cv=none; d=zohomail.com; s=zohoarc; b=icYVz7n2gytsgC93YTEVBHpMRc1cy8hC1/uvw+g76oOwo0qFO0g9Eog8Eo9wr9dqXAitUCWdgCuPZu7BK7B9pDWRaOSxjTQdUbvWSqRBek0dOvEtDs5l//s+Ncsdsp2f7mySBT7K3ybQp4K2mRh97EYahjR9VRMgzUTn3B5HfX0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693507263; h=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=gQVDJc/VWRXwS0mHPY1vDLVZJ1suUUTu444PbUJHcVk=; b=T2L8wpFb2uBBrCw4tQGoxHyvHA55dDH99fSt6G8+7TjsGDTV7AfKaxk5Fl/XLp0ZyDsG1zl+Ugs2k0zFezOdGJpvMMVEjxp1K9FUJL+Cei/Zd2fA9W/8Q/5v1N+8s9boL4VzC+hyospHpCVpJBYgZo4a5oqMCIX23BGntUcuHUs= 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 1693507263931974.3348769927759; Thu, 31 Aug 2023 11:41:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qbma5-0001tq-Fr; Thu, 31 Aug 2023 14:39:50 -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 1qbmZi-0001Q2-Aa for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:26 -0400 Received: from smtp-out1.suse.de ([2001:67c:2178:6::1c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbmZg-0002ES-5R for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:26 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 9A7A52185F; Thu, 31 Aug 2023 18:39:22 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1C4C213583; Thu, 31 Aug 2023 18:39:20 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 6LvBNVje8GQhPgAAMHmgww (envelope-from ); Thu, 31 Aug 2023 18:39:20 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1693507162; h=from:from:reply-to: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=gQVDJc/VWRXwS0mHPY1vDLVZJ1suUUTu444PbUJHcVk=; b=lxZVRwEsdFatymMuZDrCUpoz0dDkhcn5pFvrwtLNozmGFN7hAzlxLxjc4OYcFGxEcJhHlQ ew+8RYgH5YLZAUK3wwsFEaMoFD0PyiV4eHpA8l2lDh4MAwV2FhMo64bFuHb0hmgwo5rGrI IkR0PUduvWiNVk7+7bfup9RIDSsuqm4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1693507162; h=from:from:reply-to: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=gQVDJc/VWRXwS0mHPY1vDLVZJ1suUUTu444PbUJHcVk=; b=BJltEc2E7QUQeTRQUYjIxoho2UFTp8RVcla3QvKByFSoEggE/uVaAnbtvPLrU1GNekKNHU G2sQb4T/2FSdv6Dg== From: Fabiano Rosas To: qemu-devel@nongnu.org Cc: Juan Quintela , Peter Xu , Wei Wang , Leonardo Bras Subject: [PATCH v5 1/8] migration: Fix possible race when setting rp_state.error Date: Thu, 31 Aug 2023 15:39:09 -0300 Message-Id: <20230831183916.13203-2-farosas@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230831183916.13203-1-farosas@suse.de> References: <20230831183916.13203-1-farosas@suse.de> 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=2001:67c:2178:6::1c; envelope-from=farosas@suse.de; helo=smtp-out1.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 @suse.de) X-ZM-MESSAGEID: 1693507265580100003 Content-Type: text/plain; charset="utf-8" We don't need to set the rp_state.error right after a shutdown because qemu_file_shutdown() always sets the QEMUFile error, so the return path thread would have seen it and set the rp error itself. Setting the error outside of the thread is also racy because the thread could clear it after we set it. Reviewed-by: Peter Xu Signed-off-by: Fabiano Rosas --- migration/migration.c | 1 - 1 file changed, 1 deletion(-) diff --git a/migration/migration.c b/migration/migration.c index 5528acb65e..f88c86079c 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -2062,7 +2062,6 @@ static int await_return_path_close_on_source(Migratio= nState *ms) * waiting for the destination. */ qemu_file_shutdown(ms->rp_state.from_dst_file); - mark_source_rp_bad(ms); } trace_await_return_path_close_on_source_joining(); qemu_thread_join(&ms->rp_state.rp_thread); --=20 2.35.3 From nobody Thu May 16 10:35:22 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=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1693507298; cv=none; d=zohomail.com; s=zohoarc; b=Mq8VYSSBe4birhLmbjAUt0Pbjlt0oh//jzJd4uBcqjZ40y1xrioplDE2m6roHv7zCp0LJHqqmrfXw0/0Sf0dFfDvqXPUsTMQjXBThURqUrce/WXCrvKmJXsLhprRiXDQy+PbYdpUCyTQkCoBQat2f5k3+f9KEX1wUo1bC+Ad3rw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693507298; h=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=QCN0lzhzxfoxI0dPzlD2+/IaDtzUb62BaW0g8BRMoDE=; b=PTg+Fx4oaRyQf+8maFo4WvjkbYZe7UJ8tG8F64WMSMWZ5ZgpnJV4KWuSCfu1priUzSWJ0WRpsHIWmsuxeihEvyPvMt91kPAdIkLspArwSMLk5Ni8vTg6Y/bouDux1IgMPzUF/mxnjwLB4vJCz1qa9KDfDuZ1cW6OQpfHJcOR2Mw= 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 1693507298196661.1989045178384; Thu, 31 Aug 2023 11:41:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qbma7-00023y-Bs; Thu, 31 Aug 2023 14:39:51 -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 1qbmZk-0001ch-L4 for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:30 -0400 Received: from smtp-out2.suse.de ([2001:67c:2178:6::1d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbmZi-0002Em-3O for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:28 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 89EFC1F749; Thu, 31 Aug 2023 18:39:24 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0F6AB13583; Thu, 31 Aug 2023 18:39:22 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 2FnIMlre8GQhPgAAMHmgww (envelope-from ); Thu, 31 Aug 2023 18:39:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1693507164; h=from:from:reply-to: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=QCN0lzhzxfoxI0dPzlD2+/IaDtzUb62BaW0g8BRMoDE=; b=spamaL2H/hgnqFKVuW0ZgToKMSiAwD0uTYAjZ1bWMn5og6B8lnknzljgirZubtDJ2siu+y xlOz3dQ1upORahw6H04rFpU6jR6QHOprdUNmQpciIDC9qTnWTHC4qYnf5ayijBWcpWzlZ1 bvDwQwsrYnSk9qW6d8DOPaaFdr0iNoQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1693507164; h=from:from:reply-to: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=QCN0lzhzxfoxI0dPzlD2+/IaDtzUb62BaW0g8BRMoDE=; b=svy5l5hOhuCpizUZ03irYwDz8F/FVirDdquWRdy2m9TUqyKtnymQ+D3rOGGiGWrAuoRufm ews/fnnFTKBoA6Ag== From: Fabiano Rosas To: qemu-devel@nongnu.org Cc: Juan Quintela , Peter Xu , Wei Wang , Leonardo Bras Subject: [PATCH v5 2/8] migration: Fix possible races when shutting down the return path Date: Thu, 31 Aug 2023 15:39:10 -0300 Message-Id: <20230831183916.13203-3-farosas@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230831183916.13203-1-farosas@suse.de> References: <20230831183916.13203-1-farosas@suse.de> 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=2001:67c:2178:6::1d; envelope-from=farosas@suse.de; helo=smtp-out2.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 @suse.de) X-ZM-MESSAGEID: 1693507298550100001 Content-Type: text/plain; charset="utf-8" We cannot call qemu_file_shutdown() on the return path file without taking the file lock. The return path thread could be running it's cleanup code and have just cleared the from_dst_file pointer. Checking ms->to_dst_file for errors could also race with migrate_fd_cleanup() which clears the to_dst_file pointer. Protect both accesses by taking the file lock. This was caught by inspection, it should be rare, but the next patches will start calling this code from other places, so let's do the correct thing. Reviewed-by: Peter Xu Signed-off-by: Fabiano Rosas --- migration/migration.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index f88c86079c..85c171f32c 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -2052,17 +2052,18 @@ static int open_return_path_on_source(MigrationStat= e *ms, static int await_return_path_close_on_source(MigrationState *ms) { /* - * If this is a normal exit then the destination will send a SHUT and = the - * rp_thread will exit, however if there's an error we need to cause - * it to exit. + * If this is a normal exit then the destination will send a SHUT + * and the rp_thread will exit, however if there's an error we + * need to cause it to exit. shutdown(2), if we have it, will + * cause it to unblock if it's stuck waiting for the destination. */ - if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file= ) { - /* - * shutdown(2), if we have it, will cause it to unblock if it's st= uck - * waiting for the destination. - */ - qemu_file_shutdown(ms->rp_state.from_dst_file); + WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) { + if (ms->to_dst_file && ms->rp_state.from_dst_file && + qemu_file_get_error(ms->to_dst_file)) { + qemu_file_shutdown(ms->rp_state.from_dst_file); + } } + trace_await_return_path_close_on_source_joining(); qemu_thread_join(&ms->rp_state.rp_thread); ms->rp_state.rp_thread_created =3D false; --=20 2.35.3 From nobody Thu May 16 10:35:22 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=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1693507256; cv=none; d=zohomail.com; s=zohoarc; b=UVvatj7WGC0dTkZzkL/WyCnDY1wY4lo0+CaPGq4KdqfQvvW3z9DV/SAmNQ0fqoiXiyg6XE7jK4kRLCgdp3al7L48s1q/VD8c9VdcRVjvWo3h76VFYORv0SW6tYsn7gkRxr1M/k4It+BUwfyqwaTlYuWaHJdv4pxhcDUJ31AEe3I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693507256; h=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=ratvuBX3qQ91vHO0LxE42YTYPtTJVqto9t43u0ml8kE=; b=WUMB/0re1P35vfeMsWCD/jqxDvvaxfe7DGBdm/q23rdUPCu/qIfI38ncmhN1Rq6LPRu/pz2lf2TgEVshj2xekIfCg1OU9+EnllPZzd3k7Y49BcwFktfLval5d9P4V0AoXTB+z4XOXeV5s4avOzYrbG00GhPZXDbxYXw/M8lfIEg= 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 1693507256716538.2204201123054; Thu, 31 Aug 2023 11:40:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qbma4-0001tr-8a; Thu, 31 Aug 2023 14:39:49 -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 1qbmZl-0001di-PD for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:33 -0400 Received: from smtp-out1.suse.de ([2001:67c:2178:6::1c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbmZj-0002FZ-J5 for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:29 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 77FBD2185E; Thu, 31 Aug 2023 18:39:26 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id F3F2C13583; Thu, 31 Aug 2023 18:39:24 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id CILYLlze8GQhPgAAMHmgww (envelope-from ); Thu, 31 Aug 2023 18:39:24 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1693507166; h=from:from:reply-to: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=ratvuBX3qQ91vHO0LxE42YTYPtTJVqto9t43u0ml8kE=; b=A2fTGlOeOOIIbAXOt/LAdFenSrik8D+MnULgZRrI7O+HWOdeDJGYl9esJLhUuhM7xUjaKc F4xPr7mlA21HZrLHY4qJmbBKmVy60U2Cn5wEyvb3M/t2pMi3rMFbGljNdaGld6Wh+aDJxY JFxnDGAlxN9p7Ry28S0yG9Vq++1htFo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1693507166; h=from:from:reply-to: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=ratvuBX3qQ91vHO0LxE42YTYPtTJVqto9t43u0ml8kE=; b=KDSq3fCW3nEtgRRLS2LOAXLyxOQ9SwHpI+FK/Mj6VbpSeZpUiXFE5DM/U8kwOMyDKBKsSd pLFpGuxXcsl6wfCw== From: Fabiano Rosas To: qemu-devel@nongnu.org Cc: Juan Quintela , Peter Xu , Wei Wang , Leonardo Bras Subject: [PATCH v5 3/8] migration: Fix possible race when shutting down to_dst_file Date: Thu, 31 Aug 2023 15:39:11 -0300 Message-Id: <20230831183916.13203-4-farosas@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230831183916.13203-1-farosas@suse.de> References: <20230831183916.13203-1-farosas@suse.de> 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=2001:67c:2178:6::1c; envelope-from=farosas@suse.de; helo=smtp-out1.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 @suse.de) X-ZM-MESSAGEID: 1693507258891100005 Content-Type: text/plain; charset="utf-8" It's not safe to call qemu_file_shutdown() on the to_dst_file without first checking for the file's presence under the lock. The cleanup of this file happens at postcopy_pause() and migrate_fd_cleanup() which are not necessarily running in the same thread as migrate_fd_cancel(). Reviewed-by: Peter Xu Signed-off-by: Fabiano Rosas --- migration/migration.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 85c171f32c..5e6a766235 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -1245,7 +1245,7 @@ static void migrate_fd_error(MigrationState *s, const= Error *error) static void migrate_fd_cancel(MigrationState *s) { int old_state ; - QEMUFile *f =3D migrate_get_current()->to_dst_file; + trace_migrate_fd_cancel(); =20 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) { @@ -1271,11 +1271,13 @@ static void migrate_fd_cancel(MigrationState *s) * If we're unlucky the migration code might be stuck somewhere in a * send/write while the network has failed and is waiting to timeout; * if we've got shutdown(2) available then we can force it to quit. - * The outgoing qemu file gets closed in migrate_fd_cleanup that is - * called in a bh, so there is no race against this cancel. */ - if (s->state =3D=3D MIGRATION_STATUS_CANCELLING && f) { - qemu_file_shutdown(f); + if (s->state =3D=3D MIGRATION_STATUS_CANCELLING) { + WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) { + if (s->to_dst_file) { + qemu_file_shutdown(s->to_dst_file); + } + } } if (s->state =3D=3D MIGRATION_STATUS_CANCELLING && s->block_inactive) { Error *local_err =3D NULL; @@ -1519,12 +1521,14 @@ void qmp_migrate_pause(Error **errp) { MigrationState *ms =3D migrate_get_current(); MigrationIncomingState *mis =3D migration_incoming_get_current(); - int ret; + int ret =3D 0; =20 if (ms->state =3D=3D MIGRATION_STATUS_POSTCOPY_ACTIVE) { /* Source side, during postcopy */ qemu_mutex_lock(&ms->qemu_file_lock); - ret =3D qemu_file_shutdown(ms->to_dst_file); + if (ms->to_dst_file) { + ret =3D qemu_file_shutdown(ms->to_dst_file); + } qemu_mutex_unlock(&ms->qemu_file_lock); if (ret) { error_setg(errp, "Failed to pause source migration"); --=20 2.35.3 From nobody Thu May 16 10:35:22 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=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1693507256; cv=none; d=zohomail.com; s=zohoarc; b=iCpWR1QmCA6sd4B9H9FN7smS7Ms5vgkck/y1WWGDk+ajYOQwe5pD6zN9t0HGGh7sJfm463ggwa/C9Qz33Hyg+7f73ODzfjhThdsQGoqh4rJFLhYvVBgQbhIaGp7rpZdwx2zqHT+cw1TwB+tGCqY7uHka+NFtcaZgymGvVJoAb48= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693507256; h=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=xmbFysED0AHDJV1mZ+g/WSZUPpQFtPzNFvXjvePLOVI=; b=bYWMbEM59GF195ZQr2fuFtV0iMTQzgSV6HKGKagI86qd88iJxVal4uLAxXIyLhWqAnzO9LupjZUybCRvhTfCLLcBVYaHi1SX8d3tjhv5mP2E0lksvAAJxHiN4mBVrZFveY74udT3aUZRpbxG4Gn29rDUlgUnzExAguvKpaUJyCQ= 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 1693507256951499.50785493876845; Thu, 31 Aug 2023 11:40:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qbmaB-0002Q0-9P; Thu, 31 Aug 2023 14:39:55 -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 1qbmZn-0001fH-Ht for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:35 -0400 Received: from smtp-out1.suse.de ([2001:67c:2178:6::1c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbmZl-0002G8-FL for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:31 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 66B1D2185F; Thu, 31 Aug 2023 18:39:28 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id E038B13583; Thu, 31 Aug 2023 18:39:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id qP5MKl7e8GQhPgAAMHmgww (envelope-from ); Thu, 31 Aug 2023 18:39:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1693507168; h=from:from:reply-to: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=xmbFysED0AHDJV1mZ+g/WSZUPpQFtPzNFvXjvePLOVI=; b=X1iVnvNjJPuB7anH5lKLjcqh0zYMpq0kNE+RptB2RJyRVqsGfT5THX//z7wmiPpQmRDmjI Dtzsnr9oMSGLq0E/y/e+nPiogfClmKAN0pwUlRCj8KZmvJoK+i0zWtae17PH4DK5Ej1fEi a+qBEI2T74BffH/1wfXkwhMHn/KTbV4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1693507168; h=from:from:reply-to: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=xmbFysED0AHDJV1mZ+g/WSZUPpQFtPzNFvXjvePLOVI=; b=NJ0HUlVnYW3APbknPicX6hKl0Uj45Yzfd3fgwTcMNtYWhulw12OIP/IlUmg/cIWtvDO6CF iZTc3RUA1Dfpm3DA== From: Fabiano Rosas To: qemu-devel@nongnu.org Cc: Juan Quintela , Peter Xu , Wei Wang , Leonardo Bras Subject: [PATCH v5 4/8] migration: Remove redundant cleanup of postcopy_qemufile_src Date: Thu, 31 Aug 2023 15:39:12 -0300 Message-Id: <20230831183916.13203-5-farosas@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230831183916.13203-1-farosas@suse.de> References: <20230831183916.13203-1-farosas@suse.de> 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=2001:67c:2178:6::1c; envelope-from=farosas@suse.de; helo=smtp-out1.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 @suse.de) X-ZM-MESSAGEID: 1693507259014100006 Content-Type: text/plain; charset="utf-8" This file is owned by the return path thread which is already doing cleanup. Reviewed-by: Peter Xu Signed-off-by: Fabiano Rosas --- migration/migration.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 5e6a766235..195726eb4a 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -1177,12 +1177,6 @@ static void migrate_fd_cleanup(MigrationState *s) qemu_fclose(tmp); } =20 - if (s->postcopy_qemufile_src) { - migration_ioc_unregister_yank_from_file(s->postcopy_qemufile_src); - qemu_fclose(s->postcopy_qemufile_src); - s->postcopy_qemufile_src =3D NULL; - } - assert(!migration_is_active(s)); =20 if (s->state =3D=3D MIGRATION_STATUS_CANCELLING) { --=20 2.35.3 From nobody Thu May 16 10:35:22 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=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1693507222; cv=none; d=zohomail.com; s=zohoarc; b=Go/OwqOY7e99Vtiko1GJWB7U1+BO5BwmAmiWjpIVidpRMcrR8tn+dE5Jcvaf5BdcDVVtYZ0Pk5nr38n3defS45KFByS5Wfimcot6FMv43BQC9SZj6q89mQF0npkvsdg6zlZtwjp7fysyWuE8CW4kDUmsowlxk1AyODLFTvT2a4g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693507222; h=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=M4F3Z9FAUn93sfwbK8FJ9e5DMMvpXA/8ArfN50R/0eg=; b=FeaBGNS9kmb9ehOvPKOkIljMLTMFIWbQ2JsLp3wvU8sJit2j4WVh0XcmpGcjuUx0j66SCUKX8ghziwTA1AzC4C+mpUaj9qFYYlY1xaRIXr8v+aIhl5xwPkaGo1X0n1qd6uZiMYlTXdhLfmnx1PC4flUs8jC0SGFtFCuk0ol561s= 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 1693507222219192.60747657865045; Thu, 31 Aug 2023 11:40:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qbmaB-0002R6-Sl; Thu, 31 Aug 2023 14:39:55 -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 1qbmZq-0001hz-B4 for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:37 -0400 Received: from smtp-out2.suse.de ([2001:67c:2178:6::1d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbmZn-0002GS-TZ for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:33 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 5A3EB1F749; Thu, 31 Aug 2023 18:39:30 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id CFD4E13583; Thu, 31 Aug 2023 18:39:28 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 0EIhJmDe8GQhPgAAMHmgww (envelope-from ); Thu, 31 Aug 2023 18:39:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1693507170; h=from:from:reply-to: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=M4F3Z9FAUn93sfwbK8FJ9e5DMMvpXA/8ArfN50R/0eg=; b=YY49IPwuVgRaHGXDoYmwmK3PBRzys/n/wvJO/jcL/Hof89m7twixB89mlBoNzuC0C9yszp tGhBK8Hhikh9KHSYFoxhb8nZzeImfIu747x3Hhrv7QEW6xDGd+I7JeEmn5P4X33SzrVggy n1UeXkwaICtKSqyedYdxtBQ1DT+qri4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1693507170; h=from:from:reply-to: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=M4F3Z9FAUn93sfwbK8FJ9e5DMMvpXA/8ArfN50R/0eg=; b=TxpgeyYqXqD5m81dTUte9iXg3NWfxCqHgJnwEp0oo7WZxdjjgsicE5Ri9Y+CLyLaCD2Xl1 2C8cSGay+tQ9ymAQ== From: Fabiano Rosas To: qemu-devel@nongnu.org Cc: Juan Quintela , Peter Xu , Wei Wang , Leonardo Bras Subject: [PATCH v5 5/8] migration: Consolidate return path closing code Date: Thu, 31 Aug 2023 15:39:13 -0300 Message-Id: <20230831183916.13203-6-farosas@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230831183916.13203-1-farosas@suse.de> References: <20230831183916.13203-1-farosas@suse.de> 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=2001:67c:2178:6::1d; envelope-from=farosas@suse.de; helo=smtp-out2.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 @suse.de) X-ZM-MESSAGEID: 1693507223489100001 Content-Type: text/plain; charset="utf-8" We'll start calling the await_return_path_close_on_source() function from other parts of the code, so move all of the related checks and tracepoints into it. Reviewed-by: Peter Xu Signed-off-by: Fabiano Rosas --- migration/migration.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 195726eb4a..4edbee3a5d 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -2049,6 +2049,14 @@ static int open_return_path_on_source(MigrationState= *ms, /* 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) { + int ret; + + if (!ms->rp_state.rp_thread_created) { + return 0; + } + + trace_migration_return_path_end_before(); + /* * If this is a normal exit then the destination will send a SHUT * and the rp_thread will exit, however if there's an error we @@ -2066,7 +2074,10 @@ static int await_return_path_close_on_source(Migrati= onState *ms) qemu_thread_join(&ms->rp_state.rp_thread); ms->rp_state.rp_thread_created =3D false; trace_await_return_path_close_on_source_close(); - return ms->rp_state.error; + + ret =3D ms->rp_state.error; + trace_migration_return_path_end_after(ret); + return ret; } =20 static inline void @@ -2362,20 +2373,8 @@ static void migration_completion(MigrationState *s) goto fail; } =20 - /* - * If rp was opened we must clean up the thread before - * cleaning everything else up (since if there are no failures - * it will wait for the destination to send it's status in - * a SHUT command). - */ - if (s->rp_state.rp_thread_created) { - int rp_error; - trace_migration_return_path_end_before(); - rp_error =3D await_return_path_close_on_source(s); - trace_migration_return_path_end_after(rp_error); - if (rp_error) { - goto fail; - } + if (await_return_path_close_on_source(s)) { + goto fail; } =20 if (qemu_file_get_error(s->to_dst_file)) { --=20 2.35.3 From nobody Thu May 16 10:35:22 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=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1693507289; cv=none; d=zohomail.com; s=zohoarc; b=eAjLJggotG0SY3pGVATggMEUWvw09xALM4lgukWQ/PVhCSRCJ+g461Qavw7NEqrsPuCfMtz9s/jMRIldo3usa3FHiJVCE843c6iJ3+561KwFASAWatIH/FQjIDpLWTJ6ux9+g2voWdOhGmFlgkGCp7g+jQY6+4SCK3vp4U0c++w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693507289; h=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=nsROk6EVy4STMdPRXEStC03FsfSuYWtANPTfYLuwSvk=; b=DnwuLCAbs0nvpYib5uqB1zijZ+O/vaPIEXL3KR3xz80+Gh8/0EuLv9hx2u7DyyjCs2YYdCxtV8ldSv2ujt7gZu8+NOHYLfwiPwJjKSSuiePWGD9h089qdTTp9KoMVddLTeixxIZyXWeCcfOxL3cSJOndnmzm9AkTLbKWbEqiVeM= 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 1693507289016926.6560191067939; Thu, 31 Aug 2023 11:41:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qbmaC-0002Rw-LK; Thu, 31 Aug 2023 14:39:56 -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 1qbmZs-0001n1-6e for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:42 -0400 Received: from smtp-out1.suse.de ([195.135.220.28]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbmZp-0002Gs-Eh for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:35 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 4F7102185E; Thu, 31 Aug 2023 18:39:32 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C589E13583; Thu, 31 Aug 2023 18:39:30 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id MGt9I2Le8GQhPgAAMHmgww (envelope-from ); Thu, 31 Aug 2023 18:39:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1693507172; h=from:from:reply-to: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=nsROk6EVy4STMdPRXEStC03FsfSuYWtANPTfYLuwSvk=; b=r0ETOpR1nYk0ihTof6sCrIExuDvUZE/UR6h3hHw8mAjmlhjHoNfvRe+oq/SVv0gHUDPiDp +mhz0N6UJgUBjcQTxGesqor+U8XHKbJFKa1xUvaRh8auymmtvbpcijkP2jtIDsM65qx9l9 rKKUly8YEJyW1/x+z+bcr9TC8mKuXNw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1693507172; h=from:from:reply-to: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=nsROk6EVy4STMdPRXEStC03FsfSuYWtANPTfYLuwSvk=; b=HT0LQzUAULO7gUJhjl7yhJewLgycyDBPbwsbs5i2WAhvJFgGNM8g4gAsqcj2/jrgvAi2H1 oM1CBeU+YPk11LBg== From: Fabiano Rosas To: qemu-devel@nongnu.org Cc: Juan Quintela , Peter Xu , Wei Wang , Leonardo Bras Subject: [PATCH v5 6/8] migration: Replace the return path retry logic Date: Thu, 31 Aug 2023 15:39:14 -0300 Message-Id: <20230831183916.13203-7-farosas@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230831183916.13203-1-farosas@suse.de> References: <20230831183916.13203-1-farosas@suse.de> 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=195.135.220.28; envelope-from=farosas@suse.de; helo=smtp-out1.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 @suse.de) X-ZM-MESSAGEID: 1693507289535100001 Content-Type: text/plain; charset="utf-8" Replace the return path retry logic with finishing and restarting the thread. This fixes a race when resuming the migration that leads to a segfault. Currently when doing postcopy we consider that an IO error on the return path file could be due to a network intermittency. We then keep the thread alive but have it do cleanup of the 'from_dst_file' and wait on the 'postcopy_pause_rp' semaphore. When the user issues a migrate resume, a new return path is opened and the thread is allowed to continue. There's a race condition in the above mechanism. It is possible for the new return path file to be setup *before* the cleanup code in the return path thread has had a chance to run, leading to the *new* file being closed and the pointer set to NULL. When the thread is released after the resume, it tries to dereference 'from_dst_file' and crashes: Thread 7 "return path" received signal SIGSEGV, Segmentation fault. [Switching to Thread 0x7fffd1dbf700 (LWP 9611)] 0x00005555560e4893 in qemu_file_get_error_obj (f=3D0x0, errp=3D0x0) at ../m= igration/qemu-file.c:154 154 return f->last_error; (gdb) bt #0 0x00005555560e4893 in qemu_file_get_error_obj (f=3D0x0, errp=3D0x0) at= ../migration/qemu-file.c:154 #1 0x00005555560e4983 in qemu_file_get_error (f=3D0x0) at ../migration/qe= mu-file.c:206 #2 0x0000555555b9a1df in source_return_path_thread (opaque=3D0x555556e060= 00) at ../migration/migration.c:1876 #3 0x000055555602e14f in qemu_thread_start (args=3D0x55555782e780) at ../= util/qemu-thread-posix.c:541 #4 0x00007ffff38d76ea in start_thread (arg=3D0x7fffd1dbf700) at pthread_c= reate.c:477 #5 0x00007ffff35efa6f in clone () at ../sysdeps/unix/sysv/linux/x86_64/cl= one.S:95 Here's the race (important bit is open_return_path happening before migration_release_dst_files): migration | qmp | return path Reviewed-by: Peter Xu --------------------------+-----------------------------+------------------= --------------- qmp_migrate_pause() shutdown(ms->to_dst_file) f->last_error =3D -EIO migrate_detect_error() postcopy_pause() set_state(PAUSED) wait(postcopy_pause_sem) qmp_migrate(resume) migrate_fd_connect() resume =3D state =3D=3D PAUSED open_return_path <-- TOO SOON! set_state(RECOVER) post(postcopy_pause_sem) (incoming closes to_src_file) res =3D qemu_file_get_error(rp) migration_release_dst_files() ms->rp_state.from_dst_file =3D NULL post(postcopy_pause_rp_sem) postcopy_pause_return_path_thread() wait(postcopy_pause_rp_sem) rp =3D ms->rp_state.from_dst_file goto retry qemu_file_get_error(rp) SIGSEGV ---------------------------------------------------------------------------= ---------------- We can keep the retry logic without having the thread alive and waiting. The only piece of data used by it is the 'from_dst_file' and it is only allowed to proceed after a migrate resume is issued and the semaphore released at migrate_fd_connect(). Move the retry logic to outside the thread by waiting for the thread to finish before pausing the migration. Reviewed-by: Peter Xu Signed-off-by: Fabiano Rosas --- migration/migration.c | 60 ++++++++----------------------------------- migration/migration.h | 1 - 2 files changed, 11 insertions(+), 50 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 4edbee3a5d..7dfcbc3634 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -1775,18 +1775,6 @@ static void migrate_handle_rp_req_pages(MigrationSta= te *ms, const char* rbname, } } =20 -/* Return true to retry, false to quit */ -static bool postcopy_pause_return_path_thread(MigrationState *s) -{ - trace_postcopy_pause_return_path(); - - qemu_sem_wait(&s->postcopy_pause_rp_sem); - - trace_postcopy_pause_return_path_continued(); - - return true; -} - static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_na= me) { RAMBlock *block =3D qemu_ram_block_by_name(block_name); @@ -1870,7 +1858,6 @@ static void *source_return_path_thread(void *opaque) trace_source_return_path_thread_entry(); rcu_register_thread(); =20 -retry: while (!ms->rp_state.error && !qemu_file_get_error(rp) && migration_is_setup_or_active(ms->state)) { trace_source_return_path_thread_loop_top(); @@ -1992,26 +1979,7 @@ retry: } =20 out: - res =3D qemu_file_get_error(rp); - if (res) { - if (res && migration_in_postcopy()) { - /* - * Maybe there is something we can do: it looks like a - * network down issue, and we pause for a recovery. - */ - migration_release_dst_files(ms); - rp =3D NULL; - if (postcopy_pause_return_path_thread(ms)) { - /* - * Reload rp, reset the rest. Referencing it is safe since - * it's reset only by us above, or when migration completes - */ - rp =3D ms->rp_state.from_dst_file; - ms->rp_state.error =3D false; - goto retry; - } - } - + if (qemu_file_get_error(rp)) { trace_source_return_path_thread_bad_end(); mark_source_rp_bad(ms); } @@ -2022,8 +1990,7 @@ out: return NULL; } =20 -static int open_return_path_on_source(MigrationState *ms, - bool create_thread) +static int open_return_path_on_source(MigrationState *ms) { ms->rp_state.from_dst_file =3D qemu_file_get_return_path(ms->to_dst_fi= le); if (!ms->rp_state.from_dst_file) { @@ -2032,11 +1999,6 @@ static int open_return_path_on_source(MigrationState= *ms, =20 trace_open_return_path_on_source(); =20 - if (!create_thread) { - /* We're done */ - return 0; - } - qemu_thread_create(&ms->rp_state.rp_thread, "return path", source_return_path_thread, ms, QEMU_THREAD_JOINABLE= ); ms->rp_state.rp_thread_created =3D true; @@ -2076,6 +2038,7 @@ static int await_return_path_close_on_source(Migratio= nState *ms) trace_await_return_path_close_on_source_close(); =20 ret =3D ms->rp_state.error; + ms->rp_state.error =3D false; trace_migration_return_path_end_after(ret); return ret; } @@ -2551,6 +2514,13 @@ static MigThrError postcopy_pause(MigrationState *s) qemu_file_shutdown(file); qemu_fclose(file); =20 + /* + * We're already pausing, so ignore any errors on the return + * path and just wait for the thread to finish. It will be + * re-created when we resume. + */ + await_return_path_close_on_source(s); + migrate_set_state(&s->state, s->state, MIGRATION_STATUS_POSTCOPY_PAUSED); =20 @@ -2568,12 +2538,6 @@ static MigThrError postcopy_pause(MigrationState *s) if (s->state =3D=3D MIGRATION_STATUS_POSTCOPY_RECOVER) { /* Woken up by a recover procedure. Give it a shot */ =20 - /* - * Firstly, let's wake up the return path now, with a new - * return path channel. - */ - qemu_sem_post(&s->postcopy_pause_rp_sem); - /* Do the resume logic */ if (postcopy_do_resume(s) =3D=3D 0) { /* Let's continue! */ @@ -3263,7 +3227,7 @@ void migrate_fd_connect(MigrationState *s, Error *err= or_in) * QEMU uses the return path. */ if (migrate_postcopy_ram() || migrate_return_path()) { - if (open_return_path_on_source(s, !resume)) { + if (open_return_path_on_source(s)) { error_setg(&local_err, "Unable to open return-path for postcop= y"); migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED= ); migrate_set_error(s, local_err); @@ -3327,7 +3291,6 @@ static void migration_instance_finalize(Object *obj) qemu_sem_destroy(&ms->rate_limit_sem); qemu_sem_destroy(&ms->pause_sem); qemu_sem_destroy(&ms->postcopy_pause_sem); - qemu_sem_destroy(&ms->postcopy_pause_rp_sem); qemu_sem_destroy(&ms->rp_state.rp_sem); qemu_sem_destroy(&ms->rp_state.rp_pong_acks); qemu_sem_destroy(&ms->postcopy_qemufile_src_sem); @@ -3347,7 +3310,6 @@ static void migration_instance_init(Object *obj) migrate_params_init(&ms->parameters); =20 qemu_sem_init(&ms->postcopy_pause_sem, 0); - qemu_sem_init(&ms->postcopy_pause_rp_sem, 0); qemu_sem_init(&ms->rp_state.rp_sem, 0); qemu_sem_init(&ms->rp_state.rp_pong_acks, 0); qemu_sem_init(&ms->rate_limit_sem, 0); diff --git a/migration/migration.h b/migration/migration.h index 6eea18db36..36eb5ba70b 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -382,7 +382,6 @@ struct MigrationState { =20 /* Needed by postcopy-pause state */ QemuSemaphore postcopy_pause_sem; - QemuSemaphore postcopy_pause_rp_sem; /* * Whether we abort the migration if decompression errors are * detected at the destination. It is left at false for qemu --=20 2.35.3 From nobody Thu May 16 10:35:22 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=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1693507222; cv=none; d=zohomail.com; s=zohoarc; b=m9FlWDiW7mSIakCJN8zk9JcIGvLEOkYwK/ooYh5IbhRkB2lPBCzjoMnD8WrJfAb03X4Hm/e3ptX0iw6Mp/d7BBaWWbRpy7eWidTGkM422LA70/P6fnqosmBXoiNWScL0QQzSE4PRmIm6Uj5HJ7lN5FAsEJXXCqcXGQSQWVxtNb8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693507222; h=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=DGhGE717fUJFvyUY5vhbyY/GLiHbUzH5b0cd6j63xVg=; b=N1XDNHk5knjfK6Bgo20u1pSGfwh5X7MiaTcUxtDjDs3HKtAlaAbsg0DC1j/DS5yRLef7KX9FSfYYO71F1d9NuNQSmQcoJUqWMhsBTN7IJ4ehH1B0vwd3tl/XdhaP/YDW3sNEhK2mcKplKLxJjPMbDEXyJb/L/oTJeXEFpgPz+sk= 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 169350722245992.38656298976605; Thu, 31 Aug 2023 11:40:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qbmaF-0002SP-7X; Thu, 31 Aug 2023 14:39:59 -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 1qbmZt-0001n6-WE for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:42 -0400 Received: from smtp-out1.suse.de ([2001:67c:2178:6::1c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbmZr-0002HU-E0 for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:37 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 459E12185F; Thu, 31 Aug 2023 18:39:34 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id BBF0713583; Thu, 31 Aug 2023 18:39:32 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id CIFBIWTe8GQhPgAAMHmgww (envelope-from ); Thu, 31 Aug 2023 18:39:32 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1693507174; h=from:from:reply-to: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=DGhGE717fUJFvyUY5vhbyY/GLiHbUzH5b0cd6j63xVg=; b=TOxT/dtOxKvYYYFwfmHWIRLq6lS6a9z7EoGsK0F2B2TRkkbIduuXNc+em/LLH7LF0jo4D3 qas/5fJ83YH/3fLsZr93XI2F9NW7cKRwkG3BKLgKuLv8c+mYKZJ4Sfzuv4ReW6BPEAr/Nx pjUb9jSI4LCTfSgzXJn8/zHbFeZ4T/E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1693507174; h=from:from:reply-to: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=DGhGE717fUJFvyUY5vhbyY/GLiHbUzH5b0cd6j63xVg=; b=xtFVarFzxSI5O7pkqcv5//mdi8kkFYvi3HhN2ctrhti8v5PF69ejnF3KDp57gTg/3EpPrV prceE3gjlK82TVCA== From: Fabiano Rosas To: qemu-devel@nongnu.org Cc: Juan Quintela , Peter Xu , Wei Wang , Leonardo Bras Subject: [PATCH v5 7/8] migration: Move return path cleanup to main migration thread Date: Thu, 31 Aug 2023 15:39:15 -0300 Message-Id: <20230831183916.13203-8-farosas@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230831183916.13203-1-farosas@suse.de> References: <20230831183916.13203-1-farosas@suse.de> 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=2001:67c:2178:6::1c; envelope-from=farosas@suse.de; helo=smtp-out1.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 @suse.de) X-ZM-MESSAGEID: 1693507224252100005 Content-Type: text/plain; charset="utf-8" Now that the return path thread is allowed to finish during a paused migration, we can move the cleanup of the QEMUFiles to the main migration thread. Reviewed-by: Peter Xu Signed-off-by: Fabiano Rosas --- migration/migration.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/migration/migration.c b/migration/migration.c index 7dfcbc3634..7fec57ad7f 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -98,6 +98,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); =20 static bool migration_needs_multiple_sockets(void) { @@ -1177,6 +1178,12 @@ static void migrate_fd_cleanup(MigrationState *s) qemu_fclose(tmp); } =20 + /* + * 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); + assert(!migration_is_active(s)); =20 if (s->state =3D=3D MIGRATION_STATUS_CANCELLING) { @@ -1985,7 +1992,6 @@ out: } =20 trace_source_return_path_thread_end(); - migration_release_dst_files(ms); rcu_unregister_thread(); return NULL; } @@ -2039,6 +2045,9 @@ static int await_return_path_close_on_source(Migratio= nState *ms) =20 ret =3D ms->rp_state.error; ms->rp_state.error =3D false; + + migration_release_dst_files(ms); + trace_migration_return_path_end_after(ret); return ret; } --=20 2.35.3 From nobody Thu May 16 10:35:22 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=suse.de ARC-Seal: i=1; a=rsa-sha256; t=1693507228; cv=none; d=zohomail.com; s=zohoarc; b=ZbdS7WrUsKJp1dhrw9rjbWLX4g5L0G9W1fS1XWuW1lShEMjljPzHtPQetGBcUsRvNKzwLwDFJOkuwKQGhE37pqDJp2FKEn3pY4mSrtNgkfEZF9G7EkKwL2oJ0wjLlzv+6V8ByylRoaJtvkrkoxNsGWmQzr1mAEv/dTk67H6aG5g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693507228; h=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=swlq77dy5YMKKTArUbn3kVk9WsfzhHqGuEgL1/CaOcM=; b=jDouTzu6B/f8w/knY+RBc0ZUN9lKbAN7R6qLKUXY+ilKHll+GLNUbaD0Qo8A2FCsI2aHGmAq1eAvT5O2BIV5kywIiJQi6n3/3Qr3ve39/zvD6jjgB996+8b4OnikOlNMOv+kIkYrVPuc+8BuytwYI/5c22bdCg/Nv6xSXpwOtzk= 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 1693507228111619.761616182834; Thu, 31 Aug 2023 11:40:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qbmaD-0002S6-H0; Thu, 31 Aug 2023 14:39:57 -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 1qbmZw-0001nN-2j for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:43 -0400 Received: from smtp-out1.suse.de ([2001:67c:2178:6::1c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbmZt-0002I7-LL for qemu-devel@nongnu.org; Thu, 31 Aug 2023 14:39:39 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 6CDE32185E; Thu, 31 Aug 2023 18:39:36 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id B0DE313583; Thu, 31 Aug 2023 18:39:34 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 4LSDHmbe8GQhPgAAMHmgww (envelope-from ); Thu, 31 Aug 2023 18:39:34 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1693507176; h=from:from:reply-to: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=swlq77dy5YMKKTArUbn3kVk9WsfzhHqGuEgL1/CaOcM=; b=SRbdK7is1oHdqq4ddneyNK/QIPCMPW709r5e9cPJSYPLwp4peJUIgkdTGxhwEXOp6u4GRu 1ZQCwEGFq3l39XSyKX8syed9Y8Zm9ww8JwHyvG0SAUNjqVSv21NtpGX1c2HkbVCcNSmYg8 j/Dkvggzom+txHxlPAbJdGO1pwK68ZM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1693507176; h=from:from:reply-to: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=swlq77dy5YMKKTArUbn3kVk9WsfzhHqGuEgL1/CaOcM=; b=UwzeuiXzDwe8jFoP/GMKzu6pPhg3DKRalEdPdtiAra80pA/ZiTSEwU2vbh0T86T3IeNyIv fKEwF4J7gP5HZeCA== From: Fabiano Rosas To: qemu-devel@nongnu.org Cc: Juan Quintela , Peter Xu , Wei Wang , Leonardo Bras , Lukas Straub Subject: [PATCH v5 8/8] migration: Add a wrapper to cleanup migration files Date: Thu, 31 Aug 2023 15:39:16 -0300 Message-Id: <20230831183916.13203-9-farosas@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230831183916.13203-1-farosas@suse.de> References: <20230831183916.13203-1-farosas@suse.de> 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=2001:67c:2178:6::1c; envelope-from=farosas@suse.de; helo=smtp-out1.suse.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, 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 @suse.de) X-ZM-MESSAGEID: 1693507230233100001 Content-Type: text/plain; charset="utf-8" We currently have a pattern for cleaning up a migration QEMUFile: qemu_mutex_lock(&s->qemu_file_lock); file =3D s->file_name; s->file_name =3D NULL; qemu_mutex_unlock(&s->qemu_file_lock); migration_ioc_unregister_yank_from_file(file); qemu_file_shutdown(file); qemu_fclose(file); This sequence requires some consideration about locking to avoid TOC/TOU bugs and avoid passing NULL into the functions that don't expect it. There's not need to call a shutdown() right before a close() and a shutdown() in another thread being issued as a means to unblock a file should not collide with this close(). Create a wrapper function to make sure the locking is being done properly. Remove the extra shutdown(). The yank is linked to the QIOChannel, so if more than one QEMUFile share the same channel, care must be taken to (un)register only one yank function. Move the yank unregister before clearing the pointer, so we can avoid locking and add a comment explaining we're only using the QEMUFile as a way to access the channel. Signed-off-by: Fabiano Rosas Reviewed-by: Peter Xu --- migration/migration.c | 93 ++++++++++++-------------------------- migration/yank_functions.c | 5 ++ 2 files changed, 35 insertions(+), 63 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 7fec57ad7f..99d21c3442 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -217,6 +217,25 @@ MigrationIncomingState *migration_incoming_get_current= (void) return current_incoming; } =20 +static void migration_file_release(QEMUFile **file) +{ + MigrationState *ms =3D migrate_get_current(); + QEMUFile *tmp; + + /* + * Reset the pointer before releasing it to avoid holding the lock + * for too long. + */ + WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) { + tmp =3D *file; + *file =3D NULL; + } + + if (tmp) { + qemu_fclose(tmp); + } +} + void migration_incoming_transport_cleanup(MigrationIncomingState *mis) { if (mis->socket_address_list) { @@ -1155,8 +1174,6 @@ static void migrate_fd_cleanup(MigrationState *s) qemu_savevm_state_cleanup(); =20 if (s->to_dst_file) { - QEMUFile *tmp; - trace_migrate_fd_cleanup(); qemu_mutex_unlock_iothread(); if (s->migration_thread_running) { @@ -1166,16 +1183,9 @@ static void migrate_fd_cleanup(MigrationState *s) qemu_mutex_lock_iothread(); =20 multifd_save_cleanup(); - qemu_mutex_lock(&s->qemu_file_lock); - tmp =3D s->to_dst_file; - s->to_dst_file =3D NULL; - qemu_mutex_unlock(&s->qemu_file_lock); - /* - * Close the file handle without the lock to make sure the - * critical section won't block for long. - */ - migration_ioc_unregister_yank_from_file(tmp); - qemu_fclose(tmp); + + migration_ioc_unregister_yank_from_file(s->to_dst_file); + migration_file_release(&s->to_dst_file); } =20 /* @@ -1815,38 +1825,6 @@ static int migrate_handle_rp_resume_ack(MigrationSta= te *s, uint32_t value) return 0; } =20 -/* - * Release ms->rp_state.from_dst_file (and postcopy_qemufile_src if - * existed) in a safe way. - */ -static void migration_release_dst_files(MigrationState *ms) -{ - QEMUFile *file; - - WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) { - /* - * Reset the from_dst_file pointer first before releasing it, as we - * can't block within lock section - */ - file =3D ms->rp_state.from_dst_file; - ms->rp_state.from_dst_file =3D NULL; - } - - /* - * Do the same to postcopy fast path socket too if there is. No - * locking needed because this qemufile should only be managed by - * return path thread. - */ - if (ms->postcopy_qemufile_src) { - migration_ioc_unregister_yank_from_file(ms->postcopy_qemufile_src); - qemu_file_shutdown(ms->postcopy_qemufile_src); - qemu_fclose(ms->postcopy_qemufile_src); - ms->postcopy_qemufile_src =3D NULL; - } - - qemu_fclose(file); -} - /* * Handles messages sent on the return path towards the source VM * @@ -2046,7 +2024,12 @@ static int await_return_path_close_on_source(Migrati= onState *ms) ret =3D ms->rp_state.error; ms->rp_state.error =3D false; =20 - migration_release_dst_files(ms); + migration_file_release(&ms->rp_state.from_dst_file); + + if (ms->postcopy_qemufile_src) { + migration_ioc_unregister_yank_from_file(ms->postcopy_qemufile_src); + } + migration_file_release(&ms->postcopy_qemufile_src); =20 trace_migration_return_path_end_after(ret); return ret; @@ -2502,26 +2485,10 @@ static MigThrError postcopy_pause(MigrationState *s) assert(s->state =3D=3D MIGRATION_STATUS_POSTCOPY_ACTIVE); =20 while (true) { - QEMUFile *file; - - /* - * Current channel is possibly broken. Release it. Note that this= is - * guaranteed even without lock because to_dst_file should only be - * modified by the migration thread. That also guarantees that the - * unregister of yank is safe too without the lock. It should be = safe - * even to be within the qemu_file_lock, but we didn't do that to = avoid - * taking more mutex (yank_lock) within qemu_file_lock. TL;DR: we= make - * the qemu_file_lock critical section as small as possible. - */ + /* Current channel is possibly broken. Release it. */ assert(s->to_dst_file); migration_ioc_unregister_yank_from_file(s->to_dst_file); - qemu_mutex_lock(&s->qemu_file_lock); - file =3D s->to_dst_file; - s->to_dst_file =3D NULL; - qemu_mutex_unlock(&s->qemu_file_lock); - - qemu_file_shutdown(file); - qemu_fclose(file); + migration_file_release(&s->to_dst_file); =20 /* * We're already pausing, so ignore any errors on the return diff --git a/migration/yank_functions.c b/migration/yank_functions.c index d5a710a3f2..31b0d790e2 100644 --- a/migration/yank_functions.c +++ b/migration/yank_functions.c @@ -48,6 +48,11 @@ void migration_ioc_unregister_yank(QIOChannel *ioc) } } =20 +/* + * There's no direct relationship between the QEMUFile and the + * yank. This is just a convenience helper because the QIOChannel and + * the QEMUFile lifecycles happen to match. + */ void migration_ioc_unregister_yank_from_file(QEMUFile *file) { QIOChannel *ioc =3D qemu_file_get_ioc(file); --=20 2.35.3