From nobody Mon Apr 7 21:48:42 2025 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; 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513697361449540.5262568326831; Tue, 19 Dec 2017 07:29:21 -0800 (PST) Received: from localhost ([::1]:49396 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRJpJ-00017L-B4 for importer@patchew.org; Tue, 19 Dec 2017 10:29:05 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58172) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRJaN-0005NA-PU for qemu-devel@nongnu.org; Tue, 19 Dec 2017 10:13:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eRJaI-0007aF-Cw for qemu-devel@nongnu.org; Tue, 19 Dec 2017 10:13:39 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50343) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eRJaI-0007ZS-3j for qemu-devel@nongnu.org; Tue, 19 Dec 2017 10:13:34 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3EAED78EC1; Tue, 19 Dec 2017 15:13:33 +0000 (UTC) Received: from localhost (ovpn-117-161.ams2.redhat.com [10.36.117.161]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9202319658; Tue, 19 Dec 2017 15:13:32 +0000 (UTC) From: Stefan Hajnoczi To: Date: Tue, 19 Dec 2017 15:11:43 +0000 Message-Id: <20171219151144.11120-23-stefanha@redhat.com> In-Reply-To: <20171219151144.11120-1-stefanha@redhat.com> References: <20171219151144.11120-1-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 19 Dec 2017 15:13:33 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL v2 22/23] iothread: fix iothread_stop() race condition 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: Peter Maydell , Stefan Hajnoczi 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" There is a small chance that iothread_stop() hangs as follows: Thread 3 (Thread 0x7f63eba5f700 (LWP 16105)): #0 0x00007f64012c09b6 in ppoll () at /lib64/libc.so.6 #1 0x000055959992eac9 in ppoll (__ss=3D0x0, __timeout=3D0x0, __nfds=3D, __fds=3D) at /usr/include/bits/poll2.h:77 #2 0x000055959992eac9 in qemu_poll_ns (fds=3D, nfds=3D, timeout=3D) at util/qemu-timer.c:322 #3 0x0000559599930711 in aio_poll (ctx=3D0x55959bdb83c0, blocking=3Dbloc= king@entry=3Dtrue) at util/aio-posix.c:629 #4 0x00005595996806fe in iothread_run (opaque=3D0x55959bd78400) at iothr= ead.c:59 #5 0x00007f640159f609 in start_thread () at /lib64/libpthread.so.0 #6 0x00007f64012cce6f in clone () at /lib64/libc.so.6 Thread 1 (Thread 0x7f640b45b280 (LWP 16103)): #0 0x00007f64015a0b6d in pthread_join () at /lib64/libpthread.so.0 #1 0x00005595999332ef in qemu_thread_join (thread=3D) at = util/qemu-thread-posix.c:547 #2 0x00005595996808ae in iothread_stop (iothread=3D) at i= othread.c:91 #3 0x000055959968094d in iothread_stop_iter (object=3D, o= paque=3D) at iothread.c:102 #4 0x0000559599857d97 in do_object_child_foreach (obj=3Dobj@entry=3D0x55= 959bdb8100, fn=3Dfn@entry=3D0x559599680930 , opaque=3Do= paque@entry=3D0x0, recurse=3Drecurse@entry=3Dfalse) at qom/object.c:852 #5 0x0000559599859477 in object_child_foreach (obj=3Dobj@entry=3D0x55959= bdb8100, fn=3Dfn@entry=3D0x559599680930 , opaque=3Dopaq= ue@entry=3D0x0) at qom/object.c:867 #6 0x0000559599680a6e in iothread_stop_all () at iothread.c:341 #7 0x000055959955b1d5 in main (argc=3D, argv=3D, envp=3D) at vl.c:4913 The relevant code from iothread_run() is: while (!atomic_read(&iothread->stopping)) { aio_poll(iothread->ctx, true); and iothread_stop(): iothread->stopping =3D true; aio_notify(iothread->ctx); ... qemu_thread_join(&iothread->thread); The following scenario can occur: 1. IOThread: while (!atomic_read(&iothread->stopping)) -> stopping=3Dfalse 2. Main loop: iothread->stopping =3D true; aio_notify(iothread->ctx); 3. IOThread: aio_poll(iothread->ctx, true); -> hang The bug is explained by the AioContext->notify_me doc comments: "If this field is 0, everything (file descriptors, bottom halves, timers) will be re-evaluated before the next blocking poll(), thus the event_notifier_set call can be skipped." The problem is that "everything" does not include checking iothread->stopping. This means iothread_run() will block in aio_poll() if aio_notify() was called just before aio_poll(). This patch fixes the hang by replacing aio_notify() with aio_bh_schedule_oneshot(). This makes aio_poll() or g_main_loop_run() to return. Implementing this properly required a new bool running flag. The new flag prevents races that are tricky if we try to use iothread->stopping. Now iothread->stopping is purely for iothread_stop() and iothread->running is purely for the iothread_run() thread. Signed-off-by: Stefan Hajnoczi Reviewed-by: Eric Blake Message-id: 20171207201320.19284-6-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi --- include/sysemu/iothread.h | 3 ++- iothread.c | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/sysemu/iothread.h b/include/sysemu/iothread.h index 55de1715c7..799614ffd2 100644 --- a/include/sysemu/iothread.h +++ b/include/sysemu/iothread.h @@ -29,7 +29,8 @@ typedef struct { GOnce once; QemuMutex init_done_lock; QemuCond init_done_cond; /* is thread initialization done? */ - bool stopping; + bool stopping; /* has iothread_stop() been called? */ + bool running; /* should iothread_run() continue? */ int thread_id; =20 /* AioContext poll parameters */ diff --git a/iothread.c b/iothread.c index e7b93e02a3..d8b6c1fb27 100644 --- a/iothread.c +++ b/iothread.c @@ -55,7 +55,7 @@ static void *iothread_run(void *opaque) qemu_cond_signal(&iothread->init_done_cond); qemu_mutex_unlock(&iothread->init_done_lock); =20 - while (!atomic_read(&iothread->stopping)) { + while (iothread->running) { aio_poll(iothread->ctx, true); =20 if (atomic_read(&iothread->worker_context)) { @@ -78,16 +78,25 @@ static void *iothread_run(void *opaque) return NULL; } =20 +/* Runs in iothread_run() thread */ +static void iothread_stop_bh(void *opaque) +{ + IOThread *iothread =3D opaque; + + iothread->running =3D false; /* stop iothread_run() */ + + if (iothread->main_loop) { + g_main_loop_quit(iothread->main_loop); + } +} + void iothread_stop(IOThread *iothread) { if (!iothread->ctx || iothread->stopping) { return; } iothread->stopping =3D true; - aio_notify(iothread->ctx); - if (atomic_read(&iothread->main_loop)) { - g_main_loop_quit(iothread->main_loop); - } + aio_bh_schedule_oneshot(iothread->ctx, iothread_stop_bh, iothread); qemu_thread_join(&iothread->thread); } =20 @@ -134,6 +143,7 @@ static void iothread_complete(UserCreatable *obj, Error= **errp) char *name, *thread_name; =20 iothread->stopping =3D false; + iothread->running =3D true; iothread->thread_id =3D -1; iothread->ctx =3D aio_context_new(&local_error); if (!iothread->ctx) { --=20 2.14.3