mce.c 50 KB

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