Skip to content
Snippets Groups Projects
  1. Mar 08, 2019
  2. Feb 12, 2019
    • Peter Xu's avatar
      iothread: fix iothread hang when stop too soon · 6c95363d
      Peter Xu authored
      
      Lukas reported an hard to reproduce QMP iothread hang on s390 that
      QEMU might hang at pthread_join() of the QMP monitor iothread before
      quitting:
      
        Thread 1
        #0  0x000003ffad10932c in pthread_join
        #1  0x0000000109e95750 in qemu_thread_join
            at /home/thuth/devel/qemu/util/qemu-thread-posix.c:570
        #2  0x0000000109c95a1c in iothread_stop
        #3  0x0000000109bb0874 in monitor_cleanup
        #4  0x0000000109b55042 in main
      
      While the iothread is still in the main loop:
      
        Thread 4
        #0  0x000003ffad0010e4 in ??
        #1  0x000003ffad553958 in g_main_context_iterate.isra.19
        #2  0x000003ffad553d90 in g_main_loop_run
        #3  0x0000000109c9585a in iothread_run
            at /home/thuth/devel/qemu/iothread.c:74
        #4  0x0000000109e94752 in qemu_thread_start
            at /home/thuth/devel/qemu/util/qemu-thread-posix.c:502
        #5  0x000003ffad10825a in start_thread
        #6  0x000003ffad00dcf2 in thread_start
      
      IMHO it's because there's a race between the main thread and iothread
      when stopping the thread in following sequence:
      
          main thread                       iothread
          ===========                       ==============
                                            aio_poll()
          iothread_get_g_main_context
            set iothread->worker_context
          iothread_stop
            schedule iothread_stop_bh
                                              execute iothread_stop_bh [1]
                                                set iothread->running=false
                                                (since main_loop==NULL so
                                                 skip to quit main loop.
                                                 Note: although main_loop is
                                                 NULL but worker_context is
                                                 not!)
                                            atomic_read(&iothread->worker_context) [2]
                                              create main_loop object
                                              g_main_loop_run() [3]
          pthread_join() [4]
      
      We can see that when execute iothread_stop_bh() at [1] it's possible
      that main_loop is still NULL because it's only created until the first
      check of the worker_context later at [2].  Then the iothread will hang
      in the main loop [3] and it'll starve the main thread too [4].
      
      Here the simple solution should be that we check again the "running"
      variable before check against worker_context.
      
      CC: Thomas Huth <thuth@redhat.com>
      CC: Dr. David Alan Gilbert <dgilbert@redhat.com>
      CC: Stefan Hajnoczi <stefanha@redhat.com>
      CC: Lukáš Doktor <ldoktor@redhat.com>
      CC: Markus Armbruster <armbru@redhat.com>
      CC: Eric Blake <eblake@redhat.com>
      CC: Paolo Bonzini <pbonzini@redhat.com>
      Reported-by: default avatarLukáš Doktor <ldoktor@redhat.com>
      Signed-off-by: default avatarPeter Xu <peterx@redhat.com>
      Tested-by: default avatarThomas Huth <thuth@redhat.com>
      Message-id: 20190129051432.22023-1-peterx@redhat.com
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      6c95363d
  3. Oct 12, 2018
  4. Apr 10, 2018
    • Peter Xu's avatar
      iothread: workaround glib bug which hangs qmp-test · 15544349
      Peter Xu authored
      
      Free the AIO context earlier than the GMainContext (if we have) to
      workaround a glib2 bug that GSource context pointer is not cleared even
      if the context has already been destroyed (while it should).
      
      The patch itself only changed the order to destroy the objects, no
      functional change at all. Without this workaround, we can encounter
      qmp-test hang with oob (and possibly any other use case when iothread is
      used with GMainContexts):
      
        #0  0x00007f35ffe45334 in __lll_lock_wait () from /lib64/libpthread.so.0
        #1  0x00007f35ffe405d8 in _L_lock_854 () from /lib64/libpthread.so.0
        #2  0x00007f35ffe404a7 in pthread_mutex_lock () from /lib64/libpthread.so.0
        #3  0x00007f35fc5b9c9d in g_source_unref_internal (source=0x24f0600, context=0x7f35f0000960, have_lock=0) at gmain.c:1685
        #4  0x0000000000aa6672 in aio_context_unref (ctx=0x24f0600) at /root/qemu/util/async.c:497
        #5  0x000000000065851c in iothread_instance_finalize (obj=0x24f0380) at /root/qemu/iothread.c:129
        #6  0x0000000000962d79 in object_deinit (obj=0x24f0380, type=0x242e960) at /root/qemu/qom/object.c:462
        #7  0x0000000000962e0d in object_finalize (data=0x24f0380) at /root/qemu/qom/object.c:476
        #8  0x0000000000964146 in object_unref (obj=0x24f0380) at /root/qemu/qom/object.c:924
        #9  0x0000000000965880 in object_finalize_child_property (obj=0x24ec640, name=0x24efca0 "mon_iothread", opaque=0x24f0380) at /root/qemu/qom/object.c:1436
        #10 0x0000000000962c33 in object_property_del_child (obj=0x24ec640, child=0x24f0380, errp=0x0) at /root/qemu/qom/object.c:436
        #11 0x0000000000962d26 in object_unparent (obj=0x24f0380) at /root/qemu/qom/object.c:455
        #12 0x0000000000658f00 in iothread_destroy (iothread=0x24f0380) at /root/qemu/iothread.c:365
        #13 0x00000000004c67a8 in monitor_cleanup () at /root/qemu/monitor.c:4663
        #14 0x0000000000669e27 in main (argc=16, argv=0x7ffc8b1ae2f8, envp=0x7ffc8b1ae380) at /root/qemu/vl.c:4749
      
      The glib2 bug is fixed in commit 26056558b ("gmain: allow
      g_source_get_context() on destroyed sources", 2012-07-30), so the first
      good version is glib2 2.33.10. But we still support building with
      glib as old as 2.28, so we need the workaround.
      
      Let's make sure we destroy the GSources first before its owner context
      until we drop support for glib older than 2.33.10.
      
      Signed-off-by: default avatarPeter Xu <peterx@redhat.com>
      Message-Id: <20180409083956.1780-1-peterx@redhat.com>
      Reviewed-by: default avatarEric Blake <eblake@redhat.com>
      Reviewed-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Signed-off-by: default avatarEric Blake <eblake@redhat.com>
      15544349
  5. Mar 26, 2018
  6. Mar 08, 2018
    • Stefan Hajnoczi's avatar
      vl: introduce vm_shutdown() · 4486e89c
      Stefan Hajnoczi authored
      
      Commit 00d09fdb ("vl: pause vcpus before
      stopping iothreads") and commit dce8921b
      ("iothread: Stop threads before main() quits") tried to work around the
      fact that emulation was still active during termination by stopping
      iothreads.  They suffer from race conditions:
      1. virtio_scsi_handle_cmd_vq() racing with iothread_stop_all() hits the
         virtio_scsi_ctx_check() assertion failure because the BDS AioContext
         has been modified by iothread_stop_all().
      2. Guest vq kick racing with main loop termination leaves a readable
         ioeventfd that is handled by the next aio_poll() when external
         clients are enabled again, resulting in unwanted emulation activity.
      
      This patch obsoletes those commits by fully disabling emulation activity
      when vcpus are stopped.
      
      Use the new vm_shutdown() function instead of pause_all_vcpus() so that
      vm change state handlers are invoked too.  Virtio devices will now stop
      their ioeventfds, preventing further emulation activity after vm_stop().
      
      Note that vm_stop(RUN_STATE_SHUTDOWN) cannot be used because it emits a
      QMP STOP event that may affect existing clients.
      
      It is no longer necessary to call replay_disable_events() directly since
      vm_shutdown() does so already.
      
      Drop iothread_stop_all() since it is no longer used.
      
      Cc: Fam Zheng <famz@redhat.com>
      Cc: Kevin Wolf <kwolf@redhat.com>
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Reviewed-by: default avatarFam Zheng <famz@redhat.com>
      Acked-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Message-id: 20180307144205.20619-5-stefanha@redhat.com
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      4486e89c
  7. Mar 02, 2018
    • Markus Armbruster's avatar
      qapi: Empty out qapi-schema.json · 112ed241
      Markus Armbruster authored
      
      The previous commit improved compile time by including less of the
      generated QAPI headers.  This is impossible for stuff defined directly
      in qapi-schema.json, because that ends up in headers that that pull in
      everything.
      
      Move everything but include directives from qapi-schema.json to new
      sub-module qapi/misc.json, then include just the "misc" shard where
      possible.
      
      It's possible everywhere, except:
      
      * monitor.c needs qmp-command.h to get qmp_init_marshal()
      
      * monitor.c, ui/vnc.c and the generated qapi-event-FOO.c need
        qapi-event.h to get enum QAPIEvent
      
      Perhaps we'll get rid of those some other day.
      
      Adding a type to qapi/migration.json now recompiles some 120 instead
      of 2300 out of 5100 objects.
      
      Signed-off-by: default avatarMarkus Armbruster <armbru@redhat.com>
      Message-Id: <20180211093607.27351-25-armbru@redhat.com>
      [eblake: rebase to master]
      Signed-off-by: default avatarEric Blake <eblake@redhat.com>
      112ed241
  8. Feb 09, 2018
  9. Dec 19, 2017
    • Stefan Hajnoczi's avatar
      iothread: fix iothread_stop() race condition · 2362a28e
      Stefan Hajnoczi authored
      
      There is a small chance that iothread_stop() hangs as follows:
      
        Thread 3 (Thread 0x7f63eba5f700 (LWP 16105)):
        #0  0x00007f64012c09b6 in ppoll () at /lib64/libc.so.6
        #1  0x000055959992eac9 in ppoll (__ss=0x0, __timeout=0x0, __nfds=<optimized out>, __fds=<optimized out>) at /usr/include/bits/poll2.h:77
        #2  0x000055959992eac9 in qemu_poll_ns (fds=<optimized out>, nfds=<optimized out>, timeout=<optimized out>) at util/qemu-timer.c:322
        #3  0x0000559599930711 in aio_poll (ctx=0x55959bdb83c0, blocking=blocking@entry=true) at util/aio-posix.c:629
        #4  0x00005595996806fe in iothread_run (opaque=0x55959bd78400) at iothread.c:59
        #5  0x00007f640159f609 in start_thread () at /lib64/libpthread.so.0
        #6  0x00007f64012cce6f in clone () at /lib64/libc.so.6
      
        Thread 1 (Thread 0x7f640b45b280 (LWP 16103)):
        #0  0x00007f64015a0b6d in pthread_join () at /lib64/libpthread.so.0
        #1  0x00005595999332ef in qemu_thread_join (thread=<optimized out>) at util/qemu-thread-posix.c:547
        #2  0x00005595996808ae in iothread_stop (iothread=<optimized out>) at iothread.c:91
        #3  0x000055959968094d in iothread_stop_iter (object=<optimized out>, opaque=<optimized out>) at iothread.c:102
        #4  0x0000559599857d97 in do_object_child_foreach (obj=obj@entry=0x55959bdb8100, fn=fn@entry=0x559599680930 <iothread_stop_iter>, opaque=opaque@entry=0x0, recurse=recurse@entry=false) at qom/object.c:852
        #5  0x0000559599859477 in object_child_foreach (obj=obj@entry=0x55959bdb8100, fn=fn@entry=0x559599680930 <iothread_stop_iter>, opaque=opaque@entry=0x0) at qom/object.c:867
        #6  0x0000559599680a6e in iothread_stop_all () at iothread.c:341
        #7  0x000055959955b1d5 in main (argc=<optimized out>, argv=<optimized out>, envp=<optimized out>) at vl.c:4913
      
      The relevant code from iothread_run() is:
      
        while (!atomic_read(&iothread->stopping)) {
            aio_poll(iothread->ctx, true);
      
      and iothread_stop():
      
        iothread->stopping = true;
        aio_notify(iothread->ctx);
        ...
        qemu_thread_join(&iothread->thread);
      
      The following scenario can occur:
      
      1. IOThread:
        while (!atomic_read(&iothread->stopping)) -> stopping=false
      
      2. Main loop:
        iothread->stopping = true;
        aio_notify(iothread->ctx);
      
      3. IOThread:
        aio_poll(iothread->ctx, true); -> hang
      
      The bug is explained by the AioContext->notify_me doc comments:
      
        "If this field is 0, everything (file descriptors, bottom halves,
        timers) will be re-evaluated before the next blocking poll(), thus the
        event_notifier_set call can be skipped."
      
      The problem is that "everything" does not include checking
      iothread->stopping.  This means iothread_run() will block in aio_poll()
      if aio_notify() was called just before aio_poll().
      
      This patch fixes the hang by replacing aio_notify() with
      aio_bh_schedule_oneshot().  This makes aio_poll() or g_main_loop_run()
      to return.
      
      Implementing this properly required a new bool running flag.  The new
      flag prevents races that are tricky if we try to use iothread->stopping.
      Now iothread->stopping is purely for iothread_stop() and
      iothread->running is purely for the iothread_run() thread.
      
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Reviewed-by: default avatarEric Blake <eblake@redhat.com>
      Message-id: 20171207201320.19284-6-stefanha@redhat.com
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      2362a28e
    • Stefan Hajnoczi's avatar
      iothread: add iothread_by_id() API · fbcc6923
      Stefan Hajnoczi authored
      
      Encapsulate IOThread QOM object lookup so that callers don't need to
      know how and where IOThread objects live.
      
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Reviewed-by: default avatarKevin Wolf <kwolf@redhat.com>
      Reviewed-by: default avatarEric Blake <eblake@redhat.com>
      Message-id: 20171206144550.22295-8-stefanha@redhat.com
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      fbcc6923
  10. Oct 03, 2017
  11. Sep 29, 2017
  12. Sep 08, 2017
  13. Feb 21, 2017
  14. Feb 03, 2017
    • Stefan Hajnoczi's avatar
      iothread: enable AioContext polling by default · cdd7abfd
      Stefan Hajnoczi authored
      
      IOThread AioContexts are likely to consist only of event sources like
      virtqueue ioeventfds and LinuxAIO completion eventfds that are pollable
      from userspace (without system calls).
      
      We recently merged the AioContext polling feature but didn't enable it
      by default yet.  I have gone back over the performance data on the
      mailing list and picked a default polling value that gave good results.
      
      Let's enable AioContext polling by default so users don't have another
      switch they need to set manually.  If performance regressions are found
      we can still disable this for the QEMU 2.9 release.
      
      Cc: Paolo Bonzini <pbonzini@redhat.com>
      Cc: Christian Borntraeger <borntraeger@de.ibm.com>
      Cc: Karl Rister <krister@redhat.com>
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Message-id: 20170126170119.27876-1-stefanha@redhat.com
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      cdd7abfd
  15. Jan 03, 2017
  16. Oct 28, 2016
  17. Sep 28, 2016
  18. Sep 13, 2016
    • Fam Zheng's avatar
      iothread: Stop threads before main() quits · dce8921b
      Fam Zheng authored
      
      Right after main_loop ends, we release various things but keep iothread
      alive. The latter is not prepared to the sudden change of resources.
      
      Specifically, after bdrv_close_all(), virtio-scsi dataplane get a
      surprise at the empty BlockBackend:
      
      (gdb) bt
          at /usr/src/debug/qemu-2.6.0/hw/scsi/virtio-scsi.c:543
          at /usr/src/debug/qemu-2.6.0/hw/scsi/virtio-scsi.c:577
      
      It is because the d->conf.blk->root is set to NULL, then
      blk_get_aio_context() returns qemu_aio_context, whereas s->ctx is still
      pointing to the iothread:
      
          hw/scsi/virtio-scsi.c:543:
      
          if (s->dataplane_started) {
              assert(blk_get_aio_context(d->conf.blk) == s->ctx);
          }
      
      To fix this, let's stop iothreads before doing bdrv_close_all().
      
      Cc: qemu-stable@nongnu.org
      Signed-off-by: default avatarFam Zheng <famz@redhat.com>
      Reviewed-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Message-id: 1473326931-9699-1-git-send-email-famz@redhat.com
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      dce8921b
  19. Feb 04, 2016
    • Peter Maydell's avatar
      all: Clean up includes · d38ea87a
      Peter Maydell authored
      
      Clean up includes so that osdep.h is included first and headers
      which it implies are not included manually.
      
      This commit was created with scripts/clean-includes.
      
      Signed-off-by: default avatarPeter Maydell <peter.maydell@linaro.org>
      Message-id: 1454089805-5470-16-git-send-email-peter.maydell@linaro.org
      d38ea87a
  20. Dec 03, 2015
  21. Jul 24, 2015
  22. Jun 19, 2015
  23. Jun 12, 2015
  24. May 08, 2015
  25. Apr 28, 2015
  26. Sep 22, 2014
    • Chrysostomos Nanakos's avatar
      async: aio_context_new(): Handle event_notifier_init failure · 2f78e491
      Chrysostomos Nanakos authored
      
      On a system with a low limit of open files the initialization
      of the event notifier could fail and QEMU exits without printing any
      error information to the user.
      
      The problem can be easily reproduced by enforcing a low limit of open
      files and start QEMU with enough I/O threads to hit this limit.
      
      The same problem raises, without the creation of I/O threads, while
      QEMU initializes the main event loop by enforcing an even lower limit of
      open files.
      
      This commit adds an error message on failure:
      
       # qemu [...] -object iothread,id=iothread0 -object iothread,id=iothread1
       qemu: Failed to initialize event notifier: Too many open files in system
      
      Signed-off-by: default avatarChrysostomos Nanakos <cnanakos@grnet.gr>
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      2f78e491
  27. Jul 14, 2014
    • Paolo Bonzini's avatar
      AioContext: do not rely on aio_poll(ctx, true) result to end a loop · acfb23ad
      Paolo Bonzini authored
      
      Currently, whenever aio_poll(ctx, true) has completed all pending
      work it returns true *and* the next call to aio_poll(ctx, true)
      will not block.
      
      This invariant has its roots in qemu_aio_flush()'s implementation
      as "while (qemu_aio_wait()) {}".  However, qemu_aio_flush() does
      not exist anymore and bdrv_drain_all() is implemented differently;
      and this invariant is complicated to maintain and subtly different
      from the return value of GMainLoop's g_main_context_iteration.
      
      All calls to aio_poll(ctx, true) except one are guarded by a
      while() loop checking for a request to be incomplete, or a
      BlockDriverState to be idle.  The one remaining call (in
      iothread.c) uses this to delay the aio_context_release/acquire
      pair until the AioContext is quiescent, however:
      
      - we can do the same just by using non-blocking aio_poll,
        similar to how vl.c invokes main_loop_wait
      
      - it is buggy, because it does not ensure that the AioContext
        is released between an aio_notify and the next time the
        iothread goes to sleep.  This leads to hangs when stopping
        the dataplane thread.
      
      In the end, these semantics are a bad match for the current
      users of AioContext.  So modify that one exception in iothread.c,
      which also fixes the hangs, as well as the testcase so that
      it use the same idiom as the actual QEMU code.
      
      Reported-by: default avatarChristian Borntraeger <borntraeger@de.ibm.com>
      Tested-by: default avatarChristian Borntraeger <borntraeger@de.ibm.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
      acfb23ad
  28. Apr 04, 2014
  29. Mar 13, 2014
Loading