From: Eric Blake <eblake@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
QEMU Developers <qemu-devel@nongnu.org>,
"Emilio G . Cota" <cota@braap.org>, Fam Zheng <famz@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [Qemu-devel] [RFC PATCH 0/5] Scoped locks using attribute((cleanup))
Date: Mon, 11 Dec 2017 08:11:44 -0600 [thread overview]
Message-ID: <b5a01fb0-4f25-7cae-99d3-c9722912b6c1@redhat.com> (raw)
In-Reply-To: <CAFEAcA8j_tGZr-i5LE+ZXAnk4v=pfrKadmgsf=ncMHRUsYK1zQ@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2874 bytes --]
On 12/11/2017 03:38 AM, Peter Maydell wrote:
> On 8 December 2017 at 19:40, Eric Blake <eblake@redhat.com> wrote:
>> On 12/08/2017 04:55 AM, Paolo Bonzini wrote:
>>> Likewise,
>>>
>>> QEMU_WITH_LOCK(QemuMutex, guard_name, &some_mutex) {
>>> ...
>>> }
>>>
>>> is the same as
>>>
>>> qemu_mutex_lock(&some_mutex);
>>> ...
>>> qemu_mutex_unlock(&some_mutex);
>>>
>>> except that any returns within the region will unlock the mutex.
>>
>> Not just returns, but ANY manner that you leave the scope, including a
>> goto or just falling out of the end of the scope.
>
> How about longjmp()ing out of it?
Easy to test:
==========
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
void my_cleanup (int *ptr) {
int *i = ptr;
printf("in my_cleanup(%d)\n", *i);
}
jmp_buf jmp;
void foo(int i) {
while (1) {
__attribute__((cleanup(my_cleanup))) int j = i;
if (i == 0) {
printf("before leaving scope by return\n");
return;
}
if (i == 1) {
goto label;
}
if (i == 3) {
longjmp(jmp, 1);
}
if (i == 4) {
printf("before leaving scope by exit\n");
exit(0);
}
break;
}
printf("after leaving scope by break\n");
return;
label:
printf("after leaving scope by return\n");
}
int main(void) {
foo(0);
foo(1);
foo(2);
if (!setjmp(jmp)) {
foo(3);
}
printf("after leaving scope by longjmp\n");
foo(4);
}
============
But the results aren't necessarily nice, depending on how we currently
(ab)use longjmp. Under Fedora 27
gcc (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2)
I get
$ ./foo
before leaving scope by return
in my_cleanup(0)
in my_cleanup(1)
after leaving scope by return
in my_cleanup(2)
after leaving scope by break
after leaving scope by longjmp
before leaving scope by exit
I don't know if there is a way to make gcc insert stack-unwind
directives that are honored across longjmp (I know C++ does it for
exceptions; so there may be a way, and I just don't know it).
Conversely, I do know that pthread_cleanup_push/pop, which does
something similar, is permitted by POSIX to NOT work across longjmp:
Calling longjmp(3) (siglongjmp(3)) produces undefined results
if any
call has been made to pthread_cleanup_push() or
pthread_cleanup_pop()
without the matching call of the pair since the jump buffer was
filled
by setjmp(3) (sigsetjmp(3)). Likewise, calling longjmp(3)
(sig‐
longjmp(3)) from inside a clean-up handler produces undefined
results
unless the jump buffer was also filled by setjmp(3)
(sigsetjmp(3))
inside the handler.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]
next prev parent reply other threads:[~2017-12-11 14:12 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-08 10:55 [Qemu-devel] [RFC PATCH 0/5] Scoped locks using attribute((cleanup)) Paolo Bonzini
2017-12-08 10:55 ` [Qemu-devel] [PATCH 1/5] compiler: add a helper for C99 inline functions Paolo Bonzini
2017-12-08 10:55 ` [Qemu-devel] [PATCH 2/5] lock-guard: add scoped lock implementation Paolo Bonzini
2017-12-08 15:30 ` Stefan Hajnoczi
2017-12-08 17:56 ` Paolo Bonzini
2017-12-08 20:12 ` Eric Blake
2017-12-11 10:16 ` Stefan Hajnoczi
2017-12-11 13:51 ` Eric Blake
2017-12-12 9:16 ` Stefan Hajnoczi
2017-12-08 10:55 ` [Qemu-devel] [PATCH 3/5] qemu-timer: convert to use lock guards Paolo Bonzini
2017-12-08 14:26 ` Stefan Hajnoczi
2017-12-08 10:55 ` [Qemu-devel] [PATCH 4/5] qht: " Paolo Bonzini
2017-12-08 14:27 ` Stefan Hajnoczi
2017-12-08 10:55 ` [Qemu-devel] [PATCH 5/5] thread-pool: " Paolo Bonzini
2017-12-08 15:13 ` Stefan Hajnoczi
2017-12-08 18:12 ` Paolo Bonzini
2017-12-08 20:02 ` Eric Blake
2017-12-11 10:23 ` Stefan Hajnoczi
2017-12-11 22:03 ` Paolo Bonzini
2017-12-08 19:50 ` Eric Blake
2017-12-11 6:35 ` Peter Xu
2017-12-08 19:40 ` [Qemu-devel] [RFC PATCH 0/5] Scoped locks using attribute((cleanup)) Eric Blake
2017-12-11 9:38 ` Peter Maydell
2017-12-11 14:11 ` Eric Blake [this message]
2017-12-11 21:32 ` Paolo Bonzini
2017-12-12 20:41 ` Eric Blake
2017-12-15 15:50 ` Richard Henderson
2017-12-11 6:40 ` no-reply
2017-12-11 6:40 ` no-reply
2017-12-11 6:46 ` no-reply
2017-12-11 22:06 ` Emilio G. Cota
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b5a01fb0-4f25-7cae-99d3-c9722912b6c1@redhat.com \
--to=eblake@redhat.com \
--cc=cota@braap.org \
--cc=famz@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).