mce.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. /*
  2. * Machine check handler.
  3. *
  4. * K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs.
  5. * Rest from unknown author(s).
  6. * 2004 Andi Kleen. Rewrote most of it.
  7. * Copyright 2008 Intel Corporation
  8. * Author: Andi Kleen
  9. */
  10. #include <linux/thread_info.h>
  11. #include <linux/capability.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/kobject.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/kernel.h>
  21. #include <linux/percpu.h>
  22. #include <linux/string.h>
  23. #include <linux/sysdev.h>
  24. #include <linux/delay.h>
  25. #include <linux/ctype.h>
  26. #include <linux/sched.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/types.h>
  29. #include <linux/init.h>
  30. #include <linux/kmod.h>
  31. #include <linux/poll.h>
  32. #include <linux/nmi.h>
  33. #include <linux/cpu.h>
  34. #include <linux/smp.h>
  35. #include <linux/fs.h>
  36. #include <linux/mm.h>
  37. #include <asm/processor.h>
  38. #include <asm/hw_irq.h>
  39. #include <asm/apic.h>
  40. #include <asm/idle.h>
  41. #include <asm/ipi.h>
  42. #include <asm/mce.h>
  43. #include <asm/msr.h>
  44. #include "mce-internal.h"
  45. #include "mce.h"
  46. /* Handle unconfigured int18 (should never happen) */
  47. static void unexpected_machine_check(struct pt_regs *regs, long error_code)
  48. {
  49. printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
  50. smp_processor_id());
  51. }
  52. /* Call the installed machine check handler for this CPU setup. */
  53. void (*machine_check_vector)(struct pt_regs *, long error_code) =
  54. unexpected_machine_check;
  55. int mce_disabled;
  56. #ifdef CONFIG_X86_NEW_MCE
  57. #define MISC_MCELOG_MINOR 227
  58. #define SPINUNIT 100 /* 100ns */
  59. atomic_t mce_entry;
  60. DEFINE_PER_CPU(unsigned, mce_exception_count);
  61. /*
  62. * Tolerant levels:
  63. * 0: always panic on uncorrected errors, log corrected errors
  64. * 1: panic or SIGBUS on uncorrected errors, log corrected errors
  65. * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
  66. * 3: never panic or SIGBUS, log all errors (for testing only)
  67. */
  68. static int tolerant = 1;
  69. static int banks;
  70. static u64 *bank;
  71. static unsigned long notify_user;
  72. static int rip_msr;
  73. static int mce_bootlog = -1;
  74. static int monarch_timeout = -1;
  75. static int mce_panic_timeout;
  76. int mce_ser;
  77. static char trigger[128];
  78. static char *trigger_argv[2] = { trigger, NULL };
  79. static unsigned long dont_init_banks;
  80. static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
  81. static DEFINE_PER_CPU(struct mce, mces_seen);
  82. static int cpu_missing;
  83. /* MCA banks polled by the period polling timer for corrected events */
  84. DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
  85. [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
  86. };
  87. static inline int skip_bank_init(int i)
  88. {
  89. return i < BITS_PER_LONG && test_bit(i, &dont_init_banks);
  90. }
  91. static DEFINE_PER_CPU(struct work_struct, mce_work);
  92. /* Do initial initialization of a struct mce */
  93. void mce_setup(struct mce *m)
  94. {
  95. memset(m, 0, sizeof(struct mce));
  96. m->cpu = m->extcpu = smp_processor_id();
  97. rdtscll(m->tsc);
  98. /* We hope get_seconds stays lockless */
  99. m->time = get_seconds();
  100. m->cpuvendor = boot_cpu_data.x86_vendor;
  101. m->cpuid = cpuid_eax(1);
  102. #ifdef CONFIG_SMP
  103. m->socketid = cpu_data(m->extcpu).phys_proc_id;
  104. #endif
  105. m->apicid = cpu_data(m->extcpu).initial_apicid;
  106. rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
  107. }
  108. DEFINE_PER_CPU(struct mce, injectm);
  109. EXPORT_PER_CPU_SYMBOL_GPL(injectm);
  110. /*
  111. * Lockless MCE logging infrastructure.
  112. * This avoids deadlocks on printk locks without having to break locks. Also
  113. * separate MCEs from kernel messages to avoid bogus bug reports.
  114. */
  115. static struct mce_log mcelog = {
  116. .signature = MCE_LOG_SIGNATURE,
  117. .len = MCE_LOG_LEN,
  118. .recordlen = sizeof(struct mce),
  119. };
  120. void mce_log(struct mce *mce)
  121. {
  122. unsigned next, entry;
  123. mce->finished = 0;
  124. wmb();
  125. for (;;) {
  126. entry = rcu_dereference(mcelog.next);
  127. for (;;) {
  128. /*
  129. * When the buffer fills up discard new entries.
  130. * Assume that the earlier errors are the more
  131. * interesting ones:
  132. */
  133. if (entry >= MCE_LOG_LEN) {
  134. set_bit(MCE_OVERFLOW,
  135. (unsigned long *)&mcelog.flags);
  136. return;
  137. }
  138. /* Old left over entry. Skip: */
  139. if (mcelog.entry[entry].finished) {
  140. entry++;
  141. continue;
  142. }
  143. break;
  144. }
  145. smp_rmb();
  146. next = entry + 1;
  147. if (cmpxchg(&mcelog.next, entry, next) == entry)
  148. break;
  149. }
  150. memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
  151. wmb();
  152. mcelog.entry[entry].finished = 1;
  153. wmb();
  154. mce->finished = 1;
  155. set_bit(0, &notify_user);
  156. }
  157. static void print_mce(struct mce *m, int *first)
  158. {
  159. if (*first) {
  160. printk(KERN_EMERG "\n" KERN_EMERG "HARDWARE ERROR\n");
  161. *first = 0;
  162. }
  163. printk(KERN_EMERG
  164. "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
  165. m->extcpu, m->mcgstatus, m->bank, m->status);
  166. if (m->ip) {
  167. printk(KERN_EMERG "RIP%s %02x:<%016Lx> ",
  168. !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
  169. m->cs, m->ip);
  170. if (m->cs == __KERNEL_CS)
  171. print_symbol("{%s}", m->ip);
  172. printk("\n");
  173. }
  174. printk(KERN_EMERG "TSC %llx ", m->tsc);
  175. if (m->addr)
  176. printk("ADDR %llx ", m->addr);
  177. if (m->misc)
  178. printk("MISC %llx ", m->misc);
  179. printk("\n");
  180. printk(KERN_EMERG "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
  181. m->cpuvendor, m->cpuid, m->time, m->socketid,
  182. m->apicid);
  183. }
  184. static void print_mce_tail(void)
  185. {
  186. printk(KERN_EMERG "This is not a software problem!\n"
  187. KERN_EMERG "Run through mcelog --ascii to decode and contact your hardware vendor\n");
  188. }
  189. #define PANIC_TIMEOUT 5 /* 5 seconds */
  190. static atomic_t mce_paniced;
  191. /* Panic in progress. Enable interrupts and wait for final IPI */
  192. static void wait_for_panic(void)
  193. {
  194. long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
  195. preempt_disable();
  196. local_irq_enable();
  197. while (timeout-- > 0)
  198. udelay(1);
  199. if (panic_timeout == 0)
  200. panic_timeout = mce_panic_timeout;
  201. panic("Panicing machine check CPU died");
  202. }
  203. static void mce_panic(char *msg, struct mce *final, char *exp)
  204. {
  205. int i;
  206. int first = 1;
  207. /*
  208. * Make sure only one CPU runs in machine check panic
  209. */
  210. if (atomic_add_return(1, &mce_paniced) > 1)
  211. wait_for_panic();
  212. barrier();
  213. bust_spinlocks(1);
  214. console_verbose();
  215. /* First print corrected ones that are still unlogged */
  216. for (i = 0; i < MCE_LOG_LEN; i++) {
  217. struct mce *m = &mcelog.entry[i];
  218. if ((m->status & MCI_STATUS_VAL) &&
  219. !(m->status & MCI_STATUS_UC))
  220. print_mce(m, &first);
  221. }
  222. /* Now print uncorrected but with the final one last */
  223. for (i = 0; i < MCE_LOG_LEN; i++) {
  224. struct mce *m = &mcelog.entry[i];
  225. if (!(m->status & MCI_STATUS_VAL))
  226. continue;
  227. if (!final || memcmp(m, final, sizeof(struct mce)))
  228. print_mce(m, &first);
  229. }
  230. if (final)
  231. print_mce(final, &first);
  232. if (cpu_missing)
  233. printk(KERN_EMERG "Some CPUs didn't answer in synchronization\n");
  234. print_mce_tail();
  235. if (exp)
  236. printk(KERN_EMERG "Machine check: %s\n", exp);
  237. if (panic_timeout == 0)
  238. panic_timeout = mce_panic_timeout;
  239. panic(msg);
  240. }
  241. /* Support code for software error injection */
  242. static int msr_to_offset(u32 msr)
  243. {
  244. unsigned bank = __get_cpu_var(injectm.bank);
  245. if (msr == rip_msr)
  246. return offsetof(struct mce, ip);
  247. if (msr == MSR_IA32_MC0_STATUS + bank*4)
  248. return offsetof(struct mce, status);
  249. if (msr == MSR_IA32_MC0_ADDR + bank*4)
  250. return offsetof(struct mce, addr);
  251. if (msr == MSR_IA32_MC0_MISC + bank*4)
  252. return offsetof(struct mce, misc);
  253. if (msr == MSR_IA32_MCG_STATUS)
  254. return offsetof(struct mce, mcgstatus);
  255. return -1;
  256. }
  257. /* MSR access wrappers used for error injection */
  258. static u64 mce_rdmsrl(u32 msr)
  259. {
  260. u64 v;
  261. if (__get_cpu_var(injectm).finished) {
  262. int offset = msr_to_offset(msr);
  263. if (offset < 0)
  264. return 0;
  265. return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
  266. }
  267. rdmsrl(msr, v);
  268. return v;
  269. }
  270. static void mce_wrmsrl(u32 msr, u64 v)
  271. {
  272. if (__get_cpu_var(injectm).finished) {
  273. int offset = msr_to_offset(msr);
  274. if (offset >= 0)
  275. *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
  276. return;
  277. }
  278. wrmsrl(msr, v);
  279. }
  280. /*
  281. * Simple lockless ring to communicate PFNs from the exception handler with the
  282. * process context work function. This is vastly simplified because there's
  283. * only a single reader and a single writer.
  284. */
  285. #define MCE_RING_SIZE 16 /* we use one entry less */
  286. struct mce_ring {
  287. unsigned short start;
  288. unsigned short end;
  289. unsigned long ring[MCE_RING_SIZE];
  290. };
  291. static DEFINE_PER_CPU(struct mce_ring, mce_ring);
  292. /* Runs with CPU affinity in workqueue */
  293. static int mce_ring_empty(void)
  294. {
  295. struct mce_ring *r = &__get_cpu_var(mce_ring);
  296. return r->start == r->end;
  297. }
  298. static int mce_ring_get(unsigned long *pfn)
  299. {
  300. struct mce_ring *r;
  301. int ret = 0;
  302. *pfn = 0;
  303. get_cpu();
  304. r = &__get_cpu_var(mce_ring);
  305. if (r->start == r->end)
  306. goto out;
  307. *pfn = r->ring[r->start];
  308. r->start = (r->start + 1) % MCE_RING_SIZE;
  309. ret = 1;
  310. out:
  311. put_cpu();
  312. return ret;
  313. }
  314. /* Always runs in MCE context with preempt off */
  315. static int mce_ring_add(unsigned long pfn)
  316. {
  317. struct mce_ring *r = &__get_cpu_var(mce_ring);
  318. unsigned next;
  319. next = (r->end + 1) % MCE_RING_SIZE;
  320. if (next == r->start)
  321. return -1;
  322. r->ring[r->end] = pfn;
  323. wmb();
  324. r->end = next;
  325. return 0;
  326. }
  327. int mce_available(struct cpuinfo_x86 *c)
  328. {
  329. if (mce_disabled)
  330. return 0;
  331. return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
  332. }
  333. static void mce_schedule_work(void)
  334. {
  335. if (!mce_ring_empty()) {
  336. struct work_struct *work = &__get_cpu_var(mce_work);
  337. if (!work_pending(work))
  338. schedule_work(work);
  339. }
  340. }
  341. /*
  342. * Get the address of the instruction at the time of the machine check
  343. * error.
  344. */
  345. static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
  346. {
  347. if (regs && (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV))) {
  348. m->ip = regs->ip;
  349. m->cs = regs->cs;
  350. } else {
  351. m->ip = 0;
  352. m->cs = 0;
  353. }
  354. if (rip_msr)
  355. m->ip = mce_rdmsrl(rip_msr);
  356. }
  357. #ifdef CONFIG_X86_LOCAL_APIC
  358. /*
  359. * Called after interrupts have been reenabled again
  360. * when a MCE happened during an interrupts off region
  361. * in the kernel.
  362. */
  363. asmlinkage void smp_mce_self_interrupt(struct pt_regs *regs)
  364. {
  365. ack_APIC_irq();
  366. exit_idle();
  367. irq_enter();
  368. mce_notify_irq();
  369. mce_schedule_work();
  370. irq_exit();
  371. }
  372. #endif
  373. static void mce_report_event(struct pt_regs *regs)
  374. {
  375. if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
  376. mce_notify_irq();
  377. /*
  378. * Triggering the work queue here is just an insurance
  379. * policy in case the syscall exit notify handler
  380. * doesn't run soon enough or ends up running on the
  381. * wrong CPU (can happen when audit sleeps)
  382. */
  383. mce_schedule_work();
  384. return;
  385. }
  386. #ifdef CONFIG_X86_LOCAL_APIC
  387. /*
  388. * Without APIC do not notify. The event will be picked
  389. * up eventually.
  390. */
  391. if (!cpu_has_apic)
  392. return;
  393. /*
  394. * When interrupts are disabled we cannot use
  395. * kernel services safely. Trigger an self interrupt
  396. * through the APIC to instead do the notification
  397. * after interrupts are reenabled again.
  398. */
  399. apic->send_IPI_self(MCE_SELF_VECTOR);
  400. /*
  401. * Wait for idle afterwards again so that we don't leave the
  402. * APIC in a non idle state because the normal APIC writes
  403. * cannot exclude us.
  404. */
  405. apic_wait_icr_idle();
  406. #endif
  407. }
  408. DEFINE_PER_CPU(unsigned, mce_poll_count);
  409. /*
  410. * Poll for corrected events or events that happened before reset.
  411. * Those are just logged through /dev/mcelog.
  412. *
  413. * This is executed in standard interrupt context.
  414. *
  415. * Note: spec recommends to panic for fatal unsignalled
  416. * errors here. However this would be quite problematic --
  417. * we would need to reimplement the Monarch handling and
  418. * it would mess up the exclusion between exception handler
  419. * and poll hander -- * so we skip this for now.
  420. * These cases should not happen anyways, or only when the CPU
  421. * is already totally * confused. In this case it's likely it will
  422. * not fully execute the machine check handler either.
  423. */
  424. void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
  425. {
  426. struct mce m;
  427. int i;
  428. __get_cpu_var(mce_poll_count)++;
  429. mce_setup(&m);
  430. m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  431. for (i = 0; i < banks; i++) {
  432. if (!bank[i] || !test_bit(i, *b))
  433. continue;
  434. m.misc = 0;
  435. m.addr = 0;
  436. m.bank = i;
  437. m.tsc = 0;
  438. barrier();
  439. m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
  440. if (!(m.status & MCI_STATUS_VAL))
  441. continue;
  442. /*
  443. * Uncorrected or signalled events are handled by the exception
  444. * handler when it is enabled, so don't process those here.
  445. *
  446. * TBD do the same check for MCI_STATUS_EN here?
  447. */
  448. if (!(flags & MCP_UC) &&
  449. (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
  450. continue;
  451. if (m.status & MCI_STATUS_MISCV)
  452. m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
  453. if (m.status & MCI_STATUS_ADDRV)
  454. m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
  455. if (!(flags & MCP_TIMESTAMP))
  456. m.tsc = 0;
  457. /*
  458. * Don't get the IP here because it's unlikely to
  459. * have anything to do with the actual error location.
  460. */
  461. if (!(flags & MCP_DONTLOG)) {
  462. mce_log(&m);
  463. add_taint(TAINT_MACHINE_CHECK);
  464. }
  465. /*
  466. * Clear state for this bank.
  467. */
  468. mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  469. }
  470. /*
  471. * Don't clear MCG_STATUS here because it's only defined for
  472. * exceptions.
  473. */
  474. sync_core();
  475. }
  476. EXPORT_SYMBOL_GPL(machine_check_poll);
  477. /*
  478. * Do a quick check if any of the events requires a panic.
  479. * This decides if we keep the events around or clear them.
  480. */
  481. static int mce_no_way_out(struct mce *m, char **msg)
  482. {
  483. int i;
  484. for (i = 0; i < banks; i++) {
  485. m->status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
  486. if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
  487. return 1;
  488. }
  489. return 0;
  490. }
  491. /*
  492. * Variable to establish order between CPUs while scanning.
  493. * Each CPU spins initially until executing is equal its number.
  494. */
  495. static atomic_t mce_executing;
  496. /*
  497. * Defines order of CPUs on entry. First CPU becomes Monarch.
  498. */
  499. static atomic_t mce_callin;
  500. /*
  501. * Check if a timeout waiting for other CPUs happened.
  502. */
  503. static int mce_timed_out(u64 *t)
  504. {
  505. /*
  506. * The others already did panic for some reason.
  507. * Bail out like in a timeout.
  508. * rmb() to tell the compiler that system_state
  509. * might have been modified by someone else.
  510. */
  511. rmb();
  512. if (atomic_read(&mce_paniced))
  513. wait_for_panic();
  514. if (!monarch_timeout)
  515. goto out;
  516. if ((s64)*t < SPINUNIT) {
  517. /* CHECKME: Make panic default for 1 too? */
  518. if (tolerant < 1)
  519. mce_panic("Timeout synchronizing machine check over CPUs",
  520. NULL, NULL);
  521. cpu_missing = 1;
  522. return 1;
  523. }
  524. *t -= SPINUNIT;
  525. out:
  526. touch_nmi_watchdog();
  527. return 0;
  528. }
  529. /*
  530. * The Monarch's reign. The Monarch is the CPU who entered
  531. * the machine check handler first. It waits for the others to
  532. * raise the exception too and then grades them. When any
  533. * error is fatal panic. Only then let the others continue.
  534. *
  535. * The other CPUs entering the MCE handler will be controlled by the
  536. * Monarch. They are called Subjects.
  537. *
  538. * This way we prevent any potential data corruption in a unrecoverable case
  539. * and also makes sure always all CPU's errors are examined.
  540. *
  541. * Also this detects the case of an machine check event coming from outer
  542. * space (not detected by any CPUs) In this case some external agent wants
  543. * us to shut down, so panic too.
  544. *
  545. * The other CPUs might still decide to panic if the handler happens
  546. * in a unrecoverable place, but in this case the system is in a semi-stable
  547. * state and won't corrupt anything by itself. It's ok to let the others
  548. * continue for a bit first.
  549. *
  550. * All the spin loops have timeouts; when a timeout happens a CPU
  551. * typically elects itself to be Monarch.
  552. */
  553. static void mce_reign(void)
  554. {
  555. int cpu;
  556. struct mce *m = NULL;
  557. int global_worst = 0;
  558. char *msg = NULL;
  559. char *nmsg = NULL;
  560. /*
  561. * This CPU is the Monarch and the other CPUs have run
  562. * through their handlers.
  563. * Grade the severity of the errors of all the CPUs.
  564. */
  565. for_each_possible_cpu(cpu) {
  566. int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
  567. &nmsg);
  568. if (severity > global_worst) {
  569. msg = nmsg;
  570. global_worst = severity;
  571. m = &per_cpu(mces_seen, cpu);
  572. }
  573. }
  574. /*
  575. * Cannot recover? Panic here then.
  576. * This dumps all the mces in the log buffer and stops the
  577. * other CPUs.
  578. */
  579. if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
  580. mce_panic("Fatal Machine check", m, msg);
  581. /*
  582. * For UC somewhere we let the CPU who detects it handle it.
  583. * Also must let continue the others, otherwise the handling
  584. * CPU could deadlock on a lock.
  585. */
  586. /*
  587. * No machine check event found. Must be some external
  588. * source or one CPU is hung. Panic.
  589. */
  590. if (!m && tolerant < 3)
  591. mce_panic("Machine check from unknown source", NULL, NULL);
  592. /*
  593. * Now clear all the mces_seen so that they don't reappear on
  594. * the next mce.
  595. */
  596. for_each_possible_cpu(cpu)
  597. memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
  598. }
  599. static atomic_t global_nwo;
  600. /*
  601. * Start of Monarch synchronization. This waits until all CPUs have
  602. * entered the exception handler and then determines if any of them
  603. * saw a fatal event that requires panic. Then it executes them
  604. * in the entry order.
  605. * TBD double check parallel CPU hotunplug
  606. */
  607. static int mce_start(int no_way_out, int *order)
  608. {
  609. int nwo;
  610. int cpus = num_online_cpus();
  611. u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
  612. if (!timeout) {
  613. *order = -1;
  614. return no_way_out;
  615. }
  616. atomic_add(no_way_out, &global_nwo);
  617. /*
  618. * Wait for everyone.
  619. */
  620. while (atomic_read(&mce_callin) != cpus) {
  621. if (mce_timed_out(&timeout)) {
  622. atomic_set(&global_nwo, 0);
  623. *order = -1;
  624. return no_way_out;
  625. }
  626. ndelay(SPINUNIT);
  627. }
  628. /*
  629. * Cache the global no_way_out state.
  630. */
  631. nwo = atomic_read(&global_nwo);
  632. /*
  633. * Monarch starts executing now, the others wait.
  634. */
  635. if (*order == 1) {
  636. atomic_set(&mce_executing, 1);
  637. return nwo;
  638. }
  639. /*
  640. * Now start the scanning loop one by one
  641. * in the original callin order.
  642. * This way when there are any shared banks it will
  643. * be only seen by one CPU before cleared, avoiding duplicates.
  644. */
  645. while (atomic_read(&mce_executing) < *order) {
  646. if (mce_timed_out(&timeout)) {
  647. atomic_set(&global_nwo, 0);
  648. *order = -1;
  649. return no_way_out;
  650. }
  651. ndelay(SPINUNIT);
  652. }
  653. return nwo;
  654. }
  655. /*
  656. * Synchronize between CPUs after main scanning loop.
  657. * This invokes the bulk of the Monarch processing.
  658. */
  659. static int mce_end(int order)
  660. {
  661. int ret = -1;
  662. u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
  663. if (!timeout)
  664. goto reset;
  665. if (order < 0)
  666. goto reset;
  667. /*
  668. * Allow others to run.
  669. */
  670. atomic_inc(&mce_executing);
  671. if (order == 1) {
  672. /* CHECKME: Can this race with a parallel hotplug? */
  673. int cpus = num_online_cpus();
  674. /*
  675. * Monarch: Wait for everyone to go through their scanning
  676. * loops.
  677. */
  678. while (atomic_read(&mce_executing) <= cpus) {
  679. if (mce_timed_out(&timeout))
  680. goto reset;
  681. ndelay(SPINUNIT);
  682. }
  683. mce_reign();
  684. barrier();
  685. ret = 0;
  686. } else {
  687. /*
  688. * Subject: Wait for Monarch to finish.
  689. */
  690. while (atomic_read(&mce_executing) != 0) {
  691. if (mce_timed_out(&timeout))
  692. goto reset;
  693. ndelay(SPINUNIT);
  694. }
  695. /*
  696. * Don't reset anything. That's done by the Monarch.
  697. */
  698. return 0;
  699. }
  700. /*
  701. * Reset all global state.
  702. */
  703. reset:
  704. atomic_set(&global_nwo, 0);
  705. atomic_set(&mce_callin, 0);
  706. barrier();
  707. /*
  708. * Let others run again.
  709. */
  710. atomic_set(&mce_executing, 0);
  711. return ret;
  712. }
  713. /*
  714. * Check if the address reported by the CPU is in a format we can parse.
  715. * It would be possible to add code for most other cases, but all would
  716. * be somewhat complicated (e.g. segment offset would require an instruction
  717. * parser). So only support physical addresses upto page granuality for now.
  718. */
  719. static int mce_usable_address(struct mce *m)
  720. {
  721. if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
  722. return 0;
  723. if ((m->misc & 0x3f) > PAGE_SHIFT)
  724. return 0;
  725. if (((m->misc >> 6) & 7) != MCM_ADDR_PHYS)
  726. return 0;
  727. return 1;
  728. }
  729. static void mce_clear_state(unsigned long *toclear)
  730. {
  731. int i;
  732. for (i = 0; i < banks; i++) {
  733. if (test_bit(i, toclear))
  734. mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  735. }
  736. }
  737. /*
  738. * The actual machine check handler. This only handles real
  739. * exceptions when something got corrupted coming in through int 18.
  740. *
  741. * This is executed in NMI context not subject to normal locking rules. This
  742. * implies that most kernel services cannot be safely used. Don't even
  743. * think about putting a printk in there!
  744. *
  745. * On Intel systems this is entered on all CPUs in parallel through
  746. * MCE broadcast. However some CPUs might be broken beyond repair,
  747. * so be always careful when synchronizing with others.
  748. */
  749. void do_machine_check(struct pt_regs *regs, long error_code)
  750. {
  751. struct mce m, *final;
  752. int i;
  753. int worst = 0;
  754. int severity;
  755. /*
  756. * Establish sequential order between the CPUs entering the machine
  757. * check handler.
  758. */
  759. int order;
  760. /*
  761. * If no_way_out gets set, there is no safe way to recover from this
  762. * MCE. If tolerant is cranked up, we'll try anyway.
  763. */
  764. int no_way_out = 0;
  765. /*
  766. * If kill_it gets set, there might be a way to recover from this
  767. * error.
  768. */
  769. int kill_it = 0;
  770. DECLARE_BITMAP(toclear, MAX_NR_BANKS);
  771. char *msg = "Unknown";
  772. atomic_inc(&mce_entry);
  773. __get_cpu_var(mce_exception_count)++;
  774. if (notify_die(DIE_NMI, "machine check", regs, error_code,
  775. 18, SIGKILL) == NOTIFY_STOP)
  776. goto out;
  777. if (!banks)
  778. goto out;
  779. order = atomic_add_return(1, &mce_callin);
  780. mce_setup(&m);
  781. m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  782. no_way_out = mce_no_way_out(&m, &msg);
  783. final = &__get_cpu_var(mces_seen);
  784. *final = m;
  785. barrier();
  786. /*
  787. * When no restart IP must always kill or panic.
  788. */
  789. if (!(m.mcgstatus & MCG_STATUS_RIPV))
  790. kill_it = 1;
  791. /*
  792. * Go through all the banks in exclusion of the other CPUs.
  793. * This way we don't report duplicated events on shared banks
  794. * because the first one to see it will clear it.
  795. */
  796. no_way_out = mce_start(no_way_out, &order);
  797. for (i = 0; i < banks; i++) {
  798. __clear_bit(i, toclear);
  799. if (!bank[i])
  800. continue;
  801. m.misc = 0;
  802. m.addr = 0;
  803. m.bank = i;
  804. m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
  805. if ((m.status & MCI_STATUS_VAL) == 0)
  806. continue;
  807. /*
  808. * Non uncorrected or non signaled errors are handled by
  809. * machine_check_poll. Leave them alone, unless this panics.
  810. */
  811. if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
  812. !no_way_out)
  813. continue;
  814. /*
  815. * Set taint even when machine check was not enabled.
  816. */
  817. add_taint(TAINT_MACHINE_CHECK);
  818. severity = mce_severity(&m, tolerant, NULL);
  819. /*
  820. * When machine check was for corrected handler don't touch,
  821. * unless we're panicing.
  822. */
  823. if (severity == MCE_KEEP_SEVERITY && !no_way_out)
  824. continue;
  825. __set_bit(i, toclear);
  826. if (severity == MCE_NO_SEVERITY) {
  827. /*
  828. * Machine check event was not enabled. Clear, but
  829. * ignore.
  830. */
  831. continue;
  832. }
  833. /*
  834. * Kill on action required.
  835. */
  836. if (severity == MCE_AR_SEVERITY)
  837. kill_it = 1;
  838. if (m.status & MCI_STATUS_MISCV)
  839. m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
  840. if (m.status & MCI_STATUS_ADDRV)
  841. m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
  842. /*
  843. * Action optional error. Queue address for later processing.
  844. * When the ring overflows we just ignore the AO error.
  845. * RED-PEN add some logging mechanism when
  846. * usable_address or mce_add_ring fails.
  847. * RED-PEN don't ignore overflow for tolerant == 0
  848. */
  849. if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
  850. mce_ring_add(m.addr >> PAGE_SHIFT);
  851. mce_get_rip(&m, regs);
  852. mce_log(&m);
  853. if (severity > worst) {
  854. *final = m;
  855. worst = severity;
  856. }
  857. }
  858. if (!no_way_out)
  859. mce_clear_state(toclear);
  860. /*
  861. * Do most of the synchronization with other CPUs.
  862. * When there's any problem use only local no_way_out state.
  863. */
  864. if (mce_end(order) < 0)
  865. no_way_out = worst >= MCE_PANIC_SEVERITY;
  866. /*
  867. * If we have decided that we just CAN'T continue, and the user
  868. * has not set tolerant to an insane level, give up and die.
  869. *
  870. * This is mainly used in the case when the system doesn't
  871. * support MCE broadcasting or it has been disabled.
  872. */
  873. if (no_way_out && tolerant < 3)
  874. mce_panic("Fatal machine check on current CPU", final, msg);
  875. /*
  876. * If the error seems to be unrecoverable, something should be
  877. * done. Try to kill as little as possible. If we can kill just
  878. * one task, do that. If the user has set the tolerance very
  879. * high, don't try to do anything at all.
  880. */
  881. if (kill_it && tolerant < 3)
  882. force_sig(SIGBUS, current);
  883. /* notify userspace ASAP */
  884. set_thread_flag(TIF_MCE_NOTIFY);
  885. if (worst > 0)
  886. mce_report_event(regs);
  887. mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
  888. out:
  889. atomic_dec(&mce_entry);
  890. sync_core();
  891. }
  892. EXPORT_SYMBOL_GPL(do_machine_check);
  893. /* dummy to break dependency. actual code is in mm/memory-failure.c */
  894. void __attribute__((weak)) memory_failure(unsigned long pfn, int vector)
  895. {
  896. printk(KERN_ERR "Action optional memory failure at %lx ignored\n", pfn);
  897. }
  898. /*
  899. * Called after mce notification in process context. This code
  900. * is allowed to sleep. Call the high level VM handler to process
  901. * any corrupted pages.
  902. * Assume that the work queue code only calls this one at a time
  903. * per CPU.
  904. * Note we don't disable preemption, so this code might run on the wrong
  905. * CPU. In this case the event is picked up by the scheduled work queue.
  906. * This is merely a fast path to expedite processing in some common
  907. * cases.
  908. */
  909. void mce_notify_process(void)
  910. {
  911. unsigned long pfn;
  912. mce_notify_irq();
  913. while (mce_ring_get(&pfn))
  914. memory_failure(pfn, MCE_VECTOR);
  915. }
  916. static void mce_process_work(struct work_struct *dummy)
  917. {
  918. mce_notify_process();
  919. }
  920. #ifdef CONFIG_X86_MCE_INTEL
  921. /***
  922. * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
  923. * @cpu: The CPU on which the event occurred.
  924. * @status: Event status information
  925. *
  926. * This function should be called by the thermal interrupt after the
  927. * event has been processed and the decision was made to log the event
  928. * further.
  929. *
  930. * The status parameter will be saved to the 'status' field of 'struct mce'
  931. * and historically has been the register value of the
  932. * MSR_IA32_THERMAL_STATUS (Intel) msr.
  933. */
  934. void mce_log_therm_throt_event(__u64 status)
  935. {
  936. struct mce m;
  937. mce_setup(&m);
  938. m.bank = MCE_THERMAL_BANK;
  939. m.status = status;
  940. mce_log(&m);
  941. }
  942. #endif /* CONFIG_X86_MCE_INTEL */
  943. /*
  944. * Periodic polling timer for "silent" machine check errors. If the
  945. * poller finds an MCE, poll 2x faster. When the poller finds no more
  946. * errors, poll 2x slower (up to check_interval seconds).
  947. */
  948. static int check_interval = 5 * 60; /* 5 minutes */
  949. static DEFINE_PER_CPU(int, next_interval); /* in jiffies */
  950. static DEFINE_PER_CPU(struct timer_list, mce_timer);
  951. static void mcheck_timer(unsigned long data)
  952. {
  953. struct timer_list *t = &per_cpu(mce_timer, data);
  954. int *n;
  955. WARN_ON(smp_processor_id() != data);
  956. if (mce_available(&current_cpu_data)) {
  957. machine_check_poll(MCP_TIMESTAMP,
  958. &__get_cpu_var(mce_poll_banks));
  959. }
  960. /*
  961. * Alert userspace if needed. If we logged an MCE, reduce the
  962. * polling interval, otherwise increase the polling interval.
  963. */
  964. n = &__get_cpu_var(next_interval);
  965. if (mce_notify_irq())
  966. *n = max(*n/2, HZ/100);
  967. else
  968. *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
  969. t->expires = jiffies + *n;
  970. add_timer(t);
  971. }
  972. static void mce_do_trigger(struct work_struct *work)
  973. {
  974. call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT);
  975. }
  976. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  977. /*
  978. * Notify the user(s) about new machine check events.
  979. * Can be called from interrupt context, but not from machine check/NMI
  980. * context.
  981. */
  982. int mce_notify_irq(void)
  983. {
  984. /* Not more than two messages every minute */
  985. static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  986. clear_thread_flag(TIF_MCE_NOTIFY);
  987. if (test_and_clear_bit(0, &notify_user)) {
  988. wake_up_interruptible(&mce_wait);
  989. /*
  990. * There is no risk of missing notifications because
  991. * work_pending is always cleared before the function is
  992. * executed.
  993. */
  994. if (trigger[0] && !work_pending(&mce_trigger_work))
  995. schedule_work(&mce_trigger_work);
  996. if (__ratelimit(&ratelimit))
  997. printk(KERN_INFO "Machine check events logged\n");
  998. return 1;
  999. }
  1000. return 0;
  1001. }
  1002. EXPORT_SYMBOL_GPL(mce_notify_irq);
  1003. /*
  1004. * Initialize Machine Checks for a CPU.
  1005. */
  1006. static int mce_cap_init(void)
  1007. {
  1008. unsigned b;
  1009. u64 cap;
  1010. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1011. b = cap & MCG_BANKCNT_MASK;
  1012. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
  1013. if (b > MAX_NR_BANKS) {
  1014. printk(KERN_WARNING
  1015. "MCE: Using only %u machine check banks out of %u\n",
  1016. MAX_NR_BANKS, b);
  1017. b = MAX_NR_BANKS;
  1018. }
  1019. /* Don't support asymmetric configurations today */
  1020. WARN_ON(banks != 0 && b != banks);
  1021. banks = b;
  1022. if (!bank) {
  1023. bank = kmalloc(banks * sizeof(u64), GFP_KERNEL);
  1024. if (!bank)
  1025. return -ENOMEM;
  1026. memset(bank, 0xff, banks * sizeof(u64));
  1027. }
  1028. /* Use accurate RIP reporting if available. */
  1029. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  1030. rip_msr = MSR_IA32_MCG_EIP;
  1031. if (cap & MCG_SER_P)
  1032. mce_ser = 1;
  1033. return 0;
  1034. }
  1035. static void mce_init(void)
  1036. {
  1037. mce_banks_t all_banks;
  1038. u64 cap;
  1039. int i;
  1040. /*
  1041. * Log the machine checks left over from the previous reset.
  1042. */
  1043. bitmap_fill(all_banks, MAX_NR_BANKS);
  1044. machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
  1045. set_in_cr4(X86_CR4_MCE);
  1046. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1047. if (cap & MCG_CTL_P)
  1048. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  1049. for (i = 0; i < banks; i++) {
  1050. if (skip_bank_init(i))
  1051. continue;
  1052. wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]);
  1053. wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  1054. }
  1055. }
  1056. /* Add per CPU specific workarounds here */
  1057. static void mce_cpu_quirks(struct cpuinfo_x86 *c)
  1058. {
  1059. /* This should be disabled by the BIOS, but isn't always */
  1060. if (c->x86_vendor == X86_VENDOR_AMD) {
  1061. if (c->x86 == 15 && banks > 4) {
  1062. /*
  1063. * disable GART TBL walk error reporting, which
  1064. * trips off incorrectly with the IOMMU & 3ware
  1065. * & Cerberus:
  1066. */
  1067. clear_bit(10, (unsigned long *)&bank[4]);
  1068. }
  1069. if (c->x86 <= 17 && mce_bootlog < 0) {
  1070. /*
  1071. * Lots of broken BIOS around that don't clear them
  1072. * by default and leave crap in there. Don't log:
  1073. */
  1074. mce_bootlog = 0;
  1075. }
  1076. /*
  1077. * Various K7s with broken bank 0 around. Always disable
  1078. * by default.
  1079. */
  1080. if (c->x86 == 6)
  1081. bank[0] = 0;
  1082. }
  1083. if (c->x86_vendor == X86_VENDOR_INTEL) {
  1084. /*
  1085. * SDM documents that on family 6 bank 0 should not be written
  1086. * because it aliases to another special BIOS controlled
  1087. * register.
  1088. * But it's not aliased anymore on model 0x1a+
  1089. * Don't ignore bank 0 completely because there could be a
  1090. * valid event later, merely don't write CTL0.
  1091. */
  1092. if (c->x86 == 6 && c->x86_model < 0x1A)
  1093. __set_bit(0, &dont_init_banks);
  1094. /*
  1095. * All newer Intel systems support MCE broadcasting. Enable
  1096. * synchronization with a one second timeout.
  1097. */
  1098. if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
  1099. monarch_timeout < 0)
  1100. monarch_timeout = USEC_PER_SEC;
  1101. }
  1102. if (monarch_timeout < 0)
  1103. monarch_timeout = 0;
  1104. if (mce_bootlog != 0)
  1105. mce_panic_timeout = 30;
  1106. }
  1107. static void __cpuinit mce_ancient_init(struct cpuinfo_x86 *c)
  1108. {
  1109. if (c->x86 != 5)
  1110. return;
  1111. switch (c->x86_vendor) {
  1112. case X86_VENDOR_INTEL:
  1113. if (mce_p5_enabled())
  1114. intel_p5_mcheck_init(c);
  1115. break;
  1116. case X86_VENDOR_CENTAUR:
  1117. winchip_mcheck_init(c);
  1118. break;
  1119. }
  1120. }
  1121. static void mce_cpu_features(struct cpuinfo_x86 *c)
  1122. {
  1123. switch (c->x86_vendor) {
  1124. case X86_VENDOR_INTEL:
  1125. mce_intel_feature_init(c);
  1126. break;
  1127. case X86_VENDOR_AMD:
  1128. mce_amd_feature_init(c);
  1129. break;
  1130. default:
  1131. break;
  1132. }
  1133. }
  1134. static void mce_init_timer(void)
  1135. {
  1136. struct timer_list *t = &__get_cpu_var(mce_timer);
  1137. int *n = &__get_cpu_var(next_interval);
  1138. *n = check_interval * HZ;
  1139. if (!*n)
  1140. return;
  1141. setup_timer(t, mcheck_timer, smp_processor_id());
  1142. t->expires = round_jiffies(jiffies + *n);
  1143. add_timer(t);
  1144. }
  1145. /*
  1146. * Called for each booted CPU to set up machine checks.
  1147. * Must be called with preempt off:
  1148. */
  1149. void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
  1150. {
  1151. if (mce_disabled)
  1152. return;
  1153. mce_ancient_init(c);
  1154. if (!mce_available(c))
  1155. return;
  1156. if (mce_cap_init() < 0) {
  1157. mce_disabled = 1;
  1158. return;
  1159. }
  1160. mce_cpu_quirks(c);
  1161. machine_check_vector = do_machine_check;
  1162. mce_init();
  1163. mce_cpu_features(c);
  1164. mce_init_timer();
  1165. INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
  1166. }
  1167. /*
  1168. * Character device to read and clear the MCE log.
  1169. */
  1170. static DEFINE_SPINLOCK(mce_state_lock);
  1171. static int open_count; /* #times opened */
  1172. static int open_exclu; /* already open exclusive? */
  1173. static int mce_open(struct inode *inode, struct file *file)
  1174. {
  1175. spin_lock(&mce_state_lock);
  1176. if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
  1177. spin_unlock(&mce_state_lock);
  1178. return -EBUSY;
  1179. }
  1180. if (file->f_flags & O_EXCL)
  1181. open_exclu = 1;
  1182. open_count++;
  1183. spin_unlock(&mce_state_lock);
  1184. return nonseekable_open(inode, file);
  1185. }
  1186. static int mce_release(struct inode *inode, struct file *file)
  1187. {
  1188. spin_lock(&mce_state_lock);
  1189. open_count--;
  1190. open_exclu = 0;
  1191. spin_unlock(&mce_state_lock);
  1192. return 0;
  1193. }
  1194. static void collect_tscs(void *data)
  1195. {
  1196. unsigned long *cpu_tsc = (unsigned long *)data;
  1197. rdtscll(cpu_tsc[smp_processor_id()]);
  1198. }
  1199. static DEFINE_MUTEX(mce_read_mutex);
  1200. static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
  1201. loff_t *off)
  1202. {
  1203. char __user *buf = ubuf;
  1204. unsigned long *cpu_tsc;
  1205. unsigned prev, next;
  1206. int i, err;
  1207. cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
  1208. if (!cpu_tsc)
  1209. return -ENOMEM;
  1210. mutex_lock(&mce_read_mutex);
  1211. next = rcu_dereference(mcelog.next);
  1212. /* Only supports full reads right now */
  1213. if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) {
  1214. mutex_unlock(&mce_read_mutex);
  1215. kfree(cpu_tsc);
  1216. return -EINVAL;
  1217. }
  1218. err = 0;
  1219. prev = 0;
  1220. do {
  1221. for (i = prev; i < next; i++) {
  1222. unsigned long start = jiffies;
  1223. while (!mcelog.entry[i].finished) {
  1224. if (time_after_eq(jiffies, start + 2)) {
  1225. memset(mcelog.entry + i, 0,
  1226. sizeof(struct mce));
  1227. goto timeout;
  1228. }
  1229. cpu_relax();
  1230. }
  1231. smp_rmb();
  1232. err |= copy_to_user(buf, mcelog.entry + i,
  1233. sizeof(struct mce));
  1234. buf += sizeof(struct mce);
  1235. timeout:
  1236. ;
  1237. }
  1238. memset(mcelog.entry + prev, 0,
  1239. (next - prev) * sizeof(struct mce));
  1240. prev = next;
  1241. next = cmpxchg(&mcelog.next, prev, 0);
  1242. } while (next != prev);
  1243. synchronize_sched();
  1244. /*
  1245. * Collect entries that were still getting written before the
  1246. * synchronize.
  1247. */
  1248. on_each_cpu(collect_tscs, cpu_tsc, 1);
  1249. for (i = next; i < MCE_LOG_LEN; i++) {
  1250. if (mcelog.entry[i].finished &&
  1251. mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
  1252. err |= copy_to_user(buf, mcelog.entry+i,
  1253. sizeof(struct mce));
  1254. smp_rmb();
  1255. buf += sizeof(struct mce);
  1256. memset(&mcelog.entry[i], 0, sizeof(struct mce));
  1257. }
  1258. }
  1259. mutex_unlock(&mce_read_mutex);
  1260. kfree(cpu_tsc);
  1261. return err ? -EFAULT : buf - ubuf;
  1262. }
  1263. static unsigned int mce_poll(struct file *file, poll_table *wait)
  1264. {
  1265. poll_wait(file, &mce_wait, wait);
  1266. if (rcu_dereference(mcelog.next))
  1267. return POLLIN | POLLRDNORM;
  1268. return 0;
  1269. }
  1270. static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  1271. {
  1272. int __user *p = (int __user *)arg;
  1273. if (!capable(CAP_SYS_ADMIN))
  1274. return -EPERM;
  1275. switch (cmd) {
  1276. case MCE_GET_RECORD_LEN:
  1277. return put_user(sizeof(struct mce), p);
  1278. case MCE_GET_LOG_LEN:
  1279. return put_user(MCE_LOG_LEN, p);
  1280. case MCE_GETCLEAR_FLAGS: {
  1281. unsigned flags;
  1282. do {
  1283. flags = mcelog.flags;
  1284. } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
  1285. return put_user(flags, p);
  1286. }
  1287. default:
  1288. return -ENOTTY;
  1289. }
  1290. }
  1291. /* Modified in mce-inject.c, so not static or const */
  1292. struct file_operations mce_chrdev_ops = {
  1293. .open = mce_open,
  1294. .release = mce_release,
  1295. .read = mce_read,
  1296. .poll = mce_poll,
  1297. .unlocked_ioctl = mce_ioctl,
  1298. };
  1299. EXPORT_SYMBOL_GPL(mce_chrdev_ops);
  1300. static struct miscdevice mce_log_device = {
  1301. MISC_MCELOG_MINOR,
  1302. "mcelog",
  1303. &mce_chrdev_ops,
  1304. };
  1305. /*
  1306. * mce=off disables machine check
  1307. * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
  1308. * monarchtimeout is how long to wait for other CPUs on machine
  1309. * check, or 0 to not wait
  1310. * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
  1311. * mce=nobootlog Don't log MCEs from before booting.
  1312. */
  1313. static int __init mcheck_enable(char *str)
  1314. {
  1315. if (*str == 0)
  1316. enable_p5_mce();
  1317. if (*str == '=')
  1318. str++;
  1319. if (!strcmp(str, "off"))
  1320. mce_disabled = 1;
  1321. else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
  1322. mce_bootlog = (str[0] == 'b');
  1323. else if (isdigit(str[0])) {
  1324. get_option(&str, &tolerant);
  1325. if (*str == ',') {
  1326. ++str;
  1327. get_option(&str, &monarch_timeout);
  1328. }
  1329. } else {
  1330. printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
  1331. str);
  1332. return 0;
  1333. }
  1334. return 1;
  1335. }
  1336. __setup("mce", mcheck_enable);
  1337. /*
  1338. * Sysfs support
  1339. */
  1340. /*
  1341. * Disable machine checks on suspend and shutdown. We can't really handle
  1342. * them later.
  1343. */
  1344. static int mce_disable(void)
  1345. {
  1346. int i;
  1347. for (i = 0; i < banks; i++) {
  1348. if (!skip_bank_init(i))
  1349. wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
  1350. }
  1351. return 0;
  1352. }
  1353. static int mce_suspend(struct sys_device *dev, pm_message_t state)
  1354. {
  1355. return mce_disable();
  1356. }
  1357. static int mce_shutdown(struct sys_device *dev)
  1358. {
  1359. return mce_disable();
  1360. }
  1361. /*
  1362. * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
  1363. * Only one CPU is active at this time, the others get re-added later using
  1364. * CPU hotplug:
  1365. */
  1366. static int mce_resume(struct sys_device *dev)
  1367. {
  1368. mce_init();
  1369. mce_cpu_features(&current_cpu_data);
  1370. return 0;
  1371. }
  1372. static void mce_cpu_restart(void *data)
  1373. {
  1374. del_timer_sync(&__get_cpu_var(mce_timer));
  1375. if (mce_available(&current_cpu_data))
  1376. mce_init();
  1377. mce_init_timer();
  1378. }
  1379. /* Reinit MCEs after user configuration changes */
  1380. static void mce_restart(void)
  1381. {
  1382. on_each_cpu(mce_cpu_restart, NULL, 1);
  1383. }
  1384. static struct sysdev_class mce_sysclass = {
  1385. .suspend = mce_suspend,
  1386. .shutdown = mce_shutdown,
  1387. .resume = mce_resume,
  1388. .name = "machinecheck",
  1389. };
  1390. DEFINE_PER_CPU(struct sys_device, mce_dev);
  1391. __cpuinitdata
  1392. void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
  1393. static struct sysdev_attribute *bank_attrs;
  1394. static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
  1395. char *buf)
  1396. {
  1397. u64 b = bank[attr - bank_attrs];
  1398. return sprintf(buf, "%llx\n", b);
  1399. }
  1400. static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
  1401. const char *buf, size_t size)
  1402. {
  1403. u64 new;
  1404. if (strict_strtoull(buf, 0, &new) < 0)
  1405. return -EINVAL;
  1406. bank[attr - bank_attrs] = new;
  1407. mce_restart();
  1408. return size;
  1409. }
  1410. static ssize_t
  1411. show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
  1412. {
  1413. strcpy(buf, trigger);
  1414. strcat(buf, "\n");
  1415. return strlen(trigger) + 1;
  1416. }
  1417. static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
  1418. const char *buf, size_t siz)
  1419. {
  1420. char *p;
  1421. int len;
  1422. strncpy(trigger, buf, sizeof(trigger));
  1423. trigger[sizeof(trigger)-1] = 0;
  1424. len = strlen(trigger);
  1425. p = strchr(trigger, '\n');
  1426. if (*p)
  1427. *p = 0;
  1428. return len;
  1429. }
  1430. static ssize_t store_int_with_restart(struct sys_device *s,
  1431. struct sysdev_attribute *attr,
  1432. const char *buf, size_t size)
  1433. {
  1434. ssize_t ret = sysdev_store_int(s, attr, buf, size);
  1435. mce_restart();
  1436. return ret;
  1437. }
  1438. static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
  1439. static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
  1440. static SYSDEV_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
  1441. static struct sysdev_ext_attribute attr_check_interval = {
  1442. _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int,
  1443. store_int_with_restart),
  1444. &check_interval
  1445. };
  1446. static struct sysdev_attribute *mce_attrs[] = {
  1447. &attr_tolerant.attr, &attr_check_interval.attr, &attr_trigger,
  1448. &attr_monarch_timeout.attr,
  1449. NULL
  1450. };
  1451. static cpumask_var_t mce_dev_initialized;
  1452. /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
  1453. static __cpuinit int mce_create_device(unsigned int cpu)
  1454. {
  1455. int err;
  1456. int i;
  1457. if (!mce_available(&boot_cpu_data))
  1458. return -EIO;
  1459. memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
  1460. per_cpu(mce_dev, cpu).id = cpu;
  1461. per_cpu(mce_dev, cpu).cls = &mce_sysclass;
  1462. err = sysdev_register(&per_cpu(mce_dev, cpu));
  1463. if (err)
  1464. return err;
  1465. for (i = 0; mce_attrs[i]; i++) {
  1466. err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1467. if (err)
  1468. goto error;
  1469. }
  1470. for (i = 0; i < banks; i++) {
  1471. err = sysdev_create_file(&per_cpu(mce_dev, cpu),
  1472. &bank_attrs[i]);
  1473. if (err)
  1474. goto error2;
  1475. }
  1476. cpumask_set_cpu(cpu, mce_dev_initialized);
  1477. return 0;
  1478. error2:
  1479. while (--i >= 0)
  1480. sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
  1481. error:
  1482. while (--i >= 0)
  1483. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1484. sysdev_unregister(&per_cpu(mce_dev, cpu));
  1485. return err;
  1486. }
  1487. static __cpuinit void mce_remove_device(unsigned int cpu)
  1488. {
  1489. int i;
  1490. if (!cpumask_test_cpu(cpu, mce_dev_initialized))
  1491. return;
  1492. for (i = 0; mce_attrs[i]; i++)
  1493. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1494. for (i = 0; i < banks; i++)
  1495. sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
  1496. sysdev_unregister(&per_cpu(mce_dev, cpu));
  1497. cpumask_clear_cpu(cpu, mce_dev_initialized);
  1498. }
  1499. /* Make sure there are no machine checks on offlined CPUs. */
  1500. static void mce_disable_cpu(void *h)
  1501. {
  1502. unsigned long action = *(unsigned long *)h;
  1503. int i;
  1504. if (!mce_available(&current_cpu_data))
  1505. return;
  1506. if (!(action & CPU_TASKS_FROZEN))
  1507. cmci_clear();
  1508. for (i = 0; i < banks; i++) {
  1509. if (!skip_bank_init(i))
  1510. wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
  1511. }
  1512. }
  1513. static void mce_reenable_cpu(void *h)
  1514. {
  1515. unsigned long action = *(unsigned long *)h;
  1516. int i;
  1517. if (!mce_available(&current_cpu_data))
  1518. return;
  1519. if (!(action & CPU_TASKS_FROZEN))
  1520. cmci_reenable();
  1521. for (i = 0; i < banks; i++) {
  1522. if (!skip_bank_init(i))
  1523. wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]);
  1524. }
  1525. }
  1526. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  1527. static int __cpuinit
  1528. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  1529. {
  1530. unsigned int cpu = (unsigned long)hcpu;
  1531. struct timer_list *t = &per_cpu(mce_timer, cpu);
  1532. switch (action) {
  1533. case CPU_ONLINE:
  1534. case CPU_ONLINE_FROZEN:
  1535. mce_create_device(cpu);
  1536. if (threshold_cpu_callback)
  1537. threshold_cpu_callback(action, cpu);
  1538. break;
  1539. case CPU_DEAD:
  1540. case CPU_DEAD_FROZEN:
  1541. if (threshold_cpu_callback)
  1542. threshold_cpu_callback(action, cpu);
  1543. mce_remove_device(cpu);
  1544. break;
  1545. case CPU_DOWN_PREPARE:
  1546. case CPU_DOWN_PREPARE_FROZEN:
  1547. del_timer_sync(t);
  1548. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  1549. break;
  1550. case CPU_DOWN_FAILED:
  1551. case CPU_DOWN_FAILED_FROZEN:
  1552. t->expires = round_jiffies(jiffies +
  1553. __get_cpu_var(next_interval));
  1554. add_timer_on(t, cpu);
  1555. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  1556. break;
  1557. case CPU_POST_DEAD:
  1558. /* intentionally ignoring frozen here */
  1559. cmci_rediscover(cpu);
  1560. break;
  1561. }
  1562. return NOTIFY_OK;
  1563. }
  1564. static struct notifier_block mce_cpu_notifier __cpuinitdata = {
  1565. .notifier_call = mce_cpu_callback,
  1566. };
  1567. static __init int mce_init_banks(void)
  1568. {
  1569. int i;
  1570. bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks,
  1571. GFP_KERNEL);
  1572. if (!bank_attrs)
  1573. return -ENOMEM;
  1574. for (i = 0; i < banks; i++) {
  1575. struct sysdev_attribute *a = &bank_attrs[i];
  1576. a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i);
  1577. if (!a->attr.name)
  1578. goto nomem;
  1579. a->attr.mode = 0644;
  1580. a->show = show_bank;
  1581. a->store = set_bank;
  1582. }
  1583. return 0;
  1584. nomem:
  1585. while (--i >= 0)
  1586. kfree(bank_attrs[i].attr.name);
  1587. kfree(bank_attrs);
  1588. bank_attrs = NULL;
  1589. return -ENOMEM;
  1590. }
  1591. static __init int mce_init_device(void)
  1592. {
  1593. int err;
  1594. int i = 0;
  1595. if (!mce_available(&boot_cpu_data))
  1596. return -EIO;
  1597. alloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
  1598. err = mce_init_banks();
  1599. if (err)
  1600. return err;
  1601. err = sysdev_class_register(&mce_sysclass);
  1602. if (err)
  1603. return err;
  1604. for_each_online_cpu(i) {
  1605. err = mce_create_device(i);
  1606. if (err)
  1607. return err;
  1608. }
  1609. register_hotcpu_notifier(&mce_cpu_notifier);
  1610. misc_register(&mce_log_device);
  1611. return err;
  1612. }
  1613. device_initcall(mce_init_device);
  1614. #else /* CONFIG_X86_OLD_MCE: */
  1615. int nr_mce_banks;
  1616. EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */
  1617. /* This has to be run for each processor */
  1618. void mcheck_init(struct cpuinfo_x86 *c)
  1619. {
  1620. if (mce_disabled == 1)
  1621. return;
  1622. switch (c->x86_vendor) {
  1623. case X86_VENDOR_AMD:
  1624. amd_mcheck_init(c);
  1625. break;
  1626. case X86_VENDOR_INTEL:
  1627. if (c->x86 == 5)
  1628. intel_p5_mcheck_init(c);
  1629. if (c->x86 == 6)
  1630. intel_p6_mcheck_init(c);
  1631. if (c->x86 == 15)
  1632. intel_p4_mcheck_init(c);
  1633. break;
  1634. case X86_VENDOR_CENTAUR:
  1635. if (c->x86 == 5)
  1636. winchip_mcheck_init(c);
  1637. break;
  1638. default:
  1639. break;
  1640. }
  1641. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks);
  1642. }
  1643. static int __init mcheck_enable(char *str)
  1644. {
  1645. mce_disabled = -1;
  1646. return 1;
  1647. }
  1648. __setup("mce", mcheck_enable);
  1649. #endif /* CONFIG_X86_OLD_MCE */
  1650. /*
  1651. * Old style boot options parsing. Only for compatibility.
  1652. */
  1653. static int __init mcheck_disable(char *str)
  1654. {
  1655. mce_disabled = 1;
  1656. return 1;
  1657. }
  1658. __setup("nomce", mcheck_disable);