mce.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  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/edac_mce.h>
  40. #include <linux/irq_work.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. /* Emit the trace record: */
  125. trace_mce_record(mce);
  126. mce->finished = 0;
  127. wmb();
  128. for (;;) {
  129. entry = rcu_dereference_check_mce(mcelog.next);
  130. for (;;) {
  131. /*
  132. * If edac_mce is enabled, it will check the error type
  133. * and will process it, if it is a known error.
  134. * Otherwise, the error will be sent through mcelog
  135. * interface
  136. */
  137. if (edac_mce_parse(mce))
  138. return;
  139. /*
  140. * When the buffer fills up discard new entries.
  141. * Assume that the earlier errors are the more
  142. * interesting ones:
  143. */
  144. if (entry >= MCE_LOG_LEN) {
  145. set_bit(MCE_OVERFLOW,
  146. (unsigned long *)&mcelog.flags);
  147. return;
  148. }
  149. /* Old left over entry. Skip: */
  150. if (mcelog.entry[entry].finished) {
  151. entry++;
  152. continue;
  153. }
  154. break;
  155. }
  156. smp_rmb();
  157. next = entry + 1;
  158. if (cmpxchg(&mcelog.next, entry, next) == entry)
  159. break;
  160. }
  161. memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
  162. wmb();
  163. mcelog.entry[entry].finished = 1;
  164. wmb();
  165. mce->finished = 1;
  166. set_bit(0, &mce_need_notify);
  167. }
  168. static void print_mce(struct mce *m)
  169. {
  170. int ret = 0;
  171. pr_emerg(HW_ERR "CPU %d: Machine Check Exception: %Lx Bank %d: %016Lx\n",
  172. m->extcpu, m->mcgstatus, m->bank, m->status);
  173. if (m->ip) {
  174. pr_emerg(HW_ERR "RIP%s %02x:<%016Lx> ",
  175. !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
  176. m->cs, m->ip);
  177. if (m->cs == __KERNEL_CS)
  178. print_symbol("{%s}", m->ip);
  179. pr_cont("\n");
  180. }
  181. pr_emerg(HW_ERR "TSC %llx ", m->tsc);
  182. if (m->addr)
  183. pr_cont("ADDR %llx ", m->addr);
  184. if (m->misc)
  185. pr_cont("MISC %llx ", m->misc);
  186. pr_cont("\n");
  187. pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
  188. m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid);
  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. atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, &m);
  478. }
  479. /*
  480. * Clear state for this bank.
  481. */
  482. mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  483. }
  484. /*
  485. * Don't clear MCG_STATUS here because it's only defined for
  486. * exceptions.
  487. */
  488. sync_core();
  489. }
  490. EXPORT_SYMBOL_GPL(machine_check_poll);
  491. /*
  492. * Do a quick check if any of the events requires a panic.
  493. * This decides if we keep the events around or clear them.
  494. */
  495. static int mce_no_way_out(struct mce *m, char **msg)
  496. {
  497. int i;
  498. for (i = 0; i < banks; i++) {
  499. m->status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
  500. if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
  501. return 1;
  502. }
  503. return 0;
  504. }
  505. /*
  506. * Variable to establish order between CPUs while scanning.
  507. * Each CPU spins initially until executing is equal its number.
  508. */
  509. static atomic_t mce_executing;
  510. /*
  511. * Defines order of CPUs on entry. First CPU becomes Monarch.
  512. */
  513. static atomic_t mce_callin;
  514. /*
  515. * Check if a timeout waiting for other CPUs happened.
  516. */
  517. static int mce_timed_out(u64 *t)
  518. {
  519. /*
  520. * The others already did panic for some reason.
  521. * Bail out like in a timeout.
  522. * rmb() to tell the compiler that system_state
  523. * might have been modified by someone else.
  524. */
  525. rmb();
  526. if (atomic_read(&mce_paniced))
  527. wait_for_panic();
  528. if (!monarch_timeout)
  529. goto out;
  530. if ((s64)*t < SPINUNIT) {
  531. /* CHECKME: Make panic default for 1 too? */
  532. if (tolerant < 1)
  533. mce_panic("Timeout synchronizing machine check over CPUs",
  534. NULL, NULL);
  535. cpu_missing = 1;
  536. return 1;
  537. }
  538. *t -= SPINUNIT;
  539. out:
  540. touch_nmi_watchdog();
  541. return 0;
  542. }
  543. /*
  544. * The Monarch's reign. The Monarch is the CPU who entered
  545. * the machine check handler first. It waits for the others to
  546. * raise the exception too and then grades them. When any
  547. * error is fatal panic. Only then let the others continue.
  548. *
  549. * The other CPUs entering the MCE handler will be controlled by the
  550. * Monarch. They are called Subjects.
  551. *
  552. * This way we prevent any potential data corruption in a unrecoverable case
  553. * and also makes sure always all CPU's errors are examined.
  554. *
  555. * Also this detects the case of a machine check event coming from outer
  556. * space (not detected by any CPUs) In this case some external agent wants
  557. * us to shut down, so panic too.
  558. *
  559. * The other CPUs might still decide to panic if the handler happens
  560. * in a unrecoverable place, but in this case the system is in a semi-stable
  561. * state and won't corrupt anything by itself. It's ok to let the others
  562. * continue for a bit first.
  563. *
  564. * All the spin loops have timeouts; when a timeout happens a CPU
  565. * typically elects itself to be Monarch.
  566. */
  567. static void mce_reign(void)
  568. {
  569. int cpu;
  570. struct mce *m = NULL;
  571. int global_worst = 0;
  572. char *msg = NULL;
  573. char *nmsg = NULL;
  574. /*
  575. * This CPU is the Monarch and the other CPUs have run
  576. * through their handlers.
  577. * Grade the severity of the errors of all the CPUs.
  578. */
  579. for_each_possible_cpu(cpu) {
  580. int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
  581. &nmsg);
  582. if (severity > global_worst) {
  583. msg = nmsg;
  584. global_worst = severity;
  585. m = &per_cpu(mces_seen, cpu);
  586. }
  587. }
  588. /*
  589. * Cannot recover? Panic here then.
  590. * This dumps all the mces in the log buffer and stops the
  591. * other CPUs.
  592. */
  593. if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
  594. mce_panic("Fatal Machine check", m, msg);
  595. /*
  596. * For UC somewhere we let the CPU who detects it handle it.
  597. * Also must let continue the others, otherwise the handling
  598. * CPU could deadlock on a lock.
  599. */
  600. /*
  601. * No machine check event found. Must be some external
  602. * source or one CPU is hung. Panic.
  603. */
  604. if (global_worst <= MCE_KEEP_SEVERITY && tolerant < 3)
  605. mce_panic("Machine check from unknown source", NULL, NULL);
  606. /*
  607. * Now clear all the mces_seen so that they don't reappear on
  608. * the next mce.
  609. */
  610. for_each_possible_cpu(cpu)
  611. memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
  612. }
  613. static atomic_t global_nwo;
  614. /*
  615. * Start of Monarch synchronization. This waits until all CPUs have
  616. * entered the exception handler and then determines if any of them
  617. * saw a fatal event that requires panic. Then it executes them
  618. * in the entry order.
  619. * TBD double check parallel CPU hotunplug
  620. */
  621. static int mce_start(int *no_way_out)
  622. {
  623. int order;
  624. int cpus = num_online_cpus();
  625. u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
  626. if (!timeout)
  627. return -1;
  628. atomic_add(*no_way_out, &global_nwo);
  629. /*
  630. * global_nwo should be updated before mce_callin
  631. */
  632. smp_wmb();
  633. order = atomic_inc_return(&mce_callin);
  634. /*
  635. * Wait for everyone.
  636. */
  637. while (atomic_read(&mce_callin) != cpus) {
  638. if (mce_timed_out(&timeout)) {
  639. atomic_set(&global_nwo, 0);
  640. return -1;
  641. }
  642. ndelay(SPINUNIT);
  643. }
  644. /*
  645. * mce_callin should be read before global_nwo
  646. */
  647. smp_rmb();
  648. if (order == 1) {
  649. /*
  650. * Monarch: Starts executing now, the others wait.
  651. */
  652. atomic_set(&mce_executing, 1);
  653. } else {
  654. /*
  655. * Subject: Now start the scanning loop one by one in
  656. * the original callin order.
  657. * This way when there are any shared banks it will be
  658. * only seen by one CPU before cleared, avoiding duplicates.
  659. */
  660. while (atomic_read(&mce_executing) < order) {
  661. if (mce_timed_out(&timeout)) {
  662. atomic_set(&global_nwo, 0);
  663. return -1;
  664. }
  665. ndelay(SPINUNIT);
  666. }
  667. }
  668. /*
  669. * Cache the global no_way_out state.
  670. */
  671. *no_way_out = atomic_read(&global_nwo);
  672. return order;
  673. }
  674. /*
  675. * Synchronize between CPUs after main scanning loop.
  676. * This invokes the bulk of the Monarch processing.
  677. */
  678. static int mce_end(int order)
  679. {
  680. int ret = -1;
  681. u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
  682. if (!timeout)
  683. goto reset;
  684. if (order < 0)
  685. goto reset;
  686. /*
  687. * Allow others to run.
  688. */
  689. atomic_inc(&mce_executing);
  690. if (order == 1) {
  691. /* CHECKME: Can this race with a parallel hotplug? */
  692. int cpus = num_online_cpus();
  693. /*
  694. * Monarch: Wait for everyone to go through their scanning
  695. * loops.
  696. */
  697. while (atomic_read(&mce_executing) <= cpus) {
  698. if (mce_timed_out(&timeout))
  699. goto reset;
  700. ndelay(SPINUNIT);
  701. }
  702. mce_reign();
  703. barrier();
  704. ret = 0;
  705. } else {
  706. /*
  707. * Subject: Wait for Monarch to finish.
  708. */
  709. while (atomic_read(&mce_executing) != 0) {
  710. if (mce_timed_out(&timeout))
  711. goto reset;
  712. ndelay(SPINUNIT);
  713. }
  714. /*
  715. * Don't reset anything. That's done by the Monarch.
  716. */
  717. return 0;
  718. }
  719. /*
  720. * Reset all global state.
  721. */
  722. reset:
  723. atomic_set(&global_nwo, 0);
  724. atomic_set(&mce_callin, 0);
  725. barrier();
  726. /*
  727. * Let others run again.
  728. */
  729. atomic_set(&mce_executing, 0);
  730. return ret;
  731. }
  732. /*
  733. * Check if the address reported by the CPU is in a format we can parse.
  734. * It would be possible to add code for most other cases, but all would
  735. * be somewhat complicated (e.g. segment offset would require an instruction
  736. * parser). So only support physical addresses up to page granuality for now.
  737. */
  738. static int mce_usable_address(struct mce *m)
  739. {
  740. if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
  741. return 0;
  742. if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
  743. return 0;
  744. if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
  745. return 0;
  746. return 1;
  747. }
  748. static void mce_clear_state(unsigned long *toclear)
  749. {
  750. int i;
  751. for (i = 0; i < banks; i++) {
  752. if (test_bit(i, toclear))
  753. mce_wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  754. }
  755. }
  756. /*
  757. * The actual machine check handler. This only handles real
  758. * exceptions when something got corrupted coming in through int 18.
  759. *
  760. * This is executed in NMI context not subject to normal locking rules. This
  761. * implies that most kernel services cannot be safely used. Don't even
  762. * think about putting a printk in there!
  763. *
  764. * On Intel systems this is entered on all CPUs in parallel through
  765. * MCE broadcast. However some CPUs might be broken beyond repair,
  766. * so be always careful when synchronizing with others.
  767. */
  768. void do_machine_check(struct pt_regs *regs, long error_code)
  769. {
  770. struct mce m, *final;
  771. int i;
  772. int worst = 0;
  773. int severity;
  774. /*
  775. * Establish sequential order between the CPUs entering the machine
  776. * check handler.
  777. */
  778. int order;
  779. /*
  780. * If no_way_out gets set, there is no safe way to recover from this
  781. * MCE. If tolerant is cranked up, we'll try anyway.
  782. */
  783. int no_way_out = 0;
  784. /*
  785. * If kill_it gets set, there might be a way to recover from this
  786. * error.
  787. */
  788. int kill_it = 0;
  789. DECLARE_BITMAP(toclear, MAX_NR_BANKS);
  790. char *msg = "Unknown";
  791. atomic_inc(&mce_entry);
  792. percpu_inc(mce_exception_count);
  793. if (notify_die(DIE_NMI, "machine check", regs, error_code,
  794. 18, SIGKILL) == NOTIFY_STOP)
  795. goto out;
  796. if (!banks)
  797. goto out;
  798. mce_gather_info(&m, regs);
  799. final = &__get_cpu_var(mces_seen);
  800. *final = m;
  801. no_way_out = mce_no_way_out(&m, &msg);
  802. barrier();
  803. /*
  804. * When no restart IP must always kill or panic.
  805. */
  806. if (!(m.mcgstatus & MCG_STATUS_RIPV))
  807. kill_it = 1;
  808. /*
  809. * Go through all the banks in exclusion of the other CPUs.
  810. * This way we don't report duplicated events on shared banks
  811. * because the first one to see it will clear it.
  812. */
  813. order = mce_start(&no_way_out);
  814. for (i = 0; i < banks; i++) {
  815. __clear_bit(i, toclear);
  816. if (!mce_banks[i].ctl)
  817. continue;
  818. m.misc = 0;
  819. m.addr = 0;
  820. m.bank = i;
  821. m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
  822. if ((m.status & MCI_STATUS_VAL) == 0)
  823. continue;
  824. /*
  825. * Non uncorrected or non signaled errors are handled by
  826. * machine_check_poll. Leave them alone, unless this panics.
  827. */
  828. if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
  829. !no_way_out)
  830. continue;
  831. /*
  832. * Set taint even when machine check was not enabled.
  833. */
  834. add_taint(TAINT_MACHINE_CHECK);
  835. severity = mce_severity(&m, tolerant, NULL);
  836. /*
  837. * When machine check was for corrected handler don't touch,
  838. * unless we're panicing.
  839. */
  840. if (severity == MCE_KEEP_SEVERITY && !no_way_out)
  841. continue;
  842. __set_bit(i, toclear);
  843. if (severity == MCE_NO_SEVERITY) {
  844. /*
  845. * Machine check event was not enabled. Clear, but
  846. * ignore.
  847. */
  848. continue;
  849. }
  850. /*
  851. * Kill on action required.
  852. */
  853. if (severity == MCE_AR_SEVERITY)
  854. kill_it = 1;
  855. if (m.status & MCI_STATUS_MISCV)
  856. m.misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
  857. if (m.status & MCI_STATUS_ADDRV)
  858. m.addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
  859. /*
  860. * Action optional error. Queue address for later processing.
  861. * When the ring overflows we just ignore the AO error.
  862. * RED-PEN add some logging mechanism when
  863. * usable_address or mce_add_ring fails.
  864. * RED-PEN don't ignore overflow for tolerant == 0
  865. */
  866. if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
  867. mce_ring_add(m.addr >> PAGE_SHIFT);
  868. mce_log(&m);
  869. if (severity > worst) {
  870. *final = m;
  871. worst = severity;
  872. }
  873. }
  874. if (!no_way_out)
  875. mce_clear_state(toclear);
  876. /*
  877. * Do most of the synchronization with other CPUs.
  878. * When there's any problem use only local no_way_out state.
  879. */
  880. if (mce_end(order) < 0)
  881. no_way_out = worst >= MCE_PANIC_SEVERITY;
  882. /*
  883. * If we have decided that we just CAN'T continue, and the user
  884. * has not set tolerant to an insane level, give up and die.
  885. *
  886. * This is mainly used in the case when the system doesn't
  887. * support MCE broadcasting or it has been disabled.
  888. */
  889. if (no_way_out && tolerant < 3)
  890. mce_panic("Fatal machine check on current CPU", final, msg);
  891. /*
  892. * If the error seems to be unrecoverable, something should be
  893. * done. Try to kill as little as possible. If we can kill just
  894. * one task, do that. If the user has set the tolerance very
  895. * high, don't try to do anything at all.
  896. */
  897. if (kill_it && tolerant < 3)
  898. force_sig(SIGBUS, current);
  899. /* notify userspace ASAP */
  900. set_thread_flag(TIF_MCE_NOTIFY);
  901. if (worst > 0)
  902. mce_report_event(regs);
  903. mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
  904. out:
  905. atomic_dec(&mce_entry);
  906. sync_core();
  907. }
  908. EXPORT_SYMBOL_GPL(do_machine_check);
  909. /* dummy to break dependency. actual code is in mm/memory-failure.c */
  910. void __attribute__((weak)) memory_failure(unsigned long pfn, int vector)
  911. {
  912. printk(KERN_ERR "Action optional memory failure at %lx ignored\n", pfn);
  913. }
  914. /*
  915. * Called after mce notification in process context. This code
  916. * is allowed to sleep. Call the high level VM handler to process
  917. * any corrupted pages.
  918. * Assume that the work queue code only calls this one at a time
  919. * per CPU.
  920. * Note we don't disable preemption, so this code might run on the wrong
  921. * CPU. In this case the event is picked up by the scheduled work queue.
  922. * This is merely a fast path to expedite processing in some common
  923. * cases.
  924. */
  925. void mce_notify_process(void)
  926. {
  927. unsigned long pfn;
  928. mce_notify_irq();
  929. while (mce_ring_get(&pfn))
  930. memory_failure(pfn, MCE_VECTOR);
  931. }
  932. static void mce_process_work(struct work_struct *dummy)
  933. {
  934. mce_notify_process();
  935. }
  936. #ifdef CONFIG_X86_MCE_INTEL
  937. /***
  938. * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
  939. * @cpu: The CPU on which the event occurred.
  940. * @status: Event status information
  941. *
  942. * This function should be called by the thermal interrupt after the
  943. * event has been processed and the decision was made to log the event
  944. * further.
  945. *
  946. * The status parameter will be saved to the 'status' field of 'struct mce'
  947. * and historically has been the register value of the
  948. * MSR_IA32_THERMAL_STATUS (Intel) msr.
  949. */
  950. void mce_log_therm_throt_event(__u64 status)
  951. {
  952. struct mce m;
  953. mce_setup(&m);
  954. m.bank = MCE_THERMAL_BANK;
  955. m.status = status;
  956. mce_log(&m);
  957. }
  958. #endif /* CONFIG_X86_MCE_INTEL */
  959. /*
  960. * Periodic polling timer for "silent" machine check errors. If the
  961. * poller finds an MCE, poll 2x faster. When the poller finds no more
  962. * errors, poll 2x slower (up to check_interval seconds).
  963. */
  964. static int check_interval = 5 * 60; /* 5 minutes */
  965. static DEFINE_PER_CPU(int, mce_next_interval); /* in jiffies */
  966. static DEFINE_PER_CPU(struct timer_list, mce_timer);
  967. static void mce_start_timer(unsigned long data)
  968. {
  969. struct timer_list *t = &per_cpu(mce_timer, data);
  970. int *n;
  971. WARN_ON(smp_processor_id() != data);
  972. if (mce_available(__this_cpu_ptr(&cpu_info))) {
  973. machine_check_poll(MCP_TIMESTAMP,
  974. &__get_cpu_var(mce_poll_banks));
  975. }
  976. /*
  977. * Alert userspace if needed. If we logged an MCE, reduce the
  978. * polling interval, otherwise increase the polling interval.
  979. */
  980. n = &__get_cpu_var(mce_next_interval);
  981. if (mce_notify_irq())
  982. *n = max(*n/2, HZ/100);
  983. else
  984. *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
  985. t->expires = jiffies + *n;
  986. add_timer_on(t, smp_processor_id());
  987. }
  988. static void mce_do_trigger(struct work_struct *work)
  989. {
  990. call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
  991. }
  992. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  993. /*
  994. * Notify the user(s) about new machine check events.
  995. * Can be called from interrupt context, but not from machine check/NMI
  996. * context.
  997. */
  998. int mce_notify_irq(void)
  999. {
  1000. /* Not more than two messages every minute */
  1001. static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  1002. clear_thread_flag(TIF_MCE_NOTIFY);
  1003. if (test_and_clear_bit(0, &mce_need_notify)) {
  1004. /* wake processes polling /dev/mcelog */
  1005. wake_up_interruptible(&mce_chrdev_wait);
  1006. /*
  1007. * There is no risk of missing notifications because
  1008. * work_pending is always cleared before the function is
  1009. * executed.
  1010. */
  1011. if (mce_helper[0] && !work_pending(&mce_trigger_work))
  1012. schedule_work(&mce_trigger_work);
  1013. if (__ratelimit(&ratelimit))
  1014. pr_info(HW_ERR "Machine check events logged\n");
  1015. return 1;
  1016. }
  1017. return 0;
  1018. }
  1019. EXPORT_SYMBOL_GPL(mce_notify_irq);
  1020. static int __cpuinit __mcheck_cpu_mce_banks_init(void)
  1021. {
  1022. int i;
  1023. mce_banks = kzalloc(banks * sizeof(struct mce_bank), GFP_KERNEL);
  1024. if (!mce_banks)
  1025. return -ENOMEM;
  1026. for (i = 0; i < banks; i++) {
  1027. struct mce_bank *b = &mce_banks[i];
  1028. b->ctl = -1ULL;
  1029. b->init = 1;
  1030. }
  1031. return 0;
  1032. }
  1033. /*
  1034. * Initialize Machine Checks for a CPU.
  1035. */
  1036. static int __cpuinit __mcheck_cpu_cap_init(void)
  1037. {
  1038. unsigned b;
  1039. u64 cap;
  1040. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1041. b = cap & MCG_BANKCNT_MASK;
  1042. if (!banks)
  1043. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
  1044. if (b > MAX_NR_BANKS) {
  1045. printk(KERN_WARNING
  1046. "MCE: Using only %u machine check banks out of %u\n",
  1047. MAX_NR_BANKS, b);
  1048. b = MAX_NR_BANKS;
  1049. }
  1050. /* Don't support asymmetric configurations today */
  1051. WARN_ON(banks != 0 && b != banks);
  1052. banks = b;
  1053. if (!mce_banks) {
  1054. int err = __mcheck_cpu_mce_banks_init();
  1055. if (err)
  1056. return err;
  1057. }
  1058. /* Use accurate RIP reporting if available. */
  1059. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  1060. rip_msr = MSR_IA32_MCG_EIP;
  1061. if (cap & MCG_SER_P)
  1062. mce_ser = 1;
  1063. return 0;
  1064. }
  1065. static void __mcheck_cpu_init_generic(void)
  1066. {
  1067. mce_banks_t all_banks;
  1068. u64 cap;
  1069. int i;
  1070. /*
  1071. * Log the machine checks left over from the previous reset.
  1072. */
  1073. bitmap_fill(all_banks, MAX_NR_BANKS);
  1074. machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
  1075. set_in_cr4(X86_CR4_MCE);
  1076. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1077. if (cap & MCG_CTL_P)
  1078. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  1079. for (i = 0; i < banks; i++) {
  1080. struct mce_bank *b = &mce_banks[i];
  1081. if (!b->init)
  1082. continue;
  1083. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  1084. wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  1085. }
  1086. }
  1087. /* Add per CPU specific workarounds here */
  1088. static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
  1089. {
  1090. if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
  1091. pr_info("MCE: unknown CPU type - not enabling MCE support.\n");
  1092. return -EOPNOTSUPP;
  1093. }
  1094. /* This should be disabled by the BIOS, but isn't always */
  1095. if (c->x86_vendor == X86_VENDOR_AMD) {
  1096. if (c->x86 == 15 && banks > 4) {
  1097. /*
  1098. * disable GART TBL walk error reporting, which
  1099. * trips off incorrectly with the IOMMU & 3ware
  1100. * & Cerberus:
  1101. */
  1102. clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
  1103. }
  1104. if (c->x86 <= 17 && mce_bootlog < 0) {
  1105. /*
  1106. * Lots of broken BIOS around that don't clear them
  1107. * by default and leave crap in there. Don't log:
  1108. */
  1109. mce_bootlog = 0;
  1110. }
  1111. /*
  1112. * Various K7s with broken bank 0 around. Always disable
  1113. * by default.
  1114. */
  1115. if (c->x86 == 6 && banks > 0)
  1116. mce_banks[0].ctl = 0;
  1117. }
  1118. if (c->x86_vendor == X86_VENDOR_INTEL) {
  1119. /*
  1120. * SDM documents that on family 6 bank 0 should not be written
  1121. * because it aliases to another special BIOS controlled
  1122. * register.
  1123. * But it's not aliased anymore on model 0x1a+
  1124. * Don't ignore bank 0 completely because there could be a
  1125. * valid event later, merely don't write CTL0.
  1126. */
  1127. if (c->x86 == 6 && c->x86_model < 0x1A && banks > 0)
  1128. mce_banks[0].init = 0;
  1129. /*
  1130. * All newer Intel systems support MCE broadcasting. Enable
  1131. * synchronization with a one second timeout.
  1132. */
  1133. if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
  1134. monarch_timeout < 0)
  1135. monarch_timeout = USEC_PER_SEC;
  1136. /*
  1137. * There are also broken BIOSes on some Pentium M and
  1138. * earlier systems:
  1139. */
  1140. if (c->x86 == 6 && c->x86_model <= 13 && mce_bootlog < 0)
  1141. mce_bootlog = 0;
  1142. }
  1143. if (monarch_timeout < 0)
  1144. monarch_timeout = 0;
  1145. if (mce_bootlog != 0)
  1146. mce_panic_timeout = 30;
  1147. return 0;
  1148. }
  1149. static int __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
  1150. {
  1151. if (c->x86 != 5)
  1152. return 0;
  1153. switch (c->x86_vendor) {
  1154. case X86_VENDOR_INTEL:
  1155. intel_p5_mcheck_init(c);
  1156. return 1;
  1157. break;
  1158. case X86_VENDOR_CENTAUR:
  1159. winchip_mcheck_init(c);
  1160. return 1;
  1161. break;
  1162. }
  1163. return 0;
  1164. }
  1165. static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
  1166. {
  1167. switch (c->x86_vendor) {
  1168. case X86_VENDOR_INTEL:
  1169. mce_intel_feature_init(c);
  1170. break;
  1171. case X86_VENDOR_AMD:
  1172. mce_amd_feature_init(c);
  1173. break;
  1174. default:
  1175. break;
  1176. }
  1177. }
  1178. static void __mcheck_cpu_init_timer(void)
  1179. {
  1180. struct timer_list *t = &__get_cpu_var(mce_timer);
  1181. int *n = &__get_cpu_var(mce_next_interval);
  1182. setup_timer(t, mce_start_timer, smp_processor_id());
  1183. if (mce_ignore_ce)
  1184. return;
  1185. *n = check_interval * HZ;
  1186. if (!*n)
  1187. return;
  1188. t->expires = round_jiffies(jiffies + *n);
  1189. add_timer_on(t, smp_processor_id());
  1190. }
  1191. /* Handle unconfigured int18 (should never happen) */
  1192. static void unexpected_machine_check(struct pt_regs *regs, long error_code)
  1193. {
  1194. printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
  1195. smp_processor_id());
  1196. }
  1197. /* Call the installed machine check handler for this CPU setup. */
  1198. void (*machine_check_vector)(struct pt_regs *, long error_code) =
  1199. unexpected_machine_check;
  1200. /*
  1201. * Called for each booted CPU to set up machine checks.
  1202. * Must be called with preempt off:
  1203. */
  1204. void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
  1205. {
  1206. if (mce_disabled)
  1207. return;
  1208. if (__mcheck_cpu_ancient_init(c))
  1209. return;
  1210. if (!mce_available(c))
  1211. return;
  1212. if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
  1213. mce_disabled = 1;
  1214. return;
  1215. }
  1216. machine_check_vector = do_machine_check;
  1217. __mcheck_cpu_init_generic();
  1218. __mcheck_cpu_init_vendor(c);
  1219. __mcheck_cpu_init_timer();
  1220. INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
  1221. init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
  1222. }
  1223. /*
  1224. * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
  1225. */
  1226. static DEFINE_SPINLOCK(mce_chrdev_state_lock);
  1227. static int mce_chrdev_open_count; /* #times opened */
  1228. static int mce_chrdev_open_exclu; /* already open exclusive? */
  1229. static int mce_chrdev_open(struct inode *inode, struct file *file)
  1230. {
  1231. spin_lock(&mce_chrdev_state_lock);
  1232. if (mce_chrdev_open_exclu ||
  1233. (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
  1234. spin_unlock(&mce_chrdev_state_lock);
  1235. return -EBUSY;
  1236. }
  1237. if (file->f_flags & O_EXCL)
  1238. mce_chrdev_open_exclu = 1;
  1239. mce_chrdev_open_count++;
  1240. spin_unlock(&mce_chrdev_state_lock);
  1241. return nonseekable_open(inode, file);
  1242. }
  1243. static int mce_chrdev_release(struct inode *inode, struct file *file)
  1244. {
  1245. spin_lock(&mce_chrdev_state_lock);
  1246. mce_chrdev_open_count--;
  1247. mce_chrdev_open_exclu = 0;
  1248. spin_unlock(&mce_chrdev_state_lock);
  1249. return 0;
  1250. }
  1251. static void collect_tscs(void *data)
  1252. {
  1253. unsigned long *cpu_tsc = (unsigned long *)data;
  1254. rdtscll(cpu_tsc[smp_processor_id()]);
  1255. }
  1256. static int mce_apei_read_done;
  1257. /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
  1258. static int __mce_read_apei(char __user **ubuf, size_t usize)
  1259. {
  1260. int rc;
  1261. u64 record_id;
  1262. struct mce m;
  1263. if (usize < sizeof(struct mce))
  1264. return -EINVAL;
  1265. rc = apei_read_mce(&m, &record_id);
  1266. /* Error or no more MCE record */
  1267. if (rc <= 0) {
  1268. mce_apei_read_done = 1;
  1269. return rc;
  1270. }
  1271. rc = -EFAULT;
  1272. if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
  1273. return rc;
  1274. /*
  1275. * In fact, we should have cleared the record after that has
  1276. * been flushed to the disk or sent to network in
  1277. * /sbin/mcelog, but we have no interface to support that now,
  1278. * so just clear it to avoid duplication.
  1279. */
  1280. rc = apei_clear_mce(record_id);
  1281. if (rc) {
  1282. mce_apei_read_done = 1;
  1283. return rc;
  1284. }
  1285. *ubuf += sizeof(struct mce);
  1286. return 0;
  1287. }
  1288. static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
  1289. size_t usize, loff_t *off)
  1290. {
  1291. char __user *buf = ubuf;
  1292. unsigned long *cpu_tsc;
  1293. unsigned prev, next;
  1294. int i, err;
  1295. cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
  1296. if (!cpu_tsc)
  1297. return -ENOMEM;
  1298. mutex_lock(&mce_chrdev_read_mutex);
  1299. if (!mce_apei_read_done) {
  1300. err = __mce_read_apei(&buf, usize);
  1301. if (err || buf != ubuf)
  1302. goto out;
  1303. }
  1304. next = rcu_dereference_check_mce(mcelog.next);
  1305. /* Only supports full reads right now */
  1306. err = -EINVAL;
  1307. if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
  1308. goto out;
  1309. err = 0;
  1310. prev = 0;
  1311. do {
  1312. for (i = prev; i < next; i++) {
  1313. unsigned long start = jiffies;
  1314. struct mce *m = &mcelog.entry[i];
  1315. while (!m->finished) {
  1316. if (time_after_eq(jiffies, start + 2)) {
  1317. memset(m, 0, sizeof(*m));
  1318. goto timeout;
  1319. }
  1320. cpu_relax();
  1321. }
  1322. smp_rmb();
  1323. err |= copy_to_user(buf, m, sizeof(*m));
  1324. buf += sizeof(*m);
  1325. timeout:
  1326. ;
  1327. }
  1328. memset(mcelog.entry + prev, 0,
  1329. (next - prev) * sizeof(struct mce));
  1330. prev = next;
  1331. next = cmpxchg(&mcelog.next, prev, 0);
  1332. } while (next != prev);
  1333. synchronize_sched();
  1334. /*
  1335. * Collect entries that were still getting written before the
  1336. * synchronize.
  1337. */
  1338. on_each_cpu(collect_tscs, cpu_tsc, 1);
  1339. for (i = next; i < MCE_LOG_LEN; i++) {
  1340. struct mce *m = &mcelog.entry[i];
  1341. if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
  1342. err |= copy_to_user(buf, m, sizeof(*m));
  1343. smp_rmb();
  1344. buf += sizeof(*m);
  1345. memset(m, 0, sizeof(*m));
  1346. }
  1347. }
  1348. if (err)
  1349. err = -EFAULT;
  1350. out:
  1351. mutex_unlock(&mce_chrdev_read_mutex);
  1352. kfree(cpu_tsc);
  1353. return err ? err : buf - ubuf;
  1354. }
  1355. static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
  1356. {
  1357. poll_wait(file, &mce_chrdev_wait, wait);
  1358. if (rcu_access_index(mcelog.next))
  1359. return POLLIN | POLLRDNORM;
  1360. if (!mce_apei_read_done && apei_check_mce())
  1361. return POLLIN | POLLRDNORM;
  1362. return 0;
  1363. }
  1364. static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
  1365. unsigned long arg)
  1366. {
  1367. int __user *p = (int __user *)arg;
  1368. if (!capable(CAP_SYS_ADMIN))
  1369. return -EPERM;
  1370. switch (cmd) {
  1371. case MCE_GET_RECORD_LEN:
  1372. return put_user(sizeof(struct mce), p);
  1373. case MCE_GET_LOG_LEN:
  1374. return put_user(MCE_LOG_LEN, p);
  1375. case MCE_GETCLEAR_FLAGS: {
  1376. unsigned flags;
  1377. do {
  1378. flags = mcelog.flags;
  1379. } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
  1380. return put_user(flags, p);
  1381. }
  1382. default:
  1383. return -ENOTTY;
  1384. }
  1385. }
  1386. /* Modified in mce-inject.c, so not static or const */
  1387. struct file_operations mce_chrdev_ops = {
  1388. .open = mce_chrdev_open,
  1389. .release = mce_chrdev_release,
  1390. .read = mce_chrdev_read,
  1391. .poll = mce_chrdev_poll,
  1392. .unlocked_ioctl = mce_chrdev_ioctl,
  1393. .llseek = no_llseek,
  1394. };
  1395. EXPORT_SYMBOL_GPL(mce_chrdev_ops);
  1396. static struct miscdevice mce_chrdev_device = {
  1397. MISC_MCELOG_MINOR,
  1398. "mcelog",
  1399. &mce_chrdev_ops,
  1400. };
  1401. /*
  1402. * mce=off Disables machine check
  1403. * mce=no_cmci Disables CMCI
  1404. * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
  1405. * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
  1406. * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
  1407. * monarchtimeout is how long to wait for other CPUs on machine
  1408. * check, or 0 to not wait
  1409. * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
  1410. * mce=nobootlog Don't log MCEs from before booting.
  1411. */
  1412. static int __init mcheck_enable(char *str)
  1413. {
  1414. if (*str == 0) {
  1415. enable_p5_mce();
  1416. return 1;
  1417. }
  1418. if (*str == '=')
  1419. str++;
  1420. if (!strcmp(str, "off"))
  1421. mce_disabled = 1;
  1422. else if (!strcmp(str, "no_cmci"))
  1423. mce_cmci_disabled = 1;
  1424. else if (!strcmp(str, "dont_log_ce"))
  1425. mce_dont_log_ce = 1;
  1426. else if (!strcmp(str, "ignore_ce"))
  1427. mce_ignore_ce = 1;
  1428. else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
  1429. mce_bootlog = (str[0] == 'b');
  1430. else if (isdigit(str[0])) {
  1431. get_option(&str, &tolerant);
  1432. if (*str == ',') {
  1433. ++str;
  1434. get_option(&str, &monarch_timeout);
  1435. }
  1436. } else {
  1437. printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
  1438. str);
  1439. return 0;
  1440. }
  1441. return 1;
  1442. }
  1443. __setup("mce", mcheck_enable);
  1444. int __init mcheck_init(void)
  1445. {
  1446. mcheck_intel_therm_init();
  1447. return 0;
  1448. }
  1449. /*
  1450. * mce_syscore: PM support
  1451. */
  1452. /*
  1453. * Disable machine checks on suspend and shutdown. We can't really handle
  1454. * them later.
  1455. */
  1456. static int mce_disable_error_reporting(void)
  1457. {
  1458. int i;
  1459. for (i = 0; i < banks; i++) {
  1460. struct mce_bank *b = &mce_banks[i];
  1461. if (b->init)
  1462. wrmsrl(MSR_IA32_MCx_CTL(i), 0);
  1463. }
  1464. return 0;
  1465. }
  1466. static int mce_syscore_suspend(void)
  1467. {
  1468. return mce_disable_error_reporting();
  1469. }
  1470. static void mce_syscore_shutdown(void)
  1471. {
  1472. mce_disable_error_reporting();
  1473. }
  1474. /*
  1475. * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
  1476. * Only one CPU is active at this time, the others get re-added later using
  1477. * CPU hotplug:
  1478. */
  1479. static void mce_syscore_resume(void)
  1480. {
  1481. __mcheck_cpu_init_generic();
  1482. __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
  1483. }
  1484. static struct syscore_ops mce_syscore_ops = {
  1485. .suspend = mce_syscore_suspend,
  1486. .shutdown = mce_syscore_shutdown,
  1487. .resume = mce_syscore_resume,
  1488. };
  1489. /*
  1490. * mce_sysdev: Sysfs support
  1491. */
  1492. static void mce_cpu_restart(void *data)
  1493. {
  1494. del_timer_sync(&__get_cpu_var(mce_timer));
  1495. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1496. return;
  1497. __mcheck_cpu_init_generic();
  1498. __mcheck_cpu_init_timer();
  1499. }
  1500. /* Reinit MCEs after user configuration changes */
  1501. static void mce_restart(void)
  1502. {
  1503. on_each_cpu(mce_cpu_restart, NULL, 1);
  1504. }
  1505. /* Toggle features for corrected errors */
  1506. static void mce_disable_ce(void *all)
  1507. {
  1508. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1509. return;
  1510. if (all)
  1511. del_timer_sync(&__get_cpu_var(mce_timer));
  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. on_each_cpu(mce_disable_ce, (void *)1, 1);
  1577. mce_ignore_ce = 1;
  1578. } else {
  1579. /* enable ce features */
  1580. mce_ignore_ce = 0;
  1581. on_each_cpu(mce_enable_ce, (void *)1, 1);
  1582. }
  1583. }
  1584. return size;
  1585. }
  1586. static ssize_t set_cmci_disabled(struct sys_device *s,
  1587. struct sysdev_attribute *attr,
  1588. const char *buf, size_t size)
  1589. {
  1590. u64 new;
  1591. if (strict_strtoull(buf, 0, &new) < 0)
  1592. return -EINVAL;
  1593. if (mce_cmci_disabled ^ !!new) {
  1594. if (new) {
  1595. /* disable cmci */
  1596. on_each_cpu(mce_disable_ce, NULL, 1);
  1597. mce_cmci_disabled = 1;
  1598. } else {
  1599. /* enable cmci */
  1600. mce_cmci_disabled = 0;
  1601. on_each_cpu(mce_enable_ce, NULL, 1);
  1602. }
  1603. }
  1604. return size;
  1605. }
  1606. static ssize_t store_int_with_restart(struct sys_device *s,
  1607. struct sysdev_attribute *attr,
  1608. const char *buf, size_t size)
  1609. {
  1610. ssize_t ret = sysdev_store_int(s, attr, buf, size);
  1611. mce_restart();
  1612. return ret;
  1613. }
  1614. static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
  1615. static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
  1616. static SYSDEV_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
  1617. static SYSDEV_INT_ATTR(dont_log_ce, 0644, mce_dont_log_ce);
  1618. static struct sysdev_ext_attribute attr_check_interval = {
  1619. _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int,
  1620. store_int_with_restart),
  1621. &check_interval
  1622. };
  1623. static struct sysdev_ext_attribute attr_ignore_ce = {
  1624. _SYSDEV_ATTR(ignore_ce, 0644, sysdev_show_int, set_ignore_ce),
  1625. &mce_ignore_ce
  1626. };
  1627. static struct sysdev_ext_attribute attr_cmci_disabled = {
  1628. _SYSDEV_ATTR(cmci_disabled, 0644, sysdev_show_int, set_cmci_disabled),
  1629. &mce_cmci_disabled
  1630. };
  1631. static struct sysdev_attribute *mce_sysdev_attrs[] = {
  1632. &attr_tolerant.attr,
  1633. &attr_check_interval.attr,
  1634. &attr_trigger,
  1635. &attr_monarch_timeout.attr,
  1636. &attr_dont_log_ce.attr,
  1637. &attr_ignore_ce.attr,
  1638. &attr_cmci_disabled.attr,
  1639. NULL
  1640. };
  1641. static cpumask_var_t mce_sysdev_initialized;
  1642. /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
  1643. static __cpuinit int mce_sysdev_create(unsigned int cpu)
  1644. {
  1645. struct sys_device *sysdev = &per_cpu(mce_sysdev, cpu);
  1646. int err;
  1647. int i, j;
  1648. if (!mce_available(&boot_cpu_data))
  1649. return -EIO;
  1650. memset(&sysdev->kobj, 0, sizeof(struct kobject));
  1651. sysdev->id = cpu;
  1652. sysdev->cls = &mce_sysdev_class;
  1653. err = sysdev_register(sysdev);
  1654. if (err)
  1655. return err;
  1656. for (i = 0; mce_sysdev_attrs[i]; i++) {
  1657. err = sysdev_create_file(sysdev, mce_sysdev_attrs[i]);
  1658. if (err)
  1659. goto error;
  1660. }
  1661. for (j = 0; j < banks; j++) {
  1662. err = sysdev_create_file(sysdev, &mce_banks[j].attr);
  1663. if (err)
  1664. goto error2;
  1665. }
  1666. cpumask_set_cpu(cpu, mce_sysdev_initialized);
  1667. return 0;
  1668. error2:
  1669. while (--j >= 0)
  1670. sysdev_remove_file(sysdev, &mce_banks[j].attr);
  1671. error:
  1672. while (--i >= 0)
  1673. sysdev_remove_file(sysdev, mce_sysdev_attrs[i]);
  1674. sysdev_unregister(sysdev);
  1675. return err;
  1676. }
  1677. static __cpuinit void mce_sysdev_remove(unsigned int cpu)
  1678. {
  1679. struct sys_device *sysdev = &per_cpu(mce_sysdev, cpu);
  1680. int i;
  1681. if (!cpumask_test_cpu(cpu, mce_sysdev_initialized))
  1682. return;
  1683. for (i = 0; mce_sysdev_attrs[i]; i++)
  1684. sysdev_remove_file(sysdev, mce_sysdev_attrs[i]);
  1685. for (i = 0; i < banks; i++)
  1686. sysdev_remove_file(sysdev, &mce_banks[i].attr);
  1687. sysdev_unregister(sysdev);
  1688. cpumask_clear_cpu(cpu, mce_sysdev_initialized);
  1689. }
  1690. /* Make sure there are no machine checks on offlined CPUs. */
  1691. static void __cpuinit mce_disable_cpu(void *h)
  1692. {
  1693. unsigned long action = *(unsigned long *)h;
  1694. int i;
  1695. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1696. return;
  1697. if (!(action & CPU_TASKS_FROZEN))
  1698. cmci_clear();
  1699. for (i = 0; i < banks; i++) {
  1700. struct mce_bank *b = &mce_banks[i];
  1701. if (b->init)
  1702. wrmsrl(MSR_IA32_MCx_CTL(i), 0);
  1703. }
  1704. }
  1705. static void __cpuinit mce_reenable_cpu(void *h)
  1706. {
  1707. unsigned long action = *(unsigned long *)h;
  1708. int i;
  1709. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1710. return;
  1711. if (!(action & CPU_TASKS_FROZEN))
  1712. cmci_reenable();
  1713. for (i = 0; i < banks; i++) {
  1714. struct mce_bank *b = &mce_banks[i];
  1715. if (b->init)
  1716. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  1717. }
  1718. }
  1719. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  1720. static int __cpuinit
  1721. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  1722. {
  1723. unsigned int cpu = (unsigned long)hcpu;
  1724. struct timer_list *t = &per_cpu(mce_timer, cpu);
  1725. switch (action) {
  1726. case CPU_ONLINE:
  1727. case CPU_ONLINE_FROZEN:
  1728. mce_sysdev_create(cpu);
  1729. if (threshold_cpu_callback)
  1730. threshold_cpu_callback(action, cpu);
  1731. break;
  1732. case CPU_DEAD:
  1733. case CPU_DEAD_FROZEN:
  1734. if (threshold_cpu_callback)
  1735. threshold_cpu_callback(action, cpu);
  1736. mce_sysdev_remove(cpu);
  1737. break;
  1738. case CPU_DOWN_PREPARE:
  1739. case CPU_DOWN_PREPARE_FROZEN:
  1740. del_timer_sync(t);
  1741. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  1742. break;
  1743. case CPU_DOWN_FAILED:
  1744. case CPU_DOWN_FAILED_FROZEN:
  1745. if (!mce_ignore_ce && check_interval) {
  1746. t->expires = round_jiffies(jiffies +
  1747. __get_cpu_var(mce_next_interval));
  1748. add_timer_on(t, cpu);
  1749. }
  1750. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  1751. break;
  1752. case CPU_POST_DEAD:
  1753. /* intentionally ignoring frozen here */
  1754. cmci_rediscover(cpu);
  1755. break;
  1756. }
  1757. return NOTIFY_OK;
  1758. }
  1759. static struct notifier_block mce_cpu_notifier __cpuinitdata = {
  1760. .notifier_call = mce_cpu_callback,
  1761. };
  1762. static __init void mce_init_banks(void)
  1763. {
  1764. int i;
  1765. for (i = 0; i < banks; i++) {
  1766. struct mce_bank *b = &mce_banks[i];
  1767. struct sysdev_attribute *a = &b->attr;
  1768. sysfs_attr_init(&a->attr);
  1769. a->attr.name = b->attrname;
  1770. snprintf(b->attrname, ATTR_LEN, "bank%d", i);
  1771. a->attr.mode = 0644;
  1772. a->show = show_bank;
  1773. a->store = set_bank;
  1774. }
  1775. }
  1776. static __init int mcheck_init_device(void)
  1777. {
  1778. int err;
  1779. int i = 0;
  1780. if (!mce_available(&boot_cpu_data))
  1781. return -EIO;
  1782. zalloc_cpumask_var(&mce_sysdev_initialized, GFP_KERNEL);
  1783. mce_init_banks();
  1784. err = sysdev_class_register(&mce_sysdev_class);
  1785. if (err)
  1786. return err;
  1787. for_each_online_cpu(i) {
  1788. err = mce_sysdev_create(i);
  1789. if (err)
  1790. return err;
  1791. }
  1792. register_syscore_ops(&mce_syscore_ops);
  1793. register_hotcpu_notifier(&mce_cpu_notifier);
  1794. /* register character device /dev/mcelog */
  1795. misc_register(&mce_chrdev_device);
  1796. return err;
  1797. }
  1798. device_initcall(mcheck_init_device);
  1799. /*
  1800. * Old style boot options parsing. Only for compatibility.
  1801. */
  1802. static int __init mcheck_disable(char *str)
  1803. {
  1804. mce_disabled = 1;
  1805. return 1;
  1806. }
  1807. __setup("nomce", mcheck_disable);
  1808. #ifdef CONFIG_DEBUG_FS
  1809. struct dentry *mce_get_debugfs_dir(void)
  1810. {
  1811. static struct dentry *dmce;
  1812. if (!dmce)
  1813. dmce = debugfs_create_dir("mce", NULL);
  1814. return dmce;
  1815. }
  1816. static void mce_reset(void)
  1817. {
  1818. cpu_missing = 0;
  1819. atomic_set(&mce_fake_paniced, 0);
  1820. atomic_set(&mce_executing, 0);
  1821. atomic_set(&mce_callin, 0);
  1822. atomic_set(&global_nwo, 0);
  1823. }
  1824. static int fake_panic_get(void *data, u64 *val)
  1825. {
  1826. *val = fake_panic;
  1827. return 0;
  1828. }
  1829. static int fake_panic_set(void *data, u64 val)
  1830. {
  1831. mce_reset();
  1832. fake_panic = val;
  1833. return 0;
  1834. }
  1835. DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
  1836. fake_panic_set, "%llu\n");
  1837. static int __init mcheck_debugfs_init(void)
  1838. {
  1839. struct dentry *dmce, *ffake_panic;
  1840. dmce = mce_get_debugfs_dir();
  1841. if (!dmce)
  1842. return -ENOMEM;
  1843. ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
  1844. &fake_panic_fops);
  1845. if (!ffake_panic)
  1846. return -ENOMEM;
  1847. return 0;
  1848. }
  1849. late_initcall(mcheck_debugfs_init);
  1850. #endif