2005-09-06 18:16:27 -04:00
|
|
|
/*
|
|
|
|
* Detect Soft Lockups
|
|
|
|
*
|
2006-03-24 06:18:41 -05:00
|
|
|
* started by Ingo Molnar, Copyright (C) 2005, 2006 Red Hat, Inc.
|
2005-09-06 18:16:27 -04:00
|
|
|
*
|
|
|
|
* this code detects soft lockups: incidents in where on a CPU
|
|
|
|
* the kernel does not reschedule for 10 seconds or more.
|
|
|
|
*/
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/kthread.h>
|
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(print_lock);
|
|
|
|
|
2006-03-24 06:18:41 -05:00
|
|
|
static DEFINE_PER_CPU(unsigned long, touch_timestamp);
|
|
|
|
static DEFINE_PER_CPU(unsigned long, print_timestamp);
|
2005-09-06 18:16:27 -04:00
|
|
|
static DEFINE_PER_CPU(struct task_struct *, watchdog_task);
|
|
|
|
|
|
|
|
static int did_panic = 0;
|
2006-03-24 06:18:41 -05:00
|
|
|
|
|
|
|
static int
|
|
|
|
softlock_panic(struct notifier_block *this, unsigned long event, void *ptr)
|
2005-09-06 18:16:27 -04:00
|
|
|
{
|
|
|
|
did_panic = 1;
|
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block panic_block = {
|
|
|
|
.notifier_call = softlock_panic,
|
|
|
|
};
|
|
|
|
|
|
|
|
void touch_softlockup_watchdog(void)
|
|
|
|
{
|
[PATCH] Define __raw_get_cpu_var and use it
There are several instances of per_cpu(foo, raw_smp_processor_id()), which
is semantically equivalent to __get_cpu_var(foo) but without the warning
that smp_processor_id() can give if CONFIG_DEBUG_PREEMPT is enabled. For
those architectures with optimized per-cpu implementations, namely ia64,
powerpc, s390, sparc64 and x86_64, per_cpu() turns into more and slower
code than __get_cpu_var(), so it would be preferable to use __get_cpu_var
on those platforms.
This defines a __raw_get_cpu_var(x) macro which turns into per_cpu(x,
raw_smp_processor_id()) on architectures that use the generic per-cpu
implementation, and turns into __get_cpu_var(x) on the architectures that
have an optimized per-cpu implementation.
Signed-off-by: Paul Mackerras <paulus@samba.org>
Acked-by: David S. Miller <davem@davemloft.net>
Acked-by: Ingo Molnar <mingo@elte.hu>
Acked-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-06-25 08:47:14 -04:00
|
|
|
__raw_get_cpu_var(touch_timestamp) = jiffies;
|
2005-09-06 18:16:27 -04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(touch_softlockup_watchdog);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This callback runs from the timer interrupt, and checks
|
|
|
|
* whether the watchdog thread has hung or not:
|
|
|
|
*/
|
2006-03-24 06:18:41 -05:00
|
|
|
void softlockup_tick(void)
|
2005-09-06 18:16:27 -04:00
|
|
|
{
|
|
|
|
int this_cpu = smp_processor_id();
|
2006-03-24 06:18:41 -05:00
|
|
|
unsigned long touch_timestamp = per_cpu(touch_timestamp, this_cpu);
|
2005-09-06 18:16:27 -04:00
|
|
|
|
2006-03-24 06:18:41 -05:00
|
|
|
/* prevent double reports: */
|
|
|
|
if (per_cpu(print_timestamp, this_cpu) == touch_timestamp ||
|
|
|
|
did_panic ||
|
|
|
|
!per_cpu(watchdog_task, this_cpu))
|
2005-09-06 18:16:27 -04:00
|
|
|
return;
|
|
|
|
|
2006-03-24 06:18:41 -05:00
|
|
|
/* do not print during early bootup: */
|
|
|
|
if (unlikely(system_state != SYSTEM_RUNNING)) {
|
|
|
|
touch_softlockup_watchdog();
|
2005-09-06 18:16:27 -04:00
|
|
|
return;
|
2006-03-24 06:18:41 -05:00
|
|
|
}
|
2005-09-06 18:16:27 -04:00
|
|
|
|
2006-03-24 06:18:41 -05:00
|
|
|
/* Wake up the high-prio watchdog task every second: */
|
|
|
|
if (time_after(jiffies, touch_timestamp + HZ))
|
|
|
|
wake_up_process(per_cpu(watchdog_task, this_cpu));
|
|
|
|
|
|
|
|
/* Warn about unreasonable 10+ seconds delays: */
|
|
|
|
if (time_after(jiffies, touch_timestamp + 10*HZ)) {
|
|
|
|
per_cpu(print_timestamp, this_cpu) = touch_timestamp;
|
2005-09-06 18:16:27 -04:00
|
|
|
|
|
|
|
spin_lock(&print_lock);
|
|
|
|
printk(KERN_ERR "BUG: soft lockup detected on CPU#%d!\n",
|
|
|
|
this_cpu);
|
2006-03-24 06:18:41 -05:00
|
|
|
dump_stack();
|
2005-09-06 18:16:27 -04:00
|
|
|
spin_unlock(&print_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The watchdog thread - runs every second and touches the timestamp.
|
|
|
|
*/
|
|
|
|
static int watchdog(void * __bind_cpu)
|
|
|
|
{
|
|
|
|
struct sched_param param = { .sched_priority = 99 };
|
|
|
|
|
|
|
|
sched_setscheduler(current, SCHED_FIFO, ¶m);
|
|
|
|
current->flags |= PF_NOFREEZE;
|
|
|
|
|
|
|
|
/*
|
2006-03-24 06:18:41 -05:00
|
|
|
* Run briefly once per second to reset the softlockup timestamp.
|
|
|
|
* If this gets delayed for more than 10 seconds then the
|
|
|
|
* debug-printout triggers in softlockup_tick().
|
2005-09-06 18:16:27 -04:00
|
|
|
*/
|
|
|
|
while (!kthread_should_stop()) {
|
2006-03-24 06:18:41 -05:00
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
2005-09-06 18:16:27 -04:00
|
|
|
touch_softlockup_watchdog();
|
2006-03-24 06:18:41 -05:00
|
|
|
schedule();
|
2005-09-06 18:16:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create/destroy watchdog threads as CPUs come and go:
|
|
|
|
*/
|
2006-07-30 06:03:35 -04:00
|
|
|
static int __cpuinit
|
2005-09-06 18:16:27 -04:00
|
|
|
cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
|
|
|
|
{
|
|
|
|
int hotcpu = (unsigned long)hcpu;
|
|
|
|
struct task_struct *p;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case CPU_UP_PREPARE:
|
|
|
|
BUG_ON(per_cpu(watchdog_task, hotcpu));
|
|
|
|
p = kthread_create(watchdog, hcpu, "watchdog/%d", hotcpu);
|
|
|
|
if (IS_ERR(p)) {
|
|
|
|
printk("watchdog for %i failed\n", hotcpu);
|
|
|
|
return NOTIFY_BAD;
|
|
|
|
}
|
2006-03-25 06:06:32 -05:00
|
|
|
per_cpu(touch_timestamp, hotcpu) = jiffies;
|
2005-09-06 18:16:27 -04:00
|
|
|
per_cpu(watchdog_task, hotcpu) = p;
|
|
|
|
kthread_bind(p, hotcpu);
|
|
|
|
break;
|
|
|
|
case CPU_ONLINE:
|
|
|
|
wake_up_process(per_cpu(watchdog_task, hotcpu));
|
|
|
|
break;
|
|
|
|
#ifdef CONFIG_HOTPLUG_CPU
|
|
|
|
case CPU_UP_CANCELED:
|
2006-06-25 08:49:10 -04:00
|
|
|
if (!per_cpu(watchdog_task, hotcpu))
|
|
|
|
break;
|
2005-09-06 18:16:27 -04:00
|
|
|
/* Unbind so it can run. Fall thru. */
|
2005-11-07 03:58:38 -05:00
|
|
|
kthread_bind(per_cpu(watchdog_task, hotcpu),
|
|
|
|
any_online_cpu(cpu_online_map));
|
2005-09-06 18:16:27 -04:00
|
|
|
case CPU_DEAD:
|
|
|
|
p = per_cpu(watchdog_task, hotcpu);
|
|
|
|
per_cpu(watchdog_task, hotcpu) = NULL;
|
|
|
|
kthread_stop(p);
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_HOTPLUG_CPU */
|
|
|
|
}
|
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
2006-07-30 06:03:35 -04:00
|
|
|
static struct notifier_block __cpuinitdata cpu_nfb = {
|
2005-09-06 18:16:27 -04:00
|
|
|
.notifier_call = cpu_callback
|
|
|
|
};
|
|
|
|
|
|
|
|
__init void spawn_softlockup_task(void)
|
|
|
|
{
|
|
|
|
void *cpu = (void *)(long)smp_processor_id();
|
2006-09-29 05:00:22 -04:00
|
|
|
int err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
|
2005-09-06 18:16:27 -04:00
|
|
|
|
2006-09-29 05:00:22 -04:00
|
|
|
BUG_ON(err == NOTIFY_BAD);
|
2005-09-06 18:16:27 -04:00
|
|
|
cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
|
|
|
|
register_cpu_notifier(&cpu_nfb);
|
|
|
|
|
[PATCH] Notifier chain update: API changes
The kernel's implementation of notifier chains is unsafe. There is no
protection against entries being added to or removed from a chain while the
chain is in use. The issues were discussed in this thread:
http://marc.theaimsgroup.com/?l=linux-kernel&m=113018709002036&w=2
We noticed that notifier chains in the kernel fall into two basic usage
classes:
"Blocking" chains are always called from a process context
and the callout routines are allowed to sleep;
"Atomic" chains can be called from an atomic context and
the callout routines are not allowed to sleep.
We decided to codify this distinction and make it part of the API. Therefore
this set of patches introduces three new, parallel APIs: one for blocking
notifiers, one for atomic notifiers, and one for "raw" notifiers (which is
really just the old API under a new name). New kinds of data structures are
used for the heads of the chains, and new routines are defined for
registration, unregistration, and calling a chain. The three APIs are
explained in include/linux/notifier.h and their implementation is in
kernel/sys.c.
With atomic and blocking chains, the implementation guarantees that the chain
links will not be corrupted and that chain callers will not get messed up by
entries being added or removed. For raw chains the implementation provides no
guarantees at all; users of this API must provide their own protections. (The
idea was that situations may come up where the assumptions of the atomic and
blocking APIs are not appropriate, so it should be possible for users to
handle these things in their own way.)
There are some limitations, which should not be too hard to live with. For
atomic/blocking chains, registration and unregistration must always be done in
a process context since the chain is protected by a mutex/rwsem. Also, a
callout routine for a non-raw chain must not try to register or unregister
entries on its own chain. (This did happen in a couple of places and the code
had to be changed to avoid it.)
Since atomic chains may be called from within an NMI handler, they cannot use
spinlocks for synchronization. Instead we use RCU. The overhead falls almost
entirely in the unregister routine, which is okay since unregistration is much
less frequent that calling a chain.
Here is the list of chains that we adjusted and their classifications. None
of them use the raw API, so for the moment it is only a placeholder.
ATOMIC CHAINS
-------------
arch/i386/kernel/traps.c: i386die_chain
arch/ia64/kernel/traps.c: ia64die_chain
arch/powerpc/kernel/traps.c: powerpc_die_chain
arch/sparc64/kernel/traps.c: sparc64die_chain
arch/x86_64/kernel/traps.c: die_chain
drivers/char/ipmi/ipmi_si_intf.c: xaction_notifier_list
kernel/panic.c: panic_notifier_list
kernel/profile.c: task_free_notifier
net/bluetooth/hci_core.c: hci_notifier
net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_chain
net/ipv4/netfilter/ip_conntrack_core.c: ip_conntrack_expect_chain
net/ipv6/addrconf.c: inet6addr_chain
net/netfilter/nf_conntrack_core.c: nf_conntrack_chain
net/netfilter/nf_conntrack_core.c: nf_conntrack_expect_chain
net/netlink/af_netlink.c: netlink_chain
BLOCKING CHAINS
---------------
arch/powerpc/platforms/pseries/reconfig.c: pSeries_reconfig_chain
arch/s390/kernel/process.c: idle_chain
arch/x86_64/kernel/process.c idle_notifier
drivers/base/memory.c: memory_chain
drivers/cpufreq/cpufreq.c cpufreq_policy_notifier_list
drivers/cpufreq/cpufreq.c cpufreq_transition_notifier_list
drivers/macintosh/adb.c: adb_client_list
drivers/macintosh/via-pmu.c sleep_notifier_list
drivers/macintosh/via-pmu68k.c sleep_notifier_list
drivers/macintosh/windfarm_core.c wf_client_list
drivers/usb/core/notify.c usb_notifier_list
drivers/video/fbmem.c fb_notifier_list
kernel/cpu.c cpu_chain
kernel/module.c module_notify_list
kernel/profile.c munmap_notifier
kernel/profile.c task_exit_notifier
kernel/sys.c reboot_notifier_list
net/core/dev.c netdev_chain
net/decnet/dn_dev.c: dnaddr_chain
net/ipv4/devinet.c: inetaddr_chain
It's possible that some of these classifications are wrong. If they are,
please let us know or submit a patch to fix them. Note that any chain that
gets called very frequently should be atomic, because the rwsem read-locking
used for blocking chains is very likely to incur cache misses on SMP systems.
(However, if the chain's callout routines may sleep then the chain cannot be
atomic.)
The patch set was written by Alan Stern and Chandra Seetharaman, incorporating
material written by Keith Owens and suggestions from Paul McKenney and Andrew
Morton.
[jes@sgi.com: restructure the notifier chain initialization macros]
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Chandra Seetharaman <sekharan@us.ibm.com>
Signed-off-by: Jes Sorensen <jes@sgi.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-03-27 04:16:30 -05:00
|
|
|
atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
|
2005-09-06 18:16:27 -04:00
|
|
|
}
|