qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC PATCH 1/3] migration/multifd: Move channels_ready semaphore


From: Peter Xu
Subject: Re: [RFC PATCH 1/3] migration/multifd: Move channels_ready semaphore
Date: Tue, 10 Oct 2023 17:59:26 -0400

On Tue, Oct 10, 2023 at 06:43:05PM -0300, Fabiano Rosas wrote:
> Peter Xu <peterx@redhat.com> 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);
> >>     <work>
> >>     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);
> >
> > Here it means we have at least 1 free send thread, then...
> >
> >>     <enqueue work>
> >>     qemu_sem_post(&p->sem);
> >
> > ... here we enqueue some work to the current thread (pointed by "i"), no
> > matter it's free or not, as "i" may not always point to the free thread.
> >
> 
> Yes. Which means channels_ready is currently useless. Since I posted
> this I realized that and have been working on a series to remove it
> completely.
> 
> ... I'm not opposed to "fixing" whatever needs to be fixed here as well, but
> I think removing it makes sense. I'll try to focus on that and post a v2
> here.

Happy to read it.

> 
> >>     if (flags & MULTIFD_FLAG_SYNC) {
> >>         qemu_sem_wait(&p->sem_sync);
> >>     }
> >
> > So I must confess I never fully digest how these sem/mutex/.. worked in
> > multifd, since the 1st day it's introduced.. so please take below comment
> > with a grain of salt..
> 
> We definitely need to clarify some things in the multifd
> design. Specially if we're going to use it as the main migration
> infrastructure moving forward.

Exactly.

> 
> I think what we lack is a design direction. I'm not really interested in
> how things work currently, but in how they *should* work based on the
> design.

Unfortunately we can't ignore how old code works; normally old code has its
reason to work like that. So the best way to go is trying to figure out
exactly how it works with the author, unless reaching the consensus it was
a design mistake after that conversation.

The luckiest thing here is we have the author around (Juan).  Let's discuss
thoroughly with him to make sure nothing is overlooked.

> 
> I'm confused about:
> 
> 1) why channels_ready exists? Were we trying to do some lockstep
> movement of: populate MultiFDPages -> release the sender thread -> move
> to next channel -> wait for it to become ready -> repeat. If so, that
> semaphore should be per-channel I think.
> 
> (my future proposal will be to remove the channels_ready semaphore)
> 
> 2) why do we need sem_sync? The SYNC flag makes sense, but why the
> source needs to sync with itself when syncing with dst?
> 
> (my proposal in this series is to rename sem_sync to sem_done and use it
> to track sending completion)
> 
> 3) why do we need to take the params lock? Shouldn't the semaphores
> already ensure that only one of the main thread and the sender thread
> will touch the params? The comment in multifd_send_pages says that we
> don't take locks for the pages structure, but that seems pointeless to
> me since we still lock the params structure.

Heh, so I'm not the only one who is confused with all these. :) You
reminded me of the days when I was reviewing the initial versions of
multifd, since when I failed to understand the code..

It's great to start discussing this again.  I'd say go ahead and propose
your patches; I'll read them.

> 
> > It seems to me that the current design allows >1 pending_job for a thread.
> > Here the current code didn't do "wait(channels_ready)" because it doesn't
> > need to - it simply always queue an MULTIFD_FLAG_SYNC pending job over the
> > thread, and wait for it to run.
> >
> > From that POV I think I can understand why "wait(channels_ready)" is not
> > needed here.  But then I'm confused because we don't have a real QUEUE to
> > put those requests; we simply apply this:
> >
> > multifd_send_sync_main():
> >         p->flags |= MULTIFD_FLAG_SYNC;
> >
> > Even if this send thread can be busy handling a batch of pages and
> > accessing p->flags.  I think it can actually race with the send thread
> > reading the flag at the exact same time:
> >
> > multifd_send_thread():
> >             multifd_send_fill_packet(p);
> >             flags = p->flags;  <-------------- here
> 
> It doesn't race in reading due to the p->mutex lock. But it looks like
> it could miss a newly set flag when it unlocks to start sending
> (qio_channel_write*).

Right. See my follow up email, I think the "race" is that it's
unpredictable on what will happen, and I think it's possible src qemu
generates an useless packet.  Not a real race.

If I'm doing the multifd thing, I'll avoid any form of modifying p->flags
if a job was assigned already to be clear.

Also please feel free to have a look at the SYNC issue I raised in that
same follow up email.  I hope I'm wrong somewhere.

-- 
Peter Xu




reply via email to

[Prev in Thread] Current Thread [Next in Thread]