[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xen staging] systemd: Add hooks to stop/start xen-watchdog on suspend/resume



commit e54a6cd6a1f3802274cd3a58b07df5f012342192
Author:     Mykola Kvach <mykola_kvach@xxxxxxxx>
AuthorDate: Thu Jul 17 23:16:58 2025 +0300
Commit:     Anthony PERARD <anthony.perard@xxxxxxxxxx>
CommitDate: Fri Aug 1 15:36:02 2025 +0200

    systemd: Add hooks to stop/start xen-watchdog on suspend/resume
    
    This patch adds a systemd sleep hook script to stop the xen-watchdog
    service before system suspend and start it again after resume.
    
    Stopping the watchdog before a system suspend operation may look unsafe.
    Let's imagine the following situation: 'systemctl suspend' does not
    interact with the running service at all. In such a case, the Xen
    watchdog daemon freezes just before suspend. If this happens, for
    example, right before sending a ping, and Xen has not yet marked the
    domain as suspended (is_shutting_down), the Xen watchdog timer may
    trigger a false alert.
    
    This is an almost impossible situation, because typically:
        ping time = watchdog timeout / 2
    
    and the watchdog timeout is usually set to a relatively large value
    (dozens of seconds).
    
    Still, this is more likely with very short watchdog timeouts. It may
    happen in the following scenarios:
        * Significant delays occur between freezing Linux tasks and
          triggering the ACPI or PSCI sleep request or handler.
        * Long delays happen inside Xen between the entrance to the sleep
          trigger and the actual forwarding of the sleep request further.
    
    A similar situation may occur on resume with short timeouts. During the
    resume operation, Xen restores timers and the domain context. The Xen
    watchdog timer also resumes. If it schedules the domain right before the
    watchdog timeout expires, and the daemon responsible for pinging is not
    yet running, a timeout might occur.
    
    Both scenarios are rare and typically require very small watchdog
    timeouts combined with significant delays in Xen or the Linux kernel
    during suspend/resume flows.
    
    Conceptually, however, if activating and pinging the Xen watchdog is the
    responsibility of the domain and its services, then the domain should
    also manage the watchdog service/daemon lifecycle. This is similar to
    what is already done by the Xen watchdog driver inside the Linux kernel.
    
    Signed-off-by: Mykola Kvach <mykola_kvach@xxxxxxxx>
    Reviewed-by: Denis Mukhin <dmukhin@xxxxxxxx>
    Reviewed-by: Anthony PERARD <anthony.perard@xxxxxxxxxx>
---
 config/Tools.mk.in                                |  1 +
 m4/systemd.m4                                     | 14 ++++++
 tools/configure                                   | 58 ++++++++++++++++++++++-
 tools/hotplug/Linux/systemd/Makefile              |  8 +++-
 tools/hotplug/Linux/systemd/xen-watchdog-sleep.sh | 34 +++++++++++++
 5 files changed, 113 insertions(+), 2 deletions(-)

diff --git a/config/Tools.mk.in b/config/Tools.mk.in
index 463ab75965..e47ac23d11 100644
--- a/config/Tools.mk.in
+++ b/config/Tools.mk.in
@@ -53,6 +53,7 @@ CONFIG_LIBFSIMAGE   := @libfsimage@
 CONFIG_SYSTEMD      := @systemd@
 XEN_SYSTEMD_DIR     := @SYSTEMD_DIR@
 XEN_SYSTEMD_MODULES_LOAD := @SYSTEMD_MODULES_LOAD@
+XEN_SYSTEMD_SLEEP_DIR := @SYSTEMD_SLEEP_DIR@
 CONFIG_9PFS         := @ninepfs@
 
 LINUX_BACKEND_MODULES := @LINUX_BACKEND_MODULES@
diff --git a/m4/systemd.m4 b/m4/systemd.m4
index ab12ea313d..c47a25ef93 100644
--- a/m4/systemd.m4
+++ b/m4/systemd.m4
@@ -28,6 +28,12 @@ AC_DEFUN([AX_SYSTEMD_OPTIONS], [
                [set directory for systemd modules load files 
[PREFIX/lib/modules-load.d/]]),
                [SYSTEMD_MODULES_LOAD="$withval"], [SYSTEMD_MODULES_LOAD=""])
        AC_SUBST(SYSTEMD_MODULES_LOAD)
+
+       AC_ARG_WITH(systemd-sleep,
+               AS_HELP_STRING([--with-systemd-sleep=DIR],
+               [set directory for systemd sleep script files 
[PREFIX/lib/systemd/system-sleep/]]),
+               [SYSTEMD_SLEEP_DIR="$withval"], [SYSTEMD_SLEEP_DIR=""])
+       AC_SUBST(SYSTEMD_SLEEP_DIR)
 ])
 
 AC_DEFUN([AX_ENABLE_SYSTEMD_OPTS], [
@@ -69,6 +75,14 @@ AC_DEFUN([AX_CHECK_SYSTEMD_LIBS], [
        AS_IF([test "x$SYSTEMD_MODULES_LOAD" = x], [
            AC_MSG_ERROR([SYSTEMD_MODULES_LOAD is unset])
        ], [])
+
+       AS_IF([test "x$SYSTEMD_SLEEP_DIR" = x], [
+           PKG_CHECK_VAR([SYSTEMD_SLEEP_DIR], [systemd], [systemdsleepdir])
+       ], [])
+
+       AS_IF([test "x$SYSTEMD_SLEEP_DIR" = x], [
+           AC_MSG_ERROR([SYSTEMD_SLEEP_DIR is unset])
+       ], [])
 ])
 
 AC_DEFUN([AX_CHECK_SYSTEMD], [
diff --git a/tools/configure b/tools/configure
index 27ae7c52fb..5abd44e21e 100755
--- a/tools/configure
+++ b/tools/configure
@@ -652,6 +652,7 @@ ac_subst_vars='LTLIBOBJS
 LIBOBJS
 pvshim
 ninepfs
+SYSTEMD_SLEEP_DIR
 SYSTEMD_MODULES_LOAD
 SYSTEMD_DIR
 systemd
@@ -847,6 +848,7 @@ with_xenstored
 enable_systemd
 with_systemd
 with_systemd_modules_load
+with_systemd_sleep
 enable_9pfs
 enable_pvshim
 '
@@ -881,7 +883,8 @@ pixman_LIBS
 libzstd_CFLAGS
 libzstd_LIBS
 LIBNL3_CFLAGS
-LIBNL3_LIBS'
+LIBNL3_LIBS
+SYSTEMD_SLEEP_DIR'
 
 
 # Initialize some variables set by options.
@@ -1589,6 +1592,9 @@ Optional Packages:
   --with-systemd-modules-load=DIR
                           set directory for systemd modules load files
                           [PREFIX/lib/modules-load.d/]
+  --with-systemd-sleep=DIR
+                          set directory for systemd sleep script files
+                          [PREFIX/lib/systemd/system-sleep/]
 
 Some influential environment variables:
   CC          C compiler command
@@ -1630,6 +1636,8 @@ Some influential environment variables:
   LIBNL3_CFLAGS
               C compiler flags for LIBNL3, overriding pkg-config
   LIBNL3_LIBS linker flags for LIBNL3, overriding pkg-config
+  SYSTEMD_SLEEP_DIR
+              value of systemdsleepdir for systemd, overriding pkg-config
 
 Use these variables to override the choices made by `configure' or to help
 it to find libraries and programs with nonstandard names/locations.
@@ -10138,6 +10146,17 @@ fi
 
 
 
+# Check whether --with-systemd-sleep was given.
+if test ${with_systemd_sleep+y}
+then :
+  withval=$with_systemd_sleep; SYSTEMD_SLEEP_DIR="$withval"
+else $as_nop
+  SYSTEMD_SLEEP_DIR=""
+fi
+
+
+
+
 
                if test "x$enable_systemd" != "xno"
 then :
@@ -10176,6 +10195,43 @@ then :
 
            as_fn_error $? "SYSTEMD_MODULES_LOAD is unset" "$LINENO" 5
 
+fi
+
+       if test "x$SYSTEMD_SLEEP_DIR" = x
+then :
+
+
+if test -n "$SYSTEMD_SLEEP_DIR"; then
+    pkg_cv_SYSTEMD_SLEEP_DIR="$SYSTEMD_SLEEP_DIR"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists 
--print-errors \"systemd\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "systemd") 2>&5
+  ac_status=$?
+  printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_SYSTEMD_SLEEP_DIR=`$PKG_CONFIG --variable="systemdsleepdir" "systemd" 
2>/dev/null`
+                     test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+SYSTEMD_SLEEP_DIR=$pkg_cv_SYSTEMD_SLEEP_DIR
+
+if test "x$SYSTEMD_SLEEP_DIR" = x""
+then :
+
+fi
+
+fi
+
+       if test "x$SYSTEMD_SLEEP_DIR" = x
+then :
+
+           as_fn_error $? "SYSTEMD_SLEEP_DIR is unset" "$LINENO" 5
+
 fi
 
 
diff --git a/tools/hotplug/Linux/systemd/Makefile 
b/tools/hotplug/Linux/systemd/Makefile
index e29889156d..579ef9d87d 100644
--- a/tools/hotplug/Linux/systemd/Makefile
+++ b/tools/hotplug/Linux/systemd/Makefile
@@ -5,6 +5,8 @@ XEN_SYSTEMD_MODULES := xen.conf
 
 XEN_SYSTEMD_MOUNT := proc-xen.mount
 
+XEN_SYSTEMD_SLEEP_SCRIPT := xen-watchdog-sleep.sh
+
 XEN_SYSTEMD_SERVICE := xenstored.service
 XEN_SYSTEMD_SERVICE += xenconsoled.service
 XEN_SYSTEMD_SERVICE += xen-qemu-dom0-disk-backend.service
@@ -15,7 +17,8 @@ XEN_SYSTEMD_SERVICE += xendriverdomain.service
 
 ALL_XEN_SYSTEMD :=     $(XEN_SYSTEMD_MODULES)  \
                        $(XEN_SYSTEMD_MOUNT)    \
-                       $(XEN_SYSTEMD_SERVICE)
+                       $(XEN_SYSTEMD_SERVICE)  \
+                       $(XEN_SYSTEMD_SLEEP_SCRIPT)
 
 .PHONY: all
 all:   $(ALL_XEN_SYSTEMD)
@@ -31,15 +34,18 @@ distclean: clean
 install: $(ALL_XEN_SYSTEMD)
        $(INSTALL_DIR) $(DESTDIR)$(XEN_SYSTEMD_DIR)
        $(INSTALL_DIR) $(DESTDIR)$(XEN_SYSTEMD_MODULES_LOAD)
+       $(INSTALL_DIR) $(DESTDIR)$(XEN_SYSTEMD_SLEEP_DIR)
        $(INSTALL_DATA) *.service $(DESTDIR)$(XEN_SYSTEMD_DIR)
        $(INSTALL_DATA) *.mount $(DESTDIR)$(XEN_SYSTEMD_DIR)
        $(INSTALL_DATA) *.conf $(DESTDIR)$(XEN_SYSTEMD_MODULES_LOAD)
+       $(INSTALL_PROG) $(XEN_SYSTEMD_SLEEP_SCRIPT) 
$(DESTDIR)$(XEN_SYSTEMD_SLEEP_DIR)
 
 .PHONY: uninstall
 uninstall:
        rm -f $(DESTDIR)$(XEN_SYSTEMD_MODULES_LOAD)/*.conf
        rm -f $(DESTDIR)$(XEN_SYSTEMD_DIR)/*.mount
        rm -f $(DESTDIR)$(XEN_SYSTEMD_DIR)/*.service
+       rm -f $(DESTDIR)$(XEN_SYSTEMD_SLEEP_DIR)/$(XEN_SYSTEMD_SLEEP_SCRIPT)
 
 $(XEN_SYSTEMD_MODULES):
        rm -f $@.tmp
diff --git a/tools/hotplug/Linux/systemd/xen-watchdog-sleep.sh 
b/tools/hotplug/Linux/systemd/xen-watchdog-sleep.sh
new file mode 100644
index 0000000000..e9bdadc8fa
--- /dev/null
+++ b/tools/hotplug/Linux/systemd/xen-watchdog-sleep.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+# The first argument ($1) is:
+#     "pre" or "post"
+# The second argument ($2) is:
+#     "suspend", "hibernate", "hybrid-sleep", or "suspend-then-hibernate"
+
+. /etc/xen/scripts/hotplugpath.sh
+
+SERVICE_NAME="xen-watchdog.service"
+STATE_FILE="${XEN_RUN_DIR}/xen-watchdog-sleep-flag"
+
+case "$1" in
+pre)
+    if systemctl is-active --quiet "${SERVICE_NAME}"; then
+        touch "${STATE_FILE}"
+        echo "Stopping ${SERVICE_NAME} before $2."
+        systemctl stop "${SERVICE_NAME}"
+    fi
+    ;;
+post)
+    if [ -f "${STATE_FILE}" ]; then
+        echo "Starting ${SERVICE_NAME} after $2."
+        systemctl start "${SERVICE_NAME}"
+        rm "${STATE_FILE}"
+    fi
+    ;;
+*)
+    echo "Script called with unknown action '$1'. Arguments: '$@'"
+    exit 1
+    ;;
+esac
+
+exit 0
--
generated by git-patchbot for /home/xen/git/xen.git#staging



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.