mce.c 49 KB

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