[XEN,for-4.13,v3,3/7] libxl: Rename ev_devlock to ev_slowlock
diff mbox series

Message ID 20191118171309.1459302-4-anthony.perard@citrix.com
State New
Headers show
Series
  • Fix: libxl workaround, multiple connection to single QMP socket
Related show

Commit Message

Anthony PERARD Nov. 18, 2019, 5:13 p.m. UTC
We are going to introduce a different lock based on the same
implementation as the ev_devlock but with a different path. The
different slowlock will be differentiated by calling different _init()
functions.

So we rename libxl__ev_devlock to lib__ev_slowlock, but keep
libxl__ev_devlock_init().

Some log messages produced ev_slowlock are changed to print the
name of the lock file (userdata_userid).

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---

Notes:
    New patch in v2.
        Instead of introducing many libxl__ev_qmplock*.

 tools/libxl/libxl_disk.c     | 10 +++++-----
 tools/libxl/libxl_domain.c   | 10 +++++-----
 tools/libxl/libxl_internal.c | 30 +++++++++++++++++++-----------
 tools/libxl/libxl_internal.h | 33 +++++++++++++++++----------------
 4 files changed, 46 insertions(+), 37 deletions(-)

Patch
diff mbox series

diff --git a/tools/libxl/libxl_disk.c b/tools/libxl/libxl_disk.c
index 733ad284c866..77ae3a59bfb6 100644
--- a/tools/libxl/libxl_disk.c
+++ b/tools/libxl/libxl_disk.c
@@ -648,13 +648,13 @@  typedef struct {
     libxl_domid domid;
     libxl_device_disk *disk;
     libxl_device_disk disk_saved;
-    libxl__ev_devlock qmp_lock;
+    libxl__ev_slowlock qmp_lock;
     int dm_ver;
     libxl__ev_time time;
     libxl__ev_qmp qmp;
 } libxl__cdrom_insert_state;
 
-static void cdrom_insert_lock_acquired(libxl__egc *, libxl__ev_devlock *,
+static void cdrom_insert_lock_acquired(libxl__egc *, libxl__ev_slowlock *,
                                        int rc);
 static void cdrom_insert_ejected(libxl__egc *egc, libxl__ev_qmp *,
                                  const libxl__json_object *, int rc);
@@ -746,13 +746,13 @@  int libxl_cdrom_insert(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk,
         cdrom_insert_done(egc, cis, rc); /* must be last */
     } else {
         cis->qmp_lock.callback = cdrom_insert_lock_acquired;
-        libxl__ev_devlock_lock(egc, &cis->qmp_lock); /* must be last */
+        libxl__ev_slowlock_lock(egc, &cis->qmp_lock); /* must be last */
     }
     return AO_INPROGRESS;
 }
 
 static void cdrom_insert_lock_acquired(libxl__egc *egc,
-                                       libxl__ev_devlock *lock,
+                                       libxl__ev_slowlock *lock,
                                        int rc)
 {
     libxl__cdrom_insert_state *cis = CONTAINER_OF(lock, *cis, qmp_lock);
@@ -1052,7 +1052,7 @@  static void cdrom_insert_done(libxl__egc *egc,
     libxl__ev_time_deregister(gc, &cis->time);
     libxl__ev_qmp_dispose(gc, &cis->qmp);
     if (cis->qmp.payload_fd >= 0) close(cis->qmp.payload_fd);
-    libxl__ev_devlock_unlock(gc, &cis->qmp_lock);
+    libxl__ev_slowlock_unlock(gc, &cis->qmp_lock);
     libxl_device_disk_dispose(&cis->disk_saved);
     libxl__ao_complete(egc, cis->ao, rc);
 }
diff --git a/tools/libxl/libxl_domain.c b/tools/libxl/libxl_domain.c
index 33f9d9eaa481..7ca7a224f9a4 100644
--- a/tools/libxl/libxl_domain.c
+++ b/tools/libxl/libxl_domain.c
@@ -1874,12 +1874,12 @@  typedef struct {
     libxl__ev_qmp qmp;
     libxl__ev_time timeout;
     libxl_domain_config *d_config; /* user pointer */
-    libxl__ev_devlock devlock;
+    libxl__ev_slowlock devlock;
     libxl_bitmap qemuu_cpus;
 } retrieve_domain_configuration_state;
 
 static void retrieve_domain_configuration_lock_acquired(
-    libxl__egc *egc, libxl__ev_devlock *, int rc);
+    libxl__egc *egc, libxl__ev_slowlock *, int rc);
 static void retrieve_domain_configuration_cpu_queried(
     libxl__egc *egc, libxl__ev_qmp *qmp,
     const libxl__json_object *response, int rc);
