Per-domain locking in xl
cover Per-domain locking in xl
It has always been the case that different xl processes can manipulate the same domain at the same time. This could be problematic.
This series attempts to provide facility for xl to have a per-domain lock. This lock should be used whenever xl manipulates an existing domain.
The last patch is an example on using the facility and serves as an example for further refactoring. The refactoring is a bit tedious so I would like to know if people are happy with this approach before working on it further.
Wei Liu (4):
xl: move {acquire,release}_lock to xl_utils.c
xl: make lock functions work with arbitrary files and fds
xl: introduce facility to run function with per-domain lock held XXX a command to test the locking facility
tools/xl/xl.c | 19 ++++++----- tools/xl/xl.h | 6 +++- tools/xl/xl_cmdtable.c | 5 +++ tools/xl/xl_misc.c | 37 +++++++++++++++++++++ tools/xl/xl_utils.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/xl/xl_utils.h | 6 ++++ tools/xl/xl_vmcontrol.c | 73 ++-------------------------------------- 7 files changed, 154 insertions(+), 80 deletions(-)
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
RFC PATCH 1/4 xl: move {acquire, release}_lock to xl_utils.c
Pure code motion, no functional change.
--- tools/xl/xl_utils.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/xl/xl_utils.h | 3 +++ tools/xl/xl_vmcontrol.c | 67 ------------------------------------------------- 3 files changed, 70 insertions(+), 67 deletions(-)
tools/xl/xl_utils.c +67 -0
@@ -316,6 +316,73 @@ out:return ret; } +static int fd_lock =-1 ; + +int acquire_lock (void ) +{ +int rc; +struct flock fl; + +/* lock already acquired */ +if (fd_lock >=0 ) +return ERROR_INVAL ; + + fl.l_type =F_WRLCK ; + fl.l_whence =SEEK_SET ; + fl.l_start =0 ; + fl.l_len =0 ; + fd_lock =open (lockfile,O_WRONLY |O_CREAT ,S_IWUSR ); +if (fd_lock <0 ) { +fprintf (stderr,"cannot open the lockfile %s errno=%d\n" , lockfile, errno); +return ERROR_FAIL ; + } +if (fcntl (fd_lock,F_SETFD ,FD_CLOEXEC ) <0 ) { +close (fd_lock); +fprintf (stderr,"cannot set cloexec to lockfile %s errno=%d\n" , lockfile, errno); +return ERROR_FAIL ; + } +get_lock: + rc =fcntl (fd_lock,F_SETLKW , &fl); +if (rc <0 && errno ==EINTR ) + goto get_lock; +if (rc <0 ) { +fprintf (stderr,"cannot acquire lock %s errno=%d\n" , lockfile, errno); + rc =ERROR_FAIL ; + }else + rc =0 ; +return rc; +} + +int release_lock (void ) +{ +int rc; +struct flock fl; + +/* lock not acquired */ +if (fd_lock <0 ) +return ERROR_INVAL ; + +release_lock: + fl.l_type =F_UNLCK ; + fl.l_whence =SEEK_SET ; + fl.l_start =0 ; + fl.l_len =0 ; + + rc =fcntl (fd_lock,F_SETLKW , &fl); +if (rc <0 && errno ==EINTR ) + goto release_lock; +if (rc <0 ) { +fprintf (stderr,"cannot release lock %s, errno=%d\n" , lockfile, errno); + rc =ERROR_FAIL ; + }else + rc =0 ; +close (fd_lock); + fd_lock =-1 ; + +return rc; +} + +/* * Local variables: * mode: C
tools/xl/xl_utils.h +3 -0
@@ -146,6 +146,9 @@ uint32_t find_domain(const char *p) __attribute__((warn_unused_result));void print_bitmap (uint8_t *map,int maplen,FILE *stream);int do_daemonize (char *name,const char *pidfile); + +int acquire_lock (void ); +int release_lock (void );#endif /* XL_UTILS_H */ /*
tools/xl/xl_vmcontrol.c +0 -67
79 lines not on this page · raw message
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
RFC PATCH 2/4 xl: make lock functions work with arbitrary files and fds
In reply to Wei LiuRename the existing lock to xl_global lock. Refactor the functions to take the filename and fd so that they can work with any filename and fd.
No functional change.
--- tools/xl/xl.c | 19 ++++++++++--------- tools/xl/xl.h | 3 ++- tools/xl/xl_utils.c | 38 ++++++++++++++++++++------------------ tools/xl/xl_utils.h | 4 ++-- tools/xl/xl_vmcontrol.c | 6 +++--- 5 files changed, 37 insertions(+), 33 deletions(-)
tools/xl/xl.c +10 -9
@@ -35,7 +35,8 @@ int force_execution;int autoballoon =-1 ;char *blkdev_start;int run_hotplug_scripts =1 ; -char *lockfile; +char *xl_global_lockfile; +int xl_global_fd_lock =-1 ;char *default_vifscript =NULL ;char *default_bridge =NULL ;char *default_gatewaydev =NULL ;
@@ -117,14 +118,14 @@ static void parse_global_config(const char *configfile,if (!xlu_cfg_get_long (config,"run_hotplug_scripts" , &l,0 )) run_hotplug_scripts = l; -if (!xlu_cfg_get_string (config,"lockfile" , &buf,0 )) - lockfile =strdup (buf); +if (!xlu_cfg_get_string (config,"xl_global_lockfile" , &buf,0 )) + xl_global_lockfile =strdup (buf);else { - lockfile =strdup (XL_LOCK_FILE ); + xl_global_lockfile =strdup (XL_LOCK_FILE ); } -if (!lockfile) { -fprintf (stderr,"failed to allocate lockfile\n" ); +if (!xl_global_lockfile) { +fprintf (stderr,"failed to allocate xl_global_lockfile\n" );exit (1 ); }
@@ -295,9 +296,9 @@ static void xl_ctx_free(void)xtl_logger_destroy ((xentoollog_logger *)logger); logger =NULL ; } -if (lockfile) { -free (lockfile); - lockfile =NULL ; +if (xl_global_lockfile) { +free (xl_global_lockfile); + xl_global_lockfile =NULL ; } }
tools/xl/xl.h +2 -1
@@ -265,7 +265,8 @@ extern int claim_mode;extern bool progress_use_cr;extern xentoollog_level minmsglevel;#define minmsglevel_default XTL_PROGRESS -extern char *lockfile; +extern char *xl_global_lockfile; +extern int xl_global_fd_lock;extern char *default_vifscript;extern char *default_bridge;extern char *default_gatewaydev;
tools/xl/xl_utils.c +20 -18
@@ -316,50 +316,51 @@ out:return ret; } -static int fd_lock =-1 ; - -int acquire_lock (void ) +int acquire_lock (const char *lockfile,int *fd_lock) {int rc;struct flock fl;/* lock already acquired */ -if (fd_lock >=0 ) +if (*fd_lock >=0 )return ERROR_INVAL ; fl.l_type =F_WRLCK ; fl.l_whence =SEEK_SET ; fl.l_start =0 ; fl.l_len =0 ; - fd_lock =open (lockfile,O_WRONLY |O_CREAT ,S_IWUSR ); -if (fd_lock <0 ) { -fprintf (stderr,"cannot open the lockfile %s errno=%d\n" , lockfile, errno); + *fd_lock =open (lockfile,O_WRONLY |O_CREAT ,S_IWUSR ); +if (*fd_lock <0 ) { +fprintf (stderr,"cannot open the lockfile %s errno=%d\n" , + lockfile, errno);return ERROR_FAIL ; } -if (fcntl (fd_lock,F_SETFD ,FD_CLOEXEC ) <0 ) { -close (fd_lock); -fprintf (stderr,"cannot set cloexec to lockfile %s errno=%d\n" , lockfile, errno); +if (fcntl (*fd_lock,F_SETFD ,FD_CLOEXEC ) <0 ) { +close (*fd_lock); +fprintf (stderr,"cannot set cloexec to lockfile %s errno=%d\n" , + lockfile, errno);return ERROR_FAIL ; } get_lock: - rc =fcntl (fd_lock,F_SETLKW , &fl); + rc =fcntl (*fd_lock,F_SETLKW , &fl);if (rc <0 && errno ==EINTR ) goto get_lock;if (rc <0 ) { -fprintf (stderr,"cannot acquire lock %s errno=%d\n" , lockfile, errno); +fprintf (stderr,"cannot acquire lock %s errno=%d\n" , + lockfile, errno); rc =ERROR_FAIL ; }else rc =0 ;return rc; } -int release_lock (void ) +int release_lock (const char *lockfile,int *fd_lock) {int rc;struct flock fl;/* lock not acquired */ -if (fd_lock <0 ) +if (*fd_lock <0 )return ERROR_INVAL ; release_lock:
@@ -368,16 +369,17 @@ release_lock: fl.l_start =0 ; fl.l_len =0 ; - rc =fcntl (fd_lock,F_SETLKW , &fl); + rc =fcntl (*fd_lock,F_SETLKW , &fl);if (rc <0 && errno ==EINTR ) goto release_lock;if (rc <0 ) { -fprintf (stderr,"cannot release lock %s, errno=%d\n" , lockfile, errno); +fprintf (stderr,"cannot release lock %s, errno=%d\n" , + lockfile, errno); rc =ERROR_FAIL ; }else rc =0 ; -close (fd_lock); - fd_lock =-1 ; +close (*fd_lock); + *fd_lock =-1 ;return rc; }
tools/xl/xl_utils.h +2 -2
@@ -147,8 +147,8 @@ void print_bitmap(uint8_t *map, int maplen, FILE *stream);int do_daemonize (char *name,const char *pidfile); -int acquire_lock (void ); -int release_lock (void ); +int acquire_lock (const char *lockfile,int *fd_lock); +int release_lock (const char *lockfile,int *fd_lock);#endif /* XL_UTILS_H */ /*
tools/xl/xl_vmcontrol.c +3 -3
@@ -776,7 +776,7 @@ int create_domain(struct domain_create *dom_info) start:assert (domid ==INVALID_DOMID ); - rc =acquire_lock (); + rc =acquire_lock (xl_global_lockfile, &xl_global_fd_lock);if (rc <0 ) goto error_out;
@@ -838,7 +838,7 @@ start:if ( ret ) goto error_out; -release_lock (); +release_lock (xl_global_lockfile, &xl_global_fd_lock);if (restore_fd_to_close >=0 ) {if (close (restore_fd_to_close))
@@ -1012,7 +1012,7 @@ start: } error_out: -release_lock (); +release_lock (xl_global_lockfile, &xl_global_fd_lock);if (libxl_domid_valid_guest (domid)) {libxl_domain_destroy (ctx, domid,0 ); domid =INVALID_DOMID ;
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
RFC PATCH 3/4 xl: introduce facility to run function with per-domain lock held
In reply to Wei Liu--- tools/xl/xl.h | 1 + tools/xl/xl_utils.c | 19 +++++++++++++++++++ tools/xl/xl_utils.h | 3 +++ 3 files changed, 23 insertions(+)
tools/xl/xl.h +1 -0
@@ -292,6 +292,7 @@ extern void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh)#define XL_GLOBAL_CONFIG XEN_CONFIG_DIR "/xl.conf"#define XL_LOCK_FILE XEN_LOCK_DIR "/xl" +#define XL_DOMAIN_LOCK_FILE_FMT XEN_LOCK_DIR "/xl-%u"#endif /* XL_H */
tools/xl/xl_utils.c +19 -0
@@ -27,6 +27,25 @@#include "xl.h" #include "xl_utils.h" +int with_lock (uint32_t domid,domain_fn fn,void *arg) +{ +char filename[sizeof (XL_DOMAIN_LOCK_FILE_FMT )+15 ]; +int fd_lock =-1 ; +int rc; + +snprintf (filename,sizeof (filename),XL_DOMAIN_LOCK_FILE_FMT , domid); + + rc =acquire_lock (filename, &fd_lock); +if (rc) goto out; + + rc =fn (arg); + +release_lock (filename, &fd_lock); + +out: +return rc; +} +void dolog (const char *file,int line,const char *func,char *fmt, ...) {va_list ap;
tools/xl/xl_utils.h +3 -0
@@ -149,6 +149,9 @@ int do_daemonize(char *name, const char *pidfile);int acquire_lock (const char *lockfile,int *fd_lock);int release_lock (const char *lockfile,int *fd_lock); + +typedef int (*domain_fn )(void *arg); +int with_lock (uint32_t domid,domain_fn fn,void *arg);#endif /* XL_UTILS_H */ /*
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [RFC PATCH 3/4] xl: introduce facility to run function with per-domain lock held
Quoted 28 lines · Wei Liu
Signed-off-by Wei Liu <[email protected]>--- tools/xl/xl.h | 1 + tools/xl/xl_utils.c | 19 +++++++++++++++++++ tools/xl/xl_utils.h | 3 +++ 3 files changed, 23 insertions(+)tools/xl/xl.h +1
-0@@ -292,6 +292,7 @@ extern void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh)#define XL_GLOBAL_CONFIG XEN_CONFIG_DIR "/xl.conf"#define XL_LOCK_FILE XEN_LOCK_DIR "/xl" +#define XL_DOMAIN_LOCK_FILE_FMT XEN_LOCK_DIR "/xl-%u"#endif /* XL_H */ tools/xl/xl_utils.c +1
-0@@ -27,6 +27,25 @@#include "xl.h" #include "xl_utils.h" +int with_lock (uint32_t domid,domain_fn fn,void *arg)
tools/xl/xl_utils.c line 40
Quoted 10 lines
tools/xl/xl_utils.c +10
-0+{ +char filename[sizeof (XL_DOMAIN_LOCK_FILE_FMT )+15 ]; +int fd_lock =-1 ; +int rc; + +snprintf (filename,sizeof (filename),XL_DOMAIN_LOCK_FILE_FMT , domid); + + rc =acquire_lock (filename, &fd_lock); +if (rc) goto out; +
It is necessary to check if the domain is still valid here. And we should probably accept a string instead of domid in this function and call find_domain, so that we can retry. Basically:
retry:
domid = find_domain();
snprintf(...)
rc = acquire_lock()
if (rc) goto out; if (domain is not valid anymore) {
release_lock();
goto retry;
}
/* ... the rest ...*/_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
RFC PATCH 4/4 XXX a command to test the locking facility
In reply to Wei Liu--- tools/xl/xl.h | 2 ++ tools/xl/xl_cmdtable.c | 5 +++++ tools/xl/xl_misc.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+)
tools/xl/xl.h +2 -0
@@ -209,6 +209,8 @@ int main_psr_cat_show(int argc, char **argv);#endif int main_qemu_monitor_command (int argc,char **argv); +int main_lock (int argc,char **argv); +void help (const char *command);extern const char *common_domname;
tools/xl/xl_cmdtable.c +5 -0
@@ -591,6 +591,11 @@ struct cmd_spec cmd_table[] = { "Issue a qemu monitor command to the device model of a domain", "<Domain> <Command>", }, + {"lock" , + &main_lock,0 ,0 , +"Lock a domain, prevent other xl processes from manipulating it" , + "<Domain>", + }, };int cmdtable_len =sizeof (cmd_table)/sizeof (struct cmd_spec );
tools/xl/xl_misc.c +37 -0
@@ -14,6 +14,7 @@#include <limits.h> #include <stdlib.h> +#include <unistd.h> #include <libxl.h> #include <libxl_utils.h>
@@ -344,6 +345,42 @@ int main_config_update(int argc, char **argv)return 0 ; } +struct lock_arg { +uint32_t domid; +}; + +static int lock_fn (void *p) +{ +struct lock_arg *arg = p; + +fprintf (stderr,"Now I have the lock for %u\n" , arg->domid); + +sleep (10 ); + +fprintf (stderr,"Done\n" ); + +return 0 ; +} + +int main_lock (int argc,char **argv) +{ +uint32_t domid; +struct lock_arg arg; +int rc; + + domid =find_domain (argv[optind++]); + +fprintf (stderr,"About to lock %u\n" , domid); + + arg.domid = domid; + + rc =with_lock (domid, lock_fn, &arg); +fprintf (stderr,"with_lock returned, rc = %d\n" , rc); +if (rc) +return EXIT_FAILURE ; +return EXIT_SUCCESS ; +} +/* * Local variables: * mode: C
Signature · 7 lines
2.11.0 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Wei Liu writes ("[RFC PATCH 0/4] Per-domain locking in xl"):
Quoted 7 lines
It has always been the case that different xl processes can manipulate the same domain at the same time. This could be problematic.
This series attempts to provide facility for xl to have a per-domain lock. This lock should be used whenever xl manipulates an existing domain.
Unfortunately, this has to be done in a much more complicated way.
fcntl(,F_SETLKW,) is a slow' function within the meaning of the comment Machinery for asynchronous operations ("ao")' at libxl_internal.h:2100.
So acquisition of the guest lock needs to be done in callback style. Since fcntl(,F_SETLKW,) is always synchronous, I think this may need the use of fork or threads. The owner of a fcntl lock is a process, so fork won't do - but you could use flock, which is owned by an open-file (and that would avoid threads). If you do use fork you should probably try F_SETLK first so that the fast path does not involve forking.
Further observations:
We might well want new macros to help with the resulting boilerplate in domain mutation initiators.
In general we would want to avoid explicit specification of the domid, per se; rather we should pass a "locked-domain" object to any mutating sub-functions. Or we could put the locked-domain in the ctx or the ao. This needs further thought I think.
You need to clarify the status of the new lock in the lock hierarchy. (When it was outside libxl, the lock hierarchy was implicit but clear.)
The rules must forbid locking two guests at once, because of the possibility of deadlock. (An exception is migration: the destination domain is created in locked state, so no deadlock is possible.)
We must consider how this interacts with stubdoms. If a guest has a stubdom, does the stubdom have its own lock ? If so, what is the lock order ?
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Wei Liu <[email protected]> 06/14/17 7:19 PM >>>
It has always been the case that different xl processes can manipulate the same domain at the same time. This could be problematic.
This series attempts to provide facility for xl to have a per-domain lock. This lock should be used whenever xl manipulates an existing domain.
Aiui this is in response to the guest-reboot-while-migrating issue. However, wouldn't migration in such a case better be aborted, the guest allowed to shut down, and it be restarted on the destination host (the last step omitted if the guest is shutting down instead of rebooting)?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel