Home | History | Annotate | Line # | Download | only in kern
kern_heartbeat.c revision 1.7
      1  1.7  riastrad /*	$NetBSD: kern_heartbeat.c,v 1.7 2023/09/02 17:44:23 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2023 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8  1.1  riastrad  * modification, are permitted provided that the following conditions
      9  1.1  riastrad  * are met:
     10  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad /*
     30  1.1  riastrad  * heartbeat(9) -- periodic checks to ensure CPUs are making progress
     31  1.1  riastrad  *
     32  1.1  riastrad  * Manual tests to run when changing this file.  Magic numbers are for
     33  1.1  riastrad  * evbarm; adjust for other platforms.  Tests involving cpuctl
     34  1.1  riastrad  * online/offline assume a 2-CPU system -- for full testing on a >2-CPU
     35  1.1  riastrad  * system, offline all but one CPU.
     36  1.1  riastrad  *
     37  1.1  riastrad  * 1.	cpuctl offline 0
     38  1.1  riastrad  *	sleep 20
     39  1.1  riastrad  *	cpuctl online 0
     40  1.1  riastrad  *
     41  1.1  riastrad  * 2.	cpuctl offline 1
     42  1.1  riastrad  *	sleep 20
     43  1.1  riastrad  *	cpuctl online 1
     44  1.1  riastrad  *
     45  1.1  riastrad  * 3.	cpuctl offline 0
     46  1.1  riastrad  *	sysctl -w kern.heartbeat.max_period=5
     47  1.1  riastrad  *	sleep 10
     48  1.1  riastrad  *	sysctl -w kern.heartbeat.max_period=0
     49  1.1  riastrad  *	sleep 10
     50  1.1  riastrad  *	sysctl -w kern.heartbeat.max_period=5
     51  1.1  riastrad  *	sleep 10
     52  1.1  riastrad  *	cpuctl online 0
     53  1.1  riastrad  *
     54  1.1  riastrad  * 4.	sysctl -w debug.crashme_enable=1
     55  1.1  riastrad  *	sysctl -w debug.crashme.spl_spinout=1   # IPL_SOFTCLOCK
     56  1.1  riastrad  *	# verify system panics after 15sec
     57  1.1  riastrad  *
     58  1.1  riastrad  * 5.	sysctl -w debug.crashme_enable=1
     59  1.1  riastrad  *	sysctl -w debug.crashme.spl_spinout=6   # IPL_SCHED
     60  1.1  riastrad  *	# verify system panics after 15sec
     61  1.1  riastrad  *
     62  1.1  riastrad  * 6.	cpuctl offline 0
     63  1.1  riastrad  *	sysctl -w debug.crashme_enable=1
     64  1.1  riastrad  *	sysctl -w debug.crashme.spl_spinout=1   # IPL_SOFTCLOCK
     65  1.1  riastrad  *	# verify system panics after 15sec
     66  1.1  riastrad  *
     67  1.1  riastrad  * 7.	cpuctl offline 0
     68  1.1  riastrad  *	sysctl -w debug.crashme_enable=1
     69  1.1  riastrad  *	sysctl -w debug.crashme.spl_spinout=5   # IPL_VM
     70  1.1  riastrad  *	# verify system panics after 15sec
     71  1.1  riastrad  *
     72  1.1  riastrad  *	# Not this -- IPL_SCHED and IPL_HIGH spinout on a single CPU
     73  1.1  riastrad  *	# require a hardware watchdog timer.
     74  1.1  riastrad  *	#cpuctl offline 0
     75  1.1  riastrad  *	#sysctl -w debug.crashme_enable
     76  1.1  riastrad  *	#sysctl -w debug.crashme.spl_spinout=6   # IPL_SCHED
     77  1.1  riastrad  *	# hope watchdog timer kicks in
     78  1.1  riastrad  */
     79  1.1  riastrad 
     80  1.1  riastrad #include <sys/cdefs.h>
     81  1.7  riastrad __KERNEL_RCSID(0, "$NetBSD: kern_heartbeat.c,v 1.7 2023/09/02 17:44:23 riastradh Exp $");
     82  1.1  riastrad 
     83  1.1  riastrad #ifdef _KERNEL_OPT
     84  1.1  riastrad #include "opt_ddb.h"
     85  1.1  riastrad #include "opt_heartbeat.h"
     86  1.1  riastrad #endif
     87  1.1  riastrad 
     88  1.1  riastrad #include "heartbeat.h"
     89  1.1  riastrad 
     90  1.1  riastrad #include <sys/param.h>
     91  1.1  riastrad #include <sys/types.h>
     92  1.1  riastrad 
     93  1.1  riastrad #include <sys/atomic.h>
     94  1.1  riastrad #include <sys/cpu.h>
     95  1.1  riastrad #include <sys/errno.h>
     96  1.1  riastrad #include <sys/heartbeat.h>
     97  1.1  riastrad #include <sys/ipi.h>
     98  1.4  riastrad #include <sys/kernel.h>
     99  1.1  riastrad #include <sys/mutex.h>
    100  1.1  riastrad #include <sys/sysctl.h>
    101  1.1  riastrad #include <sys/systm.h>
    102  1.1  riastrad #include <sys/xcall.h>
    103  1.1  riastrad 
    104  1.1  riastrad #ifdef DDB
    105  1.1  riastrad #include <ddb/ddb.h>
    106  1.1  riastrad #endif
    107  1.1  riastrad 
    108  1.1  riastrad /*
    109  1.1  riastrad  * Global state.
    110  1.1  riastrad  *
    111  1.1  riastrad  *	heartbeat_lock serializes access to heartbeat_max_period_secs
    112  1.1  riastrad  *	and heartbeat_max_period_ticks.  Two separate variables so we
    113  1.1  riastrad  *	can avoid multiplication or division in the heartbeat routine.
    114  1.1  riastrad  *
    115  1.1  riastrad  *	heartbeat_sih is stable after initialization in
    116  1.1  riastrad  *	heartbeat_start.
    117  1.1  riastrad  */
    118  1.1  riastrad kmutex_t heartbeat_lock			__cacheline_aligned;
    119  1.1  riastrad unsigned heartbeat_max_period_secs	__read_mostly;
    120  1.1  riastrad unsigned heartbeat_max_period_ticks	__read_mostly;
    121  1.1  riastrad 
    122  1.1  riastrad void *heartbeat_sih			__read_mostly;
    123  1.1  riastrad 
    124  1.1  riastrad /*
    125  1.1  riastrad  * heartbeat_suspend()
    126  1.1  riastrad  *
    127  1.1  riastrad  *	Suspend heartbeat monitoring of the current CPU.
    128  1.1  riastrad  *
    129  1.1  riastrad  *	Called after the current CPU has been marked offline but before
    130  1.6  riastrad  *	it has stopped running, or after IPL has been raised for
    131  1.6  riastrad  *	polling-mode console input.  Caller must have preemption
    132  1.6  riastrad  *	disabled.  Non-nestable.  Reversed by heartbeat_resume.
    133  1.1  riastrad  */
    134  1.1  riastrad void
    135  1.1  riastrad heartbeat_suspend(void)
    136  1.1  riastrad {
    137  1.6  riastrad 	struct cpu_info *ci = curcpu();
    138  1.6  riastrad 	int s;
    139  1.1  riastrad 
    140  1.2  riastrad 	KASSERT(curcpu_stable());
    141  1.6  riastrad 	KASSERT((ci->ci_schedstate.spc_flags & SPCF_HEARTBEATSUSPENDED) == 0);
    142  1.1  riastrad 
    143  1.6  riastrad 	s = splsched();
    144  1.6  riastrad 	ci->ci_schedstate.spc_flags |= SPCF_HEARTBEATSUSPENDED;
    145  1.6  riastrad 	splx(s);
    146  1.1  riastrad }
    147  1.1  riastrad 
    148  1.1  riastrad /*
    149  1.4  riastrad  * heartbeat_resume_cpu(ci)
    150  1.4  riastrad  *
    151  1.4  riastrad  *	Resume heartbeat monitoring of ci.
    152  1.4  riastrad  *
    153  1.4  riastrad  *	Called at startup while cold, and whenever heartbeat monitoring
    154  1.4  riastrad  *	is re-enabled after being disabled or the period is changed.
    155  1.4  riastrad  *	When not cold, ci must be the current CPU.
    156  1.6  riastrad  *
    157  1.6  riastrad  *	Must be run at splsched.
    158  1.4  riastrad  */
    159  1.4  riastrad static void
    160  1.4  riastrad heartbeat_resume_cpu(struct cpu_info *ci)
    161  1.4  riastrad {
    162  1.4  riastrad 
    163  1.4  riastrad 	KASSERT(__predict_false(cold) || curcpu_stable());
    164  1.4  riastrad 	KASSERT(__predict_false(cold) || ci == curcpu());
    165  1.6  riastrad 	/* XXX KASSERT IPL_SCHED */
    166  1.4  riastrad 
    167  1.4  riastrad 	ci->ci_heartbeat_count = 0;
    168  1.5  riastrad 	ci->ci_heartbeat_uptime_cache = time_uptime;
    169  1.4  riastrad 	ci->ci_heartbeat_uptime_stamp = 0;
    170  1.4  riastrad }
    171  1.4  riastrad 
    172  1.4  riastrad /*
    173  1.1  riastrad  * heartbeat_resume()
    174  1.1  riastrad  *
    175  1.1  riastrad  *	Resume heartbeat monitoring of the current CPU.
    176  1.1  riastrad  *
    177  1.1  riastrad  *	Called after the current CPU has started running but before it
    178  1.6  riastrad  *	has been marked online, or when ending polling-mode input
    179  1.6  riastrad  *	before IPL is restored.  Caller must have preemption disabled.
    180  1.1  riastrad  */
    181  1.1  riastrad void
    182  1.1  riastrad heartbeat_resume(void)
    183  1.1  riastrad {
    184  1.1  riastrad 	struct cpu_info *ci = curcpu();
    185  1.1  riastrad 	int s;
    186  1.1  riastrad 
    187  1.2  riastrad 	KASSERT(curcpu_stable());
    188  1.6  riastrad 	KASSERT(ci->ci_schedstate.spc_flags & SPCF_HEARTBEATSUSPENDED);
    189  1.1  riastrad 
    190  1.1  riastrad 	/*
    191  1.1  riastrad 	 * Block heartbeats while we reset the state so we don't
    192  1.1  riastrad 	 * spuriously think we had a heart attack in the middle of
    193  1.1  riastrad 	 * resetting the count and the uptime stamp.
    194  1.1  riastrad 	 */
    195  1.1  riastrad 	s = splsched();
    196  1.6  riastrad 	ci->ci_schedstate.spc_flags &= ~SPCF_HEARTBEATSUSPENDED;
    197  1.4  riastrad 	heartbeat_resume_cpu(ci);
    198  1.1  riastrad 	splx(s);
    199  1.1  riastrad }
    200  1.1  riastrad 
    201  1.1  riastrad /*
    202  1.7  riastrad  * heartbeat_timecounter_suspended()
    203  1.7  riastrad  *
    204  1.7  riastrad  *	True if timecounter heartbeat checks are suspended because the
    205  1.7  riastrad  *	timecounter may not be advancing, false if heartbeat checks
    206  1.7  riastrad  *	should check for timecounter progress.
    207  1.7  riastrad  */
    208  1.7  riastrad static bool
    209  1.7  riastrad heartbeat_timecounter_suspended(void)
    210  1.7  riastrad {
    211  1.7  riastrad 	CPU_INFO_ITERATOR cii;
    212  1.7  riastrad 	struct cpu_info *ci;
    213  1.7  riastrad 
    214  1.7  riastrad 	/*
    215  1.7  riastrad 	 * The timecounter ticks only on the primary CPU.  Check
    216  1.7  riastrad 	 * whether it's suspended.
    217  1.7  riastrad 	 *
    218  1.7  riastrad 	 * XXX Would be nice if we could find the primary CPU without
    219  1.7  riastrad 	 * iterating over all CPUs.
    220  1.7  riastrad 	 */
    221  1.7  riastrad 	for (CPU_INFO_FOREACH(cii, ci)) {
    222  1.7  riastrad 		if (CPU_IS_PRIMARY(ci)) {
    223  1.7  riastrad 			return ci->ci_schedstate.spc_flags &
    224  1.7  riastrad 			    SPCF_HEARTBEATSUSPENDED;
    225  1.7  riastrad 		}
    226  1.7  riastrad 	}
    227  1.7  riastrad 
    228  1.7  riastrad 	/*
    229  1.7  riastrad 	 * This should be unreachable -- there had better be a primary
    230  1.7  riastrad 	 * CPU in the system!  If not, the timecounter will be busted
    231  1.7  riastrad 	 * anyway.
    232  1.7  riastrad 	 */
    233  1.7  riastrad 	panic("no primary CPU");
    234  1.7  riastrad }
    235  1.7  riastrad 
    236  1.7  riastrad /*
    237  1.1  riastrad  * heartbeat_reset_xc(a, b)
    238  1.1  riastrad  *
    239  1.1  riastrad  *	Cross-call handler to reset heartbeat state just prior to
    240  1.1  riastrad  *	enabling heartbeat checks.
    241  1.1  riastrad  */
    242  1.1  riastrad static void
    243  1.1  riastrad heartbeat_reset_xc(void *a, void *b)
    244  1.1  riastrad {
    245  1.6  riastrad 	int s;
    246  1.1  riastrad 
    247  1.6  riastrad 	s = splsched();
    248  1.6  riastrad 	heartbeat_resume_cpu(curcpu());
    249  1.6  riastrad 	splx(s);
    250  1.1  riastrad }
    251  1.1  riastrad 
    252  1.1  riastrad /*
    253  1.1  riastrad  * set_max_period(max_period)
    254  1.1  riastrad  *
    255  1.1  riastrad  *	Set the maximum period, in seconds, for heartbeat checks.
    256  1.1  riastrad  *
    257  1.1  riastrad  *	- If max_period is zero, disable them.
    258  1.1  riastrad  *
    259  1.1  riastrad  *	- If the max period was zero and max_period is nonzero, ensure
    260  1.1  riastrad  *	  all CPUs' heartbeat uptime caches are up-to-date before
    261  1.1  riastrad  *	  re-enabling them.
    262  1.1  riastrad  *
    263  1.1  riastrad  *	max_period must be below UINT_MAX/4/hz to avoid arithmetic
    264  1.1  riastrad  *	overflow and give room for slop.
    265  1.1  riastrad  *
    266  1.1  riastrad  *	Caller must hold heartbeat_lock.
    267  1.1  riastrad  */
    268  1.1  riastrad static void
    269  1.1  riastrad set_max_period(unsigned max_period)
    270  1.1  riastrad {
    271  1.1  riastrad 
    272  1.1  riastrad 	KASSERTMSG(max_period <= UINT_MAX/4/hz,
    273  1.1  riastrad 	    "max_period=%u must not exceed UINT_MAX/4/hz=%u (hz=%u)",
    274  1.1  riastrad 	    max_period, UINT_MAX/4/hz, hz);
    275  1.1  riastrad 	KASSERT(mutex_owned(&heartbeat_lock));
    276  1.1  riastrad 
    277  1.1  riastrad 	/*
    278  1.1  riastrad 	 * If we're enabling heartbeat checks, make sure we have a
    279  1.1  riastrad 	 * reasonably up-to-date time_uptime cache on all CPUs so we
    280  1.1  riastrad 	 * don't think we had an instant heart attack.
    281  1.1  riastrad 	 */
    282  1.4  riastrad 	if (heartbeat_max_period_secs == 0 && max_period != 0) {
    283  1.4  riastrad 		if (cold) {
    284  1.4  riastrad 			CPU_INFO_ITERATOR cii;
    285  1.4  riastrad 			struct cpu_info *ci;
    286  1.4  riastrad 
    287  1.4  riastrad 			for (CPU_INFO_FOREACH(cii, ci))
    288  1.4  riastrad 				heartbeat_resume_cpu(ci);
    289  1.4  riastrad 		} else {
    290  1.4  riastrad 			const uint64_t ticket =
    291  1.4  riastrad 			    xc_broadcast(0, &heartbeat_reset_xc, NULL, NULL);
    292  1.4  riastrad 			xc_wait(ticket);
    293  1.4  riastrad 		}
    294  1.4  riastrad 	}
    295  1.1  riastrad 
    296  1.1  riastrad 	/*
    297  1.1  riastrad 	 * Once the heartbeat state has been updated on all (online)
    298  1.1  riastrad 	 * CPUs, set the period.  At this point, heartbeat checks can
    299  1.1  riastrad 	 * begin.
    300  1.1  riastrad 	 */
    301  1.1  riastrad 	atomic_store_relaxed(&heartbeat_max_period_secs, max_period);
    302  1.1  riastrad 	atomic_store_relaxed(&heartbeat_max_period_ticks, max_period*hz);
    303  1.1  riastrad }
    304  1.1  riastrad 
    305  1.1  riastrad /*
    306  1.1  riastrad  * heartbeat_max_period_ticks(SYSCTLFN_ARGS)
    307  1.1  riastrad  *
    308  1.1  riastrad  *	Sysctl handler for sysctl kern.heartbeat.max_period.  Verifies
    309  1.1  riastrad  *	it lies within a reasonable interval and sets it.
    310  1.1  riastrad  */
    311  1.1  riastrad static int
    312  1.1  riastrad heartbeat_max_period_sysctl(SYSCTLFN_ARGS)
    313  1.1  riastrad {
    314  1.1  riastrad 	struct sysctlnode node;
    315  1.1  riastrad 	unsigned max_period;
    316  1.1  riastrad 	int error;
    317  1.1  riastrad 
    318  1.1  riastrad 	mutex_enter(&heartbeat_lock);
    319  1.1  riastrad 
    320  1.1  riastrad 	max_period = heartbeat_max_period_secs;
    321  1.1  riastrad 	node = *rnode;
    322  1.1  riastrad 	node.sysctl_data = &max_period;
    323  1.1  riastrad 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    324  1.1  riastrad 	if (error || newp == NULL)
    325  1.1  riastrad 		goto out;
    326  1.1  riastrad 
    327  1.1  riastrad 	/*
    328  1.1  riastrad 	 * Ensure there's plenty of slop between heartbeats.
    329  1.1  riastrad 	 */
    330  1.1  riastrad 	if (max_period > UINT_MAX/4/hz) {
    331  1.1  riastrad 		error = EOVERFLOW;
    332  1.1  riastrad 		goto out;
    333  1.1  riastrad 	}
    334  1.1  riastrad 
    335  1.1  riastrad 	/*
    336  1.1  riastrad 	 * Success!  Set the period.  This enables heartbeat checks if
    337  1.1  riastrad 	 * we went from zero period to nonzero period, or disables them
    338  1.1  riastrad 	 * if the other way around.
    339  1.1  riastrad 	 */
    340  1.1  riastrad 	set_max_period(max_period);
    341  1.1  riastrad 	error = 0;
    342  1.1  riastrad 
    343  1.1  riastrad out:	mutex_exit(&heartbeat_lock);
    344  1.1  riastrad 	return error;
    345  1.1  riastrad }
    346  1.1  riastrad 
    347  1.1  riastrad /*
    348  1.1  riastrad  * sysctl_heartbeat_setup()
    349  1.1  riastrad  *
    350  1.1  riastrad  *	Set up the kern.heartbeat.* sysctl subtree.
    351  1.1  riastrad  */
    352  1.1  riastrad SYSCTL_SETUP(sysctl_heartbeat_setup, "sysctl kern.heartbeat setup")
    353  1.1  riastrad {
    354  1.1  riastrad 	const struct sysctlnode *rnode;
    355  1.1  riastrad 	int error;
    356  1.1  riastrad 
    357  1.1  riastrad 	mutex_init(&heartbeat_lock, MUTEX_DEFAULT, IPL_NONE);
    358  1.1  riastrad 
    359  1.1  riastrad 	/* kern.heartbeat */
    360  1.1  riastrad 	error = sysctl_createv(NULL, 0, NULL, &rnode,
    361  1.1  riastrad 	    CTLFLAG_PERMANENT,
    362  1.1  riastrad 	    CTLTYPE_NODE, "heartbeat",
    363  1.1  riastrad 	    SYSCTL_DESCR("Kernel heartbeat parameters"),
    364  1.1  riastrad 	    NULL, 0, NULL, 0,
    365  1.1  riastrad 	    CTL_KERN, CTL_CREATE, CTL_EOL);
    366  1.1  riastrad 	if (error) {
    367  1.1  riastrad 		printf("%s: failed to create kern.heartbeat: %d\n",
    368  1.1  riastrad 		    __func__, error);
    369  1.1  riastrad 		return;
    370  1.1  riastrad 	}
    371  1.1  riastrad 
    372  1.1  riastrad 	/* kern.heartbeat.max_period */
    373  1.1  riastrad 	error = sysctl_createv(NULL, 0, &rnode, NULL,
    374  1.1  riastrad 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    375  1.1  riastrad 	    CTLTYPE_INT, "max_period",
    376  1.1  riastrad 	    SYSCTL_DESCR("Max seconds between heartbeats before panic"),
    377  1.1  riastrad 	    &heartbeat_max_period_sysctl, 0, NULL, 0,
    378  1.1  riastrad 	    CTL_CREATE, CTL_EOL);
    379  1.1  riastrad 	if (error) {
    380  1.1  riastrad 		printf("%s: failed to create kern.heartbeat.max_period: %d\n",
    381  1.1  riastrad 		    __func__, error);
    382  1.1  riastrad 		return;
    383  1.1  riastrad 	}
    384  1.1  riastrad }
    385  1.1  riastrad 
    386  1.1  riastrad /*
    387  1.1  riastrad  * heartbeat_intr(cookie)
    388  1.1  riastrad  *
    389  1.1  riastrad  *	Soft interrupt handler to update the local CPU's view of the
    390  1.1  riastrad  *	system uptime.  This runs at the same priority level as
    391  1.1  riastrad  *	callouts, so if callouts are stuck on this CPU, it won't run,
    392  1.1  riastrad  *	and eventually another CPU will notice that this one is stuck.
    393  1.1  riastrad  *
    394  1.1  riastrad  *	Don't do spl* here -- keep it to a minimum so if anything goes
    395  1.1  riastrad  *	wrong we don't end up with hard interrupts blocked and unable
    396  1.1  riastrad  *	to detect a missed heartbeat.
    397  1.1  riastrad  */
    398  1.1  riastrad static void
    399  1.1  riastrad heartbeat_intr(void *cookie)
    400  1.1  riastrad {
    401  1.1  riastrad 	unsigned count = atomic_load_relaxed(&curcpu()->ci_heartbeat_count);
    402  1.5  riastrad 	unsigned uptime = time_uptime;
    403  1.1  riastrad 
    404  1.1  riastrad 	atomic_store_relaxed(&curcpu()->ci_heartbeat_uptime_stamp, count);
    405  1.1  riastrad 	atomic_store_relaxed(&curcpu()->ci_heartbeat_uptime_cache, uptime);
    406  1.1  riastrad }
    407  1.1  riastrad 
    408  1.1  riastrad /*
    409  1.1  riastrad  * heartbeat_start()
    410  1.1  riastrad  *
    411  1.1  riastrad  *	Start system heartbeat monitoring.
    412  1.1  riastrad  */
    413  1.1  riastrad void
    414  1.1  riastrad heartbeat_start(void)
    415  1.1  riastrad {
    416  1.1  riastrad 	const unsigned max_period = HEARTBEAT_MAX_PERIOD_DEFAULT;
    417  1.1  riastrad 
    418  1.1  riastrad 	/*
    419  1.1  riastrad 	 * Establish a softint so we can schedule it once ready.  This
    420  1.1  riastrad 	 * should be at the lowest softint priority level so that we
    421  1.1  riastrad 	 * ensure all softint priorities are making progress.
    422  1.1  riastrad 	 */
    423  1.1  riastrad 	heartbeat_sih = softint_establish(SOFTINT_CLOCK|SOFTINT_MPSAFE,
    424  1.1  riastrad 	    &heartbeat_intr, NULL);
    425  1.1  riastrad 
    426  1.1  riastrad 	/*
    427  1.1  riastrad 	 * Now that the softint is established, kick off heartbeat
    428  1.1  riastrad 	 * monitoring with the default period.  This will initialize
    429  1.1  riastrad 	 * the per-CPU state to an up-to-date cache of time_uptime.
    430  1.1  riastrad 	 */
    431  1.1  riastrad 	mutex_enter(&heartbeat_lock);
    432  1.1  riastrad 	set_max_period(max_period);
    433  1.1  riastrad 	mutex_exit(&heartbeat_lock);
    434  1.1  riastrad }
    435  1.1  riastrad 
    436  1.1  riastrad /*
    437  1.1  riastrad  * defibrillator(cookie)
    438  1.1  riastrad  *
    439  1.1  riastrad  *	IPI handler for defibrillation.  If the CPU's heart has stopped
    440  1.1  riastrad  *	beating normally, but the CPU can still execute things,
    441  1.1  riastrad  *	acknowledge the IPI to the doctor and then panic so we at least
    442  1.1  riastrad  *	get a stack trace from whatever the current CPU is stuck doing,
    443  1.1  riastrad  *	if not a core dump.
    444  1.1  riastrad  *
    445  1.1  riastrad  *	(This metaphor is a little stretched, since defibrillation is
    446  1.1  riastrad  *	usually administered when the heart is beating errattically but
    447  1.1  riastrad  *	hasn't stopped, and causes the heart to stop temporarily, and
    448  1.1  riastrad  *	one hopes it is not fatal.  But we're (software) engineers, so
    449  1.1  riastrad  *	we can stretch metaphors like silly putty in a blender.)
    450  1.1  riastrad  */
    451  1.1  riastrad static void
    452  1.1  riastrad defibrillator(void *cookie)
    453  1.1  riastrad {
    454  1.1  riastrad 	bool *ack = cookie;
    455  1.1  riastrad 
    456  1.1  riastrad 	atomic_store_relaxed(ack, true);
    457  1.1  riastrad 	panic("%s[%d %s]: heart stopped beating", cpu_name(curcpu()),
    458  1.1  riastrad 	    curlwp->l_lid,
    459  1.1  riastrad 	    curlwp->l_name ? curlwp->l_name : curproc->p_comm);
    460  1.1  riastrad }
    461  1.1  riastrad 
    462  1.1  riastrad /*
    463  1.1  riastrad  * defibrillate(ci, unsigned d)
    464  1.1  riastrad  *
    465  1.1  riastrad  *	The patient CPU ci's heart has stopped beating after d seconds.
    466  1.1  riastrad  *	Force the patient CPU ci to panic, or panic on this CPU if the
    467  1.1  riastrad  *	patient CPU doesn't respond within 1sec.
    468  1.1  riastrad  */
    469  1.1  riastrad static void __noinline
    470  1.1  riastrad defibrillate(struct cpu_info *ci, unsigned d)
    471  1.1  riastrad {
    472  1.1  riastrad 	bool ack = false;
    473  1.1  riastrad 	ipi_msg_t msg = {
    474  1.1  riastrad 		.func = &defibrillator,
    475  1.1  riastrad 		.arg = &ack,
    476  1.1  riastrad 	};
    477  1.1  riastrad 	unsigned countdown = 1000; /* 1sec */
    478  1.1  riastrad 
    479  1.2  riastrad 	KASSERT(curcpu_stable());
    480  1.1  riastrad 
    481  1.1  riastrad 	/*
    482  1.1  riastrad 	 * First notify the console that the patient CPU's heart seems
    483  1.1  riastrad 	 * to have stopped beating.
    484  1.1  riastrad 	 */
    485  1.1  riastrad 	printf("%s: found %s heart stopped beating after %u seconds\n",
    486  1.1  riastrad 	    cpu_name(curcpu()), cpu_name(ci), d);
    487  1.1  riastrad 
    488  1.1  riastrad 	/*
    489  1.1  riastrad 	 * Next, give the patient CPU a chance to panic, so we get a
    490  1.1  riastrad 	 * stack trace on that CPU even if we don't get a crash dump.
    491  1.1  riastrad 	 */
    492  1.1  riastrad 	ipi_unicast(&msg, ci);
    493  1.1  riastrad 
    494  1.1  riastrad 	/*
    495  1.1  riastrad 	 * Busy-wait up to 1sec for the patient CPU to print a stack
    496  1.1  riastrad 	 * trace and panic.  If the patient CPU acknowledges the IPI,
    497  1.1  riastrad 	 * or if we're panicking anyway, just give up and stop here --
    498  1.1  riastrad 	 * the system is coming down soon and we should avoid getting
    499  1.1  riastrad 	 * in the way.
    500  1.1  riastrad 	 */
    501  1.1  riastrad 	while (countdown --> 0) {
    502  1.1  riastrad 		if (atomic_load_relaxed(&ack) ||
    503  1.1  riastrad 		    atomic_load_relaxed(&panicstr) != NULL)
    504  1.1  riastrad 			return;
    505  1.1  riastrad 		DELAY(1000);	/* 1ms */
    506  1.1  riastrad 	}
    507  1.1  riastrad 
    508  1.1  riastrad 	/*
    509  1.1  riastrad 	 * The patient CPU failed to acknowledge the panic request.
    510  1.1  riastrad 	 * Panic now; with any luck, we'll get a crash dump.
    511  1.1  riastrad 	 */
    512  1.1  riastrad 	panic("%s: found %s heart stopped beating and unresponsive",
    513  1.1  riastrad 	    cpu_name(curcpu()), cpu_name(ci));
    514  1.1  riastrad }
    515  1.1  riastrad 
    516  1.1  riastrad /*
    517  1.1  riastrad  * select_patient()
    518  1.1  riastrad  *
    519  1.1  riastrad  *	Select another CPU to check the heartbeat of.  Returns NULL if
    520  1.1  riastrad  *	there are no other online CPUs.  Never returns curcpu().
    521  1.1  riastrad  *	Caller must have kpreemption disabled.
    522  1.1  riastrad  */
    523  1.1  riastrad static struct cpu_info *
    524  1.1  riastrad select_patient(void)
    525  1.1  riastrad {
    526  1.1  riastrad 	CPU_INFO_ITERATOR cii;
    527  1.1  riastrad 	struct cpu_info *first = NULL, *patient = NULL, *ci;
    528  1.1  riastrad 	bool passedcur = false;
    529  1.1  riastrad 
    530  1.2  riastrad 	KASSERT(curcpu_stable());
    531  1.1  riastrad 
    532  1.1  riastrad 	/*
    533  1.1  riastrad 	 * In the iteration order of all CPUs, find the next online CPU
    534  1.1  riastrad 	 * after curcpu(), or the first online one if curcpu() is last
    535  1.1  riastrad 	 * in the iteration order.
    536  1.1  riastrad 	 */
    537  1.1  riastrad 	for (CPU_INFO_FOREACH(cii, ci)) {
    538  1.6  riastrad 		if (ci->ci_schedstate.spc_flags & SPCF_HEARTBEATSUSPENDED)
    539  1.1  riastrad 			continue;
    540  1.1  riastrad 		if (passedcur) {
    541  1.1  riastrad 			/*
    542  1.1  riastrad 			 * (...|curcpu()|ci|...)
    543  1.1  riastrad 			 *
    544  1.1  riastrad 			 * Found the patient right after curcpu().
    545  1.1  riastrad 			 */
    546  1.1  riastrad 			KASSERT(patient != ci);
    547  1.1  riastrad 			patient = ci;
    548  1.1  riastrad 			break;
    549  1.1  riastrad 		}
    550  1.1  riastrad 		if (ci == curcpu()) {
    551  1.1  riastrad 			/*
    552  1.1  riastrad 			 * (...|prev|ci=curcpu()|next|...)
    553  1.1  riastrad 			 *
    554  1.1  riastrad 			 * Note that we want next (or first, if there's
    555  1.1  riastrad 			 * nothing after curcpu()).
    556  1.1  riastrad 			 */
    557  1.1  riastrad 			passedcur = true;
    558  1.1  riastrad 			continue;
    559  1.1  riastrad 		}
    560  1.1  riastrad 		if (first == NULL) {
    561  1.1  riastrad 			/*
    562  1.1  riastrad 			 * (ci|...|curcpu()|...)
    563  1.1  riastrad 			 *
    564  1.1  riastrad 			 * Record ci as first in case there's nothing
    565  1.1  riastrad 			 * after curcpu().
    566  1.1  riastrad 			 */
    567  1.1  riastrad 			first = ci;
    568  1.1  riastrad 			continue;
    569  1.1  riastrad 		}
    570  1.1  riastrad 	}
    571  1.1  riastrad 
    572  1.1  riastrad 	/*
    573  1.1  riastrad 	 * If we hit the end, wrap around to the beginning.
    574  1.1  riastrad 	 */
    575  1.1  riastrad 	if (patient == NULL) {
    576  1.1  riastrad 		KASSERT(passedcur);
    577  1.1  riastrad 		patient = first;
    578  1.1  riastrad 	}
    579  1.1  riastrad 
    580  1.1  riastrad 	return patient;
    581  1.1  riastrad }
    582  1.1  riastrad 
    583  1.1  riastrad /*
    584  1.1  riastrad  * heartbeat()
    585  1.1  riastrad  *
    586  1.1  riastrad  *	1. Count a heartbeat on the local CPU.
    587  1.1  riastrad  *
    588  1.1  riastrad  *	2. Panic if the system uptime doesn't seem to have advanced in
    589  1.1  riastrad  *	   a while.
    590  1.1  riastrad  *
    591  1.1  riastrad  *	3. Panic if the soft interrupt on this CPU hasn't advanced the
    592  1.1  riastrad  *	   local view of the system uptime.
    593  1.1  riastrad  *
    594  1.1  riastrad  *	4. Schedule the soft interrupt to advance the local view of the
    595  1.1  riastrad  *	   system uptime.
    596  1.1  riastrad  *
    597  1.1  riastrad  *	5. Select another CPU to check the heartbeat of.
    598  1.1  riastrad  *
    599  1.1  riastrad  *	6. Panic if the other CPU hasn't advanced its view of the
    600  1.1  riastrad  *	   system uptime in a while.
    601  1.1  riastrad  */
    602  1.1  riastrad void
    603  1.1  riastrad heartbeat(void)
    604  1.1  riastrad {
    605  1.1  riastrad 	unsigned period_ticks, period_secs;
    606  1.1  riastrad 	unsigned count, uptime, cache, stamp, d;
    607  1.1  riastrad 	struct cpu_info *patient;
    608  1.1  riastrad 
    609  1.2  riastrad 	KASSERT(curcpu_stable());
    610  1.1  riastrad 
    611  1.1  riastrad 	period_ticks = atomic_load_relaxed(&heartbeat_max_period_ticks);
    612  1.1  riastrad 	period_secs = atomic_load_relaxed(&heartbeat_max_period_secs);
    613  1.1  riastrad 	if (__predict_false(period_ticks == 0) ||
    614  1.1  riastrad 	    __predict_false(period_secs == 0) ||
    615  1.6  riastrad 	    __predict_false(curcpu()->ci_schedstate.spc_flags &
    616  1.6  riastrad 		SPCF_HEARTBEATSUSPENDED))
    617  1.1  riastrad 		return;
    618  1.1  riastrad 
    619  1.1  riastrad 	/*
    620  1.1  riastrad 	 * Count a heartbeat on this CPU.
    621  1.1  riastrad 	 */
    622  1.1  riastrad 	count = curcpu()->ci_heartbeat_count++;
    623  1.1  riastrad 
    624  1.1  riastrad 	/*
    625  1.1  riastrad 	 * If the uptime hasn't changed, make sure that we haven't
    626  1.1  riastrad 	 * counted too many of our own heartbeats since the uptime last
    627  1.1  riastrad 	 * changed, and stop here -- we only do the cross-CPU work once
    628  1.1  riastrad 	 * per second.
    629  1.1  riastrad 	 */
    630  1.5  riastrad 	uptime = time_uptime;
    631  1.1  riastrad 	cache = atomic_load_relaxed(&curcpu()->ci_heartbeat_uptime_cache);
    632  1.1  riastrad 	if (__predict_true(cache == uptime)) {
    633  1.1  riastrad 		/*
    634  1.1  riastrad 		 * Timecounter hasn't advanced by more than a second.
    635  1.1  riastrad 		 * Make sure the timecounter isn't stuck according to
    636  1.7  riastrad 		 * our heartbeats -- unless timecounter heartbeats are
    637  1.7  riastrad 		 * suspended too.
    638  1.1  riastrad 		 *
    639  1.1  riastrad 		 * Our own heartbeat count can't roll back, and
    640  1.1  riastrad 		 * time_uptime should be updated before it wraps
    641  1.1  riastrad 		 * around, so d should never go negative; hence no
    642  1.1  riastrad 		 * check for d < UINT_MAX/2.
    643  1.1  riastrad 		 */
    644  1.1  riastrad 		stamp =
    645  1.1  riastrad 		    atomic_load_relaxed(&curcpu()->ci_heartbeat_uptime_stamp);
    646  1.1  riastrad 		d = count - stamp;
    647  1.7  riastrad 		if (__predict_false(d > period_ticks) &&
    648  1.7  riastrad 		    !heartbeat_timecounter_suspended()) {
    649  1.1  riastrad 			panic("%s: time has not advanced in %u heartbeats",
    650  1.1  riastrad 			    cpu_name(curcpu()), d);
    651  1.1  riastrad 		}
    652  1.1  riastrad 		return;
    653  1.1  riastrad 	}
    654  1.1  riastrad 
    655  1.1  riastrad 	/*
    656  1.1  riastrad 	 * If the uptime has changed, make sure that it hasn't changed
    657  1.1  riastrad 	 * so much that softints must be stuck on this CPU.  Since
    658  1.1  riastrad 	 * time_uptime is monotonic, this can't go negative, hence no
    659  1.1  riastrad 	 * check for d < UINT_MAX/2.
    660  1.1  riastrad 	 *
    661  1.1  riastrad 	 * This uses the hard timer interrupt handler on the current
    662  1.1  riastrad 	 * CPU to ensure soft interrupts at all priority levels have
    663  1.1  riastrad 	 * made progress.
    664  1.1  riastrad 	 */
    665  1.1  riastrad 	d = uptime - cache;
    666  1.1  riastrad 	if (__predict_false(d > period_secs)) {
    667  1.1  riastrad 		panic("%s: softints stuck for %u seconds",
    668  1.1  riastrad 		    cpu_name(curcpu()), d);
    669  1.1  riastrad 	}
    670  1.1  riastrad 
    671  1.1  riastrad 	/*
    672  1.1  riastrad 	 * Schedule a softint to update our cache of the system uptime
    673  1.1  riastrad 	 * so the next call to heartbeat, on this or another CPU, can
    674  1.1  riastrad 	 * detect progress on this one.
    675  1.1  riastrad 	 */
    676  1.1  riastrad 	softint_schedule(heartbeat_sih);
    677  1.1  riastrad 
    678  1.1  riastrad 	/*
    679  1.1  riastrad 	 * Select a patient to check the heartbeat of.  If there's no
    680  1.1  riastrad 	 * other online CPU, nothing to do.
    681  1.1  riastrad 	 */
    682  1.1  riastrad 	patient = select_patient();
    683  1.1  riastrad 	if (patient == NULL)
    684  1.1  riastrad 		return;
    685  1.1  riastrad 
    686  1.1  riastrad 	/*
    687  1.1  riastrad 	 * Verify that time is advancing on the patient CPU.  If the
    688  1.1  riastrad 	 * delta exceeds UINT_MAX/2, that means it is already ahead by
    689  1.1  riastrad 	 * a little on the other CPU, and the subtraction went
    690  1.6  riastrad 	 * negative, which is OK.  If the CPU's heartbeats have been
    691  1.6  riastrad 	 * suspended since we selected it, no worries.
    692  1.1  riastrad 	 *
    693  1.1  riastrad 	 * This uses the current CPU to ensure the other CPU has made
    694  1.1  riastrad 	 * progress, even if the other CPU's hard timer interrupt
    695  1.1  riastrad 	 * handler is stuck for some reason.
    696  1.1  riastrad 	 *
    697  1.1  riastrad 	 * XXX Maybe confirm it hasn't gone negative by more than
    698  1.1  riastrad 	 * max_period?
    699  1.1  riastrad 	 */
    700  1.1  riastrad 	d = uptime - atomic_load_relaxed(&patient->ci_heartbeat_uptime_cache);
    701  1.1  riastrad 	if (__predict_false(d > period_secs) &&
    702  1.1  riastrad 	    __predict_false(d < UINT_MAX/2) &&
    703  1.6  riastrad 	    ((patient->ci_schedstate.spc_flags & SPCF_HEARTBEATSUSPENDED)
    704  1.6  riastrad 		== 0))
    705  1.1  riastrad 		defibrillate(patient, d);
    706  1.1  riastrad }
    707  1.1  riastrad 
    708  1.1  riastrad /*
    709  1.1  riastrad  * heartbeat_dump()
    710  1.1  riastrad  *
    711  1.1  riastrad  *	Print the heartbeat data of all CPUs.  Can be called from ddb.
    712  1.1  riastrad  */
    713  1.1  riastrad #ifdef DDB
    714  1.1  riastrad static unsigned
    715  1.6  riastrad db_read_unsigned(const volatile unsigned *p)
    716  1.1  riastrad {
    717  1.1  riastrad 	unsigned x;
    718  1.1  riastrad 
    719  1.6  riastrad 	db_read_bytes((db_addr_t)(uintptr_t)p, sizeof(x), (char *)&x);
    720  1.6  riastrad 
    721  1.6  riastrad 	return x;
    722  1.6  riastrad }
    723  1.6  riastrad 
    724  1.6  riastrad static int
    725  1.6  riastrad db_read_signed(const volatile int *p)
    726  1.6  riastrad {
    727  1.6  riastrad 	int x;
    728  1.6  riastrad 
    729  1.6  riastrad 	db_read_bytes((db_addr_t)(uintptr_t)p, sizeof(x), (char *)&x);
    730  1.1  riastrad 
    731  1.1  riastrad 	return x;
    732  1.1  riastrad }
    733  1.1  riastrad 
    734  1.1  riastrad void
    735  1.1  riastrad heartbeat_dump(void)
    736  1.1  riastrad {
    737  1.1  riastrad 	struct cpu_info *ci;
    738  1.1  riastrad 
    739  1.1  riastrad 	db_printf("Heartbeats:\n");
    740  1.1  riastrad 	for (ci = db_cpu_first(); ci != NULL; ci = db_cpu_next(ci)) {
    741  1.6  riastrad 		db_printf("cpu%u: count %u uptime %u stamp %u%s\n",
    742  1.1  riastrad 		    db_read_unsigned(&ci->ci_index),
    743  1.1  riastrad 		    db_read_unsigned(&ci->ci_heartbeat_count),
    744  1.1  riastrad 		    db_read_unsigned(&ci->ci_heartbeat_uptime_cache),
    745  1.6  riastrad 		    db_read_unsigned(&ci->ci_heartbeat_uptime_stamp),
    746  1.6  riastrad 		    (db_read_signed(&ci->ci_schedstate.spc_flags) &
    747  1.6  riastrad 			SPCF_HEARTBEATSUSPENDED ? " (suspended)" : ""));
    748  1.1  riastrad 	}
    749  1.1  riastrad }
    750  1.1  riastrad #endif
    751