@@ -1907,12 +1907,12 @@  int libxl_retrieve_domain_configuration(libxl_ctx *ctx, uint32_t domid,
     rdcs->devlock.ao = ao;
     rdcs->devlock.domid = domid;
     rdcs->devlock.callback = retrieve_domain_configuration_lock_acquired;
-    libxl__ev_devlock_lock(egc, &rdcs->devlock);
+    libxl__ev_slowlock_lock(egc, &rdcs->devlock);
     return AO_INPROGRESS;
 }
 
 static void retrieve_domain_configuration_lock_acquired(
-    libxl__egc *egc, libxl__ev_devlock *devlock, int rc)
+    libxl__egc *egc, libxl__ev_slowlock *devlock, int rc)
 {
     retrieve_domain_configuration_state *rdcs =
         CONTAINER_OF(devlock, *rdcs, devlock);
@@ -2204,7 +2204,7 @@  static void retrieve_domain_configuration_end(libxl__egc *egc,
     }
 
 out:
-    libxl__ev_devlock_unlock(gc, &rdcs->devlock);
+    libxl__ev_slowlock_unlock(gc, &rdcs->devlock);
     if (lock) libxl__unlock_domain_userdata(lock);
     libxl_bitmap_dispose(&rdcs->qemuu_cpus);
     libxl__ev_qmp_dispose(gc, &rdcs->qmp);
diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c
index 0750b69cba61..9520ac36149e 100644
--- a/tools/libxl/libxl_internal.c
+++ b/tools/libxl/libxl_internal.c
@@ -575,25 +575,32 @@  void libxl__update_domain_configuration(libxl__gc *gc,
     dst->b_info.video_memkb = src->b_info.video_memkb;
 }
 
-void libxl__ev_devlock_init(libxl__ev_devlock *lock)
+static void ev_slowlock_init_internal(libxl__ev_slowlock *lock,
+                                      const char *userdata_userid)
 {
     libxl__ev_child_init(&lock->child);
+    lock->userdata_userid = userdata_userid;
     lock->path = NULL;
     lock->fd = -1;
     lock->held = false;
 }
 
-static void ev_lock_prepare_fork(libxl__egc *egc, libxl__ev_devlock *lock);
+void libxl__ev_devlock_init(libxl__ev_slowlock *lock)
+{
+    ev_slowlock_init_internal(lock, "libxl-device-changes-lock");
+}
+
+static void ev_lock_prepare_fork(libxl__egc *egc, libxl__ev_slowlock *lock);
 static void ev_lock_child_callback(libxl__egc *egc, libxl__ev_child *child,
                                    pid_t pid, int status);
 
-void libxl__ev_devlock_lock(libxl__egc *egc, libxl__ev_devlock *lock)
+void libxl__ev_slowlock_lock(libxl__egc *egc, libxl__ev_slowlock *lock)
 {
     STATE_AO_GC(lock->ao);
     const char *lockfile;
 
     lockfile = libxl__userdata_path(gc, lock->domid,
-                                    "libxl-device-changes-lock", "l");
+                                    lock->userdata_userid, "l");
     if (!lockfile) goto out;
     lock->path = libxl__strdup(NOGC, lockfile);
 
@@ -603,7 +610,7 @@  void libxl__ev_devlock_lock(libxl__egc *egc, libxl__ev_devlock *lock)
     lock->callback(egc, lock, ERROR_LOCK_FAIL);
 }
 
-static void ev_lock_prepare_fork(libxl__egc *egc, libxl__ev_devlock *lock)
+static void ev_lock_prepare_fork(libxl__egc *egc, libxl__ev_slowlock *lock)
 {
     STATE_AO_GC(lock->ao);
     pid_t pid;
@@ -670,7 +677,7 @@  static void ev_lock_prepare_fork(libxl__egc *egc, libxl__ev_devlock *lock)
     libxl_fd_set_cloexec(CTX, fd, 1);
     return;
 out:
-    libxl__ev_devlock_unlock(gc, lock);
+    libxl__ev_slowlock_unlock(gc, lock);
     lock->callback(egc, lock, ERROR_LOCK_FAIL);
 }
 
@@ -678,7 +685,7 @@  static void ev_lock_child_callback(libxl__egc *egc, libxl__ev_child *child,
                                    pid_t pid, int status)
 {
     EGC_GC;
-    libxl__ev_devlock *lock = CONTAINER_OF(child, *lock, child);
+    libxl__ev_slowlock *lock = CONTAINER_OF(child, *lock, child);
     struct stat stab, fstab;
     int rc = ERROR_LOCK_FAIL;
 
@@ -726,13 +733,14 @@  static void ev_lock_child_callback(libxl__egc *egc, libxl__ev_child *child,
             rc = ERROR_LOCK_FAIL;
     }
     if (rc) {
-        LOGD(ERROR, domid, "Failed to grab qmp-lock");
-        libxl__ev_devlock_unlock(gc, lock);
+        LOGD(ERROR, domid, "Failed to grab lock for %s",
+             lock->userdata_userid);
+        libxl__ev_slowlock_unlock(gc, lock);
     }
     lock->callback(egc, lock, rc);
 }
 
