From nobody Mon Feb 9 09:34:47 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513845637779190.20187218568935; Thu, 21 Dec 2017 00:40:37 -0800 (PST) Received: from localhost ([::1]:39395 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRwOp-0003UD-TV for importer@patchew.org; Thu, 21 Dec 2017 03:40:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35375) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRwKI-0007wa-10 for qemu-devel@nongnu.org; Thu, 21 Dec 2017 03:35:41 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eRwKE-0006AW-Rh for qemu-devel@nongnu.org; Thu, 21 Dec 2017 03:35:37 -0500 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:44639) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eRwKE-00068u-Hp for qemu-devel@nongnu.org; Thu, 21 Dec 2017 03:35:34 -0500 Received: by mail-wm0-x242.google.com with SMTP id t8so14328761wmc.3 for ; Thu, 21 Dec 2017 00:35:34 -0800 (PST) Received: from donizetti.lan (dynamic-adsl-78-12-251-125.clienti.tiscali.it. [78.12.251.125]) by smtp.gmail.com with ESMTPSA id 26sm29752439wrw.19.2017.12.21.00.35.31 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 21 Dec 2017 00:35:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=O/l1tmqpwwb29cz+x+UJmhid3AXk3LGpkQpM1r7asGc=; b=cmB4kiN7kpU/GCSmZMj6H9pHurVJrJU4v2Uh8DwskL4g8r3IU+SJ2eBqb9yrAS85ub MZJeD+JlU39on+2pLh0+v0cOxV8EBhi3tyRffd8Jl+cdtNzv0ck7G0FqCSGBQQ4FiCln ov7nToO8rc1mjSx4u2iLa3kjZB9UZ83yb7Nz5mYR5L3ofzzI4UEv1QTtWxFndoWAAxwx 1GvHJQVFGwsvXbly+p7U8Wr6XH+h60zF6W5MMSaXBzhFrbCQMndLXWGz1jEuQEVPtbIN xJ2m3w1+eQ9lJLatKGdj+k792v5lHdtIi9LhhSBW3bVN4nEBJM27HbDJ21Ir9BFuJigs kL9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=O/l1tmqpwwb29cz+x+UJmhid3AXk3LGpkQpM1r7asGc=; b=UQD8A7ibPBCMXc54NxMN/nt9pb81ziQWMj2flf0v+IB2vKTn6jYrFUmic5Pe66V9DU QmDGEljN4zJQ3nisSrituZMVZhhsydnw9e39EGtbQKtbDZDxX5cta90FWdR/tKxQ32NS bKWG++hMNn4JMg5c615DMchGh3/CsJUAhWAJnvEx/pdmLWm6RunFHgVSLPcE0MZmFRHT ILTBy/a60XBWzDC6NhfIBm3pBdqv17W6oEZkyr4SAKiOO9Nfzc9tBbKYOQP5a1/OMrZs WT9phaxSiyjBrZvgvod0lswetjLBi0eEXVE1KxntfZnknexUI0a8TlKhXuBhMWkRRoUX sm3A== X-Gm-Message-State: AKGB3mI/Z5VNFD4oWhZgg8LTHNiBIZbCZ5HQzabBGUQx5Tl3JgPLyEKo 3Gjyt4jvCWiq2GTWztpC/iTstq7o X-Google-Smtp-Source: ACJfBoumkHQ1hekvBr9/OUUFBhbgw6rp25BiVOYQmw/VhpI8LH8vjn8BxyF4bQW1n02RfGQFwNHP9w== X-Received: by 10.28.150.12 with SMTP id y12mr9414241wmd.95.1513845333077; Thu, 21 Dec 2017 00:35:33 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 21 Dec 2017 09:35:27 +0100 Message-Id: <20171221083529.6982-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171221083529.6982-1-pbonzini@redhat.com> References: <20171221083529.6982-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::242 Subject: [Qemu-devel] [PULL 02/41] qemu-thread: fix races on threads that exit very quickly X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linzhecheng Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: linzhecheng If we create a thread with QEMU_THREAD_DETACHED mode, QEMU may get a segfau= lt with low probability. The backtrace is: #0 0x00007f46c60291d7 in __GI_raise (sig=3Dsig@entry=3D6) at ../nptl/sy= sdeps/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 #5 pthread_detach (th=3D139933037614848) at pthread_detach.c:50 #6 0x0000000000829759 in qemu_thread_create (thread=3Dthread@entry=3D0x= 7ffdaa8205e0, name=3Dname@entry=3D0x94d94a "io-task-worker", start_routine= =3Dstart_routine@entry=3D0x7eb9a0 , arg=3Darg@entry=3D0x3f5cf70, mode=3Dmode@entry=3D1) at util/qemu_thr= ead_posix.c:512 #7 0x00000000007ebc96 in qio_task_run_in_thread (task=3D0x31db2c0, work= er=3Dworker@entry=3D0x7e7e40 , opaque=3D= 0xcd23380, destroy=3D0x7f1180 ) at io/task.c:141 #8 0x00000000007e7f33 in qio_channel_socket_connect_async (ioc=3Dioc@en= try=3D0x626c0b0, addr=3D, callback=3Dcallback@entry=3D0x55e0= 80 , opaque=3Dopaque@entry=3D0x42862c0, destroy=3Ddestroy@entry=3D0x0) at io/channel_socket.c:194 #9 0x000000000055bdd1 in socket_reconnect_timeout (opaque=3D0x42862c0) = 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/lib= glib-2.0.so.0 #12 0x000000000076c646 in glib_pollfds_poll () at main_loop.c:228 #13 0x000000000076c6eb in os_host_main_loop_wait (timeout=3D348000000) a= t main_loop.c:273 #14 0x000000000076c815 in main_loop_wait (nonblocking=3Dnonblocking@entr= y=3D0) at main_loop.c:521 #15 0x000000000056a511 in main_loop () at vl.c:2076 #16 0x0000000000420705 in main (argc=3D, argv=3D, envp=3D) 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=3D19951. 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 Message-Id: <20171128044656.10592-1-linzhecheng@huawei.com> Reviewed-by: Fam Zheng [Simplify the code by removing qemu_thread_set_name, and free the arguments before invoking the start routine. - Paolo] Signed-off-by: Paolo Bonzini --- util/qemu-thread-posix.c | 59 ++++++++++++++++++++++++++++++++++----------= ---- 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 7306475899..959a57079f 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -479,15 +479,29 @@ static void __attribute__((constructor)) qemu_thread_= atexit_init(void) } =20 =20 -/* Attempt to set the threads name; note that this is for debug, so - * we're not going to fail if we can't set it. - */ -static void qemu_thread_set_name(QemuThread *thread, const char *name) -{ #ifdef CONFIG_PTHREAD_SETNAME_NP - pthread_setname_np(thread->thread, name); -#endif +typedef struct { + void *(*start_routine)(void *); + void *arg; + char *name; +} QemuThreadArgs; + +static void *qemu_thread_start(void *args) +{ + QemuThreadArgs *qemu_thread_args =3D args; + void *(*start_routine)(void *) =3D qemu_thread_args->start_routine; + void *arg =3D qemu_thread_args->arg; + + /* Attempt to set the threads name; note that this is for debug, so + * we're not going to fail if we can't set it. + */ + pthread_setname_np(pthread_self(), qemu_thread_args->name); + g_free(qemu_thread_args->name); + g_free(qemu_thread_args); + return start_routine(arg); } +#endif + =20 void qemu_thread_create(QemuThread *thread, const char *name, void *(*start_routine)(void*), @@ -502,23 +516,34 @@ void qemu_thread_create(QemuThread *thread, const cha= r *name, error_exit(err, __func__); } =20 + if (mode =3D=3D QEMU_THREAD_DETACHED) { + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + } + /* Leave signal handling to the iothread. */ sigfillset(&set); pthread_sigmask(SIG_SETMASK, &set, &oldset); - err =3D pthread_create(&thread->thread, &attr, start_routine, arg); - if (err) - error_exit(err, __func__); =20 +#ifdef CONFIG_PTHREAD_SETNAME_NP if (name_threads) { - qemu_thread_set_name(thread, name); + QemuThreadArgs *qemu_thread_args; + qemu_thread_args =3D g_new0(QemuThreadArgs, 1); + qemu_thread_args->name =3D g_strdup(name); + qemu_thread_args->start_routine =3D start_routine; + qemu_thread_args->arg =3D arg; + + err =3D pthread_create(&thread->thread, &attr, + qemu_thread_start, qemu_thread_args); + } else +#endif + { + err =3D pthread_create(&thread->thread, &attr, + start_routine, arg); } =20 - if (mode =3D=3D QEMU_THREAD_DETACHED) { - err =3D pthread_detach(thread->thread); - if (err) { - error_exit(err, __func__); - } - } + if (err) + error_exit(err, __func__); + pthread_sigmask(SIG_SETMASK, &oldset, NULL); =20 pthread_attr_destroy(&attr); --=20 2.14.3