[PATCH v2] xen: handle domain_shutdown() return values

Mykola Kvach posted 1 patch 3 days, 6 hours ago
xen/arch/arm/vpsci.c    |  8 ++++++--
xen/arch/x86/compat.c   |  5 +----
xen/arch/x86/hvm/dm.c   |  3 +--
xen/arch/x86/hvm/hvm.c  | 12 ++++++++++--
xen/common/domain.c     |  9 +++++++--
xen/common/sched/core.c |  8 ++++++--
6 files changed, 31 insertions(+), 14 deletions(-)
[PATCH v2] xen: handle domain_shutdown() return values
Posted by Mykola Kvach 3 days, 6 hours ago
Ten callers discard the value returned by domain_shutdown(). MISRA C
Rule 17.7 requires the return value of a non-void function to be used,
and Directive 4.7 requires callers to test returned error information.

Return the status to guests using the legacy SCHEDOP_shutdown hypercall.
In PV shim mode, a cancelled suspend or a checkpoint returns 1. The
legacy handler loses that result and returns 0, telling the guest it
has resumed in a new domain.

Propagate the result from both remote shutdown operations. The explicit
zero in XEN_DMOP_remote_shutdown dates from when domain_shutdown()
returned void. Also propagate a failed poweroff from VCPUOP_down when
the last vCPU goes down. On that path the shim has not taken the local
vCPU offline, so reporting success would hide a failed request.

On Arm, domain_shutdown() returns zero whenever it returns. Assert
this in the PSCI poweroff and reset handlers so that a change to this
assumption is caught in debug builds. Keep the call outside ASSERT().

The HVM poweroff and triple-fault handlers, the domain crash path and
the watchdog callback cannot return a shutdown error to a caller. Log
these errors at guest error level. The configured triple-fault action
can be suspend, so only a negative result indicates failure.

Signed-off-by: Mykola Kvach <mykola_kvach@epam.com>
---
Changes in v2:
- Explain the MISRA rationale, the legacy suspend result and why the last
  vCPU's poweroff result matters. Drop Suggested-by.
- Use assertions for PSCI SYSTEM_OFF and SYSTEM_RESET on Arm.
- Use guest-class error messages for shutdown failures. Include the target
  domain when it can differ from the current domain, and the timer ID on
  watchdog expiry.
- Log poweroff failures in HVM release builds. For triple faults, report
  only negative results, since a suspend action can return 1.
- Drop the unreachable return at the end of do_sched_op_compat().

Previous RFC:
https://lore.kernel.org/xen-devel/95dacecdce8f8417562548e16a4d3e11c41a3f27.1773923242.git.mykola_kvach@epam.com/
---
 xen/arch/arm/vpsci.c    |  8 ++++++--
 xen/arch/x86/compat.c   |  5 +----
 xen/arch/x86/hvm/dm.c   |  3 +--
 xen/arch/x86/hvm/hvm.c  | 12 ++++++++++--
 xen/common/domain.c     |  9 +++++++--
 xen/common/sched/core.c |  8 ++++++--
 6 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/xen/arch/arm/vpsci.c b/xen/arch/arm/vpsci.c
index 8c7a70c7a907cf96252ae60cabb3927e6a972a8a..ae92a336ede70e1e519703efb6d0e8f23f7d3fbc 100644
--- a/xen/arch/arm/vpsci.c
+++ b/xen/arch/arm/vpsci.c
@@ -210,13 +210,17 @@ static int32_t do_psci_0_2_migrate_info_type(void)
 static void do_psci_0_2_system_off( void )
 {
     struct domain *d = current->domain;
-    domain_shutdown(d,SHUTDOWN_poweroff);
+    int rc = domain_shutdown(d, SHUTDOWN_poweroff);
+
+    ASSERT(!rc);
 }
 
 static void do_psci_0_2_system_reset(void)
 {
     struct domain *d = current->domain;
-    domain_shutdown(d,SHUTDOWN_reboot);
+    int rc = domain_shutdown(d, SHUTDOWN_reboot);
+
+    ASSERT(!rc);
 }
 
 static int32_t do_psci_1_0_system_suspend(register_t epoint, register_t cid)
diff --git a/xen/arch/x86/compat.c b/xen/arch/x86/compat.c
index 217b5b1fcc41700cde830a8c74f4c230d0e1d58c..ec3f253a8b46834932e05b53f7fa7022774f187e 100644
--- a/xen/arch/x86/compat.c
+++ b/xen/arch/x86/compat.c
@@ -39,14 +39,11 @@ long do_sched_op_compat(int cmd, unsigned long arg)
     case SCHEDOP_shutdown:
         TRACE_TIME(TRC_SCHED_SHUTDOWN,
                    current->domain->domain_id, current->vcpu_id, arg);
-        domain_shutdown(current->domain, (u8)arg);
-        break;
+        return domain_shutdown(current->domain, (u8)arg);
 
     default:
         return -ENOSYS;
     }
-
-    return 0;
 }
 
 /* Legacy hypercall (as of 0x00030202). */
