Received: from eggs.gnu.org ([2001:4830:134:3::10]:48751) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bK67g-0003rw-PQ for qemu-devel@nongnu.org; Mon, 04 Jul 2016 11:49:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bK67c-00051S-IY for qemu-devel@nongnu.org; Mon, 04 Jul 2016 11:49:23 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52780) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bK67c-00051O-AI for qemu-devel@nongnu.org; Mon, 04 Jul 2016 11:49:20 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8CFD5C049E1B for ; Mon, 4 Jul 2016 15:49:19 +0000 (UTC) References: <20160704153823.16879-1-marcandre.lureau@redhat.com> From: Paolo Bonzini Message-ID: <5b719927-c4db-e0ff-f694-6b620b28fc6f@redhat.com> Date: Mon, 4 Jul 2016 17:49:10 +0200 MIME-Version: 1.0 In-Reply-To: <20160704153823.16879-1-marcandre.lureau@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH] char: do not use atexit cleanup handler List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: marcandre.lureau@redhat.com, qemu-devel@nongnu.org On 04/07/2016 17:38, marcandre.lureau@redhat.com wrote: > From: Marc-Andr=C3=A9 Lureau >=20 > It turns out qemu is calling exit() in various places from various > threads without taking much care of resources state. The atexit() > cleanup handlers cannot easily destroy resources that are in use (by > the same thread or other). >=20 > Since c1111a24a3, TCG arm guests run into the following abort() when > running tests, the chardev mutex is locked during the write, so > qemu_mutex_destroy() returns an error: >=20 > #0 0x00007fffdbb806f5 in raise () at /lib64/libc.so.6 > #1 0x00007fffdbb822fa in abort () at /lib64/libc.so.6 > #2 0x00005555557616fe in error_exit (err=3D, msg=3Dmsg= @entry=3D0x555555c38c30 <__func__.14622> "qemu_mutex_destroy") > at /home/drjones/code/qemu/util/qemu-thread-posix.c:39 > #3 0x0000555555b0be20 in qemu_mutex_destroy (mutex=3Dmutex@entry=3D0x= 5555566aa0e0) at /home/drjones/code/qemu/util/qemu-thread-posix.c:57 > #4 0x00005555558aab00 in qemu_chr_free_common (chr=3D0x5555566aa0e0) = at /home/drjones/code/qemu/qemu-char.c:4029 > #5 0x00005555558b05f9 in qemu_chr_delete (chr=3D) at /= home/drjones/code/qemu/qemu-char.c:4038 > #6 0x00005555558b05f9 in qemu_chr_delete (chr=3D) at /= home/drjones/code/qemu/qemu-char.c:4044 > #7 0x00005555558b062c in qemu_chr_cleanup () at /home/drjones/code/qe= mu/qemu-char.c:4557 > #8 0x00007fffdbb851e8 in __run_exit_handlers () at /lib64/libc.so.6 > #9 0x00007fffdbb85235 in () at /lib64/libc.so.6 > #10 0x00005555558d1b39 in testdev_write (testdev=3D0x5555566aa0a0) at = /home/drjones/code/qemu/backends/testdev.c:71 > #11 0x00005555558d1b39 in testdev_write (chr=3D, buf=3D= 0x7fffc343fd9a "", len=3D0) at /home/drjones/code/qemu/backends/testdev.c= :95 > #12 0x00005555558adced in qemu_chr_fe_write (s=3D0x5555566aa0e0, buf=3D= buf@entry=3D0x7fffc343fd98 "0q", len=3Dlen@entry=3D2) at /home/drjones/co= de/qemu/qemu-char.c:282 >=20 > Instead of using a atexit() handler, only run the chardev cleanup as > initially proposed at the end of main(), where there are less chances > (hic) of conflicts or other races. >=20 > Signed-off-by: Marc-Andr=C3=A9 Lureau > Reported-by: Andrew Jones > --- > include/sysemu/char.h | 7 +++++++ > qemu-char.c | 4 +--- > vl.c | 1 + > 3 files changed, 9 insertions(+), 3 deletions(-) >=20 > diff --git a/include/sysemu/char.h b/include/sysemu/char.h > index 57df10a..0ea9eac 100644 > --- a/include/sysemu/char.h > +++ b/include/sysemu/char.h > @@ -151,6 +151,13 @@ CharDriverState *qemu_chr_new(const char *label, c= onst char *filename, > */ > void qemu_chr_disconnect(CharDriverState *chr); > =20 > +/** > + * @qemu_chr_cleanup: > + * > + * Delete all chardevs (when leaving qemu) > + */ > +void qemu_chr_cleanup(void); > + > /** > * @qemu_chr_new_noreplay: > * > diff --git a/qemu-char.c b/qemu-char.c > index b73969d..a542192 100644 > --- a/qemu-char.c > +++ b/qemu-char.c > @@ -4549,7 +4549,7 @@ void qmp_chardev_remove(const char *id, Error **e= rrp) > qemu_chr_delete(chr); > } > =20 > -static void qemu_chr_cleanup(void) > +void qemu_chr_cleanup(void) > { > CharDriverState *chr, *tmp; > =20 > @@ -4604,8 +4604,6 @@ static void register_types(void) > * is specified > */ > qemu_add_machine_init_done_notifier(&muxes_realize_notify); > - > - atexit(qemu_chr_cleanup); > } > =20 > type_init(register_types); > diff --git a/vl.c b/vl.c > index 9bb7f4c..d0b9ff9 100644 > --- a/vl.c > +++ b/vl.c > @@ -4596,6 +4596,7 @@ int main(int argc, char **argv, char **envp) > #ifdef CONFIG_TPM > tpm_cleanup(); > #endif > + qemu_chr_cleanup(); > =20 > return 0; > } >=20 Queued, thanks. Paolo