From: "Philippe Mathieu-Daudé" <philmd@redhat.com>
To: "Marc-André Lureau" <marcandre.lureau@redhat.com>
Cc: Peter Xu <peterx@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>,
qemu-devel@nongnu.org,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
Markus Armbruster <armbru@redhat.com>
Subject: Re: [Qemu-devel] [PATCH 2/4] chardev: make qemu_chr_fe_set_handlers() context switching safer
Date: Fri, 22 Feb 2019 09:32:59 +0100 [thread overview]
Message-ID: <fbbb45f4-520c-430b-f4ed-19a9fccd2753@redhat.com> (raw)
In-Reply-To: <20190221080357.GC3091@xz-x1>
On 2/21/19 9:03 AM, Peter Xu wrote:
> On Wed, Feb 20, 2019 at 05:06:26PM +0100, Marc-André Lureau wrote:
>> qemu_chr_fe_set_handlers() may switch the context of various
>> sources. In order to prevent dispatch races from different threads,
>> let's acquire or freeze the context, do all the source switches, and
>> then release/resume the contexts. This should help to make context
>> switching safer.
>>
>> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
>> ---
>> include/chardev/char-fe.h | 23 +++++++++
>> chardev/char-fe.c | 103 +++++++++++++++++++++++++++++++++-----
>> chardev/char-mux.c | 14 +++---
>> 3 files changed, 121 insertions(+), 19 deletions(-)
>>
>> diff --git a/include/chardev/char-fe.h b/include/chardev/char-fe.h
>> index aa1b864ccd..4051435a1c 100644
>> --- a/include/chardev/char-fe.h
>> +++ b/include/chardev/char-fe.h
>> @@ -84,6 +84,14 @@ bool qemu_chr_fe_backend_open(CharBackend *be);
>> * Set the front end char handlers. The front end takes the focus if
>> * any of the handler is non-NULL.
>> *
>> + * A chardev may have multiple main loop sources. In order to prevent
>> + * races when switching contexts, the function will temporarily block
>> + * the contexts before the source switch to prevent them from
>> + * dispatching in different threads concurrently.
>> + *
>> + * The current and the new @context must be acquirable or
>> + * running & dispatched in a loop (the function will hang otherwise).
>> + *
>> * Without associated Chardev, nothing is changed.
>> */
>> void qemu_chr_fe_set_handlers_full(CharBackend *b,
>> @@ -110,6 +118,21 @@ void qemu_chr_fe_set_handlers(CharBackend *b,
>> GMainContext *context,
>> bool set_open);
>>
>> +/**
>> + * qemu_chr_fe_set_handlers_internal:
>> + *
>> + * Same as qemu_chr_fe_set_handlers(), without context freezing.
>> + */
>> +void qemu_chr_fe_set_handlers_internal(CharBackend *b,
>> + IOCanReadHandler *fd_can_read,
>> + IOReadHandler *fd_read,
>> + IOEventHandler *fd_event,
>> + BackendChangeHandler *be_change,
>> + void *opaque,
>> + GMainContext *context,
>> + bool set_open,
>> + bool sync_state);
Can we add this function into a new header "chardev/char-internal.h"
(internal to chardev/) rather than "include/chardev/char-fe.h" (public)?
>> +
>> /**
>> * qemu_chr_fe_take_focus:
>> *
>> diff --git a/chardev/char-fe.c b/chardev/char-fe.c
>> index f3530a90e6..90cd7db007 100644
>> --- a/chardev/char-fe.c
>> +++ b/chardev/char-fe.c
>> @@ -246,15 +246,67 @@ void qemu_chr_fe_deinit(CharBackend *b, bool del)
>> }
>> }
>>
>> -void qemu_chr_fe_set_handlers_full(CharBackend *b,
>> - IOCanReadHandler *fd_can_read,
>> - IOReadHandler *fd_read,
>> - IOEventHandler *fd_event,
>> - BackendChangeHandler *be_change,
>> - void *opaque,
>> - GMainContext *context,
>> - bool set_open,
>> - bool sync_state)
>> +struct MainContextWait {
>> + QemuCond cond;
>> + QemuMutex lock;
>> +};
>> +
>> +static gboolean
>> +main_context_wait_cb(gpointer user_data)
>> +{
>> + struct MainContextWait *w = user_data;
>> +
>> + qemu_mutex_lock(&w->lock);
>> + qemu_cond_signal(&w->cond);
>> + /* wait until switching is over */
>> + qemu_cond_wait(&w->cond, &w->lock);
>
> Could previous signal() directly wake up itself here? Man
> pthread_cond_broadcast says:
>
> The pthread_cond_signal() function shall unblock at least one
> of the threads that are blocked on the specified condition
> variable cond (if any threads are blocked on cond).
>
> If more than one thread is blocked on a condition variable, the
> scheduling policy shall determine the order in which threads
> are unblocked.
>
> So AFAIU it could, because neither there's a restriction on ordering
> of how waiters are waked up, nor there's a limitation on how many
> waiters will be waked up by a single signal().
>
> Why not simply use two semaphores? Then locks can be avoided too.
>
> Regards,
>
next prev parent reply other threads:[~2019-02-22 8:33 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-20 16:06 [Qemu-devel] [PATCH 0/4] RFC: make chardev context switching safer Marc-André Lureau
2019-02-20 16:06 ` [Qemu-devel] [PATCH 1/4] iothread: wait until the glib context is acquired Marc-André Lureau
2019-02-21 7:59 ` Peter Xu
2019-02-21 10:39 ` Marc-André Lureau
2019-02-22 3:29 ` Peter Xu
2019-02-20 16:06 ` [Qemu-devel] [PATCH 2/4] chardev: make qemu_chr_fe_set_handlers() context switching safer Marc-André Lureau
2019-02-21 8:03 ` Peter Xu
2019-02-22 8:32 ` Philippe Mathieu-Daudé [this message]
2019-02-22 8:58 ` Marc-André Lureau
2019-02-22 8:56 ` Peter Xu
2019-02-20 16:06 ` [Qemu-devel] [PATCH 3/4] monitor: set the chardev context from the main context/thread Marc-André Lureau
2019-02-20 16:06 ` [Qemu-devel] [PATCH 4/4] char-socket: restart the reconnect timer to switch context Marc-André Lureau
2019-02-21 7:57 ` [Qemu-devel] [PATCH 0/4] RFC: make chardev context switching safer Peter Xu
2019-02-21 10:48 ` Marc-André Lureau
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=fbbb45f4-520c-430b-f4ed-19a9fccd2753@redhat.com \
--to=philmd@redhat.com \
--cc=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
/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).