[libvirt PATCH 25/80] qemu: Move success-only code out of endjob in qemuMigrationDstFinish

Jiri Denemark posted 80 patches 3 years, 9 months ago
There is a newer version of this series
[libvirt PATCH 25/80] qemu: Move success-only code out of endjob in qemuMigrationDstFinish
Posted by Jiri Denemark 3 years, 9 months ago
Code executed only when dom != NULL can be moved before "endjob" label,
to the only place where dom is set.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/qemu/qemu_migration.c | 50 ++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 22 deletions(-)

diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 1f6f008ad9..cb17cbd189 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -5844,8 +5844,16 @@ qemuMigrationDstFinish(virQEMUDriver *driver,
 
     if (flags & VIR_MIGRATE_OFFLINE) {
         if (retcode == 0 &&
-            qemuMigrationDstPersist(driver, vm, mig, false) == 0)
+            qemuMigrationDstPersist(driver, vm, mig, false) == 0) {
             dom = virGetDomain(dconn, vm->def->name, vm->def->uuid, -1);
+
+            if (dom &&
+                qemuMigrationCookieFormat(mig, driver, vm,
+                                  QEMU_MIGRATION_DESTINATION,
+                                  cookieout, cookieoutlen,
+                                  QEMU_MIGRATION_COOKIE_STATS) < 0)
+                VIR_WARN("Unable to encode migration cookie");
+        }
         goto endjob;
     }
 
@@ -6008,6 +6016,25 @@ qemuMigrationDstFinish(virQEMUDriver *driver,
     /* Guest is successfully running, so cancel previous auto destroy */
     qemuProcessAutoDestroyRemove(driver, vm);
 
+    if (jobData) {
+        priv->job.completed = g_steal_pointer(&jobData);
+        priv->job.completed->status = VIR_DOMAIN_JOB_STATUS_COMPLETED;
+        qemuDomainJobSetStatsType(priv->job.completed,
+                                  QEMU_DOMAIN_JOB_STATS_TYPE_MIGRATION);
+    }
+
+    if (qemuMigrationCookieFormat(mig, driver, vm,
+                                  QEMU_MIGRATION_DESTINATION,
+                                  cookieout, cookieoutlen,
+                                  QEMU_MIGRATION_COOKIE_STATS) < 0)
+        VIR_WARN("Unable to encode migration cookie");
+
+    /* Remove completed stats for post-copy, everything but timing fields
+     * is obsolete anyway.
+     */
+    if (inPostCopy)
+        g_clear_pointer(&priv->job.completed, virDomainJobDataFree);
+
     dom = virGetDomain(dconn, vm->def->name, vm->def->uuid, vm->def->id);
 
  endjob:
@@ -6028,27 +6055,6 @@ qemuMigrationDstFinish(virQEMUDriver *driver,
         }
     }
 
