commit fa58f571ee0b2ab72f8a5a19d35298d6de6469b3
Author: Jim Fehlig <jfehlig@suse.com>
Date:   Fri Feb 19 14:58:19 2021 -0700

    libxl: Add lock process indicator to libxlDomainObjPrivate object
    
    The libvirt libxl driver has no access to FDs associated with VM disks.
    The disks are opened by libxl.so and any related FDs are not exposed to
    applications. The prevents using virtlockd's auto-release feature to
    release locks when the FD is closed. Acquiring and releasing locks is
    explicitly handled by the libxl driver.
    
    The current logic is structured such that locks are acquired in
    libxlDomainStart and released in libxlDomainCleanup. This works well
    except for migration, where the locks must be released on the source
    host before the domain can be started on the destination host, but the
    domain cannot be cleaned up until the migration confirmation stage.
    When libxlDomainCleanup if finally called in the confirm stage, locks
    are again released resulting in confusing errors from virtlockd and
    libvirtd
    
    virtlockd[8095]: resource busy: Lockspace resource 'xxxxxx' is not locked
    libvirtd[8050]: resource busy: Lockspace resource 'xxxxxx' is not locked
    libvirtd[8050]: Unable to release lease on testvm
    
    The error is also encountered in some error cases, e.g. when
    libxlDomainStart fails before acquiring locks and libxlDomainCleanup
    is still used for cleanup.
    
    In lieu of a mechanism to check if a lock has been acquired, this patch
    takes an easy approach to fixing the unnecessary lock releases by adding
    an indicator to the libxlDomainPrivate object that can be set when the
    lock is acquired and cleared when the lock is released. libxlDomainCleanup
    can then skip releasing the lock in cases where it was previously released
    or never acquired in the first place.
    
    Signed-off-by: Jim Fehlig <jfehlig@suse.com>
    Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Index: libvirt-6.0.0/src/libxl/libxl_domain.c
===================================================================
--- libvirt-6.0.0.orig/src/libxl/libxl_domain.c
+++ libvirt-6.0.0/src/libxl/libxl_domain.c
@@ -857,10 +857,14 @@ libxlDomainCleanup(libxlDriverPrivatePtr
     virHostdevReAttachDomainDevices(hostdev_mgr, LIBXL_DRIVER_NAME,
                                     vm->def, hostdev_flags, NULL);
 
-    VIR_FREE(priv->lockState);
-    if (virDomainLockProcessPause(driver->lockManager, vm, &priv->lockState) < 0)
-        VIR_WARN("Unable to release lease on %s", vm->def->name);
-    VIR_DEBUG("Preserving lock state '%s'", NULLSTR(priv->lockState));
+    if (priv->lockProcessRunning) {
+        VIR_FREE(priv->lockState);
+        if (virDomainLockProcessPause(driver->lockManager, vm, &priv->lockState) < 0)
+            VIR_WARN("Unable to release lease on %s", vm->def->name);
+        else
+            priv->lockProcessRunning = false;
+        VIR_DEBUG("Preserving lock state '%s'", NULLSTR(priv->lockState));
+    }
 
     libxlLoggerCloseFile(cfg->logger, vm->def->id);
     vm->def->id = -1;
@@ -1431,6 +1435,7 @@ libxlDomainStart(libxlDriverPrivatePtr d
                                   priv->lockState) < 0)
         goto destroy_dom;
     VIR_FREE(priv->lockState);
+    priv->lockProcessRunning = true;
 
     /* Always enable domain death events */
     if (libxl_evenable_domain_death(cfg->ctx, vm->def->id, 0, &priv->deathW))
Index: libvirt-6.0.0/src/libxl/libxl_domain.h
===================================================================
--- libvirt-6.0.0.orig/src/libxl/libxl_domain.h
+++ libvirt-6.0.0/src/libxl/libxl_domain.h
@@ -68,6 +68,7 @@ struct _libxlDomainObjPrivate {
     virThreadPtr migrationDstReceiveThr;
     unsigned short migrationPort;
     char *lockState;
+    bool lockProcessRunning;
 
     struct libxlDomainJobObj job;
 
Index: libvirt-6.0.0/src/libxl/libxl_migration.c
===================================================================
--- libvirt-6.0.0.orig/src/libxl/libxl_migration.c
+++ libvirt-6.0.0/src/libxl/libxl_migration.c
@@ -1242,9 +1242,12 @@ libxlDomainMigrationSrcPerform(libxlDriv
     virObjectLock(vm);
 
     if (ret == 0) {
-        if (virDomainLockProcessPause(driver->lockManager, vm, &priv->lockState) < 0)
+        if (virDomainLockProcessPause(driver->lockManager, vm, &priv->lockState) == 0) {
+            priv->lockProcessRunning = false;
+            VIR_DEBUG("Preserving lock state '%s'", NULLSTR(priv->lockState));
+        } else {
             VIR_WARN("Unable to release lease on %s", vm->def->name);
-        VIR_DEBUG("Preserving lock state '%s'", NULLSTR(priv->lockState));
+        }
     } else {
         /*
          * Confirm phase will not be executed if perform fails. End the
@@ -1377,6 +1380,7 @@ libxlDomainMigrationSrcConfirm(libxlDriv
                                    "xen:///system",
                                    vm,
                                    priv->lockState);
+        priv->lockProcessRunning = true;
         if (libxl_domain_resume(cfg->ctx, vm->def->id, 1, 0) == 0) {
             ret = 0;
         } else {
