libxl: New event API
v8 cover libxl: New event API
There are a couple of hopefully uncontroversial new patches on the front of this series:
01/11 .gitignore/.hgignore: New names for ioemu dirs, seabios 02/11 xl: fix a couple of memory leaks
The remainder of the series is very similar to previous versions.
- Changes have been made as discussed on the list, plus:
- One memory leak (missing GC_FREE) is fixed;
- I have moved a some of the destruction in xl's domain_create to reduce false positives from valgrind;
- Fixed the ocaml stubs to pass the new ao argument (currently these stubs are synchronous, which is not ideal but it's not clear what an ocaml caller would want).
03/11 libxl: New API for providing OS events to libxl 04/11 ocaml, libxl: support "private" fields
05/11 libxl: New event generation API
06/11 libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec 07/11 libxl: Permit multithreaded event waiting
08/11 libxl: Asynchronous/long-running operation infrastructure 09/11 libxl: New convenience macro CONTAINER_OF
10/11 libxl: Introduce libxl__ev_devstate
11/11 libxl: Convert to asynchronous: device removal
The series has also been rebased and retested.
PATCH v8 1/11 .gitignore/.hgignore: New names for ioemu dirs, seabios
* Add new seabios clone directories to .gitignore.
* Add new qemu clone directories to .gitignore.
* Remove old tools/ioemu (long-obsolete) from .gitignore and .hgignore.
--- .gitignore | 27 +++++++++------------------ .hgignore | 14 -------------- 2 files changed, 9 insertions(+), 32 deletions(-)
.gitignore +9 -18
@@ -15,8 +15,6 @@ TAGS .config dist -tools/ioemu-dir -tools/ioemu-remote stubdom/*.tar.gz build-*
@@ -162,20 +160,6 @@ tools/hotplug/common/hotplugpath.sh tools/include/xen/* tools/include/xen-foreign/*.(c|h|size) tools/include/xen-foreign/checker -tools/ioemu/.pc/* -tools/ioemu/config-host.h -tools/ioemu/config-host.mak -tools/ioemu/i386-dm/Makefile -tools/ioemu/i386-dm/config.h -tools/ioemu/i386-dm/config.mak -tools/ioemu/i386-dm/qemu-dm -tools/ioemu/qemu-doc.html -tools/ioemu/qemu-img.1 -tools/ioemu/qemu-img.pod -tools/ioemu/qemu-tech.html -tools/ioemu/qemu.1 -tools/ioemu/qemu.pod -tools/ioemu/tapdisk-ioemu tools/libxc/ia64/asm/*.h tools/libxc/ia64/acpi/*.h tools/libxc/ia64/acpi/platform/*.h
@@ -285,8 +269,6 @@ tools/xm-test/*/Makefile(.in)* tools/xm-test/lib/XmTestLib/config.py tools/xm-test/lib/XmTestReport/xmtest.py tools/xm-test/tests/*.test -tools/ioemu-remote -tools/ioemu-dir tools/ocaml-xenstored* xen/.banner* xen/BLOG
@@ -325,6 +307,15 @@ unmodified_drivers/linux-2.6/*.ko unmodified_drivers/linux-2.6/*.mod.c LibVNCServer* +tools/qemu-xen-dir-remote +tools/qemu-xen-dir + +tools/qemu-xen-traditional-dir-remote +tools/qemu-xen-traditional-dir + +tools/firmware/seabios-dir-remote +tools/firmware/seabios-dir + tools/firmware/rombios/_rombios_.c tools/firmware/rombios/rombios.s tools/firmware/rombios/rombios.sym
.hgignore +0 -14
@@ -163,20 +163,6 @@ ^tools/include/xen/.*$ ^tools/include/xen-foreign/.*\.(c|h|size)$ ^tools/include/xen-foreign/checker$ -^tools/ioemu/\.pc/.*$ -^tools/ioemu/config-host\.h$ -^tools/ioemu/config-host\.mak$ -^tools/ioemu/i386-dm/Makefile$ -^tools/ioemu/i386-dm/config\.h$ -^tools/ioemu/i386-dm/config\.mak$ -^tools/ioemu/i386-dm/qemu-dm$ -^tools/ioemu/qemu-doc\.html$ -^tools/ioemu/qemu-img\.1$ -^tools/ioemu/qemu-img\.pod$ -^tools/ioemu/qemu-tech\.html$ -^tools/ioemu/qemu\.1$ -^tools/ioemu/qemu\.pod$ -^tools/ioemu/tapdisk-ioemu$ ^tools/libxc/ia64/asm/.*\.h$ ^tools/libxc/ia64/acpi/.*\.h$ ^tools/libxc/ia64/acpi/platform/.*\.h$
Signature · 1 line
1.7.2.5
Re: [PATCH 01/11] .gitignore/.hgignore: New names for ioemu dirs, seabios
Quoted 5 lines · Ian Jackson
* Add new seabios clone directories to .gitignore.
* Add new qemu clone directories to .gitignore.
* Remove oldtools/ioemu(long-obsolete) from .gitignore and .hgignore.Signed-off-by Ian Jackson <[email protected]>
Hardly seems worth it but:
PATCH v8 2/11 xl: fix a couple of memory leaks
In reply to Ian Jackson* dolog leaked the log message (!)
* main() leaked the config_data (perhaps a false positive from valgrind, but it's nicer to tidy it up).
--- tools/libxl/xl.c | 1 + tools/libxl/xl_cmdimpl.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletions(-)
tools/libxl/xl.c +1 -0
@@ -135,6 +135,7 @@ int main(int argc, char **argv) config_file, strerror(errno));parse_global_config (config_file, config_data, config_len);free (config_file); +free (config_data);/* Reset options for per-command use of getopt. */ argv += optind;
tools/libxl/xl_cmdimpl.c +2 -1
@@ -278,7 +278,7 @@ static void dolog(const char *file, int line, const char *func, char *fmt, ...)static void dolog (const char *file,int line,const char *func,char *fmt, ...) {va_list ap; -char *s; +char *s =NULL ;int rc;va_start (ap, fmt);
@@ -286,6 +286,7 @@ static void dolog(const char *file, int line, const char *func, char *fmt, ...)va_end (ap);if (rc >=0 )libxl_write_exactly (NULL , logfile, s, rc,NULL ,NULL ); +free (s); } static void printf_info(int domid,
Signature · 1 line
1.7.2.5
Re: [PATCH 02/11] xl: fix a couple of memory leaks
Quoted 6 lines · Ian Jackson
* dolog leaked the log message (!)
*
main()leaked theconfig_data(perhaps a false positive from valgrind, but it's nicer to tidy it up).Signed-off-by Ian Jackson <[email protected]>
Quoted 38 lines
--- tools/libxl/xl.c | 1 + tools/libxl/xl_cmdimpl.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletions(-)tools/libxl/xl.c +1
-0@@ -135,6 +135,7 @@ int main(int argc, char **argv) config_file, strerror(errno));parse_global_config (config_file, config_data, config_len);free (config_file); +free (config_data);/* Reset options for per-command use of getopt. */ argv += optind;tools/libxl/xl_cmdimpl.c +2
-1@@ -278,7 +278,7 @@ static void dolog(const char *file, int line, const char *func, char *fmt, ...)static void dolog (const char *file,int line,const char *func,char *fmt, ...) {va_list ap; -char *s; +char *s =NULL ;int rc;va_start (ap, fmt);@@ -286,6 +286,7 @@ static void dolog(const char *file, int line, const char *func, char *fmt, ...)va_end (ap);if (rc >=0 )libxl_write_exactly (NULL , logfile, s, rc,NULL ,NULL ); +free (s); } static void printf_info(int domid,
PATCH v8 3/11 libxl: New API for providing OS events to libxl
In reply to Ian JacksonWe provide a new set of functions and related structures
libxl_osevent_*
which are to be used by event-driven applications to receive information from libxl about which fds libxl is interested in, and what timeouts libxl is waiting for, and to pass back to libxl information about which fds are readable/writeable etc., and which timeouts have occurred. Ie, low-level events.
In this patch, this new machinery is still all unused. Callers will appear in the next patch in the series, which introduces a new API for applications to receive high-level events about actual domains etc.
--- tools/libxl/Makefile | 2 +- tools/libxl/libxl.c | 30 ++ tools/libxl/libxl.h | 6 + tools/libxl/libxl_event.c | 750 ++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_event.h | 205 ++++++++++++ tools/libxl/libxl_internal.h | 277 +++++++++++++++- 6 files changed, 1267 insertions(+), 3 deletions(-) create mode 100644 tools/libxl/libxl_event.c create mode 100644 tools/libxl/libxl_event.h
tools/libxl/Makefile +1 -1
@@ -49,7 +49,7 @@ LIBXL_LIBS += -lyajl LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \ libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \ libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \ - libxl_qmp.o $(LIBXL_OBJS-y) + libxl_qmp.o libxl_event.o $(LIBXL_OBJS-y) LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
tools/libxl/libxl.c +30 -0
@@ -45,6 +45,16 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,* only as an initialiser, not as an expression. */ memcpy (&ctx->lock, &mutex_value,sizeof (ctx->lock)); + ctx->osevent_hooks =0 ; + + ctx->fd_rindex =0 ; +LIBXL_LIST_INIT (&ctx->efds); +LIBXL_TAILQ_INIT (&ctx->etimes); + + ctx->watch_slots =0 ; +LIBXL_SLIST_INIT (&ctx->watch_freeslots); +libxl__ev_fd_init (&ctx->watch_efd); +if (stat (XENSTORE_PID_FILE , &stat_buf) !=0 ) {LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"Is xenstore daemon running?\n" "failed to stat %s" ,XENSTORE_PID_FILE );
@@ -79,9 +89,29 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,int libxl_ctx_free (libxl_ctx *ctx) {if (!ctx)return 0 ; + +int i; +GC_INIT (ctx); + +/* Deregister all libxl__ev_KINDs: */ + +for (i =0 ; i < ctx->watch_nslots; i++) +assert (!libxl__watch_slot_contents (gc, i)); +libxl__ev_fd_deregister (gc, &ctx->watch_efd); + +/* Now there should be no more events requested from the application: */ + +assert (LIBXL_LIST_EMPTY (&ctx->efds)); +assert (LIBXL_TAILQ_EMPTY (&ctx->etimes)); +if (ctx->xch)xc_interface_close (ctx->xch);libxl_version_info_dispose (&ctx->version_info);if (ctx->xsh)xs_daemon_close (ctx->xsh); + +free (ctx->fd_rindex); +free (ctx->watch_slots); + +GC_FREE ;free (ctx);return 0 ; }
tools/libxl/libxl.h +6 -0
@@ -137,6 +137,7 @@#include <xen/sysctl.h> #include <libxl_uuid.h> +#include <_libxl_list.h> typedef uint8_t libxl_mac [6 ];#define LIBXL_MAC_FMT "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx"
@@ -223,6 +224,9 @@ enum {ERROR_GUEST_TIMEDOUT =-8 ,ERROR_TIMEDOUT =-9 ,ERROR_NOPARAVIRT =-10 , +ERROR_NOT_READY =-11 , +ERROR_OSEVENT_REG_FAIL =-12 , + ERROR_BUFFERFULL = -13, };#define LIBXL_VERSION 0
@@ -648,6 +652,8 @@ const char *libxl_xenpaging_dir_path(void);/* misc */ int libxl_fd_set_cloexec (int fd); +#include <libxl_event.h> +#endif /* LIBXL_H */ /*
tools/libxl/libxl_event.c new file +750 -0
750 lines not on this page · raw message
tools/libxl/libxl_event.h new file +205 -0
205 lines not on this page · raw message
tools/libxl/libxl_internal.h +275 -2
323 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH v8 4/11 ocaml, libxl: support "private" fields
In reply to Ian JacksonThe changeset
24378:b4365e2c2595 libxl: idl: support new "private" type attribute
is not complete. Actually using this feature does not work because the ocaml idl generator does not know about it.
So add that support.
--- tools/ocaml/libs/xl/genwrap.py | 8 ++++++++ 1 files changed, 8 insertions(+), 0 deletions(-)
tools/ocaml/libs/xl/genwrap.py +8 -0
@@ -93,6 +93,8 @@ def gen_ocaml_ml(ty, interface, indent=""): s += "\t{\n" for f in ty.fields: + if f.type.private: + continue x = ocaml_instance_of(f.type, f.name) x = x.replace("\n", "\n\t\t") s += "\t\t" + x + ";\n"
@@ -148,6 +150,8 @@ def c_val(ty, c, o, indent="", parent = None): elif isinstance(ty, libxltypes.Aggregate) and (parent is None): n = 0 for f in ty.fields: + if f.type.private: + continue s += "%s\n" % c_val(f.type, "%s->%s" % (c, f.name), "Field(%s, %d)" % (o,n), parent="%s->" % (c)) n = n + 1 else:
@@ -212,6 +216,8 @@ def ocaml_Val(ty, o, c, indent="", parent = None): n = 0 for f in ty.fields: + if f.type.private: + continue s += "\n" s += "\t%s\n" % ocaml_Val(f.type, "%s_field" % ty.rawname, "%s->%s" % (c,f.name), parent="%s->" % c) s += "\tStore_field(%s, %d, %s);\n" % (o, n, "%s_field" % ty.rawname)
@@ -290,6 +296,8 @@ if __name__ == '__main__': cinc.write(autogen_header("/*", "*/")) for ty in types: + if ty.private: + continue #sys.stdout.write(" TYPE %-20s " % ty.rawname) ml.write(gen_ocaml_ml(ty, False)) ml.write("\n")
Signature · 1 line
1.7.2.5
Re: [PATCH 04/11] ocaml, libxl: support "private" fields
Quoted 8 lines · Ian Jackson
The changeset
24378:b4365e2c2595 libxl: idl: support new "private" type attribute
is not complete. Actually using this feature does not work because the ocaml idl generator does not know about it.So add that support.
Signed-off-by Ian Jackson <[email protected]>
I acked this one last time round.
Ian.
Quoted 44 lines
--- tools/ocaml/libs/xl/genwrap.py | 8 ++++++++ 1 files changed, 8 insertions(+), 0 deletions(-)tools/ocaml/libs/xl/genwrap.py +8
-0@@ -93,6 +93,8 @@ def gen_ocaml_ml(ty, interface, indent=""): s += "\t{\n" for f in ty.fields: + if f.type.private: + continue x = ocaml_instance_of(f.type, f.name) x = x.replace("\n", "\n\t\t") s += "\t\t" + x + ";\n"@@ -148,6 +150,8 @@ def c_val(ty, c, o, indent="", parent = None): elif isinstance(ty, libxltypes.Aggregate) and (parent is None): n = 0 for f in ty.fields: + if f.type.private: + continue s += "%s\n" % c_val(f.type, "%s->%s" % (c, f.name), "Field(%s, %d)" % (o,n), parent="%s->" % (c)) n = n + 1 else:@@ -212,6 +216,8 @@ def ocaml_Val(ty, o, c, indent="", parent = None): n = 0 for f in ty.fields: + if f.type.private: + continue s += "\n" s += "\t%s\n" % ocaml_Val(f.type, "%s_field" % ty.rawname, "%s->%s" % (c,f.name), parent="%s->" % c) s += "\tStore_field(%s, %d, %s);\n" % (o, n, "%s_field" % ty.rawname)@@ -290,6 +296,8 @@ if __name__ == '__main__': cinc.write(autogen_header("/*", "*/")) for ty in types: + if ty.private: + continue #sys.stdout.write(" TYPE %-20s " % ty.rawname) ml.write(gen_ocaml_ml(ty, False)) ml.write("\n")
PATCH v8 5/11 libxl: New event generation API
In reply to Ian JacksonReplace the existing API for retrieving high-level events (events about domains, etc.) from libxl with a new one.
This changes the definition and semantics of the `libxl_event' structure, and replaces the calls for obtaining information about domain death and disk eject events.
This is an incompatible change, sorry. The alternative was to try to provide both the previous horrid API and the new one, and would also involve never using the name `libxl_event' for the new interface.
The new "libxl_event" structure is blacklisted in the ocaml bindings for two reasons:
- It has a field name "type" (which is a keyword in ocaml); the ocaml idl generator should massage this field name on output, to "type_" perhaps.
- The ocaml idl generator does not support KeyedUnion.
--- tools/libxl/libxl.c | 328 +++++++++++++++++++++++++++++----------- tools/libxl/libxl.h | 55 +------ tools/libxl/libxl_event.c | 236 ++++++++++++++++++++++++++--- tools/libxl/libxl_event.h | 183 ++++++++++++++++++++++- tools/libxl/libxl_internal.h | 77 +++++++++- tools/libxl/libxl_types.idl | 34 ++++- tools/libxl/xl_cmdimpl.c | 289 +++++++++++++++++++++-------------- tools/ocaml/libs/xl/genwrap.py | 1 + 8 files changed, 927 insertions(+), 276 deletions(-)
tools/libxl/libxl.c +241 -87
@@ -45,8 +45,11 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,* only as an initialiser, not as an expression. */ memcpy (&ctx->lock, &mutex_value,sizeof (ctx->lock)); +LIBXL_TAILQ_INIT (&ctx->occurred); + ctx->osevent_hooks =0 ; + ctx->fd_polls =0 ; ctx->fd_rindex =0 ;LIBXL_LIST_INIT (&ctx->efds);LIBXL_TAILQ_INIT (&ctx->etimes);
@@ -55,6 +58,9 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,LIBXL_SLIST_INIT (&ctx->watch_freeslots);libxl__ev_fd_init (&ctx->watch_efd); +LIBXL_TAILQ_INIT (&ctx->death_list); +libxl__ev_xswatch_init (&ctx->death_watch); +if (stat (XENSTORE_PID_FILE , &stat_buf) !=0 ) {LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"Is xenstore daemon running?\n" "failed to stat %s" ,XENSTORE_PID_FILE );
@@ -86,6 +92,20 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,return rc; } +static void free_disable_deaths (libxl__gc *gc, +struct libxl__evgen_domain_death_list *l) { +libxl_evgen_domain_death *death; +while ((death =LIBXL_TAILQ_FIRST (l))) +libxl__evdisable_domain_death (gc, death); +} + +static void discard_events (struct libxl__event_list *l) { +/* doesn't bother unlinking from the list, so l is corrupt on return */ +libxl_event *ev; + LIBXL_TAILQ_FOREACH(ev, l, link) +libxl_event_free (0 , ev); +} +int libxl_ctx_free (libxl_ctx *ctx) {if (!ctx)return 0 ;
@@ -95,6 +115,13 @@ int libxl_ctx_free(libxl_ctx *ctx)/* Deregister all libxl__ev_KINDs: */ +free_disable_deaths (gc, &CTX ->death_list); +free_disable_deaths (gc, &CTX ->death_reported); + +libxl_evgen_disk_eject *eject; +while ((eject =LIBXL_LIST_FIRST (&CTX ->disk_eject_evgens))) +libxl__evdisable_disk_eject (gc, eject); +for (i =0 ; i < ctx->watch_nslots; i++)assert (!libxl__watch_slot_contents (gc, i));libxl__ev_fd_deregister (gc, &ctx->watch_efd);
@@ -108,9 +135,12 @@ int libxl_ctx_free(libxl_ctx *ctx)libxl_version_info_dispose (&ctx->version_info);if (ctx->xsh)xs_daemon_close (ctx->xsh); +free (ctx->fd_polls);free (ctx->fd_rindex);free (ctx->watch_slots); +discard_events (&ctx->occurred); +GC_FREE ;free (ctx);return 0 ;
@@ -646,117 +676,177 @@ int libxl_domain_reboot(libxl_ctx *ctx, uint32_t domid)return ret; } -int libxl_get_wait_fd (libxl_ctx *ctx,int *fd) -{ - *fd =xs_fileno (ctx->xsh); -return 0 ; -} +static void domain_death_xswatch_callback (libxl__egc *egc,libxl__ev_xswatch *w, +const char *wpath,const char *epath) { +EGC_GC ; +libxl_evgen_domain_death *evg; +uint32_t domid; +int rc; -int libxl_wait_for_domain_death (libxl_ctx *ctx,uint32_t domid,libxl_waiter *waiter) -{ - waiter->path =strdup ("@releaseDomain" ); -if (asprintf (&(waiter->token),"%d" ,LIBXL_EVENT_TYPE_DOMAIN_DEATH ) <0 ) -return -1 ; -if (!xs_watch (ctx->xsh, waiter->path, waiter->token)) -return -1 ; -return 0 ; +CTX_LOCK ; + + evg =LIBXL_TAILQ_FIRST (&CTX ->death_list); +if (!evg) goto out; + + domid = evg->domid; + +for (;;) { +int nentries =LIBXL_TAILQ_NEXT (evg, entry) ?200 :1 ; +xc_domaininfo_t domaininfos[nentries]; +const xc_domaininfo_t *got = domaininfos, *gotend; + + rc =xc_domain_getinfolist (CTX ->xch, domid, nentries, domaininfos); +if (rc ==-1 ) { +LIBXL__EVENT_DISASTER (egc,"xc_domain_getinfolist failed while" +" processing @releaseDomain watch event" , + errno,0 ); + goto out; + } + gotend = &domaininfos[rc]; + +for (;;) { +if (!evg) + goto all_reported; + +if (!rc || got->domain > evg->domid) { +/* ie, the list doesn't contain evg->domid any more so +* the domain has been destroyed */ +libxl_evgen_domain_death *evg_next; + +libxl_event *ev =NEW_EVENT (egc,DOMAIN_DESTROY , evg->domid); +if (!ev) goto out; + +libxl__event_occurred (egc, ev); + + evg->death_reported =1 ; + evg_next =LIBXL_TAILQ_NEXT (evg, entry); +LIBXL_TAILQ_REMOVE (&CTX ->death_list, evg, entry); +LIBXL_TAILQ_INSERT_HEAD (&CTX ->death_reported, evg, entry); + evg = evg_next; + +continue ; + } + +if (got == gotend) +break ; + +if (got->domain < evg->domid) { + got++; +continue ; + } + +assert (evg->domid == got->domain); + +if (!evg->shutdown_reported && + (got->flags & XEN_DOMINF_shutdown)) { +libxl_event *ev =NEW_EVENT (egc,DOMAIN_SHUTDOWN , got->domain); +if (!ev) goto out; + + ev->u.domain_shutdown.shutdown_reason = + (got->flags >> XEN_DOMINF_shutdownshift) & + XEN_DOMINF_shutdownmask; +libxl__event_occurred (egc, ev); + + evg->shutdown_reported =1 ; + } + evg =LIBXL_TAILQ_NEXT (evg, entry); + } + +assert (rc);/* rc==0 results in us eating all evgs and quitting */ + domid = gotend[-1 ].domain; + } + all_reported: + out: + +CTX_UNLOCK ; } -int libxl_wait_for_disk_ejects (libxl_ctx *ctx,uint32_t guest_domid,libxl_device_disk *disks,int num_disks,libxl_waiter *waiter) -{ +int libxl_evenable_domain_death (libxl_ctx *ctx,uint32_t domid, +libxl_ev_user user,libxl_evgen_domain_death **evgen_out) {GC_INIT (ctx); -int i, rc =-1 ; -uint32_t domid =libxl_get_stubdom_id (ctx, guest_domid); +libxl_evgen_domain_death *evg, *evg_search; +int rc; + +CTX_LOCK ; -if (!domid) - domid = guest_domid; + evg =malloc (sizeof (*evg));if (!evg) { rc =ERROR_NOMEM ; goto out; } +memset (evg,0 ,sizeof (*evg)); + evg->domid = domid; + evg->user = user; -for (i =0 ; i < num_disks; i++) { -if (asprintf (&(waiter[i].path),"%s/device/vbd/%d/eject" , -libxl__xs_get_dompath (gc, domid), -libxl__device_disk_dev_number (disks[i].vdev, -NULL ,NULL )) <0 ) - goto out; -if (asprintf (&(waiter[i].token),"%d" ,LIBXL_EVENT_TYPE_DISK_EJECT ) <0 ) - goto out; -xs_watch (ctx->xsh, waiter[i].path, waiter[i].token); + LIBXL_TAILQ_INSERT_SORTED(&ctx->death_list, entry, evg, evg_search, , + evg->domid > evg_search->domid); + +if (!libxl__ev_xswatch_isregistered (&ctx->death_watch)) { + rc =libxl__ev_xswatch_register (gc, &ctx->death_watch, + domain_death_xswatch_callback,"@releaseDomain" ); +if (rc) {libxl__evdisable_domain_death (gc, evg); goto out; } } + + *evgen_out = evg; rc =0 ; -out: + + out: +CTX_UNLOCK ;GC_FREE ;return rc; -} +}; -int libxl_get_event (libxl_ctx *ctx,libxl_event *event) -{ -unsigned int num; -char **events =xs_read_watch (ctx->xsh, &num); -if (num !=2 ) { -free (events); -return ERROR_FAIL ; - } - event->path =strdup (events[XS_WATCH_PATH ]); - event->token =strdup (events[XS_WATCH_TOKEN ]); - event->type =atoi (event->token); -free (events); -return 0 ; -} +void libxl__evdisable_domain_death (libxl__gc *gc, +libxl_evgen_domain_death *evg) { +CTX_LOCK ; -int libxl_stop_waiting (libxl_ctx *ctx,libxl_waiter *waiter) -{ -if (!xs_unwatch (ctx->xsh, waiter->path, waiter->token)) -return ERROR_FAIL ; +if (!evg->death_reported) +LIBXL_TAILQ_REMOVE (&CTX ->death_list, evg, entry);else -return 0 ; -} +LIBXL_TAILQ_REMOVE (&CTX ->death_reported, evg, entry); -int libxl_free_event (libxl_event *event) -{ -free (event->path); -free (event->token); -return 0 ; -} +free (evg); -int libxl_free_waiter (libxl_waiter *waiter) -{ -free (waiter->path); -free (waiter->token); -return 0 ; -} - -int libxl_event_get_domain_death_info (libxl_ctx *ctx,uint32_t domid,libxl_event *event,libxl_dominfo *info) -{ -if (libxl_domain_info (ctx, info, domid) <0 ) -return 0 ; +if (!LIBXL_TAILQ_FIRST (&CTX ->death_list) && +libxl__ev_xswatch_isregistered (&CTX ->death_watch)) +libxl__ev_xswatch_deregister (gc, &CTX ->death_watch); -if (info->running || (!info->shutdown && !info->dying)) -return ERROR_INVAL ; - -return 1 ; +CTX_UNLOCK ; } -int libxl_event_get_disk_eject_info (libxl_ctx *ctx,uint32_t domid,libxl_event *event,libxl_device_disk *disk) -{ +void libxl_evdisable_domain_death (libxl_ctx *ctx, +libxl_evgen_domain_death *evg) {GC_INIT (ctx); -char *path; +libxl__evdisable_domain_death (gc, evg); +GC_FREE ; +} + +static void disk_eject_xswatch_callback (libxl__egc *egc,libxl__ev_xswatch *w, +const char *wpath,const char *epath) { +EGC_GC ; +libxl_evgen_disk_eject *evg = (void *)w;char *backend;char *value;char backend_type[BACKEND_STRING_SIZE +1 ]; - value =libxl__xs_read (gc,XBT_NULL , event->path); + value =libxl__xs_read (gc,XBT_NULL , wpath); -if (!value ||strcmp (value,"eject" )) { -GC_FREE ; -return 0 ; +if (!value ||strcmp (value,"eject" )) +return ; + +if (libxl__xs_write (gc,XBT_NULL , wpath,"" )) { +LIBXL__EVENT_DISASTER (egc,"xs_write failed acknowledging eject" , + errno,LIBXL_EVENT_TYPE_DISK_EJECT ); +return ; } - path =strdup (event->path); - path[strlen (path) -6 ] ='\0' ; - backend =libxl__xs_read (gc,XBT_NULL ,libxl__sprintf (gc,"%s/backend" , path)); +libxl_event *ev =NEW_EVENT (egc,DISK_EJECT , evg->domid); +libxl_device_disk *disk = &ev->u.disk_eject.disk; + + backend =libxl__xs_read (gc,XBT_NULL , +libxl__sprintf (gc,"%.*s/backend" , + (int )strlen (wpath)-6 , wpath));sscanf (backend, - "/local/domain/%d/backend/%" TOSTRING(BACKEND_STRING_SIZE) "[a-z]/%*d/%*d", - &disk->backend_domid, backend_type); + "/local/domain/%d/backend/%" TOSTRING(BACKEND_STRING_SIZE) +"[a-z]/%*d/%*d" , + &disk->backend_domid, backend_type);if (!strcmp (backend_type,"tap" ) || !strcmp (backend_type,"vbd" )) { disk->backend =LIBXL_DISK_BACKEND_TAP ; }else if (!strcmp (backend_type,"qdisk" )) {
@@ -765,19 +855,83 @@ int libxl_event_get_disk_eject_info(libxl_ctx *ctx, uint32_t domid, libxl_event disk->backend =LIBXL_DISK_BACKEND_UNKNOWN ; } - disk->pdev_path =strdup ("" ); + disk->pdev_path =strdup ("" );/* xxx fixme malloc failure */ disk->format =LIBXL_DISK_FORMAT_EMPTY ;/* this value is returned to the user: do not free right away */ - disk->vdev =xs_read (ctx->xsh,XBT_NULL ,libxl__sprintf (gc,"%s/dev" , backend),NULL ); + disk->vdev =xs_read (CTX ->xsh,XBT_NULL , +libxl__sprintf (gc,"%s/dev" , backend),NULL ); disk->removable =1 ; disk->readwrite =0 ; disk->is_cdrom =1 ; -free (path); +libxl__event_occurred (egc, ev); +} + +int libxl_evenable_disk_eject (libxl_ctx *ctx,uint32_t guest_domid, +const char *vdev,libxl_ev_user user, +libxl_evgen_disk_eject **evgen_out) { +GC_INIT (ctx); +CTX_LOCK ; +int rc; +char *path; +libxl_evgen_disk_eject *evg =NULL ; + + evg =malloc (sizeof (*evg));if (!evg) { rc =ERROR_NOMEM ; goto out; } +memset (evg,0 ,sizeof (*evg)); + evg->user = user; + evg->domid = guest_domid; +LIBXL_LIST_INSERT_HEAD (&CTX ->disk_eject_evgens, evg, entry); + + evg->vdev =strdup (vdev); +if (!evg->vdev) { rc =ERROR_NOMEM ; goto out; } + +uint32_t domid =libxl_get_stubdom_id (ctx, guest_domid); + +if (!domid) + domid = guest_domid; + + path =libxl__sprintf (gc,"%s/device/vbd/%d/eject" , +libxl__xs_get_dompath (gc, domid), +libxl__device_disk_dev_number (vdev,NULL ,NULL )); +if (!path) { rc =ERROR_NOMEM ; goto out; } + + rc =libxl__ev_xswatch_register (gc, &evg->watch, + disk_eject_xswatch_callback, path); +if (rc) goto out; + + *evgen_out = evg; +CTX_UNLOCK ;GC_FREE ; -return 1 ; +return 0 ; + + out: +if (evg) +libxl__evdisable_disk_eject (gc, evg); +CTX_UNLOCK ; +GC_FREE ; +return rc; +} + +void libxl__evdisable_disk_eject (libxl__gc *gc,libxl_evgen_disk_eject *evg) { +CTX_LOCK ; + +LIBXL_LIST_REMOVE (evg, entry); + +if (libxl__ev_xswatch_isregistered (&evg->watch)) +libxl__ev_xswatch_deregister (gc, &evg->watch); + +free (evg->vdev); +free (evg); + +CTX_UNLOCK ; } +void libxl_evdisable_disk_eject (libxl_ctx *ctx,libxl_evgen_disk_eject *evg) { +GC_INIT (ctx); +libxl__evdisable_disk_eject (gc, evg); +GC_FREE ; +} +int libxl_domain_destroy (libxl_ctx *ctx,uint32_t domid) {GC_INIT (ctx);
tools/libxl/libxl.h +9 -46
79 lines not on this page · raw message
tools/libxl/libxl_event.c +218 -18
287 lines not on this page · raw message
tools/libxl/libxl_event.h +179 -4
195 lines not on this page · raw message
tools/libxl/libxl_internal.h +74 -3
133 lines not on this page · raw message
tools/libxl/libxl_types.idl +29 -5
@@ -75,11 +75,6 @@ libxl_action_on_shutdown = Enumeration("action_on_shutdown", [ (6, "COREDUMP_RESTART"), ]) -libxl_event_type = Enumeration("event_type", [ - (1, "DOMAIN_DEATH"), - (2, "DISK_EJECT"), - ]) - libxl_button = Enumeration("button", [ (1, "POWER"), (2, "SLEEP"),
@@ -395,3 +390,32 @@ libxl_sched_sedf = Struct("sched_sedf", [ ("extratime", integer), ("weight", integer), ], dispose_fn=None) + +libxl_event_type = Enumeration("event_type", [ + (1, "DOMAIN_SHUTDOWN"), + (2, "DOMAIN_DESTROY"), + (3, "DISK_EJECT"), + ]) + +libxl_ev_user = UInt(64) + +libxl_ev_link = Builtin("ev_link", passby=PASS_BY_REFERENCE, private=True) + +libxl_event = Struct("event",[ + ("link", libxl_ev_link), + # for use by libxl; caller may use this once the event has been + # returned by libxl_event_{check,wait} + ("domid", libxl_domid), + ("domuuid", libxl_uuid), + ("for_user", libxl_ev_user), + ("type", libxl_event_type), + ("u", KeyedUnion(None, libxl_event_type, "type", + [("domain_shutdown", Struct(None, [ + ("shutdown_reason", uint8), + ])), + ("domain_destroy", Struct(None, [])), + ("disk_eject", Struct(None, [ + ("vdev", string), + ("disk", libxl_device_disk), + ])), + ]))])
tools/libxl/xl_cmdimpl.c +176 -113
379 lines not on this page · raw message
tools/ocaml/libs/xl/genwrap.py +1 -0
@@ -275,6 +275,7 @@ if __name__ == '__main__': "device_model_info", "vcpuinfo", "topologyinfo", + "event", ] for t in blacklist:
Signature · 1 line
1.7.2.5
PATCH v8 6/11 libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec
In reply to Ian JacksonWe want a function for setting fds to nonblocking, so introduce one.
This is a very similar requirement to that for libxl_fd_set_cloexec, so make it common with that.
While we're at it, fix a few deficiences that make this latter function less desirable than it could be:
* Change the return from 0/-1 (like a syscall) to a libxl error code
* Take a boolean parameter for turning the flag on and off
* Log on error (and so, take a ctx for this purpose)
Change callers of libxl_fd_set_cloexec to notice errors. (Although, such errors are highly unlikely.)
--- tools/libxl/libxl.c | 33 ++++++++++++++++++++++++++------- tools/libxl/libxl.h | 7 ++++++- tools/libxl/libxl_qmp.c | 3 ++- tools/libxl/xl_cmdimpl.c | 3 ++- 4 files changed, 36 insertions(+), 10 deletions(-)
tools/libxl/libxl.c +26 -7
@@ -3656,19 +3656,38 @@ int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)return 0 ; } -int libxl_fd_set_cloexec (int fd) +static int fd_set_flags (libxl_ctx *ctx,int fd, +int fcntlgetop,int fcntlsetop,const char *fl, +int flagmask,int set_p) { -int flags =0 ; +int flags, r; -if ((flags =fcntl (fd,F_GETFD )) ==-1 ) { - flags =0 ; + flags =fcntl (fd, fcntlgetop); +if (flags ==-1 ) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"fcntl(,F_GET%s) failed" ,fl); +return ERROR_FAIL ; } -if ((flags &FD_CLOEXEC )) { -return 0 ; + +if (set_p) + flags |= flagmask; +else + flags &= ~flagmask; + + r =fcntl (fd, fcntlsetop, flags); +if (r ==-1 ) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"fcntl(,F_SET%s) failed" ,fl); +return ERROR_FAIL ; } -return fcntl (fd,F_SETFD , flags |FD_CLOEXEC ); + +return 0 ; } +int libxl_fd_set_cloexec (libxl_ctx *ctx,int fd,int cloexec) + {return fd_set_flags (ctx,fd,F_GETFD ,F_SETFD ,"FD" ,FD_CLOEXEC , cloexec); } + +int libxl_fd_set_nonblock (libxl_ctx *ctx,int fd,int nonblock) + {return fd_set_flags (ctx,fd,F_GETFL ,F_SETFL ,"FL" ,O_NONBLOCK , nonblock); } +/* * Local variables: * mode: C
tools/libxl/libxl.h +6 -1
@@ -613,7 +613,12 @@ const char *libxl_run_dir_path(void);const char *libxl_xenpaging_dir_path (void );/* misc */ -int libxl_fd_set_cloexec (int fd); + +/* Each of these sets or clears the flag according to whether the +* 2nd parameter is nonzero. On failure, they log, and +* return ERROR_FAIL, but also leave errno valid. */ +int libxl_fd_set_cloexec (libxl_ctx *ctx,int fd,int cloexec); +int libxl_fd_set_nonblock (libxl_ctx *ctx,int fd,int nonblock);#include <libxl_event.h>
tools/libxl/libxl_qmp.c +2 -1
@@ -325,7 +325,8 @@ static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,if (fcntl (qmp->qmp_fd,F_SETFL , flags |O_NONBLOCK ) ==-1 ) {return -1 ; } -libxl_fd_set_cloexec (qmp->qmp_fd); + ret =libxl_fd_set_cloexec (qmp->ctx, qmp->qmp_fd,1 ); +if (ret)return -1 ;memset (&qmp->addr,0 ,sizeof (&qmp->addr)); qmp->addr.sun_family =AF_UNIX ;
tools/libxl/xl_cmdimpl.c +2 -1
@@ -1509,7 +1509,8 @@ static int create_domain(struct domain_create *dom_info) restore_fd = migrate_fd; }else { restore_fd =open (restore_file,O_RDONLY ); -libxl_fd_set_cloexec (restore_fd); + rc =libxl_fd_set_cloexec (ctx, restore_fd,1 ); +if (rc)return rc; } CHK_ERRNO( libxl_read_exactly(ctx, restore_fd, &hdr,
Signature · 1 line
1.7.2.5
PATCH v8 7/11 libxl: Permit multithreaded event waiting
In reply to Ian JacksonPreviously, the context would be locked whenever we were waiting in libxl's own call to poll (waiting for operating system events).
This would mean that multiple simultaneous calls to libxl_event_wait in different threads with different parameters would not work properly.
If we simply unlock the context, it would be possible for another thread to discover the occurrence of the event we were waiting for, without us even waking up, and we would remain in poll. So we need a way to wake up other threads: a pipe, one for each thread in poll.
We also need to move some variables from globals in the ctx to be per-polling-thread.
--- tools/libxl/libxl.c | 18 +++- tools/libxl/libxl_event.c | 196 ++++++++++++++++++++++++++++++++++-------- tools/libxl/libxl_internal.h | 50 ++++++++++- 3 files changed, 218 insertions(+), 46 deletions(-)
tools/libxl/libxl.c +14 -4
@@ -49,8 +49,9 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version, ctx->osevent_hooks =0 ; - ctx->fd_polls =0 ; - ctx->fd_rindex =0 ; +LIBXL_LIST_INIT (&ctx->pollers_event); +LIBXL_LIST_INIT (&ctx->pollers_idle); +LIBXL_LIST_INIT (&ctx->efds);LIBXL_TAILQ_INIT (&ctx->etimes);
@@ -61,6 +62,9 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,LIBXL_TAILQ_INIT (&ctx->death_list);libxl__ev_xswatch_init (&ctx->death_watch); + rc =libxl__poller_init (ctx, &ctx->poller_app); +if (rc) goto out; +if (stat (XENSTORE_PID_FILE , &stat_buf) !=0 ) {LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"Is xenstore daemon running?\n" "failed to stat %s" ,XENSTORE_PID_FILE );
@@ -135,8 +139,14 @@ int libxl_ctx_free(libxl_ctx *ctx)libxl_version_info_dispose (&ctx->version_info);if (ctx->xsh)xs_daemon_close (ctx->xsh); -free (ctx->fd_polls); -free (ctx->fd_rindex); +libxl__poller_dispose (&ctx->poller_app); +assert (LIBXL_LIST_EMPTY (&ctx->pollers_event)); +libxl__poller *poller, *poller_tmp; + LIBXL_LIST_FOREACH_SAFE(poller, &ctx->pollers_idle, entry, poller_tmp) { +libxl__poller_dispose (poller); +free (poller); + } +free (ctx->watch_slots);discard_events (&ctx->occurred);
tools/libxl/libxl_event.c +158 -38
320 lines not on this page · raw message
tools/libxl/libxl_internal.h +46 -4
68 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH v8 8/11 libxl: Asynchronous/long-running operation infrastructure
In reply to Ian JacksonProvide a new set of machinery for writing public libxl functions which may take a long time. The application gets to decide whether they want the function to be synchronous, or whether they'd prefer to get a callback, or an event, when the operation is complete.
User(s) of this machinery will be introduced in later patch(es).
--- tools/libxl/libxl.h | 53 ++++++++++++ tools/libxl/libxl_event.c | 188 ++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 107 ++++++++++++++++++++++++ tools/libxl/libxl_types.idl | 4 + 4 files changed, 352 insertions(+), 0 deletions(-)
tools/libxl/libxl.h +53 -0
@@ -237,6 +237,59 @@ enum { ERROR_BUFFERFULL = -13, }; + +/* +* Some libxl operations can take a long time. These functions take a +* parameter to control their concurrency: +* libxl_asyncop_how *ao_how +* +* If ao_how==NULL, the function will be synchronous. +* +* If ao_how!=NULL, the function will set the operation going, and if +* this is successful will return 0. In this case the zero error +* response does NOT mean that the operation was successful; it just +* means that it has been successfully started. It will finish later, +* perhaps with an error. +* +* If ao_how->callback!=NULL, the callback will be called when the +* operation completes. The same rules as for libxl_event_hooks +* apply, including the reentrancy rules and the possibility of +* "disaster", except that libxl calls ao_how->callback instead of +* libxl_event_hooks.event_occurs. (See libxl_event.h.) +* +* If ao_how->callback==NULL, a libxl_event will be generated which +* can be obtained from libxl_event_wait or libxl_event_check. The +* event will have type OPERATION_COMPLETE (which is not used +* elsewhere). +* +* Note that it is possible for an asynchronous operation which is to +* result in a callback to complete during its initiating function +* call. In this case the initiating function will return 0 +* indicating the at the operation is "in progress", even though by +* the time it returns the operation is complete and the callback has +* already happened. +* +* The application must set and use ao_how->for_event (which will be +* copied into libxl_event.for_user) or ao_how->for_callback (passed +* to the callback) to determine which operation finished, and it must +* of course check the rc value for errors. +* +* *ao_how does not need to remain valid after the initiating function +* returns. +* +* Callbacks may occur on any thread in which the application calls +* libxl. +*/ + +typedef struct { +void (*callback)(libxl_ctx *ctx,int rc,void *for_callback); +union { +libxl_ev_user for_event;/* used if callback==NULL */ +void *for_callback;/* passed to callback */ + } u; +}libxl_asyncop_how ; + +#define LIBXL_VERSION 0 typedef struct {
tools/libxl/libxl_event.c +188 -0
204 lines not on this page · raw message
tools/libxl/libxl_internal.h +107 -0
137 lines not on this page · raw message
tools/libxl/libxl_types.idl +4 -0
@@ -395,6 +395,7 @@ libxl_event_type = Enumeration("event_type", [ (1, "DOMAIN_SHUTDOWN"), (2, "DOMAIN_DESTROY"), (3, "DISK_EJECT"), + (4, "OPERATION_COMPLETE"), ]) libxl_ev_user = UInt(64)
@@ -418,4 +419,7 @@ libxl_event = Struct("event",[ ("vdev", string), ("disk", libxl_device_disk), ])), + ("operation_complete", Struct(None, [ + ("rc", integer), + ])), ]))])
Signature · 1 line
1.7.2.5
Re: [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure
2012/1/26 Ian Jackson <[email protected]>:
Quoted 225 lines
Provide a new set of machinery for writing public libxl functions which may take a long time. The application gets to decide whether they want the function to be synchronous, or whether they'd prefer to get a callback, or an event, when the operation is complete.
User(s) of this machinery will be introduced in later patch(es).
Signed-off-by Ian Jackson <[email protected]>---
tools/libxl/libxl.h | 53 ++++++++++++tools/libxl/libxl_event.c| 188 ++++++++++++++++++++++++++++++++++++++++++tools/libxl/libxl_internal.h| 107 ++++++++++++++++++++++++tools/libxl/libxl_types.idl| 4 +4 files changed, 352 insertions(+), 0 deletions(-)tools/libxl/libxl.h +53
-0@@ -237,6 +237,59 @@ enum { ERROR_BUFFERFULL = -13, }; + +/* +* Some libxl operations can take a long time. These functions take a +* parameter to control their concurrency: +* libxl_asyncop_how *ao_how +* +* If ao_how==NULL, the function will be synchronous. +* +* If ao_how!=NULL, the function will set the operation going, and if +* this is successful will return 0. In this case the zero error +* response does NOT mean that the operation was successful; it just +* means that it has been successfully started. It will finish later, +* perhaps with an error. +* +* If ao_how->callback!=NULL, the callback will be called when the +* operation completes. The same rules as for libxl_event_hooks +* apply, including the reentrancy rules and the possibility of +* "disaster", except that libxl calls ao_how->callback instead of +* libxl_event_hooks.event_occurs. (See libxl_event.h.) +* +* If ao_how->callback==NULL, a libxl_event will be generated which +* can be obtained from libxl_event_wait or libxl_event_check. The +* event will have type OPERATION_COMPLETE (which is not used +* elsewhere). +* +* Note that it is possible for an asynchronous operation which is to +* result in a callback to complete during its initiating function +* call. In this case the initiating function will return 0 +* indicating the at the operation is "in progress", even though by +* the time it returns the operation is complete and the callback has +* already happened. +* +* The application must set and use ao_how->for_event (which will be +* copied into libxl_event.for_user) or ao_how->for_callback (passed +* to the callback) to determine which operation finished, and it must +* of course check the rc value for errors. +* +* *ao_how does not need to remain valid after the initiating function +* returns. +* +* Callbacks may occur on any thread in which the application calls +* libxl. +*/ + +typedef struct { + void (*callback)(libxl_ctx *ctx, int rc, void *for_callback); + union { + libxl_ev_user for_event; /* used if callback==NULL */ + void *for_callback; /* passed to callback */ + } u; +}libxl_asyncop_how ; + + #define LIBXL_VERSION 0 typedef struct {tools/libxl/libxl_event.c +127
-0@@ -771,10 +771,21 @@ static void egc_run_callbacks(libxl__egc *egc) { EGC_GC; libxl_event *ev, *ev_tmp; + LIBXL_TAILQ_FOREACH_SAFE(ev, &egc->occurred_for_callback, link, ev_tmp) { LIBXL_TAILQ_REMOVE(&egc->occurred_for_callback, ev, link); CTX->event_hooks->event_occurs(CTX->event_hooks_user, ev); } + + libxl__ao *ao, *ao_tmp; + LIBXL_TAILQ_FOREACH_SAFE(ao, &egc->aos_for_callback, + entry_for_callback, ao_tmp) { + LIBXL_TAILQ_REMOVE(&egc->aos_for_callback, ao, entry_for_callback); + ao->how.callback(CTX, ao->rc, ao->how.u.for_callback); + ao->notified = 1; + if (!ao->in_initiator) + libxl__ao__destroy(CTX, ao); + } } void libxl__egc_cleanup(libxl__egc *egc)@@ -1061,6 +1072,183 @@ int libxl_event_wait(libxl_ctx *ctx, libxl_event **event_r, return rc; } + + +/* +* The two possible state flow of an ao: +* +* Completion before initiator return: +* +* Initiator thread Possible other threads +* +* * ao_create allocates memory and +* initialises the struct +* +* * the initiator function does its +* work, setting up various internal +* asynchronous operations -----------> * asynchronous operations +* start to take place and +* might cause ao completion +* | +* * initiator calls ao_inprogress | +* - if synchronous, run event loop | +* until the ao completes | +* - ao completes on some thread +* - completing thread releases the lock +* <--------------' +* - ao_inprogress takes the lock +* - destroy the ao +* +* +* Completion after initiator return (asynch. only): +* +* +* Initiator thread Possible other threads +* +* * ao_create allocates memory and +* initialises the struct +* +* * the initiator function does its +* work, setting up various internal +* asynchronous operations -----------> * asynchronous operations +* start to take place and +* might cause ao completion +* | +* * initiator calls ao_inprogress | +* - observes event not yet done, | +* - returns to caller | +* | +* - ao completes on some thread +* - generate the event or call the callback +* - destroy the ao +*/ + +void libxl__ao__destroy (libxl_ctx *ctx,libxl__ao *ao) +{ + if (!ao) return; + if (ao->poller) libxl__poller_put(ctx, ao->poller); + ao->magic = LIBXL__AO_MAGIC_DESTROYED; + libxl__free_all(&ao->gc); + free(ao); +} + +void libxl__ao_abort (libxl__ao *ao) +{ + AO_GC; + assert(ao->magic == LIBXL__AO_MAGIC); + assert(ao->in_initiator); + assert(!ao->complete); + libxl__ao__destroy(CTX, ao); +} + +void libxl__ao_complete (libxl__egc *egc,libxl__ao *ao,int rc) +{ + assert(ao->magic == LIBXL__AO_MAGIC); + assert(!ao->complete); + ao->complete = 1; + ao->rc = rc; + + if (ao->poller) { + assert(ao->in_initiator); + libxl__poller_wakeup(egc, ao->poller); + } else if (ao->how.callback) { + LIBXL_TAILQ_INSERT_TAIL(&egc->aos_for_callback, ao, entry_for_callback); + } else { + libxl_event *ev; + ev = NEW_EVENT(egc, OPERATION_COMPLETE, ao->domid); + if (ev) { + ev->for_user = ao->how.u.for_event; + ev->u.operation_complete.rc = ao->rc; + libxl__event_occurred(egc, ev); + } + ao->notified = 1; + } + if (!ao->in_initiator && ao->notified) + libxl__ao__destroy(libxl__gc_owner(&egc->gc), ao); +} + +libxl__ao *libxl__ao_create (libxl_ctx *ctx,uint32_t domid, + const libxl_asyncop_how *how) +{ + libxl__ao *ao; + + ao = calloc(1, sizeof(*ao)); + if (!ao) goto out; + + ao->magic = LIBXL__AO_MAGIC; + ao->in_initiator = 1; + ao->poller = 0; + ao->domid = domid; + LIBXL_INIT_GC(ao->gc, ctx); + + if (how) { + ao->how = *how; + } else { + ao->poller = libxl__poller_get(ctx); + if (!ao->poller) goto out; + } + return ao;
tools/libxl/libxl_event.c int libxl_event_wait(libxl_ctx *ctx, libxl_event **event_r, · line 1200
Quoted 10 lines
tools/libxl/libxl_event.c +10
-0+ + out: + if (ao) libxl__ao__destroy(ctx, ao); + return NULL; +} + +int libxl__ao_inprogress (libxl__ao *ao) +{ + AO_GC; + int rc;
I've just started playing with this, but I've found that if you call AO_INPROGRESS without adding any event it blocks forever, and I'm not sure if that is a design decision or a bug. Anyway, sometimes like in libxl__initiate_device_remove you might call AO_INPROGRESS without adding any event, so I think it will be safe to add something like:
if (!CTX->watch_counter)
return 0;
To prevent waiting forever, if not we need to be sure we only call AO_INPROGRESS when we have added some events (which is not as practical as this simple fix). Since I've just started playing with the events API, I might be doing an incorrect usage of this, if this sounds ok please drop a line and I will send a formal patch.
Thanks, Roger.
Quoted 224 lines
tools/libxl/libxl_event.c +51
-0+ + assert(ao->magic == LIBXL__AO_MAGIC); + assert(ao->in_initiator); + + if (ao->poller) { + /* Caller wants it done synchronously. */ + /* We use a fresh gc, so that we can free things + * each time round the loop. */ + libxl__egc egc; + LIBXL_INIT_EGC(egc,CTX); + + for (;;) { + assert(ao->magic == LIBXL__AO_MAGIC); + + if (ao->complete) { + rc = ao->rc; + ao->notified = 1; + break; + } + + rc = eventloop_iteration(&egc,ao->poller); + if (rc) { + /* Oh dear, this is quite unfortunate. */ + LIBXL__LOG(CTX, LIBXL__LOG_ERROR, "Error waiting for" + " event during long-running operation (rc=%d)", rc); + sleep(1); + /* It's either this or return ERROR_I_DONT_KNOW_WHETHER + * _THE_THING_YOU_ASKED_FOR_WILL_BE_DONE_LATER_WHEN + * _YOU_DIDNT_EXPECT_IT, since we don't have any kind of + * cancellation ability. */ + } + + CTX_UNLOCK; + libxl__egc_cleanup(&egc); + CTX_LOCK; + } + } else { + rc = 0; + } + + ao->in_initiator = 0; + + if (ao->notified) { + assert(ao->complete); + libxl__ao__destroy(CTX,ao); + } + + return rc; +} + + /** Local variables: * mode: C tools/libxl/libxl_internal.h +107
-0@@ -114,6 +114,7 @@ _hidden void libxl__log(libxl_ctx *ctx, xentoollog_level msglevel, int errnoval, typedef struct libxl__gc libxl__gc; typedef struct libxl__egc libxl__egc; +typedef struct libxl__ao libxl__ao ; typedef struct libxl__ev_fd libxl__ev_fd; typedef void libxl__ev_fd_callback(libxl__egc *egc, libxl__ev_fd *ev,@@ -218,6 +219,10 @@ struct libxl__poller { * releasing the ctx lock and going into poll; when it comes out * of poll it will take the poller off the pollers_event list. * + * A thread which is waiting for completion of a synchronous ao + * will allocate a poller and record it in the ao, so that other + * threads can wake it up. + * * When a thread is done with a poller it should put it onto * pollers_idle, where it can be reused later. *@@ -324,6 +329,21 @@ struct libxl__egc { /* for event-generating functions only */ struct libxl__gc gc; struct libxl__event_list occurred_for_callback; + LIBXL_TAILQ_HEAD(, libxl__ao) aos_for_callback; +}; + +#define LIBXL__AO_MAGIC 0xA0FACE00ul +#define LIBXL__AO_MAGIC_DESTROYED 0xA0DEAD00ul + +struct libxl__ao { + uint32_t magic; + unsigned in_initiator:1, complete:1, notified:1; + int rc; + libxl__gc gc; + libxl_asyncop_how how; + libxl__poller *poller; + uint32_t domid; + LIBXL_TAILQ_ENTRY(libxl__ao) entry_for_callback; }; #define LIBXL_INIT_GC(gc,ctx) do{ \@@ -1108,6 +1128,7 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid); #define LIBXL_INIT_EGC(egc,ctx) do{ \ LIBXL_INIT_GC((egc).gc,ctx); \ LIBXL_TAILQ_INIT(&(egc).occurred_for_callback); \ + LIBXL_TAILQ_INIT(&(egc).aos_for_callback); \ } while(0) _hidden void libxl__egc_cleanup(libxl__egc *egc);@@ -1125,6 +1146,92 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc); /* +* Machinery for asynchronous operations ("ao") +* +* All "slow" functions (includes anything that might block on a +* guest or an external script) need to use the asynchronous +* operation ("ao") machinery. The function should take a parameter +* const libxl_asyncop_how *ao_how and must start with a call to +* AO_INITIATOR_ENTRY. These functions MAY NOT be called from +* outside libxl, because they can cause reentrancy callbacks. +* +* Lifecycle of an ao: +* +* - Created by libxl__ao_create (or the AO_CREATE convenience macro). +* +* - After creation, can be used by code which implements +* the operation as follows: +* - the ao's gc, for allocating memory for the lifetime +* of the operation (possibly with the help of the AO_GC +* macro to introduce the gc into scope) +* - the ao itself may be passed about to sub-functions +* so that they can stash it away etc. +* - in particular, the ao pointer must be stashed in some +* per-operation structure which is also passed as a user +* pointer to the internal event generation request routines +* libxl__evgen_FOO, so that at some point a CALLBACK will be +* made when the operation is complete. +* +* - If initiation is successful, the initiating function needs +* to run libxl__ao_inprogress right before unlocking and +* returning, and return whatever it returns (AO_INPROGRESS macro). +* +* - If the initiation is unsuccessful, the initiating function must +* call libxl__ao_abort before unlocking and returning whatever +* error code is appropriate (AO_ABORT macro). +* +* - Later, some callback function, whose callback has been requested +* directly or indirectly, should call libxl__ao_complete (with the +* ctx locked, as it will generally already be in any event callback +* function). This must happen exactly once for each ao (and not if +* the ao has been destroyed, obviously), and it may not happen +* until libxl__ao_inprogress has been called on the ao. +* +* - Note that during callback functions, two gcs are available: +* - The one in egc, whose lifetime is only this callback +* - The one in ao, whose lifetime is the asynchronous operation +* Usually callback function should use CONTAINER_OF +* to obtain its own structure, containing a pointer to the ao, +* and then use the gc from that ao. +*/ + +#define AO_CREATE (ctx, domid, ao_how) \ + libxl__ctx_lock(ctx); \ + libxl__ao *ao = libxl__ao_create(ctx, domid, ao_how); \ + if (!ao) { libxl__ctx_unlock(ctx); return ERROR_NOMEM; } \ + AO_GC; + +#define AO_INPROGRESS ({ \ + libxl_ctx *ao__ctx = libxl__gc_owner(&ao->gc); \ + int ao__rc = libxl__ao_inprogress(ao); \ + libxl__ctx_unlock(ao__ctx); /* gc is now invalid */ \ + (ao__rc); \ + }) + +#define AO_ABORT(rc) ({ \ + libxl_ctx *ao__ctx = libxl__gc_owner(&ao->gc); \ + assert(rc); \ + libxl__ao_abort(ao); \ + libxl__ctx_unlock(ao__ctx); /* gc is now invalid */ \ + (rc); \ + }) + +#define AO_GC \ + libxl__gc *const gc = &ao->gc + + +/* All of these MUST be called with the ctx locked. +* libxl__ao_inprogress MUST be called with the ctx locked exactly once. */ +_hidden libxl__ao *libxl__ao_create(libxl_ctx*, uint32_t domid, + const libxl_asyncop_how*); +_hidden int libxl__ao_inprogress(libxl__ao *ao); +_hidden void libxl__ao_abort(libxl__ao *ao); +_hidden void libxl__ao_complete(libxl__egc *egc, libxl__ao *ao, int rc); + +/* For use by ao machinery ONLY */ +_hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao *ao); + +/* * Convenience macros. */ tools/libxl/libxl_types.idl +4
-1@@ -395,6 +395,7 @@ libxl_event_type = Enumeration("event_type", [ (1, "DOMAIN_SHUTDOWN"), (2, "DOMAIN_DESTROY"), (3, "DISK_EJECT"), + (4, "OPERATION_COMPLETE"), ]) libxl_ev_user = UInt(64)@@ -418,4 +419,7 @@ libxl_event = Struct("event",[ ("vdev", string), ("disk", libxl_device_disk), ])), + ("operation_complete", Struct(None, [ + ("rc", integer), + ])), ]))]) --1.7.2.5
Quoted 4 lines
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Re: [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure
Roger Pau Monné writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure"):
Quoted 11 lines
2012/1/26 Ian Jackson <[email protected]>:
tools/libxl/libxl_event.c int libxl_event_wait(libxl_ctx *ctx, libxl_event **event_r, · line 1200
Quoted 4 lines
tools/libxl/libxl_event.c +4
-0+int libxl__ao_inprogress (libxl__ao *ao) +{ +AO_GC ; +int rc;I've just started playing with this, but I've found that if you call
AO_INPROGRESSwithout adding any event it blocks forever, and I'm not sure if that is a design decision or a bug. Anyway, sometimes like inlibxl__initiate_device_removeyou might callAO_INPROGRESSwithout adding any event, so I think it will be safe to add something like:
How are you expecting this ao to complete ? If you haven't asked for an event callback then presumably libxl__ao_complete will never be called.
Quoted 2 lines
if (!CTX->watch_counter)
return 0;
This is definitely wrong.
Ian.
Re: [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure
2012/2/15 Ian Jackson <[email protected]>:
Quoted 13 lines
Roger Pau Monné writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure"):
Quoted 11 lines
2012/1/26 Ian Jackson <[email protected]>:
tools/libxl/libxl_event.c int libxl_event_wait(libxl_ctx *ctx, libxl_event **event_r, · line 1200
Quoted 4 lines
tools/libxl/libxl_event.c +4
-0+int libxl__ao_inprogress (libxl__ao *ao) +{ + AO_GC; + int rc;I've just started playing with this, but I've found that if you call
AO_INPROGRESSwithout adding any event it blocks forever, and I'm not sure if that is a design decision or a bug. Anyway, sometimes like inlibxl__initiate_device_removeyou might callAO_INPROGRESSwithout adding any event, so I think it will be safe to add something like:
Quoted 1 line
How are you expecting this ao to complete ?
I was expecting that AO_INPROGRESS returns 0 if no events have been added, so I don't need to know whether I have added events or not to call AO_INPROGRESS.
Quoted 3 lines
If you haven't asked for
an event callback then presumablylibxl__ao_completewill never be called.
I'm talking about libxl__ao_inprogress, which is called from AO_INPROGRESS macro. With the current code this macro might be called from libxl_device_disk_remove for example without adding any events, because libxl__initiate_device_remove can return successfully without adding an event, and AO_INPROGRESS is called unconditionally.
Quoted 6 lines
Quoted 2 lines
if (!CTX->watch_counter)
return 0;This is definitely wrong.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Re: [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure
Roger Pau Monné writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure"):
Quoted 6 lines
2012/2/15 Ian Jackson <[email protected]>:
Quoted 1 line
How are you expecting this ao to complete ?
I was expecting that
AO_INPROGRESSreturns 0 if no events have been added, so I don't need to know whether I have added events or not to callAO_INPROGRESS.
You must always call AO_INPROGRESS. After calling AO_CREATE you must call AO_INPROGRESS.
You must also arrange to call libxl__ao_complete at some point. You may only call libxl__ao_complete from an event callback. So you had better have set up an event ("added" an event as you put it).
Otherwise libxl__ao_complete will never be called at all, and indeed the synchronous call will never return.
Quoted 9 lines
Quoted 3 lines
If you haven't asked for
an event callback then presumablylibxl__ao_completewill never be called.I'm talking about
libxl__ao_inprogress, which is called fromAO_INPROGRESSmacro. With the current code this macro might be called fromlibxl_device_disk_removefor example without adding any events, becauselibxl__initiate_device_removecan return successfully without adding an event, andAO_INPROGRESSis called unconditionally.
Yes, you are right, there is a bug in libxl_device_disk_remove / libxl__initiate_device_remove. I hadn't spotted that the "out" path from the old device removal code was used in a success case too.
I will fix it.
Ian.
cover libxl: Permit immediate asynchronous completion
Ian Jackson writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure"):
Quoted 5 lines
Yes, you are right, there is a bug in
libxl_device_disk_remove/libxl__initiate_device_remove. I hadn't spotted that the "out" path from the old device removal code was used in a success case too.I will fix it.
Please take a look at the following 3-patch series. I have compiled it but not tested it, but I think something like it should work.
1/3 libxl: ao: allow immediate completion
2/3 libxl: fix hang due to libxl__initiate_device_remove 3/3 libxl: Fix eventloop_iteration over-locking
Hopefully 1/3 and 2/3 will make the intended behaviour, and the intended usage clear. Sorry for confusing you with a bad example!
Thanks,
Ian.
Re: [PATCH 0/3] libxl: Permit immediate asynchronous completion
Ian Jackson writes ("[PATCH 0/3] libxl: Permit immediate asynchronous completion"):
Quoted 9 lines
Ian Jackson writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure"):
Quoted 5 lines
Yes, you are right, there is a bug in
libxl_device_disk_remove/libxl__initiate_device_remove. I hadn't spotted that the "out" path from the old device removal code was used in a success case too.I will fix it.
Please take a look at the following 3-patch series. I have compiled it but not tested it, but I think something like it should work.
Sorry, Roger, the way I sent this mail omitted you from the CC.
Quoted 6 lines
1/3 libxl: ao: allow immediate completion
2/3 libxl: fix hang due tolibxl__initiate_device_remove3/3 libxl: Fixeventloop_iterationover-lockingHopefully 1/3 and 2/3 will make the intended behaviour, and the intended usage clear. Sorry for confusing you with a bad example!
Ian.
Re: [PATCH 0/3] libxl: Permit immediate asynchronous completion
2012/2/17 Ian Jackson <[email protected]>:
Quoted 6 lines
Ian Jackson writes ("[PATCH 0/3] libxl: Permit immediate asynchronous completion"):
Quoted 5 lines
Ian Jackson writes ("Re: [Xen-devel] [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure"):
Quoted 4 lines
Yes, you are right, there is a bug in
libxl_device_disk_remove/libxl__initiate_device_remove. I hadn't spotted that the "out" path from the old device removal code was used in a success case too.
Quoted 13 lines
Quoted 4 lines
Quoted 1 line
I will fix it.
Please take a look at the following 3-patch series. I have compiled it but not tested it, but I think something like it should work.
Sorry, Roger, the way I sent this mail omitted you from the CC.
Quoted 6 lines
1/3 libxl: ao: allow immediate completion
2/3 libxl: fix hang due tolibxl__initiate_device_remove3/3 libxl: Fixeventloop_iterationover-lockingHopefully 1/3 and 2/3 will make the intended behaviour, and the intended usage clear. Sorry for confusing you with a bad example!
np, thanks for the quick fix, I would like to test those patches this afternoon if I have some time and share the results.
Quoted 1 line
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Re: [PATCH 0/3] libxl: Permit immediate asynchronous completion
Roger Pau Monné writes ("Re: [PATCH 0/3] libxl: Permit immediate asynchronous completion"):
Quoted 11 lines
Quoted 8 lines
Sorry, Roger, the way I sent this mail omitted you from the CC.
Quoted 6 lines
1/3 libxl: ao: allow immediate completion
2/3 libxl: fix hang due tolibxl__initiate_device_remove3/3 libxl: Fixeventloop_iterationover-lockingHopefully 1/3 and 2/3 will make the intended behaviour, and the intended usage clear. Sorry for confusing you with a bad example!
np, thanks for the quick fix, I would like to test those patches this afternoon if I have some time and share the results.
Thank you, that would be awesome. But note that I haven't executed these myself yet. You will almost certainly want the additional patchlet below - thanks to Ian Campbell's review for spotting the bug.
Ian.
tools/libxl/libxl_internal.h +2 -1
@@ -1233,7 +1233,8 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);libxl__ctx_lock (ctx); \libxl__ao *ao =libxl__ao_create (ctx, domid, ao_how); \if (!ao) {libxl__ctx_unlock (ctx);return ERROR_NOMEM ; } \ -EGC_INIT (ctx); +libxl__egc egc[1 ];LIBXL_INIT_EGC (egc[0 ],ctx); \ +AO_GC ;#define AO_INPROGRESS ({ \ libxl_ctx *ao__ctx = libxl__gc_owner(&ao->gc); \
PATCH 1/3 libxl: ao: allow immediate completion
In reply to Ian JacksonMake it possible to complete an ao during its initating function.
Previously this was not generally possible because initiators did not have an egc. But there is no reason why an ao initiator should not have an egc, so make the standard macros provide one.
Change the internal documentation comments accordingly. (This change, which means that an initiator function may call a completion callback directly, is already consistent with the documented external API.)
We also invent of a new state flag "constructing" which indicates whether we are between ao__create and ao__inprogress. This is a slightly optimisation which allows ao_complete to not bother poking the wakeup pipe, since the logic in ao__inprogress will not run the event loop if the ao is complete on entry.
--- tools/libxl/libxl_event.c | 8 ++++++-- tools/libxl/libxl_internal.h | 13 +++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-)
tools/libxl/libxl_event.c +6 -2
@@ -1225,7 +1225,9 @@ void libxl__ao_complete(libxl__egc *egc, libxl__ao *ao, int rc)if (ao->poller) {assert (ao->in_initiator); -libxl__poller_wakeup (egc, ao->poller); +if (!ao->constructing) +/* don't bother with this if we're not in the event loop */ +libxl__poller_wakeup (egc, ao->poller); }else if (ao->how.callback) {LIBXL_TAILQ_INSERT_TAIL (&egc->aos_for_callback, ao, entry_for_callback); }else {
@@ -1251,6 +1253,7 @@ libxl__ao *libxl__ao_create(libxl_ctx *ctx, uint32_t domid,if (!ao) goto out; ao->magic =LIBXL__AO_MAGIC ; + ao->constructing =1 ; ao->in_initiator =1 ; ao->poller =0 ; ao->domid = domid;
@@ -1275,7 +1278,8 @@ int libxl__ao_inprogress(libxl__ao *ao)int rc;assert (ao->magic ==LIBXL__AO_MAGIC ); -assert (ao->in_initiator); +assert (ao->constructing && ao->in_initiator); + ao->constructing =0 ;if (ao->poller) {/* Caller wants it done synchronously. */
tools/libxl/libxl_internal.h +9 -4
@@ -337,7 +337,7 @@ struct libxl__egc {struct libxl__ao {uint32_t magic; -unsigned in_initiator:1 , complete:1 , notified:1 ; +unsigned constructing:1 , in_initiator:1 , complete:1 , notified:1 ;int rc;libxl__gc gc;libxl_asyncop_how how;
@@ -1185,6 +1185,10 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);* AO_INITIATOR_ENTRY. These functions MAY NOT be called from * outside libxl, because they can cause reentrancy callbacks. * +* For the same reason functions taking an ao_how may make themselves +* an egc with EGC_INIT (and they will generally want to, to be able +* to immediately complete an ao during its setup). +* * Lifecycle of an ao: * * - Created by libxl__ao_create (or the AO_CREATE convenience macro).
@@ -1214,8 +1218,7 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);* directly or indirectly, should call libxl__ao_complete (with the * ctx locked, as it will generally already be in any event callback * function). This must happen exactly once for each ao (and not if -* the ao has been destroyed, obviously), and it may not happen -* until libxl__ao_inprogress has been called on the ao. +* the ao has been destroyed, obviously). * * - Note that during callback functions, two gcs are available: * - The one in egc, whose lifetime is only this callback
@@ -1229,12 +1232,13 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);libxl__ctx_lock (ctx); \libxl__ao *ao =libxl__ao_create (ctx, domid, ao_how); \if (!ao) {libxl__ctx_unlock (ctx);return ERROR_NOMEM ; } \ -AO_GC ; +EGC_INIT (ctx); #define AO_INPROGRESS ({ \ libxl_ctx *ao__ctx = libxl__gc_owner(&ao->gc); \ int ao__rc = libxl__ao_inprogress(ao); \ libxl__ctx_unlock(ao__ctx); /* gc is now invalid */ \ +EGC_FREE ; \ (ao__rc); \ })
@@ -1243,6 +1247,7 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);assert (rc); \libxl__ao_abort (ao); \libxl__ctx_unlock (ao__ctx);/* gc is now invalid */ \ +EGC_FREE ; \ (rc); \ })
Signature · 1 line
1.7.2.5
Re: [PATCH 1/3] libxl: ao: allow immediate completion
Quoted 46 lines · Ian Jackson
Make it possible to complete an ao during its initating function.
Previously this was not generally possible because initiators did not have an egc. But there is no reason why an ao initiator should not have an egc, so make the standard macros provide one.
Change the internal documentation comments accordingly. (This change, which means that an initiator function may call a completion callback directly, is already consistent with the documented external API.)
We also invent of a new state flag "constructing" which indicates whether we are between
ao__createandao__inprogress. This is a slightly optimisation which allowsao_completeto not bother poking the wakeup pipe, since the logic inao__inprogresswill not run the event loop if the ao is complete on entry.Signed-off-by Ian Jackson <[email protected]>--- tools/libxl/libxl_event.c | 8 ++++++-- tools/libxl/libxl_internal.h | 13 +++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-)tools/libxl/libxl_event.c +4
-1@@ -1225,7 +1225,9 @@ void libxl__ao_complete(libxl__egc *egc, libxl__ao *ao, int rc)if (ao->poller) {assert (ao->in_initiator); -libxl__poller_wakeup (egc, ao->poller); +if (!ao->constructing) +/* don't bother with this if we're not in the event loop */ +libxl__poller_wakeup (egc, ao->poller); }else if (ao->how.callback) {LIBXL_TAILQ_INSERT_TAIL (&egc->aos_for_callback, ao, entry_for_callback); }else {@@ -1251,6 +1253,7 @@ libxl__ao *libxl__ao_create(libxl_ctx *ctx, uint32_t domid,if (!ao) goto out; ao->magic =LIBXL__AO_MAGIC ; + ao->constructing =1 ; ao->in_initiator =1 ; ao->poller =0 ; ao->domid = domid;
tools/libxl/libxl_event.c int libxl__ao_inprogress(libxl__ao *ao) · line 1281
Quoted 5 lines
tools/libxl/libxl_event.c +1
-1@@ -1275,7 +1278,8 @@ int libxl__ao_inprogress(libxl__ao *ao)int rc;assert (ao->magic ==LIBXL__AO_MAGIC ); -assert (ao->in_initiator); +assert (ao->constructing && ao->in_initiator);
Doing two asserts will make it clearer which condition is not satisfied if we ever see this.
Quoted 39 lines
tools/libxl/libxl_event.c +1
-0+ ao->constructing =0 ;if (ao->poller) {/* Caller wants it done synchronously. */ tools/libxl/libxl_internal.h +6
-3@@ -337,7 +337,7 @@ struct libxl__egc {struct libxl__ao {uint32_t magic; -unsigned in_initiator:1 , complete:1 , notified:1 ; +unsigned constructing:1 , in_initiator:1 , complete:1 , notified:1 ;int rc;libxl__gc gc;libxl_asyncop_how how;@@ -1185,6 +1185,10 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);* AO_INITIATOR_ENTRY. These functions MAY NOT be called from * outside libxl, because they can cause reentrancy callbacks. * +* For the same reason functions taking an ao_how may make themselves +* an egc with EGC_INIT (and they will generally want to, to be able +* to immediately complete an ao during its setup). +* * Lifecycle of an ao: * * - Created by libxl__ao_create (or the AO_CREATE convenience macro). @@ -1214,8 +1218,7 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);* directly or indirectly, should call libxl__ao_complete (with the * ctx locked, as it will generally already be in any event callback * function). This must happen exactly once for each ao (and not if -* the ao has been destroyed, obviously), and it may not happen -* until libxl__ao_inprogress has been called on the ao. +* the ao has been destroyed, obviously). * * - Note that during callback functions, two gcs are available: * - The one in egc, whose lifetime is only this callback
tools/libxl/libxl_internal.h _hidden void libxl__egc_cleanup(libxl__egc *egc); · line 1235
Quoted 5 lines
tools/libxl/libxl_internal.h +1
-1@@ -1229,12 +1232,13 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);libxl__ctx_lock (ctx); \libxl__ao *ao =libxl__ao_create (ctx, domid, ao_how); \if (!ao) {libxl__ctx_unlock (ctx);return ERROR_NOMEM ; } \ -AO_GC ; +EGC_INIT (ctx);
This means that the "gc" within a function which uses this macro is now "&egc->gc" rather than "&ao->gc".
However the egc->gc is cleaned up in AO_INPROGRESS/AO_ABORT whereas the ao->gc persists until the activity completes which is a meaningful difference to think about.
Now, as it happens, none of the existing callers of AO_CREATE actually use the gc for anything (they just pass it around a bit). If, hypothetically, libxl__device_from_disk() were to allocate some memory into the libxl__device passed to libxl__initiate_device_remove(ao,device) then would initiate_device_remove be expected to copy any part of device which it wanted to keep until completion?
Another difference is that any function calling AO_CREATE cannot now be called from within libxl, since it now calls LIBXL__INIT_EGC which has this restriction. libxl_cdrom_insert does not obey this new restriction.
Ian.
Re: [PATCH 1/3] libxl: ao: allow immediate completion
Ian Campbell writes ("Re: [Xen-devel] [PATCH 1/3] libxl: ao: allow immediate completion"):
Quoted 6 lines · Ian Jackson
tools/libxl/libxl_event.c int libxl__ao_inprogress(libxl__ao *ao) · line 1281
Quoted 2 lines
tools/libxl/libxl_event.c +1
-1-assert (ao->in_initiator); +assert (ao->constructing && ao->in_initiator);Doing two asserts will make it clearer which condition is not satisfied if we ever see this.
Good point. I was foolishly thinking of bitfield optimisations but this code is not a hot path.
Quoted 5 lines
tools/libxl/libxl_internal.h _hidden void libxl__egc_cleanup(libxl__egc *egc); · line 1235
Quoted 2 lines
tools/libxl/libxl_internal.h +1
-1-AO_GC ; +EGC_INIT (ctx);This means that the "gc" within a function which uses this macro is now "
&egc->gc" rather than "&ao->gc".
You are right, and this is wrong.
Quoted 3 lines
Another difference is that any function calling
AO_CREATEcannot now be called from within libxl, since it now callsLIBXL__INIT_EGCwhich has this restriction.libxl_cdrom_insertdoes not obey this new restriction.
This restriction is not new. libxl_cdrom_insert has always violated it and needs converting to the new scheme of things.
The restriction is documented here in libxl_internal.h but sadly has a "outside" when it should say "inside":
* All "slow" functions (includes anything that might block on a
* guest or an external script) need to use the asynchronous
* operation ("ao") machinery. The function should take a parameter
* const libxl_asyncop_how *ao_how and must start with a call to
* AO_INITIATOR_ENTRY. These functions MAY NOT be called from
* outside libxl, because they can cause reentrancy callbacks.
In practice I think libxl_cdrom_insert will not mind the reentrancy so the bug is latent. It doesn't take out the ctx lock, and although a callback might involve messing with the same device there is no difference between that and another process doing the same.
Ian.
PATCH 2/3 libxl: fix hang due to libxl__initiate_device_remove
In reply to Ian Jacksonlibxl__initiate_device_remove might discover that the operation was complete, immediately (typically, if the device is already removed).
Previously, in this situation, it would return 0 to the caller but never call libxl__ao_complete. Fix this. This necessitates passing the egc in from the functions which are the ao initiators.
--- tools/libxl/libxl.c | 8 ++++---- tools/libxl/libxl_device.c | 18 ++++++++++++------ tools/libxl/libxl_internal.h | 3 ++- 3 files changed, 18 insertions(+), 11 deletions(-)
tools/libxl/libxl.c +4 -4
@@ -1362,7 +1362,7 @@ int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid, rc =libxl__device_from_disk (gc, domid, disk, &device);if (rc !=0 ) goto out; - rc =libxl__initiate_device_remove (ao, &device); + rc =libxl__initiate_device_remove (egc, ao, &device);if (rc) goto out;return AO_INPROGRESS ;
@@ -1815,7 +1815,7 @@ int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid, rc =libxl__device_from_nic (gc, domid, nic, &device);if (rc !=0 ) goto out; - rc =libxl__initiate_device_remove (ao, &device); + rc =libxl__initiate_device_remove (egc, ao, &device);if (rc) goto out;return AO_INPROGRESS ;
@@ -2159,7 +2159,7 @@ int libxl_device_vkb_remove(libxl_ctx *ctx, uint32_t domid, rc =libxl__device_from_vkb (gc, domid, vkb, &device);if (rc !=0 ) goto out; - rc =libxl__initiate_device_remove (ao, &device); + rc =libxl__initiate_device_remove (egc, ao, &device);if (rc) goto out;return AO_INPROGRESS ;
@@ -2286,7 +2286,7 @@ int libxl_device_vfb_remove(libxl_ctx *ctx, uint32_t domid, rc =libxl__device_from_vfb (gc, domid, vfb, &device);if (rc !=0 ) goto out; - rc =libxl__initiate_device_remove (ao, &device); + rc =libxl__initiate_device_remove (egc, ao, &device);if (rc) goto out;return AO_INPROGRESS ;
tools/libxl/libxl_device.c +12 -6
@@ -376,7 +376,8 @@ static void device_remove_callback(libxl__egc *egc, libxl__ev_devstate *ds,device_remove_cleanup (gc, aorm); } -int libxl__initiate_device_remove (libxl__ao *ao,libxl__device *dev) +int libxl__initiate_device_remove (libxl__egc *egc,libxl__ao *ao, +libxl__device *dev) {AO_GC ;libxl_ctx *ctx =libxl__gc_owner (gc);
@@ -388,11 +389,11 @@ int libxl__initiate_device_remove(libxl__ao *ao, libxl__device *dev)libxl__ao_device_remove *aorm =0 ;if (!state) - goto out; + goto out_ok;if (atoi (state) !=4 ) {libxl__device_destroy_tapdisk (gc, be_path);xs_rm (ctx->xsh,XBT_NULL , be_path); - goto out; + goto out_ok; } retry_transaction:
@@ -404,7 +405,7 @@ retry_transaction: goto retry_transaction; else { rc =ERROR_FAIL ; - goto out; + goto out_fail; } }
@@ -417,13 +418,18 @@ retry_transaction: rc =libxl__ev_devstate_wait (gc, &aorm->ds, device_remove_callback, state_path, XenbusStateClosed,LIBXL_DESTROY_TIMEOUT *1000 ); -if (rc) goto out; +if (rc) goto out_fail;return 0 ; - out: + out_fail: +assert (rc);device_remove_cleanup (gc, aorm);return rc; + + out_ok: +libxl__ao_complete (egc, ao,0 ); +return 0 ; }int libxl__device_destroy (libxl__gc *gc,libxl__device *dev)
tools/libxl/libxl_internal.h +2 -1
@@ -669,7 +669,8 @@ _hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state);* this is done, the ao will be completed. An error * return from libxl__initiate_device_remove means that the ao * will _not_ be completed and the caller must do so. */ -_hidden int libxl__initiate_device_remove(libxl__ao*, libxl__device *dev); +_hidden int libxl__initiate_device_remove(libxl__egc*, libxl__ao*, +libxl__device *dev);/* * libxl__ev_devstate - waits a given time for a device to
Signature · 1 line
1.7.2.5
PATCH 3/3 libxl: Fix eventloop_iteration over-locking
In reply to Ian Jacksoneventloop_iteration's head comment says that it must be called with the ctx locked exactly once, and this is indeed true, and it's done correctly at both the call sites.
However, it takes out the lock an additional time itself. This is wrong because it prevents the unlocks around poll from being effective. This would mean that a multithreaded event-loop using program might suffer from undesired blocking, as one thread trying to enter libxl might end up stalled by another thread waiting for a slow event. So remove those two lock calls.
Also add a couple of comments documenting the locking behaviour of libxl__ao_inprogress and libxl__egc_cleanup.
--- tools/libxl/libxl_event.c | 4 ---- tools/libxl/libxl_internal.h | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-)
tools/libxl/libxl_event.c +0 -4
@@ -1058,8 +1058,6 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {int rc;struct timeval now; -CTX_LOCK ; - rc =libxl__gettimeofday (gc, &now);if (rc) goto out;
@@ -1102,8 +1100,6 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {afterpoll_internal (egc, poller, poller->fd_polls_allocd, poller->fd_polls, now); -CTX_UNLOCK ; - rc =0 ; out:return rc;
tools/libxl/libxl_internal.h +2 -2
@@ -1165,7 +1165,7 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid); _hidden void libxl__egc_cleanup(libxl__egc *egc);/* Frees memory allocated within this egc's gc, and and report all * occurred events via callback, if applicable. May reenter the -* application; see restrictions above. */ +* application; see restrictions above. The ctx must be UNLOCKED. */ /* convenience macros: */
@@ -1260,7 +1260,7 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);* libxl__ao_inprogress MUST be called with the ctx locked exactly once. */ _hidden libxl__ao *libxl__ao_create(libxl_ctx*, uint32_t domid,const libxl_asyncop_how *); -_hidden int libxl__ao_inprogress(libxl__ao *ao); +_hidden int libxl__ao_inprogress(libxl__ao *ao); /* temporarily unlocks */ _hidden void libxl__ao_abort(libxl__ao *ao); _hidden void libxl__ao_complete(libxl__egc *egc, libxl__ao *ao, int rc);
Signature · 1 line
1.7.2.5
Re: [PATCH 3/3] libxl: Fix eventloop_iteration over-locking
Quoted 11 lines · Ian Jackson
eventloop_iteration's head comment says that it must be called with the ctx locked exactly once, and this is indeed true, and it's done correctly at both the call sites.
However, it takes out the lock an additional time itself. This is wrong because it prevents the unlocks around poll from being effective. This would mean that a multithreaded event-loop using program might suffer from undesired blocking, as one thread trying to enter libxl might end up stalled by another thread waiting for a slow event. So remove those two lock calls.
Quoted 2 lines
Also add a couple of comments documenting the locking behaviour of
libxl__ao_inprogressandlibxl__egc_cleanup.
Quoted 51 lines
Signed-off-by Ian Jackson <[email protected]>--- tools/libxl/libxl_event.c | 4 ---- tools/libxl/libxl_internal.h | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-)tools/libxl/libxl_event.c +0
-4@@ -1058,8 +1058,6 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {int rc;struct timeval now; -CTX_LOCK ; - rc =libxl__gettimeofday (gc, &now);if (rc) goto out;@@ -1102,8 +1100,6 @@ static int eventloop_iteration(libxl__egc *egc, libxl__poller *poller) {afterpoll_internal (egc, poller, poller->fd_polls_allocd, poller->fd_polls, now); -CTX_UNLOCK ; - rc =0 ; out:return rc;tools/libxl/libxl_internal.h +2
-2@@ -1165,7 +1165,7 @@ libxl__device_model_version_running(libxl__gc *gc, uint32_t domid); _hidden void libxl__egc_cleanup(libxl__egc *egc);/* Frees memory allocated within this egc's gc, and and report all * occurred events via callback, if applicable. May reenter the -* application; see restrictions above. */ +* application; see restrictions above. The ctx must be UNLOCKED. */ /* convenience macros: */ @@ -1260,7 +1260,7 @@ _hidden void libxl__egc_cleanup(libxl__egc *egc);* libxl__ao_inprogress MUST be called with the ctx locked exactly once. */ _hidden libxl__ao *libxl__ao_create(libxl_ctx*, uint32_t domid,const libxl_asyncop_how *); -_hidden int libxl__ao_inprogress(libxl__ao *ao); +_hidden int libxl__ao_inprogress(libxl__ao *ao); /* temporarily unlocks */ _hidden void libxl__ao_abort(libxl__ao *ao); _hidden void libxl__ao_complete(libxl__egc *egc, libxl__ao *ao, int rc);
PATCH v8 9/11 libxl: New convenience macro CONTAINER_OF
In reply to Ian JacksonProvide a convenient and type-safe wrapper which does the correct dance to subtract offsetof. This is very similar to the "container_of" macro in the Linux kernel, but it has an additional feature that instead of the type argument you may also pass an expression of that type; this makes initialising a variable with CONTAINER_OF easier.
--- tools/libxl/libxl_internal.h | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.h +29 -0
@@ -1235,6 +1235,35 @@ _hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao *ao);* Convenience macros. */ +/* +* CONTAINER_OF work like this. Given: +* typedef struct { +* ... +* member_type member_name; +* ... +* } outer_type; +* outer_type outer, *outer_var; +* member_type *inner_ptr = &outer->member_name; +* +* Then, effectively: +* outer_type *CONTAINER_OF(member_type *inner_ptr, +* *outer_var, // or type name for outer_type +* member_name); +* +* So that: +* CONTAINER_OF(inner_ptr, *outer_var, member_name) == &outer +* CONTAINER_OF(inner_ptr, outer_type, member_name) == &outer +*/ +#define CONTAINER_OF (inner_ptr, outer, member_name) \ + ({ \ + typeof(outer) *container_of_; \ + container_of_ = (void*)((char*)(inner_ptr) - \ + offsetof(typeof(outer), member_name)); \ + (void)(&container_of_->member_name == \ + (typeof(inner_ptr))0)/* type check */ ; \ + container_of_; \ + }) +/* * All of these assume (or define)
Signature · 1 line
1.7.2.5
Re: [PATCH 09/11] libxl: New convenience macro CONTAINER_OF
Quoted 8 lines · Ian Jackson
Provide a convenient and type-safe wrapper which does the correct dance to subtract offsetof. This is very similar to the "
container_of" macro in the Linux kernel, but it has an additional feature that instead of the type argument you may also pass an expression of that type; this makes initialising a variable withCONTAINER_OFeasier.Signed-off-by Ian Jackson <[email protected]>
Acked.
Quoted 44 lines
--- tools/libxl/libxl_internal.h | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-)tools/libxl/libxl_internal.h +29
-0@@ -1235,6 +1235,35 @@ _hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao *ao);* Convenience macros. */ +/* +* CONTAINER_OF work like this. Given: +* typedef struct { +* ... +* member_type member_name; +* ... +* } outer_type; +* outer_type outer, *outer_var; +* member_type *inner_ptr = &outer->member_name; +* +* Then, effectively: +* outer_type *CONTAINER_OF(member_type *inner_ptr, +* *outer_var, // or type name for outer_type +* member_name); +* +* So that: +* CONTAINER_OF(inner_ptr, *outer_var, member_name) == &outer +* CONTAINER_OF(inner_ptr, outer_type, member_name) == &outer +*/ +#define CONTAINER_OF (inner_ptr, outer, member_name) \ + ({ \ + typeof(outer) *container_of_; \ + container_of_ = (void*)((char*)(inner_ptr) - \ + offsetof(typeof(outer), member_name)); \ + (void)(&container_of_->member_name == \ + (typeof(inner_ptr))0)/* type check */ ; \ + container_of_; \ + }) +/* * All of these assume (or define)
PATCH v8 10/11 libxl: Introduce libxl__ev_devstate
In reply to Ian JacksonProvide a new-style asynchronous facility for waiting for device states on xenbus. This will replace libxl__wait_for_device_state, after the callers have been updated in later patches.
--- tools/libxl/libxl_event.c | 75 ++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 41 +++++++++++++++++++++++ 2 files changed, 116 insertions(+), 0 deletions(-)
tools/libxl/libxl_event.c +75 -0
@@ -507,6 +507,81 @@ void libxl__ev_xswatch_deregister(libxl__gc *gc, libxl__ev_xswatch *w) }/* +* waiting for device state +*/ + +static void devstate_watch_callback (libxl__egc *egc,libxl__ev_xswatch *watch, +const char *watch_path,const char *event_path) +{ +EGC_GC ; +libxl__ev_devstate *ds =CONTAINER_OF (watch, *ds, watch); +int rc; + +char *sstate =libxl__xs_read (gc,XBT_NULL , watch_path); +if (!sstate) { +if (errno ==ENOENT ) { +LIBXL__LOG (CTX ,LIBXL__LOG_DEBUG ,"backend %s wanted state %d" +" but it was removed" , watch_path, ds->wanted); + rc =ERROR_INVAL ; + }else { +LIBXL__LOG_ERRNO (CTX ,LIBXL__LOG_ERROR ,"backend %s wanted state" +" %d but read failed" , watch_path, ds->wanted); + rc =ERROR_FAIL ; + } + }else { +int got =atoi (sstate); +if (got == ds->wanted) { +LIBXL__LOG (CTX ,LIBXL__LOG_DEBUG ,"backend %s wanted state %d ok" , + watch_path, ds->wanted); + rc =0 ; + }else { +LIBXL__LOG (CTX ,LIBXL__LOG_DEBUG ,"backend %s wanted state %d" +" still waiting state %d" , watch_path, ds->wanted, got); +return ; + } + } +libxl__ev_devstate_cancel (gc, ds); + ds->callback (egc, ds, rc); +} + +static void devstate_timeout (libxl__egc *egc,libxl__ev_time *ev, +const struct timeval *requested_abs) +{ +EGC_GC ; +libxl__ev_devstate *ds =CONTAINER_OF (ev, *ds, timeout); +LIBXL__LOG (CTX ,LIBXL__LOG_DEBUG ,"backend %s wanted state %d " +" timed out" , ds->watch.path, ds->wanted); +libxl__ev_devstate_cancel (gc, ds); + ds->callback (egc, ds,ERROR_TIMEDOUT ); +} + +int libxl__ev_devstate_wait (libxl__gc *gc,libxl__ev_devstate *ds, +libxl__ev_devstate_callback cb, +const char *state_path,int state,int milliseconds) +{ +int rc; + +libxl__ev_time_init (&ds->timeout); +libxl__ev_xswatch_init (&ds->watch); + ds->wanted = state; + ds->callback = cb; + + rc =libxl__ev_time_register_rel (gc, &ds->timeout, devstate_timeout, + milliseconds); +if (rc) goto out; + + rc =libxl__ev_xswatch_register (gc, &ds->watch, devstate_watch_callback, + state_path); +if (rc) goto out; + +return 0 ; + + out: +libxl__ev_devstate_cancel (gc, ds); +return rc; +} + +/* * osevent poll */
tools/libxl/libxl_internal.h +41 -0
47 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH v8 11/11 libxl: Convert to asynchronous: device removal
In reply to Ian JacksonConvert libxl_FOO_device_remove, and the function which does the bulk of the work, libxl__device_remove, to the new async ops scheme.
Adjust all callers.
Also remove libxl__wait_for_device_state which is now obsolete.
--- tools/libxl/libxl.c | 60 +++++++++++------- tools/libxl/libxl.h | 16 ++++- tools/libxl/libxl_device.c | 113 +++++++++------------------------ tools/libxl/libxl_internal.h | 30 ++-------- tools/libxl/xl_cmdimpl.c | 4 +- tools/ocaml/libs/xl/xenlight_stubs.c | 8 +- 6 files changed, 92 insertions(+), 139 deletions(-)
tools/libxl/libxl.c +38 -22
@@ -1311,19 +1311,23 @@ out: }int libxl_device_disk_remove (libxl_ctx *ctx,uint32_t domid, -libxl_device_disk *disk) +libxl_device_disk *disk, +const libxl_asyncop_how *ao_how) { -GC_INIT (ctx); +AO_CREATE (ctx, domid, ao_how);libxl__device device;int rc; rc =libxl__device_from_disk (gc, domid, disk, &device);if (rc !=0 ) goto out; - rc =libxl__device_remove (gc, &device,1 ); + rc =libxl__initiate_device_remove (ao, &device); +if (rc) goto out; + +return AO_INPROGRESS ; + out: -GC_FREE ; -return rc; +return AO_ABORT (rc); } int libxl_device_disk_destroy(libxl_ctx *ctx, uint32_t domid,
@@ -1537,11 +1541,11 @@ int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk) ret =0 ; -libxl_device_disk_remove (ctx, domid, disks + i); +libxl_device_disk_remove (ctx, domid, disks + i,0 );libxl_device_disk_add (ctx, domid, disk); stubdomid =libxl_get_stubdom_id (ctx, domid);if (stubdomid) { -libxl_device_disk_remove (ctx, stubdomid, disks + i); +libxl_device_disk_remove (ctx, stubdomid, disks + i,0 );libxl_device_disk_add (ctx, stubdomid, disk); } out:
@@ -1760,19 +1764,23 @@ out: }int libxl_device_nic_remove (libxl_ctx *ctx,uint32_t domid, -libxl_device_nic *nic) +libxl_device_nic *nic, +const libxl_asyncop_how *ao_how) { -GC_INIT (ctx); +AO_CREATE (ctx, domid, ao_how);libxl__device device;int rc; rc =libxl__device_from_nic (gc, domid, nic, &device);if (rc !=0 ) goto out; - rc =libxl__device_remove (gc, &device,1 ); + rc =libxl__initiate_device_remove (ao, &device); +if (rc) goto out; + +return AO_INPROGRESS ; + out: -GC_FREE ; -return rc; +return AO_ABORT (rc); } int libxl_device_nic_destroy(libxl_ctx *ctx, uint32_t domid,
@@ -2100,19 +2108,23 @@ out: }int libxl_device_vkb_remove (libxl_ctx *ctx,uint32_t domid, -libxl_device_vkb *vkb) +libxl_device_vkb *vkb, +const libxl_asyncop_how *ao_how) { -GC_INIT (ctx); +AO_CREATE (ctx, domid, ao_how);libxl__device device;int rc; rc =libxl__device_from_vkb (gc, domid, vkb, &device);if (rc !=0 ) goto out; - rc =libxl__device_remove (gc, &device,1 ); + rc =libxl__initiate_device_remove (ao, &device); +if (rc) goto out; + +return AO_INPROGRESS ; + out: -GC_FREE ; -return rc; +return AO_ABORT (rc); } int libxl_device_vkb_destroy(libxl_ctx *ctx, uint32_t domid,
@@ -2217,19 +2229,23 @@ out: }int libxl_device_vfb_remove (libxl_ctx *ctx,uint32_t domid, -libxl_device_vfb *vfb) +libxl_device_vfb *vfb, +const libxl_asyncop_how *ao_how) { -GC_INIT (ctx); +AO_CREATE (ctx, domid, ao_how);libxl__device device;int rc; rc =libxl__device_from_vfb (gc, domid, vfb, &device);if (rc !=0 ) goto out; - rc =libxl__device_remove (gc, &device,1 ); + rc =libxl__initiate_device_remove (ao, &device); +if (rc) goto out; + +return AO_INPROGRESS ; + out: -GC_FREE ; -return rc; +return AO_ABORT (rc); } int libxl_device_vfb_destroy(libxl_ctx *ctx, uint32_t domid,
tools/libxl/libxl.h +12 -4
@@ -467,7 +467,9 @@ libxl_vminfo * libxl_list_vm(libxl_ctx *ctx, int *nb_vm);/* Disks */ int libxl_device_disk_init (libxl_ctx *ctx,libxl_device_disk *disk);int libxl_device_disk_add (libxl_ctx *ctx,uint32_t domid,libxl_device_disk *disk); -int libxl_device_disk_remove (libxl_ctx *ctx,uint32_t domid,libxl_device_disk *disk); +int libxl_device_disk_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_disk *disk, +const libxl_asyncop_how *ao_how);int libxl_device_disk_destroy (libxl_ctx *ctx,uint32_t domid,libxl_device_disk *disk);
@@ -491,7 +493,9 @@ int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk);/* Network Interfaces */ int libxl_device_nic_init (libxl_ctx *ctx,libxl_device_nic *nic);int libxl_device_nic_add (libxl_ctx *ctx,uint32_t domid,libxl_device_nic *nic); -int libxl_device_nic_remove (libxl_ctx *ctx,uint32_t domid,libxl_device_nic *nic); +int libxl_device_nic_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_nic *nic, +const libxl_asyncop_how *ao_how);int libxl_device_nic_destroy (libxl_ctx *ctx,uint32_t domid,libxl_device_nic *nic);libxl_device_nic *libxl_device_nic_list (libxl_ctx *ctx,uint32_t domid,int *num);
@@ -501,13 +505,17 @@ int libxl_device_nic_getinfo(libxl_ctx *ctx, uint32_t domid,/* Keyboard */ int libxl_device_vkb_init (libxl_ctx *ctx,libxl_device_vkb *vkb);int libxl_device_vkb_add (libxl_ctx *ctx,uint32_t domid,libxl_device_vkb *vkb); -int libxl_device_vkb_remove (libxl_ctx *ctx,uint32_t domid,libxl_device_vkb *vkb); +int libxl_device_vkb_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_vkb *vkb, +const libxl_asyncop_how *ao_how);int libxl_device_vkb_destroy (libxl_ctx *ctx,uint32_t domid,libxl_device_vkb *vkb);/* Framebuffer */ int libxl_device_vfb_init (libxl_ctx *ctx,libxl_device_vfb *vfb);int libxl_device_vfb_add (libxl_ctx *ctx,uint32_t domid,libxl_device_vfb *vfb); -int libxl_device_vfb_remove (libxl_ctx *ctx,uint32_t domid,libxl_device_vfb *vfb); +int libxl_device_vfb_remove (libxl_ctx *ctx,uint32_t domid, +libxl_device_vfb *vfb, +const libxl_asyncop_how *ao_how);int libxl_device_vfb_destroy (libxl_ctx *ctx,uint32_t domid,libxl_device_vfb *vfb);/* PCI Passthrough */
tools/libxl/libxl_device.c +31 -82
139 lines not on this page · raw message
tools/libxl/libxl_internal.h +5 -25
@@ -655,35 +655,15 @@ _hidden char *libxl__device_backend_path(libxl__gc *gc, libxl__device *device); _hidden char *libxl__device_frontend_path(libxl__gc *gc, libxl__device *device); _hidden int libxl__parse_backend_path(libxl__gc *gc, const char *path,libxl__device *dev); -_hidden int libxl__device_remove(libxl__gc *gc, libxl__device *dev, int wait); _hidden int libxl__device_destroy(libxl__gc *gc, libxl__device *dev); _hidden int libxl__devices_destroy(libxl__gc *gc, uint32_t domid); _hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state); -/* Handler for the libxl__wait_for_device_state callback */ -/* -* libxl__device_state_handler - Handler for the libxl__wait_for_device_state -* gc: allocation pool -* l1: array containing the path and token -* state: string that contains the state of the device -* -* Returns 0 on success, and < 0 on error. -*/ -typedef int libxl__device_state_handler (libxl__gc *gc,char **l1,char *state); - -/* -* libxl__wait_for_device_state - waits a given time for a device to -* reach a given state -* gc: allocation pool -* tv: timeval struct containing the maximum time to wait -* state: state to wait for (check xen/io/xenbus.h) -* handler: callback function to execute when state is reached -* -* Returns 0 on success, and < 0 on error. -*/ -_hidden int libxl__wait_for_device_state(libxl__gc *gc, struct timeval *tv, -XenbusState state, -libxl__device_state_handler handler); +/* Arranges that dev will be removed from its guest. When +* this is done, the ao will be completed. An error +* return from libxl__initiate_device_remove means that the ao +* will _not_ be completed and the caller must do so. */ +_hidden int libxl__initiate_device_remove(libxl__ao*, libxl__device *dev);/* * libxl__ev_devstate - waits a given time for a device to
tools/libxl/xl_cmdimpl.c +2 -2
@@ -4646,7 +4646,7 @@ int main_networkdetach(int argc, char **argv)return 1 ; } } -if (libxl_device_nic_remove (ctx, domid, &nic)) { +if (libxl_device_nic_remove (ctx, domid, &nic,0 )) {fprintf (stderr,"libxl_device_nic_del failed.\n" );return 1 ; }
@@ -4741,7 +4741,7 @@ int main_blockdetach(int argc, char **argv)fprintf (stderr,"Error: Device %s not connected.\n" , argv[optind+1 ]);return 1 ; } -if (libxl_device_disk_remove (ctx, domid, &disk)) { +if (libxl_device_disk_remove (ctx, domid, &disk,0 )) {fprintf (stderr,"libxl_device_disk_remove failed.\n" ); }return 0 ;
tools/ocaml/libs/xl/xenlight_stubs.c +4 -4
@@ -247,7 +247,7 @@ value stub_xl_device_disk_del(value info, value domid)device_disk_val (&gc, &lg, &c_info, info);INIT_CTX (); - ret =libxl_device_disk_remove (ctx,Int_val (domid), &c_info); + ret =libxl_device_disk_remove (ctx,Int_val (domid), &c_info,0 );if (ret !=0 )failwith_xl ("disk_del" , &lg);FREE_CTX ();
@@ -281,7 +281,7 @@ value stub_xl_device_nic_del(value info, value domid)device_nic_val (&gc, &lg, &c_info, info);INIT_CTX (); - ret =libxl_device_nic_remove (ctx,Int_val (domid), &c_info); + ret =libxl_device_nic_remove (ctx,Int_val (domid), &c_info,0 );if (ret !=0 )failwith_xl ("nic_del" , &lg);FREE_CTX ();
@@ -316,7 +316,7 @@ value stub_xl_device_vkb_remove(value info, value domid)device_vkb_val (&gc, &lg, &c_info, info);INIT_CTX (); - ret =libxl_device_vkb_remove (ctx,Int_val (domid), &c_info); + ret =libxl_device_vkb_remove (ctx,Int_val (domid), &c_info,0 );if (ret !=0 )failwith_xl ("vkb_clean_shutdown" , &lg);FREE_CTX ();
@@ -370,7 +370,7 @@ value stub_xl_device_vfb_remove(value info, value domid)device_vfb_val (&gc, &lg, &c_info, info);INIT_CTX (); - ret =libxl_device_vfb_remove (ctx,Int_val (domid), &c_info); + ret =libxl_device_vfb_remove (ctx,Int_val (domid), &c_info,0 );if (ret !=0 )failwith_xl ("vfb_clean_shutdown" , &lg);FREE_CTX ();
Signature · 1 line
1.7.2.5
Quoted 8 lines · Ian Jackson
There are a couple of hopefully uncontroversial new patches on the front of this series:
01/11 .gitignore/.hgignore: New names for ioemu dirs, seabios 02/11 xl: fix a couple of memory leaks
The remainder of the series is very similar to previous versions.
- Changes have been made as discussed on the list, plus:
Given that I'm happy to:
Quoted 24 lines
- One memory leak (missing
GC_FREE) is fixed;
- I have moved a some of the destruction in xl'sdomain_createto reduce false positives from valgrind;
- Fixed the ocaml stubs to pass the new ao argument (currently these stubs are synchronous, which is not ideal but it's not clear what an ocaml caller would want).03/11 libxl: New API for providing OS events to libxl 04/11 ocaml, libxl: support "private" fields
05/11 libxl: New event generation API
06/11 libxl: introducelibxl_fd_set_nonblock, rationalise_cloexec07/11 libxl: Permit multithreaded event waiting
08/11 libxl: Asynchronous/long-running operation infrastructure 09/11 libxl: New convenience macroCONTAINER_OF
10/11 libxl: Introducelibxl__ev_devstate
11/11 libxl: Convert to asynchronous: device removalThe series has also been rebased and retested.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Ian Campbell writes ("Re: [Xen-devel] [PATCH v8 00/11] libxl: New event API"):
Quoted 12 lines · Ian Jackson
Quoted 8 lines
There are a couple of hopefully uncontroversial new patches on the front of this series:
01/11 .gitignore/.hgignore: New names for ioemu dirs, seabios 02/11 xl: fix a couple of memory leaks
The remainder of the series is very similar to previous versions.
- Changes have been made as discussed on the list, plus:Given that I'm happy to:
Acked-by Ian Campbell <[email protected]>
Thanks, I have applied all 11.
Ian.