mce.c 50 KB

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