mce.c 50 KB

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