From nobody Mon Feb 9 11:51:23 2026 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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=temperror (zoho.com: Error in retrieving data from DNS) 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 1513790479696970.8426467762295; Wed, 20 Dec 2017 09:21:19 -0800 (PST) Received: from localhost ([::1]:60347 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRi36-0008Be-4Z for importer@patchew.org; Wed, 20 Dec 2017 12:20:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56031) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRhxx-0004Eq-Sh for qemu-devel@nongnu.org; Wed, 20 Dec 2017 12:15:39 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eRhxw-0006xR-Bz for qemu-devel@nongnu.org; Wed, 20 Dec 2017 12:15:37 -0500 Received: from mail-wr0-x241.google.com ([2a00:1450:400c:c0c::241]:34984) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eRhxw-0006wd-1W for qemu-devel@nongnu.org; Wed, 20 Dec 2017 12:15:36 -0500 Received: by mail-wr0-x241.google.com with SMTP id l19so10549020wrc.2 for ; Wed, 20 Dec 2017 09:15:35 -0800 (PST) Received: from 640k.lan (dynamic-adsl-78-12-251-125.clienti.tiscali.it. [78.12.251.125]) by smtp.gmail.com with ESMTPSA id f125sm2751101wme.45.2017.12.20.09.15.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Dec 2017 09:15:33 -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=jY3EPEj1ZRqWUh1Q+gBi0qZbIT31FfMrZu7CmFspgX4=; b=FXhCUaOA84uNzR3OT6T85St4Zi1rv6E7HN5XAr4W4WVh98olwMGxclPCy337wq+dRl mwFPrCtbcdXWFnBR02sbN9lX9ZAvkEZ8hRXqT3CraSzerU3tJ4F74Tj1QLgWweOxm/v2 etnCTWMDA4xh7jApPVw7OWiKp2gViVdZluxNQP2JeanrDhhhqbL4Cmp552F5uOPotIxr wC+C3/yX9kpw34bYw/oenRagDYYp+GLGCl5sFrpOPAncniQk7PkjsamYaHqGJbR+INvF VyTsdPXnH7kpr3Euz5Ep0niwuBu7eh1Z5/ALuu9mRfPhxQXMgLVS3piT2XiVscOwBJqN D37A== 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=jY3EPEj1ZRqWUh1Q+gBi0qZbIT31FfMrZu7CmFspgX4=; b=N5QXPQMduiXHNCaxyxMEdcfS2Jf28sATcyeiQrDaR48nZTMPyRTvnylzjPEqA9BzDK UGBjiURfaW17bUaeTsApGHSHXN+zJ+kr7Bw8FLwnXyzzurU+zukY+WQA1et61pTwCgDa AQyMMlhSG/c+kV58zyoSzli74bvJSK0w/s/cIWj6CzZhywNlCZTys0D+Nl47YABbCnFx XJl7XUozkjQ4XbpgX08Fz469prGrF4Y4BMoF2BUHZ15IFNTwaHiziIW+0Y5T11IVaKax hg3l4sAX39HEkaYFtHZEovMh+WvwcDUn5WpcV5+oLS2i5+yB+mqyG4kMt9N7BQRSKz+M ytOA== X-Gm-Message-State: AKGB3mIDRMagTjykiIEwcxd5ML4HtZaYAZaIyFs1MSEpFVIsNkaJqPhS P5l6MglBfbAqMr1bGo2PE3D81FvM X-Google-Smtp-Source: ACJfBotRkFnERvds15goRRbeIg5JPpg7jCDTfBslNjtlErhMBdfqsQgKlJ83/VS6Zb/e/kVYTXTKSw== X-Received: by 10.223.157.41 with SMTP id k41mr8430746wre.281.1513790134634; Wed, 20 Dec 2017 09:15:34 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 20 Dec 2017 18:14:14 +0100 Message-Id: <1513790098-9815-3-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1513790098-9815-1-git-send-email-pbonzini@redhat.com> References: <1513790098-9815-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::241 Subject: [Qemu-devel] [PULL 02/46] 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_6 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 7306475..fcd369b 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*), @@ -496,29 +510,40 @@ void qemu_thread_create(QemuThread *thread, const cha= r *name, sigset_t set, oldset; int err; pthread_attr_t attr; + QemuThreadArgs *qemu_thread_args; =20 err =3D pthread_attr_init(&attr); if (err) { 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); + 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 1.8.3.1