-    if (dom) {
-        if (jobData) {
-            priv->job.completed = g_steal_pointer(&jobData);
-            priv->job.completed->status = VIR_DOMAIN_JOB_STATUS_COMPLETED;
-            qemuDomainJobSetStatsType(priv->job.completed,
-                                      QEMU_DOMAIN_JOB_STATS_TYPE_MIGRATION);
-        }
-
-        if (qemuMigrationCookieFormat(mig, driver, vm,
-                                      QEMU_MIGRATION_DESTINATION,
-                                      cookieout, cookieoutlen,
-                                      QEMU_MIGRATION_COOKIE_STATS) < 0)
-            VIR_WARN("Unable to encode migration cookie");
-
-        /* Remove completed stats for post-copy, everything but timing fields
-         * is obsolete anyway.
-         */
-        if (inPostCopy)
-            g_clear_pointer(&priv->job.completed, virDomainJobDataFree);
-    }
-
     if (virDomainObjIsFailedPostcopy(vm)) {
         qemuProcessAutoDestroyRemove(driver, vm);
         qemuDomainCleanupAdd(vm, qemuProcessCleanupMigrationJob);
-- 
2.35.1
Re: [libvirt PATCH 25/80] qemu: Move success-only code out of endjob in qemuMigrationDstFinish
Posted by Peter Krempa 3 years, 9 months ago
On Tue, May 10, 2022 at 17:20:46 +0200, Jiri Denemark wrote:
> Code executed only when dom != NULL can be moved before "endjob" label,
> to the only place where dom is set.
> 
> Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
> ---
>  src/qemu/qemu_migration.c | 50 ++++++++++++++++++++++-----------------
>  1 file changed, 28 insertions(+), 22 deletions(-)
> 
> diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
> index 1f6f008ad9..cb17cbd189 100644
> --- a/src/qemu/qemu_migration.c
> +++ b/src/qemu/qemu_migration.c
> @@ -5844,8 +5844,16 @@ qemuMigrationDstFinish(virQEMUDriver *driver,
>  
>      if (flags & VIR_MIGRATE_OFFLINE) {
>          if (retcode == 0 &&
> -            qemuMigrationDstPersist(driver, vm, mig, false) == 0)
> +            qemuMigrationDstPersist(driver, vm, mig, false) == 0) {
>              dom = virGetDomain(dconn, vm->def->name, vm->def->uuid, -1);

IMO virGetDomain should be treated as that it can't fail ...

> +
> +            if (dom &&

... so this check seems pointless.

> +                qemuMigrationCookieFormat(mig, driver, vm,
> +                                  QEMU_MIGRATION_DESTINATION,
> +                                  cookieout, cookieoutlen,
> +                                  QEMU_MIGRATION_COOKIE_STATS) < 0)

broken alignment

> +                VIR_WARN("Unable to encode migration cookie");
> +        }
>          goto endjob;
>      }
>  
> @@ -6008,6 +6016,25 @@ qemuMigrationDstFinish(virQEMUDriver *driver,
>      /* Guest is successfully running, so cancel previous auto destroy */
>      qemuProcessAutoDestroyRemove(driver, vm);
>  
> +    if (jobData) {
> +        priv->job.completed = g_steal_pointer(&jobData);
> +        priv->job.completed->status = VIR_DOMAIN_JOB_STATUS_COMPLETED;
> +        qemuDomainJobSetStatsType(priv->job.completed,
> +                                  QEMU_DOMAIN_JOB_STATS_TYPE_MIGRATION);
> +    }
> +
> +    if (qemuMigrationCookieFormat(mig, driver, vm,
> +                                  QEMU_MIGRATION_DESTINATION,
> +                                  cookieout, cookieoutlen,
> +                                  QEMU_MIGRATION_COOKIE_STATS) < 0)
> +        VIR_WARN("Unable to encode migration cookie");

E.g. here you don't check 'dom' because it's not yet allocated.

> +
> +    /* Remove completed stats for post-copy, everything but timing fields
> +     * is obsolete anyway.
> +     */
> +    if (inPostCopy)
> +        g_clear_pointer(&priv->job.completed, virDomainJobDataFree);
> +
>      dom = virGetDomain(dconn, vm->def->name, vm->def->uuid, vm->def->id);
>  
>   endjob:

Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Re: [libvirt PATCH 25/80] qemu: Move success-only code out of endjob in qemuMigrationDstFinish
Posted by Jiri Denemark 3 years, 8 months ago
On Wed, May 11, 2022 at 15:40:58 +0200, Peter Krempa wrote:
> On Tue, May 10, 2022 at 17:20:46 +0200, Jiri Denemark wrote:
> > Code executed only when dom != NULL can be moved before "endjob" label,
> > to the only place where dom is set.
> > 
> > Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
> > ---
> >  src/qemu/qemu_migration.c | 50 ++++++++++++++++++++++-----------------
> >  1 file changed, 28 insertions(+), 22 deletions(-)
> > 
> > diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
> > index 1f6f008ad9..cb17cbd189 100644
> > --- a/src/qemu/qemu_migration.c
> > +++ b/src/qemu/qemu_migration.c
> > @@ -5844,8 +5844,16 @@ qemuMigrationDstFinish(virQEMUDriver *driver,
> >  
> >      if (flags & VIR_MIGRATE_OFFLINE) {
> >          if (retcode == 0 &&
> > -            qemuMigrationDstPersist(driver, vm, mig, false) == 0)
> > +            qemuMigrationDstPersist(driver, vm, mig, false) == 0) {
> >              dom = virGetDomain(dconn, vm->def->name, vm->def->uuid, -1);
> 
> IMO virGetDomain should be treated as that it can't fail ...

Well, it does not return void and we check the value elsewhere...

> 
> > +
> > +            if (dom &&
> 
> ... so this check seems pointless.

Mostly, but we can avoid formatting the cookie in case we know we're
going to fail anyway.

> > +                qemuMigrationCookieFormat(mig, driver, vm,
> > +                                  QEMU_MIGRATION_DESTINATION,
> > +                                  cookieout, cookieoutlen,
> > +                                  QEMU_MIGRATION_COOKIE_STATS) < 0)
> 
> broken alignment

Oops.

> > +                VIR_WARN("Unable to encode migration cookie");
> > +        }
> >          goto endjob;
> >      }
> >  

Jirka