mce.c 49 KB

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