From patchwork Wed Jan 4 01:32:01 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhanghailiang X-Patchwork-Id: 9495999 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id ACC1A606B5 for ; Wed, 4 Jan 2017 01:33:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9D31D26E8A for ; Wed, 4 Jan 2017 01:33:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8FA7727829; Wed, 4 Jan 2017 01:33:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 247A926E8A for ; Wed, 4 Jan 2017 01:33:22 +0000 (UTC) Received: from localhost ([::1]:36868 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cOaS9-0008TM-IE for patchwork-qemu-devel@patchwork.kernel.org; Tue, 03 Jan 2017 20:33:21 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49605) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cOaRQ-0008O0-IV for qemu-devel@nongnu.org; Tue, 03 Jan 2017 20:32:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cOaRP-00059b-Ko for qemu-devel@nongnu.org; Tue, 03 Jan 2017 20:32:36 -0500 Received: from szxga02-in.huawei.com ([119.145.14.65]:6254) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1cOaRI-00055F-Vn; Tue, 03 Jan 2017 20:32:29 -0500 Received: from 172.24.1.47 (EHLO SZXEML423-HUB.china.huawei.com) ([172.24.1.47]) by szxrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DSY46828; Wed, 04 Jan 2017 09:32:21 +0800 (CST) Received: from localhost (10.177.24.212) by SZXEML423-HUB.china.huawei.com (10.82.67.154) with Microsoft SMTP Server id 14.3.235.1; Wed, 4 Jan 2017 09:32:13 +0800 From: zhanghailiang To: Date: Wed, 4 Jan 2017 09:32:01 +0800 Message-ID: <1483493521-9604-1-git-send-email-zhang.zhanghailiang@huawei.com> X-Mailer: git-send-email 2.7.2.windows.1 MIME-Version: 1.0 X-Originating-IP: [10.177.24.212] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A0B0207.586C50A6.0099, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: ae2150d383821d43b514b82943c71923 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 119.145.14.65 Subject: [Qemu-devel] [PATCH v2] qemu-thread: fix qemu_thread_set_name() race in qemu_thread_create() 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: zhang.zhanghailiang@huawei.com, stefanha@gmail.com, qemu-stable@nongnu.org, Caoxinhua , arei.gonglei@huawei.com, pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Caoxinhua 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 #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 , 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 , opaque=0x7f99b8003370, destroy=0x7c6220 ) at io/task.c:133 #9 0x00000000007bda04 in qio_channel_socket_connect_async (ioc=0x7f99b80014c0, addr=0x37235d0, callback=callback@entry=0x54ad00 , 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=, argv=, envp=) at vl.c:5083 Let's detach the new thread after calling qemu_thread_set_name(). Signed-off-by: Caoxinhua Signed-off-by: zhanghailiang --- v2: Fix missing title --- util/qemu-thread-posix.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index d20cdde..d31793d 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -481,12 +481,6 @@ void qemu_thread_create(QemuThread *thread, const char *name, if (err) { error_exit(err, __func__); } - if (mode == QEMU_THREAD_DETACHED) { - err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - if (err) { - error_exit(err, __func__); - } - } /* Leave signal handling to the iothread. */ sigfillset(&set); @@ -499,6 +493,12 @@ void qemu_thread_create(QemuThread *thread, const char *name, qemu_thread_set_name(thread, name); } + if (mode == QEMU_THREAD_DETACHED) { + err = pthread_detach(thread->thread); + if (err) { + error_exit(err, __func__); + } + } pthread_sigmask(SIG_SETMASK, &oldset, NULL); pthread_attr_destroy(&attr);