Received: from eggs.gnu.org ([2001:4830:134:3::10]:47263) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V7XVs-0005gM-O3 for qemu-devel@nongnu.org; Thu, 08 Aug 2013 17:12:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V7XVn-0003AU-Cd for qemu-devel@nongnu.org; Thu, 08 Aug 2013 17:12:52 -0400 Received: from mail-ie0-x233.google.com ([2607:f8b0:4001:c03::233]:34038) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V7XVn-0003AQ-6C for qemu-devel@nongnu.org; Thu, 08 Aug 2013 17:12:47 -0400 Received: by mail-ie0-f179.google.com with SMTP id c11so2817259ieb.38 for ; Thu, 08 Aug 2013 14:12:46 -0700 (PDT) Sender: fluxion Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Roth In-Reply-To: <20130808210330.20939.61326@loki> References: <1375943171-1063-1-git-send-email-pingfank@linux.vnet.ibm.com> <1375943171-1063-2-git-send-email-pingfank@linux.vnet.ibm.com> <20130808210330.20939.61326@loki> Message-ID: <20130808211242.20939.22705@loki> Date: Thu, 08 Aug 2013 16:12:42 -0500 Subject: Re: [Qemu-devel] [PATCH v1 1/5] util: introduce gsource event abstraction List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Liu Ping Fan , qemu-devel@nongnu.org Cc: Paolo Bonzini , Stefan Hajnoczi Quoting Michael Roth (2013-08-08 16:03:30) > Quoting Liu Ping Fan (2013-08-08 01:26:07) > > Introduce struct EventsGSource. It will ease the usage of GSource > > associated with a group of files, which are dynamically allocated > > and release, ex, slirp. > > = > > Signed-off-by: Liu Ping Fan > > --- > > util/Makefile.objs | 1 + > > util/event_gsource.c | 94 ++++++++++++++++++++++++++++++++++++++++++++= ++++++++ > > util/event_gsource.h | 37 +++++++++++++++++++++ > > 3 files changed, 132 insertions(+) > > create mode 100644 util/event_gsource.c > > create mode 100644 util/event_gsource.h > > = > > diff --git a/util/Makefile.objs b/util/Makefile.objs > > index dc72ab0..eec55bd 100644 > > --- a/util/Makefile.objs > > +++ b/util/Makefile.objs > > @@ -11,3 +11,4 @@ util-obj-y +=3D iov.o aes.o qemu-config.o qemu-socket= s.o uri.o notify.o > > util-obj-y +=3D qemu-option.o qemu-progress.o > > util-obj-y +=3D hexdump.o > > util-obj-y +=3D crc32c.o > > +util-obj-y +=3D event_gsource.o > > diff --git a/util/event_gsource.c b/util/event_gsource.c > > new file mode 100644 > > index 0000000..4b9fa89 > > --- /dev/null > > +++ b/util/event_gsource.c > > @@ -0,0 +1,94 @@ > > +/* > > + * Copyright (C) 2013 IBM > > + * > > + * This program is free software; you can redistribute it and/or modi= fy > > + * it under the terms of the GNU General Public License as published = by > > + * the Free Software Foundation; under version 2 of the License. > > + * > > + * This program is distributed in the hope that it will be useful, > > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > > + * GNU General Public License for more details. > > + * > > + * You should have received a copy of the GNU General Public License > > + * along with this program; if not, see . > > + */ > > + > > +#include "event_gsource.h" > > +#include "qemu/bitops.h" > > + > > +GPollFD *events_source_add_pollfd(EventsGSource *src, int fd) > > +{ > > + GPollFD *retfd; > > + > > + retfd =3D g_slice_alloc(sizeof(GPollFD)); > > + retfd->events =3D 0; > > + retfd->fd =3D fd; > > + src->pollfds_list =3D g_list_append(src->pollfds_list, retfd); > = > I think moving to a GSource to simplify our mainloop implementation is > worthwhile even if we still rely on the global mutex and don't initially > support running those GSources outside the main iothread. But since being > able to eventually offload NetClient backends to seperate events loops to > support things like virtio-net dataplane is (I assume) still one of the > eventual goals, we should consider how to deal with concurrent > access to EventsGSource members. > = > For instance, In the case of slirp your dispatch callback may modify > src->pollfds_lists via > slirp_handler->tcp_input->socreate->events_source_add_pollfd(), while > another thread attempts to call socreate() via something like > net_slirp_hostfwd_add from the monitor (that's being driven by a separate > main loop). > = > So events_source_add_pollfd() and the various prepare/check/dispatch > functions should take a lock on pollfds_lists. > = > Dispatch is tricky though, since dispatch() invoke callbacks that may in > turn try to call events_source_add_pollfd(), as is the case above, in whi= ch > case you can deadlock. > = > I've been looking at the situation with regard to moving > qemu_set_fd_handler* to a GSource. > = > GLib has to deal with the same issue with regard to calls to > g_source_attach() while it's iterating through it's list of GSources. It > uses the GMainContext lock protect that list, and actually doesn't disall= ow > use of functions like g_source_attach() within a dispatch function. In > g_main_context_dispatch(), to work around the potential deadlock issue, it > actually builds up a separate list of dispatch cb functions and callback = data, > then drops the GMainContext lock before iterating through that list and > calling the dispatch cb functions for all the GSources that fired. > This new list it builds up is safe from concurrent modification since > only the main loop thread can access it. > = > AFAIK there's 3 ways to deal with this type of concurrency: > = > a) Move to a 1-to-1 mapping between GPollFDs and EventGSources: we can th= en > let GLib handle managing our list of GPollFDs for us. We may still nee= d a > mutex for other members of EventsGSource, but that lock can be taken f= rom > within our prepare/check/dispatch functions and held for the duration = of > the calls without any strange deadlock issues. I take that back, would still need to drop EventsGSource mutex, in EventsGSource->dispatch prior calling the dispatch cb, but this is trivial unless the dispatch cb or associated user_data can be modified, which isn't the case with this interface.