-void libxl__ev_devlock_unlock(libxl__gc *gc, libxl__ev_devlock *lock)
+void libxl__ev_slowlock_unlock(libxl__gc *gc, libxl__ev_slowlock *lock)
 {
     int r;
 
@@ -754,7 +762,7 @@  void libxl__ev_devlock_unlock(libxl__gc *gc, libxl__ev_devlock *lock)
         close(lock->fd);
     }
     free(lock->path);
-    libxl__ev_devlock_init(lock);
+    ev_slowlock_init_internal(lock, lock->userdata_userid);
 }
 
 /*
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 69d572c1866a..a0f99252c39c 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -196,7 +196,7 @@  typedef struct libxl__osevent_hook_nexi libxl__osevent_hook_nexi;
 typedef struct libxl__device_type libxl__device_type;
 typedef struct libxl__json_object libxl__json_object;
 typedef struct libxl__carefd libxl__carefd;
-typedef struct libxl__ev_devlock libxl__ev_devlock;
+typedef struct libxl__ev_slowlock libxl__ev_slowlock;
 typedef struct libxl__dm_resume_state libxl__dm_resume_state;
 typedef struct libxl__ao_device libxl__ao_device;
 typedef struct libxl__multidev libxl__multidev;
@@ -366,7 +366,7 @@  struct libxl__ev_child {
 /*
  * Lock for device hotplug, qmp_lock.
  *
- * libxl__ev_devlock implement a lock that is outside of CTX_LOCK in the
+ * libxl__ev_slowlock implement a lock that is outside of CTX_LOCK in the
  * lock hierarchy. It can be used when one want to make QMP calls to QEMU,
  * which may take a significant amount time.
  * It is to be acquired by an ao event callback.
@@ -374,42 +374,43 @@  struct libxl__ev_child {
  * It is to be acquired when adding/removing devices or making changes
  * to them when this is a slow operation and json_lock isn't appropriate.
  *
- * Possible states of libxl__ev_devlock:
+ * Possible states of libxl__ev_slowlock:
  *   Undefined
  *    Might contain anything.
  *  Idle
  *    Struct contents are defined enough to pass to any
- *    libxl__ev_devlock_* function.
+ *    libxl__ev_slowlock_* function.
  *    The struct does not contain references to any allocated private
  *    resources so can be thrown away.
  *  Active
  *    Waiting to get a lock.
  *    Needs to wait until the callback is called.
  *  LockAcquired
- *    libxl__ev_devlock_unlock will need to be called to release the lock
- *    and the resources of libxl__ev_devlock.
+ *    libxl__ev_slowlock_unlock will need to be called to release the lock
+ *    and the resources of libxl__ev_slowlock.
  *
- *  libxl__ev_devlock_init: Undefined/Idle -> Idle
- *  libxl__ev_devlock_lock: Idle -> Active
+ *  libxl__ev_*lock_init: Undefined/Idle -> Idle
+ *  libxl__ev_slowlock_lock: Idle -> Active
  *    May call callback synchronously.
- *  libxl__ev_devlock_unlock: LockAcquired/Idle -> Idle
+ *  libxl__ev_slowlock_unlock: LockAcquired/Idle -> Idle
  *  callback:     When called: Active -> LockAcquired (on error: Idle)
  *    The callback is only called once.
  */
-struct libxl__ev_devlock {
+struct libxl__ev_slowlock {
     /* filled by user */
     libxl__ao *ao;
     libxl_domid domid;
-    void (*callback)(libxl__egc *, libxl__ev_devlock *, int rc);
-    /* private to libxl__ev_devlock* */
+    void (*callback)(libxl__egc *, libxl__ev_slowlock *, int rc);
+    /* private to libxl__ev_slowlock* */
     libxl__ev_child child;
+    const char *userdata_userid;
     char *path; /* path of the lock file itself */
     int fd;
     bool held;
 };
-_hidden void libxl__ev_devlock_init(libxl__ev_devlock *);
-_hidden void libxl__ev_devlock_lock(libxl__egc *, libxl__ev_devlock *);
-_hidden void libxl__ev_devlock_unlock(libxl__gc *, libxl__ev_devlock *);
+_hidden void libxl__ev_devlock_init(libxl__ev_slowlock *);
+_hidden void libxl__ev_slowlock_lock(libxl__egc *, libxl__ev_slowlock *);
+_hidden void libxl__ev_slowlock_unlock(libxl__gc *, libxl__ev_slowlock *);
 
 /*
  * QMP asynchronous calls
@@ -2853,7 +2854,7 @@  struct libxl__multidev {
  *   unlock json config
  *
  * Or in case QEMU is the primary config, this pattern can be use:
- *   qmp_lock (libxl__ev_devlock)
+ *   qmp_lock (libxl__ev_devlock_init)
  *      lock json config (json_lock)
  *          read json config
  *          update in-memory json config with new entry, replacing