mce.c 44 KB

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