[RFC PATCH] xen/domctl: require a completed shutdown before resuming a domain

Mykola Kvach posted 1 patch 3 days, 7 hours ago
xen/common/domain.c         | 22 +++++++++++++++++++---
xen/common/domctl.c         |  2 +-
xen/include/public/domctl.h |  1 +
xen/include/xen/sched.h     |  2 +-
4 files changed, 22 insertions(+), 5 deletions(-)
[RFC PATCH] xen/domctl: require a completed shutdown before resuming a domain
Posted by Mykola Kvach 3 days, 7 hours ago
XEN_DOMCTL_resumedomain can clear a reason set with SCHEDOP_shutdown_code
before shutdown starts. It can also undo a shutdown while a vCPU is
still deferring it.

Check for completed shutdown under shutdown_lock before changing the
domain state. Return -EINVAL if shutdown has not started or is still in
progress. A repeated resume therefore also returns -EINVAL.

Soft reset already checks the vCPUs before resetting domain resources.
After a successful reset, clear the old shutdown state and release the
shutdown pauses without calling arch_domain_resume(). This also works
with on_reboot="soft-reset", where the shutdown reason is reboot.

Signed-off-by: Mykola Kvach <mykola_kvach@epam.com>
---
Follow-up to the v2 discussion of "xen/domain: make shutdown state explicit":
https://lore.kernel.org/xen-devel/a0de8496-66cb-4e7f-854e-fcc0053bd666@suse.com/
---
 xen/common/domain.c         | 22 +++++++++++++++++++---
 xen/common/domctl.c         |  2 +-
 xen/include/public/domctl.h |  1 +
 xen/include/xen/sched.h     |  2 +-
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/xen/common/domain.c b/xen/common/domain.c
index f3a2634d5f985e094236f81c49acbf6f9ebc8d58..470406af22725b150a308923e63a60cf26822168 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -1425,9 +1425,10 @@ int domain_shutdown(struct domain *d, u8 reason)
     return 0;
 }
 
-void domain_resume(struct domain *d)
+static int domain_leave_shutdown_state(struct domain *d, bool resume)
 {
     struct vcpu *v;
+    int rc = 0;
 
     /*
      * Some code paths assume that shutdown status does not get reset under
@@ -1437,9 +1438,16 @@ void domain_resume(struct domain *d)
 
     spin_lock(&d->shutdown_lock);
 
+    if ( resume && !domain_shutdown_completed(d) )
+    {
+        rc = -EINVAL;
+        goto out_unlock;
+    }
+
     d->shutdown_state = DOMSHUTDOWN_none;
 
-    arch_domain_resume(d);
+    if ( resume )
+        arch_domain_resume(d);
 
     d->shutdown_code = SHUTDOWN_CODE_INVALID;
 
@@ -1450,9 +1458,17 @@ void domain_resume(struct domain *d)
         v->paused_for_shutdown = 0;
     }
 
+ out_unlock:
     spin_unlock(&d->shutdown_lock);
 
     domain_unpause(d);
+
+    return rc;
+}
+
+int domain_resume(struct domain *d)
+{
+    return domain_leave_shutdown_state(d, true);
 }
 
 int vcpu_start_shutdown_deferral(struct vcpu *v)
@@ -1804,7 +1820,7 @@ int domain_soft_reset(struct domain *d, bool resuming)
 
     rc = arch_domain_soft_reset(d);
     if ( !rc )
-        domain_resume(d);
+        rc = domain_leave_shutdown_state(d, false);
     else
         domain_crash(d);
 
diff --git a/xen/common/domctl.c b/xen/common/domctl.c
index 83405a766a54cc45d34cd1f5797778d7e20c3528..2420ad17213ce05e2a78f99b9e386e944661e1cd 100644
--- a/xen/common/domctl.c
+++ b/xen/common/domctl.c
@@ -672,7 +672,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
         if ( d == current->domain ) /* no domain_pause() */
             ret = -EINVAL;
         else
-            domain_resume(d);
+            ret = domain_resume(d);
         break;
 
     case XEN_DOMCTL_createdomain:
diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h
index 4ca8a2d7ca65e21c3c1020d9428d6f180f7fe783..974c9327e74d2288e582708e2eb6b62d9cc3cbac 100644
--- a/xen/include/public/domctl.h
+++ b/xen/include/public/domctl.h
@@ -1311,6 +1311,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_settimeoffset                 24
 #define XEN_DOMCTL_getvcpuaffinity               25
 #define XEN_DOMCTL_real_mode_area                26 /* Obsolete PPC only */
+/* Return -EINVAL unless the domain has completed shutdown. */
 #define XEN_DOMCTL_resumedomain                  27
 #define XEN_DOMCTL_sendtrigger                   28
 #define XEN_DOMCTL_subscribe                     29
diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h
index ce1fa636db368d0e5c65fac1b13f9f6d908216d9..fd19bd075eb3af6e0ed67b9e76f9f28d5cb3ec69 100644
--- a/xen/include/xen/sched.h
+++ b/xen/include/xen/sched.h
@@ -849,7 +849,7 @@ static inline void put_pg_owner(struct domain *pg_owner)
 void domain_destroy(struct domain *d);
 int domain_kill(struct domain *d);
 int domain_shutdown(struct domain *d, u8 reason);
-void domain_resume(struct domain *d);
+int domain_resume(struct domain *d);
 
 int domain_soft_reset(struct domain *d, bool resuming);