From owner-svn-src-stable@freebsd.org Wed Aug 10 13:38:46 2016 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 46A83BB4318; Wed, 10 Aug 2016 13:38:46 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 0AB491F7C; Wed, 10 Aug 2016 13:38:45 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u7ADcjZn072567; Wed, 10 Aug 2016 13:38:45 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u7ADcivv072563; Wed, 10 Aug 2016 13:38:44 GMT (envelope-from kib@FreeBSD.org) Message-Id: <201608101338.u7ADcivv072563@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Wed, 10 Aug 2016 13:38:44 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r303912 - in stable/11/sys: amd64/amd64 conf i386/i386 x86/x86 X-SVN-Group: stable-11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Aug 2016 13:38:46 -0000 Author: kib Date: Wed Aug 10 13:38:44 2016 New Revision: 303912 URL: https://svnweb.freebsd.org/changeset/base/303912 Log: MFC r303712: Merge i386 and amd64 variants of mp_watchdog.c into x86/. Approved by: re (gjb) Added: stable/11/sys/x86/x86/mp_watchdog.c - copied unchanged from r303712, head/sys/x86/x86/mp_watchdog.c Deleted: stable/11/sys/amd64/amd64/mp_watchdog.c stable/11/sys/i386/i386/mp_watchdog.c Modified: stable/11/sys/conf/files.amd64 stable/11/sys/conf/files.i386 stable/11/sys/conf/files.pc98 Directory Properties: stable/11/ (props changed) Modified: stable/11/sys/conf/files.amd64 ============================================================================== --- stable/11/sys/conf/files.amd64 Wed Aug 10 13:32:27 2016 (r303911) +++ stable/11/sys/conf/files.amd64 Wed Aug 10 13:38:44 2016 (r303912) @@ -123,7 +123,6 @@ amd64/amd64/machdep.c standard amd64/amd64/mem.c optional mem amd64/amd64/minidump_machdep.c standard amd64/amd64/mp_machdep.c optional smp -amd64/amd64/mp_watchdog.c optional mp_watchdog smp amd64/amd64/mpboot.S optional smp amd64/amd64/pmap.c standard amd64/amd64/prof_machdep.c optional profiling-routine @@ -619,6 +618,7 @@ x86/x86/mca.c standard x86/x86/mptable.c optional mptable x86/x86/mptable_pci.c optional mptable pci x86/x86/mp_x86.c optional smp +x86/x86/mp_watchdog.c optional mp_watchdog smp x86/x86/msi.c optional pci x86/x86/nexus.c standard x86/x86/pvclock.c standard Modified: stable/11/sys/conf/files.i386 ============================================================================== --- stable/11/sys/conf/files.i386 Wed Aug 10 13:32:27 2016 (r303911) +++ stable/11/sys/conf/files.i386 Wed Aug 10 13:38:44 2016 (r303912) @@ -479,7 +479,6 @@ i386/i386/mem.c optional mem i386/i386/minidump_machdep.c standard i386/i386/mp_clock.c optional smp i386/i386/mp_machdep.c optional smp -i386/i386/mp_watchdog.c optional mp_watchdog smp i386/i386/mpboot.s optional smp i386/i386/perfmon.c optional perfmon i386/i386/pmap.c standard @@ -610,6 +609,7 @@ x86/x86/mca.c standard x86/x86/mptable.c optional apic x86/x86/mptable_pci.c optional apic pci x86/x86/mp_x86.c optional smp +x86/x86/mp_watchdog.c optional mp_watchdog smp x86/x86/msi.c optional apic pci x86/x86/nexus.c standard x86/x86/stack_machdep.c optional ddb | stack Modified: stable/11/sys/conf/files.pc98 ============================================================================== --- stable/11/sys/conf/files.pc98 Wed Aug 10 13:32:27 2016 (r303911) +++ stable/11/sys/conf/files.pc98 Wed Aug 10 13:38:44 2016 (r303912) @@ -168,7 +168,6 @@ i386/i386/mem.c optional mem i386/i386/minidump_machdep.c standard i386/i386/mp_clock.c optional smp i386/i386/mp_machdep.c optional smp -i386/i386/mp_watchdog.c optional mp_watchdog smp i386/i386/mpboot.s optional smp i386/i386/perfmon.c optional perfmon i386/i386/pmap.c standard @@ -269,9 +268,10 @@ x86/x86/io_apic.c optional apic x86/x86/legacy.c standard x86/x86/local_apic.c optional apic x86/x86/mca.c standard -x86/x86/mp_x86.c optional smp x86/x86/mptable.c optional apic x86/x86/mptable_pci.c optional apic pci +x86/x86/mp_x86.c optional smp +x86/x86/mp_watchdog.c optional mp_watchdog smp x86/x86/msi.c optional apic pci x86/x86/nexus.c standard x86/x86/stack_machdep.c optional ddb | stack Copied: stable/11/sys/x86/x86/mp_watchdog.c (from r303712, head/sys/x86/x86/mp_watchdog.c) ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ stable/11/sys/x86/x86/mp_watchdog.c Wed Aug 10 13:38:44 2016 (r303912, copy of r303712, head/sys/x86/x86/mp_watchdog.c) @@ -0,0 +1,210 @@ +/*- + * Copyright (c) 2004 Robert N. M. Watson + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include "opt_mp_watchdog.h" +#include "opt_sched.h" + +#ifdef SCHED_ULE +#error MP_WATCHDOG cannot currently be used with SCHED_ULE +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +/* + * mp_watchdog hijacks the idle thread on a specified CPU, prevents new work + * from being scheduled there, and uses it as a "watchdog" to detect kernel + * failure on other CPUs. This is made reasonable by inclusion of logical + * processors in Xeon hardware. The watchdog is configured by setting the + * debug.watchdog sysctl/tunable to the CPU of interest. A callout will then + * begin executing reseting a timer that is gradually lowered by the watching + * thread. If the timer reaches 0, the watchdog fires by ether dropping + * directly to the debugger, or by sending an NMI IPI to the boot processor. + * This is a somewhat less efficient substitute for dedicated watchdog + * hardware, but can be quite an effective tool for debugging hangs. + * + * XXXRW: This should really use the watchdog(9)/watchdog(4) framework, but + * doesn't yet. + */ +static int watchdog_cpu = -1; +static int watchdog_dontfire = 1; +static int watchdog_timer = -1; +static int watchdog_nmi = 1; + +SYSCTL_INT(_debug, OID_AUTO, watchdog_nmi, CTLFLAG_RWTUN, &watchdog_nmi, 0, + "IPI the boot processor with an NMI to enter the debugger"); + +static struct callout watchdog_callout; + +static void watchdog_change(int wdcpu); + +/* + * Number of seconds before the watchdog will fire if the callout fails to + * reset the timer. + */ +#define WATCHDOG_THRESHOLD 10 + +static void +watchdog_init(void *arg) +{ + + callout_init(&watchdog_callout, 1); + if (watchdog_cpu != -1) + watchdog_change(watchdog_cpu); +} + +/* + * This callout resets a timer until the watchdog kicks in. It acquires some + * critical locks to make sure things haven't gotten wedged with those locks + * held. + */ +static void +watchdog_function(void *arg) +{ + + /* + * Since the timer ran, we must not be wedged. Acquire some critical + * locks to make sure. Then reset the timer. + */ + mtx_lock(&Giant); + watchdog_timer = WATCHDOG_THRESHOLD; + mtx_unlock(&Giant); + callout_reset(&watchdog_callout, 1 * hz, watchdog_function, NULL); +} +SYSINIT(watchdog_init, SI_SUB_DRIVERS, SI_ORDER_ANY, watchdog_init, NULL); + +static void +watchdog_change(int wdcpu) +{ + + if (wdcpu == -1 || wdcpu == 0xffffffff) { + /* + * Disable the watchdog. + */ + watchdog_cpu = -1; + watchdog_dontfire = 1; + callout_stop(&watchdog_callout); + printf("watchdog stopped\n"); + } else { + watchdog_timer = WATCHDOG_THRESHOLD; + watchdog_dontfire = 0; + watchdog_cpu = wdcpu; + callout_reset(&watchdog_callout, 1 * hz, watchdog_function, + NULL); + } +} + +/* + * This sysctl sets which CPU is the watchdog CPU. Set to -1 or 0xffffffff + * to disable the watchdog. + */ +static int +sysctl_watchdog(SYSCTL_HANDLER_ARGS) +{ + int error, temp; + + temp = watchdog_cpu; + error = sysctl_handle_int(oidp, &temp, 0, req); + if (error) + return (error); + + if (req->newptr != NULL) + watchdog_change(temp); + return (0); +} +SYSCTL_PROC(_debug, OID_AUTO, watchdog, CTLTYPE_INT|CTLFLAG_RW, 0, 0, + sysctl_watchdog, "I", ""); + +/* + * Drop into the debugger by sending an IPI NMI to the boot processor. + */ +static void +watchdog_ipi_nmi(void) +{ + + /* + * Deliver NMI to the boot processor. Why not? + */ + lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE | + APIC_LEVEL_ASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_NMI, + boot_cpu_id); + lapic_ipi_wait(-1); +} + +/* + * ap_watchdog() is called by the SMP idle loop code. It works on the same + * premise that the disabling of logical processors does: that if the cpu is + * idle, then it can ignore the world from then on, as nothing will be + * scheduled on it. Leaving aside multi-runqueue schedulers (SCHED_ULE) and + * explicit process migration (sched_bind()), this is not an unreasonable + * assumption. + */ +void +ap_watchdog(u_int cpuid) +{ + char old_pcomm[MAXCOMLEN + 1]; + struct proc *p; + + if (watchdog_cpu != cpuid) + return; + + printf("watchdog started on cpu %d\n", cpuid); + p = curproc; + bcopy(p->p_comm, old_pcomm, MAXCOMLEN + 1); + snprintf(p->p_comm, MAXCOMLEN + 1, "mp_watchdog cpu %d", cpuid); + while (1) { + DELAY(1000000); /* One second. */ + if (watchdog_cpu != cpuid) + break; + atomic_subtract_int(&watchdog_timer, 1); + if (watchdog_timer < 4) + printf("Watchdog timer: %d\n", watchdog_timer); + if (watchdog_timer == 0 && watchdog_dontfire == 0) { + printf("Watchdog firing!\n"); + watchdog_dontfire = 1; + if (watchdog_nmi) + watchdog_ipi_nmi(); + else + kdb_enter(KDB_WHY_WATCHDOG, "mp_watchdog"); + } + } + bcopy(old_pcomm, p->p_comm, MAXCOMLEN + 1); + printf("watchdog stopped on cpu %d\n", cpuid); +}