From: Mark Williamson Subject: Re: [RFC] Xen paravirtual watchdog device [4/4] Date: Fri, 1 Jun 2007 04:53:29 +0100 Message-ID: <200706010453.29197.mark.williamson@cl.cam.ac.uk> References: <200706010444.29583.mark.williamson@cl.cam.ac.uk> <200706010451.33245.mark.williamson@cl.cam.ac.uk> Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_5g5XG8rcZSbClSs" Return-path: In-Reply-To: <200706010451.33245.mark.williamson@cl.cam.ac.uk> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org --Boundary-00=_5g5XG8rcZSbClSs Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline And as if that description wasn't enough, for your pleasure and enjoyment I present ... the actual patch! On Friday 01 June 2007, Mark Williamson wrote: > Xen virtual watchdog - hypervisor code. > > A Xen-internal software watchdog service for paravirt domains including > dom0 and driver domains. Ideally dom0 would use a hardware watchdog > device, but this is still a higher assurance design than a kernel-based > software watchdog. > > Cheers, > Mark -- Dave: Just a question. What use is a unicyle with no seat? And no pedals! Mark: To answer a question with a question: What use is a skateboard? Dave: Skateboards have wheels. Mark: My wheel has a wheel! --Boundary-00=_5g5XG8rcZSbClSs Content-Type: all/allfiles; name="watchdog4-xen.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="watchdog4-xen.patch" diff --exclude=.hg -rupN xen-incoming.hg/xen/arch/x86/x86_32/entry.S xen-watchdog-ref.hg/xen/arch/x86/x86_32/entry.S --- xen-incoming.hg/xen/arch/x86/x86_32/entry.S 2007-04-26 18:00:46.000000000 +0100 +++ xen-watchdog-ref.hg/xen/arch/x86/x86_32/entry.S 2007-06-01 04:23:08.000000000 +0100 @@ -673,6 +673,7 @@ ENTRY(hypercall_table) .long do_sysctl /* 35 */ .long do_domctl .long do_kexec_op + .long do_watchdog_op .rept NR_hypercalls-((.-hypercall_table)/4) .long do_ni_hypercall .endr @@ -716,6 +717,7 @@ ENTRY(hypercall_args_table) .byte 1 /* do_sysctl */ /* 35 */ .byte 1 /* do_domctl */ .byte 2 /* do_kexec_op */ + .byte 1 /* do_watchdog_op */ .rept NR_hypercalls-(.-hypercall_args_table) .byte 0 /* do_ni_hypercall */ .endr diff --exclude=.hg -rupN xen-incoming.hg/xen/common/domain.c xen-watchdog-ref.hg/xen/common/domain.c --- xen-incoming.hg/xen/common/domain.c 2007-05-30 23:00:35.000000000 +0100 +++ xen-watchdog-ref.hg/xen/common/domain.c 2007-06-01 04:23:08.000000000 +0100 @@ -61,6 +61,7 @@ struct domain *alloc_domain(domid_t domi spin_lock_init(&d->big_lock); spin_lock_init(&d->page_alloc_lock); spin_lock_init(&d->shutdown_lock); + spin_lock_init(&d->watchdog.lock); INIT_LIST_HEAD(&d->page_list); INIT_LIST_HEAD(&d->xenpage_list); diff --exclude=.hg -rupN xen-incoming.hg/xen/common/Makefile xen-watchdog-ref.hg/xen/common/Makefile --- xen-incoming.hg/xen/common/Makefile 2007-04-26 22:00:12.000000000 +0100 +++ xen-watchdog-ref.hg/xen/common/Makefile 2007-06-01 04:23:08.000000000 +0100 @@ -27,6 +27,7 @@ obj-y += version.o obj-y += vsprintf.o obj-y += xmalloc.o obj-y += rcupdate.o +obj-y += watchdog.o obj-$(perfc) += perfc.o obj-$(crash_debug) += gdbstub.o diff --exclude=.hg -rupN xen-incoming.hg/xen/common/watchdog.c xen-watchdog-ref.hg/xen/common/watchdog.c --- xen-incoming.hg/xen/common/watchdog.c 1970-01-01 01:00:00.000000000 +0100 +++ xen-watchdog-ref.hg/xen/common/watchdog.c 2007-06-01 04:23:09.000000000 +0100 @@ -0,0 +1,290 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void watchdog_timeout(void *data) +{ + xen_watchdog_state_t *watchdog; + struct domain *d = (struct domain *)data; + + gdprintk(XENLOG_INFO, "Watchdog timeout occurred! Oh dear!"); + + /* Watch out! Ought to have taken a reference could on the domain or be + * shutting down the timer when the domain is destroyed. */ + + /* A domain has timed out. */ + /* Lock. */ + + spin_lock(&d->watchdog.lock); + + watchdog = &d->watchdog; + + /* Shutdown domain using watchdog identifier code. */ + domain_shutdown(d, SHUTDOWN_watchdog); + + /* Unlock. */ + spin_unlock(&d->watchdog.lock); + + /* Return to Xen. Control tools will deal with crashdumps / reboots / + * etc (or Xen will crash out in the normal way if we just killed dom0). */ + } + +static int watchdog_init(XEN_GUEST_HANDLE(xen_init_watchdog_op_t) gop) +{ + xen_watchdog_state_t *watchdog; + struct domain *d = current->domain; + xen_init_watchdog_op_t op; + + copy_from_guest(&op, gop, 1); + + gdprintk(XENLOG_INFO, "Attempting to create a watchdog!\n"); + + /* Check requested flags and settings are supported. Fail if not, to avoid + * unexpected behaviour. */ + if ( (op.status & ~XEN_WATCHDOG_FEAT_SUPPORTED) != 0 ) + return -ENOSYS; + + /* TODO: sanity check other arguments here. */ + + spin_lock(&d->watchdog.lock); + + watchdog = &d->watchdog; + + if ( watchdog->timeout != 0 ) + { + gdprintk(XENLOG_INFO, "Watchdog exists for domain, aborting!\n"); + spin_unlock(&d->watchdog.lock); + return -EPERM; + } + + if ( watchdog->status & XEN_WATCHDOG_LASTBOOT ) + op.status |= XEN_WATCHDOG_LASTBOOT; + else + op.status &= ~XEN_WATCHDOG_LASTBOOT; + + /* Fill out initial state. */ + watchdog->status = op.status; + watchdog->timeout = op.timeout; + + /* Handle watchdogs on CPU 0 now. Shouldn't matter too much for the + * timebeing as they're infrequent operations. */ + init_timer(&watchdog->timer, watchdog_timeout, (void *)d, 0); + + /* Start timer. */ + set_timer(&watchdog->timer, NOW() + SECONDS(watchdog->timeout)); + + /* Unlock. */ + spin_unlock(&d->watchdog.lock); + + /* Return success to guest so that it may activate the refresh process. */ + return 0; +} + +static int watchdog_set(XEN_GUEST_HANDLE(xen_set_watchdog_op_t) guest_op) +{ + struct domain *d = current->domain; + xen_watchdog_state_t *watchdog; + xen_set_watchdog_op_t op; + + gdprintk(XENLOG_INFO, "Attempting to set a watchdog!\n"); + + copy_from_guest(&op, guest_op, 1); + + /* Check requested flags and settings are supported. Fail if not, to avoid + * unexpected behaviour. */ + if ( (op.status & ~XEN_WATCHDOG_FEAT_SUPPORTED) != 0 ) + { + gdprintk(XENLOG_INFO, "Invalid status set\n"); + return -EINVAL; + } + + if ( (op.status & ~XEN_WATCHDOG_SETSELF_ALLOWED) && !IS_PRIV(d) ) + return -EPERM; + + if ( !IS_PRIV(current->domain) && (op.domid != DOMID_SELF) ) + { + gdprintk(XENLOG_INFO, "Non privileged domain attempted to operate on another domain.\n"); + return -EPERM; + } + + if ( op.domid != DOMID_SELF ) + { + d = rcu_lock_domain_by_id(op.domid); + if (d == NULL) + return -EEXIST; + } + + watchdog = &d->watchdog; + + /* Lock structure to prevent timeout happening while we update. */ + spin_lock(&watchdog->lock); + + /* Mask out inappropriate options: Don't currently support using this call + to start / stop the watchdog. */ + op.status &= ~XEN_WATCHDOG_ACTIVE; + + /* Don't allow NOWAYOUT to be unset. */ + if ( watchdog->status & XEN_WATCHDOG_NOWAYOUT ) + op.status |= XEN_WATCHDOG_NOWAYOUT; + + /* Apply remaining changes to watchdog status. */ + watchdog->status = op.status; + + spin_unlock(&watchdog->lock); + + if ( op.domid != DOMID_SELF ) + rcu_unlock_domain(d); + + return 0; +} + +static int watchdog_query(XEN_GUEST_HANDLE(xen_query_watchdog_op_t) guest_query) +{ + xen_query_watchdog_op_t query; + xen_watchdog_state_t *watchdog; + struct domain *d = current->domain; + + copy_from_guest(&query, guest_query, 1); + + if ( !IS_PRIV(current->domain) && query.domid != DOMID_SELF ) + return -EINVAL; + + if ( query.domid != DOMID_SELF ) + { + d = get_domain_by_id(query.domid); + if ( d == NULL ) + return -EEXIST; + } + + gdprintk(XENLOG_INFO, "Querying watchdog state for domain %d\n", query.domid); + watchdog = &d->watchdog; + + spin_lock(&watchdog->lock); + + query.timeout = watchdog->timeout; + query.features = XEN_WATCHDOG_FEAT_SUPPORTED; + query.status = watchdog->status; + + spin_unlock(&watchdog->lock); + + copy_to_guest(guest_query, &query, 1); + + if ( query.domid != DOMID_SELF ) + put_domain(d); + + return 0; +} + +static int watchdog_prod(XEN_GUEST_HANDLE(xen_prod_watchdog_op_t) gop) +{ + struct domain *d = current->domain; + xen_watchdog_state_t *watchdog; + xen_prod_watchdog_op_t op; + + copy_from_guest(&op, gop, 1); + + gdprintk(XENLOG_INFO, "Prodding the watchdog!\n"); + + /* Lock structure to prevent timeout happening while we update. */ + spin_lock(&d->watchdog.lock); + + watchdog = &d->watchdog; + if ( watchdog->timeout == 0 ) + { + /* Watchdog doesn't exist. */ + spin_unlock(&d->watchdog.lock); + return -ENOENT; + } + + watchdog->timeout = op.timeout; /* safety check - maybe somewhere + * different */ + + gdprintk(XENLOG_INFO, "Setting the watchdog timeout to %lu\n", watchdog->timeout); + + /* Update timeout. */ + set_timer(&watchdog->timer, NOW() + SECONDS(watchdog->timeout)); + + /* Release mutex. */ + spin_unlock(&d->watchdog.lock); + + return 0; +} + +static int watchdog_stop(XEN_GUEST_HANDLE(xen_stop_watchdog_op_t) gop) +{ + xen_watchdog_state_t *watchdog; + struct domain *d = current->domain; + xen_stop_watchdog_op_t op; + + copy_from_guest(&op, gop, 1); + + gdprintk(XENLOG_INFO, "Attempting to shutdown the watchdog!\n"); + + /* Lock structure to prevent timeout. */ + spin_lock(&d->watchdog.lock); + + watchdog = &d->watchdog; + if ( watchdog == NULL ) + { + gdprintk(XENLOG_INFO, "No watchdog exists for this domain!\n"); + spin_unlock(&d->watchdog.lock); + return -EEXIST; /* No watchdog exists for this domain. */ + } + + /* Check if flags allow us to stop (NOWAYOUT?) */ + if ( watchdog->status & XEN_WATCHDOG_NOWAYOUT ) + { + gdprintk(XENLOG_INFO, "Not allowed to shutdown watchdog for this domain!\n"); + spin_unlock(&d->watchdog.lock); + return -EPERM; /* Operation not permitted - is this appropriate? */ + } + + gdprintk(XENLOG_INFO, "Stopping watchdog timer...\n"); + /* If OK to stop, shut down the timer and deallocate the watchdog structure. */ + stop_timer(&watchdog->timer); + + gdprintk(XENLOG_INFO, "Job done, releasing mutex.\n"); + /* Release mutex. */ + spin_unlock(&d->watchdog.lock); + + return 0; +} + +int do_watchdog_op(int cmd, XEN_GUEST_HANDLE(void) guest_op) +{ + int ret; + + switch (cmd) + { + case XEN_WATCHDOG_CMD_INIT: + ret = watchdog_init(guest_handle_cast(guest_op, xen_init_watchdog_op_t)); + break; + + case XEN_WATCHDOG_CMD_SET: + ret = watchdog_set(guest_handle_cast(guest_op, xen_set_watchdog_op_t)); + break; + + case XEN_WATCHDOG_CMD_QUERY: + ret = watchdog_query(guest_handle_cast(guest_op, xen_query_watchdog_op_t)); + break; + + case XEN_WATCHDOG_CMD_PROD: + ret = watchdog_prod(guest_handle_cast(guest_op, xen_prod_watchdog_op_t)); + break; + + case XEN_WATCHDOG_CMD_STOP: + ret = watchdog_stop(guest_handle_cast(guest_op, xen_stop_watchdog_op_t)); + break; + + default: + ret = -ENOSYS; + } + + return ret; +} diff --exclude=.hg -rupN xen-incoming.hg/xen/include/public/sched.h xen-watchdog-ref.hg/xen/include/public/sched.h --- xen-incoming.hg/xen/include/public/sched.h 2007-01-02 18:51:16.000000000 +0000 +++ xen-watchdog-ref.hg/xen/include/public/sched.h 2007-06-01 04:23:12.000000000 +0100 @@ -107,6 +107,7 @@ DEFINE_XEN_GUEST_HANDLE(sched_remote_shu #define SHUTDOWN_reboot 1 /* Clean up, kill, and then restart. */ #define SHUTDOWN_suspend 2 /* Clean up, save suspend info, kill. */ #define SHUTDOWN_crash 3 /* Tell controller we've crashed. */ +#define SHUTDOWN_watchdog 5 /* The watchdog triggered a reboot. */ #endif /* __XEN_PUBLIC_SCHED_H__ */ diff --exclude=.hg -rupN xen-incoming.hg/xen/include/public/watchdog.h xen-watchdog-ref.hg/xen/include/public/watchdog.h --- xen-incoming.hg/xen/include/public/watchdog.h 1970-01-01 01:00:00.000000000 +0100 +++ xen-watchdog-ref.hg/xen/include/public/watchdog.h 2007-06-01 04:23:12.000000000 +0100 @@ -0,0 +1,66 @@ +#ifndef __XEN_PUBLIC_WATCHDOG_H__ +#define __XEN_PUBLIC_WATCHDOG_H__ + +#include "xen.h" + +/* Xen Watchdog device - software implementation of a watchdog for use by guest + * OSes, including domain 0. Intended to give higher assurance than an + * in-guest implementation that may become corrupted by kernel bugs. + */ + +typedef struct xen_init_watchdog_op { + domid_t domid; /* IN: For now only DOMID_SELF is supported. */ + unsigned long status; /* IN: Status flags to be set. */ + unsigned long timeout; /* IN: Initial timeout. */ +} xen_init_watchdog_op_t; +DEFINE_XEN_GUEST_HANDLE(xen_init_watchdog_op_t); + +typedef struct xen_prod_watchdog_op { + domid_t domid; /* IN: For now only DOMID_SELF is supported. */ + unsigned long timeout; /* IN: New timeout, in seconds. */ +} xen_prod_watchdog_op_t; +DEFINE_XEN_GUEST_HANDLE(xen_prod_watchdog_op_t); + +typedef struct xen_set_watchdog_op { + domid_t domid; /* IN: For now only DOMID_SELF is supported. */ + u64 status; /* IN: Flags taken from XEN_WATCHDOG_* */ +} xen_set_watchdog_op_t; +DEFINE_XEN_GUEST_HANDLE(xen_set_watchdog_op_t); + +typedef struct xen_query_watchdog_op { + domid_t domid; /* IN: must be DOMID_SELF. */ + unsigned long timeout; /* OUT: current timeout in seconds. */ + u64 features; /* OUT: Flags taken from XEN_WATCHDOG_* */ + u64 status; /* OUT: Flags taken from XEN_WATCHDOG_* */ +} xen_query_watchdog_op_t; +DEFINE_XEN_GUEST_HANDLE(xen_query_watchdog_op_t); + +typedef struct xen_stop_watchdog_op { + domid_t domid; /* IN: Any other flags needed? */ +} xen_stop_watchdog_op_t; +DEFINE_XEN_GUEST_HANDLE(xen_stop_watchdog_op_t); + +/* Watchdog interface commands */ +#define XEN_WATCHDOG_CMD_INIT 0 +#define XEN_WATCHDOG_CMD_SET 1 +#define XEN_WATCHDOG_CMD_QUERY 2 +#define XEN_WATCHDOG_CMD_PROD 3 +#define XEN_WATCHDOG_CMD_STOP 4 + +/* Watchdog features and status flags. */ +#define _XEN_WATCHDOG_ACTIVE 0 /* Watchdog is active. */ +#define XEN_WATCHDOG_ACTIVE (1 << _XEN_WATCHDOG_ACTIVE) + +#define _XEN_WATCHDOG_NOWAYOUT 1 /* No way out mode. */ +#define XEN_WATCHDOG_NOWAYOUT (1 << _XEN_WATCHDOG_NOWAYOUT) + +#define _XEN_WATCHDOG_LASTBOOT 2 /* Details on last boot. */ +#define XEN_WATCHDOG_LASTBOOT (1 << _XEN_WATCHDOG_LASTBOOT) + +#define XEN_WATCHDOG_FEAT_SUPPORTED ( XEN_WATCHDOG_ACTIVE | \ + XEN_WATCHDOG_NOWAYOUT | \ + XEN_WATCHDOG_LASTBOOT ) + +#define XEN_WATCHDOG_SETSELF_ALLOWED XEN_WATCHDOG_NOWAYOUT + +#endif /* __XEN_PUBLIC_WATCHDOG_H__ */ diff --exclude=.hg -rupN xen-incoming.hg/xen/include/public/xen.h xen-watchdog-ref.hg/xen/include/public/xen.h --- xen-incoming.hg/xen/include/public/xen.h 2007-03-08 18:00:10.000000000 +0000 +++ xen-watchdog-ref.hg/xen/include/public/xen.h 2007-06-01 04:23:12.000000000 +0100 @@ -80,6 +80,7 @@ #define __HYPERVISOR_sysctl 35 #define __HYPERVISOR_domctl 36 #define __HYPERVISOR_kexec_op 37 +#define __HYPERVISOR_watchdog_op 38 /* Architecture-specific hypercall definitions. */ #define __HYPERVISOR_arch_0 48 diff --exclude=.hg -rupN xen-incoming.hg/xen/include/xen/sched.h xen-watchdog-ref.hg/xen/include/xen/sched.h --- xen-incoming.hg/xen/include/xen/sched.h 2007-05-30 23:00:35.000000000 +0100 +++ xen-watchdog-ref.hg/xen/include/xen/sched.h 2007-06-01 04:23:12.000000000 +0100 @@ -19,6 +19,7 @@ #include #include #include +#include #ifdef CONFIG_COMPAT #include @@ -223,6 +224,7 @@ struct domain struct xenoprof *xenoprof; int32_t time_offset_seconds; + xen_watchdog_state_t watchdog; struct rcu_head rcu; }; diff --exclude=.hg -rupN xen-incoming.hg/xen/include/xen/watchdog.h xen-watchdog-ref.hg/xen/include/xen/watchdog.h --- xen-incoming.hg/xen/include/xen/watchdog.h 1970-01-01 01:00:00.000000000 +0100 +++ xen-watchdog-ref.hg/xen/include/xen/watchdog.h 2007-06-01 04:23:12.000000000 +0100 @@ -0,0 +1,15 @@ +#ifndef __XEN_WATCHDOG_H__ +#define __XEN_WATCHDOG_H__ + +#include +#include + +typedef struct xen_watchdog_state { + spinlock_t lock; /* Serialises access to the other fields in + * this struct. */ + u64 status; + unsigned long timeout; /* Watchdog timeout, in seconds. */ + struct timer timer; /* Watchdog timer, for use when active. */ +} xen_watchdog_state_t; + +#endif /* __XEN_WATCHDOG_H__ */ --Boundary-00=_5g5XG8rcZSbClSs Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --Boundary-00=_5g5XG8rcZSbClSs--