mce.c 51 KB

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