From nobody Sun Feb 8 20:34:08 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.zoho.com; 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 1490053044088153.3299216460481; Mon, 20 Mar 2017 16:37:24 -0700 (PDT) Received: from localhost ([::1]:35627 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cq6ra-0007gq-Mv for importer@patchew.org; Mon, 20 Mar 2017 19:37:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41189) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cq6Qp-0001oD-6V for qemu-devel@nongnu.org; Mon, 20 Mar 2017 19:09:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cq6Qi-00051D-Qr for qemu-devel@nongnu.org; Mon, 20 Mar 2017 19:09:43 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:38733 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cq6Qi-00050Y-J6 for qemu-devel@nongnu.org; Mon, 20 Mar 2017 19:09:36 -0400 Received: from pps.filterd (m0098413.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v2KN8WsJ014290 for ; Mon, 20 Mar 2017 19:09:36 -0400 Received: from e17.ny.us.ibm.com (e17.ny.us.ibm.com [129.33.205.207]) by mx0b-001b2d01.pphosted.com with ESMTP id 29af6evvtd-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 20 Mar 2017 19:09:35 -0400 Received: from localhost by e17.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 20 Mar 2017 19:09:35 -0400 Received: from b01cxnp23034.gho.pok.ibm.com (9.57.198.29) by e17.ny.us.ibm.com (146.89.104.204) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 20 Mar 2017 19:09:31 -0400 Received: from b01ledav03.gho.pok.ibm.com (b01ledav003.gho.pok.ibm.com [9.57.199.108]) by b01cxnp23034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v2KN9Vmd45088770; Mon, 20 Mar 2017 23:09:31 GMT Received: from b01ledav03.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AB5B4B2052; Mon, 20 Mar 2017 19:09:27 -0400 (EDT) Received: from localhost (unknown [9.53.92.194]) by b01ledav03.gho.pok.ibm.com (Postfix) with ESMTP id 908AAB2046; Mon, 20 Mar 2017 19:09:27 -0400 (EDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 20 Mar 2017 18:08:08 -0500 X-Mailer: git-send-email 2.7.4 In-Reply-To: <1490051325-3770-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1490051325-3770-1-git-send-email-mdroth@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17032023-0040-0000-0000-000002EF73AD X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00006819; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000206; SDB=6.00836553; UDB=6.00411162; IPR=6.00614321; BA=6.00005224; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00014732; XFM=3.00000013; UTC=2017-03-20 23:09:33 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17032023-0041-0000-0000-000006E3783A Message-Id: <1490051325-3770-45-git-send-email-mdroth@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-03-20_16:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=4 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1702020001 definitions=main-1703200196 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.158.5 Subject: [Qemu-devel] [PATCH 44/81] 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: Caoxinhua , Paolo Bonzini , qemu-stable@nongnu.org, zhanghailiang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=3Dsig@entry=3D6) at ../nptl/sysd= eps/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/libuvpuser= hotfix.so #4 #5 pthread_setname_np (th=3D140298470549248, name=3Dname@entry=3D0x8cc74a= "io-task-worker") at ../nptl/sysdeps/unix/sysv/linux/pthread_setname.c:49 #6 0x00000000007f5f20 in qemu_thread_set_name (thread=3Dthread@entry=3D0x= 7ffd2ac09680, name=3Dname@entry=3D0x8cc74a "io-task-worker") at util/qemu_t= hread_posix.c:459 #7 0x00000000007f679e in qemu_thread_create (thread=3Dthread@entry=3D0x7f= fd2ac09680, name=3Dname@entry=3D0x8cc74a "io-task-worker",start_routine=3Ds= tart_routine@entry=3D0x7c1300 , arg=3Darg@entry=3D0= x7f99b8001720, mode=3Dmode@entry=3D1) at util/qemu_thread_posix.c:498 #8 0x00000000007c15b6 in qio_task_run_in_thread (task=3Dtask@entry=3D0x7f= 99b80033d0, worker=3Dworker@entry=3D0x7bd920 , opaque=3D0x7f99b8003370, destroy=3D0x7c6220 ) at io/task.c:133 #9 0x00000000007bda04 in qio_channel_socket_connect_async (ioc=3D0x7f99b8= 0014c0, addr=3D0x37235d0, callback=3Dcallback@entry=3D0x54ad00 , opaque=3Dopaque@entry=3D0x38118b0, destroy=3Ddestroy@entry= =3D0x0) at io/channel_socket.c:191 #10 0x00000000005487f6 in socket_reconnect_timeout (opaque=3D0x38118b0) a= t 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/libg= lib-2.0.so.0 #13 0x0000000000747386 in glib_pollfds_poll () at main_loop.c:227 #14 0x0000000000747424 in os_host_main_loop_wait (timeout=3D404000000) at= main_loop.c:272 #15 0x0000000000747575 in main_loop_wait (nonblocking=3Dnonblocking@entry= =3D0) at main_loop.c:520 #16 0x0000000000557d31 in main_loop () at vl.c:2170 #17 0x000000000041c8b7 in main (argc=3D, argv=3D, envp=3D) at vl.c:5083 Let's detach the new thread after calling qemu_thread_set_name(). Signed-off-by: Caoxinhua Signed-off-by: zhanghailiang Message-Id: <1483493521-9604-1-git-send-email-zhang.zhanghailiang@huawei.co= m> Signed-off-by: Paolo Bonzini (cherry picked from commit 2f75bd73c319a1224a64a1b5ad680b1a37ed2d7a) Signed-off-by: Michael Roth --- 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 =3D=3D QEMU_THREAD_DETACHED) { - err =3D pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED= ); - if (err) { - error_exit(err, __func__); - } - } =20 /* 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); } =20 + if (mode =3D=3D QEMU_THREAD_DETACHED) { + err =3D pthread_detach(thread->thread); + if (err) { + error_exit(err, __func__); + } + } pthread_sigmask(SIG_SETMASK, &oldset, NULL); =20 pthread_attr_destroy(&attr); --=20 2.7.4