From mboxrd@z Thu Jan 1 00:00:00 1970 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 9E7C6E71D3F for ; Fri, 29 Sep 2023 14:42:39 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qmEh0-0008Bn-0z; Fri, 29 Sep 2023 10:42:10 -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 1qmEgp-00081G-Vv for qemu-devel@nongnu.org; Fri, 29 Sep 2023 10:42:03 -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 1qmEge-0002bY-Ft for qemu-devel@nongnu.org; Fri, 29 Sep 2023 10:41:59 -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 912562188D; Fri, 29 Sep 2023 14:41:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1695998505; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=FdapKdgAB/z7PZAZNv3YF6w3zYY5CJjhQdUjj/6rUFw=; b=e8YBNn5ln9do7aEb3WrGPm7LvOoeNoryMHzP/JqNu/35LT/pPGADs27B+MIOOUAU4H+Y4r wTiDYg5CJam5AFhI1af7tpkq68eskPsjdM6OaQCmNKbDLJeEt/X0ag/ca216KOxsmMRATr VPgmE98PryoGGoESdp5SsXzY9KwHGb8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1695998505; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=FdapKdgAB/z7PZAZNv3YF6w3zYY5CJjhQdUjj/6rUFw=; b=/z85bzQ9p/PlvAxiYdRtT9C2F81OUxYQBXrefeZLrwMc19NCeJyPiFmG6aF2gsHb/ghPpL PNaU2SzwYXgxyICw== 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 237771390A; Fri, 29 Sep 2023 14:41:44 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 6oELOCjiFmXwIgAAMHmgww (envelope-from ); Fri, 29 Sep 2023 14:41:44 +0000 From: Fabiano Rosas To: Elena Ufimtseva Cc: qemu-devel@nongnu.org, Juan Quintela , Peter Xu , Leonardo Bras Subject: Re: [RFC PATCH 1/3] migration/multifd: Move channels_ready semaphore In-Reply-To: References: <20230922145319.27380-1-farosas@suse.de> <20230922145319.27380-2-farosas@suse.de> Date: Fri, 29 Sep 2023 11:41:42 -0300 Message-ID: <8734yx11p5.fsf@suse.de> MIME-Version: 1.0 Content-Type: text/plain 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Elena Ufimtseva writes: > On Fri, Sep 22, 2023 at 11:53:17AM -0300, Fabiano Rosas wrote: >> Commit d2026ee117 ("multifd: Fix the number of channels ready") moved >> the "post" of channels_ready to the start of the multifd_send_thread() >> loop and added a missing "wait" at multifd_send_sync_main(). While it >> does work, the placement of the wait goes against what the rest of the >> code does. >> >> The sequence at multifd_send_thread() is: >> >> qemu_sem_post(&multifd_send_state->channels_ready); >> qemu_sem_wait(&p->sem); >> >> if (flags & MULTIFD_FLAG_SYNC) { >> qemu_sem_post(&p->sem_sync); >> } >> >> Which means that the sending thread makes itself available >> (channels_ready) and waits for more work (sem). So the sequence in the >> migration thread should be to check if any channel is available >> (channels_ready), give it some work and set it off (sem): >> >> qemu_sem_wait(&multifd_send_state->channels_ready); >> >> qemu_sem_post(&p->sem); >> if (flags & MULTIFD_FLAG_SYNC) { >> qemu_sem_wait(&p->sem_sync); >> } >> >> The reason there's no deadlock today is that the migration thread >> enqueues the SYNC packet right before the wait on channels_ready and >> we end up taking advantage of the out-of-order post to sem: >> >> ... >> qemu_sem_post(&p->sem); >> } >> for (i = 0; i < migrate_multifd_channels(); i++) { >> MultiFDSendParams *p = &multifd_send_state->params[i]; >> >> qemu_sem_wait(&multifd_send_state->channels_ready); >> trace_multifd_send_sync_main_wait(p->id); >> qemu_sem_wait(&p->sem_sync); >> ... >> >> Move the channels_ready wait before the sem post to keep the sequence >> consistent. Also fix the error path to post to channels_ready and >> sem_sync in the correct order. >> > > Thank you Fabiano, > > Your solution is more complete. I also had in mind getting rid of > sem_sync. > > With your second patch, this one could be merged with it? > >> Signed-off-by: Fabiano Rosas >> --- >> migration/multifd.c | 4 ++-- >> 1 file changed, 2 insertions(+), 2 deletions(-) >> >> diff --git a/migration/multifd.c b/migration/multifd.c >> index a7c7a947e3..d626740f2f 100644 >> --- a/migration/multifd.c >> +++ b/migration/multifd.c >> @@ -618,6 +618,7 @@ int multifd_send_sync_main(QEMUFile *f) >> >> trace_multifd_send_sync_main_signal(p->id); >> >> + qemu_sem_wait(&multifd_send_state->channels_ready); >> qemu_mutex_lock(&p->mutex); >> >> if (p->quit) { >> @@ -635,7 +636,6 @@ int multifd_send_sync_main(QEMUFile *f) >> for (i = 0; i < migrate_multifd_channels(); i++) { >> MultiFDSendParams *p = &multifd_send_state->params[i]; >> >> - qemu_sem_wait(&multifd_send_state->channels_ready); >> trace_multifd_send_sync_main_wait(p->id); >> qemu_sem_wait(&p->sem_sync); >> >> @@ -763,8 +763,8 @@ out: >> * who pay attention to me. >> */ >> if (ret != 0) { >> - qemu_sem_post(&p->sem_sync); >> qemu_sem_post(&multifd_send_state->channels_ready); >> + qemu_sem_post(&p->sem_sync); > > Can this thread in this error case be woken up again between > these two qemu_sem_posts? > I see in other places p->quit is set to true before it. > Or maybe it should one more patch to make these consistent > as well. That's a good point. There's clearly something going on here if we need a 'running', a 'quit' and a 'exiting' flag. The tls code uses quit as a signal in one direction while the regular multifd path uses it in another. I'll give it some more thought. Thanks