mce.c 46 KB

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