Hi, Stefan, Maciej, Sorry to be slow on responding.
On Tue, Sep 03, 2024 at 03:04:54PM -0400, Stefan Hajnoczi wrote: > On Tue, 3 Sept 2024 at 12:54, Maciej S. Szmigiero > <m...@maciej.szmigiero.name> wrote: > > > > On 3.09.2024 15:55, Stefan Hajnoczi wrote: > > > On Tue, 27 Aug 2024 at 13:58, Maciej S. Szmigiero > > > <m...@maciej.szmigiero.name> wrote: > > >> > > >> From: "Maciej S. Szmigiero" <maciej.szmigi...@oracle.com> > > >> > > >> Migration code wants to manage device data sending threads in one place. > > >> > > >> QEMU has an existing thread pool implementation, however it was limited > > >> to queuing AIO operations only and essentially had a 1:1 mapping between > > >> the current AioContext and the ThreadPool in use. > > >> > > >> Implement what is necessary to queue generic (non-AIO) work on a > > >> ThreadPool > > >> too. > > >> > > >> This brings a few new operations on a pool: > > >> * thread_pool_set_minmax_threads() explicitly sets the minimum and > > >> maximum > > >> thread count in the pool. > > >> > > >> * thread_pool_join() operation waits until all the submitted work > > >> requests > > >> have finished. > > >> > > >> * thread_pool_poll() lets the new thread and / or thread completion > > >> bottom > > >> halves run (if they are indeed scheduled to be run). > > >> It is useful for thread pool users that need to launch or terminate new > > >> threads without returning to the QEMU main loop. > > > > > > Did you consider glib's GThreadPool? > > > https://docs.gtk.org/glib/struct.ThreadPool.html > > > > > > QEMU's thread pool is integrated into the QEMU event loop. If your > > > goal is to bypass the QEMU event loop, then you may as well use the > > > glib API instead. > > > > > > thread_pool_join() and thread_pool_poll() will lead to code that > > > blocks the event loop. QEMU's aio_poll() and nested event loops in > > > general are a source of hangs and re-entrancy bugs. I would prefer not > > > introducing these issues in the QEMU ThreadPool API. > > > > > > > Unfortunately, the problem with the migration code is that it is > > synchronous - it does not return to the main event loop until the > > migration is done. > > > > So the only way to handle things that need working event loop is to > > pump it manually from inside the migration code. > > > > The reason why I used the QEMU thread pool in the first place in this > > patch set version is because Peter asked me to do so during the review > > of its previous iteration [1]. > > > > Peter also asked me previously to move to QEMU synchronization > > primitives from using the Glib ones in the early version of this > > patch set [2]. > > > > I personally would rather use something common to many applications, > > well tested and with more pairs of eyes looking at it rather to > > re-invent things in QEMU. > > > > Looking at GThreadPool it seems that it lacks ability to wait until > > all queued work have finished, so this would need to be open-coded > > in the migration code. > > > > @Peter, what's your opinion on using Glib's thread pool instead of > > QEMU's one, considering the above things? > > I'll add a bit more about my thinking: > > Using QEMU's event-driven model is usually preferred because it makes > integrating with the rest of QEMU easy and avoids having lots of > single-purpose threads that are hard to observe/manage (e.g. through > the QMP monitor). > > When there is a genuine need to spawn a thread and write synchronous > code (e.g. a blocking ioctl(2) call or something CPU-intensive), then Right, AFAIU this is the current use case for VFIO, and anything beyond in migration context, where we want to use genuine threads with no need to integrate with the main even loop. Currently the VFIO workfn should read() the VFIO fd in a blocked way, then dump them to multifd threads (further dump to migration channels), during which it can wait on a semaphore. > it's okay to do that. Use QEMUBH, EventNotifier, or other QEMU APIs to > synchronize between event loop threads and special-purpose synchronous > threads. > > I haven't looked at the patch series enough to have an opinion about > whether this use case needs a special-purpose thread or not. I am > assuming it really needs to be a special-purpose thread. Peter and you > could discuss that further if you want. > > I agree with Peter's request to use QEMU's synchronization primitives. > They do not depend on the event loop so they can be used outside the > event loop. > > The issue I'm raising with this patch is that adding new join()/poll() > APIs that shouldn't be called from the event loop is bug-prone. It > will make the QEMU ThreadPool code harder to understand and maintain > because now there are two different contexts where different subsets > of this API can be used and mixing them leads to problems. To me the > non-event loop case is beyond the scope of QEMU's ThreadPool. I have > CCed Paolo, who wrote the thread pool in its current form in case he > wants to participate in the discussion. > > Using glib's ThreadPool solves the issue while still reusing an > existing thread pool implementation. Waiting for all work to complete > can be done using QemuSemaphore. Right. It's a pity that g_thread_pool_unprocessed() only monitors unqueuing of tasks, and looks like there's no g_thread_pool_flush(). Indeed the current thread poll is very aio-centric, and if we worry about misuse of the APIs we can switch to glib's threadpool. Sorry Maciej, looks like I routed you to a direction that I didn't see the side effects.. I think the fundamental request from my side (on behalf of migration) is we should avoid a specific vmstate handler managing threads on its own. E.g., any future devices (vdpa, vcpu, etc.) that may also be able to offload save() processes concurrently to threads (just like what VFIO can already do right now) should share the same pool of threads. As long as that can be achieved I am ok. Thanks, -- Peter Xu