mce.c 50 KB

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