Skip to content
Snippets Groups Projects
  1. Mar 07, 2023
  2. Feb 17, 2023
  3. Oct 27, 2022
    • David Hildenbrand's avatar
      util: Introduce qemu_thread_set_affinity() and qemu_thread_get_affinity() · 7730f32c
      David Hildenbrand authored
      
      Usually, we let upper layers handle CPU pinning, because
      pthread_setaffinity_np() (-> sched_setaffinity()) is blocked via
      seccomp when starting QEMU with
          -sandbox enable=on,resourcecontrol=deny
      
      However, we want to configure and observe the CPU affinity of threads
      from QEMU directly in some cases when the sandbox option is either not
      enabled or not active yet.
      
      So let's add a way to configure CPU pinning via
      qemu_thread_set_affinity() and obtain CPU affinity via
      qemu_thread_get_affinity() and implement them under POSIX using
      pthread_setaffinity_np() + pthread_getaffinity_np().
      
      Implementation under Windows is possible using SetProcessAffinityMask()
      + GetProcessAffinityMask(), however, that is left as future work.
      
      Reviewed-by: default avatarMichal Privoznik <mprivozn@redhat.com>
      Message-Id: <20221014134720.168738-3-david@redhat.com>
      Signed-off-by: default avatarDavid Hildenbrand <david@redhat.com>
      7730f32c
  4. Apr 06, 2022
  5. Oct 14, 2021
  6. Sep 13, 2021
  7. Jun 16, 2021
  8. Sep 23, 2020
    • Stefan Hajnoczi's avatar
      qemu/atomic.h: rename atomic_ to qatomic_ · d73415a3
      Stefan Hajnoczi authored
      
      clang's C11 atomic_fetch_*() functions only take a C11 atomic type
      pointer argument. QEMU uses direct types (int, etc) and this causes a
      compiler error when a QEMU code calls these functions in a source file
      that also included <stdatomic.h> via a system header file:
      
        $ CC=clang CXX=clang++ ./configure ... && make
        ../util/async.c:79:17: error: address argument to atomic operation must be a pointer to _Atomic type ('unsigned int *' invalid)
      
      Avoid using atomic_*() names in QEMU's atomic.h since that namespace is
      used by <stdatomic.h>. Prefix QEMU's APIs with 'q' so that atomic.h
      and <stdatomic.h> can co-exist. I checked /usr/include on my machine and
      searched GitHub for existing "qatomic_" users but there seem to be none.
      
      This patch was generated using:
      
        $ git grep -h -o '\<atomic\(64\)\?_[a-z0-9_]\+' include/qemu/atomic.h | \
          sort -u >/tmp/changed_identifiers
        $ for identifier in $(</tmp/changed_identifiers); do
              sed -i "s%\<$identifier\>%q$identifier%g" \
                  $(git grep -I -l "\<$identifier\>")
          done
      
      I manually fixed line-wrap issues and misaligned rST tables.
      
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Reviewed-by: default avatarPhilippe Mathieu-Daudé <philmd@redhat.com>
      Acked-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Message-Id: <20200923105646.47864-1-stefanha@redhat.com>
      d73415a3
  9. Jun 16, 2020
  10. Sep 16, 2019
  11. Jan 08, 2019
  12. Nov 06, 2018
    • Peter Maydell's avatar
      util/qemu-thread-posix: Fix qemu_thread_atexit* for OSX · a458774a
      Peter Maydell authored
      
      Our current implementation of qemu_thread_atexit* is broken on OSX.
      This is because it works by cerating a piece of thread-specific
      data with pthread_key_create() and using the destructor function
      for that data to run the notifier function passed to it by
      the caller of qemu_thread_atexit_add(). The expected use case
      is that the caller uses a __thread variable as the notifier,
      and uses the callback to clean up information that it is
      keeping per-thread in __thread variables.
      
      Unfortunately, on OSX this does not work, because on OSX
      a __thread variable may be destroyed (freed) before the
      pthread_key_create() destructor runs. (POSIX imposes no
      ordering constraint here; the OSX implementation happens
      to implement __thread variables in terms of pthread_key_create((),
      whereas Linux uses different mechanisms that mean the __thread
      variables will still be present when the pthread_key_create()
      destructor is run.)
      
      Fix this by switching to a scheme similar to the one qemu-thread-win32
      uses for qemu_thread_atexit: keep the thread's notifiers on a
      __thread variable, and run the notifiers on calls to
      qemu_thread_exit() and on return from the start routine passed
      to qemu_thread_start(). We do this with the pthread_cleanup_push()
      API.
      
      We take advantage of the qemu_thread_atexit_add() API
      permission not to run thread notifiers on process exit to
      avoid having to special case the main thread.
      
      Suggested-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Signed-off-by: default avatarPeter Maydell <peter.maydell@linaro.org>
      Reviewed-by: default avatarEric Blake <eblake@redhat.com>
      Message-Id: <20181105135538.28025-3-peter.maydell@linaro.org>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      a458774a
  13. Jun 28, 2018
    • Peter Xu's avatar
      qemu-thread: introduce qemu-thread-common.h · f1aff7aa
      Peter Xu authored
      
      Introduce some hooks for the shared part of qemu thread between POSIX
      and Windows implementations.  Note that in qemu_mutex_unlock_impl() we
      moved the call before unlock operation which should make more sense.
      And we don't need qemu_mutex_post_unlock() hook.
      
      Put all these shared hooks into the header files.  It should be internal
      to qemu-thread but not for qemu-thread users, hence put into util/
      directory.
      
      Reviewed-by: default avatarEmilio G. Cota <cota@braap.org>
      Signed-off-by: default avatarPeter Xu <peterx@redhat.com>
      Message-Id: <20180425025459.5258-3-peterx@redhat.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      f1aff7aa
  14. May 08, 2018
  15. Jan 16, 2018
  16. Dec 20, 2017
    • linzhecheng's avatar
      qemu-thread: fix races on threads that exit very quickly · 68a93982
      linzhecheng authored
      If we create a thread with QEMU_THREAD_DETACHED mode, QEMU may get a segfault with low probability.
      
      The backtrace is:
         #0  0x00007f46c60291d7 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
         #1  0x00007f46c602a8c8 in __GI_abort () at abort.c:90
         #2  0x00000000008543c9 in PAT_abort ()
         #3  0x000000000085140d in patchIllInsHandler ()
         #4  <signal handler called>
         #5  pthread_detach (th=139933037614848) at pthread_detach.c:50
         #6  0x0000000000829759 in qemu_thread_create (thread=thread@entry=0x7ffdaa8205e0, name=name@entry=0x94d94a "io-task-worker", start_routine=start_routine@entry=0x7eb9a0 <qio_task_thread_worker>,
             arg=arg@entry=0x3f5cf70, mode=mode@entry=1) at util/qemu_thread_posix.c:512
         #7  0x00000000007ebc96 in qio_task_run_in_thread (task=0x31db2c0, worker=worker@entry=0x7e7e40 <qio_channel_socket_connect_worker>, opaque=0xcd23380, destroy=0x7f1180 <qapi_free_SocketAddress>)
             at io/task.c:141
         #8  0x00000000007e7f33 in qio_channel_socket_connect_async (ioc=ioc@entry=0x626c0b0, addr=<optimized out>, callback=callback@entry=0x55e080 <qemu_chr_socket_connected>, opaque=opaque@entry=0x42862c0,
             destroy=destroy@entry=0x0) at io/channel_socket.c:194
         #9  0x000000000055bdd1 in socket_reconnect_timeout (opaque=0x42862c0) at qemu_char.c:4744
         #10 0x00007f46c72483b3 in g_timeout_dispatch () from /usr/lib64/libglib-2.0.so.0
         #11 0x00007f46c724799a in g_main_context_dispatch () from /usr/lib64/libglib-2.0.so.0
         #12 0x000000000076c646 in glib_pollfds_poll () at main_loop.c:228
         #13 0x000000000076c6eb in os_host_main_loop_wait (timeout=348000000) at main_loop.c:273
         #14 0x000000000076c815 in main_loop_wait (nonblocking=nonblocking@entry=0) at main_loop.c:521
         #15 0x000000000056a511 in main_loop () at vl.c:2076
         #16 0x0000000000420705 in main (argc=<optimized out>, argv=<optimized out>, envp=<optimized out>) at vl.c:4940
      
      The cause of this problem is a glibc bug; for more information, see
      https://sourceware.org/bugzilla/show_bug.cgi?id=19951
      
      .
      The solution for this bug is to use pthread_attr_setdetachstate.
      
      There is a similar issue with pthread_setname_np, which is moved
      from creating thread to created thread.
      
      Signed-off-by: default avatarlinzhecheng <linzhecheng@huawei.com>
      Message-Id: <20171128044656.10592-1-linzhecheng@huawei.com>
      Reviewed-by: default avatarFam Zheng <famz@redhat.com>
      [Simplify the code by removing qemu_thread_set_name, and free the arguments
       before invoking the start routine. - Paolo]
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      68a93982
  17. Sep 26, 2017
  18. Jul 04, 2017
  19. May 05, 2017
    • Jose Ricardo Ziviani's avatar
      trace: add qemu mutex lock and unlock trace events · 31f5a726
      Jose Ricardo Ziviani authored
      
      These trace events were very useful to help me to understand and find a
      reordering issue in vfio, for example:
      
      qemu_mutex_lock locked mutex 0x10905ad8
        vfio_region_write  (0001:03:00.0:region1+0xc0, 0x2020c, 4)
      qemu_mutex_unlock unlocked mutex 0x10905ad8
      qemu_mutex_lock locked mutex 0x10905ad8
        vfio_region_write  (0001:03:00.0:region1+0xc4, 0xa0000, 4)
      qemu_mutex_unlock unlocked mutex 0x10905ad8
      
      that also helped me to see the desired result after the fix:
      
      qemu_mutex_lock locked mutex 0x10905ad8
        vfio_region_write  (0001:03:00.0:region1+0xc0, 0x2000c, 4)
        vfio_region_write  (0001:03:00.0:region1+0xc4, 0xb0000, 4)
      qemu_mutex_unlock unlocked mutex 0x10905ad8
      
      So it could be a good idea to have these traces implemented. It's worth
      mentioning that they should be surgically enabled during the debugging,
      otherwise it can flood the trace logs with lock/unlock messages.
      
      How to use it:
      trace-event qemu_mutex_lock on|off
      trace-event qemu_mutex_unlock on|off
      or
      trace-event qemu_mutex* on|off
      
      Signed-off-by: default avatarJose Ricardo Ziviani <joserz@linux.vnet.ibm.com>
      Message-Id: <1493054398-26013-1-git-send-email-joserz@linux.vnet.ibm.com>
      Reviewed-by: default avatarFam Zheng <famz@redhat.com>
      [Also handle trylock, cond_wait and win32; trace "unlocked" while still
       in the critical section, so that "unlocked" always comes before the
       next "locked" tracepoint. - Paolo]
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      31f5a726
  20. Jan 16, 2017
    • Xinhua Cao's avatar
      qemu-thread: fix qemu_thread_set_name() race in qemu_thread_create() · 2f75bd73
      Xinhua Cao authored
      
      QEMU will crash with the follow backtrace if the new created thread exited before
      we call qemu_thread_set_name() for it.
      
        (gdb) bt
        #0 0x00007f9a68b095d7 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
        #1 0x00007f9a68b0acc8 in __GI_abort () at abort.c:90
        #2 0x00007f9a69cda389 in PAT_abort () from /usr/lib64/libuvpuserhotfix.so
        #3 0x00007f9a69cdda0d in patchIllInsHandler () from /usr/lib64/libuvpuserhotfix.so
        #4 <signal handler called>
        #5 pthread_setname_np (th=140298470549248, name=name@entry=0x8cc74a "io-task-worker") at ../nptl/sysdeps/unix/sysv/linux/pthread_setname.c:49
        #6 0x00000000007f5f20 in qemu_thread_set_name (thread=thread@entry=0x7ffd2ac09680, name=name@entry=0x8cc74a "io-task-worker") at util/qemu_thread_posix.c:459
        #7 0x00000000007f679e in qemu_thread_create (thread=thread@entry=0x7ffd2ac09680, name=name@entry=0x8cc74a "io-task-worker",start_routine=start_routine@entry=0x7c1300 <qio_task_thread_worker>, arg=arg@entry=0x7f99b8001720, mode=mode@entry=1) at util/qemu_thread_posix.c:498
        #8 0x00000000007c15b6 in qio_task_run_in_thread (task=task@entry=0x7f99b80033d0, worker=worker@entry=0x7bd920 <qio_channel_socket_connect_worker>, opaque=0x7f99b8003370, destroy=0x7c6220 <qapi_free_SocketAddress>) at io/task.c:133
        #9 0x00000000007bda04 in qio_channel_socket_connect_async (ioc=0x7f99b80014c0, addr=0x37235d0, callback=callback@entry=0x54ad00 <qemu_chr_socket_connected>, opaque=opaque@entry=0x38118b0, destroy=destroy@entry=0x0) at io/channel_socket.c:191
        #10 0x00000000005487f6 in socket_reconnect_timeout (opaque=0x38118b0) at qemu_char.c:4402
        #11 0x00007f9a6a1533b3 in g_timeout_dispatch () from /usr/lib64/libglib-2.0.so.0
        #12 0x00007f9a6a15299a in g_main_context_dispatch () from /usr/lib64/libglib-2.0.so.0
        #13 0x0000000000747386 in glib_pollfds_poll () at main_loop.c:227
        #14 0x0000000000747424 in os_host_main_loop_wait (timeout=404000000) at main_loop.c:272
        #15 0x0000000000747575 in main_loop_wait (nonblocking=nonblocking@entry=0) at main_loop.c:520
        #16 0x0000000000557d31 in main_loop () at vl.c:2170
        #17 0x000000000041c8b7 in main (argc=<optimized out>, argv=<optimized out>, envp=<optimized out>) at vl.c:5083
      
      Let's detach the new thread after calling qemu_thread_set_name().
      
      Signed-off-by: default avatarCaoxinhua <caoxinhua@huawei.com>
      Signed-off-by: default avatarzhanghailiang <zhang.zhanghailiang@huawei.com>
      Message-Id: <1483493521-9604-1-git-send-email-zhang.zhanghailiang@huawei.com>
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      2f75bd73
    • Paolo Bonzini's avatar
      qemu-thread: optimize QemuLockCnt with futexes on Linux · fbcc3e50
      Paolo Bonzini authored
      
      This is complex, but I think it is reasonably documented in the source.
      
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Reviewed-by: default avatarFam Zheng <famz@redhat.com>
      Reviewed-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      Message-id: 20170112180800.21085-5-pbonzini@redhat.com
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      fbcc3e50
  21. Oct 28, 2016
  22. Oct 24, 2016
    • Paolo Bonzini's avatar
      qemu-thread: use acquire/release to clarify semantics of QemuEvent · 374293ca
      Paolo Bonzini authored
      
      Do not use the somewhat mysterious atomic_mb_read/atomic_mb_set,
      instead make sure that the operations on QemuEvent are annotated
      with the desired acquire and release semantics.
      
      In particular, qemu_event_set wakes up the waiting thread, so it must
      be a release from the POV of the waker (compare with qemu_mutex_unlock).
      And it actually needs a full barrier, because that's the only thing that
      provides something like a "load-release".
      
      Use smp_mb_acquire until we have atomic_load_acquire and
      atomic_store_release in atomic.h.
      
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      374293ca
  23. Feb 04, 2016
  24. Sep 11, 2015
  25. Sep 02, 2015
  26. Mar 10, 2015
    • Paolo Bonzini's avatar
      qemu-thread: do not use PTHREAD_MUTEX_ERRORCHECK · 24fa9049
      Paolo Bonzini authored
      
      PTHREAD_MUTEX_ERRORCHECK is completely broken with respect to fork.
      The way to safely do fork is to bring all threads to a quiescent
      state by acquiring locks (either in callers---as we do for the
      iothread mutex---or using pthread_atfork's prepare callbacks)
      and then release them in the child.
      
      The problem is that releasing error-checking locks in the child
      fails under glibc with EPERM, because the mutex stores a different
      owner tid than the duplicated thread in the child process.  We
      could make it work for locks acquired via pthread_atfork, by
      recreating the mutex in the child instead of unlocking it
      (we know that there are no other threads that could have taken
      the mutex; but when the lock is acquired in fork's caller
      that would not be possible.
      
      The simplest solution is just to forgo error checking.
      
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      24fa9049
  27. Feb 02, 2015
    • Paolo Bonzini's avatar
      qemu-thread: fix qemu_event without futexes · 158ef8cb
      Paolo Bonzini authored
      
      This had a possible deadlock that was visible with rcutorture.
      
          qemu_event_set                    qemu_event_wait
          ----------------------------------------------------------------
                                            cmpxchg reads FREE, writes BUSY
                                            futex_wait: pthread_mutex_lock
                                            futex_wait: value == BUSY
          xchg reads BUSY, writes SET
          futex_wake: pthread_cond_broadcast
                                            futex_wait: pthread_cond_wait
                                            <deadlock>
      
      The fix is simply to avoid condvar tricks and do the obvious locking
      around pthread_cond_broadcast:
      
          qemu_event_set        qemu_event_wait
          ----------------------------------------------------------------
                                            cmpxchg reads FREE, writes BUSY
                                            futex_wait: pthread_mutex_lock
                                            futex_wait: value == BUSY
          xchg reads BUSY, writes SET
          futex_wake: pthread_mutex_lock
          (blocks)
                                            futex_wait: pthread_cond_wait
          (mutex unlocked)
          futex_wake: pthread_cond_broadcast
          futex_wake: pthread_mutex_unlock
                                            futex_wait: pthread_mutex_unlock
      
      Cc: qemu-stable@nongnu.org
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      158ef8cb
  28. Jan 13, 2015
    • Paolo Bonzini's avatar
      qemu-thread: add per-thread atexit functions · ef57137f
      Paolo Bonzini authored
      
      Destructors are the main additional feature of pthread TLS compared
      to __thread.  If we were using C++ (hint, hint!) we could have used
      thread-local objects with a destructor.  Since we are not, instead,
      we add a simple Notifier-based API.
      
      Note that the notifier must be per-thread as well.  We can add a
      global list as well later, perhaps.
      
      The Win32 implementation has some complications because a) detached
      threads used not to have a QemuThreadData; b) the main thread does
      not go through win32_start_routine, so we have to use atexit too.
      
      Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
      Reviewed-by: default avatarFam Zheng <famz@redhat.com>
      Message-id: 1417518350-6167-3-git-send-email-pbonzini@redhat.com
      Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
      ef57137f
  29. Mar 27, 2014
Loading