diff --git a/xen/arch/x86/hvm/dm.c b/xen/arch/x86/hvm/dm.c
index 91f6ca669b2b4304c8c25265253778538eac1109..7528bb5b2c60948c388e2739624589dc8a5b0d49 100644
--- a/xen/arch/x86/hvm/dm.c
+++ b/xen/arch/x86/hvm/dm.c
@@ -551,8 +551,7 @@ int dm_op(const struct dmop_args *op_args)
         const struct xen_dm_op_remote_shutdown *data =
             &op.u.remote_shutdown;
 
-        domain_shutdown(d, data->reason);
-        rc = 0;
+        rc = domain_shutdown(d, data->reason);
         break;
     }
 
diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
index 4334ff678d348f1002d5b509daeef2bf4057d78e..98deb2eefc5127ec13fdd610a6c2686c39939bb4 100644
--- a/xen/arch/x86/hvm/hvm.c
+++ b/xen/arch/x86/hvm/hvm.c
@@ -1720,8 +1720,12 @@ void hvm_vcpu_down(struct vcpu *v)
     /* ... Shut down the domain if not. */
     if ( online_count == 0 )
     {
+        int rc;
+
         gdprintk(XENLOG_INFO, "All CPUs offline -- powering off.\n");
-        domain_shutdown(d, SHUTDOWN_poweroff);
+        rc = domain_shutdown(d, SHUTDOWN_poweroff);
+        if ( unlikely(rc < 0) )
+            printk(XENLOG_G_ERR "%pd: failed to power off: %d\n", d, rc);
     }
 }
 
@@ -1750,12 +1754,16 @@ void hvm_triple_fault(void)
     struct vcpu *v = current;
     struct domain *d = v->domain;
     u8 reason = d->arch.hvm.params[HVM_PARAM_TRIPLE_FAULT_REASON];
+    int rc;
 
     gprintk(XENLOG_ERR,
             "Triple fault - invoking HVM shutdown action %d\n",
             reason);
     vcpu_show_execution_state(v);
-    domain_shutdown(d, reason);
+    rc = domain_shutdown(d, reason);
+    if ( unlikely(rc < 0) )
+        gprintk(XENLOG_ERR, "Failed to shut down after triple fault: %d\n",
+                rc);
 }
 
 void hvm_inject_event(const struct x86_event *event)
diff --git a/xen/common/domain.c b/xen/common/domain.c
index f3a2634d5f985e094236f81c49acbf6f9ebc8d58..9253c811b520977a691f831fa979eb0df135af95 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -1358,6 +1358,8 @@ int domain_kill(struct domain *d)
 
 void __domain_crash(struct domain *d)
 {
+    int rc;
+
     if ( domain_shutting_down(d) )
     {
         /* Print nothing: the domain is already shutting down. */
@@ -1374,7 +1376,10 @@ void __domain_crash(struct domain *d)
                d->domain_id, current->domain->domain_id, smp_processor_id());
     }
 
-    domain_shutdown(d, SHUTDOWN_crash);
+    rc = domain_shutdown(d, SHUTDOWN_crash);
+    if ( unlikely(rc) )
+        printk(XENLOG_G_ERR "Failed to shut down crashed domain %pd: %d\n",
+               d, rc);
 }
 
 
@@ -2198,7 +2203,7 @@ long common_vcpu_op(int cmd, struct vcpu *v, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         if ( !rc ) /* Last vcpu going down? */
         {
-            domain_shutdown(d, SHUTDOWN_poweroff);
+            rc = domain_shutdown(d, SHUTDOWN_poweroff);
             break;
         }
 
diff --git a/xen/common/sched/core.c b/xen/common/sched/core.c
index 10b6eecccd3b39cfdc684e434e6eb9cdcd707417..d3aca88cc832fe76fcb914aa870eaa82aef1113b 100644
--- a/xen/common/sched/core.c
+++ b/xen/common/sched/core.c
@@ -1588,6 +1588,7 @@ static void cf_check domain_watchdog_timeout(void *data)
      */
     struct domain *d = _p((unsigned long)data & PAGE_MASK);
     unsigned int id = (unsigned long)data & ~PAGE_MASK;
+    int rc;
 
     BUILD_BUG_ON(alignof(*d) < PAGE_SIZE);
 
@@ -1595,7 +1596,10 @@ static void cf_check domain_watchdog_timeout(void *data)
         return;
 
     printk("Watchdog timer %u fired for %pd\n", id, d);
-    domain_shutdown(d, SHUTDOWN_watchdog);
+    rc = domain_shutdown(d, SHUTDOWN_watchdog);
+    if ( unlikely(rc) )
+        printk(XENLOG_G_ERR "Watchdog timer %u failed to shut down %pd: %d\n",
+               id, d, rc);
 }
 
 static long domain_watchdog(struct domain *d, uint32_t id, uint32_t timeout)
@@ -2027,7 +2031,7 @@ ret_t do_sched_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         ret = xsm_schedop_shutdown(XSM_DM_PRIV, currd, d);
         if ( likely(!ret) )
-            domain_shutdown(d, sched_remote_shutdown.reason);
+            ret = domain_shutdown(d, sched_remote_shutdown.reason);
 
         rcu_unlock_domain(d);