qemu-e2k/util
linzhecheng 68a9398261 qemu-thread: fix races on threads that exit very quickly
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: linzhecheng <linzhecheng@huawei.com>
Message-Id: <20171128044656.10592-1-linzhecheng@huawei.com>
Reviewed-by: Fam 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: Paolo Bonzini <pbonzini@redhat.com>
2017-12-20 22:29:26 +01:00
..
Makefile.objs util: move qemu_real_host_page_size/mask to osdep.h 2017-10-10 09:45:00 -07:00
acl.c Drop Emacs local variables lists redundant with .dir-locals.el 2016-07-12 16:19:16 +02:00
aio-posix.c aio-posix: drop QEMU_AIO_POLL_MAX_NS env var 2017-11-06 11:04:38 +00:00
aio-win32.c util/aio-win32: Only select on what we are actually waiting for 2017-07-17 15:58:37 +01:00
aiocb.c block: move AioContext, QEMUTimer, main-loop to libqemuutil 2017-02-21 11:14:07 +00:00
async.c coroutine: abort if we try to schedule or enter a pending coroutine 2017-11-21 11:58:07 -05:00
base64.c include/qemu/osdep.h: Don't include qapi/error.h 2016-03-22 22:20:15 +01:00
bitmap.c bitmap: provide to_le/from_le helpers 2017-09-22 14:11:25 +02:00
bitops.c bitmap: remove BITOP_WORD() 2017-09-22 14:11:23 +02:00
buffer.c qemu-common: stop including qemu/host-utils.h from qemu-common.h 2016-05-19 16:42:28 +02:00
bufferiszero.c util: Introduce include/qemu/cpuid.h 2017-07-24 12:42:55 +01:00
cacheinfo.c configure: Drop AIX host support 2017-09-07 19:02:30 +01:00
compatfd.c cpus: remove ugly cast on sigbus_handler 2017-03-03 16:40:02 +01:00
coroutine-sigaltstack.c coroutine-sigaltstack: use helper for allocating stack memory 2016-09-29 14:13:39 +02:00
coroutine-ucontext.c coroutine-ucontext: use helper for allocating stack memory 2016-09-29 14:13:39 +02:00
coroutine-win32.c coroutine: add a macro for the coroutine stack size 2016-09-29 14:13:39 +02:00
crc32c.c util: Clean up includes 2016-02-04 17:01:04 +00:00
cutils.c Convert remaining single line fprintf() to warn_report() 2017-09-19 16:20:49 +02:00
envlist.c util: Use g_malloc/g_free in envlist.c 2017-05-07 09:57:51 +03:00
error.c error: Implement the warn and free Error functions 2017-07-13 13:50:19 +02:00
event_notifier-posix.c Remove/replace sysemu/char.h inclusion 2017-06-02 11:33:52 +04:00
event_notifier-win32.c event_notifier: prevent accidental use after close 2017-03-29 02:35:23 +03:00
fifo8.c migration: consolidate VMStateField.start 2017-02-13 17:27:13 +00:00
getauxval.c util: Clean up includes 2016-02-04 17:01:04 +00:00
hbitmap.c hbitmap: add next_zero function 2017-12-18 10:54:13 -05:00
hexdump.c util: Improved qemu_hexmap() to include an ascii dump of the buffer 2016-04-06 09:52:07 +08:00
host-utils.c host-utils: Implement unsigned quadword left/right shift and unit tests 2017-01-31 10:10:14 +11:00
id.c util: move declarations out of qemu-common.h 2016-03-22 22:20:17 +01:00
iohandler.c block: move AioContext, QEMUTimer, main-loop to libqemuutil 2017-02-21 11:14:07 +00:00
iov.c util: Fix assertion in iov_copy() upon zero 'bytes' and non-zero 'offset' 2016-08-03 18:44:57 +02:00
keyval.c qapi: Generate FOO_str() macro for QAPI enum FOO 2017-09-04 13:09:13 +02:00
lockcnt.c qemu-thread: optimize QemuLockCnt with futexes on Linux 2017-01-16 13:25:18 +00:00
log.c util/cutils: Rename qemu_strtoll(), qemu_strtoull() 2017-02-23 20:35:35 +01:00
main-loop.c Convert multi-line fprintf() to warn_report() 2017-09-19 14:09:34 +02:00
memfd.c memfd: fix configure test 2017-12-20 22:29:26 +01:00
mmap-alloc.c sparc: Make sure we mmap at SHMLBA alignment 2017-12-15 15:26:24 +00:00
module.c module: Don't load the same module if requested multiple times 2016-10-07 14:14:06 +02:00
notify.c util: Clean up includes 2016-02-04 17:01:04 +00:00
osdep.c osdep: introduce qemu_mprotect_rwx/none 2017-10-24 13:53:42 -07:00
oslib-posix.c oslib-posix: Use sysctl(2) call to resolve exec_dir on NetBSD 2017-11-02 16:19:34 +00:00
oslib-win32.c block: rip out all traces of password prompting 2017-07-11 17:44:56 +02:00
pagesize.c util: move qemu_real_host_page_size/mask to osdep.h 2017-10-10 09:45:00 -07:00
path.c util: Removed unneeded header from path.c 2017-03-14 13:26:37 +01:00
qdist.c qdist: return "(empty)" instead of NULL when printing an empty dist 2016-08-03 18:44:56 +02:00
qemu-config.c config: qemu_config_parse() return number of config groups 2017-10-09 23:21:52 -03:00
qemu-coroutine-io.c coroutine: move entry argument to qemu_coroutine_create 2016-07-13 13:26:02 +02:00
qemu-coroutine-lock.c coroutine-lock: add qemu_co_rwlock_downgrade and qemu_co_rwlock_upgrade 2017-07-17 11:28:15 +08:00
qemu-coroutine-sleep.c coroutine: simplify co_aio_sleep_ns() prototype 2017-12-19 09:25:27 +00:00
qemu-coroutine.c coroutine: abort if we try to schedule or enter a pending coroutine 2017-11-21 11:58:07 -05:00
qemu-error.c error: Revert unwanted change of warning messages 2017-07-24 14:16:29 +02:00
qemu-openpty.c util: Clean up includes 2016-02-04 17:01:04 +00:00
qemu-option.c option: Drop unused get_param_value(), get_next_param_value() 2017-12-20 09:11:50 +01:00
qemu-progress.c progress: Show current progress on SIGINFO 2017-04-28 18:48:11 +02:00
qemu-sockets.c misc: remove duplicated includes 2017-12-18 17:07:02 +03:00
qemu-thread-posix.c qemu-thread: fix races on threads that exit very quickly 2017-12-20 22:29:26 +01:00
qemu-thread-win32.c qemu-thread: Assert locks are initialized before using 2017-07-04 14:39:28 +02:00
qemu-timer-common.c util: Clean up includes 2016-02-04 17:01:04 +00:00
qemu-timer.c icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread 2017-03-14 13:51:34 +01:00
qht.c qht: fix unlock-after-free segfault upon resizing 2016-10-06 18:04:13 +02:00
range.c range: Replace internal representation of Range 2016-07-04 16:49:33 +03:00
rcu.c Revert "rcu: do not create thread in pthread_atfork callback" 2017-08-08 10:40:19 +02:00
readline.c util: move declarations out of qemu-common.h 2016-03-22 22:20:17 +01:00
stats64.c util/stats64: Fix min/max comparisons 2017-11-15 09:34:51 +01:00
systemd.c qemu-ga: obey LISTEN_PID when using systemd socket activation 2017-03-19 11:12:12 +01:00
thread-pool.c thread-pool: add missing qemu_bh_cancel in completion function 2017-03-17 12:54:21 +01:00
throttle.c throttle: Assert that bkt->max is valid in throttle_compute_wait() 2017-09-26 14:46:23 +02:00
timed-average.c Fix some typos found by codespell 2016-05-18 15:04:27 +03:00
trace-events docs: fix broken paths to docs/devel/tracing.txt 2017-07-31 13:12:53 +03:00
unicode.c util: move declarations out of qemu-common.h 2016-03-22 22:20:17 +01:00
uri.c Fix documentation and some comments (article, grammar) 2017-01-24 23:26:52 +03:00
uuid.c uuid: Tighten uuid parse 2016-09-23 11:42:52 +08:00